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