lz4hc.h
Go to the documentation of this file.
1 /*
2  LZ4 HC - High Compression Mode of LZ4
3  Header File
4  Copyright (C) 2011-2020, 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)
38 extern "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 10
50 #define LZ4HC_CLEVEL_MAX 12
51 
52 
53 /*-************************************
54  * Block Compression
55  **************************************/
66 LZ4LIB_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 
80 LZ4LIB_API int LZ4_compress_HC_extStateHC(void* stateHC, const char* src, char* dst, int srcSize, int maxDstSize, int compressionLevel);
81 
82 
91 LZ4LIB_API int LZ4_compress_HC_destSize(void* stateHC,
92  const char* src, char* dst,
93  int* srcSizePtr, int targetDstSize,
94  int compressionLevel);
95 
96 
97 /*-************************************
98  * Streaming Compression
99  * Bufferless synchronous API
100  **************************************/
101  typedef union LZ4_streamHC_u LZ4_streamHC_t; /* incomplete type (defined later) */
102 
110 LZ4LIB_API int LZ4_freeStreamHC (LZ4_streamHC_t* streamHCPtr);
111 
112 /*
113  These functions compress data in successive blocks of any size,
114  using previous blocks as dictionary, to improve compression ratio.
115  One key assumption is that previous blocks (up to 64 KB) remain read-accessible while compressing next blocks.
116  There is an exception for ring buffers, which can be smaller than 64 KB.
117  Ring-buffer scenario is automatically detected and handled within LZ4_compress_HC_continue().
118 
119  Before starting compression, state must be allocated and properly initialized.
120  LZ4_createStreamHC() does both, though compression level is set to LZ4HC_CLEVEL_DEFAULT.
121 
122  Selecting the compression level can be done with LZ4_resetStreamHC_fast() (starts a new stream)
123  or LZ4_setCompressionLevel() (anytime, between blocks in the same stream) (experimental).
124  LZ4_resetStreamHC_fast() only works on states which have been properly initialized at least once,
125  which is automatically the case when state is created using LZ4_createStreamHC().
126 
127  After reset, a first "fictional block" can be designated as initial dictionary,
128  using LZ4_loadDictHC() (Optional).
129 
130  Invoke LZ4_compress_HC_continue() to compress each successive block.
131  The number of blocks is unlimited.
132  Previous input blocks, including initial dictionary when present,
133  must remain accessible and unmodified during compression.
134 
135  It's allowed to update compression level anytime between blocks,
136  using LZ4_setCompressionLevel() (experimental).
137 
138  'dst' buffer should be sized to handle worst case scenarios
139  (see LZ4_compressBound(), it ensures compression success).
140  In case of failure, the API does not guarantee recovery,
141  so the state _must_ be reset.
142  To ensure compression success
143  whenever `dst` buffer size cannot be made >= LZ4_compressBound(),
144  consider using LZ4_compress_HC_continue_destSize().
145 
146  Whenever previous input blocks can't be preserved unmodified in-place during compression of next blocks,
147  it's possible to copy the last blocks into a more stable memory space, using LZ4_saveDictHC().
148  Return value of LZ4_saveDictHC() is the size of dictionary effectively saved into 'safeBuffer' (<= 64 KB)
149 
150  After completing a streaming compression,
151  it's possible to start a new stream of blocks, using the same LZ4_streamHC_t state,
152  just by resetting it, using LZ4_resetStreamHC_fast().
153 */
154 
155 LZ4LIB_API void LZ4_resetStreamHC_fast(LZ4_streamHC_t* streamHCPtr, int compressionLevel); /* v1.9.0+ */
156 LZ4LIB_API int LZ4_loadDictHC (LZ4_streamHC_t* streamHCPtr, const char* dictionary, int dictSize);
157 
159  const char* src, char* dst,
160  int srcSize, int maxDstSize);
161 
173  const char* src, char* dst,
174  int* srcSizePtr, int targetDstSize);
175 
176 LZ4LIB_API int LZ4_saveDictHC (LZ4_streamHC_t* streamHCPtr, char* safeBuffer, int maxDictSize);
177 
178 
179 
180 /*^**********************************************
181  * !!!!!! STATIC LINKING ONLY !!!!!!
182  ***********************************************/
183 
184 /*-******************************************************************
185  * PRIVATE DEFINITIONS :
186  * Do not use these definitions directly.
187  * They are merely exposed to allow static allocation of `LZ4_streamHC_t`.
188  * Declare an `LZ4_streamHC_t` directly, rather than any type below.
189  * Even then, only do so in the context of static linking, as definitions may change between versions.
190  ********************************************************************/
191 
192 #define LZ4HC_DICTIONARY_LOGSIZE 16
193 #define LZ4HC_MAXD (1<<LZ4HC_DICTIONARY_LOGSIZE)
194 #define LZ4HC_MAXD_MASK (LZ4HC_MAXD - 1)
195 
196 #define LZ4HC_HASH_LOG 15
197 #define LZ4HC_HASHTABLESIZE (1 << LZ4HC_HASH_LOG)
198 #define LZ4HC_HASH_MASK (LZ4HC_HASHTABLESIZE - 1)
199 
200 
201 /* Never ever use these definitions directly !
202  * Declare or allocate an LZ4_streamHC_t instead.
203 **/
206 {
209  const LZ4_byte* end; /* next block here to continue on current prefix */
210  const LZ4_byte* prefixStart; /* Indexes relative to this position */
211  const LZ4_byte* dictStart; /* alternate reference for extDict */
212  LZ4_u32 dictLimit; /* below that point, need extDict */
213  LZ4_u32 lowLimit; /* below that point, no more dict */
214  LZ4_u32 nextToUpdate; /* index from which to continue dictionary update */
216  LZ4_i8 favorDecSpeed; /* favor decompression speed if this flag set,
217  otherwise, favor compression ratio */
218  LZ4_i8 dirty; /* stream has to be fully reset if this flag is set */
220 };
221 
222 #define LZ4_STREAMHC_MINSIZE 262200 /* static size, for inter-version compatibility */
226 }; /* previously typedef'd to LZ4_streamHC_t */
227 
228 /* LZ4_streamHC_t :
229  * This structure allows static allocation of LZ4 HC streaming state.
230  * This can be used to allocate statically on stack, or as part of a larger structure.
231  *
232  * Such state **must** be initialized using LZ4_initStreamHC() before first use.
233  *
234  * Note that invoking LZ4_initStreamHC() is not required when
235  * the state was created using LZ4_createStreamHC() (which is recommended).
236  * Using the normal builder, a newly created state is automatically initialized.
237  *
238  * Static allocation shall only be used in combination with static linking.
239  */
240 
241 /* LZ4_initStreamHC() : v1.9.0+
242  * Required before first use of a statically allocated LZ4_streamHC_t.
243  * Before v1.9.0 : use LZ4_resetStreamHC() instead
244  */
245 LZ4LIB_API LZ4_streamHC_t* LZ4_initStreamHC(void* buffer, size_t size);
246 
247 
248 /*-************************************
249 * Deprecated Functions
250 **************************************/
251 /* see lz4.h LZ4_DISABLE_DEPRECATE_WARNINGS to turn off deprecation warnings */
252 
253 /* deprecated compression functions */
254 LZ4_DEPRECATED("use LZ4_compress_HC() instead") LZ4LIB_API int LZ4_compressHC (const char* source, char* dest, int inputSize);
256 LZ4_DEPRECATED("use LZ4_compress_HC() instead") LZ4LIB_API int LZ4_compressHC2 (const char* source, char* dest, int inputSize, int compressionLevel);
262 LZ4_DEPRECATED("use LZ4_compress_HC_continue() instead") LZ4LIB_API int LZ4_compressHC_continue (LZ4_streamHC_t* LZ4_streamHCPtr, const char* source, char* dest, int inputSize);
264 
265 /* Obsolete streaming functions; degraded functionality; do not use!
266  *
267  * In order to perform streaming compression, these functions depended on data
268  * that is no longer tracked in the state. They have been preserved as well as
269  * possible: using them will still produce a correct output. However, use of
270  * LZ4_slideInputBufferHC() will truncate the history of the stream, rather
271  * than preserve a window-sized chunk of history.
272  */
273 #if !defined(LZ4_STATIC_LINKING_ONLY_DISABLE_MEMORY_ALLOCATION)
274 LZ4_DEPRECATED("use LZ4_createStreamHC() instead") LZ4LIB_API void* LZ4_createHC (const char* inputBuffer);
275 LZ4_DEPRECATED("use LZ4_freeStreamHC() instead") LZ4LIB_API int LZ4_freeHC (void* LZ4HC_Data);
276 #endif
277 LZ4_DEPRECATED("use LZ4_saveDictHC() instead") LZ4LIB_API char* LZ4_slideInputBufferHC (void* LZ4HC_Data);
278 LZ4_DEPRECATED("use LZ4_compress_HC_continue() instead") LZ4LIB_API int LZ4_compressHC2_continue (void* LZ4HC_Data, const char* source, char* dest, int inputSize, int compressionLevel);
279 LZ4_DEPRECATED("use LZ4_compress_HC_continue() instead") LZ4LIB_API int LZ4_compressHC2_limitedOutput_continue (void* LZ4HC_Data, const char* source, char* dest, int inputSize, int maxOutputSize, int compressionLevel);
282 
283 
284 /* LZ4_resetStreamHC() is now replaced by LZ4_initStreamHC().
285  * The intention is to emphasize the difference with LZ4_resetStreamHC_fast(),
286  * which is now the recommended function to start a new stream of blocks,
287  * but cannot be used to initialize a memory segment containing arbitrary garbage data.
288  *
289  * It is recommended to switch to LZ4_initStreamHC().
290  * LZ4_resetStreamHC() will generate deprecation warnings in a future version.
291  */
293 
294 
295 #if defined (__cplusplus)
296 }
297 #endif
298 
299 #endif /* LZ4_HC_H_19834876238432 */
300 
301 
302 /*-**************************************************
303  * !!!!! STATIC LINKING ONLY !!!!!
304  * Following definitions are considered experimental.
305  * They should not be linked from DLL,
306  * as there is no guarantee of API stability yet.
307  * Prototypes will be promoted to "stable" status
308  * after successful usage in real-life scenarios.
309  ***************************************************/
310 #ifdef LZ4_HC_STATIC_LINKING_ONLY /* protection macro */
311 #ifndef LZ4_HC_SLO_098092834
312 #define LZ4_HC_SLO_098092834
313 
314 #define LZ4_STATIC_LINKING_ONLY /* LZ4LIB_STATIC_API */
315 #include "lz4.h"
316 
317 #if defined (__cplusplus)
318 extern "C" {
319 #endif
320 
326 LZ4LIB_STATIC_API void LZ4_setCompressionLevel(
327  LZ4_streamHC_t* LZ4_streamHCPtr, int compressionLevel);
328 
333 LZ4LIB_STATIC_API void LZ4_favorDecompressionSpeed(
334  LZ4_streamHC_t* LZ4_streamHCPtr, int favor);
335 
359 LZ4LIB_STATIC_API void LZ4_resetStreamHC_fast(
360  LZ4_streamHC_t* LZ4_streamHCPtr, int compressionLevel);
361 
373 LZ4LIB_STATIC_API int LZ4_compress_HC_extStateHC_fastReset (
374  void* state,
375  const char* src, char* dst,
376  int srcSize, int dstCapacity,
377  int compressionLevel);
378 
404 LZ4LIB_STATIC_API void LZ4_attach_HC_dictionary(
405  LZ4_streamHC_t *working_stream,
406  const LZ4_streamHC_t *dictionary_stream);
407 
408 #if defined (__cplusplus)
409 }
410 #endif
411 
412 #endif /* LZ4_HC_SLO_098092834 */
413 #endif /* LZ4_HC_STATIC_LINKING_ONLY */
LZ4_compressHC
int LZ4_compressHC(const char *src, char *dst, int srcSize)
Definition: lz4hc.c:1203
LZ4_sizeofStreamStateHC
int LZ4_sizeofStreamStateHC(void)
Definition: lz4hc.c:1216
LZ4_streamHC_u
Definition: lz4hc.h:223
LZ4_sizeofStateHC
LZ4LIB_API int LZ4_sizeofStateHC(void)
Definition: lz4hc.c:925
srcSize
char int srcSize
Definition: lz4.h:765
LZ4_compressHC2_limitedOutput
int LZ4_compressHC2_limitedOutput(const char *src, char *dst, int srcSize, int maxDstSize, int cLevel)
Definition: lz4hc.c:1206
LZ4_compressHC_limitedOutput_continue
int LZ4_compressHC_limitedOutput_continue(LZ4_streamHC_t *ctx, const char *src, char *dst, int srcSize, int maxDstSize)
Definition: lz4hc.c:1212
LZ4HC_CCtx_internal::end
const LZ4_byte * end
Definition: lz4hc.h:209
LZ4_i8
signed char LZ4_i8
Definition: lz4.h:665
LZ4_resetStreamHC_fast
LZ4LIB_API void LZ4_resetStreamHC_fast(LZ4_streamHC_t *streamHCPtr, int compressionLevel)
Definition: lz4hc.c:1033
detail::state
state
Definition: core.h:2305
LZ4_compressHC2_withStateHC
int LZ4_compressHC2_withStateHC(void *state, const char *src, char *dst, int srcSize, int cLevel)
Definition: lz4hc.c:1209
LZ4_slideInputBufferHC
char * LZ4_slideInputBufferHC(void *LZ4HC_Data)
Definition: lz4hc.c:1255
LZ4HC_CCtx_internal::hashTable
LZ4_u32 hashTable[LZ4HC_HASHTABLESIZE]
Definition: lz4hc.h:207
LZ4_createStreamHC
LZ4LIB_API LZ4_streamHC_t * LZ4_createStreamHC(void)
Definition: lz4hc.c:992
LZ4HC_CCtx_internal::dictLimit
LZ4_u32 dictLimit
Definition: lz4hc.h:212
LZ4_STREAMHC_MINSIZE
#define LZ4_STREAMHC_MINSIZE
Definition: lz4hc.h:222
LZ4HC_CCtx_internal::favorDecSpeed
LZ4_i8 favorDecSpeed
Definition: lz4hc.h:216
LZ4_compressHC_withStateHC
int LZ4_compressHC_withStateHC(void *state, const char *src, char *dst, int srcSize)
Definition: lz4hc.c:1207
LZ4_compressHC2_limitedOutput_withStateHC
int LZ4_compressHC2_limitedOutput_withStateHC(void *state, const char *src, char *dst, int srcSize, int maxDstSize, int cLevel)
Definition: lz4hc.c:1210
LZ4HC_HASHTABLESIZE
#define LZ4HC_HASHTABLESIZE
Definition: lz4hc.h:197
LZ4_byte
unsigned char LZ4_byte
Definition: lz4.h:666
maxOutputSize
char int int maxOutputSize
Definition: lz4hc.h:255
LZ4_attach_HC_dictionary
void LZ4_attach_HC_dictionary(LZ4_streamHC_t *working_stream, const LZ4_streamHC_t *dictionary_stream)
Definition: lz4hc.c:1087
LZ4_resetStreamStateHC
int LZ4_resetStreamStateHC(void *state, char *inputBuffer)
Definition: lz4hc.c:1220
LZ4_freeStreamHC
LZ4LIB_API int LZ4_freeStreamHC(LZ4_streamHC_t *streamHCPtr)
Definition: lz4hc.c:1001
LZ4_compressHC2_continue
int LZ4_compressHC2_continue(void *LZ4HC_Data, const char *src, char *dst, int srcSize, int cLevel)
Definition: lz4hc.c:1245
lz4.h
LZ4HC_CCtx_internal::nextToUpdate
LZ4_u32 nextToUpdate
Definition: lz4hc.h:214
LZ4HC_CCtx_internal::dirty
LZ4_i8 dirty
Definition: lz4hc.h:218
LZ4HC_MAXD
#define LZ4HC_MAXD
Definition: lz4hc.h:193
LZ4_streamHC_u::minStateSize
char minStateSize[LZ4_STREAMHC_MINSIZE]
Definition: lz4hc.h:224
source
const char * source
Definition: lz4hc.h:258
LZ4HC_CCtx_internal::compressionLevel
short compressionLevel
Definition: lz4hc.h:215
LZ4_compress_HC_destSize
LZ4LIB_API int LZ4_compress_HC_destSize(void *stateHC, const char *src, char *dst, int *srcSizePtr, int targetDstSize, int compressionLevel)
Definition: lz4hc.c:976
LZ4_compressHC_limitedOutput
int LZ4_compressHC_limitedOutput(const char *src, char *dst, int srcSize, int maxDstSize)
Definition: lz4hc.c:1204
LZ4_compressHC_limitedOutput_withStateHC
int LZ4_compressHC_limitedOutput_withStateHC(void *state, const char *src, char *dst, int srcSize, int maxDstSize)
Definition: lz4hc.c:1208
nonstd::span_lite::size
span_constexpr std::size_t size(span< T, Extent > const &spn)
Definition: span.hpp:1554
LZ4HC_CCtx_internal
Definition: lz4hc.h:205
LZ4_saveDictHC
LZ4LIB_API int LZ4_saveDictHC(LZ4_streamHC_t *streamHCPtr, char *safeBuffer, int maxDictSize)
Definition: lz4hc.c:1171
LZ4_compressHC_continue
int LZ4_compressHC_continue(LZ4_streamHC_t *ctx, const char *src, char *dst, int srcSize)
Definition: lz4hc.c:1211
dest
char * dest
Definition: lz4hc.h:254
LZ4HC_CCtx_internal::dictStart
const LZ4_byte * dictStart
Definition: lz4hc.h:211
LZ4HC_CCtx_internal::dictCtx
const LZ4HC_CCtx_internal * dictCtx
Definition: lz4hc.h:219
LZ4_compressHC2
int LZ4_compressHC2(const char *src, char *dst, int srcSize, int cLevel)
Definition: lz4hc.c:1205
LZ4_u16
unsigned short LZ4_u16
Definition: lz4.h:667
maxDstSize
char int int maxDstSize
Definition: lz4.h:792
compressionLevel
char int int compressionLevel
Definition: lz4hc.h:256
LZ4_resetStreamHC
LZ4LIB_API void LZ4_resetStreamHC(LZ4_streamHC_t *streamHCPtr, int compressionLevel)
Definition: lz4hc.c:1027
LZ4_compress_HC
LZ4LIB_API int LZ4_compress_HC(const char *src, char *dst, int srcSize, int dstCapacity, int compressionLevel)
Definition: lz4hc.c:958
LZ4_initStreamHC
LZ4LIB_API LZ4_streamHC_t * LZ4_initStreamHC(void *buffer, size_t size)
Definition: lz4hc.c:1011
inputBuffer
char * inputBuffer
Definition: lz4hc.h:281
LZ4_setCompressionLevel
void LZ4_setCompressionLevel(LZ4_streamHC_t *LZ4_streamHCPtr, int compressionLevel)
Definition: lz4hc.c:1051
LZ4HC_CCtx_internal::chainTable
LZ4_u16 chainTable[LZ4HC_MAXD]
Definition: lz4hc.h:208
LZ4_favorDecompressionSpeed
void LZ4_favorDecompressionSpeed(LZ4_streamHC_t *LZ4_streamHCPtr, int favor)
Definition: lz4hc.c:1059
LZ4HC_CCtx_internal::lowLimit
LZ4_u32 lowLimit
Definition: lz4hc.h:213
LZ4_loadDictHC
LZ4LIB_API int LZ4_loadDictHC(LZ4_streamHC_t *streamHCPtr, const char *dictionary, int dictSize)
Definition: lz4hc.c:1066
LZ4_freeHC
int LZ4_freeHC(void *LZ4HC_Data)
Definition: lz4hc.c:1237
LZ4_compress_HC_extStateHC_fastReset
int LZ4_compress_HC_extStateHC_fastReset(void *state, const char *src, char *dst, int srcSize, int dstCapacity, int compressionLevel)
Definition: lz4hc.c:939
LZ4_compressHC2_limitedOutput_continue
int LZ4_compressHC2_limitedOutput_continue(void *LZ4HC_Data, const char *src, char *dst, int srcSize, int dstCapacity, int cLevel)
Definition: lz4hc.c:1250
LZ4_compress_HC_extStateHC
LZ4LIB_API int LZ4_compress_HC_extStateHC(void *stateHC, const char *src, char *dst, int srcSize, int maxDstSize, int compressionLevel)
Definition: lz4hc.c:951
LZ4_compress_HC_continue_destSize
LZ4LIB_API int LZ4_compress_HC_continue_destSize(LZ4_streamHC_t *LZ4_streamHCPtr, const char *src, char *dst, int *srcSizePtr, int targetDstSize)
Definition: lz4hc.c:1159
LZ4HC_CCtx_internal::prefixStart
const LZ4_byte * prefixStart
Definition: lz4hc.h:210
LZ4_DEPRECATED
LZ4_DEPRECATED("use LZ4_compress_HC() instead") LZ4LIB_API int LZ4_compressHC(const char *source
LZ4_streamHC_u::internal_donotuse
LZ4HC_CCtx_internal internal_donotuse
Definition: lz4hc.h:225
LZ4LIB_API
#define LZ4LIB_API
Definition: lz4.h:97
dst
char * dst
Definition: lz4.h:792
LZ4_createHC
void * LZ4_createHC(const char *inputBuffer)
Definition: lz4hc.c:1229
inputSize
char int inputSize
Definition: lz4hc.h:254
LZ4_compress_HC_continue
LZ4LIB_API int LZ4_compress_HC_continue(LZ4_streamHC_t *streamHCPtr, const char *src, char *dst, int srcSize, int maxDstSize)
Definition: lz4hc.c:1151
LZ4_u32
unsigned int LZ4_u32
Definition: lz4.h:668


plotjuggler
Author(s): Davide Faconti
autogenerated on Sun Aug 11 2024 02:24:23