Skip to content

Commit

Permalink
x
Browse files Browse the repository at this point in the history
  • Loading branch information
Joe-Abraham committed May 27, 2024
1 parent b403595 commit a0946a1
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 12 deletions.
24 changes: 19 additions & 5 deletions velox/functions/prestosql/BinaryFunctions.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@

#include "folly/ssl/OpenSSLHash.h"
#include "velox/common/base/BitUtil.h"
#include "velox/common/encode/Base64.h"
#include "velox/external/md5/md5.h"
#include "velox/functions/Udf.h"
#include "velox/functions/lib/ToHex.h"
Expand Down Expand Up @@ -294,8 +293,15 @@ struct FromBase64Function {
out_type<Varbinary>& result,
const arg_type<Varchar>& input) {
try {
auto decoded = cppcodec::base64_rfc4648::decode<std::vector<uint8_t>>(
std::string(input.data(), input.size()));
std::string inputStr = std::string(input.data(), input.size());

// Calculate the number of padding characters needed
size_t padding = (4 - (inputStr.size() % 4)) % 4;
inputStr.append(padding, '=');

// Decode using cppcodec with padding
std::vector<uint8_t> decoded = cppcodec::base64_rfc4648::decode<std::vector<uint8_t>>(inputStr);

result.resize(decoded.size());
std::copy(decoded.begin(), decoded.end(), result.data());
} catch (const cppcodec::parse_error& e) {
Expand All @@ -312,8 +318,15 @@ struct FromBase64UrlFunction {
out_type<Varbinary>& result,
const arg_type<Varchar>& input) {
try {
auto decoded = cppcodec::base64_url::decode<std::vector<uint8_t>>(
std::string(input.data(), input.size()));
std::string inputStr = std::string(input.data(), input.size());

// Calculate the number of padding characters needed
size_t padding = (4 - (inputStr.size() % 4)) % 4;
inputStr.append(padding, '=');

// Decode using cppcodec with padding
std::vector<uint8_t> decoded = cppcodec::base64_url::decode<std::vector<uint8_t>>(inputStr);

result.resize(decoded.size());
std::copy(decoded.begin(), decoded.end(), result.data());
} catch (const cppcodec::parse_error& e) {
Expand All @@ -322,6 +335,7 @@ struct FromBase64UrlFunction {
}
};


template <typename T>
struct ToBase64UrlFunction {
VELOX_DEFINE_FUNCTION_TYPES(T);
Expand Down
12 changes: 5 additions & 7 deletions velox/functions/prestosql/tests/BinaryFunctionsTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -424,13 +424,13 @@ TEST_F(BinaryFunctionsTest, fromBase64) {
"Hello World from Velox!",
fromBase64("SGVsbG8gV29ybGQgZnJvbSBWZWxveCE="));

// EXPECT_THROW(fromBase64("YQ="), VeloxUserError);
// EXPECT_THROW(fromBase64("YQ==="), VeloxUserError);
EXPECT_THROW(fromBase64("YQ=+"), VeloxUserError);
EXPECT_THROW(fromBase64("YQ===/"), VeloxUserError);

// Check encoded strings without padding
// EXPECT_EQ("a", fromBase64("YQ"));
// EXPECT_EQ("ab", fromBase64("YWI"));
// EXPECT_EQ("abcd", fromBase64("YWJjZA"));
EXPECT_EQ("a", fromBase64("YQ"));
EXPECT_EQ("ab", fromBase64("YWI"));
EXPECT_EQ("abcd", fromBase64("YWJjZA"));
}

TEST_F(BinaryFunctionsTest, fromBase64Url) {
Expand All @@ -454,8 +454,6 @@ TEST_F(BinaryFunctionsTest, fromBase64Url) {
EXPECT_EQ(fromHex("FF4FBF50"), fromBase64Url("_0-_UA=="));
// the encoded string input from base 64 url should be multiple of 4 and must
// not contain invalid char like '+' and '/'
EXPECT_THROW(fromBase64Url("YQ="), VeloxUserError);
EXPECT_THROW(fromBase64Url("YQ==="), VeloxUserError);
EXPECT_THROW(fromBase64Url("YQ=+"), VeloxUserError);
EXPECT_THROW(fromBase64Url("YQ=/"), VeloxUserError);
}
Expand Down

0 comments on commit a0946a1

Please sign in to comment.