GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: base64-inl.h Lines: 84 85 98.8 %
Date: 2021-09-18 04:12:33 Branches: 75 113 66.4 %

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 "util.h"
8
9
namespace node {
10
11
extern const int8_t unbase64_table[256];
12
13
14
16253058
inline static int8_t unbase64(uint8_t x) {
15
16253058
  return unbase64_table[x];
16
}
17
18
19
4061341
inline uint32_t ReadUint32BE(const unsigned char* p) {
20
8122682
  return static_cast<uint32_t>(p[0] << 24U) |
21
8122682
         static_cast<uint32_t>(p[1] << 16U) |
22
4061341
         static_cast<uint32_t>(p[2] << 8U) |
23
4061341
         static_cast<uint32_t>(p[3]);
24
}
25
26
#ifdef _MSC_VER
27
#pragma warning(push)
28
// MSVC C4003: not enough actual parameters for macro 'identifier'
29
#pragma warning(disable : 4003)
30
#endif
31
32
template <typename TypeName>
33
6335
bool base64_decode_group_slow(char* const dst, const size_t dstlen,
34
                              const TypeName* const src, const size_t srclen,
35
                              size_t* const i, size_t* const k) {
36
  uint8_t hi;
37
  uint8_t lo;
38
#define V(expr)                                                                \
39
  for (;;) {                                                                   \
40
    const uint8_t c = static_cast<uint8_t>(src[*i]);                           \
41
    lo = unbase64(c);                                                          \
42
    *i += 1;                                                                   \
43
    if (lo < 64) break;                         /* Legal character. */         \
44
    if (c == '=' || *i >= srclen) return false; /* Stop decoding. */           \
45
  }                                                                            \
46
  expr;                                                                        \
47
  if (*i >= srclen) return false;                                              \
48
  if (*k >= dstlen) return false;                                              \
49
  hi = lo;
50





3199
  V(/* Nothing. */);
51





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





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





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

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

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

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