GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: base64-inl.h Lines: 82 83 98.8 %
Date: 2022-08-31 04:20:31 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
extern const int8_t unbase64_table[256];
13
14
15
18099157
inline static int8_t unbase64(uint8_t x) {
16
18099157
  return unbase64_table[x];
17
}
18
19
20
4522939
inline uint32_t ReadUint32BE(const unsigned char* p) {
21
9045878
  return static_cast<uint32_t>(p[0] << 24U) |
22
9045878
         static_cast<uint32_t>(p[1] << 16U) |
23
4522939
         static_cast<uint32_t>(p[2] << 8U) |
24
4522939
         static_cast<uint32_t>(p[3]);
25
}
26
27
#ifdef _MSC_VER
28
#pragma warning(push)
29
// MSVC C4003: not enough actual parameters for macro 'identifier'
30
#pragma warning(disable : 4003)
31
#endif
32
33
template <typename TypeName>
34
5975
bool base64_decode_group_slow(char* const dst, const size_t dstlen,
35
                              const TypeName* const src, const size_t srclen,
36
                              size_t* const i, size_t* const k) {
37
  uint8_t hi;
38
  uint8_t lo;
39
#define V(expr)                                                                \
40
  for (;;) {                                                                   \
41
    const uint8_t c = static_cast<uint8_t>(src[*i]);                           \
42
    lo = unbase64(c);                                                          \
43
    *i += 1;                                                                   \
44
    if (lo < 64) break;                         /* Legal character. */         \
45
    if (c == '=' || *i >= srclen) return false; /* Stop decoding. */           \
46
  }                                                                            \
47
  expr;                                                                        \
48
  if (*i >= srclen) return false;                                              \
49
  if (*k >= dstlen) return false;                                              \
50
  hi = lo;
51





3019
  V(/* Nothing. */);
52





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





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





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

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

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

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