9 #define kTopValue ((UInt32)1 << kNumTopBits) 11 #define kNumBitModelTotalBits 11 12 #define kBitModelTotal (1 << kNumBitModelTotalBits) 13 #define kNumMoveBits 5 15 #define RC_INIT_SIZE 5 18 if (range < kTopValue) { \ 20 code = (code << 8) | (*buf++); \ 26 bound = (range >> kNumBitModelTotalBits) * ttt; \ 30 *(p) = (CLzmaProb)(ttt + ((kBitModelTotal - ttt) >> kNumMoveBits)); 34 *(p) = (CLzmaProb)(ttt - (ttt >> kNumMoveBits)); 35 #define GET_BIT2(p, i, A0, A1) \ 46 #define GET_BIT(p, i) GET_BIT2(p, i, ;, ;) 48 #define TREE_GET_BIT(probs, i) \ 49 { GET_BIT((probs + i), i); } 50 #define TREE_DECODE(probs, limit, i) \ 54 TREE_GET_BIT(probs, i); \ 55 } while (i < limit); \ 62 #define TREE_6_DECODE(probs, i) TREE_DECODE(probs, (1 << 6), i) 64 #define TREE_6_DECODE(probs, i) \ 67 TREE_GET_BIT(probs, i); \ 68 TREE_GET_BIT(probs, i); \ 69 TREE_GET_BIT(probs, i); \ 70 TREE_GET_BIT(probs, i); \ 71 TREE_GET_BIT(probs, i); \ 72 TREE_GET_BIT(probs, i); \ 77 #define NORMALIZE_CHECK \ 78 if (range < kTopValue) { \ 79 if (buf >= bufLimit) \ 82 code = (code << 8) | (*buf++); \ 85 #define IF_BIT_0_CHECK(p) \ 88 bound = (range >> kNumBitModelTotalBits) * ttt; \ 90 #define UPDATE_0_CHECK range = bound; 91 #define UPDATE_1_CHECK \ 94 #define GET_BIT2_CHECK(p, i, A0, A1) \ 105 #define GET_BIT_CHECK(p, i) GET_BIT2_CHECK(p, i, ;, ;) 106 #define TREE_DECODE_CHECK(probs, limit, i) \ 110 GET_BIT_CHECK(probs + i, i) \ 111 } while (i < limit); \ 115 #define kNumPosBitsMax 4 116 #define kNumPosStatesMax (1 << kNumPosBitsMax) 118 #define kLenNumLowBits 3 119 #define kLenNumLowSymbols (1 << kLenNumLowBits) 120 #define kLenNumMidBits 3 121 #define kLenNumMidSymbols (1 << kLenNumMidBits) 122 #define kLenNumHighBits 8 123 #define kLenNumHighSymbols (1 << kLenNumHighBits) 126 #define LenChoice2 (LenChoice + 1) 127 #define LenLow (LenChoice2 + 1) 128 #define LenMid (LenLow + (kNumPosStatesMax << kLenNumLowBits)) 129 #define LenHigh (LenMid + (kNumPosStatesMax << kLenNumMidBits)) 130 #define kNumLenProbs (LenHigh + kLenNumHighSymbols) 132 #define kNumStates 12 133 #define kNumLitStates 7 135 #define kStartPosModelIndex 4 136 #define kEndPosModelIndex 14 137 #define kNumFullDistances (1 << (kEndPosModelIndex >> 1)) 139 #define kNumPosSlotBits 6 140 #define kNumLenToPosStates 4 142 #define kNumAlignBits 4 143 #define kAlignTableSize (1 << kNumAlignBits) 145 #define kMatchMinLen 2 146 #define kMatchSpecLenStart (kMatchMinLen + kLenNumLowSymbols + kLenNumMidSymbols + kLenNumHighSymbols) 149 #define IsRep (IsMatch + (kNumStates << kNumPosBitsMax)) 150 #define IsRepG0 (IsRep + kNumStates) 151 #define IsRepG1 (IsRepG0 + kNumStates) 152 #define IsRepG2 (IsRepG1 + kNumStates) 153 #define IsRep0Long (IsRepG2 + kNumStates) 154 #define PosSlot (IsRep0Long + (kNumStates << kNumPosBitsMax)) 155 #define SpecPos (PosSlot + (kNumLenToPosStates << kNumPosSlotBits)) 156 #define Align (SpecPos + kNumFullDistances - kEndPosModelIndex) 157 #define LenCoder (Align + kAlignTableSize) 158 #define RepLenCoder (LenCoder + kNumLenProbs) 159 #define Literal (RepLenCoder + kNumLenProbs) 161 #define LZMA_BASE_SIZE 1846 162 #define LZMA_LIT_SIZE 768 164 #define LzmaProps_GetNumProbs(p) ((UInt32)LZMA_BASE_SIZE + (LZMA_LIT_SIZE << ((p)->lc + (p)->lp))) 166 #if Literal != LZMA_BASE_SIZE 170 #define LZMA_DIC_MIN (1 << 12) 189 unsigned state =
p->state;
190 UInt32 rep0 =
p->reps[0], rep1 =
p->reps[1], rep2 =
p->reps[2], rep3 =
p->reps[3];
191 unsigned pbMask = ((unsigned)1 << (
p->prop.pb)) - 1;
192 unsigned lpMask = ((unsigned)1 << (
p->prop.lp)) - 1;
193 unsigned lc =
p->prop.lc;
196 SizeT dicBufSize =
p->dicBufSize;
199 UInt32 processedPos =
p->processedPos;
200 UInt32 checkDicSize =
p->checkDicSize;
211 unsigned posState = processedPos & pbMask;
218 if (checkDicSize != 0 || processedPos != 0)
220 (((processedPos & lpMask) << lc) + (dic[(dicPos == 0 ? dicBufSize : dicPos) - 1] >> (8 - lc))));
227 }
while (symbol < 0x100);
229 unsigned matchByte =
p->dic[(dicPos - rep0) + ((dicPos < rep0) ? dicBufSize : 0)];
230 unsigned offs = 0x100;
237 bit = (matchByte & offs);
238 probLit =
prob + offs +
bit + symbol;
240 }
while (symbol < 0x100);
242 dic[dicPos++] = (
Byte)symbol;
256 if (checkDicSize == 0 && processedPos == 0)
264 dic[dicPos] = dic[(dicPos - rep0) + ((dicPos < rep0) ? dicBufSize : 0)];
334 unsigned posSlot = (unsigned)
distance;
346 }
while (--numDirectBits != 0);
357 t = (0 - ((
UInt32)code >> 31));
369 }
while (--numDirectBits != 0);
390 if (checkDicSize == 0) {
393 }
else if (
distance >= checkDicSize)
404 unsigned curLen = ((rem < len) ? (
unsigned)rem : len);
405 SizeT pos = (dicPos - rep0) + ((dicPos < rep0) ? dicBufSize : 0);
407 processedPos += curLen;
410 if (
pos + curLen <= dicBufSize) {
412 ptrdiff_t
src = (ptrdiff_t)
pos - (ptrdiff_t)dicPos;
417 while (++
dest != lim);
420 dic[dicPos++] = dic[
pos];
421 if (++
pos == dicBufSize)
423 }
while (--curLen != 0);
427 }
while (dicPos <
limit &&
buf < bufLimit);
434 p->processedPos = processedPos;
448 SizeT dicBufSize =
p->dicBufSize;
449 unsigned len =
p->remainLen;
451 if (
limit - dicPos < len)
452 len = (unsigned)(
limit - dicPos);
454 if (
p->checkDicSize == 0 &&
p->prop.dicSize -
p->processedPos <= len)
455 p->checkDicSize =
p->prop.dicSize;
457 p->processedPos += len;
460 dic[dicPos] = dic[(dicPos - rep0) + ((dicPos < rep0) ? dicBufSize : 0)];
470 if (
p->checkDicSize == 0) {
471 UInt32 rem =
p->prop.dicSize -
p->processedPos;
472 if (
limit -
p->dicPos > rem)
473 limit2 =
p->dicPos + rem;
476 if (
p->processedPos >=
p->prop.dicSize)
477 p->checkDicSize =
p->prop.dicSize;
497 const Byte *bufLimit =
buf + inSize;
499 unsigned state =
p->state;
506 unsigned posState = (
p->processedPos) & ((1 <<
p->prop.pb) - 1);
515 if (
p->checkDicSize != 0 ||
p->processedPos != 0)
517 (
p->dic[(
p->dicPos == 0 ?
p->dicBufSize :
p->dicPos) - 1] >> (8 -
p->prop.lc))));
523 }
while (symbol < 0x100);
525 unsigned matchByte =
p->dic[
p->dicPos -
p->reps[0] + ((
p->dicPos <
p->reps[0]) ?
p->dicBufSize : 0)];
526 unsigned offs = 0x100;
532 bit = (matchByte & offs);
533 probLit =
prob + offs +
bit + symbol;
535 }
while (symbol < 0x100);
616 int numDirectBits = ((posSlot >> 1) - 1);
621 prob = probs +
SpecPos + ((2 | (posSlot & 1)) << numDirectBits) - posSlot - 1;
629 }
while (--numDirectBits != 0);
637 }
while (--numDirectBits != 0);
649 p->range = 0xFFFFFFFF;
661 p->needInitState = 1;
664 p->needInitState = 1;
676 for (
i = 0;
i < numProbs;
i++)
678 p->reps[0] =
p->reps[1] =
p->reps[2] =
p->reps[3] = 1;
680 p->needInitState = 0;
685 SizeT inSize = *srcLen;
694 if (
p->needFlush != 0) {
695 for (; inSize > 0 &&
p->tempBufSize <
RC_INIT_SIZE; (*srcLen)++, inSize--)
696 p->tempBuf[
p->tempBufSize++] = *
src++;
701 if (
p->tempBuf[0] != 0)
709 if (
p->dicPos >= dicLimit) {
710 if (
p->remainLen == 0 &&
p->code == 0) {
718 if (
p->remainLen != 0) {
725 if (
p->needInitState)
728 if (
p->tempBufSize == 0) {
730 const Byte *bufLimit;
734 memcpy(
p->tempBuf,
src, inSize);
735 p->tempBufSize = (unsigned)inSize;
751 (*srcLen) += processed;
755 unsigned rem =
p->tempBufSize, lookAhead = 0;
757 p->tempBuf[rem++] =
src[lookAhead++];
758 p->tempBufSize = rem;
762 (*srcLen) += lookAhead;
774 lookAhead -= (rem - (unsigned)(
p->buf -
p->tempBuf));
775 (*srcLen) += lookAhead;
793 SizeT outSize = *destLen;
794 SizeT inSize = *srcLen;
795 *srcLen = *destLen = 0;
797 SizeT inSizeCur = inSize, outSizeCur, dicPos;
800 if (
p->dicPos ==
p->dicBufSize)
803 if (outSize >
p->dicBufSize - dicPos) {
804 outSizeCur =
p->dicBufSize;
807 outSizeCur = dicPos + outSize;
808 curFinishMode = finishMode;
814 *srcLen += inSizeCur;
815 outSizeCur =
p->dicPos - dicPos;
816 memcpy(
dest,
p->dic + dicPos, outSizeCur);
818 outSize -= outSizeCur;
819 *destLen += outSizeCur;
822 if (outSizeCur == 0 || outSize == 0)
828 alloc->
Free(alloc,
p->probs);
833 alloc->
Free(alloc,
p->dic);
853 p->dicSize = dicSize;
856 if (
d >= (9 * 5 * 5))
869 if (
p->probs ==
nullptr || numProbs !=
p->numProbs) {
872 p->numProbs = numProbs;
873 if (
p->probs ==
nullptr)
893 if (
p->dic ==
nullptr || dicBufSize !=
p->dicBufSize) {
895 p->dic = (
Byte *)alloc->
Alloc(alloc, dicBufSize);
896 if (
p->dic ==
nullptr) {
901 p->dicBufSize = dicBufSize;
910 const Byte *propData,
917 SizeT inSize = *srcLen;
918 SizeT outSize = *destLen;
919 *srcLen = *destLen = 0;
928 p.dicBufSize = outSize;
938 (*destLen) =
p.dicPos;
#define SZ_ERROR_INPUT_EOF
void(* Free)(void *p, void *address)
void LzmaDec_Free(CLzmaDec *p, ISzAlloc *alloc)
#define LzmaProps_GetNumProbs(p)
#define GET_BIT_CHECK(p, i)
#define GET_BIT2(p, i, A0, A1)
void LzmaDec_FreeProbs(CLzmaDec *p, ISzAlloc *alloc)
static void LzmaDec_FreeDict(CLzmaDec *p, ISzAlloc *alloc)
#define kLenNumLowSymbols
static void LzmaDec_WriteRem(CLzmaDec *p, SizeT limit)
#define kStartPosModelIndex
SRes LzmaDec_DecodeToDic(CLzmaDec *p, SizeT dicLimit, const Byte *src, SizeT *srcLen, ELzmaFinishMode finishMode, ELzmaStatus *status)
#define TREE_DECODE(probs, limit, i)
#define GET_BIT2_CHECK(p, i, A0, A1)
void *(* Alloc)(void *p, size_t size)
static void LzmaDec_InitRc(CLzmaDec *p, const Byte *data)
void LzmaDec_InitDicAndState(CLzmaDec *p, Bool initDic, Bool initState)
static int LzmaDec_DecodeReal2(CLzmaDec *p, SizeT limit, const Byte *bufLimit)
SRes LzmaDec_AllocateProbs(CLzmaDec *p, const Byte *props, unsigned propsSize, ISzAlloc *alloc)
#define LZMA_REQUIRED_INPUT_MAX
#define kMatchSpecLenStart
#define kEndPosModelIndex
#define LzmaDec_Construct(p)
#define IF_BIT_0_CHECK(p)
#define kLenNumMidSymbols
SRes LzmaDecode(Byte *dest, SizeT *destLen, const Byte *src, SizeT *srcLen, const Byte *propData, unsigned propSize, ELzmaFinishMode finishMode, ELzmaStatus *status, ISzAlloc *alloc)
#define kNumLenToPosStates
char data[epos_bytes_allocation]
static SRes LzmaDec_AllocateProbs2(CLzmaDec *p, const CLzmaProps *propNew, ISzAlloc *alloc)
SRes LzmaProps_Decode(CLzmaProps *p, const Byte *data, unsigned size)
#define TREE_DECODE_CHECK(probs, limit, i)
void LzmaDec_Init(CLzmaDec *p)
SRes LzmaDec_DecodeToBuf(CLzmaDec *p, Byte *dest, SizeT *destLen, const Byte *src, SizeT *srcLen, ELzmaFinishMode finishMode, ELzmaStatus *status)
SRes LzmaDec_Allocate(CLzmaDec *p, const Byte *props, unsigned propsSize, ISzAlloc *alloc)
#define TREE_6_DECODE(probs, i)
static int LzmaDec_DecodeReal(CLzmaDec *p, SizeT limit, const Byte *bufLimit)
static void LzmaDec_InitStateReal(CLzmaDec *p)
static ELzmaDummy LzmaDec_TryDummy(const CLzmaDec *p, const Byte *buf, SizeT inSize)
#define SZ_ERROR_UNSUPPORTED