blob: 33ea13c329aa1fbdf642e15969927c1549f0d84e [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">
4<title>zstd 1.1.1 Manual</title>
5</head>
6<body>
7<h1>zstd 1.1.1 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">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
51<pre><b>unsigned ZSTD_versionNumber (void); </b>/**< returns version number of ZSTD */<b>
52</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
91<h3>Helper functions</h3><pre><b>int ZSTD_maxCLevel(void); </b>/*!< maximum compression level available */<b>
92size_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>
95</b></pre><BR>
96<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
102<h3>Decompression context</h3><pre><b>typedef struct ZSTD_DCtx_s ZSTD_DCtx;
103ZSTD_DCtx* ZSTD_createDCtx(void);
104size_t ZSTD_freeDCtx(ZSTD_DCtx* dctx);
105</b></pre><BR>
106<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.
195 Use ZSTD_initCStream_usingDict() for a compression which requires a dictionary.
196
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
202 (it's just a hint, to help latency a little, any other value will work fine)
203 (note : the size hint is guaranteed to be <= ZSTD_CStreamInSize() )
204 or an error code, which can be tested using ZSTD_isError().
205
206 At any moment, it's possible to flush whatever data remains within buffer, using ZSTD_flushStream().
207 `output->pos` will be updated.
208 Note some content might still be left within internal buffer if `output->size` is too small.
209 @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.
217 @return : nb of bytes still present within internal buffer (0 if it's empty)
218 or an error code, which can be tested using ZSTD_isError().
219
220
221<BR></pre>
222
223<h3>Streaming compression functions</h3><pre><b>typedef struct ZSTD_CStream_s ZSTD_CStream;
224ZSTD_CStream* ZSTD_createCStream(void);
225size_t ZSTD_freeCStream(ZSTD_CStream* zcs);
226size_t ZSTD_initCStream(ZSTD_CStream* zcs, int compressionLevel);
227size_t ZSTD_compressStream(ZSTD_CStream* zcs, ZSTD_outBuffer* output, ZSTD_inBuffer* input);
228size_t ZSTD_flushStream(ZSTD_CStream* zcs, ZSTD_outBuffer* output);
229size_t ZSTD_endStream(ZSTD_CStream* zcs, ZSTD_outBuffer* output);
230</b></pre><BR>
231<pre><b>size_t ZSTD_CStreamInSize(void); </b>/**< recommended size for input buffer */<b>
232</b></pre><BR>
233<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>
234</b></pre><BR>
235<a name="Chapter9"></a><h2>Streaming decompression - HowTo</h2><pre>
236 A ZSTD_DStream object is required to track streaming operations.
237 Use ZSTD_createDStream() and ZSTD_freeDStream() to create/release resources.
238 ZSTD_DStream objects can be re-used multiple times.
239
240 Use ZSTD_initDStream() to start a new decompression operation,
241 or ZSTD_initDStream_usingDict() if decompression requires a dictionary.
242 @return : recommended first input size
243
244 Use ZSTD_decompressStream() repetitively to consume your input.
245 The function will update both `pos` fields.
246 If `input.pos < input.size`, some input has not been consumed.
247 It's up to the caller to present again remaining data.
248 If `output.pos < output.size`, decoder has flushed everything it could.
249 @return : 0 when a frame is completely decoded and fully flushed,
250 an error code, which can be tested using ZSTD_isError(),
251 any other value > 0, which means there is still some work to do to complete the frame.
252 The return value is a suggested next input size (just an hint, to help latency).
253
254<BR></pre>
255
256<h3>Streaming decompression functions</h3><pre><b>typedef struct ZSTD_DStream_s ZSTD_DStream;
257ZSTD_DStream* ZSTD_createDStream(void);
258size_t ZSTD_freeDStream(ZSTD_DStream* zds);
259size_t ZSTD_initDStream(ZSTD_DStream* zds);
260size_t ZSTD_decompressStream(ZSTD_DStream* zds, ZSTD_outBuffer* output, ZSTD_inBuffer* input);
261</b></pre><BR>
262<pre><b>size_t ZSTD_DStreamInSize(void); </b>/*!< recommended size for input buffer */<b>
263</b></pre><BR>
264<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>
265</b></pre><BR>
266<a name="Chapter10"></a><h2>START OF ADVANCED AND EXPERIMENTAL FUNCTIONS</h2><pre> The definitions in this section are considered experimental.
267 They should never be used with a dynamic library, as they may change in the future.
268 They are provided for advanced usages.
269 Use them only in association with static linking.
270
271<BR></pre>
272
273<a name="Chapter11"></a><h2>Advanced types</h2><pre></pre>
274
Przemyslaw Skibinski1fd5b452016-10-31 10:44:44 +0100275<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 +0200276</b></pre><BR>
277<pre><b>typedef struct {
278 unsigned windowLog; </b>/**< largest match distance : larger == more compression, more memory needed during decompression */<b>
279 unsigned chainLog; </b>/**< fully searched segment : larger == more compression, slower, more memory (useless for fast) */<b>
280 unsigned hashLog; </b>/**< dispatch table : larger == faster, more memory */<b>
281 unsigned searchLog; </b>/**< nb of searches : larger == more compression, slower */<b>
282 unsigned searchLength; </b>/**< match length searched : larger == faster decompression, sometimes less compression */<b>
283 unsigned targetLength; </b>/**< acceptable match size for optimal parser (only) : larger == more compression, slower */<b>
284 ZSTD_strategy strategy;
285} ZSTD_compressionParameters;
286</b></pre><BR>
287<pre><b>typedef struct {
288 unsigned contentSizeFlag; </b>/**< 1: content size will be in frame header (if known). */<b>
289 unsigned checksumFlag; </b>/**< 1: will generate a 22-bits checksum at end of frame, to be used for error detection by decompressor */<b>
290 unsigned noDictIDFlag; </b>/**< 1: no dict ID will be saved into frame header (if dictionary compression) */<b>
291} ZSTD_frameParameters;
292</b></pre><BR>
293<pre><b>typedef struct {
294 ZSTD_compressionParameters cParams;
295 ZSTD_frameParameters fParams;
296} ZSTD_parameters;
297</b></pre><BR>
298<h3>Custom memory allocation functions</h3><pre><b>typedef void* (*ZSTD_allocFunction) (void* opaque, size_t size);
299typedef void (*ZSTD_freeFunction) (void* opaque, void* address);
300typedef struct { ZSTD_allocFunction customAlloc; ZSTD_freeFunction customFree; void* opaque; } ZSTD_customMem;
301</b></pre><BR>
302<a name="Chapter12"></a><h2>Advanced compression functions</h2><pre></pre>
303
304<pre><b>size_t ZSTD_estimateCCtxSize(ZSTD_compressionParameters cParams);
305</b><p> Gives the amount of memory allocated for a ZSTD_CCtx given a set of compression parameters.
306 `frameContentSize` is an optional parameter, provide `0` if unknown
307</p></pre><BR>
308
309<pre><b>ZSTD_CCtx* ZSTD_createCCtx_advanced(ZSTD_customMem customMem);
310</b><p> Create a ZSTD compression context using external alloc and free functions
311</p></pre><BR>
312
313<pre><b>size_t ZSTD_sizeof_CCtx(const ZSTD_CCtx* cctx);
314</b><p> Gives the amount of memory used by a given ZSTD_CCtx
315</p></pre><BR>
316
317<pre><b>ZSTD_CDict* ZSTD_createCDict_advanced(const void* dict, size_t dictSize,
318 ZSTD_parameters params, ZSTD_customMem customMem);
319</b><p> Create a ZSTD_CDict using external alloc and free, and customized compression parameters
320</p></pre><BR>
321
322<pre><b>size_t ZSTD_sizeof_CDict(const ZSTD_CDict* cdict);
323</b><p> Gives the amount of memory used by a given ZSTD_sizeof_CDict
324</p></pre><BR>
325
326<pre><b>ZSTD_parameters ZSTD_getParams(int compressionLevel, unsigned long long srcSize, size_t dictSize);
327</b><p> same as ZSTD_getCParams(), but @return a full `ZSTD_parameters` object instead of a `ZSTD_compressionParameters`.
328 All fields of `ZSTD_frameParameters` are set to default (0)
329</p></pre><BR>
330
331<pre><b>ZSTD_compressionParameters ZSTD_getCParams(int compressionLevel, unsigned long long srcSize, size_t dictSize);
332</b><p> @return ZSTD_compressionParameters structure for a selected compression level and srcSize.
333 `srcSize` value is optional, select 0 if not known
334</p></pre><BR>
335
336<pre><b>size_t ZSTD_checkCParams(ZSTD_compressionParameters params);
337</b><p> Ensure param values remain within authorized range
338</p></pre><BR>
339
340<pre><b>ZSTD_compressionParameters ZSTD_adjustCParams(ZSTD_compressionParameters cPar, unsigned long long srcSize, size_t dictSize);
341</b><p> optimize params for a given `srcSize` and `dictSize`.
342 both values are optional, select `0` if unknown.
343</p></pre><BR>
344
345<pre><b>size_t ZSTD_compress_advanced (ZSTD_CCtx* ctx,
346 void* dst, size_t dstCapacity,
347 const void* src, size_t srcSize,
348 const void* dict,size_t dictSize,
349 ZSTD_parameters params);
350</b><p> Same as ZSTD_compress_usingDict(), with fine-tune control of each compression parameter
351</p></pre><BR>
352
353<a name="Chapter13"></a><h2>Advanced decompression functions</h2><pre></pre>
354
355<pre><b>size_t ZSTD_estimateDCtxSize(void);
356</b><p> Gives the potential amount of memory allocated to create a ZSTD_DCtx
357</p></pre><BR>
358
359<pre><b>ZSTD_DCtx* ZSTD_createDCtx_advanced(ZSTD_customMem customMem);
360</b><p> Create a ZSTD decompression context using external alloc and free functions
361</p></pre><BR>
362
363<pre><b>size_t ZSTD_sizeof_DCtx(const ZSTD_DCtx* dctx);
364</b><p> Gives the amount of memory used by a given ZSTD_DCtx
365</p></pre><BR>
366
367<pre><b>size_t ZSTD_sizeof_DDict(const ZSTD_DDict* ddict);
368</b><p> Gives the amount of memory used by a given ZSTD_DDict
369</p></pre><BR>
370
371<a name="Chapter14"></a><h2>Advanced streaming functions</h2><pre></pre>
372
373<h3>Advanced Streaming compression functions</h3><pre><b>ZSTD_CStream* ZSTD_createCStream_advanced(ZSTD_customMem customMem);
374size_t ZSTD_initCStream_usingDict(ZSTD_CStream* zcs, const void* dict, size_t dictSize, int compressionLevel);
375size_t ZSTD_initCStream_advanced(ZSTD_CStream* zcs, const void* dict, size_t dictSize,
376 ZSTD_parameters params, unsigned long long pledgedSrcSize); </b>/**< pledgedSrcSize is optional and can be zero == unknown */<b>
Przemyslaw Skibinski1fd5b452016-10-31 10:44:44 +0100377size_t ZSTD_initCStream_usingCDict(ZSTD_CStream* zcs, const ZSTD_CDict* cdict); </b>/**< note : cdict will just be referenced, and must outlive compression session */<b>
378size_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 +0200379size_t ZSTD_sizeof_CStream(const ZSTD_CStream* zcs);
380</b></pre><BR>
381<h3>Advanced Streaming decompression functions</h3><pre><b>typedef enum { ZSTDdsp_maxWindowSize } ZSTD_DStreamParameter_e;
382ZSTD_DStream* ZSTD_createDStream_advanced(ZSTD_customMem customMem);
383size_t ZSTD_initDStream_usingDict(ZSTD_DStream* zds, const void* dict, size_t dictSize);
384size_t ZSTD_setDStreamParameter(ZSTD_DStream* zds, ZSTD_DStreamParameter_e paramType, unsigned paramValue);
Przemyslaw Skibinski1fd5b452016-10-31 10:44:44 +0100385size_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 +0200386size_t ZSTD_resetDStream(ZSTD_DStream* zds); </b>/**< re-use decompression parameters from previous init; saves dictionary loading */<b>
387size_t ZSTD_sizeof_DStream(const ZSTD_DStream* zds);
388</b></pre><BR>
389<a name="Chapter15"></a><h2>Buffer-less and synchronous inner streaming functions</h2><pre>
390 This is an advanced API, giving full control over buffer management, for users which need direct control over memory.
391 But it's also a complex one, with many restrictions (documented below).
Przemyslaw Skibinski1fd5b452016-10-31 10:44:44 +0100392 Prefer using normal streaming API for an easier experience
Przemyslaw Skibinski86d94242016-10-24 16:07:53 +0200393
394<BR></pre>
395
396<a name="Chapter16"></a><h2>Buffer-less streaming compression (synchronous mode)</h2><pre>
397 A ZSTD_CCtx object is required to track streaming operations.
398 Use ZSTD_createCCtx() / ZSTD_freeCCtx() to manage resource.
399 ZSTD_CCtx object can be re-used multiple times within successive compression operations.
400
401 Start by initializing a context.
402 Use ZSTD_compressBegin(), or ZSTD_compressBegin_usingDict() for dictionary compression,
403 or ZSTD_compressBegin_advanced(), for finer parameter control.
404 It's also possible to duplicate a reference context which has already been initialized, using ZSTD_copyCCtx()
405
406 Then, consume your input using ZSTD_compressContinue().
407 There are some important considerations to keep in mind when using this advanced function :
408 - ZSTD_compressContinue() has no internal buffer. It uses externally provided buffer only.
409 - Interface is synchronous : input is consumed entirely and produce 1+ (or more) compressed blocks.
410 - Caller must ensure there is enough space in `dst` to store compressed data under worst case scenario.
411 Worst case evaluation is provided by ZSTD_compressBound().
412 ZSTD_compressContinue() doesn't guarantee recover after a failed compression.
413 - ZSTD_compressContinue() presumes prior input ***is still accessible and unmodified*** (up to maximum distance size, see WindowLog).
414 It remembers all previous contiguous blocks, plus one separated memory segment (which can itself consists of multiple contiguous blocks)
415 - ZSTD_compressContinue() detects that prior input has been overwritten when `src` buffer overlaps.
416 In which case, it will "discard" the relevant memory section from its history.
417
418 Finish a frame with ZSTD_compressEnd(), which will write the last block(s) and optional checksum.
419 It's possible to use a NULL,0 src content, in which case, it will write a final empty block to end the frame,
420 Without last block mark, frames will be considered unfinished (broken) by decoders.
421
422 You can then reuse `ZSTD_CCtx` (ZSTD_compressBegin()) to compress some new frame.
423<BR></pre>
424
425<h3>Buffer-less streaming compression functions</h3><pre><b>size_t ZSTD_compressBegin(ZSTD_CCtx* cctx, int compressionLevel);
426size_t ZSTD_compressBegin_usingDict(ZSTD_CCtx* cctx, const void* dict, size_t dictSize, int compressionLevel);
427size_t ZSTD_compressBegin_advanced(ZSTD_CCtx* cctx, const void* dict, size_t dictSize, ZSTD_parameters params, unsigned long long pledgedSrcSize);
428size_t ZSTD_copyCCtx(ZSTD_CCtx* cctx, const ZSTD_CCtx* preparedCCtx, unsigned long long pledgedSrcSize);
429size_t ZSTD_compressContinue(ZSTD_CCtx* cctx, void* dst, size_t dstCapacity, const void* src, size_t srcSize);
430size_t ZSTD_compressEnd(ZSTD_CCtx* cctx, void* dst, size_t dstCapacity, const void* src, size_t srcSize);
431</b></pre><BR>
432<a name="Chapter17"></a><h2>Buffer-less streaming decompression (synchronous mode)</h2><pre>
433 A ZSTD_DCtx object is required to track streaming operations.
434 Use ZSTD_createDCtx() / ZSTD_freeDCtx() to manage it.
435 A ZSTD_DCtx object can be re-used multiple times.
436
437 First typical operation is to retrieve frame parameters, using ZSTD_getFrameParams().
438 It fills a ZSTD_frameParams structure which provide important information to correctly decode the frame,
439 such as the minimum rolling buffer size to allocate to decompress data (`windowSize`),
440 and the dictionary ID used.
441 (Note : content size is optional, it may not be present. 0 means : content size unknown).
442 Note that these values could be wrong, either because of data malformation, or because an attacker is spoofing deliberate false information.
443 As a consequence, check that values remain within valid application range, especially `windowSize`, before allocation.
444 Each application can set its own limit, depending on local restrictions. For extended interoperability, it is recommended to support at least 8 MB.
445 Frame parameters are extracted from the beginning of the compressed frame.
446 Data fragment must be large enough to ensure successful decoding, typically `ZSTD_frameHeaderSize_max` bytes.
447 @result : 0 : successful decoding, the `ZSTD_frameParams` structure is correctly filled.
448 >0 : `srcSize` is too small, please provide at least @result bytes on next attempt.
449 errorCode, which can be tested using ZSTD_isError().
450
451 Start decompression, with ZSTD_decompressBegin() or ZSTD_decompressBegin_usingDict().
452 Alternatively, you can copy a prepared context, using ZSTD_copyDCtx().
453
454 Then use ZSTD_nextSrcSizeToDecompress() and ZSTD_decompressContinue() alternatively.
455 ZSTD_nextSrcSizeToDecompress() tells how many bytes to provide as 'srcSize' to ZSTD_decompressContinue().
456 ZSTD_decompressContinue() requires this _exact_ amount of bytes, or it will fail.
457
458 @result of ZSTD_decompressContinue() is the number of bytes regenerated within 'dst' (necessarily <= dstCapacity).
459 It can be zero, which is not an error; it just means ZSTD_decompressContinue() has decoded some metadata item.
460 It can also be an error code, which can be tested with ZSTD_isError().
461
462 ZSTD_decompressContinue() needs previous data blocks during decompression, up to `windowSize`.
463 They should preferably be located contiguously, prior to current block.
464 Alternatively, a round buffer of sufficient size is also possible. Sufficient size is determined by frame parameters.
465 ZSTD_decompressContinue() is very sensitive to contiguity,
466 if 2 blocks don't follow each other, make sure that either the compressor breaks contiguity at the same place,
467 or that previous contiguous segment is large enough to properly handle maximum back-reference.
468
469 A frame is fully decoded when ZSTD_nextSrcSizeToDecompress() returns zero.
470 Context can then be reset to start a new decompression.
471
472 Note : it's possible to know if next input to present is a header or a block, using ZSTD_nextInputType().
473 This information is not required to properly decode a frame.
474
475 == Special case : skippable frames ==
476
477 Skippable frames allow integration of user-defined data into a flow of concatenated frames.
478 Skippable frames will be ignored (skipped) by a decompressor. The format of skippable frames is as follows :
479 a) Skippable frame ID - 4 Bytes, Little endian format, any value from 0x184D2A50 to 0x184D2A5F
480 b) Frame Size - 4 Bytes, Little endian format, unsigned 32-bits
481 c) Frame Content - any content (User Data) of length equal to Frame Size
482 For skippable frames ZSTD_decompressContinue() always returns 0.
483 For skippable frames ZSTD_getFrameParams() returns fparamsPtr->windowLog==0 what means that a frame is skippable.
484 It also returns Frame Size as fparamsPtr->frameContentSize.
485<BR></pre>
486
487<pre><b>typedef struct {
488 unsigned long long frameContentSize;
489 unsigned windowSize;
490 unsigned dictID;
491 unsigned checksumFlag;
492} ZSTD_frameParams;
493</b></pre><BR>
494<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>
495size_t ZSTD_decompressBegin(ZSTD_DCtx* dctx);
496size_t ZSTD_decompressBegin_usingDict(ZSTD_DCtx* dctx, const void* dict, size_t dictSize);
497void ZSTD_copyDCtx(ZSTD_DCtx* dctx, const ZSTD_DCtx* preparedDCtx);
498size_t ZSTD_nextSrcSizeToDecompress(ZSTD_DCtx* dctx);
499size_t ZSTD_decompressContinue(ZSTD_DCtx* dctx, void* dst, size_t dstCapacity, const void* src, size_t srcSize);
500typedef enum { ZSTDnit_frameHeader, ZSTDnit_blockHeader, ZSTDnit_block, ZSTDnit_lastBlock, ZSTDnit_checksum, ZSTDnit_skippableFrame } ZSTD_nextInputType_e;
501ZSTD_nextInputType_e ZSTD_nextInputType(ZSTD_DCtx* dctx);
502</b></pre><BR>
503<a name="Chapter18"></a><h2>Block functions</h2><pre>
504 Block functions produce and decode raw zstd blocks, without frame metadata.
505 Frame metadata cost is typically ~18 bytes, which can be non-negligible for very small blocks (< 100 bytes).
506 User will have to take in charge required information to regenerate data, such as compressed and content sizes.
507
508 A few rules to respect :
509 - Compressing and decompressing require a context structure
510 + Use ZSTD_createCCtx() and ZSTD_createDCtx()
511 - It is necessary to init context before starting
512 + compression : ZSTD_compressBegin()
513 + decompression : ZSTD_decompressBegin()
514 + variants _usingDict() are also allowed
515 + copyCCtx() and copyDCtx() work too
516 - Block size is limited, it must be <= ZSTD_getBlockSizeMax()
517 + If you need to compress more, cut data into multiple blocks
518 + Consider using the regular ZSTD_compress() instead, as frame metadata costs become negligible when source size is large.
519 - When a block is considered not compressible enough, ZSTD_compressBlock() result will be zero.
520 In which case, nothing is produced into `dst`.
521 + User must test for such outcome and deal directly with uncompressed data
522 + ZSTD_decompressBlock() doesn't accept uncompressed data as input !!!
523 + In case of multiple successive blocks, decoder must be informed of uncompressed block existence to follow proper history.
524 Use ZSTD_insertBlock() in such a case.
525<BR></pre>
526
527<h3>Raw zstd block functions</h3><pre><b>size_t ZSTD_getBlockSizeMax(ZSTD_CCtx* cctx);
528size_t ZSTD_compressBlock (ZSTD_CCtx* cctx, void* dst, size_t dstCapacity, const void* src, size_t srcSize);
529size_t ZSTD_decompressBlock(ZSTD_DCtx* dctx, void* dst, size_t dstCapacity, const void* src, size_t srcSize);
530size_t ZSTD_insertBlock(ZSTD_DCtx* dctx, const void* blockStart, size_t blockSize); </b>/**< insert block into `dctx` history. Useful for uncompressed blocks */<b>
531</b></pre><BR>
532</html>
533</body>