Merge pull request 'Streaming zstd: switch to ZSTD_compressStream2 / ZSTD_decompressStream' (#7) from streaming-zstd into main

Reviewed-on: #7
This commit was merged in pull request #7.
This commit is contained in:
2026-07-16 12:31:49 +02:00
+49 -19
View File
@@ -6,18 +6,31 @@
Data *data_compress(Data *data_to_compress, int compression_level) { Data *data_compress(Data *data_to_compress, int compression_level) {
log_message(LOG_LEVEL_DEBUG, "Starting to compress data"); log_message(LOG_LEVEL_DEBUG, "Starting to compress data");
Data *compressed_data = size_t dst_size = ZSTD_compressBound(data_to_compress->size);
data_create_empty(ZSTD_compressBound(data_to_compress->size)); Data *compressed_data = data_create_empty(dst_size);
compressed_data->size = ZSTD_compress( ZSTD_CCtx *cctx = ZSTD_createCCtx();
compressed_data->data, compressed_data->size, data_to_compress->data, if (!cctx) {
data_to_compress->size, compression_level); log_message(LOG_LEVEL_ERROR, "Failed to create ZSTD compression context");
if (ZSTD_isError(compressed_data->size)) {
log_message(LOG_LEVEL_ERROR, "Compression failed: %s",
ZSTD_getErrorName(compressed_data->size));
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
ZSTD_inBuffer input = {data_to_compress->data, data_to_compress->size, 0};
ZSTD_outBuffer output = {compressed_data->data, dst_size, 0};
size_t ret;
do {
ret = ZSTD_compressStream2(cctx, &output, &input, ZSTD_e_end);
if (ZSTD_isError(ret)) {
log_message(LOG_LEVEL_ERROR, "Compression failed: %s",
ZSTD_getErrorName(ret));
exit(EXIT_FAILURE);
}
} while (ret > 0);
compressed_data->size = output.pos;
ZSTD_freeCCtx(cctx);
log_message(LOG_LEVEL_DEBUG, "Data succesfully compressed from %zu to %zu", log_message(LOG_LEVEL_DEBUG, "Data succesfully compressed from %zu to %zu",
data_to_compress->size, compressed_data->size); data_to_compress->size, compressed_data->size);
return compressed_data; return compressed_data;
@@ -25,22 +38,39 @@ Data *data_compress(Data *data_to_compress, int compression_level) {
Data *data_decompress(Data *compressed_data) { Data *data_decompress(Data *compressed_data) {
log_message(LOG_LEVEL_DEBUG, "Start to decompress data"); log_message(LOG_LEVEL_DEBUG, "Start to decompress data");
Data *uncompressed_data = data_create_empty( unsigned long long dst_size = ZSTD_getFrameContentSize(
ZSTD_getFrameContentSize(compressed_data->data, compressed_data->size)); compressed_data->data, compressed_data->size);
if (ZSTD_isError(uncompressed_data->size)) { if (ZSTD_isError(dst_size)) {
log_message(LOG_LEVEL_ERROR, "Decompression failed: %s", log_message(LOG_LEVEL_ERROR, "Failed to get decompressed size: %s",
ZSTD_getErrorName(uncompressed_data->size)); ZSTD_getErrorName(dst_size));
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
uncompressed_data->size = Data *uncompressed_data = data_create_empty((size_t)dst_size);
ZSTD_decompress(uncompressed_data->data, uncompressed_data->size,
compressed_data->data, compressed_data->size); ZSTD_DCtx *dctx = ZSTD_createDCtx();
if (ZSTD_isError(uncompressed_data->size)) { if (!dctx) {
log_message(LOG_LEVEL_ERROR, "Decompression failed: %s", log_message(LOG_LEVEL_ERROR,
ZSTD_getErrorName(uncompressed_data->size)); "Failed to create ZSTD decompression context");
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
ZSTD_inBuffer input = {compressed_data->data, compressed_data->size, 0};
ZSTD_outBuffer output = {uncompressed_data->data, (size_t)dst_size, 0};
size_t ret;
do {
ret = ZSTD_decompressStream(dctx, &output, &input);
if (ZSTD_isError(ret)) {
log_message(LOG_LEVEL_ERROR, "Decompression failed: %s",
ZSTD_getErrorName(ret));
exit(EXIT_FAILURE);
}
} while (ret > 0);
uncompressed_data->size = output.pos;
ZSTD_freeDCtx(dctx);
log_message(LOG_LEVEL_DEBUG, "Decompressed data successfully"); log_message(LOG_LEVEL_DEBUG, "Decompressed data successfully");
return uncompressed_data; return uncompressed_data;
} }