1 // SPDX-License-Identifier: GPL-2.0
5 #include "util/compress.h"
6 #include "util/debug.h"
8 int zstd_init(struct zstd_data
*data
, int level
)
10 data
->comp_level
= level
;
16 int zstd_fini(struct zstd_data
*data
)
19 ZSTD_freeDStream(data
->dstream
);
24 ZSTD_freeCStream(data
->cstream
);
31 ssize_t
zstd_compress_stream_to_records(struct zstd_data
*data
, void *dst
, size_t dst_size
,
32 void *src
, size_t src_size
, size_t max_record_size
,
33 size_t process_header(void *record
, size_t increment
))
35 size_t ret
, size
, compressed
= 0;
36 ZSTD_inBuffer input
= { src
, src_size
, 0 };
37 ZSTD_outBuffer output
;
41 data
->cstream
= ZSTD_createCStream();
42 if (data
->cstream
== NULL
) {
43 pr_err("Couldn't create compression stream.\n");
47 ret
= ZSTD_initCStream(data
->cstream
, data
->comp_level
);
48 if (ZSTD_isError(ret
)) {
49 pr_err("Failed to initialize compression stream: %s\n",
50 ZSTD_getErrorName(ret
));
55 while (input
.pos
< input
.size
) {
57 size
= process_header(record
, 0);
61 output
= (ZSTD_outBuffer
){ dst
, (dst_size
> max_record_size
) ?
62 max_record_size
: dst_size
, 0 };
63 ret
= ZSTD_compressStream(data
->cstream
, &output
, &input
);
64 ZSTD_flushStream(data
->cstream
, &output
);
65 if (ZSTD_isError(ret
)) {
66 pr_err("failed to compress %ld bytes: %s\n",
67 (long)src_size
, ZSTD_getErrorName(ret
));
68 memcpy(dst
, src
, src_size
);
72 size
= process_header(record
, size
);
81 size_t zstd_decompress_stream(struct zstd_data
*data
, void *src
, size_t src_size
,
82 void *dst
, size_t dst_size
)
85 ZSTD_inBuffer input
= { src
, src_size
, 0 };
86 ZSTD_outBuffer output
= { dst
, dst_size
, 0 };
89 data
->dstream
= ZSTD_createDStream();
90 if (data
->dstream
== NULL
) {
91 pr_err("Couldn't create decompression stream.\n");
95 ret
= ZSTD_initDStream(data
->dstream
);
96 if (ZSTD_isError(ret
)) {
97 pr_err("Failed to initialize decompression stream: %s\n",
98 ZSTD_getErrorName(ret
));
102 while (input
.pos
< input
.size
) {
103 ret
= ZSTD_decompressStream(data
->dstream
, &output
, &input
);
104 if (ZSTD_isError(ret
)) {
105 pr_err("failed to decompress (B): %zd -> %zd, dst_size %zd : %s\n",
106 src_size
, output
.size
, dst_size
, ZSTD_getErrorName(ret
));
109 output
.dst
= dst
+ output
.pos
;
110 output
.size
= dst_size
- output
.pos
;