zstd_internal.h 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. /*
  2. * Copyright (c) 2016-present, 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. #ifndef ZSTD_CCOMMON_H_MODULE
  11. #define ZSTD_CCOMMON_H_MODULE
  12. /* this module contains definitions which must be identical
  13. * across compression, decompression and dictBuilder.
  14. * It also contains a few functions useful to at least 2 of them
  15. * and which benefit from being inlined */
  16. /*-*************************************
  17. * Dependencies
  18. ***************************************/
  19. #include "compiler.h"
  20. #include "mem.h"
  21. #include "debug.h" /* assert, DEBUGLOG, RAWLOG, g_debuglevel */
  22. #include "error_private.h"
  23. #define ZSTD_STATIC_LINKING_ONLY
  24. #include "zstd.h"
  25. #define FSE_STATIC_LINKING_ONLY
  26. #include "fse.h"
  27. #define HUF_STATIC_LINKING_ONLY
  28. #include "huf.h"
  29. #ifndef XXH_STATIC_LINKING_ONLY
  30. # define XXH_STATIC_LINKING_ONLY /* XXH64_state_t */
  31. #endif
  32. #include "xxhash.h" /* XXH_reset, update, digest */
  33. #if defined (__cplusplus)
  34. extern "C" {
  35. #endif
  36. /* ---- static assert (debug) --- */
  37. #define ZSTD_STATIC_ASSERT(c) DEBUG_STATIC_ASSERT(c)
  38. /*-*************************************
  39. * shared macros
  40. ***************************************/
  41. #undef MIN
  42. #undef MAX
  43. #define MIN(a,b) ((a)<(b) ? (a) : (b))
  44. #define MAX(a,b) ((a)>(b) ? (a) : (b))
  45. #define CHECK_F(f) { size_t const errcod = f; if (ERR_isError(errcod)) return errcod; } /* check and Forward error code */
  46. #define CHECK_E(f, e) { size_t const errcod = f; if (ERR_isError(errcod)) return ERROR(e); } /* check and send Error code */
  47. /*-*************************************
  48. * Common constants
  49. ***************************************/
  50. #define ZSTD_OPT_NUM (1<<12)
  51. #define ZSTD_REP_NUM 3 /* number of repcodes */
  52. #define ZSTD_REP_MOVE (ZSTD_REP_NUM-1)
  53. static const U32 repStartValue[ZSTD_REP_NUM] = { 1, 4, 8 };
  54. #define KB *(1 <<10)
  55. #define MB *(1 <<20)
  56. #define GB *(1U<<30)
  57. #define BIT7 128
  58. #define BIT6 64
  59. #define BIT5 32
  60. #define BIT4 16
  61. #define BIT1 2
  62. #define BIT0 1
  63. #define ZSTD_WINDOWLOG_ABSOLUTEMIN 10
  64. #define ZSTD_WINDOWLOG_DEFAULTMAX 27 /* Default maximum allowed window log */
  65. static const size_t ZSTD_fcs_fieldSize[4] = { 0, 2, 4, 8 };
  66. static const size_t ZSTD_did_fieldSize[4] = { 0, 1, 2, 4 };
  67. #define ZSTD_FRAMEIDSIZE 4 /* magic number size */
  68. #define ZSTD_BLOCKHEADERSIZE 3 /* C standard doesn't allow `static const` variable to be init using another `static const` variable */
  69. static const size_t ZSTD_blockHeaderSize = ZSTD_BLOCKHEADERSIZE;
  70. typedef enum { bt_raw, bt_rle, bt_compressed, bt_reserved } blockType_e;
  71. #define MIN_SEQUENCES_SIZE 1 /* nbSeq==0 */
  72. #define MIN_CBLOCK_SIZE (1 /*litCSize*/ + 1 /* RLE or RAW */ + MIN_SEQUENCES_SIZE /* nbSeq==0 */) /* for a non-null block */
  73. #define HufLog 12
  74. typedef enum { set_basic, set_rle, set_compressed, set_repeat } symbolEncodingType_e;
  75. #define LONGNBSEQ 0x7F00
  76. #define MINMATCH 3
  77. #define Litbits 8
  78. #define MaxLit ((1<<Litbits) - 1)
  79. #define MaxML 52
  80. #define MaxLL 35
  81. #define DefaultMaxOff 28
  82. #define MaxOff 31
  83. #define MaxSeq MAX(MaxLL, MaxML) /* Assumption : MaxOff < MaxLL,MaxML */
  84. #define MLFSELog 9
  85. #define LLFSELog 9
  86. #define OffFSELog 8
  87. #define MaxFSELog MAX(MAX(MLFSELog, LLFSELog), OffFSELog)
  88. static const U32 LL_bits[MaxLL+1] = { 0, 0, 0, 0, 0, 0, 0, 0,
  89. 0, 0, 0, 0, 0, 0, 0, 0,
  90. 1, 1, 1, 1, 2, 2, 3, 3,
  91. 4, 6, 7, 8, 9,10,11,12,
  92. 13,14,15,16 };
  93. static const S16 LL_defaultNorm[MaxLL+1] = { 4, 3, 2, 2, 2, 2, 2, 2,
  94. 2, 2, 2, 2, 2, 1, 1, 1,
  95. 2, 2, 2, 2, 2, 2, 2, 2,
  96. 2, 3, 2, 1, 1, 1, 1, 1,
  97. -1,-1,-1,-1 };
  98. #define LL_DEFAULTNORMLOG 6 /* for static allocation */
  99. static const U32 LL_defaultNormLog = LL_DEFAULTNORMLOG;
  100. static const U32 ML_bits[MaxML+1] = { 0, 0, 0, 0, 0, 0, 0, 0,
  101. 0, 0, 0, 0, 0, 0, 0, 0,
  102. 0, 0, 0, 0, 0, 0, 0, 0,
  103. 0, 0, 0, 0, 0, 0, 0, 0,
  104. 1, 1, 1, 1, 2, 2, 3, 3,
  105. 4, 4, 5, 7, 8, 9,10,11,
  106. 12,13,14,15,16 };
  107. static const S16 ML_defaultNorm[MaxML+1] = { 1, 4, 3, 2, 2, 2, 2, 2,
  108. 2, 1, 1, 1, 1, 1, 1, 1,
  109. 1, 1, 1, 1, 1, 1, 1, 1,
  110. 1, 1, 1, 1, 1, 1, 1, 1,
  111. 1, 1, 1, 1, 1, 1, 1, 1,
  112. 1, 1, 1, 1, 1, 1,-1,-1,
  113. -1,-1,-1,-1,-1 };
  114. #define ML_DEFAULTNORMLOG 6 /* for static allocation */
  115. static const U32 ML_defaultNormLog = ML_DEFAULTNORMLOG;
  116. static const S16 OF_defaultNorm[DefaultMaxOff+1] = { 1, 1, 1, 1, 1, 1, 2, 2,
  117. 2, 1, 1, 1, 1, 1, 1, 1,
  118. 1, 1, 1, 1, 1, 1, 1, 1,
  119. -1,-1,-1,-1,-1 };
  120. #define OF_DEFAULTNORMLOG 5 /* for static allocation */
  121. static const U32 OF_defaultNormLog = OF_DEFAULTNORMLOG;
  122. /*-*******************************************
  123. * Shared functions to include for inlining
  124. *********************************************/
  125. static void ZSTD_copy8(void* dst, const void* src) { memcpy(dst, src, 8); }
  126. #define COPY8(d,s) { ZSTD_copy8(d,s); d+=8; s+=8; }
  127. /*! ZSTD_wildcopy() :
  128. * custom version of memcpy(), can overwrite up to WILDCOPY_OVERLENGTH bytes (if length==0) */
  129. #define WILDCOPY_OVERLENGTH 8
  130. MEM_STATIC void ZSTD_wildcopy(void* dst, const void* src, ptrdiff_t length)
  131. {
  132. const BYTE* ip = (const BYTE*)src;
  133. BYTE* op = (BYTE*)dst;
  134. BYTE* const oend = op + length;
  135. do
  136. COPY8(op, ip)
  137. while (op < oend);
  138. }
  139. 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 */
  140. {
  141. const BYTE* ip = (const BYTE*)src;
  142. BYTE* op = (BYTE*)dst;
  143. BYTE* const oend = (BYTE*)dstEnd;
  144. do
  145. COPY8(op, ip)
  146. while (op < oend);
  147. }
  148. /*-*******************************************
  149. * Private declarations
  150. *********************************************/
  151. typedef struct seqDef_s {
  152. U32 offset;
  153. U16 litLength;
  154. U16 matchLength;
  155. } seqDef;
  156. typedef struct {
  157. seqDef* sequencesStart;
  158. seqDef* sequences;
  159. BYTE* litStart;
  160. BYTE* lit;
  161. BYTE* llCode;
  162. BYTE* mlCode;
  163. BYTE* ofCode;
  164. size_t maxNbSeq;
  165. size_t maxNbLit;
  166. U32 longLengthID; /* 0 == no longLength; 1 == Lit.longLength; 2 == Match.longLength; */
  167. U32 longLengthPos;
  168. } seqStore_t;
  169. const seqStore_t* ZSTD_getSeqStore(const ZSTD_CCtx* ctx); /* compress & dictBuilder */
  170. void ZSTD_seqToCodes(const seqStore_t* seqStorePtr); /* compress, dictBuilder, decodeCorpus (shouldn't get its definition from here) */
  171. /* custom memory allocation functions */
  172. void* ZSTD_malloc(size_t size, ZSTD_customMem customMem);
  173. void* ZSTD_calloc(size_t size, ZSTD_customMem customMem);
  174. void ZSTD_free(void* ptr, ZSTD_customMem customMem);
  175. MEM_STATIC U32 ZSTD_highbit32(U32 val) /* compress, dictBuilder, decodeCorpus */
  176. {
  177. assert(val != 0);
  178. {
  179. # if defined(_MSC_VER) /* Visual */
  180. unsigned long r=0;
  181. _BitScanReverse(&r, val);
  182. return (unsigned)r;
  183. # elif defined(__GNUC__) && (__GNUC__ >= 3) /* GCC Intrinsic */
  184. return 31 - __builtin_clz(val);
  185. # else /* Software version */
  186. static const U32 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 };
  187. U32 v = val;
  188. v |= v >> 1;
  189. v |= v >> 2;
  190. v |= v >> 4;
  191. v |= v >> 8;
  192. v |= v >> 16;
  193. return DeBruijnClz[(v * 0x07C4ACDDU) >> 27];
  194. # endif
  195. }
  196. }
  197. /* ZSTD_invalidateRepCodes() :
  198. * ensures next compression will not use repcodes from previous block.
  199. * Note : only works with regular variant;
  200. * do not use with extDict variant ! */
  201. void ZSTD_invalidateRepCodes(ZSTD_CCtx* cctx); /* zstdmt, adaptive_compression (shouldn't get this definition from here) */
  202. typedef struct {
  203. blockType_e blockType;
  204. U32 lastBlock;
  205. U32 origSize;
  206. } blockProperties_t;
  207. /*! ZSTD_getcBlockSize() :
  208. * Provides the size of compressed block from block header `src` */
  209. /* Used by: decompress, fullbench (does not get its definition from here) */
  210. size_t ZSTD_getcBlockSize(const void* src, size_t srcSize,
  211. blockProperties_t* bpPtr);
  212. #if defined (__cplusplus)
  213. }
  214. #endif
  215. #endif /* ZSTD_CCOMMON_H_MODULE */