zstd_internal.h 10 KB

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