Guitar
base64.h
Go to the documentation of this file.
1 
2 #ifndef BASE64_H
3 #define BASE64_H
4 
5 #include <vector>
6 #include <string>
7 
8 class Base64 {
9 private:
10  static unsigned char const PAD = '=';
11 
12  static unsigned char enc(int c)
13  {
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,
19  };
20  return table[c & 63];
21  }
22 
23  static unsigned char dec(int c)
24  {
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,
34  };
35  return table[c & 127];
36  }
37 public:
38  static void encode(char const *src, size_t length, std::vector<char> *out)
39  {
40  size_t srcpos, dstlen, dstpos;
41 
42  dstlen = (length + 2) / 3 * 4;
43  out->resize(dstlen);
44  if (dstlen == 0) {
45  return;
46  }
47  char *dst = &out->at(0);
48  dstpos = 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);
56  } else {
57  dst[dstpos + 3] = PAD;
58  }
59  dst[dstpos + 2] = enc(v >> 6);
60  } else {
61  dst[dstpos + 2] = PAD;
62  dst[dstpos + 3] = PAD;
63  }
64  dst[dstpos + 1] = enc(v >> 12);
65  dst[dstpos] = enc(v >> 18);
66  dstpos += 4;
67  }
68  }
69 
70  static void decode(char const *src, size_t length, std::vector<char> *out)
71  {
72  unsigned char const *begin = (unsigned char const *)src;
73  unsigned char const *end = begin + length;
74  unsigned char const *ptr = begin;
75  out->clear();
76  out->reserve(length * 3 / 4);
77  int count = 0;
78  int bits = 0;
79  while (1) {
80  if (isspace(*ptr)) {
81  ptr++;
82  } else {
83  unsigned char c = 0xff;
84  if (ptr < end && *ptr < 0x80) {
85  c = dec(*ptr);
86  }
87  if (c < 0x40) {
88  bits = (bits << 6) | c;
89  count++;
90  } else {
91  if (count < 4) {
92  bits <<= (4 - count) * 6;
93  }
94  c = 0xff;
95  }
96  if (count == 4 || c == 0xff) {
97  if (count >= 2) {
98  out->push_back(bits >> 16);
99  if (count >= 3) {
100  out->push_back(bits >> 8);
101  if (count == 4) {
102  out->push_back(bits);
103  }
104  }
105  }
106  count = 0;
107  bits = 0;
108  if (c == 0xff) {
109  break;
110  }
111  }
112  ptr++;
113  }
114  }
115  }
116 
117  static std::string _to_s_(std::vector<char> const *vec)
118  {
119  if (!vec || vec->empty()) return std::string();
120  return std::string((char const *)&(*vec)[0], vec->size());
121  }
122 };
123 
124 static inline void base64_encode(char const *src, size_t length, std::vector<char> *out)
125 {
126  Base64::encode(src, length, out);
127 }
128 
129 static inline void base64_decode(char const *src, size_t length, std::vector<char> *out)
130 {
131  Base64::decode(src, length, out);
132 }
133 
134 static inline void base64_encode(std::vector<char> const *src, std::vector<char> *out)
135 {
136  Base64::encode(&src->at(0), src->size(), out);
137 }
138 
139 static inline void base64_decode(std::vector<char> const *src, std::vector<char> *out)
140 {
141  Base64::decode(&src->at(0), src->size(), out);
142 }
143 
144 static inline void base64_encode(char const *src, std::vector<char> *out)
145 {
146  Base64::encode((char const *)src, strlen(src), out);
147 }
148 
149 static inline void base64_decode(char const *src, std::vector<char> *out)
150 {
151  Base64::decode((char const *)src, strlen(src), out);
152 }
153 
154 static inline std::string base64_encode(std::string const &src)
155 {
156  std::vector<char> vec;
157  base64_encode((char const *)src.c_str(), src.size(), &vec);
158  return Base64::_to_s_(&vec);
159 }
160 
161 static inline std::string base64_decode(std::string const &src)
162 {
163  std::vector<char> vec;
164  base64_decode((char const *)src.c_str(), src.size(), &vec);
165  return Base64::_to_s_(&vec);
166 }
167 
168 #endif
169 
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
Definition: base64.h:8
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