26 static const unsigned char base64_table[65] =
27 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
41 static std::string base64_encode(
const unsigned char *src,
size_t len)
43 unsigned char *out, *pos;
44 const unsigned char *end, *in;
49 olen = len * 4 / 3 + 4;
54 out = (
unsigned char*)malloc(olen);
62 while (end - in >= 3) {
63 *pos++ = base64_table[in[0] >> 2];
64 *pos++ = base64_table[((in[0] & 0x03) << 4) | (in[1] >> 4)];
65 *pos++ = base64_table[((in[1] & 0x0f) << 2) | (in[2] >> 6)];
66 *pos++ = base64_table[in[2] & 0x3f];
76 *pos++ = base64_table[in[0] >> 2];
78 *pos++ = base64_table[(in[0] & 0x03) << 4];
81 *pos++ = base64_table[((in[0] & 0x03) << 4) |
83 *pos++ = base64_table[(in[1] & 0x0f) << 2];
93 auto out_len = pos - out;
94 str = std::string(
reinterpret_cast<char*
>(out), out_len);
110 static std::vector<char> base64_decode(
const unsigned char *src,
size_t len)
112 std::vector<char> str = {};
113 unsigned char dtable[256], *out, *pos, block[4], tmp;
114 size_t i, count, olen;
117 memset(dtable, 0x80, 256);
118 for (i = 0; i <
sizeof(base64_table) - 1; i++)
119 dtable[base64_table[i]] = (
unsigned char) i;
123 for (i = 0; i < len; i++) {
124 if (dtable[src[i]] != 0x80)
128 if (count == 0 || count % 4)
131 olen = count / 4 * 3;
132 pos = out = (
unsigned char*)malloc(olen);
137 for (i = 0; i < len; i++) {
138 tmp = dtable[src[i]];
147 *pos++ = (block[0] << 2) | (block[1] >> 4);
148 *pos++ = (block[1] << 4) | (block[2] >> 2);
149 *pos++ = (block[2] << 6) | block[3];
166 auto out_len = pos - out;
167 str = std::vector<char>(out, out + out_len);