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