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