34 static inline bool is_base64(
byte c)
36 return (isalnum(c) || (c ==
'+') || (c ==
'/'));
39 static inline byte find_base64_char_index(
byte c)
41 if (
'A' <= c && c <=
'Z')
return c -
'A';
42 else if (
'a' <= c && c <=
'z')
return c -
'a' + 1 + find_base64_char_index(
'Z');
43 else if (
'0' <= c && c <=
'9')
return c -
'0' + 1 + find_base64_char_index(
'z');
44 else if (c ==
'+')
return 1 + find_base64_char_index(
'9');
45 else if (c ==
'/')
return 1 + find_base64_char_index(
'+');
46 else return 1 + find_base64_char_index(
'/');
51 static const char base64_chars[] =
52 "ABCDEFGHIJKLMNOPQRSTUVWXYZ" 53 "abcdefghijklmnopqrstuvwxyz" 62 auto buf = _in.
data();
63 auto bufLen = _in.
size();
67 char_array_3[i++] = *(buf++);
70 char_array_4[0] = (char_array_3[0] & 0xfc) >> 2;
71 char_array_4[1] = ((char_array_3[0] & 0x03) << 4) + ((char_array_3[1] & 0xf0) >> 4);
72 char_array_4[2] = ((char_array_3[1] & 0x0f) << 2) + ((char_array_3[2] & 0xc0) >> 6);
73 char_array_4[3] = char_array_3[2] & 0x3f;
75 for (i = 0; i < 4; i++)
76 ret += base64_chars[char_array_4[i]];
83 for (j = i; j < 3; j++)
84 char_array_3[j] =
'\0';
86 char_array_4[0] = (char_array_3[0] & 0xfc) >> 2;
87 char_array_4[1] = ((char_array_3[0] & 0x03) << 4) + ((char_array_3[1] & 0xf0) >> 4);
88 char_array_4[2] = ((char_array_3[1] & 0x0f) << 2) + ((char_array_3[2] & 0xc0) >> 6);
89 char_array_4[3] = char_array_3[2] & 0x3f;
91 for (j = 0; j < i + 1; j++)
92 ret += base64_chars[char_array_4[j]];
103 auto in_len = encoded_string.size();
107 byte char_array_3[3];
108 byte char_array_4[4];
111 while (in_len-- && encoded_string[in_] !=
'=' && is_base64(encoded_string[in_]))
113 char_array_4[i++] = encoded_string[in_]; in_++;
116 for (i = 0; i < 4; i++)
117 char_array_4[i] = find_base64_char_index(char_array_4[i]);
119 char_array_3[0] = (char_array_4[0] << 2) + ((char_array_4[1] & 0x30) >> 4);
120 char_array_3[1] = ((char_array_4[1] & 0xf) << 4) + ((char_array_4[2] & 0x3c) >> 2);
121 char_array_3[2] = ((char_array_4[2] & 0x3) << 6) + char_array_4[3];
123 for (i = 0; (i < 3); i++)
124 ret.push_back(char_array_3[i]);
131 for (j = i; j < 4; j++)
134 for (j = 0; j < 4; j++)
135 char_array_4[j] = find_base64_char_index(char_array_4[j]);
137 char_array_3[0] = (char_array_4[0] << 2) + ((char_array_4[1] & 0x30) >> 4);
138 char_array_3[1] = ((char_array_4[1] & 0xf) << 4) + ((char_array_4[2] & 0x3c) >> 2);
139 char_array_3[2] = ((char_array_4[2] & 0x3) << 6) + char_array_4[3];
141 for (j = 0; j < i - 1; j++)
142 ret.push_back(char_array_3[j]);
Adapted from code found on http://stackoverflow.com/questions/180947/base64-decode-snippet-in-c Origi...
std::hash for asio::adress
std::vector< byte > bytes
std::string toBase64(bytesConstRef _in)
bytes fromBase64(std::string const &_in)