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