1/*
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
38/*******************************************************************************
39 Introduction
40
41 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
52 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)
56
57 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*******************************************************************************/
70
71/*------ Version ------*/
72#define ZSTD_VERSION_MAJOR 1
73#define ZSTD_VERSION_MINOR 3
74#define ZSTD_VERSION_RELEASE 7
75
76#define ZSTD_VERSION_NUMBER (ZSTD_VERSION_MAJOR *100*100 + ZSTD_VERSION_MINOR *100 + ZSTD_VERSION_RELEASE)
77ZSTDLIB_API unsigned ZSTD_versionNumber(void); /**< useful to check dll version */
78
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)
83ZSTDLIB_API const char* ZSTD_versionString(void); /* 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* Simple API
94***************************************/
95/*! ZSTD_compress() :
96 * Compresses `src` content as a single zstd compressed frame into already allocated `dst`.
97 * Hint : compression runs faster if `dstCapacity` >= `ZSTD_compressBound(srcSize)`.
98 * @return : compressed size written into `dst` (<= `dstCapacity),
99 * or an error code if it fails (which can be tested using ZSTD_isError()). */
100ZSTDLIB_API size_t ZSTD_compress( void* dst, size_t dstCapacity,
101 const void* src, size_t srcSize,
102 int compressionLevel);
103
104/*! ZSTD_decompress() :
105 * `compressedSize` : must be the _exact_ size of some number of compressed and/or skippable frames.
106 * `dstCapacity` is an upper bound of originalSize to regenerate.
107 * If user cannot imply a maximum upper bound, it's better to use streaming mode to decompress data.
108 * @return : the number of bytes decompressed into `dst` (<= `dstCapacity`),
109 * or an errorCode if it fails (which can be tested using ZSTD_isError()). */
110ZSTDLIB_API size_t ZSTD_decompress( void* dst, size_t dstCapacity,
111 const void* src, size_t compressedSize);
112
113/*! ZSTD_getFrameContentSize() : added in v1.3.0
114 * `src` should point to the start of a ZSTD encoded frame.
115 * `srcSize` must be at least as large as the frame header.
116 * hint : any size >= `ZSTD_frameHeaderSize_max` is large enough.
117 * @return : - decompressed size of `src` frame content, if known
118 * - ZSTD_CONTENTSIZE_UNKNOWN if the size cannot be determined
119 * - ZSTD_CONTENTSIZE_ERROR if an error occurred (e.g. invalid magic number, srcSize too small)
120 * note 1 : a 0 return value means the frame is valid but "empty".
121 * note 2 : decompressed size is an optional field, it may not be present, typically in streaming mode.
122 * When `return==ZSTD_CONTENTSIZE_UNKNOWN`, data to decompress could be any size.
123 * In which case, it's necessary to use streaming mode to decompress data.
124 * Optionally, application can rely on some implicit limit,
125 * as ZSTD_decompress() only needs an upper bound of decompressed size.
126 * (For example, data could be necessarily cut into blocks <= 16 KB).
127 * note 3 : decompressed size is always present when compression is completed using single-pass functions,
128 * such as ZSTD_compress(), ZSTD_compressCCtx() ZSTD_compress_usingDict() or ZSTD_compress_usingCDict().
129 * note 4 : decompressed size can be very large (64-bits value),
130 * potentially larger than what local system can handle as a single memory segment.
131 * In which case, it's necessary to use streaming mode to decompress data.
132 * note 5 : If source is untrusted, decompressed size could be wrong or intentionally modified.
133 * Always ensure return value fits within application's authorized limits.
134 * Each application can set its own limits.
135 * note 6 : This function replaces ZSTD_getDecompressedSize() */
136#define ZSTD_CONTENTSIZE_UNKNOWN (0ULL - 1)
137#define ZSTD_CONTENTSIZE_ERROR (0ULL - 2)
138ZSTDLIB_API unsigned long long ZSTD_getFrameContentSize(const void *src, size_t srcSize);
139
140/*! ZSTD_getDecompressedSize() :
141 * NOTE: This function is now obsolete, in favor of ZSTD_getFrameContentSize().
142 * Both functions work the same way, but ZSTD_getDecompressedSize() blends
143 * "empty", "unknown" and "error" results to the same return value (0),
144 * while ZSTD_getFrameContentSize() gives them separate return values.
145 * @return : decompressed size of `src` frame content _if known and not empty_, 0 otherwise. */
146ZSTDLIB_API unsigned long long ZSTD_getDecompressedSize(const void* src, size_t srcSize);
147
148
149/*====== Helper functions ======*/
150#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 */
151ZSTDLIB_API size_t ZSTD_compressBound(size_t srcSize); /*!< maximum compressed size in worst case single-pass scenario */
152ZSTDLIB_API unsigned ZSTD_isError(size_t code); /*!< tells if a `size_t` function result is an error code */
153ZSTDLIB_API const char* ZSTD_getErrorName(size_t code); /*!< provides readable string from an error code */
154ZSTDLIB_API int ZSTD_maxCLevel(void); /*!< maximum compression level available */
155
156
157/***************************************
158* Explicit context
159***************************************/
160/*= Compression context
161 * When compressing many times,
162 * it is recommended to allocate a context just once, and re-use it for each successive compression operation.
163 * This will make workload friendlier for system's memory.
164 * Use one context per thread for parallel execution in multi-threaded environments. */
165typedef struct ZSTD_CCtx_s ZSTD_CCtx;
166ZSTDLIB_API ZSTD_CCtx* ZSTD_createCCtx(void);
167ZSTDLIB_API size_t ZSTD_freeCCtx(ZSTD_CCtx* cctx);
168
169/*! ZSTD_compressCCtx() :
170 * Same as ZSTD_compress(), requires an allocated ZSTD_CCtx (see ZSTD_createCCtx()). */
171ZSTDLIB_API size_t ZSTD_compressCCtx(ZSTD_CCtx* ctx,
172 void* dst, size_t dstCapacity,
173 const void* src, size_t srcSize,
174 int compressionLevel);
175
176/*= Decompression context
177 * When decompressing many times,
178 * it is recommended to allocate a context only once,
179 * and re-use it for each successive compression operation.
180 * This will make workload friendlier for system's memory.
181 * Use one context per thread for parallel execution. */
182typedef struct ZSTD_DCtx_s ZSTD_DCtx;
183ZSTDLIB_API ZSTD_DCtx* ZSTD_createDCtx(void);
184ZSTDLIB_API size_t ZSTD_freeDCtx(ZSTD_DCtx* dctx);
185
186/*! ZSTD_decompressDCtx() :
187 * Same as ZSTD_decompress(), requires an allocated ZSTD_DCtx (see ZSTD_createDCtx()) */
188ZSTDLIB_API size_t ZSTD_decompressDCtx(ZSTD_DCtx* ctx,
189 void* dst, size_t dstCapacity,
190 const void* src, size_t srcSize);
191
192
193/**************************
194* Simple dictionary API
195***************************/
196/*! ZSTD_compress_usingDict() :
197 * Compression using a predefined Dictionary (see dictBuilder/zdict.h).
198 * Note : This function loads the dictionary, resulting in significant startup delay.
199 * Note : When `dict == NULL || dictSize < 8` no dictionary is used. */
200ZSTDLIB_API size_t ZSTD_compress_usingDict(ZSTD_CCtx* ctx,
201 void* dst, size_t dstCapacity,
202 const void* src, size_t srcSize,
203 const void* dict,size_t dictSize,
204 int compressionLevel);
205
206/*! ZSTD_decompress_usingDict() :
207 * Decompression using a predefined Dictionary (see dictBuilder/zdict.h).
208 * Dictionary must be identical to the one used during compression.
209 * Note : This function loads the dictionary, resulting in significant startup delay.
210 * Note : When `dict == NULL || dictSize < 8` no dictionary is used. */
211ZSTDLIB_API size_t ZSTD_decompress_usingDict(ZSTD_DCtx* dctx,
212 void* dst, size_t dstCapacity,
213 const void* src, size_t srcSize,
214 const void* dict,size_t dictSize);
215
216
217/**********************************
218 * Bulk processing dictionary API
219 *********************************/
220typedef struct ZSTD_CDict_s ZSTD_CDict;
221
222/*! ZSTD_createCDict() :
223 * When compressing multiple messages / blocks with the same dictionary, it's recommended to load it just once.
224 * ZSTD_createCDict() will create a digested dictionary, ready to start future compression operations without startup delay.
225 * ZSTD_CDict can be created once and shared by multiple threads concurrently, since its usage is read-only.
226 * `dictBuffer` can be released after ZSTD_CDict creation, since its content is copied within CDict
227 * Note : A ZSTD_CDict can be created with an empty dictionary, but it is inefficient for small data. */
228ZSTDLIB_API ZSTD_CDict* ZSTD_createCDict(const void* dictBuffer, size_t dictSize,
229 int compressionLevel);
230
231/*! ZSTD_freeCDict() :
232 * Function frees memory allocated by ZSTD_createCDict(). */
233ZSTDLIB_API size_t ZSTD_freeCDict(ZSTD_CDict* CDict);
234
235/*! ZSTD_compress_usingCDict() :
236 * Compression using a digested Dictionary.
237 * Faster startup than ZSTD_compress_usingDict(), recommended when same dictionary is used multiple times.
238 * Note that compression level is decided during dictionary creation.
239 * Frame parameters are hardcoded (dictID=yes, contentSize=yes, checksum=no)
240 * Note : ZSTD_compress_usingCDict() can be used with a ZSTD_CDict created from an empty dictionary.
241 * But it is inefficient for small data, and it is recommended to use ZSTD_compressCCtx(). */
242ZSTDLIB_API size_t ZSTD_compress_usingCDict(ZSTD_CCtx* cctx,
243 void* dst, size_t dstCapacity,
244 const void* src, size_t srcSize,
245 const ZSTD_CDict* cdict);
246
247
248typedef struct ZSTD_DDict_s ZSTD_DDict;
249
250/*! ZSTD_createDDict() :
251 * Create a digested dictionary, ready to start decompression operation without startup delay.
252 * dictBuffer can be released after DDict creation, as its content is copied inside DDict */
253ZSTDLIB_API ZSTD_DDict* ZSTD_createDDict(const void* dictBuffer, size_t dictSize);
254
255/*! ZSTD_freeDDict() :
256 * Function frees memory allocated with ZSTD_createDDict() */
257ZSTDLIB_API size_t ZSTD_freeDDict(ZSTD_DDict* ddict);
258
259/*! ZSTD_decompress_usingDDict() :
260 * Decompression using a digested Dictionary.
261 * Faster startup than ZSTD_decompress_usingDict(), recommended when same dictionary is used multiple times. */
262ZSTDLIB_API size_t ZSTD_decompress_usingDDict(ZSTD_DCtx* dctx,
263 void* dst, size_t dstCapacity,
264 const void* src, size_t srcSize,
265 const ZSTD_DDict* ddict);
266
267
268/****************************
269* Streaming
270****************************/
271
272typedef struct ZSTD_inBuffer_s {
273 const void* src; /**< start of input buffer */
274 size_t size; /**< size of input buffer */
275 size_t pos; /**< position where reading stopped. Will be updated. Necessarily 0 <= pos <= size */
276} ZSTD_inBuffer;
277
278typedef struct ZSTD_outBuffer_s {
279 void* dst; /**< start of output buffer */
280 size_t size; /**< size of output buffer */
281 size_t pos; /**< position where writing stopped. Will be updated. Necessarily 0 <= pos <= size */
282} ZSTD_outBuffer;
283
284
285
286/*-***********************************************************************
287* Streaming compression - HowTo
288*
289* A ZSTD_CStream object is required to track streaming operation.
290* Use ZSTD_createCStream() and ZSTD_freeCStream() to create/release resources.
291* ZSTD_CStream objects can be reused multiple times on consecutive compression operations.
292* It is recommended to re-use ZSTD_CStream in situations where many streaming operations will be achieved consecutively,
293* since it will play nicer with system's memory, by re-using already allocated memory.
294* Use one separate ZSTD_CStream per thread for parallel execution.
295*
296* Start a new compression by initializing ZSTD_CStream context.
297* Use ZSTD_initCStream() to start a new compression operation.
298* Use variants ZSTD_initCStream_usingDict() or ZSTD_initCStream_usingCDict() for streaming with dictionary (experimental section)
299*
300* Use ZSTD_compressStream() as many times as necessary to consume input stream.
301* The function will automatically update both `pos` fields within `input` and `output`.
302* Note that the function may not consume the entire input,
303* for example, because the output buffer is already full,
304* in which case `input.pos < input.size`.
305* The caller must check if input has been entirely consumed.
306* If not, the caller must make some room to receive more compressed data,
307* typically by emptying output buffer, or allocating a new output buffer,
308* and then present again remaining input data.
309* @return : a size hint, preferred nb of bytes to use as input for next function call
310* or an error code, which can be tested using ZSTD_isError().
311* Note 1 : it's just a hint, to help latency a little, any other value will work fine.
312* Note 2 : size hint is guaranteed to be <= ZSTD_CStreamInSize()
313*
314* At any moment, it's possible to flush whatever data might remain stuck within internal buffer,
315* using ZSTD_flushStream(). `output->pos` will be updated.
316* Note that, if `output->size` is too small, a single invocation of ZSTD_flushStream() might not be enough (return code > 0).
317* In which case, make some room to receive more compressed data, and call again ZSTD_flushStream().
318* @return : 0 if internal buffers are entirely flushed,
319* >0 if some data still present within internal buffer (the value is minimal estimation of remaining size),
320* or an error code, which can be tested using ZSTD_isError().
321*
322* ZSTD_endStream() instructs to finish a frame.
323* It will perform a flush and write frame epilogue.
324* The epilogue is required for decoders to consider a frame completed.
325* flush() operation is the same, and follows same rules as ZSTD_flushStream().
326* @return : 0 if frame fully completed and fully flushed,
327* >0 if some data still present within internal buffer (the value is minimal estimation of remaining size),
328* or an error code, which can be tested using ZSTD_isError().
329*
330* *******************************************************************/
331
332typedef ZSTD_CCtx ZSTD_CStream; /**< CCtx and CStream are now effectively same object (>= v1.3.0) */
333 /* Continue to distinguish them for compatibility with older versions <= v1.2.0 */
334/*===== ZSTD_CStream management functions =====*/
335ZSTDLIB_API ZSTD_CStream* ZSTD_createCStream(void);
336ZSTDLIB_API size_t ZSTD_freeCStream(ZSTD_CStream* zcs);
337
338/*===== Streaming compression functions =====*/
339ZSTDLIB_API size_t ZSTD_initCStream(ZSTD_CStream* zcs, int compressionLevel);
340ZSTDLIB_API size_t ZSTD_compressStream(ZSTD_CStream* zcs, ZSTD_outBuffer* output, ZSTD_inBuffer* input);
341ZSTDLIB_API size_t ZSTD_flushStream(ZSTD_CStream* zcs, ZSTD_outBuffer* output);
342ZSTDLIB_API size_t ZSTD_endStream(ZSTD_CStream* zcs, ZSTD_outBuffer* output);
343
344ZSTDLIB_API size_t ZSTD_CStreamInSize(void); /**< recommended size for input buffer */
345ZSTDLIB_API size_t ZSTD_CStreamOutSize(void); /**< recommended size for output buffer. Guarantee to successfully flush at least one complete compressed block in all circumstances. */
346
347
348
349/*-***************************************************************************
350* Streaming decompression - HowTo
351*
352* A ZSTD_DStream object is required to track streaming operations.
353* Use ZSTD_createDStream() and ZSTD_freeDStream() to create/release resources.
354* ZSTD_DStream objects can be re-used multiple times.
355*
356* Use ZSTD_initDStream() to start a new decompression operation,
357* or ZSTD_initDStream_usingDict() if decompression requires a dictionary.
358* @return : recommended first input size
359*
360* Use ZSTD_decompressStream() repetitively to consume your input.
361* The function will update both `pos` fields.
362* If `input.pos < input.size`, some input has not been consumed.
363* It's up to the caller to present again remaining data.
364* The function tries to flush all data decoded immediately, repecting buffer sizes.
365* If `output.pos < output.size`, decoder has flushed everything it could.
366* But if `output.pos == output.size`, there is no such guarantee,
367* it's likely that some decoded data was not flushed and still remains within internal buffers.
368* In which case, call ZSTD_decompressStream() again to flush whatever remains in the buffer.
369* When no additional input is provided, amount of data flushed is necessarily <= ZSTD_BLOCKSIZE_MAX.
370* @return : 0 when a frame is completely decoded and fully flushed,
371* or an error code, which can be tested using ZSTD_isError(),
372* or any other value > 0, which means there is still some decoding or flushing to do to complete current frame :
373* the return value is a suggested next input size (a hint for better latency)
374* that will never load more than the current frame.
375* *******************************************************************************/
376
377typedef ZSTD_DCtx ZSTD_DStream; /**< DCtx and DStream are now effectively same object (>= v1.3.0) */
378 /* For compatibility with versions <= v1.2.0, prefer differentiating them. */
379/*===== ZSTD_DStream management functions =====*/
380ZSTDLIB_API ZSTD_DStream* ZSTD_createDStream(void);
381ZSTDLIB_API size_t ZSTD_freeDStream(ZSTD_DStream* zds);
382
383/*===== Streaming decompression functions =====*/
384ZSTDLIB_API size_t ZSTD_initDStream(ZSTD_DStream* zds);
385ZSTDLIB_API size_t ZSTD_decompressStream(ZSTD_DStream* zds, ZSTD_outBuffer* output, ZSTD_inBuffer* input);
386
387ZSTDLIB_API size_t ZSTD_DStreamInSize(void); /*!< recommended size for input buffer */
388ZSTDLIB_API size_t ZSTD_DStreamOutSize(void); /*!< recommended size for output buffer. Guarantee to successfully flush at least one complete block in all circumstances. */
389
390#endif /* ZSTD_H_235446 */
391
392
393
394
395#if defined(ZSTD_STATIC_LINKING_ONLY) && !defined(ZSTD_H_ZSTD_STATIC_LINKING_ONLY)
396#define ZSTD_H_ZSTD_STATIC_LINKING_ONLY
397
398/****************************************************************************************
399 * ADVANCED AND EXPERIMENTAL FUNCTIONS
400 ****************************************************************************************
401 * The definitions in this section are considered experimental.
402 * They should never be used with a dynamic library, as prototypes may change in the future.
403 * They are provided for advanced scenarios.
404 * Use them only in association with static linking.
405 * ***************************************************************************************/
406
407ZSTDLIB_API int ZSTD_minCLevel(void); /*!< minimum negative compression level allowed */
408
409/* --- Constants ---*/
410#define ZSTD_MAGICNUMBER 0xFD2FB528 /* v0.8+ */
411#define ZSTD_MAGIC_DICTIONARY 0xEC30A437 /* v0.7+ */
412#define ZSTD_MAGIC_SKIPPABLE_START 0x184D2A50U
413
414#define ZSTD_BLOCKSIZELOG_MAX 17
415#define ZSTD_BLOCKSIZE_MAX (1<<ZSTD_BLOCKSIZELOG_MAX) /* define, for static allocation */
416
417#define ZSTD_WINDOWLOG_MAX_32 30
418#define ZSTD_WINDOWLOG_MAX_64 31
419#define ZSTD_WINDOWLOG_MAX ((unsigned)(sizeof(size_t) == 4 ? ZSTD_WINDOWLOG_MAX_32 : ZSTD_WINDOWLOG_MAX_64))
420#define ZSTD_WINDOWLOG_MIN 10
421#define ZSTD_HASHLOG_MAX ((ZSTD_WINDOWLOG_MAX < 30) ? ZSTD_WINDOWLOG_MAX : 30)
422#define ZSTD_HASHLOG_MIN 6
423#define ZSTD_CHAINLOG_MAX_32 29
424#define ZSTD_CHAINLOG_MAX_64 30
425#define ZSTD_CHAINLOG_MAX ((unsigned)(sizeof(size_t) == 4 ? ZSTD_CHAINLOG_MAX_32 : ZSTD_CHAINLOG_MAX_64))
426#define ZSTD_CHAINLOG_MIN ZSTD_HASHLOG_MIN
427#define ZSTD_HASHLOG3_MAX 17
428#define ZSTD_SEARCHLOG_MAX (ZSTD_WINDOWLOG_MAX-1)
429#define ZSTD_SEARCHLOG_MIN 1
430#define ZSTD_SEARCHLENGTH_MAX 7 /* only for ZSTD_fast, other strategies are limited to 6 */
431#define ZSTD_SEARCHLENGTH_MIN 3 /* only for ZSTD_btopt, other strategies are limited to 4 */
432#define ZSTD_TARGETLENGTH_MAX ZSTD_BLOCKSIZE_MAX
433#define ZSTD_TARGETLENGTH_MIN 0 /* note : comparing this constant to an unsigned results in a tautological test */
434#define ZSTD_LDM_MINMATCH_MAX 4096
435#define ZSTD_LDM_MINMATCH_MIN 4
436#define ZSTD_LDM_BUCKETSIZELOG_MAX 8
437
438#define ZSTD_FRAMEHEADERSIZE_PREFIX 5 /* minimum input size to know frame header size */
439#define ZSTD_FRAMEHEADERSIZE_MIN 6
440#define ZSTD_FRAMEHEADERSIZE_MAX 18 /* for static allocation */
441static const size_t ZSTD_frameHeaderSize_prefix = ZSTD_FRAMEHEADERSIZE_PREFIX;
442static const size_t ZSTD_frameHeaderSize_min = ZSTD_FRAMEHEADERSIZE_MIN;
443static const size_t ZSTD_frameHeaderSize_max = ZSTD_FRAMEHEADERSIZE_MAX;
444static const size_t ZSTD_skippableHeaderSize = 8; /* magic number + skippable frame length */
445
446
447
448/* --- Advanced types --- */
449typedef enum { ZSTD_fast=1, ZSTD_dfast, ZSTD_greedy, ZSTD_lazy, ZSTD_lazy2,
450 ZSTD_btlazy2, ZSTD_btopt, ZSTD_btultra } ZSTD_strategy; /* from faster to stronger */
451
452typedef struct {
453 unsigned windowLog; /**< largest match distance : larger == more compression, more memory needed during decompression */
454 unsigned chainLog; /**< fully searched segment : larger == more compression, slower, more memory (useless for fast) */
455 unsigned hashLog; /**< dispatch table : larger == faster, more memory */
456 unsigned searchLog; /**< nb of searches : larger == more compression, slower */
457 unsigned searchLength; /**< match length searched : larger == faster decompression, sometimes less compression */
458 unsigned targetLength; /**< acceptable match size for optimal parser (only) : larger == more compression, slower */
459 ZSTD_strategy strategy;
460} ZSTD_compressionParameters;
461
462typedef struct {
463 unsigned contentSizeFlag; /**< 1: content size will be in frame header (when known) */
464 unsigned checksumFlag; /**< 1: generate a 32-bits checksum at end of frame, for error detection */
465 unsigned noDictIDFlag; /**< 1: no dictID will be saved into frame header (if dictionary compression) */
466} ZSTD_frameParameters;
467
468typedef struct {
469 ZSTD_compressionParameters cParams;
470 ZSTD_frameParameters fParams;
471} ZSTD_parameters;
472
473typedef struct ZSTD_CCtx_params_s ZSTD_CCtx_params;
474
475typedef enum {
476 ZSTD_dct_auto=0, /* dictionary is "full" when starting with ZSTD_MAGIC_DICTIONARY, otherwise it is "rawContent" */
477 ZSTD_dct_rawContent, /* ensures dictionary is always loaded as rawContent, even if it starts with ZSTD_MAGIC_DICTIONARY */
478 ZSTD_dct_fullDict /* refuses to load a dictionary if it does not respect Zstandard's specification */
479} ZSTD_dictContentType_e;
480
481typedef enum {
482 ZSTD_dlm_byCopy = 0, /**< Copy dictionary content internally */
483 ZSTD_dlm_byRef, /**< Reference dictionary content -- the dictionary buffer must outlive its users. */
484} ZSTD_dictLoadMethod_e;
485
486
487
488/***************************************
489* Frame size functions
490***************************************/
491
492/*! ZSTD_findFrameCompressedSize() :
493 * `src` should point to the start of a ZSTD encoded frame or skippable frame
494 * `srcSize` must be >= first frame size
495 * @return : the compressed size of the first frame starting at `src`,
496 * suitable to pass to `ZSTD_decompress` or similar,
497 * or an error code if input is invalid */
498ZSTDLIB_API size_t ZSTD_findFrameCompressedSize(const void* src, size_t srcSize);
499
500/*! ZSTD_findDecompressedSize() :
501 * `src` should point the start of a series of ZSTD encoded and/or skippable frames
502 * `srcSize` must be the _exact_ size of this series
503 * (i.e. there should be a frame boundary exactly at `srcSize` bytes after `src`)
504 * @return : - decompressed size of all data in all successive frames
505 * - if the decompressed size cannot be determined: ZSTD_CONTENTSIZE_UNKNOWN
506 * - if an error occurred: ZSTD_CONTENTSIZE_ERROR
507 *
508 * note 1 : decompressed size is an optional field, that may not be present, especially in streaming mode.
509 * When `return==ZSTD_CONTENTSIZE_UNKNOWN`, data to decompress could be any size.
510 * In which case, it's necessary to use streaming mode to decompress data.
511 * note 2 : decompressed size is always present when compression is done with ZSTD_compress()
512 * note 3 : decompressed size can be very large (64-bits value),
513 * potentially larger than what local system can handle as a single memory segment.
514 * In which case, it's necessary to use streaming mode to decompress data.
515 * note 4 : If source is untrusted, decompressed size could be wrong or intentionally modified.
516 * Always ensure result fits within application's authorized limits.
517 * Each application can set its own limits.
518 * note 5 : ZSTD_findDecompressedSize handles multiple frames, and so it must traverse the input to
519 * read each contained frame header. This is fast as most of the data is skipped,
520 * however it does mean that all frame data must be present and valid. */
521ZSTDLIB_API unsigned long long ZSTD_findDecompressedSize(const void* src, size_t srcSize);
522
523/*! ZSTD_frameHeaderSize() :
524 * srcSize must be >= ZSTD_frameHeaderSize_prefix.
525 * @return : size of the Frame Header,
526 * or an error code (if srcSize is too small) */
527ZSTDLIB_API size_t ZSTD_frameHeaderSize(const void* src, size_t srcSize);
528
529
530/***************************************
531* Memory management
532***************************************/
533
534/*! ZSTD_sizeof_*() :
535 * These functions give the current memory usage of selected object.
536 * Object memory usage can evolve when re-used. */
537ZSTDLIB_API size_t ZSTD_sizeof_CCtx(const ZSTD_CCtx* cctx);
538ZSTDLIB_API size_t ZSTD_sizeof_DCtx(const ZSTD_DCtx* dctx);
539ZSTDLIB_API size_t ZSTD_sizeof_CStream(const ZSTD_CStream* zcs);
540ZSTDLIB_API size_t ZSTD_sizeof_DStream(const ZSTD_DStream* zds);
541ZSTDLIB_API size_t ZSTD_sizeof_CDict(const ZSTD_CDict* cdict);
542ZSTDLIB_API size_t ZSTD_sizeof_DDict(const ZSTD_DDict* ddict);
543
544/*! ZSTD_estimate*() :
545 * These functions make it possible to estimate memory usage
546 * of a future {D,C}Ctx, before its creation.
547 * ZSTD_estimateCCtxSize() will provide a budget large enough for any compression level up to selected one.
548 * It will also consider src size to be arbitrarily "large", which is worst case.
549 * If srcSize is known to always be small, ZSTD_estimateCCtxSize_usingCParams() can provide a tighter estimation.
550 * ZSTD_estimateCCtxSize_usingCParams() can be used in tandem with ZSTD_getCParams() to create cParams from compressionLevel.
551 * ZSTD_estimateCCtxSize_usingCCtxParams() can be used in tandem with ZSTD_CCtxParam_setParameter(). Only single-threaded compression is supported. This function will return an error code if ZSTD_p_nbWorkers is >= 1.
552 * Note : CCtx size estimation is only correct for single-threaded compression. */
553ZSTDLIB_API size_t ZSTD_estimateCCtxSize(int compressionLevel);
554ZSTDLIB_API size_t ZSTD_estimateCCtxSize_usingCParams(ZSTD_compressionParameters cParams);
555ZSTDLIB_API size_t ZSTD_estimateCCtxSize_usingCCtxParams(const ZSTD_CCtx_params* params);
556ZSTDLIB_API size_t ZSTD_estimateDCtxSize(void);
557
558/*! ZSTD_estimateCStreamSize() :
559 * ZSTD_estimateCStreamSize() will provide a budget large enough for any compression level up to selected one.
560 * It will also consider src size to be arbitrarily "large", which is worst case.
561 * If srcSize is known to always be small, ZSTD_estimateCStreamSize_usingCParams() can provide a tighter estimation.
562 * ZSTD_estimateCStreamSize_usingCParams() can be used in tandem with ZSTD_getCParams() to create cParams from compressionLevel.
563 * ZSTD_estimateCStreamSize_usingCCtxParams() can be used in tandem with ZSTD_CCtxParam_setParameter(). Only single-threaded compression is supported. This function will return an error code if ZSTD_p_nbWorkers is >= 1.
564 * Note : CStream size estimation is only correct for single-threaded compression.
565 * ZSTD_DStream memory budget depends on window Size.
566 * This information can be passed manually, using ZSTD_estimateDStreamSize,
567 * or deducted from a valid frame Header, using ZSTD_estimateDStreamSize_fromFrame();
568 * Note : if streaming is init with function ZSTD_init?Stream_usingDict(),
569 * an internal ?Dict will be created, which additional size is not estimated here.
570 * In this case, get total size by adding ZSTD_estimate?DictSize */
571ZSTDLIB_API size_t ZSTD_estimateCStreamSize(int compressionLevel);
572ZSTDLIB_API size_t ZSTD_estimateCStreamSize_usingCParams(ZSTD_compressionParameters cParams);
573ZSTDLIB_API size_t ZSTD_estimateCStreamSize_usingCCtxParams(const ZSTD_CCtx_params* params);
574ZSTDLIB_API size_t ZSTD_estimateDStreamSize(size_t windowSize);
575ZSTDLIB_API size_t ZSTD_estimateDStreamSize_fromFrame(const void* src, size_t srcSize);
576
577/*! ZSTD_estimate?DictSize() :
578 * ZSTD_estimateCDictSize() will bet that src size is relatively "small", and content is copied, like ZSTD_createCDict().
579 * ZSTD_estimateCDictSize_advanced() makes it possible to control compression parameters precisely, like ZSTD_createCDict_advanced().
580 * Note : dictionaries created by reference (`ZSTD_dlm_byRef`) are logically smaller.
581 */
582ZSTDLIB_API size_t ZSTD_estimateCDictSize(size_t dictSize, int compressionLevel);
583ZSTDLIB_API size_t ZSTD_estimateCDictSize_advanced(size_t dictSize, ZSTD_compressionParameters cParams, ZSTD_dictLoadMethod_e dictLoadMethod);
584ZSTDLIB_API size_t ZSTD_estimateDDictSize(size_t dictSize, ZSTD_dictLoadMethod_e dictLoadMethod);
585
586/*! ZSTD_initStatic*() :
587 * Initialize an object using a pre-allocated fixed-size buffer.
588 * workspace: The memory area to emplace the object into.
589 * Provided pointer *must be 8-bytes aligned*.
590 * Buffer must outlive object.
591 * workspaceSize: Use ZSTD_estimate*Size() to determine
592 * how large workspace must be to support target scenario.
593 * @return : pointer to object (same address as workspace, just different type),
594 * or NULL if error (size too small, incorrect alignment, etc.)
595 * Note : zstd will never resize nor malloc() when using a static buffer.
596 * If the object requires more memory than available,
597 * zstd will just error out (typically ZSTD_error_memory_allocation).
598 * Note 2 : there is no corresponding "free" function.
599 * Since workspace is allocated externally, it must be freed externally too.
600 * Note 3 : cParams : use ZSTD_getCParams() to convert a compression level
601 * into its associated cParams.
602 * Limitation 1 : currently not compatible with internal dictionary creation, triggered by
603 * ZSTD_CCtx_loadDictionary(), ZSTD_initCStream_usingDict() or ZSTD_initDStream_usingDict().
604 * Limitation 2 : static cctx currently not compatible with multi-threading.
605 * Limitation 3 : static dctx is incompatible with legacy support.
606 */
607ZSTDLIB_API ZSTD_CCtx* ZSTD_initStaticCCtx(void* workspace, size_t workspaceSize);
608ZSTDLIB_API ZSTD_CStream* ZSTD_initStaticCStream(void* workspace, size_t workspaceSize); /**< same as ZSTD_initStaticCCtx() */
609
610ZSTDLIB_API ZSTD_DCtx* ZSTD_initStaticDCtx(void* workspace, size_t workspaceSize);
611ZSTDLIB_API ZSTD_DStream* ZSTD_initStaticDStream(void* workspace, size_t workspaceSize); /**< same as ZSTD_initStaticDCtx() */
612
613ZSTDLIB_API const ZSTD_CDict* ZSTD_initStaticCDict(
614 void* workspace, size_t workspaceSize,
615 const void* dict, size_t dictSize,
616 ZSTD_dictLoadMethod_e dictLoadMethod,
617 ZSTD_dictContentType_e dictContentType,
618 ZSTD_compressionParameters cParams);
619
620ZSTDLIB_API const ZSTD_DDict* ZSTD_initStaticDDict(
621 void* workspace, size_t workspaceSize,
622 const void* dict, size_t dictSize,
623 ZSTD_dictLoadMethod_e dictLoadMethod,
624 ZSTD_dictContentType_e dictContentType);
625
626/*! Custom memory allocation :
627 * These prototypes make it possible to pass your own allocation/free functions.
628 * ZSTD_customMem is provided at creation time, using ZSTD_create*_advanced() variants listed below.
629 * All allocation/free operations will be completed using these custom variants instead of regular <stdlib.h> ones.
630 */
631typedef void* (*ZSTD_allocFunction) (void* opaque, size_t size);
632typedef void (*ZSTD_freeFunction) (void* opaque, void* address);
633typedef struct { ZSTD_allocFunction customAlloc; ZSTD_freeFunction customFree; void* opaque; } ZSTD_customMem;
634static ZSTD_customMem const ZSTD_defaultCMem = { NULL, NULL, NULL }; /**< this constant defers to stdlib's functions */
635
636ZSTDLIB_API ZSTD_CCtx* ZSTD_createCCtx_advanced(ZSTD_customMem customMem);
637ZSTDLIB_API ZSTD_CStream* ZSTD_createCStream_advanced(ZSTD_customMem customMem);
638ZSTDLIB_API ZSTD_DCtx* ZSTD_createDCtx_advanced(ZSTD_customMem customMem);
639ZSTDLIB_API ZSTD_DStream* ZSTD_createDStream_advanced(ZSTD_customMem customMem);
640
641ZSTDLIB_API ZSTD_CDict* ZSTD_createCDict_advanced(const void* dict, size_t dictSize,
642 ZSTD_dictLoadMethod_e dictLoadMethod,
643 ZSTD_dictContentType_e dictContentType,
644 ZSTD_compressionParameters cParams,
645 ZSTD_customMem customMem);
646
647ZSTDLIB_API ZSTD_DDict* ZSTD_createDDict_advanced(const void* dict, size_t dictSize,
648 ZSTD_dictLoadMethod_e dictLoadMethod,
649 ZSTD_dictContentType_e dictContentType,
650 ZSTD_customMem customMem);
651
652
653
654/***************************************
655* Advanced compression functions
656***************************************/
657
658/*! ZSTD_createCDict_byReference() :
659 * Create a digested dictionary for compression
660 * Dictionary content is simply referenced, and therefore stays in dictBuffer.
661 * It is important that dictBuffer outlives CDict, it must remain read accessible throughout the lifetime of CDict */
662ZSTDLIB_API ZSTD_CDict* ZSTD_createCDict_byReference(const void* dictBuffer, size_t dictSize, int compressionLevel);
663
664/*! ZSTD_getCParams() :
665* @return ZSTD_compressionParameters structure for a selected compression level and estimated srcSize.
666* `estimatedSrcSize` value is optional, select 0 if not known */
667ZSTDLIB_API ZSTD_compressionParameters ZSTD_getCParams(int compressionLevel, unsigned long long estimatedSrcSize, size_t dictSize);
668
669/*! ZSTD_getParams() :
670* same as ZSTD_getCParams(), but @return a full `ZSTD_parameters` object instead of sub-component `ZSTD_compressionParameters`.
671* All fields of `ZSTD_frameParameters` are set to default : contentSize=1, checksum=0, noDictID=0 */
672ZSTDLIB_API ZSTD_parameters ZSTD_getParams(int compressionLevel, unsigned long long estimatedSrcSize, size_t dictSize);
673
674/*! ZSTD_checkCParams() :
675* Ensure param values remain within authorized range */
676ZSTDLIB_API size_t ZSTD_checkCParams(ZSTD_compressionParameters params);
677
678/*! ZSTD_adjustCParams() :
679 * optimize params for a given `srcSize` and `dictSize`.
680 * both values are optional, select `0` if unknown. */
681ZSTDLIB_API ZSTD_compressionParameters ZSTD_adjustCParams(ZSTD_compressionParameters cPar, unsigned long long srcSize, size_t dictSize);
682
683/*! ZSTD_compress_advanced() :
684* Same as ZSTD_compress_usingDict(), with fine-tune control over each compression parameter */
685ZSTDLIB_API size_t ZSTD_compress_advanced (ZSTD_CCtx* cctx,
686 void* dst, size_t dstCapacity,
687 const void* src, size_t srcSize,
688 const void* dict,size_t dictSize,
689 ZSTD_parameters params);
690
691/*! ZSTD_compress_usingCDict_advanced() :
692* Same as ZSTD_compress_usingCDict(), with fine-tune control over frame parameters */
693ZSTDLIB_API size_t ZSTD_compress_usingCDict_advanced(ZSTD_CCtx* cctx,
694 void* dst, size_t dstCapacity,
695 const void* src, size_t srcSize,
696 const ZSTD_CDict* cdict, ZSTD_frameParameters fParams);
697
698
699/*--- Advanced decompression functions ---*/
700
701/*! ZSTD_isFrame() :
702 * Tells if the content of `buffer` starts with a valid Frame Identifier.
703 * Note : Frame Identifier is 4 bytes. If `size < 4`, @return will always be 0.
704 * Note 2 : Legacy Frame Identifiers are considered valid only if Legacy Support is enabled.
705 * Note 3 : Skippable Frame Identifiers are considered valid. */
706ZSTDLIB_API unsigned ZSTD_isFrame(const void* buffer, size_t size);
707
708/*! ZSTD_createDDict_byReference() :
709 * Create a digested dictionary, ready to start decompression operation without startup delay.
710 * Dictionary content is referenced, and therefore stays in dictBuffer.
711 * It is important that dictBuffer outlives DDict,
712 * it must remain read accessible throughout the lifetime of DDict */
713ZSTDLIB_API ZSTD_DDict* ZSTD_createDDict_byReference(const void* dictBuffer, size_t dictSize);
714
715
716/*! ZSTD_getDictID_fromDict() :
717 * Provides the dictID stored within dictionary.
718 * if @return == 0, the dictionary is not conformant with Zstandard specification.
719 * It can still be loaded, but as a content-only dictionary. */
720ZSTDLIB_API unsigned ZSTD_getDictID_fromDict(const void* dict, size_t dictSize);
721
722/*! ZSTD_getDictID_fromDDict() :
723 * Provides the dictID of the dictionary loaded into `ddict`.
724 * If @return == 0, the dictionary is not conformant to Zstandard specification, or empty.
725 * Non-conformant dictionaries can still be loaded, but as content-only dictionaries. */
726ZSTDLIB_API unsigned ZSTD_getDictID_fromDDict(const ZSTD_DDict* ddict);
727
728/*! ZSTD_getDictID_fromFrame() :
729 * Provides the dictID required to decompressed the frame stored within `src`.
730 * If @return == 0, the dictID could not be decoded.
731 * This could for one of the following reasons :
732 * - The frame does not require a dictionary to be decoded (most common case).
733 * - The frame was built with dictID intentionally removed. Whatever dictionary is necessary is a hidden information.
734 * Note : this use case also happens when using a non-conformant dictionary.
735 * - `srcSize` is too small, and as a result, the frame header could not be decoded (only possible if `srcSize < ZSTD_FRAMEHEADERSIZE_MAX`).
736 * - This is not a Zstandard frame.
737 * When identifying the exact failure cause, it's possible to use ZSTD_getFrameHeader(), which will provide a more precise error code. */
738ZSTDLIB_API unsigned ZSTD_getDictID_fromFrame(const void* src, size_t srcSize);
739
740
741/********************************************************************
742* Advanced streaming functions
743********************************************************************/
744
745/*===== Advanced Streaming compression functions =====*/
746ZSTDLIB_API size_t ZSTD_initCStream_srcSize(ZSTD_CStream* zcs, int compressionLevel, unsigned long long pledgedSrcSize); /**< pledgedSrcSize must be correct. If it is not known at init time, use ZSTD_CONTENTSIZE_UNKNOWN. Note that, for compatibility with older programs, "0" also disables frame content size field. It may be enabled in the future. */
747ZSTDLIB_API size_t ZSTD_initCStream_usingDict(ZSTD_CStream* zcs, const void* dict, size_t dictSize, int compressionLevel); /**< creates of an internal CDict (incompatible with static CCtx), except if dict == NULL or dictSize < 8, in which case no dict is used. Note: dict is loaded with ZSTD_dm_auto (treated as a full zstd dictionary if it begins with ZSTD_MAGIC_DICTIONARY, else as raw content) and ZSTD_dlm_byCopy.*/
748ZSTDLIB_API size_t ZSTD_initCStream_advanced(ZSTD_CStream* zcs, const void* dict, size_t dictSize,
749 ZSTD_parameters params, unsigned long long pledgedSrcSize); /**< pledgedSrcSize must be correct. If srcSize is not known at init time, use value ZSTD_CONTENTSIZE_UNKNOWN. dict is loaded with ZSTD_dm_auto and ZSTD_dlm_byCopy. */
750ZSTDLIB_API size_t ZSTD_initCStream_usingCDict(ZSTD_CStream* zcs, const ZSTD_CDict* cdict); /**< note : cdict will just be referenced, and must outlive compression session */
751ZSTDLIB_API size_t ZSTD_initCStream_usingCDict_advanced(ZSTD_CStream* zcs, const ZSTD_CDict* cdict, ZSTD_frameParameters fParams, unsigned long long pledgedSrcSize); /**< same as ZSTD_initCStream_usingCDict(), with control over frame parameters. pledgedSrcSize must be correct. If srcSize is not known at init time, use value ZSTD_CONTENTSIZE_UNKNOWN. */
752
753/*! ZSTD_resetCStream() :
754 * start a new compression job, using same parameters from previous job.
755 * This is typically useful to skip dictionary loading stage, since it will re-use it in-place.
756 * Note that zcs must be init at least once before using ZSTD_resetCStream().
757 * If pledgedSrcSize is not known at reset time, use macro ZSTD_CONTENTSIZE_UNKNOWN.
758 * If pledgedSrcSize > 0, its value must be correct, as it will be written in header, and controlled at the end.
759 * For the time being, pledgedSrcSize==0 is interpreted as "srcSize unknown" for compatibility with older programs,
760 * but it will change to mean "empty" in future version, so use macro ZSTD_CONTENTSIZE_UNKNOWN instead.
761 * @return : 0, or an error code (which can be tested using ZSTD_isError())
762 */
763ZSTDLIB_API size_t ZSTD_resetCStream(ZSTD_CStream* zcs, unsigned long long pledgedSrcSize);
764
765
766typedef struct {
767 unsigned long long ingested; /* nb input bytes read and buffered */
768 unsigned long long consumed; /* nb input bytes actually compressed */
769 unsigned long long produced; /* nb of compressed bytes generated and buffered */
770 unsigned long long flushed; /* nb of compressed bytes flushed : not provided; can be tracked from caller side */
771 unsigned currentJobID; /* MT only : latest started job nb */
772 unsigned nbActiveWorkers; /* MT only : nb of workers actively compressing at probe time */
773} ZSTD_frameProgression;
774
775/* ZSTD_getFrameProgression() :
776 * tells how much data has been ingested (read from input)
777 * consumed (input actually compressed) and produced (output) for current frame.
778 * Note : (ingested - consumed) is amount of input data buffered internally, not yet compressed.
779 * Aggregates progression inside active worker threads.
780 */
781ZSTDLIB_API ZSTD_frameProgression ZSTD_getFrameProgression(const ZSTD_CCtx* cctx);
782
783/*! ZSTD_toFlushNow() :
784 * Tell how many bytes are ready to be flushed immediately.
785 * Useful for multithreading scenarios (nbWorkers >= 1).
786 * Probe the oldest active job, defined as oldest job not yet entirely flushed,
787 * and check its output buffer.
788 * @return : amount of data stored in oldest job and ready to be flushed immediately.
789 * if @return == 0, it means either :
790 * + there is no active job (could be checked with ZSTD_frameProgression()), or
791 * + oldest job is still actively compressing data,
792 * but everything it has produced has also been flushed so far,
793 * therefore flushing speed is currently limited by production speed of oldest job
794 * irrespective of the speed of concurrent newer jobs.
795 */
796ZSTDLIB_API size_t ZSTD_toFlushNow(ZSTD_CCtx* cctx);
797
798
799
800/*===== Advanced Streaming decompression functions =====*/
801typedef enum { DStream_p_maxWindowSize } ZSTD_DStreamParameter_e;
802ZSTDLIB_API size_t ZSTD_setDStreamParameter(ZSTD_DStream* zds, ZSTD_DStreamParameter_e paramType, unsigned paramValue); /* obsolete : this API will be removed in a future version */
803ZSTDLIB_API size_t ZSTD_initDStream_usingDict(ZSTD_DStream* zds, const void* dict, size_t dictSize); /**< note: no dictionary will be used if dict == NULL or dictSize < 8 */
804ZSTDLIB_API size_t ZSTD_initDStream_usingDDict(ZSTD_DStream* zds, const ZSTD_DDict* ddict); /**< note : ddict is referenced, it must outlive decompression session */
805ZSTDLIB_API size_t ZSTD_resetDStream(ZSTD_DStream* zds); /**< re-use decompression parameters from previous init; saves dictionary loading */
806
807
808/*********************************************************************
809* Buffer-less and synchronous inner streaming functions
810*
811* This is an advanced API, giving full control over buffer management, for users which need direct control over memory.
812* But it's also a complex one, with several restrictions, documented below.
813* Prefer normal streaming API for an easier experience.
814********************************************************************* */
815
816/**
817 Buffer-less streaming compression (synchronous mode)
818
819 A ZSTD_CCtx object is required to track streaming operations.
820 Use ZSTD_createCCtx() / ZSTD_freeCCtx() to manage resource.
821 ZSTD_CCtx object can be re-used multiple times within successive compression operations.
822
823 Start by initializing a context.
824 Use ZSTD_compressBegin(), or ZSTD_compressBegin_usingDict() for dictionary compression,
825 or ZSTD_compressBegin_advanced(), for finer parameter control.
826 It's also possible to duplicate a reference context which has already been initialized, using ZSTD_copyCCtx()
827
828 Then, consume your input using ZSTD_compressContinue().
829 There are some important considerations to keep in mind when using this advanced function :
830 - ZSTD_compressContinue() has no internal buffer. It uses externally provided buffers only.
831 - Interface is synchronous : input is consumed entirely and produces 1+ compressed blocks.
832 - Caller must ensure there is enough space in `dst` to store compressed data under worst case scenario.
833 Worst case evaluation is provided by ZSTD_compressBound().
834 ZSTD_compressContinue() doesn't guarantee recover after a failed compression.
835 - ZSTD_compressContinue() presumes prior input ***is still accessible and unmodified*** (up to maximum distance size, see WindowLog).
836 It remembers all previous contiguous blocks, plus one separated memory segment (which can itself consists of multiple contiguous blocks)
837 - ZSTD_compressContinue() detects that prior input has been overwritten when `src` buffer overlaps.
838 In which case, it will "discard" the relevant memory section from its history.
839
840 Finish a frame with ZSTD_compressEnd(), which will write the last block(s) and optional checksum.
841 It's possible to use srcSize==0, in which case, it will write a final empty block to end the frame.
842 Without last block mark, frames are considered unfinished (hence corrupted) by compliant decoders.
843
844 `ZSTD_CCtx` object can be re-used (ZSTD_compressBegin()) to compress again.
845*/
846
847/*===== Buffer-less streaming compression functions =====*/
848ZSTDLIB_API size_t ZSTD_compressBegin(ZSTD_CCtx* cctx, int compressionLevel);
849ZSTDLIB_API size_t ZSTD_compressBegin_usingDict(ZSTD_CCtx* cctx, const void* dict, size_t dictSize, int compressionLevel);
850ZSTDLIB_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 */
851ZSTDLIB_API size_t ZSTD_compressBegin_usingCDict(ZSTD_CCtx* cctx, const ZSTD_CDict* cdict); /**< note: fails if cdict==NULL */
852ZSTDLIB_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 */
853ZSTDLIB_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 */
854
855ZSTDLIB_API size_t ZSTD_compressContinue(ZSTD_CCtx* cctx, void* dst, size_t dstCapacity, const void* src, size_t srcSize);
856ZSTDLIB_API size_t ZSTD_compressEnd(ZSTD_CCtx* cctx, void* dst, size_t dstCapacity, const void* src, size_t srcSize);
857
858
859/*-
860 Buffer-less streaming decompression (synchronous mode)
861
862 A ZSTD_DCtx object is required to track streaming operations.
863 Use ZSTD_createDCtx() / ZSTD_freeDCtx() to manage it.
864 A ZSTD_DCtx object can be re-used multiple times.
865
866 First typical operation is to retrieve frame parameters, using ZSTD_getFrameHeader().
867 Frame header is extracted from the beginning of compressed frame, so providing only the frame's beginning is enough.
868 Data fragment must be large enough to ensure successful decoding.
869 `ZSTD_frameHeaderSize_max` bytes is guaranteed to always be large enough.
870 @result : 0 : successful decoding, the `ZSTD_frameHeader` structure is correctly filled.
871 >0 : `srcSize` is too small, please provide at least @result bytes on next attempt.
872 errorCode, which can be tested using ZSTD_isError().
873
874 It fills a ZSTD_frameHeader structure with important information to correctly decode the frame,
875 such as the dictionary ID, content size, or maximum back-reference distance (`windowSize`).
876 Note that these values could be wrong, either because of data corruption, or because a 3rd party deliberately spoofs false information.
877 As a consequence, check that values remain within valid application range.
878 For example, do not allocate memory blindly, check that `windowSize` is within expectation.
879 Each application can set its own limits, depending on local restrictions.
880 For extended interoperability, it is recommended to support `windowSize` of at least 8 MB.
881
882 ZSTD_decompressContinue() needs previous data blocks during decompression, up to `windowSize` bytes.
883 ZSTD_decompressContinue() is very sensitive to contiguity,
884 if 2 blocks don't follow each other, make sure that either the compressor breaks contiguity at the same place,
885 or that previous contiguous segment is large enough to properly handle maximum back-reference distance.
886 There are multiple ways to guarantee this condition.
887
888 The most memory efficient way is to use a round buffer of sufficient size.
889 Sufficient size is determined by invoking ZSTD_decodingBufferSize_min(),
890 which can @return an error code if required value is too large for current system (in 32-bits mode).
891 In a round buffer methodology, ZSTD_decompressContinue() decompresses each block next to previous one,
892 up to the moment there is not enough room left in the buffer to guarantee decoding another full block,
893 which maximum size is provided in `ZSTD_frameHeader` structure, field `blockSizeMax`.
894 At which point, decoding can resume from the beginning of the buffer.
895 Note that already decoded data stored in the buffer should be flushed before being overwritten.
896
897 There are alternatives possible, for example using two or more buffers of size `windowSize` each, though they consume more memory.
898
899 Finally, if you control the compression process, you can also ignore all buffer size rules,
900 as long as the encoder and decoder progress in "lock-step",
901 aka use exactly the same buffer sizes, break contiguity at the same place, etc.
902
903 Once buffers are setup, start decompression, with ZSTD_decompressBegin().
904 If decompression requires a dictionary, use ZSTD_decompressBegin_usingDict() or ZSTD_decompressBegin_usingDDict().
905
906 Then use ZSTD_nextSrcSizeToDecompress() and ZSTD_decompressContinue() alternatively.
907 ZSTD_nextSrcSizeToDecompress() tells how many bytes to provide as 'srcSize' to ZSTD_decompressContinue().
908 ZSTD_decompressContinue() requires this _exact_ amount of bytes, or it will fail.
909
910 @result of ZSTD_decompressContinue() is the number of bytes regenerated within 'dst' (necessarily <= dstCapacity).
911 It can be zero : it just means ZSTD_decompressContinue() has decoded some metadata item.
912 It can also be an error code, which can be tested with ZSTD_isError().
913
914 A frame is fully decoded when ZSTD_nextSrcSizeToDecompress() returns zero.
915 Context can then be reset to start a new decompression.
916
917 Note : it's possible to know if next input to present is a header or a block, using ZSTD_nextInputType().
918 This information is not required to properly decode a frame.
919
920 == Special case : skippable frames ==
921
922 Skippable frames allow integration of user-defined data into a flow of concatenated frames.
923 Skippable frames will be ignored (skipped) by decompressor.
924 The format of skippable frames is as follows :
925 a) Skippable frame ID - 4 Bytes, Little endian format, any value from 0x184D2A50 to 0x184D2A5F
926 b) Frame Size - 4 Bytes, Little endian format, unsigned 32-bits
927 c) Frame Content - any content (User Data) of length equal to Frame Size
928 For skippable frames ZSTD_getFrameHeader() returns zfhPtr->frameType==ZSTD_skippableFrame.
929 For skippable frames ZSTD_decompressContinue() always returns 0 : it only skips the content.
930*/
931
932/*===== Buffer-less streaming decompression functions =====*/
933typedef enum { ZSTD_frame, ZSTD_skippableFrame } ZSTD_frameType_e;
934typedef struct {
935 unsigned long long frameContentSize; /* if == ZSTD_CONTENTSIZE_UNKNOWN, it means this field is not available. 0 means "empty" */
936 unsigned long long windowSize; /* can be very large, up to <= frameContentSize */
937 unsigned blockSizeMax;
938 ZSTD_frameType_e frameType; /* if == ZSTD_skippableFrame, frameContentSize is the size of skippable content */
939 unsigned headerSize;
940 unsigned dictID;
941 unsigned checksumFlag;
942} ZSTD_frameHeader;
943/** ZSTD_getFrameHeader() :
944 * decode Frame Header, or requires larger `srcSize`.
945 * @return : 0, `zfhPtr` is correctly filled,
946 * >0, `srcSize` is too small, value is wanted `srcSize` amount,
947 * or an error code, which can be tested using ZSTD_isError() */
948ZSTDLIB_API size_t ZSTD_getFrameHeader(ZSTD_frameHeader* zfhPtr, const void* src, size_t srcSize); /**< doesn't consume input */
949ZSTDLIB_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 */
950
951ZSTDLIB_API size_t ZSTD_decompressBegin(ZSTD_DCtx* dctx);
952ZSTDLIB_API size_t ZSTD_decompressBegin_usingDict(ZSTD_DCtx* dctx, const void* dict, size_t dictSize);
953ZSTDLIB_API size_t ZSTD_decompressBegin_usingDDict(ZSTD_DCtx* dctx, const ZSTD_DDict* ddict);
954
955ZSTDLIB_API size_t ZSTD_nextSrcSizeToDecompress(ZSTD_DCtx* dctx);
956ZSTDLIB_API size_t ZSTD_decompressContinue(ZSTD_DCtx* dctx, void* dst, size_t dstCapacity, const void* src, size_t srcSize);
957
958/* misc */
959ZSTDLIB_API void ZSTD_copyDCtx(ZSTD_DCtx* dctx, const ZSTD_DCtx* preparedDCtx);
960typedef enum { ZSTDnit_frameHeader, ZSTDnit_blockHeader, ZSTDnit_block, ZSTDnit_lastBlock, ZSTDnit_checksum, ZSTDnit_skippableFrame } ZSTD_nextInputType_e;
961ZSTDLIB_API ZSTD_nextInputType_e ZSTD_nextInputType(ZSTD_DCtx* dctx);
962
963
964
965/* ============================================ */
966/** New advanced API (experimental) */
967/* ============================================ */
968
969/* API design :
970 * In this advanced API, parameters are pushed one by one into an existing context,
971 * using ZSTD_CCtx_set*() functions.
972 * Pushed parameters are sticky : they are applied to next job, and any subsequent job.
973 * It's possible to reset parameters to "default" using ZSTD_CCtx_reset().
974 * Important : "sticky" parameters only work with `ZSTD_compress_generic()` !
975 * For any other entry point, "sticky" parameters are ignored !
976 *
977 * This API is intended to replace all others advanced / experimental API entry points.
978 */
979
980/* note on enum design :
981 * All enum will be pinned to explicit values before reaching "stable API" status */
982
983typedef enum {
984 /* Opened question : should we have a format ZSTD_f_auto ?
985 * Today, it would mean exactly the same as ZSTD_f_zstd1.
986 * But, in the future, should several formats become supported,
987 * on the compression side, it would mean "default format".
988 * On the decompression side, it would mean "automatic format detection",
989 * so that ZSTD_f_zstd1 would mean "accept *only* zstd frames".
990 * Since meaning is a little different, another option could be to define different enums for compression and decompression.
991 * This question could be kept for later, when there are actually multiple formats to support,
992 * but there is also the question of pinning enum values, and pinning value `0` is especially important */
993 ZSTD_f_zstd1 = 0, /* zstd frame format, specified in zstd_compression_format.md (default) */
994 ZSTD_f_zstd1_magicless, /* Variant of zstd frame format, without initial 4-bytes magic number.
995 * Useful to save 4 bytes per generated frame.
996 * Decoder cannot recognise automatically this format, requiring instructions. */
997} ZSTD_format_e;
998
999typedef enum {
1000 /* compression format */
1001 ZSTD_p_format = 10, /* See ZSTD_format_e enum definition.
1002 * Cast selected format as unsigned for ZSTD_CCtx_setParameter() compatibility. */
1003
1004 /* compression parameters */
1005 ZSTD_p_compressionLevel=100, /* Update all compression parameters according to pre-defined cLevel table
1006 * Default level is ZSTD_CLEVEL_DEFAULT==3.
1007 * Special: value 0 means default, which is controlled by ZSTD_CLEVEL_DEFAULT.
1008 * Note 1 : it's possible to pass a negative compression level by casting it to unsigned type.
1009 * Note 2 : setting a level sets all default values of other compression parameters.
1010 * Note 3 : setting compressionLevel automatically updates ZSTD_p_compressLiterals. */
1011 ZSTD_p_windowLog, /* Maximum allowed back-reference distance, expressed as power of 2.
1012 * Must be clamped between ZSTD_WINDOWLOG_MIN and ZSTD_WINDOWLOG_MAX.
1013 * Special: value 0 means "use default windowLog".
1014 * Note: Using a window size greater than ZSTD_MAXWINDOWSIZE_DEFAULT (default: 2^27)
1015 * requires explicitly allowing such window size during decompression stage. */
1016 ZSTD_p_hashLog, /* Size of the initial probe table, as a power of 2.
1017 * Resulting table size is (1 << (hashLog+2)).
1018 * Must be clamped between ZSTD_HASHLOG_MIN and ZSTD_HASHLOG_MAX.
1019 * Larger tables improve compression ratio of strategies <= dFast,
1020 * and improve speed of strategies > dFast.
1021 * Special: value 0 means "use default hashLog". */
1022 ZSTD_p_chainLog, /* Size of the multi-probe search table, as a power of 2.
1023 * Resulting table size is (1 << (chainLog+2)).
1024 * Must be clamped between ZSTD_CHAINLOG_MIN and ZSTD_CHAINLOG_MAX.
1025 * Larger tables result in better and slower compression.
1026 * This parameter is useless when using "fast" strategy.
1027 * Note it's still useful when using "dfast" strategy,
1028 * in which case it defines a secondary probe table.
1029 * Special: value 0 means "use default chainLog". */
1030 ZSTD_p_searchLog, /* Number of search attempts, as a power of 2.
1031 * More attempts result in better and slower compression.
1032 * This parameter is useless when using "fast" and "dFast" strategies.
1033 * Special: value 0 means "use default searchLog". */
1034 ZSTD_p_minMatch, /* Minimum size of searched matches (note : repCode matches can be smaller).
1035 * Larger values make faster compression and decompression, but decrease ratio.
1036 * Must be clamped between ZSTD_SEARCHLENGTH_MIN and ZSTD_SEARCHLENGTH_MAX.
1037 * Note that currently, for all strategies < btopt, effective minimum is 4.
1038 * , for all strategies > fast, effective maximum is 6.
1039 * Special: value 0 means "use default minMatchLength". */
1040 ZSTD_p_targetLength, /* Impact of this field depends on strategy.
1041 * For strategies btopt & btultra:
1042 * Length of Match considered "good enough" to stop search.
1043 * Larger values make compression stronger, and slower.
1044 * For strategy fast:
1045 * Distance between match sampling.
1046 * Larger values make compression faster, and weaker.
1047 * Special: value 0 means "use default targetLength". */
1048 ZSTD_p_compressionStrategy, /* See ZSTD_strategy enum definition.
1049 * Cast selected strategy as unsigned for ZSTD_CCtx_setParameter() compatibility.
1050 * The higher the value of selected strategy, the more complex it is,
1051 * resulting in stronger and slower compression.
1052 * Special: value 0 means "use default strategy". */
1053
1054 ZSTD_p_enableLongDistanceMatching=160, /* Enable long distance matching.
1055 * This parameter is designed to improve compression ratio
1056 * for large inputs, by finding large matches at long distance.
1057 * It increases memory usage and window size.
1058 * Note: enabling this parameter increases ZSTD_p_windowLog to 128 MB
1059 * except when expressly set to a different value. */
1060 ZSTD_p_ldmHashLog, /* Size of the table for long distance matching, as a power of 2.
1061 * Larger values increase memory usage and compression ratio,
1062 * but decrease compression speed.
1063 * Must be clamped between ZSTD_HASHLOG_MIN and ZSTD_HASHLOG_MAX
1064 * default: windowlog - 7.
1065 * Special: value 0 means "automatically determine hashlog". */
1066 ZSTD_p_ldmMinMatch, /* Minimum match size for long distance matcher.
1067 * Larger/too small values usually decrease compression ratio.
1068 * Must be clamped between ZSTD_LDM_MINMATCH_MIN and ZSTD_LDM_MINMATCH_MAX.
1069 * Special: value 0 means "use default value" (default: 64). */
1070 ZSTD_p_ldmBucketSizeLog, /* Log size of each bucket in the LDM hash table for collision resolution.
1071 * Larger values improve collision resolution but decrease compression speed.
1072 * The maximum value is ZSTD_LDM_BUCKETSIZELOG_MAX .
1073 * Special: value 0 means "use default value" (default: 3). */
1074 ZSTD_p_ldmHashEveryLog, /* Frequency of inserting/looking up entries in the LDM hash table.
1075 * Must be clamped between 0 and (ZSTD_WINDOWLOG_MAX - ZSTD_HASHLOG_MIN).
1076 * Default is MAX(0, (windowLog - ldmHashLog)), optimizing hash table usage.
1077 * Larger values improve compression speed.
1078 * Deviating far from default value will likely result in a compression ratio decrease.
1079 * Special: value 0 means "automatically determine hashEveryLog". */
1080
1081 /* frame parameters */
1082 ZSTD_p_contentSizeFlag=200, /* Content size will be written into frame header _whenever known_ (default:1)
1083 * Content size must be known at the beginning of compression,
1084 * it is provided using ZSTD_CCtx_setPledgedSrcSize() */
1085 ZSTD_p_checksumFlag, /* A 32-bits checksum of content is written at end of frame (default:0) */
1086 ZSTD_p_dictIDFlag, /* When applicable, dictionary's ID is written into frame header (default:1) */
1087
1088 /* multi-threading parameters */
1089 /* These parameters are only useful if multi-threading is enabled (ZSTD_MULTITHREAD).
1090 * They return an error otherwise. */
1091 ZSTD_p_nbWorkers=400, /* Select how many threads will be spawned to compress in parallel.
1092 * When nbWorkers >= 1, triggers asynchronous mode :
1093 * ZSTD_compress_generic() consumes some input, flush some output if possible, and immediately gives back control to caller,
1094 * while compression work is performed in parallel, within worker threads.
1095 * (note : a strong exception to this rule is when first invocation sets ZSTD_e_end : it becomes a blocking call).
1096 * More workers improve speed, but also increase memory usage.
1097 * Default value is `0`, aka "single-threaded mode" : no worker is spawned, compression is performed inside Caller's thread, all invocations are blocking */
1098 ZSTD_p_jobSize, /* Size of a compression job. This value is enforced only in non-blocking mode.
1099 * Each compression job is completed in parallel, so this value indirectly controls the nb of active threads.
1100 * 0 means default, which is dynamically determined based on compression parameters.
1101 * Job size must be a minimum of overlapSize, or 1 MB, whichever is largest.
1102 * The minimum size is automatically and transparently enforced */
1103 ZSTD_p_overlapSizeLog, /* Size of previous input reloaded at the beginning of each job.
1104 * 0 => no overlap, 6(default) => use 1/8th of windowSize, >=9 => use full windowSize */
1105
1106 /* =================================================================== */
1107 /* experimental parameters - no stability guaranteed */
1108 /* =================================================================== */
1109
1110 ZSTD_p_forceMaxWindow=1100, /* Force back-reference distances to remain < windowSize,
1111 * even when referencing into Dictionary content (default:0) */
1112 ZSTD_p_forceAttachDict, /* ZSTD supports usage of a CDict in-place
1113 * (avoiding having to copy the compression tables
1114 * from the CDict into the working context). Using
1115 * a CDict in this way saves an initial setup step,
1116 * but comes at the cost of more work per byte of
1117 * input. ZSTD has a simple internal heuristic that
1118 * guesses which strategy will be faster. You can
1119 * use this flag to override that guess.
1120 *
1121 * Note that the by-reference, in-place strategy is
1122 * only used when reusing a compression context
1123 * with compatible compression parameters. (If
1124 * incompatible / uninitialized, the working
1125 * context needs to be cleared anyways, which is
1126 * about as expensive as overwriting it with the
1127 * dictionary context, so there's no savings in
1128 * using the CDict by-ref.)
1129 *
1130 * Values greater than 0 force attaching the dict.
1131 * Values less than 0 force copying the dict.
1132 * 0 selects the default heuristic-guided behavior.
1133 */
1134
1135} ZSTD_cParameter;
1136
1137
1138/*! ZSTD_CCtx_setParameter() :
1139 * Set one compression parameter, selected by enum ZSTD_cParameter.
1140 * Setting a parameter is generally only possible during frame initialization (before starting compression).
1141 * Exception : when using multi-threading mode (nbThreads >= 1),
1142 * following parameters can be updated _during_ compression (within same frame):
1143 * => compressionLevel, hashLog, chainLog, searchLog, minMatch, targetLength and strategy.
1144 * new parameters will be active on next job, or after a flush().
1145 * Note : when `value` type is not unsigned (int, or enum), cast it to unsigned for proper type checking.
1146 * @result : informational value (typically, value being set, correctly clamped),
1147 * or an error code (which can be tested with ZSTD_isError()). */
1148ZSTDLIB_API size_t ZSTD_CCtx_setParameter(ZSTD_CCtx* cctx, ZSTD_cParameter param, unsigned value);
1149
1150/*! ZSTD_CCtx_getParameter() :
1151 * Get the requested value of one compression parameter, selected by enum ZSTD_cParameter.
1152 * @result : 0, or an error code (which can be tested with ZSTD_isError()).
1153 */
1154ZSTDLIB_API size_t ZSTD_CCtx_getParameter(ZSTD_CCtx* cctx, ZSTD_cParameter param, unsigned* value);
1155
1156/*! ZSTD_CCtx_setPledgedSrcSize() :
1157 * Total input data size to be compressed as a single frame.
1158 * This value will be controlled at the end, and result in error if not respected.
1159 * @result : 0, or an error code (which can be tested with ZSTD_isError()).
1160 * Note 1 : 0 means zero, empty.
1161 * In order to mean "unknown content size", pass constant ZSTD_CONTENTSIZE_UNKNOWN.
1162 * ZSTD_CONTENTSIZE_UNKNOWN is default value for any new compression job.
1163 * Note 2 : If all data is provided and consumed in a single round,
1164 * this value is overriden by srcSize instead. */
1165ZSTDLIB_API size_t ZSTD_CCtx_setPledgedSrcSize(ZSTD_CCtx* cctx, unsigned long long pledgedSrcSize);
1166
1167/*! ZSTD_CCtx_loadDictionary() :
1168 * Create an internal CDict from `dict` buffer.
1169 * Decompression will have to use same dictionary.
1170 * @result : 0, or an error code (which can be tested with ZSTD_isError()).
1171 * Special: Adding a NULL (or 0-size) dictionary invalidates previous dictionary,
1172 * meaning "return to no-dictionary mode".
1173 * Note 1 : Dictionary will be used for all future compression jobs.
1174 * To return to "no-dictionary" situation, load a NULL dictionary
1175 * Note 2 : Loading a dictionary involves building tables, which are dependent on compression parameters.
1176 * For this reason, compression parameters cannot be changed anymore after loading a dictionary.
1177 * It's also a CPU consuming operation, with non-negligible impact on latency.
1178 * Note 3 :`dict` content will be copied internally.
1179 * Use ZSTD_CCtx_loadDictionary_byReference() to reference dictionary content instead.
1180 * In such a case, dictionary buffer must outlive its users.
1181 * Note 4 : Use ZSTD_CCtx_loadDictionary_advanced()
1182 * to precisely select how dictionary content must be interpreted. */
1183ZSTDLIB_API size_t ZSTD_CCtx_loadDictionary(ZSTD_CCtx* cctx, const void* dict, size_t dictSize);
1184ZSTDLIB_API size_t ZSTD_CCtx_loadDictionary_byReference(ZSTD_CCtx* cctx, const void* dict, size_t dictSize);
1185ZSTDLIB_API size_t ZSTD_CCtx_loadDictionary_advanced(ZSTD_CCtx* cctx, const void* dict, size_t dictSize, ZSTD_dictLoadMethod_e dictLoadMethod, ZSTD_dictContentType_e dictContentType);
1186
1187
1188/*! ZSTD_CCtx_refCDict() :
1189 * Reference a prepared dictionary, to be used for all next compression jobs.
1190 * Note that compression parameters are enforced from within CDict,
1191 * and supercede any compression parameter previously set within CCtx.
1192 * The dictionary will remain valid for future compression jobs using same CCtx.
1193 * @result : 0, or an error code (which can be tested with ZSTD_isError()).
1194 * Special : adding a NULL CDict means "return to no-dictionary mode".
1195 * Note 1 : Currently, only one dictionary can be managed.
1196 * Adding a new dictionary effectively "discards" any previous one.
1197 * Note 2 : CDict is just referenced, its lifetime must outlive CCtx. */
1198ZSTDLIB_API size_t ZSTD_CCtx_refCDict(ZSTD_CCtx* cctx, const ZSTD_CDict* cdict);
1199
1200/*! ZSTD_CCtx_refPrefix() :
1201 * Reference a prefix (single-usage dictionary) for next compression job.
1202 * Decompression will need same prefix to properly regenerate data.
1203 * Compressing with a prefix is similar in outcome as performing a diff and compressing it,
1204 * but performs much faster, especially during decompression (compression speed is tunable with compression level).
1205 * Note that prefix is **only used once**. Tables are discarded at end of compression job (ZSTD_e_end).
1206 * @result : 0, or an error code (which can be tested with ZSTD_isError()).
1207 * Special: Adding any prefix (including NULL) invalidates any previous prefix or dictionary
1208 * Note 1 : Prefix buffer is referenced. It **must** outlive compression job.
1209 * Its contain must remain unmodified up to end of compression (ZSTD_e_end).
1210 * Note 2 : If the intention is to diff some large src data blob with some prior version of itself,
1211 * ensure that the window size is large enough to contain the entire source.
1212 * See ZSTD_p_windowLog.
1213 * Note 3 : Referencing a prefix involves building tables, which are dependent on compression parameters.
1214 * It's a CPU consuming operation, with non-negligible impact on latency.
1215 * If there is a need to use same prefix multiple times, consider loadDictionary instead.
1216 * Note 4 : By default, the prefix is treated as raw content (ZSTD_dm_rawContent).
1217 * Use ZSTD_CCtx_refPrefix_advanced() to alter dictMode. */
1218ZSTDLIB_API size_t ZSTD_CCtx_refPrefix(ZSTD_CCtx* cctx,
1219 const void* prefix, size_t prefixSize);
1220ZSTDLIB_API size_t ZSTD_CCtx_refPrefix_advanced(ZSTD_CCtx* cctx,
1221 const void* prefix, size_t prefixSize,
1222 ZSTD_dictContentType_e dictContentType);
1223
1224/*! ZSTD_CCtx_reset() :
1225 * Return a CCtx to clean state.
1226 * Useful after an error, or to interrupt an ongoing compression job and start a new one.
1227 * Any internal data not yet flushed is cancelled.
1228 * The parameters and dictionary are kept unchanged, to reset them use ZSTD_CCtx_resetParameters().
1229 */
1230ZSTDLIB_API void ZSTD_CCtx_reset(ZSTD_CCtx* cctx);
1231
1232/*! ZSTD_CCtx_resetParameters() :
1233 * All parameters are back to default values (compression level is ZSTD_CLEVEL_DEFAULT).
1234 * Dictionary (if any) is dropped.
1235 * Resetting parameters is only possible during frame initialization (before starting compression).
1236 * To reset the context use ZSTD_CCtx_reset().
1237 * @return 0 or an error code (which can be checked with ZSTD_isError()).
1238 */
1239ZSTDLIB_API size_t ZSTD_CCtx_resetParameters(ZSTD_CCtx* cctx);
1240
1241
1242
1243typedef enum {
1244 ZSTD_e_continue=0, /* collect more data, encoder decides when to output compressed result, for optimal compression ratio */
1245 ZSTD_e_flush, /* flush any data provided so far,
1246 * it creates (at least) one new block, that can be decoded immediately on reception;
1247 * frame will continue: any future data can still reference previously compressed data, improving compression. */
1248 ZSTD_e_end /* flush any remaining data and close current frame.
1249 * any additional data starts a new frame.
1250 * each frame is independent (does not reference any content from previous frame). */
1251} ZSTD_EndDirective;
1252
1253/*! ZSTD_compress_generic() :
1254 * Behave about the same as ZSTD_compressStream. To note :
1255 * - Compression parameters are pushed into CCtx before starting compression, using ZSTD_CCtx_setParameter()
1256 * - Compression parameters cannot be changed once compression is started.
1257 * - outpot->pos must be <= dstCapacity, input->pos must be <= srcSize
1258 * - outpot->pos and input->pos will be updated. They are guaranteed to remain below their respective limit.
1259 * - In single-thread mode (default), function is blocking : it completed its job before returning to caller.
1260 * - In multi-thread mode, function is non-blocking : it just acquires a copy of input, and distribute job to internal worker threads,
1261 * and then immediately returns, just indicating that there is some data remaining to be flushed.
1262 * The function nonetheless guarantees forward progress : it will return only after it reads or write at least 1+ byte.
1263 * - Exception : in multi-threading mode, if the first call requests a ZSTD_e_end directive, it is blocking : it will complete compression before giving back control to caller.
1264 * - @return provides a minimum amount of data remaining to be flushed from internal buffers
1265 * or an error code, which can be tested using ZSTD_isError().
1266 * if @return != 0, flush is not fully completed, there is still some data left within internal buffers.
1267 * This is useful for ZSTD_e_flush, since in this case more flushes are necessary to empty all buffers.
1268 * For ZSTD_e_end, @return == 0 when internal buffers are fully flushed and frame is completed.
1269 * - after a ZSTD_e_end directive, if internal buffer is not fully flushed (@return != 0),
1270 * only ZSTD_e_end or ZSTD_e_flush operations are allowed.
1271 * Before starting a new compression job, or changing compression parameters,
1272 * it is required to fully flush internal buffers.
1273 */
1274ZSTDLIB_API size_t ZSTD_compress_generic (ZSTD_CCtx* cctx,
1275 ZSTD_outBuffer* output,
1276 ZSTD_inBuffer* input,
1277 ZSTD_EndDirective endOp);
1278
1279
1280/*! ZSTD_compress_generic_simpleArgs() :
1281 * Same as ZSTD_compress_generic(),
1282 * but using only integral types as arguments.
1283 * Argument list is larger than ZSTD_{in,out}Buffer,
1284 * but can be helpful for binders from dynamic languages
1285 * which have troubles handling structures containing memory pointers.
1286 */
1287ZSTDLIB_API size_t ZSTD_compress_generic_simpleArgs (
1288 ZSTD_CCtx* cctx,
1289 void* dst, size_t dstCapacity, size_t* dstPos,
1290 const void* src, size_t srcSize, size_t* srcPos,
1291 ZSTD_EndDirective endOp);
1292
1293
1294/*! ZSTD_CCtx_params :
1295 * Quick howto :
1296 * - ZSTD_createCCtxParams() : Create a ZSTD_CCtx_params structure
1297 * - ZSTD_CCtxParam_setParameter() : Push parameters one by one into
1298 * an existing ZSTD_CCtx_params structure.
1299 * This is similar to
1300 * ZSTD_CCtx_setParameter().
1301 * - ZSTD_CCtx_setParametersUsingCCtxParams() : Apply parameters to
1302 * an existing CCtx.
1303 * These parameters will be applied to
1304 * all subsequent compression jobs.
1305 * - ZSTD_compress_generic() : Do compression using the CCtx.
1306 * - ZSTD_freeCCtxParams() : Free the memory.
1307 *
1308 * This can be used with ZSTD_estimateCCtxSize_advanced_usingCCtxParams()
1309 * for static allocation for single-threaded compression.
1310 */
1311ZSTDLIB_API ZSTD_CCtx_params* ZSTD_createCCtxParams(void);
1312ZSTDLIB_API size_t ZSTD_freeCCtxParams(ZSTD_CCtx_params* params);
1313
1314
1315/*! ZSTD_CCtxParams_reset() :
1316 * Reset params to default values.
1317 */
1318ZSTDLIB_API size_t ZSTD_CCtxParams_reset(ZSTD_CCtx_params* params);
1319
1320/*! ZSTD_CCtxParams_init() :
1321 * Initializes the compression parameters of cctxParams according to
1322 * compression level. All other parameters are reset to their default values.
1323 */
1324ZSTDLIB_API size_t ZSTD_CCtxParams_init(ZSTD_CCtx_params* cctxParams, int compressionLevel);
1325
1326/*! ZSTD_CCtxParams_init_advanced() :
1327 * Initializes the compression and frame parameters of cctxParams according to
1328 * params. All other parameters are reset to their default values.
1329 */
1330ZSTDLIB_API size_t ZSTD_CCtxParams_init_advanced(ZSTD_CCtx_params* cctxParams, ZSTD_parameters params);
1331
1332
1333/*! ZSTD_CCtxParam_setParameter() :
1334 * Similar to ZSTD_CCtx_setParameter.
1335 * Set one compression parameter, selected by enum ZSTD_cParameter.
1336 * Parameters must be applied to a ZSTD_CCtx using ZSTD_CCtx_setParametersUsingCCtxParams().
1337 * Note : when `value` is an enum, cast it to unsigned for proper type checking.
1338 * @result : 0, or an error code (which can be tested with ZSTD_isError()).
1339 */
1340ZSTDLIB_API size_t ZSTD_CCtxParam_setParameter(ZSTD_CCtx_params* params, ZSTD_cParameter param, unsigned value);
1341
1342/*! ZSTD_CCtxParam_getParameter() :
1343 * Similar to ZSTD_CCtx_getParameter.
1344 * Get the requested value of one compression parameter, selected by enum ZSTD_cParameter.
1345 * @result : 0, or an error code (which can be tested with ZSTD_isError()).
1346 */
1347ZSTDLIB_API size_t ZSTD_CCtxParam_getParameter(ZSTD_CCtx_params* params, ZSTD_cParameter param, unsigned* value);
1348
1349/*! ZSTD_CCtx_setParametersUsingCCtxParams() :
1350 * Apply a set of ZSTD_CCtx_params to the compression context.
1351 * This can be done even after compression is started,
1352 * if nbWorkers==0, this will have no impact until a new compression is started.
1353 * if nbWorkers>=1, new parameters will be picked up at next job,
1354 * with a few restrictions (windowLog, pledgedSrcSize, nbWorkers, jobSize, and overlapLog are not updated).
1355 */
1356ZSTDLIB_API size_t ZSTD_CCtx_setParametersUsingCCtxParams(
1357 ZSTD_CCtx* cctx, const ZSTD_CCtx_params* params);
1358
1359
1360/* ==================================== */
1361/*=== Advanced decompression API ===*/
1362/* ==================================== */
1363
1364/* The following API works the same way as the advanced compression API :
1365 * a context is created, parameters are pushed into it one by one,
1366 * then the context can be used to decompress data using an interface similar to the straming API.
1367 */
1368
1369/*! ZSTD_DCtx_loadDictionary() :
1370 * Create an internal DDict from dict buffer,
1371 * to be used to decompress next frames.
1372 * @result : 0, or an error code (which can be tested with ZSTD_isError()).
1373 * Special : Adding a NULL (or 0-size) dictionary invalidates any previous dictionary,
1374 * meaning "return to no-dictionary mode".
1375 * Note 1 : `dict` content will be copied internally.
1376 * Use ZSTD_DCtx_loadDictionary_byReference()
1377 * to reference dictionary content instead.
1378 * In which case, the dictionary buffer must outlive its users.
1379 * Note 2 : Loading a dictionary involves building tables,
1380 * which has a non-negligible impact on CPU usage and latency.
1381 * Note 3 : Use ZSTD_DCtx_loadDictionary_advanced() to select
1382 * how dictionary content will be interpreted and loaded.
1383 */
1384ZSTDLIB_API size_t ZSTD_DCtx_loadDictionary(ZSTD_DCtx* dctx, const void* dict, size_t dictSize);
1385ZSTDLIB_API size_t ZSTD_DCtx_loadDictionary_byReference(ZSTD_DCtx* dctx, const void* dict, size_t dictSize);
1386ZSTDLIB_API size_t ZSTD_DCtx_loadDictionary_advanced(ZSTD_DCtx* dctx, const void* dict, size_t dictSize, ZSTD_dictLoadMethod_e dictLoadMethod, ZSTD_dictContentType_e dictContentType);
1387
1388
1389/*! ZSTD_DCtx_refDDict() :
1390 * Reference a prepared dictionary, to be used to decompress next frames.
1391 * The dictionary remains active for decompression of future frames using same DCtx.
1392 * @result : 0, or an error code (which can be tested with ZSTD_isError()).
1393 * Note 1 : Currently, only one dictionary can be managed.
1394 * Referencing a new dictionary effectively "discards" any previous one.
1395 * Special : adding a NULL DDict means "return to no-dictionary mode".
1396 * Note 2 : DDict is just referenced, its lifetime must outlive its usage from DCtx.
1397 */
1398ZSTDLIB_API size_t ZSTD_DCtx_refDDict(ZSTD_DCtx* dctx, const ZSTD_DDict* ddict);
1399
1400
1401/*! ZSTD_DCtx_refPrefix() :
1402 * Reference a prefix (single-usage dictionary) for next compression job.
1403 * This is the reverse operation of ZSTD_CCtx_refPrefix(),
1404 * and must use the same prefix as the one used during compression.
1405 * Prefix is **only used once**. Reference is discarded at end of frame.
1406 * End of frame is reached when ZSTD_DCtx_decompress_generic() returns 0.
1407 * @result : 0, or an error code (which can be tested with ZSTD_isError()).
1408 * Note 1 : Adding any prefix (including NULL) invalidates any previously set prefix or dictionary
1409 * Note 2 : Prefix buffer is referenced. It **must** outlive decompression job.
1410 * Prefix buffer must remain unmodified up to the end of frame,
1411 * reached when ZSTD_DCtx_decompress_generic() returns 0.
1412 * Note 3 : By default, the prefix is treated as raw content (ZSTD_dm_rawContent).
1413 * Use ZSTD_CCtx_refPrefix_advanced() to alter dictMode.
1414 * Note 4 : Referencing a raw content prefix has almost no cpu nor memory cost.
1415 * A fulldict prefix is more costly though.
1416 */
1417ZSTDLIB_API size_t ZSTD_DCtx_refPrefix(ZSTD_DCtx* dctx,
1418 const void* prefix, size_t prefixSize);
1419ZSTDLIB_API size_t ZSTD_DCtx_refPrefix_advanced(ZSTD_DCtx* dctx,
1420 const void* prefix, size_t prefixSize,
1421 ZSTD_dictContentType_e dictContentType);
1422
1423
1424/*! ZSTD_DCtx_setMaxWindowSize() :
1425 * Refuses allocating internal buffers for frames requiring a window size larger than provided limit.
1426 * This is useful to prevent a decoder context from reserving too much memory for itself (potential attack scenario).
1427 * This parameter is only useful in streaming mode, since no internal buffer is allocated in direct mode.
1428 * By default, a decompression context accepts all window sizes <= (1 << ZSTD_WINDOWLOG_MAX)
1429 * @return : 0, or an error code (which can be tested using ZSTD_isError()).
1430 */
1431ZSTDLIB_API size_t ZSTD_DCtx_setMaxWindowSize(ZSTD_DCtx* dctx, size_t maxWindowSize);
1432
1433
1434/*! ZSTD_DCtx_setFormat() :
1435 * Instruct the decoder context about what kind of data to decode next.
1436 * This instruction is mandatory to decode data without a fully-formed header,
1437 * such ZSTD_f_zstd1_magicless for example.
1438 * @return : 0, or an error code (which can be tested using ZSTD_isError()).
1439 */
1440ZSTDLIB_API size_t ZSTD_DCtx_setFormat(ZSTD_DCtx* dctx, ZSTD_format_e format);
1441
1442
1443/*! ZSTD_getFrameHeader_advanced() :
1444 * same as ZSTD_getFrameHeader(),
1445 * with added capability to select a format (like ZSTD_f_zstd1_magicless) */
1446ZSTDLIB_API size_t ZSTD_getFrameHeader_advanced(ZSTD_frameHeader* zfhPtr,
1447 const void* src, size_t srcSize, ZSTD_format_e format);
1448
1449
1450/*! ZSTD_decompress_generic() :
1451 * Behave the same as ZSTD_decompressStream.
1452 * Decompression parameters cannot be changed once decompression is started.
1453 * @return : an error code, which can be tested using ZSTD_isError()
1454 * if >0, a hint, nb of expected input bytes for next invocation.
1455 * `0` means : a frame has just been fully decoded and flushed.
1456 */
1457ZSTDLIB_API size_t ZSTD_decompress_generic(ZSTD_DCtx* dctx,
1458 ZSTD_outBuffer* output,
1459 ZSTD_inBuffer* input);
1460
1461
1462/*! ZSTD_decompress_generic_simpleArgs() :
1463 * Same as ZSTD_decompress_generic(),
1464 * but using only integral types as arguments.
1465 * Argument list is larger than ZSTD_{in,out}Buffer,
1466 * but can be helpful for binders from dynamic languages
1467 * which have troubles handling structures containing memory pointers.
1468 */
1469ZSTDLIB_API size_t ZSTD_decompress_generic_simpleArgs (
1470 ZSTD_DCtx* dctx,
1471 void* dst, size_t dstCapacity, size_t* dstPos,
1472 const void* src, size_t srcSize, size_t* srcPos);
1473
1474
1475/*! ZSTD_DCtx_reset() :
1476 * Return a DCtx to clean state.
1477 * If a decompression was ongoing, any internal data not yet flushed is cancelled.
1478 * All parameters are back to default values, including sticky ones.
1479 * Dictionary (if any) is dropped.
1480 * Parameters can be modified again after a reset.
1481 */
1482ZSTDLIB_API void ZSTD_DCtx_reset(ZSTD_DCtx* dctx);
1483
1484
1485
1486/* ============================ */
1487/** Block level API */
1488/* ============================ */
1489
1490/*!
1491 Block functions produce and decode raw zstd blocks, without frame metadata.
1492 Frame metadata cost is typically ~18 bytes, which can be non-negligible for very small blocks (< 100 bytes).
1493 User will have to take in charge required information to regenerate data, such as compressed and content sizes.
1494
1495 A few rules to respect :
1496 - Compressing and decompressing require a context structure
1497 + Use ZSTD_createCCtx() and ZSTD_createDCtx()
1498 - It is necessary to init context before starting
1499 + compression : any ZSTD_compressBegin*() variant, including with dictionary
1500 + decompression : any ZSTD_decompressBegin*() variant, including with dictionary
1501 + copyCCtx() and copyDCtx() can be used too
1502 - Block size is limited, it must be <= ZSTD_getBlockSize() <= ZSTD_BLOCKSIZE_MAX == 128 KB
1503 + If input is larger than a block size, it's necessary to split input data into multiple blocks
1504 + For inputs larger than a single block size, consider using the regular ZSTD_compress() instead.
1505 Frame metadata is not that costly, and quickly becomes negligible as source size grows larger.
1506 - When a block is considered not compressible enough, ZSTD_compressBlock() result will be zero.
1507 In which case, nothing is produced into `dst`.
1508 + User must test for such outcome and deal directly with uncompressed data
1509 + ZSTD_decompressBlock() doesn't accept uncompressed data as input !!!
1510 + In case of multiple successive blocks, should some of them be uncompressed,
1511 decoder must be informed of their existence in order to follow proper history.
1512 Use ZSTD_insertBlock() for such a case.
1513*/
1514
1515/*===== Raw zstd block functions =====*/
1516ZSTDLIB_API size_t ZSTD_getBlockSize (const ZSTD_CCtx* cctx);
1517ZSTDLIB_API size_t ZSTD_compressBlock (ZSTD_CCtx* cctx, void* dst, size_t dstCapacity, const void* src, size_t srcSize);
1518ZSTDLIB_API size_t ZSTD_decompressBlock(ZSTD_DCtx* dctx, void* dst, size_t dstCapacity, const void* src, size_t srcSize);
1519ZSTDLIB_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. */
1520
1521
1522#endif /* ZSTD_H_ZSTD_STATIC_LINKING_ONLY */
1523
1524#if defined (__cplusplus)
1525}
1526#endif
1527