1*61145dc2SMartin Matuska // SPDX-License-Identifier: BSD-3-Clause OR GPL-2.0-only 2eda14cbcSMatt Macy /* 3c03c5b1cSMartin Matuska * Copyright (c) 2016-2020, Yann Collet, Facebook, Inc. 4c03c5b1cSMartin Matuska * All rights reserved. 5eda14cbcSMatt Macy * 6c03c5b1cSMartin Matuska * This source code is licensed under both the BSD-style license (found in the 7c03c5b1cSMartin Matuska * LICENSE file in the root directory of this source tree) and the GPLv2 (found 8c03c5b1cSMartin Matuska * in the COPYING file in the root directory of this source tree). 9c03c5b1cSMartin Matuska * You may select, at your option, one of the above-listed licenses. 10eda14cbcSMatt Macy */ 11eda14cbcSMatt Macy #if defined (__cplusplus) 12eda14cbcSMatt Macy extern "C" { 13eda14cbcSMatt Macy #endif 14eda14cbcSMatt Macy 15eda14cbcSMatt Macy #ifndef ZSTD_H_235446 16eda14cbcSMatt Macy #define ZSTD_H_235446 17eda14cbcSMatt Macy 18eda14cbcSMatt Macy /* ====== Dependency ======*/ 19eda14cbcSMatt Macy #include <limits.h> /* INT_MAX */ 20eda14cbcSMatt Macy #include <stddef.h> /* size_t */ 21eda14cbcSMatt Macy 22eda14cbcSMatt Macy 23eda14cbcSMatt Macy /* ===== ZSTDLIB_API : control library symbols visibility ===== */ 24eda14cbcSMatt Macy #ifndef ZSTDLIB_VISIBILITY 25eda14cbcSMatt Macy # if defined(__GNUC__) && (__GNUC__ >= 4) 26eda14cbcSMatt Macy # define ZSTDLIB_VISIBILITY __attribute__ ((visibility ("default"))) 27eda14cbcSMatt Macy # else 28eda14cbcSMatt Macy # define ZSTDLIB_VISIBILITY 29eda14cbcSMatt Macy # endif 30eda14cbcSMatt Macy #endif 31eda14cbcSMatt Macy #if defined(ZSTD_DLL_EXPORT) && (ZSTD_DLL_EXPORT==1) 32eda14cbcSMatt Macy # define ZSTDLIB_API __declspec(dllexport) ZSTDLIB_VISIBILITY 33eda14cbcSMatt Macy #elif defined(ZSTD_DLL_IMPORT) && (ZSTD_DLL_IMPORT==1) 34eda14cbcSMatt Macy # define ZSTDLIB_API __declspec(dllimport) ZSTDLIB_VISIBILITY /* It isn't required but allows to generate better code, saving a function pointer load from the IAT and an indirect jump.*/ 35eda14cbcSMatt Macy #else 36eda14cbcSMatt Macy # define ZSTDLIB_API ZSTDLIB_VISIBILITY 37eda14cbcSMatt Macy #endif 38eda14cbcSMatt Macy 39eda14cbcSMatt Macy 40eda14cbcSMatt Macy /******************************************************************************* 41eda14cbcSMatt Macy Introduction 42eda14cbcSMatt Macy 43eda14cbcSMatt Macy zstd, short for Zstandard, is a fast lossless compression algorithm, targeting 44eda14cbcSMatt Macy real-time compression scenarios at zlib-level and better compression ratios. 45eda14cbcSMatt Macy The zstd compression library provides in-memory compression and decompression 46eda14cbcSMatt Macy functions. 47eda14cbcSMatt Macy 48eda14cbcSMatt Macy The library supports regular compression levels from 1 up to ZSTD_maxCLevel(), 49eda14cbcSMatt Macy which is currently 22. Levels >= 20, labeled `--ultra`, should be used with 50eda14cbcSMatt Macy caution, as they require more memory. The library also offers negative 51eda14cbcSMatt Macy compression levels, which extend the range of speed vs. ratio preferences. 52eda14cbcSMatt Macy The lower the level, the faster the speed (at the cost of compression). 53eda14cbcSMatt Macy 54eda14cbcSMatt Macy Compression can be done in: 55eda14cbcSMatt Macy - a single step (described as Simple API) 56eda14cbcSMatt Macy - a single step, reusing a context (described as Explicit context) 57eda14cbcSMatt Macy - unbounded multiple steps (described as Streaming compression) 58eda14cbcSMatt Macy 59eda14cbcSMatt Macy The compression ratio achievable on small data can be highly improved using 60eda14cbcSMatt Macy a dictionary. Dictionary compression can be performed in: 61eda14cbcSMatt Macy - a single step (described as Simple dictionary API) 62eda14cbcSMatt Macy - a single step, reusing a dictionary (described as Bulk-processing 63eda14cbcSMatt Macy dictionary API) 64eda14cbcSMatt Macy 65eda14cbcSMatt Macy Advanced experimental functions can be accessed using 66eda14cbcSMatt Macy `#define ZSTD_STATIC_LINKING_ONLY` before including zstd.h. 67eda14cbcSMatt Macy 68eda14cbcSMatt Macy Advanced experimental APIs should never be used with a dynamically-linked 69eda14cbcSMatt Macy library. They are not "stable"; their definitions or signatures may change in 70eda14cbcSMatt Macy the future. Only static linking is allowed. 71eda14cbcSMatt Macy *******************************************************************************/ 72eda14cbcSMatt Macy 73eda14cbcSMatt Macy /*------ Version ------*/ 74eda14cbcSMatt Macy #define ZSTD_VERSION_MAJOR 1 75eda14cbcSMatt Macy #define ZSTD_VERSION_MINOR 4 76eda14cbcSMatt Macy #define ZSTD_VERSION_RELEASE 5 77eda14cbcSMatt Macy 78eda14cbcSMatt Macy #define ZSTD_VERSION_NUMBER (ZSTD_VERSION_MAJOR *100*100 + ZSTD_VERSION_MINOR *100 + ZSTD_VERSION_RELEASE) 79eda14cbcSMatt Macy ZSTDLIB_API unsigned ZSTD_versionNumber(void); /**< to check runtime library version */ 80eda14cbcSMatt Macy 81eda14cbcSMatt Macy #define ZSTD_LIB_VERSION ZSTD_VERSION_MAJOR.ZSTD_VERSION_MINOR.ZSTD_VERSION_RELEASE 82eda14cbcSMatt Macy #define ZSTD_QUOTE(str) #str 83eda14cbcSMatt Macy #define ZSTD_EXPAND_AND_QUOTE(str) ZSTD_QUOTE(str) 84eda14cbcSMatt Macy #define ZSTD_VERSION_STRING ZSTD_EXPAND_AND_QUOTE(ZSTD_LIB_VERSION) 85eda14cbcSMatt Macy ZSTDLIB_API const char* ZSTD_versionString(void); /* requires v1.3.0+ */ 86eda14cbcSMatt Macy 87eda14cbcSMatt Macy /* ************************************* 88eda14cbcSMatt Macy * Default constant 89eda14cbcSMatt Macy ***************************************/ 90eda14cbcSMatt Macy #ifndef ZSTD_CLEVEL_DEFAULT 91eda14cbcSMatt Macy # define ZSTD_CLEVEL_DEFAULT 3 92eda14cbcSMatt Macy #endif 93eda14cbcSMatt Macy 94eda14cbcSMatt Macy /* ************************************* 95eda14cbcSMatt Macy * Constants 96eda14cbcSMatt Macy ***************************************/ 97eda14cbcSMatt Macy 98eda14cbcSMatt Macy /* All magic numbers are supposed read/written to/from files/memory using little-endian convention */ 99eda14cbcSMatt Macy #define ZSTD_MAGICNUMBER 0xFD2FB528 /* valid since v0.8.0 */ 100eda14cbcSMatt Macy #define ZSTD_MAGIC_DICTIONARY 0xEC30A437 /* valid since v0.7.0 */ 101eda14cbcSMatt Macy #define ZSTD_MAGIC_SKIPPABLE_START 0x184D2A50 /* all 16 values, from 0x184D2A50 to 0x184D2A5F, signal the beginning of a skippable frame */ 102eda14cbcSMatt Macy #define ZSTD_MAGIC_SKIPPABLE_MASK 0xFFFFFFF0 103eda14cbcSMatt Macy 104eda14cbcSMatt Macy #define ZSTD_BLOCKSIZELOG_MAX 17 105eda14cbcSMatt Macy #define ZSTD_BLOCKSIZE_MAX (1<<ZSTD_BLOCKSIZELOG_MAX) 106eda14cbcSMatt Macy 107eda14cbcSMatt Macy 108eda14cbcSMatt Macy 109eda14cbcSMatt Macy /*************************************** 110eda14cbcSMatt Macy * Simple API 111eda14cbcSMatt Macy ***************************************/ 112eda14cbcSMatt Macy /*! ZSTD_compress() : 113eda14cbcSMatt Macy * Compresses `src` content as a single zstd compressed frame into already allocated `dst`. 114eda14cbcSMatt Macy * Hint : compression runs faster if `dstCapacity` >= `ZSTD_compressBound(srcSize)`. 115eda14cbcSMatt Macy * @return : compressed size written into `dst` (<= `dstCapacity), 116eda14cbcSMatt Macy * or an error code if it fails (which can be tested using ZSTD_isError()). */ 117eda14cbcSMatt Macy ZSTDLIB_API size_t ZSTD_compress( void* dst, size_t dstCapacity, 118eda14cbcSMatt Macy const void* src, size_t srcSize, 119eda14cbcSMatt Macy int compressionLevel); 120eda14cbcSMatt Macy 121eda14cbcSMatt Macy /*! ZSTD_decompress() : 122eda14cbcSMatt Macy * `compressedSize` : must be the _exact_ size of some number of compressed and/or skippable frames. 123eda14cbcSMatt Macy * `dstCapacity` is an upper bound of originalSize to regenerate. 124eda14cbcSMatt Macy * If user cannot imply a maximum upper bound, it's better to use streaming mode to decompress data. 125eda14cbcSMatt Macy * @return : the number of bytes decompressed into `dst` (<= `dstCapacity`), 126eda14cbcSMatt Macy * or an errorCode if it fails (which can be tested using ZSTD_isError()). */ 127eda14cbcSMatt Macy ZSTDLIB_API size_t ZSTD_decompress( void* dst, size_t dstCapacity, 128eda14cbcSMatt Macy const void* src, size_t compressedSize); 129eda14cbcSMatt Macy 130eda14cbcSMatt Macy /*! ZSTD_getFrameContentSize() : requires v1.3.0+ 131eda14cbcSMatt Macy * `src` should point to the start of a ZSTD encoded frame. 132eda14cbcSMatt Macy * `srcSize` must be at least as large as the frame header. 133eda14cbcSMatt Macy * hint : any size >= `ZSTD_frameHeaderSize_max` is large enough. 134eda14cbcSMatt Macy * @return : - decompressed size of `src` frame content, if known 135eda14cbcSMatt Macy * - ZSTD_CONTENTSIZE_UNKNOWN if the size cannot be determined 136eda14cbcSMatt Macy * - ZSTD_CONTENTSIZE_ERROR if an error occurred (e.g. invalid magic number, srcSize too small) 137eda14cbcSMatt Macy * note 1 : a 0 return value means the frame is valid but "empty". 138eda14cbcSMatt Macy * note 2 : decompressed size is an optional field, it may not be present, typically in streaming mode. 139eda14cbcSMatt Macy * When `return==ZSTD_CONTENTSIZE_UNKNOWN`, data to decompress could be any size. 140eda14cbcSMatt Macy * In which case, it's necessary to use streaming mode to decompress data. 141eda14cbcSMatt Macy * Optionally, application can rely on some implicit limit, 142eda14cbcSMatt Macy * as ZSTD_decompress() only needs an upper bound of decompressed size. 143eda14cbcSMatt Macy * (For example, data could be necessarily cut into blocks <= 16 KB). 144eda14cbcSMatt Macy * note 3 : decompressed size is always present when compression is completed using single-pass functions, 145eda14cbcSMatt Macy * such as ZSTD_compress(), ZSTD_compressCCtx() ZSTD_compress_usingDict() or ZSTD_compress_usingCDict(). 146eda14cbcSMatt Macy * note 4 : decompressed size can be very large (64-bits value), 147eda14cbcSMatt Macy * potentially larger than what local system can handle as a single memory segment. 148eda14cbcSMatt Macy * In which case, it's necessary to use streaming mode to decompress data. 149eda14cbcSMatt Macy * note 5 : If source is untrusted, decompressed size could be wrong or intentionally modified. 150eda14cbcSMatt Macy * Always ensure return value fits within application's authorized limits. 151eda14cbcSMatt Macy * Each application can set its own limits. 152eda14cbcSMatt Macy * note 6 : This function replaces ZSTD_getDecompressedSize() */ 153eda14cbcSMatt Macy #define ZSTD_CONTENTSIZE_UNKNOWN (0ULL - 1) 154eda14cbcSMatt Macy #define ZSTD_CONTENTSIZE_ERROR (0ULL - 2) 155eda14cbcSMatt Macy ZSTDLIB_API unsigned long long ZSTD_getFrameContentSize(const void *src, size_t srcSize); 156eda14cbcSMatt Macy 157eda14cbcSMatt Macy /*! ZSTD_getDecompressedSize() : 158eda14cbcSMatt Macy * NOTE: This function is now obsolete, in favor of ZSTD_getFrameContentSize(). 159eda14cbcSMatt Macy * Both functions work the same way, but ZSTD_getDecompressedSize() blends 160eda14cbcSMatt Macy * "empty", "unknown" and "error" results to the same return value (0), 161eda14cbcSMatt Macy * while ZSTD_getFrameContentSize() gives them separate return values. 162eda14cbcSMatt Macy * @return : decompressed size of `src` frame content _if known and not empty_, 0 otherwise. */ 163eda14cbcSMatt Macy ZSTDLIB_API unsigned long long ZSTD_getDecompressedSize(const void* src, size_t srcSize); 164eda14cbcSMatt Macy 165eda14cbcSMatt Macy /*! ZSTD_findFrameCompressedSize() : 166eda14cbcSMatt Macy * `src` should point to the start of a ZSTD frame or skippable frame. 167eda14cbcSMatt Macy * `srcSize` must be >= first frame size 168eda14cbcSMatt Macy * @return : the compressed size of the first frame starting at `src`, 169eda14cbcSMatt Macy * suitable to pass as `srcSize` to `ZSTD_decompress` or similar, 170eda14cbcSMatt Macy * or an error code if input is invalid */ 171eda14cbcSMatt Macy ZSTDLIB_API size_t ZSTD_findFrameCompressedSize(const void* src, size_t srcSize); 172eda14cbcSMatt Macy 173eda14cbcSMatt Macy 174eda14cbcSMatt Macy /*====== Helper functions ======*/ 175eda14cbcSMatt Macy #define ZSTD_COMPRESSBOUND(srcSize) ((srcSize) + ((srcSize)>>8) + (((srcSize) < (128<<10)) ? (((128<<10) - (srcSize)) >> 11) /* margin, from 64 to 0 */ : 0)) /* this formula ensures that bound(A) + bound(B) <= bound(A+B) as long as A and B >= 128 KB */ 176eda14cbcSMatt Macy ZSTDLIB_API size_t ZSTD_compressBound(size_t srcSize); /*!< maximum compressed size in worst case single-pass scenario */ 177eda14cbcSMatt Macy ZSTDLIB_API unsigned ZSTD_isError(size_t code); /*!< tells if a `size_t` function result is an error code */ 178eda14cbcSMatt Macy ZSTDLIB_API const char* ZSTD_getErrorName(size_t code); /*!< provides readable string from an error code */ 179eda14cbcSMatt Macy ZSTDLIB_API int ZSTD_minCLevel(void); /*!< minimum negative compression level allowed */ 180eda14cbcSMatt Macy ZSTDLIB_API int ZSTD_maxCLevel(void); /*!< maximum compression level available */ 181eda14cbcSMatt Macy 182eda14cbcSMatt Macy 183eda14cbcSMatt Macy /*************************************** 184eda14cbcSMatt Macy * Explicit context 185eda14cbcSMatt Macy ***************************************/ 186eda14cbcSMatt Macy /*= Compression context 187eda14cbcSMatt Macy * When compressing many times, 188eda14cbcSMatt Macy * it is recommended to allocate a context just once, 189eda14cbcSMatt Macy * and re-use it for each successive compression operation. 190eda14cbcSMatt Macy * This will make workload friendlier for system's memory. 191eda14cbcSMatt Macy * Note : re-using context is just a speed / resource optimization. 192eda14cbcSMatt Macy * It doesn't change the compression ratio, which remains identical. 193eda14cbcSMatt Macy * Note 2 : In multi-threaded environments, 194eda14cbcSMatt Macy * use one different context per thread for parallel execution. 195eda14cbcSMatt Macy */ 196eda14cbcSMatt Macy typedef struct ZSTD_CCtx_s ZSTD_CCtx; 197eda14cbcSMatt Macy ZSTDLIB_API ZSTD_CCtx* ZSTD_createCCtx(void); 198eda14cbcSMatt Macy ZSTDLIB_API size_t ZSTD_freeCCtx(ZSTD_CCtx* cctx); 199eda14cbcSMatt Macy 200eda14cbcSMatt Macy /*! ZSTD_compressCCtx() : 201eda14cbcSMatt Macy * Same as ZSTD_compress(), using an explicit ZSTD_CCtx. 202eda14cbcSMatt Macy * Important : in order to behave similarly to `ZSTD_compress()`, 203eda14cbcSMatt Macy * this function compresses at requested compression level, 204eda14cbcSMatt Macy * __ignoring any other parameter__ . 205eda14cbcSMatt Macy * If any advanced parameter was set using the advanced API, 206eda14cbcSMatt Macy * they will all be reset. Only `compressionLevel` remains. 207eda14cbcSMatt Macy */ 208eda14cbcSMatt Macy ZSTDLIB_API size_t ZSTD_compressCCtx(ZSTD_CCtx* cctx, 209eda14cbcSMatt Macy void* dst, size_t dstCapacity, 210eda14cbcSMatt Macy const void* src, size_t srcSize, 211eda14cbcSMatt Macy int compressionLevel); 212eda14cbcSMatt Macy 213eda14cbcSMatt Macy /*= Decompression context 214eda14cbcSMatt Macy * When decompressing many times, 215eda14cbcSMatt Macy * it is recommended to allocate a context only once, 216eda14cbcSMatt Macy * and re-use it for each successive compression operation. 217eda14cbcSMatt Macy * This will make workload friendlier for system's memory. 218eda14cbcSMatt Macy * Use one context per thread for parallel execution. */ 219eda14cbcSMatt Macy typedef struct ZSTD_DCtx_s ZSTD_DCtx; 220eda14cbcSMatt Macy ZSTDLIB_API ZSTD_DCtx* ZSTD_createDCtx(void); 221eda14cbcSMatt Macy ZSTDLIB_API size_t ZSTD_freeDCtx(ZSTD_DCtx* dctx); 222eda14cbcSMatt Macy 223eda14cbcSMatt Macy /*! ZSTD_decompressDCtx() : 224eda14cbcSMatt Macy * Same as ZSTD_decompress(), 225eda14cbcSMatt Macy * requires an allocated ZSTD_DCtx. 226eda14cbcSMatt Macy * Compatible with sticky parameters. 227eda14cbcSMatt Macy */ 228eda14cbcSMatt Macy ZSTDLIB_API size_t ZSTD_decompressDCtx(ZSTD_DCtx* dctx, 229eda14cbcSMatt Macy void* dst, size_t dstCapacity, 230eda14cbcSMatt Macy const void* src, size_t srcSize); 231eda14cbcSMatt Macy 232eda14cbcSMatt Macy 233eda14cbcSMatt Macy /*************************************** 234eda14cbcSMatt Macy * Advanced compression API 235eda14cbcSMatt Macy ***************************************/ 236eda14cbcSMatt Macy 237eda14cbcSMatt Macy /* API design : 238eda14cbcSMatt Macy * Parameters are pushed one by one into an existing context, 239eda14cbcSMatt Macy * using ZSTD_CCtx_set*() functions. 240eda14cbcSMatt Macy * Pushed parameters are sticky : they are valid for next compressed frame, and any subsequent frame. 241eda14cbcSMatt Macy * "sticky" parameters are applicable to `ZSTD_compress2()` and `ZSTD_compressStream*()` ! 242eda14cbcSMatt Macy * __They do not apply to "simple" one-shot variants such as ZSTD_compressCCtx()__ . 243eda14cbcSMatt Macy * 244eda14cbcSMatt Macy * It's possible to reset all parameters to "default" using ZSTD_CCtx_reset(). 245eda14cbcSMatt Macy * 246eda14cbcSMatt Macy * This API supercedes all other "advanced" API entry points in the experimental section. 247eda14cbcSMatt Macy * In the future, we expect to remove from experimental API entry points which are redundant with this API. 248eda14cbcSMatt Macy */ 249eda14cbcSMatt Macy 250eda14cbcSMatt Macy 251eda14cbcSMatt Macy /* Compression strategies, listed from fastest to strongest */ 252eda14cbcSMatt Macy typedef enum { ZSTD_fast=1, 253eda14cbcSMatt Macy ZSTD_dfast=2, 254eda14cbcSMatt Macy ZSTD_greedy=3, 255eda14cbcSMatt Macy ZSTD_lazy=4, 256eda14cbcSMatt Macy ZSTD_lazy2=5, 257eda14cbcSMatt Macy ZSTD_btlazy2=6, 258eda14cbcSMatt Macy ZSTD_btopt=7, 259eda14cbcSMatt Macy ZSTD_btultra=8, 260eda14cbcSMatt Macy ZSTD_btultra2=9 261eda14cbcSMatt Macy /* note : new strategies _might_ be added in the future. 262eda14cbcSMatt Macy Only the order (from fast to strong) is guaranteed */ 263eda14cbcSMatt Macy } ZSTD_strategy; 264eda14cbcSMatt Macy 265eda14cbcSMatt Macy 266eda14cbcSMatt Macy typedef enum { 267eda14cbcSMatt Macy 268eda14cbcSMatt Macy /* compression parameters 269eda14cbcSMatt Macy * Note: When compressing with a ZSTD_CDict these parameters are superseded 270eda14cbcSMatt Macy * by the parameters used to construct the ZSTD_CDict. 271eda14cbcSMatt Macy * See ZSTD_CCtx_refCDict() for more info (superseded-by-cdict). */ 272eda14cbcSMatt Macy ZSTD_c_compressionLevel=100, /* Set compression parameters according to pre-defined cLevel table. 273eda14cbcSMatt Macy * Note that exact compression parameters are dynamically determined, 274eda14cbcSMatt Macy * depending on both compression level and srcSize (when known). 275eda14cbcSMatt Macy * Default level is ZSTD_CLEVEL_DEFAULT==3. 276eda14cbcSMatt Macy * Special: value 0 means default, which is controlled by ZSTD_CLEVEL_DEFAULT. 277eda14cbcSMatt Macy * Note 1 : it's possible to pass a negative compression level. 278eda14cbcSMatt Macy * Note 2 : setting a level does not automatically set all other compression parameters 279eda14cbcSMatt Macy * to default. Setting this will however eventually dynamically impact the compression 280eda14cbcSMatt Macy * parameters which have not been manually set. The manually set 281eda14cbcSMatt Macy * ones will 'stick'. */ 282eda14cbcSMatt Macy /* Advanced compression parameters : 283eda14cbcSMatt Macy * It's possible to pin down compression parameters to some specific values. 284eda14cbcSMatt Macy * In which case, these values are no longer dynamically selected by the compressor */ 285eda14cbcSMatt Macy ZSTD_c_windowLog=101, /* Maximum allowed back-reference distance, expressed as power of 2. 286eda14cbcSMatt Macy * This will set a memory budget for streaming decompression, 287eda14cbcSMatt Macy * with larger values requiring more memory 288eda14cbcSMatt Macy * and typically compressing more. 289eda14cbcSMatt Macy * Must be clamped between ZSTD_WINDOWLOG_MIN and ZSTD_WINDOWLOG_MAX. 290eda14cbcSMatt Macy * Special: value 0 means "use default windowLog". 291eda14cbcSMatt Macy * Note: Using a windowLog greater than ZSTD_WINDOWLOG_LIMIT_DEFAULT 292eda14cbcSMatt Macy * requires explicitly allowing such size at streaming decompression stage. */ 293eda14cbcSMatt Macy ZSTD_c_hashLog=102, /* Size of the initial probe table, as a power of 2. 294eda14cbcSMatt Macy * Resulting memory usage is (1 << (hashLog+2)). 295eda14cbcSMatt Macy * Must be clamped between ZSTD_HASHLOG_MIN and ZSTD_HASHLOG_MAX. 296eda14cbcSMatt Macy * Larger tables improve compression ratio of strategies <= dFast, 297eda14cbcSMatt Macy * and improve speed of strategies > dFast. 298eda14cbcSMatt Macy * Special: value 0 means "use default hashLog". */ 299eda14cbcSMatt Macy ZSTD_c_chainLog=103, /* Size of the multi-probe search table, as a power of 2. 300eda14cbcSMatt Macy * Resulting memory usage is (1 << (chainLog+2)). 301eda14cbcSMatt Macy * Must be clamped between ZSTD_CHAINLOG_MIN and ZSTD_CHAINLOG_MAX. 302eda14cbcSMatt Macy * Larger tables result in better and slower compression. 303eda14cbcSMatt Macy * This parameter is useless for "fast" strategy. 304eda14cbcSMatt Macy * It's still useful when using "dfast" strategy, 305eda14cbcSMatt Macy * in which case it defines a secondary probe table. 306eda14cbcSMatt Macy * Special: value 0 means "use default chainLog". */ 307eda14cbcSMatt Macy ZSTD_c_searchLog=104, /* Number of search attempts, as a power of 2. 308eda14cbcSMatt Macy * More attempts result in better and slower compression. 309eda14cbcSMatt Macy * This parameter is useless for "fast" and "dFast" strategies. 310eda14cbcSMatt Macy * Special: value 0 means "use default searchLog". */ 311eda14cbcSMatt Macy ZSTD_c_minMatch=105, /* Minimum size of searched matches. 312eda14cbcSMatt Macy * Note that Zstandard can still find matches of smaller size, 313eda14cbcSMatt Macy * it just tweaks its search algorithm to look for this size and larger. 314eda14cbcSMatt Macy * Larger values increase compression and decompression speed, but decrease ratio. 315eda14cbcSMatt Macy * Must be clamped between ZSTD_MINMATCH_MIN and ZSTD_MINMATCH_MAX. 316eda14cbcSMatt Macy * Note that currently, for all strategies < btopt, effective minimum is 4. 317eda14cbcSMatt Macy * , for all strategies > fast, effective maximum is 6. 318eda14cbcSMatt Macy * Special: value 0 means "use default minMatchLength". */ 319eda14cbcSMatt Macy ZSTD_c_targetLength=106, /* Impact of this field depends on strategy. 320eda14cbcSMatt Macy * For strategies btopt, btultra & btultra2: 321eda14cbcSMatt Macy * Length of Match considered "good enough" to stop search. 322eda14cbcSMatt Macy * Larger values make compression stronger, and slower. 323eda14cbcSMatt Macy * For strategy fast: 324eda14cbcSMatt Macy * Distance between match sampling. 325eda14cbcSMatt Macy * Larger values make compression faster, and weaker. 326eda14cbcSMatt Macy * Special: value 0 means "use default targetLength". */ 327eda14cbcSMatt Macy ZSTD_c_strategy=107, /* See ZSTD_strategy enum definition. 328eda14cbcSMatt Macy * The higher the value of selected strategy, the more complex it is, 329eda14cbcSMatt Macy * resulting in stronger and slower compression. 330eda14cbcSMatt Macy * Special: value 0 means "use default strategy". */ 331eda14cbcSMatt Macy 332eda14cbcSMatt Macy /* LDM mode parameters */ 333eda14cbcSMatt Macy ZSTD_c_enableLongDistanceMatching=160, /* Enable long distance matching. 334eda14cbcSMatt Macy * This parameter is designed to improve compression ratio 335eda14cbcSMatt Macy * for large inputs, by finding large matches at long distance. 336eda14cbcSMatt Macy * It increases memory usage and window size. 337eda14cbcSMatt Macy * Note: enabling this parameter increases default ZSTD_c_windowLog to 128 MB 338eda14cbcSMatt Macy * except when expressly set to a different value. */ 339eda14cbcSMatt Macy ZSTD_c_ldmHashLog=161, /* Size of the table for long distance matching, as a power of 2. 340eda14cbcSMatt Macy * Larger values increase memory usage and compression ratio, 341eda14cbcSMatt Macy * but decrease compression speed. 342eda14cbcSMatt Macy * Must be clamped between ZSTD_HASHLOG_MIN and ZSTD_HASHLOG_MAX 343eda14cbcSMatt Macy * default: windowlog - 7. 344eda14cbcSMatt Macy * Special: value 0 means "automatically determine hashlog". */ 345eda14cbcSMatt Macy ZSTD_c_ldmMinMatch=162, /* Minimum match size for long distance matcher. 346eda14cbcSMatt Macy * Larger/too small values usually decrease compression ratio. 347eda14cbcSMatt Macy * Must be clamped between ZSTD_LDM_MINMATCH_MIN and ZSTD_LDM_MINMATCH_MAX. 348eda14cbcSMatt Macy * Special: value 0 means "use default value" (default: 64). */ 349eda14cbcSMatt Macy ZSTD_c_ldmBucketSizeLog=163, /* Log size of each bucket in the LDM hash table for collision resolution. 350eda14cbcSMatt Macy * Larger values improve collision resolution but decrease compression speed. 351eda14cbcSMatt Macy * The maximum value is ZSTD_LDM_BUCKETSIZELOG_MAX. 352eda14cbcSMatt Macy * Special: value 0 means "use default value" (default: 3). */ 353eda14cbcSMatt Macy ZSTD_c_ldmHashRateLog=164, /* Frequency of inserting/looking up entries into the LDM hash table. 354eda14cbcSMatt Macy * Must be clamped between 0 and (ZSTD_WINDOWLOG_MAX - ZSTD_HASHLOG_MIN). 355eda14cbcSMatt Macy * Default is MAX(0, (windowLog - ldmHashLog)), optimizing hash table usage. 356eda14cbcSMatt Macy * Larger values improve compression speed. 357eda14cbcSMatt Macy * Deviating far from default value will likely result in a compression ratio decrease. 358eda14cbcSMatt Macy * Special: value 0 means "automatically determine hashRateLog". */ 359eda14cbcSMatt Macy 360eda14cbcSMatt Macy /* frame parameters */ 361eda14cbcSMatt Macy ZSTD_c_contentSizeFlag=200, /* Content size will be written into frame header _whenever known_ (default:1) 362eda14cbcSMatt Macy * Content size must be known at the beginning of compression. 363eda14cbcSMatt Macy * This is automatically the case when using ZSTD_compress2(), 364eda14cbcSMatt Macy * For streaming scenarios, content size must be provided with ZSTD_CCtx_setPledgedSrcSize() */ 365eda14cbcSMatt Macy ZSTD_c_checksumFlag=201, /* A 32-bits checksum of content is written at end of frame (default:0) */ 366eda14cbcSMatt Macy ZSTD_c_dictIDFlag=202, /* When applicable, dictionary's ID is written into frame header (default:1) */ 367eda14cbcSMatt Macy 368eda14cbcSMatt Macy /* multi-threading parameters */ 369eda14cbcSMatt Macy /* These parameters are only useful if multi-threading is enabled (compiled with build macro ZSTD_MULTITHREAD). 370eda14cbcSMatt Macy * They return an error otherwise. */ 371eda14cbcSMatt Macy ZSTD_c_nbWorkers=400, /* Select how many threads will be spawned to compress in parallel. 372eda14cbcSMatt Macy * When nbWorkers >= 1, triggers asynchronous mode when used with ZSTD_compressStream*() : 373eda14cbcSMatt Macy * ZSTD_compressStream*() consumes input and flush output if possible, but immediately gives back control to caller, 374eda14cbcSMatt Macy * while compression work is performed in parallel, within worker threads. 375eda14cbcSMatt Macy * (note : a strong exception to this rule is when first invocation of ZSTD_compressStream2() sets ZSTD_e_end : 376eda14cbcSMatt Macy * in which case, ZSTD_compressStream2() delegates to ZSTD_compress2(), which is always a blocking call). 377eda14cbcSMatt Macy * More workers improve speed, but also increase memory usage. 378eda14cbcSMatt Macy * Default value is `0`, aka "single-threaded mode" : no worker is spawned, compression is performed inside Caller's thread, all invocations are blocking */ 379eda14cbcSMatt Macy ZSTD_c_jobSize=401, /* Size of a compression job. This value is enforced only when nbWorkers >= 1. 380eda14cbcSMatt Macy * Each compression job is completed in parallel, so this value can indirectly impact the nb of active threads. 381eda14cbcSMatt Macy * 0 means default, which is dynamically determined based on compression parameters. 382eda14cbcSMatt Macy * Job size must be a minimum of overlap size, or 1 MB, whichever is largest. 383eda14cbcSMatt Macy * The minimum size is automatically and transparently enforced. */ 384eda14cbcSMatt Macy ZSTD_c_overlapLog=402, /* Control the overlap size, as a fraction of window size. 385eda14cbcSMatt Macy * The overlap size is an amount of data reloaded from previous job at the beginning of a new job. 386eda14cbcSMatt Macy * It helps preserve compression ratio, while each job is compressed in parallel. 387eda14cbcSMatt Macy * This value is enforced only when nbWorkers >= 1. 388eda14cbcSMatt Macy * Larger values increase compression ratio, but decrease speed. 389eda14cbcSMatt Macy * Possible values range from 0 to 9 : 390eda14cbcSMatt Macy * - 0 means "default" : value will be determined by the library, depending on strategy 391eda14cbcSMatt Macy * - 1 means "no overlap" 392eda14cbcSMatt Macy * - 9 means "full overlap", using a full window size. 393eda14cbcSMatt Macy * Each intermediate rank increases/decreases load size by a factor 2 : 394eda14cbcSMatt Macy * 9: full window; 8: w/2; 7: w/4; 6: w/8; 5:w/16; 4: w/32; 3:w/64; 2:w/128; 1:no overlap; 0:default 395eda14cbcSMatt Macy * default value varies between 6 and 9, depending on strategy */ 396eda14cbcSMatt Macy 397eda14cbcSMatt Macy /* note : additional experimental parameters are also available 398eda14cbcSMatt Macy * within the experimental section of the API. 399eda14cbcSMatt Macy * At the time of this writing, they include : 400eda14cbcSMatt Macy * ZSTD_c_rsyncable 401eda14cbcSMatt Macy * ZSTD_c_format 402eda14cbcSMatt Macy * ZSTD_c_forceMaxWindow 403eda14cbcSMatt Macy * ZSTD_c_forceAttachDict 404eda14cbcSMatt Macy * ZSTD_c_literalCompressionMode 405eda14cbcSMatt Macy * ZSTD_c_targetCBlockSize 406eda14cbcSMatt Macy * ZSTD_c_srcSizeHint 407eda14cbcSMatt Macy * Because they are not stable, it's necessary to define ZSTD_STATIC_LINKING_ONLY to access them. 408eda14cbcSMatt Macy * note : never ever use experimentalParam? names directly; 409eda14cbcSMatt Macy * also, the enums values themselves are unstable and can still change. 410eda14cbcSMatt Macy */ 411eda14cbcSMatt Macy ZSTD_c_experimentalParam1=500, 412eda14cbcSMatt Macy ZSTD_c_experimentalParam2=10, 413eda14cbcSMatt Macy ZSTD_c_experimentalParam3=1000, 414eda14cbcSMatt Macy ZSTD_c_experimentalParam4=1001, 415eda14cbcSMatt Macy ZSTD_c_experimentalParam5=1002, 416eda14cbcSMatt Macy ZSTD_c_experimentalParam6=1003, 417eda14cbcSMatt Macy ZSTD_c_experimentalParam7=1004 418eda14cbcSMatt Macy } ZSTD_cParameter; 419eda14cbcSMatt Macy 420eda14cbcSMatt Macy typedef struct { 421eda14cbcSMatt Macy size_t error; 422eda14cbcSMatt Macy int lowerBound; 423eda14cbcSMatt Macy int upperBound; 424eda14cbcSMatt Macy } ZSTD_bounds; 425eda14cbcSMatt Macy 426eda14cbcSMatt Macy /*! ZSTD_cParam_getBounds() : 427eda14cbcSMatt Macy * All parameters must belong to an interval with lower and upper bounds, 428eda14cbcSMatt Macy * otherwise they will either trigger an error or be automatically clamped. 429eda14cbcSMatt Macy * @return : a structure, ZSTD_bounds, which contains 430eda14cbcSMatt Macy * - an error status field, which must be tested using ZSTD_isError() 431eda14cbcSMatt Macy * - lower and upper bounds, both inclusive 432eda14cbcSMatt Macy */ 433eda14cbcSMatt Macy ZSTDLIB_API ZSTD_bounds ZSTD_cParam_getBounds(ZSTD_cParameter cParam); 434eda14cbcSMatt Macy 435eda14cbcSMatt Macy /*! ZSTD_CCtx_setParameter() : 436eda14cbcSMatt Macy * Set one compression parameter, selected by enum ZSTD_cParameter. 437eda14cbcSMatt Macy * All parameters have valid bounds. Bounds can be queried using ZSTD_cParam_getBounds(). 438eda14cbcSMatt Macy * Providing a value beyond bound will either clamp it, or trigger an error (depending on parameter). 439eda14cbcSMatt Macy * Setting a parameter is generally only possible during frame initialization (before starting compression). 440eda14cbcSMatt Macy * Exception : when using multi-threading mode (nbWorkers >= 1), 441eda14cbcSMatt Macy * the following parameters can be updated _during_ compression (within same frame): 442eda14cbcSMatt Macy * => compressionLevel, hashLog, chainLog, searchLog, minMatch, targetLength and strategy. 443eda14cbcSMatt Macy * new parameters will be active for next job only (after a flush()). 444eda14cbcSMatt Macy * @return : an error code (which can be tested using ZSTD_isError()). 445eda14cbcSMatt Macy */ 446eda14cbcSMatt Macy ZSTDLIB_API size_t ZSTD_CCtx_setParameter(ZSTD_CCtx* cctx, ZSTD_cParameter param, int value); 447eda14cbcSMatt Macy 448eda14cbcSMatt Macy /*! ZSTD_CCtx_setPledgedSrcSize() : 449eda14cbcSMatt Macy * Total input data size to be compressed as a single frame. 450eda14cbcSMatt Macy * Value will be written in frame header, unless if explicitly forbidden using ZSTD_c_contentSizeFlag. 451eda14cbcSMatt Macy * This value will also be controlled at end of frame, and trigger an error if not respected. 452eda14cbcSMatt Macy * @result : 0, or an error code (which can be tested with ZSTD_isError()). 453eda14cbcSMatt Macy * Note 1 : pledgedSrcSize==0 actually means zero, aka an empty frame. 454eda14cbcSMatt Macy * In order to mean "unknown content size", pass constant ZSTD_CONTENTSIZE_UNKNOWN. 455eda14cbcSMatt Macy * ZSTD_CONTENTSIZE_UNKNOWN is default value for any new frame. 456eda14cbcSMatt Macy * Note 2 : pledgedSrcSize is only valid once, for the next frame. 457eda14cbcSMatt Macy * It's discarded at the end of the frame, and replaced by ZSTD_CONTENTSIZE_UNKNOWN. 458eda14cbcSMatt Macy * Note 3 : Whenever all input data is provided and consumed in a single round, 459eda14cbcSMatt Macy * for example with ZSTD_compress2(), 460eda14cbcSMatt Macy * or invoking immediately ZSTD_compressStream2(,,,ZSTD_e_end), 461eda14cbcSMatt Macy * this value is automatically overridden by srcSize instead. 462eda14cbcSMatt Macy */ 463eda14cbcSMatt Macy ZSTDLIB_API size_t ZSTD_CCtx_setPledgedSrcSize(ZSTD_CCtx* cctx, unsigned long long pledgedSrcSize); 464eda14cbcSMatt Macy 465eda14cbcSMatt Macy typedef enum { 466eda14cbcSMatt Macy ZSTD_reset_session_only = 1, 467eda14cbcSMatt Macy ZSTD_reset_parameters = 2, 468eda14cbcSMatt Macy ZSTD_reset_session_and_parameters = 3 469eda14cbcSMatt Macy } ZSTD_ResetDirective; 470eda14cbcSMatt Macy 471eda14cbcSMatt Macy /*! ZSTD_CCtx_reset() : 472eda14cbcSMatt Macy * There are 2 different things that can be reset, independently or jointly : 473eda14cbcSMatt Macy * - The session : will stop compressing current frame, and make CCtx ready to start a new one. 474eda14cbcSMatt Macy * Useful after an error, or to interrupt any ongoing compression. 475eda14cbcSMatt Macy * Any internal data not yet flushed is cancelled. 476eda14cbcSMatt Macy * Compression parameters and dictionary remain unchanged. 477eda14cbcSMatt Macy * They will be used to compress next frame. 478eda14cbcSMatt Macy * Resetting session never fails. 479eda14cbcSMatt Macy * - The parameters : changes all parameters back to "default". 480eda14cbcSMatt Macy * This removes any reference to any dictionary too. 481eda14cbcSMatt Macy * Parameters can only be changed between 2 sessions (i.e. no compression is currently ongoing) 482eda14cbcSMatt Macy * otherwise the reset fails, and function returns an error value (which can be tested using ZSTD_isError()) 483eda14cbcSMatt Macy * - Both : similar to resetting the session, followed by resetting parameters. 484eda14cbcSMatt Macy */ 485eda14cbcSMatt Macy ZSTDLIB_API size_t ZSTD_CCtx_reset(ZSTD_CCtx* cctx, ZSTD_ResetDirective reset); 486eda14cbcSMatt Macy 487eda14cbcSMatt Macy /*! ZSTD_compress2() : 488eda14cbcSMatt Macy * Behave the same as ZSTD_compressCCtx(), but compression parameters are set using the advanced API. 489eda14cbcSMatt Macy * ZSTD_compress2() always starts a new frame. 490eda14cbcSMatt Macy * Should cctx hold data from a previously unfinished frame, everything about it is forgotten. 491eda14cbcSMatt Macy * - Compression parameters are pushed into CCtx before starting compression, using ZSTD_CCtx_set*() 492eda14cbcSMatt Macy * - The function is always blocking, returns when compression is completed. 493eda14cbcSMatt Macy * Hint : compression runs faster if `dstCapacity` >= `ZSTD_compressBound(srcSize)`. 494eda14cbcSMatt Macy * @return : compressed size written into `dst` (<= `dstCapacity), 495eda14cbcSMatt Macy * or an error code if it fails (which can be tested using ZSTD_isError()). 496eda14cbcSMatt Macy */ 497eda14cbcSMatt Macy ZSTDLIB_API size_t ZSTD_compress2( ZSTD_CCtx* cctx, 498eda14cbcSMatt Macy void* dst, size_t dstCapacity, 499eda14cbcSMatt Macy const void* src, size_t srcSize); 500eda14cbcSMatt Macy 501eda14cbcSMatt Macy 502eda14cbcSMatt Macy /*************************************** 503eda14cbcSMatt Macy * Advanced decompression API 504eda14cbcSMatt Macy ***************************************/ 505eda14cbcSMatt Macy 506eda14cbcSMatt Macy /* The advanced API pushes parameters one by one into an existing DCtx context. 507eda14cbcSMatt Macy * Parameters are sticky, and remain valid for all following frames 508eda14cbcSMatt Macy * using the same DCtx context. 509eda14cbcSMatt Macy * It's possible to reset parameters to default values using ZSTD_DCtx_reset(). 510eda14cbcSMatt Macy * Note : This API is compatible with existing ZSTD_decompressDCtx() and ZSTD_decompressStream(). 511eda14cbcSMatt Macy * Therefore, no new decompression function is necessary. 512eda14cbcSMatt Macy */ 513eda14cbcSMatt Macy 514eda14cbcSMatt Macy typedef enum { 515eda14cbcSMatt Macy 516eda14cbcSMatt Macy ZSTD_d_windowLogMax=100, /* Select a size limit (in power of 2) beyond which 517eda14cbcSMatt Macy * the streaming API will refuse to allocate memory buffer 518eda14cbcSMatt Macy * in order to protect the host from unreasonable memory requirements. 519eda14cbcSMatt Macy * This parameter is only useful in streaming mode, since no internal buffer is allocated in single-pass mode. 520eda14cbcSMatt Macy * By default, a decompression context accepts window sizes <= (1 << ZSTD_WINDOWLOG_LIMIT_DEFAULT). 521eda14cbcSMatt Macy * Special: value 0 means "use default maximum windowLog". */ 522eda14cbcSMatt Macy 523eda14cbcSMatt Macy /* note : additional experimental parameters are also available 524eda14cbcSMatt Macy * within the experimental section of the API. 525eda14cbcSMatt Macy * At the time of this writing, they include : 526eda14cbcSMatt Macy * ZSTD_d_format 527eda14cbcSMatt Macy * ZSTD_d_stableOutBuffer 528eda14cbcSMatt Macy * Because they are not stable, it's necessary to define ZSTD_STATIC_LINKING_ONLY to access them. 529eda14cbcSMatt Macy * note : never ever use experimentalParam? names directly 530eda14cbcSMatt Macy */ 531eda14cbcSMatt Macy ZSTD_d_experimentalParam1=1000, 532eda14cbcSMatt Macy ZSTD_d_experimentalParam2=1001 533eda14cbcSMatt Macy 534eda14cbcSMatt Macy } ZSTD_dParameter; 535eda14cbcSMatt Macy 536eda14cbcSMatt Macy /*! ZSTD_dParam_getBounds() : 537eda14cbcSMatt Macy * All parameters must belong to an interval with lower and upper bounds, 538eda14cbcSMatt Macy * otherwise they will either trigger an error or be automatically clamped. 539eda14cbcSMatt Macy * @return : a structure, ZSTD_bounds, which contains 540eda14cbcSMatt Macy * - an error status field, which must be tested using ZSTD_isError() 541eda14cbcSMatt Macy * - both lower and upper bounds, inclusive 542eda14cbcSMatt Macy */ 543eda14cbcSMatt Macy ZSTDLIB_API ZSTD_bounds ZSTD_dParam_getBounds(ZSTD_dParameter dParam); 544eda14cbcSMatt Macy 545eda14cbcSMatt Macy /*! ZSTD_DCtx_setParameter() : 546eda14cbcSMatt Macy * Set one compression parameter, selected by enum ZSTD_dParameter. 547eda14cbcSMatt Macy * All parameters have valid bounds. Bounds can be queried using ZSTD_dParam_getBounds(). 548eda14cbcSMatt Macy * Providing a value beyond bound will either clamp it, or trigger an error (depending on parameter). 549eda14cbcSMatt Macy * Setting a parameter is only possible during frame initialization (before starting decompression). 550eda14cbcSMatt Macy * @return : 0, or an error code (which can be tested using ZSTD_isError()). 551eda14cbcSMatt Macy */ 552eda14cbcSMatt Macy ZSTDLIB_API size_t ZSTD_DCtx_setParameter(ZSTD_DCtx* dctx, ZSTD_dParameter param, int value); 553eda14cbcSMatt Macy 554eda14cbcSMatt Macy /*! ZSTD_DCtx_reset() : 555eda14cbcSMatt Macy * Return a DCtx to clean state. 556eda14cbcSMatt Macy * Session and parameters can be reset jointly or separately. 557eda14cbcSMatt Macy * Parameters can only be reset when no active frame is being decompressed. 558eda14cbcSMatt Macy * @return : 0, or an error code, which can be tested with ZSTD_isError() 559eda14cbcSMatt Macy */ 560eda14cbcSMatt Macy ZSTDLIB_API size_t ZSTD_DCtx_reset(ZSTD_DCtx* dctx, ZSTD_ResetDirective reset); 561eda14cbcSMatt Macy 562eda14cbcSMatt Macy 563eda14cbcSMatt Macy /**************************** 564eda14cbcSMatt Macy * Streaming 565eda14cbcSMatt Macy ****************************/ 566eda14cbcSMatt Macy 567eda14cbcSMatt Macy typedef struct ZSTD_inBuffer_s { 568eda14cbcSMatt Macy const void* src; /**< start of input buffer */ 569eda14cbcSMatt Macy size_t size; /**< size of input buffer */ 570eda14cbcSMatt Macy size_t pos; /**< position where reading stopped. Will be updated. Necessarily 0 <= pos <= size */ 571eda14cbcSMatt Macy } ZSTD_inBuffer; 572eda14cbcSMatt Macy 573eda14cbcSMatt Macy typedef struct ZSTD_outBuffer_s { 574eda14cbcSMatt Macy void* dst; /**< start of output buffer */ 575eda14cbcSMatt Macy size_t size; /**< size of output buffer */ 576eda14cbcSMatt Macy size_t pos; /**< position where writing stopped. Will be updated. Necessarily 0 <= pos <= size */ 577eda14cbcSMatt Macy } ZSTD_outBuffer; 578eda14cbcSMatt Macy 579eda14cbcSMatt Macy 580eda14cbcSMatt Macy 581eda14cbcSMatt Macy /*-*********************************************************************** 582eda14cbcSMatt Macy * Streaming compression - HowTo 583eda14cbcSMatt Macy * 584eda14cbcSMatt Macy * A ZSTD_CStream object is required to track streaming operation. 585eda14cbcSMatt Macy * Use ZSTD_createCStream() and ZSTD_freeCStream() to create/release resources. 586eda14cbcSMatt Macy * ZSTD_CStream objects can be reused multiple times on consecutive compression operations. 587eda14cbcSMatt Macy * It is recommended to re-use ZSTD_CStream since it will play nicer with system's memory, by re-using already allocated memory. 588eda14cbcSMatt Macy * 589eda14cbcSMatt Macy * For parallel execution, use one separate ZSTD_CStream per thread. 590eda14cbcSMatt Macy * 591eda14cbcSMatt Macy * note : since v1.3.0, ZSTD_CStream and ZSTD_CCtx are the same thing. 592eda14cbcSMatt Macy * 593eda14cbcSMatt Macy * Parameters are sticky : when starting a new compression on the same context, 594eda14cbcSMatt Macy * it will re-use the same sticky parameters as previous compression session. 595eda14cbcSMatt Macy * When in doubt, it's recommended to fully initialize the context before usage. 596eda14cbcSMatt Macy * Use ZSTD_CCtx_reset() to reset the context and ZSTD_CCtx_setParameter(), 597eda14cbcSMatt Macy * ZSTD_CCtx_setPledgedSrcSize(), or ZSTD_CCtx_loadDictionary() and friends to 598eda14cbcSMatt Macy * set more specific parameters, the pledged source size, or load a dictionary. 599eda14cbcSMatt Macy * 600eda14cbcSMatt Macy * Use ZSTD_compressStream2() with ZSTD_e_continue as many times as necessary to 601eda14cbcSMatt Macy * consume input stream. The function will automatically update both `pos` 602eda14cbcSMatt Macy * fields within `input` and `output`. 603eda14cbcSMatt Macy * Note that the function may not consume the entire input, for example, because 604eda14cbcSMatt Macy * the output buffer is already full, in which case `input.pos < input.size`. 605eda14cbcSMatt Macy * The caller must check if input has been entirely consumed. 606eda14cbcSMatt Macy * If not, the caller must make some room to receive more compressed data, 607eda14cbcSMatt Macy * and then present again remaining input data. 608eda14cbcSMatt Macy * note: ZSTD_e_continue is guaranteed to make some forward progress when called, 609eda14cbcSMatt Macy * but doesn't guarantee maximal forward progress. This is especially relevant 610eda14cbcSMatt Macy * when compressing with multiple threads. The call won't block if it can 611eda14cbcSMatt Macy * consume some input, but if it can't it will wait for some, but not all, 612eda14cbcSMatt Macy * output to be flushed. 613eda14cbcSMatt Macy * @return : provides a minimum amount of data remaining to be flushed from internal buffers 614eda14cbcSMatt Macy * or an error code, which can be tested using ZSTD_isError(). 615eda14cbcSMatt Macy * 616eda14cbcSMatt Macy * At any moment, it's possible to flush whatever data might remain stuck within internal buffer, 617eda14cbcSMatt Macy * using ZSTD_compressStream2() with ZSTD_e_flush. `output->pos` will be updated. 618eda14cbcSMatt Macy * Note that, if `output->size` is too small, a single invocation with ZSTD_e_flush might not be enough (return code > 0). 619eda14cbcSMatt Macy * In which case, make some room to receive more compressed data, and call again ZSTD_compressStream2() with ZSTD_e_flush. 620eda14cbcSMatt Macy * You must continue calling ZSTD_compressStream2() with ZSTD_e_flush until it returns 0, at which point you can change the 621eda14cbcSMatt Macy * operation. 622eda14cbcSMatt Macy * note: ZSTD_e_flush will flush as much output as possible, meaning when compressing with multiple threads, it will 623eda14cbcSMatt Macy * block until the flush is complete or the output buffer is full. 624eda14cbcSMatt Macy * @return : 0 if internal buffers are entirely flushed, 625eda14cbcSMatt Macy * >0 if some data still present within internal buffer (the value is minimal estimation of remaining size), 626eda14cbcSMatt Macy * or an error code, which can be tested using ZSTD_isError(). 627eda14cbcSMatt Macy * 628eda14cbcSMatt Macy * Calling ZSTD_compressStream2() with ZSTD_e_end instructs to finish a frame. 629eda14cbcSMatt Macy * It will perform a flush and write frame epilogue. 630eda14cbcSMatt Macy * The epilogue is required for decoders to consider a frame completed. 631eda14cbcSMatt Macy * flush operation is the same, and follows same rules as calling ZSTD_compressStream2() with ZSTD_e_flush. 632eda14cbcSMatt Macy * You must continue calling ZSTD_compressStream2() with ZSTD_e_end until it returns 0, at which point you are free to 633eda14cbcSMatt Macy * start a new frame. 634eda14cbcSMatt Macy * note: ZSTD_e_end will flush as much output as possible, meaning when compressing with multiple threads, it will 635eda14cbcSMatt Macy * block until the flush is complete or the output buffer is full. 636eda14cbcSMatt Macy * @return : 0 if frame fully completed and fully flushed, 637eda14cbcSMatt Macy * >0 if some data still present within internal buffer (the value is minimal estimation of remaining size), 638eda14cbcSMatt Macy * or an error code, which can be tested using ZSTD_isError(). 639eda14cbcSMatt Macy * 640eda14cbcSMatt Macy * *******************************************************************/ 641eda14cbcSMatt Macy 642eda14cbcSMatt Macy typedef ZSTD_CCtx ZSTD_CStream; /**< CCtx and CStream are now effectively same object (>= v1.3.0) */ 643eda14cbcSMatt Macy /* Continue to distinguish them for compatibility with older versions <= v1.2.0 */ 644eda14cbcSMatt Macy /*===== ZSTD_CStream management functions =====*/ 645eda14cbcSMatt Macy ZSTDLIB_API ZSTD_CStream* ZSTD_createCStream(void); 646eda14cbcSMatt Macy ZSTDLIB_API size_t ZSTD_freeCStream(ZSTD_CStream* zcs); 647eda14cbcSMatt Macy 648eda14cbcSMatt Macy /*===== Streaming compression functions =====*/ 649eda14cbcSMatt Macy typedef enum { 650eda14cbcSMatt Macy ZSTD_e_continue=0, /* collect more data, encoder decides when to output compressed result, for optimal compression ratio */ 651eda14cbcSMatt Macy ZSTD_e_flush=1, /* flush any data provided so far, 652eda14cbcSMatt Macy * it creates (at least) one new block, that can be decoded immediately on reception; 653eda14cbcSMatt Macy * frame will continue: any future data can still reference previously compressed data, improving compression. 654eda14cbcSMatt Macy * note : multithreaded compression will block to flush as much output as possible. */ 655eda14cbcSMatt Macy ZSTD_e_end=2 /* flush any remaining data _and_ close current frame. 656eda14cbcSMatt Macy * note that frame is only closed after compressed data is fully flushed (return value == 0). 657eda14cbcSMatt Macy * After that point, any additional data starts a new frame. 658eda14cbcSMatt Macy * note : each frame is independent (does not reference any content from previous frame). 659eda14cbcSMatt Macy : note : multithreaded compression will block to flush as much output as possible. */ 660eda14cbcSMatt Macy } ZSTD_EndDirective; 661eda14cbcSMatt Macy 662eda14cbcSMatt Macy /*! ZSTD_compressStream2() : 663eda14cbcSMatt Macy * Behaves about the same as ZSTD_compressStream, with additional control on end directive. 664eda14cbcSMatt Macy * - Compression parameters are pushed into CCtx before starting compression, using ZSTD_CCtx_set*() 665eda14cbcSMatt Macy * - Compression parameters cannot be changed once compression is started (save a list of exceptions in multi-threading mode) 666eda14cbcSMatt Macy * - output->pos must be <= dstCapacity, input->pos must be <= srcSize 667eda14cbcSMatt Macy * - output->pos and input->pos will be updated. They are guaranteed to remain below their respective limit. 668eda14cbcSMatt Macy * - When nbWorkers==0 (default), function is blocking : it completes its job before returning to caller. 669eda14cbcSMatt Macy * - When nbWorkers>=1, function is non-blocking : it just acquires a copy of input, and distributes jobs to internal worker threads, flush whatever is available, 670eda14cbcSMatt Macy * and then immediately returns, just indicating that there is some data remaining to be flushed. 671eda14cbcSMatt Macy * The function nonetheless guarantees forward progress : it will return only after it reads or write at least 1+ byte. 672eda14cbcSMatt Macy * - Exception : if the first call requests a ZSTD_e_end directive and provides enough dstCapacity, the function delegates to ZSTD_compress2() which is always blocking. 673eda14cbcSMatt Macy * - @return provides a minimum amount of data remaining to be flushed from internal buffers 674eda14cbcSMatt Macy * or an error code, which can be tested using ZSTD_isError(). 675eda14cbcSMatt Macy * if @return != 0, flush is not fully completed, there is still some data left within internal buffers. 676eda14cbcSMatt Macy * This is useful for ZSTD_e_flush, since in this case more flushes are necessary to empty all buffers. 677eda14cbcSMatt Macy * For ZSTD_e_end, @return == 0 when internal buffers are fully flushed and frame is completed. 678eda14cbcSMatt Macy * - after a ZSTD_e_end directive, if internal buffer is not fully flushed (@return != 0), 679eda14cbcSMatt Macy * only ZSTD_e_end or ZSTD_e_flush operations are allowed. 680eda14cbcSMatt Macy * Before starting a new compression job, or changing compression parameters, 681eda14cbcSMatt Macy * it is required to fully flush internal buffers. 682eda14cbcSMatt Macy */ 683eda14cbcSMatt Macy ZSTDLIB_API size_t ZSTD_compressStream2( ZSTD_CCtx* cctx, 684eda14cbcSMatt Macy ZSTD_outBuffer* output, 685eda14cbcSMatt Macy ZSTD_inBuffer* input, 686eda14cbcSMatt Macy ZSTD_EndDirective endOp); 687eda14cbcSMatt Macy 688eda14cbcSMatt Macy 689eda14cbcSMatt Macy /* These buffer sizes are softly recommended. 690eda14cbcSMatt Macy * They are not required : ZSTD_compressStream*() happily accepts any buffer size, for both input and output. 691eda14cbcSMatt Macy * Respecting the recommended size just makes it a bit easier for ZSTD_compressStream*(), 692eda14cbcSMatt Macy * reducing the amount of memory shuffling and buffering, resulting in minor performance savings. 693eda14cbcSMatt Macy * 694eda14cbcSMatt Macy * However, note that these recommendations are from the perspective of a C caller program. 695eda14cbcSMatt Macy * If the streaming interface is invoked from some other language, 696eda14cbcSMatt Macy * especially managed ones such as Java or Go, through a foreign function interface such as jni or cgo, 697eda14cbcSMatt Macy * a major performance rule is to reduce crossing such interface to an absolute minimum. 698eda14cbcSMatt Macy * It's not rare that performance ends being spent more into the interface, rather than compression itself. 699eda14cbcSMatt Macy * In which cases, prefer using large buffers, as large as practical, 700eda14cbcSMatt Macy * for both input and output, to reduce the nb of roundtrips. 701eda14cbcSMatt Macy */ 702eda14cbcSMatt Macy ZSTDLIB_API size_t ZSTD_CStreamInSize(void); /**< recommended size for input buffer */ 703eda14cbcSMatt Macy ZSTDLIB_API size_t ZSTD_CStreamOutSize(void); /**< recommended size for output buffer. Guarantee to successfully flush at least one complete compressed block. */ 704eda14cbcSMatt Macy 705eda14cbcSMatt Macy 706eda14cbcSMatt Macy /* ***************************************************************************** 707eda14cbcSMatt Macy * This following is a legacy streaming API. 708eda14cbcSMatt Macy * It can be replaced by ZSTD_CCtx_reset() and ZSTD_compressStream2(). 709eda14cbcSMatt Macy * It is redundant, but remains fully supported. 710eda14cbcSMatt Macy * Advanced parameters and dictionary compression can only be used through the 711eda14cbcSMatt Macy * new API. 712eda14cbcSMatt Macy ******************************************************************************/ 713eda14cbcSMatt Macy 714eda14cbcSMatt Macy /*! 715eda14cbcSMatt Macy * Equivalent to: 716eda14cbcSMatt Macy * 717eda14cbcSMatt Macy * ZSTD_CCtx_reset(zcs, ZSTD_reset_session_only); 718eda14cbcSMatt Macy * ZSTD_CCtx_refCDict(zcs, NULL); // clear the dictionary (if any) 719eda14cbcSMatt Macy * ZSTD_CCtx_setParameter(zcs, ZSTD_c_compressionLevel, compressionLevel); 720eda14cbcSMatt Macy */ 721eda14cbcSMatt Macy ZSTDLIB_API size_t ZSTD_initCStream(ZSTD_CStream* zcs, int compressionLevel); 722eda14cbcSMatt Macy /*! 723eda14cbcSMatt Macy * Alternative for ZSTD_compressStream2(zcs, output, input, ZSTD_e_continue). 724eda14cbcSMatt Macy * NOTE: The return value is different. ZSTD_compressStream() returns a hint for 725eda14cbcSMatt Macy * the next read size (if non-zero and not an error). ZSTD_compressStream2() 726eda14cbcSMatt Macy * returns the minimum nb of bytes left to flush (if non-zero and not an error). 727eda14cbcSMatt Macy */ 728eda14cbcSMatt Macy ZSTDLIB_API size_t ZSTD_compressStream(ZSTD_CStream* zcs, ZSTD_outBuffer* output, ZSTD_inBuffer* input); 729eda14cbcSMatt Macy /*! Equivalent to ZSTD_compressStream2(zcs, output, &emptyInput, ZSTD_e_flush). */ 730eda14cbcSMatt Macy ZSTDLIB_API size_t ZSTD_flushStream(ZSTD_CStream* zcs, ZSTD_outBuffer* output); 731eda14cbcSMatt Macy /*! Equivalent to ZSTD_compressStream2(zcs, output, &emptyInput, ZSTD_e_end). */ 732eda14cbcSMatt Macy ZSTDLIB_API size_t ZSTD_endStream(ZSTD_CStream* zcs, ZSTD_outBuffer* output); 733eda14cbcSMatt Macy 734eda14cbcSMatt Macy 735eda14cbcSMatt Macy /*-*************************************************************************** 736eda14cbcSMatt Macy * Streaming decompression - HowTo 737eda14cbcSMatt Macy * 738eda14cbcSMatt Macy * A ZSTD_DStream object is required to track streaming operations. 739eda14cbcSMatt Macy * Use ZSTD_createDStream() and ZSTD_freeDStream() to create/release resources. 740eda14cbcSMatt Macy * ZSTD_DStream objects can be re-used multiple times. 741eda14cbcSMatt Macy * 742eda14cbcSMatt Macy * Use ZSTD_initDStream() to start a new decompression operation. 743eda14cbcSMatt Macy * @return : recommended first input size 744eda14cbcSMatt Macy * Alternatively, use advanced API to set specific properties. 745eda14cbcSMatt Macy * 746eda14cbcSMatt Macy * Use ZSTD_decompressStream() repetitively to consume your input. 747eda14cbcSMatt Macy * The function will update both `pos` fields. 748eda14cbcSMatt Macy * If `input.pos < input.size`, some input has not been consumed. 749eda14cbcSMatt Macy * It's up to the caller to present again remaining data. 750eda14cbcSMatt Macy * The function tries to flush all data decoded immediately, respecting output buffer size. 751eda14cbcSMatt Macy * If `output.pos < output.size`, decoder has flushed everything it could. 752eda14cbcSMatt Macy * But if `output.pos == output.size`, there might be some data left within internal buffers., 753eda14cbcSMatt Macy * In which case, call ZSTD_decompressStream() again to flush whatever remains in the buffer. 754eda14cbcSMatt Macy * Note : with no additional input provided, amount of data flushed is necessarily <= ZSTD_BLOCKSIZE_MAX. 755eda14cbcSMatt Macy * @return : 0 when a frame is completely decoded and fully flushed, 756eda14cbcSMatt Macy * or an error code, which can be tested using ZSTD_isError(), 757eda14cbcSMatt Macy * or any other value > 0, which means there is still some decoding or flushing to do to complete current frame : 758eda14cbcSMatt Macy * the return value is a suggested next input size (just a hint for better latency) 759eda14cbcSMatt Macy * that will never request more than the remaining frame size. 760eda14cbcSMatt Macy * *******************************************************************************/ 761eda14cbcSMatt Macy 762eda14cbcSMatt Macy typedef ZSTD_DCtx ZSTD_DStream; /**< DCtx and DStream are now effectively same object (>= v1.3.0) */ 763eda14cbcSMatt Macy /* For compatibility with versions <= v1.2.0, prefer differentiating them. */ 764eda14cbcSMatt Macy /*===== ZSTD_DStream management functions =====*/ 765eda14cbcSMatt Macy ZSTDLIB_API ZSTD_DStream* ZSTD_createDStream(void); 766eda14cbcSMatt Macy ZSTDLIB_API size_t ZSTD_freeDStream(ZSTD_DStream* zds); 767eda14cbcSMatt Macy 768eda14cbcSMatt Macy /*===== Streaming decompression functions =====*/ 769eda14cbcSMatt Macy 770eda14cbcSMatt Macy /* This function is redundant with the advanced API and equivalent to: 771eda14cbcSMatt Macy * 772eda14cbcSMatt Macy * ZSTD_DCtx_reset(zds, ZSTD_reset_session_only); 773eda14cbcSMatt Macy * ZSTD_DCtx_refDDict(zds, NULL); 774eda14cbcSMatt Macy */ 775eda14cbcSMatt Macy ZSTDLIB_API size_t ZSTD_initDStream(ZSTD_DStream* zds); 776eda14cbcSMatt Macy 777eda14cbcSMatt Macy ZSTDLIB_API size_t ZSTD_decompressStream(ZSTD_DStream* zds, ZSTD_outBuffer* output, ZSTD_inBuffer* input); 778eda14cbcSMatt Macy 779eda14cbcSMatt Macy ZSTDLIB_API size_t ZSTD_DStreamInSize(void); /*!< recommended size for input buffer */ 780eda14cbcSMatt Macy ZSTDLIB_API size_t ZSTD_DStreamOutSize(void); /*!< recommended size for output buffer. Guarantee to successfully flush at least one complete block in all circumstances. */ 781eda14cbcSMatt Macy 782eda14cbcSMatt Macy 783eda14cbcSMatt Macy /************************** 784eda14cbcSMatt Macy * Simple dictionary API 785eda14cbcSMatt Macy ***************************/ 786eda14cbcSMatt Macy /*! ZSTD_compress_usingDict() : 787eda14cbcSMatt Macy * Compression at an explicit compression level using a Dictionary. 788eda14cbcSMatt Macy * A dictionary can be any arbitrary data segment (also called a prefix), 789eda14cbcSMatt Macy * or a buffer with specified information (see dictBuilder/zdict.h). 790eda14cbcSMatt Macy * Note : This function loads the dictionary, resulting in significant startup delay. 791eda14cbcSMatt Macy * It's intended for a dictionary used only once. 792eda14cbcSMatt Macy * Note 2 : When `dict == NULL || dictSize < 8` no dictionary is used. */ 793eda14cbcSMatt Macy ZSTDLIB_API size_t ZSTD_compress_usingDict(ZSTD_CCtx* ctx, 794eda14cbcSMatt Macy void* dst, size_t dstCapacity, 795eda14cbcSMatt Macy const void* src, size_t srcSize, 796eda14cbcSMatt Macy const void* dict,size_t dictSize, 797eda14cbcSMatt Macy int compressionLevel); 798eda14cbcSMatt Macy 799eda14cbcSMatt Macy /*! ZSTD_decompress_usingDict() : 800eda14cbcSMatt Macy * Decompression using a known Dictionary. 801eda14cbcSMatt Macy * Dictionary must be identical to the one used during compression. 802eda14cbcSMatt Macy * Note : This function loads the dictionary, resulting in significant startup delay. 803eda14cbcSMatt Macy * It's intended for a dictionary used only once. 804eda14cbcSMatt Macy * Note : When `dict == NULL || dictSize < 8` no dictionary is used. */ 805eda14cbcSMatt Macy ZSTDLIB_API size_t ZSTD_decompress_usingDict(ZSTD_DCtx* dctx, 806eda14cbcSMatt Macy void* dst, size_t dstCapacity, 807eda14cbcSMatt Macy const void* src, size_t srcSize, 808eda14cbcSMatt Macy const void* dict,size_t dictSize); 809eda14cbcSMatt Macy 810eda14cbcSMatt Macy 811eda14cbcSMatt Macy /*********************************** 812eda14cbcSMatt Macy * Bulk processing dictionary API 813eda14cbcSMatt Macy **********************************/ 814eda14cbcSMatt Macy typedef struct ZSTD_CDict_s ZSTD_CDict; 815eda14cbcSMatt Macy 816eda14cbcSMatt Macy /*! ZSTD_createCDict() : 817eda14cbcSMatt Macy * When compressing multiple messages or blocks using the same dictionary, 818eda14cbcSMatt Macy * it's recommended to digest the dictionary only once, since it's a costly operation. 819eda14cbcSMatt Macy * ZSTD_createCDict() will create a state from digesting a dictionary. 820eda14cbcSMatt Macy * The resulting state can be used for future compression operations with very limited startup cost. 821eda14cbcSMatt Macy * ZSTD_CDict can be created once and shared by multiple threads concurrently, since its usage is read-only. 822eda14cbcSMatt Macy * @dictBuffer can be released after ZSTD_CDict creation, because its content is copied within CDict. 823eda14cbcSMatt Macy * Note 1 : Consider experimental function `ZSTD_createCDict_byReference()` if you prefer to not duplicate @dictBuffer content. 824eda14cbcSMatt Macy * Note 2 : A ZSTD_CDict can be created from an empty @dictBuffer, 825eda14cbcSMatt Macy * in which case the only thing that it transports is the @compressionLevel. 826eda14cbcSMatt Macy * This can be useful in a pipeline featuring ZSTD_compress_usingCDict() exclusively, 827eda14cbcSMatt Macy * expecting a ZSTD_CDict parameter with any data, including those without a known dictionary. */ 828eda14cbcSMatt Macy ZSTDLIB_API ZSTD_CDict* ZSTD_createCDict(const void* dictBuffer, size_t dictSize, 829eda14cbcSMatt Macy int compressionLevel); 830eda14cbcSMatt Macy 831eda14cbcSMatt Macy /*! ZSTD_freeCDict() : 832eda14cbcSMatt Macy * Function frees memory allocated by ZSTD_createCDict(). */ 833eda14cbcSMatt Macy ZSTDLIB_API size_t ZSTD_freeCDict(ZSTD_CDict* CDict); 834eda14cbcSMatt Macy 835eda14cbcSMatt Macy /*! ZSTD_compress_usingCDict() : 836eda14cbcSMatt Macy * Compression using a digested Dictionary. 837eda14cbcSMatt Macy * Recommended when same dictionary is used multiple times. 838eda14cbcSMatt Macy * Note : compression level is _decided at dictionary creation time_, 839eda14cbcSMatt Macy * and frame parameters are hardcoded (dictID=yes, contentSize=yes, checksum=no) */ 840eda14cbcSMatt Macy ZSTDLIB_API size_t ZSTD_compress_usingCDict(ZSTD_CCtx* cctx, 841eda14cbcSMatt Macy void* dst, size_t dstCapacity, 842eda14cbcSMatt Macy const void* src, size_t srcSize, 843eda14cbcSMatt Macy const ZSTD_CDict* cdict); 844eda14cbcSMatt Macy 845eda14cbcSMatt Macy 846eda14cbcSMatt Macy typedef struct ZSTD_DDict_s ZSTD_DDict; 847eda14cbcSMatt Macy 848eda14cbcSMatt Macy /*! ZSTD_createDDict() : 849eda14cbcSMatt Macy * Create a digested dictionary, ready to start decompression operation without startup delay. 850eda14cbcSMatt Macy * dictBuffer can be released after DDict creation, as its content is copied inside DDict. */ 851eda14cbcSMatt Macy ZSTDLIB_API ZSTD_DDict* ZSTD_createDDict(const void* dictBuffer, size_t dictSize); 852eda14cbcSMatt Macy 853eda14cbcSMatt Macy /*! ZSTD_freeDDict() : 854eda14cbcSMatt Macy * Function frees memory allocated with ZSTD_createDDict() */ 855eda14cbcSMatt Macy ZSTDLIB_API size_t ZSTD_freeDDict(ZSTD_DDict* ddict); 856eda14cbcSMatt Macy 857eda14cbcSMatt Macy /*! ZSTD_decompress_usingDDict() : 858eda14cbcSMatt Macy * Decompression using a digested Dictionary. 859eda14cbcSMatt Macy * Recommended when same dictionary is used multiple times. */ 860eda14cbcSMatt Macy ZSTDLIB_API size_t ZSTD_decompress_usingDDict(ZSTD_DCtx* dctx, 861eda14cbcSMatt Macy void* dst, size_t dstCapacity, 862eda14cbcSMatt Macy const void* src, size_t srcSize, 863eda14cbcSMatt Macy const ZSTD_DDict* ddict); 864eda14cbcSMatt Macy 865eda14cbcSMatt Macy 866eda14cbcSMatt Macy /******************************** 867eda14cbcSMatt Macy * Dictionary helper functions 868eda14cbcSMatt Macy *******************************/ 869eda14cbcSMatt Macy 870eda14cbcSMatt Macy /*! ZSTD_getDictID_fromDict() : 871eda14cbcSMatt Macy * Provides the dictID stored within dictionary. 872eda14cbcSMatt Macy * if @return == 0, the dictionary is not conformant with Zstandard specification. 873eda14cbcSMatt Macy * It can still be loaded, but as a content-only dictionary. */ 874eda14cbcSMatt Macy ZSTDLIB_API unsigned ZSTD_getDictID_fromDict(const void* dict, size_t dictSize); 875eda14cbcSMatt Macy 876eda14cbcSMatt Macy /*! ZSTD_getDictID_fromDDict() : 877eda14cbcSMatt Macy * Provides the dictID of the dictionary loaded into `ddict`. 878eda14cbcSMatt Macy * If @return == 0, the dictionary is not conformant to Zstandard specification, or empty. 879eda14cbcSMatt Macy * Non-conformant dictionaries can still be loaded, but as content-only dictionaries. */ 880eda14cbcSMatt Macy ZSTDLIB_API unsigned ZSTD_getDictID_fromDDict(const ZSTD_DDict* ddict); 881eda14cbcSMatt Macy 882eda14cbcSMatt Macy /*! ZSTD_getDictID_fromFrame() : 883eda14cbcSMatt Macy * Provides the dictID required to decompressed the frame stored within `src`. 884eda14cbcSMatt Macy * If @return == 0, the dictID could not be decoded. 885eda14cbcSMatt Macy * This could for one of the following reasons : 886eda14cbcSMatt Macy * - The frame does not require a dictionary to be decoded (most common case). 887eda14cbcSMatt Macy * - The frame was built with dictID intentionally removed. Whatever dictionary is necessary is a hidden information. 888eda14cbcSMatt Macy * Note : this use case also happens when using a non-conformant dictionary. 889eda14cbcSMatt Macy * - `srcSize` is too small, and as a result, the frame header could not be decoded (only possible if `srcSize < ZSTD_FRAMEHEADERSIZE_MAX`). 890eda14cbcSMatt Macy * - This is not a Zstandard frame. 891eda14cbcSMatt Macy * When identifying the exact failure cause, it's possible to use ZSTD_getFrameHeader(), which will provide a more precise error code. */ 892eda14cbcSMatt Macy ZSTDLIB_API unsigned ZSTD_getDictID_fromFrame(const void* src, size_t srcSize); 893eda14cbcSMatt Macy 894eda14cbcSMatt Macy 895eda14cbcSMatt Macy /******************************************************************************* 896eda14cbcSMatt Macy * Advanced dictionary and prefix API 897eda14cbcSMatt Macy * 898eda14cbcSMatt Macy * This API allows dictionaries to be used with ZSTD_compress2(), 899eda14cbcSMatt Macy * ZSTD_compressStream2(), and ZSTD_decompress(). Dictionaries are sticky, and 900eda14cbcSMatt Macy * only reset with the context is reset with ZSTD_reset_parameters or 901eda14cbcSMatt Macy * ZSTD_reset_session_and_parameters. Prefixes are single-use. 902eda14cbcSMatt Macy ******************************************************************************/ 903eda14cbcSMatt Macy 904eda14cbcSMatt Macy 905eda14cbcSMatt Macy /*! ZSTD_CCtx_loadDictionary() : 906eda14cbcSMatt Macy * Create an internal CDict from `dict` buffer. 907eda14cbcSMatt Macy * Decompression will have to use same dictionary. 908eda14cbcSMatt Macy * @result : 0, or an error code (which can be tested with ZSTD_isError()). 909eda14cbcSMatt Macy * Special: Loading a NULL (or 0-size) dictionary invalidates previous dictionary, 910eda14cbcSMatt Macy * meaning "return to no-dictionary mode". 911eda14cbcSMatt Macy * Note 1 : Dictionary is sticky, it will be used for all future compressed frames. 912eda14cbcSMatt Macy * To return to "no-dictionary" situation, load a NULL dictionary (or reset parameters). 913eda14cbcSMatt Macy * Note 2 : Loading a dictionary involves building tables. 914eda14cbcSMatt Macy * It's also a CPU consuming operation, with non-negligible impact on latency. 915eda14cbcSMatt Macy * Tables are dependent on compression parameters, and for this reason, 916eda14cbcSMatt Macy * compression parameters can no longer be changed after loading a dictionary. 917eda14cbcSMatt Macy * Note 3 :`dict` content will be copied internally. 918eda14cbcSMatt Macy * Use experimental ZSTD_CCtx_loadDictionary_byReference() to reference content instead. 919eda14cbcSMatt Macy * In such a case, dictionary buffer must outlive its users. 920eda14cbcSMatt Macy * Note 4 : Use ZSTD_CCtx_loadDictionary_advanced() 921eda14cbcSMatt Macy * to precisely select how dictionary content must be interpreted. */ 922eda14cbcSMatt Macy ZSTDLIB_API size_t ZSTD_CCtx_loadDictionary(ZSTD_CCtx* cctx, const void* dict, size_t dictSize); 923eda14cbcSMatt Macy 924eda14cbcSMatt Macy /*! ZSTD_CCtx_refCDict() : 925eda14cbcSMatt Macy * Reference a prepared dictionary, to be used for all next compressed frames. 926eda14cbcSMatt Macy * Note that compression parameters are enforced from within CDict, 927eda14cbcSMatt Macy * and supersede any compression parameter previously set within CCtx. 928eda14cbcSMatt Macy * The parameters ignored are labled as "superseded-by-cdict" in the ZSTD_cParameter enum docs. 929eda14cbcSMatt Macy * The ignored parameters will be used again if the CCtx is returned to no-dictionary mode. 930eda14cbcSMatt Macy * The dictionary will remain valid for future compressed frames using same CCtx. 931eda14cbcSMatt Macy * @result : 0, or an error code (which can be tested with ZSTD_isError()). 932eda14cbcSMatt Macy * Special : Referencing a NULL CDict means "return to no-dictionary mode". 933eda14cbcSMatt Macy * Note 1 : Currently, only one dictionary can be managed. 934eda14cbcSMatt Macy * Referencing a new dictionary effectively "discards" any previous one. 935eda14cbcSMatt Macy * Note 2 : CDict is just referenced, its lifetime must outlive its usage within CCtx. */ 936eda14cbcSMatt Macy ZSTDLIB_API size_t ZSTD_CCtx_refCDict(ZSTD_CCtx* cctx, const ZSTD_CDict* cdict); 937eda14cbcSMatt Macy 938eda14cbcSMatt Macy /*! ZSTD_CCtx_refPrefix() : 939eda14cbcSMatt Macy * Reference a prefix (single-usage dictionary) for next compressed frame. 940eda14cbcSMatt Macy * A prefix is **only used once**. Tables are discarded at end of frame (ZSTD_e_end). 941eda14cbcSMatt Macy * Decompression will need same prefix to properly regenerate data. 942eda14cbcSMatt Macy * Compressing with a prefix is similar in outcome as performing a diff and compressing it, 943eda14cbcSMatt Macy * but performs much faster, especially during decompression (compression speed is tunable with compression level). 944eda14cbcSMatt Macy * @result : 0, or an error code (which can be tested with ZSTD_isError()). 945eda14cbcSMatt Macy * Special: Adding any prefix (including NULL) invalidates any previous prefix or dictionary 946eda14cbcSMatt Macy * Note 1 : Prefix buffer is referenced. It **must** outlive compression. 947eda14cbcSMatt Macy * Its content must remain unmodified during compression. 948eda14cbcSMatt Macy * Note 2 : If the intention is to diff some large src data blob with some prior version of itself, 949eda14cbcSMatt Macy * ensure that the window size is large enough to contain the entire source. 950eda14cbcSMatt Macy * See ZSTD_c_windowLog. 951eda14cbcSMatt Macy * Note 3 : Referencing a prefix involves building tables, which are dependent on compression parameters. 952eda14cbcSMatt Macy * It's a CPU consuming operation, with non-negligible impact on latency. 953eda14cbcSMatt Macy * If there is a need to use the same prefix multiple times, consider loadDictionary instead. 954eda14cbcSMatt Macy * Note 4 : By default, the prefix is interpreted as raw content (ZSTD_dct_rawContent). 955eda14cbcSMatt Macy * Use experimental ZSTD_CCtx_refPrefix_advanced() to alter dictionary interpretation. */ 956eda14cbcSMatt Macy ZSTDLIB_API size_t ZSTD_CCtx_refPrefix(ZSTD_CCtx* cctx, 957eda14cbcSMatt Macy const void* prefix, size_t prefixSize); 958eda14cbcSMatt Macy 959eda14cbcSMatt Macy /*! ZSTD_DCtx_loadDictionary() : 960eda14cbcSMatt Macy * Create an internal DDict from dict buffer, 961eda14cbcSMatt Macy * to be used to decompress next frames. 962eda14cbcSMatt Macy * The dictionary remains valid for all future frames, until explicitly invalidated. 963eda14cbcSMatt Macy * @result : 0, or an error code (which can be tested with ZSTD_isError()). 964eda14cbcSMatt Macy * Special : Adding a NULL (or 0-size) dictionary invalidates any previous dictionary, 965eda14cbcSMatt Macy * meaning "return to no-dictionary mode". 966eda14cbcSMatt Macy * Note 1 : Loading a dictionary involves building tables, 967eda14cbcSMatt Macy * which has a non-negligible impact on CPU usage and latency. 968eda14cbcSMatt Macy * It's recommended to "load once, use many times", to amortize the cost 969eda14cbcSMatt Macy * Note 2 :`dict` content will be copied internally, so `dict` can be released after loading. 970eda14cbcSMatt Macy * Use ZSTD_DCtx_loadDictionary_byReference() to reference dictionary content instead. 971eda14cbcSMatt Macy * Note 3 : Use ZSTD_DCtx_loadDictionary_advanced() to take control of 972eda14cbcSMatt Macy * how dictionary content is loaded and interpreted. 973eda14cbcSMatt Macy */ 974eda14cbcSMatt Macy ZSTDLIB_API size_t ZSTD_DCtx_loadDictionary(ZSTD_DCtx* dctx, const void* dict, size_t dictSize); 975eda14cbcSMatt Macy 976eda14cbcSMatt Macy /*! ZSTD_DCtx_refDDict() : 977eda14cbcSMatt Macy * Reference a prepared dictionary, to be used to decompress next frames. 978eda14cbcSMatt Macy * The dictionary remains active for decompression of future frames using same DCtx. 979eda14cbcSMatt Macy * @result : 0, or an error code (which can be tested with ZSTD_isError()). 980eda14cbcSMatt Macy * Note 1 : Currently, only one dictionary can be managed. 981eda14cbcSMatt Macy * Referencing a new dictionary effectively "discards" any previous one. 982eda14cbcSMatt Macy * Special: referencing a NULL DDict means "return to no-dictionary mode". 983eda14cbcSMatt Macy * Note 2 : DDict is just referenced, its lifetime must outlive its usage from DCtx. 984eda14cbcSMatt Macy */ 985eda14cbcSMatt Macy ZSTDLIB_API size_t ZSTD_DCtx_refDDict(ZSTD_DCtx* dctx, const ZSTD_DDict* ddict); 986eda14cbcSMatt Macy 987eda14cbcSMatt Macy /*! ZSTD_DCtx_refPrefix() : 988eda14cbcSMatt Macy * Reference a prefix (single-usage dictionary) to decompress next frame. 989eda14cbcSMatt Macy * This is the reverse operation of ZSTD_CCtx_refPrefix(), 990eda14cbcSMatt Macy * and must use the same prefix as the one used during compression. 991eda14cbcSMatt Macy * Prefix is **only used once**. Reference is discarded at end of frame. 992eda14cbcSMatt Macy * End of frame is reached when ZSTD_decompressStream() returns 0. 993eda14cbcSMatt Macy * @result : 0, or an error code (which can be tested with ZSTD_isError()). 994eda14cbcSMatt Macy * Note 1 : Adding any prefix (including NULL) invalidates any previously set prefix or dictionary 995eda14cbcSMatt Macy * Note 2 : Prefix buffer is referenced. It **must** outlive decompression. 996eda14cbcSMatt Macy * Prefix buffer must remain unmodified up to the end of frame, 997eda14cbcSMatt Macy * reached when ZSTD_decompressStream() returns 0. 998eda14cbcSMatt Macy * Note 3 : By default, the prefix is treated as raw content (ZSTD_dct_rawContent). 999eda14cbcSMatt Macy * Use ZSTD_CCtx_refPrefix_advanced() to alter dictMode (Experimental section) 1000eda14cbcSMatt Macy * Note 4 : Referencing a raw content prefix has almost no cpu nor memory cost. 1001eda14cbcSMatt Macy * A full dictionary is more costly, as it requires building tables. 1002eda14cbcSMatt Macy */ 1003eda14cbcSMatt Macy ZSTDLIB_API size_t ZSTD_DCtx_refPrefix(ZSTD_DCtx* dctx, 1004eda14cbcSMatt Macy const void* prefix, size_t prefixSize); 1005eda14cbcSMatt Macy 1006eda14cbcSMatt Macy /* === Memory management === */ 1007eda14cbcSMatt Macy 1008eda14cbcSMatt Macy /*! ZSTD_sizeof_*() : 1009eda14cbcSMatt Macy * These functions give the _current_ memory usage of selected object. 1010eda14cbcSMatt Macy * Note that object memory usage can evolve (increase or decrease) over time. */ 1011eda14cbcSMatt Macy ZSTDLIB_API size_t ZSTD_sizeof_CCtx(const ZSTD_CCtx* cctx); 1012eda14cbcSMatt Macy ZSTDLIB_API size_t ZSTD_sizeof_DCtx(const ZSTD_DCtx* dctx); 1013eda14cbcSMatt Macy ZSTDLIB_API size_t ZSTD_sizeof_CStream(const ZSTD_CStream* zcs); 1014eda14cbcSMatt Macy ZSTDLIB_API size_t ZSTD_sizeof_DStream(const ZSTD_DStream* zds); 1015eda14cbcSMatt Macy ZSTDLIB_API size_t ZSTD_sizeof_CDict(const ZSTD_CDict* cdict); 1016eda14cbcSMatt Macy ZSTDLIB_API size_t ZSTD_sizeof_DDict(const ZSTD_DDict* ddict); 1017eda14cbcSMatt Macy 1018eda14cbcSMatt Macy #endif /* ZSTD_H_235446 */ 1019eda14cbcSMatt Macy 1020eda14cbcSMatt Macy 1021eda14cbcSMatt Macy /* ************************************************************************************** 1022eda14cbcSMatt Macy * ADVANCED AND EXPERIMENTAL FUNCTIONS 1023eda14cbcSMatt Macy **************************************************************************************** 1024eda14cbcSMatt Macy * The definitions in the following section are considered experimental. 1025eda14cbcSMatt Macy * They are provided for advanced scenarios. 1026eda14cbcSMatt Macy * They should never be used with a dynamic library, as prototypes may change in the future. 1027eda14cbcSMatt Macy * Use them only in association with static linking. 1028eda14cbcSMatt Macy * ***************************************************************************************/ 1029eda14cbcSMatt Macy 1030eda14cbcSMatt Macy #if defined(ZSTD_STATIC_LINKING_ONLY) && !defined(ZSTD_H_ZSTD_STATIC_LINKING_ONLY) 1031eda14cbcSMatt Macy #define ZSTD_H_ZSTD_STATIC_LINKING_ONLY 1032eda14cbcSMatt Macy 1033eda14cbcSMatt Macy /**************************************************************************************** 1034eda14cbcSMatt Macy * experimental API (static linking only) 1035eda14cbcSMatt Macy **************************************************************************************** 1036eda14cbcSMatt Macy * The following symbols and constants 1037eda14cbcSMatt Macy * are not planned to join "stable API" status in the near future. 1038eda14cbcSMatt Macy * They can still change in future versions. 1039eda14cbcSMatt Macy * Some of them are planned to remain in the static_only section indefinitely. 1040eda14cbcSMatt Macy * Some of them might be removed in the future (especially when redundant with existing stable functions) 1041eda14cbcSMatt Macy * ***************************************************************************************/ 1042eda14cbcSMatt Macy 1043eda14cbcSMatt Macy #define ZSTD_FRAMEHEADERSIZE_PREFIX(format) ((format) == ZSTD_f_zstd1 ? 5 : 1) /* minimum input size required to query frame header size */ 1044eda14cbcSMatt Macy #define ZSTD_FRAMEHEADERSIZE_MIN(format) ((format) == ZSTD_f_zstd1 ? 6 : 2) 1045eda14cbcSMatt Macy #define ZSTD_FRAMEHEADERSIZE_MAX 18 /* can be useful for static allocation */ 1046eda14cbcSMatt Macy #define ZSTD_SKIPPABLEHEADERSIZE 8 1047eda14cbcSMatt Macy 1048eda14cbcSMatt Macy /* compression parameter bounds */ 1049eda14cbcSMatt Macy #define ZSTD_WINDOWLOG_MAX_32 30 1050eda14cbcSMatt Macy #define ZSTD_WINDOWLOG_MAX_64 31 1051eda14cbcSMatt Macy #define ZSTD_WINDOWLOG_MAX ((int)(sizeof(size_t) == 4 ? ZSTD_WINDOWLOG_MAX_32 : ZSTD_WINDOWLOG_MAX_64)) 1052eda14cbcSMatt Macy #define ZSTD_WINDOWLOG_MIN 10 1053eda14cbcSMatt Macy #define ZSTD_HASHLOG_MAX ((ZSTD_WINDOWLOG_MAX < 30) ? ZSTD_WINDOWLOG_MAX : 30) 1054eda14cbcSMatt Macy #define ZSTD_HASHLOG_MIN 6 1055eda14cbcSMatt Macy #define ZSTD_CHAINLOG_MAX_32 29 1056eda14cbcSMatt Macy #define ZSTD_CHAINLOG_MAX_64 30 1057eda14cbcSMatt Macy #define ZSTD_CHAINLOG_MAX ((int)(sizeof(size_t) == 4 ? ZSTD_CHAINLOG_MAX_32 : ZSTD_CHAINLOG_MAX_64)) 1058eda14cbcSMatt Macy #define ZSTD_CHAINLOG_MIN ZSTD_HASHLOG_MIN 1059eda14cbcSMatt Macy #define ZSTD_SEARCHLOG_MAX (ZSTD_WINDOWLOG_MAX-1) 1060eda14cbcSMatt Macy #define ZSTD_SEARCHLOG_MIN 1 1061eda14cbcSMatt Macy #define ZSTD_MINMATCH_MAX 7 /* only for ZSTD_fast, other strategies are limited to 6 */ 1062eda14cbcSMatt Macy #define ZSTD_MINMATCH_MIN 3 /* only for ZSTD_btopt+, faster strategies are limited to 4 */ 1063eda14cbcSMatt Macy #define ZSTD_TARGETLENGTH_MAX ZSTD_BLOCKSIZE_MAX 1064eda14cbcSMatt Macy #define ZSTD_TARGETLENGTH_MIN 0 /* note : comparing this constant to an unsigned results in a tautological test */ 1065eda14cbcSMatt Macy #define ZSTD_STRATEGY_MIN ZSTD_fast 1066eda14cbcSMatt Macy #define ZSTD_STRATEGY_MAX ZSTD_btultra2 1067eda14cbcSMatt Macy 1068eda14cbcSMatt Macy 1069eda14cbcSMatt Macy #define ZSTD_OVERLAPLOG_MIN 0 1070eda14cbcSMatt Macy #define ZSTD_OVERLAPLOG_MAX 9 1071eda14cbcSMatt Macy 1072eda14cbcSMatt Macy #define ZSTD_WINDOWLOG_LIMIT_DEFAULT 27 /* by default, the streaming decoder will refuse any frame 1073eda14cbcSMatt Macy * requiring larger than (1<<ZSTD_WINDOWLOG_LIMIT_DEFAULT) window size, 1074eda14cbcSMatt Macy * to preserve host's memory from unreasonable requirements. 1075eda14cbcSMatt Macy * This limit can be overridden using ZSTD_DCtx_setParameter(,ZSTD_d_windowLogMax,). 1076eda14cbcSMatt Macy * The limit does not apply for one-pass decoders (such as ZSTD_decompress()), since no additional memory is allocated */ 1077eda14cbcSMatt Macy 1078eda14cbcSMatt Macy 1079eda14cbcSMatt Macy /* LDM parameter bounds */ 1080eda14cbcSMatt Macy #define ZSTD_LDM_HASHLOG_MIN ZSTD_HASHLOG_MIN 1081eda14cbcSMatt Macy #define ZSTD_LDM_HASHLOG_MAX ZSTD_HASHLOG_MAX 1082eda14cbcSMatt Macy #define ZSTD_LDM_MINMATCH_MIN 4 1083eda14cbcSMatt Macy #define ZSTD_LDM_MINMATCH_MAX 4096 1084eda14cbcSMatt Macy #define ZSTD_LDM_BUCKETSIZELOG_MIN 1 1085eda14cbcSMatt Macy #define ZSTD_LDM_BUCKETSIZELOG_MAX 8 1086eda14cbcSMatt Macy #define ZSTD_LDM_HASHRATELOG_MIN 0 1087eda14cbcSMatt Macy #define ZSTD_LDM_HASHRATELOG_MAX (ZSTD_WINDOWLOG_MAX - ZSTD_HASHLOG_MIN) 1088eda14cbcSMatt Macy 1089eda14cbcSMatt Macy /* Advanced parameter bounds */ 1090eda14cbcSMatt Macy #define ZSTD_TARGETCBLOCKSIZE_MIN 64 1091eda14cbcSMatt Macy #define ZSTD_TARGETCBLOCKSIZE_MAX ZSTD_BLOCKSIZE_MAX 1092eda14cbcSMatt Macy #define ZSTD_SRCSIZEHINT_MIN 0 1093eda14cbcSMatt Macy #define ZSTD_SRCSIZEHINT_MAX INT_MAX 1094eda14cbcSMatt Macy 1095eda14cbcSMatt Macy /* internal */ 1096eda14cbcSMatt Macy #define ZSTD_HASHLOG3_MAX 17 1097eda14cbcSMatt Macy 1098eda14cbcSMatt Macy 1099eda14cbcSMatt Macy /* --- Advanced types --- */ 1100eda14cbcSMatt Macy 1101eda14cbcSMatt Macy typedef struct ZSTD_CCtx_params_s ZSTD_CCtx_params; 1102eda14cbcSMatt Macy 1103eda14cbcSMatt Macy typedef struct { 1104eda14cbcSMatt Macy unsigned int matchPos; /* Match pos in dst */ 1105eda14cbcSMatt Macy /* If seqDef.offset > 3, then this is seqDef.offset - 3 1106eda14cbcSMatt Macy * If seqDef.offset < 3, then this is the corresponding repeat offset 1107eda14cbcSMatt Macy * But if seqDef.offset < 3 and litLength == 0, this is the 1108eda14cbcSMatt Macy * repeat offset before the corresponding repeat offset 1109eda14cbcSMatt Macy * And if seqDef.offset == 3 and litLength == 0, this is the 1110eda14cbcSMatt Macy * most recent repeat offset - 1 1111eda14cbcSMatt Macy */ 1112eda14cbcSMatt Macy unsigned int offset; 1113eda14cbcSMatt Macy unsigned int litLength; /* Literal length */ 1114eda14cbcSMatt Macy unsigned int matchLength; /* Match length */ 1115eda14cbcSMatt Macy /* 0 when seq not rep and seqDef.offset otherwise 1116eda14cbcSMatt Macy * when litLength == 0 this will be <= 4, otherwise <= 3 like normal 1117eda14cbcSMatt Macy */ 1118eda14cbcSMatt Macy unsigned int rep; 1119eda14cbcSMatt Macy } ZSTD_Sequence; 1120eda14cbcSMatt Macy 1121eda14cbcSMatt Macy typedef struct { 1122eda14cbcSMatt Macy unsigned windowLog; /**< largest match distance : larger == more compression, more memory needed during decompression */ 1123eda14cbcSMatt Macy unsigned chainLog; /**< fully searched segment : larger == more compression, slower, more memory (useless for fast) */ 1124eda14cbcSMatt Macy unsigned hashLog; /**< dispatch table : larger == faster, more memory */ 1125eda14cbcSMatt Macy unsigned searchLog; /**< nb of searches : larger == more compression, slower */ 1126eda14cbcSMatt Macy unsigned minMatch; /**< match length searched : larger == faster decompression, sometimes less compression */ 1127eda14cbcSMatt Macy unsigned targetLength; /**< acceptable match size for optimal parser (only) : larger == more compression, slower */ 1128eda14cbcSMatt Macy ZSTD_strategy strategy; /**< see ZSTD_strategy definition above */ 1129eda14cbcSMatt Macy } ZSTD_compressionParameters; 1130eda14cbcSMatt Macy 1131eda14cbcSMatt Macy typedef struct { 1132eda14cbcSMatt Macy int contentSizeFlag; /**< 1: content size will be in frame header (when known) */ 1133eda14cbcSMatt Macy int checksumFlag; /**< 1: generate a 32-bits checksum using XXH64 algorithm at end of frame, for error detection */ 1134eda14cbcSMatt Macy int noDictIDFlag; /**< 1: no dictID will be saved into frame header (dictID is only useful for dictionary compression) */ 1135eda14cbcSMatt Macy } ZSTD_frameParameters; 1136eda14cbcSMatt Macy 1137eda14cbcSMatt Macy typedef struct { 1138eda14cbcSMatt Macy ZSTD_compressionParameters cParams; 1139eda14cbcSMatt Macy ZSTD_frameParameters fParams; 1140eda14cbcSMatt Macy } ZSTD_parameters; 1141eda14cbcSMatt Macy 1142eda14cbcSMatt Macy typedef enum { 1143eda14cbcSMatt Macy ZSTD_dct_auto = 0, /* dictionary is "full" when starting with ZSTD_MAGIC_DICTIONARY, otherwise it is "rawContent" */ 1144eda14cbcSMatt Macy ZSTD_dct_rawContent = 1, /* ensures dictionary is always loaded as rawContent, even if it starts with ZSTD_MAGIC_DICTIONARY */ 1145eda14cbcSMatt Macy ZSTD_dct_fullDict = 2 /* refuses to load a dictionary if it does not respect Zstandard's specification, starting with ZSTD_MAGIC_DICTIONARY */ 1146eda14cbcSMatt Macy } ZSTD_dictContentType_e; 1147eda14cbcSMatt Macy 1148eda14cbcSMatt Macy typedef enum { 1149eda14cbcSMatt Macy ZSTD_dlm_byCopy = 0, /**< Copy dictionary content internally */ 1150eda14cbcSMatt Macy ZSTD_dlm_byRef = 1 /**< Reference dictionary content -- the dictionary buffer must outlive its users. */ 1151eda14cbcSMatt Macy } ZSTD_dictLoadMethod_e; 1152eda14cbcSMatt Macy 1153eda14cbcSMatt Macy typedef enum { 1154eda14cbcSMatt Macy ZSTD_f_zstd1 = 0, /* zstd frame format, specified in zstd_compression_format.md (default) */ 1155eda14cbcSMatt Macy ZSTD_f_zstd1_magicless = 1 /* Variant of zstd frame format, without initial 4-bytes magic number. 1156eda14cbcSMatt Macy * Useful to save 4 bytes per generated frame. 1157eda14cbcSMatt Macy * Decoder cannot recognise automatically this format, requiring this instruction. */ 1158eda14cbcSMatt Macy } ZSTD_format_e; 1159eda14cbcSMatt Macy 1160eda14cbcSMatt Macy typedef enum { 1161eda14cbcSMatt Macy /* Note: this enum and the behavior it controls are effectively internal 1162eda14cbcSMatt Macy * implementation details of the compressor. They are expected to continue 1163eda14cbcSMatt Macy * to evolve and should be considered only in the context of extremely 1164eda14cbcSMatt Macy * advanced performance tuning. 1165eda14cbcSMatt Macy * 1166eda14cbcSMatt Macy * Zstd currently supports the use of a CDict in three ways: 1167eda14cbcSMatt Macy * 1168eda14cbcSMatt Macy * - The contents of the CDict can be copied into the working context. This 1169eda14cbcSMatt Macy * means that the compression can search both the dictionary and input 1170eda14cbcSMatt Macy * while operating on a single set of internal tables. This makes 1171eda14cbcSMatt Macy * the compression faster per-byte of input. However, the initial copy of 1172eda14cbcSMatt Macy * the CDict's tables incurs a fixed cost at the beginning of the 1173eda14cbcSMatt Macy * compression. For small compressions (< 8 KB), that copy can dominate 1174eda14cbcSMatt Macy * the cost of the compression. 1175eda14cbcSMatt Macy * 1176eda14cbcSMatt Macy * - The CDict's tables can be used in-place. In this model, compression is 1177eda14cbcSMatt Macy * slower per input byte, because the compressor has to search two sets of 1178eda14cbcSMatt Macy * tables. However, this model incurs no start-up cost (as long as the 1179eda14cbcSMatt Macy * working context's tables can be reused). For small inputs, this can be 1180eda14cbcSMatt Macy * faster than copying the CDict's tables. 1181eda14cbcSMatt Macy * 1182eda14cbcSMatt Macy * - The CDict's tables are not used at all, and instead we use the working 1183eda14cbcSMatt Macy * context alone to reload the dictionary and use params based on the source 1184eda14cbcSMatt Macy * size. See ZSTD_compress_insertDictionary() and ZSTD_compress_usingDict(). 1185eda14cbcSMatt Macy * This method is effective when the dictionary sizes are very small relative 1186eda14cbcSMatt Macy * to the input size, and the input size is fairly large to begin with. 1187eda14cbcSMatt Macy * 1188eda14cbcSMatt Macy * Zstd has a simple internal heuristic that selects which strategy to use 1189eda14cbcSMatt Macy * at the beginning of a compression. However, if experimentation shows that 1190eda14cbcSMatt Macy * Zstd is making poor choices, it is possible to override that choice with 1191eda14cbcSMatt Macy * this enum. 1192eda14cbcSMatt Macy */ 1193eda14cbcSMatt Macy ZSTD_dictDefaultAttach = 0, /* Use the default heuristic. */ 1194eda14cbcSMatt Macy ZSTD_dictForceAttach = 1, /* Never copy the dictionary. */ 1195eda14cbcSMatt Macy ZSTD_dictForceCopy = 2, /* Always copy the dictionary. */ 1196eda14cbcSMatt Macy ZSTD_dictForceLoad = 3 /* Always reload the dictionary */ 1197eda14cbcSMatt Macy } ZSTD_dictAttachPref_e; 1198eda14cbcSMatt Macy 1199eda14cbcSMatt Macy typedef enum { 1200eda14cbcSMatt Macy ZSTD_lcm_auto = 0, /**< Automatically determine the compression mode based on the compression level. 1201eda14cbcSMatt Macy * Negative compression levels will be uncompressed, and positive compression 1202eda14cbcSMatt Macy * levels will be compressed. */ 1203eda14cbcSMatt Macy ZSTD_lcm_huffman = 1, /**< Always attempt Huffman compression. Uncompressed literals will still be 1204eda14cbcSMatt Macy * emitted if Huffman compression is not profitable. */ 1205eda14cbcSMatt Macy ZSTD_lcm_uncompressed = 2 /**< Always emit uncompressed literals. */ 1206eda14cbcSMatt Macy } ZSTD_literalCompressionMode_e; 1207eda14cbcSMatt Macy 1208eda14cbcSMatt Macy 1209eda14cbcSMatt Macy /*************************************** 1210eda14cbcSMatt Macy * Frame size functions 1211eda14cbcSMatt Macy ***************************************/ 1212eda14cbcSMatt Macy 1213eda14cbcSMatt Macy /*! ZSTD_findDecompressedSize() : 1214eda14cbcSMatt Macy * `src` should point to the start of a series of ZSTD encoded and/or skippable frames 1215eda14cbcSMatt Macy * `srcSize` must be the _exact_ size of this series 1216eda14cbcSMatt Macy * (i.e. there should be a frame boundary at `src + srcSize`) 1217eda14cbcSMatt Macy * @return : - decompressed size of all data in all successive frames 1218eda14cbcSMatt Macy * - if the decompressed size cannot be determined: ZSTD_CONTENTSIZE_UNKNOWN 1219eda14cbcSMatt Macy * - if an error occurred: ZSTD_CONTENTSIZE_ERROR 1220eda14cbcSMatt Macy * 1221eda14cbcSMatt Macy * note 1 : decompressed size is an optional field, that may not be present, especially in streaming mode. 1222eda14cbcSMatt Macy * When `return==ZSTD_CONTENTSIZE_UNKNOWN`, data to decompress could be any size. 1223eda14cbcSMatt Macy * In which case, it's necessary to use streaming mode to decompress data. 1224eda14cbcSMatt Macy * note 2 : decompressed size is always present when compression is done with ZSTD_compress() 1225eda14cbcSMatt Macy * note 3 : decompressed size can be very large (64-bits value), 1226eda14cbcSMatt Macy * potentially larger than what local system can handle as a single memory segment. 1227eda14cbcSMatt Macy * In which case, it's necessary to use streaming mode to decompress data. 1228eda14cbcSMatt Macy * note 4 : If source is untrusted, decompressed size could be wrong or intentionally modified. 1229eda14cbcSMatt Macy * Always ensure result fits within application's authorized limits. 1230eda14cbcSMatt Macy * Each application can set its own limits. 1231eda14cbcSMatt Macy * note 5 : ZSTD_findDecompressedSize handles multiple frames, and so it must traverse the input to 1232eda14cbcSMatt Macy * read each contained frame header. This is fast as most of the data is skipped, 1233eda14cbcSMatt Macy * however it does mean that all frame data must be present and valid. */ 1234eda14cbcSMatt Macy ZSTDLIB_API unsigned long long ZSTD_findDecompressedSize(const void* src, size_t srcSize); 1235eda14cbcSMatt Macy 1236eda14cbcSMatt Macy /*! ZSTD_decompressBound() : 1237eda14cbcSMatt Macy * `src` should point to the start of a series of ZSTD encoded and/or skippable frames 1238eda14cbcSMatt Macy * `srcSize` must be the _exact_ size of this series 1239eda14cbcSMatt Macy * (i.e. there should be a frame boundary at `src + srcSize`) 1240eda14cbcSMatt Macy * @return : - upper-bound for the decompressed size of all data in all successive frames 1241eda14cbcSMatt Macy * - if an error occured: ZSTD_CONTENTSIZE_ERROR 1242eda14cbcSMatt Macy * 1243eda14cbcSMatt Macy * note 1 : an error can occur if `src` contains an invalid or incorrectly formatted frame. 1244eda14cbcSMatt Macy * note 2 : the upper-bound is exact when the decompressed size field is available in every ZSTD encoded frame of `src`. 1245eda14cbcSMatt Macy * in this case, `ZSTD_findDecompressedSize` and `ZSTD_decompressBound` return the same value. 1246eda14cbcSMatt Macy * note 3 : when the decompressed size field isn't available, the upper-bound for that frame is calculated by: 1247eda14cbcSMatt Macy * upper-bound = # blocks * min(128 KB, Window_Size) 1248eda14cbcSMatt Macy */ 1249eda14cbcSMatt Macy ZSTDLIB_API unsigned long long ZSTD_decompressBound(const void* src, size_t srcSize); 1250eda14cbcSMatt Macy 1251eda14cbcSMatt Macy /*! ZSTD_frameHeaderSize() : 1252eda14cbcSMatt Macy * srcSize must be >= ZSTD_FRAMEHEADERSIZE_PREFIX. 1253eda14cbcSMatt Macy * @return : size of the Frame Header, 1254eda14cbcSMatt Macy * or an error code (if srcSize is too small) */ 1255eda14cbcSMatt Macy ZSTDLIB_API size_t ZSTD_frameHeaderSize(const void* src, size_t srcSize); 1256eda14cbcSMatt Macy 1257eda14cbcSMatt Macy /*! ZSTD_getSequences() : 1258eda14cbcSMatt Macy * Extract sequences from the sequence store 1259eda14cbcSMatt Macy * zc can be used to insert custom compression params. 1260eda14cbcSMatt Macy * This function invokes ZSTD_compress2 1261eda14cbcSMatt Macy * @return : number of sequences extracted 1262eda14cbcSMatt Macy */ 1263eda14cbcSMatt Macy ZSTDLIB_API size_t ZSTD_getSequences(ZSTD_CCtx* zc, ZSTD_Sequence* outSeqs, 1264eda14cbcSMatt Macy size_t outSeqsSize, const void* src, size_t srcSize); 1265eda14cbcSMatt Macy 1266eda14cbcSMatt Macy 1267eda14cbcSMatt Macy /*************************************** 1268eda14cbcSMatt Macy * Memory management 1269eda14cbcSMatt Macy ***************************************/ 1270eda14cbcSMatt Macy 1271eda14cbcSMatt Macy /*! ZSTD_estimate*() : 1272eda14cbcSMatt Macy * These functions make it possible to estimate memory usage 1273eda14cbcSMatt Macy * of a future {D,C}Ctx, before its creation. 1274eda14cbcSMatt Macy * 1275eda14cbcSMatt Macy * ZSTD_estimateCCtxSize() will provide a memory budget large enough 1276eda14cbcSMatt Macy * for any compression level up to selected one. 1277eda14cbcSMatt Macy * Note : Unlike ZSTD_estimateCStreamSize*(), this estimate 1278eda14cbcSMatt Macy * does not include space for a window buffer. 1279eda14cbcSMatt Macy * Therefore, the estimation is only guaranteed for single-shot compressions, not streaming. 1280eda14cbcSMatt Macy * The estimate will assume the input may be arbitrarily large, 1281eda14cbcSMatt Macy * which is the worst case. 1282eda14cbcSMatt Macy * 1283eda14cbcSMatt Macy * When srcSize can be bound by a known and rather "small" value, 1284eda14cbcSMatt Macy * this fact can be used to provide a tighter estimation 1285eda14cbcSMatt Macy * because the CCtx compression context will need less memory. 1286eda14cbcSMatt Macy * This tighter estimation can be provided by more advanced functions 1287eda14cbcSMatt Macy * ZSTD_estimateCCtxSize_usingCParams(), which can be used in tandem with ZSTD_getCParams(), 1288eda14cbcSMatt Macy * and ZSTD_estimateCCtxSize_usingCCtxParams(), which can be used in tandem with ZSTD_CCtxParams_setParameter(). 1289eda14cbcSMatt Macy * Both can be used to estimate memory using custom compression parameters and arbitrary srcSize limits. 1290eda14cbcSMatt Macy * 1291eda14cbcSMatt Macy * Note 2 : only single-threaded compression is supported. 1292eda14cbcSMatt Macy * ZSTD_estimateCCtxSize_usingCCtxParams() will return an error code if ZSTD_c_nbWorkers is >= 1. 1293eda14cbcSMatt Macy */ 1294eda14cbcSMatt Macy ZSTDLIB_API size_t ZSTD_estimateCCtxSize(int compressionLevel); 1295eda14cbcSMatt Macy ZSTDLIB_API size_t ZSTD_estimateCCtxSize_usingCParams(ZSTD_compressionParameters cParams); 1296eda14cbcSMatt Macy ZSTDLIB_API size_t ZSTD_estimateCCtxSize_usingCCtxParams(const ZSTD_CCtx_params* params); 1297eda14cbcSMatt Macy ZSTDLIB_API size_t ZSTD_estimateDCtxSize(void); 1298eda14cbcSMatt Macy 1299eda14cbcSMatt Macy /*! ZSTD_estimateCStreamSize() : 1300eda14cbcSMatt Macy * ZSTD_estimateCStreamSize() will provide a budget large enough for any compression level up to selected one. 1301eda14cbcSMatt Macy * It will also consider src size to be arbitrarily "large", which is worst case. 1302eda14cbcSMatt Macy * If srcSize is known to always be small, ZSTD_estimateCStreamSize_usingCParams() can provide a tighter estimation. 1303eda14cbcSMatt Macy * ZSTD_estimateCStreamSize_usingCParams() can be used in tandem with ZSTD_getCParams() to create cParams from compressionLevel. 1304eda14cbcSMatt Macy * ZSTD_estimateCStreamSize_usingCCtxParams() can be used in tandem with ZSTD_CCtxParams_setParameter(). Only single-threaded compression is supported. This function will return an error code if ZSTD_c_nbWorkers is >= 1. 1305eda14cbcSMatt Macy * Note : CStream size estimation is only correct for single-threaded compression. 1306eda14cbcSMatt Macy * ZSTD_DStream memory budget depends on window Size. 1307eda14cbcSMatt Macy * This information can be passed manually, using ZSTD_estimateDStreamSize, 1308eda14cbcSMatt Macy * or deducted from a valid frame Header, using ZSTD_estimateDStreamSize_fromFrame(); 1309eda14cbcSMatt Macy * Note : if streaming is init with function ZSTD_init?Stream_usingDict(), 1310eda14cbcSMatt Macy * an internal ?Dict will be created, which additional size is not estimated here. 1311eda14cbcSMatt Macy * In this case, get total size by adding ZSTD_estimate?DictSize */ 1312eda14cbcSMatt Macy ZSTDLIB_API size_t ZSTD_estimateCStreamSize(int compressionLevel); 1313eda14cbcSMatt Macy ZSTDLIB_API size_t ZSTD_estimateCStreamSize_usingCParams(ZSTD_compressionParameters cParams); 1314eda14cbcSMatt Macy ZSTDLIB_API size_t ZSTD_estimateCStreamSize_usingCCtxParams(const ZSTD_CCtx_params* params); 1315eda14cbcSMatt Macy ZSTDLIB_API size_t ZSTD_estimateDStreamSize(size_t windowSize); 1316eda14cbcSMatt Macy ZSTDLIB_API size_t ZSTD_estimateDStreamSize_fromFrame(const void* src, size_t srcSize); 1317eda14cbcSMatt Macy 1318eda14cbcSMatt Macy /*! ZSTD_estimate?DictSize() : 1319eda14cbcSMatt Macy * ZSTD_estimateCDictSize() will bet that src size is relatively "small", and content is copied, like ZSTD_createCDict(). 1320eda14cbcSMatt Macy * ZSTD_estimateCDictSize_advanced() makes it possible to control compression parameters precisely, like ZSTD_createCDict_advanced(). 1321eda14cbcSMatt Macy * Note : dictionaries created by reference (`ZSTD_dlm_byRef`) are logically smaller. 1322eda14cbcSMatt Macy */ 1323eda14cbcSMatt Macy ZSTDLIB_API size_t ZSTD_estimateCDictSize(size_t dictSize, int compressionLevel); 1324eda14cbcSMatt Macy ZSTDLIB_API size_t ZSTD_estimateCDictSize_advanced(size_t dictSize, ZSTD_compressionParameters cParams, ZSTD_dictLoadMethod_e dictLoadMethod); 1325eda14cbcSMatt Macy ZSTDLIB_API size_t ZSTD_estimateDDictSize(size_t dictSize, ZSTD_dictLoadMethod_e dictLoadMethod); 1326eda14cbcSMatt Macy 1327eda14cbcSMatt Macy /*! ZSTD_initStatic*() : 1328eda14cbcSMatt Macy * Initialize an object using a pre-allocated fixed-size buffer. 1329eda14cbcSMatt Macy * workspace: The memory area to emplace the object into. 1330eda14cbcSMatt Macy * Provided pointer *must be 8-bytes aligned*. 1331eda14cbcSMatt Macy * Buffer must outlive object. 1332eda14cbcSMatt Macy * workspaceSize: Use ZSTD_estimate*Size() to determine 1333eda14cbcSMatt Macy * how large workspace must be to support target scenario. 1334eda14cbcSMatt Macy * @return : pointer to object (same address as workspace, just different type), 1335eda14cbcSMatt Macy * or NULL if error (size too small, incorrect alignment, etc.) 1336eda14cbcSMatt Macy * Note : zstd will never resize nor malloc() when using a static buffer. 1337eda14cbcSMatt Macy * If the object requires more memory than available, 1338eda14cbcSMatt Macy * zstd will just error out (typically ZSTD_error_memory_allocation). 1339eda14cbcSMatt Macy * Note 2 : there is no corresponding "free" function. 1340eda14cbcSMatt Macy * Since workspace is allocated externally, it must be freed externally too. 1341eda14cbcSMatt Macy * Note 3 : cParams : use ZSTD_getCParams() to convert a compression level 1342eda14cbcSMatt Macy * into its associated cParams. 1343eda14cbcSMatt Macy * Limitation 1 : currently not compatible with internal dictionary creation, triggered by 1344eda14cbcSMatt Macy * ZSTD_CCtx_loadDictionary(), ZSTD_initCStream_usingDict() or ZSTD_initDStream_usingDict(). 1345eda14cbcSMatt Macy * Limitation 2 : static cctx currently not compatible with multi-threading. 1346eda14cbcSMatt Macy * Limitation 3 : static dctx is incompatible with legacy support. 1347eda14cbcSMatt Macy */ 1348eda14cbcSMatt Macy ZSTDLIB_API ZSTD_CCtx* ZSTD_initStaticCCtx(void* workspace, size_t workspaceSize); 1349eda14cbcSMatt Macy ZSTDLIB_API ZSTD_CStream* ZSTD_initStaticCStream(void* workspace, size_t workspaceSize); /**< same as ZSTD_initStaticCCtx() */ 1350eda14cbcSMatt Macy 1351eda14cbcSMatt Macy ZSTDLIB_API ZSTD_DCtx* ZSTD_initStaticDCtx(void* workspace, size_t workspaceSize); 1352eda14cbcSMatt Macy ZSTDLIB_API ZSTD_DStream* ZSTD_initStaticDStream(void* workspace, size_t workspaceSize); /**< same as ZSTD_initStaticDCtx() */ 1353eda14cbcSMatt Macy 1354eda14cbcSMatt Macy ZSTDLIB_API const ZSTD_CDict* ZSTD_initStaticCDict( 1355eda14cbcSMatt Macy void* workspace, size_t workspaceSize, 1356eda14cbcSMatt Macy const void* dict, size_t dictSize, 1357eda14cbcSMatt Macy ZSTD_dictLoadMethod_e dictLoadMethod, 1358eda14cbcSMatt Macy ZSTD_dictContentType_e dictContentType, 1359eda14cbcSMatt Macy ZSTD_compressionParameters cParams); 1360eda14cbcSMatt Macy 1361eda14cbcSMatt Macy ZSTDLIB_API const ZSTD_DDict* ZSTD_initStaticDDict( 1362eda14cbcSMatt Macy void* workspace, size_t workspaceSize, 1363eda14cbcSMatt Macy const void* dict, size_t dictSize, 1364eda14cbcSMatt Macy ZSTD_dictLoadMethod_e dictLoadMethod, 1365eda14cbcSMatt Macy ZSTD_dictContentType_e dictContentType); 1366eda14cbcSMatt Macy 1367eda14cbcSMatt Macy 1368eda14cbcSMatt Macy /*! Custom memory allocation : 1369eda14cbcSMatt Macy * These prototypes make it possible to pass your own allocation/free functions. 1370eda14cbcSMatt Macy * ZSTD_customMem is provided at creation time, using ZSTD_create*_advanced() variants listed below. 1371eda14cbcSMatt Macy * All allocation/free operations will be completed using these custom variants instead of regular <stdlib.h> ones. 1372eda14cbcSMatt Macy */ 1373eda14cbcSMatt Macy typedef void* (*ZSTD_allocFunction) (void* opaque, size_t size); 1374eda14cbcSMatt Macy typedef void (*ZSTD_freeFunction) (void* opaque, void* address); 1375eda14cbcSMatt Macy typedef struct { ZSTD_allocFunction customAlloc; ZSTD_freeFunction customFree; void* opaque; } ZSTD_customMem; 1376eda14cbcSMatt Macy static ZSTD_customMem const ZSTD_defaultCMem = { NULL, NULL, NULL }; /**< this constant defers to stdlib's functions */ 1377eda14cbcSMatt Macy 1378eda14cbcSMatt Macy ZSTDLIB_API ZSTD_CCtx* ZSTD_createCCtx_advanced(ZSTD_customMem customMem); 1379eda14cbcSMatt Macy ZSTDLIB_API ZSTD_CStream* ZSTD_createCStream_advanced(ZSTD_customMem customMem); 1380eda14cbcSMatt Macy ZSTDLIB_API ZSTD_DCtx* ZSTD_createDCtx_advanced(ZSTD_customMem customMem); 1381eda14cbcSMatt Macy ZSTDLIB_API ZSTD_DStream* ZSTD_createDStream_advanced(ZSTD_customMem customMem); 1382eda14cbcSMatt Macy 1383eda14cbcSMatt Macy ZSTDLIB_API ZSTD_CDict* ZSTD_createCDict_advanced(const void* dict, size_t dictSize, 1384eda14cbcSMatt Macy ZSTD_dictLoadMethod_e dictLoadMethod, 1385eda14cbcSMatt Macy ZSTD_dictContentType_e dictContentType, 1386eda14cbcSMatt Macy ZSTD_compressionParameters cParams, 1387eda14cbcSMatt Macy ZSTD_customMem customMem); 1388eda14cbcSMatt Macy 1389eda14cbcSMatt Macy ZSTDLIB_API ZSTD_DDict* ZSTD_createDDict_advanced(const void* dict, size_t dictSize, 1390eda14cbcSMatt Macy ZSTD_dictLoadMethod_e dictLoadMethod, 1391eda14cbcSMatt Macy ZSTD_dictContentType_e dictContentType, 1392eda14cbcSMatt Macy ZSTD_customMem customMem); 1393eda14cbcSMatt Macy 1394eda14cbcSMatt Macy 1395eda14cbcSMatt Macy 1396eda14cbcSMatt Macy /*************************************** 1397eda14cbcSMatt Macy * Advanced compression functions 1398eda14cbcSMatt Macy ***************************************/ 1399eda14cbcSMatt Macy 1400eda14cbcSMatt Macy /*! ZSTD_createCDict_byReference() : 1401eda14cbcSMatt Macy * Create a digested dictionary for compression 1402eda14cbcSMatt Macy * Dictionary content is just referenced, not duplicated. 1403eda14cbcSMatt Macy * As a consequence, `dictBuffer` **must** outlive CDict, 1404eda14cbcSMatt Macy * and its content must remain unmodified throughout the lifetime of CDict. 1405eda14cbcSMatt Macy * note: equivalent to ZSTD_createCDict_advanced(), with dictLoadMethod==ZSTD_dlm_byRef */ 1406eda14cbcSMatt Macy ZSTDLIB_API ZSTD_CDict* ZSTD_createCDict_byReference(const void* dictBuffer, size_t dictSize, int compressionLevel); 1407eda14cbcSMatt Macy 1408eda14cbcSMatt Macy /*! ZSTD_getCParams() : 1409eda14cbcSMatt Macy * @return ZSTD_compressionParameters structure for a selected compression level and estimated srcSize. 1410eda14cbcSMatt Macy * `estimatedSrcSize` value is optional, select 0 if not known */ 1411eda14cbcSMatt Macy ZSTDLIB_API ZSTD_compressionParameters ZSTD_getCParams(int compressionLevel, unsigned long long estimatedSrcSize, size_t dictSize); 1412eda14cbcSMatt Macy 1413eda14cbcSMatt Macy /*! ZSTD_getParams() : 1414eda14cbcSMatt Macy * same as ZSTD_getCParams(), but @return a full `ZSTD_parameters` object instead of sub-component `ZSTD_compressionParameters`. 1415eda14cbcSMatt Macy * All fields of `ZSTD_frameParameters` are set to default : contentSize=1, checksum=0, noDictID=0 */ 1416eda14cbcSMatt Macy ZSTDLIB_API ZSTD_parameters ZSTD_getParams(int compressionLevel, unsigned long long estimatedSrcSize, size_t dictSize); 1417eda14cbcSMatt Macy 1418eda14cbcSMatt Macy /*! ZSTD_checkCParams() : 1419eda14cbcSMatt Macy * Ensure param values remain within authorized range. 1420eda14cbcSMatt Macy * @return 0 on success, or an error code (can be checked with ZSTD_isError()) */ 1421eda14cbcSMatt Macy ZSTDLIB_API size_t ZSTD_checkCParams(ZSTD_compressionParameters params); 1422eda14cbcSMatt Macy 1423eda14cbcSMatt Macy /*! ZSTD_adjustCParams() : 1424eda14cbcSMatt Macy * optimize params for a given `srcSize` and `dictSize`. 1425eda14cbcSMatt Macy * `srcSize` can be unknown, in which case use ZSTD_CONTENTSIZE_UNKNOWN. 1426eda14cbcSMatt Macy * `dictSize` must be `0` when there is no dictionary. 1427eda14cbcSMatt Macy * cPar can be invalid : all parameters will be clamped within valid range in the @return struct. 1428eda14cbcSMatt Macy * This function never fails (wide contract) */ 1429eda14cbcSMatt Macy ZSTDLIB_API ZSTD_compressionParameters ZSTD_adjustCParams(ZSTD_compressionParameters cPar, unsigned long long srcSize, size_t dictSize); 1430eda14cbcSMatt Macy 1431eda14cbcSMatt Macy /*! ZSTD_compress_advanced() : 1432eda14cbcSMatt Macy * Note : this function is now DEPRECATED. 1433eda14cbcSMatt Macy * It can be replaced by ZSTD_compress2(), in combination with ZSTD_CCtx_setParameter() and other parameter setters. 1434eda14cbcSMatt Macy * This prototype will be marked as deprecated and generate compilation warning on reaching v1.5.x */ 1435eda14cbcSMatt Macy ZSTDLIB_API size_t ZSTD_compress_advanced(ZSTD_CCtx* cctx, 1436eda14cbcSMatt Macy void* dst, size_t dstCapacity, 1437eda14cbcSMatt Macy const void* src, size_t srcSize, 1438eda14cbcSMatt Macy const void* dict,size_t dictSize, 1439eda14cbcSMatt Macy ZSTD_parameters params); 1440eda14cbcSMatt Macy 1441eda14cbcSMatt Macy /*! ZSTD_compress_usingCDict_advanced() : 1442eda14cbcSMatt Macy * Note : this function is now REDUNDANT. 1443eda14cbcSMatt Macy * It can be replaced by ZSTD_compress2(), in combination with ZSTD_CCtx_loadDictionary() and other parameter setters. 1444eda14cbcSMatt Macy * This prototype will be marked as deprecated and generate compilation warning in some future version */ 1445eda14cbcSMatt Macy ZSTDLIB_API size_t ZSTD_compress_usingCDict_advanced(ZSTD_CCtx* cctx, 1446eda14cbcSMatt Macy void* dst, size_t dstCapacity, 1447eda14cbcSMatt Macy const void* src, size_t srcSize, 1448eda14cbcSMatt Macy const ZSTD_CDict* cdict, 1449eda14cbcSMatt Macy ZSTD_frameParameters fParams); 1450eda14cbcSMatt Macy 1451eda14cbcSMatt Macy 1452eda14cbcSMatt Macy /*! ZSTD_CCtx_loadDictionary_byReference() : 1453eda14cbcSMatt Macy * Same as ZSTD_CCtx_loadDictionary(), but dictionary content is referenced, instead of being copied into CCtx. 1454eda14cbcSMatt Macy * It saves some memory, but also requires that `dict` outlives its usage within `cctx` */ 1455eda14cbcSMatt Macy ZSTDLIB_API size_t ZSTD_CCtx_loadDictionary_byReference(ZSTD_CCtx* cctx, const void* dict, size_t dictSize); 1456eda14cbcSMatt Macy 1457eda14cbcSMatt Macy /*! ZSTD_CCtx_loadDictionary_advanced() : 1458eda14cbcSMatt Macy * Same as ZSTD_CCtx_loadDictionary(), but gives finer control over 1459eda14cbcSMatt Macy * how to load the dictionary (by copy ? by reference ?) 1460eda14cbcSMatt Macy * and how to interpret it (automatic ? force raw mode ? full mode only ?) */ 1461eda14cbcSMatt Macy ZSTDLIB_API size_t ZSTD_CCtx_loadDictionary_advanced(ZSTD_CCtx* cctx, const void* dict, size_t dictSize, ZSTD_dictLoadMethod_e dictLoadMethod, ZSTD_dictContentType_e dictContentType); 1462eda14cbcSMatt Macy 1463eda14cbcSMatt Macy /*! ZSTD_CCtx_refPrefix_advanced() : 1464eda14cbcSMatt Macy * Same as ZSTD_CCtx_refPrefix(), but gives finer control over 1465eda14cbcSMatt Macy * how to interpret prefix content (automatic ? force raw mode (default) ? full mode only ?) */ 1466eda14cbcSMatt Macy ZSTDLIB_API size_t ZSTD_CCtx_refPrefix_advanced(ZSTD_CCtx* cctx, const void* prefix, size_t prefixSize, ZSTD_dictContentType_e dictContentType); 1467eda14cbcSMatt Macy 1468eda14cbcSMatt Macy /* === experimental parameters === */ 1469eda14cbcSMatt Macy /* these parameters can be used with ZSTD_setParameter() 1470eda14cbcSMatt Macy * they are not guaranteed to remain supported in the future */ 1471eda14cbcSMatt Macy 1472eda14cbcSMatt Macy /* Enables rsyncable mode, 1473eda14cbcSMatt Macy * which makes compressed files more rsync friendly 1474eda14cbcSMatt Macy * by adding periodic synchronization points to the compressed data. 1475eda14cbcSMatt Macy * The target average block size is ZSTD_c_jobSize / 2. 1476eda14cbcSMatt Macy * It's possible to modify the job size to increase or decrease 1477eda14cbcSMatt Macy * the granularity of the synchronization point. 1478eda14cbcSMatt Macy * Once the jobSize is smaller than the window size, 1479eda14cbcSMatt Macy * it will result in compression ratio degradation. 1480eda14cbcSMatt Macy * NOTE 1: rsyncable mode only works when multithreading is enabled. 1481eda14cbcSMatt Macy * NOTE 2: rsyncable performs poorly in combination with long range mode, 1482eda14cbcSMatt Macy * since it will decrease the effectiveness of synchronization points, 1483eda14cbcSMatt Macy * though mileage may vary. 1484eda14cbcSMatt Macy * NOTE 3: Rsyncable mode limits maximum compression speed to ~400 MB/s. 1485eda14cbcSMatt Macy * If the selected compression level is already running significantly slower, 1486eda14cbcSMatt Macy * the overall speed won't be significantly impacted. 1487eda14cbcSMatt Macy */ 1488eda14cbcSMatt Macy #define ZSTD_c_rsyncable ZSTD_c_experimentalParam1 1489eda14cbcSMatt Macy 1490eda14cbcSMatt Macy /* Select a compression format. 1491eda14cbcSMatt Macy * The value must be of type ZSTD_format_e. 1492eda14cbcSMatt Macy * See ZSTD_format_e enum definition for details */ 1493eda14cbcSMatt Macy #define ZSTD_c_format ZSTD_c_experimentalParam2 1494eda14cbcSMatt Macy 1495eda14cbcSMatt Macy /* Force back-reference distances to remain < windowSize, 1496eda14cbcSMatt Macy * even when referencing into Dictionary content (default:0) */ 1497eda14cbcSMatt Macy #define ZSTD_c_forceMaxWindow ZSTD_c_experimentalParam3 1498eda14cbcSMatt Macy 1499eda14cbcSMatt Macy /* Controls whether the contents of a CDict 1500eda14cbcSMatt Macy * are used in place, or copied into the working context. 1501eda14cbcSMatt Macy * Accepts values from the ZSTD_dictAttachPref_e enum. 1502eda14cbcSMatt Macy * See the comments on that enum for an explanation of the feature. */ 1503eda14cbcSMatt Macy #define ZSTD_c_forceAttachDict ZSTD_c_experimentalParam4 1504eda14cbcSMatt Macy 1505eda14cbcSMatt Macy /* Controls how the literals are compressed (default is auto). 1506eda14cbcSMatt Macy * The value must be of type ZSTD_literalCompressionMode_e. 1507eda14cbcSMatt Macy * See ZSTD_literalCompressionMode_t enum definition for details. 1508eda14cbcSMatt Macy */ 1509eda14cbcSMatt Macy #define ZSTD_c_literalCompressionMode ZSTD_c_experimentalParam5 1510eda14cbcSMatt Macy 1511eda14cbcSMatt Macy /* Tries to fit compressed block size to be around targetCBlockSize. 1512eda14cbcSMatt Macy * No target when targetCBlockSize == 0. 1513eda14cbcSMatt Macy * There is no guarantee on compressed block size (default:0) */ 1514eda14cbcSMatt Macy #define ZSTD_c_targetCBlockSize ZSTD_c_experimentalParam6 1515eda14cbcSMatt Macy 1516eda14cbcSMatt Macy /* User's best guess of source size. 1517eda14cbcSMatt Macy * Hint is not valid when srcSizeHint == 0. 1518eda14cbcSMatt Macy * There is no guarantee that hint is close to actual source size, 1519eda14cbcSMatt Macy * but compression ratio may regress significantly if guess considerably underestimates */ 1520eda14cbcSMatt Macy #define ZSTD_c_srcSizeHint ZSTD_c_experimentalParam7 1521eda14cbcSMatt Macy 1522eda14cbcSMatt Macy /*! ZSTD_CCtx_getParameter() : 1523eda14cbcSMatt Macy * Get the requested compression parameter value, selected by enum ZSTD_cParameter, 1524eda14cbcSMatt Macy * and store it into int* value. 1525eda14cbcSMatt Macy * @return : 0, or an error code (which can be tested with ZSTD_isError()). 1526eda14cbcSMatt Macy */ 1527eda14cbcSMatt Macy ZSTDLIB_API size_t ZSTD_CCtx_getParameter(ZSTD_CCtx* cctx, ZSTD_cParameter param, int* value); 1528eda14cbcSMatt Macy 1529eda14cbcSMatt Macy 1530eda14cbcSMatt Macy /*! ZSTD_CCtx_params : 1531eda14cbcSMatt Macy * Quick howto : 1532eda14cbcSMatt Macy * - ZSTD_createCCtxParams() : Create a ZSTD_CCtx_params structure 1533eda14cbcSMatt Macy * - ZSTD_CCtxParams_setParameter() : Push parameters one by one into 1534eda14cbcSMatt Macy * an existing ZSTD_CCtx_params structure. 1535eda14cbcSMatt Macy * This is similar to 1536eda14cbcSMatt Macy * ZSTD_CCtx_setParameter(). 1537eda14cbcSMatt Macy * - ZSTD_CCtx_setParametersUsingCCtxParams() : Apply parameters to 1538eda14cbcSMatt Macy * an existing CCtx. 1539eda14cbcSMatt Macy * These parameters will be applied to 1540eda14cbcSMatt Macy * all subsequent frames. 1541eda14cbcSMatt Macy * - ZSTD_compressStream2() : Do compression using the CCtx. 1542eda14cbcSMatt Macy * - ZSTD_freeCCtxParams() : Free the memory. 1543eda14cbcSMatt Macy * 1544eda14cbcSMatt Macy * This can be used with ZSTD_estimateCCtxSize_advanced_usingCCtxParams() 1545eda14cbcSMatt Macy * for static allocation of CCtx for single-threaded compression. 1546eda14cbcSMatt Macy */ 1547eda14cbcSMatt Macy ZSTDLIB_API ZSTD_CCtx_params* ZSTD_createCCtxParams(void); 1548eda14cbcSMatt Macy ZSTDLIB_API size_t ZSTD_freeCCtxParams(ZSTD_CCtx_params* params); 1549eda14cbcSMatt Macy 1550eda14cbcSMatt Macy /*! ZSTD_CCtxParams_reset() : 1551eda14cbcSMatt Macy * Reset params to default values. 1552eda14cbcSMatt Macy */ 1553eda14cbcSMatt Macy ZSTDLIB_API size_t ZSTD_CCtxParams_reset(ZSTD_CCtx_params* params); 1554eda14cbcSMatt Macy 1555eda14cbcSMatt Macy /*! ZSTD_CCtxParams_init() : 1556eda14cbcSMatt Macy * Initializes the compression parameters of cctxParams according to 1557eda14cbcSMatt Macy * compression level. All other parameters are reset to their default values. 1558eda14cbcSMatt Macy */ 1559eda14cbcSMatt Macy ZSTDLIB_API size_t ZSTD_CCtxParams_init(ZSTD_CCtx_params* cctxParams, int compressionLevel); 1560eda14cbcSMatt Macy 1561eda14cbcSMatt Macy /*! ZSTD_CCtxParams_init_advanced() : 1562eda14cbcSMatt Macy * Initializes the compression and frame parameters of cctxParams according to 1563eda14cbcSMatt Macy * params. All other parameters are reset to their default values. 1564eda14cbcSMatt Macy */ 1565eda14cbcSMatt Macy ZSTDLIB_API size_t ZSTD_CCtxParams_init_advanced(ZSTD_CCtx_params* cctxParams, ZSTD_parameters params); 1566eda14cbcSMatt Macy 1567eda14cbcSMatt Macy /*! ZSTD_CCtxParams_setParameter() : 1568eda14cbcSMatt Macy * Similar to ZSTD_CCtx_setParameter. 1569eda14cbcSMatt Macy * Set one compression parameter, selected by enum ZSTD_cParameter. 1570eda14cbcSMatt Macy * Parameters must be applied to a ZSTD_CCtx using ZSTD_CCtx_setParametersUsingCCtxParams(). 1571eda14cbcSMatt Macy * @result : 0, or an error code (which can be tested with ZSTD_isError()). 1572eda14cbcSMatt Macy */ 1573eda14cbcSMatt Macy ZSTDLIB_API size_t ZSTD_CCtxParams_setParameter(ZSTD_CCtx_params* params, ZSTD_cParameter param, int value); 1574eda14cbcSMatt Macy 1575eda14cbcSMatt Macy /*! ZSTD_CCtxParams_getParameter() : 1576eda14cbcSMatt Macy * Similar to ZSTD_CCtx_getParameter. 1577eda14cbcSMatt Macy * Get the requested value of one compression parameter, selected by enum ZSTD_cParameter. 1578eda14cbcSMatt Macy * @result : 0, or an error code (which can be tested with ZSTD_isError()). 1579eda14cbcSMatt Macy */ 1580eda14cbcSMatt Macy ZSTDLIB_API size_t ZSTD_CCtxParams_getParameter(ZSTD_CCtx_params* params, ZSTD_cParameter param, int* value); 1581eda14cbcSMatt Macy 1582eda14cbcSMatt Macy /*! ZSTD_CCtx_setParametersUsingCCtxParams() : 1583eda14cbcSMatt Macy * Apply a set of ZSTD_CCtx_params to the compression context. 1584eda14cbcSMatt Macy * This can be done even after compression is started, 1585eda14cbcSMatt Macy * if nbWorkers==0, this will have no impact until a new compression is started. 1586eda14cbcSMatt Macy * if nbWorkers>=1, new parameters will be picked up at next job, 1587eda14cbcSMatt Macy * with a few restrictions (windowLog, pledgedSrcSize, nbWorkers, jobSize, and overlapLog are not updated). 1588eda14cbcSMatt Macy */ 1589eda14cbcSMatt Macy ZSTDLIB_API size_t ZSTD_CCtx_setParametersUsingCCtxParams( 1590eda14cbcSMatt Macy ZSTD_CCtx* cctx, const ZSTD_CCtx_params* params); 1591eda14cbcSMatt Macy 1592eda14cbcSMatt Macy /*! ZSTD_compressStream2_simpleArgs() : 1593eda14cbcSMatt Macy * Same as ZSTD_compressStream2(), 1594eda14cbcSMatt Macy * but using only integral types as arguments. 1595eda14cbcSMatt Macy * This variant might be helpful for binders from dynamic languages 1596eda14cbcSMatt Macy * which have troubles handling structures containing memory pointers. 1597eda14cbcSMatt Macy */ 1598eda14cbcSMatt Macy ZSTDLIB_API size_t ZSTD_compressStream2_simpleArgs ( 1599eda14cbcSMatt Macy ZSTD_CCtx* cctx, 1600eda14cbcSMatt Macy void* dst, size_t dstCapacity, size_t* dstPos, 1601eda14cbcSMatt Macy const void* src, size_t srcSize, size_t* srcPos, 1602eda14cbcSMatt Macy ZSTD_EndDirective endOp); 1603eda14cbcSMatt Macy 1604eda14cbcSMatt Macy 1605eda14cbcSMatt Macy /*************************************** 1606eda14cbcSMatt Macy * Advanced decompression functions 1607eda14cbcSMatt Macy ***************************************/ 1608eda14cbcSMatt Macy 1609eda14cbcSMatt Macy /*! ZSTD_isFrame() : 1610eda14cbcSMatt Macy * Tells if the content of `buffer` starts with a valid Frame Identifier. 1611eda14cbcSMatt Macy * Note : Frame Identifier is 4 bytes. If `size < 4`, @return will always be 0. 1612eda14cbcSMatt Macy * Note 2 : Legacy Frame Identifiers are considered valid only if Legacy Support is enabled. 1613eda14cbcSMatt Macy * Note 3 : Skippable Frame Identifiers are considered valid. */ 1614eda14cbcSMatt Macy ZSTDLIB_API unsigned ZSTD_isFrame(const void* buffer, size_t size); 1615eda14cbcSMatt Macy 1616eda14cbcSMatt Macy /*! ZSTD_createDDict_byReference() : 1617eda14cbcSMatt Macy * Create a digested dictionary, ready to start decompression operation without startup delay. 1618eda14cbcSMatt Macy * Dictionary content is referenced, and therefore stays in dictBuffer. 1619eda14cbcSMatt Macy * It is important that dictBuffer outlives DDict, 1620eda14cbcSMatt Macy * it must remain read accessible throughout the lifetime of DDict */ 1621eda14cbcSMatt Macy ZSTDLIB_API ZSTD_DDict* ZSTD_createDDict_byReference(const void* dictBuffer, size_t dictSize); 1622eda14cbcSMatt Macy 1623eda14cbcSMatt Macy /*! ZSTD_DCtx_loadDictionary_byReference() : 1624eda14cbcSMatt Macy * Same as ZSTD_DCtx_loadDictionary(), 1625eda14cbcSMatt Macy * but references `dict` content instead of copying it into `dctx`. 1626eda14cbcSMatt Macy * This saves memory if `dict` remains around., 1627eda14cbcSMatt Macy * However, it's imperative that `dict` remains accessible (and unmodified) while being used, so it must outlive decompression. */ 1628eda14cbcSMatt Macy ZSTDLIB_API size_t ZSTD_DCtx_loadDictionary_byReference(ZSTD_DCtx* dctx, const void* dict, size_t dictSize); 1629eda14cbcSMatt Macy 1630eda14cbcSMatt Macy /*! ZSTD_DCtx_loadDictionary_advanced() : 1631eda14cbcSMatt Macy * Same as ZSTD_DCtx_loadDictionary(), 1632eda14cbcSMatt Macy * but gives direct control over 1633eda14cbcSMatt Macy * how to load the dictionary (by copy ? by reference ?) 1634eda14cbcSMatt Macy * and how to interpret it (automatic ? force raw mode ? full mode only ?). */ 1635eda14cbcSMatt Macy ZSTDLIB_API size_t ZSTD_DCtx_loadDictionary_advanced(ZSTD_DCtx* dctx, const void* dict, size_t dictSize, ZSTD_dictLoadMethod_e dictLoadMethod, ZSTD_dictContentType_e dictContentType); 1636eda14cbcSMatt Macy 1637eda14cbcSMatt Macy /*! ZSTD_DCtx_refPrefix_advanced() : 1638eda14cbcSMatt Macy * Same as ZSTD_DCtx_refPrefix(), but gives finer control over 1639eda14cbcSMatt Macy * how to interpret prefix content (automatic ? force raw mode (default) ? full mode only ?) */ 1640eda14cbcSMatt Macy ZSTDLIB_API size_t ZSTD_DCtx_refPrefix_advanced(ZSTD_DCtx* dctx, const void* prefix, size_t prefixSize, ZSTD_dictContentType_e dictContentType); 1641eda14cbcSMatt Macy 1642eda14cbcSMatt Macy /*! ZSTD_DCtx_setMaxWindowSize() : 1643eda14cbcSMatt Macy * Refuses allocating internal buffers for frames requiring a window size larger than provided limit. 1644eda14cbcSMatt Macy * This protects a decoder context from reserving too much memory for itself (potential attack scenario). 1645eda14cbcSMatt Macy * This parameter is only useful in streaming mode, since no internal buffer is allocated in single-pass mode. 1646eda14cbcSMatt Macy * By default, a decompression context accepts all window sizes <= (1 << ZSTD_WINDOWLOG_LIMIT_DEFAULT) 1647eda14cbcSMatt Macy * @return : 0, or an error code (which can be tested using ZSTD_isError()). 1648eda14cbcSMatt Macy */ 1649eda14cbcSMatt Macy ZSTDLIB_API size_t ZSTD_DCtx_setMaxWindowSize(ZSTD_DCtx* dctx, size_t maxWindowSize); 1650eda14cbcSMatt Macy 1651eda14cbcSMatt Macy /* ZSTD_d_format 1652eda14cbcSMatt Macy * experimental parameter, 1653eda14cbcSMatt Macy * allowing selection between ZSTD_format_e input compression formats 1654eda14cbcSMatt Macy */ 1655eda14cbcSMatt Macy #define ZSTD_d_format ZSTD_d_experimentalParam1 1656eda14cbcSMatt Macy /* ZSTD_d_stableOutBuffer 1657eda14cbcSMatt Macy * Experimental parameter. 1658eda14cbcSMatt Macy * Default is 0 == disabled. Set to 1 to enable. 1659eda14cbcSMatt Macy * 1660eda14cbcSMatt Macy * Tells the decompressor that the ZSTD_outBuffer will ALWAYS be the same 1661eda14cbcSMatt Macy * between calls, except for the modifications that zstd makes to pos (the 1662eda14cbcSMatt Macy * caller must not modify pos). This is checked by the decompressor, and 1663eda14cbcSMatt Macy * decompression will fail if it ever changes. Therefore the ZSTD_outBuffer 1664eda14cbcSMatt Macy * MUST be large enough to fit the entire decompressed frame. This will be 1665eda14cbcSMatt Macy * checked when the frame content size is known. The data in the ZSTD_outBuffer 1666eda14cbcSMatt Macy * in the range [dst, dst + pos) MUST not be modified during decompression 1667eda14cbcSMatt Macy * or you will get data corruption. 1668eda14cbcSMatt Macy * 1669eda14cbcSMatt Macy * When this flags is enabled zstd won't allocate an output buffer, because 1670eda14cbcSMatt Macy * it can write directly to the ZSTD_outBuffer, but it will still allocate 1671eda14cbcSMatt Macy * an input buffer large enough to fit any compressed block. This will also 1672eda14cbcSMatt Macy * avoid the memcpy() from the internal output buffer to the ZSTD_outBuffer. 1673eda14cbcSMatt Macy * If you need to avoid the input buffer allocation use the buffer-less 1674eda14cbcSMatt Macy * streaming API. 1675eda14cbcSMatt Macy * 1676eda14cbcSMatt Macy * NOTE: So long as the ZSTD_outBuffer always points to valid memory, using 1677eda14cbcSMatt Macy * this flag is ALWAYS memory safe, and will never access out-of-bounds 1678eda14cbcSMatt Macy * memory. However, decompression WILL fail if you violate the preconditions. 1679eda14cbcSMatt Macy * 1680eda14cbcSMatt Macy * WARNING: The data in the ZSTD_outBuffer in the range [dst, dst + pos) MUST 1681eda14cbcSMatt Macy * not be modified during decompression or you will get data corruption. This 1682eda14cbcSMatt Macy * is because zstd needs to reference data in the ZSTD_outBuffer to regenerate 1683eda14cbcSMatt Macy * matches. Normally zstd maintains its own buffer for this purpose, but passing 1684eda14cbcSMatt Macy * this flag tells zstd to use the user provided buffer. 1685eda14cbcSMatt Macy */ 1686eda14cbcSMatt Macy #define ZSTD_d_stableOutBuffer ZSTD_d_experimentalParam2 1687eda14cbcSMatt Macy 1688eda14cbcSMatt Macy /*! ZSTD_DCtx_setFormat() : 1689eda14cbcSMatt Macy * Instruct the decoder context about what kind of data to decode next. 1690eda14cbcSMatt Macy * This instruction is mandatory to decode data without a fully-formed header, 1691eda14cbcSMatt Macy * such ZSTD_f_zstd1_magicless for example. 1692eda14cbcSMatt Macy * @return : 0, or an error code (which can be tested using ZSTD_isError()). */ 1693eda14cbcSMatt Macy ZSTDLIB_API size_t ZSTD_DCtx_setFormat(ZSTD_DCtx* dctx, ZSTD_format_e format); 1694eda14cbcSMatt Macy 1695eda14cbcSMatt Macy /*! ZSTD_decompressStream_simpleArgs() : 1696eda14cbcSMatt Macy * Same as ZSTD_decompressStream(), 1697eda14cbcSMatt Macy * but using only integral types as arguments. 1698eda14cbcSMatt Macy * This can be helpful for binders from dynamic languages 1699eda14cbcSMatt Macy * which have troubles handling structures containing memory pointers. 1700eda14cbcSMatt Macy */ 1701eda14cbcSMatt Macy ZSTDLIB_API size_t ZSTD_decompressStream_simpleArgs ( 1702eda14cbcSMatt Macy ZSTD_DCtx* dctx, 1703eda14cbcSMatt Macy void* dst, size_t dstCapacity, size_t* dstPos, 1704eda14cbcSMatt Macy const void* src, size_t srcSize, size_t* srcPos); 1705eda14cbcSMatt Macy 1706eda14cbcSMatt Macy 1707eda14cbcSMatt Macy /******************************************************************** 1708eda14cbcSMatt Macy * Advanced streaming functions 1709eda14cbcSMatt Macy * Warning : most of these functions are now redundant with the Advanced API. 1710eda14cbcSMatt Macy * Once Advanced API reaches "stable" status, 1711eda14cbcSMatt Macy * redundant functions will be deprecated, and then at some point removed. 1712eda14cbcSMatt Macy ********************************************************************/ 1713eda14cbcSMatt Macy 1714eda14cbcSMatt Macy /*===== Advanced Streaming compression functions =====*/ 1715eda14cbcSMatt Macy /**! ZSTD_initCStream_srcSize() : 1716eda14cbcSMatt Macy * This function is deprecated, and equivalent to: 1717eda14cbcSMatt Macy * ZSTD_CCtx_reset(zcs, ZSTD_reset_session_only); 1718eda14cbcSMatt Macy * ZSTD_CCtx_refCDict(zcs, NULL); // clear the dictionary (if any) 1719eda14cbcSMatt Macy * ZSTD_CCtx_setParameter(zcs, ZSTD_c_compressionLevel, compressionLevel); 1720eda14cbcSMatt Macy * ZSTD_CCtx_setPledgedSrcSize(zcs, pledgedSrcSize); 1721eda14cbcSMatt Macy * 1722eda14cbcSMatt Macy * pledgedSrcSize must be correct. If it is not known at init time, use 1723eda14cbcSMatt Macy * ZSTD_CONTENTSIZE_UNKNOWN. Note that, for compatibility with older programs, 1724eda14cbcSMatt Macy * "0" also disables frame content size field. It may be enabled in the future. 1725eda14cbcSMatt Macy * Note : this prototype will be marked as deprecated and generate compilation warnings on reaching v1.5.x 1726eda14cbcSMatt Macy */ 1727eda14cbcSMatt Macy ZSTDLIB_API size_t 1728eda14cbcSMatt Macy ZSTD_initCStream_srcSize(ZSTD_CStream* zcs, 1729eda14cbcSMatt Macy int compressionLevel, 1730eda14cbcSMatt Macy unsigned long long pledgedSrcSize); 1731eda14cbcSMatt Macy 1732eda14cbcSMatt Macy /**! ZSTD_initCStream_usingDict() : 1733eda14cbcSMatt Macy * This function is deprecated, and is equivalent to: 1734eda14cbcSMatt Macy * ZSTD_CCtx_reset(zcs, ZSTD_reset_session_only); 1735eda14cbcSMatt Macy * ZSTD_CCtx_setParameter(zcs, ZSTD_c_compressionLevel, compressionLevel); 1736eda14cbcSMatt Macy * ZSTD_CCtx_loadDictionary(zcs, dict, dictSize); 1737eda14cbcSMatt Macy * 1738eda14cbcSMatt Macy * Creates of an internal CDict (incompatible with static CCtx), except if 1739eda14cbcSMatt Macy * dict == NULL or dictSize < 8, in which case no dict is used. 1740eda14cbcSMatt Macy * Note: dict is loaded with ZSTD_dct_auto (treated as a full zstd dictionary if 1741eda14cbcSMatt Macy * it begins with ZSTD_MAGIC_DICTIONARY, else as raw content) and ZSTD_dlm_byCopy. 1742eda14cbcSMatt Macy * Note : this prototype will be marked as deprecated and generate compilation warnings on reaching v1.5.x 1743eda14cbcSMatt Macy */ 1744eda14cbcSMatt Macy ZSTDLIB_API size_t 1745eda14cbcSMatt Macy ZSTD_initCStream_usingDict(ZSTD_CStream* zcs, 1746eda14cbcSMatt Macy const void* dict, size_t dictSize, 1747eda14cbcSMatt Macy int compressionLevel); 1748eda14cbcSMatt Macy 1749eda14cbcSMatt Macy /**! ZSTD_initCStream_advanced() : 1750eda14cbcSMatt Macy * This function is deprecated, and is approximately equivalent to: 1751eda14cbcSMatt Macy * ZSTD_CCtx_reset(zcs, ZSTD_reset_session_only); 1752eda14cbcSMatt Macy * // Pseudocode: Set each zstd parameter and leave the rest as-is. 1753eda14cbcSMatt Macy * for ((param, value) : params) { 1754eda14cbcSMatt Macy * ZSTD_CCtx_setParameter(zcs, param, value); 1755eda14cbcSMatt Macy * } 1756eda14cbcSMatt Macy * ZSTD_CCtx_setPledgedSrcSize(zcs, pledgedSrcSize); 1757eda14cbcSMatt Macy * ZSTD_CCtx_loadDictionary(zcs, dict, dictSize); 1758eda14cbcSMatt Macy * 1759eda14cbcSMatt Macy * dict is loaded with ZSTD_dct_auto and ZSTD_dlm_byCopy. 1760eda14cbcSMatt Macy * pledgedSrcSize must be correct. 1761eda14cbcSMatt Macy * If srcSize is not known at init time, use value ZSTD_CONTENTSIZE_UNKNOWN. 1762eda14cbcSMatt Macy * Note : this prototype will be marked as deprecated and generate compilation warnings on reaching v1.5.x 1763eda14cbcSMatt Macy */ 1764eda14cbcSMatt Macy ZSTDLIB_API size_t 1765eda14cbcSMatt Macy ZSTD_initCStream_advanced(ZSTD_CStream* zcs, 1766eda14cbcSMatt Macy const void* dict, size_t dictSize, 1767eda14cbcSMatt Macy ZSTD_parameters params, 1768eda14cbcSMatt Macy unsigned long long pledgedSrcSize); 1769eda14cbcSMatt Macy 1770eda14cbcSMatt Macy /**! ZSTD_initCStream_usingCDict() : 1771eda14cbcSMatt Macy * This function is deprecated, and equivalent to: 1772eda14cbcSMatt Macy * ZSTD_CCtx_reset(zcs, ZSTD_reset_session_only); 1773eda14cbcSMatt Macy * ZSTD_CCtx_refCDict(zcs, cdict); 1774eda14cbcSMatt Macy * 1775eda14cbcSMatt Macy * note : cdict will just be referenced, and must outlive compression session 1776eda14cbcSMatt Macy * Note : this prototype will be marked as deprecated and generate compilation warnings on reaching v1.5.x 1777eda14cbcSMatt Macy */ 1778eda14cbcSMatt Macy ZSTDLIB_API size_t ZSTD_initCStream_usingCDict(ZSTD_CStream* zcs, const ZSTD_CDict* cdict); 1779eda14cbcSMatt Macy 1780eda14cbcSMatt Macy /**! ZSTD_initCStream_usingCDict_advanced() : 1781eda14cbcSMatt Macy * This function is DEPRECATED, and is approximately equivalent to: 1782eda14cbcSMatt Macy * ZSTD_CCtx_reset(zcs, ZSTD_reset_session_only); 1783eda14cbcSMatt Macy * // Pseudocode: Set each zstd frame parameter and leave the rest as-is. 1784eda14cbcSMatt Macy * for ((fParam, value) : fParams) { 1785eda14cbcSMatt Macy * ZSTD_CCtx_setParameter(zcs, fParam, value); 1786eda14cbcSMatt Macy * } 1787eda14cbcSMatt Macy * ZSTD_CCtx_setPledgedSrcSize(zcs, pledgedSrcSize); 1788eda14cbcSMatt Macy * ZSTD_CCtx_refCDict(zcs, cdict); 1789eda14cbcSMatt Macy * 1790eda14cbcSMatt Macy * same as ZSTD_initCStream_usingCDict(), with control over frame parameters. 1791eda14cbcSMatt Macy * pledgedSrcSize must be correct. If srcSize is not known at init time, use 1792eda14cbcSMatt Macy * value ZSTD_CONTENTSIZE_UNKNOWN. 1793eda14cbcSMatt Macy * Note : this prototype will be marked as deprecated and generate compilation warnings on reaching v1.5.x 1794eda14cbcSMatt Macy */ 1795eda14cbcSMatt Macy ZSTDLIB_API size_t 1796eda14cbcSMatt Macy ZSTD_initCStream_usingCDict_advanced(ZSTD_CStream* zcs, 1797eda14cbcSMatt Macy const ZSTD_CDict* cdict, 1798eda14cbcSMatt Macy ZSTD_frameParameters fParams, 1799eda14cbcSMatt Macy unsigned long long pledgedSrcSize); 1800eda14cbcSMatt Macy 1801eda14cbcSMatt Macy /*! ZSTD_resetCStream() : 1802eda14cbcSMatt Macy * This function is deprecated, and is equivalent to: 1803eda14cbcSMatt Macy * ZSTD_CCtx_reset(zcs, ZSTD_reset_session_only); 1804eda14cbcSMatt Macy * ZSTD_CCtx_setPledgedSrcSize(zcs, pledgedSrcSize); 1805eda14cbcSMatt Macy * 1806eda14cbcSMatt Macy * start a new frame, using same parameters from previous frame. 1807eda14cbcSMatt Macy * This is typically useful to skip dictionary loading stage, since it will re-use it in-place. 1808eda14cbcSMatt Macy * Note that zcs must be init at least once before using ZSTD_resetCStream(). 1809eda14cbcSMatt Macy * If pledgedSrcSize is not known at reset time, use macro ZSTD_CONTENTSIZE_UNKNOWN. 1810eda14cbcSMatt Macy * If pledgedSrcSize > 0, its value must be correct, as it will be written in header, and controlled at the end. 1811eda14cbcSMatt Macy * For the time being, pledgedSrcSize==0 is interpreted as "srcSize unknown" for compatibility with older programs, 1812eda14cbcSMatt Macy * but it will change to mean "empty" in future version, so use macro ZSTD_CONTENTSIZE_UNKNOWN instead. 1813eda14cbcSMatt Macy * @return : 0, or an error code (which can be tested using ZSTD_isError()) 1814eda14cbcSMatt Macy * Note : this prototype will be marked as deprecated and generate compilation warnings on reaching v1.5.x 1815eda14cbcSMatt Macy */ 1816eda14cbcSMatt Macy ZSTDLIB_API size_t ZSTD_resetCStream(ZSTD_CStream* zcs, unsigned long long pledgedSrcSize); 1817eda14cbcSMatt Macy 1818eda14cbcSMatt Macy 1819eda14cbcSMatt Macy typedef struct { 1820eda14cbcSMatt Macy unsigned long long ingested; /* nb input bytes read and buffered */ 1821eda14cbcSMatt Macy unsigned long long consumed; /* nb input bytes actually compressed */ 1822eda14cbcSMatt Macy unsigned long long produced; /* nb of compressed bytes generated and buffered */ 1823eda14cbcSMatt Macy unsigned long long flushed; /* nb of compressed bytes flushed : not provided; can be tracked from caller side */ 1824eda14cbcSMatt Macy unsigned currentJobID; /* MT only : latest started job nb */ 1825eda14cbcSMatt Macy unsigned nbActiveWorkers; /* MT only : nb of workers actively compressing at probe time */ 1826eda14cbcSMatt Macy } ZSTD_frameProgression; 1827eda14cbcSMatt Macy 1828eda14cbcSMatt Macy /* ZSTD_getFrameProgression() : 1829eda14cbcSMatt Macy * tells how much data has been ingested (read from input) 1830eda14cbcSMatt Macy * consumed (input actually compressed) and produced (output) for current frame. 1831eda14cbcSMatt Macy * Note : (ingested - consumed) is amount of input data buffered internally, not yet compressed. 1832eda14cbcSMatt Macy * Aggregates progression inside active worker threads. 1833eda14cbcSMatt Macy */ 1834eda14cbcSMatt Macy ZSTDLIB_API ZSTD_frameProgression ZSTD_getFrameProgression(const ZSTD_CCtx* cctx); 1835eda14cbcSMatt Macy 1836eda14cbcSMatt Macy /*! ZSTD_toFlushNow() : 1837eda14cbcSMatt Macy * Tell how many bytes are ready to be flushed immediately. 1838eda14cbcSMatt Macy * Useful for multithreading scenarios (nbWorkers >= 1). 1839eda14cbcSMatt Macy * Probe the oldest active job, defined as oldest job not yet entirely flushed, 1840eda14cbcSMatt Macy * and check its output buffer. 1841eda14cbcSMatt Macy * @return : amount of data stored in oldest job and ready to be flushed immediately. 1842eda14cbcSMatt Macy * if @return == 0, it means either : 1843eda14cbcSMatt Macy * + there is no active job (could be checked with ZSTD_frameProgression()), or 1844eda14cbcSMatt Macy * + oldest job is still actively compressing data, 1845eda14cbcSMatt Macy * but everything it has produced has also been flushed so far, 1846eda14cbcSMatt Macy * therefore flush speed is limited by production speed of oldest job 1847eda14cbcSMatt Macy * irrespective of the speed of concurrent (and newer) jobs. 1848eda14cbcSMatt Macy */ 1849eda14cbcSMatt Macy ZSTDLIB_API size_t ZSTD_toFlushNow(ZSTD_CCtx* cctx); 1850eda14cbcSMatt Macy 1851eda14cbcSMatt Macy 1852eda14cbcSMatt Macy /*===== Advanced Streaming decompression functions =====*/ 1853eda14cbcSMatt Macy /** 1854eda14cbcSMatt Macy * This function is deprecated, and is equivalent to: 1855eda14cbcSMatt Macy * 1856eda14cbcSMatt Macy * ZSTD_DCtx_reset(zds, ZSTD_reset_session_only); 1857eda14cbcSMatt Macy * ZSTD_DCtx_loadDictionary(zds, dict, dictSize); 1858eda14cbcSMatt Macy * 1859eda14cbcSMatt Macy * note: no dictionary will be used if dict == NULL or dictSize < 8 1860eda14cbcSMatt Macy * Note : this prototype will be marked as deprecated and generate compilation warnings on reaching v1.5.x 1861eda14cbcSMatt Macy */ 1862eda14cbcSMatt Macy ZSTDLIB_API size_t ZSTD_initDStream_usingDict(ZSTD_DStream* zds, const void* dict, size_t dictSize); 1863eda14cbcSMatt Macy 1864eda14cbcSMatt Macy /** 1865eda14cbcSMatt Macy * This function is deprecated, and is equivalent to: 1866eda14cbcSMatt Macy * 1867eda14cbcSMatt Macy * ZSTD_DCtx_reset(zds, ZSTD_reset_session_only); 1868eda14cbcSMatt Macy * ZSTD_DCtx_refDDict(zds, ddict); 1869eda14cbcSMatt Macy * 1870eda14cbcSMatt Macy * note : ddict is referenced, it must outlive decompression session 1871eda14cbcSMatt Macy * Note : this prototype will be marked as deprecated and generate compilation warnings on reaching v1.5.x 1872eda14cbcSMatt Macy */ 1873eda14cbcSMatt Macy ZSTDLIB_API size_t ZSTD_initDStream_usingDDict(ZSTD_DStream* zds, const ZSTD_DDict* ddict); 1874eda14cbcSMatt Macy 1875eda14cbcSMatt Macy /** 1876eda14cbcSMatt Macy * This function is deprecated, and is equivalent to: 1877eda14cbcSMatt Macy * 1878eda14cbcSMatt Macy * ZSTD_DCtx_reset(zds, ZSTD_reset_session_only); 1879eda14cbcSMatt Macy * 1880eda14cbcSMatt Macy * re-use decompression parameters from previous init; saves dictionary loading 1881eda14cbcSMatt Macy * Note : this prototype will be marked as deprecated and generate compilation warnings on reaching v1.5.x 1882eda14cbcSMatt Macy */ 1883eda14cbcSMatt Macy ZSTDLIB_API size_t ZSTD_resetDStream(ZSTD_DStream* zds); 1884eda14cbcSMatt Macy 1885eda14cbcSMatt Macy 1886eda14cbcSMatt Macy /********************************************************************* 1887eda14cbcSMatt Macy * Buffer-less and synchronous inner streaming functions 1888eda14cbcSMatt Macy * 1889eda14cbcSMatt Macy * This is an advanced API, giving full control over buffer management, for users which need direct control over memory. 1890eda14cbcSMatt Macy * But it's also a complex one, with several restrictions, documented below. 1891eda14cbcSMatt Macy * Prefer normal streaming API for an easier experience. 1892eda14cbcSMatt Macy ********************************************************************* */ 1893eda14cbcSMatt Macy 1894eda14cbcSMatt Macy /** 1895eda14cbcSMatt Macy Buffer-less streaming compression (synchronous mode) 1896eda14cbcSMatt Macy 1897eda14cbcSMatt Macy A ZSTD_CCtx object is required to track streaming operations. 1898eda14cbcSMatt Macy Use ZSTD_createCCtx() / ZSTD_freeCCtx() to manage resource. 1899eda14cbcSMatt Macy ZSTD_CCtx object can be re-used multiple times within successive compression operations. 1900eda14cbcSMatt Macy 1901eda14cbcSMatt Macy Start by initializing a context. 1902eda14cbcSMatt Macy Use ZSTD_compressBegin(), or ZSTD_compressBegin_usingDict() for dictionary compression, 1903eda14cbcSMatt Macy or ZSTD_compressBegin_advanced(), for finer parameter control. 1904eda14cbcSMatt Macy It's also possible to duplicate a reference context which has already been initialized, using ZSTD_copyCCtx() 1905eda14cbcSMatt Macy 1906eda14cbcSMatt Macy Then, consume your input using ZSTD_compressContinue(). 1907eda14cbcSMatt Macy There are some important considerations to keep in mind when using this advanced function : 1908eda14cbcSMatt Macy - ZSTD_compressContinue() has no internal buffer. It uses externally provided buffers only. 1909eda14cbcSMatt Macy - Interface is synchronous : input is consumed entirely and produces 1+ compressed blocks. 1910eda14cbcSMatt Macy - Caller must ensure there is enough space in `dst` to store compressed data under worst case scenario. 1911eda14cbcSMatt Macy Worst case evaluation is provided by ZSTD_compressBound(). 1912eda14cbcSMatt Macy ZSTD_compressContinue() doesn't guarantee recover after a failed compression. 1913eda14cbcSMatt Macy - ZSTD_compressContinue() presumes prior input ***is still accessible and unmodified*** (up to maximum distance size, see WindowLog). 1914eda14cbcSMatt Macy It remembers all previous contiguous blocks, plus one separated memory segment (which can itself consists of multiple contiguous blocks) 1915eda14cbcSMatt Macy - ZSTD_compressContinue() detects that prior input has been overwritten when `src` buffer overlaps. 1916eda14cbcSMatt Macy In which case, it will "discard" the relevant memory section from its history. 1917eda14cbcSMatt Macy 1918eda14cbcSMatt Macy Finish a frame with ZSTD_compressEnd(), which will write the last block(s) and optional checksum. 1919eda14cbcSMatt Macy It's possible to use srcSize==0, in which case, it will write a final empty block to end the frame. 1920eda14cbcSMatt Macy Without last block mark, frames are considered unfinished (hence corrupted) by compliant decoders. 1921eda14cbcSMatt Macy 1922eda14cbcSMatt Macy `ZSTD_CCtx` object can be re-used (ZSTD_compressBegin()) to compress again. 1923eda14cbcSMatt Macy */ 1924eda14cbcSMatt Macy 1925eda14cbcSMatt Macy /*===== Buffer-less streaming compression functions =====*/ 1926eda14cbcSMatt Macy ZSTDLIB_API size_t ZSTD_compressBegin(ZSTD_CCtx* cctx, int compressionLevel); 1927eda14cbcSMatt Macy ZSTDLIB_API size_t ZSTD_compressBegin_usingDict(ZSTD_CCtx* cctx, const void* dict, size_t dictSize, int compressionLevel); 1928eda14cbcSMatt Macy ZSTDLIB_API size_t ZSTD_compressBegin_advanced(ZSTD_CCtx* cctx, const void* dict, size_t dictSize, ZSTD_parameters params, unsigned long long pledgedSrcSize); /**< pledgedSrcSize : If srcSize is not known at init time, use ZSTD_CONTENTSIZE_UNKNOWN */ 1929eda14cbcSMatt Macy ZSTDLIB_API size_t ZSTD_compressBegin_usingCDict(ZSTD_CCtx* cctx, const ZSTD_CDict* cdict); /**< note: fails if cdict==NULL */ 1930eda14cbcSMatt Macy ZSTDLIB_API size_t ZSTD_compressBegin_usingCDict_advanced(ZSTD_CCtx* const cctx, const ZSTD_CDict* const cdict, ZSTD_frameParameters const fParams, unsigned long long const pledgedSrcSize); /* compression parameters are already set within cdict. pledgedSrcSize must be correct. If srcSize is not known, use macro ZSTD_CONTENTSIZE_UNKNOWN */ 1931eda14cbcSMatt Macy ZSTDLIB_API size_t ZSTD_copyCCtx(ZSTD_CCtx* cctx, const ZSTD_CCtx* preparedCCtx, unsigned long long pledgedSrcSize); /**< note: if pledgedSrcSize is not known, use ZSTD_CONTENTSIZE_UNKNOWN */ 1932eda14cbcSMatt Macy 1933eda14cbcSMatt Macy ZSTDLIB_API size_t ZSTD_compressContinue(ZSTD_CCtx* cctx, void* dst, size_t dstCapacity, const void* src, size_t srcSize); 1934eda14cbcSMatt Macy ZSTDLIB_API size_t ZSTD_compressEnd(ZSTD_CCtx* cctx, void* dst, size_t dstCapacity, const void* src, size_t srcSize); 1935eda14cbcSMatt Macy 1936eda14cbcSMatt Macy 1937eda14cbcSMatt Macy /*- 1938eda14cbcSMatt Macy Buffer-less streaming decompression (synchronous mode) 1939eda14cbcSMatt Macy 1940eda14cbcSMatt Macy A ZSTD_DCtx object is required to track streaming operations. 1941eda14cbcSMatt Macy Use ZSTD_createDCtx() / ZSTD_freeDCtx() to manage it. 1942eda14cbcSMatt Macy A ZSTD_DCtx object can be re-used multiple times. 1943eda14cbcSMatt Macy 1944eda14cbcSMatt Macy First typical operation is to retrieve frame parameters, using ZSTD_getFrameHeader(). 1945eda14cbcSMatt Macy Frame header is extracted from the beginning of compressed frame, so providing only the frame's beginning is enough. 1946eda14cbcSMatt Macy Data fragment must be large enough to ensure successful decoding. 1947eda14cbcSMatt Macy `ZSTD_frameHeaderSize_max` bytes is guaranteed to always be large enough. 1948eda14cbcSMatt Macy @result : 0 : successful decoding, the `ZSTD_frameHeader` structure is correctly filled. 1949eda14cbcSMatt Macy >0 : `srcSize` is too small, please provide at least @result bytes on next attempt. 1950eda14cbcSMatt Macy errorCode, which can be tested using ZSTD_isError(). 1951eda14cbcSMatt Macy 1952eda14cbcSMatt Macy It fills a ZSTD_frameHeader structure with important information to correctly decode the frame, 1953eda14cbcSMatt Macy such as the dictionary ID, content size, or maximum back-reference distance (`windowSize`). 1954eda14cbcSMatt Macy Note that these values could be wrong, either because of data corruption, or because a 3rd party deliberately spoofs false information. 1955eda14cbcSMatt Macy As a consequence, check that values remain within valid application range. 1956eda14cbcSMatt Macy For example, do not allocate memory blindly, check that `windowSize` is within expectation. 1957eda14cbcSMatt Macy Each application can set its own limits, depending on local restrictions. 1958eda14cbcSMatt Macy For extended interoperability, it is recommended to support `windowSize` of at least 8 MB. 1959eda14cbcSMatt Macy 1960eda14cbcSMatt Macy ZSTD_decompressContinue() needs previous data blocks during decompression, up to `windowSize` bytes. 1961eda14cbcSMatt Macy ZSTD_decompressContinue() is very sensitive to contiguity, 1962eda14cbcSMatt Macy if 2 blocks don't follow each other, make sure that either the compressor breaks contiguity at the same place, 1963eda14cbcSMatt Macy or that previous contiguous segment is large enough to properly handle maximum back-reference distance. 1964eda14cbcSMatt Macy There are multiple ways to guarantee this condition. 1965eda14cbcSMatt Macy 1966eda14cbcSMatt Macy The most memory efficient way is to use a round buffer of sufficient size. 1967eda14cbcSMatt Macy Sufficient size is determined by invoking ZSTD_decodingBufferSize_min(), 1968eda14cbcSMatt Macy which can @return an error code if required value is too large for current system (in 32-bits mode). 1969eda14cbcSMatt Macy In a round buffer methodology, ZSTD_decompressContinue() decompresses each block next to previous one, 1970eda14cbcSMatt Macy up to the moment there is not enough room left in the buffer to guarantee decoding another full block, 1971eda14cbcSMatt Macy which maximum size is provided in `ZSTD_frameHeader` structure, field `blockSizeMax`. 1972eda14cbcSMatt Macy At which point, decoding can resume from the beginning of the buffer. 1973eda14cbcSMatt Macy Note that already decoded data stored in the buffer should be flushed before being overwritten. 1974eda14cbcSMatt Macy 1975eda14cbcSMatt Macy There are alternatives possible, for example using two or more buffers of size `windowSize` each, though they consume more memory. 1976eda14cbcSMatt Macy 1977eda14cbcSMatt Macy Finally, if you control the compression process, you can also ignore all buffer size rules, 1978eda14cbcSMatt Macy as long as the encoder and decoder progress in "lock-step", 1979eda14cbcSMatt Macy aka use exactly the same buffer sizes, break contiguity at the same place, etc. 1980eda14cbcSMatt Macy 1981eda14cbcSMatt Macy Once buffers are setup, start decompression, with ZSTD_decompressBegin(). 1982eda14cbcSMatt Macy If decompression requires a dictionary, use ZSTD_decompressBegin_usingDict() or ZSTD_decompressBegin_usingDDict(). 1983eda14cbcSMatt Macy 1984eda14cbcSMatt Macy Then use ZSTD_nextSrcSizeToDecompress() and ZSTD_decompressContinue() alternatively. 1985eda14cbcSMatt Macy ZSTD_nextSrcSizeToDecompress() tells how many bytes to provide as 'srcSize' to ZSTD_decompressContinue(). 1986eda14cbcSMatt Macy ZSTD_decompressContinue() requires this _exact_ amount of bytes, or it will fail. 1987eda14cbcSMatt Macy 1988eda14cbcSMatt Macy @result of ZSTD_decompressContinue() is the number of bytes regenerated within 'dst' (necessarily <= dstCapacity). 1989eda14cbcSMatt Macy It can be zero : it just means ZSTD_decompressContinue() has decoded some metadata item. 1990eda14cbcSMatt Macy It can also be an error code, which can be tested with ZSTD_isError(). 1991eda14cbcSMatt Macy 1992eda14cbcSMatt Macy A frame is fully decoded when ZSTD_nextSrcSizeToDecompress() returns zero. 1993eda14cbcSMatt Macy Context can then be reset to start a new decompression. 1994eda14cbcSMatt Macy 1995eda14cbcSMatt Macy Note : it's possible to know if next input to present is a header or a block, using ZSTD_nextInputType(). 1996eda14cbcSMatt Macy This information is not required to properly decode a frame. 1997eda14cbcSMatt Macy 1998eda14cbcSMatt Macy == Special case : skippable frames == 1999eda14cbcSMatt Macy 2000eda14cbcSMatt Macy Skippable frames allow integration of user-defined data into a flow of concatenated frames. 2001eda14cbcSMatt Macy Skippable frames will be ignored (skipped) by decompressor. 2002eda14cbcSMatt Macy The format of skippable frames is as follows : 2003eda14cbcSMatt Macy a) Skippable frame ID - 4 Bytes, Little endian format, any value from 0x184D2A50 to 0x184D2A5F 2004eda14cbcSMatt Macy b) Frame Size - 4 Bytes, Little endian format, unsigned 32-bits 2005eda14cbcSMatt Macy c) Frame Content - any content (User Data) of length equal to Frame Size 2006eda14cbcSMatt Macy For skippable frames ZSTD_getFrameHeader() returns zfhPtr->frameType==ZSTD_skippableFrame. 2007eda14cbcSMatt Macy For skippable frames ZSTD_decompressContinue() always returns 0 : it only skips the content. 2008eda14cbcSMatt Macy */ 2009eda14cbcSMatt Macy 2010eda14cbcSMatt Macy /*===== Buffer-less streaming decompression functions =====*/ 2011eda14cbcSMatt Macy typedef enum { ZSTD_frame, ZSTD_skippableFrame } ZSTD_frameType_e; 2012eda14cbcSMatt Macy typedef struct { 2013eda14cbcSMatt Macy unsigned long long frameContentSize; /* if == ZSTD_CONTENTSIZE_UNKNOWN, it means this field is not available. 0 means "empty" */ 2014eda14cbcSMatt Macy unsigned long long windowSize; /* can be very large, up to <= frameContentSize */ 2015eda14cbcSMatt Macy unsigned blockSizeMax; 2016eda14cbcSMatt Macy ZSTD_frameType_e frameType; /* if == ZSTD_skippableFrame, frameContentSize is the size of skippable content */ 2017eda14cbcSMatt Macy unsigned headerSize; 2018eda14cbcSMatt Macy unsigned dictID; 2019eda14cbcSMatt Macy unsigned checksumFlag; 2020eda14cbcSMatt Macy } ZSTD_frameHeader; 2021eda14cbcSMatt Macy 2022eda14cbcSMatt Macy /*! ZSTD_getFrameHeader() : 2023eda14cbcSMatt Macy * decode Frame Header, or requires larger `srcSize`. 2024eda14cbcSMatt Macy * @return : 0, `zfhPtr` is correctly filled, 2025eda14cbcSMatt Macy * >0, `srcSize` is too small, value is wanted `srcSize` amount, 2026eda14cbcSMatt Macy * or an error code, which can be tested using ZSTD_isError() */ 2027eda14cbcSMatt Macy ZSTDLIB_API size_t ZSTD_getFrameHeader(ZSTD_frameHeader* zfhPtr, const void* src, size_t srcSize); /**< doesn't consume input */ 2028eda14cbcSMatt Macy /*! ZSTD_getFrameHeader_advanced() : 2029eda14cbcSMatt Macy * same as ZSTD_getFrameHeader(), 2030eda14cbcSMatt Macy * with added capability to select a format (like ZSTD_f_zstd1_magicless) */ 2031eda14cbcSMatt Macy ZSTDLIB_API size_t ZSTD_getFrameHeader_advanced(ZSTD_frameHeader* zfhPtr, const void* src, size_t srcSize, ZSTD_format_e format); 2032eda14cbcSMatt Macy ZSTDLIB_API size_t ZSTD_decodingBufferSize_min(unsigned long long windowSize, unsigned long long frameContentSize); /**< when frame content size is not known, pass in frameContentSize == ZSTD_CONTENTSIZE_UNKNOWN */ 2033eda14cbcSMatt Macy 2034eda14cbcSMatt Macy ZSTDLIB_API size_t ZSTD_decompressBegin(ZSTD_DCtx* dctx); 2035eda14cbcSMatt Macy ZSTDLIB_API size_t ZSTD_decompressBegin_usingDict(ZSTD_DCtx* dctx, const void* dict, size_t dictSize); 2036eda14cbcSMatt Macy ZSTDLIB_API size_t ZSTD_decompressBegin_usingDDict(ZSTD_DCtx* dctx, const ZSTD_DDict* ddict); 2037eda14cbcSMatt Macy 2038eda14cbcSMatt Macy ZSTDLIB_API size_t ZSTD_nextSrcSizeToDecompress(ZSTD_DCtx* dctx); 2039eda14cbcSMatt Macy ZSTDLIB_API size_t ZSTD_decompressContinue(ZSTD_DCtx* dctx, void* dst, size_t dstCapacity, const void* src, size_t srcSize); 2040eda14cbcSMatt Macy 2041eda14cbcSMatt Macy /* misc */ 2042eda14cbcSMatt Macy ZSTDLIB_API void ZSTD_copyDCtx(ZSTD_DCtx* dctx, const ZSTD_DCtx* preparedDCtx); 2043eda14cbcSMatt Macy typedef enum { ZSTDnit_frameHeader, ZSTDnit_blockHeader, ZSTDnit_block, ZSTDnit_lastBlock, ZSTDnit_checksum, ZSTDnit_skippableFrame } ZSTD_nextInputType_e; 2044eda14cbcSMatt Macy ZSTDLIB_API ZSTD_nextInputType_e ZSTD_nextInputType(ZSTD_DCtx* dctx); 2045eda14cbcSMatt Macy 2046eda14cbcSMatt Macy 2047eda14cbcSMatt Macy 2048eda14cbcSMatt Macy 2049eda14cbcSMatt Macy /* ============================ */ 2050eda14cbcSMatt Macy /** Block level API */ 2051eda14cbcSMatt Macy /* ============================ */ 2052eda14cbcSMatt Macy 2053eda14cbcSMatt Macy /*! 2054eda14cbcSMatt Macy Block functions produce and decode raw zstd blocks, without frame metadata. 2055eda14cbcSMatt Macy Frame metadata cost is typically ~12 bytes, which can be non-negligible for very small blocks (< 100 bytes). 2056eda14cbcSMatt Macy But users will have to take in charge needed metadata to regenerate data, such as compressed and content sizes. 2057eda14cbcSMatt Macy 2058eda14cbcSMatt Macy A few rules to respect : 2059eda14cbcSMatt Macy - Compressing and decompressing require a context structure 2060eda14cbcSMatt Macy + Use ZSTD_createCCtx() and ZSTD_createDCtx() 2061eda14cbcSMatt Macy - It is necessary to init context before starting 2062eda14cbcSMatt Macy + compression : any ZSTD_compressBegin*() variant, including with dictionary 2063eda14cbcSMatt Macy + decompression : any ZSTD_decompressBegin*() variant, including with dictionary 2064eda14cbcSMatt Macy + copyCCtx() and copyDCtx() can be used too 2065eda14cbcSMatt Macy - Block size is limited, it must be <= ZSTD_getBlockSize() <= ZSTD_BLOCKSIZE_MAX == 128 KB 2066eda14cbcSMatt Macy + If input is larger than a block size, it's necessary to split input data into multiple blocks 2067eda14cbcSMatt Macy + For inputs larger than a single block, consider using regular ZSTD_compress() instead. 2068eda14cbcSMatt Macy Frame metadata is not that costly, and quickly becomes negligible as source size grows larger than a block. 2069eda14cbcSMatt Macy - When a block is considered not compressible enough, ZSTD_compressBlock() result will be 0 (zero) ! 2070eda14cbcSMatt Macy ===> In which case, nothing is produced into `dst` ! 2071eda14cbcSMatt Macy + User __must__ test for such outcome and deal directly with uncompressed data 2072eda14cbcSMatt Macy + A block cannot be declared incompressible if ZSTD_compressBlock() return value was != 0. 2073eda14cbcSMatt Macy Doing so would mess up with statistics history, leading to potential data corruption. 2074eda14cbcSMatt Macy + ZSTD_decompressBlock() _doesn't accept uncompressed data as input_ !! 2075eda14cbcSMatt Macy + In case of multiple successive blocks, should some of them be uncompressed, 2076eda14cbcSMatt Macy decoder must be informed of their existence in order to follow proper history. 2077eda14cbcSMatt Macy Use ZSTD_insertBlock() for such a case. 2078eda14cbcSMatt Macy */ 2079eda14cbcSMatt Macy 2080eda14cbcSMatt Macy /*===== Raw zstd block functions =====*/ 2081eda14cbcSMatt Macy ZSTDLIB_API size_t ZSTD_getBlockSize (const ZSTD_CCtx* cctx); 2082eda14cbcSMatt Macy ZSTDLIB_API size_t ZSTD_compressBlock (ZSTD_CCtx* cctx, void* dst, size_t dstCapacity, const void* src, size_t srcSize); 2083eda14cbcSMatt Macy ZSTDLIB_API size_t ZSTD_decompressBlock(ZSTD_DCtx* dctx, void* dst, size_t dstCapacity, const void* src, size_t srcSize); 2084eda14cbcSMatt Macy ZSTDLIB_API size_t ZSTD_insertBlock (ZSTD_DCtx* dctx, const void* blockStart, size_t blockSize); /**< insert uncompressed block into `dctx` history. Useful for multi-blocks decompression. */ 2085eda14cbcSMatt Macy 2086eda14cbcSMatt Macy 2087eda14cbcSMatt Macy #endif /* ZSTD_H_ZSTD_STATIC_LINKING_ONLY */ 2088eda14cbcSMatt Macy 2089eda14cbcSMatt Macy #if defined (__cplusplus) 2090eda14cbcSMatt Macy } 2091eda14cbcSMatt Macy #endif 2092