zstd_decompress_internal.h 9.4 KB

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