blob: e7d14c59a8174a582d2522df4c75e393cecc471f [file] [log] [blame]
Stefan Reinauer2e200cd2012-10-30 14:02:45 -07001/* LzmaEnc.c -- LZMA Encoder
22009-11-24 : Igor Pavlov : Public domain */
3
4#include <string.h>
5
Stefan Reinauer2e200cd2012-10-30 14:02:45 -07006#include "LzmaEnc.h"
7
8#include "LzFind.h"
Stefan Reinauer2e200cd2012-10-30 14:02:45 -07009
10#define kBlockSizeMax ((1 << LZMA_NUM_BLOCK_SIZE_BITS) - 1)
11
12#define kBlockSize (9 << 10)
13#define kUnpackBlockSize (1 << 18)
14#define kMatchArraySize (1 << 21)
15#define kMatchRecordMaxSize ((LZMA_MATCH_LEN_MAX * 2 + 3) * LZMA_MATCH_LEN_MAX)
16
17#define kNumMaxDirectBits (31)
18
19#define kNumTopBits 24
Alexandru Gagniuc91e9f272014-01-26 22:55:01 -060020#define kTopValue ((uint32_t)1 << kNumTopBits)
Stefan Reinauer2e200cd2012-10-30 14:02:45 -070021
22#define kNumBitModelTotalBits 11
23#define kBitModelTotal (1 << kNumBitModelTotalBits)
24#define kNumMoveBits 5
25#define kProbInitValue (kBitModelTotal >> 1)
26
27#define kNumMoveReducingBits 4
28#define kNumBitPriceShiftBits 4
29#define kBitPrice (1 << kNumBitPriceShiftBits)
30
Alexandru Gagniuc9ad52fe2014-01-27 20:57:54 -060031void LzmaEncProps_Init(struct CLzmaEncProps *p)
Stefan Reinauer2e200cd2012-10-30 14:02:45 -070032{
33 p->level = 5;
34 p->dictSize = p->mc = 0;
35 p->lc = p->lp = p->pb = p->algo = p->fb = p->btMode = p->numHashBytes = p->numThreads = -1;
36 p->writeEndMark = 0;
37}
38
Alexandru Gagniuc9ad52fe2014-01-27 20:57:54 -060039void LzmaEncProps_Normalize(struct CLzmaEncProps *p)
Stefan Reinauer2e200cd2012-10-30 14:02:45 -070040{
41 int level = p->level;
42 if (level < 0) level = 5;
43 p->level = level;
44 if (p->dictSize == 0) p->dictSize = (level <= 5 ? (1 << (level * 2 + 14)) : (level == 6 ? (1 << 25) : (1 << 26)));
45 if (p->lc < 0) p->lc = 3;
46 if (p->lp < 0) p->lp = 0;
47 if (p->pb < 0) p->pb = 2;
48 if (p->algo < 0) p->algo = (level < 5 ? 0 : 1);
49 if (p->fb < 0) p->fb = (level < 7 ? 32 : 64);
50 if (p->btMode < 0) p->btMode = (p->algo == 0 ? 0 : 1);
51 if (p->numHashBytes < 0) p->numHashBytes = 4;
52 if (p->mc == 0) p->mc = (16 + (p->fb >> 1)) >> (p->btMode ? 0 : 1);
53 if (p->numThreads < 0)
Alexandru Gagniuc5edfa372014-01-29 16:50:40 -060054 p->numThreads = 1;
Stefan Reinauer2e200cd2012-10-30 14:02:45 -070055}
56
Alexandru Gagniuc9ad52fe2014-01-27 20:57:54 -060057uint32_t LzmaEncProps_GetDictSize(const struct CLzmaEncProps *props2)
Stefan Reinauer2e200cd2012-10-30 14:02:45 -070058{
Alexandru Gagniuc9ad52fe2014-01-27 20:57:54 -060059 struct CLzmaEncProps props = *props2;
Stefan Reinauer2e200cd2012-10-30 14:02:45 -070060 LzmaEncProps_Normalize(&props);
61 return props.dictSize;
62}
63
Stefan Reinauer2e200cd2012-10-30 14:02:45 -070064#define kNumLogBits (9 + (int)sizeof(size_t) / 2)
65#define kDicLogSizeMaxCompress ((kNumLogBits - 1) * 2 + 7)
66
Alexandru Gagniuc91e9f272014-01-26 22:55:01 -060067static void LzmaEnc_FastPosInit(uint8_t *g_FastPos)
Stefan Reinauer2e200cd2012-10-30 14:02:45 -070068{
69 int c = 2, slotFast;
70 g_FastPos[0] = 0;
71 g_FastPos[1] = 1;
Kyösti Mälkkiecd84242013-09-13 07:57:49 +030072
Stefan Reinauer2e200cd2012-10-30 14:02:45 -070073 for (slotFast = 2; slotFast < kNumLogBits * 2; slotFast++)
74 {
Alexandru Gagniuc91e9f272014-01-26 22:55:01 -060075 uint32_t k = (1 << ((slotFast >> 1) - 1));
76 uint32_t j;
Stefan Reinauer2e200cd2012-10-30 14:02:45 -070077 for (j = 0; j < k; j++, c++)
Alexandru Gagniuc91e9f272014-01-26 22:55:01 -060078 g_FastPos[c] = (uint8_t)slotFast;
Stefan Reinauer2e200cd2012-10-30 14:02:45 -070079 }
80}
81
Alexandru Gagniucb63b75b2014-01-29 14:56:20 -060082#define BSR2_RET(pos, res) { uint32_t macro_i = 6 + ((kNumLogBits - 1) & \
Alexandru Gagniuc91e9f272014-01-26 22:55:01 -060083 (0 - (((((uint32_t)1 << (kNumLogBits + 6)) - 1) - pos) >> 31))); \
Alexandru Gagniucb63b75b2014-01-29 14:56:20 -060084 res = p->g_FastPos[pos >> macro_i] + (macro_i * 2); }
Stefan Reinauer2e200cd2012-10-30 14:02:45 -070085/*
86#define BSR2_RET(pos, res) { res = (pos < (1 << (kNumLogBits + 6))) ? \
87 p->g_FastPos[pos >> 6] + 12 : \
88 p->g_FastPos[pos >> (6 + kNumLogBits - 1)] + (6 + (kNumLogBits - 1)) * 2; }
89*/
90
91#define GetPosSlot1(pos) p->g_FastPos[pos]
92#define GetPosSlot2(pos, res) { BSR2_RET(pos, res); }
93#define GetPosSlot(pos, res) { if (pos < kNumFullDistances) res = p->g_FastPos[pos]; else BSR2_RET(pos, res); }
94
Stefan Reinauer2e200cd2012-10-30 14:02:45 -070095#define LZMA_NUM_REPS 4
96
97typedef unsigned CState;
98
Alexandru Gagniuc9ad52fe2014-01-27 20:57:54 -060099struct COptimal
Stefan Reinauer2e200cd2012-10-30 14:02:45 -0700100{
Alexandru Gagniuc91e9f272014-01-26 22:55:01 -0600101 uint32_t price;
Stefan Reinauer2e200cd2012-10-30 14:02:45 -0700102
103 CState state;
104 int prev1IsChar;
105 int prev2;
106
Alexandru Gagniuc91e9f272014-01-26 22:55:01 -0600107 uint32_t posPrev2;
108 uint32_t backPrev2;
Stefan Reinauer2e200cd2012-10-30 14:02:45 -0700109
Alexandru Gagniuc91e9f272014-01-26 22:55:01 -0600110 uint32_t posPrev;
111 uint32_t backPrev;
112 uint32_t backs[LZMA_NUM_REPS];
Alexandru Gagniuc9ad52fe2014-01-27 20:57:54 -0600113};
Stefan Reinauer2e200cd2012-10-30 14:02:45 -0700114
115#define kNumOpts (1 << 12)
116
117#define kNumLenToPosStates 4
118#define kNumPosSlotBits 6
119#define kDicLogSizeMin 0
120#define kDicLogSizeMax 32
121#define kDistTableSizeMax (kDicLogSizeMax * 2)
122
123
124#define kNumAlignBits 4
125#define kAlignTableSize (1 << kNumAlignBits)
126#define kAlignMask (kAlignTableSize - 1)
127
128#define kStartPosModelIndex 4
129#define kEndPosModelIndex 14
130#define kNumPosModels (kEndPosModelIndex - kStartPosModelIndex)
131
132#define kNumFullDistances (1 << (kEndPosModelIndex >> 1))
133
Alexandru Gagniuc7c9bb412014-01-29 16:55:48 -0600134typedef uint16_t CLzmaProb;
135
Stefan Reinauer2e200cd2012-10-30 14:02:45 -0700136
137#define LZMA_PB_MAX 4
138#define LZMA_LC_MAX 8
139#define LZMA_LP_MAX 4
140
141#define LZMA_NUM_PB_STATES_MAX (1 << LZMA_PB_MAX)
142
143
144#define kLenNumLowBits 3
145#define kLenNumLowSymbols (1 << kLenNumLowBits)
146#define kLenNumMidBits 3
147#define kLenNumMidSymbols (1 << kLenNumMidBits)
148#define kLenNumHighBits 8
149#define kLenNumHighSymbols (1 << kLenNumHighBits)
150
151#define kLenNumSymbolsTotal (kLenNumLowSymbols + kLenNumMidSymbols + kLenNumHighSymbols)
152
153#define LZMA_MATCH_LEN_MIN 2
154#define LZMA_MATCH_LEN_MAX (LZMA_MATCH_LEN_MIN + kLenNumSymbolsTotal - 1)
155
156#define kNumStates 12
157
Alexandru Gagniuc9ad52fe2014-01-27 20:57:54 -0600158struct CLenEnc
Stefan Reinauer2e200cd2012-10-30 14:02:45 -0700159{
160 CLzmaProb choice;
161 CLzmaProb choice2;
162 CLzmaProb low[LZMA_NUM_PB_STATES_MAX << kLenNumLowBits];
163 CLzmaProb mid[LZMA_NUM_PB_STATES_MAX << kLenNumMidBits];
164 CLzmaProb high[kLenNumHighSymbols];
Alexandru Gagniuc9ad52fe2014-01-27 20:57:54 -0600165};
Stefan Reinauer2e200cd2012-10-30 14:02:45 -0700166
Alexandru Gagniuc9ad52fe2014-01-27 20:57:54 -0600167struct CLenPriceEnc
Stefan Reinauer2e200cd2012-10-30 14:02:45 -0700168{
Alexandru Gagniuc9ad52fe2014-01-27 20:57:54 -0600169 struct CLenEnc p;
Alexandru Gagniuc91e9f272014-01-26 22:55:01 -0600170 uint32_t prices[LZMA_NUM_PB_STATES_MAX][kLenNumSymbolsTotal];
171 uint32_t tableSize;
172 uint32_t counters[LZMA_NUM_PB_STATES_MAX];
Alexandru Gagniuc9ad52fe2014-01-27 20:57:54 -0600173};
Stefan Reinauer2e200cd2012-10-30 14:02:45 -0700174
Alexandru Gagniuc9ad52fe2014-01-27 20:57:54 -0600175struct CRangeEnc
Stefan Reinauer2e200cd2012-10-30 14:02:45 -0700176{
Alexandru Gagniuc91e9f272014-01-26 22:55:01 -0600177 uint32_t range;
178 uint8_t cache;
179 uint64_t low;
180 uint64_t cacheSize;
181 uint8_t *buf;
182 uint8_t *bufLim;
183 uint8_t *bufBase;
Alexandru Gagniuc9ad52fe2014-01-27 20:57:54 -0600184 struct ISeqOutStream *outStream;
Alexandru Gagniuc91e9f272014-01-26 22:55:01 -0600185 uint64_t processed;
Stefan Reinauer2e200cd2012-10-30 14:02:45 -0700186 SRes res;
Alexandru Gagniuc9ad52fe2014-01-27 20:57:54 -0600187};
Stefan Reinauer2e200cd2012-10-30 14:02:45 -0700188
Alexandru Gagniuc9ad52fe2014-01-27 20:57:54 -0600189struct CSaveState
Stefan Reinauer2e200cd2012-10-30 14:02:45 -0700190{
191 CLzmaProb *litProbs;
192
193 CLzmaProb isMatch[kNumStates][LZMA_NUM_PB_STATES_MAX];
194 CLzmaProb isRep[kNumStates];
195 CLzmaProb isRepG0[kNumStates];
196 CLzmaProb isRepG1[kNumStates];
197 CLzmaProb isRepG2[kNumStates];
198 CLzmaProb isRep0Long[kNumStates][LZMA_NUM_PB_STATES_MAX];
199
200 CLzmaProb posSlotEncoder[kNumLenToPosStates][1 << kNumPosSlotBits];
201 CLzmaProb posEncoders[kNumFullDistances - kEndPosModelIndex];
202 CLzmaProb posAlignEncoder[1 << kNumAlignBits];
Kyösti Mälkkiecd84242013-09-13 07:57:49 +0300203
Alexandru Gagniuc9ad52fe2014-01-27 20:57:54 -0600204 struct CLenPriceEnc lenEnc;
205 struct CLenPriceEnc repLenEnc;
Stefan Reinauer2e200cd2012-10-30 14:02:45 -0700206
Alexandru Gagniuc91e9f272014-01-26 22:55:01 -0600207 uint32_t reps[LZMA_NUM_REPS];
208 uint32_t state;
Alexandru Gagniuc9ad52fe2014-01-27 20:57:54 -0600209};
Stefan Reinauer2e200cd2012-10-30 14:02:45 -0700210
Alexandru Gagniuc9ad52fe2014-01-27 20:57:54 -0600211struct CLzmaEnc
Stefan Reinauer2e200cd2012-10-30 14:02:45 -0700212{
Alexandru Gagniuc9ad52fe2014-01-27 20:57:54 -0600213 struct IMatchFinder matchFinder;
Stefan Reinauer2e200cd2012-10-30 14:02:45 -0700214 void *matchFinderObj;
215
Alexandru Gagniuc9ad52fe2014-01-27 20:57:54 -0600216 struct CMatchFinder matchFinderBase;
Stefan Reinauer2e200cd2012-10-30 14:02:45 -0700217
Alexandru Gagniuc91e9f272014-01-26 22:55:01 -0600218 uint32_t optimumEndIndex;
219 uint32_t optimumCurrentIndex;
Stefan Reinauer2e200cd2012-10-30 14:02:45 -0700220
Alexandru Gagniuc91e9f272014-01-26 22:55:01 -0600221 uint32_t longestMatchLength;
222 uint32_t numPairs;
223 uint32_t numAvail;
Alexandru Gagniuc9ad52fe2014-01-27 20:57:54 -0600224 struct COptimal opt[kNumOpts];
Kyösti Mälkkiecd84242013-09-13 07:57:49 +0300225
Stefan Reinauer2e200cd2012-10-30 14:02:45 -0700226 #ifndef LZMA_LOG_BSR
Alexandru Gagniuc91e9f272014-01-26 22:55:01 -0600227 uint8_t g_FastPos[1 << kNumLogBits];
Stefan Reinauer2e200cd2012-10-30 14:02:45 -0700228 #endif
229
Alexandru Gagniuc91e9f272014-01-26 22:55:01 -0600230 uint32_t ProbPrices[kBitModelTotal >> kNumMoveReducingBits];
231 uint32_t matches[LZMA_MATCH_LEN_MAX * 2 + 2 + 1];
232 uint32_t numFastuint8_ts;
233 uint32_t additionalOffset;
234 uint32_t reps[LZMA_NUM_REPS];
235 uint32_t state;
Stefan Reinauer2e200cd2012-10-30 14:02:45 -0700236
Alexandru Gagniuc91e9f272014-01-26 22:55:01 -0600237 uint32_t posSlotPrices[kNumLenToPosStates][kDistTableSizeMax];
238 uint32_t distancesPrices[kNumLenToPosStates][kNumFullDistances];
239 uint32_t alignPrices[kAlignTableSize];
240 uint32_t alignPriceCount;
Stefan Reinauer2e200cd2012-10-30 14:02:45 -0700241
Alexandru Gagniuc91e9f272014-01-26 22:55:01 -0600242 uint32_t distTableSize;
Stefan Reinauer2e200cd2012-10-30 14:02:45 -0700243
244 unsigned lc, lp, pb;
245 unsigned lpMask, pbMask;
246
247 CLzmaProb *litProbs;
248
249 CLzmaProb isMatch[kNumStates][LZMA_NUM_PB_STATES_MAX];
250 CLzmaProb isRep[kNumStates];
251 CLzmaProb isRepG0[kNumStates];
252 CLzmaProb isRepG1[kNumStates];
253 CLzmaProb isRepG2[kNumStates];
254 CLzmaProb isRep0Long[kNumStates][LZMA_NUM_PB_STATES_MAX];
255
256 CLzmaProb posSlotEncoder[kNumLenToPosStates][1 << kNumPosSlotBits];
257 CLzmaProb posEncoders[kNumFullDistances - kEndPosModelIndex];
258 CLzmaProb posAlignEncoder[1 << kNumAlignBits];
Kyösti Mälkkiecd84242013-09-13 07:57:49 +0300259
Alexandru Gagniuc9ad52fe2014-01-27 20:57:54 -0600260 struct CLenPriceEnc lenEnc;
261 struct CLenPriceEnc repLenEnc;
Stefan Reinauer2e200cd2012-10-30 14:02:45 -0700262
263 unsigned lclp;
264
Alexandru Gagniuc91e9f272014-01-26 22:55:01 -0600265 bool fastMode;
Kyösti Mälkkiecd84242013-09-13 07:57:49 +0300266
Alexandru Gagniuc9ad52fe2014-01-27 20:57:54 -0600267 struct CRangeEnc rc;
Stefan Reinauer2e200cd2012-10-30 14:02:45 -0700268
Alexandru Gagniuc91e9f272014-01-26 22:55:01 -0600269 bool writeEndMark;
270 uint64_t nowPos64;
271 uint32_t matchPriceCount;
272 bool finished;
273 bool multiThread;
Stefan Reinauer2e200cd2012-10-30 14:02:45 -0700274
275 SRes result;
Alexandru Gagniuc91e9f272014-01-26 22:55:01 -0600276 uint32_t dictSize;
277 uint32_t matchFinderCycles;
Stefan Reinauer2e200cd2012-10-30 14:02:45 -0700278
279 int needInit;
280
Alexandru Gagniuc9ad52fe2014-01-27 20:57:54 -0600281 struct CSaveState saveState;
282};
Stefan Reinauer2e200cd2012-10-30 14:02:45 -0700283
284/*static void LzmaEnc_SaveState(CLzmaEncHandle pp)
285{
286 CLzmaEnc *p = (CLzmaEnc *)pp;
287 CSaveState *dest = &p->saveState;
288 int i;
289 dest->lenEnc = p->lenEnc;
290 dest->repLenEnc = p->repLenEnc;
291 dest->state = p->state;
292
293 for (i = 0; i < kNumStates; i++)
294 {
295 memcpy(dest->isMatch[i], p->isMatch[i], sizeof(p->isMatch[i]));
296 memcpy(dest->isRep0Long[i], p->isRep0Long[i], sizeof(p->isRep0Long[i]));
297 }
298 for (i = 0; i < kNumLenToPosStates; i++)
299 memcpy(dest->posSlotEncoder[i], p->posSlotEncoder[i], sizeof(p->posSlotEncoder[i]));
300 memcpy(dest->isRep, p->isRep, sizeof(p->isRep));
301 memcpy(dest->isRepG0, p->isRepG0, sizeof(p->isRepG0));
302 memcpy(dest->isRepG1, p->isRepG1, sizeof(p->isRepG1));
303 memcpy(dest->isRepG2, p->isRepG2, sizeof(p->isRepG2));
304 memcpy(dest->posEncoders, p->posEncoders, sizeof(p->posEncoders));
305 memcpy(dest->posAlignEncoder, p->posAlignEncoder, sizeof(p->posAlignEncoder));
306 memcpy(dest->reps, p->reps, sizeof(p->reps));
307 memcpy(dest->litProbs, p->litProbs, (0x300 << p->lclp) * sizeof(CLzmaProb));
308}*/
309
310/*static void LzmaEnc_RestoreState(CLzmaEncHandle pp)
311{
312 CLzmaEnc *dest = (CLzmaEnc *)pp;
313 const CSaveState *p = &dest->saveState;
314 int i;
315 dest->lenEnc = p->lenEnc;
316 dest->repLenEnc = p->repLenEnc;
317 dest->state = p->state;
318
319 for (i = 0; i < kNumStates; i++)
320 {
321 memcpy(dest->isMatch[i], p->isMatch[i], sizeof(p->isMatch[i]));
322 memcpy(dest->isRep0Long[i], p->isRep0Long[i], sizeof(p->isRep0Long[i]));
323 }
324 for (i = 0; i < kNumLenToPosStates; i++)
325 memcpy(dest->posSlotEncoder[i], p->posSlotEncoder[i], sizeof(p->posSlotEncoder[i]));
326 memcpy(dest->isRep, p->isRep, sizeof(p->isRep));
327 memcpy(dest->isRepG0, p->isRepG0, sizeof(p->isRepG0));
328 memcpy(dest->isRepG1, p->isRepG1, sizeof(p->isRepG1));
329 memcpy(dest->isRepG2, p->isRepG2, sizeof(p->isRepG2));
330 memcpy(dest->posEncoders, p->posEncoders, sizeof(p->posEncoders));
331 memcpy(dest->posAlignEncoder, p->posAlignEncoder, sizeof(p->posAlignEncoder));
332 memcpy(dest->reps, p->reps, sizeof(p->reps));
333 memcpy(dest->litProbs, p->litProbs, (0x300 << dest->lclp) * sizeof(CLzmaProb));
334}*/
335
Alexandru Gagniuc9ad52fe2014-01-27 20:57:54 -0600336SRes LzmaEnc_SetProps(CLzmaEncHandle pp, const struct CLzmaEncProps *props2)
Stefan Reinauer2e200cd2012-10-30 14:02:45 -0700337{
Alexandru Gagniuc9ad52fe2014-01-27 20:57:54 -0600338 struct CLzmaEnc *p = (struct CLzmaEnc *)pp;
339 struct CLzmaEncProps props = *props2;
Stefan Reinauer2e200cd2012-10-30 14:02:45 -0700340 LzmaEncProps_Normalize(&props);
341
342 if (props.lc > LZMA_LC_MAX || props.lp > LZMA_LP_MAX || props.pb > LZMA_PB_MAX ||
343 props.dictSize > (1 << kDicLogSizeMaxCompress) || props.dictSize > (1 << 30))
344 return SZ_ERROR_PARAM;
345 p->dictSize = props.dictSize;
346 p->matchFinderCycles = props.mc;
347 {
348 unsigned fb = props.fb;
349 if (fb < 5)
350 fb = 5;
351 if (fb > LZMA_MATCH_LEN_MAX)
352 fb = LZMA_MATCH_LEN_MAX;
Alexandru Gagniuc91e9f272014-01-26 22:55:01 -0600353 p->numFastuint8_ts = fb;
Stefan Reinauer2e200cd2012-10-30 14:02:45 -0700354 }
355 p->lc = props.lc;
356 p->lp = props.lp;
357 p->pb = props.pb;
358 p->fastMode = (props.algo == 0);
359 p->matchFinderBase.btMode = props.btMode;
360 {
Alexandru Gagniuc91e9f272014-01-26 22:55:01 -0600361 uint32_t numHashBytes = 4;
Stefan Reinauer2e200cd2012-10-30 14:02:45 -0700362 if (props.btMode)
363 {
364 if (props.numHashBytes < 2)
365 numHashBytes = 2;
366 else if (props.numHashBytes < 4)
367 numHashBytes = props.numHashBytes;
368 }
369 p->matchFinderBase.numHashBytes = numHashBytes;
370 }
371
372 p->matchFinderBase.cutValue = props.mc;
373
374 p->writeEndMark = props.writeEndMark;
375
Stefan Reinauer2e200cd2012-10-30 14:02:45 -0700376 return SZ_OK;
377}
378
379static const int kLiteralNextStates[kNumStates] = {0, 0, 0, 0, 1, 2, 3, 4, 5, 6, 4, 5};
380static const int kMatchNextStates[kNumStates] = {7, 7, 7, 7, 7, 7, 7, 10, 10, 10, 10, 10};
381static const int kRepNextStates[kNumStates] = {8, 8, 8, 8, 8, 8, 8, 11, 11, 11, 11, 11};
382static const int kShortRepNextStates[kNumStates]= {9, 9, 9, 9, 9, 9, 9, 11, 11, 11, 11, 11};
383
384#define IsCharState(s) ((s) < 7)
385
386#define GetLenToPosState(len) (((len) < kNumLenToPosStates + 1) ? (len) - 2 : kNumLenToPosStates - 1)
387
388#define kInfinityPrice (1 << 30)
389
Alexandru Gagniuc9ad52fe2014-01-27 20:57:54 -0600390static void RangeEnc_Construct(struct CRangeEnc *p)
Stefan Reinauer2e200cd2012-10-30 14:02:45 -0700391{
392 p->outStream = 0;
393 p->bufBase = 0;
394}
395
396#define RangeEnc_GetProcessed(p) ((p)->processed + ((p)->buf - (p)->bufBase) + (p)->cacheSize)
397
398#define RC_BUF_SIZE (1 << 16)
Alexandru Gagniuc9ad52fe2014-01-27 20:57:54 -0600399static int RangeEnc_Alloc(struct CRangeEnc *p, struct ISzAlloc *alloc)
Stefan Reinauer2e200cd2012-10-30 14:02:45 -0700400{
401 if (p->bufBase == 0)
402 {
Alexandru Gagniuc91e9f272014-01-26 22:55:01 -0600403 p->bufBase = (uint8_t *)alloc->Alloc(alloc, RC_BUF_SIZE);
Stefan Reinauer2e200cd2012-10-30 14:02:45 -0700404 if (p->bufBase == 0)
405 return 0;
406 p->bufLim = p->bufBase + RC_BUF_SIZE;
407 }
408 return 1;
409}
410
Alexandru Gagniuc9ad52fe2014-01-27 20:57:54 -0600411static void RangeEnc_Free(struct CRangeEnc *p, struct ISzAlloc *alloc)
Stefan Reinauer2e200cd2012-10-30 14:02:45 -0700412{
413 alloc->Free(alloc, p->bufBase);
414 p->bufBase = 0;
415}
416
Alexandru Gagniuc9ad52fe2014-01-27 20:57:54 -0600417static void RangeEnc_Init(struct CRangeEnc *p)
Stefan Reinauer2e200cd2012-10-30 14:02:45 -0700418{
419 /* Stream.Init(); */
420 p->low = 0;
421 p->range = 0xFFFFFFFF;
422 p->cacheSize = 1;
423 p->cache = 0;
424
425 p->buf = p->bufBase;
426
427 p->processed = 0;
428 p->res = SZ_OK;
429}
430
Alexandru Gagniuc9ad52fe2014-01-27 20:57:54 -0600431static void RangeEnc_FlushStream(struct CRangeEnc *p)
Stefan Reinauer2e200cd2012-10-30 14:02:45 -0700432{
433 size_t num;
434 if (p->res != SZ_OK)
435 return;
436 num = p->buf - p->bufBase;
437 if (num != p->outStream->Write(p->outStream, p->bufBase, num))
438 p->res = SZ_ERROR_WRITE;
439 p->processed += num;
440 p->buf = p->bufBase;
441}
442
Alexandru Gagniuc9ad52fe2014-01-27 20:57:54 -0600443static void RangeEnc_ShiftLow(struct CRangeEnc *p)
Stefan Reinauer2e200cd2012-10-30 14:02:45 -0700444{
Alexandru Gagniuc91e9f272014-01-26 22:55:01 -0600445 if ((uint32_t)p->low < (uint32_t)0xFF000000 || (int)(p->low >> 32) != 0)
Stefan Reinauer2e200cd2012-10-30 14:02:45 -0700446 {
Alexandru Gagniuc91e9f272014-01-26 22:55:01 -0600447 uint8_t temp = p->cache;
Stefan Reinauer2e200cd2012-10-30 14:02:45 -0700448 do
449 {
Alexandru Gagniuc91e9f272014-01-26 22:55:01 -0600450 uint8_t *buf = p->buf;
451 *buf++ = (uint8_t)(temp + (uint8_t)(p->low >> 32));
Stefan Reinauer2e200cd2012-10-30 14:02:45 -0700452 p->buf = buf;
453 if (buf == p->bufLim)
454 RangeEnc_FlushStream(p);
455 temp = 0xFF;
456 }
457 while (--p->cacheSize != 0);
Alexandru Gagniuc91e9f272014-01-26 22:55:01 -0600458 p->cache = (uint8_t)((uint32_t)p->low >> 24);
Stefan Reinauer2e200cd2012-10-30 14:02:45 -0700459 }
460 p->cacheSize++;
Alexandru Gagniuc91e9f272014-01-26 22:55:01 -0600461 p->low = (uint32_t)p->low << 8;
Stefan Reinauer2e200cd2012-10-30 14:02:45 -0700462}
463
Alexandru Gagniuc9ad52fe2014-01-27 20:57:54 -0600464static void RangeEnc_FlushData(struct CRangeEnc *p)
Stefan Reinauer2e200cd2012-10-30 14:02:45 -0700465{
466 int i;
467 for (i = 0; i < 5; i++)
468 RangeEnc_ShiftLow(p);
469}
470
Alexandru Gagniuc9ad52fe2014-01-27 20:57:54 -0600471static void RangeEnc_EncodeDirectBits(struct CRangeEnc *p, uint32_t value, int numBits)
Stefan Reinauer2e200cd2012-10-30 14:02:45 -0700472{
473 do
474 {
475 p->range >>= 1;
476 p->low += p->range & (0 - ((value >> --numBits) & 1));
477 if (p->range < kTopValue)
478 {
479 p->range <<= 8;
480 RangeEnc_ShiftLow(p);
481 }
482 }
483 while (numBits != 0);
484}
485
Alexandru Gagniuc9ad52fe2014-01-27 20:57:54 -0600486static void RangeEnc_EncodeBit(struct CRangeEnc *p, CLzmaProb *prob, uint32_t symbol)
Stefan Reinauer2e200cd2012-10-30 14:02:45 -0700487{
Alexandru Gagniuc91e9f272014-01-26 22:55:01 -0600488 uint32_t ttt = *prob;
489 uint32_t newBound = (p->range >> kNumBitModelTotalBits) * ttt;
Stefan Reinauer2e200cd2012-10-30 14:02:45 -0700490 if (symbol == 0)
491 {
492 p->range = newBound;
493 ttt += (kBitModelTotal - ttt) >> kNumMoveBits;
494 }
495 else
496 {
497 p->low += newBound;
498 p->range -= newBound;
499 ttt -= ttt >> kNumMoveBits;
500 }
501 *prob = (CLzmaProb)ttt;
502 if (p->range < kTopValue)
503 {
504 p->range <<= 8;
505 RangeEnc_ShiftLow(p);
506 }
507}
508
Alexandru Gagniuc9ad52fe2014-01-27 20:57:54 -0600509static void LitEnc_Encode(struct CRangeEnc *p, CLzmaProb *probs, uint32_t symbol)
Stefan Reinauer2e200cd2012-10-30 14:02:45 -0700510{
511 symbol |= 0x100;
512 do
513 {
514 RangeEnc_EncodeBit(p, probs + (symbol >> 8), (symbol >> 7) & 1);
515 symbol <<= 1;
516 }
517 while (symbol < 0x10000);
518}
519
Alexandru Gagniuc9ad52fe2014-01-27 20:57:54 -0600520static void LitEnc_EncodeMatched(struct CRangeEnc *p, CLzmaProb *probs, uint32_t symbol, uint32_t matchuint8_t)
Stefan Reinauer2e200cd2012-10-30 14:02:45 -0700521{
Alexandru Gagniuc91e9f272014-01-26 22:55:01 -0600522 uint32_t offs = 0x100;
Stefan Reinauer2e200cd2012-10-30 14:02:45 -0700523 symbol |= 0x100;
524 do
525 {
Alexandru Gagniuc91e9f272014-01-26 22:55:01 -0600526 matchuint8_t <<= 1;
527 RangeEnc_EncodeBit(p, probs + (offs + (matchuint8_t & offs) + (symbol >> 8)), (symbol >> 7) & 1);
Stefan Reinauer2e200cd2012-10-30 14:02:45 -0700528 symbol <<= 1;
Alexandru Gagniuc91e9f272014-01-26 22:55:01 -0600529 offs &= ~(matchuint8_t ^ symbol);
Stefan Reinauer2e200cd2012-10-30 14:02:45 -0700530 }
531 while (symbol < 0x10000);
532}
533
Alexandru Gagniuc91e9f272014-01-26 22:55:01 -0600534static void LzmaEnc_InitPriceTables(uint32_t *ProbPrices)
Stefan Reinauer2e200cd2012-10-30 14:02:45 -0700535{
Alexandru Gagniuc91e9f272014-01-26 22:55:01 -0600536 uint32_t i;
Stefan Reinauer2e200cd2012-10-30 14:02:45 -0700537 for (i = (1 << kNumMoveReducingBits) / 2; i < kBitModelTotal; i += (1 << kNumMoveReducingBits))
538 {
539 const int kCyclesBits = kNumBitPriceShiftBits;
Alexandru Gagniuc91e9f272014-01-26 22:55:01 -0600540 uint32_t w = i;
541 uint32_t bitCount = 0;
Stefan Reinauer2e200cd2012-10-30 14:02:45 -0700542 int j;
543 for (j = 0; j < kCyclesBits; j++)
544 {
545 w = w * w;
546 bitCount <<= 1;
Alexandru Gagniuc91e9f272014-01-26 22:55:01 -0600547 while (w >= ((uint32_t)1 << 16))
Stefan Reinauer2e200cd2012-10-30 14:02:45 -0700548 {
549 w >>= 1;
550 bitCount++;
551 }
552 }
553 ProbPrices[i >> kNumMoveReducingBits] = ((kNumBitModelTotalBits << kCyclesBits) - 15 - bitCount);
554 }
555}
556
557
558#define GET_PRICE(prob, symbol) \
559 p->ProbPrices[((prob) ^ (((-(int)(symbol))) & (kBitModelTotal - 1))) >> kNumMoveReducingBits];
560
561#define GET_PRICEa(prob, symbol) \
562 ProbPrices[((prob) ^ ((-((int)(symbol))) & (kBitModelTotal - 1))) >> kNumMoveReducingBits];
563
564#define GET_PRICE_0(prob) p->ProbPrices[(prob) >> kNumMoveReducingBits]
565#define GET_PRICE_1(prob) p->ProbPrices[((prob) ^ (kBitModelTotal - 1)) >> kNumMoveReducingBits]
566
567#define GET_PRICE_0a(prob) ProbPrices[(prob) >> kNumMoveReducingBits]
568#define GET_PRICE_1a(prob) ProbPrices[((prob) ^ (kBitModelTotal - 1)) >> kNumMoveReducingBits]
569
Alexandru Gagniuc91e9f272014-01-26 22:55:01 -0600570static uint32_t LitEnc_GetPrice(const CLzmaProb *probs, uint32_t symbol, uint32_t *ProbPrices)
Stefan Reinauer2e200cd2012-10-30 14:02:45 -0700571{
Alexandru Gagniuc91e9f272014-01-26 22:55:01 -0600572 uint32_t price = 0;
Stefan Reinauer2e200cd2012-10-30 14:02:45 -0700573 symbol |= 0x100;
574 do
575 {
576 price += GET_PRICEa(probs[symbol >> 8], (symbol >> 7) & 1);
577 symbol <<= 1;
578 }
579 while (symbol < 0x10000);
580 return price;
581}
582
Alexandru Gagniuc91e9f272014-01-26 22:55:01 -0600583static uint32_t LitEnc_GetPriceMatched(const CLzmaProb *probs, uint32_t symbol, uint32_t matchuint8_t, uint32_t *ProbPrices)
Stefan Reinauer2e200cd2012-10-30 14:02:45 -0700584{
Alexandru Gagniuc91e9f272014-01-26 22:55:01 -0600585 uint32_t price = 0;
586 uint32_t offs = 0x100;
Stefan Reinauer2e200cd2012-10-30 14:02:45 -0700587 symbol |= 0x100;
588 do
589 {
Alexandru Gagniuc91e9f272014-01-26 22:55:01 -0600590 matchuint8_t <<= 1;
591 price += GET_PRICEa(probs[offs + (matchuint8_t & offs) + (symbol >> 8)], (symbol >> 7) & 1);
Stefan Reinauer2e200cd2012-10-30 14:02:45 -0700592 symbol <<= 1;
Alexandru Gagniuc91e9f272014-01-26 22:55:01 -0600593 offs &= ~(matchuint8_t ^ symbol);
Stefan Reinauer2e200cd2012-10-30 14:02:45 -0700594 }
595 while (symbol < 0x10000);
596 return price;
597}
598
599
Alexandru Gagniuc9ad52fe2014-01-27 20:57:54 -0600600static void RcTree_Encode(struct CRangeEnc *rc, CLzmaProb *probs, int numBitLevels, uint32_t symbol)
Stefan Reinauer2e200cd2012-10-30 14:02:45 -0700601{
Alexandru Gagniuc91e9f272014-01-26 22:55:01 -0600602 uint32_t m = 1;
Stefan Reinauer2e200cd2012-10-30 14:02:45 -0700603 int i;
604 for (i = numBitLevels; i != 0;)
605 {
Alexandru Gagniuc91e9f272014-01-26 22:55:01 -0600606 uint32_t bit;
Stefan Reinauer2e200cd2012-10-30 14:02:45 -0700607 i--;
608 bit = (symbol >> i) & 1;
609 RangeEnc_EncodeBit(rc, probs + m, bit);
610 m = (m << 1) | bit;
611 }
612}
613
Alexandru Gagniuc9ad52fe2014-01-27 20:57:54 -0600614static void RcTree_ReverseEncode(struct CRangeEnc *rc, CLzmaProb *probs, int numBitLevels, uint32_t symbol)
Stefan Reinauer2e200cd2012-10-30 14:02:45 -0700615{
Alexandru Gagniuc91e9f272014-01-26 22:55:01 -0600616 uint32_t m = 1;
Stefan Reinauer2e200cd2012-10-30 14:02:45 -0700617 int i;
618 for (i = 0; i < numBitLevels; i++)
619 {
Alexandru Gagniuc91e9f272014-01-26 22:55:01 -0600620 uint32_t bit = symbol & 1;
Stefan Reinauer2e200cd2012-10-30 14:02:45 -0700621 RangeEnc_EncodeBit(rc, probs + m, bit);
622 m = (m << 1) | bit;
623 symbol >>= 1;
624 }
625}
626
Alexandru Gagniuc91e9f272014-01-26 22:55:01 -0600627static uint32_t RcTree_GetPrice(const CLzmaProb *probs, int numBitLevels, uint32_t symbol, uint32_t *ProbPrices)
Stefan Reinauer2e200cd2012-10-30 14:02:45 -0700628{
Alexandru Gagniuc91e9f272014-01-26 22:55:01 -0600629 uint32_t price = 0;
Stefan Reinauer2e200cd2012-10-30 14:02:45 -0700630 symbol |= (1 << numBitLevels);
631 while (symbol != 1)
632 {
633 price += GET_PRICEa(probs[symbol >> 1], symbol & 1);
634 symbol >>= 1;
635 }
636 return price;
637}
638
Alexandru Gagniuc91e9f272014-01-26 22:55:01 -0600639static uint32_t RcTree_ReverseGetPrice(const CLzmaProb *probs, int numBitLevels, uint32_t symbol, uint32_t *ProbPrices)
Stefan Reinauer2e200cd2012-10-30 14:02:45 -0700640{
Alexandru Gagniuc91e9f272014-01-26 22:55:01 -0600641 uint32_t price = 0;
642 uint32_t m = 1;
Stefan Reinauer2e200cd2012-10-30 14:02:45 -0700643 int i;
644 for (i = numBitLevels; i != 0; i--)
645 {
Alexandru Gagniuc91e9f272014-01-26 22:55:01 -0600646 uint32_t bit = symbol & 1;
Stefan Reinauer2e200cd2012-10-30 14:02:45 -0700647 symbol >>= 1;
648 price += GET_PRICEa(probs[m], bit);
649 m = (m << 1) | bit;
650 }
651 return price;
652}
653
654
Alexandru Gagniuc9ad52fe2014-01-27 20:57:54 -0600655static void LenEnc_Init(struct CLenEnc *p)
Stefan Reinauer2e200cd2012-10-30 14:02:45 -0700656{
657 unsigned i;
658 p->choice = p->choice2 = kProbInitValue;
659 for (i = 0; i < (LZMA_NUM_PB_STATES_MAX << kLenNumLowBits); i++)
660 p->low[i] = kProbInitValue;
661 for (i = 0; i < (LZMA_NUM_PB_STATES_MAX << kLenNumMidBits); i++)
662 p->mid[i] = kProbInitValue;
663 for (i = 0; i < kLenNumHighSymbols; i++)
664 p->high[i] = kProbInitValue;
665}
666
Alexandru Gagniuc9ad52fe2014-01-27 20:57:54 -0600667static void LenEnc_Encode(struct CLenEnc *p, struct CRangeEnc *rc, uint32_t symbol, uint32_t posState)
Stefan Reinauer2e200cd2012-10-30 14:02:45 -0700668{
669 if (symbol < kLenNumLowSymbols)
670 {
671 RangeEnc_EncodeBit(rc, &p->choice, 0);
672 RcTree_Encode(rc, p->low + (posState << kLenNumLowBits), kLenNumLowBits, symbol);
673 }
674 else
675 {
676 RangeEnc_EncodeBit(rc, &p->choice, 1);
677 if (symbol < kLenNumLowSymbols + kLenNumMidSymbols)
678 {
679 RangeEnc_EncodeBit(rc, &p->choice2, 0);
680 RcTree_Encode(rc, p->mid + (posState << kLenNumMidBits), kLenNumMidBits, symbol - kLenNumLowSymbols);
681 }
682 else
683 {
684 RangeEnc_EncodeBit(rc, &p->choice2, 1);
685 RcTree_Encode(rc, p->high, kLenNumHighBits, symbol - kLenNumLowSymbols - kLenNumMidSymbols);
686 }
687 }
688}
689
Alexandru Gagniuc9ad52fe2014-01-27 20:57:54 -0600690static void LenEnc_SetPrices(struct CLenEnc *p, uint32_t posState, uint32_t numSymbols, uint32_t *prices, uint32_t *ProbPrices)
Stefan Reinauer2e200cd2012-10-30 14:02:45 -0700691{
Alexandru Gagniuc91e9f272014-01-26 22:55:01 -0600692 uint32_t a0 = GET_PRICE_0a(p->choice);
693 uint32_t a1 = GET_PRICE_1a(p->choice);
694 uint32_t b0 = a1 + GET_PRICE_0a(p->choice2);
695 uint32_t b1 = a1 + GET_PRICE_1a(p->choice2);
696 uint32_t i = 0;
Stefan Reinauer2e200cd2012-10-30 14:02:45 -0700697 for (i = 0; i < kLenNumLowSymbols; i++)
698 {
699 if (i >= numSymbols)
700 return;
701 prices[i] = a0 + RcTree_GetPrice(p->low + (posState << kLenNumLowBits), kLenNumLowBits, i, ProbPrices);
702 }
703 for (; i < kLenNumLowSymbols + kLenNumMidSymbols; i++)
704 {
705 if (i >= numSymbols)
706 return;
707 prices[i] = b0 + RcTree_GetPrice(p->mid + (posState << kLenNumMidBits), kLenNumMidBits, i - kLenNumLowSymbols, ProbPrices);
708 }
709 for (; i < numSymbols; i++)
710 prices[i] = b1 + RcTree_GetPrice(p->high, kLenNumHighBits, i - kLenNumLowSymbols - kLenNumMidSymbols, ProbPrices);
711}
712
Alexandru Gagniuc9ad52fe2014-01-27 20:57:54 -0600713static void LenPriceEnc_UpdateTable(struct CLenPriceEnc *p, uint32_t posState, uint32_t *ProbPrices)
Stefan Reinauer2e200cd2012-10-30 14:02:45 -0700714{
715 LenEnc_SetPrices(&p->p, posState, p->tableSize, p->prices[posState], ProbPrices);
716 p->counters[posState] = p->tableSize;
717}
718
Alexandru Gagniuc9ad52fe2014-01-27 20:57:54 -0600719static void LenPriceEnc_UpdateTables(struct CLenPriceEnc *p, uint32_t numPosStates, uint32_t *ProbPrices)
Stefan Reinauer2e200cd2012-10-30 14:02:45 -0700720{
Alexandru Gagniuc91e9f272014-01-26 22:55:01 -0600721 uint32_t posState;
Stefan Reinauer2e200cd2012-10-30 14:02:45 -0700722 for (posState = 0; posState < numPosStates; posState++)
723 LenPriceEnc_UpdateTable(p, posState, ProbPrices);
724}
725
Alexandru Gagniuc9ad52fe2014-01-27 20:57:54 -0600726static void LenEnc_Encode2(struct CLenPriceEnc *p, struct CRangeEnc *rc, uint32_t symbol, uint32_t posState, bool updatePrice, uint32_t *ProbPrices)
Stefan Reinauer2e200cd2012-10-30 14:02:45 -0700727{
728 LenEnc_Encode(&p->p, rc, symbol, posState);
729 if (updatePrice)
730 if (--p->counters[posState] == 0)
731 LenPriceEnc_UpdateTable(p, posState, ProbPrices);
732}
733
734
735
736
Alexandru Gagniuc9ad52fe2014-01-27 20:57:54 -0600737static void MovePos(struct CLzmaEnc *p, uint32_t num)
Stefan Reinauer2e200cd2012-10-30 14:02:45 -0700738{
Stefan Reinauer2e200cd2012-10-30 14:02:45 -0700739 if (num != 0)
740 {
741 p->additionalOffset += num;
742 p->matchFinder.Skip(p->matchFinderObj, num);
743 }
744}
745
Alexandru Gagniuc9ad52fe2014-01-27 20:57:54 -0600746static uint32_t ReadMatchDistances(struct CLzmaEnc *p, uint32_t *numDistancePairsRes)
Stefan Reinauer2e200cd2012-10-30 14:02:45 -0700747{
Alexandru Gagniuc91e9f272014-01-26 22:55:01 -0600748 uint32_t lenRes = 0, numPairs;
Stefan Reinauer2e200cd2012-10-30 14:02:45 -0700749 p->numAvail = p->matchFinder.GetNumAvailableBytes(p->matchFinderObj);
750 numPairs = p->matchFinder.GetMatches(p->matchFinderObj, p->matches);
Stefan Reinauer2e200cd2012-10-30 14:02:45 -0700751 if (numPairs > 0)
752 {
753 lenRes = p->matches[numPairs - 2];
Alexandru Gagniuc91e9f272014-01-26 22:55:01 -0600754 if (lenRes == p->numFastuint8_ts)
Stefan Reinauer2e200cd2012-10-30 14:02:45 -0700755 {
Alexandru Gagniuc91e9f272014-01-26 22:55:01 -0600756 const uint8_t *pby = p->matchFinder.GetPointerToCurrentPos(p->matchFinderObj) - 1;
757 uint32_t distance = p->matches[numPairs - 1] + 1;
758 uint32_t numAvail = p->numAvail;
Stefan Reinauer2e200cd2012-10-30 14:02:45 -0700759 if (numAvail > LZMA_MATCH_LEN_MAX)
760 numAvail = LZMA_MATCH_LEN_MAX;
761 {
Alexandru Gagniuc91e9f272014-01-26 22:55:01 -0600762 const uint8_t *pby2 = pby - distance;
Stefan Reinauer2e200cd2012-10-30 14:02:45 -0700763 for (; lenRes < numAvail && pby[lenRes] == pby2[lenRes]; lenRes++);
764 }
765 }
766 }
767 p->additionalOffset++;
768 *numDistancePairsRes = numPairs;
769 return lenRes;
770}
771
772
Alexandru Gagniuc91e9f272014-01-26 22:55:01 -0600773#define MakeAsChar(p) (p)->backPrev = (uint32_t)(-1); (p)->prev1IsChar = false;
774#define MakeAsShortRep(p) (p)->backPrev = 0; (p)->prev1IsChar = false;
Stefan Reinauer2e200cd2012-10-30 14:02:45 -0700775#define IsShortRep(p) ((p)->backPrev == 0)
776
Alexandru Gagniuc9ad52fe2014-01-27 20:57:54 -0600777static uint32_t GetRepLen1Price(struct CLzmaEnc *p, uint32_t state, uint32_t posState)
Stefan Reinauer2e200cd2012-10-30 14:02:45 -0700778{
779 return
780 GET_PRICE_0(p->isRepG0[state]) +
781 GET_PRICE_0(p->isRep0Long[state][posState]);
782}
783
Alexandru Gagniuc9ad52fe2014-01-27 20:57:54 -0600784static uint32_t GetPureRepPrice(struct CLzmaEnc *p, uint32_t repIndex, uint32_t state, uint32_t posState)
Stefan Reinauer2e200cd2012-10-30 14:02:45 -0700785{
Alexandru Gagniuc91e9f272014-01-26 22:55:01 -0600786 uint32_t price;
Stefan Reinauer2e200cd2012-10-30 14:02:45 -0700787 if (repIndex == 0)
788 {
789 price = GET_PRICE_0(p->isRepG0[state]);
790 price += GET_PRICE_1(p->isRep0Long[state][posState]);
791 }
792 else
793 {
794 price = GET_PRICE_1(p->isRepG0[state]);
795 if (repIndex == 1)
796 price += GET_PRICE_0(p->isRepG1[state]);
797 else
798 {
799 price += GET_PRICE_1(p->isRepG1[state]);
800 price += GET_PRICE(p->isRepG2[state], repIndex - 2);
801 }
802 }
803 return price;
804}
805
Alexandru Gagniuc9ad52fe2014-01-27 20:57:54 -0600806static uint32_t GetRepPrice(struct CLzmaEnc *p, uint32_t repIndex, uint32_t len, uint32_t state, uint32_t posState)
Stefan Reinauer2e200cd2012-10-30 14:02:45 -0700807{
808 return p->repLenEnc.prices[posState][len - LZMA_MATCH_LEN_MIN] +
809 GetPureRepPrice(p, repIndex, state, posState);
810}
811
Alexandru Gagniuc9ad52fe2014-01-27 20:57:54 -0600812static uint32_t Backward(struct CLzmaEnc *p, uint32_t *backRes, uint32_t cur)
Stefan Reinauer2e200cd2012-10-30 14:02:45 -0700813{
Alexandru Gagniuc91e9f272014-01-26 22:55:01 -0600814 uint32_t posMem = p->opt[cur].posPrev;
815 uint32_t backMem = p->opt[cur].backPrev;
Stefan Reinauer2e200cd2012-10-30 14:02:45 -0700816 p->optimumEndIndex = cur;
817 do
818 {
819 if (p->opt[cur].prev1IsChar)
820 {
821 MakeAsChar(&p->opt[posMem])
822 p->opt[posMem].posPrev = posMem - 1;
823 if (p->opt[cur].prev2)
824 {
Alexandru Gagniuc91e9f272014-01-26 22:55:01 -0600825 p->opt[posMem - 1].prev1IsChar = false;
Stefan Reinauer2e200cd2012-10-30 14:02:45 -0700826 p->opt[posMem - 1].posPrev = p->opt[cur].posPrev2;
827 p->opt[posMem - 1].backPrev = p->opt[cur].backPrev2;
828 }
829 }
830 {
Alexandru Gagniuc91e9f272014-01-26 22:55:01 -0600831 uint32_t posPrev = posMem;
832 uint32_t backCur = backMem;
Kyösti Mälkkiecd84242013-09-13 07:57:49 +0300833
Stefan Reinauer2e200cd2012-10-30 14:02:45 -0700834 backMem = p->opt[posPrev].backPrev;
835 posMem = p->opt[posPrev].posPrev;
Kyösti Mälkkiecd84242013-09-13 07:57:49 +0300836
Stefan Reinauer2e200cd2012-10-30 14:02:45 -0700837 p->opt[posPrev].backPrev = backCur;
838 p->opt[posPrev].posPrev = cur;
839 cur = posPrev;
840 }
841 }
842 while (cur != 0);
843 *backRes = p->opt[0].backPrev;
844 p->optimumCurrentIndex = p->opt[0].posPrev;
845 return p->optimumCurrentIndex;
846}
847
Alexandru Gagniuc91e9f272014-01-26 22:55:01 -0600848#define LIT_PROBS(pos, prevuint8_t) (p->litProbs + ((((pos) & p->lpMask) << p->lc) + ((prevuint8_t) >> (8 - p->lc))) * 0x300)
Stefan Reinauer2e200cd2012-10-30 14:02:45 -0700849
Alexandru Gagniuc9ad52fe2014-01-27 20:57:54 -0600850static uint32_t GetOptimum(struct CLzmaEnc *p, uint32_t position, uint32_t *backRes)
Stefan Reinauer2e200cd2012-10-30 14:02:45 -0700851{
Alexandru Gagniuc91e9f272014-01-26 22:55:01 -0600852 uint32_t numAvail, mainLen, numPairs, repMaxIndex, i, posState, lenEnd, len, cur;
853 uint32_t matchPrice, repMatchPrice, normalMatchPrice;
854 uint32_t reps[LZMA_NUM_REPS], repLens[LZMA_NUM_REPS];
855 uint32_t *matches;
856 const uint8_t *data;
857 uint8_t curuint8_t, matchuint8_t;
Stefan Reinauer2e200cd2012-10-30 14:02:45 -0700858 if (p->optimumEndIndex != p->optimumCurrentIndex)
859 {
Alexandru Gagniuc9ad52fe2014-01-27 20:57:54 -0600860 const struct COptimal *opt = &p->opt[p->optimumCurrentIndex];
Alexandru Gagniuc91e9f272014-01-26 22:55:01 -0600861 uint32_t lenRes = opt->posPrev - p->optimumCurrentIndex;
Stefan Reinauer2e200cd2012-10-30 14:02:45 -0700862 *backRes = opt->backPrev;
863 p->optimumCurrentIndex = opt->posPrev;
864 return lenRes;
865 }
866 p->optimumCurrentIndex = p->optimumEndIndex = 0;
Kyösti Mälkkiecd84242013-09-13 07:57:49 +0300867
Stefan Reinauer2e200cd2012-10-30 14:02:45 -0700868 if (p->additionalOffset == 0)
869 mainLen = ReadMatchDistances(p, &numPairs);
870 else
871 {
872 mainLen = p->longestMatchLength;
873 numPairs = p->numPairs;
874 }
875
876 numAvail = p->numAvail;
877 if (numAvail < 2)
878 {
Alexandru Gagniuc91e9f272014-01-26 22:55:01 -0600879 *backRes = (uint32_t)(-1);
Stefan Reinauer2e200cd2012-10-30 14:02:45 -0700880 return 1;
881 }
882 if (numAvail > LZMA_MATCH_LEN_MAX)
883 numAvail = LZMA_MATCH_LEN_MAX;
884
885 data = p->matchFinder.GetPointerToCurrentPos(p->matchFinderObj) - 1;
886 repMaxIndex = 0;
887 for (i = 0; i < LZMA_NUM_REPS; i++)
888 {
Alexandru Gagniuc91e9f272014-01-26 22:55:01 -0600889 uint32_t lenTest;
890 const uint8_t *data2;
Stefan Reinauer2e200cd2012-10-30 14:02:45 -0700891 reps[i] = p->reps[i];
892 data2 = data - (reps[i] + 1);
893 if (data[0] != data2[0] || data[1] != data2[1])
894 {
895 repLens[i] = 0;
896 continue;
897 }
898 for (lenTest = 2; lenTest < numAvail && data[lenTest] == data2[lenTest]; lenTest++);
899 repLens[i] = lenTest;
900 if (lenTest > repLens[repMaxIndex])
901 repMaxIndex = i;
902 }
Alexandru Gagniuc91e9f272014-01-26 22:55:01 -0600903 if (repLens[repMaxIndex] >= p->numFastuint8_ts)
Stefan Reinauer2e200cd2012-10-30 14:02:45 -0700904 {
Alexandru Gagniuc91e9f272014-01-26 22:55:01 -0600905 uint32_t lenRes;
Stefan Reinauer2e200cd2012-10-30 14:02:45 -0700906 *backRes = repMaxIndex;
907 lenRes = repLens[repMaxIndex];
908 MovePos(p, lenRes - 1);
909 return lenRes;
910 }
911
912 matches = p->matches;
Alexandru Gagniuc91e9f272014-01-26 22:55:01 -0600913 if (mainLen >= p->numFastuint8_ts)
Stefan Reinauer2e200cd2012-10-30 14:02:45 -0700914 {
915 *backRes = matches[numPairs - 1] + LZMA_NUM_REPS;
916 MovePos(p, mainLen - 1);
917 return mainLen;
918 }
Alexandru Gagniuc91e9f272014-01-26 22:55:01 -0600919 curuint8_t = *data;
920 matchuint8_t = *(data - (reps[0] + 1));
Stefan Reinauer2e200cd2012-10-30 14:02:45 -0700921
Alexandru Gagniuc91e9f272014-01-26 22:55:01 -0600922 if (mainLen < 2 && curuint8_t != matchuint8_t && repLens[repMaxIndex] < 2)
Stefan Reinauer2e200cd2012-10-30 14:02:45 -0700923 {
Alexandru Gagniuc91e9f272014-01-26 22:55:01 -0600924 *backRes = (uint32_t)-1;
Stefan Reinauer2e200cd2012-10-30 14:02:45 -0700925 return 1;
926 }
927
928 p->opt[0].state = (CState)p->state;
929
930 posState = (position & p->pbMask);
931
932 {
933 const CLzmaProb *probs = LIT_PROBS(position, *(data - 1));
934 p->opt[1].price = GET_PRICE_0(p->isMatch[p->state][posState]) +
935 (!IsCharState(p->state) ?
Alexandru Gagniuc91e9f272014-01-26 22:55:01 -0600936 LitEnc_GetPriceMatched(probs, curuint8_t, matchuint8_t, p->ProbPrices) :
937 LitEnc_GetPrice(probs, curuint8_t, p->ProbPrices));
Stefan Reinauer2e200cd2012-10-30 14:02:45 -0700938 }
939
940 MakeAsChar(&p->opt[1]);
941
942 matchPrice = GET_PRICE_1(p->isMatch[p->state][posState]);
943 repMatchPrice = matchPrice + GET_PRICE_1(p->isRep[p->state]);
944
Alexandru Gagniuc91e9f272014-01-26 22:55:01 -0600945 if (matchuint8_t == curuint8_t)
Stefan Reinauer2e200cd2012-10-30 14:02:45 -0700946 {
Alexandru Gagniuc91e9f272014-01-26 22:55:01 -0600947 uint32_t shortRepPrice = repMatchPrice + GetRepLen1Price(p, p->state, posState);
Stefan Reinauer2e200cd2012-10-30 14:02:45 -0700948 if (shortRepPrice < p->opt[1].price)
949 {
950 p->opt[1].price = shortRepPrice;
951 MakeAsShortRep(&p->opt[1]);
952 }
953 }
954 lenEnd = ((mainLen >= repLens[repMaxIndex]) ? mainLen : repLens[repMaxIndex]);
955
956 if (lenEnd < 2)
957 {
958 *backRes = p->opt[1].backPrev;
959 return 1;
960 }
961
962 p->opt[1].posPrev = 0;
963 for (i = 0; i < LZMA_NUM_REPS; i++)
964 p->opt[0].backs[i] = reps[i];
965
966 len = lenEnd;
967 do
968 p->opt[len--].price = kInfinityPrice;
969 while (len >= 2);
970
971 for (i = 0; i < LZMA_NUM_REPS; i++)
972 {
Alexandru Gagniuc91e9f272014-01-26 22:55:01 -0600973 uint32_t repLen = repLens[i];
974 uint32_t price;
Stefan Reinauer2e200cd2012-10-30 14:02:45 -0700975 if (repLen < 2)
976 continue;
977 price = repMatchPrice + GetPureRepPrice(p, i, p->state, posState);
978 do
979 {
Alexandru Gagniuc91e9f272014-01-26 22:55:01 -0600980 uint32_t curAndLenPrice = price + p->repLenEnc.prices[posState][repLen - 2];
Alexandru Gagniuc9ad52fe2014-01-27 20:57:54 -0600981 struct COptimal *opt = &p->opt[repLen];
Stefan Reinauer2e200cd2012-10-30 14:02:45 -0700982 if (curAndLenPrice < opt->price)
983 {
984 opt->price = curAndLenPrice;
985 opt->posPrev = 0;
986 opt->backPrev = i;
Alexandru Gagniuc91e9f272014-01-26 22:55:01 -0600987 opt->prev1IsChar = false;
Stefan Reinauer2e200cd2012-10-30 14:02:45 -0700988 }
989 }
990 while (--repLen >= 2);
991 }
992
993 normalMatchPrice = matchPrice + GET_PRICE_0(p->isRep[p->state]);
994
995 len = ((repLens[0] >= 2) ? repLens[0] + 1 : 2);
996 if (len <= mainLen)
997 {
Alexandru Gagniuc91e9f272014-01-26 22:55:01 -0600998 uint32_t offs = 0;
Stefan Reinauer2e200cd2012-10-30 14:02:45 -0700999 while (len > matches[offs])
1000 offs += 2;
1001 for (; ; len++)
1002 {
Alexandru Gagniuc9ad52fe2014-01-27 20:57:54 -06001003 struct COptimal *opt;
Alexandru Gagniuc91e9f272014-01-26 22:55:01 -06001004 uint32_t distance = matches[offs + 1];
Stefan Reinauer2e200cd2012-10-30 14:02:45 -07001005
Alexandru Gagniuc91e9f272014-01-26 22:55:01 -06001006 uint32_t curAndLenPrice = normalMatchPrice + p->lenEnc.prices[posState][len - LZMA_MATCH_LEN_MIN];
1007 uint32_t lenToPosState = GetLenToPosState(len);
Stefan Reinauer2e200cd2012-10-30 14:02:45 -07001008 if (distance < kNumFullDistances)
1009 curAndLenPrice += p->distancesPrices[lenToPosState][distance];
1010 else
1011 {
Alexandru Gagniuc91e9f272014-01-26 22:55:01 -06001012 uint32_t slot;
Stefan Reinauer2e200cd2012-10-30 14:02:45 -07001013 GetPosSlot2(distance, slot);
1014 curAndLenPrice += p->alignPrices[distance & kAlignMask] + p->posSlotPrices[lenToPosState][slot];
1015 }
1016 opt = &p->opt[len];
1017 if (curAndLenPrice < opt->price)
1018 {
1019 opt->price = curAndLenPrice;
1020 opt->posPrev = 0;
1021 opt->backPrev = distance + LZMA_NUM_REPS;
Alexandru Gagniuc91e9f272014-01-26 22:55:01 -06001022 opt->prev1IsChar = false;
Stefan Reinauer2e200cd2012-10-30 14:02:45 -07001023 }
1024 if (len == matches[offs])
1025 {
1026 offs += 2;
1027 if (offs == numPairs)
1028 break;
1029 }
1030 }
1031 }
1032
1033 cur = 0;
1034
Stefan Reinauer2e200cd2012-10-30 14:02:45 -07001035 for (;;)
1036 {
Alexandru Gagniucb63b75b2014-01-29 14:56:20 -06001037 uint32_t numAvailFull, newLen, posPrev, state, startLen;
1038 uint32_t curPrice, curAnd1Price;
Alexandru Gagniuc91e9f272014-01-26 22:55:01 -06001039 bool nextIsChar;
Alexandru Gagniuc9ad52fe2014-01-27 20:57:54 -06001040 struct COptimal *curOpt;
1041 struct COptimal *nextOpt;
Stefan Reinauer2e200cd2012-10-30 14:02:45 -07001042
1043 cur++;
1044 if (cur == lenEnd)
1045 return Backward(p, backRes, cur);
1046
1047 newLen = ReadMatchDistances(p, &numPairs);
Alexandru Gagniuc91e9f272014-01-26 22:55:01 -06001048 if (newLen >= p->numFastuint8_ts)
Stefan Reinauer2e200cd2012-10-30 14:02:45 -07001049 {
1050 p->numPairs = numPairs;
1051 p->longestMatchLength = newLen;
1052 return Backward(p, backRes, cur);
1053 }
1054 position++;
1055 curOpt = &p->opt[cur];
1056 posPrev = curOpt->posPrev;
1057 if (curOpt->prev1IsChar)
1058 {
1059 posPrev--;
1060 if (curOpt->prev2)
1061 {
1062 state = p->opt[curOpt->posPrev2].state;
1063 if (curOpt->backPrev2 < LZMA_NUM_REPS)
1064 state = kRepNextStates[state];
1065 else
1066 state = kMatchNextStates[state];
1067 }
1068 else
1069 state = p->opt[posPrev].state;
1070 state = kLiteralNextStates[state];
1071 }
1072 else
1073 state = p->opt[posPrev].state;
1074 if (posPrev == cur - 1)
1075 {
1076 if (IsShortRep(curOpt))
1077 state = kShortRepNextStates[state];
1078 else
1079 state = kLiteralNextStates[state];
1080 }
1081 else
1082 {
Alexandru Gagniuc91e9f272014-01-26 22:55:01 -06001083 uint32_t pos;
Alexandru Gagniuc9ad52fe2014-01-27 20:57:54 -06001084 const struct COptimal *prevOpt;
Stefan Reinauer2e200cd2012-10-30 14:02:45 -07001085 if (curOpt->prev1IsChar && curOpt->prev2)
1086 {
1087 posPrev = curOpt->posPrev2;
1088 pos = curOpt->backPrev2;
1089 state = kRepNextStates[state];
1090 }
1091 else
1092 {
1093 pos = curOpt->backPrev;
1094 if (pos < LZMA_NUM_REPS)
1095 state = kRepNextStates[state];
1096 else
1097 state = kMatchNextStates[state];
1098 }
1099 prevOpt = &p->opt[posPrev];
1100 if (pos < LZMA_NUM_REPS)
1101 {
Stefan Reinauer2e200cd2012-10-30 14:02:45 -07001102 reps[0] = prevOpt->backs[pos];
1103 for (i = 1; i <= pos; i++)
1104 reps[i] = prevOpt->backs[i - 1];
1105 for (; i < LZMA_NUM_REPS; i++)
1106 reps[i] = prevOpt->backs[i];
1107 }
1108 else
1109 {
Stefan Reinauer2e200cd2012-10-30 14:02:45 -07001110 reps[0] = (pos - LZMA_NUM_REPS);
1111 for (i = 1; i < LZMA_NUM_REPS; i++)
1112 reps[i] = prevOpt->backs[i - 1];
1113 }
1114 }
1115 curOpt->state = (CState)state;
1116
1117 curOpt->backs[0] = reps[0];
1118 curOpt->backs[1] = reps[1];
1119 curOpt->backs[2] = reps[2];
1120 curOpt->backs[3] = reps[3];
1121
1122 curPrice = curOpt->price;
Alexandru Gagniuc91e9f272014-01-26 22:55:01 -06001123 nextIsChar = false;
Stefan Reinauer2e200cd2012-10-30 14:02:45 -07001124 data = p->matchFinder.GetPointerToCurrentPos(p->matchFinderObj) - 1;
Alexandru Gagniuc91e9f272014-01-26 22:55:01 -06001125 curuint8_t = *data;
1126 matchuint8_t = *(data - (reps[0] + 1));
Stefan Reinauer2e200cd2012-10-30 14:02:45 -07001127
1128 posState = (position & p->pbMask);
1129
1130 curAnd1Price = curPrice + GET_PRICE_0(p->isMatch[state][posState]);
1131 {
1132 const CLzmaProb *probs = LIT_PROBS(position, *(data - 1));
1133 curAnd1Price +=
1134 (!IsCharState(state) ?
Alexandru Gagniuc91e9f272014-01-26 22:55:01 -06001135 LitEnc_GetPriceMatched(probs, curuint8_t, matchuint8_t, p->ProbPrices) :
1136 LitEnc_GetPrice(probs, curuint8_t, p->ProbPrices));
Stefan Reinauer2e200cd2012-10-30 14:02:45 -07001137 }
1138
1139 nextOpt = &p->opt[cur + 1];
1140
1141 if (curAnd1Price < nextOpt->price)
1142 {
1143 nextOpt->price = curAnd1Price;
1144 nextOpt->posPrev = cur;
1145 MakeAsChar(nextOpt);
Alexandru Gagniuc91e9f272014-01-26 22:55:01 -06001146 nextIsChar = true;
Stefan Reinauer2e200cd2012-10-30 14:02:45 -07001147 }
1148
1149 matchPrice = curPrice + GET_PRICE_1(p->isMatch[state][posState]);
1150 repMatchPrice = matchPrice + GET_PRICE_1(p->isRep[state]);
Kyösti Mälkkiecd84242013-09-13 07:57:49 +03001151
Alexandru Gagniuc91e9f272014-01-26 22:55:01 -06001152 if (matchuint8_t == curuint8_t && !(nextOpt->posPrev < cur && nextOpt->backPrev == 0))
Stefan Reinauer2e200cd2012-10-30 14:02:45 -07001153 {
Alexandru Gagniuc91e9f272014-01-26 22:55:01 -06001154 uint32_t shortRepPrice = repMatchPrice + GetRepLen1Price(p, state, posState);
Stefan Reinauer2e200cd2012-10-30 14:02:45 -07001155 if (shortRepPrice <= nextOpt->price)
1156 {
1157 nextOpt->price = shortRepPrice;
1158 nextOpt->posPrev = cur;
1159 MakeAsShortRep(nextOpt);
Alexandru Gagniuc91e9f272014-01-26 22:55:01 -06001160 nextIsChar = true;
Stefan Reinauer2e200cd2012-10-30 14:02:45 -07001161 }
1162 }
1163 numAvailFull = p->numAvail;
1164 {
Alexandru Gagniuc91e9f272014-01-26 22:55:01 -06001165 uint32_t temp = kNumOpts - 1 - cur;
Stefan Reinauer2e200cd2012-10-30 14:02:45 -07001166 if (temp < numAvailFull)
1167 numAvailFull = temp;
1168 }
1169
1170 if (numAvailFull < 2)
1171 continue;
Alexandru Gagniuc91e9f272014-01-26 22:55:01 -06001172 numAvail = (numAvailFull <= p->numFastuint8_ts ? numAvailFull : p->numFastuint8_ts);
Stefan Reinauer2e200cd2012-10-30 14:02:45 -07001173
Alexandru Gagniuc91e9f272014-01-26 22:55:01 -06001174 if (!nextIsChar && matchuint8_t != curuint8_t) /* speed optimization */
Stefan Reinauer2e200cd2012-10-30 14:02:45 -07001175 {
1176 /* try Literal + rep0 */
Alexandru Gagniuc91e9f272014-01-26 22:55:01 -06001177 uint32_t temp;
1178 uint32_t lenTest2;
1179 const uint8_t *data2 = data - (reps[0] + 1);
1180 uint32_t limit = p->numFastuint8_ts + 1;
Stefan Reinauer2e200cd2012-10-30 14:02:45 -07001181 if (limit > numAvailFull)
1182 limit = numAvailFull;
1183
1184 for (temp = 1; temp < limit && data[temp] == data2[temp]; temp++);
1185 lenTest2 = temp - 1;
1186 if (lenTest2 >= 2)
1187 {
Alexandru Gagniuc91e9f272014-01-26 22:55:01 -06001188 uint32_t state2 = kLiteralNextStates[state];
1189 uint32_t posStateNext = (position + 1) & p->pbMask;
1190 uint32_t nextRepMatchPrice = curAnd1Price +
Stefan Reinauer2e200cd2012-10-30 14:02:45 -07001191 GET_PRICE_1(p->isMatch[state2][posStateNext]) +
1192 GET_PRICE_1(p->isRep[state2]);
1193 /* for (; lenTest2 >= 2; lenTest2--) */
1194 {
Alexandru Gagniuc91e9f272014-01-26 22:55:01 -06001195 uint32_t curAndLenPrice;
Alexandru Gagniuc9ad52fe2014-01-27 20:57:54 -06001196 struct COptimal *opt;
Alexandru Gagniuc91e9f272014-01-26 22:55:01 -06001197 uint32_t offset = cur + 1 + lenTest2;
Stefan Reinauer2e200cd2012-10-30 14:02:45 -07001198 while (lenEnd < offset)
1199 p->opt[++lenEnd].price = kInfinityPrice;
1200 curAndLenPrice = nextRepMatchPrice + GetRepPrice(p, 0, lenTest2, state2, posStateNext);
1201 opt = &p->opt[offset];
1202 if (curAndLenPrice < opt->price)
1203 {
1204 opt->price = curAndLenPrice;
1205 opt->posPrev = cur + 1;
1206 opt->backPrev = 0;
Alexandru Gagniuc91e9f272014-01-26 22:55:01 -06001207 opt->prev1IsChar = true;
1208 opt->prev2 = false;
Stefan Reinauer2e200cd2012-10-30 14:02:45 -07001209 }
1210 }
1211 }
1212 }
Kyösti Mälkkiecd84242013-09-13 07:57:49 +03001213
Stefan Reinauer2e200cd2012-10-30 14:02:45 -07001214 startLen = 2; /* speed optimization */
1215 {
Alexandru Gagniuc91e9f272014-01-26 22:55:01 -06001216 uint32_t repIndex;
Stefan Reinauer2e200cd2012-10-30 14:02:45 -07001217 for (repIndex = 0; repIndex < LZMA_NUM_REPS; repIndex++)
1218 {
Alexandru Gagniuc91e9f272014-01-26 22:55:01 -06001219 uint32_t lenTest;
1220 uint32_t lenTestTemp;
1221 uint32_t price;
1222 const uint8_t *data2 = data - (reps[repIndex] + 1);
Stefan Reinauer2e200cd2012-10-30 14:02:45 -07001223 if (data[0] != data2[0] || data[1] != data2[1])
1224 continue;
1225 for (lenTest = 2; lenTest < numAvail && data[lenTest] == data2[lenTest]; lenTest++);
1226 while (lenEnd < cur + lenTest)
1227 p->opt[++lenEnd].price = kInfinityPrice;
1228 lenTestTemp = lenTest;
1229 price = repMatchPrice + GetPureRepPrice(p, repIndex, state, posState);
1230 do
1231 {
Alexandru Gagniuc91e9f272014-01-26 22:55:01 -06001232 uint32_t curAndLenPrice = price + p->repLenEnc.prices[posState][lenTest - 2];
Alexandru Gagniuc9ad52fe2014-01-27 20:57:54 -06001233 struct COptimal *opt = &p->opt[cur + lenTest];
Stefan Reinauer2e200cd2012-10-30 14:02:45 -07001234 if (curAndLenPrice < opt->price)
1235 {
1236 opt->price = curAndLenPrice;
1237 opt->posPrev = cur;
1238 opt->backPrev = repIndex;
Alexandru Gagniuc91e9f272014-01-26 22:55:01 -06001239 opt->prev1IsChar = false;
Stefan Reinauer2e200cd2012-10-30 14:02:45 -07001240 }
1241 }
1242 while (--lenTest >= 2);
1243 lenTest = lenTestTemp;
Kyösti Mälkkiecd84242013-09-13 07:57:49 +03001244
Stefan Reinauer2e200cd2012-10-30 14:02:45 -07001245 if (repIndex == 0)
1246 startLen = lenTest + 1;
Kyösti Mälkkiecd84242013-09-13 07:57:49 +03001247
Stefan Reinauer2e200cd2012-10-30 14:02:45 -07001248 /* if (_maxMode) */
1249 {
Alexandru Gagniuc91e9f272014-01-26 22:55:01 -06001250 uint32_t lenTest2 = lenTest + 1;
1251 uint32_t limit = lenTest2 + p->numFastuint8_ts;
1252 uint32_t nextRepMatchPrice;
Stefan Reinauer2e200cd2012-10-30 14:02:45 -07001253 if (limit > numAvailFull)
1254 limit = numAvailFull;
1255 for (; lenTest2 < limit && data[lenTest2] == data2[lenTest2]; lenTest2++);
1256 lenTest2 -= lenTest + 1;
1257 if (lenTest2 >= 2)
1258 {
Alexandru Gagniuc91e9f272014-01-26 22:55:01 -06001259 uint32_t state2 = kRepNextStates[state];
1260 uint32_t posStateNext = (position + lenTest) & p->pbMask;
1261 uint32_t curAndLenCharPrice =
Stefan Reinauer2e200cd2012-10-30 14:02:45 -07001262 price + p->repLenEnc.prices[posState][lenTest - 2] +
1263 GET_PRICE_0(p->isMatch[state2][posStateNext]) +
1264 LitEnc_GetPriceMatched(LIT_PROBS(position + lenTest, data[lenTest - 1]),
1265 data[lenTest], data2[lenTest], p->ProbPrices);
1266 state2 = kLiteralNextStates[state2];
1267 posStateNext = (position + lenTest + 1) & p->pbMask;
1268 nextRepMatchPrice = curAndLenCharPrice +
1269 GET_PRICE_1(p->isMatch[state2][posStateNext]) +
1270 GET_PRICE_1(p->isRep[state2]);
Kyösti Mälkkiecd84242013-09-13 07:57:49 +03001271
Stefan Reinauer2e200cd2012-10-30 14:02:45 -07001272 /* for (; lenTest2 >= 2; lenTest2--) */
1273 {
Alexandru Gagniuc91e9f272014-01-26 22:55:01 -06001274 uint32_t curAndLenPrice;
Alexandru Gagniuc9ad52fe2014-01-27 20:57:54 -06001275 struct COptimal *opt;
Alexandru Gagniuc91e9f272014-01-26 22:55:01 -06001276 uint32_t offset = cur + lenTest + 1 + lenTest2;
Stefan Reinauer2e200cd2012-10-30 14:02:45 -07001277 while (lenEnd < offset)
1278 p->opt[++lenEnd].price = kInfinityPrice;
1279 curAndLenPrice = nextRepMatchPrice + GetRepPrice(p, 0, lenTest2, state2, posStateNext);
1280 opt = &p->opt[offset];
1281 if (curAndLenPrice < opt->price)
1282 {
1283 opt->price = curAndLenPrice;
1284 opt->posPrev = cur + lenTest + 1;
1285 opt->backPrev = 0;
Alexandru Gagniuc91e9f272014-01-26 22:55:01 -06001286 opt->prev1IsChar = true;
1287 opt->prev2 = true;
Stefan Reinauer2e200cd2012-10-30 14:02:45 -07001288 opt->posPrev2 = cur;
1289 opt->backPrev2 = repIndex;
1290 }
1291 }
1292 }
1293 }
1294 }
1295 }
Alexandru Gagniuc91e9f272014-01-26 22:55:01 -06001296 /* for (uint32_t lenTest = 2; lenTest <= newLen; lenTest++) */
Stefan Reinauer2e200cd2012-10-30 14:02:45 -07001297 if (newLen > numAvail)
1298 {
1299 newLen = numAvail;
1300 for (numPairs = 0; newLen > matches[numPairs]; numPairs += 2);
1301 matches[numPairs] = newLen;
1302 numPairs += 2;
1303 }
1304 if (newLen >= startLen)
1305 {
Alexandru Gagniuc91e9f272014-01-26 22:55:01 -06001306 uint32_t offs, curBack, posSlot;
1307 uint32_t lenTest;
Alexandru Gagniucb63b75b2014-01-29 14:56:20 -06001308
1309 normalMatchPrice = matchPrice + GET_PRICE_0(p->isRep[state]);
1310
Stefan Reinauer2e200cd2012-10-30 14:02:45 -07001311 while (lenEnd < cur + newLen)
1312 p->opt[++lenEnd].price = kInfinityPrice;
1313
1314 offs = 0;
1315 while (startLen > matches[offs])
1316 offs += 2;
1317 curBack = matches[offs + 1];
1318 GetPosSlot2(curBack, posSlot);
1319 for (lenTest = /*2*/ startLen; ; lenTest++)
1320 {
Alexandru Gagniuc91e9f272014-01-26 22:55:01 -06001321 uint32_t curAndLenPrice = normalMatchPrice + p->lenEnc.prices[posState][lenTest - LZMA_MATCH_LEN_MIN];
1322 uint32_t lenToPosState = GetLenToPosState(lenTest);
Alexandru Gagniuc9ad52fe2014-01-27 20:57:54 -06001323 struct COptimal *opt;
Stefan Reinauer2e200cd2012-10-30 14:02:45 -07001324 if (curBack < kNumFullDistances)
1325 curAndLenPrice += p->distancesPrices[lenToPosState][curBack];
1326 else
1327 curAndLenPrice += p->posSlotPrices[lenToPosState][posSlot] + p->alignPrices[curBack & kAlignMask];
Kyösti Mälkkiecd84242013-09-13 07:57:49 +03001328
Stefan Reinauer2e200cd2012-10-30 14:02:45 -07001329 opt = &p->opt[cur + lenTest];
1330 if (curAndLenPrice < opt->price)
1331 {
1332 opt->price = curAndLenPrice;
1333 opt->posPrev = cur;
1334 opt->backPrev = curBack + LZMA_NUM_REPS;
Alexandru Gagniuc91e9f272014-01-26 22:55:01 -06001335 opt->prev1IsChar = false;
Stefan Reinauer2e200cd2012-10-30 14:02:45 -07001336 }
1337
1338 if (/*_maxMode && */lenTest == matches[offs])
1339 {
1340 /* Try Match + Literal + Rep0 */
Alexandru Gagniuc91e9f272014-01-26 22:55:01 -06001341 const uint8_t *data2 = data - (curBack + 1);
1342 uint32_t lenTest2 = lenTest + 1;
1343 uint32_t limit = lenTest2 + p->numFastuint8_ts;
1344 uint32_t nextRepMatchPrice;
Stefan Reinauer2e200cd2012-10-30 14:02:45 -07001345 if (limit > numAvailFull)
1346 limit = numAvailFull;
1347 for (; lenTest2 < limit && data[lenTest2] == data2[lenTest2]; lenTest2++);
1348 lenTest2 -= lenTest + 1;
1349 if (lenTest2 >= 2)
1350 {
Alexandru Gagniuc91e9f272014-01-26 22:55:01 -06001351 uint32_t state2 = kMatchNextStates[state];
1352 uint32_t posStateNext = (position + lenTest) & p->pbMask;
1353 uint32_t curAndLenCharPrice = curAndLenPrice +
Stefan Reinauer2e200cd2012-10-30 14:02:45 -07001354 GET_PRICE_0(p->isMatch[state2][posStateNext]) +
1355 LitEnc_GetPriceMatched(LIT_PROBS(position + lenTest, data[lenTest - 1]),
1356 data[lenTest], data2[lenTest], p->ProbPrices);
1357 state2 = kLiteralNextStates[state2];
1358 posStateNext = (posStateNext + 1) & p->pbMask;
1359 nextRepMatchPrice = curAndLenCharPrice +
1360 GET_PRICE_1(p->isMatch[state2][posStateNext]) +
1361 GET_PRICE_1(p->isRep[state2]);
Kyösti Mälkkiecd84242013-09-13 07:57:49 +03001362
Stefan Reinauer2e200cd2012-10-30 14:02:45 -07001363 /* for (; lenTest2 >= 2; lenTest2--) */
1364 {
Alexandru Gagniuc91e9f272014-01-26 22:55:01 -06001365 uint32_t offset = cur + lenTest + 1 + lenTest2;
Stefan Reinauer2e200cd2012-10-30 14:02:45 -07001366 while (lenEnd < offset)
1367 p->opt[++lenEnd].price = kInfinityPrice;
1368 curAndLenPrice = nextRepMatchPrice + GetRepPrice(p, 0, lenTest2, state2, posStateNext);
1369 opt = &p->opt[offset];
1370 if (curAndLenPrice < opt->price)
1371 {
1372 opt->price = curAndLenPrice;
1373 opt->posPrev = cur + lenTest + 1;
1374 opt->backPrev = 0;
Alexandru Gagniuc91e9f272014-01-26 22:55:01 -06001375 opt->prev1IsChar = true;
1376 opt->prev2 = true;
Stefan Reinauer2e200cd2012-10-30 14:02:45 -07001377 opt->posPrev2 = cur;
1378 opt->backPrev2 = curBack + LZMA_NUM_REPS;
1379 }
1380 }
1381 }
1382 offs += 2;
1383 if (offs == numPairs)
1384 break;
1385 curBack = matches[offs + 1];
1386 if (curBack >= kNumFullDistances)
1387 GetPosSlot2(curBack, posSlot);
1388 }
1389 }
1390 }
1391 }
1392}
1393
1394#define ChangePair(smallDist, bigDist) (((bigDist) >> 7) > (smallDist))
1395
Alexandru Gagniuc9ad52fe2014-01-27 20:57:54 -06001396static uint32_t GetOptimumFast(struct CLzmaEnc *p, uint32_t *backRes)
Stefan Reinauer2e200cd2012-10-30 14:02:45 -07001397{
Alexandru Gagniuc91e9f272014-01-26 22:55:01 -06001398 uint32_t numAvail, mainLen, mainDist, numPairs, repIndex, repLen, i;
1399 const uint8_t *data;
1400 const uint32_t *matches;
Stefan Reinauer2e200cd2012-10-30 14:02:45 -07001401
1402 if (p->additionalOffset == 0)
1403 mainLen = ReadMatchDistances(p, &numPairs);
1404 else
1405 {
1406 mainLen = p->longestMatchLength;
1407 numPairs = p->numPairs;
1408 }
1409
1410 numAvail = p->numAvail;
Alexandru Gagniuc91e9f272014-01-26 22:55:01 -06001411 *backRes = (uint32_t)-1;
Stefan Reinauer2e200cd2012-10-30 14:02:45 -07001412 if (numAvail < 2)
1413 return 1;
1414 if (numAvail > LZMA_MATCH_LEN_MAX)
1415 numAvail = LZMA_MATCH_LEN_MAX;
1416 data = p->matchFinder.GetPointerToCurrentPos(p->matchFinderObj) - 1;
1417
1418 repLen = repIndex = 0;
1419 for (i = 0; i < LZMA_NUM_REPS; i++)
1420 {
Alexandru Gagniuc91e9f272014-01-26 22:55:01 -06001421 uint32_t len;
1422 const uint8_t *data2 = data - (p->reps[i] + 1);
Stefan Reinauer2e200cd2012-10-30 14:02:45 -07001423 if (data[0] != data2[0] || data[1] != data2[1])
1424 continue;
1425 for (len = 2; len < numAvail && data[len] == data2[len]; len++);
Alexandru Gagniuc91e9f272014-01-26 22:55:01 -06001426 if (len >= p->numFastuint8_ts)
Stefan Reinauer2e200cd2012-10-30 14:02:45 -07001427 {
1428 *backRes = i;
1429 MovePos(p, len - 1);
1430 return len;
1431 }
1432 if (len > repLen)
1433 {
1434 repIndex = i;
1435 repLen = len;
1436 }
1437 }
1438
1439 matches = p->matches;
Alexandru Gagniuc91e9f272014-01-26 22:55:01 -06001440 if (mainLen >= p->numFastuint8_ts)
Stefan Reinauer2e200cd2012-10-30 14:02:45 -07001441 {
1442 *backRes = matches[numPairs - 1] + LZMA_NUM_REPS;
1443 MovePos(p, mainLen - 1);
1444 return mainLen;
1445 }
1446
1447 mainDist = 0; /* for GCC */
1448 if (mainLen >= 2)
1449 {
1450 mainDist = matches[numPairs - 1];
1451 while (numPairs > 2 && mainLen == matches[numPairs - 4] + 1)
1452 {
1453 if (!ChangePair(matches[numPairs - 3], mainDist))
1454 break;
1455 numPairs -= 2;
1456 mainLen = matches[numPairs - 2];
1457 mainDist = matches[numPairs - 1];
1458 }
1459 if (mainLen == 2 && mainDist >= 0x80)
1460 mainLen = 1;
1461 }
1462
1463 if (repLen >= 2 && (
1464 (repLen + 1 >= mainLen) ||
1465 (repLen + 2 >= mainLen && mainDist >= (1 << 9)) ||
1466 (repLen + 3 >= mainLen && mainDist >= (1 << 15))))
1467 {
1468 *backRes = repIndex;
1469 MovePos(p, repLen - 1);
1470 return repLen;
1471 }
Kyösti Mälkkiecd84242013-09-13 07:57:49 +03001472
Stefan Reinauer2e200cd2012-10-30 14:02:45 -07001473 if (mainLen < 2 || numAvail <= 2)
1474 return 1;
1475
1476 p->longestMatchLength = ReadMatchDistances(p, &p->numPairs);
1477 if (p->longestMatchLength >= 2)
1478 {
Alexandru Gagniuc91e9f272014-01-26 22:55:01 -06001479 uint32_t newDistance = matches[p->numPairs - 1];
Stefan Reinauer2e200cd2012-10-30 14:02:45 -07001480 if ((p->longestMatchLength >= mainLen && newDistance < mainDist) ||
1481 (p->longestMatchLength == mainLen + 1 && !ChangePair(mainDist, newDistance)) ||
1482 (p->longestMatchLength > mainLen + 1) ||
1483 (p->longestMatchLength + 1 >= mainLen && mainLen >= 3 && ChangePair(newDistance, mainDist)))
1484 return 1;
1485 }
Kyösti Mälkkiecd84242013-09-13 07:57:49 +03001486
Stefan Reinauer2e200cd2012-10-30 14:02:45 -07001487 data = p->matchFinder.GetPointerToCurrentPos(p->matchFinderObj) - 1;
1488 for (i = 0; i < LZMA_NUM_REPS; i++)
1489 {
Alexandru Gagniuc91e9f272014-01-26 22:55:01 -06001490 uint32_t len, limit;
1491 const uint8_t *data2 = data - (p->reps[i] + 1);
Stefan Reinauer2e200cd2012-10-30 14:02:45 -07001492 if (data[0] != data2[0] || data[1] != data2[1])
1493 continue;
1494 limit = mainLen - 1;
1495 for (len = 2; len < limit && data[len] == data2[len]; len++);
1496 if (len >= limit)
1497 return 1;
1498 }
1499 *backRes = mainDist + LZMA_NUM_REPS;
1500 MovePos(p, mainLen - 2);
1501 return mainLen;
1502}
1503
Alexandru Gagniuc9ad52fe2014-01-27 20:57:54 -06001504static void WriteEndMarker(struct CLzmaEnc *p, uint32_t posState)
Stefan Reinauer2e200cd2012-10-30 14:02:45 -07001505{
Alexandru Gagniuc91e9f272014-01-26 22:55:01 -06001506 uint32_t len;
Stefan Reinauer2e200cd2012-10-30 14:02:45 -07001507 RangeEnc_EncodeBit(&p->rc, &p->isMatch[p->state][posState], 1);
1508 RangeEnc_EncodeBit(&p->rc, &p->isRep[p->state], 0);
1509 p->state = kMatchNextStates[p->state];
1510 len = LZMA_MATCH_LEN_MIN;
1511 LenEnc_Encode2(&p->lenEnc, &p->rc, len - LZMA_MATCH_LEN_MIN, posState, !p->fastMode, p->ProbPrices);
1512 RcTree_Encode(&p->rc, p->posSlotEncoder[GetLenToPosState(len)], kNumPosSlotBits, (1 << kNumPosSlotBits) - 1);
Alexandru Gagniuc91e9f272014-01-26 22:55:01 -06001513 RangeEnc_EncodeDirectBits(&p->rc, (((uint32_t)1 << 30) - 1) >> kNumAlignBits, 30 - kNumAlignBits);
Stefan Reinauer2e200cd2012-10-30 14:02:45 -07001514 RcTree_ReverseEncode(&p->rc, p->posAlignEncoder, kNumAlignBits, kAlignMask);
1515}
1516
Alexandru Gagniuc9ad52fe2014-01-27 20:57:54 -06001517static SRes CheckErrors(struct CLzmaEnc *p)
Stefan Reinauer2e200cd2012-10-30 14:02:45 -07001518{
1519 if (p->result != SZ_OK)
1520 return p->result;
1521 if (p->rc.res != SZ_OK)
1522 p->result = SZ_ERROR_WRITE;
1523 if (p->matchFinderBase.result != SZ_OK)
1524 p->result = SZ_ERROR_READ;
1525 if (p->result != SZ_OK)
Alexandru Gagniuc91e9f272014-01-26 22:55:01 -06001526 p->finished = true;
Stefan Reinauer2e200cd2012-10-30 14:02:45 -07001527 return p->result;
1528}
1529
Alexandru Gagniuc9ad52fe2014-01-27 20:57:54 -06001530static SRes Flush(struct CLzmaEnc *p, uint32_t nowPos)
Stefan Reinauer2e200cd2012-10-30 14:02:45 -07001531{
1532 /* ReleaseMFStream(); */
Alexandru Gagniuc91e9f272014-01-26 22:55:01 -06001533 p->finished = true;
Stefan Reinauer2e200cd2012-10-30 14:02:45 -07001534 if (p->writeEndMark)
1535 WriteEndMarker(p, nowPos & p->pbMask);
1536 RangeEnc_FlushData(&p->rc);
1537 RangeEnc_FlushStream(&p->rc);
1538 return CheckErrors(p);
1539}
1540
Alexandru Gagniuc9ad52fe2014-01-27 20:57:54 -06001541static void FillAlignPrices(struct CLzmaEnc *p)
Stefan Reinauer2e200cd2012-10-30 14:02:45 -07001542{
Alexandru Gagniuc91e9f272014-01-26 22:55:01 -06001543 uint32_t i;
Stefan Reinauer2e200cd2012-10-30 14:02:45 -07001544 for (i = 0; i < kAlignTableSize; i++)
1545 p->alignPrices[i] = RcTree_ReverseGetPrice(p->posAlignEncoder, kNumAlignBits, i, p->ProbPrices);
1546 p->alignPriceCount = 0;
1547}
1548
Alexandru Gagniuc9ad52fe2014-01-27 20:57:54 -06001549static void FillDistancesPrices(struct CLzmaEnc *p)
Stefan Reinauer2e200cd2012-10-30 14:02:45 -07001550{
Alexandru Gagniuc91e9f272014-01-26 22:55:01 -06001551 uint32_t tempPrices[kNumFullDistances];
1552 uint32_t i, lenToPosState;
Stefan Reinauer2e200cd2012-10-30 14:02:45 -07001553 for (i = kStartPosModelIndex; i < kNumFullDistances; i++)
1554 {
Alexandru Gagniuc91e9f272014-01-26 22:55:01 -06001555 uint32_t posSlot = GetPosSlot1(i);
1556 uint32_t footerBits = ((posSlot >> 1) - 1);
1557 uint32_t base = ((2 | (posSlot & 1)) << footerBits);
Stefan Reinauer2e200cd2012-10-30 14:02:45 -07001558 tempPrices[i] = RcTree_ReverseGetPrice(p->posEncoders + base - posSlot - 1, footerBits, i - base, p->ProbPrices);
1559 }
1560
1561 for (lenToPosState = 0; lenToPosState < kNumLenToPosStates; lenToPosState++)
1562 {
Alexandru Gagniuc91e9f272014-01-26 22:55:01 -06001563 uint32_t posSlot;
Stefan Reinauer2e200cd2012-10-30 14:02:45 -07001564 const CLzmaProb *encoder = p->posSlotEncoder[lenToPosState];
Alexandru Gagniuc91e9f272014-01-26 22:55:01 -06001565 uint32_t *posSlotPrices = p->posSlotPrices[lenToPosState];
Stefan Reinauer2e200cd2012-10-30 14:02:45 -07001566 for (posSlot = 0; posSlot < p->distTableSize; posSlot++)
1567 posSlotPrices[posSlot] = RcTree_GetPrice(encoder, kNumPosSlotBits, posSlot, p->ProbPrices);
1568 for (posSlot = kEndPosModelIndex; posSlot < p->distTableSize; posSlot++)
1569 posSlotPrices[posSlot] += ((((posSlot >> 1) - 1) - kNumAlignBits) << kNumBitPriceShiftBits);
1570
1571 {
Alexandru Gagniuc91e9f272014-01-26 22:55:01 -06001572 uint32_t *distancesPrices = p->distancesPrices[lenToPosState];
Stefan Reinauer2e200cd2012-10-30 14:02:45 -07001573 for (i = 0; i < kStartPosModelIndex; i++)
1574 distancesPrices[i] = posSlotPrices[i];
1575 for (; i < kNumFullDistances; i++)
1576 distancesPrices[i] = posSlotPrices[GetPosSlot1(i)] + tempPrices[i];
1577 }
1578 }
1579 p->matchPriceCount = 0;
1580}
1581
Alexandru Gagniuc9ad52fe2014-01-27 20:57:54 -06001582static void LzmaEnc_Construct(struct CLzmaEnc *p)
Stefan Reinauer2e200cd2012-10-30 14:02:45 -07001583{
1584 RangeEnc_Construct(&p->rc);
1585 MatchFinder_Construct(&p->matchFinderBase);
Stefan Reinauer2e200cd2012-10-30 14:02:45 -07001586
1587 {
Alexandru Gagniuc9ad52fe2014-01-27 20:57:54 -06001588 struct CLzmaEncProps props;
Stefan Reinauer2e200cd2012-10-30 14:02:45 -07001589 LzmaEncProps_Init(&props);
1590 LzmaEnc_SetProps(p, &props);
1591 }
1592
1593 #ifndef LZMA_LOG_BSR
1594 LzmaEnc_FastPosInit(p->g_FastPos);
1595 #endif
1596
1597 LzmaEnc_InitPriceTables(p->ProbPrices);
1598 p->litProbs = 0;
1599 p->saveState.litProbs = 0;
1600}
1601
Alexandru Gagniuc9ad52fe2014-01-27 20:57:54 -06001602CLzmaEncHandle LzmaEnc_Create(struct ISzAlloc *alloc)
Stefan Reinauer2e200cd2012-10-30 14:02:45 -07001603{
1604 void *p;
Alexandru Gagniuc9ad52fe2014-01-27 20:57:54 -06001605 p = alloc->Alloc(alloc, sizeof(struct CLzmaEnc));
Stefan Reinauer2e200cd2012-10-30 14:02:45 -07001606 if (p != 0)
Alexandru Gagniuc9ad52fe2014-01-27 20:57:54 -06001607 LzmaEnc_Construct((struct CLzmaEnc *)p);
Stefan Reinauer2e200cd2012-10-30 14:02:45 -07001608 return p;
1609}
1610
Alexandru Gagniuc9ad52fe2014-01-27 20:57:54 -06001611static void LzmaEnc_FreeLits(struct CLzmaEnc *p, struct ISzAlloc *alloc)
Stefan Reinauer2e200cd2012-10-30 14:02:45 -07001612{
1613 alloc->Free(alloc, p->litProbs);
1614 alloc->Free(alloc, p->saveState.litProbs);
1615 p->litProbs = 0;
1616 p->saveState.litProbs = 0;
1617}
1618
Alexandru Gagniuc9ad52fe2014-01-27 20:57:54 -06001619static void LzmaEnc_Destruct(struct CLzmaEnc *p, struct ISzAlloc *alloc, struct ISzAlloc *allocBig)
Stefan Reinauer2e200cd2012-10-30 14:02:45 -07001620{
Stefan Reinauer2e200cd2012-10-30 14:02:45 -07001621 MatchFinder_Free(&p->matchFinderBase, allocBig);
1622 LzmaEnc_FreeLits(p, alloc);
1623 RangeEnc_Free(&p->rc, alloc);
1624}
1625
Alexandru Gagniuc9ad52fe2014-01-27 20:57:54 -06001626void LzmaEnc_Destroy(CLzmaEncHandle p, struct ISzAlloc *alloc, struct ISzAlloc *allocBig)
Stefan Reinauer2e200cd2012-10-30 14:02:45 -07001627{
Alexandru Gagniuc9ad52fe2014-01-27 20:57:54 -06001628 LzmaEnc_Destruct((struct CLzmaEnc *)p, alloc, allocBig);
Stefan Reinauer2e200cd2012-10-30 14:02:45 -07001629 alloc->Free(alloc, p);
1630}
1631
Alexandru Gagniuc9ad52fe2014-01-27 20:57:54 -06001632static SRes LzmaEnc_CodeOneBlock(struct CLzmaEnc *p, bool useLimits, uint32_t maxPackSize, uint32_t maxUnpackSize)
Stefan Reinauer2e200cd2012-10-30 14:02:45 -07001633{
Alexandru Gagniuc91e9f272014-01-26 22:55:01 -06001634 uint32_t nowPos32, startPos32;
Stefan Reinauer2e200cd2012-10-30 14:02:45 -07001635 if (p->needInit)
1636 {
1637 p->matchFinder.Init(p->matchFinderObj);
1638 p->needInit = 0;
1639 }
1640
1641 if (p->finished)
1642 return p->result;
1643 RINOK(CheckErrors(p));
1644
Alexandru Gagniuc91e9f272014-01-26 22:55:01 -06001645 nowPos32 = (uint32_t)p->nowPos64;
Stefan Reinauer2e200cd2012-10-30 14:02:45 -07001646 startPos32 = nowPos32;
1647
1648 if (p->nowPos64 == 0)
1649 {
Alexandru Gagniuc91e9f272014-01-26 22:55:01 -06001650 uint32_t numPairs;
1651 uint8_t curuint8_t;
Stefan Reinauer2e200cd2012-10-30 14:02:45 -07001652 if (p->matchFinder.GetNumAvailableBytes(p->matchFinderObj) == 0)
1653 return Flush(p, nowPos32);
1654 ReadMatchDistances(p, &numPairs);
1655 RangeEnc_EncodeBit(&p->rc, &p->isMatch[p->state][0], 0);
1656 p->state = kLiteralNextStates[p->state];
Alexandru Gagniuc91e9f272014-01-26 22:55:01 -06001657 curuint8_t = p->matchFinder.GetIndexByte(p->matchFinderObj, 0 - p->additionalOffset);
1658 LitEnc_Encode(&p->rc, p->litProbs, curuint8_t);
Stefan Reinauer2e200cd2012-10-30 14:02:45 -07001659 p->additionalOffset--;
1660 nowPos32++;
1661 }
1662
1663 if (p->matchFinder.GetNumAvailableBytes(p->matchFinderObj) != 0)
1664 for (;;)
1665 {
Alexandru Gagniuc91e9f272014-01-26 22:55:01 -06001666 uint32_t pos, len, posState;
Stefan Reinauer2e200cd2012-10-30 14:02:45 -07001667
1668 if (p->fastMode)
1669 len = GetOptimumFast(p, &pos);
1670 else
1671 len = GetOptimum(p, nowPos32, &pos);
1672
Stefan Reinauer2e200cd2012-10-30 14:02:45 -07001673 posState = nowPos32 & p->pbMask;
Alexandru Gagniuc91e9f272014-01-26 22:55:01 -06001674 if (len == 1 && pos == (uint32_t)-1)
Stefan Reinauer2e200cd2012-10-30 14:02:45 -07001675 {
Alexandru Gagniuc91e9f272014-01-26 22:55:01 -06001676 uint8_t curuint8_t;
Stefan Reinauer2e200cd2012-10-30 14:02:45 -07001677 CLzmaProb *probs;
Alexandru Gagniuc91e9f272014-01-26 22:55:01 -06001678 const uint8_t *data;
Stefan Reinauer2e200cd2012-10-30 14:02:45 -07001679
1680 RangeEnc_EncodeBit(&p->rc, &p->isMatch[p->state][posState], 0);
1681 data = p->matchFinder.GetPointerToCurrentPos(p->matchFinderObj) - p->additionalOffset;
Alexandru Gagniuc91e9f272014-01-26 22:55:01 -06001682 curuint8_t = *data;
Stefan Reinauer2e200cd2012-10-30 14:02:45 -07001683 probs = LIT_PROBS(nowPos32, *(data - 1));
1684 if (IsCharState(p->state))
Alexandru Gagniuc91e9f272014-01-26 22:55:01 -06001685 LitEnc_Encode(&p->rc, probs, curuint8_t);
Stefan Reinauer2e200cd2012-10-30 14:02:45 -07001686 else
Alexandru Gagniuc91e9f272014-01-26 22:55:01 -06001687 LitEnc_EncodeMatched(&p->rc, probs, curuint8_t, *(data - p->reps[0] - 1));
Stefan Reinauer2e200cd2012-10-30 14:02:45 -07001688 p->state = kLiteralNextStates[p->state];
1689 }
1690 else
1691 {
1692 RangeEnc_EncodeBit(&p->rc, &p->isMatch[p->state][posState], 1);
1693 if (pos < LZMA_NUM_REPS)
1694 {
1695 RangeEnc_EncodeBit(&p->rc, &p->isRep[p->state], 1);
1696 if (pos == 0)
1697 {
1698 RangeEnc_EncodeBit(&p->rc, &p->isRepG0[p->state], 0);
1699 RangeEnc_EncodeBit(&p->rc, &p->isRep0Long[p->state][posState], ((len == 1) ? 0 : 1));
1700 }
1701 else
1702 {
Alexandru Gagniuc91e9f272014-01-26 22:55:01 -06001703 uint32_t distance = p->reps[pos];
Stefan Reinauer2e200cd2012-10-30 14:02:45 -07001704 RangeEnc_EncodeBit(&p->rc, &p->isRepG0[p->state], 1);
1705 if (pos == 1)
1706 RangeEnc_EncodeBit(&p->rc, &p->isRepG1[p->state], 0);
1707 else
1708 {
1709 RangeEnc_EncodeBit(&p->rc, &p->isRepG1[p->state], 1);
1710 RangeEnc_EncodeBit(&p->rc, &p->isRepG2[p->state], pos - 2);
1711 if (pos == 3)
1712 p->reps[3] = p->reps[2];
1713 p->reps[2] = p->reps[1];
1714 }
1715 p->reps[1] = p->reps[0];
1716 p->reps[0] = distance;
1717 }
1718 if (len == 1)
1719 p->state = kShortRepNextStates[p->state];
1720 else
1721 {
1722 LenEnc_Encode2(&p->repLenEnc, &p->rc, len - LZMA_MATCH_LEN_MIN, posState, !p->fastMode, p->ProbPrices);
1723 p->state = kRepNextStates[p->state];
1724 }
1725 }
1726 else
1727 {
Alexandru Gagniuc91e9f272014-01-26 22:55:01 -06001728 uint32_t posSlot;
Stefan Reinauer2e200cd2012-10-30 14:02:45 -07001729 RangeEnc_EncodeBit(&p->rc, &p->isRep[p->state], 0);
1730 p->state = kMatchNextStates[p->state];
1731 LenEnc_Encode2(&p->lenEnc, &p->rc, len - LZMA_MATCH_LEN_MIN, posState, !p->fastMode, p->ProbPrices);
1732 pos -= LZMA_NUM_REPS;
1733 GetPosSlot(pos, posSlot);
1734 RcTree_Encode(&p->rc, p->posSlotEncoder[GetLenToPosState(len)], kNumPosSlotBits, posSlot);
Kyösti Mälkkiecd84242013-09-13 07:57:49 +03001735
Stefan Reinauer2e200cd2012-10-30 14:02:45 -07001736 if (posSlot >= kStartPosModelIndex)
1737 {
Alexandru Gagniuc91e9f272014-01-26 22:55:01 -06001738 uint32_t footerBits = ((posSlot >> 1) - 1);
1739 uint32_t base = ((2 | (posSlot & 1)) << footerBits);
1740 uint32_t posReduced = pos - base;
Stefan Reinauer2e200cd2012-10-30 14:02:45 -07001741
1742 if (posSlot < kEndPosModelIndex)
1743 RcTree_ReverseEncode(&p->rc, p->posEncoders + base - posSlot - 1, footerBits, posReduced);
1744 else
1745 {
1746 RangeEnc_EncodeDirectBits(&p->rc, posReduced >> kNumAlignBits, footerBits - kNumAlignBits);
1747 RcTree_ReverseEncode(&p->rc, p->posAlignEncoder, kNumAlignBits, posReduced & kAlignMask);
1748 p->alignPriceCount++;
1749 }
1750 }
1751 p->reps[3] = p->reps[2];
1752 p->reps[2] = p->reps[1];
1753 p->reps[1] = p->reps[0];
1754 p->reps[0] = pos;
1755 p->matchPriceCount++;
1756 }
1757 }
1758 p->additionalOffset -= len;
1759 nowPos32 += len;
1760 if (p->additionalOffset == 0)
1761 {
Alexandru Gagniuc91e9f272014-01-26 22:55:01 -06001762 uint32_t processed;
Stefan Reinauer2e200cd2012-10-30 14:02:45 -07001763 if (!p->fastMode)
1764 {
1765 if (p->matchPriceCount >= (1 << 7))
1766 FillDistancesPrices(p);
1767 if (p->alignPriceCount >= kAlignTableSize)
1768 FillAlignPrices(p);
1769 }
1770 if (p->matchFinder.GetNumAvailableBytes(p->matchFinderObj) == 0)
1771 break;
1772 processed = nowPos32 - startPos32;
1773 if (useLimits)
1774 {
1775 if (processed + kNumOpts + 300 >= maxUnpackSize ||
1776 RangeEnc_GetProcessed(&p->rc) + kNumOpts * 2 >= maxPackSize)
1777 break;
1778 }
1779 else if (processed >= (1 << 15))
1780 {
1781 p->nowPos64 += nowPos32 - startPos32;
1782 return CheckErrors(p);
1783 }
1784 }
1785 }
1786 p->nowPos64 += nowPos32 - startPos32;
1787 return Flush(p, nowPos32);
1788}
1789
Alexandru Gagniuc91e9f272014-01-26 22:55:01 -06001790#define kBigHashDicLimit ((uint32_t)1 << 24)
Stefan Reinauer2e200cd2012-10-30 14:02:45 -07001791
Alexandru Gagniuc9ad52fe2014-01-27 20:57:54 -06001792static SRes LzmaEnc_Alloc(struct CLzmaEnc *p, uint32_t keepWindowSize, struct ISzAlloc *alloc, struct ISzAlloc *allocBig)
Stefan Reinauer2e200cd2012-10-30 14:02:45 -07001793{
Alexandru Gagniuc91e9f272014-01-26 22:55:01 -06001794 uint32_t beforeSize = kNumOpts;
Stefan Reinauer2e200cd2012-10-30 14:02:45 -07001795 if (!RangeEnc_Alloc(&p->rc, alloc))
1796 return SZ_ERROR_MEM;
Stefan Reinauer2e200cd2012-10-30 14:02:45 -07001797
1798 {
1799 unsigned lclp = p->lc + p->lp;
1800 if (p->litProbs == 0 || p->saveState.litProbs == 0 || p->lclp != lclp)
1801 {
1802 LzmaEnc_FreeLits(p, alloc);
1803 p->litProbs = (CLzmaProb *)alloc->Alloc(alloc, (0x300 << lclp) * sizeof(CLzmaProb));
1804 p->saveState.litProbs = (CLzmaProb *)alloc->Alloc(alloc, (0x300 << lclp) * sizeof(CLzmaProb));
1805 if (p->litProbs == 0 || p->saveState.litProbs == 0)
1806 {
1807 LzmaEnc_FreeLits(p, alloc);
1808 return SZ_ERROR_MEM;
1809 }
1810 p->lclp = lclp;
1811 }
1812 }
1813
1814 p->matchFinderBase.bigHash = (p->dictSize > kBigHashDicLimit);
1815
1816 if (beforeSize + p->dictSize < keepWindowSize)
1817 beforeSize = keepWindowSize - p->dictSize;
1818
Stefan Reinauer2e200cd2012-10-30 14:02:45 -07001819 {
Alexandru Gagniuc91e9f272014-01-26 22:55:01 -06001820 if (!MatchFinder_Create(&p->matchFinderBase, p->dictSize, beforeSize, p->numFastuint8_ts, LZMA_MATCH_LEN_MAX, allocBig))
Stefan Reinauer2e200cd2012-10-30 14:02:45 -07001821 return SZ_ERROR_MEM;
1822 p->matchFinderObj = &p->matchFinderBase;
1823 MatchFinder_CreateVTable(&p->matchFinderBase, &p->matchFinder);
1824 }
1825 return SZ_OK;
1826}
1827
Alexandru Gagniuc9ad52fe2014-01-27 20:57:54 -06001828static void LzmaEnc_Init(struct CLzmaEnc *p)
Stefan Reinauer2e200cd2012-10-30 14:02:45 -07001829{
Alexandru Gagniuc91e9f272014-01-26 22:55:01 -06001830 uint32_t i;
Stefan Reinauer2e200cd2012-10-30 14:02:45 -07001831 p->state = 0;
1832 for (i = 0 ; i < LZMA_NUM_REPS; i++)
1833 p->reps[i] = 0;
1834
1835 RangeEnc_Init(&p->rc);
1836
1837
1838 for (i = 0; i < kNumStates; i++)
1839 {
Alexandru Gagniuc91e9f272014-01-26 22:55:01 -06001840 uint32_t j;
Stefan Reinauer2e200cd2012-10-30 14:02:45 -07001841 for (j = 0; j < LZMA_NUM_PB_STATES_MAX; j++)
1842 {
1843 p->isMatch[i][j] = kProbInitValue;
1844 p->isRep0Long[i][j] = kProbInitValue;
1845 }
1846 p->isRep[i] = kProbInitValue;
1847 p->isRepG0[i] = kProbInitValue;
1848 p->isRepG1[i] = kProbInitValue;
1849 p->isRepG2[i] = kProbInitValue;
1850 }
1851
1852 {
Alexandru Gagniuc91e9f272014-01-26 22:55:01 -06001853 uint32_t num = 0x300 << (p->lp + p->lc);
Stefan Reinauer2e200cd2012-10-30 14:02:45 -07001854 for (i = 0; i < num; i++)
1855 p->litProbs[i] = kProbInitValue;
1856 }
1857
1858 {
1859 for (i = 0; i < kNumLenToPosStates; i++)
1860 {
1861 CLzmaProb *probs = p->posSlotEncoder[i];
Alexandru Gagniuc91e9f272014-01-26 22:55:01 -06001862 uint32_t j;
Stefan Reinauer2e200cd2012-10-30 14:02:45 -07001863 for (j = 0; j < (1 << kNumPosSlotBits); j++)
1864 probs[j] = kProbInitValue;
1865 }
1866 }
1867 {
1868 for (i = 0; i < kNumFullDistances - kEndPosModelIndex; i++)
1869 p->posEncoders[i] = kProbInitValue;
1870 }
1871
1872 LenEnc_Init(&p->lenEnc.p);
1873 LenEnc_Init(&p->repLenEnc.p);
1874
1875 for (i = 0; i < (1 << kNumAlignBits); i++)
1876 p->posAlignEncoder[i] = kProbInitValue;
1877
1878 p->optimumEndIndex = 0;
1879 p->optimumCurrentIndex = 0;
1880 p->additionalOffset = 0;
1881
1882 p->pbMask = (1 << p->pb) - 1;
1883 p->lpMask = (1 << p->lp) - 1;
1884}
1885
Alexandru Gagniuc9ad52fe2014-01-27 20:57:54 -06001886static void LzmaEnc_InitPrices(struct CLzmaEnc *p)
Stefan Reinauer2e200cd2012-10-30 14:02:45 -07001887{
1888 if (!p->fastMode)
1889 {
1890 FillDistancesPrices(p);
1891 FillAlignPrices(p);
1892 }
1893
1894 p->lenEnc.tableSize =
1895 p->repLenEnc.tableSize =
Alexandru Gagniuc91e9f272014-01-26 22:55:01 -06001896 p->numFastuint8_ts + 1 - LZMA_MATCH_LEN_MIN;
Stefan Reinauer2e200cd2012-10-30 14:02:45 -07001897 LenPriceEnc_UpdateTables(&p->lenEnc, 1 << p->pb, p->ProbPrices);
1898 LenPriceEnc_UpdateTables(&p->repLenEnc, 1 << p->pb, p->ProbPrices);
1899}
1900
Alexandru Gagniuc9ad52fe2014-01-27 20:57:54 -06001901static SRes LzmaEnc_AllocAndInit(struct CLzmaEnc *p, uint32_t keepWindowSize, struct ISzAlloc *alloc, struct ISzAlloc *allocBig)
Stefan Reinauer2e200cd2012-10-30 14:02:45 -07001902{
Alexandru Gagniuc91e9f272014-01-26 22:55:01 -06001903 uint32_t i;
1904 for (i = 0; i < (uint32_t)kDicLogSizeMaxCompress; i++)
1905 if (p->dictSize <= ((uint32_t)1 << i))
Stefan Reinauer2e200cd2012-10-30 14:02:45 -07001906 break;
1907 p->distTableSize = i * 2;
1908
Alexandru Gagniuc91e9f272014-01-26 22:55:01 -06001909 p->finished = false;
Stefan Reinauer2e200cd2012-10-30 14:02:45 -07001910 p->result = SZ_OK;
1911 RINOK(LzmaEnc_Alloc(p, keepWindowSize, alloc, allocBig));
1912 LzmaEnc_Init(p);
1913 LzmaEnc_InitPrices(p);
1914 p->nowPos64 = 0;
1915 return SZ_OK;
1916}
1917
Alexandru Gagniuc9ad52fe2014-01-27 20:57:54 -06001918static SRes LzmaEnc_Prepare(CLzmaEncHandle pp, struct ISeqOutStream *outStream, struct ISeqInStream *inStream,
1919 struct ISzAlloc *alloc, struct ISzAlloc *allocBig)
Stefan Reinauer2e200cd2012-10-30 14:02:45 -07001920{
Alexandru Gagniuc9ad52fe2014-01-27 20:57:54 -06001921 struct CLzmaEnc *p = (struct CLzmaEnc *)pp;
Stefan Reinauer2e200cd2012-10-30 14:02:45 -07001922 p->matchFinderBase.stream = inStream;
1923 p->needInit = 1;
1924 p->rc.outStream = outStream;
1925 return LzmaEnc_AllocAndInit(p, 0, alloc, allocBig);
1926}
1927
1928/*static SRes LzmaEnc_PrepareForLzma2(CLzmaEncHandle pp,
Alexandru Gagniuc91e9f272014-01-26 22:55:01 -06001929 ISeqInStream *inStream, uint32_t keepWindowSize,
Stefan Reinauer2e200cd2012-10-30 14:02:45 -07001930 ISzAlloc *alloc, ISzAlloc *allocBig)
1931{
1932 CLzmaEnc *p = (CLzmaEnc *)pp;
1933 p->matchFinderBase.stream = inStream;
1934 p->needInit = 1;
1935 return LzmaEnc_AllocAndInit(p, keepWindowSize, alloc, allocBig);
1936}*/
1937
Alexandru Gagniuc9ad52fe2014-01-27 20:57:54 -06001938static void LzmaEnc_SetInputBuf(struct CLzmaEnc *p, const uint8_t *src, size_t srcLen)
Stefan Reinauer2e200cd2012-10-30 14:02:45 -07001939{
1940 p->matchFinderBase.directInput = 1;
Alexandru Gagniuc91e9f272014-01-26 22:55:01 -06001941 p->matchFinderBase.bufferBase = (uint8_t *)src;
Stefan Reinauer2e200cd2012-10-30 14:02:45 -07001942 p->matchFinderBase.directInputRem = srcLen;
1943}
1944
Alexandru Gagniuc91e9f272014-01-26 22:55:01 -06001945static SRes LzmaEnc_MemPrepare(CLzmaEncHandle pp, const uint8_t *src, size_t srcLen,
Alexandru Gagniuc9ad52fe2014-01-27 20:57:54 -06001946 uint32_t keepWindowSize, struct ISzAlloc *alloc, struct ISzAlloc *allocBig)
Stefan Reinauer2e200cd2012-10-30 14:02:45 -07001947{
Alexandru Gagniuc9ad52fe2014-01-27 20:57:54 -06001948 struct CLzmaEnc *p = (struct CLzmaEnc *)pp;
Stefan Reinauer2e200cd2012-10-30 14:02:45 -07001949 LzmaEnc_SetInputBuf(p, src, srcLen);
1950 p->needInit = 1;
1951
1952 return LzmaEnc_AllocAndInit(p, keepWindowSize, alloc, allocBig);
1953}
1954
1955static void LzmaEnc_Finish(CLzmaEncHandle pp)
1956{
Alexandru Gagniucae45a982014-01-29 14:27:52 -06001957 (void)pp;
Stefan Reinauer2e200cd2012-10-30 14:02:45 -07001958}
1959
Alexandru Gagniuc9ad52fe2014-01-27 20:57:54 -06001960struct CSeqOutStreamBuf
Stefan Reinauer2e200cd2012-10-30 14:02:45 -07001961{
Alexandru Gagniuc9ad52fe2014-01-27 20:57:54 -06001962 struct ISeqOutStream funcTable;
Alexandru Gagniuc91e9f272014-01-26 22:55:01 -06001963 uint8_t *data;
1964 size_t rem;
1965 bool overflow;
Alexandru Gagniuc9ad52fe2014-01-27 20:57:54 -06001966};
Stefan Reinauer2e200cd2012-10-30 14:02:45 -07001967
1968static size_t MyWrite(void *pp, const void *data, size_t size)
1969{
Alexandru Gagniuc9ad52fe2014-01-27 20:57:54 -06001970 struct CSeqOutStreamBuf *p = (struct CSeqOutStreamBuf *)pp;
Stefan Reinauer2e200cd2012-10-30 14:02:45 -07001971 if (p->rem < size)
1972 {
1973 size = p->rem;
Alexandru Gagniuc91e9f272014-01-26 22:55:01 -06001974 p->overflow = true;
Stefan Reinauer2e200cd2012-10-30 14:02:45 -07001975 }
1976 memcpy(p->data, data, size);
1977 p->rem -= size;
1978 p->data += size;
1979 return size;
1980}
1981
1982
Alexandru Gagniuc91e9f272014-01-26 22:55:01 -06001983/*static uint32_t LzmaEnc_GetNumAvailableBytes(CLzmaEncHandle pp)
Stefan Reinauer2e200cd2012-10-30 14:02:45 -07001984{
1985 const CLzmaEnc *p = (CLzmaEnc *)pp;
1986 return p->matchFinder.GetNumAvailableBytes(p->matchFinderObj);
1987}*/
1988
Alexandru Gagniuc91e9f272014-01-26 22:55:01 -06001989/*static const uint8_t *LzmaEnc_GetCurBuf(CLzmaEncHandle pp)
Stefan Reinauer2e200cd2012-10-30 14:02:45 -07001990{
1991 const CLzmaEnc *p = (CLzmaEnc *)pp;
1992 return p->matchFinder.GetPointerToCurrentPos(p->matchFinderObj) - p->additionalOffset;
1993}*/
1994
Alexandru Gagniuc91e9f272014-01-26 22:55:01 -06001995/*static SRes LzmaEnc_CodeOneMemBlock(CLzmaEncHandle pp, bool reInit,
1996 uint8_t *dest, size_t *destLen, uint32_t desiredPackSize, uint32_t *unpackSize)
Stefan Reinauer2e200cd2012-10-30 14:02:45 -07001997{
1998 CLzmaEnc *p = (CLzmaEnc *)pp;
Alexandru Gagniuc91e9f272014-01-26 22:55:01 -06001999 uint64_t nowPos64;
Stefan Reinauer2e200cd2012-10-30 14:02:45 -07002000 SRes res;
2001 CSeqOutStreamBuf outStream;
2002
2003 outStream.funcTable.Write = MyWrite;
2004 outStream.data = dest;
2005 outStream.rem = *destLen;
Alexandru Gagniuc91e9f272014-01-26 22:55:01 -06002006 outStream.overflow = false;
Stefan Reinauer2e200cd2012-10-30 14:02:45 -07002007
Alexandru Gagniuc91e9f272014-01-26 22:55:01 -06002008 p->writeEndMark = false;
2009 p->finished = false;
Stefan Reinauer2e200cd2012-10-30 14:02:45 -07002010 p->result = SZ_OK;
2011
2012 if (reInit)
2013 LzmaEnc_Init(p);
2014 LzmaEnc_InitPrices(p);
2015 nowPos64 = p->nowPos64;
2016 RangeEnc_Init(&p->rc);
2017 p->rc.outStream = &outStream.funcTable;
2018
Alexandru Gagniuc91e9f272014-01-26 22:55:01 -06002019 res = LzmaEnc_CodeOneBlock(p, true, desiredPackSize, *unpackSize);
Kyösti Mälkkiecd84242013-09-13 07:57:49 +03002020
Alexandru Gagniuc91e9f272014-01-26 22:55:01 -06002021 *unpackSize = (uint32_t)(p->nowPos64 - nowPos64);
Stefan Reinauer2e200cd2012-10-30 14:02:45 -07002022 *destLen -= outStream.rem;
2023 if (outStream.overflow)
2024 return SZ_ERROR_OUTPUT_EOF;
2025
2026 return res;
2027}*/
2028
Alexandru Gagniuc9ad52fe2014-01-27 20:57:54 -06002029static SRes LzmaEnc_Encode2(struct CLzmaEnc *p, struct ICompressProgress *progress)
Stefan Reinauer2e200cd2012-10-30 14:02:45 -07002030{
2031 SRes res = SZ_OK;
2032
Stefan Reinauer2e200cd2012-10-30 14:02:45 -07002033 for (;;)
2034 {
Alexandru Gagniuc91e9f272014-01-26 22:55:01 -06002035 res = LzmaEnc_CodeOneBlock(p, false, 0, 0);
Stefan Reinauer2e200cd2012-10-30 14:02:45 -07002036 if (res != SZ_OK || p->finished != 0)
2037 break;
2038 if (progress != 0)
2039 {
2040 res = progress->Progress(progress, p->nowPos64, RangeEnc_GetProcessed(&p->rc));
2041 if (res != SZ_OK)
2042 {
2043 res = SZ_ERROR_PROGRESS;
2044 break;
2045 }
2046 }
2047 }
2048 LzmaEnc_Finish(p);
2049 return res;
2050}
2051
Alexandru Gagniuc9ad52fe2014-01-27 20:57:54 -06002052SRes LzmaEnc_Encode(CLzmaEncHandle pp, struct ISeqOutStream *outStream, struct ISeqInStream *inStream, struct ICompressProgress *progress,
2053 struct ISzAlloc *alloc, struct ISzAlloc *allocBig)
Stefan Reinauer2e200cd2012-10-30 14:02:45 -07002054{
2055 RINOK(LzmaEnc_Prepare(pp, outStream, inStream, alloc, allocBig));
Alexandru Gagniuc9ad52fe2014-01-27 20:57:54 -06002056 return LzmaEnc_Encode2((struct CLzmaEnc *)pp, progress);
Stefan Reinauer2e200cd2012-10-30 14:02:45 -07002057}
2058
Alexandru Gagniuc91e9f272014-01-26 22:55:01 -06002059SRes LzmaEnc_WriteProperties(CLzmaEncHandle pp, uint8_t *props, size_t *size)
Stefan Reinauer2e200cd2012-10-30 14:02:45 -07002060{
Alexandru Gagniuc9ad52fe2014-01-27 20:57:54 -06002061 struct CLzmaEnc *p = (struct CLzmaEnc *)pp;
Stefan Reinauer2e200cd2012-10-30 14:02:45 -07002062 int i;
Alexandru Gagniuc91e9f272014-01-26 22:55:01 -06002063 uint32_t dictSize = p->dictSize;
Stefan Reinauer2e200cd2012-10-30 14:02:45 -07002064 if (*size < LZMA_PROPS_SIZE)
2065 return SZ_ERROR_PARAM;
2066 *size = LZMA_PROPS_SIZE;
Alexandru Gagniuc91e9f272014-01-26 22:55:01 -06002067 props[0] = (uint8_t)((p->pb * 5 + p->lp) * 9 + p->lc);
Stefan Reinauer2e200cd2012-10-30 14:02:45 -07002068
2069 for (i = 11; i <= 30; i++)
2070 {
Alexandru Gagniuc91e9f272014-01-26 22:55:01 -06002071 if (dictSize <= ((uint32_t)2 << i))
Stefan Reinauer2e200cd2012-10-30 14:02:45 -07002072 {
2073 dictSize = (2 << i);
2074 break;
2075 }
Alexandru Gagniuc91e9f272014-01-26 22:55:01 -06002076 if (dictSize <= ((uint32_t)3 << i))
Stefan Reinauer2e200cd2012-10-30 14:02:45 -07002077 {
2078 dictSize = (3 << i);
2079 break;
2080 }
2081 }
2082
2083 for (i = 0; i < 4; i++)
Alexandru Gagniuc91e9f272014-01-26 22:55:01 -06002084 props[1 + i] = (uint8_t)(dictSize >> (8 * i));
Stefan Reinauer2e200cd2012-10-30 14:02:45 -07002085 return SZ_OK;
2086}
2087
Alexandru Gagniuc91e9f272014-01-26 22:55:01 -06002088SRes LzmaEnc_MemEncode(CLzmaEncHandle pp, uint8_t *dest, size_t *destLen, const uint8_t *src, size_t srcLen,
Alexandru Gagniuc9ad52fe2014-01-27 20:57:54 -06002089 int writeEndMark, struct ICompressProgress *progress, struct ISzAlloc *alloc, struct ISzAlloc *allocBig)
Stefan Reinauer2e200cd2012-10-30 14:02:45 -07002090{
2091 SRes res;
Alexandru Gagniuc9ad52fe2014-01-27 20:57:54 -06002092 struct CLzmaEnc *p = (struct CLzmaEnc *)pp;
Stefan Reinauer2e200cd2012-10-30 14:02:45 -07002093
Alexandru Gagniuc9ad52fe2014-01-27 20:57:54 -06002094 struct CSeqOutStreamBuf outStream;
Stefan Reinauer2e200cd2012-10-30 14:02:45 -07002095
2096 LzmaEnc_SetInputBuf(p, src, srcLen);
2097
2098 outStream.funcTable.Write = MyWrite;
2099 outStream.data = dest;
2100 outStream.rem = *destLen;
Alexandru Gagniuc91e9f272014-01-26 22:55:01 -06002101 outStream.overflow = false;
Stefan Reinauer2e200cd2012-10-30 14:02:45 -07002102
2103 p->writeEndMark = writeEndMark;
2104
2105 p->rc.outStream = &outStream.funcTable;
2106 res = LzmaEnc_MemPrepare(pp, src, srcLen, 0, alloc, allocBig);
2107 if (res == SZ_OK)
2108 res = LzmaEnc_Encode2(p, progress);
2109
2110 *destLen -= outStream.rem;
2111 if (outStream.overflow)
2112 return SZ_ERROR_OUTPUT_EOF;
2113 return res;
2114}
2115
Alexandru Gagniuc91e9f272014-01-26 22:55:01 -06002116SRes LzmaEncode(uint8_t *dest, size_t *destLen, const uint8_t *src, size_t srcLen,
Alexandru Gagniuc9ad52fe2014-01-27 20:57:54 -06002117 const struct CLzmaEncProps *props, uint8_t *propsEncoded, size_t *propsSize, int writeEndMark,
2118 struct ICompressProgress *progress, struct ISzAlloc *alloc, struct ISzAlloc *allocBig)
Stefan Reinauer2e200cd2012-10-30 14:02:45 -07002119{
Alexandru Gagniuc9ad52fe2014-01-27 20:57:54 -06002120 struct CLzmaEnc *p = (struct CLzmaEnc *)LzmaEnc_Create(alloc);
Stefan Reinauer2e200cd2012-10-30 14:02:45 -07002121 SRes res;
2122 if (p == 0)
2123 return SZ_ERROR_MEM;
2124
2125 res = LzmaEnc_SetProps(p, props);
2126 if (res == SZ_OK)
2127 {
2128 res = LzmaEnc_WriteProperties(p, propsEncoded, propsSize);
2129 if (res == SZ_OK)
2130 res = LzmaEnc_MemEncode(p, dest, destLen, src, srcLen,
2131 writeEndMark, progress, alloc, allocBig);
2132 }
2133
2134 LzmaEnc_Destroy(p, alloc, allocBig);
2135 return res;
2136}