]> git.proxmox.com Git - mirror_edk2.git/blobdiff - IntelFrameworkModulePkg/Library/LzmaCustomDecompressLib/Sdk/C/LzFind.c
ShellPkg: acpiview: Make '-h' option not require a parameter
[mirror_edk2.git] / IntelFrameworkModulePkg / Library / LzmaCustomDecompressLib / Sdk / C / LzFind.c
index 7b2cd74ae27dea81afd3079a32d5f6821dadf70a..5128fc9518b865d81cc5a27a71b34ddb723b3f0a 100644 (file)
@@ -1,26 +1,11 @@
-/** @file\r
-  LzFind.c\r
+/* LzFind.c -- Match finder for LZ algorithms\r
+2017-06-10 : Igor Pavlov : Public domain */\r
 \r
-  Based on LZMA SDK 4.65:\r
-    LzFind.c -- Match finder for LZ algorithms\r
-    2008-10-04 : Igor Pavlov : Public domain\r
-\r
-  Copyright (c) 2009, Intel Corporation<BR>\r
-  All rights reserved. This program and the accompanying materials\r
-  are licensed and made available under the terms and conditions of the BSD License\r
-  which accompanies this distribution.  The full text of the license may be found at\r
-  http://opensource.org/licenses/bsd-license.php\r
-\r
-  THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
-  WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
-\r
-**/\r
+#include "Precomp.h"\r
 \r
 #ifndef EFIAPI\r
-\r
 #include <string.h>\r
-\r
-#endif // !EFIAPI\r
+#endif\r
 \r
 #include "LzFind.h"\r
 #include "LzHash.h"\r
 #define kEmptyHashValue 0\r
 #define kMaxValForNormalize ((UInt32)0xFFFFFFFF)\r
 #define kNormalizeStepMin (1 << 10) /* it must be power of 2 */\r
-#define kNormalizeMask (~(kNormalizeStepMin - 1))\r
-#define kMaxHistorySize ((UInt32)3 << 30)\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, ISzAlloc *alloc)\r
+static void LzInWindow_Free(CMatchFinder *p, ISzAllocPtr alloc)\r
 {\r
   if (!p->directInput)\r
   {\r
-    alloc->Free(alloc, p->bufferBase);\r
-    p->bufferBase = 0;\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, ISzAlloc *alloc)\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
@@ -52,17 +37,16 @@ static int LzInWindow_Create(CMatchFinder *p, UInt32 keepSizeReserv, ISzAlloc *a
     p->blockSize = blockSize;\r
     return 1;\r
   }\r
-  if (p->bufferBase == 0 || p->blockSize != blockSize)\r
+  if (!p->bufferBase || p->blockSize != blockSize)\r
   {\r
     LzInWindow_Free(p, alloc);\r
     p->blockSize = blockSize;\r
-    p->bufferBase = (Byte *)alloc->Alloc(alloc, (size_t)blockSize);\r
+    p->bufferBase = (Byte *)ISzAlloc_Alloc(alloc, (size_t)blockSize);\r
   }\r
-  return (p->bufferBase != 0);\r
+  return (p->bufferBase != NULL);\r
 }\r
 \r
 Byte *MatchFinder_GetPointerToCurrentPos(CMatchFinder *p) { return p->buffer; }\r
-Byte MatchFinder_GetIndexByte(CMatchFinder *p, Int32 index) { return p->buffer[index]; }\r
 \r
 UInt32 MatchFinder_GetNumAvailableBytes(CMatchFinder *p) { return p->streamPos - p->pos; }\r
 \r
@@ -77,13 +61,29 @@ static void MatchFinder_ReadBlock(CMatchFinder *p)
 {\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
-    p->result = p->stream->Read(p->stream, dest, &size);\r
+\r
+    p->result = ISeqInStream_Read(p->stream, dest, &size);\r
     if (p->result != SZ_OK)\r
       return;\r
     if (size == 0)\r
@@ -100,13 +100,15 @@ static void MatchFinder_ReadBlock(CMatchFinder *p)
 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->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
@@ -131,8 +133,6 @@ static void MatchFinder_SetDefaultSettings(CMatchFinder *p)
   p->cutValue = 32;\r
   p->btMode = 1;\r
   p->numHashBytes = 4;\r
-  /* p->skipModeBits = 0; */\r
-  p->directInput = 0;\r
   p->bigHash = 0;\r
 }\r
 \r
