zstd_internal.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350
  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. /**
  49. * Return the specified error if the condition evaluates to true.
  50. *
  51. * In debug modes, prints additional information.
  52. * In order to do that (particularly, printing the conditional that failed),
  53. * this can't just wrap RETURN_ERROR().
  54. */
  55. #define RETURN_ERROR_IF(cond, err, ...) \
  56. if (cond) { \
  57. RAWLOG(3, "%s:%d: ERROR!: check %s failed, returning %s", __FILE__, __LINE__, ZSTD_QUOTE(cond), ZSTD_QUOTE(ERROR(err))); \
  58. RAWLOG(3, ": " __VA_ARGS__); \
  59. RAWLOG(3, "\n"); \
  60. return ERROR(err); \
  61. }
  62. /**
  63. * Unconditionally return the specified error.
  64. *
  65. * In debug modes, prints additional information.
  66. */
  67. #define RETURN_ERROR(err, ...) \
  68. do { \
  69. RAWLOG(3, "%s:%d: ERROR!: unconditional check failed, returning %s", __FILE__, __LINE__, ZSTD_QUOTE(ERROR(err))); \
  70. RAWLOG(3, ": " __VA_ARGS__); \
  71. RAWLOG(3, "\n"); \
  72. return ERROR(err); \
  73. } while(0);
  74. /**
  75. * If the provided expression evaluates to an error code, returns that error code.
  76. *
  77. * In debug modes, prints additional information.
  78. */
  79. #define FORWARD_IF_ERROR(err, ...) \
  80. do { \
  81. size_t const err_code = (err); \
  82. if (ERR_isError(err_code)) { \
  83. RAWLOG(3, "%s:%d: ERROR!: forwarding error in %s: %s", __FILE__, __LINE__, ZSTD_QUOTE(err), ERR_getErrorName(err_code)); \
  84. RAWLOG(3, ": " __VA_ARGS__); \
  85. RAWLOG(3, "\n"); \
  86. return err_code; \
  87. } \
  88. } while(0);
  89. /*-*************************************
  90. * Common constants
  91. ***************************************/
  92. #define ZSTD_OPT_NUM (1<<12)
  93. #define ZSTD_REP_NUM 3 /* number of repcodes */
  94. #define ZSTD_REP_MOVE (ZSTD_REP_NUM-1)
  95. static const U32 repStartValue[ZSTD_REP_NUM] = { 1, 4, 8 };
  96. #define KB *(1 <<10)
  97. #define MB *(1 <<20)
  98. #define GB *(1U<<30)
  99. #define BIT7 128
  100. #define BIT6 64
  101. #define BIT5 32
  102. #define BIT4 16
  103. #define BIT1 2
  104. #define BIT0 1
  105. #define ZSTD_WINDOWLOG_ABSOLUTEMIN 10
  106. static const size_t ZSTD_fcs_fieldSize[4] = { 0, 2, 4, 8 };
  107. static const size_t ZSTD_did_fieldSize[4] = { 0, 1, 2, 4 };
  108. #define ZSTD_FRAMEIDSIZE 4 /* magic number size */
  109. #define ZSTD_BLOCKHEADERSIZE 3 /* C standard doesn't allow `static const` variable to be init using another `static const` variable */
  110. static const size_t ZSTD_blockHeaderSize = ZSTD_BLOCKHEADERSIZE;
  111. typedef enum { bt_raw, bt_rle, bt_compressed, bt_reserved } blockType_e;
  112. #define MIN_SEQUENCES_SIZE 1 /* nbSeq==0 */
  113. #define MIN_CBLOCK_SIZE (1 /*litCSize*/ + 1 /* RLE or RAW */ + MIN_SEQUENCES_SIZE /* nbSeq==0 */) /* for a non-null block */
  114. #define HufLog 12
  115. typedef enum { set_basic, set_rle, set_compressed, set_repeat } symbolEncodingType_e;
  116. #define LONGNBSEQ 0x7F00
  117. #define MINMATCH 3
  118. #define Litbits 8
  119. #define MaxLit ((1<<Litbits) - 1)
  120. #define MaxML 52
  121. #define MaxLL 35
  122. #define DefaultMaxOff 28
  123. #define MaxOff 31
  124. #define MaxSeq MAX(MaxLL, MaxML) /* Assumption : MaxOff < MaxLL,MaxML */
  125. #define MLFSELog 9
  126. #define LLFSELog 9
  127. #define OffFSELog 8
  128. #define MaxFSELog MAX(MAX(MLFSELog, LLFSELog), OffFSELog)
  129. static const U32 LL_bits[MaxLL+1] = { 0, 0, 0, 0, 0, 0, 0, 0,
  130. 0, 0, 0, 0, 0, 0, 0, 0,
  131. 1, 1, 1, 1, 2, 2, 3, 3,
  132. 4, 6, 7, 8, 9,10,11,12,
  133. 13,14,15,16 };
  134. static const S16 LL_defaultNorm[MaxLL+1] = { 4, 3, 2, 2, 2, 2, 2, 2,
  135. 2, 2, 2, 2, 2, 1, 1, 1,
  136. 2, 2, 2, 2, 2, 2, 2, 2,
  137. 2, 3, 2, 1, 1, 1, 1, 1,
  138. -1,-1,-1,-1 };
  139. #define LL_DEFAULTNORMLOG 6 /* for static allocation */
  140. static const U32 LL_defaultNormLog = LL_DEFAULTNORMLOG;
  141. static const U32 ML_bits[MaxML+1] = { 0, 0, 0, 0, 0, 0, 0, 0,
  142. 0, 0, 0, 0, 0, 0, 0, 0,
  143. 0, 0, 0, 0, 0, 0, 0, 0,
  144. 0, 0, 0, 0, 0, 0, 0, 0,
  145. 1, 1, 1, 1, 2, 2, 3, 3,
  146. 4, 4, 5, 7, 8, 9,10,11,
  147. 12,13,14,15,16 };
  148. static const S16 ML_defaultNorm[MaxML+1] = { 1, 4, 3, 2, 2, 2, 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, 1, 1, 1,
  152. 1, 1, 1, 1, 1, 1, 1, 1,
  153. 1, 1, 1, 1, 1, 1,-1,-1,
  154. -1,-1,-1,-1,-1 };
  155. #define ML_DEFAULTNORMLOG 6 /* for static allocation */
  156. static const U32 ML_defaultNormLog = ML_DEFAULTNORMLOG;
  157. static const S16 OF_defaultNorm[DefaultMaxOff+1] = { 1, 1, 1, 1, 1, 1, 2, 2,
  158. 2, 1, 1, 1, 1, 1, 1, 1,
  159. 1, 1, 1, 1, 1, 1, 1, 1,
  160. -1,-1,-1,-1,-1 };
  161. #define OF_DEFAULTNORMLOG 5 /* for static allocation */
  162. static const U32 OF_defaultNormLog = OF_DEFAULTNORMLOG;
  163. /*-*******************************************
  164. * Shared functions to include for inlining
  165. *********************************************/
  166. static void ZSTD_copy8(void* dst, const void* src) { memcpy(dst, src, 8); }
  167. #define COPY8(d,s) { ZSTD_copy8(d,s); d+=8; s+=8; }
  168. static void ZSTD_copy16(void* dst, const void* src) { memcpy(dst, src, 16); }
  169. #define COPY16(d,s) { ZSTD_copy16(d,s); d+=16; s+=16; }
  170. #define WILDCOPY_OVERLENGTH 32
  171. #define WILDCOPY_VECLEN 16
  172. typedef enum {
  173. ZSTD_no_overlap,
  174. ZSTD_overlap_src_before_dst
  175. /* ZSTD_overlap_dst_before_src, */
  176. } ZSTD_overlap_e;
  177. /*! ZSTD_wildcopy() :
  178. * Custom version of memcpy(), can over read/write up to WILDCOPY_OVERLENGTH bytes (if length==0)
  179. * @param ovtype controls the overlap detection
  180. * - ZSTD_no_overlap: The source and destination are guaranteed to be at least WILDCOPY_VECLEN bytes apart.
  181. * - ZSTD_overlap_src_before_dst: The src and dst may overlap, but they MUST be at least 8 bytes apart.
  182. * The src buffer must be before the dst buffer.
  183. */
  184. MEM_STATIC FORCE_INLINE_ATTR DONT_VECTORIZE
  185. void ZSTD_wildcopy(void* dst, const void* src, ptrdiff_t length, ZSTD_overlap_e const ovtype)
  186. {
  187. ptrdiff_t diff = (BYTE*)dst - (const BYTE*)src;
  188. const BYTE* ip = (const BYTE*)src;
  189. BYTE* op = (BYTE*)dst;
  190. BYTE* const oend = op + length;
  191. assert(diff >= 8 || (ovtype == ZSTD_no_overlap && diff <= -WILDCOPY_VECLEN));
  192. if (ovtype == ZSTD_overlap_src_before_dst && diff < WILDCOPY_VECLEN) {
  193. /* Handle short offset copies. */
  194. do {
  195. COPY8(op, ip)
  196. } while (op < oend);
  197. } else {
  198. assert(diff >= WILDCOPY_VECLEN || diff <= -WILDCOPY_VECLEN);
  199. /* Separate out the first two COPY16() calls because the copy length is
  200. * almost certain to be short, so the branches have different
  201. * probabilities.
  202. * On gcc-9 unrolling once is +1.6%, twice is +2%, thrice is +1.8%.
  203. * On clang-8 unrolling once is +1.4%, twice is +3.3%, thrice is +3%.
  204. */
  205. COPY16(op, ip);
  206. COPY16(op, ip);
  207. if (op >= oend) return;
  208. do {
  209. COPY16(op, ip);
  210. COPY16(op, ip);
  211. }
  212. while (op < oend);
  213. }
  214. }
  215. /*-*******************************************
  216. * Private declarations
  217. *********************************************/
  218. typedef struct seqDef_s {
  219. U32 offset;
  220. U16 litLength;
  221. U16 matchLength;
  222. } seqDef;
  223. typedef struct {
  224. seqDef* sequencesStart;
  225. seqDef* sequences;
  226. BYTE* litStart;
  227. BYTE* lit;
  228. BYTE* llCode;
  229. BYTE* mlCode;
  230. BYTE* ofCode;
  231. size_t maxNbSeq;
  232. size_t maxNbLit;
  233. U32 longLengthID; /* 0 == no longLength; 1 == Lit.longLength; 2 == Match.longLength; */
  234. U32 longLengthPos;
  235. } seqStore_t;
  236. /**
  237. * Contains the compressed frame size and an upper-bound for the decompressed frame size.
  238. * Note: before using `compressedSize`, check for errors using ZSTD_isError().
  239. * similarly, before using `decompressedBound`, check for errors using:
  240. * `decompressedBound != ZSTD_CONTENTSIZE_ERROR`
  241. */
  242. typedef struct {
  243. size_t compressedSize;
  244. unsigned long long decompressedBound;
  245. } ZSTD_frameSizeInfo; /* decompress & legacy */
  246. const seqStore_t* ZSTD_getSeqStore(const ZSTD_CCtx* ctx); /* compress & dictBuilder */
  247. void ZSTD_seqToCodes(const seqStore_t* seqStorePtr); /* compress, dictBuilder, decodeCorpus (shouldn't get its definition from here) */
  248. /* custom memory allocation functions */
  249. void* ZSTD_malloc(size_t size, ZSTD_customMem customMem);
  250. void* ZSTD_calloc(size_t size, ZSTD_customMem customMem);
  251. void ZSTD_free(void* ptr, ZSTD_customMem customMem);
  252. MEM_STATIC U32 ZSTD_highbit32(U32 val) /* compress, dictBuilder, decodeCorpus */
  253. {
  254. assert(val != 0);
  255. {
  256. # if defined(_MSC_VER) /* Visual */
  257. unsigned long r=0;
  258. _BitScanReverse(&r, val);
  259. return (unsigned)r;
  260. # elif defined(__GNUC__) && (__GNUC__ >= 3) /* GCC Intrinsic */
  261. return __builtin_clz (val) ^ 31;
  262. # elif defined(__ICCARM__) /* IAR Intrinsic */
  263. return 31 - __CLZ(val);
  264. # else /* Software version */
  265. 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 };
  266. U32 v = val;
  267. v |= v >> 1;
  268. v |= v >> 2;
  269. v |= v >> 4;
  270. v |= v >> 8;
  271. v |= v >> 16;
  272. return DeBruijnClz[(v * 0x07C4ACDDU) >> 27];
  273. # endif
  274. }
  275. }
  276. /* ZSTD_invalidateRepCodes() :
  277. * ensures next compression will not use repcodes from previous block.
  278. * Note : only works with regular variant;
  279. * do not use with extDict variant ! */
  280. void ZSTD_invalidateRepCodes(ZSTD_CCtx* cctx); /* zstdmt, adaptive_compression (shouldn't get this definition from here) */
  281. typedef struct {
  282. blockType_e blockType;
  283. U32 lastBlock;
  284. U32 origSize;
  285. } blockProperties_t; /* declared here for decompress and fullbench */
  286. /*! ZSTD_getcBlockSize() :
  287. * Provides the size of compressed block from block header `src` */
  288. /* Used by: decompress, fullbench (does not get its definition from here) */
  289. size_t ZSTD_getcBlockSize(const void* src, size_t srcSize,
  290. blockProperties_t* bpPtr);
  291. /*! ZSTD_decodeSeqHeaders() :
  292. * decode sequence header from src */
  293. /* Used by: decompress, fullbench (does not get its definition from here) */
  294. size_t ZSTD_decodeSeqHeaders(ZSTD_DCtx* dctx, int* nbSeqPtr,
  295. const void* src, size_t srcSize);
  296. #if defined (__cplusplus)
  297. }
  298. #endif
  299. #endif /* ZSTD_CCOMMON_H_MODULE */