Commit | Line | Data |
---|---|---|
cf30f6a5 NT |
1 | /* |
2 | * Copyright (c) Yann Collet, Facebook, Inc. | |
3 | * All rights reserved. | |
4 | * | |
e0c1b49f NT |
5 | * This source code is licensed under both the BSD-style license (found in the |
6 | * LICENSE file in the root directory of this source tree) and the GPLv2 (found | |
7 | * in the COPYING file in the root directory of this source tree). | |
8 | * You may select, at your option, one of the above-listed licenses. | |
cf30f6a5 NT |
9 | */ |
10 | ||
e0c1b49f NT |
11 | #ifndef ZSTD_H_235446 |
12 | #define ZSTD_H_235446 | |
cf30f6a5 NT |
13 | |
14 | /* ====== Dependency ======*/ | |
e0c1b49f | 15 | #include <linux/limits.h> /* INT_MAX */ |
cf30f6a5 NT |
16 | #include <linux/types.h> /* size_t */ |
17 | ||
18 | ||
e0c1b49f NT |
19 | /* ===== ZSTDLIB_API : control library symbols visibility ===== */ |
20 | #define ZSTDLIB_VISIBILITY | |
21 | #define ZSTDLIB_API ZSTDLIB_VISIBILITY | |
22 | ||
23 | ||
24 | /* ***************************************************************************** | |
25 | Introduction | |
26 | ||
27 | zstd, short for Zstandard, is a fast lossless compression algorithm, targeting | |
28 | real-time compression scenarios at zlib-level and better compression ratios. | |
29 | The zstd compression library provides in-memory compression and decompression | |
30 | functions. | |
31 | ||
32 | The library supports regular compression levels from 1 up to ZSTD_maxCLevel(), | |
33 | which is currently 22. Levels >= 20, labeled `--ultra`, should be used with | |
34 | caution, as they require more memory. The library also offers negative | |
35 | compression levels, which extend the range of speed vs. ratio preferences. | |
36 | The lower the level, the faster the speed (at the cost of compression). | |
37 | ||
38 | Compression can be done in: | |
39 | - a single step (described as Simple API) | |
40 | - a single step, reusing a context (described as Explicit context) | |
41 | - unbounded multiple steps (described as Streaming compression) | |
42 | ||
43 | The compression ratio achievable on small data can be highly improved using | |
44 | a dictionary. Dictionary compression can be performed in: | |
45 | - a single step (described as Simple dictionary API) | |
46 | - a single step, reusing a dictionary (described as Bulk-processing | |
47 | dictionary API) | |
48 | ||
49 | Advanced experimental functions can be accessed using | |
50 | `#define ZSTD_STATIC_LINKING_ONLY` before including zstd.h. | |
51 | ||
52 | Advanced experimental APIs should never be used with a dynamically-linked | |
53 | library. They are not "stable"; their definitions or signatures may change in | |
54 | the future. Only static linking is allowed. | |
55 | *******************************************************************************/ | |
56 | ||
57 | /*------ Version ------*/ | |
58 | #define ZSTD_VERSION_MAJOR 1 | |
59 | #define ZSTD_VERSION_MINOR 4 | |
60 | #define ZSTD_VERSION_RELEASE 10 | |
61 | #define ZSTD_VERSION_NUMBER (ZSTD_VERSION_MAJOR *100*100 + ZSTD_VERSION_MINOR *100 + ZSTD_VERSION_RELEASE) | |
62 | ||
63 | /*! ZSTD_versionNumber() : | |
64 | * Return runtime library version, the value is (MAJOR*100*100 + MINOR*100 + RELEASE). */ | |
65 | ZSTDLIB_API unsigned ZSTD_versionNumber(void); | |
66 | ||
67 | #define ZSTD_LIB_VERSION ZSTD_VERSION_MAJOR.ZSTD_VERSION_MINOR.ZSTD_VERSION_RELEASE | |
68 | #define ZSTD_QUOTE(str) #str | |
69 | #define ZSTD_EXPAND_AND_QUOTE(str) ZSTD_QUOTE(str) | |
70 | #define ZSTD_VERSION_STRING ZSTD_EXPAND_AND_QUOTE(ZSTD_LIB_VERSION) | |
71 | ||
72 | /*! ZSTD_versionString() : | |
73 | * Return runtime library version, like "1.4.5". Requires v1.3.0+. */ | |
74 | ZSTDLIB_API const char* ZSTD_versionString(void); | |
75 | ||
76 | /* ************************************* | |
77 | * Default constant | |
78 | ***************************************/ | |
79 | #ifndef ZSTD_CLEVEL_DEFAULT | |
80 | # define ZSTD_CLEVEL_DEFAULT 3 | |
81 | #endif | |
82 | ||
83 | /* ************************************* | |
84 | * Constants | |
85 | ***************************************/ | |
86 | ||
87 | /* All magic numbers are supposed read/written to/from files/memory using little-endian convention */ | |
88 | #define ZSTD_MAGICNUMBER 0xFD2FB528 /* valid since v0.8.0 */ | |
89 | #define ZSTD_MAGIC_DICTIONARY 0xEC30A437 /* valid since v0.7.0 */ | |
90 | #define ZSTD_MAGIC_SKIPPABLE_START 0x184D2A50 /* all 16 values, from 0x184D2A50 to 0x184D2A5F, signal the beginning of a skippable frame */ | |
91 | #define ZSTD_MAGIC_SKIPPABLE_MASK 0xFFFFFFF0 | |
92 | ||
93 | #define ZSTD_BLOCKSIZELOG_MAX 17 | |
94 | #define ZSTD_BLOCKSIZE_MAX (1<<ZSTD_BLOCKSIZELOG_MAX) | |
95 | ||
96 | ||
97 | ||
98 | /* ************************************* | |
99 | * Simple API | |
100 | ***************************************/ | |
101 | /*! ZSTD_compress() : | |
102 | * Compresses `src` content as a single zstd compressed frame into already allocated `dst`. | |
103 | * Hint : compression runs faster if `dstCapacity` >= `ZSTD_compressBound(srcSize)`. | |
104 | * @return : compressed size written into `dst` (<= `dstCapacity), | |
105 | * or an error code if it fails (which can be tested using ZSTD_isError()). */ | |
106 | ZSTDLIB_API size_t ZSTD_compress( void* dst, size_t dstCapacity, | |
107 | const void* src, size_t srcSize, | |
108 | int compressionLevel); | |
109 | ||
110 | /*! ZSTD_decompress() : | |
111 | * `compressedSize` : must be the _exact_ size of some number of compressed and/or skippable frames. | |
112 | * `dstCapacity` is an upper bound of originalSize to regenerate. | |
113 | * If user cannot imply a maximum upper bound, it's better to use streaming mode to decompress data. | |
114 | * @return : the number of bytes decompressed into `dst` (<= `dstCapacity`), | |
115 | * or an errorCode if it fails (which can be tested using ZSTD_isError()). */ | |
116 | ZSTDLIB_API size_t ZSTD_decompress( void* dst, size_t dstCapacity, | |
117 | const void* src, size_t compressedSize); | |
118 | ||
119 | /*! ZSTD_getFrameContentSize() : requires v1.3.0+ | |
120 | * `src` should point to the start of a ZSTD encoded frame. | |
121 | * `srcSize` must be at least as large as the frame header. | |
122 | * hint : any size >= `ZSTD_frameHeaderSize_max` is large enough. | |
123 | * @return : - decompressed size of `src` frame content, if known | |
124 | * - ZSTD_CONTENTSIZE_UNKNOWN if the size cannot be determined | |
125 | * - ZSTD_CONTENTSIZE_ERROR if an error occurred (e.g. invalid magic number, srcSize too small) | |
126 | * note 1 : a 0 return value means the frame is valid but "empty". | |
127 | * note 2 : decompressed size is an optional field, it may not be present, typically in streaming mode. | |
128 | * When `return==ZSTD_CONTENTSIZE_UNKNOWN`, data to decompress could be any size. | |
129 | * In which case, it's necessary to use streaming mode to decompress data. | |
130 | * Optionally, application can rely on some implicit limit, | |
131 | * as ZSTD_decompress() only needs an upper bound of decompressed size. | |
132 | * (For example, data could be necessarily cut into blocks <= 16 KB). | |
133 | * note 3 : decompressed size is always present when compression is completed using single-pass functions, | |
134 | * such as ZSTD_compress(), ZSTD_compressCCtx() ZSTD_compress_usingDict() or ZSTD_compress_usingCDict(). | |
135 | * note 4 : decompressed size can be very large (64-bits value), | |
136 | * potentially larger than what local system can handle as a single memory segment. | |
137 | * In which case, it's necessary to use streaming mode to decompress data. | |
138 | * note 5 : If source is untrusted, decompressed size could be wrong or intentionally modified. | |
139 | * Always ensure return value fits within application's authorized limits. | |
140 | * Each application can set its own limits. | |
141 | * note 6 : This function replaces ZSTD_getDecompressedSize() */ | |
142 | #define ZSTD_CONTENTSIZE_UNKNOWN (0ULL - 1) | |
143 | #define ZSTD_CONTENTSIZE_ERROR (0ULL - 2) | |
144 | ZSTDLIB_API unsigned long long ZSTD_getFrameContentSize(const void *src, size_t srcSize); | |
145 | ||
146 | /*! ZSTD_getDecompressedSize() : | |
147 | * NOTE: This function is now obsolete, in favor of ZSTD_getFrameContentSize(). | |
148 | * Both functions work the same way, but ZSTD_getDecompressedSize() blends | |
149 | * "empty", "unknown" and "error" results to the same return value (0), | |
150 | * while ZSTD_getFrameContentSize() gives them separate return values. | |
151 | * @return : decompressed size of `src` frame content _if known and not empty_, 0 otherwise. */ | |
152 | ZSTDLIB_API unsigned long long ZSTD_getDecompressedSize(const void* src, size_t srcSize); | |
153 | ||
154 | /*! ZSTD_findFrameCompressedSize() : | |
155 | * `src` should point to the start of a ZSTD frame or skippable frame. | |
156 | * `srcSize` must be >= first frame size | |
157 | * @return : the compressed size of the first frame starting at `src`, | |
158 | * suitable to pass as `srcSize` to `ZSTD_decompress` or similar, | |
159 | * or an error code if input is invalid */ | |
160 | ZSTDLIB_API size_t ZSTD_findFrameCompressedSize(const void* src, size_t srcSize); | |
161 | ||
cf30f6a5 NT |
162 | |
163 | /*====== Helper functions ======*/ | |
e0c1b49f NT |
164 | #define ZSTD_COMPRESSBOUND(srcSize) ((srcSize) + ((srcSize)>>8) + (((srcSize) < (128<<10)) ? (((128<<10) - (srcSize)) >> 11) /* margin, from 64 to 0 */ : 0)) /* this formula ensures that bound(A) + bound(B) <= bound(A+B) as long as A and B >= 128 KB */ |
165 | ZSTDLIB_API size_t ZSTD_compressBound(size_t srcSize); /*!< maximum compressed size in worst case single-pass scenario */ | |
166 | ZSTDLIB_API unsigned ZSTD_isError(size_t code); /*!< tells if a `size_t` function result is an error code */ | |
167 | ZSTDLIB_API const char* ZSTD_getErrorName(size_t code); /*!< provides readable string from an error code */ | |
168 | ZSTDLIB_API int ZSTD_minCLevel(void); /*!< minimum negative compression level allowed */ | |
169 | ZSTDLIB_API int ZSTD_maxCLevel(void); /*!< maximum compression level available */ | |
170 | ||
171 | ||
172 | /* ************************************* | |
173 | * Explicit context | |
174 | ***************************************/ | |
175 | /*= Compression context | |
176 | * When compressing many times, | |
177 | * it is recommended to allocate a context just once, | |
178 | * and re-use it for each successive compression operation. | |
179 | * This will make workload friendlier for system's memory. | |
180 | * Note : re-using context is just a speed / resource optimization. | |
181 | * It doesn't change the compression ratio, which remains identical. | |
182 | * Note 2 : In multi-threaded environments, | |
183 | * use one different context per thread for parallel execution. | |
184 | */ | |
185 | typedef struct ZSTD_CCtx_s ZSTD_CCtx; | |
186 | ZSTDLIB_API ZSTD_CCtx* ZSTD_createCCtx(void); | |
187 | ZSTDLIB_API size_t ZSTD_freeCCtx(ZSTD_CCtx* cctx); /* accept NULL pointer */ | |
188 | ||
189 | /*! ZSTD_compressCCtx() : | |
190 | * Same as ZSTD_compress(), using an explicit ZSTD_CCtx. | |
191 | * Important : in order to behave similarly to `ZSTD_compress()`, | |
192 | * this function compresses at requested compression level, | |
193 | * __ignoring any other parameter__ . | |
194 | * If any advanced parameter was set using the advanced API, | |
195 | * they will all be reset. Only `compressionLevel` remains. | |
196 | */ | |
197 | ZSTDLIB_API size_t ZSTD_compressCCtx(ZSTD_CCtx* cctx, | |
198 | void* dst, size_t dstCapacity, | |
199 | const void* src, size_t srcSize, | |
200 | int compressionLevel); | |
201 | ||
202 | /*= Decompression context | |
203 | * When decompressing many times, | |
204 | * it is recommended to allocate a context only once, | |
205 | * and re-use it for each successive compression operation. | |
206 | * This will make workload friendlier for system's memory. | |
207 | * Use one context per thread for parallel execution. */ | |
208 | typedef struct ZSTD_DCtx_s ZSTD_DCtx; | |
209 | ZSTDLIB_API ZSTD_DCtx* ZSTD_createDCtx(void); | |
210 | ZSTDLIB_API size_t ZSTD_freeDCtx(ZSTD_DCtx* dctx); /* accept NULL pointer */ | |
211 | ||
212 | /*! ZSTD_decompressDCtx() : | |
213 | * Same as ZSTD_decompress(), | |
214 | * requires an allocated ZSTD_DCtx. | |
215 | * Compatible with sticky parameters. | |
216 | */ | |
217 | ZSTDLIB_API size_t ZSTD_decompressDCtx(ZSTD_DCtx* dctx, | |
218 | void* dst, size_t dstCapacity, | |
219 | const void* src, size_t srcSize); | |
cf30f6a5 | 220 | |
e0c1b49f NT |
221 | |
222 | /* ************************************* | |
223 | * Advanced compression API | |
224 | ***************************************/ | |
225 | ||
226 | /* API design : | |
227 | * Parameters are pushed one by one into an existing context, | |
228 | * using ZSTD_CCtx_set*() functions. | |
229 | * Pushed parameters are sticky : they are valid for next compressed frame, and any subsequent frame. | |
230 | * "sticky" parameters are applicable to `ZSTD_compress2()` and `ZSTD_compressStream*()` ! | |
231 | * __They do not apply to "simple" one-shot variants such as ZSTD_compressCCtx()__ . | |
232 | * | |
233 | * It's possible to reset all parameters to "default" using ZSTD_CCtx_reset(). | |
cf30f6a5 | 234 | * |
e0c1b49f NT |
235 | * This API supercedes all other "advanced" API entry points in the experimental section. |
236 | * In the future, we expect to remove from experimental API entry points which are redundant with this API. | |
237 | */ | |
238 | ||
239 | ||
240 | /* Compression strategies, listed from fastest to strongest */ | |
241 | typedef enum { ZSTD_fast=1, | |
242 | ZSTD_dfast=2, | |
243 | ZSTD_greedy=3, | |
244 | ZSTD_lazy=4, | |
245 | ZSTD_lazy2=5, | |
246 | ZSTD_btlazy2=6, | |
247 | ZSTD_btopt=7, | |
248 | ZSTD_btultra=8, | |
249 | ZSTD_btultra2=9 | |
250 | /* note : new strategies _might_ be added in the future. | |
251 | Only the order (from fast to strong) is guaranteed */ | |
252 | } ZSTD_strategy; | |
253 | ||
254 | ||
255 | typedef enum { | |
256 | ||
257 | /* compression parameters | |
258 | * Note: When compressing with a ZSTD_CDict these parameters are superseded | |
259 | * by the parameters used to construct the ZSTD_CDict. | |
260 | * See ZSTD_CCtx_refCDict() for more info (superseded-by-cdict). */ | |
261 | ZSTD_c_compressionLevel=100, /* Set compression parameters according to pre-defined cLevel table. | |
262 | * Note that exact compression parameters are dynamically determined, | |
263 | * depending on both compression level and srcSize (when known). | |
264 | * Default level is ZSTD_CLEVEL_DEFAULT==3. | |
265 | * Special: value 0 means default, which is controlled by ZSTD_CLEVEL_DEFAULT. | |
266 | * Note 1 : it's possible to pass a negative compression level. | |
267 | * Note 2 : setting a level does not automatically set all other compression parameters | |
268 | * to default. Setting this will however eventually dynamically impact the compression | |
269 | * parameters which have not been manually set. The manually set | |
270 | * ones will 'stick'. */ | |
271 | /* Advanced compression parameters : | |
272 | * It's possible to pin down compression parameters to some specific values. | |
273 | * In which case, these values are no longer dynamically selected by the compressor */ | |
274 | ZSTD_c_windowLog=101, /* Maximum allowed back-reference distance, expressed as power of 2. | |
275 | * This will set a memory budget for streaming decompression, | |
276 | * with larger values requiring more memory | |
277 | * and typically compressing more. | |
278 | * Must be clamped between ZSTD_WINDOWLOG_MIN and ZSTD_WINDOWLOG_MAX. | |
279 | * Special: value 0 means "use default windowLog". | |
280 | * Note: Using a windowLog greater than ZSTD_WINDOWLOG_LIMIT_DEFAULT | |
281 | * requires explicitly allowing such size at streaming decompression stage. */ | |
282 | ZSTD_c_hashLog=102, /* Size of the initial probe table, as a power of 2. | |
283 | * Resulting memory usage is (1 << (hashLog+2)). | |
284 | * Must be clamped between ZSTD_HASHLOG_MIN and ZSTD_HASHLOG_MAX. | |
285 | * Larger tables improve compression ratio of strategies <= dFast, | |
286 | * and improve speed of strategies > dFast. | |
287 | * Special: value 0 means "use default hashLog". */ | |
288 | ZSTD_c_chainLog=103, /* Size of the multi-probe search table, as a power of 2. | |
289 | * Resulting memory usage is (1 << (chainLog+2)). | |
290 | * Must be clamped between ZSTD_CHAINLOG_MIN and ZSTD_CHAINLOG_MAX. | |
291 | * Larger tables result in better and slower compression. | |
292 | * This parameter is useless for "fast" strategy. | |
293 | * It's still useful when using "dfast" strategy, | |
294 | * in which case it defines a secondary probe table. | |
295 | * Special: value 0 means "use default chainLog". */ | |
296 | ZSTD_c_searchLog=104, /* Number of search attempts, as a power of 2. | |
297 | * More attempts result in better and slower compression. | |
298 | * This parameter is useless for "fast" and "dFast" strategies. | |
299 | * Special: value 0 means "use default searchLog". */ | |
300 | ZSTD_c_minMatch=105, /* Minimum size of searched matches. | |
301 | * Note that Zstandard can still find matches of smaller size, | |
302 | * it just tweaks its search algorithm to look for this size and larger. | |
303 | * Larger values increase compression and decompression speed, but decrease ratio. | |
304 | * Must be clamped between ZSTD_MINMATCH_MIN and ZSTD_MINMATCH_MAX. | |
305 | * Note that currently, for all strategies < btopt, effective minimum is 4. | |
306 | * , for all strategies > fast, effective maximum is 6. | |
307 | * Special: value 0 means "use default minMatchLength". */ | |
308 | ZSTD_c_targetLength=106, /* Impact of this field depends on strategy. | |
309 | * For strategies btopt, btultra & btultra2: | |
310 | * Length of Match considered "good enough" to stop search. | |
311 | * Larger values make compression stronger, and slower. | |
312 | * For strategy fast: | |
313 | * Distance between match sampling. | |
314 | * Larger values make compression faster, and weaker. | |
315 | * Special: value 0 means "use default targetLength". */ | |
316 | ZSTD_c_strategy=107, /* See ZSTD_strategy enum definition. | |
317 | * The higher the value of selected strategy, the more complex it is, | |
318 | * resulting in stronger and slower compression. | |
319 | * Special: value 0 means "use default strategy". */ | |
320 | ||
321 | /* LDM mode parameters */ | |
322 | ZSTD_c_enableLongDistanceMatching=160, /* Enable long distance matching. | |
323 | * This parameter is designed to improve compression ratio | |
324 | * for large inputs, by finding large matches at long distance. | |
325 | * It increases memory usage and window size. | |
326 | * Note: enabling this parameter increases default ZSTD_c_windowLog to 128 MB | |
327 | * except when expressly set to a different value. | |
328 | * Note: will be enabled by default if ZSTD_c_windowLog >= 128 MB and | |
329 | * compression strategy >= ZSTD_btopt (== compression level 16+) */ | |
330 | ZSTD_c_ldmHashLog=161, /* Size of the table for long distance matching, as a power of 2. | |
331 | * Larger values increase memory usage and compression ratio, | |
332 | * but decrease compression speed. | |
333 | * Must be clamped between ZSTD_HASHLOG_MIN and ZSTD_HASHLOG_MAX | |
334 | * default: windowlog - 7. | |
335 | * Special: value 0 means "automatically determine hashlog". */ | |
336 | ZSTD_c_ldmMinMatch=162, /* Minimum match size for long distance matcher. | |
337 | * Larger/too small values usually decrease compression ratio. | |
338 | * Must be clamped between ZSTD_LDM_MINMATCH_MIN and ZSTD_LDM_MINMATCH_MAX. | |
339 | * Special: value 0 means "use default value" (default: 64). */ | |
340 | ZSTD_c_ldmBucketSizeLog=163, /* Log size of each bucket in the LDM hash table for collision resolution. | |
341 | * Larger values improve collision resolution but decrease compression speed. | |
342 | * The maximum value is ZSTD_LDM_BUCKETSIZELOG_MAX. | |
343 | * Special: value 0 means "use default value" (default: 3). */ | |
344 | ZSTD_c_ldmHashRateLog=164, /* Frequency of inserting/looking up entries into the LDM hash table. | |
345 | * Must be clamped between 0 and (ZSTD_WINDOWLOG_MAX - ZSTD_HASHLOG_MIN). | |
346 | * Default is MAX(0, (windowLog - ldmHashLog)), optimizing hash table usage. | |
347 | * Larger values improve compression speed. | |
348 | * Deviating far from default value will likely result in a compression ratio decrease. | |
349 | * Special: value 0 means "automatically determine hashRateLog". */ | |
350 | ||
351 | /* frame parameters */ | |
352 | ZSTD_c_contentSizeFlag=200, /* Content size will be written into frame header _whenever known_ (default:1) | |
353 | * Content size must be known at the beginning of compression. | |
354 | * This is automatically the case when using ZSTD_compress2(), | |
355 | * For streaming scenarios, content size must be provided with ZSTD_CCtx_setPledgedSrcSize() */ | |
356 | ZSTD_c_checksumFlag=201, /* A 32-bits checksum of content is written at end of frame (default:0) */ | |
357 | ZSTD_c_dictIDFlag=202, /* When applicable, dictionary's ID is written into frame header (default:1) */ | |
358 | ||
359 | /* multi-threading parameters */ | |
360 | /* These parameters are only active if multi-threading is enabled (compiled with build macro ZSTD_MULTITHREAD). | |
361 | * Otherwise, trying to set any other value than default (0) will be a no-op and return an error. | |
362 | * In a situation where it's unknown if the linked library supports multi-threading or not, | |
363 | * setting ZSTD_c_nbWorkers to any value >= 1 and consulting the return value provides a quick way to check this property. | |
364 | */ | |
365 | ZSTD_c_nbWorkers=400, /* Select how many threads will be spawned to compress in parallel. | |
366 | * When nbWorkers >= 1, triggers asynchronous mode when invoking ZSTD_compressStream*() : | |
367 | * ZSTD_compressStream*() consumes input and flush output if possible, but immediately gives back control to caller, | |
368 | * while compression is performed in parallel, within worker thread(s). | |
369 | * (note : a strong exception to this rule is when first invocation of ZSTD_compressStream2() sets ZSTD_e_end : | |
370 | * in which case, ZSTD_compressStream2() delegates to ZSTD_compress2(), which is always a blocking call). | |
371 | * More workers improve speed, but also increase memory usage. | |
372 | * Default value is `0`, aka "single-threaded mode" : no worker is spawned, | |
373 | * compression is performed inside Caller's thread, and all invocations are blocking */ | |
374 | ZSTD_c_jobSize=401, /* Size of a compression job. This value is enforced only when nbWorkers >= 1. | |
375 | * Each compression job is completed in parallel, so this value can indirectly impact the nb of active threads. | |
376 | * 0 means default, which is dynamically determined based on compression parameters. | |
377 | * Job size must be a minimum of overlap size, or 1 MB, whichever is largest. | |
378 | * The minimum size is automatically and transparently enforced. */ | |
379 | ZSTD_c_overlapLog=402, /* Control the overlap size, as a fraction of window size. | |
380 | * The overlap size is an amount of data reloaded from previous job at the beginning of a new job. | |
381 | * It helps preserve compression ratio, while each job is compressed in parallel. | |
382 | * This value is enforced only when nbWorkers >= 1. | |
383 | * Larger values increase compression ratio, but decrease speed. | |
384 | * Possible values range from 0 to 9 : | |
385 | * - 0 means "default" : value will be determined by the library, depending on strategy | |
386 | * - 1 means "no overlap" | |
387 | * - 9 means "full overlap", using a full window size. | |
388 | * Each intermediate rank increases/decreases load size by a factor 2 : | |
389 | * 9: full window; 8: w/2; 7: w/4; 6: w/8; 5:w/16; 4: w/32; 3:w/64; 2:w/128; 1:no overlap; 0:default | |
390 | * default value varies between 6 and 9, depending on strategy */ | |
391 | ||
392 | /* note : additional experimental parameters are also available | |
393 | * within the experimental section of the API. | |
394 | * At the time of this writing, they include : | |
395 | * ZSTD_c_rsyncable | |
396 | * ZSTD_c_format | |
397 | * ZSTD_c_forceMaxWindow | |
398 | * ZSTD_c_forceAttachDict | |
399 | * ZSTD_c_literalCompressionMode | |
400 | * ZSTD_c_targetCBlockSize | |
401 | * ZSTD_c_srcSizeHint | |
402 | * ZSTD_c_enableDedicatedDictSearch | |
403 | * ZSTD_c_stableInBuffer | |
404 | * ZSTD_c_stableOutBuffer | |
405 | * ZSTD_c_blockDelimiters | |
406 | * ZSTD_c_validateSequences | |
407 | * Because they are not stable, it's necessary to define ZSTD_STATIC_LINKING_ONLY to access them. | |
408 | * note : never ever use experimentalParam? names directly; | |
409 | * also, the enums values themselves are unstable and can still change. | |
410 | */ | |
411 | ZSTD_c_experimentalParam1=500, | |
412 | ZSTD_c_experimentalParam2=10, | |
413 | ZSTD_c_experimentalParam3=1000, | |
414 | ZSTD_c_experimentalParam4=1001, | |
415 | ZSTD_c_experimentalParam5=1002, | |
416 | ZSTD_c_experimentalParam6=1003, | |
417 | ZSTD_c_experimentalParam7=1004, | |
418 | ZSTD_c_experimentalParam8=1005, | |
419 | ZSTD_c_experimentalParam9=1006, | |
420 | ZSTD_c_experimentalParam10=1007, | |
421 | ZSTD_c_experimentalParam11=1008, | |
422 | ZSTD_c_experimentalParam12=1009 | |
423 | } ZSTD_cParameter; | |
424 | ||
425 | typedef struct { | |
426 | size_t error; | |
427 | int lowerBound; | |
428 | int upperBound; | |
429 | } ZSTD_bounds; | |
430 | ||
431 | /*! ZSTD_cParam_getBounds() : | |
432 | * All parameters must belong to an interval with lower and upper bounds, | |
433 | * otherwise they will either trigger an error or be automatically clamped. | |
434 | * @return : a structure, ZSTD_bounds, which contains | |
435 | * - an error status field, which must be tested using ZSTD_isError() | |
436 | * - lower and upper bounds, both inclusive | |
437 | */ | |
438 | ZSTDLIB_API ZSTD_bounds ZSTD_cParam_getBounds(ZSTD_cParameter cParam); | |
439 | ||
440 | /*! ZSTD_CCtx_setParameter() : | |
441 | * Set one compression parameter, selected by enum ZSTD_cParameter. | |
442 | * All parameters have valid bounds. Bounds can be queried using ZSTD_cParam_getBounds(). | |
443 | * Providing a value beyond bound will either clamp it, or trigger an error (depending on parameter). | |
444 | * Setting a parameter is generally only possible during frame initialization (before starting compression). | |
445 | * Exception : when using multi-threading mode (nbWorkers >= 1), | |
446 | * the following parameters can be updated _during_ compression (within same frame): | |
447 | * => compressionLevel, hashLog, chainLog, searchLog, minMatch, targetLength and strategy. | |
448 | * new parameters will be active for next job only (after a flush()). | |
449 | * @return : an error code (which can be tested using ZSTD_isError()). | |
cf30f6a5 | 450 | */ |
e0c1b49f NT |
451 | ZSTDLIB_API size_t ZSTD_CCtx_setParameter(ZSTD_CCtx* cctx, ZSTD_cParameter param, int value); |
452 | ||
453 | /*! ZSTD_CCtx_setPledgedSrcSize() : | |
454 | * Total input data size to be compressed as a single frame. | |
455 | * Value will be written in frame header, unless if explicitly forbidden using ZSTD_c_contentSizeFlag. | |
456 | * This value will also be controlled at end of frame, and trigger an error if not respected. | |
457 | * @result : 0, or an error code (which can be tested with ZSTD_isError()). | |
458 | * Note 1 : pledgedSrcSize==0 actually means zero, aka an empty frame. | |
459 | * In order to mean "unknown content size", pass constant ZSTD_CONTENTSIZE_UNKNOWN. | |
460 | * ZSTD_CONTENTSIZE_UNKNOWN is default value for any new frame. | |
461 | * Note 2 : pledgedSrcSize is only valid once, for the next frame. | |
462 | * It's discarded at the end of the frame, and replaced by ZSTD_CONTENTSIZE_UNKNOWN. | |
463 | * Note 3 : Whenever all input data is provided and consumed in a single round, | |
464 | * for example with ZSTD_compress2(), | |
465 | * or invoking immediately ZSTD_compressStream2(,,,ZSTD_e_end), | |
466 | * this value is automatically overridden by srcSize instead. | |
467 | */ | |
468 | ZSTDLIB_API size_t ZSTD_CCtx_setPledgedSrcSize(ZSTD_CCtx* cctx, unsigned long long pledgedSrcSize); | |
469 | ||
cf30f6a5 | 470 | typedef enum { |
e0c1b49f NT |
471 | ZSTD_reset_session_only = 1, |
472 | ZSTD_reset_parameters = 2, | |
473 | ZSTD_reset_session_and_parameters = 3 | |
474 | } ZSTD_ResetDirective; | |
475 | ||
476 | /*! ZSTD_CCtx_reset() : | |
477 | * There are 2 different things that can be reset, independently or jointly : | |
478 | * - The session : will stop compressing current frame, and make CCtx ready to start a new one. | |
479 | * Useful after an error, or to interrupt any ongoing compression. | |
480 | * Any internal data not yet flushed is cancelled. | |
481 | * Compression parameters and dictionary remain unchanged. | |
482 | * They will be used to compress next frame. | |
483 | * Resetting session never fails. | |
484 | * - The parameters : changes all parameters back to "default". | |
485 | * This removes any reference to any dictionary too. | |
486 | * Parameters can only be changed between 2 sessions (i.e. no compression is currently ongoing) | |
487 | * otherwise the reset fails, and function returns an error value (which can be tested using ZSTD_isError()) | |
488 | * - Both : similar to resetting the session, followed by resetting parameters. | |
cf30f6a5 | 489 | */ |
e0c1b49f NT |
490 | ZSTDLIB_API size_t ZSTD_CCtx_reset(ZSTD_CCtx* cctx, ZSTD_ResetDirective reset); |
491 | ||
492 | /*! ZSTD_compress2() : | |
493 | * Behave the same as ZSTD_compressCCtx(), but compression parameters are set using the advanced API. | |
494 | * ZSTD_compress2() always starts a new frame. | |
495 | * Should cctx hold data from a previously unfinished frame, everything about it is forgotten. | |
496 | * - Compression parameters are pushed into CCtx before starting compression, using ZSTD_CCtx_set*() | |
497 | * - The function is always blocking, returns when compression is completed. | |
498 | * Hint : compression runs faster if `dstCapacity` >= `ZSTD_compressBound(srcSize)`. | |
499 | * @return : compressed size written into `dst` (<= `dstCapacity), | |
500 | * or an error code if it fails (which can be tested using ZSTD_isError()). | |
501 | */ | |
502 | ZSTDLIB_API size_t ZSTD_compress2( ZSTD_CCtx* cctx, | |
503 | void* dst, size_t dstCapacity, | |
504 | const void* src, size_t srcSize); | |
505 | ||
506 | ||
507 | /* ************************************* | |
508 | * Advanced decompression API | |
509 | ***************************************/ | |
510 | ||
511 | /* The advanced API pushes parameters one by one into an existing DCtx context. | |
512 | * Parameters are sticky, and remain valid for all following frames | |
513 | * using the same DCtx context. | |
514 | * It's possible to reset parameters to default values using ZSTD_DCtx_reset(). | |
515 | * Note : This API is compatible with existing ZSTD_decompressDCtx() and ZSTD_decompressStream(). | |
516 | * Therefore, no new decompression function is necessary. | |
517 | */ | |
518 | ||
cf30f6a5 | 519 | typedef enum { |
cf30f6a5 | 520 | |
e0c1b49f NT |
521 | ZSTD_d_windowLogMax=100, /* Select a size limit (in power of 2) beyond which |
522 | * the streaming API will refuse to allocate memory buffer | |
523 | * in order to protect the host from unreasonable memory requirements. | |
524 | * This parameter is only useful in streaming mode, since no internal buffer is allocated in single-pass mode. | |
525 | * By default, a decompression context accepts window sizes <= (1 << ZSTD_WINDOWLOG_LIMIT_DEFAULT). | |
526 | * Special: value 0 means "use default maximum windowLog". */ | |
527 | ||
528 | /* note : additional experimental parameters are also available | |
529 | * within the experimental section of the API. | |
530 | * At the time of this writing, they include : | |
531 | * ZSTD_d_format | |
532 | * ZSTD_d_stableOutBuffer | |
533 | * ZSTD_d_forceIgnoreChecksum | |
534 | * ZSTD_d_refMultipleDDicts | |
535 | * Because they are not stable, it's necessary to define ZSTD_STATIC_LINKING_ONLY to access them. | |
536 | * note : never ever use experimentalParam? names directly | |
537 | */ | |
538 | ZSTD_d_experimentalParam1=1000, | |
539 | ZSTD_d_experimentalParam2=1001, | |
540 | ZSTD_d_experimentalParam3=1002, | |
541 | ZSTD_d_experimentalParam4=1003 | |
542 | ||
543 | } ZSTD_dParameter; | |
544 | ||
545 | /*! ZSTD_dParam_getBounds() : | |
546 | * All parameters must belong to an interval with lower and upper bounds, | |
547 | * otherwise they will either trigger an error or be automatically clamped. | |
548 | * @return : a structure, ZSTD_bounds, which contains | |
549 | * - an error status field, which must be tested using ZSTD_isError() | |
550 | * - both lower and upper bounds, inclusive | |
cf30f6a5 | 551 | */ |
e0c1b49f NT |
552 | ZSTDLIB_API ZSTD_bounds ZSTD_dParam_getBounds(ZSTD_dParameter dParam); |
553 | ||
554 | /*! ZSTD_DCtx_setParameter() : | |
555 | * Set one compression parameter, selected by enum ZSTD_dParameter. | |
556 | * All parameters have valid bounds. Bounds can be queried using ZSTD_dParam_getBounds(). | |
557 | * Providing a value beyond bound will either clamp it, or trigger an error (depending on parameter). | |
558 | * Setting a parameter is only possible during frame initialization (before starting decompression). | |
559 | * @return : 0, or an error code (which can be tested using ZSTD_isError()). | |
560 | */ | |
561 | ZSTDLIB_API size_t ZSTD_DCtx_setParameter(ZSTD_DCtx* dctx, ZSTD_dParameter param, int value); | |
562 | ||
563 | /*! ZSTD_DCtx_reset() : | |
564 | * Return a DCtx to clean state. | |
565 | * Session and parameters can be reset jointly or separately. | |
566 | * Parameters can only be reset when no active frame is being decompressed. | |
567 | * @return : 0, or an error code, which can be tested with ZSTD_isError() | |
568 | */ | |
569 | ZSTDLIB_API size_t ZSTD_DCtx_reset(ZSTD_DCtx* dctx, ZSTD_ResetDirective reset); | |
570 | ||
571 | ||
572 | /* ************************** | |
573 | * Streaming | |
574 | ****************************/ | |
575 | ||
576 | typedef struct ZSTD_inBuffer_s { | |
577 | const void* src; /*< start of input buffer */ | |
578 | size_t size; /*< size of input buffer */ | |
579 | size_t pos; /*< position where reading stopped. Will be updated. Necessarily 0 <= pos <= size */ | |
580 | } ZSTD_inBuffer; | |
581 | ||
582 | typedef struct ZSTD_outBuffer_s { | |
583 | void* dst; /*< start of output buffer */ | |
584 | size_t size; /*< size of output buffer */ | |
585 | size_t pos; /*< position where writing stopped. Will be updated. Necessarily 0 <= pos <= size */ | |
586 | } ZSTD_outBuffer; | |
587 | ||
cf30f6a5 | 588 | |
e0c1b49f NT |
589 | |
590 | /*-*********************************************************************** | |
591 | * Streaming compression - HowTo | |
592 | * | |
593 | * A ZSTD_CStream object is required to track streaming operation. | |
594 | * Use ZSTD_createCStream() and ZSTD_freeCStream() to create/release resources. | |
595 | * ZSTD_CStream objects can be reused multiple times on consecutive compression operations. | |
596 | * It is recommended to re-use ZSTD_CStream since it will play nicer with system's memory, by re-using already allocated memory. | |
597 | * | |
598 | * For parallel execution, use one separate ZSTD_CStream per thread. | |
599 | * | |
600 | * note : since v1.3.0, ZSTD_CStream and ZSTD_CCtx are the same thing. | |
601 | * | |
602 | * Parameters are sticky : when starting a new compression on the same context, | |
603 | * it will re-use the same sticky parameters as previous compression session. | |
604 | * When in doubt, it's recommended to fully initialize the context before usage. | |
605 | * Use ZSTD_CCtx_reset() to reset the context and ZSTD_CCtx_setParameter(), | |
606 | * ZSTD_CCtx_setPledgedSrcSize(), or ZSTD_CCtx_loadDictionary() and friends to | |
607 | * set more specific parameters, the pledged source size, or load a dictionary. | |
608 | * | |
609 | * Use ZSTD_compressStream2() with ZSTD_e_continue as many times as necessary to | |
610 | * consume input stream. The function will automatically update both `pos` | |
611 | * fields within `input` and `output`. | |
612 | * Note that the function may not consume the entire input, for example, because | |
613 | * the output buffer is already full, in which case `input.pos < input.size`. | |
614 | * The caller must check if input has been entirely consumed. | |
615 | * If not, the caller must make some room to receive more compressed data, | |
616 | * and then present again remaining input data. | |
617 | * note: ZSTD_e_continue is guaranteed to make some forward progress when called, | |
618 | * but doesn't guarantee maximal forward progress. This is especially relevant | |
619 | * when compressing with multiple threads. The call won't block if it can | |
620 | * consume some input, but if it can't it will wait for some, but not all, | |
621 | * output to be flushed. | |
622 | * @return : provides a minimum amount of data remaining to be flushed from internal buffers | |
623 | * or an error code, which can be tested using ZSTD_isError(). | |
624 | * | |
625 | * At any moment, it's possible to flush whatever data might remain stuck within internal buffer, | |
626 | * using ZSTD_compressStream2() with ZSTD_e_flush. `output->pos` will be updated. | |
627 | * Note that, if `output->size` is too small, a single invocation with ZSTD_e_flush might not be enough (return code > 0). | |
628 | * In which case, make some room to receive more compressed data, and call again ZSTD_compressStream2() with ZSTD_e_flush. | |
629 | * You must continue calling ZSTD_compressStream2() with ZSTD_e_flush until it returns 0, at which point you can change the | |
630 | * operation. | |
631 | * note: ZSTD_e_flush will flush as much output as possible, meaning when compressing with multiple threads, it will | |
632 | * block until the flush is complete or the output buffer is full. | |
633 | * @return : 0 if internal buffers are entirely flushed, | |
634 | * >0 if some data still present within internal buffer (the value is minimal estimation of remaining size), | |
635 | * or an error code, which can be tested using ZSTD_isError(). | |
636 | * | |
637 | * Calling ZSTD_compressStream2() with ZSTD_e_end instructs to finish a frame. | |
638 | * It will perform a flush and write frame epilogue. | |
639 | * The epilogue is required for decoders to consider a frame completed. | |
640 | * flush operation is the same, and follows same rules as calling ZSTD_compressStream2() with ZSTD_e_flush. | |
641 | * You must continue calling ZSTD_compressStream2() with ZSTD_e_end until it returns 0, at which point you are free to | |
642 | * start a new frame. | |
643 | * note: ZSTD_e_end will flush as much output as possible, meaning when compressing with multiple threads, it will | |
644 | * block until the flush is complete or the output buffer is full. | |
645 | * @return : 0 if frame fully completed and fully flushed, | |
646 | * >0 if some data still present within internal buffer (the value is minimal estimation of remaining size), | |
647 | * or an error code, which can be tested using ZSTD_isError(). | |
648 | * | |
649 | * *******************************************************************/ | |
650 | ||
651 | typedef ZSTD_CCtx ZSTD_CStream; /*< CCtx and CStream are now effectively same object (>= v1.3.0) */ | |
652 | /* Continue to distinguish them for compatibility with older versions <= v1.2.0 */ | |
653 | /*===== ZSTD_CStream management functions =====*/ | |
654 | ZSTDLIB_API ZSTD_CStream* ZSTD_createCStream(void); | |
655 | ZSTDLIB_API size_t ZSTD_freeCStream(ZSTD_CStream* zcs); /* accept NULL pointer */ | |
656 | ||
657 | /*===== Streaming compression functions =====*/ | |
658 | typedef enum { | |
659 | ZSTD_e_continue=0, /* collect more data, encoder decides when to output compressed result, for optimal compression ratio */ | |
660 | ZSTD_e_flush=1, /* flush any data provided so far, | |
661 | * it creates (at least) one new block, that can be decoded immediately on reception; | |
662 | * frame will continue: any future data can still reference previously compressed data, improving compression. | |
663 | * note : multithreaded compression will block to flush as much output as possible. */ | |
664 | ZSTD_e_end=2 /* flush any remaining data _and_ close current frame. | |
665 | * note that frame is only closed after compressed data is fully flushed (return value == 0). | |
666 | * After that point, any additional data starts a new frame. | |
667 | * note : each frame is independent (does not reference any content from previous frame). | |
668 | : note : multithreaded compression will block to flush as much output as possible. */ | |
669 | } ZSTD_EndDirective; | |
670 | ||
671 | /*! ZSTD_compressStream2() : | |
672 | * Behaves about the same as ZSTD_compressStream, with additional control on end directive. | |
673 | * - Compression parameters are pushed into CCtx before starting compression, using ZSTD_CCtx_set*() | |
674 | * - Compression parameters cannot be changed once compression is started (save a list of exceptions in multi-threading mode) | |
675 | * - output->pos must be <= dstCapacity, input->pos must be <= srcSize | |
676 | * - output->pos and input->pos will be updated. They are guaranteed to remain below their respective limit. | |
677 | * - endOp must be a valid directive | |
678 | * - When nbWorkers==0 (default), function is blocking : it completes its job before returning to caller. | |
679 | * - When nbWorkers>=1, function is non-blocking : it copies a portion of input, distributes jobs to internal worker threads, flush to output whatever is available, | |
680 | * and then immediately returns, just indicating that there is some data remaining to be flushed. | |
681 | * The function nonetheless guarantees forward progress : it will return only after it reads or write at least 1+ byte. | |
682 | * - Exception : if the first call requests a ZSTD_e_end directive and provides enough dstCapacity, the function delegates to ZSTD_compress2() which is always blocking. | |
683 | * - @return provides a minimum amount of data remaining to be flushed from internal buffers | |
684 | * or an error code, which can be tested using ZSTD_isError(). | |
685 | * if @return != 0, flush is not fully completed, there is still some data left within internal buffers. | |
686 | * This is useful for ZSTD_e_flush, since in this case more flushes are necessary to empty all buffers. | |
687 | * For ZSTD_e_end, @return == 0 when internal buffers are fully flushed and frame is completed. | |
688 | * - after a ZSTD_e_end directive, if internal buffer is not fully flushed (@return != 0), | |
689 | * only ZSTD_e_end or ZSTD_e_flush operations are allowed. | |
690 | * Before starting a new compression job, or changing compression parameters, | |
691 | * it is required to fully flush internal buffers. | |
692 | */ | |
693 | ZSTDLIB_API size_t ZSTD_compressStream2( ZSTD_CCtx* cctx, | |
694 | ZSTD_outBuffer* output, | |
695 | ZSTD_inBuffer* input, | |
696 | ZSTD_EndDirective endOp); | |
697 | ||
698 | ||
699 | /* These buffer sizes are softly recommended. | |
700 | * They are not required : ZSTD_compressStream*() happily accepts any buffer size, for both input and output. | |
701 | * Respecting the recommended size just makes it a bit easier for ZSTD_compressStream*(), | |
702 | * reducing the amount of memory shuffling and buffering, resulting in minor performance savings. | |
703 | * | |
704 | * However, note that these recommendations are from the perspective of a C caller program. | |
705 | * If the streaming interface is invoked from some other language, | |
706 | * especially managed ones such as Java or Go, through a foreign function interface such as jni or cgo, | |
707 | * a major performance rule is to reduce crossing such interface to an absolute minimum. | |
708 | * It's not rare that performance ends being spent more into the interface, rather than compression itself. | |
709 | * In which cases, prefer using large buffers, as large as practical, | |
710 | * for both input and output, to reduce the nb of roundtrips. | |
711 | */ | |
712 | ZSTDLIB_API size_t ZSTD_CStreamInSize(void); /*< recommended size for input buffer */ | |
713 | ZSTDLIB_API size_t ZSTD_CStreamOutSize(void); /*< recommended size for output buffer. Guarantee to successfully flush at least one complete compressed block. */ | |
714 | ||
715 | ||
716 | /* ***************************************************************************** | |
717 | * This following is a legacy streaming API. | |
718 | * It can be replaced by ZSTD_CCtx_reset() and ZSTD_compressStream2(). | |
719 | * It is redundant, but remains fully supported. | |
720 | * Advanced parameters and dictionary compression can only be used through the | |
721 | * new API. | |
722 | ******************************************************************************/ | |
723 | ||
724 | /*! | |
725 | * Equivalent to: | |
cf30f6a5 | 726 | * |
e0c1b49f NT |
727 | * ZSTD_CCtx_reset(zcs, ZSTD_reset_session_only); |
728 | * ZSTD_CCtx_refCDict(zcs, NULL); // clear the dictionary (if any) | |
729 | * ZSTD_CCtx_setParameter(zcs, ZSTD_c_compressionLevel, compressionLevel); | |
730 | */ | |
731 | ZSTDLIB_API size_t ZSTD_initCStream(ZSTD_CStream* zcs, int compressionLevel); | |
732 | /*! | |
733 | * Alternative for ZSTD_compressStream2(zcs, output, input, ZSTD_e_continue). | |
734 | * NOTE: The return value is different. ZSTD_compressStream() returns a hint for | |
735 | * the next read size (if non-zero and not an error). ZSTD_compressStream2() | |
736 | * returns the minimum nb of bytes left to flush (if non-zero and not an error). | |
cf30f6a5 | 737 | */ |
e0c1b49f NT |
738 | ZSTDLIB_API size_t ZSTD_compressStream(ZSTD_CStream* zcs, ZSTD_outBuffer* output, ZSTD_inBuffer* input); |
739 | /*! Equivalent to ZSTD_compressStream2(zcs, output, &emptyInput, ZSTD_e_flush). */ | |
740 | ZSTDLIB_API size_t ZSTD_flushStream(ZSTD_CStream* zcs, ZSTD_outBuffer* output); | |
741 | /*! Equivalent to ZSTD_compressStream2(zcs, output, &emptyInput, ZSTD_e_end). */ | |
742 | ZSTDLIB_API size_t ZSTD_endStream(ZSTD_CStream* zcs, ZSTD_outBuffer* output); | |
743 | ||
744 | ||
745 | /*-*************************************************************************** | |
746 | * Streaming decompression - HowTo | |
747 | * | |
748 | * A ZSTD_DStream object is required to track streaming operations. | |
749 | * Use ZSTD_createDStream() and ZSTD_freeDStream() to create/release resources. | |
750 | * ZSTD_DStream objects can be re-used multiple times. | |
751 | * | |
752 | * Use ZSTD_initDStream() to start a new decompression operation. | |
753 | * @return : recommended first input size | |
754 | * Alternatively, use advanced API to set specific properties. | |
755 | * | |
756 | * Use ZSTD_decompressStream() repetitively to consume your input. | |
757 | * The function will update both `pos` fields. | |
758 | * If `input.pos < input.size`, some input has not been consumed. | |
759 | * It's up to the caller to present again remaining data. | |
760 | * The function tries to flush all data decoded immediately, respecting output buffer size. | |
761 | * If `output.pos < output.size`, decoder has flushed everything it could. | |
762 | * But if `output.pos == output.size`, there might be some data left within internal buffers., | |
763 | * In which case, call ZSTD_decompressStream() again to flush whatever remains in the buffer. | |
764 | * Note : with no additional input provided, amount of data flushed is necessarily <= ZSTD_BLOCKSIZE_MAX. | |
765 | * @return : 0 when a frame is completely decoded and fully flushed, | |
766 | * or an error code, which can be tested using ZSTD_isError(), | |
767 | * or any other value > 0, which means there is still some decoding or flushing to do to complete current frame : | |
768 | * the return value is a suggested next input size (just a hint for better latency) | |
769 | * that will never request more than the remaining frame size. | |
770 | * *******************************************************************************/ | |
771 | ||
772 | typedef ZSTD_DCtx ZSTD_DStream; /*< DCtx and DStream are now effectively same object (>= v1.3.0) */ | |
773 | /* For compatibility with versions <= v1.2.0, prefer differentiating them. */ | |
774 | /*===== ZSTD_DStream management functions =====*/ | |
775 | ZSTDLIB_API ZSTD_DStream* ZSTD_createDStream(void); | |
776 | ZSTDLIB_API size_t ZSTD_freeDStream(ZSTD_DStream* zds); /* accept NULL pointer */ | |
777 | ||
778 | /*===== Streaming decompression functions =====*/ | |
779 | ||
780 | /* This function is redundant with the advanced API and equivalent to: | |
781 | * | |
782 | * ZSTD_DCtx_reset(zds, ZSTD_reset_session_only); | |
783 | * ZSTD_DCtx_refDDict(zds, NULL); | |
784 | */ | |
785 | ZSTDLIB_API size_t ZSTD_initDStream(ZSTD_DStream* zds); | |
786 | ||
787 | ZSTDLIB_API size_t ZSTD_decompressStream(ZSTD_DStream* zds, ZSTD_outBuffer* output, ZSTD_inBuffer* input); | |
788 | ||
789 | ZSTDLIB_API size_t ZSTD_DStreamInSize(void); /*!< recommended size for input buffer */ | |
790 | ZSTDLIB_API size_t ZSTD_DStreamOutSize(void); /*!< recommended size for output buffer. Guarantee to successfully flush at least one complete block in all circumstances. */ | |
791 | ||
792 | ||
793 | /* ************************ | |
794 | * Simple dictionary API | |
795 | ***************************/ | |
796 | /*! ZSTD_compress_usingDict() : | |
797 | * Compression at an explicit compression level using a Dictionary. | |
798 | * A dictionary can be any arbitrary data segment (also called a prefix), | |
799 | * or a buffer with specified information (see dictBuilder/zdict.h). | |
800 | * Note : This function loads the dictionary, resulting in significant startup delay. | |
801 | * It's intended for a dictionary used only once. | |
802 | * Note 2 : When `dict == NULL || dictSize < 8` no dictionary is used. */ | |
803 | ZSTDLIB_API size_t ZSTD_compress_usingDict(ZSTD_CCtx* ctx, | |
804 | void* dst, size_t dstCapacity, | |
805 | const void* src, size_t srcSize, | |
806 | const void* dict,size_t dictSize, | |
807 | int compressionLevel); | |
808 | ||
809 | /*! ZSTD_decompress_usingDict() : | |
810 | * Decompression using a known Dictionary. | |
811 | * Dictionary must be identical to the one used during compression. | |
812 | * Note : This function loads the dictionary, resulting in significant startup delay. | |
813 | * It's intended for a dictionary used only once. | |
814 | * Note : When `dict == NULL || dictSize < 8` no dictionary is used. */ | |
815 | ZSTDLIB_API size_t ZSTD_decompress_usingDict(ZSTD_DCtx* dctx, | |
816 | void* dst, size_t dstCapacity, | |
817 | const void* src, size_t srcSize, | |
818 | const void* dict,size_t dictSize); | |
819 | ||
820 | ||
821 | /* ********************************* | |
822 | * Bulk processing dictionary API | |
823 | **********************************/ | |
824 | typedef struct ZSTD_CDict_s ZSTD_CDict; | |
825 | ||
826 | /*! ZSTD_createCDict() : | |
827 | * When compressing multiple messages or blocks using the same dictionary, | |
828 | * it's recommended to digest the dictionary only once, since it's a costly operation. | |
829 | * ZSTD_createCDict() will create a state from digesting a dictionary. | |
830 | * The resulting state can be used for future compression operations with very limited startup cost. | |
831 | * ZSTD_CDict can be created once and shared by multiple threads concurrently, since its usage is read-only. | |
832 | * @dictBuffer can be released after ZSTD_CDict creation, because its content is copied within CDict. | |
833 | * Note 1 : Consider experimental function `ZSTD_createCDict_byReference()` if you prefer to not duplicate @dictBuffer content. | |
834 | * Note 2 : A ZSTD_CDict can be created from an empty @dictBuffer, | |
835 | * in which case the only thing that it transports is the @compressionLevel. | |
836 | * This can be useful in a pipeline featuring ZSTD_compress_usingCDict() exclusively, | |
837 | * expecting a ZSTD_CDict parameter with any data, including those without a known dictionary. */ | |
838 | ZSTDLIB_API ZSTD_CDict* ZSTD_createCDict(const void* dictBuffer, size_t dictSize, | |
839 | int compressionLevel); | |
840 | ||
841 | /*! ZSTD_freeCDict() : | |
842 | * Function frees memory allocated by ZSTD_createCDict(). | |
843 | * If a NULL pointer is passed, no operation is performed. */ | |
844 | ZSTDLIB_API size_t ZSTD_freeCDict(ZSTD_CDict* CDict); | |
845 | ||
846 | /*! ZSTD_compress_usingCDict() : | |
847 | * Compression using a digested Dictionary. | |
848 | * Recommended when same dictionary is used multiple times. | |
849 | * Note : compression level is _decided at dictionary creation time_, | |
850 | * and frame parameters are hardcoded (dictID=yes, contentSize=yes, checksum=no) */ | |
851 | ZSTDLIB_API size_t ZSTD_compress_usingCDict(ZSTD_CCtx* cctx, | |
852 | void* dst, size_t dstCapacity, | |
853 | const void* src, size_t srcSize, | |
854 | const ZSTD_CDict* cdict); | |
855 | ||
856 | ||
857 | typedef struct ZSTD_DDict_s ZSTD_DDict; | |
858 | ||
859 | /*! ZSTD_createDDict() : | |
860 | * Create a digested dictionary, ready to start decompression operation without startup delay. | |
861 | * dictBuffer can be released after DDict creation, as its content is copied inside DDict. */ | |
862 | ZSTDLIB_API ZSTD_DDict* ZSTD_createDDict(const void* dictBuffer, size_t dictSize); | |
863 | ||
864 | /*! ZSTD_freeDDict() : | |
865 | * Function frees memory allocated with ZSTD_createDDict() | |
866 | * If a NULL pointer is passed, no operation is performed. */ | |
867 | ZSTDLIB_API size_t ZSTD_freeDDict(ZSTD_DDict* ddict); | |
868 | ||
869 | /*! ZSTD_decompress_usingDDict() : | |
870 | * Decompression using a digested Dictionary. | |
871 | * Recommended when same dictionary is used multiple times. */ | |
872 | ZSTDLIB_API size_t ZSTD_decompress_usingDDict(ZSTD_DCtx* dctx, | |
873 | void* dst, size_t dstCapacity, | |
874 | const void* src, size_t srcSize, | |
875 | const ZSTD_DDict* ddict); | |
876 | ||
877 | ||
878 | /* ****************************** | |
879 | * Dictionary helper functions | |
880 | *******************************/ | |
881 | ||
882 | /*! ZSTD_getDictID_fromDict() : | |
883 | * Provides the dictID stored within dictionary. | |
884 | * if @return == 0, the dictionary is not conformant with Zstandard specification. | |
885 | * It can still be loaded, but as a content-only dictionary. */ | |
886 | ZSTDLIB_API unsigned ZSTD_getDictID_fromDict(const void* dict, size_t dictSize); | |
887 | ||
888 | /*! ZSTD_getDictID_fromDDict() : | |
889 | * Provides the dictID of the dictionary loaded into `ddict`. | |
890 | * If @return == 0, the dictionary is not conformant to Zstandard specification, or empty. | |
891 | * Non-conformant dictionaries can still be loaded, but as content-only dictionaries. */ | |
892 | ZSTDLIB_API unsigned ZSTD_getDictID_fromDDict(const ZSTD_DDict* ddict); | |
893 | ||
894 | /*! ZSTD_getDictID_fromFrame() : | |
895 | * Provides the dictID required to decompressed the frame stored within `src`. | |
896 | * If @return == 0, the dictID could not be decoded. | |
897 | * This could for one of the following reasons : | |
898 | * - The frame does not require a dictionary to be decoded (most common case). | |
899 | * - The frame was built with dictID intentionally removed. Whatever dictionary is necessary is a hidden information. | |
900 | * Note : this use case also happens when using a non-conformant dictionary. | |
901 | * - `srcSize` is too small, and as a result, the frame header could not be decoded (only possible if `srcSize < ZSTD_FRAMEHEADERSIZE_MAX`). | |
902 | * - This is not a Zstandard frame. | |
903 | * When identifying the exact failure cause, it's possible to use ZSTD_getFrameHeader(), which will provide a more precise error code. */ | |
904 | ZSTDLIB_API unsigned ZSTD_getDictID_fromFrame(const void* src, size_t srcSize); | |
905 | ||
906 | ||
907 | /* ***************************************************************************** | |
908 | * Advanced dictionary and prefix API | |
909 | * | |
910 | * This API allows dictionaries to be used with ZSTD_compress2(), | |
911 | * ZSTD_compressStream2(), and ZSTD_decompress(). Dictionaries are sticky, and | |
912 | * only reset with the context is reset with ZSTD_reset_parameters or | |
913 | * ZSTD_reset_session_and_parameters. Prefixes are single-use. | |
914 | ******************************************************************************/ | |
915 | ||
916 | ||
917 | /*! ZSTD_CCtx_loadDictionary() : | |
918 | * Create an internal CDict from `dict` buffer. | |
919 | * Decompression will have to use same dictionary. | |
920 | * @result : 0, or an error code (which can be tested with ZSTD_isError()). | |
921 | * Special: Loading a NULL (or 0-size) dictionary invalidates previous dictionary, | |
922 | * meaning "return to no-dictionary mode". | |
923 | * Note 1 : Dictionary is sticky, it will be used for all future compressed frames. | |
924 | * To return to "no-dictionary" situation, load a NULL dictionary (or reset parameters). | |
925 | * Note 2 : Loading a dictionary involves building tables. | |
926 | * It's also a CPU consuming operation, with non-negligible impact on latency. | |
927 | * Tables are dependent on compression parameters, and for this reason, | |
928 | * compression parameters can no longer be changed after loading a dictionary. | |
929 | * Note 3 :`dict` content will be copied internally. | |
930 | * Use experimental ZSTD_CCtx_loadDictionary_byReference() to reference content instead. | |
931 | * In such a case, dictionary buffer must outlive its users. | |
932 | * Note 4 : Use ZSTD_CCtx_loadDictionary_advanced() | |
933 | * to precisely select how dictionary content must be interpreted. */ | |
934 | ZSTDLIB_API size_t ZSTD_CCtx_loadDictionary(ZSTD_CCtx* cctx, const void* dict, size_t dictSize); | |
935 | ||
936 | /*! ZSTD_CCtx_refCDict() : | |
937 | * Reference a prepared dictionary, to be used for all next compressed frames. | |
938 | * Note that compression parameters are enforced from within CDict, | |
939 | * and supersede any compression parameter previously set within CCtx. | |
940 | * The parameters ignored are labelled as "superseded-by-cdict" in the ZSTD_cParameter enum docs. | |
941 | * The ignored parameters will be used again if the CCtx is returned to no-dictionary mode. | |
942 | * The dictionary will remain valid for future compressed frames using same CCtx. | |
943 | * @result : 0, or an error code (which can be tested with ZSTD_isError()). | |
944 | * Special : Referencing a NULL CDict means "return to no-dictionary mode". | |
945 | * Note 1 : Currently, only one dictionary can be managed. | |
946 | * Referencing a new dictionary effectively "discards" any previous one. | |
947 | * Note 2 : CDict is just referenced, its lifetime must outlive its usage within CCtx. */ | |
948 | ZSTDLIB_API size_t ZSTD_CCtx_refCDict(ZSTD_CCtx* cctx, const ZSTD_CDict* cdict); | |
949 | ||
950 | /*! ZSTD_CCtx_refPrefix() : | |
951 | * Reference a prefix (single-usage dictionary) for next compressed frame. | |
952 | * A prefix is **only used once**. Tables are discarded at end of frame (ZSTD_e_end). | |
953 | * Decompression will need same prefix to properly regenerate data. | |
954 | * Compressing with a prefix is similar in outcome as performing a diff and compressing it, | |
955 | * but performs much faster, especially during decompression (compression speed is tunable with compression level). | |
956 | * @result : 0, or an error code (which can be tested with ZSTD_isError()). | |
957 | * Special: Adding any prefix (including NULL) invalidates any previous prefix or dictionary | |
958 | * Note 1 : Prefix buffer is referenced. It **must** outlive compression. | |
959 | * Its content must remain unmodified during compression. | |
960 | * Note 2 : If the intention is to diff some large src data blob with some prior version of itself, | |
961 | * ensure that the window size is large enough to contain the entire source. | |
962 | * See ZSTD_c_windowLog. | |
963 | * Note 3 : Referencing a prefix involves building tables, which are dependent on compression parameters. | |
964 | * It's a CPU consuming operation, with non-negligible impact on latency. | |
965 | * If there is a need to use the same prefix multiple times, consider loadDictionary instead. | |
966 | * Note 4 : By default, the prefix is interpreted as raw content (ZSTD_dct_rawContent). | |
967 | * Use experimental ZSTD_CCtx_refPrefix_advanced() to alter dictionary interpretation. */ | |
968 | ZSTDLIB_API size_t ZSTD_CCtx_refPrefix(ZSTD_CCtx* cctx, | |
969 | const void* prefix, size_t prefixSize); | |
970 | ||
971 | /*! ZSTD_DCtx_loadDictionary() : | |
972 | * Create an internal DDict from dict buffer, | |
973 | * to be used to decompress next frames. | |
974 | * The dictionary remains valid for all future frames, until explicitly invalidated. | |
975 | * @result : 0, or an error code (which can be tested with ZSTD_isError()). | |
976 | * Special : Adding a NULL (or 0-size) dictionary invalidates any previous dictionary, | |
977 | * meaning "return to no-dictionary mode". | |
978 | * Note 1 : Loading a dictionary involves building tables, | |
979 | * which has a non-negligible impact on CPU usage and latency. | |
980 | * It's recommended to "load once, use many times", to amortize the cost | |
981 | * Note 2 :`dict` content will be copied internally, so `dict` can be released after loading. | |
982 | * Use ZSTD_DCtx_loadDictionary_byReference() to reference dictionary content instead. | |
983 | * Note 3 : Use ZSTD_DCtx_loadDictionary_advanced() to take control of | |
984 | * how dictionary content is loaded and interpreted. | |
985 | */ | |
986 | ZSTDLIB_API size_t ZSTD_DCtx_loadDictionary(ZSTD_DCtx* dctx, const void* dict, size_t dictSize); | |
987 | ||
988 | /*! ZSTD_DCtx_refDDict() : | |
989 | * Reference a prepared dictionary, to be used to decompress next frames. | |
990 | * The dictionary remains active for decompression of future frames using same DCtx. | |
991 | * | |
992 | * If called with ZSTD_d_refMultipleDDicts enabled, repeated calls of this function | |
993 | * will store the DDict references in a table, and the DDict used for decompression | |
994 | * will be determined at decompression time, as per the dict ID in the frame. | |
995 | * The memory for the table is allocated on the first call to refDDict, and can be | |
996 | * freed with ZSTD_freeDCtx(). | |
997 | * | |
998 | * @result : 0, or an error code (which can be tested with ZSTD_isError()). | |
999 | * Note 1 : Currently, only one dictionary can be managed. | |
1000 | * Referencing a new dictionary effectively "discards" any previous one. | |
1001 | * Special: referencing a NULL DDict means "return to no-dictionary mode". | |
1002 | * Note 2 : DDict is just referenced, its lifetime must outlive its usage from DCtx. | |
1003 | */ | |
1004 | ZSTDLIB_API size_t ZSTD_DCtx_refDDict(ZSTD_DCtx* dctx, const ZSTD_DDict* ddict); | |
1005 | ||
1006 | /*! ZSTD_DCtx_refPrefix() : | |
1007 | * Reference a prefix (single-usage dictionary) to decompress next frame. | |
1008 | * This is the reverse operation of ZSTD_CCtx_refPrefix(), | |
1009 | * and must use the same prefix as the one used during compression. | |
1010 | * Prefix is **only used once**. Reference is discarded at end of frame. | |
1011 | * End of frame is reached when ZSTD_decompressStream() returns 0. | |
1012 | * @result : 0, or an error code (which can be tested with ZSTD_isError()). | |
1013 | * Note 1 : Adding any prefix (including NULL) invalidates any previously set prefix or dictionary | |
1014 | * Note 2 : Prefix buffer is referenced. It **must** outlive decompression. | |
1015 | * Prefix buffer must remain unmodified up to the end of frame, | |
1016 | * reached when ZSTD_decompressStream() returns 0. | |
1017 | * Note 3 : By default, the prefix is treated as raw content (ZSTD_dct_rawContent). | |
1018 | * Use ZSTD_CCtx_refPrefix_advanced() to alter dictMode (Experimental section) | |
1019 | * Note 4 : Referencing a raw content prefix has almost no cpu nor memory cost. | |
1020 | * A full dictionary is more costly, as it requires building tables. | |
1021 | */ | |
1022 | ZSTDLIB_API size_t ZSTD_DCtx_refPrefix(ZSTD_DCtx* dctx, | |
1023 | const void* prefix, size_t prefixSize); | |
1024 | ||
1025 | /* === Memory management === */ | |
1026 | ||
1027 | /*! ZSTD_sizeof_*() : | |
1028 | * These functions give the _current_ memory usage of selected object. | |
1029 | * Note that object memory usage can evolve (increase or decrease) over time. */ | |
1030 | ZSTDLIB_API size_t ZSTD_sizeof_CCtx(const ZSTD_CCtx* cctx); | |
1031 | ZSTDLIB_API size_t ZSTD_sizeof_DCtx(const ZSTD_DCtx* dctx); | |
1032 | ZSTDLIB_API size_t ZSTD_sizeof_CStream(const ZSTD_CStream* zcs); | |
1033 | ZSTDLIB_API size_t ZSTD_sizeof_DStream(const ZSTD_DStream* zds); | |
1034 | ZSTDLIB_API size_t ZSTD_sizeof_CDict(const ZSTD_CDict* cdict); | |
1035 | ZSTDLIB_API size_t ZSTD_sizeof_DDict(const ZSTD_DDict* ddict); | |
1036 | ||
1037 | #endif /* ZSTD_H_235446 */ | |
1038 | ||
1039 | ||
1040 | /* ************************************************************************************** | |
1041 | * ADVANCED AND EXPERIMENTAL FUNCTIONS | |
1042 | **************************************************************************************** | |
1043 | * The definitions in the following section are considered experimental. | |
1044 | * They are provided for advanced scenarios. | |
1045 | * They should never be used with a dynamic library, as prototypes may change in the future. | |
1046 | * Use them only in association with static linking. | |
1047 | * ***************************************************************************************/ | |
1048 | ||
1049 | #if !defined(ZSTD_H_ZSTD_STATIC_LINKING_ONLY) | |
1050 | #define ZSTD_H_ZSTD_STATIC_LINKING_ONLY | |
1051 | ||
1052 | /* ************************************************************************************** | |
1053 | * experimental API (static linking only) | |
1054 | **************************************************************************************** | |
1055 | * The following symbols and constants | |
1056 | * are not planned to join "stable API" status in the near future. | |
1057 | * They can still change in future versions. | |
1058 | * Some of them are planned to remain in the static_only section indefinitely. | |
1059 | * Some of them might be removed in the future (especially when redundant with existing stable functions) | |
1060 | * ***************************************************************************************/ | |
1061 | ||
1062 | #define ZSTD_FRAMEHEADERSIZE_PREFIX(format) ((format) == ZSTD_f_zstd1 ? 5 : 1) /* minimum input size required to query frame header size */ | |
1063 | #define ZSTD_FRAMEHEADERSIZE_MIN(format) ((format) == ZSTD_f_zstd1 ? 6 : 2) | |
1064 | #define ZSTD_FRAMEHEADERSIZE_MAX 18 /* can be useful for static allocation */ | |
1065 | #define ZSTD_SKIPPABLEHEADERSIZE 8 | |
1066 | ||
1067 | /* compression parameter bounds */ | |
1068 | #define ZSTD_WINDOWLOG_MAX_32 30 | |
1069 | #define ZSTD_WINDOWLOG_MAX_64 31 | |
1070 | #define ZSTD_WINDOWLOG_MAX ((int)(sizeof(size_t) == 4 ? ZSTD_WINDOWLOG_MAX_32 : ZSTD_WINDOWLOG_MAX_64)) | |
1071 | #define ZSTD_WINDOWLOG_MIN 10 | |
1072 | #define ZSTD_HASHLOG_MAX ((ZSTD_WINDOWLOG_MAX < 30) ? ZSTD_WINDOWLOG_MAX : 30) | |
1073 | #define ZSTD_HASHLOG_MIN 6 | |
1074 | #define ZSTD_CHAINLOG_MAX_32 29 | |
1075 | #define ZSTD_CHAINLOG_MAX_64 30 | |
1076 | #define ZSTD_CHAINLOG_MAX ((int)(sizeof(size_t) == 4 ? ZSTD_CHAINLOG_MAX_32 : ZSTD_CHAINLOG_MAX_64)) | |
1077 | #define ZSTD_CHAINLOG_MIN ZSTD_HASHLOG_MIN | |
1078 | #define ZSTD_SEARCHLOG_MAX (ZSTD_WINDOWLOG_MAX-1) | |
1079 | #define ZSTD_SEARCHLOG_MIN 1 | |
1080 | #define ZSTD_MINMATCH_MAX 7 /* only for ZSTD_fast, other strategies are limited to 6 */ | |
1081 | #define ZSTD_MINMATCH_MIN 3 /* only for ZSTD_btopt+, faster strategies are limited to 4 */ | |
1082 | #define ZSTD_TARGETLENGTH_MAX ZSTD_BLOCKSIZE_MAX | |
1083 | #define ZSTD_TARGETLENGTH_MIN 0 /* note : comparing this constant to an unsigned results in a tautological test */ | |
1084 | #define ZSTD_STRATEGY_MIN ZSTD_fast | |
1085 | #define ZSTD_STRATEGY_MAX ZSTD_btultra2 | |
1086 | ||
1087 | ||
1088 | #define ZSTD_OVERLAPLOG_MIN 0 | |
1089 | #define ZSTD_OVERLAPLOG_MAX 9 | |
1090 | ||
1091 | #define ZSTD_WINDOWLOG_LIMIT_DEFAULT 27 /* by default, the streaming decoder will refuse any frame | |
1092 | * requiring larger than (1<<ZSTD_WINDOWLOG_LIMIT_DEFAULT) window size, | |
1093 | * to preserve host's memory from unreasonable requirements. | |
1094 | * This limit can be overridden using ZSTD_DCtx_setParameter(,ZSTD_d_windowLogMax,). | |
1095 | * The limit does not apply for one-pass decoders (such as ZSTD_decompress()), since no additional memory is allocated */ | |
1096 | ||
1097 | ||
1098 | /* LDM parameter bounds */ | |
1099 | #define ZSTD_LDM_HASHLOG_MIN ZSTD_HASHLOG_MIN | |
1100 | #define ZSTD_LDM_HASHLOG_MAX ZSTD_HASHLOG_MAX | |
1101 | #define ZSTD_LDM_MINMATCH_MIN 4 | |
1102 | #define ZSTD_LDM_MINMATCH_MAX 4096 | |
1103 | #define ZSTD_LDM_BUCKETSIZELOG_MIN 1 | |
1104 | #define ZSTD_LDM_BUCKETSIZELOG_MAX 8 | |
1105 | #define ZSTD_LDM_HASHRATELOG_MIN 0 | |
1106 | #define ZSTD_LDM_HASHRATELOG_MAX (ZSTD_WINDOWLOG_MAX - ZSTD_HASHLOG_MIN) | |
1107 | ||
1108 | /* Advanced parameter bounds */ | |
1109 | #define ZSTD_TARGETCBLOCKSIZE_MIN 64 | |
1110 | #define ZSTD_TARGETCBLOCKSIZE_MAX ZSTD_BLOCKSIZE_MAX | |
1111 | #define ZSTD_SRCSIZEHINT_MIN 0 | |
1112 | #define ZSTD_SRCSIZEHINT_MAX INT_MAX | |
1113 | ||
1114 | /* internal */ | |
1115 | #define ZSTD_HASHLOG3_MAX 17 | |
1116 | ||
1117 | ||
1118 | /* --- Advanced types --- */ | |
1119 | ||
1120 | typedef struct ZSTD_CCtx_params_s ZSTD_CCtx_params; | |
1121 | ||
cf30f6a5 | 1122 | typedef struct { |
e0c1b49f NT |
1123 | unsigned int offset; /* The offset of the match. (NOT the same as the offset code) |
1124 | * If offset == 0 and matchLength == 0, this sequence represents the last | |
1125 | * literals in the block of litLength size. | |
1126 | */ | |
1127 | ||
1128 | unsigned int litLength; /* Literal length of the sequence. */ | |
1129 | unsigned int matchLength; /* Match length of the sequence. */ | |
1130 | ||
1131 | /* Note: Users of this API may provide a sequence with matchLength == litLength == offset == 0. | |
1132 | * In this case, we will treat the sequence as a marker for a block boundary. | |
1133 | */ | |
1134 | ||
1135 | unsigned int rep; /* Represents which repeat offset is represented by the field 'offset'. | |
1136 | * Ranges from [0, 3]. | |
1137 | * | |
1138 | * Repeat offsets are essentially previous offsets from previous sequences sorted in | |
1139 | * recency order. For more detail, see doc/zstd_compression_format.md | |
1140 | * | |
1141 | * If rep == 0, then 'offset' does not contain a repeat offset. | |
1142 | * If rep > 0: | |
1143 | * If litLength != 0: | |
1144 | * rep == 1 --> offset == repeat_offset_1 | |
1145 | * rep == 2 --> offset == repeat_offset_2 | |
1146 | * rep == 3 --> offset == repeat_offset_3 | |
1147 | * If litLength == 0: | |
1148 | * rep == 1 --> offset == repeat_offset_2 | |
1149 | * rep == 2 --> offset == repeat_offset_3 | |
1150 | * rep == 3 --> offset == repeat_offset_1 - 1 | |
1151 | * | |
1152 | * Note: This field is optional. ZSTD_generateSequences() will calculate the value of | |
1153 | * 'rep', but repeat offsets do not necessarily need to be calculated from an external | |
1154 | * sequence provider's perspective. For example, ZSTD_compressSequences() does not | |
1155 | * use this 'rep' field at all (as of now). | |
1156 | */ | |
1157 | } ZSTD_Sequence; | |
1158 | ||
1159 | typedef struct { | |
1160 | unsigned windowLog; /*< largest match distance : larger == more compression, more memory needed during decompression */ | |
1161 | unsigned chainLog; /*< fully searched segment : larger == more compression, slower, more memory (useless for fast) */ | |
1162 | unsigned hashLog; /*< dispatch table : larger == faster, more memory */ | |
1163 | unsigned searchLog; /*< nb of searches : larger == more compression, slower */ | |
1164 | unsigned minMatch; /*< match length searched : larger == faster decompression, sometimes less compression */ | |
1165 | unsigned targetLength; /*< acceptable match size for optimal parser (only) : larger == more compression, slower */ | |
1166 | ZSTD_strategy strategy; /*< see ZSTD_strategy definition above */ | |
1167 | } ZSTD_compressionParameters; | |
1168 | ||
1169 | typedef struct { | |
1170 | int contentSizeFlag; /*< 1: content size will be in frame header (when known) */ | |
1171 | int checksumFlag; /*< 1: generate a 32-bits checksum using XXH64 algorithm at end of frame, for error detection */ | |
1172 | int noDictIDFlag; /*< 1: no dictID will be saved into frame header (dictID is only useful for dictionary compression) */ | |
cf30f6a5 NT |
1173 | } ZSTD_frameParameters; |
1174 | ||
cf30f6a5 | 1175 | typedef struct { |
e0c1b49f NT |
1176 | ZSTD_compressionParameters cParams; |
1177 | ZSTD_frameParameters fParams; | |
cf30f6a5 NT |
1178 | } ZSTD_parameters; |
1179 | ||
e0c1b49f NT |
1180 | typedef enum { |
1181 | ZSTD_dct_auto = 0, /* dictionary is "full" when starting with ZSTD_MAGIC_DICTIONARY, otherwise it is "rawContent" */ | |
1182 | ZSTD_dct_rawContent = 1, /* ensures dictionary is always loaded as rawContent, even if it starts with ZSTD_MAGIC_DICTIONARY */ | |
1183 | ZSTD_dct_fullDict = 2 /* refuses to load a dictionary if it does not respect Zstandard's specification, starting with ZSTD_MAGIC_DICTIONARY */ | |
1184 | } ZSTD_dictContentType_e; | |
1185 | ||
1186 | typedef enum { | |
1187 | ZSTD_dlm_byCopy = 0, /*< Copy dictionary content internally */ | |
1188 | ZSTD_dlm_byRef = 1 /*< Reference dictionary content -- the dictionary buffer must outlive its users. */ | |
1189 | } ZSTD_dictLoadMethod_e; | |
1190 | ||
1191 | typedef enum { | |
1192 | ZSTD_f_zstd1 = 0, /* zstd frame format, specified in zstd_compression_format.md (default) */ | |
1193 | ZSTD_f_zstd1_magicless = 1 /* Variant of zstd frame format, without initial 4-bytes magic number. | |
1194 | * Useful to save 4 bytes per generated frame. | |
1195 | * Decoder cannot recognise automatically this format, requiring this instruction. */ | |
1196 | } ZSTD_format_e; | |
1197 | ||
1198 | typedef enum { | |
1199 | /* Note: this enum controls ZSTD_d_forceIgnoreChecksum */ | |
1200 | ZSTD_d_validateChecksum = 0, | |
1201 | ZSTD_d_ignoreChecksum = 1 | |
1202 | } ZSTD_forceIgnoreChecksum_e; | |
1203 | ||
1204 | typedef enum { | |
1205 | /* Note: this enum controls ZSTD_d_refMultipleDDicts */ | |
1206 | ZSTD_rmd_refSingleDDict = 0, | |
1207 | ZSTD_rmd_refMultipleDDicts = 1 | |
1208 | } ZSTD_refMultipleDDicts_e; | |
1209 | ||
1210 | typedef enum { | |
1211 | /* Note: this enum and the behavior it controls are effectively internal | |
1212 | * implementation details of the compressor. They are expected to continue | |
1213 | * to evolve and should be considered only in the context of extremely | |
1214 | * advanced performance tuning. | |
1215 | * | |
1216 | * Zstd currently supports the use of a CDict in three ways: | |
1217 | * | |
1218 | * - The contents of the CDict can be copied into the working context. This | |
1219 | * means that the compression can search both the dictionary and input | |
1220 | * while operating on a single set of internal tables. This makes | |
1221 | * the compression faster per-byte of input. However, the initial copy of | |
1222 | * the CDict's tables incurs a fixed cost at the beginning of the | |
1223 | * compression. For small compressions (< 8 KB), that copy can dominate | |
1224 | * the cost of the compression. | |
1225 | * | |
1226 | * - The CDict's tables can be used in-place. In this model, compression is | |
1227 | * slower per input byte, because the compressor has to search two sets of | |
1228 | * tables. However, this model incurs no start-up cost (as long as the | |
1229 | * working context's tables can be reused). For small inputs, this can be | |
1230 | * faster than copying the CDict's tables. | |
1231 | * | |
1232 | * - The CDict's tables are not used at all, and instead we use the working | |
1233 | * context alone to reload the dictionary and use params based on the source | |
1234 | * size. See ZSTD_compress_insertDictionary() and ZSTD_compress_usingDict(). | |
1235 | * This method is effective when the dictionary sizes are very small relative | |
1236 | * to the input size, and the input size is fairly large to begin with. | |
1237 | * | |
1238 | * Zstd has a simple internal heuristic that selects which strategy to use | |
1239 | * at the beginning of a compression. However, if experimentation shows that | |
1240 | * Zstd is making poor choices, it is possible to override that choice with | |
1241 | * this enum. | |
1242 | */ | |
1243 | ZSTD_dictDefaultAttach = 0, /* Use the default heuristic. */ | |
1244 | ZSTD_dictForceAttach = 1, /* Never copy the dictionary. */ | |
1245 | ZSTD_dictForceCopy = 2, /* Always copy the dictionary. */ | |
1246 | ZSTD_dictForceLoad = 3 /* Always reload the dictionary */ | |
1247 | } ZSTD_dictAttachPref_e; | |
1248 | ||
1249 | typedef enum { | |
1250 | ZSTD_lcm_auto = 0, /*< Automatically determine the compression mode based on the compression level. | |
1251 | * Negative compression levels will be uncompressed, and positive compression | |
1252 | * levels will be compressed. */ | |
1253 | ZSTD_lcm_huffman = 1, /*< Always attempt Huffman compression. Uncompressed literals will still be | |
1254 | * emitted if Huffman compression is not profitable. */ | |
1255 | ZSTD_lcm_uncompressed = 2 /*< Always emit uncompressed literals. */ | |
1256 | } ZSTD_literalCompressionMode_e; | |
1257 | ||
1258 | ||
1259 | /* ************************************* | |
1260 | * Frame size functions | |
1261 | ***************************************/ | |
1262 | ||
1263 | /*! ZSTD_findDecompressedSize() : | |
1264 | * `src` should point to the start of a series of ZSTD encoded and/or skippable frames | |
1265 | * `srcSize` must be the _exact_ size of this series | |
1266 | * (i.e. there should be a frame boundary at `src + srcSize`) | |
1267 | * @return : - decompressed size of all data in all successive frames | |
1268 | * - if the decompressed size cannot be determined: ZSTD_CONTENTSIZE_UNKNOWN | |
1269 | * - if an error occurred: ZSTD_CONTENTSIZE_ERROR | |
1270 | * | |
1271 | * note 1 : decompressed size is an optional field, that may not be present, especially in streaming mode. | |
1272 | * When `return==ZSTD_CONTENTSIZE_UNKNOWN`, data to decompress could be any size. | |
1273 | * In which case, it's necessary to use streaming mode to decompress data. | |
1274 | * note 2 : decompressed size is always present when compression is done with ZSTD_compress() | |
1275 | * note 3 : decompressed size can be very large (64-bits value), | |
1276 | * potentially larger than what local system can handle as a single memory segment. | |
1277 | * In which case, it's necessary to use streaming mode to decompress data. | |
1278 | * note 4 : If source is untrusted, decompressed size could be wrong or intentionally modified. | |
1279 | * Always ensure result fits within application's authorized limits. | |
1280 | * Each application can set its own limits. | |
1281 | * note 5 : ZSTD_findDecompressedSize handles multiple frames, and so it must traverse the input to | |
1282 | * read each contained frame header. This is fast as most of the data is skipped, | |
1283 | * however it does mean that all frame data must be present and valid. */ | |
1284 | ZSTDLIB_API unsigned long long ZSTD_findDecompressedSize(const void* src, size_t srcSize); | |
1285 | ||
1286 | /*! ZSTD_decompressBound() : | |
1287 | * `src` should point to the start of a series of ZSTD encoded and/or skippable frames | |
1288 | * `srcSize` must be the _exact_ size of this series | |
1289 | * (i.e. there should be a frame boundary at `src + srcSize`) | |
1290 | * @return : - upper-bound for the decompressed size of all data in all successive frames | |
1291 | * - if an error occurred: ZSTD_CONTENTSIZE_ERROR | |
1292 | * | |
1293 | * note 1 : an error can occur if `src` contains an invalid or incorrectly formatted frame. | |
1294 | * note 2 : the upper-bound is exact when the decompressed size field is available in every ZSTD encoded frame of `src`. | |
1295 | * in this case, `ZSTD_findDecompressedSize` and `ZSTD_decompressBound` return the same value. | |
1296 | * note 3 : when the decompressed size field isn't available, the upper-bound for that frame is calculated by: | |
1297 | * upper-bound = # blocks * min(128 KB, Window_Size) | |
cf30f6a5 | 1298 | */ |
e0c1b49f | 1299 | ZSTDLIB_API unsigned long long ZSTD_decompressBound(const void* src, size_t srcSize); |
cf30f6a5 | 1300 | |
e0c1b49f NT |
1301 | /*! ZSTD_frameHeaderSize() : |
1302 | * srcSize must be >= ZSTD_FRAMEHEADERSIZE_PREFIX. | |
1303 | * @return : size of the Frame Header, | |
1304 | * or an error code (if srcSize is too small) */ | |
1305 | ZSTDLIB_API size_t ZSTD_frameHeaderSize(const void* src, size_t srcSize); | |
1306 | ||
1307 | typedef enum { | |
1308 | ZSTD_sf_noBlockDelimiters = 0, /* Representation of ZSTD_Sequence has no block delimiters, sequences only */ | |
1309 | ZSTD_sf_explicitBlockDelimiters = 1 /* Representation of ZSTD_Sequence contains explicit block delimiters */ | |
1310 | } ZSTD_sequenceFormat_e; | |
1311 | ||
1312 | /*! ZSTD_generateSequences() : | |
1313 | * Generate sequences using ZSTD_compress2, given a source buffer. | |
1314 | * | |
1315 | * Each block will end with a dummy sequence | |
1316 | * with offset == 0, matchLength == 0, and litLength == length of last literals. | |
1317 | * litLength may be == 0, and if so, then the sequence of (of: 0 ml: 0 ll: 0) | |
1318 | * simply acts as a block delimiter. | |
cf30f6a5 | 1319 | * |
e0c1b49f NT |
1320 | * zc can be used to insert custom compression params. |
1321 | * This function invokes ZSTD_compress2 | |
cf30f6a5 | 1322 | * |
e0c1b49f NT |
1323 | * The output of this function can be fed into ZSTD_compressSequences() with CCtx |
1324 | * setting of ZSTD_c_blockDelimiters as ZSTD_sf_explicitBlockDelimiters | |
1325 | * @return : number of sequences generated | |
cf30f6a5 | 1326 | */ |
cf30f6a5 | 1327 | |
e0c1b49f NT |
1328 | ZSTDLIB_API size_t ZSTD_generateSequences(ZSTD_CCtx* zc, ZSTD_Sequence* outSeqs, |
1329 | size_t outSeqsSize, const void* src, size_t srcSize); | |
cf30f6a5 | 1330 | |
e0c1b49f NT |
1331 | /*! ZSTD_mergeBlockDelimiters() : |
1332 | * Given an array of ZSTD_Sequence, remove all sequences that represent block delimiters/last literals | |
1333 | * by merging them into into the literals of the next sequence. | |
cf30f6a5 | 1334 | * |
e0c1b49f NT |
1335 | * As such, the final generated result has no explicit representation of block boundaries, |
1336 | * and the final last literals segment is not represented in the sequences. | |
cf30f6a5 | 1337 | * |
e0c1b49f NT |
1338 | * The output of this function can be fed into ZSTD_compressSequences() with CCtx |
1339 | * setting of ZSTD_c_blockDelimiters as ZSTD_sf_noBlockDelimiters | |
1340 | * @return : number of sequences left after merging | |
1341 | */ | |
1342 | ZSTDLIB_API size_t ZSTD_mergeBlockDelimiters(ZSTD_Sequence* sequences, size_t seqsSize); | |
1343 | ||
1344 | /*! ZSTD_compressSequences() : | |
1345 | * Compress an array of ZSTD_Sequence, generated from the original source buffer, into dst. | |
1346 | * If a dictionary is included, then the cctx should reference the dict. (see: ZSTD_CCtx_refCDict(), ZSTD_CCtx_loadDictionary(), etc.) | |
1347 | * The entire source is compressed into a single frame. | |
1348 | * | |
1349 | * The compression behavior changes based on cctx params. In particular: | |
1350 | * If ZSTD_c_blockDelimiters == ZSTD_sf_noBlockDelimiters, the array of ZSTD_Sequence is expected to contain | |
1351 | * no block delimiters (defined in ZSTD_Sequence). Block boundaries are roughly determined based on | |
1352 | * the block size derived from the cctx, and sequences may be split. This is the default setting. | |
1353 | * | |
1354 | * If ZSTD_c_blockDelimiters == ZSTD_sf_explicitBlockDelimiters, the array of ZSTD_Sequence is expected to contain | |
1355 | * block delimiters (defined in ZSTD_Sequence). Behavior is undefined if no block delimiters are provided. | |
1356 | * | |
1357 | * If ZSTD_c_validateSequences == 0, this function will blindly accept the sequences provided. Invalid sequences cause undefined | |
1358 | * behavior. If ZSTD_c_validateSequences == 1, then if sequence is invalid (see doc/zstd_compression_format.md for | |
1359 | * specifics regarding offset/matchlength requirements) then the function will bail out and return an error. | |
1360 | * | |
1361 | * In addition to the two adjustable experimental params, there are other important cctx params. | |
1362 | * - ZSTD_c_minMatch MUST be set as less than or equal to the smallest match generated by the match finder. It has a minimum value of ZSTD_MINMATCH_MIN. | |
1363 | * - ZSTD_c_compressionLevel accordingly adjusts the strength of the entropy coder, as it would in typical compression. | |
1364 | * - ZSTD_c_windowLog affects offset validation: this function will return an error at higher debug levels if a provided offset | |
1365 | * is larger than what the spec allows for a given window log and dictionary (if present). See: doc/zstd_compression_format.md | |
1366 | * | |
1367 | * Note: Repcodes are, as of now, always re-calculated within this function, so ZSTD_Sequence::rep is unused. | |
1368 | * Note 2: Once we integrate ability to ingest repcodes, the explicit block delims mode must respect those repcodes exactly, | |
1369 | * and cannot emit an RLE block that disagrees with the repcode history | |
1370 | * @return : final compressed size or a ZSTD error. | |
cf30f6a5 | 1371 | */ |
e0c1b49f NT |
1372 | ZSTDLIB_API size_t ZSTD_compressSequences(ZSTD_CCtx* const cctx, void* dst, size_t dstSize, |
1373 | const ZSTD_Sequence* inSeqs, size_t inSeqsSize, | |
1374 | const void* src, size_t srcSize); | |
cf30f6a5 | 1375 | |
e0c1b49f NT |
1376 | |
1377 | /*! ZSTD_writeSkippableFrame() : | |
1378 | * Generates a zstd skippable frame containing data given by src, and writes it to dst buffer. | |
cf30f6a5 | 1379 | * |
e0c1b49f NT |
1380 | * Skippable frames begin with a a 4-byte magic number. There are 16 possible choices of magic number, |
1381 | * ranging from ZSTD_MAGIC_SKIPPABLE_START to ZSTD_MAGIC_SKIPPABLE_START+15. | |
1382 | * As such, the parameter magicVariant controls the exact skippable frame magic number variant used, so | |
1383 | * the magic number used will be ZSTD_MAGIC_SKIPPABLE_START + magicVariant. | |
cf30f6a5 | 1384 | * |
e0c1b49f NT |
1385 | * Returns an error if destination buffer is not large enough, if the source size is not representable |
1386 | * with a 4-byte unsigned int, or if the parameter magicVariant is greater than 15 (and therefore invalid). | |
cf30f6a5 | 1387 | * |
e0c1b49f | 1388 | * @return : number of bytes written or a ZSTD error. |
cf30f6a5 | 1389 | */ |
e0c1b49f NT |
1390 | ZSTDLIB_API size_t ZSTD_writeSkippableFrame(void* dst, size_t dstCapacity, |
1391 | const void* src, size_t srcSize, unsigned magicVariant); | |
1392 | ||
1393 | ||
1394 | /* ************************************* | |
1395 | * Memory management | |
1396 | ***************************************/ | |
1397 | ||
1398 | /*! ZSTD_estimate*() : | |
1399 | * These functions make it possible to estimate memory usage | |
1400 | * of a future {D,C}Ctx, before its creation. | |
1401 | * | |
1402 | * ZSTD_estimateCCtxSize() will provide a memory budget large enough | |
1403 | * for any compression level up to selected one. | |
1404 | * Note : Unlike ZSTD_estimateCStreamSize*(), this estimate | |
1405 | * does not include space for a window buffer. | |
1406 | * Therefore, the estimation is only guaranteed for single-shot compressions, not streaming. | |
1407 | * The estimate will assume the input may be arbitrarily large, | |
1408 | * which is the worst case. | |
1409 | * | |
1410 | * When srcSize can be bound by a known and rather "small" value, | |
1411 | * this fact can be used to provide a tighter estimation | |
1412 | * because the CCtx compression context will need less memory. | |
1413 | * This tighter estimation can be provided by more advanced functions | |
1414 | * ZSTD_estimateCCtxSize_usingCParams(), which can be used in tandem with ZSTD_getCParams(), | |
1415 | * and ZSTD_estimateCCtxSize_usingCCtxParams(), which can be used in tandem with ZSTD_CCtxParams_setParameter(). | |
1416 | * Both can be used to estimate memory using custom compression parameters and arbitrary srcSize limits. | |
1417 | * | |
1418 | * Note 2 : only single-threaded compression is supported. | |
1419 | * ZSTD_estimateCCtxSize_usingCCtxParams() will return an error code if ZSTD_c_nbWorkers is >= 1. | |
1420 | */ | |
1421 | ZSTDLIB_API size_t ZSTD_estimateCCtxSize(int compressionLevel); | |
1422 | ZSTDLIB_API size_t ZSTD_estimateCCtxSize_usingCParams(ZSTD_compressionParameters cParams); | |
1423 | ZSTDLIB_API size_t ZSTD_estimateCCtxSize_usingCCtxParams(const ZSTD_CCtx_params* params); | |
1424 | ZSTDLIB_API size_t ZSTD_estimateDCtxSize(void); | |
1425 | ||
1426 | /*! ZSTD_estimateCStreamSize() : | |
1427 | * ZSTD_estimateCStreamSize() will provide a budget large enough for any compression level up to selected one. | |
1428 | * It will also consider src size to be arbitrarily "large", which is worst case. | |
1429 | * If srcSize is known to always be small, ZSTD_estimateCStreamSize_usingCParams() can provide a tighter estimation. | |
1430 | * ZSTD_estimateCStreamSize_usingCParams() can be used in tandem with ZSTD_getCParams() to create cParams from compressionLevel. | |
1431 | * ZSTD_estimateCStreamSize_usingCCtxParams() can be used in tandem with ZSTD_CCtxParams_setParameter(). Only single-threaded compression is supported. This function will return an error code if ZSTD_c_nbWorkers is >= 1. | |
1432 | * Note : CStream size estimation is only correct for single-threaded compression. | |
1433 | * ZSTD_DStream memory budget depends on window Size. | |
1434 | * This information can be passed manually, using ZSTD_estimateDStreamSize, | |
1435 | * or deducted from a valid frame Header, using ZSTD_estimateDStreamSize_fromFrame(); | |
1436 | * Note : if streaming is init with function ZSTD_init?Stream_usingDict(), | |
1437 | * an internal ?Dict will be created, which additional size is not estimated here. | |
1438 | * In this case, get total size by adding ZSTD_estimate?DictSize */ | |
1439 | ZSTDLIB_API size_t ZSTD_estimateCStreamSize(int compressionLevel); | |
1440 | ZSTDLIB_API size_t ZSTD_estimateCStreamSize_usingCParams(ZSTD_compressionParameters cParams); | |
1441 | ZSTDLIB_API size_t ZSTD_estimateCStreamSize_usingCCtxParams(const ZSTD_CCtx_params* params); | |
1442 | ZSTDLIB_API size_t ZSTD_estimateDStreamSize(size_t windowSize); | |
1443 | ZSTDLIB_API size_t ZSTD_estimateDStreamSize_fromFrame(const void* src, size_t srcSize); | |
1444 | ||
1445 | /*! ZSTD_estimate?DictSize() : | |
1446 | * ZSTD_estimateCDictSize() will bet that src size is relatively "small", and content is copied, like ZSTD_createCDict(). | |
1447 | * ZSTD_estimateCDictSize_advanced() makes it possible to control compression parameters precisely, like ZSTD_createCDict_advanced(). | |
1448 | * Note : dictionaries created by reference (`ZSTD_dlm_byRef`) are logically smaller. | |
1449 | */ | |
1450 | ZSTDLIB_API size_t ZSTD_estimateCDictSize(size_t dictSize, int compressionLevel); | |
1451 | ZSTDLIB_API size_t ZSTD_estimateCDictSize_advanced(size_t dictSize, ZSTD_compressionParameters cParams, ZSTD_dictLoadMethod_e dictLoadMethod); | |
1452 | ZSTDLIB_API size_t ZSTD_estimateDDictSize(size_t dictSize, ZSTD_dictLoadMethod_e dictLoadMethod); | |
1453 | ||
1454 | /*! ZSTD_initStatic*() : | |
1455 | * Initialize an object using a pre-allocated fixed-size buffer. | |
1456 | * workspace: The memory area to emplace the object into. | |
1457 | * Provided pointer *must be 8-bytes aligned*. | |
1458 | * Buffer must outlive object. | |
1459 | * workspaceSize: Use ZSTD_estimate*Size() to determine | |
1460 | * how large workspace must be to support target scenario. | |
1461 | * @return : pointer to object (same address as workspace, just different type), | |
1462 | * or NULL if error (size too small, incorrect alignment, etc.) | |
1463 | * Note : zstd will never resize nor malloc() when using a static buffer. | |
1464 | * If the object requires more memory than available, | |
1465 | * zstd will just error out (typically ZSTD_error_memory_allocation). | |
1466 | * Note 2 : there is no corresponding "free" function. | |
1467 | * Since workspace is allocated externally, it must be freed externally too. | |
1468 | * Note 3 : cParams : use ZSTD_getCParams() to convert a compression level | |
1469 | * into its associated cParams. | |
1470 | * Limitation 1 : currently not compatible with internal dictionary creation, triggered by | |
1471 | * ZSTD_CCtx_loadDictionary(), ZSTD_initCStream_usingDict() or ZSTD_initDStream_usingDict(). | |
1472 | * Limitation 2 : static cctx currently not compatible with multi-threading. | |
1473 | * Limitation 3 : static dctx is incompatible with legacy support. | |
cf30f6a5 | 1474 | */ |
e0c1b49f NT |
1475 | ZSTDLIB_API ZSTD_CCtx* ZSTD_initStaticCCtx(void* workspace, size_t workspaceSize); |
1476 | ZSTDLIB_API ZSTD_CStream* ZSTD_initStaticCStream(void* workspace, size_t workspaceSize); /*< same as ZSTD_initStaticCCtx() */ | |
1477 | ||
1478 | ZSTDLIB_API ZSTD_DCtx* ZSTD_initStaticDCtx(void* workspace, size_t workspaceSize); | |
1479 | ZSTDLIB_API ZSTD_DStream* ZSTD_initStaticDStream(void* workspace, size_t workspaceSize); /*< same as ZSTD_initStaticDCtx() */ | |
1480 | ||
1481 | ZSTDLIB_API const ZSTD_CDict* ZSTD_initStaticCDict( | |
1482 | void* workspace, size_t workspaceSize, | |
1483 | const void* dict, size_t dictSize, | |
1484 | ZSTD_dictLoadMethod_e dictLoadMethod, | |
1485 | ZSTD_dictContentType_e dictContentType, | |
1486 | ZSTD_compressionParameters cParams); | |
1487 | ||
1488 | ZSTDLIB_API const ZSTD_DDict* ZSTD_initStaticDDict( | |
1489 | void* workspace, size_t workspaceSize, | |
1490 | const void* dict, size_t dictSize, | |
1491 | ZSTD_dictLoadMethod_e dictLoadMethod, | |
1492 | ZSTD_dictContentType_e dictContentType); | |
1493 | ||
1494 | ||
1495 | /*! Custom memory allocation : | |
1496 | * These prototypes make it possible to pass your own allocation/free functions. | |
1497 | * ZSTD_customMem is provided at creation time, using ZSTD_create*_advanced() variants listed below. | |
1498 | * All allocation/free operations will be completed using these custom variants instead of regular <stdlib.h> ones. | |
1499 | */ | |
1500 | typedef void* (*ZSTD_allocFunction) (void* opaque, size_t size); | |
1501 | typedef void (*ZSTD_freeFunction) (void* opaque, void* address); | |
1502 | typedef struct { ZSTD_allocFunction customAlloc; ZSTD_freeFunction customFree; void* opaque; } ZSTD_customMem; | |
1503 | static | |
1504 | __attribute__((__unused__)) | |
1505 | ZSTD_customMem const ZSTD_defaultCMem = { NULL, NULL, NULL }; /*< this constant defers to stdlib's functions */ | |
1506 | ||
1507 | ZSTDLIB_API ZSTD_CCtx* ZSTD_createCCtx_advanced(ZSTD_customMem customMem); | |
1508 | ZSTDLIB_API ZSTD_CStream* ZSTD_createCStream_advanced(ZSTD_customMem customMem); | |
1509 | ZSTDLIB_API ZSTD_DCtx* ZSTD_createDCtx_advanced(ZSTD_customMem customMem); | |
1510 | ZSTDLIB_API ZSTD_DStream* ZSTD_createDStream_advanced(ZSTD_customMem customMem); | |
1511 | ||
1512 | ZSTDLIB_API ZSTD_CDict* ZSTD_createCDict_advanced(const void* dict, size_t dictSize, | |
1513 | ZSTD_dictLoadMethod_e dictLoadMethod, | |
1514 | ZSTD_dictContentType_e dictContentType, | |
1515 | ZSTD_compressionParameters cParams, | |
1516 | ZSTD_customMem customMem); | |
1517 | ||
1518 | /* ! Thread pool : | |
1519 | * These prototypes make it possible to share a thread pool among multiple compression contexts. | |
1520 | * This can limit resources for applications with multiple threads where each one uses | |
1521 | * a threaded compression mode (via ZSTD_c_nbWorkers parameter). | |
1522 | * ZSTD_createThreadPool creates a new thread pool with a given number of threads. | |
1523 | * Note that the lifetime of such pool must exist while being used. | |
1524 | * ZSTD_CCtx_refThreadPool assigns a thread pool to a context (use NULL argument value | |
1525 | * to use an internal thread pool). | |
1526 | * ZSTD_freeThreadPool frees a thread pool, accepts NULL pointer. | |
1527 | */ | |
1528 | typedef struct POOL_ctx_s ZSTD_threadPool; | |
1529 | ZSTDLIB_API ZSTD_threadPool* ZSTD_createThreadPool(size_t numThreads); | |
1530 | ZSTDLIB_API void ZSTD_freeThreadPool (ZSTD_threadPool* pool); /* accept NULL pointer */ | |
1531 | ZSTDLIB_API size_t ZSTD_CCtx_refThreadPool(ZSTD_CCtx* cctx, ZSTD_threadPool* pool); | |
cf30f6a5 | 1532 | |
e0c1b49f NT |
1533 | |
1534 | /* | |
1535 | * This API is temporary and is expected to change or disappear in the future! | |
cf30f6a5 | 1536 | */ |
e0c1b49f NT |
1537 | ZSTDLIB_API ZSTD_CDict* ZSTD_createCDict_advanced2( |
1538 | const void* dict, size_t dictSize, | |
1539 | ZSTD_dictLoadMethod_e dictLoadMethod, | |
1540 | ZSTD_dictContentType_e dictContentType, | |
1541 | const ZSTD_CCtx_params* cctxParams, | |
1542 | ZSTD_customMem customMem); | |
1543 | ||
1544 | ZSTDLIB_API ZSTD_DDict* ZSTD_createDDict_advanced( | |
1545 | const void* dict, size_t dictSize, | |
1546 | ZSTD_dictLoadMethod_e dictLoadMethod, | |
1547 | ZSTD_dictContentType_e dictContentType, | |
1548 | ZSTD_customMem customMem); | |
1549 | ||
1550 | ||
1551 | /* ************************************* | |
1552 | * Advanced compression functions | |
1553 | ***************************************/ | |
1554 | ||
1555 | /*! ZSTD_createCDict_byReference() : | |
1556 | * Create a digested dictionary for compression | |
1557 | * Dictionary content is just referenced, not duplicated. | |
1558 | * As a consequence, `dictBuffer` **must** outlive CDict, | |
1559 | * and its content must remain unmodified throughout the lifetime of CDict. | |
1560 | * note: equivalent to ZSTD_createCDict_advanced(), with dictLoadMethod==ZSTD_dlm_byRef */ | |
1561 | ZSTDLIB_API ZSTD_CDict* ZSTD_createCDict_byReference(const void* dictBuffer, size_t dictSize, int compressionLevel); | |
1562 | ||
1563 | /*! ZSTD_getDictID_fromCDict() : | |
1564 | * Provides the dictID of the dictionary loaded into `cdict`. | |
1565 | * If @return == 0, the dictionary is not conformant to Zstandard specification, or empty. | |
1566 | * Non-conformant dictionaries can still be loaded, but as content-only dictionaries. */ | |
1567 | ZSTDLIB_API unsigned ZSTD_getDictID_fromCDict(const ZSTD_CDict* cdict); | |
1568 | ||
1569 | /*! ZSTD_getCParams() : | |
1570 | * @return ZSTD_compressionParameters structure for a selected compression level and estimated srcSize. | |
1571 | * `estimatedSrcSize` value is optional, select 0 if not known */ | |
1572 | ZSTDLIB_API ZSTD_compressionParameters ZSTD_getCParams(int compressionLevel, unsigned long long estimatedSrcSize, size_t dictSize); | |
1573 | ||
1574 | /*! ZSTD_getParams() : | |
1575 | * same as ZSTD_getCParams(), but @return a full `ZSTD_parameters` object instead of sub-component `ZSTD_compressionParameters`. | |
1576 | * All fields of `ZSTD_frameParameters` are set to default : contentSize=1, checksum=0, noDictID=0 */ | |
1577 | ZSTDLIB_API ZSTD_parameters ZSTD_getParams(int compressionLevel, unsigned long long estimatedSrcSize, size_t dictSize); | |
1578 | ||
1579 | /*! ZSTD_checkCParams() : | |
1580 | * Ensure param values remain within authorized range. | |
1581 | * @return 0 on success, or an error code (can be checked with ZSTD_isError()) */ | |
1582 | ZSTDLIB_API size_t ZSTD_checkCParams(ZSTD_compressionParameters params); | |
1583 | ||
1584 | /*! ZSTD_adjustCParams() : | |
1585 | * optimize params for a given `srcSize` and `dictSize`. | |
1586 | * `srcSize` can be unknown, in which case use ZSTD_CONTENTSIZE_UNKNOWN. | |
1587 | * `dictSize` must be `0` when there is no dictionary. | |
1588 | * cPar can be invalid : all parameters will be clamped within valid range in the @return struct. | |
1589 | * This function never fails (wide contract) */ | |
1590 | ZSTDLIB_API ZSTD_compressionParameters ZSTD_adjustCParams(ZSTD_compressionParameters cPar, unsigned long long srcSize, size_t dictSize); | |
1591 | ||
1592 | /*! ZSTD_compress_advanced() : | |
1593 | * Note : this function is now DEPRECATED. | |
1594 | * It can be replaced by ZSTD_compress2(), in combination with ZSTD_CCtx_setParameter() and other parameter setters. | |
1595 | * This prototype will be marked as deprecated and generate compilation warning on reaching v1.5.x */ | |
1596 | ZSTDLIB_API size_t ZSTD_compress_advanced(ZSTD_CCtx* cctx, | |
1597 | void* dst, size_t dstCapacity, | |
1598 | const void* src, size_t srcSize, | |
1599 | const void* dict,size_t dictSize, | |
1600 | ZSTD_parameters params); | |
1601 | ||
1602 | /*! ZSTD_compress_usingCDict_advanced() : | |
1603 | * Note : this function is now REDUNDANT. | |
1604 | * It can be replaced by ZSTD_compress2(), in combination with ZSTD_CCtx_loadDictionary() and other parameter setters. | |
1605 | * This prototype will be marked as deprecated and generate compilation warning in some future version */ | |
1606 | ZSTDLIB_API size_t ZSTD_compress_usingCDict_advanced(ZSTD_CCtx* cctx, | |
1607 | void* dst, size_t dstCapacity, | |
1608 | const void* src, size_t srcSize, | |
1609 | const ZSTD_CDict* cdict, | |
1610 | ZSTD_frameParameters fParams); | |
1611 | ||
1612 | ||
1613 | /*! ZSTD_CCtx_loadDictionary_byReference() : | |
1614 | * Same as ZSTD_CCtx_loadDictionary(), but dictionary content is referenced, instead of being copied into CCtx. | |
1615 | * It saves some memory, but also requires that `dict` outlives its usage within `cctx` */ | |
1616 | ZSTDLIB_API size_t ZSTD_CCtx_loadDictionary_byReference(ZSTD_CCtx* cctx, const void* dict, size_t dictSize); | |
1617 | ||
1618 | /*! ZSTD_CCtx_loadDictionary_advanced() : | |
1619 | * Same as ZSTD_CCtx_loadDictionary(), but gives finer control over | |
1620 | * how to load the dictionary (by copy ? by reference ?) | |
1621 | * and how to interpret it (automatic ? force raw mode ? full mode only ?) */ | |
1622 | ZSTDLIB_API size_t ZSTD_CCtx_loadDictionary_advanced(ZSTD_CCtx* cctx, const void* dict, size_t dictSize, ZSTD_dictLoadMethod_e dictLoadMethod, ZSTD_dictContentType_e dictContentType); | |
1623 | ||
1624 | /*! ZSTD_CCtx_refPrefix_advanced() : | |
1625 | * Same as ZSTD_CCtx_refPrefix(), but gives finer control over | |
1626 | * how to interpret prefix content (automatic ? force raw mode (default) ? full mode only ?) */ | |
1627 | ZSTDLIB_API size_t ZSTD_CCtx_refPrefix_advanced(ZSTD_CCtx* cctx, const void* prefix, size_t prefixSize, ZSTD_dictContentType_e dictContentType); | |
1628 | ||
1629 | /* === experimental parameters === */ | |
1630 | /* these parameters can be used with ZSTD_setParameter() | |
1631 | * they are not guaranteed to remain supported in the future */ | |
1632 | ||
1633 | /* Enables rsyncable mode, | |
1634 | * which makes compressed files more rsync friendly | |
1635 | * by adding periodic synchronization points to the compressed data. | |
1636 | * The target average block size is ZSTD_c_jobSize / 2. | |
1637 | * It's possible to modify the job size to increase or decrease | |
1638 | * the granularity of the synchronization point. | |
1639 | * Once the jobSize is smaller than the window size, | |
1640 | * it will result in compression ratio degradation. | |
1641 | * NOTE 1: rsyncable mode only works when multithreading is enabled. | |
1642 | * NOTE 2: rsyncable performs poorly in combination with long range mode, | |
1643 | * since it will decrease the effectiveness of synchronization points, | |
1644 | * though mileage may vary. | |
1645 | * NOTE 3: Rsyncable mode limits maximum compression speed to ~400 MB/s. | |
1646 | * If the selected compression level is already running significantly slower, | |
1647 | * the overall speed won't be significantly impacted. | |
1648 | */ | |
1649 | #define ZSTD_c_rsyncable ZSTD_c_experimentalParam1 | |
1650 | ||
1651 | /* Select a compression format. | |
1652 | * The value must be of type ZSTD_format_e. | |
1653 | * See ZSTD_format_e enum definition for details */ | |
1654 | #define ZSTD_c_format ZSTD_c_experimentalParam2 | |
1655 | ||
1656 | /* Force back-reference distances to remain < windowSize, | |
1657 | * even when referencing into Dictionary content (default:0) */ | |
1658 | #define ZSTD_c_forceMaxWindow ZSTD_c_experimentalParam3 | |
1659 | ||
1660 | /* Controls whether the contents of a CDict | |
1661 | * are used in place, or copied into the working context. | |
1662 | * Accepts values from the ZSTD_dictAttachPref_e enum. | |
1663 | * See the comments on that enum for an explanation of the feature. */ | |
1664 | #define ZSTD_c_forceAttachDict ZSTD_c_experimentalParam4 | |
1665 | ||
1666 | /* Controls how the literals are compressed (default is auto). | |
1667 | * The value must be of type ZSTD_literalCompressionMode_e. | |
1668 | * See ZSTD_literalCompressionMode_t enum definition for details. | |
cf30f6a5 | 1669 | */ |
e0c1b49f NT |
1670 | #define ZSTD_c_literalCompressionMode ZSTD_c_experimentalParam5 |
1671 | ||
1672 | /* Tries to fit compressed block size to be around targetCBlockSize. | |
1673 | * No target when targetCBlockSize == 0. | |
1674 | * There is no guarantee on compressed block size (default:0) */ | |
1675 | #define ZSTD_c_targetCBlockSize ZSTD_c_experimentalParam6 | |
1676 | ||
1677 | /* User's best guess of source size. | |
1678 | * Hint is not valid when srcSizeHint == 0. | |
1679 | * There is no guarantee that hint is close to actual source size, | |
1680 | * but compression ratio may regress significantly if guess considerably underestimates */ | |
1681 | #define ZSTD_c_srcSizeHint ZSTD_c_experimentalParam7 | |
1682 | ||
1683 | /* Controls whether the new and experimental "dedicated dictionary search | |
1684 | * structure" can be used. This feature is still rough around the edges, be | |
1685 | * prepared for surprising behavior! | |
1686 | * | |
1687 | * How to use it: | |
1688 | * | |
1689 | * When using a CDict, whether to use this feature or not is controlled at | |
1690 | * CDict creation, and it must be set in a CCtxParams set passed into that | |
1691 | * construction (via ZSTD_createCDict_advanced2()). A compression will then | |
1692 | * use the feature or not based on how the CDict was constructed; the value of | |
1693 | * this param, set in the CCtx, will have no effect. | |
1694 | * | |
1695 | * However, when a dictionary buffer is passed into a CCtx, such as via | |
1696 | * ZSTD_CCtx_loadDictionary(), this param can be set on the CCtx to control | |
1697 | * whether the CDict that is created internally can use the feature or not. | |
1698 | * | |
1699 | * What it does: | |
1700 | * | |
1701 | * Normally, the internal data structures of the CDict are analogous to what | |
1702 | * would be stored in a CCtx after compressing the contents of a dictionary. | |
1703 | * To an approximation, a compression using a dictionary can then use those | |
1704 | * data structures to simply continue what is effectively a streaming | |
1705 | * compression where the simulated compression of the dictionary left off. | |
1706 | * Which is to say, the search structures in the CDict are normally the same | |
1707 | * format as in the CCtx. | |
1708 | * | |
1709 | * It is possible to do better, since the CDict is not like a CCtx: the search | |
1710 | * structures are written once during CDict creation, and then are only read | |
1711 | * after that, while the search structures in the CCtx are both read and | |
1712 | * written as the compression goes along. This means we can choose a search | |
1713 | * structure for the dictionary that is read-optimized. | |
1714 | * | |
1715 | * This feature enables the use of that different structure. | |
1716 | * | |
1717 | * Note that some of the members of the ZSTD_compressionParameters struct have | |
1718 | * different semantics and constraints in the dedicated search structure. It is | |
1719 | * highly recommended that you simply set a compression level in the CCtxParams | |
1720 | * you pass into the CDict creation call, and avoid messing with the cParams | |
1721 | * directly. | |
1722 | * | |
1723 | * Effects: | |
1724 | * | |
1725 | * This will only have any effect when the selected ZSTD_strategy | |
1726 | * implementation supports this feature. Currently, that's limited to | |
1727 | * ZSTD_greedy, ZSTD_lazy, and ZSTD_lazy2. | |
1728 | * | |
1729 | * Note that this means that the CDict tables can no longer be copied into the | |
1730 | * CCtx, so the dict attachment mode ZSTD_dictForceCopy will no longer be | |
1731 | * useable. The dictionary can only be attached or reloaded. | |
1732 | * | |
1733 | * In general, you should expect compression to be faster--sometimes very much | |
1734 | * so--and CDict creation to be slightly slower. Eventually, we will probably | |
1735 | * make this mode the default. | |
1736 | */ | |
1737 | #define ZSTD_c_enableDedicatedDictSearch ZSTD_c_experimentalParam8 | |
1738 | ||
1739 | /* ZSTD_c_stableInBuffer | |
1740 | * Experimental parameter. | |
1741 | * Default is 0 == disabled. Set to 1 to enable. | |
1742 | * | |
1743 | * Tells the compressor that the ZSTD_inBuffer will ALWAYS be the same | |
1744 | * between calls, except for the modifications that zstd makes to pos (the | |
1745 | * caller must not modify pos). This is checked by the compressor, and | |
1746 | * compression will fail if it ever changes. This means the only flush | |
1747 | * mode that makes sense is ZSTD_e_end, so zstd will error if ZSTD_e_end | |
1748 | * is not used. The data in the ZSTD_inBuffer in the range [src, src + pos) | |
1749 | * MUST not be modified during compression or you will get data corruption. | |
1750 | * | |
1751 | * When this flag is enabled zstd won't allocate an input window buffer, | |
1752 | * because the user guarantees it can reference the ZSTD_inBuffer until | |
1753 | * the frame is complete. But, it will still allocate an output buffer | |
1754 | * large enough to fit a block (see ZSTD_c_stableOutBuffer). This will also | |
1755 | * avoid the memcpy() from the input buffer to the input window buffer. | |
1756 | * | |
1757 | * NOTE: ZSTD_compressStream2() will error if ZSTD_e_end is not used. | |
1758 | * That means this flag cannot be used with ZSTD_compressStream(). | |
1759 | * | |
1760 | * NOTE: So long as the ZSTD_inBuffer always points to valid memory, using | |
1761 | * this flag is ALWAYS memory safe, and will never access out-of-bounds | |
1762 | * memory. However, compression WILL fail if you violate the preconditions. | |
1763 | * | |
1764 | * WARNING: The data in the ZSTD_inBuffer in the range [dst, dst + pos) MUST | |
1765 | * not be modified during compression or you will get data corruption. This | |
1766 | * is because zstd needs to reference data in the ZSTD_inBuffer to find | |
1767 | * matches. Normally zstd maintains its own window buffer for this purpose, | |
1768 | * but passing this flag tells zstd to use the user provided buffer. | |
1769 | */ | |
1770 | #define ZSTD_c_stableInBuffer ZSTD_c_experimentalParam9 | |
cf30f6a5 | 1771 | |
e0c1b49f NT |
1772 | /* ZSTD_c_stableOutBuffer |
1773 | * Experimental parameter. | |
1774 | * Default is 0 == disabled. Set to 1 to enable. | |
cf30f6a5 | 1775 | * |
e0c1b49f NT |
1776 | * Tells he compressor that the ZSTD_outBuffer will not be resized between |
1777 | * calls. Specifically: (out.size - out.pos) will never grow. This gives the | |
1778 | * compressor the freedom to say: If the compressed data doesn't fit in the | |
1779 | * output buffer then return ZSTD_error_dstSizeTooSmall. This allows us to | |
1780 | * always decompress directly into the output buffer, instead of decompressing | |
1781 | * into an internal buffer and copying to the output buffer. | |
cf30f6a5 | 1782 | * |
e0c1b49f NT |
1783 | * When this flag is enabled zstd won't allocate an output buffer, because |
1784 | * it can write directly to the ZSTD_outBuffer. It will still allocate the | |
1785 | * input window buffer (see ZSTD_c_stableInBuffer). | |
1786 | * | |
1787 | * Zstd will check that (out.size - out.pos) never grows and return an error | |
1788 | * if it does. While not strictly necessary, this should prevent surprises. | |
cf30f6a5 | 1789 | */ |
e0c1b49f | 1790 | #define ZSTD_c_stableOutBuffer ZSTD_c_experimentalParam10 |
cf30f6a5 | 1791 | |
e0c1b49f NT |
1792 | /* ZSTD_c_blockDelimiters |
1793 | * Default is 0 == ZSTD_sf_noBlockDelimiters. | |
cf30f6a5 | 1794 | * |
e0c1b49f | 1795 | * For use with sequence compression API: ZSTD_compressSequences(). |
cf30f6a5 | 1796 | * |
e0c1b49f NT |
1797 | * Designates whether or not the given array of ZSTD_Sequence contains block delimiters |
1798 | * and last literals, which are defined as sequences with offset == 0 and matchLength == 0. | |
1799 | * See the definition of ZSTD_Sequence for more specifics. | |
cf30f6a5 | 1800 | */ |
e0c1b49f | 1801 | #define ZSTD_c_blockDelimiters ZSTD_c_experimentalParam11 |
cf30f6a5 | 1802 | |
e0c1b49f NT |
1803 | /* ZSTD_c_validateSequences |
1804 | * Default is 0 == disabled. Set to 1 to enable sequence validation. | |
1805 | * | |
1806 | * For use with sequence compression API: ZSTD_compressSequences(). | |
1807 | * Designates whether or not we validate sequences provided to ZSTD_compressSequences() | |
1808 | * during function execution. | |
1809 | * | |
1810 | * Without validation, providing a sequence that does not conform to the zstd spec will cause | |
1811 | * undefined behavior, and may produce a corrupted block. | |
1812 | * | |
1813 | * With validation enabled, a if sequence is invalid (see doc/zstd_compression_format.md for | |
1814 | * specifics regarding offset/matchlength requirements) then the function will bail out and | |
1815 | * return an error. | |
cf30f6a5 | 1816 | * |
cf30f6a5 | 1817 | */ |
e0c1b49f | 1818 | #define ZSTD_c_validateSequences ZSTD_c_experimentalParam12 |
cf30f6a5 | 1819 | |
e0c1b49f NT |
1820 | /*! ZSTD_CCtx_getParameter() : |
1821 | * Get the requested compression parameter value, selected by enum ZSTD_cParameter, | |
1822 | * and store it into int* value. | |
1823 | * @return : 0, or an error code (which can be tested with ZSTD_isError()). | |
cf30f6a5 | 1824 | */ |
e0c1b49f NT |
1825 | ZSTDLIB_API size_t ZSTD_CCtx_getParameter(const ZSTD_CCtx* cctx, ZSTD_cParameter param, int* value); |
1826 | ||
1827 | ||
1828 | /*! ZSTD_CCtx_params : | |
1829 | * Quick howto : | |
1830 | * - ZSTD_createCCtxParams() : Create a ZSTD_CCtx_params structure | |
1831 | * - ZSTD_CCtxParams_setParameter() : Push parameters one by one into | |
1832 | * an existing ZSTD_CCtx_params structure. | |
1833 | * This is similar to | |
1834 | * ZSTD_CCtx_setParameter(). | |
1835 | * - ZSTD_CCtx_setParametersUsingCCtxParams() : Apply parameters to | |
1836 | * an existing CCtx. | |
1837 | * These parameters will be applied to | |
1838 | * all subsequent frames. | |
1839 | * - ZSTD_compressStream2() : Do compression using the CCtx. | |
1840 | * - ZSTD_freeCCtxParams() : Free the memory, accept NULL pointer. | |
1841 | * | |
1842 | * This can be used with ZSTD_estimateCCtxSize_advanced_usingCCtxParams() | |
1843 | * for static allocation of CCtx for single-threaded compression. | |
cf30f6a5 | 1844 | */ |
e0c1b49f NT |
1845 | ZSTDLIB_API ZSTD_CCtx_params* ZSTD_createCCtxParams(void); |
1846 | ZSTDLIB_API size_t ZSTD_freeCCtxParams(ZSTD_CCtx_params* params); /* accept NULL pointer */ | |
cf30f6a5 | 1847 | |
e0c1b49f NT |
1848 | /*! ZSTD_CCtxParams_reset() : |
1849 | * Reset params to default values. | |
cf30f6a5 | 1850 | */ |
e0c1b49f | 1851 | ZSTDLIB_API size_t ZSTD_CCtxParams_reset(ZSTD_CCtx_params* params); |
cf30f6a5 | 1852 | |
e0c1b49f NT |
1853 | /*! ZSTD_CCtxParams_init() : |
1854 | * Initializes the compression parameters of cctxParams according to | |
1855 | * compression level. All other parameters are reset to their default values. | |
1856 | */ | |
1857 | ZSTDLIB_API size_t ZSTD_CCtxParams_init(ZSTD_CCtx_params* cctxParams, int compressionLevel); | |
cf30f6a5 | 1858 | |
e0c1b49f NT |
1859 | /*! ZSTD_CCtxParams_init_advanced() : |
1860 | * Initializes the compression and frame parameters of cctxParams according to | |
1861 | * params. All other parameters are reset to their default values. | |
cf30f6a5 | 1862 | */ |
e0c1b49f NT |
1863 | ZSTDLIB_API size_t ZSTD_CCtxParams_init_advanced(ZSTD_CCtx_params* cctxParams, ZSTD_parameters params); |
1864 | ||
1865 | /*! ZSTD_CCtxParams_setParameter() : | |
1866 | * Similar to ZSTD_CCtx_setParameter. | |
1867 | * Set one compression parameter, selected by enum ZSTD_cParameter. | |
1868 | * Parameters must be applied to a ZSTD_CCtx using | |
1869 | * ZSTD_CCtx_setParametersUsingCCtxParams(). | |
1870 | * @result : a code representing success or failure (which can be tested with | |
1871 | * ZSTD_isError()). | |
1872 | */ | |
1873 | ZSTDLIB_API size_t ZSTD_CCtxParams_setParameter(ZSTD_CCtx_params* params, ZSTD_cParameter param, int value); | |
cf30f6a5 | 1874 | |
e0c1b49f NT |
1875 | /*! ZSTD_CCtxParams_getParameter() : |
1876 | * Similar to ZSTD_CCtx_getParameter. | |
1877 | * Get the requested value of one compression parameter, selected by enum ZSTD_cParameter. | |
1878 | * @result : 0, or an error code (which can be tested with ZSTD_isError()). | |
1879 | */ | |
1880 | ZSTDLIB_API size_t ZSTD_CCtxParams_getParameter(const ZSTD_CCtx_params* params, ZSTD_cParameter param, int* value); | |
1881 | ||
1882 | /*! ZSTD_CCtx_setParametersUsingCCtxParams() : | |
1883 | * Apply a set of ZSTD_CCtx_params to the compression context. | |
1884 | * This can be done even after compression is started, | |
1885 | * if nbWorkers==0, this will have no impact until a new compression is started. | |
1886 | * if nbWorkers>=1, new parameters will be picked up at next job, | |
1887 | * with a few restrictions (windowLog, pledgedSrcSize, nbWorkers, jobSize, and overlapLog are not updated). | |
1888 | */ | |
1889 | ZSTDLIB_API size_t ZSTD_CCtx_setParametersUsingCCtxParams( | |
1890 | ZSTD_CCtx* cctx, const ZSTD_CCtx_params* params); | |
1891 | ||
1892 | /*! ZSTD_compressStream2_simpleArgs() : | |
1893 | * Same as ZSTD_compressStream2(), | |
1894 | * but using only integral types as arguments. | |
1895 | * This variant might be helpful for binders from dynamic languages | |
1896 | * which have troubles handling structures containing memory pointers. | |
cf30f6a5 | 1897 | */ |
e0c1b49f NT |
1898 | ZSTDLIB_API size_t ZSTD_compressStream2_simpleArgs ( |
1899 | ZSTD_CCtx* cctx, | |
1900 | void* dst, size_t dstCapacity, size_t* dstPos, | |
1901 | const void* src, size_t srcSize, size_t* srcPos, | |
1902 | ZSTD_EndDirective endOp); | |
1903 | ||
1904 | ||
1905 | /* ************************************* | |
1906 | * Advanced decompression functions | |
1907 | ***************************************/ | |
1908 | ||
1909 | /*! ZSTD_isFrame() : | |
1910 | * Tells if the content of `buffer` starts with a valid Frame Identifier. | |
1911 | * Note : Frame Identifier is 4 bytes. If `size < 4`, @return will always be 0. | |
1912 | * Note 2 : Legacy Frame Identifiers are considered valid only if Legacy Support is enabled. | |
1913 | * Note 3 : Skippable Frame Identifiers are considered valid. */ | |
1914 | ZSTDLIB_API unsigned ZSTD_isFrame(const void* buffer, size_t size); | |
1915 | ||
1916 | /*! ZSTD_createDDict_byReference() : | |
1917 | * Create a digested dictionary, ready to start decompression operation without startup delay. | |
1918 | * Dictionary content is referenced, and therefore stays in dictBuffer. | |
1919 | * It is important that dictBuffer outlives DDict, | |
1920 | * it must remain read accessible throughout the lifetime of DDict */ | |
1921 | ZSTDLIB_API ZSTD_DDict* ZSTD_createDDict_byReference(const void* dictBuffer, size_t dictSize); | |
1922 | ||
1923 | /*! ZSTD_DCtx_loadDictionary_byReference() : | |
1924 | * Same as ZSTD_DCtx_loadDictionary(), | |
1925 | * but references `dict` content instead of copying it into `dctx`. | |
1926 | * This saves memory if `dict` remains around., | |
1927 | * However, it's imperative that `dict` remains accessible (and unmodified) while being used, so it must outlive decompression. */ | |
1928 | ZSTDLIB_API size_t ZSTD_DCtx_loadDictionary_byReference(ZSTD_DCtx* dctx, const void* dict, size_t dictSize); | |
1929 | ||
1930 | /*! ZSTD_DCtx_loadDictionary_advanced() : | |
1931 | * Same as ZSTD_DCtx_loadDictionary(), | |
1932 | * but gives direct control over | |
1933 | * how to load the dictionary (by copy ? by reference ?) | |
1934 | * and how to interpret it (automatic ? force raw mode ? full mode only ?). */ | |
1935 | ZSTDLIB_API size_t ZSTD_DCtx_loadDictionary_advanced(ZSTD_DCtx* dctx, const void* dict, size_t dictSize, ZSTD_dictLoadMethod_e dictLoadMethod, ZSTD_dictContentType_e dictContentType); | |
1936 | ||
1937 | /*! ZSTD_DCtx_refPrefix_advanced() : | |
1938 | * Same as ZSTD_DCtx_refPrefix(), but gives finer control over | |
1939 | * how to interpret prefix content (automatic ? force raw mode (default) ? full mode only ?) */ | |
1940 | ZSTDLIB_API size_t ZSTD_DCtx_refPrefix_advanced(ZSTD_DCtx* dctx, const void* prefix, size_t prefixSize, ZSTD_dictContentType_e dictContentType); | |
1941 | ||
1942 | /*! ZSTD_DCtx_setMaxWindowSize() : | |
1943 | * Refuses allocating internal buffers for frames requiring a window size larger than provided limit. | |
1944 | * This protects a decoder context from reserving too much memory for itself (potential attack scenario). | |
1945 | * This parameter is only useful in streaming mode, since no internal buffer is allocated in single-pass mode. | |
1946 | * By default, a decompression context accepts all window sizes <= (1 << ZSTD_WINDOWLOG_LIMIT_DEFAULT) | |
1947 | * @return : 0, or an error code (which can be tested using ZSTD_isError()). | |
1948 | */ | |
1949 | ZSTDLIB_API size_t ZSTD_DCtx_setMaxWindowSize(ZSTD_DCtx* dctx, size_t maxWindowSize); | |
cf30f6a5 | 1950 | |
e0c1b49f NT |
1951 | /*! ZSTD_DCtx_getParameter() : |
1952 | * Get the requested decompression parameter value, selected by enum ZSTD_dParameter, | |
1953 | * and store it into int* value. | |
1954 | * @return : 0, or an error code (which can be tested with ZSTD_isError()). | |
1955 | */ | |
1956 | ZSTDLIB_API size_t ZSTD_DCtx_getParameter(ZSTD_DCtx* dctx, ZSTD_dParameter param, int* value); | |
cf30f6a5 | 1957 | |
e0c1b49f NT |
1958 | /* ZSTD_d_format |
1959 | * experimental parameter, | |
1960 | * allowing selection between ZSTD_format_e input compression formats | |
cf30f6a5 | 1961 | */ |
e0c1b49f NT |
1962 | #define ZSTD_d_format ZSTD_d_experimentalParam1 |
1963 | /* ZSTD_d_stableOutBuffer | |
1964 | * Experimental parameter. | |
1965 | * Default is 0 == disabled. Set to 1 to enable. | |
1966 | * | |
1967 | * Tells the decompressor that the ZSTD_outBuffer will ALWAYS be the same | |
1968 | * between calls, except for the modifications that zstd makes to pos (the | |
1969 | * caller must not modify pos). This is checked by the decompressor, and | |
1970 | * decompression will fail if it ever changes. Therefore the ZSTD_outBuffer | |
1971 | * MUST be large enough to fit the entire decompressed frame. This will be | |
1972 | * checked when the frame content size is known. The data in the ZSTD_outBuffer | |
1973 | * in the range [dst, dst + pos) MUST not be modified during decompression | |
1974 | * or you will get data corruption. | |
1975 | * | |
1976 | * When this flags is enabled zstd won't allocate an output buffer, because | |
1977 | * it can write directly to the ZSTD_outBuffer, but it will still allocate | |
1978 | * an input buffer large enough to fit any compressed block. This will also | |
1979 | * avoid the memcpy() from the internal output buffer to the ZSTD_outBuffer. | |
1980 | * If you need to avoid the input buffer allocation use the buffer-less | |
1981 | * streaming API. | |
1982 | * | |
1983 | * NOTE: So long as the ZSTD_outBuffer always points to valid memory, using | |
1984 | * this flag is ALWAYS memory safe, and will never access out-of-bounds | |
1985 | * memory. However, decompression WILL fail if you violate the preconditions. | |
1986 | * | |
1987 | * WARNING: The data in the ZSTD_outBuffer in the range [dst, dst + pos) MUST | |
1988 | * not be modified during decompression or you will get data corruption. This | |
1989 | * is because zstd needs to reference data in the ZSTD_outBuffer to regenerate | |
1990 | * matches. Normally zstd maintains its own buffer for this purpose, but passing | |
1991 | * this flag tells zstd to use the user provided buffer. | |
cf30f6a5 | 1992 | */ |
e0c1b49f NT |
1993 | #define ZSTD_d_stableOutBuffer ZSTD_d_experimentalParam2 |
1994 | ||
1995 | /* ZSTD_d_forceIgnoreChecksum | |
1996 | * Experimental parameter. | |
1997 | * Default is 0 == disabled. Set to 1 to enable | |
cf30f6a5 | 1998 | * |
e0c1b49f NT |
1999 | * Tells the decompressor to skip checksum validation during decompression, regardless |
2000 | * of whether checksumming was specified during compression. This offers some | |
2001 | * slight performance benefits, and may be useful for debugging. | |
2002 | * Param has values of type ZSTD_forceIgnoreChecksum_e | |
cf30f6a5 | 2003 | */ |
e0c1b49f NT |
2004 | #define ZSTD_d_forceIgnoreChecksum ZSTD_d_experimentalParam3 |
2005 | ||
2006 | /* ZSTD_d_refMultipleDDicts | |
2007 | * Experimental parameter. | |
2008 | * Default is 0 == disabled. Set to 1 to enable | |
cf30f6a5 | 2009 | * |
e0c1b49f NT |
2010 | * If enabled and dctx is allocated on the heap, then additional memory will be allocated |
2011 | * to store references to multiple ZSTD_DDict. That is, multiple calls of ZSTD_refDDict() | |
2012 | * using a given ZSTD_DCtx, rather than overwriting the previous DDict reference, will instead | |
2013 | * store all references. At decompression time, the appropriate dictID is selected | |
2014 | * from the set of DDicts based on the dictID in the frame. | |
cf30f6a5 | 2015 | * |
e0c1b49f | 2016 | * Usage is simply calling ZSTD_refDDict() on multiple dict buffers. |
cf30f6a5 | 2017 | * |
e0c1b49f | 2018 | * Param has values of byte ZSTD_refMultipleDDicts_e |
cf30f6a5 | 2019 | * |
e0c1b49f NT |
2020 | * WARNING: Enabling this parameter and calling ZSTD_DCtx_refDDict(), will trigger memory |
2021 | * allocation for the hash table. ZSTD_freeDCtx() also frees this memory. | |
2022 | * Memory is allocated as per ZSTD_DCtx::customMem. | |
cf30f6a5 | 2023 | * |
e0c1b49f NT |
2024 | * Although this function allocates memory for the table, the user is still responsible for |
2025 | * memory management of the underlying ZSTD_DDict* themselves. | |
cf30f6a5 | 2026 | */ |
e0c1b49f | 2027 | #define ZSTD_d_refMultipleDDicts ZSTD_d_experimentalParam4 |
cf30f6a5 NT |
2028 | |
2029 | ||
e0c1b49f NT |
2030 | /*! ZSTD_DCtx_setFormat() : |
2031 | * Instruct the decoder context about what kind of data to decode next. | |
2032 | * This instruction is mandatory to decode data without a fully-formed header, | |
2033 | * such ZSTD_f_zstd1_magicless for example. | |
2034 | * @return : 0, or an error code (which can be tested using ZSTD_isError()). */ | |
2035 | ZSTDLIB_API size_t ZSTD_DCtx_setFormat(ZSTD_DCtx* dctx, ZSTD_format_e format); | |
cf30f6a5 | 2036 | |
e0c1b49f NT |
2037 | /*! ZSTD_decompressStream_simpleArgs() : |
2038 | * Same as ZSTD_decompressStream(), | |
2039 | * but using only integral types as arguments. | |
2040 | * This can be helpful for binders from dynamic languages | |
2041 | * which have troubles handling structures containing memory pointers. | |
cf30f6a5 | 2042 | */ |
e0c1b49f NT |
2043 | ZSTDLIB_API size_t ZSTD_decompressStream_simpleArgs ( |
2044 | ZSTD_DCtx* dctx, | |
2045 | void* dst, size_t dstCapacity, size_t* dstPos, | |
2046 | const void* src, size_t srcSize, size_t* srcPos); | |
2047 | ||
2048 | ||
2049 | /* ****************************************************************** | |
2050 | * Advanced streaming functions | |
2051 | * Warning : most of these functions are now redundant with the Advanced API. | |
2052 | * Once Advanced API reaches "stable" status, | |
2053 | * redundant functions will be deprecated, and then at some point removed. | |
2054 | ********************************************************************/ | |
2055 | ||
2056 | /*===== Advanced Streaming compression functions =====*/ | |
2057 | ||
2058 | /*! ZSTD_initCStream_srcSize() : | |
2059 | * This function is deprecated, and equivalent to: | |
2060 | * ZSTD_CCtx_reset(zcs, ZSTD_reset_session_only); | |
2061 | * ZSTD_CCtx_refCDict(zcs, NULL); // clear the dictionary (if any) | |
2062 | * ZSTD_CCtx_setParameter(zcs, ZSTD_c_compressionLevel, compressionLevel); | |
2063 | * ZSTD_CCtx_setPledgedSrcSize(zcs, pledgedSrcSize); | |
2064 | * | |
2065 | * pledgedSrcSize must be correct. If it is not known at init time, use | |
2066 | * ZSTD_CONTENTSIZE_UNKNOWN. Note that, for compatibility with older programs, | |
2067 | * "0" also disables frame content size field. It may be enabled in the future. | |
2068 | * Note : this prototype will be marked as deprecated and generate compilation warnings on reaching v1.5.x | |
2069 | */ | |
2070 | ZSTDLIB_API size_t | |
2071 | ZSTD_initCStream_srcSize(ZSTD_CStream* zcs, | |
2072 | int compressionLevel, | |
2073 | unsigned long long pledgedSrcSize); | |
2074 | ||
2075 | /*! ZSTD_initCStream_usingDict() : | |
2076 | * This function is deprecated, and is equivalent to: | |
2077 | * ZSTD_CCtx_reset(zcs, ZSTD_reset_session_only); | |
2078 | * ZSTD_CCtx_setParameter(zcs, ZSTD_c_compressionLevel, compressionLevel); | |
2079 | * ZSTD_CCtx_loadDictionary(zcs, dict, dictSize); | |
2080 | * | |
2081 | * Creates of an internal CDict (incompatible with static CCtx), except if | |
2082 | * dict == NULL or dictSize < 8, in which case no dict is used. | |
2083 | * Note: dict is loaded with ZSTD_dct_auto (treated as a full zstd dictionary if | |
2084 | * it begins with ZSTD_MAGIC_DICTIONARY, else as raw content) and ZSTD_dlm_byCopy. | |
2085 | * Note : this prototype will be marked as deprecated and generate compilation warnings on reaching v1.5.x | |
2086 | */ | |
2087 | ZSTDLIB_API size_t | |
2088 | ZSTD_initCStream_usingDict(ZSTD_CStream* zcs, | |
2089 | const void* dict, size_t dictSize, | |
2090 | int compressionLevel); | |
2091 | ||
2092 | /*! ZSTD_initCStream_advanced() : | |
2093 | * This function is deprecated, and is approximately equivalent to: | |
2094 | * ZSTD_CCtx_reset(zcs, ZSTD_reset_session_only); | |
2095 | * // Pseudocode: Set each zstd parameter and leave the rest as-is. | |
2096 | * for ((param, value) : params) { | |
2097 | * ZSTD_CCtx_setParameter(zcs, param, value); | |
2098 | * } | |
2099 | * ZSTD_CCtx_setPledgedSrcSize(zcs, pledgedSrcSize); | |
2100 | * ZSTD_CCtx_loadDictionary(zcs, dict, dictSize); | |
2101 | * | |
2102 | * dict is loaded with ZSTD_dct_auto and ZSTD_dlm_byCopy. | |
2103 | * pledgedSrcSize must be correct. | |
2104 | * If srcSize is not known at init time, use value ZSTD_CONTENTSIZE_UNKNOWN. | |
2105 | * Note : this prototype will be marked as deprecated and generate compilation warnings on reaching v1.5.x | |
2106 | */ | |
2107 | ZSTDLIB_API size_t | |
2108 | ZSTD_initCStream_advanced(ZSTD_CStream* zcs, | |
2109 | const void* dict, size_t dictSize, | |
2110 | ZSTD_parameters params, | |
2111 | unsigned long long pledgedSrcSize); | |
2112 | ||
2113 | /*! ZSTD_initCStream_usingCDict() : | |
2114 | * This function is deprecated, and equivalent to: | |
2115 | * ZSTD_CCtx_reset(zcs, ZSTD_reset_session_only); | |
2116 | * ZSTD_CCtx_refCDict(zcs, cdict); | |
2117 | * | |
2118 | * note : cdict will just be referenced, and must outlive compression session | |
2119 | * Note : this prototype will be marked as deprecated and generate compilation warnings on reaching v1.5.x | |
2120 | */ | |
2121 | ZSTDLIB_API size_t ZSTD_initCStream_usingCDict(ZSTD_CStream* zcs, const ZSTD_CDict* cdict); | |
2122 | ||
2123 | /*! ZSTD_initCStream_usingCDict_advanced() : | |
2124 | * This function is DEPRECATED, and is approximately equivalent to: | |
2125 | * ZSTD_CCtx_reset(zcs, ZSTD_reset_session_only); | |
2126 | * // Pseudocode: Set each zstd frame parameter and leave the rest as-is. | |
2127 | * for ((fParam, value) : fParams) { | |
2128 | * ZSTD_CCtx_setParameter(zcs, fParam, value); | |
2129 | * } | |
2130 | * ZSTD_CCtx_setPledgedSrcSize(zcs, pledgedSrcSize); | |
2131 | * ZSTD_CCtx_refCDict(zcs, cdict); | |
2132 | * | |
2133 | * same as ZSTD_initCStream_usingCDict(), with control over frame parameters. | |
2134 | * pledgedSrcSize must be correct. If srcSize is not known at init time, use | |
2135 | * value ZSTD_CONTENTSIZE_UNKNOWN. | |
2136 | * Note : this prototype will be marked as deprecated and generate compilation warnings on reaching v1.5.x | |
2137 | */ | |
2138 | ZSTDLIB_API size_t | |
2139 | ZSTD_initCStream_usingCDict_advanced(ZSTD_CStream* zcs, | |
2140 | const ZSTD_CDict* cdict, | |
2141 | ZSTD_frameParameters fParams, | |
2142 | unsigned long long pledgedSrcSize); | |
2143 | ||
2144 | /*! ZSTD_resetCStream() : | |
2145 | * This function is deprecated, and is equivalent to: | |
2146 | * ZSTD_CCtx_reset(zcs, ZSTD_reset_session_only); | |
2147 | * ZSTD_CCtx_setPledgedSrcSize(zcs, pledgedSrcSize); | |
2148 | * | |
2149 | * start a new frame, using same parameters from previous frame. | |
2150 | * This is typically useful to skip dictionary loading stage, since it will re-use it in-place. | |
2151 | * Note that zcs must be init at least once before using ZSTD_resetCStream(). | |
2152 | * If pledgedSrcSize is not known at reset time, use macro ZSTD_CONTENTSIZE_UNKNOWN. | |
2153 | * If pledgedSrcSize > 0, its value must be correct, as it will be written in header, and controlled at the end. | |
2154 | * For the time being, pledgedSrcSize==0 is interpreted as "srcSize unknown" for compatibility with older programs, | |
2155 | * but it will change to mean "empty" in future version, so use macro ZSTD_CONTENTSIZE_UNKNOWN instead. | |
2156 | * @return : 0, or an error code (which can be tested using ZSTD_isError()) | |
2157 | * Note : this prototype will be marked as deprecated and generate compilation warnings on reaching v1.5.x | |
2158 | */ | |
2159 | ZSTDLIB_API size_t ZSTD_resetCStream(ZSTD_CStream* zcs, unsigned long long pledgedSrcSize); | |
cf30f6a5 | 2160 | |
e0c1b49f NT |
2161 | |
2162 | typedef struct { | |
2163 | unsigned long long ingested; /* nb input bytes read and buffered */ | |
2164 | unsigned long long consumed; /* nb input bytes actually compressed */ | |
2165 | unsigned long long produced; /* nb of compressed bytes generated and buffered */ | |
2166 | unsigned long long flushed; /* nb of compressed bytes flushed : not provided; can be tracked from caller side */ | |
2167 | unsigned currentJobID; /* MT only : latest started job nb */ | |
2168 | unsigned nbActiveWorkers; /* MT only : nb of workers actively compressing at probe time */ | |
2169 | } ZSTD_frameProgression; | |
2170 | ||
2171 | /* ZSTD_getFrameProgression() : | |
2172 | * tells how much data has been ingested (read from input) | |
2173 | * consumed (input actually compressed) and produced (output) for current frame. | |
2174 | * Note : (ingested - consumed) is amount of input data buffered internally, not yet compressed. | |
2175 | * Aggregates progression inside active worker threads. | |
cf30f6a5 | 2176 | */ |
e0c1b49f NT |
2177 | ZSTDLIB_API ZSTD_frameProgression ZSTD_getFrameProgression(const ZSTD_CCtx* cctx); |
2178 | ||
2179 | /*! ZSTD_toFlushNow() : | |
2180 | * Tell how many bytes are ready to be flushed immediately. | |
2181 | * Useful for multithreading scenarios (nbWorkers >= 1). | |
2182 | * Probe the oldest active job, defined as oldest job not yet entirely flushed, | |
2183 | * and check its output buffer. | |
2184 | * @return : amount of data stored in oldest job and ready to be flushed immediately. | |
2185 | * if @return == 0, it means either : | |
2186 | * + there is no active job (could be checked with ZSTD_frameProgression()), or | |
2187 | * + oldest job is still actively compressing data, | |
2188 | * but everything it has produced has also been flushed so far, | |
2189 | * therefore flush speed is limited by production speed of oldest job | |
2190 | * irrespective of the speed of concurrent (and newer) jobs. | |
2191 | */ | |
2192 | ZSTDLIB_API size_t ZSTD_toFlushNow(ZSTD_CCtx* cctx); | |
cf30f6a5 | 2193 | |
e0c1b49f NT |
2194 | |
2195 | /*===== Advanced Streaming decompression functions =====*/ | |
2196 | ||
2197 | /*! | |
2198 | * This function is deprecated, and is equivalent to: | |
cf30f6a5 | 2199 | * |
e0c1b49f NT |
2200 | * ZSTD_DCtx_reset(zds, ZSTD_reset_session_only); |
2201 | * ZSTD_DCtx_loadDictionary(zds, dict, dictSize); | |
cf30f6a5 | 2202 | * |
e0c1b49f NT |
2203 | * note: no dictionary will be used if dict == NULL or dictSize < 8 |
2204 | * Note : this prototype will be marked as deprecated and generate compilation warnings on reaching v1.5.x | |
cf30f6a5 | 2205 | */ |
e0c1b49f NT |
2206 | ZSTDLIB_API size_t ZSTD_initDStream_usingDict(ZSTD_DStream* zds, const void* dict, size_t dictSize); |
2207 | ||
2208 | /*! | |
2209 | * This function is deprecated, and is equivalent to: | |
cf30f6a5 | 2210 | * |
e0c1b49f NT |
2211 | * ZSTD_DCtx_reset(zds, ZSTD_reset_session_only); |
2212 | * ZSTD_DCtx_refDDict(zds, ddict); | |
cf30f6a5 | 2213 | * |
e0c1b49f NT |
2214 | * note : ddict is referenced, it must outlive decompression session |
2215 | * Note : this prototype will be marked as deprecated and generate compilation warnings on reaching v1.5.x | |
cf30f6a5 | 2216 | */ |
e0c1b49f | 2217 | ZSTDLIB_API size_t ZSTD_initDStream_usingDDict(ZSTD_DStream* zds, const ZSTD_DDict* ddict); |
cf30f6a5 | 2218 | |
e0c1b49f NT |
2219 | /*! |
2220 | * This function is deprecated, and is equivalent to: | |
cf30f6a5 | 2221 | * |
e0c1b49f | 2222 | * ZSTD_DCtx_reset(zds, ZSTD_reset_session_only); |
cf30f6a5 | 2223 | * |
e0c1b49f NT |
2224 | * re-use decompression parameters from previous init; saves dictionary loading |
2225 | * Note : this prototype will be marked as deprecated and generate compilation warnings on reaching v1.5.x | |
cf30f6a5 | 2226 | */ |
e0c1b49f | 2227 | ZSTDLIB_API size_t ZSTD_resetDStream(ZSTD_DStream* zds); |
cf30f6a5 NT |
2228 | |
2229 | ||
e0c1b49f NT |
2230 | /* ******************************************************************* |
2231 | * Buffer-less and synchronous inner streaming functions | |
2232 | * | |
2233 | * This is an advanced API, giving full control over buffer management, for users which need direct control over memory. | |
2234 | * But it's also a complex one, with several restrictions, documented below. | |
2235 | * Prefer normal streaming API for an easier experience. | |
2236 | ********************************************************************* */ | |
cf30f6a5 | 2237 | |
e0c1b49f NT |
2238 | /* |
2239 | Buffer-less streaming compression (synchronous mode) | |
2240 | ||
2241 | A ZSTD_CCtx object is required to track streaming operations. | |
2242 | Use ZSTD_createCCtx() / ZSTD_freeCCtx() to manage resource. | |
2243 | ZSTD_CCtx object can be re-used multiple times within successive compression operations. | |
2244 | ||
2245 | Start by initializing a context. | |
2246 | Use ZSTD_compressBegin(), or ZSTD_compressBegin_usingDict() for dictionary compression, | |
2247 | or ZSTD_compressBegin_advanced(), for finer parameter control. | |
2248 | It's also possible to duplicate a reference context which has already been initialized, using ZSTD_copyCCtx() | |
2249 | ||
2250 | Then, consume your input using ZSTD_compressContinue(). | |
2251 | There are some important considerations to keep in mind when using this advanced function : | |
2252 | - ZSTD_compressContinue() has no internal buffer. It uses externally provided buffers only. | |
2253 | - Interface is synchronous : input is consumed entirely and produces 1+ compressed blocks. | |
2254 | - Caller must ensure there is enough space in `dst` to store compressed data under worst case scenario. | |
2255 | Worst case evaluation is provided by ZSTD_compressBound(). | |
2256 | ZSTD_compressContinue() doesn't guarantee recover after a failed compression. | |
2257 | - ZSTD_compressContinue() presumes prior input ***is still accessible and unmodified*** (up to maximum distance size, see WindowLog). | |
2258 | It remembers all previous contiguous blocks, plus one separated memory segment (which can itself consists of multiple contiguous blocks) | |
2259 | - ZSTD_compressContinue() detects that prior input has been overwritten when `src` buffer overlaps. | |
2260 | In which case, it will "discard" the relevant memory section from its history. | |
2261 | ||
2262 | Finish a frame with ZSTD_compressEnd(), which will write the last block(s) and optional checksum. | |
2263 | It's possible to use srcSize==0, in which case, it will write a final empty block to end the frame. | |
2264 | Without last block mark, frames are considered unfinished (hence corrupted) by compliant decoders. | |
2265 | ||
2266 | `ZSTD_CCtx` object can be re-used (ZSTD_compressBegin()) to compress again. | |
2267 | */ | |
cf30f6a5 | 2268 | |
e0c1b49f NT |
2269 | /*===== Buffer-less streaming compression functions =====*/ |
2270 | ZSTDLIB_API size_t ZSTD_compressBegin(ZSTD_CCtx* cctx, int compressionLevel); | |
2271 | ZSTDLIB_API size_t ZSTD_compressBegin_usingDict(ZSTD_CCtx* cctx, const void* dict, size_t dictSize, int compressionLevel); | |
2272 | ZSTDLIB_API size_t ZSTD_compressBegin_advanced(ZSTD_CCtx* cctx, const void* dict, size_t dictSize, ZSTD_parameters params, unsigned long long pledgedSrcSize); /*< pledgedSrcSize : If srcSize is not known at init time, use ZSTD_CONTENTSIZE_UNKNOWN */ | |
2273 | ZSTDLIB_API size_t ZSTD_compressBegin_usingCDict(ZSTD_CCtx* cctx, const ZSTD_CDict* cdict); /*< note: fails if cdict==NULL */ | |
2274 | ZSTDLIB_API size_t ZSTD_compressBegin_usingCDict_advanced(ZSTD_CCtx* const cctx, const ZSTD_CDict* const cdict, ZSTD_frameParameters const fParams, unsigned long long const pledgedSrcSize); /* compression parameters are already set within cdict. pledgedSrcSize must be correct. If srcSize is not known, use macro ZSTD_CONTENTSIZE_UNKNOWN */ | |
2275 | ZSTDLIB_API size_t ZSTD_copyCCtx(ZSTD_CCtx* cctx, const ZSTD_CCtx* preparedCCtx, unsigned long long pledgedSrcSize); /*< note: if pledgedSrcSize is not known, use ZSTD_CONTENTSIZE_UNKNOWN */ | |
cf30f6a5 | 2276 | |
e0c1b49f NT |
2277 | ZSTDLIB_API size_t ZSTD_compressContinue(ZSTD_CCtx* cctx, void* dst, size_t dstCapacity, const void* src, size_t srcSize); |
2278 | ZSTDLIB_API size_t ZSTD_compressEnd(ZSTD_CCtx* cctx, void* dst, size_t dstCapacity, const void* src, size_t srcSize); | |
cf30f6a5 | 2279 | |
cf30f6a5 | 2280 | |
e0c1b49f NT |
2281 | /* |
2282 | Buffer-less streaming decompression (synchronous mode) | |
2283 | ||
2284 | A ZSTD_DCtx object is required to track streaming operations. | |
2285 | Use ZSTD_createDCtx() / ZSTD_freeDCtx() to manage it. | |
2286 | A ZSTD_DCtx object can be re-used multiple times. | |
2287 | ||
2288 | First typical operation is to retrieve frame parameters, using ZSTD_getFrameHeader(). | |
2289 | Frame header is extracted from the beginning of compressed frame, so providing only the frame's beginning is enough. | |
2290 | Data fragment must be large enough to ensure successful decoding. | |
2291 | `ZSTD_frameHeaderSize_max` bytes is guaranteed to always be large enough. | |
2292 | @result : 0 : successful decoding, the `ZSTD_frameHeader` structure is correctly filled. | |
2293 | >0 : `srcSize` is too small, please provide at least @result bytes on next attempt. | |
2294 | errorCode, which can be tested using ZSTD_isError(). | |
2295 | ||
2296 | It fills a ZSTD_frameHeader structure with important information to correctly decode the frame, | |
2297 | such as the dictionary ID, content size, or maximum back-reference distance (`windowSize`). | |
2298 | Note that these values could be wrong, either because of data corruption, or because a 3rd party deliberately spoofs false information. | |
2299 | As a consequence, check that values remain within valid application range. | |
2300 | For example, do not allocate memory blindly, check that `windowSize` is within expectation. | |
2301 | Each application can set its own limits, depending on local restrictions. | |
2302 | For extended interoperability, it is recommended to support `windowSize` of at least 8 MB. | |
2303 | ||
2304 | ZSTD_decompressContinue() needs previous data blocks during decompression, up to `windowSize` bytes. | |
2305 | ZSTD_decompressContinue() is very sensitive to contiguity, | |
2306 | if 2 blocks don't follow each other, make sure that either the compressor breaks contiguity at the same place, | |
2307 | or that previous contiguous segment is large enough to properly handle maximum back-reference distance. | |
2308 | There are multiple ways to guarantee this condition. | |
2309 | ||
2310 | The most memory efficient way is to use a round buffer of sufficient size. | |
2311 | Sufficient size is determined by invoking ZSTD_decodingBufferSize_min(), | |
2312 | which can @return an error code if required value is too large for current system (in 32-bits mode). | |
2313 | In a round buffer methodology, ZSTD_decompressContinue() decompresses each block next to previous one, | |
2314 | up to the moment there is not enough room left in the buffer to guarantee decoding another full block, | |
2315 | which maximum size is provided in `ZSTD_frameHeader` structure, field `blockSizeMax`. | |
2316 | At which point, decoding can resume from the beginning of the buffer. | |
2317 | Note that already decoded data stored in the buffer should be flushed before being overwritten. | |
2318 | ||
2319 | There are alternatives possible, for example using two or more buffers of size `windowSize` each, though they consume more memory. | |
2320 | ||
2321 | Finally, if you control the compression process, you can also ignore all buffer size rules, | |
2322 | as long as the encoder and decoder progress in "lock-step", | |
2323 | aka use exactly the same buffer sizes, break contiguity at the same place, etc. | |
2324 | ||
2325 | Once buffers are setup, start decompression, with ZSTD_decompressBegin(). | |
2326 | If decompression requires a dictionary, use ZSTD_decompressBegin_usingDict() or ZSTD_decompressBegin_usingDDict(). | |
2327 | ||
2328 | Then use ZSTD_nextSrcSizeToDecompress() and ZSTD_decompressContinue() alternatively. | |
2329 | ZSTD_nextSrcSizeToDecompress() tells how many bytes to provide as 'srcSize' to ZSTD_decompressContinue(). | |
2330 | ZSTD_decompressContinue() requires this _exact_ amount of bytes, or it will fail. | |
2331 | ||
2332 | @result of ZSTD_decompressContinue() is the number of bytes regenerated within 'dst' (necessarily <= dstCapacity). | |
2333 | It can be zero : it just means ZSTD_decompressContinue() has decoded some metadata item. | |
2334 | It can also be an error code, which can be tested with ZSTD_isError(). | |
2335 | ||
2336 | A frame is fully decoded when ZSTD_nextSrcSizeToDecompress() returns zero. | |
2337 | Context can then be reset to start a new decompression. | |
2338 | ||
2339 | Note : it's possible to know if next input to present is a header or a block, using ZSTD_nextInputType(). | |
2340 | This information is not required to properly decode a frame. | |
2341 | ||
2342 | == Special case : skippable frames == | |
2343 | ||
2344 | Skippable frames allow integration of user-defined data into a flow of concatenated frames. | |
2345 | Skippable frames will be ignored (skipped) by decompressor. | |
2346 | The format of skippable frames is as follows : | |
2347 | a) Skippable frame ID - 4 Bytes, Little endian format, any value from 0x184D2A50 to 0x184D2A5F | |
2348 | b) Frame Size - 4 Bytes, Little endian format, unsigned 32-bits | |
2349 | c) Frame Content - any content (User Data) of length equal to Frame Size | |
2350 | For skippable frames ZSTD_getFrameHeader() returns zfhPtr->frameType==ZSTD_skippableFrame. | |
2351 | For skippable frames ZSTD_decompressContinue() always returns 0 : it only skips the content. | |
2352 | */ | |
2353 | ||
2354 | /*===== Buffer-less streaming decompression functions =====*/ | |
2355 | typedef enum { ZSTD_frame, ZSTD_skippableFrame } ZSTD_frameType_e; | |
cf30f6a5 | 2356 | typedef struct { |
e0c1b49f NT |
2357 | unsigned long long frameContentSize; /* if == ZSTD_CONTENTSIZE_UNKNOWN, it means this field is not available. 0 means "empty" */ |
2358 | unsigned long long windowSize; /* can be very large, up to <= frameContentSize */ | |
2359 | unsigned blockSizeMax; | |
2360 | ZSTD_frameType_e frameType; /* if == ZSTD_skippableFrame, frameContentSize is the size of skippable content */ | |
2361 | unsigned headerSize; | |
2362 | unsigned dictID; | |
2363 | unsigned checksumFlag; | |
2364 | } ZSTD_frameHeader; | |
2365 | ||
2366 | /*! ZSTD_getFrameHeader() : | |
2367 | * decode Frame Header, or requires larger `srcSize`. | |
2368 | * @return : 0, `zfhPtr` is correctly filled, | |
2369 | * >0, `srcSize` is too small, value is wanted `srcSize` amount, | |
2370 | * or an error code, which can be tested using ZSTD_isError() */ | |
2371 | ZSTDLIB_API size_t ZSTD_getFrameHeader(ZSTD_frameHeader* zfhPtr, const void* src, size_t srcSize); /*< doesn't consume input */ | |
2372 | /*! ZSTD_getFrameHeader_advanced() : | |
2373 | * same as ZSTD_getFrameHeader(), | |
2374 | * with added capability to select a format (like ZSTD_f_zstd1_magicless) */ | |
2375 | ZSTDLIB_API size_t ZSTD_getFrameHeader_advanced(ZSTD_frameHeader* zfhPtr, const void* src, size_t srcSize, ZSTD_format_e format); | |
2376 | ZSTDLIB_API size_t ZSTD_decodingBufferSize_min(unsigned long long windowSize, unsigned long long frameContentSize); /*< when frame content size is not known, pass in frameContentSize == ZSTD_CONTENTSIZE_UNKNOWN */ | |
2377 | ||
2378 | ZSTDLIB_API size_t ZSTD_decompressBegin(ZSTD_DCtx* dctx); | |
2379 | ZSTDLIB_API size_t ZSTD_decompressBegin_usingDict(ZSTD_DCtx* dctx, const void* dict, size_t dictSize); | |
2380 | ZSTDLIB_API size_t ZSTD_decompressBegin_usingDDict(ZSTD_DCtx* dctx, const ZSTD_DDict* ddict); | |
2381 | ||
2382 | ZSTDLIB_API size_t ZSTD_nextSrcSizeToDecompress(ZSTD_DCtx* dctx); | |
2383 | ZSTDLIB_API size_t ZSTD_decompressContinue(ZSTD_DCtx* dctx, void* dst, size_t dstCapacity, const void* src, size_t srcSize); | |
2384 | ||
2385 | /* misc */ | |
2386 | ZSTDLIB_API void ZSTD_copyDCtx(ZSTD_DCtx* dctx, const ZSTD_DCtx* preparedDCtx); | |
2387 | typedef enum { ZSTDnit_frameHeader, ZSTDnit_blockHeader, ZSTDnit_block, ZSTDnit_lastBlock, ZSTDnit_checksum, ZSTDnit_skippableFrame } ZSTD_nextInputType_e; | |
2388 | ZSTDLIB_API ZSTD_nextInputType_e ZSTD_nextInputType(ZSTD_DCtx* dctx); | |
2389 | ||
2390 | ||
2391 | ||
2392 | ||
2393 | /* ============================ */ | |
2394 | /* Block level API */ | |
2395 | /* ============================ */ | |
2396 | ||
2397 | /*! | |
2398 | Block functions produce and decode raw zstd blocks, without frame metadata. | |
2399 | Frame metadata cost is typically ~12 bytes, which can be non-negligible for very small blocks (< 100 bytes). | |
2400 | But users will have to take in charge needed metadata to regenerate data, such as compressed and content sizes. | |
2401 | ||
2402 | A few rules to respect : | |
2403 | - Compressing and decompressing require a context structure | |
2404 | + Use ZSTD_createCCtx() and ZSTD_createDCtx() | |
2405 | - It is necessary to init context before starting | |
2406 | + compression : any ZSTD_compressBegin*() variant, including with dictionary | |
2407 | + decompression : any ZSTD_decompressBegin*() variant, including with dictionary | |
2408 | + copyCCtx() and copyDCtx() can be used too | |
2409 | - Block size is limited, it must be <= ZSTD_getBlockSize() <= ZSTD_BLOCKSIZE_MAX == 128 KB | |
2410 | + If input is larger than a block size, it's necessary to split input data into multiple blocks | |
2411 | + For inputs larger than a single block, consider using regular ZSTD_compress() instead. | |
2412 | Frame metadata is not that costly, and quickly becomes negligible as source size grows larger than a block. | |
2413 | - When a block is considered not compressible enough, ZSTD_compressBlock() result will be 0 (zero) ! | |
2414 | ===> In which case, nothing is produced into `dst` ! | |
2415 | + User __must__ test for such outcome and deal directly with uncompressed data | |
2416 | + A block cannot be declared incompressible if ZSTD_compressBlock() return value was != 0. | |
2417 | Doing so would mess up with statistics history, leading to potential data corruption. | |
2418 | + ZSTD_decompressBlock() _doesn't accept uncompressed data as input_ !! | |
2419 | + In case of multiple successive blocks, should some of them be uncompressed, | |
2420 | decoder must be informed of their existence in order to follow proper history. | |
2421 | Use ZSTD_insertBlock() for such a case. | |
2422 | */ | |
cf30f6a5 | 2423 | |
e0c1b49f NT |
2424 | /*===== Raw zstd block functions =====*/ |
2425 | ZSTDLIB_API size_t ZSTD_getBlockSize (const ZSTD_CCtx* cctx); | |
2426 | ZSTDLIB_API size_t ZSTD_compressBlock (ZSTD_CCtx* cctx, void* dst, size_t dstCapacity, const void* src, size_t srcSize); | |
2427 | ZSTDLIB_API size_t ZSTD_decompressBlock(ZSTD_DCtx* dctx, void* dst, size_t dstCapacity, const void* src, size_t srcSize); | |
2428 | ZSTDLIB_API size_t ZSTD_insertBlock (ZSTD_DCtx* dctx, const void* blockStart, size_t blockSize); /*< insert uncompressed block into `dctx` history. Useful for multi-blocks decompression. */ | |
cf30f6a5 | 2429 | |
cf30f6a5 | 2430 | |
e0c1b49f | 2431 | #endif /* ZSTD_H_ZSTD_STATIC_LINKING_ONLY */ |
cf30f6a5 | 2432 |