]> 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 8765cbebfc9b8e6436fb491b671969f8425b17b4..670a9fab125de95aeb0c4224b23b5f20db164a85 100644 (file)
@@ -1,5 +1,5 @@
 /* LzFind.c -- Match finder for LZ algorithms\r
-2017-06-10 : Igor Pavlov : Public domain */\r
+2018-07-08 : Igor Pavlov : Public domain */\r
 \r
 #include "Precomp.h"\r
 \r
@@ -140,7 +140,7 @@ 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
@@ -149,7 +149,7 @@ void MatchFinder_Construct(CMatchFinder *p)
 \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 & ((UInt32)0 - (r & 1)));\r
@@ -370,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
@@ -381,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
@@ -402,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
@@ -410,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
@@ -428,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
@@ -439,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
@@ -470,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
@@ -482,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
@@ -522,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
@@ -537,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
@@ -563,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
@@ -574,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
@@ -596,12 +651,12 @@ 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
@@ -611,7 +666,8 @@ static UInt32 Bt3_MatchFinder_GetMatches(CMatchFinder *p, UInt32 *distances)
 \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
@@ -634,7 +690,8 @@ static UInt32 Bt4_MatchFinder_GetMatches(CMatchFinder *p, UInt32 *distances)
 \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
@@ -650,10 +707,10 @@ static UInt32 Bt4_MatchFinder_GetMatches(CMatchFinder *p, UInt32 *distances)
   if (offset != 0)\r
   {\r
     UPDATE_maxLen\r
-    distances[(size_t)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
@@ -743,7 +800,8 @@ static UInt32 Bt5_MatchFinder_GetMatches(CMatchFinder *p, UInt32 *distances)
 \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
@@ -766,7 +824,8 @@ static UInt32 Hc4_MatchFinder_GetMatches(CMatchFinder *p, UInt32 *distances)
 \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
@@ -782,7 +841,7 @@ static UInt32 Hc4_MatchFinder_GetMatches(CMatchFinder *p, UInt32 *distances)
   if (offset != 0)\r
   {\r
     UPDATE_maxLen\r
-    distances[(size_t)offset - 2] = maxLen;\r
+    distances[(size_t)offset - 2] = (UInt32)maxLen;\r
     if (maxLen == lenLimit)\r
     {\r
       p->son[p->cyclicBufferPos] = curMatch;\r
@@ -793,7 +852,7 @@ static UInt32 Hc4_MatchFinder_GetMatches(CMatchFinder *p, UInt32 *distances)
   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
@@ -879,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