GCC Code Coverage Report
Directory: ../ Exec Total Coverage
File: /home/iojs/build/workspace/node-test-commit-linux-coverage-daily/nodes/benchmark/out/../src/base64-inl.h Lines: 82 83 98.8 %
Date: 2021-02-19 04:08:54 Branches: 61 133 45.9 %

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
1212320
inline static int8_t unbase64(uint8_t x) {
15
1212320
  return unbase64_table[x];
16
}
17
18
19
295994
inline uint32_t ReadUint32BE(const unsigned char* p) {
20
591988
  return static_cast<uint32_t>(p[0] << 24U) |
21
591988
         static_cast<uint32_t>(p[1] << 16U) |
22
295994
         static_cast<uint32_t>(p[2] << 8U) |
23
295994
         static_cast<uint32_t>(p[3]);
24
}
25
26
27
template <typename TypeName>
28
14138
bool base64_decode_group_slow(char* const dst, const size_t dstlen,
29
                              const TypeName* const src, const size_t srclen,
30
                              size_t* const i, size_t* const k) {
31
  uint8_t hi;
32
  uint8_t lo;
33
#define V(expr)                                                               \
34
  for (;;) {                                                                  \
35
    const uint8_t c = src[*i];                                                \
36
    lo = unbase64(c);                                                         \
37
    *i += 1;                                                                  \
38
    if (lo < 64)                                                              \
39
      break;  /* Legal character. */                                          \
40
    if (c == '=' || *i >= srclen)                                             \
41
      return false;  /* Stop decoding. */                                     \
42
  }                                                                           \
43
  expr;                                                                       \
44
  if (*i >= srclen)                                                           \
45
    return false;                                                             \
46
  if (*k >= dstlen)                                                           \
47
    return false;                                                             \
48
  hi = lo;
49





14138
  V(/* Nothing. */);
50





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





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





28
  V(dst[(*k)++] = ((hi & 0x03) << 6) | ((lo & 0x3F) >> 0));
53
#undef V
54
22
  return true;  // Continue decoding.
55
}
56
57
58
template <typename TypeName>
59
14101
size_t base64_decode_fast(char* const dst, const size_t dstlen,
60
                          const TypeName* const src, const size_t srclen,
61
                          const size_t decoded_size) {
62

14101
  const size_t available = dstlen < decoded_size ? dstlen : decoded_size;
63
14101
  const size_t max_k = available / 3 * 3;
64
14101
  size_t max_i = srclen / 4 * 4;
65
14101
  size_t i = 0;
66
14101
  size_t k = 0;
67


606083
  while (i < max_i && k < max_k) {
68
    const unsigned char txt[] = {
69
295994
      static_cast<unsigned char>(unbase64(src[i + 0])),
70
295994
      static_cast<unsigned char>(unbase64(src[i + 1])),
71
295994
      static_cast<unsigned char>(unbase64(src[i + 2])),
72
295994
      static_cast<unsigned char>(unbase64(src[i + 3])),
73
1183976
    };
74
75
295994
    const uint32_t v = ReadUint32BE(txt);
76
    // If MSB is set, input contains whitespace or is not valid base64.
77

295994
    if (v & 0x80808080) {
78

25
      if (!base64_decode_group_slow(dst, dstlen, src, srclen, &i, &k))
79
3
        return k;
80
22
      max_i = i + (srclen - i) / 4 * 4;  // Align max_i again.
81
    } else {
82
295969
      dst[k + 0] = ((v >> 22) & 0xFC) | ((v >> 20) & 0x03);
83
295969
      dst[k + 1] = ((v >> 12) & 0xF0) | ((v >> 10) & 0x0F);
84
295969
      dst[k + 2] = ((v >>  2) & 0xC0) | ((v >>  0) & 0x3F);
85
295969
      i += 4;
86
295969
      k += 3;
87
    }
88
  }
89


14098
  if (i < srclen && k < dstlen) {
90
14093
    base64_decode_group_slow(dst, dstlen, src, srclen, &i, &k);
91
  }
92
14098
  return k;
93
}
94
95
96
template <typename TypeName>
97
14101
size_t base64_decoded_size(const TypeName* src, size_t size) {
98
  // 1-byte input cannot be decoded
99

14101
  if (size < 2)
100
    return 0;
101
102

14101
  if (src[size - 1] == '=') {
103
14083
    size--;
104

14083
    if (src[size - 1] == '=')
105
14068
      size--;
106
  }
107
14101
  return base64_decoded_size_fast(size);
108
}
109
110
111
template <typename TypeName>
112
14101
size_t base64_decode(char* const dst, const size_t dstlen,
113
                     const TypeName* const src, const size_t srclen) {
114
14101
  const size_t decoded_size = base64_decoded_size(src, srclen);
115
14101
  return base64_decode_fast(dst, dstlen, src, srclen, decoded_size);
116
}
117
118
119
386
inline size_t base64_encode(const char* src,
120
                            size_t slen,
121
                            char* dst,
122
                            size_t dlen,
123
                            Base64Mode mode) {
124
  // We know how much we'll write, just make sure that there's space.
125

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