32 #include "../include/swarmio/transport/base64.h" 36 "ABCDEFGHIJKLMNOPQRSTUVWXYZ" 37 "abcdefghijklmnopqrstuvwxyz" 42 return (isalnum(c) || (c ==
'+') || (c ==
'/'));
45 std::string
base64_encode(
unsigned char const* bytes_to_encode,
unsigned int in_len) {
49 unsigned char char_array_3[3];
50 unsigned char char_array_4[4];
53 char_array_3[i++] = *(bytes_to_encode++);
55 char_array_4[0] = (char_array_3[0] & 0xfc) >> 2;
56 char_array_4[1] = ((char_array_3[0] & 0x03) << 4) + ((char_array_3[1] & 0xf0) >> 4);
57 char_array_4[2] = ((char_array_3[1] & 0x0f) << 2) + ((char_array_3[2] & 0xc0) >> 6);
58 char_array_4[3] = char_array_3[2] & 0x3f;
60 for(i = 0; (i <4) ; i++)
68 for(j = i; j < 3; j++)
69 char_array_3[j] =
'\0';
71 char_array_4[0] = ( char_array_3[0] & 0xfc) >> 2;
72 char_array_4[1] = ((char_array_3[0] & 0x03) << 4) + ((char_array_3[1] & 0xf0) >> 4);
73 char_array_4[2] = ((char_array_3[1] & 0x0f) << 2) + ((char_array_3[2] & 0xc0) >> 6);
75 for (j = 0; (j < i + 1); j++)
88 size_t in_len = encoded_string.size();
92 unsigned char char_array_4[4], char_array_3[3];
95 while (in_len-- && ( encoded_string[in_] !=
'=') &&
is_base64(encoded_string[in_])) {
96 char_array_4[i++] = encoded_string[in_]; in_++;
98 for (i = 0; i <4; i++)
99 char_array_4[i] =
base64_chars.find(char_array_4[i]) & 0xff;
101 char_array_3[0] = ( char_array_4[0] << 2 ) + ((char_array_4[1] & 0x30) >> 4);
102 char_array_3[1] = ((char_array_4[1] & 0xf) << 4) + ((char_array_4[2] & 0x3c) >> 2);
103 char_array_3[2] = ((char_array_4[2] & 0x3) << 6) + char_array_4[3];
105 for (i = 0; (i < 3); i++)
106 ret += char_array_3[i];
112 for (j = 0; j < i; j++)
113 char_array_4[j] =
base64_chars.find(char_array_4[j]) & 0xff;
115 char_array_3[0] = (char_array_4[0] << 2) + ((char_array_4[1] & 0x30) >> 4);
116 char_array_3[1] = ((char_array_4[1] & 0xf) << 4) + ((char_array_4[2] & 0x3c) >> 2);
118 for (j = 0; (j < i - 1); j++) ret += char_array_3[j];
static const std::string base64_chars
static bool is_base64(unsigned char c)
std::string base64_encode(unsigned char const *bytes_to_encode, unsigned int in_len)
std::string base64_decode(std::string const &encoded_string)