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-06-08 04:12:09 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
15975459
inline static int8_t unbase64(uint8_t x) {
15
15975459
  return unbase64_table[x];
16
}
17
18
19
3991946
inline uint32_t ReadUint32BE(const unsigned char* p) {
20
7983892
  return static_cast<uint32_t>(p[0] << 24U) |
21
7983892
         static_cast<uint32_t>(p[1] << 16U) |
22
3991946
         static_cast<uint32_t>(p[2] << 8U) |
23
3991946
         static_cast<uint32_t>(p[3]);
24
}
25
26
#ifdef _MSC_VER
27
#pragma warning(push)
28
// MSVC C4003: not enough actual parameters for macro 'identifier'
29
#pragma warning(disable : 4003)
30
#endif
31
32
template <typename TypeName>
33
3570
bool base64_decode_group_slow(char* const dst, const size_t dstlen,
34
                              const TypeName* const src, const size_t srclen,
35
                              size_t* const i, size_t* const k) {
36
  uint8_t hi;
37
  uint8_t lo;
38
#define V(expr)                                                                \
39
  for (;;) {                                                                   \
40
    const uint8_t c = static_cast<uint8_t>(src[*i]);                           \
41
    lo = unbase64(c);                                                          \
42
    *i += 1;                                                                   \
43
    if (lo < 64) break;                         /* Legal character. */         \
44
    if (c == '=' || *i >= srclen) return false; /* Stop decoding. */           \
45
  }                                                                            \
46
  expr;                                                                        \
47
  if (*i >= srclen) return false;                                              \
48
  if (*k >= dstlen) return false;                                              \
49
  hi = lo;
50





3570
  V(/* Nothing. */);
51





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





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





88
  V(dst[(*k)++] = ((hi & 0x03) << 6) | ((lo & 0x3F) >> 0));
54
#undef V
55
69
  return true;  // Continue decoding.
56
}
57
58
#ifdef _MSC_VER
59
#pragma warning(pop)
60
#endif
61
62
template <typename TypeName>
63
3924
size_t base64_decode_fast(char* const dst, const size_t dstlen,
64
                          const TypeName* const src, const size_t srclen,
65
                          const size_t decoded_size) {
66

3924
  const size_t available = dstlen < decoded_size ? dstlen : decoded_size;
67
3924
  const size_t max_k = available / 3 * 3;
68
3924
  size_t max_i = srclen / 4 * 4;
69
3924
  size_t i = 0;
70
3924
  size_t k = 0;
71


7987738
  while (i < max_i && k < max_k) {
72
    const unsigned char txt[] = {
73
3991920
        static_cast<unsigned char>(unbase64(static_cast<uint8_t>(src[i + 0]))),
74
3991920
        static_cast<unsigned char>(unbase64(static_cast<uint8_t>(src[i + 1]))),
75
3991920
        static_cast<unsigned char>(unbase64(static_cast<uint8_t>(src[i + 2]))),
76
3991920
        static_cast<unsigned char>(unbase64(static_cast<uint8_t>(src[i + 3]))),
77
15967680
    };
78
79
3991920
    const uint32_t v = ReadUint32BE(txt);
80
    // If MSB is set, input contains whitespace or is not valid base64.
81

3991920
    if (v & 0x80808080) {
82

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


3911
  if (i < srclen && k < dstlen) {
94
3397
    base64_decode_group_slow(dst, dstlen, src, srclen, &i, &k);
95
  }
96
3911
  return k;
97
}
98
99
100
template <typename TypeName>
101
5105
size_t base64_decoded_size(const TypeName* src, size_t size) {
102
  // 1-byte input cannot be decoded
103

5105
  if (size < 2)
104
    return 0;
105
106

5105
  if (src[size - 1] == '=') {
107
2481
    size--;
108

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

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