blob: a1910ee223e92abccadf071ef835b35ad0fcef58 [file] [log] [blame]
Scott Bakereee8dd82019-09-24 12:52:34 -07001/*
2 * Copyright (c) 2016-present, 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#if defined (__cplusplus)
11extern "C" {
12#endif
13
14#ifndef ZSTD_H_235446
15#define ZSTD_H_235446
16
17/* ====== Dependency ======*/
18#include <stddef.h> /* size_t */
19
20
21/* ===== ZSTDLIB_API : control library symbols visibility ===== */
22#ifndef ZSTDLIB_VISIBILITY
23# if defined(__GNUC__) && (__GNUC__ >= 4)
24# define ZSTDLIB_VISIBILITY __attribute__ ((visibility ("default")))
25# else
26# define ZSTDLIB_VISIBILITY
27# endif
28#endif
29#if defined(ZSTD_DLL_EXPORT) && (ZSTD_DLL_EXPORT==1)
30# define ZSTDLIB_API __declspec(dllexport) ZSTDLIB_VISIBILITY
31#elif defined(ZSTD_DLL_IMPORT) && (ZSTD_DLL_IMPORT==1)
32# define ZSTDLIB_API __declspec(dllimport) ZSTDLIB_VISIBILITY /* It isn't required but allows to generate better code, saving a function pointer load from the IAT and an indirect jump.*/
33#else
34# define ZSTDLIB_API ZSTDLIB_VISIBILITY
35#endif
36
37
Scott Baker611f6bd2019-10-18 13:45:19 -070038/*******************************************************************************
Scott Bakereee8dd82019-09-24 12:52:34 -070039 Introduction
40
Scott Baker611f6bd2019-10-18 13:45:19 -070041 zstd, short for Zstandard, is a fast lossless compression algorithm, targeting
42 real-time compression scenarios at zlib-level and better compression ratios.
43 The zstd compression library provides in-memory compression and decompression
44 functions.
45
46 The library supports regular compression levels from 1 up to ZSTD_maxCLevel(),
47 which is currently 22. Levels >= 20, labeled `--ultra`, should be used with
48 caution, as they require more memory. The library also offers negative
49 compression levels, which extend the range of speed vs. ratio preferences.
50 The lower the level, the faster the speed (at the cost of compression).
51
Scott Bakereee8dd82019-09-24 12:52:34 -070052 Compression can be done in:
53 - a single step (described as Simple API)
54 - a single step, reusing a context (described as Explicit context)
55 - unbounded multiple steps (described as Streaming compression)
Scott Bakereee8dd82019-09-24 12:52:34 -070056
Scott Baker611f6bd2019-10-18 13:45:19 -070057 The compression ratio achievable on small data can be highly improved using
58 a dictionary. Dictionary compression can be performed in:
59 - a single step (described as Simple dictionary API)
60 - a single step, reusing a dictionary (described as Bulk-processing
61 dictionary API)
62
63 Advanced experimental functions can be accessed using
64 `#define ZSTD_STATIC_LINKING_ONLY` before including zstd.h.
65
66 Advanced experimental APIs should never be used with a dynamically-linked
67 library. They are not "stable"; their definitions or signatures may change in
68 the future. Only static linking is allowed.
69*******************************************************************************/
Scott Bakereee8dd82019-09-24 12:52:34 -070070
71/*------ Version ------*/
72#define ZSTD_VERSION_MAJOR 1
Scott Baker611f6bd2019-10-18 13:45:19 -070073#define ZSTD_VERSION_MINOR 4
74#define ZSTD_VERSION_RELEASE 1
Scott Bakereee8dd82019-09-24 12:52:34 -070075
76#define ZSTD_VERSION_NUMBER (ZSTD_VERSION_MAJOR *100*100 + ZSTD_VERSION_MINOR *100 + ZSTD_VERSION_RELEASE)
Scott Baker611f6bd2019-10-18 13:45:19 -070077ZSTDLIB_API unsigned ZSTD_versionNumber(void); /**< to check runtime library version */
Scott Bakereee8dd82019-09-24 12:52:34 -070078
79#define ZSTD_LIB_VERSION ZSTD_VERSION_MAJOR.ZSTD_VERSION_MINOR.ZSTD_VERSION_RELEASE
80#define ZSTD_QUOTE(str) #str
81#define ZSTD_EXPAND_AND_QUOTE(str) ZSTD_QUOTE(str)
82#define ZSTD_VERSION_STRING ZSTD_EXPAND_AND_QUOTE(ZSTD_LIB_VERSION)
Scott Baker611f6bd2019-10-18 13:45:19 -070083ZSTDLIB_API const char* ZSTD_versionString(void); /* requires v1.3.0+ */
84
85/* *************************************
86 * Default constant
87 ***************************************/
88#ifndef ZSTD_CLEVEL_DEFAULT
89# define ZSTD_CLEVEL_DEFAULT 3
90#endif
91
92/* *************************************
93 * Constants
94 ***************************************/
95
96/* All magic numbers are supposed read/written to/from files/memory using little-endian convention */
97#define ZSTD_MAGICNUMBER 0xFD2FB528 /* valid since v0.8.0 */
98#define ZSTD_MAGIC_DICTIONARY 0xEC30A437 /* valid since v0.7.0 */
99#define ZSTD_MAGIC_SKIPPABLE_START 0x184D2A50 /* all 16 values, from 0x184D2A50 to 0x184D2A5F, signal the beginning of a skippable frame */
100#define ZSTD_MAGIC_SKIPPABLE_MASK 0xFFFFFFF0
101
102#define ZSTD_BLOCKSIZELOG_MAX 17
103#define ZSTD_BLOCKSIZE_MAX (1<<ZSTD_BLOCKSIZELOG_MAX)
104
Scott Bakereee8dd82019-09-24 12:52:34 -0700105
106
107/***************************************
108* Simple API
109***************************************/
110/*! ZSTD_compress() :
111 * Compresses `src` content as a single zstd compressed frame into already allocated `dst`.
112 * Hint : compression runs faster if `dstCapacity` >= `ZSTD_compressBound(srcSize)`.
113 * @return : compressed size written into `dst` (<= `dstCapacity),
114 * or an error code if it fails (which can be tested using ZSTD_isError()). */
115ZSTDLIB_API size_t ZSTD_compress( void* dst, size_t dstCapacity,
116 const void* src, size_t srcSize,
117 int compressionLevel);
118
119/*! ZSTD_decompress() :
120 * `compressedSize` : must be the _exact_ size of some number of compressed and/or skippable frames.
121 * `dstCapacity` is an upper bound of originalSize to regenerate.
122 * If user cannot imply a maximum upper bound, it's better to use streaming mode to decompress data.
123 * @return : the number of bytes decompressed into `dst` (<= `dstCapacity`),
124 * or an errorCode if it fails (which can be tested using ZSTD_isError()). */
125ZSTDLIB_API size_t ZSTD_decompress( void* dst, size_t dstCapacity,
126 const void* src, size_t compressedSize);
127
Scott Baker611f6bd2019-10-18 13:45:19 -0700128/*! ZSTD_getFrameContentSize() : requires v1.3.0+
Scott Bakereee8dd82019-09-24 12:52:34 -0700129 * `src` should point to the start of a ZSTD encoded frame.
130 * `srcSize` must be at least as large as the frame header.
131 * hint : any size >= `ZSTD_frameHeaderSize_max` is large enough.
Scott Baker611f6bd2019-10-18 13:45:19 -0700132 * @return : - decompressed size of `src` frame content, if known
Scott Bakereee8dd82019-09-24 12:52:34 -0700133 * - ZSTD_CONTENTSIZE_UNKNOWN if the size cannot be determined
134 * - ZSTD_CONTENTSIZE_ERROR if an error occurred (e.g. invalid magic number, srcSize too small)
135 * note 1 : a 0 return value means the frame is valid but "empty".
136 * note 2 : decompressed size is an optional field, it may not be present, typically in streaming mode.
137 * When `return==ZSTD_CONTENTSIZE_UNKNOWN`, data to decompress could be any size.
138 * In which case, it's necessary to use streaming mode to decompress data.
139 * Optionally, application can rely on some implicit limit,
140 * as ZSTD_decompress() only needs an upper bound of decompressed size.
141 * (For example, data could be necessarily cut into blocks <= 16 KB).
Scott Baker611f6bd2019-10-18 13:45:19 -0700142 * note 3 : decompressed size is always present when compression is completed using single-pass functions,
143 * such as ZSTD_compress(), ZSTD_compressCCtx() ZSTD_compress_usingDict() or ZSTD_compress_usingCDict().
Scott Bakereee8dd82019-09-24 12:52:34 -0700144 * note 4 : decompressed size can be very large (64-bits value),
145 * potentially larger than what local system can handle as a single memory segment.
146 * In which case, it's necessary to use streaming mode to decompress data.
147 * note 5 : If source is untrusted, decompressed size could be wrong or intentionally modified.
148 * Always ensure return value fits within application's authorized limits.
149 * Each application can set its own limits.
150 * note 6 : This function replaces ZSTD_getDecompressedSize() */
151#define ZSTD_CONTENTSIZE_UNKNOWN (0ULL - 1)
152#define ZSTD_CONTENTSIZE_ERROR (0ULL - 2)
153ZSTDLIB_API unsigned long long ZSTD_getFrameContentSize(const void *src, size_t srcSize);
154
155/*! ZSTD_getDecompressedSize() :
156 * NOTE: This function is now obsolete, in favor of ZSTD_getFrameContentSize().
157 * Both functions work the same way, but ZSTD_getDecompressedSize() blends
158 * "empty", "unknown" and "error" results to the same return value (0),
159 * while ZSTD_getFrameContentSize() gives them separate return values.
Scott Baker611f6bd2019-10-18 13:45:19 -0700160 * @return : decompressed size of `src` frame content _if known and not empty_, 0 otherwise. */
Scott Bakereee8dd82019-09-24 12:52:34 -0700161ZSTDLIB_API unsigned long long ZSTD_getDecompressedSize(const void* src, size_t srcSize);
162
Scott Baker611f6bd2019-10-18 13:45:19 -0700163/*! ZSTD_findFrameCompressedSize() :
164 * `src` should point to the start of a ZSTD frame or skippable frame.
165 * `srcSize` must be >= first frame size
166 * @return : the compressed size of the first frame starting at `src`,
167 * suitable to pass as `srcSize` to `ZSTD_decompress` or similar,
168 * or an error code if input is invalid */
169ZSTDLIB_API size_t ZSTD_findFrameCompressedSize(const void* src, size_t srcSize);
170
Scott Bakereee8dd82019-09-24 12:52:34 -0700171
172/*====== Helper functions ======*/
173#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 */
174ZSTDLIB_API size_t ZSTD_compressBound(size_t srcSize); /*!< maximum compressed size in worst case single-pass scenario */
175ZSTDLIB_API unsigned ZSTD_isError(size_t code); /*!< tells if a `size_t` function result is an error code */
176ZSTDLIB_API const char* ZSTD_getErrorName(size_t code); /*!< provides readable string from an error code */
Scott Baker611f6bd2019-10-18 13:45:19 -0700177ZSTDLIB_API int ZSTD_minCLevel(void); /*!< minimum negative compression level allowed */
Scott Bakereee8dd82019-09-24 12:52:34 -0700178ZSTDLIB_API int ZSTD_maxCLevel(void); /*!< maximum compression level available */
179
180
181/***************************************
182* Explicit context
183***************************************/
184/*= Compression context
185 * When compressing many times,
Scott Baker611f6bd2019-10-18 13:45:19 -0700186 * it is recommended to allocate a context just once,
187 * and re-use it for each successive compression operation.
Scott Bakereee8dd82019-09-24 12:52:34 -0700188 * This will make workload friendlier for system's memory.
Scott Baker611f6bd2019-10-18 13:45:19 -0700189 * Note : re-using context is just a speed / resource optimization.
190 * It doesn't change the compression ratio, which remains identical.
191 * Note 2 : In multi-threaded environments,
192 * use one different context per thread for parallel execution.
193 */
Scott Bakereee8dd82019-09-24 12:52:34 -0700194typedef struct ZSTD_CCtx_s ZSTD_CCtx;
195ZSTDLIB_API ZSTD_CCtx* ZSTD_createCCtx(void);
196ZSTDLIB_API size_t ZSTD_freeCCtx(ZSTD_CCtx* cctx);
197
198/*! ZSTD_compressCCtx() :
Scott Baker611f6bd2019-10-18 13:45:19 -0700199 * Same as ZSTD_compress(), using an explicit ZSTD_CCtx
200 * The function will compress at requested compression level,
201 * ignoring any other parameter */
202ZSTDLIB_API size_t ZSTD_compressCCtx(ZSTD_CCtx* cctx,
Scott Bakereee8dd82019-09-24 12:52:34 -0700203 void* dst, size_t dstCapacity,
204 const void* src, size_t srcSize,
205 int compressionLevel);
206
207/*= Decompression context
208 * When decompressing many times,
209 * it is recommended to allocate a context only once,
210 * and re-use it for each successive compression operation.
211 * This will make workload friendlier for system's memory.
212 * Use one context per thread for parallel execution. */
213typedef struct ZSTD_DCtx_s ZSTD_DCtx;
214ZSTDLIB_API ZSTD_DCtx* ZSTD_createDCtx(void);
215ZSTDLIB_API size_t ZSTD_freeDCtx(ZSTD_DCtx* dctx);
216
217/*! ZSTD_decompressDCtx() :
Scott Baker611f6bd2019-10-18 13:45:19 -0700218 * Same as ZSTD_decompress(),
219 * requires an allocated ZSTD_DCtx.
220 * Compatible with sticky parameters.
221 */
222ZSTDLIB_API size_t ZSTD_decompressDCtx(ZSTD_DCtx* dctx,
Scott Bakereee8dd82019-09-24 12:52:34 -0700223 void* dst, size_t dstCapacity,
224 const void* src, size_t srcSize);
225
226
Scott Baker611f6bd2019-10-18 13:45:19 -0700227/***************************************
228* Advanced compression API
229***************************************/
Scott Bakereee8dd82019-09-24 12:52:34 -0700230
Scott Baker611f6bd2019-10-18 13:45:19 -0700231/* API design :
232 * Parameters are pushed one by one into an existing context,
233 * using ZSTD_CCtx_set*() functions.
234 * Pushed parameters are sticky : they are valid for next compressed frame, and any subsequent frame.
235 * "sticky" parameters are applicable to `ZSTD_compress2()` and `ZSTD_compressStream*()` !
236 * They do not apply to "simple" one-shot variants such as ZSTD_compressCCtx()
237 *
238 * It's possible to reset all parameters to "default" using ZSTD_CCtx_reset().
239 *
240 * This API supercedes all other "advanced" API entry points in the experimental section.
241 * In the future, we expect to remove from experimental API entry points which are redundant with this API.
242 */
Scott Bakereee8dd82019-09-24 12:52:34 -0700243
244
Scott Baker611f6bd2019-10-18 13:45:19 -0700245/* Compression strategies, listed from fastest to strongest */
246typedef enum { ZSTD_fast=1,
247 ZSTD_dfast=2,
248 ZSTD_greedy=3,
249 ZSTD_lazy=4,
250 ZSTD_lazy2=5,
251 ZSTD_btlazy2=6,
252 ZSTD_btopt=7,
253 ZSTD_btultra=8,
254 ZSTD_btultra2=9
255 /* note : new strategies _might_ be added in the future.
256 Only the order (from fast to strong) is guaranteed */
257} ZSTD_strategy;
Scott Bakereee8dd82019-09-24 12:52:34 -0700258
259
Scott Baker611f6bd2019-10-18 13:45:19 -0700260typedef enum {
Scott Bakereee8dd82019-09-24 12:52:34 -0700261
Scott Baker611f6bd2019-10-18 13:45:19 -0700262 /* compression parameters
263 * Note: When compressing with a ZSTD_CDict these parameters are superseded
264 * by the parameters used to construct the ZSTD_CDict. See ZSTD_CCtx_refCDict()
265 * for more info (superseded-by-cdict). */
266 ZSTD_c_compressionLevel=100, /* Update all compression parameters according to pre-defined cLevel table
267 * Default level is ZSTD_CLEVEL_DEFAULT==3.
268 * Special: value 0 means default, which is controlled by ZSTD_CLEVEL_DEFAULT.
269 * Note 1 : it's possible to pass a negative compression level.
270 * Note 2 : setting a level sets all default values of other compression parameters */
271 ZSTD_c_windowLog=101, /* Maximum allowed back-reference distance, expressed as power of 2.
272 * Must be clamped between ZSTD_WINDOWLOG_MIN and ZSTD_WINDOWLOG_MAX.
273 * Special: value 0 means "use default windowLog".
274 * Note: Using a windowLog greater than ZSTD_WINDOWLOG_LIMIT_DEFAULT
275 * requires explicitly allowing such window size at decompression stage if using streaming. */
276 ZSTD_c_hashLog=102, /* Size of the initial probe table, as a power of 2.
277 * Resulting memory usage is (1 << (hashLog+2)).
278 * Must be clamped between ZSTD_HASHLOG_MIN and ZSTD_HASHLOG_MAX.
279 * Larger tables improve compression ratio of strategies <= dFast,
280 * and improve speed of strategies > dFast.
281 * Special: value 0 means "use default hashLog". */
282 ZSTD_c_chainLog=103, /* Size of the multi-probe search table, as a power of 2.
283 * Resulting memory usage is (1 << (chainLog+2)).
284 * Must be clamped between ZSTD_CHAINLOG_MIN and ZSTD_CHAINLOG_MAX.
285 * Larger tables result in better and slower compression.
286 * This parameter is useless when using "fast" strategy.
287 * It's still useful when using "dfast" strategy,
288 * in which case it defines a secondary probe table.
289 * Special: value 0 means "use default chainLog". */
290 ZSTD_c_searchLog=104, /* Number of search attempts, as a power of 2.
291 * More attempts result in better and slower compression.
292 * This parameter is useless when using "fast" and "dFast" strategies.
293 * Special: value 0 means "use default searchLog". */
294 ZSTD_c_minMatch=105, /* Minimum size of searched matches.
295 * Note that Zstandard can still find matches of smaller size,
296 * it just tweaks its search algorithm to look for this size and larger.
297 * Larger values increase compression and decompression speed, but decrease ratio.
298 * Must be clamped between ZSTD_MINMATCH_MIN and ZSTD_MINMATCH_MAX.
299 * Note that currently, for all strategies < btopt, effective minimum is 4.
300 * , for all strategies > fast, effective maximum is 6.
301 * Special: value 0 means "use default minMatchLength". */
302 ZSTD_c_targetLength=106, /* Impact of this field depends on strategy.
303 * For strategies btopt, btultra & btultra2:
304 * Length of Match considered "good enough" to stop search.
305 * Larger values make compression stronger, and slower.
306 * For strategy fast:
307 * Distance between match sampling.
308 * Larger values make compression faster, and weaker.
309 * Special: value 0 means "use default targetLength". */
310 ZSTD_c_strategy=107, /* See ZSTD_strategy enum definition.
311 * The higher the value of selected strategy, the more complex it is,
312 * resulting in stronger and slower compression.
313 * Special: value 0 means "use default strategy". */
Scott Bakereee8dd82019-09-24 12:52:34 -0700314
Scott Baker611f6bd2019-10-18 13:45:19 -0700315 /* LDM mode parameters */
316 ZSTD_c_enableLongDistanceMatching=160, /* Enable long distance matching.
317 * This parameter is designed to improve compression ratio
318 * for large inputs, by finding large matches at long distance.
319 * It increases memory usage and window size.
320 * Note: enabling this parameter increases default ZSTD_c_windowLog to 128 MB
321 * except when expressly set to a different value. */
322 ZSTD_c_ldmHashLog=161, /* Size of the table for long distance matching, as a power of 2.
323 * Larger values increase memory usage and compression ratio,
324 * but decrease compression speed.
325 * Must be clamped between ZSTD_HASHLOG_MIN and ZSTD_HASHLOG_MAX
326 * default: windowlog - 7.
327 * Special: value 0 means "automatically determine hashlog". */
328 ZSTD_c_ldmMinMatch=162, /* Minimum match size for long distance matcher.
329 * Larger/too small values usually decrease compression ratio.
330 * Must be clamped between ZSTD_LDM_MINMATCH_MIN and ZSTD_LDM_MINMATCH_MAX.
331 * Special: value 0 means "use default value" (default: 64). */
332 ZSTD_c_ldmBucketSizeLog=163, /* Log size of each bucket in the LDM hash table for collision resolution.
333 * Larger values improve collision resolution but decrease compression speed.
334 * The maximum value is ZSTD_LDM_BUCKETSIZELOG_MAX.
335 * Special: value 0 means "use default value" (default: 3). */
336 ZSTD_c_ldmHashRateLog=164, /* Frequency of inserting/looking up entries into the LDM hash table.
337 * Must be clamped between 0 and (ZSTD_WINDOWLOG_MAX - ZSTD_HASHLOG_MIN).
338 * Default is MAX(0, (windowLog - ldmHashLog)), optimizing hash table usage.
339 * Larger values improve compression speed.
340 * Deviating far from default value will likely result in a compression ratio decrease.
341 * Special: value 0 means "automatically determine hashRateLog". */
Scott Bakereee8dd82019-09-24 12:52:34 -0700342
Scott Baker611f6bd2019-10-18 13:45:19 -0700343 /* frame parameters */
344 ZSTD_c_contentSizeFlag=200, /* Content size will be written into frame header _whenever known_ (default:1)
345 * Content size must be known at the beginning of compression.
346 * This is automatically the case when using ZSTD_compress2(),
347 * For streaming variants, content size must be provided with ZSTD_CCtx_setPledgedSrcSize() */
348 ZSTD_c_checksumFlag=201, /* A 32-bits checksum of content is written at end of frame (default:0) */
349 ZSTD_c_dictIDFlag=202, /* When applicable, dictionary's ID is written into frame header (default:1) */
350
351 /* multi-threading parameters */
352 /* These parameters are only useful if multi-threading is enabled (compiled with build macro ZSTD_MULTITHREAD).
353 * They return an error otherwise. */
354 ZSTD_c_nbWorkers=400, /* Select how many threads will be spawned to compress in parallel.
355 * When nbWorkers >= 1, triggers asynchronous mode when used with ZSTD_compressStream*() :
356 * ZSTD_compressStream*() consumes input and flush output if possible, but immediately gives back control to caller,
357 * while compression work is performed in parallel, within worker threads.
358 * (note : a strong exception to this rule is when first invocation of ZSTD_compressStream2() sets ZSTD_e_end :
359 * in which case, ZSTD_compressStream2() delegates to ZSTD_compress2(), which is always a blocking call).
360 * More workers improve speed, but also increase memory usage.
361 * Default value is `0`, aka "single-threaded mode" : no worker is spawned, compression is performed inside Caller's thread, all invocations are blocking */
362 ZSTD_c_jobSize=401, /* Size of a compression job. This value is enforced only when nbWorkers >= 1.
363 * Each compression job is completed in parallel, so this value can indirectly impact the nb of active threads.
364 * 0 means default, which is dynamically determined based on compression parameters.
365 * Job size must be a minimum of overlap size, or 1 MB, whichever is largest.
366 * The minimum size is automatically and transparently enforced */
367 ZSTD_c_overlapLog=402, /* Control the overlap size, as a fraction of window size.
368 * The overlap size is an amount of data reloaded from previous job at the beginning of a new job.
369 * It helps preserve compression ratio, while each job is compressed in parallel.
370 * This value is enforced only when nbWorkers >= 1.
371 * Larger values increase compression ratio, but decrease speed.
372 * Possible values range from 0 to 9 :
373 * - 0 means "default" : value will be determined by the library, depending on strategy
374 * - 1 means "no overlap"
375 * - 9 means "full overlap", using a full window size.
376 * Each intermediate rank increases/decreases load size by a factor 2 :
377 * 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
378 * default value varies between 6 and 9, depending on strategy */
379
380 /* note : additional experimental parameters are also available
381 * within the experimental section of the API.
382 * At the time of this writing, they include :
383 * ZSTD_c_rsyncable
384 * ZSTD_c_format
385 * ZSTD_c_forceMaxWindow
386 * ZSTD_c_forceAttachDict
387 * ZSTD_c_literalCompressionMode
388 * ZSTD_c_targetCBlockSize
389 * Because they are not stable, it's necessary to define ZSTD_STATIC_LINKING_ONLY to access them.
390 * note : never ever use experimentalParam? names directly;
391 * also, the enums values themselves are unstable and can still change.
392 */
393 ZSTD_c_experimentalParam1=500,
394 ZSTD_c_experimentalParam2=10,
395 ZSTD_c_experimentalParam3=1000,
396 ZSTD_c_experimentalParam4=1001,
397 ZSTD_c_experimentalParam5=1002,
398 ZSTD_c_experimentalParam6=1003,
399} ZSTD_cParameter;
400
401typedef struct {
402 size_t error;
403 int lowerBound;
404 int upperBound;
405} ZSTD_bounds;
406
407/*! ZSTD_cParam_getBounds() :
408 * All parameters must belong to an interval with lower and upper bounds,
409 * otherwise they will either trigger an error or be automatically clamped.
410 * @return : a structure, ZSTD_bounds, which contains
411 * - an error status field, which must be tested using ZSTD_isError()
412 * - lower and upper bounds, both inclusive
413 */
414ZSTDLIB_API ZSTD_bounds ZSTD_cParam_getBounds(ZSTD_cParameter cParam);
415
416/*! ZSTD_CCtx_setParameter() :
417 * Set one compression parameter, selected by enum ZSTD_cParameter.
418 * All parameters have valid bounds. Bounds can be queried using ZSTD_cParam_getBounds().
419 * Providing a value beyond bound will either clamp it, or trigger an error (depending on parameter).
420 * Setting a parameter is generally only possible during frame initialization (before starting compression).
421 * Exception : when using multi-threading mode (nbWorkers >= 1),
422 * the following parameters can be updated _during_ compression (within same frame):
423 * => compressionLevel, hashLog, chainLog, searchLog, minMatch, targetLength and strategy.
424 * new parameters will be active for next job only (after a flush()).
425 * @return : an error code (which can be tested using ZSTD_isError()).
426 */
427ZSTDLIB_API size_t ZSTD_CCtx_setParameter(ZSTD_CCtx* cctx, ZSTD_cParameter param, int value);
428
429/*! ZSTD_CCtx_setPledgedSrcSize() :
430 * Total input data size to be compressed as a single frame.
431 * Value will be written in frame header, unless if explicitly forbidden using ZSTD_c_contentSizeFlag.
432 * This value will also be controlled at end of frame, and trigger an error if not respected.
433 * @result : 0, or an error code (which can be tested with ZSTD_isError()).
434 * Note 1 : pledgedSrcSize==0 actually means zero, aka an empty frame.
435 * In order to mean "unknown content size", pass constant ZSTD_CONTENTSIZE_UNKNOWN.
436 * ZSTD_CONTENTSIZE_UNKNOWN is default value for any new frame.
437 * Note 2 : pledgedSrcSize is only valid once, for the next frame.
438 * It's discarded at the end of the frame, and replaced by ZSTD_CONTENTSIZE_UNKNOWN.
439 * Note 3 : Whenever all input data is provided and consumed in a single round,
440 * for example with ZSTD_compress2(),
441 * or invoking immediately ZSTD_compressStream2(,,,ZSTD_e_end),
442 * this value is automatically overridden by srcSize instead.
443 */
444ZSTDLIB_API size_t ZSTD_CCtx_setPledgedSrcSize(ZSTD_CCtx* cctx, unsigned long long pledgedSrcSize);
445
446typedef enum {
447 ZSTD_reset_session_only = 1,
448 ZSTD_reset_parameters = 2,
449 ZSTD_reset_session_and_parameters = 3
450} ZSTD_ResetDirective;
451
452/*! ZSTD_CCtx_reset() :
453 * There are 2 different things that can be reset, independently or jointly :
454 * - The session : will stop compressing current frame, and make CCtx ready to start a new one.
455 * Useful after an error, or to interrupt any ongoing compression.
456 * Any internal data not yet flushed is cancelled.
457 * Compression parameters and dictionary remain unchanged.
458 * They will be used to compress next frame.
459 * Resetting session never fails.
460 * - The parameters : changes all parameters back to "default".
461 * This removes any reference to any dictionary too.
462 * Parameters can only be changed between 2 sessions (i.e. no compression is currently ongoing)
463 * otherwise the reset fails, and function returns an error value (which can be tested using ZSTD_isError())
464 * - Both : similar to resetting the session, followed by resetting parameters.
465 */
466ZSTDLIB_API size_t ZSTD_CCtx_reset(ZSTD_CCtx* cctx, ZSTD_ResetDirective reset);
467
468/*! ZSTD_compress2() :
469 * Behave the same as ZSTD_compressCCtx(), but compression parameters are set using the advanced API.
470 * ZSTD_compress2() always starts a new frame.
471 * Should cctx hold data from a previously unfinished frame, everything about it is forgotten.
472 * - Compression parameters are pushed into CCtx before starting compression, using ZSTD_CCtx_set*()
473 * - The function is always blocking, returns when compression is completed.
474 * Hint : compression runs faster if `dstCapacity` >= `ZSTD_compressBound(srcSize)`.
475 * @return : compressed size written into `dst` (<= `dstCapacity),
476 * or an error code if it fails (which can be tested using ZSTD_isError()).
477 */
478ZSTDLIB_API size_t ZSTD_compress2( ZSTD_CCtx* cctx,
479 void* dst, size_t dstCapacity,
480 const void* src, size_t srcSize);
481
482
483/***************************************
484* Advanced decompression API
485***************************************/
486
487/* The advanced API pushes parameters one by one into an existing DCtx context.
488 * Parameters are sticky, and remain valid for all following frames
489 * using the same DCtx context.
490 * It's possible to reset parameters to default values using ZSTD_DCtx_reset().
491 * Note : This API is compatible with existing ZSTD_decompressDCtx() and ZSTD_decompressStream().
492 * Therefore, no new decompression function is necessary.
493 */
494
495typedef enum {
496
497 ZSTD_d_windowLogMax=100, /* Select a size limit (in power of 2) beyond which
498 * the streaming API will refuse to allocate memory buffer
499 * in order to protect the host from unreasonable memory requirements.
500 * This parameter is only useful in streaming mode, since no internal buffer is allocated in single-pass mode.
501 * By default, a decompression context accepts window sizes <= (1 << ZSTD_WINDOWLOG_LIMIT_DEFAULT).
502 * Special: value 0 means "use default maximum windowLog". */
503
504 /* note : additional experimental parameters are also available
505 * within the experimental section of the API.
506 * At the time of this writing, they include :
507 * ZSTD_c_format
508 * Because they are not stable, it's necessary to define ZSTD_STATIC_LINKING_ONLY to access them.
509 * note : never ever use experimentalParam? names directly
510 */
511 ZSTD_d_experimentalParam1=1000
512
513} ZSTD_dParameter;
514
515/*! ZSTD_dParam_getBounds() :
516 * All parameters must belong to an interval with lower and upper bounds,
517 * otherwise they will either trigger an error or be automatically clamped.
518 * @return : a structure, ZSTD_bounds, which contains
519 * - an error status field, which must be tested using ZSTD_isError()
520 * - both lower and upper bounds, inclusive
521 */
522ZSTDLIB_API ZSTD_bounds ZSTD_dParam_getBounds(ZSTD_dParameter dParam);
523
524/*! ZSTD_DCtx_setParameter() :
525 * Set one compression parameter, selected by enum ZSTD_dParameter.
526 * All parameters have valid bounds. Bounds can be queried using ZSTD_dParam_getBounds().
527 * Providing a value beyond bound will either clamp it, or trigger an error (depending on parameter).
528 * Setting a parameter is only possible during frame initialization (before starting decompression).
529 * @return : 0, or an error code (which can be tested using ZSTD_isError()).
530 */
531ZSTDLIB_API size_t ZSTD_DCtx_setParameter(ZSTD_DCtx* dctx, ZSTD_dParameter param, int value);
532
533/*! ZSTD_DCtx_reset() :
534 * Return a DCtx to clean state.
535 * Session and parameters can be reset jointly or separately.
536 * Parameters can only be reset when no active frame is being decompressed.
537 * @return : 0, or an error code, which can be tested with ZSTD_isError()
538 */
539ZSTDLIB_API size_t ZSTD_DCtx_reset(ZSTD_DCtx* dctx, ZSTD_ResetDirective reset);
Scott Bakereee8dd82019-09-24 12:52:34 -0700540
541
542/****************************
543* Streaming
544****************************/
545
546typedef struct ZSTD_inBuffer_s {
547 const void* src; /**< start of input buffer */
548 size_t size; /**< size of input buffer */
549 size_t pos; /**< position where reading stopped. Will be updated. Necessarily 0 <= pos <= size */
550} ZSTD_inBuffer;
551
552typedef struct ZSTD_outBuffer_s {
553 void* dst; /**< start of output buffer */
554 size_t size; /**< size of output buffer */
555 size_t pos; /**< position where writing stopped. Will be updated. Necessarily 0 <= pos <= size */
556} ZSTD_outBuffer;
557
558
559
560/*-***********************************************************************
561* Streaming compression - HowTo
562*
563* A ZSTD_CStream object is required to track streaming operation.
564* Use ZSTD_createCStream() and ZSTD_freeCStream() to create/release resources.
565* ZSTD_CStream objects can be reused multiple times on consecutive compression operations.
Scott Baker611f6bd2019-10-18 13:45:19 -0700566* It is recommended to re-use ZSTD_CStream since it will play nicer with system's memory, by re-using already allocated memory.
Scott Bakereee8dd82019-09-24 12:52:34 -0700567*
Scott Baker611f6bd2019-10-18 13:45:19 -0700568* For parallel execution, use one separate ZSTD_CStream per thread.
Scott Bakereee8dd82019-09-24 12:52:34 -0700569*
Scott Baker611f6bd2019-10-18 13:45:19 -0700570* note : since v1.3.0, ZSTD_CStream and ZSTD_CCtx are the same thing.
Scott Bakereee8dd82019-09-24 12:52:34 -0700571*
Scott Baker611f6bd2019-10-18 13:45:19 -0700572* Parameters are sticky : when starting a new compression on the same context,
573* it will re-use the same sticky parameters as previous compression session.
574* When in doubt, it's recommended to fully initialize the context before usage.
575* Use ZSTD_CCtx_reset() to reset the context and ZSTD_CCtx_setParameter(),
576* ZSTD_CCtx_setPledgedSrcSize(), or ZSTD_CCtx_loadDictionary() and friends to
577* set more specific parameters, the pledged source size, or load a dictionary.
578*
579* Use ZSTD_compressStream2() with ZSTD_e_continue as many times as necessary to
580* consume input stream. The function will automatically update both `pos`
581* fields within `input` and `output`.
582* Note that the function may not consume the entire input, for example, because
583* the output buffer is already full, in which case `input.pos < input.size`.
584* The caller must check if input has been entirely consumed.
585* If not, the caller must make some room to receive more compressed data,
586* and then present again remaining input data.
587* note: ZSTD_e_continue is guaranteed to make some forward progress when called,
588* but doesn't guarantee maximal forward progress. This is especially relevant
589* when compressing with multiple threads. The call won't block if it can
590* consume some input, but if it can't it will wait for some, but not all,
591* output to be flushed.
592* @return : provides a minimum amount of data remaining to be flushed from internal buffers
593* or an error code, which can be tested using ZSTD_isError().
594*
595* At any moment, it's possible to flush whatever data might remain stuck within internal buffer,
596* using ZSTD_compressStream2() with ZSTD_e_flush. `output->pos` will be updated.
597* Note that, if `output->size` is too small, a single invocation with ZSTD_e_flush might not be enough (return code > 0).
598* In which case, make some room to receive more compressed data, and call again ZSTD_compressStream2() with ZSTD_e_flush.
599* You must continue calling ZSTD_compressStream2() with ZSTD_e_flush until it returns 0, at which point you can change the
600* operation.
601* note: ZSTD_e_flush will flush as much output as possible, meaning when compressing with multiple threads, it will
602* block until the flush is complete or the output buffer is full.
603* @return : 0 if internal buffers are entirely flushed,
604* >0 if some data still present within internal buffer (the value is minimal estimation of remaining size),
Scott Bakereee8dd82019-09-24 12:52:34 -0700605* or an error code, which can be tested using ZSTD_isError().
606*
Scott Baker611f6bd2019-10-18 13:45:19 -0700607* Calling ZSTD_compressStream2() with ZSTD_e_end instructs to finish a frame.
Scott Bakereee8dd82019-09-24 12:52:34 -0700608* It will perform a flush and write frame epilogue.
609* The epilogue is required for decoders to consider a frame completed.
Scott Baker611f6bd2019-10-18 13:45:19 -0700610* flush operation is the same, and follows same rules as calling ZSTD_compressStream2() with ZSTD_e_flush.
611* You must continue calling ZSTD_compressStream2() with ZSTD_e_end until it returns 0, at which point you are free to
612* start a new frame.
613* note: ZSTD_e_end will flush as much output as possible, meaning when compressing with multiple threads, it will
614* block until the flush is complete or the output buffer is full.
Scott Bakereee8dd82019-09-24 12:52:34 -0700615* @return : 0 if frame fully completed and fully flushed,
Scott Baker611f6bd2019-10-18 13:45:19 -0700616* >0 if some data still present within internal buffer (the value is minimal estimation of remaining size),
Scott Bakereee8dd82019-09-24 12:52:34 -0700617* or an error code, which can be tested using ZSTD_isError().
618*
619* *******************************************************************/
620
621typedef ZSTD_CCtx ZSTD_CStream; /**< CCtx and CStream are now effectively same object (>= v1.3.0) */
Scott Baker611f6bd2019-10-18 13:45:19 -0700622 /* Continue to distinguish them for compatibility with older versions <= v1.2.0 */
Scott Bakereee8dd82019-09-24 12:52:34 -0700623/*===== ZSTD_CStream management functions =====*/
624ZSTDLIB_API ZSTD_CStream* ZSTD_createCStream(void);
625ZSTDLIB_API size_t ZSTD_freeCStream(ZSTD_CStream* zcs);
626
627/*===== Streaming compression functions =====*/
Scott Baker611f6bd2019-10-18 13:45:19 -0700628typedef enum {
629 ZSTD_e_continue=0, /* collect more data, encoder decides when to output compressed result, for optimal compression ratio */
630 ZSTD_e_flush=1, /* flush any data provided so far,
631 * it creates (at least) one new block, that can be decoded immediately on reception;
632 * frame will continue: any future data can still reference previously compressed data, improving compression.
633 * note : multithreaded compression will block to flush as much output as possible. */
634 ZSTD_e_end=2 /* flush any remaining data _and_ close current frame.
635 * note that frame is only closed after compressed data is fully flushed (return value == 0).
636 * After that point, any additional data starts a new frame.
637 * note : each frame is independent (does not reference any content from previous frame).
638 : note : multithreaded compression will block to flush as much output as possible. */
639} ZSTD_EndDirective;
Scott Bakereee8dd82019-09-24 12:52:34 -0700640
Scott Baker611f6bd2019-10-18 13:45:19 -0700641/*! ZSTD_compressStream2() :
642 * Behaves about the same as ZSTD_compressStream, with additional control on end directive.
643 * - Compression parameters are pushed into CCtx before starting compression, using ZSTD_CCtx_set*()
644 * - Compression parameters cannot be changed once compression is started (save a list of exceptions in multi-threading mode)
645 * - output->pos must be <= dstCapacity, input->pos must be <= srcSize
646 * - output->pos and input->pos will be updated. They are guaranteed to remain below their respective limit.
647 * - When nbWorkers==0 (default), function is blocking : it completes its job before returning to caller.
648 * - When nbWorkers>=1, function is non-blocking : it just acquires a copy of input, and distributes jobs to internal worker threads, flush whatever is available,
649 * and then immediately returns, just indicating that there is some data remaining to be flushed.
650 * The function nonetheless guarantees forward progress : it will return only after it reads or write at least 1+ byte.
651 * - 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.
652 * - @return provides a minimum amount of data remaining to be flushed from internal buffers
653 * or an error code, which can be tested using ZSTD_isError().
654 * if @return != 0, flush is not fully completed, there is still some data left within internal buffers.
655 * This is useful for ZSTD_e_flush, since in this case more flushes are necessary to empty all buffers.
656 * For ZSTD_e_end, @return == 0 when internal buffers are fully flushed and frame is completed.
657 * - after a ZSTD_e_end directive, if internal buffer is not fully flushed (@return != 0),
658 * only ZSTD_e_end or ZSTD_e_flush operations are allowed.
659 * Before starting a new compression job, or changing compression parameters,
660 * it is required to fully flush internal buffers.
661 */
662ZSTDLIB_API size_t ZSTD_compressStream2( ZSTD_CCtx* cctx,
663 ZSTD_outBuffer* output,
664 ZSTD_inBuffer* input,
665 ZSTD_EndDirective endOp);
666
667
668/* These buffer sizes are softly recommended.
669 * They are not required : ZSTD_compressStream*() happily accepts any buffer size, for both input and output.
670 * Respecting the recommended size just makes it a bit easier for ZSTD_compressStream*(),
671 * reducing the amount of memory shuffling and buffering, resulting in minor performance savings.
672 *
673 * However, note that these recommendations are from the perspective of a C caller program.
674 * If the streaming interface is invoked from some other language,
675 * especially managed ones such as Java or Go, through a foreign function interface such as jni or cgo,
676 * a major performance rule is to reduce crossing such interface to an absolute minimum.
677 * It's not rare that performance ends being spent more into the interface, rather than compression itself.
678 * In which cases, prefer using large buffers, as large as practical,
679 * for both input and output, to reduce the nb of roundtrips.
680 */
Scott Bakereee8dd82019-09-24 12:52:34 -0700681ZSTDLIB_API size_t ZSTD_CStreamInSize(void); /**< recommended size for input buffer */
Scott Baker611f6bd2019-10-18 13:45:19 -0700682ZSTDLIB_API size_t ZSTD_CStreamOutSize(void); /**< recommended size for output buffer. Guarantee to successfully flush at least one complete compressed block. */
Scott Bakereee8dd82019-09-24 12:52:34 -0700683
684
Scott Baker611f6bd2019-10-18 13:45:19 -0700685/* *****************************************************************************
686 * This following is a legacy streaming API.
687 * It can be replaced by ZSTD_CCtx_reset() and ZSTD_compressStream2().
688 * It is redundant, but remains fully supported.
689 * Advanced parameters and dictionary compression can only be used through the
690 * new API.
691 ******************************************************************************/
692
693/*!
694 * Equivalent to:
695 *
696 * ZSTD_CCtx_reset(zcs, ZSTD_reset_session_only);
697 * ZSTD_CCtx_refCDict(zcs, NULL); // clear the dictionary (if any)
698 * ZSTD_CCtx_setParameter(zcs, ZSTD_c_compressionLevel, compressionLevel);
699 */
700ZSTDLIB_API size_t ZSTD_initCStream(ZSTD_CStream* zcs, int compressionLevel);
701/*!
702 * Alternative for ZSTD_compressStream2(zcs, output, input, ZSTD_e_continue).
703 * NOTE: The return value is different. ZSTD_compressStream() returns a hint for
704 * the next read size (if non-zero and not an error). ZSTD_compressStream2()
705 * returns the minimum nb of bytes left to flush (if non-zero and not an error).
706 */
707ZSTDLIB_API size_t ZSTD_compressStream(ZSTD_CStream* zcs, ZSTD_outBuffer* output, ZSTD_inBuffer* input);
708/*! Equivalent to ZSTD_compressStream2(zcs, output, &emptyInput, ZSTD_e_flush). */
709ZSTDLIB_API size_t ZSTD_flushStream(ZSTD_CStream* zcs, ZSTD_outBuffer* output);
710/*! Equivalent to ZSTD_compressStream2(zcs, output, &emptyInput, ZSTD_e_end). */
711ZSTDLIB_API size_t ZSTD_endStream(ZSTD_CStream* zcs, ZSTD_outBuffer* output);
712
Scott Bakereee8dd82019-09-24 12:52:34 -0700713
714/*-***************************************************************************
715* Streaming decompression - HowTo
716*
717* A ZSTD_DStream object is required to track streaming operations.
718* Use ZSTD_createDStream() and ZSTD_freeDStream() to create/release resources.
719* ZSTD_DStream objects can be re-used multiple times.
720*
Scott Baker611f6bd2019-10-18 13:45:19 -0700721* Use ZSTD_initDStream() to start a new decompression operation.
722* @return : recommended first input size
723* Alternatively, use advanced API to set specific properties.
Scott Bakereee8dd82019-09-24 12:52:34 -0700724*
725* Use ZSTD_decompressStream() repetitively to consume your input.
726* The function will update both `pos` fields.
727* If `input.pos < input.size`, some input has not been consumed.
728* It's up to the caller to present again remaining data.
Scott Baker611f6bd2019-10-18 13:45:19 -0700729* The function tries to flush all data decoded immediately, respecting output buffer size.
Scott Bakereee8dd82019-09-24 12:52:34 -0700730* If `output.pos < output.size`, decoder has flushed everything it could.
Scott Baker611f6bd2019-10-18 13:45:19 -0700731* But if `output.pos == output.size`, there might be some data left within internal buffers.,
732* In which case, call ZSTD_decompressStream() again to flush whatever remains in the buffer.
733* Note : with no additional input provided, amount of data flushed is necessarily <= ZSTD_BLOCKSIZE_MAX.
734* @return : 0 when a frame is completely decoded and fully flushed,
735* or an error code, which can be tested using ZSTD_isError(),
736* or any other value > 0, which means there is still some decoding or flushing to do to complete current frame :
737* the return value is a suggested next input size (just a hint for better latency)
738* that will never request more than the remaining frame size.
Scott Bakereee8dd82019-09-24 12:52:34 -0700739* *******************************************************************************/
740
741typedef ZSTD_DCtx ZSTD_DStream; /**< DCtx and DStream are now effectively same object (>= v1.3.0) */
Scott Baker611f6bd2019-10-18 13:45:19 -0700742 /* For compatibility with versions <= v1.2.0, prefer differentiating them. */
Scott Bakereee8dd82019-09-24 12:52:34 -0700743/*===== ZSTD_DStream management functions =====*/
744ZSTDLIB_API ZSTD_DStream* ZSTD_createDStream(void);
745ZSTDLIB_API size_t ZSTD_freeDStream(ZSTD_DStream* zds);
746
747/*===== Streaming decompression functions =====*/
Scott Baker611f6bd2019-10-18 13:45:19 -0700748
749/* This function is redundant with the advanced API and equivalent to:
750 *
751 * ZSTD_DCtx_reset(zds);
752 * ZSTD_DCtx_refDDict(zds, NULL);
753 */
Scott Bakereee8dd82019-09-24 12:52:34 -0700754ZSTDLIB_API size_t ZSTD_initDStream(ZSTD_DStream* zds);
Scott Baker611f6bd2019-10-18 13:45:19 -0700755
Scott Bakereee8dd82019-09-24 12:52:34 -0700756ZSTDLIB_API size_t ZSTD_decompressStream(ZSTD_DStream* zds, ZSTD_outBuffer* output, ZSTD_inBuffer* input);
757
758ZSTDLIB_API size_t ZSTD_DStreamInSize(void); /*!< recommended size for input buffer */
759ZSTDLIB_API size_t ZSTD_DStreamOutSize(void); /*!< recommended size for output buffer. Guarantee to successfully flush at least one complete block in all circumstances. */
760
Scott Baker611f6bd2019-10-18 13:45:19 -0700761
762/**************************
763* Simple dictionary API
764***************************/
765/*! ZSTD_compress_usingDict() :
766 * Compression at an explicit compression level using a Dictionary.
767 * A dictionary can be any arbitrary data segment (also called a prefix),
768 * or a buffer with specified information (see dictBuilder/zdict.h).
769 * Note : This function loads the dictionary, resulting in significant startup delay.
770 * It's intended for a dictionary used only once.
771 * Note 2 : When `dict == NULL || dictSize < 8` no dictionary is used. */
772ZSTDLIB_API size_t ZSTD_compress_usingDict(ZSTD_CCtx* ctx,
773 void* dst, size_t dstCapacity,
774 const void* src, size_t srcSize,
775 const void* dict,size_t dictSize,
776 int compressionLevel);
777
778/*! ZSTD_decompress_usingDict() :
779 * Decompression using a known Dictionary.
780 * Dictionary must be identical to the one used during compression.
781 * Note : This function loads the dictionary, resulting in significant startup delay.
782 * It's intended for a dictionary used only once.
783 * Note : When `dict == NULL || dictSize < 8` no dictionary is used. */
784ZSTDLIB_API size_t ZSTD_decompress_usingDict(ZSTD_DCtx* dctx,
785 void* dst, size_t dstCapacity,
786 const void* src, size_t srcSize,
787 const void* dict,size_t dictSize);
788
789
790/***********************************
791 * Bulk processing dictionary API
792 **********************************/
793typedef struct ZSTD_CDict_s ZSTD_CDict;
794
795/*! ZSTD_createCDict() :
796 * When compressing multiple messages / blocks using the same dictionary, it's recommended to load it only once.
797 * ZSTD_createCDict() will create a digested dictionary, ready to start future compression operations without startup cost.
798 * ZSTD_CDict can be created once and shared by multiple threads concurrently, since its usage is read-only.
799 * `dictBuffer` can be released after ZSTD_CDict creation, because its content is copied within CDict.
800 * Consider experimental function `ZSTD_createCDict_byReference()` if you prefer to not duplicate `dictBuffer` content.
801 * Note : A ZSTD_CDict can be created from an empty dictBuffer, but it is inefficient when used to compress small data. */
802ZSTDLIB_API ZSTD_CDict* ZSTD_createCDict(const void* dictBuffer, size_t dictSize,
803 int compressionLevel);
804
805/*! ZSTD_freeCDict() :
806 * Function frees memory allocated by ZSTD_createCDict(). */
807ZSTDLIB_API size_t ZSTD_freeCDict(ZSTD_CDict* CDict);
808
809/*! ZSTD_compress_usingCDict() :
810 * Compression using a digested Dictionary.
811 * Recommended when same dictionary is used multiple times.
812 * Note : compression level is _decided at dictionary creation time_,
813 * and frame parameters are hardcoded (dictID=yes, contentSize=yes, checksum=no) */
814ZSTDLIB_API size_t ZSTD_compress_usingCDict(ZSTD_CCtx* cctx,
815 void* dst, size_t dstCapacity,
816 const void* src, size_t srcSize,
817 const ZSTD_CDict* cdict);
818
819
820typedef struct ZSTD_DDict_s ZSTD_DDict;
821
822/*! ZSTD_createDDict() :
823 * Create a digested dictionary, ready to start decompression operation without startup delay.
824 * dictBuffer can be released after DDict creation, as its content is copied inside DDict. */
825ZSTDLIB_API ZSTD_DDict* ZSTD_createDDict(const void* dictBuffer, size_t dictSize);
826
827/*! ZSTD_freeDDict() :
828 * Function frees memory allocated with ZSTD_createDDict() */
829ZSTDLIB_API size_t ZSTD_freeDDict(ZSTD_DDict* ddict);
830
831/*! ZSTD_decompress_usingDDict() :
832 * Decompression using a digested Dictionary.
833 * Recommended when same dictionary is used multiple times. */
834ZSTDLIB_API size_t ZSTD_decompress_usingDDict(ZSTD_DCtx* dctx,
835 void* dst, size_t dstCapacity,
836 const void* src, size_t srcSize,
837 const ZSTD_DDict* ddict);
838
839
840/********************************
841 * Dictionary helper functions
842 *******************************/
843
844/*! ZSTD_getDictID_fromDict() :
845 * Provides the dictID stored within dictionary.
846 * if @return == 0, the dictionary is not conformant with Zstandard specification.
847 * It can still be loaded, but as a content-only dictionary. */
848ZSTDLIB_API unsigned ZSTD_getDictID_fromDict(const void* dict, size_t dictSize);
849
850/*! ZSTD_getDictID_fromDDict() :
851 * Provides the dictID of the dictionary loaded into `ddict`.
852 * If @return == 0, the dictionary is not conformant to Zstandard specification, or empty.
853 * Non-conformant dictionaries can still be loaded, but as content-only dictionaries. */
854ZSTDLIB_API unsigned ZSTD_getDictID_fromDDict(const ZSTD_DDict* ddict);
855
856/*! ZSTD_getDictID_fromFrame() :
857 * Provides the dictID required to decompressed the frame stored within `src`.
858 * If @return == 0, the dictID could not be decoded.
859 * This could for one of the following reasons :
860 * - The frame does not require a dictionary to be decoded (most common case).
861 * - The frame was built with dictID intentionally removed. Whatever dictionary is necessary is a hidden information.
862 * Note : this use case also happens when using a non-conformant dictionary.
863 * - `srcSize` is too small, and as a result, the frame header could not be decoded (only possible if `srcSize < ZSTD_FRAMEHEADERSIZE_MAX`).
864 * - This is not a Zstandard frame.
865 * When identifying the exact failure cause, it's possible to use ZSTD_getFrameHeader(), which will provide a more precise error code. */
866ZSTDLIB_API unsigned ZSTD_getDictID_fromFrame(const void* src, size_t srcSize);
867
868
869/*******************************************************************************
870 * Advanced dictionary and prefix API
871 *
872 * This API allows dictionaries to be used with ZSTD_compress2(),
873 * ZSTD_compressStream2(), and ZSTD_decompress(). Dictionaries are sticky, and
874 * only reset with the context is reset with ZSTD_reset_parameters or
875 * ZSTD_reset_session_and_parameters. Prefixes are single-use.
876 ******************************************************************************/
877
878
879/*! ZSTD_CCtx_loadDictionary() :
880 * Create an internal CDict from `dict` buffer.
881 * Decompression will have to use same dictionary.
882 * @result : 0, or an error code (which can be tested with ZSTD_isError()).
883 * Special: Loading a NULL (or 0-size) dictionary invalidates previous dictionary,
884 * meaning "return to no-dictionary mode".
885 * Note 1 : Dictionary is sticky, it will be used for all future compressed frames.
886 * To return to "no-dictionary" situation, load a NULL dictionary (or reset parameters).
887 * Note 2 : Loading a dictionary involves building tables.
888 * It's also a CPU consuming operation, with non-negligible impact on latency.
889 * Tables are dependent on compression parameters, and for this reason,
890 * compression parameters can no longer be changed after loading a dictionary.
891 * Note 3 :`dict` content will be copied internally.
892 * Use experimental ZSTD_CCtx_loadDictionary_byReference() to reference content instead.
893 * In such a case, dictionary buffer must outlive its users.
894 * Note 4 : Use ZSTD_CCtx_loadDictionary_advanced()
895 * to precisely select how dictionary content must be interpreted. */
896ZSTDLIB_API size_t ZSTD_CCtx_loadDictionary(ZSTD_CCtx* cctx, const void* dict, size_t dictSize);
897
898/*! ZSTD_CCtx_refCDict() :
899 * Reference a prepared dictionary, to be used for all next compressed frames.
900 * Note that compression parameters are enforced from within CDict,
901 * and supersede any compression parameter previously set within CCtx.
902 * The parameters ignored are labled as "superseded-by-cdict" in the ZSTD_cParameter enum docs.
903 * The ignored parameters will be used again if the CCtx is returned to no-dictionary mode.
904 * The dictionary will remain valid for future compressed frames using same CCtx.
905 * @result : 0, or an error code (which can be tested with ZSTD_isError()).
906 * Special : Referencing a NULL CDict means "return to no-dictionary mode".
907 * Note 1 : Currently, only one dictionary can be managed.
908 * Referencing a new dictionary effectively "discards" any previous one.
909 * Note 2 : CDict is just referenced, its lifetime must outlive its usage within CCtx. */
910ZSTDLIB_API size_t ZSTD_CCtx_refCDict(ZSTD_CCtx* cctx, const ZSTD_CDict* cdict);
911
912/*! ZSTD_CCtx_refPrefix() :
913 * Reference a prefix (single-usage dictionary) for next compressed frame.
914 * A prefix is **only used once**. Tables are discarded at end of frame (ZSTD_e_end).
915 * Decompression will need same prefix to properly regenerate data.
916 * Compressing with a prefix is similar in outcome as performing a diff and compressing it,
917 * but performs much faster, especially during decompression (compression speed is tunable with compression level).
918 * @result : 0, or an error code (which can be tested with ZSTD_isError()).
919 * Special: Adding any prefix (including NULL) invalidates any previous prefix or dictionary
920 * Note 1 : Prefix buffer is referenced. It **must** outlive compression.
921 * Its content must remain unmodified during compression.
922 * Note 2 : If the intention is to diff some large src data blob with some prior version of itself,
923 * ensure that the window size is large enough to contain the entire source.
924 * See ZSTD_c_windowLog.
925 * Note 3 : Referencing a prefix involves building tables, which are dependent on compression parameters.
926 * It's a CPU consuming operation, with non-negligible impact on latency.
927 * If there is a need to use the same prefix multiple times, consider loadDictionary instead.
928 * Note 4 : By default, the prefix is interpreted as raw content (ZSTD_dm_rawContent).
929 * Use experimental ZSTD_CCtx_refPrefix_advanced() to alter dictionary interpretation. */
930ZSTDLIB_API size_t ZSTD_CCtx_refPrefix(ZSTD_CCtx* cctx,
931 const void* prefix, size_t prefixSize);
932
933/*! ZSTD_DCtx_loadDictionary() :
934 * Create an internal DDict from dict buffer,
935 * to be used to decompress next frames.
936 * The dictionary remains valid for all future frames, until explicitly invalidated.
937 * @result : 0, or an error code (which can be tested with ZSTD_isError()).
938 * Special : Adding a NULL (or 0-size) dictionary invalidates any previous dictionary,
939 * meaning "return to no-dictionary mode".
940 * Note 1 : Loading a dictionary involves building tables,
941 * which has a non-negligible impact on CPU usage and latency.
942 * It's recommended to "load once, use many times", to amortize the cost
943 * Note 2 :`dict` content will be copied internally, so `dict` can be released after loading.
944 * Use ZSTD_DCtx_loadDictionary_byReference() to reference dictionary content instead.
945 * Note 3 : Use ZSTD_DCtx_loadDictionary_advanced() to take control of
946 * how dictionary content is loaded and interpreted.
947 */
948ZSTDLIB_API size_t ZSTD_DCtx_loadDictionary(ZSTD_DCtx* dctx, const void* dict, size_t dictSize);
949
950/*! ZSTD_DCtx_refDDict() :
951 * Reference a prepared dictionary, to be used to decompress next frames.
952 * The dictionary remains active for decompression of future frames using same DCtx.
953 * @result : 0, or an error code (which can be tested with ZSTD_isError()).
954 * Note 1 : Currently, only one dictionary can be managed.
955 * Referencing a new dictionary effectively "discards" any previous one.
956 * Special: referencing a NULL DDict means "return to no-dictionary mode".
957 * Note 2 : DDict is just referenced, its lifetime must outlive its usage from DCtx.
958 */
959ZSTDLIB_API size_t ZSTD_DCtx_refDDict(ZSTD_DCtx* dctx, const ZSTD_DDict* ddict);
960
961/*! ZSTD_DCtx_refPrefix() :
962 * Reference a prefix (single-usage dictionary) to decompress next frame.
963 * This is the reverse operation of ZSTD_CCtx_refPrefix(),
964 * and must use the same prefix as the one used during compression.
965 * Prefix is **only used once**. Reference is discarded at end of frame.
966 * End of frame is reached when ZSTD_decompressStream() returns 0.
967 * @result : 0, or an error code (which can be tested with ZSTD_isError()).
968 * Note 1 : Adding any prefix (including NULL) invalidates any previously set prefix or dictionary
969 * Note 2 : Prefix buffer is referenced. It **must** outlive decompression.
970 * Prefix buffer must remain unmodified up to the end of frame,
971 * reached when ZSTD_decompressStream() returns 0.
972 * Note 3 : By default, the prefix is treated as raw content (ZSTD_dm_rawContent).
973 * Use ZSTD_CCtx_refPrefix_advanced() to alter dictMode (Experimental section)
974 * Note 4 : Referencing a raw content prefix has almost no cpu nor memory cost.
975 * A full dictionary is more costly, as it requires building tables.
976 */
977ZSTDLIB_API size_t ZSTD_DCtx_refPrefix(ZSTD_DCtx* dctx,
978 const void* prefix, size_t prefixSize);
979
980/* === Memory management === */
981
982/*! ZSTD_sizeof_*() :
983 * These functions give the _current_ memory usage of selected object.
984 * Note that object memory usage can evolve (increase or decrease) over time. */
985ZSTDLIB_API size_t ZSTD_sizeof_CCtx(const ZSTD_CCtx* cctx);
986ZSTDLIB_API size_t ZSTD_sizeof_DCtx(const ZSTD_DCtx* dctx);
987ZSTDLIB_API size_t ZSTD_sizeof_CStream(const ZSTD_CStream* zcs);
988ZSTDLIB_API size_t ZSTD_sizeof_DStream(const ZSTD_DStream* zds);
989ZSTDLIB_API size_t ZSTD_sizeof_CDict(const ZSTD_CDict* cdict);
990ZSTDLIB_API size_t ZSTD_sizeof_DDict(const ZSTD_DDict* ddict);
991
Scott Bakereee8dd82019-09-24 12:52:34 -0700992#endif /* ZSTD_H_235446 */
993
994
Scott Baker611f6bd2019-10-18 13:45:19 -0700995/* **************************************************************************************
996 * ADVANCED AND EXPERIMENTAL FUNCTIONS
997 ****************************************************************************************
998 * The definitions in the following section are considered experimental.
Scott Bakereee8dd82019-09-24 12:52:34 -0700999 * They are provided for advanced scenarios.
Scott Baker611f6bd2019-10-18 13:45:19 -07001000 * They should never be used with a dynamic library, as prototypes may change in the future.
Scott Bakereee8dd82019-09-24 12:52:34 -07001001 * Use them only in association with static linking.
1002 * ***************************************************************************************/
1003
1004#if defined(ZSTD_STATIC_LINKING_ONLY) && !defined(ZSTD_H_ZSTD_STATIC_LINKING_ONLY)
1005#define ZSTD_H_ZSTD_STATIC_LINKING_ONLY
1006
Scott Baker611f6bd2019-10-18 13:45:19 -07001007/****************************************************************************************
1008 * experimental API (static linking only)
1009 ****************************************************************************************
1010 * The following symbols and constants
1011 * are not planned to join "stable API" status in the near future.
1012 * They can still change in future versions.
1013 * Some of them are planned to remain in the static_only section indefinitely.
1014 * Some of them might be removed in the future (especially when redundant with existing stable functions)
1015 * ***************************************************************************************/
Scott Bakereee8dd82019-09-24 12:52:34 -07001016
Scott Baker611f6bd2019-10-18 13:45:19 -07001017#define ZSTD_FRAMEHEADERSIZE_PREFIX 5 /* minimum input size required to query frame header size */
Scott Bakereee8dd82019-09-24 12:52:34 -07001018#define ZSTD_FRAMEHEADERSIZE_MIN 6
Scott Baker611f6bd2019-10-18 13:45:19 -07001019#define ZSTD_FRAMEHEADERSIZE_MAX 18 /* can be useful for static allocation */
1020#define ZSTD_SKIPPABLEHEADERSIZE 8
1021
1022/* compression parameter bounds */
1023#define ZSTD_WINDOWLOG_MAX_32 30
1024#define ZSTD_WINDOWLOG_MAX_64 31
1025#define ZSTD_WINDOWLOG_MAX ((int)(sizeof(size_t) == 4 ? ZSTD_WINDOWLOG_MAX_32 : ZSTD_WINDOWLOG_MAX_64))
1026#define ZSTD_WINDOWLOG_MIN 10
1027#define ZSTD_HASHLOG_MAX ((ZSTD_WINDOWLOG_MAX < 30) ? ZSTD_WINDOWLOG_MAX : 30)
1028#define ZSTD_HASHLOG_MIN 6
1029#define ZSTD_CHAINLOG_MAX_32 29
1030#define ZSTD_CHAINLOG_MAX_64 30
1031#define ZSTD_CHAINLOG_MAX ((int)(sizeof(size_t) == 4 ? ZSTD_CHAINLOG_MAX_32 : ZSTD_CHAINLOG_MAX_64))
1032#define ZSTD_CHAINLOG_MIN ZSTD_HASHLOG_MIN
1033#define ZSTD_SEARCHLOG_MAX (ZSTD_WINDOWLOG_MAX-1)
1034#define ZSTD_SEARCHLOG_MIN 1
1035#define ZSTD_MINMATCH_MAX 7 /* only for ZSTD_fast, other strategies are limited to 6 */
1036#define ZSTD_MINMATCH_MIN 3 /* only for ZSTD_btopt+, faster strategies are limited to 4 */
1037#define ZSTD_TARGETLENGTH_MAX ZSTD_BLOCKSIZE_MAX
1038#define ZSTD_TARGETLENGTH_MIN 0 /* note : comparing this constant to an unsigned results in a tautological test */
1039#define ZSTD_STRATEGY_MIN ZSTD_fast
1040#define ZSTD_STRATEGY_MAX ZSTD_btultra2
Scott Bakereee8dd82019-09-24 12:52:34 -07001041
1042
Scott Baker611f6bd2019-10-18 13:45:19 -07001043#define ZSTD_OVERLAPLOG_MIN 0
1044#define ZSTD_OVERLAPLOG_MAX 9
1045
1046#define ZSTD_WINDOWLOG_LIMIT_DEFAULT 27 /* by default, the streaming decoder will refuse any frame
1047 * requiring larger than (1<<ZSTD_WINDOWLOG_LIMIT_DEFAULT) window size,
1048 * to preserve host's memory from unreasonable requirements.
1049 * This limit can be overridden using ZSTD_DCtx_setParameter(,ZSTD_d_windowLogMax,).
1050 * The limit does not apply for one-pass decoders (such as ZSTD_decompress()), since no additional memory is allocated */
1051
1052
1053/* LDM parameter bounds */
1054#define ZSTD_LDM_HASHLOG_MIN ZSTD_HASHLOG_MIN
1055#define ZSTD_LDM_HASHLOG_MAX ZSTD_HASHLOG_MAX
1056#define ZSTD_LDM_MINMATCH_MIN 4
1057#define ZSTD_LDM_MINMATCH_MAX 4096
1058#define ZSTD_LDM_BUCKETSIZELOG_MIN 1
1059#define ZSTD_LDM_BUCKETSIZELOG_MAX 8
1060#define ZSTD_LDM_HASHRATELOG_MIN 0
1061#define ZSTD_LDM_HASHRATELOG_MAX (ZSTD_WINDOWLOG_MAX - ZSTD_HASHLOG_MIN)
1062
1063/* Advanced parameter bounds */
1064#define ZSTD_TARGETCBLOCKSIZE_MIN 64
1065#define ZSTD_TARGETCBLOCKSIZE_MAX ZSTD_BLOCKSIZE_MAX
1066
1067/* internal */
1068#define ZSTD_HASHLOG3_MAX 17
1069
1070
1071/* --- Advanced types --- */
1072
1073typedef struct ZSTD_CCtx_params_s ZSTD_CCtx_params;
Scott Bakereee8dd82019-09-24 12:52:34 -07001074
1075typedef struct {
Scott Baker611f6bd2019-10-18 13:45:19 -07001076 unsigned windowLog; /**< largest match distance : larger == more compression, more memory needed during decompression */
1077 unsigned chainLog; /**< fully searched segment : larger == more compression, slower, more memory (useless for fast) */
1078 unsigned hashLog; /**< dispatch table : larger == faster, more memory */
1079 unsigned searchLog; /**< nb of searches : larger == more compression, slower */
1080 unsigned minMatch; /**< match length searched : larger == faster decompression, sometimes less compression */
1081 unsigned targetLength; /**< acceptable match size for optimal parser (only) : larger == more compression, slower */
1082 ZSTD_strategy strategy; /**< see ZSTD_strategy definition above */
Scott Bakereee8dd82019-09-24 12:52:34 -07001083} ZSTD_compressionParameters;
1084
1085typedef struct {
Scott Baker611f6bd2019-10-18 13:45:19 -07001086 int contentSizeFlag; /**< 1: content size will be in frame header (when known) */
1087 int checksumFlag; /**< 1: generate a 32-bits checksum using XXH64 algorithm at end of frame, for error detection */
1088 int noDictIDFlag; /**< 1: no dictID will be saved into frame header (dictID is only useful for dictionary compression) */
Scott Bakereee8dd82019-09-24 12:52:34 -07001089} ZSTD_frameParameters;
1090
1091typedef struct {
1092 ZSTD_compressionParameters cParams;
1093 ZSTD_frameParameters fParams;
1094} ZSTD_parameters;
1095
Scott Bakereee8dd82019-09-24 12:52:34 -07001096typedef enum {
Scott Baker611f6bd2019-10-18 13:45:19 -07001097 ZSTD_dct_auto = 0, /* dictionary is "full" when starting with ZSTD_MAGIC_DICTIONARY, otherwise it is "rawContent" */
1098 ZSTD_dct_rawContent = 1, /* ensures dictionary is always loaded as rawContent, even if it starts with ZSTD_MAGIC_DICTIONARY */
1099 ZSTD_dct_fullDict = 2 /* refuses to load a dictionary if it does not respect Zstandard's specification, starting with ZSTD_MAGIC_DICTIONARY */
Scott Bakereee8dd82019-09-24 12:52:34 -07001100} ZSTD_dictContentType_e;
1101
1102typedef enum {
Scott Baker611f6bd2019-10-18 13:45:19 -07001103 ZSTD_dlm_byCopy = 0, /**< Copy dictionary content internally */
1104 ZSTD_dlm_byRef = 1, /**< Reference dictionary content -- the dictionary buffer must outlive its users. */
Scott Bakereee8dd82019-09-24 12:52:34 -07001105} ZSTD_dictLoadMethod_e;
1106
Scott Baker611f6bd2019-10-18 13:45:19 -07001107typedef enum {
1108 /* Opened question : should we have a format ZSTD_f_auto ?
1109 * Today, it would mean exactly the same as ZSTD_f_zstd1.
1110 * But, in the future, should several formats become supported,
1111 * on the compression side, it would mean "default format".
1112 * On the decompression side, it would mean "automatic format detection",
1113 * so that ZSTD_f_zstd1 would mean "accept *only* zstd frames".
1114 * Since meaning is a little different, another option could be to define different enums for compression and decompression.
1115 * This question could be kept for later, when there are actually multiple formats to support,
1116 * but there is also the question of pinning enum values, and pinning value `0` is especially important */
1117 ZSTD_f_zstd1 = 0, /* zstd frame format, specified in zstd_compression_format.md (default) */
1118 ZSTD_f_zstd1_magicless = 1, /* Variant of zstd frame format, without initial 4-bytes magic number.
1119 * Useful to save 4 bytes per generated frame.
1120 * Decoder cannot recognise automatically this format, requiring this instruction. */
1121} ZSTD_format_e;
1122
1123typedef enum {
1124 /* Note: this enum and the behavior it controls are effectively internal
1125 * implementation details of the compressor. They are expected to continue
1126 * to evolve and should be considered only in the context of extremely
1127 * advanced performance tuning.
1128 *
1129 * Zstd currently supports the use of a CDict in two ways:
1130 *
1131 * - The contents of the CDict can be copied into the working context. This
1132 * means that the compression can search both the dictionary and input
1133 * while operating on a single set of internal tables. This makes
1134 * the compression faster per-byte of input. However, the initial copy of
1135 * the CDict's tables incurs a fixed cost at the beginning of the
1136 * compression. For small compressions (< 8 KB), that copy can dominate
1137 * the cost of the compression.
1138 *
1139 * - The CDict's tables can be used in-place. In this model, compression is
1140 * slower per input byte, because the compressor has to search two sets of
1141 * tables. However, this model incurs no start-up cost (as long as the
1142 * working context's tables can be reused). For small inputs, this can be
1143 * faster than copying the CDict's tables.
1144 *
1145 * Zstd has a simple internal heuristic that selects which strategy to use
1146 * at the beginning of a compression. However, if experimentation shows that
1147 * Zstd is making poor choices, it is possible to override that choice with
1148 * this enum.
1149 */
1150 ZSTD_dictDefaultAttach = 0, /* Use the default heuristic. */
1151 ZSTD_dictForceAttach = 1, /* Never copy the dictionary. */
1152 ZSTD_dictForceCopy = 2, /* Always copy the dictionary. */
1153} ZSTD_dictAttachPref_e;
1154
1155typedef enum {
1156 ZSTD_lcm_auto = 0, /**< Automatically determine the compression mode based on the compression level.
1157 * Negative compression levels will be uncompressed, and positive compression
1158 * levels will be compressed. */
1159 ZSTD_lcm_huffman = 1, /**< Always attempt Huffman compression. Uncompressed literals will still be
1160 * emitted if Huffman compression is not profitable. */
1161 ZSTD_lcm_uncompressed = 2, /**< Always emit uncompressed literals. */
1162} ZSTD_literalCompressionMode_e;
Scott Bakereee8dd82019-09-24 12:52:34 -07001163
1164
1165/***************************************
1166* Frame size functions
1167***************************************/
1168
Scott Bakereee8dd82019-09-24 12:52:34 -07001169/*! ZSTD_findDecompressedSize() :
Scott Baker611f6bd2019-10-18 13:45:19 -07001170 * `src` should point to the start of a series of ZSTD encoded and/or skippable frames
Scott Bakereee8dd82019-09-24 12:52:34 -07001171 * `srcSize` must be the _exact_ size of this series
Scott Baker611f6bd2019-10-18 13:45:19 -07001172 * (i.e. there should be a frame boundary at `src + srcSize`)
Scott Bakereee8dd82019-09-24 12:52:34 -07001173 * @return : - decompressed size of all data in all successive frames
1174 * - if the decompressed size cannot be determined: ZSTD_CONTENTSIZE_UNKNOWN
1175 * - if an error occurred: ZSTD_CONTENTSIZE_ERROR
1176 *
1177 * note 1 : decompressed size is an optional field, that may not be present, especially in streaming mode.
1178 * When `return==ZSTD_CONTENTSIZE_UNKNOWN`, data to decompress could be any size.
1179 * In which case, it's necessary to use streaming mode to decompress data.
1180 * note 2 : decompressed size is always present when compression is done with ZSTD_compress()
1181 * note 3 : decompressed size can be very large (64-bits value),
1182 * potentially larger than what local system can handle as a single memory segment.
1183 * In which case, it's necessary to use streaming mode to decompress data.
1184 * note 4 : If source is untrusted, decompressed size could be wrong or intentionally modified.
1185 * Always ensure result fits within application's authorized limits.
1186 * Each application can set its own limits.
1187 * note 5 : ZSTD_findDecompressedSize handles multiple frames, and so it must traverse the input to
1188 * read each contained frame header. This is fast as most of the data is skipped,
1189 * however it does mean that all frame data must be present and valid. */
1190ZSTDLIB_API unsigned long long ZSTD_findDecompressedSize(const void* src, size_t srcSize);
1191
Scott Baker611f6bd2019-10-18 13:45:19 -07001192/*! ZSTD_decompressBound() :
1193 * `src` should point to the start of a series of ZSTD encoded and/or skippable frames
1194 * `srcSize` must be the _exact_ size of this series
1195 * (i.e. there should be a frame boundary at `src + srcSize`)
1196 * @return : - upper-bound for the decompressed size of all data in all successive frames
1197 * - if an error occured: ZSTD_CONTENTSIZE_ERROR
1198 *
1199 * note 1 : an error can occur if `src` contains an invalid or incorrectly formatted frame.
1200 * note 2 : the upper-bound is exact when the decompressed size field is available in every ZSTD encoded frame of `src`.
1201 * in this case, `ZSTD_findDecompressedSize` and `ZSTD_decompressBound` return the same value.
1202 * note 3 : when the decompressed size field isn't available, the upper-bound for that frame is calculated by:
1203 * upper-bound = # blocks * min(128 KB, Window_Size)
1204 */
1205ZSTDLIB_API unsigned long long ZSTD_decompressBound(const void* src, size_t srcSize);
1206
Scott Bakereee8dd82019-09-24 12:52:34 -07001207/*! ZSTD_frameHeaderSize() :
Scott Baker611f6bd2019-10-18 13:45:19 -07001208 * srcSize must be >= ZSTD_FRAMEHEADERSIZE_PREFIX.
1209 * @return : size of the Frame Header,
1210 * or an error code (if srcSize is too small) */
Scott Bakereee8dd82019-09-24 12:52:34 -07001211ZSTDLIB_API size_t ZSTD_frameHeaderSize(const void* src, size_t srcSize);
1212
1213
1214/***************************************
1215* Memory management
1216***************************************/
1217
Scott Bakereee8dd82019-09-24 12:52:34 -07001218/*! ZSTD_estimate*() :
1219 * These functions make it possible to estimate memory usage
1220 * of a future {D,C}Ctx, before its creation.
1221 * ZSTD_estimateCCtxSize() will provide a budget large enough for any compression level up to selected one.
1222 * It will also consider src size to be arbitrarily "large", which is worst case.
1223 * If srcSize is known to always be small, ZSTD_estimateCCtxSize_usingCParams() can provide a tighter estimation.
1224 * ZSTD_estimateCCtxSize_usingCParams() can be used in tandem with ZSTD_getCParams() to create cParams from compressionLevel.
Scott Baker611f6bd2019-10-18 13:45:19 -07001225 * ZSTD_estimateCCtxSize_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.
Scott Bakereee8dd82019-09-24 12:52:34 -07001226 * Note : CCtx size estimation is only correct for single-threaded compression. */
1227ZSTDLIB_API size_t ZSTD_estimateCCtxSize(int compressionLevel);
1228ZSTDLIB_API size_t ZSTD_estimateCCtxSize_usingCParams(ZSTD_compressionParameters cParams);
1229ZSTDLIB_API size_t ZSTD_estimateCCtxSize_usingCCtxParams(const ZSTD_CCtx_params* params);
1230ZSTDLIB_API size_t ZSTD_estimateDCtxSize(void);
1231
1232/*! ZSTD_estimateCStreamSize() :
1233 * ZSTD_estimateCStreamSize() will provide a budget large enough for any compression level up to selected one.
1234 * It will also consider src size to be arbitrarily "large", which is worst case.
1235 * If srcSize is known to always be small, ZSTD_estimateCStreamSize_usingCParams() can provide a tighter estimation.
1236 * ZSTD_estimateCStreamSize_usingCParams() can be used in tandem with ZSTD_getCParams() to create cParams from compressionLevel.
Scott Baker611f6bd2019-10-18 13:45:19 -07001237 * 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.
Scott Bakereee8dd82019-09-24 12:52:34 -07001238 * Note : CStream size estimation is only correct for single-threaded compression.
1239 * ZSTD_DStream memory budget depends on window Size.
1240 * This information can be passed manually, using ZSTD_estimateDStreamSize,
1241 * or deducted from a valid frame Header, using ZSTD_estimateDStreamSize_fromFrame();
1242 * Note : if streaming is init with function ZSTD_init?Stream_usingDict(),
1243 * an internal ?Dict will be created, which additional size is not estimated here.
1244 * In this case, get total size by adding ZSTD_estimate?DictSize */
1245ZSTDLIB_API size_t ZSTD_estimateCStreamSize(int compressionLevel);
1246ZSTDLIB_API size_t ZSTD_estimateCStreamSize_usingCParams(ZSTD_compressionParameters cParams);
1247ZSTDLIB_API size_t ZSTD_estimateCStreamSize_usingCCtxParams(const ZSTD_CCtx_params* params);
1248ZSTDLIB_API size_t ZSTD_estimateDStreamSize(size_t windowSize);
1249ZSTDLIB_API size_t ZSTD_estimateDStreamSize_fromFrame(const void* src, size_t srcSize);
1250
1251/*! ZSTD_estimate?DictSize() :
1252 * ZSTD_estimateCDictSize() will bet that src size is relatively "small", and content is copied, like ZSTD_createCDict().
1253 * ZSTD_estimateCDictSize_advanced() makes it possible to control compression parameters precisely, like ZSTD_createCDict_advanced().
1254 * Note : dictionaries created by reference (`ZSTD_dlm_byRef`) are logically smaller.
1255 */
1256ZSTDLIB_API size_t ZSTD_estimateCDictSize(size_t dictSize, int compressionLevel);
1257ZSTDLIB_API size_t ZSTD_estimateCDictSize_advanced(size_t dictSize, ZSTD_compressionParameters cParams, ZSTD_dictLoadMethod_e dictLoadMethod);
1258ZSTDLIB_API size_t ZSTD_estimateDDictSize(size_t dictSize, ZSTD_dictLoadMethod_e dictLoadMethod);
1259
1260/*! ZSTD_initStatic*() :
1261 * Initialize an object using a pre-allocated fixed-size buffer.
1262 * workspace: The memory area to emplace the object into.
1263 * Provided pointer *must be 8-bytes aligned*.
1264 * Buffer must outlive object.
1265 * workspaceSize: Use ZSTD_estimate*Size() to determine
1266 * how large workspace must be to support target scenario.
1267 * @return : pointer to object (same address as workspace, just different type),
1268 * or NULL if error (size too small, incorrect alignment, etc.)
1269 * Note : zstd will never resize nor malloc() when using a static buffer.
1270 * If the object requires more memory than available,
1271 * zstd will just error out (typically ZSTD_error_memory_allocation).
1272 * Note 2 : there is no corresponding "free" function.
1273 * Since workspace is allocated externally, it must be freed externally too.
1274 * Note 3 : cParams : use ZSTD_getCParams() to convert a compression level
1275 * into its associated cParams.
1276 * Limitation 1 : currently not compatible with internal dictionary creation, triggered by
1277 * ZSTD_CCtx_loadDictionary(), ZSTD_initCStream_usingDict() or ZSTD_initDStream_usingDict().
1278 * Limitation 2 : static cctx currently not compatible with multi-threading.
1279 * Limitation 3 : static dctx is incompatible with legacy support.
1280 */
1281ZSTDLIB_API ZSTD_CCtx* ZSTD_initStaticCCtx(void* workspace, size_t workspaceSize);
1282ZSTDLIB_API ZSTD_CStream* ZSTD_initStaticCStream(void* workspace, size_t workspaceSize); /**< same as ZSTD_initStaticCCtx() */
1283
1284ZSTDLIB_API ZSTD_DCtx* ZSTD_initStaticDCtx(void* workspace, size_t workspaceSize);
1285ZSTDLIB_API ZSTD_DStream* ZSTD_initStaticDStream(void* workspace, size_t workspaceSize); /**< same as ZSTD_initStaticDCtx() */
1286
1287ZSTDLIB_API const ZSTD_CDict* ZSTD_initStaticCDict(
1288 void* workspace, size_t workspaceSize,
1289 const void* dict, size_t dictSize,
1290 ZSTD_dictLoadMethod_e dictLoadMethod,
1291 ZSTD_dictContentType_e dictContentType,
1292 ZSTD_compressionParameters cParams);
1293
1294ZSTDLIB_API const ZSTD_DDict* ZSTD_initStaticDDict(
1295 void* workspace, size_t workspaceSize,
1296 const void* dict, size_t dictSize,
1297 ZSTD_dictLoadMethod_e dictLoadMethod,
1298 ZSTD_dictContentType_e dictContentType);
1299
Scott Baker611f6bd2019-10-18 13:45:19 -07001300
Scott Bakereee8dd82019-09-24 12:52:34 -07001301/*! Custom memory allocation :
1302 * These prototypes make it possible to pass your own allocation/free functions.
1303 * ZSTD_customMem is provided at creation time, using ZSTD_create*_advanced() variants listed below.
1304 * All allocation/free operations will be completed using these custom variants instead of regular <stdlib.h> ones.
1305 */
1306typedef void* (*ZSTD_allocFunction) (void* opaque, size_t size);
1307typedef void (*ZSTD_freeFunction) (void* opaque, void* address);
1308typedef struct { ZSTD_allocFunction customAlloc; ZSTD_freeFunction customFree; void* opaque; } ZSTD_customMem;
1309static ZSTD_customMem const ZSTD_defaultCMem = { NULL, NULL, NULL }; /**< this constant defers to stdlib's functions */
1310
1311ZSTDLIB_API ZSTD_CCtx* ZSTD_createCCtx_advanced(ZSTD_customMem customMem);
1312ZSTDLIB_API ZSTD_CStream* ZSTD_createCStream_advanced(ZSTD_customMem customMem);
1313ZSTDLIB_API ZSTD_DCtx* ZSTD_createDCtx_advanced(ZSTD_customMem customMem);
1314ZSTDLIB_API ZSTD_DStream* ZSTD_createDStream_advanced(ZSTD_customMem customMem);
1315
1316ZSTDLIB_API ZSTD_CDict* ZSTD_createCDict_advanced(const void* dict, size_t dictSize,
1317 ZSTD_dictLoadMethod_e dictLoadMethod,
1318 ZSTD_dictContentType_e dictContentType,
1319 ZSTD_compressionParameters cParams,
1320 ZSTD_customMem customMem);
1321
1322ZSTDLIB_API ZSTD_DDict* ZSTD_createDDict_advanced(const void* dict, size_t dictSize,
1323 ZSTD_dictLoadMethod_e dictLoadMethod,
1324 ZSTD_dictContentType_e dictContentType,
1325 ZSTD_customMem customMem);
1326
1327
1328
1329/***************************************
1330* Advanced compression functions
1331***************************************/
1332
1333/*! ZSTD_createCDict_byReference() :
1334 * Create a digested dictionary for compression
Scott Baker611f6bd2019-10-18 13:45:19 -07001335 * Dictionary content is just referenced, not duplicated.
1336 * As a consequence, `dictBuffer` **must** outlive CDict,
1337 * and its content must remain unmodified throughout the lifetime of CDict. */
Scott Bakereee8dd82019-09-24 12:52:34 -07001338ZSTDLIB_API ZSTD_CDict* ZSTD_createCDict_byReference(const void* dictBuffer, size_t dictSize, int compressionLevel);
1339
1340/*! ZSTD_getCParams() :
Scott Baker611f6bd2019-10-18 13:45:19 -07001341 * @return ZSTD_compressionParameters structure for a selected compression level and estimated srcSize.
1342 * `estimatedSrcSize` value is optional, select 0 if not known */
Scott Bakereee8dd82019-09-24 12:52:34 -07001343ZSTDLIB_API ZSTD_compressionParameters ZSTD_getCParams(int compressionLevel, unsigned long long estimatedSrcSize, size_t dictSize);
1344
1345/*! ZSTD_getParams() :
Scott Baker611f6bd2019-10-18 13:45:19 -07001346 * same as ZSTD_getCParams(), but @return a full `ZSTD_parameters` object instead of sub-component `ZSTD_compressionParameters`.
1347 * All fields of `ZSTD_frameParameters` are set to default : contentSize=1, checksum=0, noDictID=0 */
Scott Bakereee8dd82019-09-24 12:52:34 -07001348ZSTDLIB_API ZSTD_parameters ZSTD_getParams(int compressionLevel, unsigned long long estimatedSrcSize, size_t dictSize);
1349
1350/*! ZSTD_checkCParams() :
Scott Baker611f6bd2019-10-18 13:45:19 -07001351 * Ensure param values remain within authorized range.
1352 * @return 0 on success, or an error code (can be checked with ZSTD_isError()) */
Scott Bakereee8dd82019-09-24 12:52:34 -07001353ZSTDLIB_API size_t ZSTD_checkCParams(ZSTD_compressionParameters params);
1354
1355/*! ZSTD_adjustCParams() :
1356 * optimize params for a given `srcSize` and `dictSize`.
Scott Baker611f6bd2019-10-18 13:45:19 -07001357 * `srcSize` can be unknown, in which case use ZSTD_CONTENTSIZE_UNKNOWN.
1358 * `dictSize` must be `0` when there is no dictionary.
1359 * cPar can be invalid : all parameters will be clamped within valid range in the @return struct.
1360 * This function never fails (wide contract) */
Scott Bakereee8dd82019-09-24 12:52:34 -07001361ZSTDLIB_API ZSTD_compressionParameters ZSTD_adjustCParams(ZSTD_compressionParameters cPar, unsigned long long srcSize, size_t dictSize);
1362
1363/*! ZSTD_compress_advanced() :
Scott Baker611f6bd2019-10-18 13:45:19 -07001364 * Same as ZSTD_compress_usingDict(), with fine-tune control over compression parameters (by structure) */
1365ZSTDLIB_API size_t ZSTD_compress_advanced(ZSTD_CCtx* cctx,
1366 void* dst, size_t dstCapacity,
1367 const void* src, size_t srcSize,
1368 const void* dict,size_t dictSize,
1369 ZSTD_parameters params);
Scott Bakereee8dd82019-09-24 12:52:34 -07001370
1371/*! ZSTD_compress_usingCDict_advanced() :
Scott Baker611f6bd2019-10-18 13:45:19 -07001372 * Same as ZSTD_compress_usingCDict(), with fine-tune control over frame parameters */
Scott Bakereee8dd82019-09-24 12:52:34 -07001373ZSTDLIB_API size_t ZSTD_compress_usingCDict_advanced(ZSTD_CCtx* cctx,
Scott Baker611f6bd2019-10-18 13:45:19 -07001374 void* dst, size_t dstCapacity,
1375 const void* src, size_t srcSize,
1376 const ZSTD_CDict* cdict,
1377 ZSTD_frameParameters fParams);
Scott Bakereee8dd82019-09-24 12:52:34 -07001378
1379
Scott Baker611f6bd2019-10-18 13:45:19 -07001380/*! ZSTD_CCtx_loadDictionary_byReference() :
1381 * Same as ZSTD_CCtx_loadDictionary(), but dictionary content is referenced, instead of being copied into CCtx.
1382 * It saves some memory, but also requires that `dict` outlives its usage within `cctx` */
1383ZSTDLIB_API size_t ZSTD_CCtx_loadDictionary_byReference(ZSTD_CCtx* cctx, const void* dict, size_t dictSize);
1384
1385/*! ZSTD_CCtx_loadDictionary_advanced() :
1386 * Same as ZSTD_CCtx_loadDictionary(), but gives finer control over
1387 * how to load the dictionary (by copy ? by reference ?)
1388 * and how to interpret it (automatic ? force raw mode ? full mode only ?) */
1389ZSTDLIB_API size_t ZSTD_CCtx_loadDictionary_advanced(ZSTD_CCtx* cctx, const void* dict, size_t dictSize, ZSTD_dictLoadMethod_e dictLoadMethod, ZSTD_dictContentType_e dictContentType);
1390
1391/*! ZSTD_CCtx_refPrefix_advanced() :
1392 * Same as ZSTD_CCtx_refPrefix(), but gives finer control over
1393 * how to interpret prefix content (automatic ? force raw mode (default) ? full mode only ?) */
1394ZSTDLIB_API size_t ZSTD_CCtx_refPrefix_advanced(ZSTD_CCtx* cctx, const void* prefix, size_t prefixSize, ZSTD_dictContentType_e dictContentType);
1395
1396/* === experimental parameters === */
1397/* these parameters can be used with ZSTD_setParameter()
1398 * they are not guaranteed to remain supported in the future */
1399
1400 /* Enables rsyncable mode,
1401 * which makes compressed files more rsync friendly
1402 * by adding periodic synchronization points to the compressed data.
1403 * The target average block size is ZSTD_c_jobSize / 2.
1404 * It's possible to modify the job size to increase or decrease
1405 * the granularity of the synchronization point.
1406 * Once the jobSize is smaller than the window size,
1407 * it will result in compression ratio degradation.
1408 * NOTE 1: rsyncable mode only works when multithreading is enabled.
1409 * NOTE 2: rsyncable performs poorly in combination with long range mode,
1410 * since it will decrease the effectiveness of synchronization points,
1411 * though mileage may vary.
1412 * NOTE 3: Rsyncable mode limits maximum compression speed to ~400 MB/s.
1413 * If the selected compression level is already running significantly slower,
1414 * the overall speed won't be significantly impacted.
1415 */
1416 #define ZSTD_c_rsyncable ZSTD_c_experimentalParam1
1417
1418/* Select a compression format.
1419 * The value must be of type ZSTD_format_e.
1420 * See ZSTD_format_e enum definition for details */
1421#define ZSTD_c_format ZSTD_c_experimentalParam2
1422
1423/* Force back-reference distances to remain < windowSize,
1424 * even when referencing into Dictionary content (default:0) */
1425#define ZSTD_c_forceMaxWindow ZSTD_c_experimentalParam3
1426
1427/* Controls whether the contents of a CDict
1428 * are used in place, or copied into the working context.
1429 * Accepts values from the ZSTD_dictAttachPref_e enum.
1430 * See the comments on that enum for an explanation of the feature. */
1431#define ZSTD_c_forceAttachDict ZSTD_c_experimentalParam4
1432
1433/* Controls how the literals are compressed (default is auto).
1434 * The value must be of type ZSTD_literalCompressionMode_e.
1435 * See ZSTD_literalCompressionMode_t enum definition for details.
1436 */
1437#define ZSTD_c_literalCompressionMode ZSTD_c_experimentalParam5
1438
1439/* Tries to fit compressed block size to be around targetCBlockSize.
1440 * No target when targetCBlockSize == 0.
1441 * There is no guarantee on compressed block size (default:0) */
1442#define ZSTD_c_targetCBlockSize ZSTD_c_experimentalParam6
1443
1444/*! ZSTD_CCtx_getParameter() :
1445 * Get the requested compression parameter value, selected by enum ZSTD_cParameter,
1446 * and store it into int* value.
1447 * @return : 0, or an error code (which can be tested with ZSTD_isError()).
1448 */
1449ZSTDLIB_API size_t ZSTD_CCtx_getParameter(ZSTD_CCtx* cctx, ZSTD_cParameter param, int* value);
1450
1451
1452/*! ZSTD_CCtx_params :
1453 * Quick howto :
1454 * - ZSTD_createCCtxParams() : Create a ZSTD_CCtx_params structure
1455 * - ZSTD_CCtxParams_setParameter() : Push parameters one by one into
1456 * an existing ZSTD_CCtx_params structure.
1457 * This is similar to
1458 * ZSTD_CCtx_setParameter().
1459 * - ZSTD_CCtx_setParametersUsingCCtxParams() : Apply parameters to
1460 * an existing CCtx.
1461 * These parameters will be applied to
1462 * all subsequent frames.
1463 * - ZSTD_compressStream2() : Do compression using the CCtx.
1464 * - ZSTD_freeCCtxParams() : Free the memory.
1465 *
1466 * This can be used with ZSTD_estimateCCtxSize_advanced_usingCCtxParams()
1467 * for static allocation of CCtx for single-threaded compression.
1468 */
1469ZSTDLIB_API ZSTD_CCtx_params* ZSTD_createCCtxParams(void);
1470ZSTDLIB_API size_t ZSTD_freeCCtxParams(ZSTD_CCtx_params* params);
1471
1472/*! ZSTD_CCtxParams_reset() :
1473 * Reset params to default values.
1474 */
1475ZSTDLIB_API size_t ZSTD_CCtxParams_reset(ZSTD_CCtx_params* params);
1476
1477/*! ZSTD_CCtxParams_init() :
1478 * Initializes the compression parameters of cctxParams according to
1479 * compression level. All other parameters are reset to their default values.
1480 */
1481ZSTDLIB_API size_t ZSTD_CCtxParams_init(ZSTD_CCtx_params* cctxParams, int compressionLevel);
1482
1483/*! ZSTD_CCtxParams_init_advanced() :
1484 * Initializes the compression and frame parameters of cctxParams according to
1485 * params. All other parameters are reset to their default values.
1486 */
1487ZSTDLIB_API size_t ZSTD_CCtxParams_init_advanced(ZSTD_CCtx_params* cctxParams, ZSTD_parameters params);
1488
1489/*! ZSTD_CCtxParams_setParameter() :
1490 * Similar to ZSTD_CCtx_setParameter.
1491 * Set one compression parameter, selected by enum ZSTD_cParameter.
1492 * Parameters must be applied to a ZSTD_CCtx using ZSTD_CCtx_setParametersUsingCCtxParams().
1493 * @result : 0, or an error code (which can be tested with ZSTD_isError()).
1494 */
1495ZSTDLIB_API size_t ZSTD_CCtxParams_setParameter(ZSTD_CCtx_params* params, ZSTD_cParameter param, int value);
1496
1497/*! ZSTD_CCtxParams_getParameter() :
1498 * Similar to ZSTD_CCtx_getParameter.
1499 * Get the requested value of one compression parameter, selected by enum ZSTD_cParameter.
1500 * @result : 0, or an error code (which can be tested with ZSTD_isError()).
1501 */
1502ZSTDLIB_API size_t ZSTD_CCtxParams_getParameter(ZSTD_CCtx_params* params, ZSTD_cParameter param, int* value);
1503
1504/*! ZSTD_CCtx_setParametersUsingCCtxParams() :
1505 * Apply a set of ZSTD_CCtx_params to the compression context.
1506 * This can be done even after compression is started,
1507 * if nbWorkers==0, this will have no impact until a new compression is started.
1508 * if nbWorkers>=1, new parameters will be picked up at next job,
1509 * with a few restrictions (windowLog, pledgedSrcSize, nbWorkers, jobSize, and overlapLog are not updated).
1510 */
1511ZSTDLIB_API size_t ZSTD_CCtx_setParametersUsingCCtxParams(
1512 ZSTD_CCtx* cctx, const ZSTD_CCtx_params* params);
1513
1514/*! ZSTD_compressStream2_simpleArgs() :
1515 * Same as ZSTD_compressStream2(),
1516 * but using only integral types as arguments.
1517 * This variant might be helpful for binders from dynamic languages
1518 * which have troubles handling structures containing memory pointers.
1519 */
1520ZSTDLIB_API size_t ZSTD_compressStream2_simpleArgs (
1521 ZSTD_CCtx* cctx,
1522 void* dst, size_t dstCapacity, size_t* dstPos,
1523 const void* src, size_t srcSize, size_t* srcPos,
1524 ZSTD_EndDirective endOp);
1525
1526
1527/***************************************
1528* Advanced decompression functions
1529***************************************/
Scott Bakereee8dd82019-09-24 12:52:34 -07001530
1531/*! ZSTD_isFrame() :
1532 * Tells if the content of `buffer` starts with a valid Frame Identifier.
1533 * Note : Frame Identifier is 4 bytes. If `size < 4`, @return will always be 0.
1534 * Note 2 : Legacy Frame Identifiers are considered valid only if Legacy Support is enabled.
1535 * Note 3 : Skippable Frame Identifiers are considered valid. */
1536ZSTDLIB_API unsigned ZSTD_isFrame(const void* buffer, size_t size);
1537
1538/*! ZSTD_createDDict_byReference() :
1539 * Create a digested dictionary, ready to start decompression operation without startup delay.
1540 * Dictionary content is referenced, and therefore stays in dictBuffer.
1541 * It is important that dictBuffer outlives DDict,
1542 * it must remain read accessible throughout the lifetime of DDict */
1543ZSTDLIB_API ZSTD_DDict* ZSTD_createDDict_byReference(const void* dictBuffer, size_t dictSize);
1544
Scott Baker611f6bd2019-10-18 13:45:19 -07001545/*! ZSTD_DCtx_loadDictionary_byReference() :
1546 * Same as ZSTD_DCtx_loadDictionary(),
1547 * but references `dict` content instead of copying it into `dctx`.
1548 * This saves memory if `dict` remains around.,
1549 * However, it's imperative that `dict` remains accessible (and unmodified) while being used, so it must outlive decompression. */
1550ZSTDLIB_API size_t ZSTD_DCtx_loadDictionary_byReference(ZSTD_DCtx* dctx, const void* dict, size_t dictSize);
Scott Bakereee8dd82019-09-24 12:52:34 -07001551
Scott Baker611f6bd2019-10-18 13:45:19 -07001552/*! ZSTD_DCtx_loadDictionary_advanced() :
1553 * Same as ZSTD_DCtx_loadDictionary(),
1554 * but gives direct control over
1555 * how to load the dictionary (by copy ? by reference ?)
1556 * and how to interpret it (automatic ? force raw mode ? full mode only ?). */
1557ZSTDLIB_API size_t ZSTD_DCtx_loadDictionary_advanced(ZSTD_DCtx* dctx, const void* dict, size_t dictSize, ZSTD_dictLoadMethod_e dictLoadMethod, ZSTD_dictContentType_e dictContentType);
Scott Bakereee8dd82019-09-24 12:52:34 -07001558
Scott Baker611f6bd2019-10-18 13:45:19 -07001559/*! ZSTD_DCtx_refPrefix_advanced() :
1560 * Same as ZSTD_DCtx_refPrefix(), but gives finer control over
1561 * how to interpret prefix content (automatic ? force raw mode (default) ? full mode only ?) */
1562ZSTDLIB_API size_t ZSTD_DCtx_refPrefix_advanced(ZSTD_DCtx* dctx, const void* prefix, size_t prefixSize, ZSTD_dictContentType_e dictContentType);
Scott Bakereee8dd82019-09-24 12:52:34 -07001563
Scott Baker611f6bd2019-10-18 13:45:19 -07001564/*! ZSTD_DCtx_setMaxWindowSize() :
1565 * Refuses allocating internal buffers for frames requiring a window size larger than provided limit.
1566 * This protects a decoder context from reserving too much memory for itself (potential attack scenario).
1567 * This parameter is only useful in streaming mode, since no internal buffer is allocated in single-pass mode.
1568 * By default, a decompression context accepts all window sizes <= (1 << ZSTD_WINDOWLOG_LIMIT_DEFAULT)
1569 * @return : 0, or an error code (which can be tested using ZSTD_isError()).
1570 */
1571ZSTDLIB_API size_t ZSTD_DCtx_setMaxWindowSize(ZSTD_DCtx* dctx, size_t maxWindowSize);
1572
1573/* ZSTD_d_format
1574 * experimental parameter,
1575 * allowing selection between ZSTD_format_e input compression formats
1576 */
1577#define ZSTD_d_format ZSTD_d_experimentalParam1
1578
1579/*! ZSTD_DCtx_setFormat() :
1580 * Instruct the decoder context about what kind of data to decode next.
1581 * This instruction is mandatory to decode data without a fully-formed header,
1582 * such ZSTD_f_zstd1_magicless for example.
1583 * @return : 0, or an error code (which can be tested using ZSTD_isError()). */
1584ZSTDLIB_API size_t ZSTD_DCtx_setFormat(ZSTD_DCtx* dctx, ZSTD_format_e format);
1585
1586/*! ZSTD_decompressStream_simpleArgs() :
1587 * Same as ZSTD_decompressStream(),
1588 * but using only integral types as arguments.
1589 * This can be helpful for binders from dynamic languages
1590 * which have troubles handling structures containing memory pointers.
1591 */
1592ZSTDLIB_API size_t ZSTD_decompressStream_simpleArgs (
1593 ZSTD_DCtx* dctx,
1594 void* dst, size_t dstCapacity, size_t* dstPos,
1595 const void* src, size_t srcSize, size_t* srcPos);
Scott Bakereee8dd82019-09-24 12:52:34 -07001596
1597
1598/********************************************************************
1599* Advanced streaming functions
Scott Baker611f6bd2019-10-18 13:45:19 -07001600* Warning : most of these functions are now redundant with the Advanced API.
1601* Once Advanced API reaches "stable" status,
1602* redundant functions will be deprecated, and then at some point removed.
Scott Bakereee8dd82019-09-24 12:52:34 -07001603********************************************************************/
1604
1605/*===== Advanced Streaming compression functions =====*/
Scott Baker611f6bd2019-10-18 13:45:19 -07001606/**! ZSTD_initCStream_srcSize() :
1607 * This function is deprecated, and equivalent to:
1608 * ZSTD_CCtx_reset(zcs, ZSTD_reset_session_only);
1609 * ZSTD_CCtx_refCDict(zcs, NULL); // clear the dictionary (if any)
1610 * ZSTD_CCtx_setParameter(zcs, ZSTD_c_compressionLevel, compressionLevel);
1611 * ZSTD_CCtx_setPledgedSrcSize(zcs, pledgedSrcSize);
1612 *
1613 * pledgedSrcSize must be correct. If it is not known at init time, use
1614 * ZSTD_CONTENTSIZE_UNKNOWN. Note that, for compatibility with older programs,
1615 * "0" also disables frame content size field. It may be enabled in the future.
1616 */
1617ZSTDLIB_API size_t ZSTD_initCStream_srcSize(ZSTD_CStream* zcs, int compressionLevel, unsigned long long pledgedSrcSize);
1618/**! ZSTD_initCStream_usingDict() :
1619 * This function is deprecated, and is equivalent to:
1620 * ZSTD_CCtx_reset(zcs, ZSTD_reset_session_only);
1621 * ZSTD_CCtx_setParameter(zcs, ZSTD_c_compressionLevel, compressionLevel);
1622 * ZSTD_CCtx_loadDictionary(zcs, dict, dictSize);
1623 *
1624 * Creates of an internal CDict (incompatible with static CCtx), except if
1625 * dict == NULL or dictSize < 8, in which case no dict is used.
1626 * Note: dict is loaded with ZSTD_dm_auto (treated as a full zstd dictionary if
1627 * it begins with ZSTD_MAGIC_DICTIONARY, else as raw content) and ZSTD_dlm_byCopy.
1628 */
1629ZSTDLIB_API size_t ZSTD_initCStream_usingDict(ZSTD_CStream* zcs, const void* dict, size_t dictSize, int compressionLevel);
1630/**! ZSTD_initCStream_advanced() :
1631 * This function is deprecated, and is approximately equivalent to:
1632 * ZSTD_CCtx_reset(zcs, ZSTD_reset_session_only);
1633 * ZSTD_CCtx_setZstdParams(zcs, params); // Set the zstd params and leave the rest as-is
1634 * ZSTD_CCtx_setPledgedSrcSize(zcs, pledgedSrcSize);
1635 * ZSTD_CCtx_loadDictionary(zcs, dict, dictSize);
1636 *
1637 * pledgedSrcSize must be correct. If srcSize is not known at init time, use
1638 * value ZSTD_CONTENTSIZE_UNKNOWN. dict is loaded with ZSTD_dm_auto and ZSTD_dlm_byCopy.
1639 */
Scott Bakereee8dd82019-09-24 12:52:34 -07001640ZSTDLIB_API size_t ZSTD_initCStream_advanced(ZSTD_CStream* zcs, const void* dict, size_t dictSize,
Scott Baker611f6bd2019-10-18 13:45:19 -07001641 ZSTD_parameters params, unsigned long long pledgedSrcSize);
1642/**! ZSTD_initCStream_usingCDict() :
1643 * This function is deprecated, and equivalent to:
1644 * ZSTD_CCtx_reset(zcs, ZSTD_reset_session_only);
1645 * ZSTD_CCtx_refCDict(zcs, cdict);
1646 *
1647 * note : cdict will just be referenced, and must outlive compression session
1648 */
1649ZSTDLIB_API size_t ZSTD_initCStream_usingCDict(ZSTD_CStream* zcs, const ZSTD_CDict* cdict);
1650/**! ZSTD_initCStream_usingCDict_advanced() :
1651 * This function is deprecated, and is approximately equivalent to:
1652 * ZSTD_CCtx_reset(zcs, ZSTD_reset_session_only);
1653 * ZSTD_CCtx_setZstdFrameParams(zcs, fParams); // Set the zstd frame params and leave the rest as-is
1654 * ZSTD_CCtx_setPledgedSrcSize(zcs, pledgedSrcSize);
1655 * ZSTD_CCtx_refCDict(zcs, cdict);
1656 *
1657 * same as ZSTD_initCStream_usingCDict(), with control over frame parameters.
1658 * pledgedSrcSize must be correct. If srcSize is not known at init time, use
1659 * value ZSTD_CONTENTSIZE_UNKNOWN.
1660 */
1661ZSTDLIB_API size_t ZSTD_initCStream_usingCDict_advanced(ZSTD_CStream* zcs, const ZSTD_CDict* cdict, ZSTD_frameParameters fParams, unsigned long long pledgedSrcSize);
Scott Bakereee8dd82019-09-24 12:52:34 -07001662
1663/*! ZSTD_resetCStream() :
Scott Baker611f6bd2019-10-18 13:45:19 -07001664 * This function is deprecated, and is equivalent to:
1665 * ZSTD_CCtx_reset(zcs, ZSTD_reset_session_only);
1666 * ZSTD_CCtx_setPledgedSrcSize(zcs, pledgedSrcSize);
1667 *
1668 * start a new frame, using same parameters from previous frame.
1669 * This is typically useful to skip dictionary loading stage, since it will re-use it in-place.
Scott Bakereee8dd82019-09-24 12:52:34 -07001670 * Note that zcs must be init at least once before using ZSTD_resetCStream().
1671 * If pledgedSrcSize is not known at reset time, use macro ZSTD_CONTENTSIZE_UNKNOWN.
1672 * If pledgedSrcSize > 0, its value must be correct, as it will be written in header, and controlled at the end.
1673 * For the time being, pledgedSrcSize==0 is interpreted as "srcSize unknown" for compatibility with older programs,
1674 * but it will change to mean "empty" in future version, so use macro ZSTD_CONTENTSIZE_UNKNOWN instead.
Scott Baker611f6bd2019-10-18 13:45:19 -07001675 * @return : 0, or an error code (which can be tested using ZSTD_isError())
1676 */
Scott Bakereee8dd82019-09-24 12:52:34 -07001677ZSTDLIB_API size_t ZSTD_resetCStream(ZSTD_CStream* zcs, unsigned long long pledgedSrcSize);
1678
1679
1680typedef struct {
Scott Baker611f6bd2019-10-18 13:45:19 -07001681 unsigned long long ingested; /* nb input bytes read and buffered */
1682 unsigned long long consumed; /* nb input bytes actually compressed */
1683 unsigned long long produced; /* nb of compressed bytes generated and buffered */
1684 unsigned long long flushed; /* nb of compressed bytes flushed : not provided; can be tracked from caller side */
1685 unsigned currentJobID; /* MT only : latest started job nb */
1686 unsigned nbActiveWorkers; /* MT only : nb of workers actively compressing at probe time */
Scott Bakereee8dd82019-09-24 12:52:34 -07001687} ZSTD_frameProgression;
1688
Scott Baker611f6bd2019-10-18 13:45:19 -07001689/* ZSTD_getFrameProgression() :
Scott Bakereee8dd82019-09-24 12:52:34 -07001690 * tells how much data has been ingested (read from input)
1691 * consumed (input actually compressed) and produced (output) for current frame.
Scott Baker611f6bd2019-10-18 13:45:19 -07001692 * Note : (ingested - consumed) is amount of input data buffered internally, not yet compressed.
1693 * Aggregates progression inside active worker threads.
Scott Bakereee8dd82019-09-24 12:52:34 -07001694 */
Scott Baker611f6bd2019-10-18 13:45:19 -07001695ZSTDLIB_API ZSTD_frameProgression ZSTD_getFrameProgression(const ZSTD_CCtx* cctx);
Scott Bakereee8dd82019-09-24 12:52:34 -07001696
Scott Baker611f6bd2019-10-18 13:45:19 -07001697/*! ZSTD_toFlushNow() :
1698 * Tell how many bytes are ready to be flushed immediately.
1699 * Useful for multithreading scenarios (nbWorkers >= 1).
1700 * Probe the oldest active job, defined as oldest job not yet entirely flushed,
1701 * and check its output buffer.
1702 * @return : amount of data stored in oldest job and ready to be flushed immediately.
1703 * if @return == 0, it means either :
1704 * + there is no active job (could be checked with ZSTD_frameProgression()), or
1705 * + oldest job is still actively compressing data,
1706 * but everything it has produced has also been flushed so far,
1707 * therefore flush speed is limited by production speed of oldest job
1708 * irrespective of the speed of concurrent (and newer) jobs.
1709 */
1710ZSTDLIB_API size_t ZSTD_toFlushNow(ZSTD_CCtx* cctx);
Scott Bakereee8dd82019-09-24 12:52:34 -07001711
1712
1713/*===== Advanced Streaming decompression functions =====*/
Scott Baker611f6bd2019-10-18 13:45:19 -07001714/**
1715 * This function is deprecated, and is equivalent to:
1716 *
1717 * ZSTD_DCtx_reset(zds, ZSTD_reset_session_only);
1718 * ZSTD_DCtx_loadDictionary(zds, dict, dictSize);
1719 *
1720 * note: no dictionary will be used if dict == NULL or dictSize < 8
1721 */
1722ZSTDLIB_API size_t ZSTD_initDStream_usingDict(ZSTD_DStream* zds, const void* dict, size_t dictSize);
1723/**
1724 * This function is deprecated, and is equivalent to:
1725 *
1726 * ZSTD_DCtx_reset(zds, ZSTD_reset_session_only);
1727 * ZSTD_DCtx_refDDict(zds, ddict);
1728 *
1729 * note : ddict is referenced, it must outlive decompression session
1730 */
1731ZSTDLIB_API size_t ZSTD_initDStream_usingDDict(ZSTD_DStream* zds, const ZSTD_DDict* ddict);
1732/**
1733 * This function is deprecated, and is equivalent to:
1734 *
1735 * ZSTD_DCtx_reset(zds, ZSTD_reset_session_only);
1736 *
1737 * re-use decompression parameters from previous init; saves dictionary loading
1738 */
1739ZSTDLIB_API size_t ZSTD_resetDStream(ZSTD_DStream* zds);
Scott Bakereee8dd82019-09-24 12:52:34 -07001740
1741
1742/*********************************************************************
1743* Buffer-less and synchronous inner streaming functions
1744*
1745* This is an advanced API, giving full control over buffer management, for users which need direct control over memory.
1746* But it's also a complex one, with several restrictions, documented below.
1747* Prefer normal streaming API for an easier experience.
1748********************************************************************* */
1749
1750/**
1751 Buffer-less streaming compression (synchronous mode)
1752
1753 A ZSTD_CCtx object is required to track streaming operations.
1754 Use ZSTD_createCCtx() / ZSTD_freeCCtx() to manage resource.
1755 ZSTD_CCtx object can be re-used multiple times within successive compression operations.
1756
1757 Start by initializing a context.
1758 Use ZSTD_compressBegin(), or ZSTD_compressBegin_usingDict() for dictionary compression,
1759 or ZSTD_compressBegin_advanced(), for finer parameter control.
1760 It's also possible to duplicate a reference context which has already been initialized, using ZSTD_copyCCtx()
1761
1762 Then, consume your input using ZSTD_compressContinue().
1763 There are some important considerations to keep in mind when using this advanced function :
1764 - ZSTD_compressContinue() has no internal buffer. It uses externally provided buffers only.
1765 - Interface is synchronous : input is consumed entirely and produces 1+ compressed blocks.
1766 - Caller must ensure there is enough space in `dst` to store compressed data under worst case scenario.
1767 Worst case evaluation is provided by ZSTD_compressBound().
1768 ZSTD_compressContinue() doesn't guarantee recover after a failed compression.
1769 - ZSTD_compressContinue() presumes prior input ***is still accessible and unmodified*** (up to maximum distance size, see WindowLog).
1770 It remembers all previous contiguous blocks, plus one separated memory segment (which can itself consists of multiple contiguous blocks)
1771 - ZSTD_compressContinue() detects that prior input has been overwritten when `src` buffer overlaps.
1772 In which case, it will "discard" the relevant memory section from its history.
1773
1774 Finish a frame with ZSTD_compressEnd(), which will write the last block(s) and optional checksum.
1775 It's possible to use srcSize==0, in which case, it will write a final empty block to end the frame.
1776 Without last block mark, frames are considered unfinished (hence corrupted) by compliant decoders.
1777
1778 `ZSTD_CCtx` object can be re-used (ZSTD_compressBegin()) to compress again.
1779*/
1780
1781/*===== Buffer-less streaming compression functions =====*/
1782ZSTDLIB_API size_t ZSTD_compressBegin(ZSTD_CCtx* cctx, int compressionLevel);
1783ZSTDLIB_API size_t ZSTD_compressBegin_usingDict(ZSTD_CCtx* cctx, const void* dict, size_t dictSize, int compressionLevel);
1784ZSTDLIB_API size_t ZSTD_compressBegin_advanced(ZSTD_CCtx* cctx, const void* dict, size_t dictSize, ZSTD_parameters params, unsigned long long pledgedSrcSize); /**< pledgedSrcSize : If srcSize is not known at init time, use ZSTD_CONTENTSIZE_UNKNOWN */
1785ZSTDLIB_API size_t ZSTD_compressBegin_usingCDict(ZSTD_CCtx* cctx, const ZSTD_CDict* cdict); /**< note: fails if cdict==NULL */
1786ZSTDLIB_API size_t ZSTD_compressBegin_usingCDict_advanced(ZSTD_CCtx* const cctx, const ZSTD_CDict* const cdict, ZSTD_frameParameters const fParams, unsigned long long const pledgedSrcSize); /* compression parameters are already set within cdict. pledgedSrcSize must be correct. If srcSize is not known, use macro ZSTD_CONTENTSIZE_UNKNOWN */
1787ZSTDLIB_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 */
1788
1789ZSTDLIB_API size_t ZSTD_compressContinue(ZSTD_CCtx* cctx, void* dst, size_t dstCapacity, const void* src, size_t srcSize);
1790ZSTDLIB_API size_t ZSTD_compressEnd(ZSTD_CCtx* cctx, void* dst, size_t dstCapacity, const void* src, size_t srcSize);
1791
1792
1793/*-
1794 Buffer-less streaming decompression (synchronous mode)
1795
1796 A ZSTD_DCtx object is required to track streaming operations.
1797 Use ZSTD_createDCtx() / ZSTD_freeDCtx() to manage it.
1798 A ZSTD_DCtx object can be re-used multiple times.
1799
1800 First typical operation is to retrieve frame parameters, using ZSTD_getFrameHeader().
1801 Frame header is extracted from the beginning of compressed frame, so providing only the frame's beginning is enough.
1802 Data fragment must be large enough to ensure successful decoding.
1803 `ZSTD_frameHeaderSize_max` bytes is guaranteed to always be large enough.
1804 @result : 0 : successful decoding, the `ZSTD_frameHeader` structure is correctly filled.
1805 >0 : `srcSize` is too small, please provide at least @result bytes on next attempt.
1806 errorCode, which can be tested using ZSTD_isError().
1807
1808 It fills a ZSTD_frameHeader structure with important information to correctly decode the frame,
1809 such as the dictionary ID, content size, or maximum back-reference distance (`windowSize`).
1810 Note that these values could be wrong, either because of data corruption, or because a 3rd party deliberately spoofs false information.
1811 As a consequence, check that values remain within valid application range.
1812 For example, do not allocate memory blindly, check that `windowSize` is within expectation.
1813 Each application can set its own limits, depending on local restrictions.
1814 For extended interoperability, it is recommended to support `windowSize` of at least 8 MB.
1815
1816 ZSTD_decompressContinue() needs previous data blocks during decompression, up to `windowSize` bytes.
1817 ZSTD_decompressContinue() is very sensitive to contiguity,
1818 if 2 blocks don't follow each other, make sure that either the compressor breaks contiguity at the same place,
1819 or that previous contiguous segment is large enough to properly handle maximum back-reference distance.
1820 There are multiple ways to guarantee this condition.
1821
1822 The most memory efficient way is to use a round buffer of sufficient size.
1823 Sufficient size is determined by invoking ZSTD_decodingBufferSize_min(),
1824 which can @return an error code if required value is too large for current system (in 32-bits mode).
1825 In a round buffer methodology, ZSTD_decompressContinue() decompresses each block next to previous one,
1826 up to the moment there is not enough room left in the buffer to guarantee decoding another full block,
1827 which maximum size is provided in `ZSTD_frameHeader` structure, field `blockSizeMax`.
1828 At which point, decoding can resume from the beginning of the buffer.
1829 Note that already decoded data stored in the buffer should be flushed before being overwritten.
1830
1831 There are alternatives possible, for example using two or more buffers of size `windowSize` each, though they consume more memory.
1832
1833 Finally, if you control the compression process, you can also ignore all buffer size rules,
1834 as long as the encoder and decoder progress in "lock-step",
1835 aka use exactly the same buffer sizes, break contiguity at the same place, etc.
1836
1837 Once buffers are setup, start decompression, with ZSTD_decompressBegin().
1838 If decompression requires a dictionary, use ZSTD_decompressBegin_usingDict() or ZSTD_decompressBegin_usingDDict().
1839
1840 Then use ZSTD_nextSrcSizeToDecompress() and ZSTD_decompressContinue() alternatively.
1841 ZSTD_nextSrcSizeToDecompress() tells how many bytes to provide as 'srcSize' to ZSTD_decompressContinue().
1842 ZSTD_decompressContinue() requires this _exact_ amount of bytes, or it will fail.
1843
1844 @result of ZSTD_decompressContinue() is the number of bytes regenerated within 'dst' (necessarily <= dstCapacity).
1845 It can be zero : it just means ZSTD_decompressContinue() has decoded some metadata item.
1846 It can also be an error code, which can be tested with ZSTD_isError().
1847
1848 A frame is fully decoded when ZSTD_nextSrcSizeToDecompress() returns zero.
1849 Context can then be reset to start a new decompression.
1850
1851 Note : it's possible to know if next input to present is a header or a block, using ZSTD_nextInputType().
1852 This information is not required to properly decode a frame.
1853
1854 == Special case : skippable frames ==
1855
1856 Skippable frames allow integration of user-defined data into a flow of concatenated frames.
1857 Skippable frames will be ignored (skipped) by decompressor.
1858 The format of skippable frames is as follows :
1859 a) Skippable frame ID - 4 Bytes, Little endian format, any value from 0x184D2A50 to 0x184D2A5F
1860 b) Frame Size - 4 Bytes, Little endian format, unsigned 32-bits
1861 c) Frame Content - any content (User Data) of length equal to Frame Size
1862 For skippable frames ZSTD_getFrameHeader() returns zfhPtr->frameType==ZSTD_skippableFrame.
1863 For skippable frames ZSTD_decompressContinue() always returns 0 : it only skips the content.
1864*/
1865
1866/*===== Buffer-less streaming decompression functions =====*/
1867typedef enum { ZSTD_frame, ZSTD_skippableFrame } ZSTD_frameType_e;
1868typedef struct {
1869 unsigned long long frameContentSize; /* if == ZSTD_CONTENTSIZE_UNKNOWN, it means this field is not available. 0 means "empty" */
1870 unsigned long long windowSize; /* can be very large, up to <= frameContentSize */
1871 unsigned blockSizeMax;
1872 ZSTD_frameType_e frameType; /* if == ZSTD_skippableFrame, frameContentSize is the size of skippable content */
1873 unsigned headerSize;
1874 unsigned dictID;
1875 unsigned checksumFlag;
1876} ZSTD_frameHeader;
Scott Baker611f6bd2019-10-18 13:45:19 -07001877
1878/*! ZSTD_getFrameHeader() :
1879 * decode Frame Header, or requires larger `srcSize`.
1880 * @return : 0, `zfhPtr` is correctly filled,
1881 * >0, `srcSize` is too small, value is wanted `srcSize` amount,
1882 * or an error code, which can be tested using ZSTD_isError() */
Scott Bakereee8dd82019-09-24 12:52:34 -07001883ZSTDLIB_API size_t ZSTD_getFrameHeader(ZSTD_frameHeader* zfhPtr, const void* src, size_t srcSize); /**< doesn't consume input */
Scott Baker611f6bd2019-10-18 13:45:19 -07001884/*! ZSTD_getFrameHeader_advanced() :
1885 * same as ZSTD_getFrameHeader(),
1886 * with added capability to select a format (like ZSTD_f_zstd1_magicless) */
1887ZSTDLIB_API size_t ZSTD_getFrameHeader_advanced(ZSTD_frameHeader* zfhPtr, const void* src, size_t srcSize, ZSTD_format_e format);
Scott Bakereee8dd82019-09-24 12:52:34 -07001888ZSTDLIB_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 */
1889
1890ZSTDLIB_API size_t ZSTD_decompressBegin(ZSTD_DCtx* dctx);
1891ZSTDLIB_API size_t ZSTD_decompressBegin_usingDict(ZSTD_DCtx* dctx, const void* dict, size_t dictSize);
1892ZSTDLIB_API size_t ZSTD_decompressBegin_usingDDict(ZSTD_DCtx* dctx, const ZSTD_DDict* ddict);
1893
1894ZSTDLIB_API size_t ZSTD_nextSrcSizeToDecompress(ZSTD_DCtx* dctx);
1895ZSTDLIB_API size_t ZSTD_decompressContinue(ZSTD_DCtx* dctx, void* dst, size_t dstCapacity, const void* src, size_t srcSize);
1896
1897/* misc */
1898ZSTDLIB_API void ZSTD_copyDCtx(ZSTD_DCtx* dctx, const ZSTD_DCtx* preparedDCtx);
1899typedef enum { ZSTDnit_frameHeader, ZSTDnit_blockHeader, ZSTDnit_block, ZSTDnit_lastBlock, ZSTDnit_checksum, ZSTDnit_skippableFrame } ZSTD_nextInputType_e;
1900ZSTDLIB_API ZSTD_nextInputType_e ZSTD_nextInputType(ZSTD_DCtx* dctx);
1901
1902
1903
Scott Bakereee8dd82019-09-24 12:52:34 -07001904
1905/* ============================ */
1906/** Block level API */
1907/* ============================ */
1908
1909/*!
1910 Block functions produce and decode raw zstd blocks, without frame metadata.
1911 Frame metadata cost is typically ~18 bytes, which can be non-negligible for very small blocks (< 100 bytes).
1912 User will have to take in charge required information to regenerate data, such as compressed and content sizes.
1913
1914 A few rules to respect :
1915 - Compressing and decompressing require a context structure
1916 + Use ZSTD_createCCtx() and ZSTD_createDCtx()
1917 - It is necessary to init context before starting
1918 + compression : any ZSTD_compressBegin*() variant, including with dictionary
1919 + decompression : any ZSTD_decompressBegin*() variant, including with dictionary
1920 + copyCCtx() and copyDCtx() can be used too
1921 - Block size is limited, it must be <= ZSTD_getBlockSize() <= ZSTD_BLOCKSIZE_MAX == 128 KB
1922 + If input is larger than a block size, it's necessary to split input data into multiple blocks
Scott Baker611f6bd2019-10-18 13:45:19 -07001923 + For inputs larger than a single block, really consider using regular ZSTD_compress() instead.
Scott Bakereee8dd82019-09-24 12:52:34 -07001924 Frame metadata is not that costly, and quickly becomes negligible as source size grows larger.
1925 - When a block is considered not compressible enough, ZSTD_compressBlock() result will be zero.
Scott Baker611f6bd2019-10-18 13:45:19 -07001926 In which case, nothing is produced into `dst` !
Scott Bakereee8dd82019-09-24 12:52:34 -07001927 + User must test for such outcome and deal directly with uncompressed data
1928 + ZSTD_decompressBlock() doesn't accept uncompressed data as input !!!
1929 + In case of multiple successive blocks, should some of them be uncompressed,
1930 decoder must be informed of their existence in order to follow proper history.
1931 Use ZSTD_insertBlock() for such a case.
1932*/
1933
Scott Bakereee8dd82019-09-24 12:52:34 -07001934/*===== Raw zstd block functions =====*/
1935ZSTDLIB_API size_t ZSTD_getBlockSize (const ZSTD_CCtx* cctx);
1936ZSTDLIB_API size_t ZSTD_compressBlock (ZSTD_CCtx* cctx, void* dst, size_t dstCapacity, const void* src, size_t srcSize);
1937ZSTDLIB_API size_t ZSTD_decompressBlock(ZSTD_DCtx* dctx, void* dst, size_t dstCapacity, const void* src, size_t srcSize);
1938ZSTDLIB_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. */
1939
1940
1941#endif /* ZSTD_H_ZSTD_STATIC_LINKING_ONLY */
1942
1943#if defined (__cplusplus)
1944}
1945#endif