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: 2020-11-21 04:10:54 Branches: 89 133 66.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
15769771
inline static int8_t unbase64(uint8_t x) {
15
15769771
  return unbase64_table[x];
16
}
17
18
19
3941289
inline uint32_t ReadUint32BE(const unsigned char* p) {
20
7882578
  return static_cast<uint32_t>(p[0] << 24U) |
21
7882578
         static_cast<uint32_t>(p[1] << 16U) |
22
3941289
         static_cast<uint32_t>(p[2] << 8U) |
23
3941289
         static_cast<uint32_t>(p[3]);
24
}
25
26
27
template <typename TypeName>
28
2067
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





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





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





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





69
  V(dst[(*k)++] = ((hi & 0x03) << 6) | ((lo & 0x3F) >> 0));
53
#undef V
54
54
  return true;  // Continue decoding.
55
}
56
57
58
template <typename TypeName>
59
2296
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

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


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

3941264
    if (v & 0x80808080) {
78

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


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

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

3430
  if (src[size - 1] == '=') {
103
1149
    size--;
104

1149
    if (src[size - 1] == '=')
105
1017
      size--;
106
  }
107
3430
  return base64_decoded_size_fast(size);
108
}
109
110
111
template <typename TypeName>
112
2296
size_t base64_decode(char* const dst, const size_t dstlen,
113
                     const TypeName* const src, const size_t srclen) {
114
2296
  const size_t decoded_size = base64_decoded_size(src, srclen);
115
2296
  return base64_decode_fast(dst, dstlen, src, srclen, decoded_size);
116
}
117
118
119
2760
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

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