michael@0: /* michael@0: LZ4 - Fast LZ compression algorithm michael@0: Header File michael@0: Copyright (C) 2011-2014, Yann Collet. michael@0: BSD 2-Clause License (http://www.opensource.org/licenses/bsd-license.php) michael@0: michael@0: Redistribution and use in source and binary forms, with or without michael@0: modification, are permitted provided that the following conditions are michael@0: met: michael@0: michael@0: * Redistributions of source code must retain the above copyright michael@0: notice, this list of conditions and the following disclaimer. michael@0: * Redistributions in binary form must reproduce the above michael@0: copyright notice, this list of conditions and the following disclaimer michael@0: in the documentation and/or other materials provided with the michael@0: distribution. michael@0: michael@0: THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS michael@0: "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT michael@0: LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR michael@0: A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT michael@0: OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, michael@0: SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT michael@0: LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, michael@0: DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY michael@0: THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT michael@0: (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE michael@0: OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. michael@0: michael@0: You can contact the author at : michael@0: - LZ4 source repository : http://code.google.com/p/lz4/ michael@0: - LZ4 public forum : https://groups.google.com/forum/#!forum/lz4c michael@0: */ michael@0: #pragma once michael@0: michael@0: #if defined (__cplusplus) michael@0: extern "C" { michael@0: #endif michael@0: michael@0: michael@0: /************************************** michael@0: Version michael@0: **************************************/ michael@0: #define LZ4_VERSION_MAJOR 1 /* for major interface/format changes */ michael@0: #define LZ4_VERSION_MINOR 2 /* for minor interface/format changes */ michael@0: #define LZ4_VERSION_RELEASE 0 /* for tweaks, bug-fixes, or development */ michael@0: michael@0: michael@0: /************************************** michael@0: Tuning parameter michael@0: **************************************/ michael@0: /* michael@0: * LZ4_MEMORY_USAGE : michael@0: * Memory usage formula : N->2^N Bytes (examples : 10 -> 1KB; 12 -> 4KB ; 16 -> 64KB; 20 -> 1MB; etc.) michael@0: * Increasing memory usage improves compression ratio michael@0: * Reduced memory usage can improve speed, due to cache effect michael@0: * Default value is 14, for 16KB, which nicely fits into Intel x86 L1 cache michael@0: */ michael@0: #define LZ4_MEMORY_USAGE 14 michael@0: michael@0: michael@0: /************************************** michael@0: Simple Functions michael@0: **************************************/ michael@0: michael@0: int LZ4_compress (const char* source, char* dest, int inputSize); michael@0: int LZ4_decompress_safe (const char* source, char* dest, int compressedSize, int maxOutputSize); michael@0: michael@0: /* michael@0: LZ4_compress() : michael@0: Compresses 'inputSize' bytes from 'source' into 'dest'. michael@0: Destination buffer must be already allocated, michael@0: and must be sized to handle worst cases situations (input data not compressible) michael@0: Worst case size evaluation is provided by function LZ4_compressBound() michael@0: inputSize : Max supported value is LZ4_MAX_INPUT_VALUE michael@0: return : the number of bytes written in buffer dest michael@0: or 0 if the compression fails michael@0: michael@0: LZ4_decompress_safe() : michael@0: compressedSize : is obviously the source size michael@0: maxOutputSize : is the size of the destination buffer, which must be already allocated. michael@0: return : the number of bytes decoded in the destination buffer (necessarily <= maxOutputSize) michael@0: If the destination buffer is not large enough, decoding will stop and output an error code (<0). michael@0: If the source stream is detected malformed, the function will stop decoding and return a negative result. michael@0: This function is protected against buffer overflow exploits : michael@0: it never writes outside of output buffer, and never reads outside of input buffer. michael@0: Therefore, it is protected against malicious data packets. michael@0: */ michael@0: michael@0: michael@0: /* michael@0: Note : michael@0: Should you prefer to explicitly allocate compression-table memory using your own allocation method, michael@0: use the streaming functions provided below, simply reset the memory area between each call to LZ4_compress_continue() michael@0: */ michael@0: michael@0: michael@0: /************************************** michael@0: Advanced Functions michael@0: **************************************/ michael@0: #define LZ4_MAX_INPUT_SIZE 0x7E000000 /* 2 113 929 216 bytes */ michael@0: #define LZ4_COMPRESSBOUND(isize) ((unsigned int)(isize) > (unsigned int)LZ4_MAX_INPUT_SIZE ? 0 : (isize) + ((isize)/255) + 16) michael@0: michael@0: /* michael@0: LZ4_compressBound() : michael@0: Provides the maximum size that LZ4 may output in a "worst case" scenario (input data not compressible) michael@0: primarily useful for memory allocation of output buffer. michael@0: macro is also provided when result needs to be evaluated at compilation (such as stack memory allocation). michael@0: michael@0: isize : is the input size. Max supported value is LZ4_MAX_INPUT_SIZE michael@0: return : maximum output size in a "worst case" scenario michael@0: or 0, if input size is too large ( > LZ4_MAX_INPUT_SIZE) michael@0: */ michael@0: int LZ4_compressBound(int isize); michael@0: michael@0: michael@0: /* michael@0: LZ4_compress_limitedOutput() : michael@0: Compress 'inputSize' bytes from 'source' into an output buffer 'dest' of maximum size 'maxOutputSize'. michael@0: If it cannot achieve it, compression will stop, and result of the function will be zero. michael@0: This function never writes outside of provided output buffer. michael@0: michael@0: inputSize : Max supported value is LZ4_MAX_INPUT_VALUE michael@0: maxOutputSize : is the size of the destination buffer (which must be already allocated) michael@0: return : the number of bytes written in buffer 'dest' michael@0: or 0 if the compression fails michael@0: */ michael@0: int LZ4_compress_limitedOutput (const char* source, char* dest, int inputSize, int maxOutputSize); michael@0: michael@0: michael@0: /* michael@0: LZ4_decompress_fast() : michael@0: originalSize : is the original and therefore uncompressed size michael@0: return : the number of bytes read from the source buffer (in other words, the compressed size) michael@0: If the source stream is malformed, the function will stop decoding and return a negative result. michael@0: Destination buffer must be already allocated. Its size must be a minimum of 'originalSize' bytes. michael@0: note : This function is a bit faster than LZ4_decompress_safe() michael@0: It provides fast decompression and fully respect memory boundaries for properly formed compressed data. michael@0: It does not provide full protection against intentionnally modified data stream. michael@0: Use this function in a trusted environment (data to decode comes from a trusted source). michael@0: */ michael@0: int LZ4_decompress_fast (const char* source, char* dest, int originalSize); michael@0: michael@0: michael@0: /* michael@0: LZ4_decompress_safe_partial() : michael@0: This function decompress a compressed block of size 'compressedSize' at position 'source' michael@0: into output buffer 'dest' of size 'maxOutputSize'. michael@0: The function tries to stop decompressing operation as soon as 'targetOutputSize' has been reached, michael@0: reducing decompression time. michael@0: return : the number of bytes decoded in the destination buffer (necessarily <= maxOutputSize) michael@0: Note : this number can be < 'targetOutputSize' should the compressed block to decode be smaller. michael@0: Always control how many bytes were decoded. michael@0: If the source stream is detected malformed, the function will stop decoding and return a negative result. michael@0: This function never writes outside of output buffer, and never reads outside of input buffer. It is therefore protected against malicious data packets michael@0: */ michael@0: int LZ4_decompress_safe_partial (const char* source, char* dest, int compressedSize, int targetOutputSize, int maxOutputSize); michael@0: michael@0: michael@0: /*********************************************** michael@0: Experimental Streaming Compression Functions michael@0: ***********************************************/ michael@0: michael@0: #define LZ4_STREAMSIZE_U32 ((1 << (LZ4_MEMORY_USAGE-2)) + 8) michael@0: #define LZ4_STREAMSIZE (LZ4_STREAMSIZE_U32 * sizeof(unsigned int)) michael@0: /* michael@0: * LZ4_stream_t michael@0: * information structure to track an LZ4 stream. michael@0: * important : set this structure content to zero before first use ! michael@0: */ michael@0: typedef struct { unsigned int table[LZ4_STREAMSIZE_U32]; } LZ4_stream_t; michael@0: michael@0: /* michael@0: * If you prefer dynamic allocation methods, michael@0: * LZ4_createStream michael@0: * provides a pointer (void*) towards an initialized LZ4_stream_t structure. michael@0: * LZ4_free just frees it. michael@0: */ michael@0: void* LZ4_createStream(); michael@0: int LZ4_free (void* LZ4_stream); michael@0: michael@0: michael@0: /* michael@0: * LZ4_loadDict michael@0: * Use this function to load a static dictionary into LZ4_stream. michael@0: * Any previous data will be forgotten, only 'dictionary' will remain in memory. michael@0: * Loading a size of 0 is allowed (same effect as init). michael@0: * Return : 1 if OK, 0 if error michael@0: */ michael@0: int LZ4_loadDict (void* LZ4_stream, const char* dictionary, int dictSize); michael@0: michael@0: /* michael@0: * LZ4_compress_continue michael@0: * Compress data block 'source', using blocks compressed before as dictionary to improve compression ratio michael@0: * Previous data blocks are assumed to still be present at their previous location. michael@0: */ michael@0: int LZ4_compress_continue (void* LZ4_stream, const char* source, char* dest, int inputSize); michael@0: michael@0: /* michael@0: * LZ4_compress_limitedOutput_continue michael@0: * Same as before, but also specify a maximum target compressed size (maxOutputSize) michael@0: * If objective cannot be met, compression exits, and returns a zero. michael@0: */ michael@0: int LZ4_compress_limitedOutput_continue (void* LZ4_stream, const char* source, char* dest, int inputSize, int maxOutputSize); michael@0: michael@0: /* michael@0: * LZ4_saveDict michael@0: * If previously compressed data block is not guaranteed to remain at its previous memory location michael@0: * save it into a safe place (char* safeBuffer) michael@0: * Note : you don't need to call LZ4_loadDict() afterwards, michael@0: * dictionary is immediately usable, you can therefore call again LZ4_compress_continue() michael@0: * Return : 1 if OK, 0 if error michael@0: * Note : any dictSize > 64 KB will be interpreted as 64KB. michael@0: */ michael@0: int LZ4_saveDict (void* LZ4_stream, char* safeBuffer, int dictSize); michael@0: michael@0: michael@0: /************************************************ michael@0: Experimental Streaming Decompression Functions michael@0: ************************************************/ michael@0: michael@0: #define LZ4_STREAMDECODESIZE_U32 4 michael@0: #define LZ4_STREAMDECODESIZE (LZ4_STREAMDECODESIZE_U32 * sizeof(unsigned int)) michael@0: /* michael@0: * LZ4_streamDecode_t michael@0: * information structure to track an LZ4 stream. michael@0: * important : set this structure content to zero before first use ! michael@0: */ michael@0: typedef struct { unsigned int table[LZ4_STREAMDECODESIZE_U32]; } LZ4_streamDecode_t; michael@0: michael@0: /* michael@0: * If you prefer dynamic allocation methods, michael@0: * LZ4_createStreamDecode() michael@0: * provides a pointer (void*) towards an initialized LZ4_streamDecode_t structure. michael@0: * LZ4_free just frees it. michael@0: */ michael@0: void* LZ4_createStreamDecode(); michael@0: int LZ4_free (void* LZ4_stream); /* yes, it's the same one as for compression */ michael@0: michael@0: /* michael@0: *_continue() : michael@0: These decoding functions allow decompression of multiple blocks in "streaming" mode. michael@0: Previously decoded blocks must still be available at the memory position where they were decoded. michael@0: If it's not possible, save the relevant part of decoded data into a safe buffer, michael@0: and indicate where it stands using LZ4_setDictDecode() michael@0: */ michael@0: int LZ4_decompress_safe_continue (void* LZ4_streamDecode, const char* source, char* dest, int compressedSize, int maxOutputSize); michael@0: int LZ4_decompress_fast_continue (void* LZ4_streamDecode, const char* source, char* dest, int originalSize); michael@0: michael@0: /* michael@0: * LZ4_setDictDecode michael@0: * Use this function to instruct where to find the dictionary. michael@0: * This function can be used to specify a static dictionary, michael@0: * or to instruct where to find some previously decoded data saved into a different memory space. michael@0: * Setting a size of 0 is allowed (same effect as no dictionary). michael@0: * Return : 1 if OK, 0 if error michael@0: */ michael@0: int LZ4_setDictDecode (void* LZ4_streamDecode, const char* dictionary, int dictSize); michael@0: michael@0: michael@0: /* michael@0: Advanced decoding functions : michael@0: *_usingDict() : michael@0: These decoding functions work the same as michael@0: a combination of LZ4_setDictDecode() followed by LZ4_decompress_x_continue() michael@0: all together into a single function call. michael@0: It doesn't use nor update an LZ4_streamDecode_t structure. michael@0: */ michael@0: int LZ4_decompress_safe_usingDict (const char* source, char* dest, int compressedSize, int maxOutputSize, const char* dictStart, int dictSize); michael@0: int LZ4_decompress_fast_usingDict (const char* source, char* dest, int originalSize, const char* dictStart, int dictSize); michael@0: michael@0: michael@0: michael@0: #if defined (__cplusplus) michael@0: } michael@0: #endif