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