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