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