Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

zstd_internal.h 7.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. /**
  2. * Copyright (c) 2016-present, Yann Collet, Facebook, Inc.
  3. * All rights reserved.
  4. *
  5. * This source code is licensed under the BSD-style license found in the
  6. * LICENSE file in the root directory of this source tree. An additional grant
  7. * of patent rights can be found in the PATENTS file in the same directory.
  8. */
  9. #ifndef ZSTD_CCOMMON_H_MODULE
  10. #define ZSTD_CCOMMON_H_MODULE
  11. /*-*************************************
  12. * Dependencies
  13. ***************************************/
  14. #include "mem.h"
  15. #include "error_private.h"
  16. #define ZSTD_STATIC_LINKING_ONLY
  17. #include "zstd.h"
  18. /*-*************************************
  19. * Common macros
  20. ***************************************/
  21. #define MIN(a,b) ((a)<(b) ? (a) : (b))
  22. #define MAX(a,b) ((a)>(b) ? (a) : (b))
  23. /*-*************************************
  24. * Common constants
  25. ***************************************/
  26. #define ZSTD_OPT_NUM (1<<12)
  27. #define ZSTD_DICT_MAGIC 0xEC30A437 /* v0.7+ */
  28. #define ZSTD_REP_NUM 3 /* number of repcodes */
  29. #define ZSTD_REP_CHECK (ZSTD_REP_NUM) /* number of repcodes to check by the optimal parser */
  30. #define ZSTD_REP_MOVE (ZSTD_REP_NUM-1)
  31. #define ZSTD_REP_MOVE_OPT (ZSTD_REP_NUM)
  32. static const U32 repStartValue[ZSTD_REP_NUM] = { 1, 4, 8 };
  33. #define KB *(1 <<10)
  34. #define MB *(1 <<20)
  35. #define GB *(1U<<30)
  36. #define BIT7 128
  37. #define BIT6 64
  38. #define BIT5 32
  39. #define BIT4 16
  40. #define BIT1 2
  41. #define BIT0 1
  42. #define ZSTD_WINDOWLOG_ABSOLUTEMIN 10
  43. static const size_t ZSTD_fcs_fieldSize[4] = { 0, 2, 4, 8 };
  44. static const size_t ZSTD_did_fieldSize[4] = { 0, 1, 2, 4 };
  45. #define ZSTD_BLOCKHEADERSIZE 3 /* C standard doesn't allow `static const` variable to be init using another `static const` variable */
  46. static const size_t ZSTD_blockHeaderSize = ZSTD_BLOCKHEADERSIZE;
  47. typedef enum { bt_raw, bt_rle, bt_compressed, bt_reserved } blockType_e;
  48. #define MIN_SEQUENCES_SIZE 1 /* nbSeq==0 */
  49. #define MIN_CBLOCK_SIZE (1 /*litCSize*/ + 1 /* RLE or RAW */ + MIN_SEQUENCES_SIZE /* nbSeq==0 */) /* for a non-null block */
  50. #define HufLog 12
  51. typedef enum { set_basic, set_rle, set_compressed, set_repeat } symbolEncodingType_e;
  52. #define LONGNBSEQ 0x7F00
  53. #define MINMATCH 3
  54. #define EQUAL_READ32 4
  55. #define Litbits 8
  56. #define MaxLit ((1<<Litbits) - 1)
  57. #define MaxML 52
  58. #define MaxLL 35
  59. #define MaxOff 28
  60. #define MaxSeq MAX(MaxLL, MaxML) /* Assumption : MaxOff < MaxLL,MaxML */
  61. #define MLFSELog 9
  62. #define LLFSELog 9
  63. #define OffFSELog 8
  64. static const U32 LL_bits[MaxLL+1] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  65. 1, 1, 1, 1, 2, 2, 3, 3, 4, 6, 7, 8, 9,10,11,12,
  66. 13,14,15,16 };
  67. static const S16 LL_defaultNorm[MaxLL+1] = { 4, 3, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 1, 1, 1,
  68. 2, 2, 2, 2, 2, 2, 2, 2, 2, 3, 2, 1, 1, 1, 1, 1,
  69. -1,-1,-1,-1 };
  70. static const U32 LL_defaultNormLog = 6;
  71. static const U32 ML_bits[MaxML+1] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  72. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  73. 1, 1, 1, 1, 2, 2, 3, 3, 4, 4, 5, 7, 8, 9,10,11,
  74. 12,13,14,15,16 };
  75. static const S16 ML_defaultNorm[MaxML+1] = { 1, 4, 3, 2, 2, 2, 2, 2, 2, 1, 1, 1, 1, 1, 1, 1,
  76. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  77. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,-1,-1,
  78. -1,-1,-1,-1,-1 };
  79. static const U32 ML_defaultNormLog = 6;
  80. static const S16 OF_defaultNorm[MaxOff+1] = { 1, 1, 1, 1, 1, 1, 2, 2, 2, 1, 1, 1, 1, 1, 1, 1,
  81. 1, 1, 1, 1, 1, 1, 1, 1,-1,-1,-1,-1,-1 };
  82. static const U32 OF_defaultNormLog = 5;
  83. /*-*******************************************
  84. * Shared functions to include for inlining
  85. *********************************************/
  86. static void ZSTD_copy8(void* dst, const void* src) { memcpy(dst, src, 8); }
  87. #define COPY8(d,s) { ZSTD_copy8(d,s); d+=8; s+=8; }
  88. /*! ZSTD_wildcopy() :
  89. * custom version of memcpy(), can copy up to 7 bytes too many (8 bytes if length==0) */
  90. #define WILDCOPY_OVERLENGTH 8
  91. MEM_STATIC void ZSTD_wildcopy(void* dst, const void* src, size_t length)
  92. {
  93. const BYTE* ip = (const BYTE*)src;
  94. BYTE* op = (BYTE*)dst;
  95. BYTE* const oend = op + length;
  96. do
  97. COPY8(op, ip)
  98. while (op < oend);
  99. }
  100. MEM_STATIC void ZSTD_wildcopy_e(void* dst, const void* src, void* dstEnd) /* should be faster for decoding, but strangely, not verified on all platform */
  101. {
  102. const BYTE* ip = (const BYTE*)src;
  103. BYTE* op = (BYTE*)dst;
  104. BYTE* const oend = (BYTE*)dstEnd;
  105. do
  106. COPY8(op, ip)
  107. while (op < oend);
  108. }
  109. /*-*******************************************
  110. * Private interfaces
  111. *********************************************/
  112. typedef struct ZSTD_stats_s ZSTD_stats_t;
  113. typedef struct {
  114. U32 off;
  115. U32 len;
  116. } ZSTD_match_t;
  117. typedef struct {
  118. U32 price;
  119. U32 off;
  120. U32 mlen;
  121. U32 litlen;
  122. U32 rep[ZSTD_REP_NUM];
  123. } ZSTD_optimal_t;
  124. typedef struct seqDef_s {
  125. U32 offset;
  126. U16 litLength;
  127. U16 matchLength;
  128. } seqDef;
  129. typedef struct {
  130. seqDef* sequencesStart;
  131. seqDef* sequences;
  132. BYTE* litStart;
  133. BYTE* lit;
  134. BYTE* llCode;
  135. BYTE* mlCode;
  136. BYTE* ofCode;
  137. U32 longLengthID; /* 0 == no longLength; 1 == Lit.longLength; 2 == Match.longLength; */
  138. U32 longLengthPos;
  139. /* opt */
  140. ZSTD_optimal_t* priceTable;
  141. ZSTD_match_t* matchTable;
  142. U32* matchLengthFreq;
  143. U32* litLengthFreq;
  144. U32* litFreq;
  145. U32* offCodeFreq;
  146. U32 matchLengthSum;
  147. U32 matchSum;
  148. U32 litLengthSum;
  149. U32 litSum;
  150. U32 offCodeSum;
  151. U32 log2matchLengthSum;
  152. U32 log2matchSum;
  153. U32 log2litLengthSum;
  154. U32 log2litSum;
  155. U32 log2offCodeSum;
  156. U32 factor;
  157. U32 cachedPrice;
  158. U32 cachedLitLength;
  159. const BYTE* cachedLiterals;
  160. } seqStore_t;
  161. const seqStore_t* ZSTD_getSeqStore(const ZSTD_CCtx* ctx);
  162. void ZSTD_seqToCodes(const seqStore_t* seqStorePtr);
  163. int ZSTD_isSkipFrame(ZSTD_DCtx* dctx);
  164. /* custom memory allocation functions */
  165. void* ZSTD_defaultAllocFunction(void* opaque, size_t size);
  166. void ZSTD_defaultFreeFunction(void* opaque, void* address);
  167. static const ZSTD_customMem defaultCustomMem = { ZSTD_defaultAllocFunction, ZSTD_defaultFreeFunction, NULL };
  168. void* ZSTD_malloc(size_t size, ZSTD_customMem customMem);
  169. void ZSTD_free(void* ptr, ZSTD_customMem customMem);
  170. /*====== common function ======*/
  171. MEM_STATIC U32 ZSTD_highbit32(U32 val)
  172. {
  173. # if defined(_MSC_VER) /* Visual */
  174. unsigned long r=0;
  175. _BitScanReverse(&r, val);
  176. return (unsigned)r;
  177. # elif defined(__GNUC__) && (__GNUC__ >= 3) /* GCC Intrinsic */
  178. return 31 - __builtin_clz(val);
  179. # else /* Software version */
  180. static const int DeBruijnClz[32] = { 0, 9, 1, 10, 13, 21, 2, 29, 11, 14, 16, 18, 22, 25, 3, 30, 8, 12, 20, 28, 15, 17, 24, 7, 19, 27, 23, 6, 26, 5, 4, 31 };
  181. U32 v = val;
  182. int r;
  183. v |= v >> 1;
  184. v |= v >> 2;
  185. v |= v >> 4;
  186. v |= v >> 8;
  187. v |= v >> 16;
  188. r = DeBruijnClz[(U32)(v * 0x07C4ACDDU) >> 27];
  189. return r;
  190. # endif
  191. }
  192. #endif /* ZSTD_CCOMMON_H_MODULE */