diff --git a/velox/common/encode/Base64.cpp b/velox/common/encode/Base64.cpp index da4e9cdbfcfdd..6ee390de80a51 100644 --- a/velox/common/encode/Base64.cpp +++ b/velox/common/encode/Base64.cpp @@ -89,11 +89,11 @@ constexpr const Base64::ReverseIndex kBase64UrlReverseIndexTable = { // Validate the character in charset with ReverseIndex table constexpr bool checkForwardIndex( - uint8_t idx, + uint8_t index, const Base64::Charset& charset, const Base64::ReverseIndex& reverseIndex) { - return (reverseIndex[static_cast(charset[idx])] == idx) && - (idx > 0 ? checkForwardIndex(idx - 1, charset, reverseIndex) : true); + return (reverseIndex[static_cast(charset[index])] == index) && + (index > 0 ? checkForwardIndex(index - 1, charset, reverseIndex) : true); } // Verify that for every entry in kBase64Charset, the corresponding entry @@ -117,22 +117,23 @@ static_assert( // Searches for a character within a charset up to a certain index. constexpr bool findCharacterInCharset( const Base64::Charset& charset, - uint8_t idx, - const char c) { - return idx < charset.size() && - ((charset[idx] == c) || findCharacterInCharset(charset, idx + 1, c)); + uint8_t index, + const char targetChar) { + return index < charset.size() && + ((charset[index] == targetChar) || + findCharacterInCharset(charset, index + 1, targetChar)); } // Checks the consistency of a reverse index mapping for a given character // set. constexpr bool checkReverseIndex( - uint8_t idx, + uint8_t index, const Base64::Charset& charset, const Base64::ReverseIndex& reverseIndex) { - return (reverseIndex[idx] == 255 - ? !findCharacterInCharset(charset, 0, static_cast(idx)) - : (charset[reverseIndex[idx]] == idx)) && - (idx > 0 ? checkReverseIndex(idx - 1, charset, reverseIndex) : true); + return (reverseIndex[index] == 255 + ? !findCharacterInCharset(charset, 0, static_cast(index)) + : (charset[reverseIndex[index]] == index)) && + (index > 0 ? checkReverseIndex(index - 1, charset, reverseIndex) : true); } // Verify that for every entry in kBase64ReverseIndexTable, the corresponding @@ -156,88 +157,92 @@ static_assert( // "kBase64UrlReverseIndexTable has incorrect entries."); // Implementation of Base64 encoding and decoding functions. +// static template -/* static */ std::string Base64::encodeImpl( - const T& data, - const Base64::Charset& charset, - bool include_pad) { - size_t outlen = calculateEncodedSize(data.size(), include_pad); - std::string out; - out.resize(outlen); - encodeImpl(data, charset, include_pad, out.data()); - return out; +std::string Base64::encodeImpl( + const T& input, + const Charset& charset, + bool includePadding) { + size_t encodedSize = calculateEncodedSize(input.size(), includePadding); + std::string encodedResult; + encodedResult.resize(encodedSize); + encodeImpl(input, charset, includePadding, encodedResult.data()); + return encodedResult; } // static -size_t Base64::calculateEncodedSize(size_t size, bool withPadding) { - if (size == 0) { +size_t Base64::calculateEncodedSize(size_t inputSize, bool withPadding) { + if (inputSize == 0) { return 0; } // Calculate the output size assuming that we are including padding. - size_t encodedSize = ((size + 2) / 3) * 4; + size_t encodedSize = ((inputSize + 2) / 3) * 4; if (!withPadding) { // If the padding was not requested, subtract the padding bytes. - encodedSize -= (3 - (size % 3)) % 3; + encodedSize -= (3 - (inputSize % 3)) % 3; } return encodedSize; } // static -void Base64::encode(const char* data, size_t len, char* output) { - encodeImpl(folly::StringPiece(data, len), kBase64Charset, true, output); +void Base64::encode(const char* input, size_t inputSize, char* output) { + encodeImpl( + folly::StringPiece(input, inputSize), kBase64Charset, true, output); } // static -void Base64::encodeUrl(const char* data, size_t len, char* output) { - encodeImpl(folly::StringPiece(data, len), kBase64UrlCharset, true, output); +void Base64::encodeUrl(const char* input, size_t inputSize, char* output) { + encodeImpl( + folly::StringPiece(input, inputSize), kBase64UrlCharset, true, output); } +// static template -/* static */ void Base64::encodeImpl( - const T& data, - const Base64::Charset& charset, - bool include_pad, - char* out) { - auto len = data.size(); - if (len == 0) { +void Base64::encodeImpl( + const T& input, + const Charset& charset, + bool includePadding, + char* outputBuffer) { + auto inputSize = input.size(); + if (inputSize == 0) { return; } - auto wp = out; - auto it = data.begin(); + auto outputPointer = outputBuffer; + auto inputIterator = input.begin(); // For each group of 3 bytes (24 bits) in the input, split that into // 4 groups of 6 bits and encode that using the supplied charset lookup - for (; len > 2; len -= 3) { - uint32_t curr = uint8_t(*it++) << 16; - curr |= uint8_t(*it++) << 8; - curr |= uint8_t(*it++); - - *wp++ = charset[(curr >> 18) & 0x3f]; - *wp++ = charset[(curr >> 12) & 0x3f]; - *wp++ = charset[(curr >> 6) & 0x3f]; - *wp++ = charset[curr & 0x3f]; + for (; inputSize > 2; inputSize -= 3) { + uint32_t inputBlock = uint8_t(*inputIterator++) << 16; + inputBlock |= uint8_t(*inputIterator++) << 8; + inputBlock |= uint8_t(*inputIterator++); + + *outputPointer++ = charset[(inputBlock >> 18) & 0x3f]; + *outputPointer++ = charset[(inputBlock >> 12) & 0x3f]; + *outputPointer++ = charset[(inputBlock >> 6) & 0x3f]; + *outputPointer++ = charset[inputBlock & 0x3f]; } - if (len > 0) { + if (inputSize > 0) { // We have either 1 or 2 input bytes left. Encode this similar to the // above (assuming 0 for all other bytes). Optionally append the '=' // character if it is requested. - uint32_t curr = uint8_t(*it++) << 16; - *wp++ = charset[(curr >> 18) & 0x3f]; - if (len > 1) { - curr |= uint8_t(*it) << 8; - *wp++ = charset[(curr >> 12) & 0x3f]; - *wp++ = charset[(curr >> 6) & 0x3f]; - if (include_pad) { - *wp = kPadding; + uint32_t inputBlock = uint8_t(*inputIterator++) << 16; + *outputPointer++ = charset[(inputBlock >> 18) & 0x3f]; + if (inputSize > 1) { + inputBlock |= uint8_t(*inputIterator) << 8; + *outputPointer++ = charset[(inputBlock >> 12) & 0x3f]; + *outputPointer++ = charset[(inputBlock >> 6) & 0x3f]; + if (includePadding) { + *outputPointer = kPadding; } } else { - *wp++ = charset[(curr >> 12) & 0x3f]; - if (include_pad) { - *wp++ = kPadding; - *wp = kPadding; + *outputPointer++ = charset[(inputBlock >> 12) & 0x3f]; + if (includePadding) { + *outputPointer++ = kPadding; + *outputPointer = kPadding; } } } @@ -249,191 +254,198 @@ std::string Base64::encode(folly::StringPiece text) { } // static -std::string Base64::encode(const char* data, size_t len) { - return encode(folly::StringPiece(data, len)); +std::string Base64::encode(const char* input, size_t inputSize) { + return encode(folly::StringPiece(input, inputSize)); } namespace { /** - * this is a quick and dirty iterator implementation for an IOBuf so that the - * template that uses iterators can work on IOBuf chains. It only implements - * postfix increment because that is all the algorithm needs, and it is a noop - * since the read<>() function already incremented the cursor. + * This is a quick and simple iterator implementation for an IOBuf so that the + * template that uses iterators can work on IOBuf chains. It only implements + * postfix increment because that is all the algorithm needs, and it is a no-op + * since the read<>() function already increments the cursor. */ class IOBufWrapper { private: class Iterator { public: - explicit Iterator(const folly::IOBuf* data) : cs_(data) {} + explicit Iterator(const folly::IOBuf* inputBuffer) : cursor_(inputBuffer) {} Iterator& operator++(int32_t) { - // This is a noop since reading from the Cursor has already moved the - // position + // This is a no-op since reading from the Cursor has already moved the + // position. return *this; } uint8_t operator*() { - // This will read _and_ increment - return cs_.read(); + // This will read _and_ increment the cursor. + return cursor_.read(); } private: - folly::io::Cursor cs_; + folly::io::Cursor cursor_; }; public: - explicit IOBufWrapper(const folly::IOBuf* data) : data_(data) {} - + explicit IOBufWrapper(const folly::IOBuf* inputBuffer) + : input_(inputBuffer) {} size_t size() const { - return data_->computeChainDataLength(); + return input_->computeChainDataLength(); } Iterator begin() const { - return Iterator(data_); + return Iterator(input_); } private: - const folly::IOBuf* data_; + const folly::IOBuf* input_; }; } // namespace // static -std::string Base64::encode(const folly::IOBuf* data) { - return encodeImpl(IOBufWrapper(data), kBase64Charset, true); +std::string Base64::encode(const folly::IOBuf* inputBuffer) { + return encodeImpl(IOBufWrapper(inputBuffer), kBase64Charset, true); } // static -std::string Base64::decode(folly::StringPiece encoded) { - std::string output; - Base64::decode(std::make_pair(encoded.data(), encoded.size()), output); - return output; +std::string Base64::decode(folly::StringPiece encodedText) { + std::string decodedResult; + Base64::decode( + std::make_pair(encodedText.data(), encodedText.size()), decodedResult); + return decodedResult; } // static void Base64::decode( const std::pair& payload, - std::string& output) { + std::string& decodedOutput) { size_t inputSize = payload.second; - output.resize(calculateDecodedSize(payload.first, inputSize)); - decode(payload.first, inputSize, output.data(), output.size()); + decodedOutput.resize(calculateDecodedSize(payload.first, inputSize)); + decode(payload.first, inputSize, decodedOutput.data(), decodedOutput.size()); } // static -void Base64::decode(const char* data, size_t size, char* output) { - size_t out_len = size / 4 * 3; - Base64::decode(data, size, output, out_len); +void Base64::decode(const char* input, size_t size, char* output) { + size_t expectedOutputSize = size / 4 * 3; + Base64::decode(input, size, output, expectedOutputSize); } // static uint8_t Base64::base64ReverseLookup( - char p, + char encodedChar, const Base64::ReverseIndex& reverseIndex) { - auto curr = reverseIndex[(uint8_t)p]; - if (curr >= 0x40) { + auto reverseLookupValue = reverseIndex[(uint8_t)encodedChar]; + if (reverseLookupValue >= 0x40) { VELOX_USER_FAIL("decode() - invalid input string: invalid characters"); } - return curr; + return reverseLookupValue; } // static -size_t -Base64::decode(const char* src, size_t src_len, char* dst, size_t dst_len) { - return decodeImpl(src, src_len, dst, dst_len, kBase64ReverseIndexTable); +size_t Base64::decode( + const char* input, + size_t inputSize, + char* output, + size_t outputSize) { + return decodeImpl( + input, inputSize, output, outputSize, kBase64ReverseIndexTable); } // static -size_t Base64::calculateDecodedSize(const char* data, size_t& size) { - if (size == 0) { +size_t Base64::calculateDecodedSize(const char* input, size_t& inputSize) { + if (inputSize == 0) { return 0; } - // Check if the input data is padded - if (isPadded(data, size)) { + // Check if the input string is padded + if (isPadded(input, inputSize)) { // If padded, ensure that the string length is a multiple of the encoded // block size - if (size % kEncodedBlockByteSize != 0) { + if (inputSize % kEncodedBlockByteSize != 0) { VELOX_USER_FAIL( "Base64::decode() - invalid input string: " "string length is not a multiple of 4."); } - auto needed = (size * kBinaryBlockByteSize) / kEncodedBlockByteSize; - auto padding = numPadding(data, size); - size -= padding; + auto decodedSize = + (inputSize * kBinaryBlockByteSize) / kEncodedBlockByteSize; + auto paddingCount = numPadding(input, inputSize); + inputSize -= paddingCount; // Adjust the needed size by deducting the bytes corresponding to the // padding from the calculated size. - return needed - - ((padding * kBinaryBlockByteSize) + (kEncodedBlockByteSize - 1)) / + return decodedSize - + ((paddingCount * kBinaryBlockByteSize) + (kEncodedBlockByteSize - 1)) / kEncodedBlockByteSize; } // If not padded, Calculate extra bytes, if any - auto extra = size % kEncodedBlockByteSize; - auto needed = (size / kEncodedBlockByteSize) * kBinaryBlockByteSize; + auto extraBytes = inputSize % kEncodedBlockByteSize; + auto decodedSize = (inputSize / kEncodedBlockByteSize) * kBinaryBlockByteSize; // Adjust the needed size for extra bytes, if present - if (extra) { - if (extra == 1) { + if (extraBytes) { + if (extraBytes == 1) { VELOX_USER_FAIL( "Base64::decode() - invalid input string: " "string length cannot be 1 more than a multiple of 4."); } - needed += (extra * kBinaryBlockByteSize) / kEncodedBlockByteSize; + decodedSize += (extraBytes * kBinaryBlockByteSize) / kEncodedBlockByteSize; } - return needed; + return decodedSize; } // static size_t Base64::decodeImpl( - const char* src, - size_t src_len, - char* dst, - size_t dst_len, - const Base64::ReverseIndex& reverseIndex) { - if (!src_len) { + const char* input, + size_t inputSize, + char* outputBuffer, + size_t outputSize, + const ReverseIndex& reverseIndex) { + if (!inputSize) { return 0; } - auto needed = calculateDecodedSize(src, src_len); - if (dst_len < needed) { + auto decodedSize = calculateDecodedSize(input, inputSize); + if (outputSize < decodedSize) { VELOX_USER_FAIL( "Base64::decode() - invalid output string: " "output string is too small."); } // Handle full groups of 4 characters - for (; src_len > 4; src_len -= 4, src += 4, dst += 3) { - // Each character of the 4 encode 6 bits of the original, grab each with + for (; inputSize > 4; inputSize -= 4, input += 4, outputBuffer += 3) { + // Each character of the 4 encodes 6 bits of the original, grab each with // the appropriate shifts to rebuild the original and then split that back - // into the original 8 bit bytes. - uint32_t last = (base64ReverseLookup(src[0], reverseIndex) << 18) | - (base64ReverseLookup(src[1], reverseIndex) << 12) | - (base64ReverseLookup(src[2], reverseIndex) << 6) | - base64ReverseLookup(src[3], reverseIndex); - dst[0] = (last >> 16) & 0xff; - dst[1] = (last >> 8) & 0xff; - dst[2] = last & 0xff; + // into the original 8-bit bytes. + uint32_t decodedBlock = + (base64ReverseLookup(input[0], reverseIndex) << 18) | + (base64ReverseLookup(input[1], reverseIndex) << 12) | + (base64ReverseLookup(input[2], reverseIndex) << 6) | + base64ReverseLookup(input[3], reverseIndex); + outputBuffer[0] = (decodedBlock >> 16) & 0xff; + outputBuffer[1] = (decodedBlock >> 8) & 0xff; + outputBuffer[2] = decodedBlock & 0xff; } - // Handle the last 2-4 characters. This is similar to the above, but the + // Handle the last 2-4 characters. This is similar to the above, but the // last 2 characters may or may not exist. - DCHECK(src_len >= 2); - uint32_t last = (base64ReverseLookup(src[0], reverseIndex) << 18) | - (base64ReverseLookup(src[1], reverseIndex) << 12); - dst[0] = (last >> 16) & 0xff; - if (src_len > 2) { - last |= base64ReverseLookup(src[2], reverseIndex) << 6; - dst[1] = (last >> 8) & 0xff; - if (src_len > 3) { - last |= base64ReverseLookup(src[3], reverseIndex); - dst[2] = last & 0xff; + DCHECK(inputSize >= 2); + uint32_t decodedBlock = (base64ReverseLookup(input[0], reverseIndex) << 18) | + (base64ReverseLookup(input[1], reverseIndex) << 12); + outputBuffer[0] = (decodedBlock >> 16) & 0xff; + if (inputSize > 2) { + decodedBlock |= base64ReverseLookup(input[2], reverseIndex) << 6; + outputBuffer[1] = (decodedBlock >> 8) & 0xff; + if (inputSize > 3) { + decodedBlock |= base64ReverseLookup(input[3], reverseIndex); + outputBuffer[2] = decodedBlock & 0xff; } } - return needed; + return decodedSize; } // static @@ -442,44 +454,46 @@ std::string Base64::encodeUrl(folly::StringPiece text) { } // static -std::string Base64::encodeUrl(const char* data, size_t len) { - return encodeUrl(folly::StringPiece(data, len)); +std::string Base64::encodeUrl(const char* input, size_t inputSize) { + return encodeUrl(folly::StringPiece(input, inputSize)); } // static -std::string Base64::encodeUrl(const folly::IOBuf* data) { - return encodeImpl(IOBufWrapper(data), kBase64UrlCharset, false); +std::string Base64::encodeUrl(const folly::IOBuf* inputBuffer) { + return encodeImpl(IOBufWrapper(inputBuffer), kBase64UrlCharset, false); } // static void Base64::decodeUrl( - const char* src, - size_t src_len, - char* dst, - size_t dst_len) { - decodeImpl(src, src_len, dst, dst_len, kBase64UrlReverseIndexTable); + const char* input, + size_t inputSize, + char* outputBuffer, + size_t outputSize) { + decodeImpl( + input, inputSize, outputBuffer, outputSize, kBase64UrlReverseIndexTable); } // static -std::string Base64::decodeUrl(folly::StringPiece encoded) { - std::string output; - Base64::decodeUrl(std::make_pair(encoded.data(), encoded.size()), output); - return output; +std::string Base64::decodeUrl(folly::StringPiece encodedText) { + std::string decodedOutput; + Base64::decodeUrl( + std::make_pair(encodedText.data(), encodedText.size()), decodedOutput); + return decodedOutput; } // static void Base64::decodeUrl( const std::pair& payload, - std::string& output) { - size_t out_len = (payload.second + 3) / 4 * 3; - output.resize(out_len, '\0'); - out_len = Base64::decodeImpl( + std::string& decodedOutput) { + size_t decodedSize = (payload.second + 3) / 4 * 3; + decodedOutput.resize(decodedSize, '\0'); + decodedSize = Base64::decodeImpl( payload.first, payload.second, - &output[0], - out_len, + &decodedOutput[0], + decodedSize, kBase64UrlReverseIndexTable); - output.resize(out_len); + decodedOutput.resize(decodedSize); } } // namespace facebook::velox::encoding diff --git a/velox/common/encode/Base64.h b/velox/common/encode/Base64.h index 13004175379a6..9eac6899c0a06 100644 --- a/velox/common/encode/Base64.h +++ b/velox/common/encode/Base64.h @@ -13,16 +13,13 @@ * See the License for the specific language governing permissions and * limitations under the License. */ + #pragma once #include -#include -#include #include - #include #include - #include "velox/common/base/GTestMacros.h" namespace facebook::velox::encoding { @@ -32,125 +29,125 @@ class Base64 { static const size_t kCharsetSize = 64; static const size_t kReverseIndexSize = 256; - /// Character set used for encoding purposes. + /// Character set used for Base64 encoding. /// Contains specific characters that form the encoding scheme. using Charset = std::array; - /// Reverse lookup table for decoding purposes. + /// Reverse lookup table for decoding. /// Maps each possible encoded character to its corresponding numeric value /// within the encoding base. using ReverseIndex = std::array; - /// Padding character used in encoding. - static const char kPadding = '='; - - /// Encodes the specified number of characters from the 'data'. - static std::string encode(const char* data, size_t len); - + /// Encodes the specified number of characters from the 'input'. + static std::string encode(const char* input, size_t inputSize); /// Encodes the specified text. static std::string encode(folly::StringPiece text); - /// Encodes the specified IOBuf data. - static std::string encode(const folly::IOBuf* text); - - /// Returns encoded size for the input of the specified size. - static size_t calculateEncodedSize(size_t size, bool withPadding = true); - - /// Encodes the specified number of characters from the 'data' and writes the - /// result to the 'output'. The output must have enough space, e.g. as + static std::string encode(const folly::IOBuf* inputBuffer); + /// Encodes the specified number of characters from the 'input' and writes the + /// result to the 'outputBuffer'. The output must have enough space as /// returned by the calculateEncodedSize(). - static void encode(const char* data, size_t size, char* output); - - /// Decodes the specified encoded text. - static std::string decode(folly::StringPiece encoded); - - /// Returns the actual size of the decoded data. Will also remove the padding - /// length from the input data 'size'. - static size_t calculateDecodedSize(const char* data, size_t& size); - - /// Decodes the specified number of characters from the 'data' and writes the - /// result to the 'output'. The output must have enough space, e.g. as - /// returned by the calculateDecodedSize(). - static void decode(const char* data, size_t size, char* output); - - static void decode( - const std::pair& payload, - std::string& output); + static void encode(const char* input, size_t inputSize, char* outputBuffer); - /// Encodes the specified number of characters from the 'data' and writes the - /// result to the 'output' using URL encoding. The output must have enough - /// space as returned by the calculateEncodedSize(). - static void encodeUrl(const char* data, size_t size, char* output); - - /// Encodes the specified number of characters from the 'data' using URL + /// Encodes the specified number of characters from the 'input' using URL /// encoding. - static std::string encodeUrl(const char* data, size_t len); - - /// Encodes the specified IOBuf data using URL encoding. - static std::string encodeUrl(const folly::IOBuf* data); - + static std::string encodeUrl(const char* input, size_t inputSize); /// Encodes the specified text using URL encoding. static std::string encodeUrl(folly::StringPiece text); + /// Encodes the specified IOBuf data using URL encoding. + static std::string encodeUrl(const folly::IOBuf* inputBuffer); + /// Encodes the specified number of characters from the 'input' and writes the + /// result to the 'outputBuffer' using URL encoding. The output must have + /// enough space as returned by the calculateEncodedSize(). + static void + encodeUrl(const char* input, size_t inputSize, char* outputBuffer); + /// Decodes the input Base64 encoded string. + static std::string decode(folly::StringPiece encodedText); + /// Decodes the specified encoded payload and writes the result to the + /// 'output'. + static void decode( + const std::pair& payload, + std::string& output); + /// Decodes the specified number of characters from the 'data' and writes the + /// result to the 'output'. The output must have enough space as + /// returned by the calculateDecodedSize(). + static void decode(const char* input, size_t inputSize, char* outputBuffer); + /// Decodes the specified number of characters from the 'input' and writes the + /// result to the 'outputBuffer'. + static size_t decode( + const char* input, + size_t inputSize, + char* outputBuffer, + size_t outputSize); + + /// Decodes the input Base64 URL encoded string. + static std::string decodeUrl(folly::StringPiece encodedText); /// Decodes the specified URL encoded payload and writes the result to the /// 'output'. static void decodeUrl( const std::pair& payload, std::string& output); + /// Decodes the specified number of characters from the 'input' using URL + /// encoding and writes the result to the 'outputBuffer' + static void decodeUrl( + const char* input, + size_t inputSize, + char* outputBuffer, + size_t outputSize); - /// Decodes the specified URL encoded text. - static std::string decodeUrl(folly::StringPiece text); - - /// Decodes the specified number of characters from the 'src' and writes the - /// result to the 'dst'. - static size_t - decode(const char* src, size_t src_len, char* dst, size_t dst_len); + /// Calculates the encoded size based on input size. + static size_t calculateEncodedSize(size_t inputSize, bool withPadding = true); - /// Decodes the specified number of characters from the 'src' using URL - /// encoding and writes the result to the 'dst'. - static void - decodeUrl(const char* src, size_t src_len, char* dst, size_t dst_len); + /// Calculates the decoded size based on encoded input and adjusts the input + /// size for padding. + static size_t calculateDecodedSize(const char* input, size_t& inputSize); private: - /// Checks if there is padding in encoded data. - static inline bool isPadded(const char* data, size_t len) { - return (len > 0 && data[len - 1] == kPadding); + // Padding character used in encoding. + static const char kPadding = '='; + + // Checks if the input Base64 string is padded. + static inline bool isPadded(const char* input, size_t inputSize) { + return (inputSize > 0 && input[inputSize - 1] == kPadding); } - /// Counts the number of padding characters in encoded data. - static inline size_t numPadding(const char* src, size_t len) { + // Counts the number of padding characters in encoded input. + static inline size_t numPadding(const char* input, size_t inputSize) { size_t numPadding{0}; - while (len > 0 && src[len - 1] == kPadding) { + while (inputSize > 0 && input[inputSize - 1] == kPadding) { numPadding++; - len--; + inputSize--; } return numPadding; } - /// Performs a reverse lookup in the reverse index to retrieve the original - /// index of a character in the base. - static uint8_t base64ReverseLookup(char p, const ReverseIndex& reverseIndex); + // Reverse lookup helper function to get the original index of a Base64 + // character. + static uint8_t base64ReverseLookup( + char encodedChar, + const ReverseIndex& reverseIndex); - /// Encodes the specified data using the provided charset. + // Encodes the specified data using the provided charset. template static std::string - encodeImpl(const T& data, const Charset& charset, bool include_pad); + encodeImpl(const T& input, const Charset& charset, bool includePadding); - /// Encodes the specified data using the provided charset. + // Encodes the specified data using the provided charset. template static void encodeImpl( - const T& data, + const T& input, const Charset& charset, - bool include_pad, - char* out); + bool includePadding, + char* outputBuffer); - /// Decodes the specified data using the provided reverse lookup table. + // Decodes the specified data using the provided reverse lookup table. static size_t decodeImpl( - const char* src, - size_t src_len, - char* dst, - size_t dst_len, - const ReverseIndex& table); + const char* input, + size_t inputSize, + char* outputBuffer, + size_t outputSize, + const ReverseIndex& reverseIndex); VELOX_FRIEND_TEST(Base64Test, checksPadding); VELOX_FRIEND_TEST(Base64Test, countsPaddingCorrectly);