escaping.h 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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/macros.h"
  28. #include "absl/strings/ascii.h"
  29. #include "absl/strings/str_join.h"
  30. #include "absl/strings/string_view.h"
  31. namespace absl {
  32. ABSL_NAMESPACE_BEGIN
  33. // CUnescape()
  34. //
  35. // Unescapes a `source` string and copies it into `dest`, rewriting C-style
  36. // escape sequences (https://en.cppreference.com/w/cpp/language/escape) into
  37. // their proper code point equivalents, returning `true` if successful.
  38. //
  39. // The following unescape sequences can be handled:
  40. //
  41. // * ASCII escape sequences ('\n','\r','\\', etc.) to their ASCII equivalents
  42. // * Octal escape sequences ('\nnn') to byte nnn. The unescaped value must
  43. // resolve to a single byte or an error will occur. E.g. values greater than
  44. // 0xff will produce an error.
  45. // * Hexadecimal escape sequences ('\xnn') to byte nn. While an arbitrary
  46. // number of following digits are allowed, the unescaped value must resolve
  47. // to a single byte or an error will occur. E.g. '\x0045' is equivalent to
  48. // '\x45', but '\x1234' will produce an error.
  49. // * Unicode escape sequences ('\unnnn' for exactly four hex digits or
  50. // '\Unnnnnnnn' for exactly eight hex digits, which will be encoded in
  51. // UTF-8. (E.g., `\u2019` unescapes to the three bytes 0xE2, 0x80, and
  52. // 0x99).
  53. //
  54. // If any errors are encountered, this function returns `false`, leaving the
  55. // `dest` output parameter in an unspecified state, and stores the first
  56. // encountered error in `error`. To disable error reporting, set `error` to
  57. // `nullptr` or use the overload with no error reporting below.
  58. //
  59. // Example:
  60. //
  61. // std::string s = "foo\\rbar\\nbaz\\t";
  62. // std::string unescaped_s;
  63. // if (!absl::CUnescape(s, &unescaped_s) {
  64. // ...
  65. // }
  66. // EXPECT_EQ(unescaped_s, "foo\rbar\nbaz\t");
  67. bool CUnescape(absl::string_view source, std::string* dest, std::string* error);
  68. // Overload of `CUnescape()` with no error reporting.
  69. inline bool CUnescape(absl::string_view source, std::string* dest) {
  70. return CUnescape(source, dest, nullptr);
  71. }
  72. // CEscape()
  73. //
  74. // Escapes a 'src' string using C-style escapes sequences
  75. // (https://en.cppreference.com/w/cpp/language/escape), escaping other
  76. // non-printable/non-whitespace bytes as octal sequences (e.g. "\377").
  77. //
  78. // Example:
  79. //
  80. // std::string s = "foo\rbar\tbaz\010\011\012\013\014\x0d\n";
  81. // std::string escaped_s = absl::CEscape(s);
  82. // EXPECT_EQ(escaped_s, "foo\\rbar\\tbaz\\010\\t\\n\\013\\014\\r\\n");
  83. std::string CEscape(absl::string_view src);
  84. // CHexEscape()
  85. //
  86. // Escapes a 'src' string using C-style escape sequences, escaping
  87. // other non-printable/non-whitespace bytes as hexadecimal sequences (e.g.
  88. // "\xFF").
  89. //
  90. // Example:
  91. //
  92. // std::string s = "foo\rbar\tbaz\010\011\012\013\014\x0d\n";
  93. // std::string escaped_s = absl::CHexEscape(s);
  94. // EXPECT_EQ(escaped_s, "foo\\rbar\\tbaz\\x08\\t\\n\\x0b\\x0c\\r\\n");
  95. std::string CHexEscape(absl::string_view src);
  96. // Utf8SafeCEscape()
  97. //
  98. // Escapes a 'src' string using C-style escape sequences, escaping bytes as
  99. // octal sequences, and passing through UTF-8 characters without conversion.
  100. // I.e., when encountering any bytes with their high bit set, this function
  101. // will not escape those values, whether or not they are valid UTF-8.
  102. std::string Utf8SafeCEscape(absl::string_view src);
  103. // Utf8SafeCHexEscape()
  104. //
  105. // Escapes a 'src' string using C-style escape sequences, escaping bytes as
  106. // hexadecimal sequences, and passing through UTF-8 characters without
  107. // conversion.
  108. std::string Utf8SafeCHexEscape(absl::string_view src);
  109. // Base64Escape()
  110. //
  111. // Encodes a `src` string into a base64-encoded 'dest' string with padding
  112. // characters. This function conforms with RFC 4648 section 4 (base64) and RFC
  113. // 2045.
  114. void Base64Escape(absl::string_view src, std::string* dest);
  115. std::string Base64Escape(absl::string_view src);
  116. // WebSafeBase64Escape()
  117. //
  118. // Encodes a `src` string into a base64 string, like Base64Escape() does, but
  119. // outputs '-' instead of '+' and '_' instead of '/', and does not pad 'dest'.
  120. // This function conforms with RFC 4648 section 5 (base64url).
  121. void WebSafeBase64Escape(absl::string_view src, std::string* dest);
  122. std::string WebSafeBase64Escape(absl::string_view src);
  123. // Base64Unescape()
  124. //
  125. // Converts a `src` string encoded in Base64 (RFC 4648 section 4) to its binary
  126. // equivalent, writing it to a `dest` buffer, returning `true` on success. If
  127. // `src` contains invalid characters, `dest` is cleared and returns `false`.
  128. // If padding is included (note that `Base64Escape()` does produce it), it must
  129. // be correct. In the padding, '=' and '.' are treated identically.
  130. bool Base64Unescape(absl::string_view src, std::string* dest);
  131. // WebSafeBase64Unescape()
  132. //
  133. // Converts a `src` string encoded in "web safe" Base64 (RFC 4648 section 5) to
  134. // its binary equivalent, writing it to a `dest` buffer. If `src` contains
  135. // invalid characters, `dest` is cleared and returns `false`. If padding is
  136. // included (note that `WebSafeBase64Escape()` does not produce it), it must be
  137. // correct. In the padding, '=' and '.' are treated identically.
  138. bool WebSafeBase64Unescape(absl::string_view src, std::string* dest);
  139. // HexStringToBytes()
  140. //
  141. // Converts an ASCII hex string into bytes, returning binary data of length
  142. // `from.size()/2`.
  143. std::string HexStringToBytes(absl::string_view from);
  144. // BytesToHexString()
  145. //
  146. // Converts binary data into an ASCII text string, returning a string of size
  147. // `2*from.size()`.
  148. std::string BytesToHexString(absl::string_view from);
  149. ABSL_NAMESPACE_END
  150. } // namespace absl
  151. #endif // ABSL_STRINGS_ESCAPING_H_