LzFind.c 20 KB

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