1e0c1b49fSNick Terrell /* SPDX-License-Identifier: GPL-2.0+ OR BSD-3-Clause */ 273f3d1b4SNick Terrell /* 3*65d1f550SNick Terrell * Copyright (c) Meta Platforms, Inc. and affiliates. 473f3d1b4SNick Terrell * All rights reserved. 573f3d1b4SNick Terrell * 6cf30f6a5SNick Terrell * This source code is licensed under both the BSD-style license (found in the 7cf30f6a5SNick Terrell * LICENSE file in the root directory of https://github.com/facebook/zstd) and 8cf30f6a5SNick Terrell * the GPLv2 (found in the COPYING file in the root directory of 9cf30f6a5SNick Terrell * https://github.com/facebook/zstd). You may select, at your option, one of the 10cf30f6a5SNick Terrell * above-listed licenses. 1173f3d1b4SNick Terrell */ 1273f3d1b4SNick Terrell 13cf30f6a5SNick Terrell #ifndef LINUX_ZSTD_H 14cf30f6a5SNick Terrell #define LINUX_ZSTD_H 15cf30f6a5SNick Terrell 16cf30f6a5SNick Terrell /** 17cf30f6a5SNick Terrell * This is a kernel-style API that wraps the upstream zstd API, which cannot be 18cf30f6a5SNick Terrell * used directly because the symbols aren't exported. It exposes the minimal 19cf30f6a5SNick Terrell * functionality which is currently required by users of zstd in the kernel. 20cf30f6a5SNick Terrell * Expose extra functions from lib/zstd/zstd.h as needed. 21cf30f6a5SNick Terrell */ 2273f3d1b4SNick Terrell 2373f3d1b4SNick Terrell /* ====== Dependency ====== */ 24cf30f6a5SNick Terrell #include <linux/types.h> 25e0c1b49fSNick Terrell #include <linux/zstd_errors.h> 26cf30f6a5SNick Terrell #include <linux/zstd_lib.h> 2773f3d1b4SNick Terrell 28cf30f6a5SNick Terrell /* ====== Helper Functions ====== */ 2973f3d1b4SNick Terrell /** 30cf30f6a5SNick Terrell * zstd_compress_bound() - maximum compressed size in worst case scenario 31cf30f6a5SNick Terrell * @src_size: The size of the data to compress. 3273f3d1b4SNick Terrell * 3373f3d1b4SNick Terrell * Return: The maximum compressed size in the worst case scenario. 3473f3d1b4SNick Terrell */ 35cf30f6a5SNick Terrell size_t zstd_compress_bound(size_t src_size); 36cf30f6a5SNick Terrell 3773f3d1b4SNick Terrell /** 38cf30f6a5SNick Terrell * zstd_is_error() - tells if a size_t function result is an error code 3973f3d1b4SNick Terrell * @code: The function result to check for error. 4073f3d1b4SNick Terrell * 4173f3d1b4SNick Terrell * Return: Non-zero iff the code is an error. 4273f3d1b4SNick Terrell */ 43cf30f6a5SNick Terrell unsigned int zstd_is_error(size_t code); 4473f3d1b4SNick Terrell 4573f3d1b4SNick Terrell /** 46cf30f6a5SNick Terrell * enum zstd_error_code - zstd error codes 4773f3d1b4SNick Terrell */ 48cf30f6a5SNick Terrell typedef ZSTD_ErrorCode zstd_error_code; 4973f3d1b4SNick Terrell 5073f3d1b4SNick Terrell /** 51cf30f6a5SNick Terrell * zstd_get_error_code() - translates an error function result to an error code 52cf30f6a5SNick Terrell * @code: The function result for which zstd_is_error(code) is true. 53cf30f6a5SNick Terrell * 54cf30f6a5SNick Terrell * Return: A unique error code for this error. 55cf30f6a5SNick Terrell */ 56cf30f6a5SNick Terrell zstd_error_code zstd_get_error_code(size_t code); 57cf30f6a5SNick Terrell 58cf30f6a5SNick Terrell /** 59cf30f6a5SNick Terrell * zstd_get_error_name() - translates an error function result to a string 60cf30f6a5SNick Terrell * @code: The function result for which zstd_is_error(code) is true. 61cf30f6a5SNick Terrell * 62cf30f6a5SNick Terrell * Return: An error string corresponding to the error code. 63cf30f6a5SNick Terrell */ 64cf30f6a5SNick Terrell const char *zstd_get_error_name(size_t code); 65cf30f6a5SNick Terrell 66cf30f6a5SNick Terrell /** 67cf30f6a5SNick Terrell * zstd_min_clevel() - minimum allowed compression level 68cf30f6a5SNick Terrell * 69cf30f6a5SNick Terrell * Return: The minimum allowed compression level. 70cf30f6a5SNick Terrell */ 71cf30f6a5SNick Terrell int zstd_min_clevel(void); 72cf30f6a5SNick Terrell 73cf30f6a5SNick Terrell /** 74cf30f6a5SNick Terrell * zstd_max_clevel() - maximum allowed compression level 75cf30f6a5SNick Terrell * 76cf30f6a5SNick Terrell * Return: The maximum allowed compression level. 77cf30f6a5SNick Terrell */ 78cf30f6a5SNick Terrell int zstd_max_clevel(void); 79cf30f6a5SNick Terrell 804fc41879SSergey Senozhatsky /** 814fc41879SSergey Senozhatsky * zstd_default_clevel() - default compression level 824fc41879SSergey Senozhatsky * 834fc41879SSergey Senozhatsky * Return: Default compression level. 844fc41879SSergey Senozhatsky */ 854fc41879SSergey Senozhatsky int zstd_default_clevel(void); 864fc41879SSergey Senozhatsky 874fc41879SSergey Senozhatsky /** 884fc41879SSergey Senozhatsky * struct zstd_custom_mem - custom memory allocation 894fc41879SSergey Senozhatsky */ 904fc41879SSergey Senozhatsky typedef ZSTD_customMem zstd_custom_mem; 914fc41879SSergey Senozhatsky 924fc41879SSergey Senozhatsky /** 934fc41879SSergey Senozhatsky * struct zstd_dict_load_method - Dictionary load method. 944fc41879SSergey Senozhatsky * See zstd_lib.h. 954fc41879SSergey Senozhatsky */ 964fc41879SSergey Senozhatsky typedef ZSTD_dictLoadMethod_e zstd_dict_load_method; 974fc41879SSergey Senozhatsky 984fc41879SSergey Senozhatsky /** 994fc41879SSergey Senozhatsky * struct zstd_dict_content_type - Dictionary context type. 1004fc41879SSergey Senozhatsky * See zstd_lib.h. 1014fc41879SSergey Senozhatsky */ 1024fc41879SSergey Senozhatsky typedef ZSTD_dictContentType_e zstd_dict_content_type; 1034fc41879SSergey Senozhatsky 104cf30f6a5SNick Terrell /* ====== Parameter Selection ====== */ 105cf30f6a5SNick Terrell 106cf30f6a5SNick Terrell /** 107cf30f6a5SNick Terrell * enum zstd_strategy - zstd compression search strategy 108cf30f6a5SNick Terrell * 109cf30f6a5SNick Terrell * From faster to stronger. See zstd_lib.h. 110cf30f6a5SNick Terrell */ 111cf30f6a5SNick Terrell typedef ZSTD_strategy zstd_strategy; 112cf30f6a5SNick Terrell 113cf30f6a5SNick Terrell /** 114cf30f6a5SNick Terrell * struct zstd_compression_parameters - zstd compression parameters 11573f3d1b4SNick Terrell * @windowLog: Log of the largest match distance. Larger means more 11673f3d1b4SNick Terrell * compression, and more memory needed during decompression. 117cf30f6a5SNick Terrell * @chainLog: Fully searched segment. Larger means more compression, 118cf30f6a5SNick Terrell * slower, and more memory (useless for fast). 11973f3d1b4SNick Terrell * @hashLog: Dispatch table. Larger means more compression, 12073f3d1b4SNick Terrell * slower, and more memory. 12173f3d1b4SNick Terrell * @searchLog: Number of searches. Larger means more compression and slower. 12273f3d1b4SNick Terrell * @searchLength: Match length searched. Larger means faster decompression, 12373f3d1b4SNick Terrell * sometimes less compression. 12473f3d1b4SNick Terrell * @targetLength: Acceptable match size for optimal parser (only). Larger means 12573f3d1b4SNick Terrell * more compression, and slower. 12673f3d1b4SNick Terrell * @strategy: The zstd compression strategy. 12773f3d1b4SNick Terrell * 128cf30f6a5SNick Terrell * See zstd_lib.h. 12973f3d1b4SNick Terrell */ 130cf30f6a5SNick Terrell typedef ZSTD_compressionParameters zstd_compression_parameters; 13173f3d1b4SNick Terrell 13273f3d1b4SNick Terrell /** 133cf30f6a5SNick Terrell * struct zstd_frame_parameters - zstd frame parameters 134cf30f6a5SNick Terrell * @contentSizeFlag: Controls whether content size will be present in the 135cf30f6a5SNick Terrell * frame header (when known). 136cf30f6a5SNick Terrell * @checksumFlag: Controls whether a 32-bit checksum is generated at the 137cf30f6a5SNick Terrell * end of the frame for error detection. 138cf30f6a5SNick Terrell * @noDictIDFlag: Controls whether dictID will be saved into the frame 139cf30f6a5SNick Terrell * header when using dictionary compression. 140cf30f6a5SNick Terrell * 141cf30f6a5SNick Terrell * The default value is all fields set to 0. See zstd_lib.h. 142cf30f6a5SNick Terrell */ 143cf30f6a5SNick Terrell typedef ZSTD_frameParameters zstd_frame_parameters; 144cf30f6a5SNick Terrell 145cf30f6a5SNick Terrell /** 146cf30f6a5SNick Terrell * struct zstd_parameters - zstd parameters 14773f3d1b4SNick Terrell * @cParams: The compression parameters. 14873f3d1b4SNick Terrell * @fParams: The frame parameters. 14973f3d1b4SNick Terrell */ 150cf30f6a5SNick Terrell typedef ZSTD_parameters zstd_parameters; 15173f3d1b4SNick Terrell 15273f3d1b4SNick Terrell /** 153cf30f6a5SNick Terrell * zstd_get_params() - returns zstd_parameters for selected level 154cf30f6a5SNick Terrell * @level: The compression level 155cf30f6a5SNick Terrell * @estimated_src_size: The estimated source size to compress or 0 156cf30f6a5SNick Terrell * if unknown. 15773f3d1b4SNick Terrell * 158cf30f6a5SNick Terrell * Return: The selected zstd_parameters. 15973f3d1b4SNick Terrell */ 160cf30f6a5SNick Terrell zstd_parameters zstd_get_params(int level, 161cf30f6a5SNick Terrell unsigned long long estimated_src_size); 162cf30f6a5SNick Terrell 1634fc41879SSergey Senozhatsky /** 1644fc41879SSergey Senozhatsky * zstd_get_cparams() - returns zstd_compression_parameters for selected level 1654fc41879SSergey Senozhatsky * @level: The compression level 1664fc41879SSergey Senozhatsky * @estimated_src_size: The estimated source size to compress or 0 1674fc41879SSergey Senozhatsky * if unknown. 1684fc41879SSergey Senozhatsky * @dict_size: Dictionary size. 1694fc41879SSergey Senozhatsky * 1704fc41879SSergey Senozhatsky * Return: The selected zstd_compression_parameters. 1714fc41879SSergey Senozhatsky */ 1724fc41879SSergey Senozhatsky zstd_compression_parameters zstd_get_cparams(int level, 1734fc41879SSergey Senozhatsky unsigned long long estimated_src_size, size_t dict_size); 1744fc41879SSergey Senozhatsky 175cf30f6a5SNick Terrell typedef ZSTD_CCtx zstd_cctx; 176*65d1f550SNick Terrell typedef ZSTD_cParameter zstd_cparameter; 177*65d1f550SNick Terrell 178*65d1f550SNick Terrell /** 179*65d1f550SNick Terrell * zstd_cctx_set_param() - sets a compression parameter 180*65d1f550SNick Terrell * @cctx: The context. Must have been initialized with zstd_init_cctx(). 181*65d1f550SNick Terrell * @param: The parameter to set. 182*65d1f550SNick Terrell * @value: The value to set the parameter to. 183*65d1f550SNick Terrell * 184*65d1f550SNick Terrell * Return: Zero or an error, which can be checked using zstd_is_error(). 185*65d1f550SNick Terrell */ 186*65d1f550SNick Terrell size_t zstd_cctx_set_param(zstd_cctx *cctx, zstd_cparameter param, int value); 187*65d1f550SNick Terrell 188*65d1f550SNick Terrell /* ====== Single-pass Compression ====== */ 18973f3d1b4SNick Terrell 19073f3d1b4SNick Terrell /** 191cf30f6a5SNick Terrell * zstd_cctx_workspace_bound() - max memory needed to initialize a zstd_cctx 192cf30f6a5SNick Terrell * @parameters: The compression parameters to be used. 19373f3d1b4SNick Terrell * 19473f3d1b4SNick Terrell * If multiple compression parameters might be used, the caller must call 195cf30f6a5SNick Terrell * zstd_cctx_workspace_bound() for each set of parameters and use the maximum 19673f3d1b4SNick Terrell * size. 19773f3d1b4SNick Terrell * 19873f3d1b4SNick Terrell * Return: A lower bound on the size of the workspace that is passed to 199cf30f6a5SNick Terrell * zstd_init_cctx(). 20073f3d1b4SNick Terrell */ 201cf30f6a5SNick Terrell size_t zstd_cctx_workspace_bound(const zstd_compression_parameters *parameters); 20273f3d1b4SNick Terrell 20373f3d1b4SNick Terrell /** 204*65d1f550SNick Terrell * zstd_cctx_workspace_bound_with_ext_seq_prod() - max memory needed to 205*65d1f550SNick Terrell * initialize a zstd_cctx when using the block-level external sequence 206*65d1f550SNick Terrell * producer API. 207*65d1f550SNick Terrell * @parameters: The compression parameters to be used. 208*65d1f550SNick Terrell * 209*65d1f550SNick Terrell * If multiple compression parameters might be used, the caller must call 210*65d1f550SNick Terrell * this function for each set of parameters and use the maximum size. 211*65d1f550SNick Terrell * 212*65d1f550SNick Terrell * Return: A lower bound on the size of the workspace that is passed to 213*65d1f550SNick Terrell * zstd_init_cctx(). 214*65d1f550SNick Terrell */ 215*65d1f550SNick Terrell size_t zstd_cctx_workspace_bound_with_ext_seq_prod(const zstd_compression_parameters *parameters); 216*65d1f550SNick Terrell 217*65d1f550SNick Terrell /** 218cf30f6a5SNick Terrell * zstd_init_cctx() - initialize a zstd compression context 21973f3d1b4SNick Terrell * @workspace: The workspace to emplace the context into. It must outlive 22073f3d1b4SNick Terrell * the returned context. 221cf30f6a5SNick Terrell * @workspace_size: The size of workspace. Use zstd_cctx_workspace_bound() to 22273f3d1b4SNick Terrell * determine how large the workspace must be. 22373f3d1b4SNick Terrell * 224cf30f6a5SNick Terrell * Return: A zstd compression context or NULL on error. 22573f3d1b4SNick Terrell */ 226cf30f6a5SNick Terrell zstd_cctx *zstd_init_cctx(void *workspace, size_t workspace_size); 22773f3d1b4SNick Terrell 22873f3d1b4SNick Terrell /** 229cf30f6a5SNick Terrell * zstd_compress_cctx() - compress src into dst with the initialized parameters 230cf30f6a5SNick Terrell * @cctx: The context. Must have been initialized with zstd_init_cctx(). 23173f3d1b4SNick Terrell * @dst: The buffer to compress src into. 232cf30f6a5SNick Terrell * @dst_capacity: The size of the destination buffer. May be any size, but 23373f3d1b4SNick Terrell * ZSTD_compressBound(srcSize) is guaranteed to be large enough. 23473f3d1b4SNick Terrell * @src: The data to compress. 235cf30f6a5SNick Terrell * @src_size: The size of the data to compress. 236cf30f6a5SNick Terrell * @parameters: The compression parameters to be used. 23773f3d1b4SNick Terrell * 23873f3d1b4SNick Terrell * Return: The compressed size or an error, which can be checked using 239cf30f6a5SNick Terrell * zstd_is_error(). 24073f3d1b4SNick Terrell */ 241cf30f6a5SNick Terrell size_t zstd_compress_cctx(zstd_cctx *cctx, void *dst, size_t dst_capacity, 242cf30f6a5SNick Terrell const void *src, size_t src_size, const zstd_parameters *parameters); 243cf30f6a5SNick Terrell 2444fc41879SSergey Senozhatsky /** 2454fc41879SSergey Senozhatsky * zstd_create_cctx_advanced() - Create compression context 2464fc41879SSergey Senozhatsky * @custom_mem: Custom allocator. 2474fc41879SSergey Senozhatsky * 2484fc41879SSergey Senozhatsky * Return: NULL on error, pointer to compression context otherwise. 2494fc41879SSergey Senozhatsky */ 2504fc41879SSergey Senozhatsky zstd_cctx *zstd_create_cctx_advanced(zstd_custom_mem custom_mem); 2514fc41879SSergey Senozhatsky 2524fc41879SSergey Senozhatsky /** 2534fc41879SSergey Senozhatsky * zstd_free_cctx() - Free compression context 2544fc41879SSergey Senozhatsky * @cdict: Pointer to compression context. 2554fc41879SSergey Senozhatsky * 2564fc41879SSergey Senozhatsky * Return: Always 0. 2574fc41879SSergey Senozhatsky */ 2584fc41879SSergey Senozhatsky size_t zstd_free_cctx(zstd_cctx* cctx); 2594fc41879SSergey Senozhatsky 2604fc41879SSergey Senozhatsky /** 2614fc41879SSergey Senozhatsky * struct zstd_cdict - Compression dictionary. 2624fc41879SSergey Senozhatsky * See zstd_lib.h. 2634fc41879SSergey Senozhatsky */ 2644fc41879SSergey Senozhatsky typedef ZSTD_CDict zstd_cdict; 2654fc41879SSergey Senozhatsky 2664fc41879SSergey Senozhatsky /** 2674fc41879SSergey Senozhatsky * zstd_create_cdict_byreference() - Create compression dictionary 2684fc41879SSergey Senozhatsky * @dict: Pointer to dictionary buffer. 2694fc41879SSergey Senozhatsky * @dict_size: Size of the dictionary buffer. 2704fc41879SSergey Senozhatsky * @dict_load_method: Dictionary load method. 2714fc41879SSergey Senozhatsky * @dict_content_type: Dictionary content type. 2724fc41879SSergey Senozhatsky * @custom_mem: Memory allocator. 2734fc41879SSergey Senozhatsky * 2744fc41879SSergey Senozhatsky * Note, this uses @dict by reference (ZSTD_dlm_byRef), so it should be 2754fc41879SSergey Senozhatsky * free before zstd_cdict is destroyed. 2764fc41879SSergey Senozhatsky * 2774fc41879SSergey Senozhatsky * Return: NULL on error, pointer to compression dictionary 2784fc41879SSergey Senozhatsky * otherwise. 2794fc41879SSergey Senozhatsky */ 2804fc41879SSergey Senozhatsky zstd_cdict *zstd_create_cdict_byreference(const void *dict, size_t dict_size, 2814fc41879SSergey Senozhatsky zstd_compression_parameters cparams, 2824fc41879SSergey Senozhatsky zstd_custom_mem custom_mem); 2834fc41879SSergey Senozhatsky 2844fc41879SSergey Senozhatsky /** 2854fc41879SSergey Senozhatsky * zstd_free_cdict() - Free compression dictionary 2864fc41879SSergey Senozhatsky * @cdict: Pointer to compression dictionary. 2874fc41879SSergey Senozhatsky * 2884fc41879SSergey Senozhatsky * Return: Always 0. 2894fc41879SSergey Senozhatsky */ 2904fc41879SSergey Senozhatsky size_t zstd_free_cdict(zstd_cdict* cdict); 2914fc41879SSergey Senozhatsky 2924fc41879SSergey Senozhatsky /** 2934fc41879SSergey Senozhatsky * zstd_compress_using_cdict() - compress src into dst using a dictionary 2944fc41879SSergey Senozhatsky * @cctx: The context. Must have been initialized with zstd_init_cctx(). 2954fc41879SSergey Senozhatsky * @dst: The buffer to compress src into. 2964fc41879SSergey Senozhatsky * @dst_capacity: The size of the destination buffer. May be any size, but 2974fc41879SSergey Senozhatsky * ZSTD_compressBound(srcSize) is guaranteed to be large enough. 2984fc41879SSergey Senozhatsky * @src: The data to compress. 2994fc41879SSergey Senozhatsky * @src_size: The size of the data to compress. 3004fc41879SSergey Senozhatsky * @cdict: The dictionary to be used. 3014fc41879SSergey Senozhatsky * 3024fc41879SSergey Senozhatsky * Return: The compressed size or an error, which can be checked using 3034fc41879SSergey Senozhatsky * zstd_is_error(). 3044fc41879SSergey Senozhatsky */ 3054fc41879SSergey Senozhatsky size_t zstd_compress_using_cdict(zstd_cctx *cctx, void *dst, 3064fc41879SSergey Senozhatsky size_t dst_capacity, const void *src, size_t src_size, 3074fc41879SSergey Senozhatsky const zstd_cdict *cdict); 3084fc41879SSergey Senozhatsky 309cf30f6a5SNick Terrell /* ====== Single-pass Decompression ====== */ 310cf30f6a5SNick Terrell 311cf30f6a5SNick Terrell typedef ZSTD_DCtx zstd_dctx; 31273f3d1b4SNick Terrell 31373f3d1b4SNick Terrell /** 314cf30f6a5SNick Terrell * zstd_dctx_workspace_bound() - max memory needed to initialize a zstd_dctx 31573f3d1b4SNick Terrell * 31673f3d1b4SNick Terrell * Return: A lower bound on the size of the workspace that is passed to 317cf30f6a5SNick Terrell * zstd_init_dctx(). 31873f3d1b4SNick Terrell */ 319cf30f6a5SNick Terrell size_t zstd_dctx_workspace_bound(void); 32073f3d1b4SNick Terrell 32173f3d1b4SNick Terrell /** 322cf30f6a5SNick Terrell * zstd_init_dctx() - initialize a zstd decompression context 32373f3d1b4SNick Terrell * @workspace: The workspace to emplace the context into. It must outlive 32473f3d1b4SNick Terrell * the returned context. 325cf30f6a5SNick Terrell * @workspace_size: The size of workspace. Use zstd_dctx_workspace_bound() to 32673f3d1b4SNick Terrell * determine how large the workspace must be. 32773f3d1b4SNick Terrell * 328cf30f6a5SNick Terrell * Return: A zstd decompression context or NULL on error. 32973f3d1b4SNick Terrell */ 330cf30f6a5SNick Terrell zstd_dctx *zstd_init_dctx(void *workspace, size_t workspace_size); 33173f3d1b4SNick Terrell 33273f3d1b4SNick Terrell /** 333cf30f6a5SNick Terrell * zstd_decompress_dctx() - decompress zstd compressed src into dst 334cf30f6a5SNick Terrell * @dctx: The decompression context. 33573f3d1b4SNick Terrell * @dst: The buffer to decompress src into. 336cf30f6a5SNick Terrell * @dst_capacity: The size of the destination buffer. Must be at least as large 33773f3d1b4SNick Terrell * as the decompressed size. If the caller cannot upper bound the 33873f3d1b4SNick Terrell * decompressed size, then it's better to use the streaming API. 33973f3d1b4SNick Terrell * @src: The zstd compressed data to decompress. Multiple concatenated 34073f3d1b4SNick Terrell * frames and skippable frames are allowed. 341cf30f6a5SNick Terrell * @src_size: The exact size of the data to decompress. 34273f3d1b4SNick Terrell * 34373f3d1b4SNick Terrell * Return: The decompressed size or an error, which can be checked using 344cf30f6a5SNick Terrell * zstd_is_error(). 34573f3d1b4SNick Terrell */ 346cf30f6a5SNick Terrell size_t zstd_decompress_dctx(zstd_dctx *dctx, void *dst, size_t dst_capacity, 347cf30f6a5SNick Terrell const void *src, size_t src_size); 34873f3d1b4SNick Terrell 3494fc41879SSergey Senozhatsky /** 3504fc41879SSergey Senozhatsky * struct zstd_ddict - Decompression dictionary. 3514fc41879SSergey Senozhatsky * See zstd_lib.h. 3524fc41879SSergey Senozhatsky */ 3534fc41879SSergey Senozhatsky typedef ZSTD_DDict zstd_ddict; 3544fc41879SSergey Senozhatsky 3554fc41879SSergey Senozhatsky /** 3564fc41879SSergey Senozhatsky * zstd_create_ddict_byreference() - Create decompression dictionary 3574fc41879SSergey Senozhatsky * @dict: Pointer to dictionary buffer. 3584fc41879SSergey Senozhatsky * @dict_size: Size of the dictionary buffer. 3594fc41879SSergey Senozhatsky * @dict_load_method: Dictionary load method. 3604fc41879SSergey Senozhatsky * @dict_content_type: Dictionary content type. 3614fc41879SSergey Senozhatsky * @custom_mem: Memory allocator. 3624fc41879SSergey Senozhatsky * 3634fc41879SSergey Senozhatsky * Note, this uses @dict by reference (ZSTD_dlm_byRef), so it should be 3644fc41879SSergey Senozhatsky * free before zstd_ddict is destroyed. 3654fc41879SSergey Senozhatsky * 3664fc41879SSergey Senozhatsky * Return: NULL on error, pointer to decompression dictionary 3674fc41879SSergey Senozhatsky * otherwise. 3684fc41879SSergey Senozhatsky */ 3694fc41879SSergey Senozhatsky zstd_ddict *zstd_create_ddict_byreference(const void *dict, size_t dict_size, 3704fc41879SSergey Senozhatsky zstd_custom_mem custom_mem); 3714fc41879SSergey Senozhatsky /** 3724fc41879SSergey Senozhatsky * zstd_free_ddict() - Free decompression dictionary 3734fc41879SSergey Senozhatsky * @dict: Pointer to the dictionary. 3744fc41879SSergey Senozhatsky * 3754fc41879SSergey Senozhatsky * Return: Always 0. 3764fc41879SSergey Senozhatsky */ 3774fc41879SSergey Senozhatsky size_t zstd_free_ddict(zstd_ddict *ddict); 3784fc41879SSergey Senozhatsky 3794fc41879SSergey Senozhatsky /** 3804fc41879SSergey Senozhatsky * zstd_create_dctx_advanced() - Create decompression context 3814fc41879SSergey Senozhatsky * @custom_mem: Custom allocator. 3824fc41879SSergey Senozhatsky * 3834fc41879SSergey Senozhatsky * Return: NULL on error, pointer to decompression context otherwise. 3844fc41879SSergey Senozhatsky */ 3854fc41879SSergey Senozhatsky zstd_dctx *zstd_create_dctx_advanced(zstd_custom_mem custom_mem); 3864fc41879SSergey Senozhatsky 3874fc41879SSergey Senozhatsky /** 3884fc41879SSergey Senozhatsky * zstd_free_dctx() -- Free decompression context 3894fc41879SSergey Senozhatsky * @dctx: Pointer to decompression context. 3904fc41879SSergey Senozhatsky * Return: Always 0. 3914fc41879SSergey Senozhatsky */ 3924fc41879SSergey Senozhatsky size_t zstd_free_dctx(zstd_dctx *dctx); 3934fc41879SSergey Senozhatsky 3944fc41879SSergey Senozhatsky /** 3954fc41879SSergey Senozhatsky * zstd_decompress_using_ddict() - decompress src into dst using a dictionary 3964fc41879SSergey Senozhatsky * @dctx: The decompression context. 3974fc41879SSergey Senozhatsky * @dst: The buffer to decompress src into. 3984fc41879SSergey Senozhatsky * @dst_capacity: The size of the destination buffer. Must be at least as large 3994fc41879SSergey Senozhatsky * as the decompressed size. If the caller cannot upper bound the 4004fc41879SSergey Senozhatsky * decompressed size, then it's better to use the streaming API. 4014fc41879SSergey Senozhatsky * @src: The zstd compressed data to decompress. Multiple concatenated 4024fc41879SSergey Senozhatsky * frames and skippable frames are allowed. 4034fc41879SSergey Senozhatsky * @src_size: The exact size of the data to decompress. 4044fc41879SSergey Senozhatsky * @ddict: The dictionary to be used. 4054fc41879SSergey Senozhatsky * 4064fc41879SSergey Senozhatsky * Return: The decompressed size or an error, which can be checked using 4074fc41879SSergey Senozhatsky * zstd_is_error(). 4084fc41879SSergey Senozhatsky */ 4094fc41879SSergey Senozhatsky size_t zstd_decompress_using_ddict(zstd_dctx *dctx, 4104fc41879SSergey Senozhatsky void *dst, size_t dst_capacity, const void *src, size_t src_size, 4114fc41879SSergey Senozhatsky const zstd_ddict *ddict); 4124fc41879SSergey Senozhatsky 4134fc41879SSergey Senozhatsky 414cf30f6a5SNick Terrell /* ====== Streaming Buffers ====== */ 41573f3d1b4SNick Terrell 41673f3d1b4SNick Terrell /** 417cf30f6a5SNick Terrell * struct zstd_in_buffer - input buffer for streaming 41873f3d1b4SNick Terrell * @src: Start of the input buffer. 41973f3d1b4SNick Terrell * @size: Size of the input buffer. 42073f3d1b4SNick Terrell * @pos: Position where reading stopped. Will be updated. 42173f3d1b4SNick Terrell * Necessarily 0 <= pos <= size. 422cf30f6a5SNick Terrell * 423cf30f6a5SNick Terrell * See zstd_lib.h. 42473f3d1b4SNick Terrell */ 425cf30f6a5SNick Terrell typedef ZSTD_inBuffer zstd_in_buffer; 42673f3d1b4SNick Terrell 42773f3d1b4SNick Terrell /** 428cf30f6a5SNick Terrell * struct zstd_out_buffer - output buffer for streaming 42973f3d1b4SNick Terrell * @dst: Start of the output buffer. 43073f3d1b4SNick Terrell * @size: Size of the output buffer. 43173f3d1b4SNick Terrell * @pos: Position where writing stopped. Will be updated. 43273f3d1b4SNick Terrell * Necessarily 0 <= pos <= size. 433cf30f6a5SNick Terrell * 434cf30f6a5SNick Terrell * See zstd_lib.h. 43573f3d1b4SNick Terrell */ 436cf30f6a5SNick Terrell typedef ZSTD_outBuffer zstd_out_buffer; 43773f3d1b4SNick Terrell 438cf30f6a5SNick Terrell /* ====== Streaming Compression ====== */ 43973f3d1b4SNick Terrell 440cf30f6a5SNick Terrell typedef ZSTD_CStream zstd_cstream; 44173f3d1b4SNick Terrell 44273f3d1b4SNick Terrell /** 443cf30f6a5SNick Terrell * zstd_cstream_workspace_bound() - memory needed to initialize a zstd_cstream 444cf30f6a5SNick Terrell * @cparams: The compression parameters to be used for compression. 44573f3d1b4SNick Terrell * 44673f3d1b4SNick Terrell * Return: A lower bound on the size of the workspace that is passed to 447cf30f6a5SNick Terrell * zstd_init_cstream(). 44873f3d1b4SNick Terrell */ 449cf30f6a5SNick Terrell size_t zstd_cstream_workspace_bound(const zstd_compression_parameters *cparams); 45073f3d1b4SNick Terrell 45173f3d1b4SNick Terrell /** 452*65d1f550SNick Terrell * zstd_cstream_workspace_bound_with_ext_seq_prod() - memory needed to initialize 453*65d1f550SNick Terrell * a zstd_cstream when using the block-level external sequence producer API. 454*65d1f550SNick Terrell * @cparams: The compression parameters to be used for compression. 455*65d1f550SNick Terrell * 456*65d1f550SNick Terrell * Return: A lower bound on the size of the workspace that is passed to 457*65d1f550SNick Terrell * zstd_init_cstream(). 458*65d1f550SNick Terrell */ 459*65d1f550SNick Terrell size_t zstd_cstream_workspace_bound_with_ext_seq_prod(const zstd_compression_parameters *cparams); 460*65d1f550SNick Terrell 461*65d1f550SNick Terrell /** 462cf30f6a5SNick Terrell * zstd_init_cstream() - initialize a zstd streaming compression context 463cf30f6a5SNick Terrell * @parameters The zstd parameters to use for compression. 464cf30f6a5SNick Terrell * @pledged_src_size: If params.fParams.contentSizeFlag == 1 then the caller 465cf30f6a5SNick Terrell * must pass the source size (zero means empty source). 466cf30f6a5SNick Terrell * Otherwise, the caller may optionally pass the source 467cf30f6a5SNick Terrell * size, or zero if unknown. 46873f3d1b4SNick Terrell * @workspace: The workspace to emplace the context into. It must outlive 46973f3d1b4SNick Terrell * the returned context. 470cf30f6a5SNick Terrell * @workspace_size: The size of workspace. 471cf30f6a5SNick Terrell * Use zstd_cstream_workspace_bound(params->cparams) to 472cf30f6a5SNick Terrell * determine how large the workspace must be. 47373f3d1b4SNick Terrell * 474cf30f6a5SNick Terrell * Return: The zstd streaming compression context or NULL on error. 47573f3d1b4SNick Terrell */ 476cf30f6a5SNick Terrell zstd_cstream *zstd_init_cstream(const zstd_parameters *parameters, 477cf30f6a5SNick Terrell unsigned long long pledged_src_size, void *workspace, size_t workspace_size); 47873f3d1b4SNick Terrell 47973f3d1b4SNick Terrell /** 480cf30f6a5SNick Terrell * zstd_reset_cstream() - reset the context using parameters from creation 481cf30f6a5SNick Terrell * @cstream: The zstd streaming compression context to reset. 482cf30f6a5SNick Terrell * @pledged_src_size: Optionally the source size, or zero if unknown. 48373f3d1b4SNick Terrell * 48473f3d1b4SNick Terrell * Resets the context using the parameters from creation. Skips dictionary 485cf30f6a5SNick Terrell * loading, since it can be reused. If `pledged_src_size` is non-zero the frame 48673f3d1b4SNick Terrell * content size is always written into the frame header. 48773f3d1b4SNick Terrell * 488cf30f6a5SNick Terrell * Return: Zero or an error, which can be checked using 489cf30f6a5SNick Terrell * zstd_is_error(). 49073f3d1b4SNick Terrell */ 491cf30f6a5SNick Terrell size_t zstd_reset_cstream(zstd_cstream *cstream, 492cf30f6a5SNick Terrell unsigned long long pledged_src_size); 493cf30f6a5SNick Terrell 49473f3d1b4SNick Terrell /** 495cf30f6a5SNick Terrell * zstd_compress_stream() - streaming compress some of input into output 496cf30f6a5SNick Terrell * @cstream: The zstd streaming compression context. 49773f3d1b4SNick Terrell * @output: Destination buffer. `output->pos` is updated to indicate how much 49873f3d1b4SNick Terrell * compressed data was written. 499cf30f6a5SNick Terrell * @input: Source buffer. `input->pos` is updated to indicate how much data 500cf30f6a5SNick Terrell * was read. Note that it may not consume the entire input, in which 501cf30f6a5SNick Terrell * case `input->pos < input->size`, and it's up to the caller to 502cf30f6a5SNick Terrell * present remaining data again. 50373f3d1b4SNick Terrell * 50473f3d1b4SNick Terrell * The `input` and `output` buffers may be any size. Guaranteed to make some 50573f3d1b4SNick Terrell * forward progress if `input` and `output` are not empty. 50673f3d1b4SNick Terrell * 50773f3d1b4SNick Terrell * Return: A hint for the number of bytes to use as the input for the next 50873f3d1b4SNick Terrell * function call or an error, which can be checked using 509cf30f6a5SNick Terrell * zstd_is_error(). 51073f3d1b4SNick Terrell */ 511cf30f6a5SNick Terrell size_t zstd_compress_stream(zstd_cstream *cstream, zstd_out_buffer *output, 512cf30f6a5SNick Terrell zstd_in_buffer *input); 513cf30f6a5SNick Terrell 51473f3d1b4SNick Terrell /** 515cf30f6a5SNick Terrell * zstd_flush_stream() - flush internal buffers into output 516cf30f6a5SNick Terrell * @cstream: The zstd streaming compression context. 51773f3d1b4SNick Terrell * @output: Destination buffer. `output->pos` is updated to indicate how much 51873f3d1b4SNick Terrell * compressed data was written. 51973f3d1b4SNick Terrell * 520cf30f6a5SNick Terrell * zstd_flush_stream() must be called until it returns 0, meaning all the data 521cf30f6a5SNick Terrell * has been flushed. Since zstd_flush_stream() causes a block to be ended, 52273f3d1b4SNick Terrell * calling it too often will degrade the compression ratio. 52373f3d1b4SNick Terrell * 52473f3d1b4SNick Terrell * Return: The number of bytes still present within internal buffers or an 525cf30f6a5SNick Terrell * error, which can be checked using zstd_is_error(). 52673f3d1b4SNick Terrell */ 527cf30f6a5SNick Terrell size_t zstd_flush_stream(zstd_cstream *cstream, zstd_out_buffer *output); 528cf30f6a5SNick Terrell 52973f3d1b4SNick Terrell /** 530cf30f6a5SNick Terrell * zstd_end_stream() - flush internal buffers into output and end the frame 531cf30f6a5SNick Terrell * @cstream: The zstd streaming compression context. 53273f3d1b4SNick Terrell * @output: Destination buffer. `output->pos` is updated to indicate how much 53373f3d1b4SNick Terrell * compressed data was written. 53473f3d1b4SNick Terrell * 535cf30f6a5SNick Terrell * zstd_end_stream() must be called until it returns 0, meaning all the data has 53673f3d1b4SNick Terrell * been flushed and the frame epilogue has been written. 53773f3d1b4SNick Terrell * 53873f3d1b4SNick Terrell * Return: The number of bytes still present within internal buffers or an 539cf30f6a5SNick Terrell * error, which can be checked using zstd_is_error(). 54073f3d1b4SNick Terrell */ 541cf30f6a5SNick Terrell size_t zstd_end_stream(zstd_cstream *cstream, zstd_out_buffer *output); 542cf30f6a5SNick Terrell 543cf30f6a5SNick Terrell /* ====== Streaming Decompression ====== */ 544cf30f6a5SNick Terrell 545cf30f6a5SNick Terrell typedef ZSTD_DStream zstd_dstream; 54673f3d1b4SNick Terrell 54773f3d1b4SNick Terrell /** 548cf30f6a5SNick Terrell * zstd_dstream_workspace_bound() - memory needed to initialize a zstd_dstream 549cf30f6a5SNick Terrell * @max_window_size: The maximum window size allowed for compressed frames. 55073f3d1b4SNick Terrell * 551cf30f6a5SNick Terrell * Return: A lower bound on the size of the workspace that is passed 552cf30f6a5SNick Terrell * to zstd_init_dstream(). 55373f3d1b4SNick Terrell */ 554cf30f6a5SNick Terrell size_t zstd_dstream_workspace_bound(size_t max_window_size); 55573f3d1b4SNick Terrell 55673f3d1b4SNick Terrell /** 557cf30f6a5SNick Terrell * zstd_init_dstream() - initialize a zstd streaming decompression context 558cf30f6a5SNick Terrell * @max_window_size: The maximum window size allowed for compressed frames. 55973f3d1b4SNick Terrell * @workspace: The workspace to emplace the context into. It must outlive 56073f3d1b4SNick Terrell * the returned context. 56173f3d1b4SNick Terrell * @workspaceSize: The size of workspace. 562cf30f6a5SNick Terrell * Use zstd_dstream_workspace_bound(max_window_size) to 563cf30f6a5SNick Terrell * determine how large the workspace must be. 56473f3d1b4SNick Terrell * 56573f3d1b4SNick Terrell * Return: The zstd streaming decompression context. 56673f3d1b4SNick Terrell */ 567cf30f6a5SNick Terrell zstd_dstream *zstd_init_dstream(size_t max_window_size, void *workspace, 568cf30f6a5SNick Terrell size_t workspace_size); 56973f3d1b4SNick Terrell 57073f3d1b4SNick Terrell /** 571cf30f6a5SNick Terrell * zstd_reset_dstream() - reset the context using parameters from creation 572cf30f6a5SNick Terrell * @dstream: The zstd streaming decompression context to reset. 57373f3d1b4SNick Terrell * 57473f3d1b4SNick Terrell * Resets the context using the parameters from creation. Skips dictionary 57573f3d1b4SNick Terrell * loading, since it can be reused. 57673f3d1b4SNick Terrell * 577cf30f6a5SNick Terrell * Return: Zero or an error, which can be checked using zstd_is_error(). 57873f3d1b4SNick Terrell */ 579cf30f6a5SNick Terrell size_t zstd_reset_dstream(zstd_dstream *dstream); 580cf30f6a5SNick Terrell 58173f3d1b4SNick Terrell /** 582cf30f6a5SNick Terrell * zstd_decompress_stream() - streaming decompress some of input into output 583cf30f6a5SNick Terrell * @dstream: The zstd streaming decompression context. 58473f3d1b4SNick Terrell * @output: Destination buffer. `output.pos` is updated to indicate how much 58573f3d1b4SNick Terrell * decompressed data was written. 58673f3d1b4SNick Terrell * @input: Source buffer. `input.pos` is updated to indicate how much data was 58773f3d1b4SNick Terrell * read. Note that it may not consume the entire input, in which case 58873f3d1b4SNick Terrell * `input.pos < input.size`, and it's up to the caller to present 58973f3d1b4SNick Terrell * remaining data again. 59073f3d1b4SNick Terrell * 59173f3d1b4SNick Terrell * The `input` and `output` buffers may be any size. Guaranteed to make some 59273f3d1b4SNick Terrell * forward progress if `input` and `output` are not empty. 593cf30f6a5SNick Terrell * zstd_decompress_stream() will not consume the last byte of the frame until 59473f3d1b4SNick Terrell * the entire frame is flushed. 59573f3d1b4SNick Terrell * 59673f3d1b4SNick Terrell * Return: Returns 0 iff a frame is completely decoded and fully flushed. 597cf30f6a5SNick Terrell * Otherwise returns a hint for the number of bytes to use as the 598cf30f6a5SNick Terrell * input for the next function call or an error, which can be checked 599cf30f6a5SNick Terrell * using zstd_is_error(). The size hint will never load more than the 600cf30f6a5SNick Terrell * frame. 60173f3d1b4SNick Terrell */ 602cf30f6a5SNick Terrell size_t zstd_decompress_stream(zstd_dstream *dstream, zstd_out_buffer *output, 603cf30f6a5SNick Terrell zstd_in_buffer *input); 604cf30f6a5SNick Terrell 605cf30f6a5SNick Terrell /* ====== Frame Inspection Functions ====== */ 60673f3d1b4SNick Terrell 60773f3d1b4SNick Terrell /** 608cf30f6a5SNick Terrell * zstd_find_frame_compressed_size() - returns the size of a compressed frame 609cf30f6a5SNick Terrell * @src: Source buffer. It should point to the start of a zstd encoded 610cf30f6a5SNick Terrell * frame or a skippable frame. 611cf30f6a5SNick Terrell * @src_size: The size of the source buffer. It must be at least as large as the 61273f3d1b4SNick Terrell * size of the frame. 61373f3d1b4SNick Terrell * 61473f3d1b4SNick Terrell * Return: The compressed size of the frame pointed to by `src` or an error, 615cf30f6a5SNick Terrell * which can be check with zstd_is_error(). 61673f3d1b4SNick Terrell * Suitable to pass to ZSTD_decompress() or similar functions. 61773f3d1b4SNick Terrell */ 618cf30f6a5SNick Terrell size_t zstd_find_frame_compressed_size(const void *src, size_t src_size); 61973f3d1b4SNick Terrell 62073f3d1b4SNick Terrell /** 621*65d1f550SNick Terrell * zstd_register_sequence_producer() - exposes the zstd library function 622*65d1f550SNick Terrell * ZSTD_registerSequenceProducer(). This is used for the block-level external 623*65d1f550SNick Terrell * sequence producer API. See upstream zstd.h for detailed documentation. 624*65d1f550SNick Terrell */ 625*65d1f550SNick Terrell typedef ZSTD_sequenceProducer_F zstd_sequence_producer_f; 626*65d1f550SNick Terrell void zstd_register_sequence_producer( 627*65d1f550SNick Terrell zstd_cctx *cctx, 628*65d1f550SNick Terrell void* sequence_producer_state, 629*65d1f550SNick Terrell zstd_sequence_producer_f sequence_producer 630*65d1f550SNick Terrell ); 631*65d1f550SNick Terrell 632*65d1f550SNick Terrell /** 633cf30f6a5SNick Terrell * struct zstd_frame_params - zstd frame parameters stored in the frame header 634e0c1b49fSNick Terrell * @frameContentSize: The frame content size, or ZSTD_CONTENTSIZE_UNKNOWN if not 635e0c1b49fSNick Terrell * present. 63673f3d1b4SNick Terrell * @windowSize: The window size, or 0 if the frame is a skippable frame. 637e0c1b49fSNick Terrell * @blockSizeMax: The maximum block size. 638e0c1b49fSNick Terrell * @frameType: The frame type (zstd or skippable) 639e0c1b49fSNick Terrell * @headerSize: The size of the frame header. 64073f3d1b4SNick Terrell * @dictID: The dictionary id, or 0 if not present. 64173f3d1b4SNick Terrell * @checksumFlag: Whether a checksum was used. 642e0c1b49fSNick Terrell * 643e0c1b49fSNick Terrell * See zstd_lib.h. 64473f3d1b4SNick Terrell */ 645*65d1f550SNick Terrell typedef ZSTD_FrameHeader zstd_frame_header; 64673f3d1b4SNick Terrell 64773f3d1b4SNick Terrell /** 648cf30f6a5SNick Terrell * zstd_get_frame_header() - extracts parameters from a zstd or skippable frame 649cf30f6a5SNick Terrell * @params: On success the frame parameters are written here. 65073f3d1b4SNick Terrell * @src: The source buffer. It must point to a zstd or skippable frame. 651cf30f6a5SNick Terrell * @src_size: The size of the source buffer. 65273f3d1b4SNick Terrell * 65373f3d1b4SNick Terrell * Return: 0 on success. If more data is required it returns how many bytes 65473f3d1b4SNick Terrell * must be provided to make forward progress. Otherwise it returns 655cf30f6a5SNick Terrell * an error, which can be checked using zstd_is_error(). 65673f3d1b4SNick Terrell */ 657cf30f6a5SNick Terrell size_t zstd_get_frame_header(zstd_frame_header *params, const void *src, 658cf30f6a5SNick Terrell size_t src_size); 65973f3d1b4SNick Terrell 660*65d1f550SNick Terrell /** 661*65d1f550SNick Terrell * struct zstd_sequence - a sequence of literals or a match 662*65d1f550SNick Terrell * 663*65d1f550SNick Terrell * @offset: The offset of the match 664*65d1f550SNick Terrell * @litLength: The literal length of the sequence 665*65d1f550SNick Terrell * @matchLength: The match length of the sequence 666*65d1f550SNick Terrell * @rep: Represents which repeat offset is used 667*65d1f550SNick Terrell */ 668*65d1f550SNick Terrell typedef ZSTD_Sequence zstd_sequence; 669*65d1f550SNick Terrell 670*65d1f550SNick Terrell /** 671*65d1f550SNick Terrell * zstd_compress_sequences_and_literals() - compress an array of zstd_sequence and literals 672*65d1f550SNick Terrell * 673*65d1f550SNick Terrell * @cctx: The zstd compression context. 674*65d1f550SNick Terrell * @dst: The buffer to compress the data into. 675*65d1f550SNick Terrell * @dst_capacity: The size of the destination buffer. 676*65d1f550SNick Terrell * @in_seqs: The array of zstd_sequence to compress. 677*65d1f550SNick Terrell * @in_seqs_size: The number of sequences in in_seqs. 678*65d1f550SNick Terrell * @literals: The literals associated to the sequences to be compressed. 679*65d1f550SNick Terrell * @lit_size: The size of the literals in the literals buffer. 680*65d1f550SNick Terrell * @lit_capacity: The size of the literals buffer. 681*65d1f550SNick Terrell * @decompressed_size: The size of the input data 682*65d1f550SNick Terrell * 683*65d1f550SNick Terrell * Return: The compressed size or an error, which can be checked using 684*65d1f550SNick Terrell * zstd_is_error(). 685*65d1f550SNick Terrell */ 686*65d1f550SNick Terrell size_t zstd_compress_sequences_and_literals(zstd_cctx *cctx, void* dst, size_t dst_capacity, 687*65d1f550SNick Terrell const zstd_sequence *in_seqs, size_t in_seqs_size, 688*65d1f550SNick Terrell const void* literals, size_t lit_size, size_t lit_capacity, 689*65d1f550SNick Terrell size_t decompressed_size); 690*65d1f550SNick Terrell 691cf30f6a5SNick Terrell #endif /* LINUX_ZSTD_H */ 692