MatchFinder.c 19 KB

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