ACloudViewer  3.9.4
A Modern Library for 3D Data Processing
lz4.h
Go to the documentation of this file.
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 // fix undefined symbols issues on windows
38 // #define LZ4LIB_API __declspec(dllexport)
39 
40 #if defined (__cplusplus)
41 extern "C" {
42 #endif
43 
44 /*
45  * lz4.h provides block compression functions, and gives full buffer control to programmer.
46  * If you need to generate inter-operable compressed data (respecting LZ4 frame specification),
47  * and can let the library handle its own memory, please use lz4frame.h instead.
48 */
49 
50 /**************************************
51 * Version
52 **************************************/
53 #define LZ4_VERSION_MAJOR 1 /* for breaking interface changes */
54 #define LZ4_VERSION_MINOR 7 /* for new (non-breaking) interface capabilities */
55 #define LZ4_VERSION_RELEASE 1 /* for tweaks, bug-fixes, or development */
56 #define LZ4_VERSION_NUMBER (LZ4_VERSION_MAJOR *100*100 + LZ4_VERSION_MINOR *100 + LZ4_VERSION_RELEASE)
57 int LZ4_versionNumber (void);
58 
59 /**************************************
60 * Tuning parameter
61 **************************************/
62 /*
63  * LZ4_MEMORY_USAGE :
64  * Memory usage formula : N->2^N Bytes (examples : 10 -> 1KB; 12 -> 4KB ; 16 -> 64KB; 20 -> 1MB; etc.)
65  * Increasing memory usage improves compression ratio
66  * Reduced memory usage can improve speed, due to cache effect
67  * Default value is 14, for 16KB, which nicely fits into Intel x86 L1 cache
68  */
69 #define LZ4_MEMORY_USAGE 14
70 
71 
72 /**************************************
73 * Simple Functions
74 **************************************/
75 
76 int LZ4_compress_default(const char* source, char* dest, int sourceSize, int maxDestSize);
77 int LZ4_decompress_safe (const char* source, char* dest, int compressedSize, int maxDecompressedSize);
78 
79 /*
80 LZ4_compress_default() :
81  Compresses 'sourceSize' bytes from buffer 'source'
82  into already allocated 'dest' buffer of size 'maxDestSize'.
83  Compression is guaranteed to succeed if 'maxDestSize' >= LZ4_compressBound(sourceSize).
84  It also runs faster, so it's a recommended setting.
85  If the function cannot compress 'source' into a more limited 'dest' budget,
86  compression stops *immediately*, and the function result is zero.
87  As a consequence, 'dest' content is not valid.
88  This function never writes outside 'dest' buffer, nor read outside 'source' buffer.
89  sourceSize : Max supported value is LZ4_MAX_INPUT_VALUE
90  maxDestSize : full or partial size of buffer 'dest' (which must be already allocated)
91  return : the number of bytes written into buffer 'dest' (necessarily <= maxOutputSize)
92  or 0 if compression fails
93 
94 LZ4_decompress_safe() :
95  compressedSize : is the precise full size of the compressed block.
96  maxDecompressedSize : is the size of destination buffer, which must be already allocated.
97  return : the number of bytes decompressed into destination buffer (necessarily <= maxDecompressedSize)
98  If destination buffer is not large enough, decoding will stop and output an error code (<0).
99  If the source stream is detected malformed, the function will stop decoding and return a negative result.
100  This function is protected against buffer overflow exploits, including malicious data packets.
101  It never writes outside output buffer, nor reads outside input buffer.
102 */
103 
104 
105 /**************************************
106 * Advanced Functions
107 **************************************/
108 #define LZ4_MAX_INPUT_SIZE 0x7E000000 /* 2 113 929 216 bytes */
109 #define LZ4_COMPRESSBOUND(isize) ((unsigned)(isize) > (unsigned)LZ4_MAX_INPUT_SIZE ? 0 : (isize) + ((isize)/255) + 16)
110 
111 /*
112 LZ4_compressBound() :
113  Provides the maximum size that LZ4 compression may output in a "worst case" scenario (input data not compressible)
114  This function is primarily useful for memory allocation purposes (destination buffer size).
115  Macro LZ4_COMPRESSBOUND() is also provided for compilation-time evaluation (stack memory allocation for example).
116  Note that LZ4_compress_default() compress faster when dest buffer size is >= LZ4_compressBound(srcSize)
117  inputSize : max supported value is LZ4_MAX_INPUT_SIZE
118  return : maximum output size in a "worst case" scenario
119  or 0, if input size is too large ( > LZ4_MAX_INPUT_SIZE)
120 */
121 int LZ4_compressBound(int inputSize);
122 
123 /*
124 LZ4_compress_fast() :
125  Same as LZ4_compress_default(), but allows to select an "acceleration" factor.
126  The larger the acceleration value, the faster the algorithm, but also the lesser the compression.
127  It's a trade-off. It can be fine tuned, with each successive value providing roughly +~3% to speed.
128  An acceleration value of "1" is the same as regular LZ4_compress_default()
129  Values <= 0 will be replaced by ACCELERATION_DEFAULT (see lz4.c), which is 1.
130 */
131 int LZ4_compress_fast (const char* source, char* dest, int sourceSize, int maxDestSize, int acceleration);
132 
133 
134 /*
135 LZ4_compress_fast_extState() :
136  Same compression function, just using an externally allocated memory space to store compression state.
137  Use LZ4_sizeofState() to know how much memory must be allocated,
138  and allocate it on 8-bytes boundaries (using malloc() typically).
139  Then, provide it as 'void* state' to compression function.
140 */
141 int LZ4_sizeofState(void);
142 int LZ4_compress_fast_extState (void* state, const char* source, char* dest, int inputSize, int maxDestSize, int acceleration);
143 
144 
145 /*
146 LZ4_compress_destSize() :
147  Reverse the logic, by compressing as much data as possible from 'source' buffer
148  into already allocated buffer 'dest' of size 'targetDestSize'.
149  This function either compresses the entire 'source' content into 'dest' if it's large enough,
150  or fill 'dest' buffer completely with as much data as possible from 'source'.
151  *sourceSizePtr : will be modified to indicate how many bytes where read from 'source' to fill 'dest'.
152  New value is necessarily <= old value.
153  return : Nb bytes written into 'dest' (necessarily <= targetDestSize)
154  or 0 if compression fails
155 */
156 int LZ4_compress_destSize (const char* source, char* dest, int* sourceSizePtr, int targetDestSize);
157 
158 
159 /*
160 LZ4_decompress_fast() :
161  originalSize : is the original and therefore uncompressed size
162  return : the number of bytes read from the source buffer (in other words, the compressed size)
163  If the source stream is detected malformed, the function will stop decoding and return a negative result.
164  Destination buffer must be already allocated. Its size must be a minimum of 'originalSize' bytes.
165  note : This function fully respect memory boundaries for properly formed compressed data.
166  It is a bit faster than LZ4_decompress_safe().
167  However, it does not provide any protection against intentionally modified data stream (malicious input).
168  Use this function in trusted environment only (data to decode comes from a trusted source).
169 */
170 int LZ4_decompress_fast (const char* source, char* dest, int originalSize);
171 
172 /*
173 LZ4_decompress_safe_partial() :
174  This function decompress a compressed block of size 'compressedSize' at position 'source'
175  into destination buffer 'dest' of size 'maxDecompressedSize'.
176  The function tries to stop decompressing operation as soon as 'targetOutputSize' has been reached,
177  reducing decompression time.
178  return : the number of bytes decoded in the destination buffer (necessarily <= maxDecompressedSize)
179  Note : this number can be < 'targetOutputSize' should the compressed block to decode be smaller.
180  Always control how many bytes were decoded.
181  If the source stream is detected malformed, the function will stop decoding and return a negative result.
182  This function never writes outside of output buffer, and never reads outside of input buffer. It is therefore protected against malicious data packets
183 */
184 int LZ4_decompress_safe_partial (const char* source, char* dest, int compressedSize, int targetOutputSize, int maxDecompressedSize);
185 
186 
187 /***********************************************
188 * Streaming Compression Functions
189 ***********************************************/
190 #define LZ4_STREAMSIZE_U64 ((1 << (LZ4_MEMORY_USAGE-3)) + 4)
191 #define LZ4_STREAMSIZE (LZ4_STREAMSIZE_U64 * sizeof(long long))
192 /*
193  * LZ4_stream_t
194  * information structure to track an LZ4 stream.
195  * important : init this structure content before first use !
196  * note : only allocated directly the structure if you are statically linking LZ4
197  * If you are using liblz4 as a DLL, please use below construction methods instead.
198  */
199 typedef struct { long long table[LZ4_STREAMSIZE_U64]; } LZ4_stream_t;
200 
201 /*
202  * LZ4_resetStream
203  * Use this function to init an allocated LZ4_stream_t structure
204  */
205 void LZ4_resetStream (LZ4_stream_t* streamPtr);
206 
207 /*
208  * LZ4_createStream will allocate and initialize an LZ4_stream_t structure
209  * LZ4_freeStream releases its memory.
210  * In the context of a DLL (liblz4), please use these methods rather than the static struct.
211  * They are more future proof, in case of a change of LZ4_stream_t size.
212  */
214 int LZ4_freeStream (LZ4_stream_t* streamPtr);
215 
216 /*
217  * LZ4_loadDict
218  * Use this function to load a static dictionary into LZ4_stream.
219  * Any previous data will be forgotten, only 'dictionary' will remain in memory.
220  * Loading a size of 0 is allowed.
221  * Return : dictionary size, in bytes (necessarily <= 64 KB)
222  */
223 int LZ4_loadDict (LZ4_stream_t* streamPtr, const char* dictionary, int dictSize);
224 
225 /*
226  * LZ4_compress_fast_continue
227  * Compress buffer content 'src', using data from previously compressed blocks as dictionary to improve compression ratio.
228  * Important : Previous data blocks are assumed to still be present and unmodified !
229  * 'dst' buffer must be already allocated.
230  * If maxDstSize >= LZ4_compressBound(srcSize), compression is guaranteed to succeed, and runs faster.
231  * If not, and if compressed data cannot fit into 'dst' buffer size, compression stops, and function returns a zero.
232  */
233 int LZ4_compress_fast_continue (LZ4_stream_t* streamPtr, const char* src, char* dst, int srcSize, int maxDstSize, int acceleration);
234 
235 /*
236  * LZ4_saveDict
237  * If previously compressed data block is not guaranteed to remain available at its memory location
238  * save it into a safer place (char* safeBuffer)
239  * Note : you don't need to call LZ4_loadDict() afterwards,
240  * dictionary is immediately usable, you can therefore call LZ4_compress_fast_continue()
241  * Return : saved dictionary size in bytes (necessarily <= dictSize), or 0 if error
242  */
243 int LZ4_saveDict (LZ4_stream_t* streamPtr, char* safeBuffer, int dictSize);
244 
245 
246 /************************************************
247 * Streaming Decompression Functions
248 ************************************************/
249 
250 #define LZ4_STREAMDECODESIZE_U64 4
251 #define LZ4_STREAMDECODESIZE (LZ4_STREAMDECODESIZE_U64 * sizeof(unsigned long long))
252 typedef struct { unsigned long long table[LZ4_STREAMDECODESIZE_U64]; } LZ4_streamDecode_t;
253 /*
254  * LZ4_streamDecode_t
255  * information structure to track an LZ4 stream.
256  * init this structure content using LZ4_setStreamDecode or memset() before first use !
257  *
258  * In the context of a DLL (liblz4) please prefer usage of construction methods below.
259  * They are more future proof, in case of a change of LZ4_streamDecode_t size in the future.
260  * LZ4_createStreamDecode will allocate and initialize an LZ4_streamDecode_t structure
261  * LZ4_freeStreamDecode releases its memory.
262  */
264 int LZ4_freeStreamDecode (LZ4_streamDecode_t* LZ4_stream);
265 
266 /*
267  * LZ4_setStreamDecode
268  * Use this function to instruct where to find the dictionary.
269  * Setting a size of 0 is allowed (same effect as reset).
270  * Return : 1 if OK, 0 if error
271  */
272 int LZ4_setStreamDecode (LZ4_streamDecode_t* LZ4_streamDecode, const char* dictionary, int dictSize);
273 
274 /*
275 *_continue() :
276  These decoding functions allow decompression of multiple blocks in "streaming" mode.
277  Previously decoded blocks *must* remain available at the memory position where they were decoded (up to 64 KB)
278  In the case of a ring buffers, decoding buffer must be either :
279  - Exactly same size as encoding buffer, with same update rule (block boundaries at same positions)
280  In which case, the decoding & encoding ring buffer can have any size, including very small ones ( < 64 KB).
281  - Larger than encoding buffer, by a minimum of maxBlockSize more bytes.
282  maxBlockSize is implementation dependent. It's the maximum size you intend to compress into a single block.
283  In which case, encoding and decoding buffers do not need to be synchronized,
284  and encoding ring buffer can have any size, including small ones ( < 64 KB).
285  - _At least_ 64 KB + 8 bytes + maxBlockSize.
286  In which case, encoding and decoding buffers do not need to be synchronized,
287  and encoding ring buffer can have any size, including larger than decoding buffer.
288  Whenever these conditions are not possible, save the last 64KB of decoded data into a safe buffer,
289  and indicate where it is saved using LZ4_setStreamDecode()
290 */
291 int LZ4_decompress_safe_continue (LZ4_streamDecode_t* LZ4_streamDecode, const char* source, char* dest, int compressedSize, int maxDecompressedSize);
292 int LZ4_decompress_fast_continue (LZ4_streamDecode_t* LZ4_streamDecode, const char* source, char* dest, int originalSize);
293 
294 
295 /*
296 Advanced decoding functions :
297 *_usingDict() :
298  These decoding functions work the same as
299  a combination of LZ4_setStreamDecode() followed by LZ4_decompress_x_continue()
300  They are stand-alone. They don't need nor update an LZ4_streamDecode_t structure.
301 */
302 int LZ4_decompress_safe_usingDict (const char* source, char* dest, int compressedSize, int maxDecompressedSize, const char* dictStart, int dictSize);
303 int LZ4_decompress_fast_usingDict (const char* source, char* dest, int originalSize, const char* dictStart, int dictSize);
304 
305 
306 
307 /**************************************
308 * Obsolete Functions
309 **************************************/
310 /* Deprecate Warnings */
311 /* Should these warnings messages be a problem,
312  it is generally possible to disable them,
313  with -Wno-deprecated-declarations for gcc
314  or _CRT_SECURE_NO_WARNINGS in Visual for example.
315  You can also define LZ4_DEPRECATE_WARNING_DEFBLOCK. */
316 #ifndef LZ4_DEPRECATE_WARNING_DEFBLOCK
317 # define LZ4_DEPRECATE_WARNING_DEFBLOCK
318 # define LZ4_GCC_VERSION (__GNUC__ * 100 + __GNUC_MINOR__)
319 # if (LZ4_GCC_VERSION >= 405) || defined(__clang__)
320 # define LZ4_DEPRECATED(message) __attribute__((deprecated(message)))
321 # elif (LZ4_GCC_VERSION >= 301)
322 # define LZ4_DEPRECATED(message) __attribute__((deprecated))
323 # elif defined(_MSC_VER)
324 # define LZ4_DEPRECATED(message) __declspec(deprecated(message))
325 # else
326 # pragma message("WARNING: You need to implement LZ4_DEPRECATED for this compiler")
327 # define LZ4_DEPRECATED(message)
328 # endif
329 #endif /* LZ4_DEPRECATE_WARNING_DEFBLOCK */
330 
331 /* Obsolete compression functions */
332 /* These functions are planned to start generate warnings by r131 approximately */
333 int LZ4_compress (const char* source, char* dest, int sourceSize);
334 int LZ4_compress_limitedOutput (const char* source, char* dest, int sourceSize, int maxOutputSize);
335 int LZ4_compress_withState (void* state, const char* source, char* dest, int inputSize);
336 int LZ4_compress_limitedOutput_withState (void* state, const char* source, char* dest, int inputSize, int maxOutputSize);
337 int LZ4_compress_continue (LZ4_stream_t* LZ4_streamPtr, const char* source, char* dest, int inputSize);
338 int LZ4_compress_limitedOutput_continue (LZ4_stream_t* LZ4_streamPtr, const char* source, char* dest, int inputSize, int maxOutputSize);
339 
340 /* Obsolete decompression functions */
341 /* These function names are completely deprecated and must no longer be used.
342  They are only provided here for compatibility with older programs.
343  - LZ4_uncompress is the same as LZ4_decompress_fast
344  - LZ4_uncompress_unknownOutputSize is the same as LZ4_decompress_safe
345  These function prototypes are now disabled; uncomment them only if you really need them.
346  It is highly recommended to stop using these prototypes and migrate to maintained ones */
347 /* int LZ4_uncompress (const char* source, char* dest, int outputSize); */
348 /* int LZ4_uncompress_unknownOutputSize (const char* source, char* dest, int isize, int maxOutputSize); */
349 
350 /* Obsolete streaming functions; use new streaming interface whenever possible */
351 LZ4_DEPRECATED("use LZ4_createStream() instead") void* LZ4_create (char* inputBuffer);
352 LZ4_DEPRECATED("use LZ4_createStream() instead") int LZ4_sizeofStreamState(void);
353 LZ4_DEPRECATED("use LZ4_resetStream() instead") int LZ4_resetStreamState(void* state, char* inputBuffer);
354 LZ4_DEPRECATED("use LZ4_saveDict() instead") char* LZ4_slideInputBuffer (void* state);
355 
356 /* Obsolete streaming decoding functions */
357 LZ4_DEPRECATED("use LZ4_decompress_safe_usingDict() instead") int LZ4_decompress_safe_withPrefix64k (const char* src, char* dst, int compressedSize, int maxDstSize);
358 LZ4_DEPRECATED("use LZ4_decompress_fast_usingDict() instead") int LZ4_decompress_fast_withPrefix64k (const char* src, char* dst, int originalSize);
359 
360 
361 #if defined (__cplusplus)
362 }
363 #endif
int LZ4_decompress_safe(const char *source, char *dest, int compressedSize, int maxDecompressedSize)
Definition: lz4.c:1288
int LZ4_decompress_fast_withPrefix64k(const char *src, char *dst, int originalSize)
Definition: lz4.c:1510
int LZ4_decompress_fast_usingDict(const char *source, char *dest, int originalSize, const char *dictStart, int dictSize)
Definition: lz4.c:1439
int LZ4_decompress_fast(const char *source, char *dest, int originalSize)
Definition: lz4.c:1298
int LZ4_compress_limitedOutput_continue(LZ4_stream_t *LZ4_streamPtr, const char *source, char *dest, int inputSize, int maxOutputSize)
Definition: lz4.c:1459
int LZ4_freeStream(LZ4_stream_t *streamPtr)
Definition: lz4.c:948
LZ4_stream_t * LZ4_createStream(void)
Definition: lz4.c:935
LZ4_streamDecode_t * LZ4_createStreamDecode(void)
Definition: lz4.c:1319
int LZ4_decompress_safe_partial(const char *source, char *dest, int compressedSize, int targetOutputSize, int maxDecompressedSize)
Definition: lz4.c:1293
int LZ4_compressBound(int inputSize)
Definition: lz4.c:372
int LZ4_compress(const char *source, char *dest, int sourceSize)
Definition: lz4.c:1456
int LZ4_compress_limitedOutput(const char *source, char *dest, int sourceSize, int maxOutputSize)
Definition: lz4.c:1455
void * LZ4_create(char *inputBuffer)
Definition: lz4.c:1489
#define LZ4_STREAMDECODESIZE_U64
Definition: lz4.h:250
int LZ4_compress_withState(void *state, const char *source, char *dest, int inputSize)
Definition: lz4.c:1458
int LZ4_sizeofState(void)
Definition: lz4.c:373
int LZ4_compress_default(const char *source, char *dest, int sourceSize, int maxDestSize)
Definition: lz4.c:697
int LZ4_decompress_safe_withPrefix64k(const char *src, char *dst, int compressedSize, int maxDstSize)
Definition: lz4.c:1505
void LZ4_resetStream(LZ4_stream_t *streamPtr)
Definition: lz4.c:943
int LZ4_compress_destSize(const char *source, char *dest, int *sourceSizePtr, int targetDestSize)
Definition: lz4.c:912
int LZ4_decompress_safe_usingDict(const char *source, char *dest, int compressedSize, int maxDecompressedSize, const char *dictStart, int dictSize)
Definition: lz4.c:1434
int LZ4_loadDict(LZ4_stream_t *streamPtr, const char *dictionary, int dictSize)
Definition: lz4.c:956
char * LZ4_slideInputBuffer(void *state)
Definition: lz4.c:1496
int LZ4_sizeofStreamState(void)
Definition: lz4.c:1474
int LZ4_compress_fast_continue(LZ4_stream_t *streamPtr, const char *src, char *dst, int srcSize, int maxDstSize, int acceleration)
Definition: lz4.c:1011
int LZ4_setStreamDecode(LZ4_streamDecode_t *LZ4_streamDecode, const char *dictionary, int dictSize)
Definition: lz4.c:1338
int LZ4_versionNumber(void)
Definition: lz4.c:371
int LZ4_saveDict(LZ4_stream_t *streamPtr, char *safeBuffer, int dictSize)
Definition: lz4.c:1083
int LZ4_compress_fast_extState(void *state, const char *source, char *dest, int inputSize, int maxDestSize, int acceleration)
Definition: lz4.c:657
int LZ4_compress_limitedOutput_withState(void *state, const char *source, char *dest, int inputSize, int maxOutputSize)
Definition: lz4.c:1457
int LZ4_decompress_safe_continue(LZ4_streamDecode_t *LZ4_streamDecode, const char *source, char *dest, int compressedSize, int maxDecompressedSize)
Definition: lz4.c:1355
int LZ4_resetStreamState(void *state, char *inputBuffer)
Definition: lz4.c:1482
#define LZ4_STREAMSIZE_U64
Definition: lz4.h:190
int LZ4_decompress_fast_continue(LZ4_streamDecode_t *LZ4_streamDecode, const char *source, char *dest, int originalSize)
Definition: lz4.c:1384
int LZ4_compress_continue(LZ4_stream_t *LZ4_streamPtr, const char *source, char *dest, int inputSize)
Definition: lz4.c:1460
#define LZ4_DEPRECATED(message)
Definition: lz4.h:327
int LZ4_freeStreamDecode(LZ4_streamDecode_t *LZ4_stream)
Definition: lz4.c:1325
int LZ4_compress_fast(const char *source, char *dest, int sourceSize, int maxDestSize, int acceleration)
Definition: lz4.c:679