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