You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

zstd_decompress_internal.h 7.3KB

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