Coverage Report

Created: 2026-07-25 06:24

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