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