Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1043)

Side by Side Diff: webrtc/base/base64.cc

Issue 2574143002: Run 'git cl format --full' on Base64. (Closed)
Patch Set: Created 4 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « webrtc/base/base64.h ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 1
2 //********************************************************************* 2 //*********************************************************************
3 //* Base64 - a simple base64 encoder and decoder. 3 //* Base64 - a simple base64 encoder and decoder.
4 //* 4 //*
5 //* Copyright (c) 1999, Bob Withers - bwit@pobox.com 5 //* Copyright (c) 1999, Bob Withers - bwit@pobox.com
6 //* 6 //*
7 //* This code may be freely used for any purpose, either personal 7 //* This code may be freely used for any purpose, either personal
8 //* or commercial, provided the authors copyright notice remains 8 //* or commercial, provided the authors copyright notice remains
9 //* intact. 9 //* intact.
10 //* 10 //*
(...skipping 12 matching lines...) Expand all
23 using std::vector; 23 using std::vector;
24 24
25 namespace rtc { 25 namespace rtc {
26 26
27 static const char kPad = '='; 27 static const char kPad = '=';
28 static const unsigned char pd = 0xFD; // Padding 28 static const unsigned char pd = 0xFD; // Padding
29 static const unsigned char sp = 0xFE; // Whitespace 29 static const unsigned char sp = 0xFE; // Whitespace
30 static const unsigned char il = 0xFF; // Illegal base64 character 30 static const unsigned char il = 0xFF; // Illegal base64 character
31 31
32 const char Base64::Base64Table[] = 32 const char Base64::Base64Table[] =
33 // 0000000000111111111122222222223333333333444444444455555555556666 33 // 0000000000111111111122222222223333333333444444444455555555556666
34 // 0123456789012345678901234567890123456789012345678901234567890123 34 // 0123456789012345678901234567890123456789012345678901234567890123
35 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; 35 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
36 36
37 // Decode Table gives the index of any valid base64 character in the 37 // Decode Table gives the index of any valid base64 character in the
38 // Base64 table 38 // Base64 table
39 // 65 == A, 97 == a, 48 == 0, 43 == +, 47 == / 39 // 65 == A, 97 == a, 48 == 0, 43 == +, 47 == /
40 40
41 const unsigned char Base64::DecodeTable[] = { 41 const unsigned char Base64::DecodeTable[] = {
42 // 0 1 2 3 4 5 6 7 8 9 42 // 0 1 2 3 4 5 6 7 8 9
43 il,il,il,il,il,il,il,il,il,sp, // 0 - 9 43 il, il, il, il, il, il, il, il, il, sp, // 0 - 9
44 sp,sp,sp,sp,il,il,il,il,il,il, // 10 - 19 44 sp, sp, sp, sp, il, il, il, il, il, il, // 10 - 19
45 il,il,il,il,il,il,il,il,il,il, // 20 - 29 45 il, il, il, il, il, il, il, il, il, il, // 20 - 29
46 il,il,sp,il,il,il,il,il,il,il, // 30 - 39 46 il, il, sp, il, il, il, il, il, il, il, // 30 - 39
47 il,il,il,62,il,il,il,63,52,53, // 40 - 49 47 il, il, il, 62, il, il, il, 63, 52, 53, // 40 - 49
48 54,55,56,57,58,59,60,61,il,il, // 50 - 59 48 54, 55, 56, 57, 58, 59, 60, 61, il, il, // 50 - 59
49 il,pd,il,il,il, 0, 1, 2, 3, 4, // 60 - 69 49 il, pd, il, il, il, 0, 1, 2, 3, 4, // 60 - 69
50 5, 6, 7, 8, 9,10,11,12,13,14, // 70 - 79 50 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, // 70 - 79
51 15,16,17,18,19,20,21,22,23,24, // 80 - 89 51 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, // 80 - 89
52 25,il,il,il,il,il,il,26,27,28, // 90 - 99 52 25, il, il, il, il, il, il, 26, 27, 28, // 90 - 99
53 29,30,31,32,33,34,35,36,37,38, // 100 - 109 53 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, // 100 - 109
54 39,40,41,42,43,44,45,46,47,48, // 110 - 119 54 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, // 110 - 119
55 49,50,51,il,il,il,il,il,il,il, // 120 - 129 55 49, 50, 51, il, il, il, il, il, il, il, // 120 - 129
56 il,il,il,il,il,il,il,il,il,il, // 130 - 139 56 il, il, il, il, il, il, il, il, il, il, // 130 - 139
57 il,il,il,il,il,il,il,il,il,il, // 140 - 149 57 il, il, il, il, il, il, il, il, il, il, // 140 - 149
58 il,il,il,il,il,il,il,il,il,il, // 150 - 159 58 il, il, il, il, il, il, il, il, il, il, // 150 - 159
59 il,il,il,il,il,il,il,il,il,il, // 160 - 169 59 il, il, il, il, il, il, il, il, il, il, // 160 - 169
60 il,il,il,il,il,il,il,il,il,il, // 170 - 179 60 il, il, il, il, il, il, il, il, il, il, // 170 - 179
61 il,il,il,il,il,il,il,il,il,il, // 180 - 189 61 il, il, il, il, il, il, il, il, il, il, // 180 - 189
62 il,il,il,il,il,il,il,il,il,il, // 190 - 199 62 il, il, il, il, il, il, il, il, il, il, // 190 - 199
63 il,il,il,il,il,il,il,il,il,il, // 200 - 209 63 il, il, il, il, il, il, il, il, il, il, // 200 - 209
64 il,il,il,il,il,il,il,il,il,il, // 210 - 219 64 il, il, il, il, il, il, il, il, il, il, // 210 - 219
65 il,il,il,il,il,il,il,il,il,il, // 220 - 229 65 il, il, il, il, il, il, il, il, il, il, // 220 - 229
66 il,il,il,il,il,il,il,il,il,il, // 230 - 239 66 il, il, il, il, il, il, il, il, il, il, // 230 - 239
67 il,il,il,il,il,il,il,il,il,il, // 240 - 249 67 il, il, il, il, il, il, il, il, il, il, // 240 - 249
68 il,il,il,il,il,il // 250 - 255 68 il, il, il, il, il, il // 250 - 255
69 }; 69 };
70 70
71 bool Base64::IsBase64Char(char ch) { 71 bool Base64::IsBase64Char(char ch) {
72 return (('A' <= ch) && (ch <= 'Z')) || 72 return (('A' <= ch) && (ch <= 'Z')) || (('a' <= ch) && (ch <= 'z')) ||
73 (('a' <= ch) && (ch <= 'z')) || 73 (('0' <= ch) && (ch <= '9')) || (ch == '+') || (ch == '/');
74 (('0' <= ch) && (ch <= '9')) ||
75 (ch == '+') || (ch == '/');
76 } 74 }
77 75
78 bool Base64::GetNextBase64Char(char ch, char* next_ch) { 76 bool Base64::GetNextBase64Char(char ch, char* next_ch) {
79 if (next_ch == NULL) { 77 if (next_ch == NULL) {
80 return false; 78 return false;
81 } 79 }
82 const char* p = strchr(Base64Table, ch); 80 const char* p = strchr(Base64Table, ch);
83 if (!p) 81 if (!p)
84 return false; 82 return false;
85 ++p; 83 ++p;
86 *next_ch = (*p) ? *p : Base64Table[0]; 84 *next_ch = (*p) ? *p : Base64Table[0];
87 return true; 85 return true;
88 } 86 }
89 87
90 bool Base64::IsBase64Encoded(const std::string& str) { 88 bool Base64::IsBase64Encoded(const std::string& str) {
91 for (size_t i = 0; i < str.size(); ++i) { 89 for (size_t i = 0; i < str.size(); ++i) {
92 if (!IsBase64Char(str.at(i))) 90 if (!IsBase64Char(str.at(i)))
93 return false; 91 return false;
94 } 92 }
95 return true; 93 return true;
96 } 94 }
97 95
98 void Base64::EncodeFromArray(const void* data, size_t len, 96 void Base64::EncodeFromArray(const void* data,
97 size_t len,
99 std::string* result) { 98 std::string* result) {
100 RTC_DCHECK(NULL != result); 99 RTC_DCHECK(NULL != result);
101 result->clear(); 100 result->clear();
102 result->resize(((len + 2) / 3) * 4); 101 result->resize(((len + 2) / 3) * 4);
103 const unsigned char* byte_data = static_cast<const unsigned char*>(data); 102 const unsigned char* byte_data = static_cast<const unsigned char*>(data);
104 103
105 unsigned char c; 104 unsigned char c;
106 size_t i = 0; 105 size_t i = 0;
107 size_t dest_ix = 0; 106 size_t dest_ix = 0;
108 while (i < len) { 107 while (i < len) {
(...skipping 19 matching lines...) Expand all
128 if (i < len) { 127 if (i < len) {
129 c = byte_data[i] & 0x3f; 128 c = byte_data[i] & 0x3f;
130 (*result)[dest_ix++] = Base64Table[c]; 129 (*result)[dest_ix++] = Base64Table[c];
131 ++i; 130 ++i;
132 } else { 131 } else {
133 (*result)[dest_ix++] = kPad; 132 (*result)[dest_ix++] = kPad;
134 } 133 }
135 } 134 }
136 } 135 }
137 136
138 size_t Base64::GetNextQuantum(DecodeFlags parse_flags, bool illegal_pads, 137 size_t Base64::GetNextQuantum(DecodeFlags parse_flags,
139 const char* data, size_t len, size_t* dpos, 138 bool illegal_pads,
140 unsigned char qbuf[4], bool* padded) 139 const char* data,
141 { 140 size_t len,
141 size_t* dpos,
142 unsigned char qbuf[4],
143 bool* padded) {
142 size_t byte_len = 0, pad_len = 0, pad_start = 0; 144 size_t byte_len = 0, pad_len = 0, pad_start = 0;
143 for (; (byte_len < 4) && (*dpos < len); ++*dpos) { 145 for (; (byte_len < 4) && (*dpos < len); ++*dpos) {
144 qbuf[byte_len] = DecodeTable[static_cast<unsigned char>(data[*dpos])]; 146 qbuf[byte_len] = DecodeTable[static_cast<unsigned char>(data[*dpos])];
145 if ((il == qbuf[byte_len]) || (illegal_pads && (pd == qbuf[byte_len]))) { 147 if ((il == qbuf[byte_len]) || (illegal_pads && (pd == qbuf[byte_len]))) {
146 if (parse_flags != DO_PARSE_ANY) 148 if (parse_flags != DO_PARSE_ANY)
147 break; 149 break;
148 // Ignore illegal characters 150 // Ignore illegal characters
149 } else if (sp == qbuf[byte_len]) { 151 } else if (sp == qbuf[byte_len]) {
150 if (parse_flags == DO_PARSE_STRICT) 152 if (parse_flags == DO_PARSE_STRICT)
151 break; 153 break;
(...skipping 30 matching lines...) Expand all
182 } else { 184 } else {
183 *padded = false; 185 *padded = false;
184 if (pad_len) { 186 if (pad_len) {
185 // Roll back illegal padding 187 // Roll back illegal padding
186 *dpos = pad_start; 188 *dpos = pad_start;
187 } 189 }
188 } 190 }
189 return byte_len; 191 return byte_len;
190 } 192 }
191 193
192 bool Base64::DecodeFromArray(const char* data, size_t len, DecodeFlags flags, 194 bool Base64::DecodeFromArray(const char* data,
193 std::string* result, size_t* data_used) { 195 size_t len,
194 return DecodeFromArrayTemplate<std::string>( 196 DecodeFlags flags,
195 data, len, flags, result, data_used); 197 std::string* result,
198 size_t* data_used) {
199 return DecodeFromArrayTemplate<std::string>(data, len, flags, result,
200 data_used);
196 } 201 }
197 202
198 bool Base64::DecodeFromArray(const char* data, size_t len, DecodeFlags flags, 203 bool Base64::DecodeFromArray(const char* data,
199 vector<char>* result, size_t* data_used) { 204 size_t len,
205 DecodeFlags flags,
206 vector<char>* result,
207 size_t* data_used) {
200 return DecodeFromArrayTemplate<vector<char>>(data, len, flags, result, 208 return DecodeFromArrayTemplate<vector<char>>(data, len, flags, result,
201 data_used); 209 data_used);
202 } 210 }
203 211
204 bool Base64::DecodeFromArray(const char* data, 212 bool Base64::DecodeFromArray(const char* data,
205 size_t len, 213 size_t len,
206 DecodeFlags flags, 214 DecodeFlags flags,
207 vector<uint8_t>* result, 215 vector<uint8_t>* result,
208 size_t* data_used) { 216 size_t* data_used) {
209 return DecodeFromArrayTemplate<vector<uint8_t>>(data, len, flags, result, 217 return DecodeFromArrayTemplate<vector<uint8_t>>(data, len, flags, result,
210 data_used); 218 data_used);
211 } 219 }
212 220
213 template<typename T> 221 template <typename T>
214 bool Base64::DecodeFromArrayTemplate(const char* data, size_t len, 222 bool Base64::DecodeFromArrayTemplate(const char* data,
215 DecodeFlags flags, T* result, 223 size_t len,
216 size_t* data_used) 224 DecodeFlags flags,
217 { 225 T* result,
226 size_t* data_used) {
218 RTC_DCHECK(NULL != result); 227 RTC_DCHECK(NULL != result);
219 RTC_DCHECK(flags <= (DO_PARSE_MASK | DO_PAD_MASK | DO_TERM_MASK)); 228 RTC_DCHECK(flags <= (DO_PARSE_MASK | DO_PAD_MASK | DO_TERM_MASK));
220 229
221 const DecodeFlags parse_flags = flags & DO_PARSE_MASK; 230 const DecodeFlags parse_flags = flags & DO_PARSE_MASK;
222 const DecodeFlags pad_flags = flags & DO_PAD_MASK; 231 const DecodeFlags pad_flags = flags & DO_PAD_MASK;
223 const DecodeFlags term_flags = flags & DO_TERM_MASK; 232 const DecodeFlags term_flags = flags & DO_TERM_MASK;
224 RTC_DCHECK(0 != parse_flags); 233 RTC_DCHECK(0 != parse_flags);
225 RTC_DCHECK(0 != pad_flags); 234 RTC_DCHECK(0 != pad_flags);
226 RTC_DCHECK(0 != term_flags); 235 RTC_DCHECK(0 != term_flags);
227 236
228 result->clear(); 237 result->clear();
229 result->reserve(len); 238 result->reserve(len);
230 239
231 size_t dpos = 0; 240 size_t dpos = 0;
232 bool success = true, padded; 241 bool success = true, padded;
233 unsigned char c, qbuf[4]; 242 unsigned char c, qbuf[4];
234 while (dpos < len) { 243 while (dpos < len) {
235 size_t qlen = GetNextQuantum(parse_flags, (DO_PAD_NO == pad_flags), 244 size_t qlen = GetNextQuantum(parse_flags, (DO_PAD_NO == pad_flags), data,
236 data, len, &dpos, qbuf, &padded); 245 len, &dpos, qbuf, &padded);
237 c = (qbuf[0] << 2) | ((qbuf[1] >> 4) & 0x3); 246 c = (qbuf[0] << 2) | ((qbuf[1] >> 4) & 0x3);
238 if (qlen >= 2) { 247 if (qlen >= 2) {
239 result->push_back(c); 248 result->push_back(c);
240 c = ((qbuf[1] << 4) & 0xf0) | ((qbuf[2] >> 2) & 0xf); 249 c = ((qbuf[1] << 4) & 0xf0) | ((qbuf[2] >> 2) & 0xf);
241 if (qlen >= 3) { 250 if (qlen >= 3) {
242 result->push_back(c); 251 result->push_back(c);
243 c = ((qbuf[2] << 6) & 0xc0) | qbuf[3]; 252 c = ((qbuf[2] << 6) & 0xc0) | qbuf[3];
244 if (qlen >= 4) { 253 if (qlen >= 4) {
245 result->push_back(c); 254 result->push_back(c);
246 c = 0; 255 c = 0;
(...skipping 12 matching lines...) Expand all
259 } 268 }
260 if ((DO_TERM_BUFFER == term_flags) && (dpos != len)) { 269 if ((DO_TERM_BUFFER == term_flags) && (dpos != len)) {
261 success = false; // unused chars 270 success = false; // unused chars
262 } 271 }
263 if (data_used) { 272 if (data_used) {
264 *data_used = dpos; 273 *data_used = dpos;
265 } 274 }
266 return success; 275 return success;
267 } 276 }
268 277
269 } // namespace rtc 278 } // namespace rtc
OLDNEW
« no previous file with comments | « webrtc/base/base64.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698