zstd_decompress_internal.h 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. /*
  2. * Copyright (c) Yann Collet, Facebook, Inc.
  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" /* ZSTD_seqSymbol */
  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 U32 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. typedef struct {
  63. ZSTD_seqSymbol LLTable[SEQSYMBOL_TABLE_SIZE(LLFSELog)]; /* Note : Space reserved for FSE Tables */
  64. ZSTD_seqSymbol OFTable[SEQSYMBOL_TABLE_SIZE(OffFSELog)]; /* is also used as temporary workspace while building hufTable during DDict creation */
  65. ZSTD_seqSymbol MLTable[SEQSYMBOL_TABLE_SIZE(MLFSELog)]; /* and therefore must be at least HUF_DECOMPRESS_WORKSPACE_SIZE large */
  66. HUF_DTable hufTable[HUF_DTABLE_SIZE(HufLog)]; /* can accommodate HUF_decompress4X */
  67. U32 rep[ZSTD_REP_NUM];
  68. U32 workspace[ZSTD_BUILD_FSE_TABLE_WKSP_SIZE_U32];
  69. } ZSTD_entropyDTables_t;
  70. typedef enum { ZSTDds_getFrameHeaderSize, ZSTDds_decodeFrameHeader,
  71. ZSTDds_decodeBlockHeader, ZSTDds_decompressBlock,
  72. ZSTDds_decompressLastBlock, ZSTDds_checkChecksum,
  73. ZSTDds_decodeSkippableHeader, ZSTDds_skipFrame } ZSTD_dStage;
  74. typedef enum { zdss_init=0, zdss_loadHeader,
  75. zdss_read, zdss_load, zdss_flush } ZSTD_dStreamStage;
  76. typedef enum {
  77. ZSTD_use_indefinitely = -1, /* Use the dictionary indefinitely */
  78. ZSTD_dont_use = 0, /* Do not use the dictionary (if one exists free it) */
  79. ZSTD_use_once = 1 /* Use the dictionary once and set to ZSTD_dont_use */
  80. } ZSTD_dictUses_e;
  81. /* Hashset for storing references to multiple ZSTD_DDict within ZSTD_DCtx */
  82. typedef struct {
  83. const ZSTD_DDict** ddictPtrTable;
  84. size_t ddictPtrTableSize;
  85. size_t ddictPtrCount;
  86. } ZSTD_DDictHashSet;
  87. struct ZSTD_DCtx_s
  88. {
  89. const ZSTD_seqSymbol* LLTptr;
  90. const ZSTD_seqSymbol* MLTptr;
  91. const ZSTD_seqSymbol* OFTptr;
  92. const HUF_DTable* HUFptr;
  93. ZSTD_entropyDTables_t entropy;
  94. U32 workspace[HUF_DECOMPRESS_WORKSPACE_SIZE_U32]; /* space needed when building huffman tables */
  95. const void* previousDstEnd; /* detect continuity */
  96. const void* prefixStart; /* start of current segment */
  97. const void* virtualStart; /* virtual start of previous segment if it was just before current one */
  98. const void* dictEnd; /* end of previous segment */
  99. size_t expected;
  100. ZSTD_frameHeader fParams;
  101. U64 processedCSize;
  102. U64 decodedSize;
  103. blockType_e bType; /* used in ZSTD_decompressContinue(), store blockType between block header decoding and block decompression stages */
  104. ZSTD_dStage stage;
  105. U32 litEntropy;
  106. U32 fseEntropy;
  107. struct xxh64_state xxhState;
  108. size_t headerSize;
  109. ZSTD_format_e format;
  110. ZSTD_forceIgnoreChecksum_e forceIgnoreChecksum; /* User specified: if == 1, will ignore checksums in compressed frame. Default == 0 */
  111. U32 validateChecksum; /* if == 1, will validate checksum. Is == 1 if (fParams.checksumFlag == 1) and (forceIgnoreChecksum == 0). */
  112. const BYTE* litPtr;
  113. ZSTD_customMem customMem;
  114. size_t litSize;
  115. size_t rleSize;
  116. size_t staticSize;
  117. int bmi2; /* == 1 if the CPU supports BMI2 and 0 otherwise. CPU support is determined dynamically once per context lifetime. */
  118. /* dictionary */
  119. ZSTD_DDict* ddictLocal;
  120. const ZSTD_DDict* ddict; /* set by ZSTD_initDStream_usingDDict(), or ZSTD_DCtx_refDDict() */
  121. U32 dictID;
  122. int ddictIsCold; /* if == 1 : dictionary is "new" for working context, and presumed "cold" (not in cpu cache) */
  123. ZSTD_dictUses_e dictUses;
  124. ZSTD_DDictHashSet* ddictSet; /* Hash set for multiple ddicts */
  125. ZSTD_refMultipleDDicts_e refMultipleDDicts; /* User specified: if == 1, will allow references to multiple DDicts. Default == 0 (disabled) */
  126. /* streaming */
  127. ZSTD_dStreamStage streamStage;
  128. char* inBuff;
  129. size_t inBuffSize;
  130. size_t inPos;
  131. size_t maxWindowSize;
  132. char* outBuff;
  133. size_t outBuffSize;
  134. size_t outStart;
  135. size_t outEnd;
  136. size_t lhSize;
  137. void* legacyContext;
  138. U32 previousLegacyVersion;
  139. U32 legacyVersion;
  140. U32 hostageByte;
  141. int noForwardProgress;
  142. ZSTD_bufferMode_e outBufferMode;
  143. ZSTD_outBuffer expectedOutBuffer;
  144. /* workspace */
  145. BYTE litBuffer[ZSTD_BLOCKSIZE_MAX + WILDCOPY_OVERLENGTH];
  146. BYTE headerBuffer[ZSTD_FRAMEHEADERSIZE_MAX];
  147. size_t oversizedDuration;
  148. #ifdef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
  149. void const* dictContentBeginForFuzzing;
  150. void const* dictContentEndForFuzzing;
  151. #endif
  152. /* Tracing */
  153. }; /* typedef'd to ZSTD_DCtx within "zstd.h" */
  154. /*-*******************************************************
  155. * Shared internal functions
  156. *********************************************************/
  157. /*! ZSTD_loadDEntropy() :
  158. * dict : must point at beginning of a valid zstd dictionary.
  159. * @return : size of dictionary header (size of magic number + dict ID + entropy tables) */
  160. size_t ZSTD_loadDEntropy(ZSTD_entropyDTables_t* entropy,
  161. const void* const dict, size_t const dictSize);
  162. /*! ZSTD_checkContinuity() :
  163. * check if next `dst` follows previous position, where decompression ended.
  164. * If yes, do nothing (continue on current segment).
  165. * If not, classify previous segment as "external dictionary", and start a new segment.
  166. * This function cannot fail. */
  167. void ZSTD_checkContinuity(ZSTD_DCtx* dctx, const void* dst, size_t dstSize);
  168. #endif /* ZSTD_DECOMPRESS_INTERNAL_H */