]> git.proxmox.com Git - mirror_edk2.git/blobdiff - BaseTools/Source/C/LzmaCompress/Sdk/C/LzmaEnc.c
BaseTools LzmaCompress: Update LZMA to new 16.04 version
[mirror_edk2.git] / BaseTools / Source / C / LzmaCompress / Sdk / C / LzmaEnc.c
index 5f70ebdef08eb65d0974a14c1d8d7bbbc8808d48..462ca675652439ddb2dd504b600f62178ae52ea5 100644 (file)
@@ -1,18 +1,7 @@
-/** @file\r
-  Based on LZMA SDK 4.65:\r
-    LzmaEnc.c -- LZMA Encoder\r
-    2009-02-02 : Igor Pavlov : Public domain\r
+/* LzmaEnc.c -- LZMA Encoder\r
+2016-05-16 : Igor Pavlov : Public domain */\r
 \r
-  Copyright (c) 2011 - 2016, Intel Corporation. All rights reserved.<BR>\r
-  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
 #include <string.h>\r
 \r
 #include "LzmaEnc.h"\r
 \r
 #include "LzFind.h"\r
-#ifdef COMPRESS_MF_MT\r
+#ifndef _7ZIP_ST\r
 #include "LzFindMt.h"\r
 #endif\r
 \r
 #ifdef SHOW_STAT\r
-static int ttt = 0;\r
+static unsigned g_STAT_OFFSET = 0;\r
 #endif\r
 \r
+#define kMaxHistorySize ((UInt32)3 << 29)\r
+/* #define kMaxHistorySize ((UInt32)7 << 29) */\r
+\r
 #define kBlockSizeMax ((1 << LZMA_NUM_BLOCK_SIZE_BITS) - 1)\r
 \r
 #define kBlockSize (9 << 10)\r
@@ -59,6 +51,7 @@ void LzmaEncProps_Init(CLzmaEncProps *p)
 {\r
   p->level = 5;\r
   p->dictSize = p->mc = 0;\r
+  p->reduceSize = (UInt64)(Int64)-1;\r
   p->lc = p->lp = p->pb = p->algo = p->fb = p->btMode = p->numHashBytes = p->numThreads = -1;\r
   p->writeEndMark = 0;\r
 }\r
@@ -68,18 +61,31 @@ void LzmaEncProps_Normalize(CLzmaEncProps *p)
   int level = p->level;\r
   if (level < 0) level = 5;\r
   p->level = level;\r
+  \r
   if (p->dictSize == 0) p->dictSize = (level <= 5 ? (1 << (level * 2 + 14)) : (level == 6 ? (1 << 25) : (1 << 26)));\r
+  if (p->dictSize > p->reduceSize)\r
+  {\r
+    unsigned i;\r
+    for (i = 11; i <= 30; i++)\r
+    {\r
+      if ((UInt32)p->reduceSize <= ((UInt32)2 << i)) { p->dictSize = ((UInt32)2 << i); break; }\r
+      if ((UInt32)p->reduceSize <= ((UInt32)3 << i)) { p->dictSize = ((UInt32)3 << i); break; }\r
+    }\r
+  }\r
+\r
   if (p->lc < 0) p->lc = 3;\r
   if (p->lp < 0) p->lp = 0;\r
   if (p->pb < 0) p->pb = 2;\r
+\r
   if (p->algo < 0) p->algo = (level < 5 ? 0 : 1);\r
   if (p->fb < 0) p->fb = (level < 7 ? 32 : 64);\r
   if (p->btMode < 0) p->btMode = (p->algo == 0 ? 0 : 1);\r
   if (p->numHashBytes < 0) p->numHashBytes = 4;\r
-  if (p->mc == 0)  p->mc = (16 + (p->fb >> 1)) >> (p->btMode ? 0 : 1);\r
+  if (p->mc == 0) p->mc = (16 + (p->fb >> 1)) >> (p->btMode ? 0 : 1);\r
+  \r
   if (p->numThreads < 0)\r
     p->numThreads =\r
-      #ifdef COMPRESS_MF_MT\r
+      #ifndef _7ZIP_ST\r
       ((p->btMode && p->algo) ? 2 : 1);\r
       #else\r
       1;\r
@@ -93,17 +99,18 @@ UInt32 LzmaEncProps_GetDictSize(const CLzmaEncProps *props2)
   return props.dictSize;\r
 }\r
 \r
+#if (_MSC_VER >= 1400)\r
+/* BSR code is fast for some new CPUs */\r
 /* #define LZMA_LOG_BSR */\r
-/* Define it for Intel's CPU */\r
-\r
+#endif\r
 \r
 #ifdef LZMA_LOG_BSR\r
 \r
-#define kDicLogSizeMaxCompress 30\r
+#define kDicLogSizeMaxCompress 32\r
 \r
-#define BSR2_RET(pos, res) { unsigned long i; _BitScanReverse(&i, (pos)); res = (i + i) + ((pos >> (i - 1)) & 1); }\r
+#define BSR2_RET(pos, res) { unsigned long zz; _BitScanReverse(&zz, (pos)); res = (zz + zz) + ((pos >> (zz - 1)) & 1); }\r
 \r
