1 /* 2 LZ4 HC - High Compression Mode of LZ4 3 Header File 4 Copyright (C) 2011-2017, 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/lz4/lz4 32 - LZ4 public forum : https://groups.google.com/forum/#!forum/lz4c 33 */ 34 #ifndef LZ4_HC_H_19834876238432 35 #define LZ4_HC_H_19834876238432 36 37 #if defined (__cplusplus) 38 extern "C" { 39 #endif 40 41 /* --- Dependency --- */ 42 /* note : lz4hc requires lz4.h/lz4.c for compilation */ 43 #include "lz4.h" /* stddef, LZ4LIB_API, LZ4_DEPRECATED */ 44 45 46 /* --- Useful constants --- */ 47 #define LZ4HC_CLEVEL_MIN 3 48 #define LZ4HC_CLEVEL_DEFAULT 9 49 #define LZ4HC_CLEVEL_OPT_MIN 10 50 #define LZ4HC_CLEVEL_MAX 12 51 52 53 /*-************************************ 54 * Block Compression 55 **************************************/ 56 /*! LZ4_compress_HC() : 57 * Compress data from `src` into `dst`, using the more powerful but slower "HC" algorithm. 58 * `dst` must be already allocated. 59 * Compression is guaranteed to succeed if `dstCapacity >= LZ4_compressBound(srcSize)` (see "lz4.h") 60 * Max supported `srcSize` value is LZ4_MAX_INPUT_SIZE (see "lz4.h") 61 * `compressionLevel` : any value between 1 and LZ4HC_CLEVEL_MAX will work. 62 * Values > LZ4HC_CLEVEL_MAX behave the same as LZ4HC_CLEVEL_MAX. 63 * @return : the number of bytes written into 'dst' 64 * or 0 if compression fails. 65 */ 66 LZ4LIB_API int LZ4_compress_HC (const char* src, char* dst, int srcSize, int dstCapacity, int compressionLevel); 67 68 69 /* Note : 70 * Decompression functions are provided within "lz4.h" (BSD license) 71 */ 72 73 74 /*! LZ4_compress_HC_extStateHC() : 75 * Same as LZ4_compress_HC(), but using an externally allocated memory segment for `state`. 76 * `state` size is provided by LZ4_sizeofStateHC(). 77 * Memory segment must be aligned on 8-bytes boundaries (which a normal malloc() should do properly). 78 */ 79 LZ4LIB_API int LZ4_sizeofStateHC(void); 80 LZ4LIB_API int LZ4_compress_HC_extStateHC(void* state, const char* src, char* dst, int srcSize, int maxDstSize, int compressionLevel); 81 82 83 /*-************************************ 84 * Streaming Compression 85 * Bufferless synchronous API 86 **************************************/ 87 typedef union LZ4_streamHC_u LZ4_streamHC_t; /* incomplete type (defined later) */ 88 89 /*! LZ4_createStreamHC() and LZ4_freeStreamHC() : 90 * These functions create and release memory for LZ4 HC streaming state. 91 * Newly created states are automatically initialized. 92 * Existing states can be re-used several times, using LZ4_resetStreamHC(). 93 * These methods are API and ABI stable, they can be used in combination with a DLL. 94 */ 95 LZ4LIB_API LZ4_streamHC_t* LZ4_createStreamHC(void); 96 LZ4LIB_API int LZ4_freeStreamHC (LZ4_streamHC_t* streamHCPtr); 97 98 LZ4LIB_API void LZ4_resetStreamHC (LZ4_streamHC_t* streamHCPtr, int compressionLevel); 99 LZ4LIB_API int LZ4_loadDictHC (LZ4_streamHC_t* streamHCPtr, const char* dictionary, int dictSize); 100 101 LZ4LIB_API int LZ4_compress_HC_continue (LZ4_streamHC_t* streamHCPtr, const char* src, char* dst, int srcSize, int maxDstSize); 102 103 LZ4LIB_API int LZ4_saveDictHC (LZ4_streamHC_t* streamHCPtr, char* safeBuffer, int maxDictSize); 104 105 /* 106 These functions compress data in successive blocks of any size, using previous blocks as dictionary. 107 One key assumption is that previous blocks (up to 64 KB) remain read-accessible while compressing next blocks. 108 There is an exception for ring buffers, which can be smaller than 64 KB. 109 Ring buffers scenario is automatically detected and handled by LZ4_compress_HC_continue(). 110 111 Before starting compression, state must be properly initialized, using LZ4_resetStreamHC(). 112 A first "fictional block" can then be designated as initial dictionary, using LZ4_loadDictHC() (Optional). 113 114 Then, use LZ4_compress_HC_continue() to compress each successive block. 115 Previous memory blocks (including initial dictionary when present) must remain accessible and unmodified during compression. 116 'dst' buffer should be sized to handle worst case scenarios (see LZ4_compressBound()), to ensure operation success. 117 Because in case of failure, the API does not guarantee context recovery, and context will have to be reset. 118 If `dst` buffer budget cannot be >= LZ4_compressBound(), consider using LZ4_compress_HC_continue_destSize() instead. 119 120 If, for any reason, previous data block can't be preserved unmodified in memory for next compression block, 121 you can save it to a more stable memory space, using LZ4_saveDictHC(). 122 Return value of LZ4_saveDictHC() is the size of dictionary effectively saved into 'safeBuffer'. 123 */ 124 125 126 /*-************************************************************** 127 * PRIVATE DEFINITIONS : 128 * Do not use these definitions. 129 * They are exposed to allow static allocation of `LZ4_streamHC_t`. 130 * Using these definitions makes the code vulnerable to potential API break when upgrading LZ4 131 ****************************************************************/ 132 #define LZ4HC_DICTIONARY_LOGSIZE 16 133 #define LZ4HC_MAXD (1<<LZ4HC_DICTIONARY_LOGSIZE) 134 #define LZ4HC_MAXD_MASK (LZ4HC_MAXD - 1) 135 136 #define LZ4HC_HASH_LOG 15 137 #define LZ4HC_HASHTABLESIZE (1 << LZ4HC_HASH_LOG) 138 #define LZ4HC_HASH_MASK (LZ4HC_HASHTABLESIZE - 1) 139 140 141 #if defined(__cplusplus) || (defined (__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L) /* C99 */) 142 #include <stdint.h> 143 144 typedef struct LZ4HC_CCtx_internal LZ4HC_CCtx_internal; 145 struct LZ4HC_CCtx_internal 146 { 147 uint32_t hashTable[LZ4HC_HASHTABLESIZE]; 148 uint16_t chainTable[LZ4HC_MAXD]; 149 const uint8_t* end; /* next block here to continue on current prefix */ 150 const uint8_t* base; /* All index relative to this position */ 151 const uint8_t* dictBase; /* alternate base for extDict */ 152 uint32_t dictLimit; /* below that point, need extDict */ 153 uint32_t lowLimit; /* below that point, no more dict */ 154 uint32_t nextToUpdate; /* index from which to continue dictionary update */ 155 short compressionLevel; 156 short favorDecSpeed; 157 const LZ4HC_CCtx_internal* dictCtx; 158 }; 159 160 #else 161 162 typedef struct LZ4HC_CCtx_internal LZ4HC_CCtx_internal; 163 struct LZ4HC_CCtx_internal 164 { 165 unsigned int hashTable[LZ4HC_HASHTABLESIZE]; 166 unsigned short chainTable[LZ4HC_MAXD]; 167 const unsigned char* end; /* next block here to continue on current prefix */ 168 const unsigned char* base; /* All index relative to this position */ 169 const unsigned char* dictBase; /* alternate base for extDict */ 170 unsigned int dictLimit; /* below that point, need extDict */ 171 unsigned int lowLimit; /* below that point, no more dict */ 172 unsigned int nextToUpdate; /* index from which to continue dictionary update */ 173 short compressionLevel; 174 short favorDecSpeed; 175 const LZ4HC_CCtx_internal* dictCtx; 176 }; 177 178 #endif 179 180 #define LZ4_STREAMHCSIZE (4*LZ4HC_HASHTABLESIZE + 2*LZ4HC_MAXD + 56) /* 262200 */ 181 #define LZ4_STREAMHCSIZE_SIZET (LZ4_STREAMHCSIZE / sizeof(size_t)) 182 union LZ4_streamHC_u { 183 size_t table[LZ4_STREAMHCSIZE_SIZET]; 184 LZ4HC_CCtx_internal internal_donotuse; 185 }; /* previously typedef'd to LZ4_streamHC_t */ 186 /* 187 LZ4_streamHC_t : 188 This structure allows static allocation of LZ4 HC streaming state. 189 State must be initialized using LZ4_resetStreamHC() before first use. 190 191 Static allocation shall only be used in combination with static linking. 192 When invoking LZ4 from a DLL, use create/free functions instead, which are API and ABI stable. 193 */ 194 195 196 /*-************************************ 197 * Deprecated Functions 198 **************************************/ 199 /* see lz4.h LZ4_DISABLE_DEPRECATE_WARNINGS to turn off deprecation warnings */ 200 201 /* deprecated compression functions */ 202 LZ4_DEPRECATED("use LZ4_compress_HC() instead") LZ4LIB_API int LZ4_compressHC (const char* source, char* dest, int inputSize); 203 LZ4_DEPRECATED("use LZ4_compress_HC() instead") LZ4LIB_API int LZ4_compressHC_limitedOutput (const char* source, char* dest, int inputSize, int maxOutputSize); 204 LZ4_DEPRECATED("use LZ4_compress_HC() instead") LZ4LIB_API int LZ4_compressHC2 (const char* source, char* dest, int inputSize, int compressionLevel); 205 LZ4_DEPRECATED("use LZ4_compress_HC() instead") LZ4LIB_API int LZ4_compressHC2_limitedOutput (const char* source, char* dest, int inputSize, int maxOutputSize, int compressionLevel); 206 LZ4_DEPRECATED("use LZ4_compress_HC_extStateHC() instead") LZ4LIB_API int LZ4_compressHC_withStateHC (void* state, const char* source, char* dest, int inputSize); 207 LZ4_DEPRECATED("use LZ4_compress_HC_extStateHC() instead") LZ4LIB_API int LZ4_compressHC_limitedOutput_withStateHC (void* state, const char* source, char* dest, int inputSize, int maxOutputSize); 208 LZ4_DEPRECATED("use LZ4_compress_HC_extStateHC() instead") LZ4LIB_API int LZ4_compressHC2_withStateHC (void* state, const char* source, char* dest, int inputSize, int compressionLevel); 209 LZ4_DEPRECATED("use LZ4_compress_HC_extStateHC() instead") LZ4LIB_API int LZ4_compressHC2_limitedOutput_withStateHC(void* state, const char* source, char* dest, int inputSize, int maxOutputSize, int compressionLevel); 210 LZ4_DEPRECATED("use LZ4_compress_HC_continue() instead") LZ4LIB_API int LZ4_compressHC_continue (LZ4_streamHC_t* LZ4_streamHCPtr, const char* source, char* dest, int inputSize); 211 LZ4_DEPRECATED("use LZ4_compress_HC_continue() instead") LZ4LIB_API int LZ4_compressHC_limitedOutput_continue (LZ4_streamHC_t* LZ4_streamHCPtr, const char* source, char* dest, int inputSize, int maxOutputSize); 212 213 /* Obsolete streaming functions; degraded functionality; do not use! 214 * 215 * In order to perform streaming compression, these functions depended on data 216 * that is no longer tracked in the state. They have been preserved as well as 217 * possible: using them will still produce a correct output. However, use of 218 * LZ4_slideInputBufferHC() will truncate the history of the stream, rather 219 * than preserve a window-sized chunk of history. 220 */ 221 LZ4_DEPRECATED("use LZ4_createStreamHC() instead") LZ4LIB_API void* LZ4_createHC (const char* inputBuffer); 222 LZ4_DEPRECATED("use LZ4_saveDictHC() instead") LZ4LIB_API char* LZ4_slideInputBufferHC (void* LZ4HC_Data); 223 LZ4_DEPRECATED("use LZ4_freeStreamHC() instead") LZ4LIB_API int LZ4_freeHC (void* LZ4HC_Data); 224 LZ4_DEPRECATED("use LZ4_compress_HC_continue() instead") LZ4LIB_API int LZ4_compressHC2_continue (void* LZ4HC_Data, const char* source, char* dest, int inputSize, int compressionLevel); 225 LZ4_DEPRECATED("use LZ4_compress_HC_continue() instead") LZ4LIB_API int LZ4_compressHC2_limitedOutput_continue (void* LZ4HC_Data, const char* source, char* dest, int inputSize, int maxOutputSize, int compressionLevel); 226 LZ4_DEPRECATED("use LZ4_createStreamHC() instead") LZ4LIB_API int LZ4_sizeofStreamStateHC(void); 227 LZ4_DEPRECATED("use LZ4_resetStreamHC() instead") LZ4LIB_API int LZ4_resetStreamStateHC(void* state, char* inputBuffer); 228 229 230 #if defined (__cplusplus) 231 } 232 #endif 233 234 #endif /* LZ4_HC_H_19834876238432 */ 235 236 237 /*-************************************************** 238 * !!!!! STATIC LINKING ONLY !!!!! 239 * Following definitions are considered experimental. 240 * They should not be linked from DLL, 241 * as there is no guarantee of API stability yet. 242 * Prototypes will be promoted to "stable" status 243 * after successfull usage in real-life scenarios. 244 ***************************************************/ 245 #ifdef LZ4_HC_STATIC_LINKING_ONLY /* protection macro */ 246 #ifndef LZ4_HC_SLO_098092834 247 #define LZ4_HC_SLO_098092834 248 249 #if defined (__cplusplus) 250 extern "C" { 251 #endif 252 253 /*! LZ4_compress_HC_destSize() : v1.8.0 (experimental) 254 * Will try to compress as much data from `src` as possible 255 * that can fit into `targetDstSize` budget. 256 * Result is provided in 2 parts : 257 * @return : the number of bytes written into 'dst' 258 * or 0 if compression fails. 259 * `srcSizePtr` : value will be updated to indicate how much bytes were read from `src` 260 */ 261 int LZ4_compress_HC_destSize(void* LZ4HC_Data, 262 const char* src, char* dst, 263 int* srcSizePtr, int targetDstSize, 264 int compressionLevel); 265 266 /*! LZ4_compress_HC_continue_destSize() : v1.8.0 (experimental) 267 * Similar as LZ4_compress_HC_continue(), 268 * but will read a variable nb of bytes from `src` 269 * to fit into `targetDstSize` budget. 270 * Result is provided in 2 parts : 271 * @return : the number of bytes written into 'dst' 272 * or 0 if compression fails. 273 * `srcSizePtr` : value will be updated to indicate how much bytes were read from `src`. 274 */ 275 int LZ4_compress_HC_continue_destSize(LZ4_streamHC_t* LZ4_streamHCPtr, 276 const char* src, char* dst, 277 int* srcSizePtr, int targetDstSize); 278 279 /*! LZ4_setCompressionLevel() : v1.8.0 (experimental) 280 * It's possible to change compression level between 2 invocations of LZ4_compress_HC_continue*() 281 */ 282 void LZ4_setCompressionLevel(LZ4_streamHC_t* LZ4_streamHCPtr, int compressionLevel); 283 284 /*! LZ4_favorDecompressionSpeed() : v1.8.2 (experimental) 285 * Parser will select decisions favoring decompression over compression ratio. 286 * Only work at highest compression settings (level >= LZ4HC_CLEVEL_OPT_MIN) 287 */ 288 void LZ4_favorDecompressionSpeed(LZ4_streamHC_t* LZ4_streamHCPtr, int favor); 289 290 /*! LZ4_resetStreamHC_fast() : 291 * When an LZ4_streamHC_t is known to be in a internally coherent state, 292 * it can often be prepared for a new compression with almost no work, only 293 * sometimes falling back to the full, expensive reset that is always required 294 * when the stream is in an indeterminate state (i.e., the reset performed by 295 * LZ4_resetStreamHC()). 296 * 297 * LZ4_streamHCs are guaranteed to be in a valid state when: 298 * - returned from LZ4_createStreamHC() 299 * - reset by LZ4_resetStreamHC() 300 * - memset(stream, 0, sizeof(LZ4_streamHC_t)) 301 * - the stream was in a valid state and was reset by LZ4_resetStreamHC_fast() 302 * - the stream was in a valid state and was then used in any compression call 303 * that returned success 304 * - the stream was in an indeterminate state and was used in a compression 305 * call that fully reset the state (LZ4_compress_HC_extStateHC()) and that 306 * returned success 307 */ 308 void LZ4_resetStreamHC_fast(LZ4_streamHC_t* LZ4_streamHCPtr, int compressionLevel); 309 310 /*! LZ4_compress_HC_extStateHC_fastReset() : 311 * A variant of LZ4_compress_HC_extStateHC(). 312 * 313 * Using this variant avoids an expensive initialization step. It is only safe 314 * to call if the state buffer is known to be correctly initialized already 315 * (see above comment on LZ4_resetStreamHC_fast() for a definition of 316 * "correctly initialized"). From a high level, the difference is that this 317 * function initializes the provided state with a call to 318 * LZ4_resetStreamHC_fast() while LZ4_compress_HC_extStateHC() starts with a 319 * call to LZ4_resetStreamHC(). 320 */ 321 int LZ4_compress_HC_extStateHC_fastReset (void* state, const char* src, char* dst, int srcSize, int dstCapacity, int compressionLevel); 322 323 /*! LZ4_attach_HC_dictionary() : 324 * This is an experimental API that allows for the efficient use of a 325 * static dictionary many times. 326 * 327 * Rather than re-loading the dictionary buffer into a working context before 328 * each compression, or copying a pre-loaded dictionary's LZ4_streamHC_t into a 329 * working LZ4_streamHC_t, this function introduces a no-copy setup mechanism, 330 * in which the working stream references the dictionary stream in-place. 331 * 332 * Several assumptions are made about the state of the dictionary stream. 333 * Currently, only streams which have been prepared by LZ4_loadDictHC() should 334 * be expected to work. 335 * 336 * Alternatively, the provided dictionary stream pointer may be NULL, in which 337 * case any existing dictionary stream is unset. 338 * 339 * A dictionary should only be attached to a stream without any history (i.e., 340 * a stream that has just been reset). 341 * 342 * The dictionary will remain attached to the working stream only for the 343 * current stream session. Calls to LZ4_resetStreamHC(_fast) will remove the 344 * dictionary context association from the working stream. The dictionary 345 * stream (and source buffer) must remain in-place / accessible / unchanged 346 * through the lifetime of the stream session. 347 */ 348 LZ4LIB_API void LZ4_attach_HC_dictionary(LZ4_streamHC_t *working_stream, const LZ4_streamHC_t *dictionary_stream); 349 350 #if defined (__cplusplus) 351 } 352 #endif 353 354 #endif /* LZ4_HC_SLO_098092834 */ 355 #endif /* LZ4_HC_STATIC_LINKING_ONLY */ 356