Path: blob/main/sys/contrib/zstd/lib/decompress/zstd_decompress_internal.h
48375 views
/*1* Copyright (c) Yann Collet, Facebook, Inc.2* All rights reserved.3*4* This source code is licensed under both the BSD-style license (found in the5* LICENSE file in the root directory of this source tree) and the GPLv2 (found6* in the COPYING file in the root directory of this source tree).7* You may select, at your option, one of the above-listed licenses.8*/91011/* zstd_decompress_internal:12* objects and definitions shared within lib/decompress modules */1314#ifndef ZSTD_DECOMPRESS_INTERNAL_H15#define ZSTD_DECOMPRESS_INTERNAL_H161718/*-*******************************************************19* Dependencies20*********************************************************/21#include "../common/mem.h" /* BYTE, U16, U32 */22#include "../common/zstd_internal.h" /* constants : MaxLL, MaxML, MaxOff, LLFSELog, etc. */23242526/*-*******************************************************27* Constants28*********************************************************/29static UNUSED_ATTR const U32 LL_base[MaxLL+1] = {300, 1, 2, 3, 4, 5, 6, 7,318, 9, 10, 11, 12, 13, 14, 15,3216, 18, 20, 22, 24, 28, 32, 40,3348, 64, 0x80, 0x100, 0x200, 0x400, 0x800, 0x1000,340x2000, 0x4000, 0x8000, 0x10000 };3536static UNUSED_ATTR const U32 OF_base[MaxOff+1] = {370, 1, 1, 5, 0xD, 0x1D, 0x3D, 0x7D,380xFD, 0x1FD, 0x3FD, 0x7FD, 0xFFD, 0x1FFD, 0x3FFD, 0x7FFD,390xFFFD, 0x1FFFD, 0x3FFFD, 0x7FFFD, 0xFFFFD, 0x1FFFFD, 0x3FFFFD, 0x7FFFFD,400xFFFFFD, 0x1FFFFFD, 0x3FFFFFD, 0x7FFFFFD, 0xFFFFFFD, 0x1FFFFFFD, 0x3FFFFFFD, 0x7FFFFFFD };4142static UNUSED_ATTR const U8 OF_bits[MaxOff+1] = {430, 1, 2, 3, 4, 5, 6, 7,448, 9, 10, 11, 12, 13, 14, 15,4516, 17, 18, 19, 20, 21, 22, 23,4624, 25, 26, 27, 28, 29, 30, 31 };4748static UNUSED_ATTR const U32 ML_base[MaxML+1] = {493, 4, 5, 6, 7, 8, 9, 10,5011, 12, 13, 14, 15, 16, 17, 18,5119, 20, 21, 22, 23, 24, 25, 26,5227, 28, 29, 30, 31, 32, 33, 34,5335, 37, 39, 41, 43, 47, 51, 59,5467, 83, 99, 0x83, 0x103, 0x203, 0x403, 0x803,550x1003, 0x2003, 0x4003, 0x8003, 0x10003 };565758/*-*******************************************************59* Decompression types60*********************************************************/61typedef struct {62U32 fastMode;63U32 tableLog;64} ZSTD_seqSymbol_header;6566typedef struct {67U16 nextState;68BYTE nbAdditionalBits;69BYTE nbBits;70U32 baseValue;71} ZSTD_seqSymbol;7273#define SEQSYMBOL_TABLE_SIZE(log) (1 + (1 << (log)))7475#define ZSTD_BUILD_FSE_TABLE_WKSP_SIZE (sizeof(S16) * (MaxSeq + 1) + (1u << MaxFSELog) + sizeof(U64))76#define ZSTD_BUILD_FSE_TABLE_WKSP_SIZE_U32 ((ZSTD_BUILD_FSE_TABLE_WKSP_SIZE + sizeof(U32) - 1) / sizeof(U32))7778typedef struct {79ZSTD_seqSymbol LLTable[SEQSYMBOL_TABLE_SIZE(LLFSELog)]; /* Note : Space reserved for FSE Tables */80ZSTD_seqSymbol OFTable[SEQSYMBOL_TABLE_SIZE(OffFSELog)]; /* is also used as temporary workspace while building hufTable during DDict creation */81ZSTD_seqSymbol MLTable[SEQSYMBOL_TABLE_SIZE(MLFSELog)]; /* and therefore must be at least HUF_DECOMPRESS_WORKSPACE_SIZE large */82HUF_DTable hufTable[HUF_DTABLE_SIZE(HufLog)]; /* can accommodate HUF_decompress4X */83U32 rep[ZSTD_REP_NUM];84U32 workspace[ZSTD_BUILD_FSE_TABLE_WKSP_SIZE_U32];85} ZSTD_entropyDTables_t;8687typedef enum { ZSTDds_getFrameHeaderSize, ZSTDds_decodeFrameHeader,88ZSTDds_decodeBlockHeader, ZSTDds_decompressBlock,89ZSTDds_decompressLastBlock, ZSTDds_checkChecksum,90ZSTDds_decodeSkippableHeader, ZSTDds_skipFrame } ZSTD_dStage;9192typedef enum { zdss_init=0, zdss_loadHeader,93zdss_read, zdss_load, zdss_flush } ZSTD_dStreamStage;9495typedef enum {96ZSTD_use_indefinitely = -1, /* Use the dictionary indefinitely */97ZSTD_dont_use = 0, /* Do not use the dictionary (if one exists free it) */98ZSTD_use_once = 1 /* Use the dictionary once and set to ZSTD_dont_use */99} ZSTD_dictUses_e;100101/* Hashset for storing references to multiple ZSTD_DDict within ZSTD_DCtx */102typedef struct {103const ZSTD_DDict** ddictPtrTable;104size_t ddictPtrTableSize;105size_t ddictPtrCount;106} ZSTD_DDictHashSet;107108#ifndef ZSTD_DECODER_INTERNAL_BUFFER109# define ZSTD_DECODER_INTERNAL_BUFFER (1 << 16)110#endif111112#define ZSTD_LBMIN 64113#define ZSTD_LBMAX (128 << 10)114115/* extra buffer, compensates when dst is not large enough to store litBuffer */116#define ZSTD_LITBUFFEREXTRASIZE BOUNDED(ZSTD_LBMIN, ZSTD_DECODER_INTERNAL_BUFFER, ZSTD_LBMAX)117118typedef enum {119ZSTD_not_in_dst = 0, /* Stored entirely within litExtraBuffer */120ZSTD_in_dst = 1, /* Stored entirely within dst (in memory after current output write) */121ZSTD_split = 2 /* Split between litExtraBuffer and dst */122} ZSTD_litLocation_e;123124struct ZSTD_DCtx_s125{126const ZSTD_seqSymbol* LLTptr;127const ZSTD_seqSymbol* MLTptr;128const ZSTD_seqSymbol* OFTptr;129const HUF_DTable* HUFptr;130ZSTD_entropyDTables_t entropy;131U32 workspace[HUF_DECOMPRESS_WORKSPACE_SIZE_U32]; /* space needed when building huffman tables */132const void* previousDstEnd; /* detect continuity */133const void* prefixStart; /* start of current segment */134const void* virtualStart; /* virtual start of previous segment if it was just before current one */135const void* dictEnd; /* end of previous segment */136size_t expected;137ZSTD_frameHeader fParams;138U64 processedCSize;139U64 decodedSize;140blockType_e bType; /* used in ZSTD_decompressContinue(), store blockType between block header decoding and block decompression stages */141ZSTD_dStage stage;142U32 litEntropy;143U32 fseEntropy;144XXH64_state_t xxhState;145size_t headerSize;146ZSTD_format_e format;147ZSTD_forceIgnoreChecksum_e forceIgnoreChecksum; /* User specified: if == 1, will ignore checksums in compressed frame. Default == 0 */148U32 validateChecksum; /* if == 1, will validate checksum. Is == 1 if (fParams.checksumFlag == 1) and (forceIgnoreChecksum == 0). */149const BYTE* litPtr;150ZSTD_customMem customMem;151size_t litSize;152size_t rleSize;153size_t staticSize;154#if DYNAMIC_BMI2 != 0155int bmi2; /* == 1 if the CPU supports BMI2 and 0 otherwise. CPU support is determined dynamically once per context lifetime. */156#endif157158/* dictionary */159ZSTD_DDict* ddictLocal;160const ZSTD_DDict* ddict; /* set by ZSTD_initDStream_usingDDict(), or ZSTD_DCtx_refDDict() */161U32 dictID;162int ddictIsCold; /* if == 1 : dictionary is "new" for working context, and presumed "cold" (not in cpu cache) */163ZSTD_dictUses_e dictUses;164ZSTD_DDictHashSet* ddictSet; /* Hash set for multiple ddicts */165ZSTD_refMultipleDDicts_e refMultipleDDicts; /* User specified: if == 1, will allow references to multiple DDicts. Default == 0 (disabled) */166167/* streaming */168ZSTD_dStreamStage streamStage;169char* inBuff;170size_t inBuffSize;171size_t inPos;172size_t maxWindowSize;173char* outBuff;174size_t outBuffSize;175size_t outStart;176size_t outEnd;177size_t lhSize;178#if defined(ZSTD_LEGACY_SUPPORT) && (ZSTD_LEGACY_SUPPORT>=1)179void* legacyContext;180U32 previousLegacyVersion;181U32 legacyVersion;182#endif183U32 hostageByte;184int noForwardProgress;185ZSTD_bufferMode_e outBufferMode;186ZSTD_outBuffer expectedOutBuffer;187188/* workspace */189BYTE* litBuffer;190const BYTE* litBufferEnd;191ZSTD_litLocation_e litBufferLocation;192BYTE litExtraBuffer[ZSTD_LITBUFFEREXTRASIZE + WILDCOPY_OVERLENGTH]; /* literal buffer can be split between storage within dst and within this scratch buffer */193BYTE headerBuffer[ZSTD_FRAMEHEADERSIZE_MAX];194195size_t oversizedDuration;196197#ifdef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION198void const* dictContentBeginForFuzzing;199void const* dictContentEndForFuzzing;200#endif201202/* Tracing */203#if ZSTD_TRACE204ZSTD_TraceCtx traceCtx;205#endif206}; /* typedef'd to ZSTD_DCtx within "zstd.h" */207208MEM_STATIC int ZSTD_DCtx_get_bmi2(const struct ZSTD_DCtx_s *dctx) {209#if DYNAMIC_BMI2 != 0210return dctx->bmi2;211#else212(void)dctx;213return 0;214#endif215}216217/*-*******************************************************218* Shared internal functions219*********************************************************/220221/*! ZSTD_loadDEntropy() :222* dict : must point at beginning of a valid zstd dictionary.223* @return : size of dictionary header (size of magic number + dict ID + entropy tables) */224size_t ZSTD_loadDEntropy(ZSTD_entropyDTables_t* entropy,225const void* const dict, size_t const dictSize);226227/*! ZSTD_checkContinuity() :228* check if next `dst` follows previous position, where decompression ended.229* If yes, do nothing (continue on current segment).230* If not, classify previous segment as "external dictionary", and start a new segment.231* This function cannot fail. */232void ZSTD_checkContinuity(ZSTD_DCtx* dctx, const void* dst, size_t dstSize);233234235#endif /* ZSTD_DECOMPRESS_INTERNAL_H */236237238