Team Fortress 2 Source Code as on 22/4/2020
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

763 lines
19 KiB

  1. /* LzFind.c -- Match finder for LZ algorithms
  2. 2009-04-22 : Igor Pavlov : Public domain */
  3. #include "Precomp.h"
  4. #include <string.h>
  5. #include "LzFind.h"
  6. #include "LzHash.h"
  7. #define kEmptyHashValue 0
  8. #define kMaxValForNormalize ((UInt32)0xFFFFFFFF)
  9. #define kNormalizeStepMin (1 << 10) /* it must be power of 2 */
  10. #define kNormalizeMask (~(kNormalizeStepMin - 1))
  11. #define kMaxHistorySize ((UInt32)3 << 30)
  12. #define kStartMaxLen 3
  13. static void LzInWindow_Free(CMatchFinder *p, ISzAlloc *alloc)
  14. {
  15. if (!p->directInput)
  16. {
  17. alloc->Free(alloc, p->bufferBase);
  18. p->bufferBase = 0;
  19. }
  20. }
  21. /* keepSizeBefore + keepSizeAfter + keepSizeReserv must be < 4G) */
  22. static int LzInWindow_Create(CMatchFinder *p, UInt32 keepSizeReserv, ISzAlloc *alloc)
  23. {
  24. UInt32 blockSize = p->keepSizeBefore + p->keepSizeAfter + keepSizeReserv;
  25. if (p->directInput)
  26. {
  27. p->blockSize = blockSize;
  28. return 1;
  29. }
  30. if (p->bufferBase == 0 || p->blockSize != blockSize)
  31. {
  32. LzInWindow_Free(p, alloc);
  33. p->blockSize = blockSize;
  34. p->bufferBase = (Byte *)alloc->Alloc(alloc, (size_t)blockSize);
  35. }
  36. return (p->bufferBase != 0);
  37. }
  38. Byte *MatchFinder_GetPointerToCurrentPos(CMatchFinder *p) { return p->buffer; }
  39. Byte MatchFinder_GetIndexByte(CMatchFinder *p, Int32 index) { return p->buffer[index]; }
  40. UInt32 MatchFinder_GetNumAvailableBytes(CMatchFinder *p) { return p->streamPos - p->pos; }
  41. void MatchFinder_ReduceOffsets(CMatchFinder *p, UInt32 subValue)
  42. {
  43. p->posLimit -= subValue;
  44. p->pos -= subValue;
  45. p->streamPos -= subValue;
  46. }
  47. static void MatchFinder_ReadBlock(CMatchFinder *p)
  48. {
  49. if (p->streamEndWasReached || p->result != SZ_OK)
  50. return;
  51. if (p->directInput)
  52. {
  53. UInt32 curSize = 0xFFFFFFFF - p->streamPos;
  54. if (curSize > p->directInputRem)
  55. curSize = (UInt32)p->directInputRem;
  56. p->directInputRem -= curSize;
  57. p->streamPos += curSize;
  58. if (p->directInputRem == 0)
  59. p->streamEndWasReached = 1;
  60. return;
  61. }
  62. for (;;)
  63. {
  64. Byte *dest = p->buffer + (p->streamPos - p->pos);
  65. size_t size = (p->bufferBase + p->blockSize - dest);
  66. if (size == 0)
  67. return;
  68. p->result = p->stream->Read(p->stream, dest, &size);
  69. if (p->result != SZ_OK)
  70. return;
  71. if (size == 0)
  72. {
  73. p->streamEndWasReached = 1;
  74. return;
  75. }
  76. p->streamPos += (UInt32)size;
  77. if (p->streamPos - p->pos > p->keepSizeAfter)
  78. return;
  79. }
  80. }
  81. void MatchFinder_MoveBlock(CMatchFinder *p)
  82. {
  83. memmove(p->bufferBase,
  84. p->buffer - p->keepSizeBefore,
  85. (size_t)(p->streamPos - p->pos + p->keepSizeBefore));
  86. p->buffer = p->bufferBase + p->keepSizeBefore;
  87. }
  88. int MatchFinder_NeedMove(CMatchFinder *p)
  89. {
  90. if (p->directInput)
  91. return 0;
  92. /* if (p->streamEndWasReached) return 0; */
  93. return ((size_t)(p->bufferBase + p->blockSize - p->buffer) <= p->keepSizeAfter);
  94. }
  95. void MatchFinder_ReadIfRequired(CMatchFinder *p)
  96. {
  97. if (p->streamEndWasReached)
  98. return;
  99. if (p->keepSizeAfter >= p->streamPos - p->pos)
  100. MatchFinder_ReadBlock(p);
  101. }
  102. static void MatchFinder_CheckAndMoveAndRead(CMatchFinder *p)
  103. {
  104. if (MatchFinder_NeedMove(p))
  105. MatchFinder_MoveBlock(p);
  106. MatchFinder_ReadBlock(p);
  107. }
  108. static void MatchFinder_SetDefaultSettings(CMatchFinder *p)
  109. {
  110. p->cutValue = 32;
  111. p->btMode = 1;
  112. p->numHashBytes = 4;
  113. p->bigHash = 0;
  114. }
  115. #define kCrcPoly 0xEDB88320
  116. void MatchFinder_Construct(CMatchFinder *p)
  117. {
  118. UInt32 i;
  119. p->bufferBase = 0;
  120. p->directInput = 0;
  121. p->hash = 0;
  122. MatchFinder_SetDefaultSettings(p);
  123. for (i = 0; i < 256; i++)
  124. {
  125. UInt32 r = i;
  126. int j;
  127. for (j = 0; j < 8; j++)
  128. r = (r >> 1) ^ (kCrcPoly & ~((r & 1) - 1));
  129. p->crc[i] = r;
  130. }
  131. }
  132. static void MatchFinder_FreeThisClassMemory(CMatchFinder *p, ISzAlloc *alloc)
  133. {
  134. alloc->Free(alloc, p->hash);
  135. p->hash = 0;
  136. }
  137. void MatchFinder_Free(CMatchFinder *p, ISzAlloc *alloc)
  138. {
  139. MatchFinder_FreeThisClassMemory(p, alloc);
  140. LzInWindow_Free(p, alloc);
  141. }
  142. static CLzRef* AllocRefs(UInt32 num, ISzAlloc *alloc)
  143. {
  144. size_t sizeInBytes = (size_t)num * sizeof(CLzRef);
  145. if (sizeInBytes / sizeof(CLzRef) != num)
  146. return 0;
  147. return (CLzRef *)alloc->Alloc(alloc, sizeInBytes);
  148. }
  149. int MatchFinder_Create(CMatchFinder *p, UInt32 historySize,
  150. UInt32 keepAddBufferBefore, UInt32 matchMaxLen, UInt32 keepAddBufferAfter,
  151. ISzAlloc *alloc)
  152. {
  153. UInt32 sizeReserv;
  154. if (historySize > kMaxHistorySize)
  155. {
  156. MatchFinder_Free(p, alloc);
  157. return 0;
  158. }
  159. sizeReserv = historySize >> 1;
  160. if (historySize > ((UInt32)2 << 30))
  161. sizeReserv = historySize >> 2;
  162. sizeReserv += (keepAddBufferBefore + matchMaxLen + keepAddBufferAfter) / 2 + (1 << 19);
  163. p->keepSizeBefore = historySize + keepAddBufferBefore + 1;
  164. p->keepSizeAfter = matchMaxLen + keepAddBufferAfter;
  165. /* we need one additional byte, since we use MoveBlock after pos++ and before dictionary using */
  166. if (LzInWindow_Create(p, sizeReserv, alloc))
  167. {
  168. UInt32 newCyclicBufferSize = historySize + 1;
  169. UInt32 hs;
  170. p->matchMaxLen = matchMaxLen;
  171. {
  172. p->fixedHashSize = 0;
  173. if (p->numHashBytes == 2)
  174. hs = (1 << 16) - 1;
  175. else
  176. {
  177. hs = historySize - 1;
  178. hs |= (hs >> 1);
  179. hs |= (hs >> 2);
  180. hs |= (hs >> 4);
  181. hs |= (hs >> 8);
  182. hs >>= 1;
  183. hs |= 0xFFFF; /* don't change it! It's required for Deflate */
  184. if (hs > (1 << 24))
  185. {
  186. if (p->numHashBytes == 3)
  187. hs = (1 << 24) - 1;
  188. else
  189. hs >>= 1;
  190. }
  191. }
  192. p->hashMask = hs;
  193. hs++;
  194. if (p->numHashBytes > 2) p->fixedHashSize += kHash2Size;
  195. if (p->numHashBytes > 3) p->fixedHashSize += kHash3Size;
  196. if (p->numHashBytes > 4) p->fixedHashSize += kHash4Size;
  197. hs += p->fixedHashSize;
  198. }
  199. {
  200. UInt32 prevSize = p->hashSizeSum + p->numSons;
  201. UInt32 newSize;
  202. p->historySize = historySize;
  203. p->hashSizeSum = hs;
  204. p->cyclicBufferSize = newCyclicBufferSize;
  205. p->numSons = (p->btMode ? newCyclicBufferSize * 2 : newCyclicBufferSize);
  206. newSize = p->hashSizeSum + p->numSons;
  207. if (p->hash != 0 && prevSize == newSize)
  208. return 1;
  209. MatchFinder_FreeThisClassMemory(p, alloc);
  210. p->hash = AllocRefs(newSize, alloc);
  211. if (p->hash != 0)
  212. {
  213. p->son = p->hash + p->hashSizeSum;
  214. return 1;
  215. }
  216. }
  217. }
  218. MatchFinder_Free(p, alloc);
  219. return 0;
  220. }
  221. static void MatchFinder_SetLimits(CMatchFinder *p)
  222. {
  223. UInt32 limit = kMaxValForNormalize - p->pos;
  224. UInt32 limit2 = p->cyclicBufferSize - p->cyclicBufferPos;
  225. if (limit2 < limit)
  226. limit = limit2;
  227. limit2 = p->streamPos - p->pos;
  228. if (limit2 <= p->keepSizeAfter)
  229. {
  230. if (limit2 > 0)
  231. limit2 = 1;
  232. }
  233. else
  234. limit2 -= p->keepSizeAfter;
  235. if (limit2 < limit)
  236. limit = limit2;
  237. {
  238. UInt32 lenLimit = p->streamPos - p->pos;
  239. if (lenLimit > p->matchMaxLen)
  240. lenLimit = p->matchMaxLen;
  241. p->lenLimit = lenLimit;
  242. }
  243. p->posLimit = p->pos + limit;
  244. }
  245. void MatchFinder_Init(CMatchFinder *p)
  246. {
  247. UInt32 i;
  248. for (i = 0; i < p->hashSizeSum; i++)
  249. p->hash[i] = kEmptyHashValue;
  250. p->cyclicBufferPos = 0;
  251. p->buffer = p->bufferBase;
  252. p->pos = p->streamPos = p->cyclicBufferSize;
  253. p->result = SZ_OK;
  254. p->streamEndWasReached = 0;
  255. MatchFinder_ReadBlock(p);
  256. MatchFinder_SetLimits(p);
  257. }
  258. static UInt32 MatchFinder_GetSubValue(CMatchFinder *p)
  259. {
  260. return (p->pos - p->historySize - 1) & kNormalizeMask;
  261. }
  262. void MatchFinder_Normalize3(UInt32 subValue, CLzRef *items, UInt32 numItems)
  263. {
  264. UInt32 i;
  265. for (i = 0; i < numItems; i++)
  266. {
  267. UInt32 value = items[i];
  268. if (value <= subValue)
  269. value = kEmptyHashValue;
  270. else
  271. value -= subValue;
  272. items[i] = value;
  273. }
  274. }
  275. static void MatchFinder_Normalize(CMatchFinder *p)
  276. {
  277. UInt32 subValue = MatchFinder_GetSubValue(p);
  278. MatchFinder_Normalize3(subValue, p->hash, p->hashSizeSum + p->numSons);
  279. MatchFinder_ReduceOffsets(p, subValue);
  280. }
  281. static void MatchFinder_CheckLimits(CMatchFinder *p)
  282. {
  283. if (p->pos == kMaxValForNormalize)
  284. MatchFinder_Normalize(p);
  285. if (!p->streamEndWasReached && p->keepSizeAfter == p->streamPos - p->pos)
  286. MatchFinder_CheckAndMoveAndRead(p);
  287. if (p->cyclicBufferPos == p->cyclicBufferSize)
  288. p->cyclicBufferPos = 0;
  289. MatchFinder_SetLimits(p);
  290. }
  291. static UInt32 * Hc_GetMatchesSpec(UInt32 lenLimit, UInt32 curMatch, UInt32 pos, const Byte *cur, CLzRef *son,
  292. UInt32 _cyclicBufferPos, UInt32 _cyclicBufferSize, UInt32 cutValue,
  293. UInt32 *distances, UInt32 maxLen)
  294. {
  295. son[_cyclicBufferPos] = curMatch;
  296. for (;;)
  297. {
  298. UInt32 delta = pos - curMatch;
  299. if (cutValue-- == 0 || delta >= _cyclicBufferSize)
  300. return distances;
  301. {
  302. const Byte *pb = cur - delta;
  303. curMatch = son[_cyclicBufferPos - delta + ((delta > _cyclicBufferPos) ? _cyclicBufferSize : 0)];
  304. if (pb[maxLen] == cur[maxLen] && *pb == *cur)
  305. {
  306. UInt32 len = 0;
  307. while (++len != lenLimit)
  308. if (pb[len] != cur[len])
  309. break;
  310. if (maxLen < len)
  311. {
  312. *distances++ = maxLen = len;
  313. *distances++ = delta - 1;
  314. if (len == lenLimit)
  315. return distances;
  316. }
  317. }
  318. }
  319. }
  320. }
  321. UInt32 * GetMatchesSpec1(UInt32 lenLimit, UInt32 curMatch, UInt32 pos, const Byte *cur, CLzRef *son,
  322. UInt32 _cyclicBufferPos, UInt32 _cyclicBufferSize, UInt32 cutValue,
  323. UInt32 *distances, UInt32 maxLen)
  324. {
  325. CLzRef *ptr0 = son + (_cyclicBufferPos << 1) + 1;
  326. CLzRef *ptr1 = son + (_cyclicBufferPos << 1);
  327. UInt32 len0 = 0, len1 = 0;
  328. for (;;)
  329. {
  330. UInt32 delta = pos - curMatch;
  331. if (cutValue-- == 0 || delta >= _cyclicBufferSize)
  332. {
  333. *ptr0 = *ptr1 = kEmptyHashValue;
  334. return distances;
  335. }
  336. {
  337. CLzRef *pair = son + ((_cyclicBufferPos - delta + ((delta > _cyclicBufferPos) ? _cyclicBufferSize : 0)) << 1);
  338. const Byte *pb = cur - delta;
  339. UInt32 len = (len0 < len1 ? len0 : len1);
  340. if (pb[len] == cur[len])
  341. {
  342. if (++len != lenLimit && pb[len] == cur[len])
  343. while (++len != lenLimit)
  344. if (pb[len] != cur[len])
  345. break;
  346. if (maxLen < len)
  347. {
  348. *distances++ = maxLen = len;
  349. *distances++ = delta - 1;
  350. if (len == lenLimit)
  351. {
  352. *ptr1 = pair[0];
  353. *ptr0 = pair[1];
  354. return distances;
  355. }
  356. }
  357. }
  358. if (pb[len] < cur[len])
  359. {
  360. *ptr1 = curMatch;
  361. ptr1 = pair + 1;
  362. curMatch = *ptr1;
  363. len1 = len;
  364. }
  365. else
  366. {
  367. *ptr0 = curMatch;
  368. ptr0 = pair;
  369. curMatch = *ptr0;
  370. len0 = len;
  371. }
  372. }
  373. }
  374. }
  375. static void SkipMatchesSpec(UInt32 lenLimit, UInt32 curMatch, UInt32 pos, const Byte *cur, CLzRef *son,
  376. UInt32 _cyclicBufferPos, UInt32 _cyclicBufferSize, UInt32 cutValue)
  377. {
  378. CLzRef *ptr0 = son + (_cyclicBufferPos << 1) + 1;
  379. CLzRef *ptr1 = son + (_cyclicBufferPos << 1);
  380. UInt32 len0 = 0, len1 = 0;
  381. for (;;)
  382. {
  383. UInt32 delta = pos - curMatch;
  384. if (cutValue-- == 0 || delta >= _cyclicBufferSize)
  385. {
  386. *ptr0 = *ptr1 = kEmptyHashValue;
  387. return;
  388. }
  389. {
  390. CLzRef *pair = son + ((_cyclicBufferPos - delta + ((delta > _cyclicBufferPos) ? _cyclicBufferSize : 0)) << 1);
  391. const Byte *pb = cur - delta;
  392. UInt32 len = (len0 < len1 ? len0 : len1);
  393. if (pb[len] == cur[len])
  394. {
  395. while (++len != lenLimit)
  396. if (pb[len] != cur[len])
  397. break;
  398. {
  399. if (len == lenLimit)
  400. {
  401. *ptr1 = pair[0];
  402. *ptr0 = pair[1];
  403. return;
  404. }
  405. }
  406. }
  407. if (pb[len] < cur[len])
  408. {
  409. *ptr1 = curMatch;
  410. ptr1 = pair + 1;
  411. curMatch = *ptr1;
  412. len1 = len;
  413. }
  414. else
  415. {
  416. *ptr0 = curMatch;
  417. ptr0 = pair;
  418. curMatch = *ptr0;
  419. len0 = len;
  420. }
  421. }
  422. }
  423. }
  424. #define MOVE_POS \
  425. ++p->cyclicBufferPos; \
  426. p->buffer++; \
  427. if (++p->pos == p->posLimit) MatchFinder_CheckLimits(p);
  428. #define MOVE_POS_RET MOVE_POS return offset;
  429. static void MatchFinder_MovePos(CMatchFinder *p) { MOVE_POS; }
  430. #define GET_MATCHES_HEADER2(minLen, ret_op) \
  431. UInt32 lenLimit; UInt32 hashValue; const Byte *cur; UInt32 curMatch; \
  432. lenLimit = p->lenLimit; { if (lenLimit < minLen) { MatchFinder_MovePos(p); ret_op; }} \
  433. cur = p->buffer;
  434. #define GET_MATCHES_HEADER(minLen) GET_MATCHES_HEADER2(minLen, return 0)
  435. #define SKIP_HEADER(minLen) GET_MATCHES_HEADER2(minLen, continue)
  436. #define MF_PARAMS(p) p->pos, p->buffer, p->son, p->cyclicBufferPos, p->cyclicBufferSize, p->cutValue
  437. #define GET_MATCHES_FOOTER(offset, maxLen) \
  438. offset = (UInt32)(GetMatchesSpec1(lenLimit, curMatch, MF_PARAMS(p), \
  439. distances + offset, maxLen) - distances); MOVE_POS_RET;
  440. #define SKIP_FOOTER \
  441. SkipMatchesSpec(lenLimit, curMatch, MF_PARAMS(p)); MOVE_POS;
  442. static UInt32 Bt2_MatchFinder_GetMatches(CMatchFinder *p, UInt32 *distances)
  443. {
  444. UInt32 offset;
  445. GET_MATCHES_HEADER(2)
  446. HASH2_CALC;
  447. curMatch = p->hash[hashValue];
  448. p->hash[hashValue] = p->pos;
  449. offset = 0;
  450. GET_MATCHES_FOOTER(offset, 1)
  451. }
  452. UInt32 Bt3Zip_MatchFinder_GetMatches(CMatchFinder *p, UInt32 *distances)
  453. {
  454. UInt32 offset;
  455. GET_MATCHES_HEADER(3)
  456. HASH_ZIP_CALC;
  457. curMatch = p->hash[hashValue];
  458. p->hash[hashValue] = p->pos;
  459. offset = 0;
  460. GET_MATCHES_FOOTER(offset, 2)
  461. }
  462. static UInt32 Bt3_MatchFinder_GetMatches(CMatchFinder *p, UInt32 *distances)
  463. {
  464. UInt32 hash2Value, delta2, maxLen, offset;
  465. GET_MATCHES_HEADER(3)
  466. HASH3_CALC;
  467. delta2 = p->pos - p->hash[hash2Value];
  468. curMatch = p->hash[kFix3HashSize + hashValue];
  469. p->hash[hash2Value] =
  470. p->hash[kFix3HashSize + hashValue] = p->pos;
  471. maxLen = 2;
  472. offset = 0;
  473. if (delta2 < p->cyclicBufferSize && *(cur - delta2) == *cur)
  474. {
  475. for (; maxLen != lenLimit; maxLen++)
  476. if (cur[(ptrdiff_t)maxLen - delta2] != cur[maxLen])
  477. break;
  478. distances[0] = maxLen;
  479. distances[1] = delta2 - 1;
  480. offset = 2;
  481. if (maxLen == lenLimit)
  482. {
  483. SkipMatchesSpec(lenLimit, curMatch, MF_PARAMS(p));
  484. MOVE_POS_RET;
  485. }
  486. }
  487. GET_MATCHES_FOOTER(offset, maxLen)
  488. }
  489. static UInt32 Bt4_MatchFinder_GetMatches(CMatchFinder *p, UInt32 *distances)
  490. {
  491. UInt32 hash2Value, hash3Value, delta2, delta3, maxLen, offset;
  492. GET_MATCHES_HEADER(4)
  493. HASH4_CALC;
  494. delta2 = p->pos - p->hash[ hash2Value];
  495. delta3 = p->pos - p->hash[kFix3HashSize + hash3Value];
  496. curMatch = p->hash[kFix4HashSize + hashValue];
  497. p->hash[ hash2Value] =
  498. p->hash[kFix3HashSize + hash3Value] =
  499. p->hash[kFix4HashSize + hashValue] = p->pos;
  500. maxLen = 1;
  501. offset = 0;
  502. if (delta2 < p->cyclicBufferSize && *(cur - delta2) == *cur)
  503. {
  504. distances[0] = maxLen = 2;
  505. distances[1] = delta2 - 1;
  506. offset = 2;
  507. }
  508. if (delta2 != delta3 && delta3 < p->cyclicBufferSize && *(cur - delta3) == *cur)
  509. {
  510. maxLen = 3;
  511. distances[offset + 1] = delta3 - 1;
  512. offset += 2;
  513. delta2 = delta3;
  514. }
  515. if (offset != 0)
  516. {
  517. for (; maxLen != lenLimit; maxLen++)
  518. if (cur[(ptrdiff_t)maxLen - delta2] != cur[maxLen])
  519. break;
  520. distances[offset - 2] = maxLen;
  521. if (maxLen == lenLimit)
  522. {
  523. SkipMatchesSpec(lenLimit, curMatch, MF_PARAMS(p));
  524. MOVE_POS_RET;
  525. }
  526. }
  527. if (maxLen < 3)
  528. maxLen = 3;
  529. GET_MATCHES_FOOTER(offset, maxLen)
  530. }
  531. static UInt32 Hc4_MatchFinder_GetMatches(CMatchFinder *p, UInt32 *distances)
  532. {
  533. UInt32 hash2Value, hash3Value, delta2, delta3, maxLen, offset;
  534. GET_MATCHES_HEADER(4)
  535. HASH4_CALC;
  536. delta2 = p->pos - p->hash[ hash2Value];
  537. delta3 = p->pos - p->hash[kFix3HashSize + hash3Value];
  538. curMatch = p->hash[kFix4HashSize + hashValue];
  539. p->hash[ hash2Value] =
  540. p->hash[kFix3HashSize + hash3Value] =
  541. p->hash[kFix4HashSize + hashValue] = p->pos;
  542. maxLen = 1;
  543. offset = 0;
  544. if (delta2 < p->cyclicBufferSize && *(cur - delta2) == *cur)
  545. {
  546. distances[0] = maxLen = 2;
  547. distances[1] = delta2 - 1;
  548. offset = 2;
  549. }
  550. if (delta2 != delta3 && delta3 < p->cyclicBufferSize && *(cur - delta3) == *cur)
  551. {
  552. maxLen = 3;
  553. distances[offset + 1] = delta3 - 1;
  554. offset += 2;
  555. delta2 = delta3;
  556. }
  557. if (offset != 0)
  558. {
  559. for (; maxLen != lenLimit; maxLen++)
  560. if (cur[(ptrdiff_t)maxLen - delta2] != cur[maxLen])
  561. break;
  562. distances[offset - 2] = maxLen;
  563. if (maxLen == lenLimit)
  564. {
  565. p->son[p->cyclicBufferPos] = curMatch;
  566. MOVE_POS_RET;
  567. }
  568. }
  569. if (maxLen < 3)
  570. maxLen = 3;
  571. offset = (UInt32)(Hc_GetMatchesSpec(lenLimit, curMatch, MF_PARAMS(p),
  572. distances + offset, maxLen) - (distances));
  573. MOVE_POS_RET
  574. }
  575. UInt32 Hc3Zip_MatchFinder_GetMatches(CMatchFinder *p, UInt32 *distances)
  576. {
  577. UInt32 offset;
  578. GET_MATCHES_HEADER(3)
  579. HASH_ZIP_CALC;
  580. curMatch = p->hash[hashValue];
  581. p->hash[hashValue] = p->pos;
  582. offset = (UInt32)(Hc_GetMatchesSpec(lenLimit, curMatch, MF_PARAMS(p),
  583. distances, 2) - (distances));
  584. MOVE_POS_RET
  585. }
  586. static void Bt2_MatchFinder_Skip(CMatchFinder *p, UInt32 num)
  587. {
  588. do
  589. {
  590. SKIP_HEADER(2)
  591. HASH2_CALC;
  592. curMatch = p->hash[hashValue];
  593. p->hash[hashValue] = p->pos;
  594. SKIP_FOOTER
  595. }
  596. while (--num != 0);
  597. }
  598. void Bt3Zip_MatchFinder_Skip(CMatchFinder *p, UInt32 num)
  599. {
  600. do
  601. {
  602. SKIP_HEADER(3)
  603. HASH_ZIP_CALC;
  604. curMatch = p->hash[hashValue];
  605. p->hash[hashValue] = p->pos;
  606. SKIP_FOOTER
  607. }
  608. while (--num != 0);
  609. }
  610. static void Bt3_MatchFinder_Skip(CMatchFinder *p, UInt32 num)
  611. {
  612. do
  613. {
  614. UInt32 hash2Value;
  615. SKIP_HEADER(3)
  616. HASH3_CALC;
  617. curMatch = p->hash[kFix3HashSize + hashValue];
  618. p->hash[hash2Value] =
  619. p->hash[kFix3HashSize + hashValue] = p->pos;
  620. SKIP_FOOTER
  621. }
  622. while (--num != 0);
  623. }
  624. static void Bt4_MatchFinder_Skip(CMatchFinder *p, UInt32 num)
  625. {
  626. do
  627. {
  628. UInt32 hash2Value, hash3Value;
  629. SKIP_HEADER(4)
  630. HASH4_CALC;
  631. curMatch = p->hash[kFix4HashSize + hashValue];
  632. p->hash[ hash2Value] =
  633. p->hash[kFix3HashSize + hash3Value] = p->pos;
  634. p->hash[kFix4HashSize + hashValue] = p->pos;
  635. SKIP_FOOTER
  636. }
  637. while (--num != 0);
  638. }
  639. static void Hc4_MatchFinder_Skip(CMatchFinder *p, UInt32 num)
  640. {
  641. do
  642. {
  643. UInt32 hash2Value, hash3Value;
  644. SKIP_HEADER(4)
  645. HASH4_CALC;
  646. curMatch = p->hash[kFix4HashSize + hashValue];
  647. p->hash[ hash2Value] =
  648. p->hash[kFix3HashSize + hash3Value] =
  649. p->hash[kFix4HashSize + hashValue] = p->pos;
  650. p->son[p->cyclicBufferPos] = curMatch;
  651. MOVE_POS
  652. }
  653. while (--num != 0);
  654. }
  655. void Hc3Zip_MatchFinder_Skip(CMatchFinder *p, UInt32 num)
  656. {
  657. do
  658. {
  659. SKIP_HEADER(3)
  660. HASH_ZIP_CALC;
  661. curMatch = p->hash[hashValue];
  662. p->hash[hashValue] = p->pos;
  663. p->son[p->cyclicBufferPos] = curMatch;
  664. MOVE_POS
  665. }
  666. while (--num != 0);
  667. }
  668. void MatchFinder_CreateVTable(CMatchFinder *p, IMatchFinder *vTable)
  669. {
  670. vTable->Init = (Mf_Init_Func)MatchFinder_Init;
  671. vTable->GetIndexByte = (Mf_GetIndexByte_Func)MatchFinder_GetIndexByte;
  672. vTable->GetNumAvailableBytes = (Mf_GetNumAvailableBytes_Func)MatchFinder_GetNumAvailableBytes;
  673. vTable->GetPointerToCurrentPos = (Mf_GetPointerToCurrentPos_Func)MatchFinder_GetPointerToCurrentPos;
  674. if (!p->btMode)
  675. {
  676. vTable->GetMatches = (Mf_GetMatches_Func)Hc4_MatchFinder_GetMatches;
  677. vTable->Skip = (Mf_Skip_Func)Hc4_MatchFinder_Skip;
  678. }
  679. else if (p->numHashBytes == 2)
  680. {
  681. vTable->GetMatches = (Mf_GetMatches_Func)Bt2_MatchFinder_GetMatches;
  682. vTable->Skip = (Mf_Skip_Func)Bt2_MatchFinder_Skip;
  683. }
  684. else if (p->numHashBytes == 3)
  685. {
  686. vTable->GetMatches = (Mf_GetMatches_Func)Bt3_MatchFinder_GetMatches;
  687. vTable->Skip = (Mf_Skip_Func)Bt3_MatchFinder_Skip;
  688. }
  689. else
  690. {
  691. vTable->GetMatches = (Mf_GetMatches_Func)Bt4_MatchFinder_GetMatches;
  692. vTable->Skip = (Mf_Skip_Func)Bt4_MatchFinder_Skip;
  693. }
  694. }