|
| 1 | +// Copyright 2026 Arash Hatami |
| 2 | + |
| 3 | +#include "url_encode_functions.hpp" |
| 4 | + |
| 5 | +#include <array> |
| 6 | +#include <sstream> |
| 7 | + |
| 8 | +namespace duckdb { |
| 9 | + |
| 10 | +// RFC 3986 unreserved characters: A-Z a-z 0-9 - _ . ~ |
| 11 | +// These are the ONLY characters that are NOT percent-encoded. |
| 12 | +static constexpr std::array<bool, 256> BuildUnreservedTable() { |
| 13 | + std::array<bool, 256> table = {}; |
| 14 | + for (auto &v : table) { |
| 15 | + v = false; |
| 16 | + } |
| 17 | + // A-Z |
| 18 | + for (int c = 'A'; c <= 'Z'; c++) { |
| 19 | + table[c] = true; |
| 20 | + } |
| 21 | + // a-z |
| 22 | + for (int c = 'a'; c <= 'z'; c++) { |
| 23 | + table[c] = true; |
| 24 | + } |
| 25 | + // 0-9 |
| 26 | + for (int c = '0'; c <= '9'; c++) { |
| 27 | + table[c] = true; |
| 28 | + } |
| 29 | + // - _ . ~ |
| 30 | + table[static_cast<uint8_t>('-')] = true; |
| 31 | + table[static_cast<uint8_t>('_')] = true; |
| 32 | + table[static_cast<uint8_t>('.')] = true; |
| 33 | + table[static_cast<uint8_t>('~')] = true; |
| 34 | + return table; |
| 35 | +} |
| 36 | + |
| 37 | +static constexpr auto UNRESERVED_TABLE = BuildUnreservedTable(); |
| 38 | + |
| 39 | +static const char HEX_DIGITS[] = "0123456789ABCDEF"; |
| 40 | + |
| 41 | +// Returns -1 for invalid hex character |
| 42 | +static int HexVal(char c) { |
| 43 | + if (c >= '0' && c <= '9') { |
| 44 | + return c - '0'; |
| 45 | + } |
| 46 | + if (c >= 'a' && c <= 'f') { |
| 47 | + return c - 'a' + 10; |
| 48 | + } |
| 49 | + if (c >= 'A' && c <= 'F') { |
| 50 | + return c - 'A' + 10; |
| 51 | + } |
| 52 | + return -1; |
| 53 | +} |
| 54 | + |
| 55 | +void UrlEncodeFunction(DataChunk &args, ExpressionState &state, Vector &result) { |
| 56 | + auto &input_vector = args.data[0]; |
| 57 | + auto result_data = FlatVector::GetData<string_t>(result); |
| 58 | + auto &result_validity = FlatVector::Validity(result); |
| 59 | + |
| 60 | + for (idx_t i = 0; i < args.size(); i++) { |
| 61 | + auto value = input_vector.GetValue(i); |
| 62 | + if (value.IsNull()) { |
| 63 | + result_validity.SetInvalid(i); |
| 64 | + continue; |
| 65 | + } |
| 66 | + |
| 67 | + auto input = value.ToString(); |
| 68 | + auto encoded = netquack::UrlEncode(input); |
| 69 | + result_data[i] = StringVector::AddString(result, encoded); |
| 70 | + } |
| 71 | +} |
| 72 | + |
| 73 | +void UrlDecodeFunction(DataChunk &args, ExpressionState &state, Vector &result) { |
| 74 | + auto &input_vector = args.data[0]; |
| 75 | + auto result_data = FlatVector::GetData<string_t>(result); |
| 76 | + auto &result_validity = FlatVector::Validity(result); |
| 77 | + |
| 78 | + for (idx_t i = 0; i < args.size(); i++) { |
| 79 | + auto value = input_vector.GetValue(i); |
| 80 | + if (value.IsNull()) { |
| 81 | + result_validity.SetInvalid(i); |
| 82 | + continue; |
| 83 | + } |
| 84 | + |
| 85 | + auto input = value.ToString(); |
| 86 | + auto decoded = netquack::UrlDecode(input); |
| 87 | + result_data[i] = StringVector::AddString(result, decoded); |
| 88 | + } |
| 89 | +} |
| 90 | + |
| 91 | +namespace netquack { |
| 92 | + |
| 93 | +std::string UrlEncode(const std::string &input) { |
| 94 | + if (input.empty()) { |
| 95 | + return ""; |
| 96 | + } |
| 97 | + |
| 98 | + std::string result; |
| 99 | + // Worst case: every byte becomes %XX (3x expansion) |
| 100 | + result.reserve(input.size() * 3); |
| 101 | + |
| 102 | + for (unsigned char c : input) { |
| 103 | + if (UNRESERVED_TABLE[c]) { |
| 104 | + result += static_cast<char>(c); |
| 105 | + } else { |
| 106 | + result += '%'; |
| 107 | + result += HEX_DIGITS[(c >> 4) & 0x0F]; |
| 108 | + result += HEX_DIGITS[c & 0x0F]; |
| 109 | + } |
| 110 | + } |
| 111 | + |
| 112 | + return result; |
| 113 | +} |
| 114 | + |
| 115 | +std::string UrlDecode(const std::string &input) { |
| 116 | + if (input.empty()) { |
| 117 | + return ""; |
| 118 | + } |
| 119 | + |
| 120 | + std::string result; |
| 121 | + result.reserve(input.size()); |
| 122 | + |
| 123 | + for (size_t i = 0; i < input.size(); i++) { |
| 124 | + if (input[i] == '%' && i + 2 < input.size()) { |
| 125 | + int hi = HexVal(input[i + 1]); |
| 126 | + int lo = HexVal(input[i + 2]); |
| 127 | + if (hi >= 0 && lo >= 0) { |
| 128 | + result += static_cast<char>((hi << 4) | lo); |
| 129 | + i += 2; |
| 130 | + continue; |
| 131 | + } |
| 132 | + // Invalid hex digits — pass through the '%' literally |
| 133 | + result += '%'; |
| 134 | + } else if (input[i] == '+') { |
| 135 | + // '+' is commonly used as space in application/x-www-form-urlencoded |
| 136 | + result += ' '; |
| 137 | + } else { |
| 138 | + result += input[i]; |
| 139 | + } |
| 140 | + } |
| 141 | + |
| 142 | + return result; |
| 143 | +} |
| 144 | + |
| 145 | +} // namespace netquack |
| 146 | +} // namespace duckdb |
0 commit comments