1/*
2 LZ4 HC - High Compression Mode of LZ4
3 Header File
4 Copyright (C) 2011-2017, Yann Collet.
5 BSD 2-Clause License (http://www.opensource.org/licenses/bsd-license.php)
6
7 Redistribution and use in source and binary forms, with or without
8 modification, are permitted provided that the following conditions are
9 met:
10
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
18 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29
30 You can contact the author at :
31 - LZ4 source repository : https://github.com/lz4/lz4
32 - LZ4 public forum : https://groups.google.com/forum/#!forum/lz4c
33*/
34#ifndef LZ4_HC_H_19834876238432
35#define LZ4_HC_H_19834876238432
36
37#if defined (__cplusplus)
38extern "C" {
39#endif
40
41/* --- Dependency --- */
42/* note : lz4hc requires lz4.h/lz4.c for compilation */
43#include "lz4.h" /* stddef, LZ4LIB_API, LZ4_DEPRECATED */
44
45
46/* --- Useful constants --- */
47#define LZ4HC_CLEVEL_MIN 3
48#define LZ4HC_CLEVEL_DEFAULT 9
49#define LZ4HC_CLEVEL_OPT_MIN 11
50#define LZ4HC_CLEVEL_MAX 12
51
52
53/*-************************************
54 * Block Compression
55 **************************************/
56/*! LZ4_compress_HC() :
57 * Compress data from `src` into `dst`, using the more powerful but slower "HC" algorithm.
58 * `dst` must be already allocated.
59 * Compression is guaranteed to succeed if `dstCapacity >= LZ4_compressBound(srcSize)` (see "lz4.h")
60 * Max supported `srcSize` value is LZ4_MAX_INPUT_SIZE (see "lz4.h")
61 * `compressionLevel` : any value between 1 and LZ4HC_CLEVEL_MAX will work.
62 * Values > LZ4HC_CLEVEL_MAX behave the same as LZ4HC_CLEVEL_MAX.
63 * @return : the number of bytes written into 'dst'
64 * or 0 if compression fails.
65 */
66LZ4LIB_API int LZ4_compress_HC (const char* src, char* dst, int srcSize, int dstCapacity, int compressionLevel);
67
68
69/* Note :
70 * Decompression functions are provided within "lz4.h" (BSD license)
71 */
72
73
74/*! LZ4_compress_HC_extStateHC() :
75 * Same as LZ4_compress_HC(), but using an externally allocated memory segment for `state`.
76 * `state` size is provided by LZ4_sizeofStateHC().
77 * Memory segment must be aligned on 8-bytes boundaries (which a normal malloc() should do properly).
78 */
79LZ4LIB_API int LZ4_sizeofStateHC(void);
80LZ4LIB_API int LZ4_compress_HC_extStateHC(void* state, const char* src, char* dst, int srcSize, int maxDstSize, int compressionLevel);
81
82
83/*-************************************
84 * Streaming Compression
85 * Bufferless synchronous API
86 **************************************/
87 typedef union LZ4_streamHC_u LZ4_streamHC_t; /* incomplete type (defined later) */
88
89/*! LZ4_createStreamHC() and LZ4_freeStreamHC() :
90 * These functions create and release memory for LZ4 HC streaming state.
91 * Newly created states are automatically initialized.
92 * Existing states can be re-used several times, using LZ4_resetStreamHC().
93 * These methods are API and ABI stable, they can be used in combination with a DLL.
94 */
95LZ4LIB_API LZ4_streamHC_t* LZ4_createStreamHC(void);
96LZ4LIB_API int LZ4_freeStreamHC (LZ4_streamHC_t* streamHCPtr);
97
98LZ4LIB_API void LZ4_resetStreamHC (LZ4_streamHC_t* streamHCPtr, int compressionLevel);
99LZ4LIB_API int LZ4_loadDictHC (LZ4_streamHC_t* streamHCPtr, const char* dictionary, int dictSize);
100
101LZ4LIB_API int LZ4_compress_HC_continue (LZ4_streamHC_t* streamHCPtr, const char* src, char* dst, int srcSize, int maxDstSize);
102
103LZ4LIB_API int LZ4_saveDictHC (LZ4_streamHC_t* streamHCPtr, char* safeBuffer, int maxDictSize);
104
105/*
106 These functions compress data in successive blocks of any size, using previous blocks as dictionary.
107 One key assumption is that previous blocks (up to 64 KB) remain read-accessible while compressing next blocks.
108 There is an exception for ring buffers, which can be smaller than 64 KB.
109 Ring buffers scenario is automatically detected and handled by LZ4_compress_HC_continue().
110
111 Before starting compression, state must be properly initialized, using LZ4_resetStreamHC().
112 A first "fictional block" can then be designated as initial dictionary, using LZ4_loadDictHC() (Optional).
113
114 Then, use LZ4_compress_HC_continue() to compress each successive block.
115 Previous memory blocks (including initial dictionary when present) must remain accessible and unmodified during compression.
116 'dst' buffer should be sized to handle worst case scenarios (see LZ4_compressBound()), to ensure operation success.
117 Because in case of failure, the API does not guarantee context recovery, and context will have to be reset.
118 If `dst` buffer budget cannot be >= LZ4_compressBound(), consider using LZ4_compress_HC_continue_destSize() instead.
119
120 If, for any reason, previous data block can't be preserved unmodified in memory for next compression block,
121 you can save it to a more stable memory space, using LZ4_saveDictHC().
122 Return value of LZ4_saveDictHC() is the size of dictionary effectively saved into 'safeBuffer'.
123*/
124
125
126/*-**************************************************************
127 * PRIVATE DEFINITIONS :
128 * Do not use these definitions.
129 * They are exposed to allow static allocation of `LZ4_streamHC_t`.
130 * Using these definitions makes the code vulnerable to potential API break when upgrading LZ4
131 ****************************************************************/
132#define LZ4HC_DICTIONARY_LOGSIZE 16
133#define LZ4HC_MAXD (1<<LZ4HC_DICTIONARY_LOGSIZE)
134#define LZ4HC_MAXD_MASK (LZ4HC_MAXD - 1)
135
136#define LZ4HC_HASH_LOG 15
137#define LZ4HC_HASHTABLESIZE (1 << LZ4HC_HASH_LOG)
138#define LZ4HC_HASH_MASK (LZ4HC_HASHTABLESIZE - 1)
139
140
141#if defined(__cplusplus) || (defined (__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L) /* C99 */)
142#include <stdint.h>
143
144typedef struct
145{
146 uint32_t hashTable[LZ4HC_HASHTABLESIZE];
147 uint16_t chainTable[LZ4HC_MAXD];
148 const uint8_t* end; /* next block here to continue on current prefix */
149 const uint8_t* base; /* All index relative to this position */
150 const uint8_t* dictBase; /* alternate base for extDict */
151 uint8_t* inputBuffer; /* deprecated */
152 uint32_t dictLimit; /* below that point, need extDict */
153 uint32_t lowLimit; /* below that point, no more dict */
154 uint32_t nextToUpdate; /* index from which to continue dictionary update */
155 int compressionLevel;
156} LZ4HC_CCtx_internal;
157
158#else
159
160typedef struct
161{
162 unsigned int hashTable[LZ4HC_HASHTABLESIZE];
163 unsigned short chainTable[LZ4HC_MAXD];
164 const unsigned char* end; /* next block here to continue on current prefix */
165 const unsigned char* base; /* All index relative to this position */
166 const unsigned char* dictBase; /* alternate base for extDict */
167 unsigned char* inputBuffer; /* deprecated */
168 unsigned int dictLimit; /* below that point, need extDict */
169 unsigned int lowLimit; /* below that point, no more dict */
170 unsigned int nextToUpdate; /* index from which to continue dictionary update */
171 int compressionLevel;
172} LZ4HC_CCtx_internal;
173
174#endif
175
176#define LZ4_STREAMHCSIZE (4*LZ4HC_HASHTABLESIZE + 2*LZ4HC_MAXD + 56) /* 262200 */
177#define LZ4_STREAMHCSIZE_SIZET (LZ4_STREAMHCSIZE / sizeof(size_t))
178union LZ4_streamHC_u {
179 size_t table[LZ4_STREAMHCSIZE_SIZET];
180 LZ4HC_CCtx_internal internal_donotuse;
181}; /* previously typedef'd to LZ4_streamHC_t */
182/*
183 LZ4_streamHC_t :
184 This structure allows static allocation of LZ4 HC streaming state.
185 State must be initialized using LZ4_resetStreamHC() before first use.
186
187 Static allocation shall only be used in combination with static linking.
188 When invoking LZ4 from a DLL, use create/free functions instead, which are API and ABI stable.
189*/
190
191
192/*-************************************
193* Deprecated Functions
194**************************************/
195/* see lz4.h LZ4_DISABLE_DEPRECATE_WARNINGS to turn off deprecation warnings */
196
197/* deprecated compression functions */
198LZ4LIB_API LZ4_DEPRECATED("use LZ4_compress_HC() instead") int LZ4_compressHC (const char* source, char* dest, int inputSize);
199LZ4LIB_API LZ4_DEPRECATED("use LZ4_compress_HC() instead") int LZ4_compressHC_limitedOutput (const char* source, char* dest, int inputSize, int maxOutputSize);
200LZ4LIB_API LZ4_DEPRECATED("use LZ4_compress_HC() instead") int LZ4_compressHC2 (const char* source, char* dest, int inputSize, int compressionLevel);
201LZ4LIB_API LZ4_DEPRECATED("use LZ4_compress_HC() instead") int LZ4_compressHC2_limitedOutput (const char* source, char* dest, int inputSize, int maxOutputSize, int compressionLevel);
202LZ4LIB_API LZ4_DEPRECATED("use LZ4_compress_HC_extStateHC() instead") int LZ4_compressHC_withStateHC (void* state, const char* source, char* dest, int inputSize);
203LZ4LIB_API LZ4_DEPRECATED("use LZ4_compress_HC_extStateHC() instead") int LZ4_compressHC_limitedOutput_withStateHC (void* state, const char* source, char* dest, int inputSize, int maxOutputSize);
204LZ4LIB_API LZ4_DEPRECATED("use LZ4_compress_HC_extStateHC() instead") int LZ4_compressHC2_withStateHC (void* state, const char* source, char* dest, int inputSize, int compressionLevel);
205LZ4LIB_API LZ4_DEPRECATED("use LZ4_compress_HC_extStateHC() instead") int LZ4_compressHC2_limitedOutput_withStateHC(void* state, const char* source, char* dest, int inputSize, int maxOutputSize, int compressionLevel);
206LZ4LIB_API LZ4_DEPRECATED("use LZ4_compress_HC_continue() instead") int LZ4_compressHC_continue (LZ4_streamHC_t* LZ4_streamHCPtr, const char* source, char* dest, int inputSize);
207LZ4LIB_API LZ4_DEPRECATED("use LZ4_compress_HC_continue() instead") int LZ4_compressHC_limitedOutput_continue (LZ4_streamHC_t* LZ4_streamHCPtr, const char* source, char* dest, int inputSize, int maxOutputSize);
208
209/* Deprecated Streaming functions using older model; should no longer be used */
210LZ4LIB_API LZ4_DEPRECATED("use LZ4_createStreamHC() instead") void* LZ4_createHC (char* inputBuffer);
211LZ4LIB_API LZ4_DEPRECATED("use LZ4_saveDictHC() instead") char* LZ4_slideInputBufferHC (void* LZ4HC_Data);
212LZ4LIB_API LZ4_DEPRECATED("use LZ4_freeStreamHC() instead") int LZ4_freeHC (void* LZ4HC_Data);
213LZ4LIB_API LZ4_DEPRECATED("use LZ4_compress_HC_continue() instead") int LZ4_compressHC2_continue (void* LZ4HC_Data, const char* source, char* dest, int inputSize, int compressionLevel);
214LZ4LIB_API LZ4_DEPRECATED("use LZ4_compress_HC_continue() instead") int LZ4_compressHC2_limitedOutput_continue (void* LZ4HC_Data, const char* source, char* dest, int inputSize, int maxOutputSize, int compressionLevel);
215LZ4LIB_API LZ4_DEPRECATED("use LZ4_createStreamHC() instead") int LZ4_sizeofStreamStateHC(void);
216LZ4LIB_API LZ4_DEPRECATED("use LZ4_resetStreamHC() instead") int LZ4_resetStreamStateHC(void* state, char* inputBuffer);
217
218
219#if defined (__cplusplus)
220}
221#endif
222
223#endif /* LZ4_HC_H_19834876238432 */
224
225
226/*-**************************************************
227 * !!!!! STATIC LINKING ONLY !!!!!
228 * Following definitions are considered experimental.
229 * They should not be linked from DLL,
230 * as there is no guarantee of API stability yet.
231 * Prototypes will be promoted to "stable" status
232 * after successfull usage in real-life scenarios.
233 ***************************************************/
234#ifdef LZ4_HC_STATIC_LINKING_ONLY /* protection macro */
235#ifndef LZ4_HC_SLO_098092834
236#define LZ4_HC_SLO_098092834
237
238/*! LZ4_compress_HC_destSize() : v1.8.0 (experimental)
239 * Will try to compress as much data from `src` as possible
240 * that can fit into `targetDstSize` budget.
241 * Result is provided in 2 parts :
242 * @return : the number of bytes written into 'dst'
243 * or 0 if compression fails.
244 * `srcSizePtr` : value will be updated to indicate how much bytes were read from `src`
245 */
246int LZ4_compress_HC_destSize(void* LZ4HC_Data,
247 const char* src, char* dst,
248 int* srcSizePtr, int targetDstSize,
249 int compressionLevel);
250
251/*! LZ4_compress_HC_continue_destSize() : v1.8.0 (experimental)
252 * Similar as LZ4_compress_HC_continue(),
253 * but will read a variable nb of bytes from `src`
254 * to fit into `targetDstSize` budget.
255 * Result is provided in 2 parts :
256 * @return : the number of bytes written into 'dst'
257 * or 0 if compression fails.
258 * `srcSizePtr` : value will be updated to indicate how much bytes were read from `src`.
259 */
260int LZ4_compress_HC_continue_destSize(LZ4_streamHC_t* LZ4_streamHCPtr,
261 const char* src, char* dst,
262 int* srcSizePtr, int targetDstSize);
263
264/*! LZ4_setCompressionLevel() : v1.8.0 (experimental)
265 * It's possible to change compression level between 2 invocations of LZ4_compress_HC_continue*()
266 */
267void LZ4_setCompressionLevel(LZ4_streamHC_t* LZ4_streamHCPtr, int compressionLevel);
268
269
270
271#endif /* LZ4_HC_SLO_098092834 */
272#endif /* LZ4_HC_STATIC_LINKING_ONLY */
273