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_internal.h 13KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405
  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. /*-*************************************
  13. * Dependencies
  14. ***************************************/
  15. #include "compiler.h"
  16. #include "mem.h"
  17. #include "error_private.h"
  18. #ifndef ZSTD_STATIC_LINKING_ONLY
  19. #define ZSTD_STATIC_LINKING_ONLY
  20. #endif
  21. #include "zstd.h"
  22. #define FSE_STATIC_LINKING_ONLY
  23. #include "fse.h"
  24. #define HUF_STATIC_LINKING_ONLY
  25. #include "huf.h"
  26. #ifndef XXH_STATIC_LINKING_ONLY
  27. # define XXH_STATIC_LINKING_ONLY /* XXH64_state_t */
  28. #endif
  29. #include "xxhash.h" /* XXH_reset, update, digest */
  30. #if defined (__cplusplus)
  31. extern "C" {
  32. #endif
  33. /*-*************************************
  34. * Debug
  35. ***************************************/
  36. #if defined(ZSTD_DEBUG) && (ZSTD_DEBUG>=1)
  37. # include <assert.h>
  38. #else
  39. # ifndef assert
  40. # define assert(condition) ((void)0)
  41. # endif
  42. #endif
  43. #define ZSTD_STATIC_ASSERT(c) { enum { ZSTD_static_assert = 1/(int)(!!(c)) }; }
  44. #if defined(ZSTD_DEBUG) && (ZSTD_DEBUG>=2)
  45. # include <stdio.h>
  46. /* recommended values for ZSTD_DEBUG display levels :
  47. * 1 : no display, enables assert() only
  48. * 2 : reserved for currently active debugging path
  49. * 3 : events once per object lifetime (CCtx, CDict)
  50. * 4 : events once per frame
  51. * 5 : events once per block
  52. * 6 : events once per sequence (*very* verbose) */
  53. # define DEBUGLOG(l, ...) { \
  54. if (l<=ZSTD_DEBUG) { \
  55. fprintf(stderr, __FILE__ ": "); \
  56. fprintf(stderr, __VA_ARGS__); \
  57. fprintf(stderr, " \n"); \
  58. } }
  59. #else
  60. # define DEBUGLOG(l, ...) {} /* disabled */
  61. #endif
  62. /*-*************************************
  63. * shared macros
  64. ***************************************/
  65. #undef MIN
  66. #undef MAX
  67. #define MIN(a,b) ((a)<(b) ? (a) : (b))
  68. #define MAX(a,b) ((a)>(b) ? (a) : (b))
  69. #define CHECK_F(f) { size_t const errcod = f; if (ERR_isError(errcod)) return errcod; } /* check and Forward error code */
  70. #define CHECK_E(f, e) { size_t const errcod = f; if (ERR_isError(errcod)) return ERROR(e); } /* check and send Error code */
  71. /*-*************************************
  72. * Common constants
  73. ***************************************/
  74. #define ZSTD_OPT_NUM (1<<12)
  75. #define ZSTD_REP_NUM 3 /* number of repcodes */
  76. #define ZSTD_REP_CHECK (ZSTD_REP_NUM) /* number of repcodes to check by the optimal parser */
  77. #define ZSTD_REP_MOVE (ZSTD_REP_NUM-1)
  78. #define ZSTD_REP_MOVE_OPT (ZSTD_REP_NUM)
  79. static const U32 repStartValue[ZSTD_REP_NUM] = { 1, 4, 8 };
  80. #define KB *(1 <<10)
  81. #define MB *(1 <<20)
  82. #define GB *(1U<<30)
  83. #define BIT7 128
  84. #define BIT6 64
  85. #define BIT5 32
  86. #define BIT4 16
  87. #define BIT1 2
  88. #define BIT0 1
  89. #define ZSTD_WINDOWLOG_ABSOLUTEMIN 10
  90. static const size_t ZSTD_fcs_fieldSize[4] = { 0, 2, 4, 8 };
  91. static const size_t ZSTD_did_fieldSize[4] = { 0, 1, 2, 4 };
  92. #define ZSTD_BLOCKHEADERSIZE 3 /* C standard doesn't allow `static const` variable to be init using another `static const` variable */
  93. static const size_t ZSTD_blockHeaderSize = ZSTD_BLOCKHEADERSIZE;
  94. typedef enum { bt_raw, bt_rle, bt_compressed, bt_reserved } blockType_e;
  95. #define MIN_SEQUENCES_SIZE 1 /* nbSeq==0 */
  96. #define MIN_CBLOCK_SIZE (1 /*litCSize*/ + 1 /* RLE or RAW */ + MIN_SEQUENCES_SIZE /* nbSeq==0 */) /* for a non-null block */
  97. #define HufLog 12
  98. typedef enum { set_basic, set_rle, set_compressed, set_repeat } symbolEncodingType_e;
  99. #define LONGNBSEQ 0x7F00
  100. #define MINMATCH 3
  101. #define Litbits 8
  102. #define MaxLit ((1<<Litbits) - 1)
  103. #define MaxML 52
  104. #define MaxLL 35
  105. #define MaxOff 28
  106. #define MaxSeq MAX(MaxLL, MaxML) /* Assumption : MaxOff < MaxLL,MaxML */
  107. #define MLFSELog 9
  108. #define LLFSELog 9
  109. #define OffFSELog 8
  110. static const U32 LL_bits[MaxLL+1] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  111. 1, 1, 1, 1, 2, 2, 3, 3, 4, 6, 7, 8, 9,10,11,12,
  112. 13,14,15,16 };
  113. static const S16 LL_defaultNorm[MaxLL+1] = { 4, 3, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 1, 1, 1,
  114. 2, 2, 2, 2, 2, 2, 2, 2, 2, 3, 2, 1, 1, 1, 1, 1,
  115. -1,-1,-1,-1 };
  116. #define LL_DEFAULTNORMLOG 6 /* for static allocation */
  117. static const U32 LL_defaultNormLog = LL_DEFAULTNORMLOG;
  118. static const U32 ML_bits[MaxML+1] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  119. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  120. 1, 1, 1, 1, 2, 2, 3, 3, 4, 4, 5, 7, 8, 9,10,11,
  121. 12,13,14,15,16 };
  122. static const S16 ML_defaultNorm[MaxML+1] = { 1, 4, 3, 2, 2, 2, 2, 2, 2, 1, 1, 1, 1, 1, 1, 1,
  123. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  124. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,-1,-1,
  125. -1,-1,-1,-1,-1 };
  126. #define ML_DEFAULTNORMLOG 6 /* for static allocation */
  127. static const U32 ML_defaultNormLog = ML_DEFAULTNORMLOG;
  128. static const S16 OF_defaultNorm[MaxOff+1] = { 1, 1, 1, 1, 1, 1, 2, 2, 2, 1, 1, 1, 1, 1, 1, 1,
  129. 1, 1, 1, 1, 1, 1, 1, 1,-1,-1,-1,-1,-1 };
  130. #define OF_DEFAULTNORMLOG 5 /* for static allocation */
  131. static const U32 OF_defaultNormLog = OF_DEFAULTNORMLOG;
  132. /*-*******************************************
  133. * Shared functions to include for inlining
  134. *********************************************/
  135. static void ZSTD_copy8(void* dst, const void* src) { memcpy(dst, src, 8); }
  136. #define COPY8(d,s) { ZSTD_copy8(d,s); d+=8; s+=8; }
  137. /*! ZSTD_wildcopy() :
  138. * custom version of memcpy(), can copy up to 7 bytes too many (8 bytes if length==0) */
  139. #define WILDCOPY_OVERLENGTH 8
  140. MEM_STATIC void ZSTD_wildcopy(void* dst, const void* src, ptrdiff_t length)
  141. {
  142. const BYTE* ip = (const BYTE*)src;
  143. BYTE* op = (BYTE*)dst;
  144. BYTE* const oend = op + length;
  145. do
  146. COPY8(op, ip)
  147. while (op < oend);
  148. }
  149. 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 */
  150. {
  151. const BYTE* ip = (const BYTE*)src;
  152. BYTE* op = (BYTE*)dst;
  153. BYTE* const oend = (BYTE*)dstEnd;
  154. do
  155. COPY8(op, ip)
  156. while (op < oend);
  157. }
  158. /*-*******************************************
  159. * Private interfaces
  160. *********************************************/
  161. typedef struct ZSTD_stats_s ZSTD_stats_t;
  162. typedef struct seqDef_s {
  163. U32 offset;
  164. U16 litLength;
  165. U16 matchLength;
  166. } seqDef;
  167. typedef struct {
  168. seqDef* sequencesStart;
  169. seqDef* sequences;
  170. BYTE* litStart;
  171. BYTE* lit;
  172. BYTE* llCode;
  173. BYTE* mlCode;
  174. BYTE* ofCode;
  175. U32 longLengthID; /* 0 == no longLength; 1 == Lit.longLength; 2 == Match.longLength; */
  176. U32 longLengthPos;
  177. U32 rep[ZSTD_REP_NUM];
  178. U32 repToConfirm[ZSTD_REP_NUM];
  179. } seqStore_t;
  180. typedef struct {
  181. U32 off;
  182. U32 len;
  183. } ZSTD_match_t;
  184. typedef struct {
  185. U32 price;
  186. U32 off;
  187. U32 mlen;
  188. U32 litlen;
  189. U32 rep[ZSTD_REP_NUM];
  190. } ZSTD_optimal_t;
  191. typedef struct {
  192. U32* litFreq;
  193. U32* litLengthFreq;
  194. U32* matchLengthFreq;
  195. U32* offCodeFreq;
  196. ZSTD_match_t* matchTable;
  197. ZSTD_optimal_t* priceTable;
  198. U32 matchLengthSum;
  199. U32 matchSum;
  200. U32 litLengthSum;
  201. U32 litSum;
  202. U32 offCodeSum;
  203. U32 log2matchLengthSum;
  204. U32 log2matchSum;
  205. U32 log2litLengthSum;
  206. U32 log2litSum;
  207. U32 log2offCodeSum;
  208. U32 factor;
  209. U32 staticPrices;
  210. U32 cachedPrice;
  211. U32 cachedLitLength;
  212. const BYTE* cachedLiterals;
  213. } optState_t;
  214. typedef struct {
  215. U32 offset;
  216. U32 checksum;
  217. } ldmEntry_t;
  218. typedef struct {
  219. ldmEntry_t* hashTable;
  220. BYTE* bucketOffsets; /* Next position in bucket to insert entry */
  221. U64 hashPower; /* Used to compute the rolling hash.
  222. * Depends on ldmParams.minMatchLength */
  223. } ldmState_t;
  224. typedef struct {
  225. U32 enableLdm; /* 1 if enable long distance matching */
  226. U32 hashLog; /* Log size of hashTable */
  227. U32 bucketSizeLog; /* Log bucket size for collision resolution, at most 8 */
  228. U32 minMatchLength; /* Minimum match length */
  229. U32 hashEveryLog; /* Log number of entries to skip */
  230. } ldmParams_t;
  231. typedef struct {
  232. U32 hufCTable[HUF_CTABLE_SIZE_U32(255)];
  233. FSE_CTable offcodeCTable[FSE_CTABLE_SIZE_U32(OffFSELog, MaxOff)];
  234. FSE_CTable matchlengthCTable[FSE_CTABLE_SIZE_U32(MLFSELog, MaxML)];
  235. FSE_CTable litlengthCTable[FSE_CTABLE_SIZE_U32(LLFSELog, MaxLL)];
  236. U32 workspace[HUF_WORKSPACE_SIZE_U32];
  237. HUF_repeat hufCTable_repeatMode;
  238. FSE_repeat offcode_repeatMode;
  239. FSE_repeat matchlength_repeatMode;
  240. FSE_repeat litlength_repeatMode;
  241. } ZSTD_entropyCTables_t;
  242. struct ZSTD_CCtx_params_s {
  243. ZSTD_compressionParameters cParams;
  244. ZSTD_frameParameters fParams;
  245. int compressionLevel;
  246. U32 forceWindow; /* force back-references to respect limit of
  247. * 1<<wLog, even for dictionary */
  248. /* Multithreading: used to pass parameters to mtctx */
  249. U32 nbThreads;
  250. unsigned jobSize;
  251. unsigned overlapSizeLog;
  252. /* Long distance matching parameters */
  253. ldmParams_t ldmParams;
  254. /* For use with createCCtxParams() and freeCCtxParams() only */
  255. ZSTD_customMem customMem;
  256. }; /* typedef'd to ZSTD_CCtx_params within "zstd.h" */
  257. const seqStore_t* ZSTD_getSeqStore(const ZSTD_CCtx* ctx);
  258. void ZSTD_seqToCodes(const seqStore_t* seqStorePtr);
  259. /* custom memory allocation functions */
  260. void* ZSTD_malloc(size_t size, ZSTD_customMem customMem);
  261. void* ZSTD_calloc(size_t size, ZSTD_customMem customMem);
  262. void ZSTD_free(void* ptr, ZSTD_customMem customMem);
  263. /*====== common function ======*/
  264. MEM_STATIC U32 ZSTD_highbit32(U32 val)
  265. {
  266. assert(val != 0);
  267. {
  268. # if defined(_MSC_VER) /* Visual */
  269. unsigned long r=0;
  270. _BitScanReverse(&r, val);
  271. return (unsigned)r;
  272. # elif defined(__GNUC__) && (__GNUC__ >= 3) /* GCC Intrinsic */
  273. return 31 - __builtin_clz(val);
  274. # else /* Software version */
  275. 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 };
  276. U32 v = val;
  277. int r;
  278. v |= v >> 1;
  279. v |= v >> 2;
  280. v |= v >> 4;
  281. v |= v >> 8;
  282. v |= v >> 16;
  283. r = DeBruijnClz[(U32)(v * 0x07C4ACDDU) >> 27];
  284. return r;
  285. # endif
  286. }
  287. }
  288. /* hidden functions */
  289. /* ZSTD_invalidateRepCodes() :
  290. * ensures next compression will not use repcodes from previous block.
  291. * Note : only works with regular variant;
  292. * do not use with extDict variant ! */
  293. void ZSTD_invalidateRepCodes(ZSTD_CCtx* cctx);
  294. /*! ZSTD_initCStream_internal() :
  295. * Private use only. Init streaming operation.
  296. * expects params to be valid.
  297. * must receive dict, or cdict, or none, but not both.
  298. * @return : 0, or an error code */
  299. size_t ZSTD_initCStream_internal(ZSTD_CStream* zcs,
  300. const void* dict, size_t dictSize,
  301. const ZSTD_CDict* cdict,
  302. ZSTD_CCtx_params params, unsigned long long pledgedSrcSize);
  303. /*! ZSTD_compressStream_generic() :
  304. * Private use only. To be called from zstdmt_compress.c in single-thread mode. */
  305. size_t ZSTD_compressStream_generic(ZSTD_CStream* zcs,
  306. ZSTD_outBuffer* output,
  307. ZSTD_inBuffer* input,
  308. ZSTD_EndDirective const flushMode);
  309. /*! ZSTD_getCParamsFromCDict() :
  310. * as the name implies */
  311. ZSTD_compressionParameters ZSTD_getCParamsFromCDict(const ZSTD_CDict* cdict);
  312. /* ZSTD_compressBegin_advanced_internal() :
  313. * Private use only. To be called from zstdmt_compress.c. */
  314. size_t ZSTD_compressBegin_advanced_internal(ZSTD_CCtx* cctx,
  315. const void* dict, size_t dictSize,
  316. ZSTD_dictMode_e dictMode,
  317. ZSTD_CCtx_params params,
  318. unsigned long long pledgedSrcSize);
  319. /* ZSTD_compress_advanced_internal() :
  320. * Private use only. To be called from zstdmt_compress.c. */
  321. size_t ZSTD_compress_advanced_internal(ZSTD_CCtx* cctx,
  322. void* dst, size_t dstCapacity,
  323. const void* src, size_t srcSize,
  324. const void* dict,size_t dictSize,
  325. ZSTD_CCtx_params params);
  326. typedef struct {
  327. blockType_e blockType;
  328. U32 lastBlock;
  329. U32 origSize;
  330. } blockProperties_t;
  331. /*! ZSTD_getcBlockSize() :
  332. * Provides the size of compressed block from block header `src` */
  333. size_t ZSTD_getcBlockSize(const void* src, size_t srcSize,
  334. blockProperties_t* bpPtr);
  335. #if defined (__cplusplus)
  336. }
  337. #endif
  338. #endif /* ZSTD_CCOMMON_H_MODULE */