Skip to content
Open
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
22 changes: 14 additions & 8 deletions cpp/src/arrow/vendored/base64.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
*/

#include "arrow/util/base64.h"
#include <array>
#include <iostream>

namespace arrow {
Expand All @@ -40,6 +41,17 @@ static const std::string base64_chars =
"abcdefghijklmnopqrstuvwxyz"
"0123456789+/";

static const std::array<int8_t, 256> kBase64Lookup = [] {
std::array<int8_t, 256> table{};
table.fill(-1);

for (size_t i = 0; i < base64_chars.size(); ++i) {
table[static_cast<uint8_t>(base64_chars[i])] = i;
}

return table;
}();

static std::string base64_encode(unsigned char const* bytes_to_encode, unsigned int in_len) {
std::string ret;
int i = 0;
Expand Down Expand Up @@ -119,22 +131,16 @@ Result<std::string> base64_decode(std::string_view encoded_string) {
return Status::Invalid("Invalid base64 input: padding in wrong position");
}

if (base64_chars.find(c) == std::string::npos) {
if (kBase64Lookup[static_cast<uint8_t>(c)] == -1) {
return Status::Invalid("Invalid base64 input: character is not valid base64 character");
}

char_array_4[i++] = c;
char_array_4[i++] = kBase64Lookup[static_cast<uint8_t>(c)];
}

in_++;

if (i == 4) {
for (i = 0; i < 4; i++) {
if (char_array_4[i] != 0) {
char_array_4[i] = base64_chars.find(char_array_4[i]) & 0xff;
}
}

char_array_3[0] = (char_array_4[0] << 2) + ((char_array_4[1] & 0x30) >> 4);
char_array_3[1] = ((char_array_4[1] & 0xf) << 4) + ((char_array_4[2] & 0x3c) >> 2);
char_array_3[2] = ((char_array_4[2] & 0x3) << 6) + char_array_4[3];
Expand Down
Loading