| 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 | * conformant with specification v1.5.1. |
| 37 | * All related operations, including memory management, are handled internally 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 { |
| 73 | LZ4F_default=0, |
| 74 | LZ4F_max64KB=4, |
| 75 | LZ4F_max256KB=5, |
| 76 | LZ4F_max1MB=6, |
| 77 | LZ4F_max4MB=7 |
| 78 | LZ4F_OBSOLETE_ENUM(max64KB = LZ4F_max64KB) |
| 79 | LZ4F_OBSOLETE_ENUM(max256KB = LZ4F_max256KB) |
| 80 | LZ4F_OBSOLETE_ENUM(max1MB = LZ4F_max1MB) |
| 81 | LZ4F_OBSOLETE_ENUM(max4MB = LZ4F_max4MB) |
| 82 | } LZ4F_blockSizeID_t; |
| 83 | |
| 84 | typedef enum { |
| 85 | LZ4F_blockLinked=0, |
| 86 | LZ4F_blockIndependent |
| 87 | LZ4F_OBSOLETE_ENUM(blockLinked = LZ4F_blockLinked) |
| 88 | LZ4F_OBSOLETE_ENUM(blockIndependent = LZ4F_blockIndependent) |
| 89 | } LZ4F_blockMode_t; |
| 90 | |
| 91 | typedef enum { |
| 92 | LZ4F_noContentChecksum=0, |
| 93 | LZ4F_contentChecksumEnabled |
| 94 | LZ4F_OBSOLETE_ENUM(noContentChecksum = LZ4F_noContentChecksum) |
| 95 | LZ4F_OBSOLETE_ENUM(contentChecksumEnabled = LZ4F_contentChecksumEnabled) |
| 96 | } LZ4F_contentChecksum_t; |
| 97 | |
| 98 | typedef enum { |
| 99 | LZ4F_frame=0, |
| 100 | LZ4F_skippableFrame |
| 101 | LZ4F_OBSOLETE_ENUM(skippableFrame = LZ4F_skippableFrame) |
| 102 | } LZ4F_frameType_t; |
| 103 | |
| 104 | #ifndef LZ4F_DISABLE_OBSOLETE_ENUMS |
| 105 | typedef LZ4F_blockSizeID_t blockSizeID_t; |
| 106 | typedef LZ4F_blockMode_t blockMode_t; |
| 107 | typedef LZ4F_frameType_t frameType_t; |
| 108 | typedef LZ4F_contentChecksum_t contentChecksum_t; |
| 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 */ |
| 118 | } LZ4F_frameInfo_t; |
| 119 | |
| 120 | typedef struct { |
| 121 | LZ4F_frameInfo_t frameInfo; |
| 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 */ |
| 125 | } LZ4F_preferences_t; |
| 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]; |
| 154 | } LZ4F_compressOptions_t; |
| 155 | |
| 156 | /* Resource Management */ |
| 157 | |
| 158 | #define LZ4F_VERSION 100 |
| 159 | LZ4F_errorCode_t LZ4F_createCompressionContext(LZ4F_compressionContext_t* cctxPtr, unsigned version); |
| 160 | LZ4F_errorCode_t LZ4F_freeCompressionContext(LZ4F_compressionContext_t cctx); |
| 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]; |
| 234 | } LZ4F_decompressOptions_t; |
| 235 | |
| 236 | |
| 237 | /* Resource management */ |
| 238 | |
| 239 | LZ4F_errorCode_t LZ4F_createDecompressionContext(LZ4F_decompressionContext_t* dctxPtr, unsigned version); |
| 240 | LZ4F_errorCode_t LZ4F_freeDecompressionContext(LZ4F_decompressionContext_t dctx); |
| 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 | |
| 255 | size_t LZ4F_getFrameInfo(LZ4F_decompressionContext_t dctx, |
| 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. The objective is to extract frame header information, typically for allocation purposes. |
| 261 | * A header size is variable and can be from 7 to 15 bytes. It's also possible to input more bytes than that. |
| 262 | * The number of bytes read from srcBuffer will be updated within *srcSizePtr (necessarily <= original value). |
| 263 | * (note that LZ4F_getFrameInfo() can also be used anytime *after* starting decompression, in this case 0 input byte is enough) |
| 264 | * Frame header info is *copied into* an already allocated LZ4F_frameInfo_t structure. |
| 265 | * The function result is an hint about 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 | * Decompression is expected to resume from where it stopped (srcBuffer + *srcSizePtr) |
| 269 | */ |
| 270 | |
| 271 | size_t LZ4F_decompress(LZ4F_decompressionContext_t dctx, |
| 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 |