lz4.h revision d7298d2059a4f2da7aa19122d3af2aacb931972b
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#if defined (__cplusplus) 38extern "C" { 39#endif 40 41/* 42 * lz4.h provides block compression functions, and gives full buffer control to programmer. 43 * If you need to generate inter-operable compressed data (respecting LZ4 frame specification), 44 * and can let the library handle its own memory, please use lz4frame.h instead. 45*/ 46 47/************************************** 48* Version 49**************************************/ 50#define LZ4_VERSION_MAJOR 1 /* for breaking interface changes */ 51#define LZ4_VERSION_MINOR 7 /* for new (non-breaking) interface capabilities */ 52#define LZ4_VERSION_RELEASE 0 /* for tweaks, bug-fixes, or development */ 53#define LZ4_VERSION_NUMBER (LZ4_VERSION_MAJOR *100*100 + LZ4_VERSION_MINOR *100 + LZ4_VERSION_RELEASE) 54int LZ4_versionNumber (void); 55 56/************************************** 57* Tuning parameter 58**************************************/ 59/* 60 * LZ4_MEMORY_USAGE : 61 * Memory usage formula : N->2^N Bytes (examples : 10 -> 1KB; 12 -> 4KB ; 16 -> 64KB; 20 -> 1MB; etc.) 62 * Increasing memory usage improves compression ratio 63 * Reduced memory usage can improve speed, due to cache effect 64 * Default value is 14, for 16KB, which nicely fits into Intel x86 L1 cache 65 */ 66#define LZ4_MEMORY_USAGE 14 67 68 69/************************************** 70* Simple Functions 71**************************************/ 72 73int LZ4_compress_safe (const char* source, char* dest, int sourceSize, int maxDestSize); 74int LZ4_decompress_safe (const char* source, char* dest, int compressedSize, int maxDecompressedSize); 75 76/* 77LZ4_compress_limitedOutput() : 78 Compresses 'sourceSize' bytes from buffer 'source' 79 into already allocated 'dest' of size 'maxOutputSize'. 80 Compression runs faster when 'maxOutputSize' >= LZ4_compressBound(sourceSize). 81 That's because in such case, it is guaranteed to compress within 'dest' budget, even in worst case scenario. 82 Compressing into a more limited space budget requires additional checks. 83 If the function cannot compress 'source' into a limited 'dest' budget, 84 compression stops *immediately*, and result of the function is zero. 85 It greatly accelerates behavior on non-compressible input, but as a consequence, 'dest' content is not valid either. 86 This function never writes outside 'dest' buffer, nor read outside 'source' buffer. 87 sourceSize : Max supported value is LZ4_MAX_INPUT_VALUE 88 maxDestSize : full or partial size of buffer 'dest' (which must be already allocated) 89 return : the number of bytes written into buffer 'dest' (necessarily <= maxOutputSize) 90 or 0 if compression fails 91 92LZ4_decompress_safe() : 93 compressedSize : is obviously the source size 94 maxDecompressedSize : is the size of the destination buffer, which must be already allocated. 95 return : the number of bytes decompressed into the destination buffer (necessarily <= maxDecompressedSize) 96 If the destination buffer is not large enough, decoding will stop and output an error code (<0). 97 If the source stream is detected malformed, the function will stop decoding and return a negative result. 98 This function is protected against buffer overflow exploits, including malicious data packets. 99 It never writes outside of output buffer, nor reads outside of input buffer. 100*/ 101 102 103/************************************** 104* Advanced Functions 105**************************************/ 106#define LZ4_MAX_INPUT_SIZE 0x7E000000 /* 2 113 929 216 bytes */ 107#define LZ4_COMPRESSBOUND(isize) ((unsigned)(isize) > (unsigned)LZ4_MAX_INPUT_SIZE ? 0 : (isize) + ((isize)/255) + 16) 108 109/* 110LZ4_compressBound() : 111 Provides the maximum size that LZ4 compression may output in a "worst case" scenario (input data not compressible) 112 This function is primarily useful for memory allocation purposes (output buffer size). 113 Macro LZ4_COMPRESSBOUND() is also provided for compilation-time evaluation (stack memory allocation for example). 114 115 inputSize : max supported value is LZ4_MAX_INPUT_SIZE 116 return : maximum output size in a "worst case" scenario 117 or 0, if input size is too large ( > LZ4_MAX_INPUT_SIZE) 118*/ 119int LZ4_compressBound(int inputSize); 120 121/* 122LZ4_compress_fast() : 123 Same as LZ4_compress_limitedOutput, but allows to select an "acceleration" factor. 124 The larger the value, the faster the algorithm, but also the lesser the compression. 125 So it's a trade-off, which can be fine tuned, selecting whichever value you want. 126 An acceleration value of "0" means "use Default value", which is typically about 15 (see lz4.c source code). 127 Note : this function is "safe", even if its name does not say it. It's just faster and compress less. 128*/ 129int LZ4_compress_fast (const char* source, char* dest, int sourceSize, int maxOutputSize, unsigned acceleration); 130 131/* 132LZ4_compress_safe_extState() : 133 Same compression function, just 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 Note that 'state' ptr must be aligned on 4-bytes boundaries. 137*/ 138int LZ4_sizeofState(void); 139int LZ4_compress_safe_extState (void* state, const char* source, char* dest, int inputSize, int maxOutputSize); 140 141 142/* 143LZ4_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*/ 153int LZ4_decompress_fast (const char* source, char* dest, int originalSize); 154 155/* 156LZ4_decompress_safe_partial() : 157 This function decompress a compressed block of size 'compressedSize' at position 'source' 158 into destination buffer 'dest' of size 'maxDecompressedSize'. 159 The function tries to stop decompressing operation as soon as 'targetOutputSize' has been reached, 160 reducing decompression time. 161 return : the number of bytes decoded in the destination buffer (necessarily <= maxDecompressedSize) 162 Note : this number can be < 'targetOutputSize' should the compressed block to decode be smaller. 163 Always control how many bytes were decoded. 164 If the source stream is detected malformed, the function will stop decoding and return a negative result. 165 This function never writes outside of output buffer, and never reads outside of input buffer. It is therefore protected against malicious data packets 166*/ 167int LZ4_decompress_safe_partial (const char* source, char* dest, int compressedSize, int targetOutputSize, int maxDecompressedSize); 168 169 170/*********************************************** 171* Streaming Compression Functions 172***********************************************/ 173#define LZ4_STREAMSIZE_U64 ((1 << (LZ4_MEMORY_USAGE-3)) + 4) 174#define LZ4_STREAMSIZE (LZ4_STREAMSIZE_U64 * sizeof(long long)) 175/* 176 * LZ4_stream_t 177 * information structure to track an LZ4 stream. 178 * important : init this structure content before first use ! 179 * note : only allocated directly the structure if you are statically linking LZ4 180 * If you are using liblz4 as a DLL, please use below construction methods instead. 181 */ 182typedef struct { long long table[LZ4_STREAMSIZE_U64]; } LZ4_stream_t; 183 184/* 185 * LZ4_resetStream 186 * Use this function to init an allocated LZ4_stream_t structure 187 */ 188void LZ4_resetStream (LZ4_stream_t* LZ4_streamPtr); 189 190/* 191 * LZ4_createStream will allocate and initialize an LZ4_stream_t structure 192 * LZ4_freeStream releases its memory. 193 * In the context of a DLL (liblz4), please use these methods rather than the static struct. 194 * They are more future proof, in case of a change of LZ4_stream_t size. 195 */ 196LZ4_stream_t* LZ4_createStream(void); 197int LZ4_freeStream (LZ4_stream_t* LZ4_streamPtr); 198 199/* 200 * LZ4_loadDict 201 * Use this function to load a static dictionary into LZ4_stream. 202 * Any previous data will be forgotten, only 'dictionary' will remain in memory. 203 * Loading a size of 0 is allowed. 204 * Return : dictionary size, in bytes (necessarily <= 64 KB) 205 */ 206int LZ4_loadDict (LZ4_stream_t* LZ4_streamPtr, const char* dictionary, int dictSize); 207 208/* 209 * LZ4_compress_safe_continue 210 * Compress data block 'source', using data from previous blocks to improve compression ratio. 211 * Important : Previous data blocks are assumed to still be present and unmodified ! 212 * dest buffer must be already allocated. 213 * if maxOutpuSize >= (inputSize), compression is guaranteed to succeed. 214 * if not, and if target size objective cannot be met, compression stops, and function returns a zero. 215 */ 216int LZ4_compress_safe_continue (LZ4_stream_t* LZ4_streamPtr, const char* source, char* dest, int inputSize, int maxOutputSize); 217 218/* 219 * LZ4_saveDict 220 * If previously compressed data block is not guaranteed to remain available at its memory location 221 * save it into a safer place (char* safeBuffer) 222 * Note : you don't need to call LZ4_loadDict() afterwards, 223 * dictionary is immediately usable, you can therefore call again LZ4_compress_continue() 224 * Return : saved dictionary size in bytes (necessarily <= dictSize), or 0 if error 225 */ 226int LZ4_saveDict (LZ4_stream_t* LZ4_streamPtr, char* safeBuffer, int dictSize); 227 228 229/************************************************ 230* Streaming Decompression Functions 231************************************************/ 232 233#define LZ4_STREAMDECODESIZE_U64 4 234#define LZ4_STREAMDECODESIZE (LZ4_STREAMDECODESIZE_U64 * sizeof(unsigned long long)) 235typedef struct { unsigned long long table[LZ4_STREAMDECODESIZE_U64]; } LZ4_streamDecode_t; 236/* 237 * LZ4_streamDecode_t 238 * information structure to track an LZ4 stream. 239 * init this structure content using LZ4_setStreamDecode or memset() before first use ! 240 * 241 * In the context of a DLL (liblz4) please prefer usage of construction methods below. 242 * They are more future proof, in case of a change of LZ4_streamDecode_t size in the future. 243 * LZ4_createStreamDecode will allocate and initialize an LZ4_streamDecode_t structure 244 * LZ4_freeStreamDecode releases its memory. 245 */ 246LZ4_streamDecode_t* LZ4_createStreamDecode(void); 247int LZ4_freeStreamDecode (LZ4_streamDecode_t* LZ4_stream); 248 249/* 250 * LZ4_setStreamDecode 251 * Use this function to instruct where to find the dictionary. 252 * Setting a size of 0 is allowed (same effect as reset). 253 * Return : 1 if OK, 0 if error 254 */ 255int LZ4_setStreamDecode (LZ4_streamDecode_t* LZ4_streamDecode, const char* dictionary, int dictSize); 256 257/* 258*_continue() : 259 These decoding functions allow decompression of multiple blocks in "streaming" mode. 260 Previously decoded blocks *must* remain available at the memory position where they were decoded (up to 64 KB) 261 If this condition is not possible, save the relevant part of decoded data into a safe buffer, 262 and indicate where is its new address using LZ4_setStreamDecode() 263*/ 264int LZ4_decompress_safe_continue (LZ4_streamDecode_t* LZ4_streamDecode, const char* source, char* dest, int compressedSize, int maxDecompressedSize); 265int LZ4_decompress_fast_continue (LZ4_streamDecode_t* LZ4_streamDecode, const char* source, char* dest, int originalSize); 266 267 268/* 269Advanced decoding functions : 270*_usingDict() : 271 These decoding functions work the same as 272 a combination of LZ4_setDictDecode() followed by LZ4_decompress_x_continue() 273 They are stand-alone and don't use nor update an LZ4_streamDecode_t structure. 274*/ 275int LZ4_decompress_safe_usingDict (const char* source, char* dest, int compressedSize, int maxDecompressedSize, const char* dictStart, int dictSize); 276int LZ4_decompress_fast_usingDict (const char* source, char* dest, int originalSize, const char* dictStart, int dictSize); 277 278 279 280/************************************** 281* Obsolete Functions 282**************************************/ 283/* Deprecate Warnings */ 284/* Should these warnings messages be a problem, 285 it is generally possible to disable them, 286 with -Wno-deprecated-declarations for gcc 287 or _CRT_SECURE_NO_WARNINGS in Visual for example. 288 You can also define LZ4_DEPRECATE_WARNING_DEFBLOCK. */ 289#ifndef LZ4_DEPRECATE_WARNING_DEFBLOCK 290# define LZ4_DEPRECATE_WARNING_DEFBLOCK 291# define LZ4_GCC_VERSION (__GNUC__ * 100 + __GNUC_MINOR__) 292# if (LZ4_GCC_VERSION >= 405) || defined(__clang__) 293# define LZ4_DEPRECATED(message) __attribute__((deprecated(message))) 294# elif (LZ4_GCC_VERSION >= 301) 295# define LZ4_DEPRECATED(message) __attribute__((deprecated)) 296# elif defined(_MSC_VER) 297# define LZ4_DEPRECATED(message) __declspec(deprecated(message)) 298# else 299# pragma message("WARNING: You need to implement LZ4_DEPRECATED for this compiler") 300# define LZ4_DEPRECATED 301# endif 302#endif // LZ4_DEPRECATE_WARNING_DEFBLOCK 303 304/* Obsolete compression functions */ 305/* These functions are planned to generate warnings by r131 approximately */ 306int LZ4_compress (const char* source, char* dest, int sourceSize); 307int LZ4_compress_limitedOutput (const char* source, char* dest, int sourceSize, int maxOutputSize); 308int LZ4_compress_withState (void* state, const char* source, char* dest, int inputSize); 309int LZ4_compress_limitedOutput_withState (void* state, const char* source, char* dest, int inputSize, int maxOutputSize); 310int LZ4_compress_continue (LZ4_stream_t* LZ4_streamPtr, const char* source, char* dest, int inputSize); 311int LZ4_compress_limitedOutput_continue (LZ4_stream_t* LZ4_streamPtr, const char* source, char* dest, int inputSize, int maxOutputSize); 312 313/* Obsolete decompression functions */ 314/* These function names are completely deprecated and must no longer be used. 315 They are only provided here for compatibility with older programs. 316 - LZ4_uncompress is the same as LZ4_decompress_fast 317 - LZ4_uncompress_unknownOutputSize is the same as LZ4_decompress_safe 318 These function prototypes are now disabled; uncomment them only if you really need them. 319 It is highly recommended to stop using these prototypes and migrate to maintained ones */ 320/* int LZ4_uncompress (const char* source, char* dest, int outputSize); */ 321/* int LZ4_uncompress_unknownOutputSize (const char* source, char* dest, int isize, int maxOutputSize); */ 322 323/* Obsolete streaming functions; use new streaming interface whenever possible */ 324LZ4_DEPRECATED("use LZ4_createStream() instead") void* LZ4_create (const char* inputBuffer); 325LZ4_DEPRECATED("use LZ4_createStream() instead") int LZ4_sizeofStreamState(void); 326LZ4_DEPRECATED("use LZ4_resetStream() instead") int LZ4_resetStreamState(void* state, const char* inputBuffer); 327LZ4_DEPRECATED("use LZ4_saveDict() instead") char* LZ4_slideInputBuffer (void* state); 328 329/* Obsolete streaming decoding functions */ 330LZ4_DEPRECATED("use LZ4_decompress_safe_usingDict() instead") int LZ4_decompress_safe_withPrefix64k (const char* source, char* dest, int compressedSize, int maxOutputSize); 331LZ4_DEPRECATED("use LZ4_decompress_fast_usingDict() instead") int LZ4_decompress_fast_withPrefix64k (const char* source, char* dest, int originalSize); 332 333 334#if defined (__cplusplus) 335} 336#endif 337