blob: 14481c49c50792e8123fe98ee18b40b676c389d7 [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
20#include "TBase64Utils.h"
21
22#include <boost/static_assert.hpp>
23
24using std::string;
25
T Jake Lucianib5e62212009-01-31 22:36:20 +000026namespace apache { namespace thrift { namespace protocol {
David Reissdb0ea152008-02-18 01:49:37 +000027
28
29static const uint8_t *kBase64EncodeTable = (const uint8_t *)
30 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
31
32void 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
David Reiss1a354642008-02-28 21:11:34 +000046static const uint8_t kBase64DecodeTable[256] ={
David Reissdb0ea152008-02-18 01:49:37 +000047 -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,
David Reiss1a354642008-02-28 21:11:34 +000054 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,
David Reissdb0ea152008-02-18 01:49:37 +000063};
64
65void 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
T Jake Lucianib5e62212009-01-31 22:36:20 +000079}}} // apache::thrift::protocol