| 30 |
ashish |
1 |
/*
|
|
|
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 |
*/
|
|
|
19 |
|
|
|
20 |
#include "TBase64Utils.h"
|
|
|
21 |
|
|
|
22 |
#include <boost/static_assert.hpp>
|
|
|
23 |
|
|
|
24 |
using std::string;
|
|
|
25 |
|
|
|
26 |
namespace apache { namespace thrift { namespace protocol {
|
|
|
27 |
|
|
|
28 |
|
|
|
29 |
static const uint8_t *kBase64EncodeTable = (const uint8_t *)
|
|
|
30 |
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
|
|
|
31 |
|
|
|
32 |
void base64_encode(const uint8_t *in, uint32_t len, uint8_t *buf) {
|
|
|
33 |
buf[0] = kBase64EncodeTable[(in[0] >> 2) & 0x3F];
|
|
|
34 |
if (len == 3) {
|
|
|
35 |
buf[1] = kBase64EncodeTable[((in[0] << 4) + (in[1] >> 4)) & 0x3f];
|
|
|
36 |
buf[2] = kBase64EncodeTable[((in[1] << 2) + (in[2] >> 6)) & 0x3f];
|
|
|
37 |
buf[3] = kBase64EncodeTable[in[2] & 0x3f];
|
|
|
38 |
} else if (len == 2) {
|
|
|
39 |
buf[1] = kBase64EncodeTable[((in[0] << 4) + (in[1] >> 4)) & 0x3f];
|
|
|
40 |
buf[2] = kBase64EncodeTable[(in[1] << 2) & 0x3f];
|
|
|
41 |
} else { // len == 1
|
|
|
42 |
buf[1] = kBase64EncodeTable[(in[0] << 4) & 0x3f];
|
|
|
43 |
}
|
|
|
44 |
}
|
|
|
45 |
|
|
|
46 |
static const uint8_t kBase64DecodeTable[256] ={
|
|
|
47 |
-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
|
|
|
48 |
-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
|
|
|
49 |
-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,62,-1,-1,-1,63,
|
|
|
50 |
52,53,54,55,56,57,58,59,60,61,-1,-1,-1,-1,-1,-1,
|
|
|
51 |
-1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,10,11,12,13,14,
|
|
|
52 |
15,16,17,18,19,20,21,22,23,24,25,-1,-1,-1,-1,-1,
|
|
|
53 |
-1,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,
|
|
|
54 |
41,42,43,44,45,46,47,48,49,50,51,-1,-1,-1,-1,-1,
|
|
|
55 |
-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
|
|
|
56 |
-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
|
|
|
57 |
-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
|
|
|
58 |
-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
|
|
|
59 |
-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
|
|
|
60 |
-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
|
|
|
61 |
-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
|
|
|
62 |
-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
|
|
|
63 |
};
|
|
|
64 |
|
|
|
65 |
void base64_decode(uint8_t *buf, uint32_t len) {
|
|
|
66 |
buf[0] = (kBase64DecodeTable[buf[0]] << 2) |
|
|
|
67 |
(kBase64DecodeTable[buf[1]] >> 4);
|
|
|
68 |
if (len > 2) {
|
|
|
69 |
buf[1] = ((kBase64DecodeTable[buf[1]] << 4) & 0xf0) |
|
|
|
70 |
(kBase64DecodeTable[buf[2]] >> 2);
|
|
|
71 |
if (len > 3) {
|
|
|
72 |
buf[2] = ((kBase64DecodeTable[buf[2]] << 6) & 0xc0) |
|
|
|
73 |
(kBase64DecodeTable[buf[3]]);
|
|
|
74 |
}
|
|
|
75 |
}
|
|
|
76 |
}
|
|
|
77 |
|
|
|
78 |
|
|
|
79 |
}}} // apache::thrift::protocol
|