zstd_internal.h 9.3 KB

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