10 #define kNumTopBits 24
11 #define kTopValue ((UInt32)1 << kNumTopBits)
13 #define kNumBitModelTotalBits 11
14 #define kBitModelTotal (1 << kNumBitModelTotalBits)
15 #define kNumMoveBits 5
17 #define RC_INIT_SIZE 5
19 #define NORMALIZE if (range < kTopValue) { range <<= 8; code = (code << 8) | (*buf++); }
21 #define IF_BIT_0(p) ttt = *(p); NORMALIZE; bound = (range >> kNumBitModelTotalBits) * ttt; if (code < bound)
22 #define UPDATE_0(p) range = bound; *(p) = (CLzmaProb)(ttt + ((kBitModelTotal - ttt) >> kNumMoveBits));
23 #define UPDATE_1(p) range -= bound; code -= bound; *(p) = (CLzmaProb)(ttt - (ttt >> kNumMoveBits));
24 #define GET_BIT2(p, i, A0, A1) IF_BIT_0(p) \
25 { UPDATE_0(p); i = (i + i); A0; } else \
26 { UPDATE_1(p); i = (i + i) + 1; A1; }
27 #define GET_BIT(p, i) GET_BIT2(p, i, ; , ;)
29 #define TREE_GET_BIT(probs, i) { GET_BIT((probs + i), i); }
30 #define TREE_DECODE(probs, limit, i) \
31 { i = 1; do { TREE_GET_BIT(probs, i); } while (i < limit); i -= limit; }
36 #define TREE_6_DECODE(probs, i) TREE_DECODE(probs, (1 << 6), i)
38 #define TREE_6_DECODE(probs, i) \
40 TREE_GET_BIT(probs, i); \
41 TREE_GET_BIT(probs, i); \
42 TREE_GET_BIT(probs, i); \
43 TREE_GET_BIT(probs, i); \
44 TREE_GET_BIT(probs, i); \
45 TREE_GET_BIT(probs, i); \
49 #define NORMAL_LITER_DEC GET_BIT(prob + symbol, symbol)
50 #define MATCHED_LITER_DEC \
52 bit = (matchByte & offs); \
53 probLit = prob + offs + bit + symbol; \
54 GET_BIT2(probLit, symbol, offs &= ~bit, offs &= bit)
56 #define NORMALIZE_CHECK if (range < kTopValue) { if (buf >= bufLimit) return DUMMY_ERROR; range <<= 8; code = (code << 8) | (*buf++); }
58 #define IF_BIT_0_CHECK(p) ttt = *(p); NORMALIZE_CHECK; bound = (range >> kNumBitModelTotalBits) * ttt; if (code < bound)
59 #define UPDATE_0_CHECK range = bound;
60 #define UPDATE_1_CHECK range -= bound; code -= bound;
61 #define GET_BIT2_CHECK(p, i, A0, A1) IF_BIT_0_CHECK(p) \
62 { UPDATE_0_CHECK; i = (i + i); A0; } else \
63 { UPDATE_1_CHECK; i = (i + i) + 1; A1; }
64 #define GET_BIT_CHECK(p, i) GET_BIT2_CHECK(p, i, ; , ;)
65 #define TREE_DECODE_CHECK(probs, limit, i) \
66 { i = 1; do { GET_BIT_CHECK(probs + i, i) } while (i < limit); i -= limit; }
69 #define kNumPosBitsMax 4
70 #define kNumPosStatesMax (1 << kNumPosBitsMax)
72 #define kLenNumLowBits 3
73 #define kLenNumLowSymbols (1 << kLenNumLowBits)
74 #define kLenNumMidBits 3
75 #define kLenNumMidSymbols (1 << kLenNumMidBits)
76 #define kLenNumHighBits 8
77 #define kLenNumHighSymbols (1 << kLenNumHighBits)
80 #define LenChoice2 (LenChoice + 1)
81 #define LenLow (LenChoice2 + 1)
82 #define LenMid (LenLow + (kNumPosStatesMax << kLenNumLowBits))
83 #define LenHigh (LenMid + (kNumPosStatesMax << kLenNumMidBits))
84 #define kNumLenProbs (LenHigh + kLenNumHighSymbols)
88 #define kNumLitStates 7
90 #define kStartPosModelIndex 4
91 #define kEndPosModelIndex 14
92 #define kNumFullDistances (1 << (kEndPosModelIndex >> 1))
94 #define kNumPosSlotBits 6
95 #define kNumLenToPosStates 4
97 #define kNumAlignBits 4
98 #define kAlignTableSize (1 << kNumAlignBits)
100 #define kMatchMinLen 2
101 #define kMatchSpecLenStart (kMatchMinLen + kLenNumLowSymbols + kLenNumMidSymbols + kLenNumHighSymbols)
104 #define IsRep (IsMatch + (kNumStates << kNumPosBitsMax))
105 #define IsRepG0 (IsRep + kNumStates)
106 #define IsRepG1 (IsRepG0 + kNumStates)
107 #define IsRepG2 (IsRepG1 + kNumStates)
108 #define IsRep0Long (IsRepG2 + kNumStates)
109 #define PosSlot (IsRep0Long + (kNumStates << kNumPosBitsMax))
110 #define SpecPos (PosSlot + (kNumLenToPosStates << kNumPosSlotBits))
111 #define Align (SpecPos + kNumFullDistances - kEndPosModelIndex)
112 #define LenCoder (Align + kAlignTableSize)
113 #define RepLenCoder (LenCoder + kNumLenProbs)
114 #define Literal (RepLenCoder + kNumLenProbs)
116 #define LZMA_BASE_SIZE 1846
117 #define LZMA_LIT_SIZE 0x300
119 #if Literal != LZMA_BASE_SIZE
123 #define LzmaProps_GetNumProbs(p) (Literal + ((UInt32)LZMA_LIT_SIZE << ((p)->lc + (p)->lp)))
125 #define LZMA_DIC_MIN (1 << 12)
144 unsigned state = p->
state;
146 unsigned pbMask = ((unsigned)1 << (p->
prop.
pb)) - 1;
147 unsigned lpMask = ((unsigned)1 << (p->
prop.
lp)) - 1;
167 unsigned posState = processedPos & pbMask;
175 if (processedPos != 0 || checkDicSize != 0)
177 (dic[(dicPos == 0 ? dicBufSize : dicPos) - 1] >> (8 - lc))));
182 state -= (state < 4) ? state : 3;
184 #ifdef _LZMA_SIZE_OPT
199 unsigned matchByte = dic[dicPos - rep0 + (dicPos < rep0 ? dicBufSize : 0)];
200 unsigned offs = 0x100;
201 state -= (state < 10) ? 3 : 6;
203 #ifdef _LZMA_SIZE_OPT
210 while (symbol < 0x100);
227 dic[dicPos++] = (
Byte)symbol;
233 prob = probs +
IsRep + state;
243 if (checkDicSize == 0 && processedPos == 0)
245 prob = probs +
IsRepG0 + state;
253 dic[dicPos] = dic[dicPos - rep0 + (dicPos < rep0 ? dicBufSize : 0)];
265 prob = probs +
IsRepG1 + state;
274 prob = probs +
IsRepG2 + state;
295 #ifdef _LZMA_SIZE_OPT
297 unsigned lim, offset;
373 unsigned posSlot = (unsigned)distance;
374 unsigned numDirectBits = (unsigned)(((distance >> 1) - 1));
375 distance = (2 | (distance & 1));
378 distance <<= numDirectBits;
379 prob = probs +
SpecPos + distance - posSlot - 1;
385 GET_BIT2(prob + i, i, ; , distance |= mask);
388 while (--numDirectBits != 0);
402 t = (0 - ((
UInt32)code >> 31));
403 distance = (distance << 1) + (t + 1);
415 while (--numDirectBits != 0);
416 prob = probs +
Align;
420 GET_BIT2(prob + i, i, ; , distance |= 1);
421 GET_BIT2(prob + i, i, ; , distance |= 2);
422 GET_BIT2(prob + i, i, ; , distance |= 4);
423 GET_BIT2(prob + i, i, ; , distance |= 8);
425 if (distance == (
UInt32)0xFFFFFFFF)
438 if (checkDicSize == 0)
440 if (distance >= processedPos)
446 else if (distance >= checkDicSize)
461 if ((rem = limit - dicPos) == 0)
467 curLen = ((rem < len) ? (
unsigned)rem : len);
468 pos = dicPos - rep0 + (dicPos < rep0 ? dicBufSize : 0);
470 processedPos += curLen;
473 if (curLen <= dicBufSize - pos)
476 ptrdiff_t src = (ptrdiff_t)pos - (ptrdiff_t)dicPos;
477 const Byte *lim = dest + curLen;
480 *(dest) = (
Byte)*(dest + src);
481 while (++dest != lim);
487 dic[dicPos++] = dic[pos];
488 if (++pos == dicBufSize)
491 while (--curLen != 0);
496 while (dicPos < limit && buf < bufLimit);
524 SizeT rem = limit - dicPos;
526 len = (unsigned)(rem);
536 dic[dicPos] = dic[dicPos - rep0 + (dicPos < rep0 ? dicBufSize : 0)];
547 SizeT limit2 = limit;
551 if (limit - p->
dicPos > rem)
582 const Byte *bufLimit = buf + inSize;
584 unsigned state = p->
state;
609 do {
GET_BIT_CHECK(prob + symbol, symbol) }
while (symbol < 0x100);
615 unsigned offs = 0x100;
622 bit = (matchByte & offs);
623 probLit = prob + offs + bit + symbol;
626 while (symbol < 0x100);
635 prob = probs +
IsRep + state;
647 prob = probs +
IsRepG0 + state;
666 prob = probs +
IsRepG1 + state;
674 prob = probs +
IsRepG2 + state;
689 unsigned limit, offset;
730 unsigned numDirectBits = ((posSlot >> 1) - 1);
736 prob = probs +
SpecPos + ((2 | (posSlot & 1)) << numDirectBits) - posSlot - 1;
745 code -= range & (((code - range) >> 31) - 1);
748 while (--numDirectBits != 0);
749 prob = probs +
Align;
758 while (--numDirectBits != 0);
796 for (i = 0; i < numProbs; i++)
806 SizeT inSize = *srcLen;
832 p->
range = 0xFFFFFFFF;
838 if (p->
dicPos >= dicLimit)
864 const Byte *bufLimit;
889 (*srcLen) += processed;
897 p->
tempBuf[rem++] = src[lookAhead++];
904 (*srcLen) += lookAhead;
919 unsigned kkk = (unsigned)(p->
buf - p->
tempBuf);
927 (*srcLen) += lookAhead;
940 SizeT outSize = *destLen;
941 SizeT inSize = *srcLen;
942 *srcLen = *destLen = 0;
945 SizeT inSizeCur = inSize, outSizeCur, dicPos;
958 outSizeCur = dicPos + outSize;
959 curFinishMode = finishMode;
965 *srcLen += inSizeCur;
966 outSizeCur = p->
dicPos - dicPos;
967 memcpy(dest, p->
dic + dicPos, outSizeCur);
969 outSize -= outSizeCur;
970 *destLen += outSizeCur;
973 if (outSizeCur == 0 || outSize == 0)
1004 dicSize = data[1] | ((
UInt32)data[2] << 8) | ((
UInt32)data[3] << 16) | ((
UInt32)data[4] << 24);
1011 if (d >= (9 * 5 * 5))
1055 if (dictSize >= ((
UInt32)1 << 30)) mask = ((
UInt32)1 << 22) - 1;
1056 else if (dictSize >= ((
UInt32)1 << 22)) mask = ((
UInt32)1 << 20) - 1;;
1057 dicBufSize = ((
SizeT)dictSize + mask) & ~mask;
1058 if (dicBufSize < dictSize)
1059 dicBufSize = dictSize;
1083 SizeT outSize = *destLen, inSize = *srcLen;
1084 *destLen = *srcLen = 0;