escaping.h 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. //
  2. // Copyright 2017 The Abseil Authors.
  3. //
  4. // Licensed under the Apache License, Version 2.0 (the "License");
  5. // you may not use this file except in compliance with the License.
  6. // You may obtain a copy of the License at
  7. //
  8. // https://www.apache.org/licenses/LICENSE-2.0
  9. //
  10. // Unless required by applicable law or agreed to in writing, software
  11. // distributed under the License is distributed on an "AS IS" BASIS,
  12. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. // See the License for the specific language governing permissions and
  14. // limitations under the License.
  15. //
  16. // -----------------------------------------------------------------------------
  17. // File: escaping.h
  18. // -----------------------------------------------------------------------------
  19. //
  20. // This header file contains string utilities involved in escaping and
  21. // unescaping strings in various ways.
  22. #ifndef ABSL_STRINGS_ESCAPING_H_
  23. #define ABSL_STRINGS_ESCAPING_H_
  24. #include <cstddef>
  25. #include <string>
  26. #include <vector>
  27. #include "absl/base/attributes.h"
  28. #include "absl/base/macros.h"
  29. #include "absl/base/nullability.h"
  30. #include "absl/strings/ascii.h"
  31. #include "absl/strings/str_join.h"
  32. #include "absl/strings/string_view.h"
  33. namespace absl {
  34. ABSL_NAMESPACE_BEGIN
  35. // CUnescape()
  36. //
  37. // Unescapes a `source` string and copies it into `dest`, rewriting C-style
  38. // escape sequences (https://en.cppreference.com/w/cpp/language/escape) into
  39. // their proper code point equivalents, returning `true` if successful.
  40. //
  41. // The following unescape sequences can be handled:
  42. //
  43. // * ASCII escape sequences ('\n','\r','\\', etc.) to their ASCII equivalents
  44. // * Octal escape sequences ('\nnn') to byte nnn. The unescaped value must
  45. // resolve to a single byte or an error will occur. E.g. values greater than
  46. // 0xff will produce an error.
  47. // * Hexadecimal escape sequences ('\xnn') to byte nn. While an arbitrary
  48. // number of following digits are allowed, the unescaped value must resolve
  49. // to a single byte or an error will occur. E.g. '\x0045' is equivalent to
  50. // '\x45', but '\x1234' will produce an error.
  51. // * Unicode escape sequences ('\unnnn' for exactly four hex digits or
  52. // '\Unnnnnnnn' for exactly eight hex digits, which will be encoded in
  53. // UTF-8. (E.g., `\u2019` unescapes to the three bytes 0xE2, 0x80, and
  54. // 0x99).
  55. //
  56. // If any errors are encountered, this function returns `false`, leaving the
  57. // `dest` output parameter in an unspecified state, and stores the first
  58. // encountered error in `error`. To disable error reporting, set `error` to
  59. // `nullptr` or use the overload with no error reporting below.
  60. //
  61. // Example:
  62. //
  63. // std::string s = "foo\\rbar\\nbaz\\t";
  64. // std::string unescaped_s;
  65. // if (!absl::CUnescape(s, &unescaped_s)) {
  66. // ...
  67. // }
  68. // EXPECT_EQ(unescaped_s, "foo\rbar\nbaz\t");
  69. bool CUnescape(absl::string_view source, absl::Nonnull<std::string*> dest,
  70. absl::Nullable<std::string*> error);
  71. // Overload of `CUnescape()` with no error reporting.
  72. inline bool CUnescape(absl::string_view source,
  73. absl::Nonnull<std::string*> dest) {
  74. return CUnescape(source, dest, nullptr);
  75. }
  76. // CEscape()
  77. //
  78. // Escapes a 'src' string using C-style escapes sequences
  79. // (https://en.cppreference.com/w/cpp/language/escape), escaping other
  80. // non-printable/non-whitespace bytes as octal sequences (e.g. "\377").
  81. //
  82. // Example:
  83. //
  84. // std::string s = "foo\rbar\tbaz\010\011\012\013\014\x0d\n";
  85. // std::string escaped_s = absl::CEscape(s);
  86. // EXPECT_EQ(escaped_s, "foo\\rbar\\tbaz\\010\\t\\n\\013\\014\\r\\n");
  87. std::string CEscape(absl::string_view src);
  88. // CHexEscape()
  89. //
  90. // Escapes a 'src' string using C-style escape sequences, escaping
  91. // other non-printable/non-whitespace bytes as hexadecimal sequences (e.g.
  92. // "\xFF").
  93. //
  94. // Example:
  95. //
  96. // std::string s = "foo\rbar\tbaz\010\011\012\013\014\x0d\n";
  97. // std::string escaped_s = absl::CHexEscape(s);
  98. // EXPECT_EQ(escaped_s, "foo\\rbar\\tbaz\\x08\\t\\n\\x0b\\x0c\\r\\n");
  99. std::string CHexEscape(absl::string_view src);
  100. // Utf8SafeCEscape()
  101. //
  102. // Escapes a 'src' string using C-style escape sequences, escaping bytes as
  103. // octal sequences, and passing through UTF-8 characters without conversion.
  104. // I.e., when encountering any bytes with their high bit set, this function
  105. // will not escape those values, whether or not they are valid UTF-8.
  106. std::string Utf8SafeCEscape(absl::string_view src);
  107. // Utf8SafeCHexEscape()
  108. //
  109. // Escapes a 'src' string using C-style escape sequences, escaping bytes as
  110. // hexadecimal sequences, and passing through UTF-8 characters without
  111. // conversion.
  112. std::string Utf8SafeCHexEscape(absl::string_view src);
  113. // Base64Escape()
  114. //
  115. // Encodes a `src` string into a base64-encoded 'dest' string with padding
  116. // characters. This function conforms with RFC 4648 section 4 (base64) and RFC
  117. // 2045.
  118. void Base64Escape(absl::string_view src, absl::Nonnull<std::string*> dest);
  119. std::string Base64Escape(absl::string_view src);
  120. // WebSafeBase64Escape()
  121. //
  122. // Encodes a `src` string into a base64 string, like Base64Escape() does, but
  123. // outputs '-' instead of '+' and '_' instead of '/', and does not pad 'dest'.
  124. // This function conforms with RFC 4648 section 5 (base64url).
  125. void WebSafeBase64Escape(absl::string_view src,
  126. absl::Nonnull<std::string*> dest);
  127. std::string WebSafeBase64Escape(absl::string_view src);
  128. // Base64Unescape()
  129. //
  130. // Converts a `src` string encoded in Base64 (RFC 4648 section 4) to its binary
  131. // equivalent, writing it to a `dest` buffer, returning `true` on success. If
  132. // `src` contains invalid characters, `dest` is cleared and returns `false`.
  133. // If padding is included (note that `Base64Escape()` does produce it), it must
  134. // be correct. In the padding, '=' and '.' are treated identically.
  135. bool Base64Unescape(absl::string_view src, absl::Nonnull<std::string*> dest);
  136. // WebSafeBase64Unescape()
  137. //
  138. // Converts a `src` string encoded in "web safe" Base64 (RFC 4648 section 5) to
  139. // its binary equivalent, writing it to a `dest` buffer. If `src` contains
  140. // invalid characters, `dest` is cleared and returns `false`. If padding is
  141. // included (note that `WebSafeBase64Escape()` does not produce it), it must be
  142. // correct. In the padding, '=' and '.' are treated identically.
  143. bool WebSafeBase64Unescape(absl::string_view src,
  144. absl::Nonnull<std::string*> dest);
  145. // HexStringToBytes()
  146. //
  147. // Converts the hexadecimal encoded data in `hex` into raw bytes in the `bytes`
  148. // output string. If `hex` does not consist of valid hexadecimal data, this
  149. // function returns false and leaves `bytes` in an unspecified state. Returns
  150. // true on success.
  151. ABSL_MUST_USE_RESULT bool HexStringToBytes(absl::string_view hex,
  152. absl::Nonnull<std::string*> bytes);
  153. // HexStringToBytes()
  154. //
  155. // Converts an ASCII hex string into bytes, returning binary data of length
  156. // `from.size()/2`. The input must be valid hexadecimal data, otherwise the
  157. // return value is unspecified.
  158. ABSL_DEPRECATED("Use the HexStringToBytes() that returns a bool")
  159. std::string HexStringToBytes(absl::string_view from);
  160. // BytesToHexString()
  161. //
  162. // Converts binary data into an ASCII text string, returning a string of size
  163. // `2*from.size()`.
  164. std::string BytesToHexString(absl::string_view from);
  165. ABSL_NAMESPACE_END
  166. } // namespace absl
  167. #endif // ABSL_STRINGS_ESCAPING_H_