LzFind.c 29 KB

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