26 const uint8_t* in =
reinterpret_cast<const uint8_t*
>(data);
27 const uint8_t* in_end = in + size;
30 if (size == 0)
return out;
33 size_t outsize = (((size - 1) / 3) + 1) * 4;
34 if (line_break > 0) outsize += outsize / line_break;
37 static const char encoding64[64] = {
38 'A',
'B',
'C',
'D',
'E',
'F',
'G',
'H',
'I',
'J',
'K',
'L',
'M',
39 'N',
'O',
'P',
'Q',
'R',
'S',
'T',
'U',
'V',
'W',
'X',
'Y',
'Z',
40 'a',
'b',
'c',
'd',
'e',
'f',
'g',
'h',
'i',
'j',
'k',
'l',
'm',
41 'n',
'o',
'p',
'q',
'r',
's',
't',
'u',
'v',
'w',
'x',
'y',
'z',
42 '0',
'1',
'2',
'3',
'4',
'5',
'6',
'7',
'8',
'9',
'+',
'/' 46 size_t line_begin = 0;
56 uint8_t fragment = *in++;
57 result = (fragment & 0xFC) >> 2;
58 out += encoding64[result];
59 result =
static_cast<uint8_t
>((fragment & 0x03) << 4);
63 out += encoding64[result];
72 result |= (fragment & 0xF0) >> 4;
73 out += encoding64[result];
74 result =
static_cast<uint8_t
>((fragment & 0x0F) << 2);
78 out += encoding64[result];
86 result |= (fragment & 0xC0) >> 6;
87 out += encoding64[result];
89 result = (fragment & 0x3F) >> 0;
90 out += encoding64[result];
94 if (line_break > 0 && out.size() - line_begin >= line_break)
97 line_begin = out.size();
109 const uint8_t* in =
reinterpret_cast<const uint8_t*
>(data);
110 const uint8_t* in_end = in + size;
115 out.reserve(size * 3 / 4);
117 static constexpr uint8_t ex = 255;
118 static constexpr uint8_t ws = 254;
120 static const uint8_t decoding64[256] = {
121 ex, ex, ex, ex, ex, ex, ex, ex, ex, ws, ws, ex, ex, ws, ex, ex,
122 ex, ex, ex, ex, ex, ex, ex, ex, ex, ex, ex, ex, ex, ex, ex, ex,
123 ws, ex, ex, ex, ex, ex, ex, ex, ex, ex, ex, 62, ex, ex, ex, 63,
124 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, ex, ex, ex, ws, ex, ex,
125 ex, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14,
126 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, ex, ex, ex, ex, ex,
127 ex, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40,
128 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, ex, ex, ex, ex, ex,
129 ex, ex, ex, ex, ex, ex, ex, ex, ex, ex, ex, ex, ex, ex, ex, ex,
130 ex, ex, ex, ex, ex, ex, ex, ex, ex, ex, ex, ex, ex, ex, ex, ex,
131 ex, ex, ex, ex, ex, ex, ex, ex, ex, ex, ex, ex, ex, ex, ex, ex,
132 ex, ex, ex, ex, ex, ex, ex, ex, ex, ex, ex, ex, ex, ex, ex, ex,
133 ex, ex, ex, ex, ex, ex, ex, ex, ex, ex, ex, ex, ex, ex, ex, ex,
134 ex, ex, ex, ex, ex, ex, ex, ex, ex, ex, ex, ex, ex, ex, ex, ex,
135 ex, ex, ex, ex, ex, ex, ex, ex, ex, ex, ex, ex, ex, ex, ex, ex,
136 ex, ex, ex, ex, ex, ex, ex, ex, ex, ex, ex, ex, ex, ex, ex, ex
139 uint8_t outchar, fragment;
141 static const char* ex_message =
142 "Invalid character encountered during base64 decoding.";
148 if (in == in_end)
return out;
150 fragment = decoding64[*in++];
152 if (fragment == ex && strict)
153 throw std::runtime_error(ex_message);
154 }
while (fragment >= ws);
156 outchar =
static_cast<uint8_t
>((fragment & 0x3F) << 2);
160 if (in == in_end)
return out;
162 fragment = decoding64[*in++];
164 if (fragment == ex && strict)
165 throw std::runtime_error(ex_message);
166 }
while (fragment >= ws);
168 outchar =
static_cast<uint8_t
>(outchar | ((fragment & 0x30) >> 4));
169 out +=
static_cast<char>(outchar);
171 outchar =
static_cast<uint8_t
>((fragment & 0x0F) << 4);
175 if (in == in_end)
return out;
177 fragment = decoding64[*in++];
179 if (fragment == ex && strict)
180 throw std::runtime_error(ex_message);
181 }
while (fragment >= ws);
183 outchar =
static_cast<uint8_t
>(outchar | ((fragment & 0x3C) >> 2));
184 out +=
static_cast<char>(outchar);
186 outchar =
static_cast<uint8_t
>((fragment & 0x03) << 6);
190 if (in == in_end)
return out;
192 fragment = decoding64[*in++];
194 if (fragment == ex && strict)
195 throw std::runtime_error(ex_message);
196 }
while (fragment >= ws);
198 outchar =
static_cast<uint8_t
>(outchar | ((fragment & 0x3F) >> 0));
199 out +=
static_cast<char>(outchar);
std::string base64_decode(const void *data, size_t size, bool strict)
Decode a string in base64 representation as described in RFC 2045 or RFC 3548 and return the original...
std::string base64_encode(const void *data, size_t size, size_t line_break)
Encode the given binary data into base64 representation as described in RFC 2045 or RFC 3548...
std::basic_string< char, std::char_traits< char >, Allocator< char > > string
string with Manager tracking