escaping.cc 39 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022
  1. // Copyright 2017 The Abseil Authors.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // https://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. #include "absl/strings/escaping.h"
  15. #include <algorithm>
  16. #include <array>
  17. #include <cassert>
  18. #include <cstddef>
  19. #include <cstdint>
  20. #include <cstring>
  21. #include <limits>
  22. #include <string>
  23. #include <utility>
  24. #include "absl/base/config.h"
  25. #include "absl/base/internal/endian.h"
  26. #include "absl/base/internal/raw_logging.h"
  27. #include "absl/base/internal/unaligned_access.h"
  28. #include "absl/base/nullability.h"
  29. #include "absl/strings/ascii.h"
  30. #include "absl/strings/charset.h"
  31. #include "absl/strings/internal/escaping.h"
  32. #include "absl/strings/internal/resize_uninitialized.h"
  33. #include "absl/strings/internal/utf8.h"
  34. #include "absl/strings/numbers.h"
  35. #include "absl/strings/str_cat.h"
  36. #include "absl/strings/string_view.h"
  37. namespace absl {
  38. ABSL_NAMESPACE_BEGIN
  39. namespace {
  40. // These are used for the leave_nulls_escaped argument to CUnescapeInternal().
  41. constexpr bool kUnescapeNulls = false;
  42. inline bool is_octal_digit(char c) { return ('0' <= c) && (c <= '7'); }
  43. inline unsigned int hex_digit_to_int(char c) {
  44. static_assert('0' == 0x30 && 'A' == 0x41 && 'a' == 0x61,
  45. "Character set must be ASCII.");
  46. assert(absl::ascii_isxdigit(static_cast<unsigned char>(c)));
  47. unsigned int x = static_cast<unsigned char>(c);
  48. if (x > '9') {
  49. x += 9;
  50. }
  51. return x & 0xf;
  52. }
  53. inline bool IsSurrogate(char32_t c, absl::string_view src,
  54. absl::Nullable<std::string*> error) {
  55. if (c >= 0xD800 && c <= 0xDFFF) {
  56. if (error) {
  57. *error = absl::StrCat("invalid surrogate character (0xD800-DFFF): \\",
  58. src);
  59. }
  60. return true;
  61. }
  62. return false;
  63. }
  64. // ----------------------------------------------------------------------
  65. // CUnescapeInternal()
  66. // Implements both CUnescape() and CUnescapeForNullTerminatedString().
  67. //
  68. // Unescapes C escape sequences and is the reverse of CEscape().
  69. //
  70. // If 'source' is valid, stores the unescaped string and its size in
  71. // 'dest' and 'dest_len' respectively, and returns true. Otherwise
  72. // returns false and optionally stores the error description in
  73. // 'error'. Set 'error' to nullptr to disable error reporting.
  74. //
  75. // 'dest' should point to a buffer that is at least as big as 'source'.
  76. // 'source' and 'dest' may be the same.
  77. //
  78. // NOTE: any changes to this function must also be reflected in the older
  79. // UnescapeCEscapeSequences().
  80. // ----------------------------------------------------------------------
  81. bool CUnescapeInternal(absl::string_view source, bool leave_nulls_escaped,
  82. absl::Nonnull<char*> dest,
  83. absl::Nonnull<ptrdiff_t*> dest_len,
  84. absl::Nullable<std::string*> error) {
  85. char* d = dest;
  86. const char* p = source.data();
  87. const char* end = p + source.size();
  88. const char* last_byte = end - 1;
  89. // Small optimization for case where source = dest and there's no escaping
  90. while (p == d && p < end && *p != '\\') p++, d++;
  91. while (p < end) {
  92. if (*p != '\\') {
  93. *d++ = *p++;
  94. } else {
  95. if (++p > last_byte) { // skip past the '\\'
  96. if (error) *error = "String cannot end with \\";
  97. return false;
  98. }
  99. switch (*p) {
  100. case 'a': *d++ = '\a'; break;
  101. case 'b': *d++ = '\b'; break;
  102. case 'f': *d++ = '\f'; break;
  103. case 'n': *d++ = '\n'; break;
  104. case 'r': *d++ = '\r'; break;
  105. case 't': *d++ = '\t'; break;
  106. case 'v': *d++ = '\v'; break;
  107. case '\\': *d++ = '\\'; break;
  108. case '?': *d++ = '\?'; break; // \? Who knew?
  109. case '\'': *d++ = '\''; break;
  110. case '"': *d++ = '\"'; break;
  111. case '0':
  112. case '1':
  113. case '2':
  114. case '3':
  115. case '4':
  116. case '5':
  117. case '6':
  118. case '7': {
  119. // octal digit: 1 to 3 digits
  120. const char* octal_start = p;
  121. unsigned int ch = static_cast<unsigned int>(*p - '0'); // digit 1
  122. if (p < last_byte && is_octal_digit(p[1]))
  123. ch = ch * 8 + static_cast<unsigned int>(*++p - '0'); // digit 2
  124. if (p < last_byte && is_octal_digit(p[1]))
  125. ch = ch * 8 + static_cast<unsigned int>(*++p - '0'); // digit 3
  126. if (ch > 0xff) {
  127. if (error) {
  128. *error = "Value of \\" +
  129. std::string(octal_start,
  130. static_cast<size_t>(p + 1 - octal_start)) +
  131. " exceeds 0xff";
  132. }
  133. return false;
  134. }
  135. if ((ch == 0) && leave_nulls_escaped) {
  136. // Copy the escape sequence for the null character
  137. const size_t octal_size = static_cast<size_t>(p + 1 - octal_start);
  138. *d++ = '\\';
  139. memmove(d, octal_start, octal_size);
  140. d += octal_size;
  141. break;
  142. }
  143. *d++ = static_cast<char>(ch);
  144. break;
  145. }
  146. case 'x':
  147. case 'X': {
  148. if (p >= last_byte) {
  149. if (error) *error = "String cannot end with \\x";
  150. return false;
  151. } else if (!absl::ascii_isxdigit(static_cast<unsigned char>(p[1]))) {
  152. if (error) *error = "\\x cannot be followed by a non-hex digit";
  153. return false;
  154. }
  155. unsigned int ch = 0;
  156. const char* hex_start = p;
  157. while (p < last_byte &&
  158. absl::ascii_isxdigit(static_cast<unsigned char>(p[1])))
  159. // Arbitrarily many hex digits
  160. ch = (ch << 4) + hex_digit_to_int(*++p);
  161. if (ch > 0xFF) {
  162. if (error) {
  163. *error = "Value of \\" +
  164. std::string(hex_start,
  165. static_cast<size_t>(p + 1 - hex_start)) +
  166. " exceeds 0xff";
  167. }
  168. return false;
  169. }
  170. if ((ch == 0) && leave_nulls_escaped) {
  171. // Copy the escape sequence for the null character
  172. const size_t hex_size = static_cast<size_t>(p + 1 - hex_start);
  173. *d++ = '\\';
  174. memmove(d, hex_start, hex_size);
  175. d += hex_size;
  176. break;
  177. }
  178. *d++ = static_cast<char>(ch);
  179. break;
  180. }
  181. case 'u': {
  182. // \uhhhh => convert 4 hex digits to UTF-8
  183. char32_t rune = 0;
  184. const char* hex_start = p;
  185. if (p + 4 >= end) {
  186. if (error) {
  187. *error = "\\u must be followed by 4 hex digits: \\" +
  188. std::string(hex_start,
  189. static_cast<size_t>(p + 1 - hex_start));
  190. }
  191. return false;
  192. }
  193. for (int i = 0; i < 4; ++i) {
  194. // Look one char ahead.
  195. if (absl::ascii_isxdigit(static_cast<unsigned char>(p[1]))) {
  196. rune = (rune << 4) + hex_digit_to_int(*++p); // Advance p.
  197. } else {
  198. if (error) {
  199. *error = "\\u must be followed by 4 hex digits: \\" +
  200. std::string(hex_start,
  201. static_cast<size_t>(p + 1 - hex_start));
  202. }
  203. return false;
  204. }
  205. }
  206. if ((rune == 0) && leave_nulls_escaped) {
  207. // Copy the escape sequence for the null character
  208. *d++ = '\\';
  209. memmove(d, hex_start, 5); // u0000
  210. d += 5;
  211. break;
  212. }
  213. if (IsSurrogate(rune, absl::string_view(hex_start, 5), error)) {
  214. return false;
  215. }
  216. d += strings_internal::EncodeUTF8Char(d, rune);
  217. break;
  218. }
  219. case 'U': {
  220. // \Uhhhhhhhh => convert 8 hex digits to UTF-8
  221. char32_t rune = 0;
  222. const char* hex_start = p;
  223. if (p + 8 >= end) {
  224. if (error) {
  225. *error = "\\U must be followed by 8 hex digits: \\" +
  226. std::string(hex_start,
  227. static_cast<size_t>(p + 1 - hex_start));
  228. }
  229. return false;
  230. }
  231. for (int i = 0; i < 8; ++i) {
  232. // Look one char ahead.
  233. if (absl::ascii_isxdigit(static_cast<unsigned char>(p[1]))) {
  234. // Don't change rune until we're sure this
  235. // is within the Unicode limit, but do advance p.
  236. uint32_t newrune = (rune << 4) + hex_digit_to_int(*++p);
  237. if (newrune > 0x10FFFF) {
  238. if (error) {
  239. *error = "Value of \\" +
  240. std::string(hex_start,
  241. static_cast<size_t>(p + 1 - hex_start)) +
  242. " exceeds Unicode limit (0x10FFFF)";
  243. }
  244. return false;
  245. } else {
  246. rune = newrune;
  247. }
  248. } else {
  249. if (error) {
  250. *error = "\\U must be followed by 8 hex digits: \\" +
  251. std::string(hex_start,
  252. static_cast<size_t>(p + 1 - hex_start));
  253. }
  254. return false;
  255. }
  256. }
  257. if ((rune == 0) && leave_nulls_escaped) {
  258. // Copy the escape sequence for the null character
  259. *d++ = '\\';
  260. memmove(d, hex_start, 9); // U00000000
  261. d += 9;
  262. break;
  263. }
  264. if (IsSurrogate(rune, absl::string_view(hex_start, 9), error)) {
  265. return false;
  266. }
  267. d += strings_internal::EncodeUTF8Char(d, rune);
  268. break;
  269. }
  270. default: {
  271. if (error) *error = std::string("Unknown escape sequence: \\") + *p;
  272. return false;
  273. }
  274. }
  275. p++; // read past letter we escaped
  276. }
  277. }
  278. *dest_len = d - dest;
  279. return true;
  280. }
  281. // ----------------------------------------------------------------------
  282. // CUnescapeInternal()
  283. //
  284. // Same as above but uses a std::string for output. 'source' and 'dest'
  285. // may be the same.
  286. // ----------------------------------------------------------------------
  287. bool CUnescapeInternal(absl::string_view source, bool leave_nulls_escaped,
  288. absl::Nonnull<std::string*> dest,
  289. absl::Nullable<std::string*> error) {
  290. strings_internal::STLStringResizeUninitialized(dest, source.size());
  291. ptrdiff_t dest_size;
  292. if (!CUnescapeInternal(source,
  293. leave_nulls_escaped,
  294. &(*dest)[0],
  295. &dest_size,
  296. error)) {
  297. return false;
  298. }
  299. dest->erase(static_cast<size_t>(dest_size));
  300. return true;
  301. }
  302. // ----------------------------------------------------------------------
  303. // CEscape()
  304. // CHexEscape()
  305. // Utf8SafeCEscape()
  306. // Utf8SafeCHexEscape()
  307. // Escapes 'src' using C-style escape sequences. This is useful for
  308. // preparing query flags. The 'Hex' version uses hexadecimal rather than
  309. // octal sequences. The 'Utf8Safe' version does not touch UTF-8 bytes.
  310. //
  311. // Escaped chars: \n, \r, \t, ", ', \, and !absl::ascii_isprint().
  312. // ----------------------------------------------------------------------
  313. std::string CEscapeInternal(absl::string_view src, bool use_hex,
  314. bool utf8_safe) {
  315. std::string dest;
  316. bool last_hex_escape = false; // true if last output char was \xNN.
  317. for (char c : src) {
  318. bool is_hex_escape = false;
  319. switch (c) {
  320. case '\n': dest.append("\\" "n"); break;
  321. case '\r': dest.append("\\" "r"); break;
  322. case '\t': dest.append("\\" "t"); break;
  323. case '\"': dest.append("\\" "\""); break;
  324. case '\'': dest.append("\\" "'"); break;
  325. case '\\': dest.append("\\" "\\"); break;
  326. default: {
  327. // Note that if we emit \xNN and the src character after that is a hex
  328. // digit then that digit must be escaped too to prevent it being
  329. // interpreted as part of the character code by C.
  330. const unsigned char uc = static_cast<unsigned char>(c);
  331. if ((!utf8_safe || uc < 0x80) &&
  332. (!absl::ascii_isprint(uc) ||
  333. (last_hex_escape && absl::ascii_isxdigit(uc)))) {
  334. if (use_hex) {
  335. dest.append("\\" "x");
  336. dest.push_back(numbers_internal::kHexChar[uc / 16]);
  337. dest.push_back(numbers_internal::kHexChar[uc % 16]);
  338. is_hex_escape = true;
  339. } else {
  340. dest.append("\\");
  341. dest.push_back(numbers_internal::kHexChar[uc / 64]);
  342. dest.push_back(numbers_internal::kHexChar[(uc % 64) / 8]);
  343. dest.push_back(numbers_internal::kHexChar[uc % 8]);
  344. }
  345. } else {
  346. dest.push_back(c);
  347. break;
  348. }
  349. }
  350. }
  351. last_hex_escape = is_hex_escape;
  352. }
  353. return dest;
  354. }
  355. /* clang-format off */
  356. constexpr std::array<unsigned char, 256> kCEscapedLen = {
  357. 4, 4, 4, 4, 4, 4, 4, 4, 4, 2, 2, 4, 4, 2, 4, 4, // \t, \n, \r
  358. 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
  359. 1, 1, 2, 1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 1, 1, 1, // ", '
  360. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // '0'..'9'
  361. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 'A'..'O'
  362. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 1, 1, 1, // 'P'..'Z', '\'
  363. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 'a'..'o'
  364. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 4, // 'p'..'z', DEL
  365. 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
  366. 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
  367. 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
  368. 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
  369. 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
  370. 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
  371. 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
  372. 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
  373. };
  374. /* clang-format on */
  375. constexpr uint32_t MakeCEscapedLittleEndianUint32(size_t c) {
  376. size_t char_len = kCEscapedLen[c];
  377. if (char_len == 1) {
  378. return static_cast<uint32_t>(c);
  379. }
  380. if (char_len == 2) {
  381. switch (c) {
  382. case '\n':
  383. return '\\' | (static_cast<uint32_t>('n') << 8);
  384. case '\r':
  385. return '\\' | (static_cast<uint32_t>('r') << 8);
  386. case '\t':
  387. return '\\' | (static_cast<uint32_t>('t') << 8);
  388. case '\"':
  389. return '\\' | (static_cast<uint32_t>('\"') << 8);
  390. case '\'':
  391. return '\\' | (static_cast<uint32_t>('\'') << 8);
  392. case '\\':
  393. return '\\' | (static_cast<uint32_t>('\\') << 8);
  394. }
  395. }
  396. return static_cast<uint32_t>('\\' | (('0' + (c / 64)) << 8) |
  397. (('0' + ((c % 64) / 8)) << 16) |
  398. (('0' + (c % 8)) << 24));
  399. }
  400. template <size_t... indexes>
  401. inline constexpr std::array<uint32_t, sizeof...(indexes)>
  402. MakeCEscapedLittleEndianUint32Array(std::index_sequence<indexes...>) {
  403. return {MakeCEscapedLittleEndianUint32(indexes)...};
  404. }
  405. constexpr std::array<uint32_t, 256> kCEscapedLittleEndianUint32Array =
  406. MakeCEscapedLittleEndianUint32Array(std::make_index_sequence<256>());
  407. // Calculates the length of the C-style escaped version of 'src'.
  408. // Assumes that non-printable characters are escaped using octal sequences, and
  409. // that UTF-8 bytes are not handled specially.
  410. inline size_t CEscapedLength(absl::string_view src) {
  411. size_t escaped_len = 0;
  412. // The maximum value of kCEscapedLen[x] is 4, so we can escape any string of
  413. // length size_t_max/4 without checking for overflow.
  414. size_t unchecked_limit =
  415. std::min<size_t>(src.size(), std::numeric_limits<size_t>::max() / 4);
  416. size_t i = 0;
  417. while (i < unchecked_limit) {
  418. // Common case: No need to check for overflow.
  419. escaped_len += kCEscapedLen[static_cast<unsigned char>(src[i++])];
  420. }
  421. while (i < src.size()) {
  422. // Beyond unchecked_limit we need to check for overflow before adding.
  423. size_t char_len = kCEscapedLen[static_cast<unsigned char>(src[i++])];
  424. ABSL_INTERNAL_CHECK(
  425. escaped_len <= std::numeric_limits<size_t>::max() - char_len,
  426. "escaped_len overflow");
  427. escaped_len += char_len;
  428. }
  429. return escaped_len;
  430. }
  431. void CEscapeAndAppendInternal(absl::string_view src,
  432. absl::Nonnull<std::string*> dest) {
  433. size_t escaped_len = CEscapedLength(src);
  434. if (escaped_len == src.size()) {
  435. dest->append(src.data(), src.size());
  436. return;
  437. }
  438. // We keep 3 slop bytes so that we can call `little_endian::Store32`
  439. // invariably regardless of the length of the escaped character.
  440. constexpr size_t slop_bytes = 3;
  441. size_t cur_dest_len = dest->size();
  442. size_t new_dest_len = cur_dest_len + escaped_len + slop_bytes;
  443. ABSL_INTERNAL_CHECK(new_dest_len > cur_dest_len, "std::string size overflow");
  444. strings_internal::AppendUninitializedTraits<std::string>::Append(
  445. dest, escaped_len + slop_bytes);
  446. char* append_ptr = &(*dest)[cur_dest_len];
  447. for (char c : src) {
  448. unsigned char uc = static_cast<unsigned char>(c);
  449. size_t char_len = kCEscapedLen[uc];
  450. uint32_t little_endian_uint32 = kCEscapedLittleEndianUint32Array[uc];
  451. little_endian::Store32(append_ptr, little_endian_uint32);
  452. append_ptr += char_len;
  453. }
  454. dest->resize(new_dest_len - slop_bytes);
  455. }
  456. // Reverses the mapping in Base64EscapeInternal; see that method's
  457. // documentation for details of the mapping.
  458. bool Base64UnescapeInternal(absl::Nullable<const char*> src_param, size_t szsrc,
  459. absl::Nullable<char*> dest, size_t szdest,
  460. const std::array<signed char, 256>& unbase64,
  461. absl::Nonnull<size_t*> len) {
  462. static const char kPad64Equals = '=';
  463. static const char kPad64Dot = '.';
  464. size_t destidx = 0;
  465. int decode = 0;
  466. int state = 0;
  467. unsigned char ch = 0;
  468. unsigned int temp = 0;
  469. // If "char" is signed by default, using *src as an array index results in
  470. // accessing negative array elements. Treat the input as a pointer to
  471. // unsigned char to avoid this.
  472. const unsigned char* src = reinterpret_cast<const unsigned char*>(src_param);
  473. // The GET_INPUT macro gets the next input character, skipping
  474. // over any whitespace, and stopping when we reach the end of the
  475. // string or when we read any non-data character. The arguments are
  476. // an arbitrary identifier (used as a label for goto) and the number
  477. // of data bytes that must remain in the input to avoid aborting the
  478. // loop.
  479. #define GET_INPUT(label, remain) \
  480. label: \
  481. --szsrc; \
  482. ch = *src++; \
  483. decode = unbase64[ch]; \
  484. if (decode < 0) { \
  485. if (absl::ascii_isspace(ch) && szsrc >= remain) goto label; \
  486. state = 4 - remain; \
  487. break; \
  488. }
  489. // if dest is null, we're just checking to see if it's legal input
  490. // rather than producing output. (I suspect this could just be done
  491. // with a regexp...). We duplicate the loop so this test can be
  492. // outside it instead of in every iteration.
  493. if (dest) {
  494. // This loop consumes 4 input bytes and produces 3 output bytes
  495. // per iteration. We can't know at the start that there is enough
  496. // data left in the string for a full iteration, so the loop may
  497. // break out in the middle; if so 'state' will be set to the
  498. // number of input bytes read.
  499. while (szsrc >= 4) {
  500. // We'll start by optimistically assuming that the next four
  501. // bytes of the string (src[0..3]) are four good data bytes
  502. // (that is, no nulls, whitespace, padding chars, or illegal
  503. // chars). We need to test src[0..2] for nulls individually
  504. // before constructing temp to preserve the property that we
  505. // never read past a null in the string (no matter how long
  506. // szsrc claims the string is).
  507. if (!src[0] || !src[1] || !src[2] ||
  508. ((temp = ((unsigned(unbase64[src[0]]) << 18) |
  509. (unsigned(unbase64[src[1]]) << 12) |
  510. (unsigned(unbase64[src[2]]) << 6) |
  511. (unsigned(unbase64[src[3]])))) &
  512. 0x80000000)) {
  513. // Iff any of those four characters was bad (null, illegal,
  514. // whitespace, padding), then temp's high bit will be set
  515. // (because unbase64[] is -1 for all bad characters).
  516. //
  517. // We'll back up and resort to the slower decoder, which knows
  518. // how to handle those cases.
  519. GET_INPUT(first, 4);
  520. temp = static_cast<unsigned char>(decode);
  521. GET_INPUT(second, 3);
  522. temp = (temp << 6) | static_cast<unsigned char>(decode);
  523. GET_INPUT(third, 2);
  524. temp = (temp << 6) | static_cast<unsigned char>(decode);
  525. GET_INPUT(fourth, 1);
  526. temp = (temp << 6) | static_cast<unsigned char>(decode);
  527. } else {
  528. // We really did have four good data bytes, so advance four
  529. // characters in the string.
  530. szsrc -= 4;
  531. src += 4;
  532. }
  533. // temp has 24 bits of input, so write that out as three bytes.
  534. if (destidx + 3 > szdest) return false;
  535. dest[destidx + 2] = static_cast<char>(temp);
  536. temp >>= 8;
  537. dest[destidx + 1] = static_cast<char>(temp);
  538. temp >>= 8;
  539. dest[destidx] = static_cast<char>(temp);
  540. destidx += 3;
  541. }
  542. } else {
  543. while (szsrc >= 4) {
  544. if (!src[0] || !src[1] || !src[2] ||
  545. ((temp = ((unsigned(unbase64[src[0]]) << 18) |
  546. (unsigned(unbase64[src[1]]) << 12) |
  547. (unsigned(unbase64[src[2]]) << 6) |
  548. (unsigned(unbase64[src[3]])))) &
  549. 0x80000000)) {
  550. GET_INPUT(first_no_dest, 4);
  551. GET_INPUT(second_no_dest, 3);
  552. GET_INPUT(third_no_dest, 2);
  553. GET_INPUT(fourth_no_dest, 1);
  554. } else {
  555. szsrc -= 4;
  556. src += 4;
  557. }
  558. destidx += 3;
  559. }
  560. }
  561. #undef GET_INPUT
  562. // if the loop terminated because we read a bad character, return
  563. // now.
  564. if (decode < 0 && ch != kPad64Equals && ch != kPad64Dot &&
  565. !absl::ascii_isspace(ch))
  566. return false;
  567. if (ch == kPad64Equals || ch == kPad64Dot) {
  568. // if we stopped by hitting an '=' or '.', un-read that character -- we'll
  569. // look at it again when we count to check for the proper number of
  570. // equals signs at the end.
  571. ++szsrc;
  572. --src;
  573. } else {
  574. // This loop consumes 1 input byte per iteration. It's used to
  575. // clean up the 0-3 input bytes remaining when the first, faster
  576. // loop finishes. 'temp' contains the data from 'state' input
  577. // characters read by the first loop.
  578. while (szsrc > 0) {
  579. --szsrc;
  580. ch = *src++;
  581. decode = unbase64[ch];
  582. if (decode < 0) {
  583. if (absl::ascii_isspace(ch)) {
  584. continue;
  585. } else if (ch == kPad64Equals || ch == kPad64Dot) {
  586. // back up one character; we'll read it again when we check
  587. // for the correct number of pad characters at the end.
  588. ++szsrc;
  589. --src;
  590. break;
  591. } else {
  592. return false;
  593. }
  594. }
  595. // Each input character gives us six bits of output.
  596. temp = (temp << 6) | static_cast<unsigned char>(decode);
  597. ++state;
  598. if (state == 4) {
  599. // If we've accumulated 24 bits of output, write that out as
  600. // three bytes.
  601. if (dest) {
  602. if (destidx + 3 > szdest) return false;
  603. dest[destidx + 2] = static_cast<char>(temp);
  604. temp >>= 8;
  605. dest[destidx + 1] = static_cast<char>(temp);
  606. temp >>= 8;
  607. dest[destidx] = static_cast<char>(temp);
  608. }
  609. destidx += 3;
  610. state = 0;
  611. temp = 0;
  612. }
  613. }
  614. }
  615. // Process the leftover data contained in 'temp' at the end of the input.
  616. int expected_equals = 0;
  617. switch (state) {
  618. case 0:
  619. // Nothing left over; output is a multiple of 3 bytes.
  620. break;
  621. case 1:
  622. // Bad input; we have 6 bits left over.
  623. return false;
  624. case 2:
  625. // Produce one more output byte from the 12 input bits we have left.
  626. if (dest) {
  627. if (destidx + 1 > szdest) return false;
  628. temp >>= 4;
  629. dest[destidx] = static_cast<char>(temp);
  630. }
  631. ++destidx;
  632. expected_equals = 2;
  633. break;
  634. case 3:
  635. // Produce two more output bytes from the 18 input bits we have left.
  636. if (dest) {
  637. if (destidx + 2 > szdest) return false;
  638. temp >>= 2;
  639. dest[destidx + 1] = static_cast<char>(temp);
  640. temp >>= 8;
  641. dest[destidx] = static_cast<char>(temp);
  642. }
  643. destidx += 2;
  644. expected_equals = 1;
  645. break;
  646. default:
  647. // state should have no other values at this point.
  648. ABSL_RAW_LOG(FATAL, "This can't happen; base64 decoder state = %d",
  649. state);
  650. }
  651. // The remainder of the string should be all whitespace, mixed with
  652. // exactly 0 equals signs, or exactly 'expected_equals' equals
  653. // signs. (Always accepting 0 equals signs is an Abseil extension
  654. // not covered in the RFC, as is accepting dot as the pad character.)
  655. int equals = 0;
  656. while (szsrc > 0) {
  657. if (*src == kPad64Equals || *src == kPad64Dot)
  658. ++equals;
  659. else if (!absl::ascii_isspace(*src))
  660. return false;
  661. --szsrc;
  662. ++src;
  663. }
  664. const bool ok = (equals == 0 || equals == expected_equals);
  665. if (ok) *len = destidx;
  666. return ok;
  667. }
  668. // The arrays below map base64-escaped characters back to their original values.
  669. // For the inverse case, see k(WebSafe)Base64Chars in the internal
  670. // escaping.cc.
  671. // These arrays were generated by the following inversion code:
  672. // #include <sys/time.h>
  673. // #include <stdlib.h>
  674. // #include <string.h>
  675. // main()
  676. // {
  677. // static const char Base64[] =
  678. // "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
  679. // char* pos;
  680. // int idx, i, j;
  681. // printf(" ");
  682. // for (i = 0; i < 255; i += 8) {
  683. // for (j = i; j < i + 8; j++) {
  684. // pos = strchr(Base64, j);
  685. // if ((pos == nullptr) || (j == 0))
  686. // idx = -1;
  687. // else
  688. // idx = pos - Base64;
  689. // if (idx == -1)
  690. // printf(" %2d, ", idx);
  691. // else
  692. // printf(" %2d/*%c*/,", idx, j);
  693. // }
  694. // printf("\n ");
  695. // }
  696. // }
  697. //
  698. // where the value of "Base64[]" was replaced by one of k(WebSafe)Base64Chars
  699. // in the internal escaping.cc.
  700. /* clang-format off */
  701. constexpr std::array<signed char, 256> kUnBase64 = {
  702. -1, -1, -1, -1, -1, -1, -1, -1,
  703. -1, -1, -1, -1, -1, -1, -1, -1,
  704. -1, -1, -1, -1, -1, -1, -1, -1,
  705. -1, -1, -1, -1, -1, -1, -1, -1,
  706. -1, -1, -1, -1, -1, -1, -1, -1,
  707. -1, -1, -1, 62/*+*/, -1, -1, -1, 63/*/ */,
  708. 52/*0*/, 53/*1*/, 54/*2*/, 55/*3*/, 56/*4*/, 57/*5*/, 58/*6*/, 59/*7*/,
  709. 60/*8*/, 61/*9*/, -1, -1, -1, -1, -1, -1,
  710. -1, 0/*A*/, 1/*B*/, 2/*C*/, 3/*D*/, 4/*E*/, 5/*F*/, 6/*G*/,
  711. 07/*H*/, 8/*I*/, 9/*J*/, 10/*K*/, 11/*L*/, 12/*M*/, 13/*N*/, 14/*O*/,
  712. 15/*P*/, 16/*Q*/, 17/*R*/, 18/*S*/, 19/*T*/, 20/*U*/, 21/*V*/, 22/*W*/,
  713. 23/*X*/, 24/*Y*/, 25/*Z*/, -1, -1, -1, -1, -1,
  714. -1, 26/*a*/, 27/*b*/, 28/*c*/, 29/*d*/, 30/*e*/, 31/*f*/, 32/*g*/,
  715. 33/*h*/, 34/*i*/, 35/*j*/, 36/*k*/, 37/*l*/, 38/*m*/, 39/*n*/, 40/*o*/,
  716. 41/*p*/, 42/*q*/, 43/*r*/, 44/*s*/, 45/*t*/, 46/*u*/, 47/*v*/, 48/*w*/,
  717. 49/*x*/, 50/*y*/, 51/*z*/, -1, -1, -1, -1, -1,
  718. -1, -1, -1, -1, -1, -1, -1, -1,
  719. -1, -1, -1, -1, -1, -1, -1, -1,
  720. -1, -1, -1, -1, -1, -1, -1, -1,
  721. -1, -1, -1, -1, -1, -1, -1, -1,
  722. -1, -1, -1, -1, -1, -1, -1, -1,
  723. -1, -1, -1, -1, -1, -1, -1, -1,
  724. -1, -1, -1, -1, -1, -1, -1, -1,
  725. -1, -1, -1, -1, -1, -1, -1, -1,
  726. -1, -1, -1, -1, -1, -1, -1, -1,
  727. -1, -1, -1, -1, -1, -1, -1, -1,
  728. -1, -1, -1, -1, -1, -1, -1, -1,
  729. -1, -1, -1, -1, -1, -1, -1, -1,
  730. -1, -1, -1, -1, -1, -1, -1, -1,
  731. -1, -1, -1, -1, -1, -1, -1, -1,
  732. -1, -1, -1, -1, -1, -1, -1, -1,
  733. -1, -1, -1, -1, -1, -1, -1, -1
  734. };
  735. constexpr std::array<signed char, 256> kUnWebSafeBase64 = {
  736. -1, -1, -1, -1, -1, -1, -1, -1,
  737. -1, -1, -1, -1, -1, -1, -1, -1,
  738. -1, -1, -1, -1, -1, -1, -1, -1,
  739. -1, -1, -1, -1, -1, -1, -1, -1,
  740. -1, -1, -1, -1, -1, -1, -1, -1,
  741. -1, -1, -1, -1, -1, 62/*-*/, -1, -1,
  742. 52/*0*/, 53/*1*/, 54/*2*/, 55/*3*/, 56/*4*/, 57/*5*/, 58/*6*/, 59/*7*/,
  743. 60/*8*/, 61/*9*/, -1, -1, -1, -1, -1, -1,
  744. -1, 0/*A*/, 1/*B*/, 2/*C*/, 3/*D*/, 4/*E*/, 5/*F*/, 6/*G*/,
  745. 07/*H*/, 8/*I*/, 9/*J*/, 10/*K*/, 11/*L*/, 12/*M*/, 13/*N*/, 14/*O*/,
  746. 15/*P*/, 16/*Q*/, 17/*R*/, 18/*S*/, 19/*T*/, 20/*U*/, 21/*V*/, 22/*W*/,
  747. 23/*X*/, 24/*Y*/, 25/*Z*/, -1, -1, -1, -1, 63/*_*/,
  748. -1, 26/*a*/, 27/*b*/, 28/*c*/, 29/*d*/, 30/*e*/, 31/*f*/, 32/*g*/,
  749. 33/*h*/, 34/*i*/, 35/*j*/, 36/*k*/, 37/*l*/, 38/*m*/, 39/*n*/, 40/*o*/,
  750. 41/*p*/, 42/*q*/, 43/*r*/, 44/*s*/, 45/*t*/, 46/*u*/, 47/*v*/, 48/*w*/,
  751. 49/*x*/, 50/*y*/, 51/*z*/, -1, -1, -1, -1, -1,
  752. -1, -1, -1, -1, -1, -1, -1, -1,
  753. -1, -1, -1, -1, -1, -1, -1, -1,
  754. -1, -1, -1, -1, -1, -1, -1, -1,
  755. -1, -1, -1, -1, -1, -1, -1, -1,
  756. -1, -1, -1, -1, -1, -1, -1, -1,
  757. -1, -1, -1, -1, -1, -1, -1, -1,
  758. -1, -1, -1, -1, -1, -1, -1, -1,
  759. -1, -1, -1, -1, -1, -1, -1, -1,
  760. -1, -1, -1, -1, -1, -1, -1, -1,
  761. -1, -1, -1, -1, -1, -1, -1, -1,
  762. -1, -1, -1, -1, -1, -1, -1, -1,
  763. -1, -1, -1, -1, -1, -1, -1, -1,
  764. -1, -1, -1, -1, -1, -1, -1, -1,
  765. -1, -1, -1, -1, -1, -1, -1, -1,
  766. -1, -1, -1, -1, -1, -1, -1, -1,
  767. -1, -1, -1, -1, -1, -1, -1, -1
  768. };
  769. /* clang-format on */
  770. template <typename String>
  771. bool Base64UnescapeInternal(absl::Nullable<const char*> src, size_t slen,
  772. absl::Nonnull<String*> dest,
  773. const std::array<signed char, 256>& unbase64) {
  774. // Determine the size of the output string. Base64 encodes every 3 bytes into
  775. // 4 characters. Any leftover chars are added directly for good measure.
  776. const size_t dest_len = 3 * (slen / 4) + (slen % 4);
  777. strings_internal::STLStringResizeUninitialized(dest, dest_len);
  778. // We are getting the destination buffer by getting the beginning of the
  779. // string and converting it into a char *.
  780. size_t len;
  781. const bool ok =
  782. Base64UnescapeInternal(src, slen, &(*dest)[0], dest_len, unbase64, &len);
  783. if (!ok) {
  784. dest->clear();
  785. return false;
  786. }
  787. // could be shorter if there was padding
  788. assert(len <= dest_len);
  789. dest->erase(len);
  790. return true;
  791. }
  792. /* clang-format off */
  793. constexpr std::array<char, 256> kHexValueLenient = {
  794. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  795. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  796. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  797. 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 0, 0, 0, 0, 0, // '0'..'9'
  798. 0, 10, 11, 12, 13, 14, 15, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 'A'..'F'
  799. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  800. 0, 10, 11, 12, 13, 14, 15, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 'a'..'f'
  801. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  802. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  803. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  804. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  805. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  806. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  807. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  808. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  809. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  810. };
  811. constexpr std::array<signed char, 256> kHexValueStrict = {
  812. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
  813. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
  814. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
  815. 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, -1, -1, -1, -1, -1, -1, // '0'..'9'
  816. -1, 10, 11, 12, 13, 14, 15, -1, -1, -1, -1, -1, -1, -1, -1, -1, // 'A'..'F'
  817. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
  818. -1, 10, 11, 12, 13, 14, 15, -1, -1, -1, -1, -1, -1, -1, -1, -1, // 'a'..'f'
  819. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
  820. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
  821. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
  822. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
  823. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
  824. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
  825. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
  826. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
  827. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
  828. };
  829. /* clang-format on */
  830. // This is a templated function so that T can be either a char*
  831. // or a string. This works because we use the [] operator to access
  832. // individual characters at a time.
  833. template <typename T>
  834. void HexStringToBytesInternal(absl::Nullable<const char*> from, T to,
  835. size_t num) {
  836. for (size_t i = 0; i < num; i++) {
  837. to[i] = static_cast<char>(kHexValueLenient[from[i * 2] & 0xFF] << 4) +
  838. (kHexValueLenient[from[i * 2 + 1] & 0xFF]);
  839. }
  840. }
  841. // This is a templated function so that T can be either a char* or a
  842. // std::string.
  843. template <typename T>
  844. void BytesToHexStringInternal(absl::Nullable<const unsigned char*> src, T dest,
  845. size_t num) {
  846. auto dest_ptr = &dest[0];
  847. for (auto src_ptr = src; src_ptr != (src + num); ++src_ptr, dest_ptr += 2) {
  848. const char* hex_p = &numbers_internal::kHexTable[*src_ptr * 2];
  849. std::copy(hex_p, hex_p + 2, dest_ptr);
  850. }
  851. }
  852. } // namespace
  853. // ----------------------------------------------------------------------
  854. // CUnescape()
  855. //
  856. // See CUnescapeInternal() for implementation details.
  857. // ----------------------------------------------------------------------
  858. bool CUnescape(absl::string_view source, absl::Nonnull<std::string*> dest,
  859. absl::Nullable<std::string*> error) {
  860. return CUnescapeInternal(source, kUnescapeNulls, dest, error);
  861. }
  862. std::string CEscape(absl::string_view src) {
  863. std::string dest;
  864. CEscapeAndAppendInternal(src, &dest);
  865. return dest;
  866. }
  867. std::string CHexEscape(absl::string_view src) {
  868. return CEscapeInternal(src, true, false);
  869. }
  870. std::string Utf8SafeCEscape(absl::string_view src) {
  871. return CEscapeInternal(src, false, true);
  872. }
  873. std::string Utf8SafeCHexEscape(absl::string_view src) {
  874. return CEscapeInternal(src, true, true);
  875. }
  876. bool Base64Unescape(absl::string_view src, absl::Nonnull<std::string*> dest) {
  877. return Base64UnescapeInternal(src.data(), src.size(), dest, kUnBase64);
  878. }
  879. bool WebSafeBase64Unescape(absl::string_view src,
  880. absl::Nonnull<std::string*> dest) {
  881. return Base64UnescapeInternal(src.data(), src.size(), dest, kUnWebSafeBase64);
  882. }
  883. void Base64Escape(absl::string_view src, absl::Nonnull<std::string*> dest) {
  884. strings_internal::Base64EscapeInternal(
  885. reinterpret_cast<const unsigned char*>(src.data()), src.size(), dest,
  886. true, strings_internal::kBase64Chars);
  887. }
  888. void WebSafeBase64Escape(absl::string_view src,
  889. absl::Nonnull<std::string*> dest) {
  890. strings_internal::Base64EscapeInternal(
  891. reinterpret_cast<const unsigned char*>(src.data()), src.size(), dest,
  892. false, strings_internal::kWebSafeBase64Chars);
  893. }
  894. std::string Base64Escape(absl::string_view src) {
  895. std::string dest;
  896. strings_internal::Base64EscapeInternal(
  897. reinterpret_cast<const unsigned char*>(src.data()), src.size(), &dest,
  898. true, strings_internal::kBase64Chars);
  899. return dest;
  900. }
  901. std::string WebSafeBase64Escape(absl::string_view src) {
  902. std::string dest;
  903. strings_internal::Base64EscapeInternal(
  904. reinterpret_cast<const unsigned char*>(src.data()), src.size(), &dest,
  905. false, strings_internal::kWebSafeBase64Chars);
  906. return dest;
  907. }
  908. bool HexStringToBytes(absl::string_view hex,
  909. absl::Nonnull<std::string*> bytes) {
  910. std::string output;
  911. size_t num_bytes = hex.size() / 2;
  912. if (hex.size() != num_bytes * 2) {
  913. return false;
  914. }
  915. absl::strings_internal::STLStringResizeUninitialized(&output, num_bytes);
  916. auto hex_p = hex.cbegin();
  917. for (std::string::iterator bin_p = output.begin(); bin_p != output.end();
  918. ++bin_p) {
  919. int h1 = absl::kHexValueStrict[static_cast<size_t>(*hex_p++)];
  920. int h2 = absl::kHexValueStrict[static_cast<size_t>(*hex_p++)];
  921. if (h1 == -1 || h2 == -1) {
  922. output.resize(static_cast<size_t>(bin_p - output.begin()));
  923. return false;
  924. }
  925. *bin_p = static_cast<char>((h1 << 4) + h2);
  926. }
  927. *bytes = std::move(output);
  928. return true;
  929. }
  930. std::string HexStringToBytes(absl::string_view from) {
  931. std::string result;
  932. const auto num = from.size() / 2;
  933. strings_internal::STLStringResizeUninitialized(&result, num);
  934. absl::HexStringToBytesInternal<std::string&>(from.data(), result, num);
  935. return result;
  936. }
  937. std::string BytesToHexString(absl::string_view from) {
  938. std::string result;
  939. strings_internal::STLStringResizeUninitialized(&result, 2 * from.size());
  940. absl::BytesToHexStringInternal<std::string&>(
  941. reinterpret_cast<const unsigned char*>(from.data()), result, from.size());
  942. return result;
  943. }
  944. ABSL_NAMESPACE_END
  945. } // namespace absl