2014-08-29 15:35:13 +00:00
|
|
|
/*
|
2014-10-27 23:39:06 +00:00
|
|
|
LZ4 auto-framing library
|
2015-03-13 01:24:08 +00:00
|
|
|
Copyright (C) 2011-2015, Yann Collet.
|
2015-03-10 15:57:42 +00:00
|
|
|
|
2014-10-27 23:39:06 +00:00
|
|
|
BSD 2-Clause License (http://www.opensource.org/licenses/bsd-license.php)
|
|
|
|
|
|
|
|
Redistribution and use in source and binary forms, with or without
|
|
|
|
modification, are permitted provided that the following conditions are
|
|
|
|
met:
|
|
|
|
|
|
|
|
* Redistributions of source code must retain the above copyright
|
|
|
|
notice, this list of conditions and the following disclaimer.
|
|
|
|
* Redistributions in binary form must reproduce the above
|
|
|
|
copyright notice, this list of conditions and the following disclaimer
|
|
|
|
in the documentation and/or other materials provided with the
|
|
|
|
distribution.
|
|
|
|
|
|
|
|
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
|
|
|
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
|
|
|
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
|
|
|
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
|
|
|
OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
|
|
|
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
|
|
|
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|
|
|
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|
|
|
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
|
|
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
|
|
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
|
|
|
|
You can contact the author at :
|
2015-03-13 01:24:08 +00:00
|
|
|
- LZ4 source repository : https://github.com/Cyan4973/lz4
|
2014-10-27 23:39:06 +00:00
|
|
|
- LZ4 public forum : https://groups.google.com/forum/#!forum/lz4c
|
2014-08-29 15:35:13 +00:00
|
|
|
*/
|
|
|
|
|
2014-10-27 23:39:06 +00:00
|
|
|
/* LZ4F is a stand-alone API to create LZ4-compressed Frames
|
2015-03-18 20:38:27 +00:00
|
|
|
* in full conformance with specification v1.5.0
|
2015-03-10 15:57:42 +00:00
|
|
|
* All related operations, including memory management, are handled by the library.
|
2014-10-27 23:39:06 +00:00
|
|
|
* */
|
2014-08-29 15:35:13 +00:00
|
|
|
|
|
|
|
|
|
|
|
/**************************************
|
2014-10-27 23:39:06 +00:00
|
|
|
Compiler Options
|
2014-08-29 15:35:13 +00:00
|
|
|
**************************************/
|
|
|
|
#ifdef _MSC_VER /* Visual Studio */
|
|
|
|
# pragma warning(disable : 4127) /* disable: C4127: conditional expression is constant */
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
|
|
/**************************************
|
2015-03-10 15:57:42 +00:00
|
|
|
* Memory routines
|
2014-08-29 15:35:13 +00:00
|
|
|
**************************************/
|
|
|
|
#include <stdlib.h> /* malloc, calloc, free */
|
2014-09-03 18:49:59 +00:00
|
|
|
#define ALLOCATOR(s) calloc(1,s)
|
2014-08-29 15:35:13 +00:00
|
|
|
#define FREEMEM free
|
2014-09-10 21:17:03 +00:00
|
|
|
#include <string.h> /* memset, memcpy, memmove */
|
2014-08-29 15:35:13 +00:00
|
|
|
#define MEM_INIT memset
|
|
|
|
|
|
|
|
|
|
|
|
/**************************************
|
2015-03-10 15:57:42 +00:00
|
|
|
* Includes
|
2014-08-29 15:35:13 +00:00
|
|
|
**************************************/
|
2014-12-13 14:05:46 +00:00
|
|
|
#include "lz4frame_static.h"
|
2014-08-29 15:35:13 +00:00
|
|
|
#include "lz4.h"
|
|
|
|
#include "lz4hc.h"
|
|
|
|
#include "xxhash.h"
|
|
|
|
|
|
|
|
|
|
|
|
/**************************************
|
2015-03-10 15:57:42 +00:00
|
|
|
* Basic Types
|
2014-08-29 15:35:13 +00:00
|
|
|
**************************************/
|
|
|
|
#if defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L) /* C99 */
|
|
|
|
# include <stdint.h>
|
2014-09-22 17:42:00 +00:00
|
|
|
typedef uint8_t BYTE;
|
|
|
|
typedef uint16_t U16;
|
|
|
|
typedef uint32_t U32;
|
|
|
|
typedef int32_t S32;
|
|
|
|
typedef uint64_t U64;
|
2014-08-29 15:35:13 +00:00
|
|
|
#else
|
2014-09-22 17:42:00 +00:00
|
|
|
typedef unsigned char BYTE;
|
|
|
|
typedef unsigned short U16;
|
|
|
|
typedef unsigned int U32;
|
|
|
|
typedef signed int S32;
|
|
|
|
typedef unsigned long long U64;
|
2014-08-29 15:35:13 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
|
|
/**************************************
|
2015-03-10 15:57:42 +00:00
|
|
|
* Constants
|
2014-08-29 15:35:13 +00:00
|
|
|
**************************************/
|
|
|
|
#define KB *(1<<10)
|
|
|
|
#define MB *(1<<20)
|
|
|
|
#define GB *(1<<30)
|
|
|
|
|
2014-08-30 17:14:44 +00:00
|
|
|
#define _1BIT 0x01
|
|
|
|
#define _2BITS 0x03
|
|
|
|
#define _3BITS 0x07
|
|
|
|
#define _4BITS 0x0F
|
|
|
|
#define _8BITS 0xFF
|
|
|
|
|
2015-03-16 16:52:14 +00:00
|
|
|
#define LZ4F_MAGIC_SKIPPABLE_START 0x184D2A50U
|
2014-08-30 17:14:44 +00:00
|
|
|
#define LZ4F_MAGICNUMBER 0x184D2204U
|
|
|
|
#define LZ4F_BLOCKUNCOMPRESSED_FLAG 0x80000000U
|
2015-04-14 17:20:47 +00:00
|
|
|
#define LZ4F_BLOCKSIZEID_DEFAULT LZ4F_max64KB
|
2014-08-29 15:38:26 +00:00
|
|
|
|
2015-04-19 14:23:53 +00:00
|
|
|
static const size_t minFHSize = 7;
|
|
|
|
static const size_t maxFHSize = 15;
|
|
|
|
static const size_t BHSize = 4;
|
2014-10-27 23:39:06 +00:00
|
|
|
static const U32 minHClevel = 3;
|
2014-08-29 15:35:13 +00:00
|
|
|
|
|
|
|
/**************************************
|
2015-03-10 15:57:42 +00:00
|
|
|
* Structures and local types
|
2014-08-29 15:35:13 +00:00
|
|
|
**************************************/
|
2014-09-22 17:42:00 +00:00
|
|
|
typedef struct
|
|
|
|
{
|
|
|
|
LZ4F_preferences_t prefs;
|
2015-03-18 20:38:27 +00:00
|
|
|
U32 version;
|
|
|
|
U32 cStage;
|
2014-09-22 17:42:00 +00:00
|
|
|
size_t maxBlockSize;
|
|
|
|
size_t maxBufferSize;
|
|
|
|
BYTE* tmpBuff;
|
|
|
|
BYTE* tmpIn;
|
|
|
|
size_t tmpInSize;
|
2015-03-18 20:38:27 +00:00
|
|
|
U64 totalInSize;
|
2014-10-05 23:48:45 +00:00
|
|
|
XXH32_state_t xxh;
|
2015-03-18 20:38:27 +00:00
|
|
|
void* lz4CtxPtr;
|
|
|
|
U32 lz4CtxLevel; /* 0: unallocated; 1: LZ4_stream_t; 3: LZ4_streamHC_t */
|
2014-08-30 17:14:44 +00:00
|
|
|
} LZ4F_cctx_internal_t;
|
2014-08-29 15:35:13 +00:00
|
|
|
|
2014-09-22 17:42:00 +00:00
|
|
|
typedef struct
|
|
|
|
{
|
2014-09-05 14:50:06 +00:00
|
|
|
LZ4F_frameInfo_t frameInfo;
|
2015-03-18 20:38:27 +00:00
|
|
|
U32 version;
|
|
|
|
U32 dStage;
|
2015-04-09 21:59:07 +00:00
|
|
|
U64 frameRemainingSize;
|
2014-09-22 17:42:00 +00:00
|
|
|
size_t maxBlockSize;
|
|
|
|
size_t maxBufferSize;
|
|
|
|
const BYTE* srcExpect;
|
|
|
|
BYTE* tmpIn;
|
|
|
|
size_t tmpInSize;
|
|
|
|
size_t tmpInTarget;
|
|
|
|
BYTE* tmpOutBuffer;
|
|
|
|
BYTE* dict;
|
|
|
|
size_t dictSize;
|
|
|
|
BYTE* tmpOut;
|
|
|
|
size_t tmpOutSize;
|
|
|
|
size_t tmpOutStart;
|
2014-10-05 23:48:45 +00:00
|
|
|
XXH32_state_t xxh;
|
2015-03-18 20:38:27 +00:00
|
|
|
BYTE header[16];
|
2014-09-05 14:50:06 +00:00
|
|
|
} LZ4F_dctx_internal_t;
|
|
|
|
|
2014-08-29 15:35:13 +00:00
|
|
|
|
|
|
|
/**************************************
|
2015-03-10 15:57:42 +00:00
|
|
|
* Error management
|
2014-09-21 08:56:21 +00:00
|
|
|
**************************************/
|
|
|
|
#define LZ4F_GENERATE_STRING(STRING) #STRING,
|
|
|
|
static const char* LZ4F_errorStrings[] = { LZ4F_LIST_ERRORS(LZ4F_GENERATE_STRING) };
|
|
|
|
|
|
|
|
|
2015-03-18 20:38:27 +00:00
|
|
|
unsigned LZ4F_isError(LZ4F_errorCode_t code)
|
2014-09-21 08:56:21 +00:00
|
|
|
{
|
2015-04-14 17:20:47 +00:00
|
|
|
return (code > (LZ4F_errorCode_t)(-LZ4F_ERROR_maxCode));
|
2014-09-21 08:56:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const char* LZ4F_getErrorName(LZ4F_errorCode_t code)
|
|
|
|
{
|
|
|
|
static const char* codeError = "Unspecified error code";
|
2014-09-22 16:38:17 +00:00
|
|
|
if (LZ4F_isError(code)) return LZ4F_errorStrings[-(int)(code)];
|
2014-09-21 08:56:21 +00:00
|
|
|
return codeError;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-08-29 15:35:13 +00:00
|
|
|
/**************************************
|
2015-03-10 15:57:42 +00:00
|
|
|
* Private functions
|
2014-08-29 15:35:13 +00:00
|
|
|
**************************************/
|
|
|
|
static size_t LZ4F_getBlockSize(unsigned blockSizeID)
|
|
|
|
{
|
2014-09-22 17:42:00 +00:00
|
|
|
static const size_t blockSizes[4] = { 64 KB, 256 KB, 1 MB, 4 MB };
|
2014-08-30 17:14:44 +00:00
|
|
|
|
2014-09-22 17:42:00 +00:00
|
|
|
if (blockSizeID == 0) blockSizeID = LZ4F_BLOCKSIZEID_DEFAULT;
|
|
|
|
blockSizeID -= 4;
|
2015-04-14 17:20:47 +00:00
|
|
|
if (blockSizeID > 3) return (size_t)-LZ4F_ERROR_maxBlockSize_invalid;
|
2014-09-22 17:42:00 +00:00
|
|
|
return blockSizes[blockSizeID];
|
2014-08-29 15:35:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-08-30 17:14:44 +00:00
|
|
|
/* unoptimized version; solves endianess & alignment issues */
|
2015-03-18 20:38:27 +00:00
|
|
|
static U32 LZ4F_readLE32 (const BYTE* srcPtr)
|
|
|
|
{
|
|
|
|
U32 value32 = srcPtr[0];
|
|
|
|
value32 += (srcPtr[1]<<8);
|
|
|
|
value32 += (srcPtr[2]<<16);
|
2015-04-09 21:59:07 +00:00
|
|
|
value32 += ((U32)srcPtr[3])<<24;
|
2015-03-18 20:38:27 +00:00
|
|
|
return value32;
|
|
|
|
}
|
|
|
|
|
2014-08-30 17:14:44 +00:00
|
|
|
static void LZ4F_writeLE32 (BYTE* dstPtr, U32 value32)
|
|
|
|
{
|
2014-09-22 17:42:00 +00:00
|
|
|
dstPtr[0] = (BYTE)value32;
|
|
|
|
dstPtr[1] = (BYTE)(value32 >> 8);
|
|
|
|
dstPtr[2] = (BYTE)(value32 >> 16);
|
|
|
|
dstPtr[3] = (BYTE)(value32 >> 24);
|
2014-08-30 17:14:44 +00:00
|
|
|
}
|
|
|
|
|
2015-03-18 20:38:27 +00:00
|
|
|
static U64 LZ4F_readLE64 (const BYTE* srcPtr)
|
2014-09-01 21:44:02 +00:00
|
|
|
{
|
2015-03-18 20:38:27 +00:00
|
|
|
U64 value64 = srcPtr[0];
|
|
|
|
value64 += (srcPtr[1]<<8);
|
|
|
|
value64 += (srcPtr[2]<<16);
|
2015-04-01 16:30:36 +00:00
|
|
|
value64 += ((U64)srcPtr[3]<<24);
|
2015-03-18 20:38:27 +00:00
|
|
|
value64 += ((U64)srcPtr[4]<<32);
|
|
|
|
value64 += ((U64)srcPtr[5]<<40);
|
|
|
|
value64 += ((U64)srcPtr[6]<<48);
|
|
|
|
value64 += ((U64)srcPtr[7]<<56);
|
|
|
|
return value64;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void LZ4F_writeLE64 (BYTE* dstPtr, U64 value64)
|
|
|
|
{
|
|
|
|
dstPtr[0] = (BYTE)value64;
|
|
|
|
dstPtr[1] = (BYTE)(value64 >> 8);
|
|
|
|
dstPtr[2] = (BYTE)(value64 >> 16);
|
|
|
|
dstPtr[3] = (BYTE)(value64 >> 24);
|
|
|
|
dstPtr[4] = (BYTE)(value64 >> 32);
|
|
|
|
dstPtr[5] = (BYTE)(value64 >> 40);
|
|
|
|
dstPtr[6] = (BYTE)(value64 >> 48);
|
|
|
|
dstPtr[7] = (BYTE)(value64 >> 56);
|
2014-09-01 21:44:02 +00:00
|
|
|
}
|
2014-08-30 17:14:44 +00:00
|
|
|
|
2014-09-01 21:44:02 +00:00
|
|
|
|
2015-03-18 20:38:27 +00:00
|
|
|
static BYTE LZ4F_headerChecksum (const void* header, size_t length)
|
2014-08-30 17:14:44 +00:00
|
|
|
{
|
2015-03-18 20:38:27 +00:00
|
|
|
U32 xxh = XXH32(header, length, 0);
|
2014-09-22 17:42:00 +00:00
|
|
|
return (BYTE)(xxh >> 8);
|
2014-08-30 17:14:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-08-29 15:35:13 +00:00
|
|
|
/**************************************
|
2015-03-10 15:57:42 +00:00
|
|
|
* Simple compression functions
|
2014-08-29 15:35:13 +00:00
|
|
|
**************************************/
|
2015-04-14 17:20:47 +00:00
|
|
|
static LZ4F_blockSizeID_t LZ4F_optimalBSID(const LZ4F_blockSizeID_t requestedBSID, const size_t srcSize)
|
2015-03-16 21:35:02 +00:00
|
|
|
{
|
2015-04-14 17:20:47 +00:00
|
|
|
LZ4F_blockSizeID_t proposedBSID = LZ4F_max64KB;
|
2015-03-16 21:35:02 +00:00
|
|
|
size_t maxBlockSize = 64 KB;
|
|
|
|
while (requestedBSID > proposedBSID)
|
|
|
|
{
|
|
|
|
if (srcSize <= maxBlockSize)
|
|
|
|
return proposedBSID;
|
2015-04-14 17:20:47 +00:00
|
|
|
proposedBSID = (LZ4F_blockSizeID_t)((int)proposedBSID + 1);
|
2015-03-16 21:35:02 +00:00
|
|
|
maxBlockSize <<= 2;
|
|
|
|
}
|
|
|
|
return requestedBSID;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-09-12 18:28:44 +00:00
|
|
|
size_t LZ4F_compressFrameBound(size_t srcSize, const LZ4F_preferences_t* preferencesPtr)
|
2014-08-29 15:35:13 +00:00
|
|
|
{
|
2015-03-10 15:57:42 +00:00
|
|
|
LZ4F_preferences_t prefs;
|
2014-09-22 17:42:00 +00:00
|
|
|
size_t headerSize;
|
|
|
|
size_t streamSize;
|
2014-08-30 17:14:44 +00:00
|
|
|
|
2014-09-22 17:42:00 +00:00
|
|
|
if (preferencesPtr!=NULL) prefs = *preferencesPtr;
|
2015-03-16 16:52:14 +00:00
|
|
|
else memset(&prefs, 0, sizeof(prefs));
|
|
|
|
|
2015-03-16 21:35:02 +00:00
|
|
|
prefs.frameInfo.blockSizeID = LZ4F_optimalBSID(prefs.frameInfo.blockSizeID, srcSize);
|
2014-09-22 17:42:00 +00:00
|
|
|
prefs.autoFlush = 1;
|
2014-08-30 17:14:44 +00:00
|
|
|
|
2015-04-19 14:23:53 +00:00
|
|
|
headerSize = maxFHSize; /* header size, including magic number and frame content size*/
|
2014-09-22 17:42:00 +00:00
|
|
|
streamSize = LZ4F_compressBound(srcSize, &prefs);
|
2014-08-30 17:14:44 +00:00
|
|
|
|
2014-09-22 17:42:00 +00:00
|
|
|
return headerSize + streamSize;
|
2014-08-29 15:35:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* LZ4F_compressFrame()
|
2015-03-16 21:35:02 +00:00
|
|
|
* Compress an entire srcBuffer into a valid LZ4 frame, as defined by specification v1.5.0, in a single step.
|
2014-10-27 23:39:06 +00:00
|
|
|
* The most important rule is that dstBuffer MUST be large enough (dstMaxSize) to ensure compression completion even in worst case.
|
|
|
|
* You can get the minimum value of dstMaxSize by using LZ4F_compressFrameBound()
|
|
|
|
* If this condition is not respected, LZ4F_compressFrame() will fail (result is an errorCode)
|
2015-03-16 21:35:02 +00:00
|
|
|
* The LZ4F_preferences_t structure is optional : you can provide NULL as argument. All preferences will then be set to default.
|
2014-10-27 23:39:06 +00:00
|
|
|
* The result of the function is the number of bytes written into dstBuffer.
|
|
|
|
* The function outputs an error code if it fails (can be tested using LZ4F_isError())
|
|
|
|
*/
|
2014-08-29 15:35:13 +00:00
|
|
|
size_t LZ4F_compressFrame(void* dstBuffer, size_t dstMaxSize, const void* srcBuffer, size_t srcSize, const LZ4F_preferences_t* preferencesPtr)
|
|
|
|
{
|
2015-03-10 15:57:42 +00:00
|
|
|
LZ4F_cctx_internal_t cctxI;
|
2015-03-16 22:22:59 +00:00
|
|
|
LZ4_stream_t lz4ctx;
|
2015-03-10 15:57:42 +00:00
|
|
|
LZ4F_preferences_t prefs;
|
|
|
|
LZ4F_compressOptions_t options;
|
2014-09-22 17:42:00 +00:00
|
|
|
LZ4F_errorCode_t errorCode;
|
|
|
|
BYTE* const dstStart = (BYTE*) dstBuffer;
|
|
|
|
BYTE* dstPtr = dstStart;
|
|
|
|
BYTE* const dstEnd = dstStart + dstMaxSize;
|
2014-08-30 17:14:44 +00:00
|
|
|
|
2015-03-10 15:57:42 +00:00
|
|
|
memset(&cctxI, 0, sizeof(cctxI)); /* works because no allocation */
|
|
|
|
memset(&options, 0, sizeof(options));
|
2014-08-29 15:35:13 +00:00
|
|
|
|
2014-09-13 09:08:55 +00:00
|
|
|
cctxI.version = LZ4F_VERSION;
|
2014-09-22 01:59:42 +00:00
|
|
|
cctxI.maxBufferSize = 5 MB; /* mess with real buffer size to prevent allocation; works because autoflush==1 & stableSrc==1 */
|
2014-09-22 01:36:20 +00:00
|
|
|
|
|
|
|
if (preferencesPtr!=NULL) prefs = *preferencesPtr;
|
2015-03-18 20:38:27 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
memset(&prefs, 0, sizeof(prefs));
|
2015-03-29 10:20:09 +00:00
|
|
|
prefs.frameInfo.contentSize = (U64)srcSize;
|
2015-03-18 20:38:27 +00:00
|
|
|
}
|
2015-03-29 10:20:09 +00:00
|
|
|
if (prefs.frameInfo.contentSize != 0)
|
2015-04-09 21:59:07 +00:00
|
|
|
prefs.frameInfo.contentSize = (U64)srcSize; /* auto-correct content size if selected (!=0) */
|
2015-03-16 16:52:14 +00:00
|
|
|
|
2015-03-16 22:22:59 +00:00
|
|
|
if (prefs.compressionLevel < minHClevel)
|
|
|
|
{
|
|
|
|
cctxI.lz4CtxPtr = &lz4ctx;
|
|
|
|
cctxI.lz4CtxLevel = 1;
|
|
|
|
}
|
|
|
|
|
2015-03-16 21:35:02 +00:00
|
|
|
prefs.frameInfo.blockSizeID = LZ4F_optimalBSID(prefs.frameInfo.blockSizeID, srcSize);
|
2014-09-12 18:28:44 +00:00
|
|
|
prefs.autoFlush = 1;
|
2014-09-22 01:36:20 +00:00
|
|
|
if (srcSize <= LZ4F_getBlockSize(prefs.frameInfo.blockSizeID))
|
2015-04-14 17:20:47 +00:00
|
|
|
prefs.frameInfo.blockMode = LZ4F_blockIndependent; /* no need for linked blocks */
|
2014-09-22 01:36:20 +00:00
|
|
|
|
2014-09-13 09:08:55 +00:00
|
|
|
options.stableSrc = 1;
|
2014-09-12 18:28:44 +00:00
|
|
|
|
2014-09-22 17:42:00 +00:00
|
|
|
if (dstMaxSize < LZ4F_compressFrameBound(srcSize, &prefs))
|
2015-04-14 17:20:47 +00:00
|
|
|
return (size_t)-LZ4F_ERROR_dstMaxSize_tooSmall;
|
2014-08-30 17:14:44 +00:00
|
|
|
|
2014-09-22 17:42:00 +00:00
|
|
|
errorCode = LZ4F_compressBegin(&cctxI, dstBuffer, dstMaxSize, &prefs); /* write header */
|
|
|
|
if (LZ4F_isError(errorCode)) return errorCode;
|
|
|
|
dstPtr += errorCode; /* header size */
|
2014-08-30 17:14:44 +00:00
|
|
|
|
2015-03-16 22:22:59 +00:00
|
|
|
errorCode = LZ4F_compressUpdate(&cctxI, dstPtr, dstEnd-dstPtr, srcBuffer, srcSize, &options);
|
2014-09-22 17:42:00 +00:00
|
|
|
if (LZ4F_isError(errorCode)) return errorCode;
|
|
|
|
dstPtr += errorCode;
|
2014-08-30 17:14:44 +00:00
|
|
|
|
2014-09-22 17:42:00 +00:00
|
|
|
errorCode = LZ4F_compressEnd(&cctxI, dstPtr, dstEnd-dstPtr, &options); /* flush last block, and generate suffix */
|
|
|
|
if (LZ4F_isError(errorCode)) return errorCode;
|
|
|
|
dstPtr += errorCode;
|
2014-08-29 15:35:13 +00:00
|
|
|
|
2015-03-16 22:22:59 +00:00
|
|
|
if (prefs.compressionLevel >= minHClevel) /* no allocation necessary with lz4 fast */
|
|
|
|
FREEMEM(cctxI.lz4CtxPtr);
|
2014-10-27 23:39:06 +00:00
|
|
|
|
2014-09-22 17:42:00 +00:00
|
|
|
return (dstPtr - dstStart);
|
2014-08-29 15:35:13 +00:00
|
|
|
}
|
2014-08-30 17:14:44 +00:00
|
|
|
|
|
|
|
|
2014-09-03 18:49:59 +00:00
|
|
|
/***********************************
|
2014-10-27 23:39:06 +00:00
|
|
|
* Advanced compression functions
|
|
|
|
* *********************************/
|
2014-08-30 17:14:44 +00:00
|
|
|
|
|
|
|
/* LZ4F_createCompressionContext() :
|
2014-10-27 23:39:06 +00:00
|
|
|
* The first thing to do is to create a compressionContext object, which will be used in all compression operations.
|
|
|
|
* This is achieved using LZ4F_createCompressionContext(), which takes as argument a version and an LZ4F_preferences_t structure.
|
|
|
|
* The version provided MUST be LZ4F_VERSION. It is intended to track potential version differences between different binaries.
|
|
|
|
* The function will provide a pointer to an allocated LZ4F_compressionContext_t object.
|
|
|
|
* If the result LZ4F_errorCode_t is not OK_NoError, there was an error during context creation.
|
|
|
|
* Object can release its memory using LZ4F_freeCompressionContext();
|
|
|
|
*/
|
2014-09-07 11:57:09 +00:00
|
|
|
LZ4F_errorCode_t LZ4F_createCompressionContext(LZ4F_compressionContext_t* LZ4F_compressionContextPtr, unsigned version)
|
2014-08-30 17:14:44 +00:00
|
|
|
{
|
2014-09-22 17:42:00 +00:00
|
|
|
LZ4F_cctx_internal_t* cctxPtr;
|
2014-08-30 17:14:44 +00:00
|
|
|
|
2014-10-27 23:39:06 +00:00
|
|
|
cctxPtr = (LZ4F_cctx_internal_t*)ALLOCATOR(sizeof(LZ4F_cctx_internal_t));
|
2015-04-14 17:20:47 +00:00
|
|
|
if (cctxPtr==NULL) return (LZ4F_errorCode_t)(-LZ4F_ERROR_allocation_failed);
|
2014-08-30 17:14:44 +00:00
|
|
|
|
2014-09-22 17:42:00 +00:00
|
|
|
cctxPtr->version = version;
|
|
|
|
cctxPtr->cStage = 0; /* Next stage : write header */
|
2014-08-30 17:14:44 +00:00
|
|
|
|
2014-09-22 17:42:00 +00:00
|
|
|
*LZ4F_compressionContextPtr = (LZ4F_compressionContext_t)cctxPtr;
|
2014-08-30 17:14:44 +00:00
|
|
|
|
2015-04-14 17:20:47 +00:00
|
|
|
return LZ4F_OK_NoError;
|
2014-08-30 17:14:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
LZ4F_errorCode_t LZ4F_freeCompressionContext(LZ4F_compressionContext_t LZ4F_compressionContext)
|
|
|
|
{
|
2014-09-22 17:42:00 +00:00
|
|
|
LZ4F_cctx_internal_t* cctxPtr = (LZ4F_cctx_internal_t*)LZ4F_compressionContext;
|
2014-08-30 17:14:44 +00:00
|
|
|
|
2015-04-12 07:21:35 +00:00
|
|
|
if (cctxPtr != NULL) /* null pointers can be safely provided to this function, like free() */
|
|
|
|
{
|
|
|
|
FREEMEM(cctxPtr->lz4CtxPtr);
|
|
|
|
FREEMEM(cctxPtr->tmpBuff);
|
|
|
|
FREEMEM(LZ4F_compressionContext);
|
|
|
|
}
|
2014-08-30 17:14:44 +00:00
|
|
|
|
2015-04-14 17:20:47 +00:00
|
|
|
return LZ4F_OK_NoError;
|
2014-08-30 17:14:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* LZ4F_compressBegin() :
|
2014-10-27 23:39:06 +00:00
|
|
|
* will write the frame header into dstBuffer.
|
|
|
|
* dstBuffer must be large enough to accommodate a header (dstMaxSize). Maximum header size is LZ4F_MAXHEADERFRAME_SIZE bytes.
|
|
|
|
* The result of the function is the number of bytes written into dstBuffer for the header
|
|
|
|
* or an error code (can be tested using LZ4F_isError())
|
|
|
|
*/
|
2014-09-07 11:57:09 +00:00
|
|
|
size_t LZ4F_compressBegin(LZ4F_compressionContext_t compressionContext, void* dstBuffer, size_t dstMaxSize, const LZ4F_preferences_t* preferencesPtr)
|
2014-08-30 17:14:44 +00:00
|
|
|
{
|
2015-03-10 15:57:42 +00:00
|
|
|
LZ4F_preferences_t prefNull;
|
2014-09-22 17:42:00 +00:00
|
|
|
LZ4F_cctx_internal_t* cctxPtr = (LZ4F_cctx_internal_t*)compressionContext;
|
|
|
|
BYTE* const dstStart = (BYTE*)dstBuffer;
|
|
|
|
BYTE* dstPtr = dstStart;
|
|
|
|
BYTE* headerStart;
|
|
|
|
size_t requiredBuffSize;
|
|
|
|
|
2015-04-19 14:23:53 +00:00
|
|
|
if (dstMaxSize < maxFHSize) return (size_t)-LZ4F_ERROR_dstMaxSize_tooSmall;
|
2015-04-14 17:20:47 +00:00
|
|
|
if (cctxPtr->cStage != 0) return (size_t)-LZ4F_ERROR_GENERIC;
|
2015-03-10 15:57:42 +00:00
|
|
|
memset(&prefNull, 0, sizeof(prefNull));
|
2014-09-22 17:42:00 +00:00
|
|
|
if (preferencesPtr == NULL) preferencesPtr = &prefNull;
|
2014-10-27 23:39:06 +00:00
|
|
|
cctxPtr->prefs = *preferencesPtr;
|
|
|
|
|
|
|
|
/* ctx Management */
|
|
|
|
{
|
2015-03-16 22:22:59 +00:00
|
|
|
U32 tableID = cctxPtr->prefs.compressionLevel<minHClevel ? 1 : 2; /* 0:nothing ; 1:LZ4 table ; 2:HC tables */
|
|
|
|
if (cctxPtr->lz4CtxLevel < tableID)
|
2014-10-27 23:39:06 +00:00
|
|
|
{
|
|
|
|
FREEMEM(cctxPtr->lz4CtxPtr);
|
|
|
|
if (cctxPtr->prefs.compressionLevel<minHClevel)
|
|
|
|
cctxPtr->lz4CtxPtr = (void*)LZ4_createStream();
|
|
|
|
else
|
|
|
|
cctxPtr->lz4CtxPtr = (void*)LZ4_createStreamHC();
|
2015-03-16 22:22:59 +00:00
|
|
|
cctxPtr->lz4CtxLevel = tableID;
|
2014-10-27 23:39:06 +00:00
|
|
|
}
|
|
|
|
}
|
2014-09-22 17:42:00 +00:00
|
|
|
|
|
|
|
/* Buffer Management */
|
|
|
|
if (cctxPtr->prefs.frameInfo.blockSizeID == 0) cctxPtr->prefs.frameInfo.blockSizeID = LZ4F_BLOCKSIZEID_DEFAULT;
|
|
|
|
cctxPtr->maxBlockSize = LZ4F_getBlockSize(cctxPtr->prefs.frameInfo.blockSizeID);
|
|
|
|
|
2015-04-14 17:20:47 +00:00
|
|
|
requiredBuffSize = cctxPtr->maxBlockSize + ((cctxPtr->prefs.frameInfo.blockMode == LZ4F_blockLinked) * 128 KB);
|
2014-09-22 17:42:00 +00:00
|
|
|
if (preferencesPtr->autoFlush)
|
2015-04-14 17:20:47 +00:00
|
|
|
requiredBuffSize = (cctxPtr->prefs.frameInfo.blockMode == LZ4F_blockLinked) * 64 KB; /* just needs dict */
|
2014-09-12 18:28:44 +00:00
|
|
|
|
2014-09-22 17:42:00 +00:00
|
|
|
if (cctxPtr->maxBufferSize < requiredBuffSize)
|
2014-09-07 11:57:09 +00:00
|
|
|
{
|
2014-09-11 21:27:14 +00:00
|
|
|
cctxPtr->maxBufferSize = requiredBuffSize;
|
2014-09-09 22:54:22 +00:00
|
|
|
FREEMEM(cctxPtr->tmpBuff);
|
2014-10-27 23:39:06 +00:00
|
|
|
cctxPtr->tmpBuff = (BYTE*)ALLOCATOR(requiredBuffSize);
|
2015-04-14 17:20:47 +00:00
|
|
|
if (cctxPtr->tmpBuff == NULL) return (size_t)-LZ4F_ERROR_allocation_failed;
|
2014-09-07 11:57:09 +00:00
|
|
|
}
|
2014-09-11 21:27:14 +00:00
|
|
|
cctxPtr->tmpIn = cctxPtr->tmpBuff;
|
2014-09-07 11:57:09 +00:00
|
|
|
cctxPtr->tmpInSize = 0;
|
2014-10-05 23:48:45 +00:00
|
|
|
XXH32_reset(&(cctxPtr->xxh), 0);
|
2015-03-16 22:22:59 +00:00
|
|
|
if (cctxPtr->prefs.compressionLevel < minHClevel)
|
2014-10-27 23:39:06 +00:00
|
|
|
LZ4_resetStream((LZ4_stream_t*)(cctxPtr->lz4CtxPtr));
|
|
|
|
else
|
|
|
|
LZ4_resetStreamHC((LZ4_streamHC_t*)(cctxPtr->lz4CtxPtr), cctxPtr->prefs.compressionLevel);
|
2014-08-30 17:14:44 +00:00
|
|
|
|
2014-09-22 17:42:00 +00:00
|
|
|
/* Magic Number */
|
|
|
|
LZ4F_writeLE32(dstPtr, LZ4F_MAGICNUMBER);
|
|
|
|
dstPtr += 4;
|
|
|
|
headerStart = dstPtr;
|
2014-08-30 17:14:44 +00:00
|
|
|
|
2014-09-22 17:42:00 +00:00
|
|
|
/* FLG Byte */
|
2015-04-12 14:17:10 +00:00
|
|
|
*dstPtr++ = (BYTE)(((1 & _2BITS) << 6) /* Version('01') */
|
2014-10-27 23:39:06 +00:00
|
|
|
+ ((cctxPtr->prefs.frameInfo.blockMode & _1BIT ) << 5) /* Block mode */
|
2015-04-12 14:17:10 +00:00
|
|
|
+ ((cctxPtr->prefs.frameInfo.contentChecksumFlag & _1BIT ) << 2) /* Frame checksum */
|
|
|
|
+ ((cctxPtr->prefs.frameInfo.contentSize > 0) << 3)); /* Frame content size */
|
2014-09-22 17:42:00 +00:00
|
|
|
/* BD Byte */
|
2015-03-18 20:38:27 +00:00
|
|
|
*dstPtr++ = (BYTE)((cctxPtr->prefs.frameInfo.blockSizeID & _3BITS) << 4);
|
|
|
|
/* Optional Frame content size field */
|
2015-03-29 10:20:09 +00:00
|
|
|
if (cctxPtr->prefs.frameInfo.contentSize)
|
2015-03-18 20:38:27 +00:00
|
|
|
{
|
2015-03-29 10:20:09 +00:00
|
|
|
LZ4F_writeLE64(dstPtr, cctxPtr->prefs.frameInfo.contentSize);
|
2015-03-18 20:38:27 +00:00
|
|
|
dstPtr += 8;
|
|
|
|
cctxPtr->totalInSize = 0;
|
|
|
|
}
|
2014-09-22 17:42:00 +00:00
|
|
|
/* CRC Byte */
|
2015-03-18 20:38:27 +00:00
|
|
|
*dstPtr = LZ4F_headerChecksum(headerStart, dstPtr - headerStart);
|
|
|
|
dstPtr++;
|
2014-08-30 17:14:44 +00:00
|
|
|
|
2015-03-18 20:38:27 +00:00
|
|
|
cctxPtr->cStage = 1; /* header written, now request input data block */
|
2014-08-30 17:14:44 +00:00
|
|
|
|
2014-09-22 17:42:00 +00:00
|
|
|
return (dstPtr - dstStart);
|
2014-08-30 17:14:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* LZ4F_compressBound() : gives the size of Dst buffer given a srcSize to handle worst case situations.
|
2014-10-27 23:39:06 +00:00
|
|
|
* The LZ4F_frameInfo_t structure is optional :
|
2015-04-14 17:51:36 +00:00
|
|
|
* you can provide NULL as argument, preferences will then be set to cover worst case situations.
|
2014-10-27 23:39:06 +00:00
|
|
|
* */
|
2014-09-10 21:17:03 +00:00
|
|
|
size_t LZ4F_compressBound(size_t srcSize, const LZ4F_preferences_t* preferencesPtr)
|
2014-08-30 17:14:44 +00:00
|
|
|
{
|
2015-03-11 17:38:44 +00:00
|
|
|
LZ4F_preferences_t prefsNull;
|
|
|
|
memset(&prefsNull, 0, sizeof(prefsNull));
|
2015-04-15 09:34:08 +00:00
|
|
|
prefsNull.frameInfo.contentChecksumFlag = LZ4F_contentChecksumEnabled; /* worst case */
|
2015-03-11 17:38:44 +00:00
|
|
|
{
|
|
|
|
const LZ4F_preferences_t* prefsPtr = (preferencesPtr==NULL) ? &prefsNull : preferencesPtr;
|
2015-04-14 17:20:47 +00:00
|
|
|
LZ4F_blockSizeID_t bid = prefsPtr->frameInfo.blockSizeID;
|
2015-03-11 17:38:44 +00:00
|
|
|
size_t blockSize = LZ4F_getBlockSize(bid);
|
|
|
|
unsigned nbBlocks = (unsigned)(srcSize / blockSize) + 1;
|
|
|
|
size_t lastBlockSize = prefsPtr->autoFlush ? srcSize % blockSize : blockSize;
|
|
|
|
size_t blockInfo = 4; /* default, without block CRC option */
|
|
|
|
size_t frameEnd = 4 + (prefsPtr->frameInfo.contentChecksumFlag*4);
|
|
|
|
|
|
|
|
return (blockInfo * nbBlocks) + (blockSize * (nbBlocks-1)) + lastBlockSize + frameEnd;;
|
|
|
|
}
|
2014-08-30 17:14:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-10-27 23:39:06 +00:00
|
|
|
typedef int (*compressFunc_t)(void* ctx, const char* src, char* dst, int srcSize, int dstSize, int level);
|
2014-09-14 23:59:30 +00:00
|
|
|
|
2014-10-27 23:39:06 +00:00
|
|
|
static size_t LZ4F_compressBlock(void* dst, const void* src, size_t srcSize, compressFunc_t compress, void* lz4ctx, int level)
|
2014-09-14 23:59:30 +00:00
|
|
|
{
|
|
|
|
/* compress one block */
|
|
|
|
BYTE* cSizePtr = (BYTE*)dst;
|
|
|
|
U32 cSize;
|
2014-10-27 23:39:06 +00:00
|
|
|
cSize = (U32)compress(lz4ctx, (const char*)src, (char*)(cSizePtr+4), (int)(srcSize), (int)(srcSize-1), level);
|
2014-09-14 23:59:30 +00:00
|
|
|
LZ4F_writeLE32(cSizePtr, cSize);
|
|
|
|
if (cSize == 0) /* compression failed */
|
|
|
|
{
|
2014-09-22 16:38:17 +00:00
|
|
|
cSize = (U32)srcSize;
|
|
|
|
LZ4F_writeLE32(cSizePtr, cSize + LZ4F_BLOCKUNCOMPRESSED_FLAG);
|
2014-09-14 23:59:30 +00:00
|
|
|
memcpy(cSizePtr+4, src, srcSize);
|
|
|
|
}
|
|
|
|
return cSize + 4;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-10-27 23:39:06 +00:00
|
|
|
static int LZ4F_localLZ4_compress_limitedOutput_withState(void* ctx, const char* src, char* dst, int srcSize, int dstSize, int level)
|
|
|
|
{
|
|
|
|
(void) level;
|
|
|
|
return LZ4_compress_limitedOutput_withState(ctx, src, dst, srcSize, dstSize);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int LZ4F_localLZ4_compress_limitedOutput_continue(void* ctx, const char* src, char* dst, int srcSize, int dstSize, int level)
|
|
|
|
{
|
|
|
|
(void) level;
|
|
|
|
return LZ4_compress_limitedOutput_continue((LZ4_stream_t*)ctx, src, dst, srcSize, dstSize);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int LZ4F_localLZ4_compressHC_limitedOutput_continue(void* ctx, const char* src, char* dst, int srcSize, int dstSize, int level)
|
|
|
|
{
|
|
|
|
(void) level;
|
|
|
|
return LZ4_compressHC_limitedOutput_continue((LZ4_streamHC_t*)ctx, src, dst, srcSize, dstSize);
|
|
|
|
}
|
|
|
|
|
2015-04-14 17:20:47 +00:00
|
|
|
static compressFunc_t LZ4F_selectCompression(LZ4F_blockMode_t blockMode, U32 level)
|
2014-10-27 23:39:06 +00:00
|
|
|
{
|
|
|
|
if (level < minHClevel)
|
|
|
|
{
|
2015-04-14 17:20:47 +00:00
|
|
|
if (blockMode == LZ4F_blockIndependent) return LZ4F_localLZ4_compress_limitedOutput_withState;
|
2014-10-27 23:39:06 +00:00
|
|
|
return LZ4F_localLZ4_compress_limitedOutput_continue;
|
|
|
|
}
|
2015-04-14 17:20:47 +00:00
|
|
|
if (blockMode == LZ4F_blockIndependent) return LZ4_compressHC2_limitedOutput_withStateHC;
|
2014-10-27 23:39:06 +00:00
|
|
|
return LZ4F_localLZ4_compressHC_limitedOutput_continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int LZ4F_localSaveDict(LZ4F_cctx_internal_t* cctxPtr)
|
|
|
|
{
|
|
|
|
if (cctxPtr->prefs.compressionLevel < minHClevel)
|
|
|
|
return LZ4_saveDict ((LZ4_stream_t*)(cctxPtr->lz4CtxPtr), (char*)(cctxPtr->tmpBuff), 64 KB);
|
|
|
|
return LZ4_saveDictHC ((LZ4_streamHC_t*)(cctxPtr->lz4CtxPtr), (char*)(cctxPtr->tmpBuff), 64 KB);
|
|
|
|
}
|
|
|
|
|
2014-09-14 23:59:30 +00:00
|
|
|
typedef enum { notDone, fromTmpBuffer, fromSrcBuffer } LZ4F_lastBlockStatus;
|
|
|
|
|
2014-09-12 18:28:44 +00:00
|
|
|
/* LZ4F_compressUpdate()
|
2014-10-27 23:39:06 +00:00
|
|
|
* LZ4F_compressUpdate() can be called repetitively to compress as much data as necessary.
|
|
|
|
* The most important rule is that dstBuffer MUST be large enough (dstMaxSize) to ensure compression completion even in worst case.
|
|
|
|
* If this condition is not respected, LZ4F_compress() will fail (result is an errorCode)
|
|
|
|
* You can get the minimum value of dstMaxSize by using LZ4F_compressBound()
|
|
|
|
* The LZ4F_compressOptions_t structure is optional : you can provide NULL as argument.
|
|
|
|
* The result of the function is the number of bytes written into dstBuffer : it can be zero, meaning input data was just buffered.
|
|
|
|
* The function outputs an error code if it fails (can be tested using LZ4F_isError())
|
|
|
|
*/
|
2014-09-12 18:28:44 +00:00
|
|
|
size_t LZ4F_compressUpdate(LZ4F_compressionContext_t compressionContext, void* dstBuffer, size_t dstMaxSize, const void* srcBuffer, size_t srcSize, const LZ4F_compressOptions_t* compressOptionsPtr)
|
2014-08-30 17:14:44 +00:00
|
|
|
{
|
2015-03-11 19:11:14 +00:00
|
|
|
LZ4F_compressOptions_t cOptionsNull;
|
2014-09-22 17:42:00 +00:00
|
|
|
LZ4F_cctx_internal_t* cctxPtr = (LZ4F_cctx_internal_t*)compressionContext;
|
|
|
|
size_t blockSize = cctxPtr->maxBlockSize;
|
|
|
|
const BYTE* srcPtr = (const BYTE*)srcBuffer;
|
|
|
|
const BYTE* const srcEnd = srcPtr + srcSize;
|
|
|
|
BYTE* const dstStart = (BYTE*)dstBuffer;
|
|
|
|
BYTE* dstPtr = dstStart;
|
|
|
|
LZ4F_lastBlockStatus lastBlockCompressed = notDone;
|
|
|
|
compressFunc_t compress;
|
|
|
|
|
|
|
|
|
2015-04-14 17:20:47 +00:00
|
|
|
if (cctxPtr->cStage != 1) return (size_t)-LZ4F_ERROR_GENERIC;
|
|
|
|
if (dstMaxSize < LZ4F_compressBound(srcSize, &(cctxPtr->prefs))) return (size_t)-LZ4F_ERROR_dstMaxSize_tooSmall;
|
2015-03-11 19:11:14 +00:00
|
|
|
memset(&cOptionsNull, 0, sizeof(cOptionsNull));
|
2014-09-22 17:42:00 +00:00
|
|
|
if (compressOptionsPtr == NULL) compressOptionsPtr = &cOptionsNull;
|
|
|
|
|
|
|
|
/* select compression function */
|
2014-10-27 23:39:06 +00:00
|
|
|
compress = LZ4F_selectCompression(cctxPtr->prefs.frameInfo.blockMode, cctxPtr->prefs.compressionLevel);
|
2014-09-22 17:42:00 +00:00
|
|
|
|
|
|
|
/* complete tmp buffer */
|
|
|
|
if (cctxPtr->tmpInSize > 0) /* some data already within tmp buffer */
|
|
|
|
{
|
|
|
|
size_t sizeToCopy = blockSize - cctxPtr->tmpInSize;
|
|
|
|
if (sizeToCopy > srcSize)
|
|
|
|
{
|
|
|
|
/* add src to tmpIn buffer */
|
|
|
|
memcpy(cctxPtr->tmpIn + cctxPtr->tmpInSize, srcBuffer, srcSize);
|
|
|
|
srcPtr = srcEnd;
|
|
|
|
cctxPtr->tmpInSize += srcSize;
|
|
|
|
/* still needs some CRC */
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* complete tmpIn block and then compress it */
|
|
|
|
lastBlockCompressed = fromTmpBuffer;
|
|
|
|
memcpy(cctxPtr->tmpIn + cctxPtr->tmpInSize, srcBuffer, sizeToCopy);
|
|
|
|
srcPtr += sizeToCopy;
|
|
|
|
|
2014-10-27 23:39:06 +00:00
|
|
|
dstPtr += LZ4F_compressBlock(dstPtr, cctxPtr->tmpIn, blockSize, compress, cctxPtr->lz4CtxPtr, cctxPtr->prefs.compressionLevel);
|
2014-09-22 17:42:00 +00:00
|
|
|
|
2015-04-14 17:20:47 +00:00
|
|
|
if (cctxPtr->prefs.frameInfo.blockMode==LZ4F_blockLinked) cctxPtr->tmpIn += blockSize;
|
2014-09-22 17:42:00 +00:00
|
|
|
cctxPtr->tmpInSize = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
while ((size_t)(srcEnd - srcPtr) >= blockSize)
|
|
|
|
{
|
|
|
|
/* compress full block */
|
2014-09-14 23:59:30 +00:00
|
|
|
lastBlockCompressed = fromSrcBuffer;
|
2014-10-27 23:39:06 +00:00
|
|
|
dstPtr += LZ4F_compressBlock(dstPtr, srcPtr, blockSize, compress, cctxPtr->lz4CtxPtr, cctxPtr->prefs.compressionLevel);
|
2014-09-22 17:42:00 +00:00
|
|
|
srcPtr += blockSize;
|
|
|
|
}
|
2014-08-30 17:14:44 +00:00
|
|
|
|
2014-09-12 18:28:44 +00:00
|
|
|
if ((cctxPtr->prefs.autoFlush) && (srcPtr < srcEnd))
|
|
|
|
{
|
2014-09-14 23:59:30 +00:00
|
|
|
/* compress remaining input < blockSize */
|
|
|
|
lastBlockCompressed = fromSrcBuffer;
|
2014-10-27 23:39:06 +00:00
|
|
|
dstPtr += LZ4F_compressBlock(dstPtr, srcPtr, srcEnd - srcPtr, compress, cctxPtr->lz4CtxPtr, cctxPtr->prefs.compressionLevel);
|
2014-09-22 17:42:00 +00:00
|
|
|
srcPtr = srcEnd;
|
2014-09-12 18:28:44 +00:00
|
|
|
}
|
|
|
|
|
2014-09-14 23:59:30 +00:00
|
|
|
/* preserve dictionary if necessary */
|
2015-04-14 17:20:47 +00:00
|
|
|
if ((cctxPtr->prefs.frameInfo.blockMode==LZ4F_blockLinked) && (lastBlockCompressed==fromSrcBuffer))
|
2014-09-09 22:54:22 +00:00
|
|
|
{
|
2014-09-14 23:59:30 +00:00
|
|
|
if (compressOptionsPtr->stableSrc)
|
2014-09-10 21:17:03 +00:00
|
|
|
{
|
2014-09-14 23:59:30 +00:00
|
|
|
cctxPtr->tmpIn = cctxPtr->tmpBuff;
|
2014-09-10 21:17:03 +00:00
|
|
|
}
|
2014-09-14 23:59:30 +00:00
|
|
|
else
|
|
|
|
{
|
2014-10-27 23:39:06 +00:00
|
|
|
int realDictSize = LZ4F_localSaveDict(cctxPtr);
|
2015-04-14 17:20:47 +00:00
|
|
|
if (realDictSize==0) return (size_t)-LZ4F_ERROR_GENERIC;
|
2014-09-14 23:59:30 +00:00
|
|
|
cctxPtr->tmpIn = cctxPtr->tmpBuff + realDictSize;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* keep tmpIn within limits */
|
2015-04-14 17:20:47 +00:00
|
|
|
if ((cctxPtr->tmpIn + blockSize) > (cctxPtr->tmpBuff + cctxPtr->maxBufferSize) /* necessarily LZ4F_blockLinked && lastBlockCompressed==fromTmpBuffer */
|
2014-10-27 23:39:06 +00:00
|
|
|
&& !(cctxPtr->prefs.autoFlush))
|
2014-09-14 23:59:30 +00:00
|
|
|
{
|
2015-03-29 12:28:32 +00:00
|
|
|
int realDictSize = LZ4F_localSaveDict(cctxPtr);
|
|
|
|
cctxPtr->tmpIn = cctxPtr->tmpBuff + realDictSize;
|
2014-09-09 22:54:22 +00:00
|
|
|
}
|
|
|
|
|
2014-09-14 23:59:30 +00:00
|
|
|
/* some input data left, necessarily < blockSize */
|
2014-09-22 17:42:00 +00:00
|
|
|
if (srcPtr < srcEnd)
|
2014-09-12 18:28:44 +00:00
|
|
|
{
|
|
|
|
/* fill tmp buffer */
|
|
|
|
size_t sizeToCopy = srcEnd - srcPtr;
|
|
|
|
memcpy(cctxPtr->tmpIn, srcPtr, sizeToCopy);
|
|
|
|
cctxPtr->tmpInSize = sizeToCopy;
|
|
|
|
}
|
2014-08-30 17:14:44 +00:00
|
|
|
|
2015-04-14 17:20:47 +00:00
|
|
|
if (cctxPtr->prefs.frameInfo.contentChecksumFlag == LZ4F_contentChecksumEnabled)
|
2015-03-18 20:38:27 +00:00
|
|
|
XXH32_update(&(cctxPtr->xxh), srcBuffer, srcSize);
|
2014-08-30 17:14:44 +00:00
|
|
|
|
2015-03-18 20:38:27 +00:00
|
|
|
cctxPtr->totalInSize += srcSize;
|
2014-09-22 17:42:00 +00:00
|
|
|
return dstPtr - dstStart;
|
2014-08-30 17:14:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* LZ4F_flush()
|
2014-10-27 23:39:06 +00:00
|
|
|
* Should you need to create compressed data immediately, without waiting for a block to be filled,
|
|
|
|
* you can call LZ4_flush(), which will immediately compress any remaining data stored within compressionContext.
|
|
|
|
* The result of the function is the number of bytes written into dstBuffer
|
|
|
|
* (it can be zero, this means there was no data left within compressionContext)
|
|
|
|
* The function outputs an error code if it fails (can be tested using LZ4F_isError())
|
|
|
|
* The LZ4F_compressOptions_t structure is optional : you can provide NULL as argument.
|
|
|
|
*/
|
2014-08-30 17:14:44 +00:00
|
|
|
size_t LZ4F_flush(LZ4F_compressionContext_t compressionContext, void* dstBuffer, size_t dstMaxSize, const LZ4F_compressOptions_t* compressOptionsPtr)
|
|
|
|
{
|
2014-09-22 17:42:00 +00:00
|
|
|
LZ4F_cctx_internal_t* cctxPtr = (LZ4F_cctx_internal_t*)compressionContext;
|
|
|
|
BYTE* const dstStart = (BYTE*)dstBuffer;
|
|
|
|
BYTE* dstPtr = dstStart;
|
2014-10-27 23:39:06 +00:00
|
|
|
compressFunc_t compress;
|
2014-08-30 17:14:44 +00:00
|
|
|
|
|
|
|
|
2014-09-22 17:42:00 +00:00
|
|
|
if (cctxPtr->tmpInSize == 0) return 0; /* nothing to flush */
|
2015-04-14 17:20:47 +00:00
|
|
|
if (cctxPtr->cStage != 1) return (size_t)-LZ4F_ERROR_GENERIC;
|
|
|
|
if (dstMaxSize < (cctxPtr->tmpInSize + 8)) return (size_t)-LZ4F_ERROR_dstMaxSize_tooSmall; /* +8 : block header(4) + block checksum(4) */
|
2015-03-15 00:42:27 +00:00
|
|
|
(void)compressOptionsPtr; /* not yet useful */
|
2014-08-30 17:14:44 +00:00
|
|
|
|
2014-09-22 17:42:00 +00:00
|
|
|
/* select compression function */
|
2014-10-27 23:39:06 +00:00
|
|
|
compress = LZ4F_selectCompression(cctxPtr->prefs.frameInfo.blockMode, cctxPtr->prefs.compressionLevel);
|
2014-09-09 22:54:22 +00:00
|
|
|
|
2014-09-14 23:59:30 +00:00
|
|
|
/* compress tmp buffer */
|
2014-10-27 23:39:06 +00:00
|
|
|
dstPtr += LZ4F_compressBlock(dstPtr, cctxPtr->tmpIn, cctxPtr->tmpInSize, compress, cctxPtr->lz4CtxPtr, cctxPtr->prefs.compressionLevel);
|
2015-04-14 17:20:47 +00:00
|
|
|
if (cctxPtr->prefs.frameInfo.blockMode==LZ4F_blockLinked) cctxPtr->tmpIn += cctxPtr->tmpInSize;
|
2014-09-22 17:42:00 +00:00
|
|
|
cctxPtr->tmpInSize = 0;
|
2014-08-30 17:14:44 +00:00
|
|
|
|
2014-09-14 23:59:30 +00:00
|
|
|
/* keep tmpIn within limits */
|
2015-04-14 17:20:47 +00:00
|
|
|
if ((cctxPtr->tmpIn + cctxPtr->maxBlockSize) > (cctxPtr->tmpBuff + cctxPtr->maxBufferSize)) /* necessarily LZ4F_blockLinked */
|
2014-09-09 22:54:22 +00:00
|
|
|
{
|
2015-03-29 12:28:32 +00:00
|
|
|
int realDictSize = LZ4F_localSaveDict(cctxPtr);
|
|
|
|
cctxPtr->tmpIn = cctxPtr->tmpBuff + realDictSize;
|
2014-09-09 22:54:22 +00:00
|
|
|
}
|
|
|
|
|
2014-09-22 17:42:00 +00:00
|
|
|
return dstPtr - dstStart;
|
2014-08-30 17:14:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* LZ4F_compressEnd()
|
2014-10-27 23:39:06 +00:00
|
|
|
* When you want to properly finish the compressed frame, just call LZ4F_compressEnd().
|
|
|
|
* It will flush whatever data remained within compressionContext (like LZ4_flush())
|
|
|
|
* but also properly finalize the frame, with an endMark and a checksum.
|
|
|
|
* The result of the function is the number of bytes written into dstBuffer (necessarily >= 4 (endMark size))
|
|
|
|
* The function outputs an error code if it fails (can be tested using LZ4F_isError())
|
|
|
|
* The LZ4F_compressOptions_t structure is optional : you can provide NULL as argument.
|
|
|
|
* compressionContext can then be used again, starting with LZ4F_compressBegin(). The preferences will remain the same.
|
|
|
|
*/
|
2014-08-30 17:14:44 +00:00
|
|
|
size_t LZ4F_compressEnd(LZ4F_compressionContext_t compressionContext, void* dstBuffer, size_t dstMaxSize, const LZ4F_compressOptions_t* compressOptionsPtr)
|
|
|
|
{
|
2014-09-22 17:42:00 +00:00
|
|
|
LZ4F_cctx_internal_t* cctxPtr = (LZ4F_cctx_internal_t*)compressionContext;
|
|
|
|
BYTE* const dstStart = (BYTE*)dstBuffer;
|
|
|
|
BYTE* dstPtr = dstStart;
|
|
|
|
size_t errorCode;
|
2014-08-30 17:14:44 +00:00
|
|
|
|
2014-09-22 17:42:00 +00:00
|
|
|
errorCode = LZ4F_flush(compressionContext, dstBuffer, dstMaxSize, compressOptionsPtr);
|
|
|
|
if (LZ4F_isError(errorCode)) return errorCode;
|
|
|
|
dstPtr += errorCode;
|
2014-08-30 17:14:44 +00:00
|
|
|
|
2014-09-22 17:42:00 +00:00
|
|
|
LZ4F_writeLE32(dstPtr, 0);
|
|
|
|
dstPtr+=4; /* endMark */
|
2014-08-30 17:14:44 +00:00
|
|
|
|
2015-04-14 17:20:47 +00:00
|
|
|
if (cctxPtr->prefs.frameInfo.contentChecksumFlag == LZ4F_contentChecksumEnabled)
|
2014-09-22 17:42:00 +00:00
|
|
|
{
|
2014-10-05 23:48:45 +00:00
|
|
|
U32 xxh = XXH32_digest(&(cctxPtr->xxh));
|
2014-09-22 17:42:00 +00:00
|
|
|
LZ4F_writeLE32(dstPtr, xxh);
|
|
|
|
dstPtr+=4; /* content Checksum */
|
|
|
|
}
|
2014-08-30 17:14:44 +00:00
|
|
|
|
2014-09-22 17:42:00 +00:00
|
|
|
cctxPtr->cStage = 0; /* state is now re-usable (with identical preferences) */
|
2014-08-30 17:14:44 +00:00
|
|
|
|
2015-03-29 10:20:09 +00:00
|
|
|
if (cctxPtr->prefs.frameInfo.contentSize)
|
2015-03-18 20:38:27 +00:00
|
|
|
{
|
2015-03-29 10:20:09 +00:00
|
|
|
if (cctxPtr->prefs.frameInfo.contentSize != cctxPtr->totalInSize)
|
2015-04-14 17:20:47 +00:00
|
|
|
return (size_t)-LZ4F_ERROR_frameSize_wrong;
|
2015-03-18 20:38:27 +00:00
|
|
|
}
|
|
|
|
|
2014-09-22 17:42:00 +00:00
|
|
|
return dstPtr - dstStart;
|
2014-08-30 17:14:44 +00:00
|
|
|
}
|
2014-09-01 21:44:02 +00:00
|
|
|
|
|
|
|
|
2015-03-16 16:52:14 +00:00
|
|
|
/**********************************
|
|
|
|
* Decompression functions
|
|
|
|
**********************************/
|
2014-09-01 21:44:02 +00:00
|
|
|
|
|
|
|
/* Resource management */
|
|
|
|
|
|
|
|
/* LZ4F_createDecompressionContext() :
|
2014-10-27 23:39:06 +00:00
|
|
|
* The first thing to do is to create a decompressionContext object, which will be used in all decompression operations.
|
|
|
|
* This is achieved using LZ4F_createDecompressionContext().
|
|
|
|
* The function will provide a pointer to a fully allocated and initialized LZ4F_decompressionContext object.
|
|
|
|
* If the result LZ4F_errorCode_t is not zero, there was an error during context creation.
|
|
|
|
* Object can release its memory using LZ4F_freeDecompressionContext();
|
|
|
|
*/
|
2015-03-16 18:19:39 +00:00
|
|
|
LZ4F_errorCode_t LZ4F_createDecompressionContext(LZ4F_decompressionContext_t* LZ4F_decompressionContextPtr, unsigned versionNumber)
|
2014-09-03 18:49:59 +00:00
|
|
|
{
|
|
|
|
LZ4F_dctx_internal_t* dctxPtr;
|
|
|
|
|
2015-03-10 15:57:42 +00:00
|
|
|
dctxPtr = (LZ4F_dctx_internal_t*)ALLOCATOR(sizeof(LZ4F_dctx_internal_t));
|
2015-04-14 17:20:47 +00:00
|
|
|
if (dctxPtr==NULL) return (LZ4F_errorCode_t)-LZ4F_ERROR_GENERIC;
|
2014-09-03 18:49:59 +00:00
|
|
|
|
2014-09-05 14:50:06 +00:00
|
|
|
dctxPtr->version = versionNumber;
|
2014-09-03 18:49:59 +00:00
|
|
|
*LZ4F_decompressionContextPtr = (LZ4F_compressionContext_t)dctxPtr;
|
2015-04-14 17:20:47 +00:00
|
|
|
return LZ4F_OK_NoError;
|
2014-09-03 18:49:59 +00:00
|
|
|
}
|
|
|
|
|
2015-03-16 18:19:39 +00:00
|
|
|
LZ4F_errorCode_t LZ4F_freeDecompressionContext(LZ4F_decompressionContext_t LZ4F_decompressionContext)
|
2014-09-03 18:49:59 +00:00
|
|
|
{
|
|
|
|
LZ4F_dctx_internal_t* dctxPtr = (LZ4F_dctx_internal_t*)LZ4F_decompressionContext;
|
2015-04-12 07:21:35 +00:00
|
|
|
if (dctxPtr != NULL) /* can accept NULL input, like free() */
|
|
|
|
{
|
|
|
|
FREEMEM(dctxPtr->tmpIn);
|
|
|
|
FREEMEM(dctxPtr->tmpOutBuffer);
|
|
|
|
FREEMEM(dctxPtr);
|
|
|
|
}
|
2015-04-14 17:20:47 +00:00
|
|
|
return LZ4F_OK_NoError;
|
2014-09-03 18:49:59 +00:00
|
|
|
}
|
|
|
|
|
2014-09-01 21:44:02 +00:00
|
|
|
|
2015-03-16 16:52:14 +00:00
|
|
|
/* ******************************************************************** */
|
|
|
|
/* ********************* Decompression ******************************** */
|
|
|
|
/* ******************************************************************** */
|
|
|
|
|
|
|
|
typedef enum { dstage_getHeader=0, dstage_storeHeader,
|
|
|
|
dstage_getCBlockSize, dstage_storeCBlockSize,
|
|
|
|
dstage_copyDirect,
|
2015-04-19 14:23:53 +00:00
|
|
|
dstage_getCBlock, dstage_storeCBlock,
|
|
|
|
dstage_decodeCBlock, dstage_decodeCBlock_intoDst,
|
|
|
|
dstage_decodeCBlock_intoTmp, dstage_flushOut,
|
2015-03-16 16:52:14 +00:00
|
|
|
dstage_getSuffix, dstage_storeSuffix,
|
2015-03-18 20:38:27 +00:00
|
|
|
dstage_getSFrameSize, dstage_storeSFrameSize,
|
2015-03-16 16:52:14 +00:00
|
|
|
dstage_skipSkippable
|
|
|
|
} dStage_t;
|
|
|
|
|
|
|
|
|
|
|
|
/* LZ4F_decodeHeader
|
|
|
|
return : nb Bytes read from srcVoidPtr (necessarily <= srcSize)
|
|
|
|
or an error code (testable with LZ4F_isError())
|
|
|
|
output : set internal values of dctx, such as
|
|
|
|
dctxPtr->frameInfo and dctxPtr->dStage.
|
2015-04-19 14:23:53 +00:00
|
|
|
input : srcVoidPtr points at the **beginning of the frame**
|
2015-03-16 16:52:14 +00:00
|
|
|
*/
|
2015-03-10 15:57:42 +00:00
|
|
|
static size_t LZ4F_decodeHeader(LZ4F_dctx_internal_t* dctxPtr, const void* srcVoidPtr, size_t srcSize)
|
2014-09-01 21:44:02 +00:00
|
|
|
{
|
|
|
|
BYTE FLG, BD, HC;
|
2015-03-18 20:38:27 +00:00
|
|
|
unsigned version, blockMode, blockChecksumFlag, contentSizeFlag, contentChecksumFlag, blockSizeID;
|
2014-09-10 21:17:03 +00:00
|
|
|
size_t bufferNeeded;
|
2015-03-18 20:38:27 +00:00
|
|
|
size_t frameHeaderSize;
|
2015-03-10 15:57:42 +00:00
|
|
|
const BYTE* srcPtr = (const BYTE*)srcVoidPtr;
|
2014-09-01 21:44:02 +00:00
|
|
|
|
|
|
|
/* need to decode header to get frameInfo */
|
2015-04-19 14:23:53 +00:00
|
|
|
if (srcSize < minFHSize) return (size_t)-LZ4F_ERROR_frameHeader_incomplete; /* minimal frame header size */
|
2015-03-18 20:38:27 +00:00
|
|
|
memset(&(dctxPtr->frameInfo), 0, sizeof(dctxPtr->frameInfo));
|
2014-09-01 21:44:02 +00:00
|
|
|
|
2015-04-19 14:23:53 +00:00
|
|
|
/* special case : skippable frames */
|
2015-03-16 16:52:14 +00:00
|
|
|
if ((LZ4F_readLE32(srcPtr) & 0xFFFFFFF0U) == LZ4F_MAGIC_SKIPPABLE_START)
|
|
|
|
{
|
2015-04-14 17:20:47 +00:00
|
|
|
dctxPtr->frameInfo.frameType = LZ4F_skippableFrame;
|
2015-03-16 18:19:39 +00:00
|
|
|
if (srcVoidPtr == (void*)(dctxPtr->header))
|
|
|
|
{
|
2015-03-18 20:38:27 +00:00
|
|
|
dctxPtr->tmpInSize = srcSize;
|
|
|
|
dctxPtr->tmpInTarget = 8;
|
|
|
|
dctxPtr->dStage = dstage_storeSFrameSize;
|
2015-03-16 18:19:39 +00:00
|
|
|
return srcSize;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2015-03-18 20:38:27 +00:00
|
|
|
dctxPtr->dStage = dstage_getSFrameSize;
|
2015-03-16 18:19:39 +00:00
|
|
|
return 4;
|
|
|
|
}
|
2015-03-16 16:52:14 +00:00
|
|
|
}
|
|
|
|
|
2014-09-01 21:44:02 +00:00
|
|
|
/* control magic number */
|
2015-04-14 17:20:47 +00:00
|
|
|
if (LZ4F_readLE32(srcPtr) != LZ4F_MAGICNUMBER) return (size_t)-LZ4F_ERROR_frameType_unknown;
|
2015-03-16 16:52:14 +00:00
|
|
|
dctxPtr->frameInfo.frameType = LZ4F_frame;
|
2014-09-01 21:44:02 +00:00
|
|
|
|
|
|
|
/* Flags */
|
2015-03-18 20:38:27 +00:00
|
|
|
FLG = srcPtr[4];
|
2015-03-10 15:57:42 +00:00
|
|
|
version = (FLG>>6) & _2BITS;
|
2014-09-01 21:44:02 +00:00
|
|
|
blockMode = (FLG>>5) & _1BIT;
|
|
|
|
blockChecksumFlag = (FLG>>4) & _1BIT;
|
|
|
|
contentSizeFlag = (FLG>>3) & _1BIT;
|
|
|
|
contentChecksumFlag = (FLG>>2) & _1BIT;
|
|
|
|
|
2015-03-18 20:38:27 +00:00
|
|
|
/* Frame Header Size */
|
2015-04-19 14:23:53 +00:00
|
|
|
frameHeaderSize = contentSizeFlag ? maxFHSize : minFHSize;
|
2015-03-18 20:38:27 +00:00
|
|
|
|
|
|
|
if (srcSize < frameHeaderSize)
|
|
|
|
{
|
2015-04-19 14:23:53 +00:00
|
|
|
/* not enough input to fully decode frame header */
|
2015-03-18 20:38:27 +00:00
|
|
|
if (srcPtr != dctxPtr->header)
|
|
|
|
memcpy(dctxPtr->header, srcPtr, srcSize);
|
|
|
|
dctxPtr->tmpInSize = srcSize;
|
|
|
|
dctxPtr->tmpInTarget = frameHeaderSize;
|
|
|
|
dctxPtr->dStage = dstage_storeHeader;
|
|
|
|
return srcSize;
|
|
|
|
}
|
|
|
|
|
|
|
|
BD = srcPtr[5];
|
|
|
|
blockSizeID = (BD>>4) & _3BITS;
|
2014-09-01 21:44:02 +00:00
|
|
|
|
|
|
|
/* validate */
|
2015-04-15 09:34:08 +00:00
|
|
|
if (version != 1) return (size_t)-LZ4F_ERROR_headerVersion_wrong; /* Version Number, only supported value */
|
|
|
|
if (blockChecksumFlag != 0) return (size_t)-LZ4F_ERROR_blockChecksum_unsupported; /* Not supported for the time being */
|
|
|
|
if (((FLG>>0)&_2BITS) != 0) return (size_t)-LZ4F_ERROR_reservedFlag_set; /* Reserved bits */
|
|
|
|
if (((BD>>7)&_1BIT) != 0) return (size_t)-LZ4F_ERROR_reservedFlag_set; /* Reserved bit */
|
|
|
|
if (blockSizeID < 4) return (size_t)-LZ4F_ERROR_maxBlockSize_invalid; /* 4-7 only supported values for the time being */
|
|
|
|
if (((BD>>0)&_4BITS) != 0) return (size_t)-LZ4F_ERROR_reservedFlag_set; /* Reserved bits */
|
2014-09-01 21:44:02 +00:00
|
|
|
|
2015-03-18 20:38:27 +00:00
|
|
|
/* check */
|
|
|
|
HC = LZ4F_headerChecksum(srcPtr+4, frameHeaderSize-5);
|
2015-04-15 09:34:08 +00:00
|
|
|
if (HC != srcPtr[frameHeaderSize-1]) return (size_t)-LZ4F_ERROR_headerChecksum_invalid; /* Bad header checksum error */
|
2015-03-18 20:38:27 +00:00
|
|
|
|
2014-09-01 21:44:02 +00:00
|
|
|
/* save */
|
2015-04-14 17:20:47 +00:00
|
|
|
dctxPtr->frameInfo.blockMode = (LZ4F_blockMode_t)blockMode;
|
|
|
|
dctxPtr->frameInfo.contentChecksumFlag = (LZ4F_contentChecksum_t)contentChecksumFlag;
|
|
|
|
dctxPtr->frameInfo.blockSizeID = (LZ4F_blockSizeID_t)blockSizeID;
|
2014-09-10 12:00:39 +00:00
|
|
|
dctxPtr->maxBlockSize = LZ4F_getBlockSize(blockSizeID);
|
2015-03-18 20:38:27 +00:00
|
|
|
if (contentSizeFlag)
|
2015-04-09 21:59:07 +00:00
|
|
|
dctxPtr->frameRemainingSize = dctxPtr->frameInfo.contentSize = LZ4F_readLE64(srcPtr+6);
|
2014-09-01 21:44:02 +00:00
|
|
|
|
2014-09-05 14:50:06 +00:00
|
|
|
/* init */
|
2014-10-05 23:48:45 +00:00
|
|
|
if (contentChecksumFlag) XXH32_reset(&(dctxPtr->xxh), 0);
|
2014-09-01 21:44:02 +00:00
|
|
|
|
2014-09-10 12:00:39 +00:00
|
|
|
/* alloc */
|
2015-04-14 17:20:47 +00:00
|
|
|
bufferNeeded = dctxPtr->maxBlockSize + ((dctxPtr->frameInfo.blockMode==LZ4F_blockLinked) * 128 KB);
|
2014-09-10 21:17:03 +00:00
|
|
|
if (bufferNeeded > dctxPtr->maxBufferSize) /* tmp buffers too small */
|
2014-09-03 18:49:59 +00:00
|
|
|
{
|
|
|
|
FREEMEM(dctxPtr->tmpIn);
|
2014-09-09 22:54:22 +00:00
|
|
|
FREEMEM(dctxPtr->tmpOutBuffer);
|
2014-09-10 21:17:03 +00:00
|
|
|
dctxPtr->maxBufferSize = bufferNeeded;
|
2015-03-10 15:57:42 +00:00
|
|
|
dctxPtr->tmpIn = (BYTE*)ALLOCATOR(dctxPtr->maxBlockSize);
|
2015-04-14 17:20:47 +00:00
|
|
|
if (dctxPtr->tmpIn == NULL) return (size_t)-LZ4F_ERROR_GENERIC;
|
2015-03-10 15:57:42 +00:00
|
|
|
dctxPtr->tmpOutBuffer= (BYTE*)ALLOCATOR(dctxPtr->maxBufferSize);
|
2015-04-14 17:20:47 +00:00
|
|
|
if (dctxPtr->tmpOutBuffer== NULL) return (size_t)-LZ4F_ERROR_GENERIC;
|
2014-09-03 18:49:59 +00:00
|
|
|
}
|
2014-09-21 08:56:21 +00:00
|
|
|
dctxPtr->tmpInSize = 0;
|
|
|
|
dctxPtr->tmpInTarget = 0;
|
2014-09-13 18:49:01 +00:00
|
|
|
dctxPtr->dict = dctxPtr->tmpOutBuffer;
|
2014-09-10 12:00:39 +00:00
|
|
|
dctxPtr->dictSize = 0;
|
2014-09-13 18:49:01 +00:00
|
|
|
dctxPtr->tmpOut = dctxPtr->tmpOutBuffer;
|
2014-09-21 08:56:21 +00:00
|
|
|
dctxPtr->tmpOutStart = 0;
|
|
|
|
dctxPtr->tmpOutSize = 0;
|
2014-09-05 14:50:06 +00:00
|
|
|
|
2015-03-16 16:52:14 +00:00
|
|
|
dctxPtr->dStage = dstage_getCBlockSize;
|
|
|
|
|
2015-03-18 20:38:27 +00:00
|
|
|
return frameHeaderSize;
|
2014-09-03 18:49:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-09-01 21:44:02 +00:00
|
|
|
/* LZ4F_getFrameInfo()
|
2014-10-27 23:39:06 +00:00
|
|
|
* This function decodes frame header information, such as blockSize.
|
|
|
|
* It is optional : you could start by calling directly LZ4F_decompress() instead.
|
|
|
|
* The objective is to extract header information without starting decompression, typically for allocation purposes.
|
|
|
|
* LZ4F_getFrameInfo() can also be used *after* starting decompression, on a valid LZ4F_decompressionContext_t.
|
|
|
|
* The number of bytes read from srcBuffer will be provided within *srcSizePtr (necessarily <= original value).
|
|
|
|
* You are expected to resume decompression from where it stopped (srcBuffer + *srcSizePtr)
|
|
|
|
* The function result is an hint of the better srcSize to use for next call to LZ4F_decompress,
|
|
|
|
* or an error code which can be tested using LZ4F_isError().
|
|
|
|
*/
|
2015-04-19 14:23:53 +00:00
|
|
|
LZ4F_errorCode_t LZ4F_getFrameInfo(LZ4F_decompressionContext_t dCtx, LZ4F_frameInfo_t* frameInfoPtr,
|
|
|
|
const void* srcBuffer, size_t* srcSizePtr)
|
2014-09-01 21:44:02 +00:00
|
|
|
{
|
2015-04-19 14:23:53 +00:00
|
|
|
LZ4F_dctx_internal_t* dctxPtr = (LZ4F_dctx_internal_t*)dCtx;
|
2014-09-01 21:44:02 +00:00
|
|
|
|
2015-04-19 14:23:53 +00:00
|
|
|
if (dctxPtr->dStage > dstage_storeHeader) /* note : requires dstage_* header related to be at beginning of enum */
|
2014-09-01 21:44:02 +00:00
|
|
|
{
|
2015-04-19 14:23:53 +00:00
|
|
|
size_t o=0, i=0;
|
|
|
|
/* frameInfo already decoded */
|
|
|
|
*srcSizePtr = 0;
|
|
|
|
*frameInfoPtr = dctxPtr->frameInfo;
|
|
|
|
return LZ4F_decompress(dCtx, NULL, &o, NULL, &i, NULL);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
size_t o=0;
|
|
|
|
size_t nextSrcSize = LZ4F_decompress(dCtx, NULL, &o, srcBuffer, srcSizePtr, NULL);
|
|
|
|
if (dctxPtr->dStage <= dstage_storeHeader) /* note : requires dstage_* header related to be at beginning of enum */
|
|
|
|
return (size_t)-LZ4F_ERROR_frameHeader_incomplete;
|
|
|
|
*frameInfoPtr = dctxPtr->frameInfo;
|
|
|
|
return nextSrcSize;
|
2014-09-01 21:44:02 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-09-03 18:49:59 +00:00
|
|
|
|
2015-04-19 14:23:53 +00:00
|
|
|
/* trivial redirector, for common prototype */
|
2014-09-21 08:56:21 +00:00
|
|
|
static int LZ4F_decompress_safe (const char* source, char* dest, int compressedSize, int maxDecompressedSize, const char* dictStart, int dictSize)
|
2014-09-09 22:54:22 +00:00
|
|
|
{
|
2015-03-18 20:38:27 +00:00
|
|
|
(void)dictStart; (void)dictSize;
|
2014-09-21 08:56:21 +00:00
|
|
|
return LZ4_decompress_safe (source, dest, compressedSize, maxDecompressedSize);
|
2014-09-13 18:49:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-09-21 08:56:21 +00:00
|
|
|
static void LZ4F_updateDict(LZ4F_dctx_internal_t* dctxPtr, const BYTE* dstPtr, size_t dstSize, const BYTE* dstPtr0, unsigned withinTmp)
|
2014-09-13 18:49:01 +00:00
|
|
|
{
|
2014-09-21 08:56:21 +00:00
|
|
|
if (dctxPtr->dictSize==0)
|
|
|
|
dctxPtr->dict = (BYTE*)dstPtr; /* priority to dictionary continuity */
|
|
|
|
|
|
|
|
if (dctxPtr->dict + dctxPtr->dictSize == dstPtr) /* dictionary continuity */
|
2014-09-13 18:49:01 +00:00
|
|
|
{
|
2014-09-21 08:56:21 +00:00
|
|
|
dctxPtr->dictSize += dstSize;
|
2014-09-13 18:49:01 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-09-21 08:56:21 +00:00
|
|
|
if (dstPtr - dstPtr0 + dstSize >= 64 KB) /* dstBuffer large enough to become dictionary */
|
2014-09-13 20:21:41 +00:00
|
|
|
{
|
2014-09-21 08:56:21 +00:00
|
|
|
dctxPtr->dict = (BYTE*)dstPtr0;
|
|
|
|
dctxPtr->dictSize = dstPtr - dstPtr0 + dstSize;
|
2014-09-13 20:21:41 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-09-21 08:56:21 +00:00
|
|
|
if ((withinTmp) && (dctxPtr->dict == dctxPtr->tmpOutBuffer))
|
|
|
|
{
|
|
|
|
/* assumption : dctxPtr->dict + dctxPtr->dictSize == dctxPtr->tmpOut + dctxPtr->tmpOutStart */
|
|
|
|
dctxPtr->dictSize += dstSize;
|
|
|
|
return;
|
|
|
|
}
|
2014-09-09 22:54:22 +00:00
|
|
|
|
2014-09-21 08:56:21 +00:00
|
|
|
if (withinTmp) /* copy relevant dict portion in front of tmpOut within tmpOutBuffer */
|
|
|
|
{
|
2014-09-22 00:32:55 +00:00
|
|
|
size_t preserveSize = dctxPtr->tmpOut - dctxPtr->tmpOutBuffer;
|
|
|
|
size_t copySize = 64 KB - dctxPtr->tmpOutSize;
|
|
|
|
BYTE* oldDictEnd = dctxPtr->dict + dctxPtr->dictSize - dctxPtr->tmpOutStart;
|
|
|
|
if (dctxPtr->tmpOutSize > 64 KB) copySize = 0;
|
|
|
|
if (copySize > preserveSize) copySize = preserveSize;
|
|
|
|
|
|
|
|
memcpy(dctxPtr->tmpOutBuffer + preserveSize - copySize, oldDictEnd - copySize, copySize);
|
|
|
|
|
|
|
|
dctxPtr->dict = dctxPtr->tmpOutBuffer;
|
|
|
|
dctxPtr->dictSize = preserveSize + dctxPtr->tmpOutStart + dstSize;
|
|
|
|
return;
|
2014-09-21 08:56:21 +00:00
|
|
|
}
|
2014-09-09 22:54:22 +00:00
|
|
|
|
2014-09-21 08:56:21 +00:00
|
|
|
if (dctxPtr->dict == dctxPtr->tmpOutBuffer) /* copy dst into tmp to complete dict */
|
|
|
|
{
|
|
|
|
if (dctxPtr->dictSize + dstSize > dctxPtr->maxBufferSize) /* tmp buffer not large enough */
|
|
|
|
{
|
|
|
|
size_t preserveSize = 64 KB - dstSize; /* note : dstSize < 64 KB */
|
|
|
|
memcpy(dctxPtr->dict, dctxPtr->dict + dctxPtr->dictSize - preserveSize, preserveSize);
|
|
|
|
dctxPtr->dictSize = preserveSize;
|
|
|
|
}
|
|
|
|
memcpy(dctxPtr->dict + dctxPtr->dictSize, dstPtr, dstSize);
|
|
|
|
dctxPtr->dictSize += dstSize;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* join dict & dest into tmp */
|
|
|
|
{
|
|
|
|
size_t preserveSize = 64 KB - dstSize; /* note : dstSize < 64 KB */
|
|
|
|
if (preserveSize > dctxPtr->dictSize) preserveSize = dctxPtr->dictSize;
|
|
|
|
memcpy(dctxPtr->tmpOutBuffer, dctxPtr->dict + dctxPtr->dictSize - preserveSize, preserveSize);
|
|
|
|
memcpy(dctxPtr->tmpOutBuffer + preserveSize, dstPtr, dstSize);
|
|
|
|
dctxPtr->dict = dctxPtr->tmpOutBuffer;
|
|
|
|
dctxPtr->dictSize = preserveSize + dstSize;
|
|
|
|
}
|
2014-09-09 22:54:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-09-21 08:56:21 +00:00
|
|
|
|
2014-09-01 21:44:02 +00:00
|
|
|
/* LZ4F_decompress()
|
2014-10-27 23:39:06 +00:00
|
|
|
* Call this function repetitively to regenerate data compressed within srcBuffer.
|
|
|
|
* The function will attempt to decode *srcSizePtr from srcBuffer, into dstBuffer of maximum size *dstSizePtr.
|
|
|
|
*
|
|
|
|
* The number of bytes regenerated into dstBuffer will be provided within *dstSizePtr (necessarily <= original value).
|
|
|
|
*
|
|
|
|
* The number of bytes effectively read from srcBuffer will be provided within *srcSizePtr (necessarily <= original value).
|
|
|
|
* If the number of bytes read is < number of bytes provided, then the decompression operation is not complete.
|
|
|
|
* You will have to call it again, continuing from where it stopped.
|
|
|
|
*
|
|
|
|
* The function result is an hint of the better srcSize to use for next call to LZ4F_decompress.
|
|
|
|
* Basically, it's the size of the current (or remaining) compressed block + header of next block.
|
|
|
|
* Respecting the hint provides some boost to performance, since it allows less buffer shuffling.
|
|
|
|
* Note that this is just a hint, you can always provide any srcSize you want.
|
|
|
|
* When a frame is fully decoded, the function result will be 0.
|
|
|
|
* If decompression failed, function result is an error code which can be tested using LZ4F_isError().
|
|
|
|
*/
|
2014-09-13 11:15:54 +00:00
|
|
|
size_t LZ4F_decompress(LZ4F_decompressionContext_t decompressionContext,
|
|
|
|
void* dstBuffer, size_t* dstSizePtr,
|
|
|
|
const void* srcBuffer, size_t* srcSizePtr,
|
|
|
|
const LZ4F_decompressOptions_t* decompressOptionsPtr)
|
2014-09-01 21:44:02 +00:00
|
|
|
{
|
|
|
|
LZ4F_dctx_internal_t* dctxPtr = (LZ4F_dctx_internal_t*)decompressionContext;
|
2015-03-11 19:11:14 +00:00
|
|
|
LZ4F_decompressOptions_t optionsNull;
|
2014-09-01 21:44:02 +00:00
|
|
|
const BYTE* const srcStart = (const BYTE*)srcBuffer;
|
2014-09-13 11:15:54 +00:00
|
|
|
const BYTE* const srcEnd = srcStart + *srcSizePtr;
|
2014-09-01 21:44:02 +00:00
|
|
|
const BYTE* srcPtr = srcStart;
|
|
|
|
BYTE* const dstStart = (BYTE*)dstBuffer;
|
2014-09-13 11:15:54 +00:00
|
|
|
BYTE* const dstEnd = dstStart + *dstSizePtr;
|
2014-09-01 21:44:02 +00:00
|
|
|
BYTE* dstPtr = dstStart;
|
2015-03-15 00:42:27 +00:00
|
|
|
const BYTE* selectedIn = NULL;
|
2014-09-13 11:15:54 +00:00
|
|
|
unsigned doAnotherStage = 1;
|
|
|
|
size_t nextSrcSizeHint = 1;
|
2014-09-03 18:49:59 +00:00
|
|
|
|
2014-09-01 21:44:02 +00:00
|
|
|
|
2015-03-11 19:11:14 +00:00
|
|
|
memset(&optionsNull, 0, sizeof(optionsNull));
|
2014-09-01 21:44:02 +00:00
|
|
|
if (decompressOptionsPtr==NULL) decompressOptionsPtr = &optionsNull;
|
2014-09-22 17:42:00 +00:00
|
|
|
*srcSizePtr = 0;
|
|
|
|
*dstSizePtr = 0;
|
2014-09-01 21:44:02 +00:00
|
|
|
|
2014-09-04 21:56:51 +00:00
|
|
|
/* expect to continue decoding src buffer where it left previously */
|
2014-09-03 18:49:59 +00:00
|
|
|
if (dctxPtr->srcExpect != NULL)
|
2014-09-01 21:44:02 +00:00
|
|
|
{
|
2015-04-15 09:34:08 +00:00
|
|
|
if (srcStart != dctxPtr->srcExpect) return (size_t)-LZ4F_ERROR_srcPtr_wrong;
|
2014-09-01 21:44:02 +00:00
|
|
|
}
|
|
|
|
|
2014-09-13 11:15:54 +00:00
|
|
|
/* programmed as a state machine */
|
|
|
|
|
|
|
|
while (doAnotherStage)
|
2014-09-01 21:44:02 +00:00
|
|
|
{
|
2014-09-13 11:15:54 +00:00
|
|
|
|
2014-09-03 18:49:59 +00:00
|
|
|
switch(dctxPtr->dStage)
|
2014-09-01 21:44:02 +00:00
|
|
|
{
|
2014-09-13 11:15:54 +00:00
|
|
|
|
2014-09-04 21:56:51 +00:00
|
|
|
case dstage_getHeader:
|
2014-09-01 21:44:02 +00:00
|
|
|
{
|
2015-04-19 14:23:53 +00:00
|
|
|
if ((size_t)(srcEnd-srcPtr) >= maxFHSize) /* enough to decode - shortcut */
|
2014-10-27 23:39:06 +00:00
|
|
|
{
|
2015-03-16 16:52:14 +00:00
|
|
|
LZ4F_errorCode_t errorCode = LZ4F_decodeHeader(dctxPtr, srcPtr, srcEnd-srcPtr);
|
|
|
|
if (LZ4F_isError(errorCode)) return errorCode;
|
|
|
|
srcPtr += errorCode;
|
2014-10-27 23:39:06 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
dctxPtr->tmpInSize = 0;
|
2015-04-19 14:23:53 +00:00
|
|
|
dctxPtr->tmpInTarget = minFHSize; /* minimum to attempt decode */
|
2014-10-27 23:39:06 +00:00
|
|
|
dctxPtr->dStage = dstage_storeHeader;
|
2014-09-04 21:56:51 +00:00
|
|
|
}
|
2014-09-13 11:15:54 +00:00
|
|
|
|
2014-09-04 21:56:51 +00:00
|
|
|
case dstage_storeHeader:
|
|
|
|
{
|
2015-03-18 20:38:27 +00:00
|
|
|
size_t sizeToCopy = dctxPtr->tmpInTarget - dctxPtr->tmpInSize;
|
2014-10-27 23:39:06 +00:00
|
|
|
if (sizeToCopy > (size_t)(srcEnd - srcPtr)) sizeToCopy = srcEnd - srcPtr;
|
|
|
|
memcpy(dctxPtr->header + dctxPtr->tmpInSize, srcPtr, sizeToCopy);
|
|
|
|
dctxPtr->tmpInSize += sizeToCopy;
|
|
|
|
srcPtr += sizeToCopy;
|
2015-03-18 20:38:27 +00:00
|
|
|
if (dctxPtr->tmpInSize < dctxPtr->tmpInTarget)
|
2014-10-27 23:39:06 +00:00
|
|
|
{
|
2015-04-19 14:23:53 +00:00
|
|
|
nextSrcSizeHint = (dctxPtr->tmpInTarget - dctxPtr->tmpInSize) + BHSize; /* rest of header + nextBlockHeader */
|
2015-03-18 20:38:27 +00:00
|
|
|
doAnotherStage = 0; /* not enough src data, ask for some more */
|
2014-10-27 23:39:06 +00:00
|
|
|
break;
|
|
|
|
}
|
2015-03-31 13:47:23 +00:00
|
|
|
{
|
|
|
|
LZ4F_errorCode_t errorCode = LZ4F_decodeHeader(dctxPtr, dctxPtr->header, dctxPtr->tmpInTarget);
|
|
|
|
if (LZ4F_isError(errorCode)) return errorCode;
|
|
|
|
}
|
2014-10-27 23:39:06 +00:00
|
|
|
break;
|
|
|
|
}
|
2014-09-13 11:15:54 +00:00
|
|
|
|
2014-09-03 18:49:59 +00:00
|
|
|
case dstage_getCBlockSize:
|
2014-09-01 21:44:02 +00:00
|
|
|
{
|
2015-04-19 14:23:53 +00:00
|
|
|
if ((size_t)(srcEnd - srcPtr) >= BHSize)
|
2014-10-27 23:39:06 +00:00
|
|
|
{
|
|
|
|
selectedIn = srcPtr;
|
2015-04-19 14:23:53 +00:00
|
|
|
srcPtr += BHSize;
|
2014-10-27 23:39:06 +00:00
|
|
|
}
|
2015-03-15 00:42:27 +00:00
|
|
|
else
|
|
|
|
{
|
2014-10-27 23:39:06 +00:00
|
|
|
/* not enough input to read cBlockSize field */
|
2015-03-15 00:42:27 +00:00
|
|
|
dctxPtr->tmpInSize = 0;
|
|
|
|
dctxPtr->dStage = dstage_storeCBlockSize;
|
|
|
|
}
|
2014-09-01 21:44:02 +00:00
|
|
|
}
|
2014-09-13 11:15:54 +00:00
|
|
|
|
2015-03-15 00:42:27 +00:00
|
|
|
if (dctxPtr->dStage == dstage_storeCBlockSize)
|
2014-09-03 18:49:59 +00:00
|
|
|
case dstage_storeCBlockSize:
|
2014-09-01 21:44:02 +00:00
|
|
|
{
|
2015-04-19 14:23:53 +00:00
|
|
|
size_t sizeToCopy = BHSize - dctxPtr->tmpInSize;
|
2014-10-27 23:39:06 +00:00
|
|
|
if (sizeToCopy > (size_t)(srcEnd - srcPtr)) sizeToCopy = srcEnd - srcPtr;
|
|
|
|
memcpy(dctxPtr->tmpIn + dctxPtr->tmpInSize, srcPtr, sizeToCopy);
|
|
|
|
srcPtr += sizeToCopy;
|
|
|
|
dctxPtr->tmpInSize += sizeToCopy;
|
2015-04-19 14:23:53 +00:00
|
|
|
if (dctxPtr->tmpInSize < BHSize) /* not enough input to get full cBlockSize; wait for more */
|
2014-10-27 23:39:06 +00:00
|
|
|
{
|
2015-04-19 14:23:53 +00:00
|
|
|
nextSrcSizeHint = BHSize - dctxPtr->tmpInSize;
|
|
|
|
doAnotherStage = 0;
|
2014-10-27 23:39:06 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
selectedIn = dctxPtr->tmpIn;
|
2014-09-01 21:44:02 +00:00
|
|
|
}
|
2014-09-13 11:15:54 +00:00
|
|
|
|
2015-03-15 00:42:27 +00:00
|
|
|
/* case dstage_decodeCBlockSize: */ /* no more direct access, to prevent scan-build warning */
|
2014-09-22 17:42:00 +00:00
|
|
|
{
|
2014-10-27 23:39:06 +00:00
|
|
|
size_t nextCBlockSize = LZ4F_readLE32(selectedIn) & 0x7FFFFFFFU;
|
|
|
|
if (nextCBlockSize==0) /* frameEnd signal, no more CBlock */
|
|
|
|
{
|
|
|
|
dctxPtr->dStage = dstage_getSuffix;
|
|
|
|
break;
|
|
|
|
}
|
2015-04-14 17:20:47 +00:00
|
|
|
if (nextCBlockSize > dctxPtr->maxBlockSize) return (size_t)-LZ4F_ERROR_GENERIC; /* invalid cBlockSize */
|
2014-10-27 23:39:06 +00:00
|
|
|
dctxPtr->tmpInTarget = nextCBlockSize;
|
|
|
|
if (LZ4F_readLE32(selectedIn) & LZ4F_BLOCKUNCOMPRESSED_FLAG)
|
|
|
|
{
|
|
|
|
dctxPtr->dStage = dstage_copyDirect;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
dctxPtr->dStage = dstage_getCBlock;
|
|
|
|
if (dstPtr==dstEnd)
|
|
|
|
{
|
2015-04-19 14:23:53 +00:00
|
|
|
nextSrcSizeHint = nextCBlockSize + BHSize;
|
2014-10-27 23:39:06 +00:00
|
|
|
doAnotherStage = 0;
|
|
|
|
}
|
2014-09-13 11:15:54 +00:00
|
|
|
break;
|
2014-09-01 21:44:02 +00:00
|
|
|
}
|
2014-09-13 11:15:54 +00:00
|
|
|
|
|
|
|
case dstage_copyDirect: /* uncompressed block */
|
2014-09-01 21:44:02 +00:00
|
|
|
{
|
2014-10-27 23:39:06 +00:00
|
|
|
size_t sizeToCopy = dctxPtr->tmpInTarget;
|
|
|
|
if ((size_t)(srcEnd-srcPtr) < sizeToCopy) sizeToCopy = srcEnd - srcPtr; /* not enough input to read full block */
|
|
|
|
if ((size_t)(dstEnd-dstPtr) < sizeToCopy) sizeToCopy = dstEnd - dstPtr;
|
|
|
|
memcpy(dstPtr, srcPtr, sizeToCopy);
|
2015-03-29 12:28:32 +00:00
|
|
|
if (dctxPtr->frameInfo.contentChecksumFlag) XXH32_update(&(dctxPtr->xxh), srcPtr, sizeToCopy);
|
2015-04-09 21:59:07 +00:00
|
|
|
if (dctxPtr->frameInfo.contentSize) dctxPtr->frameRemainingSize -= sizeToCopy;
|
2014-10-27 23:39:06 +00:00
|
|
|
|
|
|
|
/* dictionary management */
|
2015-04-14 17:20:47 +00:00
|
|
|
if (dctxPtr->frameInfo.blockMode==LZ4F_blockLinked)
|
2014-10-27 23:39:06 +00:00
|
|
|
LZ4F_updateDict(dctxPtr, dstPtr, sizeToCopy, dstStart, 0);
|
|
|
|
|
|
|
|
srcPtr += sizeToCopy;
|
|
|
|
dstPtr += sizeToCopy;
|
|
|
|
if (sizeToCopy == dctxPtr->tmpInTarget) /* all copied */
|
|
|
|
{
|
|
|
|
dctxPtr->dStage = dstage_getCBlockSize;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
dctxPtr->tmpInTarget -= sizeToCopy; /* still need to copy more */
|
2015-04-19 14:23:53 +00:00
|
|
|
nextSrcSizeHint = dctxPtr->tmpInTarget + BHSize;
|
2014-10-27 23:39:06 +00:00
|
|
|
doAnotherStage = 0;
|
2014-09-13 11:15:54 +00:00
|
|
|
break;
|
2014-09-01 21:44:02 +00:00
|
|
|
}
|
2014-09-13 11:15:54 +00:00
|
|
|
|
|
|
|
case dstage_getCBlock: /* entry from dstage_decodeCBlockSize */
|
2014-09-01 21:44:02 +00:00
|
|
|
{
|
2014-10-27 23:39:06 +00:00
|
|
|
if ((size_t)(srcEnd-srcPtr) < dctxPtr->tmpInTarget)
|
|
|
|
{
|
|
|
|
dctxPtr->tmpInSize = 0;
|
|
|
|
dctxPtr->dStage = dstage_storeCBlock;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
selectedIn = srcPtr;
|
|
|
|
srcPtr += dctxPtr->tmpInTarget;
|
|
|
|
dctxPtr->dStage = dstage_decodeCBlock;
|
2014-09-13 11:15:54 +00:00
|
|
|
break;
|
2014-09-03 18:49:59 +00:00
|
|
|
}
|
2014-09-13 11:15:54 +00:00
|
|
|
|
2014-09-03 18:49:59 +00:00
|
|
|
case dstage_storeCBlock:
|
|
|
|
{
|
2014-10-27 23:39:06 +00:00
|
|
|
size_t sizeToCopy = dctxPtr->tmpInTarget - dctxPtr->tmpInSize;
|
|
|
|
if (sizeToCopy > (size_t)(srcEnd-srcPtr)) sizeToCopy = srcEnd-srcPtr;
|
|
|
|
memcpy(dctxPtr->tmpIn + dctxPtr->tmpInSize, srcPtr, sizeToCopy);
|
|
|
|
dctxPtr->tmpInSize += sizeToCopy;
|
|
|
|
srcPtr += sizeToCopy;
|
|
|
|
if (dctxPtr->tmpInSize < dctxPtr->tmpInTarget) /* need more input */
|
|
|
|
{
|
2015-04-19 14:23:53 +00:00
|
|
|
nextSrcSizeHint = (dctxPtr->tmpInTarget - dctxPtr->tmpInSize) + BHSize;
|
2014-10-27 23:39:06 +00:00
|
|
|
doAnotherStage=0;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
selectedIn = dctxPtr->tmpIn;
|
|
|
|
dctxPtr->dStage = dstage_decodeCBlock;
|
2014-09-13 11:15:54 +00:00
|
|
|
break;
|
2014-09-03 18:49:59 +00:00
|
|
|
}
|
2014-09-13 11:15:54 +00:00
|
|
|
|
2014-09-03 18:49:59 +00:00
|
|
|
case dstage_decodeCBlock:
|
2014-10-27 23:39:06 +00:00
|
|
|
{
|
|
|
|
if ((size_t)(dstEnd-dstPtr) < dctxPtr->maxBlockSize) /* not enough place into dst : decode into tmpOut */
|
|
|
|
dctxPtr->dStage = dstage_decodeCBlock_intoTmp;
|
|
|
|
else
|
|
|
|
dctxPtr->dStage = dstage_decodeCBlock_intoDst;
|
|
|
|
break;
|
|
|
|
}
|
2014-09-21 08:56:21 +00:00
|
|
|
|
|
|
|
case dstage_decodeCBlock_intoDst:
|
2014-10-27 23:39:06 +00:00
|
|
|
{
|
|
|
|
int (*decoder)(const char*, char*, int, int, const char*, int);
|
|
|
|
int decodedSize;
|
2014-09-13 11:15:54 +00:00
|
|
|
|
2015-04-14 17:20:47 +00:00
|
|
|
if (dctxPtr->frameInfo.blockMode == LZ4F_blockLinked)
|
2014-10-27 23:39:06 +00:00
|
|
|
decoder = LZ4_decompress_safe_usingDict;
|
|
|
|
else
|
|
|
|
decoder = LZ4F_decompress_safe;
|
2014-09-13 11:15:54 +00:00
|
|
|
|
2014-10-27 23:39:06 +00:00
|
|
|
decodedSize = decoder((const char*)selectedIn, (char*)dstPtr, (int)dctxPtr->tmpInTarget, (int)dctxPtr->maxBlockSize, (const char*)dctxPtr->dict, (int)dctxPtr->dictSize);
|
2015-04-14 17:20:47 +00:00
|
|
|
if (decodedSize < 0) return (size_t)-LZ4F_ERROR_GENERIC; /* decompression failed */
|
2014-10-27 23:39:06 +00:00
|
|
|
if (dctxPtr->frameInfo.contentChecksumFlag) XXH32_update(&(dctxPtr->xxh), dstPtr, decodedSize);
|
2015-04-09 21:59:07 +00:00
|
|
|
if (dctxPtr->frameInfo.contentSize) dctxPtr->frameRemainingSize -= decodedSize;
|
2014-09-21 08:56:21 +00:00
|
|
|
|
2014-10-27 23:39:06 +00:00
|
|
|
/* dictionary management */
|
2015-04-14 17:20:47 +00:00
|
|
|
if (dctxPtr->frameInfo.blockMode==LZ4F_blockLinked)
|
2014-10-27 23:39:06 +00:00
|
|
|
LZ4F_updateDict(dctxPtr, dstPtr, decodedSize, dstStart, 0);
|
2014-09-21 08:56:21 +00:00
|
|
|
|
2014-10-27 23:39:06 +00:00
|
|
|
dstPtr += decodedSize;
|
|
|
|
dctxPtr->dStage = dstage_getCBlockSize;
|
|
|
|
break;
|
|
|
|
}
|
2014-09-13 11:15:54 +00:00
|
|
|
|
2014-09-21 08:56:21 +00:00
|
|
|
case dstage_decodeCBlock_intoTmp:
|
2014-10-27 23:39:06 +00:00
|
|
|
{
|
|
|
|
/* not enough place into dst : decode into tmpOut */
|
|
|
|
int (*decoder)(const char*, char*, int, int, const char*, int);
|
|
|
|
int decodedSize;
|
2014-09-21 08:56:21 +00:00
|
|
|
|
2015-04-14 17:20:47 +00:00
|
|
|
if (dctxPtr->frameInfo.blockMode == LZ4F_blockLinked)
|
2014-10-27 23:39:06 +00:00
|
|
|
decoder = LZ4_decompress_safe_usingDict;
|
|
|
|
else
|
|
|
|
decoder = LZ4F_decompress_safe;
|
2014-09-21 08:56:21 +00:00
|
|
|
|
2014-10-27 23:39:06 +00:00
|
|
|
/* ensure enough place for tmpOut */
|
2015-04-14 17:20:47 +00:00
|
|
|
if (dctxPtr->frameInfo.blockMode == LZ4F_blockLinked)
|
2014-09-21 08:56:21 +00:00
|
|
|
{
|
2014-10-27 23:39:06 +00:00
|
|
|
if (dctxPtr->dict == dctxPtr->tmpOutBuffer)
|
2014-09-21 08:56:21 +00:00
|
|
|
{
|
2014-10-27 23:39:06 +00:00
|
|
|
if (dctxPtr->dictSize > 128 KB)
|
|
|
|
{
|
|
|
|
memcpy(dctxPtr->dict, dctxPtr->dict + dctxPtr->dictSize - 64 KB, 64 KB);
|
|
|
|
dctxPtr->dictSize = 64 KB;
|
|
|
|
}
|
|
|
|
dctxPtr->tmpOut = dctxPtr->dict + dctxPtr->dictSize;
|
|
|
|
}
|
|
|
|
else /* dict not within tmp */
|
|
|
|
{
|
|
|
|
size_t reservedDictSpace = dctxPtr->dictSize;
|
|
|
|
if (reservedDictSpace > 64 KB) reservedDictSpace = 64 KB;
|
|
|
|
dctxPtr->tmpOut = dctxPtr->tmpOutBuffer + reservedDictSpace;
|
2014-09-21 08:56:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-10-27 23:39:06 +00:00
|
|
|
/* Decode */
|
|
|
|
decodedSize = decoder((const char*)selectedIn, (char*)dctxPtr->tmpOut, (int)dctxPtr->tmpInTarget, (int)dctxPtr->maxBlockSize, (const char*)dctxPtr->dict, (int)dctxPtr->dictSize);
|
2015-04-14 17:20:47 +00:00
|
|
|
if (decodedSize < 0) return (size_t)-LZ4F_ERROR_decompressionFailed; /* decompression failed */
|
2014-10-27 23:39:06 +00:00
|
|
|
if (dctxPtr->frameInfo.contentChecksumFlag) XXH32_update(&(dctxPtr->xxh), dctxPtr->tmpOut, decodedSize);
|
2015-04-09 21:59:07 +00:00
|
|
|
if (dctxPtr->frameInfo.contentSize) dctxPtr->frameRemainingSize -= decodedSize;
|
2014-10-27 23:39:06 +00:00
|
|
|
dctxPtr->tmpOutSize = decodedSize;
|
|
|
|
dctxPtr->tmpOutStart = 0;
|
|
|
|
dctxPtr->dStage = dstage_flushOut;
|
|
|
|
break;
|
|
|
|
}
|
2014-09-22 17:42:00 +00:00
|
|
|
|
2014-09-13 11:15:54 +00:00
|
|
|
case dstage_flushOut: /* flush decoded data from tmpOut to dstBuffer */
|
2014-10-27 23:39:06 +00:00
|
|
|
{
|
|
|
|
size_t sizeToCopy = dctxPtr->tmpOutSize - dctxPtr->tmpOutStart;
|
|
|
|
if (sizeToCopy > (size_t)(dstEnd-dstPtr)) sizeToCopy = dstEnd-dstPtr;
|
|
|
|
memcpy(dstPtr, dctxPtr->tmpOut + dctxPtr->tmpOutStart, sizeToCopy);
|
2014-09-21 08:56:21 +00:00
|
|
|
|
2014-10-27 23:39:06 +00:00
|
|
|
/* dictionary management */
|
2015-04-14 17:20:47 +00:00
|
|
|
if (dctxPtr->frameInfo.blockMode==LZ4F_blockLinked)
|
2014-10-27 23:39:06 +00:00
|
|
|
LZ4F_updateDict(dctxPtr, dstPtr, sizeToCopy, dstStart, 1);
|
2014-09-21 08:56:21 +00:00
|
|
|
|
2014-10-27 23:39:06 +00:00
|
|
|
dctxPtr->tmpOutStart += sizeToCopy;
|
|
|
|
dstPtr += sizeToCopy;
|
2014-09-21 08:56:21 +00:00
|
|
|
|
2014-10-27 23:39:06 +00:00
|
|
|
/* end of flush ? */
|
|
|
|
if (dctxPtr->tmpOutStart == dctxPtr->tmpOutSize)
|
|
|
|
{
|
|
|
|
dctxPtr->dStage = dstage_getCBlockSize;
|
|
|
|
break;
|
|
|
|
}
|
2015-04-19 14:23:53 +00:00
|
|
|
nextSrcSizeHint = BHSize;
|
2014-10-27 23:39:06 +00:00
|
|
|
doAnotherStage = 0; /* still some data to flush */
|
2014-09-03 18:49:59 +00:00
|
|
|
break;
|
|
|
|
}
|
2014-09-13 11:15:54 +00:00
|
|
|
|
2014-09-22 17:42:00 +00:00
|
|
|
case dstage_getSuffix:
|
2014-09-03 18:49:59 +00:00
|
|
|
{
|
2014-10-27 23:39:06 +00:00
|
|
|
size_t suffixSize = dctxPtr->frameInfo.contentChecksumFlag * 4;
|
2015-04-14 17:20:47 +00:00
|
|
|
if (dctxPtr->frameRemainingSize) return (size_t)-LZ4F_ERROR_frameSize_wrong; /* incorrect frame size decoded */
|
2014-10-27 23:39:06 +00:00
|
|
|
if (suffixSize == 0) /* frame completed */
|
|
|
|
{
|
|
|
|
nextSrcSizeHint = 0;
|
|
|
|
dctxPtr->dStage = dstage_getHeader;
|
|
|
|
doAnotherStage = 0;
|
|
|
|
break;
|
|
|
|
}
|
2015-03-15 00:42:27 +00:00
|
|
|
if ((srcEnd - srcPtr) < 4) /* not enough size for entire CRC */
|
|
|
|
{
|
|
|
|
dctxPtr->tmpInSize = 0;
|
|
|
|
dctxPtr->dStage = dstage_storeSuffix;
|
|
|
|
}
|
|
|
|
else
|
2014-10-27 23:39:06 +00:00
|
|
|
{
|
|
|
|
selectedIn = srcPtr;
|
|
|
|
srcPtr += 4;
|
|
|
|
}
|
2014-09-03 18:49:59 +00:00
|
|
|
}
|
2014-10-27 23:39:06 +00:00
|
|
|
|
2015-03-15 00:42:27 +00:00
|
|
|
if (dctxPtr->dStage == dstage_storeSuffix)
|
2014-10-27 23:39:06 +00:00
|
|
|
case dstage_storeSuffix:
|
2014-09-03 18:49:59 +00:00
|
|
|
{
|
2014-10-27 23:39:06 +00:00
|
|
|
size_t sizeToCopy = 4 - dctxPtr->tmpInSize;
|
|
|
|
if (sizeToCopy > (size_t)(srcEnd - srcPtr)) sizeToCopy = srcEnd - srcPtr;
|
|
|
|
memcpy(dctxPtr->tmpIn + dctxPtr->tmpInSize, srcPtr, sizeToCopy);
|
|
|
|
srcPtr += sizeToCopy;
|
|
|
|
dctxPtr->tmpInSize += sizeToCopy;
|
|
|
|
if (dctxPtr->tmpInSize < 4) /* not enough input to read complete suffix */
|
|
|
|
{
|
|
|
|
nextSrcSizeHint = 4 - dctxPtr->tmpInSize;
|
|
|
|
doAnotherStage=0;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
selectedIn = dctxPtr->tmpIn;
|
2014-09-03 18:49:59 +00:00
|
|
|
}
|
2014-09-13 11:15:54 +00:00
|
|
|
|
2015-03-15 00:42:27 +00:00
|
|
|
/* case dstage_checkSuffix: */ /* no direct call, to avoid scan-build warning */
|
2014-09-03 18:49:59 +00:00
|
|
|
{
|
2014-10-27 23:39:06 +00:00
|
|
|
U32 readCRC = LZ4F_readLE32(selectedIn);
|
|
|
|
U32 resultCRC = XXH32_digest(&(dctxPtr->xxh));
|
2015-04-15 09:34:08 +00:00
|
|
|
if (readCRC != resultCRC) return (size_t)-LZ4F_ERROR_contentChecksum_invalid;
|
2014-10-27 23:39:06 +00:00
|
|
|
nextSrcSizeHint = 0;
|
|
|
|
dctxPtr->dStage = dstage_getHeader;
|
|
|
|
doAnotherStage = 0;
|
2014-09-13 11:15:54 +00:00
|
|
|
break;
|
2014-09-01 21:44:02 +00:00
|
|
|
}
|
2015-03-16 16:52:14 +00:00
|
|
|
|
2015-03-18 20:38:27 +00:00
|
|
|
case dstage_getSFrameSize:
|
2015-03-16 16:52:14 +00:00
|
|
|
{
|
|
|
|
if ((srcEnd - srcPtr) >= 4)
|
|
|
|
{
|
|
|
|
selectedIn = srcPtr;
|
|
|
|
srcPtr += 4;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* not enough input to read cBlockSize field */
|
2015-03-18 20:38:27 +00:00
|
|
|
dctxPtr->tmpInSize = 4;
|
|
|
|
dctxPtr->tmpInTarget = 8;
|
|
|
|
dctxPtr->dStage = dstage_storeSFrameSize;
|
2015-03-16 16:52:14 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-18 20:38:27 +00:00
|
|
|
if (dctxPtr->dStage == dstage_storeSFrameSize)
|
|
|
|
case dstage_storeSFrameSize:
|
2015-03-16 16:52:14 +00:00
|
|
|
{
|
2015-03-18 20:38:27 +00:00
|
|
|
size_t sizeToCopy = dctxPtr->tmpInTarget - dctxPtr->tmpInSize;
|
2015-03-16 16:52:14 +00:00
|
|
|
if (sizeToCopy > (size_t)(srcEnd - srcPtr)) sizeToCopy = srcEnd - srcPtr;
|
|
|
|
memcpy(dctxPtr->header + dctxPtr->tmpInSize, srcPtr, sizeToCopy);
|
|
|
|
srcPtr += sizeToCopy;
|
|
|
|
dctxPtr->tmpInSize += sizeToCopy;
|
2015-03-18 20:38:27 +00:00
|
|
|
if (dctxPtr->tmpInSize < dctxPtr->tmpInTarget) /* not enough input to get full sBlockSize; wait for more */
|
2015-03-16 16:52:14 +00:00
|
|
|
{
|
2015-03-18 20:38:27 +00:00
|
|
|
nextSrcSizeHint = dctxPtr->tmpInTarget - dctxPtr->tmpInSize;
|
|
|
|
doAnotherStage = 0;
|
2015-03-16 16:52:14 +00:00
|
|
|
break;
|
|
|
|
}
|
2015-03-18 20:38:27 +00:00
|
|
|
selectedIn = dctxPtr->header + 4;
|
2015-03-16 16:52:14 +00:00
|
|
|
}
|
|
|
|
|
2015-04-09 21:59:07 +00:00
|
|
|
/* case dstage_decodeSFrameSize: */ /* no direct access */
|
2015-03-16 16:52:14 +00:00
|
|
|
{
|
2015-03-18 20:38:27 +00:00
|
|
|
size_t SFrameSize = LZ4F_readLE32(selectedIn);
|
2015-03-29 10:20:09 +00:00
|
|
|
dctxPtr->frameInfo.contentSize = SFrameSize;
|
2015-03-18 20:38:27 +00:00
|
|
|
dctxPtr->tmpInTarget = SFrameSize;
|
2015-03-16 16:52:14 +00:00
|
|
|
dctxPtr->dStage = dstage_skipSkippable;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case dstage_skipSkippable:
|
|
|
|
{
|
|
|
|
size_t skipSize = dctxPtr->tmpInTarget;
|
|
|
|
if (skipSize > (size_t)(srcEnd-srcPtr)) skipSize = srcEnd-srcPtr;
|
|
|
|
srcPtr += skipSize;
|
|
|
|
dctxPtr->tmpInTarget -= skipSize;
|
|
|
|
doAnotherStage = 0;
|
|
|
|
nextSrcSizeHint = dctxPtr->tmpInTarget;
|
|
|
|
if (nextSrcSizeHint) break;
|
|
|
|
dctxPtr->dStage = dstage_getHeader;
|
|
|
|
break;
|
|
|
|
}
|
2014-09-01 21:44:02 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-09-21 08:56:21 +00:00
|
|
|
/* preserve dictionary within tmp if necessary */
|
2015-04-14 17:20:47 +00:00
|
|
|
if ( (dctxPtr->frameInfo.blockMode==LZ4F_blockLinked)
|
2014-10-27 23:39:06 +00:00
|
|
|
&&(dctxPtr->dict != dctxPtr->tmpOutBuffer)
|
|
|
|
&&(!decompressOptionsPtr->stableDst)
|
|
|
|
&&((unsigned)(dctxPtr->dStage-1) < (unsigned)(dstage_getSuffix-1))
|
|
|
|
)
|
2014-09-21 08:56:21 +00:00
|
|
|
{
|
|
|
|
if (dctxPtr->dStage == dstage_flushOut)
|
|
|
|
{
|
|
|
|
size_t preserveSize = dctxPtr->tmpOut - dctxPtr->tmpOutBuffer;
|
2014-09-22 00:32:55 +00:00
|
|
|
size_t copySize = 64 KB - dctxPtr->tmpOutSize;
|
2014-09-21 08:56:21 +00:00
|
|
|
BYTE* oldDictEnd = dctxPtr->dict + dctxPtr->dictSize - dctxPtr->tmpOutStart;
|
2014-09-22 00:32:55 +00:00
|
|
|
if (dctxPtr->tmpOutSize > 64 KB) copySize = 0;
|
|
|
|
if (copySize > preserveSize) copySize = preserveSize;
|
2014-09-21 08:56:21 +00:00
|
|
|
|
2014-09-22 00:32:55 +00:00
|
|
|
memcpy(dctxPtr->tmpOutBuffer + preserveSize - copySize, oldDictEnd - copySize, copySize);
|
2014-09-21 08:56:21 +00:00
|
|
|
|
|
|
|
dctxPtr->dict = dctxPtr->tmpOutBuffer;
|
|
|
|
dctxPtr->dictSize = preserveSize + dctxPtr->tmpOutStart;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
size_t newDictSize = dctxPtr->dictSize;
|
|
|
|
BYTE* oldDictEnd = dctxPtr->dict + dctxPtr->dictSize;
|
|
|
|
if ((newDictSize) > 64 KB) newDictSize = 64 KB;
|
|
|
|
|
|
|
|
memcpy(dctxPtr->tmpOutBuffer, oldDictEnd - newDictSize, newDictSize);
|
|
|
|
|
|
|
|
dctxPtr->dict = dctxPtr->tmpOutBuffer;
|
|
|
|
dctxPtr->dictSize = newDictSize;
|
|
|
|
dctxPtr->tmpOut = dctxPtr->tmpOutBuffer + newDictSize;
|
|
|
|
}
|
|
|
|
}
|
2014-09-03 18:49:59 +00:00
|
|
|
|
2015-03-16 16:52:14 +00:00
|
|
|
/* require function to be called again from position where it stopped */
|
|
|
|
if (srcPtr<srcEnd)
|
2014-09-04 21:56:51 +00:00
|
|
|
dctxPtr->srcExpect = srcPtr;
|
2014-09-03 18:49:59 +00:00
|
|
|
else
|
|
|
|
dctxPtr->srcExpect = NULL;
|
2015-03-16 16:52:14 +00:00
|
|
|
|
2014-09-13 11:15:54 +00:00
|
|
|
*srcSizePtr = (srcPtr - srcStart);
|
|
|
|
*dstSizePtr = (dstPtr - dstStart);
|
|
|
|
return nextSrcSizeHint;
|
2014-09-01 21:44:02 +00:00
|
|
|
}
|