2
* GRUB -- GRand Unified Bootloader
3
* Copyright (c) 1999-2008 Igor Pavlov
4
* Copyright (C) 2008 Free Software Foundation, Inc.
6
* GRUB is free software: you can redistribute it and/or modify
7
* it under the terms of the GNU General Public License as published by
8
* the Free Software Foundation, either version 3 of the License, or
9
* (at your option) any later version.
11
* GRUB is distributed in the hope that it will be useful,
12
* but WITHOUT ANY WARRANTY; without even the implied warranty of
13
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
* GNU General Public License for more details.
16
* You should have received a copy of the GNU General Public License
17
* along with GRUB. If not, see <http://www.gnu.org/licenses/>.
21
* This code was taken from LZMA SDK 4.58 beta, and was slightly modified
22
* to adapt it to GRUB's requirement.
24
* See <http://www.7-zip.org>, for more information about LZMA.
32
#include <grub/lib/LzmaEnc.h>
34
#include <grub/lib/LzFind.h>
36
#include <grub/lib/LzFindMt.h>
39
/* #define SHOW_STAT */
40
/* #define SHOW_STAT2 */
46
#define kBlockSizeMax ((1 << LZMA_NUM_BLOCK_SIZE_BITS) - 1)
48
#define kBlockSize (9 << 10)
49
#define kUnpackBlockSize (1 << 18)
50
#define kMatchArraySize (1 << 21)
51
#define kMatchRecordMaxSize ((LZMA_MATCH_LEN_MAX * 2 + 3) * LZMA_MATCH_LEN_MAX)
53
#define kNumMaxDirectBits (31)
55
#define kNumTopBits 24
56
#define kTopValue ((UInt32)1 << kNumTopBits)
58
#define kNumBitModelTotalBits 11
59
#define kBitModelTotal (1 << kNumBitModelTotalBits)
60
#define kNumMoveBits 5
61
#define kProbInitValue (kBitModelTotal >> 1)
63
#define kNumMoveReducingBits 4
64
#define kNumBitPriceShiftBits 4
65
#define kBitPrice (1 << kNumBitPriceShiftBits)
67
void LzmaEncProps_Init(CLzmaEncProps *p)
70
p->dictSize = p->mc = 0;
71
p->lc = p->lp = p->pb = p->algo = p->fb = p->btMode = p->numHashBytes = p->numThreads = -1;
75
void LzmaEncProps_Normalize(CLzmaEncProps *p)
78
if (level < 0) level = 5;
80
if (p->dictSize == 0) p->dictSize = (level <= 5 ? (1 << (level * 2 + 14)) : (level == 6 ? (1 << 25) : (1 << 26)));
81
if (p->lc < 0) p->lc = 3;
82
if (p->lp < 0) p->lp = 0;
83
if (p->pb < 0) p->pb = 2;
84
if (p->algo < 0) p->algo = (level < 5 ? 0 : 1);
85
if (p->fb < 0) p->fb = (level < 7 ? 32 : 64);
86
if (p->btMode < 0) p->btMode = (p->algo == 0 ? 0 : 1);
87
if (p->numHashBytes < 0) p->numHashBytes = 4;
88
if (p->mc == 0) p->mc = (16 + (p->fb >> 1)) >> (p->btMode ? 0 : 1);
89
if (p->numThreads < 0) p->numThreads = ((p->btMode && p->algo) ? 2 : 1);
92
UInt32 LzmaEncProps_GetDictSize(const CLzmaEncProps *props2)
94
CLzmaEncProps props = *props2;
95
LzmaEncProps_Normalize(&props);
96
return props.dictSize;
99
/* #define LZMA_LOG_BSR */
100
/* Define it for Intel's CPU */
105
#define kDicLogSizeMaxCompress 30
107
#define BSR2_RET(pos, res) { unsigned long i; _BitScanReverse(&i, (pos)); res = (i + i) + ((pos >> (i - 1)) & 1); }
109
UInt32 GetPosSlot1(UInt32 pos)
115
#define GetPosSlot2(pos, res) { BSR2_RET(pos, res); }
116
#define GetPosSlot(pos, res) { if (pos < 2) res = pos; else BSR2_RET(pos, res); }
120
#define kNumLogBits (9 + (int)sizeof(size_t) / 2)
121
#define kDicLogSizeMaxCompress ((kNumLogBits - 1) * 2 + 7)
123
void LzmaEnc_FastPosInit(Byte *g_FastPos)
129
for (slotFast = 2; slotFast < kNumLogBits * 2; slotFast++)
131
UInt32 k = (1 << ((slotFast >> 1) - 1));
133
for (j = 0; j < k; j++, c++)
134
g_FastPos[c] = (Byte)slotFast;
138
#define BSR2_RET(pos, res) { UInt32 i = 6 + ((kNumLogBits - 1) & \
139
(0 - (((((UInt32)1 << (kNumLogBits + 6)) - 1) - pos) >> 31))); \
140
res = p->g_FastPos[pos >> i] + (i * 2); }
142
#define BSR2_RET(pos, res) { res = (pos < (1 << (kNumLogBits + 6))) ? \
143
p->g_FastPos[pos >> 6] + 12 : \
144
p->g_FastPos[pos >> (6 + kNumLogBits - 1)] + (6 + (kNumLogBits - 1)) * 2; }
147
#define GetPosSlot1(pos) p->g_FastPos[pos]
148
#define GetPosSlot2(pos, res) { BSR2_RET(pos, res); }
149
#define GetPosSlot(pos, res) { if (pos < kNumFullDistances) res = p->g_FastPos[pos]; else BSR2_RET(pos, res); }
154
#define LZMA_NUM_REPS 4
156
typedef unsigned CState;
158
typedef struct _COptimal
171
UInt32 backs[LZMA_NUM_REPS];
174
#define kNumOpts (1 << 12)
176
#define kNumLenToPosStates 4
177
#define kNumPosSlotBits 6
178
#define kDicLogSizeMin 0
179
#define kDicLogSizeMax 32
180
#define kDistTableSizeMax (kDicLogSizeMax * 2)
183
#define kNumAlignBits 4
184
#define kAlignTableSize (1 << kNumAlignBits)
185
#define kAlignMask (kAlignTableSize - 1)
187
#define kStartPosModelIndex 4
188
#define kEndPosModelIndex 14
189
#define kNumPosModels (kEndPosModelIndex - kStartPosModelIndex)
191
#define kNumFullDistances (1 << (kEndPosModelIndex / 2))
194
#define CLzmaProb UInt32
196
#define CLzmaProb UInt16
199
#define LZMA_PB_MAX 4
200
#define LZMA_LC_MAX 8
201
#define LZMA_LP_MAX 4
203
#define LZMA_NUM_PB_STATES_MAX (1 << LZMA_PB_MAX)
206
#define kLenNumLowBits 3
207
#define kLenNumLowSymbols (1 << kLenNumLowBits)
208
#define kLenNumMidBits 3
209
#define kLenNumMidSymbols (1 << kLenNumMidBits)
210
#define kLenNumHighBits 8
211
#define kLenNumHighSymbols (1 << kLenNumHighBits)
213
#define kLenNumSymbolsTotal (kLenNumLowSymbols + kLenNumMidSymbols + kLenNumHighSymbols)
215
#define LZMA_MATCH_LEN_MIN 2
216
#define LZMA_MATCH_LEN_MAX (LZMA_MATCH_LEN_MIN + kLenNumSymbolsTotal - 1)
218
#define kNumStates 12
224
CLzmaProb low[LZMA_NUM_PB_STATES_MAX << kLenNumLowBits];
225
CLzmaProb mid[LZMA_NUM_PB_STATES_MAX << kLenNumMidBits];
226
CLzmaProb high[kLenNumHighSymbols];
232
UInt32 prices[LZMA_NUM_PB_STATES_MAX][kLenNumSymbolsTotal];
234
UInt32 counters[LZMA_NUM_PB_STATES_MAX];
237
typedef struct _CRangeEnc
246
ISeqOutStream *outStream;
251
typedef struct _CSeqInStreamBuf
253
ISeqInStream funcTable;
258
static SRes MyRead(void *pp, void *data, size_t *size)
260
size_t curSize = *size;
261
CSeqInStreamBuf *p = (CSeqInStreamBuf *)pp;
262
if (p->rem < curSize)
264
memcpy(data, p->data, curSize);
275
CLzmaProb isMatch[kNumStates][LZMA_NUM_PB_STATES_MAX];
276
CLzmaProb isRep[kNumStates];
277
CLzmaProb isRepG0[kNumStates];
278
CLzmaProb isRepG1[kNumStates];
279
CLzmaProb isRepG2[kNumStates];
280
CLzmaProb isRep0Long[kNumStates][LZMA_NUM_PB_STATES_MAX];
282
CLzmaProb posSlotEncoder[kNumLenToPosStates][1 << kNumPosSlotBits];
283
CLzmaProb posEncoders[kNumFullDistances - kEndPosModelIndex];
284
CLzmaProb posAlignEncoder[1 << kNumAlignBits];
287
CLenPriceEnc repLenEnc;
289
UInt32 reps[LZMA_NUM_REPS];
293
typedef struct _CLzmaEnc
295
IMatchFinder matchFinder;
296
void *matchFinderObj;
298
#ifdef COMPRESS_MF_MT
300
CMatchFinderMt matchFinderMt;
303
CMatchFinder matchFinderBase;
305
#ifdef COMPRESS_MF_MT
309
UInt32 optimumEndIndex;
310
UInt32 optimumCurrentIndex;
312
Bool longestMatchWasFound;
313
UInt32 longestMatchLength;
314
UInt32 numDistancePairs;
316
COptimal opt[kNumOpts];
319
Byte g_FastPos[1 << kNumLogBits];
322
UInt32 ProbPrices[kBitModelTotal >> kNumMoveReducingBits];
323
UInt32 matchDistances[LZMA_MATCH_LEN_MAX * 2 + 2 + 1];
325
UInt32 additionalOffset;
326
UInt32 reps[LZMA_NUM_REPS];
329
UInt32 posSlotPrices[kNumLenToPosStates][kDistTableSizeMax];
330
UInt32 distancesPrices[kNumLenToPosStates][kNumFullDistances];
331
UInt32 alignPrices[kAlignTableSize];
332
UInt32 alignPriceCount;
334
UInt32 distTableSize;
337
unsigned lpMask, pbMask;
341
CLzmaProb isMatch[kNumStates][LZMA_NUM_PB_STATES_MAX];
342
CLzmaProb isRep[kNumStates];
343
CLzmaProb isRepG0[kNumStates];
344
CLzmaProb isRepG1[kNumStates];
345
CLzmaProb isRepG2[kNumStates];
346
CLzmaProb isRep0Long[kNumStates][LZMA_NUM_PB_STATES_MAX];
348
CLzmaProb posSlotEncoder[kNumLenToPosStates][1 << kNumPosSlotBits];
349
CLzmaProb posEncoders[kNumFullDistances - kEndPosModelIndex];
350
CLzmaProb posAlignEncoder[1 << kNumAlignBits];
353
CLenPriceEnc repLenEnc;
363
UInt32 matchPriceCount;
369
UInt32 matchFinderCycles;
371
ISeqInStream *inStream;
372
CSeqInStreamBuf seqBufInStream;
374
CSaveState saveState;
377
void LzmaEnc_SaveState(CLzmaEncHandle pp)
379
CLzmaEnc *p = (CLzmaEnc *)pp;
380
CSaveState *dest = &p->saveState;
382
dest->lenEnc = p->lenEnc;
383
dest->repLenEnc = p->repLenEnc;
384
dest->state = p->state;
386
for (i = 0; i < kNumStates; i++)
388
memcpy(dest->isMatch[i], p->isMatch[i], sizeof(p->isMatch[i]));
389
memcpy(dest->isRep0Long[i], p->isRep0Long[i], sizeof(p->isRep0Long[i]));
391
for (i = 0; i < kNumLenToPosStates; i++)
392
memcpy(dest->posSlotEncoder[i], p->posSlotEncoder[i], sizeof(p->posSlotEncoder[i]));
393
memcpy(dest->isRep, p->isRep, sizeof(p->isRep));
394
memcpy(dest->isRepG0, p->isRepG0, sizeof(p->isRepG0));
395
memcpy(dest->isRepG1, p->isRepG1, sizeof(p->isRepG1));
396
memcpy(dest->isRepG2, p->isRepG2, sizeof(p->isRepG2));
397
memcpy(dest->posEncoders, p->posEncoders, sizeof(p->posEncoders));
398
memcpy(dest->posAlignEncoder, p->posAlignEncoder, sizeof(p->posAlignEncoder));
399
memcpy(dest->reps, p->reps, sizeof(p->reps));
400
memcpy(dest->litProbs, p->litProbs, (0x300 << p->lclp) * sizeof(CLzmaProb));
403
void LzmaEnc_RestoreState(CLzmaEncHandle pp)
405
CLzmaEnc *dest = (CLzmaEnc *)pp;
406
const CSaveState *p = &dest->saveState;
408
dest->lenEnc = p->lenEnc;
409
dest->repLenEnc = p->repLenEnc;
410
dest->state = p->state;
412
for (i = 0; i < kNumStates; i++)
414
memcpy(dest->isMatch[i], p->isMatch[i], sizeof(p->isMatch[i]));
415
memcpy(dest->isRep0Long[i], p->isRep0Long[i], sizeof(p->isRep0Long[i]));
417
for (i = 0; i < kNumLenToPosStates; i++)
418
memcpy(dest->posSlotEncoder[i], p->posSlotEncoder[i], sizeof(p->posSlotEncoder[i]));
419
memcpy(dest->isRep, p->isRep, sizeof(p->isRep));
420
memcpy(dest->isRepG0, p->isRepG0, sizeof(p->isRepG0));
421
memcpy(dest->isRepG1, p->isRepG1, sizeof(p->isRepG1));
422
memcpy(dest->isRepG2, p->isRepG2, sizeof(p->isRepG2));
423
memcpy(dest->posEncoders, p->posEncoders, sizeof(p->posEncoders));
424
memcpy(dest->posAlignEncoder, p->posAlignEncoder, sizeof(p->posAlignEncoder));
425
memcpy(dest->reps, p->reps, sizeof(p->reps));
426
memcpy(dest->litProbs, p->litProbs, (0x300 << dest->lclp) * sizeof(CLzmaProb));
429
SRes LzmaEnc_SetProps(CLzmaEncHandle pp, const CLzmaEncProps *props2)
431
CLzmaEnc *p = (CLzmaEnc *)pp;
432
CLzmaEncProps props = *props2;
433
LzmaEncProps_Normalize(&props);
435
if (props.lc > LZMA_LC_MAX || props.lp > LZMA_LP_MAX || props.pb > LZMA_PB_MAX ||
436
props.dictSize > (1U << kDicLogSizeMaxCompress) || props.dictSize > (1 << 30))
437
return SZ_ERROR_PARAM;
438
p->dictSize = props.dictSize;
439
p->matchFinderCycles = props.mc;
441
unsigned fb = props.fb;
444
if (fb > LZMA_MATCH_LEN_MAX)
445
fb = LZMA_MATCH_LEN_MAX;
446
p->numFastBytes = fb;
451
p->fastMode = (props.algo == 0);
452
p->matchFinderBase.btMode = props.btMode;
454
UInt32 numHashBytes = 4;
457
if (props.numHashBytes < 2)
459
else if (props.numHashBytes < 4)
460
numHashBytes = props.numHashBytes;
462
p->matchFinderBase.numHashBytes = numHashBytes;
465
p->matchFinderBase.cutValue = props.mc;
467
p->writeEndMark = props.writeEndMark;
469
#ifdef COMPRESS_MF_MT
471
if (newMultiThread != _multiThread)
473
ReleaseMatchFinder();
474
_multiThread = newMultiThread;
477
p->multiThread = (props.numThreads > 1);
483
static const int kLiteralNextStates[kNumStates] = {0, 0, 0, 0, 1, 2, 3, 4, 5, 6, 4, 5};
484
static const int kMatchNextStates[kNumStates] = {7, 7, 7, 7, 7, 7, 7, 10, 10, 10, 10, 10};
485
static const int kRepNextStates[kNumStates] = {8, 8, 8, 8, 8, 8, 8, 11, 11, 11, 11, 11};
486
static const int kShortRepNextStates[kNumStates]= {9, 9, 9, 9, 9, 9, 9, 11, 11, 11, 11, 11};
489
void UpdateChar() { Index = kLiteralNextStates[Index]; }
490
void UpdateMatch() { Index = kMatchNextStates[Index]; }
491
void UpdateRep() { Index = kRepNextStates[Index]; }
492
void UpdateShortRep() { Index = kShortRepNextStates[Index]; }
495
#define IsCharState(s) ((s) < 7)
498
#define GetLenToPosState(len) (((len) < kNumLenToPosStates + 1) ? (len) - 2 : kNumLenToPosStates - 1)
500
#define kInfinityPrice (1 << 30)
502
static void RangeEnc_Construct(CRangeEnc *p)
508
#define RangeEnc_GetProcessed(p) ((p)->processed + ((p)->buf - (p)->bufBase) + (p)->cacheSize)
510
#define RC_BUF_SIZE (1 << 16)
511
static int RangeEnc_Alloc(CRangeEnc *p, ISzAlloc *alloc)
515
p->bufBase = (Byte *)alloc->Alloc(alloc, RC_BUF_SIZE);
518
p->bufLim = p->bufBase + RC_BUF_SIZE;
523
static void RangeEnc_Free(CRangeEnc *p, ISzAlloc *alloc)
525
alloc->Free(alloc, p->bufBase);
529
static void RangeEnc_Init(CRangeEnc *p)
533
p->range = 0xFFFFFFFF;
543
static void RangeEnc_FlushStream(CRangeEnc *p)
548
num = p->buf - p->bufBase;
549
if (num != p->outStream->Write(p->outStream, p->bufBase, num))
550
p->res = SZ_ERROR_WRITE;
555
static void MY_FAST_CALL RangeEnc_ShiftLow(CRangeEnc *p)
557
if ((UInt32)p->low < (UInt32)0xFF000000 || (int)(p->low >> 32) != 0)
559
Byte temp = p->cache;
563
*buf++ = (Byte)(temp + (Byte)(p->low >> 32));
565
if (buf == p->bufLim)
566
RangeEnc_FlushStream(p);
569
while (--p->cacheSize != 0);
570
p->cache = (Byte)((UInt32)p->low >> 24);
573
p->low = (UInt32)p->low << 8;
576
static void RangeEnc_FlushData(CRangeEnc *p)
579
for (i = 0; i < 5; i++)
580
RangeEnc_ShiftLow(p);
583
static void RangeEnc_EncodeDirectBits(CRangeEnc *p, UInt32 value, int numBits)
588
p->low += p->range & (0 - ((value >> --numBits) & 1));
589
if (p->range < kTopValue)
592
RangeEnc_ShiftLow(p);
595
while (numBits != 0);
598
static void RangeEnc_EncodeBit(CRangeEnc *p, CLzmaProb *prob, UInt32 symbol)
601
UInt32 newBound = (p->range >> kNumBitModelTotalBits) * ttt;
605
ttt += (kBitModelTotal - ttt) >> kNumMoveBits;
610
p->range -= newBound;
611
ttt -= ttt >> kNumMoveBits;
613
*prob = (CLzmaProb)ttt;
614
if (p->range < kTopValue)
617
RangeEnc_ShiftLow(p);
621
static void LitEnc_Encode(CRangeEnc *p, CLzmaProb *probs, UInt32 symbol)
626
RangeEnc_EncodeBit(p, probs + (symbol >> 8), (symbol >> 7) & 1);
629
while (symbol < 0x10000);
632
static void LitEnc_EncodeMatched(CRangeEnc *p, CLzmaProb *probs, UInt32 symbol, UInt32 matchByte)
639
RangeEnc_EncodeBit(p, probs + (offs + (matchByte & offs) + (symbol >> 8)), (symbol >> 7) & 1);
641
offs &= ~(matchByte ^ symbol);
643
while (symbol < 0x10000);
646
void LzmaEnc_InitPriceTables(UInt32 *ProbPrices)
649
for (i = (1 << kNumMoveReducingBits) / 2; i < kBitModelTotal; i += (1 << kNumMoveReducingBits))
651
const int kCyclesBits = kNumBitPriceShiftBits;
655
for (j = 0; j < kCyclesBits; j++)
659
while (w >= ((UInt32)1 << 16))
665
ProbPrices[i >> kNumMoveReducingBits] = ((kNumBitModelTotalBits << kCyclesBits) - 15 - bitCount);
670
#define GET_PRICE(prob, symbol) \
671
p->ProbPrices[((prob) ^ (((-(int)(symbol))) & (kBitModelTotal - 1))) >> kNumMoveReducingBits];
673
#define GET_PRICEa(prob, symbol) \
674
ProbPrices[((prob) ^ ((-((int)(symbol))) & (kBitModelTotal - 1))) >> kNumMoveReducingBits];
676
#define GET_PRICE_0(prob) p->ProbPrices[(prob) >> kNumMoveReducingBits]
677
#define GET_PRICE_1(prob) p->ProbPrices[((prob) ^ (kBitModelTotal - 1)) >> kNumMoveReducingBits]
679
#define GET_PRICE_0a(prob) ProbPrices[(prob) >> kNumMoveReducingBits]
680
#define GET_PRICE_1a(prob) ProbPrices[((prob) ^ (kBitModelTotal - 1)) >> kNumMoveReducingBits]
682
static UInt32 LitEnc_GetPrice(const CLzmaProb *probs, UInt32 symbol, UInt32 *ProbPrices)
688
price += GET_PRICEa(probs[symbol >> 8], (symbol >> 7) & 1);
691
while (symbol < 0x10000);
695
static UInt32 LitEnc_GetPriceMatched(const CLzmaProb *probs, UInt32 symbol, UInt32 matchByte, UInt32 *ProbPrices)
703
price += GET_PRICEa(probs[offs + (matchByte & offs) + (symbol >> 8)], (symbol >> 7) & 1);
705
offs &= ~(matchByte ^ symbol);
707
while (symbol < 0x10000);
712
static void RcTree_Encode(CRangeEnc *rc, CLzmaProb *probs, int numBitLevels, UInt32 symbol)
716
for (i = numBitLevels; i != 0 ;)
720
bit = (symbol >> i) & 1;
721
RangeEnc_EncodeBit(rc, probs + m, bit);
726
static void RcTree_ReverseEncode(CRangeEnc *rc, CLzmaProb *probs, int numBitLevels, UInt32 symbol)
730
for (i = 0; i < numBitLevels; i++)
732
UInt32 bit = symbol & 1;
733
RangeEnc_EncodeBit(rc, probs + m, bit);
739
static UInt32 RcTree_GetPrice(const CLzmaProb *probs, int numBitLevels, UInt32 symbol, UInt32 *ProbPrices)
742
symbol |= (1 << numBitLevels);
745
price += GET_PRICEa(probs[symbol >> 1], symbol & 1);
751
static UInt32 RcTree_ReverseGetPrice(const CLzmaProb *probs, int numBitLevels, UInt32 symbol, UInt32 *ProbPrices)
756
for (i = numBitLevels; i != 0; i--)
758
UInt32 bit = symbol & 1;
760
price += GET_PRICEa(probs[m], bit);
767
static void LenEnc_Init(CLenEnc *p)
770
p->choice = p->choice2 = kProbInitValue;
771
for (i = 0; i < (LZMA_NUM_PB_STATES_MAX << kLenNumLowBits); i++)
772
p->low[i] = kProbInitValue;
773
for (i = 0; i < (LZMA_NUM_PB_STATES_MAX << kLenNumMidBits); i++)
774
p->mid[i] = kProbInitValue;
775
for (i = 0; i < kLenNumHighSymbols; i++)
776
p->high[i] = kProbInitValue;
779
static void LenEnc_Encode(CLenEnc *p, CRangeEnc *rc, UInt32 symbol, UInt32 posState)
781
if (symbol < kLenNumLowSymbols)
783
RangeEnc_EncodeBit(rc, &p->choice, 0);
784
RcTree_Encode(rc, p->low + (posState << kLenNumLowBits), kLenNumLowBits, symbol);
788
RangeEnc_EncodeBit(rc, &p->choice, 1);
789
if (symbol < kLenNumLowSymbols + kLenNumMidSymbols)
791
RangeEnc_EncodeBit(rc, &p->choice2, 0);
792
RcTree_Encode(rc, p->mid + (posState << kLenNumMidBits), kLenNumMidBits, symbol - kLenNumLowSymbols);
796
RangeEnc_EncodeBit(rc, &p->choice2, 1);
797
RcTree_Encode(rc, p->high, kLenNumHighBits, symbol - kLenNumLowSymbols - kLenNumMidSymbols);
802
static void LenEnc_SetPrices(CLenEnc *p, UInt32 posState, UInt32 numSymbols, UInt32 *prices, UInt32 *ProbPrices)
804
UInt32 a0 = GET_PRICE_0a(p->choice);
805
UInt32 a1 = GET_PRICE_1a(p->choice);
806
UInt32 b0 = a1 + GET_PRICE_0a(p->choice2);
807
UInt32 b1 = a1 + GET_PRICE_1a(p->choice2);
809
for (i = 0; i < kLenNumLowSymbols; i++)
813
prices[i] = a0 + RcTree_GetPrice(p->low + (posState << kLenNumLowBits), kLenNumLowBits, i, ProbPrices);
815
for (; i < kLenNumLowSymbols + kLenNumMidSymbols; i++)
819
prices[i] = b0 + RcTree_GetPrice(p->mid + (posState << kLenNumMidBits), kLenNumMidBits, i - kLenNumLowSymbols, ProbPrices);
821
for (; i < numSymbols; i++)
822
prices[i] = b1 + RcTree_GetPrice(p->high, kLenNumHighBits, i - kLenNumLowSymbols - kLenNumMidSymbols, ProbPrices);
825
static void MY_FAST_CALL LenPriceEnc_UpdateTable(CLenPriceEnc *p, UInt32 posState, UInt32 *ProbPrices)
827
LenEnc_SetPrices(&p->p, posState, p->tableSize, p->prices[posState], ProbPrices);
828
p->counters[posState] = p->tableSize;
831
static void LenPriceEnc_UpdateTables(CLenPriceEnc *p, UInt32 numPosStates, UInt32 *ProbPrices)
834
for (posState = 0; posState < numPosStates; posState++)
835
LenPriceEnc_UpdateTable(p, posState, ProbPrices);
838
static void LenEnc_Encode2(CLenPriceEnc *p, CRangeEnc *rc, UInt32 symbol, UInt32 posState, Bool updatePrice, UInt32 *ProbPrices)
840
LenEnc_Encode(&p->p, rc, symbol, posState);
842
if (--p->counters[posState] == 0)
843
LenPriceEnc_UpdateTable(p, posState, ProbPrices);
849
static void MovePos(CLzmaEnc *p, UInt32 num)
853
printf("\n MovePos %d", num);
857
p->additionalOffset += num;
858
p->matchFinder.Skip(p->matchFinderObj, num);
862
static UInt32 ReadMatchDistances(CLzmaEnc *p, UInt32 *numDistancePairsRes)
864
UInt32 lenRes = 0, numDistancePairs;
865
numDistancePairs = p->matchFinder.GetMatches(p->matchFinderObj, p->matchDistances);
867
printf("\n i = %d numPairs = %d ", ttt, numDistancePairs / 2);
874
for (i = 0; i < numDistancePairs; i += 2)
875
printf("%2d %6d | ", p->matchDistances[i], p->matchDistances[i + 1]);
878
if (numDistancePairs > 0)
880
lenRes = p->matchDistances[numDistancePairs - 2];
881
if (lenRes == p->numFastBytes)
883
UInt32 numAvail = p->matchFinder.GetNumAvailableBytes(p->matchFinderObj) + 1;
884
const Byte *pby = p->matchFinder.GetPointerToCurrentPos(p->matchFinderObj) - 1;
885
UInt32 distance = p->matchDistances[numDistancePairs - 1] + 1;
886
if (numAvail > LZMA_MATCH_LEN_MAX)
887
numAvail = LZMA_MATCH_LEN_MAX;
890
const Byte *pby2 = pby - distance;
891
for (; lenRes < numAvail && pby[lenRes] == pby2[lenRes]; lenRes++);
895
p->additionalOffset++;
896
*numDistancePairsRes = numDistancePairs;
901
#define MakeAsChar(p) (p)->backPrev = (UInt32)(-1); (p)->prev1IsChar = False;
902
#define MakeAsShortRep(p) (p)->backPrev = 0; (p)->prev1IsChar = False;
903
#define IsShortRep(p) ((p)->backPrev == 0)
905
static UInt32 GetRepLen1Price(CLzmaEnc *p, UInt32 state, UInt32 posState)
908
GET_PRICE_0(p->isRepG0[state]) +
909
GET_PRICE_0(p->isRep0Long[state][posState]);
912
static UInt32 GetPureRepPrice(CLzmaEnc *p, UInt32 repIndex, UInt32 state, UInt32 posState)
917
price = GET_PRICE_0(p->isRepG0[state]);
918
price += GET_PRICE_1(p->isRep0Long[state][posState]);
922
price = GET_PRICE_1(p->isRepG0[state]);
924
price += GET_PRICE_0(p->isRepG1[state]);
927
price += GET_PRICE_1(p->isRepG1[state]);
928
price += GET_PRICE(p->isRepG2[state], repIndex - 2);
934
static UInt32 GetRepPrice(CLzmaEnc *p, UInt32 repIndex, UInt32 len, UInt32 state, UInt32 posState)
936
return p->repLenEnc.prices[posState][len - LZMA_MATCH_LEN_MIN] +
937
GetPureRepPrice(p, repIndex, state, posState);
940
static UInt32 Backward(CLzmaEnc *p, UInt32 *backRes, UInt32 cur)
942
UInt32 posMem = p->opt[cur].posPrev;
943
UInt32 backMem = p->opt[cur].backPrev;
944
p->optimumEndIndex = cur;
947
if (p->opt[cur].prev1IsChar)
949
MakeAsChar(&p->opt[posMem])
950
p->opt[posMem].posPrev = posMem - 1;
951
if (p->opt[cur].prev2)
953
p->opt[posMem - 1].prev1IsChar = False;
954
p->opt[posMem - 1].posPrev = p->opt[cur].posPrev2;
955
p->opt[posMem - 1].backPrev = p->opt[cur].backPrev2;
959
UInt32 posPrev = posMem;
960
UInt32 backCur = backMem;
962
backMem = p->opt[posPrev].backPrev;
963
posMem = p->opt[posPrev].posPrev;
965
p->opt[posPrev].backPrev = backCur;
966
p->opt[posPrev].posPrev = cur;
971
*backRes = p->opt[0].backPrev;
972
p->optimumCurrentIndex = p->opt[0].posPrev;
973
return p->optimumCurrentIndex;
976
#define LIT_PROBS(pos, prevByte) (p->litProbs + ((((pos) & p->lpMask) << p->lc) + ((prevByte) >> (8 - p->lc))) * 0x300)
978
static UInt32 GetOptimum(CLzmaEnc *p, UInt32 position, UInt32 *backRes)
980
UInt32 numAvailableBytes, lenMain, numDistancePairs;
982
UInt32 reps[LZMA_NUM_REPS];
983
UInt32 repLens[LZMA_NUM_REPS];
984
UInt32 repMaxIndex, i;
985
UInt32 *matchDistances;
986
Byte currentByte, matchByte;
988
UInt32 matchPrice, repMatchPrice;
991
UInt32 normalMatchPrice;
993
if (p->optimumEndIndex != p->optimumCurrentIndex)
995
const COptimal *opt = &p->opt[p->optimumCurrentIndex];
996
UInt32 lenRes = opt->posPrev - p->optimumCurrentIndex;
997
*backRes = opt->backPrev;
998
p->optimumCurrentIndex = opt->posPrev;
1001
p->optimumCurrentIndex = p->optimumEndIndex = 0;
1003
numAvailableBytes = p->matchFinder.GetNumAvailableBytes(p->matchFinderObj);
1005
if (!p->longestMatchWasFound)
1007
lenMain = ReadMatchDistances(p, &numDistancePairs);
1011
lenMain = p->longestMatchLength;
1012
numDistancePairs = p->numDistancePairs;
1013
p->longestMatchWasFound = False;
1016
data = p->matchFinder.GetPointerToCurrentPos(p->matchFinderObj) - 1;
1017
if (numAvailableBytes < 2)
1019
*backRes = (UInt32)(-1);
1022
if (numAvailableBytes > LZMA_MATCH_LEN_MAX)
1023
numAvailableBytes = LZMA_MATCH_LEN_MAX;
1026
for (i = 0; i < LZMA_NUM_REPS; i++)
1030
reps[i] = p->reps[i];
1031
data2 = data - (reps[i] + 1);
1032
if (data[0] != data2[0] || data[1] != data2[1])
1037
for (lenTest = 2; lenTest < numAvailableBytes && data[lenTest] == data2[lenTest]; lenTest++);
1038
repLens[i] = lenTest;
1039
if (lenTest > repLens[repMaxIndex])
1042
if (repLens[repMaxIndex] >= p->numFastBytes)
1045
*backRes = repMaxIndex;
1046
lenRes = repLens[repMaxIndex];
1047
MovePos(p, lenRes - 1);
1051
matchDistances = p->matchDistances;
1052
if (lenMain >= p->numFastBytes)
1054
*backRes = matchDistances[numDistancePairs - 1] + LZMA_NUM_REPS;
1055
MovePos(p, lenMain - 1);
1058
currentByte = *data;
1059
matchByte = *(data - (reps[0] + 1));
1061
if (lenMain < 2 && currentByte != matchByte && repLens[repMaxIndex] < 2)
1063
*backRes = (UInt32)-1;
1067
p->opt[0].state = (CState)p->state;
1069
posState = (position & p->pbMask);
1072
const CLzmaProb *probs = LIT_PROBS(position, *(data - 1));
1073
p->opt[1].price = GET_PRICE_0(p->isMatch[p->state][posState]) +
1074
(!IsCharState(p->state) ?
1075
LitEnc_GetPriceMatched(probs, currentByte, matchByte, p->ProbPrices) :
1076
LitEnc_GetPrice(probs, currentByte, p->ProbPrices));
1079
MakeAsChar(&p->opt[1]);
1081
matchPrice = GET_PRICE_1(p->isMatch[p->state][posState]);
1082
repMatchPrice = matchPrice + GET_PRICE_1(p->isRep[p->state]);
1084
if (matchByte == currentByte)
1086
UInt32 shortRepPrice = repMatchPrice + GetRepLen1Price(p, p->state, posState);
1087
if (shortRepPrice < p->opt[1].price)
1089
p->opt[1].price = shortRepPrice;
1090
MakeAsShortRep(&p->opt[1]);
1093
lenEnd = ((lenMain >= repLens[repMaxIndex]) ? lenMain : repLens[repMaxIndex]);
1097
*backRes = p->opt[1].backPrev;
1101
p->opt[1].posPrev = 0;
1102
for (i = 0; i < LZMA_NUM_REPS; i++)
1103
p->opt[0].backs[i] = reps[i];
1107
p->opt[len--].price = kInfinityPrice;
1110
for (i = 0; i < LZMA_NUM_REPS; i++)
1112
UInt32 repLen = repLens[i];
1116
price = repMatchPrice + GetPureRepPrice(p, i, p->state, posState);
1119
UInt32 curAndLenPrice = price + p->repLenEnc.prices[posState][repLen - 2];
1120
COptimal *opt = &p->opt[repLen];
1121
if (curAndLenPrice < opt->price)
1123
opt->price = curAndLenPrice;
1126
opt->prev1IsChar = False;
1129
while (--repLen >= 2);
1132
normalMatchPrice = matchPrice + GET_PRICE_0(p->isRep[p->state]);
1134
len = ((repLens[0] >= 2) ? repLens[0] + 1 : 2);
1138
while (len > matchDistances[offs])
1143
UInt32 distance = matchDistances[offs + 1];
1145
UInt32 curAndLenPrice = normalMatchPrice + p->lenEnc.prices[posState][len - LZMA_MATCH_LEN_MIN];
1146
UInt32 lenToPosState = GetLenToPosState(len);
1147
if (distance < kNumFullDistances)
1148
curAndLenPrice += p->distancesPrices[lenToPosState][distance];
1152
GetPosSlot2(distance, slot);
1153
curAndLenPrice += p->alignPrices[distance & kAlignMask] + p->posSlotPrices[lenToPosState][slot];
1156
if (curAndLenPrice < opt->price)
1158
opt->price = curAndLenPrice;
1160
opt->backPrev = distance + LZMA_NUM_REPS;
1161
opt->prev1IsChar = False;
1163
if (len == matchDistances[offs])
1166
if (offs == numDistancePairs)
1178
printf("\n pos = %4X", position);
1179
for (i = cur; i <= lenEnd; i++)
1180
printf("\nprice[%4X] = %d", position - cur + i, p->opt[i].price);
1186
UInt32 numAvailableBytesFull, newLen, numDistancePairs;
1193
Byte currentByte, matchByte;
1195
UInt32 curAnd1Price;
1197
UInt32 matchPrice, repMatchPrice;
1198
UInt32 numAvailableBytes;
1203
return Backward(p, backRes, cur);
1205
numAvailableBytesFull = p->matchFinder.GetNumAvailableBytes(p->matchFinderObj);
1206
newLen = ReadMatchDistances(p, &numDistancePairs);
1207
if (newLen >= p->numFastBytes)
1209
p->numDistancePairs = numDistancePairs;
1210
p->longestMatchLength = newLen;
1211
p->longestMatchWasFound = True;
1212
return Backward(p, backRes, cur);
1215
curOpt = &p->opt[cur];
1216
posPrev = curOpt->posPrev;
1217
if (curOpt->prev1IsChar)
1222
state = p->opt[curOpt->posPrev2].state;
1223
if (curOpt->backPrev2 < LZMA_NUM_REPS)
1224
state = kRepNextStates[state];
1226
state = kMatchNextStates[state];
1229
state = p->opt[posPrev].state;
1230
state = kLiteralNextStates[state];
1233
state = p->opt[posPrev].state;
1234
if (posPrev == cur - 1)
1236
if (IsShortRep(curOpt))
1237
state = kShortRepNextStates[state];
1239
state = kLiteralNextStates[state];
1244
const COptimal *prevOpt;
1245
if (curOpt->prev1IsChar && curOpt->prev2)
1247
posPrev = curOpt->posPrev2;
1248
pos = curOpt->backPrev2;
1249
state = kRepNextStates[state];
1253
pos = curOpt->backPrev;
1254
if (pos < LZMA_NUM_REPS)
1255
state = kRepNextStates[state];
1257
state = kMatchNextStates[state];
1259
prevOpt = &p->opt[posPrev];
1260
if (pos < LZMA_NUM_REPS)
1263
reps[0] = prevOpt->backs[pos];
1264
for (i = 1; i <= pos; i++)
1265
reps[i] = prevOpt->backs[i - 1];
1266
for (; i < LZMA_NUM_REPS; i++)
1267
reps[i] = prevOpt->backs[i];
1272
reps[0] = (pos - LZMA_NUM_REPS);
1273
for (i = 1; i < LZMA_NUM_REPS; i++)
1274
reps[i] = prevOpt->backs[i - 1];
1277
curOpt->state = (CState)state;
1279
curOpt->backs[0] = reps[0];
1280
curOpt->backs[1] = reps[1];
1281
curOpt->backs[2] = reps[2];
1282
curOpt->backs[3] = reps[3];
1284
curPrice = curOpt->price;
1286
data = p->matchFinder.GetPointerToCurrentPos(p->matchFinderObj) - 1;
1287
currentByte = *data;
1288
matchByte = *(data - (reps[0] + 1));
1290
posState = (position & p->pbMask);
1292
curAnd1Price = curPrice + GET_PRICE_0(p->isMatch[state][posState]);
1294
const CLzmaProb *probs = LIT_PROBS(position, *(data - 1));
1296
(!IsCharState(state) ?
1297
LitEnc_GetPriceMatched(probs, currentByte, matchByte, p->ProbPrices) :
1298
LitEnc_GetPrice(probs, currentByte, p->ProbPrices));
1301
nextOpt = &p->opt[cur + 1];
1303
if (curAnd1Price < nextOpt->price)
1305
nextOpt->price = curAnd1Price;
1306
nextOpt->posPrev = cur;
1307
MakeAsChar(nextOpt);
1311
matchPrice = curPrice + GET_PRICE_1(p->isMatch[state][posState]);
1312
repMatchPrice = matchPrice + GET_PRICE_1(p->isRep[state]);
1314
if (matchByte == currentByte && !(nextOpt->posPrev < cur && nextOpt->backPrev == 0))
1316
UInt32 shortRepPrice = repMatchPrice + GetRepLen1Price(p, state, posState);
1317
if (shortRepPrice <= nextOpt->price)
1319
nextOpt->price = shortRepPrice;
1320
nextOpt->posPrev = cur;
1321
MakeAsShortRep(nextOpt);
1327
UInt32 temp = kNumOpts - 1 - cur;
1328
if (temp < numAvailableBytesFull)
1329
numAvailableBytesFull = temp;
1331
numAvailableBytes = numAvailableBytesFull;
1333
if (numAvailableBytes < 2)
1335
if (numAvailableBytes > p->numFastBytes)
1336
numAvailableBytes = p->numFastBytes;
1337
if (!nextIsChar && matchByte != currentByte) /* speed optimization */
1339
/* try Literal + rep0 */
1342
const Byte *data2 = data - (reps[0] + 1);
1343
UInt32 limit = p->numFastBytes + 1;
1344
if (limit > numAvailableBytesFull)
1345
limit = numAvailableBytesFull;
1347
for (temp = 1; temp < limit && data[temp] == data2[temp]; temp++);
1348
lenTest2 = temp - 1;
1351
UInt32 state2 = kLiteralNextStates[state];
1352
UInt32 posStateNext = (position + 1) & p->pbMask;
1353
UInt32 nextRepMatchPrice = curAnd1Price +
1354
GET_PRICE_1(p->isMatch[state2][posStateNext]) +
1355
GET_PRICE_1(p->isRep[state2]);
1356
/* for (; lenTest2 >= 2; lenTest2--) */
1358
UInt32 curAndLenPrice;
1360
UInt32 offset = cur + 1 + lenTest2;
1361
while (lenEnd < offset)
1362
p->opt[++lenEnd].price = kInfinityPrice;
1363
curAndLenPrice = nextRepMatchPrice + GetRepPrice(p, 0, lenTest2, state2, posStateNext);
1364
opt = &p->opt[offset];
1365
if (curAndLenPrice < opt->price)
1367
opt->price = curAndLenPrice;
1368
opt->posPrev = cur + 1;
1370
opt->prev1IsChar = True;
1377
startLen = 2; /* speed optimization */
1380
for (repIndex = 0; repIndex < LZMA_NUM_REPS; repIndex++)
1385
const Byte *data2 = data - (reps[repIndex] + 1);
1386
if (data[0] != data2[0] || data[1] != data2[1])
1388
for (lenTest = 2; lenTest < numAvailableBytes && data[lenTest] == data2[lenTest]; lenTest++);
1389
while (lenEnd < cur + lenTest)
1390
p->opt[++lenEnd].price = kInfinityPrice;
1391
lenTestTemp = lenTest;
1392
price = repMatchPrice + GetPureRepPrice(p, repIndex, state, posState);
1395
UInt32 curAndLenPrice = price + p->repLenEnc.prices[posState][lenTest - 2];
1396
COptimal *opt = &p->opt[cur + lenTest];
1397
if (curAndLenPrice < opt->price)
1399
opt->price = curAndLenPrice;
1401
opt->backPrev = repIndex;
1402
opt->prev1IsChar = False;
1405
while (--lenTest >= 2);
1406
lenTest = lenTestTemp;
1409
startLen = lenTest + 1;
1413
UInt32 lenTest2 = lenTest + 1;
1414
UInt32 limit = lenTest2 + p->numFastBytes;
1415
UInt32 nextRepMatchPrice;
1416
if (limit > numAvailableBytesFull)
1417
limit = numAvailableBytesFull;
1418
for (; lenTest2 < limit && data[lenTest2] == data2[lenTest2]; lenTest2++);
1419
lenTest2 -= lenTest + 1;
1422
UInt32 state2 = kRepNextStates[state];
1423
UInt32 posStateNext = (position + lenTest) & p->pbMask;
1424
UInt32 curAndLenCharPrice =
1425
price + p->repLenEnc.prices[posState][lenTest - 2] +
1426
GET_PRICE_0(p->isMatch[state2][posStateNext]) +
1427
LitEnc_GetPriceMatched(LIT_PROBS(position + lenTest, data[lenTest - 1]),
1428
data[lenTest], data2[lenTest], p->ProbPrices);
1429
state2 = kLiteralNextStates[state2];
1430
posStateNext = (position + lenTest + 1) & p->pbMask;
1431
nextRepMatchPrice = curAndLenCharPrice +
1432
GET_PRICE_1(p->isMatch[state2][posStateNext]) +
1433
GET_PRICE_1(p->isRep[state2]);
1435
/* for (; lenTest2 >= 2; lenTest2--) */
1437
UInt32 curAndLenPrice;
1439
UInt32 offset = cur + lenTest + 1 + lenTest2;
1440
while (lenEnd < offset)
1441
p->opt[++lenEnd].price = kInfinityPrice;
1442
curAndLenPrice = nextRepMatchPrice + GetRepPrice(p, 0, lenTest2, state2, posStateNext);
1443
opt = &p->opt[offset];
1444
if (curAndLenPrice < opt->price)
1446
opt->price = curAndLenPrice;
1447
opt->posPrev = cur + lenTest + 1;
1449
opt->prev1IsChar = True;
1451
opt->posPrev2 = cur;
1452
opt->backPrev2 = repIndex;
1459
/* for (UInt32 lenTest = 2; lenTest <= newLen; lenTest++) */
1460
if (newLen > numAvailableBytes)
1462
newLen = numAvailableBytes;
1463
for (numDistancePairs = 0; newLen > matchDistances[numDistancePairs]; numDistancePairs += 2);
1464
matchDistances[numDistancePairs] = newLen;
1465
numDistancePairs += 2;
1467
if (newLen >= startLen)
1469
UInt32 normalMatchPrice = matchPrice + GET_PRICE_0(p->isRep[state]);
1470
UInt32 offs, curBack, posSlot;
1472
while (lenEnd < cur + newLen)
1473
p->opt[++lenEnd].price = kInfinityPrice;
1476
while (startLen > matchDistances[offs])
1478
curBack = matchDistances[offs + 1];
1479
GetPosSlot2(curBack, posSlot);
1480
for (lenTest = /*2*/ startLen; ; lenTest++)
1482
UInt32 curAndLenPrice = normalMatchPrice + p->lenEnc.prices[posState][lenTest - LZMA_MATCH_LEN_MIN];
1483
UInt32 lenToPosState = GetLenToPosState(lenTest);
1485
if (curBack < kNumFullDistances)
1486
curAndLenPrice += p->distancesPrices[lenToPosState][curBack];
1488
curAndLenPrice += p->posSlotPrices[lenToPosState][posSlot] + p->alignPrices[curBack & kAlignMask];
1490
opt = &p->opt[cur + lenTest];
1491
if (curAndLenPrice < opt->price)
1493
opt->price = curAndLenPrice;
1495
opt->backPrev = curBack + LZMA_NUM_REPS;
1496
opt->prev1IsChar = False;
1499
if (/*_maxMode && */lenTest == matchDistances[offs])
1501
/* Try Match + Literal + Rep0 */
1502
const Byte *data2 = data - (curBack + 1);
1503
UInt32 lenTest2 = lenTest + 1;
1504
UInt32 limit = lenTest2 + p->numFastBytes;
1505
UInt32 nextRepMatchPrice;
1506
if (limit > numAvailableBytesFull)
1507
limit = numAvailableBytesFull;
1508
for (; lenTest2 < limit && data[lenTest2] == data2[lenTest2]; lenTest2++);
1509
lenTest2 -= lenTest + 1;
1512
UInt32 state2 = kMatchNextStates[state];
1513
UInt32 posStateNext = (position + lenTest) & p->pbMask;
1514
UInt32 curAndLenCharPrice = curAndLenPrice +
1515
GET_PRICE_0(p->isMatch[state2][posStateNext]) +
1516
LitEnc_GetPriceMatched(LIT_PROBS(position + lenTest, data[lenTest - 1]),
1517
data[lenTest], data2[lenTest], p->ProbPrices);
1518
state2 = kLiteralNextStates[state2];
1519
posStateNext = (posStateNext + 1) & p->pbMask;
1520
nextRepMatchPrice = curAndLenCharPrice +
1521
GET_PRICE_1(p->isMatch[state2][posStateNext]) +
1522
GET_PRICE_1(p->isRep[state2]);
1524
/* for (; lenTest2 >= 2; lenTest2--) */
1526
UInt32 offset = cur + lenTest + 1 + lenTest2;
1527
UInt32 curAndLenPrice;
1529
while (lenEnd < offset)
1530
p->opt[++lenEnd].price = kInfinityPrice;
1531
curAndLenPrice = nextRepMatchPrice + GetRepPrice(p, 0, lenTest2, state2, posStateNext);
1532
opt = &p->opt[offset];
1533
if (curAndLenPrice < opt->price)
1535
opt->price = curAndLenPrice;
1536
opt->posPrev = cur + lenTest + 1;
1538
opt->prev1IsChar = True;
1540
opt->posPrev2 = cur;
1541
opt->backPrev2 = curBack + LZMA_NUM_REPS;
1546
if (offs == numDistancePairs)
1548
curBack = matchDistances[offs + 1];
1549
if (curBack >= kNumFullDistances)
1550
GetPosSlot2(curBack, posSlot);
1557
#define ChangePair(smallDist, bigDist) (((bigDist) >> 7) > (smallDist))
1559
static UInt32 GetOptimumFast(CLzmaEnc *p, UInt32 *backRes)
1561
UInt32 numAvailableBytes = p->matchFinder.GetNumAvailableBytes(p->matchFinderObj);
1562
UInt32 lenMain, numDistancePairs;
1564
UInt32 repLens[LZMA_NUM_REPS];
1565
UInt32 repMaxIndex, i;
1566
UInt32 *matchDistances;
1569
if (!p->longestMatchWasFound)
1571
lenMain = ReadMatchDistances(p, &numDistancePairs);
1575
lenMain = p->longestMatchLength;
1576
numDistancePairs = p->numDistancePairs;
1577
p->longestMatchWasFound = False;
1580
data = p->matchFinder.GetPointerToCurrentPos(p->matchFinderObj) - 1;
1581
if (numAvailableBytes > LZMA_MATCH_LEN_MAX)
1582
numAvailableBytes = LZMA_MATCH_LEN_MAX;
1583
if (numAvailableBytes < 2)
1585
*backRes = (UInt32)(-1);
1591
for (i = 0; i < LZMA_NUM_REPS; i++)
1593
const Byte *data2 = data - (p->reps[i] + 1);
1595
if (data[0] != data2[0] || data[1] != data2[1])
1600
for (len = 2; len < numAvailableBytes && data[len] == data2[len]; len++);
1601
if (len >= p->numFastBytes)
1604
MovePos(p, len - 1);
1608
if (len > repLens[repMaxIndex])
1611
matchDistances = p->matchDistances;
1612
if (lenMain >= p->numFastBytes)
1614
*backRes = matchDistances[numDistancePairs - 1] + LZMA_NUM_REPS;
1615
MovePos(p, lenMain - 1);
1619
backMain = 0; /* for GCC */
1622
backMain = matchDistances[numDistancePairs - 1];
1623
while (numDistancePairs > 2 && lenMain == matchDistances[numDistancePairs - 4] + 1)
1625
if (!ChangePair(matchDistances[numDistancePairs - 3], backMain))
1627
numDistancePairs -= 2;
1628
lenMain = matchDistances[numDistancePairs - 2];
1629
backMain = matchDistances[numDistancePairs - 1];
1631
if (lenMain == 2 && backMain >= 0x80)
1635
if (repLens[repMaxIndex] >= 2)
1637
if (repLens[repMaxIndex] + 1 >= lenMain ||
1638
(repLens[repMaxIndex] + 2 >= lenMain && (backMain > (1 << 9))) ||
1639
(repLens[repMaxIndex] + 3 >= lenMain && (backMain > (1 << 15))))
1642
*backRes = repMaxIndex;
1643
lenRes = repLens[repMaxIndex];
1644
MovePos(p, lenRes - 1);
1649
if (lenMain >= 2 && numAvailableBytes > 2)
1652
numAvailableBytes = p->matchFinder.GetNumAvailableBytes(p->matchFinderObj);
1653
p->longestMatchLength = ReadMatchDistances(p, &p->numDistancePairs);
1654
if (p->longestMatchLength >= 2)
1656
UInt32 newDistance = matchDistances[p->numDistancePairs - 1];
1657
if ((p->longestMatchLength >= lenMain && newDistance < backMain) ||
1658
(p->longestMatchLength == lenMain + 1 && !ChangePair(backMain, newDistance)) ||
1659
(p->longestMatchLength > lenMain + 1) ||
1660
(p->longestMatchLength + 1 >= lenMain && lenMain >= 3 && ChangePair(newDistance, backMain)))
1662
p->longestMatchWasFound = True;
1663
*backRes = (UInt32)(-1);
1667
data = p->matchFinder.GetPointerToCurrentPos(p->matchFinderObj) - 1;
1668
for (i = 0; i < LZMA_NUM_REPS; i++)
1671
const Byte *data2 = data - (p->reps[i] + 1);
1672
if (data[1] != data2[1] || data[2] != data2[2])
1677
for (len = 2; len < numAvailableBytes && data[len] == data2[len]; len++);
1678
if (len + 1 >= lenMain)
1680
p->longestMatchWasFound = True;
1681
*backRes = (UInt32)(-1);
1685
*backRes = backMain + LZMA_NUM_REPS;
1686
MovePos(p, lenMain - 2);
1689
*backRes = (UInt32)(-1);
1693
static void WriteEndMarker(CLzmaEnc *p, UInt32 posState)
1696
RangeEnc_EncodeBit(&p->rc, &p->isMatch[p->state][posState], 1);
1697
RangeEnc_EncodeBit(&p->rc, &p->isRep[p->state], 0);
1698
p->state = kMatchNextStates[p->state];
1699
len = LZMA_MATCH_LEN_MIN;
1700
LenEnc_Encode2(&p->lenEnc, &p->rc, len - LZMA_MATCH_LEN_MIN, posState, !p->fastMode, p->ProbPrices);
1701
RcTree_Encode(&p->rc, p->posSlotEncoder[GetLenToPosState(len)], kNumPosSlotBits, (1 << kNumPosSlotBits) - 1);
1702
RangeEnc_EncodeDirectBits(&p->rc, (((UInt32)1 << 30) - 1) >> kNumAlignBits, 30 - kNumAlignBits);
1703
RcTree_ReverseEncode(&p->rc, p->posAlignEncoder, kNumAlignBits, kAlignMask);
1706
static SRes CheckErrors(CLzmaEnc *p)
1708
if (p->result != SZ_OK)
1710
if (p->rc.res != SZ_OK)
1711
p->result = SZ_ERROR_WRITE;
1712
if (p->matchFinderBase.result != SZ_OK)
1713
p->result = SZ_ERROR_READ;
1714
if (p->result != SZ_OK)
1719
static SRes Flush(CLzmaEnc *p, UInt32 nowPos)
1721
/* ReleaseMFStream(); */
1723
if (p->writeEndMark)
1724
WriteEndMarker(p, nowPos & p->pbMask);
1725
RangeEnc_FlushData(&p->rc);
1726
RangeEnc_FlushStream(&p->rc);
1727
return CheckErrors(p);
1730
static void FillAlignPrices(CLzmaEnc *p)
1733
for (i = 0; i < kAlignTableSize; i++)
1734
p->alignPrices[i] = RcTree_ReverseGetPrice(p->posAlignEncoder, kNumAlignBits, i, p->ProbPrices);
1735
p->alignPriceCount = 0;
1738
static void FillDistancesPrices(CLzmaEnc *p)
1740
UInt32 tempPrices[kNumFullDistances];
1741
UInt32 i, lenToPosState;
1742
for (i = kStartPosModelIndex; i < kNumFullDistances; i++)
1744
UInt32 posSlot = GetPosSlot1(i);
1745
UInt32 footerBits = ((posSlot >> 1) - 1);
1746
UInt32 base = ((2 | (posSlot & 1)) << footerBits);
1747
tempPrices[i] = RcTree_ReverseGetPrice(p->posEncoders + base - posSlot - 1, footerBits, i - base, p->ProbPrices);
1750
for (lenToPosState = 0; lenToPosState < kNumLenToPosStates; lenToPosState++)
1753
const CLzmaProb *encoder = p->posSlotEncoder[lenToPosState];
1754
UInt32 *posSlotPrices = p->posSlotPrices[lenToPosState];
1755
for (posSlot = 0; posSlot < p->distTableSize; posSlot++)
1756
posSlotPrices[posSlot] = RcTree_GetPrice(encoder, kNumPosSlotBits, posSlot, p->ProbPrices);
1757
for (posSlot = kEndPosModelIndex; posSlot < p->distTableSize; posSlot++)
1758
posSlotPrices[posSlot] += ((((posSlot >> 1) - 1) - kNumAlignBits) << kNumBitPriceShiftBits);
1761
UInt32 *distancesPrices = p->distancesPrices[lenToPosState];
1763
for (i = 0; i < kStartPosModelIndex; i++)
1764
distancesPrices[i] = posSlotPrices[i];
1765
for (; i < kNumFullDistances; i++)
1766
distancesPrices[i] = posSlotPrices[GetPosSlot1(i)] + tempPrices[i];
1769
p->matchPriceCount = 0;
1772
void LzmaEnc_Construct(CLzmaEnc *p)
1774
RangeEnc_Construct(&p->rc);
1775
MatchFinder_Construct(&p->matchFinderBase);
1776
#ifdef COMPRESS_MF_MT
1777
MatchFinderMt_Construct(&p->matchFinderMt);
1778
p->matchFinderMt.MatchFinder = &p->matchFinderBase;
1782
CLzmaEncProps props;
1783
LzmaEncProps_Init(&props);
1784
LzmaEnc_SetProps(p, &props);
1787
#ifndef LZMA_LOG_BSR
1788
LzmaEnc_FastPosInit(p->g_FastPos);
1791
LzmaEnc_InitPriceTables(p->ProbPrices);
1793
p->saveState.litProbs = 0;
1796
CLzmaEncHandle LzmaEnc_Create(ISzAlloc *alloc)
1799
p = alloc->Alloc(alloc, sizeof(CLzmaEnc));
1801
LzmaEnc_Construct((CLzmaEnc *)p);
1805
void LzmaEnc_FreeLits(CLzmaEnc *p, ISzAlloc *alloc)
1807
alloc->Free(alloc, p->litProbs);
1808
alloc->Free(alloc, p->saveState.litProbs);
1810
p->saveState.litProbs = 0;
1813
void LzmaEnc_Destruct(CLzmaEnc *p, ISzAlloc *alloc, ISzAlloc *allocBig)
1815
#ifdef COMPRESS_MF_MT
1816
MatchFinderMt_Destruct(&p->matchFinderMt, allocBig);
1818
MatchFinder_Free(&p->matchFinderBase, allocBig);
1819
LzmaEnc_FreeLits(p, alloc);
1820
RangeEnc_Free(&p->rc, alloc);
1823
void LzmaEnc_Destroy(CLzmaEncHandle p, ISzAlloc *alloc, ISzAlloc *allocBig)
1825
LzmaEnc_Destruct((CLzmaEnc *)p, alloc, allocBig);
1826
alloc->Free(alloc, p);
1829
static SRes LzmaEnc_CodeOneBlock(CLzmaEnc *p, Bool useLimits, UInt32 maxPackSize, UInt32 maxUnpackSize)
1831
UInt32 nowPos32, startPos32;
1832
if (p->inStream != 0)
1834
p->matchFinderBase.stream = p->inStream;
1835
p->matchFinder.Init(p->matchFinderObj);
1841
RINOK(CheckErrors(p));
1843
nowPos32 = (UInt32)p->nowPos64;
1844
startPos32 = nowPos32;
1846
if (p->nowPos64 == 0)
1848
UInt32 numDistancePairs;
1850
if (p->matchFinder.GetNumAvailableBytes(p->matchFinderObj) == 0)
1851
return Flush(p, nowPos32);
1852
ReadMatchDistances(p, &numDistancePairs);
1853
RangeEnc_EncodeBit(&p->rc, &p->isMatch[p->state][0], 0);
1854
p->state = kLiteralNextStates[p->state];
1855
curByte = p->matchFinder.GetIndexByte(p->matchFinderObj, 0 - p->additionalOffset);
1856
LitEnc_Encode(&p->rc, p->litProbs, curByte);
1857
p->additionalOffset--;
1861
if (p->matchFinder.GetNumAvailableBytes(p->matchFinderObj) != 0)
1864
UInt32 pos, len, posState;
1867
len = GetOptimumFast(p, &pos);
1869
len = GetOptimum(p, nowPos32, &pos);
1872
printf("\n pos = %4X, len = %d pos = %d", nowPos32, len, pos);
1875
posState = nowPos32 & p->pbMask;
1876
if (len == 1 && pos == 0xFFFFFFFF)
1882
RangeEnc_EncodeBit(&p->rc, &p->isMatch[p->state][posState], 0);
1883
data = p->matchFinder.GetPointerToCurrentPos(p->matchFinderObj) - p->additionalOffset;
1885
probs = LIT_PROBS(nowPos32, *(data - 1));
1886
if (IsCharState(p->state))
1887
LitEnc_Encode(&p->rc, probs, curByte);
1889
LitEnc_EncodeMatched(&p->rc, probs, curByte, *(data - p->reps[0] - 1));
1890
p->state = kLiteralNextStates[p->state];
1894
RangeEnc_EncodeBit(&p->rc, &p->isMatch[p->state][posState], 1);
1895
if (pos < LZMA_NUM_REPS)
1897
RangeEnc_EncodeBit(&p->rc, &p->isRep[p->state], 1);
1900
RangeEnc_EncodeBit(&p->rc, &p->isRepG0[p->state], 0);
1901
RangeEnc_EncodeBit(&p->rc, &p->isRep0Long[p->state][posState], ((len == 1) ? 0 : 1));
1905
UInt32 distance = p->reps[pos];
1906
RangeEnc_EncodeBit(&p->rc, &p->isRepG0[p->state], 1);
1908
RangeEnc_EncodeBit(&p->rc, &p->isRepG1[p->state], 0);
1911
RangeEnc_EncodeBit(&p->rc, &p->isRepG1[p->state], 1);
1912
RangeEnc_EncodeBit(&p->rc, &p->isRepG2[p->state], pos - 2);
1914
p->reps[3] = p->reps[2];
1915
p->reps[2] = p->reps[1];
1917
p->reps[1] = p->reps[0];
1918
p->reps[0] = distance;
1921
p->state = kShortRepNextStates[p->state];
1924
LenEnc_Encode2(&p->repLenEnc, &p->rc, len - LZMA_MATCH_LEN_MIN, posState, !p->fastMode, p->ProbPrices);
1925
p->state = kRepNextStates[p->state];
1931
RangeEnc_EncodeBit(&p->rc, &p->isRep[p->state], 0);
1932
p->state = kMatchNextStates[p->state];
1933
LenEnc_Encode2(&p->lenEnc, &p->rc, len - LZMA_MATCH_LEN_MIN, posState, !p->fastMode, p->ProbPrices);
1934
pos -= LZMA_NUM_REPS;
1935
GetPosSlot(pos, posSlot);
1936
RcTree_Encode(&p->rc, p->posSlotEncoder[GetLenToPosState(len)], kNumPosSlotBits, posSlot);
1938
if (posSlot >= kStartPosModelIndex)
1940
UInt32 footerBits = ((posSlot >> 1) - 1);
1941
UInt32 base = ((2 | (posSlot & 1)) << footerBits);
1942
UInt32 posReduced = pos - base;
1944
if (posSlot < kEndPosModelIndex)
1945
RcTree_ReverseEncode(&p->rc, p->posEncoders + base - posSlot - 1, footerBits, posReduced);
1948
RangeEnc_EncodeDirectBits(&p->rc, posReduced >> kNumAlignBits, footerBits - kNumAlignBits);
1949
RcTree_ReverseEncode(&p->rc, p->posAlignEncoder, kNumAlignBits, posReduced & kAlignMask);
1950
p->alignPriceCount++;
1953
p->reps[3] = p->reps[2];
1954
p->reps[2] = p->reps[1];
1955
p->reps[1] = p->reps[0];
1957
p->matchPriceCount++;
1960
p->additionalOffset -= len;
1962
if (p->additionalOffset == 0)
1967
if (p->matchPriceCount >= (1 << 7))
1968
FillDistancesPrices(p);
1969
if (p->alignPriceCount >= kAlignTableSize)
1972
if (p->matchFinder.GetNumAvailableBytes(p->matchFinderObj) == 0)
1974
processed = nowPos32 - startPos32;
1977
if (processed + kNumOpts + 300 >= maxUnpackSize ||
1978
RangeEnc_GetProcessed(&p->rc) + kNumOpts * 2 >= maxPackSize)
1981
else if (processed >= (1 << 15))
1983
p->nowPos64 += nowPos32 - startPos32;
1984
return CheckErrors(p);
1988
p->nowPos64 += nowPos32 - startPos32;
1989
return Flush(p, nowPos32);
1992
#define kBigHashDicLimit ((UInt32)1 << 24)
1994
static SRes LzmaEnc_Alloc(CLzmaEnc *p, UInt32 keepWindowSize, ISzAlloc *alloc, ISzAlloc *allocBig)
1996
UInt32 beforeSize = kNumOpts;
1998
if (!RangeEnc_Alloc(&p->rc, alloc))
1999
return SZ_ERROR_MEM;
2000
btMode = (p->matchFinderBase.btMode != 0);
2001
#ifdef COMPRESS_MF_MT
2002
p->mtMode = (p->multiThread && !p->fastMode && btMode);
2006
unsigned lclp = p->lc + p->lp;
2007
if (p->litProbs == 0 || p->saveState.litProbs == 0 || p->lclp != lclp)
2009
LzmaEnc_FreeLits(p, alloc);
2010
p->litProbs = (CLzmaProb *)alloc->Alloc(alloc, (0x300 << lclp) * sizeof(CLzmaProb));
2011
p->saveState.litProbs = (CLzmaProb *)alloc->Alloc(alloc, (0x300 << lclp) * sizeof(CLzmaProb));
2012
if (p->litProbs == 0 || p->saveState.litProbs == 0)
2014
LzmaEnc_FreeLits(p, alloc);
2015
return SZ_ERROR_MEM;
2021
p->matchFinderBase.bigHash = (p->dictSize > kBigHashDicLimit);
2023
if (beforeSize + p->dictSize < keepWindowSize)
2024
beforeSize = keepWindowSize - p->dictSize;
2026
#ifdef COMPRESS_MF_MT
2029
RINOK(MatchFinderMt_Create(&p->matchFinderMt, p->dictSize, beforeSize, p->numFastBytes, LZMA_MATCH_LEN_MAX, allocBig));
2030
p->matchFinderObj = &p->matchFinderMt;
2031
MatchFinderMt_CreateVTable(&p->matchFinderMt, &p->matchFinder);
2036
if (!MatchFinder_Create(&p->matchFinderBase, p->dictSize, beforeSize, p->numFastBytes, LZMA_MATCH_LEN_MAX, allocBig))
2037
return SZ_ERROR_MEM;
2038
p->matchFinderObj = &p->matchFinderBase;
2039
MatchFinder_CreateVTable(&p->matchFinderBase, &p->matchFinder);
2044
void LzmaEnc_Init(CLzmaEnc *p)
2048
for(i = 0 ; i < LZMA_NUM_REPS; i++)
2051
RangeEnc_Init(&p->rc);
2054
for (i = 0; i < kNumStates; i++)
2057
for (j = 0; j < LZMA_NUM_PB_STATES_MAX; j++)
2059
p->isMatch[i][j] = kProbInitValue;
2060
p->isRep0Long[i][j] = kProbInitValue;
2062
p->isRep[i] = kProbInitValue;
2063
p->isRepG0[i] = kProbInitValue;
2064
p->isRepG1[i] = kProbInitValue;
2065
p->isRepG2[i] = kProbInitValue;
2069
UInt32 num = 0x300 << (p->lp + p->lc);
2070
for (i = 0; i < num; i++)
2071
p->litProbs[i] = kProbInitValue;
2075
for (i = 0; i < kNumLenToPosStates; i++)
2077
CLzmaProb *probs = p->posSlotEncoder[i];
2079
for (j = 0; j < (1 << kNumPosSlotBits); j++)
2080
probs[j] = kProbInitValue;
2084
for(i = 0; i < kNumFullDistances - kEndPosModelIndex; i++)
2085
p->posEncoders[i] = kProbInitValue;
2088
LenEnc_Init(&p->lenEnc.p);
2089
LenEnc_Init(&p->repLenEnc.p);
2091
for (i = 0; i < (1 << kNumAlignBits); i++)
2092
p->posAlignEncoder[i] = kProbInitValue;
2094
p->longestMatchWasFound = False;
2095
p->optimumEndIndex = 0;
2096
p->optimumCurrentIndex = 0;
2097
p->additionalOffset = 0;
2099
p->pbMask = (1 << p->pb) - 1;
2100
p->lpMask = (1 << p->lp) - 1;
2103
void LzmaEnc_InitPrices(CLzmaEnc *p)
2107
FillDistancesPrices(p);
2111
p->lenEnc.tableSize =
2112
p->repLenEnc.tableSize =
2113
p->numFastBytes + 1 - LZMA_MATCH_LEN_MIN;
2114
LenPriceEnc_UpdateTables(&p->lenEnc, 1 << p->pb, p->ProbPrices);
2115
LenPriceEnc_UpdateTables(&p->repLenEnc, 1 << p->pb, p->ProbPrices);
2118
static SRes LzmaEnc_AllocAndInit(CLzmaEnc *p, UInt32 keepWindowSize, ISzAlloc *alloc, ISzAlloc *allocBig)
2121
for (i = 0; i < (UInt32)kDicLogSizeMaxCompress; i++)
2122
if (p->dictSize <= ((UInt32)1 << i))
2124
p->distTableSize = i * 2;
2126
p->finished = False;
2128
RINOK(LzmaEnc_Alloc(p, keepWindowSize, alloc, allocBig));
2130
LzmaEnc_InitPrices(p);
2135
static SRes LzmaEnc_Prepare(CLzmaEncHandle pp, ISeqInStream *inStream, ISeqOutStream *outStream,
2136
ISzAlloc *alloc, ISzAlloc *allocBig)
2138
CLzmaEnc *p = (CLzmaEnc *)pp;
2139
p->inStream = inStream;
2140
p->rc.outStream = outStream;
2141
return LzmaEnc_AllocAndInit(p, 0, alloc, allocBig);
2144
SRes LzmaEnc_PrepareForLzma2(CLzmaEncHandle pp,
2145
ISeqInStream *inStream, UInt32 keepWindowSize,
2146
ISzAlloc *alloc, ISzAlloc *allocBig)
2148
CLzmaEnc *p = (CLzmaEnc *)pp;
2149
p->inStream = inStream;
2150
return LzmaEnc_AllocAndInit(p, keepWindowSize, alloc, allocBig);
2153
static void LzmaEnc_SetInputBuf(CLzmaEnc *p, const Byte *src, SizeT srcLen)
2155
p->seqBufInStream.funcTable.Read = MyRead;
2156
p->seqBufInStream.data = src;
2157
p->seqBufInStream.rem = srcLen;
2160
SRes LzmaEnc_MemPrepare(CLzmaEncHandle pp, const Byte *src, SizeT srcLen,
2161
UInt32 keepWindowSize, ISzAlloc *alloc, ISzAlloc *allocBig)
2163
CLzmaEnc *p = (CLzmaEnc *)pp;
2164
LzmaEnc_SetInputBuf(p, src, srcLen);
2165
p->inStream = &p->seqBufInStream.funcTable;
2166
return LzmaEnc_AllocAndInit(p, keepWindowSize, alloc, allocBig);
2169
void LzmaEnc_Finish(CLzmaEncHandle pp)
2171
#ifdef COMPRESS_MF_MT
2172
CLzmaEnc *p = (CLzmaEnc *)pp;
2174
MatchFinderMt_ReleaseStream(&p->matchFinderMt);
2180
typedef struct _CSeqOutStreamBuf
2182
ISeqOutStream funcTable;
2188
static size_t MyWrite(void *pp, const void *data, size_t size)
2190
CSeqOutStreamBuf *p = (CSeqOutStreamBuf *)pp;
2196
memcpy(p->data, data, size);
2203
UInt32 LzmaEnc_GetNumAvailableBytes(CLzmaEncHandle pp)
2205
const CLzmaEnc *p = (CLzmaEnc *)pp;
2206
return p->matchFinder.GetNumAvailableBytes(p->matchFinderObj);
2209
const Byte *LzmaEnc_GetCurBuf(CLzmaEncHandle pp)
2211
const CLzmaEnc *p = (CLzmaEnc *)pp;
2212
return p->matchFinder.GetPointerToCurrentPos(p->matchFinderObj) - p->additionalOffset;
2215
SRes LzmaEnc_CodeOneMemBlock(CLzmaEncHandle pp, Bool reInit,
2216
Byte *dest, size_t *destLen, UInt32 desiredPackSize, UInt32 *unpackSize)
2218
CLzmaEnc *p = (CLzmaEnc *)pp;
2221
CSeqOutStreamBuf outStream;
2223
outStream.funcTable.Write = MyWrite;
2224
outStream.data = dest;
2225
outStream.rem = *destLen;
2226
outStream.overflow = False;
2228
p->writeEndMark = False;
2229
p->finished = False;
2234
LzmaEnc_InitPrices(p);
2235
nowPos64 = p->nowPos64;
2236
RangeEnc_Init(&p->rc);
2237
p->rc.outStream = &outStream.funcTable;
2239
res = LzmaEnc_CodeOneBlock(pp, True, desiredPackSize, *unpackSize);
2241
*unpackSize = (UInt32)(p->nowPos64 - nowPos64);
2242
*destLen -= outStream.rem;
2243
if (outStream.overflow)
2244
return SZ_ERROR_OUTPUT_EOF;
2249
SRes LzmaEnc_Encode(CLzmaEncHandle pp, ISeqOutStream *outStream, ISeqInStream *inStream, ICompressProgress *progress,
2250
ISzAlloc *alloc, ISzAlloc *allocBig)
2252
CLzmaEnc *p = (CLzmaEnc *)pp;
2255
#ifdef COMPRESS_MF_MT
2256
Byte allocaDummy[0x300];
2258
for (i = 0; i < 16; i++)
2259
allocaDummy[i] = (Byte)i;
2262
RINOK(LzmaEnc_Prepare(pp, inStream, outStream, alloc, allocBig));
2266
res = LzmaEnc_CodeOneBlock(pp, False, 0, 0);
2267
if (res != SZ_OK || p->finished != 0)
2271
res = progress->Progress(progress, p->nowPos64, RangeEnc_GetProcessed(&p->rc));
2274
res = SZ_ERROR_PROGRESS;
2283
SRes LzmaEnc_WriteProperties(CLzmaEncHandle pp, Byte *props, SizeT *size)
2285
CLzmaEnc *p = (CLzmaEnc *)pp;
2287
UInt32 dictSize = p->dictSize;
2288
if (*size < LZMA_PROPS_SIZE)
2289
return SZ_ERROR_PARAM;
2290
*size = LZMA_PROPS_SIZE;
2291
props[0] = (Byte)((p->pb * 5 + p->lp) * 9 + p->lc);
2293
for (i = 11; i <= 30; i++)
2295
if (dictSize <= ((UInt32)2 << i))
2297
dictSize = (2 << i);
2300
if (dictSize <= ((UInt32)3 << i))
2302
dictSize = (3 << i);
2307
for (i = 0; i < 4; i++)
2308
props[1 + i] = (Byte)(dictSize >> (8 * i));
2312
SRes LzmaEnc_MemEncode(CLzmaEncHandle pp, Byte *dest, SizeT *destLen, const Byte *src, SizeT srcLen,
2313
int writeEndMark, ICompressProgress *progress, ISzAlloc *alloc, ISzAlloc *allocBig)
2316
CLzmaEnc *p = (CLzmaEnc *)pp;
2318
CSeqOutStreamBuf outStream;
2320
LzmaEnc_SetInputBuf(p, src, srcLen);
2322
outStream.funcTable.Write = MyWrite;
2323
outStream.data = dest;
2324
outStream.rem = *destLen;
2325
outStream.overflow = False;
2327
p->writeEndMark = writeEndMark;
2328
res = LzmaEnc_Encode(pp, &outStream.funcTable, &p->seqBufInStream.funcTable,
2329
progress, alloc, allocBig);
2331
*destLen -= outStream.rem;
2332
if (outStream.overflow)
2333
return SZ_ERROR_OUTPUT_EOF;
2337
SRes LzmaEncode(Byte *dest, SizeT *destLen, const Byte *src, SizeT srcLen,
2338
const CLzmaEncProps *props, Byte *propsEncoded, SizeT *propsSize, int writeEndMark,
2339
ICompressProgress *progress, ISzAlloc *alloc, ISzAlloc *allocBig)
2341
CLzmaEnc *p = (CLzmaEnc *)LzmaEnc_Create(alloc);
2344
return SZ_ERROR_MEM;
2346
res = LzmaEnc_SetProps(p, props);
2349
res = LzmaEnc_WriteProperties(p, propsEncoded, propsSize);
2351
res = LzmaEnc_MemEncode(p, dest, destLen, src, srcLen,
2352
writeEndMark, progress, alloc, allocBig);
2355
LzmaEnc_Destroy(p, alloc, allocBig);