LzFind.c 27 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127
  1. /* LzFind.c -- Match finder for LZ algorithms
  2. 2018-07-08 : 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 (~(UInt32)(kNormalizeStepMin - 1))
  11. #define kMaxHistorySize ((UInt32)7 << 29)
  12. #define kStartMaxLen 3
  13. static void LzInWindow_Free(CMatchFinder *p, ISzAllocPtr alloc)
  14. {
  15. if (!p->directInput)
  16. {
  17. ISzAlloc_Free(alloc, p->bufferBase);
  18. p->bufferBase = NULL;
  19. }
  20. }
  21. /* keepSizeBefore + keepSizeAfter + keepSizeReserv must be < 4G) */
  22. static int LzInWindow_Create(CMatchFinder *p, UInt32 keepSizeReserv, ISzAllocPtr 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 || p->blockSize != blockSize)
  31. {
  32. LzInWindow_Free(p, alloc);
  33. p->blockSize = blockSize;
  34. p->bufferBase = (Byte *)ISzAlloc_Alloc(alloc, (size_t)blockSize);
  35. }
  36. return (p->bufferBase != NULL);
  37. }
  38. Byte *MatchFinder_GetPointerToCurrentPos(CMatchFinder *p) { return p->buffer; }
  39. UInt32 MatchFinder_GetNumAvailableBytes(CMatchFinder *p) { return p->streamPos - p->pos; }
  40. void MatchFinder_ReduceOffsets(CMatchFinder *p, UInt32 subValue)
  41. {
  42. p->posLimit -= subValue;
  43. p->pos -= subValue;
  44. p->streamPos -= subValue;
  45. }
  46. static void MatchFinder_ReadBlock(CMatchFinder *p)
  47. {
  48. if (p->streamEndWasReached || p->result != SZ_OK)
  49. return;
  50. /* We use (p->streamPos - p->pos) value. (p->streamPos < p->pos) is allowed. */
  51. if (p->directInput)
  52. {
  53. UInt32 curSize = 0xFFFFFFFF - (p->streamPos - p->pos);
  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 = ISeqInStream_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. unsigned i;
  119. p->bufferBase = NULL;
  120. p->directInput = 0;
  121. p->hash = NULL;
  122. p->expectedDataSize = (UInt64)(Int64)-1;
  123. MatchFinder_SetDefaultSettings(p);
  124. for (i = 0; i < 256; i++)
  125. {
  126. UInt32 r = (UInt32)i;
  127. unsigned j;
  128. for (j = 0; j < 8; j++)
  129. r = (r >> 1) ^ (kCrcPoly & ((UInt32)0 - (r & 1)));
  130. p->crc[i] = r;
  131. }
  132. }
  133. static void MatchFinder_FreeThisClassMemory(CMatchFinder *p, ISzAllocPtr alloc)
  134. {
  135. ISzAlloc_Free(alloc, p->hash);
  136. p->hash = NULL;
  137. }
  138. void MatchFinder_Free(CMatchFinder *p, ISzAllocPtr alloc)
  139. {
  140. MatchFinder_FreeThisClassMemory(p, alloc);
  141. LzInWindow_Free(p, alloc);
  142. }
  143. static CLzRef* AllocRefs(size_t num, ISzAllocPtr alloc)
  144. {
  145. size_t sizeInBytes = (size_t)num * sizeof(CLzRef);
  146. if (sizeInBytes / sizeof(CLzRef) != num)
  147. return NULL;
  148. return (CLzRef *)ISzAlloc_Alloc(alloc, sizeInBytes);
  149. }
  150. int MatchFinder_Create(CMatchFinder *p, UInt32 historySize,
  151. UInt32 keepAddBufferBefore, UInt32 matchMaxLen, UInt32 keepAddBufferAfter,
  152. ISzAllocPtr alloc)
  153. {
  154. UInt32 sizeReserv;
  155. if (historySize > kMaxHistorySize)
  156. {
  157. MatchFinder_Free(p, alloc);
  158. return 0;
  159. }
  160. sizeReserv = historySize >> 1;
  161. if (historySize >= ((UInt32)3 << 30)) sizeReserv = historySize >> 3;
  162. else if (historySize >= ((UInt32)2 << 30)) sizeReserv = historySize >> 2;
  163. sizeReserv += (keepAddBufferBefore + matchMaxLen + keepAddBufferAfter) / 2 + (1 << 19);
  164. p->keepSizeBefore = historySize + keepAddBufferBefore + 1;
  165. p->keepSizeAfter = matchMaxLen + keepAddBufferAfter;
  166. /* we need one additional byte, since we use MoveBlock after pos++ and before dictionary using */
  167. if (LzInWindow_Create(p, sizeReserv, alloc))
  168. {
  169. UInt32 newCyclicBufferSize = historySize + 1;
  170. UInt32 hs;
  171. p->matchMaxLen = matchMaxLen;
  172. {
  173. p->fixedHashSize = 0;
  174. if (p->numHashBytes == 2)
  175. hs = (1 << 16) - 1;
  176. else
  177. {
  178. hs = historySize;
  179. if (hs > p->expectedDataSize)
  180. hs = (UInt32)p->expectedDataSize;
  181. if (hs != 0)
  182. hs--;
  183. hs |= (hs >> 1);
  184. hs |= (hs >> 2);
  185. hs |= (hs >> 4);
  186. hs |= (hs >> 8);
  187. hs >>= 1;
  188. hs |= 0xFFFF; /* don't change it! It's required for Deflate */
  189. if (hs > (1 << 24))
  190. {
  191. if (p->numHashBytes == 3)
  192. hs = (1 << 24) - 1;
  193. else
  194. hs >>= 1;
  195. /* if (bigHash) mode, GetHeads4b() in LzFindMt.c needs (hs >= ((1 << 24) - 1))) */
  196. }
  197. }
  198. p->hashMask = hs;
  199. hs++;
  200. if (p->numHashBytes > 2) p->fixedHashSize += kHash2Size;
  201. if (p->numHashBytes > 3) p->fixedHashSize += kHash3Size;
  202. if (p->numHashBytes > 4) p->fixedHashSize += kHash4Size;
  203. hs += p->fixedHashSize;
  204. }
  205. {
  206. size_t newSize;
  207. size_t numSons;
  208. p->historySize = historySize;
  209. p->hashSizeSum = hs;
  210. p->cyclicBufferSize = newCyclicBufferSize;
  211. numSons = newCyclicBufferSize;
  212. if (p->btMode)
  213. numSons <<= 1;
  214. newSize = hs + numSons;
  215. if (p->hash && p->numRefs == newSize)
  216. return 1;
  217. MatchFinder_FreeThisClassMemory(p, alloc);
  218. p->numRefs = newSize;
  219. p->hash = AllocRefs(newSize, alloc);
  220. if (p->hash)
  221. {
  222. p->son = p->hash + p->hashSizeSum;
  223. return 1;
  224. }
  225. }
  226. }
  227. MatchFinder_Free(p, alloc);
  228. return 0;
  229. }
  230. static void MatchFinder_SetLimits(CMatchFinder *p)
  231. {
  232. UInt32 limit = kMaxValForNormalize - p->pos;
  233. UInt32 limit2 = p->cyclicBufferSize - p->cyclicBufferPos;
  234. if (limit2 < limit)
  235. limit = limit2;
  236. limit2 = p->streamPos - p->pos;
  237. if (limit2 <= p->keepSizeAfter)
  238. {
  239. if (limit2 > 0)
  240. limit2 = 1;
  241. }
  242. else
  243. limit2 -= p->keepSizeAfter;
  244. if (limit2 < limit)
  245. limit = limit2;
  246. {
  247. UInt32 lenLimit = p->streamPos - p->pos;
  248. if (lenLimit > p->matchMaxLen)
  249. lenLimit = p->matchMaxLen;
  250. p->lenLimit = lenLimit;
  251. }
  252. p->posLimit = p->pos + limit;
  253. }
  254. void MatchFinder_Init_LowHash(CMatchFinder *p)
  255. {
  256. size_t i;
  257. CLzRef *items = p->hash;
  258. size_t numItems = p->fixedHashSize;
  259. for (i = 0; i < numItems; i++)
  260. items[i] = kEmptyHashValue;
  261. }
  262. void MatchFinder_Init_HighHash(CMatchFinder *p)
  263. {
  264. size_t i;
  265. CLzRef *items = p->hash + p->fixedHashSize;
  266. size_t numItems = (size_t)p->hashMask + 1;
  267. for (i = 0; i < numItems; i++)
  268. items[i] = kEmptyHashValue;
  269. }
  270. void MatchFinder_Init_3(CMatchFinder *p, int readData)
  271. {
  272. p->cyclicBufferPos = 0;
  273. p->buffer = p->bufferBase;
  274. p->pos =
  275. p->streamPos = p->cyclicBufferSize;
  276. p->result = SZ_OK;
  277. p->streamEndWasReached = 0;
  278. if (readData)
  279. MatchFinder_ReadBlock(p);
  280. MatchFinder_SetLimits(p);
  281. }
  282. void MatchFinder_Init(CMatchFinder *p)
  283. {
  284. MatchFinder_Init_HighHash(p);
  285. MatchFinder_Init_LowHash(p);
  286. MatchFinder_Init_3(p, True);
  287. }
  288. static UInt32 MatchFinder_GetSubValue(CMatchFinder *p)
  289. {
  290. return (p->pos - p->historySize - 1) & kNormalizeMask;
  291. }
  292. void MatchFinder_Normalize3(UInt32 subValue, CLzRef *items, size_t numItems)
  293. {
  294. size_t i;
  295. for (i = 0; i < numItems; i++)
  296. {
  297. UInt32 value = items[i];
  298. if (value <= subValue)
  299. value = kEmptyHashValue;
  300. else
  301. value -= subValue;
  302. items[i] = value;
  303. }
  304. }
  305. static void MatchFinder_Normalize(CMatchFinder *p)
  306. {
  307. UInt32 subValue = MatchFinder_GetSubValue(p);
  308. MatchFinder_Normalize3(subValue, p->hash, p->numRefs);
  309. MatchFinder_ReduceOffsets(p, subValue);
  310. }
  311. MY_NO_INLINE
  312. static void MatchFinder_CheckLimits(CMatchFinder *p)
  313. {
  314. if (p->pos == kMaxValForNormalize)
  315. MatchFinder_Normalize(p);
  316. if (!p->streamEndWasReached && p->keepSizeAfter == p->streamPos - p->pos)
  317. MatchFinder_CheckAndMoveAndRead(p);
  318. if (p->cyclicBufferPos == p->cyclicBufferSize)
  319. p->cyclicBufferPos = 0;
  320. MatchFinder_SetLimits(p);
  321. }
  322. /*
  323. (lenLimit > maxLen)
  324. */
  325. MY_FORCE_INLINE
  326. static UInt32 * Hc_GetMatchesSpec(unsigned lenLimit, UInt32 curMatch, UInt32 pos, const Byte *cur, CLzRef *son,
  327. UInt32 _cyclicBufferPos, UInt32 _cyclicBufferSize, UInt32 cutValue,
  328. UInt32 *distances, unsigned maxLen)
  329. {
  330. /*
  331. son[_cyclicBufferPos] = curMatch;
  332. for (;;)
  333. {
  334. UInt32 delta = pos - curMatch;
  335. if (cutValue-- == 0 || delta >= _cyclicBufferSize)
  336. return distances;
  337. {
  338. const Byte *pb = cur - delta;
  339. curMatch = son[_cyclicBufferPos - delta + ((delta > _cyclicBufferPos) ? _cyclicBufferSize : 0)];
  340. if (pb[maxLen] == cur[maxLen] && *pb == *cur)
  341. {
  342. UInt32 len = 0;
  343. while (++len != lenLimit)
  344. if (pb[len] != cur[len])
  345. break;
  346. if (maxLen < len)
  347. {
  348. maxLen = len;
  349. *distances++ = len;
  350. *distances++ = delta - 1;
  351. if (len == lenLimit)
  352. return distances;
  353. }
  354. }
  355. }
  356. }
  357. */
  358. const Byte *lim = cur + lenLimit;
  359. son[_cyclicBufferPos] = curMatch;
  360. do
  361. {
  362. UInt32 delta = pos - curMatch;
  363. if (delta >= _cyclicBufferSize)
  364. break;
  365. {
  366. ptrdiff_t diff;
  367. curMatch = son[_cyclicBufferPos - delta + ((delta > _cyclicBufferPos) ? _cyclicBufferSize : 0)];
  368. diff = (ptrdiff_t)0 - delta;
  369. if (cur[maxLen] == cur[maxLen + diff])
  370. {
  371. const Byte *c = cur;
  372. while (*c == c[diff])
  373. {
  374. if (++c == lim)
  375. {
  376. distances[0] = (UInt32)(lim - cur);
  377. distances[1] = delta - 1;
  378. return distances + 2;
  379. }
  380. }
  381. {
  382. unsigned len = (unsigned)(c - cur);
  383. if (maxLen < len)
  384. {
  385. maxLen = len;
  386. distances[0] = (UInt32)len;
  387. distances[1] = delta - 1;
  388. distances += 2;
  389. }
  390. }
  391. }
  392. }
  393. }
  394. while (--cutValue);
  395. return distances;
  396. }
  397. MY_FORCE_INLINE
  398. UInt32 * GetMatchesSpec1(UInt32 lenLimit, UInt32 curMatch, UInt32 pos, const Byte *cur, CLzRef *son,
  399. UInt32 _cyclicBufferPos, UInt32 _cyclicBufferSize, UInt32 cutValue,
  400. UInt32 *distances, UInt32 maxLen)
  401. {
  402. CLzRef *ptr0 = son + ((size_t)_cyclicBufferPos << 1) + 1;
  403. CLzRef *ptr1 = son + ((size_t)_cyclicBufferPos << 1);
  404. unsigned len0 = 0, len1 = 0;
  405. for (;;)
  406. {
  407. UInt32 delta = pos - curMatch;
  408. if (cutValue-- == 0 || delta >= _cyclicBufferSize)
  409. {
  410. *ptr0 = *ptr1 = kEmptyHashValue;
  411. return distances;
  412. }
  413. {
  414. CLzRef *pair = son + ((size_t)(_cyclicBufferPos - delta + ((delta > _cyclicBufferPos) ? _cyclicBufferSize : 0)) << 1);
  415. const Byte *pb = cur - delta;
  416. unsigned len = (len0 < len1 ? len0 : len1);
  417. UInt32 pair0 = pair[0];
  418. if (pb[len] == cur[len])
  419. {
  420. if (++len != lenLimit && pb[len] == cur[len])
  421. while (++len != lenLimit)
  422. if (pb[len] != cur[len])
  423. break;
  424. if (maxLen < len)
  425. {
  426. maxLen = (UInt32)len;
  427. *distances++ = (UInt32)len;
  428. *distances++ = delta - 1;
  429. if (len == lenLimit)
  430. {
  431. *ptr1 = pair0;
  432. *ptr0 = pair[1];
  433. return distances;
  434. }
  435. }
  436. }
  437. if (pb[len] < cur[len])
  438. {
  439. *ptr1 = curMatch;
  440. ptr1 = pair + 1;
  441. curMatch = *ptr1;
  442. len1 = len;
  443. }
  444. else
  445. {
  446. *ptr0 = curMatch;
  447. ptr0 = pair;
  448. curMatch = *ptr0;
  449. len0 = len;
  450. }
  451. }
  452. }
  453. }
  454. static void SkipMatchesSpec(UInt32 lenLimit, UInt32 curMatch, UInt32 pos, const Byte *cur, CLzRef *son,
  455. UInt32 _cyclicBufferPos, UInt32 _cyclicBufferSize, UInt32 cutValue)
  456. {
  457. CLzRef *ptr0 = son + ((size_t)_cyclicBufferPos << 1) + 1;
  458. CLzRef *ptr1 = son + ((size_t)_cyclicBufferPos << 1);
  459. unsigned len0 = 0, len1 = 0;
  460. for (;;)
  461. {
  462. UInt32 delta = pos - curMatch;
  463. if (cutValue-- == 0 || delta >= _cyclicBufferSize)
  464. {
  465. *ptr0 = *ptr1 = kEmptyHashValue;
  466. return;
  467. }
  468. {
  469. CLzRef *pair = son + ((size_t)(_cyclicBufferPos - delta + ((delta > _cyclicBufferPos) ? _cyclicBufferSize : 0)) << 1);
  470. const Byte *pb = cur - delta;
  471. unsigned len = (len0 < len1 ? len0 : len1);
  472. if (pb[len] == cur[len])
  473. {
  474. while (++len != lenLimit)
  475. if (pb[len] != cur[len])
  476. break;
  477. {
  478. if (len == lenLimit)
  479. {
  480. *ptr1 = pair[0];
  481. *ptr0 = pair[1];
  482. return;
  483. }
  484. }
  485. }
  486. if (pb[len] < cur[len])
  487. {
  488. *ptr1 = curMatch;
  489. ptr1 = pair + 1;
  490. curMatch = *ptr1;
  491. len1 = len;
  492. }
  493. else
  494. {
  495. *ptr0 = curMatch;
  496. ptr0 = pair;
  497. curMatch = *ptr0;
  498. len0 = len;
  499. }
  500. }
  501. }
  502. }
  503. #define MOVE_POS \
  504. ++p->cyclicBufferPos; \
  505. p->buffer++; \
  506. if (++p->pos == p->posLimit) MatchFinder_CheckLimits(p);
  507. #define MOVE_POS_RET MOVE_POS return (UInt32)offset;
  508. static void MatchFinder_MovePos(CMatchFinder *p) { MOVE_POS; }
  509. #define GET_MATCHES_HEADER2(minLen, ret_op) \
  510. unsigned lenLimit; UInt32 hv; const Byte *cur; UInt32 curMatch; \
  511. lenLimit = (unsigned)p->lenLimit; { if (lenLimit < minLen) { MatchFinder_MovePos(p); ret_op; }} \
  512. cur = p->buffer;
  513. #define GET_MATCHES_HEADER(minLen) GET_MATCHES_HEADER2(minLen, return 0)
  514. #define SKIP_HEADER(minLen) GET_MATCHES_HEADER2(minLen, continue)
  515. #define MF_PARAMS(p) p->pos, p->buffer, p->son, p->cyclicBufferPos, p->cyclicBufferSize, p->cutValue
  516. #define GET_MATCHES_FOOTER(offset, maxLen) \
  517. offset = (unsigned)(GetMatchesSpec1((UInt32)lenLimit, curMatch, MF_PARAMS(p), \
  518. distances + offset, (UInt32)maxLen) - distances); MOVE_POS_RET;
  519. #define SKIP_FOOTER \
  520. SkipMatchesSpec((UInt32)lenLimit, curMatch, MF_PARAMS(p)); MOVE_POS;
  521. #define UPDATE_maxLen { \
  522. ptrdiff_t diff = (ptrdiff_t)0 - d2; \
  523. const Byte *c = cur + maxLen; \
  524. const Byte *lim = cur + lenLimit; \
  525. for (; c != lim; c++) if (*(c + diff) != *c) break; \
  526. maxLen = (unsigned)(c - cur); }
  527. static UInt32 Bt2_MatchFinder_GetMatches(CMatchFinder *p, UInt32 *distances)
  528. {
  529. unsigned offset;
  530. GET_MATCHES_HEADER(2)
  531. HASH2_CALC;
  532. curMatch = p->hash[hv];
  533. p->hash[hv] = p->pos;
  534. offset = 0;
  535. GET_MATCHES_FOOTER(offset, 1)
  536. }
  537. UInt32 Bt3Zip_MatchFinder_GetMatches(CMatchFinder *p, UInt32 *distances)
  538. {
  539. unsigned offset;
  540. GET_MATCHES_HEADER(3)
  541. HASH_ZIP_CALC;
  542. curMatch = p->hash[hv];
  543. p->hash[hv] = p->pos;
  544. offset = 0;
  545. GET_MATCHES_FOOTER(offset, 2)
  546. }
  547. static UInt32 Bt3_MatchFinder_GetMatches(CMatchFinder *p, UInt32 *distances)
  548. {
  549. UInt32 h2, d2, pos;
  550. unsigned maxLen, offset;
  551. UInt32 *hash;
  552. GET_MATCHES_HEADER(3)
  553. HASH3_CALC;
  554. hash = p->hash;
  555. pos = p->pos;
  556. d2 = pos - hash[h2];
  557. curMatch = (hash + kFix3HashSize)[hv];
  558. hash[h2] = pos;
  559. (hash + kFix3HashSize)[hv] = pos;
  560. maxLen = 2;
  561. offset = 0;
  562. if (d2 < p->cyclicBufferSize && *(cur - d2) == *cur)
  563. {
  564. UPDATE_maxLen
  565. distances[0] = (UInt32)maxLen;
  566. distances[1] = d2 - 1;
  567. offset = 2;
  568. if (maxLen == lenLimit)
  569. {
  570. SkipMatchesSpec((UInt32)lenLimit, curMatch, MF_PARAMS(p));
  571. MOVE_POS_RET;
  572. }
  573. }
  574. GET_MATCHES_FOOTER(offset, maxLen)
  575. }
  576. static UInt32 Bt4_MatchFinder_GetMatches(CMatchFinder *p, UInt32 *distances)
  577. {
  578. UInt32 h2, h3, d2, d3, pos;
  579. unsigned maxLen, offset;
  580. UInt32 *hash;
  581. GET_MATCHES_HEADER(4)
  582. HASH4_CALC;
  583. hash = p->hash;
  584. pos = p->pos;
  585. d2 = pos - hash [h2];
  586. d3 = pos - (hash + kFix3HashSize)[h3];
  587. curMatch = (hash + kFix4HashSize)[hv];
  588. hash [h2] = pos;
  589. (hash + kFix3HashSize)[h3] = pos;
  590. (hash + kFix4HashSize)[hv] = pos;
  591. maxLen = 0;
  592. offset = 0;
  593. if (d2 < p->cyclicBufferSize && *(cur - d2) == *cur)
  594. {
  595. maxLen = 2;
  596. distances[0] = 2;
  597. distances[1] = d2 - 1;
  598. offset = 2;
  599. }
  600. if (d2 != d3 && d3 < p->cyclicBufferSize && *(cur - d3) == *cur)
  601. {
  602. maxLen = 3;
  603. distances[(size_t)offset + 1] = d3 - 1;
  604. offset += 2;
  605. d2 = d3;
  606. }
  607. if (offset != 0)
  608. {
  609. UPDATE_maxLen
  610. distances[(size_t)offset - 2] = (UInt32)maxLen;
  611. if (maxLen == lenLimit)
  612. {
  613. SkipMatchesSpec((UInt32)lenLimit, curMatch, MF_PARAMS(p));
  614. MOVE_POS_RET;
  615. }
  616. }
  617. if (maxLen < 3)
  618. maxLen = 3;
  619. GET_MATCHES_FOOTER(offset, maxLen)
  620. }
  621. /*
  622. static UInt32 Bt5_MatchFinder_GetMatches(CMatchFinder *p, UInt32 *distances)
  623. {
  624. UInt32 h2, h3, h4, d2, d3, d4, maxLen, offset, pos;
  625. UInt32 *hash;
  626. GET_MATCHES_HEADER(5)
  627. HASH5_CALC;
  628. hash = p->hash;
  629. pos = p->pos;
  630. d2 = pos - hash [h2];
  631. d3 = pos - (hash + kFix3HashSize)[h3];
  632. d4 = pos - (hash + kFix4HashSize)[h4];
  633. curMatch = (hash + kFix5HashSize)[hv];
  634. hash [h2] = pos;
  635. (hash + kFix3HashSize)[h3] = pos;
  636. (hash + kFix4HashSize)[h4] = pos;
  637. (hash + kFix5HashSize)[hv] = pos;
  638. maxLen = 0;
  639. offset = 0;
  640. if (d2 < p->cyclicBufferSize && *(cur - d2) == *cur)
  641. {
  642. distances[0] = maxLen = 2;
  643. distances[1] = d2 - 1;
  644. offset = 2;
  645. if (*(cur - d2 + 2) == cur[2])
  646. distances[0] = maxLen = 3;
  647. else if (d3 < p->cyclicBufferSize && *(cur - d3) == *cur)
  648. {
  649. distances[2] = maxLen = 3;
  650. distances[3] = d3 - 1;
  651. offset = 4;
  652. d2 = d3;
  653. }
  654. }
  655. else if (d3 < p->cyclicBufferSize && *(cur - d3) == *cur)
  656. {
  657. distances[0] = maxLen = 3;
  658. distances[1] = d3 - 1;
  659. offset = 2;
  660. d2 = d3;
  661. }
  662. if (d2 != d4 && d4 < p->cyclicBufferSize
  663. && *(cur - d4) == *cur
  664. && *(cur - d4 + 3) == *(cur + 3))
  665. {
  666. maxLen = 4;
  667. distances[(size_t)offset + 1] = d4 - 1;
  668. offset += 2;
  669. d2 = d4;
  670. }
  671. if (offset != 0)
  672. {
  673. UPDATE_maxLen
  674. distances[(size_t)offset - 2] = maxLen;
  675. if (maxLen == lenLimit)
  676. {
  677. SkipMatchesSpec(lenLimit, curMatch, MF_PARAMS(p));
  678. MOVE_POS_RET;
  679. }
  680. }
  681. if (maxLen < 4)
  682. maxLen = 4;
  683. GET_MATCHES_FOOTER(offset, maxLen)
  684. }
  685. */
  686. static UInt32 Hc4_MatchFinder_GetMatches(CMatchFinder *p, UInt32 *distances)
  687. {
  688. UInt32 h2, h3, d2, d3, pos;
  689. unsigned maxLen, offset;
  690. UInt32 *hash;
  691. GET_MATCHES_HEADER(4)
  692. HASH4_CALC;
  693. hash = p->hash;
  694. pos = p->pos;
  695. d2 = pos - hash [h2];
  696. d3 = pos - (hash + kFix3HashSize)[h3];
  697. curMatch = (hash + kFix4HashSize)[hv];
  698. hash [h2] = pos;
  699. (hash + kFix3HashSize)[h3] = pos;
  700. (hash + kFix4HashSize)[hv] = pos;
  701. maxLen = 0;
  702. offset = 0;
  703. if (d2 < p->cyclicBufferSize && *(cur - d2) == *cur)
  704. {
  705. maxLen = 2;
  706. distances[0] = 2;
  707. distances[1] = d2 - 1;
  708. offset = 2;
  709. }
  710. if (d2 != d3 && d3 < p->cyclicBufferSize && *(cur - d3) == *cur)
  711. {
  712. maxLen = 3;
  713. distances[(size_t)offset + 1] = d3 - 1;
  714. offset += 2;
  715. d2 = d3;
  716. }
  717. if (offset != 0)
  718. {
  719. UPDATE_maxLen
  720. distances[(size_t)offset - 2] = (UInt32)maxLen;
  721. if (maxLen == lenLimit)
  722. {
  723. p->son[p->cyclicBufferPos] = curMatch;
  724. MOVE_POS_RET;
  725. }
  726. }
  727. if (maxLen < 3)
  728. maxLen = 3;
  729. offset = (unsigned)(Hc_GetMatchesSpec(lenLimit, curMatch, MF_PARAMS(p),
  730. distances + offset, maxLen) - (distances));
  731. MOVE_POS_RET
  732. }
  733. /*
  734. static UInt32 Hc5_MatchFinder_GetMatches(CMatchFinder *p, UInt32 *distances)
  735. {
  736. UInt32 h2, h3, h4, d2, d3, d4, maxLen, offset, pos
  737. UInt32 *hash;
  738. GET_MATCHES_HEADER(5)
  739. HASH5_CALC;
  740. hash = p->hash;
  741. pos = p->pos;
  742. d2 = pos - hash [h2];
  743. d3 = pos - (hash + kFix3HashSize)[h3];
  744. d4 = pos - (hash + kFix4HashSize)[h4];
  745. curMatch = (hash + kFix5HashSize)[hv];
  746. hash [h2] = pos;
  747. (hash + kFix3HashSize)[h3] = pos;
  748. (hash + kFix4HashSize)[h4] = pos;
  749. (hash + kFix5HashSize)[hv] = pos;
  750. maxLen = 0;
  751. offset = 0;
  752. if (d2 < p->cyclicBufferSize && *(cur - d2) == *cur)
  753. {
  754. distances[0] = maxLen = 2;
  755. distances[1] = d2 - 1;
  756. offset = 2;
  757. if (*(cur - d2 + 2) == cur[2])
  758. distances[0] = maxLen = 3;
  759. else if (d3 < p->cyclicBufferSize && *(cur - d3) == *cur)
  760. {
  761. distances[2] = maxLen = 3;
  762. distances[3] = d3 - 1;
  763. offset = 4;
  764. d2 = d3;
  765. }
  766. }
  767. else if (d3 < p->cyclicBufferSize && *(cur - d3) == *cur)
  768. {
  769. distances[0] = maxLen = 3;
  770. distances[1] = d3 - 1;
  771. offset = 2;
  772. d2 = d3;
  773. }
  774. if (d2 != d4 && d4 < p->cyclicBufferSize
  775. && *(cur - d4) == *cur
  776. && *(cur - d4 + 3) == *(cur + 3))
  777. {
  778. maxLen = 4;
  779. distances[(size_t)offset + 1] = d4 - 1;
  780. offset += 2;
  781. d2 = d4;
  782. }
  783. if (offset != 0)
  784. {
  785. UPDATE_maxLen
  786. distances[(size_t)offset - 2] = maxLen;
  787. if (maxLen == lenLimit)
  788. {
  789. p->son[p->cyclicBufferPos] = curMatch;
  790. MOVE_POS_RET;
  791. }
  792. }
  793. if (maxLen < 4)
  794. maxLen = 4;
  795. offset = (UInt32)(Hc_GetMatchesSpec(lenLimit, curMatch, MF_PARAMS(p),
  796. distances + offset, maxLen) - (distances));
  797. MOVE_POS_RET
  798. }
  799. */
  800. UInt32 Hc3Zip_MatchFinder_GetMatches(CMatchFinder *p, UInt32 *distances)
  801. {
  802. unsigned offset;
  803. GET_MATCHES_HEADER(3)
  804. HASH_ZIP_CALC;
  805. curMatch = p->hash[hv];
  806. p->hash[hv] = p->pos;
  807. offset = (unsigned)(Hc_GetMatchesSpec(lenLimit, curMatch, MF_PARAMS(p),
  808. distances, 2) - (distances));
  809. MOVE_POS_RET
  810. }
  811. static void Bt2_MatchFinder_Skip(CMatchFinder *p, UInt32 num)
  812. {
  813. do
  814. {
  815. SKIP_HEADER(2)
  816. HASH2_CALC;
  817. curMatch = p->hash[hv];
  818. p->hash[hv] = p->pos;
  819. SKIP_FOOTER
  820. }
  821. while (--num != 0);
  822. }
  823. void Bt3Zip_MatchFinder_Skip(CMatchFinder *p, UInt32 num)
  824. {
  825. do
  826. {
  827. SKIP_HEADER(3)
  828. HASH_ZIP_CALC;
  829. curMatch = p->hash[hv];
  830. p->hash[hv] = p->pos;
  831. SKIP_FOOTER
  832. }
  833. while (--num != 0);
  834. }
  835. static void Bt3_MatchFinder_Skip(CMatchFinder *p, UInt32 num)
  836. {
  837. do
  838. {
  839. UInt32 h2;
  840. UInt32 *hash;
  841. SKIP_HEADER(3)
  842. HASH3_CALC;
  843. hash = p->hash;
  844. curMatch = (hash + kFix3HashSize)[hv];
  845. hash[h2] =
  846. (hash + kFix3HashSize)[hv] = p->pos;
  847. SKIP_FOOTER
  848. }
  849. while (--num != 0);
  850. }
  851. static void Bt4_MatchFinder_Skip(CMatchFinder *p, UInt32 num)
  852. {
  853. do
  854. {
  855. UInt32 h2, h3;
  856. UInt32 *hash;
  857. SKIP_HEADER(4)
  858. HASH4_CALC;
  859. hash = p->hash;
  860. curMatch = (hash + kFix4HashSize)[hv];
  861. hash [h2] =
  862. (hash + kFix3HashSize)[h3] =
  863. (hash + kFix4HashSize)[hv] = p->pos;
  864. SKIP_FOOTER
  865. }
  866. while (--num != 0);
  867. }
  868. /*
  869. static void Bt5_MatchFinder_Skip(CMatchFinder *p, UInt32 num)
  870. {
  871. do
  872. {
  873. UInt32 h2, h3, h4;
  874. UInt32 *hash;
  875. SKIP_HEADER(5)
  876. HASH5_CALC;
  877. hash = p->hash;
  878. curMatch = (hash + kFix5HashSize)[hv];
  879. hash [h2] =
  880. (hash + kFix3HashSize)[h3] =
  881. (hash + kFix4HashSize)[h4] =
  882. (hash + kFix5HashSize)[hv] = p->pos;
  883. SKIP_FOOTER
  884. }
  885. while (--num != 0);
  886. }
  887. */
  888. static void Hc4_MatchFinder_Skip(CMatchFinder *p, UInt32 num)
  889. {
  890. do
  891. {
  892. UInt32 h2, h3;
  893. UInt32 *hash;
  894. SKIP_HEADER(4)
  895. HASH4_CALC;
  896. hash = p->hash;
  897. curMatch = (hash + kFix4HashSize)[hv];
  898. hash [h2] =
  899. (hash + kFix3HashSize)[h3] =
  900. (hash + kFix4HashSize)[hv] = p->pos;
  901. p->son[p->cyclicBufferPos] = curMatch;
  902. MOVE_POS
  903. }
  904. while (--num != 0);
  905. }
  906. /*
  907. static void Hc5_MatchFinder_Skip(CMatchFinder *p, UInt32 num)
  908. {
  909. do
  910. {
  911. UInt32 h2, h3, h4;
  912. UInt32 *hash;
  913. SKIP_HEADER(5)
  914. HASH5_CALC;
  915. hash = p->hash;
  916. curMatch = hash + kFix5HashSize)[hv];
  917. hash [h2] =
  918. (hash + kFix3HashSize)[h3] =
  919. (hash + kFix4HashSize)[h4] =
  920. (hash + kFix5HashSize)[hv] = p->pos;
  921. p->son[p->cyclicBufferPos] = curMatch;
  922. MOVE_POS
  923. }
  924. while (--num != 0);
  925. }
  926. */
  927. void Hc3Zip_MatchFinder_Skip(CMatchFinder *p, UInt32 num)
  928. {
  929. do
  930. {
  931. SKIP_HEADER(3)
  932. HASH_ZIP_CALC;
  933. curMatch = p->hash[hv];
  934. p->hash[hv] = p->pos;
  935. p->son[p->cyclicBufferPos] = curMatch;
  936. MOVE_POS
  937. }
  938. while (--num != 0);
  939. }
  940. void MatchFinder_CreateVTable(CMatchFinder *p, IMatchFinder *vTable)
  941. {
  942. vTable->Init = (Mf_Init_Func)MatchFinder_Init;
  943. vTable->GetNumAvailableBytes = (Mf_GetNumAvailableBytes_Func)MatchFinder_GetNumAvailableBytes;
  944. vTable->GetPointerToCurrentPos = (Mf_GetPointerToCurrentPos_Func)MatchFinder_GetPointerToCurrentPos;
  945. if (!p->btMode)
  946. {
  947. /* if (p->numHashBytes <= 4) */
  948. {
  949. vTable->GetMatches = (Mf_GetMatches_Func)Hc4_MatchFinder_GetMatches;
  950. vTable->Skip = (Mf_Skip_Func)Hc4_MatchFinder_Skip;
  951. }
  952. /*
  953. else
  954. {
  955. vTable->GetMatches = (Mf_GetMatches_Func)Hc5_MatchFinder_GetMatches;
  956. vTable->Skip = (Mf_Skip_Func)Hc5_MatchFinder_Skip;
  957. }
  958. */
  959. }
  960. else if (p->numHashBytes == 2)
  961. {
  962. vTable->GetMatches = (Mf_GetMatches_Func)Bt2_MatchFinder_GetMatches;
  963. vTable->Skip = (Mf_Skip_Func)Bt2_MatchFinder_Skip;
  964. }
  965. else if (p->numHashBytes == 3)
  966. {
  967. vTable->GetMatches = (Mf_GetMatches_Func)Bt3_MatchFinder_GetMatches;
  968. vTable->Skip = (Mf_Skip_Func)Bt3_MatchFinder_Skip;
  969. }
  970. else /* if (p->numHashBytes == 4) */
  971. {
  972. vTable->GetMatches = (Mf_GetMatches_Func)Bt4_MatchFinder_GetMatches;
  973. vTable->Skip = (Mf_Skip_Func)Bt4_MatchFinder_Skip;
  974. }
  975. /*
  976. else
  977. {
  978. vTable->GetMatches = (Mf_GetMatches_Func)Bt5_MatchFinder_GetMatches;
  979. vTable->Skip = (Mf_Skip_Func)Bt5_MatchFinder_Skip;
  980. }
  981. */
  982. }