diff options
Diffstat (limited to 'ext/wittle_generator/Base64.h')
-rw-r--r-- | ext/wittle_generator/Base64.h | 127 |
1 files changed, 127 insertions, 0 deletions
diff --git a/ext/wittle_generator/Base64.h b/ext/wittle_generator/Base64.h new file mode 100644 index 0000000..2f30c1a --- /dev/null +++ b/ext/wittle_generator/Base64.h | |||
@@ -0,0 +1,127 @@ | |||
1 | #ifndef _MACARON_BASE64_H_ | ||
2 | #define _MACARON_BASE64_H_ | ||
3 | |||
4 | /** | ||
5 | * The MIT License (MIT) | ||
6 | * Copyright (c) 2016 tomykaira | ||
7 | * | ||
8 | * Permission is hereby granted, free of charge, to any person obtaining | ||
9 | * a copy of this software and associated documentation files (the | ||
10 | * "Software"), to deal in the Software without restriction, including | ||
11 | * without limitation the rights to use, copy, modify, merge, publish, | ||
12 | * distribute, sublicense, and/or sell copies of the Software, and to | ||
13 | * permit persons to whom the Software is furnished to do so, subject to | ||
14 | * the following conditions: | ||
15 | * | ||
16 | * The above copyright notice and this permission notice shall be | ||
17 | * included in all copies or substantial portions of the Software. | ||
18 | * | ||
19 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, | ||
20 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF | ||
21 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND | ||
22 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE | ||
23 | * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION | ||
24 | * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION | ||
25 | * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. | ||
26 | */ | ||
27 | |||
28 | #include <string> | ||
29 | |||
30 | namespace macaron { | ||
31 | |||
32 | class Base64 { | ||
33 | public: | ||
34 | static std::string Encode(const std::string data) { | ||
35 | static constexpr char sEncodingTable[] = { | ||
36 | 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', | ||
37 | 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', | ||
38 | 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', | ||
39 | 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', | ||
40 | '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '+', '/'}; | ||
41 | |||
42 | size_t in_len = data.size(); | ||
43 | size_t out_len = 4 * ((in_len + 2) / 3); | ||
44 | std::string ret(out_len, '\0'); | ||
45 | size_t i; | ||
46 | char* p = const_cast<char*>(ret.c_str()); | ||
47 | |||
48 | for (i = 0; i < in_len - 2; i += 3) { | ||
49 | *p++ = sEncodingTable[(data[i] >> 2) & 0x3F]; | ||
50 | *p++ = sEncodingTable[((data[i] & 0x3) << 4) | | ||
51 | ((int)(data[i + 1] & 0xF0) >> 4)]; | ||
52 | *p++ = sEncodingTable[((data[i + 1] & 0xF) << 2) | | ||
53 | ((int)(data[i + 2] & 0xC0) >> 6)]; | ||
54 | *p++ = sEncodingTable[data[i + 2] & 0x3F]; | ||
55 | } | ||
56 | if (i < in_len) { | ||
57 | *p++ = sEncodingTable[(data[i] >> 2) & 0x3F]; | ||
58 | if (i == (in_len - 1)) { | ||
59 | *p++ = sEncodingTable[((data[i] & 0x3) << 4)]; | ||
60 | *p++ = '='; | ||
61 | } else { | ||
62 | *p++ = sEncodingTable[((data[i] & 0x3) << 4) | | ||
63 | ((int)(data[i + 1] & 0xF0) >> 4)]; | ||
64 | *p++ = sEncodingTable[((data[i + 1] & 0xF) << 2)]; | ||
65 | } | ||
66 | *p++ = '='; | ||
67 | } | ||
68 | |||
69 | return ret; | ||
70 | } | ||
71 | |||
72 | static std::string Decode(const std::string& input, std::string& out) { | ||
73 | static constexpr unsigned char kDecodingTable[] = { | ||
74 | 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, | ||
75 | 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, | ||
76 | 64, 64, 64, 64, 64, 64, 64, 62, 64, 64, 64, 63, 52, 53, 54, 55, 56, 57, | ||
77 | 58, 59, 60, 61, 64, 64, 64, 64, 64, 64, 64, 0, 1, 2, 3, 4, 5, 6, | ||
78 | 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, | ||
79 | 25, 64, 64, 64, 64, 64, 64, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, | ||
80 | 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 64, 64, 64, | ||
81 | 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, | ||
82 | 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, | ||
83 | 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, | ||
84 | 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, | ||
85 | 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, | ||
86 | 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, | ||
87 | 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, | ||
88 | 64, 64, 64, 64}; | ||
89 | |||
90 | size_t in_len = input.size(); | ||
91 | if (in_len % 4 != 0) return "Input data size is not a multiple of 4"; | ||
92 | |||
93 | size_t out_len = in_len / 4 * 3; | ||
94 | if (input[in_len - 1] == '=') out_len--; | ||
95 | if (input[in_len - 2] == '=') out_len--; | ||
96 | |||
97 | out.resize(out_len); | ||
98 | |||
99 | for (size_t i = 0, j = 0; i < in_len;) { | ||
100 | uint32_t a = input[i] == '=' | ||
101 | ? 0 & i++ | ||
102 | : kDecodingTable[static_cast<int>(input[i++])]; | ||
103 | uint32_t b = input[i] == '=' | ||
104 | ? 0 & i++ | ||
105 | : kDecodingTable[static_cast<int>(input[i++])]; | ||
106 | uint32_t c = input[i] == '=' | ||
107 | ? 0 & i++ | ||
108 | : kDecodingTable[static_cast<int>(input[i++])]; | ||
109 | uint32_t d = input[i] == '=' | ||
110 | ? 0 & i++ | ||
111 | : kDecodingTable[static_cast<int>(input[i++])]; | ||
112 | |||
113 | uint32_t triple = | ||
114 | (a << 3 * 6) + (b << 2 * 6) + (c << 1 * 6) + (d << 0 * 6); | ||
115 | |||
116 | if (j < out_len) out[j++] = (triple >> 2 * 8) & 0xFF; | ||
117 | if (j < out_len) out[j++] = (triple >> 1 * 8) & 0xFF; | ||
118 | if (j < out_len) out[j++] = (triple >> 0 * 8) & 0xFF; | ||
119 | } | ||
120 | |||
121 | return ""; | ||
122 | } | ||
123 | }; | ||
124 | |||
125 | } // namespace macaron | ||
126 | |||
127 | #endif /* _MACARON_BASE64_H_ */ | ||