LzFind.c 20 KB

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