blob: 12ebaa93696a677525ff331616907a11a14a2f14 [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] ={
Roger Meier1d282a92011-12-11 20:30:21 +0000470xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
480xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
490xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0x3e,0xff,0xff,0xff,0x3f,
500x34,0x35,0x36,0x37,0x38,0x39,0x3a,0x3b,0x3c,0x3d,0xff,0xff,0xff,0xff,0xff,0xff,
510xff,0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,0x0a,0x0b,0x0c,0x0d,0x0e,
520x0f,0x10,0x11,0x12,0x13,0x14,0x15,0x16,0x17,0x18,0x19,0xff,0xff,0xff,0xff,0xff,
530xff,0x1a,0x1b,0x1c,0x1d,0x1e,0x1f,0x20,0x21,0x22,0x23,0x24,0x25,0x26,0x27,0x28,
540x29,0x2a,0x2b,0x2c,0x2d,0x2e,0x2f,0x30,0x31,0x32,0x33,0xff,0xff,0xff,0xff,0xff,
550xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
560xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
570xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
580xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
590xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
600xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
610xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
620xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
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