blob: 30972cd0bafc21ada40a2305fc6503e585bfb3b6 [file] [log] [blame]
inikep82057aa2016-10-06 13:23:52 +02001/*
Yann Collet4ded9e52016-08-30 10:04:33 -07002 * Copyright (c) 2016-present, Yann Collet, Facebook, Inc.
3 * All rights reserved.
4 *
5 * This source code is licensed under the BSD-style license found in the
6 * LICENSE file in the root directory of this source tree. An additional grant
7 * of patent rights can be found in the PATENTS file in the same directory.
8 */
Yann Collet4856a002015-01-24 01:58:16 +01009
Yann Collet4856a002015-01-24 01:58:16 +010010#if defined (__cplusplus)
11extern "C" {
12#endif
13
Nick Terrell05c00f22016-11-29 11:46:37 -080014#ifndef ZSTD_H_235446
15#define ZSTD_H_235446
16
inikep82057aa2016-10-06 13:23:52 +020017/* ====== Dependency ======*/
Yann Collet4856a002015-01-24 01:58:16 +010018#include <stddef.h> /* size_t */
19
20
Yann Collet426a9d42016-12-07 16:39:34 -080021/* ===== ZSTDLIB_API : control library symbols visibility ===== */
Nick Terrella1280402017-05-19 18:01:59 -070022#ifndef ZSTDLIB_VISIBILITY
23# if defined(__GNUC__) && (__GNUC__ >= 4)
24# define ZSTDLIB_VISIBILITY __attribute__ ((visibility ("default")))
25# else
26# define ZSTDLIB_VISIBILITY
27# endif
Nick Terrell8de46ab2016-12-16 13:27:30 -080028#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
Christophe Chevalierc6e84532015-12-07 17:44:09 +010035#endif
36
37
inikep2d261332016-10-06 16:28:21 +020038/*******************************************************************************************************
39 Introduction
40
Yann Colleta5ffe3d2017-05-12 16:29:19 -070041 zstd, short for Zstandard, is a fast lossless compression algorithm,
42 targeting real-time compression scenarios at zlib-level and better compression ratios.
43 The zstd compression library provides in-memory compression and decompression functions.
44 The library supports compression levels from 1 up to ZSTD_maxCLevel() which is currently 22.
Anders Oleson517577b2017-02-20 12:08:59 -080045 Levels >= 20, labeled `--ultra`, should be used with caution, as they require more memory.
inikep2d261332016-10-06 16:28:21 +020046 Compression can be done in:
47 - a single step (described as Simple API)
48 - a single step, reusing a context (described as Explicit memory management)
Yann Collet37d13002016-10-24 17:22:12 -070049 - unbounded multiple steps (described as Streaming compression)
Yann Colletfe234bf2017-06-19 15:23:19 -070050 The compression ratio achievable on small data can be highly improved using a dictionary in:
inikep2d261332016-10-06 16:28:21 +020051 - a single step (described as Simple dictionary API)
Przemyslaw Skibinski984b66c2016-10-24 15:59:51 +020052 - a single step, reusing a dictionary (described as Fast dictionary API)
inikep2d261332016-10-06 16:28:21 +020053
Yann Collet37d13002016-10-24 17:22:12 -070054 Advanced experimental functions can be accessed using #define ZSTD_STATIC_LINKING_ONLY before including zstd.h.
Yann Colletfe234bf2017-06-19 15:23:19 -070055 Advanced experimental APIs shall never be used with a dynamic library.
inikep2d261332016-10-06 16:28:21 +020056 They are not "stable", their definition may change in the future. Only static linking is allowed.
57*********************************************************************************************************/
58
59/*------ Version ------*/
Yann Collet901e85f2016-08-31 07:51:25 -070060#define ZSTD_VERSION_MAJOR 1
Yann Collet51652522017-05-08 17:52:46 -070061#define ZSTD_VERSION_MINOR 3
Yann Collet8b669532017-04-27 12:50:20 -070062#define ZSTD_VERSION_RELEASE 0
Yann Collete02808f2016-04-20 22:46:16 +020063
Yann Colletbfff8992017-05-16 16:12:23 -070064#define ZSTD_VERSION_NUMBER (ZSTD_VERSION_MAJOR *100*100 + ZSTD_VERSION_MINOR *100 + ZSTD_VERSION_RELEASE)
Yann Colletfe234bf2017-06-19 15:23:19 -070065ZSTDLIB_API unsigned ZSTD_versionNumber(void); /**< useful to check dll version */
Yann Colletbfff8992017-05-16 16:12:23 -070066
Yann Collete02808f2016-04-20 22:46:16 +020067#define ZSTD_LIB_VERSION ZSTD_VERSION_MAJOR.ZSTD_VERSION_MINOR.ZSTD_VERSION_RELEASE
68#define ZSTD_QUOTE(str) #str
69#define ZSTD_EXPAND_AND_QUOTE(str) ZSTD_QUOTE(str)
70#define ZSTD_VERSION_STRING ZSTD_EXPAND_AND_QUOTE(ZSTD_LIB_VERSION)
Yann Colletfe234bf2017-06-19 15:23:19 -070071ZSTDLIB_API const char* ZSTD_versionString(void); /* >= v1.3.0 */
Yann Collet4856a002015-01-24 01:58:16 +010072
73
inikep82057aa2016-10-06 13:23:52 +020074/***************************************
Yann Colletcf05b9d2016-07-18 16:52:10 +020075* Simple API
Yann Collet7010c272015-10-21 09:07:25 +010076***************************************/
Yann Collet953ce722016-02-04 15:28:14 +010077/*! ZSTD_compress() :
Yann Collet71ddeb62017-04-20 22:54:54 -070078 * Compresses `src` content as a single zstd compressed frame into already allocated `dst`.
79 * Hint : compression runs faster if `dstCapacity` >= `ZSTD_compressBound(srcSize)`.
80 * @return : compressed size written into `dst` (<= `dstCapacity),
81 * or an error code if it fails (which can be tested using ZSTD_isError()). */
Yann Colletcf05b9d2016-07-18 16:52:10 +020082ZSTDLIB_API size_t ZSTD_compress( void* dst, size_t dstCapacity,
83 const void* src, size_t srcSize,
84 int compressionLevel);
Yann Collet4856a002015-01-24 01:58:16 +010085
Yann Collet953ce722016-02-04 15:28:14 +010086/*! ZSTD_decompress() :
Yann Collet71ddeb62017-04-20 22:54:54 -070087 * `compressedSize` : must be the _exact_ size of some number of compressed and/or skippable frames.
Yann Colletfe234bf2017-06-19 15:23:19 -070088 * `dstCapacity` is an upper bound of originalSize to regenerate.
Yann Collet71ddeb62017-04-20 22:54:54 -070089 * If user cannot imply a maximum upper bound, it's better to use streaming mode to decompress data.
90 * @return : the number of bytes decompressed into `dst` (<= `dstCapacity`),
91 * or an errorCode if it fails (which can be tested using ZSTD_isError()). */
Yann Collet7d968c72016-02-03 02:11:32 +010092ZSTDLIB_API size_t ZSTD_decompress( void* dst, size_t dstCapacity,
Christophe Chevalierc6e84532015-12-07 17:44:09 +010093 const void* src, size_t compressedSize);
Yann Collet4856a002015-01-24 01:58:16 +010094
Yann Colletac175d42016-09-13 00:51:47 +020095/*! ZSTD_getDecompressedSize() :
Yann Colleta5ffe3d2017-05-12 16:29:19 -070096 * NOTE: This function is planned to be obsolete, in favor of ZSTD_getFrameContentSize().
97 * ZSTD_getFrameContentSize() works the same way,
98 * returning the decompressed size of a single frame,
99 * but distinguishes empty frames from frames with an unknown size, or errors.
Yann Collet71ddeb62017-04-20 22:54:54 -0700100 *
101 * 'src' is the start of a zstd compressed frame.
102 * @return : content size to be decompressed, as a 64-bits value _if known_, 0 otherwise.
Yann Colleta5ffe3d2017-05-12 16:29:19 -0700103 * note 1 : decompressed size is an optional field, it may not be present, typically in streaming mode.
Yann Collet71ddeb62017-04-20 22:54:54 -0700104 * When `return==0`, data to decompress could be any size.
105 * In which case, it's necessary to use streaming mode to decompress data.
Yann Colleta5ffe3d2017-05-12 16:29:19 -0700106 * Optionally, application can use ZSTD_decompress() while relying on implied limits.
Yann Collet71ddeb62017-04-20 22:54:54 -0700107 * (For example, data may be necessarily cut into blocks <= 16 KB).
108 * note 2 : decompressed size is always present when compression is done with ZSTD_compress()
109 * note 3 : decompressed size can be very large (64-bits value),
110 * potentially larger than what local system can handle as a single memory segment.
111 * In which case, it's necessary to use streaming mode to decompress data.
112 * note 4 : If source is untrusted, decompressed size could be wrong or intentionally modified.
113 * Always ensure result fits within application's authorized limits.
114 * Each application can set its own limits.
Yann Collet7028cbd2017-05-25 18:29:08 -0700115 * note 5 : when `return==0`, if precise failure cause is needed, use ZSTD_getFrameHeader() to know more. */
Yann Colletac175d42016-09-13 00:51:47 +0200116ZSTDLIB_API unsigned long long ZSTD_getDecompressedSize(const void* src, size_t srcSize);
117
Yann Collet41105342016-07-27 15:09:11 +0200118
Yann Colletcf05b9d2016-07-18 16:52:10 +0200119/*====== Helper functions ======*/
Yann Collet41105342016-07-27 15:09:11 +0200120ZSTDLIB_API int ZSTD_maxCLevel(void); /*!< maximum compression level available */
121ZSTDLIB_API size_t ZSTD_compressBound(size_t srcSize); /*!< maximum compressed size in worst case scenario */
Yann Collet953ce722016-02-04 15:28:14 +0100122ZSTDLIB_API unsigned ZSTD_isError(size_t code); /*!< tells if a `size_t` function result is an error code */
Yann Colletcf05b9d2016-07-18 16:52:10 +0200123ZSTDLIB_API const char* ZSTD_getErrorName(size_t code); /*!< provides readable string from an error code */
Yann Collet7010c272015-10-21 09:07:25 +0100124
125
inikep82057aa2016-10-06 13:23:52 +0200126/***************************************
Yann Collet7d968c72016-02-03 02:11:32 +0100127* Explicit memory management
Yann Collet7010c272015-10-21 09:07:25 +0100128***************************************/
Yann Collet37d13002016-10-24 17:22:12 -0700129/*= Compression context
Yann Collet71ddeb62017-04-20 22:54:54 -0700130 * When compressing many times,
131 * it is recommended to allocate a context just once, and re-use it for each successive compression operation.
132 * This will make workload friendlier for system's memory.
133 * Use one context per thread for parallel execution in multi-threaded environments. */
Yann Collet87c18b22016-08-26 01:43:47 +0200134typedef struct ZSTD_CCtx_s ZSTD_CCtx;
Christophe Chevalierc6e84532015-12-07 17:44:09 +0100135ZSTDLIB_API ZSTD_CCtx* ZSTD_createCCtx(void);
Yann Colletd469a982016-07-28 03:47:45 +0200136ZSTDLIB_API size_t ZSTD_freeCCtx(ZSTD_CCtx* cctx);
Yann Collet7010c272015-10-21 09:07:25 +0100137
inikep82057aa2016-10-06 13:23:52 +0200138/*! ZSTD_compressCCtx() :
Yann Collet71ddeb62017-04-20 22:54:54 -0700139 * Same as ZSTD_compress(), requires an allocated ZSTD_CCtx (see ZSTD_createCCtx()). */
Yann Colleta5ffe3d2017-05-12 16:29:19 -0700140ZSTDLIB_API size_t ZSTD_compressCCtx(ZSTD_CCtx* ctx,
141 void* dst, size_t dstCapacity,
142 const void* src, size_t srcSize,
143 int compressionLevel);
Yann Collet4856a002015-01-24 01:58:16 +0100144
Sean Purcelldec2b962017-03-14 11:24:09 -0700145/*= Decompression context
Yann Collet71ddeb62017-04-20 22:54:54 -0700146 * When decompressing many times,
Yann Colleta5ffe3d2017-05-12 16:29:19 -0700147 * it is recommended to allocate a context only once,
148 * and re-use it for each successive compression operation.
Yann Collet71ddeb62017-04-20 22:54:54 -0700149 * This will make workload friendlier for system's memory.
Yann Colleta5ffe3d2017-05-12 16:29:19 -0700150 * Use one context per thread for parallel execution. */
Yann Collet87c18b22016-08-26 01:43:47 +0200151typedef struct ZSTD_DCtx_s ZSTD_DCtx;
Yann Colletecd651b2016-01-07 15:35:18 +0100152ZSTDLIB_API ZSTD_DCtx* ZSTD_createDCtx(void);
Yann Colletd469a982016-07-28 03:47:45 +0200153ZSTDLIB_API size_t ZSTD_freeDCtx(ZSTD_DCtx* dctx);
Yann Colletecd651b2016-01-07 15:35:18 +0100154
inikep82057aa2016-10-06 13:23:52 +0200155/*! ZSTD_decompressDCtx() :
Yann Colleta5ffe3d2017-05-12 16:29:19 -0700156 * Same as ZSTD_decompress(), requires an allocated ZSTD_DCtx (see ZSTD_createDCtx()) */
157ZSTDLIB_API size_t ZSTD_decompressDCtx(ZSTD_DCtx* ctx,
158 void* dst, size_t dstCapacity,
159 const void* src, size_t srcSize);
Yann Collet7d968c72016-02-03 02:11:32 +0100160
161
inikep82057aa2016-10-06 13:23:52 +0200162/**************************
Yann Collet302fb532016-06-07 12:16:49 +0200163* Simple dictionary API
164***************************/
Yann Collet953ce722016-02-04 15:28:14 +0100165/*! ZSTD_compress_usingDict() :
Yann Colletfe234bf2017-06-19 15:23:19 -0700166 * Compression using a predefined Dictionary (see dictBuilder/zdict.h).
167 * Note : This function loads the dictionary, resulting in significant startup delay.
168 * Note : When `dict == NULL || dictSize < 8` no dictionary is used. */
Yann Collet7d968c72016-02-03 02:11:32 +0100169ZSTDLIB_API size_t ZSTD_compress_usingDict(ZSTD_CCtx* ctx,
170 void* dst, size_t dstCapacity,
171 const void* src, size_t srcSize,
172 const void* dict,size_t dictSize,
173 int compressionLevel);
174
Yann Collet953ce722016-02-04 15:28:14 +0100175/*! ZSTD_decompress_usingDict() :
Yann Colletfe234bf2017-06-19 15:23:19 -0700176 * Decompression using a predefined Dictionary (see dictBuilder/zdict.h).
177 * Dictionary must be identical to the one used during compression.
178 * Note : This function loads the dictionary, resulting in significant startup delay.
179 * Note : When `dict == NULL || dictSize < 8` no dictionary is used. */
Yann Collet7d968c72016-02-03 02:11:32 +0100180ZSTDLIB_API size_t ZSTD_decompress_usingDict(ZSTD_DCtx* dctx,
181 void* dst, size_t dstCapacity,
182 const void* src, size_t srcSize,
183 const void* dict,size_t dictSize);
Yann Colletecd651b2016-01-07 15:35:18 +0100184
Yann Collet4856a002015-01-24 01:58:16 +0100185
Yann Colletfe234bf2017-06-19 15:23:19 -0700186/**********************************
187 * Bulk processing dictionary API
188 *********************************/
inikep2d261332016-10-06 16:28:21 +0200189typedef struct ZSTD_CDict_s ZSTD_CDict;
190
Yann Collet302fb532016-06-07 12:16:49 +0200191/*! ZSTD_createCDict() :
Yann Colletfe234bf2017-06-19 15:23:19 -0700192 * When compressing multiple messages / blocks with the same dictionary, it's recommended to load it just once.
193 * ZSTD_createCDict() will create a digested dictionary, ready to start future compression operations without startup delay.
194 * ZSTD_CDict can be created once and shared by multiple threads concurrently, since its usage is read-only.
195 * `dictBuffer` can be released after ZSTD_CDict creation, since its content is copied within CDict */
Yann Colleta5ffe3d2017-05-12 16:29:19 -0700196ZSTDLIB_API ZSTD_CDict* ZSTD_createCDict(const void* dictBuffer, size_t dictSize,
197 int compressionLevel);
inikep2d261332016-10-06 16:28:21 +0200198
199/*! ZSTD_freeCDict() :
Yann Colletfe234bf2017-06-19 15:23:19 -0700200 * Function frees memory allocated by ZSTD_createCDict(). */
Yann Collet302fb532016-06-07 12:16:49 +0200201ZSTDLIB_API size_t ZSTD_freeCDict(ZSTD_CDict* CDict);
202
203/*! ZSTD_compress_usingCDict() :
Yann Collet4f818182017-04-17 17:57:35 -0700204 * Compression using a digested Dictionary.
205 * Faster startup than ZSTD_compress_usingDict(), recommended when same dictionary is used multiple times.
206 * Note that compression level is decided during dictionary creation.
207 * Frame parameters are hardcoded (dictID=yes, contentSize=yes, checksum=no) */
Yann Collet302fb532016-06-07 12:16:49 +0200208ZSTDLIB_API size_t ZSTD_compress_usingCDict(ZSTD_CCtx* cctx,
209 void* dst, size_t dstCapacity,
210 const void* src, size_t srcSize,
211 const ZSTD_CDict* cdict);
212
inikep2d261332016-10-06 16:28:21 +0200213
214typedef struct ZSTD_DDict_s ZSTD_DDict;
215
Yann Collet302fb532016-06-07 12:16:49 +0200216/*! ZSTD_createDDict() :
Yann Colletfe234bf2017-06-19 15:23:19 -0700217 * Create a digested dictionary, ready to start decompression operation without startup delay.
218 * dictBuffer can be released after DDict creation, as its content is copied inside DDict */
Yann Collet4e5eea62016-12-21 16:44:35 +0100219ZSTDLIB_API ZSTD_DDict* ZSTD_createDDict(const void* dictBuffer, size_t dictSize);
inikep2d261332016-10-06 16:28:21 +0200220
221/*! ZSTD_freeDDict() :
Yann Colletfe234bf2017-06-19 15:23:19 -0700222 * Function frees memory allocated with ZSTD_createDDict() */
Yann Collet302fb532016-06-07 12:16:49 +0200223ZSTDLIB_API size_t ZSTD_freeDDict(ZSTD_DDict* ddict);
224
225/*! ZSTD_decompress_usingDDict() :
Yann Colletfe234bf2017-06-19 15:23:19 -0700226 * Decompression using a digested Dictionary.
227 * Faster startup than ZSTD_decompress_usingDict(), recommended when same dictionary is used multiple times. */
Yann Collet302fb532016-06-07 12:16:49 +0200228ZSTDLIB_API size_t ZSTD_decompress_usingDDict(ZSTD_DCtx* dctx,
229 void* dst, size_t dstCapacity,
230 const void* src, size_t srcSize,
231 const ZSTD_DDict* ddict);
232
233
inikep82057aa2016-10-06 13:23:52 +0200234/****************************
Yann Collet7be46bf2016-08-19 18:39:36 +0200235* Streaming
236****************************/
237
238typedef struct ZSTD_inBuffer_s {
239 const void* src; /**< start of input buffer */
240 size_t size; /**< size of input buffer */
241 size_t pos; /**< position where reading stopped. Will be updated. Necessarily 0 <= pos <= size */
242} ZSTD_inBuffer;
243
244typedef struct ZSTD_outBuffer_s {
245 void* dst; /**< start of output buffer */
246 size_t size; /**< size of output buffer */
247 size_t pos; /**< position where writing stopped. Will be updated. Necessarily 0 <= pos <= size */
248} ZSTD_outBuffer;
249
250
Yann Collet7be46bf2016-08-19 18:39:36 +0200251
inikepba1db372016-10-06 14:22:48 +0200252/*-***********************************************************************
Przemyslaw Skibinski984b66c2016-10-24 15:59:51 +0200253* Streaming compression - HowTo
Yann Collet7be46bf2016-08-19 18:39:36 +0200254*
255* A ZSTD_CStream object is required to track streaming operation.
256* Use ZSTD_createCStream() and ZSTD_freeCStream() to create/release resources.
257* ZSTD_CStream objects can be reused multiple times on consecutive compression operations.
Yann Collet37d13002016-10-24 17:22:12 -0700258* It is recommended to re-use ZSTD_CStream in situations where many streaming operations will be achieved consecutively,
259* since it will play nicer with system's memory, by re-using already allocated memory.
260* Use one separate ZSTD_CStream per thread for parallel execution.
Yann Collet7be46bf2016-08-19 18:39:36 +0200261*
Yann Collet37d13002016-10-24 17:22:12 -0700262* Start a new compression by initializing ZSTD_CStream.
Yann Collet7be46bf2016-08-19 18:39:36 +0200263* Use ZSTD_initCStream() to start a new compression operation.
Yann Collete795c8a2016-12-13 16:39:36 +0100264* Use ZSTD_initCStream_usingDict() or ZSTD_initCStream_usingCDict() for a compression which requires a dictionary (experimental section)
Yann Collet7be46bf2016-08-19 18:39:36 +0200265*
266* Use ZSTD_compressStream() repetitively to consume input stream.
Yann Colletfa72f6b2016-09-05 17:39:56 +0200267* The function will automatically update both `pos` fields.
Yann Collet7be46bf2016-08-19 18:39:36 +0200268* Note that it may not consume the entire input, in which case `pos < size`,
269* and it's up to the caller to present again remaining data.
Yann Collet4bf317d2016-08-28 07:43:34 -0700270* @return : a size hint, preferred nb of bytes to use as input for next function call
Yann Collet7be46bf2016-08-19 18:39:36 +0200271* or an error code, which can be tested using ZSTD_isError().
Yann Collete795c8a2016-12-13 16:39:36 +0100272* Note 1 : it's just a hint, to help latency a little, any other value will work fine.
273* Note 2 : size hint is guaranteed to be <= ZSTD_CStreamInSize()
Yann Collet7be46bf2016-08-19 18:39:36 +0200274*
Yann Collete795c8a2016-12-13 16:39:36 +0100275* At any moment, it's possible to flush whatever data remains within internal buffer, using ZSTD_flushStream().
Yann Collet7be46bf2016-08-19 18:39:36 +0200276* `output->pos` will be updated.
Yann Collete795c8a2016-12-13 16:39:36 +0100277* Note that some content might still be left within internal buffer if `output->size` is too small.
Yann Collet7be46bf2016-08-19 18:39:36 +0200278* @return : nb of bytes still present within internal buffer (0 if it's empty)
279* or an error code, which can be tested using ZSTD_isError().
280*
281* ZSTD_endStream() instructs to finish a frame.
282* It will perform a flush and write frame epilogue.
283* The epilogue is required for decoders to consider a frame completed.
Yann Collet009d6042017-05-19 10:17:59 -0700284* ZSTD_endStream() may not be able to flush full data if `output->size` is too small.
Yann Collet7be46bf2016-08-19 18:39:36 +0200285* In which case, call again ZSTD_endStream() to complete the flush.
Yann Collet009d6042017-05-19 10:17:59 -0700286* @return : 0 if frame fully completed and fully flushed,
287 or >0 if some data is still present within internal buffer
288 (value is minimum size estimation for remaining data to flush, but it could be more)
Yann Collet7be46bf2016-08-19 18:39:36 +0200289* or an error code, which can be tested using ZSTD_isError().
290*
291* *******************************************************************/
292
Yann Colletfe234bf2017-06-19 15:23:19 -0700293typedef ZSTD_CCtx ZSTD_CStream; /**< CCtx and CStream are now effectively same object (>= v1.3.0) */
294 /* But continue to distinguish them for compatibility with versions <= v1.2.0 */
Sean Purcelldec2b962017-03-14 11:24:09 -0700295/*===== ZSTD_CStream management functions =====*/
Yann Collet70e3b312016-08-23 01:18:06 +0200296ZSTDLIB_API ZSTD_CStream* ZSTD_createCStream(void);
297ZSTDLIB_API size_t ZSTD_freeCStream(ZSTD_CStream* zcs);
Yann Collete795c8a2016-12-13 16:39:36 +0100298
Sean Purcelldec2b962017-03-14 11:24:09 -0700299/*===== Streaming compression functions =====*/
Yann Collet70e3b312016-08-23 01:18:06 +0200300ZSTDLIB_API size_t ZSTD_initCStream(ZSTD_CStream* zcs, int compressionLevel);
301ZSTDLIB_API size_t ZSTD_compressStream(ZSTD_CStream* zcs, ZSTD_outBuffer* output, ZSTD_inBuffer* input);
302ZSTDLIB_API size_t ZSTD_flushStream(ZSTD_CStream* zcs, ZSTD_outBuffer* output);
303ZSTDLIB_API size_t ZSTD_endStream(ZSTD_CStream* zcs, ZSTD_outBuffer* output);
Yann Collet7be46bf2016-08-19 18:39:36 +0200304
inikep82057aa2016-10-06 13:23:52 +0200305ZSTDLIB_API size_t ZSTD_CStreamInSize(void); /**< recommended size for input buffer */
306ZSTDLIB_API size_t ZSTD_CStreamOutSize(void); /**< recommended size for output buffer. Guarantee to successfully flush at least one complete compressed block in all circumstances. */
Yann Collet7be46bf2016-08-19 18:39:36 +0200307
Yann Collet7be46bf2016-08-19 18:39:36 +0200308
inikep82057aa2016-10-06 13:23:52 +0200309
inikepba1db372016-10-06 14:22:48 +0200310/*-***************************************************************************
Przemyslaw Skibinski984b66c2016-10-24 15:59:51 +0200311* Streaming decompression - HowTo
Yann Collet7be46bf2016-08-19 18:39:36 +0200312*
313* A ZSTD_DStream object is required to track streaming operations.
314* Use ZSTD_createDStream() and ZSTD_freeDStream() to create/release resources.
Yann Collet17e482e2016-08-23 16:58:10 +0200315* ZSTD_DStream objects can be re-used multiple times.
Yann Collet7be46bf2016-08-19 18:39:36 +0200316*
317* Use ZSTD_initDStream() to start a new decompression operation,
318* or ZSTD_initDStream_usingDict() if decompression requires a dictionary.
Yann Collet7c83dfd2016-09-05 19:47:43 +0200319* @return : recommended first input size
Yann Collet7be46bf2016-08-19 18:39:36 +0200320*
321* Use ZSTD_decompressStream() repetitively to consume your input.
Yann Colletfa72f6b2016-09-05 17:39:56 +0200322* The function will update both `pos` fields.
Yann Colletb3060f72016-09-09 16:44:16 +0200323* If `input.pos < input.size`, some input has not been consumed.
Yann Collet1d4208c2016-09-06 05:16:40 +0200324* It's up to the caller to present again remaining data.
Yann Colletb3060f72016-09-09 16:44:16 +0200325* If `output.pos < output.size`, decoder has flushed everything it could.
Yann Collet7be46bf2016-08-19 18:39:36 +0200326* @return : 0 when a frame is completely decoded and fully flushed,
Yann Colletfa72f6b2016-09-05 17:39:56 +0200327* an error code, which can be tested using ZSTD_isError(),
Yann Collet9ffbeea2016-12-02 18:37:38 -0800328* any other value > 0, which means there is still some decoding to do to complete current frame.
329* The return value is a suggested next input size (a hint to improve latency) that will never load more than the current frame.
Yann Collet7be46bf2016-08-19 18:39:36 +0200330* *******************************************************************************/
331
Yann Colletfe234bf2017-06-19 15:23:19 -0700332typedef ZSTD_DCtx ZSTD_DStream; /**< DCtx and DStream are now effectively same object (>= v1.3.0) */
333 /* But continue to distinguish them for compatibility with versions <= v1.2.0 */
Sean Purcelldec2b962017-03-14 11:24:09 -0700334/*===== ZSTD_DStream management functions =====*/
Yann Collet70e3b312016-08-23 01:18:06 +0200335ZSTDLIB_API ZSTD_DStream* ZSTD_createDStream(void);
336ZSTDLIB_API size_t ZSTD_freeDStream(ZSTD_DStream* zds);
Yann Collete795c8a2016-12-13 16:39:36 +0100337
Sean Purcelldec2b962017-03-14 11:24:09 -0700338/*===== Streaming decompression functions =====*/
inikep82057aa2016-10-06 13:23:52 +0200339ZSTDLIB_API size_t ZSTD_initDStream(ZSTD_DStream* zds);
340ZSTDLIB_API size_t ZSTD_decompressStream(ZSTD_DStream* zds, ZSTD_outBuffer* output, ZSTD_inBuffer* input);
Yann Collet7be46bf2016-08-19 18:39:36 +0200341
Yann Collet70e3b312016-08-23 01:18:06 +0200342ZSTDLIB_API size_t ZSTD_DStreamInSize(void); /*!< recommended size for input buffer */
Yann Collet01c19922016-09-08 19:29:04 +0200343ZSTDLIB_API size_t ZSTD_DStreamOutSize(void); /*!< recommended size for output buffer. Guarantee to successfully flush at least one complete block in all circumstances. */
Yann Collet7be46bf2016-08-19 18:39:36 +0200344
Nick Terrell05c00f22016-11-29 11:46:37 -0800345#endif /* ZSTD_H_235446 */
Yann Collet7be46bf2016-08-19 18:39:36 +0200346
347
Yann Colletd3b7f8d2016-06-04 19:47:02 +0200348
inikep82057aa2016-10-06 13:23:52 +0200349/****************************************************************************************
inikep2d261332016-10-06 16:28:21 +0200350 * START OF ADVANCED AND EXPERIMENTAL FUNCTIONS
Yann Colletd3b7f8d2016-06-04 19:47:02 +0200351 * The definitions in this section are considered experimental.
Yann Colleta49e0662016-06-21 11:54:03 +0200352 * They should never be used with a dynamic library, as they may change in the future.
Yann Colletd3b7f8d2016-06-04 19:47:02 +0200353 * They are provided for advanced usages.
354 * Use them only in association with static linking.
inikep82057aa2016-10-06 13:23:52 +0200355 * ***************************************************************************************/
Yann Colletd3b7f8d2016-06-04 19:47:02 +0200356
Yann Collet25989e32017-05-25 15:07:37 -0700357#if defined(ZSTD_STATIC_LINKING_ONLY) && !defined(ZSTD_H_ZSTD_STATIC_LINKING_ONLY)
358#define ZSTD_H_ZSTD_STATIC_LINKING_ONLY
359
inikep82057aa2016-10-06 13:23:52 +0200360/* --- Constants ---*/
Yann Collet4e5eea62016-12-21 16:44:35 +0100361#define ZSTD_MAGICNUMBER 0xFD2FB528 /* >= v0.8.0 */
Yann Colletd3b7f8d2016-06-04 19:47:02 +0200362#define ZSTD_MAGIC_SKIPPABLE_START 0x184D2A50U
363
Sean Purcelld44703d2017-03-01 14:36:25 -0800364#define ZSTD_WINDOWLOG_MAX_32 27
Yann Colleted3845d2016-07-08 12:57:10 +0200365#define ZSTD_WINDOWLOG_MAX_64 27
Nick Terrelle65aab82017-03-08 15:40:13 -0800366#define ZSTD_WINDOWLOG_MAX ((unsigned)(sizeof(size_t) == 4 ? ZSTD_WINDOWLOG_MAX_32 : ZSTD_WINDOWLOG_MAX_64))
Yann Colletcf409a72016-09-26 16:41:05 +0200367#define ZSTD_WINDOWLOG_MIN 10
Yann Colletd3b7f8d2016-06-04 19:47:02 +0200368#define ZSTD_HASHLOG_MAX ZSTD_WINDOWLOG_MAX
Yann Colletcf409a72016-09-26 16:41:05 +0200369#define ZSTD_HASHLOG_MIN 6
370#define ZSTD_CHAINLOG_MAX (ZSTD_WINDOWLOG_MAX+1)
371#define ZSTD_CHAINLOG_MIN ZSTD_HASHLOG_MIN
Yann Colletd3b7f8d2016-06-04 19:47:02 +0200372#define ZSTD_HASHLOG3_MAX 17
Yann Colletd3b7f8d2016-06-04 19:47:02 +0200373#define ZSTD_SEARCHLOG_MAX (ZSTD_WINDOWLOG_MAX-1)
374#define ZSTD_SEARCHLOG_MIN 1
Yann Collet0e07bf32016-09-07 06:33:02 +0200375#define ZSTD_SEARCHLENGTH_MAX 7 /* only for ZSTD_fast, other strategies are limited to 6 */
376#define ZSTD_SEARCHLENGTH_MIN 3 /* only for ZSTD_btopt, other strategies are limited to 4 */
Yann Colletd3b7f8d2016-06-04 19:47:02 +0200377#define ZSTD_TARGETLENGTH_MIN 4
378#define ZSTD_TARGETLENGTH_MAX 999
379
Yann Collet673f0d72016-06-06 00:26:38 +0200380#define ZSTD_FRAMEHEADERSIZE_MAX 18 /* for static allocation */
Yann Colletba75e9d2016-12-21 19:57:18 +0100381#define ZSTD_FRAMEHEADERSIZE_MIN 6
Yann Collet7c83dfd2016-09-05 19:47:43 +0200382static const size_t ZSTD_frameHeaderSize_prefix = 5;
Yann Colletba75e9d2016-12-21 19:57:18 +0100383static const size_t ZSTD_frameHeaderSize_min = ZSTD_FRAMEHEADERSIZE_MIN;
Yann Collet673f0d72016-06-06 00:26:38 +0200384static const size_t ZSTD_frameHeaderSize_max = ZSTD_FRAMEHEADERSIZE_MAX;
385static const size_t ZSTD_skippableHeaderSize = 8; /* magic number + skippable frame length */
386
Yann Colletd3b7f8d2016-06-04 19:47:02 +0200387
Przemyslaw Skibinski984b66c2016-10-24 15:59:51 +0200388/*--- Advanced types ---*/
Yann Colleta5ffe3d2017-05-12 16:29:19 -0700389typedef enum { ZSTD_fast=1, ZSTD_dfast, ZSTD_greedy, ZSTD_lazy, ZSTD_lazy2,
390 ZSTD_btlazy2, ZSTD_btopt, ZSTD_btultra } ZSTD_strategy; /* from faster to stronger */
Yann Colletd3b7f8d2016-06-04 19:47:02 +0200391
392typedef struct {
Yann Collet655393c2016-08-14 00:16:20 +0200393 unsigned windowLog; /**< largest match distance : larger == more compression, more memory needed during decompression */
394 unsigned chainLog; /**< fully searched segment : larger == more compression, slower, more memory (useless for fast) */
395 unsigned hashLog; /**< dispatch table : larger == faster, more memory */
396 unsigned searchLog; /**< nb of searches : larger == more compression, slower */
397 unsigned searchLength; /**< match length searched : larger == faster decompression, sometimes less compression */
398 unsigned targetLength; /**< acceptable match size for optimal parser (only) : larger == more compression, slower */
Yann Colletd3b7f8d2016-06-04 19:47:02 +0200399 ZSTD_strategy strategy;
400} ZSTD_compressionParameters;
401
402typedef struct {
Yann Collet0ec6a952017-01-02 00:49:42 +0100403 unsigned contentSizeFlag; /**< 1: content size will be in frame header (when known) */
404 unsigned checksumFlag; /**< 1: generate a 32-bits checksum at end of frame, for error detection */
405 unsigned noDictIDFlag; /**< 1: no dictID will be saved into frame header (if dictionary compression) */
Yann Colletd3b7f8d2016-06-04 19:47:02 +0200406} ZSTD_frameParameters;
407
408typedef struct {
409 ZSTD_compressionParameters cParams;
410 ZSTD_frameParameters fParams;
411} ZSTD_parameters;
412
Yann Colletf16f4492017-05-09 16:18:17 -0700413typedef struct {
414 unsigned long long frameContentSize;
415 unsigned windowSize;
416 unsigned dictID;
417 unsigned checksumFlag;
418} ZSTD_frameHeader;
419
inikep82057aa2016-10-06 13:23:52 +0200420/*= Custom memory allocation functions */
Yann Colletd3b7f8d2016-06-04 19:47:02 +0200421typedef void* (*ZSTD_allocFunction) (void* opaque, size_t size);
422typedef void (*ZSTD_freeFunction) (void* opaque, void* address);
423typedef struct { ZSTD_allocFunction customAlloc; ZSTD_freeFunction customFree; void* opaque; } ZSTD_customMem;
Yann Collet44e45e82017-05-30 16:12:06 -0700424/* use this constant to defer to stdlib's functions */
Yann Collet78b82342017-06-20 14:26:48 -0700425static const ZSTD_customMem ZSTD_defaultCMem = { NULL, NULL, NULL };
Yann Colletd3b7f8d2016-06-04 19:47:02 +0200426
Yann Collet05ae4b22017-06-15 18:03:34 -0700427
Sean Purcell2db72492017-02-09 10:50:43 -0800428/***************************************
Yann Collet5a36c062017-05-09 15:11:30 -0700429* Frame size functions
Sean Purcelld7bfcac2017-02-10 11:38:57 -0800430***************************************/
431
Sean Purcell9050e192017-02-22 12:12:32 -0800432/*! ZSTD_findFrameCompressedSize() :
Sean Purcell9757cc82017-02-22 12:27:15 -0800433 * `src` should point to the start of a ZSTD encoded frame or skippable frame
Sean Purcelld7bfcac2017-02-10 11:38:57 -0800434 * `srcSize` must be at least as large as the frame
Yann Collet5a36c062017-05-09 15:11:30 -0700435 * @return : the compressed size of the frame pointed to by `src`,
436 * suitable to pass to `ZSTD_decompress` or similar,
437 * or an error code if given invalid input. */
Sean Purcell9050e192017-02-22 12:12:32 -0800438ZSTDLIB_API size_t ZSTD_findFrameCompressedSize(const void* src, size_t srcSize);
Sean Purcelld7bfcac2017-02-10 11:38:57 -0800439
Sean Purcell2db72492017-02-09 10:50:43 -0800440/*! ZSTD_getFrameContentSize() :
Yann Collet5a36c062017-05-09 15:11:30 -0700441 * `src` should point to the start of a ZSTD encoded frame.
442 * `srcSize` must be at least as large as the frame header.
443 * A value >= `ZSTD_frameHeaderSize_max` is guaranteed to be large enough.
444 * @return : - decompressed size of the frame pointed to be `src` if known
445 * - ZSTD_CONTENTSIZE_UNKNOWN if the size cannot be determined
446 * - ZSTD_CONTENTSIZE_ERROR if an error occurred (e.g. invalid magic number, srcSize too small) */
447#define ZSTD_CONTENTSIZE_UNKNOWN (0ULL - 1)
448#define ZSTD_CONTENTSIZE_ERROR (0ULL - 2)
Sean Purcell2db72492017-02-09 10:50:43 -0800449ZSTDLIB_API unsigned long long ZSTD_getFrameContentSize(const void *src, size_t srcSize);
450
451/*! ZSTD_findDecompressedSize() :
Yann Collet5a36c062017-05-09 15:11:30 -0700452 * `src` should point the start of a series of ZSTD encoded and/or skippable frames
453 * `srcSize` must be the _exact_ size of this series
454 * (i.e. there should be a frame boundary exactly `srcSize` bytes after `src`)
455 * @return : - decompressed size of all data in all successive frames
456 * - if the decompressed size cannot be determined: ZSTD_CONTENTSIZE_UNKNOWN
457 * - if an error occurred: ZSTD_CONTENTSIZE_ERROR
458 *
459 * note 1 : decompressed size is an optional field, that may not be present, especially in streaming mode.
460 * When `return==ZSTD_CONTENTSIZE_UNKNOWN`, data to decompress could be any size.
461 * In which case, it's necessary to use streaming mode to decompress data.
462 * Optionally, application can still use ZSTD_decompress() while relying on implied limits.
463 * (For example, data may be necessarily cut into blocks <= 16 KB).
464 * note 2 : decompressed size is always present when compression is done with ZSTD_compress()
465 * note 3 : decompressed size can be very large (64-bits value),
466 * potentially larger than what local system can handle as a single memory segment.
467 * In which case, it's necessary to use streaming mode to decompress data.
468 * note 4 : If source is untrusted, decompressed size could be wrong or intentionally modified.
469 * Always ensure result fits within application's authorized limits.
470 * Each application can set its own limits.
471 * note 5 : ZSTD_findDecompressedSize handles multiple frames, and so it must traverse the input to
472 * read each contained frame header. This is efficient as most of the data is skipped,
473 * however it does mean that all frame data must be present and valid. */
Sean Purcell2db72492017-02-09 10:50:43 -0800474ZSTDLIB_API unsigned long long ZSTD_findDecompressedSize(const void* src, size_t srcSize);
475
Yann Colletd3b7f8d2016-06-04 19:47:02 +0200476
inikep82057aa2016-10-06 13:23:52 +0200477/***************************************
Yann Collet5a36c062017-05-09 15:11:30 -0700478* Context memory usage
479***************************************/
480
481/*! ZSTD_sizeof_*() :
482 * These functions give the current memory usage of selected object.
483 * Object memory usage can evolve if it's re-used multiple times. */
484ZSTDLIB_API size_t ZSTD_sizeof_CCtx(const ZSTD_CCtx* cctx);
485ZSTDLIB_API size_t ZSTD_sizeof_DCtx(const ZSTD_DCtx* dctx);
486ZSTDLIB_API size_t ZSTD_sizeof_CStream(const ZSTD_CStream* zcs);
487ZSTDLIB_API size_t ZSTD_sizeof_DStream(const ZSTD_DStream* zds);
488ZSTDLIB_API size_t ZSTD_sizeof_CDict(const ZSTD_CDict* cdict);
489ZSTDLIB_API size_t ZSTD_sizeof_DDict(const ZSTD_DDict* ddict);
490
491/*! ZSTD_estimate*() :
492 * These functions make it possible to estimate memory usage
493 * of a future target object, before its allocation,
494 * given a set of parameters, which vary depending on target object.
Yann Colletc35e5352017-06-01 18:44:06 -0700495 * The objective is to guide decision before allocation.
496 * Note : CCtx estimation is only correct for single-threaded compression */
Yann Collet5a36c062017-05-09 15:11:30 -0700497ZSTDLIB_API size_t ZSTD_estimateCCtxSize(ZSTD_compressionParameters cParams);
498ZSTDLIB_API size_t ZSTD_estimateDCtxSize(void);
499
500/*! ZSTD_estimate?StreamSize() :
501 * Note : if streaming is init with function ZSTD_init?Stream_usingDict(),
Yann Colletcdf7e822017-05-25 18:05:49 -0700502 * an internal ?Dict will be created, which size is not estimated here.
503 * In this case, get total size by adding ZSTD_estimate?DictSize */
Yann Collet5a36c062017-05-09 15:11:30 -0700504ZSTDLIB_API size_t ZSTD_estimateCStreamSize(ZSTD_compressionParameters cParams);
Yann Colletf16f4492017-05-09 16:18:17 -0700505ZSTDLIB_API size_t ZSTD_estimateDStreamSize(ZSTD_frameHeader fHeader);
Yann Collet5a36c062017-05-09 15:11:30 -0700506
507/*! ZSTD_estimate?DictSize() :
Yann Collet25989e32017-05-25 15:07:37 -0700508 * Note : dictionary created "byReference" are smaller */
509ZSTDLIB_API size_t ZSTD_estimateCDictSize(ZSTD_compressionParameters cParams, size_t dictSize, unsigned byReference);
510ZSTDLIB_API size_t ZSTD_estimateDDictSize(size_t dictSize, unsigned byReference);
Yann Collet5a36c062017-05-09 15:11:30 -0700511
512
513/***************************************
Yann Collet81e13ef2016-06-07 00:51:51 +0200514* Advanced compression functions
Yann Colletd3b7f8d2016-06-04 19:47:02 +0200515***************************************/
516/*! ZSTD_createCCtx_advanced() :
517 * Create a ZSTD compression context using external alloc and free functions */
518ZSTDLIB_API ZSTD_CCtx* ZSTD_createCCtx_advanced(ZSTD_customMem customMem);
519
Yann Colletc7fe2622017-05-23 13:16:00 -0700520/*! ZSTD_initStaticCCtx() : initialize a fixed-size zstd compression context
521 * workspace: The memory area to emplace the context into.
522 * Provided pointer must 8-bytes aligned.
523 * It must outlive context usage.
524 * workspaceSize: Use ZSTD_estimateCCtxSize() or ZSTD_estimateCStreamSize()
525 * to determine how large workspace must be to support scenario.
526 * @return : pointer to ZSTD_CCtx*, or NULL if error (size too small)
527 * Note : zstd will never resize nor malloc() when using a static cctx.
528 * If it needs more memory than available, it will simply error out.
529 * Note 2 : there is no corresponding "free" function.
530 * Since workspace was allocated externally, it must be freed externally too.
Yann Colletd7a3bff2017-06-19 11:53:01 -0700531 * Limitation 1 : currently not compatible with internal CDict creation, such as
532 * ZSTD_CCtx_loadDictionary() or ZSTD_initCStream_usingDict().
533 * Limitation 2 : currently not compatible with multi-threading
Yann Colletc7fe2622017-05-23 13:16:00 -0700534 */
535ZSTDLIB_API ZSTD_CCtx* ZSTD_initStaticCCtx(void* workspace, size_t workspaceSize);
536
Yann Collet33eb7ac2017-05-12 12:36:11 -0700537
Yann Collet0fdc71c2017-05-24 17:41:41 -0700538/* !!! To be deprecated !!! */
Yann Collet06e76972017-01-25 16:39:03 -0800539typedef enum {
Yann Collet14312d82017-02-23 23:42:12 -0800540 ZSTD_p_forceWindow, /* Force back-references to remain < windowSize, even when referencing Dictionary content (default:0) */
541 ZSTD_p_forceRawDict /* Force loading dictionary in "content-only" mode (no header analysis) */
Yann Collet06e76972017-01-25 16:39:03 -0800542} ZSTD_CCtxParameter;
Yann Collet4a62f792017-01-26 09:16:56 -0800543/*! ZSTD_setCCtxParameter() :
544 * Set advanced parameters, selected through enum ZSTD_CCtxParameter
545 * @result : 0, or an error code (which can be tested with ZSTD_isError()) */
Yann Colletef33d002017-01-26 12:24:21 -0800546ZSTDLIB_API size_t ZSTD_setCCtxParameter(ZSTD_CCtx* cctx, ZSTD_CCtxParameter param, unsigned value);
Yann Colletbb002742017-01-25 16:25:38 -0800547
Yann Colletcdf7e822017-05-25 18:05:49 -0700548
Yann Collet1f57c2e2016-12-21 16:20:11 +0100549/*! ZSTD_createCDict_byReference() :
550 * Create a digested dictionary for compression
551 * Dictionary content is simply referenced, and therefore stays in dictBuffer.
552 * It is important that dictBuffer outlives CDict, it must remain read accessible throughout the lifetime of CDict */
553ZSTDLIB_API ZSTD_CDict* ZSTD_createCDict_byReference(const void* dictBuffer, size_t dictSize, int compressionLevel);
554
Yann Collet7bd1a292017-06-21 11:50:33 -0700555
556typedef enum { ZSTD_dm_auto=0, ZSTD_dm_rawContent, ZSTD_dm_fullDict } ZSTD_dictMode_e;
Yann Collet81e13ef2016-06-07 00:51:51 +0200557/*! ZSTD_createCDict_advanced() :
558 * Create a ZSTD_CDict using external alloc and free, and customized compression parameters */
Yann Collet7bd1a292017-06-21 11:50:33 -0700559ZSTDLIB_API ZSTD_CDict* ZSTD_createCDict_advanced(const void* dict, size_t dictSize,
560 unsigned byReference, ZSTD_dictMode_e dictMode,
Yann Collet31533ba2017-04-27 00:29:04 -0700561 ZSTD_compressionParameters cParams, ZSTD_customMem customMem);
Yann Collet8e0ee682016-07-11 13:09:52 +0200562
Yann Colletcdf7e822017-05-25 18:05:49 -0700563/*! ZSTD_initStaticCDict_advanced() :
564 * Generate a digested dictionary in provided memory area.
565 * workspace: The memory area to emplace the dictionary into.
566 * Provided pointer must 8-bytes aligned.
567 * It must outlive dictionary usage.
568 * workspaceSize: Use ZSTD_estimateCDictSize()
569 * to determine how large workspace must be.
570 * cParams : use ZSTD_getCParams() to transform a compression level
571 * into its relevants cParams.
572 * @return : pointer to ZSTD_CDict*, or NULL if error (size too small)
573 * Note : there is no corresponding "free" function.
574 * Since workspace was allocated externally, it must be freed externally.
575 */
576ZSTDLIB_API ZSTD_CDict* ZSTD_initStaticCDict(
577 void* workspace, size_t workspaceSize,
Yann Collet7bd1a292017-06-21 11:50:33 -0700578 const void* dict, size_t dictSize,
579 unsigned byReference, ZSTD_dictMode_e dictMode,
Yann Colletcdf7e822017-05-25 18:05:49 -0700580 ZSTD_compressionParameters cParams);
581
Yann Colletd3b7f8d2016-06-04 19:47:02 +0200582/*! ZSTD_getCParams() :
Yann Collet2b36b232016-12-13 17:59:55 +0100583* @return ZSTD_compressionParameters structure for a selected compression level and estimated srcSize.
584* `estimatedSrcSize` value is optional, select 0 if not known */
585ZSTDLIB_API ZSTD_compressionParameters ZSTD_getCParams(int compressionLevel, unsigned long long estimatedSrcSize, size_t dictSize);
586
587/*! ZSTD_getParams() :
588* same as ZSTD_getCParams(), but @return a full `ZSTD_parameters` object instead of sub-component `ZSTD_compressionParameters`.
589* All fields of `ZSTD_frameParameters` are set to default (0) */
590ZSTDLIB_API ZSTD_parameters ZSTD_getParams(int compressionLevel, unsigned long long estimatedSrcSize, size_t dictSize);
Yann Colletd3b7f8d2016-06-04 19:47:02 +0200591
Yann Collet3d2cd7f2016-06-27 15:12:26 +0200592/*! ZSTD_checkCParams() :
Yann Colletd3b7f8d2016-06-04 19:47:02 +0200593* Ensure param values remain within authorized range */
594ZSTDLIB_API size_t ZSTD_checkCParams(ZSTD_compressionParameters params);
595
Yann Collet3d2cd7f2016-06-27 15:12:26 +0200596/*! ZSTD_adjustCParams() :
Yann Collet381e66c2017-06-16 17:29:35 -0700597 * optimize params for a given `srcSize` and `dictSize`.
598 * both values are optional, select `0` if unknown. */
Yann Collet52c04fe2016-07-07 11:53:18 +0200599ZSTDLIB_API ZSTD_compressionParameters ZSTD_adjustCParams(ZSTD_compressionParameters cPar, unsigned long long srcSize, size_t dictSize);
Yann Colletd3b7f8d2016-06-04 19:47:02 +0200600
601/*! ZSTD_compress_advanced() :
Yann Colletf4bd8572017-04-27 11:31:55 -0700602* Same as ZSTD_compress_usingDict(), with fine-tune control over each compression parameter */
603ZSTDLIB_API size_t ZSTD_compress_advanced (ZSTD_CCtx* cctx,
604 void* dst, size_t dstCapacity,
605 const void* src, size_t srcSize,
606 const void* dict,size_t dictSize,
607 ZSTD_parameters params);
608
609/*! ZSTD_compress_usingCDict_advanced() :
610* Same as ZSTD_compress_usingCDict(), with fine-tune control over frame parameters */
611ZSTDLIB_API size_t ZSTD_compress_usingCDict_advanced(ZSTD_CCtx* cctx,
612 void* dst, size_t dstCapacity,
613 const void* src, size_t srcSize,
614 const ZSTD_CDict* cdict, ZSTD_frameParameters fParams);
Yann Colletd3b7f8d2016-06-04 19:47:02 +0200615
Yann Collet45c03c52016-06-14 13:46:11 +0200616
Przemyslaw Skibinski984b66c2016-10-24 15:59:51 +0200617/*--- Advanced decompression functions ---*/
Yann Colletd3b7f8d2016-06-04 19:47:02 +0200618
Yann Collet179b1972016-11-02 17:30:49 -0700619/*! ZSTD_isFrame() :
620 * Tells if the content of `buffer` starts with a valid Frame Identifier.
621 * Note : Frame Identifier is 4 bytes. If `size < 4`, @return will always be 0.
622 * Note 2 : Legacy Frame Identifiers are considered valid only if Legacy Support is enabled.
623 * Note 3 : Skippable Frame Identifiers are considered valid. */
624ZSTDLIB_API unsigned ZSTD_isFrame(const void* buffer, size_t size);
625
Yann Collet81e13ef2016-06-07 00:51:51 +0200626/*! ZSTD_createDCtx_advanced() :
627 * Create a ZSTD decompression context using external alloc and free functions */
628ZSTDLIB_API ZSTD_DCtx* ZSTD_createDCtx_advanced(ZSTD_customMem customMem);
Yann Colletd3b7f8d2016-06-04 19:47:02 +0200629
Yann Collet0fdc71c2017-05-24 17:41:41 -0700630/*! ZSTD_initStaticDCtx() : initialize a fixed-size zstd decompression context
631 * workspace: The memory area to emplace the context into.
632 * Provided pointer must 8-bytes aligned.
633 * It must outlive context usage.
634 * workspaceSize: Use ZSTD_estimateDCtxSize() or ZSTD_estimateDStreamSize()
635 * to determine how large workspace must be to support scenario.
636 * @return : pointer to ZSTD_DCtx*, or NULL if error (size too small)
637 * Note : zstd will never resize nor malloc() when using a static dctx.
638 * If it needs more memory than available, it will simply error out.
Yann Colletb8136f02017-05-27 00:03:08 -0700639 * Note 2 : static dctx is incompatible with legacy support
640 * Note 3 : there is no corresponding "free" function.
Yann Collet0fdc71c2017-05-24 17:41:41 -0700641 * Since workspace was allocated externally, it must be freed externally.
642 * Limitation : currently not compatible with internal DDict creation,
643 * such as ZSTD_initDStream_usingDict().
644 */
645ZSTDLIB_API ZSTD_DCtx* ZSTD_initStaticDCtx(void* workspace, size_t workspaceSize);
646
Yann Collet4e5eea62016-12-21 16:44:35 +0100647/*! ZSTD_createDDict_byReference() :
648 * Create a digested dictionary, ready to start decompression operation without startup delay.
Yann Collet57827f92017-05-25 15:44:06 -0700649 * Dictionary content is referenced, and therefore stays in dictBuffer.
650 * It is important that dictBuffer outlives DDict,
651 * it must remain read accessible throughout the lifetime of DDict */
Yann Collet4e5eea62016-12-21 16:44:35 +0100652ZSTDLIB_API ZSTD_DDict* ZSTD_createDDict_byReference(const void* dictBuffer, size_t dictSize);
653
Sean Purcelldec2b962017-03-14 11:24:09 -0700654/*! ZSTD_createDDict_advanced() :
655 * Create a ZSTD_DDict using external alloc and free, optionally by reference */
Yann Collet4e5eea62016-12-21 16:44:35 +0100656ZSTDLIB_API ZSTD_DDict* ZSTD_createDDict_advanced(const void* dict, size_t dictSize,
657 unsigned byReference, ZSTD_customMem customMem);
658
Yann Collet25989e32017-05-25 15:07:37 -0700659/*! ZSTD_initStaticDDict() :
660 * Generate a digested dictionary in provided memory area.
661 * workspace: The memory area to emplace the dictionary into.
662 * Provided pointer must 8-bytes aligned.
663 * It must outlive dictionary usage.
664 * workspaceSize: Use ZSTD_estimateDDictSize()
665 * to determine how large workspace must be.
666 * @return : pointer to ZSTD_DDict*, or NULL if error (size too small)
667 * Note : there is no corresponding "free" function.
668 * Since workspace was allocated externally, it must be freed externally.
669 */
670ZSTDLIB_API ZSTD_DDict* ZSTD_initStaticDDict(void* workspace, size_t workspaceSize,
671 const void* dict, size_t dictSize,
672 unsigned byReference);
673
Yann Collete7a41a52016-12-05 16:21:06 -0800674/*! ZSTD_getDictID_fromDict() :
675 * Provides the dictID stored within dictionary.
676 * if @return == 0, the dictionary is not conformant with Zstandard specification.
677 * It can still be loaded, but as a content-only dictionary. */
Nick Terrell8de46ab2016-12-16 13:27:30 -0800678ZSTDLIB_API unsigned ZSTD_getDictID_fromDict(const void* dict, size_t dictSize);
Yann Collete7a41a52016-12-05 16:21:06 -0800679
680/*! ZSTD_getDictID_fromDDict() :
681 * Provides the dictID of the dictionary loaded into `ddict`.
682 * If @return == 0, the dictionary is not conformant to Zstandard specification, or empty.
683 * Non-conformant dictionaries can still be loaded, but as content-only dictionaries. */
Nick Terrell8de46ab2016-12-16 13:27:30 -0800684ZSTDLIB_API unsigned ZSTD_getDictID_fromDDict(const ZSTD_DDict* ddict);
Yann Collete7a41a52016-12-05 16:21:06 -0800685
686/*! ZSTD_getDictID_fromFrame() :
687 * Provides the dictID required to decompressed the frame stored within `src`.
688 * If @return == 0, the dictID could not be decoded.
689 * This could for one of the following reasons :
690 * - The frame does not require a dictionary to be decoded (most common case).
691 * - The frame was built with dictID intentionally removed. Whatever dictionary is necessary is a hidden information.
692 * Note : this use case also happens when using a non-conformant dictionary.
693 * - `srcSize` is too small, and as a result, the frame header could not be decoded (only possible if `srcSize < ZSTD_FRAMEHEADERSIZE_MAX`).
694 * - This is not a Zstandard frame.
Yann Collet7028cbd2017-05-25 18:29:08 -0700695 * When identifying the exact failure cause, it's possible to use ZSTD_getFrameHeader(), which will provide a more precise error code. */
Nick Terrell8de46ab2016-12-16 13:27:30 -0800696ZSTDLIB_API unsigned ZSTD_getDictID_fromFrame(const void* src, size_t srcSize);
Yann Collete7a41a52016-12-05 16:21:06 -0800697
Yann Colletd3b7f8d2016-06-04 19:47:02 +0200698
inikep82057aa2016-10-06 13:23:52 +0200699/********************************************************************
Przemyslaw Skibinski984b66c2016-10-24 15:59:51 +0200700* Advanced streaming functions
Yann Collet5a0c8e22016-08-12 01:20:36 +0200701********************************************************************/
702
inikep82057aa2016-10-06 13:23:52 +0200703/*===== Advanced Streaming compression functions =====*/
Yann Collet70e3b312016-08-23 01:18:06 +0200704ZSTDLIB_API ZSTD_CStream* ZSTD_createCStream_advanced(ZSTD_customMem customMem);
Sean Purcell2db72492017-02-09 10:50:43 -0800705ZSTDLIB_API size_t ZSTD_initCStream_srcSize(ZSTD_CStream* zcs, int compressionLevel, unsigned long long pledgedSrcSize); /**< pledgedSrcSize must be correct, a size of 0 means unknown. for a frame size of 0 use initCStream_advanced */
Yann Colletc7fe2622017-05-23 13:16:00 -0700706ZSTDLIB_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. */
Yann Collet70e3b312016-08-23 01:18:06 +0200707ZSTDLIB_API size_t ZSTD_initCStream_advanced(ZSTD_CStream* zcs, const void* dict, size_t dictSize,
Sean Purcell0f5c95a2017-02-07 16:33:48 -0800708 ZSTD_parameters params, unsigned long long pledgedSrcSize); /**< pledgedSrcSize is optional and can be 0 (meaning unknown). note: if the contentSizeFlag is set, pledgedSrcSize == 0 means the source size is actually 0 */
Yann Collet95162342016-10-25 16:19:52 -0700709ZSTDLIB_API size_t ZSTD_initCStream_usingCDict(ZSTD_CStream* zcs, const ZSTD_CDict* cdict); /**< note : cdict will just be referenced, and must outlive compression session */
Yann Collet8c910d22017-06-03 01:15:02 -0700710ZSTDLIB_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 */
Yann Collet36c2a032017-04-05 22:06:21 -0700711
712/*! ZSTD_resetCStream() :
713 * start a new compression job, using same parameters from previous job.
714 * This is typically useful to skip dictionary loading stage, since it will re-use it in-place..
715 * Note that zcs must be init at least once before using ZSTD_resetCStream().
716 * pledgedSrcSize==0 means "srcSize unknown".
717 * If pledgedSrcSize > 0, its value must be correct, as it will be written in header, and controlled at the end.
718 * @return : 0, or an error code (which can be tested using ZSTD_isError()) */
719ZSTDLIB_API size_t ZSTD_resetCStream(ZSTD_CStream* zcs, unsigned long long pledgedSrcSize);
Yann Colletcb327632016-08-23 00:30:31 +0200720
Yann Collet5a0c8e22016-08-12 01:20:36 +0200721
inikep82057aa2016-10-06 13:23:52 +0200722/*===== Advanced Streaming decompression functions =====*/
Yann Colletbb002742017-01-25 16:25:38 -0800723typedef enum { DStream_p_maxWindowSize } ZSTD_DStreamParameter_e;
Yann Collet70e3b312016-08-23 01:18:06 +0200724ZSTDLIB_API ZSTD_DStream* ZSTD_createDStream_advanced(ZSTD_customMem customMem);
Yann Collet17e482e2016-08-23 16:58:10 +0200725ZSTDLIB_API size_t ZSTD_setDStreamParameter(ZSTD_DStream* zds, ZSTD_DStreamParameter_e paramType, unsigned paramValue);
Yann Collet5a36c062017-05-09 15:11:30 -0700726ZSTDLIB_API size_t ZSTD_initDStream_usingDict(ZSTD_DStream* zds, const void* dict, size_t dictSize); /**< note: a dict will not be used if dict == NULL or dictSize < 8 */
Yann Collet95162342016-10-25 16:19:52 -0700727ZSTDLIB_API size_t ZSTD_initDStream_usingDDict(ZSTD_DStream* zds, const ZSTD_DDict* ddict); /**< note : ddict will just be referenced, and must outlive decompression session */
Yann Collet4cb21292016-09-15 14:54:07 +0200728ZSTDLIB_API size_t ZSTD_resetDStream(ZSTD_DStream* zds); /**< re-use decompression parameters from previous init; saves dictionary loading */
Yann Collet5a0c8e22016-08-12 01:20:36 +0200729
730
inikep82057aa2016-10-06 13:23:52 +0200731/*********************************************************************
Yann Collet5a0c8e22016-08-12 01:20:36 +0200732* Buffer-less and synchronous inner streaming functions
inikep82057aa2016-10-06 13:23:52 +0200733*
734* This is an advanced API, giving full control over buffer management, for users which need direct control over memory.
Yann Collet655393c2016-08-14 00:16:20 +0200735* But it's also a complex one, with many restrictions (documented below).
Yann Collet37d13002016-10-24 17:22:12 -0700736* Prefer using normal streaming API for an easier experience
inikep82057aa2016-10-06 13:23:52 +0200737********************************************************************* */
Yann Collet60ba31c2016-07-28 19:55:09 +0200738
inikep82057aa2016-10-06 13:23:52 +0200739/**
740 Buffer-less streaming compression (synchronous mode)
Yann Colletd3b7f8d2016-06-04 19:47:02 +0200741
Yann Colletd3b7f8d2016-06-04 19:47:02 +0200742 A ZSTD_CCtx object is required to track streaming operations.
Yann Collet45c03c52016-06-14 13:46:11 +0200743 Use ZSTD_createCCtx() / ZSTD_freeCCtx() to manage resource.
Yann Colletd3b7f8d2016-06-04 19:47:02 +0200744 ZSTD_CCtx object can be re-used multiple times within successive compression operations.
745
746 Start by initializing a context.
747 Use ZSTD_compressBegin(), or ZSTD_compressBegin_usingDict() for dictionary compression,
748 or ZSTD_compressBegin_advanced(), for finer parameter control.
749 It's also possible to duplicate a reference context which has already been initialized, using ZSTD_copyCCtx()
750
751 Then, consume your input using ZSTD_compressContinue().
Yann Colleta49e0662016-06-21 11:54:03 +0200752 There are some important considerations to keep in mind when using this advanced function :
753 - ZSTD_compressContinue() has no internal buffer. It uses externally provided buffer only.
Yann Collet62470b42016-07-28 15:29:08 +0200754 - Interface is synchronous : input is consumed entirely and produce 1+ (or more) compressed blocks.
Yann Colleta49e0662016-06-21 11:54:03 +0200755 - Caller must ensure there is enough space in `dst` to store compressed data under worst case scenario.
756 Worst case evaluation is provided by ZSTD_compressBound().
757 ZSTD_compressContinue() doesn't guarantee recover after a failed compression.
758 - ZSTD_compressContinue() presumes prior input ***is still accessible and unmodified*** (up to maximum distance size, see WindowLog).
759 It remembers all previous contiguous blocks, plus one separated memory segment (which can itself consists of multiple contiguous blocks)
760 - ZSTD_compressContinue() detects that prior input has been overwritten when `src` buffer overlaps.
761 In which case, it will "discard" the relevant memory section from its history.
762
Yann Collet62470b42016-07-28 15:29:08 +0200763 Finish a frame with ZSTD_compressEnd(), which will write the last block(s) and optional checksum.
Yann Collet5eb749e2017-01-11 18:21:25 +0100764 It's possible to use srcSize==0, in which case, it will write a final empty block to end the frame.
765 Without last block mark, frames will be considered unfinished (corrupted) by decoders.
Yann Colletd3b7f8d2016-06-04 19:47:02 +0200766
Yann Collet5eb749e2017-01-11 18:21:25 +0100767 `ZSTD_CCtx` object can be re-used (ZSTD_compressBegin()) to compress some new frame.
Yann Colletd3b7f8d2016-06-04 19:47:02 +0200768*/
769
inikep82057aa2016-10-06 13:23:52 +0200770/*===== Buffer-less streaming compression functions =====*/
771ZSTDLIB_API size_t ZSTD_compressBegin(ZSTD_CCtx* cctx, int compressionLevel);
772ZSTDLIB_API size_t ZSTD_compressBegin_usingDict(ZSTD_CCtx* cctx, const void* dict, size_t dictSize, int compressionLevel);
Sean Purcell0f5c95a2017-02-07 16:33:48 -0800773ZSTDLIB_API size_t ZSTD_compressBegin_advanced(ZSTD_CCtx* cctx, const void* dict, size_t dictSize, ZSTD_parameters params, unsigned long long pledgedSrcSize); /**< pledgedSrcSize is optional and can be 0 (meaning unknown). note: if the contentSizeFlag is set, pledgedSrcSize == 0 means the source size is actually 0 */
Yann Collet768df122017-04-26 15:42:10 -0700774ZSTDLIB_API size_t ZSTD_compressBegin_usingCDict(ZSTD_CCtx* cctx, const ZSTD_CDict* cdict); /**< note: fails if cdict==NULL */
Yann Collet715b9aa2017-04-18 13:55:53 -0700775ZSTDLIB_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=0 means null-size */
Sean Purcell2db72492017-02-09 10:50:43 -0800776ZSTDLIB_API size_t ZSTD_copyCCtx(ZSTD_CCtx* cctx, const ZSTD_CCtx* preparedCCtx, unsigned long long pledgedSrcSize); /**< note: if pledgedSrcSize can be 0, indicating unknown size. if it is non-zero, it must be accurate. for 0 size frames, use compressBegin_advanced */
Yann Collet715b9aa2017-04-18 13:55:53 -0700777
inikep82057aa2016-10-06 13:23:52 +0200778ZSTDLIB_API size_t ZSTD_compressContinue(ZSTD_CCtx* cctx, void* dst, size_t dstCapacity, const void* src, size_t srcSize);
779ZSTDLIB_API size_t ZSTD_compressEnd(ZSTD_CCtx* cctx, void* dst, size_t dstCapacity, const void* src, size_t srcSize);
Yann Colletd3b7f8d2016-06-04 19:47:02 +0200780
Yann Colletd3b7f8d2016-06-04 19:47:02 +0200781
Yann Colletd3b7f8d2016-06-04 19:47:02 +0200782
inikepba1db372016-10-06 14:22:48 +0200783/*-
Yann Colletcf05b9d2016-07-18 16:52:10 +0200784 Buffer-less streaming decompression (synchronous mode)
Yann Colletd3b7f8d2016-06-04 19:47:02 +0200785
786 A ZSTD_DCtx object is required to track streaming operations.
787 Use ZSTD_createDCtx() / ZSTD_freeDCtx() to manage it.
788 A ZSTD_DCtx object can be re-used multiple times.
789
Yann Collet7028cbd2017-05-25 18:29:08 -0700790 First typical operation is to retrieve frame parameters, using ZSTD_getFrameHeader().
Yann Collet6b615d32016-07-29 19:40:37 +0200791 It fills a ZSTD_frameParams structure which provide important information to correctly decode the frame,
792 such as the minimum rolling buffer size to allocate to decompress data (`windowSize`),
793 and the dictionary ID used.
794 (Note : content size is optional, it may not be present. 0 means : content size unknown).
795 Note that these values could be wrong, either because of data malformation, or because an attacker is spoofing deliberate false information.
796 As a consequence, check that values remain within valid application range, especially `windowSize`, before allocation.
797 Each application can set its own limit, depending on local restrictions. For extended interoperability, it is recommended to support at least 8 MB.
798 Frame parameters are extracted from the beginning of the compressed frame.
799 Data fragment must be large enough to ensure successful decoding, typically `ZSTD_frameHeaderSize_max` bytes.
800 @result : 0 : successful decoding, the `ZSTD_frameParams` structure is correctly filled.
801 >0 : `srcSize` is too small, please provide at least @result bytes on next attempt.
Yann Colletd469a982016-07-28 03:47:45 +0200802 errorCode, which can be tested using ZSTD_isError().
Yann Colletd3b7f8d2016-06-04 19:47:02 +0200803
Yann Collet4eff8132017-05-16 16:05:27 -0700804 Start decompression, with ZSTD_decompressBegin().
805 If decompression requires a dictionary, use ZSTD_decompressBegin_usingDict() or ZSTD_decompressBegin_usingDDict().
Yann Colletd3b7f8d2016-06-04 19:47:02 +0200806 Alternatively, you can copy a prepared context, using ZSTD_copyDCtx().
807
808 Then use ZSTD_nextSrcSizeToDecompress() and ZSTD_decompressContinue() alternatively.
Yann Collet6b615d32016-07-29 19:40:37 +0200809 ZSTD_nextSrcSizeToDecompress() tells how many bytes to provide as 'srcSize' to ZSTD_decompressContinue().
810 ZSTD_decompressContinue() requires this _exact_ amount of bytes, or it will fail.
Yann Colletd3b7f8d2016-06-04 19:47:02 +0200811
Yann Collet49bb0042016-06-04 20:17:38 +0200812 @result of ZSTD_decompressContinue() is the number of bytes regenerated within 'dst' (necessarily <= dstCapacity).
Yann Collet6b615d32016-07-29 19:40:37 +0200813 It can be zero, which is not an error; it just means ZSTD_decompressContinue() has decoded some metadata item.
814 It can also be an error code, which can be tested with ZSTD_isError().
Yann Colletd3b7f8d2016-06-04 19:47:02 +0200815
Yann Collet3b6ae772016-07-08 23:42:22 +0200816 ZSTD_decompressContinue() needs previous data blocks during decompression, up to `windowSize`.
817 They should preferably be located contiguously, prior to current block.
818 Alternatively, a round buffer of sufficient size is also possible. Sufficient size is determined by frame parameters.
819 ZSTD_decompressContinue() is very sensitive to contiguity,
820 if 2 blocks don't follow each other, make sure that either the compressor breaks contiguity at the same place,
Yann Colletd469a982016-07-28 03:47:45 +0200821 or that previous contiguous segment is large enough to properly handle maximum back-reference.
Yann Collet3b6ae772016-07-08 23:42:22 +0200822
Yann Colletd3b7f8d2016-06-04 19:47:02 +0200823 A frame is fully decoded when ZSTD_nextSrcSizeToDecompress() returns zero.
824 Context can then be reset to start a new decompression.
825
Yann Collet4c5bbf62016-07-28 20:30:25 +0200826 Note : it's possible to know if next input to present is a header or a block, using ZSTD_nextInputType().
Yann Collet6b615d32016-07-29 19:40:37 +0200827 This information is not required to properly decode a frame.
Yann Collet3b6ae772016-07-08 23:42:22 +0200828
Yann Collete795c8a2016-12-13 16:39:36 +0100829 == Special case : skippable frames ==
Yann Collet3b6ae772016-07-08 23:42:22 +0200830
Yann Colletd469a982016-07-28 03:47:45 +0200831 Skippable frames allow integration of user-defined data into a flow of concatenated frames.
Yann Collet5b567392016-07-28 01:17:22 +0200832 Skippable frames will be ignored (skipped) by a decompressor. The format of skippable frames is as follows :
Yann Colletd3b7f8d2016-06-04 19:47:02 +0200833 a) Skippable frame ID - 4 Bytes, Little endian format, any value from 0x184D2A50 to 0x184D2A5F
834 b) Frame Size - 4 Bytes, Little endian format, unsigned 32-bits
835 c) Frame Content - any content (User Data) of length equal to Frame Size
836 For skippable frames ZSTD_decompressContinue() always returns 0.
Yann Collet7028cbd2017-05-25 18:29:08 -0700837 For skippable frames ZSTD_getFrameHeader() returns fparamsPtr->windowLog==0 what means that a frame is skippable.
Sean Purcell64417cd2017-02-22 13:29:01 -0800838 Note : If fparamsPtr->frameContentSize==0, it is ambiguous: the frame might actually be a Zstd encoded frame with no content.
839 For purposes of decompression, it is valid in both cases to skip the frame using
840 ZSTD_findFrameCompressedSize to find its size in bytes.
Yann Colletd3b7f8d2016-06-04 19:47:02 +0200841 It also returns Frame Size as fparamsPtr->frameContentSize.
842*/
843
inikep82057aa2016-10-06 13:23:52 +0200844/*===== Buffer-less streaming decompression functions =====*/
Yann Colletcef02d92017-05-10 11:14:08 -0700845ZSTDLIB_API size_t ZSTD_getFrameHeader(ZSTD_frameHeader* zfhPtr, const void* src, size_t srcSize); /**< doesn't consume input, see details below */
inikep82057aa2016-10-06 13:23:52 +0200846ZSTDLIB_API size_t ZSTD_decompressBegin(ZSTD_DCtx* dctx);
847ZSTDLIB_API size_t ZSTD_decompressBegin_usingDict(ZSTD_DCtx* dctx, const void* dict, size_t dictSize);
Yann Collet4eff8132017-05-16 16:05:27 -0700848ZSTDLIB_API size_t ZSTD_decompressBegin_usingDDict(ZSTD_DCtx* dctx, const ZSTD_DDict* ddict);
inikep82057aa2016-10-06 13:23:52 +0200849ZSTDLIB_API void ZSTD_copyDCtx(ZSTD_DCtx* dctx, const ZSTD_DCtx* preparedDCtx);
Yann Collet4eff8132017-05-16 16:05:27 -0700850
inikep82057aa2016-10-06 13:23:52 +0200851ZSTDLIB_API size_t ZSTD_nextSrcSizeToDecompress(ZSTD_DCtx* dctx);
852ZSTDLIB_API size_t ZSTD_decompressContinue(ZSTD_DCtx* dctx, void* dst, size_t dstCapacity, const void* src, size_t srcSize);
853typedef enum { ZSTDnit_frameHeader, ZSTDnit_blockHeader, ZSTDnit_block, ZSTDnit_lastBlock, ZSTDnit_checksum, ZSTDnit_skippableFrame } ZSTD_nextInputType_e;
854ZSTDLIB_API ZSTD_nextInputType_e ZSTD_nextInputType(ZSTD_DCtx* dctx);
855
Yann Colletbf991502017-06-19 12:56:25 -0700856
857
858/*=== New advanced API (experimental, and compression only) ===*/
859
860/* notes on API design :
861 * In this proposal, parameters are pushed one by one into an existing CCtx,
862 * and then applied on all subsequent compression jobs.
863 * When no parameter is ever provided, CCtx is created with compression level ZSTD_CLEVEL_DEFAULT.
864 *
865 * This API is intended to replace all others experimental API.
866 * It can basically do all other use cases, and even new ones.
867 * It stands a good chance to become "stable",
868 * after a reasonable testing period.
869 */
870
871/* note on naming convention :
872 * Initially, the API favored names like ZSTD_setCCtxParameter() .
873 * In this proposal, convention is changed towards ZSTD_CCtx_setParameter() .
Yann Collet7bd1a292017-06-21 11:50:33 -0700874 * The main driver is that it identifies more clearly the target object type.
875 * It feels clearer in light of potential variants :
Yann Colletbf991502017-06-19 12:56:25 -0700876 * ZSTD_CDict_setParameter() (rather than ZSTD_setCDictParameter())
877 * ZSTD_DCtx_setParameter() (rather than ZSTD_setDCtxParameter() )
Yann Collet7bd1a292017-06-21 11:50:33 -0700878 * Left variant feels easier to distinguish.
Yann Colletbf991502017-06-19 12:56:25 -0700879 */
880
Yann Collet7bd1a292017-06-21 11:50:33 -0700881/* note on enum design :
882 * All enum will be manually set to explicit values before reaching "stable API" status */
883
Yann Colletbf991502017-06-19 12:56:25 -0700884typedef enum {
885 /* compression parameters */
886 ZSTD_p_compressionLevel=100, /* Update all compression parameters according to pre-defined cLevel table
887 * Default level is ZSTD_CLEVEL_DEFAULT==3.
888 * Special: value 0 means "do not change cLevel". */
889 ZSTD_p_windowLog, /* Maximum allowed back-reference distance, expressed as power of 2.
890 * Must be clamped between ZSTD_WINDOWLOG_MIN and ZSTD_WINDOWLOG_MAX.
Yann Colletbf991502017-06-19 12:56:25 -0700891 * Special: value 0 means "do not change windowLog". */
892 ZSTD_p_hashLog, /* Size of the probe table, as a power of 2.
893 * Resulting table size is (1 << (hashLog+2)).
894 * Must be clamped between ZSTD_HASHLOG_MIN and ZSTD_HASHLOG_MAX.
895 * Larger tables improve compression ratio of strategies <= dFast,
896 * and improve speed of strategies > dFast.
897 * Special: value 0 means "do not change hashLog". */
898 ZSTD_p_chainLog, /* Size of the full-search table, as a power of 2.
899 * Resulting table size is (1 << (chainLog+2)).
900 * Larger tables result in better and slower compression.
901 * This parameter is useless when using "fast" strategy.
902 * Special: value 0 means "do not change chainLog". */
903 ZSTD_p_searchLog, /* Number of search attempts, as a power of 2.
904 * More attempts result in better and slower compression.
905 * This parameter is useless when using "fast" and "dFast" strategies.
906 * Special: value 0 means "do not change searchLog". */
907 ZSTD_p_minMatch, /* Minimum size of searched matches (note : repCode matches can be smaller).
908 * Larger values make faster compression and decompression, but decrease ratio.
909 * Must be clamped between ZSTD_SEARCHLENGTH_MIN and ZSTD_SEARCHLENGTH_MAX.
910 * Note that currently, for all strategies < btopt, effective minimum is 4.
911 * Note that currently, for all strategies > fast, effective maximum is 6.
912 * Special: value 0 means "do not change minMatchLength". */
913 ZSTD_p_targetLength, /* Only useful for strategies >= btopt.
914 * Length of Match considered "good enough" to stop search.
915 * Larger values make compression stronger and slower.
916 * Special: value 0 means "do not change targetLength". */
917 ZSTD_p_compressionStrategy, /* See ZSTD_strategy enum definition.
918 * Cast selected strategy as unsigned for ZSTD_CCtx_setParameter() compatibility.
919 * The higher the value of selected strategy, the more complex it is,
920 * resulting in stronger and slower compression.
921 * Special: value 0 means "do not change strategy". */
922#if 0
923 ZSTD_p_windowSize, /* Maximum allowed back-reference distance.
924 * Can be set to a more precise value than windowLog.
925 * Will be transparently reduced to closest possible inferior value
926 * (see Zstandard compression format) */
927 /* Not ready yet ! */
928#endif
929
930 /* frame parameters */
931 ZSTD_p_contentSizeFlag=200, /* Content size is written into frame header _whenever known_ (default:1) */
932 ZSTD_p_checksumFlag, /* A 32-bits checksum of content is written at end of frame (default:0) */
933 ZSTD_p_dictIDFlag, /* When applicable, dictID of dictionary is provided in frame header (default:1) */
934
935 /* dictionary parameters */
936 ZSTD_p_refDictContent=300, /* Content of dictionary content will be referenced, instead of copied (default:0).
937 * This avoids duplicating dictionary content.
Yann Collet7bd1a292017-06-21 11:50:33 -0700938 * But it also requires that dictionary buffer outlives its users */
939 /* Not ready yet ! <=================================== */
940 ZSTD_p_dictMode, /* Select how dictionary must be interpreted. Value must be from type ZSTD_dictMode_e.
941 * default : 0==auto : dictionary will be "full" if it respects specification, otherwise it will be "rawContent" */
Yann Colletbf991502017-06-19 12:56:25 -0700942
943 /* multi-threading parameters */
944 ZSTD_p_nbThreads=400, /* Select how many threads a compression job can spawn (default:1)
945 * More threads improve speed, but also increase memory usage.
946 * Can only receive a value > 1 if ZSTD_MULTITHREAD is enabled.
947 * Special: value 0 means "do not change nbThreads" */
948 ZSTD_p_jobSize, /* Size of a compression job. Each compression job is completed in parallel.
949 * 0 means default, which is dynamically determined based on compression parameters.
950 * Job size must be a minimum of overlapSize, or 1 KB, whichever is largest
951 * The minimum size is automatically and transparently enforced */
952 ZSTD_p_overlapSizeLog, /* Size of previous input reloaded at the beginning of each job.
953 * 0 => no overlap, 6(default) => use 1/8th of windowSize, >=9 => use full windowSize */
954
955 /* advanced parameters - may not remain available after API update */
956 ZSTD_p_forceMaxWindow=1100, /* Force back-references to remain < windowSize,
Yann Collet7bd1a292017-06-21 11:50:33 -0700957 * even when referencing into Dictionary content.
Yann Colletbf991502017-06-19 12:56:25 -0700958 * default : 0 when using a CDict, 1 when using a Prefix */
959} ZSTD_cParameter;
960
961
962/*! ZSTD_CCtx_setParameter() :
963 * Set one compression parameter, selected by enum ZSTD_cParameter.
964 * Note : when `value` is an enum, cast it to unsigned for proper type checking.
965 * @result : 0, or an error code (which can be tested with ZSTD_isError()). */
966ZSTDLIB_API size_t ZSTD_CCtx_setParameter(ZSTD_CCtx* cctx, ZSTD_cParameter param, unsigned value);
967
968/*! ZSTD_CCtx_setPledgedSrcSize() :
969 * Total input data size to be compressed as a single frame.
970 * This value will be controlled at the end, and result in error if not respected.
971 * @result : 0, or an error code (which can be tested with ZSTD_isError()).
972 * Note 1 : 0 means zero, empty.
973 * In order to mean "unknown content size", pass constant ZSTD_CONTENTSIZE_UNKNOWN.
974 * Note that ZSTD_CONTENTSIZE_UNKNOWN is default value for new compression jobs.
975 * Note 2 : If all data is provided and consumed in a single round,
976 * this value is overriden by srcSize instead. */
977ZSTDLIB_API size_t ZSTD_CCtx_setPledgedSrcSize(ZSTD_CCtx* cctx, unsigned long long pledgedSrcSize);
978
979/*! ZSTD_CCtx_loadDictionary() :
980 * Create an internal CDict from dict buffer.
981 * Decompression will have to use same buffer.
982 * @result : 0, or an error code (which can be tested with ZSTD_isError()).
Yann Collet78b82342017-06-20 14:26:48 -0700983 * Special : Adding a NULL (or 0-size) dictionary invalidates any previous dictionary,
Yann Colletbf991502017-06-19 12:56:25 -0700984 * meaning "return to no-dictionary mode".
985 * Note 1 : Dictionary content will be copied internally,
986 * except if ZSTD_p_refDictContent is set.
987 * Note 2 : Loading a dictionary involves building tables, which are dependent on compression parameters.
Yann Collet78b82342017-06-20 14:26:48 -0700988 * For this reason, compression parameters cannot be changed anymore after loading a dictionary.
Yann Colletbf991502017-06-19 12:56:25 -0700989 * It's also a CPU-heavy operation, with non-negligible impact on latency.
990 * Note 3 : Dictionary will be used for all future compression jobs.
991 * To return to "no-dictionary" situation, load a NULL dictionary */
992ZSTDLIB_API size_t ZSTD_CCtx_loadDictionary(ZSTD_CCtx* cctx, const void* dict, size_t dictSize);
993
994/*! ZSTD_CCtx_refCDict() :
995 * Ref a prepared dictionary, to be used for all next compression jobs.
996 * Note that compression parameters are enforced from within CDict,
997 * and supercede any compression parameter previously set within CCtx.
998 * The dictionary will remain valid for future compression jobs using same CCtx.
999 * @result : 0, or an error code (which can be tested with ZSTD_isError()).
1000 * Special : adding a NULL CDict means "return to no-dictionary mode".
1001 * Note 1 : Currently, only one dictionary can be managed.
1002 * Adding a new dictionary effectively "discards" any previous one.
1003 * Note 2 : CDict is just referenced, its lifetime must outlive CCtx.
1004 */
1005ZSTDLIB_API size_t ZSTD_CCtx_refCDict(ZSTD_CCtx* cctx, const ZSTD_CDict* cdict);
1006
1007/*! ZSTD_CCtx_refPrefix() :
1008 * Reference a prefix (content-only dictionary) to bootstrap next compression job.
1009 * Decompression will have to use same prefix.
1010 * Prefix is only used once. Tables are discarded at end of compression job.
1011 * If there is a need to use same prefix multiple times, consider embedding it into a ZSTD_CDict.
1012 * @result : 0, or an error code (which can be tested with ZSTD_isError()).
1013 * Special : Adding a NULL (or 0-size) dictionary invalidates any previous prefix, meaning "return to no-dictionary mode".
1014 * Note 1 : Prefix buffer is referenced. It must outlive compression job.
1015 * Note 2 : Referencing a prefix involves building tables, which are dependent on compression parameters.
1016 * It's a CPU-heavy operation, with non-negligible impact on latency. */
Yann Collet7bd1a292017-06-21 11:50:33 -07001017ZSTDLIB_API size_t ZSTD_CCtx_refPrefix(ZSTD_CCtx* cctx, const void* prefix, size_t prefixSize); /* Not ready yet ! <===================================== */
Yann Colletbf991502017-06-19 12:56:25 -07001018
1019
1020
1021typedef enum {
1022 ZSTD_e_continue=0, /* collect more data, encoder transparently decides when to output result, for optimal conditions */
1023 ZSTD_e_flush, /* flush any data provided so far - frame will continue, future data can still reference previous data for better compression */
1024 ZSTD_e_end /* flush any remaining data and ends current frame. Any future compression starts a new frame. */
1025} ZSTD_EndDirective;
1026
Yann Collet78b82342017-06-20 14:26:48 -07001027/*! ZSTD_compress_generic() :
Yann Colletbf991502017-06-19 12:56:25 -07001028 * Behave about the same as ZSTD_compressStream. To note :
Yann Collet78b82342017-06-20 14:26:48 -07001029 * - Compression parameters are pushed into CCtx before starting compression, using ZSTD_CCtx_setParameter()
Yann Colletbf991502017-06-19 12:56:25 -07001030 * - Compression parameters cannot be changed once compression is started.
1031 * - *dstPos must be <= dstCapacity, *srcPos must be <= srcSize
1032 * - *dspPos and *srcPos will be updated. They are guaranteed to remain below their respective limit.
1033 * - @return provides the minimum amount of data still to flush from internal buffers
1034 * or an error code, which can be tested using ZSTD_isError().
Yann Collet78b82342017-06-20 14:26:48 -07001035 * if @return != 0, flush is not fully completed, there is some data left within internal buffers.
Yann Colletbf991502017-06-19 12:56:25 -07001036 * - after a ZSTD_e_end directive, if internal buffer is not fully flushed,
Yann Collet78b82342017-06-20 14:26:48 -07001037 * only ZSTD_e_end or ZSTD_e_flush operations are allowed.
Yann Colletbf991502017-06-19 12:56:25 -07001038 * It is necessary to fully flush internal buffers
1039 * before starting a new compression job, or changing compression parameters.
1040 */
1041ZSTDLIB_API size_t ZSTD_compress_generic (ZSTD_CCtx* cctx,
1042 ZSTD_outBuffer* output,
1043 ZSTD_inBuffer* input,
1044 ZSTD_EndDirective endOp);
1045
1046/*! ZSTD_CCtx_reset() :
1047 * Return a CCtx to clean state.
1048 * Useful after an error, or to interrupt an ongoing compression job and start a new one.
1049 * Any internal data not yet flushed is cancelled.
1050 * Dictionary (if any) is dropped.
1051 * It's possible to modify compression parameters after a reset.
1052 */
1053ZSTDLIB_API void ZSTD_CCtx_reset(ZSTD_CCtx* cctx); /* Not ready yet ! */
1054
1055
1056/*! ZSTD_compress_generic_simpleArgs() :
1057 * Same as ZSTD_compress_generic(),
1058 * but using only integral types as arguments.
1059 * Argument list is larger and less expressive than ZSTD_{in,out}Buffer,
1060 * but can be helpful for binders from dynamic languages
1061 * which have troubles handling structures containing memory pointers.
1062 */
1063size_t ZSTD_compress_generic_simpleArgs (
1064 ZSTD_CCtx* cctx,
1065 void* dst, size_t dstCapacity, size_t* dstPos,
1066 const void* src, size_t srcSize, size_t* srcPos,
1067 ZSTD_EndDirective endOp);
1068
1069
Yann Colletbf991502017-06-19 12:56:25 -07001070
inikep82057aa2016-10-06 13:23:52 +02001071/**
1072 Block functions
1073
1074 Block functions produce and decode raw zstd blocks, without frame metadata.
Yann Colletcf05b9d2016-07-18 16:52:10 +02001075 Frame metadata cost is typically ~18 bytes, which can be non-negligible for very small blocks (< 100 bytes).
Yann Colletd3b7f8d2016-06-04 19:47:02 +02001076 User will have to take in charge required information to regenerate data, such as compressed and content sizes.
1077
1078 A few rules to respect :
Yann Colletf246cf52016-07-06 20:30:52 +02001079 - Compressing and decompressing require a context structure
Yann Colletd3b7f8d2016-06-04 19:47:02 +02001080 + Use ZSTD_createCCtx() and ZSTD_createDCtx()
1081 - It is necessary to init context before starting
Yann Colletaf4f45b2017-04-18 03:17:44 -07001082 + compression : any ZSTD_compressBegin*() variant, including with dictionary
1083 + decompression : any ZSTD_decompressBegin*() variant, including with dictionary
1084 + copyCCtx() and copyDCtx() can be used too
Yann Colletfa3671e2017-05-19 10:51:30 -07001085 - Block size is limited, it must be <= ZSTD_getBlockSize() <= ZSTD_BLOCKSIZE_MAX
Yann Colletaf4f45b2017-04-18 03:17:44 -07001086 + If input is larger than a block size, it's necessary to split input data into multiple blocks
1087 + For inputs larger than a single block size, consider using the regular ZSTD_compress() instead.
1088 Frame metadata is not that costly, and quickly becomes negligible as source size grows larger.
Yann Colletd3b7f8d2016-06-04 19:47:02 +02001089 - When a block is considered not compressible enough, ZSTD_compressBlock() result will be zero.
1090 In which case, nothing is produced into `dst`.
1091 + User must test for such outcome and deal directly with uncompressed data
Yann Colletf246cf52016-07-06 20:30:52 +02001092 + ZSTD_decompressBlock() doesn't accept uncompressed data as input !!!
Yann Colletaf4f45b2017-04-18 03:17:44 -07001093 + In case of multiple successive blocks, should some of them be uncompressed,
1094 decoder must be informed of their existence in order to follow proper history.
1095 Use ZSTD_insertBlock() for such a case.
Yann Colletd3b7f8d2016-06-04 19:47:02 +02001096*/
1097
Yann Colletfa3671e2017-05-19 10:51:30 -07001098#define ZSTD_BLOCKSIZELOG_MAX 17
1099#define ZSTD_BLOCKSIZE_MAX (1<<ZSTD_BLOCKSIZELOG_MAX) /* define, for static allocation */
inikep82057aa2016-10-06 13:23:52 +02001100/*===== Raw zstd block functions =====*/
Yann Colletfa3671e2017-05-19 10:51:30 -07001101ZSTDLIB_API size_t ZSTD_getBlockSize (const ZSTD_CCtx* cctx);
Yann Colletd3b7f8d2016-06-04 19:47:02 +02001102ZSTDLIB_API size_t ZSTD_compressBlock (ZSTD_CCtx* cctx, void* dst, size_t dstCapacity, const void* src, size_t srcSize);
1103ZSTDLIB_API size_t ZSTD_decompressBlock(ZSTD_DCtx* dctx, void* dst, size_t dstCapacity, const void* src, size_t srcSize);
Yann Colletd5c5a772016-07-19 15:06:55 +02001104ZSTDLIB_API size_t ZSTD_insertBlock(ZSTD_DCtx* dctx, const void* blockStart, size_t blockSize); /**< insert block into `dctx` history. Useful for uncompressed blocks */
Yann Colletd3b7f8d2016-06-04 19:47:02 +02001105
1106
Nick Terrell05c00f22016-11-29 11:46:37 -08001107#endif /* ZSTD_H_ZSTD_STATIC_LINKING_ONLY */
Yann Colletd3b7f8d2016-06-04 19:47:02 +02001108
Yann Collet4856a002015-01-24 01:58:16 +01001109#if defined (__cplusplus)
1110}
1111#endif