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