File Index
Files and directories
Sign in
FILE

zstd_compress_module.c

File preview with syntax highlighting.

linux-6.19.14/lib/zstd/zstd_compress_module.c C
  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
 28static 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
 58int zstd_min_clevel(void)
 59{
 60	return ZSTD_minCLevel();
 61}
 62EXPORT_SYMBOL(zstd_min_clevel);
 63
 64int zstd_max_clevel(void)
 65{
 66	return ZSTD_maxCLevel();
 67}
 68EXPORT_SYMBOL(zstd_max_clevel);
 69
 70int zstd_default_clevel(void)
 71{
 72	return ZSTD_defaultCLevel();
 73}
 74EXPORT_SYMBOL(zstd_default_clevel);
 75
 76size_t zstd_compress_bound(size_t src_size)
 77{
 78	return ZSTD_compressBound(src_size);
 79}
 80EXPORT_SYMBOL(zstd_compress_bound);
 81
 82zstd_parameters zstd_get_params(int level,
 83	unsigned long long estimated_src_size)
 84{
 85	return ZSTD_getParams(level, estimated_src_size, 0);
 86}
 87EXPORT_SYMBOL(zstd_get_params);
 88
 89zstd_compression_parameters zstd_get_cparams(int level,
 90	unsigned long long estimated_src_size, size_t dict_size)
 91{
 92	return ZSTD_getCParams(level, estimated_src_size, dict_size);
 93}
 94EXPORT_SYMBOL(zstd_get_cparams);
 95
 96size_t zstd_cctx_set_param(zstd_cctx *cctx, ZSTD_cParameter param, int value)
 97{
 98	return ZSTD_CCtx_setParameter(cctx, param, value);
 99}
