lz4hc.h revision a357f434f06de70e5d670b8669becccb09f4c7a6
1/* 2 LZ4 HC - High Compression Mode of LZ4 3 Header File 4 Copyright (C) 2011-2015, Yann Collet. 5 BSD 2-Clause License (http://www.opensource.org/licenses/bsd-license.php) 6 7 Redistribution and use in source and binary forms, with or without 8 modification, are permitted provided that the following conditions are 9 met: 10 11 * Redistributions of source code must retain the above copyright 12 notice, this list of conditions and the following disclaimer. 13 * Redistributions in binary form must reproduce the above 14 copyright notice, this list of conditions and the following disclaimer 15 in the documentation and/or other materials provided with the 16 distribution. 17 18 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 19 "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 20 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 21 A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 22 OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 23 SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 24 LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 25 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 26 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 27 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 28 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 29 30 You can contact the author at : 31 - LZ4 source repository : https://github.com/Cyan4973/lz4 32 - LZ4 public forum : https://groups.google.com/forum/#!forum/lz4c 33*/ 34#pragma once 35 36 37#if defined (__cplusplus) 38extern "C" { 39#endif 40 41 42int LZ4_compressHC (const char* source, char* dest, int inputSize); 43/* 44LZ4_compressHC : 45 return : the number of bytes in compressed buffer dest 46 or 0 if compression fails. 47 note : destination buffer must be already allocated. 48 To avoid any problem, size it to handle worst cases situations (input data not compressible) 49 Worst case size evaluation is provided by function LZ4_compressBound() (see "lz4.h") 50*/ 51 52int LZ4_compressHC_limitedOutput (const char* source, char* dest, int inputSize, int maxOutputSize); 53/* 54LZ4_compress_limitedOutput() : 55 Compress 'inputSize' bytes from 'source' into an output buffer 'dest' of maximum size 'maxOutputSize'. 56 If it cannot achieve it, compression will stop, and result of the function will be zero. 57 This function never writes outside of provided output buffer. 58 59 inputSize : Max supported value is 1 GB 60 maxOutputSize : is maximum allowed size into the destination buffer (which must be already allocated) 61 return : the number of output bytes written in buffer 'dest' 62 or 0 if compression fails. 63*/ 64 65 66int LZ4_compressHC2 (const char* source, char* dest, int inputSize, int compressionLevel); 67int LZ4_compressHC2_limitedOutput (const char* source, char* dest, int inputSize, int maxOutputSize, int compressionLevel); 68/* 69 Same functions as above, but with programmable 'compressionLevel'. 70 Recommended values are between 4 and 9, although any value between 0 and 16 will work. 71 'compressionLevel'==0 means use default 'compressionLevel' value. 72 Values above 16 behave the same as 16. 73 Equivalent variants exist for all other compression functions below. 74*/ 75 76/* Note : 77 Decompression functions are provided within LZ4 source code (see "lz4.h") (BSD license) 78*/ 79 80 81/************************************** 82* Using an external allocation 83**************************************/ 84int LZ4_sizeofStateHC(void); 85int LZ4_compressHC_withStateHC (void* state, const char* source, char* dest, int inputSize); 86int LZ4_compressHC_limitedOutput_withStateHC (void* state, const char* source, char* dest, int inputSize, int maxOutputSize); 87 88int LZ4_compressHC2_withStateHC (void* state, const char* source, char* dest, int inputSize, int compressionLevel); 89int LZ4_compressHC2_limitedOutput_withStateHC(void* state, const char* source, char* dest, int inputSize, int maxOutputSize, int compressionLevel); 90 91/* 92These functions are provided should you prefer to allocate memory for compression tables with your own allocation methods. 93To know how much memory must be allocated for the compression tables, use : 94int LZ4_sizeofStateHC(); 95 96Note that tables must be aligned for pointer (32 or 64 bits), otherwise compression will fail (return code 0). 97 98The allocated memory can be provided to the compression functions using 'void* state' parameter. 99LZ4_compress_withStateHC() and LZ4_compress_limitedOutput_withStateHC() are equivalent to previously described functions. 100They just use the externally allocated memory for state instead of allocating their own (on stack, or on heap). 101*/ 102 103 104 105/***************************** 106* Includes 107*****************************/ 108#include <stddef.h> /* size_t */ 109 110 111/************************************** 112* Experimental Streaming Functions 113**************************************/ 114#define LZ4_STREAMHCSIZE 262192 115#define LZ4_STREAMHCSIZE_SIZET (LZ4_STREAMHCSIZE / sizeof(size_t)) 116typedef struct { size_t table[LZ4_STREAMHCSIZE_SIZET]; } LZ4_streamHC_t; 117/* 118LZ4_streamHC_t 119This structure allows static allocation of LZ4 HC streaming state. 120State must then be initialized using LZ4_resetStreamHC() before first use. 121 122Static allocation should only be used with statically linked library. 123If you want to use LZ4 as a DLL, please use construction functions below, which are more future-proof. 124*/ 125 126 127LZ4_streamHC_t* LZ4_createStreamHC(void); 128int LZ4_freeStreamHC (LZ4_streamHC_t* LZ4_streamHCPtr); 129/* 130These functions create and release memory for LZ4 HC streaming state. 131Newly created states are already initialized. 132Existing state space can be re-used anytime using LZ4_resetStreamHC(). 133If you use LZ4 as a DLL, please use these functions instead of direct struct allocation, 134to avoid size mismatch between different versions. 135*/ 136 137void LZ4_resetStreamHC (LZ4_streamHC_t* LZ4_streamHCPtr, int compressionLevel); 138int LZ4_loadDictHC (LZ4_streamHC_t* LZ4_streamHCPtr, const char* dictionary, int dictSize); 139 140int LZ4_compressHC_continue (LZ4_streamHC_t* LZ4_streamHCPtr, const char* source, char* dest, int inputSize); 141int LZ4_compressHC_limitedOutput_continue (LZ4_streamHC_t* LZ4_streamHCPtr, const char* source, char* dest, int inputSize, int maxOutputSize); 142 143int LZ4_saveDictHC (LZ4_streamHC_t* LZ4_streamHCPtr, char* safeBuffer, int maxDictSize); 144 145/* 146These functions compress data in successive blocks of any size, using previous blocks as dictionary. 147One key assumption is that each previous block will remain read-accessible while compressing next block. 148 149Before starting compression, state must be properly initialized, using LZ4_resetStreamHC(). 150A first "fictional block" can then be designated as initial dictionary, using LZ4_loadDictHC() (Optional). 151 152Then, use LZ4_compressHC_continue() or LZ4_compressHC_limitedOutput_continue() to compress each successive block. 153They work like usual LZ4_compressHC() or LZ4_compressHC_limitedOutput(), but use previous memory blocks to improve compression. 154Previous memory blocks (including initial dictionary when present) must remain accessible and unmodified during compression. 155 156If, for any reason, previous data block can't be preserved in memory during next compression block, 157you must save it to a safer memory space, 158using LZ4_saveDictHC(). 159*/ 160 161 162 163/************************************** 164 * Deprecated Streaming Functions 165 * ************************************/ 166/* Note : these streaming functions follows the older model, and should no longer be used */ 167void* LZ4_createHC (const char* inputBuffer); 168char* LZ4_slideInputBufferHC (void* LZ4HC_Data); 169int LZ4_freeHC (void* LZ4HC_Data); 170 171int LZ4_compressHC2_continue (void* LZ4HC_Data, const char* source, char* dest, int inputSize, int compressionLevel); 172int LZ4_compressHC2_limitedOutput_continue (void* LZ4HC_Data, const char* source, char* dest, int inputSize, int maxOutputSize, int compressionLevel); 173 174int LZ4_sizeofStreamStateHC(void); 175int LZ4_resetStreamStateHC(void* state, const char* inputBuffer); 176 177 178#if defined (__cplusplus) 179} 180#endif 181