^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) * FSE : Finite State Entropy decoder
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) * Copyright (C) 2013-2015, 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) * Includes
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) ****************************************************************/
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) #include "bitstream.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) #include "fse.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) #include "zstd_internal.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 FSE_isError ERR_isError
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) #define FSE_STATIC_ASSERT(c) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) { \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) enum { FSE_static_assert = 1 / (int)(!!(c)) }; \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) } /* use only *after* variable declarations */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) /* **************************************************************
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) * Templates
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) ****************************************************************/
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) designed to be included
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) for type-specific functions (template emulation in C)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) Objective is to write these functions only once, for improved maintenance
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) /* safety checks */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) #ifndef FSE_FUNCTION_EXTENSION
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) #error "FSE_FUNCTION_EXTENSION must be defined"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) #ifndef FSE_FUNCTION_TYPE
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) #error "FSE_FUNCTION_TYPE must be defined"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) /* Function names */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) #define FSE_CAT(X, Y) X##Y
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) #define FSE_FUNCTION_NAME(X, Y) FSE_CAT(X, Y)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) #define FSE_TYPE_NAME(X, Y) FSE_CAT(X, Y)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) /* Function templates */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) size_t FSE_buildDTable_wksp(FSE_DTable *dt, const short *normalizedCounter, unsigned maxSymbolValue, unsigned tableLog, void *workspace, size_t workspaceSize)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) void *const tdPtr = dt + 1; /* because *dt is unsigned, 32-bits aligned on 32-bits */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) FSE_DECODE_TYPE *const tableDecode = (FSE_DECODE_TYPE *)(tdPtr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) U16 *symbolNext = (U16 *)workspace;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) U32 const maxSV1 = maxSymbolValue + 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) U32 const tableSize = 1 << tableLog;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) U32 highThreshold = tableSize - 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) /* Sanity Checks */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) if (workspaceSize < sizeof(U16) * (FSE_MAX_SYMBOL_VALUE + 1))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) return ERROR(tableLog_tooLarge);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) if (maxSymbolValue > FSE_MAX_SYMBOL_VALUE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) return ERROR(maxSymbolValue_tooLarge);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) if (tableLog > FSE_MAX_TABLELOG)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) return ERROR(tableLog_tooLarge);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) /* Init, lay down lowprob symbols */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) FSE_DTableHeader DTableH;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) DTableH.tableLog = (U16)tableLog;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) DTableH.fastMode = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) S16 const largeLimit = (S16)(1 << (tableLog - 1));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) U32 s;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) for (s = 0; s < maxSV1; s++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) if (normalizedCounter[s] == -1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) tableDecode[highThreshold--].symbol = (FSE_FUNCTION_TYPE)s;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) symbolNext[s] = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) if (normalizedCounter[s] >= largeLimit)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) DTableH.fastMode = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) symbolNext[s] = normalizedCounter[s];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) memcpy(dt, &DTableH, sizeof(DTableH));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) /* Spread symbols */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) U32 const tableMask = tableSize - 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) U32 const step = FSE_TABLESTEP(tableSize);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) U32 s, position = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) for (s = 0; s < maxSV1; s++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) for (i = 0; i < normalizedCounter[s]; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) tableDecode[position].symbol = (FSE_FUNCTION_TYPE)s;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) position = (position + step) & tableMask;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) while (position > highThreshold)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) position = (position + step) & tableMask; /* lowprob area */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) if (position != 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) return ERROR(GENERIC); /* position must reach all cells once, otherwise normalizedCounter is incorrect */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) /* Build Decoding table */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) U32 u;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) for (u = 0; u < tableSize; u++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) FSE_FUNCTION_TYPE const symbol = (FSE_FUNCTION_TYPE)(tableDecode[u].symbol);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) U16 nextState = symbolNext[symbol]++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) tableDecode[u].nbBits = (BYTE)(tableLog - BIT_highbit32((U32)nextState));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) tableDecode[u].newState = (U16)((nextState << tableDecode[u].nbBits) - tableSize);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) /*-*******************************************************
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) * Decompression (Byte symbols)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) *********************************************************/
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) size_t FSE_buildDTable_rle(FSE_DTable *dt, BYTE symbolValue)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) void *ptr = dt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) FSE_DTableHeader *const DTableH = (FSE_DTableHeader *)ptr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) void *dPtr = dt + 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) FSE_decode_t *const cell = (FSE_decode_t *)dPtr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) DTableH->tableLog = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) DTableH->fastMode = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) cell->newState = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) cell->symbol = symbolValue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) cell->nbBits = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) size_t FSE_buildDTable_raw(FSE_DTable *dt, unsigned nbBits)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) void *ptr = dt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) FSE_DTableHeader *const DTableH = (FSE_DTableHeader *)ptr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) void *dPtr = dt + 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) FSE_decode_t *const dinfo = (FSE_decode_t *)dPtr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) const unsigned tableSize = 1 << nbBits;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) const unsigned tableMask = tableSize - 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) const unsigned maxSV1 = tableMask + 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) unsigned s;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) /* Sanity checks */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) if (nbBits < 1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) return ERROR(GENERIC); /* min size */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) /* Build Decoding Table */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) DTableH->tableLog = (U16)nbBits;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) DTableH->fastMode = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) for (s = 0; s < maxSV1; s++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) dinfo[s].newState = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) dinfo[s].symbol = (BYTE)s;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) dinfo[s].nbBits = (BYTE)nbBits;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) FORCE_INLINE size_t FSE_decompress_usingDTable_generic(void *dst, size_t maxDstSize, const void *cSrc, size_t cSrcSize, const FSE_DTable *dt,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) const unsigned fast)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) BYTE *const ostart = (BYTE *)dst;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) BYTE *op = ostart;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) BYTE *const omax = op + maxDstSize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) BYTE *const olimit = omax - 3;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) BIT_DStream_t bitD;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) FSE_DState_t state1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) FSE_DState_t state2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) /* Init */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) CHECK_F(BIT_initDStream(&bitD, cSrc, cSrcSize));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) FSE_initDState(&state1, &bitD, dt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) FSE_initDState(&state2, &bitD, dt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) #define FSE_GETSYMBOL(statePtr) fast ? FSE_decodeSymbolFast(statePtr, &bitD) : FSE_decodeSymbol(statePtr, &bitD)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) /* 4 symbols per loop */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) for (; (BIT_reloadDStream(&bitD) == BIT_DStream_unfinished) & (op < olimit); op += 4) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) op[0] = FSE_GETSYMBOL(&state1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) if (FSE_MAX_TABLELOG * 2 + 7 > sizeof(bitD.bitContainer) * 8) /* This test must be static */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) BIT_reloadDStream(&bitD);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) op[1] = FSE_GETSYMBOL(&state2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) if (FSE_MAX_TABLELOG * 4 + 7 > sizeof(bitD.bitContainer) * 8) /* This test must be static */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) if (BIT_reloadDStream(&bitD) > BIT_DStream_unfinished) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) op += 2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) op[2] = FSE_GETSYMBOL(&state1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) if (FSE_MAX_TABLELOG * 2 + 7 > sizeof(bitD.bitContainer) * 8) /* This test must be static */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) BIT_reloadDStream(&bitD);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) op[3] = FSE_GETSYMBOL(&state2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) /* tail */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) /* note : BIT_reloadDStream(&bitD) >= FSE_DStream_partiallyFilled; Ends at exactly BIT_DStream_completed */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) while (1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) if (op > (omax - 2))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) return ERROR(dstSize_tooSmall);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) *op++ = FSE_GETSYMBOL(&state1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) if (BIT_reloadDStream(&bitD) == BIT_DStream_overflow) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) *op++ = FSE_GETSYMBOL(&state2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) if (op > (omax - 2))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) return ERROR(dstSize_tooSmall);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) *op++ = FSE_GETSYMBOL(&state2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) if (BIT_reloadDStream(&bitD) == BIT_DStream_overflow) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) *op++ = FSE_GETSYMBOL(&state1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) return op - ostart;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) size_t FSE_decompress_usingDTable(void *dst, size_t originalSize, const void *cSrc, size_t cSrcSize, const FSE_DTable *dt)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) const void *ptr = dt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) const FSE_DTableHeader *DTableH = (const FSE_DTableHeader *)ptr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) const U32 fastMode = DTableH->fastMode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) /* select fast mode (static) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) if (fastMode)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) return FSE_decompress_usingDTable_generic(dst, originalSize, cSrc, cSrcSize, dt, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) return FSE_decompress_usingDTable_generic(dst, originalSize, cSrc, cSrcSize, dt, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) size_t FSE_decompress_wksp(void *dst, size_t dstCapacity, const void *cSrc, size_t cSrcSize, unsigned maxLog, void *workspace, size_t workspaceSize)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) const BYTE *const istart = (const BYTE *)cSrc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) const BYTE *ip = istart;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) unsigned tableLog;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) unsigned maxSymbolValue = FSE_MAX_SYMBOL_VALUE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) size_t NCountLength;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) FSE_DTable *dt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) short *counting;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) size_t spaceUsed32 = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) FSE_STATIC_ASSERT(sizeof(FSE_DTable) == sizeof(U32));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) dt = (FSE_DTable *)((U32 *)workspace + spaceUsed32);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) spaceUsed32 += FSE_DTABLE_SIZE_U32(maxLog);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) counting = (short *)((U32 *)workspace + spaceUsed32);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) spaceUsed32 += ALIGN(sizeof(short) * (FSE_MAX_SYMBOL_VALUE + 1), sizeof(U32)) >> 2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) if ((spaceUsed32 << 2) > workspaceSize)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) return ERROR(tableLog_tooLarge);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) workspace = (U32 *)workspace + spaceUsed32;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) workspaceSize -= (spaceUsed32 << 2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) /* normal FSE decoding mode */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) NCountLength = FSE_readNCount(counting, &maxSymbolValue, &tableLog, istart, cSrcSize);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) if (FSE_isError(NCountLength))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) return NCountLength;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) // if (NCountLength >= cSrcSize) return ERROR(srcSize_wrong); /* too small input size; supposed to be already checked in NCountLength, only remaining
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) // case : NCountLength==cSrcSize */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) if (tableLog > maxLog)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) return ERROR(tableLog_tooLarge);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) ip += NCountLength;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) cSrcSize -= NCountLength;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) CHECK_F(FSE_buildDTable_wksp(dt, counting, maxSymbolValue, tableLog, workspace, workspaceSize));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) return FSE_decompress_usingDTable(dst, dstCapacity, ip, cSrcSize, dt); /* always return, even if it is an error code */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) }