1/*
2 LZ4 auto-framing library
3 Header File
4 Copyright (C) 2011-2017, 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/lz4/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 * conformant with specification v1.5.1.
37 * It also offers streaming capabilities.
38 * lz4.h is not required when using lz4frame.h.
39 * */
40
41#ifndef LZ4F_H_09782039843
42#define LZ4F_H_09782039843
43
44#if defined (__cplusplus)
45extern "C" {
46#endif
47
48/* --- Dependency --- */
49#include <stddef.h> /* size_t */
50
51
52/**
53 Introduction
54
55 lz4frame.h implements LZ4 frame specification (doc/lz4_Frame_format.md).
56 lz4frame.h provides frame compression functions that take care
57 of encoding standard metadata alongside LZ4-compressed blocks.
58*/
59
60/*-***************************************************************
61 * Compiler specifics
62 *****************************************************************/
63/* LZ4_DLL_EXPORT :
64 * Enable exporting of functions when building a Windows DLL
65 * LZ4FLIB_API :
66 * Control library symbols visibility.
67 */
68#if defined(LZ4_DLL_EXPORT) && (LZ4_DLL_EXPORT==1)
69# define LZ4FLIB_API __declspec(dllexport)
70#elif defined(LZ4_DLL_IMPORT) && (LZ4_DLL_IMPORT==1)
71# define LZ4FLIB_API __declspec(dllimport)
72#elif defined(__GNUC__) && (__GNUC__ >= 4)
73# define LZ4FLIB_API __attribute__ ((__visibility__ ("default")))
74#else
75# define LZ4FLIB_API
76#endif
77
78#if defined(_MSC_VER)
79# define LZ4F_DEPRECATE(x) x /* __declspec(deprecated) x - only works with C++ */
80#elif defined(__clang__) || (defined(__GNUC__) && (__GNUC__ >= 6))
81# define LZ4F_DEPRECATE(x) x __attribute__((deprecated))
82#else
83# define LZ4F_DEPRECATE(x) x /* no deprecation warning for this compiler */
84#endif
85
86
87/*-************************************
88 * Error management
89 **************************************/
90typedef size_t LZ4F_errorCode_t;
91
92LZ4FLIB_API unsigned LZ4F_isError(LZ4F_errorCode_t code); /**< tells if a `LZ4F_errorCode_t` function result is an error code */
93LZ4FLIB_API const char* LZ4F_getErrorName(LZ4F_errorCode_t code); /**< return error code string; useful for debugging */
94
95
96/*-************************************
97 * Frame compression types
98 **************************************/
99/* #define LZ4F_DISABLE_OBSOLETE_ENUMS */ /* uncomment to disable obsolete enums */
100#ifndef LZ4F_DISABLE_OBSOLETE_ENUMS
101# define LZ4F_OBSOLETE_ENUM(x) , LZ4F_DEPRECATE(x) = LZ4F_##x
102#else
103# define LZ4F_OBSOLETE_ENUM(x)
104#endif
105
106/* The larger the block size, the (slightly) better the compression ratio,
107 * though there are diminishing returns.
108 * Larger blocks also increase memory usage on both compression and decompression sides. */
109typedef enum {
110 LZ4F_default=0,
111 LZ4F_max64KB=4,
112 LZ4F_max256KB=5,
113 LZ4F_max1MB=6,
114 LZ4F_max4MB=7
115 LZ4F_OBSOLETE_ENUM(max64KB)
116 LZ4F_OBSOLETE_ENUM(max256KB)
117 LZ4F_OBSOLETE_ENUM(max1MB)
118 LZ4F_OBSOLETE_ENUM(max4MB)
119} LZ4F_blockSizeID_t;
120
121/* Linked blocks sharply reduce inefficiencies when using small blocks,
122 * they compress better.
123 * However, some LZ4 decoders are only compatible with independent blocks */
124typedef enum {
125 LZ4F_blockLinked=0,
126 LZ4F_blockIndependent
127 LZ4F_OBSOLETE_ENUM(blockLinked)
128 LZ4F_OBSOLETE_ENUM(blockIndependent)
129} LZ4F_blockMode_t;
130
131typedef enum {
132 LZ4F_noContentChecksum=0,
133 LZ4F_contentChecksumEnabled
134 LZ4F_OBSOLETE_ENUM(noContentChecksum)
135 LZ4F_OBSOLETE_ENUM(contentChecksumEnabled)
136} LZ4F_contentChecksum_t;
137
138typedef enum {
139 LZ4F_frame=0,
140 LZ4F_skippableFrame
141 LZ4F_OBSOLETE_ENUM(skippableFrame)
142} LZ4F_frameType_t;
143
144#ifndef LZ4F_DISABLE_OBSOLETE_ENUMS
145typedef LZ4F_blockSizeID_t blockSizeID_t;
146typedef LZ4F_blockMode_t blockMode_t;
147typedef LZ4F_frameType_t frameType_t;
148typedef LZ4F_contentChecksum_t contentChecksum_t;
149#endif
150
151/*! LZ4F_frameInfo_t :
152 * makes it possible to supply detailed frame parameters to the stream interface.
153 * It's not required to set all fields, as long as the structure was initially memset() to zero.
154 * All reserved fields must be set to zero. */
155typedef struct {
156 LZ4F_blockSizeID_t blockSizeID; /* max64KB, max256KB, max1MB, max4MB ; 0 == default */
157 LZ4F_blockMode_t blockMode; /* blockLinked, blockIndependent ; 0 == default */
158 LZ4F_contentChecksum_t contentChecksumFlag; /* noContentChecksum, contentChecksumEnabled ; 0 == default */
159 LZ4F_frameType_t frameType; /* LZ4F_frame, skippableFrame ; 0 == default */
160 unsigned long long contentSize; /* Size of uncompressed (original) content ; 0 == unknown */
161 unsigned reserved[2]; /* must be zero for forward compatibility */
162} LZ4F_frameInfo_t;
163
164/*! LZ4F_preferences_t :
165 * makes it possible to supply detailed compression parameters to the stream interface.
166 * It's not required to set all fields, as long as the structure was initially memset() to zero.
167 * All reserved fields must be set to zero. */
168typedef struct {
169 LZ4F_frameInfo_t frameInfo;
170 int compressionLevel; /* 0 == default (fast mode); values above LZ4HC_CLEVEL_MAX count as LZ4HC_CLEVEL_MAX; values below 0 count as 0 */
171 unsigned autoFlush; /* 1 == always flush (reduce usage of tmp buffer) */
172 unsigned reserved[4]; /* must be zero for forward compatibility */
173} LZ4F_preferences_t;
174
175
176/*-*********************************
177* Simple compression function
178***********************************/
179/*!LZ4F_compressFrameBound() :
180 * Returns the maximum possible size of a frame compressed with LZ4F_compressFrame() given srcSize content and preferences.
181 * Note : this result is only usable with LZ4F_compressFrame(), not with multi-segments compression.
182 */
183LZ4FLIB_API size_t LZ4F_compressFrameBound(size_t srcSize, const LZ4F_preferences_t* preferencesPtr);
184
185/*!LZ4F_compressFrame() :
186 * Compress an entire srcBuffer into a valid LZ4 frame, as defined by specification v1.5.1
187 * An important rule is that dstBuffer MUST be large enough (dstCapacity) to store the result in worst case situation.
188 * This value is supplied by LZ4F_compressFrameBound().
189 * If this condition is not respected, LZ4F_compressFrame() will fail (result is an errorCode).
190 * The LZ4F_preferences_t structure is optional : you can provide NULL as argument. All preferences will be set to default.
191 * @return : number of bytes written into dstBuffer.
192 * or an error code if it fails (can be tested using LZ4F_isError())
193 */
194LZ4FLIB_API size_t LZ4F_compressFrame(void* dstBuffer, size_t dstCapacity, const void* srcBuffer, size_t srcSize, const LZ4F_preferences_t* preferencesPtr);
195
196
197
198/*-***********************************
199* Advanced compression functions
200*************************************/
201typedef struct LZ4F_cctx_s LZ4F_cctx; /* incomplete type */
202typedef LZ4F_cctx* LZ4F_compressionContext_t; /* for compatibility with previous API version */
203
204typedef struct {
205 unsigned stableSrc; /* 1 == src content will remain present on future calls to LZ4F_compress(); skip copying src content within tmp buffer */
206 unsigned reserved[3];
207} LZ4F_compressOptions_t;
208
209/*--- Resource Management ---*/
210
211#define LZ4F_VERSION 100
212LZ4FLIB_API unsigned LZ4F_getVersion(void);
213/*! LZ4F_createCompressionContext() :
214 * The first thing to do is to create a compressionContext object, which will be used in all compression operations.
215 * This is achieved using LZ4F_createCompressionContext(), which takes as argument a version.
216 * The version provided MUST be LZ4F_VERSION. It is intended to track potential version mismatch, notably when using DLL.
217 * The function will provide a pointer to a fully allocated LZ4F_cctx object.
218 * If @return != zero, there was an error during context creation.
219 * Object can release its memory using LZ4F_freeCompressionContext();
220 */
221LZ4FLIB_API LZ4F_errorCode_t LZ4F_createCompressionContext(LZ4F_cctx** cctxPtr, unsigned version);
222LZ4FLIB_API LZ4F_errorCode_t LZ4F_freeCompressionContext(LZ4F_cctx* cctx);
223
224
225/*---- Compression ----*/
226
227#define LZ4F_HEADER_SIZE_MAX 15
228/*! LZ4F_compressBegin() :
229 * will write the frame header into dstBuffer.
230 * dstCapacity must be large enough to store the header. Maximum header size is LZ4F_HEADER_SIZE_MAX bytes.
231 * `prefsPtr` is optional : you can provide NULL as argument, all preferences will then be set to default.
232 * @return : number of bytes written into dstBuffer for the header
233 * or an error code (which can be tested using LZ4F_isError())
234 */
235LZ4FLIB_API size_t LZ4F_compressBegin(LZ4F_cctx* cctx, void* dstBuffer, size_t dstCapacity, const LZ4F_preferences_t* prefsPtr);
236
237/*! LZ4F_compressBound() :
238 * Provides dstCapacity given a srcSize to guarantee operation success in worst case situations.
239 * prefsPtr is optional : you can provide NULL as argument, preferences will be set to cover worst case scenario.
240 * Result is always the same for a srcSize and prefsPtr, so it can be trusted to size reusable buffers.
241 * When srcSize==0, LZ4F_compressBound() provides an upper bound for LZ4F_flush() and LZ4F_compressEnd() operations.
242 */
243LZ4FLIB_API size_t LZ4F_compressBound(size_t srcSize, const LZ4F_preferences_t* prefsPtr);
244
245/*! LZ4F_compressUpdate() :
246 * LZ4F_compressUpdate() can be called repetitively to compress as much data as necessary.
247 * An important rule is that dstCapacity MUST be large enough to ensure operation success even in worst case situations.
248 * This value is provided by LZ4F_compressBound().
249 * If this condition is not respected, LZ4F_compress() will fail (result is an errorCode).
250 * LZ4F_compressUpdate() doesn't guarantee error recovery. When an error occurs, compression context must be freed or resized.
251 * `cOptPtr` is optional : NULL can be provided, in which case all options are set to default.
252 * @return : number of bytes written into `dstBuffer` (it can be zero, meaning input data was just buffered).
253 * or an error code if it fails (which can be tested using LZ4F_isError())
254 */
255LZ4FLIB_API size_t LZ4F_compressUpdate(LZ4F_cctx* cctx, void* dstBuffer, size_t dstCapacity, const void* srcBuffer, size_t srcSize, const LZ4F_compressOptions_t* cOptPtr);
256
257/*! LZ4F_flush() :
258 * When data must be generated and sent immediately, without waiting for a block to be completely filled,
259 * it's possible to call LZ4_flush(). It will immediately compress any data buffered within cctx.
260 * `dstCapacity` must be large enough to ensure the operation will be successful.
261 * `cOptPtr` is optional : it's possible to provide NULL, all options will be set to default.
262 * @return : number of bytes written into dstBuffer (it can be zero, which means there was no data stored within cctx)
263 * or an error code if it fails (which can be tested using LZ4F_isError())
264 */
265LZ4FLIB_API size_t LZ4F_flush(LZ4F_cctx* cctx, void* dstBuffer, size_t dstCapacity, const LZ4F_compressOptions_t* cOptPtr);
266
267/*! LZ4F_compressEnd() :
268 * To properly finish an LZ4 frame, invoke LZ4F_compressEnd().
269 * It will flush whatever data remained within `cctx` (like LZ4_flush())
270 * and properly finalize the frame, with an endMark and a checksum.
271 * `cOptPtr` is optional : NULL can be provided, in which case all options will be set to default.
272 * @return : number of bytes written into dstBuffer (necessarily >= 4 (endMark), or 8 if optional frame checksum is enabled)
273 * or an error code if it fails (which can be tested using LZ4F_isError())
274 * A successful call to LZ4F_compressEnd() makes `cctx` available again for another compression task.
275 */
276LZ4FLIB_API size_t LZ4F_compressEnd(LZ4F_cctx* cctx, void* dstBuffer, size_t dstCapacity, const LZ4F_compressOptions_t* cOptPtr);
277
278
279/*-*********************************
280* Decompression functions
281***********************************/
282typedef struct LZ4F_dctx_s LZ4F_dctx; /* incomplete type */
283typedef LZ4F_dctx* LZ4F_decompressionContext_t; /* compatibility with previous API versions */
284
285typedef struct {
286 unsigned stableDst; /* guarantee that decompressed data will still be there on next function calls (avoid storage into tmp buffers) */
287 unsigned reserved[3];
288} LZ4F_decompressOptions_t;
289
290
291/* Resource management */
292
293/*!LZ4F_createDecompressionContext() :
294 * Create an LZ4F_decompressionContext_t object, which will be used to track all decompression operations.
295 * The version provided MUST be LZ4F_VERSION. It is intended to track potential breaking differences between different versions.
296 * The function will provide a pointer to a fully allocated and initialized LZ4F_decompressionContext_t object.
297 * The result is an errorCode, which can be tested using LZ4F_isError().
298 * dctx memory can be released using LZ4F_freeDecompressionContext();
299 * The result of LZ4F_freeDecompressionContext() is indicative of the current state of decompressionContext when being released.
300 * That is, it should be == 0 if decompression has been completed fully and correctly.
301 */
302LZ4FLIB_API LZ4F_errorCode_t LZ4F_createDecompressionContext(LZ4F_dctx** dctxPtr, unsigned version);
303LZ4FLIB_API LZ4F_errorCode_t LZ4F_freeDecompressionContext(LZ4F_dctx* dctx);
304
305
306/*-***********************************
307* Streaming decompression functions
308*************************************/
309
310/*! LZ4F_getFrameInfo() :
311 * This function extracts frame parameters (such as max blockSize, frame checksum, etc.).
312 * Its usage is optional. Extracted information can be useful for allocation purposes, typically.
313 * This function works in 2 situations :
314 * - At the beginning of a new frame, in which case it will decode this information from `srcBuffer`, and start the decoding process.
315 * Input size must be large enough to successfully decode the entire frame header.
316 * Frame header size is variable, but is guaranteed to be <= LZ4F_HEADER_SIZE_MAX bytes.
317 * It's allowed to provide more input data than this minimum.
318 * - After decoding has been started.
319 * In which case, no input is read, frame parameters are extracted from dctx.
320 * If decoding has just started, but not yet extracted information from header, LZ4F_getFrameInfo() will fail.
321 * The number of bytes consumed from srcBuffer will be updated within *srcSizePtr (necessarily <= original value).
322 * Decompression must resume from (srcBuffer + *srcSizePtr).
323 * @return : an hint about how many srcSize bytes LZ4F_decompress() expects for next call,
324 * or an error code which can be tested using LZ4F_isError()
325 * note 1 : in case of error, dctx is not modified. Decoding operations can resume from where they stopped.
326 * note 2 : frame parameters are *copied into* an already allocated LZ4F_frameInfo_t structure.
327 */
328LZ4FLIB_API size_t LZ4F_getFrameInfo(LZ4F_dctx* dctx,
329 LZ4F_frameInfo_t* frameInfoPtr,
330 const void* srcBuffer, size_t* srcSizePtr);
331
332/*! LZ4F_decompress() :
333 * Call this function repetitively to regenerate data compressed within `srcBuffer`.
334 * The function will attempt to decode up to *srcSizePtr bytes from srcBuffer, into dstBuffer of capacity *dstSizePtr.
335 *
336 * The number of bytes regenerated into dstBuffer will be provided within *dstSizePtr (necessarily <= original value).
337 *
338 * The number of bytes read from srcBuffer will be provided within *srcSizePtr (necessarily <= original value).
339 * Number of bytes read can be < number of bytes provided, meaning there is some more data to decode.
340 * It typically happens when dstBuffer is not large enough to contain all decoded data.
341 * Remaining data will have to be presented again in a subsequent invocation.
342 *
343 * `dstBuffer` content is expected to be flushed between each invocation, as its content will be overwritten.
344 * `dstBuffer` can be changed at will between each consecutive function invocation.
345 *
346 * @return is an hint of how many `srcSize` bytes LZ4F_decompress() expects for next call.
347 * Schematically, it's the size of the current (or remaining) compressed block + header of next block.
348 * Respecting the hint provides some small speed benefit, because it skips intermediate buffers.
349 * This is just a hint though, it's always possible to provide any srcSize.
350 * When a frame is fully decoded, @return will be 0 (no more data expected).
351 * If decompression failed, @return is an error code, which can be tested using LZ4F_isError().
352 *
353 * After a frame is fully decoded, dctx can be used again to decompress another frame.
354 * After a decompression error, use LZ4F_resetDecompressionContext() before re-using dctx, to return to clean state.
355 */
356LZ4FLIB_API size_t LZ4F_decompress(LZ4F_dctx* dctx,
357 void* dstBuffer, size_t* dstSizePtr,
358 const void* srcBuffer, size_t* srcSizePtr,
359 const LZ4F_decompressOptions_t* dOptPtr);
360
361
362
363#if defined (__cplusplus)
364}
365#endif
366
367#endif /* LZ4F_H_09782039843 */
368