10c16b537SWarner Losh /* 2*5ff13fbcSAllan Jude * Copyright (c) Yann Collet, Facebook, Inc. 30c16b537SWarner Losh * All rights reserved. 40c16b537SWarner Losh * 50c16b537SWarner Losh * This source code is licensed under both the BSD-style license (found in the 60c16b537SWarner Losh * LICENSE file in the root directory of this source tree) and the GPLv2 (found 70c16b537SWarner Losh * in the COPYING file in the root directory of this source tree). 80c16b537SWarner Losh * You may select, at your option, one of the above-listed licenses. 90c16b537SWarner Losh */ 100c16b537SWarner Losh #if defined (__cplusplus) 110c16b537SWarner Losh extern "C" { 120c16b537SWarner Losh #endif 130c16b537SWarner Losh 140c16b537SWarner Losh #ifndef ZSTD_H_235446 150c16b537SWarner Losh #define ZSTD_H_235446 160c16b537SWarner Losh 170c16b537SWarner Losh /* ====== Dependency ======*/ 189cbefe25SConrad Meyer #include <limits.h> /* INT_MAX */ 190c16b537SWarner Losh #include <stddef.h> /* size_t */ 200c16b537SWarner Losh 210c16b537SWarner Losh 220c16b537SWarner Losh /* ===== ZSTDLIB_API : control library symbols visibility ===== */ 23*5ff13fbcSAllan Jude #ifndef ZSTDLIB_VISIBLE 24*5ff13fbcSAllan Jude # if defined(__GNUC__) && (__GNUC__ >= 4) && !defined(__MINGW32__) 25*5ff13fbcSAllan Jude # define ZSTDLIB_VISIBLE __attribute__ ((visibility ("default"))) 26*5ff13fbcSAllan Jude # define ZSTDLIB_HIDDEN __attribute__ ((visibility ("hidden"))) 270c16b537SWarner Losh # else 28*5ff13fbcSAllan Jude # define ZSTDLIB_VISIBLE 29*5ff13fbcSAllan Jude # define ZSTDLIB_HIDDEN 300c16b537SWarner Losh # endif 310c16b537SWarner Losh #endif 320c16b537SWarner Losh #if defined(ZSTD_DLL_EXPORT) && (ZSTD_DLL_EXPORT==1) 33*5ff13fbcSAllan Jude # define ZSTDLIB_API __declspec(dllexport) ZSTDLIB_VISIBLE 340c16b537SWarner Losh #elif defined(ZSTD_DLL_IMPORT) && (ZSTD_DLL_IMPORT==1) 35*5ff13fbcSAllan Jude # define ZSTDLIB_API __declspec(dllimport) ZSTDLIB_VISIBLE /* It isn't required but allows to generate better code, saving a function pointer load from the IAT and an indirect jump.*/ 360c16b537SWarner Losh #else 37*5ff13fbcSAllan Jude # define ZSTDLIB_API ZSTDLIB_VISIBLE 380c16b537SWarner Losh #endif 390c16b537SWarner Losh 400c16b537SWarner Losh 410f743729SConrad Meyer /******************************************************************************* 420c16b537SWarner Losh Introduction 430c16b537SWarner Losh 440f743729SConrad Meyer zstd, short for Zstandard, is a fast lossless compression algorithm, targeting 450f743729SConrad Meyer real-time compression scenarios at zlib-level and better compression ratios. 460f743729SConrad Meyer The zstd compression library provides in-memory compression and decompression 470f743729SConrad Meyer functions. 480f743729SConrad Meyer 490f743729SConrad Meyer The library supports regular compression levels from 1 up to ZSTD_maxCLevel(), 500f743729SConrad Meyer which is currently 22. Levels >= 20, labeled `--ultra`, should be used with 510f743729SConrad Meyer caution, as they require more memory. The library also offers negative 520f743729SConrad Meyer compression levels, which extend the range of speed vs. ratio preferences. 530f743729SConrad Meyer The lower the level, the faster the speed (at the cost of compression). 540f743729SConrad Meyer 550c16b537SWarner Losh Compression can be done in: 560c16b537SWarner Losh - a single step (described as Simple API) 5719fcbaf1SConrad Meyer - a single step, reusing a context (described as Explicit context) 580c16b537SWarner Losh - unbounded multiple steps (described as Streaming compression) 590c16b537SWarner Losh 600f743729SConrad Meyer The compression ratio achievable on small data can be highly improved using 610f743729SConrad Meyer a dictionary. Dictionary compression can be performed in: 620f743729SConrad Meyer - a single step (described as Simple dictionary API) 630f743729SConrad Meyer - a single step, reusing a dictionary (described as Bulk-processing 640f743729SConrad Meyer dictionary API) 650f743729SConrad Meyer 660f743729SConrad Meyer Advanced experimental functions can be accessed using 670f743729SConrad Meyer `#define ZSTD_STATIC_LINKING_ONLY` before including zstd.h. 680f743729SConrad Meyer 690f743729SConrad Meyer Advanced experimental APIs should never be used with a dynamically-linked 700f743729SConrad Meyer library. They are not "stable"; their definitions or signatures may change in 710f743729SConrad Meyer the future. Only static linking is allowed. 720f743729SConrad Meyer *******************************************************************************/ 730c16b537SWarner Losh 740c16b537SWarner Losh /*------ Version ------*/ 750c16b537SWarner Losh #define ZSTD_VERSION_MAJOR 1 76*5ff13fbcSAllan Jude #define ZSTD_VERSION_MINOR 5 77*5ff13fbcSAllan Jude #define ZSTD_VERSION_RELEASE 2 780c16b537SWarner Losh #define ZSTD_VERSION_NUMBER (ZSTD_VERSION_MAJOR *100*100 + ZSTD_VERSION_MINOR *100 + ZSTD_VERSION_RELEASE) 79f7cd7fe5SConrad Meyer 80f7cd7fe5SConrad Meyer /*! ZSTD_versionNumber() : 81f7cd7fe5SConrad Meyer * Return runtime library version, the value is (MAJOR*100*100 + MINOR*100 + RELEASE). */ 82f7cd7fe5SConrad Meyer ZSTDLIB_API unsigned ZSTD_versionNumber(void); 830c16b537SWarner Losh 840c16b537SWarner Losh #define ZSTD_LIB_VERSION ZSTD_VERSION_MAJOR.ZSTD_VERSION_MINOR.ZSTD_VERSION_RELEASE 850c16b537SWarner Losh #define ZSTD_QUOTE(str) #str 860c16b537SWarner Losh #define ZSTD_EXPAND_AND_QUOTE(str) ZSTD_QUOTE(str) 870c16b537SWarner Losh #define ZSTD_VERSION_STRING ZSTD_EXPAND_AND_QUOTE(ZSTD_LIB_VERSION) 88f7cd7fe5SConrad Meyer 89f7cd7fe5SConrad Meyer /*! ZSTD_versionString() : 90f7cd7fe5SConrad Meyer * Return runtime library version, like "1.4.5". Requires v1.3.0+. */ 91f7cd7fe5SConrad Meyer ZSTDLIB_API const char* ZSTD_versionString(void); 920c16b537SWarner Losh 934d3f1eafSConrad Meyer /* ************************************* 940f743729SConrad Meyer * Default constant 950f743729SConrad Meyer ***************************************/ 960f743729SConrad Meyer #ifndef ZSTD_CLEVEL_DEFAULT 970f743729SConrad Meyer # define ZSTD_CLEVEL_DEFAULT 3 980f743729SConrad Meyer #endif 990c16b537SWarner Losh 1004d3f1eafSConrad Meyer /* ************************************* 1012b9c00cbSConrad Meyer * Constants 1022b9c00cbSConrad Meyer ***************************************/ 1032b9c00cbSConrad Meyer 1042b9c00cbSConrad Meyer /* All magic numbers are supposed read/written to/from files/memory using little-endian convention */ 1052b9c00cbSConrad Meyer #define ZSTD_MAGICNUMBER 0xFD2FB528 /* valid since v0.8.0 */ 1062b9c00cbSConrad Meyer #define ZSTD_MAGIC_DICTIONARY 0xEC30A437 /* valid since v0.7.0 */ 1072b9c00cbSConrad Meyer #define ZSTD_MAGIC_SKIPPABLE_START 0x184D2A50 /* all 16 values, from 0x184D2A50 to 0x184D2A5F, signal the beginning of a skippable frame */ 1082b9c00cbSConrad Meyer #define ZSTD_MAGIC_SKIPPABLE_MASK 0xFFFFFFF0 1092b9c00cbSConrad Meyer 1102b9c00cbSConrad Meyer #define ZSTD_BLOCKSIZELOG_MAX 17 1112b9c00cbSConrad Meyer #define ZSTD_BLOCKSIZE_MAX (1<<ZSTD_BLOCKSIZELOG_MAX) 1122b9c00cbSConrad Meyer 1132b9c00cbSConrad Meyer 1142b9c00cbSConrad Meyer /*************************************** 1150c16b537SWarner Losh * Simple API 1160c16b537SWarner Losh ***************************************/ 1170c16b537SWarner Losh /*! ZSTD_compress() : 1180c16b537SWarner Losh * Compresses `src` content as a single zstd compressed frame into already allocated `dst`. 1190c16b537SWarner Losh * Hint : compression runs faster if `dstCapacity` >= `ZSTD_compressBound(srcSize)`. 1200c16b537SWarner Losh * @return : compressed size written into `dst` (<= `dstCapacity), 1210c16b537SWarner Losh * or an error code if it fails (which can be tested using ZSTD_isError()). */ 1220c16b537SWarner Losh ZSTDLIB_API size_t ZSTD_compress( void* dst, size_t dstCapacity, 1230c16b537SWarner Losh const void* src, size_t srcSize, 1240c16b537SWarner Losh int compressionLevel); 1250c16b537SWarner Losh 1260c16b537SWarner Losh /*! ZSTD_decompress() : 1270c16b537SWarner Losh * `compressedSize` : must be the _exact_ size of some number of compressed and/or skippable frames. 1280c16b537SWarner Losh * `dstCapacity` is an upper bound of originalSize to regenerate. 1290c16b537SWarner Losh * If user cannot imply a maximum upper bound, it's better to use streaming mode to decompress data. 1300c16b537SWarner Losh * @return : the number of bytes decompressed into `dst` (<= `dstCapacity`), 1310c16b537SWarner Losh * or an errorCode if it fails (which can be tested using ZSTD_isError()). */ 1320c16b537SWarner Losh ZSTDLIB_API size_t ZSTD_decompress( void* dst, size_t dstCapacity, 1330c16b537SWarner Losh const void* src, size_t compressedSize); 1340c16b537SWarner Losh 135a0483764SConrad Meyer /*! ZSTD_getFrameContentSize() : requires v1.3.0+ 1360c16b537SWarner Losh * `src` should point to the start of a ZSTD encoded frame. 1370c16b537SWarner Losh * `srcSize` must be at least as large as the frame header. 1380c16b537SWarner Losh * hint : any size >= `ZSTD_frameHeaderSize_max` is large enough. 1390f743729SConrad Meyer * @return : - decompressed size of `src` frame content, if known 1400c16b537SWarner Losh * - ZSTD_CONTENTSIZE_UNKNOWN if the size cannot be determined 1410c16b537SWarner Losh * - ZSTD_CONTENTSIZE_ERROR if an error occurred (e.g. invalid magic number, srcSize too small) 1420c16b537SWarner Losh * note 1 : a 0 return value means the frame is valid but "empty". 1430c16b537SWarner Losh * note 2 : decompressed size is an optional field, it may not be present, typically in streaming mode. 1440c16b537SWarner Losh * When `return==ZSTD_CONTENTSIZE_UNKNOWN`, data to decompress could be any size. 1450c16b537SWarner Losh * In which case, it's necessary to use streaming mode to decompress data. 1460c16b537SWarner Losh * Optionally, application can rely on some implicit limit, 1470c16b537SWarner Losh * as ZSTD_decompress() only needs an upper bound of decompressed size. 1480c16b537SWarner Losh * (For example, data could be necessarily cut into blocks <= 16 KB). 1490f743729SConrad Meyer * note 3 : decompressed size is always present when compression is completed using single-pass functions, 1500f743729SConrad Meyer * such as ZSTD_compress(), ZSTD_compressCCtx() ZSTD_compress_usingDict() or ZSTD_compress_usingCDict(). 1510c16b537SWarner Losh * note 4 : decompressed size can be very large (64-bits value), 1520c16b537SWarner Losh * potentially larger than what local system can handle as a single memory segment. 1530c16b537SWarner Losh * In which case, it's necessary to use streaming mode to decompress data. 1540c16b537SWarner Losh * note 5 : If source is untrusted, decompressed size could be wrong or intentionally modified. 1550c16b537SWarner Losh * Always ensure return value fits within application's authorized limits. 1560c16b537SWarner Losh * Each application can set its own limits. 1570c16b537SWarner Losh * note 6 : This function replaces ZSTD_getDecompressedSize() */ 1580c16b537SWarner Losh #define ZSTD_CONTENTSIZE_UNKNOWN (0ULL - 1) 1590c16b537SWarner Losh #define ZSTD_CONTENTSIZE_ERROR (0ULL - 2) 1600c16b537SWarner Losh ZSTDLIB_API unsigned long long ZSTD_getFrameContentSize(const void *src, size_t srcSize); 1610c16b537SWarner Losh 1620c16b537SWarner Losh /*! ZSTD_getDecompressedSize() : 1630c16b537SWarner Losh * NOTE: This function is now obsolete, in favor of ZSTD_getFrameContentSize(). 16419fcbaf1SConrad Meyer * Both functions work the same way, but ZSTD_getDecompressedSize() blends 16519fcbaf1SConrad Meyer * "empty", "unknown" and "error" results to the same return value (0), 16619fcbaf1SConrad Meyer * while ZSTD_getFrameContentSize() gives them separate return values. 1670f743729SConrad Meyer * @return : decompressed size of `src` frame content _if known and not empty_, 0 otherwise. */ 1680c16b537SWarner Losh ZSTDLIB_API unsigned long long ZSTD_getDecompressedSize(const void* src, size_t srcSize); 1690c16b537SWarner Losh 170*5ff13fbcSAllan Jude /*! ZSTD_findFrameCompressedSize() : Requires v1.4.0+ 1712b9c00cbSConrad Meyer * `src` should point to the start of a ZSTD frame or skippable frame. 1722b9c00cbSConrad Meyer * `srcSize` must be >= first frame size 1732b9c00cbSConrad Meyer * @return : the compressed size of the first frame starting at `src`, 1742b9c00cbSConrad Meyer * suitable to pass as `srcSize` to `ZSTD_decompress` or similar, 1752b9c00cbSConrad Meyer * or an error code if input is invalid */ 1762b9c00cbSConrad Meyer ZSTDLIB_API size_t ZSTD_findFrameCompressedSize(const void* src, size_t srcSize); 1772b9c00cbSConrad Meyer 1780c16b537SWarner Losh 1790c16b537SWarner Losh /*====== Helper functions ======*/ 180052d3c12SConrad Meyer #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 */ 18119fcbaf1SConrad Meyer ZSTDLIB_API size_t ZSTD_compressBound(size_t srcSize); /*!< maximum compressed size in worst case single-pass scenario */ 1820c16b537SWarner Losh ZSTDLIB_API unsigned ZSTD_isError(size_t code); /*!< tells if a `size_t` function result is an error code */ 1830c16b537SWarner Losh ZSTDLIB_API const char* ZSTD_getErrorName(size_t code); /*!< provides readable string from an error code */ 184*5ff13fbcSAllan Jude ZSTDLIB_API int ZSTD_minCLevel(void); /*!< minimum negative compression level allowed, requires v1.4.0+ */ 1850c16b537SWarner Losh ZSTDLIB_API int ZSTD_maxCLevel(void); /*!< maximum compression level available */ 186*5ff13fbcSAllan Jude ZSTDLIB_API int ZSTD_defaultCLevel(void); /*!< default compression level, specified by ZSTD_CLEVEL_DEFAULT, requires v1.5.0+ */ 1870c16b537SWarner Losh 1880c16b537SWarner Losh 1890c16b537SWarner Losh /*************************************** 19019fcbaf1SConrad Meyer * Explicit context 1910c16b537SWarner Losh ***************************************/ 1920c16b537SWarner Losh /*= Compression context 1930c16b537SWarner Losh * When compressing many times, 1944d3f1eafSConrad Meyer * it is recommended to allocate a context just once, 1954d3f1eafSConrad Meyer * and re-use it for each successive compression operation. 1960c16b537SWarner Losh * This will make workload friendlier for system's memory. 1974d3f1eafSConrad Meyer * Note : re-using context is just a speed / resource optimization. 1984d3f1eafSConrad Meyer * It doesn't change the compression ratio, which remains identical. 1994d3f1eafSConrad Meyer * Note 2 : In multi-threaded environments, 2004d3f1eafSConrad Meyer * use one different context per thread for parallel execution. 2014d3f1eafSConrad Meyer */ 2020c16b537SWarner Losh typedef struct ZSTD_CCtx_s ZSTD_CCtx; 2030c16b537SWarner Losh ZSTDLIB_API ZSTD_CCtx* ZSTD_createCCtx(void); 204*5ff13fbcSAllan Jude ZSTDLIB_API size_t ZSTD_freeCCtx(ZSTD_CCtx* cctx); /* accept NULL pointer */ 2050c16b537SWarner Losh 2060c16b537SWarner Losh /*! ZSTD_compressCCtx() : 2079cbefe25SConrad Meyer * Same as ZSTD_compress(), using an explicit ZSTD_CCtx. 2089cbefe25SConrad Meyer * Important : in order to behave similarly to `ZSTD_compress()`, 2099cbefe25SConrad Meyer * this function compresses at requested compression level, 2109cbefe25SConrad Meyer * __ignoring any other parameter__ . 2119cbefe25SConrad Meyer * If any advanced parameter was set using the advanced API, 2129cbefe25SConrad Meyer * they will all be reset. Only `compressionLevel` remains. 2139cbefe25SConrad Meyer */ 214a0483764SConrad Meyer ZSTDLIB_API size_t ZSTD_compressCCtx(ZSTD_CCtx* cctx, 2150c16b537SWarner Losh void* dst, size_t dstCapacity, 2160c16b537SWarner Losh const void* src, size_t srcSize, 2170c16b537SWarner Losh int compressionLevel); 2180c16b537SWarner Losh 2190c16b537SWarner Losh /*= Decompression context 2200c16b537SWarner Losh * When decompressing many times, 2210c16b537SWarner Losh * it is recommended to allocate a context only once, 2220c16b537SWarner Losh * and re-use it for each successive compression operation. 2230c16b537SWarner Losh * This will make workload friendlier for system's memory. 2240c16b537SWarner Losh * Use one context per thread for parallel execution. */ 2250c16b537SWarner Losh typedef struct ZSTD_DCtx_s ZSTD_DCtx; 2260c16b537SWarner Losh ZSTDLIB_API ZSTD_DCtx* ZSTD_createDCtx(void); 227*5ff13fbcSAllan Jude ZSTDLIB_API size_t ZSTD_freeDCtx(ZSTD_DCtx* dctx); /* accept NULL pointer */ 2280c16b537SWarner Losh 2290c16b537SWarner Losh /*! ZSTD_decompressDCtx() : 230a0483764SConrad Meyer * Same as ZSTD_decompress(), 231a0483764SConrad Meyer * requires an allocated ZSTD_DCtx. 232a0483764SConrad Meyer * Compatible with sticky parameters. 233a0483764SConrad Meyer */ 234a0483764SConrad Meyer ZSTDLIB_API size_t ZSTD_decompressDCtx(ZSTD_DCtx* dctx, 2350c16b537SWarner Losh void* dst, size_t dstCapacity, 2360c16b537SWarner Losh const void* src, size_t srcSize); 2370c16b537SWarner Losh 2380c16b537SWarner Losh 239*5ff13fbcSAllan Jude /********************************************* 240*5ff13fbcSAllan Jude * Advanced compression API (Requires v1.4.0+) 241*5ff13fbcSAllan Jude **********************************************/ 242a0483764SConrad Meyer 243a0483764SConrad Meyer /* API design : 244a0483764SConrad Meyer * Parameters are pushed one by one into an existing context, 245a0483764SConrad Meyer * using ZSTD_CCtx_set*() functions. 246a0483764SConrad Meyer * Pushed parameters are sticky : they are valid for next compressed frame, and any subsequent frame. 247a0483764SConrad Meyer * "sticky" parameters are applicable to `ZSTD_compress2()` and `ZSTD_compressStream*()` ! 2489cbefe25SConrad Meyer * __They do not apply to "simple" one-shot variants such as ZSTD_compressCCtx()__ . 249a0483764SConrad Meyer * 250a0483764SConrad Meyer * It's possible to reset all parameters to "default" using ZSTD_CCtx_reset(). 251a0483764SConrad Meyer * 252*5ff13fbcSAllan Jude * This API supersedes all other "advanced" API entry points in the experimental section. 253a0483764SConrad Meyer * In the future, we expect to remove from experimental API entry points which are redundant with this API. 254a0483764SConrad Meyer */ 255a0483764SConrad Meyer 256a0483764SConrad Meyer 257a0483764SConrad Meyer /* Compression strategies, listed from fastest to strongest */ 258a0483764SConrad Meyer typedef enum { ZSTD_fast=1, 259a0483764SConrad Meyer ZSTD_dfast=2, 260a0483764SConrad Meyer ZSTD_greedy=3, 261a0483764SConrad Meyer ZSTD_lazy=4, 262a0483764SConrad Meyer ZSTD_lazy2=5, 263a0483764SConrad Meyer ZSTD_btlazy2=6, 264a0483764SConrad Meyer ZSTD_btopt=7, 265a0483764SConrad Meyer ZSTD_btultra=8, 266a0483764SConrad Meyer ZSTD_btultra2=9 267a0483764SConrad Meyer /* note : new strategies _might_ be added in the future. 268a0483764SConrad Meyer Only the order (from fast to strong) is guaranteed */ 269a0483764SConrad Meyer } ZSTD_strategy; 270a0483764SConrad Meyer 271a0483764SConrad Meyer typedef enum { 272a0483764SConrad Meyer 2732b9c00cbSConrad Meyer /* compression parameters 2742b9c00cbSConrad Meyer * Note: When compressing with a ZSTD_CDict these parameters are superseded 2759cbefe25SConrad Meyer * by the parameters used to construct the ZSTD_CDict. 2769cbefe25SConrad Meyer * See ZSTD_CCtx_refCDict() for more info (superseded-by-cdict). */ 2779cbefe25SConrad Meyer ZSTD_c_compressionLevel=100, /* Set compression parameters according to pre-defined cLevel table. 2789cbefe25SConrad Meyer * Note that exact compression parameters are dynamically determined, 2799cbefe25SConrad Meyer * depending on both compression level and srcSize (when known). 280a0483764SConrad Meyer * Default level is ZSTD_CLEVEL_DEFAULT==3. 281a0483764SConrad Meyer * Special: value 0 means default, which is controlled by ZSTD_CLEVEL_DEFAULT. 282a0483764SConrad Meyer * Note 1 : it's possible to pass a negative compression level. 28337f1f268SConrad Meyer * Note 2 : setting a level does not automatically set all other compression parameters 28437f1f268SConrad Meyer * to default. Setting this will however eventually dynamically impact the compression 28537f1f268SConrad Meyer * parameters which have not been manually set. The manually set 28637f1f268SConrad Meyer * ones will 'stick'. */ 2879cbefe25SConrad Meyer /* Advanced compression parameters : 2889cbefe25SConrad Meyer * It's possible to pin down compression parameters to some specific values. 2899cbefe25SConrad Meyer * In which case, these values are no longer dynamically selected by the compressor */ 290a0483764SConrad Meyer ZSTD_c_windowLog=101, /* Maximum allowed back-reference distance, expressed as power of 2. 2919cbefe25SConrad Meyer * This will set a memory budget for streaming decompression, 2929cbefe25SConrad Meyer * with larger values requiring more memory 2939cbefe25SConrad Meyer * and typically compressing more. 294a0483764SConrad Meyer * Must be clamped between ZSTD_WINDOWLOG_MIN and ZSTD_WINDOWLOG_MAX. 295a0483764SConrad Meyer * Special: value 0 means "use default windowLog". 296a0483764SConrad Meyer * Note: Using a windowLog greater than ZSTD_WINDOWLOG_LIMIT_DEFAULT 2979cbefe25SConrad Meyer * requires explicitly allowing such size at streaming decompression stage. */ 298a0483764SConrad Meyer ZSTD_c_hashLog=102, /* Size of the initial probe table, as a power of 2. 299a0483764SConrad Meyer * Resulting memory usage is (1 << (hashLog+2)). 300a0483764SConrad Meyer * Must be clamped between ZSTD_HASHLOG_MIN and ZSTD_HASHLOG_MAX. 301a0483764SConrad Meyer * Larger tables improve compression ratio of strategies <= dFast, 302a0483764SConrad Meyer * and improve speed of strategies > dFast. 303a0483764SConrad Meyer * Special: value 0 means "use default hashLog". */ 304a0483764SConrad Meyer ZSTD_c_chainLog=103, /* Size of the multi-probe search table, as a power of 2. 305a0483764SConrad Meyer * Resulting memory usage is (1 << (chainLog+2)). 306a0483764SConrad Meyer * Must be clamped between ZSTD_CHAINLOG_MIN and ZSTD_CHAINLOG_MAX. 307a0483764SConrad Meyer * Larger tables result in better and slower compression. 3089cbefe25SConrad Meyer * This parameter is useless for "fast" strategy. 309a0483764SConrad Meyer * It's still useful when using "dfast" strategy, 310a0483764SConrad Meyer * in which case it defines a secondary probe table. 311a0483764SConrad Meyer * Special: value 0 means "use default chainLog". */ 312a0483764SConrad Meyer ZSTD_c_searchLog=104, /* Number of search attempts, as a power of 2. 313a0483764SConrad Meyer * More attempts result in better and slower compression. 3149cbefe25SConrad Meyer * This parameter is useless for "fast" and "dFast" strategies. 315a0483764SConrad Meyer * Special: value 0 means "use default searchLog". */ 316a0483764SConrad Meyer ZSTD_c_minMatch=105, /* Minimum size of searched matches. 317a0483764SConrad Meyer * Note that Zstandard can still find matches of smaller size, 318a0483764SConrad Meyer * it just tweaks its search algorithm to look for this size and larger. 319a0483764SConrad Meyer * Larger values increase compression and decompression speed, but decrease ratio. 320a0483764SConrad Meyer * Must be clamped between ZSTD_MINMATCH_MIN and ZSTD_MINMATCH_MAX. 321a0483764SConrad Meyer * Note that currently, for all strategies < btopt, effective minimum is 4. 322a0483764SConrad Meyer * , for all strategies > fast, effective maximum is 6. 323a0483764SConrad Meyer * Special: value 0 means "use default minMatchLength". */ 324a0483764SConrad Meyer ZSTD_c_targetLength=106, /* Impact of this field depends on strategy. 325a0483764SConrad Meyer * For strategies btopt, btultra & btultra2: 326a0483764SConrad Meyer * Length of Match considered "good enough" to stop search. 327a0483764SConrad Meyer * Larger values make compression stronger, and slower. 328a0483764SConrad Meyer * For strategy fast: 329a0483764SConrad Meyer * Distance between match sampling. 330a0483764SConrad Meyer * Larger values make compression faster, and weaker. 331a0483764SConrad Meyer * Special: value 0 means "use default targetLength". */ 332a0483764SConrad Meyer ZSTD_c_strategy=107, /* See ZSTD_strategy enum definition. 333a0483764SConrad Meyer * The higher the value of selected strategy, the more complex it is, 334a0483764SConrad Meyer * resulting in stronger and slower compression. 335a0483764SConrad Meyer * Special: value 0 means "use default strategy". */ 336a0483764SConrad Meyer /* LDM mode parameters */ 337a0483764SConrad Meyer ZSTD_c_enableLongDistanceMatching=160, /* Enable long distance matching. 338a0483764SConrad Meyer * This parameter is designed to improve compression ratio 339a0483764SConrad Meyer * for large inputs, by finding large matches at long distance. 340a0483764SConrad Meyer * It increases memory usage and window size. 341a0483764SConrad Meyer * Note: enabling this parameter increases default ZSTD_c_windowLog to 128 MB 342f7cd7fe5SConrad Meyer * except when expressly set to a different value. 343f7cd7fe5SConrad Meyer * Note: will be enabled by default if ZSTD_c_windowLog >= 128 MB and 344f7cd7fe5SConrad Meyer * compression strategy >= ZSTD_btopt (== compression level 16+) */ 345a0483764SConrad Meyer ZSTD_c_ldmHashLog=161, /* Size of the table for long distance matching, as a power of 2. 346a0483764SConrad Meyer * Larger values increase memory usage and compression ratio, 347a0483764SConrad Meyer * but decrease compression speed. 348a0483764SConrad Meyer * Must be clamped between ZSTD_HASHLOG_MIN and ZSTD_HASHLOG_MAX 349a0483764SConrad Meyer * default: windowlog - 7. 350a0483764SConrad Meyer * Special: value 0 means "automatically determine hashlog". */ 351a0483764SConrad Meyer ZSTD_c_ldmMinMatch=162, /* Minimum match size for long distance matcher. 352a0483764SConrad Meyer * Larger/too small values usually decrease compression ratio. 353a0483764SConrad Meyer * Must be clamped between ZSTD_LDM_MINMATCH_MIN and ZSTD_LDM_MINMATCH_MAX. 354a0483764SConrad Meyer * Special: value 0 means "use default value" (default: 64). */ 355a0483764SConrad Meyer ZSTD_c_ldmBucketSizeLog=163, /* Log size of each bucket in the LDM hash table for collision resolution. 356a0483764SConrad Meyer * Larger values improve collision resolution but decrease compression speed. 357a0483764SConrad Meyer * The maximum value is ZSTD_LDM_BUCKETSIZELOG_MAX. 358a0483764SConrad Meyer * Special: value 0 means "use default value" (default: 3). */ 359a0483764SConrad Meyer ZSTD_c_ldmHashRateLog=164, /* Frequency of inserting/looking up entries into the LDM hash table. 360a0483764SConrad Meyer * Must be clamped between 0 and (ZSTD_WINDOWLOG_MAX - ZSTD_HASHLOG_MIN). 361a0483764SConrad Meyer * Default is MAX(0, (windowLog - ldmHashLog)), optimizing hash table usage. 362a0483764SConrad Meyer * Larger values improve compression speed. 363a0483764SConrad Meyer * Deviating far from default value will likely result in a compression ratio decrease. 364a0483764SConrad Meyer * Special: value 0 means "automatically determine hashRateLog". */ 365a0483764SConrad Meyer 366a0483764SConrad Meyer /* frame parameters */ 367a0483764SConrad Meyer ZSTD_c_contentSizeFlag=200, /* Content size will be written into frame header _whenever known_ (default:1) 368a0483764SConrad Meyer * Content size must be known at the beginning of compression. 369a0483764SConrad Meyer * This is automatically the case when using ZSTD_compress2(), 3709cbefe25SConrad Meyer * For streaming scenarios, content size must be provided with ZSTD_CCtx_setPledgedSrcSize() */ 371a0483764SConrad Meyer ZSTD_c_checksumFlag=201, /* A 32-bits checksum of content is written at end of frame (default:0) */ 372a0483764SConrad Meyer ZSTD_c_dictIDFlag=202, /* When applicable, dictionary's ID is written into frame header (default:1) */ 373a0483764SConrad Meyer 374a0483764SConrad Meyer /* multi-threading parameters */ 375f7cd7fe5SConrad Meyer /* These parameters are only active if multi-threading is enabled (compiled with build macro ZSTD_MULTITHREAD). 376f7cd7fe5SConrad Meyer * Otherwise, trying to set any other value than default (0) will be a no-op and return an error. 377f7cd7fe5SConrad Meyer * In a situation where it's unknown if the linked library supports multi-threading or not, 378f7cd7fe5SConrad Meyer * setting ZSTD_c_nbWorkers to any value >= 1 and consulting the return value provides a quick way to check this property. 379f7cd7fe5SConrad Meyer */ 380a0483764SConrad Meyer ZSTD_c_nbWorkers=400, /* Select how many threads will be spawned to compress in parallel. 381f7cd7fe5SConrad Meyer * When nbWorkers >= 1, triggers asynchronous mode when invoking ZSTD_compressStream*() : 382a0483764SConrad Meyer * ZSTD_compressStream*() consumes input and flush output if possible, but immediately gives back control to caller, 383f7cd7fe5SConrad Meyer * while compression is performed in parallel, within worker thread(s). 384a0483764SConrad Meyer * (note : a strong exception to this rule is when first invocation of ZSTD_compressStream2() sets ZSTD_e_end : 385a0483764SConrad Meyer * in which case, ZSTD_compressStream2() delegates to ZSTD_compress2(), which is always a blocking call). 386a0483764SConrad Meyer * More workers improve speed, but also increase memory usage. 387f7cd7fe5SConrad Meyer * Default value is `0`, aka "single-threaded mode" : no worker is spawned, 388f7cd7fe5SConrad Meyer * compression is performed inside Caller's thread, and all invocations are blocking */ 389a0483764SConrad Meyer ZSTD_c_jobSize=401, /* Size of a compression job. This value is enforced only when nbWorkers >= 1. 390a0483764SConrad Meyer * Each compression job is completed in parallel, so this value can indirectly impact the nb of active threads. 391a0483764SConrad Meyer * 0 means default, which is dynamically determined based on compression parameters. 392*5ff13fbcSAllan Jude * Job size must be a minimum of overlap size, or ZSTDMT_JOBSIZE_MIN (= 512 KB), whichever is largest. 3939cbefe25SConrad Meyer * The minimum size is automatically and transparently enforced. */ 394a0483764SConrad Meyer ZSTD_c_overlapLog=402, /* Control the overlap size, as a fraction of window size. 395a0483764SConrad Meyer * The overlap size is an amount of data reloaded from previous job at the beginning of a new job. 396a0483764SConrad Meyer * It helps preserve compression ratio, while each job is compressed in parallel. 397a0483764SConrad Meyer * This value is enforced only when nbWorkers >= 1. 398a0483764SConrad Meyer * Larger values increase compression ratio, but decrease speed. 399a0483764SConrad Meyer * Possible values range from 0 to 9 : 400a0483764SConrad Meyer * - 0 means "default" : value will be determined by the library, depending on strategy 401a0483764SConrad Meyer * - 1 means "no overlap" 402a0483764SConrad Meyer * - 9 means "full overlap", using a full window size. 403a0483764SConrad Meyer * Each intermediate rank increases/decreases load size by a factor 2 : 404a0483764SConrad Meyer * 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 405a0483764SConrad Meyer * default value varies between 6 and 9, depending on strategy */ 406a0483764SConrad Meyer 407a0483764SConrad Meyer /* note : additional experimental parameters are also available 408a0483764SConrad Meyer * within the experimental section of the API. 409a0483764SConrad Meyer * At the time of this writing, they include : 410a0483764SConrad Meyer * ZSTD_c_rsyncable 411a0483764SConrad Meyer * ZSTD_c_format 412a0483764SConrad Meyer * ZSTD_c_forceMaxWindow 413a0483764SConrad Meyer * ZSTD_c_forceAttachDict 4142b9c00cbSConrad Meyer * ZSTD_c_literalCompressionMode 4154d3f1eafSConrad Meyer * ZSTD_c_targetCBlockSize 4169cbefe25SConrad Meyer * ZSTD_c_srcSizeHint 417f7cd7fe5SConrad Meyer * ZSTD_c_enableDedicatedDictSearch 418f7cd7fe5SConrad Meyer * ZSTD_c_stableInBuffer 419f7cd7fe5SConrad Meyer * ZSTD_c_stableOutBuffer 420f7cd7fe5SConrad Meyer * ZSTD_c_blockDelimiters 421f7cd7fe5SConrad Meyer * ZSTD_c_validateSequences 422*5ff13fbcSAllan Jude * ZSTD_c_useBlockSplitter 423*5ff13fbcSAllan Jude * ZSTD_c_useRowMatchFinder 424a0483764SConrad Meyer * Because they are not stable, it's necessary to define ZSTD_STATIC_LINKING_ONLY to access them. 425a0483764SConrad Meyer * note : never ever use experimentalParam? names directly; 426a0483764SConrad Meyer * also, the enums values themselves are unstable and can still change. 427a0483764SConrad Meyer */ 428a0483764SConrad Meyer ZSTD_c_experimentalParam1=500, 429a0483764SConrad Meyer ZSTD_c_experimentalParam2=10, 430a0483764SConrad Meyer ZSTD_c_experimentalParam3=1000, 4312b9c00cbSConrad Meyer ZSTD_c_experimentalParam4=1001, 4322b9c00cbSConrad Meyer ZSTD_c_experimentalParam5=1002, 4334d3f1eafSConrad Meyer ZSTD_c_experimentalParam6=1003, 434f7cd7fe5SConrad Meyer ZSTD_c_experimentalParam7=1004, 435f7cd7fe5SConrad Meyer ZSTD_c_experimentalParam8=1005, 436f7cd7fe5SConrad Meyer ZSTD_c_experimentalParam9=1006, 437f7cd7fe5SConrad Meyer ZSTD_c_experimentalParam10=1007, 438f7cd7fe5SConrad Meyer ZSTD_c_experimentalParam11=1008, 439*5ff13fbcSAllan Jude ZSTD_c_experimentalParam12=1009, 440*5ff13fbcSAllan Jude ZSTD_c_experimentalParam13=1010, 441*5ff13fbcSAllan Jude ZSTD_c_experimentalParam14=1011, 442*5ff13fbcSAllan Jude ZSTD_c_experimentalParam15=1012 443a0483764SConrad Meyer } ZSTD_cParameter; 444a0483764SConrad Meyer 445a0483764SConrad Meyer typedef struct { 446a0483764SConrad Meyer size_t error; 447a0483764SConrad Meyer int lowerBound; 448a0483764SConrad Meyer int upperBound; 449a0483764SConrad Meyer } ZSTD_bounds; 450a0483764SConrad Meyer 451a0483764SConrad Meyer /*! ZSTD_cParam_getBounds() : 452a0483764SConrad Meyer * All parameters must belong to an interval with lower and upper bounds, 453a0483764SConrad Meyer * otherwise they will either trigger an error or be automatically clamped. 454a0483764SConrad Meyer * @return : a structure, ZSTD_bounds, which contains 455a0483764SConrad Meyer * - an error status field, which must be tested using ZSTD_isError() 456a0483764SConrad Meyer * - lower and upper bounds, both inclusive 457a0483764SConrad Meyer */ 458a0483764SConrad Meyer ZSTDLIB_API ZSTD_bounds ZSTD_cParam_getBounds(ZSTD_cParameter cParam); 459a0483764SConrad Meyer 460a0483764SConrad Meyer /*! ZSTD_CCtx_setParameter() : 461a0483764SConrad Meyer * Set one compression parameter, selected by enum ZSTD_cParameter. 462a0483764SConrad Meyer * All parameters have valid bounds. Bounds can be queried using ZSTD_cParam_getBounds(). 463a0483764SConrad Meyer * Providing a value beyond bound will either clamp it, or trigger an error (depending on parameter). 464a0483764SConrad Meyer * Setting a parameter is generally only possible during frame initialization (before starting compression). 465a0483764SConrad Meyer * Exception : when using multi-threading mode (nbWorkers >= 1), 466a0483764SConrad Meyer * the following parameters can be updated _during_ compression (within same frame): 467a0483764SConrad Meyer * => compressionLevel, hashLog, chainLog, searchLog, minMatch, targetLength and strategy. 468a0483764SConrad Meyer * new parameters will be active for next job only (after a flush()). 469a0483764SConrad Meyer * @return : an error code (which can be tested using ZSTD_isError()). 470a0483764SConrad Meyer */ 471a0483764SConrad Meyer ZSTDLIB_API size_t ZSTD_CCtx_setParameter(ZSTD_CCtx* cctx, ZSTD_cParameter param, int value); 472a0483764SConrad Meyer 473a0483764SConrad Meyer /*! ZSTD_CCtx_setPledgedSrcSize() : 474a0483764SConrad Meyer * Total input data size to be compressed as a single frame. 475a0483764SConrad Meyer * Value will be written in frame header, unless if explicitly forbidden using ZSTD_c_contentSizeFlag. 476a0483764SConrad Meyer * This value will also be controlled at end of frame, and trigger an error if not respected. 477a0483764SConrad Meyer * @result : 0, or an error code (which can be tested with ZSTD_isError()). 478a0483764SConrad Meyer * Note 1 : pledgedSrcSize==0 actually means zero, aka an empty frame. 479a0483764SConrad Meyer * In order to mean "unknown content size", pass constant ZSTD_CONTENTSIZE_UNKNOWN. 480a0483764SConrad Meyer * ZSTD_CONTENTSIZE_UNKNOWN is default value for any new frame. 481a0483764SConrad Meyer * Note 2 : pledgedSrcSize is only valid once, for the next frame. 482a0483764SConrad Meyer * It's discarded at the end of the frame, and replaced by ZSTD_CONTENTSIZE_UNKNOWN. 483a0483764SConrad Meyer * Note 3 : Whenever all input data is provided and consumed in a single round, 484a0483764SConrad Meyer * for example with ZSTD_compress2(), 485a0483764SConrad Meyer * or invoking immediately ZSTD_compressStream2(,,,ZSTD_e_end), 4862b9c00cbSConrad Meyer * this value is automatically overridden by srcSize instead. 487a0483764SConrad Meyer */ 488a0483764SConrad Meyer ZSTDLIB_API size_t ZSTD_CCtx_setPledgedSrcSize(ZSTD_CCtx* cctx, unsigned long long pledgedSrcSize); 489a0483764SConrad Meyer 4902b9c00cbSConrad Meyer typedef enum { 4912b9c00cbSConrad Meyer ZSTD_reset_session_only = 1, 4922b9c00cbSConrad Meyer ZSTD_reset_parameters = 2, 4932b9c00cbSConrad Meyer ZSTD_reset_session_and_parameters = 3 4942b9c00cbSConrad Meyer } ZSTD_ResetDirective; 4952b9c00cbSConrad Meyer 4962b9c00cbSConrad Meyer /*! ZSTD_CCtx_reset() : 4972b9c00cbSConrad Meyer * There are 2 different things that can be reset, independently or jointly : 4982b9c00cbSConrad Meyer * - The session : will stop compressing current frame, and make CCtx ready to start a new one. 4992b9c00cbSConrad Meyer * Useful after an error, or to interrupt any ongoing compression. 5002b9c00cbSConrad Meyer * Any internal data not yet flushed is cancelled. 5012b9c00cbSConrad Meyer * Compression parameters and dictionary remain unchanged. 5022b9c00cbSConrad Meyer * They will be used to compress next frame. 5032b9c00cbSConrad Meyer * Resetting session never fails. 5042b9c00cbSConrad Meyer * - The parameters : changes all parameters back to "default". 5052b9c00cbSConrad Meyer * This removes any reference to any dictionary too. 5062b9c00cbSConrad Meyer * Parameters can only be changed between 2 sessions (i.e. no compression is currently ongoing) 5072b9c00cbSConrad Meyer * otherwise the reset fails, and function returns an error value (which can be tested using ZSTD_isError()) 5082b9c00cbSConrad Meyer * - Both : similar to resetting the session, followed by resetting parameters. 5092b9c00cbSConrad Meyer */ 5102b9c00cbSConrad Meyer ZSTDLIB_API size_t ZSTD_CCtx_reset(ZSTD_CCtx* cctx, ZSTD_ResetDirective reset); 5112b9c00cbSConrad Meyer 5122b9c00cbSConrad Meyer /*! ZSTD_compress2() : 5132b9c00cbSConrad Meyer * Behave the same as ZSTD_compressCCtx(), but compression parameters are set using the advanced API. 5142b9c00cbSConrad Meyer * ZSTD_compress2() always starts a new frame. 5152b9c00cbSConrad Meyer * Should cctx hold data from a previously unfinished frame, everything about it is forgotten. 5162b9c00cbSConrad Meyer * - Compression parameters are pushed into CCtx before starting compression, using ZSTD_CCtx_set*() 5172b9c00cbSConrad Meyer * - The function is always blocking, returns when compression is completed. 5182b9c00cbSConrad Meyer * Hint : compression runs faster if `dstCapacity` >= `ZSTD_compressBound(srcSize)`. 5192b9c00cbSConrad Meyer * @return : compressed size written into `dst` (<= `dstCapacity), 5202b9c00cbSConrad Meyer * or an error code if it fails (which can be tested using ZSTD_isError()). 5212b9c00cbSConrad Meyer */ 5222b9c00cbSConrad Meyer ZSTDLIB_API size_t ZSTD_compress2( ZSTD_CCtx* cctx, 5232b9c00cbSConrad Meyer void* dst, size_t dstCapacity, 5242b9c00cbSConrad Meyer const void* src, size_t srcSize); 5252b9c00cbSConrad Meyer 5262b9c00cbSConrad Meyer 527*5ff13fbcSAllan Jude /*********************************************** 528*5ff13fbcSAllan Jude * Advanced decompression API (Requires v1.4.0+) 529*5ff13fbcSAllan Jude ************************************************/ 5302b9c00cbSConrad Meyer 5312b9c00cbSConrad Meyer /* The advanced API pushes parameters one by one into an existing DCtx context. 5322b9c00cbSConrad Meyer * Parameters are sticky, and remain valid for all following frames 5332b9c00cbSConrad Meyer * using the same DCtx context. 5342b9c00cbSConrad Meyer * It's possible to reset parameters to default values using ZSTD_DCtx_reset(). 5352b9c00cbSConrad Meyer * Note : This API is compatible with existing ZSTD_decompressDCtx() and ZSTD_decompressStream(). 5362b9c00cbSConrad Meyer * Therefore, no new decompression function is necessary. 5372b9c00cbSConrad Meyer */ 5382b9c00cbSConrad Meyer 5392b9c00cbSConrad Meyer typedef enum { 5402b9c00cbSConrad Meyer 5412b9c00cbSConrad Meyer ZSTD_d_windowLogMax=100, /* Select a size limit (in power of 2) beyond which 5422b9c00cbSConrad Meyer * the streaming API will refuse to allocate memory buffer 5432b9c00cbSConrad Meyer * in order to protect the host from unreasonable memory requirements. 5442b9c00cbSConrad Meyer * This parameter is only useful in streaming mode, since no internal buffer is allocated in single-pass mode. 5452b9c00cbSConrad Meyer * By default, a decompression context accepts window sizes <= (1 << ZSTD_WINDOWLOG_LIMIT_DEFAULT). 5462b9c00cbSConrad Meyer * Special: value 0 means "use default maximum windowLog". */ 5472b9c00cbSConrad Meyer 5482b9c00cbSConrad Meyer /* note : additional experimental parameters are also available 5492b9c00cbSConrad Meyer * within the experimental section of the API. 5502b9c00cbSConrad Meyer * At the time of this writing, they include : 55137f1f268SConrad Meyer * ZSTD_d_format 55237f1f268SConrad Meyer * ZSTD_d_stableOutBuffer 553f7cd7fe5SConrad Meyer * ZSTD_d_forceIgnoreChecksum 554*5ff13fbcSAllan Jude * ZSTD_d_refMultipleDDicts 5552b9c00cbSConrad Meyer * Because they are not stable, it's necessary to define ZSTD_STATIC_LINKING_ONLY to access them. 5562b9c00cbSConrad Meyer * note : never ever use experimentalParam? names directly 5572b9c00cbSConrad Meyer */ 55837f1f268SConrad Meyer ZSTD_d_experimentalParam1=1000, 559f7cd7fe5SConrad Meyer ZSTD_d_experimentalParam2=1001, 560*5ff13fbcSAllan Jude ZSTD_d_experimentalParam3=1002, 561*5ff13fbcSAllan Jude ZSTD_d_experimentalParam4=1003 5622b9c00cbSConrad Meyer 5632b9c00cbSConrad Meyer } ZSTD_dParameter; 5642b9c00cbSConrad Meyer 5652b9c00cbSConrad Meyer /*! ZSTD_dParam_getBounds() : 5662b9c00cbSConrad Meyer * All parameters must belong to an interval with lower and upper bounds, 5672b9c00cbSConrad Meyer * otherwise they will either trigger an error or be automatically clamped. 5682b9c00cbSConrad Meyer * @return : a structure, ZSTD_bounds, which contains 5692b9c00cbSConrad Meyer * - an error status field, which must be tested using ZSTD_isError() 5702b9c00cbSConrad Meyer * - both lower and upper bounds, inclusive 5712b9c00cbSConrad Meyer */ 5722b9c00cbSConrad Meyer ZSTDLIB_API ZSTD_bounds ZSTD_dParam_getBounds(ZSTD_dParameter dParam); 5732b9c00cbSConrad Meyer 5742b9c00cbSConrad Meyer /*! ZSTD_DCtx_setParameter() : 5752b9c00cbSConrad Meyer * Set one compression parameter, selected by enum ZSTD_dParameter. 5762b9c00cbSConrad Meyer * All parameters have valid bounds. Bounds can be queried using ZSTD_dParam_getBounds(). 5772b9c00cbSConrad Meyer * Providing a value beyond bound will either clamp it, or trigger an error (depending on parameter). 5782b9c00cbSConrad Meyer * Setting a parameter is only possible during frame initialization (before starting decompression). 5792b9c00cbSConrad Meyer * @return : 0, or an error code (which can be tested using ZSTD_isError()). 5802b9c00cbSConrad Meyer */ 5812b9c00cbSConrad Meyer ZSTDLIB_API size_t ZSTD_DCtx_setParameter(ZSTD_DCtx* dctx, ZSTD_dParameter param, int value); 5822b9c00cbSConrad Meyer 5832b9c00cbSConrad Meyer /*! ZSTD_DCtx_reset() : 5842b9c00cbSConrad Meyer * Return a DCtx to clean state. 5852b9c00cbSConrad Meyer * Session and parameters can be reset jointly or separately. 5862b9c00cbSConrad Meyer * Parameters can only be reset when no active frame is being decompressed. 5872b9c00cbSConrad Meyer * @return : 0, or an error code, which can be tested with ZSTD_isError() 5882b9c00cbSConrad Meyer */ 5892b9c00cbSConrad Meyer ZSTDLIB_API size_t ZSTD_DCtx_reset(ZSTD_DCtx* dctx, ZSTD_ResetDirective reset); 5902b9c00cbSConrad Meyer 5912b9c00cbSConrad Meyer 5922b9c00cbSConrad Meyer /**************************** 5932b9c00cbSConrad Meyer * Streaming 5942b9c00cbSConrad Meyer ****************************/ 5952b9c00cbSConrad Meyer 5962b9c00cbSConrad Meyer typedef struct ZSTD_inBuffer_s { 5972b9c00cbSConrad Meyer const void* src; /**< start of input buffer */ 5982b9c00cbSConrad Meyer size_t size; /**< size of input buffer */ 5992b9c00cbSConrad Meyer size_t pos; /**< position where reading stopped. Will be updated. Necessarily 0 <= pos <= size */ 6002b9c00cbSConrad Meyer } ZSTD_inBuffer; 6012b9c00cbSConrad Meyer 6022b9c00cbSConrad Meyer typedef struct ZSTD_outBuffer_s { 6032b9c00cbSConrad Meyer void* dst; /**< start of output buffer */ 6042b9c00cbSConrad Meyer size_t size; /**< size of output buffer */ 6052b9c00cbSConrad Meyer size_t pos; /**< position where writing stopped. Will be updated. Necessarily 0 <= pos <= size */ 6062b9c00cbSConrad Meyer } ZSTD_outBuffer; 6072b9c00cbSConrad Meyer 6082b9c00cbSConrad Meyer 6092b9c00cbSConrad Meyer 6102b9c00cbSConrad Meyer /*-*********************************************************************** 6112b9c00cbSConrad Meyer * Streaming compression - HowTo 6122b9c00cbSConrad Meyer * 6132b9c00cbSConrad Meyer * A ZSTD_CStream object is required to track streaming operation. 6142b9c00cbSConrad Meyer * Use ZSTD_createCStream() and ZSTD_freeCStream() to create/release resources. 6152b9c00cbSConrad Meyer * ZSTD_CStream objects can be reused multiple times on consecutive compression operations. 6162b9c00cbSConrad Meyer * It is recommended to re-use ZSTD_CStream since it will play nicer with system's memory, by re-using already allocated memory. 6172b9c00cbSConrad Meyer * 6182b9c00cbSConrad Meyer * For parallel execution, use one separate ZSTD_CStream per thread. 6192b9c00cbSConrad Meyer * 6202b9c00cbSConrad Meyer * note : since v1.3.0, ZSTD_CStream and ZSTD_CCtx are the same thing. 6212b9c00cbSConrad Meyer * 6222b9c00cbSConrad Meyer * Parameters are sticky : when starting a new compression on the same context, 6232b9c00cbSConrad Meyer * it will re-use the same sticky parameters as previous compression session. 6242b9c00cbSConrad Meyer * When in doubt, it's recommended to fully initialize the context before usage. 6252b9c00cbSConrad Meyer * Use ZSTD_CCtx_reset() to reset the context and ZSTD_CCtx_setParameter(), 6262b9c00cbSConrad Meyer * ZSTD_CCtx_setPledgedSrcSize(), or ZSTD_CCtx_loadDictionary() and friends to 6272b9c00cbSConrad Meyer * set more specific parameters, the pledged source size, or load a dictionary. 6282b9c00cbSConrad Meyer * 6292b9c00cbSConrad Meyer * Use ZSTD_compressStream2() with ZSTD_e_continue as many times as necessary to 6302b9c00cbSConrad Meyer * consume input stream. The function will automatically update both `pos` 6312b9c00cbSConrad Meyer * fields within `input` and `output`. 6322b9c00cbSConrad Meyer * Note that the function may not consume the entire input, for example, because 6332b9c00cbSConrad Meyer * the output buffer is already full, in which case `input.pos < input.size`. 6342b9c00cbSConrad Meyer * The caller must check if input has been entirely consumed. 6352b9c00cbSConrad Meyer * If not, the caller must make some room to receive more compressed data, 6362b9c00cbSConrad Meyer * and then present again remaining input data. 6372b9c00cbSConrad Meyer * note: ZSTD_e_continue is guaranteed to make some forward progress when called, 6382b9c00cbSConrad Meyer * but doesn't guarantee maximal forward progress. This is especially relevant 6392b9c00cbSConrad Meyer * when compressing with multiple threads. The call won't block if it can 6402b9c00cbSConrad Meyer * consume some input, but if it can't it will wait for some, but not all, 6412b9c00cbSConrad Meyer * output to be flushed. 6422b9c00cbSConrad Meyer * @return : provides a minimum amount of data remaining to be flushed from internal buffers 6432b9c00cbSConrad Meyer * or an error code, which can be tested using ZSTD_isError(). 6442b9c00cbSConrad Meyer * 6452b9c00cbSConrad Meyer * At any moment, it's possible to flush whatever data might remain stuck within internal buffer, 6462b9c00cbSConrad Meyer * using ZSTD_compressStream2() with ZSTD_e_flush. `output->pos` will be updated. 6472b9c00cbSConrad Meyer * Note that, if `output->size` is too small, a single invocation with ZSTD_e_flush might not be enough (return code > 0). 6482b9c00cbSConrad Meyer * In which case, make some room to receive more compressed data, and call again ZSTD_compressStream2() with ZSTD_e_flush. 6492b9c00cbSConrad Meyer * You must continue calling ZSTD_compressStream2() with ZSTD_e_flush until it returns 0, at which point you can change the 6502b9c00cbSConrad Meyer * operation. 6512b9c00cbSConrad Meyer * note: ZSTD_e_flush will flush as much output as possible, meaning when compressing with multiple threads, it will 6522b9c00cbSConrad Meyer * block until the flush is complete or the output buffer is full. 6532b9c00cbSConrad Meyer * @return : 0 if internal buffers are entirely flushed, 6542b9c00cbSConrad Meyer * >0 if some data still present within internal buffer (the value is minimal estimation of remaining size), 6552b9c00cbSConrad Meyer * or an error code, which can be tested using ZSTD_isError(). 6562b9c00cbSConrad Meyer * 6572b9c00cbSConrad Meyer * Calling ZSTD_compressStream2() with ZSTD_e_end instructs to finish a frame. 6582b9c00cbSConrad Meyer * It will perform a flush and write frame epilogue. 6592b9c00cbSConrad Meyer * The epilogue is required for decoders to consider a frame completed. 6602b9c00cbSConrad Meyer * flush operation is the same, and follows same rules as calling ZSTD_compressStream2() with ZSTD_e_flush. 6612b9c00cbSConrad Meyer * You must continue calling ZSTD_compressStream2() with ZSTD_e_end until it returns 0, at which point you are free to 6622b9c00cbSConrad Meyer * start a new frame. 6632b9c00cbSConrad Meyer * note: ZSTD_e_end will flush as much output as possible, meaning when compressing with multiple threads, it will 6642b9c00cbSConrad Meyer * block until the flush is complete or the output buffer is full. 6652b9c00cbSConrad Meyer * @return : 0 if frame fully completed and fully flushed, 6662b9c00cbSConrad Meyer * >0 if some data still present within internal buffer (the value is minimal estimation of remaining size), 6672b9c00cbSConrad Meyer * or an error code, which can be tested using ZSTD_isError(). 6682b9c00cbSConrad Meyer * 6692b9c00cbSConrad Meyer * *******************************************************************/ 6702b9c00cbSConrad Meyer 6712b9c00cbSConrad Meyer typedef ZSTD_CCtx ZSTD_CStream; /**< CCtx and CStream are now effectively same object (>= v1.3.0) */ 6722b9c00cbSConrad Meyer /* Continue to distinguish them for compatibility with older versions <= v1.2.0 */ 6732b9c00cbSConrad Meyer /*===== ZSTD_CStream management functions =====*/ 6742b9c00cbSConrad Meyer ZSTDLIB_API ZSTD_CStream* ZSTD_createCStream(void); 675*5ff13fbcSAllan Jude ZSTDLIB_API size_t ZSTD_freeCStream(ZSTD_CStream* zcs); /* accept NULL pointer */ 6762b9c00cbSConrad Meyer 6772b9c00cbSConrad Meyer /*===== Streaming compression functions =====*/ 6782b9c00cbSConrad Meyer typedef enum { 6792b9c00cbSConrad Meyer ZSTD_e_continue=0, /* collect more data, encoder decides when to output compressed result, for optimal compression ratio */ 6802b9c00cbSConrad Meyer ZSTD_e_flush=1, /* flush any data provided so far, 6812b9c00cbSConrad Meyer * it creates (at least) one new block, that can be decoded immediately on reception; 6822b9c00cbSConrad Meyer * frame will continue: any future data can still reference previously compressed data, improving compression. 6832b9c00cbSConrad Meyer * note : multithreaded compression will block to flush as much output as possible. */ 6842b9c00cbSConrad Meyer ZSTD_e_end=2 /* flush any remaining data _and_ close current frame. 6852b9c00cbSConrad Meyer * note that frame is only closed after compressed data is fully flushed (return value == 0). 6862b9c00cbSConrad Meyer * After that point, any additional data starts a new frame. 6872b9c00cbSConrad Meyer * note : each frame is independent (does not reference any content from previous frame). 6882b9c00cbSConrad Meyer : note : multithreaded compression will block to flush as much output as possible. */ 6892b9c00cbSConrad Meyer } ZSTD_EndDirective; 6902b9c00cbSConrad Meyer 691*5ff13fbcSAllan Jude /*! ZSTD_compressStream2() : Requires v1.4.0+ 6922b9c00cbSConrad Meyer * Behaves about the same as ZSTD_compressStream, with additional control on end directive. 6932b9c00cbSConrad Meyer * - Compression parameters are pushed into CCtx before starting compression, using ZSTD_CCtx_set*() 6942b9c00cbSConrad Meyer * - Compression parameters cannot be changed once compression is started (save a list of exceptions in multi-threading mode) 6952b9c00cbSConrad Meyer * - output->pos must be <= dstCapacity, input->pos must be <= srcSize 6962b9c00cbSConrad Meyer * - output->pos and input->pos will be updated. They are guaranteed to remain below their respective limit. 697f7cd7fe5SConrad Meyer * - endOp must be a valid directive 6982b9c00cbSConrad Meyer * - When nbWorkers==0 (default), function is blocking : it completes its job before returning to caller. 699f7cd7fe5SConrad Meyer * - When nbWorkers>=1, function is non-blocking : it copies a portion of input, distributes jobs to internal worker threads, flush to output whatever is available, 7002b9c00cbSConrad Meyer * and then immediately returns, just indicating that there is some data remaining to be flushed. 7012b9c00cbSConrad Meyer * The function nonetheless guarantees forward progress : it will return only after it reads or write at least 1+ byte. 7022b9c00cbSConrad Meyer * - 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. 7032b9c00cbSConrad Meyer * - @return provides a minimum amount of data remaining to be flushed from internal buffers 7042b9c00cbSConrad Meyer * or an error code, which can be tested using ZSTD_isError(). 7052b9c00cbSConrad Meyer * if @return != 0, flush is not fully completed, there is still some data left within internal buffers. 7062b9c00cbSConrad Meyer * This is useful for ZSTD_e_flush, since in this case more flushes are necessary to empty all buffers. 7072b9c00cbSConrad Meyer * For ZSTD_e_end, @return == 0 when internal buffers are fully flushed and frame is completed. 7082b9c00cbSConrad Meyer * - after a ZSTD_e_end directive, if internal buffer is not fully flushed (@return != 0), 7092b9c00cbSConrad Meyer * only ZSTD_e_end or ZSTD_e_flush operations are allowed. 7102b9c00cbSConrad Meyer * Before starting a new compression job, or changing compression parameters, 7112b9c00cbSConrad Meyer * it is required to fully flush internal buffers. 7122b9c00cbSConrad Meyer */ 7132b9c00cbSConrad Meyer ZSTDLIB_API size_t ZSTD_compressStream2( ZSTD_CCtx* cctx, 7142b9c00cbSConrad Meyer ZSTD_outBuffer* output, 7152b9c00cbSConrad Meyer ZSTD_inBuffer* input, 7162b9c00cbSConrad Meyer ZSTD_EndDirective endOp); 7172b9c00cbSConrad Meyer 7182b9c00cbSConrad Meyer 7194d3f1eafSConrad Meyer /* These buffer sizes are softly recommended. 7204d3f1eafSConrad Meyer * They are not required : ZSTD_compressStream*() happily accepts any buffer size, for both input and output. 7214d3f1eafSConrad Meyer * Respecting the recommended size just makes it a bit easier for ZSTD_compressStream*(), 7224d3f1eafSConrad Meyer * reducing the amount of memory shuffling and buffering, resulting in minor performance savings. 7234d3f1eafSConrad Meyer * 7244d3f1eafSConrad Meyer * However, note that these recommendations are from the perspective of a C caller program. 7254d3f1eafSConrad Meyer * If the streaming interface is invoked from some other language, 7264d3f1eafSConrad Meyer * especially managed ones such as Java or Go, through a foreign function interface such as jni or cgo, 7274d3f1eafSConrad Meyer * a major performance rule is to reduce crossing such interface to an absolute minimum. 7284d3f1eafSConrad Meyer * It's not rare that performance ends being spent more into the interface, rather than compression itself. 7294d3f1eafSConrad Meyer * In which cases, prefer using large buffers, as large as practical, 7304d3f1eafSConrad Meyer * for both input and output, to reduce the nb of roundtrips. 7314d3f1eafSConrad Meyer */ 7324d3f1eafSConrad Meyer ZSTDLIB_API size_t ZSTD_CStreamInSize(void); /**< recommended size for input buffer */ 7334d3f1eafSConrad Meyer ZSTDLIB_API size_t ZSTD_CStreamOutSize(void); /**< recommended size for output buffer. Guarantee to successfully flush at least one complete compressed block. */ 7344d3f1eafSConrad Meyer 7354d3f1eafSConrad Meyer 7364d3f1eafSConrad Meyer /* ***************************************************************************** 737*5ff13fbcSAllan Jude * This following is a legacy streaming API, available since v1.0+ . 7384d3f1eafSConrad Meyer * It can be replaced by ZSTD_CCtx_reset() and ZSTD_compressStream2(). 7394d3f1eafSConrad Meyer * It is redundant, but remains fully supported. 740*5ff13fbcSAllan Jude * Streaming in combination with advanced parameters and dictionary compression 741*5ff13fbcSAllan Jude * can only be used through the new API. 7422b9c00cbSConrad Meyer ******************************************************************************/ 7432b9c00cbSConrad Meyer 7444d3f1eafSConrad Meyer /*! 7452b9c00cbSConrad Meyer * Equivalent to: 7462b9c00cbSConrad Meyer * 7472b9c00cbSConrad Meyer * ZSTD_CCtx_reset(zcs, ZSTD_reset_session_only); 7482b9c00cbSConrad Meyer * ZSTD_CCtx_refCDict(zcs, NULL); // clear the dictionary (if any) 7492b9c00cbSConrad Meyer * ZSTD_CCtx_setParameter(zcs, ZSTD_c_compressionLevel, compressionLevel); 7502b9c00cbSConrad Meyer */ 7512b9c00cbSConrad Meyer ZSTDLIB_API size_t ZSTD_initCStream(ZSTD_CStream* zcs, int compressionLevel); 7524d3f1eafSConrad Meyer /*! 7532b9c00cbSConrad Meyer * Alternative for ZSTD_compressStream2(zcs, output, input, ZSTD_e_continue). 7542b9c00cbSConrad Meyer * NOTE: The return value is different. ZSTD_compressStream() returns a hint for 7552b9c00cbSConrad Meyer * the next read size (if non-zero and not an error). ZSTD_compressStream2() 7564d3f1eafSConrad Meyer * returns the minimum nb of bytes left to flush (if non-zero and not an error). 7572b9c00cbSConrad Meyer */ 7582b9c00cbSConrad Meyer ZSTDLIB_API size_t ZSTD_compressStream(ZSTD_CStream* zcs, ZSTD_outBuffer* output, ZSTD_inBuffer* input); 7594d3f1eafSConrad Meyer /*! Equivalent to ZSTD_compressStream2(zcs, output, &emptyInput, ZSTD_e_flush). */ 7602b9c00cbSConrad Meyer ZSTDLIB_API size_t ZSTD_flushStream(ZSTD_CStream* zcs, ZSTD_outBuffer* output); 7614d3f1eafSConrad Meyer /*! Equivalent to ZSTD_compressStream2(zcs, output, &emptyInput, ZSTD_e_end). */ 7622b9c00cbSConrad Meyer ZSTDLIB_API size_t ZSTD_endStream(ZSTD_CStream* zcs, ZSTD_outBuffer* output); 7632b9c00cbSConrad Meyer 7642b9c00cbSConrad Meyer 7652b9c00cbSConrad Meyer /*-*************************************************************************** 7662b9c00cbSConrad Meyer * Streaming decompression - HowTo 7672b9c00cbSConrad Meyer * 7682b9c00cbSConrad Meyer * A ZSTD_DStream object is required to track streaming operations. 7692b9c00cbSConrad Meyer * Use ZSTD_createDStream() and ZSTD_freeDStream() to create/release resources. 7702b9c00cbSConrad Meyer * ZSTD_DStream objects can be re-used multiple times. 7712b9c00cbSConrad Meyer * 7722b9c00cbSConrad Meyer * Use ZSTD_initDStream() to start a new decompression operation. 7732b9c00cbSConrad Meyer * @return : recommended first input size 7742b9c00cbSConrad Meyer * Alternatively, use advanced API to set specific properties. 7752b9c00cbSConrad Meyer * 7762b9c00cbSConrad Meyer * Use ZSTD_decompressStream() repetitively to consume your input. 7772b9c00cbSConrad Meyer * The function will update both `pos` fields. 7782b9c00cbSConrad Meyer * If `input.pos < input.size`, some input has not been consumed. 7792b9c00cbSConrad Meyer * It's up to the caller to present again remaining data. 7802b9c00cbSConrad Meyer * The function tries to flush all data decoded immediately, respecting output buffer size. 7812b9c00cbSConrad Meyer * If `output.pos < output.size`, decoder has flushed everything it could. 7822b9c00cbSConrad Meyer * But if `output.pos == output.size`, there might be some data left within internal buffers., 7832b9c00cbSConrad Meyer * In which case, call ZSTD_decompressStream() again to flush whatever remains in the buffer. 7842b9c00cbSConrad Meyer * Note : with no additional input provided, amount of data flushed is necessarily <= ZSTD_BLOCKSIZE_MAX. 7852b9c00cbSConrad Meyer * @return : 0 when a frame is completely decoded and fully flushed, 7862b9c00cbSConrad Meyer * or an error code, which can be tested using ZSTD_isError(), 7872b9c00cbSConrad Meyer * or any other value > 0, which means there is still some decoding or flushing to do to complete current frame : 7882b9c00cbSConrad Meyer * the return value is a suggested next input size (just a hint for better latency) 7892b9c00cbSConrad Meyer * that will never request more than the remaining frame size. 7902b9c00cbSConrad Meyer * *******************************************************************************/ 7912b9c00cbSConrad Meyer 7922b9c00cbSConrad Meyer typedef ZSTD_DCtx ZSTD_DStream; /**< DCtx and DStream are now effectively same object (>= v1.3.0) */ 7932b9c00cbSConrad Meyer /* For compatibility with versions <= v1.2.0, prefer differentiating them. */ 7942b9c00cbSConrad Meyer /*===== ZSTD_DStream management functions =====*/ 7952b9c00cbSConrad Meyer ZSTDLIB_API ZSTD_DStream* ZSTD_createDStream(void); 796*5ff13fbcSAllan Jude ZSTDLIB_API size_t ZSTD_freeDStream(ZSTD_DStream* zds); /* accept NULL pointer */ 7972b9c00cbSConrad Meyer 7982b9c00cbSConrad Meyer /*===== Streaming decompression functions =====*/ 7992b9c00cbSConrad Meyer 8002b9c00cbSConrad Meyer /* This function is redundant with the advanced API and equivalent to: 8012b9c00cbSConrad Meyer * 80237f1f268SConrad Meyer * ZSTD_DCtx_reset(zds, ZSTD_reset_session_only); 8032b9c00cbSConrad Meyer * ZSTD_DCtx_refDDict(zds, NULL); 8042b9c00cbSConrad Meyer */ 8052b9c00cbSConrad Meyer ZSTDLIB_API size_t ZSTD_initDStream(ZSTD_DStream* zds); 8062b9c00cbSConrad Meyer 8072b9c00cbSConrad Meyer ZSTDLIB_API size_t ZSTD_decompressStream(ZSTD_DStream* zds, ZSTD_outBuffer* output, ZSTD_inBuffer* input); 8082b9c00cbSConrad Meyer 8092b9c00cbSConrad Meyer ZSTDLIB_API size_t ZSTD_DStreamInSize(void); /*!< recommended size for input buffer */ 8102b9c00cbSConrad Meyer ZSTDLIB_API size_t ZSTD_DStreamOutSize(void); /*!< recommended size for output buffer. Guarantee to successfully flush at least one complete block in all circumstances. */ 8112b9c00cbSConrad Meyer 8122b9c00cbSConrad Meyer 8132b9c00cbSConrad Meyer /************************** 8142b9c00cbSConrad Meyer * Simple dictionary API 8152b9c00cbSConrad Meyer ***************************/ 8162b9c00cbSConrad Meyer /*! ZSTD_compress_usingDict() : 8172b9c00cbSConrad Meyer * Compression at an explicit compression level using a Dictionary. 8182b9c00cbSConrad Meyer * A dictionary can be any arbitrary data segment (also called a prefix), 819*5ff13fbcSAllan Jude * or a buffer with specified information (see zdict.h). 8202b9c00cbSConrad Meyer * Note : This function loads the dictionary, resulting in significant startup delay. 8212b9c00cbSConrad Meyer * It's intended for a dictionary used only once. 8222b9c00cbSConrad Meyer * Note 2 : When `dict == NULL || dictSize < 8` no dictionary is used. */ 8232b9c00cbSConrad Meyer ZSTDLIB_API size_t ZSTD_compress_usingDict(ZSTD_CCtx* ctx, 8242b9c00cbSConrad Meyer void* dst, size_t dstCapacity, 8252b9c00cbSConrad Meyer const void* src, size_t srcSize, 8262b9c00cbSConrad Meyer const void* dict,size_t dictSize, 8272b9c00cbSConrad Meyer int compressionLevel); 8282b9c00cbSConrad Meyer 8292b9c00cbSConrad Meyer /*! ZSTD_decompress_usingDict() : 8302b9c00cbSConrad Meyer * Decompression using a known Dictionary. 8312b9c00cbSConrad Meyer * Dictionary must be identical to the one used during compression. 8322b9c00cbSConrad Meyer * Note : This function loads the dictionary, resulting in significant startup delay. 8332b9c00cbSConrad Meyer * It's intended for a dictionary used only once. 8342b9c00cbSConrad Meyer * Note : When `dict == NULL || dictSize < 8` no dictionary is used. */ 8352b9c00cbSConrad Meyer ZSTDLIB_API size_t ZSTD_decompress_usingDict(ZSTD_DCtx* dctx, 8362b9c00cbSConrad Meyer void* dst, size_t dstCapacity, 8372b9c00cbSConrad Meyer const void* src, size_t srcSize, 8382b9c00cbSConrad Meyer const void* dict,size_t dictSize); 8392b9c00cbSConrad Meyer 8402b9c00cbSConrad Meyer 8412b9c00cbSConrad Meyer /*********************************** 8422b9c00cbSConrad Meyer * Bulk processing dictionary API 8432b9c00cbSConrad Meyer **********************************/ 8442b9c00cbSConrad Meyer typedef struct ZSTD_CDict_s ZSTD_CDict; 8452b9c00cbSConrad Meyer 8462b9c00cbSConrad Meyer /*! ZSTD_createCDict() : 8479cbefe25SConrad Meyer * When compressing multiple messages or blocks using the same dictionary, 8489cbefe25SConrad Meyer * it's recommended to digest the dictionary only once, since it's a costly operation. 8499cbefe25SConrad Meyer * ZSTD_createCDict() will create a state from digesting a dictionary. 8509cbefe25SConrad Meyer * The resulting state can be used for future compression operations with very limited startup cost. 8512b9c00cbSConrad Meyer * ZSTD_CDict can be created once and shared by multiple threads concurrently, since its usage is read-only. 8529cbefe25SConrad Meyer * @dictBuffer can be released after ZSTD_CDict creation, because its content is copied within CDict. 8539cbefe25SConrad Meyer * Note 1 : Consider experimental function `ZSTD_createCDict_byReference()` if you prefer to not duplicate @dictBuffer content. 8549cbefe25SConrad Meyer * Note 2 : A ZSTD_CDict can be created from an empty @dictBuffer, 8559cbefe25SConrad Meyer * in which case the only thing that it transports is the @compressionLevel. 8569cbefe25SConrad Meyer * This can be useful in a pipeline featuring ZSTD_compress_usingCDict() exclusively, 8579cbefe25SConrad Meyer * expecting a ZSTD_CDict parameter with any data, including those without a known dictionary. */ 8582b9c00cbSConrad Meyer ZSTDLIB_API ZSTD_CDict* ZSTD_createCDict(const void* dictBuffer, size_t dictSize, 8592b9c00cbSConrad Meyer int compressionLevel); 8602b9c00cbSConrad Meyer 8612b9c00cbSConrad Meyer /*! ZSTD_freeCDict() : 862*5ff13fbcSAllan Jude * Function frees memory allocated by ZSTD_createCDict(). 863*5ff13fbcSAllan Jude * If a NULL pointer is passed, no operation is performed. */ 8642b9c00cbSConrad Meyer ZSTDLIB_API size_t ZSTD_freeCDict(ZSTD_CDict* CDict); 8652b9c00cbSConrad Meyer 8662b9c00cbSConrad Meyer /*! ZSTD_compress_usingCDict() : 8672b9c00cbSConrad Meyer * Compression using a digested Dictionary. 8682b9c00cbSConrad Meyer * Recommended when same dictionary is used multiple times. 8692b9c00cbSConrad Meyer * Note : compression level is _decided at dictionary creation time_, 8702b9c00cbSConrad Meyer * and frame parameters are hardcoded (dictID=yes, contentSize=yes, checksum=no) */ 8712b9c00cbSConrad Meyer ZSTDLIB_API size_t ZSTD_compress_usingCDict(ZSTD_CCtx* cctx, 8722b9c00cbSConrad Meyer void* dst, size_t dstCapacity, 8732b9c00cbSConrad Meyer const void* src, size_t srcSize, 8742b9c00cbSConrad Meyer const ZSTD_CDict* cdict); 8752b9c00cbSConrad Meyer 8762b9c00cbSConrad Meyer 8772b9c00cbSConrad Meyer typedef struct ZSTD_DDict_s ZSTD_DDict; 8782b9c00cbSConrad Meyer 8792b9c00cbSConrad Meyer /*! ZSTD_createDDict() : 8802b9c00cbSConrad Meyer * Create a digested dictionary, ready to start decompression operation without startup delay. 8812b9c00cbSConrad Meyer * dictBuffer can be released after DDict creation, as its content is copied inside DDict. */ 8822b9c00cbSConrad Meyer ZSTDLIB_API ZSTD_DDict* ZSTD_createDDict(const void* dictBuffer, size_t dictSize); 8832b9c00cbSConrad Meyer 8842b9c00cbSConrad Meyer /*! ZSTD_freeDDict() : 885*5ff13fbcSAllan Jude * Function frees memory allocated with ZSTD_createDDict() 886*5ff13fbcSAllan Jude * If a NULL pointer is passed, no operation is performed. */ 8872b9c00cbSConrad Meyer ZSTDLIB_API size_t ZSTD_freeDDict(ZSTD_DDict* ddict); 8882b9c00cbSConrad Meyer 8892b9c00cbSConrad Meyer /*! ZSTD_decompress_usingDDict() : 8902b9c00cbSConrad Meyer * Decompression using a digested Dictionary. 8912b9c00cbSConrad Meyer * Recommended when same dictionary is used multiple times. */ 8922b9c00cbSConrad Meyer ZSTDLIB_API size_t ZSTD_decompress_usingDDict(ZSTD_DCtx* dctx, 8932b9c00cbSConrad Meyer void* dst, size_t dstCapacity, 8942b9c00cbSConrad Meyer const void* src, size_t srcSize, 8952b9c00cbSConrad Meyer const ZSTD_DDict* ddict); 8962b9c00cbSConrad Meyer 8972b9c00cbSConrad Meyer 8982b9c00cbSConrad Meyer /******************************** 8992b9c00cbSConrad Meyer * Dictionary helper functions 9002b9c00cbSConrad Meyer *******************************/ 9012b9c00cbSConrad Meyer 902*5ff13fbcSAllan Jude /*! ZSTD_getDictID_fromDict() : Requires v1.4.0+ 9032b9c00cbSConrad Meyer * Provides the dictID stored within dictionary. 9042b9c00cbSConrad Meyer * if @return == 0, the dictionary is not conformant with Zstandard specification. 9052b9c00cbSConrad Meyer * It can still be loaded, but as a content-only dictionary. */ 9062b9c00cbSConrad Meyer ZSTDLIB_API unsigned ZSTD_getDictID_fromDict(const void* dict, size_t dictSize); 9072b9c00cbSConrad Meyer 908*5ff13fbcSAllan Jude /*! ZSTD_getDictID_fromCDict() : Requires v1.5.0+ 909*5ff13fbcSAllan Jude * Provides the dictID of the dictionary loaded into `cdict`. 910*5ff13fbcSAllan Jude * If @return == 0, the dictionary is not conformant to Zstandard specification, or empty. 911*5ff13fbcSAllan Jude * Non-conformant dictionaries can still be loaded, but as content-only dictionaries. */ 912*5ff13fbcSAllan Jude ZSTDLIB_API unsigned ZSTD_getDictID_fromCDict(const ZSTD_CDict* cdict); 913*5ff13fbcSAllan Jude 914*5ff13fbcSAllan Jude /*! ZSTD_getDictID_fromDDict() : Requires v1.4.0+ 9152b9c00cbSConrad Meyer * Provides the dictID of the dictionary loaded into `ddict`. 9162b9c00cbSConrad Meyer * If @return == 0, the dictionary is not conformant to Zstandard specification, or empty. 9172b9c00cbSConrad Meyer * Non-conformant dictionaries can still be loaded, but as content-only dictionaries. */ 9182b9c00cbSConrad Meyer ZSTDLIB_API unsigned ZSTD_getDictID_fromDDict(const ZSTD_DDict* ddict); 9192b9c00cbSConrad Meyer 920*5ff13fbcSAllan Jude /*! ZSTD_getDictID_fromFrame() : Requires v1.4.0+ 9212b9c00cbSConrad Meyer * Provides the dictID required to decompressed the frame stored within `src`. 9222b9c00cbSConrad Meyer * If @return == 0, the dictID could not be decoded. 9232b9c00cbSConrad Meyer * This could for one of the following reasons : 9242b9c00cbSConrad Meyer * - The frame does not require a dictionary to be decoded (most common case). 9252b9c00cbSConrad Meyer * - The frame was built with dictID intentionally removed. Whatever dictionary is necessary is a hidden information. 9262b9c00cbSConrad Meyer * Note : this use case also happens when using a non-conformant dictionary. 9272b9c00cbSConrad Meyer * - `srcSize` is too small, and as a result, the frame header could not be decoded (only possible if `srcSize < ZSTD_FRAMEHEADERSIZE_MAX`). 9282b9c00cbSConrad Meyer * - This is not a Zstandard frame. 9292b9c00cbSConrad Meyer * When identifying the exact failure cause, it's possible to use ZSTD_getFrameHeader(), which will provide a more precise error code. */ 9302b9c00cbSConrad Meyer ZSTDLIB_API unsigned ZSTD_getDictID_fromFrame(const void* src, size_t srcSize); 9312b9c00cbSConrad Meyer 9322b9c00cbSConrad Meyer 9332b9c00cbSConrad Meyer /******************************************************************************* 934*5ff13fbcSAllan Jude * Advanced dictionary and prefix API (Requires v1.4.0+) 9352b9c00cbSConrad Meyer * 9362b9c00cbSConrad Meyer * This API allows dictionaries to be used with ZSTD_compress2(), 937*5ff13fbcSAllan Jude * ZSTD_compressStream2(), and ZSTD_decompressDCtx(). Dictionaries are sticky, and 9382b9c00cbSConrad Meyer * only reset with the context is reset with ZSTD_reset_parameters or 9392b9c00cbSConrad Meyer * ZSTD_reset_session_and_parameters. Prefixes are single-use. 9402b9c00cbSConrad Meyer ******************************************************************************/ 9412b9c00cbSConrad Meyer 9422b9c00cbSConrad Meyer 943*5ff13fbcSAllan Jude /*! ZSTD_CCtx_loadDictionary() : Requires v1.4.0+ 944a0483764SConrad Meyer * Create an internal CDict from `dict` buffer. 945a0483764SConrad Meyer * Decompression will have to use same dictionary. 946a0483764SConrad Meyer * @result : 0, or an error code (which can be tested with ZSTD_isError()). 947a0483764SConrad Meyer * Special: Loading a NULL (or 0-size) dictionary invalidates previous dictionary, 948a0483764SConrad Meyer * meaning "return to no-dictionary mode". 949a0483764SConrad Meyer * Note 1 : Dictionary is sticky, it will be used for all future compressed frames. 950a0483764SConrad Meyer * To return to "no-dictionary" situation, load a NULL dictionary (or reset parameters). 951a0483764SConrad Meyer * Note 2 : Loading a dictionary involves building tables. 952a0483764SConrad Meyer * It's also a CPU consuming operation, with non-negligible impact on latency. 953a0483764SConrad Meyer * Tables are dependent on compression parameters, and for this reason, 954a0483764SConrad Meyer * compression parameters can no longer be changed after loading a dictionary. 955a0483764SConrad Meyer * Note 3 :`dict` content will be copied internally. 956a0483764SConrad Meyer * Use experimental ZSTD_CCtx_loadDictionary_byReference() to reference content instead. 957a0483764SConrad Meyer * In such a case, dictionary buffer must outlive its users. 958a0483764SConrad Meyer * Note 4 : Use ZSTD_CCtx_loadDictionary_advanced() 959a0483764SConrad Meyer * to precisely select how dictionary content must be interpreted. */ 960a0483764SConrad Meyer ZSTDLIB_API size_t ZSTD_CCtx_loadDictionary(ZSTD_CCtx* cctx, const void* dict, size_t dictSize); 961a0483764SConrad Meyer 962*5ff13fbcSAllan Jude /*! ZSTD_CCtx_refCDict() : Requires v1.4.0+ 963a0483764SConrad Meyer * Reference a prepared dictionary, to be used for all next compressed frames. 964a0483764SConrad Meyer * Note that compression parameters are enforced from within CDict, 9652b9c00cbSConrad Meyer * and supersede any compression parameter previously set within CCtx. 966*5ff13fbcSAllan Jude * The parameters ignored are labelled as "superseded-by-cdict" in the ZSTD_cParameter enum docs. 9672b9c00cbSConrad Meyer * The ignored parameters will be used again if the CCtx is returned to no-dictionary mode. 968a0483764SConrad Meyer * The dictionary will remain valid for future compressed frames using same CCtx. 969a0483764SConrad Meyer * @result : 0, or an error code (which can be tested with ZSTD_isError()). 970a0483764SConrad Meyer * Special : Referencing a NULL CDict means "return to no-dictionary mode". 971a0483764SConrad Meyer * Note 1 : Currently, only one dictionary can be managed. 972a0483764SConrad Meyer * Referencing a new dictionary effectively "discards" any previous one. 973a0483764SConrad Meyer * Note 2 : CDict is just referenced, its lifetime must outlive its usage within CCtx. */ 974a0483764SConrad Meyer ZSTDLIB_API size_t ZSTD_CCtx_refCDict(ZSTD_CCtx* cctx, const ZSTD_CDict* cdict); 975a0483764SConrad Meyer 976*5ff13fbcSAllan Jude /*! ZSTD_CCtx_refPrefix() : Requires v1.4.0+ 977a0483764SConrad Meyer * Reference a prefix (single-usage dictionary) for next compressed frame. 978a0483764SConrad Meyer * A prefix is **only used once**. Tables are discarded at end of frame (ZSTD_e_end). 979a0483764SConrad Meyer * Decompression will need same prefix to properly regenerate data. 980a0483764SConrad Meyer * Compressing with a prefix is similar in outcome as performing a diff and compressing it, 981a0483764SConrad Meyer * but performs much faster, especially during decompression (compression speed is tunable with compression level). 982a0483764SConrad Meyer * @result : 0, or an error code (which can be tested with ZSTD_isError()). 983a0483764SConrad Meyer * Special: Adding any prefix (including NULL) invalidates any previous prefix or dictionary 984a0483764SConrad Meyer * Note 1 : Prefix buffer is referenced. It **must** outlive compression. 985a0483764SConrad Meyer * Its content must remain unmodified during compression. 986a0483764SConrad Meyer * Note 2 : If the intention is to diff some large src data blob with some prior version of itself, 987a0483764SConrad Meyer * ensure that the window size is large enough to contain the entire source. 988a0483764SConrad Meyer * See ZSTD_c_windowLog. 989a0483764SConrad Meyer * Note 3 : Referencing a prefix involves building tables, which are dependent on compression parameters. 990a0483764SConrad Meyer * It's a CPU consuming operation, with non-negligible impact on latency. 991a0483764SConrad Meyer * If there is a need to use the same prefix multiple times, consider loadDictionary instead. 9929cbefe25SConrad Meyer * Note 4 : By default, the prefix is interpreted as raw content (ZSTD_dct_rawContent). 993a0483764SConrad Meyer * Use experimental ZSTD_CCtx_refPrefix_advanced() to alter dictionary interpretation. */ 994a0483764SConrad Meyer ZSTDLIB_API size_t ZSTD_CCtx_refPrefix(ZSTD_CCtx* cctx, 995a0483764SConrad Meyer const void* prefix, size_t prefixSize); 996a0483764SConrad Meyer 997*5ff13fbcSAllan Jude /*! ZSTD_DCtx_loadDictionary() : Requires v1.4.0+ 998a0483764SConrad Meyer * Create an internal DDict from dict buffer, 999a0483764SConrad Meyer * to be used to decompress next frames. 1000a0483764SConrad Meyer * The dictionary remains valid for all future frames, until explicitly invalidated. 1001a0483764SConrad Meyer * @result : 0, or an error code (which can be tested with ZSTD_isError()). 1002a0483764SConrad Meyer * Special : Adding a NULL (or 0-size) dictionary invalidates any previous dictionary, 1003a0483764SConrad Meyer * meaning "return to no-dictionary mode". 1004a0483764SConrad Meyer * Note 1 : Loading a dictionary involves building tables, 1005a0483764SConrad Meyer * which has a non-negligible impact on CPU usage and latency. 1006a0483764SConrad Meyer * It's recommended to "load once, use many times", to amortize the cost 1007a0483764SConrad Meyer * Note 2 :`dict` content will be copied internally, so `dict` can be released after loading. 1008a0483764SConrad Meyer * Use ZSTD_DCtx_loadDictionary_byReference() to reference dictionary content instead. 1009a0483764SConrad Meyer * Note 3 : Use ZSTD_DCtx_loadDictionary_advanced() to take control of 1010a0483764SConrad Meyer * how dictionary content is loaded and interpreted. 1011a0483764SConrad Meyer */ 1012a0483764SConrad Meyer ZSTDLIB_API size_t ZSTD_DCtx_loadDictionary(ZSTD_DCtx* dctx, const void* dict, size_t dictSize); 1013a0483764SConrad Meyer 1014*5ff13fbcSAllan Jude /*! ZSTD_DCtx_refDDict() : Requires v1.4.0+ 1015a0483764SConrad Meyer * Reference a prepared dictionary, to be used to decompress next frames. 1016a0483764SConrad Meyer * The dictionary remains active for decompression of future frames using same DCtx. 1017*5ff13fbcSAllan Jude * 1018*5ff13fbcSAllan Jude * If called with ZSTD_d_refMultipleDDicts enabled, repeated calls of this function 1019*5ff13fbcSAllan Jude * will store the DDict references in a table, and the DDict used for decompression 1020*5ff13fbcSAllan Jude * will be determined at decompression time, as per the dict ID in the frame. 1021*5ff13fbcSAllan Jude * The memory for the table is allocated on the first call to refDDict, and can be 1022*5ff13fbcSAllan Jude * freed with ZSTD_freeDCtx(). 1023*5ff13fbcSAllan Jude * 1024a0483764SConrad Meyer * @result : 0, or an error code (which can be tested with ZSTD_isError()). 1025a0483764SConrad Meyer * Note 1 : Currently, only one dictionary can be managed. 1026a0483764SConrad Meyer * Referencing a new dictionary effectively "discards" any previous one. 1027a0483764SConrad Meyer * Special: referencing a NULL DDict means "return to no-dictionary mode". 1028a0483764SConrad Meyer * Note 2 : DDict is just referenced, its lifetime must outlive its usage from DCtx. 1029a0483764SConrad Meyer */ 1030a0483764SConrad Meyer ZSTDLIB_API size_t ZSTD_DCtx_refDDict(ZSTD_DCtx* dctx, const ZSTD_DDict* ddict); 1031a0483764SConrad Meyer 1032*5ff13fbcSAllan Jude /*! ZSTD_DCtx_refPrefix() : Requires v1.4.0+ 1033a0483764SConrad Meyer * Reference a prefix (single-usage dictionary) to decompress next frame. 1034a0483764SConrad Meyer * This is the reverse operation of ZSTD_CCtx_refPrefix(), 1035a0483764SConrad Meyer * and must use the same prefix as the one used during compression. 1036a0483764SConrad Meyer * Prefix is **only used once**. Reference is discarded at end of frame. 1037a0483764SConrad Meyer * End of frame is reached when ZSTD_decompressStream() returns 0. 1038a0483764SConrad Meyer * @result : 0, or an error code (which can be tested with ZSTD_isError()). 1039a0483764SConrad Meyer * Note 1 : Adding any prefix (including NULL) invalidates any previously set prefix or dictionary 1040a0483764SConrad Meyer * Note 2 : Prefix buffer is referenced. It **must** outlive decompression. 1041a0483764SConrad Meyer * Prefix buffer must remain unmodified up to the end of frame, 1042a0483764SConrad Meyer * reached when ZSTD_decompressStream() returns 0. 10439cbefe25SConrad Meyer * Note 3 : By default, the prefix is treated as raw content (ZSTD_dct_rawContent). 1044a0483764SConrad Meyer * Use ZSTD_CCtx_refPrefix_advanced() to alter dictMode (Experimental section) 1045a0483764SConrad Meyer * Note 4 : Referencing a raw content prefix has almost no cpu nor memory cost. 1046a0483764SConrad Meyer * A full dictionary is more costly, as it requires building tables. 1047a0483764SConrad Meyer */ 1048a0483764SConrad Meyer ZSTDLIB_API size_t ZSTD_DCtx_refPrefix(ZSTD_DCtx* dctx, 1049a0483764SConrad Meyer const void* prefix, size_t prefixSize); 1050a0483764SConrad Meyer 10512b9c00cbSConrad Meyer /* === Memory management === */ 10522b9c00cbSConrad Meyer 1053*5ff13fbcSAllan Jude /*! ZSTD_sizeof_*() : Requires v1.4.0+ 10542b9c00cbSConrad Meyer * These functions give the _current_ memory usage of selected object. 10552b9c00cbSConrad Meyer * Note that object memory usage can evolve (increase or decrease) over time. */ 10562b9c00cbSConrad Meyer ZSTDLIB_API size_t ZSTD_sizeof_CCtx(const ZSTD_CCtx* cctx); 10572b9c00cbSConrad Meyer ZSTDLIB_API size_t ZSTD_sizeof_DCtx(const ZSTD_DCtx* dctx); 10582b9c00cbSConrad Meyer ZSTDLIB_API size_t ZSTD_sizeof_CStream(const ZSTD_CStream* zcs); 10592b9c00cbSConrad Meyer ZSTDLIB_API size_t ZSTD_sizeof_DStream(const ZSTD_DStream* zds); 10602b9c00cbSConrad Meyer ZSTDLIB_API size_t ZSTD_sizeof_CDict(const ZSTD_CDict* cdict); 10612b9c00cbSConrad Meyer ZSTDLIB_API size_t ZSTD_sizeof_DDict(const ZSTD_DDict* ddict); 10622b9c00cbSConrad Meyer 10632b9c00cbSConrad Meyer #endif /* ZSTD_H_235446 */ 1064a0483764SConrad Meyer 1065a0483764SConrad Meyer 10664d3f1eafSConrad Meyer /* ************************************************************************************** 10672b9c00cbSConrad Meyer * ADVANCED AND EXPERIMENTAL FUNCTIONS 10682b9c00cbSConrad Meyer **************************************************************************************** 10692b9c00cbSConrad Meyer * The definitions in the following section are considered experimental. 10702b9c00cbSConrad Meyer * They are provided for advanced scenarios. 10712b9c00cbSConrad Meyer * They should never be used with a dynamic library, as prototypes may change in the future. 10722b9c00cbSConrad Meyer * Use them only in association with static linking. 10732b9c00cbSConrad Meyer * ***************************************************************************************/ 10742b9c00cbSConrad Meyer 10752b9c00cbSConrad Meyer #if defined(ZSTD_STATIC_LINKING_ONLY) && !defined(ZSTD_H_ZSTD_STATIC_LINKING_ONLY) 10762b9c00cbSConrad Meyer #define ZSTD_H_ZSTD_STATIC_LINKING_ONLY 1077a0483764SConrad Meyer 1078*5ff13fbcSAllan Jude /* This can be overridden externally to hide static symbols. */ 1079*5ff13fbcSAllan Jude #ifndef ZSTDLIB_STATIC_API 1080*5ff13fbcSAllan Jude # if defined(ZSTD_DLL_EXPORT) && (ZSTD_DLL_EXPORT==1) 1081*5ff13fbcSAllan Jude # define ZSTDLIB_STATIC_API __declspec(dllexport) ZSTDLIB_VISIBLE 1082*5ff13fbcSAllan Jude # elif defined(ZSTD_DLL_IMPORT) && (ZSTD_DLL_IMPORT==1) 1083*5ff13fbcSAllan Jude # define ZSTDLIB_STATIC_API __declspec(dllimport) ZSTDLIB_VISIBLE 1084*5ff13fbcSAllan Jude # else 1085*5ff13fbcSAllan Jude # define ZSTDLIB_STATIC_API ZSTDLIB_VISIBLE 1086*5ff13fbcSAllan Jude # endif 1087*5ff13fbcSAllan Jude #endif 1088*5ff13fbcSAllan Jude 1089*5ff13fbcSAllan Jude /* Deprecation warnings : 1090*5ff13fbcSAllan Jude * Should these warnings be a problem, it is generally possible to disable them, 1091*5ff13fbcSAllan Jude * typically with -Wno-deprecated-declarations for gcc or _CRT_SECURE_NO_WARNINGS in Visual. 1092*5ff13fbcSAllan Jude * Otherwise, it's also possible to define ZSTD_DISABLE_DEPRECATE_WARNINGS. 1093*5ff13fbcSAllan Jude */ 1094*5ff13fbcSAllan Jude #ifdef ZSTD_DISABLE_DEPRECATE_WARNINGS 1095*5ff13fbcSAllan Jude # define ZSTD_DEPRECATED(message) ZSTDLIB_STATIC_API /* disable deprecation warnings */ 1096*5ff13fbcSAllan Jude #else 1097*5ff13fbcSAllan Jude # if defined (__cplusplus) && (__cplusplus >= 201402) /* C++14 or greater */ 1098*5ff13fbcSAllan Jude # define ZSTD_DEPRECATED(message) [[deprecated(message)]] ZSTDLIB_STATIC_API 1099*5ff13fbcSAllan Jude # elif (defined(GNUC) && (GNUC > 4 || (GNUC == 4 && GNUC_MINOR >= 5))) || defined(__clang__) 1100*5ff13fbcSAllan Jude # define ZSTD_DEPRECATED(message) ZSTDLIB_STATIC_API __attribute__((deprecated(message))) 1101*5ff13fbcSAllan Jude # elif defined(__GNUC__) && (__GNUC__ >= 3) 1102*5ff13fbcSAllan Jude # define ZSTD_DEPRECATED(message) ZSTDLIB_STATIC_API __attribute__((deprecated)) 1103*5ff13fbcSAllan Jude # elif defined(_MSC_VER) 1104*5ff13fbcSAllan Jude # define ZSTD_DEPRECATED(message) ZSTDLIB_STATIC_API __declspec(deprecated(message)) 1105*5ff13fbcSAllan Jude # else 1106*5ff13fbcSAllan Jude # pragma message("WARNING: You need to implement ZSTD_DEPRECATED for this compiler") 1107*5ff13fbcSAllan Jude # define ZSTD_DEPRECATED(message) ZSTDLIB_STATIC_API 1108*5ff13fbcSAllan Jude # endif 1109*5ff13fbcSAllan Jude #endif /* ZSTD_DISABLE_DEPRECATE_WARNINGS */ 1110*5ff13fbcSAllan Jude 1111a0483764SConrad Meyer /**************************************************************************************** 1112a0483764SConrad Meyer * experimental API (static linking only) 1113a0483764SConrad Meyer **************************************************************************************** 1114a0483764SConrad Meyer * The following symbols and constants 1115a0483764SConrad Meyer * are not planned to join "stable API" status in the near future. 1116a0483764SConrad Meyer * They can still change in future versions. 1117a0483764SConrad Meyer * Some of them are planned to remain in the static_only section indefinitely. 1118a0483764SConrad Meyer * Some of them might be removed in the future (especially when redundant with existing stable functions) 1119a0483764SConrad Meyer * ***************************************************************************************/ 1120a0483764SConrad Meyer 11219cbefe25SConrad Meyer #define ZSTD_FRAMEHEADERSIZE_PREFIX(format) ((format) == ZSTD_f_zstd1 ? 5 : 1) /* minimum input size required to query frame header size */ 11229cbefe25SConrad Meyer #define ZSTD_FRAMEHEADERSIZE_MIN(format) ((format) == ZSTD_f_zstd1 ? 6 : 2) 1123a0483764SConrad Meyer #define ZSTD_FRAMEHEADERSIZE_MAX 18 /* can be useful for static allocation */ 1124a0483764SConrad Meyer #define ZSTD_SKIPPABLEHEADERSIZE 8 1125a0483764SConrad Meyer 1126a0483764SConrad Meyer /* compression parameter bounds */ 11270c16b537SWarner Losh #define ZSTD_WINDOWLOG_MAX_32 30 11280c16b537SWarner Losh #define ZSTD_WINDOWLOG_MAX_64 31 1129a0483764SConrad Meyer #define ZSTD_WINDOWLOG_MAX ((int)(sizeof(size_t) == 4 ? ZSTD_WINDOWLOG_MAX_32 : ZSTD_WINDOWLOG_MAX_64)) 11300c16b537SWarner Losh #define ZSTD_WINDOWLOG_MIN 10 113119fcbaf1SConrad Meyer #define ZSTD_HASHLOG_MAX ((ZSTD_WINDOWLOG_MAX < 30) ? ZSTD_WINDOWLOG_MAX : 30) 11320c16b537SWarner Losh #define ZSTD_HASHLOG_MIN 6 113319fcbaf1SConrad Meyer #define ZSTD_CHAINLOG_MAX_32 29 113419fcbaf1SConrad Meyer #define ZSTD_CHAINLOG_MAX_64 30 1135a0483764SConrad Meyer #define ZSTD_CHAINLOG_MAX ((int)(sizeof(size_t) == 4 ? ZSTD_CHAINLOG_MAX_32 : ZSTD_CHAINLOG_MAX_64)) 11360c16b537SWarner Losh #define ZSTD_CHAINLOG_MIN ZSTD_HASHLOG_MIN 11370c16b537SWarner Losh #define ZSTD_SEARCHLOG_MAX (ZSTD_WINDOWLOG_MAX-1) 11380c16b537SWarner Losh #define ZSTD_SEARCHLOG_MIN 1 1139a0483764SConrad Meyer #define ZSTD_MINMATCH_MAX 7 /* only for ZSTD_fast, other strategies are limited to 6 */ 1140a0483764SConrad Meyer #define ZSTD_MINMATCH_MIN 3 /* only for ZSTD_btopt+, faster strategies are limited to 4 */ 11410f743729SConrad Meyer #define ZSTD_TARGETLENGTH_MAX ZSTD_BLOCKSIZE_MAX 11420f743729SConrad Meyer #define ZSTD_TARGETLENGTH_MIN 0 /* note : comparing this constant to an unsigned results in a tautological test */ 1143a0483764SConrad Meyer #define ZSTD_STRATEGY_MIN ZSTD_fast 1144a0483764SConrad Meyer #define ZSTD_STRATEGY_MAX ZSTD_btultra2 1145a0483764SConrad Meyer 1146a0483764SConrad Meyer 1147a0483764SConrad Meyer #define ZSTD_OVERLAPLOG_MIN 0 1148a0483764SConrad Meyer #define ZSTD_OVERLAPLOG_MAX 9 1149a0483764SConrad Meyer 1150a0483764SConrad Meyer #define ZSTD_WINDOWLOG_LIMIT_DEFAULT 27 /* by default, the streaming decoder will refuse any frame 1151a0483764SConrad Meyer * requiring larger than (1<<ZSTD_WINDOWLOG_LIMIT_DEFAULT) window size, 1152a0483764SConrad Meyer * to preserve host's memory from unreasonable requirements. 11532b9c00cbSConrad Meyer * This limit can be overridden using ZSTD_DCtx_setParameter(,ZSTD_d_windowLogMax,). 1154a0483764SConrad Meyer * The limit does not apply for one-pass decoders (such as ZSTD_decompress()), since no additional memory is allocated */ 1155a0483764SConrad Meyer 1156a0483764SConrad Meyer 1157a0483764SConrad Meyer /* LDM parameter bounds */ 1158a0483764SConrad Meyer #define ZSTD_LDM_HASHLOG_MIN ZSTD_HASHLOG_MIN 1159a0483764SConrad Meyer #define ZSTD_LDM_HASHLOG_MAX ZSTD_HASHLOG_MAX 11600f743729SConrad Meyer #define ZSTD_LDM_MINMATCH_MIN 4 1161a0483764SConrad Meyer #define ZSTD_LDM_MINMATCH_MAX 4096 1162a0483764SConrad Meyer #define ZSTD_LDM_BUCKETSIZELOG_MIN 1 11630c16b537SWarner Losh #define ZSTD_LDM_BUCKETSIZELOG_MAX 8 1164a0483764SConrad Meyer #define ZSTD_LDM_HASHRATELOG_MIN 0 1165a0483764SConrad Meyer #define ZSTD_LDM_HASHRATELOG_MAX (ZSTD_WINDOWLOG_MAX - ZSTD_HASHLOG_MIN) 11660c16b537SWarner Losh 11674d3f1eafSConrad Meyer /* Advanced parameter bounds */ 11684d3f1eafSConrad Meyer #define ZSTD_TARGETCBLOCKSIZE_MIN 64 11694d3f1eafSConrad Meyer #define ZSTD_TARGETCBLOCKSIZE_MAX ZSTD_BLOCKSIZE_MAX 11709cbefe25SConrad Meyer #define ZSTD_SRCSIZEHINT_MIN 0 11719cbefe25SConrad Meyer #define ZSTD_SRCSIZEHINT_MAX INT_MAX 11724d3f1eafSConrad Meyer 11730f743729SConrad Meyer 11740c16b537SWarner Losh /* --- Advanced types --- */ 1175a0483764SConrad Meyer 1176a0483764SConrad Meyer typedef struct ZSTD_CCtx_params_s ZSTD_CCtx_params; 11770c16b537SWarner Losh 11780c16b537SWarner Losh typedef struct { 1179f7cd7fe5SConrad Meyer unsigned int offset; /* The offset of the match. (NOT the same as the offset code) 1180f7cd7fe5SConrad Meyer * If offset == 0 and matchLength == 0, this sequence represents the last 1181f7cd7fe5SConrad Meyer * literals in the block of litLength size. 11829cbefe25SConrad Meyer */ 1183f7cd7fe5SConrad Meyer 1184f7cd7fe5SConrad Meyer unsigned int litLength; /* Literal length of the sequence. */ 1185f7cd7fe5SConrad Meyer unsigned int matchLength; /* Match length of the sequence. */ 1186f7cd7fe5SConrad Meyer 1187f7cd7fe5SConrad Meyer /* Note: Users of this API may provide a sequence with matchLength == litLength == offset == 0. 1188f7cd7fe5SConrad Meyer * In this case, we will treat the sequence as a marker for a block boundary. 11899cbefe25SConrad Meyer */ 1190f7cd7fe5SConrad Meyer 1191f7cd7fe5SConrad Meyer unsigned int rep; /* Represents which repeat offset is represented by the field 'offset'. 1192f7cd7fe5SConrad Meyer * Ranges from [0, 3]. 1193f7cd7fe5SConrad Meyer * 1194f7cd7fe5SConrad Meyer * Repeat offsets are essentially previous offsets from previous sequences sorted in 1195f7cd7fe5SConrad Meyer * recency order. For more detail, see doc/zstd_compression_format.md 1196f7cd7fe5SConrad Meyer * 1197f7cd7fe5SConrad Meyer * If rep == 0, then 'offset' does not contain a repeat offset. 1198f7cd7fe5SConrad Meyer * If rep > 0: 1199f7cd7fe5SConrad Meyer * If litLength != 0: 1200f7cd7fe5SConrad Meyer * rep == 1 --> offset == repeat_offset_1 1201f7cd7fe5SConrad Meyer * rep == 2 --> offset == repeat_offset_2 1202f7cd7fe5SConrad Meyer * rep == 3 --> offset == repeat_offset_3 1203f7cd7fe5SConrad Meyer * If litLength == 0: 1204f7cd7fe5SConrad Meyer * rep == 1 --> offset == repeat_offset_2 1205f7cd7fe5SConrad Meyer * rep == 2 --> offset == repeat_offset_3 1206f7cd7fe5SConrad Meyer * rep == 3 --> offset == repeat_offset_1 - 1 1207f7cd7fe5SConrad Meyer * 1208f7cd7fe5SConrad Meyer * Note: This field is optional. ZSTD_generateSequences() will calculate the value of 1209f7cd7fe5SConrad Meyer * 'rep', but repeat offsets do not necessarily need to be calculated from an external 1210f7cd7fe5SConrad Meyer * sequence provider's perspective. For example, ZSTD_compressSequences() does not 1211f7cd7fe5SConrad Meyer * use this 'rep' field at all (as of now). 1212f7cd7fe5SConrad Meyer */ 12139cbefe25SConrad Meyer } ZSTD_Sequence; 12149cbefe25SConrad Meyer 12159cbefe25SConrad Meyer typedef struct { 12160c16b537SWarner Losh unsigned windowLog; /**< largest match distance : larger == more compression, more memory needed during decompression */ 12170c16b537SWarner Losh unsigned chainLog; /**< fully searched segment : larger == more compression, slower, more memory (useless for fast) */ 12180c16b537SWarner Losh unsigned hashLog; /**< dispatch table : larger == faster, more memory */ 12190c16b537SWarner Losh unsigned searchLog; /**< nb of searches : larger == more compression, slower */ 1220a0483764SConrad Meyer unsigned minMatch; /**< match length searched : larger == faster decompression, sometimes less compression */ 12210c16b537SWarner Losh unsigned targetLength; /**< acceptable match size for optimal parser (only) : larger == more compression, slower */ 1222a0483764SConrad Meyer ZSTD_strategy strategy; /**< see ZSTD_strategy definition above */ 12230c16b537SWarner Losh } ZSTD_compressionParameters; 12240c16b537SWarner Losh 12250c16b537SWarner Losh typedef struct { 1226a0483764SConrad Meyer int contentSizeFlag; /**< 1: content size will be in frame header (when known) */ 1227a0483764SConrad Meyer int checksumFlag; /**< 1: generate a 32-bits checksum using XXH64 algorithm at end of frame, for error detection */ 1228a0483764SConrad Meyer int noDictIDFlag; /**< 1: no dictID will be saved into frame header (dictID is only useful for dictionary compression) */ 12290c16b537SWarner Losh } ZSTD_frameParameters; 12300c16b537SWarner Losh 12310c16b537SWarner Losh typedef struct { 12320c16b537SWarner Losh ZSTD_compressionParameters cParams; 12330c16b537SWarner Losh ZSTD_frameParameters fParams; 12340c16b537SWarner Losh } ZSTD_parameters; 12350c16b537SWarner Losh 123619fcbaf1SConrad Meyer typedef enum { 123719fcbaf1SConrad Meyer ZSTD_dct_auto = 0, /* dictionary is "full" when starting with ZSTD_MAGIC_DICTIONARY, otherwise it is "rawContent" */ 1238a0483764SConrad Meyer ZSTD_dct_rawContent = 1, /* ensures dictionary is always loaded as rawContent, even if it starts with ZSTD_MAGIC_DICTIONARY */ 1239a0483764SConrad Meyer ZSTD_dct_fullDict = 2 /* refuses to load a dictionary if it does not respect Zstandard's specification, starting with ZSTD_MAGIC_DICTIONARY */ 124019fcbaf1SConrad Meyer } ZSTD_dictContentType_e; 124119fcbaf1SConrad Meyer 124219fcbaf1SConrad Meyer typedef enum { 124319fcbaf1SConrad Meyer ZSTD_dlm_byCopy = 0, /**< Copy dictionary content internally */ 12449cbefe25SConrad Meyer ZSTD_dlm_byRef = 1 /**< Reference dictionary content -- the dictionary buffer must outlive its users. */ 124519fcbaf1SConrad Meyer } ZSTD_dictLoadMethod_e; 124619fcbaf1SConrad Meyer 1247a0483764SConrad Meyer typedef enum { 1248a0483764SConrad Meyer ZSTD_f_zstd1 = 0, /* zstd frame format, specified in zstd_compression_format.md (default) */ 12499cbefe25SConrad Meyer ZSTD_f_zstd1_magicless = 1 /* Variant of zstd frame format, without initial 4-bytes magic number. 1250a0483764SConrad Meyer * Useful to save 4 bytes per generated frame. 1251a0483764SConrad Meyer * Decoder cannot recognise automatically this format, requiring this instruction. */ 1252a0483764SConrad Meyer } ZSTD_format_e; 1253a0483764SConrad Meyer 1254a0483764SConrad Meyer typedef enum { 1255f7cd7fe5SConrad Meyer /* Note: this enum controls ZSTD_d_forceIgnoreChecksum */ 1256f7cd7fe5SConrad Meyer ZSTD_d_validateChecksum = 0, 1257f7cd7fe5SConrad Meyer ZSTD_d_ignoreChecksum = 1 1258f7cd7fe5SConrad Meyer } ZSTD_forceIgnoreChecksum_e; 1259f7cd7fe5SConrad Meyer 1260f7cd7fe5SConrad Meyer typedef enum { 1261*5ff13fbcSAllan Jude /* Note: this enum controls ZSTD_d_refMultipleDDicts */ 1262*5ff13fbcSAllan Jude ZSTD_rmd_refSingleDDict = 0, 1263*5ff13fbcSAllan Jude ZSTD_rmd_refMultipleDDicts = 1 1264*5ff13fbcSAllan Jude } ZSTD_refMultipleDDicts_e; 1265*5ff13fbcSAllan Jude 1266*5ff13fbcSAllan Jude typedef enum { 1267a0483764SConrad Meyer /* Note: this enum and the behavior it controls are effectively internal 1268a0483764SConrad Meyer * implementation details of the compressor. They are expected to continue 1269a0483764SConrad Meyer * to evolve and should be considered only in the context of extremely 1270a0483764SConrad Meyer * advanced performance tuning. 1271a0483764SConrad Meyer * 12729cbefe25SConrad Meyer * Zstd currently supports the use of a CDict in three ways: 1273a0483764SConrad Meyer * 1274a0483764SConrad Meyer * - The contents of the CDict can be copied into the working context. This 1275a0483764SConrad Meyer * means that the compression can search both the dictionary and input 1276a0483764SConrad Meyer * while operating on a single set of internal tables. This makes 1277a0483764SConrad Meyer * the compression faster per-byte of input. However, the initial copy of 1278a0483764SConrad Meyer * the CDict's tables incurs a fixed cost at the beginning of the 1279a0483764SConrad Meyer * compression. For small compressions (< 8 KB), that copy can dominate 1280a0483764SConrad Meyer * the cost of the compression. 1281a0483764SConrad Meyer * 1282a0483764SConrad Meyer * - The CDict's tables can be used in-place. In this model, compression is 1283a0483764SConrad Meyer * slower per input byte, because the compressor has to search two sets of 1284a0483764SConrad Meyer * tables. However, this model incurs no start-up cost (as long as the 1285a0483764SConrad Meyer * working context's tables can be reused). For small inputs, this can be 1286a0483764SConrad Meyer * faster than copying the CDict's tables. 1287a0483764SConrad Meyer * 12889cbefe25SConrad Meyer * - The CDict's tables are not used at all, and instead we use the working 12899cbefe25SConrad Meyer * context alone to reload the dictionary and use params based on the source 12909cbefe25SConrad Meyer * size. See ZSTD_compress_insertDictionary() and ZSTD_compress_usingDict(). 12919cbefe25SConrad Meyer * This method is effective when the dictionary sizes are very small relative 12929cbefe25SConrad Meyer * to the input size, and the input size is fairly large to begin with. 12939cbefe25SConrad Meyer * 1294a0483764SConrad Meyer * Zstd has a simple internal heuristic that selects which strategy to use 1295a0483764SConrad Meyer * at the beginning of a compression. However, if experimentation shows that 1296a0483764SConrad Meyer * Zstd is making poor choices, it is possible to override that choice with 1297a0483764SConrad Meyer * this enum. 1298a0483764SConrad Meyer */ 1299a0483764SConrad Meyer ZSTD_dictDefaultAttach = 0, /* Use the default heuristic. */ 1300a0483764SConrad Meyer ZSTD_dictForceAttach = 1, /* Never copy the dictionary. */ 1301a0483764SConrad Meyer ZSTD_dictForceCopy = 2, /* Always copy the dictionary. */ 13029cbefe25SConrad Meyer ZSTD_dictForceLoad = 3 /* Always reload the dictionary */ 1303a0483764SConrad Meyer } ZSTD_dictAttachPref_e; 13040c16b537SWarner Losh 13052b9c00cbSConrad Meyer typedef enum { 13062b9c00cbSConrad Meyer ZSTD_lcm_auto = 0, /**< Automatically determine the compression mode based on the compression level. 13072b9c00cbSConrad Meyer * Negative compression levels will be uncompressed, and positive compression 13082b9c00cbSConrad Meyer * levels will be compressed. */ 13092b9c00cbSConrad Meyer ZSTD_lcm_huffman = 1, /**< Always attempt Huffman compression. Uncompressed literals will still be 13102b9c00cbSConrad Meyer * emitted if Huffman compression is not profitable. */ 13119cbefe25SConrad Meyer ZSTD_lcm_uncompressed = 2 /**< Always emit uncompressed literals. */ 13122b9c00cbSConrad Meyer } ZSTD_literalCompressionMode_e; 13132b9c00cbSConrad Meyer 1314*5ff13fbcSAllan Jude typedef enum { 1315*5ff13fbcSAllan Jude /* Note: This enum controls features which are conditionally beneficial. Zstd typically will make a final 1316*5ff13fbcSAllan Jude * decision on whether or not to enable the feature (ZSTD_ps_auto), but setting the switch to ZSTD_ps_enable 1317*5ff13fbcSAllan Jude * or ZSTD_ps_disable allow for a force enable/disable the feature. 1318*5ff13fbcSAllan Jude */ 1319*5ff13fbcSAllan Jude ZSTD_ps_auto = 0, /* Let the library automatically determine whether the feature shall be enabled */ 1320*5ff13fbcSAllan Jude ZSTD_ps_enable = 1, /* Force-enable the feature */ 1321*5ff13fbcSAllan Jude ZSTD_ps_disable = 2 /* Do not use the feature */ 1322*5ff13fbcSAllan Jude } ZSTD_paramSwitch_e; 13230c16b537SWarner Losh 13240c16b537SWarner Losh /*************************************** 13250c16b537SWarner Losh * Frame size functions 13260c16b537SWarner Losh ***************************************/ 13270c16b537SWarner Losh 13280c16b537SWarner Losh /*! ZSTD_findDecompressedSize() : 13292b9c00cbSConrad Meyer * `src` should point to the start of a series of ZSTD encoded and/or skippable frames 13300c16b537SWarner Losh * `srcSize` must be the _exact_ size of this series 13312b9c00cbSConrad Meyer * (i.e. there should be a frame boundary at `src + srcSize`) 13320c16b537SWarner Losh * @return : - decompressed size of all data in all successive frames 13330c16b537SWarner Losh * - if the decompressed size cannot be determined: ZSTD_CONTENTSIZE_UNKNOWN 13340c16b537SWarner Losh * - if an error occurred: ZSTD_CONTENTSIZE_ERROR 13350c16b537SWarner Losh * 13360c16b537SWarner Losh * note 1 : decompressed size is an optional field, that may not be present, especially in streaming mode. 13370c16b537SWarner Losh * When `return==ZSTD_CONTENTSIZE_UNKNOWN`, data to decompress could be any size. 13380c16b537SWarner Losh * In which case, it's necessary to use streaming mode to decompress data. 13390c16b537SWarner Losh * note 2 : decompressed size is always present when compression is done with ZSTD_compress() 13400c16b537SWarner Losh * note 3 : decompressed size can be very large (64-bits value), 13410c16b537SWarner Losh * potentially larger than what local system can handle as a single memory segment. 13420c16b537SWarner Losh * In which case, it's necessary to use streaming mode to decompress data. 13430c16b537SWarner Losh * note 4 : If source is untrusted, decompressed size could be wrong or intentionally modified. 13440c16b537SWarner Losh * Always ensure result fits within application's authorized limits. 13450c16b537SWarner Losh * Each application can set its own limits. 13460c16b537SWarner Losh * note 5 : ZSTD_findDecompressedSize handles multiple frames, and so it must traverse the input to 13470c16b537SWarner Losh * read each contained frame header. This is fast as most of the data is skipped, 13480c16b537SWarner Losh * however it does mean that all frame data must be present and valid. */ 1349*5ff13fbcSAllan Jude ZSTDLIB_STATIC_API unsigned long long ZSTD_findDecompressedSize(const void* src, size_t srcSize); 13500c16b537SWarner Losh 13514d3f1eafSConrad Meyer /*! ZSTD_decompressBound() : 13522b9c00cbSConrad Meyer * `src` should point to the start of a series of ZSTD encoded and/or skippable frames 13532b9c00cbSConrad Meyer * `srcSize` must be the _exact_ size of this series 13542b9c00cbSConrad Meyer * (i.e. there should be a frame boundary at `src + srcSize`) 13552b9c00cbSConrad Meyer * @return : - upper-bound for the decompressed size of all data in all successive frames 1356*5ff13fbcSAllan Jude * - if an error occurred: ZSTD_CONTENTSIZE_ERROR 13572b9c00cbSConrad Meyer * 13582b9c00cbSConrad Meyer * note 1 : an error can occur if `src` contains an invalid or incorrectly formatted frame. 13592b9c00cbSConrad Meyer * note 2 : the upper-bound is exact when the decompressed size field is available in every ZSTD encoded frame of `src`. 13602b9c00cbSConrad Meyer * in this case, `ZSTD_findDecompressedSize` and `ZSTD_decompressBound` return the same value. 13612b9c00cbSConrad Meyer * note 3 : when the decompressed size field isn't available, the upper-bound for that frame is calculated by: 13622b9c00cbSConrad Meyer * upper-bound = # blocks * min(128 KB, Window_Size) 13632b9c00cbSConrad Meyer */ 1364*5ff13fbcSAllan Jude ZSTDLIB_STATIC_API unsigned long long ZSTD_decompressBound(const void* src, size_t srcSize); 13652b9c00cbSConrad Meyer 13660c16b537SWarner Losh /*! ZSTD_frameHeaderSize() : 1367a0483764SConrad Meyer * srcSize must be >= ZSTD_FRAMEHEADERSIZE_PREFIX. 13680f743729SConrad Meyer * @return : size of the Frame Header, 13690f743729SConrad Meyer * or an error code (if srcSize is too small) */ 1370*5ff13fbcSAllan Jude ZSTDLIB_STATIC_API size_t ZSTD_frameHeaderSize(const void* src, size_t srcSize); 13710c16b537SWarner Losh 1372f7cd7fe5SConrad Meyer typedef enum { 1373f7cd7fe5SConrad Meyer ZSTD_sf_noBlockDelimiters = 0, /* Representation of ZSTD_Sequence has no block delimiters, sequences only */ 1374f7cd7fe5SConrad Meyer ZSTD_sf_explicitBlockDelimiters = 1 /* Representation of ZSTD_Sequence contains explicit block delimiters */ 1375f7cd7fe5SConrad Meyer } ZSTD_sequenceFormat_e; 1376f7cd7fe5SConrad Meyer 1377f7cd7fe5SConrad Meyer /*! ZSTD_generateSequences() : 1378f7cd7fe5SConrad Meyer * Generate sequences using ZSTD_compress2, given a source buffer. 1379f7cd7fe5SConrad Meyer * 1380f7cd7fe5SConrad Meyer * Each block will end with a dummy sequence 1381f7cd7fe5SConrad Meyer * with offset == 0, matchLength == 0, and litLength == length of last literals. 1382f7cd7fe5SConrad Meyer * litLength may be == 0, and if so, then the sequence of (of: 0 ml: 0 ll: 0) 1383f7cd7fe5SConrad Meyer * simply acts as a block delimiter. 1384f7cd7fe5SConrad Meyer * 13859cbefe25SConrad Meyer * zc can be used to insert custom compression params. 13869cbefe25SConrad Meyer * This function invokes ZSTD_compress2 1387f7cd7fe5SConrad Meyer * 1388f7cd7fe5SConrad Meyer * The output of this function can be fed into ZSTD_compressSequences() with CCtx 1389f7cd7fe5SConrad Meyer * setting of ZSTD_c_blockDelimiters as ZSTD_sf_explicitBlockDelimiters 1390f7cd7fe5SConrad Meyer * @return : number of sequences generated 13919cbefe25SConrad Meyer */ 1392f7cd7fe5SConrad Meyer 1393*5ff13fbcSAllan Jude ZSTDLIB_STATIC_API size_t ZSTD_generateSequences(ZSTD_CCtx* zc, ZSTD_Sequence* outSeqs, 13949cbefe25SConrad Meyer size_t outSeqsSize, const void* src, size_t srcSize); 13959cbefe25SConrad Meyer 1396f7cd7fe5SConrad Meyer /*! ZSTD_mergeBlockDelimiters() : 1397f7cd7fe5SConrad Meyer * Given an array of ZSTD_Sequence, remove all sequences that represent block delimiters/last literals 1398f7cd7fe5SConrad Meyer * by merging them into into the literals of the next sequence. 1399f7cd7fe5SConrad Meyer * 1400f7cd7fe5SConrad Meyer * As such, the final generated result has no explicit representation of block boundaries, 1401f7cd7fe5SConrad Meyer * and the final last literals segment is not represented in the sequences. 1402f7cd7fe5SConrad Meyer * 1403f7cd7fe5SConrad Meyer * The output of this function can be fed into ZSTD_compressSequences() with CCtx 1404f7cd7fe5SConrad Meyer * setting of ZSTD_c_blockDelimiters as ZSTD_sf_noBlockDelimiters 1405f7cd7fe5SConrad Meyer * @return : number of sequences left after merging 1406f7cd7fe5SConrad Meyer */ 1407*5ff13fbcSAllan Jude ZSTDLIB_STATIC_API size_t ZSTD_mergeBlockDelimiters(ZSTD_Sequence* sequences, size_t seqsSize); 1408f7cd7fe5SConrad Meyer 1409f7cd7fe5SConrad Meyer /*! ZSTD_compressSequences() : 1410f7cd7fe5SConrad Meyer * Compress an array of ZSTD_Sequence, generated from the original source buffer, into dst. 1411f7cd7fe5SConrad Meyer * If a dictionary is included, then the cctx should reference the dict. (see: ZSTD_CCtx_refCDict(), ZSTD_CCtx_loadDictionary(), etc.) 1412f7cd7fe5SConrad Meyer * The entire source is compressed into a single frame. 1413f7cd7fe5SConrad Meyer * 1414f7cd7fe5SConrad Meyer * The compression behavior changes based on cctx params. In particular: 1415f7cd7fe5SConrad Meyer * If ZSTD_c_blockDelimiters == ZSTD_sf_noBlockDelimiters, the array of ZSTD_Sequence is expected to contain 1416f7cd7fe5SConrad Meyer * no block delimiters (defined in ZSTD_Sequence). Block boundaries are roughly determined based on 1417f7cd7fe5SConrad Meyer * the block size derived from the cctx, and sequences may be split. This is the default setting. 1418f7cd7fe5SConrad Meyer * 1419f7cd7fe5SConrad Meyer * If ZSTD_c_blockDelimiters == ZSTD_sf_explicitBlockDelimiters, the array of ZSTD_Sequence is expected to contain 1420f7cd7fe5SConrad Meyer * block delimiters (defined in ZSTD_Sequence). Behavior is undefined if no block delimiters are provided. 1421f7cd7fe5SConrad Meyer * 1422f7cd7fe5SConrad Meyer * If ZSTD_c_validateSequences == 0, this function will blindly accept the sequences provided. Invalid sequences cause undefined 1423f7cd7fe5SConrad Meyer * behavior. If ZSTD_c_validateSequences == 1, then if sequence is invalid (see doc/zstd_compression_format.md for 1424f7cd7fe5SConrad Meyer * specifics regarding offset/matchlength requirements) then the function will bail out and return an error. 1425f7cd7fe5SConrad Meyer * 1426f7cd7fe5SConrad Meyer * In addition to the two adjustable experimental params, there are other important cctx params. 1427f7cd7fe5SConrad Meyer * - ZSTD_c_minMatch MUST be set as less than or equal to the smallest match generated by the match finder. It has a minimum value of ZSTD_MINMATCH_MIN. 1428f7cd7fe5SConrad Meyer * - ZSTD_c_compressionLevel accordingly adjusts the strength of the entropy coder, as it would in typical compression. 1429f7cd7fe5SConrad Meyer * - ZSTD_c_windowLog affects offset validation: this function will return an error at higher debug levels if a provided offset 1430f7cd7fe5SConrad Meyer * is larger than what the spec allows for a given window log and dictionary (if present). See: doc/zstd_compression_format.md 1431f7cd7fe5SConrad Meyer * 1432f7cd7fe5SConrad Meyer * Note: Repcodes are, as of now, always re-calculated within this function, so ZSTD_Sequence::rep is unused. 1433f7cd7fe5SConrad Meyer * Note 2: Once we integrate ability to ingest repcodes, the explicit block delims mode must respect those repcodes exactly, 1434f7cd7fe5SConrad Meyer * and cannot emit an RLE block that disagrees with the repcode history 1435f7cd7fe5SConrad Meyer * @return : final compressed size or a ZSTD error. 1436f7cd7fe5SConrad Meyer */ 1437*5ff13fbcSAllan Jude ZSTDLIB_STATIC_API size_t ZSTD_compressSequences(ZSTD_CCtx* const cctx, void* dst, size_t dstSize, 1438f7cd7fe5SConrad Meyer const ZSTD_Sequence* inSeqs, size_t inSeqsSize, 1439f7cd7fe5SConrad Meyer const void* src, size_t srcSize); 1440f7cd7fe5SConrad Meyer 14410c16b537SWarner Losh 1442*5ff13fbcSAllan Jude /*! ZSTD_writeSkippableFrame() : 1443*5ff13fbcSAllan Jude * Generates a zstd skippable frame containing data given by src, and writes it to dst buffer. 1444*5ff13fbcSAllan Jude * 1445*5ff13fbcSAllan Jude * Skippable frames begin with a a 4-byte magic number. There are 16 possible choices of magic number, 1446*5ff13fbcSAllan Jude * ranging from ZSTD_MAGIC_SKIPPABLE_START to ZSTD_MAGIC_SKIPPABLE_START+15. 1447*5ff13fbcSAllan Jude * As such, the parameter magicVariant controls the exact skippable frame magic number variant used, so 1448*5ff13fbcSAllan Jude * the magic number used will be ZSTD_MAGIC_SKIPPABLE_START + magicVariant. 1449*5ff13fbcSAllan Jude * 1450*5ff13fbcSAllan Jude * Returns an error if destination buffer is not large enough, if the source size is not representable 1451*5ff13fbcSAllan Jude * with a 4-byte unsigned int, or if the parameter magicVariant is greater than 15 (and therefore invalid). 1452*5ff13fbcSAllan Jude * 1453*5ff13fbcSAllan Jude * @return : number of bytes written or a ZSTD error. 1454*5ff13fbcSAllan Jude */ 1455*5ff13fbcSAllan Jude ZSTDLIB_STATIC_API size_t ZSTD_writeSkippableFrame(void* dst, size_t dstCapacity, 1456*5ff13fbcSAllan Jude const void* src, size_t srcSize, unsigned magicVariant); 1457*5ff13fbcSAllan Jude 1458*5ff13fbcSAllan Jude /*! ZSTD_readSkippableFrame() : 1459*5ff13fbcSAllan Jude * Retrieves a zstd skippable frame containing data given by src, and writes it to dst buffer. 1460*5ff13fbcSAllan Jude * 1461*5ff13fbcSAllan Jude * The parameter magicVariant will receive the magicVariant that was supplied when the frame was written, 1462*5ff13fbcSAllan Jude * i.e. magicNumber - ZSTD_MAGIC_SKIPPABLE_START. This can be NULL if the caller is not interested 1463*5ff13fbcSAllan Jude * in the magicVariant. 1464*5ff13fbcSAllan Jude * 1465*5ff13fbcSAllan Jude * Returns an error if destination buffer is not large enough, or if the frame is not skippable. 1466*5ff13fbcSAllan Jude * 1467*5ff13fbcSAllan Jude * @return : number of bytes written or a ZSTD error. 1468*5ff13fbcSAllan Jude */ 1469*5ff13fbcSAllan Jude ZSTDLIB_API size_t ZSTD_readSkippableFrame(void* dst, size_t dstCapacity, unsigned* magicVariant, 1470*5ff13fbcSAllan Jude const void* src, size_t srcSize); 1471*5ff13fbcSAllan Jude 1472*5ff13fbcSAllan Jude /*! ZSTD_isSkippableFrame() : 1473*5ff13fbcSAllan Jude * Tells if the content of `buffer` starts with a valid Frame Identifier for a skippable frame. 1474*5ff13fbcSAllan Jude */ 1475*5ff13fbcSAllan Jude ZSTDLIB_API unsigned ZSTD_isSkippableFrame(const void* buffer, size_t size); 1476*5ff13fbcSAllan Jude 1477*5ff13fbcSAllan Jude 1478*5ff13fbcSAllan Jude 14790c16b537SWarner Losh /*************************************** 148019fcbaf1SConrad Meyer * Memory management 14810c16b537SWarner Losh ***************************************/ 14820c16b537SWarner Losh 14830c16b537SWarner Losh /*! ZSTD_estimate*() : 148437f1f268SConrad Meyer * These functions make it possible to estimate memory usage 148537f1f268SConrad Meyer * of a future {D,C}Ctx, before its creation. 14869cbefe25SConrad Meyer * 148737f1f268SConrad Meyer * ZSTD_estimateCCtxSize() will provide a memory budget large enough 148837f1f268SConrad Meyer * for any compression level up to selected one. 148937f1f268SConrad Meyer * Note : Unlike ZSTD_estimateCStreamSize*(), this estimate 149037f1f268SConrad Meyer * does not include space for a window buffer. 149137f1f268SConrad Meyer * Therefore, the estimation is only guaranteed for single-shot compressions, not streaming. 149237f1f268SConrad Meyer * The estimate will assume the input may be arbitrarily large, 149337f1f268SConrad Meyer * which is the worst case. 14949cbefe25SConrad Meyer * 149537f1f268SConrad Meyer * When srcSize can be bound by a known and rather "small" value, 149637f1f268SConrad Meyer * this fact can be used to provide a tighter estimation 149737f1f268SConrad Meyer * because the CCtx compression context will need less memory. 149837f1f268SConrad Meyer * This tighter estimation can be provided by more advanced functions 149937f1f268SConrad Meyer * ZSTD_estimateCCtxSize_usingCParams(), which can be used in tandem with ZSTD_getCParams(), 150037f1f268SConrad Meyer * and ZSTD_estimateCCtxSize_usingCCtxParams(), which can be used in tandem with ZSTD_CCtxParams_setParameter(). 150137f1f268SConrad Meyer * Both can be used to estimate memory using custom compression parameters and arbitrary srcSize limits. 150237f1f268SConrad Meyer * 150337f1f268SConrad Meyer * Note 2 : only single-threaded compression is supported. 150437f1f268SConrad Meyer * ZSTD_estimateCCtxSize_usingCCtxParams() will return an error code if ZSTD_c_nbWorkers is >= 1. 150537f1f268SConrad Meyer */ 1506*5ff13fbcSAllan Jude ZSTDLIB_STATIC_API size_t ZSTD_estimateCCtxSize(int compressionLevel); 1507*5ff13fbcSAllan Jude ZSTDLIB_STATIC_API size_t ZSTD_estimateCCtxSize_usingCParams(ZSTD_compressionParameters cParams); 1508*5ff13fbcSAllan Jude ZSTDLIB_STATIC_API size_t ZSTD_estimateCCtxSize_usingCCtxParams(const ZSTD_CCtx_params* params); 1509*5ff13fbcSAllan Jude ZSTDLIB_STATIC_API size_t ZSTD_estimateDCtxSize(void); 15100c16b537SWarner Losh 15110c16b537SWarner Losh /*! ZSTD_estimateCStreamSize() : 15120c16b537SWarner Losh * ZSTD_estimateCStreamSize() will provide a budget large enough for any compression level up to selected one. 15130c16b537SWarner Losh * It will also consider src size to be arbitrarily "large", which is worst case. 15140c16b537SWarner Losh * If srcSize is known to always be small, ZSTD_estimateCStreamSize_usingCParams() can provide a tighter estimation. 15150c16b537SWarner Losh * ZSTD_estimateCStreamSize_usingCParams() can be used in tandem with ZSTD_getCParams() to create cParams from compressionLevel. 15162b9c00cbSConrad Meyer * 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. 151719fcbaf1SConrad Meyer * Note : CStream size estimation is only correct for single-threaded compression. 15180c16b537SWarner Losh * ZSTD_DStream memory budget depends on window Size. 15190c16b537SWarner Losh * This information can be passed manually, using ZSTD_estimateDStreamSize, 15200c16b537SWarner Losh * or deducted from a valid frame Header, using ZSTD_estimateDStreamSize_fromFrame(); 15210c16b537SWarner Losh * Note : if streaming is init with function ZSTD_init?Stream_usingDict(), 15220c16b537SWarner Losh * an internal ?Dict will be created, which additional size is not estimated here. 15230c16b537SWarner Losh * In this case, get total size by adding ZSTD_estimate?DictSize */ 1524*5ff13fbcSAllan Jude ZSTDLIB_STATIC_API size_t ZSTD_estimateCStreamSize(int compressionLevel); 1525*5ff13fbcSAllan Jude ZSTDLIB_STATIC_API size_t ZSTD_estimateCStreamSize_usingCParams(ZSTD_compressionParameters cParams); 1526*5ff13fbcSAllan Jude ZSTDLIB_STATIC_API size_t ZSTD_estimateCStreamSize_usingCCtxParams(const ZSTD_CCtx_params* params); 1527*5ff13fbcSAllan Jude ZSTDLIB_STATIC_API size_t ZSTD_estimateDStreamSize(size_t windowSize); 1528*5ff13fbcSAllan Jude ZSTDLIB_STATIC_API size_t ZSTD_estimateDStreamSize_fromFrame(const void* src, size_t srcSize); 15290c16b537SWarner Losh 15300c16b537SWarner Losh /*! ZSTD_estimate?DictSize() : 15310c16b537SWarner Losh * ZSTD_estimateCDictSize() will bet that src size is relatively "small", and content is copied, like ZSTD_createCDict(). 153219fcbaf1SConrad Meyer * ZSTD_estimateCDictSize_advanced() makes it possible to control compression parameters precisely, like ZSTD_createCDict_advanced(). 153319fcbaf1SConrad Meyer * Note : dictionaries created by reference (`ZSTD_dlm_byRef`) are logically smaller. 15340c16b537SWarner Losh */ 1535*5ff13fbcSAllan Jude ZSTDLIB_STATIC_API size_t ZSTD_estimateCDictSize(size_t dictSize, int compressionLevel); 1536*5ff13fbcSAllan Jude ZSTDLIB_STATIC_API size_t ZSTD_estimateCDictSize_advanced(size_t dictSize, ZSTD_compressionParameters cParams, ZSTD_dictLoadMethod_e dictLoadMethod); 1537*5ff13fbcSAllan Jude ZSTDLIB_STATIC_API size_t ZSTD_estimateDDictSize(size_t dictSize, ZSTD_dictLoadMethod_e dictLoadMethod); 15380c16b537SWarner Losh 153919fcbaf1SConrad Meyer /*! ZSTD_initStatic*() : 154019fcbaf1SConrad Meyer * Initialize an object using a pre-allocated fixed-size buffer. 154119fcbaf1SConrad Meyer * workspace: The memory area to emplace the object into. 154219fcbaf1SConrad Meyer * Provided pointer *must be 8-bytes aligned*. 154319fcbaf1SConrad Meyer * Buffer must outlive object. 154419fcbaf1SConrad Meyer * workspaceSize: Use ZSTD_estimate*Size() to determine 154519fcbaf1SConrad Meyer * how large workspace must be to support target scenario. 154619fcbaf1SConrad Meyer * @return : pointer to object (same address as workspace, just different type), 154719fcbaf1SConrad Meyer * or NULL if error (size too small, incorrect alignment, etc.) 154819fcbaf1SConrad Meyer * Note : zstd will never resize nor malloc() when using a static buffer. 154919fcbaf1SConrad Meyer * If the object requires more memory than available, 155019fcbaf1SConrad Meyer * zstd will just error out (typically ZSTD_error_memory_allocation). 155119fcbaf1SConrad Meyer * Note 2 : there is no corresponding "free" function. 155219fcbaf1SConrad Meyer * Since workspace is allocated externally, it must be freed externally too. 155319fcbaf1SConrad Meyer * Note 3 : cParams : use ZSTD_getCParams() to convert a compression level 155419fcbaf1SConrad Meyer * into its associated cParams. 155519fcbaf1SConrad Meyer * Limitation 1 : currently not compatible with internal dictionary creation, triggered by 155619fcbaf1SConrad Meyer * ZSTD_CCtx_loadDictionary(), ZSTD_initCStream_usingDict() or ZSTD_initDStream_usingDict(). 155719fcbaf1SConrad Meyer * Limitation 2 : static cctx currently not compatible with multi-threading. 155819fcbaf1SConrad Meyer * Limitation 3 : static dctx is incompatible with legacy support. 155919fcbaf1SConrad Meyer */ 1560*5ff13fbcSAllan Jude ZSTDLIB_STATIC_API ZSTD_CCtx* ZSTD_initStaticCCtx(void* workspace, size_t workspaceSize); 1561*5ff13fbcSAllan Jude ZSTDLIB_STATIC_API ZSTD_CStream* ZSTD_initStaticCStream(void* workspace, size_t workspaceSize); /**< same as ZSTD_initStaticCCtx() */ 156219fcbaf1SConrad Meyer 1563*5ff13fbcSAllan Jude ZSTDLIB_STATIC_API ZSTD_DCtx* ZSTD_initStaticDCtx(void* workspace, size_t workspaceSize); 1564*5ff13fbcSAllan Jude ZSTDLIB_STATIC_API ZSTD_DStream* ZSTD_initStaticDStream(void* workspace, size_t workspaceSize); /**< same as ZSTD_initStaticDCtx() */ 156519fcbaf1SConrad Meyer 1566*5ff13fbcSAllan Jude ZSTDLIB_STATIC_API const ZSTD_CDict* ZSTD_initStaticCDict( 156719fcbaf1SConrad Meyer void* workspace, size_t workspaceSize, 156819fcbaf1SConrad Meyer const void* dict, size_t dictSize, 156919fcbaf1SConrad Meyer ZSTD_dictLoadMethod_e dictLoadMethod, 157019fcbaf1SConrad Meyer ZSTD_dictContentType_e dictContentType, 157119fcbaf1SConrad Meyer ZSTD_compressionParameters cParams); 157219fcbaf1SConrad Meyer 1573*5ff13fbcSAllan Jude ZSTDLIB_STATIC_API const ZSTD_DDict* ZSTD_initStaticDDict( 157419fcbaf1SConrad Meyer void* workspace, size_t workspaceSize, 157519fcbaf1SConrad Meyer const void* dict, size_t dictSize, 157619fcbaf1SConrad Meyer ZSTD_dictLoadMethod_e dictLoadMethod, 157719fcbaf1SConrad Meyer ZSTD_dictContentType_e dictContentType); 157819fcbaf1SConrad Meyer 1579a0483764SConrad Meyer 158019fcbaf1SConrad Meyer /*! Custom memory allocation : 158119fcbaf1SConrad Meyer * These prototypes make it possible to pass your own allocation/free functions. 158219fcbaf1SConrad Meyer * ZSTD_customMem is provided at creation time, using ZSTD_create*_advanced() variants listed below. 158319fcbaf1SConrad Meyer * All allocation/free operations will be completed using these custom variants instead of regular <stdlib.h> ones. 158419fcbaf1SConrad Meyer */ 158519fcbaf1SConrad Meyer typedef void* (*ZSTD_allocFunction) (void* opaque, size_t size); 158619fcbaf1SConrad Meyer typedef void (*ZSTD_freeFunction) (void* opaque, void* address); 158719fcbaf1SConrad Meyer typedef struct { ZSTD_allocFunction customAlloc; ZSTD_freeFunction customFree; void* opaque; } ZSTD_customMem; 1588f7cd7fe5SConrad Meyer static 1589f7cd7fe5SConrad Meyer #ifdef __GNUC__ 1590f7cd7fe5SConrad Meyer __attribute__((__unused__)) 1591f7cd7fe5SConrad Meyer #endif 1592f7cd7fe5SConrad Meyer ZSTD_customMem const ZSTD_defaultCMem = { NULL, NULL, NULL }; /**< this constant defers to stdlib's functions */ 159319fcbaf1SConrad Meyer 1594*5ff13fbcSAllan Jude ZSTDLIB_STATIC_API ZSTD_CCtx* ZSTD_createCCtx_advanced(ZSTD_customMem customMem); 1595*5ff13fbcSAllan Jude ZSTDLIB_STATIC_API ZSTD_CStream* ZSTD_createCStream_advanced(ZSTD_customMem customMem); 1596*5ff13fbcSAllan Jude ZSTDLIB_STATIC_API ZSTD_DCtx* ZSTD_createDCtx_advanced(ZSTD_customMem customMem); 1597*5ff13fbcSAllan Jude ZSTDLIB_STATIC_API ZSTD_DStream* ZSTD_createDStream_advanced(ZSTD_customMem customMem); 159819fcbaf1SConrad Meyer 1599*5ff13fbcSAllan Jude ZSTDLIB_STATIC_API ZSTD_CDict* ZSTD_createCDict_advanced(const void* dict, size_t dictSize, 160019fcbaf1SConrad Meyer ZSTD_dictLoadMethod_e dictLoadMethod, 160119fcbaf1SConrad Meyer ZSTD_dictContentType_e dictContentType, 160219fcbaf1SConrad Meyer ZSTD_compressionParameters cParams, 160319fcbaf1SConrad Meyer ZSTD_customMem customMem); 160419fcbaf1SConrad Meyer 1605f7cd7fe5SConrad Meyer /*! Thread pool : 1606f7cd7fe5SConrad Meyer * These prototypes make it possible to share a thread pool among multiple compression contexts. 1607f7cd7fe5SConrad Meyer * This can limit resources for applications with multiple threads where each one uses 1608f7cd7fe5SConrad Meyer * a threaded compression mode (via ZSTD_c_nbWorkers parameter). 1609f7cd7fe5SConrad Meyer * ZSTD_createThreadPool creates a new thread pool with a given number of threads. 1610f7cd7fe5SConrad Meyer * Note that the lifetime of such pool must exist while being used. 1611f7cd7fe5SConrad Meyer * ZSTD_CCtx_refThreadPool assigns a thread pool to a context (use NULL argument value 1612f7cd7fe5SConrad Meyer * to use an internal thread pool). 1613*5ff13fbcSAllan Jude * ZSTD_freeThreadPool frees a thread pool, accepts NULL pointer. 1614f7cd7fe5SConrad Meyer */ 1615f7cd7fe5SConrad Meyer typedef struct POOL_ctx_s ZSTD_threadPool; 1616*5ff13fbcSAllan Jude ZSTDLIB_STATIC_API ZSTD_threadPool* ZSTD_createThreadPool(size_t numThreads); 1617*5ff13fbcSAllan Jude ZSTDLIB_STATIC_API void ZSTD_freeThreadPool (ZSTD_threadPool* pool); /* accept NULL pointer */ 1618*5ff13fbcSAllan Jude ZSTDLIB_STATIC_API size_t ZSTD_CCtx_refThreadPool(ZSTD_CCtx* cctx, ZSTD_threadPool* pool); 1619*5ff13fbcSAllan Jude 1620f7cd7fe5SConrad Meyer 1621f7cd7fe5SConrad Meyer /* 1622f7cd7fe5SConrad Meyer * This API is temporary and is expected to change or disappear in the future! 1623f7cd7fe5SConrad Meyer */ 1624*5ff13fbcSAllan Jude ZSTDLIB_STATIC_API ZSTD_CDict* ZSTD_createCDict_advanced2( 1625f7cd7fe5SConrad Meyer const void* dict, size_t dictSize, 1626f7cd7fe5SConrad Meyer ZSTD_dictLoadMethod_e dictLoadMethod, 1627f7cd7fe5SConrad Meyer ZSTD_dictContentType_e dictContentType, 1628f7cd7fe5SConrad Meyer const ZSTD_CCtx_params* cctxParams, 1629f7cd7fe5SConrad Meyer ZSTD_customMem customMem); 1630f7cd7fe5SConrad Meyer 1631*5ff13fbcSAllan Jude ZSTDLIB_STATIC_API ZSTD_DDict* ZSTD_createDDict_advanced( 1632*5ff13fbcSAllan Jude const void* dict, size_t dictSize, 163319fcbaf1SConrad Meyer ZSTD_dictLoadMethod_e dictLoadMethod, 163419fcbaf1SConrad Meyer ZSTD_dictContentType_e dictContentType, 163519fcbaf1SConrad Meyer ZSTD_customMem customMem); 163619fcbaf1SConrad Meyer 1637*5ff13fbcSAllan Jude 16380c16b537SWarner Losh /*************************************** 16390c16b537SWarner Losh * Advanced compression functions 16400c16b537SWarner Losh ***************************************/ 16410c16b537SWarner Losh 16420c16b537SWarner Losh /*! ZSTD_createCDict_byReference() : 16430c16b537SWarner Losh * Create a digested dictionary for compression 1644a0483764SConrad Meyer * Dictionary content is just referenced, not duplicated. 1645a0483764SConrad Meyer * As a consequence, `dictBuffer` **must** outlive CDict, 16469cbefe25SConrad Meyer * and its content must remain unmodified throughout the lifetime of CDict. 16479cbefe25SConrad Meyer * note: equivalent to ZSTD_createCDict_advanced(), with dictLoadMethod==ZSTD_dlm_byRef */ 1648*5ff13fbcSAllan Jude ZSTDLIB_STATIC_API ZSTD_CDict* ZSTD_createCDict_byReference(const void* dictBuffer, size_t dictSize, int compressionLevel); 1649f7cd7fe5SConrad Meyer 16500c16b537SWarner Losh /*! ZSTD_getCParams() : 16510c16b537SWarner Losh * @return ZSTD_compressionParameters structure for a selected compression level and estimated srcSize. 16520c16b537SWarner Losh * `estimatedSrcSize` value is optional, select 0 if not known */ 1653*5ff13fbcSAllan Jude ZSTDLIB_STATIC_API ZSTD_compressionParameters ZSTD_getCParams(int compressionLevel, unsigned long long estimatedSrcSize, size_t dictSize); 16540c16b537SWarner Losh 16550c16b537SWarner Losh /*! ZSTD_getParams() : 16560c16b537SWarner Losh * same as ZSTD_getCParams(), but @return a full `ZSTD_parameters` object instead of sub-component `ZSTD_compressionParameters`. 1657052d3c12SConrad Meyer * All fields of `ZSTD_frameParameters` are set to default : contentSize=1, checksum=0, noDictID=0 */ 1658*5ff13fbcSAllan Jude ZSTDLIB_STATIC_API ZSTD_parameters ZSTD_getParams(int compressionLevel, unsigned long long estimatedSrcSize, size_t dictSize); 16590c16b537SWarner Losh 16600c16b537SWarner Losh /*! ZSTD_checkCParams() : 16612b9c00cbSConrad Meyer * Ensure param values remain within authorized range. 16622b9c00cbSConrad Meyer * @return 0 on success, or an error code (can be checked with ZSTD_isError()) */ 1663*5ff13fbcSAllan Jude ZSTDLIB_STATIC_API size_t ZSTD_checkCParams(ZSTD_compressionParameters params); 16640c16b537SWarner Losh 16650c16b537SWarner Losh /*! ZSTD_adjustCParams() : 16660c16b537SWarner Losh * optimize params for a given `srcSize` and `dictSize`. 16672b9c00cbSConrad Meyer * `srcSize` can be unknown, in which case use ZSTD_CONTENTSIZE_UNKNOWN. 16682b9c00cbSConrad Meyer * `dictSize` must be `0` when there is no dictionary. 16692b9c00cbSConrad Meyer * cPar can be invalid : all parameters will be clamped within valid range in the @return struct. 16702b9c00cbSConrad Meyer * This function never fails (wide contract) */ 1671*5ff13fbcSAllan Jude ZSTDLIB_STATIC_API ZSTD_compressionParameters ZSTD_adjustCParams(ZSTD_compressionParameters cPar, unsigned long long srcSize, size_t dictSize); 16720c16b537SWarner Losh 16730c16b537SWarner Losh /*! ZSTD_compress_advanced() : 16749cbefe25SConrad Meyer * Note : this function is now DEPRECATED. 16759cbefe25SConrad Meyer * It can be replaced by ZSTD_compress2(), in combination with ZSTD_CCtx_setParameter() and other parameter setters. 1676*5ff13fbcSAllan Jude * This prototype will generate compilation warnings. */ 1677*5ff13fbcSAllan Jude ZSTD_DEPRECATED("use ZSTD_compress2") 1678*5ff13fbcSAllan Jude size_t ZSTD_compress_advanced(ZSTD_CCtx* cctx, 16790c16b537SWarner Losh void* dst, size_t dstCapacity, 16800c16b537SWarner Losh const void* src, size_t srcSize, 16810c16b537SWarner Losh const void* dict,size_t dictSize, 16820c16b537SWarner Losh ZSTD_parameters params); 16830c16b537SWarner Losh 16840c16b537SWarner Losh /*! ZSTD_compress_usingCDict_advanced() : 1685*5ff13fbcSAllan Jude * Note : this function is now DEPRECATED. 16869cbefe25SConrad Meyer * It can be replaced by ZSTD_compress2(), in combination with ZSTD_CCtx_loadDictionary() and other parameter setters. 1687*5ff13fbcSAllan Jude * This prototype will generate compilation warnings. */ 1688*5ff13fbcSAllan Jude ZSTD_DEPRECATED("use ZSTD_compress2 with ZSTD_CCtx_loadDictionary") 1689*5ff13fbcSAllan Jude size_t ZSTD_compress_usingCDict_advanced(ZSTD_CCtx* cctx, 16900c16b537SWarner Losh void* dst, size_t dstCapacity, 16910c16b537SWarner Losh const void* src, size_t srcSize, 1692a0483764SConrad Meyer const ZSTD_CDict* cdict, 1693a0483764SConrad Meyer ZSTD_frameParameters fParams); 16940c16b537SWarner Losh 16950c16b537SWarner Losh 1696a0483764SConrad Meyer /*! ZSTD_CCtx_loadDictionary_byReference() : 1697a0483764SConrad Meyer * Same as ZSTD_CCtx_loadDictionary(), but dictionary content is referenced, instead of being copied into CCtx. 1698a0483764SConrad Meyer * It saves some memory, but also requires that `dict` outlives its usage within `cctx` */ 1699*5ff13fbcSAllan Jude ZSTDLIB_STATIC_API size_t ZSTD_CCtx_loadDictionary_byReference(ZSTD_CCtx* cctx, const void* dict, size_t dictSize); 1700a0483764SConrad Meyer 1701a0483764SConrad Meyer /*! ZSTD_CCtx_loadDictionary_advanced() : 1702a0483764SConrad Meyer * Same as ZSTD_CCtx_loadDictionary(), but gives finer control over 1703a0483764SConrad Meyer * how to load the dictionary (by copy ? by reference ?) 1704a0483764SConrad Meyer * and how to interpret it (automatic ? force raw mode ? full mode only ?) */ 1705*5ff13fbcSAllan Jude ZSTDLIB_STATIC_API size_t ZSTD_CCtx_loadDictionary_advanced(ZSTD_CCtx* cctx, const void* dict, size_t dictSize, ZSTD_dictLoadMethod_e dictLoadMethod, ZSTD_dictContentType_e dictContentType); 1706a0483764SConrad Meyer 1707a0483764SConrad Meyer /*! ZSTD_CCtx_refPrefix_advanced() : 1708a0483764SConrad Meyer * Same as ZSTD_CCtx_refPrefix(), but gives finer control over 1709a0483764SConrad Meyer * how to interpret prefix content (automatic ? force raw mode (default) ? full mode only ?) */ 1710*5ff13fbcSAllan Jude ZSTDLIB_STATIC_API size_t ZSTD_CCtx_refPrefix_advanced(ZSTD_CCtx* cctx, const void* prefix, size_t prefixSize, ZSTD_dictContentType_e dictContentType); 1711a0483764SConrad Meyer 1712a0483764SConrad Meyer /* === experimental parameters === */ 1713a0483764SConrad Meyer /* these parameters can be used with ZSTD_setParameter() 1714a0483764SConrad Meyer * they are not guaranteed to remain supported in the future */ 1715a0483764SConrad Meyer 1716a0483764SConrad Meyer /* Enables rsyncable mode, 1717a0483764SConrad Meyer * which makes compressed files more rsync friendly 1718a0483764SConrad Meyer * by adding periodic synchronization points to the compressed data. 1719a0483764SConrad Meyer * The target average block size is ZSTD_c_jobSize / 2. 1720a0483764SConrad Meyer * It's possible to modify the job size to increase or decrease 1721a0483764SConrad Meyer * the granularity of the synchronization point. 1722a0483764SConrad Meyer * Once the jobSize is smaller than the window size, 1723a0483764SConrad Meyer * it will result in compression ratio degradation. 1724a0483764SConrad Meyer * NOTE 1: rsyncable mode only works when multithreading is enabled. 1725a0483764SConrad Meyer * NOTE 2: rsyncable performs poorly in combination with long range mode, 1726a0483764SConrad Meyer * since it will decrease the effectiveness of synchronization points, 1727a0483764SConrad Meyer * though mileage may vary. 1728a0483764SConrad Meyer * NOTE 3: Rsyncable mode limits maximum compression speed to ~400 MB/s. 1729a0483764SConrad Meyer * If the selected compression level is already running significantly slower, 1730a0483764SConrad Meyer * the overall speed won't be significantly impacted. 1731a0483764SConrad Meyer */ 1732a0483764SConrad Meyer #define ZSTD_c_rsyncable ZSTD_c_experimentalParam1 1733a0483764SConrad Meyer 1734a0483764SConrad Meyer /* Select a compression format. 1735a0483764SConrad Meyer * The value must be of type ZSTD_format_e. 1736a0483764SConrad Meyer * See ZSTD_format_e enum definition for details */ 1737a0483764SConrad Meyer #define ZSTD_c_format ZSTD_c_experimentalParam2 1738a0483764SConrad Meyer 1739a0483764SConrad Meyer /* Force back-reference distances to remain < windowSize, 1740a0483764SConrad Meyer * even when referencing into Dictionary content (default:0) */ 1741a0483764SConrad Meyer #define ZSTD_c_forceMaxWindow ZSTD_c_experimentalParam3 1742a0483764SConrad Meyer 1743a0483764SConrad Meyer /* Controls whether the contents of a CDict 1744a0483764SConrad Meyer * are used in place, or copied into the working context. 1745a0483764SConrad Meyer * Accepts values from the ZSTD_dictAttachPref_e enum. 1746a0483764SConrad Meyer * See the comments on that enum for an explanation of the feature. */ 1747a0483764SConrad Meyer #define ZSTD_c_forceAttachDict ZSTD_c_experimentalParam4 1748a0483764SConrad Meyer 1749*5ff13fbcSAllan Jude /* Controlled with ZSTD_paramSwitch_e enum. 1750*5ff13fbcSAllan Jude * Default is ZSTD_ps_auto. 1751*5ff13fbcSAllan Jude * Set to ZSTD_ps_disable to never compress literals. 1752*5ff13fbcSAllan Jude * Set to ZSTD_ps_enable to always compress literals. (Note: uncompressed literals 1753*5ff13fbcSAllan Jude * may still be emitted if huffman is not beneficial to use.) 1754*5ff13fbcSAllan Jude * 1755*5ff13fbcSAllan Jude * By default, in ZSTD_ps_auto, the library will decide at runtime whether to use 1756*5ff13fbcSAllan Jude * literals compression based on the compression parameters - specifically, 1757*5ff13fbcSAllan Jude * negative compression levels do not use literal compression. 17582b9c00cbSConrad Meyer */ 17592b9c00cbSConrad Meyer #define ZSTD_c_literalCompressionMode ZSTD_c_experimentalParam5 17602b9c00cbSConrad Meyer 17614d3f1eafSConrad Meyer /* Tries to fit compressed block size to be around targetCBlockSize. 17624d3f1eafSConrad Meyer * No target when targetCBlockSize == 0. 17634d3f1eafSConrad Meyer * There is no guarantee on compressed block size (default:0) */ 17644d3f1eafSConrad Meyer #define ZSTD_c_targetCBlockSize ZSTD_c_experimentalParam6 17654d3f1eafSConrad Meyer 17669cbefe25SConrad Meyer /* User's best guess of source size. 17679cbefe25SConrad Meyer * Hint is not valid when srcSizeHint == 0. 17689cbefe25SConrad Meyer * There is no guarantee that hint is close to actual source size, 17699cbefe25SConrad Meyer * but compression ratio may regress significantly if guess considerably underestimates */ 17709cbefe25SConrad Meyer #define ZSTD_c_srcSizeHint ZSTD_c_experimentalParam7 17719cbefe25SConrad Meyer 1772f7cd7fe5SConrad Meyer /* Controls whether the new and experimental "dedicated dictionary search 1773f7cd7fe5SConrad Meyer * structure" can be used. This feature is still rough around the edges, be 1774f7cd7fe5SConrad Meyer * prepared for surprising behavior! 1775f7cd7fe5SConrad Meyer * 1776f7cd7fe5SConrad Meyer * How to use it: 1777f7cd7fe5SConrad Meyer * 1778f7cd7fe5SConrad Meyer * When using a CDict, whether to use this feature or not is controlled at 1779f7cd7fe5SConrad Meyer * CDict creation, and it must be set in a CCtxParams set passed into that 1780f7cd7fe5SConrad Meyer * construction (via ZSTD_createCDict_advanced2()). A compression will then 1781f7cd7fe5SConrad Meyer * use the feature or not based on how the CDict was constructed; the value of 1782f7cd7fe5SConrad Meyer * this param, set in the CCtx, will have no effect. 1783f7cd7fe5SConrad Meyer * 1784f7cd7fe5SConrad Meyer * However, when a dictionary buffer is passed into a CCtx, such as via 1785f7cd7fe5SConrad Meyer * ZSTD_CCtx_loadDictionary(), this param can be set on the CCtx to control 1786f7cd7fe5SConrad Meyer * whether the CDict that is created internally can use the feature or not. 1787f7cd7fe5SConrad Meyer * 1788f7cd7fe5SConrad Meyer * What it does: 1789f7cd7fe5SConrad Meyer * 1790f7cd7fe5SConrad Meyer * Normally, the internal data structures of the CDict are analogous to what 1791f7cd7fe5SConrad Meyer * would be stored in a CCtx after compressing the contents of a dictionary. 1792f7cd7fe5SConrad Meyer * To an approximation, a compression using a dictionary can then use those 1793f7cd7fe5SConrad Meyer * data structures to simply continue what is effectively a streaming 1794f7cd7fe5SConrad Meyer * compression where the simulated compression of the dictionary left off. 1795f7cd7fe5SConrad Meyer * Which is to say, the search structures in the CDict are normally the same 1796f7cd7fe5SConrad Meyer * format as in the CCtx. 1797f7cd7fe5SConrad Meyer * 1798f7cd7fe5SConrad Meyer * It is possible to do better, since the CDict is not like a CCtx: the search 1799f7cd7fe5SConrad Meyer * structures are written once during CDict creation, and then are only read 1800f7cd7fe5SConrad Meyer * after that, while the search structures in the CCtx are both read and 1801f7cd7fe5SConrad Meyer * written as the compression goes along. This means we can choose a search 1802f7cd7fe5SConrad Meyer * structure for the dictionary that is read-optimized. 1803f7cd7fe5SConrad Meyer * 1804f7cd7fe5SConrad Meyer * This feature enables the use of that different structure. 1805f7cd7fe5SConrad Meyer * 1806f7cd7fe5SConrad Meyer * Note that some of the members of the ZSTD_compressionParameters struct have 1807f7cd7fe5SConrad Meyer * different semantics and constraints in the dedicated search structure. It is 1808f7cd7fe5SConrad Meyer * highly recommended that you simply set a compression level in the CCtxParams 1809f7cd7fe5SConrad Meyer * you pass into the CDict creation call, and avoid messing with the cParams 1810f7cd7fe5SConrad Meyer * directly. 1811f7cd7fe5SConrad Meyer * 1812f7cd7fe5SConrad Meyer * Effects: 1813f7cd7fe5SConrad Meyer * 1814f7cd7fe5SConrad Meyer * This will only have any effect when the selected ZSTD_strategy 1815f7cd7fe5SConrad Meyer * implementation supports this feature. Currently, that's limited to 1816f7cd7fe5SConrad Meyer * ZSTD_greedy, ZSTD_lazy, and ZSTD_lazy2. 1817f7cd7fe5SConrad Meyer * 1818f7cd7fe5SConrad Meyer * Note that this means that the CDict tables can no longer be copied into the 1819f7cd7fe5SConrad Meyer * CCtx, so the dict attachment mode ZSTD_dictForceCopy will no longer be 1820*5ff13fbcSAllan Jude * usable. The dictionary can only be attached or reloaded. 1821f7cd7fe5SConrad Meyer * 1822f7cd7fe5SConrad Meyer * In general, you should expect compression to be faster--sometimes very much 1823f7cd7fe5SConrad Meyer * so--and CDict creation to be slightly slower. Eventually, we will probably 1824f7cd7fe5SConrad Meyer * make this mode the default. 1825f7cd7fe5SConrad Meyer */ 1826f7cd7fe5SConrad Meyer #define ZSTD_c_enableDedicatedDictSearch ZSTD_c_experimentalParam8 1827f7cd7fe5SConrad Meyer 1828f7cd7fe5SConrad Meyer /* ZSTD_c_stableInBuffer 1829f7cd7fe5SConrad Meyer * Experimental parameter. 1830f7cd7fe5SConrad Meyer * Default is 0 == disabled. Set to 1 to enable. 1831f7cd7fe5SConrad Meyer * 1832f7cd7fe5SConrad Meyer * Tells the compressor that the ZSTD_inBuffer will ALWAYS be the same 1833f7cd7fe5SConrad Meyer * between calls, except for the modifications that zstd makes to pos (the 1834f7cd7fe5SConrad Meyer * caller must not modify pos). This is checked by the compressor, and 1835f7cd7fe5SConrad Meyer * compression will fail if it ever changes. This means the only flush 1836f7cd7fe5SConrad Meyer * mode that makes sense is ZSTD_e_end, so zstd will error if ZSTD_e_end 1837f7cd7fe5SConrad Meyer * is not used. The data in the ZSTD_inBuffer in the range [src, src + pos) 1838f7cd7fe5SConrad Meyer * MUST not be modified during compression or you will get data corruption. 1839f7cd7fe5SConrad Meyer * 1840f7cd7fe5SConrad Meyer * When this flag is enabled zstd won't allocate an input window buffer, 1841f7cd7fe5SConrad Meyer * because the user guarantees it can reference the ZSTD_inBuffer until 1842f7cd7fe5SConrad Meyer * the frame is complete. But, it will still allocate an output buffer 1843f7cd7fe5SConrad Meyer * large enough to fit a block (see ZSTD_c_stableOutBuffer). This will also 1844f7cd7fe5SConrad Meyer * avoid the memcpy() from the input buffer to the input window buffer. 1845f7cd7fe5SConrad Meyer * 1846f7cd7fe5SConrad Meyer * NOTE: ZSTD_compressStream2() will error if ZSTD_e_end is not used. 1847f7cd7fe5SConrad Meyer * That means this flag cannot be used with ZSTD_compressStream(). 1848f7cd7fe5SConrad Meyer * 1849f7cd7fe5SConrad Meyer * NOTE: So long as the ZSTD_inBuffer always points to valid memory, using 1850f7cd7fe5SConrad Meyer * this flag is ALWAYS memory safe, and will never access out-of-bounds 1851f7cd7fe5SConrad Meyer * memory. However, compression WILL fail if you violate the preconditions. 1852f7cd7fe5SConrad Meyer * 1853f7cd7fe5SConrad Meyer * WARNING: The data in the ZSTD_inBuffer in the range [dst, dst + pos) MUST 1854f7cd7fe5SConrad Meyer * not be modified during compression or you will get data corruption. This 1855f7cd7fe5SConrad Meyer * is because zstd needs to reference data in the ZSTD_inBuffer to find 1856f7cd7fe5SConrad Meyer * matches. Normally zstd maintains its own window buffer for this purpose, 1857f7cd7fe5SConrad Meyer * but passing this flag tells zstd to use the user provided buffer. 1858f7cd7fe5SConrad Meyer */ 1859f7cd7fe5SConrad Meyer #define ZSTD_c_stableInBuffer ZSTD_c_experimentalParam9 1860f7cd7fe5SConrad Meyer 1861f7cd7fe5SConrad Meyer /* ZSTD_c_stableOutBuffer 1862f7cd7fe5SConrad Meyer * Experimental parameter. 1863f7cd7fe5SConrad Meyer * Default is 0 == disabled. Set to 1 to enable. 1864f7cd7fe5SConrad Meyer * 1865f7cd7fe5SConrad Meyer * Tells he compressor that the ZSTD_outBuffer will not be resized between 1866f7cd7fe5SConrad Meyer * calls. Specifically: (out.size - out.pos) will never grow. This gives the 1867f7cd7fe5SConrad Meyer * compressor the freedom to say: If the compressed data doesn't fit in the 1868f7cd7fe5SConrad Meyer * output buffer then return ZSTD_error_dstSizeTooSmall. This allows us to 1869f7cd7fe5SConrad Meyer * always decompress directly into the output buffer, instead of decompressing 1870f7cd7fe5SConrad Meyer * into an internal buffer and copying to the output buffer. 1871f7cd7fe5SConrad Meyer * 1872f7cd7fe5SConrad Meyer * When this flag is enabled zstd won't allocate an output buffer, because 1873f7cd7fe5SConrad Meyer * it can write directly to the ZSTD_outBuffer. It will still allocate the 1874f7cd7fe5SConrad Meyer * input window buffer (see ZSTD_c_stableInBuffer). 1875f7cd7fe5SConrad Meyer * 1876f7cd7fe5SConrad Meyer * Zstd will check that (out.size - out.pos) never grows and return an error 1877f7cd7fe5SConrad Meyer * if it does. While not strictly necessary, this should prevent surprises. 1878f7cd7fe5SConrad Meyer */ 1879f7cd7fe5SConrad Meyer #define ZSTD_c_stableOutBuffer ZSTD_c_experimentalParam10 1880f7cd7fe5SConrad Meyer 1881f7cd7fe5SConrad Meyer /* ZSTD_c_blockDelimiters 1882f7cd7fe5SConrad Meyer * Default is 0 == ZSTD_sf_noBlockDelimiters. 1883f7cd7fe5SConrad Meyer * 1884f7cd7fe5SConrad Meyer * For use with sequence compression API: ZSTD_compressSequences(). 1885f7cd7fe5SConrad Meyer * 1886f7cd7fe5SConrad Meyer * Designates whether or not the given array of ZSTD_Sequence contains block delimiters 1887f7cd7fe5SConrad Meyer * and last literals, which are defined as sequences with offset == 0 and matchLength == 0. 1888f7cd7fe5SConrad Meyer * See the definition of ZSTD_Sequence for more specifics. 1889f7cd7fe5SConrad Meyer */ 1890f7cd7fe5SConrad Meyer #define ZSTD_c_blockDelimiters ZSTD_c_experimentalParam11 1891f7cd7fe5SConrad Meyer 1892f7cd7fe5SConrad Meyer /* ZSTD_c_validateSequences 1893f7cd7fe5SConrad Meyer * Default is 0 == disabled. Set to 1 to enable sequence validation. 1894f7cd7fe5SConrad Meyer * 1895f7cd7fe5SConrad Meyer * For use with sequence compression API: ZSTD_compressSequences(). 1896f7cd7fe5SConrad Meyer * Designates whether or not we validate sequences provided to ZSTD_compressSequences() 1897f7cd7fe5SConrad Meyer * during function execution. 1898f7cd7fe5SConrad Meyer * 1899f7cd7fe5SConrad Meyer * Without validation, providing a sequence that does not conform to the zstd spec will cause 1900f7cd7fe5SConrad Meyer * undefined behavior, and may produce a corrupted block. 1901f7cd7fe5SConrad Meyer * 1902f7cd7fe5SConrad Meyer * With validation enabled, a if sequence is invalid (see doc/zstd_compression_format.md for 1903f7cd7fe5SConrad Meyer * specifics regarding offset/matchlength requirements) then the function will bail out and 1904f7cd7fe5SConrad Meyer * return an error. 1905f7cd7fe5SConrad Meyer * 1906f7cd7fe5SConrad Meyer */ 1907f7cd7fe5SConrad Meyer #define ZSTD_c_validateSequences ZSTD_c_experimentalParam12 1908f7cd7fe5SConrad Meyer 1909*5ff13fbcSAllan Jude /* ZSTD_c_useBlockSplitter 1910*5ff13fbcSAllan Jude * Controlled with ZSTD_paramSwitch_e enum. 1911*5ff13fbcSAllan Jude * Default is ZSTD_ps_auto. 1912*5ff13fbcSAllan Jude * Set to ZSTD_ps_disable to never use block splitter. 1913*5ff13fbcSAllan Jude * Set to ZSTD_ps_enable to always use block splitter. 1914*5ff13fbcSAllan Jude * 1915*5ff13fbcSAllan Jude * By default, in ZSTD_ps_auto, the library will decide at runtime whether to use 1916*5ff13fbcSAllan Jude * block splitting based on the compression parameters. 1917*5ff13fbcSAllan Jude */ 1918*5ff13fbcSAllan Jude #define ZSTD_c_useBlockSplitter ZSTD_c_experimentalParam13 1919*5ff13fbcSAllan Jude 1920*5ff13fbcSAllan Jude /* ZSTD_c_useRowMatchFinder 1921*5ff13fbcSAllan Jude * Controlled with ZSTD_paramSwitch_e enum. 1922*5ff13fbcSAllan Jude * Default is ZSTD_ps_auto. 1923*5ff13fbcSAllan Jude * Set to ZSTD_ps_disable to never use row-based matchfinder. 1924*5ff13fbcSAllan Jude * Set to ZSTD_ps_enable to force usage of row-based matchfinder. 1925*5ff13fbcSAllan Jude * 1926*5ff13fbcSAllan Jude * By default, in ZSTD_ps_auto, the library will decide at runtime whether to use 1927*5ff13fbcSAllan Jude * the row-based matchfinder based on support for SIMD instructions and the window log. 1928*5ff13fbcSAllan Jude * Note that this only pertains to compression strategies: greedy, lazy, and lazy2 1929*5ff13fbcSAllan Jude */ 1930*5ff13fbcSAllan Jude #define ZSTD_c_useRowMatchFinder ZSTD_c_experimentalParam14 1931*5ff13fbcSAllan Jude 1932*5ff13fbcSAllan Jude /* ZSTD_c_deterministicRefPrefix 1933*5ff13fbcSAllan Jude * Default is 0 == disabled. Set to 1 to enable. 1934*5ff13fbcSAllan Jude * 1935*5ff13fbcSAllan Jude * Zstd produces different results for prefix compression when the prefix is 1936*5ff13fbcSAllan Jude * directly adjacent to the data about to be compressed vs. when it isn't. 1937*5ff13fbcSAllan Jude * This is because zstd detects that the two buffers are contiguous and it can 1938*5ff13fbcSAllan Jude * use a more efficient match finding algorithm. However, this produces different 1939*5ff13fbcSAllan Jude * results than when the two buffers are non-contiguous. This flag forces zstd 1940*5ff13fbcSAllan Jude * to always load the prefix in non-contiguous mode, even if it happens to be 1941*5ff13fbcSAllan Jude * adjacent to the data, to guarantee determinism. 1942*5ff13fbcSAllan Jude * 1943*5ff13fbcSAllan Jude * If you really care about determinism when using a dictionary or prefix, 1944*5ff13fbcSAllan Jude * like when doing delta compression, you should select this option. It comes 1945*5ff13fbcSAllan Jude * at a speed penalty of about ~2.5% if the dictionary and data happened to be 1946*5ff13fbcSAllan Jude * contiguous, and is free if they weren't contiguous. We don't expect that 1947*5ff13fbcSAllan Jude * intentionally making the dictionary and data contiguous will be worth the 1948*5ff13fbcSAllan Jude * cost to memcpy() the data. 1949*5ff13fbcSAllan Jude */ 1950*5ff13fbcSAllan Jude #define ZSTD_c_deterministicRefPrefix ZSTD_c_experimentalParam15 1951*5ff13fbcSAllan Jude 1952a0483764SConrad Meyer /*! ZSTD_CCtx_getParameter() : 1953a0483764SConrad Meyer * Get the requested compression parameter value, selected by enum ZSTD_cParameter, 1954a0483764SConrad Meyer * and store it into int* value. 1955a0483764SConrad Meyer * @return : 0, or an error code (which can be tested with ZSTD_isError()). 1956a0483764SConrad Meyer */ 1957*5ff13fbcSAllan Jude ZSTDLIB_STATIC_API size_t ZSTD_CCtx_getParameter(const ZSTD_CCtx* cctx, ZSTD_cParameter param, int* value); 1958a0483764SConrad Meyer 1959a0483764SConrad Meyer 1960a0483764SConrad Meyer /*! ZSTD_CCtx_params : 1961a0483764SConrad Meyer * Quick howto : 1962a0483764SConrad Meyer * - ZSTD_createCCtxParams() : Create a ZSTD_CCtx_params structure 19632b9c00cbSConrad Meyer * - ZSTD_CCtxParams_setParameter() : Push parameters one by one into 1964a0483764SConrad Meyer * an existing ZSTD_CCtx_params structure. 1965a0483764SConrad Meyer * This is similar to 1966a0483764SConrad Meyer * ZSTD_CCtx_setParameter(). 1967a0483764SConrad Meyer * - ZSTD_CCtx_setParametersUsingCCtxParams() : Apply parameters to 1968a0483764SConrad Meyer * an existing CCtx. 1969a0483764SConrad Meyer * These parameters will be applied to 1970a0483764SConrad Meyer * all subsequent frames. 1971a0483764SConrad Meyer * - ZSTD_compressStream2() : Do compression using the CCtx. 1972*5ff13fbcSAllan Jude * - ZSTD_freeCCtxParams() : Free the memory, accept NULL pointer. 1973a0483764SConrad Meyer * 1974a0483764SConrad Meyer * This can be used with ZSTD_estimateCCtxSize_advanced_usingCCtxParams() 1975a0483764SConrad Meyer * for static allocation of CCtx for single-threaded compression. 1976a0483764SConrad Meyer */ 1977*5ff13fbcSAllan Jude ZSTDLIB_STATIC_API ZSTD_CCtx_params* ZSTD_createCCtxParams(void); 1978*5ff13fbcSAllan Jude ZSTDLIB_STATIC_API size_t ZSTD_freeCCtxParams(ZSTD_CCtx_params* params); /* accept NULL pointer */ 1979a0483764SConrad Meyer 1980a0483764SConrad Meyer /*! ZSTD_CCtxParams_reset() : 1981a0483764SConrad Meyer * Reset params to default values. 1982a0483764SConrad Meyer */ 1983*5ff13fbcSAllan Jude ZSTDLIB_STATIC_API size_t ZSTD_CCtxParams_reset(ZSTD_CCtx_params* params); 1984a0483764SConrad Meyer 1985a0483764SConrad Meyer /*! ZSTD_CCtxParams_init() : 1986a0483764SConrad Meyer * Initializes the compression parameters of cctxParams according to 1987a0483764SConrad Meyer * compression level. All other parameters are reset to their default values. 1988a0483764SConrad Meyer */ 1989*5ff13fbcSAllan Jude ZSTDLIB_STATIC_API size_t ZSTD_CCtxParams_init(ZSTD_CCtx_params* cctxParams, int compressionLevel); 1990a0483764SConrad Meyer 1991a0483764SConrad Meyer /*! ZSTD_CCtxParams_init_advanced() : 1992a0483764SConrad Meyer * Initializes the compression and frame parameters of cctxParams according to 1993a0483764SConrad Meyer * params. All other parameters are reset to their default values. 1994a0483764SConrad Meyer */ 1995*5ff13fbcSAllan Jude ZSTDLIB_STATIC_API size_t ZSTD_CCtxParams_init_advanced(ZSTD_CCtx_params* cctxParams, ZSTD_parameters params); 1996a0483764SConrad Meyer 1997*5ff13fbcSAllan Jude /*! ZSTD_CCtxParams_setParameter() : Requires v1.4.0+ 1998a0483764SConrad Meyer * Similar to ZSTD_CCtx_setParameter. 1999a0483764SConrad Meyer * Set one compression parameter, selected by enum ZSTD_cParameter. 2000f7cd7fe5SConrad Meyer * Parameters must be applied to a ZSTD_CCtx using 2001f7cd7fe5SConrad Meyer * ZSTD_CCtx_setParametersUsingCCtxParams(). 2002f7cd7fe5SConrad Meyer * @result : a code representing success or failure (which can be tested with 2003f7cd7fe5SConrad Meyer * ZSTD_isError()). 2004a0483764SConrad Meyer */ 2005*5ff13fbcSAllan Jude ZSTDLIB_STATIC_API size_t ZSTD_CCtxParams_setParameter(ZSTD_CCtx_params* params, ZSTD_cParameter param, int value); 2006a0483764SConrad Meyer 20072b9c00cbSConrad Meyer /*! ZSTD_CCtxParams_getParameter() : 2008a0483764SConrad Meyer * Similar to ZSTD_CCtx_getParameter. 2009a0483764SConrad Meyer * Get the requested value of one compression parameter, selected by enum ZSTD_cParameter. 2010a0483764SConrad Meyer * @result : 0, or an error code (which can be tested with ZSTD_isError()). 2011a0483764SConrad Meyer */ 2012*5ff13fbcSAllan Jude ZSTDLIB_STATIC_API size_t ZSTD_CCtxParams_getParameter(const ZSTD_CCtx_params* params, ZSTD_cParameter param, int* value); 2013a0483764SConrad Meyer 2014a0483764SConrad Meyer /*! ZSTD_CCtx_setParametersUsingCCtxParams() : 2015a0483764SConrad Meyer * Apply a set of ZSTD_CCtx_params to the compression context. 2016a0483764SConrad Meyer * This can be done even after compression is started, 2017a0483764SConrad Meyer * if nbWorkers==0, this will have no impact until a new compression is started. 2018a0483764SConrad Meyer * if nbWorkers>=1, new parameters will be picked up at next job, 2019a0483764SConrad Meyer * with a few restrictions (windowLog, pledgedSrcSize, nbWorkers, jobSize, and overlapLog are not updated). 2020a0483764SConrad Meyer */ 2021*5ff13fbcSAllan Jude ZSTDLIB_STATIC_API size_t ZSTD_CCtx_setParametersUsingCCtxParams( 2022a0483764SConrad Meyer ZSTD_CCtx* cctx, const ZSTD_CCtx_params* params); 2023a0483764SConrad Meyer 2024a0483764SConrad Meyer /*! ZSTD_compressStream2_simpleArgs() : 2025a0483764SConrad Meyer * Same as ZSTD_compressStream2(), 2026a0483764SConrad Meyer * but using only integral types as arguments. 2027a0483764SConrad Meyer * This variant might be helpful for binders from dynamic languages 2028a0483764SConrad Meyer * which have troubles handling structures containing memory pointers. 2029a0483764SConrad Meyer */ 2030*5ff13fbcSAllan Jude ZSTDLIB_STATIC_API size_t ZSTD_compressStream2_simpleArgs ( 2031a0483764SConrad Meyer ZSTD_CCtx* cctx, 2032a0483764SConrad Meyer void* dst, size_t dstCapacity, size_t* dstPos, 2033a0483764SConrad Meyer const void* src, size_t srcSize, size_t* srcPos, 2034a0483764SConrad Meyer ZSTD_EndDirective endOp); 2035a0483764SConrad Meyer 2036a0483764SConrad Meyer 2037a0483764SConrad Meyer /*************************************** 2038a0483764SConrad Meyer * Advanced decompression functions 2039a0483764SConrad Meyer ***************************************/ 20400c16b537SWarner Losh 20410c16b537SWarner Losh /*! ZSTD_isFrame() : 20420c16b537SWarner Losh * Tells if the content of `buffer` starts with a valid Frame Identifier. 20430c16b537SWarner Losh * Note : Frame Identifier is 4 bytes. If `size < 4`, @return will always be 0. 20440c16b537SWarner Losh * Note 2 : Legacy Frame Identifiers are considered valid only if Legacy Support is enabled. 20450c16b537SWarner Losh * Note 3 : Skippable Frame Identifiers are considered valid. */ 2046*5ff13fbcSAllan Jude ZSTDLIB_STATIC_API unsigned ZSTD_isFrame(const void* buffer, size_t size); 20470c16b537SWarner Losh 20480c16b537SWarner Losh /*! ZSTD_createDDict_byReference() : 20490c16b537SWarner Losh * Create a digested dictionary, ready to start decompression operation without startup delay. 20500c16b537SWarner Losh * Dictionary content is referenced, and therefore stays in dictBuffer. 20510c16b537SWarner Losh * It is important that dictBuffer outlives DDict, 20520c16b537SWarner Losh * it must remain read accessible throughout the lifetime of DDict */ 2053*5ff13fbcSAllan Jude ZSTDLIB_STATIC_API ZSTD_DDict* ZSTD_createDDict_byReference(const void* dictBuffer, size_t dictSize); 20540c16b537SWarner Losh 2055a0483764SConrad Meyer /*! ZSTD_DCtx_loadDictionary_byReference() : 2056a0483764SConrad Meyer * Same as ZSTD_DCtx_loadDictionary(), 2057a0483764SConrad Meyer * but references `dict` content instead of copying it into `dctx`. 2058a0483764SConrad Meyer * This saves memory if `dict` remains around., 2059a0483764SConrad Meyer * However, it's imperative that `dict` remains accessible (and unmodified) while being used, so it must outlive decompression. */ 2060*5ff13fbcSAllan Jude ZSTDLIB_STATIC_API size_t ZSTD_DCtx_loadDictionary_byReference(ZSTD_DCtx* dctx, const void* dict, size_t dictSize); 2061a0483764SConrad Meyer 2062a0483764SConrad Meyer /*! ZSTD_DCtx_loadDictionary_advanced() : 2063a0483764SConrad Meyer * Same as ZSTD_DCtx_loadDictionary(), 2064a0483764SConrad Meyer * but gives direct control over 2065a0483764SConrad Meyer * how to load the dictionary (by copy ? by reference ?) 2066a0483764SConrad Meyer * and how to interpret it (automatic ? force raw mode ? full mode only ?). */ 2067*5ff13fbcSAllan Jude ZSTDLIB_STATIC_API size_t ZSTD_DCtx_loadDictionary_advanced(ZSTD_DCtx* dctx, const void* dict, size_t dictSize, ZSTD_dictLoadMethod_e dictLoadMethod, ZSTD_dictContentType_e dictContentType); 2068a0483764SConrad Meyer 2069a0483764SConrad Meyer /*! ZSTD_DCtx_refPrefix_advanced() : 2070a0483764SConrad Meyer * Same as ZSTD_DCtx_refPrefix(), but gives finer control over 2071a0483764SConrad Meyer * how to interpret prefix content (automatic ? force raw mode (default) ? full mode only ?) */ 2072*5ff13fbcSAllan Jude ZSTDLIB_STATIC_API size_t ZSTD_DCtx_refPrefix_advanced(ZSTD_DCtx* dctx, const void* prefix, size_t prefixSize, ZSTD_dictContentType_e dictContentType); 2073a0483764SConrad Meyer 2074a0483764SConrad Meyer /*! ZSTD_DCtx_setMaxWindowSize() : 2075a0483764SConrad Meyer * Refuses allocating internal buffers for frames requiring a window size larger than provided limit. 2076a0483764SConrad Meyer * This protects a decoder context from reserving too much memory for itself (potential attack scenario). 2077a0483764SConrad Meyer * This parameter is only useful in streaming mode, since no internal buffer is allocated in single-pass mode. 2078a0483764SConrad Meyer * By default, a decompression context accepts all window sizes <= (1 << ZSTD_WINDOWLOG_LIMIT_DEFAULT) 2079a0483764SConrad Meyer * @return : 0, or an error code (which can be tested using ZSTD_isError()). 2080a0483764SConrad Meyer */ 2081*5ff13fbcSAllan Jude ZSTDLIB_STATIC_API size_t ZSTD_DCtx_setMaxWindowSize(ZSTD_DCtx* dctx, size_t maxWindowSize); 2082a0483764SConrad Meyer 2083f7cd7fe5SConrad Meyer /*! ZSTD_DCtx_getParameter() : 2084f7cd7fe5SConrad Meyer * Get the requested decompression parameter value, selected by enum ZSTD_dParameter, 2085f7cd7fe5SConrad Meyer * and store it into int* value. 2086f7cd7fe5SConrad Meyer * @return : 0, or an error code (which can be tested with ZSTD_isError()). 2087f7cd7fe5SConrad Meyer */ 2088*5ff13fbcSAllan Jude ZSTDLIB_STATIC_API size_t ZSTD_DCtx_getParameter(ZSTD_DCtx* dctx, ZSTD_dParameter param, int* value); 2089f7cd7fe5SConrad Meyer 2090a0483764SConrad Meyer /* ZSTD_d_format 2091a0483764SConrad Meyer * experimental parameter, 2092a0483764SConrad Meyer * allowing selection between ZSTD_format_e input compression formats 2093a0483764SConrad Meyer */ 2094a0483764SConrad Meyer #define ZSTD_d_format ZSTD_d_experimentalParam1 209537f1f268SConrad Meyer /* ZSTD_d_stableOutBuffer 209637f1f268SConrad Meyer * Experimental parameter. 209737f1f268SConrad Meyer * Default is 0 == disabled. Set to 1 to enable. 209837f1f268SConrad Meyer * 209937f1f268SConrad Meyer * Tells the decompressor that the ZSTD_outBuffer will ALWAYS be the same 210037f1f268SConrad Meyer * between calls, except for the modifications that zstd makes to pos (the 210137f1f268SConrad Meyer * caller must not modify pos). This is checked by the decompressor, and 210237f1f268SConrad Meyer * decompression will fail if it ever changes. Therefore the ZSTD_outBuffer 210337f1f268SConrad Meyer * MUST be large enough to fit the entire decompressed frame. This will be 210437f1f268SConrad Meyer * checked when the frame content size is known. The data in the ZSTD_outBuffer 210537f1f268SConrad Meyer * in the range [dst, dst + pos) MUST not be modified during decompression 210637f1f268SConrad Meyer * or you will get data corruption. 210737f1f268SConrad Meyer * 210837f1f268SConrad Meyer * When this flags is enabled zstd won't allocate an output buffer, because 210937f1f268SConrad Meyer * it can write directly to the ZSTD_outBuffer, but it will still allocate 211037f1f268SConrad Meyer * an input buffer large enough to fit any compressed block. This will also 211137f1f268SConrad Meyer * avoid the memcpy() from the internal output buffer to the ZSTD_outBuffer. 211237f1f268SConrad Meyer * If you need to avoid the input buffer allocation use the buffer-less 211337f1f268SConrad Meyer * streaming API. 211437f1f268SConrad Meyer * 211537f1f268SConrad Meyer * NOTE: So long as the ZSTD_outBuffer always points to valid memory, using 211637f1f268SConrad Meyer * this flag is ALWAYS memory safe, and will never access out-of-bounds 211737f1f268SConrad Meyer * memory. However, decompression WILL fail if you violate the preconditions. 211837f1f268SConrad Meyer * 211937f1f268SConrad Meyer * WARNING: The data in the ZSTD_outBuffer in the range [dst, dst + pos) MUST 212037f1f268SConrad Meyer * not be modified during decompression or you will get data corruption. This 212137f1f268SConrad Meyer * is because zstd needs to reference data in the ZSTD_outBuffer to regenerate 212237f1f268SConrad Meyer * matches. Normally zstd maintains its own buffer for this purpose, but passing 212337f1f268SConrad Meyer * this flag tells zstd to use the user provided buffer. 212437f1f268SConrad Meyer */ 212537f1f268SConrad Meyer #define ZSTD_d_stableOutBuffer ZSTD_d_experimentalParam2 2126a0483764SConrad Meyer 2127f7cd7fe5SConrad Meyer /* ZSTD_d_forceIgnoreChecksum 2128f7cd7fe5SConrad Meyer * Experimental parameter. 2129f7cd7fe5SConrad Meyer * Default is 0 == disabled. Set to 1 to enable 2130f7cd7fe5SConrad Meyer * 2131f7cd7fe5SConrad Meyer * Tells the decompressor to skip checksum validation during decompression, regardless 2132f7cd7fe5SConrad Meyer * of whether checksumming was specified during compression. This offers some 2133f7cd7fe5SConrad Meyer * slight performance benefits, and may be useful for debugging. 2134f7cd7fe5SConrad Meyer * Param has values of type ZSTD_forceIgnoreChecksum_e 2135f7cd7fe5SConrad Meyer */ 2136f7cd7fe5SConrad Meyer #define ZSTD_d_forceIgnoreChecksum ZSTD_d_experimentalParam3 2137f7cd7fe5SConrad Meyer 2138*5ff13fbcSAllan Jude /* ZSTD_d_refMultipleDDicts 2139*5ff13fbcSAllan Jude * Experimental parameter. 2140*5ff13fbcSAllan Jude * Default is 0 == disabled. Set to 1 to enable 2141*5ff13fbcSAllan Jude * 2142*5ff13fbcSAllan Jude * If enabled and dctx is allocated on the heap, then additional memory will be allocated 2143*5ff13fbcSAllan Jude * to store references to multiple ZSTD_DDict. That is, multiple calls of ZSTD_refDDict() 2144*5ff13fbcSAllan Jude * using a given ZSTD_DCtx, rather than overwriting the previous DDict reference, will instead 2145*5ff13fbcSAllan Jude * store all references. At decompression time, the appropriate dictID is selected 2146*5ff13fbcSAllan Jude * from the set of DDicts based on the dictID in the frame. 2147*5ff13fbcSAllan Jude * 2148*5ff13fbcSAllan Jude * Usage is simply calling ZSTD_refDDict() on multiple dict buffers. 2149*5ff13fbcSAllan Jude * 2150*5ff13fbcSAllan Jude * Param has values of byte ZSTD_refMultipleDDicts_e 2151*5ff13fbcSAllan Jude * 2152*5ff13fbcSAllan Jude * WARNING: Enabling this parameter and calling ZSTD_DCtx_refDDict(), will trigger memory 2153*5ff13fbcSAllan Jude * allocation for the hash table. ZSTD_freeDCtx() also frees this memory. 2154*5ff13fbcSAllan Jude * Memory is allocated as per ZSTD_DCtx::customMem. 2155*5ff13fbcSAllan Jude * 2156*5ff13fbcSAllan Jude * Although this function allocates memory for the table, the user is still responsible for 2157*5ff13fbcSAllan Jude * memory management of the underlying ZSTD_DDict* themselves. 2158*5ff13fbcSAllan Jude */ 2159*5ff13fbcSAllan Jude #define ZSTD_d_refMultipleDDicts ZSTD_d_experimentalParam4 2160*5ff13fbcSAllan Jude 2161*5ff13fbcSAllan Jude 2162a0483764SConrad Meyer /*! ZSTD_DCtx_setFormat() : 2163*5ff13fbcSAllan Jude * This function is REDUNDANT. Prefer ZSTD_DCtx_setParameter(). 2164a0483764SConrad Meyer * Instruct the decoder context about what kind of data to decode next. 2165a0483764SConrad Meyer * This instruction is mandatory to decode data without a fully-formed header, 2166a0483764SConrad Meyer * such ZSTD_f_zstd1_magicless for example. 2167a0483764SConrad Meyer * @return : 0, or an error code (which can be tested using ZSTD_isError()). */ 2168*5ff13fbcSAllan Jude ZSTD_DEPRECATED("use ZSTD_DCtx_setParameter() instead") 2169*5ff13fbcSAllan Jude size_t ZSTD_DCtx_setFormat(ZSTD_DCtx* dctx, ZSTD_format_e format); 2170a0483764SConrad Meyer 2171a0483764SConrad Meyer /*! ZSTD_decompressStream_simpleArgs() : 2172a0483764SConrad Meyer * Same as ZSTD_decompressStream(), 2173a0483764SConrad Meyer * but using only integral types as arguments. 2174a0483764SConrad Meyer * This can be helpful for binders from dynamic languages 2175a0483764SConrad Meyer * which have troubles handling structures containing memory pointers. 2176a0483764SConrad Meyer */ 2177*5ff13fbcSAllan Jude ZSTDLIB_STATIC_API size_t ZSTD_decompressStream_simpleArgs ( 2178a0483764SConrad Meyer ZSTD_DCtx* dctx, 2179a0483764SConrad Meyer void* dst, size_t dstCapacity, size_t* dstPos, 2180a0483764SConrad Meyer const void* src, size_t srcSize, size_t* srcPos); 2181a0483764SConrad Meyer 21820c16b537SWarner Losh 21830c16b537SWarner Losh /******************************************************************** 21840c16b537SWarner Losh * Advanced streaming functions 2185a0483764SConrad Meyer * Warning : most of these functions are now redundant with the Advanced API. 2186a0483764SConrad Meyer * Once Advanced API reaches "stable" status, 2187a0483764SConrad Meyer * redundant functions will be deprecated, and then at some point removed. 21880c16b537SWarner Losh ********************************************************************/ 21890c16b537SWarner Losh 21900c16b537SWarner Losh /*===== Advanced Streaming compression functions =====*/ 2191f7cd7fe5SConrad Meyer 2192f7cd7fe5SConrad Meyer /*! ZSTD_initCStream_srcSize() : 2193*5ff13fbcSAllan Jude * This function is DEPRECATED, and equivalent to: 21942b9c00cbSConrad Meyer * ZSTD_CCtx_reset(zcs, ZSTD_reset_session_only); 21952b9c00cbSConrad Meyer * ZSTD_CCtx_refCDict(zcs, NULL); // clear the dictionary (if any) 21962b9c00cbSConrad Meyer * ZSTD_CCtx_setParameter(zcs, ZSTD_c_compressionLevel, compressionLevel); 21972b9c00cbSConrad Meyer * ZSTD_CCtx_setPledgedSrcSize(zcs, pledgedSrcSize); 21982b9c00cbSConrad Meyer * 21992b9c00cbSConrad Meyer * pledgedSrcSize must be correct. If it is not known at init time, use 22002b9c00cbSConrad Meyer * ZSTD_CONTENTSIZE_UNKNOWN. Note that, for compatibility with older programs, 22012b9c00cbSConrad Meyer * "0" also disables frame content size field. It may be enabled in the future. 2202*5ff13fbcSAllan Jude * This prototype will generate compilation warnings. 22032b9c00cbSConrad Meyer */ 2204*5ff13fbcSAllan Jude ZSTD_DEPRECATED("use ZSTD_CCtx_reset, see zstd.h for detailed instructions") 2205*5ff13fbcSAllan Jude size_t ZSTD_initCStream_srcSize(ZSTD_CStream* zcs, 22069cbefe25SConrad Meyer int compressionLevel, 22079cbefe25SConrad Meyer unsigned long long pledgedSrcSize); 22089cbefe25SConrad Meyer 2209f7cd7fe5SConrad Meyer /*! ZSTD_initCStream_usingDict() : 2210*5ff13fbcSAllan Jude * This function is DEPRECATED, and is equivalent to: 22112b9c00cbSConrad Meyer * ZSTD_CCtx_reset(zcs, ZSTD_reset_session_only); 22122b9c00cbSConrad Meyer * ZSTD_CCtx_setParameter(zcs, ZSTD_c_compressionLevel, compressionLevel); 22132b9c00cbSConrad Meyer * ZSTD_CCtx_loadDictionary(zcs, dict, dictSize); 22142b9c00cbSConrad Meyer * 22152b9c00cbSConrad Meyer * Creates of an internal CDict (incompatible with static CCtx), except if 22162b9c00cbSConrad Meyer * dict == NULL or dictSize < 8, in which case no dict is used. 22179cbefe25SConrad Meyer * Note: dict is loaded with ZSTD_dct_auto (treated as a full zstd dictionary if 22182b9c00cbSConrad Meyer * it begins with ZSTD_MAGIC_DICTIONARY, else as raw content) and ZSTD_dlm_byCopy. 2219*5ff13fbcSAllan Jude * This prototype will generate compilation warnings. 22202b9c00cbSConrad Meyer */ 2221*5ff13fbcSAllan Jude ZSTD_DEPRECATED("use ZSTD_CCtx_reset, see zstd.h for detailed instructions") 2222*5ff13fbcSAllan Jude size_t ZSTD_initCStream_usingDict(ZSTD_CStream* zcs, 22239cbefe25SConrad Meyer const void* dict, size_t dictSize, 22249cbefe25SConrad Meyer int compressionLevel); 22259cbefe25SConrad Meyer 2226f7cd7fe5SConrad Meyer /*! ZSTD_initCStream_advanced() : 2227*5ff13fbcSAllan Jude * This function is DEPRECATED, and is approximately equivalent to: 22282b9c00cbSConrad Meyer * ZSTD_CCtx_reset(zcs, ZSTD_reset_session_only); 22299cbefe25SConrad Meyer * // Pseudocode: Set each zstd parameter and leave the rest as-is. 22309cbefe25SConrad Meyer * for ((param, value) : params) { 22319cbefe25SConrad Meyer * ZSTD_CCtx_setParameter(zcs, param, value); 22329cbefe25SConrad Meyer * } 22332b9c00cbSConrad Meyer * ZSTD_CCtx_setPledgedSrcSize(zcs, pledgedSrcSize); 22342b9c00cbSConrad Meyer * ZSTD_CCtx_loadDictionary(zcs, dict, dictSize); 22352b9c00cbSConrad Meyer * 22369cbefe25SConrad Meyer * dict is loaded with ZSTD_dct_auto and ZSTD_dlm_byCopy. 22379cbefe25SConrad Meyer * pledgedSrcSize must be correct. 22389cbefe25SConrad Meyer * If srcSize is not known at init time, use value ZSTD_CONTENTSIZE_UNKNOWN. 2239*5ff13fbcSAllan Jude * This prototype will generate compilation warnings. 22402b9c00cbSConrad Meyer */ 2241*5ff13fbcSAllan Jude ZSTD_DEPRECATED("use ZSTD_CCtx_reset, see zstd.h for detailed instructions") 2242*5ff13fbcSAllan Jude size_t ZSTD_initCStream_advanced(ZSTD_CStream* zcs, 22439cbefe25SConrad Meyer const void* dict, size_t dictSize, 22449cbefe25SConrad Meyer ZSTD_parameters params, 22459cbefe25SConrad Meyer unsigned long long pledgedSrcSize); 22469cbefe25SConrad Meyer 2247f7cd7fe5SConrad Meyer /*! ZSTD_initCStream_usingCDict() : 2248*5ff13fbcSAllan Jude * This function is DEPRECATED, and equivalent to: 22492b9c00cbSConrad Meyer * ZSTD_CCtx_reset(zcs, ZSTD_reset_session_only); 22502b9c00cbSConrad Meyer * ZSTD_CCtx_refCDict(zcs, cdict); 22512b9c00cbSConrad Meyer * 22522b9c00cbSConrad Meyer * note : cdict will just be referenced, and must outlive compression session 2253*5ff13fbcSAllan Jude * This prototype will generate compilation warnings. 22542b9c00cbSConrad Meyer */ 2255*5ff13fbcSAllan Jude ZSTD_DEPRECATED("use ZSTD_CCtx_reset and ZSTD_CCtx_refCDict, see zstd.h for detailed instructions") 2256*5ff13fbcSAllan Jude size_t ZSTD_initCStream_usingCDict(ZSTD_CStream* zcs, const ZSTD_CDict* cdict); 22579cbefe25SConrad Meyer 2258f7cd7fe5SConrad Meyer /*! ZSTD_initCStream_usingCDict_advanced() : 22599cbefe25SConrad Meyer * This function is DEPRECATED, and is approximately equivalent to: 22602b9c00cbSConrad Meyer * ZSTD_CCtx_reset(zcs, ZSTD_reset_session_only); 22619cbefe25SConrad Meyer * // Pseudocode: Set each zstd frame parameter and leave the rest as-is. 22629cbefe25SConrad Meyer * for ((fParam, value) : fParams) { 22639cbefe25SConrad Meyer * ZSTD_CCtx_setParameter(zcs, fParam, value); 22649cbefe25SConrad Meyer * } 22652b9c00cbSConrad Meyer * ZSTD_CCtx_setPledgedSrcSize(zcs, pledgedSrcSize); 22662b9c00cbSConrad Meyer * ZSTD_CCtx_refCDict(zcs, cdict); 22672b9c00cbSConrad Meyer * 22682b9c00cbSConrad Meyer * same as ZSTD_initCStream_usingCDict(), with control over frame parameters. 22692b9c00cbSConrad Meyer * pledgedSrcSize must be correct. If srcSize is not known at init time, use 22702b9c00cbSConrad Meyer * value ZSTD_CONTENTSIZE_UNKNOWN. 2271*5ff13fbcSAllan Jude * This prototype will generate compilation warnings. 22722b9c00cbSConrad Meyer */ 2273*5ff13fbcSAllan Jude ZSTD_DEPRECATED("use ZSTD_CCtx_reset and ZSTD_CCtx_refCDict, see zstd.h for detailed instructions") 2274*5ff13fbcSAllan Jude size_t ZSTD_initCStream_usingCDict_advanced(ZSTD_CStream* zcs, 22759cbefe25SConrad Meyer const ZSTD_CDict* cdict, 22769cbefe25SConrad Meyer ZSTD_frameParameters fParams, 22779cbefe25SConrad Meyer unsigned long long pledgedSrcSize); 22780c16b537SWarner Losh 22790c16b537SWarner Losh /*! ZSTD_resetCStream() : 2280*5ff13fbcSAllan Jude * This function is DEPRECATED, and is equivalent to: 22812b9c00cbSConrad Meyer * ZSTD_CCtx_reset(zcs, ZSTD_reset_session_only); 22822b9c00cbSConrad Meyer * ZSTD_CCtx_setPledgedSrcSize(zcs, pledgedSrcSize); 2283*5ff13fbcSAllan Jude * Note: ZSTD_resetCStream() interprets pledgedSrcSize == 0 as ZSTD_CONTENTSIZE_UNKNOWN, but 2284*5ff13fbcSAllan Jude * ZSTD_CCtx_setPledgedSrcSize() does not do the same, so ZSTD_CONTENTSIZE_UNKNOWN must be 2285*5ff13fbcSAllan Jude * explicitly specified. 22862b9c00cbSConrad Meyer * 2287a0483764SConrad Meyer * start a new frame, using same parameters from previous frame. 22880f743729SConrad Meyer * This is typically useful to skip dictionary loading stage, since it will re-use it in-place. 22890c16b537SWarner Losh * Note that zcs must be init at least once before using ZSTD_resetCStream(). 2290052d3c12SConrad Meyer * If pledgedSrcSize is not known at reset time, use macro ZSTD_CONTENTSIZE_UNKNOWN. 22910c16b537SWarner Losh * If pledgedSrcSize > 0, its value must be correct, as it will be written in header, and controlled at the end. 2292052d3c12SConrad Meyer * For the time being, pledgedSrcSize==0 is interpreted as "srcSize unknown" for compatibility with older programs, 229319fcbaf1SConrad Meyer * but it will change to mean "empty" in future version, so use macro ZSTD_CONTENTSIZE_UNKNOWN instead. 22940f743729SConrad Meyer * @return : 0, or an error code (which can be tested using ZSTD_isError()) 2295*5ff13fbcSAllan Jude * This prototype will generate compilation warnings. 22960f743729SConrad Meyer */ 2297*5ff13fbcSAllan Jude ZSTD_DEPRECATED("use ZSTD_CCtx_reset, see zstd.h for detailed instructions") 2298*5ff13fbcSAllan Jude size_t ZSTD_resetCStream(ZSTD_CStream* zcs, unsigned long long pledgedSrcSize); 22990c16b537SWarner Losh 23000c16b537SWarner Losh 230119fcbaf1SConrad Meyer typedef struct { 23020f743729SConrad Meyer unsigned long long ingested; /* nb input bytes read and buffered */ 23030f743729SConrad Meyer unsigned long long consumed; /* nb input bytes actually compressed */ 23040f743729SConrad Meyer unsigned long long produced; /* nb of compressed bytes generated and buffered */ 23050f743729SConrad Meyer unsigned long long flushed; /* nb of compressed bytes flushed : not provided; can be tracked from caller side */ 23060f743729SConrad Meyer unsigned currentJobID; /* MT only : latest started job nb */ 23070f743729SConrad Meyer unsigned nbActiveWorkers; /* MT only : nb of workers actively compressing at probe time */ 230819fcbaf1SConrad Meyer } ZSTD_frameProgression; 230919fcbaf1SConrad Meyer 231019fcbaf1SConrad Meyer /* ZSTD_getFrameProgression() : 231119fcbaf1SConrad Meyer * tells how much data has been ingested (read from input) 231219fcbaf1SConrad Meyer * consumed (input actually compressed) and produced (output) for current frame. 23130f743729SConrad Meyer * Note : (ingested - consumed) is amount of input data buffered internally, not yet compressed. 23140f743729SConrad Meyer * Aggregates progression inside active worker threads. 231519fcbaf1SConrad Meyer */ 2316*5ff13fbcSAllan Jude ZSTDLIB_STATIC_API ZSTD_frameProgression ZSTD_getFrameProgression(const ZSTD_CCtx* cctx); 23170f743729SConrad Meyer 23180f743729SConrad Meyer /*! ZSTD_toFlushNow() : 23190f743729SConrad Meyer * Tell how many bytes are ready to be flushed immediately. 23200f743729SConrad Meyer * Useful for multithreading scenarios (nbWorkers >= 1). 23210f743729SConrad Meyer * Probe the oldest active job, defined as oldest job not yet entirely flushed, 23220f743729SConrad Meyer * and check its output buffer. 23230f743729SConrad Meyer * @return : amount of data stored in oldest job and ready to be flushed immediately. 23240f743729SConrad Meyer * if @return == 0, it means either : 23250f743729SConrad Meyer * + there is no active job (could be checked with ZSTD_frameProgression()), or 23260f743729SConrad Meyer * + oldest job is still actively compressing data, 23270f743729SConrad Meyer * but everything it has produced has also been flushed so far, 2328a0483764SConrad Meyer * therefore flush speed is limited by production speed of oldest job 2329a0483764SConrad Meyer * irrespective of the speed of concurrent (and newer) jobs. 23300f743729SConrad Meyer */ 2331*5ff13fbcSAllan Jude ZSTDLIB_STATIC_API size_t ZSTD_toFlushNow(ZSTD_CCtx* cctx); 233219fcbaf1SConrad Meyer 233319fcbaf1SConrad Meyer 23340c16b537SWarner Losh /*===== Advanced Streaming decompression functions =====*/ 2335f7cd7fe5SConrad Meyer 2336f7cd7fe5SConrad Meyer /*! 23372b9c00cbSConrad Meyer * This function is deprecated, and is equivalent to: 23382b9c00cbSConrad Meyer * 23392b9c00cbSConrad Meyer * ZSTD_DCtx_reset(zds, ZSTD_reset_session_only); 23402b9c00cbSConrad Meyer * ZSTD_DCtx_loadDictionary(zds, dict, dictSize); 23412b9c00cbSConrad Meyer * 23422b9c00cbSConrad Meyer * note: no dictionary will be used if dict == NULL or dictSize < 8 23439cbefe25SConrad Meyer * Note : this prototype will be marked as deprecated and generate compilation warnings on reaching v1.5.x 23442b9c00cbSConrad Meyer */ 2345*5ff13fbcSAllan Jude ZSTDLIB_STATIC_API size_t ZSTD_initDStream_usingDict(ZSTD_DStream* zds, const void* dict, size_t dictSize); 23469cbefe25SConrad Meyer 2347f7cd7fe5SConrad Meyer /*! 23482b9c00cbSConrad Meyer * This function is deprecated, and is equivalent to: 23492b9c00cbSConrad Meyer * 23502b9c00cbSConrad Meyer * ZSTD_DCtx_reset(zds, ZSTD_reset_session_only); 23512b9c00cbSConrad Meyer * ZSTD_DCtx_refDDict(zds, ddict); 23522b9c00cbSConrad Meyer * 23532b9c00cbSConrad Meyer * note : ddict is referenced, it must outlive decompression session 23549cbefe25SConrad Meyer * Note : this prototype will be marked as deprecated and generate compilation warnings on reaching v1.5.x 23552b9c00cbSConrad Meyer */ 2356*5ff13fbcSAllan Jude ZSTDLIB_STATIC_API size_t ZSTD_initDStream_usingDDict(ZSTD_DStream* zds, const ZSTD_DDict* ddict); 23579cbefe25SConrad Meyer 2358f7cd7fe5SConrad Meyer /*! 23592b9c00cbSConrad Meyer * This function is deprecated, and is equivalent to: 23602b9c00cbSConrad Meyer * 23612b9c00cbSConrad Meyer * ZSTD_DCtx_reset(zds, ZSTD_reset_session_only); 23622b9c00cbSConrad Meyer * 23632b9c00cbSConrad Meyer * re-use decompression parameters from previous init; saves dictionary loading 23649cbefe25SConrad Meyer * Note : this prototype will be marked as deprecated and generate compilation warnings on reaching v1.5.x 23652b9c00cbSConrad Meyer */ 2366*5ff13fbcSAllan Jude ZSTDLIB_STATIC_API size_t ZSTD_resetDStream(ZSTD_DStream* zds); 23670c16b537SWarner Losh 23680c16b537SWarner Losh 23690c16b537SWarner Losh /********************************************************************* 23700c16b537SWarner Losh * Buffer-less and synchronous inner streaming functions 23710c16b537SWarner Losh * 23720c16b537SWarner Losh * This is an advanced API, giving full control over buffer management, for users which need direct control over memory. 23730c16b537SWarner Losh * But it's also a complex one, with several restrictions, documented below. 23740c16b537SWarner Losh * Prefer normal streaming API for an easier experience. 23750c16b537SWarner Losh ********************************************************************* */ 23760c16b537SWarner Losh 23770c16b537SWarner Losh /** 23780c16b537SWarner Losh Buffer-less streaming compression (synchronous mode) 23790c16b537SWarner Losh 23800c16b537SWarner Losh A ZSTD_CCtx object is required to track streaming operations. 23810c16b537SWarner Losh Use ZSTD_createCCtx() / ZSTD_freeCCtx() to manage resource. 23820c16b537SWarner Losh ZSTD_CCtx object can be re-used multiple times within successive compression operations. 23830c16b537SWarner Losh 23840c16b537SWarner Losh Start by initializing a context. 2385*5ff13fbcSAllan Jude Use ZSTD_compressBegin(), or ZSTD_compressBegin_usingDict() for dictionary compression. 23860c16b537SWarner Losh It's also possible to duplicate a reference context which has already been initialized, using ZSTD_copyCCtx() 23870c16b537SWarner Losh 23880c16b537SWarner Losh Then, consume your input using ZSTD_compressContinue(). 23890c16b537SWarner Losh There are some important considerations to keep in mind when using this advanced function : 23900c16b537SWarner Losh - ZSTD_compressContinue() has no internal buffer. It uses externally provided buffers only. 23910c16b537SWarner Losh - Interface is synchronous : input is consumed entirely and produces 1+ compressed blocks. 23920c16b537SWarner Losh - Caller must ensure there is enough space in `dst` to store compressed data under worst case scenario. 23930c16b537SWarner Losh Worst case evaluation is provided by ZSTD_compressBound(). 23940c16b537SWarner Losh ZSTD_compressContinue() doesn't guarantee recover after a failed compression. 23950c16b537SWarner Losh - ZSTD_compressContinue() presumes prior input ***is still accessible and unmodified*** (up to maximum distance size, see WindowLog). 23960c16b537SWarner Losh It remembers all previous contiguous blocks, plus one separated memory segment (which can itself consists of multiple contiguous blocks) 23970c16b537SWarner Losh - ZSTD_compressContinue() detects that prior input has been overwritten when `src` buffer overlaps. 23980c16b537SWarner Losh In which case, it will "discard" the relevant memory section from its history. 23990c16b537SWarner Losh 24000c16b537SWarner Losh Finish a frame with ZSTD_compressEnd(), which will write the last block(s) and optional checksum. 24010c16b537SWarner Losh It's possible to use srcSize==0, in which case, it will write a final empty block to end the frame. 24020c16b537SWarner Losh Without last block mark, frames are considered unfinished (hence corrupted) by compliant decoders. 24030c16b537SWarner Losh 24040c16b537SWarner Losh `ZSTD_CCtx` object can be re-used (ZSTD_compressBegin()) to compress again. 24050c16b537SWarner Losh */ 24060c16b537SWarner Losh 24070c16b537SWarner Losh /*===== Buffer-less streaming compression functions =====*/ 2408*5ff13fbcSAllan Jude ZSTDLIB_STATIC_API size_t ZSTD_compressBegin(ZSTD_CCtx* cctx, int compressionLevel); 2409*5ff13fbcSAllan Jude ZSTDLIB_STATIC_API size_t ZSTD_compressBegin_usingDict(ZSTD_CCtx* cctx, const void* dict, size_t dictSize, int compressionLevel); 2410*5ff13fbcSAllan Jude ZSTDLIB_STATIC_API size_t ZSTD_compressBegin_usingCDict(ZSTD_CCtx* cctx, const ZSTD_CDict* cdict); /**< note: fails if cdict==NULL */ 2411*5ff13fbcSAllan Jude ZSTDLIB_STATIC_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 */ 24120c16b537SWarner Losh 2413*5ff13fbcSAllan Jude ZSTDLIB_STATIC_API size_t ZSTD_compressContinue(ZSTD_CCtx* cctx, void* dst, size_t dstCapacity, const void* src, size_t srcSize); 2414*5ff13fbcSAllan Jude ZSTDLIB_STATIC_API size_t ZSTD_compressEnd(ZSTD_CCtx* cctx, void* dst, size_t dstCapacity, const void* src, size_t srcSize); 24150c16b537SWarner Losh 2416*5ff13fbcSAllan Jude /* The ZSTD_compressBegin_advanced() and ZSTD_compressBegin_usingCDict_advanced() are now DEPRECATED and will generate a compiler warning */ 2417*5ff13fbcSAllan Jude ZSTD_DEPRECATED("use advanced API to access custom parameters") 2418*5ff13fbcSAllan Jude 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 */ 2419*5ff13fbcSAllan Jude ZSTD_DEPRECATED("use advanced API to access custom parameters") 2420*5ff13fbcSAllan Jude 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 */ 2421f7cd7fe5SConrad Meyer /** 24220c16b537SWarner Losh Buffer-less streaming decompression (synchronous mode) 24230c16b537SWarner Losh 24240c16b537SWarner Losh A ZSTD_DCtx object is required to track streaming operations. 24250c16b537SWarner Losh Use ZSTD_createDCtx() / ZSTD_freeDCtx() to manage it. 24260c16b537SWarner Losh A ZSTD_DCtx object can be re-used multiple times. 24270c16b537SWarner Losh 24280c16b537SWarner Losh First typical operation is to retrieve frame parameters, using ZSTD_getFrameHeader(). 24290c16b537SWarner Losh Frame header is extracted from the beginning of compressed frame, so providing only the frame's beginning is enough. 24300c16b537SWarner Losh Data fragment must be large enough to ensure successful decoding. 24310c16b537SWarner Losh `ZSTD_frameHeaderSize_max` bytes is guaranteed to always be large enough. 24320c16b537SWarner Losh @result : 0 : successful decoding, the `ZSTD_frameHeader` structure is correctly filled. 24330c16b537SWarner Losh >0 : `srcSize` is too small, please provide at least @result bytes on next attempt. 24340c16b537SWarner Losh errorCode, which can be tested using ZSTD_isError(). 24350c16b537SWarner Losh 24360c16b537SWarner Losh It fills a ZSTD_frameHeader structure with important information to correctly decode the frame, 24370c16b537SWarner Losh such as the dictionary ID, content size, or maximum back-reference distance (`windowSize`). 24380c16b537SWarner Losh Note that these values could be wrong, either because of data corruption, or because a 3rd party deliberately spoofs false information. 24390c16b537SWarner Losh As a consequence, check that values remain within valid application range. 24400c16b537SWarner Losh For example, do not allocate memory blindly, check that `windowSize` is within expectation. 24410c16b537SWarner Losh Each application can set its own limits, depending on local restrictions. 24420c16b537SWarner Losh For extended interoperability, it is recommended to support `windowSize` of at least 8 MB. 24430c16b537SWarner Losh 24440c16b537SWarner Losh ZSTD_decompressContinue() needs previous data blocks during decompression, up to `windowSize` bytes. 24450c16b537SWarner Losh ZSTD_decompressContinue() is very sensitive to contiguity, 24460c16b537SWarner Losh if 2 blocks don't follow each other, make sure that either the compressor breaks contiguity at the same place, 24470c16b537SWarner Losh or that previous contiguous segment is large enough to properly handle maximum back-reference distance. 24480c16b537SWarner Losh There are multiple ways to guarantee this condition. 24490c16b537SWarner Losh 24500c16b537SWarner Losh The most memory efficient way is to use a round buffer of sufficient size. 24510c16b537SWarner Losh Sufficient size is determined by invoking ZSTD_decodingBufferSize_min(), 24520c16b537SWarner Losh which can @return an error code if required value is too large for current system (in 32-bits mode). 24530c16b537SWarner Losh In a round buffer methodology, ZSTD_decompressContinue() decompresses each block next to previous one, 24540c16b537SWarner Losh up to the moment there is not enough room left in the buffer to guarantee decoding another full block, 24550c16b537SWarner Losh which maximum size is provided in `ZSTD_frameHeader` structure, field `blockSizeMax`. 24560c16b537SWarner Losh At which point, decoding can resume from the beginning of the buffer. 24570c16b537SWarner Losh Note that already decoded data stored in the buffer should be flushed before being overwritten. 24580c16b537SWarner Losh 24590c16b537SWarner Losh There are alternatives possible, for example using two or more buffers of size `windowSize` each, though they consume more memory. 24600c16b537SWarner Losh 24610c16b537SWarner Losh Finally, if you control the compression process, you can also ignore all buffer size rules, 24620c16b537SWarner Losh as long as the encoder and decoder progress in "lock-step", 24630c16b537SWarner Losh aka use exactly the same buffer sizes, break contiguity at the same place, etc. 24640c16b537SWarner Losh 24650c16b537SWarner Losh Once buffers are setup, start decompression, with ZSTD_decompressBegin(). 24660c16b537SWarner Losh If decompression requires a dictionary, use ZSTD_decompressBegin_usingDict() or ZSTD_decompressBegin_usingDDict(). 24670c16b537SWarner Losh 24680c16b537SWarner Losh Then use ZSTD_nextSrcSizeToDecompress() and ZSTD_decompressContinue() alternatively. 24690c16b537SWarner Losh ZSTD_nextSrcSizeToDecompress() tells how many bytes to provide as 'srcSize' to ZSTD_decompressContinue(). 24700c16b537SWarner Losh ZSTD_decompressContinue() requires this _exact_ amount of bytes, or it will fail. 24710c16b537SWarner Losh 24720c16b537SWarner Losh @result of ZSTD_decompressContinue() is the number of bytes regenerated within 'dst' (necessarily <= dstCapacity). 24730c16b537SWarner Losh It can be zero : it just means ZSTD_decompressContinue() has decoded some metadata item. 24740c16b537SWarner Losh It can also be an error code, which can be tested with ZSTD_isError(). 24750c16b537SWarner Losh 24760c16b537SWarner Losh A frame is fully decoded when ZSTD_nextSrcSizeToDecompress() returns zero. 24770c16b537SWarner Losh Context can then be reset to start a new decompression. 24780c16b537SWarner Losh 24790c16b537SWarner Losh Note : it's possible to know if next input to present is a header or a block, using ZSTD_nextInputType(). 24800c16b537SWarner Losh This information is not required to properly decode a frame. 24810c16b537SWarner Losh 24820c16b537SWarner Losh == Special case : skippable frames == 24830c16b537SWarner Losh 24840c16b537SWarner Losh Skippable frames allow integration of user-defined data into a flow of concatenated frames. 24850c16b537SWarner Losh Skippable frames will be ignored (skipped) by decompressor. 24860c16b537SWarner Losh The format of skippable frames is as follows : 24870c16b537SWarner Losh a) Skippable frame ID - 4 Bytes, Little endian format, any value from 0x184D2A50 to 0x184D2A5F 24880c16b537SWarner Losh b) Frame Size - 4 Bytes, Little endian format, unsigned 32-bits 24890c16b537SWarner Losh c) Frame Content - any content (User Data) of length equal to Frame Size 24900c16b537SWarner Losh For skippable frames ZSTD_getFrameHeader() returns zfhPtr->frameType==ZSTD_skippableFrame. 24910c16b537SWarner Losh For skippable frames ZSTD_decompressContinue() always returns 0 : it only skips the content. 24920c16b537SWarner Losh */ 24930c16b537SWarner Losh 24940c16b537SWarner Losh /*===== Buffer-less streaming decompression functions =====*/ 24950c16b537SWarner Losh typedef enum { ZSTD_frame, ZSTD_skippableFrame } ZSTD_frameType_e; 24960c16b537SWarner Losh typedef struct { 24970c16b537SWarner Losh unsigned long long frameContentSize; /* if == ZSTD_CONTENTSIZE_UNKNOWN, it means this field is not available. 0 means "empty" */ 24980c16b537SWarner Losh unsigned long long windowSize; /* can be very large, up to <= frameContentSize */ 24990c16b537SWarner Losh unsigned blockSizeMax; 25000c16b537SWarner Losh ZSTD_frameType_e frameType; /* if == ZSTD_skippableFrame, frameContentSize is the size of skippable content */ 25010c16b537SWarner Losh unsigned headerSize; 25020c16b537SWarner Losh unsigned dictID; 25030c16b537SWarner Losh unsigned checksumFlag; 25040c16b537SWarner Losh } ZSTD_frameHeader; 2505a0483764SConrad Meyer 25064d3f1eafSConrad Meyer /*! ZSTD_getFrameHeader() : 25070f743729SConrad Meyer * decode Frame Header, or requires larger `srcSize`. 25080f743729SConrad Meyer * @return : 0, `zfhPtr` is correctly filled, 25090f743729SConrad Meyer * >0, `srcSize` is too small, value is wanted `srcSize` amount, 25100f743729SConrad Meyer * or an error code, which can be tested using ZSTD_isError() */ 2511*5ff13fbcSAllan Jude ZSTDLIB_STATIC_API size_t ZSTD_getFrameHeader(ZSTD_frameHeader* zfhPtr, const void* src, size_t srcSize); /**< doesn't consume input */ 2512a0483764SConrad Meyer /*! ZSTD_getFrameHeader_advanced() : 2513a0483764SConrad Meyer * same as ZSTD_getFrameHeader(), 2514a0483764SConrad Meyer * with added capability to select a format (like ZSTD_f_zstd1_magicless) */ 2515*5ff13fbcSAllan Jude ZSTDLIB_STATIC_API size_t ZSTD_getFrameHeader_advanced(ZSTD_frameHeader* zfhPtr, const void* src, size_t srcSize, ZSTD_format_e format); 2516*5ff13fbcSAllan Jude ZSTDLIB_STATIC_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 */ 25170c16b537SWarner Losh 2518*5ff13fbcSAllan Jude ZSTDLIB_STATIC_API size_t ZSTD_decompressBegin(ZSTD_DCtx* dctx); 2519*5ff13fbcSAllan Jude ZSTDLIB_STATIC_API size_t ZSTD_decompressBegin_usingDict(ZSTD_DCtx* dctx, const void* dict, size_t dictSize); 2520*5ff13fbcSAllan Jude ZSTDLIB_STATIC_API size_t ZSTD_decompressBegin_usingDDict(ZSTD_DCtx* dctx, const ZSTD_DDict* ddict); 25210c16b537SWarner Losh 2522*5ff13fbcSAllan Jude ZSTDLIB_STATIC_API size_t ZSTD_nextSrcSizeToDecompress(ZSTD_DCtx* dctx); 2523*5ff13fbcSAllan Jude ZSTDLIB_STATIC_API size_t ZSTD_decompressContinue(ZSTD_DCtx* dctx, void* dst, size_t dstCapacity, const void* src, size_t srcSize); 25240c16b537SWarner Losh 25250c16b537SWarner Losh /* misc */ 2526*5ff13fbcSAllan Jude ZSTDLIB_STATIC_API void ZSTD_copyDCtx(ZSTD_DCtx* dctx, const ZSTD_DCtx* preparedDCtx); 25270c16b537SWarner Losh typedef enum { ZSTDnit_frameHeader, ZSTDnit_blockHeader, ZSTDnit_block, ZSTDnit_lastBlock, ZSTDnit_checksum, ZSTDnit_skippableFrame } ZSTD_nextInputType_e; 2528*5ff13fbcSAllan Jude ZSTDLIB_STATIC_API ZSTD_nextInputType_e ZSTD_nextInputType(ZSTD_DCtx* dctx); 25290c16b537SWarner Losh 25300c16b537SWarner Losh 25310c16b537SWarner Losh 25320c16b537SWarner Losh 25330c16b537SWarner Losh /* ============================ */ 25340c16b537SWarner Losh /** Block level API */ 25350c16b537SWarner Losh /* ============================ */ 25360c16b537SWarner Losh 25370c16b537SWarner Losh /*! 25380c16b537SWarner Losh Block functions produce and decode raw zstd blocks, without frame metadata. 25399cbefe25SConrad Meyer Frame metadata cost is typically ~12 bytes, which can be non-negligible for very small blocks (< 100 bytes). 25409cbefe25SConrad Meyer But users will have to take in charge needed metadata to regenerate data, such as compressed and content sizes. 25410c16b537SWarner Losh 25420c16b537SWarner Losh A few rules to respect : 25430c16b537SWarner Losh - Compressing and decompressing require a context structure 25440c16b537SWarner Losh + Use ZSTD_createCCtx() and ZSTD_createDCtx() 25450c16b537SWarner Losh - It is necessary to init context before starting 25460c16b537SWarner Losh + compression : any ZSTD_compressBegin*() variant, including with dictionary 25470c16b537SWarner Losh + decompression : any ZSTD_decompressBegin*() variant, including with dictionary 25480c16b537SWarner Losh + copyCCtx() and copyDCtx() can be used too 25490c16b537SWarner Losh - Block size is limited, it must be <= ZSTD_getBlockSize() <= ZSTD_BLOCKSIZE_MAX == 128 KB 25500c16b537SWarner Losh + If input is larger than a block size, it's necessary to split input data into multiple blocks 25519cbefe25SConrad Meyer + For inputs larger than a single block, consider using regular ZSTD_compress() instead. 25529cbefe25SConrad Meyer Frame metadata is not that costly, and quickly becomes negligible as source size grows larger than a block. 25539cbefe25SConrad Meyer - When a block is considered not compressible enough, ZSTD_compressBlock() result will be 0 (zero) ! 25549cbefe25SConrad Meyer ===> In which case, nothing is produced into `dst` ! 25559cbefe25SConrad Meyer + User __must__ test for such outcome and deal directly with uncompressed data 25569cbefe25SConrad Meyer + A block cannot be declared incompressible if ZSTD_compressBlock() return value was != 0. 25579cbefe25SConrad Meyer Doing so would mess up with statistics history, leading to potential data corruption. 25589cbefe25SConrad Meyer + ZSTD_decompressBlock() _doesn't accept uncompressed data as input_ !! 25590c16b537SWarner Losh + In case of multiple successive blocks, should some of them be uncompressed, 25600c16b537SWarner Losh decoder must be informed of their existence in order to follow proper history. 25610c16b537SWarner Losh Use ZSTD_insertBlock() for such a case. 25620c16b537SWarner Losh */ 25630c16b537SWarner Losh 25640c16b537SWarner Losh /*===== Raw zstd block functions =====*/ 2565*5ff13fbcSAllan Jude ZSTDLIB_STATIC_API size_t ZSTD_getBlockSize (const ZSTD_CCtx* cctx); 2566*5ff13fbcSAllan Jude ZSTDLIB_STATIC_API size_t ZSTD_compressBlock (ZSTD_CCtx* cctx, void* dst, size_t dstCapacity, const void* src, size_t srcSize); 2567*5ff13fbcSAllan Jude ZSTDLIB_STATIC_API size_t ZSTD_decompressBlock(ZSTD_DCtx* dctx, void* dst, size_t dstCapacity, const void* src, size_t srcSize); 2568*5ff13fbcSAllan Jude ZSTDLIB_STATIC_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. */ 25690c16b537SWarner Losh 25700c16b537SWarner Losh 25710c16b537SWarner Losh #endif /* ZSTD_H_ZSTD_STATIC_LINKING_ONLY */ 25720c16b537SWarner Losh 25730c16b537SWarner Losh #if defined (__cplusplus) 25740c16b537SWarner Losh } 25750c16b537SWarner Losh #endif 2576