50class Lz4RawCodec :
public CompressionCodec {
59 [[nodiscard]] expected<std::vector<uint8_t>>
compress(
60 const uint8_t* data,
size_t size)
const override {
63 return std::vector<uint8_t>{};
67 if (size >
static_cast<size_t>((std::numeric_limits<int>::max)())) {
68 return Error{ErrorCode::INTERNAL_ERROR,
69 "LZ4: input exceeds int32 limit"};
73 int max_compressed = LZ4_compressBound(
static_cast<int>(size));
74 if (max_compressed <= 0) {
75 return Error{ErrorCode::INTERNAL_ERROR,
76 "LZ4: input too large for LZ4_compressBound"};
79 std::vector<uint8_t> out(
static_cast<size_t>(max_compressed));
81 int compressed_size = LZ4_compress_default(
82 reinterpret_cast<const char*
>(data),
83 reinterpret_cast<char*
>(out.data()),
84 static_cast<int>(size),
87 if (compressed_size <= 0) {
88 return Error{ErrorCode::INTERNAL_ERROR,
89 "LZ4 compress failed (returned " +
90 std::to_string(compressed_size) +
")"};
93 out.resize(
static_cast<size_t>(compressed_size));
105 [[nodiscard]] expected<std::vector<uint8_t>>
decompress(
106 const uint8_t* data,
size_t size,
107 size_t uncompressed_size)
const override {
109 static constexpr size_t MAX_DECOMPRESS_SIZE = 256 * 1024 * 1024;
110 if (uncompressed_size > MAX_DECOMPRESS_SIZE)
111 return Error{ErrorCode::INVALID_ARGUMENT,
"Decompression size exceeds 256 MB limit"};
113 if (uncompressed_size == 0) {
114 return std::vector<uint8_t>{};
118 if (size >
static_cast<size_t>((std::numeric_limits<int>::max)())) {
119 return Error{ErrorCode::INTERNAL_ERROR,
120 "LZ4: compressed input exceeds int32 limit"};
123 if (uncompressed_size >
static_cast<size_t>((std::numeric_limits<int>::max)())) {
124 return Error{ErrorCode::INTERNAL_ERROR,
125 "LZ4: uncompressed size exceeds int32 limit"};
128 std::vector<uint8_t> out(uncompressed_size);
130 int decompressed_size = LZ4_decompress_safe(
131 reinterpret_cast<const char*
>(data),
132 reinterpret_cast<char*
>(out.data()),
133 static_cast<int>(size),
134 static_cast<int>(uncompressed_size));
136 if (decompressed_size < 0) {
137 return Error{ErrorCode::CORRUPT_PAGE,
138 "LZ4 decompress failed (returned " +
139 std::to_string(decompressed_size) +
")"};
142 if (
static_cast<size_t>(decompressed_size) != uncompressed_size) {
143 return Error{ErrorCode::CORRUPT_PAGE,
144 "LZ4: decompressed " +
145 std::to_string(decompressed_size) +
146 " bytes but expected " +
147 std::to_string(uncompressed_size)};
157 [[nodiscard]]
Compression codec_type()
const override {
158 return Compression::LZ4_RAW;
162 [[nodiscard]]
const char* name()
const override {
179inline void register_lz4_codec() {
void register_codec(std::unique_ptr< CompressionCodec > codec)
Register a codec, transferring ownership to the registry.
static CodecRegistry & instance()
Access the process-wide singleton instance.
Compression codec interface and registry for Signet Forge.
Compression
Parquet compression codecs.
expected< std::vector< uint8_t > > decompress(Compression codec, const uint8_t *data, size_t size, size_t uncompressed_size)
Decompress data using the specified codec via the global CodecRegistry.
expected< std::vector< uint8_t > > compress(Compression codec, const uint8_t *data, size_t size)
Compress data using the specified codec via the global CodecRegistry.