| OLD | NEW |
| 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 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 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')) || (('a' <= ch) && (ch <= 'z')) || | 72 return (('A' <= ch) && (ch <= 'Z')) || (('a' <= ch) && (ch <= 'z')) || |
| 73 (('0' <= ch) && (ch <= '9')) || (ch == '+') || (ch == '/'); | 73 (('0' <= ch) && (ch <= '9')) || (ch == '+') || (ch == '/'); |
| 74 } | 74 } |
| 75 | 75 |
| 76 bool Base64::GetNextBase64Char(char ch, char* next_ch) { | 76 bool Base64::GetNextBase64Char(char ch, char* next_ch) { |
| 77 if (next_ch == NULL) { | 77 if (next_ch == nullptr) { |
| 78 return false; | 78 return false; |
| 79 } | 79 } |
| 80 const char* p = strchr(Base64Table, ch); | 80 const char* p = strchr(Base64Table, ch); |
| 81 if (!p) | 81 if (!p) |
| 82 return false; | 82 return false; |
| 83 ++p; | 83 ++p; |
| 84 *next_ch = (*p) ? *p : Base64Table[0]; | 84 *next_ch = (*p) ? *p : Base64Table[0]; |
| 85 return true; | 85 return true; |
| 86 } | 86 } |
| 87 | 87 |
| 88 bool Base64::IsBase64Encoded(const std::string& str) { | 88 bool Base64::IsBase64Encoded(const std::string& str) { |
| 89 for (size_t i = 0; i < str.size(); ++i) { | 89 for (size_t i = 0; i < str.size(); ++i) { |
| 90 if (!IsBase64Char(str.at(i))) | 90 if (!IsBase64Char(str.at(i))) |
| 91 return false; | 91 return false; |
| 92 } | 92 } |
| 93 return true; | 93 return true; |
| 94 } | 94 } |
| 95 | 95 |
| 96 void Base64::EncodeFromArray(const void* data, | 96 void Base64::EncodeFromArray(const void* data, |
| 97 size_t len, | 97 size_t len, |
| 98 std::string* result) { | 98 std::string* result) { |
| 99 RTC_DCHECK(NULL != result); | 99 RTC_DCHECK(nullptr != result); |
| 100 result->clear(); | 100 result->clear(); |
| 101 result->resize(((len + 2) / 3) * 4); | 101 result->resize(((len + 2) / 3) * 4); |
| 102 const unsigned char* byte_data = static_cast<const unsigned char*>(data); | 102 const unsigned char* byte_data = static_cast<const unsigned char*>(data); |
| 103 | 103 |
| 104 unsigned char c; | 104 unsigned char c; |
| 105 size_t i = 0; | 105 size_t i = 0; |
| 106 size_t dest_ix = 0; | 106 size_t dest_ix = 0; |
| 107 while (i < len) { | 107 while (i < len) { |
| 108 c = (byte_data[i] >> 2) & 0x3f; | 108 c = (byte_data[i] >> 2) & 0x3f; |
| 109 (*result)[dest_ix++] = Base64Table[c]; | 109 (*result)[dest_ix++] = Base64Table[c]; |
| (...skipping 107 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 217 return DecodeFromArrayTemplate<vector<uint8_t>>(data, len, flags, result, | 217 return DecodeFromArrayTemplate<vector<uint8_t>>(data, len, flags, result, |
| 218 data_used); | 218 data_used); |
| 219 } | 219 } |
| 220 | 220 |
| 221 template <typename T> | 221 template <typename T> |
| 222 bool Base64::DecodeFromArrayTemplate(const char* data, | 222 bool Base64::DecodeFromArrayTemplate(const char* data, |
| 223 size_t len, | 223 size_t len, |
| 224 DecodeFlags flags, | 224 DecodeFlags flags, |
| 225 T* result, | 225 T* result, |
| 226 size_t* data_used) { | 226 size_t* data_used) { |
| 227 RTC_DCHECK(NULL != result); | 227 RTC_DCHECK(nullptr != result); |
| 228 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)); |
| 229 | 229 |
| 230 const DecodeFlags parse_flags = flags & DO_PARSE_MASK; | 230 const DecodeFlags parse_flags = flags & DO_PARSE_MASK; |
| 231 const DecodeFlags pad_flags = flags & DO_PAD_MASK; | 231 const DecodeFlags pad_flags = flags & DO_PAD_MASK; |
| 232 const DecodeFlags term_flags = flags & DO_TERM_MASK; | 232 const DecodeFlags term_flags = flags & DO_TERM_MASK; |
| 233 RTC_DCHECK(0 != parse_flags); | 233 RTC_DCHECK(0 != parse_flags); |
| 234 RTC_DCHECK(0 != pad_flags); | 234 RTC_DCHECK(0 != pad_flags); |
| 235 RTC_DCHECK(0 != term_flags); | 235 RTC_DCHECK(0 != term_flags); |
| 236 | 236 |
| 237 result->clear(); | 237 result->clear(); |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 269 if ((DO_TERM_BUFFER == term_flags) && (dpos != len)) { | 269 if ((DO_TERM_BUFFER == term_flags) && (dpos != len)) { |
| 270 success = false; // unused chars | 270 success = false; // unused chars |
| 271 } | 271 } |
| 272 if (data_used) { | 272 if (data_used) { |
| 273 *data_used = dpos; | 273 *data_used = dpos; |
| 274 } | 274 } |
| 275 return success; | 275 return success; |
| 276 } | 276 } |
| 277 | 277 |
| 278 } // namespace rtc | 278 } // namespace rtc |
| OLD | NEW |