2015-10-18 21:18:32 +00:00
|
|
|
/* ******************************************************************
|
2016-04-22 10:43:18 +00:00
|
|
|
Huffman decoder, part of New Generation Entropy library
|
2016-03-20 04:40:39 +00:00
|
|
|
Copyright (C) 2013-2016, Yann Collet.
|
2015-10-18 21:18:32 +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-04-22 10:43:18 +00:00
|
|
|
- FSE+HUF source repository : https://github.com/Cyan4973/FiniteStateEntropy
|
2015-10-18 21:18:32 +00:00
|
|
|
- Public forum : https://groups.google.com/forum/#!forum/lz4c
|
|
|
|
****************************************************************** */
|
|
|
|
|
2016-01-06 00:58:37 +00:00
|
|
|
/* **************************************************************
|
2016-08-19 17:38:19 +00:00
|
|
|
* Dependencies
|
2015-10-18 21:18:32 +00:00
|
|
|
****************************************************************/
|
|
|
|
#include <string.h> /* memcpy, memset */
|
2016-08-19 17:38:19 +00:00
|
|
|
#include "bitstream.h" /* BIT_* */
|
2017-08-15 00:20:50 +00:00
|
|
|
#include "compiler.h"
|
2015-10-18 21:18:32 +00:00
|
|
|
#include "fse.h" /* header compression */
|
2016-06-04 22:42:28 +00:00
|
|
|
#define HUF_STATIC_LINKING_ONLY
|
|
|
|
#include "huf.h"
|
2017-08-15 18:23:28 +00:00
|
|
|
#include "error_private.h"
|
2015-10-18 21:18:32 +00:00
|
|
|
|
|
|
|
|
2016-01-06 00:58:37 +00:00
|
|
|
/* **************************************************************
|
2015-10-18 21:18:32 +00:00
|
|
|
* Error Management
|
|
|
|
****************************************************************/
|
2017-08-15 18:23:28 +00:00
|
|
|
#define HUF_isError ERR_isError
|
2016-01-06 00:58:37 +00:00
|
|
|
#define HUF_STATIC_ASSERT(c) { enum { HUF_static_assert = 1/(int)(!!(c)) }; } /* use only *after* variable declarations */
|
2018-02-22 07:52:45 +00:00
|
|
|
#define CHECK_F(f) { size_t const err_ = (f); if (HUF_isError(err_)) return err_; }
|
2015-10-18 21:18:32 +00:00
|
|
|
|
|
|
|
|
2017-06-29 18:49:59 +00:00
|
|
|
/* **************************************************************
|
|
|
|
* Byte alignment for workSpace management
|
|
|
|
****************************************************************/
|
2018-02-22 07:52:45 +00:00
|
|
|
#define HUF_ALIGN(x, a) HUF_ALIGN_MASK((x), (a) - 1)
|
2017-06-30 16:38:11 +00:00
|
|
|
#define HUF_ALIGN_MASK(x, mask) (((x) + (mask)) & ~(mask))
|
2017-06-29 18:49:59 +00:00
|
|
|
|
2018-02-22 07:52:45 +00:00
|
|
|
|
2016-06-10 22:23:43 +00:00
|
|
|
/*-***************************/
|
|
|
|
/* generic DTableDesc */
|
|
|
|
/*-***************************/
|
|
|
|
typedef struct { BYTE maxTableLog; BYTE tableType; BYTE tableLog; BYTE reserved; } DTableDesc;
|
|
|
|
|
|
|
|
static DTableDesc HUF_getDTableDesc(const HUF_DTable* table)
|
|
|
|
{
|
|
|
|
DTableDesc dtd;
|
|
|
|
memcpy(&dtd, table, sizeof(dtd));
|
|
|
|
return dtd;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-01-25 03:10:46 +00:00
|
|
|
/*-***************************/
|
|
|
|
/* single-symbol decoding */
|
|
|
|
/*-***************************/
|
2016-06-08 09:11:02 +00:00
|
|
|
typedef struct { BYTE byte; BYTE nbBits; } HUF_DEltX2; /* single-symbol decoding */
|
2015-10-18 21:18:32 +00:00
|
|
|
|
2017-06-29 18:49:59 +00:00
|
|
|
size_t HUF_readDTableX2_wksp(HUF_DTable* DTable, const void* src, size_t srcSize, void* workSpace, size_t wkspSize)
|
2015-10-18 21:18:32 +00:00
|
|
|
{
|
|
|
|
U32 tableLog = 0;
|
|
|
|
U32 nbSymbols = 0;
|
2016-06-10 22:23:43 +00:00
|
|
|
size_t iSize;
|
2016-01-06 00:58:37 +00:00
|
|
|
void* const dtPtr = DTable + 1;
|
|
|
|
HUF_DEltX2* const dt = (HUF_DEltX2*)dtPtr;
|
2015-10-18 21:18:32 +00:00
|
|
|
|
2017-06-29 19:39:34 +00:00
|
|
|
U32* rankVal;
|
|
|
|
BYTE* huffWeight;
|
|
|
|
size_t spaceUsed32 = 0;
|
2017-06-29 18:49:59 +00:00
|
|
|
|
2017-06-29 19:39:34 +00:00
|
|
|
rankVal = (U32 *)workSpace + spaceUsed32;
|
|
|
|
spaceUsed32 += HUF_TABLELOG_ABSOLUTEMAX + 1;
|
|
|
|
huffWeight = (BYTE *)((U32 *)workSpace + spaceUsed32);
|
2017-06-30 16:38:11 +00:00
|
|
|
spaceUsed32 += HUF_ALIGN(HUF_SYMBOLVALUE_MAX + 1, sizeof(U32)) >> 2;
|
2017-06-29 18:49:59 +00:00
|
|
|
|
2018-02-22 23:44:26 +00:00
|
|
|
if ((spaceUsed32 << 2) > wkspSize) return ERROR(tableLog_tooLarge);
|
2017-06-29 18:49:59 +00:00
|
|
|
|
2016-06-10 22:23:43 +00:00
|
|
|
HUF_STATIC_ASSERT(sizeof(DTableDesc) == sizeof(HUF_DTable));
|
2016-08-19 17:38:19 +00:00
|
|
|
/* memset(huffWeight, 0, sizeof(huffWeight)); */ /* is not necessary, even though some analyzer complain ... */
|
2015-10-18 21:18:32 +00:00
|
|
|
|
2016-05-20 12:36:36 +00:00
|
|
|
iSize = HUF_readStats(huffWeight, HUF_SYMBOLVALUE_MAX + 1, rankVal, &nbSymbols, &tableLog, src, srcSize);
|
2015-10-18 21:18:32 +00:00
|
|
|
if (HUF_isError(iSize)) return iSize;
|
|
|
|
|
2016-06-10 22:23:43 +00:00
|
|
|
/* Table header */
|
|
|
|
{ DTableDesc dtd = HUF_getDTableDesc(DTable);
|
2017-02-20 20:08:59 +00:00
|
|
|
if (tableLog > (U32)(dtd.maxTableLog+1)) return ERROR(tableLog_tooLarge); /* DTable too small, Huffman tree cannot fit in */
|
2016-06-10 22:23:43 +00:00
|
|
|
dtd.tableType = 0;
|
|
|
|
dtd.tableLog = (BYTE)tableLog;
|
|
|
|
memcpy(DTable, &dtd, sizeof(dtd));
|
|
|
|
}
|
2015-10-18 21:18:32 +00:00
|
|
|
|
2017-03-06 05:07:20 +00:00
|
|
|
/* Calculate starting value for each rank */
|
2016-06-10 22:23:43 +00:00
|
|
|
{ U32 n, nextRankStart = 0;
|
|
|
|
for (n=1; n<tableLog+1; n++) {
|
2017-03-06 05:07:20 +00:00
|
|
|
U32 const current = nextRankStart;
|
2016-06-10 22:23:43 +00:00
|
|
|
nextRankStart += (rankVal[n] << (n-1));
|
|
|
|
rankVal[n] = current;
|
|
|
|
} }
|
2015-10-18 21:18:32 +00:00
|
|
|
|
|
|
|
/* fill DTable */
|
2016-06-10 22:23:43 +00:00
|
|
|
{ U32 n;
|
|
|
|
for (n=0; n<nbSymbols; n++) {
|
|
|
|
U32 const w = huffWeight[n];
|
|
|
|
U32 const length = (1 << w) >> 1;
|
2017-03-06 05:07:20 +00:00
|
|
|
U32 u;
|
2016-06-10 22:23:43 +00:00
|
|
|
HUF_DEltX2 D;
|
|
|
|
D.byte = (BYTE)n; D.nbBits = (BYTE)(tableLog + 1 - w);
|
2017-03-06 05:07:20 +00:00
|
|
|
for (u = rankVal[w]; u < rankVal[w] + length; u++)
|
|
|
|
dt[u] = D;
|
2016-06-10 22:23:43 +00:00
|
|
|
rankVal[w] += length;
|
|
|
|
} }
|
2015-10-18 21:18:32 +00:00
|
|
|
|
|
|
|
return iSize;
|
|
|
|
}
|
|
|
|
|
2017-06-29 18:49:59 +00:00
|
|
|
size_t HUF_readDTableX2(HUF_DTable* DTable, const void* src, size_t srcSize)
|
|
|
|
{
|
|
|
|
U32 workSpace[HUF_DECOMPRESS_WORKSPACE_SIZE_U32];
|
|
|
|
return HUF_readDTableX2_wksp(DTable, src, srcSize,
|
|
|
|
workSpace, sizeof(workSpace));
|
|
|
|
}
|
|
|
|
|
2018-02-03 02:03:09 +00:00
|
|
|
typedef struct { U16 sequence; BYTE nbBits; BYTE length; } HUF_DEltX4; /* double-symbols decoding */
|
2016-03-20 04:40:39 +00:00
|
|
|
|
2018-03-01 19:28:42 +00:00
|
|
|
FORCE_INLINE_TEMPLATE BYTE
|
|
|
|
HUF_decodeSymbolX2(BIT_DStream_t* Dstream, const HUF_DEltX2* dt, const U32 dtLog)
|
2018-02-22 07:52:45 +00:00
|
|
|
{
|
|
|
|
size_t const val = BIT_lookBitsFast(Dstream, dtLog); /* note : dtLog >= 1 */
|
|
|
|
BYTE const c = dt[val].byte;
|
|
|
|
BIT_skipBits(Dstream, dt[val].nbBits);
|
|
|
|
return c;
|
|
|
|
}
|
|
|
|
|
|
|
|
#define HUF_DECODE_SYMBOLX2_0(ptr, DStreamPtr) \
|
|
|
|
*ptr++ = HUF_decodeSymbolX2(DStreamPtr, dt, dtLog)
|
2018-02-03 02:03:09 +00:00
|
|
|
|
2018-02-22 07:52:45 +00:00
|
|
|
#define HUF_DECODE_SYMBOLX2_1(ptr, DStreamPtr) \
|
|
|
|
if (MEM_64bits() || (HUF_TABLELOG_MAX<=12)) \
|
|
|
|
HUF_DECODE_SYMBOLX2_0(ptr, DStreamPtr)
|
2018-02-03 02:03:09 +00:00
|
|
|
|
2018-02-22 07:52:45 +00:00
|
|
|
#define HUF_DECODE_SYMBOLX2_2(ptr, DStreamPtr) \
|
|
|
|
if (MEM_64bits()) \
|
|
|
|
HUF_DECODE_SYMBOLX2_0(ptr, DStreamPtr)
|
|
|
|
|
|
|
|
HINT_INLINE size_t
|
|
|
|
HUF_decodeStreamX2(BYTE* p, BIT_DStream_t* const bitDPtr, BYTE* const pEnd, const HUF_DEltX2* const dt, const U32 dtLog)
|
|
|
|
{
|
|
|
|
BYTE* const pStart = p;
|
|
|
|
|
|
|
|
/* up to 4 symbols at a time */
|
2018-02-26 22:52:23 +00:00
|
|
|
while ((BIT_reloadDStream(bitDPtr) == BIT_DStream_unfinished) & (p < pEnd-3)) {
|
2018-02-22 07:52:45 +00:00
|
|
|
HUF_DECODE_SYMBOLX2_2(p, bitDPtr);
|
|
|
|
HUF_DECODE_SYMBOLX2_1(p, bitDPtr);
|
|
|
|
HUF_DECODE_SYMBOLX2_2(p, bitDPtr);
|
|
|
|
HUF_DECODE_SYMBOLX2_0(p, bitDPtr);
|
|
|
|
}
|
|
|
|
|
2018-02-26 22:52:23 +00:00
|
|
|
/* [0-3] symbols remaining */
|
|
|
|
if (MEM_32bits())
|
|
|
|
while ((BIT_reloadDStream(bitDPtr) == BIT_DStream_unfinished) & (p < pEnd))
|
|
|
|
HUF_DECODE_SYMBOLX2_0(p, bitDPtr);
|
2018-02-22 07:52:45 +00:00
|
|
|
|
2018-02-26 22:52:23 +00:00
|
|
|
/* no more data to retrieve from bitstream, no need to reload */
|
2018-02-22 07:52:45 +00:00
|
|
|
while (p < pEnd)
|
|
|
|
HUF_DECODE_SYMBOLX2_0(p, bitDPtr);
|
|
|
|
|
|
|
|
return pEnd-pStart;
|
|
|
|
}
|
|
|
|
|
|
|
|
FORCE_INLINE_TEMPLATE size_t
|
|
|
|
HUF_decompress1X2_usingDTable_internal_body(
|
|
|
|
void* dst, size_t dstSize,
|
|
|
|
const void* cSrc, size_t cSrcSize,
|
|
|
|
const HUF_DTable* DTable)
|
|
|
|
{
|
|
|
|
BYTE* op = (BYTE*)dst;
|
|
|
|
BYTE* const oend = op + dstSize;
|
|
|
|
const void* dtPtr = DTable + 1;
|
|
|
|
const HUF_DEltX2* const dt = (const HUF_DEltX2*)dtPtr;
|
|
|
|
BIT_DStream_t bitD;
|
|
|
|
DTableDesc const dtd = HUF_getDTableDesc(DTable);
|
|
|
|
U32 const dtLog = dtd.tableLog;
|
|
|
|
|
|
|
|
CHECK_F( BIT_initDStream(&bitD, cSrc, cSrcSize) );
|
|
|
|
|
|
|
|
HUF_decodeStreamX2(op, &bitD, oend, dt, dtLog);
|
|
|
|
|
|
|
|
if (!BIT_endOfDStream(&bitD)) return ERROR(corruption_detected);
|
|
|
|
|
|
|
|
return dstSize;
|
|
|
|
}
|
|
|
|
|
|
|
|
FORCE_INLINE_TEMPLATE size_t
|
|
|
|
HUF_decompress4X2_usingDTable_internal_body(
|
|
|
|
void* dst, size_t dstSize,
|
|
|
|
const void* cSrc, size_t cSrcSize,
|
|
|
|
const HUF_DTable* DTable)
|
|
|
|
{
|
|
|
|
/* Check */
|
|
|
|
if (cSrcSize < 10) return ERROR(corruption_detected); /* strict minimum : jump table + 1 byte per stream */
|
|
|
|
|
|
|
|
{ const BYTE* const istart = (const BYTE*) cSrc;
|
|
|
|
BYTE* const ostart = (BYTE*) dst;
|
|
|
|
BYTE* const oend = ostart + dstSize;
|
|
|
|
const void* const dtPtr = DTable + 1;
|
|
|
|
const HUF_DEltX2* const dt = (const HUF_DEltX2*)dtPtr;
|
|
|
|
|
|
|
|
/* Init */
|
|
|
|
BIT_DStream_t bitD1;
|
|
|
|
BIT_DStream_t bitD2;
|
|
|
|
BIT_DStream_t bitD3;
|
|
|
|
BIT_DStream_t bitD4;
|
|
|
|
size_t const length1 = MEM_readLE16(istart);
|
|
|
|
size_t const length2 = MEM_readLE16(istart+2);
|
|
|
|
size_t const length3 = MEM_readLE16(istart+4);
|
|
|
|
size_t const length4 = cSrcSize - (length1 + length2 + length3 + 6);
|
|
|
|
const BYTE* const istart1 = istart + 6; /* jumpTable */
|
|
|
|
const BYTE* const istart2 = istart1 + length1;
|
|
|
|
const BYTE* const istart3 = istart2 + length2;
|
|
|
|
const BYTE* const istart4 = istart3 + length3;
|
|
|
|
const size_t segmentSize = (dstSize+3) / 4;
|
|
|
|
BYTE* const opStart2 = ostart + segmentSize;
|
|
|
|
BYTE* const opStart3 = opStart2 + segmentSize;
|
|
|
|
BYTE* const opStart4 = opStart3 + segmentSize;
|
|
|
|
BYTE* op1 = ostart;
|
|
|
|
BYTE* op2 = opStart2;
|
|
|
|
BYTE* op3 = opStart3;
|
|
|
|
BYTE* op4 = opStart4;
|
|
|
|
U32 endSignal = BIT_DStream_unfinished;
|
|
|
|
DTableDesc const dtd = HUF_getDTableDesc(DTable);
|
|
|
|
U32 const dtLog = dtd.tableLog;
|
|
|
|
|
|
|
|
if (length4 > cSrcSize) return ERROR(corruption_detected); /* overflow */
|
|
|
|
CHECK_F( BIT_initDStream(&bitD1, istart1, length1) );
|
|
|
|
CHECK_F( BIT_initDStream(&bitD2, istart2, length2) );
|
|
|
|
CHECK_F( BIT_initDStream(&bitD3, istart3, length3) );
|
|
|
|
CHECK_F( BIT_initDStream(&bitD4, istart4, length4) );
|
|
|
|
|
|
|
|
/* up to 16 symbols per loop (4 symbols per stream) in 64-bit mode */
|
|
|
|
endSignal = BIT_reloadDStream(&bitD1) | BIT_reloadDStream(&bitD2) | BIT_reloadDStream(&bitD3) | BIT_reloadDStream(&bitD4);
|
|
|
|
while ( (endSignal==BIT_DStream_unfinished) && (op4<(oend-3)) ) {
|
|
|
|
HUF_DECODE_SYMBOLX2_2(op1, &bitD1);
|
|
|
|
HUF_DECODE_SYMBOLX2_2(op2, &bitD2);
|
|
|
|
HUF_DECODE_SYMBOLX2_2(op3, &bitD3);
|
|
|
|
HUF_DECODE_SYMBOLX2_2(op4, &bitD4);
|
|
|
|
HUF_DECODE_SYMBOLX2_1(op1, &bitD1);
|
|
|
|
HUF_DECODE_SYMBOLX2_1(op2, &bitD2);
|
|
|
|
HUF_DECODE_SYMBOLX2_1(op3, &bitD3);
|
|
|
|
HUF_DECODE_SYMBOLX2_1(op4, &bitD4);
|
|
|
|
HUF_DECODE_SYMBOLX2_2(op1, &bitD1);
|
|
|
|
HUF_DECODE_SYMBOLX2_2(op2, &bitD2);
|
|
|
|
HUF_DECODE_SYMBOLX2_2(op3, &bitD3);
|
|
|
|
HUF_DECODE_SYMBOLX2_2(op4, &bitD4);
|
|
|
|
HUF_DECODE_SYMBOLX2_0(op1, &bitD1);
|
|
|
|
HUF_DECODE_SYMBOLX2_0(op2, &bitD2);
|
|
|
|
HUF_DECODE_SYMBOLX2_0(op3, &bitD3);
|
|
|
|
HUF_DECODE_SYMBOLX2_0(op4, &bitD4);
|
|
|
|
BIT_reloadDStream(&bitD1);
|
|
|
|
BIT_reloadDStream(&bitD2);
|
|
|
|
BIT_reloadDStream(&bitD3);
|
|
|
|
BIT_reloadDStream(&bitD4);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* check corruption */
|
|
|
|
/* note : should not be necessary : op# advance in lock step, and we control op4.
|
2018-02-26 22:52:23 +00:00
|
|
|
* but curiously, binary generated by gcc 7.2 & 7.3 with -mbmi2 runs faster when >=1 test is present */
|
2018-02-22 07:52:45 +00:00
|
|
|
if (op1 > opStart2) return ERROR(corruption_detected);
|
|
|
|
if (op2 > opStart3) return ERROR(corruption_detected);
|
|
|
|
if (op3 > opStart4) return ERROR(corruption_detected);
|
|
|
|
/* note : op4 supposed already verified within main loop */
|
|
|
|
|
|
|
|
/* finish bitStreams one by one */
|
|
|
|
HUF_decodeStreamX2(op1, &bitD1, opStart2, dt, dtLog);
|
|
|
|
HUF_decodeStreamX2(op2, &bitD2, opStart3, dt, dtLog);
|
|
|
|
HUF_decodeStreamX2(op3, &bitD3, opStart4, dt, dtLog);
|
|
|
|
HUF_decodeStreamX2(op4, &bitD4, oend, dt, dtLog);
|
|
|
|
|
|
|
|
/* check */
|
|
|
|
{ U32 const endCheck = BIT_endOfDStream(&bitD1) & BIT_endOfDStream(&bitD2) & BIT_endOfDStream(&bitD3) & BIT_endOfDStream(&bitD4);
|
|
|
|
if (!endCheck) return ERROR(corruption_detected); }
|
|
|
|
|
|
|
|
/* decoded size */
|
|
|
|
return dstSize;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-03-01 19:28:42 +00:00
|
|
|
FORCE_INLINE_TEMPLATE U32
|
|
|
|
HUF_decodeSymbolX4(void* op, BIT_DStream_t* DStream, const HUF_DEltX4* dt, const U32 dtLog)
|
2018-02-22 07:52:45 +00:00
|
|
|
{
|
|
|
|
size_t const val = BIT_lookBitsFast(DStream, dtLog); /* note : dtLog >= 1 */
|
|
|
|
memcpy(op, dt+val, 2);
|
|
|
|
BIT_skipBits(DStream, dt[val].nbBits);
|
|
|
|
return dt[val].length;
|
|
|
|
}
|
|
|
|
|
|
|
|
FORCE_INLINE_TEMPLATE U32
|
|
|
|
HUF_decodeLastSymbolX4(void* op, BIT_DStream_t* DStream, const HUF_DEltX4* dt, const U32 dtLog)
|
|
|
|
{
|
|
|
|
size_t const val = BIT_lookBitsFast(DStream, dtLog); /* note : dtLog >= 1 */
|
|
|
|
memcpy(op, dt+val, 1);
|
|
|
|
if (dt[val].length==1) BIT_skipBits(DStream, dt[val].nbBits);
|
|
|
|
else {
|
|
|
|
if (DStream->bitsConsumed < (sizeof(DStream->bitContainer)*8)) {
|
|
|
|
BIT_skipBits(DStream, dt[val].nbBits);
|
|
|
|
if (DStream->bitsConsumed > (sizeof(DStream->bitContainer)*8))
|
|
|
|
/* ugly hack; works only because it's the last symbol. Note : can't easily extract nbBits from just this symbol */
|
|
|
|
DStream->bitsConsumed = (sizeof(DStream->bitContainer)*8);
|
|
|
|
} }
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
#define HUF_DECODE_SYMBOLX4_0(ptr, DStreamPtr) \
|
|
|
|
ptr += HUF_decodeSymbolX4(ptr, DStreamPtr, dt, dtLog)
|
|
|
|
|
|
|
|
#define HUF_DECODE_SYMBOLX4_1(ptr, DStreamPtr) \
|
|
|
|
if (MEM_64bits() || (HUF_TABLELOG_MAX<=12)) \
|
|
|
|
ptr += HUF_decodeSymbolX4(ptr, DStreamPtr, dt, dtLog)
|
|
|
|
|
|
|
|
#define HUF_DECODE_SYMBOLX4_2(ptr, DStreamPtr) \
|
|
|
|
if (MEM_64bits()) \
|
|
|
|
ptr += HUF_decodeSymbolX4(ptr, DStreamPtr, dt, dtLog)
|
|
|
|
|
|
|
|
HINT_INLINE size_t
|
|
|
|
HUF_decodeStreamX4(BYTE* p, BIT_DStream_t* bitDPtr, BYTE* const pEnd,
|
|
|
|
const HUF_DEltX4* const dt, const U32 dtLog)
|
|
|
|
{
|
|
|
|
BYTE* const pStart = p;
|
|
|
|
|
|
|
|
/* up to 8 symbols at a time */
|
|
|
|
while ((BIT_reloadDStream(bitDPtr) == BIT_DStream_unfinished) & (p < pEnd-(sizeof(bitDPtr->bitContainer)-1))) {
|
|
|
|
HUF_DECODE_SYMBOLX4_2(p, bitDPtr);
|
|
|
|
HUF_DECODE_SYMBOLX4_1(p, bitDPtr);
|
|
|
|
HUF_DECODE_SYMBOLX4_2(p, bitDPtr);
|
|
|
|
HUF_DECODE_SYMBOLX4_0(p, bitDPtr);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* closer to end : up to 2 symbols at a time */
|
|
|
|
while ((BIT_reloadDStream(bitDPtr) == BIT_DStream_unfinished) & (p <= pEnd-2))
|
|
|
|
HUF_DECODE_SYMBOLX4_0(p, bitDPtr);
|
|
|
|
|
|
|
|
while (p <= pEnd-2)
|
|
|
|
HUF_DECODE_SYMBOLX4_0(p, bitDPtr); /* no need to reload : reached the end of DStream */
|
|
|
|
|
|
|
|
if (p < pEnd)
|
|
|
|
p += HUF_decodeLastSymbolX4(p, bitDPtr, dt, dtLog);
|
|
|
|
|
|
|
|
return p-pStart;
|
|
|
|
}
|
|
|
|
|
|
|
|
FORCE_INLINE_TEMPLATE size_t
|
|
|
|
HUF_decompress1X4_usingDTable_internal_body(
|
|
|
|
void* dst, size_t dstSize,
|
|
|
|
const void* cSrc, size_t cSrcSize,
|
|
|
|
const HUF_DTable* DTable)
|
|
|
|
{
|
|
|
|
BIT_DStream_t bitD;
|
|
|
|
|
|
|
|
/* Init */
|
|
|
|
CHECK_F( BIT_initDStream(&bitD, cSrc, cSrcSize) );
|
|
|
|
|
|
|
|
/* decode */
|
|
|
|
{ BYTE* const ostart = (BYTE*) dst;
|
|
|
|
BYTE* const oend = ostart + dstSize;
|
|
|
|
const void* const dtPtr = DTable+1; /* force compiler to not use strict-aliasing */
|
|
|
|
const HUF_DEltX4* const dt = (const HUF_DEltX4*)dtPtr;
|
|
|
|
DTableDesc const dtd = HUF_getDTableDesc(DTable);
|
|
|
|
HUF_decodeStreamX4(ostart, &bitD, oend, dt, dtd.tableLog);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* check */
|
|
|
|
if (!BIT_endOfDStream(&bitD)) return ERROR(corruption_detected);
|
|
|
|
|
|
|
|
/* decoded size */
|
|
|
|
return dstSize;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
FORCE_INLINE_TEMPLATE size_t
|
|
|
|
HUF_decompress4X4_usingDTable_internal_body(
|
|
|
|
void* dst, size_t dstSize,
|
|
|
|
const void* cSrc, size_t cSrcSize,
|
|
|
|
const HUF_DTable* DTable)
|
|
|
|
{
|
|
|
|
if (cSrcSize < 10) return ERROR(corruption_detected); /* strict minimum : jump table + 1 byte per stream */
|
|
|
|
|
|
|
|
{ const BYTE* const istart = (const BYTE*) cSrc;
|
|
|
|
BYTE* const ostart = (BYTE*) dst;
|
|
|
|
BYTE* const oend = ostart + dstSize;
|
|
|
|
const void* const dtPtr = DTable+1;
|
|
|
|
const HUF_DEltX4* const dt = (const HUF_DEltX4*)dtPtr;
|
|
|
|
|
|
|
|
/* Init */
|
|
|
|
BIT_DStream_t bitD1;
|
|
|
|
BIT_DStream_t bitD2;
|
|
|
|
BIT_DStream_t bitD3;
|
|
|
|
BIT_DStream_t bitD4;
|
|
|
|
size_t const length1 = MEM_readLE16(istart);
|
|
|
|
size_t const length2 = MEM_readLE16(istart+2);
|
|
|
|
size_t const length3 = MEM_readLE16(istart+4);
|
|
|
|
size_t const length4 = cSrcSize - (length1 + length2 + length3 + 6);
|
|
|
|
const BYTE* const istart1 = istart + 6; /* jumpTable */
|
|
|
|
const BYTE* const istart2 = istart1 + length1;
|
|
|
|
const BYTE* const istart3 = istart2 + length2;
|
|
|
|
const BYTE* const istart4 = istart3 + length3;
|
|
|
|
size_t const segmentSize = (dstSize+3) / 4;
|
|
|
|
BYTE* const opStart2 = ostart + segmentSize;
|
|
|
|
BYTE* const opStart3 = opStart2 + segmentSize;
|
|
|
|
BYTE* const opStart4 = opStart3 + segmentSize;
|
|
|
|
BYTE* op1 = ostart;
|
|
|
|
BYTE* op2 = opStart2;
|
|
|
|
BYTE* op3 = opStart3;
|
|
|
|
BYTE* op4 = opStart4;
|
|
|
|
U32 endSignal;
|
|
|
|
DTableDesc const dtd = HUF_getDTableDesc(DTable);
|
|
|
|
U32 const dtLog = dtd.tableLog;
|
|
|
|
|
|
|
|
if (length4 > cSrcSize) return ERROR(corruption_detected); /* overflow */
|
|
|
|
CHECK_F( BIT_initDStream(&bitD1, istart1, length1) );
|
|
|
|
CHECK_F( BIT_initDStream(&bitD2, istart2, length2) );
|
|
|
|
CHECK_F( BIT_initDStream(&bitD3, istart3, length3) );
|
|
|
|
CHECK_F( BIT_initDStream(&bitD4, istart4, length4) );
|
|
|
|
|
|
|
|
/* 16-32 symbols per loop (4-8 symbols per stream) */
|
|
|
|
endSignal = BIT_reloadDStream(&bitD1) | BIT_reloadDStream(&bitD2) | BIT_reloadDStream(&bitD3) | BIT_reloadDStream(&bitD4);
|
|
|
|
for ( ; (endSignal==BIT_DStream_unfinished) & (op4<(oend-(sizeof(bitD4.bitContainer)-1))) ; ) {
|
|
|
|
HUF_DECODE_SYMBOLX4_2(op1, &bitD1);
|
|
|
|
HUF_DECODE_SYMBOLX4_2(op2, &bitD2);
|
|
|
|
HUF_DECODE_SYMBOLX4_2(op3, &bitD3);
|
|
|
|
HUF_DECODE_SYMBOLX4_2(op4, &bitD4);
|
|
|
|
HUF_DECODE_SYMBOLX4_1(op1, &bitD1);
|
|
|
|
HUF_DECODE_SYMBOLX4_1(op2, &bitD2);
|
|
|
|
HUF_DECODE_SYMBOLX4_1(op3, &bitD3);
|
|
|
|
HUF_DECODE_SYMBOLX4_1(op4, &bitD4);
|
|
|
|
HUF_DECODE_SYMBOLX4_2(op1, &bitD1);
|
|
|
|
HUF_DECODE_SYMBOLX4_2(op2, &bitD2);
|
|
|
|
HUF_DECODE_SYMBOLX4_2(op3, &bitD3);
|
|
|
|
HUF_DECODE_SYMBOLX4_2(op4, &bitD4);
|
|
|
|
HUF_DECODE_SYMBOLX4_0(op1, &bitD1);
|
|
|
|
HUF_DECODE_SYMBOLX4_0(op2, &bitD2);
|
|
|
|
HUF_DECODE_SYMBOLX4_0(op3, &bitD3);
|
|
|
|
HUF_DECODE_SYMBOLX4_0(op4, &bitD4);
|
|
|
|
|
|
|
|
endSignal = BIT_reloadDStream(&bitD1) | BIT_reloadDStream(&bitD2) | BIT_reloadDStream(&bitD3) | BIT_reloadDStream(&bitD4);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* check corruption */
|
|
|
|
if (op1 > opStart2) return ERROR(corruption_detected);
|
|
|
|
if (op2 > opStart3) return ERROR(corruption_detected);
|
|
|
|
if (op3 > opStart4) return ERROR(corruption_detected);
|
|
|
|
/* note : op4 already verified within main loop */
|
|
|
|
|
|
|
|
/* finish bitStreams one by one */
|
|
|
|
HUF_decodeStreamX4(op1, &bitD1, opStart2, dt, dtLog);
|
|
|
|
HUF_decodeStreamX4(op2, &bitD2, opStart3, dt, dtLog);
|
|
|
|
HUF_decodeStreamX4(op3, &bitD3, opStart4, dt, dtLog);
|
|
|
|
HUF_decodeStreamX4(op4, &bitD4, oend, dt, dtLog);
|
|
|
|
|
|
|
|
/* check */
|
|
|
|
{ U32 const endCheck = BIT_endOfDStream(&bitD1) & BIT_endOfDStream(&bitD2) & BIT_endOfDStream(&bitD3) & BIT_endOfDStream(&bitD4);
|
|
|
|
if (!endCheck) return ERROR(corruption_detected); }
|
|
|
|
|
|
|
|
/* decoded size */
|
|
|
|
return dstSize;
|
|
|
|
}
|
|
|
|
}
|
2018-02-03 02:03:09 +00:00
|
|
|
|
|
|
|
|
|
|
|
typedef size_t (*HUF_decompress_usingDTable_t)(void *dst, size_t dstSize,
|
|
|
|
const void *cSrc,
|
|
|
|
size_t cSrcSize,
|
|
|
|
const HUF_DTable *DTable);
|
|
|
|
#if DYNAMIC_BMI2
|
2018-02-22 07:52:45 +00:00
|
|
|
|
|
|
|
#define X(fn) \
|
|
|
|
\
|
|
|
|
static size_t fn##_default( \
|
|
|
|
void* dst, size_t dstSize, \
|
|
|
|
const void* cSrc, size_t cSrcSize, \
|
|
|
|
const HUF_DTable* DTable) \
|
|
|
|
{ \
|
|
|
|
return fn##_body(dst, dstSize, cSrc, cSrcSize, DTable); \
|
|
|
|
} \
|
|
|
|
\
|
|
|
|
static TARGET_ATTRIBUTE("bmi2") size_t fn##_bmi2( \
|
|
|
|
void* dst, size_t dstSize, \
|
|
|
|
const void* cSrc, size_t cSrcSize, \
|
|
|
|
const HUF_DTable* DTable) \
|
|
|
|
{ \
|
|
|
|
return fn##_body(dst, dstSize, cSrc, cSrcSize, DTable); \
|
|
|
|
} \
|
|
|
|
\
|
|
|
|
static size_t fn(void* dst, size_t dstSize, void const* cSrc, \
|
|
|
|
size_t cSrcSize, HUF_DTable const* DTable, int bmi2) \
|
|
|
|
{ \
|
|
|
|
if (bmi2) { \
|
|
|
|
return fn##_bmi2(dst, dstSize, cSrc, cSrcSize, DTable); \
|
|
|
|
} \
|
|
|
|
return fn##_default(dst, dstSize, cSrc, cSrcSize, DTable); \
|
2015-10-18 21:18:32 +00:00
|
|
|
}
|
2018-02-22 07:52:45 +00:00
|
|
|
|
2018-02-03 02:03:09 +00:00
|
|
|
#else
|
2018-02-22 07:52:45 +00:00
|
|
|
|
|
|
|
#define X(fn) \
|
|
|
|
static size_t fn(void* dst, size_t dstSize, void const* cSrc, \
|
|
|
|
size_t cSrcSize, HUF_DTable const* DTable, int bmi2) \
|
|
|
|
{ \
|
|
|
|
(void)bmi2; \
|
|
|
|
return fn##_body(dst, dstSize, cSrc, cSrcSize, DTable); \
|
2018-02-03 02:03:09 +00:00
|
|
|
}
|
2018-02-22 07:52:45 +00:00
|
|
|
|
2018-02-03 02:03:09 +00:00
|
|
|
#endif
|
2015-10-18 21:18:32 +00:00
|
|
|
|
2018-02-03 02:03:09 +00:00
|
|
|
X(HUF_decompress1X2_usingDTable_internal)
|
|
|
|
X(HUF_decompress4X2_usingDTable_internal)
|
|
|
|
X(HUF_decompress1X4_usingDTable_internal)
|
|
|
|
X(HUF_decompress4X4_usingDTable_internal)
|
2015-10-18 21:18:32 +00:00
|
|
|
|
2018-02-03 02:03:09 +00:00
|
|
|
#undef X
|
2015-10-18 21:18:32 +00:00
|
|
|
|
2018-02-22 07:52:45 +00:00
|
|
|
|
2016-06-10 22:23:43 +00:00
|
|
|
size_t HUF_decompress1X2_usingDTable(
|
|
|
|
void* dst, size_t dstSize,
|
|
|
|
const void* cSrc, size_t cSrcSize,
|
|
|
|
const HUF_DTable* DTable)
|
|
|
|
{
|
|
|
|
DTableDesc dtd = HUF_getDTableDesc(DTable);
|
|
|
|
if (dtd.tableType != 0) return ERROR(GENERIC);
|
2018-02-03 02:03:09 +00:00
|
|
|
return HUF_decompress1X2_usingDTable_internal(dst, dstSize, cSrc, cSrcSize, DTable, /* bmi2 */ 0);
|
2016-06-10 22:23:43 +00:00
|
|
|
}
|
|
|
|
|
2017-06-29 18:49:59 +00:00
|
|
|
size_t HUF_decompress1X2_DCtx_wksp(HUF_DTable* DCtx, void* dst, size_t dstSize,
|
|
|
|
const void* cSrc, size_t cSrcSize,
|
|
|
|
void* workSpace, size_t wkspSize)
|
2015-10-18 21:18:32 +00:00
|
|
|
{
|
|
|
|
const BYTE* ip = (const BYTE*) cSrc;
|
|
|
|
|
2017-06-29 18:49:59 +00:00
|
|
|
size_t const hSize = HUF_readDTableX2_wksp(DCtx, cSrc, cSrcSize, workSpace, wkspSize);
|
2016-06-10 22:23:43 +00:00
|
|
|
if (HUF_isError(hSize)) return hSize;
|
|
|
|
if (hSize >= cSrcSize) return ERROR(srcSize_wrong);
|
|
|
|
ip += hSize; cSrcSize -= hSize;
|
2015-10-18 21:18:32 +00:00
|
|
|
|
2018-02-03 02:03:09 +00:00
|
|
|
return HUF_decompress1X2_usingDTable_internal(dst, dstSize, ip, cSrcSize, DCtx, /* bmi2 */ 0);
|
2016-06-10 23:46:03 +00:00
|
|
|
}
|
|
|
|
|
2017-06-29 18:49:59 +00:00
|
|
|
|
|
|
|
size_t HUF_decompress1X2_DCtx(HUF_DTable* DCtx, void* dst, size_t dstSize,
|
|
|
|
const void* cSrc, size_t cSrcSize)
|
|
|
|
{
|
|
|
|
U32 workSpace[HUF_DECOMPRESS_WORKSPACE_SIZE_U32];
|
|
|
|
return HUF_decompress1X2_DCtx_wksp(DCtx, dst, dstSize, cSrc, cSrcSize,
|
|
|
|
workSpace, sizeof(workSpace));
|
|
|
|
}
|
|
|
|
|
2016-06-10 23:46:03 +00:00
|
|
|
size_t HUF_decompress1X2 (void* dst, size_t dstSize, const void* cSrc, size_t cSrcSize)
|
|
|
|
{
|
|
|
|
HUF_CREATE_STATIC_DTABLEX2(DTable, HUF_TABLELOG_MAX);
|
|
|
|
return HUF_decompress1X2_DCtx (DTable, dst, dstSize, cSrc, cSrcSize);
|
2015-10-18 21:18:32 +00:00
|
|
|
}
|
|
|
|
|
2016-06-10 22:23:43 +00:00
|
|
|
size_t HUF_decompress4X2_usingDTable(
|
|
|
|
void* dst, size_t dstSize,
|
|
|
|
const void* cSrc, size_t cSrcSize,
|
|
|
|
const HUF_DTable* DTable)
|
|
|
|
{
|
|
|
|
DTableDesc dtd = HUF_getDTableDesc(DTable);
|
|
|
|
if (dtd.tableType != 0) return ERROR(GENERIC);
|
2018-02-03 02:03:09 +00:00
|
|
|
return HUF_decompress4X2_usingDTable_internal(dst, dstSize, cSrc, cSrcSize, DTable, /* bmi2 */ 0);
|
2016-06-10 22:23:43 +00:00
|
|
|
}
|
|
|
|
|
2018-02-03 02:03:09 +00:00
|
|
|
static size_t HUF_decompress4X2_DCtx_wksp_bmi2(HUF_DTable* dctx, void* dst, size_t dstSize,
|
2017-06-29 18:49:59 +00:00
|
|
|
const void* cSrc, size_t cSrcSize,
|
2018-02-03 02:03:09 +00:00
|
|
|
void* workSpace, size_t wkspSize, int bmi2)
|
2015-10-18 21:18:32 +00:00
|
|
|
{
|
|
|
|
const BYTE* ip = (const BYTE*) cSrc;
|
|
|
|
|
2017-06-29 18:49:59 +00:00
|
|
|
size_t const hSize = HUF_readDTableX2_wksp (dctx, cSrc, cSrcSize,
|
|
|
|
workSpace, wkspSize);
|
2016-06-08 09:11:02 +00:00
|
|
|
if (HUF_isError(hSize)) return hSize;
|
|
|
|
if (hSize >= cSrcSize) return ERROR(srcSize_wrong);
|
2016-06-10 22:23:43 +00:00
|
|
|
ip += hSize; cSrcSize -= hSize;
|
2015-10-18 21:18:32 +00:00
|
|
|
|
2018-02-03 02:03:09 +00:00
|
|
|
return HUF_decompress4X2_usingDTable_internal(dst, dstSize, ip, cSrcSize, dctx, bmi2);
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t HUF_decompress4X2_DCtx_wksp(HUF_DTable* dctx, void* dst, size_t dstSize,
|
|
|
|
const void* cSrc, size_t cSrcSize,
|
|
|
|
void* workSpace, size_t wkspSize)
|
|
|
|
{
|
|
|
|
return HUF_decompress4X2_DCtx_wksp_bmi2(dctx, dst, dstSize, cSrc, cSrcSize, workSpace, wkspSize, 0);
|
2016-06-10 22:23:43 +00:00
|
|
|
}
|
|
|
|
|
2017-06-29 18:49:59 +00:00
|
|
|
|
|
|
|
size_t HUF_decompress4X2_DCtx (HUF_DTable* dctx, void* dst, size_t dstSize, const void* cSrc, size_t cSrcSize)
|
|
|
|
{
|
|
|
|
U32 workSpace[HUF_DECOMPRESS_WORKSPACE_SIZE_U32];
|
|
|
|
return HUF_decompress4X2_DCtx_wksp(dctx, dst, dstSize, cSrc, cSrcSize,
|
|
|
|
workSpace, sizeof(workSpace));
|
|
|
|
}
|
2016-06-10 22:23:43 +00:00
|
|
|
size_t HUF_decompress4X2 (void* dst, size_t dstSize, const void* cSrc, size_t cSrcSize)
|
|
|
|
{
|
|
|
|
HUF_CREATE_STATIC_DTABLEX2(DTable, HUF_TABLELOG_MAX);
|
|
|
|
return HUF_decompress4X2_DCtx(DTable, dst, dstSize, cSrc, cSrcSize);
|
2015-10-18 21:18:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-01-23 18:28:41 +00:00
|
|
|
/* *************************/
|
2015-10-18 21:18:32 +00:00
|
|
|
/* double-symbols decoding */
|
2016-01-23 18:28:41 +00:00
|
|
|
/* *************************/
|
2016-06-08 09:11:02 +00:00
|
|
|
typedef struct { BYTE symbol; BYTE weight; } sortedSymbol_t;
|
2015-10-18 21:18:32 +00:00
|
|
|
|
2016-12-02 19:36:11 +00:00
|
|
|
/* HUF_fillDTableX4Level2() :
|
|
|
|
* `rankValOrigin` must be a table of at least (HUF_TABLELOG_MAX + 1) U32 */
|
2015-10-18 21:18:32 +00:00
|
|
|
static void HUF_fillDTableX4Level2(HUF_DEltX4* DTable, U32 sizeLog, const U32 consumed,
|
|
|
|
const U32* rankValOrigin, const int minWeight,
|
|
|
|
const sortedSymbol_t* sortedSymbols, const U32 sortedListSize,
|
|
|
|
U32 nbBitsBaseline, U16 baseSeq)
|
|
|
|
{
|
|
|
|
HUF_DEltX4 DElt;
|
2016-12-02 19:36:11 +00:00
|
|
|
U32 rankVal[HUF_TABLELOG_MAX + 1];
|
2015-10-18 21:18:32 +00:00
|
|
|
|
|
|
|
/* get pre-calculated rankVal */
|
|
|
|
memcpy(rankVal, rankValOrigin, sizeof(rankVal));
|
|
|
|
|
|
|
|
/* fill skipped values */
|
2016-01-28 14:39:52 +00:00
|
|
|
if (minWeight>1) {
|
2015-10-18 21:18:32 +00:00
|
|
|
U32 i, skipSize = rankVal[minWeight];
|
|
|
|
MEM_writeLE16(&(DElt.sequence), baseSeq);
|
|
|
|
DElt.nbBits = (BYTE)(consumed);
|
|
|
|
DElt.length = 1;
|
|
|
|
for (i = 0; i < skipSize; i++)
|
|
|
|
DTable[i] = DElt;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* fill DTable */
|
2016-08-19 17:38:19 +00:00
|
|
|
{ U32 s; for (s=0; s<sortedListSize; s++) { /* note : sortedSymbols already skipped */
|
|
|
|
const U32 symbol = sortedSymbols[s].symbol;
|
|
|
|
const U32 weight = sortedSymbols[s].weight;
|
|
|
|
const U32 nbBits = nbBitsBaseline - weight;
|
|
|
|
const U32 length = 1 << (sizeLog-nbBits);
|
|
|
|
const U32 start = rankVal[weight];
|
|
|
|
U32 i = start;
|
|
|
|
const U32 end = start + length;
|
|
|
|
|
|
|
|
MEM_writeLE16(&(DElt.sequence), (U16)(baseSeq + (symbol << 8)));
|
|
|
|
DElt.nbBits = (BYTE)(nbBits + consumed);
|
|
|
|
DElt.length = 2;
|
|
|
|
do { DTable[i++] = DElt; } while (i<end); /* since length >= 1 */
|
|
|
|
|
|
|
|
rankVal[weight] += length;
|
|
|
|
} }
|
2015-10-18 21:18:32 +00:00
|
|
|
}
|
|
|
|
|
2017-06-29 18:49:59 +00:00
|
|
|
typedef U32 rankValCol_t[HUF_TABLELOG_MAX + 1];
|
2017-06-30 16:52:20 +00:00
|
|
|
typedef rankValCol_t rankVal_t[HUF_TABLELOG_MAX];
|
2015-10-18 21:18:32 +00:00
|
|
|
|
|
|
|
static void HUF_fillDTableX4(HUF_DEltX4* DTable, const U32 targetLog,
|
|
|
|
const sortedSymbol_t* sortedList, const U32 sortedListSize,
|
|
|
|
const U32* rankStart, rankVal_t rankValOrigin, const U32 maxWeight,
|
|
|
|
const U32 nbBitsBaseline)
|
|
|
|
{
|
2016-12-02 02:24:59 +00:00
|
|
|
U32 rankVal[HUF_TABLELOG_MAX + 1];
|
2015-10-18 21:18:32 +00:00
|
|
|
const int scaleLog = nbBitsBaseline - targetLog; /* note : targetLog >= srcLog, hence scaleLog <= 1 */
|
|
|
|
const U32 minBits = nbBitsBaseline - maxWeight;
|
|
|
|
U32 s;
|
|
|
|
|
|
|
|
memcpy(rankVal, rankValOrigin, sizeof(rankVal));
|
|
|
|
|
|
|
|
/* fill DTable */
|
2016-01-28 14:39:52 +00:00
|
|
|
for (s=0; s<sortedListSize; s++) {
|
2015-10-18 21:18:32 +00:00
|
|
|
const U16 symbol = sortedList[s].symbol;
|
|
|
|
const U32 weight = sortedList[s].weight;
|
|
|
|
const U32 nbBits = nbBitsBaseline - weight;
|
|
|
|
const U32 start = rankVal[weight];
|
|
|
|
const U32 length = 1 << (targetLog-nbBits);
|
|
|
|
|
2016-01-28 14:39:52 +00:00
|
|
|
if (targetLog-nbBits >= minBits) { /* enough room for a second symbol */
|
2015-10-18 21:18:32 +00:00
|
|
|
U32 sortedRank;
|
|
|
|
int minWeight = nbBits + scaleLog;
|
|
|
|
if (minWeight < 1) minWeight = 1;
|
|
|
|
sortedRank = rankStart[minWeight];
|
|
|
|
HUF_fillDTableX4Level2(DTable+start, targetLog-nbBits, nbBits,
|
|
|
|
rankValOrigin[nbBits], minWeight,
|
|
|
|
sortedList+sortedRank, sortedListSize-sortedRank,
|
|
|
|
nbBitsBaseline, symbol);
|
2016-01-28 14:39:52 +00:00
|
|
|
} else {
|
2015-10-18 21:18:32 +00:00
|
|
|
HUF_DEltX4 DElt;
|
|
|
|
MEM_writeLE16(&(DElt.sequence), symbol);
|
2016-03-20 04:40:39 +00:00
|
|
|
DElt.nbBits = (BYTE)(nbBits);
|
|
|
|
DElt.length = 1;
|
2016-08-19 17:38:19 +00:00
|
|
|
{ U32 const end = start + length;
|
|
|
|
U32 u;
|
2016-03-20 04:40:39 +00:00
|
|
|
for (u = start; u < end; u++) DTable[u] = DElt;
|
|
|
|
} }
|
2015-10-18 21:18:32 +00:00
|
|
|
rankVal[weight] += length;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-06-29 18:49:59 +00:00
|
|
|
size_t HUF_readDTableX4_wksp(HUF_DTable* DTable, const void* src,
|
|
|
|
size_t srcSize, void* workSpace,
|
|
|
|
size_t wkspSize)
|
2015-10-18 21:18:32 +00:00
|
|
|
{
|
|
|
|
U32 tableLog, maxW, sizeOfSort, nbSymbols;
|
2016-06-10 22:23:43 +00:00
|
|
|
DTableDesc dtd = HUF_getDTableDesc(DTable);
|
|
|
|
U32 const maxTableLog = dtd.maxTableLog;
|
2016-01-06 00:58:37 +00:00
|
|
|
size_t iSize;
|
2016-06-08 09:11:02 +00:00
|
|
|
void* dtPtr = DTable+1; /* force compiler to avoid strict-aliasing */
|
|
|
|
HUF_DEltX4* const dt = (HUF_DEltX4*)dtPtr;
|
2017-06-29 18:49:59 +00:00
|
|
|
U32 *rankStart;
|
|
|
|
|
2017-06-29 19:39:34 +00:00
|
|
|
rankValCol_t* rankVal;
|
|
|
|
U32* rankStats;
|
|
|
|
U32* rankStart0;
|
|
|
|
sortedSymbol_t* sortedSymbol;
|
|
|
|
BYTE* weightList;
|
|
|
|
size_t spaceUsed32 = 0;
|
|
|
|
|
|
|
|
rankVal = (rankValCol_t *)((U32 *)workSpace + spaceUsed32);
|
|
|
|
spaceUsed32 += (sizeof(rankValCol_t) * HUF_TABLELOG_MAX) >> 2;
|
|
|
|
rankStats = (U32 *)workSpace + spaceUsed32;
|
|
|
|
spaceUsed32 += HUF_TABLELOG_MAX + 1;
|
|
|
|
rankStart0 = (U32 *)workSpace + spaceUsed32;
|
|
|
|
spaceUsed32 += HUF_TABLELOG_MAX + 2;
|
2017-06-29 20:04:15 +00:00
|
|
|
sortedSymbol = (sortedSymbol_t *)workSpace + (spaceUsed32 * sizeof(U32)) / sizeof(sortedSymbol_t);
|
2017-06-30 16:38:11 +00:00
|
|
|
spaceUsed32 += HUF_ALIGN(sizeof(sortedSymbol_t) * (HUF_SYMBOLVALUE_MAX + 1), sizeof(U32)) >> 2;
|
2017-06-29 19:39:34 +00:00
|
|
|
weightList = (BYTE *)((U32 *)workSpace + spaceUsed32);
|
2017-06-30 16:38:11 +00:00
|
|
|
spaceUsed32 += HUF_ALIGN(HUF_SYMBOLVALUE_MAX + 1, sizeof(U32)) >> 2;
|
2017-06-29 19:39:34 +00:00
|
|
|
|
2018-02-22 23:44:26 +00:00
|
|
|
if ((spaceUsed32 << 2) > wkspSize) return ERROR(tableLog_tooLarge);
|
2017-06-29 18:49:59 +00:00
|
|
|
|
|
|
|
rankStart = rankStart0 + 1;
|
|
|
|
memset(rankStats, 0, sizeof(U32) * (2 * HUF_TABLELOG_MAX + 2 + 1));
|
2015-10-18 21:18:32 +00:00
|
|
|
|
2017-02-27 08:27:30 +00:00
|
|
|
HUF_STATIC_ASSERT(sizeof(HUF_DEltX4) == sizeof(HUF_DTable)); /* if compiler fails here, assertion is wrong */
|
2016-12-02 02:24:59 +00:00
|
|
|
if (maxTableLog > HUF_TABLELOG_MAX) return ERROR(tableLog_tooLarge);
|
2016-08-19 17:38:19 +00:00
|
|
|
/* memset(weightList, 0, sizeof(weightList)); */ /* is not necessary, even though some analyzer complain ... */
|
2015-10-18 21:18:32 +00:00
|
|
|
|
2016-05-20 12:36:36 +00:00
|
|
|
iSize = HUF_readStats(weightList, HUF_SYMBOLVALUE_MAX + 1, rankStats, &nbSymbols, &tableLog, src, srcSize);
|
2015-10-18 21:18:32 +00:00
|
|
|
if (HUF_isError(iSize)) return iSize;
|
|
|
|
|
|
|
|
/* check result */
|
2016-06-08 09:11:02 +00:00
|
|
|
if (tableLog > maxTableLog) return ERROR(tableLog_tooLarge); /* DTable can't fit code depth */
|
2015-10-18 21:18:32 +00:00
|
|
|
|
|
|
|
/* find maxWeight */
|
|
|
|
for (maxW = tableLog; rankStats[maxW]==0; maxW--) {} /* necessarily finds a solution before 0 */
|
|
|
|
|
|
|
|
/* Get start index of each weight */
|
2016-03-20 04:40:39 +00:00
|
|
|
{ U32 w, nextRankStart = 0;
|
2016-05-06 14:55:27 +00:00
|
|
|
for (w=1; w<maxW+1; w++) {
|
2015-10-18 21:18:32 +00:00
|
|
|
U32 current = nextRankStart;
|
|
|
|
nextRankStart += rankStats[w];
|
|
|
|
rankStart[w] = current;
|
|
|
|
}
|
|
|
|
rankStart[0] = nextRankStart; /* put all 0w symbols at the end of sorted list*/
|
|
|
|
sizeOfSort = nextRankStart;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* sort symbols by weight */
|
2016-03-20 04:40:39 +00:00
|
|
|
{ U32 s;
|
2016-01-28 14:39:52 +00:00
|
|
|
for (s=0; s<nbSymbols; s++) {
|
2016-05-06 14:55:27 +00:00
|
|
|
U32 const w = weightList[s];
|
|
|
|
U32 const r = rankStart[w]++;
|
2015-10-18 21:18:32 +00:00
|
|
|
sortedSymbol[r].symbol = (BYTE)s;
|
|
|
|
sortedSymbol[r].weight = (BYTE)w;
|
|
|
|
}
|
|
|
|
rankStart[0] = 0; /* forget 0w symbols; this is beginning of weight(1) */
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Build rankVal */
|
2016-05-06 14:55:27 +00:00
|
|
|
{ U32* const rankVal0 = rankVal[0];
|
2016-06-08 09:11:02 +00:00
|
|
|
{ int const rescale = (maxTableLog-tableLog) - 1; /* tableLog <= maxTableLog */
|
2016-05-06 14:55:27 +00:00
|
|
|
U32 nextRankVal = 0;
|
|
|
|
U32 w;
|
|
|
|
for (w=1; w<maxW+1; w++) {
|
|
|
|
U32 current = nextRankVal;
|
|
|
|
nextRankVal += rankStats[w] << (w+rescale);
|
|
|
|
rankVal0[w] = current;
|
|
|
|
} }
|
|
|
|
{ U32 const minBits = tableLog+1 - maxW;
|
|
|
|
U32 consumed;
|
2016-06-08 09:11:02 +00:00
|
|
|
for (consumed = minBits; consumed < maxTableLog - minBits + 1; consumed++) {
|
2016-05-06 14:55:27 +00:00
|
|
|
U32* const rankValPtr = rankVal[consumed];
|
|
|
|
U32 w;
|
|
|
|
for (w = 1; w < maxW+1; w++) {
|
|
|
|
rankValPtr[w] = rankVal0[w] >> consumed;
|
|
|
|
} } } }
|
2015-10-18 21:18:32 +00:00
|
|
|
|
2016-06-08 09:11:02 +00:00
|
|
|
HUF_fillDTableX4(dt, maxTableLog,
|
2015-10-18 21:18:32 +00:00
|
|
|
sortedSymbol, sizeOfSort,
|
|
|
|
rankStart0, rankVal, maxW,
|
|
|
|
tableLog+1);
|
|
|
|
|
2016-06-10 22:23:43 +00:00
|
|
|
dtd.tableLog = (BYTE)maxTableLog;
|
|
|
|
dtd.tableType = 1;
|
2016-06-08 09:11:02 +00:00
|
|
|
memcpy(DTable, &dtd, sizeof(dtd));
|
2015-10-18 21:18:32 +00:00
|
|
|
return iSize;
|
|
|
|
}
|
|
|
|
|
2017-06-29 18:49:59 +00:00
|
|
|
size_t HUF_readDTableX4(HUF_DTable* DTable, const void* src, size_t srcSize)
|
|
|
|
{
|
|
|
|
U32 workSpace[HUF_DECOMPRESS_WORKSPACE_SIZE_U32];
|
|
|
|
return HUF_readDTableX4_wksp(DTable, src, srcSize,
|
|
|
|
workSpace, sizeof(workSpace));
|
|
|
|
}
|
2015-10-18 21:18:32 +00:00
|
|
|
|
2016-06-10 22:23:43 +00:00
|
|
|
size_t HUF_decompress1X4_usingDTable(
|
|
|
|
void* dst, size_t dstSize,
|
|
|
|
const void* cSrc, size_t cSrcSize,
|
|
|
|
const HUF_DTable* DTable)
|
|
|
|
{
|
|
|
|
DTableDesc dtd = HUF_getDTableDesc(DTable);
|
2016-06-15 21:11:20 +00:00
|
|
|
if (dtd.tableType != 1) return ERROR(GENERIC);
|
2018-02-03 02:03:09 +00:00
|
|
|
return HUF_decompress1X4_usingDTable_internal(dst, dstSize, cSrc, cSrcSize, DTable, /* bmi2 */ 0);
|
2016-06-10 22:23:43 +00:00
|
|
|
}
|
|
|
|
|
2017-06-29 18:49:59 +00:00
|
|
|
size_t HUF_decompress1X4_DCtx_wksp(HUF_DTable* DCtx, void* dst, size_t dstSize,
|
|
|
|
const void* cSrc, size_t cSrcSize,
|
|
|
|
void* workSpace, size_t wkspSize)
|
2015-10-18 21:18:32 +00:00
|
|
|
{
|
|
|
|
const BYTE* ip = (const BYTE*) cSrc;
|
|
|
|
|
2017-06-29 18:49:59 +00:00
|
|
|
size_t const hSize = HUF_readDTableX4_wksp(DCtx, cSrc, cSrcSize,
|
|
|
|
workSpace, wkspSize);
|
2015-10-18 21:18:32 +00:00
|
|
|
if (HUF_isError(hSize)) return hSize;
|
|
|
|
if (hSize >= cSrcSize) return ERROR(srcSize_wrong);
|
2016-06-10 23:46:03 +00:00
|
|
|
ip += hSize; cSrcSize -= hSize;
|
2015-10-18 21:18:32 +00:00
|
|
|
|
2018-02-03 02:03:09 +00:00
|
|
|
return HUF_decompress1X4_usingDTable_internal(dst, dstSize, ip, cSrcSize, DCtx, /* bmi2 */ 0);
|
2016-06-10 23:46:03 +00:00
|
|
|
}
|
|
|
|
|
2017-06-29 18:49:59 +00:00
|
|
|
|
|
|
|
size_t HUF_decompress1X4_DCtx(HUF_DTable* DCtx, void* dst, size_t dstSize,
|
|
|
|
const void* cSrc, size_t cSrcSize)
|
|
|
|
{
|
|
|
|
U32 workSpace[HUF_DECOMPRESS_WORKSPACE_SIZE_U32];
|
|
|
|
return HUF_decompress1X4_DCtx_wksp(DCtx, dst, dstSize, cSrc, cSrcSize,
|
|
|
|
workSpace, sizeof(workSpace));
|
|
|
|
}
|
|
|
|
|
2016-06-10 23:46:03 +00:00
|
|
|
size_t HUF_decompress1X4 (void* dst, size_t dstSize, const void* cSrc, size_t cSrcSize)
|
|
|
|
{
|
|
|
|
HUF_CREATE_STATIC_DTABLEX4(DTable, HUF_TABLELOG_MAX);
|
|
|
|
return HUF_decompress1X4_DCtx(DTable, dst, dstSize, cSrc, cSrcSize);
|
2015-10-18 21:18:32 +00:00
|
|
|
}
|
|
|
|
|
2016-06-10 22:23:43 +00:00
|
|
|
size_t HUF_decompress4X4_usingDTable(
|
|
|
|
void* dst, size_t dstSize,
|
|
|
|
const void* cSrc, size_t cSrcSize,
|
|
|
|
const HUF_DTable* DTable)
|
|
|
|
{
|
|
|
|
DTableDesc dtd = HUF_getDTableDesc(DTable);
|
|
|
|
if (dtd.tableType != 1) return ERROR(GENERIC);
|
2018-02-03 02:03:09 +00:00
|
|
|
return HUF_decompress4X4_usingDTable_internal(dst, dstSize, cSrc, cSrcSize, DTable, /* bmi2 */ 0);
|
2016-06-10 22:23:43 +00:00
|
|
|
}
|
|
|
|
|
2018-02-03 02:03:09 +00:00
|
|
|
static size_t HUF_decompress4X4_DCtx_wksp_bmi2(HUF_DTable* dctx, void* dst, size_t dstSize,
|
2017-06-29 18:49:59 +00:00
|
|
|
const void* cSrc, size_t cSrcSize,
|
2018-02-03 02:03:09 +00:00
|
|
|
void* workSpace, size_t wkspSize, int bmi2)
|
2015-10-18 21:18:32 +00:00
|
|
|
{
|
|
|
|
const BYTE* ip = (const BYTE*) cSrc;
|
|
|
|
|
2017-06-29 18:49:59 +00:00
|
|
|
size_t hSize = HUF_readDTableX4_wksp(dctx, cSrc, cSrcSize,
|
|
|
|
workSpace, wkspSize);
|
2015-10-18 21:18:32 +00:00
|
|
|
if (HUF_isError(hSize)) return hSize;
|
|
|
|
if (hSize >= cSrcSize) return ERROR(srcSize_wrong);
|
2016-06-10 22:23:43 +00:00
|
|
|
ip += hSize; cSrcSize -= hSize;
|
|
|
|
|
2018-02-03 02:03:09 +00:00
|
|
|
return HUF_decompress4X4_usingDTable_internal(dst, dstSize, ip, cSrcSize, dctx, bmi2);
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t HUF_decompress4X4_DCtx_wksp(HUF_DTable* dctx, void* dst, size_t dstSize,
|
|
|
|
const void* cSrc, size_t cSrcSize,
|
|
|
|
void* workSpace, size_t wkspSize)
|
|
|
|
{
|
|
|
|
return HUF_decompress4X4_DCtx_wksp_bmi2(dctx, dst, dstSize, cSrc, cSrcSize, workSpace, wkspSize, /* bmi2 */ 0);
|
2016-06-10 22:23:43 +00:00
|
|
|
}
|
2015-10-18 21:18:32 +00:00
|
|
|
|
2017-06-29 18:49:59 +00:00
|
|
|
|
|
|
|
size_t HUF_decompress4X4_DCtx(HUF_DTable* dctx, void* dst, size_t dstSize,
|
|
|
|
const void* cSrc, size_t cSrcSize)
|
|
|
|
{
|
|
|
|
U32 workSpace[HUF_DECOMPRESS_WORKSPACE_SIZE_U32];
|
|
|
|
return HUF_decompress4X4_DCtx_wksp(dctx, dst, dstSize, cSrc, cSrcSize,
|
|
|
|
workSpace, sizeof(workSpace));
|
|
|
|
}
|
|
|
|
|
2016-06-10 22:23:43 +00:00
|
|
|
size_t HUF_decompress4X4 (void* dst, size_t dstSize, const void* cSrc, size_t cSrcSize)
|
|
|
|
{
|
|
|
|
HUF_CREATE_STATIC_DTABLEX4(DTable, HUF_TABLELOG_MAX);
|
|
|
|
return HUF_decompress4X4_DCtx(DTable, dst, dstSize, cSrc, cSrcSize);
|
2015-10-18 21:18:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-01-23 18:28:41 +00:00
|
|
|
/* ********************************/
|
2015-10-18 21:18:32 +00:00
|
|
|
/* Generic decompression selector */
|
2016-01-23 18:28:41 +00:00
|
|
|
/* ********************************/
|
2015-10-18 21:18:32 +00:00
|
|
|
|
2016-06-10 22:23:43 +00:00
|
|
|
size_t HUF_decompress1X_usingDTable(void* dst, size_t maxDstSize,
|
|
|
|
const void* cSrc, size_t cSrcSize,
|
|
|
|
const HUF_DTable* DTable)
|
|
|
|
{
|
|
|
|
DTableDesc const dtd = HUF_getDTableDesc(DTable);
|
2018-02-03 02:03:09 +00:00
|
|
|
return dtd.tableType ? HUF_decompress1X4_usingDTable_internal(dst, maxDstSize, cSrc, cSrcSize, DTable, /* bmi2 */ 0) :
|
|
|
|
HUF_decompress1X2_usingDTable_internal(dst, maxDstSize, cSrc, cSrcSize, DTable, /* bmi2 */ 0);
|
2016-06-10 22:23:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
size_t HUF_decompress4X_usingDTable(void* dst, size_t maxDstSize,
|
|
|
|
const void* cSrc, size_t cSrcSize,
|
|
|
|
const HUF_DTable* DTable)
|
|
|
|
{
|
|
|
|
DTableDesc const dtd = HUF_getDTableDesc(DTable);
|
2018-02-03 02:03:09 +00:00
|
|
|
return dtd.tableType ? HUF_decompress4X4_usingDTable_internal(dst, maxDstSize, cSrc, cSrcSize, DTable, /* bmi2 */ 0) :
|
|
|
|
HUF_decompress4X2_usingDTable_internal(dst, maxDstSize, cSrc, cSrcSize, DTable, /* bmi2 */ 0);
|
2016-06-10 22:23:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-10-18 21:18:32 +00:00
|
|
|
typedef struct { U32 tableTime; U32 decode256Time; } algo_time_t;
|
|
|
|
static const algo_time_t algoTime[16 /* Quantization */][3 /* single, double, quad */] =
|
|
|
|
{
|
|
|
|
/* single, double, quad */
|
|
|
|
{{0,0}, {1,1}, {2,2}}, /* Q==0 : impossible */
|
|
|
|
{{0,0}, {1,1}, {2,2}}, /* Q==1 : impossible */
|
|
|
|
{{ 38,130}, {1313, 74}, {2151, 38}}, /* Q == 2 : 12-18% */
|
|
|
|
{{ 448,128}, {1353, 74}, {2238, 41}}, /* Q == 3 : 18-25% */
|
|
|
|
{{ 556,128}, {1353, 74}, {2238, 47}}, /* Q == 4 : 25-32% */
|
|
|
|
{{ 714,128}, {1418, 74}, {2436, 53}}, /* Q == 5 : 32-38% */
|
|
|
|
{{ 883,128}, {1437, 74}, {2464, 61}}, /* Q == 6 : 38-44% */
|
|
|
|
{{ 897,128}, {1515, 75}, {2622, 68}}, /* Q == 7 : 44-50% */
|
|
|
|
{{ 926,128}, {1613, 75}, {2730, 75}}, /* Q == 8 : 50-56% */
|
|
|
|
{{ 947,128}, {1729, 77}, {3359, 77}}, /* Q == 9 : 56-62% */
|
|
|
|
{{1107,128}, {2083, 81}, {4006, 84}}, /* Q ==10 : 62-69% */
|
|
|
|
{{1177,128}, {2379, 87}, {4785, 88}}, /* Q ==11 : 69-75% */
|
|
|
|
{{1242,128}, {2415, 93}, {5155, 84}}, /* Q ==12 : 75-81% */
|
|
|
|
{{1349,128}, {2644,106}, {5260,106}}, /* Q ==13 : 81-87% */
|
|
|
|
{{1455,128}, {2422,124}, {4174,124}}, /* Q ==14 : 87-93% */
|
|
|
|
{{ 722,128}, {1891,145}, {1936,146}}, /* Q ==15 : 93-99% */
|
|
|
|
};
|
|
|
|
|
2016-06-08 09:11:02 +00:00
|
|
|
/** HUF_selectDecoder() :
|
|
|
|
* Tells which decoder is likely to decode faster,
|
|
|
|
* based on a set of pre-determined metrics.
|
|
|
|
* @return : 0==HUF_decompress4X2, 1==HUF_decompress4X4 .
|
2017-07-28 18:54:28 +00:00
|
|
|
* Assumption : 0 < cSrcSize, dstSize <= 128 KB */
|
2016-06-08 09:11:02 +00:00
|
|
|
U32 HUF_selectDecoder (size_t dstSize, size_t cSrcSize)
|
|
|
|
{
|
|
|
|
/* decoder timing evaluation */
|
2017-07-28 18:54:28 +00:00
|
|
|
U32 const Q = cSrcSize >= dstSize ? 15 : (U32)(cSrcSize * 16 / dstSize); /* Q < 16 */
|
2016-06-08 09:11:02 +00:00
|
|
|
U32 const D256 = (U32)(dstSize >> 8);
|
|
|
|
U32 const DTime0 = algoTime[Q][0].tableTime + (algoTime[Q][0].decode256Time * D256);
|
|
|
|
U32 DTime1 = algoTime[Q][1].tableTime + (algoTime[Q][1].decode256Time * D256);
|
|
|
|
DTime1 += DTime1 >> 3; /* advantage to algorithm using less memory, for cache eviction */
|
|
|
|
|
|
|
|
return DTime1 < DTime0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-10-18 21:18:32 +00:00
|
|
|
typedef size_t (*decompressionAlgo)(void* dst, size_t dstSize, const void* cSrc, size_t cSrcSize);
|
|
|
|
|
|
|
|
size_t HUF_decompress (void* dst, size_t dstSize, const void* cSrc, size_t cSrcSize)
|
|
|
|
{
|
2016-06-08 09:11:02 +00:00
|
|
|
static const decompressionAlgo decompress[2] = { HUF_decompress4X2, HUF_decompress4X4 };
|
2015-10-18 21:18:32 +00:00
|
|
|
|
|
|
|
/* validation checks */
|
|
|
|
if (dstSize == 0) return ERROR(dstSize_tooSmall);
|
|
|
|
if (cSrcSize > dstSize) return ERROR(corruption_detected); /* invalid */
|
|
|
|
if (cSrcSize == dstSize) { memcpy(dst, cSrc, dstSize); return dstSize; } /* not compressed */
|
2015-10-21 08:07:25 +00:00
|
|
|
if (cSrcSize == 1) { memset(dst, *(const BYTE*)cSrc, dstSize); return dstSize; } /* RLE */
|
2015-10-18 21:18:32 +00:00
|
|
|
|
2016-06-08 09:11:02 +00:00
|
|
|
{ U32 const algoNb = HUF_selectDecoder(dstSize, cSrcSize);
|
2016-05-05 10:41:36 +00:00
|
|
|
return decompress[algoNb](dst, dstSize, cSrc, cSrcSize);
|
|
|
|
}
|
2015-10-18 21:18:32 +00:00
|
|
|
}
|
2016-06-10 22:23:43 +00:00
|
|
|
|
2016-06-10 23:46:03 +00:00
|
|
|
size_t HUF_decompress4X_DCtx (HUF_DTable* dctx, void* dst, size_t dstSize, const void* cSrc, size_t cSrcSize)
|
2016-06-10 22:23:43 +00:00
|
|
|
{
|
|
|
|
/* validation checks */
|
|
|
|
if (dstSize == 0) return ERROR(dstSize_tooSmall);
|
|
|
|
if (cSrcSize > dstSize) return ERROR(corruption_detected); /* invalid */
|
|
|
|
if (cSrcSize == dstSize) { memcpy(dst, cSrc, dstSize); return dstSize; } /* not compressed */
|
|
|
|
if (cSrcSize == 1) { memset(dst, *(const BYTE*)cSrc, dstSize); return dstSize; } /* RLE */
|
|
|
|
|
|
|
|
{ U32 const algoNb = HUF_selectDecoder(dstSize, cSrcSize);
|
|
|
|
return algoNb ? HUF_decompress4X4_DCtx(dctx, dst, dstSize, cSrc, cSrcSize) :
|
|
|
|
HUF_decompress4X2_DCtx(dctx, dst, dstSize, cSrc, cSrcSize) ;
|
|
|
|
}
|
|
|
|
}
|
2016-06-10 23:46:03 +00:00
|
|
|
|
2017-06-29 23:22:32 +00:00
|
|
|
size_t HUF_decompress4X_hufOnly(HUF_DTable* dctx, void* dst, size_t dstSize, const void* cSrc, size_t cSrcSize)
|
2016-06-11 21:26:22 +00:00
|
|
|
{
|
2017-06-29 23:22:32 +00:00
|
|
|
U32 workSpace[HUF_DECOMPRESS_WORKSPACE_SIZE_U32];
|
|
|
|
return HUF_decompress4X_hufOnly_wksp(dctx, dst, dstSize, cSrc, cSrcSize,
|
|
|
|
workSpace, sizeof(workSpace));
|
2016-06-11 21:26:22 +00:00
|
|
|
}
|
|
|
|
|
2017-06-29 18:49:59 +00:00
|
|
|
|
|
|
|
size_t HUF_decompress4X_hufOnly_wksp(HUF_DTable* dctx, void* dst,
|
|
|
|
size_t dstSize, const void* cSrc,
|
|
|
|
size_t cSrcSize, void* workSpace,
|
|
|
|
size_t wkspSize)
|
|
|
|
{
|
|
|
|
/* validation checks */
|
|
|
|
if (dstSize == 0) return ERROR(dstSize_tooSmall);
|
2017-07-28 18:54:28 +00:00
|
|
|
if (cSrcSize == 0) return ERROR(corruption_detected);
|
2017-06-29 18:49:59 +00:00
|
|
|
|
|
|
|
{ U32 const algoNb = HUF_selectDecoder(dstSize, cSrcSize);
|
|
|
|
return algoNb ? HUF_decompress4X4_DCtx_wksp(dctx, dst, dstSize, cSrc, cSrcSize, workSpace, wkspSize):
|
|
|
|
HUF_decompress4X2_DCtx_wksp(dctx, dst, dstSize, cSrc, cSrcSize, workSpace, wkspSize);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t HUF_decompress1X_DCtx_wksp(HUF_DTable* dctx, void* dst, size_t dstSize,
|
|
|
|
const void* cSrc, size_t cSrcSize,
|
|
|
|
void* workSpace, size_t wkspSize)
|
2016-06-10 23:46:03 +00:00
|
|
|
{
|
|
|
|
/* validation checks */
|
|
|
|
if (dstSize == 0) return ERROR(dstSize_tooSmall);
|
|
|
|
if (cSrcSize > dstSize) return ERROR(corruption_detected); /* invalid */
|
|
|
|
if (cSrcSize == dstSize) { memcpy(dst, cSrc, dstSize); return dstSize; } /* not compressed */
|
|
|
|
if (cSrcSize == 1) { memset(dst, *(const BYTE*)cSrc, dstSize); return dstSize; } /* RLE */
|
|
|
|
|
|
|
|
{ U32 const algoNb = HUF_selectDecoder(dstSize, cSrcSize);
|
2017-06-29 18:49:59 +00:00
|
|
|
return algoNb ? HUF_decompress1X4_DCtx_wksp(dctx, dst, dstSize, cSrc,
|
|
|
|
cSrcSize, workSpace, wkspSize):
|
|
|
|
HUF_decompress1X2_DCtx_wksp(dctx, dst, dstSize, cSrc,
|
|
|
|
cSrcSize, workSpace, wkspSize);
|
2016-06-10 23:46:03 +00:00
|
|
|
}
|
|
|
|
}
|
2017-06-29 18:49:59 +00:00
|
|
|
|
|
|
|
size_t HUF_decompress1X_DCtx(HUF_DTable* dctx, void* dst, size_t dstSize,
|
|
|
|
const void* cSrc, size_t cSrcSize)
|
|
|
|
{
|
|
|
|
U32 workSpace[HUF_DECOMPRESS_WORKSPACE_SIZE_U32];
|
|
|
|
return HUF_decompress1X_DCtx_wksp(dctx, dst, dstSize, cSrc, cSrcSize,
|
|
|
|
workSpace, sizeof(workSpace));
|
|
|
|
}
|
2018-02-03 02:03:09 +00:00
|
|
|
|
|
|
|
|
|
|
|
size_t HUF_decompress1X_usingDTable_bmi2(void* dst, size_t maxDstSize, const void* cSrc, size_t cSrcSize, const HUF_DTable* DTable, int bmi2)
|
|
|
|
{
|
|
|
|
DTableDesc const dtd = HUF_getDTableDesc(DTable);
|
|
|
|
return dtd.tableType ? HUF_decompress1X4_usingDTable_internal(dst, maxDstSize, cSrc, cSrcSize, DTable, bmi2) :
|
|
|
|
HUF_decompress1X2_usingDTable_internal(dst, maxDstSize, cSrc, cSrcSize, DTable, bmi2);
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t HUF_decompress1X2_DCtx_wksp_bmi2(HUF_DTable* dctx, void* dst, size_t dstSize, const void* cSrc, size_t cSrcSize, void* workSpace, size_t wkspSize, int bmi2)
|
|
|
|
{
|
|
|
|
const BYTE* ip = (const BYTE*) cSrc;
|
|
|
|
|
|
|
|
size_t const hSize = HUF_readDTableX2_wksp(dctx, cSrc, cSrcSize, workSpace, wkspSize);
|
|
|
|
if (HUF_isError(hSize)) return hSize;
|
|
|
|
if (hSize >= cSrcSize) return ERROR(srcSize_wrong);
|
|
|
|
ip += hSize; cSrcSize -= hSize;
|
|
|
|
|
|
|
|
return HUF_decompress1X2_usingDTable_internal(dst, dstSize, ip, cSrcSize, dctx, bmi2);
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t HUF_decompress4X_usingDTable_bmi2(void* dst, size_t maxDstSize, const void* cSrc, size_t cSrcSize, const HUF_DTable* DTable, int bmi2)
|
|
|
|
{
|
|
|
|
DTableDesc const dtd = HUF_getDTableDesc(DTable);
|
|
|
|
return dtd.tableType ? HUF_decompress4X4_usingDTable_internal(dst, maxDstSize, cSrc, cSrcSize, DTable, bmi2) :
|
|
|
|
HUF_decompress4X2_usingDTable_internal(dst, maxDstSize, cSrc, cSrcSize, DTable, bmi2);
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t HUF_decompress4X_hufOnly_wksp_bmi2(HUF_DTable* dctx, void* dst, size_t dstSize, const void* cSrc, size_t cSrcSize, void* workSpace, size_t wkspSize, int bmi2)
|
|
|
|
{
|
|
|
|
/* validation checks */
|
|
|
|
if (dstSize == 0) return ERROR(dstSize_tooSmall);
|
|
|
|
if (cSrcSize == 0) return ERROR(corruption_detected);
|
|
|
|
|
|
|
|
{ U32 const algoNb = HUF_selectDecoder(dstSize, cSrcSize);
|
|
|
|
return algoNb ? HUF_decompress4X4_DCtx_wksp_bmi2(dctx, dst, dstSize, cSrc, cSrcSize, workSpace, wkspSize, bmi2) :
|
|
|
|
HUF_decompress4X2_DCtx_wksp_bmi2(dctx, dst, dstSize, cSrc, cSrcSize, workSpace, wkspSize, bmi2);
|
|
|
|
}
|
|
|
|
}
|