@@ -141,62 +141,68 @@ static void MatchFinder_SetDefaultSettings(CMatchFinder *p)
 void MatchFinder_Construct(CMatchFinder *p)\r
 {\r
   UInt32 i;\r
-  p->bufferBase = 0;\r
+  p->bufferBase = NULL;\r
   p->directInput = 0;\r
-  p->hash = 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
-    int j;\r
+    unsigned j;\r
     for (j = 0; j < 8; j++)\r
-      r = (r >> 1) ^ (kCrcPoly & ~((r & 1) - 1));\r
+      r = (r >> 1) ^ (kCrcPoly & ((UInt32)0 - (r & 1)));\r
     p->crc[i] = r;\r
   }\r
 }\r
 \r
-static void MatchFinder_FreeThisClassMemory(CMatchFinder *p, ISzAlloc *alloc)\r
+static void MatchFinder_FreeThisClassMemory(CMatchFinder *p, ISzAllocPtr alloc)\r
 {\r
-  alloc->Free(alloc, p->hash);\r
-  p->hash = 0;\r
+  ISzAlloc_Free(alloc, p->hash);\r
+  p->hash = NULL;\r
 }\r
 \r
-void MatchFinder_Free(CMatchFinder *p, ISzAlloc *alloc)\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(UInt32 num, ISzAlloc *alloc)\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 0;\r
-  return (CLzRef *)alloc->Alloc(alloc, sizeInBytes);\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
-    ISzAlloc *alloc)\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)2 << 30))\r
-    sizeReserv = historySize >> 2;\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 /* >> p->skipModeBits */) + 1;\r
+    UInt32 newCyclicBufferSize = historySize + 1;\r
     UInt32 hs;\r
     p->matchMaxLen = matchMaxLen;\r
     {\r
@@ -205,13 +211,16 @@ int MatchFinder_Create(CMatchFinder *p, UInt32 historySize,
         hs = (1 << 16) - 1;\r
       else\r
       {\r
-        hs = historySize - 1;\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 >>= p->skipModeBits; */\r
         hs |= 0xFFFF; /* don't change it! It's required for Deflate */\r
         if (hs > (1 << 24))\r
         {\r
@@ -219,6 +228,7 @@ int MatchFinder_Create(CMatchFinder *p, UInt32 historySize,
             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
@@ -230,24 +240,32 @@ int MatchFinder_Create(CMatchFinder *p, UInt32 historySize,
     }\r
 \r
     {\r
-      UInt32 prevSize = p->hashSizeSum + p->numSons;\r
-      UInt32 newSize;\r
+      size_t newSize;\r
+      size_t numSons;\r
       p->historySize = historySize;\r
       p->hashSizeSum = hs;\r
       p->cyclicBufferSize = newCyclicBufferSize;\r
-      p->numSons = (p->btMode ? newCyclicBufferSize * 2 : newCyclicBufferSize);\r
-      newSize = p->hashSizeSum + p->numSons;\r
-      if (p->hash != 0 && prevSize == newSize)\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
-      if (p->hash != 0)\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
@@ -256,9 +274,11 @@ static void MatchFinder_SetLimits(CMatchFinder *p)
 {\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
@@ -266,8 +286,10 @@ static void MatchFinder_SetLimits(CMatchFinder *p)
   }\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
@@ -277,28 +299,59 @@ static void MatchFinder_SetLimits(CMatchFinder *p)
   p->posLimit = p->pos + limit;\r
 }\r
 \r
-void MatchFinder_Init(CMatchFinder *p)\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
-  UInt32 i;\r
-  for (i = 0; i < p->hashSizeSum; i++)\r
-    p->hash[i] = kEmptyHashValue;\r
   p->cyclicBufferPos = 0;\r
   p->buffer = p->bufferBase;\r
-  p->pos = p->streamPos = p->cyclicBufferSize;\r
+  p->pos =\r
+  p->streamPos = p->cyclicBufferSize;\r
   p->result = SZ_OK;\r
   p->streamEndWasReached = 0;\r
-  MatchFinder_ReadBlock(p);\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, UInt32 numItems)\r
+void MatchFinder_Normalize3(UInt32 subValue, CLzRef *items, size_t numItems)\r
 {\r
-  UInt32 i;\r
+  size_t i;\r
   for (i = 0; i < numItems; i++)\r
   {\r
     UInt32 value = items[i];\r
@@ -313,7 +366,7 @@ void MatchFinder_Normalize3(UInt32 subValue, CLzRef *items, UInt32 numItems)
 static void MatchFinder_Normalize(CMatchFinder *p)\r
 {\r
   UInt32 subValue = MatchFinder_GetSubValue(p);\r
-  MatchFinder_Normalize3(subValue, p->hash, p->hashSizeSum + p->numSons);\r
+  MatchFinder_Normalize3(subValue, p->hash, p->numRefs);\r
   MatchFinder_ReduceOffsets(p, subValue);\r
 }\r
 \r
@@ -474,7 +527,7 @@ static void SkipMatchesSpec(UInt32 lenLimit, UInt32 curMatch, UInt32 pos, const
 static void MatchFinder_MovePos(CMatchFinder *p) { MOVE_POS; }\r
 \r
 #define GET_MATCHES_HEADER2(minLen, ret_op) \\r
-  UInt32 lenLimit; UInt32 hashValue; const Byte *cur; UInt32 curMatch; \\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
@@ -490,13 +543,20 @@ static void MatchFinder_MovePos(CMatchFinder *p) { MOVE_POS; }
 #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[hashValue];\r
-  p->hash[hashValue] = p->pos;\r
+  curMatch = p->hash[hv];\r
+  p->hash[hv] = p->pos;\r
   offset = 0;\r
   GET_MATCHES_FOOTER(offset, 1)\r
 }\r
@@ -506,35 +566,38 @@ UInt32 Bt3Zip_MatchFinder_GetMatches(CMatchFinder *p, UInt32 *distances)
   UInt32 offset;\r
   GET_MATCHES_HEADER(3)\r
   HASH_ZIP_CALC;\r
-  curMatch = p->hash[hashValue];\r
-  p->hash[hashValue] = p->pos;\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 hash2Value, delta2, maxLen, offset;\r
+  UInt32 h2, d2, maxLen, offset, pos;\r
+  UInt32 *hash;\r
   GET_MATCHES_HEADER(3)\r
 \r
   HASH3_CALC;\r
 \r
-  delta2 = p->pos - p->hash[hash2Value];\r
-  curMatch = p->hash[kFix3HashSize + hashValue];\r
-  \r
-  p->hash[hash2Value] =\r
-  p->hash[kFix3HashSize + hashValue] = p->pos;\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
-  if (delta2 < p->cyclicBufferSize && *(cur - delta2) == *cur)\r
+\r
+  if (d2 < p->cyclicBufferSize && *(cur - d2) == *cur)\r
   {\r
-    for (; maxLen != lenLimit; maxLen++)\r
-      if (cur[(ptrdiff_t)maxLen - delta2] != cur[maxLen])\r
-        break;\r
+    UPDATE_maxLen\r
     distances[0] = maxLen;\r
-    distances[1] = delta2 - 1;\r
+    distances[1] = d2 - 1;\r
     offset = 2;\r
     if (maxLen == lenLimit)\r
     {\r
@@ -542,114 +605,287 @@ static UInt32 Bt3_MatchFinder_GetMatches(CMatchFinder *p, UInt32 *distances)
       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 hash2Value, hash3Value, delta2, delta3, maxLen, offset;\r
+  UInt32 h2, h3, d2, d3, maxLen, offset, pos;\r
+  UInt32 *hash;\r
   GET_MATCHES_HEADER(4)\r
 \r
   HASH4_CALC;\r
 \r
-  delta2 = p->pos - p->hash[                hash2Value];\r
-  delta3 = p->pos - p->hash[kFix3HashSize + hash3Value];\r
-  curMatch = p->hash[kFix4HashSize + hashValue];\r
-  \r
-  p->hash[                hash2Value] =\r
-  p->hash[kFix3HashSize + hash3Value] =\r
-  p->hash[kFix4HashSize + hashValue] = p->pos;\r
+  hash = p->hash;\r
+  pos = p->pos;\r
+\r
+  d2 = pos - hash[                h2];\r
+  d3 = pos - (hash + kFix3HashSize)[h3];\r
 \r
-  maxLen = 1;\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
-  if (delta2 < p->cyclicBufferSize && *(cur - delta2) == *cur)\r
+  \r
+  if (d2 < p->cyclicBufferSize && *(cur - d2) == *cur)\r
   {\r
     distances[0] = maxLen = 2;\r
-    distances[1] = delta2 - 1;\r
+    distances[1] = d2 - 1;\r
     offset = 2;\r
   }\r
-  if (delta2 != delta3 && delta3 < p->cyclicBufferSize && *(cur - delta3) == *cur)\r
+  \r
+  if (d2 != d3 && d3 < p->cyclicBufferSize && *(cur - d3) == *cur)\r
   {\r
     maxLen = 3;\r
-    distances[offset + 1] = delta3 - 1;\r
+    distances[(size_t)offset + 1] = d3 - 1;\r
     offset += 2;\r
-    delta2 = delta3;\r
+    d2 = d3;\r
   }\r
+  \r
   if (offset != 0)\r
   {\r
-    for (; maxLen != lenLimit; maxLen++)\r
-      if (cur[(ptrdiff_t)maxLen - delta2] != cur[maxLen])\r
-        break;\r
-    distances[offset - 2] = maxLen;\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 hash2Value, hash3Value, delta2, delta3, maxLen, offset;\r
+  UInt32 h2, h3, d2, d3, maxLen, offset, pos;\r
+  UInt32 *hash;\r
   GET_MATCHES_HEADER(4)\r
 \r
   HASH4_CALC;\r
 \r
-  delta2 = p->pos - p->hash[                hash2Value];\r
-  delta3 = p->pos - p->hash[kFix3HashSize + hash3Value];\r
-  curMatch = p->hash[kFix4HashSize + hashValue];\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
-  p->hash[                hash2Value] =\r
-  p->hash[kFix3HashSize + hash3Value] =\r
-  p->hash[kFix4HashSize + hashValue] = p->pos;\r
+  hash[                h2] = pos;\r
+  (hash + kFix3HashSize)[h3] = pos;\r
+  (hash + kFix4HashSize)[hv] = pos;\r
 \r
-  maxLen = 1;\r
+  maxLen = 0;\r
   offset = 0;\r
-  if (delta2 < p->cyclicBufferSize && *(cur - delta2) == *cur)\r
+\r
+  if (d2 < p->cyclicBufferSize && *(cur - d2) == *cur)\r
   {\r
     distances[0] = maxLen = 2;\r
-    distances[1] = delta2 - 1;\r
+    distances[1] = d2 - 1;\r
     offset = 2;\r
   }\r
-  if (delta2 != delta3 && delta3 < p->cyclicBufferSize && *(cur - delta3) == *cur)\r
+  \r
+  if (d2 != d3 && d3 < p->cyclicBufferSize && *(cur - d3) == *cur)\r
   {\r
     maxLen = 3;\r
-    distances[offset + 1] = delta3 - 1;\r
+    distances[(size_t)offset + 1] = d3 - 1;\r
     offset += 2;\r
-    delta2 = delta3;\r
+    d2 = d3;\r
   }\r
+  \r
   if (offset != 0)\r
   {\r
-    for (; maxLen != lenLimit; maxLen++)\r
-      if (cur[(ptrdiff_t)maxLen - delta2] != cur[maxLen])\r
-        break;\r
-    distances[offset - 2] = maxLen;\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
+      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[hashValue];\r
-  p->hash[hashValue] = p->pos;\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
+      distances, 2) - (distances));\r
   MOVE_POS_RET\r
 }\r
 \r
@@ -659,8 +895,8 @@ static void Bt2_MatchFinder_Skip(CMatchFinder *p, UInt32 num)
   {\r
     SKIP_HEADER(2)\r
     HASH2_CALC;\r
-    curMatch = p->hash[hashValue];\r
-    p->hash[hashValue] = p->pos;\r
+    curMatch = p->hash[hv];\r
+    p->hash[hv] = p->pos;\r
     SKIP_FOOTER\r
   }\r
   while (--num != 0);\r
@@ -672,8 +908,8 @@ void Bt3Zip_MatchFinder_Skip(CMatchFinder *p, UInt32 num)
   {\r
     SKIP_HEADER(3)\r
     HASH_ZIP_CALC;\r
-    curMatch = p->hash[hashValue];\r
-    p->hash[hashValue] = p->pos;\r
+    curMatch = p->hash[hv];\r
+    p->hash[hv] = p->pos;\r
     SKIP_FOOTER\r
   }\r
   while (--num != 0);\r
@@ -683,12 +919,14 @@ static void Bt3_MatchFinder_Skip(CMatchFinder *p, UInt32 num)
 {\r
   do\r
   {\r
-    UInt32 hash2Value;\r
+    UInt32 h2;\r
+    UInt32 *hash;\r
     SKIP_HEADER(3)\r
     HASH3_CALC;\r
-    curMatch = p->hash[kFix3HashSize + hashValue];\r
-    p->hash[hash2Value] =\r
-    p->hash[kFix3HashSize + hashValue] = p->pos;\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
@@ -698,43 +936,90 @@ static void Bt4_MatchFinder_Skip(CMatchFinder *p, UInt32 num)
 {\r
   do\r
   {\r
-    UInt32 hash2Value, hash3Value;\r
+    UInt32 h2, h3;\r
+    UInt32 *hash;\r
     SKIP_HEADER(4)\r
     HASH4_CALC;\r
-    curMatch = p->hash[kFix4HashSize + hashValue];\r
-    p->hash[                hash2Value] =\r
-    p->hash[kFix3HashSize + hash3Value] = p->pos;\r
-    p->hash[kFix4HashSize + hashValue] = p->pos;\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 hash2Value, hash3Value;\r
+    UInt32 h2, h3;\r
+    UInt32 *hash;\r
     SKIP_HEADER(4)\r
     HASH4_CALC;\r
-    curMatch = p->hash[kFix4HashSize + hashValue];\r
-    p->hash[                hash2Value] =\r
-    p->hash[kFix3HashSize + hash3Value] =\r
-    p->hash[kFix4HashSize + hashValue] = p->pos;\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[hashValue];\r
-    p->hash[hashValue] = p->pos;\r
+    curMatch = p->hash[hv];\r
+    p->hash[hv] = p->pos;\r
     p->son[p->cyclicBufferPos] = curMatch;\r
     MOVE_POS\r
   }\r
@@ -744,13 +1029,22 @@ void Hc3Zip_MatchFinder_Skip(CMatchFinder *p, UInt32 num)
 void MatchFinder_CreateVTable(CMatchFinder *p, IMatchFinder *vTable)\r
 {\r
   vTable->Init = (Mf_Init_Func)MatchFinder_Init;\r
-  vTable->GetIndexByte = (Mf_GetIndexByte_Func)MatchFinder_GetIndexByte;\r
   vTable->GetNumAvailableBytes = (Mf_GetNumAvailableBytes_Func)MatchFinder_GetNumAvailableBytes;\r
   vTable->GetPointerToCurrentPos = (Mf_GetPointerToCurrentPos_Func)MatchFinder_GetPointerToCurrentPos;\r
   if (!p->btMode)\r
   {\r
-    vTable->GetMatches = (Mf_GetMatches_Func)Hc4_MatchFinder_GetMatches;\r
-    vTable->Skip = (Mf_Skip_Func)Hc4_MatchFinder_Skip;\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
@@ -762,9 +1056,16 @@ void MatchFinder_CreateVTable(CMatchFinder *p, IMatchFinder *vTable)
     vTable->GetMatches = (Mf_GetMatches_Func)Bt3_MatchFinder_GetMatches;\r
     vTable->Skip = (Mf_Skip_Func)Bt3_MatchFinder_Skip;\r
   }\r
-  else\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