Path: blob/master/Utilities/cmzstd/lib/decompress/huf_decompress.c
3158 views
/* ******************************************************************1* huff0 huffman decoder,2* part of Finite State Entropy library3* Copyright (c) Meta Platforms, Inc. and affiliates.4*5* You can contact the author at :6* - FSE+HUF source repository : https://github.com/Cyan4973/FiniteStateEntropy7*8* This source code is licensed under both the BSD-style license (found in the9* LICENSE file in the root directory of this source tree) and the GPLv2 (found10* in the COPYING file in the root directory of this source tree).11* You may select, at your option, one of the above-listed licenses.12****************************************************************** */1314/* **************************************************************15* Dependencies16****************************************************************/17#include "../common/zstd_deps.h" /* ZSTD_memcpy, ZSTD_memset */18#include "../common/compiler.h"19#include "../common/bitstream.h" /* BIT_* */20#include "../common/fse.h" /* to compress headers */21#include "../common/huf.h"22#include "../common/error_private.h"23#include "../common/zstd_internal.h"24#include "../common/bits.h" /* ZSTD_highbit32, ZSTD_countTrailingZeros64 */2526/* **************************************************************27* Constants28****************************************************************/2930#define HUF_DECODER_FAST_TABLELOG 113132/* **************************************************************33* Macros34****************************************************************/3536/* These two optional macros force the use one way or another of the two37* Huffman decompression implementations. You can't force in both directions38* at the same time.39*/40#if defined(HUF_FORCE_DECOMPRESS_X1) && \41defined(HUF_FORCE_DECOMPRESS_X2)42#error "Cannot force the use of the X1 and X2 decoders at the same time!"43#endif4445/* When DYNAMIC_BMI2 is enabled, fast decoders are only called when bmi2 is46* supported at runtime, so we can add the BMI2 target attribute.47* When it is disabled, we will still get BMI2 if it is enabled statically.48*/49#if DYNAMIC_BMI250# define HUF_FAST_BMI2_ATTRS BMI2_TARGET_ATTRIBUTE51#else52# define HUF_FAST_BMI2_ATTRS53#endif5455#ifdef __cplusplus56# define HUF_EXTERN_C extern "C"57#else58# define HUF_EXTERN_C59#endif60#define HUF_ASM_DECL HUF_EXTERN_C6162#if DYNAMIC_BMI263# define HUF_NEED_BMI2_FUNCTION 164#else65# define HUF_NEED_BMI2_FUNCTION 066#endif6768/* **************************************************************69* Error Management70****************************************************************/71#define HUF_isError ERR_isError727374/* **************************************************************75* Byte alignment for workSpace management76****************************************************************/77#define HUF_ALIGN(x, a) HUF_ALIGN_MASK((x), (a) - 1)78#define HUF_ALIGN_MASK(x, mask) (((x) + (mask)) & ~(mask))798081/* **************************************************************82* BMI2 Variant Wrappers83****************************************************************/84typedef size_t (*HUF_DecompressUsingDTableFn)(void *dst, size_t dstSize,85const void *cSrc,86size_t cSrcSize,87const HUF_DTable *DTable);8889#if DYNAMIC_BMI29091#define HUF_DGEN(fn) \92\93static size_t fn##_default( \94void* dst, size_t dstSize, \95const void* cSrc, size_t cSrcSize, \96const HUF_DTable* DTable) \97{ \98return fn##_body(dst, dstSize, cSrc, cSrcSize, DTable); \99} \100\101static BMI2_TARGET_ATTRIBUTE size_t fn##_bmi2( \102void* dst, size_t dstSize, \103const void* cSrc, size_t cSrcSize, \104const HUF_DTable* DTable) \105{ \106return fn##_body(dst, dstSize, cSrc, cSrcSize, DTable); \107} \108\109static size_t fn(void* dst, size_t dstSize, void const* cSrc, \110size_t cSrcSize, HUF_DTable const* DTable, int flags) \111{ \112if (flags & HUF_flags_bmi2) { \113return fn##_bmi2(dst, dstSize, cSrc, cSrcSize, DTable); \114} \115return fn##_default(dst, dstSize, cSrc, cSrcSize, DTable); \116}117118#else119120#define HUF_DGEN(fn) \121static size_t fn(void* dst, size_t dstSize, void const* cSrc, \122size_t cSrcSize, HUF_DTable const* DTable, int flags) \123{ \124(void)flags; \125return fn##_body(dst, dstSize, cSrc, cSrcSize, DTable); \126}127128#endif129130131/*-***************************/132/* generic DTableDesc */133/*-***************************/134typedef struct { BYTE maxTableLog; BYTE tableType; BYTE tableLog; BYTE reserved; } DTableDesc;135136static DTableDesc HUF_getDTableDesc(const HUF_DTable* table)137{138DTableDesc dtd;139ZSTD_memcpy(&dtd, table, sizeof(dtd));140return dtd;141}142143static size_t HUF_initFastDStream(BYTE const* ip) {144BYTE const lastByte = ip[7];145size_t const bitsConsumed = lastByte ? 8 - ZSTD_highbit32(lastByte) : 0;146size_t const value = MEM_readLEST(ip) | 1;147assert(bitsConsumed <= 8);148assert(sizeof(size_t) == 8);149return value << bitsConsumed;150}151152153/**154* The input/output arguments to the Huffman fast decoding loop:155*156* ip [in/out] - The input pointers, must be updated to reflect what is consumed.157* op [in/out] - The output pointers, must be updated to reflect what is written.158* bits [in/out] - The bitstream containers, must be updated to reflect the current state.159* dt [in] - The decoding table.160* ilimit [in] - The input limit, stop when any input pointer is below ilimit.161* oend [in] - The end of the output stream. op[3] must not cross oend.162* iend [in] - The end of each input stream. ip[i] may cross iend[i],163* as long as it is above ilimit, but that indicates corruption.164*/165typedef struct {166BYTE const* ip[4];167BYTE* op[4];168U64 bits[4];169void const* dt;170BYTE const* ilimit;171BYTE* oend;172BYTE const* iend[4];173} HUF_DecompressFastArgs;174175typedef void (*HUF_DecompressFastLoopFn)(HUF_DecompressFastArgs*);176177/**178* Initializes args for the fast decoding loop.179* @returns 1 on success180* 0 if the fallback implementation should be used.181* Or an error code on failure.182*/183static size_t HUF_DecompressFastArgs_init(HUF_DecompressFastArgs* args, void* dst, size_t dstSize, void const* src, size_t srcSize, const HUF_DTable* DTable)184{185void const* dt = DTable + 1;186U32 const dtLog = HUF_getDTableDesc(DTable).tableLog;187188const BYTE* const ilimit = (const BYTE*)src + 6 + 8;189190BYTE* const oend = (BYTE*)dst + dstSize;191192/* The fast decoding loop assumes 64-bit little-endian.193* This condition is false on x32.194*/195if (!MEM_isLittleEndian() || MEM_32bits())196return 0;197198/* strict minimum : jump table + 1 byte per stream */199if (srcSize < 10)200return ERROR(corruption_detected);201202/* Must have at least 8 bytes per stream because we don't handle initializing smaller bit containers.203* If table log is not correct at this point, fallback to the old decoder.204* On small inputs we don't have enough data to trigger the fast loop, so use the old decoder.205*/206if (dtLog != HUF_DECODER_FAST_TABLELOG)207return 0;208209/* Read the jump table. */210{211const BYTE* const istart = (const BYTE*)src;212size_t const length1 = MEM_readLE16(istart);213size_t const length2 = MEM_readLE16(istart+2);214size_t const length3 = MEM_readLE16(istart+4);215size_t const length4 = srcSize - (length1 + length2 + length3 + 6);216args->iend[0] = istart + 6; /* jumpTable */217args->iend[1] = args->iend[0] + length1;218args->iend[2] = args->iend[1] + length2;219args->iend[3] = args->iend[2] + length3;220221/* HUF_initFastDStream() requires this, and this small of an input222* won't benefit from the ASM loop anyways.223* length1 must be >= 16 so that ip[0] >= ilimit before the loop224* starts.225*/226if (length1 < 16 || length2 < 8 || length3 < 8 || length4 < 8)227return 0;228if (length4 > srcSize) return ERROR(corruption_detected); /* overflow */229}230/* ip[] contains the position that is currently loaded into bits[]. */231args->ip[0] = args->iend[1] - sizeof(U64);232args->ip[1] = args->iend[2] - sizeof(U64);233args->ip[2] = args->iend[3] - sizeof(U64);234args->ip[3] = (BYTE const*)src + srcSize - sizeof(U64);235236/* op[] contains the output pointers. */237args->op[0] = (BYTE*)dst;238args->op[1] = args->op[0] + (dstSize+3)/4;239args->op[2] = args->op[1] + (dstSize+3)/4;240args->op[3] = args->op[2] + (dstSize+3)/4;241242/* No point to call the ASM loop for tiny outputs. */243if (args->op[3] >= oend)244return 0;245246/* bits[] is the bit container.247* It is read from the MSB down to the LSB.248* It is shifted left as it is read, and zeros are249* shifted in. After the lowest valid bit a 1 is250* set, so that CountTrailingZeros(bits[]) can be used251* to count how many bits we've consumed.252*/253args->bits[0] = HUF_initFastDStream(args->ip[0]);254args->bits[1] = HUF_initFastDStream(args->ip[1]);255args->bits[2] = HUF_initFastDStream(args->ip[2]);256args->bits[3] = HUF_initFastDStream(args->ip[3]);257258/* If ip[] >= ilimit, it is guaranteed to be safe to259* reload bits[]. It may be beyond its section, but is260* guaranteed to be valid (>= istart).261*/262args->ilimit = ilimit;263264args->oend = oend;265args->dt = dt;266267return 1;268}269270static size_t HUF_initRemainingDStream(BIT_DStream_t* bit, HUF_DecompressFastArgs const* args, int stream, BYTE* segmentEnd)271{272/* Validate that we haven't overwritten. */273if (args->op[stream] > segmentEnd)274return ERROR(corruption_detected);275/* Validate that we haven't read beyond iend[].276* Note that ip[] may be < iend[] because the MSB is277* the next bit to read, and we may have consumed 100%278* of the stream, so down to iend[i] - 8 is valid.279*/280if (args->ip[stream] < args->iend[stream] - 8)281return ERROR(corruption_detected);282283/* Construct the BIT_DStream_t. */284assert(sizeof(size_t) == 8);285bit->bitContainer = MEM_readLEST(args->ip[stream]);286bit->bitsConsumed = ZSTD_countTrailingZeros64(args->bits[stream]);287bit->start = (const char*)args->iend[0];288bit->limitPtr = bit->start + sizeof(size_t);289bit->ptr = (const char*)args->ip[stream];290291return 0;292}293294295#ifndef HUF_FORCE_DECOMPRESS_X2296297/*-***************************/298/* single-symbol decoding */299/*-***************************/300typedef struct { BYTE nbBits; BYTE byte; } HUF_DEltX1; /* single-symbol decoding */301302/**303* Packs 4 HUF_DEltX1 structs into a U64. This is used to lay down 4 entries at304* a time.305*/306static U64 HUF_DEltX1_set4(BYTE symbol, BYTE nbBits) {307U64 D4;308if (MEM_isLittleEndian()) {309D4 = (U64)((symbol << 8) + nbBits);310} else {311D4 = (U64)(symbol + (nbBits << 8));312}313assert(D4 < (1U << 16));314D4 *= 0x0001000100010001ULL;315return D4;316}317318/**319* Increase the tableLog to targetTableLog and rescales the stats.320* If tableLog > targetTableLog this is a no-op.321* @returns New tableLog322*/323static U32 HUF_rescaleStats(BYTE* huffWeight, U32* rankVal, U32 nbSymbols, U32 tableLog, U32 targetTableLog)324{325if (tableLog > targetTableLog)326return tableLog;327if (tableLog < targetTableLog) {328U32 const scale = targetTableLog - tableLog;329U32 s;330/* Increase the weight for all non-zero probability symbols by scale. */331for (s = 0; s < nbSymbols; ++s) {332huffWeight[s] += (BYTE)((huffWeight[s] == 0) ? 0 : scale);333}334/* Update rankVal to reflect the new weights.335* All weights except 0 get moved to weight + scale.336* Weights [1, scale] are empty.337*/338for (s = targetTableLog; s > scale; --s) {339rankVal[s] = rankVal[s - scale];340}341for (s = scale; s > 0; --s) {342rankVal[s] = 0;343}344}345return targetTableLog;346}347348typedef struct {349U32 rankVal[HUF_TABLELOG_ABSOLUTEMAX + 1];350U32 rankStart[HUF_TABLELOG_ABSOLUTEMAX + 1];351U32 statsWksp[HUF_READ_STATS_WORKSPACE_SIZE_U32];352BYTE symbols[HUF_SYMBOLVALUE_MAX + 1];353BYTE huffWeight[HUF_SYMBOLVALUE_MAX + 1];354} HUF_ReadDTableX1_Workspace;355356size_t HUF_readDTableX1_wksp(HUF_DTable* DTable, const void* src, size_t srcSize, void* workSpace, size_t wkspSize, int flags)357{358U32 tableLog = 0;359U32 nbSymbols = 0;360size_t iSize;361void* const dtPtr = DTable + 1;362HUF_DEltX1* const dt = (HUF_DEltX1*)dtPtr;363HUF_ReadDTableX1_Workspace* wksp = (HUF_ReadDTableX1_Workspace*)workSpace;364365DEBUG_STATIC_ASSERT(HUF_DECOMPRESS_WORKSPACE_SIZE >= sizeof(*wksp));366if (sizeof(*wksp) > wkspSize) return ERROR(tableLog_tooLarge);367368DEBUG_STATIC_ASSERT(sizeof(DTableDesc) == sizeof(HUF_DTable));369/* ZSTD_memset(huffWeight, 0, sizeof(huffWeight)); */ /* is not necessary, even though some analyzer complain ... */370371iSize = HUF_readStats_wksp(wksp->huffWeight, HUF_SYMBOLVALUE_MAX + 1, wksp->rankVal, &nbSymbols, &tableLog, src, srcSize, wksp->statsWksp, sizeof(wksp->statsWksp), flags);372if (HUF_isError(iSize)) return iSize;373374375/* Table header */376{ DTableDesc dtd = HUF_getDTableDesc(DTable);377U32 const maxTableLog = dtd.maxTableLog + 1;378U32 const targetTableLog = MIN(maxTableLog, HUF_DECODER_FAST_TABLELOG);379tableLog = HUF_rescaleStats(wksp->huffWeight, wksp->rankVal, nbSymbols, tableLog, targetTableLog);380if (tableLog > (U32)(dtd.maxTableLog+1)) return ERROR(tableLog_tooLarge); /* DTable too small, Huffman tree cannot fit in */381dtd.tableType = 0;382dtd.tableLog = (BYTE)tableLog;383ZSTD_memcpy(DTable, &dtd, sizeof(dtd));384}385386/* Compute symbols and rankStart given rankVal:387*388* rankVal already contains the number of values of each weight.389*390* symbols contains the symbols ordered by weight. First are the rankVal[0]391* weight 0 symbols, followed by the rankVal[1] weight 1 symbols, and so on.392* symbols[0] is filled (but unused) to avoid a branch.393*394* rankStart contains the offset where each rank belongs in the DTable.395* rankStart[0] is not filled because there are no entries in the table for396* weight 0.397*/398{ int n;399U32 nextRankStart = 0;400int const unroll = 4;401int const nLimit = (int)nbSymbols - unroll + 1;402for (n=0; n<(int)tableLog+1; n++) {403U32 const curr = nextRankStart;404nextRankStart += wksp->rankVal[n];405wksp->rankStart[n] = curr;406}407for (n=0; n < nLimit; n += unroll) {408int u;409for (u=0; u < unroll; ++u) {410size_t const w = wksp->huffWeight[n+u];411wksp->symbols[wksp->rankStart[w]++] = (BYTE)(n+u);412}413}414for (; n < (int)nbSymbols; ++n) {415size_t const w = wksp->huffWeight[n];416wksp->symbols[wksp->rankStart[w]++] = (BYTE)n;417}418}419420/* fill DTable421* We fill all entries of each weight in order.422* That way length is a constant for each iteration of the outer loop.423* We can switch based on the length to a different inner loop which is424* optimized for that particular case.425*/426{ U32 w;427int symbol = wksp->rankVal[0];428int rankStart = 0;429for (w=1; w<tableLog+1; ++w) {430int const symbolCount = wksp->rankVal[w];431int const length = (1 << w) >> 1;432int uStart = rankStart;433BYTE const nbBits = (BYTE)(tableLog + 1 - w);434int s;435int u;436switch (length) {437case 1:438for (s=0; s<symbolCount; ++s) {439HUF_DEltX1 D;440D.byte = wksp->symbols[symbol + s];441D.nbBits = nbBits;442dt[uStart] = D;443uStart += 1;444}445break;446case 2:447for (s=0; s<symbolCount; ++s) {448HUF_DEltX1 D;449D.byte = wksp->symbols[symbol + s];450D.nbBits = nbBits;451dt[uStart+0] = D;452dt[uStart+1] = D;453uStart += 2;454}455break;456case 4:457for (s=0; s<symbolCount; ++s) {458U64 const D4 = HUF_DEltX1_set4(wksp->symbols[symbol + s], nbBits);459MEM_write64(dt + uStart, D4);460uStart += 4;461}462break;463case 8:464for (s=0; s<symbolCount; ++s) {465U64 const D4 = HUF_DEltX1_set4(wksp->symbols[symbol + s], nbBits);466MEM_write64(dt + uStart, D4);467MEM_write64(dt + uStart + 4, D4);468uStart += 8;469}470break;471default:472for (s=0; s<symbolCount; ++s) {473U64 const D4 = HUF_DEltX1_set4(wksp->symbols[symbol + s], nbBits);474for (u=0; u < length; u += 16) {475MEM_write64(dt + uStart + u + 0, D4);476MEM_write64(dt + uStart + u + 4, D4);477MEM_write64(dt + uStart + u + 8, D4);478MEM_write64(dt + uStart + u + 12, D4);479}480assert(u == length);481uStart += length;482}483break;484}485symbol += symbolCount;486rankStart += symbolCount * length;487}488}489return iSize;490}491492FORCE_INLINE_TEMPLATE BYTE493HUF_decodeSymbolX1(BIT_DStream_t* Dstream, const HUF_DEltX1* dt, const U32 dtLog)494{495size_t const val = BIT_lookBitsFast(Dstream, dtLog); /* note : dtLog >= 1 */496BYTE const c = dt[val].byte;497BIT_skipBits(Dstream, dt[val].nbBits);498return c;499}500501#define HUF_DECODE_SYMBOLX1_0(ptr, DStreamPtr) \502*ptr++ = HUF_decodeSymbolX1(DStreamPtr, dt, dtLog)503504#define HUF_DECODE_SYMBOLX1_1(ptr, DStreamPtr) \505if (MEM_64bits() || (HUF_TABLELOG_MAX<=12)) \506HUF_DECODE_SYMBOLX1_0(ptr, DStreamPtr)507508#define HUF_DECODE_SYMBOLX1_2(ptr, DStreamPtr) \509if (MEM_64bits()) \510HUF_DECODE_SYMBOLX1_0(ptr, DStreamPtr)511512HINT_INLINE size_t513HUF_decodeStreamX1(BYTE* p, BIT_DStream_t* const bitDPtr, BYTE* const pEnd, const HUF_DEltX1* const dt, const U32 dtLog)514{515BYTE* const pStart = p;516517/* up to 4 symbols at a time */518if ((pEnd - p) > 3) {519while ((BIT_reloadDStream(bitDPtr) == BIT_DStream_unfinished) & (p < pEnd-3)) {520HUF_DECODE_SYMBOLX1_2(p, bitDPtr);521HUF_DECODE_SYMBOLX1_1(p, bitDPtr);522HUF_DECODE_SYMBOLX1_2(p, bitDPtr);523HUF_DECODE_SYMBOLX1_0(p, bitDPtr);524}525} else {526BIT_reloadDStream(bitDPtr);527}528529/* [0-3] symbols remaining */530if (MEM_32bits())531while ((BIT_reloadDStream(bitDPtr) == BIT_DStream_unfinished) & (p < pEnd))532HUF_DECODE_SYMBOLX1_0(p, bitDPtr);533534/* no more data to retrieve from bitstream, no need to reload */535while (p < pEnd)536HUF_DECODE_SYMBOLX1_0(p, bitDPtr);537538return (size_t)(pEnd-pStart);539}540541FORCE_INLINE_TEMPLATE size_t542HUF_decompress1X1_usingDTable_internal_body(543void* dst, size_t dstSize,544const void* cSrc, size_t cSrcSize,545const HUF_DTable* DTable)546{547BYTE* op = (BYTE*)dst;548BYTE* const oend = op + dstSize;549const void* dtPtr = DTable + 1;550const HUF_DEltX1* const dt = (const HUF_DEltX1*)dtPtr;551BIT_DStream_t bitD;552DTableDesc const dtd = HUF_getDTableDesc(DTable);553U32 const dtLog = dtd.tableLog;554555CHECK_F( BIT_initDStream(&bitD, cSrc, cSrcSize) );556557HUF_decodeStreamX1(op, &bitD, oend, dt, dtLog);558559if (!BIT_endOfDStream(&bitD)) return ERROR(corruption_detected);560561return dstSize;562}563564/* HUF_decompress4X1_usingDTable_internal_body():565* Conditions :566* @dstSize >= 6567*/568FORCE_INLINE_TEMPLATE size_t569HUF_decompress4X1_usingDTable_internal_body(570void* dst, size_t dstSize,571const void* cSrc, size_t cSrcSize,572const HUF_DTable* DTable)573{574/* Check */575if (cSrcSize < 10) return ERROR(corruption_detected); /* strict minimum : jump table + 1 byte per stream */576577{ const BYTE* const istart = (const BYTE*) cSrc;578BYTE* const ostart = (BYTE*) dst;579BYTE* const oend = ostart + dstSize;580BYTE* const olimit = oend - 3;581const void* const dtPtr = DTable + 1;582const HUF_DEltX1* const dt = (const HUF_DEltX1*)dtPtr;583584/* Init */585BIT_DStream_t bitD1;586BIT_DStream_t bitD2;587BIT_DStream_t bitD3;588BIT_DStream_t bitD4;589size_t const length1 = MEM_readLE16(istart);590size_t const length2 = MEM_readLE16(istart+2);591size_t const length3 = MEM_readLE16(istart+4);592size_t const length4 = cSrcSize - (length1 + length2 + length3 + 6);593const BYTE* const istart1 = istart + 6; /* jumpTable */594const BYTE* const istart2 = istart1 + length1;595const BYTE* const istart3 = istart2 + length2;596const BYTE* const istart4 = istart3 + length3;597const size_t segmentSize = (dstSize+3) / 4;598BYTE* const opStart2 = ostart + segmentSize;599BYTE* const opStart3 = opStart2 + segmentSize;600BYTE* const opStart4 = opStart3 + segmentSize;601BYTE* op1 = ostart;602BYTE* op2 = opStart2;603BYTE* op3 = opStart3;604BYTE* op4 = opStart4;605DTableDesc const dtd = HUF_getDTableDesc(DTable);606U32 const dtLog = dtd.tableLog;607U32 endSignal = 1;608609if (length4 > cSrcSize) return ERROR(corruption_detected); /* overflow */610if (opStart4 > oend) return ERROR(corruption_detected); /* overflow */611if (dstSize < 6) return ERROR(corruption_detected); /* stream 4-split doesn't work */612CHECK_F( BIT_initDStream(&bitD1, istart1, length1) );613CHECK_F( BIT_initDStream(&bitD2, istart2, length2) );614CHECK_F( BIT_initDStream(&bitD3, istart3, length3) );615CHECK_F( BIT_initDStream(&bitD4, istart4, length4) );616617/* up to 16 symbols per loop (4 symbols per stream) in 64-bit mode */618if ((size_t)(oend - op4) >= sizeof(size_t)) {619for ( ; (endSignal) & (op4 < olimit) ; ) {620HUF_DECODE_SYMBOLX1_2(op1, &bitD1);621HUF_DECODE_SYMBOLX1_2(op2, &bitD2);622HUF_DECODE_SYMBOLX1_2(op3, &bitD3);623HUF_DECODE_SYMBOLX1_2(op4, &bitD4);624HUF_DECODE_SYMBOLX1_1(op1, &bitD1);625HUF_DECODE_SYMBOLX1_1(op2, &bitD2);626HUF_DECODE_SYMBOLX1_1(op3, &bitD3);627HUF_DECODE_SYMBOLX1_1(op4, &bitD4);628HUF_DECODE_SYMBOLX1_2(op1, &bitD1);629HUF_DECODE_SYMBOLX1_2(op2, &bitD2);630HUF_DECODE_SYMBOLX1_2(op3, &bitD3);631HUF_DECODE_SYMBOLX1_2(op4, &bitD4);632HUF_DECODE_SYMBOLX1_0(op1, &bitD1);633HUF_DECODE_SYMBOLX1_0(op2, &bitD2);634HUF_DECODE_SYMBOLX1_0(op3, &bitD3);635HUF_DECODE_SYMBOLX1_0(op4, &bitD4);636endSignal &= BIT_reloadDStreamFast(&bitD1) == BIT_DStream_unfinished;637endSignal &= BIT_reloadDStreamFast(&bitD2) == BIT_DStream_unfinished;638endSignal &= BIT_reloadDStreamFast(&bitD3) == BIT_DStream_unfinished;639endSignal &= BIT_reloadDStreamFast(&bitD4) == BIT_DStream_unfinished;640}641}642643/* check corruption */644/* note : should not be necessary : op# advance in lock step, and we control op4.645* but curiously, binary generated by gcc 7.2 & 7.3 with -mbmi2 runs faster when >=1 test is present */646if (op1 > opStart2) return ERROR(corruption_detected);647if (op2 > opStart3) return ERROR(corruption_detected);648if (op3 > opStart4) return ERROR(corruption_detected);649/* note : op4 supposed already verified within main loop */650651/* finish bitStreams one by one */652HUF_decodeStreamX1(op1, &bitD1, opStart2, dt, dtLog);653HUF_decodeStreamX1(op2, &bitD2, opStart3, dt, dtLog);654HUF_decodeStreamX1(op3, &bitD3, opStart4, dt, dtLog);655HUF_decodeStreamX1(op4, &bitD4, oend, dt, dtLog);656657/* check */658{ U32 const endCheck = BIT_endOfDStream(&bitD1) & BIT_endOfDStream(&bitD2) & BIT_endOfDStream(&bitD3) & BIT_endOfDStream(&bitD4);659if (!endCheck) return ERROR(corruption_detected); }660661/* decoded size */662return dstSize;663}664}665666#if HUF_NEED_BMI2_FUNCTION667static BMI2_TARGET_ATTRIBUTE668size_t HUF_decompress4X1_usingDTable_internal_bmi2(void* dst, size_t dstSize, void const* cSrc,669size_t cSrcSize, HUF_DTable const* DTable) {670return HUF_decompress4X1_usingDTable_internal_body(dst, dstSize, cSrc, cSrcSize, DTable);671}672#endif673674static675size_t HUF_decompress4X1_usingDTable_internal_default(void* dst, size_t dstSize, void const* cSrc,676size_t cSrcSize, HUF_DTable const* DTable) {677return HUF_decompress4X1_usingDTable_internal_body(dst, dstSize, cSrc, cSrcSize, DTable);678}679680#if ZSTD_ENABLE_ASM_X86_64_BMI2681682HUF_ASM_DECL void HUF_decompress4X1_usingDTable_internal_fast_asm_loop(HUF_DecompressFastArgs* args) ZSTDLIB_HIDDEN;683684#endif685686static HUF_FAST_BMI2_ATTRS687void HUF_decompress4X1_usingDTable_internal_fast_c_loop(HUF_DecompressFastArgs* args)688{689U64 bits[4];690BYTE const* ip[4];691BYTE* op[4];692U16 const* const dtable = (U16 const*)args->dt;693BYTE* const oend = args->oend;694BYTE const* const ilimit = args->ilimit;695696/* Copy the arguments to local variables */697ZSTD_memcpy(&bits, &args->bits, sizeof(bits));698ZSTD_memcpy((void*)(&ip), &args->ip, sizeof(ip));699ZSTD_memcpy(&op, &args->op, sizeof(op));700701assert(MEM_isLittleEndian());702assert(!MEM_32bits());703704for (;;) {705BYTE* olimit;706int stream;707int symbol;708709/* Assert loop preconditions */710#ifndef NDEBUG711for (stream = 0; stream < 4; ++stream) {712assert(op[stream] <= (stream == 3 ? oend : op[stream + 1]));713assert(ip[stream] >= ilimit);714}715#endif716/* Compute olimit */717{718/* Each iteration produces 5 output symbols per stream */719size_t const oiters = (size_t)(oend - op[3]) / 5;720/* Each iteration consumes up to 11 bits * 5 = 55 bits < 7 bytes721* per stream.722*/723size_t const iiters = (size_t)(ip[0] - ilimit) / 7;724/* We can safely run iters iterations before running bounds checks */725size_t const iters = MIN(oiters, iiters);726size_t const symbols = iters * 5;727728/* We can simply check that op[3] < olimit, instead of checking all729* of our bounds, since we can't hit the other bounds until we've run730* iters iterations, which only happens when op[3] == olimit.731*/732olimit = op[3] + symbols;733734/* Exit fast decoding loop once we get close to the end. */735if (op[3] + 20 > olimit)736break;737738/* Exit the decoding loop if any input pointer has crossed the739* previous one. This indicates corruption, and a precondition740* to our loop is that ip[i] >= ip[0].741*/742for (stream = 1; stream < 4; ++stream) {743if (ip[stream] < ip[stream - 1])744goto _out;745}746}747748#ifndef NDEBUG749for (stream = 1; stream < 4; ++stream) {750assert(ip[stream] >= ip[stream - 1]);751}752#endif753754do {755/* Decode 5 symbols in each of the 4 streams */756for (symbol = 0; symbol < 5; ++symbol) {757for (stream = 0; stream < 4; ++stream) {758int const index = (int)(bits[stream] >> 53);759int const entry = (int)dtable[index];760bits[stream] <<= (entry & 63);761op[stream][symbol] = (BYTE)((entry >> 8) & 0xFF);762}763}764/* Reload the bitstreams */765for (stream = 0; stream < 4; ++stream) {766int const ctz = ZSTD_countTrailingZeros64(bits[stream]);767int const nbBits = ctz & 7;768int const nbBytes = ctz >> 3;769op[stream] += 5;770ip[stream] -= nbBytes;771bits[stream] = MEM_read64(ip[stream]) | 1;772bits[stream] <<= nbBits;773}774} while (op[3] < olimit);775}776777_out:778779/* Save the final values of each of the state variables back to args. */780ZSTD_memcpy(&args->bits, &bits, sizeof(bits));781ZSTD_memcpy((void*)(&args->ip), &ip, sizeof(ip));782ZSTD_memcpy(&args->op, &op, sizeof(op));783}784785/**786* @returns @p dstSize on success (>= 6)787* 0 if the fallback implementation should be used788* An error if an error occurred789*/790static HUF_FAST_BMI2_ATTRS791size_t792HUF_decompress4X1_usingDTable_internal_fast(793void* dst, size_t dstSize,794const void* cSrc, size_t cSrcSize,795const HUF_DTable* DTable,796HUF_DecompressFastLoopFn loopFn)797{798void const* dt = DTable + 1;799const BYTE* const iend = (const BYTE*)cSrc + 6;800BYTE* const oend = (BYTE*)dst + dstSize;801HUF_DecompressFastArgs args;802{ size_t const ret = HUF_DecompressFastArgs_init(&args, dst, dstSize, cSrc, cSrcSize, DTable);803FORWARD_IF_ERROR(ret, "Failed to init fast loop args");804if (ret == 0)805return 0;806}807808assert(args.ip[0] >= args.ilimit);809loopFn(&args);810811/* Our loop guarantees that ip[] >= ilimit and that we haven't812* overwritten any op[].813*/814assert(args.ip[0] >= iend);815assert(args.ip[1] >= iend);816assert(args.ip[2] >= iend);817assert(args.ip[3] >= iend);818assert(args.op[3] <= oend);819(void)iend;820821/* finish bit streams one by one. */822{ size_t const segmentSize = (dstSize+3) / 4;823BYTE* segmentEnd = (BYTE*)dst;824int i;825for (i = 0; i < 4; ++i) {826BIT_DStream_t bit;827if (segmentSize <= (size_t)(oend - segmentEnd))828segmentEnd += segmentSize;829else830segmentEnd = oend;831FORWARD_IF_ERROR(HUF_initRemainingDStream(&bit, &args, i, segmentEnd), "corruption");832/* Decompress and validate that we've produced exactly the expected length. */833args.op[i] += HUF_decodeStreamX1(args.op[i], &bit, segmentEnd, (HUF_DEltX1 const*)dt, HUF_DECODER_FAST_TABLELOG);834if (args.op[i] != segmentEnd) return ERROR(corruption_detected);835}836}837838/* decoded size */839assert(dstSize != 0);840return dstSize;841}842843HUF_DGEN(HUF_decompress1X1_usingDTable_internal)844845static size_t HUF_decompress4X1_usingDTable_internal(void* dst, size_t dstSize, void const* cSrc,846size_t cSrcSize, HUF_DTable const* DTable, int flags)847{848HUF_DecompressUsingDTableFn fallbackFn = HUF_decompress4X1_usingDTable_internal_default;849HUF_DecompressFastLoopFn loopFn = HUF_decompress4X1_usingDTable_internal_fast_c_loop;850851#if DYNAMIC_BMI2852if (flags & HUF_flags_bmi2) {853fallbackFn = HUF_decompress4X1_usingDTable_internal_bmi2;854# if ZSTD_ENABLE_ASM_X86_64_BMI2855if (!(flags & HUF_flags_disableAsm)) {856loopFn = HUF_decompress4X1_usingDTable_internal_fast_asm_loop;857}858# endif859} else {860return fallbackFn(dst, dstSize, cSrc, cSrcSize, DTable);861}862#endif863864#if ZSTD_ENABLE_ASM_X86_64_BMI2 && defined(__BMI2__)865if (!(flags & HUF_flags_disableAsm)) {866loopFn = HUF_decompress4X1_usingDTable_internal_fast_asm_loop;867}868#endif869870if (!(flags & HUF_flags_disableFast)) {871size_t const ret = HUF_decompress4X1_usingDTable_internal_fast(dst, dstSize, cSrc, cSrcSize, DTable, loopFn);872if (ret != 0)873return ret;874}875return fallbackFn(dst, dstSize, cSrc, cSrcSize, DTable);876}877878static size_t HUF_decompress4X1_DCtx_wksp(HUF_DTable* dctx, void* dst, size_t dstSize,879const void* cSrc, size_t cSrcSize,880void* workSpace, size_t wkspSize, int flags)881{882const BYTE* ip = (const BYTE*) cSrc;883884size_t const hSize = HUF_readDTableX1_wksp(dctx, cSrc, cSrcSize, workSpace, wkspSize, flags);885if (HUF_isError(hSize)) return hSize;886if (hSize >= cSrcSize) return ERROR(srcSize_wrong);887ip += hSize; cSrcSize -= hSize;888889return HUF_decompress4X1_usingDTable_internal(dst, dstSize, ip, cSrcSize, dctx, flags);890}891892#endif /* HUF_FORCE_DECOMPRESS_X2 */893894895#ifndef HUF_FORCE_DECOMPRESS_X1896897/* *************************/898/* double-symbols decoding */899/* *************************/900901typedef struct { U16 sequence; BYTE nbBits; BYTE length; } HUF_DEltX2; /* double-symbols decoding */902typedef struct { BYTE symbol; } sortedSymbol_t;903typedef U32 rankValCol_t[HUF_TABLELOG_MAX + 1];904typedef rankValCol_t rankVal_t[HUF_TABLELOG_MAX];905906/**907* Constructs a HUF_DEltX2 in a U32.908*/909static U32 HUF_buildDEltX2U32(U32 symbol, U32 nbBits, U32 baseSeq, int level)910{911U32 seq;912DEBUG_STATIC_ASSERT(offsetof(HUF_DEltX2, sequence) == 0);913DEBUG_STATIC_ASSERT(offsetof(HUF_DEltX2, nbBits) == 2);914DEBUG_STATIC_ASSERT(offsetof(HUF_DEltX2, length) == 3);915DEBUG_STATIC_ASSERT(sizeof(HUF_DEltX2) == sizeof(U32));916if (MEM_isLittleEndian()) {917seq = level == 1 ? symbol : (baseSeq + (symbol << 8));918return seq + (nbBits << 16) + ((U32)level << 24);919} else {920seq = level == 1 ? (symbol << 8) : ((baseSeq << 8) + symbol);921return (seq << 16) + (nbBits << 8) + (U32)level;922}923}924925/**926* Constructs a HUF_DEltX2.927*/928static HUF_DEltX2 HUF_buildDEltX2(U32 symbol, U32 nbBits, U32 baseSeq, int level)929{930HUF_DEltX2 DElt;931U32 const val = HUF_buildDEltX2U32(symbol, nbBits, baseSeq, level);932DEBUG_STATIC_ASSERT(sizeof(DElt) == sizeof(val));933ZSTD_memcpy(&DElt, &val, sizeof(val));934return DElt;935}936937/**938* Constructs 2 HUF_DEltX2s and packs them into a U64.939*/940static U64 HUF_buildDEltX2U64(U32 symbol, U32 nbBits, U16 baseSeq, int level)941{942U32 DElt = HUF_buildDEltX2U32(symbol, nbBits, baseSeq, level);943return (U64)DElt + ((U64)DElt << 32);944}945946/**947* Fills the DTable rank with all the symbols from [begin, end) that are each948* nbBits long.949*950* @param DTableRank The start of the rank in the DTable.951* @param begin The first symbol to fill (inclusive).952* @param end The last symbol to fill (exclusive).953* @param nbBits Each symbol is nbBits long.954* @param tableLog The table log.955* @param baseSeq If level == 1 { 0 } else { the first level symbol }956* @param level The level in the table. Must be 1 or 2.957*/958static void HUF_fillDTableX2ForWeight(959HUF_DEltX2* DTableRank,960sortedSymbol_t const* begin, sortedSymbol_t const* end,961U32 nbBits, U32 tableLog,962U16 baseSeq, int const level)963{964U32 const length = 1U << ((tableLog - nbBits) & 0x1F /* quiet static-analyzer */);965const sortedSymbol_t* ptr;966assert(level >= 1 && level <= 2);967switch (length) {968case 1:969for (ptr = begin; ptr != end; ++ptr) {970HUF_DEltX2 const DElt = HUF_buildDEltX2(ptr->symbol, nbBits, baseSeq, level);971*DTableRank++ = DElt;972}973break;974case 2:975for (ptr = begin; ptr != end; ++ptr) {976HUF_DEltX2 const DElt = HUF_buildDEltX2(ptr->symbol, nbBits, baseSeq, level);977DTableRank[0] = DElt;978DTableRank[1] = DElt;979DTableRank += 2;980}981break;982case 4:983for (ptr = begin; ptr != end; ++ptr) {984U64 const DEltX2 = HUF_buildDEltX2U64(ptr->symbol, nbBits, baseSeq, level);985ZSTD_memcpy(DTableRank + 0, &DEltX2, sizeof(DEltX2));986ZSTD_memcpy(DTableRank + 2, &DEltX2, sizeof(DEltX2));987DTableRank += 4;988}989break;990case 8:991for (ptr = begin; ptr != end; ++ptr) {992U64 const DEltX2 = HUF_buildDEltX2U64(ptr->symbol, nbBits, baseSeq, level);993ZSTD_memcpy(DTableRank + 0, &DEltX2, sizeof(DEltX2));994ZSTD_memcpy(DTableRank + 2, &DEltX2, sizeof(DEltX2));995ZSTD_memcpy(DTableRank + 4, &DEltX2, sizeof(DEltX2));996ZSTD_memcpy(DTableRank + 6, &DEltX2, sizeof(DEltX2));997DTableRank += 8;998}999break;1000default:1001for (ptr = begin; ptr != end; ++ptr) {1002U64 const DEltX2 = HUF_buildDEltX2U64(ptr->symbol, nbBits, baseSeq, level);1003HUF_DEltX2* const DTableRankEnd = DTableRank + length;1004for (; DTableRank != DTableRankEnd; DTableRank += 8) {1005ZSTD_memcpy(DTableRank + 0, &DEltX2, sizeof(DEltX2));1006ZSTD_memcpy(DTableRank + 2, &DEltX2, sizeof(DEltX2));1007ZSTD_memcpy(DTableRank + 4, &DEltX2, sizeof(DEltX2));1008ZSTD_memcpy(DTableRank + 6, &DEltX2, sizeof(DEltX2));1009}1010}1011break;1012}1013}10141015/* HUF_fillDTableX2Level2() :1016* `rankValOrigin` must be a table of at least (HUF_TABLELOG_MAX + 1) U32 */1017static void HUF_fillDTableX2Level2(HUF_DEltX2* DTable, U32 targetLog, const U32 consumedBits,1018const U32* rankVal, const int minWeight, const int maxWeight1,1019const sortedSymbol_t* sortedSymbols, U32 const* rankStart,1020U32 nbBitsBaseline, U16 baseSeq)1021{1022/* Fill skipped values (all positions up to rankVal[minWeight]).1023* These are positions only get a single symbol because the combined weight1024* is too large.1025*/1026if (minWeight>1) {1027U32 const length = 1U << ((targetLog - consumedBits) & 0x1F /* quiet static-analyzer */);1028U64 const DEltX2 = HUF_buildDEltX2U64(baseSeq, consumedBits, /* baseSeq */ 0, /* level */ 1);1029int const skipSize = rankVal[minWeight];1030assert(length > 1);1031assert((U32)skipSize < length);1032switch (length) {1033case 2:1034assert(skipSize == 1);1035ZSTD_memcpy(DTable, &DEltX2, sizeof(DEltX2));1036break;1037case 4:1038assert(skipSize <= 4);1039ZSTD_memcpy(DTable + 0, &DEltX2, sizeof(DEltX2));1040ZSTD_memcpy(DTable + 2, &DEltX2, sizeof(DEltX2));1041break;1042default:1043{1044int i;1045for (i = 0; i < skipSize; i += 8) {1046ZSTD_memcpy(DTable + i + 0, &DEltX2, sizeof(DEltX2));1047ZSTD_memcpy(DTable + i + 2, &DEltX2, sizeof(DEltX2));1048ZSTD_memcpy(DTable + i + 4, &DEltX2, sizeof(DEltX2));1049ZSTD_memcpy(DTable + i + 6, &DEltX2, sizeof(DEltX2));1050}1051}1052}1053}10541055/* Fill each of the second level symbols by weight. */1056{1057int w;1058for (w = minWeight; w < maxWeight1; ++w) {1059int const begin = rankStart[w];1060int const end = rankStart[w+1];1061U32 const nbBits = nbBitsBaseline - w;1062U32 const totalBits = nbBits + consumedBits;1063HUF_fillDTableX2ForWeight(1064DTable + rankVal[w],1065sortedSymbols + begin, sortedSymbols + end,1066totalBits, targetLog,1067baseSeq, /* level */ 2);1068}1069}1070}10711072static void HUF_fillDTableX2(HUF_DEltX2* DTable, const U32 targetLog,1073const sortedSymbol_t* sortedList,1074const U32* rankStart, rankValCol_t* rankValOrigin, const U32 maxWeight,1075const U32 nbBitsBaseline)1076{1077U32* const rankVal = rankValOrigin[0];1078const int scaleLog = nbBitsBaseline - targetLog; /* note : targetLog >= srcLog, hence scaleLog <= 1 */1079const U32 minBits = nbBitsBaseline - maxWeight;1080int w;1081int const wEnd = (int)maxWeight + 1;10821083/* Fill DTable in order of weight. */1084for (w = 1; w < wEnd; ++w) {1085int const begin = (int)rankStart[w];1086int const end = (int)rankStart[w+1];1087U32 const nbBits = nbBitsBaseline - w;10881089if (targetLog-nbBits >= minBits) {1090/* Enough room for a second symbol. */1091int start = rankVal[w];1092U32 const length = 1U << ((targetLog - nbBits) & 0x1F /* quiet static-analyzer */);1093int minWeight = nbBits + scaleLog;1094int s;1095if (minWeight < 1) minWeight = 1;1096/* Fill the DTable for every symbol of weight w.1097* These symbols get at least 1 second symbol.1098*/1099for (s = begin; s != end; ++s) {1100HUF_fillDTableX2Level2(1101DTable + start, targetLog, nbBits,1102rankValOrigin[nbBits], minWeight, wEnd,1103sortedList, rankStart,1104nbBitsBaseline, sortedList[s].symbol);1105start += length;1106}1107} else {1108/* Only a single symbol. */1109HUF_fillDTableX2ForWeight(1110DTable + rankVal[w],1111sortedList + begin, sortedList + end,1112nbBits, targetLog,1113/* baseSeq */ 0, /* level */ 1);1114}1115}1116}11171118typedef struct {1119rankValCol_t rankVal[HUF_TABLELOG_MAX];1120U32 rankStats[HUF_TABLELOG_MAX + 1];1121U32 rankStart0[HUF_TABLELOG_MAX + 3];1122sortedSymbol_t sortedSymbol[HUF_SYMBOLVALUE_MAX + 1];1123BYTE weightList[HUF_SYMBOLVALUE_MAX + 1];1124U32 calleeWksp[HUF_READ_STATS_WORKSPACE_SIZE_U32];1125} HUF_ReadDTableX2_Workspace;11261127size_t HUF_readDTableX2_wksp(HUF_DTable* DTable,1128const void* src, size_t srcSize,1129void* workSpace, size_t wkspSize, int flags)1130{1131U32 tableLog, maxW, nbSymbols;1132DTableDesc dtd = HUF_getDTableDesc(DTable);1133U32 maxTableLog = dtd.maxTableLog;1134size_t iSize;1135void* dtPtr = DTable+1; /* force compiler to avoid strict-aliasing */1136HUF_DEltX2* const dt = (HUF_DEltX2*)dtPtr;1137U32 *rankStart;11381139HUF_ReadDTableX2_Workspace* const wksp = (HUF_ReadDTableX2_Workspace*)workSpace;11401141if (sizeof(*wksp) > wkspSize) return ERROR(GENERIC);11421143rankStart = wksp->rankStart0 + 1;1144ZSTD_memset(wksp->rankStats, 0, sizeof(wksp->rankStats));1145ZSTD_memset(wksp->rankStart0, 0, sizeof(wksp->rankStart0));11461147DEBUG_STATIC_ASSERT(sizeof(HUF_DEltX2) == sizeof(HUF_DTable)); /* if compiler fails here, assertion is wrong */1148if (maxTableLog > HUF_TABLELOG_MAX) return ERROR(tableLog_tooLarge);1149/* ZSTD_memset(weightList, 0, sizeof(weightList)); */ /* is not necessary, even though some analyzer complain ... */11501151iSize = HUF_readStats_wksp(wksp->weightList, HUF_SYMBOLVALUE_MAX + 1, wksp->rankStats, &nbSymbols, &tableLog, src, srcSize, wksp->calleeWksp, sizeof(wksp->calleeWksp), flags);1152if (HUF_isError(iSize)) return iSize;11531154/* check result */1155if (tableLog > maxTableLog) return ERROR(tableLog_tooLarge); /* DTable can't fit code depth */1156if (tableLog <= HUF_DECODER_FAST_TABLELOG && maxTableLog > HUF_DECODER_FAST_TABLELOG) maxTableLog = HUF_DECODER_FAST_TABLELOG;11571158/* find maxWeight */1159for (maxW = tableLog; wksp->rankStats[maxW]==0; maxW--) {} /* necessarily finds a solution before 0 */11601161/* Get start index of each weight */1162{ U32 w, nextRankStart = 0;1163for (w=1; w<maxW+1; w++) {1164U32 curr = nextRankStart;1165nextRankStart += wksp->rankStats[w];1166rankStart[w] = curr;1167}1168rankStart[0] = nextRankStart; /* put all 0w symbols at the end of sorted list*/1169rankStart[maxW+1] = nextRankStart;1170}11711172/* sort symbols by weight */1173{ U32 s;1174for (s=0; s<nbSymbols; s++) {1175U32 const w = wksp->weightList[s];1176U32 const r = rankStart[w]++;1177wksp->sortedSymbol[r].symbol = (BYTE)s;1178}1179rankStart[0] = 0; /* forget 0w symbols; this is beginning of weight(1) */1180}11811182/* Build rankVal */1183{ U32* const rankVal0 = wksp->rankVal[0];1184{ int const rescale = (maxTableLog-tableLog) - 1; /* tableLog <= maxTableLog */1185U32 nextRankVal = 0;1186U32 w;1187for (w=1; w<maxW+1; w++) {1188U32 curr = nextRankVal;1189nextRankVal += wksp->rankStats[w] << (w+rescale);1190rankVal0[w] = curr;1191} }1192{ U32 const minBits = tableLog+1 - maxW;1193U32 consumed;1194for (consumed = minBits; consumed < maxTableLog - minBits + 1; consumed++) {1195U32* const rankValPtr = wksp->rankVal[consumed];1196U32 w;1197for (w = 1; w < maxW+1; w++) {1198rankValPtr[w] = rankVal0[w] >> consumed;1199} } } }12001201HUF_fillDTableX2(dt, maxTableLog,1202wksp->sortedSymbol,1203wksp->rankStart0, wksp->rankVal, maxW,1204tableLog+1);12051206dtd.tableLog = (BYTE)maxTableLog;1207dtd.tableType = 1;1208ZSTD_memcpy(DTable, &dtd, sizeof(dtd));1209return iSize;1210}121112121213FORCE_INLINE_TEMPLATE U321214HUF_decodeSymbolX2(void* op, BIT_DStream_t* DStream, const HUF_DEltX2* dt, const U32 dtLog)1215{1216size_t const val = BIT_lookBitsFast(DStream, dtLog); /* note : dtLog >= 1 */1217ZSTD_memcpy(op, &dt[val].sequence, 2);1218BIT_skipBits(DStream, dt[val].nbBits);1219return dt[val].length;1220}12211222FORCE_INLINE_TEMPLATE U321223HUF_decodeLastSymbolX2(void* op, BIT_DStream_t* DStream, const HUF_DEltX2* dt, const U32 dtLog)1224{1225size_t const val = BIT_lookBitsFast(DStream, dtLog); /* note : dtLog >= 1 */1226ZSTD_memcpy(op, &dt[val].sequence, 1);1227if (dt[val].length==1) {1228BIT_skipBits(DStream, dt[val].nbBits);1229} else {1230if (DStream->bitsConsumed < (sizeof(DStream->bitContainer)*8)) {1231BIT_skipBits(DStream, dt[val].nbBits);1232if (DStream->bitsConsumed > (sizeof(DStream->bitContainer)*8))1233/* ugly hack; works only because it's the last symbol. Note : can't easily extract nbBits from just this symbol */1234DStream->bitsConsumed = (sizeof(DStream->bitContainer)*8);1235}1236}1237return 1;1238}12391240#define HUF_DECODE_SYMBOLX2_0(ptr, DStreamPtr) \1241ptr += HUF_decodeSymbolX2(ptr, DStreamPtr, dt, dtLog)12421243#define HUF_DECODE_SYMBOLX2_1(ptr, DStreamPtr) \1244if (MEM_64bits() || (HUF_TABLELOG_MAX<=12)) \1245ptr += HUF_decodeSymbolX2(ptr, DStreamPtr, dt, dtLog)12461247#define HUF_DECODE_SYMBOLX2_2(ptr, DStreamPtr) \1248if (MEM_64bits()) \1249ptr += HUF_decodeSymbolX2(ptr, DStreamPtr, dt, dtLog)12501251HINT_INLINE size_t1252HUF_decodeStreamX2(BYTE* p, BIT_DStream_t* bitDPtr, BYTE* const pEnd,1253const HUF_DEltX2* const dt, const U32 dtLog)1254{1255BYTE* const pStart = p;12561257/* up to 8 symbols at a time */1258if ((size_t)(pEnd - p) >= sizeof(bitDPtr->bitContainer)) {1259if (dtLog <= 11 && MEM_64bits()) {1260/* up to 10 symbols at a time */1261while ((BIT_reloadDStream(bitDPtr) == BIT_DStream_unfinished) & (p < pEnd-9)) {1262HUF_DECODE_SYMBOLX2_0(p, bitDPtr);1263HUF_DECODE_SYMBOLX2_0(p, bitDPtr);1264HUF_DECODE_SYMBOLX2_0(p, bitDPtr);1265HUF_DECODE_SYMBOLX2_0(p, bitDPtr);1266HUF_DECODE_SYMBOLX2_0(p, bitDPtr);1267}1268} else {1269/* up to 8 symbols at a time */1270while ((BIT_reloadDStream(bitDPtr) == BIT_DStream_unfinished) & (p < pEnd-(sizeof(bitDPtr->bitContainer)-1))) {1271HUF_DECODE_SYMBOLX2_2(p, bitDPtr);1272HUF_DECODE_SYMBOLX2_1(p, bitDPtr);1273HUF_DECODE_SYMBOLX2_2(p, bitDPtr);1274HUF_DECODE_SYMBOLX2_0(p, bitDPtr);1275}1276}1277} else {1278BIT_reloadDStream(bitDPtr);1279}12801281/* closer to end : up to 2 symbols at a time */1282if ((size_t)(pEnd - p) >= 2) {1283while ((BIT_reloadDStream(bitDPtr) == BIT_DStream_unfinished) & (p <= pEnd-2))1284HUF_DECODE_SYMBOLX2_0(p, bitDPtr);12851286while (p <= pEnd-2)1287HUF_DECODE_SYMBOLX2_0(p, bitDPtr); /* no need to reload : reached the end of DStream */1288}12891290if (p < pEnd)1291p += HUF_decodeLastSymbolX2(p, bitDPtr, dt, dtLog);12921293return p-pStart;1294}12951296FORCE_INLINE_TEMPLATE size_t1297HUF_decompress1X2_usingDTable_internal_body(1298void* dst, size_t dstSize,1299const void* cSrc, size_t cSrcSize,1300const HUF_DTable* DTable)1301{1302BIT_DStream_t bitD;13031304/* Init */1305CHECK_F( BIT_initDStream(&bitD, cSrc, cSrcSize) );13061307/* decode */1308{ BYTE* const ostart = (BYTE*) dst;1309BYTE* const oend = ostart + dstSize;1310const void* const dtPtr = DTable+1; /* force compiler to not use strict-aliasing */1311const HUF_DEltX2* const dt = (const HUF_DEltX2*)dtPtr;1312DTableDesc const dtd = HUF_getDTableDesc(DTable);1313HUF_decodeStreamX2(ostart, &bitD, oend, dt, dtd.tableLog);1314}13151316/* check */1317if (!BIT_endOfDStream(&bitD)) return ERROR(corruption_detected);13181319/* decoded size */1320return dstSize;1321}13221323/* HUF_decompress4X2_usingDTable_internal_body():1324* Conditions:1325* @dstSize >= 61326*/1327FORCE_INLINE_TEMPLATE size_t1328HUF_decompress4X2_usingDTable_internal_body(1329void* dst, size_t dstSize,1330const void* cSrc, size_t cSrcSize,1331const HUF_DTable* DTable)1332{1333if (cSrcSize < 10) return ERROR(corruption_detected); /* strict minimum : jump table + 1 byte per stream */13341335{ const BYTE* const istart = (const BYTE*) cSrc;1336BYTE* const ostart = (BYTE*) dst;1337BYTE* const oend = ostart + dstSize;1338BYTE* const olimit = oend - (sizeof(size_t)-1);1339const void* const dtPtr = DTable+1;1340const HUF_DEltX2* const dt = (const HUF_DEltX2*)dtPtr;13411342/* Init */1343BIT_DStream_t bitD1;1344BIT_DStream_t bitD2;1345BIT_DStream_t bitD3;1346BIT_DStream_t bitD4;1347size_t const length1 = MEM_readLE16(istart);1348size_t const length2 = MEM_readLE16(istart+2);1349size_t const length3 = MEM_readLE16(istart+4);1350size_t const length4 = cSrcSize - (length1 + length2 + length3 + 6);1351const BYTE* const istart1 = istart + 6; /* jumpTable */1352const BYTE* const istart2 = istart1 + length1;1353const BYTE* const istart3 = istart2 + length2;1354const BYTE* const istart4 = istart3 + length3;1355size_t const segmentSize = (dstSize+3) / 4;1356BYTE* const opStart2 = ostart + segmentSize;1357BYTE* const opStart3 = opStart2 + segmentSize;1358BYTE* const opStart4 = opStart3 + segmentSize;1359BYTE* op1 = ostart;1360BYTE* op2 = opStart2;1361BYTE* op3 = opStart3;1362BYTE* op4 = opStart4;1363U32 endSignal = 1;1364DTableDesc const dtd = HUF_getDTableDesc(DTable);1365U32 const dtLog = dtd.tableLog;13661367if (length4 > cSrcSize) return ERROR(corruption_detected); /* overflow */1368if (opStart4 > oend) return ERROR(corruption_detected); /* overflow */1369if (dstSize < 6) return ERROR(corruption_detected); /* stream 4-split doesn't work */1370CHECK_F( BIT_initDStream(&bitD1, istart1, length1) );1371CHECK_F( BIT_initDStream(&bitD2, istart2, length2) );1372CHECK_F( BIT_initDStream(&bitD3, istart3, length3) );1373CHECK_F( BIT_initDStream(&bitD4, istart4, length4) );13741375/* 16-32 symbols per loop (4-8 symbols per stream) */1376if ((size_t)(oend - op4) >= sizeof(size_t)) {1377for ( ; (endSignal) & (op4 < olimit); ) {1378#if defined(__clang__) && (defined(__x86_64__) || defined(__i386__))1379HUF_DECODE_SYMBOLX2_2(op1, &bitD1);1380HUF_DECODE_SYMBOLX2_1(op1, &bitD1);1381HUF_DECODE_SYMBOLX2_2(op1, &bitD1);1382HUF_DECODE_SYMBOLX2_0(op1, &bitD1);1383HUF_DECODE_SYMBOLX2_2(op2, &bitD2);1384HUF_DECODE_SYMBOLX2_1(op2, &bitD2);1385HUF_DECODE_SYMBOLX2_2(op2, &bitD2);1386HUF_DECODE_SYMBOLX2_0(op2, &bitD2);1387endSignal &= BIT_reloadDStreamFast(&bitD1) == BIT_DStream_unfinished;1388endSignal &= BIT_reloadDStreamFast(&bitD2) == BIT_DStream_unfinished;1389HUF_DECODE_SYMBOLX2_2(op3, &bitD3);1390HUF_DECODE_SYMBOLX2_1(op3, &bitD3);1391HUF_DECODE_SYMBOLX2_2(op3, &bitD3);1392HUF_DECODE_SYMBOLX2_0(op3, &bitD3);1393HUF_DECODE_SYMBOLX2_2(op4, &bitD4);1394HUF_DECODE_SYMBOLX2_1(op4, &bitD4);1395HUF_DECODE_SYMBOLX2_2(op4, &bitD4);1396HUF_DECODE_SYMBOLX2_0(op4, &bitD4);1397endSignal &= BIT_reloadDStreamFast(&bitD3) == BIT_DStream_unfinished;1398endSignal &= BIT_reloadDStreamFast(&bitD4) == BIT_DStream_unfinished;1399#else1400HUF_DECODE_SYMBOLX2_2(op1, &bitD1);1401HUF_DECODE_SYMBOLX2_2(op2, &bitD2);1402HUF_DECODE_SYMBOLX2_2(op3, &bitD3);1403HUF_DECODE_SYMBOLX2_2(op4, &bitD4);1404HUF_DECODE_SYMBOLX2_1(op1, &bitD1);1405HUF_DECODE_SYMBOLX2_1(op2, &bitD2);1406HUF_DECODE_SYMBOLX2_1(op3, &bitD3);1407HUF_DECODE_SYMBOLX2_1(op4, &bitD4);1408HUF_DECODE_SYMBOLX2_2(op1, &bitD1);1409HUF_DECODE_SYMBOLX2_2(op2, &bitD2);1410HUF_DECODE_SYMBOLX2_2(op3, &bitD3);1411HUF_DECODE_SYMBOLX2_2(op4, &bitD4);1412HUF_DECODE_SYMBOLX2_0(op1, &bitD1);1413HUF_DECODE_SYMBOLX2_0(op2, &bitD2);1414HUF_DECODE_SYMBOLX2_0(op3, &bitD3);1415HUF_DECODE_SYMBOLX2_0(op4, &bitD4);1416endSignal = (U32)LIKELY((U32)1417(BIT_reloadDStreamFast(&bitD1) == BIT_DStream_unfinished)1418& (BIT_reloadDStreamFast(&bitD2) == BIT_DStream_unfinished)1419& (BIT_reloadDStreamFast(&bitD3) == BIT_DStream_unfinished)1420& (BIT_reloadDStreamFast(&bitD4) == BIT_DStream_unfinished));1421#endif1422}1423}14241425/* check corruption */1426if (op1 > opStart2) return ERROR(corruption_detected);1427if (op2 > opStart3) return ERROR(corruption_detected);1428if (op3 > opStart4) return ERROR(corruption_detected);1429/* note : op4 already verified within main loop */14301431/* finish bitStreams one by one */1432HUF_decodeStreamX2(op1, &bitD1, opStart2, dt, dtLog);1433HUF_decodeStreamX2(op2, &bitD2, opStart3, dt, dtLog);1434HUF_decodeStreamX2(op3, &bitD3, opStart4, dt, dtLog);1435HUF_decodeStreamX2(op4, &bitD4, oend, dt, dtLog);14361437/* check */1438{ U32 const endCheck = BIT_endOfDStream(&bitD1) & BIT_endOfDStream(&bitD2) & BIT_endOfDStream(&bitD3) & BIT_endOfDStream(&bitD4);1439if (!endCheck) return ERROR(corruption_detected); }14401441/* decoded size */1442return dstSize;1443}1444}14451446#if HUF_NEED_BMI2_FUNCTION1447static BMI2_TARGET_ATTRIBUTE1448size_t HUF_decompress4X2_usingDTable_internal_bmi2(void* dst, size_t dstSize, void const* cSrc,1449size_t cSrcSize, HUF_DTable const* DTable) {1450return HUF_decompress4X2_usingDTable_internal_body(dst, dstSize, cSrc, cSrcSize, DTable);1451}1452#endif14531454static1455size_t HUF_decompress4X2_usingDTable_internal_default(void* dst, size_t dstSize, void const* cSrc,1456size_t cSrcSize, HUF_DTable const* DTable) {1457return HUF_decompress4X2_usingDTable_internal_body(dst, dstSize, cSrc, cSrcSize, DTable);1458}14591460#if ZSTD_ENABLE_ASM_X86_64_BMI214611462HUF_ASM_DECL void HUF_decompress4X2_usingDTable_internal_fast_asm_loop(HUF_DecompressFastArgs* args) ZSTDLIB_HIDDEN;14631464#endif14651466static HUF_FAST_BMI2_ATTRS1467void HUF_decompress4X2_usingDTable_internal_fast_c_loop(HUF_DecompressFastArgs* args)1468{1469U64 bits[4];1470BYTE const* ip[4];1471BYTE* op[4];1472BYTE* oend[4];1473HUF_DEltX2 const* const dtable = (HUF_DEltX2 const*)args->dt;1474BYTE const* const ilimit = args->ilimit;14751476/* Copy the arguments to local registers. */1477ZSTD_memcpy(&bits, &args->bits, sizeof(bits));1478ZSTD_memcpy((void*)(&ip), &args->ip, sizeof(ip));1479ZSTD_memcpy(&op, &args->op, sizeof(op));14801481oend[0] = op[1];1482oend[1] = op[2];1483oend[2] = op[3];1484oend[3] = args->oend;14851486assert(MEM_isLittleEndian());1487assert(!MEM_32bits());14881489for (;;) {1490BYTE* olimit;1491int stream;1492int symbol;14931494/* Assert loop preconditions */1495#ifndef NDEBUG1496for (stream = 0; stream < 4; ++stream) {1497assert(op[stream] <= oend[stream]);1498assert(ip[stream] >= ilimit);1499}1500#endif1501/* Compute olimit */1502{1503/* Each loop does 5 table lookups for each of the 4 streams.1504* Each table lookup consumes up to 11 bits of input, and produces1505* up to 2 bytes of output.1506*/1507/* We can consume up to 7 bytes of input per iteration per stream.1508* We also know that each input pointer is >= ip[0]. So we can run1509* iters loops before running out of input.1510*/1511size_t iters = (size_t)(ip[0] - ilimit) / 7;1512/* Each iteration can produce up to 10 bytes of output per stream.1513* Each output stream my advance at different rates. So take the1514* minimum number of safe iterations among all the output streams.1515*/1516for (stream = 0; stream < 4; ++stream) {1517size_t const oiters = (size_t)(oend[stream] - op[stream]) / 10;1518iters = MIN(iters, oiters);1519}15201521/* Each iteration produces at least 5 output symbols. So until1522* op[3] crosses olimit, we know we haven't executed iters1523* iterations yet. This saves us maintaining an iters counter,1524* at the expense of computing the remaining # of iterations1525* more frequently.1526*/1527olimit = op[3] + (iters * 5);15281529/* Exit the fast decoding loop if we are too close to the end. */1530if (op[3] + 10 > olimit)1531break;15321533/* Exit the decoding loop if any input pointer has crossed the1534* previous one. This indicates corruption, and a precondition1535* to our loop is that ip[i] >= ip[0].1536*/1537for (stream = 1; stream < 4; ++stream) {1538if (ip[stream] < ip[stream - 1])1539goto _out;1540}1541}15421543#ifndef NDEBUG1544for (stream = 1; stream < 4; ++stream) {1545assert(ip[stream] >= ip[stream - 1]);1546}1547#endif15481549do {1550/* Do 5 table lookups for each of the first 3 streams */1551for (symbol = 0; symbol < 5; ++symbol) {1552for (stream = 0; stream < 3; ++stream) {1553int const index = (int)(bits[stream] >> 53);1554HUF_DEltX2 const entry = dtable[index];1555MEM_write16(op[stream], entry.sequence);1556bits[stream] <<= (entry.nbBits);1557op[stream] += (entry.length);1558}1559}1560/* Do 1 table lookup from the final stream */1561{1562int const index = (int)(bits[3] >> 53);1563HUF_DEltX2 const entry = dtable[index];1564MEM_write16(op[3], entry.sequence);1565bits[3] <<= (entry.nbBits);1566op[3] += (entry.length);1567}1568/* Do 4 table lookups from the final stream & reload bitstreams */1569for (stream = 0; stream < 4; ++stream) {1570/* Do a table lookup from the final stream.1571* This is interleaved with the reloading to reduce register1572* pressure. This shouldn't be necessary, but compilers can1573* struggle with codegen with high register pressure.1574*/1575{1576int const index = (int)(bits[3] >> 53);1577HUF_DEltX2 const entry = dtable[index];1578MEM_write16(op[3], entry.sequence);1579bits[3] <<= (entry.nbBits);1580op[3] += (entry.length);1581}1582/* Reload the bistreams. The final bitstream must be reloaded1583* after the 5th symbol was decoded.1584*/1585{1586int const ctz = ZSTD_countTrailingZeros64(bits[stream]);1587int const nbBits = ctz & 7;1588int const nbBytes = ctz >> 3;1589ip[stream] -= nbBytes;1590bits[stream] = MEM_read64(ip[stream]) | 1;1591bits[stream] <<= nbBits;1592}1593}1594} while (op[3] < olimit);1595}15961597_out:15981599/* Save the final values of each of the state variables back to args. */1600ZSTD_memcpy(&args->bits, &bits, sizeof(bits));1601ZSTD_memcpy((void*)(&args->ip), &ip, sizeof(ip));1602ZSTD_memcpy(&args->op, &op, sizeof(op));1603}160416051606static HUF_FAST_BMI2_ATTRS size_t1607HUF_decompress4X2_usingDTable_internal_fast(1608void* dst, size_t dstSize,1609const void* cSrc, size_t cSrcSize,1610const HUF_DTable* DTable,1611HUF_DecompressFastLoopFn loopFn) {1612void const* dt = DTable + 1;1613const BYTE* const iend = (const BYTE*)cSrc + 6;1614BYTE* const oend = (BYTE*)dst + dstSize;1615HUF_DecompressFastArgs args;1616{1617size_t const ret = HUF_DecompressFastArgs_init(&args, dst, dstSize, cSrc, cSrcSize, DTable);1618FORWARD_IF_ERROR(ret, "Failed to init asm args");1619if (ret == 0)1620return 0;1621}16221623assert(args.ip[0] >= args.ilimit);1624loopFn(&args);16251626/* note : op4 already verified within main loop */1627assert(args.ip[0] >= iend);1628assert(args.ip[1] >= iend);1629assert(args.ip[2] >= iend);1630assert(args.ip[3] >= iend);1631assert(args.op[3] <= oend);1632(void)iend;16331634/* finish bitStreams one by one */1635{1636size_t const segmentSize = (dstSize+3) / 4;1637BYTE* segmentEnd = (BYTE*)dst;1638int i;1639for (i = 0; i < 4; ++i) {1640BIT_DStream_t bit;1641if (segmentSize <= (size_t)(oend - segmentEnd))1642segmentEnd += segmentSize;1643else1644segmentEnd = oend;1645FORWARD_IF_ERROR(HUF_initRemainingDStream(&bit, &args, i, segmentEnd), "corruption");1646args.op[i] += HUF_decodeStreamX2(args.op[i], &bit, segmentEnd, (HUF_DEltX2 const*)dt, HUF_DECODER_FAST_TABLELOG);1647if (args.op[i] != segmentEnd)1648return ERROR(corruption_detected);1649}1650}16511652/* decoded size */1653return dstSize;1654}16551656static size_t HUF_decompress4X2_usingDTable_internal(void* dst, size_t dstSize, void const* cSrc,1657size_t cSrcSize, HUF_DTable const* DTable, int flags)1658{1659HUF_DecompressUsingDTableFn fallbackFn = HUF_decompress4X2_usingDTable_internal_default;1660HUF_DecompressFastLoopFn loopFn = HUF_decompress4X2_usingDTable_internal_fast_c_loop;16611662#if DYNAMIC_BMI21663if (flags & HUF_flags_bmi2) {1664fallbackFn = HUF_decompress4X2_usingDTable_internal_bmi2;1665# if ZSTD_ENABLE_ASM_X86_64_BMI21666if (!(flags & HUF_flags_disableAsm)) {1667loopFn = HUF_decompress4X2_usingDTable_internal_fast_asm_loop;1668}1669# endif1670} else {1671return fallbackFn(dst, dstSize, cSrc, cSrcSize, DTable);1672}1673#endif16741675#if ZSTD_ENABLE_ASM_X86_64_BMI2 && defined(__BMI2__)1676if (!(flags & HUF_flags_disableAsm)) {1677loopFn = HUF_decompress4X2_usingDTable_internal_fast_asm_loop;1678}1679#endif16801681if (!(flags & HUF_flags_disableFast)) {1682size_t const ret = HUF_decompress4X2_usingDTable_internal_fast(dst, dstSize, cSrc, cSrcSize, DTable, loopFn);1683if (ret != 0)1684return ret;1685}1686return fallbackFn(dst, dstSize, cSrc, cSrcSize, DTable);1687}16881689HUF_DGEN(HUF_decompress1X2_usingDTable_internal)16901691size_t HUF_decompress1X2_DCtx_wksp(HUF_DTable* DCtx, void* dst, size_t dstSize,1692const void* cSrc, size_t cSrcSize,1693void* workSpace, size_t wkspSize, int flags)1694{1695const BYTE* ip = (const BYTE*) cSrc;16961697size_t const hSize = HUF_readDTableX2_wksp(DCtx, cSrc, cSrcSize,1698workSpace, wkspSize, flags);1699if (HUF_isError(hSize)) return hSize;1700if (hSize >= cSrcSize) return ERROR(srcSize_wrong);1701ip += hSize; cSrcSize -= hSize;17021703return HUF_decompress1X2_usingDTable_internal(dst, dstSize, ip, cSrcSize, DCtx, flags);1704}17051706static size_t HUF_decompress4X2_DCtx_wksp(HUF_DTable* dctx, void* dst, size_t dstSize,1707const void* cSrc, size_t cSrcSize,1708void* workSpace, size_t wkspSize, int flags)1709{1710const BYTE* ip = (const BYTE*) cSrc;17111712size_t hSize = HUF_readDTableX2_wksp(dctx, cSrc, cSrcSize,1713workSpace, wkspSize, flags);1714if (HUF_isError(hSize)) return hSize;1715if (hSize >= cSrcSize) return ERROR(srcSize_wrong);1716ip += hSize; cSrcSize -= hSize;17171718return HUF_decompress4X2_usingDTable_internal(dst, dstSize, ip, cSrcSize, dctx, flags);1719}17201721#endif /* HUF_FORCE_DECOMPRESS_X1 */172217231724/* ***********************************/1725/* Universal decompression selectors */1726/* ***********************************/172717281729#if !defined(HUF_FORCE_DECOMPRESS_X1) && !defined(HUF_FORCE_DECOMPRESS_X2)1730typedef struct { U32 tableTime; U32 decode256Time; } algo_time_t;1731static const algo_time_t algoTime[16 /* Quantization */][2 /* single, double */] =1732{1733/* single, double, quad */1734{{0,0}, {1,1}}, /* Q==0 : impossible */1735{{0,0}, {1,1}}, /* Q==1 : impossible */1736{{ 150,216}, { 381,119}}, /* Q == 2 : 12-18% */1737{{ 170,205}, { 514,112}}, /* Q == 3 : 18-25% */1738{{ 177,199}, { 539,110}}, /* Q == 4 : 25-32% */1739{{ 197,194}, { 644,107}}, /* Q == 5 : 32-38% */1740{{ 221,192}, { 735,107}}, /* Q == 6 : 38-44% */1741{{ 256,189}, { 881,106}}, /* Q == 7 : 44-50% */1742{{ 359,188}, {1167,109}}, /* Q == 8 : 50-56% */1743{{ 582,187}, {1570,114}}, /* Q == 9 : 56-62% */1744{{ 688,187}, {1712,122}}, /* Q ==10 : 62-69% */1745{{ 825,186}, {1965,136}}, /* Q ==11 : 69-75% */1746{{ 976,185}, {2131,150}}, /* Q ==12 : 75-81% */1747{{1180,186}, {2070,175}}, /* Q ==13 : 81-87% */1748{{1377,185}, {1731,202}}, /* Q ==14 : 87-93% */1749{{1412,185}, {1695,202}}, /* Q ==15 : 93-99% */1750};1751#endif17521753/** HUF_selectDecoder() :1754* Tells which decoder is likely to decode faster,1755* based on a set of pre-computed metrics.1756* @return : 0==HUF_decompress4X1, 1==HUF_decompress4X2 .1757* Assumption : 0 < dstSize <= 128 KB */1758U32 HUF_selectDecoder (size_t dstSize, size_t cSrcSize)1759{1760assert(dstSize > 0);1761assert(dstSize <= 128*1024);1762#if defined(HUF_FORCE_DECOMPRESS_X1)1763(void)dstSize;1764(void)cSrcSize;1765return 0;1766#elif defined(HUF_FORCE_DECOMPRESS_X2)1767(void)dstSize;1768(void)cSrcSize;1769return 1;1770#else1771/* decoder timing evaluation */1772{ U32 const Q = (cSrcSize >= dstSize) ? 15 : (U32)(cSrcSize * 16 / dstSize); /* Q < 16 */1773U32 const D256 = (U32)(dstSize >> 8);1774U32 const DTime0 = algoTime[Q][0].tableTime + (algoTime[Q][0].decode256Time * D256);1775U32 DTime1 = algoTime[Q][1].tableTime + (algoTime[Q][1].decode256Time * D256);1776DTime1 += DTime1 >> 5; /* small advantage to algorithm using less memory, to reduce cache eviction */1777return DTime1 < DTime0;1778}1779#endif1780}17811782size_t HUF_decompress1X_DCtx_wksp(HUF_DTable* dctx, void* dst, size_t dstSize,1783const void* cSrc, size_t cSrcSize,1784void* workSpace, size_t wkspSize, int flags)1785{1786/* validation checks */1787if (dstSize == 0) return ERROR(dstSize_tooSmall);1788if (cSrcSize > dstSize) return ERROR(corruption_detected); /* invalid */1789if (cSrcSize == dstSize) { ZSTD_memcpy(dst, cSrc, dstSize); return dstSize; } /* not compressed */1790if (cSrcSize == 1) { ZSTD_memset(dst, *(const BYTE*)cSrc, dstSize); return dstSize; } /* RLE */17911792{ U32 const algoNb = HUF_selectDecoder(dstSize, cSrcSize);1793#if defined(HUF_FORCE_DECOMPRESS_X1)1794(void)algoNb;1795assert(algoNb == 0);1796return HUF_decompress1X1_DCtx_wksp(dctx, dst, dstSize, cSrc,1797cSrcSize, workSpace, wkspSize, flags);1798#elif defined(HUF_FORCE_DECOMPRESS_X2)1799(void)algoNb;1800assert(algoNb == 1);1801return HUF_decompress1X2_DCtx_wksp(dctx, dst, dstSize, cSrc,1802cSrcSize, workSpace, wkspSize, flags);1803#else1804return algoNb ? HUF_decompress1X2_DCtx_wksp(dctx, dst, dstSize, cSrc,1805cSrcSize, workSpace, wkspSize, flags):1806HUF_decompress1X1_DCtx_wksp(dctx, dst, dstSize, cSrc,1807cSrcSize, workSpace, wkspSize, flags);1808#endif1809}1810}181118121813size_t HUF_decompress1X_usingDTable(void* dst, size_t maxDstSize, const void* cSrc, size_t cSrcSize, const HUF_DTable* DTable, int flags)1814{1815DTableDesc const dtd = HUF_getDTableDesc(DTable);1816#if defined(HUF_FORCE_DECOMPRESS_X1)1817(void)dtd;1818assert(dtd.tableType == 0);1819return HUF_decompress1X1_usingDTable_internal(dst, maxDstSize, cSrc, cSrcSize, DTable, flags);1820#elif defined(HUF_FORCE_DECOMPRESS_X2)1821(void)dtd;1822assert(dtd.tableType == 1);1823return HUF_decompress1X2_usingDTable_internal(dst, maxDstSize, cSrc, cSrcSize, DTable, flags);1824#else1825return dtd.tableType ? HUF_decompress1X2_usingDTable_internal(dst, maxDstSize, cSrc, cSrcSize, DTable, flags) :1826HUF_decompress1X1_usingDTable_internal(dst, maxDstSize, cSrc, cSrcSize, DTable, flags);1827#endif1828}18291830#ifndef HUF_FORCE_DECOMPRESS_X21831size_t HUF_decompress1X1_DCtx_wksp(HUF_DTable* dctx, void* dst, size_t dstSize, const void* cSrc, size_t cSrcSize, void* workSpace, size_t wkspSize, int flags)1832{1833const BYTE* ip = (const BYTE*) cSrc;18341835size_t const hSize = HUF_readDTableX1_wksp(dctx, cSrc, cSrcSize, workSpace, wkspSize, flags);1836if (HUF_isError(hSize)) return hSize;1837if (hSize >= cSrcSize) return ERROR(srcSize_wrong);1838ip += hSize; cSrcSize -= hSize;18391840return HUF_decompress1X1_usingDTable_internal(dst, dstSize, ip, cSrcSize, dctx, flags);1841}1842#endif18431844size_t HUF_decompress4X_usingDTable(void* dst, size_t maxDstSize, const void* cSrc, size_t cSrcSize, const HUF_DTable* DTable, int flags)1845{1846DTableDesc const dtd = HUF_getDTableDesc(DTable);1847#if defined(HUF_FORCE_DECOMPRESS_X1)1848(void)dtd;1849assert(dtd.tableType == 0);1850return HUF_decompress4X1_usingDTable_internal(dst, maxDstSize, cSrc, cSrcSize, DTable, flags);1851#elif defined(HUF_FORCE_DECOMPRESS_X2)1852(void)dtd;1853assert(dtd.tableType == 1);1854return HUF_decompress4X2_usingDTable_internal(dst, maxDstSize, cSrc, cSrcSize, DTable, flags);1855#else1856return dtd.tableType ? HUF_decompress4X2_usingDTable_internal(dst, maxDstSize, cSrc, cSrcSize, DTable, flags) :1857HUF_decompress4X1_usingDTable_internal(dst, maxDstSize, cSrc, cSrcSize, DTable, flags);1858#endif1859}18601861size_t HUF_decompress4X_hufOnly_wksp(HUF_DTable* dctx, void* dst, size_t dstSize, const void* cSrc, size_t cSrcSize, void* workSpace, size_t wkspSize, int flags)1862{1863/* validation checks */1864if (dstSize == 0) return ERROR(dstSize_tooSmall);1865if (cSrcSize == 0) return ERROR(corruption_detected);18661867{ U32 const algoNb = HUF_selectDecoder(dstSize, cSrcSize);1868#if defined(HUF_FORCE_DECOMPRESS_X1)1869(void)algoNb;1870assert(algoNb == 0);1871return HUF_decompress4X1_DCtx_wksp(dctx, dst, dstSize, cSrc, cSrcSize, workSpace, wkspSize, flags);1872#elif defined(HUF_FORCE_DECOMPRESS_X2)1873(void)algoNb;1874assert(algoNb == 1);1875return HUF_decompress4X2_DCtx_wksp(dctx, dst, dstSize, cSrc, cSrcSize, workSpace, wkspSize, flags);1876#else1877return algoNb ? HUF_decompress4X2_DCtx_wksp(dctx, dst, dstSize, cSrc, cSrcSize, workSpace, wkspSize, flags) :1878HUF_decompress4X1_DCtx_wksp(dctx, dst, dstSize, cSrc, cSrcSize, workSpace, wkspSize, flags);1879#endif1880}1881}188218831884