45 static const std::string base64_chars =
46 "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
47 "abcdefghijklmnopqrstuvwxyz"
51 static inline bool is_base64(
unsigned char c) {
52 return (isalnum(
c) || (
c ==
'+') || (
c ==
'/'));
55 std::string
base64_encode(
const unsigned char* bytes_to_encode,
unsigned int in_len) {
59 unsigned char char_array_3[3];
60 unsigned char char_array_4[4];
63 char_array_3[
i++] = *(bytes_to_encode++);
65 char_array_4[0] = (char_array_3[0] & 0xfc) >> 2;
66 char_array_4[1] = ((char_array_3[0] & 0x03) << 4) + ((char_array_3[1] & 0xf0) >> 4);
67 char_array_4[2] = ((char_array_3[1] & 0x0f) << 2) + ((char_array_3[2] & 0xc0) >> 6);
68 char_array_4[3] = char_array_3[2] & 0x3f;
70 for(
i = 0; (
i <4) ;
i++)
71 ret += base64_chars[char_array_4[
i]];
78 for(j =
i; j < 3; j++)
79 char_array_3[j] =
'\0';
81 char_array_4[0] = ( char_array_3[0] & 0xfc) >> 2;
82 char_array_4[1] = ((char_array_3[0] & 0x03) << 4) + ((char_array_3[1] & 0xf0) >> 4);
83 char_array_4[2] = ((char_array_3[1] & 0x0f) << 2) + ((char_array_3[2] & 0xc0) >> 6);
85 for (j = 0; (j <
i + 1); j++)
86 ret += base64_chars[char_array_4[j]];
97 std::vector<unsigned char>
base64_decode(
const std::string& encoded_string) {
98 int in_len = encoded_string.size();
102 unsigned char char_array_4[4], char_array_3[3];
103 std::vector<unsigned char> ret;
105 while (in_len-- && ( encoded_string[in_] !=
'=') && is_base64(encoded_string[in_])) {
106 char_array_4[
i++] = encoded_string[in_]; in_++;
108 for (
i = 0;
i <4;
i++)
109 char_array_4[
i] = base64_chars.find(char_array_4[
i]);
111 char_array_3[0] = (char_array_4[0] << 2) + ((char_array_4[1] & 0x30) >> 4);
112 char_array_3[1] = ((char_array_4[1] & 0xf) << 4) + ((char_array_4[2] & 0x3c) >> 2);
113 char_array_3[2] = ((char_array_4[2] & 0x3) << 6) + char_array_4[3];
115 for (
i = 0; (
i < 3);
i++)
116 ret.push_back(char_array_3[
i]);
122 for (j =
i; j <4; j++)
125 for (j = 0; j <4; j++)
126 char_array_4[j] = base64_chars.find(char_array_4[j]);
128 char_array_3[0] = (char_array_4[0] << 2) + ((char_array_4[1] & 0x30) >> 4);
129 char_array_3[1] = ((char_array_4[1] & 0xf) << 4) + ((char_array_4[2] & 0x3c) >> 2);
130 char_array_3[2] = ((char_array_4[2] & 0x3) << 6) + char_array_4[3];
132 for (j = 0; (j <
i - 1); j++) ret.push_back(char_array_3[j]);