Skip to content

Commit

Permalink
c
Browse files Browse the repository at this point in the history
  • Loading branch information
Joe-Abraham committed Oct 3, 2024
1 parent 3e7a092 commit f3e0450
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 18 deletions.
34 changes: 20 additions & 14 deletions velox/common/encode/Base64.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -169,14 +169,14 @@ std::string Base64::encodeImpl(
}

// static
size_t Base64::calculateEncodedSize(size_t inputSize, bool withPadding) {
size_t Base64::calculateEncodedSize(size_t inputSize, bool includePadding) {
if (inputSize == 0) {
return 0;
}

// Calculate the output size assuming that we are including padding.
size_t encodedSize = ((inputSize + 2) / 3) * 4;
if (!withPadding) {
if (!includePadding) {
// If the padding was not requested, subtract the padding bytes.
encodedSize -= (3 - (inputSize % 3)) % 3;
}
Expand Down Expand Up @@ -248,6 +248,7 @@ Status Base64::encodeImpl(
}
}
}

return Status::OK();
}

Expand Down Expand Up @@ -333,9 +334,10 @@ void Base64::decode(
}

// static
void Base64::decode(const char* input, size_t size, char* output) {
size_t expectedOutputSize = size / 4 * 3;
(void)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)Base64::decode(input, inputSize, outputBuffer, outputSize);
}

// static
Expand Down Expand Up @@ -367,6 +369,7 @@ Status Base64::calculateDecodedSize(
size_t& inputSize,
size_t& decodedSize) {
if (inputSize == 0) {
decodedSize = 0;
return Status::OK();
}

Expand Down Expand Up @@ -415,12 +418,15 @@ Status Base64::decodeImpl(
char* outputBuffer,
size_t outputSize,
const Base64::ReverseIndex& reverseIndex) {
if (!inputSize) {
if (inputSize == 0) {
return Status::OK();
}

size_t decodedSize;
(void)calculateDecodedSize(input, inputSize, decodedSize);
auto status = calculateDecodedSize(input, inputSize, decodedSize);
if (!status.ok()) {
return status;
}
if (outputSize < decodedSize) {
return Status::UserError(
"Base64::decode() - invalid output string: output string is too small.");
Expand All @@ -437,9 +443,9 @@ Status Base64::decodeImpl(
(base64ReverseLookup(input[1], reverseIndex, lookupStatus) << 12) |
(base64ReverseLookup(input[2], reverseIndex, lookupStatus) << 6) |
base64ReverseLookup(input[3], reverseIndex, lookupStatus);
outputBuffer[0] = static_cast<char>((decodedBlock >> 16) & 0xff);
outputBuffer[1] = static_cast<char>((decodedBlock >> 8) & 0xff);
outputBuffer[2] = static_cast<char>(decodedBlock & 0xff);
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
Expand All @@ -448,18 +454,18 @@ Status Base64::decodeImpl(
uint32_t decodedBlock =
(base64ReverseLookup(input[0], reverseIndex, lookupStatus) << 18) |
(base64ReverseLookup(input[1], reverseIndex, lookupStatus) << 12);
outputBuffer[0] = static_cast<char>((decodedBlock >> 16) & 0xff);
outputBuffer[0] = (decodedBlock >> 16) & 0xff;
if (inputSize > 2) {
decodedBlock |= base64ReverseLookup(input[2], reverseIndex, lookupStatus)
<< 6;
outputBuffer[1] = static_cast<char>((decodedBlock >> 8) & 0xff);
outputBuffer[1] = (decodedBlock >> 8) & 0xff;
if (inputSize > 3) {
decodedBlock |= base64ReverseLookup(input[3], reverseIndex, lookupStatus);
outputBuffer[2] = static_cast<char>(decodedBlock & 0xff);
outputBuffer[2] = decodedBlock & 0xff;
}
}

return (lookupStatus != Status::OK()) ? lookupStatus : Status::OK();
return !lookupStatus.ok() ? lookupStatus : Status::OK();
}

// static
Expand Down
12 changes: 8 additions & 4 deletions velox/functions/prestosql/BinaryFunctions.h
Original file line number Diff line number Diff line change
Expand Up @@ -295,9 +295,11 @@ struct FromBase64Function {
FOLLY_ALWAYS_INLINE Status call(out_type<Varbinary>& result, const T& input) {
auto inputSize = input.size();
size_t decodedSize;
auto status = encoding::Base64::calculateDecodedSize(input.data(), inputSize,decodedSize);
if(status != Status::OK())
auto status = encoding::Base64::calculateDecodedSize(
input.data(), inputSize, decodedSize);
if (!status.ok()) {
return status;
}
result.resize(decodedSize);
return encoding::Base64::decode(
input.data(), inputSize, result.data(), result.size());
Expand All @@ -311,9 +313,11 @@ struct FromBase64UrlFunction {
call(out_type<Varbinary>& result, const arg_type<Varchar>& input) {
auto inputSize = input.size();
size_t decodedSize;
auto status = encoding::Base64::calculateDecodedSize(input.data(), inputSize,decodedSize);
if(status != Status::OK())
auto status = encoding::Base64::calculateDecodedSize(
input.data(), inputSize, decodedSize);
if (!status.ok()) {
return status;
}
result.resize(decodedSize);
return encoding::Base64::decodeUrl(
input.data(), inputSize, result.data(), result.size());
Expand Down

0 comments on commit f3e0450

Please sign in to comment.