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-07-19 22:14:24 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
7854
static inline constexpr size_t base64_encoded_size(size_t size) {
14
7854
  return ((size + 2) / 3 * 4);
15
}
16
17
// Doesn't check for padding at the end.  Can be 1-2 bytes over.
18
341769
static inline constexpr size_t base64_decoded_size_fast(size_t size) {
19
  // 1-byte input cannot be decoded
20
341769
  return size > 1 ? (size / 4) * 3 + (size % 4 + 1) / 2 : 0;
21
}
22
23
template <typename TypeName>
24
341759
size_t base64_decoded_size(const TypeName* src, size_t size) {
25
  // 1-byte input cannot be decoded
26

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

341759
  if (src[size - 1] == '=') {
30
341665
    size--;
31

341665
    if (src[size - 1] == '=')
32
341538
      size--;
33
  }
34
341759
  return base64_decoded_size_fast(size);
35
}
36
37
38
extern const int8_t unbase64_table[256];
39
40
41
43774418
inline static int8_t unbase64(uint8_t x) {
42
43774418
  return unbase64_table[x];
43
}
44
45
46
template <typename TypeName>
47
341822
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





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





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





207
  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
341754
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

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


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

10772638
    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
10772570
      dst[k + 0] = ((v >> 22) & 0xFC) | ((v >> 20) & 0x03);
99
10772570
      dst[k + 1] = ((v >> 12) & 0xF0) | ((v >> 10) & 0x0F);
100
10772570
      dst[k + 2] = ((v >>  2) & 0xC0) | ((v >>  0) & 0x3F);
101
10772570
      i += 4;
102
10772570
      k += 3;
103
    }
104
  }
105


341740
  if (i < srclen && k < dstlen) {
106
341690
    base64_decode_group_slow(dst, dstlen, src, srclen, &i, &k);
107
  }
108
341740
  return k;
109
}
110
111
112
template <typename TypeName>
113
341754
size_t base64_decode(char* const dst, const size_t dstlen,
114
                     const TypeName* const src, const size_t srclen) {
115
341754
  const size_t decoded_size = base64_decoded_size(src, srclen);
116
341754
  return base64_decode_fast(dst, dstlen, src, srclen, decoded_size);
117
}
118
119
2632
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

2632
  CHECK(dlen >= base64_encoded_size(slen) &&
125
        "not enough space provided for base64 encode");
126
127
2632
  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
2632
  i = 0;
141
2632
  k = 0;
142
2632
  n = slen / 3 * 3;
143
144
367229852
  while (i < n) {
145
183613610
    a = src[i + 0] & 0xff;
146
183613610
    b = src[i + 1] & 0xff;
147
183613610
    c = src[i + 2] & 0xff;
148
149
183613610
    dst[k + 0] = table[a >> 2];
150
183613610
    dst[k + 1] = table[((a & 3) << 4) | (b >> 4)];
151
183613610
    dst[k + 2] = table[((b & 0x0f) << 2) | (c >> 6)];
152
183613610
    dst[k + 3] = table[c & 0x3f];
153
154
183613610
    i += 3;
155
183613610
    k += 4;
156
  }
157
158
2632
  switch (slen - n) {
159
    case 1:
160
490
      a = src[i + 0] & 0xff;
161
490
      dst[k + 0] = table[a >> 2];
162
490
      dst[k + 1] = table[(a & 3) << 4];
163
490
      dst[k + 2] = '=';
164
490
      dst[k + 3] = '=';
165
490
      break;
166
    case 2:
167
1279
      a = src[i + 0] & 0xff;
168
1279
      b = src[i + 1] & 0xff;
169
1279
      dst[k + 0] = table[a >> 2];
170
1279
      dst[k + 1] = table[((a & 3) << 4) | (b >> 4)];
171
1279
      dst[k + 2] = table[(b & 0x0f) << 2];
172
1279
      dst[k + 3] = '=';
173
1279
      break;
174
  }
175
176
2632
  return dlen;
177
}
178
}  // namespace node
179
180
181
#endif  // defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS
182
183
#endif  // SRC_BASE64_H_