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