2015-01-24 00:58:16 +00:00
|
|
|
/*
|
|
|
|
zstd - standard compression library
|
|
|
|
Copyright (C) 2014-2015, Yann Collet.
|
|
|
|
|
|
|
|
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 :
|
|
|
|
- zstd source repository : https://github.com/Cyan4973/zstd
|
|
|
|
- ztsd public forum : https://groups.google.com/forum/#!forum/lz4c
|
|
|
|
*/
|
|
|
|
|
2015-10-21 08:07:25 +00:00
|
|
|
/* ***************************************************************
|
2015-01-24 00:58:16 +00:00
|
|
|
* Tuning parameters
|
2015-10-21 08:07:25 +00:00
|
|
|
*****************************************************************/
|
|
|
|
/*!
|
|
|
|
* MEMORY_USAGE :
|
2015-01-24 00:58:16 +00:00
|
|
|
* Memory usage formula : N->2^N Bytes (examples : 10 -> 1KB; 12 -> 4KB ; 16 -> 64KB; 20 -> 1MB; etc.)
|
|
|
|
* Increasing memory usage improves compression ratio
|
2015-10-21 08:07:25 +00:00
|
|
|
* Reduced memory usage can improve speed, due to cache effect
|
|
|
|
*/
|
2015-10-30 05:40:22 +00:00
|
|
|
#define ZSTD_MEMORY_USAGE 16
|
2015-01-24 00:58:16 +00:00
|
|
|
|
2015-10-21 08:07:25 +00:00
|
|
|
/*!
|
|
|
|
* HEAPMODE :
|
|
|
|
* Select how default compression functions will allocate memory for their hash table,
|
|
|
|
* in memory stack (0, fastest), or in memory heap (1, requires malloc())
|
|
|
|
* Note that compression context is fairly large, as a consequence heap memory is recommended.
|
|
|
|
*/
|
|
|
|
#ifndef ZSTD_HEAPMODE
|
|
|
|
# define ZSTD_HEAPMODE 1
|
|
|
|
#endif /* ZSTD_HEAPMODE */
|
|
|
|
|
|
|
|
/*!
|
|
|
|
* LEGACY_SUPPORT :
|
|
|
|
* decompressor can decode older formats (starting from Zstd 0.1+)
|
|
|
|
*/
|
2015-10-18 21:18:32 +00:00
|
|
|
#ifndef ZSTD_LEGACY_SUPPORT
|
|
|
|
# define ZSTD_LEGACY_SUPPORT 1
|
2015-10-21 08:07:25 +00:00
|
|
|
#endif
|
2015-01-24 00:58:16 +00:00
|
|
|
|
|
|
|
|
2015-10-21 08:07:25 +00:00
|
|
|
/* *******************************************************
|
2015-01-24 00:58:16 +00:00
|
|
|
* Includes
|
2015-10-21 08:07:25 +00:00
|
|
|
*********************************************************/
|
2015-01-24 00:58:16 +00:00
|
|
|
#include <stdlib.h> /* calloc */
|
|
|
|
#include <string.h> /* memcpy, memmove */
|
|
|
|
#include <stdio.h> /* debug : printf */
|
2015-10-18 21:18:32 +00:00
|
|
|
#include "mem.h" /* low level memory routines */
|
2015-01-24 00:58:16 +00:00
|
|
|
#include "zstd_static.h"
|
2015-10-29 16:15:14 +00:00
|
|
|
#include "zstd_internal.h"
|
2015-10-18 21:18:32 +00:00
|
|
|
#include "fse_static.h"
|
|
|
|
#include "huff0.h"
|
|
|
|
|
|
|
|
#if defined(ZSTD_LEGACY_SUPPORT) && (ZSTD_LEGACY_SUPPORT==1)
|
2015-10-30 10:21:50 +00:00
|
|
|
# include "zstd_legacy.h"
|
2015-10-21 08:07:25 +00:00
|
|
|
#endif
|
2015-01-24 00:58:16 +00:00
|
|
|
|
|
|
|
|
2015-10-21 08:07:25 +00:00
|
|
|
/* *******************************************************
|
2015-01-24 00:58:16 +00:00
|
|
|
* Compiler specifics
|
2015-10-21 08:07:25 +00:00
|
|
|
*********************************************************/
|
2015-02-01 09:13:22 +00:00
|
|
|
#ifdef __AVX2__
|
2015-01-28 00:23:14 +00:00
|
|
|
# include <immintrin.h> /* AVX2 intrinsics */
|
|
|
|
#endif
|
|
|
|
|
2015-01-24 00:58:16 +00:00
|
|
|
#ifdef _MSC_VER /* Visual Studio */
|
|
|
|
# define FORCE_INLINE static __forceinline
|
|
|
|
# include <intrin.h> /* For Visual 2005 */
|
|
|
|
# pragma warning(disable : 4127) /* disable: C4127: conditional expression is constant */
|
|
|
|
# pragma warning(disable : 4324) /* disable: C4324: padded structure */
|
|
|
|
#else
|
|
|
|
# define GCC_VERSION (__GNUC__ * 100 + __GNUC_MINOR__)
|
|
|
|
# ifdef __GNUC__
|
|
|
|
# define FORCE_INLINE static inline __attribute__((always_inline))
|
|
|
|
# else
|
|
|
|
# define FORCE_INLINE static inline
|
|
|
|
# endif
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
2015-10-19 01:14:43 +00:00
|
|
|
/* *******************************************************
|
2015-01-24 00:58:16 +00:00
|
|
|
* Constants
|
|
|
|
*********************************************************/
|
|
|
|
#define HASH_LOG (ZSTD_MEMORY_USAGE - 2)
|
|
|
|
#define HASH_TABLESIZE (1 << HASH_LOG)
|
|
|
|
#define HASH_MASK (HASH_TABLESIZE - 1)
|
|
|
|
|
|
|
|
#define KNUTH 2654435761
|
|
|
|
|
|
|
|
#define BIT7 128
|
|
|
|
#define BIT6 64
|
|
|
|
#define BIT5 32
|
|
|
|
#define BIT4 16
|
2015-10-19 18:25:44 +00:00
|
|
|
#define BIT1 2
|
|
|
|
#define BIT0 1
|
2015-01-24 00:58:16 +00:00
|
|
|
|
2015-01-31 14:09:51 +00:00
|
|
|
#define KB *(1 <<10)
|
|
|
|
#define MB *(1 <<20)
|
2015-02-16 17:06:26 +00:00
|
|
|
#define GB *(1U<<30)
|
2015-01-24 00:58:16 +00:00
|
|
|
|
2015-02-09 02:50:11 +00:00
|
|
|
#define BLOCKSIZE (128 KB) /* define, for static allocation */
|
2015-10-19 18:25:44 +00:00
|
|
|
#define MIN_SEQUENCES_SIZE (2 /*seqNb*/ + 2 /*dumps*/ + 3 /*seqTables*/ + 1 /*bitStream*/)
|
|
|
|
#define MIN_CBLOCK_SIZE (3 /*litCSize*/ + MIN_SEQUENCES_SIZE)
|
|
|
|
#define IS_RAW BIT0
|
|
|
|
#define IS_RLE BIT1
|
|
|
|
|
2015-02-17 12:38:44 +00:00
|
|
|
static const U32 g_maxDistance = 4 * BLOCKSIZE;
|
2015-01-31 14:09:51 +00:00
|
|
|
static const U32 g_maxLimit = 1 GB;
|
2015-01-24 00:58:16 +00:00
|
|
|
static const U32 g_searchStrength = 8;
|
|
|
|
|
2015-07-08 00:54:25 +00:00
|
|
|
#define WORKPLACESIZE (BLOCKSIZE*3)
|
2015-01-24 00:58:16 +00:00
|
|
|
#define MINMATCH 4
|
|
|
|
#define LitFSELog 11
|
|
|
|
#define MLFSELog 10
|
|
|
|
#define LLFSELog 10
|
|
|
|
#define OffFSELog 9
|
2015-08-08 01:16:11 +00:00
|
|
|
#define MAX(a,b) ((a)<(b)?(b):(a))
|
|
|
|
#define MaxSeq MAX(MaxLL, MaxML)
|
2015-01-24 00:58:16 +00:00
|
|
|
|
|
|
|
#define LITERAL_NOENTROPY 63
|
|
|
|
#define COMMAND_NOENTROPY 7 /* to remove */
|
|
|
|
|
|
|
|
static const size_t ZSTD_blockHeaderSize = 3;
|
|
|
|
static const size_t ZSTD_frameHeaderSize = 4;
|
|
|
|
|
|
|
|
|
2015-10-19 01:14:43 +00:00
|
|
|
/* *******************************************************
|
2015-01-24 00:58:16 +00:00
|
|
|
* Memory operations
|
2015-10-19 01:14:43 +00:00
|
|
|
**********************************************************/
|
2015-10-22 14:31:46 +00:00
|
|
|
static void ZSTD_copy4(void* dst, const void* src) { memcpy(dst, src, 4); }
|
2015-01-24 00:58:16 +00:00
|
|
|
|
|
|
|
|
2015-10-19 01:14:43 +00:00
|
|
|
/* **************************************
|
2015-01-24 00:58:16 +00:00
|
|
|
* Local structures
|
2015-10-19 01:14:43 +00:00
|
|
|
****************************************/
|
2015-02-09 02:50:11 +00:00
|
|
|
void ZSTD_resetSeqStore(seqStore_t* ssPtr)
|
|
|
|
{
|
|
|
|
ssPtr->offset = ssPtr->offsetStart;
|
|
|
|
ssPtr->lit = ssPtr->litStart;
|
|
|
|
ssPtr->litLength = ssPtr->litLengthStart;
|
|
|
|
ssPtr->matchLength = ssPtr->matchLengthStart;
|
|
|
|
ssPtr->dumps = ssPtr->dumpsStart;
|
|
|
|
}
|
|
|
|
|
2015-10-21 13:39:26 +00:00
|
|
|
struct ZSTD_CCtx_s
|
2015-01-24 00:58:16 +00:00
|
|
|
{
|
|
|
|
const BYTE* base;
|
|
|
|
U32 current;
|
2015-01-31 14:09:51 +00:00
|
|
|
U32 nextUpdate;
|
2015-02-09 02:50:11 +00:00
|
|
|
seqStore_t seqStore;
|
2015-02-01 10:57:30 +00:00
|
|
|
#ifdef __AVX2__
|
|
|
|
__m256i hashTable[HASH_TABLESIZE>>3];
|
|
|
|
#else
|
|
|
|
U32 hashTable[HASH_TABLESIZE];
|
2015-01-24 00:58:16 +00:00
|
|
|
#endif
|
2015-10-23 14:21:53 +00:00
|
|
|
BYTE buffer[WORKPLACESIZE];
|
2015-10-21 08:07:25 +00:00
|
|
|
};
|
2015-01-24 00:58:16 +00:00
|
|
|
|
|
|
|
|
2015-10-21 13:39:26 +00:00
|
|
|
void ZSTD_resetCCtx(ZSTD_CCtx* ctx)
|
2015-01-24 00:58:16 +00:00
|
|
|
{
|
2015-10-21 08:07:25 +00:00
|
|
|
ctx->base = NULL;
|
2015-10-23 14:21:53 +00:00
|
|
|
ctx->seqStore.buffer = ctx->buffer;
|
2015-02-09 02:50:11 +00:00
|
|
|
ctx->seqStore.offsetStart = (U32*) (ctx->seqStore.buffer);
|
2015-07-08 00:54:25 +00:00
|
|
|
ctx->seqStore.offCodeStart = (BYTE*) (ctx->seqStore.offsetStart + (BLOCKSIZE>>2));
|
|
|
|
ctx->seqStore.litStart = ctx->seqStore.offCodeStart + (BLOCKSIZE>>2);
|
2015-02-09 02:50:11 +00:00
|
|
|
ctx->seqStore.litLengthStart = ctx->seqStore.litStart + BLOCKSIZE;
|
|
|
|
ctx->seqStore.matchLengthStart = ctx->seqStore.litLengthStart + (BLOCKSIZE>>2);
|
|
|
|
ctx->seqStore.dumpsStart = ctx->seqStore.matchLengthStart + (BLOCKSIZE>>2);
|
2015-10-22 14:31:46 +00:00
|
|
|
memset(ctx->hashTable, 0, sizeof(ctx->hashTable));
|
2015-01-24 00:58:16 +00:00
|
|
|
}
|
|
|
|
|
2015-10-21 13:39:26 +00:00
|
|
|
ZSTD_CCtx* ZSTD_createCCtx(void)
|
2015-01-24 00:58:16 +00:00
|
|
|
{
|
2015-10-21 13:39:26 +00:00
|
|
|
ZSTD_CCtx* ctx = (ZSTD_CCtx*) malloc( sizeof(ZSTD_CCtx) );
|
2015-10-21 08:07:25 +00:00
|
|
|
if (ctx==NULL) return NULL;
|
|
|
|
ZSTD_resetCCtx(ctx);
|
|
|
|
return ctx;
|
2015-01-24 00:58:16 +00:00
|
|
|
}
|
|
|
|
|
2015-10-21 13:39:26 +00:00
|
|
|
size_t ZSTD_freeCCtx(ZSTD_CCtx* ctx)
|
2015-01-24 00:58:16 +00:00
|
|
|
{
|
2015-02-09 02:50:11 +00:00
|
|
|
free(ctx);
|
2015-01-24 00:58:16 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-10-19 01:14:43 +00:00
|
|
|
/* *************************************
|
2015-01-24 00:58:16 +00:00
|
|
|
* Error Management
|
2015-10-19 01:14:43 +00:00
|
|
|
***************************************/
|
2015-10-18 21:18:32 +00:00
|
|
|
/*! ZSTD_isError
|
|
|
|
* tells if a return value is an error code */
|
|
|
|
unsigned ZSTD_isError(size_t code) { return ERR_isError(code); }
|
2015-01-24 00:58:16 +00:00
|
|
|
|
2015-10-18 21:18:32 +00:00
|
|
|
/*! ZSTD_getErrorName
|
|
|
|
* provides error code string (useful for debugging) */
|
|
|
|
const char* ZSTD_getErrorName(size_t code) { return ERR_getErrorName(code); }
|
2015-01-24 00:58:16 +00:00
|
|
|
|
|
|
|
|
2015-10-19 01:14:43 +00:00
|
|
|
/* *************************************
|
2015-01-24 00:58:16 +00:00
|
|
|
* Tool functions
|
2015-10-19 01:14:43 +00:00
|
|
|
***************************************/
|
2015-01-24 00:58:16 +00:00
|
|
|
unsigned ZSTD_versionNumber (void) { return ZSTD_VERSION_NUMBER; }
|
|
|
|
|
2015-10-21 13:39:26 +00:00
|
|
|
|
2015-10-19 01:14:43 +00:00
|
|
|
/* *******************************************************
|
2015-01-24 00:58:16 +00:00
|
|
|
* Compression
|
|
|
|
*********************************************************/
|
|
|
|
size_t ZSTD_compressBound(size_t srcSize) /* maximum compressed size */
|
|
|
|
{
|
|
|
|
return FSE_compressBound(srcSize) + 12;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-10-22 14:31:46 +00:00
|
|
|
size_t ZSTD_noCompressBlock (void* dst, size_t maxDstSize, const void* src, size_t srcSize)
|
2015-01-24 00:58:16 +00:00
|
|
|
{
|
2015-01-26 09:24:04 +00:00
|
|
|
BYTE* const ostart = (BYTE* const)dst;
|
2015-01-24 00:58:16 +00:00
|
|
|
|
2015-10-19 18:25:44 +00:00
|
|
|
if (srcSize + ZSTD_blockHeaderSize > maxDstSize) return ERROR(dstSize_tooSmall);
|
|
|
|
memcpy(ostart + ZSTD_blockHeaderSize, src, srcSize);
|
2015-01-24 00:58:16 +00:00
|
|
|
|
2015-02-09 02:50:11 +00:00
|
|
|
/* Build header */
|
|
|
|
ostart[0] = (BYTE)(srcSize>>16);
|
|
|
|
ostart[1] = (BYTE)(srcSize>>8);
|
2015-07-25 23:23:57 +00:00
|
|
|
ostart[2] = (BYTE) srcSize;
|
2015-10-19 18:25:44 +00:00
|
|
|
ostart[0] += (BYTE)(bt_raw<<6); /* is a raw (uncompressed) block */
|
2015-01-24 00:58:16 +00:00
|
|
|
|
2015-10-19 18:25:44 +00:00
|
|
|
return ZSTD_blockHeaderSize+srcSize;
|
2015-01-24 00:58:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-10-19 18:25:44 +00:00
|
|
|
static size_t ZSTD_compressRawLiteralsBlock (void* dst, size_t maxDstSize, const void* src, size_t srcSize)
|
2015-01-24 00:58:16 +00:00
|
|
|
{
|
2015-01-26 09:24:04 +00:00
|
|
|
BYTE* const ostart = (BYTE* const)dst;
|
2015-01-24 00:58:16 +00:00
|
|
|
|
2015-10-19 18:25:44 +00:00
|
|
|
if (srcSize + 3 > maxDstSize) return ERROR(dstSize_tooSmall);
|
2015-01-24 00:58:16 +00:00
|
|
|
|
2015-10-19 18:25:44 +00:00
|
|
|
MEM_writeLE32(dst, ((U32)srcSize << 2) | IS_RAW);
|
|
|
|
memcpy(ostart + 3, src, srcSize);
|
|
|
|
return srcSize + 3;
|
2015-01-24 00:58:16 +00:00
|
|
|
}
|
|
|
|
|
2015-10-19 18:25:44 +00:00
|
|
|
static size_t ZSTD_compressRleLiteralsBlock (void* dst, size_t maxDstSize, const void* src, size_t srcSize)
|
|
|
|
{
|
|
|
|
BYTE* const ostart = (BYTE* const)dst;
|
2015-01-24 00:58:16 +00:00
|
|
|
|
2015-10-19 18:25:44 +00:00
|
|
|
(void)maxDstSize;
|
|
|
|
MEM_writeLE32(dst, ((U32)srcSize << 2) | IS_RLE); /* note : maxDstSize > litHeaderSize > 4 */
|
|
|
|
ostart[3] = *(const BYTE*)src;
|
|
|
|
return 4;
|
|
|
|
}
|
2015-01-24 00:58:16 +00:00
|
|
|
|
2015-10-19 18:25:44 +00:00
|
|
|
size_t ZSTD_minGain(size_t srcSize) { return (srcSize >> 6) + 1; }
|
2015-01-24 00:58:16 +00:00
|
|
|
|
2015-10-19 18:25:44 +00:00
|
|
|
static size_t ZSTD_compressLiterals (void* dst, size_t maxDstSize,
|
2015-10-22 14:31:46 +00:00
|
|
|
const void* src, size_t srcSize)
|
2015-01-24 00:58:16 +00:00
|
|
|
{
|
2015-07-25 23:23:57 +00:00
|
|
|
const size_t minGain = ZSTD_minGain(srcSize);
|
|
|
|
BYTE* const ostart = (BYTE*)dst;
|
2015-08-11 13:18:45 +00:00
|
|
|
size_t hsize;
|
2015-10-19 18:25:44 +00:00
|
|
|
static const size_t litHeaderSize = 5;
|
2015-08-11 13:18:45 +00:00
|
|
|
|
2015-10-19 18:25:44 +00:00
|
|
|
if (maxDstSize < litHeaderSize+1) return ERROR(dstSize_tooSmall); /* not enough space for compression */
|
2015-08-11 13:18:45 +00:00
|
|
|
|
2015-10-19 18:25:44 +00:00
|
|
|
hsize = HUF_compress(ostart+litHeaderSize, maxDstSize-litHeaderSize, src, srcSize);
|
2015-07-25 23:23:57 +00:00
|
|
|
|
2015-10-19 18:25:44 +00:00
|
|
|
if ((hsize==0) || (hsize >= srcSize - minGain)) return ZSTD_compressRawLiteralsBlock(dst, maxDstSize, src, srcSize);
|
|
|
|
if (hsize==1) return ZSTD_compressRleLiteralsBlock(dst, maxDstSize, src, srcSize);
|
2015-07-25 23:23:57 +00:00
|
|
|
|
|
|
|
/* Build header */
|
|
|
|
{
|
2015-10-19 18:25:44 +00:00
|
|
|
ostart[0] = (BYTE)(srcSize << 2); /* is a block, is compressed */
|
|
|
|
ostart[1] = (BYTE)(srcSize >> 6);
|
|
|
|
ostart[2] = (BYTE)(srcSize >>14);
|
|
|
|
ostart[2] += (BYTE)(hsize << 5);
|
|
|
|
ostart[3] = (BYTE)(hsize >> 3);
|
|
|
|
ostart[4] = (BYTE)(hsize >>11);
|
2015-07-25 23:23:57 +00:00
|
|
|
}
|
|
|
|
|
2015-10-19 18:25:44 +00:00
|
|
|
return hsize+litHeaderSize;
|
2015-01-24 00:58:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-10-22 14:31:46 +00:00
|
|
|
size_t ZSTD_compressSequences(BYTE* dst, size_t maxDstSize,
|
|
|
|
const seqStore_t* seqStorePtr,
|
|
|
|
size_t srcSize)
|
2015-01-24 00:58:16 +00:00
|
|
|
{
|
2015-08-11 13:18:45 +00:00
|
|
|
U32 count[MaxSeq+1];
|
|
|
|
S16 norm[MaxSeq+1];
|
2015-01-24 00:58:16 +00:00
|
|
|
size_t mostFrequent;
|
|
|
|
U32 max = 255;
|
|
|
|
U32 tableLog = 11;
|
|
|
|
U32 CTable_LitLength [FSE_CTABLE_SIZE_U32(LLFSELog, MaxLL )];
|
2015-02-09 02:50:11 +00:00
|
|
|
U32 CTable_OffsetBits [FSE_CTABLE_SIZE_U32(OffFSELog,MaxOff)];
|
2015-01-24 00:58:16 +00:00
|
|
|
U32 CTable_MatchLength[FSE_CTABLE_SIZE_U32(MLFSELog, MaxML )];
|
2015-08-08 01:16:11 +00:00
|
|
|
U32 LLtype, Offtype, MLtype; /* compressed, raw or rle */
|
2015-02-09 02:50:11 +00:00
|
|
|
const BYTE* const op_lit_start = seqStorePtr->litStart;
|
2015-08-08 01:16:11 +00:00
|
|
|
const BYTE* const llTable = seqStorePtr->litLengthStart;
|
2015-10-19 18:25:44 +00:00
|
|
|
const BYTE* const llPtr = seqStorePtr->litLength;
|
2015-08-08 01:16:11 +00:00
|
|
|
const BYTE* const mlTable = seqStorePtr->matchLengthStart;
|
|
|
|
const U32* const offsetTable = seqStorePtr->offsetStart;
|
|
|
|
BYTE* const offCodeTable = seqStorePtr->offCodeStart;
|
2015-08-07 14:21:00 +00:00
|
|
|
BYTE* op = dst;
|
|
|
|
BYTE* const oend = dst + maxDstSize;
|
2015-10-19 18:25:44 +00:00
|
|
|
const size_t nbSeq = llPtr - llTable;
|
2015-01-24 00:58:16 +00:00
|
|
|
const size_t minGain = ZSTD_minGain(srcSize);
|
|
|
|
const size_t maxCSize = srcSize - minGain;
|
|
|
|
BYTE* seqHead;
|
|
|
|
|
|
|
|
|
2015-08-07 14:21:00 +00:00
|
|
|
/* Compress literals */
|
2015-01-24 00:58:16 +00:00
|
|
|
{
|
|
|
|
size_t cSize;
|
2015-10-22 14:31:46 +00:00
|
|
|
size_t litSize = seqStorePtr->lit - op_lit_start;
|
2015-07-25 23:23:57 +00:00
|
|
|
|
2015-10-19 18:25:44 +00:00
|
|
|
if (litSize <= LITERAL_NOENTROPY)
|
|
|
|
cSize = ZSTD_compressRawLiteralsBlock(op, maxDstSize, op_lit_start, litSize);
|
2015-01-24 00:58:16 +00:00
|
|
|
else
|
|
|
|
cSize = ZSTD_compressLiterals(op, maxDstSize, op_lit_start, litSize);
|
|
|
|
if (ZSTD_isError(cSize)) return cSize;
|
|
|
|
op += cSize;
|
|
|
|
}
|
|
|
|
|
2015-08-08 01:16:11 +00:00
|
|
|
/* Sequences Header */
|
2015-10-19 18:25:44 +00:00
|
|
|
if ((oend-op) < MIN_SEQUENCES_SIZE)
|
2015-10-18 21:18:32 +00:00
|
|
|
return ERROR(dstSize_tooSmall);
|
|
|
|
MEM_writeLE16(op, (U16)nbSeq); op+=2;
|
2015-01-24 00:58:16 +00:00
|
|
|
seqHead = op;
|
|
|
|
|
2015-08-08 01:16:11 +00:00
|
|
|
/* dumps : contains too large lengths */
|
2015-01-24 00:58:16 +00:00
|
|
|
{
|
2015-02-09 02:50:11 +00:00
|
|
|
size_t dumpsLength = seqStorePtr->dumps - seqStorePtr->dumpsStart;
|
2015-01-24 00:58:16 +00:00
|
|
|
if (dumpsLength < 512)
|
|
|
|
{
|
|
|
|
op[0] = (BYTE)(dumpsLength >> 8);
|
|
|
|
op[1] = (BYTE)(dumpsLength);
|
|
|
|
op += 2;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
op[0] = 2;
|
|
|
|
op[1] = (BYTE)(dumpsLength>>8);
|
|
|
|
op[2] = (BYTE)(dumpsLength);
|
|
|
|
op += 3;
|
|
|
|
}
|
2015-10-18 21:18:32 +00:00
|
|
|
if ((size_t)(oend-op) < dumpsLength+6) return ERROR(dstSize_tooSmall);
|
2015-02-09 02:50:11 +00:00
|
|
|
memcpy(op, seqStorePtr->dumpsStart, dumpsLength);
|
2015-01-24 00:58:16 +00:00
|
|
|
op += dumpsLength;
|
|
|
|
}
|
|
|
|
|
2015-08-07 14:21:00 +00:00
|
|
|
/* CTable for Literal Lengths */
|
2015-01-24 00:58:16 +00:00
|
|
|
max = MaxLL;
|
2015-07-04 23:56:41 +00:00
|
|
|
mostFrequent = FSE_countFast(count, &max, seqStorePtr->litLengthStart, nbSeq);
|
2015-07-07 07:45:35 +00:00
|
|
|
if ((mostFrequent == nbSeq) && (nbSeq > 2))
|
2015-01-24 00:58:16 +00:00
|
|
|
{
|
2015-02-09 02:50:11 +00:00
|
|
|
*op++ = *(seqStorePtr->litLengthStart);
|
2015-01-24 00:58:16 +00:00
|
|
|
FSE_buildCTable_rle(CTable_LitLength, (BYTE)max);
|
|
|
|
LLtype = bt_rle;
|
|
|
|
}
|
|
|
|
else if ((nbSeq < 64) || (mostFrequent < (nbSeq >> (LLbits-1))))
|
|
|
|
{
|
|
|
|
FSE_buildCTable_raw(CTable_LitLength, LLbits);
|
|
|
|
LLtype = bt_raw;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2015-09-10 22:26:09 +00:00
|
|
|
size_t NCountSize;
|
2015-01-24 00:58:16 +00:00
|
|
|
tableLog = FSE_optimalTableLog(LLFSELog, nbSeq, max);
|
|
|
|
FSE_normalizeCount(norm, tableLog, count, nbSeq, max);
|
2015-09-10 22:26:09 +00:00
|
|
|
NCountSize = FSE_writeNCount(op, oend-op, norm, max, tableLog); /* overflow protected */
|
2015-10-18 21:18:32 +00:00
|
|
|
if (FSE_isError(NCountSize)) return ERROR(GENERIC);
|
2015-08-11 13:18:45 +00:00
|
|
|
op += NCountSize;
|
2015-01-24 00:58:16 +00:00
|
|
|
FSE_buildCTable(CTable_LitLength, norm, max, tableLog);
|
|
|
|
LLtype = bt_compressed;
|
|
|
|
}
|
|
|
|
|
2015-08-07 14:21:00 +00:00
|
|
|
/* CTable for Offsets codes */
|
2015-01-24 00:58:16 +00:00
|
|
|
{
|
2015-08-07 14:21:00 +00:00
|
|
|
/* create Offset codes */
|
2015-01-24 00:58:16 +00:00
|
|
|
size_t i;
|
|
|
|
max = MaxOff;
|
|
|
|
for (i=0; i<nbSeq; i++)
|
|
|
|
{
|
2015-08-08 01:16:11 +00:00
|
|
|
offCodeTable[i] = (BYTE)ZSTD_highbit(offsetTable[i]) + 1;
|
|
|
|
if (offsetTable[i]==0) offCodeTable[i]=0;
|
2015-01-24 00:58:16 +00:00
|
|
|
}
|
2015-08-08 01:16:11 +00:00
|
|
|
mostFrequent = FSE_countFast(count, &max, offCodeTable, nbSeq);
|
2015-01-24 00:58:16 +00:00
|
|
|
}
|
2015-07-07 07:45:35 +00:00
|
|
|
if ((mostFrequent == nbSeq) && (nbSeq > 2))
|
2015-01-24 00:58:16 +00:00
|
|
|
{
|
2015-08-08 01:16:11 +00:00
|
|
|
*op++ = *offCodeTable;
|
2015-01-24 00:58:16 +00:00
|
|
|
FSE_buildCTable_rle(CTable_OffsetBits, (BYTE)max);
|
|
|
|
Offtype = bt_rle;
|
|
|
|
}
|
|
|
|
else if ((nbSeq < 64) || (mostFrequent < (nbSeq >> (Offbits-1))))
|
|
|
|
{
|
|
|
|
FSE_buildCTable_raw(CTable_OffsetBits, Offbits);
|
|
|
|
Offtype = bt_raw;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2015-09-10 22:26:09 +00:00
|
|
|
size_t NCountSize;
|
2015-01-24 00:58:16 +00:00
|
|
|
tableLog = FSE_optimalTableLog(OffFSELog, nbSeq, max);
|
|
|
|
FSE_normalizeCount(norm, tableLog, count, nbSeq, max);
|
2015-09-10 22:26:09 +00:00
|
|
|
NCountSize = FSE_writeNCount(op, oend-op, norm, max, tableLog); /* overflow protected */
|
2015-10-18 21:18:32 +00:00
|
|
|
if (FSE_isError(NCountSize)) return ERROR(GENERIC);
|
2015-08-11 13:18:45 +00:00
|
|
|
op += NCountSize;
|
2015-01-24 00:58:16 +00:00
|
|
|
FSE_buildCTable(CTable_OffsetBits, norm, max, tableLog);
|
|
|
|
Offtype = bt_compressed;
|
|
|
|
}
|
|
|
|
|
2015-08-07 14:21:00 +00:00
|
|
|
/* CTable for MatchLengths */
|
2015-01-24 00:58:16 +00:00
|
|
|
max = MaxML;
|
2015-07-04 23:56:41 +00:00
|
|
|
mostFrequent = FSE_countFast(count, &max, seqStorePtr->matchLengthStart, nbSeq);
|
2015-07-07 07:45:35 +00:00
|
|
|
if ((mostFrequent == nbSeq) && (nbSeq > 2))
|
2015-01-24 00:58:16 +00:00
|
|
|
{
|
2015-02-09 02:50:11 +00:00
|
|
|
*op++ = *seqStorePtr->matchLengthStart;
|
2015-01-24 00:58:16 +00:00
|
|
|
FSE_buildCTable_rle(CTable_MatchLength, (BYTE)max);
|
|
|
|
MLtype = bt_rle;
|
|
|
|
}
|
|
|
|
else if ((nbSeq < 64) || (mostFrequent < (nbSeq >> (MLbits-1))))
|
|
|
|
{
|
|
|
|
FSE_buildCTable_raw(CTable_MatchLength, MLbits);
|
|
|
|
MLtype = bt_raw;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2015-09-10 22:26:09 +00:00
|
|
|
size_t NCountSize;
|
2015-01-24 00:58:16 +00:00
|
|
|
tableLog = FSE_optimalTableLog(MLFSELog, nbSeq, max);
|
|
|
|
FSE_normalizeCount(norm, tableLog, count, nbSeq, max);
|
2015-09-10 22:26:09 +00:00
|
|
|
NCountSize = FSE_writeNCount(op, oend-op, norm, max, tableLog); /* overflow protected */
|
2015-10-18 21:18:32 +00:00
|
|
|
if (FSE_isError(NCountSize)) return ERROR(GENERIC);
|
2015-08-11 13:18:45 +00:00
|
|
|
op += NCountSize;
|
2015-01-24 00:58:16 +00:00
|
|
|
FSE_buildCTable(CTable_MatchLength, norm, max, tableLog);
|
|
|
|
MLtype = bt_compressed;
|
|
|
|
}
|
|
|
|
|
|
|
|
seqHead[0] += (BYTE)((LLtype<<6) + (Offtype<<4) + (MLtype<<2));
|
|
|
|
|
2015-08-07 14:21:00 +00:00
|
|
|
/* Encoding Sequences */
|
2015-01-24 00:58:16 +00:00
|
|
|
{
|
2015-08-07 14:21:00 +00:00
|
|
|
size_t streamSize, errorCode;
|
2015-10-18 21:18:32 +00:00
|
|
|
BIT_CStream_t blockStream;
|
2015-01-24 00:58:16 +00:00
|
|
|
FSE_CState_t stateMatchLength;
|
|
|
|
FSE_CState_t stateOffsetBits;
|
|
|
|
FSE_CState_t stateLitLength;
|
2015-08-08 01:16:11 +00:00
|
|
|
int i;
|
2015-01-24 00:58:16 +00:00
|
|
|
|
2015-10-18 21:18:32 +00:00
|
|
|
errorCode = BIT_initCStream(&blockStream, op, oend-op);
|
|
|
|
if (ERR_isError(errorCode)) return ERROR(dstSize_tooSmall); /* not enough space remaining */
|
2015-01-24 00:58:16 +00:00
|
|
|
FSE_initCState(&stateMatchLength, CTable_MatchLength);
|
|
|
|
FSE_initCState(&stateOffsetBits, CTable_OffsetBits);
|
|
|
|
FSE_initCState(&stateLitLength, CTable_LitLength);
|
|
|
|
|
2015-08-11 13:18:45 +00:00
|
|
|
for (i=(int)nbSeq-1; i>=0; i--)
|
2015-01-24 00:58:16 +00:00
|
|
|
{
|
2015-08-08 01:16:11 +00:00
|
|
|
BYTE matchLength = mlTable[i];
|
|
|
|
U32 offset = offsetTable[i];
|
|
|
|
BYTE offCode = offCodeTable[i]; /* 32b*/ /* 64b*/
|
2015-01-24 00:58:16 +00:00
|
|
|
U32 nbBits = (offCode-1) * (!!offCode);
|
2015-08-08 01:16:11 +00:00
|
|
|
BYTE litLength = llTable[i]; /* (7)*/ /* (7)*/
|
2015-07-04 23:56:41 +00:00
|
|
|
FSE_encodeSymbol(&blockStream, &stateMatchLength, matchLength); /* 17 */ /* 17 */
|
2015-10-18 21:18:32 +00:00
|
|
|
if (MEM_32bits()) BIT_flushBits(&blockStream); /* 7 */
|
|
|
|
BIT_addBits(&blockStream, offset, nbBits); /* 32 */ /* 42 */
|
|
|
|
if (MEM_32bits()) BIT_flushBits(&blockStream); /* 7 */
|
2015-07-04 23:56:41 +00:00
|
|
|
FSE_encodeSymbol(&blockStream, &stateOffsetBits, offCode); /* 16 */ /* 51 */
|
|
|
|
FSE_encodeSymbol(&blockStream, &stateLitLength, litLength); /* 26 */ /* 61 */
|
2015-10-18 21:18:32 +00:00
|
|
|
BIT_flushBits(&blockStream); /* 7 */ /* 7 */
|
2015-01-24 00:58:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
FSE_flushCState(&blockStream, &stateMatchLength);
|
|
|
|
FSE_flushCState(&blockStream, &stateOffsetBits);
|
|
|
|
FSE_flushCState(&blockStream, &stateLitLength);
|
|
|
|
|
2015-10-18 21:18:32 +00:00
|
|
|
streamSize = BIT_closeCStream(&blockStream);
|
|
|
|
if (streamSize==0) return ERROR(dstSize_tooSmall); /* not enough space */
|
2015-08-07 14:21:00 +00:00
|
|
|
op += streamSize;
|
|
|
|
}
|
2015-01-24 00:58:16 +00:00
|
|
|
|
|
|
|
/* check compressibility */
|
|
|
|
if ((size_t)(op-dst) >= maxCSize) return 0;
|
|
|
|
|
|
|
|
return op - dst;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-02-01 10:57:30 +00:00
|
|
|
//static const U32 hashMask = (1<<HASH_LOG)-1;
|
2015-01-24 00:58:16 +00:00
|
|
|
|
|
|
|
//static U32 ZSTD_hashPtr(const void* p) { return (U32) _bextr_u64(*(U64*)p * prime7bytes, (56-HASH_LOG), HASH_LOG); }
|
|
|
|
//static U32 ZSTD_hashPtr(const void* p) { return ( (*(U64*)p * prime7bytes) << 8 >> (64-HASH_LOG)); }
|
|
|
|
//static U32 ZSTD_hashPtr(const void* p) { return ( (*(U64*)p * prime7bytes) >> (56-HASH_LOG)) & ((1<<HASH_LOG)-1); }
|
|
|
|
//static U32 ZSTD_hashPtr(const void* p) { return ( ((*(U64*)p & 0xFFFFFFFFFFFFFF) * prime7bytes) >> (64-HASH_LOG)); }
|
|
|
|
|
2015-10-29 15:49:43 +00:00
|
|
|
//static const U64 prime8bytes = 14923729446516375013ULL;
|
2015-01-24 00:58:16 +00:00
|
|
|
//static U32 ZSTD_hashPtr(const void* p) { return ( (*(U64*)p * prime8bytes) >> (64-HASH_LOG)); }
|
2015-10-29 15:49:43 +00:00
|
|
|
|
|
|
|
static const U64 prime7bytes = 58295818150454627ULL;
|
2015-10-18 21:18:32 +00:00
|
|
|
static U32 ZSTD_hashPtr(const void* p) { return ( (MEM_read64(p) * prime7bytes) >> (56-HASH_LOG)) & HASH_MASK; }
|
2015-10-29 15:49:43 +00:00
|
|
|
|
|
|
|
//static const U64 prime6bytes = 227718039650203ULL;
|
|
|
|
//static U32 ZSTD_hashPtr(const void* p) { return ( (MEM_read64(p) * prime6bytes) >> (48-HASH_LOG)) & HASH_MASK; }
|
|
|
|
|
|
|
|
//static const U64 prime5bytes = 889523592379ULL;
|
2015-01-24 00:58:16 +00:00
|
|
|
//static U32 ZSTD_hashPtr(const void* p) { return ( (*(U64*)p * prime5bytes) >> (40-HASH_LOG)) & HASH_MASK; }
|
2015-10-29 15:49:43 +00:00
|
|
|
|
2015-01-24 00:58:16 +00:00
|
|
|
//static U32 ZSTD_hashPtr(const void* p) { return ( (*(U32*)p * KNUTH) >> (32-HASH_LOG)); }
|
|
|
|
|
|
|
|
static void ZSTD_addPtr(U32* table, const BYTE* p, const BYTE* start) { table[ZSTD_hashPtr(p)] = (U32)(p-start); }
|
|
|
|
|
|
|
|
static const BYTE* ZSTD_updateMatch(U32* table, const BYTE* p, const BYTE* start)
|
|
|
|
{
|
|
|
|
U32 h = ZSTD_hashPtr(p);
|
|
|
|
const BYTE* r;
|
|
|
|
r = table[h] + start;
|
2015-10-30 05:40:22 +00:00
|
|
|
table[h] = (U32)(p-start);
|
2015-01-24 00:58:16 +00:00
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int ZSTD_checkMatch(const BYTE* match, const BYTE* ip)
|
|
|
|
{
|
2015-10-18 21:18:32 +00:00
|
|
|
return MEM_read32(match) == MEM_read32(ip);
|
2015-01-24 00:58:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-10-21 13:39:26 +00:00
|
|
|
static size_t ZSTD_compressBlock(ZSTD_CCtx* ctx, void* dst, size_t maxDstSize, const void* src, size_t srcSize)
|
2015-01-24 00:58:16 +00:00
|
|
|
{
|
2015-02-09 02:50:11 +00:00
|
|
|
U32* HashTable = (U32*)(ctx->hashTable);
|
|
|
|
seqStore_t* seqStorePtr = &(ctx->seqStore);
|
|
|
|
const BYTE* const base = ctx->base;
|
2015-01-24 00:58:16 +00:00
|
|
|
|
|
|
|
const BYTE* const istart = (const BYTE*)src;
|
|
|
|
const BYTE* ip = istart + 1;
|
|
|
|
const BYTE* anchor = istart;
|
|
|
|
const BYTE* const iend = istart + srcSize;
|
2015-10-22 14:31:46 +00:00
|
|
|
const BYTE* const ilimit = iend - 8;
|
2015-01-24 00:58:16 +00:00
|
|
|
|
2015-10-29 15:49:43 +00:00
|
|
|
size_t offset_2=4, offset_1=4;
|
2015-01-24 00:58:16 +00:00
|
|
|
|
|
|
|
|
2015-02-09 02:50:11 +00:00
|
|
|
/* init */
|
2015-10-30 05:40:22 +00:00
|
|
|
if (ip-base < 4)
|
|
|
|
{
|
|
|
|
ZSTD_addPtr(HashTable, ip+0, base);
|
|
|
|
ZSTD_addPtr(HashTable, ip+1, base);
|
|
|
|
ZSTD_addPtr(HashTable, ip+2, base);
|
|
|
|
ZSTD_addPtr(HashTable, ip+3, base);
|
|
|
|
ip += 4;
|
|
|
|
}
|
2015-02-09 02:50:11 +00:00
|
|
|
ZSTD_resetSeqStore(seqStorePtr);
|
|
|
|
|
2015-01-24 00:58:16 +00:00
|
|
|
/* Main Search Loop */
|
2015-10-30 05:40:22 +00:00
|
|
|
while (ip <= ilimit)
|
2015-01-24 00:58:16 +00:00
|
|
|
{
|
2015-10-30 05:40:22 +00:00
|
|
|
const BYTE* match = ZSTD_updateMatch(HashTable, ip, base);
|
2015-01-24 00:58:16 +00:00
|
|
|
|
2015-10-30 05:40:22 +00:00
|
|
|
if (ZSTD_checkMatch(ip-offset_2,ip)) match = ip-offset_2;
|
|
|
|
if (!ZSTD_checkMatch(match,ip)) { ip += ((ip-anchor) >> g_searchStrength) + 1; offset_2 = offset_1; continue; }
|
|
|
|
while ((ip>anchor) && (match>base) && (ip[-1] == match[-1])) { ip--; match--; } /* catch up */
|
2015-01-24 00:58:16 +00:00
|
|
|
|
|
|
|
{
|
|
|
|
size_t litLength = ip-anchor;
|
|
|
|
size_t matchLength = ZSTD_count(ip+MINMATCH, match+MINMATCH, iend);
|
2015-10-30 05:40:22 +00:00
|
|
|
size_t offsetCode = ip-match;
|
2015-10-29 15:49:43 +00:00
|
|
|
if (offsetCode == offset_2) offsetCode = 0;
|
|
|
|
offset_2 = offset_1;
|
|
|
|
offset_1 = ip-match;
|
2015-02-09 02:50:11 +00:00
|
|
|
ZSTD_storeSeq(seqStorePtr, litLength, anchor, offsetCode, matchLength);
|
2015-01-24 00:58:16 +00:00
|
|
|
|
|
|
|
/* Fill Table */
|
|
|
|
ZSTD_addPtr(HashTable, ip+1, base);
|
|
|
|
ip += matchLength + MINMATCH;
|
|
|
|
anchor = ip;
|
2015-10-30 05:40:22 +00:00
|
|
|
if (ip <= ilimit) ZSTD_addPtr(HashTable, ip-2, base);
|
2015-01-24 00:58:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Last Literals */
|
2015-08-11 13:18:45 +00:00
|
|
|
{
|
|
|
|
size_t lastLLSize = iend - anchor;
|
|
|
|
memcpy(seqStorePtr->lit, anchor, lastLLSize);
|
|
|
|
seqStorePtr->lit += lastLLSize;
|
|
|
|
}
|
2015-01-24 00:58:16 +00:00
|
|
|
|
|
|
|
/* Finale compression stage */
|
2015-02-09 02:50:11 +00:00
|
|
|
return ZSTD_compressSequences((BYTE*)dst, maxDstSize,
|
2015-08-11 13:18:45 +00:00
|
|
|
seqStorePtr, srcSize);
|
2015-01-24 00:58:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-10-21 13:39:26 +00:00
|
|
|
size_t ZSTD_compressBegin(ZSTD_CCtx* ctx, void* dst, size_t maxDstSize)
|
2015-01-31 14:09:51 +00:00
|
|
|
{
|
|
|
|
/* Sanity check */
|
2015-10-18 21:18:32 +00:00
|
|
|
if (maxDstSize < ZSTD_frameHeaderSize) return ERROR(dstSize_tooSmall);
|
2015-01-31 14:09:51 +00:00
|
|
|
|
|
|
|
/* Init */
|
|
|
|
ZSTD_resetCCtx(ctx);
|
|
|
|
|
|
|
|
/* Write Header */
|
2015-10-18 21:26:26 +00:00
|
|
|
MEM_writeLE32(dst, ZSTD_magicNumber);
|
2015-01-31 14:09:51 +00:00
|
|
|
|
|
|
|
return ZSTD_frameHeaderSize;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-10-21 13:39:26 +00:00
|
|
|
static void ZSTD_scaleDownCtx(ZSTD_CCtx* ctx, const U32 limit)
|
2015-01-24 00:58:16 +00:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
2015-07-08 00:54:25 +00:00
|
|
|
#if defined(__AVX2__)
|
2015-01-24 00:58:16 +00:00
|
|
|
/* AVX2 version */
|
2015-02-01 10:57:30 +00:00
|
|
|
__m256i* h = ctx->hashTable;
|
2015-01-24 00:58:16 +00:00
|
|
|
const __m256i limit8 = _mm256_set1_epi32(limit);
|
2015-02-01 10:57:30 +00:00
|
|
|
for (i=0; i<(HASH_TABLESIZE>>3); i++)
|
2015-01-24 00:58:16 +00:00
|
|
|
{
|
|
|
|
__m256i src =_mm256_loadu_si256((const __m256i*)(h+i));
|
2015-01-31 14:09:51 +00:00
|
|
|
const __m256i dec = _mm256_min_epu32(src, limit8);
|
|
|
|
src = _mm256_sub_epi32(src, dec);
|
2015-01-24 00:58:16 +00:00
|
|
|
_mm256_storeu_si256((__m256i*)(h+i), src);
|
|
|
|
}
|
|
|
|
#else
|
2015-07-08 00:54:25 +00:00
|
|
|
/* this should be auto-vectorized by compiler */
|
2015-02-01 10:57:30 +00:00
|
|
|
U32* h = ctx->hashTable;
|
2015-01-24 00:58:16 +00:00
|
|
|
for (i=0; i<HASH_TABLESIZE; ++i)
|
|
|
|
{
|
2015-01-31 14:09:51 +00:00
|
|
|
U32 dec;
|
|
|
|
if (h[i] > limit) dec = limit; else dec = h[i];
|
|
|
|
h[i] -= dec;
|
2015-01-24 00:58:16 +00:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-10-21 13:39:26 +00:00
|
|
|
static void ZSTD_limitCtx(ZSTD_CCtx* ctx, const U32 limit)
|
2015-01-24 00:58:16 +00:00
|
|
|
{
|
2015-01-31 14:09:51 +00:00
|
|
|
int i;
|
2015-01-24 00:58:16 +00:00
|
|
|
|
2015-01-31 14:09:51 +00:00
|
|
|
if (limit > g_maxLimit)
|
|
|
|
{
|
2015-10-21 08:07:25 +00:00
|
|
|
ZSTD_scaleDownCtx(ctx, limit);
|
2015-01-31 14:09:51 +00:00
|
|
|
ctx->base += limit;
|
|
|
|
ctx->current -= limit;
|
|
|
|
ctx->nextUpdate -= limit;
|
|
|
|
return;
|
|
|
|
}
|
2015-01-24 00:58:16 +00:00
|
|
|
|
2015-07-08 00:54:25 +00:00
|
|
|
#if defined(__AVX2__)
|
2015-01-31 14:09:51 +00:00
|
|
|
/* AVX2 version */
|
|
|
|
{
|
2015-02-01 10:57:30 +00:00
|
|
|
__m256i* h = ctx->hashTable;
|
2015-01-31 19:10:53 +00:00
|
|
|
const __m256i limit8 = _mm256_set1_epi32(limit);
|
2015-02-01 10:57:30 +00:00
|
|
|
//printf("Address h : %0X\n", (U32)h); // address test
|
|
|
|
for (i=0; i<(HASH_TABLESIZE>>3); i++)
|
2015-01-31 19:10:53 +00:00
|
|
|
{
|
2015-02-01 10:57:30 +00:00
|
|
|
__m256i src =_mm256_loadu_si256((const __m256i*)(h+i)); // Unfortunately, clang doesn't guarantee 32-bytes alignment
|
2015-01-31 19:10:53 +00:00
|
|
|
src = _mm256_max_epu32(src, limit8);
|
|
|
|
_mm256_storeu_si256((__m256i*)(h+i), src);
|
|
|
|
}
|
2015-01-31 14:09:51 +00:00
|
|
|
}
|
|
|
|
#else
|
2015-07-08 00:54:25 +00:00
|
|
|
/* this should be auto-vectorized by compiler */
|
2015-01-31 14:09:51 +00:00
|
|
|
{
|
2015-02-01 10:57:30 +00:00
|
|
|
U32* h = (U32*)(ctx->hashTable);
|
|
|
|
for (i=0; i<HASH_TABLESIZE; ++i)
|
|
|
|
{
|
|
|
|
if (h[i] < limit) h[i] = limit;
|
|
|
|
}
|
2015-01-31 14:09:51 +00:00
|
|
|
}
|
|
|
|
#endif
|
2015-01-24 00:58:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-10-21 13:39:26 +00:00
|
|
|
size_t ZSTD_compressContinue(ZSTD_CCtx* ctx, void* dst, size_t maxDstSize, const void* src, size_t srcSize)
|
2015-01-24 00:58:16 +00:00
|
|
|
{
|
2015-01-26 09:24:04 +00:00
|
|
|
const BYTE* const istart = (const BYTE* const)src;
|
2015-01-24 00:58:16 +00:00
|
|
|
const BYTE* ip = istart;
|
2015-01-26 09:24:04 +00:00
|
|
|
BYTE* const ostart = (BYTE* const)dst;
|
2015-01-24 00:58:16 +00:00
|
|
|
BYTE* op = ostart;
|
2015-01-31 14:09:51 +00:00
|
|
|
const U32 updateRate = 2 * BLOCKSIZE;
|
2015-01-24 00:58:16 +00:00
|
|
|
|
2015-01-24 12:31:55 +00:00
|
|
|
/* Init */
|
2015-01-31 14:09:51 +00:00
|
|
|
if (ctx->base==NULL)
|
|
|
|
ctx->base = (const BYTE*)src, ctx->current=0, ctx->nextUpdate = g_maxDistance;
|
2015-01-24 12:31:55 +00:00
|
|
|
if (src != ctx->base + ctx->current) /* not contiguous */
|
2015-01-24 00:58:16 +00:00
|
|
|
{
|
2015-08-11 13:18:45 +00:00
|
|
|
ZSTD_resetCCtx(ctx);
|
|
|
|
ctx->base = (const BYTE*)src;
|
|
|
|
ctx->current = 0;
|
2015-01-24 00:58:16 +00:00
|
|
|
}
|
|
|
|
ctx->current += (U32)srcSize;
|
|
|
|
|
|
|
|
while (srcSize)
|
|
|
|
{
|
|
|
|
size_t cSize;
|
|
|
|
size_t blockSize = BLOCKSIZE;
|
|
|
|
if (blockSize > srcSize) blockSize = srcSize;
|
|
|
|
|
2015-09-10 22:26:09 +00:00
|
|
|
if (maxDstSize < 2*ZSTD_blockHeaderSize+1) /* one RLE block + endMark */
|
2015-10-18 21:18:32 +00:00
|
|
|
return ERROR(dstSize_tooSmall);
|
2015-08-11 13:18:45 +00:00
|
|
|
|
2015-01-31 14:09:51 +00:00
|
|
|
/* update hash table */
|
2015-08-11 13:18:45 +00:00
|
|
|
if (g_maxDistance <= BLOCKSIZE) /* static test ; yes == blocks are independent */
|
2015-01-24 00:58:16 +00:00
|
|
|
{
|
|
|
|
ZSTD_resetCCtx(ctx);
|
|
|
|
ctx->base = ip;
|
|
|
|
ctx->current=0;
|
|
|
|
}
|
2015-01-31 14:09:51 +00:00
|
|
|
else if (ip >= ctx->base + ctx->nextUpdate)
|
2015-01-24 00:58:16 +00:00
|
|
|
{
|
2015-01-31 14:09:51 +00:00
|
|
|
ctx->nextUpdate += updateRate;
|
|
|
|
ZSTD_limitCtx(ctx, ctx->nextUpdate - g_maxDistance);
|
2015-01-24 00:58:16 +00:00
|
|
|
}
|
|
|
|
|
2015-01-24 12:31:55 +00:00
|
|
|
/* compress */
|
2015-01-24 00:58:16 +00:00
|
|
|
cSize = ZSTD_compressBlock(ctx, op+ZSTD_blockHeaderSize, maxDstSize-ZSTD_blockHeaderSize, ip, blockSize);
|
|
|
|
if (cSize == 0)
|
|
|
|
{
|
2015-01-24 12:31:55 +00:00
|
|
|
cSize = ZSTD_noCompressBlock(op, maxDstSize, ip, blockSize); /* block is not compressible */
|
2015-01-24 00:58:16 +00:00
|
|
|
if (ZSTD_isError(cSize)) return cSize;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (ZSTD_isError(cSize)) return cSize;
|
|
|
|
op[0] = (BYTE)(cSize>>16);
|
|
|
|
op[1] = (BYTE)(cSize>>8);
|
|
|
|
op[2] = (BYTE)cSize;
|
|
|
|
op[0] += (BYTE)(bt_compressed << 6); /* is a compressed block */
|
|
|
|
cSize += 3;
|
|
|
|
}
|
|
|
|
op += cSize;
|
|
|
|
maxDstSize -= cSize;
|
|
|
|
ip += blockSize;
|
|
|
|
srcSize -= blockSize;
|
|
|
|
}
|
|
|
|
|
|
|
|
return op-ostart;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-10-21 13:39:26 +00:00
|
|
|
size_t ZSTD_compressEnd(ZSTD_CCtx* ctx, void* dst, size_t maxDstSize)
|
2015-01-24 00:58:16 +00:00
|
|
|
{
|
2015-01-26 09:24:04 +00:00
|
|
|
BYTE* op = (BYTE*)dst;
|
2015-01-24 00:58:16 +00:00
|
|
|
|
2015-01-31 14:09:51 +00:00
|
|
|
/* Sanity check */
|
2015-01-24 00:58:16 +00:00
|
|
|
(void)ctx;
|
2015-10-18 21:18:32 +00:00
|
|
|
if (maxDstSize < ZSTD_blockHeaderSize) return ERROR(dstSize_tooSmall);
|
2015-01-24 00:58:16 +00:00
|
|
|
|
2015-01-31 14:09:51 +00:00
|
|
|
/* End of frame */
|
2015-01-24 00:58:16 +00:00
|
|
|
op[0] = (BYTE)(bt_end << 6);
|
|
|
|
op[1] = 0;
|
|
|
|
op[2] = 0;
|
|
|
|
|
|
|
|
return 3;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-10-21 13:39:26 +00:00
|
|
|
size_t ZSTD_compressCCtx(ZSTD_CCtx* ctx, void* dst, size_t maxDstSize, const void* src, size_t srcSize)
|
2015-01-24 00:58:16 +00:00
|
|
|
{
|
2015-01-26 09:24:04 +00:00
|
|
|
BYTE* const ostart = (BYTE* const)dst;
|
2015-01-24 00:58:16 +00:00
|
|
|
BYTE* op = ostart;
|
2015-10-21 08:07:25 +00:00
|
|
|
size_t oSize;
|
2015-01-24 00:58:16 +00:00
|
|
|
|
2015-01-24 12:31:55 +00:00
|
|
|
/* Header */
|
2015-10-21 08:07:25 +00:00
|
|
|
oSize = ZSTD_compressBegin(ctx, dst, maxDstSize);
|
|
|
|
if(ZSTD_isError(oSize)) return oSize;
|
|
|
|
op += oSize;
|
|
|
|
maxDstSize -= oSize;
|
2015-01-24 00:58:16 +00:00
|
|
|
|
2015-01-24 12:31:55 +00:00
|
|
|
/* Compression */
|
2015-10-21 08:07:25 +00:00
|
|
|
oSize = ZSTD_compressContinue(ctx, op, maxDstSize, src, srcSize);
|
|
|
|
if (ZSTD_isError(oSize)) return oSize;
|
|
|
|
op += oSize;
|
|
|
|
maxDstSize -= oSize;
|
2015-01-24 00:58:16 +00:00
|
|
|
|
2015-01-24 12:31:55 +00:00
|
|
|
/* Close frame */
|
2015-10-21 08:07:25 +00:00
|
|
|
oSize = ZSTD_compressEnd(ctx, op, maxDstSize);
|
|
|
|
if(ZSTD_isError(oSize)) return oSize;
|
|
|
|
op += oSize;
|
2015-01-24 00:58:16 +00:00
|
|
|
|
|
|
|
return (op - ostart);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
size_t ZSTD_compress(void* dst, size_t maxDstSize, const void* src, size_t srcSize)
|
|
|
|
{
|
|
|
|
size_t r;
|
2015-10-21 08:07:25 +00:00
|
|
|
#if defined(ZSTD_HEAPMODE) && (ZSTD_HEAPMODE==1)
|
2015-10-21 13:39:26 +00:00
|
|
|
ZSTD_CCtx* ctx;
|
2015-01-24 00:58:16 +00:00
|
|
|
ctx = ZSTD_createCCtx();
|
2015-10-18 21:18:32 +00:00
|
|
|
if (ctx==NULL) return ERROR(GENERIC);
|
2015-10-21 08:07:25 +00:00
|
|
|
# else
|
2015-10-21 13:39:26 +00:00
|
|
|
ZSTD_CCtx ctxBody;
|
|
|
|
ZSTD_CCtx* const ctx = &ctxBody;
|
2015-10-21 08:07:25 +00:00
|
|
|
# endif
|
|
|
|
|
2015-01-24 00:58:16 +00:00
|
|
|
r = ZSTD_compressCCtx(ctx, dst, maxDstSize, src, srcSize);
|
2015-10-21 08:07:25 +00:00
|
|
|
|
|
|
|
#if defined(ZSTD_HEAPMODE) && (ZSTD_HEAPMODE==1)
|
2015-01-24 00:58:16 +00:00
|
|
|
ZSTD_freeCCtx(ctx);
|
2015-10-21 08:07:25 +00:00
|
|
|
#endif
|
|
|
|
|
2015-01-24 00:58:16 +00:00
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-10-19 01:14:43 +00:00
|
|
|
/* *************************************************************
|
|
|
|
* Decompression section
|
|
|
|
***************************************************************/
|
2015-10-21 13:39:26 +00:00
|
|
|
struct ZSTD_DCtx_s
|
2015-10-18 21:18:32 +00:00
|
|
|
{
|
|
|
|
U32 LLTable[FSE_DTABLE_SIZE_U32(LLFSELog)];
|
|
|
|
U32 OffTable[FSE_DTABLE_SIZE_U32(OffFSELog)];
|
|
|
|
U32 MLTable[FSE_DTABLE_SIZE_U32(MLFSELog)];
|
|
|
|
void* previousDstEnd;
|
|
|
|
void* base;
|
|
|
|
size_t expected;
|
|
|
|
blockType_t bType;
|
|
|
|
U32 phase;
|
2015-10-18 21:26:26 +00:00
|
|
|
const BYTE* litPtr;
|
|
|
|
size_t litBufSize;
|
|
|
|
size_t litSize;
|
2015-10-23 14:21:53 +00:00
|
|
|
BYTE litBuffer[BLOCKSIZE + 8 /* margin for wildcopy */];
|
2015-10-19 01:14:43 +00:00
|
|
|
}; /* typedef'd to ZSTD_Dctx within "zstd_static.h" */
|
2015-01-24 00:58:16 +00:00
|
|
|
|
2015-10-19 18:25:44 +00:00
|
|
|
|
2015-01-24 00:58:16 +00:00
|
|
|
size_t ZSTD_getcBlockSize(const void* src, size_t srcSize, blockProperties_t* bpPtr)
|
|
|
|
{
|
2015-01-26 09:24:04 +00:00
|
|
|
const BYTE* const in = (const BYTE* const)src;
|
2015-01-24 00:58:16 +00:00
|
|
|
BYTE headerFlags;
|
|
|
|
U32 cSize;
|
|
|
|
|
2015-10-18 21:18:32 +00:00
|
|
|
if (srcSize < 3) return ERROR(srcSize_wrong);
|
2015-01-24 00:58:16 +00:00
|
|
|
|
|
|
|
headerFlags = *in;
|
|
|
|
cSize = in[2] + (in[1]<<8) + ((in[0] & 7)<<16);
|
|
|
|
|
2015-01-26 09:24:04 +00:00
|
|
|
bpPtr->blockType = (blockType_t)(headerFlags >> 6);
|
2015-01-24 00:58:16 +00:00
|
|
|
bpPtr->origSize = (bpPtr->blockType == bt_rle) ? cSize : 0;
|
|
|
|
|
|
|
|
if (bpPtr->blockType == bt_end) return 0;
|
|
|
|
if (bpPtr->blockType == bt_rle) return 1;
|
|
|
|
return cSize;
|
|
|
|
}
|
|
|
|
|
|
|
|
static size_t ZSTD_copyUncompressedBlock(void* dst, size_t maxDstSize, const void* src, size_t srcSize)
|
|
|
|
{
|
2015-10-18 21:18:32 +00:00
|
|
|
if (srcSize > maxDstSize) return ERROR(dstSize_tooSmall);
|
2015-01-24 00:58:16 +00:00
|
|
|
memcpy(dst, src, srcSize);
|
|
|
|
return srcSize;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-10-19 18:25:44 +00:00
|
|
|
/** ZSTD_decompressLiterals
|
|
|
|
@return : nb of bytes read from src, or an error code*/
|
|
|
|
static size_t ZSTD_decompressLiterals(void* dst, size_t* maxDstSizePtr,
|
2015-01-24 00:58:16 +00:00
|
|
|
const void* src, size_t srcSize)
|
|
|
|
{
|
2015-07-25 23:23:57 +00:00
|
|
|
const BYTE* ip = (const BYTE*)src;
|
2015-08-24 19:17:11 +00:00
|
|
|
|
2015-10-19 18:25:44 +00:00
|
|
|
const size_t litSize = (MEM_readLE32(src) & 0x1FFFFF) >> 2; /* no buffer issue : srcSize >= MIN_CBLOCK_SIZE */
|
|
|
|
const size_t litCSize = (MEM_readLE32(ip+2) & 0xFFFFFF) >> 5; /* no buffer issue : srcSize >= MIN_CBLOCK_SIZE */
|
2015-08-24 19:17:11 +00:00
|
|
|
|
2015-10-19 18:25:44 +00:00
|
|
|
if (litSize > *maxDstSizePtr) return ERROR(corruption_detected);
|
|
|
|
if (litCSize + 5 > srcSize) return ERROR(corruption_detected);
|
2015-07-25 23:23:57 +00:00
|
|
|
|
2015-10-19 18:25:44 +00:00
|
|
|
if (HUF_isError(HUF_decompress(dst, litSize, ip+5, litCSize))) return ERROR(corruption_detected);
|
|
|
|
|
|
|
|
*maxDstSizePtr = litSize;
|
|
|
|
return litCSize + 5;
|
2015-01-24 00:58:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-10-19 18:25:44 +00:00
|
|
|
/** ZSTD_decodeLiteralsBlock
|
|
|
|
@return : nb of bytes read from src (< srcSize )*/
|
2015-01-24 00:58:16 +00:00
|
|
|
size_t ZSTD_decodeLiteralsBlock(void* ctx,
|
2015-10-23 18:25:06 +00:00
|
|
|
const void* src, size_t srcSize) /* note : srcSize < BLOCKSIZE */
|
2015-01-24 00:58:16 +00:00
|
|
|
{
|
2015-10-21 13:39:26 +00:00
|
|
|
ZSTD_DCtx* dctx = (ZSTD_DCtx*)ctx;
|
2015-10-23 18:25:06 +00:00
|
|
|
const BYTE* const istart = (const BYTE*) src;
|
2015-01-24 00:58:16 +00:00
|
|
|
|
2015-10-19 18:25:44 +00:00
|
|
|
/* any compressed block with literals segment must be at least this size */
|
|
|
|
if (srcSize < MIN_CBLOCK_SIZE) return ERROR(corruption_detected);
|
2015-01-24 00:58:16 +00:00
|
|
|
|
2015-10-19 18:25:44 +00:00
|
|
|
switch(*istart & 3)
|
2015-01-24 00:58:16 +00:00
|
|
|
{
|
2015-10-23 18:25:06 +00:00
|
|
|
/* compressed */
|
2015-10-19 18:25:44 +00:00
|
|
|
case 0:
|
2015-01-24 00:58:16 +00:00
|
|
|
{
|
2015-10-23 14:21:53 +00:00
|
|
|
size_t litSize = BLOCKSIZE;
|
|
|
|
const size_t readSize = ZSTD_decompressLiterals(dctx->litBuffer, &litSize, src, srcSize);
|
2015-10-18 21:18:32 +00:00
|
|
|
dctx->litPtr = dctx->litBuffer;
|
2015-10-23 18:25:06 +00:00
|
|
|
dctx->litBufSize = BLOCKSIZE+8;
|
2015-10-23 14:21:53 +00:00
|
|
|
dctx->litSize = litSize;
|
2015-10-19 18:25:44 +00:00
|
|
|
return readSize; /* works if it's an error too */
|
|
|
|
}
|
|
|
|
case IS_RAW:
|
|
|
|
{
|
|
|
|
const size_t litSize = (MEM_readLE32(istart) & 0xFFFFFF) >> 2; /* no buffer issue : srcSize >= MIN_CBLOCK_SIZE */
|
2015-10-23 14:21:53 +00:00
|
|
|
if (litSize > srcSize-11) /* risk of reading too far with wildcopy */
|
|
|
|
{
|
|
|
|
if (litSize > srcSize-3) return ERROR(corruption_detected);
|
|
|
|
memcpy(dctx->litBuffer, istart, litSize);
|
2015-10-23 18:25:06 +00:00
|
|
|
dctx->litBufSize = BLOCKSIZE+8;
|
2015-10-23 14:21:53 +00:00
|
|
|
dctx->litSize = litSize;
|
|
|
|
return litSize+3;
|
|
|
|
}
|
|
|
|
/* direct reference into compressed stream */
|
2015-10-19 18:25:44 +00:00
|
|
|
dctx->litPtr = istart+3;
|
|
|
|
dctx->litBufSize = srcSize-3;
|
|
|
|
dctx->litSize = litSize;
|
2015-10-23 14:21:53 +00:00
|
|
|
return litSize+3; }
|
2015-10-19 18:25:44 +00:00
|
|
|
case IS_RLE:
|
2015-01-24 00:58:16 +00:00
|
|
|
{
|
2015-10-19 18:25:44 +00:00
|
|
|
const size_t litSize = (MEM_readLE32(istart) & 0xFFFFFF) >> 2; /* no buffer issue : srcSize >= MIN_CBLOCK_SIZE */
|
|
|
|
if (litSize > BLOCKSIZE) return ERROR(corruption_detected);
|
|
|
|
memset(dctx->litBuffer, istart[3], litSize);
|
2015-10-18 21:18:32 +00:00
|
|
|
dctx->litPtr = dctx->litBuffer;
|
2015-10-23 18:25:06 +00:00
|
|
|
dctx->litBufSize = BLOCKSIZE+8;
|
2015-10-19 18:25:44 +00:00
|
|
|
dctx->litSize = litSize;
|
|
|
|
return 4;
|
2015-01-24 00:58:16 +00:00
|
|
|
}
|
2015-10-22 14:31:46 +00:00
|
|
|
default:
|
|
|
|
return ERROR(corruption_detected); /* forbidden nominal case */
|
2015-01-24 00:58:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-10-14 16:53:44 +00:00
|
|
|
size_t ZSTD_decodeSeqHeaders(int* nbSeq, const BYTE** dumpsPtr, size_t* dumpsLengthPtr,
|
2015-07-04 23:56:41 +00:00
|
|
|
FSE_DTable* DTableLL, FSE_DTable* DTableML, FSE_DTable* DTableOffb,
|
2015-01-24 00:58:16 +00:00
|
|
|
const void* src, size_t srcSize)
|
|
|
|
{
|
2015-01-26 09:24:04 +00:00
|
|
|
const BYTE* const istart = (const BYTE* const)src;
|
2015-01-24 00:58:16 +00:00
|
|
|
const BYTE* ip = istart;
|
|
|
|
const BYTE* const iend = istart + srcSize;
|
|
|
|
U32 LLtype, Offtype, MLtype;
|
|
|
|
U32 LLlog, Offlog, MLlog;
|
|
|
|
size_t dumpsLength;
|
|
|
|
|
2015-09-10 22:26:09 +00:00
|
|
|
/* check */
|
2015-10-18 21:18:32 +00:00
|
|
|
if (srcSize < 5) return ERROR(srcSize_wrong);
|
2015-08-21 01:44:20 +00:00
|
|
|
|
2015-01-24 00:58:16 +00:00
|
|
|
/* SeqHead */
|
2015-10-18 21:18:32 +00:00
|
|
|
*nbSeq = MEM_readLE16(ip); ip+=2;
|
2015-01-24 00:58:16 +00:00
|
|
|
LLtype = *ip >> 6;
|
|
|
|
Offtype = (*ip >> 4) & 3;
|
|
|
|
MLtype = (*ip >> 2) & 3;
|
|
|
|
if (*ip & 2)
|
|
|
|
{
|
|
|
|
dumpsLength = ip[2];
|
|
|
|
dumpsLength += ip[1] << 8;
|
|
|
|
ip += 3;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
dumpsLength = ip[1];
|
|
|
|
dumpsLength += (ip[0] & 1) << 8;
|
|
|
|
ip += 2;
|
|
|
|
}
|
|
|
|
*dumpsPtr = ip;
|
|
|
|
ip += dumpsLength;
|
2015-10-14 16:53:44 +00:00
|
|
|
*dumpsLengthPtr = dumpsLength;
|
2015-01-24 00:58:16 +00:00
|
|
|
|
2015-09-10 22:26:09 +00:00
|
|
|
/* check */
|
2015-10-18 21:18:32 +00:00
|
|
|
if (ip > iend-3) return ERROR(srcSize_wrong); /* min : all 3 are "raw", hence no header, but at least xxLog bits per type */
|
2015-08-21 01:44:20 +00:00
|
|
|
|
2015-01-24 00:58:16 +00:00
|
|
|
/* sequences */
|
|
|
|
{
|
|
|
|
S16 norm[MaxML+1]; /* assumption : MaxML >= MaxLL and MaxOff */
|
2015-02-01 10:57:30 +00:00
|
|
|
size_t headerSize;
|
2015-01-24 00:58:16 +00:00
|
|
|
|
|
|
|
/* Build DTables */
|
|
|
|
switch(LLtype)
|
|
|
|
{
|
|
|
|
U32 max;
|
|
|
|
case bt_rle :
|
|
|
|
LLlog = 0;
|
|
|
|
FSE_buildDTable_rle(DTableLL, *ip++); break;
|
|
|
|
case bt_raw :
|
|
|
|
LLlog = LLbits;
|
|
|
|
FSE_buildDTable_raw(DTableLL, LLbits); break;
|
|
|
|
default :
|
|
|
|
max = MaxLL;
|
2015-07-04 23:56:41 +00:00
|
|
|
headerSize = FSE_readNCount(norm, &max, &LLlog, ip, iend-ip);
|
2015-10-18 21:18:32 +00:00
|
|
|
if (FSE_isError(headerSize)) return ERROR(GENERIC);
|
|
|
|
if (LLlog > LLFSELog) return ERROR(corruption_detected);
|
2015-02-01 10:57:30 +00:00
|
|
|
ip += headerSize;
|
2015-01-24 00:58:16 +00:00
|
|
|
FSE_buildDTable(DTableLL, norm, max, LLlog);
|
|
|
|
}
|
|
|
|
|
|
|
|
switch(Offtype)
|
|
|
|
{
|
|
|
|
U32 max;
|
|
|
|
case bt_rle :
|
|
|
|
Offlog = 0;
|
2015-10-19 14:32:47 +00:00
|
|
|
if (ip > iend-2) return ERROR(srcSize_wrong); /* min : "raw", hence no header, but at least xxLog bits */
|
|
|
|
FSE_buildDTable_rle(DTableOffb, *ip++ & MaxOff); /* if *ip > MaxOff, data is corrupted */
|
|
|
|
break;
|
2015-01-24 00:58:16 +00:00
|
|
|
case bt_raw :
|
|
|
|
Offlog = Offbits;
|
|
|
|
FSE_buildDTable_raw(DTableOffb, Offbits); break;
|
|
|
|
default :
|
|
|
|
max = MaxOff;
|
2015-07-04 23:56:41 +00:00
|
|
|
headerSize = FSE_readNCount(norm, &max, &Offlog, ip, iend-ip);
|
2015-10-18 21:18:32 +00:00
|
|
|
if (FSE_isError(headerSize)) return ERROR(GENERIC);
|
|
|
|
if (Offlog > OffFSELog) return ERROR(corruption_detected);
|
2015-02-01 10:57:30 +00:00
|
|
|
ip += headerSize;
|
2015-01-24 00:58:16 +00:00
|
|
|
FSE_buildDTable(DTableOffb, norm, max, Offlog);
|
|
|
|
}
|
|
|
|
|
|
|
|
switch(MLtype)
|
|
|
|
{
|
|
|
|
U32 max;
|
|
|
|
case bt_rle :
|
|
|
|
MLlog = 0;
|
2015-10-18 21:18:32 +00:00
|
|
|
if (ip > iend-2) return ERROR(srcSize_wrong); /* min : "raw", hence no header, but at least xxLog bits */
|
2015-01-24 00:58:16 +00:00
|
|
|
FSE_buildDTable_rle(DTableML, *ip++); break;
|
|
|
|
case bt_raw :
|
|
|
|
MLlog = MLbits;
|
|
|
|
FSE_buildDTable_raw(DTableML, MLbits); break;
|
|
|
|
default :
|
|
|
|
max = MaxML;
|
2015-07-04 23:56:41 +00:00
|
|
|
headerSize = FSE_readNCount(norm, &max, &MLlog, ip, iend-ip);
|
2015-10-18 21:18:32 +00:00
|
|
|
if (FSE_isError(headerSize)) return ERROR(GENERIC);
|
|
|
|
if (MLlog > MLFSELog) return ERROR(corruption_detected);
|
2015-02-01 10:57:30 +00:00
|
|
|
ip += headerSize;
|
2015-01-24 00:58:16 +00:00
|
|
|
FSE_buildDTable(DTableML, norm, max, MLlog);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return ip-istart;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-08-11 13:18:45 +00:00
|
|
|
typedef struct {
|
|
|
|
size_t litLength;
|
|
|
|
size_t offset;
|
|
|
|
size_t matchLength;
|
|
|
|
} seq_t;
|
2015-01-24 00:58:16 +00:00
|
|
|
|
2015-08-11 13:18:45 +00:00
|
|
|
typedef struct {
|
2015-10-18 21:18:32 +00:00
|
|
|
BIT_DStream_t DStream;
|
2015-08-11 13:18:45 +00:00
|
|
|
FSE_DState_t stateLL;
|
|
|
|
FSE_DState_t stateOffb;
|
|
|
|
FSE_DState_t stateML;
|
|
|
|
size_t prevOffset;
|
|
|
|
const BYTE* dumps;
|
2015-10-14 16:53:44 +00:00
|
|
|
const BYTE* dumpsEnd;
|
2015-08-11 13:18:45 +00:00
|
|
|
} seqState_t;
|
|
|
|
|
|
|
|
|
2015-08-20 06:46:10 +00:00
|
|
|
static void ZSTD_decodeSequence(seq_t* seq, seqState_t* seqState)
|
2015-08-11 13:18:45 +00:00
|
|
|
{
|
|
|
|
size_t litLength;
|
|
|
|
size_t prevOffset;
|
|
|
|
size_t offset;
|
|
|
|
size_t matchLength;
|
|
|
|
const BYTE* dumps = seqState->dumps;
|
2015-10-14 16:53:44 +00:00
|
|
|
const BYTE* const de = seqState->dumpsEnd;
|
2015-08-11 13:18:45 +00:00
|
|
|
|
|
|
|
/* Literal length */
|
|
|
|
litLength = FSE_decodeSymbol(&(seqState->stateLL), &(seqState->DStream));
|
2015-08-20 06:46:10 +00:00
|
|
|
prevOffset = litLength ? seq->offset : seqState->prevOffset;
|
|
|
|
seqState->prevOffset = seq->offset;
|
2015-08-11 13:18:45 +00:00
|
|
|
if (litLength == MaxLL)
|
|
|
|
{
|
2015-10-19 01:14:43 +00:00
|
|
|
U32 add = *dumps++;
|
2015-08-11 13:18:45 +00:00
|
|
|
if (add < 255) litLength += add;
|
|
|
|
else
|
|
|
|
{
|
2015-10-19 01:14:43 +00:00
|
|
|
litLength = MEM_readLE32(dumps) & 0xFFFFFF; /* no pb : dumps is always followed by seq tables > 1 byte */
|
|
|
|
dumps += 3;
|
2015-08-11 13:18:45 +00:00
|
|
|
}
|
2015-10-19 01:14:43 +00:00
|
|
|
if (dumps >= de) dumps = de-1; /* late correction, to avoid read overflow (data is now corrupted anyway) */
|
2015-08-11 13:18:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Offset */
|
|
|
|
{
|
2015-10-30 05:40:22 +00:00
|
|
|
static const U32 offsetPrefix[MaxOff+1] = {
|
2015-10-19 14:32:47 +00:00
|
|
|
1 /*fake*/, 1, 2, 4, 8, 16, 32, 64, 128, 256,
|
2015-10-19 01:36:34 +00:00
|
|
|
512, 1024, 2048, 4096, 8192, 16384, 32768, 65536, 131072, 262144,
|
2015-10-19 14:32:47 +00:00
|
|
|
524288, 1048576, 2097152, 4194304, 8388608, 16777216, 33554432, /*fake*/ 1, 1, 1, 1, 1 };
|
2015-08-11 13:18:45 +00:00
|
|
|
U32 offsetCode, nbBits;
|
2015-10-19 01:36:34 +00:00
|
|
|
offsetCode = FSE_decodeSymbol(&(seqState->stateOffb), &(seqState->DStream)); /* <= maxOff, by table construction */
|
2015-10-18 21:18:32 +00:00
|
|
|
if (MEM_32bits()) BIT_reloadDStream(&(seqState->DStream));
|
2015-08-11 13:18:45 +00:00
|
|
|
nbBits = offsetCode - 1;
|
|
|
|
if (offsetCode==0) nbBits = 0; /* cmove */
|
2015-10-19 01:36:34 +00:00
|
|
|
offset = offsetPrefix[offsetCode] + BIT_readBits(&(seqState->DStream), nbBits);
|
2015-10-18 21:18:32 +00:00
|
|
|
if (MEM_32bits()) BIT_reloadDStream(&(seqState->DStream));
|
2015-10-19 01:14:43 +00:00
|
|
|
if (offsetCode==0) offset = prevOffset; /* cmove */
|
2015-08-11 13:18:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* MatchLength */
|
|
|
|
matchLength = FSE_decodeSymbol(&(seqState->stateML), &(seqState->DStream));
|
|
|
|
if (matchLength == MaxML)
|
|
|
|
{
|
2015-10-19 01:14:43 +00:00
|
|
|
U32 add = *dumps++;
|
2015-08-11 13:18:45 +00:00
|
|
|
if (add < 255) matchLength += add;
|
|
|
|
else
|
|
|
|
{
|
2015-10-19 01:14:43 +00:00
|
|
|
matchLength = MEM_readLE32(dumps) & 0xFFFFFF; /* no pb : dumps is always followed by seq tables > 1 byte */
|
|
|
|
dumps += 3;
|
2015-08-11 13:18:45 +00:00
|
|
|
}
|
2015-10-19 01:14:43 +00:00
|
|
|
if (dumps >= de) dumps = de-1; /* late correction, to avoid read overflow (data is now corrupted anyway) */
|
2015-08-11 13:18:45 +00:00
|
|
|
}
|
|
|
|
matchLength += MINMATCH;
|
|
|
|
|
|
|
|
/* save result */
|
2015-08-20 06:46:10 +00:00
|
|
|
seq->litLength = litLength;
|
|
|
|
seq->offset = offset;
|
|
|
|
seq->matchLength = matchLength;
|
2015-08-11 13:18:45 +00:00
|
|
|
seqState->dumps = dumps;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-08-21 01:44:20 +00:00
|
|
|
static size_t ZSTD_execSequence(BYTE* op,
|
2015-09-10 22:26:09 +00:00
|
|
|
seq_t sequence,
|
2015-10-23 18:25:06 +00:00
|
|
|
const BYTE** litPtr, const BYTE* const litLimit_8,
|
2015-09-10 22:26:09 +00:00
|
|
|
BYTE* const base, BYTE* const oend)
|
2015-08-11 13:18:45 +00:00
|
|
|
{
|
2015-08-23 22:13:49 +00:00
|
|
|
static const int dec32table[] = {0, 1, 2, 1, 4, 4, 4, 4}; /* added */
|
2015-08-11 13:18:45 +00:00
|
|
|
static const int dec64table[] = {8, 8, 8, 7, 8, 9,10,11}; /* substracted */
|
|
|
|
const BYTE* const ostart = op;
|
2015-10-19 02:12:23 +00:00
|
|
|
BYTE* const oLitEnd = op + sequence.litLength;
|
|
|
|
BYTE* const oMatchEnd = op + sequence.litLength + sequence.matchLength; /* risk : address space overflow (32-bits) */
|
|
|
|
BYTE* const oend_8 = oend-8;
|
|
|
|
const BYTE* const litEnd = *litPtr + sequence.litLength;
|
2015-08-20 06:46:10 +00:00
|
|
|
|
|
|
|
/* check */
|
2015-10-19 02:12:23 +00:00
|
|
|
if (oLitEnd > oend_8) return ERROR(dstSize_tooSmall); /* last match must start at a minimum distance of 8 from oend */
|
2015-10-19 01:14:43 +00:00
|
|
|
if (oMatchEnd > oend) return ERROR(dstSize_tooSmall); /* overwrite beyond dst buffer */
|
2015-10-29 15:49:43 +00:00
|
|
|
if (litEnd > litLimit_8) return ERROR(corruption_detected); /* risk read beyond lit buffer */
|
2015-08-11 13:18:45 +00:00
|
|
|
|
|
|
|
/* copy Literals */
|
2015-10-19 02:12:23 +00:00
|
|
|
ZSTD_wildcopy(op, *litPtr, sequence.litLength); /* note : oLitEnd <= oend-8 : no risk of overwrite beyond oend */
|
|
|
|
op = oLitEnd;
|
2015-08-20 14:55:50 +00:00
|
|
|
*litPtr = litEnd; /* update for next sequence */
|
2015-08-11 13:18:45 +00:00
|
|
|
|
2015-09-10 22:26:09 +00:00
|
|
|
/* copy Match */
|
2015-08-11 13:18:45 +00:00
|
|
|
{
|
2015-10-19 01:14:43 +00:00
|
|
|
const BYTE* match = op - sequence.offset;
|
2015-08-11 13:18:45 +00:00
|
|
|
|
2015-09-10 22:26:09 +00:00
|
|
|
/* check */
|
2015-10-20 02:01:38 +00:00
|
|
|
//if (match > op) return ERROR(corruption_detected); /* address space overflow test (is clang optimizer removing this test ?) */
|
2015-10-22 14:31:46 +00:00
|
|
|
if (sequence.offset > (size_t)op) return ERROR(corruption_detected); /* address space overflow test (this test seems kept by clang optimizer) */
|
2015-10-18 21:18:32 +00:00
|
|
|
if (match < base) return ERROR(corruption_detected);
|
2015-08-11 13:18:45 +00:00
|
|
|
|
2015-10-18 21:18:32 +00:00
|
|
|
/* close range match, overlap */
|
2015-08-11 13:18:45 +00:00
|
|
|
if (sequence.offset < 8)
|
|
|
|
{
|
|
|
|
const int dec64 = dec64table[sequence.offset];
|
|
|
|
op[0] = match[0];
|
|
|
|
op[1] = match[1];
|
|
|
|
op[2] = match[2];
|
|
|
|
op[3] = match[3];
|
|
|
|
match += dec32table[sequence.offset];
|
|
|
|
ZSTD_copy4(op+4, match);
|
|
|
|
match -= dec64;
|
2015-10-20 02:01:38 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
ZSTD_copy8(op, match);
|
|
|
|
}
|
2015-09-10 22:26:09 +00:00
|
|
|
op += 8; match += 8;
|
2015-08-11 13:18:45 +00:00
|
|
|
|
2015-10-19 01:14:43 +00:00
|
|
|
if (oMatchEnd > oend-12)
|
2015-08-11 13:18:45 +00:00
|
|
|
{
|
2015-10-19 02:12:23 +00:00
|
|
|
if (op < oend_8)
|
2015-08-11 13:18:45 +00:00
|
|
|
{
|
2015-10-19 02:12:23 +00:00
|
|
|
ZSTD_wildcopy(op, match, oend_8 - op);
|
|
|
|
match += oend_8 - op;
|
|
|
|
op = oend_8;
|
2015-08-11 13:18:45 +00:00
|
|
|
}
|
2015-10-19 01:14:43 +00:00
|
|
|
while (op < oMatchEnd) *op++ = *match++;
|
2015-08-11 13:18:45 +00:00
|
|
|
}
|
|
|
|
else
|
2015-10-19 01:14:43 +00:00
|
|
|
{
|
2015-08-20 14:55:50 +00:00
|
|
|
ZSTD_wildcopy(op, match, sequence.matchLength-8); /* works even if matchLength < 8 */
|
2015-10-19 01:14:43 +00:00
|
|
|
}
|
2015-08-11 13:18:45 +00:00
|
|
|
}
|
|
|
|
|
2015-10-19 01:14:43 +00:00
|
|
|
return oMatchEnd - ostart;
|
2015-08-11 13:18:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static size_t ZSTD_decompressSequences(
|
|
|
|
void* ctx,
|
|
|
|
void* dst, size_t maxDstSize,
|
2015-10-18 21:18:32 +00:00
|
|
|
const void* seqStart, size_t seqSize)
|
2015-08-11 13:18:45 +00:00
|
|
|
{
|
2015-10-21 13:39:26 +00:00
|
|
|
ZSTD_DCtx* dctx = (ZSTD_DCtx*)ctx;
|
2015-08-11 13:18:45 +00:00
|
|
|
const BYTE* ip = (const BYTE*)seqStart;
|
|
|
|
const BYTE* const iend = ip + seqSize;
|
|
|
|
BYTE* const ostart = (BYTE* const)dst;
|
|
|
|
BYTE* op = ostart;
|
|
|
|
BYTE* const oend = ostart + maxDstSize;
|
2015-10-14 16:53:44 +00:00
|
|
|
size_t errorCode, dumpsLength;
|
2015-10-18 21:18:32 +00:00
|
|
|
const BYTE* litPtr = dctx->litPtr;
|
2015-10-23 18:25:06 +00:00
|
|
|
const BYTE* const litLimit_8 = litPtr + dctx->litBufSize - 8;
|
2015-10-18 21:18:32 +00:00
|
|
|
const BYTE* const litEnd = litPtr + dctx->litSize;
|
2015-08-11 13:18:45 +00:00
|
|
|
int nbSeq;
|
|
|
|
const BYTE* dumps;
|
2015-08-21 01:44:20 +00:00
|
|
|
U32* DTableLL = dctx->LLTable;
|
2015-09-10 22:26:09 +00:00
|
|
|
U32* DTableML = dctx->MLTable;
|
2015-08-21 01:44:20 +00:00
|
|
|
U32* DTableOffb = dctx->OffTable;
|
2015-09-10 22:26:09 +00:00
|
|
|
BYTE* const base = (BYTE*) (dctx->base);
|
2015-08-11 13:18:45 +00:00
|
|
|
|
|
|
|
/* Build Decoding Tables */
|
2015-10-14 16:53:44 +00:00
|
|
|
errorCode = ZSTD_decodeSeqHeaders(&nbSeq, &dumps, &dumpsLength,
|
2015-08-11 13:18:45 +00:00
|
|
|
DTableLL, DTableML, DTableOffb,
|
|
|
|
ip, iend-ip);
|
|
|
|
if (ZSTD_isError(errorCode)) return errorCode;
|
|
|
|
ip += errorCode;
|
|
|
|
|
|
|
|
/* Regen sequences */
|
|
|
|
{
|
2015-08-20 06:46:10 +00:00
|
|
|
seq_t sequence;
|
2015-08-11 13:18:45 +00:00
|
|
|
seqState_t seqState;
|
|
|
|
|
2015-08-20 06:46:10 +00:00
|
|
|
memset(&sequence, 0, sizeof(sequence));
|
2015-10-22 14:31:46 +00:00
|
|
|
sequence.offset = 4;
|
2015-08-11 13:18:45 +00:00
|
|
|
seqState.dumps = dumps;
|
2015-10-14 16:53:44 +00:00
|
|
|
seqState.dumpsEnd = dumps + dumpsLength;
|
2015-10-22 14:31:46 +00:00
|
|
|
seqState.prevOffset = 4;
|
2015-10-18 21:18:32 +00:00
|
|
|
errorCode = BIT_initDStream(&(seqState.DStream), ip, iend-ip);
|
|
|
|
if (ERR_isError(errorCode)) return ERROR(corruption_detected);
|
2015-08-11 13:18:45 +00:00
|
|
|
FSE_initDState(&(seqState.stateLL), &(seqState.DStream), DTableLL);
|
|
|
|
FSE_initDState(&(seqState.stateOffb), &(seqState.DStream), DTableOffb);
|
|
|
|
FSE_initDState(&(seqState.stateML), &(seqState.DStream), DTableML);
|
|
|
|
|
2015-10-18 21:18:32 +00:00
|
|
|
for ( ; (BIT_reloadDStream(&(seqState.DStream)) <= BIT_DStream_completed) && (nbSeq>0) ; )
|
2015-08-11 13:18:45 +00:00
|
|
|
{
|
2015-08-20 06:46:10 +00:00
|
|
|
size_t oneSeqSize;
|
2015-08-11 13:18:45 +00:00
|
|
|
nbSeq--;
|
2015-08-20 06:46:10 +00:00
|
|
|
ZSTD_decodeSequence(&sequence, &seqState);
|
2015-10-23 18:25:06 +00:00
|
|
|
oneSeqSize = ZSTD_execSequence(op, sequence, &litPtr, litLimit_8, base, oend);
|
2015-08-20 06:46:10 +00:00
|
|
|
if (ZSTD_isError(oneSeqSize)) return oneSeqSize;
|
|
|
|
op += oneSeqSize;
|
2015-08-11 13:18:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* check if reached exact end */
|
2015-10-18 21:18:32 +00:00
|
|
|
if ( !BIT_endOfDStream(&(seqState.DStream)) ) return ERROR(corruption_detected); /* requested too much : data is corrupted */
|
|
|
|
if (nbSeq<0) return ERROR(corruption_detected); /* requested too many sequences : data is corrupted */
|
2015-08-11 13:18:45 +00:00
|
|
|
|
|
|
|
/* last literal segment */
|
|
|
|
{
|
|
|
|
size_t lastLLSize = litEnd - litPtr;
|
2015-10-18 21:18:32 +00:00
|
|
|
if (litPtr > litEnd) return ERROR(corruption_detected);
|
|
|
|
if (op+lastLLSize > oend) return ERROR(dstSize_tooSmall);
|
2015-10-22 14:31:46 +00:00
|
|
|
if (op != litPtr) memcpy(op, litPtr, lastLLSize);
|
2015-08-11 13:18:45 +00:00
|
|
|
op += lastLLSize;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return op-ostart;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static size_t ZSTD_decompressBlock(
|
|
|
|
void* ctx,
|
|
|
|
void* dst, size_t maxDstSize,
|
|
|
|
const void* src, size_t srcSize)
|
|
|
|
{
|
2015-10-19 18:25:44 +00:00
|
|
|
/* blockType == blockCompressed */
|
2015-08-11 13:18:45 +00:00
|
|
|
const BYTE* ip = (const BYTE*)src;
|
|
|
|
|
|
|
|
/* Decode literals sub-block */
|
2015-10-19 18:25:44 +00:00
|
|
|
size_t litCSize = ZSTD_decodeLiteralsBlock(ctx, src, srcSize);
|
|
|
|
if (ZSTD_isError(litCSize)) return litCSize;
|
|
|
|
ip += litCSize;
|
|
|
|
srcSize -= litCSize;
|
2015-08-11 13:18:45 +00:00
|
|
|
|
2015-10-18 21:18:32 +00:00
|
|
|
return ZSTD_decompressSequences(ctx, dst, maxDstSize, ip, srcSize);
|
2015-08-11 13:18:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-01-24 00:58:16 +00:00
|
|
|
static size_t ZSTD_decompressDCtx(void* ctx, void* dst, size_t maxDstSize, const void* src, size_t srcSize)
|
|
|
|
{
|
2015-01-26 09:24:04 +00:00
|
|
|
const BYTE* ip = (const BYTE*)src;
|
2015-01-24 00:58:16 +00:00
|
|
|
const BYTE* iend = ip + srcSize;
|
2015-01-26 09:24:04 +00:00
|
|
|
BYTE* const ostart = (BYTE* const)dst;
|
2015-01-24 00:58:16 +00:00
|
|
|
BYTE* op = ostart;
|
|
|
|
BYTE* const oend = ostart + maxDstSize;
|
|
|
|
size_t remainingSize = srcSize;
|
|
|
|
U32 magicNumber;
|
|
|
|
blockProperties_t blockProperties;
|
|
|
|
|
2015-08-11 13:18:45 +00:00
|
|
|
/* Frame Header */
|
2015-10-18 21:18:32 +00:00
|
|
|
if (srcSize < ZSTD_frameHeaderSize+ZSTD_blockHeaderSize) return ERROR(srcSize_wrong);
|
2015-10-18 21:26:26 +00:00
|
|
|
magicNumber = MEM_readLE32(src);
|
2015-10-18 21:18:32 +00:00
|
|
|
#if defined(ZSTD_LEGACY_SUPPORT) && (ZSTD_LEGACY_SUPPORT==1)
|
2015-10-30 10:21:50 +00:00
|
|
|
if (ZSTD_isLegacy(magicNumber))
|
|
|
|
return ZSTD_decompressLegacy(dst, maxDstSize, src, srcSize, magicNumber);
|
2015-10-23 14:21:53 +00:00
|
|
|
#endif
|
2015-10-18 21:18:32 +00:00
|
|
|
if (magicNumber != ZSTD_magicNumber) return ERROR(prefix_unknown);
|
2015-01-24 00:58:16 +00:00
|
|
|
ip += ZSTD_frameHeaderSize; remainingSize -= ZSTD_frameHeaderSize;
|
|
|
|
|
2015-08-11 13:18:45 +00:00
|
|
|
/* Loop on each block */
|
2015-01-24 00:58:16 +00:00
|
|
|
while (1)
|
|
|
|
{
|
2015-10-19 18:25:44 +00:00
|
|
|
size_t decodedSize=0;
|
|
|
|
size_t cBlockSize = ZSTD_getcBlockSize(ip, iend-ip, &blockProperties);
|
|
|
|
if (ZSTD_isError(cBlockSize)) return cBlockSize;
|
2015-01-24 00:58:16 +00:00
|
|
|
|
|
|
|
ip += ZSTD_blockHeaderSize;
|
|
|
|
remainingSize -= ZSTD_blockHeaderSize;
|
2015-10-19 18:25:44 +00:00
|
|
|
if (cBlockSize > remainingSize) return ERROR(srcSize_wrong);
|
2015-01-24 00:58:16 +00:00
|
|
|
|
|
|
|
switch(blockProperties.blockType)
|
|
|
|
{
|
|
|
|
case bt_compressed:
|
2015-10-19 18:25:44 +00:00
|
|
|
decodedSize = ZSTD_decompressBlock(ctx, op, oend-op, ip, cBlockSize);
|
2015-01-24 00:58:16 +00:00
|
|
|
break;
|
|
|
|
case bt_raw :
|
2015-10-19 18:25:44 +00:00
|
|
|
decodedSize = ZSTD_copyUncompressedBlock(op, oend-op, ip, cBlockSize);
|
2015-01-24 00:58:16 +00:00
|
|
|
break;
|
|
|
|
case bt_rle :
|
2015-10-18 21:18:32 +00:00
|
|
|
return ERROR(GENERIC); /* not yet supported */
|
2015-01-24 00:58:16 +00:00
|
|
|
break;
|
|
|
|
case bt_end :
|
|
|
|
/* end of frame */
|
2015-10-18 21:18:32 +00:00
|
|
|
if (remainingSize) return ERROR(srcSize_wrong);
|
2015-01-24 00:58:16 +00:00
|
|
|
break;
|
|
|
|
default:
|
2015-10-19 18:25:44 +00:00
|
|
|
return ERROR(GENERIC); /* impossible */
|
2015-01-24 00:58:16 +00:00
|
|
|
}
|
2015-10-19 18:25:44 +00:00
|
|
|
if (cBlockSize == 0) break; /* bt_end */
|
2015-01-24 00:58:16 +00:00
|
|
|
|
2015-10-19 18:25:44 +00:00
|
|
|
if (ZSTD_isError(decodedSize)) return decodedSize;
|
|
|
|
op += decodedSize;
|
|
|
|
ip += cBlockSize;
|
|
|
|
remainingSize -= cBlockSize;
|
2015-01-24 00:58:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return op-ostart;
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t ZSTD_decompress(void* dst, size_t maxDstSize, const void* src, size_t srcSize)
|
|
|
|
{
|
2015-10-21 13:39:26 +00:00
|
|
|
ZSTD_DCtx ctx;
|
2015-09-10 22:26:09 +00:00
|
|
|
ctx.base = dst;
|
2015-08-21 01:44:20 +00:00
|
|
|
return ZSTD_decompressDCtx(&ctx, dst, maxDstSize, src, srcSize);
|
2015-01-24 00:58:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-10-22 14:31:46 +00:00
|
|
|
/* ******************************
|
2015-01-24 00:58:16 +00:00
|
|
|
* Streaming Decompression API
|
2015-10-22 14:31:46 +00:00
|
|
|
********************************/
|
2015-01-24 00:58:16 +00:00
|
|
|
|
2015-10-21 13:39:26 +00:00
|
|
|
size_t ZSTD_resetDCtx(ZSTD_DCtx* dctx)
|
2015-09-10 22:26:09 +00:00
|
|
|
{
|
|
|
|
dctx->expected = ZSTD_frameHeaderSize;
|
|
|
|
dctx->phase = 0;
|
|
|
|
dctx->previousDstEnd = NULL;
|
|
|
|
dctx->base = NULL;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-10-21 13:39:26 +00:00
|
|
|
ZSTD_DCtx* ZSTD_createDCtx(void)
|
2015-01-24 00:58:16 +00:00
|
|
|
{
|
2015-10-21 13:39:26 +00:00
|
|
|
ZSTD_DCtx* dctx = (ZSTD_DCtx*)malloc(sizeof(ZSTD_DCtx));
|
2015-07-05 07:10:40 +00:00
|
|
|
if (dctx==NULL) return NULL;
|
2015-09-10 22:26:09 +00:00
|
|
|
ZSTD_resetDCtx(dctx);
|
2015-07-05 07:10:40 +00:00
|
|
|
return dctx;
|
2015-01-24 00:58:16 +00:00
|
|
|
}
|
|
|
|
|
2015-10-21 13:39:26 +00:00
|
|
|
size_t ZSTD_freeDCtx(ZSTD_DCtx* dctx)
|
2015-01-24 00:58:16 +00:00
|
|
|
{
|
|
|
|
free(dctx);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-10-21 13:39:26 +00:00
|
|
|
size_t ZSTD_nextSrcSizeToDecompress(ZSTD_DCtx* dctx)
|
2015-01-24 00:58:16 +00:00
|
|
|
{
|
2015-10-21 13:39:26 +00:00
|
|
|
return dctx->expected;
|
2015-01-24 00:58:16 +00:00
|
|
|
}
|
|
|
|
|
2015-10-21 13:39:26 +00:00
|
|
|
size_t ZSTD_decompressContinue(ZSTD_DCtx* ctx, void* dst, size_t maxDstSize, const void* src, size_t srcSize)
|
2015-01-24 00:58:16 +00:00
|
|
|
{
|
2015-02-16 17:06:26 +00:00
|
|
|
/* Sanity check */
|
2015-10-18 21:18:32 +00:00
|
|
|
if (srcSize != ctx->expected) return ERROR(srcSize_wrong);
|
2015-09-10 22:26:09 +00:00
|
|
|
if (dst != ctx->previousDstEnd) /* not contiguous */
|
|
|
|
ctx->base = dst;
|
2015-01-24 00:58:16 +00:00
|
|
|
|
2015-02-16 17:06:26 +00:00
|
|
|
/* Decompress : frame header */
|
|
|
|
if (ctx->phase == 0)
|
2015-01-24 00:58:16 +00:00
|
|
|
{
|
2015-02-16 17:06:26 +00:00
|
|
|
/* Check frame magic header */
|
2015-10-18 21:26:26 +00:00
|
|
|
U32 magicNumber = MEM_readLE32(src);
|
2015-10-18 21:18:32 +00:00
|
|
|
if (magicNumber != ZSTD_magicNumber) return ERROR(prefix_unknown);
|
2015-02-16 17:06:26 +00:00
|
|
|
ctx->phase = 1;
|
|
|
|
ctx->expected = ZSTD_blockHeaderSize;
|
|
|
|
return 0;
|
2015-01-24 00:58:16 +00:00
|
|
|
}
|
2015-02-16 17:06:26 +00:00
|
|
|
|
|
|
|
/* Decompress : block header */
|
|
|
|
if (ctx->phase == 1)
|
2015-01-24 00:58:16 +00:00
|
|
|
{
|
2015-02-16 17:06:26 +00:00
|
|
|
blockProperties_t bp;
|
|
|
|
size_t blockSize = ZSTD_getcBlockSize(src, ZSTD_blockHeaderSize, &bp);
|
|
|
|
if (ZSTD_isError(blockSize)) return blockSize;
|
|
|
|
if (bp.blockType == bt_end)
|
|
|
|
{
|
|
|
|
ctx->expected = 0;
|
|
|
|
ctx->phase = 0;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
ctx->expected = blockSize;
|
|
|
|
ctx->bType = bp.blockType;
|
|
|
|
ctx->phase = 2;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Decompress : block content */
|
|
|
|
{
|
|
|
|
size_t rSize;
|
2015-01-24 00:58:16 +00:00
|
|
|
switch(ctx->bType)
|
|
|
|
{
|
|
|
|
case bt_compressed:
|
2015-02-16 17:06:26 +00:00
|
|
|
rSize = ZSTD_decompressBlock(ctx, dst, maxDstSize, src, srcSize);
|
2015-01-24 00:58:16 +00:00
|
|
|
break;
|
|
|
|
case bt_raw :
|
2015-02-16 17:06:26 +00:00
|
|
|
rSize = ZSTD_copyUncompressedBlock(dst, maxDstSize, src, srcSize);
|
2015-01-24 00:58:16 +00:00
|
|
|
break;
|
|
|
|
case bt_rle :
|
2015-10-18 21:18:32 +00:00
|
|
|
return ERROR(GENERIC); /* not yet handled */
|
2015-01-24 00:58:16 +00:00
|
|
|
break;
|
2015-02-16 17:06:26 +00:00
|
|
|
case bt_end : /* should never happen (filtered at phase 1) */
|
2015-01-24 00:58:16 +00:00
|
|
|
rSize = 0;
|
|
|
|
break;
|
|
|
|
default:
|
2015-10-18 21:18:32 +00:00
|
|
|
return ERROR(GENERIC);
|
2015-01-24 00:58:16 +00:00
|
|
|
}
|
2015-02-16 17:06:26 +00:00
|
|
|
ctx->phase = 1;
|
|
|
|
ctx->expected = ZSTD_blockHeaderSize;
|
2015-09-10 22:26:09 +00:00
|
|
|
ctx->previousDstEnd = (void*)( ((char*)dst) + rSize);
|
2015-02-16 17:06:26 +00:00
|
|
|
return rSize;
|
2015-01-24 00:58:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|