Skip to content

Commit

Permalink
Refactor Base64 APIs as non-throwing APIs
Browse files Browse the repository at this point in the history
  • Loading branch information
Joe-Abraham committed Nov 24, 2024
1 parent 78d761b commit 963f91b
Show file tree
Hide file tree
Showing 6 changed files with 196 additions and 130 deletions.
203 changes: 126 additions & 77 deletions velox/common/encode/Base64.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,7 @@
#include <folly/Portability.h>
#include <folly/container/Foreach.h>
#include <folly/io/Cursor.h>
#include <stdint.h>

#include "velox/common/base/Exceptions.h"
#include <cstdint>

namespace facebook::velox::encoding {

Expand Down Expand Up @@ -163,10 +161,10 @@ std::string Base64::encodeImpl(
const T& input,
const Charset& charset,
bool includePadding) {
size_t encodedSize = calculateEncodedSize(input.size(), includePadding);
size_t encodedSize{calculateEncodedSize(input.size(), includePadding)};
std::string encodedResult;
encodedResult.resize(encodedSize);
encodeImpl(input, charset, includePadding, encodedResult.data());
(void)encodeImpl(input, charset, includePadding, encodedResult.data());
return encodedResult;
}

Expand All @@ -186,27 +184,31 @@ size_t Base64::calculateEncodedSize(size_t inputSize, bool withPadding) {
}

// static
void Base64::encode(const char* input, size_t inputSize, char* output) {
encodeImpl(
Status Base64::encode(const char* input, size_t inputSize, char* output) {
return encodeImpl(
folly::StringPiece(input, inputSize), kBase64Charset, true, output);
}

// static
void Base64::encodeUrl(const char* input, size_t inputSize, char* output) {
encodeImpl(
folly::StringPiece(input, inputSize), kBase64UrlCharset, true, output);
Status
Base64::encodeUrl(const char* input, size_t inputSize, char* outputBuffer) {
return encodeImpl(
folly::StringPiece(input, inputSize),
kBase64UrlCharset,
true,
outputBuffer);
}

// static
template <class T>
void Base64::encodeImpl(
Status Base64::encodeImpl(
const T& input,
const Charset& charset,
bool includePadding,
char* outputBuffer) {
auto inputSize = input.size();
if (inputSize == 0) {
return;
return Status::OK();
}

auto outputPointer = outputBuffer;
Expand All @@ -215,9 +217,9 @@ void Base64::encodeImpl(
// 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 (; inputSize > 2; inputSize -= 3) {
uint32_t inputBlock = uint8_t(*inputIterator++) << 16;
inputBlock |= uint8_t(*inputIterator++) << 8;
inputBlock |= uint8_t(*inputIterator++);
uint32_t inputBlock = static_cast<uint8_t>(*inputIterator++) << 16;
inputBlock |= static_cast<uint8_t>(*inputIterator++) << 8;
inputBlock |= static_cast<uint8_t>(*inputIterator++);

*outputPointer++ = charset[(inputBlock >> 18) & 0x3f];
*outputPointer++ = charset[(inputBlock >> 12) & 0x3f];
Expand All @@ -229,10 +231,10 @@ void Base64::encodeImpl(
// 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 inputBlock = uint8_t(*inputIterator++) << 16;
uint32_t inputBlock = static_cast<uint8_t>(*inputIterator++) << 16;
*outputPointer++ = charset[(inputBlock >> 18) & 0x3f];
if (inputSize > 1) {
inputBlock |= uint8_t(*inputIterator) << 8;
inputBlock |= static_cast<uint8_t>(*inputIterator) << 8;
*outputPointer++ = charset[(inputBlock >> 12) & 0x3f];
*outputPointer++ = charset[(inputBlock >> 6) & 0x3f];
if (includePadding) {
Expand All @@ -246,6 +248,7 @@ void Base64::encodeImpl(
}
}
}
return Status::OK();
}

// static
Expand Down Expand Up @@ -310,8 +313,7 @@ std::string Base64::encode(const folly::IOBuf* inputBuffer) {
// static
std::string Base64::decode(folly::StringPiece encodedText) {
std::string decodedResult;
Base64::decode(
std::make_pair(encodedText.data(), encodedText.size()), decodedResult);
decode(std::make_pair(encodedText.data(), encodedText.size()), decodedResult);
return decodedResult;
}

Expand All @@ -320,29 +322,36 @@ void Base64::decode(
const std::pair<const char*, int32_t>& payload,
std::string& decodedOutput) {
size_t inputSize = payload.second;
decodedOutput.resize(calculateDecodedSize(payload.first, inputSize));
decode(payload.first, inputSize, decodedOutput.data(), decodedOutput.size());
size_t decodedSize;
(void)calculateDecodedSize(payload.first, inputSize, decodedSize);
decodedOutput.resize(decodedSize);
(void)decode(
payload.first, inputSize, decodedOutput.data(), decodedOutput.size());
}

// static
void Base64::decode(const char* input, size_t size, char* output) {
size_t expectedOutputSize = size / 4 * 3;
Base64::decode(input, size, output, expectedOutputSize);
void Base64::decode(const char* input, size_t inputSize, char* outputBuffer) {
size_t outputSize;
(void)calculateDecodedSize(input, inputSize, outputSize);
(void)decode(input, inputSize, outputBuffer, outputSize);
}

// static
uint8_t Base64::base64ReverseLookup(
Status Base64::base64ReverseLookup(
char encodedChar,
const Base64::ReverseIndex& reverseIndex) {
auto reverseLookupValue = reverseIndex[(uint8_t)encodedChar];
const ReverseIndex& reverseIndex,
uint8_t& reverseLookupValue) {
reverseLookupValue = reverseIndex[static_cast<uint8_t>(encodedChar)];
if (reverseLookupValue >= 0x40) {
VELOX_USER_FAIL("decode() - invalid input string: invalid characters");
return Status::UserError(fmt::format(
"decode() - invalid input string: invalid character '{}'",
encodedChar));
}
return reverseLookupValue;
return Status::OK();
}

// static
size_t Base64::decode(
Status Base64::decode(
const char* input,
size_t inputSize,
char* output,
Expand All @@ -352,98 +361,137 @@ size_t Base64::decode(
}

// static
size_t Base64::calculateDecodedSize(const char* input, size_t& inputSize) {
Status Base64::calculateDecodedSize(
const char* input,
size_t& inputSize,
size_t& decodedSize) {
if (inputSize == 0) {
return 0;
decodedSize = 0;
return Status::OK();
}

// 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 (inputSize % kEncodedBlockByteSize != 0) {
VELOX_USER_FAIL(
return Status::UserError(
"Base64::decode() - invalid input string: "
"string length is not a multiple of 4.");
}

auto decodedSize =
(inputSize * kBinaryBlockByteSize) / kEncodedBlockByteSize;
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 decodedSize -
decodedSize -=
((paddingCount * kBinaryBlockByteSize) + (kEncodedBlockByteSize - 1)) /
kEncodedBlockByteSize;
return Status::OK();
}
// If not padded, Calculate extra bytes, if any
auto extraBytes = inputSize % kEncodedBlockByteSize;
auto decodedSize = (inputSize / kEncodedBlockByteSize) * kBinaryBlockByteSize;
decodedSize = (inputSize / kEncodedBlockByteSize) * kBinaryBlockByteSize;

// Adjust the needed size for extra bytes, if present
if (extraBytes) {
if (extraBytes == 1) {
VELOX_USER_FAIL(
return Status::UserError(
"Base64::decode() - invalid input string: "
"string length cannot be 1 more than a multiple of 4.");
}
decodedSize += (extraBytes * kBinaryBlockByteSize) / kEncodedBlockByteSize;
}

return decodedSize;
return Status::OK();
}

// static
size_t Base64::decodeImpl(
Status Base64::decodeImpl(
const char* input,
size_t inputSize,
char* outputBuffer,
size_t outputSize,
const ReverseIndex& reverseIndex) {
if (!inputSize) {
return 0;
if (inputSize == 0) {
return Status::OK();
}

auto decodedSize = calculateDecodedSize(input, inputSize);
size_t decodedSize;
auto status = calculateDecodedSize(input, inputSize, decodedSize);
if (!status.ok()) {
return status;
}
if (outputSize < decodedSize) {
VELOX_USER_FAIL(
return Status::UserError(
"Base64::decode() - invalid output string: "
"output string is too small.");
}

// Handle full groups of 4 characters
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 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;
while (inputSize > 4) {
uint32_t decodedBlock = 0;
uint8_t reverseLookupValue;

// Process each character in the group
for (int i = 0; i < 4; ++i) {
status = base64ReverseLookup(input[i], reverseIndex, reverseLookupValue);
if (!status.ok()) {
return status;
}
decodedBlock |= reverseLookupValue << (18 - 6 * i);
}

// Write the decoded bytes to the output buffer
outputBuffer[0] = static_cast<char>((decodedBlock >> 16) & 0xff);
outputBuffer[1] = static_cast<char>((decodedBlock >> 8) & 0xff);
outputBuffer[2] = static_cast<char>(decodedBlock & 0xff);

// Advance the pointers
input += 4;
outputBuffer += 3;
inputSize -= 4;
}

// Handle the last 2-4 characters. This is similar to the above, but the
// last 2 characters may or may not exist.
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;
// Handle the last 2-4 characters
if (inputSize >= 2) {
uint32_t decodedBlock = 0;
uint8_t reverseLookupValue;

// Process the first two characters
for (int i = 0; i < 2; ++i) {
status = base64ReverseLookup(input[i], reverseIndex, reverseLookupValue);
if (!status.ok()) {
return status;
}
decodedBlock |= reverseLookupValue << (18 - 6 * i);
}
outputBuffer[0] = static_cast<char>((decodedBlock >> 16) & 0xff);

if (inputSize > 2) {
// Process the third character
status = base64ReverseLookup(input[2], reverseIndex, reverseLookupValue);
if (!status.ok()) {
return status;
}
decodedBlock |= reverseLookupValue << 6;
outputBuffer[1] = static_cast<char>((decodedBlock >> 8) & 0xff);

if (inputSize > 3) {
status =
base64ReverseLookup(input[3], reverseIndex, reverseLookupValue);
if (!status.ok()) {
return status;
}
decodedBlock |= reverseLookupValue;
outputBuffer[2] = static_cast<char>(decodedBlock & 0xff);
}
}
}

return decodedSize;
return Status::OK();
}

// static
Expand All @@ -462,19 +510,19 @@ std::string Base64::encodeUrl(const folly::IOBuf* inputBuffer) {
}

// static
void Base64::decodeUrl(
Status Base64::decodeUrl(
const char* input,
size_t inputSize,
char* outputBuffer,
size_t outputSize) {
decodeImpl(
return decodeImpl(
input, inputSize, outputBuffer, outputSize, kBase64UrlReverseIndexTable);
}

// static
std::string Base64::decodeUrl(folly::StringPiece encodedText) {
std::string decodedOutput;
Base64::decodeUrl(
decodeUrl(
std::make_pair(encodedText.data(), encodedText.size()), decodedOutput);
return decodedOutput;
}
Expand All @@ -483,15 +531,16 @@ std::string Base64::decodeUrl(folly::StringPiece encodedText) {
void Base64::decodeUrl(
const std::pair<const char*, int32_t>& payload,
std::string& decodedOutput) {
size_t decodedSize = (payload.second + 3) / 4 * 3;
decodedOutput.resize(decodedSize, '\0');
decodedSize = Base64::decodeImpl(
size_t inputSize = payload.second;
size_t decodedSize;
(void)calculateDecodedSize(payload.first, inputSize, decodedSize);
decodedOutput.resize(decodedSize);
(void)decodeImpl(
payload.first,
payload.second,
&decodedOutput[0],
decodedSize,
decodedOutput.size(),
kBase64UrlReverseIndexTable);
decodedOutput.resize(decodedSize);
}

} // namespace facebook::velox::encoding
Loading

0 comments on commit 963f91b

Please sign in to comment.