blob: beb76ebdff5c5849fa2bd52aa8bd0c8937c55b94 [file] [log] [blame]
David Reissea2cba82009-03-30 21:35:00 +00001/*
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied. See the License for the
16 * specific language governing permissions and limitations
17 * under the License.
18 */
David Reissdb0ea152008-02-18 01:49:37 +000019
Roger Meier4285ba22013-06-10 21:17:23 +020020#include <thrift/protocol/TBase64Utils.h>
David Reissdb0ea152008-02-18 01:49:37 +000021
22#include <boost/static_assert.hpp>
23
24using std::string;
25
Konrad Grochowski16a23a62014-11-13 15:33:38 +010026namespace apache {
27namespace thrift {
28namespace protocol {
David Reissdb0ea152008-02-18 01:49:37 +000029
Konrad Grochowski16a23a62014-11-13 15:33:38 +010030static const uint8_t* kBase64EncodeTable
31 = (const uint8_t*)"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
David Reissdb0ea152008-02-18 01:49:37 +000032
Konrad Grochowski16a23a62014-11-13 15:33:38 +010033void base64_encode(const uint8_t* in, uint32_t len, uint8_t* buf) {
Roger Meier4cb8e402012-05-27 18:05:16 +000034 buf[0] = kBase64EncodeTable[(in[0] >> 2) & 0x3f];
David Reissdb0ea152008-02-18 01:49:37 +000035 if (len == 3) {
Roger Meier4cb8e402012-05-27 18:05:16 +000036 buf[1] = kBase64EncodeTable[((in[0] << 4) & 0x30) | ((in[1] >> 4) & 0x0f)];
37 buf[2] = kBase64EncodeTable[((in[1] << 2) & 0x3c) | ((in[2] >> 6) & 0x03)];
David Reissdb0ea152008-02-18 01:49:37 +000038 buf[3] = kBase64EncodeTable[in[2] & 0x3f];
39 } else if (len == 2) {
Roger Meier4cb8e402012-05-27 18:05:16 +000040 buf[1] = kBase64EncodeTable[((in[0] << 4) & 0x30) | ((in[1] >> 4) & 0x0f)];
41 buf[2] = kBase64EncodeTable[(in[1] << 2) & 0x3c];
Konrad Grochowski16a23a62014-11-13 15:33:38 +010042 } else { // len == 1
Roger Meier4cb8e402012-05-27 18:05:16 +000043 buf[1] = kBase64EncodeTable[(in[0] << 4) & 0x30];
David Reissdb0ea152008-02-18 01:49:37 +000044 }
45}
46
Konrad Grochowski16a23a62014-11-13 15:33:38 +010047static const uint8_t kBase64DecodeTable[256] = {
48 0xff,
49 0xff,
50 0xff,
51 0xff,
52 0xff,
53 0xff,
54 0xff,
55 0xff,
56 0xff,
57 0xff,
58 0xff,
59 0xff,
60 0xff,
61 0xff,
62 0xff,
63 0xff,
64 0xff,
65 0xff,
66 0xff,
67 0xff,
68 0xff,
69 0xff,
70 0xff,
71 0xff,
72 0xff,
73 0xff,
74 0xff,
75 0xff,
76 0xff,
77 0xff,
78 0xff,
79 0xff,
80 0xff,
81 0xff,
82 0xff,
83 0xff,
84 0xff,
85 0xff,
86 0xff,
87 0xff,
88 0xff,
89 0xff,
90 0xff,
91 0x3e,
92 0xff,
93 0xff,
94 0xff,
95 0x3f,
96 0x34,
97 0x35,
98 0x36,
99 0x37,
100 0x38,
101 0x39,
102 0x3a,
103 0x3b,
104 0x3c,
105 0x3d,
106 0xff,
107 0xff,
108 0xff,
109 0xff,
110 0xff,
111 0xff,
112 0xff,
113 0x00,
114 0x01,
115 0x02,
116 0x03,
117 0x04,
118 0x05,
119 0x06,
120 0x07,
121 0x08,
122 0x09,
123 0x0a,
124 0x0b,
125 0x0c,
126 0x0d,
127 0x0e,
128 0x0f,
129 0x10,
130 0x11,
131 0x12,
132 0x13,
133 0x14,
134 0x15,
135 0x16,
136 0x17,
137 0x18,
138 0x19,
139 0xff,
140 0xff,
141 0xff,
142 0xff,
143 0xff,
144 0xff,
145 0x1a,
146 0x1b,
147 0x1c,
148 0x1d,
149 0x1e,
150 0x1f,
151 0x20,
152 0x21,
153 0x22,
154 0x23,
155 0x24,
156 0x25,
157 0x26,
158 0x27,
159 0x28,
160 0x29,
161 0x2a,
162 0x2b,
163 0x2c,
164 0x2d,
165 0x2e,
166 0x2f,
167 0x30,
168 0x31,
169 0x32,
170 0x33,
171 0xff,
172 0xff,
173 0xff,
174 0xff,
175 0xff,
176 0xff,
177 0xff,
178 0xff,
179 0xff,
180 0xff,
181 0xff,
182 0xff,
183 0xff,
184 0xff,
185 0xff,
186 0xff,
187 0xff,
188 0xff,
189 0xff,
190 0xff,
191 0xff,
192 0xff,
193 0xff,
194 0xff,
195 0xff,
196 0xff,
197 0xff,
198 0xff,
199 0xff,
200 0xff,
201 0xff,
202 0xff,
203 0xff,
204 0xff,
205 0xff,
206 0xff,
207 0xff,
208 0xff,
209 0xff,
210 0xff,
211 0xff,
212 0xff,
213 0xff,
214 0xff,
215 0xff,
216 0xff,
217 0xff,
218 0xff,
219 0xff,
220 0xff,
221 0xff,
222 0xff,
223 0xff,
224 0xff,
225 0xff,
226 0xff,
227 0xff,
228 0xff,
229 0xff,
230 0xff,
231 0xff,
232 0xff,
233 0xff,
234 0xff,
235 0xff,
236 0xff,
237 0xff,
238 0xff,
239 0xff,
240 0xff,
241 0xff,
242 0xff,
243 0xff,
244 0xff,
245 0xff,
246 0xff,
247 0xff,
248 0xff,
249 0xff,
250 0xff,
251 0xff,
252 0xff,
253 0xff,
254 0xff,
255 0xff,
256 0xff,
257 0xff,
258 0xff,
259 0xff,
260 0xff,
261 0xff,
262 0xff,
263 0xff,
264 0xff,
265 0xff,
266 0xff,
267 0xff,
268 0xff,
269 0xff,
270 0xff,
271 0xff,
272 0xff,
273 0xff,
274 0xff,
275 0xff,
276 0xff,
277 0xff,
278 0xff,
279 0xff,
280 0xff,
281 0xff,
282 0xff,
283 0xff,
284 0xff,
285 0xff,
286 0xff,
287 0xff,
288 0xff,
289 0xff,
290 0xff,
291 0xff,
292 0xff,
293 0xff,
294 0xff,
295 0xff,
296 0xff,
297 0xff,
298 0xff,
299 0xff,
300 0xff,
301 0xff,
302 0xff,
303 0xff,
David Reissdb0ea152008-02-18 01:49:37 +0000304};
305
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100306void base64_decode(uint8_t* buf, uint32_t len) {
307 buf[0] = (kBase64DecodeTable[buf[0]] << 2) | (kBase64DecodeTable[buf[1]] >> 4);
David Reissdb0ea152008-02-18 01:49:37 +0000308 if (len > 2) {
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100309 buf[1] = ((kBase64DecodeTable[buf[1]] << 4) & 0xf0) | (kBase64DecodeTable[buf[2]] >> 2);
David Reissdb0ea152008-02-18 01:49:37 +0000310 if (len > 3) {
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100311 buf[2] = ((kBase64DecodeTable[buf[2]] << 6) & 0xc0) | (kBase64DecodeTable[buf[3]]);
David Reissdb0ea152008-02-18 01:49:37 +0000312 }
313 }
314}
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100315}
316}
317} // apache::thrift::protocol