kusano 7d535a
/*
kusano 7d535a
   LZ4 auto-framing library
kusano 7d535a
   Header File
kusano 7d535a
   Copyright (C) 2011-2015, Yann Collet.
kusano 7d535a
   BSD 2-Clause License (http://www.opensource.org/licenses/bsd-license.php)
kusano 7d535a
kusano 7d535a
   Redistribution and use in source and binary forms, with or without
kusano 7d535a
   modification, are permitted provided that the following conditions are
kusano 7d535a
   met:
kusano 7d535a
kusano 7d535a
       * Redistributions of source code must retain the above copyright
kusano 7d535a
   notice, this list of conditions and the following disclaimer.
kusano 7d535a
       * Redistributions in binary form must reproduce the above
kusano 7d535a
   copyright notice, this list of conditions and the following disclaimer
kusano 7d535a
   in the documentation and/or other materials provided with the
kusano 7d535a
   distribution.
kusano 7d535a
kusano 7d535a
   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
kusano 7d535a
   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
kusano 7d535a
   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
kusano 7d535a
   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
kusano 7d535a
   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
kusano 7d535a
   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
kusano 7d535a
   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
kusano 7d535a
   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
kusano 7d535a
   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
kusano 7d535a
   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
kusano 7d535a
   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
kusano 7d535a
kusano 7d535a
   You can contact the author at :
kusano 7d535a
   - LZ4 source repository : https://github.com/Cyan4973/lz4
kusano 7d535a
   - LZ4 public forum : https://groups.google.com/forum/#!forum/lz4c
kusano 7d535a
*/
kusano 7d535a
kusano 7d535a
/* LZ4F is a stand-alone API to create LZ4-compressed frames
kusano 7d535a
 * fully conformant to specification v1.5.1.
kusano 7d535a
 * All related operations, including memory management, are handled by the library.
kusano 7d535a
 * You don't need lz4.h when using lz4frame.h.
kusano 7d535a
 * */
