2014-08-29 15:35:13 +00:00
|
|
|
/*
|
2014-10-27 23:39:06 +00:00
|
|
|
LZ4 auto-framing library
|
2016-06-29 19:48:27 +00:00
|
|
|
Copyright (C) 2011-2016, 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 :
|
2016-06-29 19:48:27 +00:00
|
|
|
- LZ4 homepage : http://www.lz4.org
|
2016-11-03 14:12:57 +00:00
|
|
|
- LZ4 source repository : https://github.com/lz4/lz4
|
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
|
|
|
|
|
|
|
|
2016-06-29 19:48:27 +00:00
|
|
|
/*-************************************
|
2015-04-27 01:31:56 +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
|
|
|
|
|
|
|
|
|
2018-01-26 16:29:00 +00:00
|
|
|
/*-************************************
|
|
|
|
* Tuning parameters
|
|
|
|
**************************************/
|
|
|
|
/*
|
2018-03-12 20:11:44 +00:00
|
|
|
* LZ4F_HEAPMODE :
|
2018-01-26 16:29:00 +00:00
|
|
|
* Select how default compression functions will allocate memory for their hash table,
|
|
|
|
* in memory stack (0:default, fastest), or in memory heap (1:requires malloc()).
|
|
|
|
*/
|
2018-03-12 20:11:44 +00:00
|
|
|
#ifndef LZ4F_HEAPMODE
|
|
|
|
# define LZ4F_HEAPMODE 0
|
2018-01-26 16:29:00 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
|
2016-06-29 19:48:27 +00:00
|
|
|
/*-************************************
|
2015-03-10 15:57:42 +00:00
|
|
|
* Memory routines
|
2014-08-29 15:35:13 +00:00
|
|
|
**************************************/
|
|
|
|
#include <stdlib.h> /* malloc, calloc, free */
|
2018-01-26 22:29:50 +00:00
|
|
|
#define ALLOC(s) malloc(s)
|
|
|
|
#define ALLOC_AND_ZERO(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
|
|
|
|
|
|
|
|
|
2016-06-29 19:48:27 +00:00
|
|
|
/*-************************************
|
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"
|
2017-08-10 07:48:19 +00:00
|
|
|
#define LZ4_HC_STATIC_LINKING_ONLY
|
2014-08-29 15:35:13 +00:00
|
|
|
#include "lz4hc.h"
|
2016-06-29 19:48:27 +00:00
|
|
|
#define XXH_STATIC_LINKING_ONLY
|
2014-08-29 15:35:13 +00:00
|
|
|
#include "xxhash.h"
|
|
|
|
|
|
|
|
|
2016-11-09 10:45:55 +00:00
|
|
|
/*-************************************
|
2017-08-12 00:46:52 +00:00
|
|
|
* Debug
|
2016-11-09 10:45:55 +00:00
|
|
|
**************************************/
|
2018-01-14 06:47:46 +00:00
|
|
|
#if defined(LZ4_DEBUG) && (LZ4_DEBUG>=1)
|
|
|
|
# include <assert.h>
|
|
|
|
#else
|
|
|
|
# ifndef assert
|
|
|
|
# define assert(condition) ((void)0)
|
|
|
|
# endif
|
|
|
|
#endif
|
|
|
|
|
2017-08-17 17:55:30 +00:00
|
|
|
#define LZ4F_STATIC_ASSERT(c) { enum { LZ4F_static_assert = 1/(int)(!!(c)) }; } /* use only *after* variable declarations */
|
2016-11-09 10:45:55 +00:00
|
|
|
|
|
|
|
|
2016-06-29 19:48:27 +00:00
|
|
|
/*-************************************
|
2015-03-10 15:57:42 +00:00
|
|
|
* Basic Types
|
2014-08-29 15:35:13 +00:00
|
|
|
**************************************/
|
2016-08-11 12:44:53 +00:00
|
|
|
#if !defined (__VMS) && (defined (__cplusplus) || (defined (__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L) /* C99 */) )
|
2014-08-29 15:35:13 +00:00
|
|
|
# include <stdint.h>
|
2016-08-11 12:44:53 +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
|
2016-08-11 12:44:53 +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
|
|
|
|
|
2016-11-04 03:32:51 +00:00
|
|
|
|
2016-08-11 12:44:53 +00:00
|
|
|
/* unoptimized version; solves endianess & alignment issues */
|
|
|
|
static U32 LZ4F_readLE32 (const void* src)
|
|
|
|
{
|
|
|
|
const BYTE* const srcPtr = (const BYTE*)src;
|
|
|
|
U32 value32 = srcPtr[0];
|
|
|
|
value32 += (srcPtr[1]<<8);
|
|
|
|
value32 += (srcPtr[2]<<16);
|
|
|
|
value32 += ((U32)srcPtr[3])<<24;
|
|
|
|
return value32;
|
|
|
|
}
|
|
|
|
|
2016-11-04 03:32:51 +00:00
|
|
|
static void LZ4F_writeLE32 (void* dst, U32 value32)
|
2016-08-11 12:44:53 +00:00
|
|
|
{
|
2016-11-04 03:32:51 +00:00
|
|
|
BYTE* const dstPtr = (BYTE*)dst;
|
2016-08-11 12:44:53 +00:00
|
|
|
dstPtr[0] = (BYTE)value32;
|
|
|
|
dstPtr[1] = (BYTE)(value32 >> 8);
|
|
|
|
dstPtr[2] = (BYTE)(value32 >> 16);
|
|
|
|
dstPtr[3] = (BYTE)(value32 >> 24);
|
|
|
|
}
|
|
|
|
|
2016-11-04 03:32:51 +00:00
|
|
|
static U64 LZ4F_readLE64 (const void* src)
|
2016-08-11 12:44:53 +00:00
|
|
|
{
|
2016-11-04 03:32:51 +00:00
|
|
|
const BYTE* const srcPtr = (const BYTE*)src;
|
2016-08-11 12:44:53 +00:00
|
|
|
U64 value64 = srcPtr[0];
|
|
|
|
value64 += ((U64)srcPtr[1]<<8);
|
|
|
|
value64 += ((U64)srcPtr[2]<<16);
|
|
|
|
value64 += ((U64)srcPtr[3]<<24);
|
|
|
|
value64 += ((U64)srcPtr[4]<<32);
|
|
|
|
value64 += ((U64)srcPtr[5]<<40);
|
|
|
|
value64 += ((U64)srcPtr[6]<<48);
|
|
|
|
value64 += ((U64)srcPtr[7]<<56);
|
|
|
|
return value64;
|
|
|
|
}
|
|
|
|
|
2016-11-04 03:32:51 +00:00
|
|
|
static void LZ4F_writeLE64 (void* dst, U64 value64)
|
2016-08-11 12:44:53 +00:00
|
|
|
{
|
2016-11-04 03:32:51 +00:00
|
|
|
BYTE* const dstPtr = (BYTE*)dst;
|
2016-08-11 12:44:53 +00:00
|
|
|
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-08-29 15:35:13 +00:00
|
|
|
|
2016-06-29 19:48:27 +00:00
|
|
|
/*-************************************
|
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;
|
2017-08-09 00:43:11 +00:00
|
|
|
static const size_t maxFHSize = LZ4F_HEADER_SIZE_MAX; /* 19 */
|
2015-04-19 14:23:53 +00:00
|
|
|
static const size_t BHSize = 4;
|
2014-08-29 15:35:13 +00:00
|
|
|
|
2015-04-27 01:31:56 +00:00
|
|
|
|
2016-06-29 19:48:27 +00:00
|
|
|
/*-************************************
|
2015-03-10 15:57:42 +00:00
|
|
|
* Structures and local types
|
2014-08-29 15:35:13 +00:00
|
|
|
**************************************/
|
2015-04-27 01:31:56 +00:00
|
|
|
typedef struct LZ4F_cctx_s
|
2014-09-22 17:42:00 +00:00
|
|
|
{
|
|
|
|
LZ4F_preferences_t prefs;
|
2015-03-18 20:38:27 +00:00
|
|
|
U32 version;
|
|
|
|
U32 cStage;
|
2017-08-10 19:12:53 +00:00
|
|
|
const LZ4F_CDict* cdict;
|
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;
|
2018-03-21 14:07:24 +00:00
|
|
|
U16 lz4CtxAlloc; /* sized for: 0 = none, 1 = lz4 ctx, 2 = lz4hc ctx */
|
|
|
|
U16 lz4CtxState; /* in use as: 0 = none, 1 = lz4 ctx, 2 = lz4hc ctx */
|
2015-04-27 01:31:56 +00:00
|
|
|
} LZ4F_cctx_t;
|
2014-08-29 15:35:13 +00:00
|
|
|
|
|
|
|
|
2016-06-29 19:48:27 +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;
|
|
|
|
}
|
|
|
|
|
2016-11-11 23:11:56 +00:00
|
|
|
LZ4F_errorCodes LZ4F_getErrorCode(size_t functionResult)
|
|
|
|
{
|
|
|
|
if (!LZ4F_isError(functionResult)) return LZ4F_OK_NoError;
|
2016-11-11 23:40:58 +00:00
|
|
|
return (LZ4F_errorCodes)(-(ptrdiff_t)functionResult);
|
2016-11-11 23:11:56 +00:00
|
|
|
}
|
|
|
|
|
2016-11-09 15:07:28 +00:00
|
|
|
static LZ4F_errorCode_t err0r(LZ4F_errorCodes code)
|
|
|
|
{
|
2017-05-10 19:25:05 +00:00
|
|
|
/* A compilation error here means sizeof(ptrdiff_t) is not large enough */
|
2017-08-17 17:55:30 +00:00
|
|
|
LZ4F_STATIC_ASSERT(sizeof(ptrdiff_t) >= sizeof(size_t));
|
2016-11-11 23:11:56 +00:00
|
|
|
return (LZ4F_errorCode_t)-(ptrdiff_t)code;
|
2016-11-09 15:07:28 +00:00
|
|
|
}
|
2016-11-04 03:32:51 +00:00
|
|
|
|
2016-11-04 00:31:41 +00:00
|
|
|
unsigned LZ4F_getVersion(void) { return LZ4F_VERSION; }
|
2016-11-04 00:14:25 +00:00
|
|
|
|
2017-08-10 07:48:19 +00:00
|
|
|
int LZ4F_compressionLevel_max(void) { return LZ4HC_CLEVEL_MAX; }
|
|
|
|
|
2014-09-21 08:56:21 +00:00
|
|
|
|
2016-06-29 19:48:27 +00:00
|
|
|
/*-************************************
|
2015-03-10 15:57:42 +00:00
|
|
|
* Private functions
|
2014-08-29 15:35:13 +00:00
|
|
|
**************************************/
|
2016-11-11 23:11:56 +00:00
|
|
|
#define MIN(a,b) ( (a) < (b) ? (a) : (b) )
|
|
|
|
|
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;
|
2016-11-04 03:32:51 +00:00
|
|
|
if (blockSizeID > 3) return err0r(LZ4F_ERROR_maxBlockSize_invalid);
|
2014-09-22 17:42:00 +00:00
|
|
|
return blockSizes[blockSizeID];
|
2014-08-29 15:35:13 +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
|
|
|
{
|
2016-11-04 01:34:54 +00:00
|
|
|
U32 const 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
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-06-29 19:48:27 +00:00
|
|
|
/*-************************************
|
2016-11-11 23:11:56 +00:00
|
|
|
* Simple-pass compression functions
|
2014-08-29 15:35:13 +00:00
|
|
|
**************************************/
|
2017-05-10 19:25:05 +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;
|
2016-11-11 23:11:56 +00:00
|
|
|
while (requestedBSID > proposedBSID) {
|
2015-03-16 21:35:02 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2017-08-12 00:46:52 +00:00
|
|
|
/*! LZ4F_compressBound_internal() :
|
|
|
|
* Provides dstCapacity given a srcSize to guarantee operation success in worst case situations.
|
|
|
|
* prefsPtr is optional : if NULL is provided, preferences will be set to cover worst case scenario.
|
|
|
|
* @return is always the same for a srcSize and prefsPtr, so it can be relied upon to size reusable buffers.
|
|
|
|
* When srcSize==0, LZ4F_compressBound() provides an upper bound for LZ4F_flush() and LZ4F_compressEnd() operations.
|
2016-12-21 13:18:01 +00:00
|
|
|
*/
|
2017-05-10 19:25:05 +00:00
|
|
|
static size_t LZ4F_compressBound_internal(size_t srcSize,
|
|
|
|
const LZ4F_preferences_t* preferencesPtr,
|
|
|
|
size_t alreadyBuffered)
|
2016-11-11 23:11:56 +00:00
|
|
|
{
|
|
|
|
LZ4F_preferences_t prefsNull;
|
2018-01-26 22:29:50 +00:00
|
|
|
MEM_INIT(&prefsNull, 0, sizeof(prefsNull));
|
2016-11-11 23:11:56 +00:00
|
|
|
prefsNull.frameInfo.contentChecksumFlag = LZ4F_contentChecksumEnabled; /* worst case */
|
|
|
|
{ const LZ4F_preferences_t* const prefsPtr = (preferencesPtr==NULL) ? &prefsNull : preferencesPtr;
|
2016-12-21 13:18:01 +00:00
|
|
|
U32 const flush = prefsPtr->autoFlush | (srcSize==0);
|
2017-08-12 00:46:52 +00:00
|
|
|
LZ4F_blockSizeID_t const blockID = prefsPtr->frameInfo.blockSizeID;
|
|
|
|
size_t const blockSize = LZ4F_getBlockSize(blockID);
|
2016-11-11 23:11:56 +00:00
|
|
|
size_t const maxBuffered = blockSize - 1;
|
|
|
|
size_t const bufferedSize = MIN(alreadyBuffered, maxBuffered);
|
|
|
|
size_t const maxSrcSize = srcSize + bufferedSize;
|
|
|
|
unsigned const nbFullBlocks = (unsigned)(maxSrcSize / blockSize);
|
2018-01-14 06:39:39 +00:00
|
|
|
size_t const partialBlockSize = maxSrcSize & (blockSize-1);
|
2016-12-21 13:18:01 +00:00
|
|
|
size_t const lastBlockSize = flush ? partialBlockSize : 0;
|
2016-11-11 23:11:56 +00:00
|
|
|
unsigned const nbBlocks = nbFullBlocks + (lastBlockSize>0);
|
|
|
|
|
2017-08-12 00:46:52 +00:00
|
|
|
size_t const blockHeaderSize = 4;
|
|
|
|
size_t const blockCRCSize = 4 * prefsPtr->frameInfo.blockChecksumFlag;
|
2016-11-11 23:11:56 +00:00
|
|
|
size_t const frameEnd = 4 + (prefsPtr->frameInfo.contentChecksumFlag*4);
|
|
|
|
|
2017-08-12 00:46:52 +00:00
|
|
|
return ((blockHeaderSize + blockCRCSize) * nbBlocks) +
|
|
|
|
(blockSize * nbFullBlocks) + lastBlockSize + frameEnd;
|
2016-11-11 23:11:56 +00:00
|
|
|
}
|
|
|
|
}
|
2015-03-16 21:35:02 +00:00
|
|
|
|
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;
|
2017-08-09 00:43:11 +00:00
|
|
|
size_t const headerSize = maxFHSize; /* max header size, including optional fields */
|
2014-08-30 17:14:44 +00:00
|
|
|
|
2014-09-22 17:42:00 +00:00
|
|
|
if (preferencesPtr!=NULL) prefs = *preferencesPtr;
|
2018-01-26 22:29:50 +00:00
|
|
|
else MEM_INIT(&prefs, 0, sizeof(prefs));
|
2014-09-22 17:42:00 +00:00
|
|
|
prefs.autoFlush = 1;
|
2014-08-30 17:14:44 +00:00
|
|
|
|
2016-11-11 23:11:56 +00:00
|
|
|
return headerSize + LZ4F_compressBound_internal(srcSize, &prefs, 0);;
|
2014-08-29 15:35:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-08-09 23:51:19 +00:00
|
|
|
/*! LZ4F_compressFrame_usingCDict() :
|
|
|
|
* Compress srcBuffer using a dictionary, in a single step.
|
|
|
|
* cdict can be NULL, in which case, no dictionary is used.
|
|
|
|
* dstBuffer MUST be >= LZ4F_compressFrameBound(srcSize, preferencesPtr).
|
|
|
|
* The LZ4F_preferences_t structure is optional : you may provide NULL as argument,
|
|
|
|
* however, it's the only way to provide a dictID, so it's not recommended.
|
2017-08-10 07:48:19 +00:00
|
|
|
* @return : number of bytes written into dstBuffer,
|
2017-08-09 23:51:19 +00:00
|
|
|
* or an error code if it fails (can be tested using LZ4F_isError())
|
2017-05-10 19:25:05 +00:00
|
|
|
*/
|
2018-01-26 16:29:00 +00:00
|
|
|
size_t LZ4F_compressFrame_usingCDict(LZ4F_cctx* cctx,
|
|
|
|
void* dstBuffer, size_t dstCapacity,
|
2017-08-09 23:51:19 +00:00
|
|
|
const void* srcBuffer, size_t srcSize,
|
|
|
|
const LZ4F_CDict* cdict,
|
|
|
|
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;
|
|
|
|
LZ4F_compressOptions_t options;
|
2014-09-22 17:42:00 +00:00
|
|
|
BYTE* const dstStart = (BYTE*) dstBuffer;
|
|
|
|
BYTE* dstPtr = dstStart;
|
2016-11-04 01:34:54 +00:00
|
|
|
BYTE* const dstEnd = dstStart + dstCapacity;
|
2014-08-30 17:14:44 +00:00
|
|
|
|
2015-04-27 01:31:56 +00:00
|
|
|
if (preferencesPtr!=NULL)
|
|
|
|
prefs = *preferencesPtr;
|
2015-03-18 20:38:27 +00:00
|
|
|
else
|
2018-01-26 22:29:50 +00:00
|
|
|
MEM_INIT(&prefs, 0, sizeof(prefs));
|
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 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))
|
2017-08-09 23:51:19 +00:00
|
|
|
prefs.frameInfo.blockMode = LZ4F_blockIndependent; /* only one block => no need for inter-block link */
|
2014-09-22 01:36:20 +00:00
|
|
|
|
2018-01-26 22:29:50 +00:00
|
|
|
MEM_INIT(&options, 0, sizeof(options));
|
2014-09-13 09:08:55 +00:00
|
|
|
options.stableSrc = 1;
|
2014-09-12 18:28:44 +00:00
|
|
|
|
2017-08-09 23:51:19 +00:00
|
|
|
if (dstCapacity < LZ4F_compressFrameBound(srcSize, &prefs)) /* condition to guarantee success */
|
2016-11-04 03:32:51 +00:00
|
|
|
return err0r(LZ4F_ERROR_dstMaxSize_tooSmall);
|
2014-08-30 17:14:44 +00:00
|
|
|
|
2018-01-26 16:29:00 +00:00
|
|
|
{ size_t const headerSize = LZ4F_compressBegin_usingCDict(cctx, dstBuffer, dstCapacity, cdict, &prefs); /* write header */
|
2016-11-04 01:34:54 +00:00
|
|
|
if (LZ4F_isError(headerSize)) return headerSize;
|
|
|
|
dstPtr += headerSize; /* header size */ }
|
2014-08-30 17:14:44 +00:00
|
|
|
|
2018-01-26 16:29:00 +00:00
|
|
|
{ size_t const cSize = LZ4F_compressUpdate(cctx, dstPtr, dstEnd-dstPtr, srcBuffer, srcSize, &options);
|
2016-11-04 01:34:54 +00:00
|
|
|
if (LZ4F_isError(cSize)) return cSize;
|
|
|
|
dstPtr += cSize; }
|
2014-08-30 17:14:44 +00:00
|
|
|
|
2018-01-26 16:29:00 +00:00
|
|
|
{ size_t const tailSize = LZ4F_compressEnd(cctx, dstPtr, dstEnd-dstPtr, &options); /* flush last block, and generate suffix */
|
2016-11-04 01:34:54 +00:00
|
|
|
if (LZ4F_isError(tailSize)) return tailSize;
|
|
|
|
dstPtr += tailSize; }
|
2014-08-29 15:35:13 +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
|
|
|
|
|
|
|
|
2017-08-09 23:51:19 +00:00
|
|
|
/*! LZ4F_compressFrame() :
|
|
|
|
* Compress an entire srcBuffer into a valid LZ4 frame, in a single step.
|
|
|
|
* dstBuffer MUST be >= LZ4F_compressFrameBound(srcSize, preferencesPtr).
|
|
|
|
* The LZ4F_preferences_t structure is optional : you can provide NULL as argument. All preferences will be set to default.
|
|
|
|
* @return : number of bytes written into dstBuffer.
|
|
|
|
* or an error code if it fails (can be tested using LZ4F_isError())
|
|
|
|
*/
|
|
|
|
size_t LZ4F_compressFrame(void* dstBuffer, size_t dstCapacity,
|
|
|
|
const void* srcBuffer, size_t srcSize,
|
|
|
|
const LZ4F_preferences_t* preferencesPtr)
|
|
|
|
{
|
2018-01-26 16:29:00 +00:00
|
|
|
size_t result;
|
2018-03-12 20:11:44 +00:00
|
|
|
#if (LZ4F_HEAPMODE)
|
2018-01-26 16:29:00 +00:00
|
|
|
LZ4F_cctx_t *cctxPtr;
|
2018-03-13 21:35:44 +00:00
|
|
|
result = LZ4F_createCompressionContext(&cctxPtr, LZ4F_VERSION);
|
|
|
|
if (LZ4F_isError(result)) return result;
|
2018-01-26 16:29:00 +00:00
|
|
|
#else
|
|
|
|
LZ4F_cctx_t cctx;
|
|
|
|
LZ4_stream_t lz4ctx;
|
|
|
|
LZ4F_cctx_t *cctxPtr = &cctx;
|
|
|
|
|
|
|
|
MEM_INIT(&cctx, 0, sizeof(cctx));
|
|
|
|
cctx.version = LZ4F_VERSION;
|
|
|
|
cctx.maxBufferSize = 5 MB; /* mess with real buffer size to prevent dynamic allocation; works only because autoflush==1 & stableSrc==1 */
|
|
|
|
if (preferencesPtr == NULL ||
|
2018-03-12 20:11:44 +00:00
|
|
|
preferencesPtr->compressionLevel < LZ4HC_CLEVEL_MIN)
|
|
|
|
{
|
2018-01-26 16:29:00 +00:00
|
|
|
LZ4_resetStream(&lz4ctx);
|
|
|
|
cctxPtr->lz4CtxPtr = &lz4ctx;
|
2018-03-13 21:45:09 +00:00
|
|
|
cctxPtr->lz4CtxAlloc = 1;
|
|
|
|
cctxPtr->lz4CtxState = 1;
|
2018-01-26 16:29:00 +00:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
result = LZ4F_compressFrame_usingCDict(cctxPtr, dstBuffer, dstCapacity,
|
|
|
|
srcBuffer, srcSize,
|
|
|
|
NULL, preferencesPtr);
|
|
|
|
|
2018-03-12 20:11:44 +00:00
|
|
|
#if (LZ4F_HEAPMODE)
|
2018-01-26 16:29:00 +00:00
|
|
|
LZ4F_freeCompressionContext(cctxPtr);
|
|
|
|
#else
|
|
|
|
if (preferencesPtr != NULL &&
|
2018-03-12 20:11:44 +00:00
|
|
|
preferencesPtr->compressionLevel >= LZ4HC_CLEVEL_MIN)
|
|
|
|
{
|
2018-01-26 16:29:00 +00:00
|
|
|
FREEMEM(cctxPtr->lz4CtxPtr);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
return result;
|
2017-08-09 23:51:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*-***************************************************
|
|
|
|
* Dictionary compression
|
|
|
|
*****************************************************/
|
|
|
|
|
|
|
|
struct LZ4F_CDict_s {
|
|
|
|
void* dictContent;
|
|
|
|
LZ4_stream_t* fastCtx;
|
|
|
|
LZ4_streamHC_t* HCCtx;
|
|
|
|
}; /* typedef'd to LZ4F_CDict within lz4frame_static.h */
|
|
|
|
|
2017-08-12 00:46:52 +00:00
|
|
|
/*! LZ4F_createCDict() :
|
2017-08-09 23:51:19 +00:00
|
|
|
* When compressing multiple messages / blocks with the same dictionary, it's recommended to load it just once.
|
2017-08-12 00:46:52 +00:00
|
|
|
* LZ4F_createCDict() will create a digested dictionary, ready to start future compression operations without startup delay.
|
|
|
|
* LZ4F_CDict can be created once and shared by multiple threads concurrently, since its usage is read-only.
|
|
|
|
* `dictBuffer` can be released after LZ4F_CDict creation, since its content is copied within CDict
|
2017-08-09 23:51:19 +00:00
|
|
|
* @return : digested dictionary for compression, or NULL if failed */
|
|
|
|
LZ4F_CDict* LZ4F_createCDict(const void* dictBuffer, size_t dictSize)
|
|
|
|
{
|
|
|
|
const char* dictStart = (const char*)dictBuffer;
|
|
|
|
LZ4F_CDict* cdict = (LZ4F_CDict*) malloc(sizeof(*cdict));
|
|
|
|
if (!cdict) return NULL;
|
|
|
|
if (dictSize > 64 KB) {
|
|
|
|
dictStart += dictSize - 64 KB;
|
|
|
|
dictSize = 64 KB;
|
|
|
|
}
|
2018-01-26 22:29:50 +00:00
|
|
|
cdict->dictContent = ALLOC(dictSize);
|
2017-08-09 23:51:19 +00:00
|
|
|
cdict->fastCtx = LZ4_createStream();
|
|
|
|
cdict->HCCtx = LZ4_createStreamHC();
|
|
|
|
if (!cdict->dictContent || !cdict->fastCtx || !cdict->HCCtx) {
|
|
|
|
LZ4F_freeCDict(cdict);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
memcpy(cdict->dictContent, dictStart, dictSize);
|
|
|
|
LZ4_resetStream(cdict->fastCtx);
|
2017-08-10 01:22:26 +00:00
|
|
|
LZ4_loadDict (cdict->fastCtx, (const char*)cdict->dictContent, (int)dictSize);
|
2017-08-09 23:51:19 +00:00
|
|
|
LZ4_resetStreamHC(cdict->HCCtx, LZ4HC_CLEVEL_DEFAULT);
|
2017-08-10 01:22:26 +00:00
|
|
|
LZ4_loadDictHC(cdict->HCCtx, (const char*)cdict->dictContent, (int)dictSize);
|
2017-08-09 23:51:19 +00:00
|
|
|
return cdict;
|
|
|
|
}
|
|
|
|
|
|
|
|
void LZ4F_freeCDict(LZ4F_CDict* cdict)
|
|
|
|
{
|
|
|
|
if (cdict==NULL) return; /* support free on NULL */
|
|
|
|
FREEMEM(cdict->dictContent);
|
|
|
|
LZ4_freeStream(cdict->fastCtx);
|
|
|
|
LZ4_freeStreamHC(cdict->HCCtx);
|
|
|
|
FREEMEM(cdict);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-06-29 19:48:27 +00:00
|
|
|
/*-*********************************
|
2015-04-27 01:31:56 +00:00
|
|
|
* Advanced compression functions
|
|
|
|
***********************************/
|
2014-08-30 17:14:44 +00:00
|
|
|
|
2016-11-04 01:34:54 +00:00
|
|
|
/*! LZ4F_createCompressionContext() :
|
2017-08-09 23:51:19 +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 incompatible 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();
|
2016-11-04 01:34:54 +00:00
|
|
|
*/
|
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
|
|
|
{
|
2018-01-26 22:29:50 +00:00
|
|
|
LZ4F_cctx_t* const cctxPtr = (LZ4F_cctx_t*)ALLOC_AND_ZERO(sizeof(LZ4F_cctx_t));
|
2016-11-04 03:32:51 +00:00
|
|
|
if (cctxPtr==NULL) return err0r(LZ4F_ERROR_allocation_failed);
|
2014-08-30 17:14:44 +00:00
|
|
|
|
2014-09-22 17:42:00 +00:00
|
|
|
cctxPtr->version = version;
|
2017-08-09 23:51:19 +00:00
|
|
|
cctxPtr->cStage = 0; /* Next stage : init stream */
|
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)
|
|
|
|
{
|
2016-11-04 01:34:54 +00:00
|
|
|
LZ4F_cctx_t* const cctxPtr = (LZ4F_cctx_t*)LZ4F_compressionContext;
|
2014-08-30 17:14:44 +00:00
|
|
|
|
2017-08-09 23:51:19 +00:00
|
|
|
if (cctxPtr != NULL) { /* support free on NULL */
|
|
|
|
FREEMEM(cctxPtr->lz4CtxPtr); /* works because LZ4_streamHC_t and LZ4_stream_t are simple POD types */
|
2015-04-12 07:21:35 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-03-12 20:11:55 +00:00
|
|
|
static void LZ4F_applyCDict(void* ctx,
|
|
|
|
const LZ4F_CDict* cdict,
|
|
|
|
int level) {
|
|
|
|
if (level < LZ4HC_CLEVEL_MIN) {
|
|
|
|
LZ4_stream_t_internal* internal_ctx = &((LZ4_stream_t *)ctx)->internal_donotuse;
|
|
|
|
assert(!internal_ctx->initCheck);
|
2018-04-10 17:12:30 +00:00
|
|
|
LZ4_resetStream_fast((LZ4_stream_t *)ctx);
|
2018-04-11 20:04:24 +00:00
|
|
|
LZ4_attach_dictionary((LZ4_stream_t *)ctx, cdict ? cdict->fastCtx : NULL);
|
2018-03-12 20:11:55 +00:00
|
|
|
} else {
|
|
|
|
if (cdict) {
|
|
|
|
memcpy(ctx, cdict->HCCtx, sizeof(*cdict->HCCtx));
|
|
|
|
LZ4_setCompressionLevel((LZ4_streamHC_t*)ctx, level);
|
|
|
|
} else {
|
|
|
|
LZ4_resetStreamHC((LZ4_streamHC_t*)(ctx), level);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-08-09 23:51:19 +00:00
|
|
|
/*! LZ4F_compressBegin_usingCDict() :
|
|
|
|
* init streaming compression and writes frame header into dstBuffer.
|
|
|
|
* dstBuffer must be >= LZ4F_HEADER_SIZE_MAX bytes.
|
|
|
|
* @return : number of bytes written into dstBuffer for the header
|
|
|
|
* or an error code (can be tested using LZ4F_isError())
|
2016-11-04 01:34:54 +00:00
|
|
|
*/
|
2017-08-09 23:51:19 +00:00
|
|
|
size_t LZ4F_compressBegin_usingCDict(LZ4F_cctx* cctxPtr,
|
|
|
|
void* dstBuffer, size_t dstCapacity,
|
|
|
|
const LZ4F_CDict* cdict,
|
|
|
|
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
|
|
|
BYTE* const dstStart = (BYTE*)dstBuffer;
|
|
|
|
BYTE* dstPtr = dstStart;
|
|
|
|
BYTE* headerStart;
|
|
|
|
|
2016-11-11 23:11:56 +00:00
|
|
|
if (dstCapacity < maxFHSize) return err0r(LZ4F_ERROR_dstMaxSize_tooSmall);
|
2018-01-26 22:29:50 +00:00
|
|
|
MEM_INIT(&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;
|
|
|
|
|
2017-04-09 01:27:31 +00:00
|
|
|
/* Ctx Management */
|
2018-03-13 21:45:09 +00:00
|
|
|
{ U16 const ctxTypeID = (cctxPtr->prefs.compressionLevel < LZ4HC_CLEVEL_MIN) ? 1 : 2;
|
|
|
|
if (cctxPtr->lz4CtxAlloc < ctxTypeID) {
|
2014-10-27 23:39:06 +00:00
|
|
|
FREEMEM(cctxPtr->lz4CtxPtr);
|
2018-02-12 19:44:53 +00:00
|
|
|
if (cctxPtr->prefs.compressionLevel < LZ4HC_CLEVEL_MIN) {
|
2014-10-27 23:39:06 +00:00
|
|
|
cctxPtr->lz4CtxPtr = (void*)LZ4_createStream();
|
2018-02-12 19:44:53 +00:00
|
|
|
} else {
|
2014-10-27 23:39:06 +00:00
|
|
|
cctxPtr->lz4CtxPtr = (void*)LZ4_createStreamHC();
|
2018-02-12 19:44:53 +00:00
|
|
|
}
|
2017-04-09 01:27:31 +00:00
|
|
|
if (cctxPtr->lz4CtxPtr == NULL) return err0r(LZ4F_ERROR_allocation_failed);
|
2018-03-13 21:45:09 +00:00
|
|
|
cctxPtr->lz4CtxAlloc = ctxTypeID;
|
|
|
|
cctxPtr->lz4CtxState = ctxTypeID;
|
|
|
|
} else if (cctxPtr->lz4CtxState != ctxTypeID) {
|
2018-02-12 19:44:53 +00:00
|
|
|
/* otherwise, a sufficient buffer is allocated, but we need to
|
|
|
|
* reset it to the correct context type */
|
|
|
|
if (cctxPtr->prefs.compressionLevel < LZ4HC_CLEVEL_MIN) {
|
|
|
|
LZ4_resetStream((LZ4_stream_t *) cctxPtr->lz4CtxPtr);
|
|
|
|
} else {
|
|
|
|
LZ4_resetStreamHC((LZ4_streamHC_t *) cctxPtr->lz4CtxPtr, cctxPtr->prefs.compressionLevel);
|
|
|
|
}
|
2018-03-13 21:45:09 +00:00
|
|
|
cctxPtr->lz4CtxState = ctxTypeID;
|
2018-02-12 17:09:38 +00:00
|
|
|
}
|
|
|
|
}
|
2014-09-22 17:42:00 +00:00
|
|
|
|
|
|
|
/* Buffer Management */
|
2017-08-09 23:51:19 +00:00
|
|
|
if (cctxPtr->prefs.frameInfo.blockSizeID == 0)
|
|
|
|
cctxPtr->prefs.frameInfo.blockSizeID = LZ4F_BLOCKSIZEID_DEFAULT;
|
2014-09-22 17:42:00 +00:00
|
|
|
cctxPtr->maxBlockSize = LZ4F_getBlockSize(cctxPtr->prefs.frameInfo.blockSizeID);
|
|
|
|
|
2017-08-09 23:51:19 +00:00
|
|
|
{ size_t const requiredBuffSize = preferencesPtr->autoFlush ?
|
|
|
|
(cctxPtr->prefs.frameInfo.blockMode == LZ4F_blockLinked) * 64 KB : /* only needs windows size */
|
|
|
|
cctxPtr->maxBlockSize + ((cctxPtr->prefs.frameInfo.blockMode == LZ4F_blockLinked) * 128 KB);
|
|
|
|
|
|
|
|
if (cctxPtr->maxBufferSize < requiredBuffSize) {
|
|
|
|
cctxPtr->maxBufferSize = 0;
|
|
|
|
FREEMEM(cctxPtr->tmpBuff);
|
2018-03-13 21:47:34 +00:00
|
|
|
cctxPtr->tmpBuff = (BYTE*)ALLOC_AND_ZERO(requiredBuffSize);
|
2017-08-09 23:51:19 +00:00
|
|
|
if (cctxPtr->tmpBuff == NULL) return err0r(LZ4F_ERROR_allocation_failed);
|
|
|
|
cctxPtr->maxBufferSize = requiredBuffSize;
|
|
|
|
} }
|
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);
|
2017-08-09 23:51:19 +00:00
|
|
|
|
|
|
|
/* context init */
|
2017-08-10 19:12:53 +00:00
|
|
|
cctxPtr->cdict = cdict;
|
|
|
|
if (cctxPtr->prefs.frameInfo.blockMode == LZ4F_blockLinked) {
|
|
|
|
/* frame init only for blockLinked : blockIndependent will be init at each block */
|
2018-03-12 20:11:55 +00:00
|
|
|
LZ4F_applyCDict(cctxPtr->lz4CtxPtr, cdict, cctxPtr->prefs.compressionLevel);
|
2017-08-09 23:51:19 +00:00
|
|
|
}
|
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') */
|
2017-08-12 00:46:52 +00:00
|
|
|
+ ((cctxPtr->prefs.frameInfo.blockMode & _1BIT ) << 5)
|
|
|
|
+ ((cctxPtr->prefs.frameInfo.blockChecksumFlag & _1BIT ) << 4)
|
|
|
|
+ ((cctxPtr->prefs.frameInfo.contentSize > 0) << 3)
|
|
|
|
+ ((cctxPtr->prefs.frameInfo.contentChecksumFlag & _1BIT ) << 2)
|
|
|
|
+ (cctxPtr->prefs.frameInfo.dictID > 0) );
|
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 */
|
2016-06-29 19:48:27 +00:00
|
|
|
if (cctxPtr->prefs.frameInfo.contentSize) {
|
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;
|
|
|
|
}
|
2017-08-09 00:43:11 +00:00
|
|
|
/* Optional dictionary ID field */
|
|
|
|
if (cctxPtr->prefs.frameInfo.dictID) {
|
|
|
|
LZ4F_writeLE32(dstPtr, cctxPtr->prefs.frameInfo.dictID);
|
|
|
|
dstPtr += 4;
|
|
|
|
}
|
2017-08-09 23:51:19 +00:00
|
|
|
/* Header 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-09-22 17:42:00 +00:00
|
|
|
return (dstPtr - dstStart);
|
2014-08-30 17:14:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-08-09 23:51:19 +00:00
|
|
|
/*! LZ4F_compressBegin() :
|
|
|
|
* init streaming compression and writes frame header into dstBuffer.
|
|
|
|
* dstBuffer must be >= LZ4F_HEADER_SIZE_MAX bytes.
|
|
|
|
* preferencesPtr can be NULL, in which case default parameters are selected.
|
|
|
|
* @return : number of bytes written into dstBuffer for the header
|
|
|
|
* or an error code (can be tested using LZ4F_isError())
|
|
|
|
*/
|
|
|
|
size_t LZ4F_compressBegin(LZ4F_cctx* cctxPtr,
|
|
|
|
void* dstBuffer, size_t dstCapacity,
|
|
|
|
const LZ4F_preferences_t* preferencesPtr)
|
|
|
|
{
|
|
|
|
return LZ4F_compressBegin_usingCDict(cctxPtr, dstBuffer, dstCapacity,
|
|
|
|
NULL, preferencesPtr);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-01-14 06:39:39 +00:00
|
|
|
/* LZ4F_compressBound() :
|
|
|
|
* @return minimum capacity of dstBuffer for a given srcSize to handle worst case scenario.
|
|
|
|
* LZ4F_preferences_t structure is optional : if NULL, preferences will be set to cover worst case scenario.
|
|
|
|
* This function cannot fail.
|
2016-11-11 23:11:56 +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
|
|
|
{
|
2016-11-11 23:11:56 +00:00
|
|
|
return LZ4F_compressBound_internal(srcSize, preferencesPtr, (size_t)-1);
|
2014-08-30 17:14:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-08-10 19:12:53 +00:00
|
|
|
typedef int (*compressFunc_t)(void* ctx, const char* src, char* dst, int srcSize, int dstSize, int level, const LZ4F_CDict* cdict);
|
2014-09-14 23:59:30 +00:00
|
|
|
|
2017-08-12 00:46:52 +00:00
|
|
|
|
|
|
|
/*! LZ4F_makeBlock():
|
|
|
|
* compress a single block, add header and checksum
|
|
|
|
* assumption : dst buffer capacity is >= srcSize */
|
|
|
|
static size_t LZ4F_makeBlock(void* dst, const void* src, size_t srcSize,
|
|
|
|
compressFunc_t compress, void* lz4ctx, int level,
|
|
|
|
const LZ4F_CDict* cdict, LZ4F_blockChecksum_t crcFlag)
|
2014-09-14 23:59:30 +00:00
|
|
|
{
|
2016-11-04 01:34:54 +00:00
|
|
|
BYTE* const cSizePtr = (BYTE*)dst;
|
2017-08-10 19:12:53 +00:00
|
|
|
U32 cSize = (U32)compress(lz4ctx, (const char*)src, (char*)(cSizePtr+4),
|
|
|
|
(int)(srcSize), (int)(srcSize-1),
|
|
|
|
level, cdict);
|
2014-09-14 23:59:30 +00:00
|
|
|
LZ4F_writeLE32(cSizePtr, cSize);
|
2016-06-29 19:48:27 +00:00
|
|
|
if (cSize == 0) { /* compression failed */
|
2014-09-22 16:38:17 +00:00
|
|
|
cSize = (U32)srcSize;
|
2016-11-11 23:40:58 +00:00
|
|
|
LZ4F_writeLE32(cSizePtr, cSize | LZ4F_BLOCKUNCOMPRESSED_FLAG);
|
2014-09-14 23:59:30 +00:00
|
|
|
memcpy(cSizePtr+4, src, srcSize);
|
|
|
|
}
|
2017-08-12 00:46:52 +00:00
|
|
|
if (crcFlag) {
|
|
|
|
U32 const crc32 = XXH32(cSizePtr+4, cSize, 0); /* checksum of compressed data */
|
|
|
|
LZ4F_writeLE32(cSizePtr+4+cSize, crc32);
|
|
|
|
}
|
|
|
|
return 4 + cSize + ((U32)crcFlag)*4;
|
2014-09-14 23:59:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-08-10 19:12:53 +00:00
|
|
|
static int LZ4F_compressBlock(void* ctx, const char* src, char* dst, int srcSize, int dstCapacity, int level, const LZ4F_CDict* cdict)
|
2014-10-27 23:39:06 +00:00
|
|
|
{
|
2017-04-09 08:41:36 +00:00
|
|
|
int const acceleration = (level < -1) ? -level : 1;
|
2018-03-12 20:11:55 +00:00
|
|
|
LZ4F_applyCDict(ctx, cdict, level);
|
2017-08-10 19:12:53 +00:00
|
|
|
if (cdict) {
|
|
|
|
return LZ4_compress_fast_continue((LZ4_stream_t*)ctx, src, dst, srcSize, dstCapacity, acceleration);
|
2018-02-12 17:19:13 +00:00
|
|
|
} else {
|
2018-04-11 19:13:01 +00:00
|
|
|
return LZ4_compress_fast_extState_fastReset(ctx, src, dst, srcSize, dstCapacity, acceleration);
|
2017-08-10 19:12:53 +00:00
|
|
|
}
|
2014-10-27 23:39:06 +00:00
|
|
|
}
|
|
|
|
|
2017-08-10 19:12:53 +00:00
|
|
|
static int LZ4F_compressBlock_continue(void* ctx, const char* src, char* dst, int srcSize, int dstCapacity, int level, const LZ4F_CDict* cdict)
|
2014-10-27 23:39:06 +00:00
|
|
|
{
|
2017-04-09 08:41:36 +00:00
|
|
|
int const acceleration = (level < -1) ? -level : 1;
|
2017-08-10 19:12:53 +00:00
|
|
|
(void)cdict; /* init once at beginning of frame */
|
2017-04-09 08:41:36 +00:00
|
|
|
return LZ4_compress_fast_continue((LZ4_stream_t*)ctx, src, dst, srcSize, dstCapacity, acceleration);
|
2014-10-27 23:39:06 +00:00
|
|
|
}
|
|
|
|
|
2017-08-10 19:12:53 +00:00
|
|
|
static int LZ4F_compressBlockHC(void* ctx, const char* src, char* dst, int srcSize, int dstCapacity, int level, const LZ4F_CDict* cdict)
|
|
|
|
{
|
|
|
|
if (cdict) {
|
2018-03-12 20:11:55 +00:00
|
|
|
LZ4F_applyCDict(ctx, cdict, level);
|
2017-08-10 19:12:53 +00:00
|
|
|
return LZ4_compress_HC_continue((LZ4_streamHC_t*)ctx, src, dst, srcSize, dstCapacity);
|
|
|
|
}
|
|
|
|
return LZ4_compress_HC_extStateHC(ctx, src, dst, srcSize, dstCapacity, level);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int LZ4F_compressBlockHC_continue(void* ctx, const char* src, char* dst, int srcSize, int dstCapacity, int level, const LZ4F_CDict* cdict)
|
2014-10-27 23:39:06 +00:00
|
|
|
{
|
2017-08-10 19:12:53 +00:00
|
|
|
(void)level; (void)cdict; /* init once at beginning of frame */
|
2017-08-09 23:51:19 +00:00
|
|
|
return LZ4_compress_HC_continue((LZ4_streamHC_t*)ctx, src, dst, srcSize, dstCapacity);
|
2014-10-27 23:39:06 +00:00
|
|
|
}
|
|
|
|
|
2017-08-10 19:12:53 +00:00
|
|
|
static compressFunc_t LZ4F_selectCompression(LZ4F_blockMode_t blockMode, int level)
|
2014-10-27 23:39:06 +00:00
|
|
|
{
|
2016-12-22 10:41:05 +00:00
|
|
|
if (level < LZ4HC_CLEVEL_MIN) {
|
2017-08-10 19:12:53 +00:00
|
|
|
if (blockMode == LZ4F_blockIndependent) return LZ4F_compressBlock;
|
|
|
|
return LZ4F_compressBlock_continue;
|
2014-10-27 23:39:06 +00:00
|
|
|
}
|
2017-08-10 19:12:53 +00:00
|
|
|
if (blockMode == LZ4F_blockIndependent) return LZ4F_compressBlockHC;
|
|
|
|
return LZ4F_compressBlockHC_continue;
|
2014-10-27 23:39:06 +00:00
|
|
|
}
|
|
|
|
|
2015-04-27 01:31:56 +00:00
|
|
|
static int LZ4F_localSaveDict(LZ4F_cctx_t* cctxPtr)
|
2014-10-27 23:39:06 +00:00
|
|
|
{
|
2016-12-22 10:41:05 +00:00
|
|
|
if (cctxPtr->prefs.compressionLevel < LZ4HC_CLEVEL_MIN)
|
2014-10-27 23:39:06 +00:00
|
|
|
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;
|
|
|
|
|
2016-06-29 19:48:27 +00:00
|
|
|
/*! LZ4F_compressUpdate() :
|
2017-08-10 07:48:19 +00:00
|
|
|
* LZ4F_compressUpdate() can be called repetitively to compress as much data as necessary.
|
|
|
|
* dstBuffer MUST be >= LZ4F_compressBound(srcSize, preferencesPtr).
|
|
|
|
* LZ4F_compressOptions_t structure is optional : you can provide NULL as argument.
|
|
|
|
* @return : the number of bytes written into dstBuffer. It can be zero, meaning input data was just buffered.
|
|
|
|
* or an error code if it fails (which can be tested using LZ4F_isError())
|
|
|
|
*/
|
2017-08-12 00:46:52 +00:00
|
|
|
size_t LZ4F_compressUpdate(LZ4F_cctx* cctxPtr,
|
|
|
|
void* dstBuffer, size_t dstCapacity,
|
|
|
|
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;
|
2016-11-04 01:34:54 +00:00
|
|
|
size_t const blockSize = cctxPtr->maxBlockSize;
|
2014-09-22 17:42:00 +00:00
|
|
|
const BYTE* srcPtr = (const BYTE*)srcBuffer;
|
|
|
|
const BYTE* const srcEnd = srcPtr + srcSize;
|
|
|
|
BYTE* const dstStart = (BYTE*)dstBuffer;
|
|
|
|
BYTE* dstPtr = dstStart;
|
|
|
|
LZ4F_lastBlockStatus lastBlockCompressed = notDone;
|
2017-08-10 19:12:53 +00:00
|
|
|
compressFunc_t const compress = LZ4F_selectCompression(cctxPtr->prefs.frameInfo.blockMode, cctxPtr->prefs.compressionLevel);
|
2014-09-22 17:42:00 +00:00
|
|
|
|
|
|
|
|
2016-11-04 03:32:51 +00:00
|
|
|
if (cctxPtr->cStage != 1) return err0r(LZ4F_ERROR_GENERIC);
|
2018-01-31 22:33:16 +00:00
|
|
|
if (dstCapacity < LZ4F_compressBound_internal(srcSize, &(cctxPtr->prefs), cctxPtr->tmpInSize))
|
|
|
|
return err0r(LZ4F_ERROR_dstMaxSize_tooSmall);
|
2018-01-26 22:29:50 +00:00
|
|
|
MEM_INIT(&cOptionsNull, 0, sizeof(cOptionsNull));
|
2014-09-22 17:42:00 +00:00
|
|
|
if (compressOptionsPtr == NULL) compressOptionsPtr = &cOptionsNull;
|
|
|
|
|
|
|
|
/* complete tmp buffer */
|
2016-06-29 19:48:27 +00:00
|
|
|
if (cctxPtr->tmpInSize > 0) { /* some data already within tmp buffer */
|
2016-11-04 01:34:54 +00:00
|
|
|
size_t const sizeToCopy = blockSize - cctxPtr->tmpInSize;
|
2016-06-29 19:48:27 +00:00
|
|
|
if (sizeToCopy > srcSize) {
|
2014-09-22 17:42:00 +00:00
|
|
|
/* add src to tmpIn buffer */
|
|
|
|
memcpy(cctxPtr->tmpIn + cctxPtr->tmpInSize, srcBuffer, srcSize);
|
|
|
|
srcPtr = srcEnd;
|
|
|
|
cctxPtr->tmpInSize += srcSize;
|
|
|
|
/* still needs some CRC */
|
2016-06-29 19:48:27 +00:00
|
|
|
} else {
|
2014-09-22 17:42:00 +00:00
|
|
|
/* complete tmpIn block and then compress it */
|
|
|
|
lastBlockCompressed = fromTmpBuffer;
|
|
|
|
memcpy(cctxPtr->tmpIn + cctxPtr->tmpInSize, srcBuffer, sizeToCopy);
|
|
|
|
srcPtr += sizeToCopy;
|
|
|
|
|
2017-08-12 00:46:52 +00:00
|
|
|
dstPtr += LZ4F_makeBlock(dstPtr, cctxPtr->tmpIn, blockSize,
|
|
|
|
compress, cctxPtr->lz4CtxPtr, cctxPtr->prefs.compressionLevel,
|
|
|
|
cctxPtr->cdict, cctxPtr->prefs.frameInfo.blockChecksumFlag);
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-06-29 19:48:27 +00:00
|
|
|
while ((size_t)(srcEnd - srcPtr) >= blockSize) {
|
2017-08-10 19:12:53 +00:00
|
|
|
/* compress full blocks */
|
2014-09-14 23:59:30 +00:00
|
|
|
lastBlockCompressed = fromSrcBuffer;
|
2017-08-12 00:46:52 +00:00
|
|
|
dstPtr += LZ4F_makeBlock(dstPtr, srcPtr, blockSize,
|
|
|
|
compress, cctxPtr->lz4CtxPtr, cctxPtr->prefs.compressionLevel,
|
|
|
|
cctxPtr->cdict, cctxPtr->prefs.frameInfo.blockChecksumFlag);
|
2014-09-22 17:42:00 +00:00
|
|
|
srcPtr += blockSize;
|
|
|
|
}
|
2014-08-30 17:14:44 +00:00
|
|
|
|
2016-06-29 19:48:27 +00:00
|
|
|
if ((cctxPtr->prefs.autoFlush) && (srcPtr < srcEnd)) {
|
2014-09-14 23:59:30 +00:00
|
|
|
/* compress remaining input < blockSize */
|
|
|
|
lastBlockCompressed = fromSrcBuffer;
|
2017-08-12 00:46:52 +00:00
|
|
|
dstPtr += LZ4F_makeBlock(dstPtr, srcPtr, srcEnd - srcPtr,
|
|
|
|
compress, cctxPtr->lz4CtxPtr, cctxPtr->prefs.compressionLevel,
|
|
|
|
cctxPtr->cdict, cctxPtr->prefs.frameInfo.blockChecksumFlag);
|
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 */
|
2016-06-29 19:48:27 +00:00
|
|
|
if ((cctxPtr->prefs.frameInfo.blockMode==LZ4F_blockLinked) && (lastBlockCompressed==fromSrcBuffer)) {
|
|
|
|
if (compressOptionsPtr->stableSrc) {
|
2014-09-14 23:59:30 +00:00
|
|
|
cctxPtr->tmpIn = cctxPtr->tmpBuff;
|
2016-06-29 19:48:27 +00:00
|
|
|
} else {
|
2017-04-09 08:41:36 +00:00
|
|
|
int const realDictSize = LZ4F_localSaveDict(cctxPtr);
|
2016-11-04 03:32:51 +00:00
|
|
|
if (realDictSize==0) return err0r(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
|
|
|
{
|
2017-04-09 08:41:36 +00:00
|
|
|
int const realDictSize = LZ4F_localSaveDict(cctxPtr);
|
2015-03-29 12:28:32 +00:00
|
|
|
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 */
|
2016-06-29 19:48:27 +00:00
|
|
|
if (srcPtr < srcEnd) {
|
2014-09-12 18:28:44 +00:00
|
|
|
/* fill tmp buffer */
|
2016-11-04 01:34:54 +00:00
|
|
|
size_t const sizeToCopy = srcEnd - srcPtr;
|
2014-09-12 18:28:44 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-06-29 19:48:27 +00:00
|
|
|
/*! LZ4F_flush() :
|
2017-05-10 19:25:05 +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.
|
|
|
|
*/
|
2016-11-11 23:11:56 +00:00
|
|
|
size_t LZ4F_flush(LZ4F_cctx* cctxPtr, void* dstBuffer, size_t dstCapacity, const LZ4F_compressOptions_t* compressOptionsPtr)
|
2014-08-30 17:14:44 +00:00
|
|
|
{
|
2014-09-22 17:42:00 +00:00
|
|
|
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 */
|
2016-11-04 03:32:51 +00:00
|
|
|
if (cctxPtr->cStage != 1) return err0r(LZ4F_ERROR_GENERIC);
|
2016-11-11 23:11:56 +00:00
|
|
|
if (dstCapacity < (cctxPtr->tmpInSize + 4)) return err0r(LZ4F_ERROR_dstMaxSize_tooSmall); /* +4 : block header(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 */
|
2017-08-10 19:12:53 +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 */
|
2017-08-12 00:46:52 +00:00
|
|
|
dstPtr += LZ4F_makeBlock(dstPtr, cctxPtr->tmpIn, cctxPtr->tmpInSize,
|
|
|
|
compress, cctxPtr->lz4CtxPtr, cctxPtr->prefs.compressionLevel,
|
|
|
|
cctxPtr->cdict, cctxPtr->prefs.frameInfo.blockChecksumFlag);
|
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 */
|
2016-06-29 19:48:27 +00:00
|
|
|
if ((cctxPtr->tmpIn + cctxPtr->maxBlockSize) > (cctxPtr->tmpBuff + cctxPtr->maxBufferSize)) { /* necessarily LZ4F_blockLinked */
|
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
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-06-29 19:48:27 +00:00
|
|
|
/*! LZ4F_compressEnd() :
|
2017-05-10 19:25:05 +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.
|
|
|
|
*/
|
2016-11-04 01:34:54 +00:00
|
|
|
size_t LZ4F_compressEnd(LZ4F_cctx* cctxPtr, void* dstBuffer, size_t dstMaxSize, const LZ4F_compressOptions_t* compressOptionsPtr)
|
2014-08-30 17:14:44 +00:00
|
|
|
{
|
2014-09-22 17:42:00 +00:00
|
|
|
BYTE* const dstStart = (BYTE*)dstBuffer;
|
|
|
|
BYTE* dstPtr = dstStart;
|
2014-08-30 17:14:44 +00:00
|
|
|
|
2016-11-04 01:34:54 +00:00
|
|
|
size_t const flushSize = LZ4F_flush(cctxPtr, dstBuffer, dstMaxSize, compressOptionsPtr);
|
|
|
|
if (LZ4F_isError(flushSize)) return flushSize;
|
|
|
|
dstPtr += flushSize;
|
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
|
|
|
|
2016-06-29 19:48:27 +00:00
|
|
|
if (cctxPtr->prefs.frameInfo.contentChecksumFlag == LZ4F_contentChecksumEnabled) {
|
2016-11-04 01:34:54 +00:00
|
|
|
U32 const 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) */
|
2016-09-06 08:36:16 +00:00
|
|
|
cctxPtr->maxBufferSize = 0; /* reuse HC context */
|
2014-08-30 17:14:44 +00:00
|
|
|
|
2016-06-29 19:48:27 +00:00
|
|
|
if (cctxPtr->prefs.frameInfo.contentSize) {
|
2015-03-29 10:20:09 +00:00
|
|
|
if (cctxPtr->prefs.frameInfo.contentSize != cctxPtr->totalInSize)
|
2016-11-04 03:32:51 +00:00
|
|
|
return err0r(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
|
|
|
|
|
|
|
|
2016-08-11 12:44:53 +00:00
|
|
|
/*-***************************************************
|
|
|
|
* Frame Decompression
|
|
|
|
*****************************************************/
|
2014-09-01 21:44:02 +00:00
|
|
|
|
2017-08-12 00:46:52 +00:00
|
|
|
typedef enum {
|
|
|
|
dstage_getFrameHeader=0, dstage_storeFrameHeader,
|
|
|
|
dstage_init,
|
|
|
|
dstage_getBlockHeader, dstage_storeBlockHeader,
|
|
|
|
dstage_copyDirect, dstage_getBlockChecksum,
|
|
|
|
dstage_getCBlock, dstage_storeCBlock,
|
2018-01-14 08:15:07 +00:00
|
|
|
dstage_flushOut,
|
2017-08-12 00:46:52 +00:00
|
|
|
dstage_getSuffix, dstage_storeSuffix,
|
|
|
|
dstage_getSFrameSize, dstage_storeSFrameSize,
|
|
|
|
dstage_skipSkippable
|
|
|
|
} dStage_t;
|
|
|
|
|
2016-11-04 01:34:54 +00:00
|
|
|
struct LZ4F_dctx_s {
|
|
|
|
LZ4F_frameInfo_t frameInfo;
|
|
|
|
U32 version;
|
2017-08-12 00:46:52 +00:00
|
|
|
dStage_t dStage;
|
2016-11-04 01:34:54 +00:00
|
|
|
U64 frameRemainingSize;
|
|
|
|
size_t maxBlockSize;
|
|
|
|
size_t maxBufferSize;
|
|
|
|
BYTE* tmpIn;
|
|
|
|
size_t tmpInSize;
|
|
|
|
size_t tmpInTarget;
|
|
|
|
BYTE* tmpOutBuffer;
|
2017-08-12 00:46:52 +00:00
|
|
|
const BYTE* dict;
|
2016-11-04 01:34:54 +00:00
|
|
|
size_t dictSize;
|
|
|
|
BYTE* tmpOut;
|
|
|
|
size_t tmpOutSize;
|
|
|
|
size_t tmpOutStart;
|
|
|
|
XXH32_state_t xxh;
|
2017-08-12 00:46:52 +00:00
|
|
|
XXH32_state_t blockChecksum;
|
2017-08-17 17:55:30 +00:00
|
|
|
BYTE header[LZ4F_HEADER_SIZE_MAX];
|
2016-11-04 01:34:54 +00:00
|
|
|
}; /* typedef'd to LZ4F_dctx in lz4frame.h */
|
|
|
|
|
2014-09-01 21:44:02 +00:00
|
|
|
|
2016-08-11 12:44:53 +00:00
|
|
|
/*! LZ4F_createDecompressionContext() :
|
2017-08-12 00:46:52 +00:00
|
|
|
* Create a decompressionContext object, which will track all decompression operations.
|
|
|
|
* Provides a pointer to a fully allocated and initialized LZ4F_decompressionContext object.
|
|
|
|
* Object can later be released using LZ4F_freeDecompressionContext().
|
|
|
|
* @return : if != 0, there was an error during context creation.
|
2017-05-10 19:25:05 +00:00
|
|
|
*/
|
2016-11-04 01:34:54 +00:00
|
|
|
LZ4F_errorCode_t LZ4F_createDecompressionContext(LZ4F_dctx** LZ4F_decompressionContextPtr, unsigned versionNumber)
|
2014-09-03 18:49:59 +00:00
|
|
|
{
|
2018-01-26 22:29:50 +00:00
|
|
|
LZ4F_dctx* const dctx = (LZ4F_dctx*)ALLOC_AND_ZERO(sizeof(LZ4F_dctx));
|
2017-08-12 00:46:52 +00:00
|
|
|
if (dctx==NULL) return err0r(LZ4F_ERROR_GENERIC);
|
2014-09-03 18:49:59 +00:00
|
|
|
|
2017-08-12 00:46:52 +00:00
|
|
|
dctx->version = versionNumber;
|
|
|
|
*LZ4F_decompressionContextPtr = dctx;
|
2015-04-14 17:20:47 +00:00
|
|
|
return LZ4F_OK_NoError;
|
2014-09-03 18:49:59 +00:00
|
|
|
}
|
|
|
|
|
2017-08-12 00:46:52 +00:00
|
|
|
LZ4F_errorCode_t LZ4F_freeDecompressionContext(LZ4F_dctx* dctx)
|
2014-09-03 18:49:59 +00:00
|
|
|
{
|
2015-04-20 11:23:33 +00:00
|
|
|
LZ4F_errorCode_t result = LZ4F_OK_NoError;
|
2017-08-12 00:46:52 +00:00
|
|
|
if (dctx != NULL) { /* can accept NULL input, like free() */
|
|
|
|
result = (LZ4F_errorCode_t)dctx->dStage;
|
|
|
|
FREEMEM(dctx->tmpIn);
|
|
|
|
FREEMEM(dctx->tmpOutBuffer);
|
|
|
|
FREEMEM(dctx);
|
2015-04-12 07:21:35 +00:00
|
|
|
}
|
2015-04-20 11:23:33 +00:00
|
|
|
return result;
|
2014-09-03 18:49:59 +00:00
|
|
|
}
|
|
|
|
|
2014-09-01 21:44:02 +00:00
|
|
|
|
2016-11-04 01:34:54 +00:00
|
|
|
/*==--- Streaming Decompression operations ---==*/
|
2015-03-16 16:52:14 +00:00
|
|
|
|
2017-05-10 23:28:36 +00:00
|
|
|
void LZ4F_resetDecompressionContext(LZ4F_dctx* dctx)
|
2017-03-29 19:51:08 +00:00
|
|
|
{
|
2017-08-12 00:46:52 +00:00
|
|
|
dctx->dStage = dstage_getFrameHeader;
|
2017-08-10 23:53:57 +00:00
|
|
|
dctx->dict = NULL;
|
|
|
|
dctx->dictSize = 0;
|
2017-03-29 19:51:08 +00:00
|
|
|
}
|
|
|
|
|
2015-03-16 16:52:14 +00:00
|
|
|
|
2016-08-11 12:44:53 +00:00
|
|
|
/*! LZ4F_headerSize() :
|
2017-05-10 19:25:05 +00:00
|
|
|
* @return : size of frame header
|
|
|
|
* or an error code, which can be tested using LZ4F_isError()
|
|
|
|
*/
|
2016-08-11 12:44:53 +00:00
|
|
|
static size_t LZ4F_headerSize(const void* src, size_t srcSize)
|
|
|
|
{
|
|
|
|
/* minimal srcSize to determine header size */
|
2016-11-04 03:32:51 +00:00
|
|
|
if (srcSize < 5) return err0r(LZ4F_ERROR_frameHeader_incomplete);
|
2016-08-11 12:44:53 +00:00
|
|
|
|
|
|
|
/* special case : skippable frames */
|
|
|
|
if ((LZ4F_readLE32(src) & 0xFFFFFFF0U) == LZ4F_MAGIC_SKIPPABLE_START) return 8;
|
|
|
|
|
|
|
|
/* control magic number */
|
2017-08-09 00:43:11 +00:00
|
|
|
if (LZ4F_readLE32(src) != LZ4F_MAGICNUMBER)
|
|
|
|
return err0r(LZ4F_ERROR_frameType_unknown);
|
2016-08-11 12:44:53 +00:00
|
|
|
|
|
|
|
/* Frame Header Size */
|
|
|
|
{ BYTE const FLG = ((const BYTE*)src)[4];
|
|
|
|
U32 const contentSizeFlag = (FLG>>3) & _1BIT;
|
2017-08-09 00:43:11 +00:00
|
|
|
U32 const dictIDFlag = FLG & _1BIT;
|
|
|
|
return minFHSize + (contentSizeFlag*8) + (dictIDFlag*4);
|
2016-08-11 12:44:53 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-06-29 19:48:27 +00:00
|
|
|
/*! LZ4F_decodeHeader() :
|
2017-05-10 19:25:05 +00:00
|
|
|
* input : `src` points at the **beginning of the frame**
|
|
|
|
* output : set internal values of dctx, such as
|
2017-08-12 00:46:52 +00:00
|
|
|
* dctx->frameInfo and dctx->dStage.
|
2017-05-10 19:25:05 +00:00
|
|
|
* Also allocates internal buffers.
|
|
|
|
* @return : nb Bytes read from src (necessarily <= srcSize)
|
|
|
|
* or an error code (testable with LZ4F_isError())
|
|
|
|
*/
|
2017-08-12 00:46:52 +00:00
|
|
|
static size_t LZ4F_decodeHeader(LZ4F_dctx* dctx, const void* src, size_t srcSize)
|
2014-09-01 21:44:02 +00:00
|
|
|
{
|
2017-08-12 00:46:52 +00:00
|
|
|
unsigned blockMode, blockChecksumFlag, contentSizeFlag, contentChecksumFlag, dictIDFlag, blockSizeID;
|
2015-03-18 20:38:27 +00:00
|
|
|
size_t frameHeaderSize;
|
2016-11-04 01:34:54 +00:00
|
|
|
const BYTE* srcPtr = (const BYTE*)src;
|
2014-09-01 21:44:02 +00:00
|
|
|
|
|
|
|
/* need to decode header to get frameInfo */
|
2016-11-04 03:32:51 +00:00
|
|
|
if (srcSize < minFHSize) return err0r(LZ4F_ERROR_frameHeader_incomplete); /* minimal frame header size */
|
2018-01-26 22:29:50 +00:00
|
|
|
MEM_INIT(&(dctx->frameInfo), 0, sizeof(dctx->frameInfo));
|
2014-09-01 21:44:02 +00:00
|
|
|
|
2015-04-19 14:23:53 +00:00
|
|
|
/* special case : skippable frames */
|
2016-06-29 19:48:27 +00:00
|
|
|
if ((LZ4F_readLE32(srcPtr) & 0xFFFFFFF0U) == LZ4F_MAGIC_SKIPPABLE_START) {
|
2017-08-12 00:46:52 +00:00
|
|
|
dctx->frameInfo.frameType = LZ4F_skippableFrame;
|
|
|
|
if (src == (void*)(dctx->header)) {
|
|
|
|
dctx->tmpInSize = srcSize;
|
|
|
|
dctx->tmpInTarget = 8;
|
|
|
|
dctx->dStage = dstage_storeSFrameSize;
|
2015-03-16 18:19:39 +00:00
|
|
|
return srcSize;
|
2016-06-29 19:48:27 +00:00
|
|
|
} else {
|
2017-08-12 00:46:52 +00:00
|
|
|
dctx->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 */
|
2017-08-09 00:43:11 +00:00
|
|
|
if (LZ4F_readLE32(srcPtr) != LZ4F_MAGICNUMBER)
|
|
|
|
return err0r(LZ4F_ERROR_frameType_unknown);
|
2017-08-12 00:46:52 +00:00
|
|
|
dctx->frameInfo.frameType = LZ4F_frame;
|
2014-09-01 21:44:02 +00:00
|
|
|
|
|
|
|
/* Flags */
|
2017-03-17 22:11:09 +00:00
|
|
|
{ U32 const FLG = srcPtr[4];
|
|
|
|
U32 const version = (FLG>>6) & _2BITS;
|
2017-08-12 00:46:52 +00:00
|
|
|
blockChecksumFlag = (FLG>>4) & _1BIT;
|
2017-03-17 22:11:09 +00:00
|
|
|
blockMode = (FLG>>5) & _1BIT;
|
|
|
|
contentSizeFlag = (FLG>>3) & _1BIT;
|
|
|
|
contentChecksumFlag = (FLG>>2) & _1BIT;
|
2017-08-09 00:43:11 +00:00
|
|
|
dictIDFlag = FLG & _1BIT;
|
2017-03-17 22:11:09 +00:00
|
|
|
/* validate */
|
2017-08-12 00:46:52 +00:00
|
|
|
if (((FLG>>1)&_1BIT) != 0) return err0r(LZ4F_ERROR_reservedFlag_set); /* Reserved bit */
|
2017-03-17 22:11:09 +00:00
|
|
|
if (version != 1) return err0r(LZ4F_ERROR_headerVersion_wrong); /* Version Number, only supported value */
|
|
|
|
}
|
2014-09-01 21:44:02 +00:00
|
|
|
|
2015-03-18 20:38:27 +00:00
|
|
|
/* Frame Header Size */
|
2017-08-09 00:43:11 +00:00
|
|
|
frameHeaderSize = minFHSize + (contentSizeFlag*8) + (dictIDFlag*4);
|
2015-03-18 20:38:27 +00:00
|
|
|
|
2016-06-29 19:48:27 +00:00
|
|
|
if (srcSize < frameHeaderSize) {
|
2015-04-19 14:23:53 +00:00
|
|
|
/* not enough input to fully decode frame header */
|
2017-08-12 00:46:52 +00:00
|
|
|
if (srcPtr != dctx->header)
|
|
|
|
memcpy(dctx->header, srcPtr, srcSize);
|
|
|
|
dctx->tmpInSize = srcSize;
|
|
|
|
dctx->tmpInTarget = frameHeaderSize;
|
|
|
|
dctx->dStage = dstage_storeFrameHeader;
|
2015-03-18 20:38:27 +00:00
|
|
|
return srcSize;
|
|
|
|
}
|
|
|
|
|
2017-03-17 22:11:09 +00:00
|
|
|
{ U32 const BD = srcPtr[5];
|
|
|
|
blockSizeID = (BD>>4) & _3BITS;
|
|
|
|
/* validate */
|
|
|
|
if (((BD>>7)&_1BIT) != 0) return err0r(LZ4F_ERROR_reservedFlag_set); /* Reserved bit */
|
|
|
|
if (blockSizeID < 4) return err0r(LZ4F_ERROR_maxBlockSize_invalid); /* 4-7 only supported values for the time being */
|
|
|
|
if (((BD>>0)&_4BITS) != 0) return err0r(LZ4F_ERROR_reservedFlag_set); /* Reserved bits */
|
|
|
|
}
|
2014-09-01 21:44:02 +00:00
|
|
|
|
2016-11-15 22:01:37 +00:00
|
|
|
/* check header */
|
2017-08-09 00:43:11 +00:00
|
|
|
{ BYTE const HC = LZ4F_headerChecksum(srcPtr+4, frameHeaderSize-5);
|
|
|
|
if (HC != srcPtr[frameHeaderSize-1])
|
|
|
|
return err0r(LZ4F_ERROR_headerChecksum_invalid);
|
|
|
|
}
|
2015-03-18 20:38:27 +00:00
|
|
|
|
2014-09-01 21:44:02 +00:00
|
|
|
/* save */
|
2017-08-12 00:46:52 +00:00
|
|
|
dctx->frameInfo.blockMode = (LZ4F_blockMode_t)blockMode;
|
|
|
|
dctx->frameInfo.blockChecksumFlag = (LZ4F_blockChecksum_t)blockChecksumFlag;
|
|
|
|
dctx->frameInfo.contentChecksumFlag = (LZ4F_contentChecksum_t)contentChecksumFlag;
|
|
|
|
dctx->frameInfo.blockSizeID = (LZ4F_blockSizeID_t)blockSizeID;
|
|
|
|
dctx->maxBlockSize = LZ4F_getBlockSize(blockSizeID);
|
2015-03-18 20:38:27 +00:00
|
|
|
if (contentSizeFlag)
|
2017-08-12 00:46:52 +00:00
|
|
|
dctx->frameRemainingSize =
|
|
|
|
dctx->frameInfo.contentSize = LZ4F_readLE64(srcPtr+6);
|
2017-08-09 00:43:11 +00:00
|
|
|
if (dictIDFlag)
|
2017-08-12 00:46:52 +00:00
|
|
|
dctx->frameInfo.dictID = LZ4F_readLE32(srcPtr + frameHeaderSize - 5);
|
2014-09-01 21:44:02 +00:00
|
|
|
|
2017-08-12 00:46:52 +00:00
|
|
|
dctx->dStage = dstage_init;
|
2015-03-16 16:52:14 +00:00
|
|
|
|
2015-03-18 20:38:27 +00:00
|
|
|
return frameHeaderSize;
|
2014-09-03 18:49:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-06-29 19:48:27 +00:00
|
|
|
/*! LZ4F_getFrameInfo() :
|
2017-08-12 00:46:52 +00:00
|
|
|
* This function extracts frame parameters (max blockSize, frame checksum, etc.).
|
|
|
|
* Usage is optional. Objective is to provide relevant information for allocation purposes.
|
|
|
|
* This function works in 2 situations :
|
2017-03-29 00:10:01 +00:00
|
|
|
* - At the beginning of a new frame, in which case it will decode this information from `srcBuffer`, and start the decoding process.
|
|
|
|
* Amount of input data provided must be large enough to successfully decode the frame header.
|
|
|
|
* A header size is variable, but is guaranteed to be <= LZ4F_HEADER_SIZE_MAX bytes. It's possible to provide more input data than this minimum.
|
|
|
|
* - After decoding has been started. In which case, no input is read, frame parameters are extracted from dctx.
|
2017-08-12 00:46:52 +00:00
|
|
|
* The number of bytes consumed from srcBuffer will be updated within *srcSizePtr (necessarily <= original value).
|
|
|
|
* Decompression must resume from (srcBuffer + *srcSizePtr).
|
2017-03-29 00:10:01 +00:00
|
|
|
* @return : an hint about how many srcSize bytes LZ4F_decompress() expects for next call,
|
|
|
|
* or an error code which can be tested using LZ4F_isError()
|
2017-08-12 00:46:52 +00:00
|
|
|
* note 1 : in case of error, dctx is not modified. Decoding operations can resume from where they stopped.
|
|
|
|
* note 2 : frame parameters are *copied into* an already allocated LZ4F_frameInfo_t structure.
|
2017-03-29 00:10:01 +00:00
|
|
|
*/
|
2017-08-12 00:46:52 +00:00
|
|
|
LZ4F_errorCode_t LZ4F_getFrameInfo(LZ4F_dctx* dctx, LZ4F_frameInfo_t* frameInfoPtr,
|
2015-04-19 14:23:53 +00:00
|
|
|
const void* srcBuffer, size_t* srcSizePtr)
|
2014-09-01 21:44:02 +00:00
|
|
|
{
|
2017-08-12 00:46:52 +00:00
|
|
|
if (dctx->dStage > dstage_storeFrameHeader) { /* assumption : dstage_* header enum at beginning of range */
|
2015-04-19 14:23:53 +00:00
|
|
|
/* frameInfo already decoded */
|
2016-08-11 12:44:53 +00:00
|
|
|
size_t o=0, i=0;
|
2015-04-19 14:23:53 +00:00
|
|
|
*srcSizePtr = 0;
|
2017-08-12 00:46:52 +00:00
|
|
|
*frameInfoPtr = dctx->frameInfo;
|
2017-05-10 23:28:36 +00:00
|
|
|
/* returns : recommended nb of bytes for LZ4F_decompress() */
|
2017-08-12 00:46:52 +00:00
|
|
|
return LZ4F_decompress(dctx, NULL, &o, NULL, &i, NULL);
|
2016-06-29 19:48:27 +00:00
|
|
|
} else {
|
2017-08-12 00:46:52 +00:00
|
|
|
if (dctx->dStage == dstage_storeFrameHeader) {
|
2017-03-29 00:10:01 +00:00
|
|
|
/* frame decoding already started, in the middle of header => automatic fail */
|
|
|
|
*srcSizePtr = 0;
|
|
|
|
return err0r(LZ4F_ERROR_frameDecoding_alreadyStarted);
|
|
|
|
} else {
|
|
|
|
size_t decodeResult;
|
|
|
|
size_t const hSize = LZ4F_headerSize(srcBuffer, *srcSizePtr);
|
|
|
|
if (LZ4F_isError(hSize)) { *srcSizePtr=0; return hSize; }
|
2017-05-10 23:28:36 +00:00
|
|
|
if (*srcSizePtr < hSize) {
|
|
|
|
*srcSizePtr=0;
|
|
|
|
return err0r(LZ4F_ERROR_frameHeader_incomplete);
|
|
|
|
}
|
2017-03-29 00:10:01 +00:00
|
|
|
|
2017-08-12 00:46:52 +00:00
|
|
|
decodeResult = LZ4F_decodeHeader(dctx, srcBuffer, hSize);
|
2017-03-29 00:10:01 +00:00
|
|
|
if (LZ4F_isError(decodeResult)) {
|
|
|
|
*srcSizePtr = 0;
|
|
|
|
} else {
|
|
|
|
*srcSizePtr = decodeResult;
|
|
|
|
decodeResult = BHSize; /* block header size */
|
|
|
|
}
|
2017-08-12 00:46:52 +00:00
|
|
|
*frameInfoPtr = dctx->frameInfo;
|
2017-03-29 00:10:01 +00:00
|
|
|
return decodeResult;
|
|
|
|
} }
|
2014-09-01 21:44:02 +00:00
|
|
|
}
|
|
|
|
|
2014-09-03 18:49:59 +00:00
|
|
|
|
2017-08-12 00:46:52 +00:00
|
|
|
/* LZ4F_updateDict() :
|
|
|
|
* only used for LZ4F_blockLinked mode */
|
|
|
|
static void LZ4F_updateDict(LZ4F_dctx* dctx, const BYTE* dstPtr, size_t dstSize, const BYTE* dstPtr0, unsigned withinTmp)
|
2014-09-13 18:49:01 +00:00
|
|
|
{
|
2017-08-12 00:46:52 +00:00
|
|
|
if (dctx->dictSize==0)
|
|
|
|
dctx->dict = (const BYTE*)dstPtr; /* priority to dictionary continuity */
|
2014-09-21 08:56:21 +00:00
|
|
|
|
2017-08-12 00:46:52 +00:00
|
|
|
if (dctx->dict + dctx->dictSize == dstPtr) { /* dictionary continuity */
|
|
|
|
dctx->dictSize += dstSize;
|
2014-09-13 18:49:01 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-06-29 19:48:27 +00:00
|
|
|
if (dstPtr - dstPtr0 + dstSize >= 64 KB) { /* dstBuffer large enough to become dictionary */
|
2017-08-12 00:46:52 +00:00
|
|
|
dctx->dict = (const BYTE*)dstPtr0;
|
|
|
|
dctx->dictSize = dstPtr - dstPtr0 + dstSize;
|
2014-09-13 20:21:41 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-08-12 00:46:52 +00:00
|
|
|
if ((withinTmp) && (dctx->dict == dctx->tmpOutBuffer)) {
|
|
|
|
/* assumption : dctx->dict + dctx->dictSize == dctx->tmpOut + dctx->tmpOutStart */
|
|
|
|
dctx->dictSize += dstSize;
|
2014-09-21 08:56:21 +00:00
|
|
|
return;
|
|
|
|
}
|
2014-09-09 22:54:22 +00:00
|
|
|
|
2016-06-29 19:48:27 +00:00
|
|
|
if (withinTmp) { /* copy relevant dict portion in front of tmpOut within tmpOutBuffer */
|
2017-08-12 00:46:52 +00:00
|
|
|
size_t const preserveSize = dctx->tmpOut - dctx->tmpOutBuffer;
|
|
|
|
size_t copySize = 64 KB - dctx->tmpOutSize;
|
|
|
|
const BYTE* const oldDictEnd = dctx->dict + dctx->dictSize - dctx->tmpOutStart;
|
|
|
|
if (dctx->tmpOutSize > 64 KB) copySize = 0;
|
2014-09-22 00:32:55 +00:00
|
|
|
if (copySize > preserveSize) copySize = preserveSize;
|
|
|
|
|
2017-08-12 00:46:52 +00:00
|
|
|
memcpy(dctx->tmpOutBuffer + preserveSize - copySize, oldDictEnd - copySize, copySize);
|
2014-09-22 00:32:55 +00:00
|
|
|
|
2017-08-12 00:46:52 +00:00
|
|
|
dctx->dict = dctx->tmpOutBuffer;
|
|
|
|
dctx->dictSize = preserveSize + dctx->tmpOutStart + dstSize;
|
2014-09-22 00:32:55 +00:00
|
|
|
return;
|
2014-09-21 08:56:21 +00:00
|
|
|
}
|
2014-09-09 22:54:22 +00:00
|
|
|
|
2017-08-12 00:46:52 +00:00
|
|
|
if (dctx->dict == dctx->tmpOutBuffer) { /* copy dst into tmp to complete dict */
|
|
|
|
if (dctx->dictSize + dstSize > dctx->maxBufferSize) { /* tmp buffer not large enough */
|
2016-11-15 22:01:37 +00:00
|
|
|
size_t const preserveSize = 64 KB - dstSize; /* note : dstSize < 64 KB */
|
2017-08-12 00:46:52 +00:00
|
|
|
memcpy(dctx->tmpOutBuffer, dctx->dict + dctx->dictSize - preserveSize, preserveSize);
|
|
|
|
dctx->dictSize = preserveSize;
|
2014-09-21 08:56:21 +00:00
|
|
|
}
|
2017-08-12 00:46:52 +00:00
|
|
|
memcpy(dctx->tmpOutBuffer + dctx->dictSize, dstPtr, dstSize);
|
|
|
|
dctx->dictSize += dstSize;
|
2014-09-21 08:56:21 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* join dict & dest into tmp */
|
2016-06-29 19:48:27 +00:00
|
|
|
{ size_t preserveSize = 64 KB - dstSize; /* note : dstSize < 64 KB */
|
2017-08-12 00:46:52 +00:00
|
|
|
if (preserveSize > dctx->dictSize) preserveSize = dctx->dictSize;
|
|
|
|
memcpy(dctx->tmpOutBuffer, dctx->dict + dctx->dictSize - preserveSize, preserveSize);
|
|
|
|
memcpy(dctx->tmpOutBuffer + preserveSize, dstPtr, dstSize);
|
|
|
|
dctx->dict = dctx->tmpOutBuffer;
|
|
|
|
dctx->dictSize = preserveSize + dstSize;
|
2014-09-21 08:56:21 +00:00
|
|
|
}
|
2014-09-09 22:54:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-09-21 08:56:21 +00:00
|
|
|
|
2016-06-29 19:48:27 +00:00
|
|
|
/*! LZ4F_decompress() :
|
2017-08-10 23:53:57 +00:00
|
|
|
* Call this function repetitively to regenerate compressed data in srcBuffer.
|
|
|
|
* The function will attempt to decode up to *srcSizePtr bytes from srcBuffer
|
|
|
|
* into dstBuffer of capacity *dstSizePtr.
|
2017-05-10 19:25:05 +00:00
|
|
|
*
|
2017-08-10 23:53:57 +00:00
|
|
|
* The number of bytes regenerated into dstBuffer will be provided within *dstSizePtr (necessarily <= original value).
|
2017-05-10 19:25:05 +00:00
|
|
|
*
|
2017-08-10 23:53:57 +00:00
|
|
|
* The number of bytes effectively read from srcBuffer will be provided within *srcSizePtr (necessarily <= original value).
|
|
|
|
* If number of bytes read is < number of bytes provided, then decompression operation is not complete.
|
|
|
|
* Remaining data will have to be presented again in a subsequent invocation.
|
2017-05-10 19:25:05 +00:00
|
|
|
*
|
2017-08-10 23:53:57 +00:00
|
|
|
* The function result is an hint of the better srcSize to use for next call to LZ4F_decompress.
|
|
|
|
* Schematically, it's the size of the current (or remaining) compressed block + header of next block.
|
|
|
|
* Respecting the hint provides a small boost to performance, since it allows less buffer shuffling.
|
|
|
|
* Note that this is just a hint, and it's always possible to any srcSize value.
|
|
|
|
* When a frame is fully decoded, @return will be 0.
|
|
|
|
* If decompression failed, @return is an error code which can be tested using LZ4F_isError().
|
2017-05-10 19:25:05 +00:00
|
|
|
*/
|
2017-08-12 00:46:52 +00:00
|
|
|
size_t LZ4F_decompress(LZ4F_dctx* dctx,
|
2014-09-13 11:15:54 +00:00
|
|
|
void* dstBuffer, size_t* dstSizePtr,
|
|
|
|
const void* srcBuffer, size_t* srcSizePtr,
|
|
|
|
const LZ4F_decompressOptions_t* decompressOptionsPtr)
|
2014-09-01 21:44:02 +00:00
|
|
|
{
|
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
|
|
|
|
2018-01-26 22:29:50 +00:00
|
|
|
MEM_INIT(&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
|
|
|
|
2017-08-12 00:46:52 +00:00
|
|
|
/* behaves as a state machine */
|
2014-09-13 11:15:54 +00:00
|
|
|
|
2016-06-29 19:48:27 +00:00
|
|
|
while (doAnotherStage) {
|
2014-09-13 11:15:54 +00:00
|
|
|
|
2017-08-12 00:46:52 +00:00
|
|
|
switch(dctx->dStage)
|
2014-09-01 21:44:02 +00:00
|
|
|
{
|
2014-09-13 11:15:54 +00:00
|
|
|
|
2017-08-12 00:46:52 +00:00
|
|
|
case dstage_getFrameHeader:
|
2016-08-11 12:44:53 +00:00
|
|
|
if ((size_t)(srcEnd-srcPtr) >= maxFHSize) { /* enough to decode - shortcut */
|
2017-08-12 00:46:52 +00:00
|
|
|
size_t const hSize = LZ4F_decodeHeader(dctx, srcPtr, srcEnd-srcPtr); /* will update dStage appropriately */
|
2016-08-11 12:44:53 +00:00
|
|
|
if (LZ4F_isError(hSize)) return hSize;
|
|
|
|
srcPtr += hSize;
|
|
|
|
break;
|
2014-09-04 21:56:51 +00:00
|
|
|
}
|
2017-08-12 00:46:52 +00:00
|
|
|
dctx->tmpInSize = 0;
|
2017-03-29 00:10:01 +00:00
|
|
|
if (srcEnd-srcPtr == 0) return minFHSize; /* 0-size input */
|
2017-08-12 00:46:52 +00:00
|
|
|
dctx->tmpInTarget = minFHSize; /* minimum to attempt decode */
|
|
|
|
dctx->dStage = dstage_storeFrameHeader;
|
2017-06-19 05:09:36 +00:00
|
|
|
/* fall-through */
|
2014-09-13 11:15:54 +00:00
|
|
|
|
2017-08-12 00:46:52 +00:00
|
|
|
case dstage_storeFrameHeader:
|
|
|
|
{ size_t const sizeToCopy = MIN(dctx->tmpInTarget - dctx->tmpInSize, (size_t)(srcEnd - srcPtr));
|
|
|
|
memcpy(dctx->header + dctx->tmpInSize, srcPtr, sizeToCopy);
|
|
|
|
dctx->tmpInSize += sizeToCopy;
|
2014-10-27 23:39:06 +00:00
|
|
|
srcPtr += sizeToCopy;
|
2017-09-23 22:06:24 +00:00
|
|
|
}
|
|
|
|
if (dctx->tmpInSize < dctx->tmpInTarget) {
|
|
|
|
nextSrcSizeHint = (dctx->tmpInTarget - dctx->tmpInSize) + BHSize; /* rest of header + nextBlockHeader */
|
|
|
|
doAnotherStage = 0; /* not enough src data, ask for some more */
|
2014-10-27 23:39:06 +00:00
|
|
|
break;
|
|
|
|
}
|
2017-09-23 22:06:24 +00:00
|
|
|
{ size_t const hSize = LZ4F_decodeHeader(dctx, dctx->header, dctx->tmpInTarget); /* will update dStage appropriately */
|
|
|
|
if (LZ4F_isError(hSize)) return hSize;
|
|
|
|
}
|
|
|
|
break;
|
2014-09-13 11:15:54 +00:00
|
|
|
|
2017-03-29 00:10:01 +00:00
|
|
|
case dstage_init:
|
2017-08-12 00:46:52 +00:00
|
|
|
if (dctx->frameInfo.contentChecksumFlag) XXH32_reset(&(dctx->xxh), 0);
|
2017-03-29 00:10:01 +00:00
|
|
|
/* internal buffers allocation */
|
2017-09-23 22:06:24 +00:00
|
|
|
{ size_t const bufferNeeded = dctx->maxBlockSize
|
2017-10-04 22:24:08 +00:00
|
|
|
+ ((dctx->frameInfo.blockMode==LZ4F_blockLinked) * 128 KB);
|
2017-08-12 00:46:52 +00:00
|
|
|
if (bufferNeeded > dctx->maxBufferSize) { /* tmp buffers too small */
|
|
|
|
dctx->maxBufferSize = 0; /* ensure allocation will be re-attempted on next entry*/
|
|
|
|
FREEMEM(dctx->tmpIn);
|
2018-01-26 22:29:50 +00:00
|
|
|
dctx->tmpIn = (BYTE*)ALLOC(dctx->maxBlockSize + 4 /* block checksum */);
|
2017-09-23 22:06:24 +00:00
|
|
|
if (dctx->tmpIn == NULL)
|
|
|
|
return err0r(LZ4F_ERROR_allocation_failed);
|
2017-08-12 00:46:52 +00:00
|
|
|
FREEMEM(dctx->tmpOutBuffer);
|
2018-01-26 22:29:50 +00:00
|
|
|
dctx->tmpOutBuffer= (BYTE*)ALLOC(bufferNeeded);
|
2017-09-23 22:06:24 +00:00
|
|
|
if (dctx->tmpOutBuffer== NULL)
|
|
|
|
return err0r(LZ4F_ERROR_allocation_failed);
|
2017-08-12 00:46:52 +00:00
|
|
|
dctx->maxBufferSize = bufferNeeded;
|
2017-03-29 00:10:01 +00:00
|
|
|
} }
|
2017-08-12 00:46:52 +00:00
|
|
|
dctx->tmpInSize = 0;
|
|
|
|
dctx->tmpInTarget = 0;
|
|
|
|
dctx->tmpOut = dctx->tmpOutBuffer;
|
|
|
|
dctx->tmpOutStart = 0;
|
|
|
|
dctx->tmpOutSize = 0;
|
2017-03-29 00:10:01 +00:00
|
|
|
|
2017-08-12 00:46:52 +00:00
|
|
|
dctx->dStage = dstage_getBlockHeader;
|
2017-06-19 05:09:36 +00:00
|
|
|
/* fall-through */
|
2017-03-29 00:10:01 +00:00
|
|
|
|
2017-08-12 00:46:52 +00:00
|
|
|
case dstage_getBlockHeader:
|
2016-06-29 19:48:27 +00:00
|
|
|
if ((size_t)(srcEnd - srcPtr) >= BHSize) {
|
|
|
|
selectedIn = srcPtr;
|
|
|
|
srcPtr += BHSize;
|
|
|
|
} else {
|
2014-10-27 23:39:06 +00:00
|
|
|
/* not enough input to read cBlockSize field */
|
2017-08-12 00:46:52 +00:00
|
|
|
dctx->tmpInSize = 0;
|
|
|
|
dctx->dStage = dstage_storeBlockHeader;
|
2014-09-01 21:44:02 +00:00
|
|
|
}
|
2014-09-13 11:15:54 +00:00
|
|
|
|
2017-08-12 00:46:52 +00:00
|
|
|
if (dctx->dStage == dstage_storeBlockHeader) /* can be skipped */
|
|
|
|
case dstage_storeBlockHeader:
|
2017-09-23 22:06:24 +00:00
|
|
|
{ size_t const remainingInput = (size_t)(srcEnd - srcPtr);
|
|
|
|
size_t const wantedData = BHSize - dctx->tmpInSize;
|
|
|
|
size_t const sizeToCopy = MIN(wantedData, remainingInput);
|
2017-08-12 00:46:52 +00:00
|
|
|
memcpy(dctx->tmpIn + dctx->tmpInSize, srcPtr, sizeToCopy);
|
2014-10-27 23:39:06 +00:00
|
|
|
srcPtr += sizeToCopy;
|
2017-08-12 00:46:52 +00:00
|
|
|
dctx->tmpInSize += sizeToCopy;
|
2017-09-23 22:06:24 +00:00
|
|
|
|
2017-08-12 00:46:52 +00:00
|
|
|
if (dctx->tmpInSize < BHSize) { /* not enough input for cBlockSize */
|
|
|
|
nextSrcSizeHint = BHSize - dctx->tmpInSize;
|
2015-04-19 14:23:53 +00:00
|
|
|
doAnotherStage = 0;
|
2014-10-27 23:39:06 +00:00
|
|
|
break;
|
|
|
|
}
|
2017-08-12 00:46:52 +00:00
|
|
|
selectedIn = dctx->tmpIn;
|
2017-09-23 22:06:24 +00:00
|
|
|
} /* if (dctx->dStage == dstage_storeBlockHeader) */
|
2014-09-13 11:15:54 +00:00
|
|
|
|
2017-08-12 00:46:52 +00:00
|
|
|
/* decode block header */
|
2016-08-11 12:44:53 +00:00
|
|
|
{ size_t const nextCBlockSize = LZ4F_readLE32(selectedIn) & 0x7FFFFFFFU;
|
2017-08-12 00:46:52 +00:00
|
|
|
size_t const crcSize = dctx->frameInfo.blockChecksumFlag * 4;
|
|
|
|
if (nextCBlockSize==0) { /* frameEnd signal, no more block */
|
|
|
|
dctx->dStage = dstage_getSuffix;
|
2014-10-27 23:39:06 +00:00
|
|
|
break;
|
|
|
|
}
|
2017-08-12 00:46:52 +00:00
|
|
|
if (nextCBlockSize > dctx->maxBlockSize)
|
2017-05-10 23:28:36 +00:00
|
|
|
return err0r(LZ4F_ERROR_maxBlockSize_invalid);
|
2016-06-29 19:48:27 +00:00
|
|
|
if (LZ4F_readLE32(selectedIn) & LZ4F_BLOCKUNCOMPRESSED_FLAG) {
|
2017-08-12 00:46:52 +00:00
|
|
|
/* next block is uncompressed */
|
|
|
|
dctx->tmpInTarget = nextCBlockSize;
|
|
|
|
if (dctx->frameInfo.blockChecksumFlag) {
|
|
|
|
XXH32_reset(&dctx->blockChecksum, 0);
|
|
|
|
}
|
|
|
|
dctx->dStage = dstage_copyDirect;
|
2014-10-27 23:39:06 +00:00
|
|
|
break;
|
|
|
|
}
|
2017-08-12 00:46:52 +00:00
|
|
|
/* next block is a compressed block */
|
|
|
|
dctx->tmpInTarget = nextCBlockSize + crcSize;
|
|
|
|
dctx->dStage = dstage_getCBlock;
|
2016-06-29 19:48:27 +00:00
|
|
|
if (dstPtr==dstEnd) {
|
2017-08-12 00:46:52 +00:00
|
|
|
nextSrcSizeHint = nextCBlockSize + crcSize + 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 */
|
2017-08-12 00:46:52 +00:00
|
|
|
{ size_t const minBuffSize = MIN((size_t)(srcEnd-srcPtr), (size_t)(dstEnd-dstPtr));
|
|
|
|
size_t const sizeToCopy = MIN(dctx->tmpInTarget, minBuffSize);
|
2014-10-27 23:39:06 +00:00
|
|
|
memcpy(dstPtr, srcPtr, sizeToCopy);
|
2017-08-12 00:46:52 +00:00
|
|
|
if (dctx->frameInfo.blockChecksumFlag) {
|
|
|
|
XXH32_update(&dctx->blockChecksum, srcPtr, sizeToCopy);
|
|
|
|
}
|
|
|
|
if (dctx->frameInfo.contentChecksumFlag)
|
|
|
|
XXH32_update(&dctx->xxh, srcPtr, sizeToCopy);
|
|
|
|
if (dctx->frameInfo.contentSize)
|
|
|
|
dctx->frameRemainingSize -= sizeToCopy;
|
2014-10-27 23:39:06 +00:00
|
|
|
|
2017-08-12 00:46:52 +00:00
|
|
|
/* history management (linked blocks only)*/
|
|
|
|
if (dctx->frameInfo.blockMode == LZ4F_blockLinked)
|
|
|
|
LZ4F_updateDict(dctx, dstPtr, sizeToCopy, dstStart, 0);
|
2014-10-27 23:39:06 +00:00
|
|
|
|
|
|
|
srcPtr += sizeToCopy;
|
|
|
|
dstPtr += sizeToCopy;
|
2017-08-12 00:46:52 +00:00
|
|
|
if (sizeToCopy == dctx->tmpInTarget) { /* all done */
|
|
|
|
if (dctx->frameInfo.blockChecksumFlag) {
|
|
|
|
dctx->tmpInSize = 0;
|
|
|
|
dctx->dStage = dstage_getBlockChecksum;
|
|
|
|
} else
|
|
|
|
dctx->dStage = dstage_getBlockHeader; /* new block */
|
2014-10-27 23:39:06 +00:00
|
|
|
break;
|
|
|
|
}
|
2017-08-12 00:46:52 +00:00
|
|
|
dctx->tmpInTarget -= sizeToCopy; /* need to copy more */
|
|
|
|
nextSrcSizeHint = dctx->tmpInTarget +
|
|
|
|
+ dctx->frameInfo.contentChecksumFlag * 4 /* block checksum */
|
|
|
|
+ BHSize /* next header size */;
|
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
|
|
|
|
2017-08-12 00:46:52 +00:00
|
|
|
/* check block checksum for recently transferred uncompressed block */
|
|
|
|
case dstage_getBlockChecksum:
|
|
|
|
{ const void* crcSrc;
|
|
|
|
if ((srcEnd-srcPtr >= 4) && (dctx->tmpInSize==0)) {
|
|
|
|
crcSrc = srcPtr;
|
|
|
|
srcPtr += 4;
|
|
|
|
} else {
|
|
|
|
size_t const stillToCopy = 4 - dctx->tmpInSize;
|
|
|
|
size_t const sizeToCopy = MIN(stillToCopy, (size_t)(srcEnd-srcPtr));
|
|
|
|
memcpy(dctx->header + dctx->tmpInSize, srcPtr, sizeToCopy);
|
|
|
|
dctx->tmpInSize += sizeToCopy;
|
|
|
|
srcPtr += sizeToCopy;
|
|
|
|
if (dctx->tmpInSize < 4) { /* all input consumed */
|
|
|
|
doAnotherStage = 0;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
crcSrc = dctx->header;
|
|
|
|
}
|
|
|
|
{ U32 const readCRC = LZ4F_readLE32(crcSrc);
|
|
|
|
U32 const calcCRC = XXH32_digest(&dctx->blockChecksum);
|
|
|
|
if (readCRC != calcCRC)
|
|
|
|
return err0r(LZ4F_ERROR_blockChecksum_invalid);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
dctx->dStage = dstage_getBlockHeader; /* new block */
|
|
|
|
break;
|
|
|
|
|
2017-10-04 22:24:08 +00:00
|
|
|
case dstage_getCBlock:
|
2017-08-12 00:46:52 +00:00
|
|
|
if ((size_t)(srcEnd-srcPtr) < dctx->tmpInTarget) {
|
|
|
|
dctx->tmpInSize = 0;
|
|
|
|
dctx->dStage = dstage_storeCBlock;
|
2014-09-13 11:15:54 +00:00
|
|
|
break;
|
2014-09-03 18:49:59 +00:00
|
|
|
}
|
2017-08-12 00:46:52 +00:00
|
|
|
/* input large enough to read full block directly */
|
2016-06-29 19:48:27 +00:00
|
|
|
selectedIn = srcPtr;
|
2017-08-12 00:46:52 +00:00
|
|
|
srcPtr += dctx->tmpInTarget;
|
2014-09-13 11:15:54 +00:00
|
|
|
|
2018-01-14 08:15:07 +00:00
|
|
|
if (0) /* jump over next block */
|
2014-09-03 18:49:59 +00:00
|
|
|
case dstage_storeCBlock:
|
2017-09-23 22:06:24 +00:00
|
|
|
{ size_t const wantedData = dctx->tmpInTarget - dctx->tmpInSize;
|
|
|
|
size_t const inputLeft = (size_t)(srcEnd-srcPtr);
|
|
|
|
size_t const sizeToCopy = MIN(wantedData, inputLeft);
|
2017-08-12 00:46:52 +00:00
|
|
|
memcpy(dctx->tmpIn + dctx->tmpInSize, srcPtr, sizeToCopy);
|
|
|
|
dctx->tmpInSize += sizeToCopy;
|
2014-10-27 23:39:06 +00:00
|
|
|
srcPtr += sizeToCopy;
|
2017-08-12 00:46:52 +00:00
|
|
|
if (dctx->tmpInSize < dctx->tmpInTarget) { /* need more input */
|
|
|
|
nextSrcSizeHint = (dctx->tmpInTarget - dctx->tmpInSize) + BHSize;
|
2014-10-27 23:39:06 +00:00
|
|
|
doAnotherStage=0;
|
|
|
|
break;
|
|
|
|
}
|
2017-08-12 00:46:52 +00:00
|
|
|
selectedIn = dctx->tmpIn;
|
2014-09-03 18:49:59 +00:00
|
|
|
}
|
2014-09-13 11:15:54 +00:00
|
|
|
|
2018-01-14 08:15:07 +00:00
|
|
|
/* At this stage, input is large enough to decode a block */
|
2017-08-12 00:46:52 +00:00
|
|
|
if (dctx->frameInfo.blockChecksumFlag) {
|
|
|
|
dctx->tmpInTarget -= 4;
|
2018-01-14 06:47:46 +00:00
|
|
|
assert(selectedIn != NULL); /* selectedIn is defined at this stage (either srcPtr, or dctx->tmpIn) */
|
2017-08-12 00:46:52 +00:00
|
|
|
{ U32 const readBlockCrc = LZ4F_readLE32(selectedIn + dctx->tmpInTarget);
|
|
|
|
U32 const calcBlockCrc = XXH32(selectedIn, dctx->tmpInTarget, 0);
|
|
|
|
if (readBlockCrc != calcBlockCrc)
|
|
|
|
return err0r(LZ4F_ERROR_blockChecksum_invalid);
|
|
|
|
} }
|
2014-09-21 08:56:21 +00:00
|
|
|
|
2018-01-14 08:15:07 +00:00
|
|
|
if ((size_t)(dstEnd-dstPtr) >= dctx->maxBlockSize) {
|
|
|
|
/* enough capacity in `dst` to decompress directly there */
|
|
|
|
int const decodedSize = LZ4_decompress_safe_usingDict(
|
2017-08-10 23:53:57 +00:00
|
|
|
(const char*)selectedIn, (char*)dstPtr,
|
2017-08-12 00:46:52 +00:00
|
|
|
(int)dctx->tmpInTarget, (int)dctx->maxBlockSize,
|
|
|
|
(const char*)dctx->dict, (int)dctx->dictSize);
|
2016-11-04 03:32:51 +00:00
|
|
|
if (decodedSize < 0) return err0r(LZ4F_ERROR_GENERIC); /* decompression failed */
|
2017-08-12 00:46:52 +00:00
|
|
|
if (dctx->frameInfo.contentChecksumFlag)
|
|
|
|
XXH32_update(&(dctx->xxh), dstPtr, decodedSize);
|
|
|
|
if (dctx->frameInfo.contentSize)
|
|
|
|
dctx->frameRemainingSize -= decodedSize;
|
2014-09-21 08:56:21 +00:00
|
|
|
|
2014-10-27 23:39:06 +00:00
|
|
|
/* dictionary management */
|
2017-08-12 00:46:52 +00:00
|
|
|
if (dctx->frameInfo.blockMode==LZ4F_blockLinked)
|
|
|
|
LZ4F_updateDict(dctx, dstPtr, decodedSize, dstStart, 0);
|
2014-09-21 08:56:21 +00:00
|
|
|
|
2014-10-27 23:39:06 +00:00
|
|
|
dstPtr += decodedSize;
|
2017-08-12 00:46:52 +00:00
|
|
|
dctx->dStage = dstage_getBlockHeader;
|
2014-10-27 23:39:06 +00:00
|
|
|
break;
|
|
|
|
}
|
2014-09-13 11:15:54 +00:00
|
|
|
|
2016-06-29 19:48:27 +00:00
|
|
|
/* not enough place into dst : decode into tmpOut */
|
2017-08-12 00:46:52 +00:00
|
|
|
/* ensure enough place for tmpOut */
|
|
|
|
if (dctx->frameInfo.blockMode == LZ4F_blockLinked) {
|
|
|
|
if (dctx->dict == dctx->tmpOutBuffer) {
|
|
|
|
if (dctx->dictSize > 128 KB) {
|
|
|
|
memcpy(dctx->tmpOutBuffer, dctx->dict + dctx->dictSize - 64 KB, 64 KB);
|
|
|
|
dctx->dictSize = 64 KB;
|
2014-09-21 08:56:21 +00:00
|
|
|
}
|
2017-08-12 00:46:52 +00:00
|
|
|
dctx->tmpOut = dctx->tmpOutBuffer + dctx->dictSize;
|
|
|
|
} else { /* dict not within tmp */
|
|
|
|
size_t const reservedDictSpace = MIN(dctx->dictSize, 64 KB);
|
|
|
|
dctx->tmpOut = dctx->tmpOutBuffer + reservedDictSpace;
|
2014-09-21 08:56:21 +00:00
|
|
|
}
|
2017-08-12 00:46:52 +00:00
|
|
|
}
|
2014-09-21 08:56:21 +00:00
|
|
|
|
2017-08-12 00:46:52 +00:00
|
|
|
/* Decode block */
|
|
|
|
{ int const decodedSize = LZ4_decompress_safe_usingDict(
|
|
|
|
(const char*)selectedIn, (char*)dctx->tmpOut,
|
|
|
|
(int)dctx->tmpInTarget, (int)dctx->maxBlockSize,
|
|
|
|
(const char*)dctx->dict, (int)dctx->dictSize);
|
|
|
|
if (decodedSize < 0) /* decompression failed */
|
|
|
|
return err0r(LZ4F_ERROR_decompressionFailed);
|
|
|
|
if (dctx->frameInfo.contentChecksumFlag)
|
|
|
|
XXH32_update(&(dctx->xxh), dctx->tmpOut, decodedSize);
|
|
|
|
if (dctx->frameInfo.contentSize)
|
|
|
|
dctx->frameRemainingSize -= decodedSize;
|
|
|
|
dctx->tmpOutSize = decodedSize;
|
|
|
|
dctx->tmpOutStart = 0;
|
|
|
|
dctx->dStage = dstage_flushOut;
|
2014-10-27 23:39:06 +00:00
|
|
|
}
|
2017-08-12 00:46:52 +00:00
|
|
|
/* fall-through */
|
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 */
|
2017-08-12 00:46:52 +00:00
|
|
|
{ size_t const sizeToCopy = MIN(dctx->tmpOutSize - dctx->tmpOutStart, (size_t)(dstEnd-dstPtr));
|
|
|
|
memcpy(dstPtr, dctx->tmpOut + dctx->tmpOutStart, sizeToCopy);
|
2014-09-21 08:56:21 +00:00
|
|
|
|
2014-10-27 23:39:06 +00:00
|
|
|
/* dictionary management */
|
2017-08-12 00:46:52 +00:00
|
|
|
if (dctx->frameInfo.blockMode==LZ4F_blockLinked)
|
|
|
|
LZ4F_updateDict(dctx, dstPtr, sizeToCopy, dstStart, 1);
|
2014-09-21 08:56:21 +00:00
|
|
|
|
2017-08-12 00:46:52 +00:00
|
|
|
dctx->tmpOutStart += sizeToCopy;
|
2014-10-27 23:39:06 +00:00
|
|
|
dstPtr += sizeToCopy;
|
2014-09-21 08:56:21 +00:00
|
|
|
|
2017-08-12 00:46:52 +00:00
|
|
|
if (dctx->tmpOutStart == dctx->tmpOutSize) { /* all flushed */
|
|
|
|
dctx->dStage = dstage_getBlockHeader; /* get next block */
|
2014-10-27 23:39:06 +00:00
|
|
|
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:
|
2017-09-23 22:06:24 +00:00
|
|
|
if (dctx->frameRemainingSize)
|
|
|
|
return err0r(LZ4F_ERROR_frameSize_wrong); /* incorrect frame size decoded */
|
|
|
|
if (!dctx->frameInfo.contentChecksumFlag) { /* no checksum, frame is completed */
|
|
|
|
nextSrcSizeHint = 0;
|
|
|
|
LZ4F_resetDecompressionContext(dctx);
|
|
|
|
doAnotherStage = 0;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if ((srcEnd - srcPtr) < 4) { /* not enough size for entire CRC */
|
|
|
|
dctx->tmpInSize = 0;
|
|
|
|
dctx->dStage = dstage_storeSuffix;
|
|
|
|
} else {
|
|
|
|
selectedIn = srcPtr;
|
|
|
|
srcPtr += 4;
|
2014-09-03 18:49:59 +00:00
|
|
|
}
|
2014-10-27 23:39:06 +00:00
|
|
|
|
2017-08-12 00:46:52 +00:00
|
|
|
if (dctx->dStage == dstage_storeSuffix) /* can be skipped */
|
2014-10-27 23:39:06 +00:00
|
|
|
case dstage_storeSuffix:
|
2017-09-23 22:06:24 +00:00
|
|
|
{ size_t const remainingInput = (size_t)(srcEnd - srcPtr);
|
|
|
|
size_t const wantedData = 4 - dctx->tmpInSize;
|
|
|
|
size_t const sizeToCopy = MIN(wantedData, remainingInput);
|
2017-08-12 00:46:52 +00:00
|
|
|
memcpy(dctx->tmpIn + dctx->tmpInSize, srcPtr, sizeToCopy);
|
2014-10-27 23:39:06 +00:00
|
|
|
srcPtr += sizeToCopy;
|
2017-08-12 00:46:52 +00:00
|
|
|
dctx->tmpInSize += sizeToCopy;
|
|
|
|
if (dctx->tmpInSize < 4) { /* not enough input to read complete suffix */
|
|
|
|
nextSrcSizeHint = 4 - dctx->tmpInSize;
|
2014-10-27 23:39:06 +00:00
|
|
|
doAnotherStage=0;
|
|
|
|
break;
|
|
|
|
}
|
2017-08-12 00:46:52 +00:00
|
|
|
selectedIn = dctx->tmpIn;
|
2017-09-23 22:06:24 +00:00
|
|
|
} /* if (dctx->dStage == dstage_storeSuffix) */
|
2014-09-13 11:15:54 +00:00
|
|
|
|
2017-08-12 00:46:52 +00:00
|
|
|
/* case dstage_checkSuffix: */ /* no direct call, avoid scan-build warning */
|
2016-06-29 19:48:27 +00:00
|
|
|
{ U32 const readCRC = LZ4F_readLE32(selectedIn);
|
2017-08-12 00:46:52 +00:00
|
|
|
U32 const resultCRC = XXH32_digest(&(dctx->xxh));
|
|
|
|
if (readCRC != resultCRC)
|
|
|
|
return err0r(LZ4F_ERROR_contentChecksum_invalid);
|
2014-10-27 23:39:06 +00:00
|
|
|
nextSrcSizeHint = 0;
|
2017-08-12 00:46:52 +00:00
|
|
|
LZ4F_resetDecompressionContext(dctx);
|
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
|
|
|
}
|
2015-03-16 16:52:14 +00:00
|
|
|
|
2015-03-18 20:38:27 +00:00
|
|
|
case dstage_getSFrameSize:
|
2016-06-29 19:48:27 +00:00
|
|
|
if ((srcEnd - srcPtr) >= 4) {
|
|
|
|
selectedIn = srcPtr;
|
|
|
|
srcPtr += 4;
|
|
|
|
} else {
|
2015-03-16 16:52:14 +00:00
|
|
|
/* not enough input to read cBlockSize field */
|
2017-08-12 00:46:52 +00:00
|
|
|
dctx->tmpInSize = 4;
|
|
|
|
dctx->tmpInTarget = 8;
|
|
|
|
dctx->dStage = dstage_storeSFrameSize;
|
2015-03-16 16:52:14 +00:00
|
|
|
}
|
|
|
|
|
2017-08-12 00:46:52 +00:00
|
|
|
if (dctx->dStage == dstage_storeSFrameSize)
|
2015-03-18 20:38:27 +00:00
|
|
|
case dstage_storeSFrameSize:
|
2015-03-16 16:52:14 +00:00
|
|
|
{
|
2017-08-12 00:46:52 +00:00
|
|
|
size_t const sizeToCopy = MIN(dctx->tmpInTarget - dctx->tmpInSize,
|
|
|
|
(size_t)(srcEnd - srcPtr) );
|
|
|
|
memcpy(dctx->header + dctx->tmpInSize, srcPtr, sizeToCopy);
|
2015-03-16 16:52:14 +00:00
|
|
|
srcPtr += sizeToCopy;
|
2017-08-12 00:46:52 +00:00
|
|
|
dctx->tmpInSize += sizeToCopy;
|
|
|
|
if (dctx->tmpInSize < dctx->tmpInTarget) {
|
2017-05-10 23:28:36 +00:00
|
|
|
/* not enough input to get full sBlockSize; wait for more */
|
2017-08-12 00:46:52 +00:00
|
|
|
nextSrcSizeHint = dctx->tmpInTarget - dctx->tmpInSize;
|
2015-03-18 20:38:27 +00:00
|
|
|
doAnotherStage = 0;
|
2015-03-16 16:52:14 +00:00
|
|
|
break;
|
|
|
|
}
|
2017-08-12 00:46:52 +00:00
|
|
|
selectedIn = dctx->header + 4;
|
2017-09-23 22:06:24 +00:00
|
|
|
} /* if (dctx->dStage == dstage_storeSFrameSize) */
|
2015-03-16 16:52:14 +00:00
|
|
|
|
2015-04-09 21:59:07 +00:00
|
|
|
/* case dstage_decodeSFrameSize: */ /* no direct access */
|
2016-06-29 19:48:27 +00:00
|
|
|
{ size_t const SFrameSize = LZ4F_readLE32(selectedIn);
|
2017-08-12 00:46:52 +00:00
|
|
|
dctx->frameInfo.contentSize = SFrameSize;
|
|
|
|
dctx->tmpInTarget = SFrameSize;
|
|
|
|
dctx->dStage = dstage_skipSkippable;
|
2015-03-16 16:52:14 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case dstage_skipSkippable:
|
2017-08-12 00:46:52 +00:00
|
|
|
{ size_t const skipSize = MIN(dctx->tmpInTarget, (size_t)(srcEnd-srcPtr));
|
2015-03-16 16:52:14 +00:00
|
|
|
srcPtr += skipSize;
|
2017-08-12 00:46:52 +00:00
|
|
|
dctx->tmpInTarget -= skipSize;
|
2015-03-16 16:52:14 +00:00
|
|
|
doAnotherStage = 0;
|
2017-08-12 00:46:52 +00:00
|
|
|
nextSrcSizeHint = dctx->tmpInTarget;
|
|
|
|
if (nextSrcSizeHint) break; /* still more to skip */
|
2017-09-23 22:06:24 +00:00
|
|
|
/* frame fully skipped : prepare context for a new frame */
|
2017-08-12 00:46:52 +00:00
|
|
|
LZ4F_resetDecompressionContext(dctx);
|
2015-03-16 16:52:14 +00:00
|
|
|
break;
|
|
|
|
}
|
2014-09-01 21:44:02 +00:00
|
|
|
}
|
2017-09-23 22:06:24 +00:00
|
|
|
} /* while (doAnotherStage) */
|
|
|
|
|
|
|
|
/* preserve history within tmp whenever necessary */
|
|
|
|
LZ4F_STATIC_ASSERT((unsigned)dstage_init == 2);
|
|
|
|
if ( (dctx->frameInfo.blockMode==LZ4F_blockLinked) /* next block will use up to 64KB from previous ones */
|
|
|
|
&& (dctx->dict != dctx->tmpOutBuffer) /* dictionary is not already within tmp */
|
|
|
|
&& (!decompressOptionsPtr->stableDst) /* cannot rely on dst data to remain there for next call */
|
|
|
|
&& ((unsigned)(dctx->dStage)-2 < (unsigned)(dstage_getSuffix)-2) ) /* valid stages : [init ... getSuffix[ */
|
2014-09-21 08:56:21 +00:00
|
|
|
{
|
2017-08-12 00:46:52 +00:00
|
|
|
if (dctx->dStage == dstage_flushOut) {
|
2017-09-23 22:06:24 +00:00
|
|
|
size_t const preserveSize = dctx->tmpOut - dctx->tmpOutBuffer;
|
2017-08-12 00:46:52 +00:00
|
|
|
size_t copySize = 64 KB - dctx->tmpOutSize;
|
|
|
|
const BYTE* oldDictEnd = dctx->dict + dctx->dictSize - dctx->tmpOutStart;
|
|
|
|
if (dctx->tmpOutSize > 64 KB) copySize = 0;
|
2014-09-22 00:32:55 +00:00
|
|
|
if (copySize > preserveSize) copySize = preserveSize;
|
2014-09-21 08:56:21 +00:00
|
|
|
|
2018-01-05 22:30:49 +00:00
|
|
|
if (copySize > 0)
|
|
|
|
memcpy(dctx->tmpOutBuffer + preserveSize - copySize, oldDictEnd - copySize, copySize);
|
2014-09-21 08:56:21 +00:00
|
|
|
|
2017-08-12 00:46:52 +00:00
|
|
|
dctx->dict = dctx->tmpOutBuffer;
|
|
|
|
dctx->dictSize = preserveSize + dctx->tmpOutStart;
|
2016-06-29 19:48:27 +00:00
|
|
|
} else {
|
2017-09-23 22:06:24 +00:00
|
|
|
const BYTE* const oldDictEnd = dctx->dict + dctx->dictSize;
|
|
|
|
size_t const newDictSize = MIN(dctx->dictSize, 64 KB);
|
2014-09-21 08:56:21 +00:00
|
|
|
|
2018-01-05 22:30:49 +00:00
|
|
|
if (newDictSize > 0)
|
|
|
|
memcpy(dctx->tmpOutBuffer, oldDictEnd - newDictSize, newDictSize);
|
2014-09-21 08:56:21 +00:00
|
|
|
|
2017-08-12 00:46:52 +00:00
|
|
|
dctx->dict = dctx->tmpOutBuffer;
|
|
|
|
dctx->dictSize = newDictSize;
|
|
|
|
dctx->tmpOut = dctx->tmpOutBuffer + newDictSize;
|
2014-09-21 08:56:21 +00:00
|
|
|
}
|
|
|
|
}
|
2014-09-03 18:49:59 +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
|
|
|
}
|
2017-08-10 23:53:57 +00:00
|
|
|
|
|
|
|
/*! LZ4F_decompress_usingDict() :
|
|
|
|
* Same as LZ4F_decompress(), using a predefined dictionary.
|
|
|
|
* Dictionary is used "in place", without any preprocessing.
|
|
|
|
* It must remain accessible throughout the entire frame decoding.
|
|
|
|
*/
|
2017-08-12 00:46:52 +00:00
|
|
|
size_t LZ4F_decompress_usingDict(LZ4F_dctx* dctx,
|
2017-08-10 23:53:57 +00:00
|
|
|
void* dstBuffer, size_t* dstSizePtr,
|
|
|
|
const void* srcBuffer, size_t* srcSizePtr,
|
|
|
|
const void* dict, size_t dictSize,
|
|
|
|
const LZ4F_decompressOptions_t* decompressOptionsPtr)
|
|
|
|
{
|
2017-08-12 00:46:52 +00:00
|
|
|
if (dctx->dStage <= dstage_init) {
|
|
|
|
dctx->dict = (const BYTE*)dict;
|
|
|
|
dctx->dictSize = dictSize;
|
2017-08-10 23:53:57 +00:00
|
|
|
}
|
2017-08-12 00:46:52 +00:00
|
|
|
return LZ4F_decompress(dctx, dstBuffer, dstSizePtr,
|
2017-08-10 23:53:57 +00:00
|
|
|
srcBuffer, srcSizePtr,
|
|
|
|
decompressOptionsPtr);
|
|
|
|
}
|