blob: 9638af11ef5dabd5e46a4826929fdff186140685 [file] [log] [blame]
Yann Collet439eb772015-01-31 10:52:59 +01001/*
2 zstd - standard compression library
3 Header File for static linking only
4 Copyright (C) 2014-2015, Yann Collet.
5
6 BSD 2-Clause License (http://www.opensource.org/licenses/bsd-license.php)
7
8 Redistribution and use in source and binary forms, with or without
9 modification, are permitted provided that the following conditions are
10 met:
11 * Redistributions of source code must retain the above copyright
12 notice, this list of conditions and the following disclaimer.
13 * Redistributions in binary form must reproduce the above
14 copyright notice, this list of conditions and the following disclaimer
15 in the documentation and/or other materials provided with the
16 distribution.
17 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18 "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
20 A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
21 OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22 SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23 LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28
29 You can contact the author at :
30 - zstd source repository : https://github.com/Cyan4973/zstd
31 - ztsd public forum : https://groups.google.com/forum/#!forum/lz4c
32*/
Yann Colletaa074052015-10-30 11:21:50 +010033#ifndef ZSTD_STATIC_H
34#define ZSTD_STATIC_H
Yann Collet439eb772015-01-31 10:52:59 +010035
Yann Collet59aac5f2015-10-14 16:28:19 +010036/* The objects defined into this file should be considered experimental.
37 * They are not labelled stable, as their prototype may change in the future.
38 * You can use them for tests, provide feedback, or if you can endure risk of future changes.
39 */
40
Yann Collet439eb772015-01-31 10:52:59 +010041#if defined (__cplusplus)
42extern "C" {
43#endif
44
Yann Collet353c5d22015-10-21 14:39:26 +010045/* *************************************
Yann Collet439eb772015-01-31 10:52:59 +010046* Includes
Yann Collet353c5d22015-10-21 14:39:26 +010047***************************************/
Yann Collet439eb772015-01-31 10:52:59 +010048#include "zstd.h"
Yann Collet5be2dd22015-11-11 13:43:58 +010049#include "mem.h"
50
51
52/* *************************************
53* Types
54***************************************/
Yann Collet5be2dd22015-11-11 13:43:58 +010055#define ZSTD_WINDOWLOG_MAX 26
56#define ZSTD_WINDOWLOG_MIN 18
Yann Collet88fcd292015-11-25 14:42:45 +010057#define ZSTD_WINDOWLOG_ABSOLUTEMIN 11
Yann Collet5be2dd22015-11-11 13:43:58 +010058#define ZSTD_CONTENTLOG_MAX (ZSTD_WINDOWLOG_MAX+1)
59#define ZSTD_CONTENTLOG_MIN 4
60#define ZSTD_HASHLOG_MAX 28
61#define ZSTD_HASHLOG_MIN 4
62#define ZSTD_SEARCHLOG_MAX (ZSTD_CONTENTLOG_MAX-1)
63#define ZSTD_SEARCHLOG_MIN 1
64#define ZSTD_SEARCHLENGTH_MAX 7
65#define ZSTD_SEARCHLENGTH_MIN 4
66
Yann Collet88fcd292015-11-25 14:42:45 +010067/** from faster to stronger */
68typedef enum { ZSTD_fast, ZSTD_greedy, ZSTD_lazy, ZSTD_lazy2, ZSTD_btlazy2 } ZSTD_strategy;
69
70typedef struct
71{
72 U64 srcSize; /* optional : tells how much bytes are present in the frame. Use 0 if not known. */
73 U32 windowLog; /* largest match distance : larger == more compression, more memory needed during decompression */
74 U32 contentLog; /* full search segment : larger == more compression, slower, more memory (useless for fast) */
75 U32 hashLog; /* dispatch table : larger == more memory, faster */
76 U32 searchLog; /* nb of searches : larger == more compression, slower */
77 U32 searchLength; /* size of matches : larger == faster decompression, sometimes less compression */
78 ZSTD_strategy strategy;
79} ZSTD_parameters;
80
81
82/* *************************************
83* Advanced function
84***************************************/
85/** ZSTD_getParams
86* return ZSTD_parameters structure for a selected compression level and srcSize.
87* srcSizeHint value is optional, select 0 if not known */
Christophe Chevalierc6e84532015-12-07 17:44:09 +010088ZSTDLIB_API ZSTD_parameters ZSTD_getParams(int compressionLevel, U64 srcSizeHint);
Yann Collet88fcd292015-11-25 14:42:45 +010089
90/** ZSTD_validateParams
91* correct params value to remain within authorized range */
Christophe Chevalierc6e84532015-12-07 17:44:09 +010092ZSTDLIB_API void ZSTD_validateParams(ZSTD_parameters* params);
Yann Collet88fcd292015-11-25 14:42:45 +010093
94/** ZSTD_compress_advanced
95* Same as ZSTD_compressCCtx(), with fine-tune control of each compression parameter */
Christophe Chevalierc6e84532015-12-07 17:44:09 +010096ZSTDLIB_API size_t ZSTD_compress_advanced (ZSTD_CCtx* ctx,
97 void* dst, size_t maxDstSize,
98 const void* src, size_t srcSize,
99 ZSTD_parameters params);
Yann Collet88fcd292015-11-25 14:42:45 +0100100
Yann Colletfdcad6d2015-12-17 23:50:15 +0100101/** ZSTD_compress_usingDict
102* Same as ZSTD_compressCCtx(), using a Dictionary content as prefix */
103ZSTDLIB_API size_t ZSTD_compress_usingDict(ZSTD_CCtx* ctx,
104 void* dst, size_t maxDstSize,
105 const void* src, size_t srcSize,
106 const void* dict,size_t dictSize,
107 int compressionLevel);
108
Yann Collet88fcd292015-11-25 14:42:45 +0100109
110/* **************************************
Yann Colletfdcad6d2015-12-17 23:50:15 +0100111* Streaming functions (direct mode)
Yann Collet88fcd292015-11-25 14:42:45 +0100112****************************************/
Christophe Chevalierc6e84532015-12-07 17:44:09 +0100113ZSTDLIB_API size_t ZSTD_compressBegin(ZSTD_CCtx* cctx, void* dst, size_t maxDstSize, int compressionLevel);
114ZSTDLIB_API size_t ZSTD_compressBegin_advanced(ZSTD_CCtx* ctx, void* dst, size_t maxDstSize, ZSTD_parameters params);
115ZSTDLIB_API size_t ZSTD_compress_insertDictionary(ZSTD_CCtx* ctx, const void* src, size_t srcSize);
Yann Collet417890c2015-12-04 17:16:37 +0100116
Christophe Chevalierc6e84532015-12-07 17:44:09 +0100117ZSTDLIB_API size_t ZSTD_compressContinue(ZSTD_CCtx* cctx, void* dst, size_t maxDstSize, const void* src, size_t srcSize);
118ZSTDLIB_API size_t ZSTD_compressEnd(ZSTD_CCtx* cctx, void* dst, size_t maxDstSize);
Yann Collet88fcd292015-11-25 14:42:45 +0100119
Yann Collet53fbf012015-11-28 14:08:01 +0100120/**
121 Streaming compression, bufferless mode
122
123 A ZSTD_CCtx object is required to track streaming operations.
124 Use ZSTD_createCCtx() / ZSTD_freeCCtx() to manage it.
125 A ZSTD_CCtx object can be re-used multiple times.
126
127 First operation is to start a new frame.
128 Use ZSTD_compressBegin().
129 You may also prefer the advanced derivative ZSTD_compressBegin_advanced(), for finer parameter control.
130
Yann Collet0cde77b2015-12-08 14:47:46 +0100131 It's then possible to add a dictionary with ZSTD_compress_insertDictionary()
Yann Collet417890c2015-12-04 17:16:37 +0100132 Note that dictionary presence is a "hidden" information,
133 the decoder needs to be aware that it is required for proper decoding, or decoding will fail.
134
Yann Collet53fbf012015-11-28 14:08:01 +0100135 Then, consume your input using ZSTD_compressContinue().
136 The interface is synchronous, so all input will be consumed.
137 You must ensure there is enough space in destination buffer to store compressed data under worst case scenario.
138 Worst case evaluation is provided by ZSTD_compressBound().
139
140 Finish a frame with ZSTD_compressEnd(), which will write the epilogue.
141 Without it, the frame will be considered incomplete by decoders.
142 You can then re-use ZSTD_CCtx to compress new frames.
143*/
144
Yann Collet88fcd292015-11-25 14:42:45 +0100145
146typedef struct ZSTD_DCtx_s ZSTD_DCtx;
Christophe Chevalierc6e84532015-12-07 17:44:09 +0100147ZSTDLIB_API ZSTD_DCtx* ZSTD_createDCtx(void);
148ZSTDLIB_API size_t ZSTD_freeDCtx(ZSTD_DCtx* dctx);
Yann Collet88fcd292015-11-25 14:42:45 +0100149
Christophe Chevalierc6e84532015-12-07 17:44:09 +0100150ZSTDLIB_API size_t ZSTD_resetDCtx(ZSTD_DCtx* dctx);
151ZSTDLIB_API size_t ZSTD_getFrameParams(ZSTD_parameters* params, const void* src, size_t srcSize);
152ZSTDLIB_API void ZSTD_decompress_insertDictionary(ZSTD_DCtx* ctx, const void* src, size_t srcSize);
Yann Collet417890c2015-12-04 17:16:37 +0100153
Christophe Chevalierc6e84532015-12-07 17:44:09 +0100154ZSTDLIB_API size_t ZSTD_nextSrcSizeToDecompress(ZSTD_DCtx* dctx);
155ZSTDLIB_API size_t ZSTD_decompressContinue(ZSTD_DCtx* dctx, void* dst, size_t maxDstSize, const void* src, size_t srcSize);
Yann Collet417890c2015-12-04 17:16:37 +0100156
Yann Collet88fcd292015-11-25 14:42:45 +0100157/**
158 Streaming decompression, bufferless mode
159
160 A ZSTD_DCtx object is required to track streaming operations.
161 Use ZSTD_createDCtx() / ZSTD_freeDCtx() to manage it.
162 A ZSTD_DCtx object can be re-used multiple times. Use ZSTD_resetDCtx() to return to fresh status.
163
164 First operation is to retrieve frame parameters, using ZSTD_getFrameParams().
165 This function doesn't consume its input. It needs enough input data to properly decode the frame header.
Yann Collet417890c2015-12-04 17:16:37 +0100166 Objective is to retrieve *params.windowlog, to know minimum amount of memory required during decoding.
Yann Collet800fa6c2015-11-27 14:30:23 +0100167 Result : 0 when successful, it means the ZSTD_parameters structure has been filled.
Yann Collet88fcd292015-11-25 14:42:45 +0100168 >0 : means there is not enough data into src. Provides the expected size to successfully decode header.
169 errorCode, which can be tested using ZSTD_isError() (For example, if it's not a ZSTD header)
170
Yann Collet417890c2015-12-04 17:16:37 +0100171 Then, you can optionally insert a dictionary. This operation must mimic the compressor behavior, otherwise decompression will fail or be corrupted.
172
Yann Collet88fcd292015-11-25 14:42:45 +0100173 Then it's possible to start decompression.
174 Use ZSTD_nextSrcSizeToDecompress() and ZSTD_decompressContinue() alternatively.
175 ZSTD_nextSrcSizeToDecompress() tells how much bytes to provide as 'srcSize' to ZSTD_decompressContinue().
Yann Collet417890c2015-12-04 17:16:37 +0100176 ZSTD_decompressContinue() requires this exact amount of bytes, or it will fail.
Yann Collet800fa6c2015-11-27 14:30:23 +0100177 ZSTD_decompressContinue() needs previous data blocks during decompression, up to (1 << windowlog).
178 They should preferably be located contiguously, prior to current block. Alternatively, a round buffer is also possible.
Yann Collet88fcd292015-11-25 14:42:45 +0100179
180 @result of ZSTD_decompressContinue() is the number of bytes regenerated within 'dst'.
181 It can be zero, which is not an error; it just means ZSTD_decompressContinue() has decoded some header.
182
183 A frame is fully decoded when ZSTD_nextSrcSizeToDecompress() returns zero.
184*/
185
186
187/* *************************************
188* Pre-defined compression levels
189***************************************/
190#define ZSTD_MAX_CLEVEL 20
Christophe Chevalier2abb04d2015-12-09 23:55:23 +0100191ZSTDLIB_API unsigned ZSTD_maxCLevel (void);
Yann Colletd6080882015-12-09 09:05:22 +0100192static const ZSTD_parameters ZSTD_defaultParameters[4][ZSTD_MAX_CLEVEL+1] = {
Yann Collet88fcd292015-11-25 14:42:45 +0100193{ /* "default" */
Yann Colleta3082592015-12-02 13:38:48 +0100194 /* W, C, H, S, L, strat */
Yann Collet88fcd292015-11-25 14:42:45 +0100195 { 0, 18, 12, 12, 1, 4, ZSTD_fast }, /* level 0 - never used */
196 { 0, 19, 13, 14, 1, 7, ZSTD_fast }, /* level 1 */
197 { 0, 19, 15, 16, 1, 6, ZSTD_fast }, /* level 2 */
198 { 0, 20, 18, 20, 1, 6, ZSTD_fast }, /* level 3 */
199 { 0, 21, 19, 21, 1, 6, ZSTD_fast }, /* level 4 */
200 { 0, 20, 14, 18, 3, 5, ZSTD_greedy }, /* level 5 */
201 { 0, 20, 18, 19, 3, 5, ZSTD_greedy }, /* level 6 */
202 { 0, 21, 17, 20, 3, 5, ZSTD_lazy }, /* level 7 */
203 { 0, 21, 19, 20, 3, 5, ZSTD_lazy }, /* level 8 */
204 { 0, 21, 20, 20, 3, 5, ZSTD_lazy2 }, /* level 9 */
205 { 0, 21, 19, 21, 4, 5, ZSTD_lazy2 }, /* level 10 */
206 { 0, 22, 20, 22, 4, 5, ZSTD_lazy2 }, /* level 11 */
207 { 0, 22, 20, 22, 5, 5, ZSTD_lazy2 }, /* level 12 */
208 { 0, 22, 21, 22, 5, 5, ZSTD_lazy2 }, /* level 13 */
209 { 0, 22, 22, 23, 5, 5, ZSTD_lazy2 }, /* level 14 */
210 { 0, 23, 23, 23, 5, 5, ZSTD_lazy2 }, /* level 15 */
211 { 0, 23, 21, 22, 5, 5, ZSTD_btlazy2 }, /* level 16 */
212 { 0, 23, 24, 23, 4, 5, ZSTD_btlazy2 }, /* level 17 */
213 { 0, 25, 24, 23, 5, 5, ZSTD_btlazy2 }, /* level 18 */
214 { 0, 25, 26, 23, 5, 5, ZSTD_btlazy2 }, /* level 19 */
Yann Collet53fbf012015-11-28 14:08:01 +0100215 { 0, 26, 27, 25, 9, 5, ZSTD_btlazy2 }, /* level 20 */
Yann Collet5be2dd22015-11-11 13:43:58 +0100216},
Yann Colletd6080882015-12-09 09:05:22 +0100217{ /* for srcSize <= 256 KB */
Yann Colletf54f5702015-12-16 19:38:54 +0100218 /* W, C, H, S, L, strat */
219 { 0, 18, 13, 14, 1, 7, ZSTD_fast }, /* level 0 - never used */
220 { 0, 18, 14, 15, 1, 6, ZSTD_fast }, /* level 1 */
221 { 0, 18, 14, 15, 1, 5, ZSTD_fast }, /* level 2 */
222 { 0, 18, 12, 15, 3, 7, ZSTD_greedy }, /* level 3 */
223 { 0, 18, 13, 15, 4, 7, ZSTD_greedy }, /* level 4 */
224 { 0, 18, 14, 15, 5, 7, ZSTD_greedy }, /* level 5 */
225 { 0, 18, 13, 15, 4, 7, ZSTD_lazy }, /* level 6 */
226 { 0, 18, 14, 16, 5, 7, ZSTD_lazy }, /* level 7 */
227 { 0, 18, 15, 16, 6, 7, ZSTD_lazy }, /* level 8 */
228 { 0, 18, 15, 15, 7, 7, ZSTD_lazy }, /* level 9 */
229 { 0, 18, 16, 16, 7, 7, ZSTD_lazy }, /* level 10 */
230 { 0, 18, 16, 16, 8, 4, ZSTD_lazy }, /* level 11 */
231 { 0, 18, 17, 16, 8, 4, ZSTD_lazy }, /* level 12 */
232 { 0, 18, 17, 16, 9, 4, ZSTD_lazy }, /* level 13 */
233 { 0, 18, 18, 16, 9, 4, ZSTD_lazy }, /* level 14 */
234 { 0, 18, 17, 17, 9, 4, ZSTD_lazy2 }, /* level 15 */
235 { 0, 18, 18, 18, 9, 4, ZSTD_lazy2 }, /* level 16 */
236 { 0, 18, 18, 18, 10, 4, ZSTD_lazy2 }, /* level 17 */
237 { 0, 18, 18, 18, 11, 4, ZSTD_lazy2 }, /* level 18 */
238 { 0, 18, 18, 18, 12, 4, ZSTD_lazy2 }, /* level 19 */
239 { 0, 18, 18, 18, 13, 4, ZSTD_lazy2 }, /* level 20 */
Yann Colletd6080882015-12-09 09:05:22 +0100240},
Yann Collet88fcd292015-11-25 14:42:45 +0100241{ /* for srcSize <= 128 KB */
Yann Colleta3082592015-12-02 13:38:48 +0100242 /* W, C, H, S, L, strat */
Yann Collet88fcd292015-11-25 14:42:45 +0100243 { 0, 17, 12, 12, 1, 4, ZSTD_fast }, /* level 0 - never used */
244 { 0, 17, 12, 13, 1, 6, ZSTD_fast }, /* level 1 */
245 { 0, 17, 15, 16, 1, 5, ZSTD_fast }, /* level 2 */
246 { 0, 17, 16, 17, 1, 5, ZSTD_fast }, /* level 3 */
247 { 0, 17, 13, 15, 2, 4, ZSTD_greedy }, /* level 4 */
248 { 0, 17, 15, 17, 3, 4, ZSTD_greedy }, /* level 5 */
249 { 0, 17, 14, 17, 3, 4, ZSTD_lazy }, /* level 6 */
250 { 0, 17, 16, 17, 4, 4, ZSTD_lazy }, /* level 7 */
251 { 0, 17, 16, 17, 4, 4, ZSTD_lazy2 }, /* level 8 */
252 { 0, 17, 17, 16, 5, 4, ZSTD_lazy2 }, /* level 9 */
253 { 0, 17, 17, 16, 6, 4, ZSTD_lazy2 }, /* level 10 */
254 { 0, 17, 17, 16, 7, 4, ZSTD_lazy2 }, /* level 11 */
255 { 0, 17, 17, 16, 8, 4, ZSTD_lazy2 }, /* level 12 */
256 { 0, 17, 18, 16, 4, 4, ZSTD_btlazy2 }, /* level 13 */
257 { 0, 17, 18, 16, 5, 4, ZSTD_btlazy2 }, /* level 14 */
258 { 0, 17, 18, 16, 6, 4, ZSTD_btlazy2 }, /* level 15 */
259 { 0, 17, 18, 16, 7, 4, ZSTD_btlazy2 }, /* level 16 */
260 { 0, 17, 18, 16, 8, 4, ZSTD_btlazy2 }, /* level 17 */
261 { 0, 17, 18, 16, 9, 4, ZSTD_btlazy2 }, /* level 18 */
262 { 0, 17, 18, 16, 10, 4, ZSTD_btlazy2 }, /* level 19 */
263 { 0, 17, 18, 18, 12, 4, ZSTD_btlazy2 }, /* level 20 */
264},
Yann Colleta3082592015-12-02 13:38:48 +0100265{ /* for srcSize <= 16 KB */
266 /* W, C, H, S, L, strat */
267 { 0, 0, 0, 0, 0, 0, ZSTD_fast }, /* level 0 - never used */
268 { 0, 14, 14, 14, 1, 4, ZSTD_fast }, /* level 1 */
269 { 0, 14, 14, 16, 1, 4, ZSTD_fast }, /* level 1 */
270 { 0, 14, 14, 14, 5, 4, ZSTD_greedy }, /* level 3 */
271 { 0, 14, 14, 14, 8, 4, ZSTD_greedy }, /* level 4 */
272 { 0, 14, 11, 14, 6, 4, ZSTD_lazy }, /* level 5 */
273 { 0, 14, 14, 13, 6, 5, ZSTD_lazy }, /* level 6 */
274 { 0, 14, 14, 14, 7, 6, ZSTD_lazy }, /* level 7 */
275 { 0, 14, 14, 14, 8, 4, ZSTD_lazy }, /* level 8 */
276 { 0, 14, 14, 15, 9, 4, ZSTD_lazy }, /* level 9 */
277 { 0, 14, 14, 15, 10, 4, ZSTD_lazy }, /* level 10 */
278 { 0, 14, 15, 15, 6, 4, ZSTD_btlazy2 }, /* level 11 */
279 { 0, 14, 15, 15, 7, 4, ZSTD_btlazy2 }, /* level 12 */
280 { 0, 14, 15, 15, 8, 4, ZSTD_btlazy2 }, /* level 13 */
281 { 0, 14, 15, 15, 9, 4, ZSTD_btlazy2 }, /* level 14 */
282 { 0, 14, 15, 15, 10, 4, ZSTD_btlazy2 }, /* level 15 */
283 { 0, 14, 15, 15, 11, 4, ZSTD_btlazy2 }, /* level 16 */
284 { 0, 14, 15, 15, 12, 4, ZSTD_btlazy2 }, /* level 17 */
285 { 0, 14, 15, 15, 13, 4, ZSTD_btlazy2 }, /* level 18 */
286 { 0, 14, 15, 15, 14, 4, ZSTD_btlazy2 }, /* level 19 */
287 { 0, 14, 15, 15, 15, 4, ZSTD_btlazy2 }, /* level 20 */
288},
Yann Collet5be2dd22015-11-11 13:43:58 +0100289};
290
291
292/* *************************************
Yann Collet439eb772015-01-31 10:52:59 +0100293* Error management
Yann Collet353c5d22015-10-21 14:39:26 +0100294***************************************/
Yann Colletb1f3f4b2015-10-18 22:18:32 +0100295#include "error.h"
Yann Collet439eb772015-01-31 10:52:59 +0100296
297
298#if defined (__cplusplus)
299}
Yann Colletc5d46b52015-02-16 18:06:26 +0100300#endif
Yann Colletaa074052015-10-30 11:21:50 +0100301
302#endif /* ZSTD_STATIC_H */