1 2 //********************************************************************* 3 //* C_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 12 #ifndef RTC_BASE_THIRD_PARTY_BASE64_BASE64_H_ 13 #define RTC_BASE_THIRD_PARTY_BASE64_BASE64_H_ 14 15 #include <string> 16 #include <vector> 17 18 #include "absl/strings/string_view.h" 19 #include "rtc_base/system/rtc_export.h" 20 21 namespace rtc { 22 23 class Base64 { 24 public: 25 enum DecodeOption { 26 DO_PARSE_STRICT = 1, // Parse only base64 characters 27 DO_PARSE_WHITE = 2, // Parse only base64 and whitespace characters 28 DO_PARSE_ANY = 3, // Parse all characters 29 DO_PARSE_MASK = 3, 30 31 DO_PAD_YES = 4, // Padding is required 32 DO_PAD_ANY = 8, // Padding is optional 33 DO_PAD_NO = 12, // Padding is disallowed 34 DO_PAD_MASK = 12, 35 36 DO_TERM_BUFFER = 16, // Must termiante at end of buffer 37 DO_TERM_CHAR = 32, // May terminate at any character boundary 38 DO_TERM_ANY = 48, // May terminate at a sub-character bit offset 39 DO_TERM_MASK = 48, 40 41 // Strictest interpretation 42 DO_STRICT = DO_PARSE_STRICT | DO_PAD_YES | DO_TERM_BUFFER, 43 44 DO_LAX = DO_PARSE_ANY | DO_PAD_ANY | DO_TERM_CHAR, 45 }; 46 typedef int DecodeFlags; 47 48 static bool IsBase64Char(char ch); 49 50 // Get the char next to the `ch` from the Base64Table. 51 // If the `ch` is the last one in the Base64Table then returns 52 // the first one from the table. 53 // Expects the `ch` be a base64 char. 54 // The result will be saved in `next_ch`. 55 // Returns true on success. 56 static bool GetNextBase64Char(char ch, char* next_ch); 57 58 // Determines whether the given string consists entirely of valid base64 59 // encoded characters. 60 static bool IsBase64Encoded(absl::string_view str); 61 62 RTC_EXPORT static void EncodeFromArray(const void* data, 63 size_t len, 64 std::string* result); 65 RTC_EXPORT static bool DecodeFromArray(const char* data, 66 size_t len, 67 DecodeFlags flags, 68 std::string* result, 69 size_t* data_used); 70 static bool DecodeFromArray(const char* data, 71 size_t len, 72 DecodeFlags flags, 73 std::vector<char>* result, 74 size_t* data_used); 75 static bool DecodeFromArray(const char* data, 76 size_t len, 77 DecodeFlags flags, 78 std::vector<uint8_t>* result, 79 size_t* data_used); 80 81 // Convenience Methods Encode(absl::string_view data)82 static inline std::string Encode(absl::string_view data) { 83 std::string result; 84 EncodeFromArray(data.data(), data.size(), &result); 85 return result; 86 } Decode(absl::string_view data,DecodeFlags flags)87 static inline std::string Decode(absl::string_view data, DecodeFlags flags) { 88 std::string result; 89 DecodeFromArray(data.data(), data.size(), flags, &result, nullptr); 90 return result; 91 } Decode(absl::string_view data,DecodeFlags flags,std::string * result,size_t * data_used)92 static inline bool Decode(absl::string_view data, 93 DecodeFlags flags, 94 std::string* result, 95 size_t* data_used) { 96 return DecodeFromArray(data.data(), data.size(), flags, result, data_used); 97 } Decode(absl::string_view data,DecodeFlags flags,std::vector<char> * result,size_t * data_used)98 static inline bool Decode(absl::string_view data, 99 DecodeFlags flags, 100 std::vector<char>* result, 101 size_t* data_used) { 102 return DecodeFromArray(data.data(), data.size(), flags, result, data_used); 103 } 104 105 private: 106 static const char Base64Table[]; 107 static const unsigned char DecodeTable[]; 108 109 static size_t GetNextQuantum(DecodeFlags parse_flags, 110 bool illegal_pads, 111 const char* data, 112 size_t len, 113 size_t* dpos, 114 unsigned char qbuf[4], 115 bool* padded); 116 template <typename T> 117 static bool DecodeFromArrayTemplate(const char* data, 118 size_t len, 119 DecodeFlags flags, 120 T* result, 121 size_t* data_used); 122 }; 123 124 } // namespace rtc 125 126 #endif /* RTC_BASE_THIRD_PARTY_BASE64_BASE64_H_ */ 127