EvEmu  0.8.4
11 September 2021
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
Base64.cpp
Go to the documentation of this file.
1 
5 /*
6 Copyright (C) 2004,2005 Anders Hedstrom
7 
8 This library is made available under the terms of the GNU GPL.
9 
10 If you would like to use this library in a closed-source application,
11 a separate license agreement is available. For information about
12 the closed-source license agreement for the C++ sockets library,
13 please visit http://www.alhem.net/Sockets/license.html and/or
14 email license@alhem.net.
15 
16 This program is free software; you can redistribute it and/or
17 modify it under the terms of the GNU General Public License
18 as published by the Free Software Foundation; either version 2
19 of the License, or (at your option) any later version.
20 
21 This program is distributed in the hope that it will be useful,
22 but WITHOUT ANY WARRANTY; without even the implied warranty of
23 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 GNU General Public License for more details.
25 
26 You should have received a copy of the GNU General Public License
27 along with this program; if not, write to the Free Software
28 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
29 */
30 #include "Base64.h"
31 
32 #ifdef SOCKETS_NAMESPACE
33 namespace SOCKETS_NAMESPACE {
34 #endif
35 
36 
37 const char *Base64::bstr =
38  "ABCDEFGHIJKLMNOPQ"
39  "RSTUVWXYZabcdefgh"
40  "ijklmnopqrstuvwxy"
41  "z0123456789+/";
42 
43 const char Base64::rstr[] = {
44  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
45  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
46  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 62, 0, 0, 0, 63,
47  52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 0, 0, 0, 0, 0, 0,
48  0, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14,
49  15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 0, 0, 0, 0, 0,
50  0, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40,
51  41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 0, 0, 0, 0, 0};
52 
53 
54 void Base64::encode(FILE *fil, std::string& output, bool add_crlf)
55 {
56  size_t remain;
57  size_t i = 0;
58  size_t o = 0;
59  char input[4];
60 
61  output = "";
62  remain = fread(input,1,3,fil);
63  while (remain > 0)
64  {
65  if (add_crlf && o && o % 76 == 0)
66  output += "\n";
67  switch (remain)
68  {
69  case 1:
70  output += bstr[ ((input[i] >> 2) & 0x3f) ];
71  output += bstr[ ((input[i] << 4) & 0x30) ];
72  output += "==";
73  break;
74  case 2:
75  output += bstr[ ((input[i] >> 2) & 0x3f) ];
76  output += bstr[ ((input[i] << 4) & 0x30) + ((input[i + 1] >> 4) & 0x0f) ];
77  output += bstr[ ((input[i + 1] << 2) & 0x3c) ];
78  output += "=";
79  break;
80  default:
81  output += bstr[ ((input[i] >> 2) & 0x3f) ];
82  output += bstr[ ((input[i] << 4) & 0x30) + ((input[i + 1] >> 4) & 0x0f) ];
83  output += bstr[ ((input[i + 1] << 2) & 0x3c) + ((input[i + 2] >> 6) & 0x03) ];
84  output += bstr[ (input[i + 2] & 0x3f) ];
85  }
86  o += 4;
87  //
88  remain = fread(input,1,3,fil);
89  }
90 }
91 
92 
93 void Base64::encode(const std::string& str_in, std::string& str_out, bool add_crlf)
94 {
95  encode(str_in.c_str(), str_in.size(), str_out, add_crlf);
96 }
97 
98 
99 void Base64::encode(const char* input,size_t l,std::string& output, bool add_crlf)
100 {
101  size_t i = 0;
102  size_t o = 0;
103 
104  output = "";
105  while (i < l)
106  {
107  size_t remain = l - i;
108  if (add_crlf && o && o % 76 == 0)
109  output += "\n";
110  switch (remain)
111  {
112  case 1:
113  output += bstr[ ((input[i] >> 2) & 0x3f) ];
114  output += bstr[ ((input[i] << 4) & 0x30) ];
115  output += "==";
116  break;
117  case 2:
118  output += bstr[ ((input[i] >> 2) & 0x3f) ];
119  output += bstr[ ((input[i] << 4) & 0x30) + ((input[i + 1] >> 4) & 0x0f) ];
120  output += bstr[ ((input[i + 1] << 2) & 0x3c) ];
121  output += "=";
122  break;
123  default:
124  output += bstr[ ((input[i] >> 2) & 0x3f) ];
125  output += bstr[ ((input[i] << 4) & 0x30) + ((input[i + 1] >> 4) & 0x0f) ];
126  output += bstr[ ((input[i + 1] << 2) & 0x3c) + ((input[i + 2] >> 6) & 0x03) ];
127  output += bstr[ (input[i + 2] & 0x3f) ];
128  }
129  o += 4;
130  i += 3;
131  }
132 }
133 
134 
135 void Base64::encode(unsigned char* input,size_t l,std::string& output,bool add_crlf)
136 {
137  size_t i = 0;
138  size_t o = 0;
139 
140  output = "";
141  while (i < l)
142  {
143  size_t remain = l - i;
144  if (add_crlf && o && o % 76 == 0)
145  output += "\n";
146  switch (remain)
147  {
148  case 1:
149  output += bstr[ ((input[i] >> 2) & 0x3f) ];
150  output += bstr[ ((input[i] << 4) & 0x30) ];
151  output += "==";
152  break;
153  case 2:
154  output += bstr[ ((input[i] >> 2) & 0x3f) ];
155  output += bstr[ ((input[i] << 4) & 0x30) + ((input[i + 1] >> 4) & 0x0f) ];
156  output += bstr[ ((input[i + 1] << 2) & 0x3c) ];
157  output += "=";
158  break;
159  default:
160  output += bstr[ ((input[i] >> 2) & 0x3f) ];
161  output += bstr[ ((input[i] << 4) & 0x30) + ((input[i + 1] >> 4) & 0x0f) ];
162  output += bstr[ ((input[i + 1] << 2) & 0x3c) + ((input[i + 2] >> 6) & 0x03) ];
163  output += bstr[ (input[i + 2] & 0x3f) ];
164  }
165  o += 4;
166  i += 3;
167  }
168 }
169 
170 
171 void Base64::decode(const std::string& input,std::string& output)
172 {
173  size_t i = 0;
174  size_t l = input.size();
175 
176  output = "";
177  while (i < l)
178  {
179  while (i < l && (input[i] == 13 || input[i] == 10))
180  ++i;
181  if (i < l)
182  {
183  char b1 = (char)((rstr[(int)input[i]] << 2 & 0xfc) +
184  (rstr[(int)input[i + 1]] >> 4 & 0x03));
185  output += b1;
186  if (input[i + 2] != '=')
187  {
188  char b2 = (char)((rstr[(int)input[i + 1]] << 4 & 0xf0) +
189  (rstr[(int)input[i + 2]] >> 2 & 0x0f));
190  output += b2;
191  }
192  if (input[i + 3] != '=')
193  {
194  char b3 = (char)((rstr[(int)input[i + 2]] << 6 & 0xc0) +
195  rstr[(int)input[i + 3]]);
196  output += b3;
197  }
198  i += 4;
199  }
200  }
201 }
202 
203 
204 void Base64::decode(const std::string& input, unsigned char *output, size_t& sz)
205 {
206  size_t i = 0;
207  size_t l = input.size();
208  size_t j = 0;
209 
210  while (i < l)
211  {
212  while (i < l && (input[i] == 13 || input[i] == 10))
213  ++i;
214  if (i < l)
215  {
216  unsigned char b1 = (unsigned char)((rstr[(int)input[i]] << 2 & 0xfc) +
217  (rstr[(int)input[i + 1]] >> 4 & 0x03));
218  if (output)
219  {
220  output[j] = b1;
221  }
222  ++j;
223  if (input[i + 2] != '=')
224  {
225  unsigned char b2 = (unsigned char)((rstr[(int)input[i + 1]] << 4 & 0xf0) +
226  (rstr[(int)input[i + 2]] >> 2 & 0x0f));
227  if (output)
228  {
229  output[j] = b2;
230  }
231  ++j;
232  }
233  if (input[i + 3] != '=')
234  {
235  unsigned char b3 = (unsigned char)((rstr[(int)input[i + 2]] << 6 & 0xc0) +
236  rstr[(int)input[i + 3]]);
237  if (output)
238  {
239  output[j] = b3;
240  }
241  ++j;
242  }
243  i += 4;
244  }
245  }
246  sz = j;
247 }
248 
249 
250 size_t Base64::decode_length(const std::string& str64)
251 {
252  if (!str64.size() || str64.size() % 4)
253  return 0;
254  size_t l = 3 * (str64.size() / 4 - 1) + 1;
255  if (str64[str64.size() - 2] != '=')
256  ++l;
257  if (str64[str64.size() - 1] != '=')
258  ++l;
259  return l;
260 }
261 
262 
263 #ifdef SOCKETS_NAMESPACE
264 }
265 #endif
266 
static size_t decode_length(const std::string &)
Definition: Base64.cpp:250
static const char rstr[128]
Definition: Base64.h:59
static void decode(const std::string &, std::string &)
Definition: Base64.cpp:171
static const char * bstr
Definition: Base64.h:58
static void encode(FILE *, std::string &, bool add_crlf=true)
Definition: Base64.cpp:54