^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) * Huffman decoder, part of New Generation Entropy library
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) * Copyright (C) 2013-2016, Yann Collet.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) * BSD 2-Clause License (http://www.opensource.org/licenses/bsd-license.php)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) * Redistribution and use in source and binary forms, with or without
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) * modification, are permitted provided that the following conditions are
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) * met:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) * * Redistributions of source code must retain the above copyright
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) * notice, this list of conditions and the following disclaimer.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) * * Redistributions in binary form must reproduce the above
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) * copyright notice, this list of conditions and the following disclaimer
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) * in the documentation and/or other materials provided with the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) * distribution.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) * This program is free software; you can redistribute it and/or modify it under
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) * the terms of the GNU General Public License version 2 as published by the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) * Free Software Foundation. This program is dual-licensed; you may select
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) * either version 2 of the GNU General Public License ("GPL") or BSD license
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) * ("BSD").
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) * You can contact the author at :
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) * - Source repository : https://github.com/Cyan4973/FiniteStateEntropy
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) /* **************************************************************
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) * Compiler specifics
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) ****************************************************************/
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) #define FORCE_INLINE static __always_inline
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) /* **************************************************************
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) * Dependencies
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) ****************************************************************/
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) #include "bitstream.h" /* BIT_* */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) #include "fse.h" /* header compression */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) #include "huf.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) #include <linux/compiler.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) #include <linux/kernel.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) #include <linux/string.h> /* memcpy, memset */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) /* **************************************************************
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) * Error Management
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) ****************************************************************/
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) #define HUF_STATIC_ASSERT(c) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) { \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) enum { HUF_static_assert = 1 / (int)(!!(c)) }; \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) } /* use only *after* variable declarations */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) /*-***************************/
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) /* generic DTableDesc */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) /*-***************************/
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) typedef struct {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) BYTE maxTableLog;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) BYTE tableType;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) BYTE tableLog;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) BYTE reserved;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) } DTableDesc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) static DTableDesc HUF_getDTableDesc(const HUF_DTable *table)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) DTableDesc dtd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) memcpy(&dtd, table, sizeof(dtd));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) return dtd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) /*-***************************/
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) /* single-symbol decoding */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) /*-***************************/
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) typedef struct {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) BYTE byte;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) BYTE nbBits;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) } HUF_DEltX2; /* single-symbol decoding */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) size_t HUF_readDTableX2_wksp(HUF_DTable *DTable, const void *src, size_t srcSize, void *workspace, size_t workspaceSize)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) U32 tableLog = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) U32 nbSymbols = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) size_t iSize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) void *const dtPtr = DTable + 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) HUF_DEltX2 *const dt = (HUF_DEltX2 *)dtPtr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) U32 *rankVal;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) BYTE *huffWeight;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) size_t spaceUsed32 = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) rankVal = (U32 *)workspace + spaceUsed32;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) spaceUsed32 += HUF_TABLELOG_ABSOLUTEMAX + 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) huffWeight = (BYTE *)((U32 *)workspace + spaceUsed32);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) spaceUsed32 += ALIGN(HUF_SYMBOLVALUE_MAX + 1, sizeof(U32)) >> 2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) if ((spaceUsed32 << 2) > workspaceSize)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) return ERROR(tableLog_tooLarge);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) workspace = (U32 *)workspace + spaceUsed32;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) workspaceSize -= (spaceUsed32 << 2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) HUF_STATIC_ASSERT(sizeof(DTableDesc) == sizeof(HUF_DTable));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) /* memset(huffWeight, 0, sizeof(huffWeight)); */ /* is not necessary, even though some analyzer complain ... */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) iSize = HUF_readStats_wksp(huffWeight, HUF_SYMBOLVALUE_MAX + 1, rankVal, &nbSymbols, &tableLog, src, srcSize, workspace, workspaceSize);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) if (HUF_isError(iSize))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) return iSize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) /* Table header */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) DTableDesc dtd = HUF_getDTableDesc(DTable);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) if (tableLog > (U32)(dtd.maxTableLog + 1))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) return ERROR(tableLog_tooLarge); /* DTable too small, Huffman tree cannot fit in */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) dtd.tableType = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) dtd.tableLog = (BYTE)tableLog;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) memcpy(DTable, &dtd, sizeof(dtd));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) /* Calculate starting value for each rank */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) U32 n, nextRankStart = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) for (n = 1; n < tableLog + 1; n++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) U32 const curr = nextRankStart;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) nextRankStart += (rankVal[n] << (n - 1));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) rankVal[n] = curr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) /* fill DTable */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) U32 n;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) for (n = 0; n < nbSymbols; n++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) U32 const w = huffWeight[n];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) U32 const length = (1 << w) >> 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) U32 u;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) HUF_DEltX2 D;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) D.byte = (BYTE)n;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) D.nbBits = (BYTE)(tableLog + 1 - w);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) for (u = rankVal[w]; u < rankVal[w] + length; u++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) dt[u] = D;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) rankVal[w] += length;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) return iSize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) static BYTE HUF_decodeSymbolX2(BIT_DStream_t *Dstream, const HUF_DEltX2 *dt, const U32 dtLog)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) size_t const val = BIT_lookBitsFast(Dstream, dtLog); /* note : dtLog >= 1 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) BYTE const c = dt[val].byte;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) BIT_skipBits(Dstream, dt[val].nbBits);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) return c;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) #define HUF_DECODE_SYMBOLX2_0(ptr, DStreamPtr) *ptr++ = HUF_decodeSymbolX2(DStreamPtr, dt, dtLog)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) #define HUF_DECODE_SYMBOLX2_1(ptr, DStreamPtr) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) if (ZSTD_64bits() || (HUF_TABLELOG_MAX <= 12)) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) HUF_DECODE_SYMBOLX2_0(ptr, DStreamPtr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) #define HUF_DECODE_SYMBOLX2_2(ptr, DStreamPtr) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) if (ZSTD_64bits()) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) HUF_DECODE_SYMBOLX2_0(ptr, DStreamPtr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) FORCE_INLINE size_t HUF_decodeStreamX2(BYTE *p, BIT_DStream_t *const bitDPtr, BYTE *const pEnd, const HUF_DEltX2 *const dt, const U32 dtLog)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) BYTE *const pStart = p;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) /* up to 4 symbols at a time */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) while ((BIT_reloadDStream(bitDPtr) == BIT_DStream_unfinished) && (p <= pEnd - 4)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) HUF_DECODE_SYMBOLX2_2(p, bitDPtr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) HUF_DECODE_SYMBOLX2_1(p, bitDPtr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) HUF_DECODE_SYMBOLX2_2(p, bitDPtr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) HUF_DECODE_SYMBOLX2_0(p, bitDPtr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) /* closer to the end */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) while ((BIT_reloadDStream(bitDPtr) == BIT_DStream_unfinished) && (p < pEnd))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) HUF_DECODE_SYMBOLX2_0(p, bitDPtr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) /* no more data to retrieve from bitstream, hence no need to reload */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) while (p < pEnd)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) HUF_DECODE_SYMBOLX2_0(p, bitDPtr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) return pEnd - pStart;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) static size_t HUF_decompress1X2_usingDTable_internal(void *dst, size_t dstSize, const void *cSrc, size_t cSrcSize, const HUF_DTable *DTable)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) BYTE *op = (BYTE *)dst;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) BYTE *const oend = op + dstSize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) const void *dtPtr = DTable + 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) const HUF_DEltX2 *const dt = (const HUF_DEltX2 *)dtPtr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) BIT_DStream_t bitD;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) DTableDesc const dtd = HUF_getDTableDesc(DTable);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) U32 const dtLog = dtd.tableLog;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) size_t const errorCode = BIT_initDStream(&bitD, cSrc, cSrcSize);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) if (HUF_isError(errorCode))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) return errorCode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) HUF_decodeStreamX2(op, &bitD, oend, dt, dtLog);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) /* check */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) if (!BIT_endOfDStream(&bitD))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) return ERROR(corruption_detected);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) return dstSize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) size_t HUF_decompress1X2_usingDTable(void *dst, size_t dstSize, const void *cSrc, size_t cSrcSize, const HUF_DTable *DTable)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) DTableDesc dtd = HUF_getDTableDesc(DTable);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) if (dtd.tableType != 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) return ERROR(GENERIC);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) return HUF_decompress1X2_usingDTable_internal(dst, dstSize, cSrc, cSrcSize, DTable);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) size_t HUF_decompress1X2_DCtx_wksp(HUF_DTable *DCtx, void *dst, size_t dstSize, const void *cSrc, size_t cSrcSize, void *workspace, size_t workspaceSize)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) const BYTE *ip = (const BYTE *)cSrc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) size_t const hSize = HUF_readDTableX2_wksp(DCtx, cSrc, cSrcSize, workspace, workspaceSize);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) if (HUF_isError(hSize))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) return hSize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) if (hSize >= cSrcSize)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) return ERROR(srcSize_wrong);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) ip += hSize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) cSrcSize -= hSize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) return HUF_decompress1X2_usingDTable_internal(dst, dstSize, ip, cSrcSize, DCtx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) static size_t HUF_decompress4X2_usingDTable_internal(void *dst, size_t dstSize, const void *cSrc, size_t cSrcSize, const HUF_DTable *DTable)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) /* Check */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) if (cSrcSize < 10)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) return ERROR(corruption_detected); /* strict minimum : jump table + 1 byte per stream */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) const BYTE *const istart = (const BYTE *)cSrc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) BYTE *const ostart = (BYTE *)dst;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) BYTE *const oend = ostart + dstSize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) const void *const dtPtr = DTable + 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) const HUF_DEltX2 *const dt = (const HUF_DEltX2 *)dtPtr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) /* Init */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) BIT_DStream_t bitD1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) BIT_DStream_t bitD2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) BIT_DStream_t bitD3;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) BIT_DStream_t bitD4;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) size_t const length1 = ZSTD_readLE16(istart);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) size_t const length2 = ZSTD_readLE16(istart + 2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) size_t const length3 = ZSTD_readLE16(istart + 4);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) size_t const length4 = cSrcSize - (length1 + length2 + length3 + 6);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) const BYTE *const istart1 = istart + 6; /* jumpTable */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) const BYTE *const istart2 = istart1 + length1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) const BYTE *const istart3 = istart2 + length2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) const BYTE *const istart4 = istart3 + length3;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) const size_t segmentSize = (dstSize + 3) / 4;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) BYTE *const opStart2 = ostart + segmentSize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) BYTE *const opStart3 = opStart2 + segmentSize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) BYTE *const opStart4 = opStart3 + segmentSize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) BYTE *op1 = ostart;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) BYTE *op2 = opStart2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) BYTE *op3 = opStart3;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) BYTE *op4 = opStart4;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) U32 endSignal;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) DTableDesc const dtd = HUF_getDTableDesc(DTable);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) U32 const dtLog = dtd.tableLog;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) if (length4 > cSrcSize)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) return ERROR(corruption_detected); /* overflow */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) size_t const errorCode = BIT_initDStream(&bitD1, istart1, length1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) if (HUF_isError(errorCode))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) return errorCode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) size_t const errorCode = BIT_initDStream(&bitD2, istart2, length2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) if (HUF_isError(errorCode))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) return errorCode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) size_t const errorCode = BIT_initDStream(&bitD3, istart3, length3);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) if (HUF_isError(errorCode))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) return errorCode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) size_t const errorCode = BIT_initDStream(&bitD4, istart4, length4);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) if (HUF_isError(errorCode))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) return errorCode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) /* 16-32 symbols per loop (4-8 symbols per stream) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) endSignal = BIT_reloadDStream(&bitD1) | BIT_reloadDStream(&bitD2) | BIT_reloadDStream(&bitD3) | BIT_reloadDStream(&bitD4);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) for (; (endSignal == BIT_DStream_unfinished) && (op4 < (oend - 7));) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) HUF_DECODE_SYMBOLX2_2(op1, &bitD1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) HUF_DECODE_SYMBOLX2_2(op2, &bitD2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) HUF_DECODE_SYMBOLX2_2(op3, &bitD3);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) HUF_DECODE_SYMBOLX2_2(op4, &bitD4);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) HUF_DECODE_SYMBOLX2_1(op1, &bitD1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) HUF_DECODE_SYMBOLX2_1(op2, &bitD2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) HUF_DECODE_SYMBOLX2_1(op3, &bitD3);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) HUF_DECODE_SYMBOLX2_1(op4, &bitD4);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) HUF_DECODE_SYMBOLX2_2(op1, &bitD1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) HUF_DECODE_SYMBOLX2_2(op2, &bitD2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) HUF_DECODE_SYMBOLX2_2(op3, &bitD3);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) HUF_DECODE_SYMBOLX2_2(op4, &bitD4);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) HUF_DECODE_SYMBOLX2_0(op1, &bitD1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) HUF_DECODE_SYMBOLX2_0(op2, &bitD2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) HUF_DECODE_SYMBOLX2_0(op3, &bitD3);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) HUF_DECODE_SYMBOLX2_0(op4, &bitD4);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) endSignal = BIT_reloadDStream(&bitD1) | BIT_reloadDStream(&bitD2) | BIT_reloadDStream(&bitD3) | BIT_reloadDStream(&bitD4);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) /* check corruption */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) if (op1 > opStart2)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) return ERROR(corruption_detected);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) if (op2 > opStart3)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) return ERROR(corruption_detected);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) if (op3 > opStart4)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) return ERROR(corruption_detected);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) /* note : op4 supposed already verified within main loop */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) /* finish bitStreams one by one */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) HUF_decodeStreamX2(op1, &bitD1, opStart2, dt, dtLog);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) HUF_decodeStreamX2(op2, &bitD2, opStart3, dt, dtLog);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) HUF_decodeStreamX2(op3, &bitD3, opStart4, dt, dtLog);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) HUF_decodeStreamX2(op4, &bitD4, oend, dt, dtLog);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) /* check */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) endSignal = BIT_endOfDStream(&bitD1) & BIT_endOfDStream(&bitD2) & BIT_endOfDStream(&bitD3) & BIT_endOfDStream(&bitD4);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) if (!endSignal)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) return ERROR(corruption_detected);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) /* decoded size */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) return dstSize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) size_t HUF_decompress4X2_usingDTable(void *dst, size_t dstSize, const void *cSrc, size_t cSrcSize, const HUF_DTable *DTable)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357) DTableDesc dtd = HUF_getDTableDesc(DTable);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358) if (dtd.tableType != 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359) return ERROR(GENERIC);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360) return HUF_decompress4X2_usingDTable_internal(dst, dstSize, cSrc, cSrcSize, DTable);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363) size_t HUF_decompress4X2_DCtx_wksp(HUF_DTable *dctx, void *dst, size_t dstSize, const void *cSrc, size_t cSrcSize, void *workspace, size_t workspaceSize)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365) const BYTE *ip = (const BYTE *)cSrc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367) size_t const hSize = HUF_readDTableX2_wksp(dctx, cSrc, cSrcSize, workspace, workspaceSize);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368) if (HUF_isError(hSize))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369) return hSize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370) if (hSize >= cSrcSize)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371) return ERROR(srcSize_wrong);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372) ip += hSize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373) cSrcSize -= hSize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375) return HUF_decompress4X2_usingDTable_internal(dst, dstSize, ip, cSrcSize, dctx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378) /* *************************/
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379) /* double-symbols decoding */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380) /* *************************/
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381) typedef struct {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382) U16 sequence;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383) BYTE nbBits;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384) BYTE length;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385) } HUF_DEltX4; /* double-symbols decoding */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387) typedef struct {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388) BYTE symbol;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389) BYTE weight;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 390) } sortedSymbol_t;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 391)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 392) /* HUF_fillDTableX4Level2() :
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 393) * `rankValOrigin` must be a table of at least (HUF_TABLELOG_MAX + 1) U32 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 394) static void HUF_fillDTableX4Level2(HUF_DEltX4 *DTable, U32 sizeLog, const U32 consumed, const U32 *rankValOrigin, const int minWeight,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 395) const sortedSymbol_t *sortedSymbols, const U32 sortedListSize, U32 nbBitsBaseline, U16 baseSeq)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 396) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 397) HUF_DEltX4 DElt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 398) U32 rankVal[HUF_TABLELOG_MAX + 1];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 399)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 400) /* get pre-calculated rankVal */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 401) memcpy(rankVal, rankValOrigin, sizeof(rankVal));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 402)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 403) /* fill skipped values */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 404) if (minWeight > 1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 405) U32 i, skipSize = rankVal[minWeight];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 406) ZSTD_writeLE16(&(DElt.sequence), baseSeq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 407) DElt.nbBits = (BYTE)(consumed);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 408) DElt.length = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 409) for (i = 0; i < skipSize; i++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 410) DTable[i] = DElt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 411) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 412)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 413) /* fill DTable */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 414) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 415) U32 s;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 416) for (s = 0; s < sortedListSize; s++) { /* note : sortedSymbols already skipped */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 417) const U32 symbol = sortedSymbols[s].symbol;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 418) const U32 weight = sortedSymbols[s].weight;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 419) const U32 nbBits = nbBitsBaseline - weight;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 420) const U32 length = 1 << (sizeLog - nbBits);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 421) const U32 start = rankVal[weight];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 422) U32 i = start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 423) const U32 end = start + length;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 424)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 425) ZSTD_writeLE16(&(DElt.sequence), (U16)(baseSeq + (symbol << 8)));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 426) DElt.nbBits = (BYTE)(nbBits + consumed);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 427) DElt.length = 2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 428) do {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 429) DTable[i++] = DElt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 430) } while (i < end); /* since length >= 1 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 431)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 432) rankVal[weight] += length;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 433) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 434) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 435) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 436)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 437) typedef U32 rankVal_t[HUF_TABLELOG_MAX][HUF_TABLELOG_MAX + 1];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 438) typedef U32 rankValCol_t[HUF_TABLELOG_MAX + 1];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 439)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 440) static void HUF_fillDTableX4(HUF_DEltX4 *DTable, const U32 targetLog, const sortedSymbol_t *sortedList, const U32 sortedListSize, const U32 *rankStart,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 441) rankVal_t rankValOrigin, const U32 maxWeight, const U32 nbBitsBaseline)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 442) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 443) U32 rankVal[HUF_TABLELOG_MAX + 1];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 444) const int scaleLog = nbBitsBaseline - targetLog; /* note : targetLog >= srcLog, hence scaleLog <= 1 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 445) const U32 minBits = nbBitsBaseline - maxWeight;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 446) U32 s;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 447)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 448) memcpy(rankVal, rankValOrigin, sizeof(rankVal));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 449)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 450) /* fill DTable */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 451) for (s = 0; s < sortedListSize; s++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 452) const U16 symbol = sortedList[s].symbol;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 453) const U32 weight = sortedList[s].weight;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 454) const U32 nbBits = nbBitsBaseline - weight;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 455) const U32 start = rankVal[weight];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 456) const U32 length = 1 << (targetLog - nbBits);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 457)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 458) if (targetLog - nbBits >= minBits) { /* enough room for a second symbol */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 459) U32 sortedRank;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 460) int minWeight = nbBits + scaleLog;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 461) if (minWeight < 1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 462) minWeight = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 463) sortedRank = rankStart[minWeight];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 464) HUF_fillDTableX4Level2(DTable + start, targetLog - nbBits, nbBits, rankValOrigin[nbBits], minWeight, sortedList + sortedRank,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 465) sortedListSize - sortedRank, nbBitsBaseline, symbol);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 466) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 467) HUF_DEltX4 DElt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 468) ZSTD_writeLE16(&(DElt.sequence), symbol);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 469) DElt.nbBits = (BYTE)(nbBits);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 470) DElt.length = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 471) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 472) U32 const end = start + length;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 473) U32 u;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 474) for (u = start; u < end; u++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 475) DTable[u] = DElt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 476) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 477) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 478) rankVal[weight] += length;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 479) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 480) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 481)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 482) size_t HUF_readDTableX4_wksp(HUF_DTable *DTable, const void *src, size_t srcSize, void *workspace, size_t workspaceSize)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 483) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 484) U32 tableLog, maxW, sizeOfSort, nbSymbols;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 485) DTableDesc dtd = HUF_getDTableDesc(DTable);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 486) U32 const maxTableLog = dtd.maxTableLog;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 487) size_t iSize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 488) void *dtPtr = DTable + 1; /* force compiler to avoid strict-aliasing */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 489) HUF_DEltX4 *const dt = (HUF_DEltX4 *)dtPtr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 490) U32 *rankStart;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 491)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 492) rankValCol_t *rankVal;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 493) U32 *rankStats;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 494) U32 *rankStart0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 495) sortedSymbol_t *sortedSymbol;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 496) BYTE *weightList;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 497) size_t spaceUsed32 = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 498)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 499) HUF_STATIC_ASSERT((sizeof(rankValCol_t) & 3) == 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 500)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 501) rankVal = (rankValCol_t *)((U32 *)workspace + spaceUsed32);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 502) spaceUsed32 += (sizeof(rankValCol_t) * HUF_TABLELOG_MAX) >> 2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 503) rankStats = (U32 *)workspace + spaceUsed32;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 504) spaceUsed32 += HUF_TABLELOG_MAX + 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 505) rankStart0 = (U32 *)workspace + spaceUsed32;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 506) spaceUsed32 += HUF_TABLELOG_MAX + 2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 507) sortedSymbol = (sortedSymbol_t *)((U32 *)workspace + spaceUsed32);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 508) spaceUsed32 += ALIGN(sizeof(sortedSymbol_t) * (HUF_SYMBOLVALUE_MAX + 1), sizeof(U32)) >> 2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 509) weightList = (BYTE *)((U32 *)workspace + spaceUsed32);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 510) spaceUsed32 += ALIGN(HUF_SYMBOLVALUE_MAX + 1, sizeof(U32)) >> 2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 511)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 512) if ((spaceUsed32 << 2) > workspaceSize)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 513) return ERROR(tableLog_tooLarge);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 514) workspace = (U32 *)workspace + spaceUsed32;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 515) workspaceSize -= (spaceUsed32 << 2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 516)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 517) rankStart = rankStart0 + 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 518) memset(rankStats, 0, sizeof(U32) * (2 * HUF_TABLELOG_MAX + 2 + 1));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 519)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 520) HUF_STATIC_ASSERT(sizeof(HUF_DEltX4) == sizeof(HUF_DTable)); /* if compiler fails here, assertion is wrong */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 521) if (maxTableLog > HUF_TABLELOG_MAX)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 522) return ERROR(tableLog_tooLarge);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 523) /* memset(weightList, 0, sizeof(weightList)); */ /* is not necessary, even though some analyzer complain ... */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 524)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 525) iSize = HUF_readStats_wksp(weightList, HUF_SYMBOLVALUE_MAX + 1, rankStats, &nbSymbols, &tableLog, src, srcSize, workspace, workspaceSize);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 526) if (HUF_isError(iSize))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 527) return iSize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 528)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 529) /* check result */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 530) if (tableLog > maxTableLog)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 531) return ERROR(tableLog_tooLarge); /* DTable can't fit code depth */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 532)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 533) /* find maxWeight */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 534) for (maxW = tableLog; rankStats[maxW] == 0; maxW--) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 535) } /* necessarily finds a solution before 0 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 536)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 537) /* Get start index of each weight */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 538) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 539) U32 w, nextRankStart = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 540) for (w = 1; w < maxW + 1; w++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 541) U32 curr = nextRankStart;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 542) nextRankStart += rankStats[w];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 543) rankStart[w] = curr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 544) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 545) rankStart[0] = nextRankStart; /* put all 0w symbols at the end of sorted list*/
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 546) sizeOfSort = nextRankStart;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 547) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 548)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 549) /* sort symbols by weight */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 550) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 551) U32 s;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 552) for (s = 0; s < nbSymbols; s++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 553) U32 const w = weightList[s];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 554) U32 const r = rankStart[w]++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 555) sortedSymbol[r].symbol = (BYTE)s;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 556) sortedSymbol[r].weight = (BYTE)w;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 557) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 558) rankStart[0] = 0; /* forget 0w symbols; this is beginning of weight(1) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 559) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 560)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 561) /* Build rankVal */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 562) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 563) U32 *const rankVal0 = rankVal[0];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 564) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 565) int const rescale = (maxTableLog - tableLog) - 1; /* tableLog <= maxTableLog */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 566) U32 nextRankVal = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 567) U32 w;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 568) for (w = 1; w < maxW + 1; w++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 569) U32 curr = nextRankVal;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 570) nextRankVal += rankStats[w] << (w + rescale);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 571) rankVal0[w] = curr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 572) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 573) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 574) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 575) U32 const minBits = tableLog + 1 - maxW;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 576) U32 consumed;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 577) for (consumed = minBits; consumed < maxTableLog - minBits + 1; consumed++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 578) U32 *const rankValPtr = rankVal[consumed];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 579) U32 w;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 580) for (w = 1; w < maxW + 1; w++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 581) rankValPtr[w] = rankVal0[w] >> consumed;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 582) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 583) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 584) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 585) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 586)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 587) HUF_fillDTableX4(dt, maxTableLog, sortedSymbol, sizeOfSort, rankStart0, rankVal, maxW, tableLog + 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 588)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 589) dtd.tableLog = (BYTE)maxTableLog;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 590) dtd.tableType = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 591) memcpy(DTable, &dtd, sizeof(dtd));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 592) return iSize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 593) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 594)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 595) static U32 HUF_decodeSymbolX4(void *op, BIT_DStream_t *DStream, const HUF_DEltX4 *dt, const U32 dtLog)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 596) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 597) size_t const val = BIT_lookBitsFast(DStream, dtLog); /* note : dtLog >= 1 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 598) memcpy(op, dt + val, 2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 599) BIT_skipBits(DStream, dt[val].nbBits);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 600) return dt[val].length;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 601) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 602)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 603) static U32 HUF_decodeLastSymbolX4(void *op, BIT_DStream_t *DStream, const HUF_DEltX4 *dt, const U32 dtLog)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 604) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 605) size_t const val = BIT_lookBitsFast(DStream, dtLog); /* note : dtLog >= 1 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 606) memcpy(op, dt + val, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 607) if (dt[val].length == 1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 608) BIT_skipBits(DStream, dt[val].nbBits);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 609) else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 610) if (DStream->bitsConsumed < (sizeof(DStream->bitContainer) * 8)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 611) BIT_skipBits(DStream, dt[val].nbBits);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 612) if (DStream->bitsConsumed > (sizeof(DStream->bitContainer) * 8))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 613) /* ugly hack; works only because it's the last symbol. Note : can't easily extract nbBits from just this symbol */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 614) DStream->bitsConsumed = (sizeof(DStream->bitContainer) * 8);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 615) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 616) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 617) return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 618) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 619)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 620) #define HUF_DECODE_SYMBOLX4_0(ptr, DStreamPtr) ptr += HUF_decodeSymbolX4(ptr, DStreamPtr, dt, dtLog)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 621)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 622) #define HUF_DECODE_SYMBOLX4_1(ptr, DStreamPtr) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 623) if (ZSTD_64bits() || (HUF_TABLELOG_MAX <= 12)) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 624) ptr += HUF_decodeSymbolX4(ptr, DStreamPtr, dt, dtLog)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 625)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 626) #define HUF_DECODE_SYMBOLX4_2(ptr, DStreamPtr) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 627) if (ZSTD_64bits()) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 628) ptr += HUF_decodeSymbolX4(ptr, DStreamPtr, dt, dtLog)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 629)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 630) FORCE_INLINE size_t HUF_decodeStreamX4(BYTE *p, BIT_DStream_t *bitDPtr, BYTE *const pEnd, const HUF_DEltX4 *const dt, const U32 dtLog)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 631) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 632) BYTE *const pStart = p;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 633)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 634) /* up to 8 symbols at a time */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 635) while ((BIT_reloadDStream(bitDPtr) == BIT_DStream_unfinished) & (p < pEnd - (sizeof(bitDPtr->bitContainer) - 1))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 636) HUF_DECODE_SYMBOLX4_2(p, bitDPtr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 637) HUF_DECODE_SYMBOLX4_1(p, bitDPtr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 638) HUF_DECODE_SYMBOLX4_2(p, bitDPtr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 639) HUF_DECODE_SYMBOLX4_0(p, bitDPtr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 640) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 641)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 642) /* closer to end : up to 2 symbols at a time */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 643) while ((BIT_reloadDStream(bitDPtr) == BIT_DStream_unfinished) & (p <= pEnd - 2))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 644) HUF_DECODE_SYMBOLX4_0(p, bitDPtr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 645)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 646) while (p <= pEnd - 2)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 647) HUF_DECODE_SYMBOLX4_0(p, bitDPtr); /* no need to reload : reached the end of DStream */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 648)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 649) if (p < pEnd)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 650) p += HUF_decodeLastSymbolX4(p, bitDPtr, dt, dtLog);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 651)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 652) return p - pStart;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 653) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 654)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 655) static size_t HUF_decompress1X4_usingDTable_internal(void *dst, size_t dstSize, const void *cSrc, size_t cSrcSize, const HUF_DTable *DTable)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 656) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 657) BIT_DStream_t bitD;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 658)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 659) /* Init */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 660) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 661) size_t const errorCode = BIT_initDStream(&bitD, cSrc, cSrcSize);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 662) if (HUF_isError(errorCode))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 663) return errorCode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 664) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 665)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 666) /* decode */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 667) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 668) BYTE *const ostart = (BYTE *)dst;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 669) BYTE *const oend = ostart + dstSize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 670) const void *const dtPtr = DTable + 1; /* force compiler to not use strict-aliasing */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 671) const HUF_DEltX4 *const dt = (const HUF_DEltX4 *)dtPtr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 672) DTableDesc const dtd = HUF_getDTableDesc(DTable);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 673) HUF_decodeStreamX4(ostart, &bitD, oend, dt, dtd.tableLog);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 674) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 675)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 676) /* check */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 677) if (!BIT_endOfDStream(&bitD))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 678) return ERROR(corruption_detected);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 679)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 680) /* decoded size */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 681) return dstSize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 682) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 683)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 684) size_t HUF_decompress1X4_usingDTable(void *dst, size_t dstSize, const void *cSrc, size_t cSrcSize, const HUF_DTable *DTable)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 685) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 686) DTableDesc dtd = HUF_getDTableDesc(DTable);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 687) if (dtd.tableType != 1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 688) return ERROR(GENERIC);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 689) return HUF_decompress1X4_usingDTable_internal(dst, dstSize, cSrc, cSrcSize, DTable);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 690) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 691)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 692) size_t HUF_decompress1X4_DCtx_wksp(HUF_DTable *DCtx, void *dst, size_t dstSize, const void *cSrc, size_t cSrcSize, void *workspace, size_t workspaceSize)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 693) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 694) const BYTE *ip = (const BYTE *)cSrc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 695)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 696) size_t const hSize = HUF_readDTableX4_wksp(DCtx, cSrc, cSrcSize, workspace, workspaceSize);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 697) if (HUF_isError(hSize))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 698) return hSize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 699) if (hSize >= cSrcSize)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 700) return ERROR(srcSize_wrong);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 701) ip += hSize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 702) cSrcSize -= hSize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 703)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 704) return HUF_decompress1X4_usingDTable_internal(dst, dstSize, ip, cSrcSize, DCtx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 705) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 706)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 707) static size_t HUF_decompress4X4_usingDTable_internal(void *dst, size_t dstSize, const void *cSrc, size_t cSrcSize, const HUF_DTable *DTable)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 708) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 709) if (cSrcSize < 10)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 710) return ERROR(corruption_detected); /* strict minimum : jump table + 1 byte per stream */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 711)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 712) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 713) const BYTE *const istart = (const BYTE *)cSrc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 714) BYTE *const ostart = (BYTE *)dst;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 715) BYTE *const oend = ostart + dstSize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 716) const void *const dtPtr = DTable + 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 717) const HUF_DEltX4 *const dt = (const HUF_DEltX4 *)dtPtr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 718)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 719) /* Init */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 720) BIT_DStream_t bitD1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 721) BIT_DStream_t bitD2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 722) BIT_DStream_t bitD3;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 723) BIT_DStream_t bitD4;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 724) size_t const length1 = ZSTD_readLE16(istart);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 725) size_t const length2 = ZSTD_readLE16(istart + 2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 726) size_t const length3 = ZSTD_readLE16(istart + 4);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 727) size_t const length4 = cSrcSize - (length1 + length2 + length3 + 6);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 728) const BYTE *const istart1 = istart + 6; /* jumpTable */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 729) const BYTE *const istart2 = istart1 + length1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 730) const BYTE *const istart3 = istart2 + length2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 731) const BYTE *const istart4 = istart3 + length3;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 732) size_t const segmentSize = (dstSize + 3) / 4;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 733) BYTE *const opStart2 = ostart + segmentSize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 734) BYTE *const opStart3 = opStart2 + segmentSize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 735) BYTE *const opStart4 = opStart3 + segmentSize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 736) BYTE *op1 = ostart;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 737) BYTE *op2 = opStart2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 738) BYTE *op3 = opStart3;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 739) BYTE *op4 = opStart4;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 740) U32 endSignal;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 741) DTableDesc const dtd = HUF_getDTableDesc(DTable);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 742) U32 const dtLog = dtd.tableLog;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 743)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 744) if (length4 > cSrcSize)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 745) return ERROR(corruption_detected); /* overflow */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 746) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 747) size_t const errorCode = BIT_initDStream(&bitD1, istart1, length1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 748) if (HUF_isError(errorCode))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 749) return errorCode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 750) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 751) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 752) size_t const errorCode = BIT_initDStream(&bitD2, istart2, length2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 753) if (HUF_isError(errorCode))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 754) return errorCode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 755) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 756) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 757) size_t const errorCode = BIT_initDStream(&bitD3, istart3, length3);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 758) if (HUF_isError(errorCode))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 759) return errorCode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 760) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 761) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 762) size_t const errorCode = BIT_initDStream(&bitD4, istart4, length4);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 763) if (HUF_isError(errorCode))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 764) return errorCode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 765) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 766)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 767) /* 16-32 symbols per loop (4-8 symbols per stream) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 768) endSignal = BIT_reloadDStream(&bitD1) | BIT_reloadDStream(&bitD2) | BIT_reloadDStream(&bitD3) | BIT_reloadDStream(&bitD4);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 769) for (; (endSignal == BIT_DStream_unfinished) & (op4 < (oend - (sizeof(bitD4.bitContainer) - 1)));) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 770) HUF_DECODE_SYMBOLX4_2(op1, &bitD1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 771) HUF_DECODE_SYMBOLX4_2(op2, &bitD2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 772) HUF_DECODE_SYMBOLX4_2(op3, &bitD3);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 773) HUF_DECODE_SYMBOLX4_2(op4, &bitD4);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 774) HUF_DECODE_SYMBOLX4_1(op1, &bitD1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 775) HUF_DECODE_SYMBOLX4_1(op2, &bitD2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 776) HUF_DECODE_SYMBOLX4_1(op3, &bitD3);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 777) HUF_DECODE_SYMBOLX4_1(op4, &bitD4);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 778) HUF_DECODE_SYMBOLX4_2(op1, &bitD1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 779) HUF_DECODE_SYMBOLX4_2(op2, &bitD2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 780) HUF_DECODE_SYMBOLX4_2(op3, &bitD3);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 781) HUF_DECODE_SYMBOLX4_2(op4, &bitD4);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 782) HUF_DECODE_SYMBOLX4_0(op1, &bitD1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 783) HUF_DECODE_SYMBOLX4_0(op2, &bitD2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 784) HUF_DECODE_SYMBOLX4_0(op3, &bitD3);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 785) HUF_DECODE_SYMBOLX4_0(op4, &bitD4);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 786)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 787) endSignal = BIT_reloadDStream(&bitD1) | BIT_reloadDStream(&bitD2) | BIT_reloadDStream(&bitD3) | BIT_reloadDStream(&bitD4);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 788) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 789)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 790) /* check corruption */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 791) if (op1 > opStart2)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 792) return ERROR(corruption_detected);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 793) if (op2 > opStart3)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 794) return ERROR(corruption_detected);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 795) if (op3 > opStart4)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 796) return ERROR(corruption_detected);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 797) /* note : op4 already verified within main loop */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 798)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 799) /* finish bitStreams one by one */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 800) HUF_decodeStreamX4(op1, &bitD1, opStart2, dt, dtLog);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 801) HUF_decodeStreamX4(op2, &bitD2, opStart3, dt, dtLog);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 802) HUF_decodeStreamX4(op3, &bitD3, opStart4, dt, dtLog);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 803) HUF_decodeStreamX4(op4, &bitD4, oend, dt, dtLog);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 804)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 805) /* check */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 806) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 807) U32 const endCheck = BIT_endOfDStream(&bitD1) & BIT_endOfDStream(&bitD2) & BIT_endOfDStream(&bitD3) & BIT_endOfDStream(&bitD4);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 808) if (!endCheck)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 809) return ERROR(corruption_detected);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 810) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 811)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 812) /* decoded size */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 813) return dstSize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 814) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 815) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 816)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 817) size_t HUF_decompress4X4_usingDTable(void *dst, size_t dstSize, const void *cSrc, size_t cSrcSize, const HUF_DTable *DTable)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 818) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 819) DTableDesc dtd = HUF_getDTableDesc(DTable);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 820) if (dtd.tableType != 1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 821) return ERROR(GENERIC);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 822) return HUF_decompress4X4_usingDTable_internal(dst, dstSize, cSrc, cSrcSize, DTable);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 823) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 824)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 825) size_t HUF_decompress4X4_DCtx_wksp(HUF_DTable *dctx, void *dst, size_t dstSize, const void *cSrc, size_t cSrcSize, void *workspace, size_t workspaceSize)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 826) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 827) const BYTE *ip = (const BYTE *)cSrc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 828)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 829) size_t hSize = HUF_readDTableX4_wksp(dctx, cSrc, cSrcSize, workspace, workspaceSize);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 830) if (HUF_isError(hSize))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 831) return hSize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 832) if (hSize >= cSrcSize)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 833) return ERROR(srcSize_wrong);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 834) ip += hSize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 835) cSrcSize -= hSize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 836)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 837) return HUF_decompress4X4_usingDTable_internal(dst, dstSize, ip, cSrcSize, dctx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 838) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 839)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 840) /* ********************************/
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 841) /* Generic decompression selector */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 842) /* ********************************/
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 843)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 844) size_t HUF_decompress1X_usingDTable(void *dst, size_t maxDstSize, const void *cSrc, size_t cSrcSize, const HUF_DTable *DTable)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 845) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 846) DTableDesc const dtd = HUF_getDTableDesc(DTable);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 847) return dtd.tableType ? HUF_decompress1X4_usingDTable_internal(dst, maxDstSize, cSrc, cSrcSize, DTable)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 848) : HUF_decompress1X2_usingDTable_internal(dst, maxDstSize, cSrc, cSrcSize, DTable);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 849) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 850)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 851) size_t HUF_decompress4X_usingDTable(void *dst, size_t maxDstSize, const void *cSrc, size_t cSrcSize, const HUF_DTable *DTable)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 852) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 853) DTableDesc const dtd = HUF_getDTableDesc(DTable);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 854) return dtd.tableType ? HUF_decompress4X4_usingDTable_internal(dst, maxDstSize, cSrc, cSrcSize, DTable)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 855) : HUF_decompress4X2_usingDTable_internal(dst, maxDstSize, cSrc, cSrcSize, DTable);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 856) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 857)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 858) typedef struct {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 859) U32 tableTime;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 860) U32 decode256Time;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 861) } algo_time_t;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 862) static const algo_time_t algoTime[16 /* Quantization */][3 /* single, double, quad */] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 863) /* single, double, quad */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 864) {{0, 0}, {1, 1}, {2, 2}}, /* Q==0 : impossible */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 865) {{0, 0}, {1, 1}, {2, 2}}, /* Q==1 : impossible */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 866) {{38, 130}, {1313, 74}, {2151, 38}}, /* Q == 2 : 12-18% */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 867) {{448, 128}, {1353, 74}, {2238, 41}}, /* Q == 3 : 18-25% */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 868) {{556, 128}, {1353, 74}, {2238, 47}}, /* Q == 4 : 25-32% */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 869) {{714, 128}, {1418, 74}, {2436, 53}}, /* Q == 5 : 32-38% */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 870) {{883, 128}, {1437, 74}, {2464, 61}}, /* Q == 6 : 38-44% */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 871) {{897, 128}, {1515, 75}, {2622, 68}}, /* Q == 7 : 44-50% */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 872) {{926, 128}, {1613, 75}, {2730, 75}}, /* Q == 8 : 50-56% */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 873) {{947, 128}, {1729, 77}, {3359, 77}}, /* Q == 9 : 56-62% */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 874) {{1107, 128}, {2083, 81}, {4006, 84}}, /* Q ==10 : 62-69% */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 875) {{1177, 128}, {2379, 87}, {4785, 88}}, /* Q ==11 : 69-75% */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 876) {{1242, 128}, {2415, 93}, {5155, 84}}, /* Q ==12 : 75-81% */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 877) {{1349, 128}, {2644, 106}, {5260, 106}}, /* Q ==13 : 81-87% */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 878) {{1455, 128}, {2422, 124}, {4174, 124}}, /* Q ==14 : 87-93% */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 879) {{722, 128}, {1891, 145}, {1936, 146}}, /* Q ==15 : 93-99% */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 880) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 881)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 882) /** HUF_selectDecoder() :
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 883) * Tells which decoder is likely to decode faster,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 884) * based on a set of pre-determined metrics.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 885) * @return : 0==HUF_decompress4X2, 1==HUF_decompress4X4 .
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 886) * Assumption : 0 < cSrcSize < dstSize <= 128 KB */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 887) U32 HUF_selectDecoder(size_t dstSize, size_t cSrcSize)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 888) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 889) /* decoder timing evaluation */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 890) U32 const Q = (U32)(cSrcSize * 16 / dstSize); /* Q < 16 since dstSize > cSrcSize */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 891) U32 const D256 = (U32)(dstSize >> 8);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 892) U32 const DTime0 = algoTime[Q][0].tableTime + (algoTime[Q][0].decode256Time * D256);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 893) U32 DTime1 = algoTime[Q][1].tableTime + (algoTime[Q][1].decode256Time * D256);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 894) DTime1 += DTime1 >> 3; /* advantage to algorithm using less memory, for cache eviction */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 895)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 896) return DTime1 < DTime0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 897) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 898)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 899) typedef size_t (*decompressionAlgo)(void *dst, size_t dstSize, const void *cSrc, size_t cSrcSize);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 900)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 901) size_t HUF_decompress4X_DCtx_wksp(HUF_DTable *dctx, void *dst, size_t dstSize, const void *cSrc, size_t cSrcSize, void *workspace, size_t workspaceSize)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 902) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 903) /* validation checks */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 904) if (dstSize == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 905) return ERROR(dstSize_tooSmall);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 906) if (cSrcSize > dstSize)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 907) return ERROR(corruption_detected); /* invalid */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 908) if (cSrcSize == dstSize) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 909) memcpy(dst, cSrc, dstSize);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 910) return dstSize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 911) } /* not compressed */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 912) if (cSrcSize == 1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 913) memset(dst, *(const BYTE *)cSrc, dstSize);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 914) return dstSize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 915) } /* RLE */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 916)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 917) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 918) U32 const algoNb = HUF_selectDecoder(dstSize, cSrcSize);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 919) return algoNb ? HUF_decompress4X4_DCtx_wksp(dctx, dst, dstSize, cSrc, cSrcSize, workspace, workspaceSize)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 920) : HUF_decompress4X2_DCtx_wksp(dctx, dst, dstSize, cSrc, cSrcSize, workspace, workspaceSize);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 921) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 922) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 923)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 924) size_t HUF_decompress4X_hufOnly_wksp(HUF_DTable *dctx, void *dst, size_t dstSize, const void *cSrc, size_t cSrcSize, void *workspace, size_t workspaceSize)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 925) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 926) /* validation checks */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 927) if (dstSize == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 928) return ERROR(dstSize_tooSmall);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 929) if ((cSrcSize >= dstSize) || (cSrcSize <= 1))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 930) return ERROR(corruption_detected); /* invalid */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 931)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 932) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 933) U32 const algoNb = HUF_selectDecoder(dstSize, cSrcSize);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 934) return algoNb ? HUF_decompress4X4_DCtx_wksp(dctx, dst, dstSize, cSrc, cSrcSize, workspace, workspaceSize)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 935) : HUF_decompress4X2_DCtx_wksp(dctx, dst, dstSize, cSrc, cSrcSize, workspace, workspaceSize);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 936) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 937) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 938)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 939) size_t HUF_decompress1X_DCtx_wksp(HUF_DTable *dctx, void *dst, size_t dstSize, const void *cSrc, size_t cSrcSize, void *workspace, size_t workspaceSize)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 940) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 941) /* validation checks */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 942) if (dstSize == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 943) return ERROR(dstSize_tooSmall);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 944) if (cSrcSize > dstSize)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 945) return ERROR(corruption_detected); /* invalid */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 946) if (cSrcSize == dstSize) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 947) memcpy(dst, cSrc, dstSize);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 948) return dstSize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 949) } /* not compressed */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 950) if (cSrcSize == 1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 951) memset(dst, *(const BYTE *)cSrc, dstSize);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 952) return dstSize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 953) } /* RLE */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 954)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 955) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 956) U32 const algoNb = HUF_selectDecoder(dstSize, cSrcSize);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 957) return algoNb ? HUF_decompress1X4_DCtx_wksp(dctx, dst, dstSize, cSrc, cSrcSize, workspace, workspaceSize)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 958) : HUF_decompress1X2_DCtx_wksp(dctx, dst, dstSize, cSrc, cSrcSize, workspace, workspaceSize);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 959) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 960) }