ClickHouse/src/Functions/FunctionBase58Conversion.h

164 lines
5.1 KiB
C++
Raw Normal View History

2022-06-15 06:49:55 +00:00
#pragma once
2022-07-16 16:58:47 +00:00
#include <Columns/ColumnConst.h>
#include <Common/MemorySanitizer.h>
#include <Columns/ColumnString.h>
#include <DataTypes/DataTypeString.h>
#include <Functions/FunctionFactory.h>
#include <Functions/FunctionHelpers.h>
#include <IO/WriteHelpers.h>
#include <Common/base58.h>
#include <cstring>
2022-06-15 06:49:55 +00:00
namespace DB
{
namespace ErrorCodes
{
extern const int ILLEGAL_COLUMN;
extern const int ILLEGAL_TYPE_OF_ARGUMENT;
extern const int BAD_ARGUMENTS;
}
struct Base58Encode
{
static constexpr auto name = "base58Encode";
2022-07-16 16:58:47 +00:00
static void process(const ColumnString & src_column, ColumnString::MutablePtr & dst_column, size_t input_rows_count)
2022-06-15 06:49:55 +00:00
{
2022-06-16 10:11:41 +00:00
auto & dst_data = dst_column->getChars();
auto & dst_offsets = dst_column->getOffsets();
2022-07-16 16:58:47 +00:00
/// Base58 has efficiency of 73% (8/11) [https://monerodocs.org/cryptography/base58/],
/// and we take double scale to avoid any reallocation.
size_t max_result_size = ceil(2 * src_column.getChars().size() + 1);
2022-06-16 10:11:41 +00:00
2022-07-16 16:58:47 +00:00
dst_data.resize(max_result_size);
2022-06-16 10:11:41 +00:00
dst_offsets.resize(input_rows_count);
2022-07-16 16:58:47 +00:00
const ColumnString::Offsets & src_offsets = src_column.getOffsets();
2022-06-16 10:11:41 +00:00
2022-07-16 16:58:47 +00:00
const auto * source = src_column.getChars().data();
2022-06-16 10:11:41 +00:00
auto * dst = dst_data.data();
auto * dst_pos = dst;
size_t src_offset_prev = 0;
2022-06-15 06:49:55 +00:00
2022-06-16 10:11:41 +00:00
for (size_t row = 0; row < input_rows_count; ++row)
{
size_t srclen = src_offsets[row] - src_offset_prev - 1;
2022-07-16 16:58:47 +00:00
encodeBase58(source, dst_pos);
size_t encoded_length = strlen(reinterpret_cast<const char *>(dst_pos));
2022-06-16 10:11:41 +00:00
source += srclen + 1;
2022-07-16 16:58:47 +00:00
dst_pos += encoded_length + 1;
2022-06-16 10:11:41 +00:00
dst_offsets[row] = dst_pos - dst;
src_offset_prev = src_offsets[row];
}
dst_data.resize(dst_pos - dst);
2022-06-15 06:49:55 +00:00
}
};
struct Base58Decode
{
static constexpr auto name = "base58Decode";
2022-07-16 16:58:47 +00:00
static void process(const ColumnString & src_column, ColumnString::MutablePtr & dst_column, size_t input_rows_count)
2022-06-15 06:49:55 +00:00
{
2022-06-16 20:52:45 +00:00
auto & dst_data = dst_column->getChars();
auto & dst_offsets = dst_column->getOffsets();
2022-06-15 06:49:55 +00:00
2022-07-16 16:58:47 +00:00
/// Base58 has efficiency of 73% (8/11) [https://monerodocs.org/cryptography/base58/],
/// and decoded value will be no longer than source.
size_t max_result_size = src_column.getChars().size() + 1;
2022-06-15 06:49:55 +00:00
2022-07-16 16:58:47 +00:00
dst_data.resize(max_result_size);
2022-06-16 20:52:45 +00:00
dst_offsets.resize(input_rows_count);
2022-07-16 16:58:47 +00:00
const ColumnString::Offsets & src_offsets = src_column.getOffsets();
2022-06-16 20:52:45 +00:00
2022-07-16 16:58:47 +00:00
const auto * source = src_column.getChars().data();
2022-06-16 20:52:45 +00:00
auto * dst = dst_data.data();
auto * dst_pos = dst;
size_t src_offset_prev = 0;
for (size_t row = 0; row < input_rows_count; ++row)
{
size_t srclen = src_offsets[row] - src_offset_prev - 1;
2022-07-16 16:58:47 +00:00
if (!decodeBase58(source, dst_pos))
throw Exception("Invalid Base58 value, cannot be decoded", ErrorCodes::BAD_ARGUMENTS);
size_t encoded_length = strlen(reinterpret_cast<const char *>(dst_pos));
2022-06-16 20:52:45 +00:00
source += srclen + 1;
2022-07-16 16:58:47 +00:00
dst_pos += encoded_length + 1;
2022-06-16 20:52:45 +00:00
dst_offsets[row] = dst_pos - dst;
src_offset_prev = src_offsets[row];
}
dst_data.resize(dst_pos - dst);
2022-06-15 06:49:55 +00:00
}
};
template <typename Func>
class FunctionBase58Conversion : public IFunction
{
public:
static constexpr auto name = Func::name;
static FunctionPtr create(ContextPtr)
{
return std::make_shared<FunctionBase58Conversion>();
}
String getName() const override
{
return Func::name;
}
2022-07-16 16:58:47 +00:00
size_t getNumberOfArguments() const override { return 1; }
2022-06-15 06:49:55 +00:00
bool isSuitableForShortCircuitArgumentsExecution(const DataTypesWithConstInfo & /*arguments*/) const override { return true; }
bool useDefaultImplementationForConstants() const override { return true; }
ColumnNumbers getArgumentsThatAreAlwaysConstant() const override { return {1}; }
DataTypePtr getReturnTypeImpl(const ColumnsWithTypeAndName & arguments) const override
{
2022-07-16 16:58:47 +00:00
if (arguments.size() != 1)
throw Exception("Wrong number of arguments for function " + getName() + ": 1 expected.", ErrorCodes::BAD_ARGUMENTS);
2022-06-15 06:49:55 +00:00
if (!isString(arguments[0].type))
throw Exception(
2022-07-16 16:58:47 +00:00
"Illegal type " + arguments[0].type->getName() + " of first argument of function " + getName() + ". Must be String.",
2022-06-15 06:49:55 +00:00
ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT);
return std::make_shared<DataTypeString>();
}
ColumnPtr executeImpl(const ColumnsWithTypeAndName & arguments, const DataTypePtr &, size_t input_rows_count) const override
{
const ColumnPtr column_string = arguments[0].column;
const ColumnString * input = checkAndGetColumn<ColumnString>(column_string.get());
if (!input)
throw Exception(
2022-06-20 14:30:54 +00:00
"Illegal column " + arguments[0].column->getName() + " of first argument of function " + getName() + ", must be String",
2022-06-15 06:49:55 +00:00
ErrorCodes::ILLEGAL_COLUMN);
auto dst_column = ColumnString::create();
2022-07-16 16:58:47 +00:00
Func::process(*input, dst_column, input_rows_count);
2022-06-15 06:49:55 +00:00
return dst_column;
}
};
}