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
unbase64(uint8_t x)14 inline static int8_t unbase64(uint8_t x) {
15 return unbase64_table[x];
16 }
17
18
ReadUint32BE(const unsigned char * p)19 inline uint32_t ReadUint32BE(const unsigned char* p) {
20 return static_cast<uint32_t>(p[0] << 24U) |
21 static_cast<uint32_t>(p[1] << 16U) |
22 static_cast<uint32_t>(p[2] << 8U) |
23 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>
base64_decode_group_slow(char * const dst,const size_t dstlen,const TypeName * const src,const size_t srclen,size_t * const i,size_t * const k)33 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 V(/* Nothing. */);
51 V(dst[(*k)++] = ((hi & 0x3F) << 2) | ((lo & 0x30) >> 4));
52 V(dst[(*k)++] = ((hi & 0x0F) << 4) | ((lo & 0x3C) >> 2));
53 V(dst[(*k)++] = ((hi & 0x03) << 6) | ((lo & 0x3F) >> 0));
54 #undef V
55 return true; // Continue decoding.
56 }
57
58 #ifdef _MSC_VER
59 #pragma warning(pop)
60 #endif
61
62 template <typename TypeName>
base64_decode_fast(char * const dst,const size_t dstlen,const TypeName * const src,const size_t srclen,const size_t decoded_size)63 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 const size_t available = dstlen < decoded_size ? dstlen : decoded_size;
67 const size_t max_k = available / 3 * 3;
68 size_t max_i = srclen / 4 * 4;
69 size_t i = 0;
70 size_t k = 0;
71 while (i < max_i && k < max_k) {
72 const unsigned char txt[] = {
73 static_cast<unsigned char>(unbase64(static_cast<uint8_t>(src[i + 0]))),
74 static_cast<unsigned char>(unbase64(static_cast<uint8_t>(src[i + 1]))),
75 static_cast<unsigned char>(unbase64(static_cast<uint8_t>(src[i + 2]))),
76 static_cast<unsigned char>(unbase64(static_cast<uint8_t>(src[i + 3]))),
77 };
78
79 const uint32_t v = ReadUint32BE(txt);
80 // If MSB is set, input contains whitespace or is not valid base64.
81 if (v & 0x80808080) {
82 if (!base64_decode_group_slow(dst, dstlen, src, srclen, &i, &k))
83 return k;
84 max_i = i + (srclen - i) / 4 * 4; // Align max_i again.
85 } else {
86 dst[k + 0] = ((v >> 22) & 0xFC) | ((v >> 20) & 0x03);
87 dst[k + 1] = ((v >> 12) & 0xF0) | ((v >> 10) & 0x0F);
88 dst[k + 2] = ((v >> 2) & 0xC0) | ((v >> 0) & 0x3F);
89 i += 4;
90 k += 3;
91 }
92 }
93 if (i < srclen && k < dstlen) {
94 base64_decode_group_slow(dst, dstlen, src, srclen, &i, &k);
95 }
96 return k;
97 }
98
99
100 template <typename TypeName>
base64_decoded_size(const TypeName * src,size_t size)101 size_t base64_decoded_size(const TypeName* src, size_t size) {
102 // 1-byte input cannot be decoded
103 if (size < 2)
104 return 0;
105
106 if (src[size - 1] == '=') {
107 size--;
108 if (src[size - 1] == '=')
109 size--;
110 }
111 return base64_decoded_size_fast(size);
112 }
113
114
115 template <typename TypeName>
base64_decode(char * const dst,const size_t dstlen,const TypeName * const src,const size_t srclen)116 size_t base64_decode(char* const dst, const size_t dstlen,
117 const TypeName* const src, const size_t srclen) {
118 const size_t decoded_size = base64_decoded_size(src, srclen);
119 return base64_decode_fast(dst, dstlen, src, srclen, decoded_size);
120 }
121
122
base64_encode(const char * src,size_t slen,char * dst,size_t dlen,Base64Mode mode)123 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 CHECK(dlen >= base64_encoded_size(slen, mode) &&
130 "not enough space provided for base64 encode");
131
132 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 const char* table = base64_select_table(mode);
142
143 i = 0;
144 k = 0;
145 n = slen / 3 * 3;
146
147 while (i < n) {
148 a = src[i + 0] & 0xff;
149 b = src[i + 1] & 0xff;
150 c = src[i + 2] & 0xff;
151
152 dst[k + 0] = table[a >> 2];
153 dst[k + 1] = table[((a & 3) << 4) | (b >> 4)];
154 dst[k + 2] = table[((b & 0x0f) << 2) | (c >> 6)];
155 dst[k + 3] = table[c & 0x3f];
156
157 i += 3;
158 k += 4;
159 }
160
161 switch (slen - n) {
162 case 1:
163 a = src[i + 0] & 0xff;
164 dst[k + 0] = table[a >> 2];
165 dst[k + 1] = table[(a & 3) << 4];
166 if (mode == Base64Mode::NORMAL) {
167 dst[k + 2] = '=';
168 dst[k + 3] = '=';
169 }
170 break;
171 case 2:
172 a = src[i + 0] & 0xff;
173 b = src[i + 1] & 0xff;
174 dst[k + 0] = table[a >> 2];
175 dst[k + 1] = table[((a & 3) << 4) | (b >> 4)];
176 dst[k + 2] = table[(b & 0x0f) << 2];
177 if (mode == Base64Mode::NORMAL)
178 dst[k + 3] = '=';
179 break;
180 }
181
182 return dlen;
183 }
184
185 } // namespace node
186
187 #endif // defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS
188
189 #endif // SRC_BASE64_INL_H_
190