2N/A/*
2N/A * GRUB -- GRand Unified Bootloader
2N/A * Copyright (c) 1999-2008 Igor Pavlov
2N/A * Copyright (C) 2008 Free Software Foundation, Inc.
2N/A *
2N/A * GRUB is free software: you can redistribute it and/or modify
2N/A * it under the terms of the GNU General Public License as published by
2N/A * the Free Software Foundation, either version 3 of the License, or
2N/A * (at your option) any later version.
2N/A *
2N/A * GRUB is distributed in the hope that it will be useful,
2N/A * but WITHOUT ANY WARRANTY; without even the implied warranty of
2N/A * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
2N/A * GNU General Public License for more details.
2N/A *
2N/A * You should have received a copy of the GNU General Public License
2N/A * along with GRUB. If not, see <http://www.gnu.org/licenses/>.
2N/A */
2N/A
2N/A/*
2N/A * This code was taken from LZMA SDK 4.58 beta, and was slightly modified
2N/A * to adapt it to GRUB's requirement.
2N/A *
2N/A * See <http://www.7-zip.org>, for more information about LZMA.
2N/A */
2N/A
2N/A
2N/A#include <config.h>
2N/A
2N/A#include <string.h>
2N/A
2N/A#include <grub/lib/LzFind.h>
2N/A#include <grub/lib/LzHash.h>
2N/A
2N/A#define kEmptyHashValue 0
2N/A#define kMaxValForNormalize ((UInt32)0xFFFFFFFF)
2N/A#define kNormalizeStepMin (1 << 10) /* it must be power of 2 */
2N/A#define kNormalizeMask (~(kNormalizeStepMin - 1))
2N/A#define kMaxHistorySize ((UInt32)3 << 30)
2N/A
2N/A#define kStartMaxLen 3
2N/A
2N/Astatic void LzInWindow_Free(CMatchFinder *p, ISzAlloc *alloc)
2N/A{
2N/A if (!p->directInput)
2N/A {
2N/A alloc->Free(alloc, p->bufferBase);
2N/A p->bufferBase = 0;
2N/A }
2N/A}
2N/A
2N/A/* keepSizeBefore + keepSizeAfter + keepSizeReserv must be < 4G) */
2N/A
2N/Astatic int LzInWindow_Create(CMatchFinder *p, UInt32 keepSizeReserv, ISzAlloc *alloc)
2N/A{
2N/A UInt32 blockSize = p->keepSizeBefore + p->keepSizeAfter + keepSizeReserv;
2N/A if (p->directInput)
2N/A {
2N/A p->blockSize = blockSize;
2N/A return 1;
2N/A }
2N/A if (p->bufferBase == 0 || p->blockSize != blockSize)
2N/A {
2N/A LzInWindow_Free(p, alloc);
2N/A p->blockSize = blockSize;
2N/A p->bufferBase = (Byte *)alloc->Alloc(alloc, (size_t)blockSize);
2N/A }
2N/A return (p->bufferBase != 0);
2N/A}
2N/A
2N/AByte *MatchFinder_GetPointerToCurrentPos(CMatchFinder *p) { return p->buffer; }
2N/AByte MatchFinder_GetIndexByte(CMatchFinder *p, Int32 index) { return p->buffer[index]; }
2N/A
2N/AUInt32 MatchFinder_GetNumAvailableBytes(CMatchFinder *p) { return p->streamPos - p->pos; }
2N/A
2N/Avoid MatchFinder_ReduceOffsets(CMatchFinder *p, UInt32 subValue)
2N/A{
2N/A p->posLimit -= subValue;
2N/A p->pos -= subValue;
2N/A p->streamPos -= subValue;
2N/A}
2N/A
2N/Astatic void MatchFinder_ReadBlock(CMatchFinder *p)
2N/A{
2N/A if (p->streamEndWasReached || p->result != SZ_OK)
2N/A return;
2N/A for (;;)
2N/A {
2N/A Byte *dest = p->buffer + (p->streamPos - p->pos);
2N/A size_t size = (p->bufferBase + p->blockSize - dest);
2N/A if (size == 0)
2N/A return;
2N/A p->result = p->stream->Read(p->stream, dest, &size);
2N/A if (p->result != SZ_OK)
2N/A return;
2N/A if (size == 0)
2N/A {
2N/A p->streamEndWasReached = 1;
2N/A return;
2N/A }
2N/A p->streamPos += (UInt32)size;
2N/A if (p->streamPos - p->pos > p->keepSizeAfter)
2N/A return;
2N/A }
2N/A}
2N/A
2N/Avoid MatchFinder_MoveBlock(CMatchFinder *p)
2N/A{
2N/A memmove(p->bufferBase,
2N/A p->buffer - p->keepSizeBefore,
2N/A (size_t)(p->streamPos - p->pos + p->keepSizeBefore));
2N/A p->buffer = p->bufferBase + p->keepSizeBefore;
2N/A}
2N/A
2N/Aint MatchFinder_NeedMove(CMatchFinder *p)
2N/A{
2N/A /* if (p->streamEndWasReached) return 0; */
2N/A return ((size_t)(p->bufferBase + p->blockSize - p->buffer) <= p->keepSizeAfter);
2N/A}
2N/A
2N/Avoid MatchFinder_ReadIfRequired(CMatchFinder *p)
2N/A{
2N/A if (p->streamEndWasReached)
2N/A return;
2N/A if (p->keepSizeAfter >= p->streamPos - p->pos)
2N/A MatchFinder_ReadBlock(p);
2N/A}
2N/A
2N/Astatic void MatchFinder_CheckAndMoveAndRead(CMatchFinder *p)
2N/A{
2N/A if (MatchFinder_NeedMove(p))
2N/A MatchFinder_MoveBlock(p);
2N/A MatchFinder_ReadBlock(p);
2N/A}
2N/A
2N/Astatic void MatchFinder_SetDefaultSettings(CMatchFinder *p)
2N/A{
2N/A p->cutValue = 32;
2N/A p->btMode = 1;
2N/A p->numHashBytes = 4;
2N/A /* p->skipModeBits = 0; */
2N/A p->directInput = 0;
2N/A p->bigHash = 0;
2N/A}
2N/A
2N/A#define kCrcPoly 0xEDB88320
2N/A
2N/Avoid MatchFinder_Construct(CMatchFinder *p)
2N/A{
2N/A UInt32 i;
2N/A p->bufferBase = 0;
2N/A p->directInput = 0;
2N/A p->hash = 0;
2N/A MatchFinder_SetDefaultSettings(p);
2N/A
2N/A for (i = 0; i < 256; i++)
2N/A {
2N/A UInt32 r = i;
2N/A int j;
2N/A for (j = 0; j < 8; j++)
2N/A r = (r >> 1) ^ (kCrcPoly & ~((r & 1) - 1));
2N/A p->crc[i] = r;
2N/A }
2N/A}
2N/A
2N/Astatic void MatchFinder_FreeThisClassMemory(CMatchFinder *p, ISzAlloc *alloc)
2N/A{
2N/A alloc->Free(alloc, p->hash);
2N/A p->hash = 0;
2N/A}
2N/A
2N/Avoid MatchFinder_Free(CMatchFinder *p, ISzAlloc *alloc)
2N/A{
2N/A MatchFinder_FreeThisClassMemory(p, alloc);
2N/A LzInWindow_Free(p, alloc);
2N/A}
2N/A
2N/Astatic CLzRef* AllocRefs(UInt32 num, ISzAlloc *alloc)
2N/A{
2N/A size_t sizeInBytes = (size_t)num * sizeof(CLzRef);
2N/A if (sizeInBytes / sizeof(CLzRef) != num)
2N/A return 0;
2N/A return (CLzRef *)alloc->Alloc(alloc, sizeInBytes);
2N/A}
2N/A
2N/Aint MatchFinder_Create(CMatchFinder *p, UInt32 historySize,
2N/A UInt32 keepAddBufferBefore, UInt32 matchMaxLen, UInt32 keepAddBufferAfter,
2N/A ISzAlloc *alloc)
2N/A{
2N/A UInt32 sizeReserv;
2N/A if (historySize > kMaxHistorySize)
2N/A {
2N/A MatchFinder_Free(p, alloc);
2N/A return 0;
2N/A }
2N/A sizeReserv = historySize >> 1;
2N/A if (historySize > ((UInt32)2 << 30))
2N/A sizeReserv = historySize >> 2;
2N/A sizeReserv += (keepAddBufferBefore + matchMaxLen + keepAddBufferAfter) / 2 + (1 << 19);
2N/A
2N/A p->keepSizeBefore = historySize + keepAddBufferBefore + 1;
2N/A p->keepSizeAfter = matchMaxLen + keepAddBufferAfter;
2N/A /* we need one additional byte, since we use MoveBlock after pos++ and before dictionary using */
2N/A if (LzInWindow_Create(p, sizeReserv, alloc))
2N/A {
2N/A UInt32 newCyclicBufferSize = (historySize /* >> p->skipModeBits */) + 1;
2N/A UInt32 hs;
2N/A p->matchMaxLen = matchMaxLen;
2N/A {
2N/A p->fixedHashSize = 0;
2N/A if (p->numHashBytes == 2)
2N/A hs = (1 << 16) - 1;
2N/A else
2N/A {
2N/A hs = historySize - 1;
2N/A hs |= (hs >> 1);
2N/A hs |= (hs >> 2);
2N/A hs |= (hs >> 4);
2N/A hs |= (hs >> 8);
2N/A hs >>= 1;
2N/A /* hs >>= p->skipModeBits; */
2N/A hs |= 0xFFFF; /* don't change it! It's required for Deflate */
2N/A if (hs > (1 << 24))
2N/A {
2N/A if (p->numHashBytes == 3)
2N/A hs = (1 << 24) - 1;
2N/A else
2N/A hs >>= 1;
2N/A }
2N/A }
2N/A p->hashMask = hs;
2N/A hs++;
2N/A if (p->numHashBytes > 2) p->fixedHashSize += kHash2Size;
2N/A if (p->numHashBytes > 3) p->fixedHashSize += kHash3Size;
2N/A if (p->numHashBytes > 4) p->fixedHashSize += kHash4Size;
2N/A hs += p->fixedHashSize;
2N/A }
2N/A
2N/A {
2N/A UInt32 prevSize = p->hashSizeSum + p->numSons;
2N/A UInt32 newSize;
2N/A p->historySize = historySize;
2N/A p->hashSizeSum = hs;
2N/A p->cyclicBufferSize = newCyclicBufferSize;
2N/A p->numSons = (p->btMode ? newCyclicBufferSize * 2 : newCyclicBufferSize);
2N/A newSize = p->hashSizeSum + p->numSons;
2N/A if (p->hash != 0 && prevSize == newSize)
2N/A return 1;
2N/A MatchFinder_FreeThisClassMemory(p, alloc);
2N/A p->hash = AllocRefs(newSize, alloc);
2N/A if (p->hash != 0)
2N/A {
2N/A p->son = p->hash + p->hashSizeSum;
2N/A return 1;
2N/A }
2N/A }
2N/A }
2N/A MatchFinder_Free(p, alloc);
2N/A return 0;
2N/A}
2N/A
2N/Astatic void MatchFinder_SetLimits(CMatchFinder *p)
2N/A{
2N/A UInt32 limit = kMaxValForNormalize - p->pos;
2N/A UInt32 limit2 = p->cyclicBufferSize - p->cyclicBufferPos;
2N/A if (limit2 < limit)
2N/A limit = limit2;
2N/A limit2 = p->streamPos - p->pos;
2N/A if (limit2 <= p->keepSizeAfter)
2N/A {
2N/A if (limit2 > 0)
2N/A limit2 = 1;
2N/A }
2N/A else
2N/A limit2 -= p->keepSizeAfter;
2N/A if (limit2 < limit)
2N/A limit = limit2;
2N/A {
2N/A UInt32 lenLimit = p->streamPos - p->pos;
2N/A if (lenLimit > p->matchMaxLen)
2N/A lenLimit = p->matchMaxLen;
2N/A p->lenLimit = lenLimit;
2N/A }
2N/A p->posLimit = p->pos + limit;
2N/A}
2N/A
2N/Avoid MatchFinder_Init(CMatchFinder *p)
2N/A{
2N/A UInt32 i;
2N/A for(i = 0; i < p->hashSizeSum; i++)
2N/A p->hash[i] = kEmptyHashValue;
2N/A p->cyclicBufferPos = 0;
2N/A p->buffer = p->bufferBase;
2N/A p->pos = p->streamPos = p->cyclicBufferSize;
2N/A p->result = SZ_OK;
2N/A p->streamEndWasReached = 0;
2N/A MatchFinder_ReadBlock(p);
2N/A MatchFinder_SetLimits(p);
2N/A}
2N/A
2N/Astatic UInt32 MatchFinder_GetSubValue(CMatchFinder *p)
2N/A{
2N/A return (p->pos - p->historySize - 1) & kNormalizeMask;
2N/A}
2N/A
2N/Avoid MatchFinder_Normalize3(UInt32 subValue, CLzRef *items, UInt32 numItems)
2N/A{
2N/A UInt32 i;
2N/A for (i = 0; i < numItems; i++)
2N/A {
2N/A UInt32 value = items[i];
2N/A if (value <= subValue)
2N/A value = kEmptyHashValue;
2N/A else
2N/A value -= subValue;
2N/A items[i] = value;
2N/A }
2N/A}
2N/A
2N/Astatic void MatchFinder_Normalize(CMatchFinder *p)
2N/A{
2N/A UInt32 subValue = MatchFinder_GetSubValue(p);
2N/A MatchFinder_Normalize3(subValue, p->hash, p->hashSizeSum + p->numSons);
2N/A MatchFinder_ReduceOffsets(p, subValue);
2N/A}
2N/A
2N/Astatic void MatchFinder_CheckLimits(CMatchFinder *p)
2N/A{
2N/A if (p->pos == kMaxValForNormalize)
2N/A MatchFinder_Normalize(p);
2N/A if (!p->streamEndWasReached && p->keepSizeAfter == p->streamPos - p->pos)
2N/A MatchFinder_CheckAndMoveAndRead(p);
2N/A if (p->cyclicBufferPos == p->cyclicBufferSize)
2N/A p->cyclicBufferPos = 0;
2N/A MatchFinder_SetLimits(p);
2N/A}
2N/A
2N/Astatic UInt32 * Hc_GetMatchesSpec(UInt32 lenLimit, UInt32 curMatch, UInt32 pos, const Byte *cur, CLzRef *son,
2N/A UInt32 _cyclicBufferPos, UInt32 _cyclicBufferSize, UInt32 cutValue,
2N/A UInt32 *distances, UInt32 maxLen)
2N/A{
2N/A son[_cyclicBufferPos] = curMatch;
2N/A for (;;)
2N/A {
2N/A UInt32 delta = pos - curMatch;
2N/A if (cutValue-- == 0 || delta >= _cyclicBufferSize)
2N/A return distances;
2N/A {
2N/A const Byte *pb = cur - delta;
2N/A curMatch = son[_cyclicBufferPos - delta + ((delta > _cyclicBufferPos) ? _cyclicBufferSize : 0)];
2N/A if (pb[maxLen] == cur[maxLen] && *pb == *cur)
2N/A {
2N/A UInt32 len = 0;
2N/A while(++len != lenLimit)
2N/A if (pb[len] != cur[len])
2N/A break;
2N/A if (maxLen < len)
2N/A {
2N/A *distances++ = maxLen = len;
2N/A *distances++ = delta - 1;
2N/A if (len == lenLimit)
2N/A return distances;
2N/A }
2N/A }
2N/A }
2N/A }
2N/A}
2N/A
2N/AUInt32 * GetMatchesSpec1(UInt32 lenLimit, UInt32 curMatch, UInt32 pos, const Byte *cur, CLzRef *son,
2N/A UInt32 _cyclicBufferPos, UInt32 _cyclicBufferSize, UInt32 cutValue,
2N/A UInt32 *distances, UInt32 maxLen)
2N/A{
2N/A CLzRef *ptr0 = son + (_cyclicBufferPos << 1) + 1;
2N/A CLzRef *ptr1 = son + (_cyclicBufferPos << 1);
2N/A UInt32 len0 = 0, len1 = 0;
2N/A for (;;)
2N/A {
2N/A UInt32 delta = pos - curMatch;
2N/A if (cutValue-- == 0 || delta >= _cyclicBufferSize)
2N/A {
2N/A *ptr0 = *ptr1 = kEmptyHashValue;
2N/A return distances;
2N/A }
2N/A {
2N/A CLzRef *pair = son + ((_cyclicBufferPos - delta + ((delta > _cyclicBufferPos) ? _cyclicBufferSize : 0)) << 1);
2N/A const Byte *pb = cur - delta;
2N/A UInt32 len = (len0 < len1 ? len0 : len1);
2N/A if (pb[len] == cur[len])
2N/A {
2N/A if (++len != lenLimit && pb[len] == cur[len])
2N/A while(++len != lenLimit)
2N/A if (pb[len] != cur[len])
2N/A break;
2N/A if (maxLen < len)
2N/A {
2N/A *distances++ = maxLen = len;
2N/A *distances++ = delta - 1;
2N/A if (len == lenLimit)
2N/A {
2N/A *ptr1 = pair[0];
2N/A *ptr0 = pair[1];
2N/A return distances;
2N/A }
2N/A }
2N/A }
2N/A if (pb[len] < cur[len])
2N/A {
2N/A *ptr1 = curMatch;
2N/A ptr1 = pair + 1;
2N/A curMatch = *ptr1;
2N/A len1 = len;
2N/A }
2N/A else
2N/A {
2N/A *ptr0 = curMatch;
2N/A ptr0 = pair;
2N/A curMatch = *ptr0;
2N/A len0 = len;
2N/A }
2N/A }
2N/A }
2N/A}
2N/A
2N/Astatic void SkipMatchesSpec(UInt32 lenLimit, UInt32 curMatch, UInt32 pos, const Byte *cur, CLzRef *son,
2N/A UInt32 _cyclicBufferPos, UInt32 _cyclicBufferSize, UInt32 cutValue)
2N/A{
2N/A CLzRef *ptr0 = son + (_cyclicBufferPos << 1) + 1;
2N/A CLzRef *ptr1 = son + (_cyclicBufferPos << 1);
2N/A UInt32 len0 = 0, len1 = 0;
2N/A for (;;)
2N/A {
2N/A UInt32 delta = pos - curMatch;
2N/A if (cutValue-- == 0 || delta >= _cyclicBufferSize)
2N/A {
2N/A *ptr0 = *ptr1 = kEmptyHashValue;
2N/A return;
2N/A }
2N/A {
2N/A CLzRef *pair = son + ((_cyclicBufferPos - delta + ((delta > _cyclicBufferPos) ? _cyclicBufferSize : 0)) << 1);
2N/A const Byte *pb = cur - delta;
2N/A UInt32 len = (len0 < len1 ? len0 : len1);
2N/A if (pb[len] == cur[len])
2N/A {
2N/A while(++len != lenLimit)
2N/A if (pb[len] != cur[len])
2N/A break;
2N/A {
2N/A if (len == lenLimit)
2N/A {
2N/A *ptr1 = pair[0];
2N/A *ptr0 = pair[1];
2N/A return;
2N/A }
2N/A }
2N/A }
2N/A if (pb[len] < cur[len])
2N/A {
2N/A *ptr1 = curMatch;
2N/A ptr1 = pair + 1;
2N/A curMatch = *ptr1;
2N/A len1 = len;
2N/A }
2N/A else
2N/A {
2N/A *ptr0 = curMatch;
2N/A ptr0 = pair;
2N/A curMatch = *ptr0;
2N/A len0 = len;
2N/A }
2N/A }
2N/A }
2N/A}
2N/A
2N/A#define MOVE_POS \
2N/A ++p->cyclicBufferPos; \
2N/A p->buffer++; \
2N/A if (++p->pos == p->posLimit) MatchFinder_CheckLimits(p);
2N/A
2N/A#define MOVE_POS_RET MOVE_POS return offset;
2N/A
2N/Astatic void MatchFinder_MovePos(CMatchFinder *p) { MOVE_POS; }
2N/A
2N/A#define GET_MATCHES_HEADER2(minLen, ret_op) \
2N/A UInt32 lenLimit; UInt32 hashValue; const Byte *cur; UInt32 curMatch; \
2N/A lenLimit = p->lenLimit; { if (lenLimit < minLen) { MatchFinder_MovePos(p); ret_op; }} \
2N/A cur = p->buffer;
2N/A
2N/A#define GET_MATCHES_HEADER(minLen) GET_MATCHES_HEADER2(minLen, return 0)
2N/A#define SKIP_HEADER(minLen) GET_MATCHES_HEADER2(minLen, continue)
2N/A
2N/A#define MF_PARAMS(p) p->pos, p->buffer, p->son, p->cyclicBufferPos, p->cyclicBufferSize, p->cutValue
2N/A
2N/A#define GET_MATCHES_FOOTER(offset, maxLen) \
2N/A offset = (UInt32)(GetMatchesSpec1(lenLimit, curMatch, MF_PARAMS(p), \
2N/A distances + offset, maxLen) - distances); MOVE_POS_RET;
2N/A
2N/A#define SKIP_FOOTER \
2N/A SkipMatchesSpec(lenLimit, curMatch, MF_PARAMS(p)); MOVE_POS;
2N/A
2N/Astatic UInt32 Bt2_MatchFinder_GetMatches(CMatchFinder *p, UInt32 *distances)
2N/A{
2N/A UInt32 offset;
2N/A GET_MATCHES_HEADER(2)
2N/A HASH2_CALC;
2N/A curMatch = p->hash[hashValue];
2N/A p->hash[hashValue] = p->pos;
2N/A offset = 0;
2N/A GET_MATCHES_FOOTER(offset, 1)
2N/A}
2N/A
2N/AUInt32 Bt3Zip_MatchFinder_GetMatches(CMatchFinder *p, UInt32 *distances)
2N/A{
2N/A UInt32 offset;
2N/A GET_MATCHES_HEADER(3)
2N/A HASH_ZIP_CALC;
2N/A curMatch = p->hash[hashValue];
2N/A p->hash[hashValue] = p->pos;
2N/A offset = 0;
2N/A GET_MATCHES_FOOTER(offset, 2)
2N/A}
2N/A
2N/Astatic UInt32 Bt3_MatchFinder_GetMatches(CMatchFinder *p, UInt32 *distances)
2N/A{
2N/A UInt32 hash2Value, delta2, maxLen, offset;
2N/A GET_MATCHES_HEADER(3)
2N/A
2N/A HASH3_CALC;
2N/A
2N/A delta2 = p->pos - p->hash[hash2Value];
2N/A curMatch = p->hash[kFix3HashSize + hashValue];
2N/A
2N/A p->hash[hash2Value] =
2N/A p->hash[kFix3HashSize + hashValue] = p->pos;
2N/A
2N/A
2N/A maxLen = 2;
2N/A offset = 0;
2N/A if (delta2 < p->cyclicBufferSize && *(cur - delta2) == *cur)
2N/A {
2N/A for (; maxLen != lenLimit; maxLen++)
2N/A if (cur[(ptrdiff_t)maxLen - delta2] != cur[maxLen])
2N/A break;
2N/A distances[0] = maxLen;
2N/A distances[1] = delta2 - 1;
2N/A offset = 2;
2N/A if (maxLen == lenLimit)
2N/A {
2N/A SkipMatchesSpec(lenLimit, curMatch, MF_PARAMS(p));
2N/A MOVE_POS_RET;
2N/A }
2N/A }
2N/A GET_MATCHES_FOOTER(offset, maxLen)
2N/A}
2N/A
2N/Astatic UInt32 Bt4_MatchFinder_GetMatches(CMatchFinder *p, UInt32 *distances)
2N/A{
2N/A UInt32 hash2Value, hash3Value, delta2, delta3, maxLen, offset;
2N/A GET_MATCHES_HEADER(4)
2N/A
2N/A HASH4_CALC;
2N/A
2N/A delta2 = p->pos - p->hash[ hash2Value];
2N/A delta3 = p->pos - p->hash[kFix3HashSize + hash3Value];
2N/A curMatch = p->hash[kFix4HashSize + hashValue];
2N/A
2N/A p->hash[ hash2Value] =
2N/A p->hash[kFix3HashSize + hash3Value] =
2N/A p->hash[kFix4HashSize + hashValue] = p->pos;
2N/A
2N/A maxLen = 1;
2N/A offset = 0;
2N/A if (delta2 < p->cyclicBufferSize && *(cur - delta2) == *cur)
2N/A {
2N/A distances[0] = maxLen = 2;
2N/A distances[1] = delta2 - 1;
2N/A offset = 2;
2N/A }
2N/A if (delta2 != delta3 && delta3 < p->cyclicBufferSize && *(cur - delta3) == *cur)
2N/A {
2N/A maxLen = 3;
2N/A distances[offset + 1] = delta3 - 1;
2N/A offset += 2;
2N/A delta2 = delta3;
2N/A }
2N/A if (offset != 0)
2N/A {
2N/A for (; maxLen != lenLimit; maxLen++)
2N/A if (cur[(ptrdiff_t)maxLen - delta2] != cur[maxLen])
2N/A break;
2N/A distances[offset - 2] = maxLen;
2N/A if (maxLen == lenLimit)
2N/A {
2N/A SkipMatchesSpec(lenLimit, curMatch, MF_PARAMS(p));
2N/A MOVE_POS_RET;
2N/A }
2N/A }
2N/A if (maxLen < 3)
2N/A maxLen = 3;
2N/A GET_MATCHES_FOOTER(offset, maxLen)
2N/A}
2N/A
2N/Astatic UInt32 Hc4_MatchFinder_GetMatches(CMatchFinder *p, UInt32 *distances)
2N/A{
2N/A UInt32 hash2Value, hash3Value, delta2, delta3, maxLen, offset;
2N/A GET_MATCHES_HEADER(4)
2N/A
2N/A HASH4_CALC;
2N/A
2N/A delta2 = p->pos - p->hash[ hash2Value];
2N/A delta3 = p->pos - p->hash[kFix3HashSize + hash3Value];
2N/A curMatch = p->hash[kFix4HashSize + hashValue];
2N/A
2N/A p->hash[ hash2Value] =
2N/A p->hash[kFix3HashSize + hash3Value] =
2N/A p->hash[kFix4HashSize + hashValue] = p->pos;
2N/A
2N/A maxLen = 1;
2N/A offset = 0;
2N/A if (delta2 < p->cyclicBufferSize && *(cur - delta2) == *cur)
2N/A {
2N/A distances[0] = maxLen = 2;
2N/A distances[1] = delta2 - 1;
2N/A offset = 2;
2N/A }
2N/A if (delta2 != delta3 && delta3 < p->cyclicBufferSize && *(cur - delta3) == *cur)
2N/A {
2N/A maxLen = 3;
2N/A distances[offset + 1] = delta3 - 1;
2N/A offset += 2;
2N/A delta2 = delta3;
2N/A }
2N/A if (offset != 0)
2N/A {
2N/A for (; maxLen != lenLimit; maxLen++)
2N/A if (cur[(ptrdiff_t)maxLen - delta2] != cur[maxLen])
2N/A break;
2N/A distances[offset - 2] = maxLen;
2N/A if (maxLen == lenLimit)
2N/A {
2N/A p->son[p->cyclicBufferPos] = curMatch;
2N/A MOVE_POS_RET;
2N/A }
2N/A }
2N/A if (maxLen < 3)
2N/A maxLen = 3;
2N/A offset = (UInt32)(Hc_GetMatchesSpec(lenLimit, curMatch, MF_PARAMS(p),
2N/A distances + offset, maxLen) - (distances));
2N/A MOVE_POS_RET
2N/A}
2N/A
2N/AUInt32 Hc3Zip_MatchFinder_GetMatches(CMatchFinder *p, UInt32 *distances)
2N/A{
2N/A UInt32 offset;
2N/A GET_MATCHES_HEADER(3)
2N/A HASH_ZIP_CALC;
2N/A curMatch = p->hash[hashValue];
2N/A p->hash[hashValue] = p->pos;
2N/A offset = (UInt32)(Hc_GetMatchesSpec(lenLimit, curMatch, MF_PARAMS(p),
2N/A distances, 2) - (distances));
2N/A MOVE_POS_RET
2N/A}
2N/A
2N/Astatic void Bt2_MatchFinder_Skip(CMatchFinder *p, UInt32 num)
2N/A{
2N/A do
2N/A {
2N/A SKIP_HEADER(2)
2N/A HASH2_CALC;
2N/A curMatch = p->hash[hashValue];
2N/A p->hash[hashValue] = p->pos;
2N/A SKIP_FOOTER
2N/A }
2N/A while (--num != 0);
2N/A}
2N/A
2N/Avoid Bt3Zip_MatchFinder_Skip(CMatchFinder *p, UInt32 num)
2N/A{
2N/A do
2N/A {
2N/A SKIP_HEADER(3)
2N/A HASH_ZIP_CALC;
2N/A curMatch = p->hash[hashValue];
2N/A p->hash[hashValue] = p->pos;
2N/A SKIP_FOOTER
2N/A }
2N/A while (--num != 0);
2N/A}
2N/A
2N/Astatic void Bt3_MatchFinder_Skip(CMatchFinder *p, UInt32 num)
2N/A{
2N/A do
2N/A {
2N/A UInt32 hash2Value;
2N/A SKIP_HEADER(3)
2N/A HASH3_CALC;
2N/A curMatch = p->hash[kFix3HashSize + hashValue];
2N/A p->hash[hash2Value] =
2N/A p->hash[kFix3HashSize + hashValue] = p->pos;
2N/A SKIP_FOOTER
2N/A }
2N/A while (--num != 0);
2N/A}
2N/A
2N/Astatic void Bt4_MatchFinder_Skip(CMatchFinder *p, UInt32 num)
2N/A{
2N/A do
2N/A {
2N/A UInt32 hash2Value, hash3Value;
2N/A SKIP_HEADER(4)
2N/A HASH4_CALC;
2N/A curMatch = p->hash[kFix4HashSize + hashValue];
2N/A p->hash[ hash2Value] =
2N/A p->hash[kFix3HashSize + hash3Value] = p->pos;
2N/A p->hash[kFix4HashSize + hashValue] = p->pos;
2N/A SKIP_FOOTER
2N/A }
2N/A while (--num != 0);
2N/A}
2N/A
2N/Astatic void Hc4_MatchFinder_Skip(CMatchFinder *p, UInt32 num)
2N/A{
2N/A do
2N/A {
2N/A UInt32 hash2Value, hash3Value;
2N/A SKIP_HEADER(4)
2N/A HASH4_CALC;
2N/A curMatch = p->hash[kFix4HashSize + hashValue];
2N/A p->hash[ hash2Value] =
2N/A p->hash[kFix3HashSize + hash3Value] =
2N/A p->hash[kFix4HashSize + hashValue] = p->pos;
2N/A p->son[p->cyclicBufferPos] = curMatch;
2N/A MOVE_POS
2N/A }
2N/A while (--num != 0);
2N/A}
2N/A
2N/Avoid Hc3Zip_MatchFinder_Skip(CMatchFinder *p, UInt32 num)
2N/A{
2N/A do
2N/A {
2N/A SKIP_HEADER(3)
2N/A HASH_ZIP_CALC;
2N/A curMatch = p->hash[hashValue];
2N/A p->hash[hashValue] = p->pos;
2N/A p->son[p->cyclicBufferPos] = curMatch;
2N/A MOVE_POS
2N/A }
2N/A while (--num != 0);
2N/A}
2N/A
2N/Avoid MatchFinder_CreateVTable(CMatchFinder *p, IMatchFinder *vTable)
2N/A{
2N/A vTable->Init = (Mf_Init_Func)MatchFinder_Init;
2N/A vTable->GetIndexByte = (Mf_GetIndexByte_Func)MatchFinder_GetIndexByte;
2N/A vTable->GetNumAvailableBytes = (Mf_GetNumAvailableBytes_Func)MatchFinder_GetNumAvailableBytes;
2N/A vTable->GetPointerToCurrentPos = (Mf_GetPointerToCurrentPos_Func)MatchFinder_GetPointerToCurrentPos;
2N/A if (!p->btMode)
2N/A {
2N/A vTable->GetMatches = (Mf_GetMatches_Func)Hc4_MatchFinder_GetMatches;
2N/A vTable->Skip = (Mf_Skip_Func)Hc4_MatchFinder_Skip;
2N/A }
2N/A else if (p->numHashBytes == 2)
2N/A {
2N/A vTable->GetMatches = (Mf_GetMatches_Func)Bt2_MatchFinder_GetMatches;
2N/A vTable->Skip = (Mf_Skip_Func)Bt2_MatchFinder_Skip;
2N/A }
2N/A else if (p->numHashBytes == 3)
2N/A {
2N/A vTable->GetMatches = (Mf_GetMatches_Func)Bt3_MatchFinder_GetMatches;
2N/A vTable->Skip = (Mf_Skip_Func)Bt3_MatchFinder_Skip;
2N/A }
2N/A else
2N/A {
2N/A vTable->GetMatches = (Mf_GetMatches_Func)Bt4_MatchFinder_GetMatches;
2N/A vTable->Skip = (Mf_Skip_Func)Bt4_MatchFinder_Skip;
2N/A }
2N/A}