| 1 | |
| 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 |
|
| 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 |
|
| 58 | int zstd_min_clevel(void)
|
| 59 | {
|
| 60 | return ZSTD_minCLevel();
|
| 61 | }
|
| 62 | EXPORT_SYMBOL(zstd_min_clevel);
|
| 63 |
|
| 64 | int zstd_max_clevel(void)
|
| 65 | {
|
| 66 | return ZSTD_maxCLevel();
|
| 67 | }
|
| 68 | EXPORT_SYMBOL(zstd_max_clevel);
|
| 69 |
|
| 70 | int zstd_default_clevel(void)
|
| 71 | {
|
| 72 | return ZSTD_defaultCLevel();
|
| 73 | }
|
| 74 | EXPORT_SYMBOL(zstd_default_clevel);
|
| 75 |
|
| 76 | size_t zstd_compress_bound(size_t src_size)
|
| 77 | {
|
| 78 | return ZSTD_compressBound(src_size);
|
| 79 | }
|
| 80 | EXPORT_SYMBOL(zstd_compress_bound);
|
| 81 |
|
| 82 | zstd_parameters zstd_get_params(int level,
|
| 83 | unsigned long long estimated_src_size)
|
| 84 | {
|
| 85 | return ZSTD_getParams(level, estimated_src_size, 0);
|
| 86 | }
|
| 87 | EXPORT_SYMBOL(zstd_get_params);
|
| 88 |
|
| 89 | zstd_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 | }
|
| 94 | EXPORT_SYMBOL(zstd_get_cparams);
|
| 95 |
|
| 96 | size_t zstd_cctx_set_param(zstd_cctx *cctx, ZSTD_cParameter param, int value)
|
| 97 | {
|
| 98 | return ZSTD_CCtx_setParameter(cctx, param, value);
|
| 99 | }
|
| 100 | EXPORT_SYMBOL(zstd_cctx_set_param);
|
| 101 |
|
| 102 | size_t zstd_cctx_workspace_bound(const zstd_compression_parameters *cparams)
|
| 103 | {
|
| 104 | return ZSTD_estimateCCtxSize_usingCParams(*cparams);
|
| 105 | }
|
| 106 | EXPORT_SYMBOL(zstd_cctx_workspace_bound);
|
| 107 |
|
| 108 | |
| 109 | static 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 |
|
| 126 | static 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 |
|
| 136 | size_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 | }
|
| 143 | EXPORT_SYMBOL(zstd_cctx_workspace_bound_with_ext_seq_prod);
|
| 144 |
|
| 145 | size_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 | }
|
| 152 | EXPORT_SYMBOL(zstd_cstream_workspace_bound_with_ext_seq_prod);
|
| 153 |
|
| 154 | zstd_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 | }
|
| 160 | EXPORT_SYMBOL(zstd_init_cctx);
|
| 161 |
|
| 162 | zstd_cctx *zstd_create_cctx_advanced(zstd_custom_mem custom_mem)
|
| 163 | {
|
| 164 | return ZSTD_createCCtx_advanced(custom_mem);
|
| 165 | }
|
| 166 | EXPORT_SYMBOL(zstd_create_cctx_advanced);
|
| 167 |
|
| 168 | size_t zstd_free_cctx(zstd_cctx *cctx)
|
| 169 | {
|
| 170 | return ZSTD_freeCCtx(cctx);
|
| 171 | }
|
| 172 | EXPORT_SYMBOL(zstd_free_cctx);
|
| 173 |
|
| 174 | zstd_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 | }
|
| 181 | EXPORT_SYMBOL(zstd_create_cdict_byreference);
|
| 182 |
|
| 183 | size_t zstd_free_cdict(zstd_cdict *cdict)
|
| 184 | {
|
| 185 | return ZSTD_freeCDict(cdict);
|
| 186 | }
|
| 187 | EXPORT_SYMBOL(zstd_free_cdict);
|
| 188 |
|
| 189 | size_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 | }
|
| 195 | EXPORT_SYMBOL(zstd_compress_cctx);
|
| 196 |
|
| 197 | size_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 | }
|
| 204 | EXPORT_SYMBOL(zstd_compress_using_cdict);
|
| 205 |
|
| 206 | size_t zstd_cstream_workspace_bound(const zstd_compression_parameters *cparams)
|
| 207 | {
|
| 208 | return ZSTD_estimateCStreamSize_usingCParams(*cparams);
|
| 209 | }
|
| 210 | EXPORT_SYMBOL(zstd_cstream_workspace_bound);
|
| 211 |
|
| 212 | zstd_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 |
|
| 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 | }
|
| 233 | EXPORT_SYMBOL(zstd_init_cstream);
|
| 234 |
|
| 235 | size_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 | }
|
| 244 | EXPORT_SYMBOL(zstd_reset_cstream);
|
| 245 |
|
| 246 | size_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 | }
|
| 251 | EXPORT_SYMBOL(zstd_compress_stream);
|
| 252 |
|
| 253 | size_t zstd_flush_stream(zstd_cstream *cstream, zstd_out_buffer *output)
|
| 254 | {
|
| 255 | return ZSTD_flushStream(cstream, output);
|
| 256 | }
|
| 257 | EXPORT_SYMBOL(zstd_flush_stream);
|
| 258 |
|
| 259 | size_t zstd_end_stream(zstd_cstream *cstream, zstd_out_buffer *output)
|
| 260 | {
|
| 261 | return ZSTD_endStream(cstream, output);
|
| 262 | }
|
| 263 | EXPORT_SYMBOL(zstd_end_stream);
|
| 264 |
|
| 265 | void 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 | }
|
| 272 | EXPORT_SYMBOL(zstd_register_sequence_producer);
|
| 273 |
|
| 274 | size_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 | }
|
| 283 | EXPORT_SYMBOL(zstd_compress_sequences_and_literals);
|
| 284 |
|
| 285 | MODULE_LICENSE("Dual BSD/GPL");
|
| 286 | MODULE_DESCRIPTION("Zstd Compressor");
|