tizen 2.4 release
[external/nghttp2.git] / src / base64.h
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 namespace nghttp2 {
33
34 namespace base64 {
35
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', '+', '/',
44   };
45   std::string res;
46   size_t len = last - first;
47   if (len == 0) {
48     return res;
49   }
50   size_t r = len % 3;
51   InputIterator j = last - r;
52   char temp[4];
53   while (first != j) {
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));
62   }
63   if (r == 2) {
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];
69     temp[3] = '=';
70     res.append(temp, sizeof(temp));
71   } else if (r == 1) {
72     int n = static_cast<unsigned char>(*first++) << 16;
73     temp[0] = CHAR_TABLE[n >> 18];
74     temp[1] = CHAR_TABLE[(n >> 12) & 0x3fu];
75     temp[2] = '=';
76     temp[3] = '=';
77     res.append(temp, sizeof(temp));
78   }
79   return res;
80 }
81
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 == '=') {
86       break;
87     }
88   }
89   return first;
90 }
91
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,
109       -1, -1, -1, -1};
110   std::string res;
111   InputIterator k[4];
112   int eq = 0;
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)
119         if (i != 1) {
120           res.clear();
121         }
122         return res;
123       } else if (*k[i - 1] == '=' && eq == 0) {
124         eq = i;
125       }
126       first = k[i - 1] + 1;
127     }
128     if (eq) {
129       break;
130     }
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])];
135     res += n >> 16;
136     res += n >> 8 & 0xffu;
137     res += n & 0xffu;
138   }
139   if (eq) {
140     if (eq <= 2) {
141       res.clear();
142       return res;
143     } else {
144       for (int i = eq; i <= 4; ++i) {
145         if (*k[i - 1] != '=') {
146           res.clear();
147           return res;
148         }
149       }
150       if (eq == 3) {
151         int n = (INDEX_TABLE[static_cast<unsigned char>(*k[0])] << 18) +
152                 (INDEX_TABLE[static_cast<unsigned char>(*k[1])] << 12);
153         res += n >> 16;
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);
158         res += n >> 16;
159         res += n >> 8 & 0xffu;
160       }
161     }
162   }
163   return res;
164 }
165
166 } // namespace base64
167
168 } // namespace nghttp2
169
170 #endif // BASE64_H