blob: 65548a4bb9341077313e2452dcb20669d3584e86 [file] [log] [blame]
Nick Terrell4d347a92021-09-22 15:06:08 -07001// SPDX-License-Identifier: GPL-2.0+ OR BSD-3-Clause
Nick Terrella4943082021-03-29 14:23:36 -07002/*
3 * Copyright (c) Facebook, Inc.
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 */
Nick Terrell29c5de82020-08-10 23:11:20 -070011
12#include <linux/kernel.h>
13#include <linux/module.h>
Nick Terrellf58e9a92020-12-02 00:56:56 -080014#include <linux/string.h>
Nick Terrell29c5de82020-08-10 23:11:20 -070015#include <linux/zstd.h>
16
Nick Terrellf58e9a92020-12-02 00:56:56 -080017#include "common/zstd_deps.h"
18#include "common/zstd_internal.h"
19
Nick Terrell162491f2021-09-22 18:13:02 -070020#define ZSTD_FORWARD_IF_ERR(ret) \
21 do { \
22 size_t const __ret = (ret); \
23 if (ZSTD_isError(__ret)) \
24 return __ret; \
25 } while (0)
26
27static size_t zstd_cctx_init(zstd_cctx *cctx, const zstd_parameters *parameters,
28 unsigned long long pledged_src_size)
29{
30 ZSTD_FORWARD_IF_ERR(ZSTD_CCtx_reset(
31 cctx, ZSTD_reset_session_and_parameters));
32 ZSTD_FORWARD_IF_ERR(ZSTD_CCtx_setPledgedSrcSize(
33 cctx, pledged_src_size));
34 ZSTD_FORWARD_IF_ERR(ZSTD_CCtx_setParameter(
35 cctx, ZSTD_c_windowLog, parameters->cParams.windowLog));
36 ZSTD_FORWARD_IF_ERR(ZSTD_CCtx_setParameter(
37 cctx, ZSTD_c_hashLog, parameters->cParams.hashLog));
38 ZSTD_FORWARD_IF_ERR(ZSTD_CCtx_setParameter(
39 cctx, ZSTD_c_chainLog, parameters->cParams.chainLog));
40 ZSTD_FORWARD_IF_ERR(ZSTD_CCtx_setParameter(
41 cctx, ZSTD_c_searchLog, parameters->cParams.searchLog));
42 ZSTD_FORWARD_IF_ERR(ZSTD_CCtx_setParameter(
43 cctx, ZSTD_c_minMatch, parameters->cParams.minMatch));
44 ZSTD_FORWARD_IF_ERR(ZSTD_CCtx_setParameter(
45 cctx, ZSTD_c_targetLength, parameters->cParams.targetLength));
46 ZSTD_FORWARD_IF_ERR(ZSTD_CCtx_setParameter(
47 cctx, ZSTD_c_strategy, parameters->cParams.strategy));
48 ZSTD_FORWARD_IF_ERR(ZSTD_CCtx_setParameter(
49 cctx, ZSTD_c_contentSizeFlag, parameters->fParams.contentSizeFlag));
50 ZSTD_FORWARD_IF_ERR(ZSTD_CCtx_setParameter(
51 cctx, ZSTD_c_checksumFlag, parameters->fParams.checksumFlag));
52 ZSTD_FORWARD_IF_ERR(ZSTD_CCtx_setParameter(
53 cctx, ZSTD_c_dictIDFlag, !parameters->fParams.noDictIDFlag));
54 return 0;
55}
56
Nick Terrelld334ad22021-03-29 17:52:28 -070057int zstd_min_clevel(void)
58{
59 return ZSTD_minCLevel();
60}
61EXPORT_SYMBOL(zstd_min_clevel);
62
63int zstd_max_clevel(void)
64{
65 return ZSTD_maxCLevel();
66}
67EXPORT_SYMBOL(zstd_max_clevel);
68
Nick Terrellf58e9a92020-12-02 00:56:56 -080069size_t zstd_compress_bound(size_t src_size)
70{
71 return ZSTD_compressBound(src_size);
72}
73EXPORT_SYMBOL(zstd_compress_bound);
74
Nick Terrelle4b914e2020-12-03 12:37:30 -080075zstd_parameters zstd_get_params(int level,
Nick Terrell3256c002020-12-02 11:07:17 -080076 unsigned long long estimated_src_size)
Nick Terrellf58e9a92020-12-02 00:56:56 -080077{
Nick Terrelle4b914e2020-12-03 12:37:30 -080078 return ZSTD_getParams(level, estimated_src_size, 0);
Nick Terrellf58e9a92020-12-02 00:56:56 -080079}
80EXPORT_SYMBOL(zstd_get_params);
81
Nick Terrelle4b914e2020-12-03 12:37:30 -080082size_t zstd_cctx_workspace_bound(const zstd_compression_parameters *cparams)
Nick Terrellf58e9a92020-12-02 00:56:56 -080083{
Nick Terrelle4b914e2020-12-03 12:37:30 -080084 return ZSTD_estimateCCtxSize_usingCParams(*cparams);
Nick Terrellf58e9a92020-12-02 00:56:56 -080085}
86EXPORT_SYMBOL(zstd_cctx_workspace_bound);
87
88zstd_cctx *zstd_init_cctx(void *workspace, size_t workspace_size)
89{
90 if (workspace == NULL)
91 return NULL;
92 return ZSTD_initStaticCCtx(workspace, workspace_size);
93}
94EXPORT_SYMBOL(zstd_init_cctx);
95
Nick Terrell3256c002020-12-02 11:07:17 -080096size_t zstd_compress_cctx(zstd_cctx *cctx, void *dst, size_t dst_capacity,
Nick Terrelle4b914e2020-12-03 12:37:30 -080097 const void *src, size_t src_size, const zstd_parameters *parameters)
Nick Terrellf58e9a92020-12-02 00:56:56 -080098{
Nick Terrell162491f2021-09-22 18:13:02 -070099 ZSTD_FORWARD_IF_ERR(zstd_cctx_init(cctx, parameters, src_size));
100 return ZSTD_compress2(cctx, dst, dst_capacity, src, src_size);
Nick Terrellf58e9a92020-12-02 00:56:56 -0800101}
102EXPORT_SYMBOL(zstd_compress_cctx);
103
Nick Terrelle4b914e2020-12-03 12:37:30 -0800104size_t zstd_cstream_workspace_bound(const zstd_compression_parameters *cparams)
Nick Terrellf58e9a92020-12-02 00:56:56 -0800105{
Nick Terrelle4b914e2020-12-03 12:37:30 -0800106 return ZSTD_estimateCStreamSize_usingCParams(*cparams);
Nick Terrellf58e9a92020-12-02 00:56:56 -0800107}
108EXPORT_SYMBOL(zstd_cstream_workspace_bound);
109
Nick Terrelle4b914e2020-12-03 12:37:30 -0800110zstd_cstream *zstd_init_cstream(const zstd_parameters *parameters,
Nick Terrell3256c002020-12-02 11:07:17 -0800111 unsigned long long pledged_src_size, void *workspace, size_t workspace_size)
Nick Terrellf58e9a92020-12-02 00:56:56 -0800112{
Nick Terrellf58e9a92020-12-02 00:56:56 -0800113 zstd_cstream *cstream;
Nick Terrellf58e9a92020-12-02 00:56:56 -0800114
115 if (workspace == NULL)
116 return NULL;
117
118 cstream = ZSTD_initStaticCStream(workspace, workspace_size);
119 if (cstream == NULL)
120 return NULL;
121
122 /* 0 means unknown in linux zstd API but means 0 in new zstd API */
123 if (pledged_src_size == 0)
124 pledged_src_size = ZSTD_CONTENTSIZE_UNKNOWN;
125
Nick Terrell162491f2021-09-22 18:13:02 -0700126 if (ZSTD_isError(zstd_cctx_init(cstream, parameters, pledged_src_size)))
Nick Terrellf58e9a92020-12-02 00:56:56 -0800127 return NULL;
128
129 return cstream;
130}
131EXPORT_SYMBOL(zstd_init_cstream);
132
Nick Terrell3256c002020-12-02 11:07:17 -0800133size_t zstd_reset_cstream(zstd_cstream *cstream,
134 unsigned long long pledged_src_size)
Nick Terrellf58e9a92020-12-02 00:56:56 -0800135{
136 return ZSTD_resetCStream(cstream, pledged_src_size);
137}
138EXPORT_SYMBOL(zstd_reset_cstream);
139
Nick Terrelle4b914e2020-12-03 12:37:30 -0800140size_t zstd_compress_stream(zstd_cstream *cstream, zstd_out_buffer *output,
141 zstd_in_buffer *input)
Nick Terrellf58e9a92020-12-02 00:56:56 -0800142{
Nick Terrelle4b914e2020-12-03 12:37:30 -0800143 return ZSTD_compressStream(cstream, output, input);
Nick Terrellf58e9a92020-12-02 00:56:56 -0800144}
145EXPORT_SYMBOL(zstd_compress_stream);
146
Nick Terrelle4b914e2020-12-03 12:37:30 -0800147size_t zstd_flush_stream(zstd_cstream *cstream, zstd_out_buffer *output)
Nick Terrellf58e9a92020-12-02 00:56:56 -0800148{
Nick Terrelle4b914e2020-12-03 12:37:30 -0800149 return ZSTD_flushStream(cstream, output);
Nick Terrellf58e9a92020-12-02 00:56:56 -0800150}
151EXPORT_SYMBOL(zstd_flush_stream);
152
Nick Terrelle4b914e2020-12-03 12:37:30 -0800153size_t zstd_end_stream(zstd_cstream *cstream, zstd_out_buffer *output)
Nick Terrellf58e9a92020-12-02 00:56:56 -0800154{
Nick Terrelle4b914e2020-12-03 12:37:30 -0800155 return ZSTD_endStream(cstream, output);
Nick Terrellf58e9a92020-12-02 00:56:56 -0800156}
157EXPORT_SYMBOL(zstd_end_stream);
Nick Terrell29c5de82020-08-10 23:11:20 -0700158
159MODULE_LICENSE("Dual BSD/GPL");
160MODULE_DESCRIPTION("Zstd Compressor");