100EXPORT_SYMBOL(zstd_cctx_set_param);
101
102size_t zstd_cctx_workspace_bound(const zstd_compression_parameters *cparams)
103{
104	return ZSTD_estimateCCtxSize_usingCParams(*cparams);
105}
106EXPORT_SYMBOL(zstd_cctx_workspace_bound);
107
108// Used by zstd_cctx_workspace_bound_with_ext_seq_prod()
109static size_t dummy_external_sequence_producer(
110	void *sequenceProducerState,
111	ZSTD_Sequence *outSeqs, size_t outSeqsCapacity,
112	const void *src, size_t srcSize,
113	const void *dict, size_t dictSize,
114	int compressionLevel,
115	size_t windowSize)
116{
117	(void)sequenceProducerState;
118	(void)outSeqs; (void)outSeqsCapacity;
119	(void)src; (void)srcSize;
120	(void)dict; (void)dictSize;
121	(void)compressionLevel;
122	(void)windowSize;
123	return ZSTD_SEQUENCE_PRODUCER_ERROR;
124}
125
126static void init_cctx_params_from_compress_params(
127	ZSTD_CCtx_params *cctx_params,
128	const zstd_compression_parameters *compress_params)
129{
130	ZSTD_parameters zstd_params;
131	memset(&zstd_params, 0, sizeof(zstd_params));
132	zstd_params.cParams = *compress_params;
133	ZSTD_CCtxParams_init_advanced(cctx_params, zstd_params);
134}
135
136size_t zstd_cctx_workspace_bound_with_ext_seq_prod(const zstd_compression_parameters *compress_params)
137{
138	ZSTD_CCtx_params cctx_params;
139	init_cctx_params_from_compress_params(&cctx_params, compress_params);
140	ZSTD_CCtxParams_registerSequenceProducer(&cctx_params, NULL, dummy_external_sequence_producer);
141	return ZSTD_estimateCCtxSize_usingCCtxParams(&cctx_params);
142}
143EXPORT_SYMBOL(zstd_cctx_workspace_bound_with_ext_seq_prod);
144
145size_t zstd_cstream_workspace_bound_with_ext_seq_prod(const zstd_compression_parameters *compress_params)
146{
147	ZSTD_CCtx_params cctx_params;
148	init_cctx_params_from_compress_params(&cctx_params, compress_params);
149	ZSTD_CCtxParams_registerSequenceProducer(&cctx_params, NULL, dummy_external_sequence_producer);
150	return ZSTD_estimateCStreamSize_usingCCtxParams(&cctx_params);
151}
152EXPORT_SYMBOL(zstd_cstream_workspace_bound_with_ext_seq_prod);
153
154zstd_cctx *zstd_init_cctx(void *workspace, size_t workspace_size)
155{
156	if (workspace == NULL)
157		return NULL;
158	return ZSTD_initStaticCCtx(workspace, workspace_size);
159}
160EXPORT_SYMBOL(zstd_init_cctx);
161
162zstd_cctx *zstd_create_cctx_advanced(zstd_custom_mem custom_mem)
163{
164	return ZSTD_createCCtx_advanced(custom_mem);
165}
166EXPORT_SYMBOL(zstd_create_cctx_advanced);
167
168size_t zstd_free_cctx(zstd_cctx *cctx)
169{
170	return ZSTD_freeCCtx(cctx);
171}
172EXPORT_SYMBOL(zstd_free_cctx);
173
174zstd_cdict *zstd_create_cdict_byreference(const void *dict, size_t dict_size,
175					  zstd_compression_parameters cparams,
176					  zstd_custom_mem custom_mem)
177{
178	return ZSTD_createCDict_advanced(dict, dict_size, ZSTD_dlm_byRef,
179					 ZSTD_dct_auto, cparams, custom_mem);
180}
181EXPORT_SYMBOL(zstd_create_cdict_byreference);
182
183size_t zstd_free_cdict(zstd_cdict *cdict)
184{
185	return ZSTD_freeCDict(cdict);
186}
187EXPORT_SYMBOL(zstd_free_cdict);
188
189size_t zstd_compress_cctx(zstd_cctx *cctx, void *dst, size_t dst_capacity,
190	const void *src, size_t src_size, const zstd_parameters *parameters)
191{
192	ZSTD_FORWARD_IF_ERR(zstd_cctx_init(cctx, parameters, src_size));
193	return ZSTD_compress2(cctx, dst, dst_capacity, src, src_size);
194}
195EXPORT_SYMBOL(zstd_compress_cctx);
196
197size_t zstd_compress_using_cdict(zstd_cctx *cctx, void *dst,
198	size_t dst_capacity, const void *src, size_t src_size,
199	const ZSTD_CDict *cdict)
200{
201	return ZSTD_compress_usingCDict(cctx, dst, dst_capacity,
202					src, src_size, cdict);
203}
204EXPORT_SYMBOL(zstd_compress_using_cdict);
205
206size_t zstd_cstream_workspace_bound(const zstd_compression_parameters *cparams)
207{
208	return ZSTD_estimateCStreamSize_usingCParams(*cparams);
209}
210EXPORT_SYMBOL(zstd_cstream_workspace_bound);
211
212zstd_cstream *zstd_init_cstream(const zstd_parameters *parameters,
213	unsigned long long pledged_src_size, void *workspace, size_t workspace_size)
214{
215	zstd_cstream *cstream;
216
217	if (workspace == NULL)
218		return NULL;
219
220	cstream = ZSTD_initStaticCStream(workspace, workspace_size);
221	if (cstream == NULL)
222		return NULL;
223
224	/* 0 means unknown in linux zstd API but means 0 in new zstd API */
225	if (pledged_src_size == 0)
226		pledged_src_size = ZSTD_CONTENTSIZE_UNKNOWN;
227
228	if (ZSTD_isError(zstd_cctx_init(cstream, parameters, pledged_src_size)))
229		return NULL;
230
231	return cstream;
232}
233EXPORT_SYMBOL(zstd_init_cstream);
234
235size_t zstd_reset_cstream(zstd_cstream *cstream,
236	unsigned long long pledged_src_size)
237{
238	if (pledged_src_size == 0)
239		pledged_src_size = ZSTD_CONTENTSIZE_UNKNOWN;
240	ZSTD_FORWARD_IF_ERR( ZSTD_CCtx_reset(cstream, ZSTD_reset_session_only) );
241	ZSTD_FORWARD_IF_ERR( ZSTD_CCtx_setPledgedSrcSize(cstream, pledged_src_size) );
242	return 0;
243}
244EXPORT_SYMBOL(zstd_reset_cstream);
245
246size_t zstd_compress_stream(zstd_cstream *cstream, zstd_out_buffer *output,
247	zstd_in_buffer *input)
248{
249	return ZSTD_compressStream(cstream, output, input);
250}
251EXPORT_SYMBOL(zstd_compress_stream);
252
253size_t zstd_flush_stream(zstd_cstream *cstream, zstd_out_buffer *output)
254{
255	return ZSTD_flushStream(cstream, output);
256}
257EXPORT_SYMBOL(zstd_flush_stream);
258
259size_t zstd_end_stream(zstd_cstream *cstream, zstd_out_buffer *output)
260{
261	return ZSTD_endStream(cstream, output);
262}
263EXPORT_SYMBOL(zstd_end_stream);
264
265void zstd_register_sequence_producer(
266  zstd_cctx *cctx,
267  void* sequence_producer_state,
268  zstd_sequence_producer_f sequence_producer
269) {
270	ZSTD_registerSequenceProducer(cctx, sequence_producer_state, sequence_producer);
271}
272EXPORT_SYMBOL(zstd_register_sequence_producer);
273
274size_t zstd_compress_sequences_and_literals(zstd_cctx *cctx, void* dst, size_t dst_capacity,
275					    const zstd_sequence *in_seqs, size_t in_seqs_size,
276					    const void* literals, size_t lit_size, size_t lit_capacity,
277					    size_t decompressed_size)
278{
279	return ZSTD_compressSequencesAndLiterals(cctx, dst, dst_capacity, in_seqs,
280						 in_seqs_size, literals, lit_size,
281						 lit_capacity, decompressed_size);
282}
283EXPORT_SYMBOL(zstd_compress_sequences_and_literals);
284
285MODULE_LICENSE("Dual BSD/GPL");
286MODULE_DESCRIPTION("Zstd Compressor");
~/ Read-only file preview.