-UInt32 GetPosSlot1(UInt32 pos)\r
+static UInt32 GetPosSlot1(UInt32 pos)\r
 {\r
   UInt32 res;\r
   BSR2_RET(pos, res);\r
@@ -114,27 +121,44 @@ UInt32 GetPosSlot1(UInt32 pos)
 \r
 #else\r
 \r
-#define kNumLogBits (9 + (int)sizeof(size_t) / 2)\r
+#define kNumLogBits (9 + sizeof(size_t) / 2)\r
+/* #define kNumLogBits (11 + sizeof(size_t) / 8 * 3) */\r
+\r
 #define kDicLogSizeMaxCompress ((kNumLogBits - 1) * 2 + 7)\r
 \r
-void LzmaEnc_FastPosInit(Byte *g_FastPos)\r
+static void LzmaEnc_FastPosInit(Byte *g_FastPos)\r
 {\r
-  int c = 2, slotFast;\r
+  unsigned slot;\r
   g_FastPos[0] = 0;\r
   g_FastPos[1] = 1;\r
+  g_FastPos += 2;\r
   \r
-  for (slotFast = 2; slotFast < kNumLogBits * 2; slotFast++)\r
+  for (slot = 2; slot < kNumLogBits * 2; slot++)\r
   {\r
-    UInt32 k = (1 << ((slotFast >> 1) - 1));\r
-    UInt32 j;\r
-    for (j = 0; j < k; j++, c++)\r
-      g_FastPos[c] = (Byte)slotFast;\r
+    size_t k = ((size_t)1 << ((slot >> 1) - 1));\r
+    size_t j;\r
+    for (j = 0; j < k; j++)\r
+      g_FastPos[j] = (Byte)slot;\r
+    g_FastPos += k;\r
   }\r
 }\r
 \r
-#define BSR2_RET(pos, res) { UInt32 ij = 6 + ((kNumLogBits - 1) & \\r
+/* we can use ((limit - pos) >> 31) only if (pos < ((UInt32)1 << 31)) */\r
+/*\r
+#define BSR2_RET(pos, res) { UInt32 zz = 6 + ((kNumLogBits - 1) & \\r
   (0 - (((((UInt32)1 << (kNumLogBits + 6)) - 1) - pos) >> 31))); \\r
-  res = p->g_FastPos[pos >> ij] + (ij * 2); }\r
+  res = p->g_FastPos[pos >> zz] + (zz * 2); }\r
+*/\r
+\r
+/*\r
+#define BSR2_RET(pos, res) { UInt32 zz = 6 + ((kNumLogBits - 1) & \\r
+  (0 - (((((UInt32)1 << (kNumLogBits)) - 1) - (pos >> 6)) >> 31))); \\r
+  res = p->g_FastPos[pos >> zz] + (zz * 2); }\r
+*/\r
+\r
+#define BSR2_RET(pos, res) { UInt32 zz = (pos < (1 << (kNumLogBits + 6))) ? 6 : 6 + kNumLogBits - 1; \\r
+  res = p->g_FastPos[pos >> zz] + (zz * 2); }\r
+\r
 /*\r
 #define BSR2_RET(pos, res) { res = (pos < (1 << (kNumLogBits + 6))) ? \\r
   p->g_FastPos[pos >> 6] + 12 : \\r
@@ -152,7 +176,7 @@ void LzmaEnc_FastPosInit(Byte *g_FastPos)
 \r
 typedef unsigned CState;\r
 \r
-typedef struct _COptimal\r
+typedef struct\r
 {\r
   UInt32 price;\r
 \r
@@ -185,7 +209,7 @@ typedef struct _COptimal
 #define kEndPosModelIndex 14\r
 #define kNumPosModels (kEndPosModelIndex - kStartPosModelIndex)\r
 \r
-#define kNumFullDistances (1 << (kEndPosModelIndex / 2))\r
+#define kNumFullDistances (1 << (kEndPosModelIndex >> 1))\r
 \r
 #ifdef _LZMA_PROB32\r
 #define CLzmaProb UInt32\r
@@ -214,6 +238,7 @@ typedef struct _COptimal
 \r
 #define kNumStates 12\r
 \r
+\r
 typedef struct\r
 {\r
   CLzmaProb choice;\r
@@ -223,15 +248,17 @@ typedef struct
   CLzmaProb high[kLenNumHighSymbols];\r
 } CLenEnc;\r
 \r
+\r
 typedef struct\r
 {\r
   CLenEnc p;\r
-  UInt32 prices[LZMA_NUM_PB_STATES_MAX][kLenNumSymbolsTotal];\r
   UInt32 tableSize;\r
+  UInt32 prices[LZMA_NUM_PB_STATES_MAX][kLenNumSymbolsTotal];\r
   UInt32 counters[LZMA_NUM_PB_STATES_MAX];\r
 } CLenPriceEnc;\r
 \r
-typedef struct _CRangeEnc\r
+\r
+typedef struct\r
 {\r
   UInt32 range;\r
   Byte cache;\r
@@ -245,30 +272,14 @@ typedef struct _CRangeEnc
   SRes res;\r
 } CRangeEnc;\r
 \r
-typedef struct _CSeqInStreamBuf\r
-{\r
-  ISeqInStream funcTable;\r
-  const Byte *data;\r
-  SizeT rem;\r
-} CSeqInStreamBuf;\r
-\r
-static SRes MyRead(void *pp, void *data, size_t *size)\r
-{\r
-  size_t curSize = *size;\r
-  CSeqInStreamBuf *p = (CSeqInStreamBuf *)pp;\r
-  if (p->rem < curSize)\r
-    curSize = p->rem;\r
-  memcpy(data, p->data, curSize);\r
-  p->rem -= curSize;\r
-  p->data += curSize;\r
-  *size = curSize;\r
-  return SZ_OK;\r
-}\r
 \r
 typedef struct\r
 {\r
   CLzmaProb *litProbs;\r
 \r
+  UInt32 state;\r
+  UInt32 reps[LZMA_NUM_REPS];\r
+\r
   CLzmaProb isMatch[kNumStates][LZMA_NUM_PB_STATES_MAX];\r
   CLzmaProb isRep[kNumStates];\r
   CLzmaProb isRepG0[kNumStates];\r
@@ -282,33 +293,61 @@ typedef struct
   \r
   CLenPriceEnc lenEnc;\r
   CLenPriceEnc repLenEnc;\r
-\r
-  UInt32 reps[LZMA_NUM_REPS];\r
-  UInt32 state;\r
 } CSaveState;\r
 \r
-typedef struct _CLzmaEnc\r
+\r
+typedef struct\r
 {\r
-  IMatchFinder matchFinder;\r
   void *matchFinderObj;\r
+  IMatchFinder matchFinder;\r
+\r
+  UInt32 optimumEndIndex;\r
+  UInt32 optimumCurrentIndex;\r
 \r
-  #ifdef COMPRESS_MF_MT\r
+  UInt32 longestMatchLength;\r
+  UInt32 numPairs;\r
+  UInt32 numAvail;\r
+\r
+  UInt32 numFastBytes;\r
+  UInt32 additionalOffset;\r
+  UInt32 reps[LZMA_NUM_REPS];\r
+  UInt32 state;\r
+\r
+  unsigned lc, lp, pb;\r
+  unsigned lpMask, pbMask;\r
+  unsigned lclp;\r
+\r
+  CLzmaProb *litProbs;\r
+\r
+  Bool fastMode;\r
+  Bool writeEndMark;\r
+  Bool finished;\r
+  Bool multiThread;\r
+  Bool needInit;\r
+\r
+  UInt64 nowPos64;\r
+  \r
+  UInt32 matchPriceCount;\r
+  UInt32 alignPriceCount;\r
+\r
+  UInt32 distTableSize;\r
+\r
+  UInt32 dictSize;\r
+  SRes result;\r
+\r
+  CRangeEnc rc;\r
+\r
+  #ifndef _7ZIP_ST\r
   Bool mtMode;\r
   CMatchFinderMt matchFinderMt;\r
   #endif\r
 \r
   CMatchFinder matchFinderBase;\r
 \r
-  #ifdef COMPRESS_MF_MT\r
+  #ifndef _7ZIP_ST\r
   Byte pad[128];\r
   #endif\r
   \r
-  UInt32 optimumEndIndex;\r
-  UInt32 optimumCurrentIndex;\r
-\r
-  UInt32 longestMatchLength;\r
-  UInt32 numPairs;\r
-  UInt32 numAvail;\r
   COptimal opt[kNumOpts];\r
   \r
   #ifndef LZMA_LOG_BSR\r
@@ -317,22 +356,10 @@ typedef struct _CLzmaEnc
 \r
   UInt32 ProbPrices[kBitModelTotal >> kNumMoveReducingBits];\r
   UInt32 matches[LZMA_MATCH_LEN_MAX * 2 + 2 + 1];\r
-  UInt32 numFastBytes;\r
-  UInt32 additionalOffset;\r
-  UInt32 reps[LZMA_NUM_REPS];\r
-  UInt32 state;\r
 \r
   UInt32 posSlotPrices[kNumLenToPosStates][kDistTableSizeMax];\r
   UInt32 distancesPrices[kNumLenToPosStates][kNumFullDistances];\r
   UInt32 alignPrices[kAlignTableSize];\r
-  UInt32 alignPriceCount;\r
-\r
-  UInt32 distTableSize;\r
-\r
-  unsigned lc, lp, pb;\r
-  unsigned lpMask, pbMask;\r
-\r
-  CLzmaProb *litProbs;\r
 \r
   CLzmaProb isMatch[kNumStates][LZMA_NUM_PB_STATES_MAX];\r
   CLzmaProb isRep[kNumStates];\r
@@ -348,28 +375,14 @@ typedef struct _CLzmaEnc
   CLenPriceEnc lenEnc;\r
   CLenPriceEnc repLenEnc;\r
 \r
-  unsigned lclp;\r
-\r
-  Bool fastMode;\r
-  \r
-  CRangeEnc rc;\r
-\r
-  Bool writeEndMark;\r
-  UInt64 nowPos64;\r
-  UInt32 matchPriceCount;\r
-  Bool finished;\r
-  Bool multiThread;\r
-\r
-  SRes result;\r
-  UInt32 dictSize;\r
-  UInt32 matchFinderCycles;\r
-\r
-  ISeqInStream *inStream;\r
-  CSeqInStreamBuf seqBufInStream;\r
-\r
   CSaveState saveState;\r
+\r
+  #ifndef _7ZIP_ST\r
+  Byte pad2[128];\r
+  #endif\r
 } CLzmaEnc;\r
 \r
+\r
 void LzmaEnc_SaveState(CLzmaEncHandle pp)\r
 {\r
   CLzmaEnc *p = (CLzmaEnc *)pp;\r
@@ -393,7 +406,7 @@ void LzmaEnc_SaveState(CLzmaEncHandle pp)
   memcpy(dest->posEncoders, p->posEncoders, sizeof(p->posEncoders));\r
   memcpy(dest->posAlignEncoder, p->posAlignEncoder, sizeof(p->posAlignEncoder));\r
   memcpy(dest->reps, p->reps, sizeof(p->reps));\r
-  memcpy(dest->litProbs, p->litProbs, (0x300 << p->lclp) * sizeof(CLzmaProb));\r
+  memcpy(dest->litProbs, p->litProbs, ((UInt32)0x300 << p->lclp) * sizeof(CLzmaProb));\r
 }\r
 \r
 void LzmaEnc_RestoreState(CLzmaEncHandle pp)\r
@@ -419,7 +432,7 @@ void LzmaEnc_RestoreState(CLzmaEncHandle pp)
   memcpy(dest->posEncoders, p->posEncoders, sizeof(p->posEncoders));\r
   memcpy(dest->posAlignEncoder, p->posAlignEncoder, sizeof(p->posAlignEncoder));\r
   memcpy(dest->reps, p->reps, sizeof(p->reps));\r
-  memcpy(dest->litProbs, p->litProbs, (0x300 << dest->lclp) * sizeof(CLzmaProb));\r
+  memcpy(dest->litProbs, p->litProbs, ((UInt32)0x300 << dest->lclp) * sizeof(CLzmaProb));\r
 }\r
 \r
 SRes LzmaEnc_SetProps(CLzmaEncHandle pp, const CLzmaEncProps *props2)\r
@@ -428,11 +441,14 @@ SRes LzmaEnc_SetProps(CLzmaEncHandle pp, const CLzmaEncProps *props2)
   CLzmaEncProps props = *props2;\r
   LzmaEncProps_Normalize(&props);\r
 \r
-  if (props.lc > LZMA_LC_MAX || props.lp > LZMA_LP_MAX || props.pb > LZMA_PB_MAX ||\r
-      props.dictSize > (1 << kDicLogSizeMaxCompress) || props.dictSize > (1 << 30))\r
+  if (props.lc > LZMA_LC_MAX\r
+      || props.lp > LZMA_LP_MAX\r
+      || props.pb > LZMA_PB_MAX\r
+      || props.dictSize > ((UInt64)1 << kDicLogSizeMaxCompress)\r
+      || props.dictSize > kMaxHistorySize)\r
     return SZ_ERROR_PARAM;\r
+\r
   p->dictSize = props.dictSize;\r
-  p->matchFinderCycles = props.mc;\r
   {\r
     unsigned fb = props.fb;\r
     if (fb < 5)\r
@@ -445,7 +461,7 @@ SRes LzmaEnc_SetProps(CLzmaEncHandle pp, const CLzmaEncProps *props2)
   p->lp = props.lp;\r
   p->pb = props.pb;\r
   p->fastMode = (props.algo == 0);\r
-  p->matchFinderBase.btMode = props.btMode;\r
+  p->matchFinderBase.btMode = (Byte)(props.btMode ? 1 : 0);\r
   {\r
     UInt32 numHashBytes = 4;\r
     if (props.btMode)\r
@@ -462,7 +478,7 @@ SRes LzmaEnc_SetProps(CLzmaEncHandle pp, const CLzmaEncProps *props2)
 \r
   p->writeEndMark = props.writeEndMark;\r
 \r
-  #ifdef COMPRESS_MF_MT\r
+  #ifndef _7ZIP_ST\r
   /*\r
   if (newMultiThread != _multiThread)\r
   {\r
@@ -489,8 +505,8 @@ static const int kShortRepNextStates[kNumStates]= {9, 9, 9, 9, 9, 9, 9, 11, 11,
 \r
 static void RangeEnc_Construct(CRangeEnc *p)\r
 {\r
-  p->outStream = 0;\r
-  p->bufBase = 0;\r
+  p->outStream = NULL;\r
+  p->bufBase = NULL;\r
 }\r
 \r
 #define RangeEnc_GetProcessed(p) ((p)->processed + ((p)->buf - (p)->bufBase) + (p)->cacheSize)\r
@@ -498,10 +514,10 @@ static void RangeEnc_Construct(CRangeEnc *p)
 #define RC_BUF_SIZE (1 << 16)\r
 static int RangeEnc_Alloc(CRangeEnc *p, ISzAlloc *alloc)\r
 {\r
-  if (p->bufBase == 0)\r
+  if (!p->bufBase)\r
   {\r
     p->bufBase = (Byte *)alloc->Alloc(alloc, RC_BUF_SIZE);\r
-    if (p->bufBase == 0)\r
+    if (!p->bufBase)\r
       return 0;\r
     p->bufLim = p->bufBase + RC_BUF_SIZE;\r
   }\r
@@ -542,7 +558,7 @@ static void RangeEnc_FlushStream(CRangeEnc *p)
 \r
 static void MY_FAST_CALL RangeEnc_ShiftLow(CRangeEnc *p)\r
 {\r
-  if ((UInt32)p->low < (UInt32)0xFF000000 || (int)(p->low >> 32) != 0)\r
+  if ((UInt32)p->low < (UInt32)0xFF000000 || (unsigned)(p->low >> 32) != 0)\r
   {\r
     Byte temp = p->cache;\r
     do\r
@@ -568,7 +584,7 @@ static void RangeEnc_FlushData(CRangeEnc *p)
     RangeEnc_ShiftLow(p);\r
 }\r
 \r
-static void RangeEnc_EncodeDirectBits(CRangeEnc *p, UInt32 value, int numBits)\r
+static void RangeEnc_EncodeDirectBits(CRangeEnc *p, UInt32 value, unsigned numBits)\r
 {\r
   do\r
   {\r
@@ -631,7 +647,7 @@ static void LitEnc_EncodeMatched(CRangeEnc *p, CLzmaProb *probs, UInt32 symbol,
   while (symbol < 0x10000);\r
 }\r
 \r
-void LzmaEnc_InitPriceTables(UInt32 *ProbPrices)\r
+static void LzmaEnc_InitPriceTables(UInt32 *ProbPrices)\r
 {\r
   UInt32 i;\r
   for (i = (1 << kNumMoveReducingBits) / 2; i < kBitModelTotal; i += (1 << kNumMoveReducingBits))\r
@@ -667,7 +683,7 @@ void LzmaEnc_InitPriceTables(UInt32 *ProbPrices)
 #define GET_PRICE_0a(prob) ProbPrices[(prob) >> kNumMoveReducingBits]\r
 #define GET_PRICE_1a(prob) ProbPrices[((prob) ^ (kBitModelTotal - 1)) >> kNumMoveReducingBits]\r
 \r
-static UInt32 LitEnc_GetPrice(const CLzmaProb *probs, UInt32 symbol, UInt32 *ProbPrices)\r
+static UInt32 LitEnc_GetPrice(const CLzmaProb *probs, UInt32 symbol, const UInt32 *ProbPrices)\r
 {\r
   UInt32 price = 0;\r
   symbol |= 0x100;\r
@@ -680,7 +696,7 @@ static UInt32 LitEnc_GetPrice(const CLzmaProb *probs, UInt32 symbol, UInt32 *Pro
   return price;\r
 }\r
 \r
-static UInt32 LitEnc_GetPriceMatched(const CLzmaProb *probs, UInt32 symbol, UInt32 matchByte, UInt32 *ProbPrices)\r
+static UInt32 LitEnc_GetPriceMatched(const CLzmaProb *probs, UInt32 symbol, UInt32 matchByte, const UInt32 *ProbPrices)\r
 {\r
   UInt32 price = 0;\r
   UInt32 offs = 0x100;\r
@@ -724,7 +740,7 @@ static void RcTree_ReverseEncode(CRangeEnc *rc, CLzmaProb *probs, int numBitLeve
   }\r
 }\r
 \r
-static UInt32 RcTree_GetPrice(const CLzmaProb *probs, int numBitLevels, UInt32 symbol, UInt32 *ProbPrices)\r
+static UInt32 RcTree_GetPrice(const CLzmaProb *probs, int numBitLevels, UInt32 symbol, const UInt32 *ProbPrices)\r
 {\r
   UInt32 price = 0;\r
   symbol |= (1 << numBitLevels);\r
@@ -736,7 +752,7 @@ static UInt32 RcTree_GetPrice(const CLzmaProb *probs, int numBitLevels, UInt32 s
   return price;\r
 }\r
 \r
-static UInt32 RcTree_ReverseGetPrice(const CLzmaProb *probs, int numBitLevels, UInt32 symbol, UInt32 *ProbPrices)\r
+static UInt32 RcTree_ReverseGetPrice(const CLzmaProb *probs, int numBitLevels, UInt32 symbol, const UInt32 *ProbPrices)\r
 {\r
   UInt32 price = 0;\r
   UInt32 m = 1;\r
@@ -787,7 +803,7 @@ static void LenEnc_Encode(CLenEnc *p, CRangeEnc *rc, UInt32 symbol, UInt32 posSt
   }\r
 }\r
 \r
-static void LenEnc_SetPrices(CLenEnc *p, UInt32 posState, UInt32 numSymbols, UInt32 *prices, UInt32 *ProbPrices)\r
+static void LenEnc_SetPrices(CLenEnc *p, UInt32 posState, UInt32 numSymbols, UInt32 *prices, const UInt32 *ProbPrices)\r
 {\r
   UInt32 a0 = GET_PRICE_0a(p->choice);\r
   UInt32 a1 = GET_PRICE_1a(p->choice);\r
@@ -810,20 +826,20 @@ static void LenEnc_SetPrices(CLenEnc *p, UInt32 posState, UInt32 numSymbols, UIn
     prices[i] = b1 + RcTree_GetPrice(p->high, kLenNumHighBits, i - kLenNumLowSymbols - kLenNumMidSymbols, ProbPrices);\r
 }\r
 \r
-static void MY_FAST_CALL LenPriceEnc_UpdateTable(CLenPriceEnc *p, UInt32 posState, UInt32 *ProbPrices)\r
+static void MY_FAST_CALL LenPriceEnc_UpdateTable(CLenPriceEnc *p, UInt32 posState, const UInt32 *ProbPrices)\r
 {\r
   LenEnc_SetPrices(&p->p, posState, p->tableSize, p->prices[posState], ProbPrices);\r
   p->counters[posState] = p->tableSize;\r
 }\r
 \r
-static void LenPriceEnc_UpdateTables(CLenPriceEnc *p, UInt32 numPosStates, UInt32 *ProbPrices)\r
+static void LenPriceEnc_UpdateTables(CLenPriceEnc *p, UInt32 numPosStates, const UInt32 *ProbPrices)\r
 {\r
   UInt32 posState;\r
   for (posState = 0; posState < numPosStates; posState++)\r
     LenPriceEnc_UpdateTable(p, posState, ProbPrices);\r
 }\r
 \r
-static void LenEnc_Encode2(CLenPriceEnc *p, CRangeEnc *rc, UInt32 symbol, UInt32 posState, Bool updatePrice, UInt32 *ProbPrices)\r
+static void LenEnc_Encode2(CLenPriceEnc *p, CRangeEnc *rc, UInt32 symbol, UInt32 posState, Bool updatePrice, const UInt32 *ProbPrices)\r
 {\r
   LenEnc_Encode(&p->p, rc, symbol, posState);\r
   if (updatePrice)\r
@@ -837,9 +853,10 @@ static void LenEnc_Encode2(CLenPriceEnc *p, CRangeEnc *rc, UInt32 symbol, UInt32
 static void MovePos(CLzmaEnc *p, UInt32 num)\r
 {\r
   #ifdef SHOW_STAT\r
-  ttt += num;\r
-  printf("\n MovePos %d", num);\r
+  g_STAT_OFFSET += num;\r
+  printf("\n MovePos %u", num);\r
   #endif\r
+  \r
   if (num != 0)\r
   {\r
     p->additionalOffset += num;\r
@@ -852,28 +869,32 @@ static UInt32 ReadMatchDistances(CLzmaEnc *p, UInt32 *numDistancePairsRes)
   UInt32 lenRes = 0, numPairs;\r
   p->numAvail = p->matchFinder.GetNumAvailableBytes(p->matchFinderObj);\r
   numPairs = p->matchFinder.GetMatches(p->matchFinderObj, p->matches);\r
+  \r
   #ifdef SHOW_STAT\r
-  printf("\n i = %d numPairs = %d    ", ttt, numPairs / 2);\r
-  ttt++;\r
+  printf("\n i = %u numPairs = %u    ", g_STAT_OFFSET, numPairs / 2);\r
+  g_STAT_OFFSET++;\r
   {\r
     UInt32 i;\r
     for (i = 0; i < numPairs; i += 2)\r
-      printf("%2d %6d   | ", p->matches[i], p->matches[i + 1]);\r
+      printf("%2u %6u   | ", p->matches[i], p->matches[i + 1]);\r
   }\r
   #endif\r
+  \r
   if (numPairs > 0)\r
   {\r
     lenRes = p->matches[numPairs - 2];\r
     if (lenRes == p->numFastBytes)\r
     {\r
-      const Byte *pby = p->matchFinder.GetPointerToCurrentPos(p->matchFinderObj) - 1;\r
-      UInt32 distance = p->matches[numPairs - 1] + 1;\r
       UInt32 numAvail = p->numAvail;\r
       if (numAvail > LZMA_MATCH_LEN_MAX)\r
         numAvail = LZMA_MATCH_LEN_MAX;\r
       {\r
-        const Byte *pby2 = pby - distance;\r
-        for (; lenRes < numAvail && pby[lenRes] == pby2[lenRes]; lenRes++);\r
+        const Byte *pbyCur = p->matchFinder.GetPointerToCurrentPos(p->matchFinderObj) - 1;\r
+        const Byte *pby = pbyCur + lenRes;\r
+        ptrdiff_t dif = (ptrdiff_t)-1 - p->matches[numPairs - 1];\r
+        const Byte *pbyLim = pbyCur + numAvail;\r
+        for (; pby != pbyLim && *pby == pby[dif]; pby++);\r
+        lenRes = (UInt32)(pby - pbyCur);\r
       }\r
     }\r
   }\r
@@ -958,16 +979,21 @@ static UInt32 Backward(CLzmaEnc *p, UInt32 *backRes, UInt32 cur)
   return p->optimumCurrentIndex;\r
 }\r
 \r
-#define LIT_PROBS(pos, prevByte) (p->litProbs + ((((pos) & p->lpMask) << p->lc) + ((prevByte) >> (8 - p->lc))) * 0x300)\r
+#define LIT_PROBS(pos, prevByte) (p->litProbs + ((((pos) & p->lpMask) << p->lc) + ((prevByte) >> (8 - p->lc))) * (UInt32)0x300)\r
 \r
 static UInt32 GetOptimum(CLzmaEnc *p, UInt32 position, UInt32 *backRes)\r
 {\r
-  UInt32 numAvail, mainLen, numPairs, repMaxIndex, i, posState, lenEnd, len, cur;\r
-  UInt32 matchPrice, repMatchPrice, normalMatchPrice;\r
+  UInt32 lenEnd, cur;\r
   UInt32 reps[LZMA_NUM_REPS], repLens[LZMA_NUM_REPS];\r
   UInt32 *matches;\r
+\r
+  {\r
+\r
+  UInt32 numAvail, mainLen, numPairs, repMaxIndex, i, posState, len;\r
+  UInt32 matchPrice, repMatchPrice, normalMatchPrice;\r
   const Byte *data;\r
   Byte curByte, matchByte;\r
+\r
   if (p->optimumEndIndex != p->optimumCurrentIndex)\r
   {\r
     const COptimal *opt = &p->opt[p->optimumCurrentIndex];\r
@@ -1002,7 +1028,7 @@ static UInt32 GetOptimum(CLzmaEnc *p, UInt32 position, UInt32 *backRes)
     UInt32 lenTest;\r
     const Byte *data2;\r
     reps[i] = p->reps[i];\r
-    data2 = data - (reps[i] + 1);\r
+    data2 = data - reps[i] - 1;\r
     if (data[0] != data2[0] || data[1] != data2[1])\r
     {\r
       repLens[i] = 0;\r
@@ -1146,20 +1172,25 @@ static UInt32 GetOptimum(CLzmaEnc *p, UInt32 position, UInt32 *backRes)
   cur = 0;\r
 \r
     #ifdef SHOW_STAT2\r
-    if (position >= 0)\r
+    /* if (position >= 0) */\r
     {\r
-      unsigned ii;\r
+      unsigned i;\r
       printf("\n pos = %4X", position);\r
-      for (ii = cur; ii <= lenEnd; ii++)\r
-      printf("\nprice[%4X] = %d", position - cur + ii, p->opt[ii].price);\r
+      for (i = cur; i <= lenEnd; i++)\r
+      printf("\nprice[%4X] = %u", position - cur + i, p->opt[i].price);\r
     }\r
     #endif\r
 \r
