1<html> 2<head> 3<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> 4<title>zstd 1.3.7 Manual</title> 5</head> 6<body> 7<h1>zstd 1.3.7 Manual</h1> 8<hr> 9<a name="Contents"></a><h2>Contents</h2> 10<ol> 11<li><a href="#Chapter1">Introduction</a></li> 12<li><a href="#Chapter2">Version</a></li> 13<li><a href="#Chapter3">Default constant</a></li> 14<li><a href="#Chapter4">Simple API</a></li> 15<li><a href="#Chapter5">Explicit context</a></li> 16<li><a href="#Chapter6">Simple dictionary API</a></li> 17<li><a href="#Chapter7">Bulk processing dictionary API</a></li> 18<li><a href="#Chapter8">Streaming</a></li> 19<li><a href="#Chapter9">Streaming compression - HowTo</a></li> 20<li><a href="#Chapter10">Streaming decompression - HowTo</a></li> 21<li><a href="#Chapter11">ADVANCED AND EXPERIMENTAL FUNCTIONS</a></li> 22<li><a href="#Chapter12">Frame size functions</a></li> 23<li><a href="#Chapter13">Memory management</a></li> 24<li><a href="#Chapter14">Advanced compression functions</a></li> 25<li><a href="#Chapter15">Advanced decompression functions</a></li> 26<li><a href="#Chapter16">Advanced streaming functions</a></li> 27<li><a href="#Chapter17">Buffer-less and synchronous inner streaming functions</a></li> 28<li><a href="#Chapter18">Buffer-less streaming compression (synchronous mode)</a></li> 29<li><a href="#Chapter19">Buffer-less streaming decompression (synchronous mode)</a></li> 30<li><a href="#Chapter20">New advanced API (experimental)</a></li> 31<li><a href="#Chapter21">Block level API</a></li> 32</ol> 33<hr> 34<a name="Chapter1"></a><h2>Introduction</h2><pre> 35 zstd, short for Zstandard, is a fast lossless compression algorithm, targeting 36 real-time compression scenarios at zlib-level and better compression ratios. 37 The zstd compression library provides in-memory compression and decompression 38 functions. 39 40 The library supports regular compression levels from 1 up to ZSTD_maxCLevel(), 41 which is currently 22. Levels >= 20, labeled `--ultra`, should be used with 42 caution, as they require more memory. The library also offers negative 43 compression levels, which extend the range of speed vs. ratio preferences. 44 The lower the level, the faster the speed (at the cost of compression). 45 46 Compression can be done in: 47 - a single step (described as Simple API) 48 - a single step, reusing a context (described as Explicit context) 49 - unbounded multiple steps (described as Streaming compression) 50 51 The compression ratio achievable on small data can be highly improved using 52 a dictionary. Dictionary compression can be performed in: 53 - a single step (described as Simple dictionary API) 54 - a single step, reusing a dictionary (described as Bulk-processing 55 dictionary API) 56 57 Advanced experimental functions can be accessed using 58 `#define ZSTD_STATIC_LINKING_ONLY` before including zstd.h. 59 60 Advanced experimental APIs should never be used with a dynamically-linked 61 library. They are not "stable"; their definitions or signatures may change in 62 the future. Only static linking is allowed. 63<BR></pre> 64 65<a name="Chapter2"></a><h2>Version</h2><pre></pre> 66 67<pre><b>unsigned ZSTD_versionNumber(void); </b>/**< useful to check dll version */<b> 68</b></pre><BR> 69<a name="Chapter3"></a><h2>Default constant</h2><pre></pre> 70 71<a name="Chapter4"></a><h2>Simple API</h2><pre></pre> 72 73<pre><b>size_t ZSTD_compress( void* dst, size_t dstCapacity, 74 const void* src, size_t srcSize, 75 int compressionLevel); 76</b><p> Compresses `src` content as a single zstd compressed frame into already allocated `dst`. 77 Hint : compression runs faster if `dstCapacity` >= `ZSTD_compressBound(srcSize)`. 78 @return : compressed size written into `dst` (<= `dstCapacity), 79 or an error code if it fails (which can be tested using ZSTD_isError()). 80</p></pre><BR> 81 82<pre><b>size_t ZSTD_decompress( void* dst, size_t dstCapacity, 83 const void* src, size_t compressedSize); 84</b><p> `compressedSize` : must be the _exact_ size of some number of compressed and/or skippable frames. 85 `dstCapacity` is an upper bound of originalSize to regenerate. 86 If user cannot imply a maximum upper bound, it's better to use streaming mode to decompress data. 87 @return : the number of bytes decompressed into `dst` (<= `dstCapacity`), 88 or an errorCode if it fails (which can be tested using ZSTD_isError()). 89</p></pre><BR> 90 91<pre><b>#define ZSTD_CONTENTSIZE_UNKNOWN (0ULL - 1) 92#define ZSTD_CONTENTSIZE_ERROR (0ULL - 2) 93unsigned long long ZSTD_getFrameContentSize(const void *src, size_t srcSize); 94</b><p> `src` should point to the start of a ZSTD encoded frame. 95 `srcSize` must be at least as large as the frame header. 96 hint : any size >= `ZSTD_frameHeaderSize_max` is large enough. 97 @return : - decompressed size of `src` frame content, if known 98 - ZSTD_CONTENTSIZE_UNKNOWN if the size cannot be determined 99 - ZSTD_CONTENTSIZE_ERROR if an error occurred (e.g. invalid magic number, srcSize too small) 100 note 1 : a 0 return value means the frame is valid but "empty". 101 note 2 : decompressed size is an optional field, it may not be present, typically in streaming mode. 102 When `return==ZSTD_CONTENTSIZE_UNKNOWN`, data to decompress could be any size. 103 In which case, it's necessary to use streaming mode to decompress data. 104 Optionally, application can rely on some implicit limit, 105 as ZSTD_decompress() only needs an upper bound of decompressed size. 106 (For example, data could be necessarily cut into blocks <= 16 KB). 107 note 3 : decompressed size is always present when compression is completed using single-pass functions, 108 such as ZSTD_compress(), ZSTD_compressCCtx() ZSTD_compress_usingDict() or ZSTD_compress_usingCDict(). 109 note 4 : decompressed size can be very large (64-bits value), 110 potentially larger than what local system can handle as a single memory segment. 111 In which case, it's necessary to use streaming mode to decompress data. 112 note 5 : If source is untrusted, decompressed size could be wrong or intentionally modified. 113 Always ensure return value fits within application's authorized limits. 114 Each application can set its own limits. 115 note 6 : This function replaces ZSTD_getDecompressedSize() 116</p></pre><BR> 117 118<pre><b>unsigned long long ZSTD_getDecompressedSize(const void* src, size_t srcSize); 119</b><p> NOTE: This function is now obsolete, in favor of ZSTD_getFrameContentSize(). 120 Both functions work the same way, but ZSTD_getDecompressedSize() blends 121 "empty", "unknown" and "error" results to the same return value (0), 122 while ZSTD_getFrameContentSize() gives them separate return values. 123 @return : decompressed size of `src` frame content _if known and not empty_, 0 otherwise. 124</p></pre><BR> 125 126<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> 127size_t ZSTD_compressBound(size_t srcSize); </b>/*!< maximum compressed size in worst case single-pass scenario */<b> 128unsigned ZSTD_isError(size_t code); </b>/*!< tells if a `size_t` function result is an error code */<b> 129const char* ZSTD_getErrorName(size_t code); </b>/*!< provides readable string from an error code */<b> 130int ZSTD_maxCLevel(void); </b>/*!< maximum compression level available */<b> 131</pre></b><BR> 132<a name="Chapter5"></a><h2>Explicit context</h2><pre></pre> 133 134<h3>Compression context</h3><pre> When compressing many times, 135 it is recommended to allocate a context just once, and re-use it for each successive compression operation. 136 This will make workload friendlier for system's memory. 137 Use one context per thread for parallel execution in multi-threaded environments. 138</pre><b><pre>typedef struct ZSTD_CCtx_s ZSTD_CCtx; 139ZSTD_CCtx* ZSTD_createCCtx(void); 140size_t ZSTD_freeCCtx(ZSTD_CCtx* cctx); 141</pre></b><BR> 142<pre><b>size_t ZSTD_compressCCtx(ZSTD_CCtx* ctx, 143 void* dst, size_t dstCapacity, 144 const void* src, size_t srcSize, 145 int compressionLevel); 146</b><p> Same as ZSTD_compress(), requires an allocated ZSTD_CCtx (see ZSTD_createCCtx()). 147</p></pre><BR> 148 149<h3>Decompression context</h3><pre> When decompressing many times, 150 it is recommended to allocate a context only once, 151 and re-use it for each successive compression operation. 152 This will make workload friendlier for system's memory. 153 Use one context per thread for parallel execution. 154</pre><b><pre>typedef struct ZSTD_DCtx_s ZSTD_DCtx; 155ZSTD_DCtx* ZSTD_createDCtx(void); 156size_t ZSTD_freeDCtx(ZSTD_DCtx* dctx); 157</pre></b><BR> 158<pre><b>size_t ZSTD_decompressDCtx(ZSTD_DCtx* ctx, 159 void* dst, size_t dstCapacity, 160 const void* src, size_t srcSize); 161</b><p> Same as ZSTD_decompress(), requires an allocated ZSTD_DCtx (see ZSTD_createDCtx()) 162</p></pre><BR> 163 164<a name="Chapter6"></a><h2>Simple dictionary API</h2><pre></pre> 165 166<pre><b>size_t ZSTD_compress_usingDict(ZSTD_CCtx* ctx, 167 void* dst, size_t dstCapacity, 168 const void* src, size_t srcSize, 169 const void* dict,size_t dictSize, 170 int compressionLevel); 171</b><p> Compression using a predefined Dictionary (see dictBuilder/zdict.h). 172 Note : This function loads the dictionary, resulting in significant startup delay. 173 Note : When `dict == NULL || dictSize < 8` no dictionary is used. 174</p></pre><BR> 175 176<pre><b>size_t ZSTD_decompress_usingDict(ZSTD_DCtx* dctx, 177 void* dst, size_t dstCapacity, 178 const void* src, size_t srcSize, 179 const void* dict,size_t dictSize); 180</b><p> Decompression using a predefined Dictionary (see dictBuilder/zdict.h). 181 Dictionary must be identical to the one used during compression. 182 Note : This function loads the dictionary, resulting in significant startup delay. 183 Note : When `dict == NULL || dictSize < 8` no dictionary is used. 184</p></pre><BR> 185 186<a name="Chapter7"></a><h2>Bulk processing dictionary API</h2><pre></pre> 187 188<pre><b>ZSTD_CDict* ZSTD_createCDict(const void* dictBuffer, size_t dictSize, 189 int compressionLevel); 190</b><p> When compressing multiple messages / blocks with the same dictionary, it's recommended to load it just once. 191 ZSTD_createCDict() will create a digested dictionary, ready to start future compression operations without startup delay. 192 ZSTD_CDict can be created once and shared by multiple threads concurrently, since its usage is read-only. 193 `dictBuffer` can be released after ZSTD_CDict creation, since its content is copied within CDict 194 Note : A ZSTD_CDict can be created with an empty dictionary, but it is inefficient for small data. 195</p></pre><BR> 196 197<pre><b>size_t ZSTD_freeCDict(ZSTD_CDict* CDict); 198</b><p> Function frees memory allocated by ZSTD_createCDict(). 199</p></pre><BR> 200 201<pre><b>size_t ZSTD_compress_usingCDict(ZSTD_CCtx* cctx, 202 void* dst, size_t dstCapacity, 203 const void* src, size_t srcSize, 204 const ZSTD_CDict* cdict); 205</b><p> Compression using a digested Dictionary. 206 Faster startup than ZSTD_compress_usingDict(), recommended when same dictionary is used multiple times. 207 Note that compression level is decided during dictionary creation. 208 Frame parameters are hardcoded (dictID=yes, contentSize=yes, checksum=no) 209 Note : ZSTD_compress_usingCDict() can be used with a ZSTD_CDict created from an empty dictionary. 210 But it is inefficient for small data, and it is recommended to use ZSTD_compressCCtx(). 211</p></pre><BR> 212 213<pre><b>ZSTD_DDict* ZSTD_createDDict(const void* dictBuffer, size_t dictSize); 214</b><p> Create a digested dictionary, ready to start decompression operation without startup delay. 215 dictBuffer can be released after DDict creation, as its content is copied inside DDict 216</p></pre><BR> 217 218<pre><b>size_t ZSTD_freeDDict(ZSTD_DDict* ddict); 219</b><p> Function frees memory allocated with ZSTD_createDDict() 220</p></pre><BR> 221 222<pre><b>size_t ZSTD_decompress_usingDDict(ZSTD_DCtx* dctx, 223 void* dst, size_t dstCapacity, 224 const void* src, size_t srcSize, 225 const ZSTD_DDict* ddict); 226</b><p> Decompression using a digested Dictionary. 227 Faster startup than ZSTD_decompress_usingDict(), recommended when same dictionary is used multiple times. 228</p></pre><BR> 229 230<a name="Chapter8"></a><h2>Streaming</h2><pre></pre> 231 232<pre><b>typedef struct ZSTD_inBuffer_s { 233 const void* src; </b>/**< start of input buffer */<b> 234 size_t size; </b>/**< size of input buffer */<b> 235 size_t pos; </b>/**< position where reading stopped. Will be updated. Necessarily 0 <= pos <= size */<b> 236} ZSTD_inBuffer; 237</b></pre><BR> 238<pre><b>typedef struct ZSTD_outBuffer_s { 239 void* dst; </b>/**< start of output buffer */<b> 240 size_t size; </b>/**< size of output buffer */<b> 241 size_t pos; </b>/**< position where writing stopped. Will be updated. Necessarily 0 <= pos <= size */<b> 242} ZSTD_outBuffer; 243</b></pre><BR> 244<a name="Chapter9"></a><h2>Streaming compression - HowTo</h2><pre> 245 A ZSTD_CStream object is required to track streaming operation. 246 Use ZSTD_createCStream() and ZSTD_freeCStream() to create/release resources. 247 ZSTD_CStream objects can be reused multiple times on consecutive compression operations. 248 It is recommended to re-use ZSTD_CStream in situations where many streaming operations will be achieved consecutively, 249 since it will play nicer with system's memory, by re-using already allocated memory. 250 Use one separate ZSTD_CStream per thread for parallel execution. 251 252 Start a new compression by initializing ZSTD_CStream context. 253 Use ZSTD_initCStream() to start a new compression operation. 254 Use variants ZSTD_initCStream_usingDict() or ZSTD_initCStream_usingCDict() for streaming with dictionary (experimental section) 255 256 Use ZSTD_compressStream() as many times as necessary to consume input stream. 257 The function will automatically update both `pos` fields within `input` and `output`. 258 Note that the function may not consume the entire input, 259 for example, because the output buffer is already full, 260 in which case `input.pos < input.size`. 261 The caller must check if input has been entirely consumed. 262 If not, the caller must make some room to receive more compressed data, 263 typically by emptying output buffer, or allocating a new output buffer, 264 and then present again remaining input data. 265 @return : a size hint, preferred nb of bytes to use as input for next function call 266 or an error code, which can be tested using ZSTD_isError(). 267 Note 1 : it's just a hint, to help latency a little, any other value will work fine. 268 Note 2 : size hint is guaranteed to be <= ZSTD_CStreamInSize() 269 270 At any moment, it's possible to flush whatever data might remain stuck within internal buffer, 271 using ZSTD_flushStream(). `output->pos` will be updated. 272 Note that, if `output->size` is too small, a single invocation of ZSTD_flushStream() might not be enough (return code > 0). 273 In which case, make some room to receive more compressed data, and call again ZSTD_flushStream(). 274 @return : 0 if internal buffers are entirely flushed, 275 >0 if some data still present within internal buffer (the value is minimal estimation of remaining size), 276 or an error code, which can be tested using ZSTD_isError(). 277 278 ZSTD_endStream() instructs to finish a frame. 279 It will perform a flush and write frame epilogue. 280 The epilogue is required for decoders to consider a frame completed. 281 flush() operation is the same, and follows same rules as ZSTD_flushStream(). 282 @return : 0 if frame fully completed and fully flushed, 283 >0 if some data still present within internal buffer (the value is minimal estimation of remaining size), 284 or an error code, which can be tested using ZSTD_isError(). 285 286 287<BR></pre> 288 289<pre><b>typedef ZSTD_CCtx ZSTD_CStream; </b>/**< CCtx and CStream are now effectively same object (>= v1.3.0) */<b> 290</b></pre><BR> 291<h3>ZSTD_CStream management functions</h3><pre></pre><b><pre>ZSTD_CStream* ZSTD_createCStream(void); 292size_t ZSTD_freeCStream(ZSTD_CStream* zcs); 293</pre></b><BR> 294<h3>Streaming compression functions</h3><pre></pre><b><pre>size_t ZSTD_initCStream(ZSTD_CStream* zcs, int compressionLevel); 295size_t ZSTD_compressStream(ZSTD_CStream* zcs, ZSTD_outBuffer* output, ZSTD_inBuffer* input); 296size_t ZSTD_flushStream(ZSTD_CStream* zcs, ZSTD_outBuffer* output); 297size_t ZSTD_endStream(ZSTD_CStream* zcs, ZSTD_outBuffer* output); 298</pre></b><BR> 299<pre><b>size_t ZSTD_CStreamInSize(void); </b>/**< recommended size for input buffer */<b> 300</b></pre><BR> 301<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> 302</b></pre><BR> 303<a name="Chapter10"></a><h2>Streaming decompression - HowTo</h2><pre> 304 A ZSTD_DStream object is required to track streaming operations. 305 Use ZSTD_createDStream() and ZSTD_freeDStream() to create/release resources. 306 ZSTD_DStream objects can be re-used multiple times. 307 308 Use ZSTD_initDStream() to start a new decompression operation, 309 or ZSTD_initDStream_usingDict() if decompression requires a dictionary. 310 @return : recommended first input size 311 312 Use ZSTD_decompressStream() repetitively to consume your input. 313 The function will update both `pos` fields. 314 If `input.pos < input.size`, some input has not been consumed. 315 It's up to the caller to present again remaining data. 316 The function tries to flush all data decoded immediately, repecting buffer sizes. 317 If `output.pos < output.size`, decoder has flushed everything it could. 318 But if `output.pos == output.size`, there is no such guarantee, 319 it's likely that some decoded data was not flushed and still remains within internal buffers. 320 In which case, call ZSTD_decompressStream() again to flush whatever remains in the buffer. 321 When no additional input is provided, amount of data flushed is necessarily <= ZSTD_BLOCKSIZE_MAX. 322 @return : 0 when a frame is completely decoded and fully flushed, 323 or an error code, which can be tested using ZSTD_isError(), 324 or any other value > 0, which means there is still some decoding or flushing to do to complete current frame : 325 the return value is a suggested next input size (a hint for better latency) 326 that will never load more than the current frame. 327 328<BR></pre> 329 330<pre><b>typedef ZSTD_DCtx ZSTD_DStream; </b>/**< DCtx and DStream are now effectively same object (>= v1.3.0) */<b> 331</b></pre><BR> 332<h3>ZSTD_DStream management functions</h3><pre></pre><b><pre>ZSTD_DStream* ZSTD_createDStream(void); 333size_t ZSTD_freeDStream(ZSTD_DStream* zds); 334</pre></b><BR> 335<h3>Streaming decompression functions</h3><pre></pre><b><pre>size_t ZSTD_initDStream(ZSTD_DStream* zds); 336size_t ZSTD_decompressStream(ZSTD_DStream* zds, ZSTD_outBuffer* output, ZSTD_inBuffer* input); 337</pre></b><BR> 338<pre><b>size_t ZSTD_DStreamInSize(void); </b>/*!< recommended size for input buffer */<b> 339</b></pre><BR> 340<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> 341</b></pre><BR> 342<a name="Chapter11"></a><h2>ADVANCED AND EXPERIMENTAL FUNCTIONS</h2><pre> 343 The definitions in this section are considered experimental. 344 They should never be used with a dynamic library, as prototypes may change in the future. 345 They are provided for advanced scenarios. 346 Use them only in association with static linking. 347 348<BR></pre> 349 350<pre><b>int ZSTD_minCLevel(void); </b>/*!< minimum negative compression level allowed */<b> 351</b></pre><BR> 352<pre><b>typedef enum { ZSTD_fast=1, ZSTD_dfast, ZSTD_greedy, ZSTD_lazy, ZSTD_lazy2, 353 ZSTD_btlazy2, ZSTD_btopt, ZSTD_btultra } ZSTD_strategy; </b>/* from faster to stronger */<b> 354</b></pre><BR> 355<pre><b>typedef struct { 356 unsigned windowLog; </b>/**< largest match distance : larger == more compression, more memory needed during decompression */<b> 357 unsigned chainLog; </b>/**< fully searched segment : larger == more compression, slower, more memory (useless for fast) */<b> 358 unsigned hashLog; </b>/**< dispatch table : larger == faster, more memory */<b> 359 unsigned searchLog; </b>/**< nb of searches : larger == more compression, slower */<b> 360 unsigned searchLength; </b>/**< match length searched : larger == faster decompression, sometimes less compression */<b> 361 unsigned targetLength; </b>/**< acceptable match size for optimal parser (only) : larger == more compression, slower */<b> 362 ZSTD_strategy strategy; 363} ZSTD_compressionParameters; 364</b></pre><BR> 365<pre><b>typedef struct { 366 unsigned contentSizeFlag; </b>/**< 1: content size will be in frame header (when known) */<b> 367 unsigned checksumFlag; </b>/**< 1: generate a 32-bits checksum at end of frame, for error detection */<b> 368 unsigned noDictIDFlag; </b>/**< 1: no dictID will be saved into frame header (if dictionary compression) */<b> 369} ZSTD_frameParameters; 370</b></pre><BR> 371<pre><b>typedef struct { 372 ZSTD_compressionParameters cParams; 373 ZSTD_frameParameters fParams; 374} ZSTD_parameters; 375</b></pre><BR> 376<pre><b>typedef enum { 377 ZSTD_dct_auto=0, </b>/* dictionary is "full" when starting with ZSTD_MAGIC_DICTIONARY, otherwise it is "rawContent" */<b> 378 ZSTD_dct_rawContent, </b>/* ensures dictionary is always loaded as rawContent, even if it starts with ZSTD_MAGIC_DICTIONARY */<b> 379 ZSTD_dct_fullDict </b>/* refuses to load a dictionary if it does not respect Zstandard's specification */<b> 380} ZSTD_dictContentType_e; 381</b></pre><BR> 382<pre><b>typedef enum { 383 ZSTD_dlm_byCopy = 0, </b>/**< Copy dictionary content internally */<b> 384 ZSTD_dlm_byRef, </b>/**< Reference dictionary content -- the dictionary buffer must outlive its users. */<b> 385} ZSTD_dictLoadMethod_e; 386</b></pre><BR> 387<a name="Chapter12"></a><h2>Frame size functions</h2><pre></pre> 388 389<pre><b>size_t ZSTD_findFrameCompressedSize(const void* src, size_t srcSize); 390</b><p> `src` should point to the start of a ZSTD encoded frame or skippable frame 391 `srcSize` must be >= first frame size 392 @return : the compressed size of the first frame starting at `src`, 393 suitable to pass to `ZSTD_decompress` or similar, 394 or an error code if input is invalid 395</p></pre><BR> 396 397<pre><b>unsigned long long ZSTD_findDecompressedSize(const void* src, size_t srcSize); 398</b><p> `src` should point the start of a series of ZSTD encoded and/or skippable frames 399 `srcSize` must be the _exact_ size of this series 400 (i.e. there should be a frame boundary exactly at `srcSize` bytes after `src`) 401 @return : - decompressed size of all data in all successive frames 402 - if the decompressed size cannot be determined: ZSTD_CONTENTSIZE_UNKNOWN 403 - if an error occurred: ZSTD_CONTENTSIZE_ERROR 404 405 note 1 : decompressed size is an optional field, that may not be present, especially in streaming mode. 406 When `return==ZSTD_CONTENTSIZE_UNKNOWN`, data to decompress could be any size. 407 In which case, it's necessary to use streaming mode to decompress data. 408 note 2 : decompressed size is always present when compression is done with ZSTD_compress() 409 note 3 : decompressed size can be very large (64-bits value), 410 potentially larger than what local system can handle as a single memory segment. 411 In which case, it's necessary to use streaming mode to decompress data. 412 note 4 : If source is untrusted, decompressed size could be wrong or intentionally modified. 413 Always ensure result fits within application's authorized limits. 414 Each application can set its own limits. 415 note 5 : ZSTD_findDecompressedSize handles multiple frames, and so it must traverse the input to 416 read each contained frame header. This is fast as most of the data is skipped, 417 however it does mean that all frame data must be present and valid. 418</p></pre><BR> 419 420<pre><b>size_t ZSTD_frameHeaderSize(const void* src, size_t srcSize); 421</b><p> srcSize must be >= ZSTD_frameHeaderSize_prefix. 422 @return : size of the Frame Header, 423 or an error code (if srcSize is too small) 424</p></pre><BR> 425 426<a name="Chapter13"></a><h2>Memory management</h2><pre></pre> 427 428<pre><b>size_t ZSTD_sizeof_CCtx(const ZSTD_CCtx* cctx); 429size_t ZSTD_sizeof_DCtx(const ZSTD_DCtx* dctx); 430size_t ZSTD_sizeof_CStream(const ZSTD_CStream* zcs); 431size_t ZSTD_sizeof_DStream(const ZSTD_DStream* zds); 432size_t ZSTD_sizeof_CDict(const ZSTD_CDict* cdict); 433size_t ZSTD_sizeof_DDict(const ZSTD_DDict* ddict); 434</b><p> These functions give the current memory usage of selected object. 435 Object memory usage can evolve when re-used. 436</p></pre><BR> 437 438<pre><b>size_t ZSTD_estimateCCtxSize(int compressionLevel); 439size_t ZSTD_estimateCCtxSize_usingCParams(ZSTD_compressionParameters cParams); 440size_t ZSTD_estimateCCtxSize_usingCCtxParams(const ZSTD_CCtx_params* params); 441size_t ZSTD_estimateDCtxSize(void); 442</b><p> These functions make it possible to estimate memory usage 443 of a future {D,C}Ctx, before its creation. 444 ZSTD_estimateCCtxSize() will provide a budget large enough for any compression level up to selected one. 445 It will also consider src size to be arbitrarily "large", which is worst case. 446 If srcSize is known to always be small, ZSTD_estimateCCtxSize_usingCParams() can provide a tighter estimation. 447 ZSTD_estimateCCtxSize_usingCParams() can be used in tandem with ZSTD_getCParams() to create cParams from compressionLevel. 448 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_nbWorkers is >= 1. 449 Note : CCtx size estimation is only correct for single-threaded compression. 450</p></pre><BR> 451 452<pre><b>size_t ZSTD_estimateCStreamSize(int compressionLevel); 453size_t ZSTD_estimateCStreamSize_usingCParams(ZSTD_compressionParameters cParams); 454size_t ZSTD_estimateCStreamSize_usingCCtxParams(const ZSTD_CCtx_params* params); 455size_t ZSTD_estimateDStreamSize(size_t windowSize); 456size_t ZSTD_estimateDStreamSize_fromFrame(const void* src, size_t srcSize); 457</b><p> ZSTD_estimateCStreamSize() will provide a budget large enough for any compression level up to selected one. 458 It will also consider src size to be arbitrarily "large", which is worst case. 459 If srcSize is known to always be small, ZSTD_estimateCStreamSize_usingCParams() can provide a tighter estimation. 460 ZSTD_estimateCStreamSize_usingCParams() can be used in tandem with ZSTD_getCParams() to create cParams from compressionLevel. 461 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_nbWorkers is >= 1. 462 Note : CStream size estimation is only correct for single-threaded compression. 463 ZSTD_DStream memory budget depends on window Size. 464 This information can be passed manually, using ZSTD_estimateDStreamSize, 465 or deducted from a valid frame Header, using ZSTD_estimateDStreamSize_fromFrame(); 466 Note : if streaming is init with function ZSTD_init?Stream_usingDict(), 467 an internal ?Dict will be created, which additional size is not estimated here. 468 In this case, get total size by adding ZSTD_estimate?DictSize 469</p></pre><BR> 470 471<pre><b>size_t ZSTD_estimateCDictSize(size_t dictSize, int compressionLevel); 472size_t ZSTD_estimateCDictSize_advanced(size_t dictSize, ZSTD_compressionParameters cParams, ZSTD_dictLoadMethod_e dictLoadMethod); 473size_t ZSTD_estimateDDictSize(size_t dictSize, ZSTD_dictLoadMethod_e dictLoadMethod); 474</b><p> ZSTD_estimateCDictSize() will bet that src size is relatively "small", and content is copied, like ZSTD_createCDict(). 475 ZSTD_estimateCDictSize_advanced() makes it possible to control compression parameters precisely, like ZSTD_createCDict_advanced(). 476 Note : dictionaries created by reference (`ZSTD_dlm_byRef`) are logically smaller. 477 478</p></pre><BR> 479 480<pre><b>ZSTD_CCtx* ZSTD_initStaticCCtx(void* workspace, size_t workspaceSize); 481ZSTD_CStream* ZSTD_initStaticCStream(void* workspace, size_t workspaceSize); </b>/**< same as ZSTD_initStaticCCtx() */<b> 482</b><p> Initialize an object using a pre-allocated fixed-size buffer. 483 workspace: The memory area to emplace the object into. 484 Provided pointer *must be 8-bytes aligned*. 485 Buffer must outlive object. 486 workspaceSize: Use ZSTD_estimate*Size() to determine 487 how large workspace must be to support target scenario. 488 @return : pointer to object (same address as workspace, just different type), 489 or NULL if error (size too small, incorrect alignment, etc.) 490 Note : zstd will never resize nor malloc() when using a static buffer. 491 If the object requires more memory than available, 492 zstd will just error out (typically ZSTD_error_memory_allocation). 493 Note 2 : there is no corresponding "free" function. 494 Since workspace is allocated externally, it must be freed externally too. 495 Note 3 : cParams : use ZSTD_getCParams() to convert a compression level 496 into its associated cParams. 497 Limitation 1 : currently not compatible with internal dictionary creation, triggered by 498 ZSTD_CCtx_loadDictionary(), ZSTD_initCStream_usingDict() or ZSTD_initDStream_usingDict(). 499 Limitation 2 : static cctx currently not compatible with multi-threading. 500 Limitation 3 : static dctx is incompatible with legacy support. 501 502</p></pre><BR> 503 504<pre><b>ZSTD_DStream* ZSTD_initStaticDStream(void* workspace, size_t workspaceSize); </b>/**< same as ZSTD_initStaticDCtx() */<b> 505</b></pre><BR> 506<pre><b>typedef void* (*ZSTD_allocFunction) (void* opaque, size_t size); 507typedef void (*ZSTD_freeFunction) (void* opaque, void* address); 508typedef struct { ZSTD_allocFunction customAlloc; ZSTD_freeFunction customFree; void* opaque; } ZSTD_customMem; 509static ZSTD_customMem const ZSTD_defaultCMem = { NULL, NULL, NULL }; </b>/**< this constant defers to stdlib's functions */<b> 510</b><p> These prototypes make it possible to pass your own allocation/free functions. 511 ZSTD_customMem is provided at creation time, using ZSTD_create*_advanced() variants listed below. 512 All allocation/free operations will be completed using these custom variants instead of regular <stdlib.h> ones. 513 514</p></pre><BR> 515 516<a name="Chapter14"></a><h2>Advanced compression functions</h2><pre></pre> 517 518<pre><b>ZSTD_CDict* ZSTD_createCDict_byReference(const void* dictBuffer, size_t dictSize, int compressionLevel); 519</b><p> Create a digested dictionary for compression 520 Dictionary content is simply referenced, and therefore stays in dictBuffer. 521 It is important that dictBuffer outlives CDict, it must remain read accessible throughout the lifetime of CDict 522</p></pre><BR> 523 524<pre><b>ZSTD_compressionParameters ZSTD_getCParams(int compressionLevel, unsigned long long estimatedSrcSize, size_t dictSize); 525</b><p> @return ZSTD_compressionParameters structure for a selected compression level and estimated srcSize. 526 `estimatedSrcSize` value is optional, select 0 if not known 527</p></pre><BR> 528 529<pre><b>ZSTD_parameters ZSTD_getParams(int compressionLevel, unsigned long long estimatedSrcSize, size_t dictSize); 530</b><p> same as ZSTD_getCParams(), but @return a full `ZSTD_parameters` object instead of sub-component `ZSTD_compressionParameters`. 531 All fields of `ZSTD_frameParameters` are set to default : contentSize=1, checksum=0, noDictID=0 532</p></pre><BR> 533 534<pre><b>size_t ZSTD_checkCParams(ZSTD_compressionParameters params); 535</b><p> Ensure param values remain within authorized range 536</p></pre><BR> 537 538<pre><b>ZSTD_compressionParameters ZSTD_adjustCParams(ZSTD_compressionParameters cPar, unsigned long long srcSize, size_t dictSize); 539</b><p> optimize params for a given `srcSize` and `dictSize`. 540 both values are optional, select `0` if unknown. 541</p></pre><BR> 542 543<pre><b>size_t ZSTD_compress_advanced (ZSTD_CCtx* cctx, 544 void* dst, size_t dstCapacity, 545 const void* src, size_t srcSize, 546 const void* dict,size_t dictSize, 547 ZSTD_parameters params); 548</b><p> Same as ZSTD_compress_usingDict(), with fine-tune control over each compression parameter 549</p></pre><BR> 550 551<pre><b>size_t ZSTD_compress_usingCDict_advanced(ZSTD_CCtx* cctx, 552 void* dst, size_t dstCapacity, 553 const void* src, size_t srcSize, 554 const ZSTD_CDict* cdict, ZSTD_frameParameters fParams); 555</b><p> Same as ZSTD_compress_usingCDict(), with fine-tune control over frame parameters 556</p></pre><BR> 557 558<a name="Chapter15"></a><h2>Advanced decompression functions</h2><pre></pre> 559 560<pre><b>unsigned ZSTD_isFrame(const void* buffer, size_t size); 561</b><p> Tells if the content of `buffer` starts with a valid Frame Identifier. 562 Note : Frame Identifier is 4 bytes. If `size < 4`, @return will always be 0. 563 Note 2 : Legacy Frame Identifiers are considered valid only if Legacy Support is enabled. 564 Note 3 : Skippable Frame Identifiers are considered valid. 565</p></pre><BR> 566 567<pre><b>ZSTD_DDict* ZSTD_createDDict_byReference(const void* dictBuffer, size_t dictSize); 568</b><p> Create a digested dictionary, ready to start decompression operation without startup delay. 569 Dictionary content is referenced, and therefore stays in dictBuffer. 570 It is important that dictBuffer outlives DDict, 571 it must remain read accessible throughout the lifetime of DDict 572</p></pre><BR> 573 574<pre><b>unsigned ZSTD_getDictID_fromDict(const void* dict, size_t dictSize); 575</b><p> Provides the dictID stored within dictionary. 576 if @return == 0, the dictionary is not conformant with Zstandard specification. 577 It can still be loaded, but as a content-only dictionary. 578</p></pre><BR> 579 580<pre><b>unsigned ZSTD_getDictID_fromDDict(const ZSTD_DDict* ddict); 581</b><p> Provides the dictID of the dictionary loaded into `ddict`. 582 If @return == 0, the dictionary is not conformant to Zstandard specification, or empty. 583 Non-conformant dictionaries can still be loaded, but as content-only dictionaries. 584</p></pre><BR> 585 586<pre><b>unsigned ZSTD_getDictID_fromFrame(const void* src, size_t srcSize); 587</b><p> Provides the dictID required to decompressed the frame stored within `src`. 588 If @return == 0, the dictID could not be decoded. 589 This could for one of the following reasons : 590 - The frame does not require a dictionary to be decoded (most common case). 591 - The frame was built with dictID intentionally removed. Whatever dictionary is necessary is a hidden information. 592 Note : this use case also happens when using a non-conformant dictionary. 593 - `srcSize` is too small, and as a result, the frame header could not be decoded (only possible if `srcSize < ZSTD_FRAMEHEADERSIZE_MAX`). 594 - This is not a Zstandard frame. 595 When identifying the exact failure cause, it's possible to use ZSTD_getFrameHeader(), which will provide a more precise error code. 596</p></pre><BR> 597 598<a name="Chapter16"></a><h2>Advanced streaming functions</h2><pre></pre> 599 600<h3>Advanced Streaming compression functions</h3><pre></pre><b><pre>size_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> 601size_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> 602size_t ZSTD_initCStream_advanced(ZSTD_CStream* zcs, const void* dict, size_t dictSize, 603 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> 604size_t ZSTD_initCStream_usingCDict(ZSTD_CStream* zcs, const ZSTD_CDict* cdict); </b>/**< note : cdict will just be referenced, and must outlive compression session */<b> 605size_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> 606</pre></b><BR> 607<pre><b>size_t ZSTD_resetCStream(ZSTD_CStream* zcs, unsigned long long pledgedSrcSize); 608</b><p> start a new compression job, using same parameters from previous job. 609 This is typically useful to skip dictionary loading stage, since it will re-use it in-place. 610 Note that zcs must be init at least once before using ZSTD_resetCStream(). 611 If pledgedSrcSize is not known at reset time, use macro ZSTD_CONTENTSIZE_UNKNOWN. 612 If pledgedSrcSize > 0, its value must be correct, as it will be written in header, and controlled at the end. 613 For the time being, pledgedSrcSize==0 is interpreted as "srcSize unknown" for compatibility with older programs, 614 but it will change to mean "empty" in future version, so use macro ZSTD_CONTENTSIZE_UNKNOWN instead. 615 @return : 0, or an error code (which can be tested using ZSTD_isError()) 616 617</p></pre><BR> 618 619<pre><b>typedef struct { 620 unsigned long long ingested; </b>/* nb input bytes read and buffered */<b> 621 unsigned long long consumed; </b>/* nb input bytes actually compressed */<b> 622 unsigned long long produced; </b>/* nb of compressed bytes generated and buffered */<b> 623 unsigned long long flushed; </b>/* nb of compressed bytes flushed : not provided; can be tracked from caller side */<b> 624 unsigned currentJobID; </b>/* MT only : latest started job nb */<b> 625 unsigned nbActiveWorkers; </b>/* MT only : nb of workers actively compressing at probe time */<b> 626} ZSTD_frameProgression; 627</b></pre><BR> 628<pre><b>size_t ZSTD_toFlushNow(ZSTD_CCtx* cctx); 629</b><p> Tell how many bytes are ready to be flushed immediately. 630 Useful for multithreading scenarios (nbWorkers >= 1). 631 Probe the oldest active job, defined as oldest job not yet entirely flushed, 632 and check its output buffer. 633 @return : amount of data stored in oldest job and ready to be flushed immediately. 634 if @return == 0, it means either : 635 + there is no active job (could be checked with ZSTD_frameProgression()), or 636 + oldest job is still actively compressing data, 637 but everything it has produced has also been flushed so far, 638 therefore flushing speed is currently limited by production speed of oldest job 639 irrespective of the speed of concurrent newer jobs. 640 641</p></pre><BR> 642 643<h3>Advanced Streaming decompression functions</h3><pre></pre><b><pre>typedef enum { DStream_p_maxWindowSize } ZSTD_DStreamParameter_e; 644size_t ZSTD_setDStreamParameter(ZSTD_DStream* zds, ZSTD_DStreamParameter_e paramType, unsigned paramValue); </b>/* obsolete : this API will be removed in a future version */<b> 645size_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> 646size_t ZSTD_initDStream_usingDDict(ZSTD_DStream* zds, const ZSTD_DDict* ddict); </b>/**< note : ddict is referenced, it must outlive decompression session */<b> 647size_t ZSTD_resetDStream(ZSTD_DStream* zds); </b>/**< re-use decompression parameters from previous init; saves dictionary loading */<b> 648</pre></b><BR> 649<a name="Chapter17"></a><h2>Buffer-less and synchronous inner streaming functions</h2><pre> 650 This is an advanced API, giving full control over buffer management, for users which need direct control over memory. 651 But it's also a complex one, with several restrictions, documented below. 652 Prefer normal streaming API for an easier experience. 653 654<BR></pre> 655 656<a name="Chapter18"></a><h2>Buffer-less streaming compression (synchronous mode)</h2><pre> 657 A ZSTD_CCtx object is required to track streaming operations. 658 Use ZSTD_createCCtx() / ZSTD_freeCCtx() to manage resource. 659 ZSTD_CCtx object can be re-used multiple times within successive compression operations. 660 661 Start by initializing a context. 662 Use ZSTD_compressBegin(), or ZSTD_compressBegin_usingDict() for dictionary compression, 663 or ZSTD_compressBegin_advanced(), for finer parameter control. 664 It's also possible to duplicate a reference context which has already been initialized, using ZSTD_copyCCtx() 665 666 Then, consume your input using ZSTD_compressContinue(). 667 There are some important considerations to keep in mind when using this advanced function : 668 - ZSTD_compressContinue() has no internal buffer. It uses externally provided buffers only. 669 - Interface is synchronous : input is consumed entirely and produces 1+ compressed blocks. 670 - Caller must ensure there is enough space in `dst` to store compressed data under worst case scenario. 671 Worst case evaluation is provided by ZSTD_compressBound(). 672 ZSTD_compressContinue() doesn't guarantee recover after a failed compression. 673 - ZSTD_compressContinue() presumes prior input ***is still accessible and unmodified*** (up to maximum distance size, see WindowLog). 674 It remembers all previous contiguous blocks, plus one separated memory segment (which can itself consists of multiple contiguous blocks) 675 - ZSTD_compressContinue() detects that prior input has been overwritten when `src` buffer overlaps. 676 In which case, it will "discard" the relevant memory section from its history. 677 678 Finish a frame with ZSTD_compressEnd(), which will write the last block(s) and optional checksum. 679 It's possible to use srcSize==0, in which case, it will write a final empty block to end the frame. 680 Without last block mark, frames are considered unfinished (hence corrupted) by compliant decoders. 681 682 `ZSTD_CCtx` object can be re-used (ZSTD_compressBegin()) to compress again. 683<BR></pre> 684 685<h3>Buffer-less streaming compression functions</h3><pre></pre><b><pre>size_t ZSTD_compressBegin(ZSTD_CCtx* cctx, int compressionLevel); 686size_t ZSTD_compressBegin_usingDict(ZSTD_CCtx* cctx, const void* dict, size_t dictSize, int compressionLevel); 687size_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> 688size_t ZSTD_compressBegin_usingCDict(ZSTD_CCtx* cctx, const ZSTD_CDict* cdict); </b>/**< note: fails if cdict==NULL */<b> 689size_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> 690size_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> 691</pre></b><BR> 692<a name="Chapter19"></a><h2>Buffer-less streaming decompression (synchronous mode)</h2><pre> 693 A ZSTD_DCtx object is required to track streaming operations. 694 Use ZSTD_createDCtx() / ZSTD_freeDCtx() to manage it. 695 A ZSTD_DCtx object can be re-used multiple times. 696 697 First typical operation is to retrieve frame parameters, using ZSTD_getFrameHeader(). 698 Frame header is extracted from the beginning of compressed frame, so providing only the frame's beginning is enough. 699 Data fragment must be large enough to ensure successful decoding. 700 `ZSTD_frameHeaderSize_max` bytes is guaranteed to always be large enough. 701 @result : 0 : successful decoding, the `ZSTD_frameHeader` structure is correctly filled. 702 >0 : `srcSize` is too small, please provide at least @result bytes on next attempt. 703 errorCode, which can be tested using ZSTD_isError(). 704 705 It fills a ZSTD_frameHeader structure with important information to correctly decode the frame, 706 such as the dictionary ID, content size, or maximum back-reference distance (`windowSize`). 707 Note that these values could be wrong, either because of data corruption, or because a 3rd party deliberately spoofs false information. 708 As a consequence, check that values remain within valid application range. 709 For example, do not allocate memory blindly, check that `windowSize` is within expectation. 710 Each application can set its own limits, depending on local restrictions. 711 For extended interoperability, it is recommended to support `windowSize` of at least 8 MB. 712 713 ZSTD_decompressContinue() needs previous data blocks during decompression, up to `windowSize` bytes. 714 ZSTD_decompressContinue() is very sensitive to contiguity, 715 if 2 blocks don't follow each other, make sure that either the compressor breaks contiguity at the same place, 716 or that previous contiguous segment is large enough to properly handle maximum back-reference distance. 717 There are multiple ways to guarantee this condition. 718 719 The most memory efficient way is to use a round buffer of sufficient size. 720 Sufficient size is determined by invoking ZSTD_decodingBufferSize_min(), 721 which can @return an error code if required value is too large for current system (in 32-bits mode). 722 In a round buffer methodology, ZSTD_decompressContinue() decompresses each block next to previous one, 723 up to the moment there is not enough room left in the buffer to guarantee decoding another full block, 724 which maximum size is provided in `ZSTD_frameHeader` structure, field `blockSizeMax`. 725 At which point, decoding can resume from the beginning of the buffer. 726 Note that already decoded data stored in the buffer should be flushed before being overwritten. 727 728 There are alternatives possible, for example using two or more buffers of size `windowSize` each, though they consume more memory. 729 730 Finally, if you control the compression process, you can also ignore all buffer size rules, 731 as long as the encoder and decoder progress in "lock-step", 732 aka use exactly the same buffer sizes, break contiguity at the same place, etc. 733 734 Once buffers are setup, start decompression, with ZSTD_decompressBegin(). 735 If decompression requires a dictionary, use ZSTD_decompressBegin_usingDict() or ZSTD_decompressBegin_usingDDict(). 736 737 Then use ZSTD_nextSrcSizeToDecompress() and ZSTD_decompressContinue() alternatively. 738 ZSTD_nextSrcSizeToDecompress() tells how many bytes to provide as 'srcSize' to ZSTD_decompressContinue(). 739 ZSTD_decompressContinue() requires this _exact_ amount of bytes, or it will fail. 740 741 @result of ZSTD_decompressContinue() is the number of bytes regenerated within 'dst' (necessarily <= dstCapacity). 742 It can be zero : it just means ZSTD_decompressContinue() has decoded some metadata item. 743 It can also be an error code, which can be tested with ZSTD_isError(). 744 745 A frame is fully decoded when ZSTD_nextSrcSizeToDecompress() returns zero. 746 Context can then be reset to start a new decompression. 747 748 Note : it's possible to know if next input to present is a header or a block, using ZSTD_nextInputType(). 749 This information is not required to properly decode a frame. 750 751 == Special case : skippable frames 752 753 Skippable frames allow integration of user-defined data into a flow of concatenated frames. 754 Skippable frames will be ignored (skipped) by decompressor. 755 The format of skippable frames is as follows : 756 a) Skippable frame ID - 4 Bytes, Little endian format, any value from 0x184D2A50 to 0x184D2A5F 757 b) Frame Size - 4 Bytes, Little endian format, unsigned 32-bits 758 c) Frame Content - any content (User Data) of length equal to Frame Size 759 For skippable frames ZSTD_getFrameHeader() returns zfhPtr->frameType==ZSTD_skippableFrame. 760 For skippable frames ZSTD_decompressContinue() always returns 0 : it only skips the content. 761<BR></pre> 762 763<h3>Buffer-less streaming decompression functions</h3><pre></pre><b><pre>typedef enum { ZSTD_frame, ZSTD_skippableFrame } ZSTD_frameType_e; 764typedef struct { 765 unsigned long long frameContentSize; </b>/* if == ZSTD_CONTENTSIZE_UNKNOWN, it means this field is not available. 0 means "empty" */<b> 766 unsigned long long windowSize; </b>/* can be very large, up to <= frameContentSize */<b> 767 unsigned blockSizeMax; 768 ZSTD_frameType_e frameType; </b>/* if == ZSTD_skippableFrame, frameContentSize is the size of skippable content */<b> 769 unsigned headerSize; 770 unsigned dictID; 771 unsigned checksumFlag; 772} ZSTD_frameHeader; 773</b>/** ZSTD_getFrameHeader() :<b> 774 * decode Frame Header, or requires larger `srcSize`. 775 * @return : 0, `zfhPtr` is correctly filled, 776 * >0, `srcSize` is too small, value is wanted `srcSize` amount, 777 * or an error code, which can be tested using ZSTD_isError() */ 778size_t ZSTD_getFrameHeader(ZSTD_frameHeader* zfhPtr, const void* src, size_t srcSize); </b>/**< doesn't consume input */<b> 779size_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> 780</pre></b><BR> 781<pre><b>typedef enum { ZSTDnit_frameHeader, ZSTDnit_blockHeader, ZSTDnit_block, ZSTDnit_lastBlock, ZSTDnit_checksum, ZSTDnit_skippableFrame } ZSTD_nextInputType_e; 782</b></pre><BR> 783<a name="Chapter20"></a><h2>New advanced API (experimental)</h2><pre></pre> 784 785<pre><b>typedef enum { 786 </b>/* Opened question : should we have a format ZSTD_f_auto ?<b> 787 * Today, it would mean exactly the same as ZSTD_f_zstd1. 788 * But, in the future, should several formats become supported, 789 * on the compression side, it would mean "default format". 790 * On the decompression side, it would mean "automatic format detection", 791 * so that ZSTD_f_zstd1 would mean "accept *only* zstd frames". 792 * Since meaning is a little different, another option could be to define different enums for compression and decompression. 793 * This question could be kept for later, when there are actually multiple formats to support, 794 * but there is also the question of pinning enum values, and pinning value `0` is especially important */ 795 ZSTD_f_zstd1 = 0, </b>/* zstd frame format, specified in zstd_compression_format.md (default) */<b> 796 ZSTD_f_zstd1_magicless, </b>/* Variant of zstd frame format, without initial 4-bytes magic number.<b> 797 * Useful to save 4 bytes per generated frame. 798 * Decoder cannot recognise automatically this format, requiring instructions. */ 799} ZSTD_format_e; 800</b></pre><BR> 801<pre><b>typedef enum { 802 </b>/* compression format */<b> 803 ZSTD_p_format = 10, </b>/* See ZSTD_format_e enum definition.<b> 804 * Cast selected format as unsigned for ZSTD_CCtx_setParameter() compatibility. */ 805 806 </b>/* compression parameters */<b> 807 ZSTD_p_compressionLevel=100, </b>/* Update all compression parameters according to pre-defined cLevel table<b> 808 * Default level is ZSTD_CLEVEL_DEFAULT==3. 809 * Special: value 0 means default, which is controlled by ZSTD_CLEVEL_DEFAULT. 810 * Note 1 : it's possible to pass a negative compression level by casting it to unsigned type. 811 * Note 2 : setting a level sets all default values of other compression parameters. 812 * Note 3 : setting compressionLevel automatically updates ZSTD_p_compressLiterals. */ 813 ZSTD_p_windowLog, </b>/* Maximum allowed back-reference distance, expressed as power of 2.<b> 814 * Must be clamped between ZSTD_WINDOWLOG_MIN and ZSTD_WINDOWLOG_MAX. 815 * Special: value 0 means "use default windowLog". 816 * Note: Using a window size greater than ZSTD_MAXWINDOWSIZE_DEFAULT (default: 2^27) 817 * requires explicitly allowing such window size during decompression stage. */ 818 ZSTD_p_hashLog, </b>/* Size of the initial probe table, as a power of 2.<b> 819 * Resulting table size is (1 << (hashLog+2)). 820 * Must be clamped between ZSTD_HASHLOG_MIN and ZSTD_HASHLOG_MAX. 821 * Larger tables improve compression ratio of strategies <= dFast, 822 * and improve speed of strategies > dFast. 823 * Special: value 0 means "use default hashLog". */ 824 ZSTD_p_chainLog, </b>/* Size of the multi-probe search table, as a power of 2.<b> 825 * Resulting table size is (1 << (chainLog+2)). 826 * Must be clamped between ZSTD_CHAINLOG_MIN and ZSTD_CHAINLOG_MAX. 827 * Larger tables result in better and slower compression. 828 * This parameter is useless when using "fast" strategy. 829 * Note it's still useful when using "dfast" strategy, 830 * in which case it defines a secondary probe table. 831 * Special: value 0 means "use default chainLog". */ 832 ZSTD_p_searchLog, </b>/* Number of search attempts, as a power of 2.<b> 833 * More attempts result in better and slower compression. 834 * This parameter is useless when using "fast" and "dFast" strategies. 835 * Special: value 0 means "use default searchLog". */ 836 ZSTD_p_minMatch, </b>/* Minimum size of searched matches (note : repCode matches can be smaller).<b> 837 * Larger values make faster compression and decompression, but decrease ratio. 838 * Must be clamped between ZSTD_SEARCHLENGTH_MIN and ZSTD_SEARCHLENGTH_MAX. 839 * Note that currently, for all strategies < btopt, effective minimum is 4. 840 * , for all strategies > fast, effective maximum is 6. 841 * Special: value 0 means "use default minMatchLength". */ 842 ZSTD_p_targetLength, </b>/* Impact of this field depends on strategy.<b> 843 * For strategies btopt & btultra: 844 * Length of Match considered "good enough" to stop search. 845 * Larger values make compression stronger, and slower. 846 * For strategy fast: 847 * Distance between match sampling. 848 * Larger values make compression faster, and weaker. 849 * Special: value 0 means "use default targetLength". */ 850 ZSTD_p_compressionStrategy, </b>/* See ZSTD_strategy enum definition.<b> 851 * Cast selected strategy as unsigned for ZSTD_CCtx_setParameter() compatibility. 852 * The higher the value of selected strategy, the more complex it is, 853 * resulting in stronger and slower compression. 854 * Special: value 0 means "use default strategy". */ 855 856 ZSTD_p_enableLongDistanceMatching=160, </b>/* Enable long distance matching.<b> 857 * This parameter is designed to improve compression ratio 858 * for large inputs, by finding large matches at long distance. 859 * It increases memory usage and window size. 860 * Note: enabling this parameter increases ZSTD_p_windowLog to 128 MB 861 * except when expressly set to a different value. */ 862 ZSTD_p_ldmHashLog, </b>/* Size of the table for long distance matching, as a power of 2.<b> 863 * Larger values increase memory usage and compression ratio, 864 * but decrease compression speed. 865 * Must be clamped between ZSTD_HASHLOG_MIN and ZSTD_HASHLOG_MAX 866 * default: windowlog - 7. 867 * Special: value 0 means "automatically determine hashlog". */ 868 ZSTD_p_ldmMinMatch, </b>/* Minimum match size for long distance matcher.<b> 869 * Larger/too small values usually decrease compression ratio. 870 * Must be clamped between ZSTD_LDM_MINMATCH_MIN and ZSTD_LDM_MINMATCH_MAX. 871 * Special: value 0 means "use default value" (default: 64). */ 872 ZSTD_p_ldmBucketSizeLog, </b>/* Log size of each bucket in the LDM hash table for collision resolution.<b> 873 * Larger values improve collision resolution but decrease compression speed. 874 * The maximum value is ZSTD_LDM_BUCKETSIZELOG_MAX . 875 * Special: value 0 means "use default value" (default: 3). */ 876 ZSTD_p_ldmHashEveryLog, </b>/* Frequency of inserting/looking up entries in the LDM hash table.<b> 877 * Must be clamped between 0 and (ZSTD_WINDOWLOG_MAX - ZSTD_HASHLOG_MIN). 878 * Default is MAX(0, (windowLog - ldmHashLog)), optimizing hash table usage. 879 * Larger values improve compression speed. 880 * Deviating far from default value will likely result in a compression ratio decrease. 881 * Special: value 0 means "automatically determine hashEveryLog". */ 882 883 </b>/* frame parameters */<b> 884 ZSTD_p_contentSizeFlag=200, </b>/* Content size will be written into frame header _whenever known_ (default:1)<b> 885 * Content size must be known at the beginning of compression, 886 * it is provided using ZSTD_CCtx_setPledgedSrcSize() */ 887 ZSTD_p_checksumFlag, </b>/* A 32-bits checksum of content is written at end of frame (default:0) */<b> 888 ZSTD_p_dictIDFlag, </b>/* When applicable, dictionary's ID is written into frame header (default:1) */<b> 889 890 </b>/* multi-threading parameters */<b> 891 </b>/* These parameters are only useful if multi-threading is enabled (ZSTD_MULTITHREAD).<b> 892 * They return an error otherwise. */ 893 ZSTD_p_nbWorkers=400, </b>/* Select how many threads will be spawned to compress in parallel.<b> 894 * When nbWorkers >= 1, triggers asynchronous mode : 895 * ZSTD_compress_generic() consumes some input, flush some output if possible, and immediately gives back control to caller, 896 * while compression work is performed in parallel, within worker threads. 897 * (note : a strong exception to this rule is when first invocation sets ZSTD_e_end : it becomes a blocking call). 898 * More workers improve speed, but also increase memory usage. 899 * Default value is `0`, aka "single-threaded mode" : no worker is spawned, compression is performed inside Caller's thread, all invocations are blocking */ 900 ZSTD_p_jobSize, </b>/* Size of a compression job. This value is enforced only in non-blocking mode.<b> 901 * Each compression job is completed in parallel, so this value indirectly controls the nb of active threads. 902 * 0 means default, which is dynamically determined based on compression parameters. 903 * Job size must be a minimum of overlapSize, or 1 MB, whichever is largest. 904 * The minimum size is automatically and transparently enforced */ 905 ZSTD_p_overlapSizeLog, </b>/* Size of previous input reloaded at the beginning of each job.<b> 906 * 0 => no overlap, 6(default) => use 1/8th of windowSize, >=9 => use full windowSize */ 907 908 </b>/* =================================================================== */<b> 909 </b>/* experimental parameters - no stability guaranteed */<b> 910 </b>/* =================================================================== */<b> 911 912 ZSTD_p_forceMaxWindow=1100, </b>/* Force back-reference distances to remain < windowSize,<b> 913 * even when referencing into Dictionary content (default:0) */ 914 ZSTD_p_forceAttachDict, </b>/* ZSTD supports usage of a CDict in-place<b> 915 * (avoiding having to copy the compression tables 916 * from the CDict into the working context). Using 917 * a CDict in this way saves an initial setup step, 918 * but comes at the cost of more work per byte of 919 * input. ZSTD has a simple internal heuristic that 920 * guesses which strategy will be faster. You can 921 * use this flag to override that guess. 922 * 923 * Note that the by-reference, in-place strategy is 924 * only used when reusing a compression context 925 * with compatible compression parameters. (If 926 * incompatible / uninitialized, the working 927 * context needs to be cleared anyways, which is 928 * about as expensive as overwriting it with the 929 * dictionary context, so there's no savings in 930 * using the CDict by-ref.) 931 * 932 * Values greater than 0 force attaching the dict. 933 * Values less than 0 force copying the dict. 934 * 0 selects the default heuristic-guided behavior. 935 */ 936 937} ZSTD_cParameter; 938</b></pre><BR> 939<pre><b>size_t ZSTD_CCtx_setParameter(ZSTD_CCtx* cctx, ZSTD_cParameter param, unsigned value); 940</b><p> Set one compression parameter, selected by enum ZSTD_cParameter. 941 Setting a parameter is generally only possible during frame initialization (before starting compression). 942 Exception : when using multi-threading mode (nbThreads >= 1), 943 following parameters can be updated _during_ compression (within same frame): 944 => compressionLevel, hashLog, chainLog, searchLog, minMatch, targetLength and strategy. 945 new parameters will be active on next job, or after a flush(). 946 Note : when `value` type is not unsigned (int, or enum), cast it to unsigned for proper type checking. 947 @result : informational value (typically, value being set, correctly clamped), 948 or an error code (which can be tested with ZSTD_isError()). 949</p></pre><BR> 950 951<pre><b>size_t ZSTD_CCtx_getParameter(ZSTD_CCtx* cctx, ZSTD_cParameter param, unsigned* value); 952</b><p> Get the requested value of one compression parameter, selected by enum ZSTD_cParameter. 953 @result : 0, or an error code (which can be tested with ZSTD_isError()). 954 955</p></pre><BR> 956 957<pre><b>size_t ZSTD_CCtx_setPledgedSrcSize(ZSTD_CCtx* cctx, unsigned long long pledgedSrcSize); 958</b><p> Total input data size to be compressed as a single frame. 959 This value will be controlled at the end, and result in error if not respected. 960 @result : 0, or an error code (which can be tested with ZSTD_isError()). 961 Note 1 : 0 means zero, empty. 962 In order to mean "unknown content size", pass constant ZSTD_CONTENTSIZE_UNKNOWN. 963 ZSTD_CONTENTSIZE_UNKNOWN is default value for any new compression job. 964 Note 2 : If all data is provided and consumed in a single round, 965 this value is overriden by srcSize instead. 966</p></pre><BR> 967 968<pre><b>size_t ZSTD_CCtx_loadDictionary(ZSTD_CCtx* cctx, const void* dict, size_t dictSize); 969size_t ZSTD_CCtx_loadDictionary_byReference(ZSTD_CCtx* cctx, const void* dict, size_t dictSize); 970size_t ZSTD_CCtx_loadDictionary_advanced(ZSTD_CCtx* cctx, const void* dict, size_t dictSize, ZSTD_dictLoadMethod_e dictLoadMethod, ZSTD_dictContentType_e dictContentType); 971</b><p> Create an internal CDict from `dict` buffer. 972 Decompression will have to use same dictionary. 973 @result : 0, or an error code (which can be tested with ZSTD_isError()). 974 Special: Adding a NULL (or 0-size) dictionary invalidates previous dictionary, 975 meaning "return to no-dictionary mode". 976 Note 1 : Dictionary will be used for all future compression jobs. 977 To return to "no-dictionary" situation, load a NULL dictionary 978 Note 2 : Loading a dictionary involves building tables, which are dependent on compression parameters. 979 For this reason, compression parameters cannot be changed anymore after loading a dictionary. 980 It's also a CPU consuming operation, with non-negligible impact on latency. 981 Note 3 :`dict` content will be copied internally. 982 Use ZSTD_CCtx_loadDictionary_byReference() to reference dictionary content instead. 983 In such a case, dictionary buffer must outlive its users. 984 Note 4 : Use ZSTD_CCtx_loadDictionary_advanced() 985 to precisely select how dictionary content must be interpreted. 986</p></pre><BR> 987 988<pre><b>size_t ZSTD_CCtx_refCDict(ZSTD_CCtx* cctx, const ZSTD_CDict* cdict); 989</b><p> Reference a prepared dictionary, to be used for all next compression jobs. 990 Note that compression parameters are enforced from within CDict, 991 and supercede any compression parameter previously set within CCtx. 992 The dictionary will remain valid for future compression jobs using same CCtx. 993 @result : 0, or an error code (which can be tested with ZSTD_isError()). 994 Special : adding a NULL CDict means "return to no-dictionary mode". 995 Note 1 : Currently, only one dictionary can be managed. 996 Adding a new dictionary effectively "discards" any previous one. 997 Note 2 : CDict is just referenced, its lifetime must outlive CCtx. 998</p></pre><BR> 999 1000<pre><b>size_t ZSTD_CCtx_refPrefix(ZSTD_CCtx* cctx, 1001 const void* prefix, size_t prefixSize); 1002size_t ZSTD_CCtx_refPrefix_advanced(ZSTD_CCtx* cctx, 1003 const void* prefix, size_t prefixSize, 1004 ZSTD_dictContentType_e dictContentType); 1005</b><p> Reference a prefix (single-usage dictionary) for next compression job. 1006 Decompression will need same prefix to properly regenerate data. 1007 Compressing with a prefix is similar in outcome as performing a diff and compressing it, 1008 but performs much faster, especially during decompression (compression speed is tunable with compression level). 1009 Note that prefix is **only used once**. Tables are discarded at end of compression job (ZSTD_e_end). 1010 @result : 0, or an error code (which can be tested with ZSTD_isError()). 1011 Special: Adding any prefix (including NULL) invalidates any previous prefix or dictionary 1012 Note 1 : Prefix buffer is referenced. It **must** outlive compression job. 1013 Its contain must remain unmodified up to end of compression (ZSTD_e_end). 1014 Note 2 : If the intention is to diff some large src data blob with some prior version of itself, 1015 ensure that the window size is large enough to contain the entire source. 1016 See ZSTD_p_windowLog. 1017 Note 3 : Referencing a prefix involves building tables, which are dependent on compression parameters. 1018 It's a CPU consuming operation, with non-negligible impact on latency. 1019 If there is a need to use same prefix multiple times, consider loadDictionary instead. 1020 Note 4 : By default, the prefix is treated as raw content (ZSTD_dm_rawContent). 1021 Use ZSTD_CCtx_refPrefix_advanced() to alter dictMode. 1022</p></pre><BR> 1023 1024<pre><b>void ZSTD_CCtx_reset(ZSTD_CCtx* cctx); 1025</b><p> Return a CCtx to clean state. 1026 Useful after an error, or to interrupt an ongoing compression job and start a new one. 1027 Any internal data not yet flushed is cancelled. 1028 The parameters and dictionary are kept unchanged, to reset them use ZSTD_CCtx_resetParameters(). 1029 1030</p></pre><BR> 1031 1032<pre><b>size_t ZSTD_CCtx_resetParameters(ZSTD_CCtx* cctx); 1033</b><p> All parameters are back to default values (compression level is ZSTD_CLEVEL_DEFAULT). 1034 Dictionary (if any) is dropped. 1035 Resetting parameters is only possible during frame initialization (before starting compression). 1036 To reset the context use ZSTD_CCtx_reset(). 1037 @return 0 or an error code (which can be checked with ZSTD_isError()). 1038 1039</p></pre><BR> 1040 1041<pre><b>typedef enum { 1042 ZSTD_e_continue=0, </b>/* collect more data, encoder decides when to output compressed result, for optimal compression ratio */<b> 1043 ZSTD_e_flush, </b>/* flush any data provided so far,<b> 1044 * it creates (at least) one new block, that can be decoded immediately on reception; 1045 * frame will continue: any future data can still reference previously compressed data, improving compression. */ 1046 ZSTD_e_end </b>/* flush any remaining data and close current frame.<b> 1047 * any additional data starts a new frame. 1048 * each frame is independent (does not reference any content from previous frame). */ 1049} ZSTD_EndDirective; 1050</b></pre><BR> 1051<pre><b>size_t ZSTD_compress_generic (ZSTD_CCtx* cctx, 1052 ZSTD_outBuffer* output, 1053 ZSTD_inBuffer* input, 1054 ZSTD_EndDirective endOp); 1055</b><p> Behave about the same as ZSTD_compressStream. To note : 1056 - Compression parameters are pushed into CCtx before starting compression, using ZSTD_CCtx_setParameter() 1057 - Compression parameters cannot be changed once compression is started. 1058 - outpot->pos must be <= dstCapacity, input->pos must be <= srcSize 1059 - outpot->pos and input->pos will be updated. They are guaranteed to remain below their respective limit. 1060 - In single-thread mode (default), function is blocking : it completed its job before returning to caller. 1061 - In multi-thread mode, function is non-blocking : it just acquires a copy of input, and distribute job to internal worker threads, 1062 and then immediately returns, just indicating that there is some data remaining to be flushed. 1063 The function nonetheless guarantees forward progress : it will return only after it reads or write at least 1+ byte. 1064 - 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. 1065 - @return provides a minimum amount of data remaining to be flushed from internal buffers 1066 or an error code, which can be tested using ZSTD_isError(). 1067 if @return != 0, flush is not fully completed, there is still some data left within internal buffers. 1068 This is useful for ZSTD_e_flush, since in this case more flushes are necessary to empty all buffers. 1069 For ZSTD_e_end, @return == 0 when internal buffers are fully flushed and frame is completed. 1070 - after a ZSTD_e_end directive, if internal buffer is not fully flushed (@return != 0), 1071 only ZSTD_e_end or ZSTD_e_flush operations are allowed. 1072 Before starting a new compression job, or changing compression parameters, 1073 it is required to fully flush internal buffers. 1074 1075</p></pre><BR> 1076 1077<pre><b>size_t ZSTD_compress_generic_simpleArgs ( 1078 ZSTD_CCtx* cctx, 1079 void* dst, size_t dstCapacity, size_t* dstPos, 1080 const void* src, size_t srcSize, size_t* srcPos, 1081 ZSTD_EndDirective endOp); 1082</b><p> Same as ZSTD_compress_generic(), 1083 but using only integral types as arguments. 1084 Argument list is larger than ZSTD_{in,out}Buffer, 1085 but can be helpful for binders from dynamic languages 1086 which have troubles handling structures containing memory pointers. 1087 1088</p></pre><BR> 1089 1090<pre><b>ZSTD_CCtx_params* ZSTD_createCCtxParams(void); 1091size_t ZSTD_freeCCtxParams(ZSTD_CCtx_params* params); 1092</b><p> Quick howto : 1093 - ZSTD_createCCtxParams() : Create a ZSTD_CCtx_params structure 1094 - ZSTD_CCtxParam_setParameter() : Push parameters one by one into 1095 an existing ZSTD_CCtx_params structure. 1096 This is similar to 1097 ZSTD_CCtx_setParameter(). 1098 - ZSTD_CCtx_setParametersUsingCCtxParams() : Apply parameters to 1099 an existing CCtx. 1100 These parameters will be applied to 1101 all subsequent compression jobs. 1102 - ZSTD_compress_generic() : Do compression using the CCtx. 1103 - ZSTD_freeCCtxParams() : Free the memory. 1104 1105 This can be used with ZSTD_estimateCCtxSize_advanced_usingCCtxParams() 1106 for static allocation for single-threaded compression. 1107 1108</p></pre><BR> 1109 1110<pre><b>size_t ZSTD_CCtxParams_reset(ZSTD_CCtx_params* params); 1111</b><p> Reset params to default values. 1112 1113</p></pre><BR> 1114 1115<pre><b>size_t ZSTD_CCtxParams_init(ZSTD_CCtx_params* cctxParams, int compressionLevel); 1116</b><p> Initializes the compression parameters of cctxParams according to 1117 compression level. All other parameters are reset to their default values. 1118 1119</p></pre><BR> 1120 1121<pre><b>size_t ZSTD_CCtxParams_init_advanced(ZSTD_CCtx_params* cctxParams, ZSTD_parameters params); 1122</b><p> Initializes the compression and frame parameters of cctxParams according to 1123 params. All other parameters are reset to their default values. 1124 1125</p></pre><BR> 1126 1127<pre><b>size_t ZSTD_CCtxParam_setParameter(ZSTD_CCtx_params* params, ZSTD_cParameter param, unsigned value); 1128</b><p> Similar to ZSTD_CCtx_setParameter. 1129 Set one compression parameter, selected by enum ZSTD_cParameter. 1130 Parameters must be applied to a ZSTD_CCtx using ZSTD_CCtx_setParametersUsingCCtxParams(). 1131 Note : when `value` is an enum, cast it to unsigned for proper type checking. 1132 @result : 0, or an error code (which can be tested with ZSTD_isError()). 1133 1134</p></pre><BR> 1135 1136<pre><b>size_t ZSTD_CCtxParam_getParameter(ZSTD_CCtx_params* params, ZSTD_cParameter param, unsigned* value); 1137</b><p> Similar to ZSTD_CCtx_getParameter. 1138 Get the requested value of one compression parameter, selected by enum ZSTD_cParameter. 1139 @result : 0, or an error code (which can be tested with ZSTD_isError()). 1140 1141</p></pre><BR> 1142 1143<pre><b>size_t ZSTD_CCtx_setParametersUsingCCtxParams( 1144 ZSTD_CCtx* cctx, const ZSTD_CCtx_params* params); 1145</b><p> Apply a set of ZSTD_CCtx_params to the compression context. 1146 This can be done even after compression is started, 1147 if nbWorkers==0, this will have no impact until a new compression is started. 1148 if nbWorkers>=1, new parameters will be picked up at next job, 1149 with a few restrictions (windowLog, pledgedSrcSize, nbWorkers, jobSize, and overlapLog are not updated). 1150 1151</p></pre><BR> 1152 1153<h3>Advanced decompression API</h3><pre></pre><b><pre></b>/* ==================================== */<b> 1154</pre></b><BR> 1155<pre><b>size_t ZSTD_DCtx_loadDictionary(ZSTD_DCtx* dctx, const void* dict, size_t dictSize); 1156size_t ZSTD_DCtx_loadDictionary_byReference(ZSTD_DCtx* dctx, const void* dict, size_t dictSize); 1157size_t ZSTD_DCtx_loadDictionary_advanced(ZSTD_DCtx* dctx, const void* dict, size_t dictSize, ZSTD_dictLoadMethod_e dictLoadMethod, ZSTD_dictContentType_e dictContentType); 1158</b><p> Create an internal DDict from dict buffer, 1159 to be used to decompress next frames. 1160 @result : 0, or an error code (which can be tested with ZSTD_isError()). 1161 Special : Adding a NULL (or 0-size) dictionary invalidates any previous dictionary, 1162 meaning "return to no-dictionary mode". 1163 Note 1 : `dict` content will be copied internally. 1164 Use ZSTD_DCtx_loadDictionary_byReference() 1165 to reference dictionary content instead. 1166 In which case, the dictionary buffer must outlive its users. 1167 Note 2 : Loading a dictionary involves building tables, 1168 which has a non-negligible impact on CPU usage and latency. 1169 Note 3 : Use ZSTD_DCtx_loadDictionary_advanced() to select 1170 how dictionary content will be interpreted and loaded. 1171 1172</p></pre><BR> 1173 1174<pre><b>size_t ZSTD_DCtx_refDDict(ZSTD_DCtx* dctx, const ZSTD_DDict* ddict); 1175</b><p> Reference a prepared dictionary, to be used to decompress next frames. 1176 The dictionary remains active for decompression of future frames using same DCtx. 1177 @result : 0, or an error code (which can be tested with ZSTD_isError()). 1178 Note 1 : Currently, only one dictionary can be managed. 1179 Referencing a new dictionary effectively "discards" any previous one. 1180 Special : adding a NULL DDict means "return to no-dictionary mode". 1181 Note 2 : DDict is just referenced, its lifetime must outlive its usage from DCtx. 1182 1183</p></pre><BR> 1184 1185<pre><b>size_t ZSTD_DCtx_refPrefix(ZSTD_DCtx* dctx, 1186 const void* prefix, size_t prefixSize); 1187size_t ZSTD_DCtx_refPrefix_advanced(ZSTD_DCtx* dctx, 1188 const void* prefix, size_t prefixSize, 1189 ZSTD_dictContentType_e dictContentType); 1190</b><p> Reference a prefix (single-usage dictionary) for next compression job. 1191 This is the reverse operation of ZSTD_CCtx_refPrefix(), 1192 and must use the same prefix as the one used during compression. 1193 Prefix is **only used once**. Reference is discarded at end of frame. 1194 End of frame is reached when ZSTD_DCtx_decompress_generic() returns 0. 1195 @result : 0, or an error code (which can be tested with ZSTD_isError()). 1196 Note 1 : Adding any prefix (including NULL) invalidates any previously set prefix or dictionary 1197 Note 2 : Prefix buffer is referenced. It **must** outlive decompression job. 1198 Prefix buffer must remain unmodified up to the end of frame, 1199 reached when ZSTD_DCtx_decompress_generic() returns 0. 1200 Note 3 : By default, the prefix is treated as raw content (ZSTD_dm_rawContent). 1201 Use ZSTD_CCtx_refPrefix_advanced() to alter dictMode. 1202 Note 4 : Referencing a raw content prefix has almost no cpu nor memory cost. 1203 A fulldict prefix is more costly though. 1204 1205</p></pre><BR> 1206 1207<pre><b>size_t ZSTD_DCtx_setMaxWindowSize(ZSTD_DCtx* dctx, size_t maxWindowSize); 1208</b><p> Refuses allocating internal buffers for frames requiring a window size larger than provided limit. 1209 This is useful to prevent a decoder context from reserving too much memory for itself (potential attack scenario). 1210 This parameter is only useful in streaming mode, since no internal buffer is allocated in direct mode. 1211 By default, a decompression context accepts all window sizes <= (1 << ZSTD_WINDOWLOG_MAX) 1212 @return : 0, or an error code (which can be tested using ZSTD_isError()). 1213 1214</p></pre><BR> 1215 1216<pre><b>size_t ZSTD_DCtx_setFormat(ZSTD_DCtx* dctx, ZSTD_format_e format); 1217</b><p> Instruct the decoder context about what kind of data to decode next. 1218 This instruction is mandatory to decode data without a fully-formed header, 1219 such ZSTD_f_zstd1_magicless for example. 1220 @return : 0, or an error code (which can be tested using ZSTD_isError()). 1221 1222</p></pre><BR> 1223 1224<pre><b>size_t ZSTD_getFrameHeader_advanced(ZSTD_frameHeader* zfhPtr, 1225 const void* src, size_t srcSize, ZSTD_format_e format); 1226</b><p> same as ZSTD_getFrameHeader(), 1227 with added capability to select a format (like ZSTD_f_zstd1_magicless) 1228</p></pre><BR> 1229 1230<pre><b>size_t ZSTD_decompress_generic(ZSTD_DCtx* dctx, 1231 ZSTD_outBuffer* output, 1232 ZSTD_inBuffer* input); 1233</b><p> Behave the same as ZSTD_decompressStream. 1234 Decompression parameters cannot be changed once decompression is started. 1235 @return : an error code, which can be tested using ZSTD_isError() 1236 if >0, a hint, nb of expected input bytes for next invocation. 1237 `0` means : a frame has just been fully decoded and flushed. 1238 1239</p></pre><BR> 1240 1241<pre><b>size_t ZSTD_decompress_generic_simpleArgs ( 1242 ZSTD_DCtx* dctx, 1243 void* dst, size_t dstCapacity, size_t* dstPos, 1244 const void* src, size_t srcSize, size_t* srcPos); 1245</b><p> Same as ZSTD_decompress_generic(), 1246 but using only integral types as arguments. 1247 Argument list is larger than ZSTD_{in,out}Buffer, 1248 but can be helpful for binders from dynamic languages 1249 which have troubles handling structures containing memory pointers. 1250 1251</p></pre><BR> 1252 1253<pre><b>void ZSTD_DCtx_reset(ZSTD_DCtx* dctx); 1254</b><p> Return a DCtx to clean state. 1255 If a decompression was ongoing, any internal data not yet flushed is cancelled. 1256 All parameters are back to default values, including sticky ones. 1257 Dictionary (if any) is dropped. 1258 Parameters can be modified again after a reset. 1259 1260</p></pre><BR> 1261 1262<a name="Chapter21"></a><h2>Block level API</h2><pre></pre> 1263 1264<pre><b></b><p> Frame metadata cost is typically ~18 bytes, which can be non-negligible for very small blocks (< 100 bytes). 1265 User will have to take in charge required information to regenerate data, such as compressed and content sizes. 1266 1267 A few rules to respect : 1268 - Compressing and decompressing require a context structure 1269 + Use ZSTD_createCCtx() and ZSTD_createDCtx() 1270 - It is necessary to init context before starting 1271 + compression : any ZSTD_compressBegin*() variant, including with dictionary 1272 + decompression : any ZSTD_decompressBegin*() variant, including with dictionary 1273 + copyCCtx() and copyDCtx() can be used too 1274 - Block size is limited, it must be <= ZSTD_getBlockSize() <= ZSTD_BLOCKSIZE_MAX == 128 KB 1275 + If input is larger than a block size, it's necessary to split input data into multiple blocks 1276 + For inputs larger than a single block size, consider using the regular ZSTD_compress() instead. 1277 Frame metadata is not that costly, and quickly becomes negligible as source size grows larger. 1278 - When a block is considered not compressible enough, ZSTD_compressBlock() result will be zero. 1279 In which case, nothing is produced into `dst`. 1280 + User must test for such outcome and deal directly with uncompressed data 1281 + ZSTD_decompressBlock() doesn't accept uncompressed data as input !!! 1282 + In case of multiple successive blocks, should some of them be uncompressed, 1283 decoder must be informed of their existence in order to follow proper history. 1284 Use ZSTD_insertBlock() for such a case. 1285</p></pre><BR> 1286 1287<h3>Raw zstd block functions</h3><pre></pre><b><pre>size_t ZSTD_getBlockSize (const ZSTD_CCtx* cctx); 1288size_t ZSTD_compressBlock (ZSTD_CCtx* cctx, void* dst, size_t dstCapacity, const void* src, size_t srcSize); 1289size_t ZSTD_decompressBlock(ZSTD_DCtx* dctx, void* dst, size_t dstCapacity, const void* src, size_t srcSize); 1290size_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> 1291</pre></b><BR> 1292</html> 1293</body> 1294