• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2    LZ4 HC - High Compression Mode of LZ4
3    Header File
4    Copyright (C) 2011-2016, 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 is not an independent module, it requires lz4.h/lz4.c for proper 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    11
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` : Recommended values are between 4 and 9, although any value between 1 and LZ4HC_MAX_CLEVEL will work.
62  *                      Values >LZ4HC_MAX_CLEVEL behave the same as LZ4HC_MAX_CLEVEL.
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() will do properly).
78  */
79 LZ4LIB_API int LZ4_compress_HC_extStateHC(void* state, const char* src, char* dst, int srcSize, int maxDstSize, int compressionLevel);
80 LZ4LIB_API int LZ4_sizeofStateHC(void);
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, using LZ4_compressBound(), to ensure operation success.
117 
118   If, for any reason, previous data blocks can't be preserved unmodified in memory during next compression block,
119   you must save it to a safer memory space, using LZ4_saveDictHC().
120   Return value of LZ4_saveDictHC() is the size of dictionary effectively saved into 'safeBuffer'.
121 */
122 
123 
124 /*-******************************************
125  * !!!!!   STATIC LINKING ONLY   !!!!!
126  *******************************************/
127 
128  /*-*************************************
129  * PRIVATE DEFINITIONS :
130  * Do not use these definitions.
131  * They are exposed to allow static allocation of `LZ4_streamHC_t`.
132  * Using these definitions makes the code vulnerable to potential API break when upgrading LZ4
133  **************************************/
134 #define LZ4HC_DICTIONARY_LOGSIZE 17
135 #define LZ4HC_MAXD (1<<LZ4HC_DICTIONARY_LOGSIZE)
136 #define LZ4HC_MAXD_MASK (LZ4HC_MAXD - 1)
137 
138 #define LZ4HC_HASH_LOG 15
139 #define LZ4HC_HASHTABLESIZE (1 << LZ4HC_HASH_LOG)
140 #define LZ4HC_HASH_MASK (LZ4HC_HASHTABLESIZE - 1)
141 
142 
143 #if defined(__cplusplus) || (defined (__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L) /* C99 */)
144 #include <stdint.h>
145 
146 typedef struct
147 {
148     uint32_t   hashTable[LZ4HC_HASHTABLESIZE];
149     uint16_t   chainTable[LZ4HC_MAXD];
150     const uint8_t* end;        /* next block here to continue on current prefix */
151     const uint8_t* base;       /* All index relative to this position */
152     const uint8_t* dictBase;   /* alternate base for extDict */
153     uint8_t* inputBuffer;      /* deprecated */
154     uint32_t   dictLimit;        /* below that point, need extDict */
155     uint32_t   lowLimit;         /* below that point, no more dict */
156     uint32_t   nextToUpdate;     /* index from which to continue dictionary update */
157     uint32_t   searchNum;        /* only for optimal parser */
158     uint32_t   compressionLevel;
159 } LZ4HC_CCtx_internal;
160 
161 #else
162 
163 typedef struct
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 char* inputBuffer;      /* deprecated */
171     unsigned int   dictLimit;        /* below that point, need extDict */
172     unsigned int   lowLimit;         /* below that point, no more dict */
173     unsigned int   nextToUpdate;     /* index from which to continue dictionary update */
174     unsigned int   searchNum;        /* only for optimal parser */
175     unsigned int   compressionLevel;
176 } LZ4HC_CCtx_internal;
177 
178 #endif
179 
180 #define LZ4_STREAMHCSIZE       (4*LZ4HC_HASHTABLESIZE + 2*LZ4HC_MAXD + 56) /* 393268 */
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 /* these functions will trigger warning messages in future releases */
203 LZ4_DEPRECATED("use LZ4_compress_HC() instead") int LZ4_compressHC               (const char* source, char* dest, int inputSize);
204 LZ4_DEPRECATED("use LZ4_compress_HC() instead") int LZ4_compressHC_limitedOutput (const char* source, char* dest, int inputSize, int maxOutputSize);
205 LZ4_DEPRECATED("use LZ4_compress_HC() instead") int LZ4_compressHC2 (const char* source, char* dest, int inputSize, int compressionLevel);
206 LZ4_DEPRECATED("use LZ4_compress_HC() instead") int LZ4_compressHC2_limitedOutput (const char* source, char* dest, int inputSize, int maxOutputSize, int compressionLevel);
207 LZ4_DEPRECATED("use LZ4_compress_HC_extStateHC() instead") int LZ4_compressHC_withStateHC               (void* state, const char* source, char* dest, int inputSize);
208 LZ4_DEPRECATED("use LZ4_compress_HC_extStateHC() instead") int LZ4_compressHC_limitedOutput_withStateHC (void* state, const char* source, char* dest, int inputSize, int maxOutputSize);
209 LZ4_DEPRECATED("use LZ4_compress_HC_extStateHC() instead") int LZ4_compressHC2_withStateHC (void* state, const char* source, char* dest, int inputSize, int compressionLevel);
210 LZ4_DEPRECATED("use LZ4_compress_HC_extStateHC() instead") int LZ4_compressHC2_limitedOutput_withStateHC(void* state, const char* source, char* dest, int inputSize, int maxOutputSize, int compressionLevel);
211 LZ4_DEPRECATED("use LZ4_compress_HC_continue() instead") int LZ4_compressHC_continue               (LZ4_streamHC_t* LZ4_streamHCPtr, const char* source, char* dest, int inputSize);
212 LZ4_DEPRECATED("use LZ4_compress_HC_continue() instead") int LZ4_compressHC_limitedOutput_continue (LZ4_streamHC_t* LZ4_streamHCPtr, const char* source, char* dest, int inputSize, int maxOutputSize);
213 
214 /* Deprecated Streaming functions using older model; should no longer be used */
215 LZ4_DEPRECATED("use LZ4_createStreamHC() instead") void* LZ4_createHC (char* inputBuffer);
216 LZ4_DEPRECATED("use LZ4_saveDictHC() instead")     char* LZ4_slideInputBufferHC (void* LZ4HC_Data);
217 LZ4_DEPRECATED("use LZ4_freeStreamHC() instead")   int   LZ4_freeHC (void* LZ4HC_Data);
218 LZ4_DEPRECATED("use LZ4_compress_HC_continue() instead") int LZ4_compressHC2_continue (void* LZ4HC_Data, const char* source, char* dest, int inputSize, int compressionLevel);
219 LZ4_DEPRECATED("use LZ4_compress_HC_continue() instead") int LZ4_compressHC2_limitedOutput_continue (void* LZ4HC_Data, const char* source, char* dest, int inputSize, int maxOutputSize, int compressionLevel);
220 LZ4_DEPRECATED("use LZ4_createStreamHC() instead") int   LZ4_sizeofStreamStateHC(void);
221 LZ4_DEPRECATED("use LZ4_resetStreamHC() instead")  int   LZ4_resetStreamStateHC(void* state, char* inputBuffer);
222 
223 
224 #if defined (__cplusplus)
225 }
226 #endif
227 
228 #endif /* LZ4_HC_H_19834876238432 */
229