GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: base64-inl.h Lines: 82 83 98.8 %
Date: 2022-10-02 04:22:39 Branches: 73 111 65.8 %

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
18452554
inline static int8_t unbase64(uint8_t x) {
20
18452554
  return unbase64_table[x];
21
}
22
23
24
4611280
inline uint32_t ReadUint32BE(const unsigned char* p) {
25
9222560
  return static_cast<uint32_t>(p[0] << 24U) |
26
9222560
         static_cast<uint32_t>(p[1] << 16U) |
27
4611280
         static_cast<uint32_t>(p[2] << 8U) |
28
4611280
         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
5997
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





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





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





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





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

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

7525
  if (i < srclen && k < dstlen) {
99
5680
    base64_decode_group_slow(dst, dstlen, src, srclen, &i, &k);
100
  }
101
7525
  return k;
102
}
103
104
105
template <typename TypeName>
106
13990
size_t base64_decoded_size(const TypeName* src, size_t size) {
107
  // 1-byte input cannot be decoded
108
13990
  if (size < 2)
109
    return 0;
110
111
13990
  if (src[size - 1] == '=') {
112
856
    size--;
113
856
    if (src[size - 1] == '=')
114
388
      size--;
115
  }
116
13990
  return base64_decoded_size_fast(size);
117
}
118
119
120
template <typename TypeName>
121
7558
size_t base64_decode(char* const dst, const size_t dstlen,
122
                     const TypeName* const src, const size_t srclen) {
123
7558
  const size_t decoded_size = base64_decoded_size(src, srclen);
124
7558
  return base64_decode_fast(dst, dstlen, src, srclen, decoded_size);
125
}
126
127
128
6249
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

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