Home | History | Annotate | Download | only in lib
      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 : http://code.google.com/p/lz4/
     32    - LZ4 source mirror : https://github.com/Cyan4973/lz4
     33    - LZ4 public forum : https://groups.google.com/forum/#!forum/lz4c
     34 */
     35 
     36 /* LZ4F is a stand-alone API to create LZ4-compressed frames
     37  * fully conformant to specification v1.4.1.
     38  * All related operations, including memory management, are handled by the library.
     39  * You don't need lz4.h when using lz4frame.h.
     40  * */
     41 
     42 #pragma once
     43 
     44 #if defined (__cplusplus)
     45 extern "C" {
     46 #endif
     47 
     48 /**************************************
     49    Includes
     50 **************************************/
     51 #include <stddef.h>   /* size_t */
     52 
     53 
     54 /**************************************
     55  * Error management
     56  * ************************************/
     57 typedef size_t LZ4F_errorCode_t;
     58 
     59 unsigned    LZ4F_isError(LZ4F_errorCode_t code);
     60 const char* LZ4F_getErrorName(LZ4F_errorCode_t code);   /* return error code string; useful for debugging */
     61 
     62 
     63 /**************************************
     64  * Frame compression types
     65  * ************************************/
     66 typedef enum { LZ4F_default=0, max64KB=4, max256KB=5, max1MB=6, max4MB=7 } blockSizeID_t;
     67 typedef enum { blockLinked=0, blockIndependent} blockMode_t;
     68 typedef enum { noContentChecksum=0, contentChecksumEnabled } contentChecksum_t;
     69 
     70 typedef struct {
     71   blockSizeID_t     blockSizeID;           /* max64KB, max256KB, max1MB, max4MB ; 0 == default */
     72   blockMode_t       blockMode;             /* blockLinked, blockIndependent ; 0 == default */
     73   contentChecksum_t contentChecksumFlag;   /* noContentChecksum, contentChecksumEnabled ; 0 == default  */
     74   unsigned          reserved[5];
     75 } LZ4F_frameInfo_t;
     76 
     77 typedef struct {
     78   LZ4F_frameInfo_t frameInfo;
     79   unsigned         compressionLevel;       /* 0 == default (fast mode); values above 16 count as 16 */
     80   unsigned         autoFlush;              /* 1 == always flush : reduce need for tmp buffer */
     81   unsigned         reserved[4];
     82 } LZ4F_preferences_t;
     83 
     84 
     85 /***********************************
     86  * Simple compression function
     87  * *********************************/
     88 size_t LZ4F_compressFrameBound(size_t srcSize, const LZ4F_preferences_t* preferencesPtr);
     89 
     90 size_t LZ4F_compressFrame(void* dstBuffer, size_t dstMaxSize, const void* srcBuffer, size_t srcSize, const LZ4F_preferences_t* preferencesPtr);
     91 /* LZ4F_compressFrame()
     92  * Compress an entire srcBuffer into a valid LZ4 frame, as defined by specification v1.4.1.
     93  * The most important rule is that dstBuffer MUST be large enough (dstMaxSize) to ensure compression completion even in worst case.
     94  * You can get the minimum value of dstMaxSize by using LZ4F_compressFrameBound()
     95  * If this condition is not respected, LZ4F_compressFrame() will fail (result is an errorCode)
     96  * The LZ4F_preferences_t structure is optional : you can provide NULL as argument. All preferences will be set to default.
     97  * The result of the function is the number of bytes written into dstBuffer.
     98  * The function outputs an error code if it fails (can be tested using LZ4F_isError())
     99  */
    100 
    101 
    102 
    103 /**********************************
    104  * Advanced compression functions
    105  * ********************************/
    106 typedef void* LZ4F_compressionContext_t;
    107 
    108 typedef struct {
    109   unsigned stableSrc;    /* 1 == src content will remain available on future calls to LZ4F_compress(); avoid saving src content within tmp buffer as future dictionary */
    110   unsigned reserved[3];
    111 } LZ4F_compressOptions_t;
    112 
    113 /* Resource Management */
    114 
    115 #define LZ4F_VERSION 100
    116 LZ4F_errorCode_t LZ4F_createCompressionContext(LZ4F_compressionContext_t* LZ4F_compressionContextPtr, unsigned version);
    117 LZ4F_errorCode_t LZ4F_freeCompressionContext(LZ4F_compressionContext_t LZ4F_compressionContext);
    118 /* LZ4F_createCompressionContext() :
    119  * The first thing to do is to create a compressionContext object, which will be used in all compression operations.
    120  * This is achieved using LZ4F_createCompressionContext(), which takes as argument a version and an LZ4F_preferences_t structure.
    121  * The version provided MUST be LZ4F_VERSION. It is intended to track potential version differences between different binaries.
    122  * The function will provide a pointer to a fully allocated LZ4F_compressionContext_t object.
    123  * If the result LZ4F_errorCode_t is not zero, there was an error during context creation.
    124  * Object can release its memory using LZ4F_freeCompressionContext();
    125  */
    126 
    127 
    128 /* Compression */
    129 
    130 size_t LZ4F_compressBegin(LZ4F_compressionContext_t compressionContext, void* dstBuffer, size_t dstMaxSize, const LZ4F_preferences_t* preferencesPtr);
    131 /* LZ4F_compressBegin() :
    132  * will write the frame header into dstBuffer.
    133  * dstBuffer must be large enough to accommodate a header (dstMaxSize). Maximum header size is 19 bytes.
    134  * The LZ4F_preferences_t structure is optional : you can provide NULL as argument, all preferences will then be set to default.
    135  * The result of the function is the number of bytes written into dstBuffer for the header
    136  * or an error code (can be tested using LZ4F_isError())
    137  */
    138 
    139 size_t LZ4F_compressBound(size_t srcSize, const LZ4F_preferences_t* preferencesPtr);
    140 /* LZ4F_compressBound() :
    141  * Provides the minimum size of Dst buffer given srcSize to handle worst case situations.
    142  * preferencesPtr is optional : you can provide NULL as argument, all preferences will then be set to default.
    143  * Note that different preferences will produce in different results.
    144  */
    145 
    146 size_t LZ4F_compressUpdate(LZ4F_compressionContext_t compressionContext, void* dstBuffer, size_t dstMaxSize, const void* srcBuffer, size_t srcSize, const LZ4F_compressOptions_t* compressOptionsPtr);
    147 /* LZ4F_compressUpdate()
    148  * LZ4F_compressUpdate() can be called repetitively to compress as much data as necessary.
    149  * The most important rule is that dstBuffer MUST be large enough (dstMaxSize) to ensure compression completion even in worst case.
    150  * If this condition is not respected, LZ4F_compress() will fail (result is an errorCode)
    151  * You can get the minimum value of dstMaxSize by using LZ4F_compressBound()
    152  * The LZ4F_compressOptions_t structure is optional : you can provide NULL as argument.
    153  * The result of the function is the number of bytes written into dstBuffer : it can be zero, meaning input data was just buffered.
    154  * The function outputs an error code if it fails (can be tested using LZ4F_isError())
    155  */
    156 
    157 size_t LZ4F_flush(LZ4F_compressionContext_t compressionContext, void* dstBuffer, size_t dstMaxSize, const LZ4F_compressOptions_t* compressOptionsPtr);
    158 /* LZ4F_flush()
    159  * Should you need to create compressed data immediately, without waiting for a block to be filled,
    160  * you can call LZ4_flush(), which will immediately compress any remaining data buffered within compressionContext.
    161  * The LZ4F_compressOptions_t structure is optional : you can provide NULL as argument.
    162  * The result of the function is the number of bytes written into dstBuffer
    163  * (it can be zero, this means there was no data left within compressionContext)
    164  * The function outputs an error code if it fails (can be tested using LZ4F_isError())
    165  */
    166 
    167 size_t LZ4F_compressEnd(LZ4F_compressionContext_t compressionContext, void* dstBuffer, size_t dstMaxSize, const LZ4F_compressOptions_t* compressOptionsPtr);
    168 /* LZ4F_compressEnd()
    169  * When you want to properly finish the compressed frame, just call LZ4F_compressEnd().
    170  * It will flush whatever data remained within compressionContext (like LZ4_flush())
    171  * but also properly finalize the frame, with an endMark and a checksum.
    172  * The result of the function is the number of bytes written into dstBuffer (necessarily >= 4 (endMark size))
    173  * The function outputs an error code if it fails (can be tested using LZ4F_isError())
    174  * The LZ4F_compressOptions_t structure is optional : you can provide NULL as argument.
    175  * compressionContext can then be used again, starting with LZ4F_compressBegin().
    176  */
    177 
    178 
    179 /***********************************
    180  * Decompression functions
    181  * *********************************/
    182 
    183 typedef void* LZ4F_decompressionContext_t;
    184 
    185 typedef struct {
    186   unsigned stableDst;       /* guarantee that decompressed data will still be there on next function calls (avoid storage into tmp buffers) */
    187   unsigned reserved[3];
    188 } LZ4F_decompressOptions_t;
    189 
    190 
    191 /* Resource management */
    192 
    193 LZ4F_errorCode_t LZ4F_createDecompressionContext(LZ4F_decompressionContext_t* ctxPtr, unsigned version);
    194 LZ4F_errorCode_t LZ4F_freeDecompressionContext(LZ4F_decompressionContext_t ctx);
    195 /* LZ4F_createDecompressionContext() :
    196  * The first thing to do is to create a decompressionContext object, which will be used in all decompression operations.
    197  * This is achieved using LZ4F_createDecompressionContext().
    198  * The version provided MUST be LZ4F_VERSION. It is intended to track potential version differences between different binaries.
    199  * The function will provide a pointer to a fully allocated and initialized LZ4F_decompressionContext_t object.
    200  * If the result LZ4F_errorCode_t is not OK_NoError, there was an error during context creation.
    201  * Object can release its memory using LZ4F_freeDecompressionContext();
    202  */
    203 
    204 
    205 /* Decompression */
    206 
    207 size_t LZ4F_getFrameInfo(LZ4F_decompressionContext_t ctx,
    208                          LZ4F_frameInfo_t* frameInfoPtr,
    209                          const void* srcBuffer, size_t* srcSizePtr);
    210 /* LZ4F_getFrameInfo()
    211  * This function decodes frame header information, such as blockSize.
    212  * It is optional : you could start by calling directly LZ4F_decompress() instead.
    213  * The objective is to extract header information without starting decompression, typically for allocation purposes.
    214  * LZ4F_getFrameInfo() can also be used *after* starting decompression, on a valid LZ4F_decompressionContext_t.
    215  * The number of bytes read from srcBuffer will be provided within *srcSizePtr (necessarily <= original value).
    216  * You are expected to resume decompression from where it stopped (srcBuffer + *srcSizePtr)
    217  * The function result is an hint of how many srcSize bytes LZ4F_decompress() expects for next call,
    218  * or an error code which can be tested using LZ4F_isError().
    219  */
    220 
    221 size_t LZ4F_decompress(LZ4F_decompressionContext_t ctx,
    222                        void* dstBuffer, size_t* dstSizePtr,
    223                        const void* srcBuffer, size_t* srcSizePtr,
    224                        const LZ4F_decompressOptions_t* optionsPtr);
    225 /* LZ4F_decompress()
    226  * Call this function repetitively to regenerate data compressed within srcBuffer.
    227  * The function will attempt to decode *srcSizePtr bytes from srcBuffer, into dstBuffer of maximum size *dstSizePtr.
    228  *
    229  * The number of bytes regenerated into dstBuffer will be provided within *dstSizePtr (necessarily <= original value).
    230  *
    231  * The number of bytes read from srcBuffer will be provided within *srcSizePtr (necessarily <= original value).
    232  * If number of bytes read is < number of bytes provided, then decompression operation is not completed.
    233  * It typically happens when dstBuffer is not large enough to contain all decoded data.
    234  * LZ4F_decompress() must be called again, starting from where it stopped (srcBuffer + *srcSizePtr)
    235  * The function will check this condition, and refuse to continue if it is not respected.
    236  *
    237  * dstBuffer is supposed to be flushed between each call to the function, since its content will be overwritten.
    238  * dst arguments can be changed at will with each consecutive call to the function.
    239  *
    240  * The function result is an hint of how many srcSize bytes LZ4F_decompress() expects for next call.
    241  * Schematically, it's the size of the current (or remaining) compressed block + header of next block.
    242  * Respecting the hint provides some boost to performance, since it does skip intermediate buffers.
    243  * This is just a hint, you can always provide any srcSize you want.
    244  * When a frame is fully decoded, the function result will be 0. (no more data expected)
    245  * If decompression failed, function result is an error code, which can be tested using LZ4F_isError().
    246  */
    247 
    248 
    249 
    250 #if defined (__cplusplus)
    251 }
    252 #endif
    253