From fdf0a84e826d3a9ec0ce6f5a3f5adc967fe99408 Mon Sep 17 00:00:00 2001 From: Anna Henningsen Date: Wed, 30 Sep 2020 18:09:08 +0200 Subject: [PATCH] src: move all base64.h inline methods into -inl.h header file Refs: https://github.com/nodejs/node/pull/34944#discussion_r480321239 PR-URL: https://github.com/nodejs/node/pull/35432 Reviewed-By: Richard Lau Reviewed-By: Joyee Cheung Reviewed-By: James M Snell Reviewed-By: Gireesh Punathil Reviewed-By: Rich Trott --- src/base64-inl.h | 85 ++++++++++++++++++++++++++++++++++++++ src/base64.h | 78 ++-------------------------------- src/inspector_socket.cc | 2 +- src/string_bytes.cc | 2 +- test/cctest/test_base64.cc | 2 +- 5 files changed, 92 insertions(+), 77 deletions(-) diff --git a/src/base64-inl.h b/src/base64-inl.h index 9132faa614ea2b..57c11bde2d7af2 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 +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 +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 24fc57e1d60385..e58baa5f6e4692 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 #include @@ -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 -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 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 ec347732404189..35306af3c660a2 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 68c7c06d8df1de..b03268c49afcbe 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" diff --git a/test/cctest/test_base64.cc b/test/cctest/test_base64.cc index be61618f9086ed..e92498da61b871 100644 --- a/test/cctest/test_base64.cc +++ b/test/cctest/test_base64.cc @@ -1,4 +1,4 @@ -#include "base64.h" +#include "base64-inl.h" #include #include