+++ /dev/null
-/* LzFind.c -- Match finder for LZ algorithms\r
-2017-06-10 : Igor Pavlov : Public domain */\r
-\r
-#include "Precomp.h"\r
-\r
-#ifndef EFIAPI\r
-#include <string.h>\r
-#endif\r
-\r
-#include "LzFind.h"\r
-#include "LzHash.h"\r
-\r
-#define kEmptyHashValue 0\r
-#define kMaxValForNormalize ((UInt32)0xFFFFFFFF)\r
-#define kNormalizeStepMin (1 << 10) /* it must be power of 2 */\r
-#define kNormalizeMask (~(UInt32)(kNormalizeStepMin - 1))\r
-#define kMaxHistorySize ((UInt32)7 << 29)\r
-\r
-#define kStartMaxLen 3\r
-\r
-static void LzInWindow_Free(CMatchFinder *p, ISzAllocPtr alloc)\r
-{\r
- if (!p->directInput)\r
- {\r
- ISzAlloc_Free(alloc, p->bufferBase);\r
- p->bufferBase = NULL;\r
- }\r
-}\r
-\r
-/* keepSizeBefore + keepSizeAfter + keepSizeReserv must be < 4G) */\r
-\r
-static int LzInWindow_Create(CMatchFinder *p, UInt32 keepSizeReserv, ISzAllocPtr alloc)\r
-{\r
- UInt32 blockSize = p->keepSizeBefore + p->keepSizeAfter + keepSizeReserv;\r
- if (p->directInput)\r
- {\r
- p->blockSize = blockSize;\r
- return 1;\r
- }\r
- if (!p->bufferBase || p->blockSize != blockSize)\r
- {\r
- LzInWindow_Free(p, alloc);\r
- p->blockSize = blockSize;\r
- p->bufferBase = (Byte *)ISzAlloc_Alloc(alloc, (size_t)blockSize);\r
- }\r
- return (p->bufferBase != NULL);\r
-}\r
-\r
-Byte *MatchFinder_GetPointerToCurrentPos(CMatchFinder *p) { return p->buffer; }\r
-\r
-UInt32 MatchFinder_GetNumAvailableBytes(CMatchFinder *p) { return p->streamPos - p->pos; }\r
-\r
-void MatchFinder_ReduceOffsets(CMatchFinder *p, UInt32 subValue)\r
-{\r
- p->posLimit -= subValue;\r
- p->pos -= subValue;\r
- p->streamPos -= subValue;\r
-}\r
-\r
-static void MatchFinder_ReadBlock(CMatchFinder *p)\r
-{\r
- if (p->streamEndWasReached || p->result != SZ_OK)\r
- return;\r
-\r
- /* We use (p->streamPos - p->pos) value. (p->streamPos < p->pos) is allowed. */\r
-\r
- if (p->directInput)\r
- {\r
- UInt32 curSize = 0xFFFFFFFF - (p->streamPos - p->pos);\r
- if (curSize > p->directInputRem)\r
- curSize = (UInt32)p->directInputRem;\r
- p->directInputRem -= curSize;\r
- p->streamPos += curSize;\r
- if (p->directInputRem == 0)\r
- p->streamEndWasReached = 1;\r
- return;\r
- }\r
- \r
- for (;;)\r
- {\r
- Byte *dest = p->buffer + (p->streamPos - p->pos);\r
- size_t size = (p->bufferBase + p->blockSize - dest);\r
- if (size == 0)\r
- return;\r
-\r
- p->result = ISeqInStream_Read(p->stream, dest, &size);\r
- if (p->result != SZ_OK)\r
- return;\r
- if (size == 0)\r
- {\r
- p->streamEndWasReached = 1;\r
- return;\r
- }\r
- p->streamPos += (UInt32)size;\r
- if (p->streamPos - p->pos > p->keepSizeAfter)\r
- return;\r
- }\r
-}\r
-\r
-void MatchFinder_MoveBlock(CMatchFinder *p)\r
-{\r
- memmove(p->bufferBase,\r
- p->buffer - p->keepSizeBefore,\r
- (size_t)(p->streamPos - p->pos) + p->keepSizeBefore);\r
- p->buffer = p->bufferBase + p->keepSizeBefore;\r
-}\r
-\r
-int MatchFinder_NeedMove(CMatchFinder *p)\r
-{\r
- if (p->directInput)\r
- return 0;\r
- /* if (p->streamEndWasReached) return 0; */\r
- return ((size_t)(p->bufferBase + p->blockSize - p->buffer) <= p->keepSizeAfter);\r
-}\r
-\r
-void MatchFinder_ReadIfRequired(CMatchFinder *p)\r
-{\r
- if (p->streamEndWasReached)\r
- return;\r
- if (p->keepSizeAfter >= p->streamPos - p->pos)\r
- MatchFinder_ReadBlock(p);\r
-}\r
-\r
-static void MatchFinder_CheckAndMoveAndRead(CMatchFinder *p)\r
-{\r
- if (MatchFinder_NeedMove(p))\r
- MatchFinder_MoveBlock(p);\r
- MatchFinder_ReadBlock(p);\r
-}\r
-\r
-static void MatchFinder_SetDefaultSettings(CMatchFinder *p)\r
-{\r
- p->cutValue = 32;\r
- p->btMode = 1;\r
- p->numHashBytes = 4;\r
- p->bigHash = 0;\r
-}\r
-\r
-#define kCrcPoly 0xEDB88320\r
-\r
-void MatchFinder_Construct(CMatchFinder *p)\r
-{\r
- UInt32 i;\r
- p->bufferBase = NULL;\r
- p->directInput = 0;\r
- p->hash = NULL;\r
- p->expectedDataSize = (UInt64)(Int64)-1;\r
- MatchFinder_SetDefaultSettings(p);\r
-\r
- for (i = 0; i < 256; i++)\r
- {\r
- UInt32 r = i;\r
- unsigned j;\r
- for (j = 0; j < 8; j++)\r
- r = (r >> 1) ^ (kCrcPoly & ((UInt32)0 - (r & 1)));\r
- p->crc[i] = r;\r
- }\r
-}\r
-\r
-static void MatchFinder_FreeThisClassMemory(CMatchFinder *p, ISzAllocPtr alloc)\r
-{\r
- ISzAlloc_Free(alloc, p->hash);\r
- p->hash = NULL;\r
-}\r
-\r
-void MatchFinder_Free(CMatchFinder *p, ISzAllocPtr alloc)\r
-{\r
- MatchFinder_FreeThisClassMemory(p, alloc);\r
- LzInWindow_Free(p, alloc);\r
-}\r
-\r
-static CLzRef* AllocRefs(size_t num, ISzAllocPtr alloc)\r
-{\r
- size_t sizeInBytes = (size_t)num * sizeof(CLzRef);\r
- if (sizeInBytes / sizeof(CLzRef) != num)\r
- return NULL;\r
- return (CLzRef *)ISzAlloc_Alloc(alloc, sizeInBytes);\r
-}\r
-\r
-int MatchFinder_Create(CMatchFinder *p, UInt32 historySize,\r
- UInt32 keepAddBufferBefore, UInt32 matchMaxLen, UInt32 keepAddBufferAfter,\r
- ISzAllocPtr alloc)\r
-{\r
- UInt32 sizeReserv;\r
- \r
- if (historySize > kMaxHistorySize)\r
- {\r
- MatchFinder_Free(p, alloc);\r
- return 0;\r
- }\r
- \r
- sizeReserv = historySize >> 1;\r
- if (historySize >= ((UInt32)3 << 30)) sizeReserv = historySize >> 3;\r
- else if (historySize >= ((UInt32)2 << 30)) sizeReserv = historySize >> 2;\r
- \r
- sizeReserv += (keepAddBufferBefore + matchMaxLen + keepAddBufferAfter) / 2 + (1 << 19);\r
-\r
- p->keepSizeBefore = historySize + keepAddBufferBefore + 1;\r
- p->keepSizeAfter = matchMaxLen + keepAddBufferAfter;\r
- \r
- /* we need one additional byte, since we use MoveBlock after pos++ and before dictionary using */\r
- \r
- if (LzInWindow_Create(p, sizeReserv, alloc))\r
- {\r
- UInt32 newCyclicBufferSize = historySize + 1;\r
- UInt32 hs;\r
- p->matchMaxLen = matchMaxLen;\r
- {\r
- p->fixedHashSize = 0;\r
- if (p->numHashBytes == 2)\r
- hs = (1 << 16) - 1;\r
- else\r
- {\r
- hs = historySize;\r
- if (hs > p->expectedDataSize)\r
- hs = (UInt32)p->expectedDataSize;\r
- if (hs != 0)\r
- hs--;\r
- hs |= (hs >> 1);\r
- hs |= (hs >> 2);\r
- hs |= (hs >> 4);\r
- hs |= (hs >> 8);\r
- hs >>= 1;\r
- hs |= 0xFFFF; /* don't change it! It's required for Deflate */\r
- if (hs > (1 << 24))\r
- {\r
- if (p->numHashBytes == 3)\r
- hs = (1 << 24) - 1;\r
- else\r
- hs >>= 1;\r
- /* if (bigHash) mode, GetHeads4b() in LzFindMt.c needs (hs >= ((1 << 24) - 1))) */\r
- }\r
- }\r
- p->hashMask = hs;\r
- hs++;\r
- if (p->numHashBytes > 2) p->fixedHashSize += kHash2Size;\r
- if (p->numHashBytes > 3) p->fixedHashSize += kHash3Size;\r
- if (p->numHashBytes > 4) p->fixedHashSize += kHash4Size;\r
- hs += p->fixedHashSize;\r
- }\r
-\r
- {\r
- size_t newSize;\r
- size_t numSons;\r
- p->historySize = historySize;\r
- p->hashSizeSum = hs;\r
- p->cyclicBufferSize = newCyclicBufferSize;\r
- \r
- numSons = newCyclicBufferSize;\r
- if (p->btMode)\r
- numSons <<= 1;\r
- newSize = hs + numSons;\r
-\r
- if (p->hash && p->numRefs == newSize)\r
- return 1;\r
- \r
- MatchFinder_FreeThisClassMemory(p, alloc);\r
- p->numRefs = newSize;\r
- p->hash = AllocRefs(newSize, alloc);\r
- \r
- if (p->hash)\r
- {\r
- p->son = p->hash + p->hashSizeSum;\r
- return 1;\r
- }\r
- }\r
- }\r
-\r
- MatchFinder_Free(p, alloc);\r
- return 0;\r
-}\r
-\r
-static void MatchFinder_SetLimits(CMatchFinder *p)\r
-{\r
- UInt32 limit = kMaxValForNormalize - p->pos;\r
- UInt32 limit2 = p->cyclicBufferSize - p->cyclicBufferPos;\r
- \r
- if (limit2 < limit)\r
- limit = limit2;\r
- limit2 = p->streamPos - p->pos;\r
- \r
- if (limit2 <= p->keepSizeAfter)\r
- {\r
- if (limit2 > 0)\r
- limit2 = 1;\r
- }\r
- else\r
- limit2 -= p->keepSizeAfter;\r
- \r
- if (limit2 < limit)\r
- limit = limit2;\r
- \r
- {\r
- UInt32 lenLimit = p->streamPos - p->pos;\r
- if (lenLimit > p->matchMaxLen)\r
- lenLimit = p->matchMaxLen;\r
- p->lenLimit = lenLimit;\r
- }\r
- p->posLimit = p->pos + limit;\r
-}\r
-\r
-\r
-void MatchFinder_Init_LowHash(CMatchFinder *p)\r
-{\r
- size_t i;\r
- CLzRef *items = p->hash;\r
- size_t numItems = p->fixedHashSize;\r
- for (i = 0; i < numItems; i++)\r
- items[i] = kEmptyHashValue;\r
-}\r
-\r
-\r
-void MatchFinder_Init_HighHash(CMatchFinder *p)\r
-{\r
- size_t i;\r
- CLzRef *items = p->hash + p->fixedHashSize;\r
- size_t numItems = (size_t)p->hashMask + 1;\r
- for (i = 0; i < numItems; i++)\r
- items[i] = kEmptyHashValue;\r
-}\r
-\r
-\r
-void MatchFinder_Init_3(CMatchFinder *p, int readData)\r
-{\r
- p->cyclicBufferPos = 0;\r
- p->buffer = p->bufferBase;\r
- p->pos =\r
- p->streamPos = p->cyclicBufferSize;\r
- p->result = SZ_OK;\r
- p->streamEndWasReached = 0;\r
- \r
- if (readData)\r
- MatchFinder_ReadBlock(p);\r
- \r
- MatchFinder_SetLimits(p);\r
-}\r
-\r
-\r
-void MatchFinder_Init(CMatchFinder *p)\r
-{\r
- MatchFinder_Init_HighHash(p);\r
- MatchFinder_Init_LowHash(p);\r
- MatchFinder_Init_3(p, True);\r
-}\r
-\r
- \r
-static UInt32 MatchFinder_GetSubValue(CMatchFinder *p)\r
-{\r
- return (p->pos - p->historySize - 1) & kNormalizeMask;\r
-}\r
-\r
-void MatchFinder_Normalize3(UInt32 subValue, CLzRef *items, size_t numItems)\r
-{\r
- size_t i;\r
- for (i = 0; i < numItems; i++)\r
- {\r
- UInt32 value = items[i];\r
- if (value <= subValue)\r
- value = kEmptyHashValue;\r
- else\r
- value -= subValue;\r
- items[i] = value;\r
- }\r
-}\r
-\r
-static void MatchFinder_Normalize(CMatchFinder *p)\r
-{\r
- UInt32 subValue = MatchFinder_GetSubValue(p);\r
- MatchFinder_Normalize3(subValue, p->hash, p->numRefs);\r
- MatchFinder_ReduceOffsets(p, subValue);\r
-}\r
-\r
-static void MatchFinder_CheckLimits(CMatchFinder *p)\r
-{\r
- if (p->pos == kMaxValForNormalize)\r
- MatchFinder_Normalize(p);\r
- if (!p->streamEndWasReached && p->keepSizeAfter == p->streamPos - p->pos)\r
- MatchFinder_CheckAndMoveAndRead(p);\r
- if (p->cyclicBufferPos == p->cyclicBufferSize)\r
- p->cyclicBufferPos = 0;\r
- MatchFinder_SetLimits(p);\r
-}\r
-\r
-static UInt32 * Hc_GetMatchesSpec(UInt32 lenLimit, UInt32 curMatch, UInt32 pos, const Byte *cur, CLzRef *son,\r
- UInt32 _cyclicBufferPos, UInt32 _cyclicBufferSize, UInt32 cutValue,\r
- UInt32 *distances, UInt32 maxLen)\r
-{\r
- son[_cyclicBufferPos] = curMatch;\r
- for (;;)\r
- {\r
- UInt32 delta = pos - curMatch;\r
- if (cutValue-- == 0 || delta >= _cyclicBufferSize)\r
- return distances;\r
- {\r
- const Byte *pb = cur - delta;\r
- curMatch = son[_cyclicBufferPos - delta + ((delta > _cyclicBufferPos) ? _cyclicBufferSize : 0)];\r
- if (pb[maxLen] == cur[maxLen] && *pb == *cur)\r
- {\r
- UInt32 len = 0;\r
- while (++len != lenLimit)\r
- if (pb[len] != cur[len])\r
- break;\r
- if (maxLen < len)\r
- {\r
- *distances++ = maxLen = len;\r
- *distances++ = delta - 1;\r
- if (len == lenLimit)\r
- return distances;\r
- }\r
- }\r
- }\r
- }\r
-}\r
-\r
-UInt32 * GetMatchesSpec1(UInt32 lenLimit, UInt32 curMatch, UInt32 pos, const Byte *cur, CLzRef *son,\r
- UInt32 _cyclicBufferPos, UInt32 _cyclicBufferSize, UInt32 cutValue,\r
- UInt32 *distances, UInt32 maxLen)\r
-{\r
- CLzRef *ptr0 = son + (_cyclicBufferPos << 1) + 1;\r
- CLzRef *ptr1 = son + (_cyclicBufferPos << 1);\r
- UInt32 len0 = 0, len1 = 0;\r
- for (;;)\r
- {\r
- UInt32 delta = pos - curMatch;\r
- if (cutValue-- == 0 || delta >= _cyclicBufferSize)\r
- {\r
- *ptr0 = *ptr1 = kEmptyHashValue;\r
- return distances;\r
- }\r
- {\r
- CLzRef *pair = son + ((_cyclicBufferPos - delta + ((delta > _cyclicBufferPos) ? _cyclicBufferSize : 0)) << 1);\r
- const Byte *pb = cur - delta;\r
- UInt32 len = (len0 < len1 ? len0 : len1);\r
- if (pb[len] == cur[len])\r
- {\r
- if (++len != lenLimit && pb[len] == cur[len])\r
- while (++len != lenLimit)\r
- if (pb[len] != cur[len])\r
- break;\r
- if (maxLen < len)\r
- {\r
- *distances++ = maxLen = len;\r
- *distances++ = delta - 1;\r
- if (len == lenLimit)\r
- {\r
- *ptr1 = pair[0];\r
- *ptr0 = pair[1];\r
- return distances;\r
- }\r
- }\r
- }\r
- if (pb[len] < cur[len])\r
- {\r
- *ptr1 = curMatch;\r
- ptr1 = pair + 1;\r
- curMatch = *ptr1;\r
- len1 = len;\r
- }\r
- else\r
- {\r
- *ptr0 = curMatch;\r
- ptr0 = pair;\r
- curMatch = *ptr0;\r
- len0 = len;\r
- }\r
- }\r
- }\r
-}\r
-\r
-static void SkipMatchesSpec(UInt32 lenLimit, UInt32 curMatch, UInt32 pos, const Byte *cur, CLzRef *son,\r
- UInt32 _cyclicBufferPos, UInt32 _cyclicBufferSize, UInt32 cutValue)\r
-{\r
- CLzRef *ptr0 = son + (_cyclicBufferPos << 1) + 1;\r
- CLzRef *ptr1 = son + (_cyclicBufferPos << 1);\r
- UInt32 len0 = 0, len1 = 0;\r
- for (;;)\r
- {\r
- UInt32 delta = pos - curMatch;\r
- if (cutValue-- == 0 || delta >= _cyclicBufferSize)\r
- {\r
- *ptr0 = *ptr1 = kEmptyHashValue;\r
- return;\r
- }\r
- {\r
- CLzRef *pair = son + ((_cyclicBufferPos - delta + ((delta > _cyclicBufferPos) ? _cyclicBufferSize : 0)) << 1);\r
- const Byte *pb = cur - delta;\r
- UInt32 len = (len0 < len1 ? len0 : len1);\r
- if (pb[len] == cur[len])\r
- {\r
- while (++len != lenLimit)\r
- if (pb[len] != cur[len])\r
- break;\r
- {\r
- if (len == lenLimit)\r
- {\r
- *ptr1 = pair[0];\r
- *ptr0 = pair[1];\r
- return;\r
- }\r
- }\r
- }\r
- if (pb[len] < cur[len])\r
- {\r
- *ptr1 = curMatch;\r
- ptr1 = pair + 1;\r
- curMatch = *ptr1;\r
- len1 = len;\r
- }\r
- else\r
- {\r
- *ptr0 = curMatch;\r
- ptr0 = pair;\r
- curMatch = *ptr0;\r
- len0 = len;\r
- }\r
- }\r
- }\r
-}\r
-\r
-#define MOVE_POS \\r
- ++p->cyclicBufferPos; \\r
- p->buffer++; \\r
- if (++p->pos == p->posLimit) MatchFinder_CheckLimits(p);\r
-\r
-#define MOVE_POS_RET MOVE_POS return offset;\r
-\r
-static void MatchFinder_MovePos(CMatchFinder *p) { MOVE_POS; }\r
-\r
-#define GET_MATCHES_HEADER2(minLen, ret_op) \\r
- UInt32 lenLimit; UInt32 hv; const Byte *cur; UInt32 curMatch; \\r
- lenLimit = p->lenLimit; { if (lenLimit < minLen) { MatchFinder_MovePos(p); ret_op; }} \\r
- cur = p->buffer;\r
-\r
-#define GET_MATCHES_HEADER(minLen) GET_MATCHES_HEADER2(minLen, return 0)\r
-#define SKIP_HEADER(minLen) GET_MATCHES_HEADER2(minLen, continue)\r
-\r
-#define MF_PARAMS(p) p->pos, p->buffer, p->son, p->cyclicBufferPos, p->cyclicBufferSize, p->cutValue\r
-\r
-#define GET_MATCHES_FOOTER(offset, maxLen) \\r
- offset = (UInt32)(GetMatchesSpec1(lenLimit, curMatch, MF_PARAMS(p), \\r
- distances + offset, maxLen) - distances); MOVE_POS_RET;\r
-\r
-#define SKIP_FOOTER \\r
- SkipMatchesSpec(lenLimit, curMatch, MF_PARAMS(p)); MOVE_POS;\r
-\r
-#define UPDATE_maxLen { \\r
- ptrdiff_t diff = (ptrdiff_t)0 - d2; \\r
- const Byte *c = cur + maxLen; \\r
- const Byte *lim = cur + lenLimit; \\r
- for (; c != lim; c++) if (*(c + diff) != *c) break; \\r
- maxLen = (UInt32)(c - cur); }\r
-\r
-static UInt32 Bt2_MatchFinder_GetMatches(CMatchFinder *p, UInt32 *distances)\r
-{\r
- UInt32 offset;\r
- GET_MATCHES_HEADER(2)\r
- HASH2_CALC;\r
- curMatch = p->hash[hv];\r
- p->hash[hv] = p->pos;\r
- offset = 0;\r
- GET_MATCHES_FOOTER(offset, 1)\r
-}\r
-\r
-UInt32 Bt3Zip_MatchFinder_GetMatches(CMatchFinder *p, UInt32 *distances)\r
-{\r
- UInt32 offset;\r
- GET_MATCHES_HEADER(3)\r
- HASH_ZIP_CALC;\r
- curMatch = p->hash[hv];\r
- p->hash[hv] = p->pos;\r
- offset = 0;\r
- GET_MATCHES_FOOTER(offset, 2)\r
-}\r
-\r
-static UInt32 Bt3_MatchFinder_GetMatches(CMatchFinder *p, UInt32 *distances)\r
-{\r
- UInt32 h2, d2, maxLen, offset, pos;\r
- UInt32 *hash;\r
- GET_MATCHES_HEADER(3)\r
-\r
- HASH3_CALC;\r
-\r
- hash = p->hash;\r
- pos = p->pos;\r
-\r
- d2 = pos - hash[h2];\r
-\r
- curMatch = (hash + kFix3HashSize)[hv];\r
- \r
- hash[h2] = pos;\r
- (hash + kFix3HashSize)[hv] = pos;\r
-\r
- maxLen = 2;\r
- offset = 0;\r
-\r
- if (d2 < p->cyclicBufferSize && *(cur - d2) == *cur)\r
- {\r
- UPDATE_maxLen\r
- distances[0] = maxLen;\r
- distances[1] = d2 - 1;\r
- offset = 2;\r
- if (maxLen == lenLimit)\r
- {\r
- SkipMatchesSpec(lenLimit, curMatch, MF_PARAMS(p));\r
- MOVE_POS_RET;\r
- }\r
- }\r
- \r
- GET_MATCHES_FOOTER(offset, maxLen)\r
-}\r
-\r
-static UInt32 Bt4_MatchFinder_GetMatches(CMatchFinder *p, UInt32 *distances)\r
-{\r
- UInt32 h2, h3, d2, d3, maxLen, offset, pos;\r
- UInt32 *hash;\r
- GET_MATCHES_HEADER(4)\r
-\r
- HASH4_CALC;\r
-\r
- hash = p->hash;\r
- pos = p->pos;\r
-\r
- d2 = pos - hash[ h2];\r
- d3 = pos - (hash + kFix3HashSize)[h3];\r
-\r
- curMatch = (hash + kFix4HashSize)[hv];\r
-\r
- hash[ h2] = pos;\r
- (hash + kFix3HashSize)[h3] = pos;\r
- (hash + kFix4HashSize)[hv] = pos;\r
-\r
- maxLen = 0;\r
- offset = 0;\r
- \r
- if (d2 < p->cyclicBufferSize && *(cur - d2) == *cur)\r
- {\r
- distances[0] = maxLen = 2;\r
- distances[1] = d2 - 1;\r
- offset = 2;\r
- }\r
- \r
- if (d2 != d3 && d3 < p->cyclicBufferSize && *(cur - d3) == *cur)\r
- {\r
- maxLen = 3;\r
- distances[(size_t)offset + 1] = d3 - 1;\r
- offset += 2;\r
- d2 = d3;\r
- }\r
- \r
- if (offset != 0)\r
- {\r
- UPDATE_maxLen\r
- distances[(size_t)offset - 2] = maxLen;\r
- if (maxLen == lenLimit)\r
- {\r
- SkipMatchesSpec(lenLimit, curMatch, MF_PARAMS(p));\r
- MOVE_POS_RET;\r
- }\r
- }\r
- \r
- if (maxLen < 3)\r
- maxLen = 3;\r
- \r
- GET_MATCHES_FOOTER(offset, maxLen)\r
-}\r
-\r
-/*\r
-static UInt32 Bt5_MatchFinder_GetMatches(CMatchFinder *p, UInt32 *distances)\r
-{\r
- UInt32 h2, h3, h4, d2, d3, d4, maxLen, offset, pos;\r
- UInt32 *hash;\r
- GET_MATCHES_HEADER(5)\r
-\r
- HASH5_CALC;\r
-\r
- hash = p->hash;\r
- pos = p->pos;\r
-\r
- d2 = pos - hash[ h2];\r
- d3 = pos - (hash + kFix3HashSize)[h3];\r
- d4 = pos - (hash + kFix4HashSize)[h4];\r
-\r
- curMatch = (hash + kFix5HashSize)[hv];\r
-\r
- hash[ h2] = pos;\r
- (hash + kFix3HashSize)[h3] = pos;\r
- (hash + kFix4HashSize)[h4] = pos;\r
- (hash + kFix5HashSize)[hv] = pos;\r
-\r
- maxLen = 0;\r
- offset = 0;\r
-\r
- if (d2 < p->cyclicBufferSize && *(cur - d2) == *cur)\r
- {\r
- distances[0] = maxLen = 2;\r
- distances[1] = d2 - 1;\r
- offset = 2;\r
- if (*(cur - d2 + 2) == cur[2])\r
- distances[0] = maxLen = 3;\r
- else if (d3 < p->cyclicBufferSize && *(cur - d3) == *cur)\r
- {\r
- distances[2] = maxLen = 3;\r
- distances[3] = d3 - 1;\r
- offset = 4;\r
- d2 = d3;\r
- }\r
- }\r
- else if (d3 < p->cyclicBufferSize && *(cur - d3) == *cur)\r
- {\r
- distances[0] = maxLen = 3;\r
- distances[1] = d3 - 1;\r
- offset = 2;\r
- d2 = d3;\r
- }\r
- \r
- if (d2 != d4 && d4 < p->cyclicBufferSize\r
- && *(cur - d4) == *cur\r
- && *(cur - d4 + 3) == *(cur + 3))\r
- {\r
- maxLen = 4;\r
- distances[(size_t)offset + 1] = d4 - 1;\r
- offset += 2;\r
- d2 = d4;\r
- }\r
- \r
- if (offset != 0)\r
- {\r
- UPDATE_maxLen\r
- distances[(size_t)offset - 2] = maxLen;\r
- if (maxLen == lenLimit)\r
- {\r
- SkipMatchesSpec(lenLimit, curMatch, MF_PARAMS(p));\r
- MOVE_POS_RET;\r
- }\r
- }\r
-\r
- if (maxLen < 4)\r
- maxLen = 4;\r
- \r
- GET_MATCHES_FOOTER(offset, maxLen)\r
-}\r
-*/\r
-\r
-static UInt32 Hc4_MatchFinder_GetMatches(CMatchFinder *p, UInt32 *distances)\r
-{\r
- UInt32 h2, h3, d2, d3, maxLen, offset, pos;\r
- UInt32 *hash;\r
- GET_MATCHES_HEADER(4)\r
-\r
- HASH4_CALC;\r
-\r
- hash = p->hash;\r
- pos = p->pos;\r
- \r
- d2 = pos - hash[ h2];\r
- d3 = pos - (hash + kFix3HashSize)[h3];\r
- \r
- curMatch = (hash + kFix4HashSize)[hv];\r
-\r
- hash[ h2] = pos;\r
- (hash + kFix3HashSize)[h3] = pos;\r
- (hash + kFix4HashSize)[hv] = pos;\r
-\r
- maxLen = 0;\r
- offset = 0;\r
-\r
- if (d2 < p->cyclicBufferSize && *(cur - d2) == *cur)\r
- {\r
- distances[0] = maxLen = 2;\r
- distances[1] = d2 - 1;\r
- offset = 2;\r
- }\r
- \r
- if (d2 != d3 && d3 < p->cyclicBufferSize && *(cur - d3) == *cur)\r
- {\r
- maxLen = 3;\r
- distances[(size_t)offset + 1] = d3 - 1;\r
- offset += 2;\r
- d2 = d3;\r
- }\r
- \r
- if (offset != 0)\r
- {\r
- UPDATE_maxLen\r
- distances[(size_t)offset - 2] = maxLen;\r
- if (maxLen == lenLimit)\r
- {\r
- p->son[p->cyclicBufferPos] = curMatch;\r
- MOVE_POS_RET;\r
- }\r
- }\r
- \r
- if (maxLen < 3)\r
- maxLen = 3;\r
-\r
- offset = (UInt32)(Hc_GetMatchesSpec(lenLimit, curMatch, MF_PARAMS(p),\r
- distances + offset, maxLen) - (distances));\r
- MOVE_POS_RET\r
-}\r
-\r
-/*\r
-static UInt32 Hc5_MatchFinder_GetMatches(CMatchFinder *p, UInt32 *distances)\r
-{\r
- UInt32 h2, h3, h4, d2, d3, d4, maxLen, offset, pos\r
- UInt32 *hash;\r
- GET_MATCHES_HEADER(5)\r
-\r
- HASH5_CALC;\r
-\r
- hash = p->hash;\r
- pos = p->pos;\r
- \r
- d2 = pos - hash[ h2];\r
- d3 = pos - (hash + kFix3HashSize)[h3];\r
- d4 = pos - (hash + kFix4HashSize)[h4];\r
-\r
- curMatch = (hash + kFix5HashSize)[hv];\r
-\r
- hash[ h2] = pos;\r
- (hash + kFix3HashSize)[h3] = pos;\r
- (hash + kFix4HashSize)[h4] = pos;\r
- (hash + kFix5HashSize)[hv] = pos;\r
-\r
- maxLen = 0;\r
- offset = 0;\r
-\r
- if (d2 < p->cyclicBufferSize && *(cur - d2) == *cur)\r
- {\r
- distances[0] = maxLen = 2;\r
- distances[1] = d2 - 1;\r
- offset = 2;\r
- if (*(cur - d2 + 2) == cur[2])\r
- distances[0] = maxLen = 3;\r
- else if (d3 < p->cyclicBufferSize && *(cur - d3) == *cur)\r
- {\r
- distances[2] = maxLen = 3;\r
- distances[3] = d3 - 1;\r
- offset = 4;\r
- d2 = d3;\r
- }\r
- }\r
- else if (d3 < p->cyclicBufferSize && *(cur - d3) == *cur)\r
- {\r
- distances[0] = maxLen = 3;\r
- distances[1] = d3 - 1;\r
- offset = 2;\r
- d2 = d3;\r
- }\r
- \r
- if (d2 != d4 && d4 < p->cyclicBufferSize\r
- && *(cur - d4) == *cur\r
- && *(cur - d4 + 3) == *(cur + 3))\r
- {\r
- maxLen = 4;\r
- distances[(size_t)offset + 1] = d4 - 1;\r
- offset += 2;\r
- d2 = d4;\r
- }\r
- \r
- if (offset != 0)\r
- {\r
- UPDATE_maxLen\r
- distances[(size_t)offset - 2] = maxLen;\r
- if (maxLen == lenLimit)\r
- {\r
- p->son[p->cyclicBufferPos] = curMatch;\r
- MOVE_POS_RET;\r
- }\r
- }\r
- \r
- if (maxLen < 4)\r
- maxLen = 4;\r
-\r
- offset = (UInt32)(Hc_GetMatchesSpec(lenLimit, curMatch, MF_PARAMS(p),\r
- distances + offset, maxLen) - (distances));\r
- MOVE_POS_RET\r
-}\r
-*/\r
-\r
-UInt32 Hc3Zip_MatchFinder_GetMatches(CMatchFinder *p, UInt32 *distances)\r
-{\r
- UInt32 offset;\r
- GET_MATCHES_HEADER(3)\r
- HASH_ZIP_CALC;\r
- curMatch = p->hash[hv];\r
- p->hash[hv] = p->pos;\r
- offset = (UInt32)(Hc_GetMatchesSpec(lenLimit, curMatch, MF_PARAMS(p),\r
- distances, 2) - (distances));\r
- MOVE_POS_RET\r
-}\r
-\r
-static void Bt2_MatchFinder_Skip(CMatchFinder *p, UInt32 num)\r
-{\r
- do\r
- {\r
- SKIP_HEADER(2)\r
- HASH2_CALC;\r
- curMatch = p->hash[hv];\r
- p->hash[hv] = p->pos;\r
- SKIP_FOOTER\r
- }\r
- while (--num != 0);\r
-}\r
-\r
-void Bt3Zip_MatchFinder_Skip(CMatchFinder *p, UInt32 num)\r
-{\r
- do\r
- {\r
- SKIP_HEADER(3)\r
- HASH_ZIP_CALC;\r
- curMatch = p->hash[hv];\r
- p->hash[hv] = p->pos;\r
- SKIP_FOOTER\r
- }\r
- while (--num != 0);\r
-}\r
-\r
-static void Bt3_MatchFinder_Skip(CMatchFinder *p, UInt32 num)\r
-{\r
- do\r
- {\r
- UInt32 h2;\r
- UInt32 *hash;\r
- SKIP_HEADER(3)\r
- HASH3_CALC;\r
- hash = p->hash;\r
- curMatch = (hash + kFix3HashSize)[hv];\r
- hash[h2] =\r
- (hash + kFix3HashSize)[hv] = p->pos;\r
- SKIP_FOOTER\r
- }\r
- while (--num != 0);\r
-}\r
-\r
-static void Bt4_MatchFinder_Skip(CMatchFinder *p, UInt32 num)\r
-{\r
- do\r
- {\r
- UInt32 h2, h3;\r
- UInt32 *hash;\r
- SKIP_HEADER(4)\r
- HASH4_CALC;\r
- hash = p->hash;\r
- curMatch = (hash + kFix4HashSize)[hv];\r
- hash[ h2] =\r
- (hash + kFix3HashSize)[h3] =\r
- (hash + kFix4HashSize)[hv] = p->pos;\r
- SKIP_FOOTER\r
- }\r
- while (--num != 0);\r
-}\r
-\r
-/*\r
-static void Bt5_MatchFinder_Skip(CMatchFinder *p, UInt32 num)\r
-{\r
- do\r
- {\r
- UInt32 h2, h3, h4;\r
- UInt32 *hash;\r
- SKIP_HEADER(5)\r
- HASH5_CALC;\r
- hash = p->hash;\r
- curMatch = (hash + kFix5HashSize)[hv];\r
- hash[ h2] =\r
- (hash + kFix3HashSize)[h3] =\r
- (hash + kFix4HashSize)[h4] =\r
- (hash + kFix5HashSize)[hv] = p->pos;\r
- SKIP_FOOTER\r
- }\r
- while (--num != 0);\r
-}\r
-*/\r
-\r
-static void Hc4_MatchFinder_Skip(CMatchFinder *p, UInt32 num)\r
-{\r
- do\r
- {\r
- UInt32 h2, h3;\r
- UInt32 *hash;\r
- SKIP_HEADER(4)\r
- HASH4_CALC;\r
- hash = p->hash;\r
- curMatch = (hash + kFix4HashSize)[hv];\r
- hash[ h2] =\r
- (hash + kFix3HashSize)[h3] =\r
- (hash + kFix4HashSize)[hv] = p->pos;\r
- p->son[p->cyclicBufferPos] = curMatch;\r
- MOVE_POS\r
- }\r
- while (--num != 0);\r
-}\r
-\r
-/*\r
-static void Hc5_MatchFinder_Skip(CMatchFinder *p, UInt32 num)\r
-{\r
- do\r
- {\r
- UInt32 h2, h3, h4;\r
- UInt32 *hash;\r
- SKIP_HEADER(5)\r
- HASH5_CALC;\r
- hash = p->hash;\r
- curMatch = hash + kFix5HashSize)[hv];\r
- hash[ h2] =\r
- (hash + kFix3HashSize)[h3] =\r
- (hash + kFix4HashSize)[h4] =\r
- (hash + kFix5HashSize)[hv] = p->pos;\r
- p->son[p->cyclicBufferPos] = curMatch;\r
- MOVE_POS\r
- }\r
- while (--num != 0);\r
-}\r
-*/\r
-\r
-void Hc3Zip_MatchFinder_Skip(CMatchFinder *p, UInt32 num)\r
-{\r
- do\r
- {\r
- SKIP_HEADER(3)\r
- HASH_ZIP_CALC;\r
- curMatch = p->hash[hv];\r
- p->hash[hv] = p->pos;\r
- p->son[p->cyclicBufferPos] = curMatch;\r
- MOVE_POS\r
- }\r
- while (--num != 0);\r
-}\r
-\r
-void MatchFinder_CreateVTable(CMatchFinder *p, IMatchFinder *vTable)\r
-{\r
- vTable->Init = (Mf_Init_Func)MatchFinder_Init;\r
- vTable->GetNumAvailableBytes = (Mf_GetNumAvailableBytes_Func)MatchFinder_GetNumAvailableBytes;\r
- vTable->GetPointerToCurrentPos = (Mf_GetPointerToCurrentPos_Func)MatchFinder_GetPointerToCurrentPos;\r
- if (!p->btMode)\r
- {\r
- /* if (p->numHashBytes <= 4) */\r
- {\r
- vTable->GetMatches = (Mf_GetMatches_Func)Hc4_MatchFinder_GetMatches;\r
- vTable->Skip = (Mf_Skip_Func)Hc4_MatchFinder_Skip;\r
- }\r
- /*\r
- else\r
- {\r
- vTable->GetMatches = (Mf_GetMatches_Func)Hc5_MatchFinder_GetMatches;\r
- vTable->Skip = (Mf_Skip_Func)Hc5_MatchFinder_Skip;\r
- }\r
- */\r
- }\r
- else if (p->numHashBytes == 2)\r
- {\r
- vTable->GetMatches = (Mf_GetMatches_Func)Bt2_MatchFinder_GetMatches;\r
- vTable->Skip = (Mf_Skip_Func)Bt2_MatchFinder_Skip;\r
- }\r
- else if (p->numHashBytes == 3)\r
- {\r
- vTable->GetMatches = (Mf_GetMatches_Func)Bt3_MatchFinder_GetMatches;\r
- vTable->Skip = (Mf_Skip_Func)Bt3_MatchFinder_Skip;\r
- }\r
- else /* if (p->numHashBytes == 4) */\r
- {\r
- vTable->GetMatches = (Mf_GetMatches_Func)Bt4_MatchFinder_GetMatches;\r
- vTable->Skip = (Mf_Skip_Func)Bt4_MatchFinder_Skip;\r
- }\r
- /*\r
- else\r
- {\r
- vTable->GetMatches = (Mf_GetMatches_Func)Bt5_MatchFinder_GetMatches;\r
- vTable->Skip = (Mf_Skip_Func)Bt5_MatchFinder_Skip;\r
- }\r
- */\r
-}\r