GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: base64-inl.h Lines: 82 83 98.8 %
Date: 2022-07-29 04:16:17 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
17873790
inline static int8_t unbase64(uint8_t x) {
16
17873790
  return unbase64_table[x];
17
}
18
19
20
4466486
inline uint32_t ReadUint32BE(const unsigned char* p) {
21
8932972
  return static_cast<uint32_t>(p[0] << 24U) |
22
8932972
         static_cast<uint32_t>(p[1] << 16U) |
23
4466486
         static_cast<uint32_t>(p[2] << 8U) |
24
4466486
         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
6441
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





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





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





1437
  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
8028
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
8028
  const size_t available = dstlen < decoded_size ? dstlen : decoded_size;
68
8028
  const size_t max_k = available / 3 * 3;
69
8028
  size_t max_i = srclen / 4 * 4;
70
8028
  size_t i = 0;
71
8028
  size_t k = 0;
72

8940301
  while (i < max_i && k < max_k) {
73
35729224
    const unsigned char txt[] = {
74
8932306
        static_cast<unsigned char>(unbase64(static_cast<uint8_t>(src[i + 0]))),
75
8932306
        static_cast<unsigned char>(unbase64(static_cast<uint8_t>(src[i + 1]))),
76
8932306
        static_cast<unsigned char>(unbase64(static_cast<uint8_t>(src[i + 2]))),
77
8932306
        static_cast<unsigned char>(unbase64(static_cast<uint8_t>(src[i + 3]))),
78
    };
79
80
8932306
    const uint32_t v = ReadUint32BE(txt);
81
    // If MSB is set, input contains whitespace or is not valid base64.
82
8932306
    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
8932155
      dst[k + 0] = ((v >> 22) & 0xFC) | ((v >> 20) & 0x03);
88
8932155
      dst[k + 1] = ((v >> 12) & 0xF0) | ((v >> 10) & 0x0F);
89
8932155
      dst[k + 2] = ((v >>  2) & 0xC0) | ((v >>  0) & 0x3F);
90
8932155
      i += 4;
91
8932155
      k += 3;
92
    }
93
  }
94

7995
  if (i < srclen && k < dstlen) {
95
6124
    base64_decode_group_slow(dst, dstlen, src, srclen, &i, &k);
96
  }
97
7995
  return k;
98
}
99
100
101
template <typename TypeName>
102
14424
size_t base64_decoded_size(const TypeName* src, size_t size) {
103
  // 1-byte input cannot be decoded
104
14424
  if (size < 2)
105
    return 0;
106
107
14424
  if (src[size - 1] == '=') {
108
1322
    size--;
109
1322
    if (src[size - 1] == '=')
110
914
      size--;
111
  }
112
14424
  return base64_decoded_size_fast(size);
113
}
114
115
116
template <typename TypeName>
117
8028
size_t base64_decode(char* const dst, const size_t dstlen,
118
                     const TypeName* const src, const size_t srclen) {
119
8028
  const size_t decoded_size = base64_decoded_size(src, srclen);
120
8028
  return base64_decode_fast(dst, dstlen, src, srclen, decoded_size);
121
}
122
123
124
5534
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

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