36static const std::string base64_chars =
37 "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
38 "abcdefghijklmnopqrstuvwxyz"
42static inline bool is_base64(
unsigned char c) {
43 return (isalnum(c) || (c ==
'-') || (c ==
'_'));
46std::string base64_encode(
char const* bytes_to_encode,
unsigned int in_len) {
50 unsigned char char_array_3[3];
51 unsigned char char_array_4[4];
54 char_array_3[i++] = *(bytes_to_encode++);
56 char_array_4[0] = (char_array_3[0] & 0xfc) >> 2;
57 char_array_4[1] = ((char_array_3[0] & 0x03) << 4) + ((char_array_3[1] & 0xf0) >> 4);
58 char_array_4[2] = ((char_array_3[1] & 0x0f) << 2) + ((char_array_3[2] & 0xc0) >> 6);
59 char_array_4[3] = char_array_3[2] & 0x3f;
61 for(i = 0; (i <4) ; i++)
62 ret += base64_chars[char_array_4[i]];
69 for(j = i; j < 3; j++)
70 char_array_3[j] =
'\0';
72 char_array_4[0] = (char_array_3[0] & 0xfc) >> 2;
73 char_array_4[1] = ((char_array_3[0] & 0x03) << 4) + ((char_array_3[1] & 0xf0) >> 4);
74 char_array_4[2] = ((char_array_3[1] & 0x0f) << 2) + ((char_array_3[2] & 0xc0) >> 6);
75 char_array_4[3] = char_array_3[2] & 0x3f;
77 for (j = 0; (j < i + 1); j++)
78 ret += base64_chars[char_array_4[j]];
88std::string base64_decode(std::string
const& encoded_string) {
89 int in_len = encoded_string.size();
93 unsigned char char_array_4[4], char_array_3[3];
96 while (in_len-- && ( encoded_string[in_] !=
'.') && is_base64(encoded_string[in_])) {
97 char_array_4[i++] = encoded_string[in_]; in_++;
99 for (i = 0; i <4; i++)
100 char_array_4[i] = base64_chars.find(char_array_4[i]);
102 char_array_3[0] = (char_array_4[0] << 2) + ((char_array_4[1] & 0x30) >> 4);
103 char_array_3[1] = ((char_array_4[1] & 0xf) << 4) + ((char_array_4[2] & 0x3c) >> 2);
104 char_array_3[2] = ((char_array_4[2] & 0x3) << 6) + char_array_4[3];
106 for (i = 0; (i < 3); i++)
107 ret += char_array_3[i];
113 for (j = i; j <4; j++)
116 for (j = 0; j <4; j++)
117 char_array_4[j] = base64_chars.find(char_array_4[j]);
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 (j = 0; (j < i - 1); j++) ret += char_array_3[j];