ROOTANA
mlz4frame.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 MLZ4F_errorCode_t;
57 
58 unsigned MLZ4F_isError(MLZ4F_errorCode_t code);
59 const char* MLZ4F_getErrorName(MLZ4F_errorCode_t code); /* return error code string; useful for debugging */
60 
61 
62 /**************************************
63  * Frame compression types
64  * ************************************/
65 //#define MLZ4F_DISABLE_OBSOLETE_ENUMS
66 #ifndef MLZ4F_DISABLE_OBSOLETE_ENUMS
67 # define MLZ4F_OBSOLETE_ENUM(x) ,x
68 #else
69 # define MLZ4F_OBSOLETE_ENUM(x)
70 #endif
71 
72 typedef enum {
77  MLZ4F_max4MB=7
83 
84 typedef enum {
90 
91 typedef enum {
94  MLZ4F_OBSOLETE_ENUM(noContentChecksum = MLZ4F_noContentChecksum)
95  MLZ4F_OBSOLETE_ENUM(contentChecksumEnabled = MLZ4F_contentChecksumEnabled)
97 
98 typedef enum {
101  MLZ4F_OBSOLETE_ENUM(skippableFrame = MLZ4F_skippableFrame)
103 
104 #ifndef MLZ4F_DISABLE_OBSOLETE_ENUMS
109 #endif
110 
111 typedef struct {
112  MLZ4F_blockSizeID_t blockSizeID; /* max64KB, max256KB, max1MB, max4MB ; 0 == default */
113  MLZ4F_blockMode_t blockMode; /* blockLinked, blockIndependent ; 0 == default */
114  MLZ4F_contentChecksum_t contentChecksumFlag; /* noContentChecksum, contentChecksumEnabled ; 0 == default */
115  MLZ4F_frameType_t frameType; /* MLZ4F_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 MLZ4F_compressFrameBound(size_t srcSize, const MLZ4F_preferences_t* preferencesPtr);
132 
133 size_t MLZ4F_compressFrame(void* dstBuffer, size_t dstMaxSize, const void* srcBuffer, size_t srcSize, const MLZ4F_preferences_t* preferencesPtr);
134 /* MLZ4F_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 MLZ4F_compressFrameBound()
138  * If this condition is not respected, MLZ4F_compressFrame() will fail (result is an errorCode)
139  * The MLZ4F_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 MLZ4F_isError())
142  */
143 
144 
145 
146 /**********************************
147 * Advanced compression functions
148 **********************************/
149 typedef struct MLZ4F_cctx_s* MLZ4F_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 MLZ4F_compress(); avoid saving src content within tmp buffer as future dictionary */
153  unsigned reserved[3];
155 
156 /* Resource Management */
157 
158 #define MLZ4F_VERSION 100
161 /* MLZ4F_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 MLZ4F_createCompressionContext(), which takes as argument a version and an MLZ4F_preferences_t structure.
164  * The version provided MUST be MLZ4F_VERSION. It is intended to track potential version differences between different binaries.
165  * The function will provide a pointer to a fully allocated MLZ4F_compressionContext_t object.
166  * If the result MLZ4F_errorCode_t is not zero, there was an error during context creation.
167  * Object can release its memory using MLZ4F_freeCompressionContext();
168  */
169 
170 
171 /* Compression */
172 
173 size_t MLZ4F_compressBegin(MLZ4F_compressionContext_t cctx, void* dstBuffer, size_t dstMaxSize, const MLZ4F_preferences_t* prefsPtr);
174 /* MLZ4F_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 MLZ4F_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 MLZ4F_isError())
180  */
181 
182 size_t MLZ4F_compressBound(size_t srcSize, const MLZ4F_preferences_t* prefsPtr);
183 /* MLZ4F_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 MLZ4F_compressUpdate(MLZ4F_compressionContext_t cctx, void* dstBuffer, size_t dstMaxSize, const void* srcBuffer, size_t srcSize, const MLZ4F_compressOptions_t* cOptPtr);
191 /* MLZ4F_compressUpdate()
192  * MLZ4F_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 MLZ4F_compressBound().
195  * If this condition is not respected, MLZ4F_compress() will fail (result is an errorCode).
196  * MLZ4F_compressUpdate() doesn't guarantee error recovery, so you have to reset compression context when an error occurs.
197  * The MLZ4F_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 MLZ4F_isError())
200  */
201 
202 size_t MLZ4F_flush(MLZ4F_compressionContext_t cctx, void* dstBuffer, size_t dstMaxSize, const MLZ4F_compressOptions_t* cOptPtr);
203 /* MLZ4F_flush()
204  * Should you need to generate compressed data immediately, without waiting for the current block to be filled,
205  * you can call MLZ4_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  * MLZ4F_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 MLZ4F_isError())
211  */
212 
213 size_t MLZ4F_compressEnd(MLZ4F_compressionContext_t cctx, void* dstBuffer, size_t dstMaxSize, const MLZ4F_compressOptions_t* cOptPtr);
214 /* MLZ4F_compressEnd()
215  * When you want to properly finish the compressed frame, just call MLZ4F_compressEnd().
216  * It will flush whatever data remained within compressionContext (like MLZ4_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 MLZ4F_isError())
220  * The MLZ4F_compressOptions_t structure is optional : you can provide NULL as argument.
221  * A successful call to MLZ4F_compressEnd() makes cctx available again for next compression task.
222  */
223 
224 
225 /***********************************
226 * Decompression functions
227 ***********************************/
228 
229 typedef struct MLZ4F_dctx_s* MLZ4F_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 /* MLZ4F_createDecompressionContext() :
242  * The first thing to do is to create an MLZ4F_decompressionContext_t object, which will be used in all decompression operations.
243  * This is achieved using MLZ4F_createDecompressionContext().
244  * The version provided MUST be MLZ4F_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 MLZ4F_decompressionContext_t object.
246  * The result is an errorCode, which can be tested using MLZ4F_isError().
247  * dctx memory can be released using MLZ4F_freeDecompressionContext();
248  * The result of MLZ4F_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  MLZ4F_frameInfo_t* frameInfoPtr,
257  const void* srcBuffer, size_t* srcSizePtr);
258 /* MLZ4F_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 MLZ4F_decompress() instead.
261  * The objective is to extract frame header information, typically for allocation purposes.
262  * MLZ4F_getFrameInfo() can also be used anytime *after* starting decompression, on any valid MLZ4F_decompressionContext_t.
263  * The result is *copied* into an existing MLZ4F_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 MLZ4F_decompress() expects for next call,
266  * or an error code which can be tested using MLZ4F_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 MLZ4F_decompressOptions_t* dOptPtr);
275 /* MLZ4F_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  * MLZ4F_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 MLZ4F_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 MLZ4F_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
MLZ4F_blockSizeID_t
Definition: mlz4frame.h:72
@ MLZ4F_max64KB
Definition: mlz4frame.h:74
@ MLZ4F_max4MB
Definition: mlz4frame.h:77
@ MLZ4F_default
Definition: mlz4frame.h:73
@ MLZ4F_max256KB
Definition: mlz4frame.h:75
@ MLZ4F_max1MB
Definition: mlz4frame.h:76
size_t MLZ4F_compressEnd(MLZ4F_compressionContext_t cctx, void *dstBuffer, size_t dstMaxSize, const MLZ4F_compressOptions_t *cOptPtr)
Definition: lz4frame.cxx:714
MLZ4F_errorCode_t MLZ4F_freeDecompressionContext(MLZ4F_decompressionContext_t dctx)
Definition: lz4frame.cxx:772
size_t MLZ4F_decompress(MLZ4F_decompressionContext_t dctx, void *dstBuffer, size_t *dstSizePtr, const void *srcBuffer, size_t *srcSizePtr, const MLZ4F_decompressOptions_t *dOptPtr)
Definition: lz4frame.cxx:1044
size_t MLZ4F_errorCode_t
Definition: mlz4frame.h:56
MLZ4F_blockSizeID_t blockSizeID_t
Definition: mlz4frame.h:105
unsigned MLZ4F_isError(MLZ4F_errorCode_t code)
Definition: lz4frame.cxx:160
size_t MLZ4F_compressBound(size_t srcSize, const MLZ4F_preferences_t *prefsPtr)
Definition: lz4frame.cxx:477
struct MLZ4F_cctx_s * MLZ4F_compressionContext_t
Definition: mlz4frame.h:149
size_t MLZ4F_compressUpdate(MLZ4F_compressionContext_t cctx, void *dstBuffer, size_t dstMaxSize, const void *srcBuffer, size_t srcSize, const MLZ4F_compressOptions_t *cOptPtr)
Definition: lz4frame.cxx:562
#define MLZ4F_OBSOLETE_ENUM(x)
Definition: mlz4frame.h:67
size_t MLZ4F_compressFrame(void *dstBuffer, size_t dstMaxSize, const void *srcBuffer, size_t srcSize, const MLZ4F_preferences_t *preferencesPtr)
Definition: lz4frame.cxx:284
MLZ4F_frameType_t
Definition: mlz4frame.h:98
@ MLZ4F_frame
Definition: mlz4frame.h:99
@ MLZ4F_skippableFrame
Definition: mlz4frame.h:100
MLZ4F_errorCode_t MLZ4F_freeCompressionContext(MLZ4F_compressionContext_t cctx)
Definition: lz4frame.cxx:371
size_t MLZ4F_compressBegin(MLZ4F_compressionContext_t cctx, void *dstBuffer, size_t dstMaxSize, const MLZ4F_preferences_t *prefsPtr)
Definition: lz4frame.cxx:392
size_t MLZ4F_getFrameInfo(MLZ4F_decompressionContext_t dctx, MLZ4F_frameInfo_t *frameInfoPtr, const void *srcBuffer, size_t *srcSizePtr)
Definition: lz4frame.cxx:928
MLZ4F_errorCode_t MLZ4F_createCompressionContext(MLZ4F_compressionContext_t *cctxPtr, unsigned version)
Definition: lz4frame.cxx:355
struct MLZ4F_dctx_s * MLZ4F_decompressionContext_t
Definition: mlz4frame.h:229
size_t MLZ4F_flush(MLZ4F_compressionContext_t cctx, void *dstBuffer, size_t dstMaxSize, const MLZ4F_compressOptions_t *cOptPtr)
Definition: lz4frame.cxx:673
MLZ4F_frameType_t frameType_t
Definition: mlz4frame.h:107
MLZ4F_contentChecksum_t
Definition: mlz4frame.h:91
@ MLZ4F_contentChecksumEnabled
Definition: mlz4frame.h:93
@ MLZ4F_noContentChecksum
Definition: mlz4frame.h:92
MLZ4F_blockMode_t blockMode_t
Definition: mlz4frame.h:106
const char * MLZ4F_getErrorName(MLZ4F_errorCode_t code)
Definition: lz4frame.cxx:165
size_t MLZ4F_compressFrameBound(size_t srcSize, const MLZ4F_preferences_t *preferencesPtr)
Definition: lz4frame.cxx:256
MLZ4F_errorCode_t MLZ4F_createDecompressionContext(MLZ4F_decompressionContext_t *dctxPtr, unsigned version)
Definition: lz4frame.cxx:760
MLZ4F_contentChecksum_t contentChecksum_t
Definition: mlz4frame.h:108
MLZ4F_blockMode_t
Definition: mlz4frame.h:84
@ MLZ4F_blockIndependent
Definition: mlz4frame.h:86
@ MLZ4F_blockLinked
Definition: mlz4frame.h:85
MLZ4F_blockMode_t blockMode
Definition: mlz4frame.h:113
MLZ4F_blockSizeID_t blockSizeID
Definition: mlz4frame.h:112
unsigned long long contentSize
Definition: mlz4frame.h:116
MLZ4F_frameType_t frameType
Definition: mlz4frame.h:115
MLZ4F_contentChecksum_t contentChecksumFlag
Definition: mlz4frame.h:114
MLZ4F_frameInfo_t frameInfo
Definition: mlz4frame.h:121
unsigned autoFlush
Definition: mlz4frame.h:123