nlib
lz4frame.h
1 /*
2  LZ4 auto-framing library
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 
35 /* LZ4F is a stand-alone API to create LZ4-compressed frames
36  * fully conformant to specification v1.5.1.
37  * All related operations, including memory management, are handled by the library.
38  * You don't need lz4.h when using lz4frame.h.
39  * */
40 
41 #pragma once
42 
43 #ifndef LZ4_API
44 #ifdef NLIB_WINDLL
45 # ifdef nx_oss_lz4_EXPORTS
46 # define LZ4_API __declspec(dllexport)
47 # else
48 # define LZ4_API __declspec(dllimport)
49 # endif
50 #elif defined(__linux__) || defined(__FreeBSD__)
51 # define LZ4_API __attribute__((visibility("default")))
52 #else
53 # define LZ4_API
54 #endif
55 #endif
56 
57 
58 #if defined (__cplusplus)
59 extern "C" {
60 #endif
61 
62 /**************************************
63 * Includes
64 **************************************/
65 #include <stddef.h> /* size_t */
66 
67 
68 /**************************************
69  * Error management
70  * ************************************/
71 typedef size_t LZ4F_errorCode_t;
72 
73 LZ4_API unsigned LZ4F_isError(LZ4F_errorCode_t code);
74 LZ4_API const char* LZ4F_getErrorName(LZ4F_errorCode_t code); /* return error code string; useful for debugging */
75 
76 
77 /**************************************
78  * Frame compression types
79  * ************************************/
80 #define LZ4F_DISABLE_OBSOLETE_ENUMS
81 #ifndef LZ4F_DISABLE_OBSOLETE_ENUMS
82 # define LZ4F_OBSOLETE_ENUM(x) ,x
83 #else
84 # define LZ4F_OBSOLETE_ENUM(x)
85 #endif
86 
87 typedef enum {
88  LZ4F_default=0,
89  LZ4F_max64KB=4,
90  LZ4F_max256KB=5,
91  LZ4F_max1MB=6,
92  LZ4F_max4MB=7
93  LZ4F_OBSOLETE_ENUM(max64KB = LZ4F_max64KB)
94  LZ4F_OBSOLETE_ENUM(max256KB = LZ4F_max256KB)
95  LZ4F_OBSOLETE_ENUM(max1MB = LZ4F_max1MB)
96  LZ4F_OBSOLETE_ENUM(max4MB = LZ4F_max4MB)
97 } LZ4F_blockSizeID_t;
98 
99 typedef enum {
100  LZ4F_blockLinked=0,
101  LZ4F_blockIndependent
102  LZ4F_OBSOLETE_ENUM(blockLinked = LZ4F_blockLinked)
103  LZ4F_OBSOLETE_ENUM(blockIndependent = LZ4F_blockIndependent)
104 } LZ4F_blockMode_t;
105 
106 typedef enum {
107  LZ4F_noContentChecksum=0,
108  LZ4F_contentChecksumEnabled
109  LZ4F_OBSOLETE_ENUM(noContentChecksum = LZ4F_noContentChecksum)
110  LZ4F_OBSOLETE_ENUM(contentChecksumEnabled = LZ4F_contentChecksumEnabled)
111 } LZ4F_contentChecksum_t;
112 
113 typedef enum {
114  LZ4F_frame=0,
115  LZ4F_skippableFrame
116  LZ4F_OBSOLETE_ENUM(skippableFrame = LZ4F_skippableFrame)
117 } LZ4F_frameType_t;
118 
119 #ifndef LZ4F_DISABLE_OBSOLETE_ENUMS
120 typedef LZ4F_blockSizeID_t blockSizeID_t;
121 typedef LZ4F_blockMode_t blockMode_t;
122 typedef LZ4F_frameType_t frameType_t;
123 typedef LZ4F_contentChecksum_t contentChecksum_t;
124 #endif
125 
126 typedef struct {
127  LZ4F_blockSizeID_t blockSizeID; /* max64KB, max256KB, max1MB, max4MB ; 0 == default */
128  LZ4F_blockMode_t blockMode; /* blockLinked, blockIndependent ; 0 == default */
129  LZ4F_contentChecksum_t contentChecksumFlag; /* noContentChecksum, contentChecksumEnabled ; 0 == default */
130  LZ4F_frameType_t frameType; /* LZ4F_frame, skippableFrame ; 0 == default */
131  unsigned long long contentSize; /* Size of uncompressed (original) content ; 0 == unknown */
132  unsigned reserved[2]; /* must be zero for forward compatibility */
133 } LZ4F_frameInfo_t;
134 
135 typedef struct {
136  LZ4F_frameInfo_t frameInfo;
137  int compressionLevel; /* 0 == default (fast mode); values above 16 count as 16; values below 0 count as 0 */
138  unsigned autoFlush; /* 1 == always flush (reduce need for tmp buffer) */
139  unsigned reserved[4]; /* must be zero for forward compatibility */
140 } LZ4F_preferences_t;
141 
142 
143 /***********************************
144  * Simple compression function
145  * *********************************/
146 LZ4_API size_t LZ4F_compressFrameBound(size_t srcSize, const LZ4F_preferences_t* preferencesPtr);
147 
148 LZ4_API size_t LZ4F_compressFrame(void* dstBuffer, size_t dstMaxSize, const void* srcBuffer, size_t srcSize, const LZ4F_preferences_t* preferencesPtr);
149 /* LZ4F_compressFrame()
150  * Compress an entire srcBuffer into a valid LZ4 frame, as defined by specification v1.5.1
151  * The most important rule is that dstBuffer MUST be large enough (dstMaxSize) to ensure compression completion even in worst case.
152  * You can get the minimum value of dstMaxSize by using LZ4F_compressFrameBound()
153  * If this condition is not respected, LZ4F_compressFrame() will fail (result is an errorCode)
154  * The LZ4F_preferences_t structure is optional : you can provide NULL as argument. All preferences will be set to default.
155  * The result of the function is the number of bytes written into dstBuffer.
156  * The function outputs an error code if it fails (can be tested using LZ4F_isError())
157  */
158 
159 
160 
161 /**********************************
162 * Advanced compression functions
163 **********************************/
164 typedef struct LZ4F_cctx_s* LZ4F_compressionContext_t; /* must be aligned on 8-bytes */
165 
166 typedef struct {
167  unsigned stableSrc; /* 1 == src content will remain available on future calls to LZ4F_compress(); avoid saving src content within tmp buffer as future dictionary */
168  unsigned reserved[3];
169 } LZ4F_compressOptions_t;
170 
171 /* Resource Management */
172 
173 #define LZ4F_VERSION 100
174 LZ4_API LZ4F_errorCode_t LZ4F_createCompressionContext(LZ4F_compressionContext_t* cctxPtr, unsigned version);
175 LZ4_API LZ4F_errorCode_t LZ4F_freeCompressionContext(LZ4F_compressionContext_t cctx);
176 /* LZ4F_createCompressionContext() :
177  * The first thing to do is to create a compressionContext object, which will be used in all compression operations.
178  * This is achieved using LZ4F_createCompressionContext(), which takes as argument a version and an LZ4F_preferences_t structure.
179  * The version provided MUST be LZ4F_VERSION. It is intended to track potential version differences between different binaries.
180  * The function will provide a pointer to a fully allocated LZ4F_compressionContext_t object.
181  * If the result LZ4F_errorCode_t is not zero, there was an error during context creation.
182  * Object can release its memory using LZ4F_freeCompressionContext();
183  */
184 
185 
186 /* Compression */
187 
188 LZ4_API size_t LZ4F_compressBegin(LZ4F_compressionContext_t cctx, void* dstBuffer, size_t dstMaxSize, const LZ4F_preferences_t* prefsPtr);
189 /* LZ4F_compressBegin() :
190  * will write the frame header into dstBuffer.
191  * dstBuffer must be large enough to accommodate a header (dstMaxSize). Maximum header size is 15 bytes.
192  * The LZ4F_preferences_t structure is optional : you can provide NULL as argument, all preferences will then be set to default.
193  * The result of the function is the number of bytes written into dstBuffer for the header
194  * or an error code (can be tested using LZ4F_isError())
195  */
196 
197 LZ4_API size_t LZ4F_compressBound(size_t srcSize, const LZ4F_preferences_t* prefsPtr);
198 /* LZ4F_compressBound() :
199  * Provides the minimum size of Dst buffer given srcSize to handle worst case situations.
200  * Different preferences can produce different results.
201  * prefsPtr is optional : you can provide NULL as argument, all preferences will then be set to cover worst case.
202  * This function includes frame termination cost (4 bytes, or 8 if frame checksum is enabled)
203  */
204 
205 LZ4_API size_t LZ4F_compressUpdate(LZ4F_compressionContext_t cctx, void* dstBuffer, size_t dstMaxSize, const void* srcBuffer, size_t srcSize, const LZ4F_compressOptions_t* cOptPtr);
206 /* LZ4F_compressUpdate()
207  * LZ4F_compressUpdate() can be called repetitively to compress as much data as necessary.
208  * The most important rule is that dstBuffer MUST be large enough (dstMaxSize) to ensure compression completion even in worst case.
209  * You can get the minimum value of dstMaxSize by using LZ4F_compressBound().
210  * If this condition is not respected, LZ4F_compress() will fail (result is an errorCode).
211  * LZ4F_compressUpdate() doesn't guarantee error recovery, so you have to reset compression context when an error occurs.
212  * The LZ4F_compressOptions_t structure is optional : you can provide NULL as argument.
213  * The result of the function is the number of bytes written into dstBuffer : it can be zero, meaning input data was just buffered.
214  * The function outputs an error code if it fails (can be tested using LZ4F_isError())
215  */
216 
217 LZ4_API size_t LZ4F_flush(LZ4F_compressionContext_t cctx, void* dstBuffer, size_t dstMaxSize, const LZ4F_compressOptions_t* cOptPtr);
218 /* LZ4F_flush()
219  * Should you need to generate compressed data immediately, without waiting for the current block to be filled,
220  * you can call LZ4_flush(), which will immediately compress any remaining data buffered within cctx.
221  * Note that dstMaxSize must be large enough to ensure the operation will be successful.
222  * LZ4F_compressOptions_t structure is optional : you can provide NULL as argument.
223  * The result of the function is the number of bytes written into dstBuffer
224  * (it can be zero, this means there was no data left within cctx)
225  * The function outputs an error code if it fails (can be tested using LZ4F_isError())
226  */
227 
228 LZ4_API size_t LZ4F_compressEnd(LZ4F_compressionContext_t cctx, void* dstBuffer, size_t dstMaxSize, const LZ4F_compressOptions_t* cOptPtr);
229 /* LZ4F_compressEnd()
230  * When you want to properly finish the compressed frame, just call LZ4F_compressEnd().
231  * It will flush whatever data remained within compressionContext (like LZ4_flush())
232  * but also properly finalize the frame, with an endMark and a checksum.
233  * The result of the function is the number of bytes written into dstBuffer (necessarily >= 4 (endMark), or 8 if optional frame checksum is enabled)
234  * The function outputs an error code if it fails (can be tested using LZ4F_isError())
235  * The LZ4F_compressOptions_t structure is optional : you can provide NULL as argument.
236  * A successful call to LZ4F_compressEnd() makes cctx available again for next compression task.
237  */
238 
239 
240 /***********************************
241 * Decompression functions
242 ***********************************/
243 
244 typedef struct LZ4F_dctx_s* LZ4F_decompressionContext_t; /* must be aligned on 8-bytes */
245 
246 typedef struct {
247  unsigned stableDst; /* guarantee that decompressed data will still be there on next function calls (avoid storage into tmp buffers) */
248  unsigned reserved[3];
249 } LZ4F_decompressOptions_t;
250 
251 
252 /* Resource management */
253 
254 LZ4_API LZ4F_errorCode_t LZ4F_createDecompressionContext(LZ4F_decompressionContext_t* dctxPtr, unsigned version);
255 LZ4_API LZ4F_errorCode_t LZ4F_freeDecompressionContext(LZ4F_decompressionContext_t dctx);
256 /* LZ4F_createDecompressionContext() :
257  * The first thing to do is to create an LZ4F_decompressionContext_t object, which will be used in all decompression operations.
258  * This is achieved using LZ4F_createDecompressionContext().
259  * The version provided MUST be LZ4F_VERSION. It is intended to track potential breaking differences between different versions.
260  * The function will provide a pointer to a fully allocated and initialized LZ4F_decompressionContext_t object.
261  * The result is an errorCode, which can be tested using LZ4F_isError().
262  * dctx memory can be released using LZ4F_freeDecompressionContext();
263  * The result of LZ4F_freeDecompressionContext() is indicative of the current state of decompressionContext when being released.
264  * That is, it should be == 0 if decompression has been completed fully and correctly.
265  */
266 
267 
268 /* Decompression */
269 
270 LZ4_API size_t LZ4F_getFrameInfo(LZ4F_decompressionContext_t dctx,
271  LZ4F_frameInfo_t* frameInfoPtr,
272  const void* srcBuffer, size_t* srcSizePtr);
273 /* LZ4F_getFrameInfo()
274  * This function decodes frame header information (such as max blockSize, frame checksum, etc.).
275  * Its usage is optional : you can start by calling directly LZ4F_decompress() instead.
276  * The objective is to extract frame header information, typically for allocation purposes.
277  * LZ4F_getFrameInfo() can also be used anytime *after* starting decompression, on any valid LZ4F_decompressionContext_t.
278  * The result is *copied* into an existing LZ4F_frameInfo_t structure which must be already allocated.
279  * The number of bytes read from srcBuffer will be provided within *srcSizePtr (necessarily <= original value).
280  * The function result is an hint of how many srcSize bytes LZ4F_decompress() expects for next call,
281  * or an error code which can be tested using LZ4F_isError()
282  * (typically, when there is not enough src bytes to fully decode the frame header)
283  * You are expected to resume decompression from where it stopped (srcBuffer + *srcSizePtr)
284  */
285 
286 LZ4_API size_t LZ4F_decompress(LZ4F_decompressionContext_t dctx,
287  void* dstBuffer, size_t* dstSizePtr,
288  const void* srcBuffer, size_t* srcSizePtr,
289  const LZ4F_decompressOptions_t* dOptPtr);
290 /* LZ4F_decompress()
291  * Call this function repetitively to regenerate data compressed within srcBuffer.
292  * The function will attempt to decode *srcSizePtr bytes from srcBuffer, into dstBuffer of maximum size *dstSizePtr.
293  *
294  * The number of bytes regenerated into dstBuffer will be provided within *dstSizePtr (necessarily <= original value).
295  *
296  * The number of bytes read from srcBuffer will be provided within *srcSizePtr (necessarily <= original value).
297  * If number of bytes read is < number of bytes provided, then decompression operation is not completed.
298  * It typically happens when dstBuffer is not large enough to contain all decoded data.
299  * LZ4F_decompress() must be called again, starting from where it stopped (srcBuffer + *srcSizePtr)
300  * The function will check this condition, and refuse to continue if it is not respected.
301  *
302  * dstBuffer is supposed to be flushed between each call to the function, since its content will be overwritten.
303  * dst arguments can be changed at will with each consecutive call to the function.
304  *
305  * The function result is an hint of how many srcSize bytes LZ4F_decompress() expects for next call.
306  * Schematically, it's the size of the current (or remaining) compressed block + header of next block.
307  * Respecting the hint provides some boost to performance, since it does skip intermediate buffers.
308  * This is just a hint, you can always provide any srcSize you want.
309  * When a frame is fully decoded, the function result will be 0 (no more data expected).
310  * If decompression failed, function result is an error code, which can be tested using LZ4F_isError().
311  *
312  * After a frame is fully decoded, dctx can be used again to decompress another frame.
313  */
314 
315 
316 #if defined (__cplusplus)
317 }
318 #endif