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