1*4882a593Smuzhiyun /* LZ4 Kernel Interface
2*4882a593Smuzhiyun *
3*4882a593Smuzhiyun * Copyright (C) 2013, LG Electronics, Kyungsik Lee <kyungsik.lee@lge.com>
4*4882a593Smuzhiyun * Copyright (C) 2016, Sven Schmidt <4sschmid@informatik.uni-hamburg.de>
5*4882a593Smuzhiyun *
6*4882a593Smuzhiyun * This program is free software; you can redistribute it and/or modify
7*4882a593Smuzhiyun * it under the terms of the GNU General Public License version 2 as
8*4882a593Smuzhiyun * published by the Free Software Foundation.
9*4882a593Smuzhiyun *
10*4882a593Smuzhiyun * This file is based on the original header file
11*4882a593Smuzhiyun * for LZ4 - Fast LZ compression algorithm.
12*4882a593Smuzhiyun *
13*4882a593Smuzhiyun * LZ4 - Fast LZ compression algorithm
14*4882a593Smuzhiyun * Copyright (C) 2011-2016, Yann Collet.
15*4882a593Smuzhiyun * BSD 2-Clause License (http://www.opensource.org/licenses/bsd-license.php)
16*4882a593Smuzhiyun * Redistribution and use in source and binary forms, with or without
17*4882a593Smuzhiyun * modification, are permitted provided that the following conditions are
18*4882a593Smuzhiyun * met:
19*4882a593Smuzhiyun * * Redistributions of source code must retain the above copyright
20*4882a593Smuzhiyun * notice, this list of conditions and the following disclaimer.
21*4882a593Smuzhiyun * * Redistributions in binary form must reproduce the above
22*4882a593Smuzhiyun * copyright notice, this list of conditions and the following disclaimer
23*4882a593Smuzhiyun * in the documentation and/or other materials provided with the
24*4882a593Smuzhiyun * distribution.
25*4882a593Smuzhiyun * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
26*4882a593Smuzhiyun * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
27*4882a593Smuzhiyun * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
28*4882a593Smuzhiyun * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
29*4882a593Smuzhiyun * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
30*4882a593Smuzhiyun * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
31*4882a593Smuzhiyun * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
32*4882a593Smuzhiyun * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
33*4882a593Smuzhiyun * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
34*4882a593Smuzhiyun * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
35*4882a593Smuzhiyun * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36*4882a593Smuzhiyun * You can contact the author at :
37*4882a593Smuzhiyun * - LZ4 homepage : http://www.lz4.org
38*4882a593Smuzhiyun * - LZ4 source repository : https://github.com/lz4/lz4
39*4882a593Smuzhiyun */
40*4882a593Smuzhiyun
41*4882a593Smuzhiyun #ifndef __LZ4_H__
42*4882a593Smuzhiyun #define __LZ4_H__
43*4882a593Smuzhiyun
44*4882a593Smuzhiyun #include <linux/types.h>
45*4882a593Smuzhiyun #include <linux/string.h> /* memset, memcpy */
46*4882a593Smuzhiyun
47*4882a593Smuzhiyun /*-************************************************************************
48*4882a593Smuzhiyun * CONSTANTS
49*4882a593Smuzhiyun **************************************************************************/
50*4882a593Smuzhiyun /*
51*4882a593Smuzhiyun * LZ4_MEMORY_USAGE :
52*4882a593Smuzhiyun * Memory usage formula : N->2^N Bytes
53*4882a593Smuzhiyun * (examples : 10 -> 1KB; 12 -> 4KB ; 16 -> 64KB; 20 -> 1MB; etc.)
54*4882a593Smuzhiyun * Increasing memory usage improves compression ratio
55*4882a593Smuzhiyun * Reduced memory usage can improve speed, due to cache effect
56*4882a593Smuzhiyun * Default value is 14, for 16KB, which nicely fits into Intel x86 L1 cache
57*4882a593Smuzhiyun */
58*4882a593Smuzhiyun #define LZ4_MEMORY_USAGE 14
59*4882a593Smuzhiyun
60*4882a593Smuzhiyun #define LZ4_MAX_INPUT_SIZE 0x7E000000 /* 2 113 929 216 bytes */
61*4882a593Smuzhiyun #define LZ4_COMPRESSBOUND(isize) (\
62*4882a593Smuzhiyun (unsigned int)(isize) > (unsigned int)LZ4_MAX_INPUT_SIZE \
63*4882a593Smuzhiyun ? 0 \
64*4882a593Smuzhiyun : (isize) + ((isize)/255) + 16)
65*4882a593Smuzhiyun
66*4882a593Smuzhiyun #define LZ4_ACCELERATION_DEFAULT 1
67*4882a593Smuzhiyun #define LZ4_HASHLOG (LZ4_MEMORY_USAGE-2)
68*4882a593Smuzhiyun #define LZ4_HASHTABLESIZE (1 << LZ4_MEMORY_USAGE)
69*4882a593Smuzhiyun #define LZ4_HASH_SIZE_U32 (1 << LZ4_HASHLOG)
70*4882a593Smuzhiyun
71*4882a593Smuzhiyun #define LZ4HC_MIN_CLEVEL 3
72*4882a593Smuzhiyun #define LZ4HC_DEFAULT_CLEVEL 9
73*4882a593Smuzhiyun #define LZ4HC_MAX_CLEVEL 16
74*4882a593Smuzhiyun
75*4882a593Smuzhiyun #define LZ4HC_DICTIONARY_LOGSIZE 16
76*4882a593Smuzhiyun #define LZ4HC_MAXD (1<<LZ4HC_DICTIONARY_LOGSIZE)
77*4882a593Smuzhiyun #define LZ4HC_MAXD_MASK (LZ4HC_MAXD - 1)
78*4882a593Smuzhiyun #define LZ4HC_HASH_LOG (LZ4HC_DICTIONARY_LOGSIZE - 1)
79*4882a593Smuzhiyun #define LZ4HC_HASHTABLESIZE (1 << LZ4HC_HASH_LOG)
80*4882a593Smuzhiyun #define LZ4HC_HASH_MASK (LZ4HC_HASHTABLESIZE - 1)
81*4882a593Smuzhiyun
82*4882a593Smuzhiyun /*-************************************************************************
83*4882a593Smuzhiyun * STREAMING CONSTANTS AND STRUCTURES
84*4882a593Smuzhiyun **************************************************************************/
85*4882a593Smuzhiyun #define LZ4_STREAMSIZE_U64 ((1 << (LZ4_MEMORY_USAGE - 3)) + 4)
86*4882a593Smuzhiyun #define LZ4_STREAMSIZE (LZ4_STREAMSIZE_U64 * sizeof(unsigned long long))
87*4882a593Smuzhiyun
88*4882a593Smuzhiyun #define LZ4_STREAMHCSIZE 262192
89*4882a593Smuzhiyun #define LZ4_STREAMHCSIZE_SIZET (262192 / sizeof(size_t))
90*4882a593Smuzhiyun
91*4882a593Smuzhiyun #define LZ4_STREAMDECODESIZE_U64 4
92*4882a593Smuzhiyun #define LZ4_STREAMDECODESIZE (LZ4_STREAMDECODESIZE_U64 * \
93*4882a593Smuzhiyun sizeof(unsigned long long))
94*4882a593Smuzhiyun
95*4882a593Smuzhiyun /*
96*4882a593Smuzhiyun * LZ4_stream_t - information structure to track an LZ4 stream.
97*4882a593Smuzhiyun */
98*4882a593Smuzhiyun typedef struct {
99*4882a593Smuzhiyun uint32_t hashTable[LZ4_HASH_SIZE_U32];
100*4882a593Smuzhiyun uint32_t currentOffset;
101*4882a593Smuzhiyun uint32_t initCheck;
102*4882a593Smuzhiyun const uint8_t *dictionary;
103*4882a593Smuzhiyun uint8_t *bufferStart;
104*4882a593Smuzhiyun uint32_t dictSize;
105*4882a593Smuzhiyun } LZ4_stream_t_internal;
106*4882a593Smuzhiyun typedef union {
107*4882a593Smuzhiyun unsigned long long table[LZ4_STREAMSIZE_U64];
108*4882a593Smuzhiyun LZ4_stream_t_internal internal_donotuse;
109*4882a593Smuzhiyun } LZ4_stream_t;
110*4882a593Smuzhiyun
111*4882a593Smuzhiyun /*
112*4882a593Smuzhiyun * LZ4_streamHC_t - information structure to track an LZ4HC stream.
113*4882a593Smuzhiyun */
114*4882a593Smuzhiyun typedef struct {
115*4882a593Smuzhiyun unsigned int hashTable[LZ4HC_HASHTABLESIZE];
116*4882a593Smuzhiyun unsigned short chainTable[LZ4HC_MAXD];
117*4882a593Smuzhiyun /* next block to continue on current prefix */
118*4882a593Smuzhiyun const unsigned char *end;
119*4882a593Smuzhiyun /* All index relative to this position */
120*4882a593Smuzhiyun const unsigned char *base;
121*4882a593Smuzhiyun /* alternate base for extDict */
122*4882a593Smuzhiyun const unsigned char *dictBase;
123*4882a593Smuzhiyun /* below that point, need extDict */
124*4882a593Smuzhiyun unsigned int dictLimit;
125*4882a593Smuzhiyun /* below that point, no more dict */
126*4882a593Smuzhiyun unsigned int lowLimit;
127*4882a593Smuzhiyun /* index from which to continue dict update */
128*4882a593Smuzhiyun unsigned int nextToUpdate;
129*4882a593Smuzhiyun unsigned int compressionLevel;
130*4882a593Smuzhiyun } LZ4HC_CCtx_internal;
131*4882a593Smuzhiyun typedef union {
132*4882a593Smuzhiyun size_t table[LZ4_STREAMHCSIZE_SIZET];
133*4882a593Smuzhiyun LZ4HC_CCtx_internal internal_donotuse;
134*4882a593Smuzhiyun } LZ4_streamHC_t;
135*4882a593Smuzhiyun
136*4882a593Smuzhiyun /*
137*4882a593Smuzhiyun * LZ4_streamDecode_t - information structure to track an
138*4882a593Smuzhiyun * LZ4 stream during decompression.
139*4882a593Smuzhiyun *
140*4882a593Smuzhiyun * init this structure using LZ4_setStreamDecode (or memset()) before first use
141*4882a593Smuzhiyun */
142*4882a593Smuzhiyun typedef struct {
143*4882a593Smuzhiyun const uint8_t *externalDict;
144*4882a593Smuzhiyun size_t extDictSize;
145*4882a593Smuzhiyun const uint8_t *prefixEnd;
146*4882a593Smuzhiyun size_t prefixSize;
147*4882a593Smuzhiyun } LZ4_streamDecode_t_internal;
148*4882a593Smuzhiyun typedef union {
149*4882a593Smuzhiyun unsigned long long table[LZ4_STREAMDECODESIZE_U64];
150*4882a593Smuzhiyun LZ4_streamDecode_t_internal internal_donotuse;
151*4882a593Smuzhiyun } LZ4_streamDecode_t;
152*4882a593Smuzhiyun
153*4882a593Smuzhiyun /*-************************************************************************
154*4882a593Smuzhiyun * SIZE OF STATE
155*4882a593Smuzhiyun **************************************************************************/
156*4882a593Smuzhiyun #define LZ4_MEM_COMPRESS LZ4_STREAMSIZE
157*4882a593Smuzhiyun #define LZ4HC_MEM_COMPRESS LZ4_STREAMHCSIZE
158*4882a593Smuzhiyun
159*4882a593Smuzhiyun /*-************************************************************************
160*4882a593Smuzhiyun * Compression Functions
161*4882a593Smuzhiyun **************************************************************************/
162*4882a593Smuzhiyun
163*4882a593Smuzhiyun /**
164*4882a593Smuzhiyun * LZ4_compressBound() - Max. output size in worst case szenarios
165*4882a593Smuzhiyun * @isize: Size of the input data
166*4882a593Smuzhiyun *
167*4882a593Smuzhiyun * Return: Max. size LZ4 may output in a "worst case" szenario
168*4882a593Smuzhiyun * (data not compressible)
169*4882a593Smuzhiyun */
LZ4_compressBound(size_t isize)170*4882a593Smuzhiyun static inline int LZ4_compressBound(size_t isize)
171*4882a593Smuzhiyun {
172*4882a593Smuzhiyun return LZ4_COMPRESSBOUND(isize);
173*4882a593Smuzhiyun }
174*4882a593Smuzhiyun
175*4882a593Smuzhiyun /**
176*4882a593Smuzhiyun * LZ4_compress_default() - Compress data from source to dest
177*4882a593Smuzhiyun * @source: source address of the original data
178*4882a593Smuzhiyun * @dest: output buffer address of the compressed data
179*4882a593Smuzhiyun * @inputSize: size of the input data. Max supported value is LZ4_MAX_INPUT_SIZE
180*4882a593Smuzhiyun * @maxOutputSize: full or partial size of buffer 'dest'
181*4882a593Smuzhiyun * which must be already allocated
182*4882a593Smuzhiyun * @wrkmem: address of the working memory.
183*4882a593Smuzhiyun * This requires 'workmem' of LZ4_MEM_COMPRESS.
184*4882a593Smuzhiyun *
185*4882a593Smuzhiyun * Compresses 'sourceSize' bytes from buffer 'source'
186*4882a593Smuzhiyun * into already allocated 'dest' buffer of size 'maxOutputSize'.
187*4882a593Smuzhiyun * Compression is guaranteed to succeed if
188*4882a593Smuzhiyun * 'maxOutputSize' >= LZ4_compressBound(inputSize).
189*4882a593Smuzhiyun * It also runs faster, so it's a recommended setting.
190*4882a593Smuzhiyun * If the function cannot compress 'source' into a more limited 'dest' budget,
191*4882a593Smuzhiyun * compression stops *immediately*, and the function result is zero.
192*4882a593Smuzhiyun * As a consequence, 'dest' content is not valid.
193*4882a593Smuzhiyun *
194*4882a593Smuzhiyun * Return: Number of bytes written into buffer 'dest'
195*4882a593Smuzhiyun * (necessarily <= maxOutputSize) or 0 if compression fails
196*4882a593Smuzhiyun */
197*4882a593Smuzhiyun int LZ4_compress_default(const char *source, char *dest, int inputSize,
198*4882a593Smuzhiyun int maxOutputSize, void *wrkmem);
199*4882a593Smuzhiyun
200*4882a593Smuzhiyun /**
201*4882a593Smuzhiyun * LZ4_compress_fast() - As LZ4_compress_default providing an acceleration param
202*4882a593Smuzhiyun * @source: source address of the original data
203*4882a593Smuzhiyun * @dest: output buffer address of the compressed data
204*4882a593Smuzhiyun * @inputSize: size of the input data. Max supported value is LZ4_MAX_INPUT_SIZE
205*4882a593Smuzhiyun * @maxOutputSize: full or partial size of buffer 'dest'
206*4882a593Smuzhiyun * which must be already allocated
207*4882a593Smuzhiyun * @acceleration: acceleration factor
208*4882a593Smuzhiyun * @wrkmem: address of the working memory.
209*4882a593Smuzhiyun * This requires 'workmem' of LZ4_MEM_COMPRESS.
210*4882a593Smuzhiyun *
211*4882a593Smuzhiyun * Same as LZ4_compress_default(), but allows to select an "acceleration"
212*4882a593Smuzhiyun * factor. The larger the acceleration value, the faster the algorithm,
213*4882a593Smuzhiyun * but also the lesser the compression. It's a trade-off. It can be fine tuned,
214*4882a593Smuzhiyun * with each successive value providing roughly +~3% to speed.
215*4882a593Smuzhiyun * An acceleration value of "1" is the same as regular LZ4_compress_default()
216*4882a593Smuzhiyun * Values <= 0 will be replaced by LZ4_ACCELERATION_DEFAULT, which is 1.
217*4882a593Smuzhiyun *
218*4882a593Smuzhiyun * Return: Number of bytes written into buffer 'dest'
219*4882a593Smuzhiyun * (necessarily <= maxOutputSize) or 0 if compression fails
220*4882a593Smuzhiyun */
221*4882a593Smuzhiyun int LZ4_compress_fast(const char *source, char *dest, int inputSize,
222*4882a593Smuzhiyun int maxOutputSize, int acceleration, void *wrkmem);
223*4882a593Smuzhiyun
224*4882a593Smuzhiyun /**
225*4882a593Smuzhiyun * LZ4_compress_destSize() - Compress as much data as possible
226*4882a593Smuzhiyun * from source to dest
227*4882a593Smuzhiyun * @source: source address of the original data
228*4882a593Smuzhiyun * @dest: output buffer address of the compressed data
229*4882a593Smuzhiyun * @sourceSizePtr: will be modified to indicate how many bytes where read
230*4882a593Smuzhiyun * from 'source' to fill 'dest'. New value is necessarily <= old value.
231*4882a593Smuzhiyun * @targetDestSize: Size of buffer 'dest' which must be already allocated
232*4882a593Smuzhiyun * @wrkmem: address of the working memory.
233*4882a593Smuzhiyun * This requires 'workmem' of LZ4_MEM_COMPRESS.
234*4882a593Smuzhiyun *
235*4882a593Smuzhiyun * Reverse the logic, by compressing as much data as possible
236*4882a593Smuzhiyun * from 'source' buffer into already allocated buffer 'dest'
237*4882a593Smuzhiyun * of size 'targetDestSize'.
238*4882a593Smuzhiyun * This function either compresses the entire 'source' content into 'dest'
239*4882a593Smuzhiyun * if it's large enough, or fill 'dest' buffer completely with as much data as
240*4882a593Smuzhiyun * possible from 'source'.
241*4882a593Smuzhiyun *
242*4882a593Smuzhiyun * Return: Number of bytes written into 'dest' (necessarily <= targetDestSize)
243*4882a593Smuzhiyun * or 0 if compression fails
244*4882a593Smuzhiyun */
245*4882a593Smuzhiyun int LZ4_compress_destSize(const char *source, char *dest, int *sourceSizePtr,
246*4882a593Smuzhiyun int targetDestSize, void *wrkmem);
247*4882a593Smuzhiyun
248*4882a593Smuzhiyun /*-************************************************************************
249*4882a593Smuzhiyun * Decompression Functions
250*4882a593Smuzhiyun **************************************************************************/
251*4882a593Smuzhiyun
252*4882a593Smuzhiyun /**
253*4882a593Smuzhiyun * LZ4_decompress_fast() - Decompresses data from 'source' into 'dest'
254*4882a593Smuzhiyun * @source: source address of the compressed data
255*4882a593Smuzhiyun * @dest: output buffer address of the uncompressed data
256*4882a593Smuzhiyun * which must be already allocated with 'originalSize' bytes
257*4882a593Smuzhiyun * @originalSize: is the original and therefore uncompressed size
258*4882a593Smuzhiyun *
259*4882a593Smuzhiyun * Decompresses data from 'source' into 'dest'.
260*4882a593Smuzhiyun * This function fully respect memory boundaries for properly formed
261*4882a593Smuzhiyun * compressed data.
262*4882a593Smuzhiyun * It is a bit faster than LZ4_decompress_safe().
263*4882a593Smuzhiyun * However, it does not provide any protection against intentionally
264*4882a593Smuzhiyun * modified data stream (malicious input).
265*4882a593Smuzhiyun * Use this function in trusted environment only
266*4882a593Smuzhiyun * (data to decode comes from a trusted source).
267*4882a593Smuzhiyun *
268*4882a593Smuzhiyun * Return: number of bytes read from the source buffer
269*4882a593Smuzhiyun * or a negative result if decompression fails.
270*4882a593Smuzhiyun */
271*4882a593Smuzhiyun int LZ4_decompress_fast(const char *source, char *dest, int originalSize);
272*4882a593Smuzhiyun
273*4882a593Smuzhiyun /**
274*4882a593Smuzhiyun * LZ4_decompress_safe() - Decompression protected against buffer overflow
275*4882a593Smuzhiyun * @source: source address of the compressed data
276*4882a593Smuzhiyun * @dest: output buffer address of the uncompressed data
277*4882a593Smuzhiyun * which must be already allocated
278*4882a593Smuzhiyun * @compressedSize: is the precise full size of the compressed block
279*4882a593Smuzhiyun * @maxDecompressedSize: is the size of 'dest' buffer
280*4882a593Smuzhiyun *
281*4882a593Smuzhiyun * Decompresses data from 'source' into 'dest'.
282*4882a593Smuzhiyun * If the source stream is detected malformed, the function will
283*4882a593Smuzhiyun * stop decoding and return a negative result.
284*4882a593Smuzhiyun * This function is protected against buffer overflow exploits,
285*4882a593Smuzhiyun * including malicious data packets. It never writes outside output buffer,
286*4882a593Smuzhiyun * nor reads outside input buffer.
287*4882a593Smuzhiyun *
288*4882a593Smuzhiyun * Return: number of bytes decompressed into destination buffer
289*4882a593Smuzhiyun * (necessarily <= maxDecompressedSize)
290*4882a593Smuzhiyun * or a negative result in case of error
291*4882a593Smuzhiyun */
292*4882a593Smuzhiyun int LZ4_decompress_safe(const char *source, char *dest, int compressedSize,
293*4882a593Smuzhiyun int maxDecompressedSize);
294*4882a593Smuzhiyun
295*4882a593Smuzhiyun /**
296*4882a593Smuzhiyun * LZ4_decompress_safe_partial() - Decompress a block of size 'compressedSize'
297*4882a593Smuzhiyun * at position 'source' into buffer 'dest'
298*4882a593Smuzhiyun * @source: source address of the compressed data
299*4882a593Smuzhiyun * @dest: output buffer address of the decompressed data which must be
300*4882a593Smuzhiyun * already allocated
301*4882a593Smuzhiyun * @compressedSize: is the precise full size of the compressed block.
302*4882a593Smuzhiyun * @targetOutputSize: the decompression operation will try
303*4882a593Smuzhiyun * to stop as soon as 'targetOutputSize' has been reached
304*4882a593Smuzhiyun * @maxDecompressedSize: is the size of destination buffer
305*4882a593Smuzhiyun *
306*4882a593Smuzhiyun * This function decompresses a compressed block of size 'compressedSize'
307*4882a593Smuzhiyun * at position 'source' into destination buffer 'dest'
308*4882a593Smuzhiyun * of size 'maxDecompressedSize'.
309*4882a593Smuzhiyun * The function tries to stop decompressing operation as soon as
310*4882a593Smuzhiyun * 'targetOutputSize' has been reached, reducing decompression time.
311*4882a593Smuzhiyun * This function never writes outside of output buffer,
312*4882a593Smuzhiyun * and never reads outside of input buffer.
313*4882a593Smuzhiyun * It is therefore protected against malicious data packets.
314*4882a593Smuzhiyun *
315*4882a593Smuzhiyun * Return: the number of bytes decoded in the destination buffer
316*4882a593Smuzhiyun * (necessarily <= maxDecompressedSize)
317*4882a593Smuzhiyun * or a negative result in case of error
318*4882a593Smuzhiyun *
319*4882a593Smuzhiyun */
320*4882a593Smuzhiyun int LZ4_decompress_safe_partial(const char *source, char *dest,
321*4882a593Smuzhiyun int compressedSize, int targetOutputSize, int maxDecompressedSize);
322*4882a593Smuzhiyun
323*4882a593Smuzhiyun /*-************************************************************************
324*4882a593Smuzhiyun * LZ4 HC Compression
325*4882a593Smuzhiyun **************************************************************************/
326*4882a593Smuzhiyun
327*4882a593Smuzhiyun /**
328*4882a593Smuzhiyun * LZ4_compress_HC() - Compress data from `src` into `dst`, using HC algorithm
329*4882a593Smuzhiyun * @src: source address of the original data
330*4882a593Smuzhiyun * @dst: output buffer address of the compressed data
331*4882a593Smuzhiyun * @srcSize: size of the input data. Max supported value is LZ4_MAX_INPUT_SIZE
332*4882a593Smuzhiyun * @dstCapacity: full or partial size of buffer 'dst',
333*4882a593Smuzhiyun * which must be already allocated
334*4882a593Smuzhiyun * @compressionLevel: Recommended values are between 4 and 9, although any
335*4882a593Smuzhiyun * value between 1 and LZ4HC_MAX_CLEVEL will work.
336*4882a593Smuzhiyun * Values >LZ4HC_MAX_CLEVEL behave the same as 16.
337*4882a593Smuzhiyun * @wrkmem: address of the working memory.
338*4882a593Smuzhiyun * This requires 'wrkmem' of size LZ4HC_MEM_COMPRESS.
339*4882a593Smuzhiyun *
340*4882a593Smuzhiyun * Compress data from 'src' into 'dst', using the more powerful
341*4882a593Smuzhiyun * but slower "HC" algorithm. Compression is guaranteed to succeed if
342*4882a593Smuzhiyun * `dstCapacity >= LZ4_compressBound(srcSize)
343*4882a593Smuzhiyun *
344*4882a593Smuzhiyun * Return : the number of bytes written into 'dst' or 0 if compression fails.
345*4882a593Smuzhiyun */
346*4882a593Smuzhiyun int LZ4_compress_HC(const char *src, char *dst, int srcSize, int dstCapacity,
347*4882a593Smuzhiyun int compressionLevel, void *wrkmem);
348*4882a593Smuzhiyun
349*4882a593Smuzhiyun /**
350*4882a593Smuzhiyun * LZ4_resetStreamHC() - Init an allocated 'LZ4_streamHC_t' structure
351*4882a593Smuzhiyun * @streamHCPtr: pointer to the 'LZ4_streamHC_t' structure
352*4882a593Smuzhiyun * @compressionLevel: Recommended values are between 4 and 9, although any
353*4882a593Smuzhiyun * value between 1 and LZ4HC_MAX_CLEVEL will work.
354*4882a593Smuzhiyun * Values >LZ4HC_MAX_CLEVEL behave the same as 16.
355*4882a593Smuzhiyun *
356*4882a593Smuzhiyun * An LZ4_streamHC_t structure can be allocated once
357*4882a593Smuzhiyun * and re-used multiple times.
358*4882a593Smuzhiyun * Use this function to init an allocated `LZ4_streamHC_t` structure
359*4882a593Smuzhiyun * and start a new compression.
360*4882a593Smuzhiyun */
361*4882a593Smuzhiyun void LZ4_resetStreamHC(LZ4_streamHC_t *streamHCPtr, int compressionLevel);
362*4882a593Smuzhiyun
363*4882a593Smuzhiyun /**
364*4882a593Smuzhiyun * LZ4_loadDictHC() - Load a static dictionary into LZ4_streamHC
365*4882a593Smuzhiyun * @streamHCPtr: pointer to the LZ4HC_stream_t
366*4882a593Smuzhiyun * @dictionary: dictionary to load
367*4882a593Smuzhiyun * @dictSize: size of dictionary
368*4882a593Smuzhiyun *
369*4882a593Smuzhiyun * Use this function to load a static dictionary into LZ4HC_stream.
370*4882a593Smuzhiyun * Any previous data will be forgotten, only 'dictionary'
371*4882a593Smuzhiyun * will remain in memory.
372*4882a593Smuzhiyun * Loading a size of 0 is allowed.
373*4882a593Smuzhiyun *
374*4882a593Smuzhiyun * Return : dictionary size, in bytes (necessarily <= 64 KB)
375*4882a593Smuzhiyun */
376*4882a593Smuzhiyun int LZ4_loadDictHC(LZ4_streamHC_t *streamHCPtr, const char *dictionary,
377*4882a593Smuzhiyun int dictSize);
378*4882a593Smuzhiyun
379*4882a593Smuzhiyun /**
380*4882a593Smuzhiyun * LZ4_compress_HC_continue() - Compress 'src' using data from previously
381*4882a593Smuzhiyun * compressed blocks as a dictionary using the HC algorithm
382*4882a593Smuzhiyun * @streamHCPtr: Pointer to the previous 'LZ4_streamHC_t' structure
383*4882a593Smuzhiyun * @src: source address of the original data
384*4882a593Smuzhiyun * @dst: output buffer address of the compressed data,
385*4882a593Smuzhiyun * which must be already allocated
386*4882a593Smuzhiyun * @srcSize: size of the input data. Max supported value is LZ4_MAX_INPUT_SIZE
387*4882a593Smuzhiyun * @maxDstSize: full or partial size of buffer 'dest'
388*4882a593Smuzhiyun * which must be already allocated
389*4882a593Smuzhiyun *
390*4882a593Smuzhiyun * These functions compress data in successive blocks of any size, using
391*4882a593Smuzhiyun * previous blocks as dictionary. One key assumption is that previous
392*4882a593Smuzhiyun * blocks (up to 64 KB) remain read-accessible while
393*4882a593Smuzhiyun * compressing next blocks. There is an exception for ring buffers,
394*4882a593Smuzhiyun * which can be smaller than 64 KB.
395*4882a593Smuzhiyun * Ring buffers scenario is automatically detected and handled by
396*4882a593Smuzhiyun * LZ4_compress_HC_continue().
397*4882a593Smuzhiyun * Before starting compression, state must be properly initialized,
398*4882a593Smuzhiyun * using LZ4_resetStreamHC().
399*4882a593Smuzhiyun * A first "fictional block" can then be designated as
400*4882a593Smuzhiyun * initial dictionary, using LZ4_loadDictHC() (Optional).
401*4882a593Smuzhiyun * Then, use LZ4_compress_HC_continue()
402*4882a593Smuzhiyun * to compress each successive block. Previous memory blocks
403*4882a593Smuzhiyun * (including initial dictionary when present) must remain accessible
404*4882a593Smuzhiyun * and unmodified during compression.
405*4882a593Smuzhiyun * 'dst' buffer should be sized to handle worst case scenarios, using
406*4882a593Smuzhiyun * LZ4_compressBound(), to ensure operation success.
407*4882a593Smuzhiyun * If, for any reason, previous data blocks can't be preserved unmodified
408*4882a593Smuzhiyun * in memory during next compression block,
409*4882a593Smuzhiyun * you must save it to a safer memory space, using LZ4_saveDictHC().
410*4882a593Smuzhiyun * Return value of LZ4_saveDictHC() is the size of dictionary
411*4882a593Smuzhiyun * effectively saved into 'safeBuffer'.
412*4882a593Smuzhiyun *
413*4882a593Smuzhiyun * Return: Number of bytes written into buffer 'dst' or 0 if compression fails
414*4882a593Smuzhiyun */
415*4882a593Smuzhiyun int LZ4_compress_HC_continue(LZ4_streamHC_t *streamHCPtr, const char *src,
416*4882a593Smuzhiyun char *dst, int srcSize, int maxDstSize);
417*4882a593Smuzhiyun
418*4882a593Smuzhiyun /**
419*4882a593Smuzhiyun * LZ4_saveDictHC() - Save static dictionary from LZ4HC_stream
420*4882a593Smuzhiyun * @streamHCPtr: pointer to the 'LZ4HC_stream_t' structure
421*4882a593Smuzhiyun * @safeBuffer: buffer to save dictionary to, must be already allocated
422*4882a593Smuzhiyun * @maxDictSize: size of 'safeBuffer'
423*4882a593Smuzhiyun *
424*4882a593Smuzhiyun * If previously compressed data block is not guaranteed
425*4882a593Smuzhiyun * to remain available at its memory location,
426*4882a593Smuzhiyun * save it into a safer place (char *safeBuffer).
427*4882a593Smuzhiyun * Note : you don't need to call LZ4_loadDictHC() afterwards,
428*4882a593Smuzhiyun * dictionary is immediately usable, you can therefore call
429*4882a593Smuzhiyun * LZ4_compress_HC_continue().
430*4882a593Smuzhiyun *
431*4882a593Smuzhiyun * Return : saved dictionary size in bytes (necessarily <= maxDictSize),
432*4882a593Smuzhiyun * or 0 if error.
433*4882a593Smuzhiyun */
434*4882a593Smuzhiyun int LZ4_saveDictHC(LZ4_streamHC_t *streamHCPtr, char *safeBuffer,
435*4882a593Smuzhiyun int maxDictSize);
436*4882a593Smuzhiyun
437*4882a593Smuzhiyun /*-*********************************************
438*4882a593Smuzhiyun * Streaming Compression Functions
439*4882a593Smuzhiyun ***********************************************/
440*4882a593Smuzhiyun
441*4882a593Smuzhiyun /**
442*4882a593Smuzhiyun * LZ4_resetStream() - Init an allocated 'LZ4_stream_t' structure
443*4882a593Smuzhiyun * @LZ4_stream: pointer to the 'LZ4_stream_t' structure
444*4882a593Smuzhiyun *
445*4882a593Smuzhiyun * An LZ4_stream_t structure can be allocated once
446*4882a593Smuzhiyun * and re-used multiple times.
447*4882a593Smuzhiyun * Use this function to init an allocated `LZ4_stream_t` structure
448*4882a593Smuzhiyun * and start a new compression.
449*4882a593Smuzhiyun */
450*4882a593Smuzhiyun void LZ4_resetStream(LZ4_stream_t *LZ4_stream);
451*4882a593Smuzhiyun
452*4882a593Smuzhiyun /**
453*4882a593Smuzhiyun * LZ4_loadDict() - Load a static dictionary into LZ4_stream
454*4882a593Smuzhiyun * @streamPtr: pointer to the LZ4_stream_t
455*4882a593Smuzhiyun * @dictionary: dictionary to load
456*4882a593Smuzhiyun * @dictSize: size of dictionary
457*4882a593Smuzhiyun *
458*4882a593Smuzhiyun * Use this function to load a static dictionary into LZ4_stream.
459*4882a593Smuzhiyun * Any previous data will be forgotten, only 'dictionary'
460*4882a593Smuzhiyun * will remain in memory.
461*4882a593Smuzhiyun * Loading a size of 0 is allowed.
462*4882a593Smuzhiyun *
463*4882a593Smuzhiyun * Return : dictionary size, in bytes (necessarily <= 64 KB)
464*4882a593Smuzhiyun */
465*4882a593Smuzhiyun int LZ4_loadDict(LZ4_stream_t *streamPtr, const char *dictionary,
466*4882a593Smuzhiyun int dictSize);
467*4882a593Smuzhiyun
468*4882a593Smuzhiyun /**
469*4882a593Smuzhiyun * LZ4_saveDict() - Save static dictionary from LZ4_stream
470*4882a593Smuzhiyun * @streamPtr: pointer to the 'LZ4_stream_t' structure
471*4882a593Smuzhiyun * @safeBuffer: buffer to save dictionary to, must be already allocated
472*4882a593Smuzhiyun * @dictSize: size of 'safeBuffer'
473*4882a593Smuzhiyun *
474*4882a593Smuzhiyun * If previously compressed data block is not guaranteed
475*4882a593Smuzhiyun * to remain available at its memory location,
476*4882a593Smuzhiyun * save it into a safer place (char *safeBuffer).
477*4882a593Smuzhiyun * Note : you don't need to call LZ4_loadDict() afterwards,
478*4882a593Smuzhiyun * dictionary is immediately usable, you can therefore call
479*4882a593Smuzhiyun * LZ4_compress_fast_continue().
480*4882a593Smuzhiyun *
481*4882a593Smuzhiyun * Return : saved dictionary size in bytes (necessarily <= dictSize),
482*4882a593Smuzhiyun * or 0 if error.
483*4882a593Smuzhiyun */
484*4882a593Smuzhiyun int LZ4_saveDict(LZ4_stream_t *streamPtr, char *safeBuffer, int dictSize);
485*4882a593Smuzhiyun
486*4882a593Smuzhiyun /**
487*4882a593Smuzhiyun * LZ4_compress_fast_continue() - Compress 'src' using data from previously
488*4882a593Smuzhiyun * compressed blocks as a dictionary
489*4882a593Smuzhiyun * @streamPtr: Pointer to the previous 'LZ4_stream_t' structure
490*4882a593Smuzhiyun * @src: source address of the original data
491*4882a593Smuzhiyun * @dst: output buffer address of the compressed data,
492*4882a593Smuzhiyun * which must be already allocated
493*4882a593Smuzhiyun * @srcSize: size of the input data. Max supported value is LZ4_MAX_INPUT_SIZE
494*4882a593Smuzhiyun * @maxDstSize: full or partial size of buffer 'dest'
495*4882a593Smuzhiyun * which must be already allocated
496*4882a593Smuzhiyun * @acceleration: acceleration factor
497*4882a593Smuzhiyun *
498*4882a593Smuzhiyun * Compress buffer content 'src', using data from previously compressed blocks
499*4882a593Smuzhiyun * as dictionary to improve compression ratio.
500*4882a593Smuzhiyun * Important : Previous data blocks are assumed to still
501*4882a593Smuzhiyun * be present and unmodified !
502*4882a593Smuzhiyun * If maxDstSize >= LZ4_compressBound(srcSize),
503*4882a593Smuzhiyun * compression is guaranteed to succeed, and runs faster.
504*4882a593Smuzhiyun *
505*4882a593Smuzhiyun * Return: Number of bytes written into buffer 'dst' or 0 if compression fails
506*4882a593Smuzhiyun */
507*4882a593Smuzhiyun int LZ4_compress_fast_continue(LZ4_stream_t *streamPtr, const char *src,
508*4882a593Smuzhiyun char *dst, int srcSize, int maxDstSize, int acceleration);
509*4882a593Smuzhiyun
510*4882a593Smuzhiyun /**
511*4882a593Smuzhiyun * LZ4_setStreamDecode() - Instruct where to find dictionary
512*4882a593Smuzhiyun * @LZ4_streamDecode: the 'LZ4_streamDecode_t' structure
513*4882a593Smuzhiyun * @dictionary: dictionary to use
514*4882a593Smuzhiyun * @dictSize: size of dictionary
515*4882a593Smuzhiyun *
516*4882a593Smuzhiyun * Use this function to instruct where to find the dictionary.
517*4882a593Smuzhiyun * Setting a size of 0 is allowed (same effect as reset).
518*4882a593Smuzhiyun *
519*4882a593Smuzhiyun * Return: 1 if OK, 0 if error
520*4882a593Smuzhiyun */
521*4882a593Smuzhiyun int LZ4_setStreamDecode(LZ4_streamDecode_t *LZ4_streamDecode,
522*4882a593Smuzhiyun const char *dictionary, int dictSize);
523*4882a593Smuzhiyun
524*4882a593Smuzhiyun /**
525*4882a593Smuzhiyun * LZ4_decompress_safe_continue() - Decompress blocks in streaming mode
526*4882a593Smuzhiyun * @LZ4_streamDecode: the 'LZ4_streamDecode_t' structure
527*4882a593Smuzhiyun * @source: source address of the compressed data
528*4882a593Smuzhiyun * @dest: output buffer address of the uncompressed data
529*4882a593Smuzhiyun * which must be already allocated
530*4882a593Smuzhiyun * @compressedSize: is the precise full size of the compressed block
531*4882a593Smuzhiyun * @maxDecompressedSize: is the size of 'dest' buffer
532*4882a593Smuzhiyun *
533*4882a593Smuzhiyun * This decoding function allows decompression of multiple blocks
534*4882a593Smuzhiyun * in "streaming" mode.
535*4882a593Smuzhiyun * Previously decoded blocks *must* remain available at the memory position
536*4882a593Smuzhiyun * where they were decoded (up to 64 KB)
537*4882a593Smuzhiyun * In the case of a ring buffers, decoding buffer must be either :
538*4882a593Smuzhiyun * - Exactly same size as encoding buffer, with same update rule
539*4882a593Smuzhiyun * (block boundaries at same positions) In which case,
540*4882a593Smuzhiyun * the decoding & encoding ring buffer can have any size,
541*4882a593Smuzhiyun * including very small ones ( < 64 KB).
542*4882a593Smuzhiyun * - Larger than encoding buffer, by a minimum of maxBlockSize more bytes.
543*4882a593Smuzhiyun * maxBlockSize is implementation dependent.
544*4882a593Smuzhiyun * It's the maximum size you intend to compress into a single block.
545*4882a593Smuzhiyun * In which case, encoding and decoding buffers do not need
546*4882a593Smuzhiyun * to be synchronized, and encoding ring buffer can have any size,
547*4882a593Smuzhiyun * including small ones ( < 64 KB).
548*4882a593Smuzhiyun * - _At least_ 64 KB + 8 bytes + maxBlockSize.
549*4882a593Smuzhiyun * In which case, encoding and decoding buffers do not need to be
550*4882a593Smuzhiyun * synchronized, and encoding ring buffer can have any size,
551*4882a593Smuzhiyun * including larger than decoding buffer. W
552*4882a593Smuzhiyun * Whenever these conditions are not possible, save the last 64KB of decoded
553*4882a593Smuzhiyun * data into a safe buffer, and indicate where it is saved
554*4882a593Smuzhiyun * using LZ4_setStreamDecode()
555*4882a593Smuzhiyun *
556*4882a593Smuzhiyun * Return: number of bytes decompressed into destination buffer
557*4882a593Smuzhiyun * (necessarily <= maxDecompressedSize)
558*4882a593Smuzhiyun * or a negative result in case of error
559*4882a593Smuzhiyun */
560*4882a593Smuzhiyun int LZ4_decompress_safe_continue(LZ4_streamDecode_t *LZ4_streamDecode,
561*4882a593Smuzhiyun const char *source, char *dest, int compressedSize,
562*4882a593Smuzhiyun int maxDecompressedSize);
563*4882a593Smuzhiyun
564*4882a593Smuzhiyun /**
565*4882a593Smuzhiyun * LZ4_decompress_fast_continue() - Decompress blocks in streaming mode
566*4882a593Smuzhiyun * @LZ4_streamDecode: the 'LZ4_streamDecode_t' structure
567*4882a593Smuzhiyun * @source: source address of the compressed data
568*4882a593Smuzhiyun * @dest: output buffer address of the uncompressed data
569*4882a593Smuzhiyun * which must be already allocated with 'originalSize' bytes
570*4882a593Smuzhiyun * @originalSize: is the original and therefore uncompressed size
571*4882a593Smuzhiyun *
572*4882a593Smuzhiyun * This decoding function allows decompression of multiple blocks
573*4882a593Smuzhiyun * in "streaming" mode.
574*4882a593Smuzhiyun * Previously decoded blocks *must* remain available at the memory position
575*4882a593Smuzhiyun * where they were decoded (up to 64 KB)
576*4882a593Smuzhiyun * In the case of a ring buffers, decoding buffer must be either :
577*4882a593Smuzhiyun * - Exactly same size as encoding buffer, with same update rule
578*4882a593Smuzhiyun * (block boundaries at same positions) In which case,
579*4882a593Smuzhiyun * the decoding & encoding ring buffer can have any size,
580*4882a593Smuzhiyun * including very small ones ( < 64 KB).
581*4882a593Smuzhiyun * - Larger than encoding buffer, by a minimum of maxBlockSize more bytes.
582*4882a593Smuzhiyun * maxBlockSize is implementation dependent.
583*4882a593Smuzhiyun * It's the maximum size you intend to compress into a single block.
584*4882a593Smuzhiyun * In which case, encoding and decoding buffers do not need
585*4882a593Smuzhiyun * to be synchronized, and encoding ring buffer can have any size,
586*4882a593Smuzhiyun * including small ones ( < 64 KB).
587*4882a593Smuzhiyun * - _At least_ 64 KB + 8 bytes + maxBlockSize.
588*4882a593Smuzhiyun * In which case, encoding and decoding buffers do not need to be
589*4882a593Smuzhiyun * synchronized, and encoding ring buffer can have any size,
590*4882a593Smuzhiyun * including larger than decoding buffer. W
591*4882a593Smuzhiyun * Whenever these conditions are not possible, save the last 64KB of decoded
592*4882a593Smuzhiyun * data into a safe buffer, and indicate where it is saved
593*4882a593Smuzhiyun * using LZ4_setStreamDecode()
594*4882a593Smuzhiyun *
595*4882a593Smuzhiyun * Return: number of bytes decompressed into destination buffer
596*4882a593Smuzhiyun * (necessarily <= maxDecompressedSize)
597*4882a593Smuzhiyun * or a negative result in case of error
598*4882a593Smuzhiyun */
599*4882a593Smuzhiyun int LZ4_decompress_fast_continue(LZ4_streamDecode_t *LZ4_streamDecode,
600*4882a593Smuzhiyun const char *source, char *dest, int originalSize);
601*4882a593Smuzhiyun
602*4882a593Smuzhiyun /**
603*4882a593Smuzhiyun * LZ4_decompress_safe_usingDict() - Same as LZ4_setStreamDecode()
604*4882a593Smuzhiyun * followed by LZ4_decompress_safe_continue()
605*4882a593Smuzhiyun * @source: source address of the compressed data
606*4882a593Smuzhiyun * @dest: output buffer address of the uncompressed data
607*4882a593Smuzhiyun * which must be already allocated
608*4882a593Smuzhiyun * @compressedSize: is the precise full size of the compressed block
609*4882a593Smuzhiyun * @maxDecompressedSize: is the size of 'dest' buffer
610*4882a593Smuzhiyun * @dictStart: pointer to the start of the dictionary in memory
611*4882a593Smuzhiyun * @dictSize: size of dictionary
612*4882a593Smuzhiyun *
613*4882a593Smuzhiyun * This decoding function works the same as
614*4882a593Smuzhiyun * a combination of LZ4_setStreamDecode() followed by
615*4882a593Smuzhiyun * LZ4_decompress_safe_continue()
616*4882a593Smuzhiyun * It is stand-alone, and doesn't need an LZ4_streamDecode_t structure.
617*4882a593Smuzhiyun *
618*4882a593Smuzhiyun * Return: number of bytes decompressed into destination buffer
619*4882a593Smuzhiyun * (necessarily <= maxDecompressedSize)
620*4882a593Smuzhiyun * or a negative result in case of error
621*4882a593Smuzhiyun */
622*4882a593Smuzhiyun int LZ4_decompress_safe_usingDict(const char *source, char *dest,
623*4882a593Smuzhiyun int compressedSize, int maxDecompressedSize, const char *dictStart,
624*4882a593Smuzhiyun int dictSize);
625*4882a593Smuzhiyun
626*4882a593Smuzhiyun /**
627*4882a593Smuzhiyun * LZ4_decompress_fast_usingDict() - Same as LZ4_setStreamDecode()
628*4882a593Smuzhiyun * followed by LZ4_decompress_fast_continue()
629*4882a593Smuzhiyun * @source: source address of the compressed data
630*4882a593Smuzhiyun * @dest: output buffer address of the uncompressed data
631*4882a593Smuzhiyun * which must be already allocated with 'originalSize' bytes
632*4882a593Smuzhiyun * @originalSize: is the original and therefore uncompressed size
633*4882a593Smuzhiyun * @dictStart: pointer to the start of the dictionary in memory
634*4882a593Smuzhiyun * @dictSize: size of dictionary
635*4882a593Smuzhiyun *
636*4882a593Smuzhiyun * This decoding function works the same as
637*4882a593Smuzhiyun * a combination of LZ4_setStreamDecode() followed by
638*4882a593Smuzhiyun * LZ4_decompress_fast_continue()
639*4882a593Smuzhiyun * It is stand-alone, and doesn't need an LZ4_streamDecode_t structure.
640*4882a593Smuzhiyun *
641*4882a593Smuzhiyun * Return: number of bytes decompressed into destination buffer
642*4882a593Smuzhiyun * (necessarily <= maxDecompressedSize)
643*4882a593Smuzhiyun * or a negative result in case of error
644*4882a593Smuzhiyun */
645*4882a593Smuzhiyun int LZ4_decompress_fast_usingDict(const char *source, char *dest,
646*4882a593Smuzhiyun int originalSize, const char *dictStart, int dictSize);
647*4882a593Smuzhiyun
648*4882a593Smuzhiyun #endif
649