zstd_internal.h 13 KB

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