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