10c16b537SWarner Losh<html> 20c16b537SWarner Losh<head> 30c16b537SWarner Losh<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> 4*052d3c12SConrad Meyer<title>zstd 1.3.3 Manual</title> 50c16b537SWarner Losh</head> 60c16b537SWarner Losh<body> 7*052d3c12SConrad Meyer<h1>zstd 1.3.3 Manual</h1> 80c16b537SWarner Losh<hr> 90c16b537SWarner Losh<a name="Contents"></a><h2>Contents</h2> 100c16b537SWarner Losh<ol> 110c16b537SWarner Losh<li><a href="#Chapter1">Introduction</a></li> 120c16b537SWarner Losh<li><a href="#Chapter2">Version</a></li> 130c16b537SWarner Losh<li><a href="#Chapter3">Simple API</a></li> 140c16b537SWarner Losh<li><a href="#Chapter4">Explicit memory management</a></li> 150c16b537SWarner Losh<li><a href="#Chapter5">Simple dictionary API</a></li> 160c16b537SWarner Losh<li><a href="#Chapter6">Bulk processing dictionary API</a></li> 170c16b537SWarner Losh<li><a href="#Chapter7">Streaming</a></li> 180c16b537SWarner Losh<li><a href="#Chapter8">Streaming compression - HowTo</a></li> 190c16b537SWarner Losh<li><a href="#Chapter9">Streaming decompression - HowTo</a></li> 200c16b537SWarner Losh<li><a href="#Chapter10">START OF ADVANCED AND EXPERIMENTAL FUNCTIONS</a></li> 210c16b537SWarner Losh<li><a href="#Chapter11">Advanced types</a></li> 22*052d3c12SConrad Meyer<li><a href="#Chapter12">Custom memory allocation functions</a></li> 23*052d3c12SConrad Meyer<li><a href="#Chapter13">Frame size functions</a></li> 24*052d3c12SConrad Meyer<li><a href="#Chapter14">Context memory usage</a></li> 25*052d3c12SConrad Meyer<li><a href="#Chapter15">Advanced compression functions</a></li> 26*052d3c12SConrad Meyer<li><a href="#Chapter16">Advanced decompression functions</a></li> 27*052d3c12SConrad Meyer<li><a href="#Chapter17">Advanced streaming functions</a></li> 28*052d3c12SConrad Meyer<li><a href="#Chapter18">Buffer-less and synchronous inner streaming functions</a></li> 29*052d3c12SConrad Meyer<li><a href="#Chapter19">Buffer-less streaming compression (synchronous mode)</a></li> 30*052d3c12SConrad Meyer<li><a href="#Chapter20">Buffer-less streaming decompression (synchronous mode)</a></li> 31*052d3c12SConrad Meyer<li><a href="#Chapter21">New advanced API (experimental)</a></li> 32*052d3c12SConrad Meyer<li><a href="#Chapter22">Block level API</a></li> 330c16b537SWarner Losh</ol> 340c16b537SWarner Losh<hr> 350c16b537SWarner Losh<a name="Chapter1"></a><h2>Introduction</h2><pre> 360c16b537SWarner Losh zstd, short for Zstandard, is a fast lossless compression algorithm, 370c16b537SWarner Losh targeting real-time compression scenarios at zlib-level and better compression ratios. 380c16b537SWarner Losh The zstd compression library provides in-memory compression and decompression functions. 390c16b537SWarner Losh The library supports compression levels from 1 up to ZSTD_maxCLevel() which is currently 22. 400c16b537SWarner Losh Levels >= 20, labeled `--ultra`, should be used with caution, as they require more memory. 410c16b537SWarner Losh Compression can be done in: 420c16b537SWarner Losh - a single step (described as Simple API) 430c16b537SWarner Losh - a single step, reusing a context (described as Explicit memory management) 440c16b537SWarner Losh - unbounded multiple steps (described as Streaming compression) 450c16b537SWarner Losh The compression ratio achievable on small data can be highly improved using a dictionary in: 460c16b537SWarner Losh - a single step (described as Simple dictionary API) 470c16b537SWarner Losh - a single step, reusing a dictionary (described as Fast dictionary API) 480c16b537SWarner Losh 490c16b537SWarner Losh Advanced experimental functions can be accessed using #define ZSTD_STATIC_LINKING_ONLY before including zstd.h. 500c16b537SWarner Losh Advanced experimental APIs shall never be used with a dynamic library. 510c16b537SWarner Losh They are not "stable", their definition may change in the future. Only static linking is allowed. 520c16b537SWarner Losh<BR></pre> 530c16b537SWarner Losh 540c16b537SWarner Losh<a name="Chapter2"></a><h2>Version</h2><pre></pre> 550c16b537SWarner Losh 560c16b537SWarner Losh<pre><b>unsigned ZSTD_versionNumber(void); </b>/**< useful to check dll version */<b> 570c16b537SWarner Losh</b></pre><BR> 580c16b537SWarner Losh<a name="Chapter3"></a><h2>Simple API</h2><pre></pre> 590c16b537SWarner Losh 600c16b537SWarner Losh<pre><b>size_t ZSTD_compress( void* dst, size_t dstCapacity, 610c16b537SWarner Losh const void* src, size_t srcSize, 620c16b537SWarner Losh int compressionLevel); 630c16b537SWarner Losh</b><p> Compresses `src` content as a single zstd compressed frame into already allocated `dst`. 640c16b537SWarner Losh Hint : compression runs faster if `dstCapacity` >= `ZSTD_compressBound(srcSize)`. 650c16b537SWarner Losh @return : compressed size written into `dst` (<= `dstCapacity), 660c16b537SWarner Losh or an error code if it fails (which can be tested using ZSTD_isError()). 670c16b537SWarner Losh</p></pre><BR> 680c16b537SWarner Losh 690c16b537SWarner Losh<pre><b>size_t ZSTD_decompress( void* dst, size_t dstCapacity, 700c16b537SWarner Losh const void* src, size_t compressedSize); 710c16b537SWarner Losh</b><p> `compressedSize` : must be the _exact_ size of some number of compressed and/or skippable frames. 720c16b537SWarner Losh `dstCapacity` is an upper bound of originalSize to regenerate. 730c16b537SWarner Losh If user cannot imply a maximum upper bound, it's better to use streaming mode to decompress data. 740c16b537SWarner Losh @return : the number of bytes decompressed into `dst` (<= `dstCapacity`), 750c16b537SWarner Losh or an errorCode if it fails (which can be tested using ZSTD_isError()). 760c16b537SWarner Losh</p></pre><BR> 770c16b537SWarner Losh 780c16b537SWarner Losh<pre><b>#define ZSTD_CONTENTSIZE_UNKNOWN (0ULL - 1) 790c16b537SWarner Losh#define ZSTD_CONTENTSIZE_ERROR (0ULL - 2) 800c16b537SWarner Loshunsigned long long ZSTD_getFrameContentSize(const void *src, size_t srcSize); 810c16b537SWarner Losh</b><p> `src` should point to the start of a ZSTD encoded frame. 820c16b537SWarner Losh `srcSize` must be at least as large as the frame header. 830c16b537SWarner Losh hint : any size >= `ZSTD_frameHeaderSize_max` is large enough. 840c16b537SWarner Losh @return : - decompressed size of the frame in `src`, if known 850c16b537SWarner Losh - ZSTD_CONTENTSIZE_UNKNOWN if the size cannot be determined 860c16b537SWarner Losh - ZSTD_CONTENTSIZE_ERROR if an error occurred (e.g. invalid magic number, srcSize too small) 870c16b537SWarner Losh note 1 : a 0 return value means the frame is valid but "empty". 880c16b537SWarner Losh note 2 : decompressed size is an optional field, it may not be present, typically in streaming mode. 890c16b537SWarner Losh When `return==ZSTD_CONTENTSIZE_UNKNOWN`, data to decompress could be any size. 900c16b537SWarner Losh In which case, it's necessary to use streaming mode to decompress data. 910c16b537SWarner Losh Optionally, application can rely on some implicit limit, 920c16b537SWarner Losh as ZSTD_decompress() only needs an upper bound of decompressed size. 930c16b537SWarner Losh (For example, data could be necessarily cut into blocks <= 16 KB). 940c16b537SWarner Losh note 3 : decompressed size is always present when compression is done with ZSTD_compress() 950c16b537SWarner Losh note 4 : decompressed size can be very large (64-bits value), 960c16b537SWarner Losh potentially larger than what local system can handle as a single memory segment. 970c16b537SWarner Losh In which case, it's necessary to use streaming mode to decompress data. 980c16b537SWarner Losh note 5 : If source is untrusted, decompressed size could be wrong or intentionally modified. 990c16b537SWarner Losh Always ensure return value fits within application's authorized limits. 1000c16b537SWarner Losh Each application can set its own limits. 1010c16b537SWarner Losh note 6 : This function replaces ZSTD_getDecompressedSize() 1020c16b537SWarner Losh</p></pre><BR> 1030c16b537SWarner Losh 1040c16b537SWarner Losh<pre><b>unsigned long long ZSTD_getDecompressedSize(const void* src, size_t srcSize); 1050c16b537SWarner Losh</b><p> NOTE: This function is now obsolete, in favor of ZSTD_getFrameContentSize(). 1060c16b537SWarner Losh Both functions work the same way, 1070c16b537SWarner Losh but ZSTD_getDecompressedSize() blends 1080c16b537SWarner Losh "empty", "unknown" and "error" results in the same return value (0), 1090c16b537SWarner Losh while ZSTD_getFrameContentSize() distinguishes them. 1100c16b537SWarner Losh 1110c16b537SWarner Losh 'src' is the start of a zstd compressed frame. 1120c16b537SWarner Losh @return : content size to be decompressed, as a 64-bits value _if known and not empty_, 0 otherwise. 1130c16b537SWarner Losh</p></pre><BR> 1140c16b537SWarner Losh 115*052d3c12SConrad Meyer<h3>Helper functions</h3><pre></pre><b><pre>#define ZSTD_COMPRESSBOUND(srcSize) ((srcSize) + ((srcSize)>>8) + (((srcSize) < (128<<10)) ? (((128<<10) - (srcSize)) >> 11) </b>/* 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 */<b> 1160c16b537SWarner Loshsize_t ZSTD_compressBound(size_t srcSize); </b>/*!< maximum compressed size in worst case scenario */<b> 1170c16b537SWarner Loshunsigned ZSTD_isError(size_t code); </b>/*!< tells if a `size_t` function result is an error code */<b> 1180c16b537SWarner Loshconst char* ZSTD_getErrorName(size_t code); </b>/*!< provides readable string from an error code */<b> 1190c16b537SWarner Loshint ZSTD_maxCLevel(void); </b>/*!< maximum compression level available */<b> 1200c16b537SWarner Losh</pre></b><BR> 1210c16b537SWarner Losh<a name="Chapter4"></a><h2>Explicit memory management</h2><pre></pre> 1220c16b537SWarner Losh 1230c16b537SWarner Losh<h3>Compression context</h3><pre> When compressing many times, 1240c16b537SWarner Losh it is recommended to allocate a context just once, and re-use it for each successive compression operation. 1250c16b537SWarner Losh This will make workload friendlier for system's memory. 1260c16b537SWarner Losh Use one context per thread for parallel execution in multi-threaded environments. 1270c16b537SWarner Losh</pre><b><pre>typedef struct ZSTD_CCtx_s ZSTD_CCtx; 1280c16b537SWarner LoshZSTD_CCtx* ZSTD_createCCtx(void); 1290c16b537SWarner Loshsize_t ZSTD_freeCCtx(ZSTD_CCtx* cctx); 1300c16b537SWarner Losh</pre></b><BR> 1310c16b537SWarner Losh<pre><b>size_t ZSTD_compressCCtx(ZSTD_CCtx* ctx, 1320c16b537SWarner Losh void* dst, size_t dstCapacity, 1330c16b537SWarner Losh const void* src, size_t srcSize, 1340c16b537SWarner Losh int compressionLevel); 1350c16b537SWarner Losh</b><p> Same as ZSTD_compress(), requires an allocated ZSTD_CCtx (see ZSTD_createCCtx()). 1360c16b537SWarner Losh</p></pre><BR> 1370c16b537SWarner Losh 1380c16b537SWarner Losh<h3>Decompression context</h3><pre> When decompressing many times, 1390c16b537SWarner Losh it is recommended to allocate a context only once, 1400c16b537SWarner Losh and re-use it for each successive compression operation. 1410c16b537SWarner Losh This will make workload friendlier for system's memory. 1420c16b537SWarner Losh Use one context per thread for parallel execution. 1430c16b537SWarner Losh</pre><b><pre>typedef struct ZSTD_DCtx_s ZSTD_DCtx; 1440c16b537SWarner LoshZSTD_DCtx* ZSTD_createDCtx(void); 1450c16b537SWarner Loshsize_t ZSTD_freeDCtx(ZSTD_DCtx* dctx); 1460c16b537SWarner Losh</pre></b><BR> 1470c16b537SWarner Losh<pre><b>size_t ZSTD_decompressDCtx(ZSTD_DCtx* ctx, 1480c16b537SWarner Losh void* dst, size_t dstCapacity, 1490c16b537SWarner Losh const void* src, size_t srcSize); 1500c16b537SWarner Losh</b><p> Same as ZSTD_decompress(), requires an allocated ZSTD_DCtx (see ZSTD_createDCtx()) 1510c16b537SWarner Losh</p></pre><BR> 1520c16b537SWarner Losh 1530c16b537SWarner Losh<a name="Chapter5"></a><h2>Simple dictionary API</h2><pre></pre> 1540c16b537SWarner Losh 1550c16b537SWarner Losh<pre><b>size_t ZSTD_compress_usingDict(ZSTD_CCtx* ctx, 1560c16b537SWarner Losh void* dst, size_t dstCapacity, 1570c16b537SWarner Losh const void* src, size_t srcSize, 1580c16b537SWarner Losh const void* dict,size_t dictSize, 1590c16b537SWarner Losh int compressionLevel); 1600c16b537SWarner Losh</b><p> Compression using a predefined Dictionary (see dictBuilder/zdict.h). 1610c16b537SWarner Losh Note : This function loads the dictionary, resulting in significant startup delay. 1620c16b537SWarner Losh Note : When `dict == NULL || dictSize < 8` no dictionary is used. 1630c16b537SWarner Losh</p></pre><BR> 1640c16b537SWarner Losh 1650c16b537SWarner Losh<pre><b>size_t ZSTD_decompress_usingDict(ZSTD_DCtx* dctx, 1660c16b537SWarner Losh void* dst, size_t dstCapacity, 1670c16b537SWarner Losh const void* src, size_t srcSize, 1680c16b537SWarner Losh const void* dict,size_t dictSize); 1690c16b537SWarner Losh</b><p> Decompression using a predefined Dictionary (see dictBuilder/zdict.h). 1700c16b537SWarner Losh Dictionary must be identical to the one used during compression. 1710c16b537SWarner Losh Note : This function loads the dictionary, resulting in significant startup delay. 1720c16b537SWarner Losh Note : When `dict == NULL || dictSize < 8` no dictionary is used. 1730c16b537SWarner Losh</p></pre><BR> 1740c16b537SWarner Losh 1750c16b537SWarner Losh<a name="Chapter6"></a><h2>Bulk processing dictionary API</h2><pre></pre> 1760c16b537SWarner Losh 1770c16b537SWarner Losh<pre><b>ZSTD_CDict* ZSTD_createCDict(const void* dictBuffer, size_t dictSize, 1780c16b537SWarner Losh int compressionLevel); 1790c16b537SWarner Losh</b><p> When compressing multiple messages / blocks with the same dictionary, it's recommended to load it just once. 1800c16b537SWarner Losh ZSTD_createCDict() will create a digested dictionary, ready to start future compression operations without startup delay. 1810c16b537SWarner Losh ZSTD_CDict can be created once and shared by multiple threads concurrently, since its usage is read-only. 1820c16b537SWarner Losh `dictBuffer` can be released after ZSTD_CDict creation, since its content is copied within CDict 1830c16b537SWarner Losh</p></pre><BR> 1840c16b537SWarner Losh 1850c16b537SWarner Losh<pre><b>size_t ZSTD_freeCDict(ZSTD_CDict* CDict); 1860c16b537SWarner Losh</b><p> Function frees memory allocated by ZSTD_createCDict(). 1870c16b537SWarner Losh</p></pre><BR> 1880c16b537SWarner Losh 1890c16b537SWarner Losh<pre><b>size_t ZSTD_compress_usingCDict(ZSTD_CCtx* cctx, 1900c16b537SWarner Losh void* dst, size_t dstCapacity, 1910c16b537SWarner Losh const void* src, size_t srcSize, 1920c16b537SWarner Losh const ZSTD_CDict* cdict); 1930c16b537SWarner Losh</b><p> Compression using a digested Dictionary. 1940c16b537SWarner Losh Faster startup than ZSTD_compress_usingDict(), recommended when same dictionary is used multiple times. 1950c16b537SWarner Losh Note that compression level is decided during dictionary creation. 1960c16b537SWarner Losh Frame parameters are hardcoded (dictID=yes, contentSize=yes, checksum=no) 1970c16b537SWarner Losh</p></pre><BR> 1980c16b537SWarner Losh 1990c16b537SWarner Losh<pre><b>ZSTD_DDict* ZSTD_createDDict(const void* dictBuffer, size_t dictSize); 2000c16b537SWarner Losh</b><p> Create a digested dictionary, ready to start decompression operation without startup delay. 2010c16b537SWarner Losh dictBuffer can be released after DDict creation, as its content is copied inside DDict 2020c16b537SWarner Losh</p></pre><BR> 2030c16b537SWarner Losh 2040c16b537SWarner Losh<pre><b>size_t ZSTD_freeDDict(ZSTD_DDict* ddict); 2050c16b537SWarner Losh</b><p> Function frees memory allocated with ZSTD_createDDict() 2060c16b537SWarner Losh</p></pre><BR> 2070c16b537SWarner Losh 2080c16b537SWarner Losh<pre><b>size_t ZSTD_decompress_usingDDict(ZSTD_DCtx* dctx, 2090c16b537SWarner Losh void* dst, size_t dstCapacity, 2100c16b537SWarner Losh const void* src, size_t srcSize, 2110c16b537SWarner Losh const ZSTD_DDict* ddict); 2120c16b537SWarner Losh</b><p> Decompression using a digested Dictionary. 2130c16b537SWarner Losh Faster startup than ZSTD_decompress_usingDict(), recommended when same dictionary is used multiple times. 2140c16b537SWarner Losh</p></pre><BR> 2150c16b537SWarner Losh 2160c16b537SWarner Losh<a name="Chapter7"></a><h2>Streaming</h2><pre></pre> 2170c16b537SWarner Losh 2180c16b537SWarner Losh<pre><b>typedef struct ZSTD_inBuffer_s { 2190c16b537SWarner Losh const void* src; </b>/**< start of input buffer */<b> 2200c16b537SWarner Losh size_t size; </b>/**< size of input buffer */<b> 2210c16b537SWarner Losh size_t pos; </b>/**< position where reading stopped. Will be updated. Necessarily 0 <= pos <= size */<b> 2220c16b537SWarner Losh} ZSTD_inBuffer; 2230c16b537SWarner Losh</b></pre><BR> 2240c16b537SWarner Losh<pre><b>typedef struct ZSTD_outBuffer_s { 2250c16b537SWarner Losh void* dst; </b>/**< start of output buffer */<b> 2260c16b537SWarner Losh size_t size; </b>/**< size of output buffer */<b> 2270c16b537SWarner Losh size_t pos; </b>/**< position where writing stopped. Will be updated. Necessarily 0 <= pos <= size */<b> 2280c16b537SWarner Losh} ZSTD_outBuffer; 2290c16b537SWarner Losh</b></pre><BR> 2300c16b537SWarner Losh<a name="Chapter8"></a><h2>Streaming compression - HowTo</h2><pre> 2310c16b537SWarner Losh A ZSTD_CStream object is required to track streaming operation. 2320c16b537SWarner Losh Use ZSTD_createCStream() and ZSTD_freeCStream() to create/release resources. 2330c16b537SWarner Losh ZSTD_CStream objects can be reused multiple times on consecutive compression operations. 2340c16b537SWarner Losh It is recommended to re-use ZSTD_CStream in situations where many streaming operations will be achieved consecutively, 2350c16b537SWarner Losh since it will play nicer with system's memory, by re-using already allocated memory. 2360c16b537SWarner Losh Use one separate ZSTD_CStream per thread for parallel execution. 2370c16b537SWarner Losh 2380c16b537SWarner Losh Start a new compression by initializing ZSTD_CStream. 2390c16b537SWarner Losh Use ZSTD_initCStream() to start a new compression operation. 2400c16b537SWarner Losh Use ZSTD_initCStream_usingDict() or ZSTD_initCStream_usingCDict() for a compression which requires a dictionary (experimental section) 2410c16b537SWarner Losh 2420c16b537SWarner Losh Use ZSTD_compressStream() repetitively to consume input stream. 2430c16b537SWarner Losh The function will automatically update both `pos` fields. 2440c16b537SWarner Losh Note that it may not consume the entire input, in which case `pos < size`, 2450c16b537SWarner Losh and it's up to the caller to present again remaining data. 2460c16b537SWarner Losh @return : a size hint, preferred nb of bytes to use as input for next function call 2470c16b537SWarner Losh or an error code, which can be tested using ZSTD_isError(). 2480c16b537SWarner Losh Note 1 : it's just a hint, to help latency a little, any other value will work fine. 2490c16b537SWarner Losh Note 2 : size hint is guaranteed to be <= ZSTD_CStreamInSize() 2500c16b537SWarner Losh 2510c16b537SWarner Losh At any moment, it's possible to flush whatever data remains within internal buffer, using ZSTD_flushStream(). 2520c16b537SWarner Losh `output->pos` will be updated. 2530c16b537SWarner Losh Note that some content might still be left within internal buffer if `output->size` is too small. 2540c16b537SWarner Losh @return : nb of bytes still present within internal buffer (0 if it's empty) 2550c16b537SWarner Losh or an error code, which can be tested using ZSTD_isError(). 2560c16b537SWarner Losh 2570c16b537SWarner Losh ZSTD_endStream() instructs to finish a frame. 2580c16b537SWarner Losh It will perform a flush and write frame epilogue. 2590c16b537SWarner Losh The epilogue is required for decoders to consider a frame completed. 2600c16b537SWarner Losh ZSTD_endStream() may not be able to flush full data if `output->size` is too small. 2610c16b537SWarner Losh In which case, call again ZSTD_endStream() to complete the flush. 2620c16b537SWarner Losh @return : 0 if frame fully completed and fully flushed, 2630c16b537SWarner Losh or >0 if some data is still present within internal buffer 2640c16b537SWarner Losh (value is minimum size estimation for remaining data to flush, but it could be more) 2650c16b537SWarner Losh or an error code, which can be tested using ZSTD_isError(). 2660c16b537SWarner Losh 2670c16b537SWarner Losh 2680c16b537SWarner Losh<BR></pre> 2690c16b537SWarner Losh 2700c16b537SWarner Losh<pre><b>typedef ZSTD_CCtx ZSTD_CStream; </b>/**< CCtx and CStream are now effectively same object (>= v1.3.0) */<b> 2710c16b537SWarner Losh</b></pre><BR> 2720c16b537SWarner Losh<h3>ZSTD_CStream management functions</h3><pre></pre><b><pre>ZSTD_CStream* ZSTD_createCStream(void); 2730c16b537SWarner Loshsize_t ZSTD_freeCStream(ZSTD_CStream* zcs); 2740c16b537SWarner Losh</pre></b><BR> 2750c16b537SWarner Losh<h3>Streaming compression functions</h3><pre></pre><b><pre>size_t ZSTD_initCStream(ZSTD_CStream* zcs, int compressionLevel); 2760c16b537SWarner Loshsize_t ZSTD_compressStream(ZSTD_CStream* zcs, ZSTD_outBuffer* output, ZSTD_inBuffer* input); 2770c16b537SWarner Loshsize_t ZSTD_flushStream(ZSTD_CStream* zcs, ZSTD_outBuffer* output); 2780c16b537SWarner Loshsize_t ZSTD_endStream(ZSTD_CStream* zcs, ZSTD_outBuffer* output); 2790c16b537SWarner Losh</pre></b><BR> 2800c16b537SWarner Losh<pre><b>size_t ZSTD_CStreamInSize(void); </b>/**< recommended size for input buffer */<b> 2810c16b537SWarner Losh</b></pre><BR> 2820c16b537SWarner Losh<pre><b>size_t ZSTD_CStreamOutSize(void); </b>/**< recommended size for output buffer. Guarantee to successfully flush at least one complete compressed block in all circumstances. */<b> 2830c16b537SWarner Losh</b></pre><BR> 2840c16b537SWarner Losh<a name="Chapter9"></a><h2>Streaming decompression - HowTo</h2><pre> 2850c16b537SWarner Losh A ZSTD_DStream object is required to track streaming operations. 2860c16b537SWarner Losh Use ZSTD_createDStream() and ZSTD_freeDStream() to create/release resources. 2870c16b537SWarner Losh ZSTD_DStream objects can be re-used multiple times. 2880c16b537SWarner Losh 2890c16b537SWarner Losh Use ZSTD_initDStream() to start a new decompression operation, 2900c16b537SWarner Losh or ZSTD_initDStream_usingDict() if decompression requires a dictionary. 2910c16b537SWarner Losh @return : recommended first input size 2920c16b537SWarner Losh 2930c16b537SWarner Losh Use ZSTD_decompressStream() repetitively to consume your input. 2940c16b537SWarner Losh The function will update both `pos` fields. 2950c16b537SWarner Losh If `input.pos < input.size`, some input has not been consumed. 2960c16b537SWarner Losh It's up to the caller to present again remaining data. 2970c16b537SWarner Losh If `output.pos < output.size`, decoder has flushed everything it could. 2980c16b537SWarner Losh @return : 0 when a frame is completely decoded and fully flushed, 2990c16b537SWarner Losh an error code, which can be tested using ZSTD_isError(), 3000c16b537SWarner Losh any other value > 0, which means there is still some decoding to do to complete current frame. 3010c16b537SWarner Losh The return value is a suggested next input size (a hint to improve latency) that will never load more than the current frame. 3020c16b537SWarner Losh 3030c16b537SWarner Losh<BR></pre> 3040c16b537SWarner Losh 3050c16b537SWarner Losh<pre><b>typedef ZSTD_DCtx ZSTD_DStream; </b>/**< DCtx and DStream are now effectively same object (>= v1.3.0) */<b> 3060c16b537SWarner Losh</b></pre><BR> 3070c16b537SWarner Losh<h3>ZSTD_DStream management functions</h3><pre></pre><b><pre>ZSTD_DStream* ZSTD_createDStream(void); 3080c16b537SWarner Loshsize_t ZSTD_freeDStream(ZSTD_DStream* zds); 3090c16b537SWarner Losh</pre></b><BR> 3100c16b537SWarner Losh<h3>Streaming decompression functions</h3><pre></pre><b><pre>size_t ZSTD_initDStream(ZSTD_DStream* zds); 3110c16b537SWarner Loshsize_t ZSTD_decompressStream(ZSTD_DStream* zds, ZSTD_outBuffer* output, ZSTD_inBuffer* input); 3120c16b537SWarner Losh</pre></b><BR> 3130c16b537SWarner Losh<pre><b>size_t ZSTD_DStreamInSize(void); </b>/*!< recommended size for input buffer */<b> 3140c16b537SWarner Losh</b></pre><BR> 3150c16b537SWarner Losh<pre><b>size_t ZSTD_DStreamOutSize(void); </b>/*!< recommended size for output buffer. Guarantee to successfully flush at least one complete block in all circumstances. */<b> 3160c16b537SWarner Losh</b></pre><BR> 3170c16b537SWarner Losh<a name="Chapter10"></a><h2>START OF ADVANCED AND EXPERIMENTAL FUNCTIONS</h2><pre> The definitions in this section are considered experimental. 3180c16b537SWarner Losh They should never be used with a dynamic library, as prototypes may change in the future. 3190c16b537SWarner Losh They are provided for advanced scenarios. 3200c16b537SWarner Losh Use them only in association with static linking. 3210c16b537SWarner Losh 3220c16b537SWarner Losh<BR></pre> 3230c16b537SWarner Losh 3240c16b537SWarner Losh<a name="Chapter11"></a><h2>Advanced types</h2><pre></pre> 3250c16b537SWarner Losh 3260c16b537SWarner Losh<pre><b>typedef enum { ZSTD_fast=1, ZSTD_dfast, ZSTD_greedy, ZSTD_lazy, ZSTD_lazy2, 3270c16b537SWarner Losh ZSTD_btlazy2, ZSTD_btopt, ZSTD_btultra } ZSTD_strategy; </b>/* from faster to stronger */<b> 3280c16b537SWarner Losh</b></pre><BR> 3290c16b537SWarner Losh<pre><b>typedef struct { 3300c16b537SWarner Losh unsigned windowLog; </b>/**< largest match distance : larger == more compression, more memory needed during decompression */<b> 3310c16b537SWarner Losh unsigned chainLog; </b>/**< fully searched segment : larger == more compression, slower, more memory (useless for fast) */<b> 3320c16b537SWarner Losh unsigned hashLog; </b>/**< dispatch table : larger == faster, more memory */<b> 3330c16b537SWarner Losh unsigned searchLog; </b>/**< nb of searches : larger == more compression, slower */<b> 3340c16b537SWarner Losh unsigned searchLength; </b>/**< match length searched : larger == faster decompression, sometimes less compression */<b> 3350c16b537SWarner Losh unsigned targetLength; </b>/**< acceptable match size for optimal parser (only) : larger == more compression, slower */<b> 3360c16b537SWarner Losh ZSTD_strategy strategy; 3370c16b537SWarner Losh} ZSTD_compressionParameters; 3380c16b537SWarner Losh</b></pre><BR> 3390c16b537SWarner Losh<pre><b>typedef struct { 3400c16b537SWarner Losh unsigned contentSizeFlag; </b>/**< 1: content size will be in frame header (when known) */<b> 3410c16b537SWarner Losh unsigned checksumFlag; </b>/**< 1: generate a 32-bits checksum at end of frame, for error detection */<b> 3420c16b537SWarner Losh unsigned noDictIDFlag; </b>/**< 1: no dictID will be saved into frame header (if dictionary compression) */<b> 3430c16b537SWarner Losh} ZSTD_frameParameters; 3440c16b537SWarner Losh</b></pre><BR> 3450c16b537SWarner Losh<pre><b>typedef struct { 3460c16b537SWarner Losh ZSTD_compressionParameters cParams; 3470c16b537SWarner Losh ZSTD_frameParameters fParams; 3480c16b537SWarner Losh} ZSTD_parameters; 3490c16b537SWarner Losh</b></pre><BR> 350*052d3c12SConrad Meyer<a name="Chapter12"></a><h2>Custom memory allocation functions</h2><pre></pre> 351*052d3c12SConrad Meyer 352*052d3c12SConrad Meyer<pre><b>typedef struct { ZSTD_allocFunction customAlloc; ZSTD_freeFunction customFree; void* opaque; } ZSTD_customMem; 353*052d3c12SConrad Meyer</b></pre><BR> 354*052d3c12SConrad Meyer<a name="Chapter13"></a><h2>Frame size functions</h2><pre></pre> 3550c16b537SWarner Losh 3560c16b537SWarner Losh<pre><b>size_t ZSTD_findFrameCompressedSize(const void* src, size_t srcSize); 3570c16b537SWarner Losh</b><p> `src` should point to the start of a ZSTD encoded frame or skippable frame 358*052d3c12SConrad Meyer `srcSize` must be >= first frame size 3590c16b537SWarner Losh @return : the compressed size of the first frame starting at `src`, 3600c16b537SWarner Losh suitable to pass to `ZSTD_decompress` or similar, 3610c16b537SWarner Losh or an error code if input is invalid 3620c16b537SWarner Losh</p></pre><BR> 3630c16b537SWarner Losh 3640c16b537SWarner Losh<pre><b>unsigned long long ZSTD_findDecompressedSize(const void* src, size_t srcSize); 3650c16b537SWarner Losh</b><p> `src` should point the start of a series of ZSTD encoded and/or skippable frames 3660c16b537SWarner Losh `srcSize` must be the _exact_ size of this series 3670c16b537SWarner Losh (i.e. there should be a frame boundary exactly at `srcSize` bytes after `src`) 3680c16b537SWarner Losh @return : - decompressed size of all data in all successive frames 3690c16b537SWarner Losh - if the decompressed size cannot be determined: ZSTD_CONTENTSIZE_UNKNOWN 3700c16b537SWarner Losh - if an error occurred: ZSTD_CONTENTSIZE_ERROR 3710c16b537SWarner Losh 3720c16b537SWarner Losh note 1 : decompressed size is an optional field, that may not be present, especially in streaming mode. 3730c16b537SWarner Losh When `return==ZSTD_CONTENTSIZE_UNKNOWN`, data to decompress could be any size. 3740c16b537SWarner Losh In which case, it's necessary to use streaming mode to decompress data. 3750c16b537SWarner Losh note 2 : decompressed size is always present when compression is done with ZSTD_compress() 3760c16b537SWarner Losh note 3 : decompressed size can be very large (64-bits value), 3770c16b537SWarner Losh potentially larger than what local system can handle as a single memory segment. 3780c16b537SWarner Losh In which case, it's necessary to use streaming mode to decompress data. 3790c16b537SWarner Losh note 4 : If source is untrusted, decompressed size could be wrong or intentionally modified. 3800c16b537SWarner Losh Always ensure result fits within application's authorized limits. 3810c16b537SWarner Losh Each application can set its own limits. 3820c16b537SWarner Losh note 5 : ZSTD_findDecompressedSize handles multiple frames, and so it must traverse the input to 3830c16b537SWarner Losh read each contained frame header. This is fast as most of the data is skipped, 3840c16b537SWarner Losh however it does mean that all frame data must be present and valid. 3850c16b537SWarner Losh</p></pre><BR> 3860c16b537SWarner Losh 3870c16b537SWarner Losh<pre><b>size_t ZSTD_frameHeaderSize(const void* src, size_t srcSize); 3880c16b537SWarner Losh</b><p> `src` should point to the start of a ZSTD frame 3890c16b537SWarner Losh `srcSize` must be >= ZSTD_frameHeaderSize_prefix. 3900c16b537SWarner Losh @return : size of the Frame Header 3910c16b537SWarner Losh</p></pre><BR> 3920c16b537SWarner Losh 393*052d3c12SConrad Meyer<a name="Chapter14"></a><h2>Context memory usage</h2><pre></pre> 3940c16b537SWarner Losh 3950c16b537SWarner Losh<pre><b>size_t ZSTD_sizeof_CCtx(const ZSTD_CCtx* cctx); 3960c16b537SWarner Loshsize_t ZSTD_sizeof_DCtx(const ZSTD_DCtx* dctx); 3970c16b537SWarner Loshsize_t ZSTD_sizeof_CStream(const ZSTD_CStream* zcs); 3980c16b537SWarner Loshsize_t ZSTD_sizeof_DStream(const ZSTD_DStream* zds); 3990c16b537SWarner Loshsize_t ZSTD_sizeof_CDict(const ZSTD_CDict* cdict); 4000c16b537SWarner Loshsize_t ZSTD_sizeof_DDict(const ZSTD_DDict* ddict); 4010c16b537SWarner Losh</b><p> These functions give the current memory usage of selected object. 4020c16b537SWarner Losh Object memory usage can evolve when re-used multiple times. 4030c16b537SWarner Losh</p></pre><BR> 4040c16b537SWarner Losh 4050c16b537SWarner Losh<pre><b>size_t ZSTD_estimateCCtxSize(int compressionLevel); 4060c16b537SWarner Loshsize_t ZSTD_estimateCCtxSize_usingCParams(ZSTD_compressionParameters cParams); 4070c16b537SWarner Loshsize_t ZSTD_estimateCCtxSize_usingCCtxParams(const ZSTD_CCtx_params* params); 4080c16b537SWarner Loshsize_t ZSTD_estimateDCtxSize(void); 4090c16b537SWarner Losh</b><p> These functions make it possible to estimate memory usage 4100c16b537SWarner Losh of a future {D,C}Ctx, before its creation. 4110c16b537SWarner Losh ZSTD_estimateCCtxSize() will provide a budget large enough for any compression level up to selected one. 4120c16b537SWarner Losh It will also consider src size to be arbitrarily "large", which is worst case. 4130c16b537SWarner Losh If srcSize is known to always be small, ZSTD_estimateCCtxSize_usingCParams() can provide a tighter estimation. 4140c16b537SWarner Losh ZSTD_estimateCCtxSize_usingCParams() can be used in tandem with ZSTD_getCParams() to create cParams from compressionLevel. 4150c16b537SWarner Losh ZSTD_estimateCCtxSize_usingCCtxParams() can be used in tandem with ZSTD_CCtxParam_setParameter(). Only single-threaded compression is supported. This function will return an error code if ZSTD_p_nbThreads is > 1. 4160c16b537SWarner Losh Note : CCtx estimation is only correct for single-threaded compression 4170c16b537SWarner Losh</p></pre><BR> 4180c16b537SWarner Losh 4190c16b537SWarner Losh<pre><b>size_t ZSTD_estimateCStreamSize(int compressionLevel); 4200c16b537SWarner Loshsize_t ZSTD_estimateCStreamSize_usingCParams(ZSTD_compressionParameters cParams); 4210c16b537SWarner Loshsize_t ZSTD_estimateCStreamSize_usingCCtxParams(const ZSTD_CCtx_params* params); 4220c16b537SWarner Loshsize_t ZSTD_estimateDStreamSize(size_t windowSize); 4230c16b537SWarner Loshsize_t ZSTD_estimateDStreamSize_fromFrame(const void* src, size_t srcSize); 4240c16b537SWarner Losh</b><p> ZSTD_estimateCStreamSize() will provide a budget large enough for any compression level up to selected one. 4250c16b537SWarner Losh It will also consider src size to be arbitrarily "large", which is worst case. 4260c16b537SWarner Losh If srcSize is known to always be small, ZSTD_estimateCStreamSize_usingCParams() can provide a tighter estimation. 4270c16b537SWarner Losh ZSTD_estimateCStreamSize_usingCParams() can be used in tandem with ZSTD_getCParams() to create cParams from compressionLevel. 4280c16b537SWarner Losh ZSTD_estimateCStreamSize_usingCCtxParams() can be used in tandem with ZSTD_CCtxParam_setParameter(). Only single-threaded compression is supported. This function will return an error code if ZSTD_p_nbThreads is set to a value > 1. 4290c16b537SWarner Losh Note : CStream estimation is only correct for single-threaded compression. 4300c16b537SWarner Losh ZSTD_DStream memory budget depends on window Size. 4310c16b537SWarner Losh This information can be passed manually, using ZSTD_estimateDStreamSize, 4320c16b537SWarner Losh or deducted from a valid frame Header, using ZSTD_estimateDStreamSize_fromFrame(); 4330c16b537SWarner Losh Note : if streaming is init with function ZSTD_init?Stream_usingDict(), 4340c16b537SWarner Losh an internal ?Dict will be created, which additional size is not estimated here. 4350c16b537SWarner Losh In this case, get total size by adding ZSTD_estimate?DictSize 4360c16b537SWarner Losh</p></pre><BR> 4370c16b537SWarner Losh 4380c16b537SWarner Losh<pre><b>typedef enum { 4390c16b537SWarner Losh ZSTD_dlm_byCopy = 0, </b>/**< Copy dictionary content internally */<b> 4400c16b537SWarner Losh ZSTD_dlm_byRef, </b>/**< Reference dictionary content -- the dictionary buffer must outlive its users. */<b> 4410c16b537SWarner Losh} ZSTD_dictLoadMethod_e; 4420c16b537SWarner Losh</b></pre><BR> 4430c16b537SWarner Losh<pre><b>size_t ZSTD_estimateCDictSize(size_t dictSize, int compressionLevel); 4440c16b537SWarner Loshsize_t ZSTD_estimateCDictSize_advanced(size_t dictSize, ZSTD_compressionParameters cParams, ZSTD_dictLoadMethod_e dictLoadMethod); 4450c16b537SWarner Loshsize_t ZSTD_estimateDDictSize(size_t dictSize, ZSTD_dictLoadMethod_e dictLoadMethod); 4460c16b537SWarner Losh</b><p> ZSTD_estimateCDictSize() will bet that src size is relatively "small", and content is copied, like ZSTD_createCDict(). 4470c16b537SWarner Losh ZSTD_estimateCStreamSize_advanced_usingCParams() makes it possible to control precisely compression parameters, like ZSTD_createCDict_advanced(). 4480c16b537SWarner Losh Note : dictionary created by reference using ZSTD_dlm_byRef are smaller 4490c16b537SWarner Losh 4500c16b537SWarner Losh</p></pre><BR> 4510c16b537SWarner Losh 452*052d3c12SConrad Meyer<a name="Chapter15"></a><h2>Advanced compression functions</h2><pre></pre> 4530c16b537SWarner Losh 4540c16b537SWarner Losh<pre><b>ZSTD_CCtx* ZSTD_createCCtx_advanced(ZSTD_customMem customMem); 4550c16b537SWarner Losh</b><p> Create a ZSTD compression context using external alloc and free functions 4560c16b537SWarner Losh</p></pre><BR> 4570c16b537SWarner Losh 4580c16b537SWarner Losh<pre><b>ZSTD_CCtx* ZSTD_initStaticCCtx(void* workspace, size_t workspaceSize); 4590c16b537SWarner Losh</b><p> workspace: The memory area to emplace the context into. 4600c16b537SWarner Losh Provided pointer must 8-bytes aligned. 4610c16b537SWarner Losh It must outlive context usage. 4620c16b537SWarner Losh workspaceSize: Use ZSTD_estimateCCtxSize() or ZSTD_estimateCStreamSize() 4630c16b537SWarner Losh to determine how large workspace must be to support scenario. 464*052d3c12SConrad Meyer @return : pointer to ZSTD_CCtx* (same address as workspace, but different type), 465*052d3c12SConrad Meyer or NULL if error (typically size too small) 4660c16b537SWarner Losh Note : zstd will never resize nor malloc() when using a static cctx. 4670c16b537SWarner Losh If it needs more memory than available, it will simply error out. 4680c16b537SWarner Losh Note 2 : there is no corresponding "free" function. 4690c16b537SWarner Losh Since workspace was allocated externally, it must be freed externally too. 4700c16b537SWarner Losh Limitation 1 : currently not compatible with internal CDict creation, such as 4710c16b537SWarner Losh ZSTD_CCtx_loadDictionary() or ZSTD_initCStream_usingDict(). 4720c16b537SWarner Losh Limitation 2 : currently not compatible with multi-threading 4730c16b537SWarner Losh 4740c16b537SWarner Losh</p></pre><BR> 4750c16b537SWarner Losh 4760c16b537SWarner Losh<pre><b>ZSTD_CDict* ZSTD_createCDict_byReference(const void* dictBuffer, size_t dictSize, int compressionLevel); 4770c16b537SWarner Losh</b><p> Create a digested dictionary for compression 4780c16b537SWarner Losh Dictionary content is simply referenced, and therefore stays in dictBuffer. 4790c16b537SWarner Losh It is important that dictBuffer outlives CDict, it must remain read accessible throughout the lifetime of CDict 4800c16b537SWarner Losh</p></pre><BR> 4810c16b537SWarner Losh 4820c16b537SWarner Losh<pre><b>typedef enum { ZSTD_dm_auto=0, </b>/* dictionary is "full" if it starts with ZSTD_MAGIC_DICTIONARY, otherwise it is "rawContent" */<b> 4830c16b537SWarner Losh ZSTD_dm_rawContent, </b>/* ensures dictionary is always loaded as rawContent, even if it starts with ZSTD_MAGIC_DICTIONARY */<b> 4840c16b537SWarner Losh ZSTD_dm_fullDict </b>/* refuses to load a dictionary if it does not respect Zstandard's specification */<b> 4850c16b537SWarner Losh} ZSTD_dictMode_e; 4860c16b537SWarner Losh</b></pre><BR> 4870c16b537SWarner Losh<pre><b>ZSTD_CDict* ZSTD_createCDict_advanced(const void* dict, size_t dictSize, 4880c16b537SWarner Losh ZSTD_dictLoadMethod_e dictLoadMethod, 4890c16b537SWarner Losh ZSTD_dictMode_e dictMode, 4900c16b537SWarner Losh ZSTD_compressionParameters cParams, 4910c16b537SWarner Losh ZSTD_customMem customMem); 4920c16b537SWarner Losh</b><p> Create a ZSTD_CDict using external alloc and free, and customized compression parameters 4930c16b537SWarner Losh</p></pre><BR> 4940c16b537SWarner Losh 4950c16b537SWarner Losh<pre><b>ZSTD_CDict* ZSTD_initStaticCDict( 4960c16b537SWarner Losh void* workspace, size_t workspaceSize, 4970c16b537SWarner Losh const void* dict, size_t dictSize, 4980c16b537SWarner Losh ZSTD_dictLoadMethod_e dictLoadMethod, ZSTD_dictMode_e dictMode, 4990c16b537SWarner Losh ZSTD_compressionParameters cParams); 5000c16b537SWarner Losh</b><p> Generate a digested dictionary in provided memory area. 5010c16b537SWarner Losh workspace: The memory area to emplace the dictionary into. 5020c16b537SWarner Losh Provided pointer must 8-bytes aligned. 5030c16b537SWarner Losh It must outlive dictionary usage. 5040c16b537SWarner Losh workspaceSize: Use ZSTD_estimateCDictSize() 5050c16b537SWarner Losh to determine how large workspace must be. 5060c16b537SWarner Losh cParams : use ZSTD_getCParams() to transform a compression level 5070c16b537SWarner Losh into its relevants cParams. 508*052d3c12SConrad Meyer @return : pointer to ZSTD_CDict* (same address as workspace, but different type), 509*052d3c12SConrad Meyer or NULL if error (typically, size too small). 5100c16b537SWarner Losh Note : there is no corresponding "free" function. 5110c16b537SWarner Losh Since workspace was allocated externally, it must be freed externally. 5120c16b537SWarner Losh 5130c16b537SWarner Losh</p></pre><BR> 5140c16b537SWarner Losh 5150c16b537SWarner Losh<pre><b>ZSTD_compressionParameters ZSTD_getCParams(int compressionLevel, unsigned long long estimatedSrcSize, size_t dictSize); 5160c16b537SWarner Losh</b><p> @return ZSTD_compressionParameters structure for a selected compression level and estimated srcSize. 5170c16b537SWarner Losh `estimatedSrcSize` value is optional, select 0 if not known 5180c16b537SWarner Losh</p></pre><BR> 5190c16b537SWarner Losh 5200c16b537SWarner Losh<pre><b>ZSTD_parameters ZSTD_getParams(int compressionLevel, unsigned long long estimatedSrcSize, size_t dictSize); 5210c16b537SWarner Losh</b><p> same as ZSTD_getCParams(), but @return a full `ZSTD_parameters` object instead of sub-component `ZSTD_compressionParameters`. 522*052d3c12SConrad Meyer All fields of `ZSTD_frameParameters` are set to default : contentSize=1, checksum=0, noDictID=0 5230c16b537SWarner Losh</p></pre><BR> 5240c16b537SWarner Losh 5250c16b537SWarner Losh<pre><b>size_t ZSTD_checkCParams(ZSTD_compressionParameters params); 5260c16b537SWarner Losh</b><p> Ensure param values remain within authorized range 5270c16b537SWarner Losh</p></pre><BR> 5280c16b537SWarner Losh 5290c16b537SWarner Losh<pre><b>ZSTD_compressionParameters ZSTD_adjustCParams(ZSTD_compressionParameters cPar, unsigned long long srcSize, size_t dictSize); 5300c16b537SWarner Losh</b><p> optimize params for a given `srcSize` and `dictSize`. 5310c16b537SWarner Losh both values are optional, select `0` if unknown. 5320c16b537SWarner Losh</p></pre><BR> 5330c16b537SWarner Losh 5340c16b537SWarner Losh<pre><b>size_t ZSTD_compress_advanced (ZSTD_CCtx* cctx, 5350c16b537SWarner Losh void* dst, size_t dstCapacity, 5360c16b537SWarner Losh const void* src, size_t srcSize, 5370c16b537SWarner Losh const void* dict,size_t dictSize, 5380c16b537SWarner Losh ZSTD_parameters params); 5390c16b537SWarner Losh</b><p> Same as ZSTD_compress_usingDict(), with fine-tune control over each compression parameter 5400c16b537SWarner Losh</p></pre><BR> 5410c16b537SWarner Losh 5420c16b537SWarner Losh<pre><b>size_t ZSTD_compress_usingCDict_advanced(ZSTD_CCtx* cctx, 5430c16b537SWarner Losh void* dst, size_t dstCapacity, 5440c16b537SWarner Losh const void* src, size_t srcSize, 5450c16b537SWarner Losh const ZSTD_CDict* cdict, ZSTD_frameParameters fParams); 5460c16b537SWarner Losh</b><p> Same as ZSTD_compress_usingCDict(), with fine-tune control over frame parameters 5470c16b537SWarner Losh</p></pre><BR> 5480c16b537SWarner Losh 549*052d3c12SConrad Meyer<a name="Chapter16"></a><h2>Advanced decompression functions</h2><pre></pre> 5500c16b537SWarner Losh 5510c16b537SWarner Losh<pre><b>unsigned ZSTD_isFrame(const void* buffer, size_t size); 5520c16b537SWarner Losh</b><p> Tells if the content of `buffer` starts with a valid Frame Identifier. 5530c16b537SWarner Losh Note : Frame Identifier is 4 bytes. If `size < 4`, @return will always be 0. 5540c16b537SWarner Losh Note 2 : Legacy Frame Identifiers are considered valid only if Legacy Support is enabled. 5550c16b537SWarner Losh Note 3 : Skippable Frame Identifiers are considered valid. 5560c16b537SWarner Losh</p></pre><BR> 5570c16b537SWarner Losh 5580c16b537SWarner Losh<pre><b>ZSTD_DCtx* ZSTD_createDCtx_advanced(ZSTD_customMem customMem); 5590c16b537SWarner Losh</b><p> Create a ZSTD decompression context using external alloc and free functions 5600c16b537SWarner Losh</p></pre><BR> 5610c16b537SWarner Losh 5620c16b537SWarner Losh<pre><b>ZSTD_DCtx* ZSTD_initStaticDCtx(void* workspace, size_t workspaceSize); 5630c16b537SWarner Losh</b><p> workspace: The memory area to emplace the context into. 5640c16b537SWarner Losh Provided pointer must 8-bytes aligned. 5650c16b537SWarner Losh It must outlive context usage. 5660c16b537SWarner Losh workspaceSize: Use ZSTD_estimateDCtxSize() or ZSTD_estimateDStreamSize() 5670c16b537SWarner Losh to determine how large workspace must be to support scenario. 568*052d3c12SConrad Meyer @return : pointer to ZSTD_DCtx* (same address as workspace, but different type), 569*052d3c12SConrad Meyer or NULL if error (typically size too small) 5700c16b537SWarner Losh Note : zstd will never resize nor malloc() when using a static dctx. 5710c16b537SWarner Losh If it needs more memory than available, it will simply error out. 5720c16b537SWarner Losh Note 2 : static dctx is incompatible with legacy support 5730c16b537SWarner Losh Note 3 : there is no corresponding "free" function. 5740c16b537SWarner Losh Since workspace was allocated externally, it must be freed externally. 5750c16b537SWarner Losh Limitation : currently not compatible with internal DDict creation, 5760c16b537SWarner Losh such as ZSTD_initDStream_usingDict(). 5770c16b537SWarner Losh 5780c16b537SWarner Losh</p></pre><BR> 5790c16b537SWarner Losh 5800c16b537SWarner Losh<pre><b>ZSTD_DDict* ZSTD_createDDict_byReference(const void* dictBuffer, size_t dictSize); 5810c16b537SWarner Losh</b><p> Create a digested dictionary, ready to start decompression operation without startup delay. 5820c16b537SWarner Losh Dictionary content is referenced, and therefore stays in dictBuffer. 5830c16b537SWarner Losh It is important that dictBuffer outlives DDict, 5840c16b537SWarner Losh it must remain read accessible throughout the lifetime of DDict 5850c16b537SWarner Losh</p></pre><BR> 5860c16b537SWarner Losh 5870c16b537SWarner Losh<pre><b>ZSTD_DDict* ZSTD_createDDict_advanced(const void* dict, size_t dictSize, 5880c16b537SWarner Losh ZSTD_dictLoadMethod_e dictLoadMethod, 5890c16b537SWarner Losh ZSTD_customMem customMem); 5900c16b537SWarner Losh</b><p> Create a ZSTD_DDict using external alloc and free, optionally by reference 5910c16b537SWarner Losh</p></pre><BR> 5920c16b537SWarner Losh 5930c16b537SWarner Losh<pre><b>ZSTD_DDict* ZSTD_initStaticDDict(void* workspace, size_t workspaceSize, 5940c16b537SWarner Losh const void* dict, size_t dictSize, 5950c16b537SWarner Losh ZSTD_dictLoadMethod_e dictLoadMethod); 5960c16b537SWarner Losh</b><p> Generate a digested dictionary in provided memory area. 5970c16b537SWarner Losh workspace: The memory area to emplace the dictionary into. 5980c16b537SWarner Losh Provided pointer must 8-bytes aligned. 5990c16b537SWarner Losh It must outlive dictionary usage. 6000c16b537SWarner Losh workspaceSize: Use ZSTD_estimateDDictSize() 6010c16b537SWarner Losh to determine how large workspace must be. 6020c16b537SWarner Losh @return : pointer to ZSTD_DDict*, or NULL if error (size too small) 6030c16b537SWarner Losh Note : there is no corresponding "free" function. 6040c16b537SWarner Losh Since workspace was allocated externally, it must be freed externally. 6050c16b537SWarner Losh 6060c16b537SWarner Losh</p></pre><BR> 6070c16b537SWarner Losh 6080c16b537SWarner Losh<pre><b>unsigned ZSTD_getDictID_fromDict(const void* dict, size_t dictSize); 6090c16b537SWarner Losh</b><p> Provides the dictID stored within dictionary. 6100c16b537SWarner Losh if @return == 0, the dictionary is not conformant with Zstandard specification. 6110c16b537SWarner Losh It can still be loaded, but as a content-only dictionary. 6120c16b537SWarner Losh</p></pre><BR> 6130c16b537SWarner Losh 6140c16b537SWarner Losh<pre><b>unsigned ZSTD_getDictID_fromDDict(const ZSTD_DDict* ddict); 6150c16b537SWarner Losh</b><p> Provides the dictID of the dictionary loaded into `ddict`. 6160c16b537SWarner Losh If @return == 0, the dictionary is not conformant to Zstandard specification, or empty. 6170c16b537SWarner Losh Non-conformant dictionaries can still be loaded, but as content-only dictionaries. 6180c16b537SWarner Losh</p></pre><BR> 6190c16b537SWarner Losh 6200c16b537SWarner Losh<pre><b>unsigned ZSTD_getDictID_fromFrame(const void* src, size_t srcSize); 6210c16b537SWarner Losh</b><p> Provides the dictID required to decompressed the frame stored within `src`. 6220c16b537SWarner Losh If @return == 0, the dictID could not be decoded. 6230c16b537SWarner Losh This could for one of the following reasons : 6240c16b537SWarner Losh - The frame does not require a dictionary to be decoded (most common case). 6250c16b537SWarner Losh - The frame was built with dictID intentionally removed. Whatever dictionary is necessary is a hidden information. 6260c16b537SWarner Losh Note : this use case also happens when using a non-conformant dictionary. 6270c16b537SWarner Losh - `srcSize` is too small, and as a result, the frame header could not be decoded (only possible if `srcSize < ZSTD_FRAMEHEADERSIZE_MAX`). 6280c16b537SWarner Losh - This is not a Zstandard frame. 6290c16b537SWarner Losh When identifying the exact failure cause, it's possible to use ZSTD_getFrameHeader(), which will provide a more precise error code. 6300c16b537SWarner Losh</p></pre><BR> 6310c16b537SWarner Losh 632*052d3c12SConrad Meyer<a name="Chapter17"></a><h2>Advanced streaming functions</h2><pre></pre> 6330c16b537SWarner Losh 6340c16b537SWarner Losh<h3>Advanced Streaming compression functions</h3><pre></pre><b><pre>ZSTD_CStream* ZSTD_createCStream_advanced(ZSTD_customMem customMem); 6350c16b537SWarner LoshZSTD_CStream* ZSTD_initStaticCStream(void* workspace, size_t workspaceSize); </b>/**< same as ZSTD_initStaticCCtx() */<b> 636*052d3c12SConrad Meyersize_t ZSTD_initCStream_srcSize(ZSTD_CStream* zcs, int compressionLevel, unsigned long long pledgedSrcSize); </b>/**< pledgedSrcSize must be correct. If it is not known at init time, use ZSTD_CONTENTSIZE_UNKNOWN. Note that, for compatibility with older programs, "0" also disables frame content size field. It may be enabled in the future. */<b> 6370c16b537SWarner Loshsize_t ZSTD_initCStream_usingDict(ZSTD_CStream* zcs, const void* dict, size_t dictSize, int compressionLevel); </b>/**< creates of an internal CDict (incompatible with static CCtx), except if dict == NULL or dictSize < 8, in which case no dict is used. Note: dict is loaded with ZSTD_dm_auto (treated as a full zstd dictionary if it begins with ZSTD_MAGIC_DICTIONARY, else as raw content) and ZSTD_dlm_byCopy.*/<b> 6380c16b537SWarner Loshsize_t ZSTD_initCStream_advanced(ZSTD_CStream* zcs, const void* dict, size_t dictSize, 639*052d3c12SConrad Meyer ZSTD_parameters params, unsigned long long pledgedSrcSize); </b>/**< pledgedSrcSize must be correct. If srcSize is not known at init time, use value ZSTD_CONTENTSIZE_UNKNOWN. dict is loaded with ZSTD_dm_auto and ZSTD_dlm_byCopy. */<b> 6400c16b537SWarner Loshsize_t ZSTD_initCStream_usingCDict(ZSTD_CStream* zcs, const ZSTD_CDict* cdict); </b>/**< note : cdict will just be referenced, and must outlive compression session */<b> 641*052d3c12SConrad Meyersize_t ZSTD_initCStream_usingCDict_advanced(ZSTD_CStream* zcs, const ZSTD_CDict* cdict, ZSTD_frameParameters fParams, unsigned long long pledgedSrcSize); </b>/**< same as ZSTD_initCStream_usingCDict(), with control over frame parameters. pledgedSrcSize must be correct. If srcSize is not known at init time, use value ZSTD_CONTENTSIZE_UNKNOWN. */<b> 6420c16b537SWarner Losh</pre></b><BR> 6430c16b537SWarner Losh<pre><b>size_t ZSTD_resetCStream(ZSTD_CStream* zcs, unsigned long long pledgedSrcSize); 6440c16b537SWarner Losh</b><p> start a new compression job, using same parameters from previous job. 6450c16b537SWarner Losh This is typically useful to skip dictionary loading stage, since it will re-use it in-place.. 6460c16b537SWarner Losh Note that zcs must be init at least once before using ZSTD_resetCStream(). 647*052d3c12SConrad Meyer If pledgedSrcSize is not known at reset time, use macro ZSTD_CONTENTSIZE_UNKNOWN. 6480c16b537SWarner Losh If pledgedSrcSize > 0, its value must be correct, as it will be written in header, and controlled at the end. 649*052d3c12SConrad Meyer For the time being, pledgedSrcSize==0 is interpreted as "srcSize unknown" for compatibility with older programs, 650*052d3c12SConrad Meyer but it may change to mean "empty" in some future version, so prefer using macro ZSTD_CONTENTSIZE_UNKNOWN. 6510c16b537SWarner Losh @return : 0, or an error code (which can be tested using ZSTD_isError()) 6520c16b537SWarner Losh</p></pre><BR> 6530c16b537SWarner Losh 6540c16b537SWarner Losh<h3>Advanced Streaming decompression functions</h3><pre></pre><b><pre>ZSTD_DStream* ZSTD_createDStream_advanced(ZSTD_customMem customMem); 6550c16b537SWarner LoshZSTD_DStream* ZSTD_initStaticDStream(void* workspace, size_t workspaceSize); </b>/**< same as ZSTD_initStaticDCtx() */<b> 6560c16b537SWarner Loshtypedef enum { DStream_p_maxWindowSize } ZSTD_DStreamParameter_e; 6570c16b537SWarner Loshsize_t ZSTD_setDStreamParameter(ZSTD_DStream* zds, ZSTD_DStreamParameter_e paramType, unsigned paramValue); </b>/* obsolete : this API will be removed in a future version */<b> 6580c16b537SWarner Loshsize_t ZSTD_initDStream_usingDict(ZSTD_DStream* zds, const void* dict, size_t dictSize); </b>/**< note: no dictionary will be used if dict == NULL or dictSize < 8 */<b> 6590c16b537SWarner Loshsize_t ZSTD_initDStream_usingDDict(ZSTD_DStream* zds, const ZSTD_DDict* ddict); </b>/**< note : ddict is referenced, it must outlive decompression session */<b> 6600c16b537SWarner Loshsize_t ZSTD_resetDStream(ZSTD_DStream* zds); </b>/**< re-use decompression parameters from previous init; saves dictionary loading */<b> 6610c16b537SWarner Losh</pre></b><BR> 662*052d3c12SConrad Meyer<a name="Chapter18"></a><h2>Buffer-less and synchronous inner streaming functions</h2><pre> 6630c16b537SWarner Losh This is an advanced API, giving full control over buffer management, for users which need direct control over memory. 6640c16b537SWarner Losh But it's also a complex one, with several restrictions, documented below. 6650c16b537SWarner Losh Prefer normal streaming API for an easier experience. 6660c16b537SWarner Losh 6670c16b537SWarner Losh<BR></pre> 6680c16b537SWarner Losh 669*052d3c12SConrad Meyer<a name="Chapter19"></a><h2>Buffer-less streaming compression (synchronous mode)</h2><pre> 6700c16b537SWarner Losh A ZSTD_CCtx object is required to track streaming operations. 6710c16b537SWarner Losh Use ZSTD_createCCtx() / ZSTD_freeCCtx() to manage resource. 6720c16b537SWarner Losh ZSTD_CCtx object can be re-used multiple times within successive compression operations. 6730c16b537SWarner Losh 6740c16b537SWarner Losh Start by initializing a context. 6750c16b537SWarner Losh Use ZSTD_compressBegin(), or ZSTD_compressBegin_usingDict() for dictionary compression, 6760c16b537SWarner Losh or ZSTD_compressBegin_advanced(), for finer parameter control. 6770c16b537SWarner Losh It's also possible to duplicate a reference context which has already been initialized, using ZSTD_copyCCtx() 6780c16b537SWarner Losh 6790c16b537SWarner Losh Then, consume your input using ZSTD_compressContinue(). 6800c16b537SWarner Losh There are some important considerations to keep in mind when using this advanced function : 6810c16b537SWarner Losh - ZSTD_compressContinue() has no internal buffer. It uses externally provided buffers only. 6820c16b537SWarner Losh - Interface is synchronous : input is consumed entirely and produces 1+ compressed blocks. 6830c16b537SWarner Losh - Caller must ensure there is enough space in `dst` to store compressed data under worst case scenario. 6840c16b537SWarner Losh Worst case evaluation is provided by ZSTD_compressBound(). 6850c16b537SWarner Losh ZSTD_compressContinue() doesn't guarantee recover after a failed compression. 6860c16b537SWarner Losh - ZSTD_compressContinue() presumes prior input ***is still accessible and unmodified*** (up to maximum distance size, see WindowLog). 6870c16b537SWarner Losh It remembers all previous contiguous blocks, plus one separated memory segment (which can itself consists of multiple contiguous blocks) 6880c16b537SWarner Losh - ZSTD_compressContinue() detects that prior input has been overwritten when `src` buffer overlaps. 6890c16b537SWarner Losh In which case, it will "discard" the relevant memory section from its history. 6900c16b537SWarner Losh 6910c16b537SWarner Losh Finish a frame with ZSTD_compressEnd(), which will write the last block(s) and optional checksum. 6920c16b537SWarner Losh It's possible to use srcSize==0, in which case, it will write a final empty block to end the frame. 6930c16b537SWarner Losh Without last block mark, frames are considered unfinished (hence corrupted) by compliant decoders. 6940c16b537SWarner Losh 6950c16b537SWarner Losh `ZSTD_CCtx` object can be re-used (ZSTD_compressBegin()) to compress again. 6960c16b537SWarner Losh<BR></pre> 6970c16b537SWarner Losh 6980c16b537SWarner Losh<h3>Buffer-less streaming compression functions</h3><pre></pre><b><pre>size_t ZSTD_compressBegin(ZSTD_CCtx* cctx, int compressionLevel); 6990c16b537SWarner Loshsize_t ZSTD_compressBegin_usingDict(ZSTD_CCtx* cctx, const void* dict, size_t dictSize, int compressionLevel); 700*052d3c12SConrad Meyersize_t ZSTD_compressBegin_advanced(ZSTD_CCtx* cctx, const void* dict, size_t dictSize, ZSTD_parameters params, unsigned long long pledgedSrcSize); </b>/**< pledgedSrcSize : If srcSize is not known at init time, use ZSTD_CONTENTSIZE_UNKNOWN */<b> 7010c16b537SWarner Loshsize_t ZSTD_compressBegin_usingCDict(ZSTD_CCtx* cctx, const ZSTD_CDict* cdict); </b>/**< note: fails if cdict==NULL */<b> 702*052d3c12SConrad Meyersize_t ZSTD_compressBegin_usingCDict_advanced(ZSTD_CCtx* const cctx, const ZSTD_CDict* const cdict, ZSTD_frameParameters const fParams, unsigned long long const pledgedSrcSize); </b>/* compression parameters are already set within cdict. pledgedSrcSize must be correct. If srcSize is not known, use macro ZSTD_CONTENTSIZE_UNKNOWN */<b> 703*052d3c12SConrad Meyersize_t ZSTD_copyCCtx(ZSTD_CCtx* cctx, const ZSTD_CCtx* preparedCCtx, unsigned long long pledgedSrcSize); </b>/**< note: if pledgedSrcSize is not known, use ZSTD_CONTENTSIZE_UNKNOWN */<b> 7040c16b537SWarner Losh</pre></b><BR> 705*052d3c12SConrad Meyer<a name="Chapter20"></a><h2>Buffer-less streaming decompression (synchronous mode)</h2><pre> 7060c16b537SWarner Losh A ZSTD_DCtx object is required to track streaming operations. 7070c16b537SWarner Losh Use ZSTD_createDCtx() / ZSTD_freeDCtx() to manage it. 7080c16b537SWarner Losh A ZSTD_DCtx object can be re-used multiple times. 7090c16b537SWarner Losh 7100c16b537SWarner Losh First typical operation is to retrieve frame parameters, using ZSTD_getFrameHeader(). 7110c16b537SWarner Losh Frame header is extracted from the beginning of compressed frame, so providing only the frame's beginning is enough. 7120c16b537SWarner Losh Data fragment must be large enough to ensure successful decoding. 7130c16b537SWarner Losh `ZSTD_frameHeaderSize_max` bytes is guaranteed to always be large enough. 7140c16b537SWarner Losh @result : 0 : successful decoding, the `ZSTD_frameHeader` structure is correctly filled. 7150c16b537SWarner Losh >0 : `srcSize` is too small, please provide at least @result bytes on next attempt. 7160c16b537SWarner Losh errorCode, which can be tested using ZSTD_isError(). 7170c16b537SWarner Losh 7180c16b537SWarner Losh It fills a ZSTD_frameHeader structure with important information to correctly decode the frame, 7190c16b537SWarner Losh such as the dictionary ID, content size, or maximum back-reference distance (`windowSize`). 7200c16b537SWarner Losh Note that these values could be wrong, either because of data corruption, or because a 3rd party deliberately spoofs false information. 7210c16b537SWarner Losh As a consequence, check that values remain within valid application range. 7220c16b537SWarner Losh For example, do not allocate memory blindly, check that `windowSize` is within expectation. 7230c16b537SWarner Losh Each application can set its own limits, depending on local restrictions. 7240c16b537SWarner Losh For extended interoperability, it is recommended to support `windowSize` of at least 8 MB. 7250c16b537SWarner Losh 7260c16b537SWarner Losh ZSTD_decompressContinue() needs previous data blocks during decompression, up to `windowSize` bytes. 7270c16b537SWarner Losh ZSTD_decompressContinue() is very sensitive to contiguity, 7280c16b537SWarner Losh if 2 blocks don't follow each other, make sure that either the compressor breaks contiguity at the same place, 7290c16b537SWarner Losh or that previous contiguous segment is large enough to properly handle maximum back-reference distance. 7300c16b537SWarner Losh There are multiple ways to guarantee this condition. 7310c16b537SWarner Losh 7320c16b537SWarner Losh The most memory efficient way is to use a round buffer of sufficient size. 7330c16b537SWarner Losh Sufficient size is determined by invoking ZSTD_decodingBufferSize_min(), 7340c16b537SWarner Losh which can @return an error code if required value is too large for current system (in 32-bits mode). 7350c16b537SWarner Losh In a round buffer methodology, ZSTD_decompressContinue() decompresses each block next to previous one, 7360c16b537SWarner Losh up to the moment there is not enough room left in the buffer to guarantee decoding another full block, 7370c16b537SWarner Losh which maximum size is provided in `ZSTD_frameHeader` structure, field `blockSizeMax`. 7380c16b537SWarner Losh At which point, decoding can resume from the beginning of the buffer. 7390c16b537SWarner Losh Note that already decoded data stored in the buffer should be flushed before being overwritten. 7400c16b537SWarner Losh 7410c16b537SWarner Losh There are alternatives possible, for example using two or more buffers of size `windowSize` each, though they consume more memory. 7420c16b537SWarner Losh 7430c16b537SWarner Losh Finally, if you control the compression process, you can also ignore all buffer size rules, 7440c16b537SWarner Losh as long as the encoder and decoder progress in "lock-step", 7450c16b537SWarner Losh aka use exactly the same buffer sizes, break contiguity at the same place, etc. 7460c16b537SWarner Losh 7470c16b537SWarner Losh Once buffers are setup, start decompression, with ZSTD_decompressBegin(). 7480c16b537SWarner Losh If decompression requires a dictionary, use ZSTD_decompressBegin_usingDict() or ZSTD_decompressBegin_usingDDict(). 7490c16b537SWarner Losh 7500c16b537SWarner Losh Then use ZSTD_nextSrcSizeToDecompress() and ZSTD_decompressContinue() alternatively. 7510c16b537SWarner Losh ZSTD_nextSrcSizeToDecompress() tells how many bytes to provide as 'srcSize' to ZSTD_decompressContinue(). 7520c16b537SWarner Losh ZSTD_decompressContinue() requires this _exact_ amount of bytes, or it will fail. 7530c16b537SWarner Losh 7540c16b537SWarner Losh @result of ZSTD_decompressContinue() is the number of bytes regenerated within 'dst' (necessarily <= dstCapacity). 7550c16b537SWarner Losh It can be zero : it just means ZSTD_decompressContinue() has decoded some metadata item. 7560c16b537SWarner Losh It can also be an error code, which can be tested with ZSTD_isError(). 7570c16b537SWarner Losh 7580c16b537SWarner Losh A frame is fully decoded when ZSTD_nextSrcSizeToDecompress() returns zero. 7590c16b537SWarner Losh Context can then be reset to start a new decompression. 7600c16b537SWarner Losh 7610c16b537SWarner Losh Note : it's possible to know if next input to present is a header or a block, using ZSTD_nextInputType(). 7620c16b537SWarner Losh This information is not required to properly decode a frame. 7630c16b537SWarner Losh 7640c16b537SWarner Losh == Special case : skippable frames 7650c16b537SWarner Losh 7660c16b537SWarner Losh Skippable frames allow integration of user-defined data into a flow of concatenated frames. 7670c16b537SWarner Losh Skippable frames will be ignored (skipped) by decompressor. 7680c16b537SWarner Losh The format of skippable frames is as follows : 7690c16b537SWarner Losh a) Skippable frame ID - 4 Bytes, Little endian format, any value from 0x184D2A50 to 0x184D2A5F 7700c16b537SWarner Losh b) Frame Size - 4 Bytes, Little endian format, unsigned 32-bits 7710c16b537SWarner Losh c) Frame Content - any content (User Data) of length equal to Frame Size 7720c16b537SWarner Losh For skippable frames ZSTD_getFrameHeader() returns zfhPtr->frameType==ZSTD_skippableFrame. 7730c16b537SWarner Losh For skippable frames ZSTD_decompressContinue() always returns 0 : it only skips the content. 7740c16b537SWarner Losh<BR></pre> 7750c16b537SWarner Losh 7760c16b537SWarner Losh<h3>Buffer-less streaming decompression functions</h3><pre></pre><b><pre>typedef enum { ZSTD_frame, ZSTD_skippableFrame } ZSTD_frameType_e; 7770c16b537SWarner Loshtypedef struct { 7780c16b537SWarner Losh unsigned long long frameContentSize; </b>/* if == ZSTD_CONTENTSIZE_UNKNOWN, it means this field is not available. 0 means "empty" */<b> 7790c16b537SWarner Losh unsigned long long windowSize; </b>/* can be very large, up to <= frameContentSize */<b> 7800c16b537SWarner Losh unsigned blockSizeMax; 7810c16b537SWarner Losh ZSTD_frameType_e frameType; </b>/* if == ZSTD_skippableFrame, frameContentSize is the size of skippable content */<b> 7820c16b537SWarner Losh unsigned headerSize; 7830c16b537SWarner Losh unsigned dictID; 7840c16b537SWarner Losh unsigned checksumFlag; 7850c16b537SWarner Losh} ZSTD_frameHeader; 7860c16b537SWarner Loshsize_t ZSTD_getFrameHeader(ZSTD_frameHeader* zfhPtr, const void* src, size_t srcSize); </b>/**< doesn't consume input */<b> 7870c16b537SWarner Loshsize_t ZSTD_decodingBufferSize_min(unsigned long long windowSize, unsigned long long frameContentSize); </b>/**< when frame content size is not known, pass in frameContentSize == ZSTD_CONTENTSIZE_UNKNOWN */<b> 7880c16b537SWarner Losh</pre></b><BR> 7890c16b537SWarner Losh<pre><b>typedef enum { ZSTDnit_frameHeader, ZSTDnit_blockHeader, ZSTDnit_block, ZSTDnit_lastBlock, ZSTDnit_checksum, ZSTDnit_skippableFrame } ZSTD_nextInputType_e; 7900c16b537SWarner Losh</b></pre><BR> 791*052d3c12SConrad Meyer<a name="Chapter21"></a><h2>New advanced API (experimental)</h2><pre></pre> 7920c16b537SWarner Losh 7930c16b537SWarner Losh<pre><b>typedef enum { 7940c16b537SWarner Losh </b>/* Question : should we have a format ZSTD_f_auto ?<b> 7950c16b537SWarner Losh * For the time being, it would mean exactly the same as ZSTD_f_zstd1. 7960c16b537SWarner Losh * But, in the future, should several formats be supported, 7970c16b537SWarner Losh * on the compression side, it would mean "default format". 7980c16b537SWarner Losh * On the decompression side, it would mean "multi format", 7990c16b537SWarner Losh * and ZSTD_f_zstd1 could be reserved to mean "accept *only* zstd frames". 8000c16b537SWarner Losh * Since meaning is a little different, another option could be to define different enums for compression and decompression. 8010c16b537SWarner Losh * This question could be kept for later, when there are actually multiple formats to support, 8020c16b537SWarner Losh * but there is also the question of pinning enum values, and pinning value `0` is especially important */ 8030c16b537SWarner Losh ZSTD_f_zstd1 = 0, </b>/* zstd frame format, specified in zstd_compression_format.md (default) */<b> 8040c16b537SWarner Losh ZSTD_f_zstd1_magicless, </b>/* Variant of zstd frame format, without initial 4-bytes magic number.<b> 8050c16b537SWarner Losh * Useful to save 4 bytes per generated frame. 8060c16b537SWarner Losh * Decoder cannot recognise automatically this format, requiring instructions. */ 8070c16b537SWarner Losh} ZSTD_format_e; 8080c16b537SWarner Losh</b></pre><BR> 8090c16b537SWarner Losh<pre><b>typedef enum { 8100c16b537SWarner Losh </b>/* compression format */<b> 8110c16b537SWarner Losh ZSTD_p_format = 10, </b>/* See ZSTD_format_e enum definition.<b> 8120c16b537SWarner Losh * Cast selected format as unsigned for ZSTD_CCtx_setParameter() compatibility. */ 8130c16b537SWarner Losh 8140c16b537SWarner Losh </b>/* compression parameters */<b> 8150c16b537SWarner Losh ZSTD_p_compressionLevel=100, </b>/* Update all compression parameters according to pre-defined cLevel table<b> 8160c16b537SWarner Losh * Default level is ZSTD_CLEVEL_DEFAULT==3. 8170c16b537SWarner Losh * Special: value 0 means "do not change cLevel". */ 8180c16b537SWarner Losh ZSTD_p_windowLog, </b>/* Maximum allowed back-reference distance, expressed as power of 2.<b> 8190c16b537SWarner Losh * Must be clamped between ZSTD_WINDOWLOG_MIN and ZSTD_WINDOWLOG_MAX. 8200c16b537SWarner Losh * Special: value 0 means "do not change windowLog". 8210c16b537SWarner Losh * Note: Using a window size greater than ZSTD_MAXWINDOWSIZE_DEFAULT (default: 2^27) 8220c16b537SWarner Losh * requires setting the maximum window size at least as large during decompression. */ 8230c16b537SWarner Losh ZSTD_p_hashLog, </b>/* Size of the probe table, as a power of 2.<b> 8240c16b537SWarner Losh * Resulting table size is (1 << (hashLog+2)). 8250c16b537SWarner Losh * Must be clamped between ZSTD_HASHLOG_MIN and ZSTD_HASHLOG_MAX. 8260c16b537SWarner Losh * Larger tables improve compression ratio of strategies <= dFast, 8270c16b537SWarner Losh * and improve speed of strategies > dFast. 8280c16b537SWarner Losh * Special: value 0 means "do not change hashLog". */ 8290c16b537SWarner Losh ZSTD_p_chainLog, </b>/* Size of the full-search table, as a power of 2.<b> 8300c16b537SWarner Losh * Resulting table size is (1 << (chainLog+2)). 8310c16b537SWarner Losh * Larger tables result in better and slower compression. 8320c16b537SWarner Losh * This parameter is useless when using "fast" strategy. 8330c16b537SWarner Losh * Special: value 0 means "do not change chainLog". */ 8340c16b537SWarner Losh ZSTD_p_searchLog, </b>/* Number of search attempts, as a power of 2.<b> 8350c16b537SWarner Losh * More attempts result in better and slower compression. 8360c16b537SWarner Losh * This parameter is useless when using "fast" and "dFast" strategies. 8370c16b537SWarner Losh * Special: value 0 means "do not change searchLog". */ 8380c16b537SWarner Losh ZSTD_p_minMatch, </b>/* Minimum size of searched matches (note : repCode matches can be smaller).<b> 8390c16b537SWarner Losh * Larger values make faster compression and decompression, but decrease ratio. 8400c16b537SWarner Losh * Must be clamped between ZSTD_SEARCHLENGTH_MIN and ZSTD_SEARCHLENGTH_MAX. 8410c16b537SWarner Losh * Note that currently, for all strategies < btopt, effective minimum is 4. 8420c16b537SWarner Losh * Note that currently, for all strategies > fast, effective maximum is 6. 8430c16b537SWarner Losh * Special: value 0 means "do not change minMatchLength". */ 8440c16b537SWarner Losh ZSTD_p_targetLength, </b>/* Only useful for strategies >= btopt.<b> 8450c16b537SWarner Losh * Length of Match considered "good enough" to stop search. 8460c16b537SWarner Losh * Larger values make compression stronger and slower. 8470c16b537SWarner Losh * Special: value 0 means "do not change targetLength". */ 8480c16b537SWarner Losh ZSTD_p_compressionStrategy, </b>/* See ZSTD_strategy enum definition.<b> 8490c16b537SWarner Losh * Cast selected strategy as unsigned for ZSTD_CCtx_setParameter() compatibility. 8500c16b537SWarner Losh * The higher the value of selected strategy, the more complex it is, 8510c16b537SWarner Losh * resulting in stronger and slower compression. 8520c16b537SWarner Losh * Special: value 0 means "do not change strategy". */ 8530c16b537SWarner Losh 8540c16b537SWarner Losh </b>/* frame parameters */<b> 855*052d3c12SConrad Meyer ZSTD_p_contentSizeFlag=200, </b>/* Content size will be written into frame header _whenever known_ (default:1)<b> 856*052d3c12SConrad Meyer * Content size must be known at the beginning of compression, 857*052d3c12SConrad Meyer * it is provided using ZSTD_CCtx_setPledgedSrcSize() */ 8580c16b537SWarner Losh ZSTD_p_checksumFlag, </b>/* A 32-bits checksum of content is written at end of frame (default:0) */<b> 859*052d3c12SConrad Meyer ZSTD_p_dictIDFlag, </b>/* When applicable, dictionary's ID is written into frame header (default:1) */<b> 8600c16b537SWarner Losh 8610c16b537SWarner Losh </b>/* multi-threading parameters */<b> 8620c16b537SWarner Losh ZSTD_p_nbThreads=400, </b>/* Select how many threads a compression job can spawn (default:1)<b> 8630c16b537SWarner Losh * More threads improve speed, but also increase memory usage. 8640c16b537SWarner Losh * Can only receive a value > 1 if ZSTD_MULTITHREAD is enabled. 8650c16b537SWarner Losh * Special: value 0 means "do not change nbThreads" */ 866*052d3c12SConrad Meyer ZSTD_p_jobSize, </b>/* Size of a compression job. This value is only enforced in streaming (non-blocking) mode.<b> 867*052d3c12SConrad Meyer * Each compression job is completed in parallel, so indirectly controls the nb of active threads. 8680c16b537SWarner Losh * 0 means default, which is dynamically determined based on compression parameters. 8690c16b537SWarner Losh * Job size must be a minimum of overlapSize, or 1 KB, whichever is largest 8700c16b537SWarner Losh * The minimum size is automatically and transparently enforced */ 8710c16b537SWarner Losh ZSTD_p_overlapSizeLog, </b>/* Size of previous input reloaded at the beginning of each job.<b> 8720c16b537SWarner Losh * 0 => no overlap, 6(default) => use 1/8th of windowSize, >=9 => use full windowSize */ 8730c16b537SWarner Losh 8740c16b537SWarner Losh </b>/* advanced parameters - may not remain available after API update */<b> 8750c16b537SWarner Losh ZSTD_p_forceMaxWindow=1100, </b>/* Force back-reference distances to remain < windowSize,<b> 8760c16b537SWarner Losh * even when referencing into Dictionary content (default:0) */ 8770c16b537SWarner Losh ZSTD_p_enableLongDistanceMatching=1200, </b>/* Enable long distance matching.<b> 8780c16b537SWarner Losh * This parameter is designed to improve the compression 8790c16b537SWarner Losh * ratio for large inputs with long distance matches. 8800c16b537SWarner Losh * This increases the memory usage as well as window size. 8810c16b537SWarner Losh * Note: setting this parameter sets all the LDM parameters 8820c16b537SWarner Losh * as well as ZSTD_p_windowLog. It should be set after 8830c16b537SWarner Losh * ZSTD_p_compressionLevel and before ZSTD_p_windowLog and 8840c16b537SWarner Losh * other LDM parameters. Setting the compression level 8850c16b537SWarner Losh * after this parameter overrides the window log, though LDM 8860c16b537SWarner Losh * will remain enabled until explicitly disabled. */ 8870c16b537SWarner Losh ZSTD_p_ldmHashLog, </b>/* Size of the table for long distance matching, as a power of 2.<b> 8880c16b537SWarner Losh * Larger values increase memory usage and compression ratio, but decrease 8890c16b537SWarner Losh * compression speed. 8900c16b537SWarner Losh * Must be clamped between ZSTD_HASHLOG_MIN and ZSTD_HASHLOG_MAX 8910c16b537SWarner Losh * (default: windowlog - 7). */ 8920c16b537SWarner Losh ZSTD_p_ldmMinMatch, </b>/* Minimum size of searched matches for long distance matcher.<b> 8930c16b537SWarner Losh * Larger/too small values usually decrease compression ratio. 8940c16b537SWarner Losh * Must be clamped between ZSTD_LDM_MINMATCH_MIN 8950c16b537SWarner Losh * and ZSTD_LDM_MINMATCH_MAX (default: 64). */ 8960c16b537SWarner Losh ZSTD_p_ldmBucketSizeLog, </b>/* Log size of each bucket in the LDM hash table for collision resolution.<b> 8970c16b537SWarner Losh * Larger values usually improve collision resolution but may decrease 8980c16b537SWarner Losh * compression speed. 8990c16b537SWarner Losh * The maximum value is ZSTD_LDM_BUCKETSIZELOG_MAX (default: 3). */ 9000c16b537SWarner Losh ZSTD_p_ldmHashEveryLog, </b>/* Frequency of inserting/looking up entries in the LDM hash table.<b> 9010c16b537SWarner Losh * The default is MAX(0, (windowLog - ldmHashLog)) to 9020c16b537SWarner Losh * optimize hash table usage. 9030c16b537SWarner Losh * Larger values improve compression speed. Deviating far from the 9040c16b537SWarner Losh * default value will likely result in a decrease in compression ratio. 9050c16b537SWarner Losh * Must be clamped between 0 and ZSTD_WINDOWLOG_MAX - ZSTD_HASHLOG_MIN. */ 9060c16b537SWarner Losh 9070c16b537SWarner Losh} ZSTD_cParameter; 9080c16b537SWarner Losh</b></pre><BR> 9090c16b537SWarner Losh<pre><b>size_t ZSTD_CCtx_setParameter(ZSTD_CCtx* cctx, ZSTD_cParameter param, unsigned value); 9100c16b537SWarner Losh</b><p> Set one compression parameter, selected by enum ZSTD_cParameter. 9110c16b537SWarner Losh Note : when `value` is an enum, cast it to unsigned for proper type checking. 912*052d3c12SConrad Meyer @result : informational value (typically, the one being set, possibly corrected), 913*052d3c12SConrad Meyer or an error code (which can be tested with ZSTD_isError()). 9140c16b537SWarner Losh</p></pre><BR> 9150c16b537SWarner Losh 9160c16b537SWarner Losh<pre><b>size_t ZSTD_CCtx_setPledgedSrcSize(ZSTD_CCtx* cctx, unsigned long long pledgedSrcSize); 9170c16b537SWarner Losh</b><p> Total input data size to be compressed as a single frame. 9180c16b537SWarner Losh This value will be controlled at the end, and result in error if not respected. 9190c16b537SWarner Losh @result : 0, or an error code (which can be tested with ZSTD_isError()). 9200c16b537SWarner Losh Note 1 : 0 means zero, empty. 9210c16b537SWarner Losh In order to mean "unknown content size", pass constant ZSTD_CONTENTSIZE_UNKNOWN. 922*052d3c12SConrad Meyer ZSTD_CONTENTSIZE_UNKNOWN is default value for any new compression job. 9230c16b537SWarner Losh Note 2 : If all data is provided and consumed in a single round, 9240c16b537SWarner Losh this value is overriden by srcSize instead. 9250c16b537SWarner Losh</p></pre><BR> 9260c16b537SWarner Losh 9270c16b537SWarner Losh<pre><b>size_t ZSTD_CCtx_loadDictionary(ZSTD_CCtx* cctx, const void* dict, size_t dictSize); 9280c16b537SWarner Loshsize_t ZSTD_CCtx_loadDictionary_byReference(ZSTD_CCtx* cctx, const void* dict, size_t dictSize); 9290c16b537SWarner Loshsize_t ZSTD_CCtx_loadDictionary_advanced(ZSTD_CCtx* cctx, const void* dict, size_t dictSize, ZSTD_dictLoadMethod_e dictLoadMethod, ZSTD_dictMode_e dictMode); 9300c16b537SWarner Losh</b><p> Create an internal CDict from dict buffer. 9310c16b537SWarner Losh Decompression will have to use same buffer. 9320c16b537SWarner Losh @result : 0, or an error code (which can be tested with ZSTD_isError()). 9330c16b537SWarner Losh Special : Adding a NULL (or 0-size) dictionary invalidates any previous dictionary, 9340c16b537SWarner Losh meaning "return to no-dictionary mode". 9350c16b537SWarner Losh Note 1 : `dict` content will be copied internally. Use 9360c16b537SWarner Losh ZSTD_CCtx_loadDictionary_byReference() to reference dictionary 9370c16b537SWarner Losh content instead. The dictionary buffer must then outlive its 9380c16b537SWarner Losh users. 9390c16b537SWarner Losh Note 2 : Loading a dictionary involves building tables, which are dependent on compression parameters. 9400c16b537SWarner Losh For this reason, compression parameters cannot be changed anymore after loading a dictionary. 9410c16b537SWarner Losh It's also a CPU-heavy operation, with non-negligible impact on latency. 9420c16b537SWarner Losh Note 3 : Dictionary will be used for all future compression jobs. 9430c16b537SWarner Losh To return to "no-dictionary" situation, load a NULL dictionary 9440c16b537SWarner Losh Note 5 : Use ZSTD_CCtx_loadDictionary_advanced() to select how dictionary 9450c16b537SWarner Losh content will be interpreted. 9460c16b537SWarner Losh 9470c16b537SWarner Losh</p></pre><BR> 9480c16b537SWarner Losh 9490c16b537SWarner Losh<pre><b>size_t ZSTD_CCtx_refCDict(ZSTD_CCtx* cctx, const ZSTD_CDict* cdict); 9500c16b537SWarner Losh</b><p> Reference a prepared dictionary, to be used for all next compression jobs. 9510c16b537SWarner Losh Note that compression parameters are enforced from within CDict, 9520c16b537SWarner Losh and supercede any compression parameter previously set within CCtx. 9530c16b537SWarner Losh The dictionary will remain valid for future compression jobs using same CCtx. 9540c16b537SWarner Losh @result : 0, or an error code (which can be tested with ZSTD_isError()). 9550c16b537SWarner Losh Special : adding a NULL CDict means "return to no-dictionary mode". 9560c16b537SWarner Losh Note 1 : Currently, only one dictionary can be managed. 9570c16b537SWarner Losh Adding a new dictionary effectively "discards" any previous one. 9580c16b537SWarner Losh Note 2 : CDict is just referenced, its lifetime must outlive CCtx. 9590c16b537SWarner Losh 9600c16b537SWarner Losh</p></pre><BR> 9610c16b537SWarner Losh 9620c16b537SWarner Losh<pre><b>size_t ZSTD_CCtx_refPrefix(ZSTD_CCtx* cctx, const void* prefix, size_t prefixSize); 9630c16b537SWarner Loshsize_t ZSTD_CCtx_refPrefix_advanced(ZSTD_CCtx* cctx, const void* prefix, size_t prefixSize, ZSTD_dictMode_e dictMode); 9640c16b537SWarner Losh</b><p> Reference a prefix (single-usage dictionary) for next compression job. 9650c16b537SWarner Losh Decompression need same prefix to properly regenerate data. 9660c16b537SWarner Losh Prefix is **only used once**. Tables are discarded at end of compression job. 9670c16b537SWarner Losh Subsequent compression jobs will be done without prefix (if none is explicitly referenced). 9680c16b537SWarner Losh If there is a need to use same prefix multiple times, consider embedding it into a ZSTD_CDict instead. 9690c16b537SWarner Losh @result : 0, or an error code (which can be tested with ZSTD_isError()). 9700c16b537SWarner Losh Special : Adding any prefix (including NULL) invalidates any previous prefix or dictionary 9710c16b537SWarner Losh Note 1 : Prefix buffer is referenced. It must outlive compression job. 9720c16b537SWarner Losh Note 2 : Referencing a prefix involves building tables, which are dependent on compression parameters. 9730c16b537SWarner Losh It's a CPU-heavy operation, with non-negligible impact on latency. 9740c16b537SWarner Losh Note 3 : By default, the prefix is treated as raw content 9750c16b537SWarner Losh (ZSTD_dm_rawContent). Use ZSTD_CCtx_refPrefix_advanced() to alter 9760c16b537SWarner Losh dictMode. 9770c16b537SWarner Losh</p></pre><BR> 9780c16b537SWarner Losh 9790c16b537SWarner Losh<pre><b>typedef enum { 9800c16b537SWarner Losh ZSTD_e_continue=0, </b>/* collect more data, encoder transparently decides when to output result, for optimal conditions */<b> 9810c16b537SWarner Losh ZSTD_e_flush, </b>/* flush any data provided so far - frame will continue, future data can still reference previous data for better compression */<b> 9820c16b537SWarner Losh ZSTD_e_end </b>/* flush any remaining data and close current frame. Any additional data starts a new frame. */<b> 9830c16b537SWarner Losh} ZSTD_EndDirective; 9840c16b537SWarner Losh</b></pre><BR> 9850c16b537SWarner Losh<pre><b>size_t ZSTD_compress_generic (ZSTD_CCtx* cctx, 9860c16b537SWarner Losh ZSTD_outBuffer* output, 9870c16b537SWarner Losh ZSTD_inBuffer* input, 9880c16b537SWarner Losh ZSTD_EndDirective endOp); 9890c16b537SWarner Losh</b><p> Behave about the same as ZSTD_compressStream. To note : 9900c16b537SWarner Losh - Compression parameters are pushed into CCtx before starting compression, using ZSTD_CCtx_setParameter() 9910c16b537SWarner Losh - Compression parameters cannot be changed once compression is started. 9920c16b537SWarner Losh - outpot->pos must be <= dstCapacity, input->pos must be <= srcSize 9930c16b537SWarner Losh - outpot->pos and input->pos will be updated. They are guaranteed to remain below their respective limit. 994*052d3c12SConrad Meyer - In single-thread mode (default), function is blocking : it completed its job before returning to caller. 995*052d3c12SConrad Meyer - In multi-thread mode, function is non-blocking : it just acquires a copy of input, and distribute job to internal worker threads, 996*052d3c12SConrad Meyer and then immediately returns, just indicating that there is some data remaining to be flushed. 997*052d3c12SConrad Meyer The function nonetheless guarantees forward progress : it will return only after it reads or write at least 1+ byte. 998*052d3c12SConrad Meyer - Exception : in multi-threading mode, if the first call requests a ZSTD_e_end directive, it is blocking : it will complete compression before giving back control to caller. 999*052d3c12SConrad Meyer - @return provides the minimum amount of data remaining to be flushed from internal buffers 10000c16b537SWarner Losh or an error code, which can be tested using ZSTD_isError(). 1001*052d3c12SConrad Meyer if @return != 0, flush is not fully completed, there is still some data left within internal buffers. 1002*052d3c12SConrad Meyer This is useful to determine if a ZSTD_e_flush or ZSTD_e_end directive is completed. 1003*052d3c12SConrad Meyer - after a ZSTD_e_end directive, if internal buffer is not fully flushed (@return != 0), 10040c16b537SWarner Losh only ZSTD_e_end or ZSTD_e_flush operations are allowed. 1005*052d3c12SConrad Meyer Before starting a new compression job, or changing compression parameters, 1006*052d3c12SConrad Meyer it is required to fully flush internal buffers. 10070c16b537SWarner Losh 10080c16b537SWarner Losh</p></pre><BR> 10090c16b537SWarner Losh 10100c16b537SWarner Losh<pre><b>void ZSTD_CCtx_reset(ZSTD_CCtx* cctx); </b>/* Not ready yet ! */<b> 10110c16b537SWarner Losh</b><p> Return a CCtx to clean state. 10120c16b537SWarner Losh Useful after an error, or to interrupt an ongoing compression job and start a new one. 10130c16b537SWarner Losh Any internal data not yet flushed is cancelled. 10140c16b537SWarner Losh Dictionary (if any) is dropped. 10150c16b537SWarner Losh All parameters are back to default values. 10160c16b537SWarner Losh It's possible to modify compression parameters after a reset. 10170c16b537SWarner Losh 10180c16b537SWarner Losh</p></pre><BR> 10190c16b537SWarner Losh 10200c16b537SWarner Losh<pre><b>size_t ZSTD_compress_generic_simpleArgs ( 10210c16b537SWarner Losh ZSTD_CCtx* cctx, 10220c16b537SWarner Losh void* dst, size_t dstCapacity, size_t* dstPos, 10230c16b537SWarner Losh const void* src, size_t srcSize, size_t* srcPos, 10240c16b537SWarner Losh ZSTD_EndDirective endOp); 10250c16b537SWarner Losh</b><p> Same as ZSTD_compress_generic(), 10260c16b537SWarner Losh but using only integral types as arguments. 10270c16b537SWarner Losh Argument list is larger than ZSTD_{in,out}Buffer, 10280c16b537SWarner Losh but can be helpful for binders from dynamic languages 10290c16b537SWarner Losh which have troubles handling structures containing memory pointers. 10300c16b537SWarner Losh 10310c16b537SWarner Losh</p></pre><BR> 10320c16b537SWarner Losh 10330c16b537SWarner Losh<pre><b>ZSTD_CCtx_params* ZSTD_createCCtxParams(void); 10340c16b537SWarner Losh</b><p> Quick howto : 10350c16b537SWarner Losh - ZSTD_createCCtxParams() : Create a ZSTD_CCtx_params structure 10360c16b537SWarner Losh - ZSTD_CCtxParam_setParameter() : Push parameters one by one into 10370c16b537SWarner Losh an existing ZSTD_CCtx_params structure. 10380c16b537SWarner Losh This is similar to 10390c16b537SWarner Losh ZSTD_CCtx_setParameter(). 10400c16b537SWarner Losh - ZSTD_CCtx_setParametersUsingCCtxParams() : Apply parameters to 10410c16b537SWarner Losh an existing CCtx. 10420c16b537SWarner Losh These parameters will be applied to 10430c16b537SWarner Losh all subsequent compression jobs. 10440c16b537SWarner Losh - ZSTD_compress_generic() : Do compression using the CCtx. 10450c16b537SWarner Losh - ZSTD_freeCCtxParams() : Free the memory. 10460c16b537SWarner Losh 10470c16b537SWarner Losh This can be used with ZSTD_estimateCCtxSize_advanced_usingCCtxParams() 10480c16b537SWarner Losh for static allocation for single-threaded compression. 10490c16b537SWarner Losh 10500c16b537SWarner Losh</p></pre><BR> 10510c16b537SWarner Losh 10520c16b537SWarner Losh<pre><b>size_t ZSTD_resetCCtxParams(ZSTD_CCtx_params* params); 10530c16b537SWarner Losh</b><p> Reset params to default, with the default compression level. 10540c16b537SWarner Losh 10550c16b537SWarner Losh</p></pre><BR> 10560c16b537SWarner Losh 10570c16b537SWarner Losh<pre><b>size_t ZSTD_initCCtxParams(ZSTD_CCtx_params* cctxParams, int compressionLevel); 10580c16b537SWarner Losh</b><p> Initializes the compression parameters of cctxParams according to 10590c16b537SWarner Losh compression level. All other parameters are reset to their default values. 10600c16b537SWarner Losh 10610c16b537SWarner Losh</p></pre><BR> 10620c16b537SWarner Losh 10630c16b537SWarner Losh<pre><b>size_t ZSTD_initCCtxParams_advanced(ZSTD_CCtx_params* cctxParams, ZSTD_parameters params); 10640c16b537SWarner Losh</b><p> Initializes the compression and frame parameters of cctxParams according to 10650c16b537SWarner Losh params. All other parameters are reset to their default values. 10660c16b537SWarner Losh 10670c16b537SWarner Losh</p></pre><BR> 10680c16b537SWarner Losh 10690c16b537SWarner Losh<pre><b>size_t ZSTD_CCtxParam_setParameter(ZSTD_CCtx_params* params, ZSTD_cParameter param, unsigned value); 10700c16b537SWarner Losh</b><p> Similar to ZSTD_CCtx_setParameter. 10710c16b537SWarner Losh Set one compression parameter, selected by enum ZSTD_cParameter. 10720c16b537SWarner Losh Parameters must be applied to a ZSTD_CCtx using ZSTD_CCtx_setParametersUsingCCtxParams(). 10730c16b537SWarner Losh Note : when `value` is an enum, cast it to unsigned for proper type checking. 10740c16b537SWarner Losh @result : 0, or an error code (which can be tested with ZSTD_isError()). 10750c16b537SWarner Losh 10760c16b537SWarner Losh</p></pre><BR> 10770c16b537SWarner Losh 10780c16b537SWarner Losh<pre><b>size_t ZSTD_CCtx_setParametersUsingCCtxParams( 10790c16b537SWarner Losh ZSTD_CCtx* cctx, const ZSTD_CCtx_params* params); 10800c16b537SWarner Losh</b><p> Apply a set of ZSTD_CCtx_params to the compression context. 10810c16b537SWarner Losh This must be done before the dictionary is loaded. 10820c16b537SWarner Losh The pledgedSrcSize is treated as unknown. 10830c16b537SWarner Losh Multithreading parameters are applied only if nbThreads > 1. 10840c16b537SWarner Losh 10850c16b537SWarner Losh</p></pre><BR> 10860c16b537SWarner Losh 10870c16b537SWarner Losh<h3>Advanced parameters for decompression API</h3><pre></pre><b><pre></pre></b><BR> 10880c16b537SWarner Losh<pre><b>size_t ZSTD_DCtx_loadDictionary(ZSTD_DCtx* dctx, const void* dict, size_t dictSize); </b>/* not implemented */<b> 10890c16b537SWarner Loshsize_t ZSTD_DCtx_loadDictionary_byReference(ZSTD_DCtx* dctx, const void* dict, size_t dictSize); </b>/* not implemented */<b> 10900c16b537SWarner Loshsize_t ZSTD_DCtx_loadDictionary_advanced(ZSTD_DCtx* dctx, const void* dict, size_t dictSize, ZSTD_dictLoadMethod_e dictLoadMethod, ZSTD_dictMode_e dictMode); </b>/* not implemented */<b> 10910c16b537SWarner Losh</b><p> Create an internal DDict from dict buffer, 10920c16b537SWarner Losh to be used to decompress next frames. 10930c16b537SWarner Losh @result : 0, or an error code (which can be tested with ZSTD_isError()). 10940c16b537SWarner Losh Special : Adding a NULL (or 0-size) dictionary invalidates any previous dictionary, 10950c16b537SWarner Losh meaning "return to no-dictionary mode". 10960c16b537SWarner Losh Note 1 : `dict` content will be copied internally. 10970c16b537SWarner Losh Use ZSTD_DCtx_loadDictionary_byReference() 10980c16b537SWarner Losh to reference dictionary content instead. 10990c16b537SWarner Losh In which case, the dictionary buffer must outlive its users. 11000c16b537SWarner Losh Note 2 : Loading a dictionary involves building tables, 11010c16b537SWarner Losh which has a non-negligible impact on CPU usage and latency. 11020c16b537SWarner Losh Note 3 : Use ZSTD_DCtx_loadDictionary_advanced() to select 11030c16b537SWarner Losh how dictionary content will be interpreted and loaded. 11040c16b537SWarner Losh 11050c16b537SWarner Losh</p></pre><BR> 11060c16b537SWarner Losh 11070c16b537SWarner Losh<pre><b>size_t ZSTD_DCtx_refDDict(ZSTD_DCtx* dctx, const ZSTD_DDict* ddict); </b>/* not implemented */<b> 11080c16b537SWarner Losh</b><p> Reference a prepared dictionary, to be used to decompress next frames. 11090c16b537SWarner Losh The dictionary remains active for decompression of future frames using same DCtx. 11100c16b537SWarner Losh @result : 0, or an error code (which can be tested with ZSTD_isError()). 11110c16b537SWarner Losh Note 1 : Currently, only one dictionary can be managed. 11120c16b537SWarner Losh Referencing a new dictionary effectively "discards" any previous one. 11130c16b537SWarner Losh Special : adding a NULL DDict means "return to no-dictionary mode". 11140c16b537SWarner Losh Note 2 : DDict is just referenced, its lifetime must outlive its usage from DCtx. 11150c16b537SWarner Losh 11160c16b537SWarner Losh</p></pre><BR> 11170c16b537SWarner Losh 11180c16b537SWarner Losh<pre><b>size_t ZSTD_DCtx_refPrefix(ZSTD_DCtx* dctx, const void* prefix, size_t prefixSize); </b>/* not implemented */<b> 11190c16b537SWarner Loshsize_t ZSTD_DCtx_refPrefix_advanced(ZSTD_DCtx* dctx, const void* prefix, size_t prefixSize, ZSTD_dictMode_e dictMode); </b>/* not implemented */<b> 11200c16b537SWarner Losh</b><p> Reference a prefix (single-usage dictionary) for next compression job. 11210c16b537SWarner Losh Prefix is **only used once**. It must be explicitly referenced before each frame. 11220c16b537SWarner Losh If there is a need to use same prefix multiple times, consider embedding it into a ZSTD_DDict instead. 11230c16b537SWarner Losh @result : 0, or an error code (which can be tested with ZSTD_isError()). 11240c16b537SWarner Losh Note 1 : Adding any prefix (including NULL) invalidates any previously set prefix or dictionary 11250c16b537SWarner Losh Note 2 : Prefix buffer is referenced. It must outlive compression job. 11260c16b537SWarner Losh Note 3 : By default, the prefix is treated as raw content (ZSTD_dm_rawContent). 11270c16b537SWarner Losh Use ZSTD_CCtx_refPrefix_advanced() to alter dictMode. 11280c16b537SWarner Losh Note 4 : Referencing a raw content prefix has almost no cpu nor memory cost. 11290c16b537SWarner Losh 11300c16b537SWarner Losh</p></pre><BR> 11310c16b537SWarner Losh 11320c16b537SWarner Losh<pre><b>size_t ZSTD_DCtx_setMaxWindowSize(ZSTD_DCtx* dctx, size_t maxWindowSize); 11330c16b537SWarner Losh</b><p> Refuses allocating internal buffers for frames requiring a window size larger than provided limit. 11340c16b537SWarner Losh This is useful to prevent a decoder context from reserving too much memory for itself (potential attack scenario). 11350c16b537SWarner Losh This parameter is only useful in streaming mode, since no internal buffer is allocated in direct mode. 11360c16b537SWarner Losh By default, a decompression context accepts all window sizes <= (1 << ZSTD_WINDOWLOG_MAX) 11370c16b537SWarner Losh @return : 0, or an error code (which can be tested using ZSTD_isError()). 11380c16b537SWarner Losh 11390c16b537SWarner Losh</p></pre><BR> 11400c16b537SWarner Losh 11410c16b537SWarner Losh<pre><b>size_t ZSTD_DCtx_setFormat(ZSTD_DCtx* dctx, ZSTD_format_e format); 11420c16b537SWarner Losh</b><p> Instruct the decoder context about what kind of data to decode next. 11430c16b537SWarner Losh This instruction is mandatory to decode data without a fully-formed header, 11440c16b537SWarner Losh such ZSTD_f_zstd1_magicless for example. 11450c16b537SWarner Losh @return : 0, or an error code (which can be tested using ZSTD_isError()). 11460c16b537SWarner Losh 11470c16b537SWarner Losh</p></pre><BR> 11480c16b537SWarner Losh 11490c16b537SWarner Losh<pre><b>size_t ZSTD_decompress_generic(ZSTD_DCtx* dctx, 11500c16b537SWarner Losh ZSTD_outBuffer* output, 11510c16b537SWarner Losh ZSTD_inBuffer* input); 11520c16b537SWarner Losh</b><p> Behave the same as ZSTD_decompressStream. 11530c16b537SWarner Losh Decompression parameters cannot be changed once decompression is started. 11540c16b537SWarner Losh @return : an error code, which can be tested using ZSTD_isError() 11550c16b537SWarner Losh if >0, a hint, nb of expected input bytes for next invocation. 11560c16b537SWarner Losh `0` means : a frame has just been fully decoded and flushed. 11570c16b537SWarner Losh 11580c16b537SWarner Losh</p></pre><BR> 11590c16b537SWarner Losh 11600c16b537SWarner Losh<pre><b>size_t ZSTD_decompress_generic_simpleArgs ( 11610c16b537SWarner Losh ZSTD_DCtx* dctx, 11620c16b537SWarner Losh void* dst, size_t dstCapacity, size_t* dstPos, 11630c16b537SWarner Losh const void* src, size_t srcSize, size_t* srcPos); 11640c16b537SWarner Losh</b><p> Same as ZSTD_decompress_generic(), 11650c16b537SWarner Losh but using only integral types as arguments. 11660c16b537SWarner Losh Argument list is larger than ZSTD_{in,out}Buffer, 11670c16b537SWarner Losh but can be helpful for binders from dynamic languages 11680c16b537SWarner Losh which have troubles handling structures containing memory pointers. 11690c16b537SWarner Losh 11700c16b537SWarner Losh</p></pre><BR> 11710c16b537SWarner Losh 11720c16b537SWarner Losh<pre><b>void ZSTD_DCtx_reset(ZSTD_DCtx* dctx); 11730c16b537SWarner Losh</b><p> Return a DCtx to clean state. 11740c16b537SWarner Losh If a decompression was ongoing, any internal data not yet flushed is cancelled. 11750c16b537SWarner Losh All parameters are back to default values, including sticky ones. 11760c16b537SWarner Losh Dictionary (if any) is dropped. 11770c16b537SWarner Losh Parameters can be modified again after a reset. 11780c16b537SWarner Losh 11790c16b537SWarner Losh</p></pre><BR> 11800c16b537SWarner Losh 1181*052d3c12SConrad Meyer<a name="Chapter22"></a><h2>Block level API</h2><pre></pre> 11820c16b537SWarner Losh 11830c16b537SWarner Losh<pre><b></b><p> Frame metadata cost is typically ~18 bytes, which can be non-negligible for very small blocks (< 100 bytes). 11840c16b537SWarner Losh User will have to take in charge required information to regenerate data, such as compressed and content sizes. 11850c16b537SWarner Losh 11860c16b537SWarner Losh A few rules to respect : 11870c16b537SWarner Losh - Compressing and decompressing require a context structure 11880c16b537SWarner Losh + Use ZSTD_createCCtx() and ZSTD_createDCtx() 11890c16b537SWarner Losh - It is necessary to init context before starting 11900c16b537SWarner Losh + compression : any ZSTD_compressBegin*() variant, including with dictionary 11910c16b537SWarner Losh + decompression : any ZSTD_decompressBegin*() variant, including with dictionary 11920c16b537SWarner Losh + copyCCtx() and copyDCtx() can be used too 11930c16b537SWarner Losh - Block size is limited, it must be <= ZSTD_getBlockSize() <= ZSTD_BLOCKSIZE_MAX == 128 KB 11940c16b537SWarner Losh + If input is larger than a block size, it's necessary to split input data into multiple blocks 11950c16b537SWarner Losh + For inputs larger than a single block size, consider using the regular ZSTD_compress() instead. 11960c16b537SWarner Losh Frame metadata is not that costly, and quickly becomes negligible as source size grows larger. 11970c16b537SWarner Losh - When a block is considered not compressible enough, ZSTD_compressBlock() result will be zero. 11980c16b537SWarner Losh In which case, nothing is produced into `dst`. 11990c16b537SWarner Losh + User must test for such outcome and deal directly with uncompressed data 12000c16b537SWarner Losh + ZSTD_decompressBlock() doesn't accept uncompressed data as input !!! 12010c16b537SWarner Losh + In case of multiple successive blocks, should some of them be uncompressed, 12020c16b537SWarner Losh decoder must be informed of their existence in order to follow proper history. 12030c16b537SWarner Losh Use ZSTD_insertBlock() for such a case. 12040c16b537SWarner Losh</p></pre><BR> 12050c16b537SWarner Losh 12060c16b537SWarner Losh<h3>Raw zstd block functions</h3><pre></pre><b><pre>size_t ZSTD_getBlockSize (const ZSTD_CCtx* cctx); 12070c16b537SWarner Loshsize_t ZSTD_compressBlock (ZSTD_CCtx* cctx, void* dst, size_t dstCapacity, const void* src, size_t srcSize); 12080c16b537SWarner Loshsize_t ZSTD_decompressBlock(ZSTD_DCtx* dctx, void* dst, size_t dstCapacity, const void* src, size_t srcSize); 12090c16b537SWarner Loshsize_t ZSTD_insertBlock(ZSTD_DCtx* dctx, const void* blockStart, size_t blockSize); </b>/**< insert uncompressed block into `dctx` history. Useful for multi-blocks decompression */<b> 12100c16b537SWarner Losh</pre></b><BR> 12110c16b537SWarner Losh</html> 12120c16b537SWarner Losh</body> 1213