Path: blob/main/sys/contrib/openzfs/module/zstd/lib/common/fse_decompress.c
48774 views
// SPDX-License-Identifier: BSD-3-Clause OR GPL-2.0-only1/* ******************************************************************2* FSE : Finite State Entropy decoder3* Copyright (c) 2013-2020, Yann Collet, Facebook, Inc.4*5* You can contact the author at :6* - FSE source repository : https://github.com/Cyan4973/FiniteStateEntropy7* - Public forum : https://groups.google.com/forum/#!forum/lz4c8*9* This source code is licensed under both the BSD-style license (found in the10* LICENSE file in the root directory of this source tree) and the GPLv2 (found11* in the COPYING file in the root directory of this source tree).12* You may select, at your option, one of the above-listed licenses.13****************************************************************** */141516/* **************************************************************17* Includes18****************************************************************/19#include <stdlib.h> /* malloc, free, qsort */20#include <string.h> /* memcpy, memset */21#include "bitstream.h"22#include "compiler.h"23#define FSE_STATIC_LINKING_ONLY24#include "fse.h"25#include "error_private.h"262728/* **************************************************************29* Error Management30****************************************************************/31#define FSE_isError ERR_isError32#define FSE_STATIC_ASSERT(c) DEBUG_STATIC_ASSERT(c) /* use only *after* variable declarations */333435/* **************************************************************36* Templates37****************************************************************/38/*39designed to be included40for type-specific functions (template emulation in C)41Objective is to write these functions only once, for improved maintenance42*/4344/* safety checks */45#ifndef FSE_FUNCTION_EXTENSION46# error "FSE_FUNCTION_EXTENSION must be defined"47#endif48#ifndef FSE_FUNCTION_TYPE49# error "FSE_FUNCTION_TYPE must be defined"50#endif5152/* Function names */53#define FSE_CAT(X,Y) X##Y54#define FSE_FUNCTION_NAME(X,Y) FSE_CAT(X,Y)55#define FSE_TYPE_NAME(X,Y) FSE_CAT(X,Y)565758/* Function templates */59size_t FSE_buildDTable(FSE_DTable* dt, const short* normalizedCounter, unsigned maxSymbolValue, unsigned tableLog)60{61void* const tdPtr = dt+1; /* because *dt is unsigned, 32-bits aligned on 32-bits */62FSE_DECODE_TYPE* const tableDecode = (FSE_DECODE_TYPE*) (tdPtr);63U16 symbolNext[FSE_MAX_SYMBOL_VALUE+1];6465U32 const maxSV1 = maxSymbolValue + 1;66U32 const tableSize = 1 << tableLog;67U32 highThreshold = tableSize-1;6869/* Sanity Checks */70if (maxSymbolValue > FSE_MAX_SYMBOL_VALUE) return ERROR(maxSymbolValue_tooLarge);71if (tableLog > FSE_MAX_TABLELOG) return ERROR(tableLog_tooLarge);7273/* Init, lay down lowprob symbols */74{ FSE_DTableHeader DTableH;75DTableH.tableLog = (U16)tableLog;76DTableH.fastMode = 1;77{ S16 const largeLimit= (S16)(1 << (tableLog-1));78U32 s;79for (s=0; s<maxSV1; s++) {80if (normalizedCounter[s]==-1) {81tableDecode[highThreshold--].symbol = (FSE_FUNCTION_TYPE)s;82symbolNext[s] = 1;83} else {84if (normalizedCounter[s] >= largeLimit) DTableH.fastMode=0;85symbolNext[s] = normalizedCounter[s];86} } }87memcpy(dt, &DTableH, sizeof(DTableH));88}8990/* Spread symbols */91{ U32 const tableMask = tableSize-1;92U32 const step = FSE_TABLESTEP(tableSize);93U32 s, position = 0;94for (s=0; s<maxSV1; s++) {95int i;96for (i=0; i<normalizedCounter[s]; i++) {97tableDecode[position].symbol = (FSE_FUNCTION_TYPE)s;98position = (position + step) & tableMask;99while (position > highThreshold) position = (position + step) & tableMask; /* lowprob area */100} }101if (position!=0) return ERROR(GENERIC); /* position must reach all cells once, otherwise normalizedCounter is incorrect */102}103104/* Build Decoding table */105{ U32 u;106for (u=0; u<tableSize; u++) {107FSE_FUNCTION_TYPE const symbol = (FSE_FUNCTION_TYPE)(tableDecode[u].symbol);108U32 const nextState = symbolNext[symbol]++;109tableDecode[u].nbBits = (BYTE) (tableLog - BIT_highbit32(nextState) );110tableDecode[u].newState = (U16) ( (nextState << tableDecode[u].nbBits) - tableSize);111} }112113return 0;114}115116117#ifndef FSE_COMMONDEFS_ONLY118119/*-*******************************************************120* Decompression (Byte symbols)121*********************************************************/122size_t FSE_buildDTable_rle (FSE_DTable* dt, BYTE symbolValue)123{124void* ptr = dt;125FSE_DTableHeader* const DTableH = (FSE_DTableHeader*)ptr;126void* dPtr = dt + 1;127FSE_decode_t* const cell = (FSE_decode_t*)dPtr;128129DTableH->tableLog = 0;130DTableH->fastMode = 0;131132cell->newState = 0;133cell->symbol = symbolValue;134cell->nbBits = 0;135136return 0;137}138139140size_t FSE_buildDTable_raw (FSE_DTable* dt, unsigned nbBits)141{142void* ptr = dt;143FSE_DTableHeader* const DTableH = (FSE_DTableHeader*)ptr;144void* dPtr = dt + 1;145FSE_decode_t* const dinfo = (FSE_decode_t*)dPtr;146const unsigned tableSize = 1 << nbBits;147const unsigned tableMask = tableSize - 1;148const unsigned maxSV1 = tableMask+1;149unsigned s;150151/* Sanity checks */152if (nbBits < 1) return ERROR(GENERIC); /* min size */153154/* Build Decoding Table */155DTableH->tableLog = (U16)nbBits;156DTableH->fastMode = 1;157for (s=0; s<maxSV1; s++) {158dinfo[s].newState = 0;159dinfo[s].symbol = (BYTE)s;160dinfo[s].nbBits = (BYTE)nbBits;161}162163return 0;164}165166FORCE_INLINE_TEMPLATE size_t FSE_decompress_usingDTable_generic(167void* dst, size_t maxDstSize,168const void* cSrc, size_t cSrcSize,169const FSE_DTable* dt, const unsigned fast)170{171BYTE* const ostart = (BYTE*) dst;172BYTE* op = ostart;173BYTE* const omax = op + maxDstSize;174BYTE* const olimit = omax-3;175176BIT_DStream_t bitD;177FSE_DState_t state1;178FSE_DState_t state2;179180/* Init */181CHECK_F(BIT_initDStream(&bitD, cSrc, cSrcSize));182183FSE_initDState(&state1, &bitD, dt);184FSE_initDState(&state2, &bitD, dt);185186#define FSE_GETSYMBOL(statePtr) fast ? FSE_decodeSymbolFast(statePtr, &bitD) : FSE_decodeSymbol(statePtr, &bitD)187188/* 4 symbols per loop */189for ( ; (BIT_reloadDStream(&bitD)==BIT_DStream_unfinished) & (op<olimit) ; op+=4) {190op[0] = FSE_GETSYMBOL(&state1);191192if (FSE_MAX_TABLELOG*2+7 > sizeof(bitD.bitContainer)*8) /* This test must be static */193BIT_reloadDStream(&bitD);194195op[1] = FSE_GETSYMBOL(&state2);196197if (FSE_MAX_TABLELOG*4+7 > sizeof(bitD.bitContainer)*8) /* This test must be static */198{ if (BIT_reloadDStream(&bitD) > BIT_DStream_unfinished) { op+=2; break; } }199200op[2] = FSE_GETSYMBOL(&state1);201202if (FSE_MAX_TABLELOG*2+7 > sizeof(bitD.bitContainer)*8) /* This test must be static */203BIT_reloadDStream(&bitD);204205op[3] = FSE_GETSYMBOL(&state2);206}207208/* tail */209/* note : BIT_reloadDStream(&bitD) >= FSE_DStream_partiallyFilled; Ends at exactly BIT_DStream_completed */210while (1) {211if (op>(omax-2)) return ERROR(dstSize_tooSmall);212*op++ = FSE_GETSYMBOL(&state1);213if (BIT_reloadDStream(&bitD)==BIT_DStream_overflow) {214*op++ = FSE_GETSYMBOL(&state2);215break;216}217218if (op>(omax-2)) return ERROR(dstSize_tooSmall);219*op++ = FSE_GETSYMBOL(&state2);220if (BIT_reloadDStream(&bitD)==BIT_DStream_overflow) {221*op++ = FSE_GETSYMBOL(&state1);222break;223} }224225return op-ostart;226}227228229size_t FSE_decompress_usingDTable(void* dst, size_t originalSize,230const void* cSrc, size_t cSrcSize,231const FSE_DTable* dt)232{233const void* ptr = dt;234const FSE_DTableHeader* DTableH = (const FSE_DTableHeader*)ptr;235const U32 fastMode = DTableH->fastMode;236237/* select fast mode (static) */238if (fastMode) return FSE_decompress_usingDTable_generic(dst, originalSize, cSrc, cSrcSize, dt, 1);239return FSE_decompress_usingDTable_generic(dst, originalSize, cSrc, cSrcSize, dt, 0);240}241242243size_t FSE_decompress_wksp(void* dst, size_t dstCapacity, const void* cSrc, size_t cSrcSize, FSE_DTable* workSpace, unsigned maxLog)244{245const BYTE* const istart = (const BYTE*)cSrc;246const BYTE* ip = istart;247short counting[FSE_MAX_SYMBOL_VALUE+1];248unsigned tableLog;249unsigned maxSymbolValue = FSE_MAX_SYMBOL_VALUE;250251/* normal FSE decoding mode */252size_t const NCountLength = FSE_readNCount (counting, &maxSymbolValue, &tableLog, istart, cSrcSize);253if (FSE_isError(NCountLength)) return NCountLength;254/* if (NCountLength >= cSrcSize) return ERROR(srcSize_wrong); */ /* too small input size; supposed to be already checked in NCountLength, only remaining case : NCountLength==cSrcSize */255if (tableLog > maxLog) return ERROR(tableLog_tooLarge);256ip += NCountLength;257cSrcSize -= NCountLength;258259CHECK_F( FSE_buildDTable (workSpace, counting, maxSymbolValue, tableLog) );260261return FSE_decompress_usingDTable (dst, dstCapacity, ip, cSrcSize, workSpace); /* always return, even if it is an error code */262}263264265typedef FSE_DTable DTable_max_t[FSE_DTABLE_SIZE_U32(FSE_MAX_TABLELOG)];266267size_t FSE_decompress(void* dst, size_t dstCapacity, const void* cSrc, size_t cSrcSize)268{269DTable_max_t dt; /* Static analyzer seems unable to understand this table will be properly initialized later */270return FSE_decompress_wksp(dst, dstCapacity, cSrc, cSrcSize, dt, FSE_MAX_TABLELOG);271}272273274275#endif /* FSE_COMMONDEFS_ONLY */276277278