• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 // Base64Unescape()
121 //
122 // Converts a `src` string encoded in Base64 to its binary equivalent, writing
123 // it to a `dest` buffer, returning `true` on success. If `src` contains invalid
124 // characters, `dest` is cleared and returns `false`.
125 bool Base64Unescape(absl::string_view src, std::string* dest);
126 
127 // WebSafeBase64Unescape()
128 //
129 // Converts a `src` string encoded in Base64 to its binary equivalent, writing
130 // it to a `dest` buffer, but using '-' instead of '+', and '_' instead of '/'.
131 // If `src` contains invalid characters, `dest` is cleared and returns `false`.
132 bool WebSafeBase64Unescape(absl::string_view src, std::string* dest);
133 
134 // Base64Escape()
135 //
136 // Encodes a `src` string into a base64-encoded string, with padding characters.
137 // This function conforms with RFC 4648 section 4 (base64).
138 void Base64Escape(absl::string_view src, std::string* dest);
139 std::string Base64Escape(absl::string_view src);
140 
141 // WebSafeBase64Escape()
142 //
143 // Encodes a `src` string into a base64-like string, using '-' instead of '+'
144 // and '_' instead of '/', and without padding. This function conforms with RFC
145 // 4648 section 5 (base64url).
146 void WebSafeBase64Escape(absl::string_view src, std::string* dest);
147 std::string WebSafeBase64Escape(absl::string_view src);
148 
149 // HexStringToBytes()
150 //
151 // Converts an ASCII hex string into bytes, returning binary data of length
152 // `from.size()/2`.
153 std::string HexStringToBytes(absl::string_view from);
154 
155 // BytesToHexString()
156 //
157 // Converts binary data into an ASCII text string, returning a string of size
158 // `2*from.size()`.
159 std::string BytesToHexString(absl::string_view from);
160 
161 ABSL_NAMESPACE_END
162 }  // namespace absl
163 
164 #endif  // ABSL_STRINGS_ESCAPING_H_
165