zstd_compress_internal.h 55 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367
  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. /* This header contains definitions
  11. * that shall **only** be used by modules within lib/compress.
  12. */
  13. #ifndef ZSTD_COMPRESS_H
  14. #define ZSTD_COMPRESS_H
  15. /*-*************************************
  16. * Dependencies
  17. ***************************************/
  18. #include "../common/zstd_internal.h"
  19. #include "zstd_cwksp.h"
  20. #ifdef ZSTD_MULTITHREAD
  21. # include "zstdmt_compress.h"
  22. #endif
  23. #if defined (__cplusplus)
  24. extern "C" {
  25. #endif
  26. /*-*************************************
  27. * Constants
  28. ***************************************/
  29. #define kSearchStrength 8
  30. #define HASH_READ_SIZE 8
  31. #define ZSTD_DUBT_UNSORTED_MARK 1 /* For btlazy2 strategy, index ZSTD_DUBT_UNSORTED_MARK==1 means "unsorted".
  32. It could be confused for a real successor at index "1", if sorted as larger than its predecessor.
  33. It's not a big deal though : candidate will just be sorted again.
  34. Additionally, candidate position 1 will be lost.
  35. But candidate 1 cannot hide a large tree of candidates, so it's a minimal loss.
  36. The benefit is that ZSTD_DUBT_UNSORTED_MARK cannot be mishandled after table re-use with a different strategy.
  37. This constant is required by ZSTD_compressBlock_btlazy2() and ZSTD_reduceTable_internal() */
  38. /*-*************************************
  39. * Context memory management
  40. ***************************************/
  41. typedef enum { ZSTDcs_created=0, ZSTDcs_init, ZSTDcs_ongoing, ZSTDcs_ending } ZSTD_compressionStage_e;
  42. typedef enum { zcss_init=0, zcss_load, zcss_flush } ZSTD_cStreamStage;
  43. typedef struct ZSTD_prefixDict_s {
  44. const void* dict;
  45. size_t dictSize;
  46. ZSTD_dictContentType_e dictContentType;
  47. } ZSTD_prefixDict;
  48. typedef struct {
  49. void* dictBuffer;
  50. void const* dict;
  51. size_t dictSize;
  52. ZSTD_dictContentType_e dictContentType;
  53. ZSTD_CDict* cdict;
  54. } ZSTD_localDict;
  55. typedef struct {
  56. HUF_CElt CTable[HUF_CTABLE_SIZE_U32(255)];
  57. HUF_repeat repeatMode;
  58. } ZSTD_hufCTables_t;
  59. typedef struct {
  60. FSE_CTable offcodeCTable[FSE_CTABLE_SIZE_U32(OffFSELog, MaxOff)];
  61. FSE_CTable matchlengthCTable[FSE_CTABLE_SIZE_U32(MLFSELog, MaxML)];
  62. FSE_CTable litlengthCTable[FSE_CTABLE_SIZE_U32(LLFSELog, MaxLL)];
  63. FSE_repeat offcode_repeatMode;
  64. FSE_repeat matchlength_repeatMode;
  65. FSE_repeat litlength_repeatMode;
  66. } ZSTD_fseCTables_t;
  67. typedef struct {
  68. ZSTD_hufCTables_t huf;
  69. ZSTD_fseCTables_t fse;
  70. } ZSTD_entropyCTables_t;
  71. /***********************************************
  72. * Entropy buffer statistics structs and funcs *
  73. ***********************************************/
  74. /** ZSTD_hufCTablesMetadata_t :
  75. * Stores Literals Block Type for a super-block in hType, and
  76. * huffman tree description in hufDesBuffer.
  77. * hufDesSize refers to the size of huffman tree description in bytes.
  78. * This metadata is populated in ZSTD_buildBlockEntropyStats_literals() */
  79. typedef struct {
  80. symbolEncodingType_e hType;
  81. BYTE hufDesBuffer[ZSTD_MAX_HUF_HEADER_SIZE];
  82. size_t hufDesSize;
  83. } ZSTD_hufCTablesMetadata_t;
  84. /** ZSTD_fseCTablesMetadata_t :
  85. * Stores symbol compression modes for a super-block in {ll, ol, ml}Type, and
  86. * fse tables in fseTablesBuffer.
  87. * fseTablesSize refers to the size of fse tables in bytes.
  88. * This metadata is populated in ZSTD_buildBlockEntropyStats_sequences() */
  89. typedef struct {
  90. symbolEncodingType_e llType;
  91. symbolEncodingType_e ofType;
  92. symbolEncodingType_e mlType;
  93. BYTE fseTablesBuffer[ZSTD_MAX_FSE_HEADERS_SIZE];
  94. size_t fseTablesSize;
  95. size_t lastCountSize; /* This is to account for bug in 1.3.4. More detail in ZSTD_entropyCompressSeqStore_internal() */
  96. } ZSTD_fseCTablesMetadata_t;
  97. typedef struct {
  98. ZSTD_hufCTablesMetadata_t hufMetadata;
  99. ZSTD_fseCTablesMetadata_t fseMetadata;
  100. } ZSTD_entropyCTablesMetadata_t;
  101. /** ZSTD_buildBlockEntropyStats() :
  102. * Builds entropy for the block.
  103. * @return : 0 on success or error code */
  104. size_t ZSTD_buildBlockEntropyStats(seqStore_t* seqStorePtr,
  105. const ZSTD_entropyCTables_t* prevEntropy,
  106. ZSTD_entropyCTables_t* nextEntropy,
  107. const ZSTD_CCtx_params* cctxParams,
  108. ZSTD_entropyCTablesMetadata_t* entropyMetadata,
  109. void* workspace, size_t wkspSize);
  110. /*********************************
  111. * Compression internals structs *
  112. *********************************/
  113. typedef struct {
  114. U32 off; /* Offset code (offset + ZSTD_REP_MOVE) for the match */
  115. U32 len; /* Raw length of match */
  116. } ZSTD_match_t;
  117. typedef struct {
  118. U32 offset; /* Offset of sequence */
  119. U32 litLength; /* Length of literals prior to match */
  120. U32 matchLength; /* Raw length of match */
  121. } rawSeq;
  122. typedef struct {
  123. rawSeq* seq; /* The start of the sequences */
  124. size_t pos; /* The index in seq where reading stopped. pos <= size. */
  125. size_t posInSequence; /* The position within the sequence at seq[pos] where reading
  126. stopped. posInSequence <= seq[pos].litLength + seq[pos].matchLength */
  127. size_t size; /* The number of sequences. <= capacity. */
  128. size_t capacity; /* The capacity starting from `seq` pointer */
  129. } rawSeqStore_t;
  130. UNUSED_ATTR static const rawSeqStore_t kNullRawSeqStore = {NULL, 0, 0, 0, 0};
  131. typedef struct {
  132. int price;
  133. U32 off;
  134. U32 mlen;
  135. U32 litlen;
  136. U32 rep[ZSTD_REP_NUM];
  137. } ZSTD_optimal_t;
  138. typedef enum { zop_dynamic=0, zop_predef } ZSTD_OptPrice_e;
  139. typedef struct {
  140. /* All tables are allocated inside cctx->workspace by ZSTD_resetCCtx_internal() */
  141. unsigned* litFreq; /* table of literals statistics, of size 256 */
  142. unsigned* litLengthFreq; /* table of litLength statistics, of size (MaxLL+1) */
  143. unsigned* matchLengthFreq; /* table of matchLength statistics, of size (MaxML+1) */
  144. unsigned* offCodeFreq; /* table of offCode statistics, of size (MaxOff+1) */
  145. ZSTD_match_t* matchTable; /* list of found matches, of size ZSTD_OPT_NUM+1 */
  146. ZSTD_optimal_t* priceTable; /* All positions tracked by optimal parser, of size ZSTD_OPT_NUM+1 */
  147. U32 litSum; /* nb of literals */
  148. U32 litLengthSum; /* nb of litLength codes */
  149. U32 matchLengthSum; /* nb of matchLength codes */
  150. U32 offCodeSum; /* nb of offset codes */
  151. U32 litSumBasePrice; /* to compare to log2(litfreq) */
  152. U32 litLengthSumBasePrice; /* to compare to log2(llfreq) */
  153. U32 matchLengthSumBasePrice;/* to compare to log2(mlfreq) */
  154. U32 offCodeSumBasePrice; /* to compare to log2(offreq) */
  155. ZSTD_OptPrice_e priceType; /* prices can be determined dynamically, or follow a pre-defined cost structure */
  156. const ZSTD_entropyCTables_t* symbolCosts; /* pre-calculated dictionary statistics */
  157. ZSTD_literalCompressionMode_e literalCompressionMode;
  158. } optState_t;
  159. typedef struct {
  160. ZSTD_entropyCTables_t entropy;
  161. U32 rep[ZSTD_REP_NUM];
  162. } ZSTD_compressedBlockState_t;
  163. typedef struct {
  164. BYTE const* nextSrc; /* next block here to continue on current prefix */
  165. BYTE const* base; /* All regular indexes relative to this position */
  166. BYTE const* dictBase; /* extDict indexes relative to this position */
  167. U32 dictLimit; /* below that point, need extDict */
  168. U32 lowLimit; /* below that point, no more valid data */
  169. U32 nbOverflowCorrections; /* Number of times overflow correction has run since
  170. * ZSTD_window_init(). Useful for debugging coredumps
  171. * and for ZSTD_WINDOW_OVERFLOW_CORRECT_FREQUENTLY.
  172. */
  173. } ZSTD_window_t;
  174. typedef struct ZSTD_matchState_t ZSTD_matchState_t;
  175. #define ZSTD_ROW_HASH_CACHE_SIZE 8 /* Size of prefetching hash cache for row-based matchfinder */
  176. struct ZSTD_matchState_t {
  177. ZSTD_window_t window; /* State for window round buffer management */
  178. U32 loadedDictEnd; /* index of end of dictionary, within context's referential.
  179. * When loadedDictEnd != 0, a dictionary is in use, and still valid.
  180. * This relies on a mechanism to set loadedDictEnd=0 when dictionary is no longer within distance.
  181. * Such mechanism is provided within ZSTD_window_enforceMaxDist() and ZSTD_checkDictValidity().
  182. * When dict referential is copied into active context (i.e. not attached),
  183. * loadedDictEnd == dictSize, since referential starts from zero.
  184. */
  185. U32 nextToUpdate; /* index from which to continue table update */
  186. U32 hashLog3; /* dispatch table for matches of len==3 : larger == faster, more memory */
  187. U32 rowHashLog; /* For row-based matchfinder: Hashlog based on nb of rows in the hashTable.*/
  188. U16* tagTable; /* For row-based matchFinder: A row-based table containing the hashes and head index. */
  189. U32 hashCache[ZSTD_ROW_HASH_CACHE_SIZE]; /* For row-based matchFinder: a cache of hashes to improve speed */
  190. U32* hashTable;
  191. U32* hashTable3;
  192. U32* chainTable;
  193. U32 forceNonContiguous; /* Non-zero if we should force non-contiguous load for the next window update. */
  194. int dedicatedDictSearch; /* Indicates whether this matchState is using the
  195. * dedicated dictionary search structure.
  196. */
  197. optState_t opt; /* optimal parser state */
  198. const ZSTD_matchState_t* dictMatchState;
  199. ZSTD_compressionParameters cParams;
  200. const rawSeqStore_t* ldmSeqStore;
  201. };
  202. typedef struct {
  203. ZSTD_compressedBlockState_t* prevCBlock;
  204. ZSTD_compressedBlockState_t* nextCBlock;
  205. ZSTD_matchState_t matchState;
  206. } ZSTD_blockState_t;
  207. typedef struct {
  208. U32 offset;
  209. U32 checksum;
  210. } ldmEntry_t;
  211. typedef struct {
  212. BYTE const* split;
  213. U32 hash;
  214. U32 checksum;
  215. ldmEntry_t* bucket;
  216. } ldmMatchCandidate_t;
  217. #define LDM_BATCH_SIZE 64
  218. typedef struct {
  219. ZSTD_window_t window; /* State for the window round buffer management */
  220. ldmEntry_t* hashTable;
  221. U32 loadedDictEnd;
  222. BYTE* bucketOffsets; /* Next position in bucket to insert entry */
  223. size_t splitIndices[LDM_BATCH_SIZE];
  224. ldmMatchCandidate_t matchCandidates[LDM_BATCH_SIZE];
  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 hashRateLog; /* Log number of entries to skip */
  232. U32 windowLog; /* Window log for the LDM */
  233. } ldmParams_t;
  234. typedef struct {
  235. int collectSequences;
  236. ZSTD_Sequence* seqStart;
  237. size_t seqIndex;
  238. size_t maxSequences;
  239. } SeqCollector;
  240. struct ZSTD_CCtx_params_s {
  241. ZSTD_format_e format;
  242. ZSTD_compressionParameters cParams;
  243. ZSTD_frameParameters fParams;
  244. int compressionLevel;
  245. int forceWindow; /* force back-references to respect limit of
  246. * 1<<wLog, even for dictionary */
  247. size_t targetCBlockSize; /* Tries to fit compressed block size to be around targetCBlockSize.
  248. * No target when targetCBlockSize == 0.
  249. * There is no guarantee on compressed block size */
  250. int srcSizeHint; /* User's best guess of source size.
  251. * Hint is not valid when srcSizeHint == 0.
  252. * There is no guarantee that hint is close to actual source size */
  253. ZSTD_dictAttachPref_e attachDictPref;
  254. ZSTD_literalCompressionMode_e literalCompressionMode;
  255. /* Multithreading: used to pass parameters to mtctx */
  256. int nbWorkers;
  257. size_t jobSize;
  258. int overlapLog;
  259. int rsyncable;
  260. /* Long distance matching parameters */
  261. ldmParams_t ldmParams;
  262. /* Dedicated dict search algorithm trigger */
  263. int enableDedicatedDictSearch;
  264. /* Input/output buffer modes */
  265. ZSTD_bufferMode_e inBufferMode;
  266. ZSTD_bufferMode_e outBufferMode;
  267. /* Sequence compression API */
  268. ZSTD_sequenceFormat_e blockDelimiters;
  269. int validateSequences;
  270. /* Block splitting */
  271. int splitBlocks;
  272. /* Param for deciding whether to use row-based matchfinder */
  273. ZSTD_useRowMatchFinderMode_e useRowMatchFinder;
  274. /* Always load a dictionary in ext-dict mode (not prefix mode)? */
  275. int deterministicRefPrefix;
  276. /* Internal use, for createCCtxParams() and freeCCtxParams() only */
  277. ZSTD_customMem customMem;
  278. }; /* typedef'd to ZSTD_CCtx_params within "zstd.h" */
  279. #define COMPRESS_SEQUENCES_WORKSPACE_SIZE (sizeof(unsigned) * (MaxSeq + 2))
  280. #define ENTROPY_WORKSPACE_SIZE (HUF_WORKSPACE_SIZE + COMPRESS_SEQUENCES_WORKSPACE_SIZE)
  281. /**
  282. * Indicates whether this compression proceeds directly from user-provided
  283. * source buffer to user-provided destination buffer (ZSTDb_not_buffered), or
  284. * whether the context needs to buffer the input/output (ZSTDb_buffered).
  285. */
  286. typedef enum {
  287. ZSTDb_not_buffered,
  288. ZSTDb_buffered
  289. } ZSTD_buffered_policy_e;
  290. struct ZSTD_CCtx_s {
  291. ZSTD_compressionStage_e stage;
  292. int cParamsChanged; /* == 1 if cParams(except wlog) or compression level are changed in requestedParams. Triggers transmission of new params to ZSTDMT (if available) then reset to 0. */
  293. int bmi2; /* == 1 if the CPU supports BMI2 and 0 otherwise. CPU support is determined dynamically once per context lifetime. */
  294. ZSTD_CCtx_params requestedParams;
  295. ZSTD_CCtx_params appliedParams;
  296. ZSTD_CCtx_params simpleApiParams; /* Param storage used by the simple API - not sticky. Must only be used in top-level simple API functions for storage. */
  297. U32 dictID;
  298. size_t dictContentSize;
  299. ZSTD_cwksp workspace; /* manages buffer for dynamic allocations */
  300. size_t blockSize;
  301. unsigned long long pledgedSrcSizePlusOne; /* this way, 0 (default) == unknown */
  302. unsigned long long consumedSrcSize;
  303. unsigned long long producedCSize;
  304. XXH64_state_t xxhState;
  305. ZSTD_customMem customMem;
  306. ZSTD_threadPool* pool;
  307. size_t staticSize;
  308. SeqCollector seqCollector;
  309. int isFirstBlock;
  310. int initialized;
  311. seqStore_t seqStore; /* sequences storage ptrs */
  312. ldmState_t ldmState; /* long distance matching state */
  313. rawSeq* ldmSequences; /* Storage for the ldm output sequences */
  314. size_t maxNbLdmSequences;
  315. rawSeqStore_t externSeqStore; /* Mutable reference to external sequences */
  316. ZSTD_blockState_t blockState;
  317. U32* entropyWorkspace; /* entropy workspace of ENTROPY_WORKSPACE_SIZE bytes */
  318. /* Wether we are streaming or not */
  319. ZSTD_buffered_policy_e bufferedPolicy;
  320. /* streaming */
  321. char* inBuff;
  322. size_t inBuffSize;
  323. size_t inToCompress;
  324. size_t inBuffPos;
  325. size_t inBuffTarget;
  326. char* outBuff;
  327. size_t outBuffSize;
  328. size_t outBuffContentSize;
  329. size_t outBuffFlushedSize;
  330. ZSTD_cStreamStage streamStage;
  331. U32 frameEnded;
  332. /* Stable in/out buffer verification */
  333. ZSTD_inBuffer expectedInBuffer;
  334. size_t expectedOutBufferSize;
  335. /* Dictionary */
  336. ZSTD_localDict localDict;
  337. const ZSTD_CDict* cdict;
  338. ZSTD_prefixDict prefixDict; /* single-usage dictionary */
  339. /* Multi-threading */
  340. #ifdef ZSTD_MULTITHREAD
  341. ZSTDMT_CCtx* mtctx;
  342. #endif
  343. /* Tracing */
  344. #if ZSTD_TRACE
  345. ZSTD_TraceCtx traceCtx;
  346. #endif
  347. };
  348. typedef enum { ZSTD_dtlm_fast, ZSTD_dtlm_full } ZSTD_dictTableLoadMethod_e;
  349. typedef enum {
  350. ZSTD_noDict = 0,
  351. ZSTD_extDict = 1,
  352. ZSTD_dictMatchState = 2,
  353. ZSTD_dedicatedDictSearch = 3
  354. } ZSTD_dictMode_e;
  355. typedef enum {
  356. ZSTD_cpm_noAttachDict = 0, /* Compression with ZSTD_noDict or ZSTD_extDict.
  357. * In this mode we use both the srcSize and the dictSize
  358. * when selecting and adjusting parameters.
  359. */
  360. ZSTD_cpm_attachDict = 1, /* Compression with ZSTD_dictMatchState or ZSTD_dedicatedDictSearch.
  361. * In this mode we only take the srcSize into account when selecting
  362. * and adjusting parameters.
  363. */
  364. ZSTD_cpm_createCDict = 2, /* Creating a CDict.
  365. * In this mode we take both the source size and the dictionary size
  366. * into account when selecting and adjusting the parameters.
  367. */
  368. ZSTD_cpm_unknown = 3, /* ZSTD_getCParams, ZSTD_getParams, ZSTD_adjustParams.
  369. * We don't know what these parameters are for. We default to the legacy
  370. * behavior of taking both the source size and the dict size into account
  371. * when selecting and adjusting parameters.
  372. */
  373. } ZSTD_cParamMode_e;
  374. typedef size_t (*ZSTD_blockCompressor) (
  375. ZSTD_matchState_t* bs, seqStore_t* seqStore, U32 rep[ZSTD_REP_NUM],
  376. void const* src, size_t srcSize);
  377. ZSTD_blockCompressor ZSTD_selectBlockCompressor(ZSTD_strategy strat, ZSTD_useRowMatchFinderMode_e rowMatchfinderMode, ZSTD_dictMode_e dictMode);
  378. MEM_STATIC U32 ZSTD_LLcode(U32 litLength)
  379. {
  380. static const BYTE LL_Code[64] = { 0, 1, 2, 3, 4, 5, 6, 7,
  381. 8, 9, 10, 11, 12, 13, 14, 15,
  382. 16, 16, 17, 17, 18, 18, 19, 19,
  383. 20, 20, 20, 20, 21, 21, 21, 21,
  384. 22, 22, 22, 22, 22, 22, 22, 22,
  385. 23, 23, 23, 23, 23, 23, 23, 23,
  386. 24, 24, 24, 24, 24, 24, 24, 24,
  387. 24, 24, 24, 24, 24, 24, 24, 24 };
  388. static const U32 LL_deltaCode = 19;
  389. return (litLength > 63) ? ZSTD_highbit32(litLength) + LL_deltaCode : LL_Code[litLength];
  390. }
  391. /* ZSTD_MLcode() :
  392. * note : mlBase = matchLength - MINMATCH;
  393. * because it's the format it's stored in seqStore->sequences */
  394. MEM_STATIC U32 ZSTD_MLcode(U32 mlBase)
  395. {
  396. static const BYTE ML_Code[128] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15,
  397. 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31,
  398. 32, 32, 33, 33, 34, 34, 35, 35, 36, 36, 36, 36, 37, 37, 37, 37,
  399. 38, 38, 38, 38, 38, 38, 38, 38, 39, 39, 39, 39, 39, 39, 39, 39,
  400. 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40,
  401. 41, 41, 41, 41, 41, 41, 41, 41, 41, 41, 41, 41, 41, 41, 41, 41,
  402. 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42,
  403. 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42 };
  404. static const U32 ML_deltaCode = 36;
  405. return (mlBase > 127) ? ZSTD_highbit32(mlBase) + ML_deltaCode : ML_Code[mlBase];
  406. }
  407. typedef struct repcodes_s {
  408. U32 rep[3];
  409. } repcodes_t;
  410. MEM_STATIC repcodes_t ZSTD_updateRep(U32 const rep[3], U32 const offset, U32 const ll0)
  411. {
  412. repcodes_t newReps;
  413. if (offset >= ZSTD_REP_NUM) { /* full offset */
  414. newReps.rep[2] = rep[1];
  415. newReps.rep[1] = rep[0];
  416. newReps.rep[0] = offset - ZSTD_REP_MOVE;
  417. } else { /* repcode */
  418. U32 const repCode = offset + ll0;
  419. if (repCode > 0) { /* note : if repCode==0, no change */
  420. U32 const currentOffset = (repCode==ZSTD_REP_NUM) ? (rep[0] - 1) : rep[repCode];
  421. newReps.rep[2] = (repCode >= 2) ? rep[1] : rep[2];
  422. newReps.rep[1] = rep[0];
  423. newReps.rep[0] = currentOffset;
  424. } else { /* repCode == 0 */
  425. ZSTD_memcpy(&newReps, rep, sizeof(newReps));
  426. }
  427. }
  428. return newReps;
  429. }
  430. /* ZSTD_cParam_withinBounds:
  431. * @return 1 if value is within cParam bounds,
  432. * 0 otherwise */
  433. MEM_STATIC int ZSTD_cParam_withinBounds(ZSTD_cParameter cParam, int value)
  434. {
  435. ZSTD_bounds const bounds = ZSTD_cParam_getBounds(cParam);
  436. if (ZSTD_isError(bounds.error)) return 0;
  437. if (value < bounds.lowerBound) return 0;
  438. if (value > bounds.upperBound) return 0;
  439. return 1;
  440. }
  441. /* ZSTD_noCompressBlock() :
  442. * Writes uncompressed block to dst buffer from given src.
  443. * Returns the size of the block */
  444. MEM_STATIC size_t ZSTD_noCompressBlock (void* dst, size_t dstCapacity, const void* src, size_t srcSize, U32 lastBlock)
  445. {
  446. U32 const cBlockHeader24 = lastBlock + (((U32)bt_raw)<<1) + (U32)(srcSize << 3);
  447. RETURN_ERROR_IF(srcSize + ZSTD_blockHeaderSize > dstCapacity,
  448. dstSize_tooSmall, "dst buf too small for uncompressed block");
  449. MEM_writeLE24(dst, cBlockHeader24);
  450. ZSTD_memcpy((BYTE*)dst + ZSTD_blockHeaderSize, src, srcSize);
  451. return ZSTD_blockHeaderSize + srcSize;
  452. }
  453. MEM_STATIC size_t ZSTD_rleCompressBlock (void* dst, size_t dstCapacity, BYTE src, size_t srcSize, U32 lastBlock)
  454. {
  455. BYTE* const op = (BYTE*)dst;
  456. U32 const cBlockHeader = lastBlock + (((U32)bt_rle)<<1) + (U32)(srcSize << 3);
  457. RETURN_ERROR_IF(dstCapacity < 4, dstSize_tooSmall, "");
  458. MEM_writeLE24(op, cBlockHeader);
  459. op[3] = src;
  460. return 4;
  461. }
  462. /* ZSTD_minGain() :
  463. * minimum compression required
  464. * to generate a compress block or a compressed literals section.
  465. * note : use same formula for both situations */
  466. MEM_STATIC size_t ZSTD_minGain(size_t srcSize, ZSTD_strategy strat)
  467. {
  468. U32 const minlog = (strat>=ZSTD_btultra) ? (U32)(strat) - 1 : 6;
  469. ZSTD_STATIC_ASSERT(ZSTD_btultra == 8);
  470. assert(ZSTD_cParam_withinBounds(ZSTD_c_strategy, strat));
  471. return (srcSize >> minlog) + 2;
  472. }
  473. MEM_STATIC int ZSTD_disableLiteralsCompression(const ZSTD_CCtx_params* cctxParams)
  474. {
  475. switch (cctxParams->literalCompressionMode) {
  476. case ZSTD_lcm_huffman:
  477. return 0;
  478. case ZSTD_lcm_uncompressed:
  479. return 1;
  480. default:
  481. assert(0 /* impossible: pre-validated */);
  482. /* fall-through */
  483. case ZSTD_lcm_auto:
  484. return (cctxParams->cParams.strategy == ZSTD_fast) && (cctxParams->cParams.targetLength > 0);
  485. }
  486. }
  487. /*! ZSTD_safecopyLiterals() :
  488. * memcpy() function that won't read beyond more than WILDCOPY_OVERLENGTH bytes past ilimit_w.
  489. * Only called when the sequence ends past ilimit_w, so it only needs to be optimized for single
  490. * large copies.
  491. */
  492. static void ZSTD_safecopyLiterals(BYTE* op, BYTE const* ip, BYTE const* const iend, BYTE const* ilimit_w) {
  493. assert(iend > ilimit_w);
  494. if (ip <= ilimit_w) {
  495. ZSTD_wildcopy(op, ip, ilimit_w - ip, ZSTD_no_overlap);
  496. op += ilimit_w - ip;
  497. ip = ilimit_w;
  498. }
  499. while (ip < iend) *op++ = *ip++;
  500. }
  501. /*! ZSTD_storeSeq() :
  502. * Store a sequence (litlen, litPtr, offCode and mlBase) into seqStore_t.
  503. * `offCode` : distance to match + ZSTD_REP_MOVE (values <= ZSTD_REP_MOVE are repCodes).
  504. * `mlBase` : matchLength - MINMATCH
  505. * Allowed to overread literals up to litLimit.
  506. */
  507. HINT_INLINE UNUSED_ATTR
  508. void ZSTD_storeSeq(seqStore_t* seqStorePtr, size_t litLength, const BYTE* literals, const BYTE* litLimit, U32 offCode, size_t mlBase)
  509. {
  510. BYTE const* const litLimit_w = litLimit - WILDCOPY_OVERLENGTH;
  511. BYTE const* const litEnd = literals + litLength;
  512. #if defined(DEBUGLEVEL) && (DEBUGLEVEL >= 6)
  513. static const BYTE* g_start = NULL;
  514. if (g_start==NULL) g_start = (const BYTE*)literals; /* note : index only works for compression within a single segment */
  515. { U32 const pos = (U32)((const BYTE*)literals - g_start);
  516. DEBUGLOG(6, "Cpos%7u :%3u literals, match%4u bytes at offCode%7u",
  517. pos, (U32)litLength, (U32)mlBase+MINMATCH, (U32)offCode);
  518. }
  519. #endif
  520. assert((size_t)(seqStorePtr->sequences - seqStorePtr->sequencesStart) < seqStorePtr->maxNbSeq);
  521. /* copy Literals */
  522. assert(seqStorePtr->maxNbLit <= 128 KB);
  523. assert(seqStorePtr->lit + litLength <= seqStorePtr->litStart + seqStorePtr->maxNbLit);
  524. assert(literals + litLength <= litLimit);
  525. if (litEnd <= litLimit_w) {
  526. /* Common case we can use wildcopy.
  527. * First copy 16 bytes, because literals are likely short.
  528. */
  529. assert(WILDCOPY_OVERLENGTH >= 16);
  530. ZSTD_copy16(seqStorePtr->lit, literals);
  531. if (litLength > 16) {
  532. ZSTD_wildcopy(seqStorePtr->lit+16, literals+16, (ptrdiff_t)litLength-16, ZSTD_no_overlap);
  533. }
  534. } else {
  535. ZSTD_safecopyLiterals(seqStorePtr->lit, literals, litEnd, litLimit_w);
  536. }
  537. seqStorePtr->lit += litLength;
  538. /* literal Length */
  539. if (litLength>0xFFFF) {
  540. assert(seqStorePtr->longLengthType == ZSTD_llt_none); /* there can only be a single long length */
  541. seqStorePtr->longLengthType = ZSTD_llt_literalLength;
  542. seqStorePtr->longLengthPos = (U32)(seqStorePtr->sequences - seqStorePtr->sequencesStart);
  543. }
  544. seqStorePtr->sequences[0].litLength = (U16)litLength;
  545. /* match offset */
  546. seqStorePtr->sequences[0].offset = offCode + 1;
  547. /* match Length */
  548. if (mlBase>0xFFFF) {
  549. assert(seqStorePtr->longLengthType == ZSTD_llt_none); /* there can only be a single long length */
  550. seqStorePtr->longLengthType = ZSTD_llt_matchLength;
  551. seqStorePtr->longLengthPos = (U32)(seqStorePtr->sequences - seqStorePtr->sequencesStart);
  552. }
  553. seqStorePtr->sequences[0].matchLength = (U16)mlBase;
  554. seqStorePtr->sequences++;
  555. }
  556. /*-*************************************
  557. * Match length counter
  558. ***************************************/
  559. static unsigned ZSTD_NbCommonBytes (size_t val)
  560. {
  561. if (MEM_isLittleEndian()) {
  562. if (MEM_64bits()) {
  563. # if defined(_MSC_VER) && defined(_WIN64)
  564. # if STATIC_BMI2
  565. return _tzcnt_u64(val) >> 3;
  566. # else
  567. unsigned long r = 0;
  568. return _BitScanForward64( &r, (U64)val ) ? (unsigned)(r >> 3) : 0;
  569. # endif
  570. # elif defined(__GNUC__) && (__GNUC__ >= 4)
  571. return (__builtin_ctzll((U64)val) >> 3);
  572. # else
  573. static const int DeBruijnBytePos[64] = { 0, 0, 0, 0, 0, 1, 1, 2,
  574. 0, 3, 1, 3, 1, 4, 2, 7,
  575. 0, 2, 3, 6, 1, 5, 3, 5,
  576. 1, 3, 4, 4, 2, 5, 6, 7,
  577. 7, 0, 1, 2, 3, 3, 4, 6,
  578. 2, 6, 5, 5, 3, 4, 5, 6,
  579. 7, 1, 2, 4, 6, 4, 4, 5,
  580. 7, 2, 6, 5, 7, 6, 7, 7 };
  581. return DeBruijnBytePos[((U64)((val & -(long long)val) * 0x0218A392CDABBD3FULL)) >> 58];
  582. # endif
  583. } else { /* 32 bits */
  584. # if defined(_MSC_VER)
  585. unsigned long r=0;
  586. return _BitScanForward( &r, (U32)val ) ? (unsigned)(r >> 3) : 0;
  587. # elif defined(__GNUC__) && (__GNUC__ >= 3)
  588. return (__builtin_ctz((U32)val) >> 3);
  589. # else
  590. static const int DeBruijnBytePos[32] = { 0, 0, 3, 0, 3, 1, 3, 0,
  591. 3, 2, 2, 1, 3, 2, 0, 1,
  592. 3, 3, 1, 2, 2, 2, 2, 0,
  593. 3, 1, 2, 0, 1, 0, 1, 1 };
  594. return DeBruijnBytePos[((U32)((val & -(S32)val) * 0x077CB531U)) >> 27];
  595. # endif
  596. }
  597. } else { /* Big Endian CPU */
  598. if (MEM_64bits()) {
  599. # if defined(_MSC_VER) && defined(_WIN64)
  600. # if STATIC_BMI2
  601. return _lzcnt_u64(val) >> 3;
  602. # else
  603. unsigned long r = 0;
  604. return _BitScanReverse64(&r, (U64)val) ? (unsigned)(r >> 3) : 0;
  605. # endif
  606. # elif defined(__GNUC__) && (__GNUC__ >= 4)
  607. return (__builtin_clzll(val) >> 3);
  608. # else
  609. unsigned r;
  610. const unsigned n32 = sizeof(size_t)*4; /* calculate this way due to compiler complaining in 32-bits mode */
  611. if (!(val>>n32)) { r=4; } else { r=0; val>>=n32; }
  612. if (!(val>>16)) { r+=2; val>>=8; } else { val>>=24; }
  613. r += (!val);
  614. return r;
  615. # endif
  616. } else { /* 32 bits */
  617. # if defined(_MSC_VER)
  618. unsigned long r = 0;
  619. return _BitScanReverse( &r, (unsigned long)val ) ? (unsigned)(r >> 3) : 0;
  620. # elif defined(__GNUC__) && (__GNUC__ >= 3)
  621. return (__builtin_clz((U32)val) >> 3);
  622. # else
  623. unsigned r;
  624. if (!(val>>16)) { r=2; val>>=8; } else { r=0; val>>=24; }
  625. r += (!val);
  626. return r;
  627. # endif
  628. } }
  629. }
  630. MEM_STATIC size_t ZSTD_count(const BYTE* pIn, const BYTE* pMatch, const BYTE* const pInLimit)
  631. {
  632. const BYTE* const pStart = pIn;
  633. const BYTE* const pInLoopLimit = pInLimit - (sizeof(size_t)-1);
  634. if (pIn < pInLoopLimit) {
  635. { size_t const diff = MEM_readST(pMatch) ^ MEM_readST(pIn);
  636. if (diff) return ZSTD_NbCommonBytes(diff); }
  637. pIn+=sizeof(size_t); pMatch+=sizeof(size_t);
  638. while (pIn < pInLoopLimit) {
  639. size_t const diff = MEM_readST(pMatch) ^ MEM_readST(pIn);
  640. if (!diff) { pIn+=sizeof(size_t); pMatch+=sizeof(size_t); continue; }
  641. pIn += ZSTD_NbCommonBytes(diff);
  642. return (size_t)(pIn - pStart);
  643. } }
  644. if (MEM_64bits() && (pIn<(pInLimit-3)) && (MEM_read32(pMatch) == MEM_read32(pIn))) { pIn+=4; pMatch+=4; }
  645. if ((pIn<(pInLimit-1)) && (MEM_read16(pMatch) == MEM_read16(pIn))) { pIn+=2; pMatch+=2; }
  646. if ((pIn<pInLimit) && (*pMatch == *pIn)) pIn++;
  647. return (size_t)(pIn - pStart);
  648. }
  649. /** ZSTD_count_2segments() :
  650. * can count match length with `ip` & `match` in 2 different segments.
  651. * convention : on reaching mEnd, match count continue starting from iStart
  652. */
  653. MEM_STATIC size_t
  654. ZSTD_count_2segments(const BYTE* ip, const BYTE* match,
  655. const BYTE* iEnd, const BYTE* mEnd, const BYTE* iStart)
  656. {
  657. const BYTE* const vEnd = MIN( ip + (mEnd - match), iEnd);
  658. size_t const matchLength = ZSTD_count(ip, match, vEnd);
  659. if (match + matchLength != mEnd) return matchLength;
  660. DEBUGLOG(7, "ZSTD_count_2segments: found a 2-parts match (current length==%zu)", matchLength);
  661. DEBUGLOG(7, "distance from match beginning to end dictionary = %zi", mEnd - match);
  662. DEBUGLOG(7, "distance from current pos to end buffer = %zi", iEnd - ip);
  663. DEBUGLOG(7, "next byte : ip==%02X, istart==%02X", ip[matchLength], *iStart);
  664. DEBUGLOG(7, "final match length = %zu", matchLength + ZSTD_count(ip+matchLength, iStart, iEnd));
  665. return matchLength + ZSTD_count(ip+matchLength, iStart, iEnd);
  666. }
  667. /*-*************************************
  668. * Hashes
  669. ***************************************/
  670. static const U32 prime3bytes = 506832829U;
  671. static U32 ZSTD_hash3(U32 u, U32 h) { return ((u << (32-24)) * prime3bytes) >> (32-h) ; }
  672. MEM_STATIC size_t ZSTD_hash3Ptr(const void* ptr, U32 h) { return ZSTD_hash3(MEM_readLE32(ptr), h); } /* only in zstd_opt.h */
  673. static const U32 prime4bytes = 2654435761U;
  674. static U32 ZSTD_hash4(U32 u, U32 h) { return (u * prime4bytes) >> (32-h) ; }
  675. static size_t ZSTD_hash4Ptr(const void* ptr, U32 h) { return ZSTD_hash4(MEM_read32(ptr), h); }
  676. static const U64 prime5bytes = 889523592379ULL;
  677. static size_t ZSTD_hash5(U64 u, U32 h) { return (size_t)(((u << (64-40)) * prime5bytes) >> (64-h)) ; }
  678. static size_t ZSTD_hash5Ptr(const void* p, U32 h) { return ZSTD_hash5(MEM_readLE64(p), h); }
  679. static const U64 prime6bytes = 227718039650203ULL;
  680. static size_t ZSTD_hash6(U64 u, U32 h) { return (size_t)(((u << (64-48)) * prime6bytes) >> (64-h)) ; }
  681. static size_t ZSTD_hash6Ptr(const void* p, U32 h) { return ZSTD_hash6(MEM_readLE64(p), h); }
  682. static const U64 prime7bytes = 58295818150454627ULL;
  683. static size_t ZSTD_hash7(U64 u, U32 h) { return (size_t)(((u << (64-56)) * prime7bytes) >> (64-h)) ; }
  684. static size_t ZSTD_hash7Ptr(const void* p, U32 h) { return ZSTD_hash7(MEM_readLE64(p), h); }
  685. static const U64 prime8bytes = 0xCF1BBCDCB7A56463ULL;
  686. static size_t ZSTD_hash8(U64 u, U32 h) { return (size_t)(((u) * prime8bytes) >> (64-h)) ; }
  687. static size_t ZSTD_hash8Ptr(const void* p, U32 h) { return ZSTD_hash8(MEM_readLE64(p), h); }
  688. MEM_STATIC FORCE_INLINE_ATTR
  689. size_t ZSTD_hashPtr(const void* p, U32 hBits, U32 mls)
  690. {
  691. switch(mls)
  692. {
  693. default:
  694. case 4: return ZSTD_hash4Ptr(p, hBits);
  695. case 5: return ZSTD_hash5Ptr(p, hBits);
  696. case 6: return ZSTD_hash6Ptr(p, hBits);
  697. case 7: return ZSTD_hash7Ptr(p, hBits);
  698. case 8: return ZSTD_hash8Ptr(p, hBits);
  699. }
  700. }
  701. /** ZSTD_ipow() :
  702. * Return base^exponent.
  703. */
  704. static U64 ZSTD_ipow(U64 base, U64 exponent)
  705. {
  706. U64 power = 1;
  707. while (exponent) {
  708. if (exponent & 1) power *= base;
  709. exponent >>= 1;
  710. base *= base;
  711. }
  712. return power;
  713. }
  714. #define ZSTD_ROLL_HASH_CHAR_OFFSET 10
  715. /** ZSTD_rollingHash_append() :
  716. * Add the buffer to the hash value.
  717. */
  718. static U64 ZSTD_rollingHash_append(U64 hash, void const* buf, size_t size)
  719. {
  720. BYTE const* istart = (BYTE const*)buf;
  721. size_t pos;
  722. for (pos = 0; pos < size; ++pos) {
  723. hash *= prime8bytes;
  724. hash += istart[pos] + ZSTD_ROLL_HASH_CHAR_OFFSET;
  725. }
  726. return hash;
  727. }
  728. /** ZSTD_rollingHash_compute() :
  729. * Compute the rolling hash value of the buffer.
  730. */
  731. MEM_STATIC U64 ZSTD_rollingHash_compute(void const* buf, size_t size)
  732. {
  733. return ZSTD_rollingHash_append(0, buf, size);
  734. }
  735. /** ZSTD_rollingHash_primePower() :
  736. * Compute the primePower to be passed to ZSTD_rollingHash_rotate() for a hash
  737. * over a window of length bytes.
  738. */
  739. MEM_STATIC U64 ZSTD_rollingHash_primePower(U32 length)
  740. {
  741. return ZSTD_ipow(prime8bytes, length - 1);
  742. }
  743. /** ZSTD_rollingHash_rotate() :
  744. * Rotate the rolling hash by one byte.
  745. */
  746. MEM_STATIC U64 ZSTD_rollingHash_rotate(U64 hash, BYTE toRemove, BYTE toAdd, U64 primePower)
  747. {
  748. hash -= (toRemove + ZSTD_ROLL_HASH_CHAR_OFFSET) * primePower;
  749. hash *= prime8bytes;
  750. hash += toAdd + ZSTD_ROLL_HASH_CHAR_OFFSET;
  751. return hash;
  752. }
  753. /*-*************************************
  754. * Round buffer management
  755. ***************************************/
  756. #if (ZSTD_WINDOWLOG_MAX_64 > 31)
  757. # error "ZSTD_WINDOWLOG_MAX is too large : would overflow ZSTD_CURRENT_MAX"
  758. #endif
  759. /* Max current allowed */
  760. #define ZSTD_CURRENT_MAX ((3U << 29) + (1U << ZSTD_WINDOWLOG_MAX))
  761. /* Maximum chunk size before overflow correction needs to be called again */
  762. #define ZSTD_CHUNKSIZE_MAX \
  763. ( ((U32)-1) /* Maximum ending current index */ \
  764. - ZSTD_CURRENT_MAX) /* Maximum beginning lowLimit */
  765. /**
  766. * ZSTD_window_clear():
  767. * Clears the window containing the history by simply setting it to empty.
  768. */
  769. MEM_STATIC void ZSTD_window_clear(ZSTD_window_t* window)
  770. {
  771. size_t const endT = (size_t)(window->nextSrc - window->base);
  772. U32 const end = (U32)endT;
  773. window->lowLimit = end;
  774. window->dictLimit = end;
  775. }
  776. MEM_STATIC U32 ZSTD_window_isEmpty(ZSTD_window_t const window)
  777. {
  778. return window.dictLimit == 1 &&
  779. window.lowLimit == 1 &&
  780. (window.nextSrc - window.base) == 1;
  781. }
  782. /**
  783. * ZSTD_window_hasExtDict():
  784. * Returns non-zero if the window has a non-empty extDict.
  785. */
  786. MEM_STATIC U32 ZSTD_window_hasExtDict(ZSTD_window_t const window)
  787. {
  788. return window.lowLimit < window.dictLimit;
  789. }
  790. /**
  791. * ZSTD_matchState_dictMode():
  792. * Inspects the provided matchState and figures out what dictMode should be
  793. * passed to the compressor.
  794. */
  795. MEM_STATIC ZSTD_dictMode_e ZSTD_matchState_dictMode(const ZSTD_matchState_t *ms)
  796. {
  797. return ZSTD_window_hasExtDict(ms->window) ?
  798. ZSTD_extDict :
  799. ms->dictMatchState != NULL ?
  800. (ms->dictMatchState->dedicatedDictSearch ? ZSTD_dedicatedDictSearch : ZSTD_dictMatchState) :
  801. ZSTD_noDict;
  802. }
  803. /* Defining this macro to non-zero tells zstd to run the overflow correction
  804. * code much more frequently. This is very inefficient, and should only be
  805. * used for tests and fuzzers.
  806. */
  807. #ifndef ZSTD_WINDOW_OVERFLOW_CORRECT_FREQUENTLY
  808. # ifdef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
  809. # define ZSTD_WINDOW_OVERFLOW_CORRECT_FREQUENTLY 1
  810. # else
  811. # define ZSTD_WINDOW_OVERFLOW_CORRECT_FREQUENTLY 0
  812. # endif
  813. #endif
  814. /**
  815. * ZSTD_window_canOverflowCorrect():
  816. * Returns non-zero if the indices are large enough for overflow correction
  817. * to work correctly without impacting compression ratio.
  818. */
  819. MEM_STATIC U32 ZSTD_window_canOverflowCorrect(ZSTD_window_t const window,
  820. U32 cycleLog,
  821. U32 maxDist,
  822. U32 loadedDictEnd,
  823. void const* src)
  824. {
  825. U32 const cycleSize = 1u << cycleLog;
  826. U32 const curr = (U32)((BYTE const*)src - window.base);
  827. U32 const minIndexToOverflowCorrect = cycleSize + MAX(maxDist, cycleSize);
  828. /* Adjust the min index to backoff the overflow correction frequency,
  829. * so we don't waste too much CPU in overflow correction. If this
  830. * computation overflows we don't really care, we just need to make
  831. * sure it is at least minIndexToOverflowCorrect.
  832. */
  833. U32 const adjustment = window.nbOverflowCorrections + 1;
  834. U32 const adjustedIndex = MAX(minIndexToOverflowCorrect * adjustment,
  835. minIndexToOverflowCorrect);
  836. U32 const indexLargeEnough = curr > adjustedIndex;
  837. /* Only overflow correct early if the dictionary is invalidated already,
  838. * so we don't hurt compression ratio.
  839. */
  840. U32 const dictionaryInvalidated = curr > maxDist + loadedDictEnd;
  841. return indexLargeEnough && dictionaryInvalidated;
  842. }
  843. /**
  844. * ZSTD_window_needOverflowCorrection():
  845. * Returns non-zero if the indices are getting too large and need overflow
  846. * protection.
  847. */
  848. MEM_STATIC U32 ZSTD_window_needOverflowCorrection(ZSTD_window_t const window,
  849. U32 cycleLog,
  850. U32 maxDist,
  851. U32 loadedDictEnd,
  852. void const* src,
  853. void const* srcEnd)
  854. {
  855. U32 const curr = (U32)((BYTE const*)srcEnd - window.base);
  856. if (ZSTD_WINDOW_OVERFLOW_CORRECT_FREQUENTLY) {
  857. if (ZSTD_window_canOverflowCorrect(window, cycleLog, maxDist, loadedDictEnd, src)) {
  858. return 1;
  859. }
  860. }
  861. return curr > ZSTD_CURRENT_MAX;
  862. }
  863. /**
  864. * ZSTD_window_correctOverflow():
  865. * Reduces the indices to protect from index overflow.
  866. * Returns the correction made to the indices, which must be applied to every
  867. * stored index.
  868. *
  869. * The least significant cycleLog bits of the indices must remain the same,
  870. * which may be 0. Every index up to maxDist in the past must be valid.
  871. */
  872. MEM_STATIC U32 ZSTD_window_correctOverflow(ZSTD_window_t* window, U32 cycleLog,
  873. U32 maxDist, void const* src)
  874. {
  875. /* preemptive overflow correction:
  876. * 1. correction is large enough:
  877. * lowLimit > (3<<29) ==> current > 3<<29 + 1<<windowLog
  878. * 1<<windowLog <= newCurrent < 1<<chainLog + 1<<windowLog
  879. *
  880. * current - newCurrent
  881. * > (3<<29 + 1<<windowLog) - (1<<windowLog + 1<<chainLog)
  882. * > (3<<29) - (1<<chainLog)
  883. * > (3<<29) - (1<<30) (NOTE: chainLog <= 30)
  884. * > 1<<29
  885. *
  886. * 2. (ip+ZSTD_CHUNKSIZE_MAX - cctx->base) doesn't overflow:
  887. * After correction, current is less than (1<<chainLog + 1<<windowLog).
  888. * In 64-bit mode we are safe, because we have 64-bit ptrdiff_t.
  889. * In 32-bit mode we are safe, because (chainLog <= 29), so
  890. * ip+ZSTD_CHUNKSIZE_MAX - cctx->base < 1<<32.
  891. * 3. (cctx->lowLimit + 1<<windowLog) < 1<<32:
  892. * windowLog <= 31 ==> 3<<29 + 1<<windowLog < 7<<29 < 1<<32.
  893. */
  894. U32 const cycleSize = 1u << cycleLog;
  895. U32 const cycleMask = cycleSize - 1;
  896. U32 const curr = (U32)((BYTE const*)src - window->base);
  897. U32 const currentCycle0 = curr & cycleMask;
  898. /* Exclude zero so that newCurrent - maxDist >= 1. */
  899. U32 const currentCycle1 = currentCycle0 == 0 ? cycleSize : currentCycle0;
  900. U32 const newCurrent = currentCycle1 + MAX(maxDist, cycleSize);
  901. U32 const correction = curr - newCurrent;
  902. /* maxDist must be a power of two so that:
  903. * (newCurrent & cycleMask) == (curr & cycleMask)
  904. * This is required to not corrupt the chains / binary tree.
  905. */
  906. assert((maxDist & (maxDist - 1)) == 0);
  907. assert((curr & cycleMask) == (newCurrent & cycleMask));
  908. assert(curr > newCurrent);
  909. if (!ZSTD_WINDOW_OVERFLOW_CORRECT_FREQUENTLY) {
  910. /* Loose bound, should be around 1<<29 (see above) */
  911. assert(correction > 1<<28);
  912. }
  913. window->base += correction;
  914. window->dictBase += correction;
  915. if (window->lowLimit <= correction) window->lowLimit = 1;
  916. else window->lowLimit -= correction;
  917. if (window->dictLimit <= correction) window->dictLimit = 1;
  918. else window->dictLimit -= correction;
  919. /* Ensure we can still reference the full window. */
  920. assert(newCurrent >= maxDist);
  921. assert(newCurrent - maxDist >= 1);
  922. /* Ensure that lowLimit and dictLimit didn't underflow. */
  923. assert(window->lowLimit <= newCurrent);
  924. assert(window->dictLimit <= newCurrent);
  925. ++window->nbOverflowCorrections;
  926. DEBUGLOG(4, "Correction of 0x%x bytes to lowLimit=0x%x", correction,
  927. window->lowLimit);
  928. return correction;
  929. }
  930. /**
  931. * ZSTD_window_enforceMaxDist():
  932. * Updates lowLimit so that:
  933. * (srcEnd - base) - lowLimit == maxDist + loadedDictEnd
  934. *
  935. * It ensures index is valid as long as index >= lowLimit.
  936. * This must be called before a block compression call.
  937. *
  938. * loadedDictEnd is only defined if a dictionary is in use for current compression.
  939. * As the name implies, loadedDictEnd represents the index at end of dictionary.
  940. * The value lies within context's referential, it can be directly compared to blockEndIdx.
  941. *
  942. * If loadedDictEndPtr is NULL, no dictionary is in use, and we use loadedDictEnd == 0.
  943. * If loadedDictEndPtr is not NULL, we set it to zero after updating lowLimit.
  944. * This is because dictionaries are allowed to be referenced fully
  945. * as long as the last byte of the dictionary is in the window.
  946. * Once input has progressed beyond window size, dictionary cannot be referenced anymore.
  947. *
  948. * In normal dict mode, the dictionary lies between lowLimit and dictLimit.
  949. * In dictMatchState mode, lowLimit and dictLimit are the same,
  950. * and the dictionary is below them.
  951. * forceWindow and dictMatchState are therefore incompatible.
  952. */
  953. MEM_STATIC void
  954. ZSTD_window_enforceMaxDist(ZSTD_window_t* window,
  955. const void* blockEnd,
  956. U32 maxDist,
  957. U32* loadedDictEndPtr,
  958. const ZSTD_matchState_t** dictMatchStatePtr)
  959. {
  960. U32 const blockEndIdx = (U32)((BYTE const*)blockEnd - window->base);
  961. U32 const loadedDictEnd = (loadedDictEndPtr != NULL) ? *loadedDictEndPtr : 0;
  962. DEBUGLOG(5, "ZSTD_window_enforceMaxDist: blockEndIdx=%u, maxDist=%u, loadedDictEnd=%u",
  963. (unsigned)blockEndIdx, (unsigned)maxDist, (unsigned)loadedDictEnd);
  964. /* - When there is no dictionary : loadedDictEnd == 0.
  965. In which case, the test (blockEndIdx > maxDist) is merely to avoid
  966. overflowing next operation `newLowLimit = blockEndIdx - maxDist`.
  967. - When there is a standard dictionary :
  968. Index referential is copied from the dictionary,
  969. which means it starts from 0.
  970. In which case, loadedDictEnd == dictSize,
  971. and it makes sense to compare `blockEndIdx > maxDist + dictSize`
  972. since `blockEndIdx` also starts from zero.
  973. - When there is an attached dictionary :
  974. loadedDictEnd is expressed within the referential of the context,
  975. so it can be directly compared against blockEndIdx.
  976. */
  977. if (blockEndIdx > maxDist + loadedDictEnd) {
  978. U32 const newLowLimit = blockEndIdx - maxDist;
  979. if (window->lowLimit < newLowLimit) window->lowLimit = newLowLimit;
  980. if (window->dictLimit < window->lowLimit) {
  981. DEBUGLOG(5, "Update dictLimit to match lowLimit, from %u to %u",
  982. (unsigned)window->dictLimit, (unsigned)window->lowLimit);
  983. window->dictLimit = window->lowLimit;
  984. }
  985. /* On reaching window size, dictionaries are invalidated */
  986. if (loadedDictEndPtr) *loadedDictEndPtr = 0;
  987. if (dictMatchStatePtr) *dictMatchStatePtr = NULL;
  988. }
  989. }
  990. /* Similar to ZSTD_window_enforceMaxDist(),
  991. * but only invalidates dictionary
  992. * when input progresses beyond window size.
  993. * assumption : loadedDictEndPtr and dictMatchStatePtr are valid (non NULL)
  994. * loadedDictEnd uses same referential as window->base
  995. * maxDist is the window size */
  996. MEM_STATIC void
  997. ZSTD_checkDictValidity(const ZSTD_window_t* window,
  998. const void* blockEnd,
  999. U32 maxDist,
  1000. U32* loadedDictEndPtr,
  1001. const ZSTD_matchState_t** dictMatchStatePtr)
  1002. {
  1003. assert(loadedDictEndPtr != NULL);
  1004. assert(dictMatchStatePtr != NULL);
  1005. { U32 const blockEndIdx = (U32)((BYTE const*)blockEnd - window->base);
  1006. U32 const loadedDictEnd = *loadedDictEndPtr;
  1007. DEBUGLOG(5, "ZSTD_checkDictValidity: blockEndIdx=%u, maxDist=%u, loadedDictEnd=%u",
  1008. (unsigned)blockEndIdx, (unsigned)maxDist, (unsigned)loadedDictEnd);
  1009. assert(blockEndIdx >= loadedDictEnd);
  1010. if (blockEndIdx > loadedDictEnd + maxDist) {
  1011. /* On reaching window size, dictionaries are invalidated.
  1012. * For simplification, if window size is reached anywhere within next block,
  1013. * the dictionary is invalidated for the full block.
  1014. */
  1015. DEBUGLOG(6, "invalidating dictionary for current block (distance > windowSize)");
  1016. *loadedDictEndPtr = 0;
  1017. *dictMatchStatePtr = NULL;
  1018. } else {
  1019. if (*loadedDictEndPtr != 0) {
  1020. DEBUGLOG(6, "dictionary considered valid for current block");
  1021. } } }
  1022. }
  1023. MEM_STATIC void ZSTD_window_init(ZSTD_window_t* window) {
  1024. ZSTD_memset(window, 0, sizeof(*window));
  1025. window->base = (BYTE const*)"";
  1026. window->dictBase = (BYTE const*)"";
  1027. window->dictLimit = 1; /* start from 1, so that 1st position is valid */
  1028. window->lowLimit = 1; /* it ensures first and later CCtx usages compress the same */
  1029. window->nextSrc = window->base + 1; /* see issue #1241 */
  1030. window->nbOverflowCorrections = 0;
  1031. }
  1032. /**
  1033. * ZSTD_window_update():
  1034. * Updates the window by appending [src, src + srcSize) to the window.
  1035. * If it is not contiguous, the current prefix becomes the extDict, and we
  1036. * forget about the extDict. Handles overlap of the prefix and extDict.
  1037. * Returns non-zero if the segment is contiguous.
  1038. */
  1039. MEM_STATIC U32 ZSTD_window_update(ZSTD_window_t* window,
  1040. void const* src, size_t srcSize,
  1041. int forceNonContiguous)
  1042. {
  1043. BYTE const* const ip = (BYTE const*)src;
  1044. U32 contiguous = 1;
  1045. DEBUGLOG(5, "ZSTD_window_update");
  1046. if (srcSize == 0)
  1047. return contiguous;
  1048. assert(window->base != NULL);
  1049. assert(window->dictBase != NULL);
  1050. /* Check if blocks follow each other */
  1051. if (src != window->nextSrc || forceNonContiguous) {
  1052. /* not contiguous */
  1053. size_t const distanceFromBase = (size_t)(window->nextSrc - window->base);
  1054. DEBUGLOG(5, "Non contiguous blocks, new segment starts at %u", window->dictLimit);
  1055. window->lowLimit = window->dictLimit;
  1056. assert(distanceFromBase == (size_t)(U32)distanceFromBase); /* should never overflow */
  1057. window->dictLimit = (U32)distanceFromBase;
  1058. window->dictBase = window->base;
  1059. window->base = ip - distanceFromBase;
  1060. /* ms->nextToUpdate = window->dictLimit; */
  1061. if (window->dictLimit - window->lowLimit < HASH_READ_SIZE) window->lowLimit = window->dictLimit; /* too small extDict */
  1062. contiguous = 0;
  1063. }
  1064. window->nextSrc = ip + srcSize;
  1065. /* if input and dictionary overlap : reduce dictionary (area presumed modified by input) */
  1066. if ( (ip+srcSize > window->dictBase + window->lowLimit)
  1067. & (ip < window->dictBase + window->dictLimit)) {
  1068. ptrdiff_t const highInputIdx = (ip + srcSize) - window->dictBase;
  1069. U32 const lowLimitMax = (highInputIdx > (ptrdiff_t)window->dictLimit) ? window->dictLimit : (U32)highInputIdx;
  1070. window->lowLimit = lowLimitMax;
  1071. DEBUGLOG(5, "Overlapping extDict and input : new lowLimit = %u", window->lowLimit);
  1072. }
  1073. return contiguous;
  1074. }
  1075. /**
  1076. * Returns the lowest allowed match index. It may either be in the ext-dict or the prefix.
  1077. */
  1078. MEM_STATIC U32 ZSTD_getLowestMatchIndex(const ZSTD_matchState_t* ms, U32 curr, unsigned windowLog)
  1079. {
  1080. U32 const maxDistance = 1U << windowLog;
  1081. U32 const lowestValid = ms->window.lowLimit;
  1082. U32 const withinWindow = (curr - lowestValid > maxDistance) ? curr - maxDistance : lowestValid;
  1083. U32 const isDictionary = (ms->loadedDictEnd != 0);
  1084. /* When using a dictionary the entire dictionary is valid if a single byte of the dictionary
  1085. * is within the window. We invalidate the dictionary (and set loadedDictEnd to 0) when it isn't
  1086. * valid for the entire block. So this check is sufficient to find the lowest valid match index.
  1087. */
  1088. U32 const matchLowest = isDictionary ? lowestValid : withinWindow;
  1089. return matchLowest;
  1090. }
  1091. /**
  1092. * Returns the lowest allowed match index in the prefix.
  1093. */
  1094. MEM_STATIC U32 ZSTD_getLowestPrefixIndex(const ZSTD_matchState_t* ms, U32 curr, unsigned windowLog)
  1095. {
  1096. U32 const maxDistance = 1U << windowLog;
  1097. U32 const lowestValid = ms->window.dictLimit;
  1098. U32 const withinWindow = (curr - lowestValid > maxDistance) ? curr - maxDistance : lowestValid;
  1099. U32 const isDictionary = (ms->loadedDictEnd != 0);
  1100. /* When computing the lowest prefix index we need to take the dictionary into account to handle
  1101. * the edge case where the dictionary and the source are contiguous in memory.
  1102. */
  1103. U32 const matchLowest = isDictionary ? lowestValid : withinWindow;
  1104. return matchLowest;
  1105. }
  1106. /* debug functions */
  1107. #if (DEBUGLEVEL>=2)
  1108. MEM_STATIC double ZSTD_fWeight(U32 rawStat)
  1109. {
  1110. U32 const fp_accuracy = 8;
  1111. U32 const fp_multiplier = (1 << fp_accuracy);
  1112. U32 const newStat = rawStat + 1;
  1113. U32 const hb = ZSTD_highbit32(newStat);
  1114. U32 const BWeight = hb * fp_multiplier;
  1115. U32 const FWeight = (newStat << fp_accuracy) >> hb;
  1116. U32 const weight = BWeight + FWeight;
  1117. assert(hb + fp_accuracy < 31);
  1118. return (double)weight / fp_multiplier;
  1119. }
  1120. /* display a table content,
  1121. * listing each element, its frequency, and its predicted bit cost */
  1122. MEM_STATIC void ZSTD_debugTable(const U32* table, U32 max)
  1123. {
  1124. unsigned u, sum;
  1125. for (u=0, sum=0; u<=max; u++) sum += table[u];
  1126. DEBUGLOG(2, "total nb elts: %u", sum);
  1127. for (u=0; u<=max; u++) {
  1128. DEBUGLOG(2, "%2u: %5u (%.2f)",
  1129. u, table[u], ZSTD_fWeight(sum) - ZSTD_fWeight(table[u]) );
  1130. }
  1131. }
  1132. #endif
  1133. #if defined (__cplusplus)
  1134. }
  1135. #endif
  1136. /* ===============================================================
  1137. * Shared internal declarations
  1138. * These prototypes may be called from sources not in lib/compress
  1139. * =============================================================== */
  1140. /* ZSTD_loadCEntropy() :
  1141. * dict : must point at beginning of a valid zstd dictionary.
  1142. * return : size of dictionary header (size of magic number + dict ID + entropy tables)
  1143. * assumptions : magic number supposed already checked
  1144. * and dictSize >= 8 */
  1145. size_t ZSTD_loadCEntropy(ZSTD_compressedBlockState_t* bs, void* workspace,
  1146. const void* const dict, size_t dictSize);
  1147. void ZSTD_reset_compressedBlockState(ZSTD_compressedBlockState_t* bs);
  1148. /* ==============================================================
  1149. * Private declarations
  1150. * These prototypes shall only be called from within lib/compress
  1151. * ============================================================== */
  1152. /* ZSTD_getCParamsFromCCtxParams() :
  1153. * cParams are built depending on compressionLevel, src size hints,
  1154. * LDM and manually set compression parameters.
  1155. * Note: srcSizeHint == 0 means 0!
  1156. */
  1157. ZSTD_compressionParameters ZSTD_getCParamsFromCCtxParams(
  1158. const ZSTD_CCtx_params* CCtxParams, U64 srcSizeHint, size_t dictSize, ZSTD_cParamMode_e mode);
  1159. /*! ZSTD_initCStream_internal() :
  1160. * Private use only. Init streaming operation.
  1161. * expects params to be valid.
  1162. * must receive dict, or cdict, or none, but not both.
  1163. * @return : 0, or an error code */
  1164. size_t ZSTD_initCStream_internal(ZSTD_CStream* zcs,
  1165. const void* dict, size_t dictSize,
  1166. const ZSTD_CDict* cdict,
  1167. const ZSTD_CCtx_params* params, unsigned long long pledgedSrcSize);
  1168. void ZSTD_resetSeqStore(seqStore_t* ssPtr);
  1169. /*! ZSTD_getCParamsFromCDict() :
  1170. * as the name implies */
  1171. ZSTD_compressionParameters ZSTD_getCParamsFromCDict(const ZSTD_CDict* cdict);
  1172. /* ZSTD_compressBegin_advanced_internal() :
  1173. * Private use only. To be called from zstdmt_compress.c. */
  1174. size_t ZSTD_compressBegin_advanced_internal(ZSTD_CCtx* cctx,
  1175. const void* dict, size_t dictSize,
  1176. ZSTD_dictContentType_e dictContentType,
  1177. ZSTD_dictTableLoadMethod_e dtlm,
  1178. const ZSTD_CDict* cdict,
  1179. const ZSTD_CCtx_params* params,
  1180. unsigned long long pledgedSrcSize);
  1181. /* ZSTD_compress_advanced_internal() :
  1182. * Private use only. To be called from zstdmt_compress.c. */
  1183. size_t ZSTD_compress_advanced_internal(ZSTD_CCtx* cctx,
  1184. void* dst, size_t dstCapacity,
  1185. const void* src, size_t srcSize,
  1186. const void* dict,size_t dictSize,
  1187. const ZSTD_CCtx_params* params);
  1188. /* ZSTD_writeLastEmptyBlock() :
  1189. * output an empty Block with end-of-frame mark to complete a frame
  1190. * @return : size of data written into `dst` (== ZSTD_blockHeaderSize (defined in zstd_internal.h))
  1191. * or an error code if `dstCapacity` is too small (<ZSTD_blockHeaderSize)
  1192. */
  1193. size_t ZSTD_writeLastEmptyBlock(void* dst, size_t dstCapacity);
  1194. /* ZSTD_referenceExternalSequences() :
  1195. * Must be called before starting a compression operation.
  1196. * seqs must parse a prefix of the source.
  1197. * This cannot be used when long range matching is enabled.
  1198. * Zstd will use these sequences, and pass the literals to a secondary block
  1199. * compressor.
  1200. * @return : An error code on failure.
  1201. * NOTE: seqs are not verified! Invalid sequences can cause out-of-bounds memory
  1202. * access and data corruption.
  1203. */
  1204. size_t ZSTD_referenceExternalSequences(ZSTD_CCtx* cctx, rawSeq* seq, size_t nbSeq);
  1205. /** ZSTD_cycleLog() :
  1206. * condition for correct operation : hashLog > 1 */
  1207. U32 ZSTD_cycleLog(U32 hashLog, ZSTD_strategy strat);
  1208. /** ZSTD_CCtx_trace() :
  1209. * Trace the end of a compression call.
  1210. */
  1211. void ZSTD_CCtx_trace(ZSTD_CCtx* cctx, size_t extraCSize);
  1212. #endif /* ZSTD_COMPRESS_H */