1 // SPDX-License-Identifier: GPL-2.0+ OR BSD-3- 1 // SPDX-License-Identifier: GPL-2.0+ OR BSD-3-Clause 2 /* 2 /* 3 * Copyright (c) Facebook, Inc. 3 * Copyright (c) Facebook, Inc. 4 * All rights reserved. 4 * All rights reserved. 5 * 5 * 6 * This source code is licensed under both the 6 * This source code is licensed under both the BSD-style license (found in the 7 * LICENSE file in the root directory of this 7 * LICENSE file in the root directory of this source tree) and the GPLv2 (found 8 * in the COPYING file in the root directory o 8 * in the COPYING file in the root directory of this source tree). 9 * You may select, at your option, one of the 9 * You may select, at your option, one of the above-listed licenses. 10 */ 10 */ 11 11 12 #include <linux/kernel.h> 12 #include <linux/kernel.h> 13 #include <linux/module.h> 13 #include <linux/module.h> 14 #include <linux/string.h> 14 #include <linux/string.h> 15 #include <linux/zstd.h> 15 #include <linux/zstd.h> 16 16 17 #include "common/zstd_deps.h" 17 #include "common/zstd_deps.h" 18 #include "common/zstd_internal.h" 18 #include "common/zstd_internal.h" 19 19 20 #define ZSTD_FORWARD_IF_ERR(ret) \ 20 #define ZSTD_FORWARD_IF_ERR(ret) \ 21 do { \ 21 do { \ 22 size_t const __ret = (ret); \ 22 size_t const __ret = (ret); \ 23 if (ZSTD_isError(__ret)) \ 23 if (ZSTD_isError(__ret)) \ 24 return __ret; \ 24 return __ret; \ 25 } while (0) 25 } while (0) 26 26 27 static size_t zstd_cctx_init(zstd_cctx *cctx, 27 static size_t zstd_cctx_init(zstd_cctx *cctx, const zstd_parameters *parameters, 28 unsigned long long pledged_src_size) 28 unsigned long long pledged_src_size) 29 { 29 { 30 ZSTD_FORWARD_IF_ERR(ZSTD_CCtx_reset( 30 ZSTD_FORWARD_IF_ERR(ZSTD_CCtx_reset( 31 cctx, ZSTD_reset_session_and_p 31 cctx, ZSTD_reset_session_and_parameters)); 32 ZSTD_FORWARD_IF_ERR(ZSTD_CCtx_setPledg 32 ZSTD_FORWARD_IF_ERR(ZSTD_CCtx_setPledgedSrcSize( 33 cctx, pledged_src_size)); 33 cctx, pledged_src_size)); 34 ZSTD_FORWARD_IF_ERR(ZSTD_CCtx_setParam 34 ZSTD_FORWARD_IF_ERR(ZSTD_CCtx_setParameter( 35 cctx, ZSTD_c_windowLog, parame 35 cctx, ZSTD_c_windowLog, parameters->cParams.windowLog)); 36 ZSTD_FORWARD_IF_ERR(ZSTD_CCtx_setParam 36 ZSTD_FORWARD_IF_ERR(ZSTD_CCtx_setParameter( 37 cctx, ZSTD_c_hashLog, paramete 37 cctx, ZSTD_c_hashLog, parameters->cParams.hashLog)); 38 ZSTD_FORWARD_IF_ERR(ZSTD_CCtx_setParam 38 ZSTD_FORWARD_IF_ERR(ZSTD_CCtx_setParameter( 39 cctx, ZSTD_c_chainLog, paramet 39 cctx, ZSTD_c_chainLog, parameters->cParams.chainLog)); 40 ZSTD_FORWARD_IF_ERR(ZSTD_CCtx_setParam 40 ZSTD_FORWARD_IF_ERR(ZSTD_CCtx_setParameter( 41 cctx, ZSTD_c_searchLog, parame 41 cctx, ZSTD_c_searchLog, parameters->cParams.searchLog)); 42 ZSTD_FORWARD_IF_ERR(ZSTD_CCtx_setParam 42 ZSTD_FORWARD_IF_ERR(ZSTD_CCtx_setParameter( 43 cctx, ZSTD_c_minMatch, paramet 43 cctx, ZSTD_c_minMatch, parameters->cParams.minMatch)); 44 ZSTD_FORWARD_IF_ERR(ZSTD_CCtx_setParam 44 ZSTD_FORWARD_IF_ERR(ZSTD_CCtx_setParameter( 45 cctx, ZSTD_c_targetLength, par 45 cctx, ZSTD_c_targetLength, parameters->cParams.targetLength)); 46 ZSTD_FORWARD_IF_ERR(ZSTD_CCtx_setParam 46 ZSTD_FORWARD_IF_ERR(ZSTD_CCtx_setParameter( 47 cctx, ZSTD_c_strategy, paramet 47 cctx, ZSTD_c_strategy, parameters->cParams.strategy)); 48 ZSTD_FORWARD_IF_ERR(ZSTD_CCtx_setParam 48 ZSTD_FORWARD_IF_ERR(ZSTD_CCtx_setParameter( 49 cctx, ZSTD_c_contentSizeFlag, 49 cctx, ZSTD_c_contentSizeFlag, parameters->fParams.contentSizeFlag)); 50 ZSTD_FORWARD_IF_ERR(ZSTD_CCtx_setParam 50 ZSTD_FORWARD_IF_ERR(ZSTD_CCtx_setParameter( 51 cctx, ZSTD_c_checksumFlag, par 51 cctx, ZSTD_c_checksumFlag, parameters->fParams.checksumFlag)); 52 ZSTD_FORWARD_IF_ERR(ZSTD_CCtx_setParam 52 ZSTD_FORWARD_IF_ERR(ZSTD_CCtx_setParameter( 53 cctx, ZSTD_c_dictIDFlag, !para 53 cctx, ZSTD_c_dictIDFlag, !parameters->fParams.noDictIDFlag)); 54 return 0; 54 return 0; 55 } 55 } 56 56 57 int zstd_min_clevel(void) 57 int zstd_min_clevel(void) 58 { 58 { 59 return ZSTD_minCLevel(); 59 return ZSTD_minCLevel(); 60 } 60 } 61 EXPORT_SYMBOL(zstd_min_clevel); 61 EXPORT_SYMBOL(zstd_min_clevel); 62 62 63 int zstd_max_clevel(void) 63 int zstd_max_clevel(void) 64 { 64 { 65 return ZSTD_maxCLevel(); 65 return ZSTD_maxCLevel(); 66 } 66 } 67 EXPORT_SYMBOL(zstd_max_clevel); 67 EXPORT_SYMBOL(zstd_max_clevel); 68 68 69 int zstd_default_clevel(void) 69 int zstd_default_clevel(void) 70 { 70 { 71 return ZSTD_defaultCLevel(); 71 return ZSTD_defaultCLevel(); 72 } 72 } 73 EXPORT_SYMBOL(zstd_default_clevel); 73 EXPORT_SYMBOL(zstd_default_clevel); 74 74 75 size_t zstd_compress_bound(size_t src_size) 75 size_t zstd_compress_bound(size_t src_size) 76 { 76 { 77 return ZSTD_compressBound(src_size); 77 return ZSTD_compressBound(src_size); 78 } 78 } 79 EXPORT_SYMBOL(zstd_compress_bound); 79 EXPORT_SYMBOL(zstd_compress_bound); 80 80 81 zstd_parameters zstd_get_params(int level, 81 zstd_parameters zstd_get_params(int level, 82 unsigned long long estimated_src_size) 82 unsigned long long estimated_src_size) 83 { 83 { 84 return ZSTD_getParams(level, estimated 84 return ZSTD_getParams(level, estimated_src_size, 0); 85 } 85 } 86 EXPORT_SYMBOL(zstd_get_params); 86 EXPORT_SYMBOL(zstd_get_params); 87 87 88 zstd_compression_parameters zstd_get_cparams(i 88 zstd_compression_parameters zstd_get_cparams(int level, 89 unsigned long long estimated_src_size, 89 unsigned long long estimated_src_size, size_t dict_size) 90 { 90 { 91 return ZSTD_getCParams(level, estimate 91 return ZSTD_getCParams(level, estimated_src_size, dict_size); 92 } 92 } 93 EXPORT_SYMBOL(zstd_get_cparams); 93 EXPORT_SYMBOL(zstd_get_cparams); 94 94 95 size_t zstd_cctx_workspace_bound(const zstd_co 95 size_t zstd_cctx_workspace_bound(const zstd_compression_parameters *cparams) 96 { 96 { 97 return ZSTD_estimateCCtxSize_usingCPar 97 return ZSTD_estimateCCtxSize_usingCParams(*cparams); 98 } 98 } 99 EXPORT_SYMBOL(zstd_cctx_workspace_bound); 99 EXPORT_SYMBOL(zstd_cctx_workspace_bound); 100 100 101 zstd_cctx *zstd_init_cctx(void *workspace, siz 101 zstd_cctx *zstd_init_cctx(void *workspace, size_t workspace_size) 102 { 102 { 103 if (workspace == NULL) 103 if (workspace == NULL) 104 return NULL; 104 return NULL; 105 return ZSTD_initStaticCCtx(workspace, 105 return ZSTD_initStaticCCtx(workspace, workspace_size); 106 } 106 } 107 EXPORT_SYMBOL(zstd_init_cctx); 107 EXPORT_SYMBOL(zstd_init_cctx); 108 108 109 zstd_cctx *zstd_create_cctx_advanced(zstd_cust 109 zstd_cctx *zstd_create_cctx_advanced(zstd_custom_mem custom_mem) 110 { 110 { 111 return ZSTD_createCCtx_advanced(custom 111 return ZSTD_createCCtx_advanced(custom_mem); 112 } 112 } 113 EXPORT_SYMBOL(zstd_create_cctx_advanced); 113 EXPORT_SYMBOL(zstd_create_cctx_advanced); 114 114 115 size_t zstd_free_cctx(zstd_cctx *cctx) 115 size_t zstd_free_cctx(zstd_cctx *cctx) 116 { 116 { 117 return ZSTD_freeCCtx(cctx); 117 return ZSTD_freeCCtx(cctx); 118 } 118 } 119 EXPORT_SYMBOL(zstd_free_cctx); 119 EXPORT_SYMBOL(zstd_free_cctx); 120 120 121 zstd_cdict *zstd_create_cdict_byreference(cons 121 zstd_cdict *zstd_create_cdict_byreference(const void *dict, size_t dict_size, 122 zstd 122 zstd_compression_parameters cparams, 123 zstd 123 zstd_custom_mem custom_mem) 124 { 124 { 125 return ZSTD_createCDict_advanced(dict, 125 return ZSTD_createCDict_advanced(dict, dict_size, ZSTD_dlm_byRef, 126 ZSTD_ 126 ZSTD_dct_auto, cparams, custom_mem); 127 } 127 } 128 EXPORT_SYMBOL(zstd_create_cdict_byreference); 128 EXPORT_SYMBOL(zstd_create_cdict_byreference); 129 129 130 size_t zstd_free_cdict(zstd_cdict *cdict) 130 size_t zstd_free_cdict(zstd_cdict *cdict) 131 { 131 { 132 return ZSTD_freeCDict(cdict); 132 return ZSTD_freeCDict(cdict); 133 } 133 } 134 EXPORT_SYMBOL(zstd_free_cdict); 134 EXPORT_SYMBOL(zstd_free_cdict); 135 135 136 size_t zstd_compress_cctx(zstd_cctx *cctx, voi 136 size_t zstd_compress_cctx(zstd_cctx *cctx, void *dst, size_t dst_capacity, 137 const void *src, size_t src_size, cons 137 const void *src, size_t src_size, const zstd_parameters *parameters) 138 { 138 { 139 ZSTD_FORWARD_IF_ERR(zstd_cctx_init(cct 139 ZSTD_FORWARD_IF_ERR(zstd_cctx_init(cctx, parameters, src_size)); 140 return ZSTD_compress2(cctx, dst, dst_c 140 return ZSTD_compress2(cctx, dst, dst_capacity, src, src_size); 141 } 141 } 142 EXPORT_SYMBOL(zstd_compress_cctx); 142 EXPORT_SYMBOL(zstd_compress_cctx); 143 143 144 size_t zstd_compress_using_cdict(zstd_cctx *cc 144 size_t zstd_compress_using_cdict(zstd_cctx *cctx, void *dst, 145 size_t dst_capacity, const void *src, 145 size_t dst_capacity, const void *src, size_t src_size, 146 const ZSTD_CDict *cdict) 146 const ZSTD_CDict *cdict) 147 { 147 { 148 return ZSTD_compress_usingCDict(cctx, 148 return ZSTD_compress_usingCDict(cctx, dst, dst_capacity, 149 src, s 149 src, src_size, cdict); 150 } 150 } 151 EXPORT_SYMBOL(zstd_compress_using_cdict); 151 EXPORT_SYMBOL(zstd_compress_using_cdict); 152 152 153 size_t zstd_cstream_workspace_bound(const zstd 153 size_t zstd_cstream_workspace_bound(const zstd_compression_parameters *cparams) 154 { 154 { 155 return ZSTD_estimateCStreamSize_usingC 155 return ZSTD_estimateCStreamSize_usingCParams(*cparams); 156 } 156 } 157 EXPORT_SYMBOL(zstd_cstream_workspace_bound); 157 EXPORT_SYMBOL(zstd_cstream_workspace_bound); 158 158 159 zstd_cstream *zstd_init_cstream(const zstd_par 159 zstd_cstream *zstd_init_cstream(const zstd_parameters *parameters, 160 unsigned long long pledged_src_size, v 160 unsigned long long pledged_src_size, void *workspace, size_t workspace_size) 161 { 161 { 162 zstd_cstream *cstream; 162 zstd_cstream *cstream; 163 163 164 if (workspace == NULL) 164 if (workspace == NULL) 165 return NULL; 165 return NULL; 166 166 167 cstream = ZSTD_initStaticCStream(works 167 cstream = ZSTD_initStaticCStream(workspace, workspace_size); 168 if (cstream == NULL) 168 if (cstream == NULL) 169 return NULL; 169 return NULL; 170 170 171 /* 0 means unknown in linux zstd API b 171 /* 0 means unknown in linux zstd API but means 0 in new zstd API */ 172 if (pledged_src_size == 0) 172 if (pledged_src_size == 0) 173 pledged_src_size = ZSTD_CONTEN 173 pledged_src_size = ZSTD_CONTENTSIZE_UNKNOWN; 174 174 175 if (ZSTD_isError(zstd_cctx_init(cstrea 175 if (ZSTD_isError(zstd_cctx_init(cstream, parameters, pledged_src_size))) 176 return NULL; 176 return NULL; 177 177 178 return cstream; 178 return cstream; 179 } 179 } 180 EXPORT_SYMBOL(zstd_init_cstream); 180 EXPORT_SYMBOL(zstd_init_cstream); 181 181 182 size_t zstd_reset_cstream(zstd_cstream *cstrea 182 size_t zstd_reset_cstream(zstd_cstream *cstream, 183 unsigned long long pledged_src_size) 183 unsigned long long pledged_src_size) 184 { 184 { 185 if (pledged_src_size == 0) 185 if (pledged_src_size == 0) 186 pledged_src_size = ZSTD_CONTEN 186 pledged_src_size = ZSTD_CONTENTSIZE_UNKNOWN; 187 ZSTD_FORWARD_IF_ERR( ZSTD_CCtx_reset(c 187 ZSTD_FORWARD_IF_ERR( ZSTD_CCtx_reset(cstream, ZSTD_reset_session_only) ); 188 ZSTD_FORWARD_IF_ERR( ZSTD_CCtx_setPled 188 ZSTD_FORWARD_IF_ERR( ZSTD_CCtx_setPledgedSrcSize(cstream, pledged_src_size) ); 189 return 0; 189 return 0; 190 } 190 } 191 EXPORT_SYMBOL(zstd_reset_cstream); 191 EXPORT_SYMBOL(zstd_reset_cstream); 192 192 193 size_t zstd_compress_stream(zstd_cstream *cstr 193 size_t zstd_compress_stream(zstd_cstream *cstream, zstd_out_buffer *output, 194 zstd_in_buffer *input) 194 zstd_in_buffer *input) 195 { 195 { 196 return ZSTD_compressStream(cstream, ou 196 return ZSTD_compressStream(cstream, output, input); 197 } 197 } 198 EXPORT_SYMBOL(zstd_compress_stream); 198 EXPORT_SYMBOL(zstd_compress_stream); 199 199 200 size_t zstd_flush_stream(zstd_cstream *cstream 200 size_t zstd_flush_stream(zstd_cstream *cstream, zstd_out_buffer *output) 201 { 201 { 202 return ZSTD_flushStream(cstream, outpu 202 return ZSTD_flushStream(cstream, output); 203 } 203 } 204 EXPORT_SYMBOL(zstd_flush_stream); 204 EXPORT_SYMBOL(zstd_flush_stream); 205 205 206 size_t zstd_end_stream(zstd_cstream *cstream, 206 size_t zstd_end_stream(zstd_cstream *cstream, zstd_out_buffer *output) 207 { 207 { 208 return ZSTD_endStream(cstream, output) 208 return ZSTD_endStream(cstream, output); 209 } 209 } 210 EXPORT_SYMBOL(zstd_end_stream); 210 EXPORT_SYMBOL(zstd_end_stream); 211 211 212 MODULE_LICENSE("Dual BSD/GPL"); 212 MODULE_LICENSE("Dual BSD/GPL"); 213 MODULE_DESCRIPTION("Zstd Compressor"); 213 MODULE_DESCRIPTION("Zstd Compressor"); 214 214
Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.