Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ARROW-14274: [C++] Refine base64 api #11397

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 2 additions & 5 deletions cpp/src/arrow/flight/client_header_internal.cc
Original file line number Diff line number Diff line change
Expand Up @@ -293,11 +293,8 @@ std::string CookieCache::GetValidCookiesAsString() {
void AddBasicAuthHeaders(grpc::ClientContext* context, const std::string& username,
const std::string& password) {
const std::string credentials = username + ":" + password;
context->AddMetadata(
kAuthHeader,
kBasicPrefix + arrow::util::base64_encode(
reinterpret_cast<const unsigned char*>(credentials.c_str()),
static_cast<unsigned int>(credentials.size())));
context->AddMetadata(kAuthHeader,
kBasicPrefix + arrow::util::base64_encode(credentials));
}

/// \brief Get bearer token from inbound headers.
Expand Down
5 changes: 3 additions & 2 deletions cpp/src/arrow/util/base64.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,17 @@

#include <string>

#include "arrow/util/string_view.h"
#include "arrow/util/visibility.h"

namespace arrow {
namespace util {

ARROW_EXPORT
std::string base64_encode(unsigned char const*, unsigned int len);
std::string base64_encode(string_view s);

ARROW_EXPORT
std::string base64_decode(std::string const& s);
std::string base64_decode(string_view s);

} // namespace util
} // namespace arrow
10 changes: 8 additions & 2 deletions cpp/src/arrow/vendored/base64.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ static inline bool is_base64(unsigned char c) {
return (isalnum(c) || (c == '+') || (c == '/'));
}

std::string base64_encode(unsigned char const* bytes_to_encode, unsigned int in_len) {
static std::string base64_encode(unsigned char const* bytes_to_encode, unsigned int in_len) {
std::string ret;
int i = 0;
int j = 0;
Expand Down Expand Up @@ -87,7 +87,13 @@ std::string base64_encode(unsigned char const* bytes_to_encode, unsigned int in_

}

std::string base64_decode(std::string const& encoded_string) {
std::string base64_encode(string_view string_to_encode) {
auto bytes_to_encode = reinterpret_cast<const unsigned char*>(string_to_encode.data());
auto in_len = static_cast<unsigned int>(string_to_encode.size());
return base64_encode(bytes_to_encode, in_len);
}

std::string base64_decode(string_view encoded_string) {
size_t in_len = encoded_string.size();
int i = 0;
int j = 0;
Expand Down
6 changes: 4 additions & 2 deletions cpp/src/gandiva/gdv_function_stubs.cc
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
#include "arrow/util/base64.h"
#include "arrow/util/double_conversion.h"
#include "arrow/util/formatting.h"
#include "arrow/util/string_view.h"
#include "arrow/util/utf8.h"
#include "arrow/util/value_parsing.h"
#include "gandiva/engine.h"
Expand Down Expand Up @@ -337,7 +338,7 @@ const char* gdv_fn_base64_encode_binary(int64_t context, const char* in, int32_t
}
// use arrow method to encode base64 string
std::string encoded_str =
arrow::util::base64_encode(reinterpret_cast<const unsigned char*>(in), in_len);
arrow::util::base64_encode(arrow::util::string_view(in, in_len));
*out_len = static_cast<int32_t>(encoded_str.length());
// allocate memory for response
char* ret = reinterpret_cast<char*>(
Expand All @@ -364,7 +365,8 @@ const char* gdv_fn_base64_decode_utf8(int64_t context, const char* in, int32_t i
return "";
}
// use arrow method to decode base64 string
std::string decoded_str = arrow::util::base64_decode(std::string(in, in_len));
std::string decoded_str =
arrow::util::base64_decode(arrow::util::string_view(in, in_len));
*out_len = static_cast<int32_t>(decoded_str.length());
// allocate memory for response
char* ret = reinterpret_cast<char*>(
Expand Down
4 changes: 1 addition & 3 deletions cpp/src/parquet/arrow/writer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -422,9 +422,7 @@ Status GetSchemaMetadata(const ::arrow::Schema& schema, ::arrow::MemoryPool* poo

// The serialized schema is not UTF-8, which is required for Thrift
std::string schema_as_string = serialized->ToString();
std::string schema_base64 = ::arrow::util::base64_encode(
reinterpret_cast<const unsigned char*>(schema_as_string.data()),
static_cast<unsigned int>(schema_as_string.size()));
std::string schema_base64 = ::arrow::util::base64_encode(schema_as_string);
result->Append(kArrowSchemaKey, schema_base64);
*out = result;
return Status::OK();
Expand Down
8 changes: 3 additions & 5 deletions cpp/src/parquet/encryption/key_encryption_key.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,11 @@ namespace encryption {
// locally, and does not involve an interaction with a KMS server.
class KeyEncryptionKey {
public:
KeyEncryptionKey(const std::string& kek_bytes, const std::string& kek_id,
const std::string& encoded_wrapped_kek)
KeyEncryptionKey(std::string kek_bytes, std::string kek_id,
std::string encoded_wrapped_kek)
: kek_bytes_(std::move(kek_bytes)),
kek_id_(std::move(kek_id)),
encoded_kek_id_(
::arrow::util::base64_encode(reinterpret_cast<const uint8_t*>(kek_id_.data()),
static_cast<uint32_t>(kek_id_.size()))),
encoded_kek_id_(::arrow::util::base64_encode(kek_id_)),
encoded_wrapped_kek_(std::move(encoded_wrapped_kek)) {}

const std::string& kek_bytes() const { return kek_bytes_; }
Expand Down
4 changes: 2 additions & 2 deletions cpp/src/parquet/encryption/key_toolkit_internal.cc
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,8 @@ std::string EncryptKeyLocally(const std::string& key_bytes, const std::string& m
static_cast<int>(master_key.size()), reinterpret_cast<const uint8_t*>(aad.data()),
static_cast<int>(aad.size()), reinterpret_cast<uint8_t*>(&encrypted_key[0]));

return ::arrow::util::base64_encode(reinterpret_cast<const uint8_t*>(&encrypted_key[0]),
encrypted_key_len);
return ::arrow::util::base64_encode(
::arrow::util::string_view(encrypted_key.data(), encrypted_key_len));
}

std::string DecryptKeyLocally(const std::string& encoded_encrypted_key,
Expand Down