]> git.proxmox.com Git - mirror_edk2.git/blobdiff - MdeModulePkg/Library/LzmaCustomDecompressLib/Sdk/C/LzFind.c
MdeModulePkg Lzma: Update LZMA SDK version to 19.00
[mirror_edk2.git] / MdeModulePkg / Library / LzmaCustomDecompressLib / Sdk / C / LzFind.c
index a860e068e4b9df322b3a7e2fa5913c7ef15e20d3..670a9fab125de95aeb0c4224b23b5f20db164a85 100644 (file)
@@ -1,5 +1,5 @@
 /* LzFind.c -- Match finder for LZ algorithms\r
-2015-10-15 : Igor Pavlov : Public domain */\r
+2018-07-08 : Igor Pavlov : Public domain */\r
 \r
 #include "Precomp.h"\r
 \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
+    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
@@ -41,7 +41,7 @@ static int LzInWindow_Create(CMatchFinder *p, UInt32 keepSizeReserv, ISzAlloc *a
   {\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 != NULL);\r
 }\r
@@ -75,7 +75,7 @@ static void MatchFinder_ReadBlock(CMatchFinder *p)
       p->streamEndWasReached = 1;\r
     return;\r
   }\r
-  \r
+\r
   for (;;)\r
   {\r
     Byte *dest = p->buffer + (p->streamPos - p->pos);\r
@@ -83,7 +83,7 @@ static void MatchFinder_ReadBlock(CMatchFinder *p)
     if (size == 0)\r
       return;\r
 \r
-    p->result = p->stream->Read(p->stream, dest, &size);\r
+    p->result = ISeqInStream_Read(p->stream, dest, &size);\r
     if (p->result != SZ_OK)\r
       return;\r
     if (size == 0)\r
@@ -140,65 +140,66 @@ static void MatchFinder_SetDefaultSettings(CMatchFinder *p)
 \r
 void MatchFinder_Construct(CMatchFinder *p)\r
 {\r
-  UInt32 i;\r
+  unsigned 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
+    UInt32 r = (UInt32)i;\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
+  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(size_t 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 NULL;\r
-  return (CLzRef *)alloc->Alloc(alloc, sizeInBytes);\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
+\r
   if (historySize > kMaxHistorySize)\r
   {\r
     MatchFinder_Free(p, alloc);\r
     return 0;\r
   }\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
+\r
   sizeReserv += (keepAddBufferBefore + matchMaxLen + keepAddBufferAfter) / 2 + (1 << 19);\r
 \r
   p->keepSizeBefore = historySize + keepAddBufferBefore + 1;\r
   p->keepSizeAfter = matchMaxLen + keepAddBufferAfter;\r
-  \r
+\r
   /* we need one additional byte, since we use MoveBlock after pos++ and before dictionary using */\r
-  \r
+\r
   if (LzInWindow_Create(p, sizeReserv, alloc))\r
   {\r
     UInt32 newCyclicBufferSize = historySize + 1;\r
@@ -210,7 +211,11 @@ 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
@@ -240,7 +245,7 @@ int MatchFinder_Create(CMatchFinder *p, UInt32 historySize,
       p->historySize = historySize;\r
       p->hashSizeSum = hs;\r
       p->cyclicBufferSize = newCyclicBufferSize;\r
-      \r
+\r
       numSons = newCyclicBufferSize;\r
       if (p->btMode)\r
         numSons <<= 1;\r
@@ -248,11 +253,11 @@ int MatchFinder_Create(CMatchFinder *p, UInt32 historySize,
 \r
       if (p->hash && p->numRefs == newSize)\r
         return 1;\r
-      \r
+\r
       MatchFinder_FreeThisClassMemory(p, alloc);\r
       p->numRefs = newSize;\r
       p->hash = AllocRefs(newSize, alloc);\r
-      \r
+\r
       if (p->hash)\r
       {\r
         p->son = p->hash + p->hashSizeSum;\r
@@ -269,11 +274,11 @@ static void MatchFinder_SetLimits(CMatchFinder *p)
 {\r
   UInt32 limit = kMaxValForNormalize - p->pos;\r
   UInt32 limit2 = p->cyclicBufferSize - p->cyclicBufferPos;\r
-  \r
+\r
   if (limit2 < limit)\r
     limit = limit2;\r
   limit2 = p->streamPos - p->pos;\r
-  \r
+\r
   if (limit2 <= p->keepSizeAfter)\r
   {\r
     if (limit2 > 0)\r
@@ -281,10 +286,10 @@ static void MatchFinder_SetLimits(CMatchFinder *p)
   }\r
   else\r
     limit2 -= p->keepSizeAfter;\r
-  \r
+\r
   if (limit2 < limit)\r
     limit = limit2;\r
-  \r
+\r
   {\r
     UInt32 lenLimit = p->streamPos - p->pos;\r
     if (lenLimit > p->matchMaxLen)\r
@@ -294,31 +299,51 @@ static void MatchFinder_SetLimits(CMatchFinder *p)
   p->posLimit = p->pos + limit;\r
 }\r
 \r
-void MatchFinder_Init_2(CMatchFinder *p, int readData)\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
-  UInt32 *hash = p->hash;\r
-  UInt32 num = p->hashSizeSum;\r
-  for (i = 0; i < num; i++)\r
-    hash[i] = kEmptyHashValue;\r
-  \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
-  \r
+\r
   if (readData)\r
     MatchFinder_ReadBlock(p);\r
-  \r
+\r
   MatchFinder_SetLimits(p);\r
 }\r
 \r
+\r
 void MatchFinder_Init(CMatchFinder *p)\r
 {\r
-  MatchFinder_Init_2(p, True);\r
+  MatchFinder_Init_HighHash(p);\r
+  MatchFinder_Init_LowHash(p);\r
+  MatchFinder_Init_3(p, True);\r
 }\r
-  \r
+\r
+\r
 static UInt32 MatchFinder_GetSubValue(CMatchFinder *p)\r
 {\r
   return (p->pos - p->historySize - 1) & kNormalizeMask;\r
@@ -345,6 +370,8 @@ static void MatchFinder_Normalize(CMatchFinder *p)
   MatchFinder_ReduceOffsets(p, subValue);\r
 }\r
 \r
+\r
+MY_NO_INLINE\r
 static void MatchFinder_CheckLimits(CMatchFinder *p)\r
 {\r
   if (p->pos == kMaxValForNormalize)\r
@@ -356,10 +383,16 @@ static void MatchFinder_CheckLimits(CMatchFinder *p)
   MatchFinder_SetLimits(p);\r
 }\r
 \r
-static UInt32 * Hc_GetMatchesSpec(UInt32 lenLimit, UInt32 curMatch, UInt32 pos, const Byte *cur, CLzRef *son,\r
+\r
+/*\r
+  (lenLimit > maxLen)\r
+*/\r
+MY_FORCE_INLINE\r
+static UInt32 * Hc_GetMatchesSpec(unsigned lenLimit, UInt32 curMatch, UInt32 pos, const Byte *cur, CLzRef *son,\r
     UInt32 _cyclicBufferPos, UInt32 _cyclicBufferSize, UInt32 cutValue,\r
-    UInt32 *distances, UInt32 maxLen)\r
+    UInt32 *distances, unsigned maxLen)\r
 {\r
+  /*\r
   son[_cyclicBufferPos] = curMatch;\r
   for (;;)\r
   {\r
@@ -377,7 +410,8 @@ static UInt32 * Hc_GetMatchesSpec(UInt32 lenLimit, UInt32 curMatch, UInt32 pos,
             break;\r
         if (maxLen < len)\r
         {\r
-          *distances++ = maxLen = len;\r
+          maxLen = len;\r
+          *distances++ = len;\r
           *distances++ = delta - 1;\r
           if (len == lenLimit)\r
             return distances;\r
@@ -385,15 +419,58 @@ static UInt32 * Hc_GetMatchesSpec(UInt32 lenLimit, UInt32 curMatch, UInt32 pos,
       }\r
     }\r
   }\r
+  */\r
+\r
+  const Byte *lim = cur + lenLimit;\r
+  son[_cyclicBufferPos] = curMatch;\r
+  do\r
+  {\r
+    UInt32 delta = pos - curMatch;\r
+    if (delta >= _cyclicBufferSize)\r
+      break;\r
+    {\r
+      ptrdiff_t diff;\r
+      curMatch = son[_cyclicBufferPos - delta + ((delta > _cyclicBufferPos) ? _cyclicBufferSize : 0)];\r
+      diff = (ptrdiff_t)0 - delta;\r
+      if (cur[maxLen] == cur[maxLen + diff])\r
+      {\r
+        const Byte *c = cur;\r
+        while (*c == c[diff])\r
+        {\r
+          if (++c == lim)\r
+          {\r
+            distances[0] = (UInt32)(lim - cur);\r
+            distances[1] = delta - 1;\r
+            return distances + 2;\r
+          }\r
+        }\r
+        {\r
+          unsigned len = (unsigned)(c - cur);\r
+          if (maxLen < len)\r
+          {\r
+            maxLen = len;\r
+            distances[0] = (UInt32)len;\r
+            distances[1] = delta - 1;\r
+            distances += 2;\r
+          }\r
+        }\r
+      }\r
+    }\r
+  }\r
+  while (--cutValue);\r
+\r
+  return distances;\r
 }\r
 \r
+\r
+MY_FORCE_INLINE\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
+  CLzRef *ptr0 = son + ((size_t)_cyclicBufferPos << 1) + 1;\r
+  CLzRef *ptr1 = son + ((size_t)_cyclicBufferPos << 1);\r
+  unsigned len0 = 0, len1 = 0;\r
   for (;;)\r
   {\r
     UInt32 delta = pos - curMatch;\r
@@ -403,9 +480,10 @@ UInt32 * GetMatchesSpec1(UInt32 lenLimit, UInt32 curMatch, UInt32 pos, const Byt
       return distances;\r
     }\r
     {\r
-      CLzRef *pair = son + ((_cyclicBufferPos - delta + ((delta > _cyclicBufferPos) ? _cyclicBufferSize : 0)) << 1);\r
+      CLzRef *pair = son + ((size_t)(_cyclicBufferPos - delta + ((delta > _cyclicBufferPos) ? _cyclicBufferSize : 0)) << 1);\r
       const Byte *pb = cur - delta;\r
-      UInt32 len = (len0 < len1 ? len0 : len1);\r
+      unsigned len = (len0 < len1 ? len0 : len1);\r
+      UInt32 pair0 = pair[0];\r
       if (pb[len] == cur[len])\r
       {\r
         if (++len != lenLimit && pb[len] == cur[len])\r
@@ -414,11 +492,12 @@ UInt32 * GetMatchesSpec1(UInt32 lenLimit, UInt32 curMatch, UInt32 pos, const Byt
               break;\r
         if (maxLen < len)\r
         {\r
-          *distances++ = maxLen = len;\r
+          maxLen = (UInt32)len;\r
+          *distances++ = (UInt32)len;\r
           *distances++ = delta - 1;\r
           if (len == lenLimit)\r
           {\r
-            *ptr1 = pair[0];\r
+            *ptr1 = pair0;\r
             *ptr0 = pair[1];\r
             return distances;\r
           }\r
@@ -445,9 +524,9 @@ UInt32 * GetMatchesSpec1(UInt32 lenLimit, UInt32 curMatch, UInt32 pos, const Byt
 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
+  CLzRef *ptr0 = son + ((size_t)_cyclicBufferPos << 1) + 1;\r
+  CLzRef *ptr1 = son + ((size_t)_cyclicBufferPos << 1);\r
+  unsigned len0 = 0, len1 = 0;\r
   for (;;)\r
   {\r
     UInt32 delta = pos - curMatch;\r
@@ -457,9 +536,9 @@ static void SkipMatchesSpec(UInt32 lenLimit, UInt32 curMatch, UInt32 pos, const
       return;\r
     }\r
     {\r
-      CLzRef *pair = son + ((_cyclicBufferPos - delta + ((delta > _cyclicBufferPos) ? _cyclicBufferSize : 0)) << 1);\r
+      CLzRef *pair = son + ((size_t)(_cyclicBufferPos - delta + ((delta > _cyclicBufferPos) ? _cyclicBufferSize : 0)) << 1);\r
       const Byte *pb = cur - delta;\r
-      UInt32 len = (len0 < len1 ? len0 : len1);\r
+      unsigned len = (len0 < len1 ? len0 : len1);\r
       if (pb[len] == cur[len])\r
       {\r
         while (++len != lenLimit)\r
@@ -497,13 +576,13 @@ static void SkipMatchesSpec(UInt32 lenLimit, UInt32 curMatch, UInt32 pos, const
   p->buffer++; \\r
   if (++p->pos == p->posLimit) MatchFinder_CheckLimits(p);\r
 \r
-#define MOVE_POS_RET MOVE_POS return offset;\r
+#define MOVE_POS_RET MOVE_POS return (UInt32)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
+  unsigned lenLimit; UInt32 hv; const Byte *cur; UInt32 curMatch; \\r
+  lenLimit = (unsigned)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
@@ -512,22 +591,22 @@ static void MatchFinder_MovePos(CMatchFinder *p) { MOVE_POS; }
 #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
+  offset = (unsigned)(GetMatchesSpec1((UInt32)lenLimit, curMatch, MF_PARAMS(p), \\r
+  distances + offset, (UInt32)maxLen) - distances); MOVE_POS_RET;\r
 \r
 #define SKIP_FOOTER \\r
-  SkipMatchesSpec(lenLimit, curMatch, MF_PARAMS(p)); MOVE_POS;\r
+  SkipMatchesSpec((UInt32)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
+    maxLen = (unsigned)(c - cur); }\r
 \r
 static UInt32 Bt2_MatchFinder_GetMatches(CMatchFinder *p, UInt32 *distances)\r
 {\r
-  UInt32 offset;\r
+  unsigned offset;\r
   GET_MATCHES_HEADER(2)\r
   HASH2_CALC;\r
   curMatch = p->hash[hv];\r
@@ -538,7 +617,7 @@ static UInt32 Bt2_MatchFinder_GetMatches(CMatchFinder *p, UInt32 *distances)
 \r
 UInt32 Bt3Zip_MatchFinder_GetMatches(CMatchFinder *p, UInt32 *distances)\r
 {\r
-  UInt32 offset;\r
+  unsigned offset;\r
   GET_MATCHES_HEADER(3)\r
   HASH_ZIP_CALC;\r
   curMatch = p->hash[hv];\r
@@ -549,7 +628,8 @@ UInt32 Bt3Zip_MatchFinder_GetMatches(CMatchFinder *p, UInt32 *distances)
 \r
 static UInt32 Bt3_MatchFinder_GetMatches(CMatchFinder *p, UInt32 *distances)\r
 {\r
-  UInt32 h2, d2, maxLen, offset, pos;\r
+  UInt32 h2, d2, pos;\r
+  unsigned maxLen, offset;\r
   UInt32 *hash;\r
   GET_MATCHES_HEADER(3)\r
 \r
@@ -560,10 +640,10 @@ static UInt32 Bt3_MatchFinder_GetMatches(CMatchFinder *p, UInt32 *distances)
 \r
   d2 = pos - hash[h2];\r
 \r
-  curMatch = hash[kFix3HashSize + hv];\r
-  \r
+  curMatch = (hash + kFix3HashSize)[hv];\r
+\r
   hash[h2] = pos;\r
-  hash[kFix3HashSize + hv] = pos;\r
+  (hash + kFix3HashSize)[hv] = pos;\r
 \r
   maxLen = 2;\r
   offset = 0;\r
@@ -571,22 +651,23 @@ static UInt32 Bt3_MatchFinder_GetMatches(CMatchFinder *p, UInt32 *distances)
   if (d2 < p->cyclicBufferSize && *(cur - d2) == *cur)\r
   {\r
     UPDATE_maxLen\r
-    distances[0] = maxLen;\r
+    distances[0] = (UInt32)maxLen;\r
     distances[1] = d2 - 1;\r
     offset = 2;\r
     if (maxLen == lenLimit)\r
     {\r
-      SkipMatchesSpec(lenLimit, curMatch, MF_PARAMS(p));\r
+      SkipMatchesSpec((UInt32)lenLimit, curMatch, MF_PARAMS(p));\r
       MOVE_POS_RET;\r
     }\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 h2, h3, d2, d3, pos;\r
+  unsigned maxLen, offset;\r
   UInt32 *hash;\r
   GET_MATCHES_HEADER(4)\r
 \r
@@ -596,46 +677,47 @@ static UInt32 Bt4_MatchFinder_GetMatches(CMatchFinder *p, UInt32 *distances)
   pos = p->pos;\r
 \r
   d2 = pos - hash[                h2];\r
-  d3 = pos - hash[kFix3HashSize + h3];\r
+  d3 = pos - (hash + kFix3HashSize)[h3];\r
 \r
-  curMatch = hash[kFix4HashSize + hv];\r
+  curMatch = (hash + kFix4HashSize)[hv];\r
 \r
   hash[                h2] = pos;\r
-  hash[kFix3HashSize + h3] = pos;\r
-  hash[kFix4HashSize + hv] = pos;\r
+  (hash + kFix3HashSize)[h3] = pos;\r
+  (hash + kFix4HashSize)[hv] = pos;\r
 \r
   maxLen = 0;\r
   offset = 0;\r
-  \r
+\r
   if (d2 < p->cyclicBufferSize && *(cur - d2) == *cur)\r
   {\r
-    distances[0] = maxLen = 2;\r
+    maxLen = 2;\r
+    distances[0] = 2;\r
     distances[1] = d2 - 1;\r
     offset = 2;\r
   }\r
-  \r
+\r
   if (d2 != d3 && d3 < p->cyclicBufferSize && *(cur - d3) == *cur)\r
   {\r
     maxLen = 3;\r
-    distances[offset + 1] = d3 - 1;\r
+    distances[(size_t)offset + 1] = d3 - 1;\r
     offset += 2;\r
     d2 = d3;\r
   }\r
-  \r
+\r
   if (offset != 0)\r
   {\r
     UPDATE_maxLen\r
-    distances[offset - 2] = maxLen;\r
+    distances[(size_t)offset - 2] = (UInt32)maxLen;\r
     if (maxLen == lenLimit)\r
     {\r
-      SkipMatchesSpec(lenLimit, curMatch, MF_PARAMS(p));\r
+      SkipMatchesSpec((UInt32)lenLimit, curMatch, MF_PARAMS(p));\r
       MOVE_POS_RET;\r
     }\r
   }\r
-  \r
+\r
   if (maxLen < 3)\r
     maxLen = 3;\r
-  \r
+\r
   GET_MATCHES_FOOTER(offset, maxLen)\r
 }\r
 \r
@@ -652,15 +734,15 @@ static UInt32 Bt5_MatchFinder_GetMatches(CMatchFinder *p, UInt32 *distances)
   pos = p->pos;\r
 \r
   d2 = pos - hash[                h2];\r
-  d3 = pos - hash[kFix3HashSize + h3];\r
-  d4 = pos - hash[kFix4HashSize + h4];\r
+  d3 = pos - (hash + kFix3HashSize)[h3];\r
+  d4 = pos - (hash + kFix4HashSize)[h4];\r
 \r
-  curMatch = hash[kFix5HashSize + hv];\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
+  (hash + kFix3HashSize)[h3] = pos;\r
+  (hash + kFix4HashSize)[h4] = pos;\r
+  (hash + kFix5HashSize)[hv] = pos;\r
 \r
   maxLen = 0;\r
   offset = 0;\r
@@ -687,21 +769,21 @@ static UInt32 Bt5_MatchFinder_GetMatches(CMatchFinder *p, UInt32 *distances)
     offset = 2;\r
     d2 = d3;\r
   }\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[offset + 1] = d4 - 1;\r
+    distances[(size_t)offset + 1] = d4 - 1;\r
     offset += 2;\r
     d2 = d4;\r
   }\r
-  \r
+\r
   if (offset != 0)\r
   {\r
     UPDATE_maxLen\r
-    distances[offset - 2] = maxLen;\r
+    distances[(size_t)offset - 2] = maxLen;\r
     if (maxLen == lenLimit)\r
     {\r
       SkipMatchesSpec(lenLimit, curMatch, MF_PARAMS(p));\r
@@ -711,14 +793,15 @@ static UInt32 Bt5_MatchFinder_GetMatches(CMatchFinder *p, UInt32 *distances)
 \r
   if (maxLen < 4)\r
     maxLen = 4;\r
-  \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 h2, h3, d2, d3, pos;\r
+  unsigned maxLen, offset;\r
   UInt32 *hash;\r
   GET_MATCHES_HEADER(4)\r
 \r
@@ -726,49 +809,50 @@ static UInt32 Hc4_MatchFinder_GetMatches(CMatchFinder *p, UInt32 *distances)
 \r
   hash = p->hash;\r
   pos = p->pos;\r
-  \r
+\r
   d2 = pos - hash[                h2];\r
-  d3 = pos - hash[kFix3HashSize + h3];\r
-  \r
-  curMatch = hash[kFix4HashSize + hv];\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
+  (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
+    maxLen = 2;\r
+    distances[0] = 2;\r
     distances[1] = d2 - 1;\r
     offset = 2;\r
   }\r
-  \r
+\r
   if (d2 != d3 && d3 < p->cyclicBufferSize && *(cur - d3) == *cur)\r
   {\r
     maxLen = 3;\r
-    distances[offset + 1] = d3 - 1;\r
+    distances[(size_t)offset + 1] = d3 - 1;\r
     offset += 2;\r
     d2 = d3;\r
   }\r
-  \r
+\r
   if (offset != 0)\r
   {\r
     UPDATE_maxLen\r
-    distances[offset - 2] = maxLen;\r
+    distances[(size_t)offset - 2] = (UInt32)maxLen;\r
     if (maxLen == lenLimit)\r
     {\r
       p->son[p->cyclicBufferPos] = curMatch;\r
       MOVE_POS_RET;\r
     }\r
   }\r
-  \r
+\r
   if (maxLen < 3)\r
     maxLen = 3;\r
 \r
-  offset = (UInt32)(Hc_GetMatchesSpec(lenLimit, curMatch, MF_PARAMS(p),\r
+  offset = (unsigned)(Hc_GetMatchesSpec(lenLimit, curMatch, MF_PARAMS(p),\r
       distances + offset, maxLen) - (distances));\r
   MOVE_POS_RET\r
 }\r
@@ -784,17 +868,17 @@ static UInt32 Hc5_MatchFinder_GetMatches(CMatchFinder *p, UInt32 *distances)
 \r
   hash = p->hash;\r
   pos = p->pos;\r
-  \r
+\r
   d2 = pos - hash[                h2];\r
-  d3 = pos - hash[kFix3HashSize + h3];\r
-  d4 = pos - hash[kFix4HashSize + h4];\r
+  d3 = pos - (hash + kFix3HashSize)[h3];\r
+  d4 = pos - (hash + kFix4HashSize)[h4];\r
 \r
-  curMatch = hash[kFix5HashSize + hv];\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
+  (hash + kFix3HashSize)[h3] = pos;\r
+  (hash + kFix4HashSize)[h4] = pos;\r
+  (hash + kFix5HashSize)[hv] = pos;\r
 \r
   maxLen = 0;\r
   offset = 0;\r
@@ -821,28 +905,28 @@ static UInt32 Hc5_MatchFinder_GetMatches(CMatchFinder *p, UInt32 *distances)
     offset = 2;\r
     d2 = d3;\r
   }\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[offset + 1] = d4 - 1;\r
+    distances[(size_t)offset + 1] = d4 - 1;\r
     offset += 2;\r
     d2 = d4;\r
   }\r
-  \r
+\r
   if (offset != 0)\r
   {\r
     UPDATE_maxLen\r
-    distances[offset - 2] = 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
+\r
   if (maxLen < 4)\r
     maxLen = 4;\r
 \r
@@ -854,12 +938,12 @@ static UInt32 Hc5_MatchFinder_GetMatches(CMatchFinder *p, UInt32 *distances)
 \r
 UInt32 Hc3Zip_MatchFinder_GetMatches(CMatchFinder *p, UInt32 *distances)\r
 {\r
-  UInt32 offset;\r
+  unsigned 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
+  offset = (unsigned)(Hc_GetMatchesSpec(lenLimit, curMatch, MF_PARAMS(p),\r
       distances, 2) - (distances));\r
   MOVE_POS_RET\r
 }\r
@@ -899,9 +983,9 @@ static void Bt3_MatchFinder_Skip(CMatchFinder *p, UInt32 num)
     SKIP_HEADER(3)\r
     HASH3_CALC;\r
     hash = p->hash;\r
-    curMatch = hash[kFix3HashSize + hv];\r
+    curMatch = (hash + kFix3HashSize)[hv];\r
     hash[h2] =\r
-    hash[kFix3HashSize + hv] = p->pos;\r
+    (hash + kFix3HashSize)[hv] = p->pos;\r
     SKIP_FOOTER\r
   }\r
   while (--num != 0);\r
@@ -916,10 +1000,10 @@ static void Bt4_MatchFinder_Skip(CMatchFinder *p, UInt32 num)
     SKIP_HEADER(4)\r
     HASH4_CALC;\r
     hash = p->hash;\r
-    curMatch = hash[kFix4HashSize + hv];\r
+    curMatch = (hash + kFix4HashSize)[hv];\r
     hash[                h2] =\r
-    hash[kFix3HashSize + h3] =\r
-    hash[kFix4HashSize + hv] = p->pos;\r
+    (hash + kFix3HashSize)[h3] =\r
+    (hash + kFix4HashSize)[hv] = p->pos;\r
     SKIP_FOOTER\r
   }\r
   while (--num != 0);\r
@@ -935,11 +1019,11 @@ static void Bt5_MatchFinder_Skip(CMatchFinder *p, UInt32 num)
     SKIP_HEADER(5)\r
     HASH5_CALC;\r
     hash = p->hash;\r
-    curMatch = hash[kFix5HashSize + hv];\r
+    curMatch = (hash + kFix5HashSize)[hv];\r
     hash[                h2] =\r
-    hash[kFix3HashSize + h3] =\r
-    hash[kFix4HashSize + h4] =\r
-    hash[kFix5HashSize + hv] = p->pos;\r
+    (hash + kFix3HashSize)[h3] =\r
+    (hash + kFix4HashSize)[h4] =\r
+    (hash + kFix5HashSize)[hv] = p->pos;\r
     SKIP_FOOTER\r
   }\r
   while (--num != 0);\r
@@ -955,10 +1039,10 @@ static void Hc4_MatchFinder_Skip(CMatchFinder *p, UInt32 num)
     SKIP_HEADER(4)\r
     HASH4_CALC;\r
     hash = p->hash;\r
-    curMatch = hash[kFix4HashSize + hv];\r
+    curMatch = (hash + kFix4HashSize)[hv];\r
     hash[                h2] =\r
-    hash[kFix3HashSize + h3] =\r
-    hash[kFix4HashSize + hv] = p->pos;\r
+    (hash + kFix3HashSize)[h3] =\r
+    (hash + kFix4HashSize)[hv] = p->pos;\r
     p->son[p->cyclicBufferPos] = curMatch;\r
     MOVE_POS\r
   }\r
@@ -975,11 +1059,11 @@ static void Hc5_MatchFinder_Skip(CMatchFinder *p, UInt32 num)
     SKIP_HEADER(5)\r
     HASH5_CALC;\r
     hash = p->hash;\r
-    curMatch = p->hash[kFix5HashSize + hv];\r
+    curMatch = hash + kFix5HashSize)[hv];\r
     hash[                h2] =\r
-    hash[kFix3HashSize + h3] =\r
-    hash[kFix4HashSize + h4] =\r
-    hash[kFix5HashSize + hv] = p->pos;\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