GCC Code Coverage Report
Directory: ../src/ Exec Total Coverage
File: /home/node-core-coverage/node-core-coverage/workdir/node/out/../src/base64.h Lines: 73 73 100.0 %
Date: 2016-11-30 Branches: 86 139 61.9 %

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 <stddef.h>
9
10
namespace node {
11
//// Base 64 ////
12
#define base64_encoded_size(size) ((size + 2 - ((size + 2) % 3)) / 3 * 4)
13
14
15
// Doesn't check for padding at the end.  Can be 1-2 bytes over.
16
static inline size_t base64_decoded_size_fast(size_t size) {
17
115
  size_t remainder = size % 4;
18
19
115
  size = (size / 4) * 3;
20

115
  if (remainder) {
21

64
    if (size == 0 && remainder == 1) {
22
      // special case: 1-byte input cannot be decoded
23
      size = 0;
24
    } else {
25
      // non-padded input, add 1 or 2 extra bytes
26

64
      size += 1 + (remainder == 3);
27
    }
28
  }
29
30
  return size;
31
}
32
33
template <typename TypeName>
34
105
size_t base64_decoded_size(const TypeName* src, size_t size) {
35

105
  if (size == 0)
36
    return 0;
37
38

105
  if (src[size - 1] == '=')
39
40
    size--;
40


105
  if (size > 0 && src[size - 1] == '=')
41
19
    size--;
42
43
105
  return base64_decoded_size_fast(size);
44
}
45
46
47
extern const int8_t unbase64_table[256];
48
49
50
#define unbase64(x)                                                           \
51
  static_cast<uint8_t>(unbase64_table[static_cast<uint8_t>(x)])
52
53
54
template <typename TypeName>
55
65
size_t base64_decode_slow(char* dst, size_t dstlen,
56
                          const TypeName* src, size_t srclen) {
57
  uint8_t hi;
58
  uint8_t lo;
59
65
  size_t i = 0;
60
65
  size_t k = 0;
61
  for (;;) {
62
#define V(expr)                                                               \
63
    while (i < srclen) {                                                      \
64
      const uint8_t c = src[i];                                               \
65
      lo = unbase64(c);                                                       \
66
      i += 1;                                                                 \
67
      if (lo < 64)                                                            \
68
        break;  /* Legal character. */                                        \
69
      if (c == '=')                                                           \
70
        return k;                                                             \
71
    }                                                                         \
72
    expr;                                                                     \
73
    if (i >= srclen)                                                          \
74
      return k;                                                               \
75
    if (k >= dstlen)                                                          \
76
      return k;                                                               \
77
    hi = lo;
78





565
    V(/* Nothing. */);
79





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





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





471
    V(dst[k++] = ((hi & 0x03) << 6) | ((lo & 0x3F) >> 0));
82
#undef V
83
  }
84
  UNREACHABLE();
85
}
86
87
88
template <typename TypeName>
89
102
size_t base64_decode_fast(char* const dst, const size_t dstlen,
90
                          const TypeName* const src, const size_t srclen,
91
                          const size_t decoded_size) {
92

102
  const size_t available = dstlen < decoded_size ? dstlen : decoded_size;
93
102
  const size_t max_i = srclen / 4 * 4;
94
102
  const size_t max_k = available / 3 * 3;
95
102
  size_t i = 0;
96
102
  size_t k = 0;
97

7194536
  while (i < max_i && k < max_k) {
98
    const uint32_t v =
99
3597227
        unbase64(src[i + 0]) << 24 |
100
3597227
        unbase64(src[i + 1]) << 16 |
101
7194454
        unbase64(src[i + 2]) << 8 |
102
14388908
        unbase64(src[i + 3]);
103
    // If MSB is set, input contains whitespace or is not valid base64.
104

3597227
    if (v & 0x80808080) {
105
      break;
106
    }
107
3597217
    dst[k + 0] = ((v >> 22) & 0xFC) | ((v >> 20) & 0x03);
108
3597217
    dst[k + 1] = ((v >> 12) & 0xF0) | ((v >> 10) & 0x0F);
109
3597217
    dst[k + 2] = ((v >>  2) & 0xC0) | ((v >>  0) & 0x3F);
110
3597217
    i += 4;
111
3597217
    k += 3;
112
  }
113

102
  if (i < srclen && k < dstlen) {
114
65
    return k + base64_decode_slow(dst + k, dstlen - k, src + i, srclen - i);
115
  }
116
  return k;
117
}
118
119
120
template <typename TypeName>
121
102
size_t base64_decode(char* const dst, const size_t dstlen,
122
                     const TypeName* const src, const size_t srclen) {
123
102
  const size_t decoded_size = base64_decoded_size(src, srclen);
124
102
  return base64_decode_fast(dst, dstlen, src, srclen, decoded_size);
125
}
126
127
189
static size_t base64_encode(const char* src,
128
                            size_t slen,
129
                            char* dst,
130
                            size_t dlen) {
131
  // We know how much we'll write, just make sure that there's space.
132
189
  CHECK(dlen >= base64_encoded_size(slen) &&
133
        "not enough space provided for base64 encode");
134
135
189
  dlen = base64_encoded_size(slen);
136
137
  unsigned a;
138
  unsigned b;
139
  unsigned c;
140
  unsigned i;
141
  unsigned k;
142
  unsigned n;
143
144
  static const char table[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
145
                              "abcdefghijklmnopqrstuvwxyz"
146
                              "0123456789+/";
147
148
189
  i = 0;
149
189
  k = 0;
150
189
  n = slen / 3 * 3;
151
152
188217283
  while (i < n) {
153
94108547
    a = src[i + 0] & 0xff;
154
94108547
    b = src[i + 1] & 0xff;
155
94108547
    c = src[i + 2] & 0xff;
156
157
94108547
    dst[k + 0] = table[a >> 2];
158
94108547
    dst[k + 1] = table[((a & 3) << 4) | (b >> 4)];
159
94108547
    dst[k + 2] = table[((b & 0x0f) << 2) | (c >> 6)];
160
94108547
    dst[k + 3] = table[c & 0x3f];
161
162
94108547
    i += 3;
163
94108547
    k += 4;
164
  }
165
166
189
  if (n != slen) {
167
86
    switch (slen - n) {
168
      case 1:
169
46
        a = src[i + 0] & 0xff;
170
46
        dst[k + 0] = table[a >> 2];
171
46
        dst[k + 1] = table[(a & 3) << 4];
172
46
        dst[k + 2] = '=';
173
46
        dst[k + 3] = '=';
174
46
        break;
175
176
      case 2:
177
40
        a = src[i + 0] & 0xff;
178
40
        b = src[i + 1] & 0xff;
179
40
        dst[k + 0] = table[a >> 2];
180
40
        dst[k + 1] = table[((a & 3) << 4) | (b >> 4)];
181
40
        dst[k + 2] = table[(b & 0x0f) << 2];
182
40
        dst[k + 3] = '=';
183
40
        break;
184
    }
185
  }
186
187
189
  return dlen;
188
}
189
}  // namespace node
190
191
192
#endif  // defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS
193
194
#endif  // SRC_BASE64_H_