20static const char b64str[]
21 =
"ABCDEFGHIJKLMNOPQRSTUVWXYZ"
22 "abcdefghijklmnopqrstuvwxyz"
27 const unsigned char *in =
static_cast<const unsigned char *
>(bytes);
28 const unsigned char *end = in + nbytes;
32 out << b64str[((in[0] & 0x03) << 4) | (in[1] >> 4)];
33 out << b64str[((in[1] & 0x0f) << 2) | (in[2] >> 6)];
34 out << b64str[in[2] & 0x3f];
42 out << b64str[(in[0] & 0x03) << 4];
47 out << b64str[((in[0] & 0x03) << 4) | (in[1] >> 4)];
48 out << b64str[(in[1] & 0x0f) << 2];
54static const unsigned char b64table[] =
56 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,
57 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,
58 255,255,255,255,255,255,255,255,255,255,255,62, 255,62, 255,63,
59 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 255,255,255,0, 255,255,
60 255,0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14,
61 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 255,255,255,255,63,
62 255,26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40,
63 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 255,255,255,255,255,
64 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,
65 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,
66 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,
67 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,
68 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,
69 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,
70 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,
71 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255
74void DecodeBase64(
const char *src,
size_t len, std::vector<char> &buf)
76 const unsigned char *in = (
const unsigned char *)src;
79 for (
size_t i=0; i<len; ++i) {
if (b64table[in[i]] != 255) { ++count; } }
80 if (count % 4 != 0) {
return; }
82 unsigned char *
out = (
unsigned char *)buf.data();
86 for (
size_t i=0; i<len; ++i)
88 unsigned char t = b64table[in[i]];
89 if (
t == 255) {
continue; }
90 if (in[i] ==
'=') { ++pad; }
94 *
out++ = (c[0] << 2) | (c[1] >> 4);
95 if (pad <= 1) { *
out++ = (c[1] << 4) | (c[2] >> 2); }
96 if (pad == 0) { *
out++ = (c[2] << 6) | c[3]; }
100 buf.resize(
out - (
unsigned char *)buf.data());