1baa3858d3f5d128a5c8466b700098109edcad5f2repo sync/* LzmaEnc.c -- LZMA Encoder
2baa3858d3f5d128a5c8466b700098109edcad5f2repo sync2010-04-16 : Igor Pavlov : Public domain */
3baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
4baa3858d3f5d128a5c8466b700098109edcad5f2repo sync#include <string.h>
5baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
6baa3858d3f5d128a5c8466b700098109edcad5f2repo sync/* #define SHOW_STAT */
7baa3858d3f5d128a5c8466b700098109edcad5f2repo sync/* #define SHOW_STAT2 */
8baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
9baa3858d3f5d128a5c8466b700098109edcad5f2repo sync#if defined(SHOW_STAT) || defined(SHOW_STAT2)
10baa3858d3f5d128a5c8466b700098109edcad5f2repo sync#include <stdio.h>
11baa3858d3f5d128a5c8466b700098109edcad5f2repo sync#endif
12baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
13baa3858d3f5d128a5c8466b700098109edcad5f2repo sync#include "LzmaEnc.h"
14baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
15baa3858d3f5d128a5c8466b700098109edcad5f2repo sync#include "LzFind.h"
16baa3858d3f5d128a5c8466b700098109edcad5f2repo sync#ifndef _7ZIP_ST
17baa3858d3f5d128a5c8466b700098109edcad5f2repo sync#include "LzFindMt.h"
18baa3858d3f5d128a5c8466b700098109edcad5f2repo sync#endif
19baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
20baa3858d3f5d128a5c8466b700098109edcad5f2repo sync#ifdef SHOW_STAT
21baa3858d3f5d128a5c8466b700098109edcad5f2repo syncstatic int ttt = 0;
22baa3858d3f5d128a5c8466b700098109edcad5f2repo sync#endif
23baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
24baa3858d3f5d128a5c8466b700098109edcad5f2repo sync#define kBlockSizeMax ((1 << LZMA_NUM_BLOCK_SIZE_BITS) - 1)
25baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
26baa3858d3f5d128a5c8466b700098109edcad5f2repo sync#define kBlockSize (9 << 10)
27baa3858d3f5d128a5c8466b700098109edcad5f2repo sync#define kUnpackBlockSize (1 << 18)
28baa3858d3f5d128a5c8466b700098109edcad5f2repo sync#define kMatchArraySize (1 << 21)
29baa3858d3f5d128a5c8466b700098109edcad5f2repo sync#define kMatchRecordMaxSize ((LZMA_MATCH_LEN_MAX * 2 + 3) * LZMA_MATCH_LEN_MAX)
30baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
31baa3858d3f5d128a5c8466b700098109edcad5f2repo sync#define kNumMaxDirectBits (31)
32baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
33baa3858d3f5d128a5c8466b700098109edcad5f2repo sync#define kNumTopBits 24
34baa3858d3f5d128a5c8466b700098109edcad5f2repo sync#define kTopValue ((UInt32)1 << kNumTopBits)
35baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
36baa3858d3f5d128a5c8466b700098109edcad5f2repo sync#define kNumBitModelTotalBits 11
37baa3858d3f5d128a5c8466b700098109edcad5f2repo sync#define kBitModelTotal (1 << kNumBitModelTotalBits)
38baa3858d3f5d128a5c8466b700098109edcad5f2repo sync#define kNumMoveBits 5
39baa3858d3f5d128a5c8466b700098109edcad5f2repo sync#define kProbInitValue (kBitModelTotal >> 1)
40baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
41baa3858d3f5d128a5c8466b700098109edcad5f2repo sync#define kNumMoveReducingBits 4
42baa3858d3f5d128a5c8466b700098109edcad5f2repo sync#define kNumBitPriceShiftBits 4
43baa3858d3f5d128a5c8466b700098109edcad5f2repo sync#define kBitPrice (1 << kNumBitPriceShiftBits)
44baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
45baa3858d3f5d128a5c8466b700098109edcad5f2repo syncvoid LzmaEncProps_Init(CLzmaEncProps *p)
46baa3858d3f5d128a5c8466b700098109edcad5f2repo sync{
47baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  p->level = 5;
48baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  p->dictSize = p->mc = 0;
49baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  p->lc = p->lp = p->pb = p->algo = p->fb = p->btMode = p->numHashBytes = p->numThreads = -1;
50baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  p->writeEndMark = 0;
51baa3858d3f5d128a5c8466b700098109edcad5f2repo sync}
52baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
53baa3858d3f5d128a5c8466b700098109edcad5f2repo syncvoid LzmaEncProps_Normalize(CLzmaEncProps *p)
54baa3858d3f5d128a5c8466b700098109edcad5f2repo sync{
55baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  int level = p->level;
56baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  if (level < 0) level = 5;
57baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  p->level = level;
58baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  if (p->dictSize == 0) p->dictSize = (level <= 5 ? (1 << (level * 2 + 14)) : (level == 6 ? (1 << 25) : (1 << 26)));
59baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  if (p->lc < 0) p->lc = 3;
60baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  if (p->lp < 0) p->lp = 0;
61baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  if (p->pb < 0) p->pb = 2;
62baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  if (p->algo < 0) p->algo = (level < 5 ? 0 : 1);
63baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  if (p->fb < 0) p->fb = (level < 7 ? 32 : 64);
64baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  if (p->btMode < 0) p->btMode = (p->algo == 0 ? 0 : 1);
65baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  if (p->numHashBytes < 0) p->numHashBytes = 4;
66baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  if (p->mc == 0)  p->mc = (16 + (p->fb >> 1)) >> (p->btMode ? 0 : 1);
67baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  if (p->numThreads < 0)
68baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    p->numThreads =
69baa3858d3f5d128a5c8466b700098109edcad5f2repo sync      #ifndef _7ZIP_ST
70baa3858d3f5d128a5c8466b700098109edcad5f2repo sync      ((p->btMode && p->algo) ? 2 : 1);
71baa3858d3f5d128a5c8466b700098109edcad5f2repo sync      #else
72baa3858d3f5d128a5c8466b700098109edcad5f2repo sync      1;
73baa3858d3f5d128a5c8466b700098109edcad5f2repo sync      #endif
74baa3858d3f5d128a5c8466b700098109edcad5f2repo sync}
75baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
76baa3858d3f5d128a5c8466b700098109edcad5f2repo syncUInt32 LzmaEncProps_GetDictSize(const CLzmaEncProps *props2)
77baa3858d3f5d128a5c8466b700098109edcad5f2repo sync{
78baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  CLzmaEncProps props = *props2;
79baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  LzmaEncProps_Normalize(&props);
80baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  return props.dictSize;
81baa3858d3f5d128a5c8466b700098109edcad5f2repo sync}
82baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
83baa3858d3f5d128a5c8466b700098109edcad5f2repo sync/* #define LZMA_LOG_BSR */
84baa3858d3f5d128a5c8466b700098109edcad5f2repo sync/* Define it for Intel's CPU */
85baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
86baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
87baa3858d3f5d128a5c8466b700098109edcad5f2repo sync#ifdef LZMA_LOG_BSR
88baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
89baa3858d3f5d128a5c8466b700098109edcad5f2repo sync#define kDicLogSizeMaxCompress 30
90baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
91baa3858d3f5d128a5c8466b700098109edcad5f2repo sync#define BSR2_RET(pos, res) { unsigned long i; _BitScanReverse(&i, (pos)); res = (i + i) + ((pos >> (i - 1)) & 1); }
92baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
93baa3858d3f5d128a5c8466b700098109edcad5f2repo syncUInt32 GetPosSlot1(UInt32 pos)
94baa3858d3f5d128a5c8466b700098109edcad5f2repo sync{
95baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  UInt32 res;
96baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  BSR2_RET(pos, res);
97baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  return res;
98baa3858d3f5d128a5c8466b700098109edcad5f2repo sync}
99baa3858d3f5d128a5c8466b700098109edcad5f2repo sync#define GetPosSlot2(pos, res) { BSR2_RET(pos, res); }
100baa3858d3f5d128a5c8466b700098109edcad5f2repo sync#define GetPosSlot(pos, res) { if (pos < 2) res = pos; else BSR2_RET(pos, res); }
101baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
102baa3858d3f5d128a5c8466b700098109edcad5f2repo sync#else
103baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
104baa3858d3f5d128a5c8466b700098109edcad5f2repo sync#define kNumLogBits (9 + (int)sizeof(size_t) / 2)
105baa3858d3f5d128a5c8466b700098109edcad5f2repo sync#define kDicLogSizeMaxCompress ((kNumLogBits - 1) * 2 + 7)
106baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
107baa3858d3f5d128a5c8466b700098109edcad5f2repo syncvoid LzmaEnc_FastPosInit(Byte *g_FastPos)
108baa3858d3f5d128a5c8466b700098109edcad5f2repo sync{
109baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  int c = 2, slotFast;
110baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  g_FastPos[0] = 0;
111baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  g_FastPos[1] = 1;
112baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
113baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  for (slotFast = 2; slotFast < kNumLogBits * 2; slotFast++)
114baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  {
115baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    UInt32 k = (1 << ((slotFast >> 1) - 1));
116baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    UInt32 j;
117baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    for (j = 0; j < k; j++, c++)
118baa3858d3f5d128a5c8466b700098109edcad5f2repo sync      g_FastPos[c] = (Byte)slotFast;
119baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  }
120baa3858d3f5d128a5c8466b700098109edcad5f2repo sync}
121baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
122baa3858d3f5d128a5c8466b700098109edcad5f2repo sync#define BSR2_RET(pos, res) { UInt32 i = 6 + ((kNumLogBits - 1) & \
123baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  (0 - (((((UInt32)1 << (kNumLogBits + 6)) - 1) - pos) >> 31))); \
124baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  res = p->g_FastPos[pos >> i] + (i * 2); }
125baa3858d3f5d128a5c8466b700098109edcad5f2repo sync/*
126baa3858d3f5d128a5c8466b700098109edcad5f2repo sync#define BSR2_RET(pos, res) { res = (pos < (1 << (kNumLogBits + 6))) ? \
127baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  p->g_FastPos[pos >> 6] + 12 : \
128baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  p->g_FastPos[pos >> (6 + kNumLogBits - 1)] + (6 + (kNumLogBits - 1)) * 2; }
129baa3858d3f5d128a5c8466b700098109edcad5f2repo sync*/
130baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
131baa3858d3f5d128a5c8466b700098109edcad5f2repo sync#define GetPosSlot1(pos) p->g_FastPos[pos]
132baa3858d3f5d128a5c8466b700098109edcad5f2repo sync#define GetPosSlot2(pos, res) { BSR2_RET(pos, res); }
133baa3858d3f5d128a5c8466b700098109edcad5f2repo sync#define GetPosSlot(pos, res) { if (pos < kNumFullDistances) res = p->g_FastPos[pos]; else BSR2_RET(pos, res); }
134baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
135baa3858d3f5d128a5c8466b700098109edcad5f2repo sync#endif
136baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
137baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
138baa3858d3f5d128a5c8466b700098109edcad5f2repo sync#define LZMA_NUM_REPS 4
139baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
140baa3858d3f5d128a5c8466b700098109edcad5f2repo synctypedef unsigned CState;
141baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
142baa3858d3f5d128a5c8466b700098109edcad5f2repo synctypedef struct
143baa3858d3f5d128a5c8466b700098109edcad5f2repo sync{
144baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  UInt32 price;
145baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
146baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  CState state;
147baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  int prev1IsChar;
148baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  int prev2;
149baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
150baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  UInt32 posPrev2;
151baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  UInt32 backPrev2;
152baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
153baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  UInt32 posPrev;
154baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  UInt32 backPrev;
155baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  UInt32 backs[LZMA_NUM_REPS];
156baa3858d3f5d128a5c8466b700098109edcad5f2repo sync} COptimal;
157baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
158baa3858d3f5d128a5c8466b700098109edcad5f2repo sync#define kNumOpts (1 << 12)
159baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
160baa3858d3f5d128a5c8466b700098109edcad5f2repo sync#define kNumLenToPosStates 4
161baa3858d3f5d128a5c8466b700098109edcad5f2repo sync#define kNumPosSlotBits 6
162baa3858d3f5d128a5c8466b700098109edcad5f2repo sync#define kDicLogSizeMin 0
163baa3858d3f5d128a5c8466b700098109edcad5f2repo sync#define kDicLogSizeMax 32
164baa3858d3f5d128a5c8466b700098109edcad5f2repo sync#define kDistTableSizeMax (kDicLogSizeMax * 2)
165baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
166baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
167baa3858d3f5d128a5c8466b700098109edcad5f2repo sync#define kNumAlignBits 4
168baa3858d3f5d128a5c8466b700098109edcad5f2repo sync#define kAlignTableSize (1 << kNumAlignBits)
169baa3858d3f5d128a5c8466b700098109edcad5f2repo sync#define kAlignMask (kAlignTableSize - 1)
170baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
171baa3858d3f5d128a5c8466b700098109edcad5f2repo sync#define kStartPosModelIndex 4
172baa3858d3f5d128a5c8466b700098109edcad5f2repo sync#define kEndPosModelIndex 14
173baa3858d3f5d128a5c8466b700098109edcad5f2repo sync#define kNumPosModels (kEndPosModelIndex - kStartPosModelIndex)
174baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
175baa3858d3f5d128a5c8466b700098109edcad5f2repo sync#define kNumFullDistances (1 << (kEndPosModelIndex >> 1))
176baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
177baa3858d3f5d128a5c8466b700098109edcad5f2repo sync#ifdef _LZMA_PROB32
178baa3858d3f5d128a5c8466b700098109edcad5f2repo sync#define CLzmaProb UInt32
179baa3858d3f5d128a5c8466b700098109edcad5f2repo sync#else
180baa3858d3f5d128a5c8466b700098109edcad5f2repo sync#define CLzmaProb UInt16
181baa3858d3f5d128a5c8466b700098109edcad5f2repo sync#endif
182baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
183baa3858d3f5d128a5c8466b700098109edcad5f2repo sync#define LZMA_PB_MAX 4
184baa3858d3f5d128a5c8466b700098109edcad5f2repo sync#define LZMA_LC_MAX 8
185baa3858d3f5d128a5c8466b700098109edcad5f2repo sync#define LZMA_LP_MAX 4
186baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
187baa3858d3f5d128a5c8466b700098109edcad5f2repo sync#define LZMA_NUM_PB_STATES_MAX (1 << LZMA_PB_MAX)
188baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
189baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
190baa3858d3f5d128a5c8466b700098109edcad5f2repo sync#define kLenNumLowBits 3
191baa3858d3f5d128a5c8466b700098109edcad5f2repo sync#define kLenNumLowSymbols (1 << kLenNumLowBits)
192baa3858d3f5d128a5c8466b700098109edcad5f2repo sync#define kLenNumMidBits 3
193baa3858d3f5d128a5c8466b700098109edcad5f2repo sync#define kLenNumMidSymbols (1 << kLenNumMidBits)
194baa3858d3f5d128a5c8466b700098109edcad5f2repo sync#define kLenNumHighBits 8
195baa3858d3f5d128a5c8466b700098109edcad5f2repo sync#define kLenNumHighSymbols (1 << kLenNumHighBits)
196baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
197baa3858d3f5d128a5c8466b700098109edcad5f2repo sync#define kLenNumSymbolsTotal (kLenNumLowSymbols + kLenNumMidSymbols + kLenNumHighSymbols)
198baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
199baa3858d3f5d128a5c8466b700098109edcad5f2repo sync#define LZMA_MATCH_LEN_MIN 2
200baa3858d3f5d128a5c8466b700098109edcad5f2repo sync#define LZMA_MATCH_LEN_MAX (LZMA_MATCH_LEN_MIN + kLenNumSymbolsTotal - 1)
201baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
202baa3858d3f5d128a5c8466b700098109edcad5f2repo sync#define kNumStates 12
203baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
204baa3858d3f5d128a5c8466b700098109edcad5f2repo synctypedef struct
205baa3858d3f5d128a5c8466b700098109edcad5f2repo sync{
206baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  CLzmaProb choice;
207baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  CLzmaProb choice2;
208baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  CLzmaProb low[LZMA_NUM_PB_STATES_MAX << kLenNumLowBits];
209baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  CLzmaProb mid[LZMA_NUM_PB_STATES_MAX << kLenNumMidBits];
210baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  CLzmaProb high[kLenNumHighSymbols];
211baa3858d3f5d128a5c8466b700098109edcad5f2repo sync} CLenEnc;
212baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
213baa3858d3f5d128a5c8466b700098109edcad5f2repo synctypedef struct
214baa3858d3f5d128a5c8466b700098109edcad5f2repo sync{
215baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  CLenEnc p;
216baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  UInt32 prices[LZMA_NUM_PB_STATES_MAX][kLenNumSymbolsTotal];
217baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  UInt32 tableSize;
218baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  UInt32 counters[LZMA_NUM_PB_STATES_MAX];
219baa3858d3f5d128a5c8466b700098109edcad5f2repo sync} CLenPriceEnc;
220baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
221baa3858d3f5d128a5c8466b700098109edcad5f2repo synctypedef struct
222baa3858d3f5d128a5c8466b700098109edcad5f2repo sync{
223baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  UInt32 range;
224baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  Byte cache;
225baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  UInt64 low;
226baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  UInt64 cacheSize;
227baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  Byte *buf;
228baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  Byte *bufLim;
229baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  Byte *bufBase;
230baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  ISeqOutStream *outStream;
231baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  UInt64 processed;
232baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  SRes res;
233baa3858d3f5d128a5c8466b700098109edcad5f2repo sync} CRangeEnc;
234baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
235baa3858d3f5d128a5c8466b700098109edcad5f2repo synctypedef struct
236baa3858d3f5d128a5c8466b700098109edcad5f2repo sync{
237baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  CLzmaProb *litProbs;
238baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
239baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  CLzmaProb isMatch[kNumStates][LZMA_NUM_PB_STATES_MAX];
240baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  CLzmaProb isRep[kNumStates];
241baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  CLzmaProb isRepG0[kNumStates];
242baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  CLzmaProb isRepG1[kNumStates];
243baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  CLzmaProb isRepG2[kNumStates];
244baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  CLzmaProb isRep0Long[kNumStates][LZMA_NUM_PB_STATES_MAX];
245baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
246baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  CLzmaProb posSlotEncoder[kNumLenToPosStates][1 << kNumPosSlotBits];
247baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  CLzmaProb posEncoders[kNumFullDistances - kEndPosModelIndex];
248baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  CLzmaProb posAlignEncoder[1 << kNumAlignBits];
249baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
250baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  CLenPriceEnc lenEnc;
251baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  CLenPriceEnc repLenEnc;
252baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
253baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  UInt32 reps[LZMA_NUM_REPS];
254baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  UInt32 state;
255baa3858d3f5d128a5c8466b700098109edcad5f2repo sync} CSaveState;
256baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
257baa3858d3f5d128a5c8466b700098109edcad5f2repo synctypedef struct
258baa3858d3f5d128a5c8466b700098109edcad5f2repo sync{
259baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  IMatchFinder matchFinder;
260baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  void *matchFinderObj;
261baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
262baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  #ifndef _7ZIP_ST
263baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  Bool mtMode;
264baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  CMatchFinderMt matchFinderMt;
265baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  #endif
266baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
267baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  CMatchFinder matchFinderBase;
268baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
269baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  #ifndef _7ZIP_ST
270baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  Byte pad[128];
271baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  #endif
272baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
273baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  UInt32 optimumEndIndex;
274baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  UInt32 optimumCurrentIndex;
275baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
276baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  UInt32 longestMatchLength;
277baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  UInt32 numPairs;
278baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  UInt32 numAvail;
279baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  COptimal opt[kNumOpts];
280baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
281baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  #ifndef LZMA_LOG_BSR
282baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  Byte g_FastPos[1 << kNumLogBits];
283baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  #endif
284baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
285baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  UInt32 ProbPrices[kBitModelTotal >> kNumMoveReducingBits];
286baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  UInt32 matches[LZMA_MATCH_LEN_MAX * 2 + 2 + 1];
287baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  UInt32 numFastBytes;
288baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  UInt32 additionalOffset;
289baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  UInt32 reps[LZMA_NUM_REPS];
290baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  UInt32 state;
291baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
292baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  UInt32 posSlotPrices[kNumLenToPosStates][kDistTableSizeMax];
293baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  UInt32 distancesPrices[kNumLenToPosStates][kNumFullDistances];
294baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  UInt32 alignPrices[kAlignTableSize];
295baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  UInt32 alignPriceCount;
296baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
297baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  UInt32 distTableSize;
298baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
299baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  unsigned lc, lp, pb;
300baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  unsigned lpMask, pbMask;
301baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
302baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  CLzmaProb *litProbs;
303baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
304baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  CLzmaProb isMatch[kNumStates][LZMA_NUM_PB_STATES_MAX];
305baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  CLzmaProb isRep[kNumStates];
306baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  CLzmaProb isRepG0[kNumStates];
307baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  CLzmaProb isRepG1[kNumStates];
308baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  CLzmaProb isRepG2[kNumStates];
309baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  CLzmaProb isRep0Long[kNumStates][LZMA_NUM_PB_STATES_MAX];
310baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
311baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  CLzmaProb posSlotEncoder[kNumLenToPosStates][1 << kNumPosSlotBits];
312baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  CLzmaProb posEncoders[kNumFullDistances - kEndPosModelIndex];
313baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  CLzmaProb posAlignEncoder[1 << kNumAlignBits];
314baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
315baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  CLenPriceEnc lenEnc;
316baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  CLenPriceEnc repLenEnc;
317baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
318baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  unsigned lclp;
319baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
320baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  Bool fastMode;
321baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
322baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  CRangeEnc rc;
323baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
324baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  Bool writeEndMark;
325baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  UInt64 nowPos64;
326baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  UInt32 matchPriceCount;
327baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  Bool finished;
328baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  Bool multiThread;
329baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
330baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  SRes result;
331baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  UInt32 dictSize;
332baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  UInt32 matchFinderCycles;
333baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
334baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  int needInit;
335baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
336baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  CSaveState saveState;
337baa3858d3f5d128a5c8466b700098109edcad5f2repo sync} CLzmaEnc;
338baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
339baa3858d3f5d128a5c8466b700098109edcad5f2repo syncvoid LzmaEnc_SaveState(CLzmaEncHandle pp)
340baa3858d3f5d128a5c8466b700098109edcad5f2repo sync{
341baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  CLzmaEnc *p = (CLzmaEnc *)pp;
342baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  CSaveState *dest = &p->saveState;
343baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  int i;
344baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  dest->lenEnc = p->lenEnc;
345baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  dest->repLenEnc = p->repLenEnc;
346baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  dest->state = p->state;
347baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
348baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  for (i = 0; i < kNumStates; i++)
349baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  {
350baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    memcpy(dest->isMatch[i], p->isMatch[i], sizeof(p->isMatch[i]));
351baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    memcpy(dest->isRep0Long[i], p->isRep0Long[i], sizeof(p->isRep0Long[i]));
352baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  }
353baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  for (i = 0; i < kNumLenToPosStates; i++)
354baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    memcpy(dest->posSlotEncoder[i], p->posSlotEncoder[i], sizeof(p->posSlotEncoder[i]));
355baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  memcpy(dest->isRep, p->isRep, sizeof(p->isRep));
356baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  memcpy(dest->isRepG0, p->isRepG0, sizeof(p->isRepG0));
357baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  memcpy(dest->isRepG1, p->isRepG1, sizeof(p->isRepG1));
358baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  memcpy(dest->isRepG2, p->isRepG2, sizeof(p->isRepG2));
359baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  memcpy(dest->posEncoders, p->posEncoders, sizeof(p->posEncoders));
360baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  memcpy(dest->posAlignEncoder, p->posAlignEncoder, sizeof(p->posAlignEncoder));
361baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  memcpy(dest->reps, p->reps, sizeof(p->reps));
362baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  memcpy(dest->litProbs, p->litProbs, (0x300 << p->lclp) * sizeof(CLzmaProb));
363baa3858d3f5d128a5c8466b700098109edcad5f2repo sync}
364baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
365baa3858d3f5d128a5c8466b700098109edcad5f2repo syncvoid LzmaEnc_RestoreState(CLzmaEncHandle pp)
366baa3858d3f5d128a5c8466b700098109edcad5f2repo sync{
367baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  CLzmaEnc *dest = (CLzmaEnc *)pp;
368baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  const CSaveState *p = &dest->saveState;
369baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  int i;
370baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  dest->lenEnc = p->lenEnc;
371baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  dest->repLenEnc = p->repLenEnc;
372baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  dest->state = p->state;
373baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
374baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  for (i = 0; i < kNumStates; i++)
375baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  {
376baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    memcpy(dest->isMatch[i], p->isMatch[i], sizeof(p->isMatch[i]));
377baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    memcpy(dest->isRep0Long[i], p->isRep0Long[i], sizeof(p->isRep0Long[i]));
378baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  }
379baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  for (i = 0; i < kNumLenToPosStates; i++)
380baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    memcpy(dest->posSlotEncoder[i], p->posSlotEncoder[i], sizeof(p->posSlotEncoder[i]));
381baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  memcpy(dest->isRep, p->isRep, sizeof(p->isRep));
382baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  memcpy(dest->isRepG0, p->isRepG0, sizeof(p->isRepG0));
383baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  memcpy(dest->isRepG1, p->isRepG1, sizeof(p->isRepG1));
384baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  memcpy(dest->isRepG2, p->isRepG2, sizeof(p->isRepG2));
385baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  memcpy(dest->posEncoders, p->posEncoders, sizeof(p->posEncoders));
386baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  memcpy(dest->posAlignEncoder, p->posAlignEncoder, sizeof(p->posAlignEncoder));
387baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  memcpy(dest->reps, p->reps, sizeof(p->reps));
388baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  memcpy(dest->litProbs, p->litProbs, (0x300 << dest->lclp) * sizeof(CLzmaProb));
389baa3858d3f5d128a5c8466b700098109edcad5f2repo sync}
390baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
391baa3858d3f5d128a5c8466b700098109edcad5f2repo syncSRes LzmaEnc_SetProps(CLzmaEncHandle pp, const CLzmaEncProps *props2)
392baa3858d3f5d128a5c8466b700098109edcad5f2repo sync{
393baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  CLzmaEnc *p = (CLzmaEnc *)pp;
394baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  CLzmaEncProps props = *props2;
395baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  LzmaEncProps_Normalize(&props);
396baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
397baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  if (props.lc > LZMA_LC_MAX || props.lp > LZMA_LP_MAX || props.pb > LZMA_PB_MAX ||
398baa3858d3f5d128a5c8466b700098109edcad5f2repo sync      props.dictSize > ((UInt32)1 << kDicLogSizeMaxCompress) || props.dictSize > ((UInt32)1 << 30))
399baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    return SZ_ERROR_PARAM;
400baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  p->dictSize = props.dictSize;
401baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  p->matchFinderCycles = props.mc;
402baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  {
403baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    unsigned fb = props.fb;
404baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    if (fb < 5)
405baa3858d3f5d128a5c8466b700098109edcad5f2repo sync      fb = 5;
406baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    if (fb > LZMA_MATCH_LEN_MAX)
407baa3858d3f5d128a5c8466b700098109edcad5f2repo sync      fb = LZMA_MATCH_LEN_MAX;
408baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    p->numFastBytes = fb;
409baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  }
410baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  p->lc = props.lc;
411baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  p->lp = props.lp;
412baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  p->pb = props.pb;
413baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  p->fastMode = (props.algo == 0);
414baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  p->matchFinderBase.btMode = props.btMode;
415baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  {
416baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    UInt32 numHashBytes = 4;
417baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    if (props.btMode)
418baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    {
419baa3858d3f5d128a5c8466b700098109edcad5f2repo sync      if (props.numHashBytes < 2)
420baa3858d3f5d128a5c8466b700098109edcad5f2repo sync        numHashBytes = 2;
421baa3858d3f5d128a5c8466b700098109edcad5f2repo sync      else if (props.numHashBytes < 4)
422baa3858d3f5d128a5c8466b700098109edcad5f2repo sync        numHashBytes = props.numHashBytes;
423baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    }
424baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    p->matchFinderBase.numHashBytes = numHashBytes;
425baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  }
426baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
427baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  p->matchFinderBase.cutValue = props.mc;
428baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
429baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  p->writeEndMark = props.writeEndMark;
430baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
431baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  #ifndef _7ZIP_ST
432baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  /*
433baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  if (newMultiThread != _multiThread)
434baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  {
435baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    ReleaseMatchFinder();
436baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    _multiThread = newMultiThread;
437baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  }
438baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  */
439baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  p->multiThread = (props.numThreads > 1);
440baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  #endif
441baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
442baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  return SZ_OK;
443baa3858d3f5d128a5c8466b700098109edcad5f2repo sync}
444baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
445baa3858d3f5d128a5c8466b700098109edcad5f2repo syncstatic const int kLiteralNextStates[kNumStates] = {0, 0, 0, 0, 1, 2, 3, 4,  5,  6,   4, 5};
446baa3858d3f5d128a5c8466b700098109edcad5f2repo syncstatic const int kMatchNextStates[kNumStates]   = {7, 7, 7, 7, 7, 7, 7, 10, 10, 10, 10, 10};
447baa3858d3f5d128a5c8466b700098109edcad5f2repo syncstatic const int kRepNextStates[kNumStates]     = {8, 8, 8, 8, 8, 8, 8, 11, 11, 11, 11, 11};
448baa3858d3f5d128a5c8466b700098109edcad5f2repo syncstatic const int kShortRepNextStates[kNumStates]= {9, 9, 9, 9, 9, 9, 9, 11, 11, 11, 11, 11};
449baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
450baa3858d3f5d128a5c8466b700098109edcad5f2repo sync#define IsCharState(s) ((s) < 7)
451baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
452baa3858d3f5d128a5c8466b700098109edcad5f2repo sync#define GetLenToPosState(len) (((len) < kNumLenToPosStates + 1) ? (len) - 2 : kNumLenToPosStates - 1)
453baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
454baa3858d3f5d128a5c8466b700098109edcad5f2repo sync#define kInfinityPrice (1 << 30)
455baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
456baa3858d3f5d128a5c8466b700098109edcad5f2repo syncstatic void RangeEnc_Construct(CRangeEnc *p)
457baa3858d3f5d128a5c8466b700098109edcad5f2repo sync{
458baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  p->outStream = 0;
459baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  p->bufBase = 0;
460baa3858d3f5d128a5c8466b700098109edcad5f2repo sync}
461baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
462baa3858d3f5d128a5c8466b700098109edcad5f2repo sync#define RangeEnc_GetProcessed(p) ((p)->processed + ((p)->buf - (p)->bufBase) + (p)->cacheSize)
463baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
464baa3858d3f5d128a5c8466b700098109edcad5f2repo sync#define RC_BUF_SIZE (1 << 16)
465baa3858d3f5d128a5c8466b700098109edcad5f2repo syncstatic int RangeEnc_Alloc(CRangeEnc *p, ISzAlloc *alloc)
466baa3858d3f5d128a5c8466b700098109edcad5f2repo sync{
467baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  if (p->bufBase == 0)
468baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  {
469baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    p->bufBase = (Byte *)alloc->Alloc(alloc, RC_BUF_SIZE);
470baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    if (p->bufBase == 0)
471baa3858d3f5d128a5c8466b700098109edcad5f2repo sync      return 0;
472baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    p->bufLim = p->bufBase + RC_BUF_SIZE;
473baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  }
474baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  return 1;
475baa3858d3f5d128a5c8466b700098109edcad5f2repo sync}
476baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
477baa3858d3f5d128a5c8466b700098109edcad5f2repo syncstatic void RangeEnc_Free(CRangeEnc *p, ISzAlloc *alloc)
478baa3858d3f5d128a5c8466b700098109edcad5f2repo sync{
479baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  alloc->Free(alloc, p->bufBase);
480baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  p->bufBase = 0;
481baa3858d3f5d128a5c8466b700098109edcad5f2repo sync}
482baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
483baa3858d3f5d128a5c8466b700098109edcad5f2repo syncstatic void RangeEnc_Init(CRangeEnc *p)
484baa3858d3f5d128a5c8466b700098109edcad5f2repo sync{
485baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  /* Stream.Init(); */
486baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  p->low = 0;
487baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  p->range = 0xFFFFFFFF;
488baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  p->cacheSize = 1;
489baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  p->cache = 0;
490baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
491baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  p->buf = p->bufBase;
492baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
493baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  p->processed = 0;
494baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  p->res = SZ_OK;
495baa3858d3f5d128a5c8466b700098109edcad5f2repo sync}
496baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
497baa3858d3f5d128a5c8466b700098109edcad5f2repo syncstatic void RangeEnc_FlushStream(CRangeEnc *p)
498baa3858d3f5d128a5c8466b700098109edcad5f2repo sync{
499baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  size_t num;
500baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  if (p->res != SZ_OK)
501baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    return;
502baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  num = p->buf - p->bufBase;
503baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  if (num != p->outStream->Write(p->outStream, p->bufBase, num))
504baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    p->res = SZ_ERROR_WRITE;
505baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  p->processed += num;
506baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  p->buf = p->bufBase;
507baa3858d3f5d128a5c8466b700098109edcad5f2repo sync}
508baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
509baa3858d3f5d128a5c8466b700098109edcad5f2repo syncstatic void MY_FAST_CALL RangeEnc_ShiftLow(CRangeEnc *p)
510baa3858d3f5d128a5c8466b700098109edcad5f2repo sync{
511baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  if ((UInt32)p->low < (UInt32)0xFF000000 || (int)(p->low >> 32) != 0)
512baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  {
513baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    Byte temp = p->cache;
514baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    do
515baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    {
516baa3858d3f5d128a5c8466b700098109edcad5f2repo sync      Byte *buf = p->buf;
517baa3858d3f5d128a5c8466b700098109edcad5f2repo sync      *buf++ = (Byte)(temp + (Byte)(p->low >> 32));
518baa3858d3f5d128a5c8466b700098109edcad5f2repo sync      p->buf = buf;
519baa3858d3f5d128a5c8466b700098109edcad5f2repo sync      if (buf == p->bufLim)
520baa3858d3f5d128a5c8466b700098109edcad5f2repo sync        RangeEnc_FlushStream(p);
521baa3858d3f5d128a5c8466b700098109edcad5f2repo sync      temp = 0xFF;
522baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    }
523baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    while (--p->cacheSize != 0);
524baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    p->cache = (Byte)((UInt32)p->low >> 24);
525baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  }
526baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  p->cacheSize++;
527baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  p->low = (UInt32)p->low << 8;
528baa3858d3f5d128a5c8466b700098109edcad5f2repo sync}
529baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
530baa3858d3f5d128a5c8466b700098109edcad5f2repo syncstatic void RangeEnc_FlushData(CRangeEnc *p)
531baa3858d3f5d128a5c8466b700098109edcad5f2repo sync{
532baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  int i;
533baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  for (i = 0; i < 5; i++)
534baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    RangeEnc_ShiftLow(p);
535baa3858d3f5d128a5c8466b700098109edcad5f2repo sync}
536baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
537baa3858d3f5d128a5c8466b700098109edcad5f2repo syncstatic void RangeEnc_EncodeDirectBits(CRangeEnc *p, UInt32 value, int numBits)
538baa3858d3f5d128a5c8466b700098109edcad5f2repo sync{
539baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  do
540baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  {
541baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    p->range >>= 1;
542baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    p->low += p->range & (0 - ((value >> --numBits) & 1));
543baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    if (p->range < kTopValue)
544baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    {
545baa3858d3f5d128a5c8466b700098109edcad5f2repo sync      p->range <<= 8;
546baa3858d3f5d128a5c8466b700098109edcad5f2repo sync      RangeEnc_ShiftLow(p);
547baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    }
548baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  }
549baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  while (numBits != 0);
550baa3858d3f5d128a5c8466b700098109edcad5f2repo sync}
551baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
552baa3858d3f5d128a5c8466b700098109edcad5f2repo syncstatic void RangeEnc_EncodeBit(CRangeEnc *p, CLzmaProb *prob, UInt32 symbol)
553baa3858d3f5d128a5c8466b700098109edcad5f2repo sync{
554baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  UInt32 ttt = *prob;
555baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  UInt32 newBound = (p->range >> kNumBitModelTotalBits) * ttt;
556baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  if (symbol == 0)
557baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  {
558baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    p->range = newBound;
559baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    ttt += (kBitModelTotal - ttt) >> kNumMoveBits;
560baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  }
561baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  else
562baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  {
563baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    p->low += newBound;
564baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    p->range -= newBound;
565baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    ttt -= ttt >> kNumMoveBits;
566baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  }
567baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  *prob = (CLzmaProb)ttt;
568baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  if (p->range < kTopValue)
569baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  {
570baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    p->range <<= 8;
571baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    RangeEnc_ShiftLow(p);
572baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  }
573baa3858d3f5d128a5c8466b700098109edcad5f2repo sync}
574baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
575baa3858d3f5d128a5c8466b700098109edcad5f2repo syncstatic void LitEnc_Encode(CRangeEnc *p, CLzmaProb *probs, UInt32 symbol)
576baa3858d3f5d128a5c8466b700098109edcad5f2repo sync{
577baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  symbol |= 0x100;
578baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  do
579baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  {
580baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    RangeEnc_EncodeBit(p, probs + (symbol >> 8), (symbol >> 7) & 1);
581baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    symbol <<= 1;
582baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  }
583baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  while (symbol < 0x10000);
584baa3858d3f5d128a5c8466b700098109edcad5f2repo sync}
585baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
586baa3858d3f5d128a5c8466b700098109edcad5f2repo syncstatic void LitEnc_EncodeMatched(CRangeEnc *p, CLzmaProb *probs, UInt32 symbol, UInt32 matchByte)
587baa3858d3f5d128a5c8466b700098109edcad5f2repo sync{
588baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  UInt32 offs = 0x100;
589baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  symbol |= 0x100;
590baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  do
591baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  {
592baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    matchByte <<= 1;
593baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    RangeEnc_EncodeBit(p, probs + (offs + (matchByte & offs) + (symbol >> 8)), (symbol >> 7) & 1);
594baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    symbol <<= 1;
595baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    offs &= ~(matchByte ^ symbol);
596baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  }
597baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  while (symbol < 0x10000);
598baa3858d3f5d128a5c8466b700098109edcad5f2repo sync}
599baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
600baa3858d3f5d128a5c8466b700098109edcad5f2repo syncvoid LzmaEnc_InitPriceTables(UInt32 *ProbPrices)
601baa3858d3f5d128a5c8466b700098109edcad5f2repo sync{
602baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  UInt32 i;
603baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  for (i = (1 << kNumMoveReducingBits) / 2; i < kBitModelTotal; i += (1 << kNumMoveReducingBits))
604baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  {
605baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    const int kCyclesBits = kNumBitPriceShiftBits;
606baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    UInt32 w = i;
607baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    UInt32 bitCount = 0;
608baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    int j;
609baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    for (j = 0; j < kCyclesBits; j++)
610baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    {
611baa3858d3f5d128a5c8466b700098109edcad5f2repo sync      w = w * w;
612baa3858d3f5d128a5c8466b700098109edcad5f2repo sync      bitCount <<= 1;
613baa3858d3f5d128a5c8466b700098109edcad5f2repo sync      while (w >= ((UInt32)1 << 16))
614baa3858d3f5d128a5c8466b700098109edcad5f2repo sync      {
615baa3858d3f5d128a5c8466b700098109edcad5f2repo sync        w >>= 1;
616baa3858d3f5d128a5c8466b700098109edcad5f2repo sync        bitCount++;
617baa3858d3f5d128a5c8466b700098109edcad5f2repo sync      }
618baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    }
619baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    ProbPrices[i >> kNumMoveReducingBits] = ((kNumBitModelTotalBits << kCyclesBits) - 15 - bitCount);
620baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  }
621baa3858d3f5d128a5c8466b700098109edcad5f2repo sync}
622baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
623baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
624baa3858d3f5d128a5c8466b700098109edcad5f2repo sync#define GET_PRICE(prob, symbol) \
625baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  p->ProbPrices[((prob) ^ (((-(int)(symbol))) & (kBitModelTotal - 1))) >> kNumMoveReducingBits];
626baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
627baa3858d3f5d128a5c8466b700098109edcad5f2repo sync#define GET_PRICEa(prob, symbol) \
628baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  ProbPrices[((prob) ^ ((-((int)(symbol))) & (kBitModelTotal - 1))) >> kNumMoveReducingBits];
629baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
630baa3858d3f5d128a5c8466b700098109edcad5f2repo sync#define GET_PRICE_0(prob) p->ProbPrices[(prob) >> kNumMoveReducingBits]
631baa3858d3f5d128a5c8466b700098109edcad5f2repo sync#define GET_PRICE_1(prob) p->ProbPrices[((prob) ^ (kBitModelTotal - 1)) >> kNumMoveReducingBits]
632baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
633baa3858d3f5d128a5c8466b700098109edcad5f2repo sync#define GET_PRICE_0a(prob) ProbPrices[(prob) >> kNumMoveReducingBits]
634baa3858d3f5d128a5c8466b700098109edcad5f2repo sync#define GET_PRICE_1a(prob) ProbPrices[((prob) ^ (kBitModelTotal - 1)) >> kNumMoveReducingBits]
635baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
636baa3858d3f5d128a5c8466b700098109edcad5f2repo syncstatic UInt32 LitEnc_GetPrice(const CLzmaProb *probs, UInt32 symbol, UInt32 *ProbPrices)
637baa3858d3f5d128a5c8466b700098109edcad5f2repo sync{
638baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  UInt32 price = 0;
639baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  symbol |= 0x100;
640baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  do
641baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  {
642baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    price += GET_PRICEa(probs[symbol >> 8], (symbol >> 7) & 1);
643baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    symbol <<= 1;
644baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  }
645baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  while (symbol < 0x10000);
646baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  return price;
647baa3858d3f5d128a5c8466b700098109edcad5f2repo sync}
648baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
649baa3858d3f5d128a5c8466b700098109edcad5f2repo syncstatic UInt32 LitEnc_GetPriceMatched(const CLzmaProb *probs, UInt32 symbol, UInt32 matchByte, UInt32 *ProbPrices)
650baa3858d3f5d128a5c8466b700098109edcad5f2repo sync{
651baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  UInt32 price = 0;
652baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  UInt32 offs = 0x100;
653baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  symbol |= 0x100;
654baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  do
655baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  {
656baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    matchByte <<= 1;
657baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    price += GET_PRICEa(probs[offs + (matchByte & offs) + (symbol >> 8)], (symbol >> 7) & 1);
658baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    symbol <<= 1;
659baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    offs &= ~(matchByte ^ symbol);
660baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  }
661baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  while (symbol < 0x10000);
662baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  return price;
663baa3858d3f5d128a5c8466b700098109edcad5f2repo sync}
664baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
665baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
666baa3858d3f5d128a5c8466b700098109edcad5f2repo syncstatic void RcTree_Encode(CRangeEnc *rc, CLzmaProb *probs, int numBitLevels, UInt32 symbol)
667baa3858d3f5d128a5c8466b700098109edcad5f2repo sync{
668baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  UInt32 m = 1;
669baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  int i;
670baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  for (i = numBitLevels; i != 0;)
671baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  {
672baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    UInt32 bit;
673baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    i--;
674baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    bit = (symbol >> i) & 1;
675baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    RangeEnc_EncodeBit(rc, probs + m, bit);
676baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    m = (m << 1) | bit;
677baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  }
678baa3858d3f5d128a5c8466b700098109edcad5f2repo sync}
679baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
680baa3858d3f5d128a5c8466b700098109edcad5f2repo syncstatic void RcTree_ReverseEncode(CRangeEnc *rc, CLzmaProb *probs, int numBitLevels, UInt32 symbol)
681baa3858d3f5d128a5c8466b700098109edcad5f2repo sync{
682baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  UInt32 m = 1;
683baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  int i;
684baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  for (i = 0; i < numBitLevels; i++)
685baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  {
686baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    UInt32 bit = symbol & 1;
687baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    RangeEnc_EncodeBit(rc, probs + m, bit);
688baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    m = (m << 1) | bit;
689baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    symbol >>= 1;
690baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  }
691baa3858d3f5d128a5c8466b700098109edcad5f2repo sync}
692baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
693baa3858d3f5d128a5c8466b700098109edcad5f2repo syncstatic UInt32 RcTree_GetPrice(const CLzmaProb *probs, int numBitLevels, UInt32 symbol, UInt32 *ProbPrices)
694baa3858d3f5d128a5c8466b700098109edcad5f2repo sync{
695baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  UInt32 price = 0;
696baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  symbol |= (1 << numBitLevels);
697baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  while (symbol != 1)
698baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  {
699baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    price += GET_PRICEa(probs[symbol >> 1], symbol & 1);
700baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    symbol >>= 1;
701baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  }
702baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  return price;
703baa3858d3f5d128a5c8466b700098109edcad5f2repo sync}
704baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
705baa3858d3f5d128a5c8466b700098109edcad5f2repo syncstatic UInt32 RcTree_ReverseGetPrice(const CLzmaProb *probs, int numBitLevels, UInt32 symbol, UInt32 *ProbPrices)
706baa3858d3f5d128a5c8466b700098109edcad5f2repo sync{
707baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  UInt32 price = 0;
708baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  UInt32 m = 1;
709baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  int i;
710baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  for (i = numBitLevels; i != 0; i--)
711baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  {
712baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    UInt32 bit = symbol & 1;
713baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    symbol >>= 1;
714baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    price += GET_PRICEa(probs[m], bit);
715baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    m = (m << 1) | bit;
716baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  }
717baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  return price;
718baa3858d3f5d128a5c8466b700098109edcad5f2repo sync}
719baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
720baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
721baa3858d3f5d128a5c8466b700098109edcad5f2repo syncstatic void LenEnc_Init(CLenEnc *p)
722baa3858d3f5d128a5c8466b700098109edcad5f2repo sync{
723baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  unsigned i;
724baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  p->choice = p->choice2 = kProbInitValue;
725baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  for (i = 0; i < (LZMA_NUM_PB_STATES_MAX << kLenNumLowBits); i++)
726baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    p->low[i] = kProbInitValue;
727baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  for (i = 0; i < (LZMA_NUM_PB_STATES_MAX << kLenNumMidBits); i++)
728baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    p->mid[i] = kProbInitValue;
729baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  for (i = 0; i < kLenNumHighSymbols; i++)
730baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    p->high[i] = kProbInitValue;
731baa3858d3f5d128a5c8466b700098109edcad5f2repo sync}
732baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
733baa3858d3f5d128a5c8466b700098109edcad5f2repo syncstatic void LenEnc_Encode(CLenEnc *p, CRangeEnc *rc, UInt32 symbol, UInt32 posState)
734baa3858d3f5d128a5c8466b700098109edcad5f2repo sync{
735baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  if (symbol < kLenNumLowSymbols)
736baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  {
737baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    RangeEnc_EncodeBit(rc, &p->choice, 0);
738baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    RcTree_Encode(rc, p->low + (posState << kLenNumLowBits), kLenNumLowBits, symbol);
739baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  }
740baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  else
741baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  {
742baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    RangeEnc_EncodeBit(rc, &p->choice, 1);
743baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    if (symbol < kLenNumLowSymbols + kLenNumMidSymbols)
744baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    {
745baa3858d3f5d128a5c8466b700098109edcad5f2repo sync      RangeEnc_EncodeBit(rc, &p->choice2, 0);
746baa3858d3f5d128a5c8466b700098109edcad5f2repo sync      RcTree_Encode(rc, p->mid + (posState << kLenNumMidBits), kLenNumMidBits, symbol - kLenNumLowSymbols);
747baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    }
748baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    else
749baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    {
750baa3858d3f5d128a5c8466b700098109edcad5f2repo sync      RangeEnc_EncodeBit(rc, &p->choice2, 1);
751baa3858d3f5d128a5c8466b700098109edcad5f2repo sync      RcTree_Encode(rc, p->high, kLenNumHighBits, symbol - kLenNumLowSymbols - kLenNumMidSymbols);
752baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    }
753baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  }
754baa3858d3f5d128a5c8466b700098109edcad5f2repo sync}
755baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
756baa3858d3f5d128a5c8466b700098109edcad5f2repo syncstatic void LenEnc_SetPrices(CLenEnc *p, UInt32 posState, UInt32 numSymbols, UInt32 *prices, UInt32 *ProbPrices)
757baa3858d3f5d128a5c8466b700098109edcad5f2repo sync{
758baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  UInt32 a0 = GET_PRICE_0a(p->choice);
759baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  UInt32 a1 = GET_PRICE_1a(p->choice);
760baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  UInt32 b0 = a1 + GET_PRICE_0a(p->choice2);
761baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  UInt32 b1 = a1 + GET_PRICE_1a(p->choice2);
762baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  UInt32 i = 0;
763baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  for (i = 0; i < kLenNumLowSymbols; i++)
764baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  {
765baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    if (i >= numSymbols)
766baa3858d3f5d128a5c8466b700098109edcad5f2repo sync      return;
767baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    prices[i] = a0 + RcTree_GetPrice(p->low + (posState << kLenNumLowBits), kLenNumLowBits, i, ProbPrices);
768baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  }
769baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  for (; i < kLenNumLowSymbols + kLenNumMidSymbols; i++)
770baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  {
771baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    if (i >= numSymbols)
772baa3858d3f5d128a5c8466b700098109edcad5f2repo sync      return;
773baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    prices[i] = b0 + RcTree_GetPrice(p->mid + (posState << kLenNumMidBits), kLenNumMidBits, i - kLenNumLowSymbols, ProbPrices);
774baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  }
775baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  for (; i < numSymbols; i++)
776baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    prices[i] = b1 + RcTree_GetPrice(p->high, kLenNumHighBits, i - kLenNumLowSymbols - kLenNumMidSymbols, ProbPrices);
777baa3858d3f5d128a5c8466b700098109edcad5f2repo sync}
778baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
779baa3858d3f5d128a5c8466b700098109edcad5f2repo syncstatic void MY_FAST_CALL LenPriceEnc_UpdateTable(CLenPriceEnc *p, UInt32 posState, UInt32 *ProbPrices)
780baa3858d3f5d128a5c8466b700098109edcad5f2repo sync{
781baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  LenEnc_SetPrices(&p->p, posState, p->tableSize, p->prices[posState], ProbPrices);
782baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  p->counters[posState] = p->tableSize;
783baa3858d3f5d128a5c8466b700098109edcad5f2repo sync}
784baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
785baa3858d3f5d128a5c8466b700098109edcad5f2repo syncstatic void LenPriceEnc_UpdateTables(CLenPriceEnc *p, UInt32 numPosStates, UInt32 *ProbPrices)
786baa3858d3f5d128a5c8466b700098109edcad5f2repo sync{
787baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  UInt32 posState;
788baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  for (posState = 0; posState < numPosStates; posState++)
789baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    LenPriceEnc_UpdateTable(p, posState, ProbPrices);
790baa3858d3f5d128a5c8466b700098109edcad5f2repo sync}
791baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
792baa3858d3f5d128a5c8466b700098109edcad5f2repo syncstatic void LenEnc_Encode2(CLenPriceEnc *p, CRangeEnc *rc, UInt32 symbol, UInt32 posState, Bool updatePrice, UInt32 *ProbPrices)
793baa3858d3f5d128a5c8466b700098109edcad5f2repo sync{
794baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  LenEnc_Encode(&p->p, rc, symbol, posState);
795baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  if (updatePrice)
796baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    if (--p->counters[posState] == 0)
797baa3858d3f5d128a5c8466b700098109edcad5f2repo sync      LenPriceEnc_UpdateTable(p, posState, ProbPrices);
798baa3858d3f5d128a5c8466b700098109edcad5f2repo sync}
799baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
800baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
801baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
802baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
803baa3858d3f5d128a5c8466b700098109edcad5f2repo syncstatic void MovePos(CLzmaEnc *p, UInt32 num)
804baa3858d3f5d128a5c8466b700098109edcad5f2repo sync{
805baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  #ifdef SHOW_STAT
806baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  ttt += num;
807baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  printf("\n MovePos %d", num);
808baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  #endif
809baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  if (num != 0)
810baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  {
811baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    p->additionalOffset += num;
812baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    p->matchFinder.Skip(p->matchFinderObj, num);
813baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  }
814baa3858d3f5d128a5c8466b700098109edcad5f2repo sync}
815baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
816baa3858d3f5d128a5c8466b700098109edcad5f2repo syncstatic UInt32 ReadMatchDistances(CLzmaEnc *p, UInt32 *numDistancePairsRes)
817baa3858d3f5d128a5c8466b700098109edcad5f2repo sync{
818baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  UInt32 lenRes = 0, numPairs;
819baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  p->numAvail = p->matchFinder.GetNumAvailableBytes(p->matchFinderObj);
820baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  numPairs = p->matchFinder.GetMatches(p->matchFinderObj, p->matches);
821baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  #ifdef SHOW_STAT
822baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  printf("\n i = %d numPairs = %d    ", ttt, numPairs / 2);
823baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  ttt++;
824baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  {
825baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    UInt32 i;
826baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    for (i = 0; i < numPairs; i += 2)
827baa3858d3f5d128a5c8466b700098109edcad5f2repo sync      printf("%2d %6d   | ", p->matches[i], p->matches[i + 1]);
828baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  }
829baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  #endif
830baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  if (numPairs > 0)
831baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  {
832baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    lenRes = p->matches[numPairs - 2];
833baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    if (lenRes == p->numFastBytes)
834baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    {
835baa3858d3f5d128a5c8466b700098109edcad5f2repo sync      const Byte *pby = p->matchFinder.GetPointerToCurrentPos(p->matchFinderObj) - 1;
836baa3858d3f5d128a5c8466b700098109edcad5f2repo sync      UInt32 distance = p->matches[numPairs - 1] + 1;
837baa3858d3f5d128a5c8466b700098109edcad5f2repo sync      UInt32 numAvail = p->numAvail;
838baa3858d3f5d128a5c8466b700098109edcad5f2repo sync      if (numAvail > LZMA_MATCH_LEN_MAX)
839baa3858d3f5d128a5c8466b700098109edcad5f2repo sync        numAvail = LZMA_MATCH_LEN_MAX;
840baa3858d3f5d128a5c8466b700098109edcad5f2repo sync      {
841baa3858d3f5d128a5c8466b700098109edcad5f2repo sync        const Byte *pby2 = pby - distance;
842baa3858d3f5d128a5c8466b700098109edcad5f2repo sync        for (; lenRes < numAvail && pby[lenRes] == pby2[lenRes]; lenRes++);
843baa3858d3f5d128a5c8466b700098109edcad5f2repo sync      }
844baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    }
845baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  }
846baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  p->additionalOffset++;
847baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  *numDistancePairsRes = numPairs;
848baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  return lenRes;
849baa3858d3f5d128a5c8466b700098109edcad5f2repo sync}
850baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
851baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
852baa3858d3f5d128a5c8466b700098109edcad5f2repo sync#define MakeAsChar(p) (p)->backPrev = (UInt32)(-1); (p)->prev1IsChar = False;
853baa3858d3f5d128a5c8466b700098109edcad5f2repo sync#define MakeAsShortRep(p) (p)->backPrev = 0; (p)->prev1IsChar = False;
854baa3858d3f5d128a5c8466b700098109edcad5f2repo sync#define IsShortRep(p) ((p)->backPrev == 0)
855baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
856baa3858d3f5d128a5c8466b700098109edcad5f2repo syncstatic UInt32 GetRepLen1Price(CLzmaEnc *p, UInt32 state, UInt32 posState)
857baa3858d3f5d128a5c8466b700098109edcad5f2repo sync{
858baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  return
859baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    GET_PRICE_0(p->isRepG0[state]) +
860baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    GET_PRICE_0(p->isRep0Long[state][posState]);
861baa3858d3f5d128a5c8466b700098109edcad5f2repo sync}
862baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
863baa3858d3f5d128a5c8466b700098109edcad5f2repo syncstatic UInt32 GetPureRepPrice(CLzmaEnc *p, UInt32 repIndex, UInt32 state, UInt32 posState)
864baa3858d3f5d128a5c8466b700098109edcad5f2repo sync{
865baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  UInt32 price;
866baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  if (repIndex == 0)
867baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  {
868baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    price = GET_PRICE_0(p->isRepG0[state]);
869baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    price += GET_PRICE_1(p->isRep0Long[state][posState]);
870baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  }
871baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  else
872baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  {
873baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    price = GET_PRICE_1(p->isRepG0[state]);
874baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    if (repIndex == 1)
875baa3858d3f5d128a5c8466b700098109edcad5f2repo sync      price += GET_PRICE_0(p->isRepG1[state]);
876baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    else
877baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    {
878baa3858d3f5d128a5c8466b700098109edcad5f2repo sync      price += GET_PRICE_1(p->isRepG1[state]);
879baa3858d3f5d128a5c8466b700098109edcad5f2repo sync      price += GET_PRICE(p->isRepG2[state], repIndex - 2);
880baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    }
881baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  }
882baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  return price;
883baa3858d3f5d128a5c8466b700098109edcad5f2repo sync}
884baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
885baa3858d3f5d128a5c8466b700098109edcad5f2repo syncstatic UInt32 GetRepPrice(CLzmaEnc *p, UInt32 repIndex, UInt32 len, UInt32 state, UInt32 posState)
886baa3858d3f5d128a5c8466b700098109edcad5f2repo sync{
887baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  return p->repLenEnc.prices[posState][len - LZMA_MATCH_LEN_MIN] +
888baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    GetPureRepPrice(p, repIndex, state, posState);
889baa3858d3f5d128a5c8466b700098109edcad5f2repo sync}
890baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
891baa3858d3f5d128a5c8466b700098109edcad5f2repo syncstatic UInt32 Backward(CLzmaEnc *p, UInt32 *backRes, UInt32 cur)
892baa3858d3f5d128a5c8466b700098109edcad5f2repo sync{
893baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  UInt32 posMem = p->opt[cur].posPrev;
894baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  UInt32 backMem = p->opt[cur].backPrev;
895baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  p->optimumEndIndex = cur;
896baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  do
897baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  {
898baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    if (p->opt[cur].prev1IsChar)
899baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    {
900baa3858d3f5d128a5c8466b700098109edcad5f2repo sync      MakeAsChar(&p->opt[posMem])
901baa3858d3f5d128a5c8466b700098109edcad5f2repo sync      p->opt[posMem].posPrev = posMem - 1;
902baa3858d3f5d128a5c8466b700098109edcad5f2repo sync      if (p->opt[cur].prev2)
903baa3858d3f5d128a5c8466b700098109edcad5f2repo sync      {
904baa3858d3f5d128a5c8466b700098109edcad5f2repo sync        p->opt[posMem - 1].prev1IsChar = False;
905baa3858d3f5d128a5c8466b700098109edcad5f2repo sync        p->opt[posMem - 1].posPrev = p->opt[cur].posPrev2;
906baa3858d3f5d128a5c8466b700098109edcad5f2repo sync        p->opt[posMem - 1].backPrev = p->opt[cur].backPrev2;
907baa3858d3f5d128a5c8466b700098109edcad5f2repo sync      }
908baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    }
909baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    {
910baa3858d3f5d128a5c8466b700098109edcad5f2repo sync      UInt32 posPrev = posMem;
911baa3858d3f5d128a5c8466b700098109edcad5f2repo sync      UInt32 backCur = backMem;
912baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
913baa3858d3f5d128a5c8466b700098109edcad5f2repo sync      backMem = p->opt[posPrev].backPrev;
914baa3858d3f5d128a5c8466b700098109edcad5f2repo sync      posMem = p->opt[posPrev].posPrev;
915baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
916baa3858d3f5d128a5c8466b700098109edcad5f2repo sync      p->opt[posPrev].backPrev = backCur;
917baa3858d3f5d128a5c8466b700098109edcad5f2repo sync      p->opt[posPrev].posPrev = cur;
918baa3858d3f5d128a5c8466b700098109edcad5f2repo sync      cur = posPrev;
919baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    }
920baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  }
921baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  while (cur != 0);
922baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  *backRes = p->opt[0].backPrev;
923baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  p->optimumCurrentIndex  = p->opt[0].posPrev;
924baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  return p->optimumCurrentIndex;
925baa3858d3f5d128a5c8466b700098109edcad5f2repo sync}
926baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
927baa3858d3f5d128a5c8466b700098109edcad5f2repo sync#define LIT_PROBS(pos, prevByte) (p->litProbs + ((((pos) & p->lpMask) << p->lc) + ((prevByte) >> (8 - p->lc))) * 0x300)
928baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
929baa3858d3f5d128a5c8466b700098109edcad5f2repo syncstatic UInt32 GetOptimum(CLzmaEnc *p, UInt32 position, UInt32 *backRes)
930baa3858d3f5d128a5c8466b700098109edcad5f2repo sync{
931baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  UInt32 numAvail, mainLen, numPairs, repMaxIndex, i, posState, lenEnd, len, cur;
932baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  UInt32 matchPrice, repMatchPrice, normalMatchPrice;
933baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  UInt32 reps[LZMA_NUM_REPS], repLens[LZMA_NUM_REPS];
934baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  UInt32 *matches;
935baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  const Byte *data;
936baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  Byte curByte, matchByte;
937baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  if (p->optimumEndIndex != p->optimumCurrentIndex)
938baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  {
939baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    const COptimal *opt = &p->opt[p->optimumCurrentIndex];
940baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    UInt32 lenRes = opt->posPrev - p->optimumCurrentIndex;
941baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    *backRes = opt->backPrev;
942baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    p->optimumCurrentIndex = opt->posPrev;
943baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    return lenRes;
944baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  }
945baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  p->optimumCurrentIndex = p->optimumEndIndex = 0;
946baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
947baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  if (p->additionalOffset == 0)
948baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    mainLen = ReadMatchDistances(p, &numPairs);
949baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  else
950baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  {
951baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    mainLen = p->longestMatchLength;
952baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    numPairs = p->numPairs;
953baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  }
954baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
955baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  numAvail = p->numAvail;
956baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  if (numAvail < 2)
957baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  {
958baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    *backRes = (UInt32)(-1);
959baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    return 1;
960baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  }
961baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  if (numAvail > LZMA_MATCH_LEN_MAX)
962baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    numAvail = LZMA_MATCH_LEN_MAX;
963baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
964baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  data = p->matchFinder.GetPointerToCurrentPos(p->matchFinderObj) - 1;
965baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  repMaxIndex = 0;
966baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  for (i = 0; i < LZMA_NUM_REPS; i++)
967baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  {
968baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    UInt32 lenTest;
969baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    const Byte *data2;
970baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    reps[i] = p->reps[i];
971baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    data2 = data - (reps[i] + 1);
972baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    if (data[0] != data2[0] || data[1] != data2[1])
973baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    {
974baa3858d3f5d128a5c8466b700098109edcad5f2repo sync      repLens[i] = 0;
975baa3858d3f5d128a5c8466b700098109edcad5f2repo sync      continue;
976baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    }
977baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    for (lenTest = 2; lenTest < numAvail && data[lenTest] == data2[lenTest]; lenTest++);
978baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    repLens[i] = lenTest;
979baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    if (lenTest > repLens[repMaxIndex])
980baa3858d3f5d128a5c8466b700098109edcad5f2repo sync      repMaxIndex = i;
981baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  }
982baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  if (repLens[repMaxIndex] >= p->numFastBytes)
983baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  {
984baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    UInt32 lenRes;
985baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    *backRes = repMaxIndex;
986baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    lenRes = repLens[repMaxIndex];
987baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    MovePos(p, lenRes - 1);
988baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    return lenRes;
989baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  }
990baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
991baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  matches = p->matches;
992baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  if (mainLen >= p->numFastBytes)
993baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  {
994baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    *backRes = matches[numPairs - 1] + LZMA_NUM_REPS;
995baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    MovePos(p, mainLen - 1);
996baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    return mainLen;
997baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  }
998baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  curByte = *data;
999baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  matchByte = *(data - (reps[0] + 1));
1000baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
1001baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  if (mainLen < 2 && curByte != matchByte && repLens[repMaxIndex] < 2)
1002baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  {
1003baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    *backRes = (UInt32)-1;
1004baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    return 1;
1005baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  }
1006baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
1007baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  p->opt[0].state = (CState)p->state;
1008baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
1009baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  posState = (position & p->pbMask);
1010baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
1011baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  {
1012baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    const CLzmaProb *probs = LIT_PROBS(position, *(data - 1));
1013baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    p->opt[1].price = GET_PRICE_0(p->isMatch[p->state][posState]) +
1014baa3858d3f5d128a5c8466b700098109edcad5f2repo sync        (!IsCharState(p->state) ?
1015baa3858d3f5d128a5c8466b700098109edcad5f2repo sync          LitEnc_GetPriceMatched(probs, curByte, matchByte, p->ProbPrices) :
1016baa3858d3f5d128a5c8466b700098109edcad5f2repo sync          LitEnc_GetPrice(probs, curByte, p->ProbPrices));
1017baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  }
1018baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
1019baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  MakeAsChar(&p->opt[1]);
1020baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
1021baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  matchPrice = GET_PRICE_1(p->isMatch[p->state][posState]);
1022baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  repMatchPrice = matchPrice + GET_PRICE_1(p->isRep[p->state]);
1023baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
1024baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  if (matchByte == curByte)
1025baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  {
1026baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    UInt32 shortRepPrice = repMatchPrice + GetRepLen1Price(p, p->state, posState);
1027baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    if (shortRepPrice < p->opt[1].price)
1028baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    {
1029baa3858d3f5d128a5c8466b700098109edcad5f2repo sync      p->opt[1].price = shortRepPrice;
1030baa3858d3f5d128a5c8466b700098109edcad5f2repo sync      MakeAsShortRep(&p->opt[1]);
1031baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    }
1032baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  }
1033baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  lenEnd = ((mainLen >= repLens[repMaxIndex]) ? mainLen : repLens[repMaxIndex]);
1034baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
1035baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  if (lenEnd < 2)
1036baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  {
1037baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    *backRes = p->opt[1].backPrev;
1038baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    return 1;
1039baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  }
1040baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
1041baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  p->opt[1].posPrev = 0;
1042baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  for (i = 0; i < LZMA_NUM_REPS; i++)
1043baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    p->opt[0].backs[i] = reps[i];
1044baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
1045baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  len = lenEnd;
1046baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  do
1047baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    p->opt[len--].price = kInfinityPrice;
1048baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  while (len >= 2);
1049baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
1050baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  for (i = 0; i < LZMA_NUM_REPS; i++)
1051baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  {
1052baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    UInt32 repLen = repLens[i];
1053baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    UInt32 price;
1054baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    if (repLen < 2)
1055baa3858d3f5d128a5c8466b700098109edcad5f2repo sync      continue;
1056baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    price = repMatchPrice + GetPureRepPrice(p, i, p->state, posState);
1057baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    do
1058baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    {
1059baa3858d3f5d128a5c8466b700098109edcad5f2repo sync      UInt32 curAndLenPrice = price + p->repLenEnc.prices[posState][repLen - 2];
1060baa3858d3f5d128a5c8466b700098109edcad5f2repo sync      COptimal *opt = &p->opt[repLen];
1061baa3858d3f5d128a5c8466b700098109edcad5f2repo sync      if (curAndLenPrice < opt->price)
1062baa3858d3f5d128a5c8466b700098109edcad5f2repo sync      {
1063baa3858d3f5d128a5c8466b700098109edcad5f2repo sync        opt->price = curAndLenPrice;
1064baa3858d3f5d128a5c8466b700098109edcad5f2repo sync        opt->posPrev = 0;
1065baa3858d3f5d128a5c8466b700098109edcad5f2repo sync        opt->backPrev = i;
1066baa3858d3f5d128a5c8466b700098109edcad5f2repo sync        opt->prev1IsChar = False;
1067baa3858d3f5d128a5c8466b700098109edcad5f2repo sync      }
1068baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    }
1069baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    while (--repLen >= 2);
1070baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  }
1071baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
1072baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  normalMatchPrice = matchPrice + GET_PRICE_0(p->isRep[p->state]);
1073baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
1074baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  len = ((repLens[0] >= 2) ? repLens[0] + 1 : 2);
1075baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  if (len <= mainLen)
1076baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  {
1077baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    UInt32 offs = 0;
1078baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    while (len > matches[offs])
1079baa3858d3f5d128a5c8466b700098109edcad5f2repo sync      offs += 2;
1080baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    for (; ; len++)
1081baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    {
1082baa3858d3f5d128a5c8466b700098109edcad5f2repo sync      COptimal *opt;
1083baa3858d3f5d128a5c8466b700098109edcad5f2repo sync      UInt32 distance = matches[offs + 1];
1084baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
1085baa3858d3f5d128a5c8466b700098109edcad5f2repo sync      UInt32 curAndLenPrice = normalMatchPrice + p->lenEnc.prices[posState][len - LZMA_MATCH_LEN_MIN];
1086baa3858d3f5d128a5c8466b700098109edcad5f2repo sync      UInt32 lenToPosState = GetLenToPosState(len);
1087baa3858d3f5d128a5c8466b700098109edcad5f2repo sync      if (distance < kNumFullDistances)
1088baa3858d3f5d128a5c8466b700098109edcad5f2repo sync        curAndLenPrice += p->distancesPrices[lenToPosState][distance];
1089baa3858d3f5d128a5c8466b700098109edcad5f2repo sync      else
1090baa3858d3f5d128a5c8466b700098109edcad5f2repo sync      {
1091baa3858d3f5d128a5c8466b700098109edcad5f2repo sync        UInt32 slot;
1092baa3858d3f5d128a5c8466b700098109edcad5f2repo sync        GetPosSlot2(distance, slot);
1093baa3858d3f5d128a5c8466b700098109edcad5f2repo sync        curAndLenPrice += p->alignPrices[distance & kAlignMask] + p->posSlotPrices[lenToPosState][slot];
1094baa3858d3f5d128a5c8466b700098109edcad5f2repo sync      }
1095baa3858d3f5d128a5c8466b700098109edcad5f2repo sync      opt = &p->opt[len];
1096baa3858d3f5d128a5c8466b700098109edcad5f2repo sync      if (curAndLenPrice < opt->price)
1097baa3858d3f5d128a5c8466b700098109edcad5f2repo sync      {
1098baa3858d3f5d128a5c8466b700098109edcad5f2repo sync        opt->price = curAndLenPrice;
1099baa3858d3f5d128a5c8466b700098109edcad5f2repo sync        opt->posPrev = 0;
1100baa3858d3f5d128a5c8466b700098109edcad5f2repo sync        opt->backPrev = distance + LZMA_NUM_REPS;
1101baa3858d3f5d128a5c8466b700098109edcad5f2repo sync        opt->prev1IsChar = False;
1102baa3858d3f5d128a5c8466b700098109edcad5f2repo sync      }
1103baa3858d3f5d128a5c8466b700098109edcad5f2repo sync      if (len == matches[offs])
1104baa3858d3f5d128a5c8466b700098109edcad5f2repo sync      {
1105baa3858d3f5d128a5c8466b700098109edcad5f2repo sync        offs += 2;
1106baa3858d3f5d128a5c8466b700098109edcad5f2repo sync        if (offs == numPairs)
1107baa3858d3f5d128a5c8466b700098109edcad5f2repo sync          break;
1108baa3858d3f5d128a5c8466b700098109edcad5f2repo sync      }
1109baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    }
1110baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  }
1111baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
1112baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  cur = 0;
1113baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
1114baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    #ifdef SHOW_STAT2
1115baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    if (position >= 0)
1116baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    {
1117baa3858d3f5d128a5c8466b700098109edcad5f2repo sync      unsigned i;
1118baa3858d3f5d128a5c8466b700098109edcad5f2repo sync      printf("\n pos = %4X", position);
1119baa3858d3f5d128a5c8466b700098109edcad5f2repo sync      for (i = cur; i <= lenEnd; i++)
1120baa3858d3f5d128a5c8466b700098109edcad5f2repo sync      printf("\nprice[%4X] = %d", position - cur + i, p->opt[i].price);
1121baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    }
1122baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    #endif
1123baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
1124baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  for (;;)
1125baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  {
1126baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    UInt32 numAvailFull, newLen, numPairs, posPrev, state, posState, startLen;
1127baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    UInt32 curPrice, curAnd1Price, matchPrice, repMatchPrice;
1128baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    Bool nextIsChar;
1129baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    Byte curByte, matchByte;
1130baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    const Byte *data;
1131baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    COptimal *curOpt;
1132baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    COptimal *nextOpt;
1133baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
1134baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    cur++;
1135baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    if (cur == lenEnd)
1136baa3858d3f5d128a5c8466b700098109edcad5f2repo sync      return Backward(p, backRes, cur);
1137baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
1138baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    newLen = ReadMatchDistances(p, &numPairs);
1139baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    if (newLen >= p->numFastBytes)
1140baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    {
1141baa3858d3f5d128a5c8466b700098109edcad5f2repo sync      p->numPairs = numPairs;
1142baa3858d3f5d128a5c8466b700098109edcad5f2repo sync      p->longestMatchLength = newLen;
1143baa3858d3f5d128a5c8466b700098109edcad5f2repo sync      return Backward(p, backRes, cur);
1144baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    }
1145baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    position++;
1146baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    curOpt = &p->opt[cur];
1147baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    posPrev = curOpt->posPrev;
1148baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    if (curOpt->prev1IsChar)
1149baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    {
1150baa3858d3f5d128a5c8466b700098109edcad5f2repo sync      posPrev--;
1151baa3858d3f5d128a5c8466b700098109edcad5f2repo sync      if (curOpt->prev2)
1152baa3858d3f5d128a5c8466b700098109edcad5f2repo sync      {
1153baa3858d3f5d128a5c8466b700098109edcad5f2repo sync        state = p->opt[curOpt->posPrev2].state;
1154baa3858d3f5d128a5c8466b700098109edcad5f2repo sync        if (curOpt->backPrev2 < LZMA_NUM_REPS)
1155baa3858d3f5d128a5c8466b700098109edcad5f2repo sync          state = kRepNextStates[state];
1156baa3858d3f5d128a5c8466b700098109edcad5f2repo sync        else
1157baa3858d3f5d128a5c8466b700098109edcad5f2repo sync          state = kMatchNextStates[state];
1158baa3858d3f5d128a5c8466b700098109edcad5f2repo sync      }
1159baa3858d3f5d128a5c8466b700098109edcad5f2repo sync      else
1160baa3858d3f5d128a5c8466b700098109edcad5f2repo sync        state = p->opt[posPrev].state;
1161baa3858d3f5d128a5c8466b700098109edcad5f2repo sync      state = kLiteralNextStates[state];
1162baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    }
1163baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    else
1164baa3858d3f5d128a5c8466b700098109edcad5f2repo sync      state = p->opt[posPrev].state;
1165baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    if (posPrev == cur - 1)
1166baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    {
1167baa3858d3f5d128a5c8466b700098109edcad5f2repo sync      if (IsShortRep(curOpt))
1168baa3858d3f5d128a5c8466b700098109edcad5f2repo sync        state = kShortRepNextStates[state];
1169baa3858d3f5d128a5c8466b700098109edcad5f2repo sync      else
1170baa3858d3f5d128a5c8466b700098109edcad5f2repo sync        state = kLiteralNextStates[state];
1171baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    }
1172baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    else
1173baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    {
1174baa3858d3f5d128a5c8466b700098109edcad5f2repo sync      UInt32 pos;
1175baa3858d3f5d128a5c8466b700098109edcad5f2repo sync      const COptimal *prevOpt;
1176baa3858d3f5d128a5c8466b700098109edcad5f2repo sync      if (curOpt->prev1IsChar && curOpt->prev2)
1177baa3858d3f5d128a5c8466b700098109edcad5f2repo sync      {
1178baa3858d3f5d128a5c8466b700098109edcad5f2repo sync        posPrev = curOpt->posPrev2;
1179baa3858d3f5d128a5c8466b700098109edcad5f2repo sync        pos = curOpt->backPrev2;
1180baa3858d3f5d128a5c8466b700098109edcad5f2repo sync        state = kRepNextStates[state];
1181baa3858d3f5d128a5c8466b700098109edcad5f2repo sync      }
1182baa3858d3f5d128a5c8466b700098109edcad5f2repo sync      else
1183baa3858d3f5d128a5c8466b700098109edcad5f2repo sync      {
1184baa3858d3f5d128a5c8466b700098109edcad5f2repo sync        pos = curOpt->backPrev;
1185baa3858d3f5d128a5c8466b700098109edcad5f2repo sync        if (pos < LZMA_NUM_REPS)
1186baa3858d3f5d128a5c8466b700098109edcad5f2repo sync          state = kRepNextStates[state];
1187baa3858d3f5d128a5c8466b700098109edcad5f2repo sync        else
1188baa3858d3f5d128a5c8466b700098109edcad5f2repo sync          state = kMatchNextStates[state];
1189baa3858d3f5d128a5c8466b700098109edcad5f2repo sync      }
1190baa3858d3f5d128a5c8466b700098109edcad5f2repo sync      prevOpt = &p->opt[posPrev];
1191baa3858d3f5d128a5c8466b700098109edcad5f2repo sync      if (pos < LZMA_NUM_REPS)
1192baa3858d3f5d128a5c8466b700098109edcad5f2repo sync      {
1193baa3858d3f5d128a5c8466b700098109edcad5f2repo sync        UInt32 i;
1194baa3858d3f5d128a5c8466b700098109edcad5f2repo sync        reps[0] = prevOpt->backs[pos];
1195baa3858d3f5d128a5c8466b700098109edcad5f2repo sync        for (i = 1; i <= pos; i++)
1196baa3858d3f5d128a5c8466b700098109edcad5f2repo sync          reps[i] = prevOpt->backs[i - 1];
1197baa3858d3f5d128a5c8466b700098109edcad5f2repo sync        for (; i < LZMA_NUM_REPS; i++)
1198baa3858d3f5d128a5c8466b700098109edcad5f2repo sync          reps[i] = prevOpt->backs[i];
1199baa3858d3f5d128a5c8466b700098109edcad5f2repo sync      }
1200baa3858d3f5d128a5c8466b700098109edcad5f2repo sync      else
1201baa3858d3f5d128a5c8466b700098109edcad5f2repo sync      {
1202baa3858d3f5d128a5c8466b700098109edcad5f2repo sync        UInt32 i;
1203baa3858d3f5d128a5c8466b700098109edcad5f2repo sync        reps[0] = (pos - LZMA_NUM_REPS);
1204baa3858d3f5d128a5c8466b700098109edcad5f2repo sync        for (i = 1; i < LZMA_NUM_REPS; i++)
1205baa3858d3f5d128a5c8466b700098109edcad5f2repo sync          reps[i] = prevOpt->backs[i - 1];
1206baa3858d3f5d128a5c8466b700098109edcad5f2repo sync      }
1207baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    }
1208baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    curOpt->state = (CState)state;
1209baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
1210baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    curOpt->backs[0] = reps[0];
1211baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    curOpt->backs[1] = reps[1];
1212baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    curOpt->backs[2] = reps[2];
1213baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    curOpt->backs[3] = reps[3];
1214baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
1215baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    curPrice = curOpt->price;
1216baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    nextIsChar = False;
1217baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    data = p->matchFinder.GetPointerToCurrentPos(p->matchFinderObj) - 1;
1218baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    curByte = *data;
1219baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    matchByte = *(data - (reps[0] + 1));
1220baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
1221baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    posState = (position & p->pbMask);
1222baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
1223baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    curAnd1Price = curPrice + GET_PRICE_0(p->isMatch[state][posState]);
1224baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    {
1225baa3858d3f5d128a5c8466b700098109edcad5f2repo sync      const CLzmaProb *probs = LIT_PROBS(position, *(data - 1));
1226baa3858d3f5d128a5c8466b700098109edcad5f2repo sync      curAnd1Price +=
1227baa3858d3f5d128a5c8466b700098109edcad5f2repo sync        (!IsCharState(state) ?
1228baa3858d3f5d128a5c8466b700098109edcad5f2repo sync          LitEnc_GetPriceMatched(probs, curByte, matchByte, p->ProbPrices) :
1229baa3858d3f5d128a5c8466b700098109edcad5f2repo sync          LitEnc_GetPrice(probs, curByte, p->ProbPrices));
1230baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    }
1231baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
1232baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    nextOpt = &p->opt[cur + 1];
1233baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
1234baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    if (curAnd1Price < nextOpt->price)
1235baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    {
1236baa3858d3f5d128a5c8466b700098109edcad5f2repo sync      nextOpt->price = curAnd1Price;
1237baa3858d3f5d128a5c8466b700098109edcad5f2repo sync      nextOpt->posPrev = cur;
1238baa3858d3f5d128a5c8466b700098109edcad5f2repo sync      MakeAsChar(nextOpt);
1239baa3858d3f5d128a5c8466b700098109edcad5f2repo sync      nextIsChar = True;
1240baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    }
1241baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
1242baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    matchPrice = curPrice + GET_PRICE_1(p->isMatch[state][posState]);
1243baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    repMatchPrice = matchPrice + GET_PRICE_1(p->isRep[state]);
1244baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
1245baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    if (matchByte == curByte && !(nextOpt->posPrev < cur && nextOpt->backPrev == 0))
1246baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    {
1247baa3858d3f5d128a5c8466b700098109edcad5f2repo sync      UInt32 shortRepPrice = repMatchPrice + GetRepLen1Price(p, state, posState);
1248baa3858d3f5d128a5c8466b700098109edcad5f2repo sync      if (shortRepPrice <= nextOpt->price)
1249baa3858d3f5d128a5c8466b700098109edcad5f2repo sync      {
1250baa3858d3f5d128a5c8466b700098109edcad5f2repo sync        nextOpt->price = shortRepPrice;
1251baa3858d3f5d128a5c8466b700098109edcad5f2repo sync        nextOpt->posPrev = cur;
1252baa3858d3f5d128a5c8466b700098109edcad5f2repo sync        MakeAsShortRep(nextOpt);
1253baa3858d3f5d128a5c8466b700098109edcad5f2repo sync        nextIsChar = True;
1254baa3858d3f5d128a5c8466b700098109edcad5f2repo sync      }
1255baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    }
1256baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    numAvailFull = p->numAvail;
1257baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    {
1258baa3858d3f5d128a5c8466b700098109edcad5f2repo sync      UInt32 temp = kNumOpts - 1 - cur;
1259baa3858d3f5d128a5c8466b700098109edcad5f2repo sync      if (temp < numAvailFull)
1260baa3858d3f5d128a5c8466b700098109edcad5f2repo sync        numAvailFull = temp;
1261baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    }
1262baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
1263baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    if (numAvailFull < 2)
1264baa3858d3f5d128a5c8466b700098109edcad5f2repo sync      continue;
1265baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    numAvail = (numAvailFull <= p->numFastBytes ? numAvailFull : p->numFastBytes);
1266baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
1267baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    if (!nextIsChar && matchByte != curByte) /* speed optimization */
1268baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    {
1269baa3858d3f5d128a5c8466b700098109edcad5f2repo sync      /* try Literal + rep0 */
1270baa3858d3f5d128a5c8466b700098109edcad5f2repo sync      UInt32 temp;
1271baa3858d3f5d128a5c8466b700098109edcad5f2repo sync      UInt32 lenTest2;
1272baa3858d3f5d128a5c8466b700098109edcad5f2repo sync      const Byte *data2 = data - (reps[0] + 1);
1273baa3858d3f5d128a5c8466b700098109edcad5f2repo sync      UInt32 limit = p->numFastBytes + 1;
1274baa3858d3f5d128a5c8466b700098109edcad5f2repo sync      if (limit > numAvailFull)
1275baa3858d3f5d128a5c8466b700098109edcad5f2repo sync        limit = numAvailFull;
1276baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
1277baa3858d3f5d128a5c8466b700098109edcad5f2repo sync      for (temp = 1; temp < limit && data[temp] == data2[temp]; temp++);
1278baa3858d3f5d128a5c8466b700098109edcad5f2repo sync      lenTest2 = temp - 1;
1279baa3858d3f5d128a5c8466b700098109edcad5f2repo sync      if (lenTest2 >= 2)
1280baa3858d3f5d128a5c8466b700098109edcad5f2repo sync      {
1281baa3858d3f5d128a5c8466b700098109edcad5f2repo sync        UInt32 state2 = kLiteralNextStates[state];
1282baa3858d3f5d128a5c8466b700098109edcad5f2repo sync        UInt32 posStateNext = (position + 1) & p->pbMask;
1283baa3858d3f5d128a5c8466b700098109edcad5f2repo sync        UInt32 nextRepMatchPrice = curAnd1Price +
1284baa3858d3f5d128a5c8466b700098109edcad5f2repo sync            GET_PRICE_1(p->isMatch[state2][posStateNext]) +
1285baa3858d3f5d128a5c8466b700098109edcad5f2repo sync            GET_PRICE_1(p->isRep[state2]);
1286baa3858d3f5d128a5c8466b700098109edcad5f2repo sync        /* for (; lenTest2 >= 2; lenTest2--) */
1287baa3858d3f5d128a5c8466b700098109edcad5f2repo sync        {
1288baa3858d3f5d128a5c8466b700098109edcad5f2repo sync          UInt32 curAndLenPrice;
1289baa3858d3f5d128a5c8466b700098109edcad5f2repo sync          COptimal *opt;
1290baa3858d3f5d128a5c8466b700098109edcad5f2repo sync          UInt32 offset = cur + 1 + lenTest2;
1291baa3858d3f5d128a5c8466b700098109edcad5f2repo sync          while (lenEnd < offset)
1292baa3858d3f5d128a5c8466b700098109edcad5f2repo sync            p->opt[++lenEnd].price = kInfinityPrice;
1293baa3858d3f5d128a5c8466b700098109edcad5f2repo sync          curAndLenPrice = nextRepMatchPrice + GetRepPrice(p, 0, lenTest2, state2, posStateNext);
1294baa3858d3f5d128a5c8466b700098109edcad5f2repo sync          opt = &p->opt[offset];
1295baa3858d3f5d128a5c8466b700098109edcad5f2repo sync          if (curAndLenPrice < opt->price)
1296baa3858d3f5d128a5c8466b700098109edcad5f2repo sync          {
1297baa3858d3f5d128a5c8466b700098109edcad5f2repo sync            opt->price = curAndLenPrice;
1298baa3858d3f5d128a5c8466b700098109edcad5f2repo sync            opt->posPrev = cur + 1;
1299baa3858d3f5d128a5c8466b700098109edcad5f2repo sync            opt->backPrev = 0;
1300baa3858d3f5d128a5c8466b700098109edcad5f2repo sync            opt->prev1IsChar = True;
1301baa3858d3f5d128a5c8466b700098109edcad5f2repo sync            opt->prev2 = False;
1302baa3858d3f5d128a5c8466b700098109edcad5f2repo sync          }
1303baa3858d3f5d128a5c8466b700098109edcad5f2repo sync        }
1304baa3858d3f5d128a5c8466b700098109edcad5f2repo sync      }
1305baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    }
1306baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
1307baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    startLen = 2; /* speed optimization */
1308baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    {
1309baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    UInt32 repIndex;
1310baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    for (repIndex = 0; repIndex < LZMA_NUM_REPS; repIndex++)
1311baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    {
1312baa3858d3f5d128a5c8466b700098109edcad5f2repo sync      UInt32 lenTest;
1313baa3858d3f5d128a5c8466b700098109edcad5f2repo sync      UInt32 lenTestTemp;
1314baa3858d3f5d128a5c8466b700098109edcad5f2repo sync      UInt32 price;
1315baa3858d3f5d128a5c8466b700098109edcad5f2repo sync      const Byte *data2 = data - (reps[repIndex] + 1);
1316baa3858d3f5d128a5c8466b700098109edcad5f2repo sync      if (data[0] != data2[0] || data[1] != data2[1])
1317baa3858d3f5d128a5c8466b700098109edcad5f2repo sync        continue;
1318baa3858d3f5d128a5c8466b700098109edcad5f2repo sync      for (lenTest = 2; lenTest < numAvail && data[lenTest] == data2[lenTest]; lenTest++);
1319baa3858d3f5d128a5c8466b700098109edcad5f2repo sync      while (lenEnd < cur + lenTest)
1320baa3858d3f5d128a5c8466b700098109edcad5f2repo sync        p->opt[++lenEnd].price = kInfinityPrice;
1321baa3858d3f5d128a5c8466b700098109edcad5f2repo sync      lenTestTemp = lenTest;
1322baa3858d3f5d128a5c8466b700098109edcad5f2repo sync      price = repMatchPrice + GetPureRepPrice(p, repIndex, state, posState);
1323baa3858d3f5d128a5c8466b700098109edcad5f2repo sync      do
1324baa3858d3f5d128a5c8466b700098109edcad5f2repo sync      {
1325baa3858d3f5d128a5c8466b700098109edcad5f2repo sync        UInt32 curAndLenPrice = price + p->repLenEnc.prices[posState][lenTest - 2];
1326baa3858d3f5d128a5c8466b700098109edcad5f2repo sync        COptimal *opt = &p->opt[cur + lenTest];
1327baa3858d3f5d128a5c8466b700098109edcad5f2repo sync        if (curAndLenPrice < opt->price)
1328baa3858d3f5d128a5c8466b700098109edcad5f2repo sync        {
1329baa3858d3f5d128a5c8466b700098109edcad5f2repo sync          opt->price = curAndLenPrice;
1330baa3858d3f5d128a5c8466b700098109edcad5f2repo sync          opt->posPrev = cur;
1331baa3858d3f5d128a5c8466b700098109edcad5f2repo sync          opt->backPrev = repIndex;
1332baa3858d3f5d128a5c8466b700098109edcad5f2repo sync          opt->prev1IsChar = False;
1333baa3858d3f5d128a5c8466b700098109edcad5f2repo sync        }
1334baa3858d3f5d128a5c8466b700098109edcad5f2repo sync      }
1335baa3858d3f5d128a5c8466b700098109edcad5f2repo sync      while (--lenTest >= 2);
1336baa3858d3f5d128a5c8466b700098109edcad5f2repo sync      lenTest = lenTestTemp;
1337baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
1338baa3858d3f5d128a5c8466b700098109edcad5f2repo sync      if (repIndex == 0)
1339baa3858d3f5d128a5c8466b700098109edcad5f2repo sync        startLen = lenTest + 1;
1340baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
1341baa3858d3f5d128a5c8466b700098109edcad5f2repo sync      /* if (_maxMode) */
1342baa3858d3f5d128a5c8466b700098109edcad5f2repo sync        {
1343baa3858d3f5d128a5c8466b700098109edcad5f2repo sync          UInt32 lenTest2 = lenTest + 1;
1344baa3858d3f5d128a5c8466b700098109edcad5f2repo sync          UInt32 limit = lenTest2 + p->numFastBytes;
1345baa3858d3f5d128a5c8466b700098109edcad5f2repo sync          UInt32 nextRepMatchPrice;
1346baa3858d3f5d128a5c8466b700098109edcad5f2repo sync          if (limit > numAvailFull)
1347baa3858d3f5d128a5c8466b700098109edcad5f2repo sync            limit = numAvailFull;
1348baa3858d3f5d128a5c8466b700098109edcad5f2repo sync          for (; lenTest2 < limit && data[lenTest2] == data2[lenTest2]; lenTest2++);
1349baa3858d3f5d128a5c8466b700098109edcad5f2repo sync          lenTest2 -= lenTest + 1;
1350baa3858d3f5d128a5c8466b700098109edcad5f2repo sync          if (lenTest2 >= 2)
1351baa3858d3f5d128a5c8466b700098109edcad5f2repo sync          {
1352baa3858d3f5d128a5c8466b700098109edcad5f2repo sync            UInt32 state2 = kRepNextStates[state];
1353baa3858d3f5d128a5c8466b700098109edcad5f2repo sync            UInt32 posStateNext = (position + lenTest) & p->pbMask;
1354baa3858d3f5d128a5c8466b700098109edcad5f2repo sync            UInt32 curAndLenCharPrice =
1355baa3858d3f5d128a5c8466b700098109edcad5f2repo sync                price + p->repLenEnc.prices[posState][lenTest - 2] +
1356baa3858d3f5d128a5c8466b700098109edcad5f2repo sync                GET_PRICE_0(p->isMatch[state2][posStateNext]) +
1357baa3858d3f5d128a5c8466b700098109edcad5f2repo sync                LitEnc_GetPriceMatched(LIT_PROBS(position + lenTest, data[lenTest - 1]),
1358baa3858d3f5d128a5c8466b700098109edcad5f2repo sync                    data[lenTest], data2[lenTest], p->ProbPrices);
1359baa3858d3f5d128a5c8466b700098109edcad5f2repo sync            state2 = kLiteralNextStates[state2];
1360baa3858d3f5d128a5c8466b700098109edcad5f2repo sync            posStateNext = (position + lenTest + 1) & p->pbMask;
1361baa3858d3f5d128a5c8466b700098109edcad5f2repo sync            nextRepMatchPrice = curAndLenCharPrice +
1362baa3858d3f5d128a5c8466b700098109edcad5f2repo sync                GET_PRICE_1(p->isMatch[state2][posStateNext]) +
1363baa3858d3f5d128a5c8466b700098109edcad5f2repo sync                GET_PRICE_1(p->isRep[state2]);
1364baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
1365baa3858d3f5d128a5c8466b700098109edcad5f2repo sync            /* for (; lenTest2 >= 2; lenTest2--) */
1366baa3858d3f5d128a5c8466b700098109edcad5f2repo sync            {
1367baa3858d3f5d128a5c8466b700098109edcad5f2repo sync              UInt32 curAndLenPrice;
1368baa3858d3f5d128a5c8466b700098109edcad5f2repo sync              COptimal *opt;
1369baa3858d3f5d128a5c8466b700098109edcad5f2repo sync              UInt32 offset = cur + lenTest + 1 + lenTest2;
1370baa3858d3f5d128a5c8466b700098109edcad5f2repo sync              while (lenEnd < offset)
1371baa3858d3f5d128a5c8466b700098109edcad5f2repo sync                p->opt[++lenEnd].price = kInfinityPrice;
1372baa3858d3f5d128a5c8466b700098109edcad5f2repo sync              curAndLenPrice = nextRepMatchPrice + GetRepPrice(p, 0, lenTest2, state2, posStateNext);
1373baa3858d3f5d128a5c8466b700098109edcad5f2repo sync              opt = &p->opt[offset];
1374baa3858d3f5d128a5c8466b700098109edcad5f2repo sync              if (curAndLenPrice < opt->price)
1375baa3858d3f5d128a5c8466b700098109edcad5f2repo sync              {
1376baa3858d3f5d128a5c8466b700098109edcad5f2repo sync                opt->price = curAndLenPrice;
1377baa3858d3f5d128a5c8466b700098109edcad5f2repo sync                opt->posPrev = cur + lenTest + 1;
1378baa3858d3f5d128a5c8466b700098109edcad5f2repo sync                opt->backPrev = 0;
1379baa3858d3f5d128a5c8466b700098109edcad5f2repo sync                opt->prev1IsChar = True;
1380baa3858d3f5d128a5c8466b700098109edcad5f2repo sync                opt->prev2 = True;
1381baa3858d3f5d128a5c8466b700098109edcad5f2repo sync                opt->posPrev2 = cur;
1382baa3858d3f5d128a5c8466b700098109edcad5f2repo sync                opt->backPrev2 = repIndex;
1383baa3858d3f5d128a5c8466b700098109edcad5f2repo sync              }
1384baa3858d3f5d128a5c8466b700098109edcad5f2repo sync            }
1385baa3858d3f5d128a5c8466b700098109edcad5f2repo sync          }
1386baa3858d3f5d128a5c8466b700098109edcad5f2repo sync        }
1387baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    }
1388baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    }
1389baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    /* for (UInt32 lenTest = 2; lenTest <= newLen; lenTest++) */
1390baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    if (newLen > numAvail)
1391baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    {
1392baa3858d3f5d128a5c8466b700098109edcad5f2repo sync      newLen = numAvail;
1393baa3858d3f5d128a5c8466b700098109edcad5f2repo sync      for (numPairs = 0; newLen > matches[numPairs]; numPairs += 2);
1394baa3858d3f5d128a5c8466b700098109edcad5f2repo sync      matches[numPairs] = newLen;
1395baa3858d3f5d128a5c8466b700098109edcad5f2repo sync      numPairs += 2;
1396baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    }
1397baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    if (newLen >= startLen)
1398baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    {
1399baa3858d3f5d128a5c8466b700098109edcad5f2repo sync      UInt32 normalMatchPrice = matchPrice + GET_PRICE_0(p->isRep[state]);
1400baa3858d3f5d128a5c8466b700098109edcad5f2repo sync      UInt32 offs, curBack, posSlot;
1401baa3858d3f5d128a5c8466b700098109edcad5f2repo sync      UInt32 lenTest;
1402baa3858d3f5d128a5c8466b700098109edcad5f2repo sync      while (lenEnd < cur + newLen)
1403baa3858d3f5d128a5c8466b700098109edcad5f2repo sync        p->opt[++lenEnd].price = kInfinityPrice;
1404baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
1405baa3858d3f5d128a5c8466b700098109edcad5f2repo sync      offs = 0;
1406baa3858d3f5d128a5c8466b700098109edcad5f2repo sync      while (startLen > matches[offs])
1407baa3858d3f5d128a5c8466b700098109edcad5f2repo sync        offs += 2;
1408baa3858d3f5d128a5c8466b700098109edcad5f2repo sync      curBack = matches[offs + 1];
1409baa3858d3f5d128a5c8466b700098109edcad5f2repo sync      GetPosSlot2(curBack, posSlot);
1410baa3858d3f5d128a5c8466b700098109edcad5f2repo sync      for (lenTest = /*2*/ startLen; ; lenTest++)
1411baa3858d3f5d128a5c8466b700098109edcad5f2repo sync      {
1412baa3858d3f5d128a5c8466b700098109edcad5f2repo sync        UInt32 curAndLenPrice = normalMatchPrice + p->lenEnc.prices[posState][lenTest - LZMA_MATCH_LEN_MIN];
1413baa3858d3f5d128a5c8466b700098109edcad5f2repo sync        UInt32 lenToPosState = GetLenToPosState(lenTest);
1414baa3858d3f5d128a5c8466b700098109edcad5f2repo sync        COptimal *opt;
1415baa3858d3f5d128a5c8466b700098109edcad5f2repo sync        if (curBack < kNumFullDistances)
1416baa3858d3f5d128a5c8466b700098109edcad5f2repo sync          curAndLenPrice += p->distancesPrices[lenToPosState][curBack];
1417baa3858d3f5d128a5c8466b700098109edcad5f2repo sync        else
1418baa3858d3f5d128a5c8466b700098109edcad5f2repo sync          curAndLenPrice += p->posSlotPrices[lenToPosState][posSlot] + p->alignPrices[curBack & kAlignMask];
1419baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
1420baa3858d3f5d128a5c8466b700098109edcad5f2repo sync        opt = &p->opt[cur + lenTest];
1421baa3858d3f5d128a5c8466b700098109edcad5f2repo sync        if (curAndLenPrice < opt->price)
1422baa3858d3f5d128a5c8466b700098109edcad5f2repo sync        {
1423baa3858d3f5d128a5c8466b700098109edcad5f2repo sync          opt->price = curAndLenPrice;
1424baa3858d3f5d128a5c8466b700098109edcad5f2repo sync          opt->posPrev = cur;
1425baa3858d3f5d128a5c8466b700098109edcad5f2repo sync          opt->backPrev = curBack + LZMA_NUM_REPS;
1426baa3858d3f5d128a5c8466b700098109edcad5f2repo sync          opt->prev1IsChar = False;
1427baa3858d3f5d128a5c8466b700098109edcad5f2repo sync        }
1428baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
1429baa3858d3f5d128a5c8466b700098109edcad5f2repo sync        if (/*_maxMode && */lenTest == matches[offs])
1430baa3858d3f5d128a5c8466b700098109edcad5f2repo sync        {
1431baa3858d3f5d128a5c8466b700098109edcad5f2repo sync          /* Try Match + Literal + Rep0 */
1432baa3858d3f5d128a5c8466b700098109edcad5f2repo sync          const Byte *data2 = data - (curBack + 1);
1433baa3858d3f5d128a5c8466b700098109edcad5f2repo sync          UInt32 lenTest2 = lenTest + 1;
1434baa3858d3f5d128a5c8466b700098109edcad5f2repo sync          UInt32 limit = lenTest2 + p->numFastBytes;
1435baa3858d3f5d128a5c8466b700098109edcad5f2repo sync          UInt32 nextRepMatchPrice;
1436baa3858d3f5d128a5c8466b700098109edcad5f2repo sync          if (limit > numAvailFull)
1437baa3858d3f5d128a5c8466b700098109edcad5f2repo sync            limit = numAvailFull;
1438baa3858d3f5d128a5c8466b700098109edcad5f2repo sync          for (; lenTest2 < limit && data[lenTest2] == data2[lenTest2]; lenTest2++);
1439baa3858d3f5d128a5c8466b700098109edcad5f2repo sync          lenTest2 -= lenTest + 1;
1440baa3858d3f5d128a5c8466b700098109edcad5f2repo sync          if (lenTest2 >= 2)
1441baa3858d3f5d128a5c8466b700098109edcad5f2repo sync          {
1442baa3858d3f5d128a5c8466b700098109edcad5f2repo sync            UInt32 state2 = kMatchNextStates[state];
1443baa3858d3f5d128a5c8466b700098109edcad5f2repo sync            UInt32 posStateNext = (position + lenTest) & p->pbMask;
1444baa3858d3f5d128a5c8466b700098109edcad5f2repo sync            UInt32 curAndLenCharPrice = curAndLenPrice +
1445baa3858d3f5d128a5c8466b700098109edcad5f2repo sync                GET_PRICE_0(p->isMatch[state2][posStateNext]) +
1446baa3858d3f5d128a5c8466b700098109edcad5f2repo sync                LitEnc_GetPriceMatched(LIT_PROBS(position + lenTest, data[lenTest - 1]),
1447baa3858d3f5d128a5c8466b700098109edcad5f2repo sync                    data[lenTest], data2[lenTest], p->ProbPrices);
1448baa3858d3f5d128a5c8466b700098109edcad5f2repo sync            state2 = kLiteralNextStates[state2];
1449baa3858d3f5d128a5c8466b700098109edcad5f2repo sync            posStateNext = (posStateNext + 1) & p->pbMask;
1450baa3858d3f5d128a5c8466b700098109edcad5f2repo sync            nextRepMatchPrice = curAndLenCharPrice +
1451baa3858d3f5d128a5c8466b700098109edcad5f2repo sync                GET_PRICE_1(p->isMatch[state2][posStateNext]) +
1452baa3858d3f5d128a5c8466b700098109edcad5f2repo sync                GET_PRICE_1(p->isRep[state2]);
1453baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
1454baa3858d3f5d128a5c8466b700098109edcad5f2repo sync            /* for (; lenTest2 >= 2; lenTest2--) */
1455baa3858d3f5d128a5c8466b700098109edcad5f2repo sync            {
1456baa3858d3f5d128a5c8466b700098109edcad5f2repo sync              UInt32 offset = cur + lenTest + 1 + lenTest2;
1457baa3858d3f5d128a5c8466b700098109edcad5f2repo sync              UInt32 curAndLenPrice;
1458baa3858d3f5d128a5c8466b700098109edcad5f2repo sync              COptimal *opt;
1459baa3858d3f5d128a5c8466b700098109edcad5f2repo sync              while (lenEnd < offset)
1460baa3858d3f5d128a5c8466b700098109edcad5f2repo sync                p->opt[++lenEnd].price = kInfinityPrice;
1461baa3858d3f5d128a5c8466b700098109edcad5f2repo sync              curAndLenPrice = nextRepMatchPrice + GetRepPrice(p, 0, lenTest2, state2, posStateNext);
1462baa3858d3f5d128a5c8466b700098109edcad5f2repo sync              opt = &p->opt[offset];
1463baa3858d3f5d128a5c8466b700098109edcad5f2repo sync              if (curAndLenPrice < opt->price)
1464baa3858d3f5d128a5c8466b700098109edcad5f2repo sync              {
1465baa3858d3f5d128a5c8466b700098109edcad5f2repo sync                opt->price = curAndLenPrice;
1466baa3858d3f5d128a5c8466b700098109edcad5f2repo sync                opt->posPrev = cur + lenTest + 1;
1467baa3858d3f5d128a5c8466b700098109edcad5f2repo sync                opt->backPrev = 0;
1468baa3858d3f5d128a5c8466b700098109edcad5f2repo sync                opt->prev1IsChar = True;
1469baa3858d3f5d128a5c8466b700098109edcad5f2repo sync                opt->prev2 = True;
1470baa3858d3f5d128a5c8466b700098109edcad5f2repo sync                opt->posPrev2 = cur;
1471baa3858d3f5d128a5c8466b700098109edcad5f2repo sync                opt->backPrev2 = curBack + LZMA_NUM_REPS;
1472baa3858d3f5d128a5c8466b700098109edcad5f2repo sync              }
1473baa3858d3f5d128a5c8466b700098109edcad5f2repo sync            }
1474baa3858d3f5d128a5c8466b700098109edcad5f2repo sync          }
1475baa3858d3f5d128a5c8466b700098109edcad5f2repo sync          offs += 2;
1476baa3858d3f5d128a5c8466b700098109edcad5f2repo sync          if (offs == numPairs)
1477baa3858d3f5d128a5c8466b700098109edcad5f2repo sync            break;
1478baa3858d3f5d128a5c8466b700098109edcad5f2repo sync          curBack = matches[offs + 1];
1479baa3858d3f5d128a5c8466b700098109edcad5f2repo sync          if (curBack >= kNumFullDistances)
1480baa3858d3f5d128a5c8466b700098109edcad5f2repo sync            GetPosSlot2(curBack, posSlot);
1481baa3858d3f5d128a5c8466b700098109edcad5f2repo sync        }
1482baa3858d3f5d128a5c8466b700098109edcad5f2repo sync      }
1483baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    }
1484baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  }
1485baa3858d3f5d128a5c8466b700098109edcad5f2repo sync}
1486baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
1487baa3858d3f5d128a5c8466b700098109edcad5f2repo sync#define ChangePair(smallDist, bigDist) (((bigDist) >> 7) > (smallDist))
1488baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
1489baa3858d3f5d128a5c8466b700098109edcad5f2repo syncstatic UInt32 GetOptimumFast(CLzmaEnc *p, UInt32 *backRes)
1490baa3858d3f5d128a5c8466b700098109edcad5f2repo sync{
1491baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  UInt32 numAvail, mainLen, mainDist, numPairs, repIndex, repLen, i;
1492baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  const Byte *data;
1493baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  const UInt32 *matches;
1494baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
1495baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  if (p->additionalOffset == 0)
1496baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    mainLen = ReadMatchDistances(p, &numPairs);
1497baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  else
1498baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  {
1499baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    mainLen = p->longestMatchLength;
1500baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    numPairs = p->numPairs;
1501baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  }
1502baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
1503baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  numAvail = p->numAvail;
1504baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  *backRes = (UInt32)-1;
1505baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  if (numAvail < 2)
1506baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    return 1;
1507baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  if (numAvail > LZMA_MATCH_LEN_MAX)
1508baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    numAvail = LZMA_MATCH_LEN_MAX;
1509baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  data = p->matchFinder.GetPointerToCurrentPos(p->matchFinderObj) - 1;
1510baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
1511baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  repLen = repIndex = 0;
1512baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  for (i = 0; i < LZMA_NUM_REPS; i++)
1513baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  {
1514baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    UInt32 len;
1515baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    const Byte *data2 = data - (p->reps[i] + 1);
1516baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    if (data[0] != data2[0] || data[1] != data2[1])
1517baa3858d3f5d128a5c8466b700098109edcad5f2repo sync      continue;
1518baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    for (len = 2; len < numAvail && data[len] == data2[len]; len++);
1519baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    if (len >= p->numFastBytes)
1520baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    {
1521baa3858d3f5d128a5c8466b700098109edcad5f2repo sync      *backRes = i;
1522baa3858d3f5d128a5c8466b700098109edcad5f2repo sync      MovePos(p, len - 1);
1523baa3858d3f5d128a5c8466b700098109edcad5f2repo sync      return len;
1524baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    }
1525baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    if (len > repLen)
1526baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    {
1527baa3858d3f5d128a5c8466b700098109edcad5f2repo sync      repIndex = i;
1528baa3858d3f5d128a5c8466b700098109edcad5f2repo sync      repLen = len;
1529baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    }
1530baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  }
1531baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
1532baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  matches = p->matches;
1533baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  if (mainLen >= p->numFastBytes)
1534baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  {
1535baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    *backRes = matches[numPairs - 1] + LZMA_NUM_REPS;
1536baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    MovePos(p, mainLen - 1);
1537baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    return mainLen;
1538baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  }
1539baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
1540baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  mainDist = 0; /* for GCC */
1541baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  if (mainLen >= 2)
1542baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  {
1543baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    mainDist = matches[numPairs - 1];
1544baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    while (numPairs > 2 && mainLen == matches[numPairs - 4] + 1)
1545baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    {
1546baa3858d3f5d128a5c8466b700098109edcad5f2repo sync      if (!ChangePair(matches[numPairs - 3], mainDist))
1547baa3858d3f5d128a5c8466b700098109edcad5f2repo sync        break;
1548baa3858d3f5d128a5c8466b700098109edcad5f2repo sync      numPairs -= 2;
1549baa3858d3f5d128a5c8466b700098109edcad5f2repo sync      mainLen = matches[numPairs - 2];
1550baa3858d3f5d128a5c8466b700098109edcad5f2repo sync      mainDist = matches[numPairs - 1];
1551baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    }
1552baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    if (mainLen == 2 && mainDist >= 0x80)
1553baa3858d3f5d128a5c8466b700098109edcad5f2repo sync      mainLen = 1;
1554baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  }
1555baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
1556baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  if (repLen >= 2 && (
1557baa3858d3f5d128a5c8466b700098109edcad5f2repo sync        (repLen + 1 >= mainLen) ||
1558baa3858d3f5d128a5c8466b700098109edcad5f2repo sync        (repLen + 2 >= mainLen && mainDist >= (1 << 9)) ||
1559baa3858d3f5d128a5c8466b700098109edcad5f2repo sync        (repLen + 3 >= mainLen && mainDist >= (1 << 15))))
1560baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  {
1561baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    *backRes = repIndex;
1562baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    MovePos(p, repLen - 1);
1563baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    return repLen;
1564baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  }
1565baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
1566baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  if (mainLen < 2 || numAvail <= 2)
1567baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    return 1;
1568baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
1569baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  p->longestMatchLength = ReadMatchDistances(p, &p->numPairs);
1570baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  if (p->longestMatchLength >= 2)
1571baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  {
1572baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    UInt32 newDistance = matches[p->numPairs - 1];
1573baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    if ((p->longestMatchLength >= mainLen && newDistance < mainDist) ||
1574baa3858d3f5d128a5c8466b700098109edcad5f2repo sync        (p->longestMatchLength == mainLen + 1 && !ChangePair(mainDist, newDistance)) ||
1575baa3858d3f5d128a5c8466b700098109edcad5f2repo sync        (p->longestMatchLength > mainLen + 1) ||
1576baa3858d3f5d128a5c8466b700098109edcad5f2repo sync        (p->longestMatchLength + 1 >= mainLen && mainLen >= 3 && ChangePair(newDistance, mainDist)))
1577baa3858d3f5d128a5c8466b700098109edcad5f2repo sync      return 1;
1578baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  }
1579baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
1580baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  data = p->matchFinder.GetPointerToCurrentPos(p->matchFinderObj) - 1;
1581baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  for (i = 0; i < LZMA_NUM_REPS; i++)
1582baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  {
1583baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    UInt32 len, limit;
1584baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    const Byte *data2 = data - (p->reps[i] + 1);
1585baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    if (data[0] != data2[0] || data[1] != data2[1])
1586baa3858d3f5d128a5c8466b700098109edcad5f2repo sync      continue;
1587baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    limit = mainLen - 1;
1588baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    for (len = 2; len < limit && data[len] == data2[len]; len++);
1589baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    if (len >= limit)
1590baa3858d3f5d128a5c8466b700098109edcad5f2repo sync      return 1;
1591baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  }
1592baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  *backRes = mainDist + LZMA_NUM_REPS;
1593baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  MovePos(p, mainLen - 2);
1594baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  return mainLen;
1595baa3858d3f5d128a5c8466b700098109edcad5f2repo sync}
1596baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
1597baa3858d3f5d128a5c8466b700098109edcad5f2repo syncstatic void WriteEndMarker(CLzmaEnc *p, UInt32 posState)
1598baa3858d3f5d128a5c8466b700098109edcad5f2repo sync{
1599baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  UInt32 len;
1600baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  RangeEnc_EncodeBit(&p->rc, &p->isMatch[p->state][posState], 1);
1601baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  RangeEnc_EncodeBit(&p->rc, &p->isRep[p->state], 0);
1602baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  p->state = kMatchNextStates[p->state];
1603baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  len = LZMA_MATCH_LEN_MIN;
1604baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  LenEnc_Encode2(&p->lenEnc, &p->rc, len - LZMA_MATCH_LEN_MIN, posState, !p->fastMode, p->ProbPrices);
1605baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  RcTree_Encode(&p->rc, p->posSlotEncoder[GetLenToPosState(len)], kNumPosSlotBits, (1 << kNumPosSlotBits) - 1);
1606baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  RangeEnc_EncodeDirectBits(&p->rc, (((UInt32)1 << 30) - 1) >> kNumAlignBits, 30 - kNumAlignBits);
1607baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  RcTree_ReverseEncode(&p->rc, p->posAlignEncoder, kNumAlignBits, kAlignMask);
1608baa3858d3f5d128a5c8466b700098109edcad5f2repo sync}
1609baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
1610baa3858d3f5d128a5c8466b700098109edcad5f2repo syncstatic SRes CheckErrors(CLzmaEnc *p)
1611baa3858d3f5d128a5c8466b700098109edcad5f2repo sync{
1612baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  if (p->result != SZ_OK)
1613baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    return p->result;
1614baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  if (p->rc.res != SZ_OK)
1615baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    p->result = SZ_ERROR_WRITE;
1616baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  if (p->matchFinderBase.result != SZ_OK)
1617baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    p->result = SZ_ERROR_READ;
1618baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  if (p->result != SZ_OK)
1619baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    p->finished = True;
1620baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  return p->result;
1621baa3858d3f5d128a5c8466b700098109edcad5f2repo sync}
1622baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
1623baa3858d3f5d128a5c8466b700098109edcad5f2repo syncstatic SRes Flush(CLzmaEnc *p, UInt32 nowPos)
1624baa3858d3f5d128a5c8466b700098109edcad5f2repo sync{
1625baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  /* ReleaseMFStream(); */
1626baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  p->finished = True;
1627baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  if (p->writeEndMark)
1628baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    WriteEndMarker(p, nowPos & p->pbMask);
1629baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  RangeEnc_FlushData(&p->rc);
1630baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  RangeEnc_FlushStream(&p->rc);
1631baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  return CheckErrors(p);
1632baa3858d3f5d128a5c8466b700098109edcad5f2repo sync}
1633baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
1634baa3858d3f5d128a5c8466b700098109edcad5f2repo syncstatic void FillAlignPrices(CLzmaEnc *p)
1635baa3858d3f5d128a5c8466b700098109edcad5f2repo sync{
1636baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  UInt32 i;
1637baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  for (i = 0; i < kAlignTableSize; i++)
1638baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    p->alignPrices[i] = RcTree_ReverseGetPrice(p->posAlignEncoder, kNumAlignBits, i, p->ProbPrices);
1639baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  p->alignPriceCount = 0;
1640baa3858d3f5d128a5c8466b700098109edcad5f2repo sync}
1641baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
1642baa3858d3f5d128a5c8466b700098109edcad5f2repo syncstatic void FillDistancesPrices(CLzmaEnc *p)
1643baa3858d3f5d128a5c8466b700098109edcad5f2repo sync{
1644baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  UInt32 tempPrices[kNumFullDistances];
1645baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  UInt32 i, lenToPosState;
1646baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  for (i = kStartPosModelIndex; i < kNumFullDistances; i++)
1647baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  {
1648baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    UInt32 posSlot = GetPosSlot1(i);
1649baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    UInt32 footerBits = ((posSlot >> 1) - 1);
1650baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    UInt32 base = ((2 | (posSlot & 1)) << footerBits);
1651baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    tempPrices[i] = RcTree_ReverseGetPrice(p->posEncoders + base - posSlot - 1, footerBits, i - base, p->ProbPrices);
1652baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  }
1653baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
1654baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  for (lenToPosState = 0; lenToPosState < kNumLenToPosStates; lenToPosState++)
1655baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  {
1656baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    UInt32 posSlot;
1657baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    const CLzmaProb *encoder = p->posSlotEncoder[lenToPosState];
1658baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    UInt32 *posSlotPrices = p->posSlotPrices[lenToPosState];
1659baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    for (posSlot = 0; posSlot < p->distTableSize; posSlot++)
1660baa3858d3f5d128a5c8466b700098109edcad5f2repo sync      posSlotPrices[posSlot] = RcTree_GetPrice(encoder, kNumPosSlotBits, posSlot, p->ProbPrices);
1661baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    for (posSlot = kEndPosModelIndex; posSlot < p->distTableSize; posSlot++)
1662baa3858d3f5d128a5c8466b700098109edcad5f2repo sync      posSlotPrices[posSlot] += ((((posSlot >> 1) - 1) - kNumAlignBits) << kNumBitPriceShiftBits);
1663baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
1664baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    {
1665baa3858d3f5d128a5c8466b700098109edcad5f2repo sync      UInt32 *distancesPrices = p->distancesPrices[lenToPosState];
1666baa3858d3f5d128a5c8466b700098109edcad5f2repo sync      UInt32 i;
1667baa3858d3f5d128a5c8466b700098109edcad5f2repo sync      for (i = 0; i < kStartPosModelIndex; i++)
1668baa3858d3f5d128a5c8466b700098109edcad5f2repo sync        distancesPrices[i] = posSlotPrices[i];
1669baa3858d3f5d128a5c8466b700098109edcad5f2repo sync      for (; i < kNumFullDistances; i++)
1670baa3858d3f5d128a5c8466b700098109edcad5f2repo sync        distancesPrices[i] = posSlotPrices[GetPosSlot1(i)] + tempPrices[i];
1671baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    }
1672baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  }
1673baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  p->matchPriceCount = 0;
1674baa3858d3f5d128a5c8466b700098109edcad5f2repo sync}
1675baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
1676baa3858d3f5d128a5c8466b700098109edcad5f2repo syncvoid LzmaEnc_Construct(CLzmaEnc *p)
1677baa3858d3f5d128a5c8466b700098109edcad5f2repo sync{
1678baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  RangeEnc_Construct(&p->rc);
1679baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  MatchFinder_Construct(&p->matchFinderBase);
1680baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  #ifndef _7ZIP_ST
1681baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  MatchFinderMt_Construct(&p->matchFinderMt);
1682baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  p->matchFinderMt.MatchFinder = &p->matchFinderBase;
1683baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  #endif
1684baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
1685baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  {
1686baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    CLzmaEncProps props;
1687baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    LzmaEncProps_Init(&props);
1688baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    LzmaEnc_SetProps(p, &props);
1689baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  }
1690baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
1691baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  #ifndef LZMA_LOG_BSR
1692baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  LzmaEnc_FastPosInit(p->g_FastPos);
1693baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  #endif
1694baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
1695baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  LzmaEnc_InitPriceTables(p->ProbPrices);
1696baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  p->litProbs = 0;
1697baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  p->saveState.litProbs = 0;
1698baa3858d3f5d128a5c8466b700098109edcad5f2repo sync}
1699baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
1700baa3858d3f5d128a5c8466b700098109edcad5f2repo syncCLzmaEncHandle LzmaEnc_Create(ISzAlloc *alloc)
1701baa3858d3f5d128a5c8466b700098109edcad5f2repo sync{
1702baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  void *p;
1703baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  p = alloc->Alloc(alloc, sizeof(CLzmaEnc));
1704baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  if (p != 0)
1705baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    LzmaEnc_Construct((CLzmaEnc *)p);
1706baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  return p;
1707baa3858d3f5d128a5c8466b700098109edcad5f2repo sync}
1708baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
1709baa3858d3f5d128a5c8466b700098109edcad5f2repo syncvoid LzmaEnc_FreeLits(CLzmaEnc *p, ISzAlloc *alloc)
1710baa3858d3f5d128a5c8466b700098109edcad5f2repo sync{
1711baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  alloc->Free(alloc, p->litProbs);
1712baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  alloc->Free(alloc, p->saveState.litProbs);
1713baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  p->litProbs = 0;
1714baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  p->saveState.litProbs = 0;
1715baa3858d3f5d128a5c8466b700098109edcad5f2repo sync}
1716baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
1717baa3858d3f5d128a5c8466b700098109edcad5f2repo syncvoid LzmaEnc_Destruct(CLzmaEnc *p, ISzAlloc *alloc, ISzAlloc *allocBig)
1718baa3858d3f5d128a5c8466b700098109edcad5f2repo sync{
1719baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  #ifndef _7ZIP_ST
1720baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  MatchFinderMt_Destruct(&p->matchFinderMt, allocBig);
1721baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  #endif
1722baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  MatchFinder_Free(&p->matchFinderBase, allocBig);
1723baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  LzmaEnc_FreeLits(p, alloc);
1724baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  RangeEnc_Free(&p->rc, alloc);
1725baa3858d3f5d128a5c8466b700098109edcad5f2repo sync}
1726baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
1727baa3858d3f5d128a5c8466b700098109edcad5f2repo syncvoid LzmaEnc_Destroy(CLzmaEncHandle p, ISzAlloc *alloc, ISzAlloc *allocBig)
1728baa3858d3f5d128a5c8466b700098109edcad5f2repo sync{
1729baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  LzmaEnc_Destruct((CLzmaEnc *)p, alloc, allocBig);
1730baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  alloc->Free(alloc, p);
1731baa3858d3f5d128a5c8466b700098109edcad5f2repo sync}
1732baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
1733baa3858d3f5d128a5c8466b700098109edcad5f2repo syncstatic SRes LzmaEnc_CodeOneBlock(CLzmaEnc *p, Bool useLimits, UInt32 maxPackSize, UInt32 maxUnpackSize)
1734baa3858d3f5d128a5c8466b700098109edcad5f2repo sync{
1735baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  UInt32 nowPos32, startPos32;
1736baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  if (p->needInit)
1737baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  {
1738baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    p->matchFinder.Init(p->matchFinderObj);
1739baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    p->needInit = 0;
1740baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  }
1741baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
1742baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  if (p->finished)
1743baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    return p->result;
1744baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  RINOK(CheckErrors(p));
1745baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
1746baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  nowPos32 = (UInt32)p->nowPos64;
1747baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  startPos32 = nowPos32;
1748baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
1749baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  if (p->nowPos64 == 0)
1750baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  {
1751baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    UInt32 numPairs;
1752baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    Byte curByte;
1753baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    if (p->matchFinder.GetNumAvailableBytes(p->matchFinderObj) == 0)
1754baa3858d3f5d128a5c8466b700098109edcad5f2repo sync      return Flush(p, nowPos32);
1755baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    ReadMatchDistances(p, &numPairs);
1756baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    RangeEnc_EncodeBit(&p->rc, &p->isMatch[p->state][0], 0);
1757baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    p->state = kLiteralNextStates[p->state];
1758baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    curByte = p->matchFinder.GetIndexByte(p->matchFinderObj, 0 - p->additionalOffset);
1759baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    LitEnc_Encode(&p->rc, p->litProbs, curByte);
1760baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    p->additionalOffset--;
1761baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    nowPos32++;
1762baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  }
1763baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
1764baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  if (p->matchFinder.GetNumAvailableBytes(p->matchFinderObj) != 0)
1765baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  for (;;)
1766baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  {
1767baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    UInt32 pos, len, posState;
1768baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
1769baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    if (p->fastMode)
1770baa3858d3f5d128a5c8466b700098109edcad5f2repo sync      len = GetOptimumFast(p, &pos);
1771baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    else
1772baa3858d3f5d128a5c8466b700098109edcad5f2repo sync      len = GetOptimum(p, nowPos32, &pos);
1773baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
1774baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    #ifdef SHOW_STAT2
1775baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    printf("\n pos = %4X,   len = %d   pos = %d", nowPos32, len, pos);
1776baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    #endif
1777baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
1778baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    posState = nowPos32 & p->pbMask;
1779baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    if (len == 1 && pos == (UInt32)-1)
1780baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    {
1781baa3858d3f5d128a5c8466b700098109edcad5f2repo sync      Byte curByte;
1782baa3858d3f5d128a5c8466b700098109edcad5f2repo sync      CLzmaProb *probs;
1783baa3858d3f5d128a5c8466b700098109edcad5f2repo sync      const Byte *data;
1784baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
1785baa3858d3f5d128a5c8466b700098109edcad5f2repo sync      RangeEnc_EncodeBit(&p->rc, &p->isMatch[p->state][posState], 0);
1786baa3858d3f5d128a5c8466b700098109edcad5f2repo sync      data = p->matchFinder.GetPointerToCurrentPos(p->matchFinderObj) - p->additionalOffset;
1787baa3858d3f5d128a5c8466b700098109edcad5f2repo sync      curByte = *data;
1788baa3858d3f5d128a5c8466b700098109edcad5f2repo sync      probs = LIT_PROBS(nowPos32, *(data - 1));
1789baa3858d3f5d128a5c8466b700098109edcad5f2repo sync      if (IsCharState(p->state))
1790baa3858d3f5d128a5c8466b700098109edcad5f2repo sync        LitEnc_Encode(&p->rc, probs, curByte);
1791baa3858d3f5d128a5c8466b700098109edcad5f2repo sync      else
1792baa3858d3f5d128a5c8466b700098109edcad5f2repo sync        LitEnc_EncodeMatched(&p->rc, probs, curByte, *(data - p->reps[0] - 1));
1793baa3858d3f5d128a5c8466b700098109edcad5f2repo sync      p->state = kLiteralNextStates[p->state];
1794baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    }
1795baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    else
1796baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    {
1797baa3858d3f5d128a5c8466b700098109edcad5f2repo sync      RangeEnc_EncodeBit(&p->rc, &p->isMatch[p->state][posState], 1);
1798baa3858d3f5d128a5c8466b700098109edcad5f2repo sync      if (pos < LZMA_NUM_REPS)
1799baa3858d3f5d128a5c8466b700098109edcad5f2repo sync      {
1800baa3858d3f5d128a5c8466b700098109edcad5f2repo sync        RangeEnc_EncodeBit(&p->rc, &p->isRep[p->state], 1);
1801baa3858d3f5d128a5c8466b700098109edcad5f2repo sync        if (pos == 0)
1802baa3858d3f5d128a5c8466b700098109edcad5f2repo sync        {
1803baa3858d3f5d128a5c8466b700098109edcad5f2repo sync          RangeEnc_EncodeBit(&p->rc, &p->isRepG0[p->state], 0);
1804baa3858d3f5d128a5c8466b700098109edcad5f2repo sync          RangeEnc_EncodeBit(&p->rc, &p->isRep0Long[p->state][posState], ((len == 1) ? 0 : 1));
1805baa3858d3f5d128a5c8466b700098109edcad5f2repo sync        }
1806baa3858d3f5d128a5c8466b700098109edcad5f2repo sync        else
1807baa3858d3f5d128a5c8466b700098109edcad5f2repo sync        {
1808baa3858d3f5d128a5c8466b700098109edcad5f2repo sync          UInt32 distance = p->reps[pos];
1809baa3858d3f5d128a5c8466b700098109edcad5f2repo sync          RangeEnc_EncodeBit(&p->rc, &p->isRepG0[p->state], 1);
1810baa3858d3f5d128a5c8466b700098109edcad5f2repo sync          if (pos == 1)
1811baa3858d3f5d128a5c8466b700098109edcad5f2repo sync            RangeEnc_EncodeBit(&p->rc, &p->isRepG1[p->state], 0);
1812baa3858d3f5d128a5c8466b700098109edcad5f2repo sync          else
1813baa3858d3f5d128a5c8466b700098109edcad5f2repo sync          {
1814baa3858d3f5d128a5c8466b700098109edcad5f2repo sync            RangeEnc_EncodeBit(&p->rc, &p->isRepG1[p->state], 1);
1815baa3858d3f5d128a5c8466b700098109edcad5f2repo sync            RangeEnc_EncodeBit(&p->rc, &p->isRepG2[p->state], pos - 2);
1816baa3858d3f5d128a5c8466b700098109edcad5f2repo sync            if (pos == 3)
1817baa3858d3f5d128a5c8466b700098109edcad5f2repo sync              p->reps[3] = p->reps[2];
1818baa3858d3f5d128a5c8466b700098109edcad5f2repo sync            p->reps[2] = p->reps[1];
1819baa3858d3f5d128a5c8466b700098109edcad5f2repo sync          }
1820baa3858d3f5d128a5c8466b700098109edcad5f2repo sync          p->reps[1] = p->reps[0];
1821baa3858d3f5d128a5c8466b700098109edcad5f2repo sync          p->reps[0] = distance;
1822baa3858d3f5d128a5c8466b700098109edcad5f2repo sync        }
1823baa3858d3f5d128a5c8466b700098109edcad5f2repo sync        if (len == 1)
1824baa3858d3f5d128a5c8466b700098109edcad5f2repo sync          p->state = kShortRepNextStates[p->state];
1825baa3858d3f5d128a5c8466b700098109edcad5f2repo sync        else
1826baa3858d3f5d128a5c8466b700098109edcad5f2repo sync        {
1827baa3858d3f5d128a5c8466b700098109edcad5f2repo sync          LenEnc_Encode2(&p->repLenEnc, &p->rc, len - LZMA_MATCH_LEN_MIN, posState, !p->fastMode, p->ProbPrices);
1828baa3858d3f5d128a5c8466b700098109edcad5f2repo sync          p->state = kRepNextStates[p->state];
1829baa3858d3f5d128a5c8466b700098109edcad5f2repo sync        }
1830baa3858d3f5d128a5c8466b700098109edcad5f2repo sync      }
1831baa3858d3f5d128a5c8466b700098109edcad5f2repo sync      else
1832baa3858d3f5d128a5c8466b700098109edcad5f2repo sync      {
1833baa3858d3f5d128a5c8466b700098109edcad5f2repo sync        UInt32 posSlot;
1834baa3858d3f5d128a5c8466b700098109edcad5f2repo sync        RangeEnc_EncodeBit(&p->rc, &p->isRep[p->state], 0);
1835baa3858d3f5d128a5c8466b700098109edcad5f2repo sync        p->state = kMatchNextStates[p->state];
1836baa3858d3f5d128a5c8466b700098109edcad5f2repo sync        LenEnc_Encode2(&p->lenEnc, &p->rc, len - LZMA_MATCH_LEN_MIN, posState, !p->fastMode, p->ProbPrices);
1837baa3858d3f5d128a5c8466b700098109edcad5f2repo sync        pos -= LZMA_NUM_REPS;
1838baa3858d3f5d128a5c8466b700098109edcad5f2repo sync        GetPosSlot(pos, posSlot);
1839baa3858d3f5d128a5c8466b700098109edcad5f2repo sync        RcTree_Encode(&p->rc, p->posSlotEncoder[GetLenToPosState(len)], kNumPosSlotBits, posSlot);
1840baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
1841baa3858d3f5d128a5c8466b700098109edcad5f2repo sync        if (posSlot >= kStartPosModelIndex)
1842baa3858d3f5d128a5c8466b700098109edcad5f2repo sync        {
1843baa3858d3f5d128a5c8466b700098109edcad5f2repo sync          UInt32 footerBits = ((posSlot >> 1) - 1);
1844baa3858d3f5d128a5c8466b700098109edcad5f2repo sync          UInt32 base = ((2 | (posSlot & 1)) << footerBits);
1845baa3858d3f5d128a5c8466b700098109edcad5f2repo sync          UInt32 posReduced = pos - base;
1846baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
1847baa3858d3f5d128a5c8466b700098109edcad5f2repo sync          if (posSlot < kEndPosModelIndex)
1848baa3858d3f5d128a5c8466b700098109edcad5f2repo sync            RcTree_ReverseEncode(&p->rc, p->posEncoders + base - posSlot - 1, footerBits, posReduced);
1849baa3858d3f5d128a5c8466b700098109edcad5f2repo sync          else
1850baa3858d3f5d128a5c8466b700098109edcad5f2repo sync          {
1851baa3858d3f5d128a5c8466b700098109edcad5f2repo sync            RangeEnc_EncodeDirectBits(&p->rc, posReduced >> kNumAlignBits, footerBits - kNumAlignBits);
1852baa3858d3f5d128a5c8466b700098109edcad5f2repo sync            RcTree_ReverseEncode(&p->rc, p->posAlignEncoder, kNumAlignBits, posReduced & kAlignMask);
1853baa3858d3f5d128a5c8466b700098109edcad5f2repo sync            p->alignPriceCount++;
1854baa3858d3f5d128a5c8466b700098109edcad5f2repo sync          }
1855baa3858d3f5d128a5c8466b700098109edcad5f2repo sync        }
1856baa3858d3f5d128a5c8466b700098109edcad5f2repo sync        p->reps[3] = p->reps[2];
1857baa3858d3f5d128a5c8466b700098109edcad5f2repo sync        p->reps[2] = p->reps[1];
1858baa3858d3f5d128a5c8466b700098109edcad5f2repo sync        p->reps[1] = p->reps[0];
1859baa3858d3f5d128a5c8466b700098109edcad5f2repo sync        p->reps[0] = pos;
1860baa3858d3f5d128a5c8466b700098109edcad5f2repo sync        p->matchPriceCount++;
1861baa3858d3f5d128a5c8466b700098109edcad5f2repo sync      }
1862baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    }
1863baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    p->additionalOffset -= len;
1864baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    nowPos32 += len;
1865baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    if (p->additionalOffset == 0)
1866baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    {
1867baa3858d3f5d128a5c8466b700098109edcad5f2repo sync      UInt32 processed;
1868baa3858d3f5d128a5c8466b700098109edcad5f2repo sync      if (!p->fastMode)
1869baa3858d3f5d128a5c8466b700098109edcad5f2repo sync      {
1870baa3858d3f5d128a5c8466b700098109edcad5f2repo sync        if (p->matchPriceCount >= (1 << 7))
1871baa3858d3f5d128a5c8466b700098109edcad5f2repo sync          FillDistancesPrices(p);
1872baa3858d3f5d128a5c8466b700098109edcad5f2repo sync        if (p->alignPriceCount >= kAlignTableSize)
1873baa3858d3f5d128a5c8466b700098109edcad5f2repo sync          FillAlignPrices(p);
1874baa3858d3f5d128a5c8466b700098109edcad5f2repo sync      }
1875baa3858d3f5d128a5c8466b700098109edcad5f2repo sync      if (p->matchFinder.GetNumAvailableBytes(p->matchFinderObj) == 0)
1876baa3858d3f5d128a5c8466b700098109edcad5f2repo sync        break;
1877baa3858d3f5d128a5c8466b700098109edcad5f2repo sync      processed = nowPos32 - startPos32;
1878baa3858d3f5d128a5c8466b700098109edcad5f2repo sync      if (useLimits)
1879baa3858d3f5d128a5c8466b700098109edcad5f2repo sync      {
1880baa3858d3f5d128a5c8466b700098109edcad5f2repo sync        if (processed + kNumOpts + 300 >= maxUnpackSize ||
1881baa3858d3f5d128a5c8466b700098109edcad5f2repo sync            RangeEnc_GetProcessed(&p->rc) + kNumOpts * 2 >= maxPackSize)
1882baa3858d3f5d128a5c8466b700098109edcad5f2repo sync          break;
1883baa3858d3f5d128a5c8466b700098109edcad5f2repo sync      }
1884baa3858d3f5d128a5c8466b700098109edcad5f2repo sync      else if (processed >= (1 << 15))
1885baa3858d3f5d128a5c8466b700098109edcad5f2repo sync      {
1886baa3858d3f5d128a5c8466b700098109edcad5f2repo sync        p->nowPos64 += nowPos32 - startPos32;
1887baa3858d3f5d128a5c8466b700098109edcad5f2repo sync        return CheckErrors(p);
1888baa3858d3f5d128a5c8466b700098109edcad5f2repo sync      }
1889baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    }
1890baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  }
1891baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  p->nowPos64 += nowPos32 - startPos32;
1892baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  return Flush(p, nowPos32);
1893baa3858d3f5d128a5c8466b700098109edcad5f2repo sync}
1894baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
1895baa3858d3f5d128a5c8466b700098109edcad5f2repo sync#define kBigHashDicLimit ((UInt32)1 << 24)
1896baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
1897baa3858d3f5d128a5c8466b700098109edcad5f2repo syncstatic SRes LzmaEnc_Alloc(CLzmaEnc *p, UInt32 keepWindowSize, ISzAlloc *alloc, ISzAlloc *allocBig)
1898baa3858d3f5d128a5c8466b700098109edcad5f2repo sync{
1899baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  UInt32 beforeSize = kNumOpts;
1900baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  Bool btMode;
1901baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  if (!RangeEnc_Alloc(&p->rc, alloc))
1902baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    return SZ_ERROR_MEM;
1903baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  btMode = (p->matchFinderBase.btMode != 0);
1904baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  #ifndef _7ZIP_ST
1905baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  p->mtMode = (p->multiThread && !p->fastMode && btMode);
1906baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  #endif
1907baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
1908baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  {
1909baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    unsigned lclp = p->lc + p->lp;
1910baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    if (p->litProbs == 0 || p->saveState.litProbs == 0 || p->lclp != lclp)
1911baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    {
1912baa3858d3f5d128a5c8466b700098109edcad5f2repo sync      LzmaEnc_FreeLits(p, alloc);
1913baa3858d3f5d128a5c8466b700098109edcad5f2repo sync      p->litProbs = (CLzmaProb *)alloc->Alloc(alloc, (0x300 << lclp) * sizeof(CLzmaProb));
1914baa3858d3f5d128a5c8466b700098109edcad5f2repo sync      p->saveState.litProbs = (CLzmaProb *)alloc->Alloc(alloc, (0x300 << lclp) * sizeof(CLzmaProb));
1915baa3858d3f5d128a5c8466b700098109edcad5f2repo sync      if (p->litProbs == 0 || p->saveState.litProbs == 0)
1916baa3858d3f5d128a5c8466b700098109edcad5f2repo sync      {
1917baa3858d3f5d128a5c8466b700098109edcad5f2repo sync        LzmaEnc_FreeLits(p, alloc);
1918baa3858d3f5d128a5c8466b700098109edcad5f2repo sync        return SZ_ERROR_MEM;
1919baa3858d3f5d128a5c8466b700098109edcad5f2repo sync      }
1920baa3858d3f5d128a5c8466b700098109edcad5f2repo sync      p->lclp = lclp;
1921baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    }
1922baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  }
1923baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
1924baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  p->matchFinderBase.bigHash = (p->dictSize > kBigHashDicLimit);
1925baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
1926baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  if (beforeSize + p->dictSize < keepWindowSize)
1927baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    beforeSize = keepWindowSize - p->dictSize;
1928baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
1929baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  #ifndef _7ZIP_ST
1930baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  if (p->mtMode)
1931baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  {
1932baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    RINOK(MatchFinderMt_Create(&p->matchFinderMt, p->dictSize, beforeSize, p->numFastBytes, LZMA_MATCH_LEN_MAX, allocBig));
1933baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    p->matchFinderObj = &p->matchFinderMt;
1934baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    MatchFinderMt_CreateVTable(&p->matchFinderMt, &p->matchFinder);
1935baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  }
1936baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  else
1937baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  #endif
1938baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  {
1939baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    if (!MatchFinder_Create(&p->matchFinderBase, p->dictSize, beforeSize, p->numFastBytes, LZMA_MATCH_LEN_MAX, allocBig))
1940baa3858d3f5d128a5c8466b700098109edcad5f2repo sync      return SZ_ERROR_MEM;
1941baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    p->matchFinderObj = &p->matchFinderBase;
1942baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    MatchFinder_CreateVTable(&p->matchFinderBase, &p->matchFinder);
1943baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  }
1944baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  return SZ_OK;
1945baa3858d3f5d128a5c8466b700098109edcad5f2repo sync}
1946baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
1947baa3858d3f5d128a5c8466b700098109edcad5f2repo syncvoid LzmaEnc_Init(CLzmaEnc *p)
1948baa3858d3f5d128a5c8466b700098109edcad5f2repo sync{
1949baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  UInt32 i;
1950baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  p->state = 0;
1951baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  for (i = 0 ; i < LZMA_NUM_REPS; i++)
1952baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    p->reps[i] = 0;
1953baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
1954baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  RangeEnc_Init(&p->rc);
1955baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
1956baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
1957baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  for (i = 0; i < kNumStates; i++)
1958baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  {
1959baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    UInt32 j;
1960baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    for (j = 0; j < LZMA_NUM_PB_STATES_MAX; j++)
1961baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    {
1962baa3858d3f5d128a5c8466b700098109edcad5f2repo sync      p->isMatch[i][j] = kProbInitValue;
1963baa3858d3f5d128a5c8466b700098109edcad5f2repo sync      p->isRep0Long[i][j] = kProbInitValue;
1964baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    }
1965baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    p->isRep[i] = kProbInitValue;
1966baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    p->isRepG0[i] = kProbInitValue;
1967baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    p->isRepG1[i] = kProbInitValue;
1968baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    p->isRepG2[i] = kProbInitValue;
1969baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  }
1970baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
1971baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  {
1972baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    UInt32 num = 0x300 << (p->lp + p->lc);
1973baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    for (i = 0; i < num; i++)
1974baa3858d3f5d128a5c8466b700098109edcad5f2repo sync      p->litProbs[i] = kProbInitValue;
1975baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  }
1976baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
1977baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  {
1978baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    for (i = 0; i < kNumLenToPosStates; i++)
1979baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    {
1980baa3858d3f5d128a5c8466b700098109edcad5f2repo sync      CLzmaProb *probs = p->posSlotEncoder[i];
1981baa3858d3f5d128a5c8466b700098109edcad5f2repo sync      UInt32 j;
1982baa3858d3f5d128a5c8466b700098109edcad5f2repo sync      for (j = 0; j < (1 << kNumPosSlotBits); j++)
1983baa3858d3f5d128a5c8466b700098109edcad5f2repo sync        probs[j] = kProbInitValue;
1984baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    }
1985baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  }
1986baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  {
1987baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    for (i = 0; i < kNumFullDistances - kEndPosModelIndex; i++)
1988baa3858d3f5d128a5c8466b700098109edcad5f2repo sync      p->posEncoders[i] = kProbInitValue;
1989baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  }
1990baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
1991baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  LenEnc_Init(&p->lenEnc.p);
1992baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  LenEnc_Init(&p->repLenEnc.p);
1993baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
1994baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  for (i = 0; i < (1 << kNumAlignBits); i++)
1995baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    p->posAlignEncoder[i] = kProbInitValue;
1996baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
1997baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  p->optimumEndIndex = 0;
1998baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  p->optimumCurrentIndex = 0;
1999baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  p->additionalOffset = 0;
2000baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
2001baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  p->pbMask = (1 << p->pb) - 1;
2002baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  p->lpMask = (1 << p->lp) - 1;
2003baa3858d3f5d128a5c8466b700098109edcad5f2repo sync}
2004baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
2005baa3858d3f5d128a5c8466b700098109edcad5f2repo syncvoid LzmaEnc_InitPrices(CLzmaEnc *p)
2006baa3858d3f5d128a5c8466b700098109edcad5f2repo sync{
2007baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  if (!p->fastMode)
2008baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  {
2009baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    FillDistancesPrices(p);
2010baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    FillAlignPrices(p);
2011baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  }
2012baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
2013baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  p->lenEnc.tableSize =
2014baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  p->repLenEnc.tableSize =
2015baa3858d3f5d128a5c8466b700098109edcad5f2repo sync      p->numFastBytes + 1 - LZMA_MATCH_LEN_MIN;
2016baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  LenPriceEnc_UpdateTables(&p->lenEnc, 1 << p->pb, p->ProbPrices);
2017baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  LenPriceEnc_UpdateTables(&p->repLenEnc, 1 << p->pb, p->ProbPrices);
2018baa3858d3f5d128a5c8466b700098109edcad5f2repo sync}
2019baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
2020baa3858d3f5d128a5c8466b700098109edcad5f2repo syncstatic SRes LzmaEnc_AllocAndInit(CLzmaEnc *p, UInt32 keepWindowSize, ISzAlloc *alloc, ISzAlloc *allocBig)
2021baa3858d3f5d128a5c8466b700098109edcad5f2repo sync{
2022baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  UInt32 i;
2023baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  for (i = 0; i < (UInt32)kDicLogSizeMaxCompress; i++)
2024baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    if (p->dictSize <= ((UInt32)1 << i))
2025baa3858d3f5d128a5c8466b700098109edcad5f2repo sync      break;
2026baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  p->distTableSize = i * 2;
2027baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
2028baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  p->finished = False;
2029baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  p->result = SZ_OK;
2030baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  RINOK(LzmaEnc_Alloc(p, keepWindowSize, alloc, allocBig));
2031baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  LzmaEnc_Init(p);
2032baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  LzmaEnc_InitPrices(p);
2033baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  p->nowPos64 = 0;
2034baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  return SZ_OK;
2035baa3858d3f5d128a5c8466b700098109edcad5f2repo sync}
2036baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
2037baa3858d3f5d128a5c8466b700098109edcad5f2repo syncstatic SRes LzmaEnc_Prepare(CLzmaEncHandle pp, ISeqOutStream *outStream, ISeqInStream *inStream,
2038baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    ISzAlloc *alloc, ISzAlloc *allocBig)
2039baa3858d3f5d128a5c8466b700098109edcad5f2repo sync{
2040baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  CLzmaEnc *p = (CLzmaEnc *)pp;
2041baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  p->matchFinderBase.stream = inStream;
2042baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  p->needInit = 1;
2043baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  p->rc.outStream = outStream;
2044baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  return LzmaEnc_AllocAndInit(p, 0, alloc, allocBig);
2045baa3858d3f5d128a5c8466b700098109edcad5f2repo sync}
2046baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
2047baa3858d3f5d128a5c8466b700098109edcad5f2repo syncSRes LzmaEnc_PrepareForLzma2(CLzmaEncHandle pp,
2048baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    ISeqInStream *inStream, UInt32 keepWindowSize,
2049baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    ISzAlloc *alloc, ISzAlloc *allocBig)
2050baa3858d3f5d128a5c8466b700098109edcad5f2repo sync{
2051baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  CLzmaEnc *p = (CLzmaEnc *)pp;
2052baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  p->matchFinderBase.stream = inStream;
2053baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  p->needInit = 1;
2054baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  return LzmaEnc_AllocAndInit(p, keepWindowSize, alloc, allocBig);
2055baa3858d3f5d128a5c8466b700098109edcad5f2repo sync}
2056baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
2057baa3858d3f5d128a5c8466b700098109edcad5f2repo syncstatic void LzmaEnc_SetInputBuf(CLzmaEnc *p, const Byte *src, SizeT srcLen)
2058baa3858d3f5d128a5c8466b700098109edcad5f2repo sync{
2059baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  p->matchFinderBase.directInput = 1;
2060baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  p->matchFinderBase.bufferBase = (Byte *)src;
2061baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  p->matchFinderBase.directInputRem = srcLen;
2062baa3858d3f5d128a5c8466b700098109edcad5f2repo sync}
2063baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
2064baa3858d3f5d128a5c8466b700098109edcad5f2repo syncSRes LzmaEnc_MemPrepare(CLzmaEncHandle pp, const Byte *src, SizeT srcLen,
2065baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    UInt32 keepWindowSize, ISzAlloc *alloc, ISzAlloc *allocBig)
2066baa3858d3f5d128a5c8466b700098109edcad5f2repo sync{
2067baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  CLzmaEnc *p = (CLzmaEnc *)pp;
2068baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  LzmaEnc_SetInputBuf(p, src, srcLen);
2069baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  p->needInit = 1;
2070baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
2071baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  return LzmaEnc_AllocAndInit(p, keepWindowSize, alloc, allocBig);
2072baa3858d3f5d128a5c8466b700098109edcad5f2repo sync}
2073baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
2074baa3858d3f5d128a5c8466b700098109edcad5f2repo syncvoid LzmaEnc_Finish(CLzmaEncHandle pp)
2075baa3858d3f5d128a5c8466b700098109edcad5f2repo sync{
2076baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  #ifndef _7ZIP_ST
2077baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  CLzmaEnc *p = (CLzmaEnc *)pp;
2078baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  if (p->mtMode)
2079baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    MatchFinderMt_ReleaseStream(&p->matchFinderMt);
2080baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  #else
2081baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  pp = pp;
2082baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  #endif
2083baa3858d3f5d128a5c8466b700098109edcad5f2repo sync}
2084baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
2085baa3858d3f5d128a5c8466b700098109edcad5f2repo synctypedef struct
2086baa3858d3f5d128a5c8466b700098109edcad5f2repo sync{
2087baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  ISeqOutStream funcTable;
2088baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  Byte *data;
2089baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  SizeT rem;
2090baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  Bool overflow;
2091baa3858d3f5d128a5c8466b700098109edcad5f2repo sync} CSeqOutStreamBuf;
2092baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
2093baa3858d3f5d128a5c8466b700098109edcad5f2repo syncstatic size_t MyWrite(void *pp, const void *data, size_t size)
2094baa3858d3f5d128a5c8466b700098109edcad5f2repo sync{
2095baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  CSeqOutStreamBuf *p = (CSeqOutStreamBuf *)pp;
2096baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  if (p->rem < size)
2097baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  {
2098baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    size = p->rem;
2099baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    p->overflow = True;
2100baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  }
2101baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  memcpy(p->data, data, size);
2102baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  p->rem -= size;
2103baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  p->data += size;
2104baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  return size;
2105baa3858d3f5d128a5c8466b700098109edcad5f2repo sync}
2106baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
2107baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
2108baa3858d3f5d128a5c8466b700098109edcad5f2repo syncUInt32 LzmaEnc_GetNumAvailableBytes(CLzmaEncHandle pp)
2109baa3858d3f5d128a5c8466b700098109edcad5f2repo sync{
2110baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  const CLzmaEnc *p = (CLzmaEnc *)pp;
2111baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  return p->matchFinder.GetNumAvailableBytes(p->matchFinderObj);
2112baa3858d3f5d128a5c8466b700098109edcad5f2repo sync}
2113baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
2114baa3858d3f5d128a5c8466b700098109edcad5f2repo syncconst Byte *LzmaEnc_GetCurBuf(CLzmaEncHandle pp)
2115baa3858d3f5d128a5c8466b700098109edcad5f2repo sync{
2116baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  const CLzmaEnc *p = (CLzmaEnc *)pp;
2117baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  return p->matchFinder.GetPointerToCurrentPos(p->matchFinderObj) - p->additionalOffset;
2118baa3858d3f5d128a5c8466b700098109edcad5f2repo sync}
2119baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
2120baa3858d3f5d128a5c8466b700098109edcad5f2repo syncSRes LzmaEnc_CodeOneMemBlock(CLzmaEncHandle pp, Bool reInit,
2121baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    Byte *dest, size_t *destLen, UInt32 desiredPackSize, UInt32 *unpackSize)
2122baa3858d3f5d128a5c8466b700098109edcad5f2repo sync{
2123baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  CLzmaEnc *p = (CLzmaEnc *)pp;
2124baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  UInt64 nowPos64;
2125baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  SRes res;
2126baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  CSeqOutStreamBuf outStream;
2127baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
2128baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  outStream.funcTable.Write = MyWrite;
2129baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  outStream.data = dest;
2130baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  outStream.rem = *destLen;
2131baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  outStream.overflow = False;
2132baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
2133baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  p->writeEndMark = False;
2134baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  p->finished = False;
2135baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  p->result = SZ_OK;
2136baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
2137baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  if (reInit)
2138baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    LzmaEnc_Init(p);
2139baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  LzmaEnc_InitPrices(p);
2140baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  nowPos64 = p->nowPos64;
2141baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  RangeEnc_Init(&p->rc);
2142baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  p->rc.outStream = &outStream.funcTable;
2143baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
2144baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  res = LzmaEnc_CodeOneBlock(p, True, desiredPackSize, *unpackSize);
2145baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
2146baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  *unpackSize = (UInt32)(p->nowPos64 - nowPos64);
2147baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  *destLen -= outStream.rem;
2148baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  if (outStream.overflow)
2149baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    return SZ_ERROR_OUTPUT_EOF;
2150baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
2151baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  return res;
2152baa3858d3f5d128a5c8466b700098109edcad5f2repo sync}
2153baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
2154baa3858d3f5d128a5c8466b700098109edcad5f2repo syncstatic SRes LzmaEnc_Encode2(CLzmaEnc *p, ICompressProgress *progress)
2155baa3858d3f5d128a5c8466b700098109edcad5f2repo sync{
2156baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  SRes res = SZ_OK;
2157baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
2158baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  #ifndef _7ZIP_ST
2159baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  Byte allocaDummy[0x300];
2160baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  int i = 0;
2161baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  for (i = 0; i < 16; i++)
2162baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    allocaDummy[i] = (Byte)i;
2163baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  #endif
2164baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
2165baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  for (;;)
2166baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  {
2167baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    res = LzmaEnc_CodeOneBlock(p, False, 0, 0);
2168baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    if (res != SZ_OK || p->finished != 0)
2169baa3858d3f5d128a5c8466b700098109edcad5f2repo sync      break;
2170baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    if (progress != 0)
2171baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    {
2172baa3858d3f5d128a5c8466b700098109edcad5f2repo sync      res = progress->Progress(progress, p->nowPos64, RangeEnc_GetProcessed(&p->rc));
2173baa3858d3f5d128a5c8466b700098109edcad5f2repo sync      if (res != SZ_OK)
2174baa3858d3f5d128a5c8466b700098109edcad5f2repo sync      {
2175baa3858d3f5d128a5c8466b700098109edcad5f2repo sync        res = SZ_ERROR_PROGRESS;
2176baa3858d3f5d128a5c8466b700098109edcad5f2repo sync        break;
2177baa3858d3f5d128a5c8466b700098109edcad5f2repo sync      }
2178baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    }
2179baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  }
2180baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  LzmaEnc_Finish(p);
2181baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  return res;
2182baa3858d3f5d128a5c8466b700098109edcad5f2repo sync}
2183baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
2184baa3858d3f5d128a5c8466b700098109edcad5f2repo syncSRes LzmaEnc_Encode(CLzmaEncHandle pp, ISeqOutStream *outStream, ISeqInStream *inStream, ICompressProgress *progress,
2185baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    ISzAlloc *alloc, ISzAlloc *allocBig)
2186baa3858d3f5d128a5c8466b700098109edcad5f2repo sync{
2187baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  RINOK(LzmaEnc_Prepare(pp, outStream, inStream, alloc, allocBig));
2188baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  return LzmaEnc_Encode2((CLzmaEnc *)pp, progress);
2189baa3858d3f5d128a5c8466b700098109edcad5f2repo sync}
2190baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
2191baa3858d3f5d128a5c8466b700098109edcad5f2repo syncSRes LzmaEnc_WriteProperties(CLzmaEncHandle pp, Byte *props, SizeT *size)
2192baa3858d3f5d128a5c8466b700098109edcad5f2repo sync{
2193baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  CLzmaEnc *p = (CLzmaEnc *)pp;
2194baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  int i;
2195baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  UInt32 dictSize = p->dictSize;
2196baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  if (*size < LZMA_PROPS_SIZE)
2197baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    return SZ_ERROR_PARAM;
2198baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  *size = LZMA_PROPS_SIZE;
2199baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  props[0] = (Byte)((p->pb * 5 + p->lp) * 9 + p->lc);
2200baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
2201baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  for (i = 11; i <= 30; i++)
2202baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  {
2203baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    if (dictSize <= ((UInt32)2 << i))
2204baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    {
2205baa3858d3f5d128a5c8466b700098109edcad5f2repo sync      dictSize = (2 << i);
2206baa3858d3f5d128a5c8466b700098109edcad5f2repo sync      break;
2207baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    }
2208baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    if (dictSize <= ((UInt32)3 << i))
2209baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    {
2210baa3858d3f5d128a5c8466b700098109edcad5f2repo sync      dictSize = (3 << i);
2211baa3858d3f5d128a5c8466b700098109edcad5f2repo sync      break;
2212baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    }
2213baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  }
2214baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
2215baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  for (i = 0; i < 4; i++)
2216baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    props[1 + i] = (Byte)(dictSize >> (8 * i));
2217baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  return SZ_OK;
2218baa3858d3f5d128a5c8466b700098109edcad5f2repo sync}
2219baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
2220baa3858d3f5d128a5c8466b700098109edcad5f2repo syncSRes LzmaEnc_MemEncode(CLzmaEncHandle pp, Byte *dest, SizeT *destLen, const Byte *src, SizeT srcLen,
2221baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    int writeEndMark, ICompressProgress *progress, ISzAlloc *alloc, ISzAlloc *allocBig)
2222baa3858d3f5d128a5c8466b700098109edcad5f2repo sync{
2223baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  SRes res;
2224baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  CLzmaEnc *p = (CLzmaEnc *)pp;
2225baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
2226baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  CSeqOutStreamBuf outStream;
2227baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
2228baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  LzmaEnc_SetInputBuf(p, src, srcLen);
2229baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
2230baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  outStream.funcTable.Write = MyWrite;
2231baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  outStream.data = dest;
2232baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  outStream.rem = *destLen;
2233baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  outStream.overflow = False;
2234baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
2235baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  p->writeEndMark = writeEndMark;
2236baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
2237baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  p->rc.outStream = &outStream.funcTable;
2238baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  res = LzmaEnc_MemPrepare(pp, src, srcLen, 0, alloc, allocBig);
2239baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  if (res == SZ_OK)
2240baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    res = LzmaEnc_Encode2(p, progress);
2241baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
2242baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  *destLen -= outStream.rem;
2243baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  if (outStream.overflow)
2244baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    return SZ_ERROR_OUTPUT_EOF;
2245baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  return res;
2246baa3858d3f5d128a5c8466b700098109edcad5f2repo sync}
2247baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
2248baa3858d3f5d128a5c8466b700098109edcad5f2repo syncSRes LzmaEncode(Byte *dest, SizeT *destLen, const Byte *src, SizeT srcLen,
2249baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    const CLzmaEncProps *props, Byte *propsEncoded, SizeT *propsSize, int writeEndMark,
2250baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    ICompressProgress *progress, ISzAlloc *alloc, ISzAlloc *allocBig)
2251baa3858d3f5d128a5c8466b700098109edcad5f2repo sync{
2252baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  CLzmaEnc *p = (CLzmaEnc *)LzmaEnc_Create(alloc);
2253baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  SRes res;
2254baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  if (p == 0)
2255baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    return SZ_ERROR_MEM;
2256baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
2257baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  res = LzmaEnc_SetProps(p, props);
2258baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  if (res == SZ_OK)
2259baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  {
2260baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    res = LzmaEnc_WriteProperties(p, propsEncoded, propsSize);
2261baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    if (res == SZ_OK)
2262baa3858d3f5d128a5c8466b700098109edcad5f2repo sync      res = LzmaEnc_MemEncode(p, dest, destLen, src, srcLen,
2263baa3858d3f5d128a5c8466b700098109edcad5f2repo sync          writeEndMark, progress, alloc, allocBig);
2264baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  }
2265baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
2266baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  LzmaEnc_Destroy(p, alloc, allocBig);
2267baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  return res;
2268baa3858d3f5d128a5c8466b700098109edcad5f2repo sync}
2269