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