escaping.h 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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. // Base64Unescape()
  110. //
  111. // Converts a `src` string encoded in Base64 to its binary equivalent, writing
  112. // it to a `dest` buffer, returning `true` on success. If `src` contains invalid
  113. // characters, `dest` is cleared and returns `false`.
  114. bool Base64Unescape(absl::string_view src, std::string* dest);
  115. // WebSafeBase64Unescape()
  116. //
  117. // Converts a `src` string encoded in Base64 to its binary equivalent, writing
  118. // it to a `dest` buffer, but using '-' instead of '+', and '_' instead of '/'.
  119. // If `src` contains invalid characters, `dest` is cleared and returns `false`.
  120. bool WebSafeBase64Unescape(absl::string_view src, std::string* dest);
  121. // Base64Escape()
  122. //
  123. // Encodes a `src` string into a base64-encoded string, with padding characters.
  124. // This function conforms with RFC 4648 section 4 (base64).
  125. void Base64Escape(absl::string_view src, std::string* dest);
  126. std::string Base64Escape(absl::string_view src);
  127. // WebSafeBase64Escape()
  128. //
  129. // Encodes a `src` string into a base64-like string, using '-' instead of '+'
  130. // and '_' instead of '/', and without padding. This function conforms with RFC
  131. // 4648 section 5 (base64url).
  132. void WebSafeBase64Escape(absl::string_view src, std::string* dest);
  133. std::string WebSafeBase64Escape(absl::string_view src);
  134. // HexStringToBytes()
  135. //
  136. // Converts an ASCII hex string into bytes, returning binary data of length
  137. // `from.size()/2`.
  138. std::string HexStringToBytes(absl::string_view from);
  139. // BytesToHexString()
  140. //
  141. // Converts binary data into an ASCII text string, returning a string of size
  142. // `2*from.size()`.
  143. std::string BytesToHexString(absl::string_view from);
  144. ABSL_NAMESPACE_END
  145. } // namespace absl
  146. #endif // ABSL_STRINGS_ESCAPING_H_