You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

223 lines
9.3 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. /*
  2. * libjingle
  3. * Copyright 2004, Google Inc.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice,
  9. * this list of conditions and the following disclaimer.
  10. * 2. Redistributions in binary form must reproduce the above copyright notice,
  11. * this list of conditions and the following disclaimer in the documentation
  12. * and/or other materials provided with the distribution.
  13. * 3. The name of the author may not be used to endorse or promote products
  14. * derived from this software without specific prior written permission.
  15. *
  16. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
  17. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  18. * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
  19. * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  20. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  21. * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
  22. * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
  23. * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
  24. * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  25. * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. */
  27. #ifndef TALK_BASE_STRINGENCODE_H_
  28. #define TALK_BASE_STRINGENCODE_H_
  29. #include <string>
  30. #include <sstream>
  31. #include <vector>
  32. namespace talk_base {
  33. //////////////////////////////////////////////////////////////////////
  34. // String Encoding Utilities
  35. //////////////////////////////////////////////////////////////////////
  36. // Convert an unsigned value to it's utf8 representation. Returns the length
  37. // of the encoded string, or 0 if the encoding is longer than buflen - 1.
  38. size_t utf8_encode(char* buffer, size_t buflen, unsigned long value);
  39. // Decode the utf8 encoded value pointed to by source. Returns the number of
  40. // bytes used by the encoding, or 0 if the encoding is invalid.
  41. size_t utf8_decode(const char* source, size_t srclen, unsigned long* value);
  42. // Escaping prefixes illegal characters with the escape character. Compact, but
  43. // illegal characters still appear in the string.
  44. size_t escape(char * buffer, size_t buflen,
  45. const char * source, size_t srclen,
  46. const char * illegal, char escape);
  47. // Note: in-place unescaping (buffer == source) is allowed.
  48. size_t unescape(char * buffer, size_t buflen,
  49. const char * source, size_t srclen,
  50. char escape);
  51. // Encoding replaces illegal characters with the escape character and 2 hex
  52. // chars, so it's a little less compact than escape, but completely removes
  53. // illegal characters. note that hex digits should not be used as illegal
  54. // characters.
  55. size_t encode(char * buffer, size_t buflen,
  56. const char * source, size_t srclen,
  57. const char * illegal, char escape);
  58. // Note: in-place decoding (buffer == source) is allowed.
  59. size_t decode(char * buffer, size_t buflen,
  60. const char * source, size_t srclen,
  61. char escape);
  62. // Returns a list of characters that may be unsafe for use in the name of a
  63. // file, suitable for passing to the 'illegal' member of escape or encode.
  64. const char* unsafe_filename_characters();
  65. // url_encode is an encode operation with a predefined set of illegal characters
  66. // and escape character (for use in URLs, obviously).
  67. size_t url_encode(char * buffer, size_t buflen,
  68. const char * source, size_t srclen);
  69. // Note: in-place decoding (buffer == source) is allowed.
  70. size_t url_decode(char * buffer, size_t buflen,
  71. const char * source, size_t srclen);
  72. // html_encode prevents data embedded in html from containing markup.
  73. size_t html_encode(char * buffer, size_t buflen,
  74. const char * source, size_t srclen);
  75. // Note: in-place decoding (buffer == source) is allowed.
  76. size_t html_decode(char * buffer, size_t buflen,
  77. const char * source, size_t srclen);
  78. // xml_encode makes data suitable for inside xml attributes and values.
  79. size_t xml_encode(char * buffer, size_t buflen,
  80. const char * source, size_t srclen);
  81. // Note: in-place decoding (buffer == source) is allowed.
  82. size_t xml_decode(char * buffer, size_t buflen,
  83. const char * source, size_t srclen);
  84. // Convert an unsigned value from 0 to 15 to the hex character equivalent...
  85. char hex_encode(unsigned char val);
  86. // ...and vice-versa.
  87. bool hex_decode(char ch, unsigned char* val);
  88. // hex_encode shows the hex representation of binary data in ascii.
  89. size_t hex_encode(char* buffer, size_t buflen,
  90. const char* source, size_t srclen);
  91. // hex_encode, but separate each byte representation with a delimiter.
  92. // |delimiter| == 0 means no delimiter
  93. // If the buffer is too short, we return 0
  94. size_t hex_encode_with_delimiter(char* buffer, size_t buflen,
  95. const char* source, size_t srclen,
  96. char delimiter);
  97. // Helper functions for hex_encode.
  98. std::string hex_encode(const char* source, size_t srclen);
  99. std::string hex_encode_with_delimiter(const char* source, size_t srclen,
  100. char delimiter);
  101. // hex_decode converts ascii hex to binary.
  102. size_t hex_decode(char* buffer, size_t buflen,
  103. const char* source, size_t srclen);
  104. // hex_decode, assuming that there is a delimiter between every byte
  105. // pair.
  106. // |delimiter| == 0 means no delimiter
  107. // If the buffer is too short or the data is invalid, we return 0.
  108. size_t hex_decode_with_delimiter(char* buffer, size_t buflen,
  109. const char* source, size_t srclen,
  110. char delimiter);
  111. // Helper functions for hex_decode.
  112. size_t hex_decode(char* buffer, size_t buflen, const std::string& source);
  113. size_t hex_decode_with_delimiter(char* buffer, size_t buflen,
  114. const std::string& source, char delimiter);
  115. // Apply any suitable string transform (including the ones above) to an STL
  116. // string. Stack-allocated temporary space is used for the transformation,
  117. // so value and source may refer to the same string.
  118. typedef size_t (*Transform)(char * buffer, size_t buflen,
  119. const char * source, size_t srclen);
  120. size_t transform(std::string& value, size_t maxlen, const std::string& source,
  121. Transform t);
  122. // Return the result of applying transform t to source.
  123. std::string s_transform(const std::string& source, Transform t);
  124. // Convenience wrappers.
  125. inline std::string s_url_encode(const std::string& source) {
  126. return s_transform(source, url_encode);
  127. }
  128. inline std::string s_url_decode(const std::string& source) {
  129. return s_transform(source, url_decode);
  130. }
  131. // Splits the source string into multiple fields separated by delimiter,
  132. // with duplicates of delimiter creating empty fields.
  133. size_t split(const std::string& source, char delimiter,
  134. std::vector<std::string>* fields);
  135. // Splits the source string into multiple fields separated by delimiter,
  136. // with duplicates of delimiter ignored. Trailing delimiter ignored.
  137. size_t tokenize(const std::string& source, char delimiter,
  138. std::vector<std::string>* fields);
  139. // Tokenize and append the tokens to fields. Return the new size of fields.
  140. size_t tokenize_append(const std::string& source, char delimiter,
  141. std::vector<std::string>* fields);
  142. // Splits the source string into multiple fields separated by delimiter, with
  143. // duplicates of delimiter ignored. Trailing delimiter ignored. A substring in
  144. // between the start_mark and the end_mark is treated as a single field. Return
  145. // the size of fields. For example, if source is "filename
  146. // \"/Library/Application Support/media content.txt\"", delimiter is ' ', and
  147. // the start_mark and end_mark are '"', this method returns two fields:
  148. // "filename" and "/Library/Application Support/media content.txt".
  149. size_t tokenize(const std::string& source, char delimiter, char start_mark,
  150. char end_mark, std::vector<std::string>* fields);
  151. // Safe sprintf to std::string
  152. //void sprintf(std::string& value, size_t maxlen, const char * format, ...)
  153. // PRINTF_FORMAT(3);
  154. // Convert arbitrary values to/from a string.
  155. template <class T>
  156. static bool ToString(const T &t, std::string* s) {
  157. std::ostringstream oss;
  158. oss << t;
  159. *s = oss.str();
  160. return !oss.fail();
  161. }
  162. template <class T>
  163. static bool FromString(const std::string& s, T* t) {
  164. std::istringstream iss(s);
  165. iss >> *t;
  166. return !iss.fail();
  167. }
  168. // Inline versions of the string conversion routines.
  169. template<typename T>
  170. static inline std::string ToString(T val) {
  171. std::string str; ToString(val, &str); return str;
  172. }
  173. template<typename T>
  174. static inline T FromString(const std::string& str) {
  175. T val; FromString(str, &val); return val;
  176. }
  177. template<typename T>
  178. static inline T FromString(const T& defaultValue, const std::string& str) {
  179. T val(defaultValue); FromString(str, &val); return val;
  180. }
  181. // simple function to strip out characters which shouldn't be
  182. // used in filenames
  183. char make_char_safe_for_filename(char c);
  184. //////////////////////////////////////////////////////////////////////
  185. } // namespace talk_base
  186. #endif // TALK_BASE_STRINGENCODE_H__