David Reiss | db0ea15 | 2008-02-18 01:49:37 +0000 | [diff] [blame] | 1 | // Copyright (c) 2006- Facebook |
| 2 | // Distributed under the Thrift Software License |
| 3 | // |
| 4 | // See accompanying file LICENSE or visit the Thrift site at: |
| 5 | // http://developers.facebook.com/thrift/ |
| 6 | |
| 7 | #include "TBase64Utils.h" |
| 8 | |
| 9 | #include <boost/static_assert.hpp> |
| 10 | |
| 11 | using std::string; |
| 12 | |
| 13 | namespace facebook { namespace thrift { namespace protocol { |
| 14 | |
| 15 | |
| 16 | static const uint8_t *kBase64EncodeTable = (const uint8_t *) |
| 17 | "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; |
| 18 | |
| 19 | void base64_encode(const uint8_t *in, uint32_t len, uint8_t *buf) { |
| 20 | buf[0] = kBase64EncodeTable[(in[0] >> 2) & 0x3F]; |
| 21 | if (len == 3) { |
| 22 | buf[1] = kBase64EncodeTable[((in[0] << 4) + (in[1] >> 4)) & 0x3f]; |
| 23 | buf[2] = kBase64EncodeTable[((in[1] << 2) + (in[2] >> 6)) & 0x3f]; |
| 24 | buf[3] = kBase64EncodeTable[in[2] & 0x3f]; |
| 25 | } else if (len == 2) { |
| 26 | buf[1] = kBase64EncodeTable[((in[0] << 4) + (in[1] >> 4)) & 0x3f]; |
| 27 | buf[2] = kBase64EncodeTable[(in[1] << 2) & 0x3f]; |
| 28 | } else { // len == 1 |
| 29 | buf[1] = kBase64EncodeTable[(in[0] << 4) & 0x3f]; |
| 30 | } |
| 31 | } |
| 32 | |
David Reiss | 1a35464 | 2008-02-28 21:11:34 +0000 | [diff] [blame] | 33 | static const uint8_t kBase64DecodeTable[256] ={ |
David Reiss | db0ea15 | 2008-02-18 01:49:37 +0000 | [diff] [blame] | 34 | -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, |
| 35 | -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, |
| 36 | -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,62,-1,-1,-1,63, |
| 37 | 52,53,54,55,56,57,58,59,60,61,-1,-1,-1,-1,-1,-1, |
| 38 | -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,10,11,12,13,14, |
| 39 | 15,16,17,18,19,20,21,22,23,24,25,-1,-1,-1,-1,-1, |
| 40 | -1,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40, |
David Reiss | 1a35464 | 2008-02-28 21:11:34 +0000 | [diff] [blame] | 41 | 41,42,43,44,45,46,47,48,49,50,51,-1,-1,-1,-1,-1, |
| 42 | -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, |
| 43 | -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, |
| 44 | -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, |
| 45 | -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, |
| 46 | -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, |
| 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,-1,-1,-1,-1,-1, |
David Reiss | db0ea15 | 2008-02-18 01:49:37 +0000 | [diff] [blame] | 50 | }; |
| 51 | |
| 52 | void base64_decode(uint8_t *buf, uint32_t len) { |
| 53 | buf[0] = (kBase64DecodeTable[buf[0]] << 2) | |
| 54 | (kBase64DecodeTable[buf[1]] >> 4); |
| 55 | if (len > 2) { |
| 56 | buf[1] = ((kBase64DecodeTable[buf[1]] << 4) & 0xf0) | |
| 57 | (kBase64DecodeTable[buf[2]] >> 2); |
| 58 | if (len > 3) { |
| 59 | buf[2] = ((kBase64DecodeTable[buf[2]] << 6) & 0xc0) | |
| 60 | (kBase64DecodeTable[buf[3]]); |
| 61 | } |
| 62 | } |
| 63 | } |
| 64 | |
| 65 | |
| 66 | }}} // facebook::thrift::protocol |