1 // SPDX-License-Identifier: GPL-2.0+ OR BSD-3-Clause
2 /*
3 * Copyright (c) Meta Platforms, Inc. and affiliates.
4 * All rights reserved.
5 *
6 * This source code is licensed under both the BSD-style license (found in the
7 * LICENSE file in the root directory of this source tree) and the GPLv2 (found
8 * in the COPYING file in the root directory of this source tree).
9 * You may select, at your option, one of the above-listed licenses.
10 */
11
12 #include <linux/kernel.h>
13 #include <linux/module.h>
14 #include <linux/string.h>
15 #include <linux/zstd.h>
16
17 #include "common/zstd_deps.h"
18 #include "common/zstd_internal.h"
19 #include "compress/zstd_compress_internal.h"
20
21 #define ZSTD_FORWARD_IF_ERR(ret) \
22 do { \
23 size_t const __ret = (ret); \
24 if (ZSTD_isError(__ret)) \
25 return __ret; \
26 } while (0)
27
zstd_cctx_init(zstd_cctx * cctx,const zstd_parameters * parameters,unsigned long long pledged_src_size)28 static size_t zstd_cctx_init(zstd_cctx *cctx, const zstd_parameters *parameters,
29 unsigned long long pledged_src_size)
30 {
31 ZSTD_FORWARD_IF_ERR(ZSTD_CCtx_reset(
32 cctx, ZSTD_reset_session_and_parameters));
33 ZSTD_FORWARD_IF_ERR(ZSTD_CCtx_setPledgedSrcSize(
34 cctx, pledged_src_size));
35 ZSTD_FORWARD_IF_ERR(ZSTD_CCtx_setParameter(
36 cctx, ZSTD_c_windowLog, parameters->cParams.windowLog));
37 ZSTD_FORWARD_IF_ERR(ZSTD_CCtx_setParameter(
38 cctx, ZSTD_c_hashLog, parameters->cParams.hashLog));
39 ZSTD_FORWARD_IF_ERR(ZSTD_CCtx_setParameter(
40 cctx, ZSTD_c_chainLog, parameters->cParams.chainLog));
41 ZSTD_FORWARD_IF_ERR(ZSTD_CCtx_setParameter(
42 cctx, ZSTD_c_searchLog, parameters->cParams.searchLog));
43 ZSTD_FORWARD_IF_ERR(ZSTD_CCtx_setParameter(
44 cctx, ZSTD_c_minMatch, parameters->cParams.minMatch));
45 ZSTD_FORWARD_IF_ERR(ZSTD_CCtx_setParameter(
46 cctx, ZSTD_c_targetLength, parameters->cParams.targetLength));
47 ZSTD_FORWARD_IF_ERR(ZSTD_CCtx_setParameter(
48 cctx, ZSTD_c_strategy, parameters->cParams.strategy));
49 ZSTD_FORWARD_IF_ERR(ZSTD_CCtx_setParameter(
50 cctx, ZSTD_c_contentSizeFlag, parameters->fParams.contentSizeFlag));
51 ZSTD_FORWARD_IF_ERR(ZSTD_CCtx_setParameter(
52 cctx, ZSTD_c_checksumFlag, parameters->fParams.checksumFlag));
53 ZSTD_FORWARD_IF_ERR(ZSTD_CCtx_setParameter(
54 cctx, ZSTD_c_dictIDFlag, !parameters->fParams.noDictIDFlag));
55 return 0;
56 }
57
zstd_min_clevel(void)58 int zstd_min_clevel(void)
59 {
60 return ZSTD_minCLevel();
61 }
62 EXPORT_SYMBOL(zstd_min_clevel);
63
zstd_max_clevel(void)64 int zstd_max_clevel(void)
65 {
66 return ZSTD_maxCLevel();
67 }
68 EXPORT_SYMBOL(zstd_max_clevel);
69
zstd_compress_bound(size_t src_size)70 size_t zstd_compress_bound(size_t src_size)
71 {
72 return ZSTD_compressBound(src_size);
73 }
74 EXPORT_SYMBOL(zstd_compress_bound);
75
zstd_get_params(int level,unsigned long long estimated_src_size)76 zstd_parameters zstd_get_params(int level,
77 unsigned long long estimated_src_size)
78 {
79 return ZSTD_getParams(level, estimated_src_size, 0);
80 }
81 EXPORT_SYMBOL(zstd_get_params);
82
zstd_cctx_set_param(zstd_cctx * cctx,ZSTD_cParameter param,int value)83 size_t zstd_cctx_set_param(zstd_cctx *cctx, ZSTD_cParameter param, int value)
84 {
85 return ZSTD_CCtx_setParameter(cctx, param, value);
86 }
87 EXPORT_SYMBOL(zstd_cctx_set_param);
88
zstd_cctx_workspace_bound(const zstd_compression_parameters * cparams)89 size_t zstd_cctx_workspace_bound(const zstd_compression_parameters *cparams)
90 {
91 return ZSTD_estimateCCtxSize_usingCParams(*cparams);
92 }
93 EXPORT_SYMBOL(zstd_cctx_workspace_bound);
94
95 // Used by zstd_cctx_workspace_bound_with_ext_seq_prod()
dummy_external_sequence_producer(void * sequenceProducerState,ZSTD_Sequence * outSeqs,size_t outSeqsCapacity,const void * src,size_t srcSize,const void * dict,size_t dictSize,int compressionLevel,size_t windowSize)96 static size_t dummy_external_sequence_producer(
97 void *sequenceProducerState,
98 ZSTD_Sequence *outSeqs, size_t outSeqsCapacity,
99 const void *src, size_t srcSize,
100 const void *dict, size_t dictSize,
101 int compressionLevel,
102 size_t windowSize)
103 {
104 (void)sequenceProducerState;
105 (void)outSeqs; (void)outSeqsCapacity;
106 (void)src; (void)srcSize;
107 (void)dict; (void)dictSize;
108 (void)compressionLevel;
109 (void)windowSize;
110 return ZSTD_SEQUENCE_PRODUCER_ERROR;
111 }
112
init_cctx_params_from_compress_params(ZSTD_CCtx_params * cctx_params,const zstd_compression_parameters * compress_params)113 static void init_cctx_params_from_compress_params(
114 ZSTD_CCtx_params *cctx_params,
115 const zstd_compression_parameters *compress_params)
116 {
117 ZSTD_parameters zstd_params;
118 memset(&zstd_params, 0, sizeof(zstd_params));
119 zstd_params.cParams = *compress_params;
120 ZSTD_CCtxParams_init_advanced(cctx_params, zstd_params);
121 }
122
zstd_cctx_workspace_bound_with_ext_seq_prod(const zstd_compression_parameters * compress_params)123 size_t zstd_cctx_workspace_bound_with_ext_seq_prod(const zstd_compression_parameters *compress_params)
124 {
125 ZSTD_CCtx_params cctx_params;
126 init_cctx_params_from_compress_params(&cctx_params, compress_params);
127 ZSTD_CCtxParams_registerSequenceProducer(&cctx_params, NULL, dummy_external_sequence_producer);
128 return ZSTD_estimateCCtxSize_usingCCtxParams(&cctx_params);
129 }
130 EXPORT_SYMBOL(zstd_cctx_workspace_bound_with_ext_seq_prod);
131
zstd_cstream_workspace_bound_with_ext_seq_prod(const zstd_compression_parameters * compress_params)132 size_t zstd_cstream_workspace_bound_with_ext_seq_prod(const zstd_compression_parameters *compress_params)
133 {
134 ZSTD_CCtx_params cctx_params;
135 init_cctx_params_from_compress_params(&cctx_params, compress_params);
136 ZSTD_CCtxParams_registerSequenceProducer(&cctx_params, NULL, dummy_external_sequence_producer);
137 return ZSTD_estimateCStreamSize_usingCCtxParams(&cctx_params);
138 }
139 EXPORT_SYMBOL(zstd_cstream_workspace_bound_with_ext_seq_prod);
140
zstd_init_cctx(void * workspace,size_t workspace_size)141 zstd_cctx *zstd_init_cctx(void *workspace, size_t workspace_size)
142 {
143 if (workspace == NULL)
144 return NULL;
145 return ZSTD_initStaticCCtx(workspace, workspace_size);
146 }
147 EXPORT_SYMBOL(zstd_init_cctx);
148
zstd_compress_cctx(zstd_cctx * cctx,void * dst,size_t dst_capacity,const void * src,size_t src_size,const zstd_parameters * parameters)149 size_t zstd_compress_cctx(zstd_cctx *cctx, void *dst, size_t dst_capacity,
150 const void *src, size_t src_size, const zstd_parameters *parameters)
151 {
152 ZSTD_FORWARD_IF_ERR(zstd_cctx_init(cctx, parameters, src_size));
153 return ZSTD_compress2(cctx, dst, dst_capacity, src, src_size);
154 }
155 EXPORT_SYMBOL(zstd_compress_cctx);
156
zstd_cstream_workspace_bound(const zstd_compression_parameters * cparams)157 size_t zstd_cstream_workspace_bound(const zstd_compression_parameters *cparams)
158 {
159 return ZSTD_estimateCStreamSize_usingCParams(*cparams);
160 }
161 EXPORT_SYMBOL(zstd_cstream_workspace_bound);
162
zstd_init_cstream(const zstd_parameters * parameters,unsigned long long pledged_src_size,void * workspace,size_t workspace_size)163 zstd_cstream *zstd_init_cstream(const zstd_parameters *parameters,
164 unsigned long long pledged_src_size, void *workspace, size_t workspace_size)
165 {
166 zstd_cstream *cstream;
167
168 if (workspace == NULL)
169 return NULL;
170
171 cstream = ZSTD_initStaticCStream(workspace, workspace_size);
172 if (cstream == NULL)
173 return NULL;
174
175 /* 0 means unknown in linux zstd API but means 0 in new zstd API */
176 if (pledged_src_size == 0)
177 pledged_src_size = ZSTD_CONTENTSIZE_UNKNOWN;
178
179 if (ZSTD_isError(zstd_cctx_init(cstream, parameters, pledged_src_size)))
180 return NULL;
181
182 return cstream;
183 }
184 EXPORT_SYMBOL(zstd_init_cstream);
185
zstd_reset_cstream(zstd_cstream * cstream,unsigned long long pledged_src_size)186 size_t zstd_reset_cstream(zstd_cstream *cstream,
187 unsigned long long pledged_src_size)
188 {
189 if (pledged_src_size == 0)
190 pledged_src_size = ZSTD_CONTENTSIZE_UNKNOWN;
191 ZSTD_FORWARD_IF_ERR( ZSTD_CCtx_reset(cstream, ZSTD_reset_session_only) );
192 ZSTD_FORWARD_IF_ERR( ZSTD_CCtx_setPledgedSrcSize(cstream, pledged_src_size) );
193 return 0;
194 }
195 EXPORT_SYMBOL(zstd_reset_cstream);
196
zstd_compress_stream(zstd_cstream * cstream,zstd_out_buffer * output,zstd_in_buffer * input)197 size_t zstd_compress_stream(zstd_cstream *cstream, zstd_out_buffer *output,
198 zstd_in_buffer *input)
199 {
200 return ZSTD_compressStream(cstream, output, input);
201 }
202 EXPORT_SYMBOL(zstd_compress_stream);
203
zstd_flush_stream(zstd_cstream * cstream,zstd_out_buffer * output)204 size_t zstd_flush_stream(zstd_cstream *cstream, zstd_out_buffer *output)
205 {
206 return ZSTD_flushStream(cstream, output);
207 }
208 EXPORT_SYMBOL(zstd_flush_stream);
209
zstd_end_stream(zstd_cstream * cstream,zstd_out_buffer * output)210 size_t zstd_end_stream(zstd_cstream *cstream, zstd_out_buffer *output)
211 {
212 return ZSTD_endStream(cstream, output);
213 }
214 EXPORT_SYMBOL(zstd_end_stream);
215
zstd_register_sequence_producer(zstd_cctx * cctx,void * sequence_producer_state,zstd_sequence_producer_f sequence_producer)216 void zstd_register_sequence_producer(
217 zstd_cctx *cctx,
218 void* sequence_producer_state,
219 zstd_sequence_producer_f sequence_producer
220 ) {
221 ZSTD_registerSequenceProducer(cctx, sequence_producer_state, sequence_producer);
222 }
223 EXPORT_SYMBOL(zstd_register_sequence_producer);
224
zstd_compress_sequences_and_literals(zstd_cctx * cctx,void * dst,size_t dst_capacity,const zstd_sequence * in_seqs,size_t in_seqs_size,const void * literals,size_t lit_size,size_t lit_capacity,size_t decompressed_size)225 size_t zstd_compress_sequences_and_literals(zstd_cctx *cctx, void* dst, size_t dst_capacity,
226 const zstd_sequence *in_seqs, size_t in_seqs_size,
227 const void* literals, size_t lit_size, size_t lit_capacity,
228 size_t decompressed_size)
229 {
230 return ZSTD_compressSequencesAndLiterals(cctx, dst, dst_capacity, in_seqs,
231 in_seqs_size, literals, lit_size,
232 lit_capacity, decompressed_size);
233 }
234 EXPORT_SYMBOL(zstd_compress_sequences_and_literals);
235
236 MODULE_LICENSE("Dual BSD/GPL");
237 MODULE_DESCRIPTION("Zstd Compressor");
238