• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 
2 //*********************************************************************
3 //* Base64 - a simple base64 encoder and decoder.
4 //*
5 //*     Copyright (c) 1999, Bob Withers - bwit@pobox.com
6 //*
7 //* This code may be freely used for any purpose, either personal
8 //* or commercial, provided the authors copyright notice remains
9 //* intact.
10 //*
11 //* Enhancements by Stanley Yamane:
12 //*     o reverse lookup table for the decode function
13 //*     o reserve string buffer space in advance
14 //*
15 //*********************************************************************
16 
17 #include "rtc_base/third_party/base64/base64.h"
18 
19 #include <assert.h>
20 #include <string.h>
21 
22 using std::vector;
23 
24 namespace rtc {
25 
26 static const char kPad = '=';
27 static const unsigned char pd = 0xFD;  // Padding
28 static const unsigned char sp = 0xFE;  // Whitespace
29 static const unsigned char il = 0xFF;  // Illegal base64 character
30 
31 const char Base64::Base64Table[] =
32     // 0000000000111111111122222222223333333333444444444455555555556666
33     // 0123456789012345678901234567890123456789012345678901234567890123
34     "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
35 
36 // Decode Table gives the index of any valid base64 character in the
37 // Base64 table
38 // 65 == A, 97 == a, 48 == 0, 43 == +, 47 == /
39 
40 const unsigned char Base64::DecodeTable[] = {
41     // 0  1  2  3  4  5  6  7  8  9
42     il, il, il, il, il, il, il, il, il, sp,  //   0 -   9
43     sp, sp, sp, sp, il, il, il, il, il, il,  //  10 -  19
44     il, il, il, il, il, il, il, il, il, il,  //  20 -  29
45     il, il, sp, il, il, il, il, il, il, il,  //  30 -  39
46     il, il, il, 62, il, il, il, 63, 52, 53,  //  40 -  49
47     54, 55, 56, 57, 58, 59, 60, 61, il, il,  //  50 -  59
48     il, pd, il, il, il, 0,  1,  2,  3,  4,   //  60 -  69
49     5,  6,  7,  8,  9,  10, 11, 12, 13, 14,  //  70 -  79
50     15, 16, 17, 18, 19, 20, 21, 22, 23, 24,  //  80 -  89
51     25, il, il, il, il, il, il, 26, 27, 28,  //  90 -  99
52     29, 30, 31, 32, 33, 34, 35, 36, 37, 38,  // 100 - 109
53     39, 40, 41, 42, 43, 44, 45, 46, 47, 48,  // 110 - 119
54     49, 50, 51, il, il, il, il, il, il, il,  // 120 - 129
55     il, il, il, il, il, il, il, il, il, il,  // 130 - 139
56     il, il, il, il, il, il, il, il, il, il,  // 140 - 149
57     il, il, il, il, il, il, il, il, il, il,  // 150 - 159
58     il, il, il, il, il, il, il, il, il, il,  // 160 - 169
59     il, il, il, il, il, il, il, il, il, il,  // 170 - 179
60     il, il, il, il, il, il, il, il, il, il,  // 180 - 189
61     il, il, il, il, il, il, il, il, il, il,  // 190 - 199
62     il, il, il, il, il, il, il, il, il, il,  // 200 - 209
63     il, il, il, il, il, il, il, il, il, il,  // 210 - 219
64     il, il, il, il, il, il, il, il, il, il,  // 220 - 229
65     il, il, il, il, il, il, il, il, il, il,  // 230 - 239
66     il, il, il, il, il, il, il, il, il, il,  // 240 - 249
67     il, il, il, il, il, il                   // 250 - 255
68 };
69 
IsBase64Char(char ch)70 bool Base64::IsBase64Char(char ch) {
71   return (('A' <= ch) && (ch <= 'Z')) || (('a' <= ch) && (ch <= 'z')) ||
72          (('0' <= ch) && (ch <= '9')) || (ch == '+') || (ch == '/');
73 }
74 
GetNextBase64Char(char ch,char * next_ch)75 bool Base64::GetNextBase64Char(char ch, char* next_ch) {
76   if (next_ch == nullptr) {
77     return false;
78   }
79   const char* p = strchr(Base64Table, ch);
80   if (!p)
81     return false;
82   ++p;
83   *next_ch = (*p) ? *p : Base64Table[0];
84   return true;
85 }
86 
IsBase64Encoded(const std::string & str)87 bool Base64::IsBase64Encoded(const std::string& str) {
88   for (size_t i = 0; i < str.size(); ++i) {
89     if (!IsBase64Char(str.at(i)))
90       return false;
91   }
92   return true;
93 }
94 
EncodeFromArray(const void * data,size_t len,std::string * result)95 void Base64::EncodeFromArray(const void* data,
96                              size_t len,
97                              std::string* result) {
98   assert(nullptr != result);
99   result->clear();
100   result->resize(((len + 2) / 3) * 4);
101   const unsigned char* byte_data = static_cast<const unsigned char*>(data);
102 
103   unsigned char c;
104   size_t i = 0;
105   size_t dest_ix = 0;
106   while (i < len) {
107     c = (byte_data[i] >> 2) & 0x3f;
108     (*result)[dest_ix++] = Base64Table[c];
109 
110     c = (byte_data[i] << 4) & 0x3f;
111     if (++i < len) {
112       c |= (byte_data[i] >> 4) & 0x0f;
113     }
114     (*result)[dest_ix++] = Base64Table[c];
115 
116     if (i < len) {
117       c = (byte_data[i] << 2) & 0x3f;
118       if (++i < len) {
119         c |= (byte_data[i] >> 6) & 0x03;
120       }
121       (*result)[dest_ix++] = Base64Table[c];
122     } else {
123       (*result)[dest_ix++] = kPad;
124     }
125 
126     if (i < len) {
127       c = byte_data[i] & 0x3f;
128       (*result)[dest_ix++] = Base64Table[c];
129       ++i;
130     } else {
131       (*result)[dest_ix++] = kPad;
132     }
133   }
134 }
135 
GetNextQuantum(DecodeFlags parse_flags,bool illegal_pads,const char * data,size_t len,size_t * dpos,unsigned char qbuf[4],bool * padded)136 size_t Base64::GetNextQuantum(DecodeFlags parse_flags,
137                               bool illegal_pads,
138                               const char* data,
139                               size_t len,
140                               size_t* dpos,
141                               unsigned char qbuf[4],
142                               bool* padded) {
143   size_t byte_len = 0, pad_len = 0, pad_start = 0;
144   for (; (byte_len < 4) && (*dpos < len); ++*dpos) {
145     qbuf[byte_len] = DecodeTable[static_cast<unsigned char>(data[*dpos])];
146     if ((il == qbuf[byte_len]) || (illegal_pads && (pd == qbuf[byte_len]))) {
147       if (parse_flags != DO_PARSE_ANY)
148         break;
149       // Ignore illegal characters
150     } else if (sp == qbuf[byte_len]) {
151       if (parse_flags == DO_PARSE_STRICT)
152         break;
153       // Ignore spaces
154     } else if (pd == qbuf[byte_len]) {
155       if (byte_len < 2) {
156         if (parse_flags != DO_PARSE_ANY)
157           break;
158         // Ignore unexpected padding
159       } else if (byte_len + pad_len >= 4) {
160         if (parse_flags != DO_PARSE_ANY)
161           break;
162         // Ignore extra pads
163       } else {
164         if (1 == ++pad_len) {
165           pad_start = *dpos;
166         }
167       }
168     } else {
169       if (pad_len > 0) {
170         if (parse_flags != DO_PARSE_ANY)
171           break;
172         // Ignore pads which are followed by data
173         pad_len = 0;
174       }
175       ++byte_len;
176     }
177   }
178   for (size_t i = byte_len; i < 4; ++i) {
179     qbuf[i] = 0;
180   }
181   if (4 == byte_len + pad_len) {
182     *padded = true;
183   } else {
184     *padded = false;
185     if (pad_len) {
186       // Roll back illegal padding
187       *dpos = pad_start;
188     }
189   }
190   return byte_len;
191 }
192 
DecodeFromArray(const char * data,size_t len,DecodeFlags flags,std::string * result,size_t * data_used)193 bool Base64::DecodeFromArray(const char* data,
194                              size_t len,
195                              DecodeFlags flags,
196                              std::string* result,
197                              size_t* data_used) {
198   return DecodeFromArrayTemplate<std::string>(data, len, flags, result,
199                                               data_used);
200 }
201 
DecodeFromArray(const char * data,size_t len,DecodeFlags flags,vector<char> * result,size_t * data_used)202 bool Base64::DecodeFromArray(const char* data,
203                              size_t len,
204                              DecodeFlags flags,
205                              vector<char>* result,
206                              size_t* data_used) {
207   return DecodeFromArrayTemplate<vector<char>>(data, len, flags, result,
208                                                data_used);
209 }
210 
DecodeFromArray(const char * data,size_t len,DecodeFlags flags,vector<uint8_t> * result,size_t * data_used)211 bool Base64::DecodeFromArray(const char* data,
212                              size_t len,
213                              DecodeFlags flags,
214                              vector<uint8_t>* result,
215                              size_t* data_used) {
216   return DecodeFromArrayTemplate<vector<uint8_t>>(data, len, flags, result,
217                                                   data_used);
218 }
219 
220 template <typename T>
DecodeFromArrayTemplate(const char * data,size_t len,DecodeFlags flags,T * result,size_t * data_used)221 bool Base64::DecodeFromArrayTemplate(const char* data,
222                                      size_t len,
223                                      DecodeFlags flags,
224                                      T* result,
225                                      size_t* data_used) {
226   assert(nullptr != result);
227   assert(flags <= (DO_PARSE_MASK | DO_PAD_MASK | DO_TERM_MASK));
228 
229   const DecodeFlags parse_flags = flags & DO_PARSE_MASK;
230   const DecodeFlags pad_flags = flags & DO_PAD_MASK;
231   const DecodeFlags term_flags = flags & DO_TERM_MASK;
232   assert(0 != parse_flags);
233   assert(0 != pad_flags);
234   assert(0 != term_flags);
235 
236   result->clear();
237   result->reserve(len);
238 
239   size_t dpos = 0;
240   bool success = true, padded;
241   unsigned char c, qbuf[4];
242   while (dpos < len) {
243     size_t qlen = GetNextQuantum(parse_flags, (DO_PAD_NO == pad_flags), data,
244                                  len, &dpos, qbuf, &padded);
245     c = (qbuf[0] << 2) | ((qbuf[1] >> 4) & 0x3);
246     if (qlen >= 2) {
247       result->push_back(c);
248       c = ((qbuf[1] << 4) & 0xf0) | ((qbuf[2] >> 2) & 0xf);
249       if (qlen >= 3) {
250         result->push_back(c);
251         c = ((qbuf[2] << 6) & 0xc0) | qbuf[3];
252         if (qlen >= 4) {
253           result->push_back(c);
254           c = 0;
255         }
256       }
257     }
258     if (qlen < 4) {
259       if ((DO_TERM_ANY != term_flags) && (0 != c)) {
260         success = false;  // unused bits
261       }
262       if ((DO_PAD_YES == pad_flags) && !padded) {
263         success = false;  // expected padding
264       }
265       break;
266     }
267   }
268   if ((DO_TERM_BUFFER == term_flags) && (dpos != len)) {
269     success = false;  // unused chars
270   }
271   if (data_used) {
272     *data_used = dpos;
273   }
274   return success;
275 }
276 
277 }  // namespace rtc
278