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 #include <cctype>
8 #include <cstring>
9 #include <limits>
10 
11 class Base64 {
12 private:
13  static unsigned char const PAD = '=';
14 
15  static unsigned char enc(int c)
16  {
17  static const unsigned char table[] = {
18  0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49, 0x4a, 0x4b, 0x4c, 0x4d, 0x4e, 0x4f, 0x50,
19  0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58, 0x59, 0x5a, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
20  0x67, 0x68, 0x69, 0x6a, 0x6b, 0x6c, 0x6d, 0x6e, 0x6f, 0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76,
21  0x77, 0x78, 0x79, 0x7a, 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x2b, 0x2f,
22  };
23  return table[c & 63];
24  }
25 
26  static unsigned char dec(int c)
27  {
28  static const unsigned char table[] = {
29  0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
30  0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
31  0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x3e, 0xff, 0xff, 0xff, 0x3f,
32  0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x3a, 0x3b, 0x3c, 0x3d, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
33  0xff, 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e,
34  0x0f, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0xff, 0xff, 0xff, 0xff, 0xff,
35  0xff, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28,
36  0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f, 0x30, 0x31, 0x32, 0x33, 0xff, 0xff, 0xff, 0xff, 0xff,
37  };
38  return table[c & 127];
39  }
40 public:
41  static bool decode_checked(char const *src, size_t length, std::vector<char> *out)
42  {
43  if (!out) return false;
44  out->clear();
45  if (!src && length != 0) return false;
46 
47  unsigned char quartet[4];
48  size_t count = 0;
49  bool finished = false;
50  for (size_t i = 0; i < length; ++i) {
51  unsigned char ch = static_cast<unsigned char>(src[i]);
52  if (std::isspace(ch)) continue;
53  if (finished) return false;
54  if (ch == PAD) {
55  quartet[count++] = 0x40;
56  } else {
57  if (ch >= 0x80) return false;
58  unsigned char value = dec(ch);
59  if (value >= 0x40) return false;
60  quartet[count++] = value;
61  }
62  if (count != 4) continue;
63 
64  if (quartet[0] >= 0x40 || quartet[1] >= 0x40) return false;
65  if (quartet[2] == 0x40 && quartet[3] != 0x40) return false;
66  out->push_back(static_cast<char>((quartet[0] << 2) | (quartet[1] >> 4)));
67  if (quartet[2] != 0x40) {
68  out->push_back(static_cast<char>((quartet[1] << 4) | (quartet[2] >> 2)));
69  if (quartet[3] != 0x40) {
70  out->push_back(static_cast<char>((quartet[2] << 6) | quartet[3]));
71  }
72  }
73  finished = quartet[2] == 0x40 || quartet[3] == 0x40;
74  count = 0;
75  }
76  if (count != 0) {
77  out->clear();
78  return false;
79  }
80  return true;
81  }
82 
83  static void encode(char const *src, size_t length, std::vector<char> *out)
84  {
85  if (!out) return;
86  out->clear();
87  if (!src && length != 0) return;
88  if (length > std::numeric_limits<size_t>::max() / 4 * 3) return;
89  size_t srcpos = 0;
90  size_t dstlen = length / 3 * 4;
91  if (length % 3 != 0) dstlen += 4;
92  out->resize(dstlen);
93  if (dstlen == 0) {
94  return;
95  }
96  char *dst = &out->at(0);
97  size_t dstpos = 0;
98  for (srcpos = 0; srcpos < length; srcpos += 3) {
99  int v = static_cast<unsigned char>(src[srcpos]) << 16;
100  if (srcpos + 1 < length) {
101  v |= static_cast<unsigned char>(src[srcpos + 1]) << 8;
102  if (srcpos + 2 < length) {
103  v |= static_cast<unsigned char>(src[srcpos + 2]);
104  dst[dstpos + 3] = enc(v);
105  } else {
106  dst[dstpos + 3] = PAD;
107  }
108  dst[dstpos + 2] = enc(v >> 6);
109  } else {
110  dst[dstpos + 2] = PAD;
111  dst[dstpos + 3] = PAD;
112  }
113  dst[dstpos + 1] = enc(v >> 12);
114  dst[dstpos] = enc(v >> 18);
115  dstpos += 4;
116  }
117  }
118 
119  static void decode(char const *src, size_t length, std::vector<char> *out)
120  {
121  (void)decode_checked(src, length, out);
122  }
123 
124  static std::string _to_s_(std::vector<char> const *vec)
125  {
126  if (!vec || vec->empty()) return std::string();
127  return std::string((char const *)&(*vec)[0], vec->size());
128  }
129 };
130 
131 static inline void base64_encode(char const *src, size_t length, std::vector<char> *out)
132 {
133  Base64::encode(src, length, out);
134 }
135 
136 static inline void base64_decode(char const *src, size_t length, std::vector<char> *out)
137 {
138  Base64::decode(src, length, out);
139 }
140 
141 static inline void base64_encode(std::vector<char> const *src, std::vector<char> *out)
142 {
143  if (!src) {
144  out->clear();
145  return;
146  }
147  Base64::encode(src->data(), src->size(), out);
148 }
149 
150 static inline void base64_decode(std::vector<char> const *src, std::vector<char> *out)
151 {
152  if (!src) {
153  out->clear();
154  return;
155  }
156  Base64::decode(src->data(), src->size(), out);
157 }
158 
159 static inline void base64_encode(char const *src, std::vector<char> *out)
160 {
161  Base64::encode((char const *)src, strlen(src), out);
162 }
163 
164 static inline void base64_decode(char const *src, std::vector<char> *out)
165 {
166  Base64::decode((char const *)src, strlen(src), out);
167 }
168 
169 static inline std::string base64_encode(std::string_view const &src)
170 {
171  std::vector<char> vec;
172  base64_encode((char const *)src.data(), src.size(), &vec);
173  return Base64::_to_s_(&vec);
174 }
175 
176 static inline std::string base64_decode(std::string_view const &src)
177 {
178  std::vector<char> vec;
179  base64_decode((char const *)src.data(), src.size(), &vec);
180  return Base64::_to_s_(&vec);
181 }
182 
183 #endif
184 
Definition: base64.h:11
static std::string _to_s_(std::vector< char > const *vec)
Definition: base64.h:124
static unsigned char enc(int c)
Definition: base64.h:15
static unsigned char const PAD
Definition: base64.h:13
static void encode(char const *src, size_t length, std::vector< char > *out)
Definition: base64.h:83
static unsigned char dec(int c)
Definition: base64.h:26
static bool decode_checked(char const *src, size_t length, std::vector< char > *out)
Definition: base64.h:41
static void decode(char const *src, size_t length, std::vector< char > *out)
Definition: base64.h:119
static void base64_decode(char const *src, size_t length, std::vector< char > *out)
Definition: base64.h:136
static void base64_encode(char const *src, size_t length, std::vector< char > *out)
Definition: base64.h:131