1 /*
2    LZ4 - Fast LZ compression algorithm
3    Header File
4    Copyright (C) 2011-2013, 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 homepage : http://fastcompression.blogspot.com/p/lz4.html
32    - LZ4 source repository : http://code.google.com/p/lz4/
33 */
34 #pragma once
35 
36 #if defined (__cplusplus)
37 extern "C" {
38 #endif
39 
40 
41 /**************************************
42    Version
43 **************************************/
44 #define LZ4_VERSION_MAJOR    1    /* for major interface/format changes  */
45 #define LZ4_VERSION_MINOR    1    /* for minor interface/format changes  */
46 #define LZ4_VERSION_RELEASE  3    /* for tweaks, bug-fixes, or development */
47 
48 
49 /**************************************
50    Compiler Options
51 **************************************/
52 #if (defined(__GNUC__) && defined(__STRICT_ANSI__)) || (defined(_MSC_VER) && !defined(__cplusplus))   /* Visual Studio */
53 #  define inline __inline           /* Visual C is not C99, but supports some kind of inline */
54 #endif
55 
56 
57 /**************************************
58    Simple Functions
59 **************************************/
60 
61 int LZ4_compress        (const char* source, char* dest, int inputSize);
62 int LZ4_decompress_safe (const char* source, char* dest, int inputSize, int maxOutputSize);
63 
64 /*
65 LZ4_compress() :
66     Compresses 'inputSize' bytes from 'source' into 'dest'.
67     Destination buffer must be already allocated,
68     and must be sized to handle worst cases situations (input data not compressible)
69     Worst case size evaluation is provided by function LZ4_compressBound()
70     inputSize : Max supported value is LZ4_MAX_INPUT_VALUE
71     return : the number of bytes written in buffer dest
72              or 0 if the compression fails
73 
74 LZ4_decompress_safe() :
75     maxOutputSize : is the size of the destination buffer (which must be already allocated)
76     return : the number of bytes decoded in the destination buffer (necessarily <= maxOutputSize)
77              If the source stream is detected malformed, the function will stop decoding and return a negative result.
78              This function is protected against buffer overflow exploits (never writes outside of output buffer, and never reads outside of input buffer). Therefore, it is protected against malicious data packets
79 */
80 
81 
82 /**************************************
83    Advanced Functions
84 **************************************/
85 #define LZ4_MAX_INPUT_SIZE        0x7E000000   /* 2 113 929 216 bytes */
86 #define LZ4_COMPRESSBOUND(isize)  ((unsigned int)(isize) > (unsigned int)LZ4_MAX_INPUT_SIZE ? 0 : (isize) + ((isize)/255) + 16)
87 
88 /*
89 LZ4_compressBound() :
90     Provides the maximum size that LZ4 may output in a "worst case" scenario (input data not compressible)
91     primarily useful for memory allocation of output buffer.
92     inline function is recommended for the general case,
93     macro is also provided when result needs to be evaluated at compilation (such as stack memory allocation).
94 
95     isize  : is the input size. Max supported value is LZ4_MAX_INPUT_SIZE
96     return : maximum output size in a "worst case" scenario
97              or 0, if input size is too large ( > LZ4_MAX_INPUT_SIZE)
98 */
99 int LZ4_compressBound(int isize);
100 
101 
102 /*
103 LZ4_compress_limitedOutput() :
104     Compress 'inputSize' bytes from 'source' into an output buffer 'dest' of maximum size 'maxOutputSize'.
105     If it cannot achieve it, compression will stop, and result of the function will be zero.
106     This function never writes outside of provided output buffer.
107 
108     inputSize  : Max supported value is LZ4_MAX_INPUT_VALUE
109     maxOutputSize : is the size of the destination buffer (which must be already allocated)
110     return : the number of bytes written in buffer 'dest'
111              or 0 if the compression fails
112 */
113 int LZ4_compress_limitedOutput (const char* source, char* dest, int inputSize, int maxOutputSize);
114 
115 
116 /*
117 LZ4_decompress_fast() :
118     originalSize : is the original and therefore uncompressed size
119     return : the number of bytes read from the source buffer (in other words, the compressed size)
120              If the source stream is malformed, the function will stop decoding and return a negative result.
121     note : This function is a bit faster than LZ4_decompress_safe()
122            This function never writes outside of output buffers, but may read beyond input buffer in case of malicious data packet.
123            Use this function preferably into a trusted environment (data to decode comes from a trusted source).
124            Destination buffer must be already allocated. Its size must be a minimum of 'outputSize' bytes.
125 */
126 int LZ4_decompress_fast (const char* source, char* dest, int originalSize);
127 
128 
129 /*
130 LZ4_decompress_safe_partial() :
131     This function decompress a compressed block of size 'inputSize' at position 'source'
132     into output buffer 'dest' of size 'maxOutputSize'.
133     The function tries to stop decompressing operation as soon as 'targetOutputSize' has been reached,
134     reducing decompression time.
135     return : the number of bytes decoded in the destination buffer (necessarily <= maxOutputSize)
136        Note : this number can be < 'targetOutputSize' should the compressed block to decode be smaller.
137              Always control how many bytes were decoded.
138              If the source stream is detected malformed, the function will stop decoding and return a negative result.
139              This function never writes outside of output buffer, and never reads outside of input buffer. It is therefore protected against malicious data packets
140 */
141 int LZ4_decompress_safe_partial (const char* source, char* dest, int inputSize, int targetOutputSize, int maxOutputSize);
142 
143 
144 /*
145 These functions are provided should you prefer to allocate memory for compression tables with your own allocation methods.
146 To know how much memory must be allocated for the compression tables, use :
147 int LZ4_sizeofState();
148 
149 Note that tables must be aligned on 4-bytes boundaries, otherwise compression will fail (return code 0).
150 
151 The allocated memory can be provided to the compressions functions using 'void* state' parameter.
152 LZ4_compress_withState() and LZ4_compress_limitedOutput_withState() are equivalent to previously described functions.
153 They just use the externally allocated memory area instead of allocating their own (on stack, or on heap).
154 */
155 int LZ4_sizeofState(void);
156 int LZ4_compress_withState               (void* state, const char* source, char* dest, int inputSize);
157 int LZ4_compress_limitedOutput_withState (void* state, const char* source, char* dest, int inputSize, int maxOutputSize);
158 
159 
160 /**************************************
161    Streaming Functions
162 **************************************/
163 void* LZ4_create (const char* inputBuffer);
164 int   LZ4_compress_continue (void* LZ4_Data, const char* source, char* dest, int inputSize);
165 int   LZ4_compress_limitedOutput_continue (void* LZ4_Data, const char* source, char* dest, int inputSize, int maxOutputSize);
166 char* LZ4_slideInputBuffer (void* LZ4_Data);
167 int   LZ4_free (void* LZ4_Data);
168 
169 /*
170 These functions allow the compression of dependent blocks, where each block benefits from prior 64 KB within preceding blocks.
171 In order to achieve this, it is necessary to start creating the LZ4 Data Structure, thanks to the function :
172 
173 void* LZ4_create (const char* inputBuffer);
174 The result of the function is the (void*) pointer on the LZ4 Data Structure.
175 This pointer will be needed in all other functions.
176 If the pointer returned is NULL, then the allocation has failed, and compression must be aborted.
177 The only parameter 'const char* inputBuffer' must, obviously, point at the beginning of input buffer.
178 The input buffer must be already allocated, and size at least 192KB.
179 'inputBuffer' will also be the 'const char* source' of the first block.
180 
181 All blocks are expected to lay next to each other within the input buffer, starting from 'inputBuffer'.
182 To compress each block, use either LZ4_compress_continue() or LZ4_compress_limitedOutput_continue().
183 Their behavior are identical to LZ4_compress() or LZ4_compress_limitedOutput(),
184 but require the LZ4 Data Structure as their first argument, and check that each block starts right after the previous one.
185 If next block does not begin immediately after the previous one, the compression will fail (return 0).
186 
187 When it's no longer possible to lay the next block after the previous one (not enough space left into input buffer), a call to :
188 char* LZ4_slideInputBuffer(void* LZ4_Data);
189 must be performed. It will typically copy the latest 64KB of input at the beginning of input buffer.
190 Note that, for this function to work properly, minimum size of an input buffer must be 192KB.
191 ==> The memory position where the next input data block must start is provided as the result of the function.
192 
193 Compression can then resume, using LZ4_compress_continue() or LZ4_compress_limitedOutput_continue(), as usual.
194 
195 When compression is completed, a call to LZ4_free() will release the memory used by the LZ4 Data Structure.
196 */
197 
198 
199 int LZ4_sizeofStreamState(void);
200 int LZ4_resetStreamState(void* state, const char* inputBuffer);
201 
202 /*
203 These functions achieve the same result as :
204 void* LZ4_create (const char* inputBuffer);
205 
206 They are provided here to allow the user program to allocate memory using its own routines.
207 
208 To know how much space must be allocated, use LZ4_sizeofStreamState();
209 Note also that space must be 4-bytes aligned.
210 
211 Once space is allocated, you must initialize it using : LZ4_resetStreamState(void* state, const char* inputBuffer);
212 void* state is a pointer to the space allocated.
213 It must be aligned on 4-bytes boundaries, and be large enough.
214 The parameter 'const char* inputBuffer' must, obviously, point at the beginning of input buffer.
215 The input buffer must be already allocated, and size at least 192KB.
216 'inputBuffer' will also be the 'const char* source' of the first block.
217 
218 The same space can be re-used multiple times, just by initializing it each time with LZ4_resetStreamState().
219 return value of LZ4_resetStreamState() must be 0 is OK.
220 Any other value means there was an error (typically, pointer is not aligned on 4-bytes boundaries).
221 */
222 
223 
224 int LZ4_decompress_safe_withPrefix64k (const char* source, char* dest, int inputSize, int maxOutputSize);
225 int LZ4_decompress_fast_withPrefix64k (const char* source, char* dest, int outputSize);
226 
227 /*
228 *_withPrefix64k() :
229     These decoding functions work the same as their "normal name" versions,
230     but can use up to 64KB of data in front of 'char* dest'.
231     These functions are necessary to decode inter-dependant blocks.
232 */
233 
234 
235 /**************************************
236    Obsolete Functions
237 **************************************/
238 /*
239 These functions are deprecated and should no longer be used.
240 They are provided here for compatibility with existing user programs.
241 */
242 int LZ4_uncompress (const char* source, char* dest, int outputSize);
243 int LZ4_uncompress_unknownOutputSize (const char* source, char* dest, int isize, int maxOutputSize);
244 
245 
246 #if defined (__cplusplus)
247 }
248 #endif
249