1 // This is a grab-bag file for string utilities involved in escaping and 2 // unescaping strings in various ways. Who knew there were so many? 3 // 4 // There are more escaping functions in: 5 // webutil/html/tagutils.h (Escaping strings for HTML, PRE, JavaScript, etc.) 6 // webutil/url/url.h (Escaping for URL's, both RFC-2396 and other methods) 7 // template/template_modifiers.h (All sorts of stuff) 8 // util/regexp/re2/re2.h (Escaping for literals within regular expressions 9 // - see RE2::QuoteMeta). 10 // And probably many more places, as well. 11 12 #ifndef DYNAMIC_DEPTH_INTERNAL_STRINGS_ESCAPING_H_ // NOLINT 13 #define DYNAMIC_DEPTH_INTERNAL_STRINGS_ESCAPING_H_ // NOLINT 14 15 #include <stddef.h> // For ptrdiff_t. 16 17 #include <string> 18 19 #include "base/port.h" 20 21 namespace dynamic_depth { 22 23 // ---------------------------------------------------------------------- 24 // Base64Unescape() 25 // Converts "src" which is encoded in Base64 to its binary equivalent and 26 // writes it to "dest". If src contains invalid characters, dest is cleared 27 // and the function returns false. Returns true on success. 28 // ---------------------------------------------------------------------- 29 bool Base64Unescape(const string& src, string* dest); 30 31 // ---------------------------------------------------------------------- 32 // WebSafeBase64Unescape() 33 // This is a variation of Base64Unescape which uses '-' instead of '+', and 34 // '_' instead of '/'. src is not null terminated, instead specify len. I 35 // recommend that slen<szdest, but we honor szdest anyway. 36 // RETURNS the length of dest, or -1 if there's an error. 37 38 // The variation that stores into a string clears the string first, and 39 // returns false (with dest empty) if src contains invalid chars; for 40 // this version src and dest must be different strings. 41 // ---------------------------------------------------------------------- 42 bool WebSafeBase64Unescape(const string& src, string* dest); 43 44 // ---------------------------------------------------------------------- 45 // Base64Escape() 46 // Encode "src" to "dest" using base64 encoding. 47 // src is not null terminated, instead specify len. 48 // 'dest' should have at least CalculateBase64EscapedLen() length. 49 // RETURNS the length of dest. 50 // It also has an extra parameter "do_padding", 51 // which when set to false will prevent padding with "=". 52 // ---------------------------------------------------------------------- 53 void Base64Escape(const unsigned char* src, ptrdiff_t szsrc, string* dest, 54 bool do_padding); 55 56 // ---------------------------------------------------------------------- 57 // b2a_hex() 58 // Description: Binary-to-Ascii hex conversion. This converts 59 // 'num' bytes of binary to a 2*'num'-character hexadecimal representation 60 // Return value: 2*'num' characters of ascii string 61 // ---------------------------------------------------------------------- 62 string b2a_hex(const char* from, ptrdiff_t num); 63 64 } // namespace dynamic_depth 65 66 #endif // DYNAMIC_DEPTH_INTERNAL_STRINGS_ESCAPING_H_ // NOLINT 67