zstd_ldm.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598
  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. */
  9. #include "zstd_ldm.h"
  10. #include "debug.h"
  11. #include "zstd_fast.h" /* ZSTD_fillHashTable() */
  12. #include "zstd_double_fast.h" /* ZSTD_fillDoubleHashTable() */
  13. #define LDM_BUCKET_SIZE_LOG 3
  14. #define LDM_MIN_MATCH_LENGTH 64
  15. #define LDM_HASH_RLOG 7
  16. #define LDM_HASH_CHAR_OFFSET 10
  17. void ZSTD_ldm_adjustParameters(ldmParams_t* params,
  18. ZSTD_compressionParameters const* cParams)
  19. {
  20. params->windowLog = cParams->windowLog;
  21. ZSTD_STATIC_ASSERT(LDM_BUCKET_SIZE_LOG <= ZSTD_LDM_BUCKETSIZELOG_MAX);
  22. DEBUGLOG(4, "ZSTD_ldm_adjustParameters");
  23. if (!params->bucketSizeLog) params->bucketSizeLog = LDM_BUCKET_SIZE_LOG;
  24. if (!params->minMatchLength) params->minMatchLength = LDM_MIN_MATCH_LENGTH;
  25. if (cParams->strategy >= ZSTD_btopt) {
  26. /* Get out of the way of the optimal parser */
  27. U32 const minMatch = MAX(cParams->targetLength, params->minMatchLength);
  28. assert(minMatch >= ZSTD_LDM_MINMATCH_MIN);
  29. assert(minMatch <= ZSTD_LDM_MINMATCH_MAX);
  30. params->minMatchLength = minMatch;
  31. }
  32. if (params->hashLog == 0) {
  33. params->hashLog = MAX(ZSTD_HASHLOG_MIN, params->windowLog - LDM_HASH_RLOG);
  34. assert(params->hashLog <= ZSTD_HASHLOG_MAX);
  35. }
  36. if (params->hashRateLog == 0) {
  37. params->hashRateLog = params->windowLog < params->hashLog
  38. ? 0
  39. : params->windowLog - params->hashLog;
  40. }
  41. params->bucketSizeLog = MIN(params->bucketSizeLog, params->hashLog);
  42. }
  43. size_t ZSTD_ldm_getTableSize(ldmParams_t params)
  44. {
  45. size_t const ldmHSize = ((size_t)1) << params.hashLog;
  46. size_t const ldmBucketSizeLog = MIN(params.bucketSizeLog, params.hashLog);
  47. size_t const ldmBucketSize =
  48. ((size_t)1) << (params.hashLog - ldmBucketSizeLog);
  49. size_t const totalSize = ldmBucketSize + ldmHSize * sizeof(ldmEntry_t);
  50. return params.enableLdm ? totalSize : 0;
  51. }
  52. size_t ZSTD_ldm_getMaxNbSeq(ldmParams_t params, size_t maxChunkSize)
  53. {
  54. return params.enableLdm ? (maxChunkSize / params.minMatchLength) : 0;
  55. }
  56. /** ZSTD_ldm_getSmallHash() :
  57. * numBits should be <= 32
  58. * If numBits==0, returns 0.
  59. * @return : the most significant numBits of value. */
  60. static U32 ZSTD_ldm_getSmallHash(U64 value, U32 numBits)
  61. {
  62. assert(numBits <= 32);
  63. return numBits == 0 ? 0 : (U32)(value >> (64 - numBits));
  64. }
  65. /** ZSTD_ldm_getChecksum() :
  66. * numBitsToDiscard should be <= 32
  67. * @return : the next most significant 32 bits after numBitsToDiscard */
  68. static U32 ZSTD_ldm_getChecksum(U64 hash, U32 numBitsToDiscard)
  69. {
  70. assert(numBitsToDiscard <= 32);
  71. return (hash >> (64 - 32 - numBitsToDiscard)) & 0xFFFFFFFF;
  72. }
  73. /** ZSTD_ldm_getTag() ;
  74. * Given the hash, returns the most significant numTagBits bits
  75. * after (32 + hbits) bits.
  76. *
  77. * If there are not enough bits remaining, return the last
  78. * numTagBits bits. */
  79. static U32 ZSTD_ldm_getTag(U64 hash, U32 hbits, U32 numTagBits)
  80. {
  81. assert(numTagBits < 32 && hbits <= 32);
  82. if (32 - hbits < numTagBits) {
  83. return hash & (((U32)1 << numTagBits) - 1);
  84. } else {
  85. return (hash >> (32 - hbits - numTagBits)) & (((U32)1 << numTagBits) - 1);
  86. }
  87. }
  88. /** ZSTD_ldm_getBucket() :
  89. * Returns a pointer to the start of the bucket associated with hash. */
  90. static ldmEntry_t* ZSTD_ldm_getBucket(
  91. ldmState_t* ldmState, size_t hash, ldmParams_t const ldmParams)
  92. {
  93. return ldmState->hashTable + (hash << ldmParams.bucketSizeLog);
  94. }
  95. /** ZSTD_ldm_insertEntry() :
  96. * Insert the entry with corresponding hash into the hash table */
  97. static void ZSTD_ldm_insertEntry(ldmState_t* ldmState,
  98. size_t const hash, const ldmEntry_t entry,
  99. ldmParams_t const ldmParams)
  100. {
  101. BYTE* const bucketOffsets = ldmState->bucketOffsets;
  102. *(ZSTD_ldm_getBucket(ldmState, hash, ldmParams) + bucketOffsets[hash]) = entry;
  103. bucketOffsets[hash]++;
  104. bucketOffsets[hash] &= ((U32)1 << ldmParams.bucketSizeLog) - 1;
  105. }
  106. /** ZSTD_ldm_makeEntryAndInsertByTag() :
  107. *
  108. * Gets the small hash, checksum, and tag from the rollingHash.
  109. *
  110. * If the tag matches (1 << ldmParams.hashRateLog)-1, then
  111. * creates an ldmEntry from the offset, and inserts it into the hash table.
  112. *
  113. * hBits is the length of the small hash, which is the most significant hBits
  114. * of rollingHash. The checksum is the next 32 most significant bits, followed
  115. * by ldmParams.hashRateLog bits that make up the tag. */
  116. static void ZSTD_ldm_makeEntryAndInsertByTag(ldmState_t* ldmState,
  117. U64 const rollingHash,
  118. U32 const hBits,
  119. U32 const offset,
  120. ldmParams_t const ldmParams)
  121. {
  122. U32 const tag = ZSTD_ldm_getTag(rollingHash, hBits, ldmParams.hashRateLog);
  123. U32 const tagMask = ((U32)1 << ldmParams.hashRateLog) - 1;
  124. if (tag == tagMask) {
  125. U32 const hash = ZSTD_ldm_getSmallHash(rollingHash, hBits);
  126. U32 const checksum = ZSTD_ldm_getChecksum(rollingHash, hBits);
  127. ldmEntry_t entry;
  128. entry.offset = offset;
  129. entry.checksum = checksum;
  130. ZSTD_ldm_insertEntry(ldmState, hash, entry, ldmParams);
  131. }
  132. }
  133. /** ZSTD_ldm_countBackwardsMatch() :
  134. * Returns the number of bytes that match backwards before pIn and pMatch.
  135. *
  136. * We count only bytes where pMatch >= pBase and pIn >= pAnchor. */
  137. static size_t ZSTD_ldm_countBackwardsMatch(
  138. const BYTE* pIn, const BYTE* pAnchor,
  139. const BYTE* pMatch, const BYTE* pBase)
  140. {
  141. size_t matchLength = 0;
  142. while (pIn > pAnchor && pMatch > pBase && pIn[-1] == pMatch[-1]) {
  143. pIn--;
  144. pMatch--;
  145. matchLength++;
  146. }
  147. return matchLength;
  148. }
  149. /** ZSTD_ldm_fillFastTables() :
  150. *
  151. * Fills the relevant tables for the ZSTD_fast and ZSTD_dfast strategies.
  152. * This is similar to ZSTD_loadDictionaryContent.
  153. *
  154. * The tables for the other strategies are filled within their
  155. * block compressors. */
  156. static size_t ZSTD_ldm_fillFastTables(ZSTD_matchState_t* ms,
  157. void const* end)
  158. {
  159. const BYTE* const iend = (const BYTE*)end;
  160. switch(ms->cParams.strategy)
  161. {
  162. case ZSTD_fast:
  163. ZSTD_fillHashTable(ms, iend, ZSTD_dtlm_fast);
  164. break;
  165. case ZSTD_dfast:
  166. ZSTD_fillDoubleHashTable(ms, iend, ZSTD_dtlm_fast);
  167. break;
  168. case ZSTD_greedy:
  169. case ZSTD_lazy:
  170. case ZSTD_lazy2:
  171. case ZSTD_btlazy2:
  172. case ZSTD_btopt:
  173. case ZSTD_btultra:
  174. case ZSTD_btultra2:
  175. break;
  176. default:
  177. assert(0); /* not possible : not a valid strategy id */
  178. }
  179. return 0;
  180. }
  181. /** ZSTD_ldm_fillLdmHashTable() :
  182. *
  183. * Fills hashTable from (lastHashed + 1) to iend (non-inclusive).
  184. * lastHash is the rolling hash that corresponds to lastHashed.
  185. *
  186. * Returns the rolling hash corresponding to position iend-1. */
  187. static U64 ZSTD_ldm_fillLdmHashTable(ldmState_t* state,
  188. U64 lastHash, const BYTE* lastHashed,
  189. const BYTE* iend, const BYTE* base,
  190. U32 hBits, ldmParams_t const ldmParams)
  191. {
  192. U64 rollingHash = lastHash;
  193. const BYTE* cur = lastHashed + 1;
  194. while (cur < iend) {
  195. rollingHash = ZSTD_rollingHash_rotate(rollingHash, cur[-1],
  196. cur[ldmParams.minMatchLength-1],
  197. state->hashPower);
  198. ZSTD_ldm_makeEntryAndInsertByTag(state,
  199. rollingHash, hBits,
  200. (U32)(cur - base), ldmParams);
  201. ++cur;
  202. }
  203. return rollingHash;
  204. }
  205. /** ZSTD_ldm_limitTableUpdate() :
  206. *
  207. * Sets cctx->nextToUpdate to a position corresponding closer to anchor
  208. * if it is far way
  209. * (after a long match, only update tables a limited amount). */
  210. static void ZSTD_ldm_limitTableUpdate(ZSTD_matchState_t* ms, const BYTE* anchor)
  211. {
  212. U32 const current = (U32)(anchor - ms->window.base);
  213. if (current > ms->nextToUpdate + 1024) {
  214. ms->nextToUpdate =
  215. current - MIN(512, current - ms->nextToUpdate - 1024);
  216. }
  217. }
  218. static size_t ZSTD_ldm_generateSequences_internal(
  219. ldmState_t* ldmState, rawSeqStore_t* rawSeqStore,
  220. ldmParams_t const* params, void const* src, size_t srcSize)
  221. {
  222. /* LDM parameters */
  223. int const extDict = ZSTD_window_hasExtDict(ldmState->window);
  224. U32 const minMatchLength = params->minMatchLength;
  225. U64 const hashPower = ldmState->hashPower;
  226. U32 const hBits = params->hashLog - params->bucketSizeLog;
  227. U32 const ldmBucketSize = 1U << params->bucketSizeLog;
  228. U32 const hashRateLog = params->hashRateLog;
  229. U32 const ldmTagMask = (1U << params->hashRateLog) - 1;
  230. /* Prefix and extDict parameters */
  231. U32 const dictLimit = ldmState->window.dictLimit;
  232. U32 const lowestIndex = extDict ? ldmState->window.lowLimit : dictLimit;
  233. BYTE const* const base = ldmState->window.base;
  234. BYTE const* const dictBase = extDict ? ldmState->window.dictBase : NULL;
  235. BYTE const* const dictStart = extDict ? dictBase + lowestIndex : NULL;
  236. BYTE const* const dictEnd = extDict ? dictBase + dictLimit : NULL;
  237. BYTE const* const lowPrefixPtr = base + dictLimit;
  238. /* Input bounds */
  239. BYTE const* const istart = (BYTE const*)src;
  240. BYTE const* const iend = istart + srcSize;
  241. BYTE const* const ilimit = iend - MAX(minMatchLength, HASH_READ_SIZE);
  242. /* Input positions */
  243. BYTE const* anchor = istart;
  244. BYTE const* ip = istart;
  245. /* Rolling hash */
  246. BYTE const* lastHashed = NULL;
  247. U64 rollingHash = 0;
  248. while (ip <= ilimit) {
  249. size_t mLength;
  250. U32 const current = (U32)(ip - base);
  251. size_t forwardMatchLength = 0, backwardMatchLength = 0;
  252. ldmEntry_t* bestEntry = NULL;
  253. if (ip != istart) {
  254. rollingHash = ZSTD_rollingHash_rotate(rollingHash, lastHashed[0],
  255. lastHashed[minMatchLength],
  256. hashPower);
  257. } else {
  258. rollingHash = ZSTD_rollingHash_compute(ip, minMatchLength);
  259. }
  260. lastHashed = ip;
  261. /* Do not insert and do not look for a match */
  262. if (ZSTD_ldm_getTag(rollingHash, hBits, hashRateLog) != ldmTagMask) {
  263. ip++;
  264. continue;
  265. }
  266. /* Get the best entry and compute the match lengths */
  267. {
  268. ldmEntry_t* const bucket =
  269. ZSTD_ldm_getBucket(ldmState,
  270. ZSTD_ldm_getSmallHash(rollingHash, hBits),
  271. *params);
  272. ldmEntry_t* cur;
  273. size_t bestMatchLength = 0;
  274. U32 const checksum = ZSTD_ldm_getChecksum(rollingHash, hBits);
  275. for (cur = bucket; cur < bucket + ldmBucketSize; ++cur) {
  276. size_t curForwardMatchLength, curBackwardMatchLength,
  277. curTotalMatchLength;
  278. if (cur->checksum != checksum || cur->offset <= lowestIndex) {
  279. continue;
  280. }
  281. if (extDict) {
  282. BYTE const* const curMatchBase =
  283. cur->offset < dictLimit ? dictBase : base;
  284. BYTE const* const pMatch = curMatchBase + cur->offset;
  285. BYTE const* const matchEnd =
  286. cur->offset < dictLimit ? dictEnd : iend;
  287. BYTE const* const lowMatchPtr =
  288. cur->offset < dictLimit ? dictStart : lowPrefixPtr;
  289. curForwardMatchLength = ZSTD_count_2segments(
  290. ip, pMatch, iend,
  291. matchEnd, lowPrefixPtr);
  292. if (curForwardMatchLength < minMatchLength) {
  293. continue;
  294. }
  295. curBackwardMatchLength =
  296. ZSTD_ldm_countBackwardsMatch(ip, anchor, pMatch,
  297. lowMatchPtr);
  298. curTotalMatchLength = curForwardMatchLength +
  299. curBackwardMatchLength;
  300. } else { /* !extDict */
  301. BYTE const* const pMatch = base + cur->offset;
  302. curForwardMatchLength = ZSTD_count(ip, pMatch, iend);
  303. if (curForwardMatchLength < minMatchLength) {
  304. continue;
  305. }
  306. curBackwardMatchLength =
  307. ZSTD_ldm_countBackwardsMatch(ip, anchor, pMatch,
  308. lowPrefixPtr);
  309. curTotalMatchLength = curForwardMatchLength +
  310. curBackwardMatchLength;
  311. }
  312. if (curTotalMatchLength > bestMatchLength) {
  313. bestMatchLength = curTotalMatchLength;
  314. forwardMatchLength = curForwardMatchLength;
  315. backwardMatchLength = curBackwardMatchLength;
  316. bestEntry = cur;
  317. }
  318. }
  319. }
  320. /* No match found -- continue searching */
  321. if (bestEntry == NULL) {
  322. ZSTD_ldm_makeEntryAndInsertByTag(ldmState, rollingHash,
  323. hBits, current,
  324. *params);
  325. ip++;
  326. continue;
  327. }
  328. /* Match found */
  329. mLength = forwardMatchLength + backwardMatchLength;
  330. ip -= backwardMatchLength;
  331. {
  332. /* Store the sequence:
  333. * ip = current - backwardMatchLength
  334. * The match is at (bestEntry->offset - backwardMatchLength)
  335. */
  336. U32 const matchIndex = bestEntry->offset;
  337. U32 const offset = current - matchIndex;
  338. rawSeq* const seq = rawSeqStore->seq + rawSeqStore->size;
  339. /* Out of sequence storage */
  340. if (rawSeqStore->size == rawSeqStore->capacity)
  341. return ERROR(dstSize_tooSmall);
  342. seq->litLength = (U32)(ip - anchor);
  343. seq->matchLength = (U32)mLength;
  344. seq->offset = offset;
  345. rawSeqStore->size++;
  346. }
  347. /* Insert the current entry into the hash table */
  348. ZSTD_ldm_makeEntryAndInsertByTag(ldmState, rollingHash, hBits,
  349. (U32)(lastHashed - base),
  350. *params);
  351. assert(ip + backwardMatchLength == lastHashed);
  352. /* Fill the hash table from lastHashed+1 to ip+mLength*/
  353. /* Heuristic: don't need to fill the entire table at end of block */
  354. if (ip + mLength <= ilimit) {
  355. rollingHash = ZSTD_ldm_fillLdmHashTable(
  356. ldmState, rollingHash, lastHashed,
  357. ip + mLength, base, hBits, *params);
  358. lastHashed = ip + mLength - 1;
  359. }
  360. ip += mLength;
  361. anchor = ip;
  362. }
  363. return iend - anchor;
  364. }
  365. /*! ZSTD_ldm_reduceTable() :
  366. * reduce table indexes by `reducerValue` */
  367. static void ZSTD_ldm_reduceTable(ldmEntry_t* const table, U32 const size,
  368. U32 const reducerValue)
  369. {
  370. U32 u;
  371. for (u = 0; u < size; u++) {
  372. if (table[u].offset < reducerValue) table[u].offset = 0;
  373. else table[u].offset -= reducerValue;
  374. }
  375. }
  376. size_t ZSTD_ldm_generateSequences(
  377. ldmState_t* ldmState, rawSeqStore_t* sequences,
  378. ldmParams_t const* params, void const* src, size_t srcSize)
  379. {
  380. U32 const maxDist = 1U << params->windowLog;
  381. BYTE const* const istart = (BYTE const*)src;
  382. BYTE const* const iend = istart + srcSize;
  383. size_t const kMaxChunkSize = 1 << 20;
  384. size_t const nbChunks = (srcSize / kMaxChunkSize) + ((srcSize % kMaxChunkSize) != 0);
  385. size_t chunk;
  386. size_t leftoverSize = 0;
  387. assert(ZSTD_CHUNKSIZE_MAX >= kMaxChunkSize);
  388. /* Check that ZSTD_window_update() has been called for this chunk prior
  389. * to passing it to this function.
  390. */
  391. assert(ldmState->window.nextSrc >= (BYTE const*)src + srcSize);
  392. /* The input could be very large (in zstdmt), so it must be broken up into
  393. * chunks to enforce the maximmum distance and handle overflow correction.
  394. */
  395. assert(sequences->pos <= sequences->size);
  396. assert(sequences->size <= sequences->capacity);
  397. for (chunk = 0; chunk < nbChunks && sequences->size < sequences->capacity; ++chunk) {
  398. BYTE const* const chunkStart = istart + chunk * kMaxChunkSize;
  399. size_t const remaining = (size_t)(iend - chunkStart);
  400. BYTE const *const chunkEnd =
  401. (remaining < kMaxChunkSize) ? iend : chunkStart + kMaxChunkSize;
  402. size_t const chunkSize = chunkEnd - chunkStart;
  403. size_t newLeftoverSize;
  404. size_t const prevSize = sequences->size;
  405. assert(chunkStart < iend);
  406. /* 1. Perform overflow correction if necessary. */
  407. if (ZSTD_window_needOverflowCorrection(ldmState->window, chunkEnd)) {
  408. U32 const ldmHSize = 1U << params->hashLog;
  409. U32 const correction = ZSTD_window_correctOverflow(
  410. &ldmState->window, /* cycleLog */ 0, maxDist, src);
  411. ZSTD_ldm_reduceTable(ldmState->hashTable, ldmHSize, correction);
  412. }
  413. /* 2. We enforce the maximum offset allowed.
  414. *
  415. * kMaxChunkSize should be small enough that we don't lose too much of
  416. * the window through early invalidation.
  417. * TODO: * Test the chunk size.
  418. * * Try invalidation after the sequence generation and test the
  419. * the offset against maxDist directly.
  420. */
  421. ZSTD_window_enforceMaxDist(&ldmState->window, chunkEnd, maxDist, NULL, NULL);
  422. /* 3. Generate the sequences for the chunk, and get newLeftoverSize. */
  423. newLeftoverSize = ZSTD_ldm_generateSequences_internal(
  424. ldmState, sequences, params, chunkStart, chunkSize);
  425. if (ZSTD_isError(newLeftoverSize))
  426. return newLeftoverSize;
  427. /* 4. We add the leftover literals from previous iterations to the first
  428. * newly generated sequence, or add the `newLeftoverSize` if none are
  429. * generated.
  430. */
  431. /* Prepend the leftover literals from the last call */
  432. if (prevSize < sequences->size) {
  433. sequences->seq[prevSize].litLength += (U32)leftoverSize;
  434. leftoverSize = newLeftoverSize;
  435. } else {
  436. assert(newLeftoverSize == chunkSize);
  437. leftoverSize += chunkSize;
  438. }
  439. }
  440. return 0;
  441. }
  442. void ZSTD_ldm_skipSequences(rawSeqStore_t* rawSeqStore, size_t srcSize, U32 const minMatch) {
  443. while (srcSize > 0 && rawSeqStore->pos < rawSeqStore->size) {
  444. rawSeq* seq = rawSeqStore->seq + rawSeqStore->pos;
  445. if (srcSize <= seq->litLength) {
  446. /* Skip past srcSize literals */
  447. seq->litLength -= (U32)srcSize;
  448. return;
  449. }
  450. srcSize -= seq->litLength;
  451. seq->litLength = 0;
  452. if (srcSize < seq->matchLength) {
  453. /* Skip past the first srcSize of the match */
  454. seq->matchLength -= (U32)srcSize;
  455. if (seq->matchLength < minMatch) {
  456. /* The match is too short, omit it */
  457. if (rawSeqStore->pos + 1 < rawSeqStore->size) {
  458. seq[1].litLength += seq[0].matchLength;
  459. }
  460. rawSeqStore->pos++;
  461. }
  462. return;
  463. }
  464. srcSize -= seq->matchLength;
  465. seq->matchLength = 0;
  466. rawSeqStore->pos++;
  467. }
  468. }
  469. /**
  470. * If the sequence length is longer than remaining then the sequence is split
  471. * between this block and the next.
  472. *
  473. * Returns the current sequence to handle, or if the rest of the block should
  474. * be literals, it returns a sequence with offset == 0.
  475. */
  476. static rawSeq maybeSplitSequence(rawSeqStore_t* rawSeqStore,
  477. U32 const remaining, U32 const minMatch)
  478. {
  479. rawSeq sequence = rawSeqStore->seq[rawSeqStore->pos];
  480. assert(sequence.offset > 0);
  481. /* Likely: No partial sequence */
  482. if (remaining >= sequence.litLength + sequence.matchLength) {
  483. rawSeqStore->pos++;
  484. return sequence;
  485. }
  486. /* Cut the sequence short (offset == 0 ==> rest is literals). */
  487. if (remaining <= sequence.litLength) {
  488. sequence.offset = 0;
  489. } else if (remaining < sequence.litLength + sequence.matchLength) {
  490. sequence.matchLength = remaining - sequence.litLength;
  491. if (sequence.matchLength < minMatch) {
  492. sequence.offset = 0;
  493. }
  494. }
  495. /* Skip past `remaining` bytes for the future sequences. */
  496. ZSTD_ldm_skipSequences(rawSeqStore, remaining, minMatch);
  497. return sequence;
  498. }
  499. size_t ZSTD_ldm_blockCompress(rawSeqStore_t* rawSeqStore,
  500. ZSTD_matchState_t* ms, seqStore_t* seqStore, U32 rep[ZSTD_REP_NUM],
  501. void const* src, size_t srcSize)
  502. {
  503. const ZSTD_compressionParameters* const cParams = &ms->cParams;
  504. unsigned const minMatch = cParams->minMatch;
  505. ZSTD_blockCompressor const blockCompressor =
  506. ZSTD_selectBlockCompressor(cParams->strategy, ZSTD_matchState_dictMode(ms));
  507. /* Input bounds */
  508. BYTE const* const istart = (BYTE const*)src;
  509. BYTE const* const iend = istart + srcSize;
  510. /* Input positions */
  511. BYTE const* ip = istart;
  512. DEBUGLOG(5, "ZSTD_ldm_blockCompress: srcSize=%zu", srcSize);
  513. assert(rawSeqStore->pos <= rawSeqStore->size);
  514. assert(rawSeqStore->size <= rawSeqStore->capacity);
  515. /* Loop through each sequence and apply the block compressor to the lits */
  516. while (rawSeqStore->pos < rawSeqStore->size && ip < iend) {
  517. /* maybeSplitSequence updates rawSeqStore->pos */
  518. rawSeq const sequence = maybeSplitSequence(rawSeqStore,
  519. (U32)(iend - ip), minMatch);
  520. int i;
  521. /* End signal */
  522. if (sequence.offset == 0)
  523. break;
  524. assert(sequence.offset <= (1U << cParams->windowLog));
  525. assert(ip + sequence.litLength + sequence.matchLength <= iend);
  526. /* Fill tables for block compressor */
  527. ZSTD_ldm_limitTableUpdate(ms, ip);
  528. ZSTD_ldm_fillFastTables(ms, ip);
  529. /* Run the block compressor */
  530. DEBUGLOG(5, "calling block compressor on segment of size %u", sequence.litLength);
  531. {
  532. size_t const newLitLength =
  533. blockCompressor(ms, seqStore, rep, ip, sequence.litLength);
  534. ip += sequence.litLength;
  535. /* Update the repcodes */
  536. for (i = ZSTD_REP_NUM - 1; i > 0; i--)
  537. rep[i] = rep[i-1];
  538. rep[0] = sequence.offset;
  539. /* Store the sequence */
  540. ZSTD_storeSeq(seqStore, newLitLength, ip - newLitLength,
  541. sequence.offset + ZSTD_REP_MOVE,
  542. sequence.matchLength - MINMATCH);
  543. ip += sequence.matchLength;
  544. }
  545. }
  546. /* Fill the tables for the block compressor */
  547. ZSTD_ldm_limitTableUpdate(ms, ip);
  548. ZSTD_ldm_fillFastTables(ms, ip);
  549. /* Compress the last literals */
  550. return blockCompressor(ms, seqStore, rep, ip, iend - ip);
  551. }