Skip to content

Commit

Permalink
decode base64
Browse files Browse the repository at this point in the history
  • Loading branch information
Joe-Abraham committed Aug 16, 2024
1 parent 798aa1f commit ef739fc
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 19 deletions.
20 changes: 10 additions & 10 deletions velox/common/encode/Base64.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ static_assert(
// "kBase64UrlReverseIndexTable has incorrect entries.");

// Implementation of Base64 encoding and decoding functions.
// static
// static
template <class T>
std::string Base64::encodeImpl(
const T& data,
Expand Down Expand Up @@ -194,7 +194,7 @@ Status Base64::encodeUrl(std::string_view data, char* output) {
return encodeImpl(data, kBase64UrlCharset, true, output);
}

// static
// static
template <class T>
Status Base64::encodeImpl(
const T& data,
Expand Down Expand Up @@ -339,7 +339,7 @@ uint8_t Base64::base64ReverseLookup(
}

// static
size_t Base64::decode(
Status Base64::decode(
std::string_view src,
size_t src_len,
char* dst,
Expand Down Expand Up @@ -391,19 +391,19 @@ size_t Base64::calculateDecodedSize(std::string_view data, size_t& size) {
}

// static
size_t Base64::decodeImpl(
Status Base64::decodeImpl(
std::string_view src,
size_t src_len,
char* dst,
size_t dst_len,
const Base64::ReverseIndex& reverseIndex) {
if (!src_len) {
return 0;
return Status::OK();
}

auto needed = calculateDecodedSize(src, src_len);
if (dst_len < needed) {
VELOX_USER_FAIL(
return Status::UserError(
"Base64::decode() - invalid output string: "
"output string is too small.");
}
Expand Down Expand Up @@ -437,7 +437,7 @@ size_t Base64::decodeImpl(
}
}

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

// static
Expand All @@ -451,12 +451,12 @@ std::string Base64::encodeUrl(const folly::IOBuf* data) {
}

// static
void Base64::decodeUrl(
Status Base64::decodeUrl(
std::string_view src,
size_t src_len,
char* dst,
size_t dst_len) {
decodeImpl(src, src_len, dst, dst_len, kBase64UrlReverseIndexTable);
return decodeImpl(src, src_len, dst, dst_len, kBase64UrlReverseIndexTable);
}

// static
Expand All @@ -470,7 +470,7 @@ std::string Base64::decodeUrl(std::string_view encoded) {
void Base64::decodeUrl(std::string_view payload, std::string& output) {
size_t out_len = (payload.size() + 3) / 4 * 3;
output.resize(out_len, '\0');
out_len = Base64::decodeImpl(
Base64::decodeImpl(
payload.data(),
payload.size(),
&output[0],
Expand Down
6 changes: 3 additions & 3 deletions velox/common/encode/Base64.h
Original file line number Diff line number Diff line change
Expand Up @@ -93,12 +93,12 @@ class Base64 {

/// Decodes the specified number of characters from the 'src' and writes the
/// result to the 'dst'.
static size_t
static Status
decode(std::string_view src, size_t src_len, char* dst, size_t dst_len);

/// Decodes the specified number of characters from the 'src' using URL
/// encoding and writes the result to the 'dst'.
static void
static Status
decodeUrl(std::string_view src, size_t src_len, char* dst, size_t dst_len);

private:
Expand Down Expand Up @@ -138,7 +138,7 @@ class Base64 {
char* out);

// Decodes the specified data using the provided reverse lookup table.
static size_t decodeImpl(
static Status decodeImpl(
std::string_view src,
size_t src_len,
char* dst,
Expand Down
11 changes: 5 additions & 6 deletions velox/functions/prestosql/BinaryFunctions.h
Original file line number Diff line number Diff line change
Expand Up @@ -290,25 +290,24 @@ struct FromBase64Function {
// T can be either arg_type<Varchar> or arg_type<Varbinary>. These are the
// same, but hard-coding one of them might be confusing.
template <typename T>
FOLLY_ALWAYS_INLINE void call(out_type<Varbinary>& result, const T& input) {
FOLLY_ALWAYS_INLINE Status call(out_type<Varbinary>& result, const T& input) {
auto inputSize = input.size();
result.resize(
encoding::Base64::calculateDecodedSize(input.data(), inputSize));
encoding::Base64::decode(
return encoding::Base64::decode(
input.data(), inputSize, result.data(), result.size());
}
};

template <typename T>
struct FromBase64UrlFunction {
VELOX_DEFINE_FUNCTION_TYPES(T);
FOLLY_ALWAYS_INLINE void call(
out_type<Varbinary>& result,
const arg_type<Varchar>& input) {
FOLLY_ALWAYS_INLINE Status
call(out_type<Varbinary>& result, const arg_type<Varchar>& input) {
auto inputSize = input.size();
result.resize(
encoding::Base64::calculateDecodedSize(input.data(), inputSize));
encoding::Base64::decodeUrl(
return encoding::Base64::decodeUrl(
input.data(), inputSize, result.data(), result.size());
}
};
Expand Down

0 comments on commit ef739fc

Please sign in to comment.