blob: 81901ce9f10dd7f7b0ed355d480fd5c57da25f9a [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
Przemyslaw Skibinski4da53212016-12-07 11:18:40 +010091<h3>Helper functions</h3><pre></pre><b><pre>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>
Przemyslaw Skibinski4da53212016-12-07 11:18:40 +010095</pre></b><BR>
Przemyslaw Skibinski86d94242016-10-24 16:07:53 +020096<a name="Chapter4"></a><h2>Explicit memory management</h2><pre></pre>
97
Przemyslaw Skibinski4da53212016-12-07 11:18:40 +010098<h3>Compression context</h3><pre> When compressing many times,
99 it is recommended to allocate a context just once, and re-use it for each successive compression operation.
100 This will make workload friendlier for system's memory.
101 Use one context per thread for parallel execution in multi-threaded environments.
102</pre><b><pre>typedef struct ZSTD_CCtx_s ZSTD_CCtx;
103ZSTD_CCtx* ZSTD_createCCtx(void);
104size_t ZSTD_freeCCtx(ZSTD_CCtx* cctx);
105</pre></b><BR>
Przemyslaw Skibinski86d94242016-10-24 16:07:53 +0200106<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 -0700107</b><p> Same as ZSTD_compress(), requires an allocated ZSTD_CCtx (see ZSTD_createCCtx()).
Przemyslaw Skibinski86d94242016-10-24 16:07:53 +0200108</p></pre><BR>
109
Przemyslaw Skibinski4da53212016-12-07 11:18:40 +0100110<h3>Decompression context</h3><pre></pre><b><pre>typedef struct ZSTD_DCtx_s ZSTD_DCtx;
Przemyslaw Skibinski86d94242016-10-24 16:07:53 +0200111ZSTD_DCtx* ZSTD_createDCtx(void);
112size_t ZSTD_freeDCtx(ZSTD_DCtx* dctx);
Przemyslaw Skibinski4da53212016-12-07 11:18:40 +0100113</pre></b><BR>
Przemyslaw Skibinski86d94242016-10-24 16:07:53 +0200114<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 -0700115</b><p> Same as ZSTD_decompress(), requires an allocated ZSTD_DCtx (see ZSTD_createDCtx()).
Przemyslaw Skibinski86d94242016-10-24 16:07:53 +0200116</p></pre><BR>
117
118<a name="Chapter5"></a><h2>Simple dictionary API</h2><pre></pre>
119
120<pre><b>size_t ZSTD_compress_usingDict(ZSTD_CCtx* ctx,
121 void* dst, size_t dstCapacity,
122 const void* src, size_t srcSize,
123 const void* dict,size_t dictSize,
124 int compressionLevel);
125</b><p> Compression using a predefined Dictionary (see dictBuilder/zdict.h).
Nick Terrelld82efd82016-11-02 16:47:53 -0700126 Note : This function loads the dictionary, resulting in significant startup delay.
127 Note : When `dict == NULL || dictSize < 8` no dictionary is used.
Przemyslaw Skibinski86d94242016-10-24 16:07:53 +0200128</p></pre><BR>
129
130<pre><b>size_t ZSTD_decompress_usingDict(ZSTD_DCtx* dctx,
131 void* dst, size_t dstCapacity,
132 const void* src, size_t srcSize,
133 const void* dict,size_t dictSize);
134</b><p> Decompression using a predefined Dictionary (see dictBuilder/zdict.h).
135 Dictionary must be identical to the one used during compression.
Nick Terrelld82efd82016-11-02 16:47:53 -0700136 Note : This function loads the dictionary, resulting in significant startup delay.
137 Note : When `dict == NULL || dictSize < 8` no dictionary is used.
Przemyslaw Skibinski86d94242016-10-24 16:07:53 +0200138</p></pre><BR>
139
140<a name="Chapter6"></a><h2>Fast dictionary API</h2><pre></pre>
141
142<pre><b>ZSTD_CDict* ZSTD_createCDict(const void* dict, size_t dictSize, int compressionLevel);
Przemyslaw Skibinski1fd5b452016-10-31 10:44:44 +0100143</b><p> When compressing multiple messages / blocks with the same dictionary, it's recommended to load it just once.
144 ZSTD_createCDict() will create a digested dictionary, ready to start future compression operations without startup delay.
145 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 -0700146 `dict` can be released after ZSTD_CDict creation.
Przemyslaw Skibinski86d94242016-10-24 16:07:53 +0200147</p></pre><BR>
148
149<pre><b>size_t ZSTD_freeCDict(ZSTD_CDict* CDict);
Nick Terrelld82efd82016-11-02 16:47:53 -0700150</b><p> Function frees memory allocated by ZSTD_createCDict().
Przemyslaw Skibinski86d94242016-10-24 16:07:53 +0200151</p></pre><BR>
152
153<pre><b>size_t ZSTD_compress_usingCDict(ZSTD_CCtx* cctx,
154 void* dst, size_t dstCapacity,
155 const void* src, size_t srcSize,
156 const ZSTD_CDict* cdict);
157</b><p> Compression using a digested Dictionary.
158 Faster startup than ZSTD_compress_usingDict(), recommended when same dictionary is used multiple times.
Nick Terrelld82efd82016-11-02 16:47:53 -0700159 Note that compression level is decided during dictionary creation.
Przemyslaw Skibinski86d94242016-10-24 16:07:53 +0200160</p></pre><BR>
161
162<pre><b>ZSTD_DDict* ZSTD_createDDict(const void* dict, size_t dictSize);
163</b><p> Create a digested dictionary, ready to start decompression operation without startup delay.
Nick Terrelld82efd82016-11-02 16:47:53 -0700164 `dict` can be released after creation.
Przemyslaw Skibinski86d94242016-10-24 16:07:53 +0200165</p></pre><BR>
166
167<pre><b>size_t ZSTD_freeDDict(ZSTD_DDict* ddict);
168</b><p> Function frees memory allocated with ZSTD_createDDict()
169</p></pre><BR>
170
171<pre><b>size_t ZSTD_decompress_usingDDict(ZSTD_DCtx* dctx,
172 void* dst, size_t dstCapacity,
173 const void* src, size_t srcSize,
174 const ZSTD_DDict* ddict);
Nick Terrelld82efd82016-11-02 16:47:53 -0700175</b><p> Decompression using a digested Dictionary.
Przemyslaw Skibinski86d94242016-10-24 16:07:53 +0200176 Faster startup than ZSTD_decompress_usingDict(), recommended when same dictionary is used multiple times.
177</p></pre><BR>
178
179<a name="Chapter7"></a><h2>Streaming</h2><pre></pre>
180
181<pre><b>typedef struct ZSTD_inBuffer_s {
182 const void* src; </b>/**< start of input buffer */<b>
183 size_t size; </b>/**< size of input buffer */<b>
184 size_t pos; </b>/**< position where reading stopped. Will be updated. Necessarily 0 <= pos <= size */<b>
185} ZSTD_inBuffer;
186</b></pre><BR>
187<pre><b>typedef struct ZSTD_outBuffer_s {
188 void* dst; </b>/**< start of output buffer */<b>
189 size_t size; </b>/**< size of output buffer */<b>
190 size_t pos; </b>/**< position where writing stopped. Will be updated. Necessarily 0 <= pos <= size */<b>
191} ZSTD_outBuffer;
192</b></pre><BR>
193<a name="Chapter8"></a><h2>Streaming compression - HowTo</h2><pre>
194 A ZSTD_CStream object is required to track streaming operation.
195 Use ZSTD_createCStream() and ZSTD_freeCStream() to create/release resources.
196 ZSTD_CStream objects can be reused multiple times on consecutive compression operations.
Przemyslaw Skibinski1fd5b452016-10-31 10:44:44 +0100197 It is recommended to re-use ZSTD_CStream in situations where many streaming operations will be achieved consecutively,
198 since it will play nicer with system's memory, by re-using already allocated memory.
199 Use one separate ZSTD_CStream per thread for parallel execution.
Przemyslaw Skibinski86d94242016-10-24 16:07:53 +0200200
Przemyslaw Skibinski1fd5b452016-10-31 10:44:44 +0100201 Start a new compression by initializing ZSTD_CStream.
Przemyslaw Skibinski86d94242016-10-24 16:07:53 +0200202 Use ZSTD_initCStream() to start a new compression operation.
203 Use ZSTD_initCStream_usingDict() for a compression which requires a dictionary.
204
205 Use ZSTD_compressStream() repetitively to consume input stream.
206 The function will automatically update both `pos` fields.
207 Note that it may not consume the entire input, in which case `pos < size`,
208 and it's up to the caller to present again remaining data.
209 @return : a size hint, preferred nb of bytes to use as input for next function call
210 (it's just a hint, to help latency a little, any other value will work fine)
211 (note : the size hint is guaranteed to be <= ZSTD_CStreamInSize() )
212 or an error code, which can be tested using ZSTD_isError().
213
214 At any moment, it's possible to flush whatever data remains within buffer, using ZSTD_flushStream().
215 `output->pos` will be updated.
216 Note some content might still be left within internal buffer if `output->size` is too small.
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 ZSTD_endStream() instructs to finish a frame.
221 It will perform a flush and write frame epilogue.
222 The epilogue is required for decoders to consider a frame completed.
223 Similar to ZSTD_flushStream(), it may not be able to flush the full content if `output->size` is too small.
224 In which case, call again ZSTD_endStream() to complete the flush.
225 @return : nb of bytes still present within internal buffer (0 if it's empty)
226 or an error code, which can be tested using ZSTD_isError().
227
228
229<BR></pre>
230
Przemyslaw Skibinski4da53212016-12-07 11:18:40 +0100231<h3>Streaming compression functions</h3><pre></pre><b><pre>typedef struct ZSTD_CStream_s ZSTD_CStream;
Przemyslaw Skibinski86d94242016-10-24 16:07:53 +0200232ZSTD_CStream* ZSTD_createCStream(void);
233size_t ZSTD_freeCStream(ZSTD_CStream* zcs);
234size_t ZSTD_initCStream(ZSTD_CStream* zcs, int compressionLevel);
235size_t ZSTD_compressStream(ZSTD_CStream* zcs, ZSTD_outBuffer* output, ZSTD_inBuffer* input);
236size_t ZSTD_flushStream(ZSTD_CStream* zcs, ZSTD_outBuffer* output);
237size_t ZSTD_endStream(ZSTD_CStream* zcs, ZSTD_outBuffer* output);
Przemyslaw Skibinski4da53212016-12-07 11:18:40 +0100238</pre></b><BR>
Przemyslaw Skibinski86d94242016-10-24 16:07:53 +0200239<pre><b>size_t ZSTD_CStreamInSize(void); </b>/**< recommended size for input buffer */<b>
240</b></pre><BR>
241<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>
242</b></pre><BR>
243<a name="Chapter9"></a><h2>Streaming decompression - HowTo</h2><pre>
244 A ZSTD_DStream object is required to track streaming operations.
245 Use ZSTD_createDStream() and ZSTD_freeDStream() to create/release resources.
246 ZSTD_DStream objects can be re-used multiple times.
247
248 Use ZSTD_initDStream() to start a new decompression operation,
249 or ZSTD_initDStream_usingDict() if decompression requires a dictionary.
250 @return : recommended first input size
251
252 Use ZSTD_decompressStream() repetitively to consume your input.
253 The function will update both `pos` fields.
254 If `input.pos < input.size`, some input has not been consumed.
255 It's up to the caller to present again remaining data.
256 If `output.pos < output.size`, decoder has flushed everything it could.
257 @return : 0 when a frame is completely decoded and fully flushed,
258 an error code, which can be tested using ZSTD_isError(),
Przemyslaw Skibinski4da53212016-12-07 11:18:40 +0100259 any other value > 0, which means there is still some decoding to do to complete current frame.
260 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 +0200261
262<BR></pre>
263
Przemyslaw Skibinski4da53212016-12-07 11:18:40 +0100264<h3>Streaming decompression functions</h3><pre></pre><b><pre>typedef struct ZSTD_DStream_s ZSTD_DStream;
Przemyslaw Skibinski86d94242016-10-24 16:07:53 +0200265ZSTD_DStream* ZSTD_createDStream(void);
266size_t ZSTD_freeDStream(ZSTD_DStream* zds);
267size_t ZSTD_initDStream(ZSTD_DStream* zds);
268size_t ZSTD_decompressStream(ZSTD_DStream* zds, ZSTD_outBuffer* output, ZSTD_inBuffer* input);
Przemyslaw Skibinski4da53212016-12-07 11:18:40 +0100269</pre></b><BR>
Przemyslaw Skibinski86d94242016-10-24 16:07:53 +0200270<pre><b>size_t ZSTD_DStreamInSize(void); </b>/*!< recommended size for input buffer */<b>
271</b></pre><BR>
272<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>
273</b></pre><BR>
274<a name="Chapter10"></a><h2>START OF ADVANCED AND EXPERIMENTAL FUNCTIONS</h2><pre> The definitions in this section are considered experimental.
275 They should never be used with a dynamic library, as they may change in the future.
276 They are provided for advanced usages.
277 Use them only in association with static linking.
278
279<BR></pre>
280
281<a name="Chapter11"></a><h2>Advanced types</h2><pre></pre>
282
Przemyslaw Skibinski1fd5b452016-10-31 10:44:44 +0100283<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 +0200284</b></pre><BR>
285<pre><b>typedef struct {
286 unsigned windowLog; </b>/**< largest match distance : larger == more compression, more memory needed during decompression */<b>
287 unsigned chainLog; </b>/**< fully searched segment : larger == more compression, slower, more memory (useless for fast) */<b>
288 unsigned hashLog; </b>/**< dispatch table : larger == faster, more memory */<b>
289 unsigned searchLog; </b>/**< nb of searches : larger == more compression, slower */<b>
290 unsigned searchLength; </b>/**< match length searched : larger == faster decompression, sometimes less compression */<b>
291 unsigned targetLength; </b>/**< acceptable match size for optimal parser (only) : larger == more compression, slower */<b>
292 ZSTD_strategy strategy;
293} ZSTD_compressionParameters;
294</b></pre><BR>
295<pre><b>typedef struct {
296 unsigned contentSizeFlag; </b>/**< 1: content size will be in frame header (if known). */<b>
297 unsigned checksumFlag; </b>/**< 1: will generate a 22-bits checksum at end of frame, to be used for error detection by decompressor */<b>
298 unsigned noDictIDFlag; </b>/**< 1: no dict ID will be saved into frame header (if dictionary compression) */<b>
299} ZSTD_frameParameters;
300</b></pre><BR>
301<pre><b>typedef struct {
302 ZSTD_compressionParameters cParams;
303 ZSTD_frameParameters fParams;
304} ZSTD_parameters;
305</b></pre><BR>
Przemyslaw Skibinski4da53212016-12-07 11:18:40 +0100306<h3>Custom memory allocation functions</h3><pre></pre><b><pre>typedef void* (*ZSTD_allocFunction) (void* opaque, size_t size);
Przemyslaw Skibinski86d94242016-10-24 16:07:53 +0200307typedef void (*ZSTD_freeFunction) (void* opaque, void* address);
308typedef struct { ZSTD_allocFunction customAlloc; ZSTD_freeFunction customFree; void* opaque; } ZSTD_customMem;
Przemyslaw Skibinski4da53212016-12-07 11:18:40 +0100309</pre></b><BR>
Przemyslaw Skibinski86d94242016-10-24 16:07:53 +0200310<a name="Chapter12"></a><h2>Advanced compression functions</h2><pre></pre>
311
312<pre><b>size_t ZSTD_estimateCCtxSize(ZSTD_compressionParameters cParams);
313</b><p> Gives the amount of memory allocated for a ZSTD_CCtx given a set of compression parameters.
314 `frameContentSize` is an optional parameter, provide `0` if unknown
315</p></pre><BR>
316
317<pre><b>ZSTD_CCtx* ZSTD_createCCtx_advanced(ZSTD_customMem customMem);
318</b><p> Create a ZSTD compression context using external alloc and free functions
319</p></pre><BR>
320
321<pre><b>size_t ZSTD_sizeof_CCtx(const ZSTD_CCtx* cctx);
322</b><p> Gives the amount of memory used by a given ZSTD_CCtx
323</p></pre><BR>
324
325<pre><b>ZSTD_CDict* ZSTD_createCDict_advanced(const void* dict, size_t dictSize,
326 ZSTD_parameters params, ZSTD_customMem customMem);
327</b><p> Create a ZSTD_CDict using external alloc and free, and customized compression parameters
328</p></pre><BR>
329
330<pre><b>size_t ZSTD_sizeof_CDict(const ZSTD_CDict* cdict);
331</b><p> Gives the amount of memory used by a given ZSTD_sizeof_CDict
332</p></pre><BR>
333
334<pre><b>ZSTD_parameters ZSTD_getParams(int compressionLevel, unsigned long long srcSize, size_t dictSize);
335</b><p> same as ZSTD_getCParams(), but @return a full `ZSTD_parameters` object instead of a `ZSTD_compressionParameters`.
336 All fields of `ZSTD_frameParameters` are set to default (0)
337</p></pre><BR>
338
339<pre><b>ZSTD_compressionParameters ZSTD_getCParams(int compressionLevel, unsigned long long srcSize, size_t dictSize);
340</b><p> @return ZSTD_compressionParameters structure for a selected compression level and srcSize.
341 `srcSize` value is optional, select 0 if not known
342</p></pre><BR>
343
344<pre><b>size_t ZSTD_checkCParams(ZSTD_compressionParameters params);
345</b><p> Ensure param values remain within authorized range
346</p></pre><BR>
347
348<pre><b>ZSTD_compressionParameters ZSTD_adjustCParams(ZSTD_compressionParameters cPar, unsigned long long srcSize, size_t dictSize);
349</b><p> optimize params for a given `srcSize` and `dictSize`.
350 both values are optional, select `0` if unknown.
351</p></pre><BR>
352
353<pre><b>size_t ZSTD_compress_advanced (ZSTD_CCtx* ctx,
354 void* dst, size_t dstCapacity,
355 const void* src, size_t srcSize,
356 const void* dict,size_t dictSize,
357 ZSTD_parameters params);
358</b><p> Same as ZSTD_compress_usingDict(), with fine-tune control of each compression parameter
359</p></pre><BR>
360
361<a name="Chapter13"></a><h2>Advanced decompression functions</h2><pre></pre>
362
Przemyslaw Skibinski4da53212016-12-07 11:18:40 +0100363<pre><b>unsigned ZSTD_isFrame(const void* buffer, size_t size);
364</b><p> Tells if the content of `buffer` starts with a valid Frame Identifier.
365 Note : Frame Identifier is 4 bytes. If `size < 4`, @return will always be 0.
366 Note 2 : Legacy Frame Identifiers are considered valid only if Legacy Support is enabled.
367 Note 3 : Skippable Frame Identifiers are considered valid.
368</p></pre><BR>
369
Przemyslaw Skibinski86d94242016-10-24 16:07:53 +0200370<pre><b>size_t ZSTD_estimateDCtxSize(void);
371</b><p> Gives the potential amount of memory allocated to create a ZSTD_DCtx
372</p></pre><BR>
373
374<pre><b>ZSTD_DCtx* ZSTD_createDCtx_advanced(ZSTD_customMem customMem);
375</b><p> Create a ZSTD decompression context using external alloc and free functions
376</p></pre><BR>
377
378<pre><b>size_t ZSTD_sizeof_DCtx(const ZSTD_DCtx* dctx);
379</b><p> Gives the amount of memory used by a given ZSTD_DCtx
380</p></pre><BR>
381
382<pre><b>size_t ZSTD_sizeof_DDict(const ZSTD_DDict* ddict);
383</b><p> Gives the amount of memory used by a given ZSTD_DDict
384</p></pre><BR>
385
Przemyslaw Skibinski4da53212016-12-07 11:18:40 +0100386<pre><b>unsigned ZSTD_getDictID_fromDict(const void* dict, size_t dictSize);
387</b><p> Provides the dictID stored within dictionary.
388 if @return == 0, the dictionary is not conformant with Zstandard specification.
389 It can still be loaded, but as a content-only dictionary.
390</p></pre><BR>
391
392<pre><b>unsigned ZSTD_getDictID_fromDDict(const ZSTD_DDict* ddict);
393</b><p> Provides the dictID of the dictionary loaded into `ddict`.
394 If @return == 0, the dictionary is not conformant to Zstandard specification, or empty.
395 Non-conformant dictionaries can still be loaded, but as content-only dictionaries.
396</p></pre><BR>
397
398<pre><b>unsigned ZSTD_getDictID_fromFrame(const void* src, size_t srcSize);
399</b><p> Provides the dictID required to decompressed the frame stored within `src`.
400 If @return == 0, the dictID could not be decoded.
401 This could for one of the following reasons :
402 - The frame does not require a dictionary to be decoded (most common case).
403 - The frame was built with dictID intentionally removed. Whatever dictionary is necessary is a hidden information.
404 Note : this use case also happens when using a non-conformant dictionary.
405 - `srcSize` is too small, and as a result, the frame header could not be decoded (only possible if `srcSize < ZSTD_FRAMEHEADERSIZE_MAX`).
406 - This is not a Zstandard frame.
407 When identifying the exact failure cause, it's possible to used ZSTD_getFrameParams(), which will provide a more precise error code.
408</p></pre><BR>
409
Przemyslaw Skibinski86d94242016-10-24 16:07:53 +0200410<a name="Chapter14"></a><h2>Advanced streaming functions</h2><pre></pre>
411
Przemyslaw Skibinski4da53212016-12-07 11:18:40 +0100412<h3>Advanced Streaming compression functions</h3><pre></pre><b><pre>ZSTD_CStream* ZSTD_createCStream_advanced(ZSTD_customMem customMem);
Przemyslaw Skibinski86d94242016-10-24 16:07:53 +0200413size_t ZSTD_initCStream_usingDict(ZSTD_CStream* zcs, const void* dict, size_t dictSize, int compressionLevel);
414size_t ZSTD_initCStream_advanced(ZSTD_CStream* zcs, const void* dict, size_t dictSize,
415 ZSTD_parameters params, unsigned long long pledgedSrcSize); </b>/**< pledgedSrcSize is optional and can be zero == unknown */<b>
Przemyslaw Skibinski1fd5b452016-10-31 10:44:44 +0100416size_t ZSTD_initCStream_usingCDict(ZSTD_CStream* zcs, const ZSTD_CDict* cdict); </b>/**< note : cdict will just be referenced, and must outlive compression session */<b>
417size_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 +0200418size_t ZSTD_sizeof_CStream(const ZSTD_CStream* zcs);
Przemyslaw Skibinski4da53212016-12-07 11:18:40 +0100419</pre></b><BR>
420<h3>Advanced Streaming decompression functions</h3><pre></pre><b><pre>typedef enum { ZSTDdsp_maxWindowSize } ZSTD_DStreamParameter_e;
Przemyslaw Skibinski86d94242016-10-24 16:07:53 +0200421ZSTD_DStream* ZSTD_createDStream_advanced(ZSTD_customMem customMem);
422size_t ZSTD_initDStream_usingDict(ZSTD_DStream* zds, const void* dict, size_t dictSize);
423size_t ZSTD_setDStreamParameter(ZSTD_DStream* zds, ZSTD_DStreamParameter_e paramType, unsigned paramValue);
Przemyslaw Skibinski1fd5b452016-10-31 10:44:44 +0100424size_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 +0200425size_t ZSTD_resetDStream(ZSTD_DStream* zds); </b>/**< re-use decompression parameters from previous init; saves dictionary loading */<b>
426size_t ZSTD_sizeof_DStream(const ZSTD_DStream* zds);
Przemyslaw Skibinski4da53212016-12-07 11:18:40 +0100427</pre></b><BR>
Przemyslaw Skibinski86d94242016-10-24 16:07:53 +0200428<a name="Chapter15"></a><h2>Buffer-less and synchronous inner streaming functions</h2><pre>
429 This is an advanced API, giving full control over buffer management, for users which need direct control over memory.
430 But it's also a complex one, with many restrictions (documented below).
Przemyslaw Skibinski1fd5b452016-10-31 10:44:44 +0100431 Prefer using normal streaming API for an easier experience
Przemyslaw Skibinski86d94242016-10-24 16:07:53 +0200432
433<BR></pre>
434
435<a name="Chapter16"></a><h2>Buffer-less streaming compression (synchronous mode)</h2><pre>
436 A ZSTD_CCtx object is required to track streaming operations.
437 Use ZSTD_createCCtx() / ZSTD_freeCCtx() to manage resource.
438 ZSTD_CCtx object can be re-used multiple times within successive compression operations.
439
440 Start by initializing a context.
441 Use ZSTD_compressBegin(), or ZSTD_compressBegin_usingDict() for dictionary compression,
442 or ZSTD_compressBegin_advanced(), for finer parameter control.
443 It's also possible to duplicate a reference context which has already been initialized, using ZSTD_copyCCtx()
444
445 Then, consume your input using ZSTD_compressContinue().
446 There are some important considerations to keep in mind when using this advanced function :
447 - ZSTD_compressContinue() has no internal buffer. It uses externally provided buffer only.
448 - Interface is synchronous : input is consumed entirely and produce 1+ (or more) compressed blocks.
449 - Caller must ensure there is enough space in `dst` to store compressed data under worst case scenario.
450 Worst case evaluation is provided by ZSTD_compressBound().
451 ZSTD_compressContinue() doesn't guarantee recover after a failed compression.
452 - ZSTD_compressContinue() presumes prior input ***is still accessible and unmodified*** (up to maximum distance size, see WindowLog).
453 It remembers all previous contiguous blocks, plus one separated memory segment (which can itself consists of multiple contiguous blocks)
454 - ZSTD_compressContinue() detects that prior input has been overwritten when `src` buffer overlaps.
455 In which case, it will "discard" the relevant memory section from its history.
456
457 Finish a frame with ZSTD_compressEnd(), which will write the last block(s) and optional checksum.
458 It's possible to use a NULL,0 src content, in which case, it will write a final empty block to end the frame,
459 Without last block mark, frames will be considered unfinished (broken) by decoders.
460
461 You can then reuse `ZSTD_CCtx` (ZSTD_compressBegin()) to compress some new frame.
462<BR></pre>
463
Przemyslaw Skibinski4da53212016-12-07 11:18:40 +0100464<h3>Buffer-less streaming compression functions</h3><pre></pre><b><pre>size_t ZSTD_compressBegin(ZSTD_CCtx* cctx, int compressionLevel);
Przemyslaw Skibinski86d94242016-10-24 16:07:53 +0200465size_t ZSTD_compressBegin_usingDict(ZSTD_CCtx* cctx, const void* dict, size_t dictSize, int compressionLevel);
466size_t ZSTD_compressBegin_advanced(ZSTD_CCtx* cctx, const void* dict, size_t dictSize, ZSTD_parameters params, unsigned long long pledgedSrcSize);
467size_t ZSTD_copyCCtx(ZSTD_CCtx* cctx, const ZSTD_CCtx* preparedCCtx, unsigned long long pledgedSrcSize);
468size_t ZSTD_compressContinue(ZSTD_CCtx* cctx, void* dst, size_t dstCapacity, const void* src, size_t srcSize);
469size_t ZSTD_compressEnd(ZSTD_CCtx* cctx, void* dst, size_t dstCapacity, const void* src, size_t srcSize);
Przemyslaw Skibinski4da53212016-12-07 11:18:40 +0100470</pre></b><BR>
Przemyslaw Skibinski86d94242016-10-24 16:07:53 +0200471<a name="Chapter17"></a><h2>Buffer-less streaming decompression (synchronous mode)</h2><pre>
472 A ZSTD_DCtx object is required to track streaming operations.
473 Use ZSTD_createDCtx() / ZSTD_freeDCtx() to manage it.
474 A ZSTD_DCtx object can be re-used multiple times.
475
476 First typical operation is to retrieve frame parameters, using ZSTD_getFrameParams().
477 It fills a ZSTD_frameParams structure which provide important information to correctly decode the frame,
478 such as the minimum rolling buffer size to allocate to decompress data (`windowSize`),
479 and the dictionary ID used.
480 (Note : content size is optional, it may not be present. 0 means : content size unknown).
481 Note that these values could be wrong, either because of data malformation, or because an attacker is spoofing deliberate false information.
482 As a consequence, check that values remain within valid application range, especially `windowSize`, before allocation.
483 Each application can set its own limit, depending on local restrictions. For extended interoperability, it is recommended to support at least 8 MB.
484 Frame parameters are extracted from the beginning of the compressed frame.
485 Data fragment must be large enough to ensure successful decoding, typically `ZSTD_frameHeaderSize_max` bytes.
486 @result : 0 : successful decoding, the `ZSTD_frameParams` structure is correctly filled.
487 >0 : `srcSize` is too small, please provide at least @result bytes on next attempt.
488 errorCode, which can be tested using ZSTD_isError().
489
490 Start decompression, with ZSTD_decompressBegin() or ZSTD_decompressBegin_usingDict().
491 Alternatively, you can copy a prepared context, using ZSTD_copyDCtx().
492
493 Then use ZSTD_nextSrcSizeToDecompress() and ZSTD_decompressContinue() alternatively.
494 ZSTD_nextSrcSizeToDecompress() tells how many bytes to provide as 'srcSize' to ZSTD_decompressContinue().
495 ZSTD_decompressContinue() requires this _exact_ amount of bytes, or it will fail.
496
497 @result of ZSTD_decompressContinue() is the number of bytes regenerated within 'dst' (necessarily <= dstCapacity).
498 It can be zero, which is not an error; it just means ZSTD_decompressContinue() has decoded some metadata item.
499 It can also be an error code, which can be tested with ZSTD_isError().
500
501 ZSTD_decompressContinue() needs previous data blocks during decompression, up to `windowSize`.
502 They should preferably be located contiguously, prior to current block.
503 Alternatively, a round buffer of sufficient size is also possible. Sufficient size is determined by frame parameters.
504 ZSTD_decompressContinue() is very sensitive to contiguity,
505 if 2 blocks don't follow each other, make sure that either the compressor breaks contiguity at the same place,
506 or that previous contiguous segment is large enough to properly handle maximum back-reference.
507
508 A frame is fully decoded when ZSTD_nextSrcSizeToDecompress() returns zero.
509 Context can then be reset to start a new decompression.
510
511 Note : it's possible to know if next input to present is a header or a block, using ZSTD_nextInputType().
512 This information is not required to properly decode a frame.
513
Przemyslaw Skibinski4da53212016-12-07 11:18:40 +0100514 == Special case : skippable frames ==
Przemyslaw Skibinski86d94242016-10-24 16:07:53 +0200515
516 Skippable frames allow integration of user-defined data into a flow of concatenated frames.
517 Skippable frames will be ignored (skipped) by a decompressor. The format of skippable frames is as follows :
518 a) Skippable frame ID - 4 Bytes, Little endian format, any value from 0x184D2A50 to 0x184D2A5F
519 b) Frame Size - 4 Bytes, Little endian format, unsigned 32-bits
520 c) Frame Content - any content (User Data) of length equal to Frame Size
521 For skippable frames ZSTD_decompressContinue() always returns 0.
522 For skippable frames ZSTD_getFrameParams() returns fparamsPtr->windowLog==0 what means that a frame is skippable.
523 It also returns Frame Size as fparamsPtr->frameContentSize.
524<BR></pre>
525
526<pre><b>typedef struct {
527 unsigned long long frameContentSize;
528 unsigned windowSize;
529 unsigned dictID;
530 unsigned checksumFlag;
531} ZSTD_frameParams;
532</b></pre><BR>
Przemyslaw Skibinski4da53212016-12-07 11:18:40 +0100533<h3>Buffer-less streaming decompression functions</h3><pre></pre><b><pre>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 +0200534size_t ZSTD_decompressBegin(ZSTD_DCtx* dctx);
535size_t ZSTD_decompressBegin_usingDict(ZSTD_DCtx* dctx, const void* dict, size_t dictSize);
536void ZSTD_copyDCtx(ZSTD_DCtx* dctx, const ZSTD_DCtx* preparedDCtx);
537size_t ZSTD_nextSrcSizeToDecompress(ZSTD_DCtx* dctx);
538size_t ZSTD_decompressContinue(ZSTD_DCtx* dctx, void* dst, size_t dstCapacity, const void* src, size_t srcSize);
539typedef enum { ZSTDnit_frameHeader, ZSTDnit_blockHeader, ZSTDnit_block, ZSTDnit_lastBlock, ZSTDnit_checksum, ZSTDnit_skippableFrame } ZSTD_nextInputType_e;
540ZSTD_nextInputType_e ZSTD_nextInputType(ZSTD_DCtx* dctx);
Przemyslaw Skibinski4da53212016-12-07 11:18:40 +0100541</pre></b><BR>
Przemyslaw Skibinski86d94242016-10-24 16:07:53 +0200542<a name="Chapter18"></a><h2>Block functions</h2><pre>
543 Block functions produce and decode raw zstd blocks, without frame metadata.
544 Frame metadata cost is typically ~18 bytes, which can be non-negligible for very small blocks (< 100 bytes).
545 User will have to take in charge required information to regenerate data, such as compressed and content sizes.
546
547 A few rules to respect :
548 - Compressing and decompressing require a context structure
549 + Use ZSTD_createCCtx() and ZSTD_createDCtx()
550 - It is necessary to init context before starting
551 + compression : ZSTD_compressBegin()
552 + decompression : ZSTD_decompressBegin()
553 + variants _usingDict() are also allowed
554 + copyCCtx() and copyDCtx() work too
555 - Block size is limited, it must be <= ZSTD_getBlockSizeMax()
556 + If you need to compress more, cut data into multiple blocks
557 + Consider using the regular ZSTD_compress() instead, as frame metadata costs become negligible when source size is large.
558 - When a block is considered not compressible enough, ZSTD_compressBlock() result will be zero.
559 In which case, nothing is produced into `dst`.
560 + User must test for such outcome and deal directly with uncompressed data
561 + ZSTD_decompressBlock() doesn't accept uncompressed data as input !!!
562 + In case of multiple successive blocks, decoder must be informed of uncompressed block existence to follow proper history.
563 Use ZSTD_insertBlock() in such a case.
564<BR></pre>
565
Przemyslaw Skibinski4da53212016-12-07 11:18:40 +0100566<h3>Raw zstd block functions</h3><pre></pre><b><pre>size_t ZSTD_getBlockSizeMax(ZSTD_CCtx* cctx);
Przemyslaw Skibinski86d94242016-10-24 16:07:53 +0200567size_t ZSTD_compressBlock (ZSTD_CCtx* cctx, void* dst, size_t dstCapacity, const void* src, size_t srcSize);
568size_t ZSTD_decompressBlock(ZSTD_DCtx* dctx, void* dst, size_t dstCapacity, const void* src, size_t srcSize);
569size_t ZSTD_insertBlock(ZSTD_DCtx* dctx, const void* blockStart, size_t blockSize); </b>/**< insert block into `dctx` history. Useful for uncompressed blocks */<b>
Przemyslaw Skibinski4da53212016-12-07 11:18:40 +0100570</pre></b><BR>
Przemyslaw Skibinski86d94242016-10-24 16:07:53 +0200571</html>
572</body>