nlib
lz4.h
1 /*
2  LZ4 - Fast LZ compression algorithm
3  Header File
4  Copyright (C) 2011-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 
12  * Redistributions of source code must retain the above copyright
13  notice, this list of conditions and the following disclaimer.
14  * Redistributions in binary form must reproduce the above
15  copyright notice, this list of conditions and the following disclaimer
16  in the documentation and/or other materials provided with the
17  distribution.
18 
19  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22  A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23  OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24  SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25  LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26  DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27  THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29  OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 
31  You can contact the author at :
32  - LZ4 source repository : https://github.com/Cyan4973/lz4
33  - LZ4 public forum : https://groups.google.com/forum/#!forum/lz4c
34 */
35 #pragma once
36 
37 #ifndef LZ4_API
38 #ifdef NLIB_WINDLL
39 # ifdef nx_oss_lz4_EXPORTS
40 # define LZ4_API __declspec(dllexport)
41 # else
42 # define LZ4_API __declspec(dllimport)
43 # endif
44 #elif defined(__linux__) || defined(__FreeBSD__) || defined(__APPLE__)
45 # define LZ4_API __attribute__((visibility("default")))
46 #else
47 # define LZ4_API
48 #endif
49 #endif
50 
51 #if defined (__cplusplus)
52 extern "C" {
53 #endif
54 
55 /*
56  * lz4.h provides block compression functions, and gives full buffer control to programmer.
57  * If you need to generate inter-operable compressed data (respecting LZ4 frame specification),
58  * and can let the library handle its own memory, please use lz4frame.h instead.
59 */
60 
61 /**************************************
62 * Version
63 **************************************/
64 #define LZ4_VERSION_MAJOR 1 /* for breaking interface changes */
65 #define LZ4_VERSION_MINOR 7 /* for new (non-breaking) interface capabilities */
66 #define LZ4_VERSION_RELEASE 0 /* for tweaks, bug-fixes, or development */
67 #define LZ4_VERSION_NUMBER (LZ4_VERSION_MAJOR *100*100 + LZ4_VERSION_MINOR *100 + LZ4_VERSION_RELEASE)
68 LZ4_API int LZ4_versionNumber (void);
69 
70 /**************************************
71 * Tuning parameter
72 **************************************/
73 /*
74  * LZ4_MEMORY_USAGE :
75  * Memory usage formula : N->2^N Bytes (examples : 10 -> 1KB; 12 -> 4KB ; 16 -> 64KB; 20 -> 1MB; etc.)
76  * Increasing memory usage improves compression ratio
77  * Reduced memory usage can improve speed, due to cache effect
78  * Default value is 14, for 16KB, which nicely fits into Intel x86 L1 cache
79  */
80 #define LZ4_MEMORY_USAGE 14
81 
82 
83 /**************************************
84 * Simple Functions
85 **************************************/
86 
87 LZ4_API int LZ4_compress_default(const char* source, char* dest, int sourceSize, int maxDestSize);
88 LZ4_API int LZ4_decompress_safe(const char* source, char* dest, int compressedSize, int maxDecompressedSize);
89 
90 /*
91 LZ4_compress_default() :
92  Compresses 'sourceSize' bytes from buffer 'source'
93  into already allocated 'dest' buffer of size 'maxDestSize'.
94  Compression is guaranteed to succeed if 'maxDestSize' >= LZ4_compressBound(sourceSize).
95  It also runs faster, so it's a recommended setting.
96  If the function cannot compress 'source' into a more limited 'dest' budget,
97  compression stops *immediately*, and the function result is zero.
98  As a consequence, 'dest' content is not valid.
99  This function never writes outside 'dest' buffer, nor read outside 'source' buffer.
100  sourceSize : Max supported value is LZ4_MAX_INPUT_VALUE
101  maxDestSize : full or partial size of buffer 'dest' (which must be already allocated)
102  return : the number of bytes written into buffer 'dest' (necessarily <= maxOutputSize)
103  or 0 if compression fails
104 
105 LZ4_decompress_safe() :
106  compressedSize : is the precise full size of the compressed block.
107  maxDecompressedSize : is the size of destination buffer, which must be already allocated.
108  return : the number of bytes decompressed into destination buffer (necessarily <= maxDecompressedSize)
109  If destination buffer is not large enough, decoding will stop and output an error code (<0).
110  If the source stream is detected malformed, the function will stop decoding and return a negative result.
111  This function is protected against buffer overflow exploits, including malicious data packets.
112  It never writes outside output buffer, nor reads outside input buffer.
113 */
114 
115 
116 /**************************************
117 * Advanced Functions
118 **************************************/
119 #define LZ4_MAX_INPUT_SIZE 0x7E000000 /* 2 113 929 216 bytes */
120 #define LZ4_COMPRESSBOUND(isize) ((unsigned)(isize) > (unsigned)LZ4_MAX_INPUT_SIZE ? 0 : (isize) + ((isize)/255) + 16)
121 
122 /*
123 LZ4_compressBound() :
124  Provides the maximum size that LZ4 compression may output in a "worst case" scenario (input data not compressible)
125  This function is primarily useful for memory allocation purposes (destination buffer size).
126  Macro LZ4_COMPRESSBOUND() is also provided for compilation-time evaluation (stack memory allocation for example).
127  Note that LZ4_compress_default() compress faster when dest buffer size is >= LZ4_compressBound(srcSize)
128  inputSize : max supported value is LZ4_MAX_INPUT_SIZE
129  return : maximum output size in a "worst case" scenario
130  or 0, if input size is too large ( > LZ4_MAX_INPUT_SIZE)
131 */
132 LZ4_API int LZ4_compressBound(int inputSize);
133 
134 /*
135 LZ4_compress_fast() :
136  Same as LZ4_compress_default(), but allows to select an "acceleration" factor.
137  The larger the acceleration value, the faster the algorithm, but also the lesser the compression.
138  It's a trade-off. It can be fine tuned, with each successive value providing roughly +~3% to speed.
139  An acceleration value of "1" is the same as regular LZ4_compress_default()
140  Values <= 0 will be replaced by ACCELERATION_DEFAULT (see lz4.c), which is 1.
141 */
142 LZ4_API int LZ4_compress_fast(const char* source, char* dest, int sourceSize, int maxDestSize, int acceleration);
143 
144 
145 /*
146 LZ4_compress_fast_extState() :
147  Same compression function, just using an externally allocated memory space to store compression state.
148  Use LZ4_sizeofState() to know how much memory must be allocated,
149  and allocate it on 8-bytes boundaries (using malloc() typically).
150  Then, provide it as 'void* state' to compression function.
151 */
152 LZ4_API int LZ4_sizeofState(void);
153 LZ4_API int LZ4_compress_fast_extState(void* state, const char* source, char* dest, int inputSize, int maxDestSize, int acceleration);
154 
155 
156 /*
157 LZ4_compress_destSize() :
158  Reverse the logic, by compressing as much data as possible from 'source' buffer
159  into already allocated buffer 'dest' of size 'targetDestSize'.
160  This function either compresses the entire 'source' content into 'dest' if it's large enough,
161  or fill 'dest' buffer completely with as much data as possible from 'source'.
162  *sourceSizePtr : will be modified to indicate how many bytes where read from 'source' to fill 'dest'.
163  New value is necessarily <= old value.
164  return : Nb bytes written into 'dest' (necessarily <= targetDestSize)
165  or 0 if compression fails
166 */
167 LZ4_API int LZ4_compress_destSize(const char* source, char* dest, int* sourceSizePtr, int targetDestSize);
168 
169 
170 /*
171 LZ4_decompress_fast() :
172  originalSize : is the original and therefore uncompressed size
173  return : the number of bytes read from the source buffer (in other words, the compressed size)
174  If the source stream is detected malformed, the function will stop decoding and return a negative result.
175  Destination buffer must be already allocated. Its size must be a minimum of 'originalSize' bytes.
176  note : This function fully respect memory boundaries for properly formed compressed data.
177  It is a bit faster than LZ4_decompress_safe().
178  However, it does not provide any protection against intentionally modified data stream (malicious input).
179  Use this function in trusted environment only (data to decode comes from a trusted source).
180 */
181 LZ4_API int LZ4_decompress_fast(const char* source, char* dest, int originalSize);
182 
183 /*
184 LZ4_decompress_safe_partial() :
185  This function decompress a compressed block of size 'compressedSize' at position 'source'
186  into destination buffer 'dest' of size 'maxDecompressedSize'.
187  The function tries to stop decompressing operation as soon as 'targetOutputSize' has been reached,
188  reducing decompression time.
189  return : the number of bytes decoded in the destination buffer (necessarily <= maxDecompressedSize)
190  Note : this number can be < 'targetOutputSize' should the compressed block to decode be smaller.
191  Always control how many bytes were decoded.
192  If the source stream is detected malformed, the function will stop decoding and return a negative result.
193  This function never writes outside of output buffer, and never reads outside of input buffer. It is therefore protected against malicious data packets
194 */
195 LZ4_API int LZ4_decompress_safe_partial(const char* source, char* dest, int compressedSize, int targetOutputSize, int maxDecompressedSize);
196 
197 
198 /***********************************************
199 * Streaming Compression Functions
200 ***********************************************/
201 #define LZ4_STREAMSIZE_U64 ((1 << (LZ4_MEMORY_USAGE-3)) + 4)
202 #define LZ4_STREAMSIZE (LZ4_STREAMSIZE_U64 * sizeof(long long))
203 /*
204  * LZ4_stream_t
205  * information structure to track an LZ4 stream.
206  * important : init this structure content before first use !
207  * note : only allocated directly the structure if you are statically linking LZ4
208  * If you are using liblz4 as a DLL, please use below construction methods instead.
209  */
210 typedef struct { long long table[LZ4_STREAMSIZE_U64]; } LZ4_stream_t;
211 
212 /*
213  * LZ4_resetStream
214  * Use this function to init an allocated LZ4_stream_t structure
215  */
216 LZ4_API void LZ4_resetStream(LZ4_stream_t* streamPtr);
217 
218 /*
219  * LZ4_createStream will allocate and initialize an LZ4_stream_t structure
220  * LZ4_freeStream releases its memory.
221  * In the context of a DLL (liblz4), please use these methods rather than the static struct.
222  * They are more future proof, in case of a change of LZ4_stream_t size.
223  */
224 LZ4_API LZ4_stream_t* LZ4_createStream(void);
225 LZ4_API int LZ4_freeStream(LZ4_stream_t* streamPtr);
226 
227 /*
228  * LZ4_loadDict
229  * Use this function to load a static dictionary into LZ4_stream.
230  * Any previous data will be forgotten, only 'dictionary' will remain in memory.
231  * Loading a size of 0 is allowed.
232  * Return : dictionary size, in bytes (necessarily <= 64 KB)
233  */
234 LZ4_API int LZ4_loadDict(LZ4_stream_t* streamPtr, const char* dictionary, int dictSize);
235 
236 /*
237  * LZ4_compress_fast_continue
238  * Compress buffer content 'src', using data from previously compressed blocks as dictionary to improve compression ratio.
239  * Important : Previous data blocks are assumed to still be present and unmodified !
240  * 'dst' buffer must be already allocated.
241  * If maxDstSize >= LZ4_compressBound(srcSize), compression is guaranteed to succeed, and runs faster.
242  * If not, and if compressed data cannot fit into 'dst' buffer size, compression stops, and function returns a zero.
243  */
244 LZ4_API int LZ4_compress_fast_continue(LZ4_stream_t* streamPtr, const char* src, char* dst, int srcSize, int maxDstSize, int acceleration);
245 
246 /*
247  * LZ4_saveDict
248  * If previously compressed data block is not guaranteed to remain available at its memory location
249  * save it into a safer place (char* safeBuffer)
250  * Note : you don't need to call LZ4_loadDict() afterwards,
251  * dictionary is immediately usable, you can therefore call LZ4_compress_fast_continue()
252  * Return : saved dictionary size in bytes (necessarily <= dictSize), or 0 if error
253  */
254 LZ4_API int LZ4_saveDict(LZ4_stream_t* streamPtr, char* safeBuffer, int dictSize);
255 
256 
257 /************************************************
258 * Streaming Decompression Functions
259 ************************************************/
260 
261 #define LZ4_STREAMDECODESIZE_U64 4
262 #define LZ4_STREAMDECODESIZE (LZ4_STREAMDECODESIZE_U64 * sizeof(unsigned long long))
263 typedef struct { unsigned long long table[LZ4_STREAMDECODESIZE_U64]; } LZ4_streamDecode_t;
264 /*
265  * LZ4_streamDecode_t
266  * information structure to track an LZ4 stream.
267  * init this structure content using LZ4_setStreamDecode or memset() before first use !
268  *
269  * In the context of a DLL (liblz4) please prefer usage of construction methods below.
270  * They are more future proof, in case of a change of LZ4_streamDecode_t size in the future.
271  * LZ4_createStreamDecode will allocate and initialize an LZ4_streamDecode_t structure
272  * LZ4_freeStreamDecode releases its memory.
273  */
274 LZ4_API LZ4_streamDecode_t* LZ4_createStreamDecode(void);
275 LZ4_API int LZ4_freeStreamDecode(LZ4_streamDecode_t* LZ4_stream);
276 
277 /*
278  * LZ4_setStreamDecode
279  * Use this function to instruct where to find the dictionary.
280  * Setting a size of 0 is allowed (same effect as reset).
281  * Return : 1 if OK, 0 if error
282  */
283 LZ4_API int LZ4_setStreamDecode(LZ4_streamDecode_t* LZ4_streamDecode, const char* dictionary, int dictSize);
284 
285 /*
286 *_continue() :
287  These decoding functions allow decompression of multiple blocks in "streaming" mode.
288  Previously decoded blocks *must* remain available at the memory position where they were decoded (up to 64 KB)
289  In the case of a ring buffers, decoding buffer must be either :
290  - Exactly same size as encoding buffer, with same update rule (block boundaries at same positions)
291  In which case, the decoding & encoding ring buffer can have any size, including very small ones ( < 64 KB).
292  - Larger than encoding buffer, by a minimum of maxBlockSize more bytes.
293  maxBlockSize is implementation dependent. It's the maximum size you intend to compress into a single block.
294  In which case, encoding and decoding buffers do not need to be synchronized,
295  and encoding ring buffer can have any size, including small ones ( < 64 KB).
296  - _At least_ 64 KB + 8 bytes + maxBlockSize.
297  In which case, encoding and decoding buffers do not need to be synchronized,
298  and encoding ring buffer can have any size, including larger than decoding buffer.
299  Whenever these conditions are not possible, save the last 64KB of decoded data into a safe buffer,
300  and indicate where it is saved using LZ4_setStreamDecode()
301 */
302 LZ4_API int LZ4_decompress_safe_continue(LZ4_streamDecode_t* LZ4_streamDecode, const char* source, char* dest, int compressedSize, int maxDecompressedSize);
303 LZ4_API int LZ4_decompress_fast_continue(LZ4_streamDecode_t* LZ4_streamDecode, const char* source, char* dest, int originalSize);
304 
305 
306 /*
307 Advanced decoding functions :
308 *_usingDict() :
309  These decoding functions work the same as
310  a combination of LZ4_setStreamDecode() followed by LZ4_decompress_x_continue()
311  They are stand-alone. They don't need nor update an LZ4_streamDecode_t structure.
312 */
313 LZ4_API int LZ4_decompress_safe_usingDict(const char* source, char* dest, int compressedSize, int maxDecompressedSize, const char* dictStart, int dictSize);
314 LZ4_API int LZ4_decompress_fast_usingDict(const char* source, char* dest, int originalSize, const char* dictStart, int dictSize);
315 
316 
317 
318 /**************************************
319 * Obsolete Functions
320 **************************************/
321 
322 /* Obsolete compression functions */
323 /* These functions are planned to start generate warnings by r131 approximately */
324 LZ4_API int LZ4_compress(const char* source, char* dest, int sourceSize);
325 LZ4_API int LZ4_compress_limitedOutput(const char* source, char* dest, int sourceSize, int maxOutputSize);
326 LZ4_API int LZ4_compress_withState(void* state, const char* source, char* dest, int inputSize);
327 LZ4_API int LZ4_compress_limitedOutput_withState(void* state, const char* source, char* dest, int inputSize, int maxOutputSize);
328 LZ4_API int LZ4_compress_continue(LZ4_stream_t* LZ4_streamPtr, const char* source, char* dest, int inputSize);
329 LZ4_API int LZ4_compress_limitedOutput_continue(LZ4_stream_t* LZ4_streamPtr, const char* source, char* dest, int inputSize, int maxOutputSize);
330 
331 
332 #if defined (__cplusplus)
333 }
334 #endif