• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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, -1,
143     -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
144     -1, -1, -1, -1, -1, 62, -1, -1, -1, 63, 52, 53, 54, 55, 56, 57, 58, 59, 60,
145     61, -1, -1, -1, -1, -1, -1, -1, 0,  1,  2,  3,  4,  5,  6,  7,  8,  9,  10,
146     11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, -1, -1, -1, -1,
147     -1, -1, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42,
148     43, 44, 45, 46, 47, 48, 49, 50, 51, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
149     -1, -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, -1,
151     -1, -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, -1,
153     -1, -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, -1,
155     -1, -1, -1, -1, -1, -1, -1, -1, -1};
156   assert(std::distance(first, last) % 4 == 0);
157   auto p = d_first;
158   for (; first != last;) {
159     uint32_t n = 0;
160     for (int i = 1; i <= 4; ++i, ++first) {
161       auto idx = INDEX_TABLE[static_cast<size_t>(*first)];
162       if (idx == -1) {
163         if (i <= 2) {
164           return d_first;
165         }
166         if (i == 3) {
167           if (*first == '=' && *(first + 1) == '=' && first + 2 == last) {
168             *p++ = n >> 16;
169             return p;
170           }
171           return d_first;
172         }
173         if (*first == '=' && first + 1 == last) {
174           *p++ = n >> 16;
175           *p++ = n >> 8 & 0xffu;
176           return p;
177         }
178         return d_first;
179       }
180 
181       n += idx << (24 - i * 6);
182     }
183 
184     *p++ = n >> 16;
185     *p++ = n >> 8 & 0xffu;
186     *p++ = n & 0xffu;
187   }
188 
189   return p;
190 }
191 
decode(InputIt first,InputIt last)192 template <typename InputIt> std::string decode(InputIt first, InputIt last) {
193   auto len = std::distance(first, last);
194   if (len % 4 != 0) {
195     return "";
196   }
197   std::string res;
198   res.resize(len / 4 * 3);
199 
200   res.erase(decode(first, last, std::begin(res)), std::end(res));
201 
202   return res;
203 }
204 
205 template <typename InputIt>
decode(BlockAllocator & balloc,InputIt first,InputIt last)206 std::span<const uint8_t> decode(BlockAllocator &balloc, InputIt first,
207                                 InputIt last) {
208   auto len = std::distance(first, last);
209   if (len % 4 != 0) {
210     return {};
211   }
212   auto iov = make_byte_ref(balloc, len / 4 * 3 + 1);
213   auto p = std::begin(iov);
214 
215   p = decode(first, last, p);
216   *p = '\0';
217 
218   return {std::begin(iov), p};
219 }
220 
221 } // namespace base64
222 
223 } // namespace nghttp2
224 
225 #endif // BASE64_H
226