25#ifndef MYSQLROUTER_HTTP_BASE64_INCLUDED
26#define MYSQLROUTER_HTTP_BASE64_INCLUDED
134 template <Base64Endianess endianess,
bool PaddingMandatory,
char PaddingChar>
136 const std::string &encoded,
138 std::vector<uint8_t> out((encoded.size() + 3) / 4 * 3);
140 constexpr unsigned int shift_pos_0 =
142 constexpr unsigned int shift_pos_1 =
144 constexpr unsigned int shift_pos_2 =
147 auto out_it = out.begin();
148 auto data_it = encoded.cbegin();
149 const auto data_end_it = encoded.cend();
150 while (
const size_t data_left =
std::distance(data_it, data_end_it)) {
152 throw std::runtime_error(
"invalid sequence");
155 if (PaddingMandatory && (data_left < 4)) {
156 throw std::runtime_error(
"missing padding");
160 bool is_padding =
false;
161 const size_t max_rounds = std::min(
size_t{4}, data_left);
162 uint32_t sextets = 0;
163 for (
size_t cnt = 0; cnt < max_rounds; ++cnt) {
164 const uint8_t b64 = *(data_it++);
165 if (is_padding && b64 != PaddingChar) {
166 throw std::runtime_error(
"invalid char, expected padding");
168 const int8_t c = (inverse_alphabet[b64]);
171 if (data_left <= 4 && cnt >= 2 && b64 == PaddingChar) {
175 throw std::runtime_error(std::string(
"invalid char"));
182 v |= c << (6 * (3 - cnt));
213 *(out_it++) =
static_cast<uint8_t
>(v >> shift_pos_0);
215 if (0 !=
static_cast<uint8_t
>(v >> shift_pos_1)) {
216 throw std::runtime_error(
"unused bits");
220 *(out_it++) =
static_cast<uint8_t
>(v >> shift_pos_0);
221 *(out_it++) =
static_cast<uint8_t
>(v >> shift_pos_1);
223 if (0 !=
static_cast<uint8_t
>(v >> shift_pos_2)) {
224 throw std::runtime_error(
"unused bits");
228 *(out_it++) =
static_cast<uint8_t
>(v >> shift_pos_0);
229 *(out_it++) =
static_cast<uint8_t
>(v >> shift_pos_1);
230 *(out_it++) =
static_cast<uint8_t
>(v >> shift_pos_2);
240 template <Base64Endianess endianess,
bool PaddingMandatory,
char PaddingChar>
241 static std::string
encode(
const std::vector<uint8_t> &data,
245 out.resize((data.size() + 2) / 3 * 4);
247 constexpr unsigned int shift_pos_0 =
249 constexpr unsigned int shift_pos_1 =
251 constexpr unsigned int shift_pos_2 =
254 auto out_it = out.begin();
255 auto data_it = data.begin();
256 const auto data_end_it = data.end();
257 while (
const size_t data_left =
std::distance(data_it, data_end_it)) {
261 size_t padding_pos = 4;
264 v |= (*(data_it++)) << shift_pos_0;
268 v |= (*(data_it++)) << shift_pos_0;
269 v |= (*(data_it++)) << shift_pos_1;
273 v |= (*(data_it++)) << shift_pos_0;
274 v |= (*(data_it++)) << shift_pos_1;
275 v |= (*(data_it++)) << shift_pos_2;
281 for (cnt = 0; cnt < 4; ++cnt) {
282 if (cnt >= padding_pos) {
289 pos = (v & (0x3f << (3 * 6))) >> 3 * 6;
296 *(out_it++) = alphabet.at(pos);
299 if (PaddingMandatory) {
301 for (; cnt < 4; ++cnt) {
302 *(out_it++) = PaddingChar;
338 "alphabet MUST less <= 128 chars");
339 return (v_ndx >= v.size() ? -1
340 : (v[v_ndx] ==
static_cast<char>(character))
341 ?
static_cast<uint8_t
>(v_ndx)
365 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20,
366 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39,
367 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58,
368 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77,
369 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96,
370 97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111, 112,
371 113, 114, 115, 116, 117, 118, 119, 120, 121, 122, 123, 124, 125, 126, 127,
372 128, 129, 130, 131, 132, 133, 134, 135, 136, 137, 138, 139, 140, 141, 142,
373 143, 144, 145, 146, 147, 148, 149, 150, 151, 152, 153, 154, 155, 156, 157,
374 158, 159, 160, 161, 162, 163, 164, 165, 166, 167, 168, 169, 170, 171, 172,
375 173, 174, 175, 176, 177, 178, 179, 180, 181, 182, 183, 184, 185, 186, 187,
376 188, 189, 190, 191, 192, 193, 194, 195, 196, 197, 198, 199, 200, 201, 202,
377 203, 204, 205, 206, 207, 208, 209, 210, 211, 212, 213, 214, 215, 216, 217,
378 218, 219, 220, 221, 222, 223, 224, 225, 226, 227, 228, 229, 230, 231, 232,
379 233, 234, 235, 236, 237, 238, 239, 240, 241, 242, 243, 244, 245, 246, 247,
380 248, 249, 250, 251, 252, 253, 254, 255>;
389template <std::size_t... I>
391 std::index_sequence<I...>) {
406 {
'A',
'B',
'C',
'D',
'E',
'F',
'G',
'H',
407 'I',
'J',
'K',
'L',
'M',
'N',
'O',
'P',
408 'Q',
'R',
'S',
'T',
'U',
'V',
'W',
'X',
409 'Y',
'Z',
'a',
'b',
'c',
'd',
'e',
'f',
410 'g',
'h',
'i',
'j',
'k',
'l',
'm',
'n',
411 'o',
'p',
'q',
'r',
's',
't',
'u',
'v',
412 'w',
'x',
'y',
'z',
'0',
'1',
'2',
'3',
413 '4',
'5',
'6',
'7',
'8',
'9',
'+',
'/'}};
422 {
'A',
'B',
'C',
'D',
'E',
'F',
'G',
'H',
423 'I',
'J',
'K',
'L',
'M',
'N',
'O',
'P',
424 'Q',
'R',
'S',
'T',
'U',
'V',
'W',
'X',
425 'Y',
'Z',
'a',
'b',
'c',
'd',
'e',
'f',
426 'g',
'h',
'i',
'j',
'k',
'l',
'm',
'n',
427 'o',
'p',
'q',
'r',
's',
't',
'u',
'v',
428 'w',
'x',
'y',
'z',
'0',
'1',
'2',
'3',
429 '4',
'5',
'6',
'7',
'8',
'9',
'-',
'_'}};
452 {
'A',
'B',
'C',
'D',
'E',
'F',
'G',
'H',
453 'I',
'J',
'K',
'L',
'M',
'N',
'O',
'P',
454 'Q',
'R',
'S',
'T',
'U',
'V',
'W',
'X',
455 'Y',
'Z',
'a',
'b',
'c',
'd',
'e',
'f',
456 'g',
'h',
'i',
'j',
'k',
'l',
'm',
'n',
457 'o',
'p',
'q',
'r',
's',
't',
'u',
'v',
458 'w',
'x',
'y',
'z',
'0',
'1',
'2',
'3',
459 '4',
'5',
'6',
'7',
'8',
'9',
'.',
'/'}};
468 {
'.',
'/',
'0',
'1',
'2',
'3',
'4',
'5',
469 '6',
'7',
'8',
'9',
'A',
'B',
'C',
'D',
470 'E',
'F',
'G',
'H',
'I',
'J',
'K',
'L',
471 'M',
'N',
'O',
'P',
'Q',
'R',
'S',
'T',
472 'U',
'V',
'W',
'X',
'Y',
'Z',
'a',
'b',
473 'c',
'd',
'e',
'f',
'g',
'h',
'i',
'j',
474 'k',
'l',
'm',
'n',
'o',
'p',
'q',
'r',
475 's',
't',
'u',
'v',
'w',
'x',
'y',
'z'}};
484 {
'.',
'/',
'A',
'B',
'C',
'D',
'E',
'F',
485 'G',
'H',
'I',
'J',
'K',
'L',
'M',
'N',
486 'O',
'P',
'Q',
'R',
'S',
'T',
'U',
'V',
487 'W',
'X',
'Y',
'Z',
'a',
'b',
'c',
'd',
488 'e',
'f',
'g',
'h',
'i',
'j',
'k',
'l',
489 'm',
'n',
'o',
'p',
'q',
'r',
's',
't',
490 'u',
'v',
'w',
'x',
'y',
'z',
'0',
'1',
491 '2',
'3',
'4',
'5',
'6',
'7',
'8',
'9'}};
500 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
501 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f,
502 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
503 0x38, 0x39, 0x3a, 0x3b, 0x3c, 0x3d, 0x3e, 0x3f,
504 0x40, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47,
505 0x48, 0x49, 0x4a, 0x4b, 0x4c, 0x4d, 0x4e, 0x4f,
506 0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,
507 0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f,
530 static std::vector<uint8_t>
decode(
const std::string &encoded) {
531 return Base64Impl::decode<E, PaddingMandatory, PaddingChar>(
532 encoded, Alphabet::inverse_alphabet);
538 static std::string
encode(
const std::vector<uint8_t> &decoded) {
539 return Base64Impl::encode<E, PaddingMandatory, PaddingChar>(
540 decoded, Alphabet::alphabet);
static constexpr inverse_alphabet_type HTTP_COMMON_EXPORT inverse_alphabet
Definition: base64.h:431
static constexpr alphabet_type HTTP_COMMON_EXPORT alphabet
Definition: base64.h:421
static constexpr alphabet_type HTTP_COMMON_EXPORT alphabet
Definition: base64.h:405
static constexpr inverse_alphabet_type HTTP_COMMON_EXPORT inverse_alphabet
Definition: base64.h:415
static constexpr alphabet_type HTTP_COMMON_EXPORT alphabet
Definition: base64.h:483
static constexpr inverse_alphabet_type HTTP_COMMON_EXPORT inverse_alphabet
Definition: base64.h:493
static constexpr alphabet_type HTTP_COMMON_EXPORT alphabet
Definition: base64.h:467
static constexpr inverse_alphabet_type HTTP_COMMON_EXPORT inverse_alphabet
Definition: base64.h:477
Base64 alphabet for MCF.
Definition: base64.h:449
static constexpr alphabet_type HTTP_COMMON_EXPORT alphabet
Definition: base64.h:451
static constexpr inverse_alphabet_type HTTP_COMMON_EXPORT inverse_alphabet
Definition: base64.h:461
static constexpr alphabet_type HTTP_COMMON_EXPORT alphabet
Definition: base64.h:499
static constexpr inverse_alphabet_type HTTP_COMMON_EXPORT inverse_alphabet
Definition: base64.h:510
Base64 codec base class.
Definition: base64.h:520
static std::string encode(const std::vector< uint8_t > &decoded)
encode binary to base64.
Definition: base64.h:538
static std::vector< uint8_t > decode(const std::string &encoded)
decode a base64 encoded string to binary.
Definition: base64.h:530
Generic Base64 codec.
Definition: base64.h:119
static std::vector< uint8_t > decode(const std::string &encoded, const inverse_alphabet_type &inverse_alphabet)
Definition: base64.h:135
static std::string encode(const std::vector< uint8_t > &data, const alphabet_type &alphabet)
Definition: base64.h:241
std::array< int8_t, 256 > inverse_alphabet_type
type of all inverse mappings of alphabets.
Definition: base64.h:132
std::array< char, 64 > alphabet_type
type of all alphabet.
Definition: base64.h:124
#define HTTP_COMMON_EXPORT
Definition: http_common_export.h:39
constexpr inverse_alphabet_type inverse(const alphabet_type &v)
inverse
Definition: base64.h:398
constexpr int8_t find_pos_of_char(const alphabet_type &v, uint8_t character, size_t v_ndx=0)
find position of char in alphabet.
Definition: base64.h:334
std::index_sequence< 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111, 112, 113, 114, 115, 116, 117, 118, 119, 120, 121, 122, 123, 124, 125, 126, 127, 128, 129, 130, 131, 132, 133, 134, 135, 136, 137, 138, 139, 140, 141, 142, 143, 144, 145, 146, 147, 148, 149, 150, 151, 152, 153, 154, 155, 156, 157, 158, 159, 160, 161, 162, 163, 164, 165, 166, 167, 168, 169, 170, 171, 172, 173, 174, 175, 176, 177, 178, 179, 180, 181, 182, 183, 184, 185, 186, 187, 188, 189, 190, 191, 192, 193, 194, 195, 196, 197, 198, 199, 200, 201, 202, 203, 204, 205, 206, 207, 208, 209, 210, 211, 212, 213, 214, 215, 216, 217, 218, 219, 220, 221, 222, 223, 224, 225, 226, 227, 228, 229, 230, 231, 232, 233, 234, 235, 236, 237, 238, 239, 240, 241, 242, 243, 244, 245, 246, 247, 248, 249, 250, 251, 252, 253, 254, 255 > ndx_256
Definition: base64.h:380
constexpr inverse_alphabet_type make_inverse_array(const alphabet_type &v, std::index_sequence< I... >)
build inverse-alphabet.
Definition: base64.h:390
Base64Impl::inverse_alphabet_type inverse_alphabet_type
type of all inverse mappings of alphabets.
Definition: base64.h:325
Base64Impl::alphabet_type alphabet_type
type of all alphabet.
Definition: base64.h:317
Definition: ut0tuple.h:56
bool distance(const dd::Spatial_reference_system *srs, const Geometry *g1, const Geometry *g2, double *distance, bool *is_null) noexcept
Computes the distance between two geometries.
Definition: distance.cc:39
Base64Endianess
Definition: base64.h:38