blob: a4766e335197057846fdc01148a3840ab6555a09 [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)
23# define ZSTDLIB_API __attribute__ ((visibility ("default")))
24#elif defined(ZSTD_DLL_EXPORT) && (ZSTD_DLL_EXPORT==1)
Christophe Chevalier7b053242015-12-09 15:48:22 +010025# define ZSTDLIB_API __declspec(dllexport)
Przemyslaw Skibinski179555c2016-11-15 18:05:46 +010026#elif defined(ZSTD_DLL_IMPORT) && (ZSTD_DLL_IMPORT==1)
27# define ZSTDLIB_API __declspec(dllimport) /* It isn't required but allows to generate better code, saving a function pointer load from the IAT and an indirect jump.*/
Christophe Chevalierc6e84532015-12-07 17:44:09 +010028#else
29# define ZSTDLIB_API
30#endif
31
32
inikep2d261332016-10-06 16:28:21 +020033/*******************************************************************************************************
34 Introduction
35
Yann Collet37d13002016-10-24 17:22:12 -070036 zstd, short for Zstandard, is a fast lossless compression algorithm, targeting real-time compression scenarios
inikep2d261332016-10-06 16:28:21 +020037 at zlib-level and better compression ratios. The zstd compression library provides in-memory compression and
38 decompression functions. The library supports compression levels from 1 up to ZSTD_maxCLevel() which is 22.
Yann Collet37d13002016-10-24 17:22:12 -070039 Levels >= 20, labelled `--ultra`, should be used with caution, as they require more memory.
inikep2d261332016-10-06 16:28:21 +020040 Compression can be done in:
41 - a single step (described as Simple API)
42 - a single step, reusing a context (described as Explicit memory management)
Yann Collet37d13002016-10-24 17:22:12 -070043 - unbounded multiple steps (described as Streaming compression)
inikep2d261332016-10-06 16:28:21 +020044 The compression ratio achievable on small data can be highly improved using compression with a dictionary in:
45 - a single step (described as Simple dictionary API)
Przemyslaw Skibinski984b66c2016-10-24 15:59:51 +020046 - a single step, reusing a dictionary (described as Fast dictionary API)
inikep2d261332016-10-06 16:28:21 +020047
Yann Collet37d13002016-10-24 17:22:12 -070048 Advanced experimental functions can be accessed using #define ZSTD_STATIC_LINKING_ONLY before including zstd.h.
49 These APIs shall never be used with a dynamic library.
inikep2d261332016-10-06 16:28:21 +020050 They are not "stable", their definition may change in the future. Only static linking is allowed.
51*********************************************************************************************************/
52
53/*------ Version ------*/
Yann Collet901e85f2016-08-31 07:51:25 -070054#define ZSTD_VERSION_MAJOR 1
Yann Collet1eb2fdc2016-09-18 12:21:47 +020055#define ZSTD_VERSION_MINOR 1
Yann Colletfd3be6b2016-11-07 14:35:41 -080056#define ZSTD_VERSION_RELEASE 2
Yann Collete02808f2016-04-20 22:46:16 +020057
58#define ZSTD_LIB_VERSION ZSTD_VERSION_MAJOR.ZSTD_VERSION_MINOR.ZSTD_VERSION_RELEASE
59#define ZSTD_QUOTE(str) #str
60#define ZSTD_EXPAND_AND_QUOTE(str) ZSTD_QUOTE(str)
61#define ZSTD_VERSION_STRING ZSTD_EXPAND_AND_QUOTE(ZSTD_LIB_VERSION)
62
Yann Collet213089c2015-06-18 07:43:16 -080063#define ZSTD_VERSION_NUMBER (ZSTD_VERSION_MAJOR *100*100 + ZSTD_VERSION_MINOR *100 + ZSTD_VERSION_RELEASE)
Yann Collet379908b2016-12-06 10:36:15 -080064ZSTDLIB_API unsigned ZSTD_versionNumber(void); /**< library version number; to be used when checking dll version */
Yann Collet4856a002015-01-24 01:58:16 +010065
66
inikep82057aa2016-10-06 13:23:52 +020067/***************************************
Yann Colletcf05b9d2016-07-18 16:52:10 +020068* Simple API
Yann Collet7010c272015-10-21 09:07:25 +010069***************************************/
Yann Collet953ce722016-02-04 15:28:14 +010070/*! ZSTD_compress() :
Yann Colletac175d42016-09-13 00:51:47 +020071 Compresses `src` content as a single zstd compressed frame into already allocated `dst`.
Yann Colletcf05b9d2016-07-18 16:52:10 +020072 Hint : compression runs faster if `dstCapacity` >= `ZSTD_compressBound(srcSize)`.
Yann Collet64deef32016-09-14 00:16:07 +020073 @return : compressed size written into `dst` (<= `dstCapacity),
Nick Terrelld82efd82016-11-02 16:47:53 -070074 or an error code if it fails (which can be tested using ZSTD_isError()). */
Yann Colletcf05b9d2016-07-18 16:52:10 +020075ZSTDLIB_API size_t ZSTD_compress( void* dst, size_t dstCapacity,
76 const void* src, size_t srcSize,
77 int compressionLevel);
Yann Collet4856a002015-01-24 01:58:16 +010078
Yann Collet953ce722016-02-04 15:28:14 +010079/*! ZSTD_decompress() :
Yann Colletac175d42016-09-13 00:51:47 +020080 `compressedSize` : must be the _exact_ size of a single compressed frame.
81 `dstCapacity` is an upper bound of originalSize.
82 If user cannot imply a maximum upper bound, it's better to use streaming mode to decompress data.
Yann Collet953ce722016-02-04 15:28:14 +010083 @return : the number of bytes decompressed into `dst` (<= `dstCapacity`),
Nick Terrelld82efd82016-11-02 16:47:53 -070084 or an errorCode if it fails (which can be tested using ZSTD_isError()). */
Yann Collet7d968c72016-02-03 02:11:32 +010085ZSTDLIB_API size_t ZSTD_decompress( void* dst, size_t dstCapacity,
Christophe Chevalierc6e84532015-12-07 17:44:09 +010086 const void* src, size_t compressedSize);
Yann Collet4856a002015-01-24 01:58:16 +010087
Yann Colletac175d42016-09-13 00:51:47 +020088/*! ZSTD_getDecompressedSize() :
89* 'src' is the start of a zstd compressed frame.
90* @return : content size to be decompressed, as a 64-bits value _if known_, 0 otherwise.
91* note 1 : decompressed size is an optional field, that may not be present, especially in streaming mode.
92* When `return==0`, data to decompress could be any size.
93* In which case, it's necessary to use streaming mode to decompress data.
94* Optionally, application can still use ZSTD_decompress() while relying on implied limits.
95* (For example, data may be necessarily cut into blocks <= 16 KB).
96* note 2 : decompressed size is always present when compression is done with ZSTD_compress()
97* note 3 : decompressed size can be very large (64-bits value),
98* potentially larger than what local system can handle as a single memory segment.
99* In which case, it's necessary to use streaming mode to decompress data.
100* note 4 : If source is untrusted, decompressed size could be wrong or intentionally modified.
101* Always ensure result fits within application's authorized limits.
102* Each application can set its own limits.
103* note 5 : when `return==0`, if precise failure cause is needed, use ZSTD_getFrameParams() to know more. */
104ZSTDLIB_API unsigned long long ZSTD_getDecompressedSize(const void* src, size_t srcSize);
105
Yann Collet41105342016-07-27 15:09:11 +0200106
Yann Colletcf05b9d2016-07-18 16:52:10 +0200107/*====== Helper functions ======*/
Yann Collet41105342016-07-27 15:09:11 +0200108ZSTDLIB_API int ZSTD_maxCLevel(void); /*!< maximum compression level available */
109ZSTDLIB_API size_t ZSTD_compressBound(size_t srcSize); /*!< maximum compressed size in worst case scenario */
Yann Collet953ce722016-02-04 15:28:14 +0100110ZSTDLIB_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 +0200111ZSTDLIB_API const char* ZSTD_getErrorName(size_t code); /*!< provides readable string from an error code */
Yann Collet7010c272015-10-21 09:07:25 +0100112
113
inikep82057aa2016-10-06 13:23:52 +0200114/***************************************
Yann Collet7d968c72016-02-03 02:11:32 +0100115* Explicit memory management
Yann Collet7010c272015-10-21 09:07:25 +0100116***************************************/
Yann Collet37d13002016-10-24 17:22:12 -0700117/*= Compression context
Yann Collet379908b2016-12-06 10:36:15 -0800118* When compressing many times,
Yann Collet37d13002016-10-24 17:22:12 -0700119* it is recommended to allocate a context just once, and re-use it for each successive compression operation.
Yann Collet379908b2016-12-06 10:36:15 -0800120* This will make workload friendlier for system's memory.
Yann Collet37d13002016-10-24 17:22:12 -0700121* Use one context per thread for parallel execution in multi-threaded environments. */
Yann Collet87c18b22016-08-26 01:43:47 +0200122typedef struct ZSTD_CCtx_s ZSTD_CCtx;
Christophe Chevalierc6e84532015-12-07 17:44:09 +0100123ZSTDLIB_API ZSTD_CCtx* ZSTD_createCCtx(void);
Yann Colletd469a982016-07-28 03:47:45 +0200124ZSTDLIB_API size_t ZSTD_freeCCtx(ZSTD_CCtx* cctx);
Yann Collet7010c272015-10-21 09:07:25 +0100125
inikep82057aa2016-10-06 13:23:52 +0200126/*! ZSTD_compressCCtx() :
Nick Terrelld82efd82016-11-02 16:47:53 -0700127 Same as ZSTD_compress(), requires an allocated ZSTD_CCtx (see ZSTD_createCCtx()). */
Yann Collet7d968c72016-02-03 02:11:32 +0100128ZSTDLIB_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 +0100129
inikep82057aa2016-10-06 13:23:52 +0200130/*= Decompression context */
Yann Collet87c18b22016-08-26 01:43:47 +0200131typedef struct ZSTD_DCtx_s ZSTD_DCtx;
Yann Colletecd651b2016-01-07 15:35:18 +0100132ZSTDLIB_API ZSTD_DCtx* ZSTD_createDCtx(void);
Yann Colletd469a982016-07-28 03:47:45 +0200133ZSTDLIB_API size_t ZSTD_freeDCtx(ZSTD_DCtx* dctx);
Yann Colletecd651b2016-01-07 15:35:18 +0100134
inikep82057aa2016-10-06 13:23:52 +0200135/*! ZSTD_decompressDCtx() :
Nick Terrelld82efd82016-11-02 16:47:53 -0700136* Same as ZSTD_decompress(), requires an allocated ZSTD_DCtx (see ZSTD_createDCtx()). */
Yann Collet7d968c72016-02-03 02:11:32 +0100137ZSTDLIB_API size_t ZSTD_decompressDCtx(ZSTD_DCtx* ctx, void* dst, size_t dstCapacity, const void* src, size_t srcSize);
138
139
inikep82057aa2016-10-06 13:23:52 +0200140/**************************
Yann Collet302fb532016-06-07 12:16:49 +0200141* Simple dictionary API
142***************************/
Yann Collet953ce722016-02-04 15:28:14 +0100143/*! ZSTD_compress_usingDict() :
Yann Colletd469a982016-07-28 03:47:45 +0200144* Compression using a predefined Dictionary (see dictBuilder/zdict.h).
Nick Terrelld82efd82016-11-02 16:47:53 -0700145* Note : This function loads the dictionary, resulting in significant startup delay.
146* Note : When `dict == NULL || dictSize < 8` no dictionary is used. */
Yann Collet7d968c72016-02-03 02:11:32 +0100147ZSTDLIB_API size_t ZSTD_compress_usingDict(ZSTD_CCtx* ctx,
148 void* dst, size_t dstCapacity,
149 const void* src, size_t srcSize,
150 const void* dict,size_t dictSize,
151 int compressionLevel);
152
Yann Collet953ce722016-02-04 15:28:14 +0100153/*! ZSTD_decompress_usingDict() :
Yann Colletd469a982016-07-28 03:47:45 +0200154* Decompression using a predefined Dictionary (see dictBuilder/zdict.h).
Yann Collet81e13ef2016-06-07 00:51:51 +0200155* Dictionary must be identical to the one used during compression.
Nick Terrelld82efd82016-11-02 16:47:53 -0700156* Note : This function loads the dictionary, resulting in significant startup delay.
157* Note : When `dict == NULL || dictSize < 8` no dictionary is used. */
Yann Collet7d968c72016-02-03 02:11:32 +0100158ZSTDLIB_API size_t ZSTD_decompress_usingDict(ZSTD_DCtx* dctx,
159 void* dst, size_t dstCapacity,
160 const void* src, size_t srcSize,
161 const void* dict,size_t dictSize);
Yann Colletecd651b2016-01-07 15:35:18 +0100162
Yann Collet4856a002015-01-24 01:58:16 +0100163
inikep82057aa2016-10-06 13:23:52 +0200164/****************************
Przemyslaw Skibinski984b66c2016-10-24 15:59:51 +0200165* Fast dictionary API
Yann Collet302fb532016-06-07 12:16:49 +0200166****************************/
inikep2d261332016-10-06 16:28:21 +0200167typedef struct ZSTD_CDict_s ZSTD_CDict;
168
Yann Collet302fb532016-06-07 12:16:49 +0200169/*! ZSTD_createCDict() :
Yann Collet37d13002016-10-24 17:22:12 -0700170* When compressing multiple messages / blocks with the same dictionary, it's recommended to load it just once.
171* ZSTD_createCDict() will create a digested dictionary, ready to start future compression operations without startup delay.
172* ZSTD_CDict can be created once and used by multiple threads concurrently, as its usage is read-only.
Nick Terrelld82efd82016-11-02 16:47:53 -0700173* `dict` can be released after ZSTD_CDict creation. */
Yann Collet302fb532016-06-07 12:16:49 +0200174ZSTDLIB_API ZSTD_CDict* ZSTD_createCDict(const void* dict, size_t dictSize, int compressionLevel);
inikep2d261332016-10-06 16:28:21 +0200175
176/*! ZSTD_freeCDict() :
Nick Terrelld82efd82016-11-02 16:47:53 -0700177* Function frees memory allocated by ZSTD_createCDict(). */
Yann Collet302fb532016-06-07 12:16:49 +0200178ZSTDLIB_API size_t ZSTD_freeCDict(ZSTD_CDict* CDict);
179
180/*! ZSTD_compress_usingCDict() :
Yann Colletd469a982016-07-28 03:47:45 +0200181* Compression using a digested Dictionary.
Yann Colletcf05b9d2016-07-18 16:52:10 +0200182* Faster startup than ZSTD_compress_usingDict(), recommended when same dictionary is used multiple times.
Nick Terrelld82efd82016-11-02 16:47:53 -0700183* Note that compression level is decided during dictionary creation. */
Yann Collet302fb532016-06-07 12:16:49 +0200184ZSTDLIB_API size_t ZSTD_compress_usingCDict(ZSTD_CCtx* cctx,
185 void* dst, size_t dstCapacity,
186 const void* src, size_t srcSize,
187 const ZSTD_CDict* cdict);
188
inikep2d261332016-10-06 16:28:21 +0200189
190typedef struct ZSTD_DDict_s ZSTD_DDict;
191
Yann Collet302fb532016-06-07 12:16:49 +0200192/*! ZSTD_createDDict() :
193* Create a digested dictionary, ready to start decompression operation without startup delay.
Nick Terrelld82efd82016-11-02 16:47:53 -0700194* `dict` can be released after creation. */
Yann Collet302fb532016-06-07 12:16:49 +0200195ZSTDLIB_API ZSTD_DDict* ZSTD_createDDict(const void* dict, size_t dictSize);
inikep2d261332016-10-06 16:28:21 +0200196
197/*! ZSTD_freeDDict() :
198* Function frees memory allocated with ZSTD_createDDict() */
Yann Collet302fb532016-06-07 12:16:49 +0200199ZSTDLIB_API size_t ZSTD_freeDDict(ZSTD_DDict* ddict);
200
201/*! ZSTD_decompress_usingDDict() :
Nick Terrelld82efd82016-11-02 16:47:53 -0700202* Decompression using a digested Dictionary.
Yann Colletcf05b9d2016-07-18 16:52:10 +0200203* Faster startup than ZSTD_decompress_usingDict(), recommended when same dictionary is used multiple times. */
Yann Collet302fb532016-06-07 12:16:49 +0200204ZSTDLIB_API size_t ZSTD_decompress_usingDDict(ZSTD_DCtx* dctx,
205 void* dst, size_t dstCapacity,
206 const void* src, size_t srcSize,
207 const ZSTD_DDict* ddict);
208
209
inikep82057aa2016-10-06 13:23:52 +0200210/****************************
Yann Collet7be46bf2016-08-19 18:39:36 +0200211* Streaming
212****************************/
213
214typedef struct ZSTD_inBuffer_s {
215 const void* src; /**< start of input buffer */
216 size_t size; /**< size of input buffer */
217 size_t pos; /**< position where reading stopped. Will be updated. Necessarily 0 <= pos <= size */
218} ZSTD_inBuffer;
219
220typedef struct ZSTD_outBuffer_s {
221 void* dst; /**< start of output buffer */
222 size_t size; /**< size of output buffer */
223 size_t pos; /**< position where writing stopped. Will be updated. Necessarily 0 <= pos <= size */
224} ZSTD_outBuffer;
225
226
Yann Collet7be46bf2016-08-19 18:39:36 +0200227
inikepba1db372016-10-06 14:22:48 +0200228/*-***********************************************************************
Przemyslaw Skibinski984b66c2016-10-24 15:59:51 +0200229* Streaming compression - HowTo
Yann Collet7be46bf2016-08-19 18:39:36 +0200230*
231* A ZSTD_CStream object is required to track streaming operation.
232* Use ZSTD_createCStream() and ZSTD_freeCStream() to create/release resources.
233* ZSTD_CStream objects can be reused multiple times on consecutive compression operations.
Yann Collet37d13002016-10-24 17:22:12 -0700234* It is recommended to re-use ZSTD_CStream in situations where many streaming operations will be achieved consecutively,
235* since it will play nicer with system's memory, by re-using already allocated memory.
236* Use one separate ZSTD_CStream per thread for parallel execution.
Yann Collet7be46bf2016-08-19 18:39:36 +0200237*
Yann Collet37d13002016-10-24 17:22:12 -0700238* Start a new compression by initializing ZSTD_CStream.
Yann Collet7be46bf2016-08-19 18:39:36 +0200239* Use ZSTD_initCStream() to start a new compression operation.
Yann Collete795c8a2016-12-13 16:39:36 +0100240* Use ZSTD_initCStream_usingDict() or ZSTD_initCStream_usingCDict() for a compression which requires a dictionary (experimental section)
Yann Collet7be46bf2016-08-19 18:39:36 +0200241*
242* Use ZSTD_compressStream() repetitively to consume input stream.
Yann Colletfa72f6b2016-09-05 17:39:56 +0200243* The function will automatically update both `pos` fields.
Yann Collet7be46bf2016-08-19 18:39:36 +0200244* Note that it may not consume the entire input, in which case `pos < size`,
245* and it's up to the caller to present again remaining data.
Yann Collet4bf317d2016-08-28 07:43:34 -0700246* @return : a size hint, preferred nb of bytes to use as input for next function call
Yann Collet7be46bf2016-08-19 18:39:36 +0200247* or an error code, which can be tested using ZSTD_isError().
Yann Collete795c8a2016-12-13 16:39:36 +0100248* Note 1 : it's just a hint, to help latency a little, any other value will work fine.
249* Note 2 : size hint is guaranteed to be <= ZSTD_CStreamInSize()
Yann Collet7be46bf2016-08-19 18:39:36 +0200250*
Yann Collete795c8a2016-12-13 16:39:36 +0100251* At any moment, it's possible to flush whatever data remains within internal buffer, using ZSTD_flushStream().
Yann Collet7be46bf2016-08-19 18:39:36 +0200252* `output->pos` will be updated.
Yann Collete795c8a2016-12-13 16:39:36 +0100253* Note that some content might still be left within internal buffer if `output->size` is too small.
Yann Collet7be46bf2016-08-19 18:39:36 +0200254* @return : nb of bytes still present within internal buffer (0 if it's empty)
255* or an error code, which can be tested using ZSTD_isError().
256*
257* ZSTD_endStream() instructs to finish a frame.
258* It will perform a flush and write frame epilogue.
259* The epilogue is required for decoders to consider a frame completed.
260* Similar to ZSTD_flushStream(), it may not be able to flush the full content if `output->size` is too small.
261* In which case, call again ZSTD_endStream() to complete the flush.
Yann Collete795c8a2016-12-13 16:39:36 +0100262* @return : nb of bytes still present within internal buffer (0 if it's empty, hence compression completed)
Yann Collet7be46bf2016-08-19 18:39:36 +0200263* or an error code, which can be tested using ZSTD_isError().
264*
265* *******************************************************************/
266
267typedef struct ZSTD_CStream_s ZSTD_CStream;
Yann Collet70e3b312016-08-23 01:18:06 +0200268ZSTDLIB_API ZSTD_CStream* ZSTD_createCStream(void);
269ZSTDLIB_API size_t ZSTD_freeCStream(ZSTD_CStream* zcs);
Yann Collete795c8a2016-12-13 16:39:36 +0100270
Yann Collet70e3b312016-08-23 01:18:06 +0200271ZSTDLIB_API size_t ZSTD_initCStream(ZSTD_CStream* zcs, int compressionLevel);
272ZSTDLIB_API size_t ZSTD_compressStream(ZSTD_CStream* zcs, ZSTD_outBuffer* output, ZSTD_inBuffer* input);
273ZSTDLIB_API size_t ZSTD_flushStream(ZSTD_CStream* zcs, ZSTD_outBuffer* output);
274ZSTDLIB_API size_t ZSTD_endStream(ZSTD_CStream* zcs, ZSTD_outBuffer* output);
Yann Collet7be46bf2016-08-19 18:39:36 +0200275
inikep82057aa2016-10-06 13:23:52 +0200276ZSTDLIB_API size_t ZSTD_CStreamInSize(void); /**< recommended size for input buffer */
277ZSTDLIB_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 +0200278
Yann Collet7be46bf2016-08-19 18:39:36 +0200279
inikep82057aa2016-10-06 13:23:52 +0200280
inikepba1db372016-10-06 14:22:48 +0200281/*-***************************************************************************
Przemyslaw Skibinski984b66c2016-10-24 15:59:51 +0200282* Streaming decompression - HowTo
Yann Collet7be46bf2016-08-19 18:39:36 +0200283*
284* A ZSTD_DStream object is required to track streaming operations.
285* Use ZSTD_createDStream() and ZSTD_freeDStream() to create/release resources.
Yann Collet17e482e2016-08-23 16:58:10 +0200286* ZSTD_DStream objects can be re-used multiple times.
Yann Collet7be46bf2016-08-19 18:39:36 +0200287*
288* Use ZSTD_initDStream() to start a new decompression operation,
289* or ZSTD_initDStream_usingDict() if decompression requires a dictionary.
Yann Collet7c83dfd2016-09-05 19:47:43 +0200290* @return : recommended first input size
Yann Collet7be46bf2016-08-19 18:39:36 +0200291*
292* Use ZSTD_decompressStream() repetitively to consume your input.
Yann Colletfa72f6b2016-09-05 17:39:56 +0200293* The function will update both `pos` fields.
Yann Colletb3060f72016-09-09 16:44:16 +0200294* If `input.pos < input.size`, some input has not been consumed.
Yann Collet1d4208c2016-09-06 05:16:40 +0200295* It's up to the caller to present again remaining data.
Yann Colletb3060f72016-09-09 16:44:16 +0200296* If `output.pos < output.size`, decoder has flushed everything it could.
Yann Collet7be46bf2016-08-19 18:39:36 +0200297* @return : 0 when a frame is completely decoded and fully flushed,
Yann Colletfa72f6b2016-09-05 17:39:56 +0200298* an error code, which can be tested using ZSTD_isError(),
Yann Collet9ffbeea2016-12-02 18:37:38 -0800299* any other value > 0, which means there is still some decoding to do to complete current frame.
300* 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 +0200301* *******************************************************************************/
302
303typedef struct ZSTD_DStream_s ZSTD_DStream;
Yann Collet70e3b312016-08-23 01:18:06 +0200304ZSTDLIB_API ZSTD_DStream* ZSTD_createDStream(void);
305ZSTDLIB_API size_t ZSTD_freeDStream(ZSTD_DStream* zds);
Yann Collete795c8a2016-12-13 16:39:36 +0100306
inikep82057aa2016-10-06 13:23:52 +0200307ZSTDLIB_API size_t ZSTD_initDStream(ZSTD_DStream* zds);
308ZSTDLIB_API size_t ZSTD_decompressStream(ZSTD_DStream* zds, ZSTD_outBuffer* output, ZSTD_inBuffer* input);
Yann Collet7be46bf2016-08-19 18:39:36 +0200309
Yann Collet70e3b312016-08-23 01:18:06 +0200310ZSTDLIB_API size_t ZSTD_DStreamInSize(void); /*!< recommended size for input buffer */
Yann Collet01c19922016-09-08 19:29:04 +0200311ZSTDLIB_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 +0200312
Nick Terrell05c00f22016-11-29 11:46:37 -0800313#endif /* ZSTD_H_235446 */
Yann Collet7be46bf2016-08-19 18:39:36 +0200314
315
Nick Terrell05c00f22016-11-29 11:46:37 -0800316#if defined(ZSTD_STATIC_LINKING_ONLY) && !defined(ZSTD_H_ZSTD_STATIC_LINKING_ONLY)
317#define ZSTD_H_ZSTD_STATIC_LINKING_ONLY
Yann Colletd3b7f8d2016-06-04 19:47:02 +0200318
inikep82057aa2016-10-06 13:23:52 +0200319/****************************************************************************************
inikep2d261332016-10-06 16:28:21 +0200320 * START OF ADVANCED AND EXPERIMENTAL FUNCTIONS
Yann Colletd3b7f8d2016-06-04 19:47:02 +0200321 * The definitions in this section are considered experimental.
Yann Colleta49e0662016-06-21 11:54:03 +0200322 * They should never be used with a dynamic library, as they may change in the future.
Yann Colletd3b7f8d2016-06-04 19:47:02 +0200323 * They are provided for advanced usages.
324 * Use them only in association with static linking.
inikep82057aa2016-10-06 13:23:52 +0200325 * ***************************************************************************************/
Yann Colletd3b7f8d2016-06-04 19:47:02 +0200326
inikep82057aa2016-10-06 13:23:52 +0200327/* --- Constants ---*/
Yann Collet7bf72bb2016-07-20 13:36:43 +0200328#define ZSTD_MAGICNUMBER 0xFD2FB528 /* v0.8 */
Yann Colletd3b7f8d2016-06-04 19:47:02 +0200329#define ZSTD_MAGIC_SKIPPABLE_START 0x184D2A50U
330
Yann Colleted3845d2016-07-08 12:57:10 +0200331#define ZSTD_WINDOWLOG_MAX_32 25
332#define ZSTD_WINDOWLOG_MAX_64 27
333#define ZSTD_WINDOWLOG_MAX ((U32)(MEM_32bits() ? ZSTD_WINDOWLOG_MAX_32 : ZSTD_WINDOWLOG_MAX_64))
Yann Colletcf409a72016-09-26 16:41:05 +0200334#define ZSTD_WINDOWLOG_MIN 10
Yann Colletd3b7f8d2016-06-04 19:47:02 +0200335#define ZSTD_HASHLOG_MAX ZSTD_WINDOWLOG_MAX
Yann Colletcf409a72016-09-26 16:41:05 +0200336#define ZSTD_HASHLOG_MIN 6
337#define ZSTD_CHAINLOG_MAX (ZSTD_WINDOWLOG_MAX+1)
338#define ZSTD_CHAINLOG_MIN ZSTD_HASHLOG_MIN
Yann Colletd3b7f8d2016-06-04 19:47:02 +0200339#define ZSTD_HASHLOG3_MAX 17
Yann Colletd3b7f8d2016-06-04 19:47:02 +0200340#define ZSTD_SEARCHLOG_MAX (ZSTD_WINDOWLOG_MAX-1)
341#define ZSTD_SEARCHLOG_MIN 1
Yann Collet0e07bf32016-09-07 06:33:02 +0200342#define ZSTD_SEARCHLENGTH_MAX 7 /* only for ZSTD_fast, other strategies are limited to 6 */
343#define ZSTD_SEARCHLENGTH_MIN 3 /* only for ZSTD_btopt, other strategies are limited to 4 */
Yann Colletd3b7f8d2016-06-04 19:47:02 +0200344#define ZSTD_TARGETLENGTH_MIN 4
345#define ZSTD_TARGETLENGTH_MAX 999
346
Yann Collet673f0d72016-06-06 00:26:38 +0200347#define ZSTD_FRAMEHEADERSIZE_MAX 18 /* for static allocation */
Yann Collet7c83dfd2016-09-05 19:47:43 +0200348static const size_t ZSTD_frameHeaderSize_prefix = 5;
349static const size_t ZSTD_frameHeaderSize_min = 6;
Yann Collet673f0d72016-06-06 00:26:38 +0200350static const size_t ZSTD_frameHeaderSize_max = ZSTD_FRAMEHEADERSIZE_MAX;
351static const size_t ZSTD_skippableHeaderSize = 8; /* magic number + skippable frame length */
352
Yann Colletd3b7f8d2016-06-04 19:47:02 +0200353
Przemyslaw Skibinski984b66c2016-10-24 15:59:51 +0200354/*--- Advanced types ---*/
Przemyslaw Skibinski5c5f01f2016-10-25 12:25:07 +0200355typedef 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 +0200356
357typedef struct {
Yann Collet655393c2016-08-14 00:16:20 +0200358 unsigned windowLog; /**< largest match distance : larger == more compression, more memory needed during decompression */
359 unsigned chainLog; /**< fully searched segment : larger == more compression, slower, more memory (useless for fast) */
360 unsigned hashLog; /**< dispatch table : larger == faster, more memory */
361 unsigned searchLog; /**< nb of searches : larger == more compression, slower */
362 unsigned searchLength; /**< match length searched : larger == faster decompression, sometimes less compression */
363 unsigned targetLength; /**< acceptable match size for optimal parser (only) : larger == more compression, slower */
Yann Colletd3b7f8d2016-06-04 19:47:02 +0200364 ZSTD_strategy strategy;
365} ZSTD_compressionParameters;
366
367typedef struct {
Yann Collet655393c2016-08-14 00:16:20 +0200368 unsigned contentSizeFlag; /**< 1: content size will be in frame header (if known). */
369 unsigned checksumFlag; /**< 1: will generate a 22-bits checksum at end of frame, to be used for error detection by decompressor */
370 unsigned noDictIDFlag; /**< 1: no dict ID will be saved into frame header (if dictionary compression) */
Yann Colletd3b7f8d2016-06-04 19:47:02 +0200371} ZSTD_frameParameters;
372
373typedef struct {
374 ZSTD_compressionParameters cParams;
375 ZSTD_frameParameters fParams;
376} ZSTD_parameters;
377
inikep82057aa2016-10-06 13:23:52 +0200378/*= Custom memory allocation functions */
Yann Colletd3b7f8d2016-06-04 19:47:02 +0200379typedef void* (*ZSTD_allocFunction) (void* opaque, size_t size);
380typedef void (*ZSTD_freeFunction) (void* opaque, void* address);
381typedef struct { ZSTD_allocFunction customAlloc; ZSTD_freeFunction customFree; void* opaque; } ZSTD_customMem;
382
383
inikep82057aa2016-10-06 13:23:52 +0200384/***************************************
Yann Collet81e13ef2016-06-07 00:51:51 +0200385* Advanced compression functions
Yann Colletd3b7f8d2016-06-04 19:47:02 +0200386***************************************/
Yann Collet3ae543c2016-07-11 03:12:17 +0200387/*! ZSTD_estimateCCtxSize() :
388 * Gives the amount of memory allocated for a ZSTD_CCtx given a set of compression parameters.
389 * `frameContentSize` is an optional parameter, provide `0` if unknown */
Yann Collet88472382016-07-14 17:05:38 +0200390ZSTDLIB_API size_t ZSTD_estimateCCtxSize(ZSTD_compressionParameters cParams);
Yann Collet3ae543c2016-07-11 03:12:17 +0200391
Yann Colletd3b7f8d2016-06-04 19:47:02 +0200392/*! ZSTD_createCCtx_advanced() :
393 * Create a ZSTD compression context using external alloc and free functions */
394ZSTDLIB_API ZSTD_CCtx* ZSTD_createCCtx_advanced(ZSTD_customMem customMem);
395
Yann Colletd7c65892016-09-15 02:50:27 +0200396/*! ZSTD_sizeofCCtx() :
397 * Gives the amount of memory used by a given ZSTD_CCtx */
398ZSTDLIB_API size_t ZSTD_sizeof_CCtx(const ZSTD_CCtx* cctx);
399
Yann Collet81e13ef2016-06-07 00:51:51 +0200400/*! ZSTD_createCDict_advanced() :
401 * Create a ZSTD_CDict using external alloc and free, and customized compression parameters */
402ZSTDLIB_API ZSTD_CDict* ZSTD_createCDict_advanced(const void* dict, size_t dictSize,
403 ZSTD_parameters params, ZSTD_customMem customMem);
Yann Colletd3b7f8d2016-06-04 19:47:02 +0200404
Yann Colletd7c65892016-09-15 02:50:27 +0200405/*! ZSTD_sizeof_CDict() :
406 * Gives the amount of memory used by a given ZSTD_sizeof_CDict */
407ZSTDLIB_API size_t ZSTD_sizeof_CDict(const ZSTD_CDict* cdict);
Yann Collet8e0ee682016-07-11 13:09:52 +0200408
Yann Collet6c6e1752016-06-27 15:28:45 +0200409/*! ZSTD_getParams() :
410* same as ZSTD_getCParams(), but @return a full `ZSTD_parameters` object instead of a `ZSTD_compressionParameters`.
411* All fields of `ZSTD_frameParameters` are set to default (0) */
Yann Collet70e3b312016-08-23 01:18:06 +0200412ZSTDLIB_API ZSTD_parameters ZSTD_getParams(int compressionLevel, unsigned long long srcSize, size_t dictSize);
Yann Collet6c6e1752016-06-27 15:28:45 +0200413
Yann Colletd3b7f8d2016-06-04 19:47:02 +0200414/*! ZSTD_getCParams() :
415* @return ZSTD_compressionParameters structure for a selected compression level and srcSize.
416* `srcSize` value is optional, select 0 if not known */
Yann Collet52c04fe2016-07-07 11:53:18 +0200417ZSTDLIB_API ZSTD_compressionParameters ZSTD_getCParams(int compressionLevel, unsigned long long srcSize, size_t dictSize);
Yann Colletd3b7f8d2016-06-04 19:47:02 +0200418
Yann Collet3d2cd7f2016-06-27 15:12:26 +0200419/*! ZSTD_checkCParams() :
Yann Colletd3b7f8d2016-06-04 19:47:02 +0200420* Ensure param values remain within authorized range */
421ZSTDLIB_API size_t ZSTD_checkCParams(ZSTD_compressionParameters params);
422
Yann Collet3d2cd7f2016-06-27 15:12:26 +0200423/*! ZSTD_adjustCParams() :
Yann Colletd3b7f8d2016-06-04 19:47:02 +0200424* optimize params for a given `srcSize` and `dictSize`.
425* both values are optional, select `0` if unknown. */
Yann Collet52c04fe2016-07-07 11:53:18 +0200426ZSTDLIB_API ZSTD_compressionParameters ZSTD_adjustCParams(ZSTD_compressionParameters cPar, unsigned long long srcSize, size_t dictSize);
Yann Colletd3b7f8d2016-06-04 19:47:02 +0200427
428/*! ZSTD_compress_advanced() :
429* Same as ZSTD_compress_usingDict(), with fine-tune control of each compression parameter */
430ZSTDLIB_API size_t ZSTD_compress_advanced (ZSTD_CCtx* ctx,
431 void* dst, size_t dstCapacity,
432 const void* src, size_t srcSize,
433 const void* dict,size_t dictSize,
434 ZSTD_parameters params);
435
Yann Collet45c03c52016-06-14 13:46:11 +0200436
Przemyslaw Skibinski984b66c2016-10-24 15:59:51 +0200437/*--- Advanced decompression functions ---*/
Yann Colletd3b7f8d2016-06-04 19:47:02 +0200438
Yann Collet179b1972016-11-02 17:30:49 -0700439/*! ZSTD_isFrame() :
440 * Tells if the content of `buffer` starts with a valid Frame Identifier.
441 * Note : Frame Identifier is 4 bytes. If `size < 4`, @return will always be 0.
442 * Note 2 : Legacy Frame Identifiers are considered valid only if Legacy Support is enabled.
443 * Note 3 : Skippable Frame Identifiers are considered valid. */
444ZSTDLIB_API unsigned ZSTD_isFrame(const void* buffer, size_t size);
445
Yann Colletd158c352016-07-11 13:46:25 +0200446/*! ZSTD_estimateDCtxSize() :
447 * Gives the potential amount of memory allocated to create a ZSTD_DCtx */
448ZSTDLIB_API size_t ZSTD_estimateDCtxSize(void);
449
Yann Collet81e13ef2016-06-07 00:51:51 +0200450/*! ZSTD_createDCtx_advanced() :
451 * Create a ZSTD decompression context using external alloc and free functions */
452ZSTDLIB_API ZSTD_DCtx* ZSTD_createDCtx_advanced(ZSTD_customMem customMem);
Yann Colletd3b7f8d2016-06-04 19:47:02 +0200453
Yann Colletd7c65892016-09-15 02:50:27 +0200454/*! ZSTD_sizeof_DCtx() :
Yann Colletd158c352016-07-11 13:46:25 +0200455 * Gives the amount of memory used by a given ZSTD_DCtx */
Yann Collet70e3b312016-08-23 01:18:06 +0200456ZSTDLIB_API size_t ZSTD_sizeof_DCtx(const ZSTD_DCtx* dctx);
Yann Collet8e0ee682016-07-11 13:09:52 +0200457
Yann Collete91c4b42016-09-14 16:55:44 +0200458/*! ZSTD_sizeof_DDict() :
459 * Gives the amount of memory used by a given ZSTD_DDict */
460ZSTDLIB_API size_t ZSTD_sizeof_DDict(const ZSTD_DDict* ddict);
461
Yann Collete7a41a52016-12-05 16:21:06 -0800462/*! ZSTD_getDictID_fromDict() :
463 * Provides the dictID stored within dictionary.
464 * if @return == 0, the dictionary is not conformant with Zstandard specification.
465 * It can still be loaded, but as a content-only dictionary. */
466unsigned ZSTD_getDictID_fromDict(const void* dict, size_t dictSize);
467
468/*! ZSTD_getDictID_fromDDict() :
469 * Provides the dictID of the dictionary loaded into `ddict`.
470 * If @return == 0, the dictionary is not conformant to Zstandard specification, or empty.
471 * Non-conformant dictionaries can still be loaded, but as content-only dictionaries. */
472unsigned ZSTD_getDictID_fromDDict(const ZSTD_DDict* ddict);
473
474/*! ZSTD_getDictID_fromFrame() :
475 * Provides the dictID required to decompressed the frame stored within `src`.
476 * If @return == 0, the dictID could not be decoded.
477 * This could for one of the following reasons :
478 * - The frame does not require a dictionary to be decoded (most common case).
479 * - The frame was built with dictID intentionally removed. Whatever dictionary is necessary is a hidden information.
480 * Note : this use case also happens when using a non-conformant dictionary.
481 * - `srcSize` is too small, and as a result, the frame header could not be decoded (only possible if `srcSize < ZSTD_FRAMEHEADERSIZE_MAX`).
482 * - This is not a Zstandard frame.
483 * When identifying the exact failure cause, it's possible to used ZSTD_getFrameParams(), which will provide a more precise error code. */
484unsigned ZSTD_getDictID_fromFrame(const void* src, size_t srcSize);
485
Yann Colletd3b7f8d2016-06-04 19:47:02 +0200486
inikep82057aa2016-10-06 13:23:52 +0200487/********************************************************************
Przemyslaw Skibinski984b66c2016-10-24 15:59:51 +0200488* Advanced streaming functions
Yann Collet5a0c8e22016-08-12 01:20:36 +0200489********************************************************************/
490
inikep82057aa2016-10-06 13:23:52 +0200491/*===== Advanced Streaming compression functions =====*/
Yann Collet70e3b312016-08-23 01:18:06 +0200492ZSTDLIB_API ZSTD_CStream* ZSTD_createCStream_advanced(ZSTD_customMem customMem);
Yann Collete795c8a2016-12-13 16:39:36 +0100493ZSTDLIB_API size_t ZSTD_initCStream_srcSize(ZSTD_CStream* zcs, int compressionLevel, unsigned long long pledgedSrcSize); /**< pledgedSrcSize must be correct */
Yann Collet70e3b312016-08-23 01:18:06 +0200494ZSTDLIB_API size_t ZSTD_initCStream_usingDict(ZSTD_CStream* zcs, const void* dict, size_t dictSize, int compressionLevel);
495ZSTDLIB_API size_t ZSTD_initCStream_advanced(ZSTD_CStream* zcs, const void* dict, size_t dictSize,
Yann Collet4cb21292016-09-15 14:54:07 +0200496 ZSTD_parameters params, unsigned long long pledgedSrcSize); /**< pledgedSrcSize is optional and can be zero == unknown */
Yann Collet95162342016-10-25 16:19:52 -0700497ZSTDLIB_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 Colletee5b7252016-10-27 14:20:55 -0700498ZSTDLIB_API size_t ZSTD_resetCStream(ZSTD_CStream* zcs, unsigned long long pledgedSrcSize); /**< re-use compression parameters from previous init; skip dictionary loading stage; zcs must be init at least once before */
Yann Collet70e3b312016-08-23 01:18:06 +0200499ZSTDLIB_API size_t ZSTD_sizeof_CStream(const ZSTD_CStream* zcs);
Yann Colletcb327632016-08-23 00:30:31 +0200500
Yann Collet5a0c8e22016-08-12 01:20:36 +0200501
inikep82057aa2016-10-06 13:23:52 +0200502/*===== Advanced Streaming decompression functions =====*/
Yann Collet17e482e2016-08-23 16:58:10 +0200503typedef enum { ZSTDdsp_maxWindowSize } ZSTD_DStreamParameter_e;
Yann Collet70e3b312016-08-23 01:18:06 +0200504ZSTDLIB_API ZSTD_DStream* ZSTD_createDStream_advanced(ZSTD_customMem customMem);
505ZSTDLIB_API size_t ZSTD_initDStream_usingDict(ZSTD_DStream* zds, const void* dict, size_t dictSize);
Yann Collet17e482e2016-08-23 16:58:10 +0200506ZSTDLIB_API size_t ZSTD_setDStreamParameter(ZSTD_DStream* zds, ZSTD_DStreamParameter_e paramType, unsigned paramValue);
Yann Collet95162342016-10-25 16:19:52 -0700507ZSTDLIB_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 +0200508ZSTDLIB_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 +0200509ZSTDLIB_API size_t ZSTD_sizeof_DStream(const ZSTD_DStream* zds);
Yann Collet5a0c8e22016-08-12 01:20:36 +0200510
511
inikep82057aa2016-10-06 13:23:52 +0200512/*********************************************************************
Yann Collet5a0c8e22016-08-12 01:20:36 +0200513* Buffer-less and synchronous inner streaming functions
inikep82057aa2016-10-06 13:23:52 +0200514*
515* 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 +0200516* But it's also a complex one, with many restrictions (documented below).
Yann Collet37d13002016-10-24 17:22:12 -0700517* Prefer using normal streaming API for an easier experience
inikep82057aa2016-10-06 13:23:52 +0200518********************************************************************* */
Yann Collet60ba31c2016-07-28 19:55:09 +0200519
inikep82057aa2016-10-06 13:23:52 +0200520/**
521 Buffer-less streaming compression (synchronous mode)
Yann Colletd3b7f8d2016-06-04 19:47:02 +0200522
Yann Colletd3b7f8d2016-06-04 19:47:02 +0200523 A ZSTD_CCtx object is required to track streaming operations.
Yann Collet45c03c52016-06-14 13:46:11 +0200524 Use ZSTD_createCCtx() / ZSTD_freeCCtx() to manage resource.
Yann Colletd3b7f8d2016-06-04 19:47:02 +0200525 ZSTD_CCtx object can be re-used multiple times within successive compression operations.
526
527 Start by initializing a context.
528 Use ZSTD_compressBegin(), or ZSTD_compressBegin_usingDict() for dictionary compression,
529 or ZSTD_compressBegin_advanced(), for finer parameter control.
530 It's also possible to duplicate a reference context which has already been initialized, using ZSTD_copyCCtx()
531
532 Then, consume your input using ZSTD_compressContinue().
Yann Colleta49e0662016-06-21 11:54:03 +0200533 There are some important considerations to keep in mind when using this advanced function :
534 - ZSTD_compressContinue() has no internal buffer. It uses externally provided buffer only.
Yann Collet62470b42016-07-28 15:29:08 +0200535 - Interface is synchronous : input is consumed entirely and produce 1+ (or more) compressed blocks.
Yann Colleta49e0662016-06-21 11:54:03 +0200536 - Caller must ensure there is enough space in `dst` to store compressed data under worst case scenario.
537 Worst case evaluation is provided by ZSTD_compressBound().
538 ZSTD_compressContinue() doesn't guarantee recover after a failed compression.
539 - ZSTD_compressContinue() presumes prior input ***is still accessible and unmodified*** (up to maximum distance size, see WindowLog).
540 It remembers all previous contiguous blocks, plus one separated memory segment (which can itself consists of multiple contiguous blocks)
541 - ZSTD_compressContinue() detects that prior input has been overwritten when `src` buffer overlaps.
542 In which case, it will "discard" the relevant memory section from its history.
543
Yann Collet62470b42016-07-28 15:29:08 +0200544 Finish a frame with ZSTD_compressEnd(), which will write the last block(s) and optional checksum.
545 It's possible to use a NULL,0 src content, in which case, it will write a final empty block to end the frame,
546 Without last block mark, frames will be considered unfinished (broken) by decoders.
Yann Colletd3b7f8d2016-06-04 19:47:02 +0200547
Yann Colleta49e0662016-06-21 11:54:03 +0200548 You can then reuse `ZSTD_CCtx` (ZSTD_compressBegin()) to compress some new frame.
Yann Colletd3b7f8d2016-06-04 19:47:02 +0200549*/
550
inikep82057aa2016-10-06 13:23:52 +0200551/*===== Buffer-less streaming compression functions =====*/
552ZSTDLIB_API size_t ZSTD_compressBegin(ZSTD_CCtx* cctx, int compressionLevel);
553ZSTDLIB_API size_t ZSTD_compressBegin_usingDict(ZSTD_CCtx* cctx, const void* dict, size_t dictSize, int compressionLevel);
554ZSTDLIB_API size_t ZSTD_compressBegin_advanced(ZSTD_CCtx* cctx, const void* dict, size_t dictSize, ZSTD_parameters params, unsigned long long pledgedSrcSize);
555ZSTDLIB_API size_t ZSTD_copyCCtx(ZSTD_CCtx* cctx, const ZSTD_CCtx* preparedCCtx, unsigned long long pledgedSrcSize);
556ZSTDLIB_API size_t ZSTD_compressContinue(ZSTD_CCtx* cctx, void* dst, size_t dstCapacity, const void* src, size_t srcSize);
557ZSTDLIB_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 +0200558
Yann Colletd3b7f8d2016-06-04 19:47:02 +0200559
Yann Colletd3b7f8d2016-06-04 19:47:02 +0200560
inikepba1db372016-10-06 14:22:48 +0200561/*-
Yann Colletcf05b9d2016-07-18 16:52:10 +0200562 Buffer-less streaming decompression (synchronous mode)
Yann Colletd3b7f8d2016-06-04 19:47:02 +0200563
564 A ZSTD_DCtx object is required to track streaming operations.
565 Use ZSTD_createDCtx() / ZSTD_freeDCtx() to manage it.
566 A ZSTD_DCtx object can be re-used multiple times.
567
Yann Collet6b615d32016-07-29 19:40:37 +0200568 First typical operation is to retrieve frame parameters, using ZSTD_getFrameParams().
569 It fills a ZSTD_frameParams structure which provide important information to correctly decode the frame,
570 such as the minimum rolling buffer size to allocate to decompress data (`windowSize`),
571 and the dictionary ID used.
572 (Note : content size is optional, it may not be present. 0 means : content size unknown).
573 Note that these values could be wrong, either because of data malformation, or because an attacker is spoofing deliberate false information.
574 As a consequence, check that values remain within valid application range, especially `windowSize`, before allocation.
575 Each application can set its own limit, depending on local restrictions. For extended interoperability, it is recommended to support at least 8 MB.
576 Frame parameters are extracted from the beginning of the compressed frame.
577 Data fragment must be large enough to ensure successful decoding, typically `ZSTD_frameHeaderSize_max` bytes.
578 @result : 0 : successful decoding, the `ZSTD_frameParams` structure is correctly filled.
579 >0 : `srcSize` is too small, please provide at least @result bytes on next attempt.
Yann Colletd469a982016-07-28 03:47:45 +0200580 errorCode, which can be tested using ZSTD_isError().
Yann Colletd3b7f8d2016-06-04 19:47:02 +0200581
582 Start decompression, with ZSTD_decompressBegin() or ZSTD_decompressBegin_usingDict().
583 Alternatively, you can copy a prepared context, using ZSTD_copyDCtx().
584
585 Then use ZSTD_nextSrcSizeToDecompress() and ZSTD_decompressContinue() alternatively.
Yann Collet6b615d32016-07-29 19:40:37 +0200586 ZSTD_nextSrcSizeToDecompress() tells how many bytes to provide as 'srcSize' to ZSTD_decompressContinue().
587 ZSTD_decompressContinue() requires this _exact_ amount of bytes, or it will fail.
Yann Colletd3b7f8d2016-06-04 19:47:02 +0200588
Yann Collet49bb0042016-06-04 20:17:38 +0200589 @result of ZSTD_decompressContinue() is the number of bytes regenerated within 'dst' (necessarily <= dstCapacity).
Yann Collet6b615d32016-07-29 19:40:37 +0200590 It can be zero, which is not an error; it just means ZSTD_decompressContinue() has decoded some metadata item.
591 It can also be an error code, which can be tested with ZSTD_isError().
Yann Colletd3b7f8d2016-06-04 19:47:02 +0200592
Yann Collet3b6ae772016-07-08 23:42:22 +0200593 ZSTD_decompressContinue() needs previous data blocks during decompression, up to `windowSize`.
594 They should preferably be located contiguously, prior to current block.
595 Alternatively, a round buffer of sufficient size is also possible. Sufficient size is determined by frame parameters.
596 ZSTD_decompressContinue() is very sensitive to contiguity,
597 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 +0200598 or that previous contiguous segment is large enough to properly handle maximum back-reference.
Yann Collet3b6ae772016-07-08 23:42:22 +0200599
Yann Colletd3b7f8d2016-06-04 19:47:02 +0200600 A frame is fully decoded when ZSTD_nextSrcSizeToDecompress() returns zero.
601 Context can then be reset to start a new decompression.
602
Yann Collet4c5bbf62016-07-28 20:30:25 +0200603 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 +0200604 This information is not required to properly decode a frame.
Yann Collet3b6ae772016-07-08 23:42:22 +0200605
Yann Collete795c8a2016-12-13 16:39:36 +0100606 == Special case : skippable frames ==
Yann Collet3b6ae772016-07-08 23:42:22 +0200607
Yann Colletd469a982016-07-28 03:47:45 +0200608 Skippable frames allow integration of user-defined data into a flow of concatenated frames.
Yann Collet5b567392016-07-28 01:17:22 +0200609 Skippable frames will be ignored (skipped) by a decompressor. The format of skippable frames is as follows :
Yann Colletd3b7f8d2016-06-04 19:47:02 +0200610 a) Skippable frame ID - 4 Bytes, Little endian format, any value from 0x184D2A50 to 0x184D2A5F
611 b) Frame Size - 4 Bytes, Little endian format, unsigned 32-bits
612 c) Frame Content - any content (User Data) of length equal to Frame Size
613 For skippable frames ZSTD_decompressContinue() always returns 0.
614 For skippable frames ZSTD_getFrameParams() returns fparamsPtr->windowLog==0 what means that a frame is skippable.
615 It also returns Frame Size as fparamsPtr->frameContentSize.
616*/
617
inikep82057aa2016-10-06 13:23:52 +0200618typedef struct {
619 unsigned long long frameContentSize;
620 unsigned windowSize;
621 unsigned dictID;
622 unsigned checksumFlag;
623} ZSTD_frameParams;
Yann Colletd3b7f8d2016-06-04 19:47:02 +0200624
inikep82057aa2016-10-06 13:23:52 +0200625/*===== Buffer-less streaming decompression functions =====*/
626ZSTDLIB_API size_t ZSTD_getFrameParams(ZSTD_frameParams* fparamsPtr, const void* src, size_t srcSize); /**< doesn't consume input, see details below */
627ZSTDLIB_API size_t ZSTD_decompressBegin(ZSTD_DCtx* dctx);
628ZSTDLIB_API size_t ZSTD_decompressBegin_usingDict(ZSTD_DCtx* dctx, const void* dict, size_t dictSize);
629ZSTDLIB_API void ZSTD_copyDCtx(ZSTD_DCtx* dctx, const ZSTD_DCtx* preparedDCtx);
630ZSTDLIB_API size_t ZSTD_nextSrcSizeToDecompress(ZSTD_DCtx* dctx);
631ZSTDLIB_API size_t ZSTD_decompressContinue(ZSTD_DCtx* dctx, void* dst, size_t dstCapacity, const void* src, size_t srcSize);
632typedef enum { ZSTDnit_frameHeader, ZSTDnit_blockHeader, ZSTDnit_block, ZSTDnit_lastBlock, ZSTDnit_checksum, ZSTDnit_skippableFrame } ZSTD_nextInputType_e;
633ZSTDLIB_API ZSTD_nextInputType_e ZSTD_nextInputType(ZSTD_DCtx* dctx);
634
635/**
636 Block functions
637
638 Block functions produce and decode raw zstd blocks, without frame metadata.
Yann Colletcf05b9d2016-07-18 16:52:10 +0200639 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 +0200640 User will have to take in charge required information to regenerate data, such as compressed and content sizes.
641
642 A few rules to respect :
Yann Colletf246cf52016-07-06 20:30:52 +0200643 - Compressing and decompressing require a context structure
Yann Colletd3b7f8d2016-06-04 19:47:02 +0200644 + Use ZSTD_createCCtx() and ZSTD_createDCtx()
645 - It is necessary to init context before starting
646 + compression : ZSTD_compressBegin()
647 + decompression : ZSTD_decompressBegin()
648 + variants _usingDict() are also allowed
649 + copyCCtx() and copyDCtx() work too
Yann Colletcf05b9d2016-07-18 16:52:10 +0200650 - Block size is limited, it must be <= ZSTD_getBlockSizeMax()
651 + If you need to compress more, cut data into multiple blocks
652 + Consider using the regular ZSTD_compress() instead, as frame metadata costs become negligible when source size is large.
Yann Colletd3b7f8d2016-06-04 19:47:02 +0200653 - When a block is considered not compressible enough, ZSTD_compressBlock() result will be zero.
654 In which case, nothing is produced into `dst`.
655 + User must test for such outcome and deal directly with uncompressed data
Yann Colletf246cf52016-07-06 20:30:52 +0200656 + ZSTD_decompressBlock() doesn't accept uncompressed data as input !!!
657 + In case of multiple successive blocks, decoder must be informed of uncompressed block existence to follow proper history.
658 Use ZSTD_insertBlock() in such a case.
Yann Colletd3b7f8d2016-06-04 19:47:02 +0200659*/
660
Yann Colletcf05b9d2016-07-18 16:52:10 +0200661#define ZSTD_BLOCKSIZE_ABSOLUTEMAX (128 * 1024) /* define, for static allocation */
inikep82057aa2016-10-06 13:23:52 +0200662/*===== Raw zstd block functions =====*/
Yann Colletcf05b9d2016-07-18 16:52:10 +0200663ZSTDLIB_API size_t ZSTD_getBlockSizeMax(ZSTD_CCtx* cctx);
Yann Colletd3b7f8d2016-06-04 19:47:02 +0200664ZSTDLIB_API size_t ZSTD_compressBlock (ZSTD_CCtx* cctx, void* dst, size_t dstCapacity, const void* src, size_t srcSize);
665ZSTDLIB_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 +0200666ZSTDLIB_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 +0200667
668
Nick Terrell05c00f22016-11-29 11:46:37 -0800669#endif /* ZSTD_H_ZSTD_STATIC_LINKING_ONLY */
Yann Colletd3b7f8d2016-06-04 19:47:02 +0200670
Yann Collet4856a002015-01-24 01:58:16 +0100671#if defined (__cplusplus)
672}
673#endif