|
| 1 | +// Copyright 2026 Arash Hatami |
| 2 | + |
| 3 | +#include "base64_functions.hpp" |
| 4 | + |
| 5 | +#include <array> |
| 6 | + |
| 7 | +namespace duckdb { |
| 8 | + |
| 9 | +// Base64 alphabet |
| 10 | +static const char BASE64_CHARS[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; |
| 11 | + |
| 12 | +// Reverse lookup table: maps ASCII byte value to Base64 index (255 = invalid) |
| 13 | +static constexpr std::array<uint8_t, 256> BuildBase64DecodeTable() { |
| 14 | + std::array<uint8_t, 256> table = {}; |
| 15 | + for (auto &v : table) { |
| 16 | + v = 255; |
| 17 | + } |
| 18 | + for (uint8_t i = 0; i < 64; i++) { |
| 19 | + const char *chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; |
| 20 | + table[static_cast<uint8_t>(chars[i])] = i; |
| 21 | + } |
| 22 | + return table; |
| 23 | +} |
| 24 | + |
| 25 | +static constexpr auto BASE64_DECODE_TABLE = BuildBase64DecodeTable(); |
| 26 | + |
| 27 | +void Base64EncodeFunction(DataChunk &args, ExpressionState &state, Vector &result) { |
| 28 | + auto &input_vector = args.data[0]; |
| 29 | + auto result_data = FlatVector::GetData<string_t>(result); |
| 30 | + auto &result_validity = FlatVector::Validity(result); |
| 31 | + |
| 32 | + for (idx_t i = 0; i < args.size(); i++) { |
| 33 | + auto value = input_vector.GetValue(i); |
| 34 | + if (value.IsNull()) { |
| 35 | + result_validity.SetInvalid(i); |
| 36 | + continue; |
| 37 | + } |
| 38 | + |
| 39 | + auto input = value.ToString(); |
| 40 | + |
| 41 | + try { |
| 42 | + auto encoded = netquack::Base64Encode(input); |
| 43 | + result_data[i] = StringVector::AddString(result, encoded); |
| 44 | + } catch (const std::exception &e) { |
| 45 | + result_data[i] = StringVector::AddString(result, "Error encoding base64: " + std::string(e.what())); |
| 46 | + } |
| 47 | + } |
| 48 | +} |
| 49 | + |
| 50 | +void Base64DecodeFunction(DataChunk &args, ExpressionState &state, Vector &result) { |
| 51 | + auto &input_vector = args.data[0]; |
| 52 | + auto result_data = FlatVector::GetData<string_t>(result); |
| 53 | + auto &result_validity = FlatVector::Validity(result); |
| 54 | + |
| 55 | + for (idx_t i = 0; i < args.size(); i++) { |
| 56 | + auto value = input_vector.GetValue(i); |
| 57 | + if (value.IsNull()) { |
| 58 | + result_validity.SetInvalid(i); |
| 59 | + continue; |
| 60 | + } |
| 61 | + |
| 62 | + auto input = value.ToString(); |
| 63 | + |
| 64 | + try { |
| 65 | + auto decoded = netquack::Base64Decode(input); |
| 66 | + result_data[i] = StringVector::AddString(result, decoded); |
| 67 | + } catch (const std::exception &e) { |
| 68 | + result_data[i] = StringVector::AddString(result, "Error decoding base64: " + std::string(e.what())); |
| 69 | + } |
| 70 | + } |
| 71 | +} |
| 72 | + |
| 73 | +namespace netquack { |
| 74 | + |
| 75 | +std::string Base64Encode(const std::string &input) { |
| 76 | + if (input.empty()) { |
| 77 | + return ""; |
| 78 | + } |
| 79 | + |
| 80 | + const auto *data = reinterpret_cast<const uint8_t *>(input.data()); |
| 81 | + size_t len = input.size(); |
| 82 | + |
| 83 | + // Calculate output size: 4 output chars per 3 input bytes, rounded up |
| 84 | + size_t output_len = 4 * ((len + 2) / 3); |
| 85 | + std::string result; |
| 86 | + result.reserve(output_len); |
| 87 | + |
| 88 | + for (size_t i = 0; i < len; i += 3) { |
| 89 | + uint32_t octet_a = data[i]; |
| 90 | + uint32_t octet_b = (i + 1 < len) ? data[i + 1] : 0; |
| 91 | + uint32_t octet_c = (i + 2 < len) ? data[i + 2] : 0; |
| 92 | + |
| 93 | + uint32_t triple = (octet_a << 16) | (octet_b << 8) | octet_c; |
| 94 | + |
| 95 | + result += BASE64_CHARS[(triple >> 18) & 0x3F]; |
| 96 | + result += BASE64_CHARS[(triple >> 12) & 0x3F]; |
| 97 | + result += (i + 1 < len) ? BASE64_CHARS[(triple >> 6) & 0x3F] : '='; |
| 98 | + result += (i + 2 < len) ? BASE64_CHARS[triple & 0x3F] : '='; |
| 99 | + } |
| 100 | + |
| 101 | + return result; |
| 102 | +} |
| 103 | + |
| 104 | +std::string Base64Decode(const std::string &input) { |
| 105 | + if (input.empty()) { |
| 106 | + return ""; |
| 107 | + } |
| 108 | + |
| 109 | + // Strip whitespace (spaces, tabs, newlines, carriage returns) |
| 110 | + std::string cleaned; |
| 111 | + cleaned.reserve(input.size()); |
| 112 | + for (char c : input) { |
| 113 | + if (c != ' ' && c != '\t' && c != '\n' && c != '\r') { |
| 114 | + cleaned += c; |
| 115 | + } |
| 116 | + } |
| 117 | + |
| 118 | + if (cleaned.empty()) { |
| 119 | + return ""; |
| 120 | + } |
| 121 | + |
| 122 | + // Validate length (must be multiple of 4 for standard base64) |
| 123 | + if (cleaned.size() % 4 != 0) { |
| 124 | + return "INVALID_BASE64"; |
| 125 | + } |
| 126 | + |
| 127 | + // Validate characters |
| 128 | + for (size_t i = 0; i < cleaned.size(); i++) { |
| 129 | + char c = cleaned[i]; |
| 130 | + if (c == '=') { |
| 131 | + // Padding only allowed at the end (last 1-2 chars) |
| 132 | + if (i < cleaned.size() - 2) { |
| 133 | + return "INVALID_BASE64"; |
| 134 | + } |
| 135 | + } else if (BASE64_DECODE_TABLE[static_cast<uint8_t>(c)] == 255) { |
| 136 | + return "INVALID_BASE64"; |
| 137 | + } |
| 138 | + } |
| 139 | + |
| 140 | + // Count padding |
| 141 | + size_t padding = 0; |
| 142 | + if (!cleaned.empty() && cleaned[cleaned.size() - 1] == '=') { |
| 143 | + padding++; |
| 144 | + } |
| 145 | + if (cleaned.size() > 1 && cleaned[cleaned.size() - 2] == '=') { |
| 146 | + padding++; |
| 147 | + } |
| 148 | + |
| 149 | + // Calculate output size |
| 150 | + size_t output_len = (cleaned.size() / 4) * 3 - padding; |
| 151 | + std::string result; |
| 152 | + result.reserve(output_len); |
| 153 | + |
| 154 | + for (size_t i = 0; i < cleaned.size(); i += 4) { |
| 155 | + uint32_t sextet_a = BASE64_DECODE_TABLE[static_cast<uint8_t>(cleaned[i])]; |
| 156 | + uint32_t sextet_b = BASE64_DECODE_TABLE[static_cast<uint8_t>(cleaned[i + 1])]; |
| 157 | + uint32_t sextet_c = (cleaned[i + 2] == '=') ? 0 : BASE64_DECODE_TABLE[static_cast<uint8_t>(cleaned[i + 2])]; |
| 158 | + uint32_t sextet_d = (cleaned[i + 3] == '=') ? 0 : BASE64_DECODE_TABLE[static_cast<uint8_t>(cleaned[i + 3])]; |
| 159 | + |
| 160 | + uint32_t triple = (sextet_a << 18) | (sextet_b << 12) | (sextet_c << 6) | sextet_d; |
| 161 | + |
| 162 | + result += static_cast<char>((triple >> 16) & 0xFF); |
| 163 | + if (cleaned[i + 2] != '=') { |
| 164 | + result += static_cast<char>((triple >> 8) & 0xFF); |
| 165 | + } |
| 166 | + if (cleaned[i + 3] != '=') { |
| 167 | + result += static_cast<char>(triple & 0xFF); |
| 168 | + } |
| 169 | + } |
| 170 | + |
| 171 | + return result; |
| 172 | +} |
| 173 | + |
| 174 | +} // namespace netquack |
| 175 | +} // namespace duckdb |
0 commit comments