blob: 6a987026a2071a0c1cee9e3beb7ab67f251b936a [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
101
102/* **************************************
103* Streaming functions (bufferless mode)
104****************************************/
Christophe Chevalierc6e84532015-12-07 17:44:09 +0100105ZSTDLIB_API size_t ZSTD_compressBegin(ZSTD_CCtx* cctx, void* dst, size_t maxDstSize, int compressionLevel);
106ZSTDLIB_API size_t ZSTD_compressBegin_advanced(ZSTD_CCtx* ctx, void* dst, size_t maxDstSize, ZSTD_parameters params);
107ZSTDLIB_API size_t ZSTD_compress_insertDictionary(ZSTD_CCtx* ctx, const void* src, size_t srcSize);
Yann Collet417890c2015-12-04 17:16:37 +0100108
Christophe Chevalierc6e84532015-12-07 17:44:09 +0100109ZSTDLIB_API size_t ZSTD_compressContinue(ZSTD_CCtx* cctx, void* dst, size_t maxDstSize, const void* src, size_t srcSize);
110ZSTDLIB_API size_t ZSTD_compressEnd(ZSTD_CCtx* cctx, void* dst, size_t maxDstSize);
Yann Collet88fcd292015-11-25 14:42:45 +0100111
Yann Collet53fbf012015-11-28 14:08:01 +0100112/**
113 Streaming compression, bufferless mode
114
115 A ZSTD_CCtx object is required to track streaming operations.
116 Use ZSTD_createCCtx() / ZSTD_freeCCtx() to manage it.
117 A ZSTD_CCtx object can be re-used multiple times.
118
119 First operation is to start a new frame.
120 Use ZSTD_compressBegin().
121 You may also prefer the advanced derivative ZSTD_compressBegin_advanced(), for finer parameter control.
122
Yann Collet417890c2015-12-04 17:16:37 +0100123 It's then possible to add a dictionary with ZSTD_compressDictionary()
124 Note that dictionary presence is a "hidden" information,
125 the decoder needs to be aware that it is required for proper decoding, or decoding will fail.
126
Yann Collet53fbf012015-11-28 14:08:01 +0100127 Then, consume your input using ZSTD_compressContinue().
128 The interface is synchronous, so all input will be consumed.
129 You must ensure there is enough space in destination buffer to store compressed data under worst case scenario.
130 Worst case evaluation is provided by ZSTD_compressBound().
131
132 Finish a frame with ZSTD_compressEnd(), which will write the epilogue.
133 Without it, the frame will be considered incomplete by decoders.
134 You can then re-use ZSTD_CCtx to compress new frames.
135*/
136
Yann Collet88fcd292015-11-25 14:42:45 +0100137
138typedef struct ZSTD_DCtx_s ZSTD_DCtx;
Christophe Chevalierc6e84532015-12-07 17:44:09 +0100139ZSTDLIB_API ZSTD_DCtx* ZSTD_createDCtx(void);
140ZSTDLIB_API size_t ZSTD_freeDCtx(ZSTD_DCtx* dctx);
Yann Collet88fcd292015-11-25 14:42:45 +0100141
Christophe Chevalierc6e84532015-12-07 17:44:09 +0100142ZSTDLIB_API size_t ZSTD_resetDCtx(ZSTD_DCtx* dctx);
143ZSTDLIB_API size_t ZSTD_getFrameParams(ZSTD_parameters* params, const void* src, size_t srcSize);
144ZSTDLIB_API void ZSTD_decompress_insertDictionary(ZSTD_DCtx* ctx, const void* src, size_t srcSize);
Yann Collet417890c2015-12-04 17:16:37 +0100145
Christophe Chevalierc6e84532015-12-07 17:44:09 +0100146ZSTDLIB_API size_t ZSTD_nextSrcSizeToDecompress(ZSTD_DCtx* dctx);
147ZSTDLIB_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 +0100148
Yann Collet88fcd292015-11-25 14:42:45 +0100149/**
150 Streaming decompression, bufferless mode
151
152 A ZSTD_DCtx object is required to track streaming operations.
153 Use ZSTD_createDCtx() / ZSTD_freeDCtx() to manage it.
154 A ZSTD_DCtx object can be re-used multiple times. Use ZSTD_resetDCtx() to return to fresh status.
155
156 First operation is to retrieve frame parameters, using ZSTD_getFrameParams().
157 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 +0100158 Objective is to retrieve *params.windowlog, to know minimum amount of memory required during decoding.
Yann Collet800fa6c2015-11-27 14:30:23 +0100159 Result : 0 when successful, it means the ZSTD_parameters structure has been filled.
Yann Collet88fcd292015-11-25 14:42:45 +0100160 >0 : means there is not enough data into src. Provides the expected size to successfully decode header.
161 errorCode, which can be tested using ZSTD_isError() (For example, if it's not a ZSTD header)
162
Yann Collet417890c2015-12-04 17:16:37 +0100163 Then, you can optionally insert a dictionary. This operation must mimic the compressor behavior, otherwise decompression will fail or be corrupted.
164
Yann Collet88fcd292015-11-25 14:42:45 +0100165 Then it's possible to start decompression.
166 Use ZSTD_nextSrcSizeToDecompress() and ZSTD_decompressContinue() alternatively.
167 ZSTD_nextSrcSizeToDecompress() tells how much bytes to provide as 'srcSize' to ZSTD_decompressContinue().
Yann Collet417890c2015-12-04 17:16:37 +0100168 ZSTD_decompressContinue() requires this exact amount of bytes, or it will fail.
Yann Collet800fa6c2015-11-27 14:30:23 +0100169 ZSTD_decompressContinue() needs previous data blocks during decompression, up to (1 << windowlog).
170 They should preferably be located contiguously, prior to current block. Alternatively, a round buffer is also possible.
Yann Collet88fcd292015-11-25 14:42:45 +0100171
172 @result of ZSTD_decompressContinue() is the number of bytes regenerated within 'dst'.
173 It can be zero, which is not an error; it just means ZSTD_decompressContinue() has decoded some header.
174
175 A frame is fully decoded when ZSTD_nextSrcSizeToDecompress() returns zero.
176*/
177
178
179/* *************************************
180* Pre-defined compression levels
181***************************************/
182#define ZSTD_MAX_CLEVEL 20
Yann Colleta3082592015-12-02 13:38:48 +0100183static const ZSTD_parameters ZSTD_defaultParameters[3][ZSTD_MAX_CLEVEL+1] = {
Yann Collet88fcd292015-11-25 14:42:45 +0100184{ /* "default" */
Yann Colleta3082592015-12-02 13:38:48 +0100185 /* W, C, H, S, L, strat */
Yann Collet88fcd292015-11-25 14:42:45 +0100186 { 0, 18, 12, 12, 1, 4, ZSTD_fast }, /* level 0 - never used */
187 { 0, 19, 13, 14, 1, 7, ZSTD_fast }, /* level 1 */
188 { 0, 19, 15, 16, 1, 6, ZSTD_fast }, /* level 2 */
189 { 0, 20, 18, 20, 1, 6, ZSTD_fast }, /* level 3 */
190 { 0, 21, 19, 21, 1, 6, ZSTD_fast }, /* level 4 */
191 { 0, 20, 14, 18, 3, 5, ZSTD_greedy }, /* level 5 */
192 { 0, 20, 18, 19, 3, 5, ZSTD_greedy }, /* level 6 */
193 { 0, 21, 17, 20, 3, 5, ZSTD_lazy }, /* level 7 */
194 { 0, 21, 19, 20, 3, 5, ZSTD_lazy }, /* level 8 */
195 { 0, 21, 20, 20, 3, 5, ZSTD_lazy2 }, /* level 9 */
196 { 0, 21, 19, 21, 4, 5, ZSTD_lazy2 }, /* level 10 */
197 { 0, 22, 20, 22, 4, 5, ZSTD_lazy2 }, /* level 11 */
198 { 0, 22, 20, 22, 5, 5, ZSTD_lazy2 }, /* level 12 */
199 { 0, 22, 21, 22, 5, 5, ZSTD_lazy2 }, /* level 13 */
200 { 0, 22, 22, 23, 5, 5, ZSTD_lazy2 }, /* level 14 */
201 { 0, 23, 23, 23, 5, 5, ZSTD_lazy2 }, /* level 15 */
202 { 0, 23, 21, 22, 5, 5, ZSTD_btlazy2 }, /* level 16 */
203 { 0, 23, 24, 23, 4, 5, ZSTD_btlazy2 }, /* level 17 */
204 { 0, 25, 24, 23, 5, 5, ZSTD_btlazy2 }, /* level 18 */
205 { 0, 25, 26, 23, 5, 5, ZSTD_btlazy2 }, /* level 19 */
Yann Collet53fbf012015-11-28 14:08:01 +0100206 { 0, 26, 27, 25, 9, 5, ZSTD_btlazy2 }, /* level 20 */
Yann Collet5be2dd22015-11-11 13:43:58 +0100207},
Yann Collet88fcd292015-11-25 14:42:45 +0100208{ /* for srcSize <= 128 KB */
Yann Colleta3082592015-12-02 13:38:48 +0100209 /* W, C, H, S, L, strat */
Yann Collet88fcd292015-11-25 14:42:45 +0100210 { 0, 17, 12, 12, 1, 4, ZSTD_fast }, /* level 0 - never used */
211 { 0, 17, 12, 13, 1, 6, ZSTD_fast }, /* level 1 */
212 { 0, 17, 15, 16, 1, 5, ZSTD_fast }, /* level 2 */
213 { 0, 17, 16, 17, 1, 5, ZSTD_fast }, /* level 3 */
214 { 0, 17, 13, 15, 2, 4, ZSTD_greedy }, /* level 4 */
215 { 0, 17, 15, 17, 3, 4, ZSTD_greedy }, /* level 5 */
216 { 0, 17, 14, 17, 3, 4, ZSTD_lazy }, /* level 6 */
217 { 0, 17, 16, 17, 4, 4, ZSTD_lazy }, /* level 7 */
218 { 0, 17, 16, 17, 4, 4, ZSTD_lazy2 }, /* level 8 */
219 { 0, 17, 17, 16, 5, 4, ZSTD_lazy2 }, /* level 9 */
220 { 0, 17, 17, 16, 6, 4, ZSTD_lazy2 }, /* level 10 */
221 { 0, 17, 17, 16, 7, 4, ZSTD_lazy2 }, /* level 11 */
222 { 0, 17, 17, 16, 8, 4, ZSTD_lazy2 }, /* level 12 */
223 { 0, 17, 18, 16, 4, 4, ZSTD_btlazy2 }, /* level 13 */
224 { 0, 17, 18, 16, 5, 4, ZSTD_btlazy2 }, /* level 14 */
225 { 0, 17, 18, 16, 6, 4, ZSTD_btlazy2 }, /* level 15 */
226 { 0, 17, 18, 16, 7, 4, ZSTD_btlazy2 }, /* level 16 */
227 { 0, 17, 18, 16, 8, 4, ZSTD_btlazy2 }, /* level 17 */
228 { 0, 17, 18, 16, 9, 4, ZSTD_btlazy2 }, /* level 18 */
229 { 0, 17, 18, 16, 10, 4, ZSTD_btlazy2 }, /* level 19 */
230 { 0, 17, 18, 18, 12, 4, ZSTD_btlazy2 }, /* level 20 */
231},
Yann Colleta3082592015-12-02 13:38:48 +0100232{ /* for srcSize <= 16 KB */
233 /* W, C, H, S, L, strat */
234 { 0, 0, 0, 0, 0, 0, ZSTD_fast }, /* level 0 - never used */
235 { 0, 14, 14, 14, 1, 4, ZSTD_fast }, /* level 1 */
236 { 0, 14, 14, 16, 1, 4, ZSTD_fast }, /* level 1 */
237 { 0, 14, 14, 14, 5, 4, ZSTD_greedy }, /* level 3 */
238 { 0, 14, 14, 14, 8, 4, ZSTD_greedy }, /* level 4 */
239 { 0, 14, 11, 14, 6, 4, ZSTD_lazy }, /* level 5 */
240 { 0, 14, 14, 13, 6, 5, ZSTD_lazy }, /* level 6 */
241 { 0, 14, 14, 14, 7, 6, ZSTD_lazy }, /* level 7 */
242 { 0, 14, 14, 14, 8, 4, ZSTD_lazy }, /* level 8 */
243 { 0, 14, 14, 15, 9, 4, ZSTD_lazy }, /* level 9 */
244 { 0, 14, 14, 15, 10, 4, ZSTD_lazy }, /* level 10 */
245 { 0, 14, 15, 15, 6, 4, ZSTD_btlazy2 }, /* level 11 */
246 { 0, 14, 15, 15, 7, 4, ZSTD_btlazy2 }, /* level 12 */
247 { 0, 14, 15, 15, 8, 4, ZSTD_btlazy2 }, /* level 13 */
248 { 0, 14, 15, 15, 9, 4, ZSTD_btlazy2 }, /* level 14 */
249 { 0, 14, 15, 15, 10, 4, ZSTD_btlazy2 }, /* level 15 */
250 { 0, 14, 15, 15, 11, 4, ZSTD_btlazy2 }, /* level 16 */
251 { 0, 14, 15, 15, 12, 4, ZSTD_btlazy2 }, /* level 17 */
252 { 0, 14, 15, 15, 13, 4, ZSTD_btlazy2 }, /* level 18 */
253 { 0, 14, 15, 15, 14, 4, ZSTD_btlazy2 }, /* level 19 */
254 { 0, 14, 15, 15, 15, 4, ZSTD_btlazy2 }, /* level 20 */
255},
Yann Collet5be2dd22015-11-11 13:43:58 +0100256};
257
258
259/* *************************************
Yann Collet439eb772015-01-31 10:52:59 +0100260* Error management
Yann Collet353c5d22015-10-21 14:39:26 +0100261***************************************/
Yann Colletb1f3f4b2015-10-18 22:18:32 +0100262#include "error.h"
Yann Collet439eb772015-01-31 10:52:59 +0100263
264
265#if defined (__cplusplus)
266}
Yann Colletc5d46b52015-02-16 18:06:26 +0100267#endif
Yann Colletaa074052015-10-30 11:21:50 +0100268
269#endif /* ZSTD_STATIC_H */