zstd_internal.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490
  1. /*
  2. * Copyright (c) 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. #if !defined(ZSTD_NO_INTRINSICS) && defined(__ARM_NEON)
  20. #include <arm_neon.h>
  21. #endif
  22. #include "compiler.h"
  23. #include "mem.h"
  24. #include "debug.h" /* assert, DEBUGLOG, RAWLOG, g_debuglevel */
  25. #include "error_private.h"
  26. #define ZSTD_STATIC_LINKING_ONLY
  27. #include "../zstd.h"
  28. #define FSE_STATIC_LINKING_ONLY
  29. #include "fse.h"
  30. #define HUF_STATIC_LINKING_ONLY
  31. #include "huf.h"
  32. #ifndef XXH_STATIC_LINKING_ONLY
  33. # define XXH_STATIC_LINKING_ONLY /* XXH64_state_t */
  34. #endif
  35. #include "xxhash.h" /* XXH_reset, update, digest */
  36. #ifndef ZSTD_NO_TRACE
  37. # include "zstd_trace.h"
  38. #else
  39. # define ZSTD_TRACE 0
  40. #endif
  41. #if defined (__cplusplus)
  42. extern "C" {
  43. #endif
  44. /* ---- static assert (debug) --- */
  45. #define ZSTD_STATIC_ASSERT(c) DEBUG_STATIC_ASSERT(c)
  46. #define ZSTD_isError ERR_isError /* for inlining */
  47. #define FSE_isError ERR_isError
  48. #define HUF_isError ERR_isError
  49. /*-*************************************
  50. * shared macros
  51. ***************************************/
  52. #undef MIN
  53. #undef MAX
  54. #define MIN(a,b) ((a)<(b) ? (a) : (b))
  55. #define MAX(a,b) ((a)>(b) ? (a) : (b))
  56. /**
  57. * Ignore: this is an internal helper.
  58. *
  59. * This is a helper function to help force C99-correctness during compilation.
  60. * Under strict compilation modes, variadic macro arguments can't be empty.
  61. * However, variadic function arguments can be. Using a function therefore lets
  62. * us statically check that at least one (string) argument was passed,
  63. * independent of the compilation flags.
  64. */
  65. static INLINE_KEYWORD UNUSED_ATTR
  66. void _force_has_format_string(const char *format, ...) {
  67. (void)format;
  68. }
  69. /**
  70. * Ignore: this is an internal helper.
  71. *
  72. * We want to force this function invocation to be syntactically correct, but
  73. * we don't want to force runtime evaluation of its arguments.
  74. */
  75. #define _FORCE_HAS_FORMAT_STRING(...) \
  76. if (0) { \
  77. _force_has_format_string(__VA_ARGS__); \
  78. }
  79. /**
  80. * Return the specified error if the condition evaluates to true.
  81. *
  82. * In debug modes, prints additional information.
  83. * In order to do that (particularly, printing the conditional that failed),
  84. * this can't just wrap RETURN_ERROR().
  85. */
  86. #define RETURN_ERROR_IF(cond, err, ...) \
  87. if (cond) { \
  88. RAWLOG(3, "%s:%d: ERROR!: check %s failed, returning %s", \
  89. __FILE__, __LINE__, ZSTD_QUOTE(cond), ZSTD_QUOTE(ERROR(err))); \
  90. _FORCE_HAS_FORMAT_STRING(__VA_ARGS__); \
  91. RAWLOG(3, ": " __VA_ARGS__); \
  92. RAWLOG(3, "\n"); \
  93. return ERROR(err); \
  94. }
  95. /**
  96. * Unconditionally return the specified error.
  97. *
  98. * In debug modes, prints additional information.
  99. */
  100. #define RETURN_ERROR(err, ...) \
  101. do { \
  102. RAWLOG(3, "%s:%d: ERROR!: unconditional check failed, returning %s", \
  103. __FILE__, __LINE__, ZSTD_QUOTE(ERROR(err))); \
  104. _FORCE_HAS_FORMAT_STRING(__VA_ARGS__); \
  105. RAWLOG(3, ": " __VA_ARGS__); \
  106. RAWLOG(3, "\n"); \
  107. return ERROR(err); \
  108. } while(0);
  109. /**
  110. * If the provided expression evaluates to an error code, returns that error code.
  111. *
  112. * In debug modes, prints additional information.
  113. */
  114. #define FORWARD_IF_ERROR(err, ...) \
  115. do { \
  116. size_t const err_code = (err); \
  117. if (ERR_isError(err_code)) { \
  118. RAWLOG(3, "%s:%d: ERROR!: forwarding error in %s: %s", \
  119. __FILE__, __LINE__, ZSTD_QUOTE(err), ERR_getErrorName(err_code)); \
  120. _FORCE_HAS_FORMAT_STRING(__VA_ARGS__); \
  121. RAWLOG(3, ": " __VA_ARGS__); \
  122. RAWLOG(3, "\n"); \
  123. return err_code; \
  124. } \
  125. } while(0);
  126. /*-*************************************
  127. * Common constants
  128. ***************************************/
  129. #define ZSTD_OPT_NUM (1<<12)
  130. #define ZSTD_REP_NUM 3 /* number of repcodes */
  131. #define ZSTD_REP_MOVE (ZSTD_REP_NUM-1)
  132. static UNUSED_ATTR const U32 repStartValue[ZSTD_REP_NUM] = { 1, 4, 8 };
  133. #define KB *(1 <<10)
  134. #define MB *(1 <<20)
  135. #define GB *(1U<<30)
  136. #define BIT7 128
  137. #define BIT6 64
  138. #define BIT5 32
  139. #define BIT4 16
  140. #define BIT1 2
  141. #define BIT0 1
  142. #define ZSTD_WINDOWLOG_ABSOLUTEMIN 10
  143. static UNUSED_ATTR const size_t ZSTD_fcs_fieldSize[4] = { 0, 2, 4, 8 };
  144. static UNUSED_ATTR const size_t ZSTD_did_fieldSize[4] = { 0, 1, 2, 4 };
  145. #define ZSTD_FRAMEIDSIZE 4 /* magic number size */
  146. #define ZSTD_BLOCKHEADERSIZE 3 /* C standard doesn't allow `static const` variable to be init using another `static const` variable */
  147. static UNUSED_ATTR const size_t ZSTD_blockHeaderSize = ZSTD_BLOCKHEADERSIZE;
  148. typedef enum { bt_raw, bt_rle, bt_compressed, bt_reserved } blockType_e;
  149. #define ZSTD_FRAMECHECKSUMSIZE 4
  150. #define MIN_SEQUENCES_SIZE 1 /* nbSeq==0 */
  151. #define MIN_CBLOCK_SIZE (1 /*litCSize*/ + 1 /* RLE or RAW */ + MIN_SEQUENCES_SIZE /* nbSeq==0 */) /* for a non-null block */
  152. #define HufLog 12
  153. typedef enum { set_basic, set_rle, set_compressed, set_repeat } symbolEncodingType_e;
  154. #define LONGNBSEQ 0x7F00
  155. #define MINMATCH 3
  156. #define Litbits 8
  157. #define MaxLit ((1<<Litbits) - 1)
  158. #define MaxML 52
  159. #define MaxLL 35
  160. #define DefaultMaxOff 28
  161. #define MaxOff 31
  162. #define MaxSeq MAX(MaxLL, MaxML) /* Assumption : MaxOff < MaxLL,MaxML */
  163. #define MLFSELog 9
  164. #define LLFSELog 9
  165. #define OffFSELog 8
  166. #define MaxFSELog MAX(MAX(MLFSELog, LLFSELog), OffFSELog)
  167. #define ZSTD_MAX_HUF_HEADER_SIZE 128 /* header + <= 127 byte tree description */
  168. /* Each table cannot take more than #symbols * FSELog bits */
  169. #define ZSTD_MAX_FSE_HEADERS_SIZE (((MaxML + 1) * MLFSELog + (MaxLL + 1) * LLFSELog + (MaxOff + 1) * OffFSELog + 7) / 8)
  170. static UNUSED_ATTR const U32 LL_bits[MaxLL+1] = {
  171. 0, 0, 0, 0, 0, 0, 0, 0,
  172. 0, 0, 0, 0, 0, 0, 0, 0,
  173. 1, 1, 1, 1, 2, 2, 3, 3,
  174. 4, 6, 7, 8, 9,10,11,12,
  175. 13,14,15,16
  176. };
  177. static UNUSED_ATTR const S16 LL_defaultNorm[MaxLL+1] = {
  178. 4, 3, 2, 2, 2, 2, 2, 2,
  179. 2, 2, 2, 2, 2, 1, 1, 1,
  180. 2, 2, 2, 2, 2, 2, 2, 2,
  181. 2, 3, 2, 1, 1, 1, 1, 1,
  182. -1,-1,-1,-1
  183. };
  184. #define LL_DEFAULTNORMLOG 6 /* for static allocation */
  185. static UNUSED_ATTR const U32 LL_defaultNormLog = LL_DEFAULTNORMLOG;
  186. static UNUSED_ATTR const U32 ML_bits[MaxML+1] = {
  187. 0, 0, 0, 0, 0, 0, 0, 0,
  188. 0, 0, 0, 0, 0, 0, 0, 0,
  189. 0, 0, 0, 0, 0, 0, 0, 0,
  190. 0, 0, 0, 0, 0, 0, 0, 0,
  191. 1, 1, 1, 1, 2, 2, 3, 3,
  192. 4, 4, 5, 7, 8, 9,10,11,
  193. 12,13,14,15,16
  194. };
  195. static UNUSED_ATTR const S16 ML_defaultNorm[MaxML+1] = {
  196. 1, 4, 3, 2, 2, 2, 2, 2,
  197. 2, 1, 1, 1, 1, 1, 1, 1,
  198. 1, 1, 1, 1, 1, 1, 1, 1,
  199. 1, 1, 1, 1, 1, 1, 1, 1,
  200. 1, 1, 1, 1, 1, 1, 1, 1,
  201. 1, 1, 1, 1, 1, 1,-1,-1,
  202. -1,-1,-1,-1,-1
  203. };
  204. #define ML_DEFAULTNORMLOG 6 /* for static allocation */
  205. static UNUSED_ATTR const U32 ML_defaultNormLog = ML_DEFAULTNORMLOG;
  206. static UNUSED_ATTR const S16 OF_defaultNorm[DefaultMaxOff+1] = {
  207. 1, 1, 1, 1, 1, 1, 2, 2,
  208. 2, 1, 1, 1, 1, 1, 1, 1,
  209. 1, 1, 1, 1, 1, 1, 1, 1,
  210. -1,-1,-1,-1,-1
  211. };
  212. #define OF_DEFAULTNORMLOG 5 /* for static allocation */
  213. static UNUSED_ATTR const U32 OF_defaultNormLog = OF_DEFAULTNORMLOG;
  214. /*-*******************************************
  215. * Shared functions to include for inlining
  216. *********************************************/
  217. static void ZSTD_copy8(void* dst, const void* src) {
  218. #if !defined(ZSTD_NO_INTRINSICS) && defined(__ARM_NEON)
  219. vst1_u8((uint8_t*)dst, vld1_u8((const uint8_t*)src));
  220. #else
  221. ZSTD_memcpy(dst, src, 8);
  222. #endif
  223. }
  224. #define COPY8(d,s) { ZSTD_copy8(d,s); d+=8; s+=8; }
  225. static void ZSTD_copy16(void* dst, const void* src) {
  226. #if !defined(ZSTD_NO_INTRINSICS) && defined(__ARM_NEON)
  227. vst1q_u8((uint8_t*)dst, vld1q_u8((const uint8_t*)src));
  228. #else
  229. ZSTD_memcpy(dst, src, 16);
  230. #endif
  231. }
  232. #define COPY16(d,s) { ZSTD_copy16(d,s); d+=16; s+=16; }
  233. #define WILDCOPY_OVERLENGTH 32
  234. #define WILDCOPY_VECLEN 16
  235. typedef enum {
  236. ZSTD_no_overlap,
  237. ZSTD_overlap_src_before_dst
  238. /* ZSTD_overlap_dst_before_src, */
  239. } ZSTD_overlap_e;
  240. /*! ZSTD_wildcopy() :
  241. * Custom version of ZSTD_memcpy(), can over read/write up to WILDCOPY_OVERLENGTH bytes (if length==0)
  242. * @param ovtype controls the overlap detection
  243. * - ZSTD_no_overlap: The source and destination are guaranteed to be at least WILDCOPY_VECLEN bytes apart.
  244. * - ZSTD_overlap_src_before_dst: The src and dst may overlap, but they MUST be at least 8 bytes apart.
  245. * The src buffer must be before the dst buffer.
  246. */
  247. MEM_STATIC FORCE_INLINE_ATTR
  248. void ZSTD_wildcopy(void* dst, const void* src, ptrdiff_t length, ZSTD_overlap_e const ovtype)
  249. {
  250. ptrdiff_t diff = (BYTE*)dst - (const BYTE*)src;
  251. const BYTE* ip = (const BYTE*)src;
  252. BYTE* op = (BYTE*)dst;
  253. BYTE* const oend = op + length;
  254. assert(diff >= 8 || (ovtype == ZSTD_no_overlap && diff <= -WILDCOPY_VECLEN));
  255. if (ovtype == ZSTD_overlap_src_before_dst && diff < WILDCOPY_VECLEN) {
  256. /* Handle short offset copies. */
  257. do {
  258. COPY8(op, ip)
  259. } while (op < oend);
  260. } else {
  261. assert(diff >= WILDCOPY_VECLEN || diff <= -WILDCOPY_VECLEN);
  262. /* Separate out the first COPY16() call because the copy length is
  263. * almost certain to be short, so the branches have different
  264. * probabilities. Since it is almost certain to be short, only do
  265. * one COPY16() in the first call. Then, do two calls per loop since
  266. * at that point it is more likely to have a high trip count.
  267. */
  268. #ifdef __aarch64__
  269. do {
  270. COPY16(op, ip);
  271. }
  272. while (op < oend);
  273. #else
  274. ZSTD_copy16(op, ip);
  275. if (16 >= length) return;
  276. op += 16;
  277. ip += 16;
  278. do {
  279. COPY16(op, ip);
  280. COPY16(op, ip);
  281. }
  282. while (op < oend);
  283. #endif
  284. }
  285. }
  286. MEM_STATIC size_t ZSTD_limitCopy(void* dst, size_t dstCapacity, const void* src, size_t srcSize)
  287. {
  288. size_t const length = MIN(dstCapacity, srcSize);
  289. if (length > 0) {
  290. ZSTD_memcpy(dst, src, length);
  291. }
  292. return length;
  293. }
  294. /* define "workspace is too large" as this number of times larger than needed */
  295. #define ZSTD_WORKSPACETOOLARGE_FACTOR 3
  296. /* when workspace is continuously too large
  297. * during at least this number of times,
  298. * context's memory usage is considered wasteful,
  299. * because it's sized to handle a worst case scenario which rarely happens.
  300. * In which case, resize it down to free some memory */
  301. #define ZSTD_WORKSPACETOOLARGE_MAXDURATION 128
  302. /* Controls whether the input/output buffer is buffered or stable. */
  303. typedef enum {
  304. ZSTD_bm_buffered = 0, /* Buffer the input/output */
  305. ZSTD_bm_stable = 1 /* ZSTD_inBuffer/ZSTD_outBuffer is stable */
  306. } ZSTD_bufferMode_e;
  307. /*-*******************************************
  308. * Private declarations
  309. *********************************************/
  310. typedef struct seqDef_s {
  311. U32 offset; /* offset == rawOffset + ZSTD_REP_NUM, or equivalently, offCode + 1 */
  312. U16 litLength;
  313. U16 matchLength;
  314. } seqDef;
  315. /* Controls whether seqStore has a single "long" litLength or matchLength. See seqStore_t. */
  316. typedef enum {
  317. ZSTD_llt_none = 0, /* no longLengthType */
  318. ZSTD_llt_literalLength = 1, /* represents a long literal */
  319. ZSTD_llt_matchLength = 2 /* represents a long match */
  320. } ZSTD_longLengthType_e;
  321. typedef struct {
  322. seqDef* sequencesStart;
  323. seqDef* sequences; /* ptr to end of sequences */
  324. BYTE* litStart;
  325. BYTE* lit; /* ptr to end of literals */
  326. BYTE* llCode;
  327. BYTE* mlCode;
  328. BYTE* ofCode;
  329. size_t maxNbSeq;
  330. size_t maxNbLit;
  331. /* longLengthPos and longLengthType to allow us to represent either a single litLength or matchLength
  332. * in the seqStore that has a value larger than U16 (if it exists). To do so, we increment
  333. * the existing value of the litLength or matchLength by 0x10000.
  334. */
  335. ZSTD_longLengthType_e longLengthType;
  336. U32 longLengthPos; /* Index of the sequence to apply long length modification to */
  337. } seqStore_t;
  338. typedef struct {
  339. U32 litLength;
  340. U32 matchLength;
  341. } ZSTD_sequenceLength;
  342. /**
  343. * Returns the ZSTD_sequenceLength for the given sequences. It handles the decoding of long sequences
  344. * indicated by longLengthPos and longLengthType, and adds MINMATCH back to matchLength.
  345. */
  346. MEM_STATIC ZSTD_sequenceLength ZSTD_getSequenceLength(seqStore_t const* seqStore, seqDef const* seq)
  347. {
  348. ZSTD_sequenceLength seqLen;
  349. seqLen.litLength = seq->litLength;
  350. seqLen.matchLength = seq->matchLength + MINMATCH;
  351. if (seqStore->longLengthPos == (U32)(seq - seqStore->sequencesStart)) {
  352. if (seqStore->longLengthType == ZSTD_llt_literalLength) {
  353. seqLen.litLength += 0xFFFF;
  354. }
  355. if (seqStore->longLengthType == ZSTD_llt_matchLength) {
  356. seqLen.matchLength += 0xFFFF;
  357. }
  358. }
  359. return seqLen;
  360. }
  361. /**
  362. * Contains the compressed frame size and an upper-bound for the decompressed frame size.
  363. * Note: before using `compressedSize`, check for errors using ZSTD_isError().
  364. * similarly, before using `decompressedBound`, check for errors using:
  365. * `decompressedBound != ZSTD_CONTENTSIZE_ERROR`
  366. */
  367. typedef struct {
  368. size_t compressedSize;
  369. unsigned long long decompressedBound;
  370. } ZSTD_frameSizeInfo; /* decompress & legacy */
  371. const seqStore_t* ZSTD_getSeqStore(const ZSTD_CCtx* ctx); /* compress & dictBuilder */
  372. void ZSTD_seqToCodes(const seqStore_t* seqStorePtr); /* compress, dictBuilder, decodeCorpus (shouldn't get its definition from here) */
  373. /* custom memory allocation functions */
  374. void* ZSTD_customMalloc(size_t size, ZSTD_customMem customMem);
  375. void* ZSTD_customCalloc(size_t size, ZSTD_customMem customMem);
  376. void ZSTD_customFree(void* ptr, ZSTD_customMem customMem);
  377. MEM_STATIC U32 ZSTD_highbit32(U32 val) /* compress, dictBuilder, decodeCorpus */
  378. {
  379. assert(val != 0);
  380. {
  381. # if defined(_MSC_VER) /* Visual */
  382. # if STATIC_BMI2 == 1
  383. return _lzcnt_u32(val)^31;
  384. # else
  385. unsigned long r=0;
  386. return _BitScanReverse(&r, val) ? (unsigned)r : 0;
  387. # endif
  388. # elif defined(__GNUC__) && (__GNUC__ >= 3) /* GCC Intrinsic */
  389. return __builtin_clz (val) ^ 31;
  390. # elif defined(__ICCARM__) /* IAR Intrinsic */
  391. return 31 - __CLZ(val);
  392. # else /* Software version */
  393. 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 };
  394. U32 v = val;
  395. v |= v >> 1;
  396. v |= v >> 2;
  397. v |= v >> 4;
  398. v |= v >> 8;
  399. v |= v >> 16;
  400. return DeBruijnClz[(v * 0x07C4ACDDU) >> 27];
  401. # endif
  402. }
  403. }
  404. /* ZSTD_invalidateRepCodes() :
  405. * ensures next compression will not use repcodes from previous block.
  406. * Note : only works with regular variant;
  407. * do not use with extDict variant ! */
  408. void ZSTD_invalidateRepCodes(ZSTD_CCtx* cctx); /* zstdmt, adaptive_compression (shouldn't get this definition from here) */
  409. typedef struct {
  410. blockType_e blockType;
  411. U32 lastBlock;
  412. U32 origSize;
  413. } blockProperties_t; /* declared here for decompress and fullbench */
  414. /*! ZSTD_getcBlockSize() :
  415. * Provides the size of compressed block from block header `src` */
  416. /* Used by: decompress, fullbench (does not get its definition from here) */
  417. size_t ZSTD_getcBlockSize(const void* src, size_t srcSize,
  418. blockProperties_t* bpPtr);
  419. /*! ZSTD_decodeSeqHeaders() :
  420. * decode sequence header from src */
  421. /* Used by: decompress, fullbench (does not get its definition from here) */
  422. size_t ZSTD_decodeSeqHeaders(ZSTD_DCtx* dctx, int* nbSeqPtr,
  423. const void* src, size_t srcSize);
  424. #if defined (__cplusplus)
  425. }
  426. #endif
  427. #endif /* ZSTD_CCOMMON_H_MODULE */