Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-07-15 08:27:07

0001 //
0002 // Copyright 2017 The Abseil Authors.
0003 //
0004 // Licensed under the Apache License, Version 2.0 (the "License");
0005 // you may not use this file except in compliance with the License.
0006 // You may obtain a copy of the License at
0007 //
0008 //      https://www.apache.org/licenses/LICENSE-2.0
0009 //
0010 // Unless required by applicable law or agreed to in writing, software
0011 // distributed under the License is distributed on an "AS IS" BASIS,
0012 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
0013 // See the License for the specific language governing permissions and
0014 // limitations under the License.
0015 //
0016 // -----------------------------------------------------------------------------
0017 // File: escaping.h
0018 // -----------------------------------------------------------------------------
0019 //
0020 // This header file contains string utilities involved in escaping and
0021 // unescaping strings in various ways.
0022 
0023 #ifndef ABSL_STRINGS_ESCAPING_H_
0024 #define ABSL_STRINGS_ESCAPING_H_
0025 
0026 #include <cstddef>
0027 #include <string>
0028 #include <vector>
0029 
0030 #include "absl/base/attributes.h"
0031 #include "absl/base/macros.h"
0032 #include "absl/base/nullability.h"
0033 #include "absl/strings/ascii.h"
0034 #include "absl/strings/str_join.h"
0035 #include "absl/strings/string_view.h"
0036 
0037 namespace absl {
0038 ABSL_NAMESPACE_BEGIN
0039 
0040 // CUnescape()
0041 //
0042 // Unescapes a `source` string and copies it into `dest`, rewriting C-style
0043 // escape sequences (https://en.cppreference.com/w/cpp/language/escape) into
0044 // their proper code point equivalents, returning `true` if successful.
0045 //
0046 // The following unescape sequences can be handled:
0047 //
0048 //   * ASCII escape sequences ('\n','\r','\\', etc.) to their ASCII equivalents
0049 //   * Octal escape sequences ('\nnn') to byte nnn. The unescaped value must
0050 //     resolve to a single byte or an error will occur. E.g. values greater than
0051 //     0xff will produce an error.
0052 //   * Hexadecimal escape sequences ('\xnn') to byte nn. While an arbitrary
0053 //     number of following digits are allowed, the unescaped value must resolve
0054 //     to a single byte or an error will occur. E.g. '\x0045' is equivalent to
0055 //     '\x45', but '\x1234' will produce an error.
0056 //   * Unicode escape sequences ('\unnnn' for exactly four hex digits or
0057 //     '\Unnnnnnnn' for exactly eight hex digits, which will be encoded in
0058 //     UTF-8. (E.g., `\u2019` unescapes to the three bytes 0xE2, 0x80, and
0059 //     0x99).
0060 //
0061 // If any errors are encountered, this function returns `false`, leaving the
0062 // `dest` output parameter in an unspecified state, and stores the first
0063 // encountered error in `error`. To disable error reporting, set `error` to
0064 // `nullptr` or use the overload with no error reporting below.
0065 //
0066 // Example:
0067 //
0068 //   std::string s = "foo\\rbar\\nbaz\\t";
0069 //   std::string unescaped_s;
0070 //   if (!absl::CUnescape(s, &unescaped_s)) {
0071 //     ...
0072 //   }
0073 //   EXPECT_EQ(unescaped_s, "foo\rbar\nbaz\t");
0074 bool CUnescape(absl::string_view source, absl::Nonnull<std::string*> dest,
0075                absl::Nullable<std::string*> error);
0076 
0077 // Overload of `CUnescape()` with no error reporting.
0078 inline bool CUnescape(absl::string_view source,
0079                       absl::Nonnull<std::string*> dest) {
0080   return CUnescape(source, dest, nullptr);
0081 }
0082 
0083 // CEscape()
0084 //
0085 // Escapes a 'src' string using C-style escapes sequences
0086 // (https://en.cppreference.com/w/cpp/language/escape), escaping other
0087 // non-printable/non-whitespace bytes as octal sequences (e.g. "\377").
0088 //
0089 // Example:
0090 //
0091 //   std::string s = "foo\rbar\tbaz\010\011\012\013\014\x0d\n";
0092 //   std::string escaped_s = absl::CEscape(s);
0093 //   EXPECT_EQ(escaped_s, "foo\\rbar\\tbaz\\010\\t\\n\\013\\014\\r\\n");
0094 std::string CEscape(absl::string_view src);
0095 
0096 // CHexEscape()
0097 //
0098 // Escapes a 'src' string using C-style escape sequences, escaping
0099 // other non-printable/non-whitespace bytes as hexadecimal sequences (e.g.
0100 // "\xFF").
0101 //
0102 // Example:
0103 //
0104 //   std::string s = "foo\rbar\tbaz\010\011\012\013\014\x0d\n";
0105 //   std::string escaped_s = absl::CHexEscape(s);
0106 //   EXPECT_EQ(escaped_s, "foo\\rbar\\tbaz\\x08\\t\\n\\x0b\\x0c\\r\\n");
0107 std::string CHexEscape(absl::string_view src);
0108 
0109 // Utf8SafeCEscape()
0110 //
0111 // Escapes a 'src' string using C-style escape sequences, escaping bytes as
0112 // octal sequences, and passing through UTF-8 characters without conversion.
0113 // I.e., when encountering any bytes with their high bit set, this function
0114 // will not escape those values, whether or not they are valid UTF-8.
0115 std::string Utf8SafeCEscape(absl::string_view src);
0116 
0117 // Utf8SafeCHexEscape()
0118 //
0119 // Escapes a 'src' string using C-style escape sequences, escaping bytes as
0120 // hexadecimal sequences, and passing through UTF-8 characters without
0121 // conversion.
0122 std::string Utf8SafeCHexEscape(absl::string_view src);
0123 
0124 // Base64Escape()
0125 //
0126 // Encodes a `src` string into a base64-encoded 'dest' string with padding
0127 // characters. This function conforms with RFC 4648 section 4 (base64) and RFC
0128 // 2045.
0129 void Base64Escape(absl::string_view src, absl::Nonnull<std::string*> dest);
0130 std::string Base64Escape(absl::string_view src);
0131 
0132 // WebSafeBase64Escape()
0133 //
0134 // Encodes a `src` string into a base64 string, like Base64Escape() does, but
0135 // outputs '-' instead of '+' and '_' instead of '/', and does not pad 'dest'.
0136 // This function conforms with RFC 4648 section 5 (base64url).
0137 void WebSafeBase64Escape(absl::string_view src,
0138                          absl::Nonnull<std::string*> dest);
0139 std::string WebSafeBase64Escape(absl::string_view src);
0140 
0141 // Base64Unescape()
0142 //
0143 // Converts a `src` string encoded in Base64 (RFC 4648 section 4) to its binary
0144 // equivalent, writing it to a `dest` buffer, returning `true` on success. If
0145 // `src` contains invalid characters, `dest` is cleared and returns `false`.
0146 // If padding is included (note that `Base64Escape()` does produce it), it must
0147 // be correct. In the padding, '=' and '.' are treated identically.
0148 bool Base64Unescape(absl::string_view src, absl::Nonnull<std::string*> dest);
0149 
0150 // WebSafeBase64Unescape()
0151 //
0152 // Converts a `src` string encoded in "web safe" Base64 (RFC 4648 section 5) to
0153 // its binary equivalent, writing it to a `dest` buffer. If `src` contains
0154 // invalid characters, `dest` is cleared and returns `false`. If padding is
0155 // included (note that `WebSafeBase64Escape()` does not produce it), it must be
0156 // correct. In the padding, '=' and '.' are treated identically.
0157 bool WebSafeBase64Unescape(absl::string_view src,
0158                            absl::Nonnull<std::string*> dest);
0159 
0160 // HexStringToBytes()
0161 //
0162 // Converts the hexadecimal encoded data in `hex` into raw bytes in the `bytes`
0163 // output string.  If `hex` does not consist of valid hexadecimal data, this
0164 // function returns false and leaves `bytes` in an unspecified state. Returns
0165 // true on success.
0166 ABSL_MUST_USE_RESULT bool HexStringToBytes(absl::string_view hex,
0167                                            absl::Nonnull<std::string*> bytes);
0168 
0169 // HexStringToBytes()
0170 //
0171 // Converts an ASCII hex string into bytes, returning binary data of length
0172 // `from.size()/2`. The input must be valid hexadecimal data, otherwise the
0173 // return value is unspecified.
0174 ABSL_DEPRECATED("Use the HexStringToBytes() that returns a bool")
0175 std::string HexStringToBytes(absl::string_view from);
0176 
0177 // BytesToHexString()
0178 //
0179 // Converts binary data into an ASCII text string, returning a string of size
0180 // `2*from.size()`.
0181 std::string BytesToHexString(absl::string_view from);
0182 
0183 ABSL_NAMESPACE_END
0184 }  // namespace absl
0185 
0186 #endif  // ABSL_STRINGS_ESCAPING_H_