zstd_decompress_internal.h 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. /* SPDX-License-Identifier: GPL-2.0+ OR BSD-3-Clause */
  2. /*
  3. * Copyright (c) Meta Platforms, Inc. and affiliates.
  4. * All rights reserved.
  5. *
  6. * This source code is licensed under both the BSD-style license (found in the
  7. * LICENSE file in the root directory of this source tree) and the GPLv2 (found
  8. * in the COPYING file in the root directory of this source tree).
  9. * You may select, at your option, one of the above-listed licenses.
  10. */
  11. /* zstd_decompress_internal:
  12. * objects and definitions shared within lib/decompress modules */
  13. #ifndef ZSTD_DECOMPRESS_INTERNAL_H
  14. #define ZSTD_DECOMPRESS_INTERNAL_H
  15. /*-*******************************************************
  16. * Dependencies
  17. *********************************************************/
  18. #include "../common/mem.h" /* BYTE, U16, U32 */
  19. #include "../common/zstd_internal.h" /* constants : MaxLL, MaxML, MaxOff, LLFSELog, etc. */
  20. /*-*******************************************************
  21. * Constants
  22. *********************************************************/
  23. static UNUSED_ATTR const U32 LL_base[MaxLL+1] = {
  24. 0, 1, 2, 3, 4, 5, 6, 7,
  25. 8, 9, 10, 11, 12, 13, 14, 15,
  26. 16, 18, 20, 22, 24, 28, 32, 40,
  27. 48, 64, 0x80, 0x100, 0x200, 0x400, 0x800, 0x1000,
  28. 0x2000, 0x4000, 0x8000, 0x10000 };
  29. static UNUSED_ATTR const U32 OF_base[MaxOff+1] = {
  30. 0, 1, 1, 5, 0xD, 0x1D, 0x3D, 0x7D,
  31. 0xFD, 0x1FD, 0x3FD, 0x7FD, 0xFFD, 0x1FFD, 0x3FFD, 0x7FFD,
  32. 0xFFFD, 0x1FFFD, 0x3FFFD, 0x7FFFD, 0xFFFFD, 0x1FFFFD, 0x3FFFFD, 0x7FFFFD,
  33. 0xFFFFFD, 0x1FFFFFD, 0x3FFFFFD, 0x7FFFFFD, 0xFFFFFFD, 0x1FFFFFFD, 0x3FFFFFFD, 0x7FFFFFFD };
  34. static UNUSED_ATTR const U8 OF_bits[MaxOff+1] = {
  35. 0, 1, 2, 3, 4, 5, 6, 7,
  36. 8, 9, 10, 11, 12, 13, 14, 15,
  37. 16, 17, 18, 19, 20, 21, 22, 23,
  38. 24, 25, 26, 27, 28, 29, 30, 31 };
  39. static UNUSED_ATTR const U32 ML_base[MaxML+1] = {
  40. 3, 4, 5, 6, 7, 8, 9, 10,
  41. 11, 12, 13, 14, 15, 16, 17, 18,
  42. 19, 20, 21, 22, 23, 24, 25, 26,
  43. 27, 28, 29, 30, 31, 32, 33, 34,
  44. 35, 37, 39, 41, 43, 47, 51, 59,
  45. 67, 83, 99, 0x83, 0x103, 0x203, 0x403, 0x803,
  46. 0x1003, 0x2003, 0x4003, 0x8003, 0x10003 };
  47. /*-*******************************************************
  48. * Decompression types
  49. *********************************************************/
  50. typedef struct {
  51. U32 fastMode;
  52. U32 tableLog;
  53. } ZSTD_seqSymbol_header;
  54. typedef struct {
  55. U16 nextState;
  56. BYTE nbAdditionalBits;
  57. BYTE nbBits;
  58. U32 baseValue;
  59. } ZSTD_seqSymbol;
  60. #define SEQSYMBOL_TABLE_SIZE(log) (1 + (1 << (log)))
  61. #define ZSTD_BUILD_FSE_TABLE_WKSP_SIZE (sizeof(S16) * (MaxSeq + 1) + (1u << MaxFSELog) + sizeof(U64))
  62. #define ZSTD_BUILD_FSE_TABLE_WKSP_SIZE_U32 ((ZSTD_BUILD_FSE_TABLE_WKSP_SIZE + sizeof(U32) - 1) / sizeof(U32))
  63. #define ZSTD_HUFFDTABLE_CAPACITY_LOG 12
  64. typedef struct {
  65. ZSTD_seqSymbol LLTable[SEQSYMBOL_TABLE_SIZE(LLFSELog)]; /* Note : Space reserved for FSE Tables */
  66. ZSTD_seqSymbol OFTable[SEQSYMBOL_TABLE_SIZE(OffFSELog)]; /* is also used as temporary workspace while building hufTable during DDict creation */
  67. ZSTD_seqSymbol MLTable[SEQSYMBOL_TABLE_SIZE(MLFSELog)]; /* and therefore must be at least HUF_DECOMPRESS_WORKSPACE_SIZE large */
  68. HUF_DTable hufTable[HUF_DTABLE_SIZE(ZSTD_HUFFDTABLE_CAPACITY_LOG)]; /* can accommodate HUF_decompress4X */
  69. U32 rep[ZSTD_REP_NUM];
  70. U32 workspace[ZSTD_BUILD_FSE_TABLE_WKSP_SIZE_U32];
  71. } ZSTD_entropyDTables_t;
  72. typedef enum { ZSTDds_getFrameHeaderSize, ZSTDds_decodeFrameHeader,
  73. ZSTDds_decodeBlockHeader, ZSTDds_decompressBlock,
  74. ZSTDds_decompressLastBlock, ZSTDds_checkChecksum,
  75. ZSTDds_decodeSkippableHeader, ZSTDds_skipFrame } ZSTD_dStage;
  76. typedef enum { zdss_init=0, zdss_loadHeader,
  77. zdss_read, zdss_load, zdss_flush } ZSTD_dStreamStage;
  78. typedef enum {
  79. ZSTD_use_indefinitely = -1, /* Use the dictionary indefinitely */
  80. ZSTD_dont_use = 0, /* Do not use the dictionary (if one exists free it) */
  81. ZSTD_use_once = 1 /* Use the dictionary once and set to ZSTD_dont_use */
  82. } ZSTD_dictUses_e;
  83. /* Hashset for storing references to multiple ZSTD_DDict within ZSTD_DCtx */
  84. typedef struct {
  85. const ZSTD_DDict** ddictPtrTable;
  86. size_t ddictPtrTableSize;
  87. size_t ddictPtrCount;
  88. } ZSTD_DDictHashSet;
  89. #ifndef ZSTD_DECODER_INTERNAL_BUFFER
  90. # define ZSTD_DECODER_INTERNAL_BUFFER (1 << 16)
  91. #endif
  92. #define ZSTD_LBMIN 64
  93. #define ZSTD_LBMAX (128 << 10)
  94. /* extra buffer, compensates when dst is not large enough to store litBuffer */
  95. #define ZSTD_LITBUFFEREXTRASIZE BOUNDED(ZSTD_LBMIN, ZSTD_DECODER_INTERNAL_BUFFER, ZSTD_LBMAX)
  96. typedef enum {
  97. ZSTD_not_in_dst = 0, /* Stored entirely within litExtraBuffer */
  98. ZSTD_in_dst = 1, /* Stored entirely within dst (in memory after current output write) */
  99. ZSTD_split = 2 /* Split between litExtraBuffer and dst */
  100. } ZSTD_litLocation_e;
  101. struct ZSTD_DCtx_s
  102. {
  103. const ZSTD_seqSymbol* LLTptr;
  104. const ZSTD_seqSymbol* MLTptr;
  105. const ZSTD_seqSymbol* OFTptr;
  106. const HUF_DTable* HUFptr;
  107. ZSTD_entropyDTables_t entropy;
  108. U32 workspace[HUF_DECOMPRESS_WORKSPACE_SIZE_U32]; /* space needed when building huffman tables */
  109. const void* previousDstEnd; /* detect continuity */
  110. const void* prefixStart; /* start of current segment */
  111. const void* virtualStart; /* virtual start of previous segment if it was just before current one */
  112. const void* dictEnd; /* end of previous segment */
  113. size_t expected;
  114. ZSTD_FrameHeader fParams;
  115. U64 processedCSize;
  116. U64 decodedSize;
  117. blockType_e bType; /* used in ZSTD_decompressContinue(), store blockType between block header decoding and block decompression stages */
  118. ZSTD_dStage stage;
  119. U32 litEntropy;
  120. U32 fseEntropy;
  121. struct xxh64_state xxhState;
  122. size_t headerSize;
  123. ZSTD_format_e format;
  124. ZSTD_forceIgnoreChecksum_e forceIgnoreChecksum; /* User specified: if == 1, will ignore checksums in compressed frame. Default == 0 */
  125. U32 validateChecksum; /* if == 1, will validate checksum. Is == 1 if (fParams.checksumFlag == 1) and (forceIgnoreChecksum == 0). */
  126. const BYTE* litPtr;
  127. ZSTD_customMem customMem;
  128. size_t litSize;
  129. size_t rleSize;
  130. size_t staticSize;
  131. int isFrameDecompression;
  132. #if DYNAMIC_BMI2
  133. int bmi2; /* == 1 if the CPU supports BMI2 and 0 otherwise. CPU support is determined dynamically once per context lifetime. */
  134. #endif
  135. /* dictionary */
  136. ZSTD_DDict* ddictLocal;
  137. const ZSTD_DDict* ddict; /* set by ZSTD_initDStream_usingDDict(), or ZSTD_DCtx_refDDict() */
  138. U32 dictID;
  139. int ddictIsCold; /* if == 1 : dictionary is "new" for working context, and presumed "cold" (not in cpu cache) */
  140. ZSTD_dictUses_e dictUses;
  141. ZSTD_DDictHashSet* ddictSet; /* Hash set for multiple ddicts */
  142. ZSTD_refMultipleDDicts_e refMultipleDDicts; /* User specified: if == 1, will allow references to multiple DDicts. Default == 0 (disabled) */
  143. int disableHufAsm;
  144. int maxBlockSizeParam;
  145. /* streaming */
  146. ZSTD_dStreamStage streamStage;
  147. char* inBuff;
  148. size_t inBuffSize;
  149. size_t inPos;
  150. size_t maxWindowSize;
  151. char* outBuff;
  152. size_t outBuffSize;
  153. size_t outStart;
  154. size_t outEnd;
  155. size_t lhSize;
  156. U32 hostageByte;
  157. int noForwardProgress;
  158. ZSTD_bufferMode_e outBufferMode;
  159. ZSTD_outBuffer expectedOutBuffer;
  160. /* workspace */
  161. BYTE* litBuffer;
  162. const BYTE* litBufferEnd;
  163. ZSTD_litLocation_e litBufferLocation;
  164. BYTE litExtraBuffer[ZSTD_LITBUFFEREXTRASIZE + WILDCOPY_OVERLENGTH]; /* literal buffer can be split between storage within dst and within this scratch buffer */
  165. BYTE headerBuffer[ZSTD_FRAMEHEADERSIZE_MAX];
  166. size_t oversizedDuration;
  167. #ifdef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
  168. void const* dictContentBeginForFuzzing;
  169. void const* dictContentEndForFuzzing;
  170. #endif
  171. /* Tracing */
  172. }; /* typedef'd to ZSTD_DCtx within "zstd.h" */
  173. MEM_STATIC int ZSTD_DCtx_get_bmi2(const struct ZSTD_DCtx_s *dctx) {
  174. #if DYNAMIC_BMI2
  175. return dctx->bmi2;
  176. #else
  177. (void)dctx;
  178. return 0;
  179. #endif
  180. }
  181. /*-*******************************************************
  182. * Shared internal functions
  183. *********************************************************/
  184. /*! ZSTD_loadDEntropy() :
  185. * dict : must point at beginning of a valid zstd dictionary.
  186. * @return : size of dictionary header (size of magic number + dict ID + entropy tables) */
  187. size_t ZSTD_loadDEntropy(ZSTD_entropyDTables_t* entropy,
  188. const void* const dict, size_t const dictSize);
  189. /*! ZSTD_checkContinuity() :
  190. * check if next `dst` follows previous position, where decompression ended.
  191. * If yes, do nothing (continue on current segment).
  192. * If not, classify previous segment as "external dictionary", and start a new segment.
  193. * This function cannot fail. */
  194. void ZSTD_checkContinuity(ZSTD_DCtx* dctx, const void* dst, size_t dstSize);
  195. #endif /* ZSTD_DECOMPRESS_INTERNAL_H */