diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/base64-inl.h | 85 | ||||
-rw-r--r-- | src/base64.h | 78 | ||||
-rw-r--r-- | src/inspector_socket.cc | 2 | ||||
-rw-r--r-- | src/string_bytes.cc | 2 |
4 files changed, 91 insertions, 76 deletions
diff --git a/src/base64-inl.h b/src/base64-inl.h index 9132faa614e..57c11bde2d7 100644 --- a/src/base64-inl.h +++ b/src/base64-inl.h @@ -3,6 +3,7 @@ #if defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS +#include "base64.h" #include "util.h" namespace node { @@ -91,6 +92,90 @@ size_t base64_decode_fast(char* const dst, const size_t dstlen, return k; } + +template <typename TypeName> +size_t base64_decoded_size(const TypeName* src, size_t size) { + // 1-byte input cannot be decoded + if (size < 2) + return 0; + + if (src[size - 1] == '=') { + size--; + if (src[size - 1] == '=') + size--; + } + return base64_decoded_size_fast(size); +} + + +template <typename TypeName> +size_t base64_decode(char* const dst, const size_t dstlen, + const TypeName* const src, const size_t srclen) { + const size_t decoded_size = base64_decoded_size(src, srclen); + return base64_decode_fast(dst, dstlen, src, srclen, decoded_size); +} + + +inline size_t base64_encode(const char* src, + size_t slen, + char* dst, + size_t dlen) { + // We know how much we'll write, just make sure that there's space. + CHECK(dlen >= base64_encoded_size(slen) && + "not enough space provided for base64 encode"); + + dlen = base64_encoded_size(slen); + + unsigned a; + unsigned b; + unsigned c; + unsigned i; + unsigned k; + unsigned n; + + static const char table[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ" + "abcdefghijklmnopqrstuvwxyz" + "0123456789+/"; + + i = 0; + k = 0; + n = slen / 3 * 3; + + while (i < n) { + a = src[i + 0] & 0xff; + b = src[i + 1] & 0xff; + c = src[i + 2] & 0xff; + + dst[k + 0] = table[a >> 2]; + dst[k + 1] = table[((a & 3) << 4) | (b >> 4)]; + dst[k + 2] = table[((b & 0x0f) << 2) | (c >> 6)]; + dst[k + 3] = table[c & 0x3f]; + + i += 3; + k += 4; + } + + switch (slen - n) { + case 1: + a = src[i + 0] & 0xff; + dst[k + 0] = table[a >> 2]; + dst[k + 1] = table[(a & 3) << 4]; + dst[k + 2] = '='; + dst[k + 3] = '='; + break; + case 2: + a = src[i + 0] & 0xff; + b = src[i + 1] & 0xff; + dst[k + 0] = table[a >> 2]; + dst[k + 1] = table[((a & 3) << 4) | (b >> 4)]; + dst[k + 2] = table[(b & 0x0f) << 2]; + dst[k + 3] = '='; + break; + } + + return dlen; +} + } // namespace node #endif // defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS diff --git a/src/base64.h b/src/base64.h index 24fc57e1d60..e58baa5f6e4 100644 --- a/src/base64.h +++ b/src/base64.h @@ -4,7 +4,6 @@ #if defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS #include "util.h" -#include "base64-inl.h" #include <cstddef> #include <cstdint> @@ -24,85 +23,16 @@ static inline constexpr size_t base64_decoded_size_fast(size_t size) { inline uint32_t ReadUint32BE(const unsigned char* p); template <typename TypeName> -size_t base64_decoded_size(const TypeName* src, size_t size) { - // 1-byte input cannot be decoded - if (size < 2) - return 0; - - if (src[size - 1] == '=') { - size--; - if (src[size - 1] == '=') - size--; - } - return base64_decoded_size_fast(size); -} +size_t base64_decoded_size(const TypeName* src, size_t size); template <typename TypeName> size_t base64_decode(char* const dst, const size_t dstlen, - const TypeName* const src, const size_t srclen) { - const size_t decoded_size = base64_decoded_size(src, srclen); - return base64_decode_fast(dst, dstlen, src, srclen, decoded_size); -} + const TypeName* const src, const size_t srclen); -static size_t base64_encode(const char* src, +inline size_t base64_encode(const char* src, size_t slen, char* dst, - size_t dlen) { - // We know how much we'll write, just make sure that there's space. - CHECK(dlen >= base64_encoded_size(slen) && - "not enough space provided for base64 encode"); - - dlen = base64_encoded_size(slen); - - unsigned a; - unsigned b; - unsigned c; - unsigned i; - unsigned k; - unsigned n; - - static const char table[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ" - "abcdefghijklmnopqrstuvwxyz" - "0123456789+/"; - - i = 0; - k = 0; - n = slen / 3 * 3; - - while (i < n) { - a = src[i + 0] & 0xff; - b = src[i + 1] & 0xff; - c = src[i + 2] & 0xff; - - dst[k + 0] = table[a >> 2]; - dst[k + 1] = table[((a & 3) << 4) | (b >> 4)]; - dst[k + 2] = table[((b & 0x0f) << 2) | (c >> 6)]; - dst[k + 3] = table[c & 0x3f]; - - i += 3; - k += 4; - } - - switch (slen - n) { - case 1: - a = src[i + 0] & 0xff; - dst[k + 0] = table[a >> 2]; - dst[k + 1] = table[(a & 3) << 4]; - dst[k + 2] = '='; - dst[k + 3] = '='; - break; - case 2: - a = src[i + 0] & 0xff; - b = src[i + 1] & 0xff; - dst[k + 0] = table[a >> 2]; - dst[k + 1] = table[((a & 3) << 4) | (b >> 4)]; - dst[k + 2] = table[(b & 0x0f) << 2]; - dst[k + 3] = '='; - break; - } - - return dlen; -} + size_t dlen); } // namespace node diff --git a/src/inspector_socket.cc b/src/inspector_socket.cc index ec347732404..35306af3c66 100644 --- a/src/inspector_socket.cc +++ b/src/inspector_socket.cc @@ -1,7 +1,7 @@ #include "inspector_socket.h" #include "llhttp.h" -#include "base64.h" +#include "base64-inl.h" #include "util-inl.h" #include "openssl/sha.h" // Sha-1 hash diff --git a/src/string_bytes.cc b/src/string_bytes.cc index 68c7c06d8df..b03268c49af 100644 --- a/src/string_bytes.cc +++ b/src/string_bytes.cc @@ -21,7 +21,7 @@ #include "string_bytes.h" -#include "base64.h" +#include "base64-inl.h" #include "env-inl.h" #include "node_buffer.h" #include "node_errors.h" |