+  }\r
+\r
   for (;;)\r
   {\r
-    UInt32 numAvailFull, newLen, posPrev, state, startLen;\r
-    UInt32 curPrice, curAnd1Price;\r
+    UInt32 numAvail;\r
+    UInt32 numAvailFull, newLen, numPairs, posPrev, state, posState, startLen;\r
+    UInt32 curPrice, curAnd1Price, matchPrice, repMatchPrice;\r
     Bool nextIsChar;\r
+    Byte curByte, matchByte;\r
+    const Byte *data;\r
     COptimal *curOpt;\r
     COptimal *nextOpt;\r
 \r
@@ -1222,6 +1253,7 @@ static UInt32 GetOptimum(CLzmaEnc *p, UInt32 position, UInt32 *backRes)
       prevOpt = &p->opt[posPrev];\r
       if (pos < LZMA_NUM_REPS)\r
       {\r
+        UInt32 i;\r
         reps[0] = prevOpt->backs[pos];\r
         for (i = 1; i <= pos; i++)\r
           reps[i] = prevOpt->backs[i - 1];\r
@@ -1230,6 +1262,7 @@ static UInt32 GetOptimum(CLzmaEnc *p, UInt32 position, UInt32 *backRes)
       }\r
       else\r
       {\r
+        UInt32 i;\r
         reps[0] = (pos - LZMA_NUM_REPS);\r
         for (i = 1; i < LZMA_NUM_REPS; i++)\r
           reps[i] = prevOpt->backs[i - 1];\r
@@ -1299,7 +1332,7 @@ static UInt32 GetOptimum(CLzmaEnc *p, UInt32 position, UInt32 *backRes)
       /* try Literal + rep0 */\r
       UInt32 temp;\r
       UInt32 lenTest2;\r
-      const Byte *data2 = data - (reps[0] + 1);\r
+      const Byte *data2 = data - reps[0] - 1;\r
       UInt32 limit = p->numFastBytes + 1;\r
       if (limit > numAvailFull)\r
         limit = numAvailFull;\r
@@ -1315,11 +1348,12 @@ static UInt32 GetOptimum(CLzmaEnc *p, UInt32 position, UInt32 *backRes)
             GET_PRICE_1(p->isRep[state2]);\r
         /* for (; lenTest2 >= 2; lenTest2--) */\r
         {\r
+          UInt32 curAndLenPrice;\r
           COptimal *opt;\r
           UInt32 offset = cur + 1 + lenTest2;\r
           while (lenEnd < offset)\r
             p->opt[++lenEnd].price = kInfinityPrice;\r
-          UInt32 curAndLenPrice = nextRepMatchPrice + GetRepPrice(p, 0, lenTest2, state2, posStateNext);\r
+          curAndLenPrice = nextRepMatchPrice + GetRepPrice(p, 0, lenTest2, state2, posStateNext);\r
           opt = &p->opt[offset];\r
           if (curAndLenPrice < opt->price)\r
           {\r
@@ -1341,7 +1375,7 @@ static UInt32 GetOptimum(CLzmaEnc *p, UInt32 position, UInt32 *backRes)
       UInt32 lenTest;\r
       UInt32 lenTestTemp;\r
       UInt32 price;\r
-      const Byte *data2 = data - (reps[repIndex] + 1);\r
+      const Byte *data2 = data - reps[repIndex] - 1;\r
       if (data[0] != data2[0] || data[1] != data2[1])\r
         continue;\r
       for (lenTest = 2; lenTest < numAvail && data[lenTest] == data2[lenTest]; lenTest++);\r
@@ -1366,20 +1400,18 @@ static UInt32 GetOptimum(CLzmaEnc *p, UInt32 position, UInt32 *backRes)
       \r
       if (repIndex == 0)\r
         startLen = lenTest + 1;\r
-\r
-#ifndef _MSC_VER\r
-      if (1 /* _maxMode */)\r
-#endif\r
+        \r
+      /* if (_maxMode) */\r
         {\r
           UInt32 lenTest2 = lenTest + 1;\r
           UInt32 limit = lenTest2 + p->numFastBytes;\r
-          UInt32 nextRepMatchPrice;\r
           if (limit > numAvailFull)\r
             limit = numAvailFull;\r
           for (; lenTest2 < limit && data[lenTest2] == data2[lenTest2]; lenTest2++);\r
           lenTest2 -= lenTest + 1;\r
           if (lenTest2 >= 2)\r
           {\r
+            UInt32 nextRepMatchPrice;\r
             UInt32 state2 = kRepNextStates[state];\r
             UInt32 posStateNext = (position + lenTest) & p->pbMask;\r
             UInt32 curAndLenCharPrice =\r
@@ -1427,7 +1459,7 @@ static UInt32 GetOptimum(CLzmaEnc *p, UInt32 position, UInt32 *backRes)
     }\r
     if (newLen >= startLen)\r
     {\r
-      normalMatchPrice = matchPrice + GET_PRICE_0(p->isRep[state]);\r
+      UInt32 normalMatchPrice = matchPrice + GET_PRICE_0(p->isRep[state]);\r
       UInt32 offs, curBack, posSlot;\r
       UInt32 lenTest;\r
       while (lenEnd < cur + newLen)\r
@@ -1440,8 +1472,8 @@ static UInt32 GetOptimum(CLzmaEnc *p, UInt32 position, UInt32 *backRes)
       GetPosSlot2(curBack, posSlot);\r
       for (lenTest = /*2*/ startLen; ; lenTest++)\r
       {\r
-        UInt32 curAndLenPrice;\r
-        curAndLenPrice = normalMatchPrice + p->lenEnc.prices[posState][lenTest - LZMA_MATCH_LEN_MIN];\r
+        UInt32 curAndLenPrice = normalMatchPrice + p->lenEnc.prices[posState][lenTest - LZMA_MATCH_LEN_MIN];\r
+        {\r
         UInt32 lenToPosState = GetLenToPosState(lenTest);\r
         COptimal *opt;\r
         if (curBack < kNumFullDistances)\r
@@ -1457,20 +1489,21 @@ static UInt32 GetOptimum(CLzmaEnc *p, UInt32 position, UInt32 *backRes)
           opt->backPrev = curBack + LZMA_NUM_REPS;\r
           opt->prev1IsChar = False;\r
         }\r
+        }\r
 \r
         if (/*_maxMode && */lenTest == matches[offs])\r
         {\r
           /* Try Match + Literal + Rep0 */\r
-          const Byte *data2 = data - (curBack + 1);\r
+          const Byte *data2 = data - curBack - 1;\r
           UInt32 lenTest2 = lenTest + 1;\r
           UInt32 limit = lenTest2 + p->numFastBytes;\r
-          UInt32 nextRepMatchPrice;\r
           if (limit > numAvailFull)\r
             limit = numAvailFull;\r
           for (; lenTest2 < limit && data[lenTest2] == data2[lenTest2]; lenTest2++);\r
           lenTest2 -= lenTest + 1;\r
           if (lenTest2 >= 2)\r
           {\r
+            UInt32 nextRepMatchPrice;\r
             UInt32 state2 = kMatchNextStates[state];\r
             UInt32 posStateNext = (position + lenTest) & p->pbMask;\r
             UInt32 curAndLenCharPrice = curAndLenPrice +\r
@@ -1486,14 +1519,15 @@ static UInt32 GetOptimum(CLzmaEnc *p, UInt32 position, UInt32 *backRes)
             /* for (; lenTest2 >= 2; lenTest2--) */\r
             {\r
               UInt32 offset = cur + lenTest + 1 + lenTest2;\r
-\r
+              UInt32 curAndLenPrice2;\r
+              COptimal *opt;\r
               while (lenEnd < offset)\r
                 p->opt[++lenEnd].price = kInfinityPrice;\r
-              curAndLenPrice = nextRepMatchPrice + GetRepPrice(p, 0, lenTest2, state2, posStateNext);\r
+              curAndLenPrice2 = nextRepMatchPrice + GetRepPrice(p, 0, lenTest2, state2, posStateNext);\r
               opt = &p->opt[offset];\r
-              if (curAndLenPrice < opt->price)\r
+              if (curAndLenPrice2 < opt->price)\r
               {\r
-                opt->price = curAndLenPrice;\r
+                opt->price = curAndLenPrice2;\r
                 opt->posPrev = cur + lenTest + 1;\r
                 opt->backPrev = 0;\r
                 opt->prev1IsChar = True;\r
@@ -1543,7 +1577,7 @@ static UInt32 GetOptimumFast(CLzmaEnc *p, UInt32 *backRes)
   for (i = 0; i < LZMA_NUM_REPS; i++)\r
   {\r
     UInt32 len;\r
-    const Byte *data2 = data - (p->reps[i] + 1);\r
+    const Byte *data2 = data - p->reps[i] - 1;\r
     if (data[0] != data2[0] || data[1] != data2[1])\r
       continue;\r
     for (len = 2; len < numAvail && data[len] == data2[len]; len++);\r
@@ -1612,7 +1646,7 @@ static UInt32 GetOptimumFast(CLzmaEnc *p, UInt32 *backRes)
   for (i = 0; i < LZMA_NUM_REPS; i++)\r
   {\r
     UInt32 len, limit;\r
-    const Byte *data2 = data - (p->reps[i] + 1);\r
+    const Byte *data2 = data - p->reps[i] - 1;\r
     if (data[0] != data2[0] || data[1] != data2[1])\r
       continue;\r
     limit = mainLen - 1;\r
@@ -1694,7 +1728,6 @@ static void FillDistancesPrices(CLzmaEnc *p)
 \r
     {\r
       UInt32 *distancesPrices = p->distancesPrices[lenToPosState];\r
-\r
       for (i = 0; i < kStartPosModelIndex; i++)\r
         distancesPrices[i] = posSlotPrices[i];\r
       for (; i < kNumFullDistances; i++)\r
@@ -1708,7 +1741,8 @@ void LzmaEnc_Construct(CLzmaEnc *p)
 {\r
   RangeEnc_Construct(&p->rc);\r
   MatchFinder_Construct(&p->matchFinderBase);\r
-  #ifdef COMPRESS_MF_MT\r
+  \r
+  #ifndef _7ZIP_ST\r
   MatchFinderMt_Construct(&p->matchFinderMt);\r
   p->matchFinderMt.MatchFinder = &p->matchFinderBase;\r
   #endif\r
@@ -1724,15 +1758,15 @@ void LzmaEnc_Construct(CLzmaEnc *p)
   #endif\r
 \r
   LzmaEnc_InitPriceTables(p->ProbPrices);\r
-  p->litProbs = 0;\r
-  p->saveState.litProbs = 0;\r
+  p->litProbs = NULL;\r
+  p->saveState.litProbs = NULL;\r
 }\r
 \r
 CLzmaEncHandle LzmaEnc_Create(ISzAlloc *alloc)\r
 {\r
   void *p;\r
   p = alloc->Alloc(alloc, sizeof(CLzmaEnc));\r
-  if (p != 0)\r
+  if (p)\r
     LzmaEnc_Construct((CLzmaEnc *)p);\r
   return p;\r
 }\r
@@ -1741,15 +1775,16 @@ void LzmaEnc_FreeLits(CLzmaEnc *p, ISzAlloc *alloc)
 {\r
   alloc->Free(alloc, p->litProbs);\r
   alloc->Free(alloc, p->saveState.litProbs);\r
-  p->litProbs = 0;\r
-  p->saveState.litProbs = 0;\r
+  p->litProbs = NULL;\r
+  p->saveState.litProbs = NULL;\r
 }\r
 \r
 void LzmaEnc_Destruct(CLzmaEnc *p, ISzAlloc *alloc, ISzAlloc *allocBig)\r
 {\r
-  #ifdef COMPRESS_MF_MT\r
+  #ifndef _7ZIP_ST\r
   MatchFinderMt_Destruct(&p->matchFinderMt, allocBig);\r
   #endif\r
+  \r
   MatchFinder_Free(&p->matchFinderBase, allocBig);\r
   LzmaEnc_FreeLits(p, alloc);\r
   RangeEnc_Free(&p->rc, alloc);\r
@@ -1764,11 +1799,10 @@ void LzmaEnc_Destroy(CLzmaEncHandle p, ISzAlloc *alloc, ISzAlloc *allocBig)
 static SRes LzmaEnc_CodeOneBlock(CLzmaEnc *p, Bool useLimits, UInt32 maxPackSize, UInt32 maxUnpackSize)\r
 {\r
   UInt32 nowPos32, startPos32;\r
-  if (p->inStream != 0)\r
+  if (p->needInit)\r
   {\r
-    p->matchFinderBase.stream = p->inStream;\r
     p->matchFinder.Init(p->matchFinderObj);\r
-    p->inStream = 0;\r
+    p->needInit = 0;\r
   }\r
 \r
   if (p->finished)\r
@@ -1787,7 +1821,7 @@ static SRes LzmaEnc_CodeOneBlock(CLzmaEnc *p, Bool useLimits, UInt32 maxPackSize
     ReadMatchDistances(p, &numPairs);\r
     RangeEnc_EncodeBit(&p->rc, &p->isMatch[p->state][0], 0);\r
     p->state = kLiteralNextStates[p->state];\r
-    curByte = p->matchFinder.GetIndexByte(p->matchFinderObj, 0 - p->additionalOffset);\r
+    curByte = *(p->matchFinder.GetPointerToCurrentPos(p->matchFinderObj) - p->additionalOffset);\r
     LitEnc_Encode(&p->rc, p->litProbs, curByte);\r
     p->additionalOffset--;\r
     nowPos32++;\r
@@ -1804,7 +1838,7 @@ static SRes LzmaEnc_CodeOneBlock(CLzmaEnc *p, Bool useLimits, UInt32 maxPackSize
       len = GetOptimum(p, nowPos32, &pos);\r
 \r
     #ifdef SHOW_STAT2\r
-    printf("\n pos = %4X,   len = %d   pos = %d", nowPos32, len, pos);\r
+    printf("\n pos = %4X,   len = %u   pos = %u", nowPos32, len, pos);\r
     #endif\r
 \r
     posState = nowPos32 & p->pbMask;\r
@@ -1913,7 +1947,7 @@ static SRes LzmaEnc_CodeOneBlock(CLzmaEnc *p, Bool useLimits, UInt32 maxPackSize
             RangeEnc_GetProcessed(&p->rc) + kNumOpts * 2 >= maxPackSize)\r
           break;\r
       }\r
-      else if (processed >= (1 << 15))\r
+      else if (processed >= (1 << 17))\r
       {\r
         p->nowPos64 += nowPos32 - startPos32;\r
         return CheckErrors(p);\r
@@ -1929,24 +1963,21 @@ static SRes LzmaEnc_CodeOneBlock(CLzmaEnc *p, Bool useLimits, UInt32 maxPackSize
 static SRes LzmaEnc_Alloc(CLzmaEnc *p, UInt32 keepWindowSize, ISzAlloc *alloc, ISzAlloc *allocBig)\r
 {\r
   UInt32 beforeSize = kNumOpts;\r
-  #ifdef COMPRESS_MF_MT\r
-  Bool btMode;\r
-  #endif\r
   if (!RangeEnc_Alloc(&p->rc, alloc))\r
     return SZ_ERROR_MEM;\r
-  #ifdef COMPRESS_MF_MT\r
-  btMode = (p->matchFinderBase.btMode != 0);\r
-  p->mtMode = (p->multiThread && !p->fastMode && btMode);\r
+\r
+  #ifndef _7ZIP_ST\r
+  p->mtMode = (p->multiThread && !p->fastMode && (p->matchFinderBase.btMode != 0));\r
   #endif\r
 \r
   {\r
     unsigned lclp = p->lc + p->lp;\r
-    if (p->litProbs == 0 || p->saveState.litProbs == 0 || p->lclp != lclp)\r
+    if (!p->litProbs || !p->saveState.litProbs || p->lclp != lclp)\r
     {\r
       LzmaEnc_FreeLits(p, alloc);\r
-      p->litProbs = (CLzmaProb *)alloc->Alloc(alloc, (0x300 << lclp) * sizeof(CLzmaProb));\r
-      p->saveState.litProbs = (CLzmaProb *)alloc->Alloc(alloc, (0x300 << lclp) * sizeof(CLzmaProb));\r
-      if (p->litProbs == 0 || p->saveState.litProbs == 0)\r
+      p->litProbs = (CLzmaProb *)alloc->Alloc(alloc, ((UInt32)0x300 << lclp) * sizeof(CLzmaProb));\r
+      p->saveState.litProbs = (CLzmaProb *)alloc->Alloc(alloc, ((UInt32)0x300 << lclp) * sizeof(CLzmaProb));\r
+      if (!p->litProbs || !p->saveState.litProbs)\r
       {\r
         LzmaEnc_FreeLits(p, alloc);\r
         return SZ_ERROR_MEM;\r
@@ -1955,12 +1986,12 @@ static SRes LzmaEnc_Alloc(CLzmaEnc *p, UInt32 keepWindowSize, ISzAlloc *alloc, I
     }\r
   }\r
 \r
-  p->matchFinderBase.bigHash = (p->dictSize > kBigHashDicLimit);\r
+  p->matchFinderBase.bigHash = (Byte)(p->dictSize > kBigHashDicLimit ? 1 : 0);\r
 \r
   if (beforeSize + p->dictSize < keepWindowSize)\r
     beforeSize = keepWindowSize - p->dictSize;\r
 \r
-  #ifdef COMPRESS_MF_MT\r
+  #ifndef _7ZIP_ST\r
   if (p->mtMode)\r
   {\r
     RINOK(MatchFinderMt_Create(&p->matchFinderMt, p->dictSize, beforeSize, p->numFastBytes, LZMA_MATCH_LEN_MAX, allocBig));\r
@@ -1975,6 +2006,7 @@ static SRes LzmaEnc_Alloc(CLzmaEnc *p, UInt32 keepWindowSize, ISzAlloc *alloc, I
     p->matchFinderObj = &p->matchFinderBase;\r
     MatchFinder_CreateVTable(&p->matchFinderBase, &p->matchFinder);\r
   }\r
+  \r
   return SZ_OK;\r
 }\r
 \r
@@ -2003,9 +2035,10 @@ void LzmaEnc_Init(CLzmaEnc *p)
   }\r
 \r
   {\r
-    UInt32 num = 0x300 << (p->lp + p->lc);\r
+    UInt32 num = (UInt32)0x300 << (p->lp + p->lc);\r
+    CLzmaProb *probs = p->litProbs;\r
     for (i = 0; i < num; i++)\r
-      p->litProbs[i] = kProbInitValue;\r
+      probs[i] = kProbInitValue;\r
   }\r
 \r
   {\r
@@ -2068,11 +2101,12 @@ static SRes LzmaEnc_AllocAndInit(CLzmaEnc *p, UInt32 keepWindowSize, ISzAlloc *a
   return SZ_OK;\r
 }\r
 \r
-static SRes LzmaEnc_Prepare(CLzmaEncHandle pp, ISeqInStream *inStream, ISeqOutStream *outStream,\r
+static SRes LzmaEnc_Prepare(CLzmaEncHandle pp, ISeqOutStream *outStream, ISeqInStream *inStream,\r
     ISzAlloc *alloc, ISzAlloc *allocBig)\r
 {\r
   CLzmaEnc *p = (CLzmaEnc *)pp;\r
-  p->inStream = inStream;\r
+  p->matchFinderBase.stream = inStream;\r
+  p->needInit = 1;\r
   p->rc.outStream = outStream;\r
   return LzmaEnc_AllocAndInit(p, 0, alloc, allocBig);\r
 }\r
@@ -2082,15 +2116,16 @@ SRes LzmaEnc_PrepareForLzma2(CLzmaEncHandle pp,
     ISzAlloc *alloc, ISzAlloc *allocBig)\r
 {\r
   CLzmaEnc *p = (CLzmaEnc *)pp;\r
-  p->inStream = inStream;\r
+  p->matchFinderBase.stream = inStream;\r
+  p->needInit = 1;\r
   return LzmaEnc_AllocAndInit(p, keepWindowSize, alloc, allocBig);\r
 }\r
 \r
 static void LzmaEnc_SetInputBuf(CLzmaEnc *p, const Byte *src, SizeT srcLen)\r
 {\r
-  p->seqBufInStream.funcTable.Read = MyRead;\r
-  p->seqBufInStream.data = src;\r
-  p->seqBufInStream.rem = srcLen;\r
+  p->matchFinderBase.directInput = 1;\r
+  p->matchFinderBase.bufferBase = (Byte *)src;\r
+  p->matchFinderBase.directInputRem = srcLen;\r
 }\r
 \r
 SRes LzmaEnc_MemPrepare(CLzmaEncHandle pp, const Byte *src, SizeT srcLen,\r
@@ -2098,22 +2133,24 @@ SRes LzmaEnc_MemPrepare(CLzmaEncHandle pp, const Byte *src, SizeT srcLen,
 {\r
   CLzmaEnc *p = (CLzmaEnc *)pp;\r
   LzmaEnc_SetInputBuf(p, src, srcLen);\r
-  p->inStream = &p->seqBufInStream.funcTable;\r
+  p->needInit = 1;\r
+\r
   return LzmaEnc_AllocAndInit(p, keepWindowSize, alloc, allocBig);\r
 }\r
 \r
 void LzmaEnc_Finish(CLzmaEncHandle pp)\r
 {\r
-  #ifdef COMPRESS_MF_MT\r
+  #ifndef _7ZIP_ST\r
   CLzmaEnc *p = (CLzmaEnc *)pp;\r
   if (p->mtMode)\r
     MatchFinderMt_ReleaseStream(&p->matchFinderMt);\r
   #else\r
-  (void)pp;\r
+  UNUSED_VAR(pp);\r
   #endif\r
 }\r
 \r
-typedef struct _CSeqOutStreamBuf\r
+\r
+typedef struct\r
 {\r
   ISeqOutStream funcTable;\r
   Byte *data;\r
@@ -2142,12 +2179,14 @@ UInt32 LzmaEnc_GetNumAvailableBytes(CLzmaEncHandle pp)
   return p->matchFinder.GetNumAvailableBytes(p->matchFinderObj);\r
 }\r
 \r
+\r
 const Byte *LzmaEnc_GetCurBuf(CLzmaEncHandle pp)\r
 {\r
   const CLzmaEnc *p = (CLzmaEnc *)pp;\r
   return p->matchFinder.GetPointerToCurrentPos(p->matchFinderObj) - p->additionalOffset;\r
 }\r
 \r
+\r
 SRes LzmaEnc_CodeOneMemBlock(CLzmaEncHandle pp, Bool reInit,\r
     Byte *dest, size_t *destLen, UInt32 desiredPackSize, UInt32 *unpackSize)\r
 {\r
@@ -2182,27 +2221,23 @@ SRes LzmaEnc_CodeOneMemBlock(CLzmaEncHandle pp, Bool reInit,
   return res;\r
 }\r
 \r
-SRes LzmaEnc_Encode(CLzmaEncHandle pp, ISeqOutStream *outStream, ISeqInStream *inStream, ICompressProgress *progress,\r
-    ISzAlloc *alloc, ISzAlloc *allocBig)\r
+\r
+static SRes LzmaEnc_Encode2(CLzmaEnc *p, ICompressProgress *progress)\r
 {\r
-  CLzmaEnc *p = (CLzmaEnc *)pp;\r
   SRes res = SZ_OK;\r
 \r
-  #ifdef COMPRESS_MF_MT\r
+  #ifndef _7ZIP_ST\r
   Byte allocaDummy[0x300];\r
-  int i = 0;\r
-  for (i = 0; i < 16; i++)\r
-    allocaDummy[i] = (Byte)i;\r
+  allocaDummy[0] = 0;\r
+  allocaDummy[1] = allocaDummy[0];\r
   #endif\r
 \r
-  RINOK(LzmaEnc_Prepare(pp, inStream, outStream, alloc, allocBig));\r
-\r
   for (;;)\r
   {\r
     res = LzmaEnc_CodeOneBlock(p, False, 0, 0);\r
-    if (res != SZ_OK || p->finished != 0)\r
+    if (res != SZ_OK || p->finished)\r
       break;\r
-    if (progress != 0)\r
+    if (progress)\r
     {\r
       res = progress->Progress(progress, p->nowPos64, RangeEnc_GetProcessed(&p->rc));\r
       if (res != SZ_OK)\r
@@ -2212,32 +2247,47 @@ SRes LzmaEnc_Encode(CLzmaEncHandle pp, ISeqOutStream *outStream, ISeqInStream *i
       }\r
     }\r
   }\r
-  LzmaEnc_Finish(pp);\r
+  \r
+  LzmaEnc_Finish(p);\r
+\r
+  /*\r
+  if (res == S_OK && !Inline_MatchFinder_IsFinishedOK(&p->matchFinderBase))\r
+    res = SZ_ERROR_FAIL;\r
+  }\r
+  */\r
+\r
   return res;\r
 }\r
 \r
+\r
+SRes LzmaEnc_Encode(CLzmaEncHandle pp, ISeqOutStream *outStream, ISeqInStream *inStream, ICompressProgress *progress,\r
+    ISzAlloc *alloc, ISzAlloc *allocBig)\r
+{\r
+  RINOK(LzmaEnc_Prepare(pp, outStream, inStream, alloc, allocBig));\r
+  return LzmaEnc_Encode2((CLzmaEnc *)pp, progress);\r
+}\r
+\r
+\r
 SRes LzmaEnc_WriteProperties(CLzmaEncHandle pp, Byte *props, SizeT *size)\r
 {\r
   CLzmaEnc *p = (CLzmaEnc *)pp;\r
-  int i;\r
+  unsigned i;\r
   UInt32 dictSize = p->dictSize;\r
   if (*size < LZMA_PROPS_SIZE)\r
     return SZ_ERROR_PARAM;\r
   *size = LZMA_PROPS_SIZE;\r
   props[0] = (Byte)((p->pb * 5 + p->lp) * 9 + p->lc);\r
 \r
-  for (i = 11; i <= 30; i++)\r
+  if (dictSize >= ((UInt32)1 << 22))\r
   {\r
-    if (dictSize <= ((UInt32)2 << i))\r
-    {\r
-      dictSize = (2 << i);\r
-      break;\r
-    }\r
-    if (dictSize <= ((UInt32)3 << i))\r
-    {\r
-      dictSize = (3 << i);\r
-      break;\r
-    }\r
+    UInt32 kDictMask = ((UInt32)1 << 20) - 1;\r
+    if (dictSize < (UInt32)0xFFFFFFFF - kDictMask)\r
+      dictSize = (dictSize + kDictMask) & ~kDictMask;\r
+  }\r
+  else for (i = 11; i <= 30; i++)\r
+  {\r
+    if (dictSize <= ((UInt32)2 << i)) { dictSize = (2 << i); break; }\r
+    if (dictSize <= ((UInt32)3 << i)) { dictSize = (3 << i); break; }\r
   }\r
 \r
   for (i = 0; i < 4; i++)\r
@@ -2245,6 +2295,7 @@ SRes LzmaEnc_WriteProperties(CLzmaEncHandle pp, Byte *props, SizeT *size)
   return SZ_OK;\r
 }\r
 \r
+\r
 SRes LzmaEnc_MemEncode(CLzmaEncHandle pp, Byte *dest, SizeT *destLen, const Byte *src, SizeT srcLen,\r
     int writeEndMark, ICompressProgress *progress, ISzAlloc *alloc, ISzAlloc *allocBig)\r
 {\r
@@ -2253,16 +2304,22 @@ SRes LzmaEnc_MemEncode(CLzmaEncHandle pp, Byte *dest, SizeT *destLen, const Byte
 \r
   CSeqOutStreamBuf outStream;\r
 \r
-  LzmaEnc_SetInputBuf(p, src, srcLen);\r
-\r
   outStream.funcTable.Write = MyWrite;\r
   outStream.data = dest;\r
   outStream.rem = *destLen;\r
   outStream.overflow = False;\r
 \r
   p->writeEndMark = writeEndMark;\r
-  res = LzmaEnc_Encode(pp, &outStream.funcTable, &p->seqBufInStream.funcTable,\r
-      progress, alloc, allocBig);\r
+  p->rc.outStream = &outStream.funcTable;\r
+\r
+  res = LzmaEnc_MemPrepare(pp, src, srcLen, 0, alloc, allocBig);\r
+  \r
+  if (res == SZ_OK)\r
+  {\r
+    res = LzmaEnc_Encode2(p, progress);\r
+    if (res == SZ_OK && p->nowPos64 != srcLen)\r
+      res = SZ_ERROR_FAIL;\r
+  }\r
 \r
   *destLen -= outStream.rem;\r
   if (outStream.overflow)\r
@@ -2270,13 +2327,14 @@ SRes LzmaEnc_MemEncode(CLzmaEncHandle pp, Byte *dest, SizeT *destLen, const Byte
   return res;\r
 }\r
 \r
+\r
 SRes LzmaEncode(Byte *dest, SizeT *destLen, const Byte *src, SizeT srcLen,\r
     const CLzmaEncProps *props, Byte *propsEncoded, SizeT *propsSize, int writeEndMark,\r
     ICompressProgress *progress, ISzAlloc *alloc, ISzAlloc *allocBig)\r
 {\r
   CLzmaEnc *p = (CLzmaEnc *)LzmaEnc_Create(alloc);\r
   SRes res;\r
-  if (p == 0)\r
+  if (!p)\r
     return SZ_ERROR_MEM;\r
 \r
   res = LzmaEnc_SetProps(p, props);\r