Nick Terrell | a494308 | 2021-03-29 14:23:36 -0700 | [diff] [blame^] | 1 | // 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 Terrell | 29c5de8 | 2020-08-10 23:11:20 -0700 | [diff] [blame] | 11 | |
| 12 | #include <linux/kernel.h> |
| 13 | #include <linux/module.h> |
Nick Terrell | f58e9a9 | 2020-12-02 00:56:56 -0800 | [diff] [blame] | 14 | #include <linux/string.h> |
Nick Terrell | 29c5de8 | 2020-08-10 23:11:20 -0700 | [diff] [blame] | 15 | #include <linux/zstd.h> |
| 16 | |
Nick Terrell | f58e9a9 | 2020-12-02 00:56:56 -0800 | [diff] [blame] | 17 | #include "common/zstd_deps.h" |
| 18 | #include "common/zstd_internal.h" |
| 19 | |
Nick Terrell | f58e9a9 | 2020-12-02 00:56:56 -0800 | [diff] [blame] | 20 | size_t zstd_compress_bound(size_t src_size) |
| 21 | { |
| 22 | return ZSTD_compressBound(src_size); |
| 23 | } |
| 24 | EXPORT_SYMBOL(zstd_compress_bound); |
| 25 | |
Nick Terrell | e4b914e | 2020-12-03 12:37:30 -0800 | [diff] [blame] | 26 | zstd_parameters zstd_get_params(int level, |
Nick Terrell | 3256c00 | 2020-12-02 11:07:17 -0800 | [diff] [blame] | 27 | unsigned long long estimated_src_size) |
Nick Terrell | f58e9a9 | 2020-12-02 00:56:56 -0800 | [diff] [blame] | 28 | { |
Nick Terrell | e4b914e | 2020-12-03 12:37:30 -0800 | [diff] [blame] | 29 | return ZSTD_getParams(level, estimated_src_size, 0); |
Nick Terrell | f58e9a9 | 2020-12-02 00:56:56 -0800 | [diff] [blame] | 30 | } |
| 31 | EXPORT_SYMBOL(zstd_get_params); |
| 32 | |
Nick Terrell | e4b914e | 2020-12-03 12:37:30 -0800 | [diff] [blame] | 33 | size_t zstd_cctx_workspace_bound(const zstd_compression_parameters *cparams) |
Nick Terrell | f58e9a9 | 2020-12-02 00:56:56 -0800 | [diff] [blame] | 34 | { |
Nick Terrell | e4b914e | 2020-12-03 12:37:30 -0800 | [diff] [blame] | 35 | return ZSTD_estimateCCtxSize_usingCParams(*cparams); |
Nick Terrell | f58e9a9 | 2020-12-02 00:56:56 -0800 | [diff] [blame] | 36 | } |
| 37 | EXPORT_SYMBOL(zstd_cctx_workspace_bound); |
| 38 | |
| 39 | zstd_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 | } |
| 45 | EXPORT_SYMBOL(zstd_init_cctx); |
| 46 | |
Nick Terrell | 3256c00 | 2020-12-02 11:07:17 -0800 | [diff] [blame] | 47 | size_t zstd_compress_cctx(zstd_cctx *cctx, void *dst, size_t dst_capacity, |
Nick Terrell | e4b914e | 2020-12-03 12:37:30 -0800 | [diff] [blame] | 48 | const void *src, size_t src_size, const zstd_parameters *parameters) |
Nick Terrell | f58e9a9 | 2020-12-02 00:56:56 -0800 | [diff] [blame] | 49 | { |
Nick Terrell | e4b914e | 2020-12-03 12:37:30 -0800 | [diff] [blame] | 50 | return ZSTD_compress_advanced(cctx, dst, dst_capacity, src, src_size, NULL, 0, *parameters); |
Nick Terrell | f58e9a9 | 2020-12-02 00:56:56 -0800 | [diff] [blame] | 51 | } |
| 52 | EXPORT_SYMBOL(zstd_compress_cctx); |
| 53 | |
Nick Terrell | e4b914e | 2020-12-03 12:37:30 -0800 | [diff] [blame] | 54 | size_t zstd_cstream_workspace_bound(const zstd_compression_parameters *cparams) |
Nick Terrell | f58e9a9 | 2020-12-02 00:56:56 -0800 | [diff] [blame] | 55 | { |
Nick Terrell | e4b914e | 2020-12-03 12:37:30 -0800 | [diff] [blame] | 56 | return ZSTD_estimateCStreamSize_usingCParams(*cparams); |
Nick Terrell | f58e9a9 | 2020-12-02 00:56:56 -0800 | [diff] [blame] | 57 | } |
| 58 | EXPORT_SYMBOL(zstd_cstream_workspace_bound); |
| 59 | |
Nick Terrell | e4b914e | 2020-12-03 12:37:30 -0800 | [diff] [blame] | 60 | zstd_cstream *zstd_init_cstream(const zstd_parameters *parameters, |
Nick Terrell | 3256c00 | 2020-12-02 11:07:17 -0800 | [diff] [blame] | 61 | unsigned long long pledged_src_size, void *workspace, size_t workspace_size) |
Nick Terrell | f58e9a9 | 2020-12-02 00:56:56 -0800 | [diff] [blame] | 62 | { |
Nick Terrell | f58e9a9 | 2020-12-02 00:56:56 -0800 | [diff] [blame] | 63 | 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 Terrell | e4b914e | 2020-12-03 12:37:30 -0800 | [diff] [blame] | 77 | ret = ZSTD_initCStream_advanced(cstream, NULL, 0, *parameters, pledged_src_size); |
Nick Terrell | f58e9a9 | 2020-12-02 00:56:56 -0800 | [diff] [blame] | 78 | if (ZSTD_isError(ret)) |
| 79 | return NULL; |
| 80 | |
| 81 | return cstream; |
| 82 | } |
| 83 | EXPORT_SYMBOL(zstd_init_cstream); |
| 84 | |
Nick Terrell | 3256c00 | 2020-12-02 11:07:17 -0800 | [diff] [blame] | 85 | size_t zstd_reset_cstream(zstd_cstream *cstream, |
| 86 | unsigned long long pledged_src_size) |
Nick Terrell | f58e9a9 | 2020-12-02 00:56:56 -0800 | [diff] [blame] | 87 | { |
| 88 | return ZSTD_resetCStream(cstream, pledged_src_size); |
| 89 | } |
| 90 | EXPORT_SYMBOL(zstd_reset_cstream); |
| 91 | |
Nick Terrell | e4b914e | 2020-12-03 12:37:30 -0800 | [diff] [blame] | 92 | size_t zstd_compress_stream(zstd_cstream *cstream, zstd_out_buffer *output, |
| 93 | zstd_in_buffer *input) |
Nick Terrell | f58e9a9 | 2020-12-02 00:56:56 -0800 | [diff] [blame] | 94 | { |
Nick Terrell | e4b914e | 2020-12-03 12:37:30 -0800 | [diff] [blame] | 95 | return ZSTD_compressStream(cstream, output, input); |
Nick Terrell | f58e9a9 | 2020-12-02 00:56:56 -0800 | [diff] [blame] | 96 | } |
| 97 | EXPORT_SYMBOL(zstd_compress_stream); |
| 98 | |
Nick Terrell | e4b914e | 2020-12-03 12:37:30 -0800 | [diff] [blame] | 99 | size_t zstd_flush_stream(zstd_cstream *cstream, zstd_out_buffer *output) |
Nick Terrell | f58e9a9 | 2020-12-02 00:56:56 -0800 | [diff] [blame] | 100 | { |
Nick Terrell | e4b914e | 2020-12-03 12:37:30 -0800 | [diff] [blame] | 101 | return ZSTD_flushStream(cstream, output); |
Nick Terrell | f58e9a9 | 2020-12-02 00:56:56 -0800 | [diff] [blame] | 102 | } |
| 103 | EXPORT_SYMBOL(zstd_flush_stream); |
| 104 | |
Nick Terrell | e4b914e | 2020-12-03 12:37:30 -0800 | [diff] [blame] | 105 | size_t zstd_end_stream(zstd_cstream *cstream, zstd_out_buffer *output) |
Nick Terrell | f58e9a9 | 2020-12-02 00:56:56 -0800 | [diff] [blame] | 106 | { |
Nick Terrell | e4b914e | 2020-12-03 12:37:30 -0800 | [diff] [blame] | 107 | return ZSTD_endStream(cstream, output); |
Nick Terrell | f58e9a9 | 2020-12-02 00:56:56 -0800 | [diff] [blame] | 108 | } |
| 109 | EXPORT_SYMBOL(zstd_end_stream); |
Nick Terrell | 29c5de8 | 2020-08-10 23:11:20 -0700 | [diff] [blame] | 110 | |
| 111 | MODULE_LICENSE("Dual BSD/GPL"); |
| 112 | MODULE_DESCRIPTION("Zstd Compressor"); |