nlib
lz4hc.h
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 #ifndef LZ4_API
37 #ifdef NLIB_WINDLL
38 # ifdef nx_oss_lz4_EXPORTS
39 # define LZ4_API __declspec(dllexport)
40 # else
41 # define LZ4_API __declspec(dllimport)
42 # endif
43 #elif defined(__linux__) || defined(__FreeBSD__) || defined(__APPLE__)
44 # define LZ4_API __attribute__((visibility("default")))
45 #else
46 # define LZ4_API
47 #endif
48 #endif
49 
50 
51 #if defined (__cplusplus)
52 extern "C" {
53 #endif
54 
55 /*****************************
56 * Includes
57 *****************************/
58 #include <stddef.h> /* size_t */
59 
60 
61 /**************************************
62 * Block Compression
63 **************************************/
64 LZ4_API int LZ4_compress_HC (const char* src, char* dst, int srcSize, int maxDstSize, int compressionLevel);
65 /*
66 LZ4_compress_HC :
67  Destination buffer 'dst' must be already allocated.
68  Compression completion is guaranteed if 'dst' buffer is sized to handle worst circumstances (data not compressible)
69  Worst size evaluation is provided by function LZ4_compressBound() (see "lz4.h")
70  srcSize : Max supported value is LZ4_MAX_INPUT_SIZE (see "lz4.h")
71  compressionLevel : Recommended values are between 4 and 9, although any value between 0 and 16 will work.
72  0 means "use default value" (see lz4hc.c).
73  Values >16 behave the same as 16.
74  return : the number of bytes written into buffer 'dst'
75  or 0 if compression fails.
76 */
77 
78 
79 /* Note :
80  Decompression functions are provided within LZ4 source code (see "lz4.h") (BSD license)
81 */
82 
83 
84 LZ4_API int LZ4_sizeofStateHC(void);
85 LZ4_API int LZ4_compress_HC_extStateHC(void* state, const char* src, char* dst, int srcSize, int maxDstSize, int compressionLevel);
86 /*
87 LZ4_compress_HC_extStateHC() :
88  Use this function if you prefer to manually allocate memory for compression tables.
89  To know how much memory must be allocated for the compression tables, use :
90  int LZ4_sizeofStateHC();
91 
92  Allocated memory must be aligned on 8-bytes boundaries (which a normal malloc() will do properly).
93 
94  The allocated memory can then be provided to the compression functions using 'void* state' parameter.
95  LZ4_compress_HC_extStateHC() is equivalent to previously described function.
96  It just uses externally allocated memory for stateHC.
97 */
98 
99 
100 /**************************************
101 * Streaming Compression
102 **************************************/
103 #define LZ4_STREAMHCSIZE 262192
104 #define LZ4_STREAMHCSIZE_SIZET (LZ4_STREAMHCSIZE / sizeof(size_t))
105 typedef struct { size_t table[LZ4_STREAMHCSIZE_SIZET]; } LZ4_streamHC_t;
106 /*
107  LZ4_streamHC_t
108  This structure allows static allocation of LZ4 HC streaming state.
109  State must then be initialized using LZ4_resetStreamHC() before first use.
110 
111  Static allocation should only be used in combination with static linking.
112  If you want to use LZ4 as a DLL, please use construction functions below, which are future-proof.
113 */
114 
115 
116 LZ4_API LZ4_streamHC_t* LZ4_createStreamHC(void);
117 LZ4_API int LZ4_freeStreamHC(LZ4_streamHC_t* streamHCPtr);
118 /*
119  These functions create and release memory for LZ4 HC streaming state.
120  Newly created states are already initialized.
121  Existing state space can be re-used anytime using LZ4_resetStreamHC().
122  If you use LZ4 as a DLL, use these functions instead of static structure allocation,
123  to avoid size mismatch between different versions.
124 */
125 
126 LZ4_API void LZ4_resetStreamHC(LZ4_streamHC_t* streamHCPtr, int compressionLevel);
127 LZ4_API int LZ4_loadDictHC(LZ4_streamHC_t* streamHCPtr, const char* dictionary, int dictSize);
128 
129 LZ4_API int LZ4_compress_HC_continue(LZ4_streamHC_t* streamHCPtr, const char* src, char* dst, int srcSize, int maxDstSize);
130 
131 LZ4_API int LZ4_saveDictHC(LZ4_streamHC_t* streamHCPtr, char* safeBuffer, int maxDictSize);
132 
133 /*
134  These functions compress data in successive blocks of any size, using previous blocks as dictionary.
135  One key assumption is that previous blocks (up to 64 KB) remain read-accessible while compressing next blocks.
136  There is an exception for ring buffers, which can be smaller 64 KB.
137  Such case is automatically detected and correctly handled by LZ4_compress_HC_continue().
138 
139  Before starting compression, state must be properly initialized, using LZ4_resetStreamHC().
140  A first "fictional block" can then be designated as initial dictionary, using LZ4_loadDictHC() (Optional).
141 
142  Then, use LZ4_compress_HC_continue() to compress each successive block.
143  It works like LZ4_compress_HC(), but use previous memory blocks as dictionary to improve compression.
144  Previous memory blocks (including initial dictionary when present) must remain accessible and unmodified during compression.
145  As a reminder, size 'dst' buffer to handle worst cases, using LZ4_compressBound(), to ensure success of compression operation.
146 
147  If, for any reason, previous data blocks can't be preserved unmodified in memory during next compression block,
148  you must save it to a safer memory space, using LZ4_saveDictHC().
149  Return value of LZ4_saveDictHC() is the size of dictionary effectively saved into 'safeBuffer'.
150 */
151 
152 /* compression functions */
153 /* these functions are planned to trigger warning messages by r131 approximately */
154 LZ4_API int LZ4_compressHC(const char* source, char* dest, int inputSize);
155 LZ4_API int LZ4_compressHC_limitedOutput(const char* source, char* dest, int inputSize, int maxOutputSize);
156 LZ4_API int LZ4_compressHC2(const char* source, char* dest, int inputSize, int compressionLevel);
157 LZ4_API int LZ4_compressHC2_limitedOutput(const char* source, char* dest, int inputSize, int maxOutputSize, int compressionLevel);
158 LZ4_API int LZ4_compressHC_withStateHC(void* state, const char* source, char* dest, int inputSize);
159 LZ4_API int LZ4_compressHC_limitedOutput_withStateHC(void* state, const char* source, char* dest, int inputSize, int maxOutputSize);
160 LZ4_API int LZ4_compressHC2_withStateHC(void* state, const char* source, char* dest, int inputSize, int compressionLevel);
161 LZ4_API int LZ4_compressHC2_limitedOutput_withStateHC(void* state, const char* source, char* dest, int inputSize, int maxOutputSize, int compressionLevel);
162 LZ4_API int LZ4_compressHC_continue(LZ4_streamHC_t* LZ4_streamHCPtr, const char* source, char* dest, int inputSize);
163 LZ4_API int LZ4_compressHC_limitedOutput_continue(LZ4_streamHC_t* LZ4_streamHCPtr, const char* source, char* dest, int inputSize, int maxOutputSize);
164 
165 #if defined (__cplusplus)
166 }
167 #endif