| 1 | /* SPDX-License-Identifier: GPL-2.0+ OR BSD-3-Clause */ | 
|---|
| 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 https://github.com/facebook/zstd) and | 
|---|
| 8 | * the GPLv2 (found in the COPYING file in the root directory of | 
|---|
| 9 | * https://github.com/facebook/zstd). You may select, at your option, one of the | 
|---|
| 10 | * above-listed licenses. | 
|---|
| 11 | */ | 
|---|
| 12 |  | 
|---|
| 13 | #ifndef LINUX_ZSTD_H | 
|---|
| 14 | #define LINUX_ZSTD_H | 
|---|
| 15 |  | 
|---|
| 16 | /** | 
|---|
| 17 | * This is a kernel-style API that wraps the upstream zstd API, which cannot be | 
|---|
| 18 | * used directly because the symbols aren't exported. It exposes the minimal | 
|---|
| 19 | * functionality which is currently required by users of zstd in the kernel. | 
|---|
| 20 | * Expose extra functions from lib/zstd/zstd.h as needed. | 
|---|
| 21 | */ | 
|---|
| 22 |  | 
|---|
| 23 | /* ======   Dependency   ====== */ | 
|---|
| 24 | #include <linux/types.h> | 
|---|
| 25 | #include <linux/zstd_errors.h> | 
|---|
| 26 | #include <linux/zstd_lib.h> | 
|---|
| 27 |  | 
|---|
| 28 | /* ======   Helper Functions   ====== */ | 
|---|
| 29 | /** | 
|---|
| 30 | * zstd_compress_bound() - maximum compressed size in worst case scenario | 
|---|
| 31 | * @src_size: The size of the data to compress. | 
|---|
| 32 | * | 
|---|
| 33 | * Return:    The maximum compressed size in the worst case scenario. | 
|---|
| 34 | */ | 
|---|
| 35 | size_t zstd_compress_bound(size_t src_size); | 
|---|
| 36 |  | 
|---|
| 37 | /** | 
|---|
| 38 | * zstd_is_error() - tells if a size_t function result is an error code | 
|---|
| 39 | * @code:  The function result to check for error. | 
|---|
| 40 | * | 
|---|
| 41 | * Return: Non-zero iff the code is an error. | 
|---|
| 42 | */ | 
|---|
| 43 | unsigned int zstd_is_error(size_t code); | 
|---|
| 44 |  | 
|---|
| 45 | /** | 
|---|
| 46 | * enum zstd_error_code - zstd error codes | 
|---|
| 47 | */ | 
|---|
| 48 | typedef ZSTD_ErrorCode zstd_error_code; | 
|---|
| 49 |  | 
|---|
| 50 | /** | 
|---|
| 51 | * zstd_get_error_code() - translates an error function result to an error code | 
|---|
| 52 | * @code:  The function result for which zstd_is_error(code) is true. | 
|---|
| 53 | * | 
|---|
| 54 | * Return: A unique error code for this error. | 
|---|
| 55 | */ | 
|---|
| 56 | zstd_error_code zstd_get_error_code(size_t code); | 
|---|
| 57 |  | 
|---|
| 58 | /** | 
|---|
| 59 | * zstd_get_error_name() - translates an error function result to a string | 
|---|
| 60 | * @code:  The function result for which zstd_is_error(code) is true. | 
|---|
| 61 | * | 
|---|
| 62 | * Return: An error string corresponding to the error code. | 
|---|
| 63 | */ | 
|---|
| 64 | const char *zstd_get_error_name(size_t code); | 
|---|
| 65 |  | 
|---|
| 66 | /** | 
|---|
| 67 | * zstd_min_clevel() - minimum allowed compression level | 
|---|
| 68 | * | 
|---|
| 69 | * Return: The minimum allowed compression level. | 
|---|
| 70 | */ | 
|---|
| 71 | int zstd_min_clevel(void); | 
|---|
| 72 |  | 
|---|
| 73 | /** | 
|---|
| 74 | * zstd_max_clevel() - maximum allowed compression level | 
|---|
| 75 | * | 
|---|
| 76 | * Return: The maximum allowed compression level. | 
|---|
| 77 | */ | 
|---|
| 78 | int zstd_max_clevel(void); | 
|---|
| 79 |  | 
|---|
| 80 | /** | 
|---|
| 81 | * zstd_default_clevel() - default compression level | 
|---|
| 82 | * | 
|---|
| 83 | * Return: Default compression level. | 
|---|
| 84 | */ | 
|---|
| 85 | int zstd_default_clevel(void); | 
|---|
| 86 |  | 
|---|
| 87 | /** | 
|---|
| 88 | * struct zstd_custom_mem - custom memory allocation | 
|---|
| 89 | */ | 
|---|
| 90 | typedef ZSTD_customMem zstd_custom_mem; | 
|---|
| 91 |  | 
|---|
| 92 | /** | 
|---|
| 93 | * struct zstd_dict_load_method - Dictionary load method. | 
|---|
| 94 | * See zstd_lib.h. | 
|---|
| 95 | */ | 
|---|
| 96 | typedef ZSTD_dictLoadMethod_e zstd_dict_load_method; | 
|---|
| 97 |  | 
|---|
| 98 | /** | 
|---|
| 99 | * struct zstd_dict_content_type - Dictionary context type. | 
|---|
| 100 | * See zstd_lib.h. | 
|---|
| 101 | */ | 
|---|
| 102 | typedef ZSTD_dictContentType_e zstd_dict_content_type; | 
|---|
| 103 |  | 
|---|
| 104 | /* ======   Parameter Selection   ====== */ | 
|---|
| 105 |  | 
|---|
| 106 | /** | 
|---|
| 107 | * enum zstd_strategy - zstd compression search strategy | 
|---|
| 108 | * | 
|---|
| 109 | * From faster to stronger. See zstd_lib.h. | 
|---|
| 110 | */ | 
|---|
| 111 | typedef ZSTD_strategy zstd_strategy; | 
|---|
| 112 |  | 
|---|
| 113 | /** | 
|---|
| 114 | * struct zstd_compression_parameters - zstd compression parameters | 
|---|
| 115 | * @windowLog:    Log of the largest match distance. Larger means more | 
|---|
| 116 | *                compression, and more memory needed during decompression. | 
|---|
| 117 | * @chainLog:     Fully searched segment. Larger means more compression, | 
|---|
| 118 | *                slower, and more memory (useless for fast). | 
|---|
| 119 | * @hashLog:      Dispatch table. Larger means more compression, | 
|---|
| 120 | *                slower, and more memory. | 
|---|
| 121 | * @searchLog:    Number of searches. Larger means more compression and slower. | 
|---|
| 122 | * @searchLength: Match length searched. Larger means faster decompression, | 
|---|
| 123 | *                sometimes less compression. | 
|---|
| 124 | * @targetLength: Acceptable match size for optimal parser (only). Larger means | 
|---|
| 125 | *                more compression, and slower. | 
|---|
| 126 | * @strategy:     The zstd compression strategy. | 
|---|
| 127 | * | 
|---|
| 128 | * See zstd_lib.h. | 
|---|
| 129 | */ | 
|---|
| 130 | typedef ZSTD_compressionParameters zstd_compression_parameters; | 
|---|
| 131 |  | 
|---|
| 132 | /** | 
|---|
| 133 | * struct zstd_frame_parameters - zstd frame parameters | 
|---|
| 134 | * @contentSizeFlag: Controls whether content size will be present in the | 
|---|
| 135 | *                   frame header (when known). | 
|---|
| 136 | * @checksumFlag:    Controls whether a 32-bit checksum is generated at the | 
|---|
| 137 | *                   end of the frame for error detection. | 
|---|
| 138 | * @noDictIDFlag:    Controls whether dictID will be saved into the frame | 
|---|
| 139 | *                   header when using dictionary compression. | 
|---|
| 140 | * | 
|---|
| 141 | * The default value is all fields set to 0. See zstd_lib.h. | 
|---|
| 142 | */ | 
|---|
| 143 | typedef ZSTD_frameParameters zstd_frame_parameters; | 
|---|
| 144 |  | 
|---|
| 145 | /** | 
|---|
| 146 | * struct zstd_parameters - zstd parameters | 
|---|
| 147 | * @cParams: The compression parameters. | 
|---|
| 148 | * @fParams: The frame parameters. | 
|---|
| 149 | */ | 
|---|
| 150 | typedef ZSTD_parameters zstd_parameters; | 
|---|
| 151 |  | 
|---|
| 152 | /** | 
|---|
| 153 | * zstd_get_params() - returns zstd_parameters for selected level | 
|---|
| 154 | * @level:              The compression level | 
|---|
| 155 | * @estimated_src_size: The estimated source size to compress or 0 | 
|---|
| 156 | *                      if unknown. | 
|---|
| 157 | * | 
|---|
| 158 | * Return:              The selected zstd_parameters. | 
|---|
| 159 | */ | 
|---|
| 160 | zstd_parameters zstd_get_params(int level, | 
|---|
| 161 | unsigned long long estimated_src_size); | 
|---|
| 162 |  | 
|---|
| 163 | /** | 
|---|
| 164 | * zstd_get_cparams() - returns zstd_compression_parameters for selected level | 
|---|
| 165 | * @level:              The compression level | 
|---|
| 166 | * @estimated_src_size: The estimated source size to compress or 0 | 
|---|
| 167 | *                      if unknown. | 
|---|
| 168 | * @dict_size:          Dictionary size. | 
|---|
| 169 | * | 
|---|
| 170 | * Return:              The selected zstd_compression_parameters. | 
|---|
| 171 | */ | 
|---|
| 172 | zstd_compression_parameters zstd_get_cparams(int level, | 
|---|
| 173 | unsigned long long estimated_src_size, size_t dict_size); | 
|---|
| 174 |  | 
|---|
| 175 | typedef ZSTD_CCtx zstd_cctx; | 
|---|
| 176 | typedef ZSTD_cParameter zstd_cparameter; | 
|---|
| 177 |  | 
|---|
| 178 | /** | 
|---|
| 179 | * zstd_cctx_set_param() - sets a compression parameter | 
|---|
| 180 | * @cctx:         The context. Must have been initialized with zstd_init_cctx(). | 
|---|
| 181 | * @param:        The parameter to set. | 
|---|
| 182 | * @value:        The value to set the parameter to. | 
|---|
| 183 | * | 
|---|
| 184 | * Return:        Zero or an error, which can be checked using zstd_is_error(). | 
|---|
| 185 | */ | 
|---|
| 186 | size_t zstd_cctx_set_param(zstd_cctx *cctx, zstd_cparameter param, int value); | 
|---|
| 187 |  | 
|---|
| 188 | /* ======   Single-pass Compression   ====== */ | 
|---|
| 189 |  | 
|---|
| 190 | /** | 
|---|
| 191 | * zstd_cctx_workspace_bound() - max memory needed to initialize a zstd_cctx | 
|---|
| 192 | * @parameters: The compression parameters to be used. | 
|---|
| 193 | * | 
|---|
| 194 | * If multiple compression parameters might be used, the caller must call | 
|---|
| 195 | * zstd_cctx_workspace_bound() for each set of parameters and use the maximum | 
|---|
| 196 | * size. | 
|---|
| 197 | * | 
|---|
| 198 | * Return:      A lower bound on the size of the workspace that is passed to | 
|---|
| 199 | *              zstd_init_cctx(). | 
|---|
| 200 | */ | 
|---|
| 201 | size_t zstd_cctx_workspace_bound(const zstd_compression_parameters *parameters); | 
|---|
| 202 |  | 
|---|
| 203 | /** | 
|---|
| 204 | * zstd_cctx_workspace_bound_with_ext_seq_prod() - max memory needed to | 
|---|
| 205 | * initialize a zstd_cctx when using the block-level external sequence | 
|---|
| 206 | * producer API. | 
|---|
| 207 | * @parameters: The compression parameters to be used. | 
|---|
| 208 | * | 
|---|
| 209 | * If multiple compression parameters might be used, the caller must call | 
|---|
| 210 | * this function for each set of parameters and use the maximum size. | 
|---|
| 211 | * | 
|---|
| 212 | * Return:      A lower bound on the size of the workspace that is passed to | 
|---|
| 213 | *              zstd_init_cctx(). | 
|---|
| 214 | */ | 
|---|
| 215 | size_t zstd_cctx_workspace_bound_with_ext_seq_prod(const zstd_compression_parameters *parameters); | 
|---|
| 216 |  | 
|---|
| 217 | /** | 
|---|
| 218 | * zstd_init_cctx() - initialize a zstd compression context | 
|---|
| 219 | * @workspace:      The workspace to emplace the context into. It must outlive | 
|---|
| 220 | *                  the returned context. | 
|---|
| 221 | * @workspace_size: The size of workspace. Use zstd_cctx_workspace_bound() to | 
|---|
| 222 | *                  determine how large the workspace must be. | 
|---|
| 223 | * | 
|---|
| 224 | * Return:          A zstd compression context or NULL on error. | 
|---|
| 225 | */ | 
|---|
| 226 | zstd_cctx *zstd_init_cctx(void *workspace, size_t workspace_size); | 
|---|
| 227 |  | 
|---|
| 228 | /** | 
|---|
| 229 | * zstd_compress_cctx() - compress src into dst with the initialized parameters | 
|---|
| 230 | * @cctx:         The context. Must have been initialized with zstd_init_cctx(). | 
|---|
| 231 | * @dst:          The buffer to compress src into. | 
|---|
| 232 | * @dst_capacity: The size of the destination buffer. May be any size, but | 
|---|
| 233 | *                ZSTD_compressBound(srcSize) is guaranteed to be large enough. | 
|---|
| 234 | * @src:          The data to compress. | 
|---|
| 235 | * @src_size:     The size of the data to compress. | 
|---|
| 236 | * @parameters:   The compression parameters to be used. | 
|---|
| 237 | * | 
|---|
| 238 | * Return:        The compressed size or an error, which can be checked using | 
|---|
| 239 | *                zstd_is_error(). | 
|---|
| 240 | */ | 
|---|
| 241 | size_t zstd_compress_cctx(zstd_cctx *cctx, void *dst, size_t dst_capacity, | 
|---|
| 242 | const void *src, size_t src_size, const zstd_parameters *parameters); | 
|---|
| 243 |  | 
|---|
| 244 | /** | 
|---|
| 245 | * zstd_create_cctx_advanced() - Create compression context | 
|---|
| 246 | * @custom_mem:   Custom allocator. | 
|---|
| 247 | * | 
|---|
| 248 | * Return:        NULL on error, pointer to compression context otherwise. | 
|---|
| 249 | */ | 
|---|
| 250 | zstd_cctx *zstd_create_cctx_advanced(zstd_custom_mem custom_mem); | 
|---|
| 251 |  | 
|---|
| 252 | /** | 
|---|
| 253 | * zstd_free_cctx() - Free compression context | 
|---|
| 254 | * @cdict:        Pointer to compression context. | 
|---|
| 255 | * | 
|---|
| 256 | * Return:        Always 0. | 
|---|
| 257 | */ | 
|---|
| 258 | size_t zstd_free_cctx(zstd_cctx* cctx); | 
|---|
| 259 |  | 
|---|
| 260 | /** | 
|---|
| 261 | * struct zstd_cdict - Compression dictionary. | 
|---|
| 262 | * See zstd_lib.h. | 
|---|
| 263 | */ | 
|---|
| 264 | typedef ZSTD_CDict zstd_cdict; | 
|---|
| 265 |  | 
|---|
| 266 | /** | 
|---|
| 267 | * zstd_create_cdict_byreference() - Create compression dictionary | 
|---|
| 268 | * @dict:              Pointer to dictionary buffer. | 
|---|
| 269 | * @dict_size:         Size of the dictionary buffer. | 
|---|
| 270 | * @dict_load_method:  Dictionary load method. | 
|---|
| 271 | * @dict_content_type: Dictionary content type. | 
|---|
| 272 | * @custom_mem:        Memory allocator. | 
|---|
| 273 | * | 
|---|
| 274 | * Note, this uses @dict by reference (ZSTD_dlm_byRef), so it should be | 
|---|
| 275 | * free before zstd_cdict is destroyed. | 
|---|
| 276 | * | 
|---|
| 277 | * Return:             NULL on error, pointer to compression dictionary | 
|---|
| 278 | *                     otherwise. | 
|---|
| 279 | */ | 
|---|
| 280 | zstd_cdict *zstd_create_cdict_byreference(const void *dict, size_t dict_size, | 
|---|
| 281 | zstd_compression_parameters cparams, | 
|---|
| 282 | zstd_custom_mem custom_mem); | 
|---|
| 283 |  | 
|---|
| 284 | /** | 
|---|
| 285 | * zstd_free_cdict() - Free compression dictionary | 
|---|
| 286 | * @cdict:        Pointer to compression dictionary. | 
|---|
| 287 | * | 
|---|
| 288 | * Return:        Always 0. | 
|---|
| 289 | */ | 
|---|
| 290 | size_t zstd_free_cdict(zstd_cdict* cdict); | 
|---|
| 291 |  | 
|---|
| 292 | /** | 
|---|
| 293 | * zstd_compress_using_cdict() - compress src into dst using a dictionary | 
|---|
| 294 | * @cctx:         The context. Must have been initialized with zstd_init_cctx(). | 
|---|
| 295 | * @dst:          The buffer to compress src into. | 
|---|
| 296 | * @dst_capacity: The size of the destination buffer. May be any size, but | 
|---|
| 297 | *                ZSTD_compressBound(srcSize) is guaranteed to be large enough. | 
|---|
| 298 | * @src:          The data to compress. | 
|---|
| 299 | * @src_size:     The size of the data to compress. | 
|---|
| 300 | * @cdict:        The dictionary to be used. | 
|---|
| 301 | * | 
|---|
| 302 | * Return:        The compressed size or an error, which can be checked using | 
|---|
| 303 | *                zstd_is_error(). | 
|---|
| 304 | */ | 
|---|
| 305 | size_t zstd_compress_using_cdict(zstd_cctx *cctx, void *dst, | 
|---|
| 306 | size_t dst_capacity, const void *src, size_t src_size, | 
|---|
| 307 | const zstd_cdict *cdict); | 
|---|
| 308 |  | 
|---|
| 309 | /* ======   Single-pass Decompression   ====== */ | 
|---|
| 310 |  | 
|---|
| 311 | typedef ZSTD_DCtx zstd_dctx; | 
|---|
| 312 |  | 
|---|
| 313 | /** | 
|---|
| 314 | * zstd_dctx_workspace_bound() - max memory needed to initialize a zstd_dctx | 
|---|
| 315 | * | 
|---|
| 316 | * Return: A lower bound on the size of the workspace that is passed to | 
|---|
| 317 | *         zstd_init_dctx(). | 
|---|
| 318 | */ | 
|---|
| 319 | size_t zstd_dctx_workspace_bound(void); | 
|---|
| 320 |  | 
|---|
| 321 | /** | 
|---|
| 322 | * zstd_init_dctx() - initialize a zstd decompression context | 
|---|
| 323 | * @workspace:      The workspace to emplace the context into. It must outlive | 
|---|
| 324 | *                  the returned context. | 
|---|
| 325 | * @workspace_size: The size of workspace. Use zstd_dctx_workspace_bound() to | 
|---|
| 326 | *                  determine how large the workspace must be. | 
|---|
| 327 | * | 
|---|
| 328 | * Return:          A zstd decompression context or NULL on error. | 
|---|
| 329 | */ | 
|---|
| 330 | zstd_dctx *zstd_init_dctx(void *workspace, size_t workspace_size); | 
|---|
| 331 |  | 
|---|
| 332 | /** | 
|---|
| 333 | * zstd_decompress_dctx() - decompress zstd compressed src into dst | 
|---|
| 334 | * @dctx:         The decompression context. | 
|---|
| 335 | * @dst:          The buffer to decompress src into. | 
|---|
| 336 | * @dst_capacity: The size of the destination buffer. Must be at least as large | 
|---|
| 337 | *                as the decompressed size. If the caller cannot upper bound the | 
|---|
| 338 | *                decompressed size, then it's better to use the streaming API. | 
|---|
| 339 | * @src:          The zstd compressed data to decompress. Multiple concatenated | 
|---|
| 340 | *                frames and skippable frames are allowed. | 
|---|
| 341 | * @src_size:     The exact size of the data to decompress. | 
|---|
| 342 | * | 
|---|
| 343 | * Return:        The decompressed size or an error, which can be checked using | 
|---|
| 344 | *                zstd_is_error(). | 
|---|
| 345 | */ | 
|---|
| 346 | size_t zstd_decompress_dctx(zstd_dctx *dctx, void *dst, size_t dst_capacity, | 
|---|
| 347 | const void *src, size_t src_size); | 
|---|
| 348 |  | 
|---|
| 349 | /** | 
|---|
| 350 | * struct zstd_ddict - Decompression dictionary. | 
|---|
| 351 | * See zstd_lib.h. | 
|---|
| 352 | */ | 
|---|
| 353 | typedef ZSTD_DDict zstd_ddict; | 
|---|
| 354 |  | 
|---|
| 355 | /** | 
|---|
| 356 | * zstd_create_ddict_byreference() - Create decompression dictionary | 
|---|
| 357 | * @dict:              Pointer to dictionary buffer. | 
|---|
| 358 | * @dict_size:         Size of the dictionary buffer. | 
|---|
| 359 | * @dict_load_method:  Dictionary load method. | 
|---|
| 360 | * @dict_content_type: Dictionary content type. | 
|---|
| 361 | * @custom_mem:        Memory allocator. | 
|---|
| 362 | * | 
|---|
| 363 | * Note, this uses @dict by reference (ZSTD_dlm_byRef), so it should be | 
|---|
| 364 | * free before zstd_ddict is destroyed. | 
|---|
| 365 | * | 
|---|
| 366 | * Return:             NULL on error, pointer to decompression dictionary | 
|---|
| 367 | *                     otherwise. | 
|---|
| 368 | */ | 
|---|
| 369 | zstd_ddict *zstd_create_ddict_byreference(const void *dict, size_t dict_size, | 
|---|
| 370 | zstd_custom_mem custom_mem); | 
|---|
| 371 | /** | 
|---|
| 372 | * zstd_free_ddict() - Free decompression dictionary | 
|---|
| 373 | * @dict:         Pointer to the dictionary. | 
|---|
| 374 | * | 
|---|
| 375 | * Return:        Always 0. | 
|---|
| 376 | */ | 
|---|
| 377 | size_t zstd_free_ddict(zstd_ddict *ddict); | 
|---|
| 378 |  | 
|---|
| 379 | /** | 
|---|
| 380 | * zstd_create_dctx_advanced() - Create decompression context | 
|---|
| 381 | * @custom_mem:   Custom allocator. | 
|---|
| 382 | * | 
|---|
| 383 | * Return:        NULL on error, pointer to decompression context otherwise. | 
|---|
| 384 | */ | 
|---|
| 385 | zstd_dctx *zstd_create_dctx_advanced(zstd_custom_mem custom_mem); | 
|---|
| 386 |  | 
|---|
| 387 | /** | 
|---|
| 388 | * zstd_free_dctx() -- Free decompression context | 
|---|
| 389 | * @dctx:         Pointer to decompression context. | 
|---|
| 390 | * Return:        Always 0. | 
|---|
| 391 | */ | 
|---|
| 392 | size_t zstd_free_dctx(zstd_dctx *dctx); | 
|---|
| 393 |  | 
|---|
| 394 | /** | 
|---|
| 395 | * zstd_decompress_using_ddict() - decompress src into dst using a dictionary | 
|---|
| 396 | * @dctx:         The decompression context. | 
|---|
| 397 | * @dst:          The buffer to decompress src into. | 
|---|
| 398 | * @dst_capacity: The size of the destination buffer. Must be at least as large | 
|---|
| 399 | *                as the decompressed size. If the caller cannot upper bound the | 
|---|
| 400 | *                decompressed size, then it's better to use the streaming API. | 
|---|
| 401 | * @src:          The zstd compressed data to decompress. Multiple concatenated | 
|---|
| 402 | *                frames and skippable frames are allowed. | 
|---|
| 403 | * @src_size:     The exact size of the data to decompress. | 
|---|
| 404 | * @ddict:        The dictionary to be used. | 
|---|
| 405 | * | 
|---|
| 406 | * Return:        The decompressed size or an error, which can be checked using | 
|---|
| 407 | *                zstd_is_error(). | 
|---|
| 408 | */ | 
|---|
| 409 | size_t zstd_decompress_using_ddict(zstd_dctx *dctx, | 
|---|
| 410 | void *dst, size_t dst_capacity, const void *src, size_t src_size, | 
|---|
| 411 | const zstd_ddict *ddict); | 
|---|
| 412 |  | 
|---|
| 413 |  | 
|---|
| 414 | /* ======   Streaming Buffers   ====== */ | 
|---|
| 415 |  | 
|---|
| 416 | /** | 
|---|
| 417 | * struct zstd_in_buffer - input buffer for streaming | 
|---|
| 418 | * @src:  Start of the input buffer. | 
|---|
| 419 | * @size: Size of the input buffer. | 
|---|
| 420 | * @pos:  Position where reading stopped. Will be updated. | 
|---|
| 421 | *        Necessarily 0 <= pos <= size. | 
|---|
| 422 | * | 
|---|
| 423 | * See zstd_lib.h. | 
|---|
| 424 | */ | 
|---|
| 425 | typedef ZSTD_inBuffer zstd_in_buffer; | 
|---|
| 426 |  | 
|---|
| 427 | /** | 
|---|
| 428 | * struct zstd_out_buffer - output buffer for streaming | 
|---|
| 429 | * @dst:  Start of the output buffer. | 
|---|
| 430 | * @size: Size of the output buffer. | 
|---|
| 431 | * @pos:  Position where writing stopped. Will be updated. | 
|---|
| 432 | *        Necessarily 0 <= pos <= size. | 
|---|
| 433 | * | 
|---|
| 434 | * See zstd_lib.h. | 
|---|
| 435 | */ | 
|---|
| 436 | typedef ZSTD_outBuffer zstd_out_buffer; | 
|---|
| 437 |  | 
|---|
| 438 | /* ======   Streaming Compression   ====== */ | 
|---|
| 439 |  | 
|---|
| 440 | typedef ZSTD_CStream zstd_cstream; | 
|---|
| 441 |  | 
|---|
| 442 | /** | 
|---|
| 443 | * zstd_cstream_workspace_bound() - memory needed to initialize a zstd_cstream | 
|---|
| 444 | * @cparams: The compression parameters to be used for compression. | 
|---|
| 445 | * | 
|---|
| 446 | * Return:   A lower bound on the size of the workspace that is passed to | 
|---|
| 447 | *           zstd_init_cstream(). | 
|---|
| 448 | */ | 
|---|
| 449 | size_t zstd_cstream_workspace_bound(const zstd_compression_parameters *cparams); | 
|---|
| 450 |  | 
|---|
| 451 | /** | 
|---|
| 452 | * zstd_cstream_workspace_bound_with_ext_seq_prod() - memory needed to initialize | 
|---|
| 453 | * a zstd_cstream when using the block-level external sequence producer API. | 
|---|
| 454 | * @cparams: The compression parameters to be used for compression. | 
|---|
| 455 | * | 
|---|
| 456 | * Return:   A lower bound on the size of the workspace that is passed to | 
|---|
| 457 | *           zstd_init_cstream(). | 
|---|
| 458 | */ | 
|---|
| 459 | size_t zstd_cstream_workspace_bound_with_ext_seq_prod(const zstd_compression_parameters *cparams); | 
|---|
| 460 |  | 
|---|
| 461 | /** | 
|---|
| 462 | * zstd_init_cstream() - initialize a zstd streaming compression context | 
|---|
| 463 | * @parameters        The zstd parameters to use for compression. | 
|---|
| 464 | * @pledged_src_size: If params.fParams.contentSizeFlag == 1 then the caller | 
|---|
| 465 | *                    must pass the source size (zero means empty source). | 
|---|
| 466 | *                    Otherwise, the caller may optionally pass the source | 
|---|
| 467 | *                    size, or zero if unknown. | 
|---|
| 468 | * @workspace:        The workspace to emplace the context into. It must outlive | 
|---|
| 469 | *                    the returned context. | 
|---|
| 470 | * @workspace_size:   The size of workspace. | 
|---|
| 471 | *                    Use zstd_cstream_workspace_bound(params->cparams) to | 
|---|
| 472 | *                    determine how large the workspace must be. | 
|---|
| 473 | * | 
|---|
| 474 | * Return:            The zstd streaming compression context or NULL on error. | 
|---|
| 475 | */ | 
|---|
| 476 | zstd_cstream *zstd_init_cstream(const zstd_parameters *parameters, | 
|---|
| 477 | unsigned long long pledged_src_size, void *workspace, size_t workspace_size); | 
|---|
| 478 |  | 
|---|
| 479 | /** | 
|---|
| 480 | * zstd_reset_cstream() - reset the context using parameters from creation | 
|---|
| 481 | * @cstream:          The zstd streaming compression context to reset. | 
|---|
| 482 | * @pledged_src_size: Optionally the source size, or zero if unknown. | 
|---|
| 483 | * | 
|---|
| 484 | * Resets the context using the parameters from creation. Skips dictionary | 
|---|
| 485 | * loading, since it can be reused. If `pledged_src_size` is non-zero the frame | 
|---|
| 486 | * content size is always written into the frame header. | 
|---|
| 487 | * | 
|---|
| 488 | * Return:            Zero or an error, which can be checked using | 
|---|
| 489 | *                    zstd_is_error(). | 
|---|
| 490 | */ | 
|---|
| 491 | size_t zstd_reset_cstream(zstd_cstream *cstream, | 
|---|
| 492 | unsigned long long pledged_src_size); | 
|---|
| 493 |  | 
|---|
| 494 | /** | 
|---|
| 495 | * zstd_compress_stream() - streaming compress some of input into output | 
|---|
| 496 | * @cstream: The zstd streaming compression context. | 
|---|
| 497 | * @output:  Destination buffer. `output->pos` is updated to indicate how much | 
|---|
| 498 | *           compressed data was written. | 
|---|
| 499 | * @input:   Source buffer. `input->pos` is updated to indicate how much data | 
|---|
| 500 | *           was read. Note that it may not consume the entire input, in which | 
|---|
| 501 | *           case `input->pos < input->size`, and it's up to the caller to | 
|---|
| 502 | *           present remaining data again. | 
|---|
| 503 | * | 
|---|
| 504 | * The `input` and `output` buffers may be any size. Guaranteed to make some | 
|---|
| 505 | * forward progress if `input` and `output` are not empty. | 
|---|
| 506 | * | 
|---|
| 507 | * Return:   A hint for the number of bytes to use as the input for the next | 
|---|
| 508 | *           function call or an error, which can be checked using | 
|---|
| 509 | *           zstd_is_error(). | 
|---|
| 510 | */ | 
|---|
| 511 | size_t zstd_compress_stream(zstd_cstream *cstream, zstd_out_buffer *output, | 
|---|
| 512 | zstd_in_buffer *input); | 
|---|
| 513 |  | 
|---|
| 514 | /** | 
|---|
| 515 | * zstd_flush_stream() - flush internal buffers into output | 
|---|
| 516 | * @cstream: The zstd streaming compression context. | 
|---|
| 517 | * @output:  Destination buffer. `output->pos` is updated to indicate how much | 
|---|
| 518 | *           compressed data was written. | 
|---|
| 519 | * | 
|---|
| 520 | * zstd_flush_stream() must be called until it returns 0, meaning all the data | 
|---|
| 521 | * has been flushed. Since zstd_flush_stream() causes a block to be ended, | 
|---|
| 522 | * calling it too often will degrade the compression ratio. | 
|---|
| 523 | * | 
|---|
| 524 | * Return:   The number of bytes still present within internal buffers or an | 
|---|
| 525 | *           error, which can be checked using zstd_is_error(). | 
|---|
| 526 | */ | 
|---|
| 527 | size_t zstd_flush_stream(zstd_cstream *cstream, zstd_out_buffer *output); | 
|---|
| 528 |  | 
|---|
| 529 | /** | 
|---|
| 530 | * zstd_end_stream() - flush internal buffers into output and end the frame | 
|---|
| 531 | * @cstream: The zstd streaming compression context. | 
|---|
| 532 | * @output:  Destination buffer. `output->pos` is updated to indicate how much | 
|---|
| 533 | *           compressed data was written. | 
|---|
| 534 | * | 
|---|
| 535 | * zstd_end_stream() must be called until it returns 0, meaning all the data has | 
|---|
| 536 | * been flushed and the frame epilogue has been written. | 
|---|
| 537 | * | 
|---|
| 538 | * Return:   The number of bytes still present within internal buffers or an | 
|---|
| 539 | *           error, which can be checked using zstd_is_error(). | 
|---|
| 540 | */ | 
|---|
| 541 | size_t zstd_end_stream(zstd_cstream *cstream, zstd_out_buffer *output); | 
|---|
| 542 |  | 
|---|
| 543 | /* ======   Streaming Decompression   ====== */ | 
|---|
| 544 |  | 
|---|
| 545 | typedef ZSTD_DStream zstd_dstream; | 
|---|
| 546 |  | 
|---|
| 547 | /** | 
|---|
| 548 | * zstd_dstream_workspace_bound() - memory needed to initialize a zstd_dstream | 
|---|
| 549 | * @max_window_size: The maximum window size allowed for compressed frames. | 
|---|
| 550 | * | 
|---|
| 551 | * Return:           A lower bound on the size of the workspace that is passed | 
|---|
| 552 | *                   to zstd_init_dstream(). | 
|---|
| 553 | */ | 
|---|
| 554 | size_t zstd_dstream_workspace_bound(size_t max_window_size); | 
|---|
| 555 |  | 
|---|
| 556 | /** | 
|---|
| 557 | * zstd_init_dstream() - initialize a zstd streaming decompression context | 
|---|
| 558 | * @max_window_size: The maximum window size allowed for compressed frames. | 
|---|
| 559 | * @workspace:       The workspace to emplace the context into. It must outlive | 
|---|
| 560 | *                   the returned context. | 
|---|
| 561 | * @workspaceSize:   The size of workspace. | 
|---|
| 562 | *                   Use zstd_dstream_workspace_bound(max_window_size) to | 
|---|
| 563 | *                   determine how large the workspace must be. | 
|---|
| 564 | * | 
|---|
| 565 | * Return:           The zstd streaming decompression context. | 
|---|
| 566 | */ | 
|---|
| 567 | zstd_dstream *zstd_init_dstream(size_t max_window_size, void *workspace, | 
|---|
| 568 | size_t workspace_size); | 
|---|
| 569 |  | 
|---|
| 570 | /** | 
|---|
| 571 | * zstd_reset_dstream() - reset the context using parameters from creation | 
|---|
| 572 | * @dstream: The zstd streaming decompression context to reset. | 
|---|
| 573 | * | 
|---|
| 574 | * Resets the context using the parameters from creation. Skips dictionary | 
|---|
| 575 | * loading, since it can be reused. | 
|---|
| 576 | * | 
|---|
| 577 | * Return:   Zero or an error, which can be checked using zstd_is_error(). | 
|---|
| 578 | */ | 
|---|
| 579 | size_t zstd_reset_dstream(zstd_dstream *dstream); | 
|---|
| 580 |  | 
|---|
| 581 | /** | 
|---|
| 582 | * zstd_decompress_stream() - streaming decompress some of input into output | 
|---|
| 583 | * @dstream: The zstd streaming decompression context. | 
|---|
| 584 | * @output:  Destination buffer. `output.pos` is updated to indicate how much | 
|---|
| 585 | *           decompressed data was written. | 
|---|
| 586 | * @input:   Source buffer. `input.pos` is updated to indicate how much data was | 
|---|
| 587 | *           read. Note that it may not consume the entire input, in which case | 
|---|
| 588 | *           `input.pos < input.size`, and it's up to the caller to present | 
|---|
| 589 | *           remaining data again. | 
|---|
| 590 | * | 
|---|
| 591 | * The `input` and `output` buffers may be any size. Guaranteed to make some | 
|---|
| 592 | * forward progress if `input` and `output` are not empty. | 
|---|
| 593 | * zstd_decompress_stream() will not consume the last byte of the frame until | 
|---|
| 594 | * the entire frame is flushed. | 
|---|
| 595 | * | 
|---|
| 596 | * Return:   Returns 0 iff a frame is completely decoded and fully flushed. | 
|---|
| 597 | *           Otherwise returns a hint for the number of bytes to use as the | 
|---|
| 598 | *           input for the next function call or an error, which can be checked | 
|---|
| 599 | *           using zstd_is_error(). The size hint will never load more than the | 
|---|
| 600 | *           frame. | 
|---|
| 601 | */ | 
|---|
| 602 | size_t zstd_decompress_stream(zstd_dstream *dstream, zstd_out_buffer *output, | 
|---|
| 603 | zstd_in_buffer *input); | 
|---|
| 604 |  | 
|---|
| 605 | /* ======   Frame Inspection Functions ====== */ | 
|---|
| 606 |  | 
|---|
| 607 | /** | 
|---|
| 608 | * zstd_find_frame_compressed_size() - returns the size of a compressed frame | 
|---|
| 609 | * @src:      Source buffer. It should point to the start of a zstd encoded | 
|---|
| 610 | *            frame or a skippable frame. | 
|---|
| 611 | * @src_size: The size of the source buffer. It must be at least as large as the | 
|---|
| 612 | *            size of the frame. | 
|---|
| 613 | * | 
|---|
| 614 | * Return:    The compressed size of the frame pointed to by `src` or an error, | 
|---|
| 615 | *            which can be check with zstd_is_error(). | 
|---|
| 616 | *            Suitable to pass to ZSTD_decompress() or similar functions. | 
|---|
| 617 | */ | 
|---|
| 618 | size_t zstd_find_frame_compressed_size(const void *src, size_t src_size); | 
|---|
| 619 |  | 
|---|
| 620 | /** | 
|---|
| 621 | * zstd_register_sequence_producer() - exposes the zstd library function | 
|---|
| 622 | * ZSTD_registerSequenceProducer(). This is used for the block-level external | 
|---|
| 623 | * sequence producer API. See upstream zstd.h for detailed documentation. | 
|---|
| 624 | */ | 
|---|
| 625 | typedef ZSTD_sequenceProducer_F zstd_sequence_producer_f; | 
|---|
| 626 | void zstd_register_sequence_producer( | 
|---|
| 627 | zstd_cctx *cctx, | 
|---|
| 628 | void* sequence_producer_state, | 
|---|
| 629 | zstd_sequence_producer_f sequence_producer | 
|---|
| 630 | ); | 
|---|
| 631 |  | 
|---|
| 632 | /** | 
|---|
| 633 | * struct zstd_frame_params - zstd frame parameters stored in the frame header | 
|---|
| 634 | * @frameContentSize: The frame content size, or ZSTD_CONTENTSIZE_UNKNOWN if not | 
|---|
| 635 | *                    present. | 
|---|
| 636 | * @windowSize:       The window size, or 0 if the frame is a skippable frame. | 
|---|
| 637 | * @blockSizeMax:     The maximum block size. | 
|---|
| 638 | * @frameType:        The frame type (zstd or skippable) | 
|---|
| 639 | * @headerSize:       The size of the frame header. | 
|---|
| 640 | * @dictID:           The dictionary id, or 0 if not present. | 
|---|
| 641 | * @checksumFlag:     Whether a checksum was used. | 
|---|
| 642 | * | 
|---|
| 643 | * See zstd_lib.h. | 
|---|
| 644 | */ | 
|---|
| 645 | typedef ZSTD_FrameHeader ; | 
|---|
| 646 |  | 
|---|
| 647 | /** | 
|---|
| 648 | * zstd_get_frame_header() - extracts parameters from a zstd or skippable frame | 
|---|
| 649 | * @params:   On success the frame parameters are written here. | 
|---|
| 650 | * @src:      The source buffer. It must point to a zstd or skippable frame. | 
|---|
| 651 | * @src_size: The size of the source buffer. | 
|---|
| 652 | * | 
|---|
| 653 | * Return:    0 on success. If more data is required it returns how many bytes | 
|---|
| 654 | *            must be provided to make forward progress. Otherwise it returns | 
|---|
| 655 | *            an error, which can be checked using zstd_is_error(). | 
|---|
| 656 | */ | 
|---|
| 657 | size_t (zstd_frame_header *params, const void *src, | 
|---|
| 658 | size_t src_size); | 
|---|
| 659 |  | 
|---|
| 660 | /** | 
|---|
| 661 | * struct zstd_sequence - a sequence of literals or a match | 
|---|
| 662 | * | 
|---|
| 663 | * @offset: The offset of the match | 
|---|
| 664 | * @litLength: The literal length of the sequence | 
|---|
| 665 | * @matchLength: The match length of the sequence | 
|---|
| 666 | * @rep: Represents which repeat offset is used | 
|---|
| 667 | */ | 
|---|
| 668 | typedef ZSTD_Sequence zstd_sequence; | 
|---|
| 669 |  | 
|---|
| 670 | /** | 
|---|
| 671 | * zstd_compress_sequences_and_literals() - compress an array of zstd_sequence and literals | 
|---|
| 672 | * | 
|---|
| 673 | * @cctx: The zstd compression context. | 
|---|
| 674 | * @dst: The buffer to compress the data into. | 
|---|
| 675 | * @dst_capacity: The size of the destination buffer. | 
|---|
| 676 | * @in_seqs: The array of zstd_sequence to compress. | 
|---|
| 677 | * @in_seqs_size: The number of sequences in in_seqs. | 
|---|
| 678 | * @literals: The literals associated to the sequences to be compressed. | 
|---|
| 679 | * @lit_size: The size of the literals in the literals buffer. | 
|---|
| 680 | * @lit_capacity: The size of the literals buffer. | 
|---|
| 681 | * @decompressed_size: The size of the input data | 
|---|
| 682 | * | 
|---|
| 683 | * Return: The compressed size or an error, which can be checked using | 
|---|
| 684 | * 	   zstd_is_error(). | 
|---|
| 685 | */ | 
|---|
| 686 | size_t zstd_compress_sequences_and_literals(zstd_cctx *cctx, void* dst, size_t dst_capacity, | 
|---|
| 687 | const zstd_sequence *in_seqs, size_t in_seqs_size, | 
|---|
| 688 | const void* literals, size_t lit_size, size_t lit_capacity, | 
|---|
| 689 | size_t decompressed_size); | 
|---|
| 690 |  | 
|---|
| 691 | #endif  /* LINUX_ZSTD_H */ | 
|---|
| 692 |  | 
|---|