[BugFix] lz4 encode/decode (#63629)

Signed-off-by: jervisli <jervisli@tencent.com>
This commit is contained in:
IHEII 2025-09-29 19:12:14 +08:00 committed by GitHub
parent d10eae9d79
commit b80cda36ec
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 8 additions and 5 deletions

View File

@ -118,11 +118,12 @@ uint8_t* encode_string_lz4(const void* data, size_t size, uint8_t* buff, int enc
throw std::runtime_error(
fmt::format("The input size for compression should be less than {}", LZ4_MAX_INPUT_SIZE));
}
uint64_t encode_size =
auto encode_size =
LZ4_compress_fast(reinterpret_cast<const char*>(data), reinterpret_cast<char*>(buff + sizeof(uint64_t)),
size, LZ4_compressBound(size), std::max(1, std::abs(encode_level / 10000) % 100));
if (encode_size <= 0) {
throw std::runtime_error("lz4 compress error.");
throw std::runtime_error(
fmt::format("lz4 compress failed: raw size = {}, compressed get encode size = {}.", size, encode_size));
}
buff = write_little_endian_64(encode_size, buff);
@ -135,10 +136,12 @@ uint8_t* encode_string_lz4(const void* data, size_t size, uint8_t* buff, int enc
const uint8_t* decode_string_lz4(const uint8_t* buff, void* target, size_t size) {
uint64_t encode_size = 0;
buff = read_little_endian_64(buff, &encode_size);
uint64_t decode_size = LZ4_decompress_safe(reinterpret_cast<const char*>(buff), reinterpret_cast<char*>(target),
encode_size, size);
auto decode_size = LZ4_decompress_safe(reinterpret_cast<const char*>(buff), reinterpret_cast<char*>(target),
encode_size, size);
if (decode_size <= 0) {
throw std::runtime_error("lz4 decompress error.");
throw std::runtime_error(fmt::format(
"lz4 decompress failed: encode size = {}, raw size = {}, decompressed get decode size = {}.",
encode_size, size, decode_size));
}
if (size != decode_size) {
throw std::runtime_error(