From 1770c8b9f3b95e649d8d33fa14acf64653c51949 Mon Sep 17 00:00:00 2001 From: Stephane Janel Date: Sat, 20 Apr 2024 20:40:55 +0200 Subject: [PATCH] [Code refactoring] - Base64 code clean up --- src/tech/include/base64.hpp | 39 ++++++++++++++++++++----------------- 1 file changed, 21 insertions(+), 18 deletions(-) diff --git a/src/tech/include/base64.hpp b/src/tech/include/base64.hpp index 688b1856..0ebbde20 100644 --- a/src/tech/include/base64.hpp +++ b/src/tech/include/base64.hpp @@ -2,6 +2,9 @@ #include #include +#include +#include +#include #include #include "cct_string.hpp" @@ -9,25 +12,25 @@ namespace cct { namespace details { -inline void B64Encode(std::span binData, char *out, char *endOut) { - int bitsCollected = 0; - unsigned int accumulator = 0; +inline void B64EncodeImpl(std::span binData, char *out, char *endOut) { + int bitsCollected{}; + uint32_t accumulator{}; - static constexpr const char *const kB64Table = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; + static constexpr const char kB64Table[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; + static constexpr auto kB64NbBits = 6; + static constexpr decltype(accumulator) kMask6 = (1U << kB64NbBits) - 1U; - for (char ch : binData) { - accumulator = (accumulator << 8) | (ch & 0xFFU); - bitsCollected += 8; - while (bitsCollected >= 6) { - bitsCollected -= 6; - *out = kB64Table[(accumulator >> bitsCollected) & 0x3FU]; - ++out; + for (uint8_t ch : binData) { + accumulator = (accumulator << CHAR_BIT) | ch; + bitsCollected += CHAR_BIT; + while (bitsCollected >= kB64NbBits) { + bitsCollected -= kB64NbBits; + *out++ = kB64Table[(accumulator >> bitsCollected) & kMask6]; } } if (bitsCollected > 0) { - accumulator <<= 6 - bitsCollected; - *out = kB64Table[accumulator & 0x3FU]; - ++out; + accumulator <<= kB64NbBits - bitsCollected; + *out++ = kB64Table[accumulator & kMask6]; } std::fill(out, endOut, '='); @@ -39,7 +42,7 @@ constexpr auto B64EncodedLen(auto binDataLen) { return static_cast( [[nodiscard]] inline string B64Encode(std::span binData) { string ret(details::B64EncodedLen(binData.size()), 0); - details::B64Encode(binData, ret.data(), ret.data() + ret.size()); + details::B64EncodeImpl(binData, ret.data(), ret.data() + ret.size()); return ret; } string B64Encode(const char *) = delete; @@ -47,18 +50,18 @@ string B64Encode(const char *) = delete; template [[nodiscard]] auto B64Encode(const char (&binData)[N]) { std::array ret; - details::B64Encode(binData, ret.data(), ret.data() + ret.size()); + details::B64EncodeImpl(binData, ret.data(), ret.data() + ret.size()); return ret; } template [[nodiscard]] auto B64Encode(const std::array &binData) { std::array ret; - details::B64Encode(binData, ret.data(), ret.data() + ret.size()); + details::B64EncodeImpl(binData, ret.data(), ret.data() + ret.size()); return ret; } [[nodiscard]] string B64Decode(std::span ascData); string B64Decode(const char *) = delete; -} // namespace cct \ No newline at end of file +} // namespace cct