blob: 1badcbd793face44c347b365f9ffe3c2bc975495 [file] [log] [blame]
Przemyslaw Skibinski86d94242016-10-24 16:07:53 +02001<html>
2<head>
3<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
Przemyslaw Skibinski4da53212016-12-07 11:18:40 +01004<title>zstd 1.1.2 Manual</title>
Przemyslaw Skibinski86d94242016-10-24 16:07:53 +02005</head>
6<body>
Przemyslaw Skibinski4da53212016-12-07 11:18:40 +01007<h1>zstd 1.1.2 Manual</h1>
Przemyslaw Skibinski86d94242016-10-24 16:07:53 +02008<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">Simple API</a></li>
14<li><a href="#Chapter4">Explicit memory management</a></li>
15<li><a href="#Chapter5">Simple dictionary API</a></li>
16<li><a href="#Chapter6">Fast dictionary API</a></li>
17<li><a href="#Chapter7">Streaming</a></li>
18<li><a href="#Chapter8">Streaming compression - HowTo</a></li>
19<li><a href="#Chapter9">Streaming decompression - HowTo</a></li>
20<li><a href="#Chapter10">START OF ADVANCED AND EXPERIMENTAL FUNCTIONS</a></li>
21<li><a href="#Chapter11">Advanced types</a></li>
22<li><a href="#Chapter12">Advanced compression functions</a></li>
23<li><a href="#Chapter13">Advanced decompression functions</a></li>
24<li><a href="#Chapter14">Advanced streaming functions</a></li>
25<li><a href="#Chapter15">Buffer-less and synchronous inner streaming functions</a></li>
26<li><a href="#Chapter16">Buffer-less streaming compression (synchronous mode)</a></li>
27<li><a href="#Chapter17">Buffer-less streaming decompression (synchronous mode)</a></li>
28<li><a href="#Chapter18">Block functions</a></li>
29</ol>
30<hr>
31<a name="Chapter1"></a><h2>Introduction</h2><pre>
Przemyslaw Skibinski1fd5b452016-10-31 10:44:44 +010032 zstd, short for Zstandard, is a fast lossless compression algorithm, targeting real-time compression scenarios
Przemyslaw Skibinski86d94242016-10-24 16:07:53 +020033 at zlib-level and better compression ratios. The zstd compression library provides in-memory compression and
34 decompression functions. The library supports compression levels from 1 up to ZSTD_maxCLevel() which is 22.
Przemyslaw Skibinski1fd5b452016-10-31 10:44:44 +010035 Levels >= 20, labelled `--ultra`, should be used with caution, as they require more memory.
Przemyslaw Skibinski86d94242016-10-24 16:07:53 +020036 Compression can be done in:
37 - a single step (described as Simple API)
38 - a single step, reusing a context (described as Explicit memory management)
Przemyslaw Skibinski1fd5b452016-10-31 10:44:44 +010039 - unbounded multiple steps (described as Streaming compression)
Przemyslaw Skibinski86d94242016-10-24 16:07:53 +020040 The compression ratio achievable on small data can be highly improved using compression with a dictionary in:
41 - a single step (described as Simple dictionary API)
42 - a single step, reusing a dictionary (described as Fast dictionary API)
43
Przemyslaw Skibinski1fd5b452016-10-31 10:44:44 +010044 Advanced experimental functions can be accessed using #define ZSTD_STATIC_LINKING_ONLY before including zstd.h.
45 These APIs shall never be used with a dynamic library.
Przemyslaw Skibinski86d94242016-10-24 16:07:53 +020046 They are not "stable", their definition may change in the future. Only static linking is allowed.
47<BR></pre>
48
49<a name="Chapter2"></a><h2>Version</h2><pre></pre>
50
Przemyslaw Skibinski4da53212016-12-07 11:18:40 +010051<pre><b>unsigned ZSTD_versionNumber(void); </b>/**< library version number; to be used when checking dll version */<b>
Przemyslaw Skibinski86d94242016-10-24 16:07:53 +020052</b></pre><BR>
53<a name="Chapter3"></a><h2>Simple API</h2><pre></pre>
54
55<pre><b>size_t ZSTD_compress( void* dst, size_t dstCapacity,
56 const void* src, size_t srcSize,
57 int compressionLevel);
58</b><p> Compresses `src` content as a single zstd compressed frame into already allocated `dst`.
59 Hint : compression runs faster if `dstCapacity` >= `ZSTD_compressBound(srcSize)`.
60 @return : compressed size written into `dst` (<= `dstCapacity),
Nick Terrelld82efd82016-11-02 16:47:53 -070061 or an error code if it fails (which can be tested using ZSTD_isError()).
Przemyslaw Skibinski86d94242016-10-24 16:07:53 +020062</p></pre><BR>
63
64<pre><b>size_t ZSTD_decompress( void* dst, size_t dstCapacity,
65 const void* src, size_t compressedSize);
66</b><p> `compressedSize` : must be the _exact_ size of a single compressed frame.
67 `dstCapacity` is an upper bound of originalSize.
68 If user cannot imply a maximum upper bound, it's better to use streaming mode to decompress data.
69 @return : the number of bytes decompressed into `dst` (<= `dstCapacity`),
Nick Terrelld82efd82016-11-02 16:47:53 -070070 or an errorCode if it fails (which can be tested using ZSTD_isError()).
Przemyslaw Skibinski86d94242016-10-24 16:07:53 +020071</p></pre><BR>
72
73<pre><b>unsigned long long ZSTD_getDecompressedSize(const void* src, size_t srcSize);
74</b><p> 'src' is the start of a zstd compressed frame.
75 @return : content size to be decompressed, as a 64-bits value _if known_, 0 otherwise.
76 note 1 : decompressed size is an optional field, that may not be present, especially in streaming mode.
77 When `return==0`, data to decompress could be any size.
78 In which case, it's necessary to use streaming mode to decompress data.
79 Optionally, application can still use ZSTD_decompress() while relying on implied limits.
80 (For example, data may be necessarily cut into blocks <= 16 KB).
81 note 2 : decompressed size is always present when compression is done with ZSTD_compress()
82 note 3 : decompressed size can be very large (64-bits value),
83 potentially larger than what local system can handle as a single memory segment.
84 In which case, it's necessary to use streaming mode to decompress data.
85 note 4 : If source is untrusted, decompressed size could be wrong or intentionally modified.
86 Always ensure result fits within application's authorized limits.
87 Each application can set its own limits.
88 note 5 : when `return==0`, if precise failure cause is needed, use ZSTD_getFrameParams() to know more.
89</p></pre><BR>
90
Yann Colletdc993122016-12-14 14:53:47 +010091<h3>Helper functions</h3><pre><b>int ZSTD_maxCLevel(void); </b>/*!< maximum compression level available */<b>
Przemyslaw Skibinski86d94242016-10-24 16:07:53 +020092size_t ZSTD_compressBound(size_t srcSize); </b>/*!< maximum compressed size in worst case scenario */<b>
93unsigned ZSTD_isError(size_t code); </b>/*!< tells if a `size_t` function result is an error code */<b>
94const char* ZSTD_getErrorName(size_t code); </b>/*!< provides readable string from an error code */<b>
Yann Colletdc993122016-12-14 14:53:47 +010095</b></pre><BR>
Przemyslaw Skibinski86d94242016-10-24 16:07:53 +020096<a name="Chapter4"></a><h2>Explicit memory management</h2><pre></pre>
97
Przemyslaw Skibinski86d94242016-10-24 16:07:53 +020098<pre><b>size_t ZSTD_compressCCtx(ZSTD_CCtx* ctx, void* dst, size_t dstCapacity, const void* src, size_t srcSize, int compressionLevel);
Nick Terrelld82efd82016-11-02 16:47:53 -070099</b><p> Same as ZSTD_compress(), requires an allocated ZSTD_CCtx (see ZSTD_createCCtx()).
Przemyslaw Skibinski86d94242016-10-24 16:07:53 +0200100</p></pre><BR>
101
Yann Colletdc993122016-12-14 14:53:47 +0100102<h3>Decompression context</h3><pre><b>typedef struct ZSTD_DCtx_s ZSTD_DCtx;
Przemyslaw Skibinski86d94242016-10-24 16:07:53 +0200103ZSTD_DCtx* ZSTD_createDCtx(void);
104size_t ZSTD_freeDCtx(ZSTD_DCtx* dctx);
Yann Colletdc993122016-12-14 14:53:47 +0100105</b></pre><BR>
Przemyslaw Skibinski86d94242016-10-24 16:07:53 +0200106<pre><b>size_t ZSTD_decompressDCtx(ZSTD_DCtx* ctx, void* dst, size_t dstCapacity, const void* src, size_t srcSize);
Nick Terrelld82efd82016-11-02 16:47:53 -0700107</b><p> Same as ZSTD_decompress(), requires an allocated ZSTD_DCtx (see ZSTD_createDCtx()).
Przemyslaw Skibinski86d94242016-10-24 16:07:53 +0200108</p></pre><BR>
109
110<a name="Chapter5"></a><h2>Simple dictionary API</h2><pre></pre>
111
112<pre><b>size_t ZSTD_compress_usingDict(ZSTD_CCtx* ctx,
113 void* dst, size_t dstCapacity,
114 const void* src, size_t srcSize,
115 const void* dict,size_t dictSize,
116 int compressionLevel);
117</b><p> Compression using a predefined Dictionary (see dictBuilder/zdict.h).
Nick Terrelld82efd82016-11-02 16:47:53 -0700118 Note : This function loads the dictionary, resulting in significant startup delay.
119 Note : When `dict == NULL || dictSize < 8` no dictionary is used.
Przemyslaw Skibinski86d94242016-10-24 16:07:53 +0200120</p></pre><BR>
121
122<pre><b>size_t ZSTD_decompress_usingDict(ZSTD_DCtx* dctx,
123 void* dst, size_t dstCapacity,
124 const void* src, size_t srcSize,
125 const void* dict,size_t dictSize);
126</b><p> Decompression using a predefined Dictionary (see dictBuilder/zdict.h).
127 Dictionary must be identical to the one used during compression.
Nick Terrelld82efd82016-11-02 16:47:53 -0700128 Note : This function loads the dictionary, resulting in significant startup delay.
129 Note : When `dict == NULL || dictSize < 8` no dictionary is used.
Przemyslaw Skibinski86d94242016-10-24 16:07:53 +0200130</p></pre><BR>
131
132<a name="Chapter6"></a><h2>Fast dictionary API</h2><pre></pre>
133
134<pre><b>ZSTD_CDict* ZSTD_createCDict(const void* dict, size_t dictSize, int compressionLevel);
Przemyslaw Skibinski1fd5b452016-10-31 10:44:44 +0100135</b><p> When compressing multiple messages / blocks with the same dictionary, it's recommended to load it just once.
136 ZSTD_createCDict() will create a digested dictionary, ready to start future compression operations without startup delay.
137 ZSTD_CDict can be created once and used by multiple threads concurrently, as its usage is read-only.
Nick Terrelld82efd82016-11-02 16:47:53 -0700138 `dict` can be released after ZSTD_CDict creation.
Przemyslaw Skibinski86d94242016-10-24 16:07:53 +0200139</p></pre><BR>
140
141<pre><b>size_t ZSTD_freeCDict(ZSTD_CDict* CDict);
Nick Terrelld82efd82016-11-02 16:47:53 -0700142</b><p> Function frees memory allocated by ZSTD_createCDict().
Przemyslaw Skibinski86d94242016-10-24 16:07:53 +0200143</p></pre><BR>
144
145<pre><b>size_t ZSTD_compress_usingCDict(ZSTD_CCtx* cctx,
146 void* dst, size_t dstCapacity,
147 const void* src, size_t srcSize,
148 const ZSTD_CDict* cdict);
149</b><p> Compression using a digested Dictionary.
150 Faster startup than ZSTD_compress_usingDict(), recommended when same dictionary is used multiple times.
Nick Terrelld82efd82016-11-02 16:47:53 -0700151 Note that compression level is decided during dictionary creation.
Przemyslaw Skibinski86d94242016-10-24 16:07:53 +0200152</p></pre><BR>
153
154<pre><b>ZSTD_DDict* ZSTD_createDDict(const void* dict, size_t dictSize);
155</b><p> Create a digested dictionary, ready to start decompression operation without startup delay.
Nick Terrelld82efd82016-11-02 16:47:53 -0700156 `dict` can be released after creation.
Przemyslaw Skibinski86d94242016-10-24 16:07:53 +0200157</p></pre><BR>
158
159<pre><b>size_t ZSTD_freeDDict(ZSTD_DDict* ddict);
160</b><p> Function frees memory allocated with ZSTD_createDDict()
161</p></pre><BR>
162
163<pre><b>size_t ZSTD_decompress_usingDDict(ZSTD_DCtx* dctx,
164 void* dst, size_t dstCapacity,
165 const void* src, size_t srcSize,
166 const ZSTD_DDict* ddict);
Nick Terrelld82efd82016-11-02 16:47:53 -0700167</b><p> Decompression using a digested Dictionary.
Przemyslaw Skibinski86d94242016-10-24 16:07:53 +0200168 Faster startup than ZSTD_decompress_usingDict(), recommended when same dictionary is used multiple times.
169</p></pre><BR>
170
171<a name="Chapter7"></a><h2>Streaming</h2><pre></pre>
172
173<pre><b>typedef struct ZSTD_inBuffer_s {
174 const void* src; </b>/**< start of input buffer */<b>
175 size_t size; </b>/**< size of input buffer */<b>
176 size_t pos; </b>/**< position where reading stopped. Will be updated. Necessarily 0 <= pos <= size */<b>
177} ZSTD_inBuffer;
178</b></pre><BR>
179<pre><b>typedef struct ZSTD_outBuffer_s {
180 void* dst; </b>/**< start of output buffer */<b>
181 size_t size; </b>/**< size of output buffer */<b>
182 size_t pos; </b>/**< position where writing stopped. Will be updated. Necessarily 0 <= pos <= size */<b>
183} ZSTD_outBuffer;
184</b></pre><BR>
185<a name="Chapter8"></a><h2>Streaming compression - HowTo</h2><pre>
186 A ZSTD_CStream object is required to track streaming operation.
187 Use ZSTD_createCStream() and ZSTD_freeCStream() to create/release resources.
188 ZSTD_CStream objects can be reused multiple times on consecutive compression operations.
Przemyslaw Skibinski1fd5b452016-10-31 10:44:44 +0100189 It is recommended to re-use ZSTD_CStream in situations where many streaming operations will be achieved consecutively,
190 since it will play nicer with system's memory, by re-using already allocated memory.
191 Use one separate ZSTD_CStream per thread for parallel execution.
Przemyslaw Skibinski86d94242016-10-24 16:07:53 +0200192
Przemyslaw Skibinski1fd5b452016-10-31 10:44:44 +0100193 Start a new compression by initializing ZSTD_CStream.
Przemyslaw Skibinski86d94242016-10-24 16:07:53 +0200194 Use ZSTD_initCStream() to start a new compression operation.
Yann Colletdc993122016-12-14 14:53:47 +0100195 Use ZSTD_initCStream_usingDict() or ZSTD_initCStream_usingCDict() for a compression which requires a dictionary (experimental section)
Przemyslaw Skibinski86d94242016-10-24 16:07:53 +0200196
197 Use ZSTD_compressStream() repetitively to consume input stream.
198 The function will automatically update both `pos` fields.
199 Note that it may not consume the entire input, in which case `pos < size`,
200 and it's up to the caller to present again remaining data.
201 @return : a size hint, preferred nb of bytes to use as input for next function call
Przemyslaw Skibinski86d94242016-10-24 16:07:53 +0200202 or an error code, which can be tested using ZSTD_isError().
Yann Colletdc993122016-12-14 14:53:47 +0100203 Note 1 : it's just a hint, to help latency a little, any other value will work fine.
204 Note 2 : size hint is guaranteed to be <= ZSTD_CStreamInSize()
Przemyslaw Skibinski86d94242016-10-24 16:07:53 +0200205
Yann Colletdc993122016-12-14 14:53:47 +0100206 At any moment, it's possible to flush whatever data remains within internal buffer, using ZSTD_flushStream().
Przemyslaw Skibinski86d94242016-10-24 16:07:53 +0200207 `output->pos` will be updated.
Yann Colletdc993122016-12-14 14:53:47 +0100208 Note that some content might still be left within internal buffer if `output->size` is too small.
Przemyslaw Skibinski86d94242016-10-24 16:07:53 +0200209 @return : nb of bytes still present within internal buffer (0 if it's empty)
210 or an error code, which can be tested using ZSTD_isError().
211
212 ZSTD_endStream() instructs to finish a frame.
213 It will perform a flush and write frame epilogue.
214 The epilogue is required for decoders to consider a frame completed.
215 Similar to ZSTD_flushStream(), it may not be able to flush the full content if `output->size` is too small.
216 In which case, call again ZSTD_endStream() to complete the flush.
Yann Colletdc993122016-12-14 14:53:47 +0100217 @return : nb of bytes still present within internal buffer (0 if it's empty, hence compression completed)
Przemyslaw Skibinski86d94242016-10-24 16:07:53 +0200218 or an error code, which can be tested using ZSTD_isError().
219
220
221<BR></pre>
222
Przemyslaw Skibinski86d94242016-10-24 16:07:53 +0200223<pre><b>size_t ZSTD_CStreamInSize(void); </b>/**< recommended size for input buffer */<b>
224</b></pre><BR>
225<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>
226</b></pre><BR>
227<a name="Chapter9"></a><h2>Streaming decompression - HowTo</h2><pre>
228 A ZSTD_DStream object is required to track streaming operations.
229 Use ZSTD_createDStream() and ZSTD_freeDStream() to create/release resources.
230 ZSTD_DStream objects can be re-used multiple times.
231
232 Use ZSTD_initDStream() to start a new decompression operation,
233 or ZSTD_initDStream_usingDict() if decompression requires a dictionary.
234 @return : recommended first input size
235
236 Use ZSTD_decompressStream() repetitively to consume your input.
237 The function will update both `pos` fields.
238 If `input.pos < input.size`, some input has not been consumed.
239 It's up to the caller to present again remaining data.
240 If `output.pos < output.size`, decoder has flushed everything it could.
241 @return : 0 when a frame is completely decoded and fully flushed,
242 an error code, which can be tested using ZSTD_isError(),
Przemyslaw Skibinski4da53212016-12-07 11:18:40 +0100243 any other value > 0, which means there is still some decoding to do to complete current frame.
244 The return value is a suggested next input size (a hint to improve latency) that will never load more than the current frame.
Przemyslaw Skibinski86d94242016-10-24 16:07:53 +0200245
246<BR></pre>
247
Przemyslaw Skibinski86d94242016-10-24 16:07:53 +0200248<pre><b>size_t ZSTD_DStreamInSize(void); </b>/*!< recommended size for input buffer */<b>
249</b></pre><BR>
250<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>
251</b></pre><BR>
252<a name="Chapter10"></a><h2>START OF ADVANCED AND EXPERIMENTAL FUNCTIONS</h2><pre> The definitions in this section are considered experimental.
253 They should never be used with a dynamic library, as they may change in the future.
254 They are provided for advanced usages.
255 Use them only in association with static linking.
256
257<BR></pre>
258
259<a name="Chapter11"></a><h2>Advanced types</h2><pre></pre>
260
Przemyslaw Skibinski1fd5b452016-10-31 10:44:44 +0100261<pre><b>typedef enum { ZSTD_fast, ZSTD_dfast, ZSTD_greedy, ZSTD_lazy, ZSTD_lazy2, ZSTD_btlazy2, ZSTD_btopt, ZSTD_btopt2 } ZSTD_strategy; </b>/* from faster to stronger */<b>
Przemyslaw Skibinski86d94242016-10-24 16:07:53 +0200262</b></pre><BR>
263<pre><b>typedef struct {
264 unsigned windowLog; </b>/**< largest match distance : larger == more compression, more memory needed during decompression */<b>
265 unsigned chainLog; </b>/**< fully searched segment : larger == more compression, slower, more memory (useless for fast) */<b>
266 unsigned hashLog; </b>/**< dispatch table : larger == faster, more memory */<b>
267 unsigned searchLog; </b>/**< nb of searches : larger == more compression, slower */<b>
268 unsigned searchLength; </b>/**< match length searched : larger == faster decompression, sometimes less compression */<b>
269 unsigned targetLength; </b>/**< acceptable match size for optimal parser (only) : larger == more compression, slower */<b>
270 ZSTD_strategy strategy;
271} ZSTD_compressionParameters;
272</b></pre><BR>
273<pre><b>typedef struct {
274 unsigned contentSizeFlag; </b>/**< 1: content size will be in frame header (if known). */<b>
275 unsigned checksumFlag; </b>/**< 1: will generate a 22-bits checksum at end of frame, to be used for error detection by decompressor */<b>
276 unsigned noDictIDFlag; </b>/**< 1: no dict ID will be saved into frame header (if dictionary compression) */<b>
277} ZSTD_frameParameters;
278</b></pre><BR>
279<pre><b>typedef struct {
280 ZSTD_compressionParameters cParams;
281 ZSTD_frameParameters fParams;
282} ZSTD_parameters;
283</b></pre><BR>
Yann Colletdc993122016-12-14 14:53:47 +0100284<h3>Custom memory allocation functions</h3><pre><b>typedef void* (*ZSTD_allocFunction) (void* opaque, size_t size);
Przemyslaw Skibinski86d94242016-10-24 16:07:53 +0200285typedef void (*ZSTD_freeFunction) (void* opaque, void* address);
286typedef struct { ZSTD_allocFunction customAlloc; ZSTD_freeFunction customFree; void* opaque; } ZSTD_customMem;
Yann Colletdc993122016-12-14 14:53:47 +0100287</b></pre><BR>
Przemyslaw Skibinski86d94242016-10-24 16:07:53 +0200288<a name="Chapter12"></a><h2>Advanced compression functions</h2><pre></pre>
289
290<pre><b>size_t ZSTD_estimateCCtxSize(ZSTD_compressionParameters cParams);
291</b><p> Gives the amount of memory allocated for a ZSTD_CCtx given a set of compression parameters.
292 `frameContentSize` is an optional parameter, provide `0` if unknown
293</p></pre><BR>
294
295<pre><b>ZSTD_CCtx* ZSTD_createCCtx_advanced(ZSTD_customMem customMem);
296</b><p> Create a ZSTD compression context using external alloc and free functions
297</p></pre><BR>
298
299<pre><b>size_t ZSTD_sizeof_CCtx(const ZSTD_CCtx* cctx);
300</b><p> Gives the amount of memory used by a given ZSTD_CCtx
301</p></pre><BR>
302
303<pre><b>ZSTD_CDict* ZSTD_createCDict_advanced(const void* dict, size_t dictSize,
304 ZSTD_parameters params, ZSTD_customMem customMem);
305</b><p> Create a ZSTD_CDict using external alloc and free, and customized compression parameters
306</p></pre><BR>
307
308<pre><b>size_t ZSTD_sizeof_CDict(const ZSTD_CDict* cdict);
309</b><p> Gives the amount of memory used by a given ZSTD_sizeof_CDict
310</p></pre><BR>
311
Yann Colletdc993122016-12-14 14:53:47 +0100312<pre><b>ZSTD_compressionParameters ZSTD_getCParams(int compressionLevel, unsigned long long estimatedSrcSize, size_t dictSize);
313</b><p> @return ZSTD_compressionParameters structure for a selected compression level and estimated srcSize.
314 `estimatedSrcSize` value is optional, select 0 if not known
Przemyslaw Skibinski86d94242016-10-24 16:07:53 +0200315</p></pre><BR>
316
Yann Colletdc993122016-12-14 14:53:47 +0100317<pre><b>ZSTD_parameters ZSTD_getParams(int compressionLevel, unsigned long long estimatedSrcSize, size_t dictSize);
318</b><p> same as ZSTD_getCParams(), but @return a full `ZSTD_parameters` object instead of sub-component `ZSTD_compressionParameters`.
319 All fields of `ZSTD_frameParameters` are set to default (0)
Przemyslaw Skibinski86d94242016-10-24 16:07:53 +0200320</p></pre><BR>
321
322<pre><b>size_t ZSTD_checkCParams(ZSTD_compressionParameters params);
323</b><p> Ensure param values remain within authorized range
324</p></pre><BR>
325
326<pre><b>ZSTD_compressionParameters ZSTD_adjustCParams(ZSTD_compressionParameters cPar, unsigned long long srcSize, size_t dictSize);
327</b><p> optimize params for a given `srcSize` and `dictSize`.
328 both values are optional, select `0` if unknown.
329</p></pre><BR>
330
331<pre><b>size_t ZSTD_compress_advanced (ZSTD_CCtx* ctx,
332 void* dst, size_t dstCapacity,
333 const void* src, size_t srcSize,
334 const void* dict,size_t dictSize,
335 ZSTD_parameters params);
336</b><p> Same as ZSTD_compress_usingDict(), with fine-tune control of each compression parameter
337</p></pre><BR>
338
339<a name="Chapter13"></a><h2>Advanced decompression functions</h2><pre></pre>
340
Przemyslaw Skibinski4da53212016-12-07 11:18:40 +0100341<pre><b>unsigned ZSTD_isFrame(const void* buffer, size_t size);
342</b><p> Tells if the content of `buffer` starts with a valid Frame Identifier.
343 Note : Frame Identifier is 4 bytes. If `size < 4`, @return will always be 0.
344 Note 2 : Legacy Frame Identifiers are considered valid only if Legacy Support is enabled.
345 Note 3 : Skippable Frame Identifiers are considered valid.
346</p></pre><BR>
347
Przemyslaw Skibinski86d94242016-10-24 16:07:53 +0200348<pre><b>size_t ZSTD_estimateDCtxSize(void);
349</b><p> Gives the potential amount of memory allocated to create a ZSTD_DCtx
350</p></pre><BR>
351
352<pre><b>ZSTD_DCtx* ZSTD_createDCtx_advanced(ZSTD_customMem customMem);
353</b><p> Create a ZSTD decompression context using external alloc and free functions
354</p></pre><BR>
355
356<pre><b>size_t ZSTD_sizeof_DCtx(const ZSTD_DCtx* dctx);
357</b><p> Gives the amount of memory used by a given ZSTD_DCtx
358</p></pre><BR>
359
360<pre><b>size_t ZSTD_sizeof_DDict(const ZSTD_DDict* ddict);
361</b><p> Gives the amount of memory used by a given ZSTD_DDict
362</p></pre><BR>
363
Przemyslaw Skibinski4da53212016-12-07 11:18:40 +0100364<pre><b>unsigned ZSTD_getDictID_fromDict(const void* dict, size_t dictSize);
365</b><p> Provides the dictID stored within dictionary.
366 if @return == 0, the dictionary is not conformant with Zstandard specification.
367 It can still be loaded, but as a content-only dictionary.
368</p></pre><BR>
369
370<pre><b>unsigned ZSTD_getDictID_fromDDict(const ZSTD_DDict* ddict);
371</b><p> Provides the dictID of the dictionary loaded into `ddict`.
372 If @return == 0, the dictionary is not conformant to Zstandard specification, or empty.
373 Non-conformant dictionaries can still be loaded, but as content-only dictionaries.
374</p></pre><BR>
375
376<pre><b>unsigned ZSTD_getDictID_fromFrame(const void* src, size_t srcSize);
377</b><p> Provides the dictID required to decompressed the frame stored within `src`.
378 If @return == 0, the dictID could not be decoded.
379 This could for one of the following reasons :
380 - The frame does not require a dictionary to be decoded (most common case).
381 - The frame was built with dictID intentionally removed. Whatever dictionary is necessary is a hidden information.
382 Note : this use case also happens when using a non-conformant dictionary.
383 - `srcSize` is too small, and as a result, the frame header could not be decoded (only possible if `srcSize < ZSTD_FRAMEHEADERSIZE_MAX`).
384 - This is not a Zstandard frame.
385 When identifying the exact failure cause, it's possible to used ZSTD_getFrameParams(), which will provide a more precise error code.
386</p></pre><BR>
387
Przemyslaw Skibinski86d94242016-10-24 16:07:53 +0200388<a name="Chapter14"></a><h2>Advanced streaming functions</h2><pre></pre>
389
Yann Colletdc993122016-12-14 14:53:47 +0100390<h3>Advanced Streaming compression functions</h3><pre><b>ZSTD_CStream* ZSTD_createCStream_advanced(ZSTD_customMem customMem);
391size_t ZSTD_initCStream_srcSize(ZSTD_CStream* zcs, int compressionLevel, unsigned long long pledgedSrcSize); </b>/**< pledgedSrcSize must be correct */<b>
Przemyslaw Skibinski86d94242016-10-24 16:07:53 +0200392size_t ZSTD_initCStream_usingDict(ZSTD_CStream* zcs, const void* dict, size_t dictSize, int compressionLevel);
393size_t ZSTD_initCStream_advanced(ZSTD_CStream* zcs, const void* dict, size_t dictSize,
394 ZSTD_parameters params, unsigned long long pledgedSrcSize); </b>/**< pledgedSrcSize is optional and can be zero == unknown */<b>
Przemyslaw Skibinski1fd5b452016-10-31 10:44:44 +0100395size_t ZSTD_initCStream_usingCDict(ZSTD_CStream* zcs, const ZSTD_CDict* cdict); </b>/**< note : cdict will just be referenced, and must outlive compression session */<b>
396size_t ZSTD_resetCStream(ZSTD_CStream* zcs, unsigned long long pledgedSrcSize); </b>/**< re-use compression parameters from previous init; skip dictionary loading stage; zcs must be init at least once before */<b>
Przemyslaw Skibinski86d94242016-10-24 16:07:53 +0200397size_t ZSTD_sizeof_CStream(const ZSTD_CStream* zcs);
Yann Colletdc993122016-12-14 14:53:47 +0100398</b></pre><BR>
399<h3>Advanced Streaming decompression functions</h3><pre><b>typedef enum { ZSTDdsp_maxWindowSize } ZSTD_DStreamParameter_e;
Przemyslaw Skibinski86d94242016-10-24 16:07:53 +0200400ZSTD_DStream* ZSTD_createDStream_advanced(ZSTD_customMem customMem);
401size_t ZSTD_initDStream_usingDict(ZSTD_DStream* zds, const void* dict, size_t dictSize);
402size_t ZSTD_setDStreamParameter(ZSTD_DStream* zds, ZSTD_DStreamParameter_e paramType, unsigned paramValue);
Przemyslaw Skibinski1fd5b452016-10-31 10:44:44 +0100403size_t ZSTD_initDStream_usingDDict(ZSTD_DStream* zds, const ZSTD_DDict* ddict); </b>/**< note : ddict will just be referenced, and must outlive decompression session */<b>
Przemyslaw Skibinski86d94242016-10-24 16:07:53 +0200404size_t ZSTD_resetDStream(ZSTD_DStream* zds); </b>/**< re-use decompression parameters from previous init; saves dictionary loading */<b>
405size_t ZSTD_sizeof_DStream(const ZSTD_DStream* zds);
Yann Colletdc993122016-12-14 14:53:47 +0100406</b></pre><BR>
Przemyslaw Skibinski86d94242016-10-24 16:07:53 +0200407<a name="Chapter15"></a><h2>Buffer-less and synchronous inner streaming functions</h2><pre>
408 This is an advanced API, giving full control over buffer management, for users which need direct control over memory.
409 But it's also a complex one, with many restrictions (documented below).
Przemyslaw Skibinski1fd5b452016-10-31 10:44:44 +0100410 Prefer using normal streaming API for an easier experience
Przemyslaw Skibinski86d94242016-10-24 16:07:53 +0200411
412<BR></pre>
413
414<a name="Chapter16"></a><h2>Buffer-less streaming compression (synchronous mode)</h2><pre>
415 A ZSTD_CCtx object is required to track streaming operations.
416 Use ZSTD_createCCtx() / ZSTD_freeCCtx() to manage resource.
417 ZSTD_CCtx object can be re-used multiple times within successive compression operations.
418
419 Start by initializing a context.
420 Use ZSTD_compressBegin(), or ZSTD_compressBegin_usingDict() for dictionary compression,
421 or ZSTD_compressBegin_advanced(), for finer parameter control.
422 It's also possible to duplicate a reference context which has already been initialized, using ZSTD_copyCCtx()
423
424 Then, consume your input using ZSTD_compressContinue().
425 There are some important considerations to keep in mind when using this advanced function :
426 - ZSTD_compressContinue() has no internal buffer. It uses externally provided buffer only.
427 - Interface is synchronous : input is consumed entirely and produce 1+ (or more) compressed blocks.
428 - Caller must ensure there is enough space in `dst` to store compressed data under worst case scenario.
429 Worst case evaluation is provided by ZSTD_compressBound().
430 ZSTD_compressContinue() doesn't guarantee recover after a failed compression.
431 - ZSTD_compressContinue() presumes prior input ***is still accessible and unmodified*** (up to maximum distance size, see WindowLog).
432 It remembers all previous contiguous blocks, plus one separated memory segment (which can itself consists of multiple contiguous blocks)
433 - ZSTD_compressContinue() detects that prior input has been overwritten when `src` buffer overlaps.
434 In which case, it will "discard" the relevant memory section from its history.
435
436 Finish a frame with ZSTD_compressEnd(), which will write the last block(s) and optional checksum.
437 It's possible to use a NULL,0 src content, in which case, it will write a final empty block to end the frame,
438 Without last block mark, frames will be considered unfinished (broken) by decoders.
439
440 You can then reuse `ZSTD_CCtx` (ZSTD_compressBegin()) to compress some new frame.
441<BR></pre>
442
Yann Colletdc993122016-12-14 14:53:47 +0100443<h3>Buffer-less streaming compression functions</h3><pre><b>size_t ZSTD_compressBegin(ZSTD_CCtx* cctx, int compressionLevel);
Przemyslaw Skibinski86d94242016-10-24 16:07:53 +0200444size_t ZSTD_compressBegin_usingDict(ZSTD_CCtx* cctx, const void* dict, size_t dictSize, int compressionLevel);
445size_t ZSTD_compressBegin_advanced(ZSTD_CCtx* cctx, const void* dict, size_t dictSize, ZSTD_parameters params, unsigned long long pledgedSrcSize);
446size_t ZSTD_copyCCtx(ZSTD_CCtx* cctx, const ZSTD_CCtx* preparedCCtx, unsigned long long pledgedSrcSize);
447size_t ZSTD_compressContinue(ZSTD_CCtx* cctx, void* dst, size_t dstCapacity, const void* src, size_t srcSize);
448size_t ZSTD_compressEnd(ZSTD_CCtx* cctx, void* dst, size_t dstCapacity, const void* src, size_t srcSize);
Yann Colletdc993122016-12-14 14:53:47 +0100449</b></pre><BR>
Przemyslaw Skibinski86d94242016-10-24 16:07:53 +0200450<a name="Chapter17"></a><h2>Buffer-less streaming decompression (synchronous mode)</h2><pre>
451 A ZSTD_DCtx object is required to track streaming operations.
452 Use ZSTD_createDCtx() / ZSTD_freeDCtx() to manage it.
453 A ZSTD_DCtx object can be re-used multiple times.
454
455 First typical operation is to retrieve frame parameters, using ZSTD_getFrameParams().
456 It fills a ZSTD_frameParams structure which provide important information to correctly decode the frame,
457 such as the minimum rolling buffer size to allocate to decompress data (`windowSize`),
458 and the dictionary ID used.
459 (Note : content size is optional, it may not be present. 0 means : content size unknown).
460 Note that these values could be wrong, either because of data malformation, or because an attacker is spoofing deliberate false information.
461 As a consequence, check that values remain within valid application range, especially `windowSize`, before allocation.
462 Each application can set its own limit, depending on local restrictions. For extended interoperability, it is recommended to support at least 8 MB.
463 Frame parameters are extracted from the beginning of the compressed frame.
464 Data fragment must be large enough to ensure successful decoding, typically `ZSTD_frameHeaderSize_max` bytes.
465 @result : 0 : successful decoding, the `ZSTD_frameParams` structure is correctly filled.
466 >0 : `srcSize` is too small, please provide at least @result bytes on next attempt.
467 errorCode, which can be tested using ZSTD_isError().
468
469 Start decompression, with ZSTD_decompressBegin() or ZSTD_decompressBegin_usingDict().
470 Alternatively, you can copy a prepared context, using ZSTD_copyDCtx().
471
472 Then use ZSTD_nextSrcSizeToDecompress() and ZSTD_decompressContinue() alternatively.
473 ZSTD_nextSrcSizeToDecompress() tells how many bytes to provide as 'srcSize' to ZSTD_decompressContinue().
474 ZSTD_decompressContinue() requires this _exact_ amount of bytes, or it will fail.
475
476 @result of ZSTD_decompressContinue() is the number of bytes regenerated within 'dst' (necessarily <= dstCapacity).
477 It can be zero, which is not an error; it just means ZSTD_decompressContinue() has decoded some metadata item.
478 It can also be an error code, which can be tested with ZSTD_isError().
479
480 ZSTD_decompressContinue() needs previous data blocks during decompression, up to `windowSize`.
481 They should preferably be located contiguously, prior to current block.
482 Alternatively, a round buffer of sufficient size is also possible. Sufficient size is determined by frame parameters.
483 ZSTD_decompressContinue() is very sensitive to contiguity,
484 if 2 blocks don't follow each other, make sure that either the compressor breaks contiguity at the same place,
485 or that previous contiguous segment is large enough to properly handle maximum back-reference.
486
487 A frame is fully decoded when ZSTD_nextSrcSizeToDecompress() returns zero.
488 Context can then be reset to start a new decompression.
489
490 Note : it's possible to know if next input to present is a header or a block, using ZSTD_nextInputType().
491 This information is not required to properly decode a frame.
492
Yann Colletdc993122016-12-14 14:53:47 +0100493 == Special case : skippable frames ==
Przemyslaw Skibinski86d94242016-10-24 16:07:53 +0200494
495 Skippable frames allow integration of user-defined data into a flow of concatenated frames.
496 Skippable frames will be ignored (skipped) by a decompressor. The format of skippable frames is as follows :
497 a) Skippable frame ID - 4 Bytes, Little endian format, any value from 0x184D2A50 to 0x184D2A5F
498 b) Frame Size - 4 Bytes, Little endian format, unsigned 32-bits
499 c) Frame Content - any content (User Data) of length equal to Frame Size
500 For skippable frames ZSTD_decompressContinue() always returns 0.
501 For skippable frames ZSTD_getFrameParams() returns fparamsPtr->windowLog==0 what means that a frame is skippable.
502 It also returns Frame Size as fparamsPtr->frameContentSize.
503<BR></pre>
504
505<pre><b>typedef struct {
506 unsigned long long frameContentSize;
507 unsigned windowSize;
508 unsigned dictID;
509 unsigned checksumFlag;
510} ZSTD_frameParams;
511</b></pre><BR>
Yann Colletdc993122016-12-14 14:53:47 +0100512<h3>Buffer-less streaming decompression functions</h3><pre><b>size_t ZSTD_getFrameParams(ZSTD_frameParams* fparamsPtr, const void* src, size_t srcSize); </b>/**< doesn't consume input, see details below */<b>
Przemyslaw Skibinski86d94242016-10-24 16:07:53 +0200513size_t ZSTD_decompressBegin(ZSTD_DCtx* dctx);
514size_t ZSTD_decompressBegin_usingDict(ZSTD_DCtx* dctx, const void* dict, size_t dictSize);
515void ZSTD_copyDCtx(ZSTD_DCtx* dctx, const ZSTD_DCtx* preparedDCtx);
516size_t ZSTD_nextSrcSizeToDecompress(ZSTD_DCtx* dctx);
517size_t ZSTD_decompressContinue(ZSTD_DCtx* dctx, void* dst, size_t dstCapacity, const void* src, size_t srcSize);
518typedef enum { ZSTDnit_frameHeader, ZSTDnit_blockHeader, ZSTDnit_block, ZSTDnit_lastBlock, ZSTDnit_checksum, ZSTDnit_skippableFrame } ZSTD_nextInputType_e;
519ZSTD_nextInputType_e ZSTD_nextInputType(ZSTD_DCtx* dctx);
Yann Colletdc993122016-12-14 14:53:47 +0100520</b></pre><BR>
Przemyslaw Skibinski86d94242016-10-24 16:07:53 +0200521<a name="Chapter18"></a><h2>Block functions</h2><pre>
522 Block functions produce and decode raw zstd blocks, without frame metadata.
523 Frame metadata cost is typically ~18 bytes, which can be non-negligible for very small blocks (< 100 bytes).
524 User will have to take in charge required information to regenerate data, such as compressed and content sizes.
525
526 A few rules to respect :
527 - Compressing and decompressing require a context structure
528 + Use ZSTD_createCCtx() and ZSTD_createDCtx()
529 - It is necessary to init context before starting
530 + compression : ZSTD_compressBegin()
531 + decompression : ZSTD_decompressBegin()
532 + variants _usingDict() are also allowed
533 + copyCCtx() and copyDCtx() work too
534 - Block size is limited, it must be <= ZSTD_getBlockSizeMax()
535 + If you need to compress more, cut data into multiple blocks
536 + Consider using the regular ZSTD_compress() instead, as frame metadata costs become negligible when source size is large.
537 - When a block is considered not compressible enough, ZSTD_compressBlock() result will be zero.
538 In which case, nothing is produced into `dst`.
539 + User must test for such outcome and deal directly with uncompressed data
540 + ZSTD_decompressBlock() doesn't accept uncompressed data as input !!!
541 + In case of multiple successive blocks, decoder must be informed of uncompressed block existence to follow proper history.
542 Use ZSTD_insertBlock() in such a case.
543<BR></pre>
544
Yann Colletdc993122016-12-14 14:53:47 +0100545<h3>Raw zstd block functions</h3><pre><b>size_t ZSTD_getBlockSizeMax(ZSTD_CCtx* cctx);
Przemyslaw Skibinski86d94242016-10-24 16:07:53 +0200546size_t ZSTD_compressBlock (ZSTD_CCtx* cctx, void* dst, size_t dstCapacity, const void* src, size_t srcSize);
547size_t ZSTD_decompressBlock(ZSTD_DCtx* dctx, void* dst, size_t dstCapacity, const void* src, size_t srcSize);
548size_t ZSTD_insertBlock(ZSTD_DCtx* dctx, const void* blockStart, size_t blockSize); </b>/**< insert block into `dctx` history. Useful for uncompressed blocks */<b>
Yann Colletdc993122016-12-14 14:53:47 +0100549</b></pre><BR>
Przemyslaw Skibinski86d94242016-10-24 16:07:53 +0200550</html>
551</body>