blob: fb7dda173d191e21624c7bb590d879248cdf1c03 [file] [log] [blame]
Nick Terrella4943082021-03-29 14:23:36 -07001// SPDX-License-Identifier: GPL-2.0-only
2/*
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 Terrellf58e9a92020-12-02 00:56:56 -080020size_t zstd_compress_bound(size_t src_size)
21{
22 return ZSTD_compressBound(src_size);
23}
24EXPORT_SYMBOL(zstd_compress_bound);
25
Nick Terrelle4b914e2020-12-03 12:37:30 -080026zstd_parameters zstd_get_params(int level,
Nick Terrell3256c002020-12-02 11:07:17 -080027 unsigned long long estimated_src_size)
Nick Terrellf58e9a92020-12-02 00:56:56 -080028{
Nick Terrelle4b914e2020-12-03 12:37:30 -080029 return ZSTD_getParams(level, estimated_src_size, 0);
Nick Terrellf58e9a92020-12-02 00:56:56 -080030}
31EXPORT_SYMBOL(zstd_get_params);
32
Nick Terrelle4b914e2020-12-03 12:37:30 -080033size_t zstd_cctx_workspace_bound(const zstd_compression_parameters *cparams)
Nick Terrellf58e9a92020-12-02 00:56:56 -080034{
Nick Terrelle4b914e2020-12-03 12:37:30 -080035 return ZSTD_estimateCCtxSize_usingCParams(*cparams);
Nick Terrellf58e9a92020-12-02 00:56:56 -080036}
37EXPORT_SYMBOL(zstd_cctx_workspace_bound);
38
39zstd_cctx *zstd_init_cctx(void *workspace, size_t workspace_size)
40{
41 if (workspace == NULL)
42 return NULL;
43 return ZSTD_initStaticCCtx(workspace, workspace_size);
44}
45EXPORT_SYMBOL(zstd_init_cctx);
46
Nick Terrell3256c002020-12-02 11:07:17 -080047size_t zstd_compress_cctx(zstd_cctx *cctx, void *dst, size_t dst_capacity,
Nick Terrelle4b914e2020-12-03 12:37:30 -080048 const void *src, size_t src_size, const zstd_parameters *parameters)
Nick Terrellf58e9a92020-12-02 00:56:56 -080049{
Nick Terrelle4b914e2020-12-03 12:37:30 -080050 return ZSTD_compress_advanced(cctx, dst, dst_capacity, src, src_size, NULL, 0, *parameters);
Nick Terrellf58e9a92020-12-02 00:56:56 -080051}
52EXPORT_SYMBOL(zstd_compress_cctx);
53
Nick Terrelle4b914e2020-12-03 12:37:30 -080054size_t zstd_cstream_workspace_bound(const zstd_compression_parameters *cparams)
Nick Terrellf58e9a92020-12-02 00:56:56 -080055{
Nick Terrelle4b914e2020-12-03 12:37:30 -080056 return ZSTD_estimateCStreamSize_usingCParams(*cparams);
Nick Terrellf58e9a92020-12-02 00:56:56 -080057}
58EXPORT_SYMBOL(zstd_cstream_workspace_bound);
59
Nick Terrelle4b914e2020-12-03 12:37:30 -080060zstd_cstream *zstd_init_cstream(const zstd_parameters *parameters,
Nick Terrell3256c002020-12-02 11:07:17 -080061 unsigned long long pledged_src_size, void *workspace, size_t workspace_size)
Nick Terrellf58e9a92020-12-02 00:56:56 -080062{
Nick Terrellf58e9a92020-12-02 00:56:56 -080063 zstd_cstream *cstream;
64 size_t ret;
65
66 if (workspace == NULL)
67 return NULL;
68
69 cstream = ZSTD_initStaticCStream(workspace, workspace_size);
70 if (cstream == NULL)
71 return NULL;
72
73 /* 0 means unknown in linux zstd API but means 0 in new zstd API */
74 if (pledged_src_size == 0)
75 pledged_src_size = ZSTD_CONTENTSIZE_UNKNOWN;
76
Nick Terrelle4b914e2020-12-03 12:37:30 -080077 ret = ZSTD_initCStream_advanced(cstream, NULL, 0, *parameters, pledged_src_size);
Nick Terrellf58e9a92020-12-02 00:56:56 -080078 if (ZSTD_isError(ret))
79 return NULL;
80
81 return cstream;
82}
83EXPORT_SYMBOL(zstd_init_cstream);
84
Nick Terrell3256c002020-12-02 11:07:17 -080085size_t zstd_reset_cstream(zstd_cstream *cstream,
86 unsigned long long pledged_src_size)
Nick Terrellf58e9a92020-12-02 00:56:56 -080087{
88 return ZSTD_resetCStream(cstream, pledged_src_size);
89}
90EXPORT_SYMBOL(zstd_reset_cstream);
91
Nick Terrelle4b914e2020-12-03 12:37:30 -080092size_t zstd_compress_stream(zstd_cstream *cstream, zstd_out_buffer *output,
93 zstd_in_buffer *input)
Nick Terrellf58e9a92020-12-02 00:56:56 -080094{
Nick Terrelle4b914e2020-12-03 12:37:30 -080095 return ZSTD_compressStream(cstream, output, input);
Nick Terrellf58e9a92020-12-02 00:56:56 -080096}
97EXPORT_SYMBOL(zstd_compress_stream);
98
Nick Terrelle4b914e2020-12-03 12:37:30 -080099size_t zstd_flush_stream(zstd_cstream *cstream, zstd_out_buffer *output)
Nick Terrellf58e9a92020-12-02 00:56:56 -0800100{
Nick Terrelle4b914e2020-12-03 12:37:30 -0800101 return ZSTD_flushStream(cstream, output);
Nick Terrellf58e9a92020-12-02 00:56:56 -0800102}
103EXPORT_SYMBOL(zstd_flush_stream);
104
Nick Terrelle4b914e2020-12-03 12:37:30 -0800105size_t zstd_end_stream(zstd_cstream *cstream, zstd_out_buffer *output)
Nick Terrellf58e9a92020-12-02 00:56:56 -0800106{
Nick Terrelle4b914e2020-12-03 12:37:30 -0800107 return ZSTD_endStream(cstream, output);
Nick Terrellf58e9a92020-12-02 00:56:56 -0800108}
109EXPORT_SYMBOL(zstd_end_stream);
Nick Terrell29c5de82020-08-10 23:11:20 -0700110
111MODULE_LICENSE("Dual BSD/GPL");
112MODULE_DESCRIPTION("Zstd Compressor");