The C and C++ Include Header Files
/usr/include/nodejs/src/base64.h
$ cat -n /usr/include/nodejs/src/base64.h 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
9 #include
10 11 namespace node { 12 //// Base 64 //// 13 static inline constexpr size_t base64_encoded_size(size_t size) { 14 return ((size + 2) / 3 * 4); 15 } 16 17 // Doesn't check for padding at the end. Can be 1-2 bytes over. 18 static inline constexpr size_t base64_decoded_size_fast(size_t size) { 19 // 1-byte input cannot be decoded 20 return size > 1 ? (size / 4) * 3 + (size % 4 + 1) / 2 : 0; 21 } 22 23 template
24 size_t base64_decoded_size(const TypeName* src, size_t size) { 25 // 1-byte input cannot be decoded 26 if (size < 2) 27 return 0; 28 29 if (src[size - 1] == '=') { 30 size--; 31 if (src[size - 1] == '=') 32 size--; 33 } 34 return base64_decoded_size_fast(size); 35 } 36 37 38 extern const int8_t unbase64_table[256]; 39 40 41 inline static int8_t unbase64(uint8_t x) { 42 return unbase64_table[x]; 43 } 44 45 46 template
47 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 V(/* Nothing. */); 69 V(dst[(*k)++] = ((hi & 0x3F) << 2) | ((lo & 0x30) >> 4)); 70 V(dst[(*k)++] = ((hi & 0x0F) << 4) | ((lo & 0x3C) >> 2)); 71 V(dst[(*k)++] = ((hi & 0x03) << 6) | ((lo & 0x3F) >> 0)); 72 #undef V 73 return true; // Continue decoding. 74 } 75 76 77 template
78 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 const size_t available = dstlen < decoded_size ? dstlen : decoded_size; 82 const size_t max_k = available / 3 * 3; 83 size_t max_i = srclen / 4 * 4; 84 size_t i = 0; 85 size_t k = 0; 86 while (i < max_i && k < max_k) { 87 const uint32_t v = 88 unbase64(src[i + 0]) << 24 | 89 unbase64(src[i + 1]) << 16 | 90 unbase64(src[i + 2]) << 8 | 91 unbase64(src[i + 3]); 92 // If MSB is set, input contains whitespace or is not valid base64. 93 if (v & 0x80808080) { 94 if (!base64_decode_group_slow(dst, dstlen, src, srclen, &i, &k)) 95 return k; 96 max_i = i + (srclen - i) / 4 * 4; // Align max_i again. 97 } else { 98 dst[k + 0] = ((v >> 22) & 0xFC) | ((v >> 20) & 0x03); 99 dst[k + 1] = ((v >> 12) & 0xF0) | ((v >> 10) & 0x0F); 100 dst[k + 2] = ((v >> 2) & 0xC0) | ((v >> 0) & 0x3F); 101 i += 4; 102 k += 3; 103 } 104 } 105 if (i < srclen && k < dstlen) { 106 base64_decode_group_slow(dst, dstlen, src, srclen, &i, &k); 107 } 108 return k; 109 } 110 111 112 template
113 size_t base64_decode(char* const dst, const size_t dstlen, 114 const TypeName* const src, const size_t srclen) { 115 const size_t decoded_size = base64_decoded_size(src, srclen); 116 return base64_decode_fast(dst, dstlen, src, srclen, decoded_size); 117 } 118 119 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 CHECK(dlen >= base64_encoded_size(slen) && 125 "not enough space provided for base64 encode"); 126 127 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 i = 0; 141 k = 0; 142 n = slen / 3 * 3; 143 144 while (i < n) { 145 a = src[i + 0] & 0xff; 146 b = src[i + 1] & 0xff; 147 c = src[i + 2] & 0xff; 148 149 dst[k + 0] = table[a >> 2]; 150 dst[k + 1] = table[((a & 3) << 4) | (b >> 4)]; 151 dst[k + 2] = table[((b & 0x0f) << 2) | (c >> 6)]; 152 dst[k + 3] = table[c & 0x3f]; 153 154 i += 3; 155 k += 4; 156 } 157 158 switch (slen - n) { 159 case 1: 160 a = src[i + 0] & 0xff; 161 dst[k + 0] = table[a >> 2]; 162 dst[k + 1] = table[(a & 3) << 4]; 163 dst[k + 2] = '='; 164 dst[k + 3] = '='; 165 break; 166 case 2: 167 a = src[i + 0] & 0xff; 168 b = src[i + 1] & 0xff; 169 dst[k + 0] = table[a >> 2]; 170 dst[k + 1] = table[((a & 3) << 4) | (b >> 4)]; 171 dst[k + 2] = table[(b & 0x0f) << 2]; 172 dst[k + 3] = '='; 173 break; 174 } 175 176 return dlen; 177 } 178 } // namespace node 179 180 181 #endif // defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS 182 183 #endif // SRC_BASE64_H_
Contact us
|
About us
|
Term of use
|
Copyright © 2000-2024 MyWebUniversity.com ™