167 static const unsigned char base16[128] =
169 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
170 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
171 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
172 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
173 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
174 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
175 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
176 0x08, 0x09, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
177 0xFF, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0xFF,
178 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
179 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
180 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
181 0xFF, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0xFF,
182 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
183 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
184 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF
187 size_t ilength = str.length();
189 assert(((ilength % 2) == 0) &&
"Invalid Base16 sting!");
190 if ((ilength % 2) != 0)
193 size_t olength = ilength / 2;
196 result.resize(olength, 0);
198 for (
size_t i = 0, j = 0; i < ilength;)
200 uint8_t a = (uint8_t)str[i++];
201 uint8_t b = (uint8_t)str[i++];
204 assert(((a < 0x80) && (b < 0x80)) &&
"Invalid Base16 content!");
205 if ((a >= 0x80) || (b >= 0x80))
212 result[j++] = ((a << 4) | b);
220 const char base32[] =
"ABCDEFGHIJKLMNOPQRSTUVWXYZ234567=";
222 size_t ilength = str.length();
223 size_t olength = ((ilength / 5) * 8) + ((ilength % 5) ? 8 : 0);
226 result.resize(olength, 0);
228 for (
size_t i = 0, j = 0; i < ilength;)
230 size_t block = ((ilength - i) < 5 ? (ilength - i) : 5);
231 uint8_t n1, n2, n3, n4, n5, n6, n7, n8;
232 n1 = n2 = n3 = n4 = n5 = n6 = n7 = n8 = 0;
237 n8 = (((uint8_t)str[i + 4] & 0x1F) >> 0);
238 n7 = (((uint8_t)str[i + 4] & 0xE0) >> 5);
240 n7 |= (((uint8_t)str[i + 3] & 0x03) << 3);
241 n6 = (((uint8_t)str[i + 3] & 0x7C) >> 2);
242 n5 = (((uint8_t)str[i + 3] & 0x80) >> 7);
244 n5 |= (((uint8_t)str[i + 2] & 0x0F) << 1);
245 n4 = (((uint8_t)str[i + 2] & 0xF0) >> 4);
247 n4 |= (((uint8_t)str[i + 1] & 0x01) << 4);
248 n3 = (((uint8_t)str[i + 1] & 0x3E) >> 1);
249 n2 = (((uint8_t)str[i + 1] & 0xC0) >> 6);
251 n2 |= (((uint8_t)str[i + 0] & 0x07) << 2);
252 n1 = (((uint8_t)str[i + 0] & 0xF8) >> 3);
255 assert(
false &&
"Invalid Base32 operation!");
260 assert((n1 <= 31) &&
"Invalid Base32 n1 value!");
261 assert((n2 <= 31) &&
"Invalid Base32 n2 value!" );
262 assert((n3 <= 31) &&
"Invalid Base32 n3 value!" );
263 assert((n4 <= 31) &&
"Invalid Base32 n4 value!" );
264 assert((n5 <= 31) &&
"Invalid Base32 n5 value!" );
265 assert((n6 <= 31) &&
"Invalid Base32 n6 value!" );
266 assert((n7 <= 31) &&
"Invalid Base32 n7 value!" );
267 assert((n8 <= 31) &&
"Invalid Base32 n8 value!" );
272 case 1: n3 = n4 = 32;
274 case 3: n6 = n7 = 32;
279 assert(
false &&
"Invalid Base32 operation!");
283 result[j++] = base32[n1];
284 result[j++] = base32[n2];
285 result[j++] = base32[n3];
286 result[j++] = base32[n4];
287 result[j++] = base32[n5];
288 result[j++] = base32[n6];
289 result[j++] = base32[n7];
290 result[j++] = base32[n8];
298 static const unsigned char base32[128] =
300 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
301 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
302 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
303 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
304 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
305 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
306 0xFF, 0xFF, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f,
307 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x20, 0xFF, 0xFF,
308 0xFF, 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06,
309 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e,
310 0x0f, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16,
311 0x17, 0x18, 0x19, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
312 0xFF, 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06,
313 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e,
314 0x0f, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16,
315 0x17, 0x18, 0x19, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF
318 size_t ilength = str.length();
320 assert(((ilength % 8) == 0) &&
"Invalid Base32 sting!");
321 if ((ilength % 8) != 0)
324 size_t olength = (ilength / 8) * 5;
327 result.resize(olength, 0);
329 for (
size_t i = 0, j = 0; i < ilength;)
332 uint8_t n1 = (uint8_t)str[i++];
333 uint8_t n2 = (uint8_t)str[i++];
334 uint8_t n3 = (uint8_t)str[i++];
335 uint8_t n4 = (uint8_t)str[i++];
336 uint8_t n5 = (uint8_t)str[i++];
337 uint8_t n6 = (uint8_t)str[i++];
338 uint8_t n7 = (uint8_t)str[i++];
339 uint8_t n8 = (uint8_t)str[i++];
342 assert(((n1 < 0x80) && (n2 < 0x80) && (n3 < 0x80) && (n4 < 0x80) && (n5 < 0x80) && (n6 < 0x80) && (n7 < 0x80) && (n8 < 0x80)) &&
"Invalid Base32 content!");
343 if ((n1 >= 0x80) || (n2 >= 0x80) || (n3 >= 0x80) || (n4 >= 0x80) || (n5 >= 0x80) || (n6 >= 0x80) || (n7 >= 0x80) || (n8 >= 0x80))
357 assert(((n1 <= 31) && (n2 <= 31)) &&
"Invalid Base32 content!");
358 if ((n1 > 31) || (n2 > 31))
362 assert(((n3 <= 32) && (n4 <= 32) && (n5 <= 32) && (n6 <= 32) && (n7 <= 32) && (n8 <= 32)) &&
"Invalid Base32 content!");
363 if ((n3 > 32) || (n4 > 32) || (n5 > 32) || (n6 > 32) || (n7 > 32) || (n8 > 32))
367 result[j++] = ((n1 & 0x1f) << 3) | ((n2 & 0x1c) >> 2);
368 result[j++] = ((n2 & 0x03) << 6) | ((n3 & 0x1f) << 1) | ((n4 & 0x10) >> 4);
369 result[j++] = ((n4 & 0x0f) << 4) | ((n5 & 0x1e) >> 1);
370 result[j++] = ((n5 & 0x01) << 7) | ((n6 & 0x1f) << 2) | ((n7 & 0x18) >> 3);
371 result[j++] = ((n7 & 0x07) << 5) | ((n8 & 0x1f));
376 result.resize(result.size() - 1);
377 assert((((n7 == 32) && (n6 == 32)) || (n7 != 32)) &&
"Invalid Base32 content!");
380 result.resize(result.size() - 1);
383 result.resize(result.size() - 1);
384 assert((((n4 == 32) && (n3 == 32)) || (n4 != 32)) &&
"Invalid Base32 content!");
387 result.resize(result.size() - 1);
430 static const unsigned char base64[256] =
432 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
433 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
434 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x3e, 0x00, 0x00, 0x00, 0x3f,
435 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x3a, 0x3b, 0x3c, 0x3d, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
436 0x00, 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e,
437 0x0f, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x00, 0x00, 0x00, 0x00, 0x00,
438 0x00, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28,
439 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f, 0x30, 0x31, 0x32, 0x33, 0x00, 0x00, 0x00, 0x00, 0x00,
440 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
441 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
442 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
443 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
444 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
445 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
446 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
447 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
450 size_t ilength = str.length();
452 if (ilength % 4 != 0)
455 size_t olength = ilength / 4 * 3;
457 if (str[ilength - 1] ==
'=') olength--;
458 if (str[ilength - 2] ==
'=') olength--;
461 result.resize(olength, 0);
463 for (
size_t i = 0, j = 0; i < ilength;)
465 uint32_t sextet_a = str[i] ==
'=' ? 0 & i++ : base64[(uint8_t)str[i++]];
466 uint32_t sextet_b = str[i] ==
'=' ? 0 & i++ : base64[(uint8_t)str[i++]];
467 uint32_t sextet_c = str[i] ==
'=' ? 0 & i++ : base64[(uint8_t)str[i++]];
468 uint32_t sextet_d = str[i] ==
'=' ? 0 & i++ : base64[(uint8_t)str[i++]];
470 uint32_t triple = (sextet_a << 3 * 6) + (sextet_b << 2 * 6) + (sextet_c << 1 * 6) + (sextet_d << 0 * 6);
472 if (j < olength) result[j++] = (triple >> 2 * 8) & 0xFF;
473 if (j < olength) result[j++] = (triple >> 1 * 8) & 0xFF;
474 if (j < olength) result[j++] = (triple >> 0 * 8) & 0xFF;