kusano 7d535a
kusano 7d535a
#pragma once
kusano 7d535a
kusano 7d535a
#if defined (__cplusplus)
kusano 7d535a
extern "C" {
kusano 7d535a
#endif
kusano 7d535a
kusano 7d535a
/**************************************
kusano 7d535a
*  Includes
kusano 7d535a
**************************************/
kusano 7d535a
#include <stddef.h>   /* size_t */</stddef.h>
kusano 7d535a
kusano 7d535a
kusano 7d535a
/**************************************
kusano 7d535a
 * Error management
kusano 7d535a
 * ************************************/
kusano 7d535a
typedef size_t LZ4F_errorCode_t;
kusano 7d535a
kusano 7d535a
unsigned    LZ4F_isError(LZ4F_errorCode_t code);
kusano 7d535a
const char* LZ4F_getErrorName(LZ4F_errorCode_t code);   /* return error code string; useful for debugging */
kusano 7d535a
kusano 7d535a
kusano 7d535a
/**************************************
kusano 7d535a
 * Frame compression types
kusano 7d535a
 * ************************************/
kusano 7d535a
//#define LZ4F_DISABLE_OBSOLETE_ENUMS
kusano 7d535a
#ifndef LZ4F_DISABLE_OBSOLETE_ENUMS
kusano 7d535a
#  define LZ4F_OBSOLETE_ENUM(x) ,x
kusano 7d535a
#else
kusano 7d535a
#  define LZ4F_OBSOLETE_ENUM(x)
kusano 7d535a
#endif
kusano 7d535a
kusano 7d535a
typedef enum {
kusano 7d535a
    LZ4F_default=0,
kusano 7d535a
    LZ4F_max64KB=4,
kusano 7d535a
    LZ4F_max256KB=5,
kusano 7d535a
    LZ4F_max1MB=6,
kusano 7d535a
    LZ4F_max4MB=7
kusano 7d535a
    LZ4F_OBSOLETE_ENUM(max64KB = LZ4F_max64KB)
kusano 7d535a
    LZ4F_OBSOLETE_ENUM(max256KB = LZ4F_max256KB)
kusano 7d535a
    LZ4F_OBSOLETE_ENUM(max1MB = LZ4F_max1MB)
kusano 7d535a
    LZ4F_OBSOLETE_ENUM(max4MB = LZ4F_max4MB)
kusano 7d535a
} LZ4F_blockSizeID_t;
kusano 7d535a
kusano 7d535a
typedef enum {
kusano 7d535a
    LZ4F_blockLinked=0,
kusano 7d535a
    LZ4F_blockIndependent
kusano 7d535a
    LZ4F_OBSOLETE_ENUM(blockLinked = LZ4F_blockLinked)
kusano 7d535a
    LZ4F_OBSOLETE_ENUM(blockIndependent = LZ4F_blockIndependent)
kusano 7d535a
} LZ4F_blockMode_t;
kusano 7d535a
kusano 7d535a
typedef enum {
kusano 7d535a
    LZ4F_noContentChecksum=0,
kusano 7d535a
    LZ4F_contentChecksumEnabled
kusano 7d535a
    LZ4F_OBSOLETE_ENUM(noContentChecksum = LZ4F_noContentChecksum)
kusano 7d535a
    LZ4F_OBSOLETE_ENUM(contentChecksumEnabled = LZ4F_contentChecksumEnabled)
kusano 7d535a
} LZ4F_contentChecksum_t;
kusano 7d535a
kusano 7d535a
typedef enum {
kusano 7d535a
    LZ4F_frame=0,
kusano 7d535a
    LZ4F_skippableFrame
kusano 7d535a
    LZ4F_OBSOLETE_ENUM(skippableFrame = LZ4F_skippableFrame)
kusano 7d535a
} LZ4F_frameType_t;
kusano 7d535a
kusano 7d535a
#ifndef LZ4F_DISABLE_OBSOLETE_ENUMS
kusano 7d535a
typedef LZ4F_blockSizeID_t blockSizeID_t;
kusano 7d535a
typedef LZ4F_blockMode_t blockMode_t;
kusano 7d535a
typedef LZ4F_frameType_t frameType_t;
kusano 7d535a
typedef LZ4F_contentChecksum_t contentChecksum_t;
kusano 7d535a
#endif
kusano 7d535a
kusano 7d535a
typedef struct {
kusano 7d535a
  LZ4F_blockSizeID_t     blockSizeID;           /* max64KB, max256KB, max1MB, max4MB ; 0 == default */
kusano 7d535a
  LZ4F_blockMode_t       blockMode;             /* blockLinked, blockIndependent ; 0 == default */
kusano 7d535a
  LZ4F_contentChecksum_t contentChecksumFlag;   /* noContentChecksum, contentChecksumEnabled ; 0 == default  */
kusano 7d535a
  LZ4F_frameType_t       frameType;             /* LZ4F_frame, skippableFrame ; 0 == default */
kusano 7d535a
  unsigned long long     contentSize;           /* Size of uncompressed (original) content ; 0 == unknown */
kusano 7d535a
  unsigned               reserved[2];           /* must be zero for forward compatibility */
kusano 7d535a
} LZ4F_frameInfo_t;
kusano 7d535a
kusano 7d535a
typedef struct {
kusano 7d535a
  LZ4F_frameInfo_t frameInfo;
kusano 7d535a
  int      compressionLevel;       /* 0 == default (fast mode); values above 16 count as 16; values below 0 count as 0 */
kusano 7d535a
  unsigned autoFlush;              /* 1 == always flush (reduce need for tmp buffer) */
kusano 7d535a
  unsigned reserved[4];            /* must be zero for forward compatibility */
kusano 7d535a
} LZ4F_preferences_t;
kusano 7d535a
kusano 7d535a
kusano 7d535a
/***********************************
kusano 7d535a
 * Simple compression function
kusano 7d535a
 * *********************************/
kusano 7d535a
size_t LZ4F_compressFrameBound(size_t srcSize, const LZ4F_preferences_t* preferencesPtr);
kusano 7d535a
kusano 7d535a
size_t LZ4F_compressFrame(void* dstBuffer, size_t dstMaxSize, const void* srcBuffer, size_t srcSize, const LZ4F_preferences_t* preferencesPtr);
kusano 7d535a
/* LZ4F_compressFrame()
kusano 7d535a
 * Compress an entire srcBuffer into a valid LZ4 frame, as defined by specification v1.5.1
kusano 7d535a
 * The most important rule is that dstBuffer MUST be large enough (dstMaxSize) to ensure compression completion even in worst case.
kusano 7d535a
 * You can get the minimum value of dstMaxSize by using LZ4F_compressFrameBound()
kusano 7d535a
 * If this condition is not respected, LZ4F_compressFrame() will fail (result is an errorCode)
kusano 7d535a
 * The LZ4F_preferences_t structure is optional : you can provide NULL as argument. All preferences will be set to default.
kusano 7d535a
 * The result of the function is the number of bytes written into dstBuffer.
kusano 7d535a
 * The function outputs an error code if it fails (can be tested using LZ4F_isError())
kusano 7d535a
 */
kusano 7d535a
kusano 7d535a
kusano 7d535a
kusano 7d535a
/**********************************
kusano 7d535a
*  Advanced compression functions
kusano 7d535a
**********************************/
kusano 7d535a
typedef struct LZ4F_cctx_s* LZ4F_compressionContext_t;   /* must be aligned on 8-bytes */
kusano 7d535a
kusano 7d535a
typedef struct {
kusano 7d535a
  unsigned stableSrc;    /* 1 == src content will remain available on future calls to LZ4F_compress(); avoid saving src content within tmp buffer as future dictionary */
kusano 7d535a
  unsigned reserved[3];
kusano 7d535a
} LZ4F_compressOptions_t;
kusano 7d535a
kusano 7d535a
/* Resource Management */
kusano 7d535a
kusano 7d535a
#define LZ4F_VERSION 100
kusano 7d535a
LZ4F_errorCode_t LZ4F_createCompressionContext(LZ4F_compressionContext_t* cctxPtr, unsigned version);
kusano 7d535a
LZ4F_errorCode_t LZ4F_freeCompressionContext(LZ4F_compressionContext_t cctx);
kusano 7d535a
/* LZ4F_createCompressionContext() :
kusano 7d535a
 * The first thing to do is to create a compressionContext object, which will be used in all compression operations.
kusano 7d535a
 * This is achieved using LZ4F_createCompressionContext(), which takes as argument a version and an LZ4F_preferences_t structure.
kusano 7d535a
 * The version provided MUST be LZ4F_VERSION. It is intended to track potential version differences between different binaries.
kusano 7d535a
 * The function will provide a pointer to a fully allocated LZ4F_compressionContext_t object.
kusano 7d535a
 * If the result LZ4F_errorCode_t is not zero, there was an error during context creation.
kusano 7d535a
 * Object can release its memory using LZ4F_freeCompressionContext();
kusano 7d535a
 */
kusano 7d535a
kusano 7d535a
kusano 7d535a
/* Compression */
kusano 7d535a
kusano 7d535a
size_t LZ4F_compressBegin(LZ4F_compressionContext_t cctx, void* dstBuffer, size_t dstMaxSize, const LZ4F_preferences_t* prefsPtr);
kusano 7d535a
/* LZ4F_compressBegin() :
kusano 7d535a
 * will write the frame header into dstBuffer.
kusano 7d535a
 * dstBuffer must be large enough to accommodate a header (dstMaxSize). Maximum header size is 15 bytes.
kusano 7d535a
 * The LZ4F_preferences_t structure is optional : you can provide NULL as argument, all preferences will then be set to default.
kusano 7d535a
 * The result of the function is the number of bytes written into dstBuffer for the header
kusano 7d535a
 * or an error code (can be tested using LZ4F_isError())
kusano 7d535a
 */
kusano 7d535a
kusano 7d535a
size_t LZ4F_compressBound(size_t srcSize, const LZ4F_preferences_t* prefsPtr);
kusano 7d535a
/* LZ4F_compressBound() :
kusano 7d535a
 * Provides the minimum size of Dst buffer given srcSize to handle worst case situations.
kusano 7d535a
 * Different preferences can produce different results.
kusano 7d535a
 * prefsPtr is optional : you can provide NULL as argument, all preferences will then be set to cover worst case.
kusano 7d535a
 * This function includes frame termination cost (4 bytes, or 8 if frame checksum is enabled)
kusano 7d535a
 */
kusano 7d535a
kusano 7d535a
size_t LZ4F_compressUpdate(LZ4F_compressionContext_t cctx, void* dstBuffer, size_t dstMaxSize, const void* srcBuffer, size_t srcSize, const LZ4F_compressOptions_t* cOptPtr);
kusano 7d535a
/* LZ4F_compressUpdate()
kusano 7d535a
 * LZ4F_compressUpdate() can be called repetitively to compress as much data as necessary.
kusano 7d535a
 * The most important rule is that dstBuffer MUST be large enough (dstMaxSize) to ensure compression completion even in worst case.
kusano 7d535a
 * You can get the minimum value of dstMaxSize by using LZ4F_compressBound().
kusano 7d535a
 * If this condition is not respected, LZ4F_compress() will fail (result is an errorCode).
kusano 7d535a
 * LZ4F_compressUpdate() doesn't guarantee error recovery, so you have to reset compression context when an error occurs.
kusano 7d535a
 * The LZ4F_compressOptions_t structure is optional : you can provide NULL as argument.
kusano 7d535a
 * The result of the function is the number of bytes written into dstBuffer : it can be zero, meaning input data was just buffered.
kusano 7d535a
 * The function outputs an error code if it fails (can be tested using LZ4F_isError())
kusano 7d535a
 */
kusano 7d535a
kusano 7d535a
size_t LZ4F_flush(LZ4F_compressionContext_t cctx, void* dstBuffer, size_t dstMaxSize, const LZ4F_compressOptions_t* cOptPtr);
kusano 7d535a
/* LZ4F_flush()
kusano 7d535a
 * Should you need to generate compressed data immediately, without waiting for the current block to be filled,
kusano 7d535a
 * you can call LZ4_flush(), which will immediately compress any remaining data buffered within cctx.
kusano 7d535a
 * Note that dstMaxSize must be large enough to ensure the operation will be successful.
kusano 7d535a
 * LZ4F_compressOptions_t structure is optional : you can provide NULL as argument.
kusano 7d535a
 * The result of the function is the number of bytes written into dstBuffer
kusano 7d535a
 * (it can be zero, this means there was no data left within cctx)
kusano 7d535a
 * The function outputs an error code if it fails (can be tested using LZ4F_isError())
kusano 7d535a
 */
kusano 7d535a
kusano 7d535a
size_t LZ4F_compressEnd(LZ4F_compressionContext_t cctx, void* dstBuffer, size_t dstMaxSize, const LZ4F_compressOptions_t* cOptPtr);
kusano 7d535a
/* LZ4F_compressEnd()
kusano 7d535a
 * When you want to properly finish the compressed frame, just call LZ4F_compressEnd().
kusano 7d535a
 * It will flush whatever data remained within compressionContext (like LZ4_flush())
kusano 7d535a
 * but also properly finalize the frame, with an endMark and a checksum.
kusano 7d535a
 * The result of the function is the number of bytes written into dstBuffer (necessarily >= 4 (endMark), or 8 if optional frame checksum is enabled)
kusano 7d535a
 * The function outputs an error code if it fails (can be tested using LZ4F_isError())
kusano 7d535a
 * The LZ4F_compressOptions_t structure is optional : you can provide NULL as argument.
kusano 7d535a
 * A successful call to LZ4F_compressEnd() makes cctx available again for next compression task.
kusano 7d535a
 */
kusano 7d535a
kusano 7d535a
kusano 7d535a
/***********************************
kusano 7d535a
*  Decompression functions
kusano 7d535a
***********************************/
kusano 7d535a
kusano 7d535a
typedef struct LZ4F_dctx_s* LZ4F_decompressionContext_t;   /* must be aligned on 8-bytes */
kusano 7d535a
kusano 7d535a
typedef struct {
kusano 7d535a
  unsigned stableDst;       /* guarantee that decompressed data will still be there on next function calls (avoid storage into tmp buffers) */
kusano 7d535a
  unsigned reserved[3];
kusano 7d535a
} LZ4F_decompressOptions_t;
kusano 7d535a
kusano 7d535a
kusano 7d535a
/* Resource management */
kusano 7d535a
kusano 7d535a
LZ4F_errorCode_t LZ4F_createDecompressionContext(LZ4F_decompressionContext_t* dctxPtr, unsigned version);
kusano 7d535a
LZ4F_errorCode_t LZ4F_freeDecompressionContext(LZ4F_decompressionContext_t dctx);
kusano 7d535a
/* LZ4F_createDecompressionContext() :
kusano 7d535a
 * The first thing to do is to create an LZ4F_decompressionContext_t object, which will be used in all decompression operations.
kusano 7d535a
 * This is achieved using LZ4F_createDecompressionContext().
kusano 7d535a
 * The version provided MUST be LZ4F_VERSION. It is intended to track potential breaking differences between different versions.
kusano 7d535a
 * The function will provide a pointer to a fully allocated and initialized LZ4F_decompressionContext_t object.
kusano 7d535a
 * The result is an errorCode, which can be tested using LZ4F_isError().
kusano 7d535a
 * dctx memory can be released using LZ4F_freeDecompressionContext();
kusano 7d535a
 * The result of LZ4F_freeDecompressionContext() is indicative of the current state of decompressionContext when being released.
kusano 7d535a
 * That is, it should be == 0 if decompression has been completed fully and correctly.
kusano 7d535a
 */
kusano 7d535a
kusano 7d535a
kusano 7d535a
/* Decompression */
kusano 7d535a
kusano 7d535a
size_t LZ4F_getFrameInfo(LZ4F_decompressionContext_t dctx,
kusano 7d535a
                         LZ4F_frameInfo_t* frameInfoPtr,
kusano 7d535a
                         const void* srcBuffer, size_t* srcSizePtr);
kusano 7d535a
/* LZ4F_getFrameInfo()
kusano 7d535a
 * This function decodes frame header information (such as max blockSize, frame checksum, etc.).
kusano 7d535a
 * Its usage is optional : you can start by calling directly LZ4F_decompress() instead.
kusano 7d535a
 * The objective is to extract frame header information, typically for allocation purposes.
kusano 7d535a
 * LZ4F_getFrameInfo() can also be used anytime *after* starting decompression, on any valid LZ4F_decompressionContext_t.
kusano 7d535a
 * The result is *copied* into an existing LZ4F_frameInfo_t structure which must be already allocated.
kusano 7d535a
 * The number of bytes read from srcBuffer will be provided within *srcSizePtr (necessarily <= original value).
kusano 7d535a
 * The function result is an hint of how many srcSize bytes LZ4F_decompress() expects for next call,
kusano 7d535a
 *                        or an error code which can be tested using LZ4F_isError()
kusano 7d535a
 *                        (typically, when there is not enough src bytes to fully decode the frame header)
kusano 7d535a
 * You are expected to resume decompression from where it stopped (srcBuffer + *srcSizePtr)
kusano 7d535a
 */
kusano 7d535a
kusano 7d535a
size_t LZ4F_decompress(LZ4F_decompressionContext_t dctx,
kusano 7d535a
                       void* dstBuffer, size_t* dstSizePtr,
kusano 7d535a
                       const void* srcBuffer, size_t* srcSizePtr,
kusano 7d535a
                       const LZ4F_decompressOptions_t* dOptPtr);
kusano 7d535a
/* LZ4F_decompress()
kusano 7d535a
 * Call this function repetitively to regenerate data compressed within srcBuffer.
kusano 7d535a
 * The function will attempt to decode *srcSizePtr bytes from srcBuffer, into dstBuffer of maximum size *dstSizePtr.
kusano 7d535a
 *
kusano 7d535a
 * The number of bytes regenerated into dstBuffer will be provided within *dstSizePtr (necessarily <= original value).
kusano 7d535a
 *
kusano 7d535a
 * The number of bytes read from srcBuffer will be provided within *srcSizePtr (necessarily <= original value).
kusano 7d535a
 * If number of bytes read is < number of bytes provided, then decompression operation is not completed.
kusano 7d535a
 * It typically happens when dstBuffer is not large enough to contain all decoded data.
kusano 7d535a
 * LZ4F_decompress() must be called again, starting from where it stopped (srcBuffer + *srcSizePtr)
kusano 7d535a
 * The function will check this condition, and refuse to continue if it is not respected.
kusano 7d535a
 *
kusano 7d535a
 * dstBuffer is supposed to be flushed between each call to the function, since its content will be overwritten.
kusano 7d535a
 * dst arguments can be changed at will with each consecutive call to the function.
kusano 7d535a
 *
kusano 7d535a
 * The function result is an hint of how many srcSize bytes LZ4F_decompress() expects for next call.
kusano 7d535a
 * Schematically, it's the size of the current (or remaining) compressed block + header of next block.
kusano 7d535a
 * Respecting the hint provides some boost to performance, since it does skip intermediate buffers.
kusano 7d535a
 * This is just a hint, you can always provide any srcSize you want.
kusano 7d535a
 * When a frame is fully decoded, the function result will be 0 (no more data expected).
kusano 7d535a
 * If decompression failed, function result is an error code, which can be tested using LZ4F_isError().
kusano 7d535a
 *
kusano 7d535a
 * After a frame is fully decoded, dctx can be used again to decompress another frame.
kusano 7d535a
 */
kusano 7d535a
kusano 7d535a
kusano 7d535a
#if defined (__cplusplus)
kusano 7d535a
}
kusano 7d535a
#endif