mirror of
https://github.com/ClickHouse/ClickHouse.git
synced 2024-11-22 07:31:57 +00:00
Minor fixups
This commit is contained in:
parent
fdb32e825c
commit
71a42d427a
@ -12,7 +12,7 @@
|
||||
# include <Common/MemorySanitizer.h>
|
||||
|
||||
# include <cstddef>
|
||||
# include <span>
|
||||
# include <string_view>
|
||||
|
||||
namespace DB
|
||||
{
|
||||
@ -28,13 +28,15 @@ enum class Base64Variant : uint8_t
|
||||
Url
|
||||
};
|
||||
|
||||
inline std::string preprocessBase64Url(std::span<const UInt8> src)
|
||||
inline std::string preprocessBase64Url(std::string_view src)
|
||||
{
|
||||
std::string padded_src;
|
||||
padded_src.reserve(src.size() + 3);
|
||||
|
||||
// Do symbol substitution as described in https://datatracker.ietf.org/doc/html/rfc4648#page-7
|
||||
for (size_t i = 0; i < src.size(); ++i)
|
||||
for (auto s : src)
|
||||
{
|
||||
switch (src[i])
|
||||
switch (s)
|
||||
{
|
||||
case '_':
|
||||
padded_src += '/';
|
||||
@ -43,12 +45,12 @@ inline std::string preprocessBase64Url(std::span<const UInt8> src)
|
||||
padded_src += '+';
|
||||
break;
|
||||
default:
|
||||
padded_src += src[i];
|
||||
padded_src += s;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
// insert padding to please aklomp library
|
||||
/// Insert padding to please aklomp library
|
||||
size_t remainder = src.size() % 4;
|
||||
switch (remainder)
|
||||
{
|
||||
@ -100,10 +102,10 @@ struct Base64Encode
|
||||
return ((string_length - string_count) / 3 + string_count) * 4 + string_count;
|
||||
}
|
||||
|
||||
static size_t perform(std::span<const UInt8> src, UInt8 * dst)
|
||||
static size_t perform(std::string_view src, UInt8 * dst)
|
||||
{
|
||||
size_t outlen = 0;
|
||||
base64_encode(reinterpret_cast<const char *>(src.data()), src.size(), reinterpret_cast<char *>(dst), &outlen, 0);
|
||||
base64_encode(src.data(), src.size(), reinterpret_cast<char *>(dst), &outlen, 0);
|
||||
|
||||
if constexpr (variant == Base64Variant::Url)
|
||||
outlen = postprocessBase64Url(dst, outlen);
|
||||
@ -122,7 +124,7 @@ struct Base64Decode
|
||||
return ((string_length - string_count) / 4 + string_count) * 3 + string_count;
|
||||
}
|
||||
|
||||
static size_t perform(std::span<const UInt8> src, UInt8 * dst)
|
||||
static size_t perform(std::string_view src, UInt8 * dst)
|
||||
{
|
||||
int rc;
|
||||
size_t outlen = 0;
|
||||
@ -133,7 +135,7 @@ struct Base64Decode
|
||||
}
|
||||
else
|
||||
{
|
||||
rc = base64_decode(reinterpret_cast<const char *>(src.data()), src.size(), reinterpret_cast<char *>(dst), &outlen, 0);
|
||||
rc = base64_decode(src.data(), src.size(), reinterpret_cast<char *>(dst), &outlen, 0);
|
||||
}
|
||||
|
||||
if (rc != 1)
|
||||
@ -157,7 +159,7 @@ struct TryBase64Decode
|
||||
return Base64Decode<variant>::getBufferSize(string_length, string_count);
|
||||
}
|
||||
|
||||
static size_t perform(std::span<const UInt8> src, UInt8 * dst)
|
||||
static size_t perform(std::string_view src, UInt8 * dst)
|
||||
{
|
||||
int rc;
|
||||
size_t outlen = 0;
|
||||
@ -168,7 +170,7 @@ struct TryBase64Decode
|
||||
}
|
||||
else
|
||||
{
|
||||
rc = base64_decode(reinterpret_cast<const char *>(src.data()), src.size(), reinterpret_cast<char *>(dst), &outlen, 0);
|
||||
rc = base64_decode(src.data(), src.size(), reinterpret_cast<char *>(dst), &outlen, 0);
|
||||
}
|
||||
|
||||
if (rc != 1)
|
||||
@ -232,7 +234,7 @@ private:
|
||||
|
||||
auto * dst = dst_chars.data();
|
||||
auto * dst_pos = dst;
|
||||
const auto * src = src_chars.data();
|
||||
const auto * src = reinterpret_cast<const char *>(src_chars.data());
|
||||
|
||||
size_t src_offset_prev = 0;
|
||||
for (size_t row = 0; row < src_row_count; ++row)
|
||||
@ -272,7 +274,7 @@ private:
|
||||
|
||||
auto * dst = dst_chars.data();
|
||||
auto * dst_pos = dst;
|
||||
const auto * src = src_chars.data();
|
||||
const auto * src = reinterpret_cast<const char *>(src_chars.data());
|
||||
|
||||
for (size_t row = 0; row < src_row_count; ++row)
|
||||
{
|
||||
|
@ -1,6 +1,5 @@
|
||||
-- Tags: no-fasttest
|
||||
|
||||
SET send_logs_level = 'fatal';
|
||||
-- no-fasttest because aklomp-base64 library is required
|
||||
|
||||
SELECT base64Encode(); -- { serverError NUMBER_OF_ARGUMENTS_DOESNT_MATCH }
|
||||
SELECT base64Decode(); -- { serverError NUMBER_OF_ARGUMENTS_DOESNT_MATCH }
|
||||
|
@ -1,4 +1,5 @@
|
||||
-- Tags: no-fasttest
|
||||
-- no-fasttest because aklomp-base64 library is required
|
||||
|
||||
-- incorrect number of arguments
|
||||
SELECT base64UrlEncode(); -- { serverError NUMBER_OF_ARGUMENTS_DOESNT_MATCH }
|
||||
@ -9,22 +10,20 @@ SELECT base64UrlDecode('foo', 'excess argument'); -- { serverError NUMBER_OF_ARG
|
||||
SELECT tryBase64UrlDecode('foo', 'excess argument'); -- { serverError NUMBER_OF_ARGUMENTS_DOESNT_MATCH }
|
||||
|
||||
-- test with valid inputs
|
||||
SELECT 'https://clickhouse.com' as original, base64UrlEncode(original) as encoded, base64UrlDecode(encoded) as decoded, tryBase64UrlDecode(encoded) as try_decoded;
|
||||
|
||||
-- encoding differs from base64Encode
|
||||
SELECT '12?' as original, base64UrlEncode(original) as encoded, base64UrlDecode(encoded) as decoded, tryBase64UrlDecode(encoded) as try_decoded;
|
||||
SELECT 'https://clickhouse.com' AS original, base64UrlEncode(original) AS encoded, base64UrlDecode(encoded), tryBase64UrlDecode(encoded);
|
||||
SELECT '12?' AS original, base64UrlEncode(original) AS encoded, base64UrlDecode(encoded), tryBase64UrlDecode(encoded);
|
||||
SELECT 'https://www.google.com/search?q=clickhouse+base64+decode&sca_esv=739f8bb380e4c7ed&ei=TfRiZqCDIrmnwPAP2KLRkA8&ved=0ahUKEwjg3ZHitsmGAxW5ExAIHVhRFPIQ4dUDCBA&uact=5&oq=clickhouse+base64+decode' AS original, base64UrlEncode(original) AS encoded, base64UrlDecode(encoded), tryBase64UrlDecode(encoded);
|
||||
|
||||
-- long string
|
||||
SELECT 'https://www.google.com/search?q=clickhouse+base64+decode&sca_esv=739f8bb380e4c7ed&ei=TfRiZqCDIrmnwPAP2KLRkA8&ved=0ahUKEwjg3ZHitsmGAxW5ExAIHVhRFPIQ4dUDCBA&uact=5&oq=clickhouse+base64+decode' as original, base64UrlEncode(original) as encoded, base64UrlDecode(encoded) as decoded, tryBase64UrlDecode(encoded) as try_decoded;
|
||||
-- encoded value has no padding
|
||||
SELECT 'aHR0cHM6Ly9jbGlj' AS encoded, base64UrlDecode(encoded), tryBase64UrlDecode(encoded);
|
||||
-- encoded value has one-byte padding
|
||||
SELECT 'aHR0cHM6Ly9jbGlja2g' AS encoded, base64UrlDecode(encoded), tryBase64UrlDecode(encoded);
|
||||
-- encoded value has two-bytes padding
|
||||
SELECT 'aHR0cHM6Ly9jbGljaw' AS encoded, base64UrlDecode(encoded), tryBase64UrlDecode(encoded);
|
||||
|
||||
-- no padding
|
||||
SELECT 'aHR0cHM6Ly9jbGlj' as encoded, base64UrlDecode(encoded) as decoded, tryBase64UrlDecode(encoded) as try_decoded;
|
||||
-- one-byte padding
|
||||
SELECT 'aHR0cHM6Ly9jbGlja2g' as encoded, base64UrlDecode(encoded) as decoded, tryBase64UrlDecode(encoded) as try_decoded;
|
||||
-- two-bytes padding
|
||||
SELECT 'aHR0cHM6Ly9jbGljaw' as encoded, base64UrlDecode(encoded) as decoded, tryBase64UrlDecode(encoded) as try_decoded;
|
||||
-- test with invalid inputs
|
||||
|
||||
-- invalid inputs
|
||||
SELECT base64UrlDecode('https://clickhouse.com'); -- { serverError INCORRECT_DATA }
|
||||
SELECT tryBase64UrlDecode('https://clickhouse.com');
|
||||
SELECT base64UrlDecode('12?'); -- { serverError INCORRECT_DATA }
|
||||
@ -33,4 +32,5 @@ SELECT base64UrlDecode('aHR0cHM6Ly9jbGlja'); -- { serverError INCORRECT_DATA }
|
||||
SELECT tryBase64UrlDecode('aHR0cHM6Ly9jbGlja');
|
||||
|
||||
-- test FixedString argument
|
||||
SELECT toFixedString('https://clickhouse.com', 22) as original, base64UrlEncode(original) as encoded, base64UrlDecode(encoded) as decoded, tryBase64UrlDecode(encoded) as try_decoded;
|
||||
|
||||
SELECT toFixedString('https://clickhouse.com', 22) AS original, base64UrlEncode(original) AS encoded, base64UrlDecode(encoded), tryBase64UrlDecode(encoded);
|
||||
|
Loading…
Reference in New Issue
Block a user