NVBIO
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
lz4.h
Go to the documentation of this file.
1 /*
2  LZ4 - Fast LZ compression algorithm
3  Header File
4  Copyright (C) 2011-2014, 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 : http://code.google.com/p/lz4/
32  - LZ4 public forum : https://groups.google.com/forum/#!forum/lz4c
33 */
34 #pragma once
35 
36 #if defined (__cplusplus)
37 extern "C" {
38 #endif
39 
40 /*
41  * lz4.h provides raw compression format functions, for optimal performance and integration into programs.
42  * If you need to generate data using an inter-operable format (respecting the framing specification),
43  * please use lz4frame.h instead.
44 */
45 
46 /**************************************
47  Version
48 **************************************/
49 #define LZ4_VERSION_MAJOR 1 /* for breaking interface changes */
50 #define LZ4_VERSION_MINOR 5 /* for new (non-breaking) interface capabilities */
51 #define LZ4_VERSION_RELEASE 0 /* for tweaks, bug-fixes, or development */
52 #define LZ4_VERSION_NUMBER (LZ4_VERSION_MAJOR *100*100 + LZ4_VERSION_MINOR *100 + LZ4_VERSION_RELEASE)
53 int LZ4_versionNumber (void);
54 
55 /**************************************
56  Tuning parameter
57 **************************************/
58 /*
59  * LZ4_MEMORY_USAGE :
60  * Memory usage formula : N->2^N Bytes (examples : 10 -> 1KB; 12 -> 4KB ; 16 -> 64KB; 20 -> 1MB; etc.)
61  * Increasing memory usage improves compression ratio
62  * Reduced memory usage can improve speed, due to cache effect
63  * Default value is 14, for 16KB, which nicely fits into Intel x86 L1 cache
64  */
65 #define LZ4_MEMORY_USAGE 14
66 
67 
68 /**************************************
69  Simple Functions
70 **************************************/
71 
72 int LZ4_compress (const char* source, char* dest, int sourceSize);
73 int LZ4_decompress_safe (const char* source, char* dest, int compressedSize, int maxDecompressedSize);
74 
75 /*
76 LZ4_compress() :
77  Compresses 'sourceSize' bytes from 'source' into 'dest'.
78  Destination buffer must be already allocated,
79  and must be sized to handle worst cases situations (input data not compressible)
80  Worst case size evaluation is provided by function LZ4_compressBound()
81  inputSize : Max supported value is LZ4_MAX_INPUT_SIZE
82  return : the number of bytes written in buffer dest
83  or 0 if the compression fails
84 
85 LZ4_decompress_safe() :
86  compressedSize : is obviously the source size
87  maxDecompressedSize : is the size of the destination buffer, which must be already allocated.
88  return : the number of bytes decompressed into the destination buffer (necessarily <= maxDecompressedSize)
89  If the destination buffer is not large enough, decoding will stop and output an error code (<0).
90  If the source stream is detected malformed, the function will stop decoding and return a negative result.
91  This function is protected against buffer overflow exploits,
92  and never writes outside of output buffer, nor reads outside of input buffer.
93  It is also protected against malicious data packets.
94 */
95 
96 
97 /**************************************
98  Advanced Functions
99 **************************************/
100 #define LZ4_MAX_INPUT_SIZE 0x7E000000 /* 2 113 929 216 bytes */
101 #define LZ4_COMPRESSBOUND(isize) ((unsigned int)(isize) > (unsigned int)LZ4_MAX_INPUT_SIZE ? 0 : (isize) + ((isize)/255) + 16)
102 
103 /*
104 LZ4_compressBound() :
105  Provides the maximum size that LZ4 compression may output in a "worst case" scenario (input data not compressible)
106  This function is primarily useful for memory allocation purposes (output buffer size).
107  Macro LZ4_COMPRESSBOUND() is also provided for compilation-time evaluation (stack memory allocation for example).
108 
109  isize : is the input size. Max supported value is LZ4_MAX_INPUT_SIZE
110  return : maximum output size in a "worst case" scenario
111  or 0, if input size is too large ( > LZ4_MAX_INPUT_SIZE)
112 */
113 int LZ4_compressBound(int isize);
114 
115 
116 /*
117 LZ4_compress_limitedOutput() :
118  Compress 'sourceSize' bytes from 'source' into an output buffer 'dest' of maximum size 'maxOutputSize'.
119  If it cannot achieve it, compression will stop, and result of the function will be zero.
120  This saves time and memory on detecting non-compressible (or barely compressible) data.
121  This function never writes outside of provided output buffer.
122 
123  sourceSize : Max supported value is LZ4_MAX_INPUT_VALUE
124  maxOutputSize : is the size of the destination buffer (which must be already allocated)
125  return : the number of bytes written in buffer 'dest'
126  or 0 if compression fails
127 */
128 int LZ4_compress_limitedOutput (const char* source, char* dest, int sourceSize, int maxOutputSize);
129 
130 
131 /*
132 LZ4_compress_withState() :
133  Same compression functions, but using an externally allocated memory space to store compression state.
134  Use LZ4_sizeofState() to know how much memory must be allocated,
135  and then, provide it as 'void* state' to compression functions.
136 */
137 int LZ4_sizeofState(void);
138 int LZ4_compress_withState (void* state, const char* source, char* dest, int inputSize);
139 int LZ4_compress_limitedOutput_withState (void* state, const char* source, char* dest, int inputSize, int maxOutputSize);
140 
141 
142 /*
143 LZ4_decompress_fast() :
144  originalSize : is the original and therefore uncompressed size
145  return : the number of bytes read from the source buffer (in other words, the compressed size)
146  If the source stream is detected malformed, the function will stop decoding and return a negative result.
147  Destination buffer must be already allocated. Its size must be a minimum of 'originalSize' bytes.
148  note : This function fully respect memory boundaries for properly formed compressed data.
149  It is a bit faster than LZ4_decompress_safe().
150  However, it does not provide any protection against intentionally modified data stream (malicious input).
151  Use this function in trusted environment only (data to decode comes from a trusted source).
152 */
153 int LZ4_decompress_fast (const char* source, char* dest, int originalSize);
154 
155 
156 /*
157 LZ4_decompress_safe_partial() :
158  This function decompress a compressed block of size 'compressedSize' at position 'source'
159  into destination buffer 'dest' of size 'maxDecompressedSize'.
160  The function tries to stop decompressing operation as soon as 'targetOutputSize' has been reached,
161  reducing decompression time.
162  return : the number of bytes decoded in the destination buffer (necessarily <= maxDecompressedSize)
163  Note : this number can be < 'targetOutputSize' should the compressed block to decode be smaller.
164  Always control how many bytes were decoded.
165  If the source stream is detected malformed, the function will stop decoding and return a negative result.
166  This function never writes outside of output buffer, and never reads outside of input buffer. It is therefore protected against malicious data packets
167 */
168 int LZ4_decompress_safe_partial (const char* source, char* dest, int compressedSize, int targetOutputSize, int maxDecompressedSize);
169 
170 
171 /***********************************************
172  Streaming Compression Functions
173 ***********************************************/
174 
175 #define LZ4_STREAMSIZE_U64 ((1 << (LZ4_MEMORY_USAGE-3)) + 4)
176 #define LZ4_STREAMSIZE (LZ4_STREAMSIZE_U64 * sizeof(long long))
177 /*
178  * LZ4_stream_t
179  * information structure to track an LZ4 stream.
180  * important : init this structure content before first use !
181  * note : only allocated directly the structure if you are statically linking LZ4
182  * If you are using liblz4 as a DLL, please use below construction methods instead.
183  */
184 typedef struct { long long table[LZ4_STREAMSIZE_U64]; } LZ4_stream_t;
185 
186 /*
187  * LZ4_resetStream
188  * Use this function to init an allocated LZ4_stream_t structure
189  */
190 void LZ4_resetStream (LZ4_stream_t* LZ4_streamPtr);
191 
192 /*
193  * LZ4_createStream will allocate and initialize an LZ4_stream_t structure
194  * LZ4_freeStream releases its memory.
195  * In the context of a DLL (liblz4), please use these methods rather than the static struct.
196  * They are more future proof, in case of a change of LZ4_stream_t size.
197  */
199 int LZ4_freeStream (LZ4_stream_t* LZ4_streamPtr);
200 
201 /*
202  * LZ4_loadDict
203  * Use this function to load a static dictionary into LZ4_stream.
204  * Any previous data will be forgotten, only 'dictionary' will remain in memory.
205  * Loading a size of 0 is allowed.
206  * Return : dictionary size, in bytes (necessarily <= 64 KB)
207  */
208 int LZ4_loadDict (LZ4_stream_t* LZ4_streamPtr, const char* dictionary, int dictSize);
209 
210 /*
211  * LZ4_compress_continue
212  * Compress data block 'source', using blocks compressed before as dictionary to improve compression ratio
213  * Previous data blocks are assumed to still be present at their previous location.
214  */
215 int LZ4_compress_continue (LZ4_stream_t* LZ4_streamPtr, const char* source, char* dest, int inputSize);
216 
217 /*
218  * LZ4_compress_limitedOutput_continue
219  * Same as before, but also specify a maximum target compressed size (maxOutputSize)
220  * If objective cannot be met, compression exits, and returns a zero.
221  */
222 int LZ4_compress_limitedOutput_continue (LZ4_stream_t* LZ4_streamPtr, const char* source, char* dest, int inputSize, int maxOutputSize);
223 
224 /*
225  * LZ4_saveDict
226  * If previously compressed data block is not guaranteed to remain available at its memory location
227  * save it into a safer place (char* safeBuffer)
228  * Note : you don't need to call LZ4_loadDict() afterwards,
229  * dictionary is immediately usable, you can therefore call again LZ4_compress_continue()
230  * Return : dictionary size in bytes, or 0 if error
231  * Note : any dictSize > 64 KB will be interpreted as 64KB.
232  */
233 int LZ4_saveDict (LZ4_stream_t* LZ4_streamPtr, char* safeBuffer, int dictSize);
234 
235 
236 /************************************************
237  Streaming Decompression Functions
238 ************************************************/
239 
240 #define LZ4_STREAMDECODESIZE_U64 4
241 #define LZ4_STREAMDECODESIZE (LZ4_STREAMDECODESIZE_U64 * sizeof(unsigned long long))
242 typedef struct { unsigned long long table[LZ4_STREAMDECODESIZE_U64]; } LZ4_streamDecode_t;
243 /*
244  * LZ4_streamDecode_t
245  * information structure to track an LZ4 stream.
246  * init this structure content using LZ4_setStreamDecode or memset() before first use !
247  *
248  * In the context of a DLL (liblz4) please prefer usage of construction methods below.
249  * They are more future proof, in case of a change of LZ4_streamDecode_t size in the future.
250  * LZ4_createStreamDecode will allocate and initialize an LZ4_streamDecode_t structure
251  * LZ4_freeStreamDecode releases its memory.
252  */
254 int LZ4_freeStreamDecode (LZ4_streamDecode_t* LZ4_stream);
255 
256 /*
257  * LZ4_setStreamDecode
258  * Use this function to instruct where to find the dictionary.
259  * Setting a size of 0 is allowed (same effect as reset).
260  * Return : 1 if OK, 0 if error
261  */
262 int LZ4_setStreamDecode (LZ4_streamDecode_t* LZ4_streamDecode, const char* dictionary, int dictSize);
263 
264 /*
265 *_continue() :
266  These decoding functions allow decompression of multiple blocks in "streaming" mode.
267  Previously decoded blocks *must* remain available at the memory position where they were decoded (up to 64 KB)
268  If this condition is not possible, save the relevant part of decoded data into a safe buffer,
269  and indicate where is its new address using LZ4_setStreamDecode()
270 */
271 int LZ4_decompress_safe_continue (LZ4_streamDecode_t* LZ4_streamDecode, const char* source, char* dest, int compressedSize, int maxDecompressedSize);
272 int LZ4_decompress_fast_continue (LZ4_streamDecode_t* LZ4_streamDecode, const char* source, char* dest, int originalSize);
273 
274 
275 /*
276 Advanced decoding functions :
277 *_usingDict() :
278  These decoding functions work the same as
279  a combination of LZ4_setDictDecode() followed by LZ4_decompress_x_continue()
280  They are stand-alone and don't use nor update an LZ4_streamDecode_t structure.
281 */
282 int LZ4_decompress_safe_usingDict (const char* source, char* dest, int compressedSize, int maxDecompressedSize, const char* dictStart, int dictSize);
283 int LZ4_decompress_fast_usingDict (const char* source, char* dest, int originalSize, const char* dictStart, int dictSize);
284 
285 
286 
287 /**************************************
288  Obsolete Functions
289 **************************************/
290 /*
291 Obsolete decompression functions
292 These function names are deprecated and should no longer be used.
293 They are only provided here for compatibility with older user programs.
294 - LZ4_uncompress is the same as LZ4_decompress_fast
295 - LZ4_uncompress_unknownOutputSize is the same as LZ4_decompress_safe
296 These function prototypes are now disabled; uncomment them if you really need them.
297 It is highly recommended to stop using these functions and migrate to newer ones */
298 /* int LZ4_uncompress (const char* source, char* dest, int outputSize); */
299 /* int LZ4_uncompress_unknownOutputSize (const char* source, char* dest, int isize, int maxOutputSize); */
300 
301 
302 /* Obsolete streaming functions; use new streaming interface whenever possible */
303 void* LZ4_create (const char* inputBuffer);
304 int LZ4_sizeofStreamState(void);
305 int LZ4_resetStreamState(void* state, const char* inputBuffer);
306 char* LZ4_slideInputBuffer (void* state);
307 
308 /* Obsolete streaming decoding functions */
309 int LZ4_decompress_safe_withPrefix64k (const char* source, char* dest, int compressedSize, int maxOutputSize);
310 int LZ4_decompress_fast_withPrefix64k (const char* source, char* dest, int originalSize);
311 
312 
313 #if defined (__cplusplus)
314 }
315 #endif