Path: blob/master/Utilities/cmzstd/lib/decompress/zstd_decompress_internal.h
3158 views
/*1* Copyright (c) Meta Platforms, Inc. and affiliates.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))77#define ZSTD_HUFFDTABLE_CAPACITY_LOG 127879typedef struct {80ZSTD_seqSymbol LLTable[SEQSYMBOL_TABLE_SIZE(LLFSELog)]; /* Note : Space reserved for FSE Tables */81ZSTD_seqSymbol OFTable[SEQSYMBOL_TABLE_SIZE(OffFSELog)]; /* is also used as temporary workspace while building hufTable during DDict creation */82ZSTD_seqSymbol MLTable[SEQSYMBOL_TABLE_SIZE(MLFSELog)]; /* and therefore must be at least HUF_DECOMPRESS_WORKSPACE_SIZE large */83HUF_DTable hufTable[HUF_DTABLE_SIZE(ZSTD_HUFFDTABLE_CAPACITY_LOG)]; /* can accommodate HUF_decompress4X */84U32 rep[ZSTD_REP_NUM];85U32 workspace[ZSTD_BUILD_FSE_TABLE_WKSP_SIZE_U32];86} ZSTD_entropyDTables_t;8788typedef enum { ZSTDds_getFrameHeaderSize, ZSTDds_decodeFrameHeader,89ZSTDds_decodeBlockHeader, ZSTDds_decompressBlock,90ZSTDds_decompressLastBlock, ZSTDds_checkChecksum,91ZSTDds_decodeSkippableHeader, ZSTDds_skipFrame } ZSTD_dStage;9293typedef enum { zdss_init=0, zdss_loadHeader,94zdss_read, zdss_load, zdss_flush } ZSTD_dStreamStage;9596typedef enum {97ZSTD_use_indefinitely = -1, /* Use the dictionary indefinitely */98ZSTD_dont_use = 0, /* Do not use the dictionary (if one exists free it) */99ZSTD_use_once = 1 /* Use the dictionary once and set to ZSTD_dont_use */100} ZSTD_dictUses_e;101102/* Hashset for storing references to multiple ZSTD_DDict within ZSTD_DCtx */103typedef struct {104const ZSTD_DDict** ddictPtrTable;105size_t ddictPtrTableSize;106size_t ddictPtrCount;107} ZSTD_DDictHashSet;108109#ifndef ZSTD_DECODER_INTERNAL_BUFFER110# define ZSTD_DECODER_INTERNAL_BUFFER (1 << 16)111#endif112113#define ZSTD_LBMIN 64114#define ZSTD_LBMAX (128 << 10)115116/* extra buffer, compensates when dst is not large enough to store litBuffer */117#define ZSTD_LITBUFFEREXTRASIZE BOUNDED(ZSTD_LBMIN, ZSTD_DECODER_INTERNAL_BUFFER, ZSTD_LBMAX)118119typedef enum {120ZSTD_not_in_dst = 0, /* Stored entirely within litExtraBuffer */121ZSTD_in_dst = 1, /* Stored entirely within dst (in memory after current output write) */122ZSTD_split = 2 /* Split between litExtraBuffer and dst */123} ZSTD_litLocation_e;124125struct ZSTD_DCtx_s126{127const ZSTD_seqSymbol* LLTptr;128const ZSTD_seqSymbol* MLTptr;129const ZSTD_seqSymbol* OFTptr;130const HUF_DTable* HUFptr;131ZSTD_entropyDTables_t entropy;132U32 workspace[HUF_DECOMPRESS_WORKSPACE_SIZE_U32]; /* space needed when building huffman tables */133const void* previousDstEnd; /* detect continuity */134const void* prefixStart; /* start of current segment */135const void* virtualStart; /* virtual start of previous segment if it was just before current one */136const void* dictEnd; /* end of previous segment */137size_t expected;138ZSTD_frameHeader fParams;139U64 processedCSize;140U64 decodedSize;141blockType_e bType; /* used in ZSTD_decompressContinue(), store blockType between block header decoding and block decompression stages */142ZSTD_dStage stage;143U32 litEntropy;144U32 fseEntropy;145XXH64_state_t xxhState;146size_t headerSize;147ZSTD_format_e format;148ZSTD_forceIgnoreChecksum_e forceIgnoreChecksum; /* User specified: if == 1, will ignore checksums in compressed frame. Default == 0 */149U32 validateChecksum; /* if == 1, will validate checksum. Is == 1 if (fParams.checksumFlag == 1) and (forceIgnoreChecksum == 0). */150const BYTE* litPtr;151ZSTD_customMem customMem;152size_t litSize;153size_t rleSize;154size_t staticSize;155#if DYNAMIC_BMI2 != 0156int bmi2; /* == 1 if the CPU supports BMI2 and 0 otherwise. CPU support is determined dynamically once per context lifetime. */157#endif158159/* dictionary */160ZSTD_DDict* ddictLocal;161const ZSTD_DDict* ddict; /* set by ZSTD_initDStream_usingDDict(), or ZSTD_DCtx_refDDict() */162U32 dictID;163int ddictIsCold; /* if == 1 : dictionary is "new" for working context, and presumed "cold" (not in cpu cache) */164ZSTD_dictUses_e dictUses;165ZSTD_DDictHashSet* ddictSet; /* Hash set for multiple ddicts */166ZSTD_refMultipleDDicts_e refMultipleDDicts; /* User specified: if == 1, will allow references to multiple DDicts. Default == 0 (disabled) */167int disableHufAsm;168169/* streaming */170ZSTD_dStreamStage streamStage;171char* inBuff;172size_t inBuffSize;173size_t inPos;174size_t maxWindowSize;175char* outBuff;176size_t outBuffSize;177size_t outStart;178size_t outEnd;179size_t lhSize;180#if defined(ZSTD_LEGACY_SUPPORT) && (ZSTD_LEGACY_SUPPORT>=1)181void* legacyContext;182U32 previousLegacyVersion;183U32 legacyVersion;184#endif185U32 hostageByte;186int noForwardProgress;187ZSTD_bufferMode_e outBufferMode;188ZSTD_outBuffer expectedOutBuffer;189190/* workspace */191BYTE* litBuffer;192const BYTE* litBufferEnd;193ZSTD_litLocation_e litBufferLocation;194BYTE litExtraBuffer[ZSTD_LITBUFFEREXTRASIZE + WILDCOPY_OVERLENGTH]; /* literal buffer can be split between storage within dst and within this scratch buffer */195BYTE headerBuffer[ZSTD_FRAMEHEADERSIZE_MAX];196197size_t oversizedDuration;198199#ifdef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION200void const* dictContentBeginForFuzzing;201void const* dictContentEndForFuzzing;202#endif203204/* Tracing */205#if ZSTD_TRACE206ZSTD_TraceCtx traceCtx;207#endif208}; /* typedef'd to ZSTD_DCtx within "zstd.h" */209210MEM_STATIC int ZSTD_DCtx_get_bmi2(const struct ZSTD_DCtx_s *dctx) {211#if DYNAMIC_BMI2 != 0212return dctx->bmi2;213#else214(void)dctx;215return 0;216#endif217}218219/*-*******************************************************220* Shared internal functions221*********************************************************/222223/*! ZSTD_loadDEntropy() :224* dict : must point at beginning of a valid zstd dictionary.225* @return : size of dictionary header (size of magic number + dict ID + entropy tables) */226size_t ZSTD_loadDEntropy(ZSTD_entropyDTables_t* entropy,227const void* const dict, size_t const dictSize);228229/*! ZSTD_checkContinuity() :230* check if next `dst` follows previous position, where decompression ended.231* If yes, do nothing (continue on current segment).232* If not, classify previous segment as "external dictionary", and start a new segment.233* This function cannot fail. */234void ZSTD_checkContinuity(ZSTD_DCtx* dctx, const void* dst, size_t dstSize);235236237#endif /* ZSTD_DECOMPRESS_INTERNAL_H */238239240