blob: f29120ede81e31dd7a8f781d5a03046e45a9b1ea [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***************************************/
55/** from faster to stronger */
56typedef enum { ZSTD_fast, ZSTD_greedy, ZSTD_lazy, ZSTD_lazy2, ZSTD_btlazy2 } ZSTD_strategy;
57
58typedef struct
59{
60 U32 windowLog; /* largest match distance : impact decompression buffer size */
61 U32 contentLog; /* full search segment : larger == more compression, slower, more memory (useless for fast) */
62 U32 hashLog; /* dispatch table : larger == more memory, faster*/
63 U32 searchLog; /* nb of searches : larger == more compression, slower*/
64 U32 searchLength; /* size of matches : larger == faster decompression */
65 ZSTD_strategy strategy;
66} ZSTD_parameters;
67
68
69/* *************************************
70* Advanced function
71***************************************/
72/** ZSTD_compress_advanced
73* Same as ZSTD_compressCCtx(), with fine-tune control of each compression parameter */
74size_t ZSTD_compress_advanced (ZSTD_CCtx* ctx,
75 void* dst, size_t maxDstSize,
76 const void* src, size_t srcSize,
77 ZSTD_parameters params);
78
79/** ZSTD_validateParams
80 correct params value to remain within authorized range
81 srcSizeHint value is optional, select 0 if not known */
82void ZSTD_validateParams(ZSTD_parameters* params, U64 srcSizeHint);
Yann Collet439eb772015-01-31 10:52:59 +010083
84
Yann Collet353c5d22015-10-21 14:39:26 +010085/* *************************************
Yann Collet439eb772015-01-31 10:52:59 +010086* Streaming functions
Yann Collet353c5d22015-10-21 14:39:26 +010087***************************************/
Yann Collet5be2dd22015-11-11 13:43:58 +010088size_t ZSTD_compressBegin(ZSTD_CCtx* cctx, void* dst, size_t maxDstSize, int compressionLevel, U64 srcSizeHint);
Yann Collet353c5d22015-10-21 14:39:26 +010089size_t ZSTD_compressContinue(ZSTD_CCtx* cctx, void* dst, size_t maxDstSize, const void* src, size_t srcSize);
90size_t ZSTD_compressEnd(ZSTD_CCtx* cctx, void* dst, size_t maxDstSize);
Yann Collet439eb772015-01-31 10:52:59 +010091
Yann Collet439eb772015-01-31 10:52:59 +010092
Yann Collet353c5d22015-10-21 14:39:26 +010093typedef struct ZSTD_DCtx_s ZSTD_DCtx;
94ZSTD_DCtx* ZSTD_createDCtx(void);
95size_t ZSTD_resetDCtx(ZSTD_DCtx* dctx);
96size_t ZSTD_freeDCtx(ZSTD_DCtx* dctx);
Yann Collet7393c5a2015-07-04 18:20:42 -080097
Yann Collet353c5d22015-10-21 14:39:26 +010098size_t ZSTD_nextSrcSizeToDecompress(ZSTD_DCtx* dctx);
99size_t ZSTD_decompressContinue(ZSTD_DCtx* dctx, void* dst, size_t maxDstSize, const void* src, size_t srcSize);
Yann Collet00be3432015-02-20 18:53:31 +0100100/*
101 Use above functions alternatively.
Yann Collet59aac5f2015-10-14 16:28:19 +0100102 ZSTD_nextSrcSizeToDecompress() tells how much bytes to provide as 'srcSize' to ZSTD_decompressContinue().
Yann Colletb1f3f4b2015-10-18 22:18:32 +0100103 ZSTD_decompressContinue() will use previous data blocks to improve compression if they are located prior to current block.
Yann Collet59aac5f2015-10-14 16:28:19 +0100104 Result is the number of bytes regenerated within 'dst'.
Yann Collet00be3432015-02-20 18:53:31 +0100105 It can be zero, which is not an error; it just means ZSTD_decompressContinue() has decoded some header.
106*/
Yann Collet439eb772015-01-31 10:52:59 +0100107
Yann Collet353c5d22015-10-21 14:39:26 +0100108/* *************************************
Yann Colletb1f3f4b2015-10-18 22:18:32 +0100109* Prefix - version detection
Yann Collet353c5d22015-10-21 14:39:26 +0100110***************************************/
Yann Collet083fcc82015-10-25 14:06:35 +0100111#define ZSTD_magicNumber 0xFD2FB523 /* v0.3 (current)*/
Yann Colletb1f3f4b2015-10-18 22:18:32 +0100112
113
Yann Collet353c5d22015-10-21 14:39:26 +0100114/* *************************************
Yann Collet5be2dd22015-11-11 13:43:58 +0100115* Pre-defined compression levels
116***************************************/
117#define ZSTD_MAX_CLEVEL 20
118#define ZSTD_WINDOWLOG_MAX 26
119#define ZSTD_WINDOWLOG_MIN 18
120#define ZSTD_CONTENTLOG_MAX (ZSTD_WINDOWLOG_MAX+1)
121#define ZSTD_CONTENTLOG_MIN 4
122#define ZSTD_HASHLOG_MAX 28
123#define ZSTD_HASHLOG_MIN 4
124#define ZSTD_SEARCHLOG_MAX (ZSTD_CONTENTLOG_MAX-1)
125#define ZSTD_SEARCHLOG_MIN 1
126#define ZSTD_SEARCHLENGTH_MAX 7
127#define ZSTD_SEARCHLENGTH_MIN 4
128
129static const ZSTD_parameters ZSTD_defaultParameters[2][ZSTD_MAX_CLEVEL+1] = {
130{ /* for <= 128 KB */
131 /* W, C, H, S, L, strat */
132 { 17, 12, 12, 1, 4, ZSTD_fast }, /* level 0 - never used */
133 { 17, 12, 13, 1, 6, ZSTD_fast }, /* level 1 */
134 { 17, 15, 16, 1, 5, ZSTD_fast }, /* level 2 */
135 { 17, 16, 17, 1, 5, ZSTD_fast }, /* level 3 */
136 { 17, 13, 15, 2, 4, ZSTD_greedy }, /* level 4 */
137 { 17, 15, 17, 3, 4, ZSTD_greedy }, /* level 5 */
138 { 17, 14, 17, 3, 4, ZSTD_lazy }, /* level 6 */
139 { 17, 16, 17, 4, 4, ZSTD_lazy }, /* level 7 */
140 { 17, 16, 17, 4, 4, ZSTD_lazy2 }, /* level 8 */
141 { 17, 17, 16, 5, 4, ZSTD_lazy2 }, /* level 9 */
142 { 17, 17, 16, 6, 4, ZSTD_lazy2 }, /* level 10 */
143 { 17, 17, 16, 7, 4, ZSTD_lazy2 }, /* level 11 */
144 { 17, 17, 16, 8, 4, ZSTD_lazy2 }, /* level 12 */
145 { 17, 18, 16, 4, 4, ZSTD_btlazy2 }, /* level 13 */
146 { 17, 18, 16, 5, 4, ZSTD_btlazy2 }, /* level 14 */
147 { 17, 18, 16, 6, 4, ZSTD_btlazy2 }, /* level 15 */
148 { 17, 18, 16, 7, 4, ZSTD_btlazy2 }, /* level 16 */
149 { 17, 18, 16, 8, 4, ZSTD_btlazy2 }, /* level 17 */
150 { 17, 18, 16, 9, 4, ZSTD_btlazy2 }, /* level 18 */
151 { 17, 18, 16, 10, 4, ZSTD_btlazy2 }, /* level 19 */
152 { 17, 18, 18, 12, 4, ZSTD_btlazy2 }, /* level 20 */
153},
154{ /* for > 128 KB */
155 /* W, C, H, S, L, strat */
156 { 18, 12, 12, 1, 4, ZSTD_fast }, /* level 0 - never used */
157 { 18, 14, 14, 1, 7, ZSTD_fast }, /* level 1 - in fact redirected towards zstd fast */
158 { 19, 15, 16, 1, 6, ZSTD_fast }, /* level 2 */
159 { 20, 18, 20, 1, 6, ZSTD_fast }, /* level 3 */
160 { 21, 19, 21, 1, 6, ZSTD_fast }, /* level 4 */
161 { 20, 13, 18, 5, 5, ZSTD_greedy }, /* level 5 */
162 { 20, 17, 19, 3, 5, ZSTD_greedy }, /* level 6 */
163 { 21, 17, 20, 3, 5, ZSTD_lazy }, /* level 7 */
164 { 21, 19, 20, 3, 5, ZSTD_lazy }, /* level 8 */
165 { 21, 20, 20, 3, 5, ZSTD_lazy2 }, /* level 9 */
166 { 21, 19, 20, 4, 5, ZSTD_lazy2 }, /* level 10 */
167 { 22, 20, 22, 4, 5, ZSTD_lazy2 }, /* level 11 */
168 { 22, 20, 22, 5, 5, ZSTD_lazy2 }, /* level 12 */
169 { 22, 21, 22, 5, 5, ZSTD_lazy2 }, /* level 13 */
170 { 22, 22, 23, 5, 5, ZSTD_lazy2 }, /* level 14 */
171 { 23, 23, 23, 5, 5, ZSTD_lazy2 }, /* level 15 */
172 { 23, 21, 22, 5, 5, ZSTD_btlazy2 }, /* level 16 */
173 { 23, 24, 23, 4, 5, ZSTD_btlazy2 }, /* level 17 */
174 { 25, 24, 23, 5, 5, ZSTD_btlazy2 }, /* level 18 */
175 { 25, 26, 23, 5, 5, ZSTD_btlazy2 }, /* level 19 */
176 { 26, 27, 24, 6, 5, ZSTD_btlazy2 }, /* level 20 */
177}
178};
179
180
181/* *************************************
Yann Collet439eb772015-01-31 10:52:59 +0100182* Error management
Yann Collet353c5d22015-10-21 14:39:26 +0100183***************************************/
Yann Colletb1f3f4b2015-10-18 22:18:32 +0100184#include "error.h"
Yann Collet439eb772015-01-31 10:52:59 +0100185
186
187#if defined (__cplusplus)
188}
Yann Colletc5d46b52015-02-16 18:06:26 +0100189#endif
Yann Colletaa074052015-10-30 11:21:50 +0100190
191#endif /* ZSTD_STATIC_H */