Line data Source code
1 : #ifndef THINGER_UTIL_BASE64_HPP
2 : #define THINGER_UTIL_BASE64_HPP
3 :
4 : #include <string>
5 : #include <vector>
6 : #include <stdexcept>
7 :
8 : namespace thinger::util {
9 :
10 : class base64 {
11 : private:
12 : static constexpr const char* base64_chars =
13 : "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
14 : "abcdefghijklmnopqrstuvwxyz"
15 : "0123456789+/";
16 :
17 62 : static inline bool is_base64(unsigned char c) {
18 62 : return (isalnum(c) || (c == '+') || (c == '/'));
19 : }
20 :
21 : public:
22 38 : static std::string encode(const std::string& input) {
23 38 : return encode(reinterpret_cast<const unsigned char*>(input.c_str()), input.length());
24 : }
25 :
26 114 : static std::string encode(const unsigned char* bytes_to_encode, size_t in_len) {
27 114 : std::string ret;
28 114 : int i = 0;
29 114 : int j = 0;
30 : unsigned char char_array_3[3];
31 : unsigned char char_array_4[4];
32 :
33 2242 : while (in_len--) {
34 2128 : char_array_3[i++] = *(bytes_to_encode++);
35 2128 : if (i == 3) {
36 646 : char_array_4[0] = (char_array_3[0] & 0xfc) >> 2;
37 646 : char_array_4[1] = ((char_array_3[0] & 0x03) << 4) + ((char_array_3[1] & 0xf0) >> 4);
38 646 : char_array_4[2] = ((char_array_3[1] & 0x0f) << 2) + ((char_array_3[2] & 0xc0) >> 6);
39 646 : char_array_4[3] = char_array_3[2] & 0x3f;
40 :
41 3230 : for(i = 0; i < 4; i++)
42 2584 : ret += base64_chars[char_array_4[i]];
43 646 : i = 0;
44 : }
45 : }
46 :
47 114 : if (i) {
48 266 : for(j = i; j < 3; j++)
49 152 : char_array_3[j] = '\0';
50 :
51 114 : char_array_4[0] = (char_array_3[0] & 0xfc) >> 2;
52 114 : char_array_4[1] = ((char_array_3[0] & 0x03) << 4) + ((char_array_3[1] & 0xf0) >> 4);
53 114 : char_array_4[2] = ((char_array_3[1] & 0x0f) << 2) + ((char_array_3[2] & 0xc0) >> 6);
54 :
55 418 : for (j = 0; j < i + 1; j++)
56 304 : ret += base64_chars[char_array_4[j]];
57 :
58 266 : while((i++ < 3))
59 152 : ret += '=';
60 : }
61 :
62 228 : return ret;
63 0 : }
64 :
65 4 : static std::string decode(const std::string& encoded_string) {
66 4 : size_t in_len = encoded_string.size();
67 4 : int i = 0;
68 4 : int j = 0;
69 4 : int in_ = 0;
70 : unsigned char char_array_4[4], char_array_3[3];
71 4 : std::string ret;
72 :
73 66 : while (in_len-- && (encoded_string[in_] != '=') && is_base64(encoded_string[in_])) {
74 62 : char_array_4[i++] = encoded_string[in_]; in_++;
75 62 : if (i == 4) {
76 70 : for (i = 0; i < 4; i++) {
77 112 : char_array_4[i] = static_cast<unsigned char>(std::string(base64_chars).find(char_array_4[i]));
78 : }
79 :
80 14 : char_array_3[0] = (char_array_4[0] << 2) + ((char_array_4[1] & 0x30) >> 4);
81 14 : char_array_3[1] = ((char_array_4[1] & 0xf) << 4) + ((char_array_4[2] & 0x3c) >> 2);
82 14 : char_array_3[2] = ((char_array_4[2] & 0x3) << 6) + char_array_4[3];
83 :
84 56 : for (i = 0; i < 3; i++)
85 42 : ret += char_array_3[i];
86 14 : i = 0;
87 : }
88 : }
89 :
90 4 : if (i) {
91 8 : for (j = 0; j < i; j++) {
92 12 : char_array_4[j] = static_cast<unsigned char>(std::string(base64_chars).find(char_array_4[j]));
93 : }
94 :
95 2 : char_array_3[0] = (char_array_4[0] << 2) + ((char_array_4[1] & 0x30) >> 4);
96 2 : char_array_3[1] = ((char_array_4[1] & 0xf) << 4) + ((char_array_4[2] & 0x3c) >> 2);
97 :
98 6 : for (j = 0; j < i - 1; j++)
99 4 : ret += char_array_3[j];
100 : }
101 :
102 8 : return ret;
103 0 : }
104 : };
105 :
106 : } // namespace thinger::util
107 :
108 : #endif // THINGER_UTIL_BASE64_HPP
|