GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: base64-inl.h Lines: 82 83 98.8 %
Date: 2022-11-04 03:21:42 Branches: 62 71 87.3 %

Line Branch Exec Source
1
#ifndef SRC_BASE64_INL_H_
2
#define SRC_BASE64_INL_H_
3
4
#if defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS
5
6
#include "base64.h"
7
#include "libbase64.h"
8
#include "util.h"
9
10
namespace node {
11
12
static constexpr char base64_table_url[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
13
                                           "abcdefghijklmnopqrstuvwxyz"
14
                                           "0123456789-_";
15
16
extern const int8_t unbase64_table[256];
17
18
19
18460453
inline static int8_t unbase64(uint8_t x) {
20
18460453
  return unbase64_table[x];
21
}
22
23
24
4613089
inline uint32_t ReadUint32BE(const unsigned char* p) {
25
9226178
  return static_cast<uint32_t>(p[0] << 24U) |
26
9226178
         static_cast<uint32_t>(p[1] << 16U) |
27
4613089
         static_cast<uint32_t>(p[2] << 8U) |
28
4613089
         static_cast<uint32_t>(p[3]);
29
}
30
31
#ifdef _MSC_VER
32
#pragma warning(push)
33
// MSVC C4003: not enough actual parameters for macro 'identifier'
34
#pragma warning(disable : 4003)
35
#endif
36
37
template <typename TypeName>
38
6617
bool base64_decode_group_slow(char* const dst, const size_t dstlen,
39
                              const TypeName* const src, const size_t srclen,
40
                              size_t* const i, size_t* const k) {
41
  uint8_t hi;
42
  uint8_t lo;
43
#define V(expr)                                                                \
44
  for (;;) {                                                                   \
45
    const uint8_t c = static_cast<uint8_t>(src[*i]);                           \
46
    lo = unbase64(c);                                                          \
47
    *i += 1;                                                                   \
48
    if (lo < 64) break;                         /* Legal character. */         \
49
    if (c == '=' || *i >= srclen) return false; /* Stop decoding. */           \
50
  }                                                                            \
51
  expr;                                                                        \
52
  if (*i >= srclen) return false;                                              \
53
  if (*k >= dstlen) return false;                                              \
54
  hi = lo;
55


6617
  V(/* Nothing. */);
56


6439
  V(dst[(*k)++] = ((hi & 0x3F) << 2) | ((lo & 0x30) >> 4));
57


2997
  V(dst[(*k)++] = ((hi & 0x0F) << 4) | ((lo & 0x3C) >> 2));
58


168
  V(dst[(*k)++] = ((hi & 0x03) << 6) | ((lo & 0x3F) >> 0));
59
#undef V
60
120
  return true;  // Continue decoding.
61
}
62
63
#ifdef _MSC_VER
64
#pragma warning(pop)
65
#endif
66
67
template <typename TypeName>
68
8164
size_t base64_decode_fast(char* const dst, const size_t dstlen,
69
                          const TypeName* const src, const size_t srclen,
70
                          const size_t decoded_size) {
71
8164
  const size_t available = dstlen < decoded_size ? dstlen : decoded_size;
72
8164
  const size_t max_k = available / 3 * 3;
73
8164
  size_t max_i = srclen / 4 * 4;
74
8164
  size_t i = 0;
75
8164
  size_t k = 0;
76

9233625
  while (i < max_i && k < max_k) {
77
36902048
    const unsigned char txt[] = {
78
9225512
        static_cast<unsigned char>(unbase64(static_cast<uint8_t>(src[i + 0]))),
79
9225512
        static_cast<unsigned char>(unbase64(static_cast<uint8_t>(src[i + 1]))),
80
9225512
        static_cast<unsigned char>(unbase64(static_cast<uint8_t>(src[i + 2]))),
81
9225512
        static_cast<unsigned char>(unbase64(static_cast<uint8_t>(src[i + 3]))),
82
    };
83
84
9225512
    const uint32_t v = ReadUint32BE(txt);
85
    // If MSB is set, input contains whitespace or is not valid base64.
86
9225512
    if (v & 0x80808080) {
87
171
      if (!base64_decode_group_slow(dst, dstlen, src, srclen, &i, &k))
88
51
        return k;
89
120
      max_i = i + (srclen - i) / 4 * 4;  // Align max_i again.
90
    } else {
91
9225341
      dst[k + 0] = ((v >> 22) & 0xFC) | ((v >> 20) & 0x03);
92
9225341
      dst[k + 1] = ((v >> 12) & 0xF0) | ((v >> 10) & 0x0F);
93
9225341
      dst[k + 2] = ((v >>  2) & 0xC0) | ((v >>  0) & 0x3F);
94
9225341
      i += 4;
95
9225341
      k += 3;
96
    }
97
  }
98

8113
  if (i < srclen && k < dstlen) {
99
6258
    base64_decode_group_slow(dst, dstlen, src, srclen, &i, &k);
100
  }
101
8113
  return k;
102
}
103
104
105
template <typename TypeName>
106
14586
size_t base64_decoded_size(const TypeName* src, size_t size) {
107
  // 1-byte input cannot be decoded
108
14586
  if (size < 2)
109
    return 0;
110
111
14586
  if (src[size - 1] == '=') {
112
1424
    size--;
113
1424
    if (src[size - 1] == '=')
114
918
      size--;
115
  }
116
14586
  return base64_decoded_size_fast(size);
117
}
118
119
120
template <typename TypeName>
121
8164
size_t base64_decode(char* const dst, const size_t dstlen,
122
                     const TypeName* const src, const size_t srclen) {
123
8164
  const size_t decoded_size = base64_decoded_size(src, srclen);
124
8164
  return base64_decode_fast(dst, dstlen, src, srclen, decoded_size);
125
}
126
127
128
5860
inline size_t base64_encode(const char* src,
129
                            size_t slen,
130
                            char* dst,
131
                            size_t dlen,
132
                            Base64Mode mode) {
133
  // We know how much we'll write, just make sure that there's space.
134

5860
  CHECK(dlen >= base64_encoded_size(slen, mode) &&
135
        "not enough space provided for base64 encode");
136
137
5860
  dlen = base64_encoded_size(slen, mode);
138
139
5860
  if (mode == Base64Mode::NORMAL) {
140
2026
    ::base64_encode(src, slen, dst, &dlen, 0);
141
2026
    return dlen;
142
  }
143
144
  unsigned a;
145
  unsigned b;
146
  unsigned c;
147
  unsigned i;
148
  unsigned k;
149
  unsigned n;
150
151
3834
  const char* table = base64_table_url;
152
153
3834
  i = 0;
154
3834
  k = 0;
155
3834
  n = slen / 3 * 3;
156
157
101286
  while (i < n) {
158
97452
    a = src[i + 0] & 0xff;
159
97452
    b = src[i + 1] & 0xff;
160
97452
    c = src[i + 2] & 0xff;
161
162
97452
    dst[k + 0] = table[a >> 2];
163
97452
    dst[k + 1] = table[((a & 3) << 4) | (b >> 4)];
164
97452
    dst[k + 2] = table[((b & 0x0f) << 2) | (c >> 6)];
165
97452
    dst[k + 3] = table[c & 0x3f];
166
167
97452
    i += 3;
168
97452
    k += 4;
169
  }
170
171
3834
  switch (slen - n) {
172
1336
    case 1:
173
1336
      a = src[i + 0] & 0xff;
174
1336
      dst[k + 0] = table[a >> 2];
175
1336
      dst[k + 1] = table[(a & 3) << 4];
176
1336
      break;
177
1091
    case 2:
178
1091
      a = src[i + 0] & 0xff;
179
1091
      b = src[i + 1] & 0xff;
180
1091
      dst[k + 0] = table[a >> 2];
181
1091
      dst[k + 1] = table[((a & 3) << 4) | (b >> 4)];
182
1091
      dst[k + 2] = table[(b & 0x0f) << 2];
183
1091
      break;
184
  }
185
186
3834
  return dlen;
187
}
188
189
}  // namespace node
190
191
#endif  // defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS
192
193
#endif  // SRC_BASE64_INL_H_