162 static const unsigned char base16[128] =
164 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
165 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
166 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
167 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
168 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
169 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
170 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
171 0x08, 0x09, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
172 0xFF, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0xFF,
173 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
174 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
175 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
176 0xFF, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0xFF,
177 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
178 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
179 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF
182 size_t ilength = str.length();
184 assert(((ilength % 2) == 0) &&
"Invalid Base16 sting!");
185 if ((ilength % 2) != 0)
188 size_t olength = ilength / 2;
191 result.resize(olength, 0);
193 for (
size_t i = 0, j = 0; i < ilength;)
195 uint8_t a = (uint8_t)str[i++];
196 uint8_t b = (uint8_t)str[i++];
199 assert(((a < 0x80) && (b < 0x80)) &&
"Invalid Base16 content!");
200 if ((a >= 0x80) || (b >= 0x80))
207 result[j++] = ((a << 4) | b);
215 const char base32[] =
"ABCDEFGHIJKLMNOPQRSTUVWXYZ234567=";
217 size_t ilength = str.length();
218 size_t olength = ((ilength / 5) * 8) + ((ilength % 5) ? 8 : 0);
221 result.resize(olength, 0);
223 for (
size_t i = 0, j = 0; i < ilength;)
225 size_t block = ((ilength - i) < 5 ? (ilength - i) : 5);
226 uint8_t n1, n2, n3, n4, n5, n6, n7, n8;
227 n1 = n2 = n3 = n4 = n5 = n6 = n7 = n8 = 0;
232 n8 = (((uint8_t)str[i + 4] & 0x1F) >> 0);
233 n7 = (((uint8_t)str[i + 4] & 0xE0) >> 5);
235 n7 |= (((uint8_t)str[i + 3] & 0x03) << 3);
236 n6 = (((uint8_t)str[i + 3] & 0x7C) >> 2);
237 n5 = (((uint8_t)str[i + 3] & 0x80) >> 7);
239 n5 |= (((uint8_t)str[i + 2] & 0x0F) << 1);
240 n4 = (((uint8_t)str[i + 2] & 0xF0) >> 4);
242 n4 |= (((uint8_t)str[i + 1] & 0x01) << 4);
243 n3 = (((uint8_t)str[i + 1] & 0x3E) >> 1);
244 n2 = (((uint8_t)str[i + 1] & 0xC0) >> 6);
246 n2 |= (((uint8_t)str[i + 0] & 0x07) << 2);
247 n1 = (((uint8_t)str[i + 0] & 0xF8) >> 3);
250 assert(
false &&
"Invalid Base32 operation!");
255 assert((n1 <= 31) &&
"Invalid Base32 n1 value!");
256 assert((n2 <= 31) &&
"Invalid Base32 n2 value!" );
257 assert((n3 <= 31) &&
"Invalid Base32 n3 value!" );
258 assert((n4 <= 31) &&
"Invalid Base32 n4 value!" );
259 assert((n5 <= 31) &&
"Invalid Base32 n5 value!" );
260 assert((n6 <= 31) &&
"Invalid Base32 n6 value!" );
261 assert((n7 <= 31) &&
"Invalid Base32 n7 value!" );
262 assert((n8 <= 31) &&
"Invalid Base32 n8 value!" );
267 case 1: n3 = n4 = 32;
269 case 3: n6 = n7 = 32;
274 assert(
false &&
"Invalid Base32 operation!");
278 result[j++] = base32[n1];
279 result[j++] = base32[n2];
280 result[j++] = base32[n3];
281 result[j++] = base32[n4];
282 result[j++] = base32[n5];
283 result[j++] = base32[n6];
284 result[j++] = base32[n7];
285 result[j++] = base32[n8];
293 static const unsigned char base32[128] =
295 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
296 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
297 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
298 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
299 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
300 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
301 0xFF, 0xFF, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f,
302 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x20, 0xFF, 0xFF,
303 0xFF, 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06,
304 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e,
305 0x0f, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16,
306 0x17, 0x18, 0x19, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
307 0xFF, 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06,
308 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e,
309 0x0f, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16,
310 0x17, 0x18, 0x19, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF
313 size_t ilength = str.length();
315 assert(((ilength % 8) == 0) &&
"Invalid Base32 sting!");
316 if ((ilength % 8) != 0)
319 size_t olength = (ilength / 8) * 5;
322 result.resize(olength, 0);
324 for (
size_t i = 0, j = 0; i < ilength;)
327 uint8_t n1 = (uint8_t)str[i++];
328 uint8_t n2 = (uint8_t)str[i++];
329 uint8_t n3 = (uint8_t)str[i++];
330 uint8_t n4 = (uint8_t)str[i++];
331 uint8_t n5 = (uint8_t)str[i++];
332 uint8_t n6 = (uint8_t)str[i++];
333 uint8_t n7 = (uint8_t)str[i++];
334 uint8_t n8 = (uint8_t)str[i++];
337 assert(((n1 < 0x80) && (n2 < 0x80) && (n3 < 0x80) && (n4 < 0x80) && (n5 < 0x80) && (n6 < 0x80) && (n7 < 0x80) && (n8 < 0x80)) &&
"Invalid Base32 content!");
338 if ((n1 >= 0x80) || (n2 >= 0x80) || (n3 >= 0x80) || (n4 >= 0x80) || (n5 >= 0x80) || (n6 >= 0x80) || (n7 >= 0x80) || (n8 >= 0x80))
352 assert(((n1 <= 31) && (n2 <= 31)) &&
"Invalid Base32 content!");
353 if ((n1 > 31) || (n2 > 31))
357 assert(((n3 <= 32) && (n4 <= 32) && (n5 <= 32) && (n6 <= 32) && (n7 <= 32) && (n8 <= 32)) &&
"Invalid Base32 content!");
358 if ((n3 > 32) || (n4 > 32) || (n5 > 32) || (n6 > 32) || (n7 > 32) || (n8 > 32))
362 result[j++] = ((n1 & 0x1f) << 3) | ((n2 & 0x1c) >> 2);
363 result[j++] = ((n2 & 0x03) << 6) | ((n3 & 0x1f) << 1) | ((n4 & 0x10) >> 4);
364 result[j++] = ((n4 & 0x0f) << 4) | ((n5 & 0x1e) >> 1);
365 result[j++] = ((n5 & 0x01) << 7) | ((n6 & 0x1f) << 2) | ((n7 & 0x18) >> 3);
366 result[j++] = ((n7 & 0x07) << 5) | ((n8 & 0x1f));
371 result.resize(result.size() - 1);
372 assert((((n7 == 32) && (n6 == 32)) || (n7 != 32)) &&
"Invalid Base32 content!");
375 result.resize(result.size() - 1);
378 result.resize(result.size() - 1);
379 assert((((n4 == 32) && (n3 == 32)) || (n4 != 32)) &&
"Invalid Base32 content!");
382 result.resize(result.size() - 1);
425 static const unsigned char base64[256] =
427 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
428 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
429 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x3e, 0x00, 0x00, 0x00, 0x3f,
430 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x3a, 0x3b, 0x3c, 0x3d, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
431 0x00, 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e,
432 0x0f, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x00, 0x00, 0x00, 0x00, 0x00,
433 0x00, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28,
434 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f, 0x30, 0x31, 0x32, 0x33, 0x00, 0x00, 0x00, 0x00, 0x00,
435 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
436 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
437 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
438 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
439 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 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
445 size_t ilength = str.length();
447 if (ilength % 4 != 0)
450 size_t olength = ilength / 4 * 3;
452 if (str[ilength - 1] ==
'=') olength--;
453 if (str[ilength - 2] ==
'=') olength--;
456 result.resize(olength, 0);
458 for (
size_t i = 0, j = 0; i < ilength;)
460 uint32_t sextet_a = str[i] ==
'=' ? 0 & i++ : base64[(uint8_t)str[i++]];
461 uint32_t sextet_b = str[i] ==
'=' ? 0 & i++ : base64[(uint8_t)str[i++]];
462 uint32_t sextet_c = str[i] ==
'=' ? 0 & i++ : base64[(uint8_t)str[i++]];
463 uint32_t sextet_d = str[i] ==
'=' ? 0 & i++ : base64[(uint8_t)str[i++]];
465 uint32_t triple = (sextet_a << 3 * 6) + (sextet_b << 2 * 6) + (sextet_c << 1 * 6) + (sextet_d << 0 * 6);
467 if (j < olength) result[j++] = (triple >> 2 * 8) & 0xFF;
468 if (j < olength) result[j++] = (triple >> 1 * 8) & 0xFF;
469 if (j < olength) result[j++] = (triple >> 0 * 8) & 0xFF;