blob: 3852b720012e64476b3eb817ab145c15b48af143 [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 ===== */
22#if defined(__GNUC__) && (__GNUC__ >= 4)
Nick Terrell8de46ab2016-12-16 13:27:30 -080023# define ZSTDLIB_VISIBILITY __attribute__ ((visibility ("default")))
Christophe Chevalierc6e84532015-12-07 17:44:09 +010024#else
Nick Terrell8de46ab2016-12-16 13:27:30 -080025# define ZSTDLIB_VISIBILITY
26#endif
27#if defined(ZSTD_DLL_EXPORT) && (ZSTD_DLL_EXPORT==1)
28# define ZSTDLIB_API __declspec(dllexport) ZSTDLIB_VISIBILITY
29#elif defined(ZSTD_DLL_IMPORT) && (ZSTD_DLL_IMPORT==1)
30# 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.*/
31#else
32# define ZSTDLIB_API ZSTDLIB_VISIBILITY
Christophe Chevalierc6e84532015-12-07 17:44:09 +010033#endif
34
35
inikep2d261332016-10-06 16:28:21 +020036/*******************************************************************************************************
37 Introduction
38
Yann Collet37d13002016-10-24 17:22:12 -070039 zstd, short for Zstandard, is a fast lossless compression algorithm, targeting real-time compression scenarios
inikep2d261332016-10-06 16:28:21 +020040 at zlib-level and better compression ratios. The zstd compression library provides in-memory compression and
41 decompression functions. The library supports compression levels from 1 up to ZSTD_maxCLevel() which is 22.
Anders Oleson517577b2017-02-20 12:08:59 -080042 Levels >= 20, labeled `--ultra`, should be used with caution, as they require more memory.
inikep2d261332016-10-06 16:28:21 +020043 Compression can be done in:
44 - a single step (described as Simple API)
45 - a single step, reusing a context (described as Explicit memory management)
Yann Collet37d13002016-10-24 17:22:12 -070046 - unbounded multiple steps (described as Streaming compression)
inikep2d261332016-10-06 16:28:21 +020047 The compression ratio achievable on small data can be highly improved using compression with a dictionary in:
48 - a single step (described as Simple dictionary API)
Przemyslaw Skibinski984b66c2016-10-24 15:59:51 +020049 - a single step, reusing a dictionary (described as Fast dictionary API)
inikep2d261332016-10-06 16:28:21 +020050
Yann Collet37d13002016-10-24 17:22:12 -070051 Advanced experimental functions can be accessed using #define ZSTD_STATIC_LINKING_ONLY before including zstd.h.
52 These APIs shall never be used with a dynamic library.
inikep2d261332016-10-06 16:28:21 +020053 They are not "stable", their definition may change in the future. Only static linking is allowed.
54*********************************************************************************************************/
55
56/*------ Version ------*/
Yann Collet901e85f2016-08-31 07:51:25 -070057#define ZSTD_VERSION_MAJOR 1
Yann Collet1eb2fdc2016-09-18 12:21:47 +020058#define ZSTD_VERSION_MINOR 1
Yann Colletf3dfcdc2017-03-21 12:18:28 -070059#define ZSTD_VERSION_RELEASE 5
Yann Collete02808f2016-04-20 22:46:16 +020060
61#define ZSTD_LIB_VERSION ZSTD_VERSION_MAJOR.ZSTD_VERSION_MINOR.ZSTD_VERSION_RELEASE
62#define ZSTD_QUOTE(str) #str
63#define ZSTD_EXPAND_AND_QUOTE(str) ZSTD_QUOTE(str)
64#define ZSTD_VERSION_STRING ZSTD_EXPAND_AND_QUOTE(ZSTD_LIB_VERSION)
65
Yann Collet213089c2015-06-18 07:43:16 -080066#define ZSTD_VERSION_NUMBER (ZSTD_VERSION_MAJOR *100*100 + ZSTD_VERSION_MINOR *100 + ZSTD_VERSION_RELEASE)
Yann Collet379908b2016-12-06 10:36:15 -080067ZSTDLIB_API unsigned ZSTD_versionNumber(void); /**< library version number; to be used when checking dll version */
Yann Collet4856a002015-01-24 01:58:16 +010068
69
inikep82057aa2016-10-06 13:23:52 +020070/***************************************
Yann Colletcf05b9d2016-07-18 16:52:10 +020071* Simple API
Yann Collet7010c272015-10-21 09:07:25 +010072***************************************/
Yann Collet953ce722016-02-04 15:28:14 +010073/*! ZSTD_compress() :
Yann Collet71ddeb62017-04-20 22:54:54 -070074 * Compresses `src` content as a single zstd compressed frame into already allocated `dst`.
75 * Hint : compression runs faster if `dstCapacity` >= `ZSTD_compressBound(srcSize)`.
76 * @return : compressed size written into `dst` (<= `dstCapacity),
77 * or an error code if it fails (which can be tested using ZSTD_isError()). */
Yann Colletcf05b9d2016-07-18 16:52:10 +020078ZSTDLIB_API size_t ZSTD_compress( void* dst, size_t dstCapacity,
79 const void* src, size_t srcSize,
80 int compressionLevel);
Yann Collet4856a002015-01-24 01:58:16 +010081
Yann Collet953ce722016-02-04 15:28:14 +010082/*! ZSTD_decompress() :
Yann Collet71ddeb62017-04-20 22:54:54 -070083 * `compressedSize` : must be the _exact_ size of some number of compressed and/or skippable frames.
84 * `dstCapacity` is an upper bound of originalSize.
85 * If user cannot imply a maximum upper bound, it's better to use streaming mode to decompress data.
86 * @return : the number of bytes decompressed into `dst` (<= `dstCapacity`),
87 * or an errorCode if it fails (which can be tested using ZSTD_isError()). */
Yann Collet7d968c72016-02-03 02:11:32 +010088ZSTDLIB_API size_t ZSTD_decompress( void* dst, size_t dstCapacity,
Christophe Chevalierc6e84532015-12-07 17:44:09 +010089 const void* src, size_t compressedSize);
Yann Collet4856a002015-01-24 01:58:16 +010090
Yann Colletac175d42016-09-13 00:51:47 +020091/*! ZSTD_getDecompressedSize() :
Yann Collet71ddeb62017-04-20 22:54:54 -070092 * NOTE: This function is planned to be obsolete, in favour of ZSTD_getFrameContentSize.
93 * ZSTD_getFrameContentSize functions the same way, returning the decompressed size of a single
94 * frame, but distinguishes empty frames from frames with an unknown size, or errors.
95 *
96 * Additionally, ZSTD_findDecompressedSize can be used instead. It can handle multiple
97 * concatenated frames in one buffer, and so is more general.
98 * As a result however, it requires more computation and entire frames to be passed to it,
99 * as opposed to ZSTD_getFrameContentSize which requires only a single frame's header.
100 *
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.
103 * note 1 : decompressed size is an optional field, that may not be present, especially in streaming mode.
104 * When `return==0`, data to decompress could be any size.
105 * In which case, it's necessary to use streaming mode to decompress data.
106 * Optionally, application can still use ZSTD_decompress() while relying on implied limits.
107 * (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.
115 * note 5 : when `return==0`, if precise failure cause is needed, use ZSTD_getFrameParams() 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 Collet7d968c72016-02-03 02:11:32 +0100140ZSTDLIB_API size_t ZSTD_compressCCtx(ZSTD_CCtx* ctx, void* dst, size_t dstCapacity, const void* src, size_t srcSize, int compressionLevel);
Yann Collet4856a002015-01-24 01:58:16 +0100141
Sean Purcelldec2b962017-03-14 11:24:09 -0700142/*= Decompression context
Yann Collet71ddeb62017-04-20 22:54:54 -0700143 * When decompressing many times,
144 * it is recommended to allocate a context just once, and re-use it for each successive compression operation.
145 * This will make workload friendlier for system's memory.
146 * Use one context per thread for parallel execution in multi-threaded environments. */
Yann Collet87c18b22016-08-26 01:43:47 +0200147typedef struct ZSTD_DCtx_s ZSTD_DCtx;
Yann Colletecd651b2016-01-07 15:35:18 +0100148ZSTDLIB_API ZSTD_DCtx* ZSTD_createDCtx(void);
Yann Colletd469a982016-07-28 03:47:45 +0200149ZSTDLIB_API size_t ZSTD_freeDCtx(ZSTD_DCtx* dctx);
Yann Colletecd651b2016-01-07 15:35:18 +0100150
inikep82057aa2016-10-06 13:23:52 +0200151/*! ZSTD_decompressDCtx() :
Yann Collet71ddeb62017-04-20 22:54:54 -0700152 * Same as ZSTD_decompress(), requires an allocated ZSTD_DCtx (see ZSTD_createDCtx()). */
Yann Collet7d968c72016-02-03 02:11:32 +0100153ZSTDLIB_API size_t ZSTD_decompressDCtx(ZSTD_DCtx* ctx, void* dst, size_t dstCapacity, const void* src, size_t srcSize);
154
155
inikep82057aa2016-10-06 13:23:52 +0200156/**************************
Yann Collet302fb532016-06-07 12:16:49 +0200157* Simple dictionary API
158***************************/
Yann Collet953ce722016-02-04 15:28:14 +0100159/*! ZSTD_compress_usingDict() :
Yann Colletd469a982016-07-28 03:47:45 +0200160* Compression using a predefined Dictionary (see dictBuilder/zdict.h).
Nick Terrelld82efd82016-11-02 16:47:53 -0700161* Note : This function loads the dictionary, resulting in significant startup delay.
162* Note : When `dict == NULL || dictSize < 8` no dictionary is used. */
Yann Collet7d968c72016-02-03 02:11:32 +0100163ZSTDLIB_API size_t ZSTD_compress_usingDict(ZSTD_CCtx* ctx,
164 void* dst, size_t dstCapacity,
165 const void* src, size_t srcSize,
166 const void* dict,size_t dictSize,
167 int compressionLevel);
168
Yann Collet953ce722016-02-04 15:28:14 +0100169/*! ZSTD_decompress_usingDict() :
Yann Colletd469a982016-07-28 03:47:45 +0200170* Decompression using a predefined Dictionary (see dictBuilder/zdict.h).
Yann Collet81e13ef2016-06-07 00:51:51 +0200171* Dictionary must be identical to the one used during compression.
Nick Terrelld82efd82016-11-02 16:47:53 -0700172* Note : This function loads the dictionary, resulting in significant startup delay.
173* Note : When `dict == NULL || dictSize < 8` no dictionary is used. */
Yann Collet7d968c72016-02-03 02:11:32 +0100174ZSTDLIB_API size_t ZSTD_decompress_usingDict(ZSTD_DCtx* dctx,
175 void* dst, size_t dstCapacity,
176 const void* src, size_t srcSize,
177 const void* dict,size_t dictSize);
Yann Colletecd651b2016-01-07 15:35:18 +0100178
Yann Collet4856a002015-01-24 01:58:16 +0100179
inikep82057aa2016-10-06 13:23:52 +0200180/****************************
Przemyslaw Skibinski984b66c2016-10-24 15:59:51 +0200181* Fast dictionary API
Yann Collet302fb532016-06-07 12:16:49 +0200182****************************/
inikep2d261332016-10-06 16:28:21 +0200183typedef struct ZSTD_CDict_s ZSTD_CDict;
184
Yann Collet302fb532016-06-07 12:16:49 +0200185/*! ZSTD_createCDict() :
Yann Collet37d13002016-10-24 17:22:12 -0700186* When compressing multiple messages / blocks with the same dictionary, it's recommended to load it just once.
187* ZSTD_createCDict() will create a digested dictionary, ready to start future compression operations without startup delay.
188* ZSTD_CDict can be created once and used by multiple threads concurrently, as its usage is read-only.
Yann Collet1f57c2e2016-12-21 16:20:11 +0100189* `dictBuffer` can be released after ZSTD_CDict creation, as its content is copied within CDict */
190ZSTDLIB_API ZSTD_CDict* ZSTD_createCDict(const void* dictBuffer, size_t dictSize, int compressionLevel);
inikep2d261332016-10-06 16:28:21 +0200191
192/*! ZSTD_freeCDict() :
Nick Terrelld82efd82016-11-02 16:47:53 -0700193* Function frees memory allocated by ZSTD_createCDict(). */
Yann Collet302fb532016-06-07 12:16:49 +0200194ZSTDLIB_API size_t ZSTD_freeCDict(ZSTD_CDict* CDict);
195
196/*! ZSTD_compress_usingCDict() :
Yann Collet4f818182017-04-17 17:57:35 -0700197 * Compression using a digested Dictionary.
198 * Faster startup than ZSTD_compress_usingDict(), recommended when same dictionary is used multiple times.
199 * Note that compression level is decided during dictionary creation.
200 * Frame parameters are hardcoded (dictID=yes, contentSize=yes, checksum=no) */
Yann Collet302fb532016-06-07 12:16:49 +0200201ZSTDLIB_API size_t ZSTD_compress_usingCDict(ZSTD_CCtx* cctx,
202 void* dst, size_t dstCapacity,
203 const void* src, size_t srcSize,
204 const ZSTD_CDict* cdict);
205
inikep2d261332016-10-06 16:28:21 +0200206
207typedef struct ZSTD_DDict_s ZSTD_DDict;
208
Yann Collet302fb532016-06-07 12:16:49 +0200209/*! ZSTD_createDDict() :
210* Create a digested dictionary, ready to start decompression operation without startup delay.
Yann Collet4e5eea62016-12-21 16:44:35 +0100211* dictBuffer can be released after DDict creation, as its content is copied inside DDict */
212ZSTDLIB_API ZSTD_DDict* ZSTD_createDDict(const void* dictBuffer, size_t dictSize);
inikep2d261332016-10-06 16:28:21 +0200213
214/*! ZSTD_freeDDict() :
215* Function frees memory allocated with ZSTD_createDDict() */
Yann Collet302fb532016-06-07 12:16:49 +0200216ZSTDLIB_API size_t ZSTD_freeDDict(ZSTD_DDict* ddict);
217
218/*! ZSTD_decompress_usingDDict() :
Nick Terrelld82efd82016-11-02 16:47:53 -0700219* Decompression using a digested Dictionary.
Yann Colletcf05b9d2016-07-18 16:52:10 +0200220* Faster startup than ZSTD_decompress_usingDict(), recommended when same dictionary is used multiple times. */
Yann Collet302fb532016-06-07 12:16:49 +0200221ZSTDLIB_API size_t ZSTD_decompress_usingDDict(ZSTD_DCtx* dctx,
222 void* dst, size_t dstCapacity,
223 const void* src, size_t srcSize,
224 const ZSTD_DDict* ddict);
225
226
inikep82057aa2016-10-06 13:23:52 +0200227/****************************
Yann Collet7be46bf2016-08-19 18:39:36 +0200228* Streaming
229****************************/
230
231typedef struct ZSTD_inBuffer_s {
232 const void* src; /**< start of input buffer */
233 size_t size; /**< size of input buffer */
234 size_t pos; /**< position where reading stopped. Will be updated. Necessarily 0 <= pos <= size */
235} ZSTD_inBuffer;
236
237typedef struct ZSTD_outBuffer_s {
238 void* dst; /**< start of output buffer */
239 size_t size; /**< size of output buffer */
240 size_t pos; /**< position where writing stopped. Will be updated. Necessarily 0 <= pos <= size */
241} ZSTD_outBuffer;
242
243
Yann Collet7be46bf2016-08-19 18:39:36 +0200244
inikepba1db372016-10-06 14:22:48 +0200245/*-***********************************************************************
Przemyslaw Skibinski984b66c2016-10-24 15:59:51 +0200246* Streaming compression - HowTo
Yann Collet7be46bf2016-08-19 18:39:36 +0200247*
248* A ZSTD_CStream object is required to track streaming operation.
249* Use ZSTD_createCStream() and ZSTD_freeCStream() to create/release resources.
250* ZSTD_CStream objects can be reused multiple times on consecutive compression operations.
Yann Collet37d13002016-10-24 17:22:12 -0700251* It is recommended to re-use ZSTD_CStream in situations where many streaming operations will be achieved consecutively,
252* since it will play nicer with system's memory, by re-using already allocated memory.
253* Use one separate ZSTD_CStream per thread for parallel execution.
Yann Collet7be46bf2016-08-19 18:39:36 +0200254*
Yann Collet37d13002016-10-24 17:22:12 -0700255* Start a new compression by initializing ZSTD_CStream.
Yann Collet7be46bf2016-08-19 18:39:36 +0200256* Use ZSTD_initCStream() to start a new compression operation.
Yann Collete795c8a2016-12-13 16:39:36 +0100257* Use ZSTD_initCStream_usingDict() or ZSTD_initCStream_usingCDict() for a compression which requires a dictionary (experimental section)
Yann Collet7be46bf2016-08-19 18:39:36 +0200258*
259* Use ZSTD_compressStream() repetitively to consume input stream.
Yann Colletfa72f6b2016-09-05 17:39:56 +0200260* The function will automatically update both `pos` fields.
Yann Collet7be46bf2016-08-19 18:39:36 +0200261* Note that it may not consume the entire input, in which case `pos < size`,
262* and it's up to the caller to present again remaining data.
Yann Collet4bf317d2016-08-28 07:43:34 -0700263* @return : a size hint, preferred nb of bytes to use as input for next function call
Yann Collet7be46bf2016-08-19 18:39:36 +0200264* or an error code, which can be tested using ZSTD_isError().
Yann Collete795c8a2016-12-13 16:39:36 +0100265* Note 1 : it's just a hint, to help latency a little, any other value will work fine.
266* Note 2 : size hint is guaranteed to be <= ZSTD_CStreamInSize()
Yann Collet7be46bf2016-08-19 18:39:36 +0200267*
Yann Collete795c8a2016-12-13 16:39:36 +0100268* At any moment, it's possible to flush whatever data remains within internal buffer, using ZSTD_flushStream().
Yann Collet7be46bf2016-08-19 18:39:36 +0200269* `output->pos` will be updated.
Yann Collete795c8a2016-12-13 16:39:36 +0100270* Note that some content might still be left within internal buffer if `output->size` is too small.
Yann Collet7be46bf2016-08-19 18:39:36 +0200271* @return : nb of bytes still present within internal buffer (0 if it's empty)
272* or an error code, which can be tested using ZSTD_isError().
273*
274* ZSTD_endStream() instructs to finish a frame.
275* It will perform a flush and write frame epilogue.
276* The epilogue is required for decoders to consider a frame completed.
277* Similar to ZSTD_flushStream(), it may not be able to flush the full content if `output->size` is too small.
278* In which case, call again ZSTD_endStream() to complete the flush.
Yann Collete795c8a2016-12-13 16:39:36 +0100279* @return : nb of bytes still present within internal buffer (0 if it's empty, hence compression completed)
Yann Collet7be46bf2016-08-19 18:39:36 +0200280* or an error code, which can be tested using ZSTD_isError().
281*
282* *******************************************************************/
283
284typedef struct ZSTD_CStream_s ZSTD_CStream;
Sean Purcelldec2b962017-03-14 11:24:09 -0700285/*===== ZSTD_CStream management functions =====*/
Yann Collet70e3b312016-08-23 01:18:06 +0200286ZSTDLIB_API ZSTD_CStream* ZSTD_createCStream(void);
287ZSTDLIB_API size_t ZSTD_freeCStream(ZSTD_CStream* zcs);
Yann Collete795c8a2016-12-13 16:39:36 +0100288
Sean Purcelldec2b962017-03-14 11:24:09 -0700289/*===== Streaming compression functions =====*/
Yann Collet70e3b312016-08-23 01:18:06 +0200290ZSTDLIB_API size_t ZSTD_initCStream(ZSTD_CStream* zcs, int compressionLevel);
291ZSTDLIB_API size_t ZSTD_compressStream(ZSTD_CStream* zcs, ZSTD_outBuffer* output, ZSTD_inBuffer* input);
292ZSTDLIB_API size_t ZSTD_flushStream(ZSTD_CStream* zcs, ZSTD_outBuffer* output);
293ZSTDLIB_API size_t ZSTD_endStream(ZSTD_CStream* zcs, ZSTD_outBuffer* output);
Yann Collet7be46bf2016-08-19 18:39:36 +0200294
inikep82057aa2016-10-06 13:23:52 +0200295ZSTDLIB_API size_t ZSTD_CStreamInSize(void); /**< recommended size for input buffer */
296ZSTDLIB_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 +0200297
Yann Collet7be46bf2016-08-19 18:39:36 +0200298
inikep82057aa2016-10-06 13:23:52 +0200299
inikepba1db372016-10-06 14:22:48 +0200300/*-***************************************************************************
Przemyslaw Skibinski984b66c2016-10-24 15:59:51 +0200301* Streaming decompression - HowTo
Yann Collet7be46bf2016-08-19 18:39:36 +0200302*
303* A ZSTD_DStream object is required to track streaming operations.
304* Use ZSTD_createDStream() and ZSTD_freeDStream() to create/release resources.
Yann Collet17e482e2016-08-23 16:58:10 +0200305* ZSTD_DStream objects can be re-used multiple times.
Yann Collet7be46bf2016-08-19 18:39:36 +0200306*
307* Use ZSTD_initDStream() to start a new decompression operation,
308* or ZSTD_initDStream_usingDict() if decompression requires a dictionary.
Yann Collet7c83dfd2016-09-05 19:47:43 +0200309* @return : recommended first input size
Yann Collet7be46bf2016-08-19 18:39:36 +0200310*
311* Use ZSTD_decompressStream() repetitively to consume your input.
Yann Colletfa72f6b2016-09-05 17:39:56 +0200312* The function will update both `pos` fields.
Yann Colletb3060f72016-09-09 16:44:16 +0200313* If `input.pos < input.size`, some input has not been consumed.
Yann Collet1d4208c2016-09-06 05:16:40 +0200314* It's up to the caller to present again remaining data.
Yann Colletb3060f72016-09-09 16:44:16 +0200315* If `output.pos < output.size`, decoder has flushed everything it could.
Yann Collet7be46bf2016-08-19 18:39:36 +0200316* @return : 0 when a frame is completely decoded and fully flushed,
Yann Colletfa72f6b2016-09-05 17:39:56 +0200317* an error code, which can be tested using ZSTD_isError(),
Yann Collet9ffbeea2016-12-02 18:37:38 -0800318* any other value > 0, which means there is still some decoding to do to complete current frame.
319* 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 +0200320* *******************************************************************************/
321
322typedef struct ZSTD_DStream_s ZSTD_DStream;
Sean Purcelldec2b962017-03-14 11:24:09 -0700323/*===== ZSTD_DStream management functions =====*/
Yann Collet70e3b312016-08-23 01:18:06 +0200324ZSTDLIB_API ZSTD_DStream* ZSTD_createDStream(void);
325ZSTDLIB_API size_t ZSTD_freeDStream(ZSTD_DStream* zds);
Yann Collete795c8a2016-12-13 16:39:36 +0100326
Sean Purcelldec2b962017-03-14 11:24:09 -0700327/*===== Streaming decompression functions =====*/
inikep82057aa2016-10-06 13:23:52 +0200328ZSTDLIB_API size_t ZSTD_initDStream(ZSTD_DStream* zds);
329ZSTDLIB_API size_t ZSTD_decompressStream(ZSTD_DStream* zds, ZSTD_outBuffer* output, ZSTD_inBuffer* input);
Yann Collet7be46bf2016-08-19 18:39:36 +0200330
Yann Collet70e3b312016-08-23 01:18:06 +0200331ZSTDLIB_API size_t ZSTD_DStreamInSize(void); /*!< recommended size for input buffer */
Yann Collet01c19922016-09-08 19:29:04 +0200332ZSTDLIB_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 +0200333
Nick Terrell05c00f22016-11-29 11:46:37 -0800334#endif /* ZSTD_H_235446 */
Yann Collet7be46bf2016-08-19 18:39:36 +0200335
336
Nick Terrell05c00f22016-11-29 11:46:37 -0800337#if defined(ZSTD_STATIC_LINKING_ONLY) && !defined(ZSTD_H_ZSTD_STATIC_LINKING_ONLY)
338#define ZSTD_H_ZSTD_STATIC_LINKING_ONLY
Yann Colletd3b7f8d2016-06-04 19:47:02 +0200339
inikep82057aa2016-10-06 13:23:52 +0200340/****************************************************************************************
inikep2d261332016-10-06 16:28:21 +0200341 * START OF ADVANCED AND EXPERIMENTAL FUNCTIONS
Yann Colletd3b7f8d2016-06-04 19:47:02 +0200342 * The definitions in this section are considered experimental.
Yann Colleta49e0662016-06-21 11:54:03 +0200343 * They should never be used with a dynamic library, as they may change in the future.
Yann Colletd3b7f8d2016-06-04 19:47:02 +0200344 * They are provided for advanced usages.
345 * Use them only in association with static linking.
inikep82057aa2016-10-06 13:23:52 +0200346 * ***************************************************************************************/
Yann Colletd3b7f8d2016-06-04 19:47:02 +0200347
inikep82057aa2016-10-06 13:23:52 +0200348/* --- Constants ---*/
Yann Collet4e5eea62016-12-21 16:44:35 +0100349#define ZSTD_MAGICNUMBER 0xFD2FB528 /* >= v0.8.0 */
Yann Colletd3b7f8d2016-06-04 19:47:02 +0200350#define ZSTD_MAGIC_SKIPPABLE_START 0x184D2A50U
351
Sean Purcell2db72492017-02-09 10:50:43 -0800352#define ZSTD_CONTENTSIZE_UNKNOWN (0ULL - 1)
353#define ZSTD_CONTENTSIZE_ERROR (0ULL - 2)
354
Sean Purcelld44703d2017-03-01 14:36:25 -0800355#define ZSTD_WINDOWLOG_MAX_32 27
Yann Colleted3845d2016-07-08 12:57:10 +0200356#define ZSTD_WINDOWLOG_MAX_64 27
Nick Terrelle65aab82017-03-08 15:40:13 -0800357#define ZSTD_WINDOWLOG_MAX ((unsigned)(sizeof(size_t) == 4 ? ZSTD_WINDOWLOG_MAX_32 : ZSTD_WINDOWLOG_MAX_64))
Yann Colletcf409a72016-09-26 16:41:05 +0200358#define ZSTD_WINDOWLOG_MIN 10
Yann Colletd3b7f8d2016-06-04 19:47:02 +0200359#define ZSTD_HASHLOG_MAX ZSTD_WINDOWLOG_MAX
Yann Colletcf409a72016-09-26 16:41:05 +0200360#define ZSTD_HASHLOG_MIN 6
361#define ZSTD_CHAINLOG_MAX (ZSTD_WINDOWLOG_MAX+1)
362#define ZSTD_CHAINLOG_MIN ZSTD_HASHLOG_MIN
Yann Colletd3b7f8d2016-06-04 19:47:02 +0200363#define ZSTD_HASHLOG3_MAX 17
Yann Colletd3b7f8d2016-06-04 19:47:02 +0200364#define ZSTD_SEARCHLOG_MAX (ZSTD_WINDOWLOG_MAX-1)
365#define ZSTD_SEARCHLOG_MIN 1
Yann Collet0e07bf32016-09-07 06:33:02 +0200366#define ZSTD_SEARCHLENGTH_MAX 7 /* only for ZSTD_fast, other strategies are limited to 6 */
367#define ZSTD_SEARCHLENGTH_MIN 3 /* only for ZSTD_btopt, other strategies are limited to 4 */
Yann Colletd3b7f8d2016-06-04 19:47:02 +0200368#define ZSTD_TARGETLENGTH_MIN 4
369#define ZSTD_TARGETLENGTH_MAX 999
370
Yann Collet673f0d72016-06-06 00:26:38 +0200371#define ZSTD_FRAMEHEADERSIZE_MAX 18 /* for static allocation */
Yann Colletba75e9d2016-12-21 19:57:18 +0100372#define ZSTD_FRAMEHEADERSIZE_MIN 6
Yann Collet7c83dfd2016-09-05 19:47:43 +0200373static const size_t ZSTD_frameHeaderSize_prefix = 5;
Yann Colletba75e9d2016-12-21 19:57:18 +0100374static const size_t ZSTD_frameHeaderSize_min = ZSTD_FRAMEHEADERSIZE_MIN;
Yann Collet673f0d72016-06-06 00:26:38 +0200375static const size_t ZSTD_frameHeaderSize_max = ZSTD_FRAMEHEADERSIZE_MAX;
376static const size_t ZSTD_skippableHeaderSize = 8; /* magic number + skippable frame length */
377
Yann Colletd3b7f8d2016-06-04 19:47:02 +0200378
Przemyslaw Skibinski984b66c2016-10-24 15:59:51 +0200379/*--- Advanced types ---*/
Przemyslaw Skibinski5c5f01f2016-10-25 12:25:07 +0200380typedef enum { ZSTD_fast, ZSTD_dfast, ZSTD_greedy, ZSTD_lazy, ZSTD_lazy2, ZSTD_btlazy2, ZSTD_btopt, ZSTD_btopt2 } ZSTD_strategy; /* from faster to stronger */
Yann Colletd3b7f8d2016-06-04 19:47:02 +0200381
382typedef struct {
Yann Collet655393c2016-08-14 00:16:20 +0200383 unsigned windowLog; /**< largest match distance : larger == more compression, more memory needed during decompression */
384 unsigned chainLog; /**< fully searched segment : larger == more compression, slower, more memory (useless for fast) */
385 unsigned hashLog; /**< dispatch table : larger == faster, more memory */
386 unsigned searchLog; /**< nb of searches : larger == more compression, slower */
387 unsigned searchLength; /**< match length searched : larger == faster decompression, sometimes less compression */
388 unsigned targetLength; /**< acceptable match size for optimal parser (only) : larger == more compression, slower */
Yann Colletd3b7f8d2016-06-04 19:47:02 +0200389 ZSTD_strategy strategy;
390} ZSTD_compressionParameters;
391
392typedef struct {
Yann Collet0ec6a952017-01-02 00:49:42 +0100393 unsigned contentSizeFlag; /**< 1: content size will be in frame header (when known) */
394 unsigned checksumFlag; /**< 1: generate a 32-bits checksum at end of frame, for error detection */
395 unsigned noDictIDFlag; /**< 1: no dictID will be saved into frame header (if dictionary compression) */
Yann Colletd3b7f8d2016-06-04 19:47:02 +0200396} ZSTD_frameParameters;
397
398typedef struct {
399 ZSTD_compressionParameters cParams;
400 ZSTD_frameParameters fParams;
401} ZSTD_parameters;
402
inikep82057aa2016-10-06 13:23:52 +0200403/*= Custom memory allocation functions */
Yann Colletd3b7f8d2016-06-04 19:47:02 +0200404typedef void* (*ZSTD_allocFunction) (void* opaque, size_t size);
405typedef void (*ZSTD_freeFunction) (void* opaque, void* address);
406typedef struct { ZSTD_allocFunction customAlloc; ZSTD_freeFunction customFree; void* opaque; } ZSTD_customMem;
407
Sean Purcell2db72492017-02-09 10:50:43 -0800408/***************************************
Sean Purcelld7bfcac2017-02-10 11:38:57 -0800409* Compressed size functions
410***************************************/
411
Sean Purcell9050e192017-02-22 12:12:32 -0800412/*! ZSTD_findFrameCompressedSize() :
Sean Purcell9757cc82017-02-22 12:27:15 -0800413 * `src` should point to the start of a ZSTD encoded frame or skippable frame
Sean Purcelld7bfcac2017-02-10 11:38:57 -0800414 * `srcSize` must be at least as large as the frame
415 * @return : the compressed size of the frame pointed to by `src`, suitable to pass to
416 * `ZSTD_decompress` or similar, or an error code if given invalid input. */
Sean Purcell9050e192017-02-22 12:12:32 -0800417ZSTDLIB_API size_t ZSTD_findFrameCompressedSize(const void* src, size_t srcSize);
Sean Purcelld7bfcac2017-02-10 11:38:57 -0800418
419/***************************************
Sean Purcell2db72492017-02-09 10:50:43 -0800420* Decompressed size functions
421***************************************/
422/*! ZSTD_getFrameContentSize() :
423* `src` should point to the start of a ZSTD encoded frame
Sean Purcell269b2cd2017-02-09 13:25:30 -0800424* `srcSize` must be at least as large as the frame header. A value greater than or equal
425* to `ZSTD_frameHeaderSize_max` is guaranteed to be large enough in all cases.
Sean Purcell2db72492017-02-09 10:50:43 -0800426* @return : decompressed size of the frame pointed to be `src` if known, otherwise
427* - ZSTD_CONTENTSIZE_UNKNOWN if the size cannot be determined
Anders Oleson517577b2017-02-20 12:08:59 -0800428* - ZSTD_CONTENTSIZE_ERROR if an error occurred (e.g. invalid magic number, srcSize too small) */
Sean Purcell2db72492017-02-09 10:50:43 -0800429ZSTDLIB_API unsigned long long ZSTD_getFrameContentSize(const void *src, size_t srcSize);
430
431/*! ZSTD_findDecompressedSize() :
432* `src` should point the start of a series of ZSTD encoded and/or skippable frames
433* `srcSize` must be the _exact_ size of this series
434* (i.e. there should be a frame boundary exactly `srcSize` bytes after `src`)
435* @return : the decompressed size of all data in the contained frames, as a 64-bit value _if known_
436* - if the decompressed size cannot be determined: ZSTD_CONTENTSIZE_UNKNOWN
437* - if an error occurred: ZSTD_CONTENTSIZE_ERROR
438*
439* note 1 : decompressed size is an optional field, that may not be present, especially in streaming mode.
440* When `return==ZSTD_CONTENTSIZE_UNKNOWN`, data to decompress could be any size.
441* In which case, it's necessary to use streaming mode to decompress data.
442* Optionally, application can still use ZSTD_decompress() while relying on implied limits.
443* (For example, data may be necessarily cut into blocks <= 16 KB).
444* note 2 : decompressed size is always present when compression is done with ZSTD_compress()
445* note 3 : decompressed size can be very large (64-bits value),
446* potentially larger than what local system can handle as a single memory segment.
447* In which case, it's necessary to use streaming mode to decompress data.
448* note 4 : If source is untrusted, decompressed size could be wrong or intentionally modified.
449* Always ensure result fits within application's authorized limits.
450* Each application can set its own limits.
451* note 5 : ZSTD_findDecompressedSize handles multiple frames, and so it must traverse the input to
452* read each contained frame header. This is efficient as most of the data is skipped,
453* however it does mean that all frame data must be present and valid. */
454ZSTDLIB_API unsigned long long ZSTD_findDecompressedSize(const void* src, size_t srcSize);
455
Yann Colletd3b7f8d2016-06-04 19:47:02 +0200456
inikep82057aa2016-10-06 13:23:52 +0200457/***************************************
Yann Collet81e13ef2016-06-07 00:51:51 +0200458* Advanced compression functions
Yann Colletd3b7f8d2016-06-04 19:47:02 +0200459***************************************/
Yann Collet3ae543c2016-07-11 03:12:17 +0200460/*! ZSTD_estimateCCtxSize() :
461 * Gives the amount of memory allocated for a ZSTD_CCtx given a set of compression parameters.
462 * `frameContentSize` is an optional parameter, provide `0` if unknown */
Yann Collet88472382016-07-14 17:05:38 +0200463ZSTDLIB_API size_t ZSTD_estimateCCtxSize(ZSTD_compressionParameters cParams);
Yann Collet3ae543c2016-07-11 03:12:17 +0200464
Yann Colletd3b7f8d2016-06-04 19:47:02 +0200465/*! ZSTD_createCCtx_advanced() :
466 * Create a ZSTD compression context using external alloc and free functions */
467ZSTDLIB_API ZSTD_CCtx* ZSTD_createCCtx_advanced(ZSTD_customMem customMem);
468
Yann Colletd7c65892016-09-15 02:50:27 +0200469/*! ZSTD_sizeofCCtx() :
470 * Gives the amount of memory used by a given ZSTD_CCtx */
471ZSTDLIB_API size_t ZSTD_sizeof_CCtx(const ZSTD_CCtx* cctx);
472
Yann Collet06e76972017-01-25 16:39:03 -0800473typedef enum {
Yann Collet14312d82017-02-23 23:42:12 -0800474 ZSTD_p_forceWindow, /* Force back-references to remain < windowSize, even when referencing Dictionary content (default:0) */
475 ZSTD_p_forceRawDict /* Force loading dictionary in "content-only" mode (no header analysis) */
Yann Collet06e76972017-01-25 16:39:03 -0800476} ZSTD_CCtxParameter;
Yann Collet4a62f792017-01-26 09:16:56 -0800477/*! ZSTD_setCCtxParameter() :
478 * Set advanced parameters, selected through enum ZSTD_CCtxParameter
479 * @result : 0, or an error code (which can be tested with ZSTD_isError()) */
Yann Colletef33d002017-01-26 12:24:21 -0800480ZSTDLIB_API size_t ZSTD_setCCtxParameter(ZSTD_CCtx* cctx, ZSTD_CCtxParameter param, unsigned value);
Yann Colletbb002742017-01-25 16:25:38 -0800481
Yann Collet1f57c2e2016-12-21 16:20:11 +0100482/*! ZSTD_createCDict_byReference() :
483 * Create a digested dictionary for compression
484 * Dictionary content is simply referenced, and therefore stays in dictBuffer.
485 * It is important that dictBuffer outlives CDict, it must remain read accessible throughout the lifetime of CDict */
486ZSTDLIB_API ZSTD_CDict* ZSTD_createCDict_byReference(const void* dictBuffer, size_t dictSize, int compressionLevel);
487
Yann Collet81e13ef2016-06-07 00:51:51 +0200488/*! ZSTD_createCDict_advanced() :
489 * Create a ZSTD_CDict using external alloc and free, and customized compression parameters */
Yann Collet1f57c2e2016-12-21 16:20:11 +0100490ZSTDLIB_API ZSTD_CDict* ZSTD_createCDict_advanced(const void* dict, size_t dictSize, unsigned byReference,
Yann Collet31533ba2017-04-27 00:29:04 -0700491 ZSTD_compressionParameters cParams, ZSTD_customMem customMem);
Yann Colletd3b7f8d2016-06-04 19:47:02 +0200492
Yann Colletd7c65892016-09-15 02:50:27 +0200493/*! ZSTD_sizeof_CDict() :
494 * Gives the amount of memory used by a given ZSTD_sizeof_CDict */
495ZSTDLIB_API size_t ZSTD_sizeof_CDict(const ZSTD_CDict* cdict);
Yann Collet8e0ee682016-07-11 13:09:52 +0200496
Yann Colletd3b7f8d2016-06-04 19:47:02 +0200497/*! ZSTD_getCParams() :
Yann Collet2b36b232016-12-13 17:59:55 +0100498* @return ZSTD_compressionParameters structure for a selected compression level and estimated srcSize.
499* `estimatedSrcSize` value is optional, select 0 if not known */
500ZSTDLIB_API ZSTD_compressionParameters ZSTD_getCParams(int compressionLevel, unsigned long long estimatedSrcSize, size_t dictSize);
501
502/*! ZSTD_getParams() :
503* same as ZSTD_getCParams(), but @return a full `ZSTD_parameters` object instead of sub-component `ZSTD_compressionParameters`.
504* All fields of `ZSTD_frameParameters` are set to default (0) */
505ZSTDLIB_API ZSTD_parameters ZSTD_getParams(int compressionLevel, unsigned long long estimatedSrcSize, size_t dictSize);
Yann Colletd3b7f8d2016-06-04 19:47:02 +0200506
Yann Collet3d2cd7f2016-06-27 15:12:26 +0200507/*! ZSTD_checkCParams() :
Yann Colletd3b7f8d2016-06-04 19:47:02 +0200508* Ensure param values remain within authorized range */
509ZSTDLIB_API size_t ZSTD_checkCParams(ZSTD_compressionParameters params);
510
Yann Collet3d2cd7f2016-06-27 15:12:26 +0200511/*! ZSTD_adjustCParams() :
Yann Colletd3b7f8d2016-06-04 19:47:02 +0200512* optimize params for a given `srcSize` and `dictSize`.
513* both values are optional, select `0` if unknown. */
Yann Collet52c04fe2016-07-07 11:53:18 +0200514ZSTDLIB_API ZSTD_compressionParameters ZSTD_adjustCParams(ZSTD_compressionParameters cPar, unsigned long long srcSize, size_t dictSize);
Yann Colletd3b7f8d2016-06-04 19:47:02 +0200515
516/*! ZSTD_compress_advanced() :
Yann Colletf4bd8572017-04-27 11:31:55 -0700517* Same as ZSTD_compress_usingDict(), with fine-tune control over each compression parameter */
518ZSTDLIB_API size_t ZSTD_compress_advanced (ZSTD_CCtx* cctx,
519 void* dst, size_t dstCapacity,
520 const void* src, size_t srcSize,
521 const void* dict,size_t dictSize,
522 ZSTD_parameters params);
523
524/*! ZSTD_compress_usingCDict_advanced() :
525* Same as ZSTD_compress_usingCDict(), with fine-tune control over frame parameters */
526ZSTDLIB_API size_t ZSTD_compress_usingCDict_advanced(ZSTD_CCtx* cctx,
527 void* dst, size_t dstCapacity,
528 const void* src, size_t srcSize,
529 const ZSTD_CDict* cdict, ZSTD_frameParameters fParams);
Yann Colletd3b7f8d2016-06-04 19:47:02 +0200530
Yann Collet45c03c52016-06-14 13:46:11 +0200531
Przemyslaw Skibinski984b66c2016-10-24 15:59:51 +0200532/*--- Advanced decompression functions ---*/
Yann Colletd3b7f8d2016-06-04 19:47:02 +0200533
Yann Collet179b1972016-11-02 17:30:49 -0700534/*! ZSTD_isFrame() :
535 * Tells if the content of `buffer` starts with a valid Frame Identifier.
536 * Note : Frame Identifier is 4 bytes. If `size < 4`, @return will always be 0.
537 * Note 2 : Legacy Frame Identifiers are considered valid only if Legacy Support is enabled.
538 * Note 3 : Skippable Frame Identifiers are considered valid. */
539ZSTDLIB_API unsigned ZSTD_isFrame(const void* buffer, size_t size);
540
Yann Colletd158c352016-07-11 13:46:25 +0200541/*! ZSTD_estimateDCtxSize() :
542 * Gives the potential amount of memory allocated to create a ZSTD_DCtx */
543ZSTDLIB_API size_t ZSTD_estimateDCtxSize(void);
544
Yann Collet81e13ef2016-06-07 00:51:51 +0200545/*! ZSTD_createDCtx_advanced() :
546 * Create a ZSTD decompression context using external alloc and free functions */
547ZSTDLIB_API ZSTD_DCtx* ZSTD_createDCtx_advanced(ZSTD_customMem customMem);
Yann Colletd3b7f8d2016-06-04 19:47:02 +0200548
Yann Colletd7c65892016-09-15 02:50:27 +0200549/*! ZSTD_sizeof_DCtx() :
Yann Colletd158c352016-07-11 13:46:25 +0200550 * Gives the amount of memory used by a given ZSTD_DCtx */
Yann Collet70e3b312016-08-23 01:18:06 +0200551ZSTDLIB_API size_t ZSTD_sizeof_DCtx(const ZSTD_DCtx* dctx);
Yann Collet8e0ee682016-07-11 13:09:52 +0200552
Yann Collet4e5eea62016-12-21 16:44:35 +0100553/*! ZSTD_createDDict_byReference() :
554 * Create a digested dictionary, ready to start decompression operation without startup delay.
555 * Dictionary content is simply referenced, and therefore stays in dictBuffer.
556 * It is important that dictBuffer outlives DDict, it must remain read accessible throughout the lifetime of DDict */
557ZSTDLIB_API ZSTD_DDict* ZSTD_createDDict_byReference(const void* dictBuffer, size_t dictSize);
558
Sean Purcelldec2b962017-03-14 11:24:09 -0700559/*! ZSTD_createDDict_advanced() :
560 * Create a ZSTD_DDict using external alloc and free, optionally by reference */
Yann Collet4e5eea62016-12-21 16:44:35 +0100561ZSTDLIB_API ZSTD_DDict* ZSTD_createDDict_advanced(const void* dict, size_t dictSize,
562 unsigned byReference, ZSTD_customMem customMem);
563
Yann Collete91c4b42016-09-14 16:55:44 +0200564/*! ZSTD_sizeof_DDict() :
565 * Gives the amount of memory used by a given ZSTD_DDict */
566ZSTDLIB_API size_t ZSTD_sizeof_DDict(const ZSTD_DDict* ddict);
567
Yann Collete7a41a52016-12-05 16:21:06 -0800568/*! ZSTD_getDictID_fromDict() :
569 * Provides the dictID stored within dictionary.
570 * if @return == 0, the dictionary is not conformant with Zstandard specification.
571 * It can still be loaded, but as a content-only dictionary. */
Nick Terrell8de46ab2016-12-16 13:27:30 -0800572ZSTDLIB_API unsigned ZSTD_getDictID_fromDict(const void* dict, size_t dictSize);
Yann Collete7a41a52016-12-05 16:21:06 -0800573
574/*! ZSTD_getDictID_fromDDict() :
575 * Provides the dictID of the dictionary loaded into `ddict`.
576 * If @return == 0, the dictionary is not conformant to Zstandard specification, or empty.
577 * Non-conformant dictionaries can still be loaded, but as content-only dictionaries. */
Nick Terrell8de46ab2016-12-16 13:27:30 -0800578ZSTDLIB_API unsigned ZSTD_getDictID_fromDDict(const ZSTD_DDict* ddict);
Yann Collete7a41a52016-12-05 16:21:06 -0800579
580/*! ZSTD_getDictID_fromFrame() :
581 * Provides the dictID required to decompressed the frame stored within `src`.
582 * If @return == 0, the dictID could not be decoded.
583 * This could for one of the following reasons :
584 * - The frame does not require a dictionary to be decoded (most common case).
585 * - The frame was built with dictID intentionally removed. Whatever dictionary is necessary is a hidden information.
586 * Note : this use case also happens when using a non-conformant dictionary.
587 * - `srcSize` is too small, and as a result, the frame header could not be decoded (only possible if `srcSize < ZSTD_FRAMEHEADERSIZE_MAX`).
588 * - This is not a Zstandard frame.
589 * When identifying the exact failure cause, it's possible to used ZSTD_getFrameParams(), which will provide a more precise error code. */
Nick Terrell8de46ab2016-12-16 13:27:30 -0800590ZSTDLIB_API unsigned ZSTD_getDictID_fromFrame(const void* src, size_t srcSize);
Yann Collete7a41a52016-12-05 16:21:06 -0800591
Yann Colletd3b7f8d2016-06-04 19:47:02 +0200592
inikep82057aa2016-10-06 13:23:52 +0200593/********************************************************************
Przemyslaw Skibinski984b66c2016-10-24 15:59:51 +0200594* Advanced streaming functions
Yann Collet5a0c8e22016-08-12 01:20:36 +0200595********************************************************************/
596
inikep82057aa2016-10-06 13:23:52 +0200597/*===== Advanced Streaming compression functions =====*/
Yann Collet70e3b312016-08-23 01:18:06 +0200598ZSTDLIB_API ZSTD_CStream* ZSTD_createCStream_advanced(ZSTD_customMem customMem);
Yann Collet36c2a032017-04-05 22:06:21 -0700599ZSTDLIB_API size_t ZSTD_sizeof_CStream(const ZSTD_CStream* zcs); /**< size of CStream is variable, depending primarily on compression level */
Sean Purcell2db72492017-02-09 10:50:43 -0800600ZSTDLIB_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 */
Sean Purcell0b5370a2017-01-18 13:44:43 -0800601ZSTDLIB_API size_t ZSTD_initCStream_usingDict(ZSTD_CStream* zcs, const void* dict, size_t dictSize, int compressionLevel); /**< note: a dict will not be used if dict == NULL or dictSize < 8 */
Yann Collet70e3b312016-08-23 01:18:06 +0200602ZSTDLIB_API size_t ZSTD_initCStream_advanced(ZSTD_CStream* zcs, const void* dict, size_t dictSize,
Sean Purcell0f5c95a2017-02-07 16:33:48 -0800603 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 -0700604ZSTDLIB_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 Collet77bf59e2017-04-27 11:43:04 -0700605ZSTDLIB_API size_t ZSTD_initCStream_usingCDict_advanced(ZSTD_CStream* zcs, const ZSTD_CDict* cdict, unsigned long long pledgedSrcSize, ZSTD_frameParameters fParams); /**< same as ZSTD_initCStream_usingCDict(), with control over frame parameters */
Yann Collet36c2a032017-04-05 22:06:21 -0700606
607/*! ZSTD_resetCStream() :
608 * start a new compression job, using same parameters from previous job.
609 * This is typically useful to skip dictionary loading stage, since it will re-use it in-place..
610 * Note that zcs must be init at least once before using ZSTD_resetCStream().
611 * pledgedSrcSize==0 means "srcSize unknown".
612 * If pledgedSrcSize > 0, its value must be correct, as it will be written in header, and controlled at the end.
613 * @return : 0, or an error code (which can be tested using ZSTD_isError()) */
614ZSTDLIB_API size_t ZSTD_resetCStream(ZSTD_CStream* zcs, unsigned long long pledgedSrcSize);
Yann Colletcb327632016-08-23 00:30:31 +0200615
Yann Collet5a0c8e22016-08-12 01:20:36 +0200616
inikep82057aa2016-10-06 13:23:52 +0200617/*===== Advanced Streaming decompression functions =====*/
Yann Colletbb002742017-01-25 16:25:38 -0800618typedef enum { DStream_p_maxWindowSize } ZSTD_DStreamParameter_e;
Yann Collet70e3b312016-08-23 01:18:06 +0200619ZSTDLIB_API ZSTD_DStream* ZSTD_createDStream_advanced(ZSTD_customMem customMem);
Sean Purcell0b5370a2017-01-18 13:44:43 -0800620ZSTDLIB_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 Collet17e482e2016-08-23 16:58:10 +0200621ZSTDLIB_API size_t ZSTD_setDStreamParameter(ZSTD_DStream* zds, ZSTD_DStreamParameter_e paramType, unsigned paramValue);
Yann Collet95162342016-10-25 16:19:52 -0700622ZSTDLIB_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 +0200623ZSTDLIB_API size_t ZSTD_resetDStream(ZSTD_DStream* zds); /**< re-use decompression parameters from previous init; saves dictionary loading */
Yann Collet70e3b312016-08-23 01:18:06 +0200624ZSTDLIB_API size_t ZSTD_sizeof_DStream(const ZSTD_DStream* zds);
Yann Collet5a0c8e22016-08-12 01:20:36 +0200625
626
inikep82057aa2016-10-06 13:23:52 +0200627/*********************************************************************
Yann Collet5a0c8e22016-08-12 01:20:36 +0200628* Buffer-less and synchronous inner streaming functions
inikep82057aa2016-10-06 13:23:52 +0200629*
630* 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 +0200631* But it's also a complex one, with many restrictions (documented below).
Yann Collet37d13002016-10-24 17:22:12 -0700632* Prefer using normal streaming API for an easier experience
inikep82057aa2016-10-06 13:23:52 +0200633********************************************************************* */
Yann Collet60ba31c2016-07-28 19:55:09 +0200634
inikep82057aa2016-10-06 13:23:52 +0200635/**
636 Buffer-less streaming compression (synchronous mode)
Yann Colletd3b7f8d2016-06-04 19:47:02 +0200637
Yann Colletd3b7f8d2016-06-04 19:47:02 +0200638 A ZSTD_CCtx object is required to track streaming operations.
Yann Collet45c03c52016-06-14 13:46:11 +0200639 Use ZSTD_createCCtx() / ZSTD_freeCCtx() to manage resource.
Yann Colletd3b7f8d2016-06-04 19:47:02 +0200640 ZSTD_CCtx object can be re-used multiple times within successive compression operations.
641
642 Start by initializing a context.
643 Use ZSTD_compressBegin(), or ZSTD_compressBegin_usingDict() for dictionary compression,
644 or ZSTD_compressBegin_advanced(), for finer parameter control.
645 It's also possible to duplicate a reference context which has already been initialized, using ZSTD_copyCCtx()
646
647 Then, consume your input using ZSTD_compressContinue().
Yann Colleta49e0662016-06-21 11:54:03 +0200648 There are some important considerations to keep in mind when using this advanced function :
649 - ZSTD_compressContinue() has no internal buffer. It uses externally provided buffer only.
Yann Collet62470b42016-07-28 15:29:08 +0200650 - Interface is synchronous : input is consumed entirely and produce 1+ (or more) compressed blocks.
Yann Colleta49e0662016-06-21 11:54:03 +0200651 - Caller must ensure there is enough space in `dst` to store compressed data under worst case scenario.
652 Worst case evaluation is provided by ZSTD_compressBound().
653 ZSTD_compressContinue() doesn't guarantee recover after a failed compression.
654 - ZSTD_compressContinue() presumes prior input ***is still accessible and unmodified*** (up to maximum distance size, see WindowLog).
655 It remembers all previous contiguous blocks, plus one separated memory segment (which can itself consists of multiple contiguous blocks)
656 - ZSTD_compressContinue() detects that prior input has been overwritten when `src` buffer overlaps.
657 In which case, it will "discard" the relevant memory section from its history.
658
Yann Collet62470b42016-07-28 15:29:08 +0200659 Finish a frame with ZSTD_compressEnd(), which will write the last block(s) and optional checksum.
Yann Collet5eb749e2017-01-11 18:21:25 +0100660 It's possible to use srcSize==0, in which case, it will write a final empty block to end the frame.
661 Without last block mark, frames will be considered unfinished (corrupted) by decoders.
Yann Colletd3b7f8d2016-06-04 19:47:02 +0200662
Yann Collet5eb749e2017-01-11 18:21:25 +0100663 `ZSTD_CCtx` object can be re-used (ZSTD_compressBegin()) to compress some new frame.
Yann Colletd3b7f8d2016-06-04 19:47:02 +0200664*/
665
inikep82057aa2016-10-06 13:23:52 +0200666/*===== Buffer-less streaming compression functions =====*/
667ZSTDLIB_API size_t ZSTD_compressBegin(ZSTD_CCtx* cctx, int compressionLevel);
668ZSTDLIB_API size_t ZSTD_compressBegin_usingDict(ZSTD_CCtx* cctx, const void* dict, size_t dictSize, int compressionLevel);
Sean Purcell0f5c95a2017-02-07 16:33:48 -0800669ZSTDLIB_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 -0700670ZSTDLIB_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 -0700671ZSTDLIB_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 */
672ZSTDLIB_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 */
673
inikep82057aa2016-10-06 13:23:52 +0200674ZSTDLIB_API size_t ZSTD_compressContinue(ZSTD_CCtx* cctx, void* dst, size_t dstCapacity, const void* src, size_t srcSize);
675ZSTDLIB_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 +0200676
Yann Colletd3b7f8d2016-06-04 19:47:02 +0200677
Yann Colletd3b7f8d2016-06-04 19:47:02 +0200678
inikepba1db372016-10-06 14:22:48 +0200679/*-
Yann Colletcf05b9d2016-07-18 16:52:10 +0200680 Buffer-less streaming decompression (synchronous mode)
Yann Colletd3b7f8d2016-06-04 19:47:02 +0200681
682 A ZSTD_DCtx object is required to track streaming operations.
683 Use ZSTD_createDCtx() / ZSTD_freeDCtx() to manage it.
684 A ZSTD_DCtx object can be re-used multiple times.
685
Yann Collet6b615d32016-07-29 19:40:37 +0200686 First typical operation is to retrieve frame parameters, using ZSTD_getFrameParams().
687 It fills a ZSTD_frameParams structure which provide important information to correctly decode the frame,
688 such as the minimum rolling buffer size to allocate to decompress data (`windowSize`),
689 and the dictionary ID used.
690 (Note : content size is optional, it may not be present. 0 means : content size unknown).
691 Note that these values could be wrong, either because of data malformation, or because an attacker is spoofing deliberate false information.
692 As a consequence, check that values remain within valid application range, especially `windowSize`, before allocation.
693 Each application can set its own limit, depending on local restrictions. For extended interoperability, it is recommended to support at least 8 MB.
694 Frame parameters are extracted from the beginning of the compressed frame.
695 Data fragment must be large enough to ensure successful decoding, typically `ZSTD_frameHeaderSize_max` bytes.
696 @result : 0 : successful decoding, the `ZSTD_frameParams` structure is correctly filled.
697 >0 : `srcSize` is too small, please provide at least @result bytes on next attempt.
Yann Colletd469a982016-07-28 03:47:45 +0200698 errorCode, which can be tested using ZSTD_isError().
Yann Colletd3b7f8d2016-06-04 19:47:02 +0200699
700 Start decompression, with ZSTD_decompressBegin() or ZSTD_decompressBegin_usingDict().
701 Alternatively, you can copy a prepared context, using ZSTD_copyDCtx().
702
703 Then use ZSTD_nextSrcSizeToDecompress() and ZSTD_decompressContinue() alternatively.
Yann Collet6b615d32016-07-29 19:40:37 +0200704 ZSTD_nextSrcSizeToDecompress() tells how many bytes to provide as 'srcSize' to ZSTD_decompressContinue().
705 ZSTD_decompressContinue() requires this _exact_ amount of bytes, or it will fail.
Yann Colletd3b7f8d2016-06-04 19:47:02 +0200706
Yann Collet49bb0042016-06-04 20:17:38 +0200707 @result of ZSTD_decompressContinue() is the number of bytes regenerated within 'dst' (necessarily <= dstCapacity).
Yann Collet6b615d32016-07-29 19:40:37 +0200708 It can be zero, which is not an error; it just means ZSTD_decompressContinue() has decoded some metadata item.
709 It can also be an error code, which can be tested with ZSTD_isError().
Yann Colletd3b7f8d2016-06-04 19:47:02 +0200710
Yann Collet3b6ae772016-07-08 23:42:22 +0200711 ZSTD_decompressContinue() needs previous data blocks during decompression, up to `windowSize`.
712 They should preferably be located contiguously, prior to current block.
713 Alternatively, a round buffer of sufficient size is also possible. Sufficient size is determined by frame parameters.
714 ZSTD_decompressContinue() is very sensitive to contiguity,
715 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 +0200716 or that previous contiguous segment is large enough to properly handle maximum back-reference.
Yann Collet3b6ae772016-07-08 23:42:22 +0200717
Yann Colletd3b7f8d2016-06-04 19:47:02 +0200718 A frame is fully decoded when ZSTD_nextSrcSizeToDecompress() returns zero.
719 Context can then be reset to start a new decompression.
720
Yann Collet4c5bbf62016-07-28 20:30:25 +0200721 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 +0200722 This information is not required to properly decode a frame.
Yann Collet3b6ae772016-07-08 23:42:22 +0200723
Yann Collete795c8a2016-12-13 16:39:36 +0100724 == Special case : skippable frames ==
Yann Collet3b6ae772016-07-08 23:42:22 +0200725
Yann Colletd469a982016-07-28 03:47:45 +0200726 Skippable frames allow integration of user-defined data into a flow of concatenated frames.
Yann Collet5b567392016-07-28 01:17:22 +0200727 Skippable frames will be ignored (skipped) by a decompressor. The format of skippable frames is as follows :
Yann Colletd3b7f8d2016-06-04 19:47:02 +0200728 a) Skippable frame ID - 4 Bytes, Little endian format, any value from 0x184D2A50 to 0x184D2A5F
729 b) Frame Size - 4 Bytes, Little endian format, unsigned 32-bits
730 c) Frame Content - any content (User Data) of length equal to Frame Size
731 For skippable frames ZSTD_decompressContinue() always returns 0.
732 For skippable frames ZSTD_getFrameParams() returns fparamsPtr->windowLog==0 what means that a frame is skippable.
Sean Purcell64417cd2017-02-22 13:29:01 -0800733 Note : If fparamsPtr->frameContentSize==0, it is ambiguous: the frame might actually be a Zstd encoded frame with no content.
734 For purposes of decompression, it is valid in both cases to skip the frame using
735 ZSTD_findFrameCompressedSize to find its size in bytes.
Yann Colletd3b7f8d2016-06-04 19:47:02 +0200736 It also returns Frame Size as fparamsPtr->frameContentSize.
737*/
738
inikep82057aa2016-10-06 13:23:52 +0200739typedef struct {
740 unsigned long long frameContentSize;
741 unsigned windowSize;
742 unsigned dictID;
743 unsigned checksumFlag;
744} ZSTD_frameParams;
Yann Colletd3b7f8d2016-06-04 19:47:02 +0200745
inikep82057aa2016-10-06 13:23:52 +0200746/*===== Buffer-less streaming decompression functions =====*/
747ZSTDLIB_API size_t ZSTD_getFrameParams(ZSTD_frameParams* fparamsPtr, const void* src, size_t srcSize); /**< doesn't consume input, see details below */
748ZSTDLIB_API size_t ZSTD_decompressBegin(ZSTD_DCtx* dctx);
749ZSTDLIB_API size_t ZSTD_decompressBegin_usingDict(ZSTD_DCtx* dctx, const void* dict, size_t dictSize);
750ZSTDLIB_API void ZSTD_copyDCtx(ZSTD_DCtx* dctx, const ZSTD_DCtx* preparedDCtx);
751ZSTDLIB_API size_t ZSTD_nextSrcSizeToDecompress(ZSTD_DCtx* dctx);
752ZSTDLIB_API size_t ZSTD_decompressContinue(ZSTD_DCtx* dctx, void* dst, size_t dstCapacity, const void* src, size_t srcSize);
753typedef enum { ZSTDnit_frameHeader, ZSTDnit_blockHeader, ZSTDnit_block, ZSTDnit_lastBlock, ZSTDnit_checksum, ZSTDnit_skippableFrame } ZSTD_nextInputType_e;
754ZSTDLIB_API ZSTD_nextInputType_e ZSTD_nextInputType(ZSTD_DCtx* dctx);
755
756/**
757 Block functions
758
759 Block functions produce and decode raw zstd blocks, without frame metadata.
Yann Colletcf05b9d2016-07-18 16:52:10 +0200760 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 +0200761 User will have to take in charge required information to regenerate data, such as compressed and content sizes.
762
763 A few rules to respect :
Yann Colletf246cf52016-07-06 20:30:52 +0200764 - Compressing and decompressing require a context structure
Yann Colletd3b7f8d2016-06-04 19:47:02 +0200765 + Use ZSTD_createCCtx() and ZSTD_createDCtx()
766 - It is necessary to init context before starting
Yann Colletaf4f45b2017-04-18 03:17:44 -0700767 + compression : any ZSTD_compressBegin*() variant, including with dictionary
768 + decompression : any ZSTD_decompressBegin*() variant, including with dictionary
769 + copyCCtx() and copyDCtx() can be used too
770 - Block size is limited, it must be <= ZSTD_getBlockSizeMax() <= ZSTD_BLOCKSIZE_ABSOLUTEMAX
771 + If input is larger than a block size, it's necessary to split input data into multiple blocks
772 + For inputs larger than a single block size, consider using the regular ZSTD_compress() instead.
773 Frame metadata is not that costly, and quickly becomes negligible as source size grows larger.
Yann Colletd3b7f8d2016-06-04 19:47:02 +0200774 - When a block is considered not compressible enough, ZSTD_compressBlock() result will be zero.
775 In which case, nothing is produced into `dst`.
776 + User must test for such outcome and deal directly with uncompressed data
Yann Colletf246cf52016-07-06 20:30:52 +0200777 + ZSTD_decompressBlock() doesn't accept uncompressed data as input !!!
Yann Colletaf4f45b2017-04-18 03:17:44 -0700778 + In case of multiple successive blocks, should some of them be uncompressed,
779 decoder must be informed of their existence in order to follow proper history.
780 Use ZSTD_insertBlock() for such a case.
Yann Colletd3b7f8d2016-06-04 19:47:02 +0200781*/
782
Yann Colletcf05b9d2016-07-18 16:52:10 +0200783#define ZSTD_BLOCKSIZE_ABSOLUTEMAX (128 * 1024) /* define, for static allocation */
inikep82057aa2016-10-06 13:23:52 +0200784/*===== Raw zstd block functions =====*/
Yann Colletcf05b9d2016-07-18 16:52:10 +0200785ZSTDLIB_API size_t ZSTD_getBlockSizeMax(ZSTD_CCtx* cctx);
Yann Colletd3b7f8d2016-06-04 19:47:02 +0200786ZSTDLIB_API size_t ZSTD_compressBlock (ZSTD_CCtx* cctx, void* dst, size_t dstCapacity, const void* src, size_t srcSize);
787ZSTDLIB_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 +0200788ZSTDLIB_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 +0200789
790
Nick Terrell05c00f22016-11-29 11:46:37 -0800791#endif /* ZSTD_H_ZSTD_STATIC_LINKING_ONLY */
Yann Colletd3b7f8d2016-06-04 19:47:02 +0200792
Yann Collet4856a002015-01-24 01:58:16 +0100793#if defined (__cplusplus)
794}
795#endif