6 #include <samchon/ByteArray.hpp> 36 template <
typename Container>
37 static auto encode(
const Container &byte_array) -> std::string
39 static const std::array<char, 64> BASE64_CHAR_ARRAY =
41 'A',
'B',
'C',
'D',
'E',
'F',
'G',
'H',
42 'I',
'J',
'K',
'L',
'M',
'N',
'O',
'P',
43 'Q',
'R',
'S',
'T',
'U',
'V',
'W',
'X',
44 'Y',
'Z',
'a',
'b',
'c',
'd',
'e',
'f',
45 'g',
'h',
'i',
'j',
'k',
'l',
'm',
'n',
46 'o',
'p',
'q',
'r',
's',
't',
'u',
'v',
47 'w',
'x',
'y',
'z',
'0',
'1',
'2',
'3',
48 '4',
'5',
'6',
'7',
'8',
'9',
'+',
'/' 51 unsigned char *bytes = (
unsigned char*)byte_array.data();
53 str.reserve(byte_array.size() * 4);
55 long long left_size = (
long long)byte_array.size();
59 std::array<unsigned char, 3> input;
60 std::array<unsigned char, 4> output;
64 input[i++] = *(bytes++);
67 output[0] = (input[0] & 0xfc) >> 2;
68 output[1] = ((input[0] & 0x03) << 4) + ((input[1] & 0xf0) >> 4);
69 output[2] = ((input[1] & 0x0f) << 2) + ((input[2] & 0xc0) >> 6);
70 output[3] = input[2] & 0x3f;
72 for (i = 0; (i < 4); i++)
73 str += BASE64_CHAR_ARRAY[output[i]];
80 for (j = i; j < 3; j++)
83 output[0] = (input[0] & 0xfc) >> 2;
84 output[1] = ((input[0] & 0x03) << 4) + ((input[1] & 0xf0) >> 4);
85 output[2] = ((input[1] & 0x0f) << 2) + ((input[2] & 0xc0) >> 6);
86 output[3] = input[2] & 0x3f;
88 for (j = 0; (j < i + 1); j++)
89 str += BASE64_CHAR_ARRAY[output[j]];
109 static const std::array<int, 256> BASE64_DECODE_ARRAY =
111 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
112 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
113 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 62, -1, -1, -1, 63,
114 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, -1, -1, -1, -1, -1, -1,
115 -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14,
116 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, -1, -1, -1, -1, -1,
117 -1, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40,
118 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, -1, -1, -1, -1, -1,
119 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
120 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
121 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
122 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
123 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
124 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
125 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
126 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1
130 data.reserve(str.size() * 3);
132 int space_idx = 0, phase;
139 for (
size_t i = 0; i < str.size(); i++)
141 d = BASE64_DECODE_ARRAY[(int)str[i]];
151 c = ((prev_d << 2) | ((d & 0x30) >> 4));
156 c = (((prev_d & 0xf) << 4) | ((d & 0x3c) >> 2));
161 c = (((prev_d & 0x03) << 6) | d);
static auto encode(const Container &byte_array) -> std::string
Encode from binary data to base64-string.
static auto decode(const std::string &str) -> ByteArray
Decode from base64-string to binary data.
Utility class for base64 format's en-decoding.