1 /*
2 * nghttp2 - HTTP/2 C Library
3 *
4 * Copyright (c) 2013 Tatsuhiro Tsujikawa
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining
7 * a copy of this software and associated documentation files (the
8 * "Software"), to deal in the Software without restriction, including
9 * without limitation the rights to use, copy, modify, merge, publish,
10 * distribute, sublicense, and/or sell copies of the Software, and to
11 * permit persons to whom the Software is furnished to do so, subject to
12 * the following conditions:
13 *
14 * The above copyright notice and this permission notice shall be
15 * included in all copies or substantial portions of the Software.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
20 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
21 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
22 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
23 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24 */
25 #ifndef BASE64_H
26 #define BASE64_H
27
28 #include "nghttp2_config.h"
29
30 #include <string>
31
32 #include "template.h"
33 #include "allocator.h"
34
35 namespace nghttp2 {
36
37 namespace base64 {
38
39 namespace {
40 constexpr char B64_CHARS[] = {
41 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M',
42 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z',
43 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm',
44 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z',
45 '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '+', '/',
46 };
47 } // namespace
48
encode(InputIt first,InputIt last)49 template <typename InputIt> std::string encode(InputIt first, InputIt last) {
50 std::string res;
51 size_t len = last - first;
52 if (len == 0) {
53 return res;
54 }
55 size_t r = len % 3;
56 res.resize((len + 2) / 3 * 4);
57 auto j = last - r;
58 auto p = std::begin(res);
59 while (first != j) {
60 uint32_t n = static_cast<uint8_t>(*first++) << 16;
61 n += static_cast<uint8_t>(*first++) << 8;
62 n += static_cast<uint8_t>(*first++);
63 *p++ = B64_CHARS[n >> 18];
64 *p++ = B64_CHARS[(n >> 12) & 0x3fu];
65 *p++ = B64_CHARS[(n >> 6) & 0x3fu];
66 *p++ = B64_CHARS[n & 0x3fu];
67 }
68
69 if (r == 2) {
70 uint32_t n = static_cast<uint8_t>(*first++) << 16;
71 n += static_cast<uint8_t>(*first++) << 8;
72 *p++ = B64_CHARS[n >> 18];
73 *p++ = B64_CHARS[(n >> 12) & 0x3fu];
74 *p++ = B64_CHARS[(n >> 6) & 0x3fu];
75 *p++ = '=';
76 } else if (r == 1) {
77 uint32_t n = static_cast<uint8_t>(*first++) << 16;
78 *p++ = B64_CHARS[n >> 18];
79 *p++ = B64_CHARS[(n >> 12) & 0x3fu];
80 *p++ = '=';
81 *p++ = '=';
82 }
83 return res;
84 }
85
encode_length(size_t n)86 constexpr size_t encode_length(size_t n) { return (n + 2) / 3 * 4; }
87
88 template <typename InputIt, typename OutputIt>
encode(InputIt first,InputIt last,OutputIt d_first)89 OutputIt encode(InputIt first, InputIt last, OutputIt d_first) {
90 size_t len = last - first;
91 if (len == 0) {
92 return d_first;
93 }
94 auto r = len % 3;
95 auto j = last - r;
96 auto p = d_first;
97 while (first != j) {
98 uint32_t n = static_cast<uint8_t>(*first++) << 16;
99 n += static_cast<uint8_t>(*first++) << 8;
100 n += static_cast<uint8_t>(*first++);
101 *p++ = B64_CHARS[n >> 18];
102 *p++ = B64_CHARS[(n >> 12) & 0x3fu];
103 *p++ = B64_CHARS[(n >> 6) & 0x3fu];
104 *p++ = B64_CHARS[n & 0x3fu];
105 }
106
107 switch (r) {
108 case 2: {
109 uint32_t n = static_cast<uint8_t>(*first++) << 16;
110 n += static_cast<uint8_t>(*first++) << 8;
111 *p++ = B64_CHARS[n >> 18];
112 *p++ = B64_CHARS[(n >> 12) & 0x3fu];
113 *p++ = B64_CHARS[(n >> 6) & 0x3fu];
114 *p++ = '=';
115 break;
116 }
117 case 1: {
118 uint32_t n = static_cast<uint8_t>(*first++) << 16;
119 *p++ = B64_CHARS[n >> 18];
120 *p++ = B64_CHARS[(n >> 12) & 0x3fu];
121 *p++ = '=';
122 *p++ = '=';
123 break;
124 }
125 }
126 return p;
127 }
128
129 template <typename InputIt>
next_decode_input(InputIt first,InputIt last,const int * tbl)130 InputIt next_decode_input(InputIt first, InputIt last, const int *tbl) {
131 for (; first != last; ++first) {
132 if (tbl[static_cast<size_t>(*first)] != -1 || *first == '=') {
133 break;
134 }
135 }
136 return first;
137 }
138
139 template <typename InputIt, typename OutputIt>
decode(InputIt first,InputIt last,OutputIt d_first)140 OutputIt decode(InputIt first, InputIt last, OutputIt d_first) {
141 static constexpr int INDEX_TABLE[] = {
142 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
143 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
144 -1, -1, -1, -1, -1, -1, -1, 62, -1, -1, -1, 63, 52, 53, 54, 55, 56, 57,
145 58, 59, 60, 61, -1, -1, -1, -1, -1, -1, -1, 0, 1, 2, 3, 4, 5, 6,
146 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24,
147 25, -1, -1, -1, -1, -1, -1, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36,
148 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, -1, -1, -1,
149 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
150 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
151 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
152 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
153 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
154 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
155 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
156 -1, -1, -1, -1};
157 assert(std::distance(first, last) % 4 == 0);
158 auto p = d_first;
159 for (; first != last;) {
160 uint32_t n = 0;
161 for (int i = 1; i <= 4; ++i, ++first) {
162 auto idx = INDEX_TABLE[static_cast<size_t>(*first)];
163 if (idx == -1) {
164 if (i <= 2) {
165 return d_first;
166 }
167 if (i == 3) {
168 if (*first == '=' && *(first + 1) == '=' && first + 2 == last) {
169 *p++ = n >> 16;
170 return p;
171 }
172 return d_first;
173 }
174 if (*first == '=' && first + 1 == last) {
175 *p++ = n >> 16;
176 *p++ = n >> 8 & 0xffu;
177 return p;
178 }
179 return d_first;
180 }
181
182 n += idx << (24 - i * 6);
183 }
184
185 *p++ = n >> 16;
186 *p++ = n >> 8 & 0xffu;
187 *p++ = n & 0xffu;
188 }
189
190 return p;
191 }
192
decode(InputIt first,InputIt last)193 template <typename InputIt> std::string decode(InputIt first, InputIt last) {
194 auto len = std::distance(first, last);
195 if (len % 4 != 0) {
196 return "";
197 }
198 std::string res;
199 res.resize(len / 4 * 3);
200
201 res.erase(decode(first, last, std::begin(res)), std::end(res));
202
203 return res;
204 }
205
206 template <typename InputIt>
decode(BlockAllocator & balloc,InputIt first,InputIt last)207 StringRef decode(BlockAllocator &balloc, InputIt first, InputIt last) {
208 auto len = std::distance(first, last);
209 if (len % 4 != 0) {
210 return StringRef::from_lit("");
211 }
212 auto iov = make_byte_ref(balloc, len / 4 * 3 + 1);
213 auto p = iov.base;
214
215 p = decode(first, last, p);
216 *p = '\0';
217
218 return StringRef{iov.base, p};
219 }
220
221 } // namespace base64
222
223 } // namespace nghttp2
224
225 #endif // BASE64_H
226