10 static unsigned char const PAD =
'=';
12 static unsigned char enc(
int c)
14 static const unsigned char table[] = {
15 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49, 0x4a, 0x4b, 0x4c, 0x4d, 0x4e, 0x4f, 0x50,
16 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58, 0x59, 0x5a, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
17 0x67, 0x68, 0x69, 0x6a, 0x6b, 0x6c, 0x6d, 0x6e, 0x6f, 0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76,
18 0x77, 0x78, 0x79, 0x7a, 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x2b, 0x2f,
23 static unsigned char dec(
int c)
25 static const unsigned char table[] = {
26 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
27 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
28 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x3e, 0xff, 0xff, 0xff, 0x3f,
29 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x3a, 0x3b, 0x3c, 0x3d, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
30 0xff, 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e,
31 0x0f, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0xff, 0xff, 0xff, 0xff, 0xff,
32 0xff, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28,
33 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f, 0x30, 0x31, 0x32, 0x33, 0xff, 0xff, 0xff, 0xff, 0xff,
35 return table[c & 127];
38 static void encode(
char const *src,
size_t length, std::vector<char> *out)
40 size_t srcpos, dstlen, dstpos;
42 dstlen = (length + 2) / 3 * 4;
47 char *dst = &out->at(0);
49 for (srcpos = 0; srcpos < length; srcpos += 3) {
50 int v = (
unsigned char)src[srcpos] << 16;
51 if (srcpos + 1 < length) {
52 v |= (
unsigned char)src[srcpos + 1] << 8;
53 if (srcpos + 2 < length) {
54 v |= (
unsigned char)src[srcpos + 2];
55 dst[dstpos + 3] =
enc(v);
57 dst[dstpos + 3] =
PAD;
59 dst[dstpos + 2] =
enc(v >> 6);
61 dst[dstpos + 2] =
PAD;
62 dst[dstpos + 3] =
PAD;
64 dst[dstpos + 1] =
enc(v >> 12);
65 dst[dstpos] =
enc(v >> 18);
70 static void decode(
char const *src,
size_t length, std::vector<char> *out)
72 unsigned char const *begin = (
unsigned char const *)src;
73 unsigned char const *end = begin + length;
74 unsigned char const *ptr = begin;
76 out->reserve(length * 3 / 4);
83 unsigned char c = 0xff;
84 if (ptr < end && *ptr < 0x80) {
88 bits = (bits << 6) | c;
92 bits <<= (4 - count) * 6;
96 if (count == 4 || c == 0xff) {
98 out->push_back(bits >> 16);
100 out->push_back(bits >> 8);
102 out->push_back(bits);
117 static std::string
_to_s_(std::vector<char>
const *vec)
119 if (!vec || vec->empty())
return std::string();
120 return std::string((
char const *)&(*vec)[0], vec->size());
124 static inline void base64_encode(
char const *src,
size_t length, std::vector<char> *out)
129 static inline void base64_decode(
char const *src,
size_t length, std::vector<char> *out)
134 static inline void base64_encode(std::vector<char>
const *src, std::vector<char> *out)
139 static inline void base64_decode(std::vector<char>
const *src, std::vector<char> *out)
156 std::vector<char> vec;
163 std::vector<char> vec;
static void base64_decode(char const *src, size_t length, std::vector< char > *out)
Definition: base64.h:129
static void base64_encode(char const *src, size_t length, std::vector< char > *out)
Definition: base64.h:124
static std::string _to_s_(std::vector< char > const *vec)
Definition: base64.h:117
static unsigned char enc(int c)
Definition: base64.h:12
static void encode(char const *src, size_t length, std::vector< char > *out)
Definition: base64.h:38
static unsigned char dec(int c)
Definition: base64.h:23
static void decode(char const *src, size_t length, std::vector< char > *out)
Definition: base64.h:70
static unsigned char const PAD
Definition: base64.h:10