2 * nghttp2 - HTTP/2 C Library
4 * Copyright (c) 2013 Tatsuhiro Tsujikawa
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:
14 * The above copyright notice and this permission notice shall be
15 * included in all copies or substantial portions of the Software.
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.
28 #include "nghttp2_config.h"
36 template <typename InputIterator>
37 std::string encode(InputIterator first, InputIterator last) {
38 static const char CHAR_TABLE[] = {
39 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M',
40 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z',
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 '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '+', '/',
46 size_t len = last - first;
51 InputIterator j = last - r;
54 int n = static_cast<unsigned char>(*first++) << 16;
55 n += static_cast<unsigned char>(*first++) << 8;
56 n += static_cast<unsigned char>(*first++);
57 temp[0] = CHAR_TABLE[n >> 18];
58 temp[1] = CHAR_TABLE[(n >> 12) & 0x3fu];
59 temp[2] = CHAR_TABLE[(n >> 6) & 0x3fu];
60 temp[3] = CHAR_TABLE[n & 0x3fu];
61 res.append(temp, sizeof(temp));
64 int n = static_cast<unsigned char>(*first++) << 16;
65 n += static_cast<unsigned char>(*first++) << 8;
66 temp[0] = CHAR_TABLE[n >> 18];
67 temp[1] = CHAR_TABLE[(n >> 12) & 0x3fu];
68 temp[2] = CHAR_TABLE[(n >> 6) & 0x3fu];
70 res.append(temp, sizeof(temp));
72 int n = static_cast<unsigned char>(*first++) << 16;
73 temp[0] = CHAR_TABLE[n >> 18];
74 temp[1] = CHAR_TABLE[(n >> 12) & 0x3fu];
77 res.append(temp, sizeof(temp));
82 template <typename InputIterator>
83 InputIterator getNext(InputIterator first, InputIterator last, const int *tbl) {
84 for (; first != last; ++first) {
85 if (tbl[static_cast<size_t>(*first)] != -1 || *first == '=') {
92 template <typename InputIterator>
93 std::string decode(InputIterator first, InputIterator last) {
94 static const int INDEX_TABLE[] = {
95 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
96 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
97 -1, -1, -1, -1, -1, -1, -1, 62, -1, -1, -1, 63, 52, 53, 54, 55, 56, 57,
98 58, 59, 60, 61, -1, -1, -1, -1, -1, -1, -1, 0, 1, 2, 3, 4, 5, 6,
99 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24,
100 25, -1, -1, -1, -1, -1, -1, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36,
101 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, -1, -1, -1,
102 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
103 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
104 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
105 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
106 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
107 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
108 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
113 for (; first != last;) {
114 for (int i = 1; i <= 4; ++i) {
115 k[i - 1] = getNext(first, last, INDEX_TABLE);
116 if (k[i - 1] == last) {
117 // If i == 1, input may look like this: "TWFu\n" (i.e.,
118 // garbage at the end)
123 } else if (*k[i - 1] == '=' && eq == 0) {
126 first = k[i - 1] + 1;
131 int n = (INDEX_TABLE[static_cast<unsigned char>(*k[0])] << 18) +
132 (INDEX_TABLE[static_cast<unsigned char>(*k[1])] << 12) +
133 (INDEX_TABLE[static_cast<unsigned char>(*k[2])] << 6) +
134 INDEX_TABLE[static_cast<unsigned char>(*k[3])];
136 res += n >> 8 & 0xffu;
144 for (int i = eq; i <= 4; ++i) {
145 if (*k[i - 1] != '=') {
151 int n = (INDEX_TABLE[static_cast<unsigned char>(*k[0])] << 18) +
152 (INDEX_TABLE[static_cast<unsigned char>(*k[1])] << 12);
154 } else if (eq == 4) {
155 int n = (INDEX_TABLE[static_cast<unsigned char>(*k[0])] << 18) +
156 (INDEX_TABLE[static_cast<unsigned char>(*k[1])] << 12) +
157 (INDEX_TABLE[static_cast<unsigned char>(*k[2])] << 6);
159 res += n >> 8 & 0xffu;
166 } // namespace base64
168 } // namespace nghttp2