goat3d

diff libs/openctm/liblzma/LzmaEnc.c @ 14:188c697b3b49

- added a document describing the goat3d file format chunk hierarchy - started an alternative XML-based file format - added the openctm library
author John Tsiombikas <nuclear@member.fsf.org>
date Thu, 26 Sep 2013 04:47:05 +0300
parents
children
line diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/libs/openctm/liblzma/LzmaEnc.c	Thu Sep 26 04:47:05 2013 +0300
     1.3 @@ -0,0 +1,2281 @@
     1.4 +/* LzmaEnc.c -- LZMA Encoder
     1.5 +2009-02-02 : Igor Pavlov : Public domain */
     1.6 +
     1.7 +#include <string.h>
     1.8 +
     1.9 +/* #define SHOW_STAT */
    1.10 +/* #define SHOW_STAT2 */
    1.11 +
    1.12 +#if defined(SHOW_STAT) || defined(SHOW_STAT2)
    1.13 +#include <stdio.h>
    1.14 +#endif
    1.15 +
    1.16 +#include "LzmaEnc.h"
    1.17 +
    1.18 +#include "LzFind.h"
    1.19 +#ifdef COMPRESS_MF_MT
    1.20 +#include "LzFindMt.h"
    1.21 +#endif
    1.22 +
    1.23 +#ifdef SHOW_STAT
    1.24 +static int ttt = 0;
    1.25 +#endif
    1.26 +
    1.27 +#define kBlockSizeMax ((1 << LZMA_NUM_BLOCK_SIZE_BITS) - 1)
    1.28 +
    1.29 +#define kBlockSize (9 << 10)
    1.30 +#define kUnpackBlockSize (1 << 18)
    1.31 +#define kMatchArraySize (1 << 21)
    1.32 +#define kMatchRecordMaxSize ((LZMA_MATCH_LEN_MAX * 2 + 3) * LZMA_MATCH_LEN_MAX)
    1.33 +
    1.34 +#define kNumMaxDirectBits (31)
    1.35 +
    1.36 +#define kNumTopBits 24
    1.37 +#define kTopValue ((UInt32)1 << kNumTopBits)
    1.38 +
    1.39 +#define kNumBitModelTotalBits 11
    1.40 +#define kBitModelTotal (1 << kNumBitModelTotalBits)
    1.41 +#define kNumMoveBits 5
    1.42 +#define kProbInitValue (kBitModelTotal >> 1)
    1.43 +
    1.44 +#define kNumMoveReducingBits 4
    1.45 +#define kNumBitPriceShiftBits 4
    1.46 +#define kBitPrice (1 << kNumBitPriceShiftBits)
    1.47 +
    1.48 +void LzmaEncProps_Init(CLzmaEncProps *p)
    1.49 +{
    1.50 +  p->level = 5;
    1.51 +  p->dictSize = p->mc = 0;
    1.52 +  p->lc = p->lp = p->pb = p->algo = p->fb = p->btMode = p->numHashBytes = p->numThreads = -1;
    1.53 +  p->writeEndMark = 0;
    1.54 +}
    1.55 +
    1.56 +void LzmaEncProps_Normalize(CLzmaEncProps *p)
    1.57 +{
    1.58 +  int level = p->level;
    1.59 +  if (level < 0) level = 5;
    1.60 +  p->level = level;
    1.61 +  if (p->dictSize == 0) p->dictSize = (level <= 5 ? (1 << (level * 2 + 14)) : (level == 6 ? (1 << 25) : (1 << 26)));
    1.62 +  if (p->lc < 0) p->lc = 3;
    1.63 +  if (p->lp < 0) p->lp = 0;
    1.64 +  if (p->pb < 0) p->pb = 2;
    1.65 +  if (p->algo < 0) p->algo = (level < 5 ? 0 : 1);
    1.66 +  if (p->fb < 0) p->fb = (level < 7 ? 32 : 64);
    1.67 +  if (p->btMode < 0) p->btMode = (p->algo == 0 ? 0 : 1);
    1.68 +  if (p->numHashBytes < 0) p->numHashBytes = 4;
    1.69 +  if (p->mc == 0)  p->mc = (16 + (p->fb >> 1)) >> (p->btMode ? 0 : 1);
    1.70 +  if (p->numThreads < 0)
    1.71 +    p->numThreads =
    1.72 +      #ifdef COMPRESS_MF_MT
    1.73 +      ((p->btMode && p->algo) ? 2 : 1);
    1.74 +      #else
    1.75 +      1;
    1.76 +      #endif
    1.77 +}
    1.78 +
    1.79 +UInt32 LzmaEncProps_GetDictSize(const CLzmaEncProps *props2)
    1.80 +{
    1.81 +  CLzmaEncProps props = *props2;
    1.82 +  LzmaEncProps_Normalize(&props);
    1.83 +  return props.dictSize;
    1.84 +}
    1.85 +
    1.86 +/* #define LZMA_LOG_BSR */
    1.87 +/* Define it for Intel's CPU */
    1.88 +
    1.89 +
    1.90 +#ifdef LZMA_LOG_BSR
    1.91 +
    1.92 +#define kDicLogSizeMaxCompress 30
    1.93 +
    1.94 +#define BSR2_RET(pos, res) { unsigned long i; _BitScanReverse(&i, (pos)); res = (i + i) + ((pos >> (i - 1)) & 1); }
    1.95 +
    1.96 +static UInt32 GetPosSlot1(UInt32 pos)
    1.97 +{
    1.98 +  UInt32 res;
    1.99 +  BSR2_RET(pos, res);
   1.100 +  return res;
   1.101 +}
   1.102 +#define GetPosSlot2(pos, res) { BSR2_RET(pos, res); }
   1.103 +#define GetPosSlot(pos, res) { if (pos < 2) res = pos; else BSR2_RET(pos, res); }
   1.104 +
   1.105 +#else
   1.106 +
   1.107 +#define kNumLogBits (9 + (int)sizeof(size_t) / 2)
   1.108 +#define kDicLogSizeMaxCompress ((kNumLogBits - 1) * 2 + 7)
   1.109 +
   1.110 +void LzmaEnc_FastPosInit(Byte *g_FastPos)
   1.111 +{
   1.112 +  int c = 2, slotFast;
   1.113 +  g_FastPos[0] = 0;
   1.114 +  g_FastPos[1] = 1;
   1.115 +  
   1.116 +  for (slotFast = 2; slotFast < kNumLogBits * 2; slotFast++)
   1.117 +  {
   1.118 +    UInt32 k = (1 << ((slotFast >> 1) - 1));
   1.119 +    UInt32 j;
   1.120 +    for (j = 0; j < k; j++, c++)
   1.121 +      g_FastPos[c] = (Byte)slotFast;
   1.122 +  }
   1.123 +}
   1.124 +
   1.125 +#define BSR2_RET(pos, res) { UInt32 i = 6 + ((kNumLogBits - 1) & \
   1.126 +  (0 - (((((UInt32)1 << (kNumLogBits + 6)) - 1) - pos) >> 31))); \
   1.127 +  res = p->g_FastPos[pos >> i] + (i * 2); }
   1.128 +/*
   1.129 +#define BSR2_RET(pos, res) { res = (pos < (1 << (kNumLogBits + 6))) ? \
   1.130 +  p->g_FastPos[pos >> 6] + 12 : \
   1.131 +  p->g_FastPos[pos >> (6 + kNumLogBits - 1)] + (6 + (kNumLogBits - 1)) * 2; }
   1.132 +*/
   1.133 +
   1.134 +#define GetPosSlot1(pos) p->g_FastPos[pos]
   1.135 +#define GetPosSlot2(pos, res) { BSR2_RET(pos, res); }
   1.136 +#define GetPosSlot(pos, res) { if (pos < kNumFullDistances) res = p->g_FastPos[pos]; else BSR2_RET(pos, res); }
   1.137 +
   1.138 +#endif
   1.139 +
   1.140 +
   1.141 +#define LZMA_NUM_REPS 4
   1.142 +
   1.143 +typedef unsigned CState;
   1.144 +
   1.145 +typedef struct _COptimal
   1.146 +{
   1.147 +  UInt32 price;
   1.148 +
   1.149 +  CState state;
   1.150 +  int prev1IsChar;
   1.151 +  int prev2;
   1.152 +
   1.153 +  UInt32 posPrev2;
   1.154 +  UInt32 backPrev2;
   1.155 +
   1.156 +  UInt32 posPrev;
   1.157 +  UInt32 backPrev;
   1.158 +  UInt32 backs[LZMA_NUM_REPS];
   1.159 +} COptimal;
   1.160 +
   1.161 +#define kNumOpts (1 << 12)
   1.162 +
   1.163 +#define kNumLenToPosStates 4
   1.164 +#define kNumPosSlotBits 6
   1.165 +#define kDicLogSizeMin 0
   1.166 +#define kDicLogSizeMax 32
   1.167 +#define kDistTableSizeMax (kDicLogSizeMax * 2)
   1.168 +
   1.169 +
   1.170 +#define kNumAlignBits 4
   1.171 +#define kAlignTableSize (1 << kNumAlignBits)
   1.172 +#define kAlignMask (kAlignTableSize - 1)
   1.173 +
   1.174 +#define kStartPosModelIndex 4
   1.175 +#define kEndPosModelIndex 14
   1.176 +#define kNumPosModels (kEndPosModelIndex - kStartPosModelIndex)
   1.177 +
   1.178 +#define kNumFullDistances (1 << (kEndPosModelIndex / 2))
   1.179 +
   1.180 +#ifdef _LZMA_PROB32
   1.181 +#define CLzmaProb UInt32
   1.182 +#else
   1.183 +#define CLzmaProb UInt16
   1.184 +#endif
   1.185 +
   1.186 +#define LZMA_PB_MAX 4
   1.187 +#define LZMA_LC_MAX 8
   1.188 +#define LZMA_LP_MAX 4
   1.189 +
   1.190 +#define LZMA_NUM_PB_STATES_MAX (1 << LZMA_PB_MAX)
   1.191 +
   1.192 +
   1.193 +#define kLenNumLowBits 3
   1.194 +#define kLenNumLowSymbols (1 << kLenNumLowBits)
   1.195 +#define kLenNumMidBits 3
   1.196 +#define kLenNumMidSymbols (1 << kLenNumMidBits)
   1.197 +#define kLenNumHighBits 8
   1.198 +#define kLenNumHighSymbols (1 << kLenNumHighBits)
   1.199 +
   1.200 +#define kLenNumSymbolsTotal (kLenNumLowSymbols + kLenNumMidSymbols + kLenNumHighSymbols)
   1.201 +
   1.202 +#define LZMA_MATCH_LEN_MIN 2
   1.203 +#define LZMA_MATCH_LEN_MAX (LZMA_MATCH_LEN_MIN + kLenNumSymbolsTotal - 1)
   1.204 +
   1.205 +#define kNumStates 12
   1.206 +
   1.207 +typedef struct
   1.208 +{
   1.209 +  CLzmaProb choice;
   1.210 +  CLzmaProb choice2;
   1.211 +  CLzmaProb low[LZMA_NUM_PB_STATES_MAX << kLenNumLowBits];
   1.212 +  CLzmaProb mid[LZMA_NUM_PB_STATES_MAX << kLenNumMidBits];
   1.213 +  CLzmaProb high[kLenNumHighSymbols];
   1.214 +} CLenEnc;
   1.215 +
   1.216 +typedef struct
   1.217 +{
   1.218 +  CLenEnc p;
   1.219 +  UInt32 prices[LZMA_NUM_PB_STATES_MAX][kLenNumSymbolsTotal];
   1.220 +  UInt32 tableSize;
   1.221 +  UInt32 counters[LZMA_NUM_PB_STATES_MAX];
   1.222 +} CLenPriceEnc;
   1.223 +
   1.224 +typedef struct _CRangeEnc
   1.225 +{
   1.226 +  UInt32 range;
   1.227 +  Byte cache;
   1.228 +  UInt64 low;
   1.229 +  UInt64 cacheSize;
   1.230 +  Byte *buf;
   1.231 +  Byte *bufLim;
   1.232 +  Byte *bufBase;
   1.233 +  ISeqOutStream *outStream;
   1.234 +  UInt64 processed;
   1.235 +  SRes res;
   1.236 +} CRangeEnc;
   1.237 +
   1.238 +typedef struct _CSeqInStreamBuf
   1.239 +{
   1.240 +  ISeqInStream funcTable;
   1.241 +  const Byte *data;
   1.242 +  SizeT rem;
   1.243 +} CSeqInStreamBuf;
   1.244 +
   1.245 +static SRes MyRead(void *pp, void *data, size_t *size)
   1.246 +{
   1.247 +  size_t curSize = *size;
   1.248 +  CSeqInStreamBuf *p = (CSeqInStreamBuf *)pp;
   1.249 +  if (p->rem < curSize)
   1.250 +    curSize = p->rem;
   1.251 +  memcpy(data, p->data, curSize);
   1.252 +  p->rem -= curSize;
   1.253 +  p->data += curSize;
   1.254 +  *size = curSize;
   1.255 +  return SZ_OK;
   1.256 +}
   1.257 +
   1.258 +typedef struct
   1.259 +{
   1.260 +  CLzmaProb *litProbs;
   1.261 +
   1.262 +  CLzmaProb isMatch[kNumStates][LZMA_NUM_PB_STATES_MAX];
   1.263 +  CLzmaProb isRep[kNumStates];
   1.264 +  CLzmaProb isRepG0[kNumStates];
   1.265 +  CLzmaProb isRepG1[kNumStates];
   1.266 +  CLzmaProb isRepG2[kNumStates];
   1.267 +  CLzmaProb isRep0Long[kNumStates][LZMA_NUM_PB_STATES_MAX];
   1.268 +
   1.269 +  CLzmaProb posSlotEncoder[kNumLenToPosStates][1 << kNumPosSlotBits];
   1.270 +  CLzmaProb posEncoders[kNumFullDistances - kEndPosModelIndex];
   1.271 +  CLzmaProb posAlignEncoder[1 << kNumAlignBits];
   1.272 +  
   1.273 +  CLenPriceEnc lenEnc;
   1.274 +  CLenPriceEnc repLenEnc;
   1.275 +
   1.276 +  UInt32 reps[LZMA_NUM_REPS];
   1.277 +  UInt32 state;
   1.278 +} CSaveState;
   1.279 +
   1.280 +typedef struct _CLzmaEnc
   1.281 +{
   1.282 +  IMatchFinder matchFinder;
   1.283 +  void *matchFinderObj;
   1.284 +
   1.285 +  #ifdef COMPRESS_MF_MT
   1.286 +  Bool mtMode;
   1.287 +  CMatchFinderMt matchFinderMt;
   1.288 +  #endif
   1.289 +
   1.290 +  CMatchFinder matchFinderBase;
   1.291 +
   1.292 +  #ifdef COMPRESS_MF_MT
   1.293 +  Byte pad[128];
   1.294 +  #endif
   1.295 +  
   1.296 +  UInt32 optimumEndIndex;
   1.297 +  UInt32 optimumCurrentIndex;
   1.298 +
   1.299 +  UInt32 longestMatchLength;
   1.300 +  UInt32 numPairs;
   1.301 +  UInt32 numAvail;
   1.302 +  COptimal opt[kNumOpts];
   1.303 +  
   1.304 +  #ifndef LZMA_LOG_BSR
   1.305 +  Byte g_FastPos[1 << kNumLogBits];
   1.306 +  #endif
   1.307 +
   1.308 +  UInt32 ProbPrices[kBitModelTotal >> kNumMoveReducingBits];
   1.309 +  UInt32 matches[LZMA_MATCH_LEN_MAX * 2 + 2 + 1];
   1.310 +  UInt32 numFastBytes;
   1.311 +  UInt32 additionalOffset;
   1.312 +  UInt32 reps[LZMA_NUM_REPS];
   1.313 +  UInt32 state;
   1.314 +
   1.315 +  UInt32 posSlotPrices[kNumLenToPosStates][kDistTableSizeMax];
   1.316 +  UInt32 distancesPrices[kNumLenToPosStates][kNumFullDistances];
   1.317 +  UInt32 alignPrices[kAlignTableSize];
   1.318 +  UInt32 alignPriceCount;
   1.319 +
   1.320 +  UInt32 distTableSize;
   1.321 +
   1.322 +  unsigned lc, lp, pb;
   1.323 +  unsigned lpMask, pbMask;
   1.324 +
   1.325 +  CLzmaProb *litProbs;
   1.326 +
   1.327 +  CLzmaProb isMatch[kNumStates][LZMA_NUM_PB_STATES_MAX];
   1.328 +  CLzmaProb isRep[kNumStates];
   1.329 +  CLzmaProb isRepG0[kNumStates];
   1.330 +  CLzmaProb isRepG1[kNumStates];
   1.331 +  CLzmaProb isRepG2[kNumStates];
   1.332 +  CLzmaProb isRep0Long[kNumStates][LZMA_NUM_PB_STATES_MAX];
   1.333 +
   1.334 +  CLzmaProb posSlotEncoder[kNumLenToPosStates][1 << kNumPosSlotBits];
   1.335 +  CLzmaProb posEncoders[kNumFullDistances - kEndPosModelIndex];
   1.336 +  CLzmaProb posAlignEncoder[1 << kNumAlignBits];
   1.337 +  
   1.338 +  CLenPriceEnc lenEnc;
   1.339 +  CLenPriceEnc repLenEnc;
   1.340 +
   1.341 +  unsigned lclp;
   1.342 +
   1.343 +  Bool fastMode;
   1.344 +  
   1.345 +  CRangeEnc rc;
   1.346 +
   1.347 +  Bool writeEndMark;
   1.348 +  UInt64 nowPos64;
   1.349 +  UInt32 matchPriceCount;
   1.350 +  Bool finished;
   1.351 +  Bool multiThread;
   1.352 +
   1.353 +  SRes result;
   1.354 +  UInt32 dictSize;
   1.355 +  UInt32 matchFinderCycles;
   1.356 +
   1.357 +  ISeqInStream *inStream;
   1.358 +  CSeqInStreamBuf seqBufInStream;
   1.359 +
   1.360 +  CSaveState saveState;
   1.361 +} CLzmaEnc;
   1.362 +
   1.363 +void LzmaEnc_SaveState(CLzmaEncHandle pp)
   1.364 +{
   1.365 +  CLzmaEnc *p = (CLzmaEnc *)pp;
   1.366 +  CSaveState *dest = &p->saveState;
   1.367 +  int i;
   1.368 +  dest->lenEnc = p->lenEnc;
   1.369 +  dest->repLenEnc = p->repLenEnc;
   1.370 +  dest->state = p->state;
   1.371 +
   1.372 +  for (i = 0; i < kNumStates; i++)
   1.373 +  {
   1.374 +    memcpy(dest->isMatch[i], p->isMatch[i], sizeof(p->isMatch[i]));
   1.375 +    memcpy(dest->isRep0Long[i], p->isRep0Long[i], sizeof(p->isRep0Long[i]));
   1.376 +  }
   1.377 +  for (i = 0; i < kNumLenToPosStates; i++)
   1.378 +    memcpy(dest->posSlotEncoder[i], p->posSlotEncoder[i], sizeof(p->posSlotEncoder[i]));
   1.379 +  memcpy(dest->isRep, p->isRep, sizeof(p->isRep));
   1.380 +  memcpy(dest->isRepG0, p->isRepG0, sizeof(p->isRepG0));
   1.381 +  memcpy(dest->isRepG1, p->isRepG1, sizeof(p->isRepG1));
   1.382 +  memcpy(dest->isRepG2, p->isRepG2, sizeof(p->isRepG2));
   1.383 +  memcpy(dest->posEncoders, p->posEncoders, sizeof(p->posEncoders));
   1.384 +  memcpy(dest->posAlignEncoder, p->posAlignEncoder, sizeof(p->posAlignEncoder));
   1.385 +  memcpy(dest->reps, p->reps, sizeof(p->reps));
   1.386 +  memcpy(dest->litProbs, p->litProbs, (0x300 << p->lclp) * sizeof(CLzmaProb));
   1.387 +}
   1.388 +
   1.389 +void LzmaEnc_RestoreState(CLzmaEncHandle pp)
   1.390 +{
   1.391 +  CLzmaEnc *dest = (CLzmaEnc *)pp;
   1.392 +  const CSaveState *p = &dest->saveState;
   1.393 +  int i;
   1.394 +  dest->lenEnc = p->lenEnc;
   1.395 +  dest->repLenEnc = p->repLenEnc;
   1.396 +  dest->state = p->state;
   1.397 +
   1.398 +  for (i = 0; i < kNumStates; i++)
   1.399 +  {
   1.400 +    memcpy(dest->isMatch[i], p->isMatch[i], sizeof(p->isMatch[i]));
   1.401 +    memcpy(dest->isRep0Long[i], p->isRep0Long[i], sizeof(p->isRep0Long[i]));
   1.402 +  }
   1.403 +  for (i = 0; i < kNumLenToPosStates; i++)
   1.404 +    memcpy(dest->posSlotEncoder[i], p->posSlotEncoder[i], sizeof(p->posSlotEncoder[i]));
   1.405 +  memcpy(dest->isRep, p->isRep, sizeof(p->isRep));
   1.406 +  memcpy(dest->isRepG0, p->isRepG0, sizeof(p->isRepG0));
   1.407 +  memcpy(dest->isRepG1, p->isRepG1, sizeof(p->isRepG1));
   1.408 +  memcpy(dest->isRepG2, p->isRepG2, sizeof(p->isRepG2));
   1.409 +  memcpy(dest->posEncoders, p->posEncoders, sizeof(p->posEncoders));
   1.410 +  memcpy(dest->posAlignEncoder, p->posAlignEncoder, sizeof(p->posAlignEncoder));
   1.411 +  memcpy(dest->reps, p->reps, sizeof(p->reps));
   1.412 +  memcpy(dest->litProbs, p->litProbs, (0x300 << dest->lclp) * sizeof(CLzmaProb));
   1.413 +}
   1.414 +
   1.415 +SRes LzmaEnc_SetProps(CLzmaEncHandle pp, const CLzmaEncProps *props2)
   1.416 +{
   1.417 +  CLzmaEnc *p = (CLzmaEnc *)pp;
   1.418 +  CLzmaEncProps props = *props2;
   1.419 +  LzmaEncProps_Normalize(&props);
   1.420 +
   1.421 +  if (props.lc > LZMA_LC_MAX || props.lp > LZMA_LP_MAX || props.pb > LZMA_PB_MAX ||
   1.422 +      props.dictSize > (1U << kDicLogSizeMaxCompress) || props.dictSize > (1U << 30))
   1.423 +    return SZ_ERROR_PARAM;
   1.424 +  p->dictSize = props.dictSize;
   1.425 +  p->matchFinderCycles = props.mc;
   1.426 +  {
   1.427 +    unsigned fb = props.fb;
   1.428 +    if (fb < 5)
   1.429 +      fb = 5;
   1.430 +    if (fb > LZMA_MATCH_LEN_MAX)
   1.431 +      fb = LZMA_MATCH_LEN_MAX;
   1.432 +    p->numFastBytes = fb;
   1.433 +  }
   1.434 +  p->lc = props.lc;
   1.435 +  p->lp = props.lp;
   1.436 +  p->pb = props.pb;
   1.437 +  p->fastMode = (props.algo == 0);
   1.438 +  p->matchFinderBase.btMode = props.btMode;
   1.439 +  {
   1.440 +    UInt32 numHashBytes = 4;
   1.441 +    if (props.btMode)
   1.442 +    {
   1.443 +      if (props.numHashBytes < 2)
   1.444 +        numHashBytes = 2;
   1.445 +      else if (props.numHashBytes < 4)
   1.446 +        numHashBytes = props.numHashBytes;
   1.447 +    }
   1.448 +    p->matchFinderBase.numHashBytes = numHashBytes;
   1.449 +  }
   1.450 +
   1.451 +  p->matchFinderBase.cutValue = props.mc;
   1.452 +
   1.453 +  p->writeEndMark = props.writeEndMark;
   1.454 +
   1.455 +  #ifdef COMPRESS_MF_MT
   1.456 +  /*
   1.457 +  if (newMultiThread != _multiThread)
   1.458 +  {
   1.459 +    ReleaseMatchFinder();
   1.460 +    _multiThread = newMultiThread;
   1.461 +  }
   1.462 +  */
   1.463 +  p->multiThread = (props.numThreads > 1);
   1.464 +  #endif
   1.465 +
   1.466 +  return SZ_OK;
   1.467 +}
   1.468 +
   1.469 +static const int kLiteralNextStates[kNumStates] = {0, 0, 0, 0, 1, 2, 3, 4,  5,  6,   4, 5};
   1.470 +static const int kMatchNextStates[kNumStates]   = {7, 7, 7, 7, 7, 7, 7, 10, 10, 10, 10, 10};
   1.471 +static const int kRepNextStates[kNumStates]     = {8, 8, 8, 8, 8, 8, 8, 11, 11, 11, 11, 11};
   1.472 +static const int kShortRepNextStates[kNumStates]= {9, 9, 9, 9, 9, 9, 9, 11, 11, 11, 11, 11};
   1.473 +
   1.474 +#define IsCharState(s) ((s) < 7)
   1.475 +
   1.476 +#define GetLenToPosState(len) (((len) < kNumLenToPosStates + 1) ? (len) - 2 : kNumLenToPosStates - 1)
   1.477 +
   1.478 +#define kInfinityPrice (1 << 30)
   1.479 +
   1.480 +static void RangeEnc_Construct(CRangeEnc *p)
   1.481 +{
   1.482 +  p->outStream = 0;
   1.483 +  p->bufBase = 0;
   1.484 +}
   1.485 +
   1.486 +#define RangeEnc_GetProcessed(p) ((p)->processed + ((p)->buf - (p)->bufBase) + (p)->cacheSize)
   1.487 +
   1.488 +#define RC_BUF_SIZE (1 << 16)
   1.489 +static int RangeEnc_Alloc(CRangeEnc *p, ISzAlloc *alloc)
   1.490 +{
   1.491 +  if (p->bufBase == 0)
   1.492 +  {
   1.493 +    p->bufBase = (Byte *)alloc->Alloc(alloc, RC_BUF_SIZE);
   1.494 +    if (p->bufBase == 0)
   1.495 +      return 0;
   1.496 +    p->bufLim = p->bufBase + RC_BUF_SIZE;
   1.497 +  }
   1.498 +  return 1;
   1.499 +}
   1.500 +
   1.501 +static void RangeEnc_Free(CRangeEnc *p, ISzAlloc *alloc)
   1.502 +{
   1.503 +  alloc->Free(alloc, p->bufBase);
   1.504 +  p->bufBase = 0;
   1.505 +}
   1.506 +
   1.507 +static void RangeEnc_Init(CRangeEnc *p)
   1.508 +{
   1.509 +  /* Stream.Init(); */
   1.510 +  p->low = 0;
   1.511 +  p->range = 0xFFFFFFFF;
   1.512 +  p->cacheSize = 1;
   1.513 +  p->cache = 0;
   1.514 +
   1.515 +  p->buf = p->bufBase;
   1.516 +
   1.517 +  p->processed = 0;
   1.518 +  p->res = SZ_OK;
   1.519 +}
   1.520 +
   1.521 +static void RangeEnc_FlushStream(CRangeEnc *p)
   1.522 +{
   1.523 +  size_t num;
   1.524 +  if (p->res != SZ_OK)
   1.525 +    return;
   1.526 +  num = p->buf - p->bufBase;
   1.527 +  if (num != p->outStream->Write(p->outStream, p->bufBase, num))
   1.528 +    p->res = SZ_ERROR_WRITE;
   1.529 +  p->processed += num;
   1.530 +  p->buf = p->bufBase;
   1.531 +}
   1.532 +
   1.533 +static void MY_FAST_CALL RangeEnc_ShiftLow(CRangeEnc *p)
   1.534 +{
   1.535 +  if ((UInt32)p->low < (UInt32)0xFF000000 || (int)(p->low >> 32) != 0)
   1.536 +  {
   1.537 +    Byte temp = p->cache;
   1.538 +    do
   1.539 +    {
   1.540 +      Byte *buf = p->buf;
   1.541 +      *buf++ = (Byte)(temp + (Byte)(p->low >> 32));
   1.542 +      p->buf = buf;
   1.543 +      if (buf == p->bufLim)
   1.544 +        RangeEnc_FlushStream(p);
   1.545 +      temp = 0xFF;
   1.546 +    }
   1.547 +    while (--p->cacheSize != 0);
   1.548 +    p->cache = (Byte)((UInt32)p->low >> 24);
   1.549 +  }
   1.550 +  p->cacheSize++;
   1.551 +  p->low = (UInt32)p->low << 8;
   1.552 +}
   1.553 +
   1.554 +static void RangeEnc_FlushData(CRangeEnc *p)
   1.555 +{
   1.556 +  int i;
   1.557 +  for (i = 0; i < 5; i++)
   1.558 +    RangeEnc_ShiftLow(p);
   1.559 +}
   1.560 +
   1.561 +static void RangeEnc_EncodeDirectBits(CRangeEnc *p, UInt32 value, int numBits)
   1.562 +{
   1.563 +  do
   1.564 +  {
   1.565 +    p->range >>= 1;
   1.566 +    p->low += p->range & (0 - ((value >> --numBits) & 1));
   1.567 +    if (p->range < kTopValue)
   1.568 +    {
   1.569 +      p->range <<= 8;
   1.570 +      RangeEnc_ShiftLow(p);
   1.571 +    }
   1.572 +  }
   1.573 +  while (numBits != 0);
   1.574 +}
   1.575 +
   1.576 +static void RangeEnc_EncodeBit(CRangeEnc *p, CLzmaProb *prob, UInt32 symbol)
   1.577 +{
   1.578 +  UInt32 ttt = *prob;
   1.579 +  UInt32 newBound = (p->range >> kNumBitModelTotalBits) * ttt;
   1.580 +  if (symbol == 0)
   1.581 +  {
   1.582 +    p->range = newBound;
   1.583 +    ttt += (kBitModelTotal - ttt) >> kNumMoveBits;
   1.584 +  }
   1.585 +  else
   1.586 +  {
   1.587 +    p->low += newBound;
   1.588 +    p->range -= newBound;
   1.589 +    ttt -= ttt >> kNumMoveBits;
   1.590 +  }
   1.591 +  *prob = (CLzmaProb)ttt;
   1.592 +  if (p->range < kTopValue)
   1.593 +  {
   1.594 +    p->range <<= 8;
   1.595 +    RangeEnc_ShiftLow(p);
   1.596 +  }
   1.597 +}
   1.598 +
   1.599 +static void LitEnc_Encode(CRangeEnc *p, CLzmaProb *probs, UInt32 symbol)
   1.600 +{
   1.601 +  symbol |= 0x100;
   1.602 +  do
   1.603 +  {
   1.604 +    RangeEnc_EncodeBit(p, probs + (symbol >> 8), (symbol >> 7) & 1);
   1.605 +    symbol <<= 1;
   1.606 +  }
   1.607 +  while (symbol < 0x10000);
   1.608 +}
   1.609 +
   1.610 +static void LitEnc_EncodeMatched(CRangeEnc *p, CLzmaProb *probs, UInt32 symbol, UInt32 matchByte)
   1.611 +{
   1.612 +  UInt32 offs = 0x100;
   1.613 +  symbol |= 0x100;
   1.614 +  do
   1.615 +  {
   1.616 +    matchByte <<= 1;
   1.617 +    RangeEnc_EncodeBit(p, probs + (offs + (matchByte & offs) + (symbol >> 8)), (symbol >> 7) & 1);
   1.618 +    symbol <<= 1;
   1.619 +    offs &= ~(matchByte ^ symbol);
   1.620 +  }
   1.621 +  while (symbol < 0x10000);
   1.622 +}
   1.623 +
   1.624 +void LzmaEnc_InitPriceTables(UInt32 *ProbPrices)
   1.625 +{
   1.626 +  UInt32 i;
   1.627 +  for (i = (1 << kNumMoveReducingBits) / 2; i < kBitModelTotal; i += (1 << kNumMoveReducingBits))
   1.628 +  {
   1.629 +    const int kCyclesBits = kNumBitPriceShiftBits;
   1.630 +    UInt32 w = i;
   1.631 +    UInt32 bitCount = 0;
   1.632 +    int j;
   1.633 +    for (j = 0; j < kCyclesBits; j++)
   1.634 +    {
   1.635 +      w = w * w;
   1.636 +      bitCount <<= 1;
   1.637 +      while (w >= ((UInt32)1 << 16))
   1.638 +      {
   1.639 +        w >>= 1;
   1.640 +        bitCount++;
   1.641 +      }
   1.642 +    }
   1.643 +    ProbPrices[i >> kNumMoveReducingBits] = ((kNumBitModelTotalBits << kCyclesBits) - 15 - bitCount);
   1.644 +  }
   1.645 +}
   1.646 +
   1.647 +
   1.648 +#define GET_PRICE(prob, symbol) \
   1.649 +  p->ProbPrices[((prob) ^ (((-(int)(symbol))) & (kBitModelTotal - 1))) >> kNumMoveReducingBits];
   1.650 +
   1.651 +#define GET_PRICEa(prob, symbol) \
   1.652 +  ProbPrices[((prob) ^ ((-((int)(symbol))) & (kBitModelTotal - 1))) >> kNumMoveReducingBits];
   1.653 +
   1.654 +#define GET_PRICE_0(prob) p->ProbPrices[(prob) >> kNumMoveReducingBits]
   1.655 +#define GET_PRICE_1(prob) p->ProbPrices[((prob) ^ (kBitModelTotal - 1)) >> kNumMoveReducingBits]
   1.656 +
   1.657 +#define GET_PRICE_0a(prob) ProbPrices[(prob) >> kNumMoveReducingBits]
   1.658 +#define GET_PRICE_1a(prob) ProbPrices[((prob) ^ (kBitModelTotal - 1)) >> kNumMoveReducingBits]
   1.659 +
   1.660 +static UInt32 LitEnc_GetPrice(const CLzmaProb *probs, UInt32 symbol, UInt32 *ProbPrices)
   1.661 +{
   1.662 +  UInt32 price = 0;
   1.663 +  symbol |= 0x100;
   1.664 +  do
   1.665 +  {
   1.666 +    price += GET_PRICEa(probs[symbol >> 8], (symbol >> 7) & 1);
   1.667 +    symbol <<= 1;
   1.668 +  }
   1.669 +  while (symbol < 0x10000);
   1.670 +  return price;
   1.671 +}
   1.672 +
   1.673 +static UInt32 LitEnc_GetPriceMatched(const CLzmaProb *probs, UInt32 symbol, UInt32 matchByte, UInt32 *ProbPrices)
   1.674 +{
   1.675 +  UInt32 price = 0;
   1.676 +  UInt32 offs = 0x100;
   1.677 +  symbol |= 0x100;
   1.678 +  do
   1.679 +  {
   1.680 +    matchByte <<= 1;
   1.681 +    price += GET_PRICEa(probs[offs + (matchByte & offs) + (symbol >> 8)], (symbol >> 7) & 1);
   1.682 +    symbol <<= 1;
   1.683 +    offs &= ~(matchByte ^ symbol);
   1.684 +  }
   1.685 +  while (symbol < 0x10000);
   1.686 +  return price;
   1.687 +}
   1.688 +
   1.689 +
   1.690 +static void RcTree_Encode(CRangeEnc *rc, CLzmaProb *probs, int numBitLevels, UInt32 symbol)
   1.691 +{
   1.692 +  UInt32 m = 1;
   1.693 +  int i;
   1.694 +  for (i = numBitLevels; i != 0;)
   1.695 +  {
   1.696 +    UInt32 bit;
   1.697 +    i--;
   1.698 +    bit = (symbol >> i) & 1;
   1.699 +    RangeEnc_EncodeBit(rc, probs + m, bit);
   1.700 +    m = (m << 1) | bit;
   1.701 +  }
   1.702 +}
   1.703 +
   1.704 +static void RcTree_ReverseEncode(CRangeEnc *rc, CLzmaProb *probs, int numBitLevels, UInt32 symbol)
   1.705 +{
   1.706 +  UInt32 m = 1;
   1.707 +  int i;
   1.708 +  for (i = 0; i < numBitLevels; i++)
   1.709 +  {
   1.710 +    UInt32 bit = symbol & 1;
   1.711 +    RangeEnc_EncodeBit(rc, probs + m, bit);
   1.712 +    m = (m << 1) | bit;
   1.713 +    symbol >>= 1;
   1.714 +  }
   1.715 +}
   1.716 +
   1.717 +static UInt32 RcTree_GetPrice(const CLzmaProb *probs, int numBitLevels, UInt32 symbol, UInt32 *ProbPrices)
   1.718 +{
   1.719 +  UInt32 price = 0;
   1.720 +  symbol |= (1 << numBitLevels);
   1.721 +  while (symbol != 1)
   1.722 +  {
   1.723 +    price += GET_PRICEa(probs[symbol >> 1], symbol & 1);
   1.724 +    symbol >>= 1;
   1.725 +  }
   1.726 +  return price;
   1.727 +}
   1.728 +
   1.729 +static UInt32 RcTree_ReverseGetPrice(const CLzmaProb *probs, int numBitLevels, UInt32 symbol, UInt32 *ProbPrices)
   1.730 +{
   1.731 +  UInt32 price = 0;
   1.732 +  UInt32 m = 1;
   1.733 +  int i;
   1.734 +  for (i = numBitLevels; i != 0; i--)
   1.735 +  {
   1.736 +    UInt32 bit = symbol & 1;
   1.737 +    symbol >>= 1;
   1.738 +    price += GET_PRICEa(probs[m], bit);
   1.739 +    m = (m << 1) | bit;
   1.740 +  }
   1.741 +  return price;
   1.742 +}
   1.743 +
   1.744 +
   1.745 +static void LenEnc_Init(CLenEnc *p)
   1.746 +{
   1.747 +  unsigned i;
   1.748 +  p->choice = p->choice2 = kProbInitValue;
   1.749 +  for (i = 0; i < (LZMA_NUM_PB_STATES_MAX << kLenNumLowBits); i++)
   1.750 +    p->low[i] = kProbInitValue;
   1.751 +  for (i = 0; i < (LZMA_NUM_PB_STATES_MAX << kLenNumMidBits); i++)
   1.752 +    p->mid[i] = kProbInitValue;
   1.753 +  for (i = 0; i < kLenNumHighSymbols; i++)
   1.754 +    p->high[i] = kProbInitValue;
   1.755 +}
   1.756 +
   1.757 +static void LenEnc_Encode(CLenEnc *p, CRangeEnc *rc, UInt32 symbol, UInt32 posState)
   1.758 +{
   1.759 +  if (symbol < kLenNumLowSymbols)
   1.760 +  {
   1.761 +    RangeEnc_EncodeBit(rc, &p->choice, 0);
   1.762 +    RcTree_Encode(rc, p->low + (posState << kLenNumLowBits), kLenNumLowBits, symbol);
   1.763 +  }
   1.764 +  else
   1.765 +  {
   1.766 +    RangeEnc_EncodeBit(rc, &p->choice, 1);
   1.767 +    if (symbol < kLenNumLowSymbols + kLenNumMidSymbols)
   1.768 +    {
   1.769 +      RangeEnc_EncodeBit(rc, &p->choice2, 0);
   1.770 +      RcTree_Encode(rc, p->mid + (posState << kLenNumMidBits), kLenNumMidBits, symbol - kLenNumLowSymbols);
   1.771 +    }
   1.772 +    else
   1.773 +    {
   1.774 +      RangeEnc_EncodeBit(rc, &p->choice2, 1);
   1.775 +      RcTree_Encode(rc, p->high, kLenNumHighBits, symbol - kLenNumLowSymbols - kLenNumMidSymbols);
   1.776 +    }
   1.777 +  }
   1.778 +}
   1.779 +
   1.780 +static void LenEnc_SetPrices(CLenEnc *p, UInt32 posState, UInt32 numSymbols, UInt32 *prices, UInt32 *ProbPrices)
   1.781 +{
   1.782 +  UInt32 a0 = GET_PRICE_0a(p->choice);
   1.783 +  UInt32 a1 = GET_PRICE_1a(p->choice);
   1.784 +  UInt32 b0 = a1 + GET_PRICE_0a(p->choice2);
   1.785 +  UInt32 b1 = a1 + GET_PRICE_1a(p->choice2);
   1.786 +  UInt32 i = 0;
   1.787 +  for (i = 0; i < kLenNumLowSymbols; i++)
   1.788 +  {
   1.789 +    if (i >= numSymbols)
   1.790 +      return;
   1.791 +    prices[i] = a0 + RcTree_GetPrice(p->low + (posState << kLenNumLowBits), kLenNumLowBits, i, ProbPrices);
   1.792 +  }
   1.793 +  for (; i < kLenNumLowSymbols + kLenNumMidSymbols; i++)
   1.794 +  {
   1.795 +    if (i >= numSymbols)
   1.796 +      return;
   1.797 +    prices[i] = b0 + RcTree_GetPrice(p->mid + (posState << kLenNumMidBits), kLenNumMidBits, i - kLenNumLowSymbols, ProbPrices);
   1.798 +  }
   1.799 +  for (; i < numSymbols; i++)
   1.800 +    prices[i] = b1 + RcTree_GetPrice(p->high, kLenNumHighBits, i - kLenNumLowSymbols - kLenNumMidSymbols, ProbPrices);
   1.801 +}
   1.802 +
   1.803 +static void MY_FAST_CALL LenPriceEnc_UpdateTable(CLenPriceEnc *p, UInt32 posState, UInt32 *ProbPrices)
   1.804 +{
   1.805 +  LenEnc_SetPrices(&p->p, posState, p->tableSize, p->prices[posState], ProbPrices);
   1.806 +  p->counters[posState] = p->tableSize;
   1.807 +}
   1.808 +
   1.809 +static void LenPriceEnc_UpdateTables(CLenPriceEnc *p, UInt32 numPosStates, UInt32 *ProbPrices)
   1.810 +{
   1.811 +  UInt32 posState;
   1.812 +  for (posState = 0; posState < numPosStates; posState++)
   1.813 +    LenPriceEnc_UpdateTable(p, posState, ProbPrices);
   1.814 +}
   1.815 +
   1.816 +static void LenEnc_Encode2(CLenPriceEnc *p, CRangeEnc *rc, UInt32 symbol, UInt32 posState, Bool updatePrice, UInt32 *ProbPrices)
   1.817 +{
   1.818 +  LenEnc_Encode(&p->p, rc, symbol, posState);
   1.819 +  if (updatePrice)
   1.820 +    if (--p->counters[posState] == 0)
   1.821 +      LenPriceEnc_UpdateTable(p, posState, ProbPrices);
   1.822 +}
   1.823 +
   1.824 +
   1.825 +
   1.826 +
   1.827 +static void MovePos(CLzmaEnc *p, UInt32 num)
   1.828 +{
   1.829 +  #ifdef SHOW_STAT
   1.830 +  ttt += num;
   1.831 +  printf("\n MovePos %d", num);
   1.832 +  #endif
   1.833 +  if (num != 0)
   1.834 +  {
   1.835 +    p->additionalOffset += num;
   1.836 +    p->matchFinder.Skip(p->matchFinderObj, num);
   1.837 +  }
   1.838 +}
   1.839 +
   1.840 +static UInt32 ReadMatchDistances(CLzmaEnc *p, UInt32 *numDistancePairsRes)
   1.841 +{
   1.842 +  UInt32 lenRes = 0, numPairs;
   1.843 +  p->numAvail = p->matchFinder.GetNumAvailableBytes(p->matchFinderObj);
   1.844 +  numPairs = p->matchFinder.GetMatches(p->matchFinderObj, p->matches);
   1.845 +  #ifdef SHOW_STAT
   1.846 +  printf("\n i = %d numPairs = %d    ", ttt, numPairs / 2);
   1.847 +  ttt++;
   1.848 +  {
   1.849 +    UInt32 i;
   1.850 +    for (i = 0; i < numPairs; i += 2)
   1.851 +      printf("%2d %6d   | ", p->matches[i], p->matches[i + 1]);
   1.852 +  }
   1.853 +  #endif
   1.854 +  if (numPairs > 0)
   1.855 +  {
   1.856 +    lenRes = p->matches[numPairs - 2];
   1.857 +    if (lenRes == p->numFastBytes)
   1.858 +    {
   1.859 +      const Byte *pby = p->matchFinder.GetPointerToCurrentPos(p->matchFinderObj) - 1;
   1.860 +      UInt32 distance = p->matches[numPairs - 1] + 1;
   1.861 +      UInt32 numAvail = p->numAvail;
   1.862 +      if (numAvail > LZMA_MATCH_LEN_MAX)
   1.863 +        numAvail = LZMA_MATCH_LEN_MAX;
   1.864 +      {
   1.865 +        const Byte *pby2 = pby - distance;
   1.866 +        for (; lenRes < numAvail && pby[lenRes] == pby2[lenRes]; lenRes++);
   1.867 +      }
   1.868 +    }
   1.869 +  }
   1.870 +  p->additionalOffset++;
   1.871 +  *numDistancePairsRes = numPairs;
   1.872 +  return lenRes;
   1.873 +}
   1.874 +
   1.875 +
   1.876 +#define MakeAsChar(p) (p)->backPrev = (UInt32)(-1); (p)->prev1IsChar = False;
   1.877 +#define MakeAsShortRep(p) (p)->backPrev = 0; (p)->prev1IsChar = False;
   1.878 +#define IsShortRep(p) ((p)->backPrev == 0)
   1.879 +
   1.880 +static UInt32 GetRepLen1Price(CLzmaEnc *p, UInt32 state, UInt32 posState)
   1.881 +{
   1.882 +  return
   1.883 +    GET_PRICE_0(p->isRepG0[state]) +
   1.884 +    GET_PRICE_0(p->isRep0Long[state][posState]);
   1.885 +}
   1.886 +
   1.887 +static UInt32 GetPureRepPrice(CLzmaEnc *p, UInt32 repIndex, UInt32 state, UInt32 posState)
   1.888 +{
   1.889 +  UInt32 price;
   1.890 +  if (repIndex == 0)
   1.891 +  {
   1.892 +    price = GET_PRICE_0(p->isRepG0[state]);
   1.893 +    price += GET_PRICE_1(p->isRep0Long[state][posState]);
   1.894 +  }
   1.895 +  else
   1.896 +  {
   1.897 +    price = GET_PRICE_1(p->isRepG0[state]);
   1.898 +    if (repIndex == 1)
   1.899 +      price += GET_PRICE_0(p->isRepG1[state]);
   1.900 +    else
   1.901 +    {
   1.902 +      price += GET_PRICE_1(p->isRepG1[state]);
   1.903 +      price += GET_PRICE(p->isRepG2[state], repIndex - 2);
   1.904 +    }
   1.905 +  }
   1.906 +  return price;
   1.907 +}
   1.908 +
   1.909 +static UInt32 GetRepPrice(CLzmaEnc *p, UInt32 repIndex, UInt32 len, UInt32 state, UInt32 posState)
   1.910 +{
   1.911 +  return p->repLenEnc.prices[posState][len - LZMA_MATCH_LEN_MIN] +
   1.912 +    GetPureRepPrice(p, repIndex, state, posState);
   1.913 +}
   1.914 +
   1.915 +static UInt32 Backward(CLzmaEnc *p, UInt32 *backRes, UInt32 cur)
   1.916 +{
   1.917 +  UInt32 posMem = p->opt[cur].posPrev;
   1.918 +  UInt32 backMem = p->opt[cur].backPrev;
   1.919 +  p->optimumEndIndex = cur;
   1.920 +  do
   1.921 +  {
   1.922 +    if (p->opt[cur].prev1IsChar)
   1.923 +    {
   1.924 +      MakeAsChar(&p->opt[posMem])
   1.925 +      p->opt[posMem].posPrev = posMem - 1;
   1.926 +      if (p->opt[cur].prev2)
   1.927 +      {
   1.928 +        p->opt[posMem - 1].prev1IsChar = False;
   1.929 +        p->opt[posMem - 1].posPrev = p->opt[cur].posPrev2;
   1.930 +        p->opt[posMem - 1].backPrev = p->opt[cur].backPrev2;
   1.931 +      }
   1.932 +    }
   1.933 +    {
   1.934 +      UInt32 posPrev = posMem;
   1.935 +      UInt32 backCur = backMem;
   1.936 +      
   1.937 +      backMem = p->opt[posPrev].backPrev;
   1.938 +      posMem = p->opt[posPrev].posPrev;
   1.939 +      
   1.940 +      p->opt[posPrev].backPrev = backCur;
   1.941 +      p->opt[posPrev].posPrev = cur;
   1.942 +      cur = posPrev;
   1.943 +    }
   1.944 +  }
   1.945 +  while (cur != 0);
   1.946 +  *backRes = p->opt[0].backPrev;
   1.947 +  p->optimumCurrentIndex  = p->opt[0].posPrev;
   1.948 +  return p->optimumCurrentIndex;
   1.949 +}
   1.950 +
   1.951 +#define LIT_PROBS(pos, prevByte) (p->litProbs + ((((pos) & p->lpMask) << p->lc) + ((prevByte) >> (8 - p->lc))) * 0x300)
   1.952 +
   1.953 +static UInt32 GetOptimum(CLzmaEnc *p, UInt32 position, UInt32 *backRes)
   1.954 +{
   1.955 +  UInt32 numAvail, mainLen, numPairs, repMaxIndex, i, posState, lenEnd, len, cur;
   1.956 +  UInt32 matchPrice, repMatchPrice, normalMatchPrice;
   1.957 +  UInt32 reps[LZMA_NUM_REPS], repLens[LZMA_NUM_REPS];
   1.958 +  UInt32 *matches;
   1.959 +  const Byte *data;
   1.960 +  Byte curByte, matchByte;
   1.961 +  if (p->optimumEndIndex != p->optimumCurrentIndex)
   1.962 +  {
   1.963 +    const COptimal *opt = &p->opt[p->optimumCurrentIndex];
   1.964 +    UInt32 lenRes = opt->posPrev - p->optimumCurrentIndex;
   1.965 +    *backRes = opt->backPrev;
   1.966 +    p->optimumCurrentIndex = opt->posPrev;
   1.967 +    return lenRes;
   1.968 +  }
   1.969 +  p->optimumCurrentIndex = p->optimumEndIndex = 0;
   1.970 +  
   1.971 +  if (p->additionalOffset == 0)
   1.972 +    mainLen = ReadMatchDistances(p, &numPairs);
   1.973 +  else
   1.974 +  {
   1.975 +    mainLen = p->longestMatchLength;
   1.976 +    numPairs = p->numPairs;
   1.977 +  }
   1.978 +
   1.979 +  numAvail = p->numAvail;
   1.980 +  if (numAvail < 2)
   1.981 +  {
   1.982 +    *backRes = (UInt32)(-1);
   1.983 +    return 1;
   1.984 +  }
   1.985 +  if (numAvail > LZMA_MATCH_LEN_MAX)
   1.986 +    numAvail = LZMA_MATCH_LEN_MAX;
   1.987 +
   1.988 +  data = p->matchFinder.GetPointerToCurrentPos(p->matchFinderObj) - 1;
   1.989 +  repMaxIndex = 0;
   1.990 +  for (i = 0; i < LZMA_NUM_REPS; i++)
   1.991 +  {
   1.992 +    UInt32 lenTest;
   1.993 +    const Byte *data2;
   1.994 +    reps[i] = p->reps[i];
   1.995 +    data2 = data - (reps[i] + 1);
   1.996 +    if (data[0] != data2[0] || data[1] != data2[1])
   1.997 +    {
   1.998 +      repLens[i] = 0;
   1.999 +      continue;
  1.1000 +    }
  1.1001 +    for (lenTest = 2; lenTest < numAvail && data[lenTest] == data2[lenTest]; lenTest++);
  1.1002 +    repLens[i] = lenTest;
  1.1003 +    if (lenTest > repLens[repMaxIndex])
  1.1004 +      repMaxIndex = i;
  1.1005 +  }
  1.1006 +  if (repLens[repMaxIndex] >= p->numFastBytes)
  1.1007 +  {
  1.1008 +    UInt32 lenRes;
  1.1009 +    *backRes = repMaxIndex;
  1.1010 +    lenRes = repLens[repMaxIndex];
  1.1011 +    MovePos(p, lenRes - 1);
  1.1012 +    return lenRes;
  1.1013 +  }
  1.1014 +
  1.1015 +  matches = p->matches;
  1.1016 +  if (mainLen >= p->numFastBytes)
  1.1017 +  {
  1.1018 +    *backRes = matches[numPairs - 1] + LZMA_NUM_REPS;
  1.1019 +    MovePos(p, mainLen - 1);
  1.1020 +    return mainLen;
  1.1021 +  }
  1.1022 +  curByte = *data;
  1.1023 +  matchByte = *(data - (reps[0] + 1));
  1.1024 +
  1.1025 +  if (mainLen < 2 && curByte != matchByte && repLens[repMaxIndex] < 2)
  1.1026 +  {
  1.1027 +    *backRes = (UInt32)-1;
  1.1028 +    return 1;
  1.1029 +  }
  1.1030 +
  1.1031 +  p->opt[0].state = (CState)p->state;
  1.1032 +
  1.1033 +  posState = (position & p->pbMask);
  1.1034 +
  1.1035 +  {
  1.1036 +    const CLzmaProb *probs = LIT_PROBS(position, *(data - 1));
  1.1037 +    p->opt[1].price = GET_PRICE_0(p->isMatch[p->state][posState]) +
  1.1038 +        (!IsCharState(p->state) ?
  1.1039 +          LitEnc_GetPriceMatched(probs, curByte, matchByte, p->ProbPrices) :
  1.1040 +          LitEnc_GetPrice(probs, curByte, p->ProbPrices));
  1.1041 +  }
  1.1042 +
  1.1043 +  MakeAsChar(&p->opt[1]);
  1.1044 +
  1.1045 +  matchPrice = GET_PRICE_1(p->isMatch[p->state][posState]);
  1.1046 +  repMatchPrice = matchPrice + GET_PRICE_1(p->isRep[p->state]);
  1.1047 +
  1.1048 +  if (matchByte == curByte)
  1.1049 +  {
  1.1050 +    UInt32 shortRepPrice = repMatchPrice + GetRepLen1Price(p, p->state, posState);
  1.1051 +    if (shortRepPrice < p->opt[1].price)
  1.1052 +    {
  1.1053 +      p->opt[1].price = shortRepPrice;
  1.1054 +      MakeAsShortRep(&p->opt[1]);
  1.1055 +    }
  1.1056 +  }
  1.1057 +  lenEnd = ((mainLen >= repLens[repMaxIndex]) ? mainLen : repLens[repMaxIndex]);
  1.1058 +
  1.1059 +  if (lenEnd < 2)
  1.1060 +  {
  1.1061 +    *backRes = p->opt[1].backPrev;
  1.1062 +    return 1;
  1.1063 +  }
  1.1064 +
  1.1065 +  p->opt[1].posPrev = 0;
  1.1066 +  for (i = 0; i < LZMA_NUM_REPS; i++)
  1.1067 +    p->opt[0].backs[i] = reps[i];
  1.1068 +
  1.1069 +  len = lenEnd;
  1.1070 +  do
  1.1071 +    p->opt[len--].price = kInfinityPrice;
  1.1072 +  while (len >= 2);
  1.1073 +
  1.1074 +  for (i = 0; i < LZMA_NUM_REPS; i++)
  1.1075 +  {
  1.1076 +    UInt32 repLen = repLens[i];
  1.1077 +    UInt32 price;
  1.1078 +    if (repLen < 2)
  1.1079 +      continue;
  1.1080 +    price = repMatchPrice + GetPureRepPrice(p, i, p->state, posState);
  1.1081 +    do
  1.1082 +    {
  1.1083 +      UInt32 curAndLenPrice = price + p->repLenEnc.prices[posState][repLen - 2];
  1.1084 +      COptimal *opt = &p->opt[repLen];
  1.1085 +      if (curAndLenPrice < opt->price)
  1.1086 +      {
  1.1087 +        opt->price = curAndLenPrice;
  1.1088 +        opt->posPrev = 0;
  1.1089 +        opt->backPrev = i;
  1.1090 +        opt->prev1IsChar = False;
  1.1091 +      }
  1.1092 +    }
  1.1093 +    while (--repLen >= 2);
  1.1094 +  }
  1.1095 +
  1.1096 +  normalMatchPrice = matchPrice + GET_PRICE_0(p->isRep[p->state]);
  1.1097 +
  1.1098 +  len = ((repLens[0] >= 2) ? repLens[0] + 1 : 2);
  1.1099 +  if (len <= mainLen)
  1.1100 +  {
  1.1101 +    UInt32 offs = 0;
  1.1102 +    while (len > matches[offs])
  1.1103 +      offs += 2;
  1.1104 +    for (; ; len++)
  1.1105 +    {
  1.1106 +      COptimal *opt;
  1.1107 +      UInt32 distance = matches[offs + 1];
  1.1108 +
  1.1109 +      UInt32 curAndLenPrice = normalMatchPrice + p->lenEnc.prices[posState][len - LZMA_MATCH_LEN_MIN];
  1.1110 +      UInt32 lenToPosState = GetLenToPosState(len);
  1.1111 +      if (distance < kNumFullDistances)
  1.1112 +        curAndLenPrice += p->distancesPrices[lenToPosState][distance];
  1.1113 +      else
  1.1114 +      {
  1.1115 +        UInt32 slot;
  1.1116 +        GetPosSlot2(distance, slot);
  1.1117 +        curAndLenPrice += p->alignPrices[distance & kAlignMask] + p->posSlotPrices[lenToPosState][slot];
  1.1118 +      }
  1.1119 +      opt = &p->opt[len];
  1.1120 +      if (curAndLenPrice < opt->price)
  1.1121 +      {
  1.1122 +        opt->price = curAndLenPrice;
  1.1123 +        opt->posPrev = 0;
  1.1124 +        opt->backPrev = distance + LZMA_NUM_REPS;
  1.1125 +        opt->prev1IsChar = False;
  1.1126 +      }
  1.1127 +      if (len == matches[offs])
  1.1128 +      {
  1.1129 +        offs += 2;
  1.1130 +        if (offs == numPairs)
  1.1131 +          break;
  1.1132 +      }
  1.1133 +    }
  1.1134 +  }
  1.1135 +
  1.1136 +  cur = 0;
  1.1137 +
  1.1138 +    #ifdef SHOW_STAT2
  1.1139 +    if (position >= 0)
  1.1140 +    {
  1.1141 +      unsigned i;
  1.1142 +      printf("\n pos = %4X", position);
  1.1143 +      for (i = cur; i <= lenEnd; i++)
  1.1144 +      printf("\nprice[%4X] = %d", position - cur + i, p->opt[i].price);
  1.1145 +    }
  1.1146 +    #endif
  1.1147 +
  1.1148 +  for (;;)
  1.1149 +  {
  1.1150 +    UInt32 numAvailFull, newLen, numPairs, posPrev, state, posState, startLen;
  1.1151 +    UInt32 curPrice, curAnd1Price, matchPrice, repMatchPrice;
  1.1152 +    Bool nextIsChar;
  1.1153 +    Byte curByte, matchByte;
  1.1154 +    const Byte *data;
  1.1155 +    COptimal *curOpt;
  1.1156 +    COptimal *nextOpt;
  1.1157 +
  1.1158 +    cur++;
  1.1159 +    if (cur == lenEnd)
  1.1160 +      return Backward(p, backRes, cur);
  1.1161 +
  1.1162 +    newLen = ReadMatchDistances(p, &numPairs);
  1.1163 +    if (newLen >= p->numFastBytes)
  1.1164 +    {
  1.1165 +      p->numPairs = numPairs;
  1.1166 +      p->longestMatchLength = newLen;
  1.1167 +      return Backward(p, backRes, cur);
  1.1168 +    }
  1.1169 +    position++;
  1.1170 +    curOpt = &p->opt[cur];
  1.1171 +    posPrev = curOpt->posPrev;
  1.1172 +    if (curOpt->prev1IsChar)
  1.1173 +    {
  1.1174 +      posPrev--;
  1.1175 +      if (curOpt->prev2)
  1.1176 +      {
  1.1177 +        state = p->opt[curOpt->posPrev2].state;
  1.1178 +        if (curOpt->backPrev2 < LZMA_NUM_REPS)
  1.1179 +          state = kRepNextStates[state];
  1.1180 +        else
  1.1181 +          state = kMatchNextStates[state];
  1.1182 +      }
  1.1183 +      else
  1.1184 +        state = p->opt[posPrev].state;
  1.1185 +      state = kLiteralNextStates[state];
  1.1186 +    }
  1.1187 +    else
  1.1188 +      state = p->opt[posPrev].state;
  1.1189 +    if (posPrev == cur - 1)
  1.1190 +    {
  1.1191 +      if (IsShortRep(curOpt))
  1.1192 +        state = kShortRepNextStates[state];
  1.1193 +      else
  1.1194 +        state = kLiteralNextStates[state];
  1.1195 +    }
  1.1196 +    else
  1.1197 +    {
  1.1198 +      UInt32 pos;
  1.1199 +      const COptimal *prevOpt;
  1.1200 +      if (curOpt->prev1IsChar && curOpt->prev2)
  1.1201 +      {
  1.1202 +        posPrev = curOpt->posPrev2;
  1.1203 +        pos = curOpt->backPrev2;
  1.1204 +        state = kRepNextStates[state];
  1.1205 +      }
  1.1206 +      else
  1.1207 +      {
  1.1208 +        pos = curOpt->backPrev;
  1.1209 +        if (pos < LZMA_NUM_REPS)
  1.1210 +          state = kRepNextStates[state];
  1.1211 +        else
  1.1212 +          state = kMatchNextStates[state];
  1.1213 +      }
  1.1214 +      prevOpt = &p->opt[posPrev];
  1.1215 +      if (pos < LZMA_NUM_REPS)
  1.1216 +      {
  1.1217 +        UInt32 i;
  1.1218 +        reps[0] = prevOpt->backs[pos];
  1.1219 +        for (i = 1; i <= pos; i++)
  1.1220 +          reps[i] = prevOpt->backs[i - 1];
  1.1221 +        for (; i < LZMA_NUM_REPS; i++)
  1.1222 +          reps[i] = prevOpt->backs[i];
  1.1223 +      }
  1.1224 +      else
  1.1225 +      {
  1.1226 +        UInt32 i;
  1.1227 +        reps[0] = (pos - LZMA_NUM_REPS);
  1.1228 +        for (i = 1; i < LZMA_NUM_REPS; i++)
  1.1229 +          reps[i] = prevOpt->backs[i - 1];
  1.1230 +      }
  1.1231 +    }
  1.1232 +    curOpt->state = (CState)state;
  1.1233 +
  1.1234 +    curOpt->backs[0] = reps[0];
  1.1235 +    curOpt->backs[1] = reps[1];
  1.1236 +    curOpt->backs[2] = reps[2];
  1.1237 +    curOpt->backs[3] = reps[3];
  1.1238 +
  1.1239 +    curPrice = curOpt->price;
  1.1240 +    nextIsChar = False;
  1.1241 +    data = p->matchFinder.GetPointerToCurrentPos(p->matchFinderObj) - 1;
  1.1242 +    curByte = *data;
  1.1243 +    matchByte = *(data - (reps[0] + 1));
  1.1244 +
  1.1245 +    posState = (position & p->pbMask);
  1.1246 +
  1.1247 +    curAnd1Price = curPrice + GET_PRICE_0(p->isMatch[state][posState]);
  1.1248 +    {
  1.1249 +      const CLzmaProb *probs = LIT_PROBS(position, *(data - 1));
  1.1250 +      curAnd1Price +=
  1.1251 +        (!IsCharState(state) ?
  1.1252 +          LitEnc_GetPriceMatched(probs, curByte, matchByte, p->ProbPrices) :
  1.1253 +          LitEnc_GetPrice(probs, curByte, p->ProbPrices));
  1.1254 +    }
  1.1255 +
  1.1256 +    nextOpt = &p->opt[cur + 1];
  1.1257 +
  1.1258 +    if (curAnd1Price < nextOpt->price)
  1.1259 +    {
  1.1260 +      nextOpt->price = curAnd1Price;
  1.1261 +      nextOpt->posPrev = cur;
  1.1262 +      MakeAsChar(nextOpt);
  1.1263 +      nextIsChar = True;
  1.1264 +    }
  1.1265 +
  1.1266 +    matchPrice = curPrice + GET_PRICE_1(p->isMatch[state][posState]);
  1.1267 +    repMatchPrice = matchPrice + GET_PRICE_1(p->isRep[state]);
  1.1268 +    
  1.1269 +    if (matchByte == curByte && !(nextOpt->posPrev < cur && nextOpt->backPrev == 0))
  1.1270 +    {
  1.1271 +      UInt32 shortRepPrice = repMatchPrice + GetRepLen1Price(p, state, posState);
  1.1272 +      if (shortRepPrice <= nextOpt->price)
  1.1273 +      {
  1.1274 +        nextOpt->price = shortRepPrice;
  1.1275 +        nextOpt->posPrev = cur;
  1.1276 +        MakeAsShortRep(nextOpt);
  1.1277 +        nextIsChar = True;
  1.1278 +      }
  1.1279 +    }
  1.1280 +    numAvailFull = p->numAvail;
  1.1281 +    {
  1.1282 +      UInt32 temp = kNumOpts - 1 - cur;
  1.1283 +      if (temp < numAvailFull)
  1.1284 +        numAvailFull = temp;
  1.1285 +    }
  1.1286 +
  1.1287 +    if (numAvailFull < 2)
  1.1288 +      continue;
  1.1289 +    numAvail = (numAvailFull <= p->numFastBytes ? numAvailFull : p->numFastBytes);
  1.1290 +
  1.1291 +    if (!nextIsChar && matchByte != curByte) /* speed optimization */
  1.1292 +    {
  1.1293 +      /* try Literal + rep0 */
  1.1294 +      UInt32 temp;
  1.1295 +      UInt32 lenTest2;
  1.1296 +      const Byte *data2 = data - (reps[0] + 1);
  1.1297 +      UInt32 limit = p->numFastBytes + 1;
  1.1298 +      if (limit > numAvailFull)
  1.1299 +        limit = numAvailFull;
  1.1300 +
  1.1301 +      for (temp = 1; temp < limit && data[temp] == data2[temp]; temp++);
  1.1302 +      lenTest2 = temp - 1;
  1.1303 +      if (lenTest2 >= 2)
  1.1304 +      {
  1.1305 +        UInt32 state2 = kLiteralNextStates[state];
  1.1306 +        UInt32 posStateNext = (position + 1) & p->pbMask;
  1.1307 +        UInt32 nextRepMatchPrice = curAnd1Price +
  1.1308 +            GET_PRICE_1(p->isMatch[state2][posStateNext]) +
  1.1309 +            GET_PRICE_1(p->isRep[state2]);
  1.1310 +        /* for (; lenTest2 >= 2; lenTest2--) */
  1.1311 +        {
  1.1312 +          UInt32 curAndLenPrice;
  1.1313 +          COptimal *opt;
  1.1314 +          UInt32 offset = cur + 1 + lenTest2;
  1.1315 +          while (lenEnd < offset)
  1.1316 +            p->opt[++lenEnd].price = kInfinityPrice;
  1.1317 +          curAndLenPrice = nextRepMatchPrice + GetRepPrice(p, 0, lenTest2, state2, posStateNext);
  1.1318 +          opt = &p->opt[offset];
  1.1319 +          if (curAndLenPrice < opt->price)
  1.1320 +          {
  1.1321 +            opt->price = curAndLenPrice;
  1.1322 +            opt->posPrev = cur + 1;
  1.1323 +            opt->backPrev = 0;
  1.1324 +            opt->prev1IsChar = True;
  1.1325 +            opt->prev2 = False;
  1.1326 +          }
  1.1327 +        }
  1.1328 +      }
  1.1329 +    }
  1.1330 +    
  1.1331 +    startLen = 2; /* speed optimization */
  1.1332 +    {
  1.1333 +    UInt32 repIndex;
  1.1334 +    for (repIndex = 0; repIndex < LZMA_NUM_REPS; repIndex++)
  1.1335 +    {
  1.1336 +      UInt32 lenTest;
  1.1337 +      UInt32 lenTestTemp;
  1.1338 +      UInt32 price;
  1.1339 +      const Byte *data2 = data - (reps[repIndex] + 1);
  1.1340 +      if (data[0] != data2[0] || data[1] != data2[1])
  1.1341 +        continue;
  1.1342 +      for (lenTest = 2; lenTest < numAvail && data[lenTest] == data2[lenTest]; lenTest++);
  1.1343 +      while (lenEnd < cur + lenTest)
  1.1344 +        p->opt[++lenEnd].price = kInfinityPrice;
  1.1345 +      lenTestTemp = lenTest;
  1.1346 +      price = repMatchPrice + GetPureRepPrice(p, repIndex, state, posState);
  1.1347 +      do
  1.1348 +      {
  1.1349 +        UInt32 curAndLenPrice = price + p->repLenEnc.prices[posState][lenTest - 2];
  1.1350 +        COptimal *opt = &p->opt[cur + lenTest];
  1.1351 +        if (curAndLenPrice < opt->price)
  1.1352 +        {
  1.1353 +          opt->price = curAndLenPrice;
  1.1354 +          opt->posPrev = cur;
  1.1355 +          opt->backPrev = repIndex;
  1.1356 +          opt->prev1IsChar = False;
  1.1357 +        }
  1.1358 +      }
  1.1359 +      while (--lenTest >= 2);
  1.1360 +      lenTest = lenTestTemp;
  1.1361 +      
  1.1362 +      if (repIndex == 0)
  1.1363 +        startLen = lenTest + 1;
  1.1364 +        
  1.1365 +      /* if (_maxMode) */
  1.1366 +        {
  1.1367 +          UInt32 lenTest2 = lenTest + 1;
  1.1368 +          UInt32 limit = lenTest2 + p->numFastBytes;
  1.1369 +          UInt32 nextRepMatchPrice;
  1.1370 +          if (limit > numAvailFull)
  1.1371 +            limit = numAvailFull;
  1.1372 +          for (; lenTest2 < limit && data[lenTest2] == data2[lenTest2]; lenTest2++);
  1.1373 +          lenTest2 -= lenTest + 1;
  1.1374 +          if (lenTest2 >= 2)
  1.1375 +          {
  1.1376 +            UInt32 state2 = kRepNextStates[state];
  1.1377 +            UInt32 posStateNext = (position + lenTest) & p->pbMask;
  1.1378 +            UInt32 curAndLenCharPrice =
  1.1379 +                price + p->repLenEnc.prices[posState][lenTest - 2] +
  1.1380 +                GET_PRICE_0(p->isMatch[state2][posStateNext]) +
  1.1381 +                LitEnc_GetPriceMatched(LIT_PROBS(position + lenTest, data[lenTest - 1]),
  1.1382 +                    data[lenTest], data2[lenTest], p->ProbPrices);
  1.1383 +            state2 = kLiteralNextStates[state2];
  1.1384 +            posStateNext = (position + lenTest + 1) & p->pbMask;
  1.1385 +            nextRepMatchPrice = curAndLenCharPrice +
  1.1386 +                GET_PRICE_1(p->isMatch[state2][posStateNext]) +
  1.1387 +                GET_PRICE_1(p->isRep[state2]);
  1.1388 +            
  1.1389 +            /* for (; lenTest2 >= 2; lenTest2--) */
  1.1390 +            {
  1.1391 +              UInt32 curAndLenPrice;
  1.1392 +              COptimal *opt;
  1.1393 +              UInt32 offset = cur + lenTest + 1 + lenTest2;
  1.1394 +              while (lenEnd < offset)
  1.1395 +                p->opt[++lenEnd].price = kInfinityPrice;
  1.1396 +              curAndLenPrice = nextRepMatchPrice + GetRepPrice(p, 0, lenTest2, state2, posStateNext);
  1.1397 +              opt = &p->opt[offset];
  1.1398 +              if (curAndLenPrice < opt->price)
  1.1399 +              {
  1.1400 +                opt->price = curAndLenPrice;
  1.1401 +                opt->posPrev = cur + lenTest + 1;
  1.1402 +                opt->backPrev = 0;
  1.1403 +                opt->prev1IsChar = True;
  1.1404 +                opt->prev2 = True;
  1.1405 +                opt->posPrev2 = cur;
  1.1406 +                opt->backPrev2 = repIndex;
  1.1407 +              }
  1.1408 +            }
  1.1409 +          }
  1.1410 +        }
  1.1411 +    }
  1.1412 +    }
  1.1413 +    /* for (UInt32 lenTest = 2; lenTest <= newLen; lenTest++) */
  1.1414 +    if (newLen > numAvail)
  1.1415 +    {
  1.1416 +      newLen = numAvail;
  1.1417 +      for (numPairs = 0; newLen > matches[numPairs]; numPairs += 2);
  1.1418 +      matches[numPairs] = newLen;
  1.1419 +      numPairs += 2;
  1.1420 +    }
  1.1421 +    if (newLen >= startLen)
  1.1422 +    {
  1.1423 +      UInt32 normalMatchPrice = matchPrice + GET_PRICE_0(p->isRep[state]);
  1.1424 +      UInt32 offs, curBack, posSlot;
  1.1425 +      UInt32 lenTest;
  1.1426 +      while (lenEnd < cur + newLen)
  1.1427 +        p->opt[++lenEnd].price = kInfinityPrice;
  1.1428 +
  1.1429 +      offs = 0;
  1.1430 +      while (startLen > matches[offs])
  1.1431 +        offs += 2;
  1.1432 +      curBack = matches[offs + 1];
  1.1433 +      GetPosSlot2(curBack, posSlot);
  1.1434 +      for (lenTest = /*2*/ startLen; ; lenTest++)
  1.1435 +      {
  1.1436 +        UInt32 curAndLenPrice = normalMatchPrice + p->lenEnc.prices[posState][lenTest - LZMA_MATCH_LEN_MIN];
  1.1437 +        UInt32 lenToPosState = GetLenToPosState(lenTest);
  1.1438 +        COptimal *opt;
  1.1439 +        if (curBack < kNumFullDistances)
  1.1440 +          curAndLenPrice += p->distancesPrices[lenToPosState][curBack];
  1.1441 +        else
  1.1442 +          curAndLenPrice += p->posSlotPrices[lenToPosState][posSlot] + p->alignPrices[curBack & kAlignMask];
  1.1443 +        
  1.1444 +        opt = &p->opt[cur + lenTest];
  1.1445 +        if (curAndLenPrice < opt->price)
  1.1446 +        {
  1.1447 +          opt->price = curAndLenPrice;
  1.1448 +          opt->posPrev = cur;
  1.1449 +          opt->backPrev = curBack + LZMA_NUM_REPS;
  1.1450 +          opt->prev1IsChar = False;
  1.1451 +        }
  1.1452 +
  1.1453 +        if (/*_maxMode && */lenTest == matches[offs])
  1.1454 +        {
  1.1455 +          /* Try Match + Literal + Rep0 */
  1.1456 +          const Byte *data2 = data - (curBack + 1);
  1.1457 +          UInt32 lenTest2 = lenTest + 1;
  1.1458 +          UInt32 limit = lenTest2 + p->numFastBytes;
  1.1459 +          UInt32 nextRepMatchPrice;
  1.1460 +          if (limit > numAvailFull)
  1.1461 +            limit = numAvailFull;
  1.1462 +          for (; lenTest2 < limit && data[lenTest2] == data2[lenTest2]; lenTest2++);
  1.1463 +          lenTest2 -= lenTest + 1;
  1.1464 +          if (lenTest2 >= 2)
  1.1465 +          {
  1.1466 +            UInt32 state2 = kMatchNextStates[state];
  1.1467 +            UInt32 posStateNext = (position + lenTest) & p->pbMask;
  1.1468 +            UInt32 curAndLenCharPrice = curAndLenPrice +
  1.1469 +                GET_PRICE_0(p->isMatch[state2][posStateNext]) +
  1.1470 +                LitEnc_GetPriceMatched(LIT_PROBS(position + lenTest, data[lenTest - 1]),
  1.1471 +                    data[lenTest], data2[lenTest], p->ProbPrices);
  1.1472 +            state2 = kLiteralNextStates[state2];
  1.1473 +            posStateNext = (posStateNext + 1) & p->pbMask;
  1.1474 +            nextRepMatchPrice = curAndLenCharPrice +
  1.1475 +                GET_PRICE_1(p->isMatch[state2][posStateNext]) +
  1.1476 +                GET_PRICE_1(p->isRep[state2]);
  1.1477 +            
  1.1478 +            /* for (; lenTest2 >= 2; lenTest2--) */
  1.1479 +            {
  1.1480 +              UInt32 offset = cur + lenTest + 1 + lenTest2;
  1.1481 +              UInt32 curAndLenPrice;
  1.1482 +              COptimal *opt;
  1.1483 +              while (lenEnd < offset)
  1.1484 +                p->opt[++lenEnd].price = kInfinityPrice;
  1.1485 +              curAndLenPrice = nextRepMatchPrice + GetRepPrice(p, 0, lenTest2, state2, posStateNext);
  1.1486 +              opt = &p->opt[offset];
  1.1487 +              if (curAndLenPrice < opt->price)
  1.1488 +              {
  1.1489 +                opt->price = curAndLenPrice;
  1.1490 +                opt->posPrev = cur + lenTest + 1;
  1.1491 +                opt->backPrev = 0;
  1.1492 +                opt->prev1IsChar = True;
  1.1493 +                opt->prev2 = True;
  1.1494 +                opt->posPrev2 = cur;
  1.1495 +                opt->backPrev2 = curBack + LZMA_NUM_REPS;
  1.1496 +              }
  1.1497 +            }
  1.1498 +          }
  1.1499 +          offs += 2;
  1.1500 +          if (offs == numPairs)
  1.1501 +            break;
  1.1502 +          curBack = matches[offs + 1];
  1.1503 +          if (curBack >= kNumFullDistances)
  1.1504 +            GetPosSlot2(curBack, posSlot);
  1.1505 +        }
  1.1506 +      }
  1.1507 +    }
  1.1508 +  }
  1.1509 +}
  1.1510 +
  1.1511 +#define ChangePair(smallDist, bigDist) (((bigDist) >> 7) > (smallDist))
  1.1512 +
  1.1513 +static UInt32 GetOptimumFast(CLzmaEnc *p, UInt32 *backRes)
  1.1514 +{
  1.1515 +  UInt32 numAvail, mainLen, mainDist, numPairs, repIndex, repLen, i;
  1.1516 +  const Byte *data;
  1.1517 +  const UInt32 *matches;
  1.1518 +
  1.1519 +  if (p->additionalOffset == 0)
  1.1520 +    mainLen = ReadMatchDistances(p, &numPairs);
  1.1521 +  else
  1.1522 +  {
  1.1523 +    mainLen = p->longestMatchLength;
  1.1524 +    numPairs = p->numPairs;
  1.1525 +  }
  1.1526 +
  1.1527 +  numAvail = p->numAvail;
  1.1528 +  *backRes = (UInt32)-1;
  1.1529 +  if (numAvail < 2)
  1.1530 +    return 1;
  1.1531 +  if (numAvail > LZMA_MATCH_LEN_MAX)
  1.1532 +    numAvail = LZMA_MATCH_LEN_MAX;
  1.1533 +  data = p->matchFinder.GetPointerToCurrentPos(p->matchFinderObj) - 1;
  1.1534 +
  1.1535 +  repLen = repIndex = 0;
  1.1536 +  for (i = 0; i < LZMA_NUM_REPS; i++)
  1.1537 +  {
  1.1538 +    UInt32 len;
  1.1539 +    const Byte *data2 = data - (p->reps[i] + 1);
  1.1540 +    if (data[0] != data2[0] || data[1] != data2[1])
  1.1541 +      continue;
  1.1542 +    for (len = 2; len < numAvail && data[len] == data2[len]; len++);
  1.1543 +    if (len >= p->numFastBytes)
  1.1544 +    {
  1.1545 +      *backRes = i;
  1.1546 +      MovePos(p, len - 1);
  1.1547 +      return len;
  1.1548 +    }
  1.1549 +    if (len > repLen)
  1.1550 +    {
  1.1551 +      repIndex = i;
  1.1552 +      repLen = len;
  1.1553 +    }
  1.1554 +  }
  1.1555 +
  1.1556 +  matches = p->matches;
  1.1557 +  if (mainLen >= p->numFastBytes)
  1.1558 +  {
  1.1559 +    *backRes = matches[numPairs - 1] + LZMA_NUM_REPS;
  1.1560 +    MovePos(p, mainLen - 1);
  1.1561 +    return mainLen;
  1.1562 +  }
  1.1563 +
  1.1564 +  mainDist = 0; /* for GCC */
  1.1565 +  if (mainLen >= 2)
  1.1566 +  {
  1.1567 +    mainDist = matches[numPairs - 1];
  1.1568 +    while (numPairs > 2 && mainLen == matches[numPairs - 4] + 1)
  1.1569 +    {
  1.1570 +      if (!ChangePair(matches[numPairs - 3], mainDist))
  1.1571 +        break;
  1.1572 +      numPairs -= 2;
  1.1573 +      mainLen = matches[numPairs - 2];
  1.1574 +      mainDist = matches[numPairs - 1];
  1.1575 +    }
  1.1576 +    if (mainLen == 2 && mainDist >= 0x80)
  1.1577 +      mainLen = 1;
  1.1578 +  }
  1.1579 +
  1.1580 +  if (repLen >= 2 && (
  1.1581 +        (repLen + 1 >= mainLen) ||
  1.1582 +        (repLen + 2 >= mainLen && mainDist >= (1 << 9)) ||
  1.1583 +        (repLen + 3 >= mainLen && mainDist >= (1 << 15))))
  1.1584 +  {
  1.1585 +    *backRes = repIndex;
  1.1586 +    MovePos(p, repLen - 1);
  1.1587 +    return repLen;
  1.1588 +  }
  1.1589 +  
  1.1590 +  if (mainLen < 2 || numAvail <= 2)
  1.1591 +    return 1;
  1.1592 +
  1.1593 +  p->longestMatchLength = ReadMatchDistances(p, &p->numPairs);
  1.1594 +  if (p->longestMatchLength >= 2)
  1.1595 +  {
  1.1596 +    UInt32 newDistance = matches[p->numPairs - 1];
  1.1597 +    if ((p->longestMatchLength >= mainLen && newDistance < mainDist) ||
  1.1598 +        (p->longestMatchLength == mainLen + 1 && !ChangePair(mainDist, newDistance)) ||
  1.1599 +        (p->longestMatchLength > mainLen + 1) ||
  1.1600 +        (p->longestMatchLength + 1 >= mainLen && mainLen >= 3 && ChangePair(newDistance, mainDist)))
  1.1601 +      return 1;
  1.1602 +  }
  1.1603 +  
  1.1604 +  data = p->matchFinder.GetPointerToCurrentPos(p->matchFinderObj) - 1;
  1.1605 +  for (i = 0; i < LZMA_NUM_REPS; i++)
  1.1606 +  {
  1.1607 +    UInt32 len, limit;
  1.1608 +    const Byte *data2 = data - (p->reps[i] + 1);
  1.1609 +    if (data[0] != data2[0] || data[1] != data2[1])
  1.1610 +      continue;
  1.1611 +    limit = mainLen - 1;
  1.1612 +    for (len = 2; len < limit && data[len] == data2[len]; len++);
  1.1613 +    if (len >= limit)
  1.1614 +      return 1;
  1.1615 +  }
  1.1616 +  *backRes = mainDist + LZMA_NUM_REPS;
  1.1617 +  MovePos(p, mainLen - 2);
  1.1618 +  return mainLen;
  1.1619 +}
  1.1620 +
  1.1621 +static void WriteEndMarker(CLzmaEnc *p, UInt32 posState)
  1.1622 +{
  1.1623 +  UInt32 len;
  1.1624 +  RangeEnc_EncodeBit(&p->rc, &p->isMatch[p->state][posState], 1);
  1.1625 +  RangeEnc_EncodeBit(&p->rc, &p->isRep[p->state], 0);
  1.1626 +  p->state = kMatchNextStates[p->state];
  1.1627 +  len = LZMA_MATCH_LEN_MIN;
  1.1628 +  LenEnc_Encode2(&p->lenEnc, &p->rc, len - LZMA_MATCH_LEN_MIN, posState, !p->fastMode, p->ProbPrices);
  1.1629 +  RcTree_Encode(&p->rc, p->posSlotEncoder[GetLenToPosState(len)], kNumPosSlotBits, (1 << kNumPosSlotBits) - 1);
  1.1630 +  RangeEnc_EncodeDirectBits(&p->rc, (((UInt32)1 << 30) - 1) >> kNumAlignBits, 30 - kNumAlignBits);
  1.1631 +  RcTree_ReverseEncode(&p->rc, p->posAlignEncoder, kNumAlignBits, kAlignMask);
  1.1632 +}
  1.1633 +
  1.1634 +static SRes CheckErrors(CLzmaEnc *p)
  1.1635 +{
  1.1636 +  if (p->result != SZ_OK)
  1.1637 +    return p->result;
  1.1638 +  if (p->rc.res != SZ_OK)
  1.1639 +    p->result = SZ_ERROR_WRITE;
  1.1640 +  if (p->matchFinderBase.result != SZ_OK)
  1.1641 +    p->result = SZ_ERROR_READ;
  1.1642 +  if (p->result != SZ_OK)
  1.1643 +    p->finished = True;
  1.1644 +  return p->result;
  1.1645 +}
  1.1646 +
  1.1647 +static SRes Flush(CLzmaEnc *p, UInt32 nowPos)
  1.1648 +{
  1.1649 +  /* ReleaseMFStream(); */
  1.1650 +  p->finished = True;
  1.1651 +  if (p->writeEndMark)
  1.1652 +    WriteEndMarker(p, nowPos & p->pbMask);
  1.1653 +  RangeEnc_FlushData(&p->rc);
  1.1654 +  RangeEnc_FlushStream(&p->rc);
  1.1655 +  return CheckErrors(p);
  1.1656 +}
  1.1657 +
  1.1658 +static void FillAlignPrices(CLzmaEnc *p)
  1.1659 +{
  1.1660 +  UInt32 i;
  1.1661 +  for (i = 0; i < kAlignTableSize; i++)
  1.1662 +    p->alignPrices[i] = RcTree_ReverseGetPrice(p->posAlignEncoder, kNumAlignBits, i, p->ProbPrices);
  1.1663 +  p->alignPriceCount = 0;
  1.1664 +}
  1.1665 +
  1.1666 +static void FillDistancesPrices(CLzmaEnc *p)
  1.1667 +{
  1.1668 +  UInt32 tempPrices[kNumFullDistances];
  1.1669 +  UInt32 i, lenToPosState;
  1.1670 +  for (i = kStartPosModelIndex; i < kNumFullDistances; i++)
  1.1671 +  {
  1.1672 +    UInt32 posSlot = GetPosSlot1(i);
  1.1673 +    UInt32 footerBits = ((posSlot >> 1) - 1);
  1.1674 +    UInt32 base = ((2 | (posSlot & 1)) << footerBits);
  1.1675 +    tempPrices[i] = RcTree_ReverseGetPrice(p->posEncoders + base - posSlot - 1, footerBits, i - base, p->ProbPrices);
  1.1676 +  }
  1.1677 +
  1.1678 +  for (lenToPosState = 0; lenToPosState < kNumLenToPosStates; lenToPosState++)
  1.1679 +  {
  1.1680 +    UInt32 posSlot;
  1.1681 +    const CLzmaProb *encoder = p->posSlotEncoder[lenToPosState];
  1.1682 +    UInt32 *posSlotPrices = p->posSlotPrices[lenToPosState];
  1.1683 +    for (posSlot = 0; posSlot < p->distTableSize; posSlot++)
  1.1684 +      posSlotPrices[posSlot] = RcTree_GetPrice(encoder, kNumPosSlotBits, posSlot, p->ProbPrices);
  1.1685 +    for (posSlot = kEndPosModelIndex; posSlot < p->distTableSize; posSlot++)
  1.1686 +      posSlotPrices[posSlot] += ((((posSlot >> 1) - 1) - kNumAlignBits) << kNumBitPriceShiftBits);
  1.1687 +
  1.1688 +    {
  1.1689 +      UInt32 *distancesPrices = p->distancesPrices[lenToPosState];
  1.1690 +      UInt32 i;
  1.1691 +      for (i = 0; i < kStartPosModelIndex; i++)
  1.1692 +        distancesPrices[i] = posSlotPrices[i];
  1.1693 +      for (; i < kNumFullDistances; i++)
  1.1694 +        distancesPrices[i] = posSlotPrices[GetPosSlot1(i)] + tempPrices[i];
  1.1695 +    }
  1.1696 +  }
  1.1697 +  p->matchPriceCount = 0;
  1.1698 +}
  1.1699 +
  1.1700 +void LzmaEnc_Construct(CLzmaEnc *p)
  1.1701 +{
  1.1702 +  RangeEnc_Construct(&p->rc);
  1.1703 +  MatchFinder_Construct(&p->matchFinderBase);
  1.1704 +  #ifdef COMPRESS_MF_MT
  1.1705 +  MatchFinderMt_Construct(&p->matchFinderMt);
  1.1706 +  p->matchFinderMt.MatchFinder = &p->matchFinderBase;
  1.1707 +  #endif
  1.1708 +
  1.1709 +  {
  1.1710 +    CLzmaEncProps props;
  1.1711 +    LzmaEncProps_Init(&props);
  1.1712 +    LzmaEnc_SetProps(p, &props);
  1.1713 +  }
  1.1714 +
  1.1715 +  #ifndef LZMA_LOG_BSR
  1.1716 +  LzmaEnc_FastPosInit(p->g_FastPos);
  1.1717 +  #endif
  1.1718 +
  1.1719 +  LzmaEnc_InitPriceTables(p->ProbPrices);
  1.1720 +  p->litProbs = 0;
  1.1721 +  p->saveState.litProbs = 0;
  1.1722 +}
  1.1723 +
  1.1724 +CLzmaEncHandle LzmaEnc_Create(ISzAlloc *alloc)
  1.1725 +{
  1.1726 +  void *p;
  1.1727 +  p = alloc->Alloc(alloc, sizeof(CLzmaEnc));
  1.1728 +  if (p != 0)
  1.1729 +    LzmaEnc_Construct((CLzmaEnc *)p);
  1.1730 +  return p;
  1.1731 +}
  1.1732 +
  1.1733 +void LzmaEnc_FreeLits(CLzmaEnc *p, ISzAlloc *alloc)
  1.1734 +{
  1.1735 +  alloc->Free(alloc, p->litProbs);
  1.1736 +  alloc->Free(alloc, p->saveState.litProbs);
  1.1737 +  p->litProbs = 0;
  1.1738 +  p->saveState.litProbs = 0;
  1.1739 +}
  1.1740 +
  1.1741 +void LzmaEnc_Destruct(CLzmaEnc *p, ISzAlloc *alloc, ISzAlloc *allocBig)
  1.1742 +{
  1.1743 +  #ifdef COMPRESS_MF_MT
  1.1744 +  MatchFinderMt_Destruct(&p->matchFinderMt, allocBig);
  1.1745 +  #endif
  1.1746 +  MatchFinder_Free(&p->matchFinderBase, allocBig);
  1.1747 +  LzmaEnc_FreeLits(p, alloc);
  1.1748 +  RangeEnc_Free(&p->rc, alloc);
  1.1749 +}
  1.1750 +
  1.1751 +void LzmaEnc_Destroy(CLzmaEncHandle p, ISzAlloc *alloc, ISzAlloc *allocBig)
  1.1752 +{
  1.1753 +  LzmaEnc_Destruct((CLzmaEnc *)p, alloc, allocBig);
  1.1754 +  alloc->Free(alloc, p);
  1.1755 +}
  1.1756 +
  1.1757 +static SRes LzmaEnc_CodeOneBlock(CLzmaEnc *p, Bool useLimits, UInt32 maxPackSize, UInt32 maxUnpackSize)
  1.1758 +{
  1.1759 +  UInt32 nowPos32, startPos32;
  1.1760 +  if (p->inStream != 0)
  1.1761 +  {
  1.1762 +    p->matchFinderBase.stream = p->inStream;
  1.1763 +    p->matchFinder.Init(p->matchFinderObj);
  1.1764 +    p->inStream = 0;
  1.1765 +  }
  1.1766 +
  1.1767 +  if (p->finished)
  1.1768 +    return p->result;
  1.1769 +  RINOK(CheckErrors(p));
  1.1770 +
  1.1771 +  nowPos32 = (UInt32)p->nowPos64;
  1.1772 +  startPos32 = nowPos32;
  1.1773 +
  1.1774 +  if (p->nowPos64 == 0)
  1.1775 +  {
  1.1776 +    UInt32 numPairs;
  1.1777 +    Byte curByte;
  1.1778 +    if (p->matchFinder.GetNumAvailableBytes(p->matchFinderObj) == 0)
  1.1779 +      return Flush(p, nowPos32);
  1.1780 +    ReadMatchDistances(p, &numPairs);
  1.1781 +    RangeEnc_EncodeBit(&p->rc, &p->isMatch[p->state][0], 0);
  1.1782 +    p->state = kLiteralNextStates[p->state];
  1.1783 +    curByte = p->matchFinder.GetIndexByte(p->matchFinderObj, 0 - p->additionalOffset);
  1.1784 +    LitEnc_Encode(&p->rc, p->litProbs, curByte);
  1.1785 +    p->additionalOffset--;
  1.1786 +    nowPos32++;
  1.1787 +  }
  1.1788 +
  1.1789 +  if (p->matchFinder.GetNumAvailableBytes(p->matchFinderObj) != 0)
  1.1790 +  for (;;)
  1.1791 +  {
  1.1792 +    UInt32 pos, len, posState;
  1.1793 +
  1.1794 +    if (p->fastMode)
  1.1795 +      len = GetOptimumFast(p, &pos);
  1.1796 +    else
  1.1797 +      len = GetOptimum(p, nowPos32, &pos);
  1.1798 +
  1.1799 +    #ifdef SHOW_STAT2
  1.1800 +    printf("\n pos = %4X,   len = %d   pos = %d", nowPos32, len, pos);
  1.1801 +    #endif
  1.1802 +
  1.1803 +    posState = nowPos32 & p->pbMask;
  1.1804 +    if (len == 1 && pos == (UInt32)-1)
  1.1805 +    {
  1.1806 +      Byte curByte;
  1.1807 +      CLzmaProb *probs;
  1.1808 +      const Byte *data;
  1.1809 +
  1.1810 +      RangeEnc_EncodeBit(&p->rc, &p->isMatch[p->state][posState], 0);
  1.1811 +      data = p->matchFinder.GetPointerToCurrentPos(p->matchFinderObj) - p->additionalOffset;
  1.1812 +      curByte = *data;
  1.1813 +      probs = LIT_PROBS(nowPos32, *(data - 1));
  1.1814 +      if (IsCharState(p->state))
  1.1815 +        LitEnc_Encode(&p->rc, probs, curByte);
  1.1816 +      else
  1.1817 +        LitEnc_EncodeMatched(&p->rc, probs, curByte, *(data - p->reps[0] - 1));
  1.1818 +      p->state = kLiteralNextStates[p->state];
  1.1819 +    }
  1.1820 +    else
  1.1821 +    {
  1.1822 +      RangeEnc_EncodeBit(&p->rc, &p->isMatch[p->state][posState], 1);
  1.1823 +      if (pos < LZMA_NUM_REPS)
  1.1824 +      {
  1.1825 +        RangeEnc_EncodeBit(&p->rc, &p->isRep[p->state], 1);
  1.1826 +        if (pos == 0)
  1.1827 +        {
  1.1828 +          RangeEnc_EncodeBit(&p->rc, &p->isRepG0[p->state], 0);
  1.1829 +          RangeEnc_EncodeBit(&p->rc, &p->isRep0Long[p->state][posState], ((len == 1) ? 0 : 1));
  1.1830 +        }
  1.1831 +        else
  1.1832 +        {
  1.1833 +          UInt32 distance = p->reps[pos];
  1.1834 +          RangeEnc_EncodeBit(&p->rc, &p->isRepG0[p->state], 1);
  1.1835 +          if (pos == 1)
  1.1836 +            RangeEnc_EncodeBit(&p->rc, &p->isRepG1[p->state], 0);
  1.1837 +          else
  1.1838 +          {
  1.1839 +            RangeEnc_EncodeBit(&p->rc, &p->isRepG1[p->state], 1);
  1.1840 +            RangeEnc_EncodeBit(&p->rc, &p->isRepG2[p->state], pos - 2);
  1.1841 +            if (pos == 3)
  1.1842 +              p->reps[3] = p->reps[2];
  1.1843 +            p->reps[2] = p->reps[1];
  1.1844 +          }
  1.1845 +          p->reps[1] = p->reps[0];
  1.1846 +          p->reps[0] = distance;
  1.1847 +        }
  1.1848 +        if (len == 1)
  1.1849 +          p->state = kShortRepNextStates[p->state];
  1.1850 +        else
  1.1851 +        {
  1.1852 +          LenEnc_Encode2(&p->repLenEnc, &p->rc, len - LZMA_MATCH_LEN_MIN, posState, !p->fastMode, p->ProbPrices);
  1.1853 +          p->state = kRepNextStates[p->state];
  1.1854 +        }
  1.1855 +      }
  1.1856 +      else
  1.1857 +      {
  1.1858 +        UInt32 posSlot;
  1.1859 +        RangeEnc_EncodeBit(&p->rc, &p->isRep[p->state], 0);
  1.1860 +        p->state = kMatchNextStates[p->state];
  1.1861 +        LenEnc_Encode2(&p->lenEnc, &p->rc, len - LZMA_MATCH_LEN_MIN, posState, !p->fastMode, p->ProbPrices);
  1.1862 +        pos -= LZMA_NUM_REPS;
  1.1863 +        GetPosSlot(pos, posSlot);
  1.1864 +        RcTree_Encode(&p->rc, p->posSlotEncoder[GetLenToPosState(len)], kNumPosSlotBits, posSlot);
  1.1865 +        
  1.1866 +        if (posSlot >= kStartPosModelIndex)
  1.1867 +        {
  1.1868 +          UInt32 footerBits = ((posSlot >> 1) - 1);
  1.1869 +          UInt32 base = ((2 | (posSlot & 1)) << footerBits);
  1.1870 +          UInt32 posReduced = pos - base;
  1.1871 +
  1.1872 +          if (posSlot < kEndPosModelIndex)
  1.1873 +            RcTree_ReverseEncode(&p->rc, p->posEncoders + base - posSlot - 1, footerBits, posReduced);
  1.1874 +          else
  1.1875 +          {
  1.1876 +            RangeEnc_EncodeDirectBits(&p->rc, posReduced >> kNumAlignBits, footerBits - kNumAlignBits);
  1.1877 +            RcTree_ReverseEncode(&p->rc, p->posAlignEncoder, kNumAlignBits, posReduced & kAlignMask);
  1.1878 +            p->alignPriceCount++;
  1.1879 +          }
  1.1880 +        }
  1.1881 +        p->reps[3] = p->reps[2];
  1.1882 +        p->reps[2] = p->reps[1];
  1.1883 +        p->reps[1] = p->reps[0];
  1.1884 +        p->reps[0] = pos;
  1.1885 +        p->matchPriceCount++;
  1.1886 +      }
  1.1887 +    }
  1.1888 +    p->additionalOffset -= len;
  1.1889 +    nowPos32 += len;
  1.1890 +    if (p->additionalOffset == 0)
  1.1891 +    {
  1.1892 +      UInt32 processed;
  1.1893 +      if (!p->fastMode)
  1.1894 +      {
  1.1895 +        if (p->matchPriceCount >= (1 << 7))
  1.1896 +          FillDistancesPrices(p);
  1.1897 +        if (p->alignPriceCount >= kAlignTableSize)
  1.1898 +          FillAlignPrices(p);
  1.1899 +      }
  1.1900 +      if (p->matchFinder.GetNumAvailableBytes(p->matchFinderObj) == 0)
  1.1901 +        break;
  1.1902 +      processed = nowPos32 - startPos32;
  1.1903 +      if (useLimits)
  1.1904 +      {
  1.1905 +        if (processed + kNumOpts + 300 >= maxUnpackSize ||
  1.1906 +            RangeEnc_GetProcessed(&p->rc) + kNumOpts * 2 >= maxPackSize)
  1.1907 +          break;
  1.1908 +      }
  1.1909 +      else if (processed >= (1 << 15))
  1.1910 +      {
  1.1911 +        p->nowPos64 += nowPos32 - startPos32;
  1.1912 +        return CheckErrors(p);
  1.1913 +      }
  1.1914 +    }
  1.1915 +  }
  1.1916 +  p->nowPos64 += nowPos32 - startPos32;
  1.1917 +  return Flush(p, nowPos32);
  1.1918 +}
  1.1919 +
  1.1920 +#define kBigHashDicLimit ((UInt32)1 << 24)
  1.1921 +
  1.1922 +static SRes LzmaEnc_Alloc(CLzmaEnc *p, UInt32 keepWindowSize, ISzAlloc *alloc, ISzAlloc *allocBig)
  1.1923 +{
  1.1924 +  UInt32 beforeSize = kNumOpts;
  1.1925 +  Bool btMode;
  1.1926 +  if (!RangeEnc_Alloc(&p->rc, alloc))
  1.1927 +    return SZ_ERROR_MEM;
  1.1928 +  btMode = (p->matchFinderBase.btMode != 0);
  1.1929 +  #ifdef COMPRESS_MF_MT
  1.1930 +  p->mtMode = (p->multiThread && !p->fastMode && btMode);
  1.1931 +  #endif
  1.1932 +
  1.1933 +  {
  1.1934 +    unsigned lclp = p->lc + p->lp;
  1.1935 +    if (p->litProbs == 0 || p->saveState.litProbs == 0 || p->lclp != lclp)
  1.1936 +    {
  1.1937 +      LzmaEnc_FreeLits(p, alloc);
  1.1938 +      p->litProbs = (CLzmaProb *)alloc->Alloc(alloc, (0x300 << lclp) * sizeof(CLzmaProb));
  1.1939 +      p->saveState.litProbs = (CLzmaProb *)alloc->Alloc(alloc, (0x300 << lclp) * sizeof(CLzmaProb));
  1.1940 +      if (p->litProbs == 0 || p->saveState.litProbs == 0)
  1.1941 +      {
  1.1942 +        LzmaEnc_FreeLits(p, alloc);
  1.1943 +        return SZ_ERROR_MEM;
  1.1944 +      }
  1.1945 +      p->lclp = lclp;
  1.1946 +    }
  1.1947 +  }
  1.1948 +
  1.1949 +  p->matchFinderBase.bigHash = (p->dictSize > kBigHashDicLimit);
  1.1950 +
  1.1951 +  if (beforeSize + p->dictSize < keepWindowSize)
  1.1952 +    beforeSize = keepWindowSize - p->dictSize;
  1.1953 +
  1.1954 +  #ifdef COMPRESS_MF_MT
  1.1955 +  if (p->mtMode)
  1.1956 +  {
  1.1957 +    RINOK(MatchFinderMt_Create(&p->matchFinderMt, p->dictSize, beforeSize, p->numFastBytes, LZMA_MATCH_LEN_MAX, allocBig));
  1.1958 +    p->matchFinderObj = &p->matchFinderMt;
  1.1959 +    MatchFinderMt_CreateVTable(&p->matchFinderMt, &p->matchFinder);
  1.1960 +  }
  1.1961 +  else
  1.1962 +  #endif
  1.1963 +  {
  1.1964 +    if (!MatchFinder_Create(&p->matchFinderBase, p->dictSize, beforeSize, p->numFastBytes, LZMA_MATCH_LEN_MAX, allocBig))
  1.1965 +      return SZ_ERROR_MEM;
  1.1966 +    p->matchFinderObj = &p->matchFinderBase;
  1.1967 +    MatchFinder_CreateVTable(&p->matchFinderBase, &p->matchFinder);
  1.1968 +  }
  1.1969 +  return SZ_OK;
  1.1970 +}
  1.1971 +
  1.1972 +void LzmaEnc_Init(CLzmaEnc *p)
  1.1973 +{
  1.1974 +  UInt32 i;
  1.1975 +  p->state = 0;
  1.1976 +  for (i = 0 ; i < LZMA_NUM_REPS; i++)
  1.1977 +    p->reps[i] = 0;
  1.1978 +
  1.1979 +  RangeEnc_Init(&p->rc);
  1.1980 +
  1.1981 +
  1.1982 +  for (i = 0; i < kNumStates; i++)
  1.1983 +  {
  1.1984 +    UInt32 j;
  1.1985 +    for (j = 0; j < LZMA_NUM_PB_STATES_MAX; j++)
  1.1986 +    {
  1.1987 +      p->isMatch[i][j] = kProbInitValue;
  1.1988 +      p->isRep0Long[i][j] = kProbInitValue;
  1.1989 +    }
  1.1990 +    p->isRep[i] = kProbInitValue;
  1.1991 +    p->isRepG0[i] = kProbInitValue;
  1.1992 +    p->isRepG1[i] = kProbInitValue;
  1.1993 +    p->isRepG2[i] = kProbInitValue;
  1.1994 +  }
  1.1995 +
  1.1996 +  {
  1.1997 +    UInt32 num = 0x300 << (p->lp + p->lc);
  1.1998 +    for (i = 0; i < num; i++)
  1.1999 +      p->litProbs[i] = kProbInitValue;
  1.2000 +  }
  1.2001 +
  1.2002 +  {
  1.2003 +    for (i = 0; i < kNumLenToPosStates; i++)
  1.2004 +    {
  1.2005 +      CLzmaProb *probs = p->posSlotEncoder[i];
  1.2006 +      UInt32 j;
  1.2007 +      for (j = 0; j < (1 << kNumPosSlotBits); j++)
  1.2008 +        probs[j] = kProbInitValue;
  1.2009 +    }
  1.2010 +  }
  1.2011 +  {
  1.2012 +    for (i = 0; i < kNumFullDistances - kEndPosModelIndex; i++)
  1.2013 +      p->posEncoders[i] = kProbInitValue;
  1.2014 +  }
  1.2015 +
  1.2016 +  LenEnc_Init(&p->lenEnc.p);
  1.2017 +  LenEnc_Init(&p->repLenEnc.p);
  1.2018 +
  1.2019 +  for (i = 0; i < (1 << kNumAlignBits); i++)
  1.2020 +    p->posAlignEncoder[i] = kProbInitValue;
  1.2021 +
  1.2022 +  p->optimumEndIndex = 0;
  1.2023 +  p->optimumCurrentIndex = 0;
  1.2024 +  p->additionalOffset = 0;
  1.2025 +
  1.2026 +  p->pbMask = (1 << p->pb) - 1;
  1.2027 +  p->lpMask = (1 << p->lp) - 1;
  1.2028 +}
  1.2029 +
  1.2030 +void LzmaEnc_InitPrices(CLzmaEnc *p)
  1.2031 +{
  1.2032 +  if (!p->fastMode)
  1.2033 +  {
  1.2034 +    FillDistancesPrices(p);
  1.2035 +    FillAlignPrices(p);
  1.2036 +  }
  1.2037 +
  1.2038 +  p->lenEnc.tableSize =
  1.2039 +  p->repLenEnc.tableSize =
  1.2040 +      p->numFastBytes + 1 - LZMA_MATCH_LEN_MIN;
  1.2041 +  LenPriceEnc_UpdateTables(&p->lenEnc, 1 << p->pb, p->ProbPrices);
  1.2042 +  LenPriceEnc_UpdateTables(&p->repLenEnc, 1 << p->pb, p->ProbPrices);
  1.2043 +}
  1.2044 +
  1.2045 +static SRes LzmaEnc_AllocAndInit(CLzmaEnc *p, UInt32 keepWindowSize, ISzAlloc *alloc, ISzAlloc *allocBig)
  1.2046 +{
  1.2047 +  UInt32 i;
  1.2048 +  for (i = 0; i < (UInt32)kDicLogSizeMaxCompress; i++)
  1.2049 +    if (p->dictSize <= ((UInt32)1 << i))
  1.2050 +      break;
  1.2051 +  p->distTableSize = i * 2;
  1.2052 +
  1.2053 +  p->finished = False;
  1.2054 +  p->result = SZ_OK;
  1.2055 +  RINOK(LzmaEnc_Alloc(p, keepWindowSize, alloc, allocBig));
  1.2056 +  LzmaEnc_Init(p);
  1.2057 +  LzmaEnc_InitPrices(p);
  1.2058 +  p->nowPos64 = 0;
  1.2059 +  return SZ_OK;
  1.2060 +}
  1.2061 +
  1.2062 +static SRes LzmaEnc_Prepare(CLzmaEncHandle pp, ISeqInStream *inStream, ISeqOutStream *outStream,
  1.2063 +    ISzAlloc *alloc, ISzAlloc *allocBig)
  1.2064 +{
  1.2065 +  CLzmaEnc *p = (CLzmaEnc *)pp;
  1.2066 +  p->inStream = inStream;
  1.2067 +  p->rc.outStream = outStream;
  1.2068 +  return LzmaEnc_AllocAndInit(p, 0, alloc, allocBig);
  1.2069 +}
  1.2070 +
  1.2071 +SRes LzmaEnc_PrepareForLzma2(CLzmaEncHandle pp,
  1.2072 +    ISeqInStream *inStream, UInt32 keepWindowSize,
  1.2073 +    ISzAlloc *alloc, ISzAlloc *allocBig)
  1.2074 +{
  1.2075 +  CLzmaEnc *p = (CLzmaEnc *)pp;
  1.2076 +  p->inStream = inStream;
  1.2077 +  return LzmaEnc_AllocAndInit(p, keepWindowSize, alloc, allocBig);
  1.2078 +}
  1.2079 +
  1.2080 +static void LzmaEnc_SetInputBuf(CLzmaEnc *p, const Byte *src, SizeT srcLen)
  1.2081 +{
  1.2082 +  p->seqBufInStream.funcTable.Read = MyRead;
  1.2083 +  p->seqBufInStream.data = src;
  1.2084 +  p->seqBufInStream.rem = srcLen;
  1.2085 +}
  1.2086 +
  1.2087 +SRes LzmaEnc_MemPrepare(CLzmaEncHandle pp, const Byte *src, SizeT srcLen,
  1.2088 +    UInt32 keepWindowSize, ISzAlloc *alloc, ISzAlloc *allocBig)
  1.2089 +{
  1.2090 +  CLzmaEnc *p = (CLzmaEnc *)pp;
  1.2091 +  LzmaEnc_SetInputBuf(p, src, srcLen);
  1.2092 +  p->inStream = &p->seqBufInStream.funcTable;
  1.2093 +  return LzmaEnc_AllocAndInit(p, keepWindowSize, alloc, allocBig);
  1.2094 +}
  1.2095 +
  1.2096 +void LzmaEnc_Finish(CLzmaEncHandle pp)
  1.2097 +{
  1.2098 +  #ifdef COMPRESS_MF_MT
  1.2099 +  CLzmaEnc *p = (CLzmaEnc *)pp;
  1.2100 +  if (p->mtMode)
  1.2101 +    MatchFinderMt_ReleaseStream(&p->matchFinderMt);
  1.2102 +  #else
  1.2103 +  pp = pp;
  1.2104 +  #endif
  1.2105 +}
  1.2106 +
  1.2107 +typedef struct _CSeqOutStreamBuf
  1.2108 +{
  1.2109 +  ISeqOutStream funcTable;
  1.2110 +  Byte *data;
  1.2111 +  SizeT rem;
  1.2112 +  Bool overflow;
  1.2113 +} CSeqOutStreamBuf;
  1.2114 +
  1.2115 +static size_t MyWrite(void *pp, const void *data, size_t size)
  1.2116 +{
  1.2117 +  CSeqOutStreamBuf *p = (CSeqOutStreamBuf *)pp;
  1.2118 +  if (p->rem < size)
  1.2119 +  {
  1.2120 +    size = p->rem;
  1.2121 +    p->overflow = True;
  1.2122 +  }
  1.2123 +  memcpy(p->data, data, size);
  1.2124 +  p->rem -= size;
  1.2125 +  p->data += size;
  1.2126 +  return size;
  1.2127 +}
  1.2128 +
  1.2129 +
  1.2130 +UInt32 LzmaEnc_GetNumAvailableBytes(CLzmaEncHandle pp)
  1.2131 +{
  1.2132 +  const CLzmaEnc *p = (CLzmaEnc *)pp;
  1.2133 +  return p->matchFinder.GetNumAvailableBytes(p->matchFinderObj);
  1.2134 +}
  1.2135 +
  1.2136 +const Byte *LzmaEnc_GetCurBuf(CLzmaEncHandle pp)
  1.2137 +{
  1.2138 +  const CLzmaEnc *p = (CLzmaEnc *)pp;
  1.2139 +  return p->matchFinder.GetPointerToCurrentPos(p->matchFinderObj) - p->additionalOffset;
  1.2140 +}
  1.2141 +
  1.2142 +SRes LzmaEnc_CodeOneMemBlock(CLzmaEncHandle pp, Bool reInit,
  1.2143 +    Byte *dest, size_t *destLen, UInt32 desiredPackSize, UInt32 *unpackSize)
  1.2144 +{
  1.2145 +  CLzmaEnc *p = (CLzmaEnc *)pp;
  1.2146 +  UInt64 nowPos64;
  1.2147 +  SRes res;
  1.2148 +  CSeqOutStreamBuf outStream;
  1.2149 +
  1.2150 +  outStream.funcTable.Write = MyWrite;
  1.2151 +  outStream.data = dest;
  1.2152 +  outStream.rem = *destLen;
  1.2153 +  outStream.overflow = False;
  1.2154 +
  1.2155 +  p->writeEndMark = False;
  1.2156 +  p->finished = False;
  1.2157 +  p->result = SZ_OK;
  1.2158 +
  1.2159 +  if (reInit)
  1.2160 +    LzmaEnc_Init(p);
  1.2161 +  LzmaEnc_InitPrices(p);
  1.2162 +  nowPos64 = p->nowPos64;
  1.2163 +  RangeEnc_Init(&p->rc);
  1.2164 +  p->rc.outStream = &outStream.funcTable;
  1.2165 +
  1.2166 +  res = LzmaEnc_CodeOneBlock(p, True, desiredPackSize, *unpackSize);
  1.2167 +  
  1.2168 +  *unpackSize = (UInt32)(p->nowPos64 - nowPos64);
  1.2169 +  *destLen -= outStream.rem;
  1.2170 +  if (outStream.overflow)
  1.2171 +    return SZ_ERROR_OUTPUT_EOF;
  1.2172 +
  1.2173 +  return res;
  1.2174 +}
  1.2175 +
  1.2176 +SRes LzmaEnc_Encode(CLzmaEncHandle pp, ISeqOutStream *outStream, ISeqInStream *inStream, ICompressProgress *progress,
  1.2177 +    ISzAlloc *alloc, ISzAlloc *allocBig)
  1.2178 +{
  1.2179 +  CLzmaEnc *p = (CLzmaEnc *)pp;
  1.2180 +  SRes res = SZ_OK;
  1.2181 +
  1.2182 +  #ifdef COMPRESS_MF_MT
  1.2183 +  Byte allocaDummy[0x300];
  1.2184 +  int i = 0;
  1.2185 +  for (i = 0; i < 16; i++)
  1.2186 +    allocaDummy[i] = (Byte)i;
  1.2187 +  #endif
  1.2188 +
  1.2189 +  RINOK(LzmaEnc_Prepare(pp, inStream, outStream, alloc, allocBig));
  1.2190 +
  1.2191 +  for (;;)
  1.2192 +  {
  1.2193 +    res = LzmaEnc_CodeOneBlock(p, False, 0, 0);
  1.2194 +    if (res != SZ_OK || p->finished != 0)
  1.2195 +      break;
  1.2196 +    if (progress != 0)
  1.2197 +    {
  1.2198 +      res = progress->Progress(progress, p->nowPos64, RangeEnc_GetProcessed(&p->rc));
  1.2199 +      if (res != SZ_OK)
  1.2200 +      {
  1.2201 +        res = SZ_ERROR_PROGRESS;
  1.2202 +        break;
  1.2203 +      }
  1.2204 +    }
  1.2205 +  }
  1.2206 +  LzmaEnc_Finish(pp);
  1.2207 +  return res;
  1.2208 +}
  1.2209 +
  1.2210 +SRes LzmaEnc_WriteProperties(CLzmaEncHandle pp, Byte *props, SizeT *size)
  1.2211 +{
  1.2212 +  CLzmaEnc *p = (CLzmaEnc *)pp;
  1.2213 +  int i;
  1.2214 +  UInt32 dictSize = p->dictSize;
  1.2215 +  if (*size < LZMA_PROPS_SIZE)
  1.2216 +    return SZ_ERROR_PARAM;
  1.2217 +  *size = LZMA_PROPS_SIZE;
  1.2218 +  props[0] = (Byte)((p->pb * 5 + p->lp) * 9 + p->lc);
  1.2219 +
  1.2220 +  for (i = 11; i <= 30; i++)
  1.2221 +  {
  1.2222 +    if (dictSize <= ((UInt32)2 << i))
  1.2223 +    {
  1.2224 +      dictSize = (2 << i);
  1.2225 +      break;
  1.2226 +    }
  1.2227 +    if (dictSize <= ((UInt32)3 << i))
  1.2228 +    {
  1.2229 +      dictSize = (3 << i);
  1.2230 +      break;
  1.2231 +    }
  1.2232 +  }
  1.2233 +
  1.2234 +  for (i = 0; i < 4; i++)
  1.2235 +    props[1 + i] = (Byte)(dictSize >> (8 * i));
  1.2236 +  return SZ_OK;
  1.2237 +}
  1.2238 +
  1.2239 +SRes LzmaEnc_MemEncode(CLzmaEncHandle pp, Byte *dest, SizeT *destLen, const Byte *src, SizeT srcLen,
  1.2240 +    int writeEndMark, ICompressProgress *progress, ISzAlloc *alloc, ISzAlloc *allocBig)
  1.2241 +{
  1.2242 +  SRes res;
  1.2243 +  CLzmaEnc *p = (CLzmaEnc *)pp;
  1.2244 +
  1.2245 +  CSeqOutStreamBuf outStream;
  1.2246 +
  1.2247 +  LzmaEnc_SetInputBuf(p, src, srcLen);
  1.2248 +
  1.2249 +  outStream.funcTable.Write = MyWrite;
  1.2250 +  outStream.data = dest;
  1.2251 +  outStream.rem = *destLen;
  1.2252 +  outStream.overflow = False;
  1.2253 +
  1.2254 +  p->writeEndMark = writeEndMark;
  1.2255 +  res = LzmaEnc_Encode(pp, &outStream.funcTable, &p->seqBufInStream.funcTable,
  1.2256 +      progress, alloc, allocBig);
  1.2257 +
  1.2258 +  *destLen -= outStream.rem;
  1.2259 +  if (outStream.overflow)
  1.2260 +    return SZ_ERROR_OUTPUT_EOF;
  1.2261 +  return res;
  1.2262 +}
  1.2263 +
  1.2264 +SRes LzmaEncode(Byte *dest, SizeT *destLen, const Byte *src, SizeT srcLen,
  1.2265 +    const CLzmaEncProps *props, Byte *propsEncoded, SizeT *propsSize, int writeEndMark,
  1.2266 +    ICompressProgress *progress, ISzAlloc *alloc, ISzAlloc *allocBig)
  1.2267 +{
  1.2268 +  CLzmaEnc *p = (CLzmaEnc *)LzmaEnc_Create(alloc);
  1.2269 +  SRes res;
  1.2270 +  if (p == 0)
  1.2271 +    return SZ_ERROR_MEM;
  1.2272 +
  1.2273 +  res = LzmaEnc_SetProps(p, props);
  1.2274 +  if (res == SZ_OK)
  1.2275 +  {
  1.2276 +    res = LzmaEnc_WriteProperties(p, propsEncoded, propsSize);
  1.2277 +    if (res == SZ_OK)
  1.2278 +      res = LzmaEnc_MemEncode(p, dest, destLen, src, srcLen,
  1.2279 +          writeEndMark, progress, alloc, allocBig);
  1.2280 +  }
  1.2281 +
  1.2282 +  LzmaEnc_Destroy(p, alloc, allocBig);
  1.2283 +  return res;
  1.2284 +}