• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "libbase64.h"
8 #include "util.h"
9 
10 namespace node {
11 
12 static constexpr char base64_table_url[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
13                                            "abcdefghijklmnopqrstuvwxyz"
14                                            "0123456789-_";
15 
16 extern const int8_t unbase64_table[256];
17 
18 
unbase64(uint8_t x)19 inline static int8_t unbase64(uint8_t x) {
20   return unbase64_table[x];
21 }
22 
23 
ReadUint32BE(const unsigned char * p)24 inline uint32_t ReadUint32BE(const unsigned char* p) {
25   return static_cast<uint32_t>(p[0] << 24U) |
26          static_cast<uint32_t>(p[1] << 16U) |
27          static_cast<uint32_t>(p[2] << 8U) |
28          static_cast<uint32_t>(p[3]);
29 }
30 
31 #ifdef _MSC_VER
32 #pragma warning(push)
33 // MSVC C4003: not enough actual parameters for macro 'identifier'
34 #pragma warning(disable : 4003)
35 #endif
36 
37 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)38 bool base64_decode_group_slow(char* const dst, const size_t dstlen,
39                               const TypeName* const src, const size_t srclen,
40                               size_t* const i, size_t* const k) {
41   uint8_t hi;
42   uint8_t lo;
43 #define V(expr)                                                                \
44   for (;;) {                                                                   \
45     const uint8_t c = static_cast<uint8_t>(src[*i]);                           \
46     lo = unbase64(c);                                                          \
47     *i += 1;                                                                   \
48     if (lo < 64) break;                         /* Legal character. */         \
49     if (c == '=' || *i >= srclen) return false; /* Stop decoding. */           \
50   }                                                                            \
51   expr;                                                                        \
52   if (*i >= srclen) return false;                                              \
53   if (*k >= dstlen) return false;                                              \
54   hi = lo;
55   V(/* Nothing. */);
56   V(dst[(*k)++] = ((hi & 0x3F) << 2) | ((lo & 0x30) >> 4));
57   V(dst[(*k)++] = ((hi & 0x0F) << 4) | ((lo & 0x3C) >> 2));
58   V(dst[(*k)++] = ((hi & 0x03) << 6) | ((lo & 0x3F) >> 0));
59 #undef V
60   return true;  // Continue decoding.
61 }
62 
63 #ifdef _MSC_VER
64 #pragma warning(pop)
65 #endif
66 
67 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)68 size_t base64_decode_fast(char* const dst, const size_t dstlen,
69                           const TypeName* const src, const size_t srclen,
70                           const size_t decoded_size) {
71   const size_t available = dstlen < decoded_size ? dstlen : decoded_size;
72   const size_t max_k = available / 3 * 3;
73   size_t max_i = srclen / 4 * 4;
74   size_t i = 0;
75   size_t k = 0;
76   while (i < max_i && k < max_k) {
77     const unsigned char txt[] = {
78         static_cast<unsigned char>(unbase64(static_cast<uint8_t>(src[i + 0]))),
79         static_cast<unsigned char>(unbase64(static_cast<uint8_t>(src[i + 1]))),
80         static_cast<unsigned char>(unbase64(static_cast<uint8_t>(src[i + 2]))),
81         static_cast<unsigned char>(unbase64(static_cast<uint8_t>(src[i + 3]))),
82     };
83 
84     const uint32_t v = ReadUint32BE(txt);
85     // If MSB is set, input contains whitespace or is not valid base64.
86     if (v & 0x80808080) {
87       if (!base64_decode_group_slow(dst, dstlen, src, srclen, &i, &k))
88         return k;
89       max_i = i + (srclen - i) / 4 * 4;  // Align max_i again.
90     } else {
91       dst[k + 0] = ((v >> 22) & 0xFC) | ((v >> 20) & 0x03);
92       dst[k + 1] = ((v >> 12) & 0xF0) | ((v >> 10) & 0x0F);
93       dst[k + 2] = ((v >>  2) & 0xC0) | ((v >>  0) & 0x3F);
94       i += 4;
95       k += 3;
96     }
97   }
98   if (i < srclen && k < dstlen) {
99     base64_decode_group_slow(dst, dstlen, src, srclen, &i, &k);
100   }
101   return k;
102 }
103 
104 
105 template <typename TypeName>
base64_decoded_size(const TypeName * src,size_t size)106 size_t base64_decoded_size(const TypeName* src, size_t size) {
107   // 1-byte input cannot be decoded
108   if (size < 2)
109     return 0;
110 
111   if (src[size - 1] == '=') {
112     size--;
113     if (src[size - 1] == '=')
114       size--;
115   }
116   return base64_decoded_size_fast(size);
117 }
118 
119 
120 template <typename TypeName>
base64_decode(char * const dst,const size_t dstlen,const TypeName * const src,const size_t srclen)121 size_t base64_decode(char* const dst, const size_t dstlen,
122                      const TypeName* const src, const size_t srclen) {
123   const size_t decoded_size = base64_decoded_size(src, srclen);
124   return base64_decode_fast(dst, dstlen, src, srclen, decoded_size);
125 }
126 
127 
base64_encode(const char * src,size_t slen,char * dst,size_t dlen,Base64Mode mode)128 inline size_t base64_encode(const char* src,
129                             size_t slen,
130                             char* dst,
131                             size_t dlen,
132                             Base64Mode mode) {
133   // We know how much we'll write, just make sure that there's space.
134   CHECK(dlen >= base64_encoded_size(slen, mode) &&
135         "not enough space provided for base64 encode");
136 
137   dlen = base64_encoded_size(slen, mode);
138 
139   if (mode == Base64Mode::NORMAL) {
140     ::base64_encode(src, slen, dst, &dlen, 0);
141     return dlen;
142   }
143 
144   unsigned a;
145   unsigned b;
146   unsigned c;
147   unsigned i;
148   unsigned k;
149   unsigned n;
150 
151   const char* table = base64_table_url;
152 
153   i = 0;
154   k = 0;
155   n = slen / 3 * 3;
156 
157   while (i < n) {
158     a = src[i + 0] & 0xff;
159     b = src[i + 1] & 0xff;
160     c = src[i + 2] & 0xff;
161 
162     dst[k + 0] = table[a >> 2];
163     dst[k + 1] = table[((a & 3) << 4) | (b >> 4)];
164     dst[k + 2] = table[((b & 0x0f) << 2) | (c >> 6)];
165     dst[k + 3] = table[c & 0x3f];
166 
167     i += 3;
168     k += 4;
169   }
170 
171   switch (slen - n) {
172     case 1:
173       a = src[i + 0] & 0xff;
174       dst[k + 0] = table[a >> 2];
175       dst[k + 1] = table[(a & 3) << 4];
176       break;
177     case 2:
178       a = src[i + 0] & 0xff;
179       b = src[i + 1] & 0xff;
180       dst[k + 0] = table[a >> 2];
181       dst[k + 1] = table[((a & 3) << 4) | (b >> 4)];
182       dst[k + 2] = table[(b & 0x0f) << 2];
183       break;
184   }
185 
186   return dlen;
187 }
188 
189 }  // namespace node
190 
191 #endif  // defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS
192 
193 #endif  // SRC_BASE64_INL_H_
194