GCC Code Coverage Report
Directory: ../ Exec Total Coverage
File: /home/iojs/build/workspace/node-test-commit-linux-coverage-daily/nodes/benchmark/out/../src/base64.h Lines: 76 77 98.7 %
Date: 2020-09-03 22:13:26 Branches: 86 131 65.6 %

Line Branch Exec Source
1
#ifndef SRC_BASE64_H_
2
#define SRC_BASE64_H_
3
4
#if defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS
5
6
#include "util.h"
7
8
#include <cstddef>
9
#include <cstdint>
10
11
namespace node {
12
//// Base 64 ////
13
3621
static inline constexpr size_t base64_encoded_size(size_t size) {
14
3621
  return ((size + 2) / 3 * 4);
15
}
16
17
// Doesn't check for padding at the end.  Can be 1-2 bytes over.
18
354098
static inline constexpr size_t base64_decoded_size_fast(size_t size) {
19
  // 1-byte input cannot be decoded
20
354098
  return size > 1 ? (size / 4) * 3 + (size % 4 + 1) / 2 : 0;
21
}
22
23
template <typename TypeName>
24
354088
size_t base64_decoded_size(const TypeName* src, size_t size) {
25
  // 1-byte input cannot be decoded
26

354088
  if (size < 2)
27
    return 0;
28
29

354088
  if (src[size - 1] == '=') {
30
353996
    size--;
31

353996
    if (src[size - 1] == '=')
32
353902
      size--;
33
  }
34
354088
  return base64_decoded_size_fast(size);
35
}
36
37
38
extern const int8_t unbase64_table[256];
39
40
41
44836295
inline static int8_t unbase64(uint8_t x) {
42
44836295
  return unbase64_table[x];
43
}
44
45
46
template <typename TypeName>
47
354153
bool base64_decode_group_slow(char* const dst, const size_t dstlen,
48
                              const TypeName* const src, const size_t srclen,
49
                              size_t* const i, size_t* const k) {
50
  uint8_t hi;
51
  uint8_t lo;
52
#define V(expr)                                                               \
53
  for (;;) {                                                                  \
54
    const uint8_t c = src[*i];                                                \
55
    lo = unbase64(c);                                                         \
56
    *i += 1;                                                                  \
57
    if (lo < 64)                                                              \
58
      break;  /* Legal character. */                                          \
59
    if (c == '=' || *i >= srclen)                                             \
60
      return false;  /* Stop decoding. */                                     \
61
  }                                                                           \
62
  expr;                                                                       \
63
  if (*i >= srclen)                                                           \
64
    return false;                                                             \
65
  if (*k >= dstlen)                                                           \
66
    return false;                                                             \
67
  hi = lo;
68





354153
  V(/* Nothing. */);
69





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





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





69
  V(dst[(*k)++] = ((hi & 0x03) << 6) | ((lo & 0x3F) >> 0));
72
#undef V
73
54
  return true;  // Continue decoding.
74
}
75
76
77
template <typename TypeName>
78
354083
size_t base64_decode_fast(char* const dst, const size_t dstlen,
79
                          const TypeName* const src, const size_t srclen,
80
                          const size_t decoded_size) {
81

354083
  const size_t available = dstlen < decoded_size ? dstlen : decoded_size;
82
354083
  const size_t max_k = available / 3 * 3;
83
354083
  size_t max_i = srclen / 4 * 4;
84
354083
  size_t i = 0;
85
354083
  size_t k = 0;
86


22417955
  while (i < max_i && k < max_k) {
87
    const uint32_t v =
88
22063900
        unbase64(src[i + 0]) << 24 |
89
22063900
        unbase64(src[i + 1]) << 16 |
90
22063900
        unbase64(src[i + 2]) << 8 |
91
22063900
        unbase64(src[i + 3]);
92
    // If MSB is set, input contains whitespace or is not valid base64.
93

11031950
    if (v & 0x80808080) {
94

68
      if (!base64_decode_group_slow(dst, dstlen, src, srclen, &i, &k))
95
14
        return k;
96
54
      max_i = i + (srclen - i) / 4 * 4;  // Align max_i again.
97
    } else {
98
11031882
      dst[k + 0] = ((v >> 22) & 0xFC) | ((v >> 20) & 0x03);
99
11031882
      dst[k + 1] = ((v >> 12) & 0xF0) | ((v >> 10) & 0x0F);
100
11031882
      dst[k + 2] = ((v >>  2) & 0xC0) | ((v >>  0) & 0x3F);
101
11031882
      i += 4;
102
11031882
      k += 3;
103
    }
104
  }
105


354069
  if (i < srclen && k < dstlen) {
106
354021
    base64_decode_group_slow(dst, dstlen, src, srclen, &i, &k);
107
  }
108
354069
  return k;
109
}
110
111
112
template <typename TypeName>
113
354083
size_t base64_decode(char* const dst, const size_t dstlen,
114
                     const TypeName* const src, const size_t srclen) {
115
354083
  const size_t decoded_size = base64_decoded_size(src, srclen);
116
354083
  return base64_decode_fast(dst, dstlen, src, srclen, decoded_size);
117
}
118
119
1221
static size_t base64_encode(const char* src,
120
                            size_t slen,
121
                            char* dst,
122
                            size_t dlen) {
123
  // We know how much we'll write, just make sure that there's space.
124

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