1 /* LzmaDec.c -- LZMA Decoder
2 2009-09-20 : Igor Pavlov : Public domain */
9 #include <linux/string.h>
11 #define kNumTopBits 24
12 #define kTopValue ((UInt32)1 << kNumTopBits)
14 #define kNumBitModelTotalBits 11
15 #define kBitModelTotal (1 << kNumBitModelTotalBits)
16 #define kNumMoveBits 5
18 #define RC_INIT_SIZE 5
20 #define NORMALIZE if (range < kTopValue) { range <<= 8; code = (code << 8) | (*buf++); }
22 #define IF_BIT_0(p) ttt = *(p); NORMALIZE; bound = (range >> kNumBitModelTotalBits) * ttt; if (code < bound)
23 #define UPDATE_0(p) range = bound; *(p) = (CLzmaProb)(ttt + ((kBitModelTotal - ttt) >> kNumMoveBits));
24 #define UPDATE_1(p) range -= bound; code -= bound; *(p) = (CLzmaProb)(ttt - (ttt >> kNumMoveBits));
25 #define GET_BIT2(p, i, A0, A1) IF_BIT_0(p) \
26 { UPDATE_0(p); i = (i + i); A0; } else \
27 { UPDATE_1(p); i = (i + i) + 1; A1; }
28 #define GET_BIT(p, i) GET_BIT2(p, i, ; , ;)
30 #define TREE_GET_BIT(probs, i) { GET_BIT((probs + i), i); }
31 #define TREE_DECODE(probs, limit, i) \
32 { i = 1; do { TREE_GET_BIT(probs, i); } while (i < limit); i -= limit; }
34 /* #define _LZMA_SIZE_OPT */
37 #define TREE_6_DECODE(probs, i) TREE_DECODE(probs, (1 << 6), i)
39 #define TREE_6_DECODE(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); \
46 TREE_GET_BIT(probs, i); \
50 #define NORMALIZE_CHECK if (range < kTopValue) { if (buf >= bufLimit) return DUMMY_ERROR; range <<= 8; code = (code << 8) | (*buf++); }
52 #define IF_BIT_0_CHECK(p) ttt = *(p); NORMALIZE_CHECK; bound = (range >> kNumBitModelTotalBits) * ttt; if (code < bound)
53 #define UPDATE_0_CHECK range = bound;
54 #define UPDATE_1_CHECK range -= bound; code -= bound;
55 #define GET_BIT2_CHECK(p, i, A0, A1) IF_BIT_0_CHECK(p) \
56 { UPDATE_0_CHECK; i = (i + i); A0; } else \
57 { UPDATE_1_CHECK; i = (i + i) + 1; A1; }
58 #define GET_BIT_CHECK(p, i) GET_BIT2_CHECK(p, i, ; , ;)
59 #define TREE_DECODE_CHECK(probs, limit, i) \
60 { i = 1; do { GET_BIT_CHECK(probs + i, i) } while (i < limit); i -= limit; }
63 #define kNumPosBitsMax 4
64 #define kNumPosStatesMax (1 << kNumPosBitsMax)
66 #define kLenNumLowBits 3
67 #define kLenNumLowSymbols (1 << kLenNumLowBits)
68 #define kLenNumMidBits 3
69 #define kLenNumMidSymbols (1 << kLenNumMidBits)
70 #define kLenNumHighBits 8
71 #define kLenNumHighSymbols (1 << kLenNumHighBits)
74 #define LenChoice2 (LenChoice + 1)
75 #define LenLow (LenChoice2 + 1)
76 #define LenMid (LenLow + (kNumPosStatesMax << kLenNumLowBits))
77 #define LenHigh (LenMid + (kNumPosStatesMax << kLenNumMidBits))
78 #define kNumLenProbs (LenHigh + kLenNumHighSymbols)
82 #define kNumLitStates 7
84 #define kStartPosModelIndex 4
85 #define kEndPosModelIndex 14
86 #define kNumFullDistances (1 << (kEndPosModelIndex >> 1))
88 #define kNumPosSlotBits 6
89 #define kNumLenToPosStates 4
91 #define kNumAlignBits 4
92 #define kAlignTableSize (1 << kNumAlignBits)
94 #define kMatchMinLen 2
95 #define kMatchSpecLenStart (kMatchMinLen + kLenNumLowSymbols + kLenNumMidSymbols + kLenNumHighSymbols)
98 #define IsRep (IsMatch + (kNumStates << kNumPosBitsMax))
99 #define IsRepG0 (IsRep + kNumStates)
100 #define IsRepG1 (IsRepG0 + kNumStates)
101 #define IsRepG2 (IsRepG1 + kNumStates)
102 #define IsRep0Long (IsRepG2 + kNumStates)
103 #define PosSlot (IsRep0Long + (kNumStates << kNumPosBitsMax))
104 #define SpecPos (PosSlot + (kNumLenToPosStates << kNumPosSlotBits))
105 #define Align (SpecPos + kNumFullDistances - kEndPosModelIndex)
106 #define LenCoder (Align + kAlignTableSize)
107 #define RepLenCoder (LenCoder + kNumLenProbs)
108 #define Literal (RepLenCoder + kNumLenProbs)
110 #define LZMA_BASE_SIZE 1846
111 #define LZMA_LIT_SIZE 768
113 #define LzmaProps_GetNumProbs(p) ((UInt32)LZMA_BASE_SIZE + (LZMA_LIT_SIZE << ((p)->lc + (p)->lp)))
115 #if Literal != LZMA_BASE_SIZE
119 #define LZMA_DIC_MIN (1 << 12)
121 /* First LZMA-symbol is always decoded.
122 And it decodes new LZMA-symbols while (buf < bufLimit), but "buf" is without last normalization
126 SZ_ERROR_DATA - Error
128 < kMatchSpecLenStart : normal remain
129 = kMatchSpecLenStart : finished
130 = kMatchSpecLenStart + 1 : Flush marker
131 = kMatchSpecLenStart + 2 : State Init Marker
134 static int MY_FAST_CALL LzmaDec_DecodeReal(CLzmaDec *p, SizeT limit, const Byte *bufLimit)
136 CLzmaProb *probs = p->probs;
138 unsigned state = p->state;
139 UInt32 rep0 = p->reps[0], rep1 = p->reps[1], rep2 = p->reps[2], rep3 = p->reps[3];
140 unsigned pbMask = ((unsigned)1 << (p->prop.pb)) - 1;
141 unsigned lpMask = ((unsigned)1 << (p->prop.lp)) - 1;
142 unsigned lc = p->prop.lc;
145 SizeT dicBufSize = p->dicBufSize;
146 SizeT dicPos = p->dicPos;
148 UInt32 processedPos = p->processedPos;
149 UInt32 checkDicSize = p->checkDicSize;
152 const Byte *buf = p->buf;
153 UInt32 range = p->range;
154 UInt32 code = p->code;
163 unsigned posState = processedPos & pbMask;
165 prob = probs + IsMatch + (state << kNumPosBitsMax) + posState;
170 prob = probs + Literal;
171 if (checkDicSize != 0 || processedPos != 0)
172 prob += (LZMA_LIT_SIZE * (((processedPos & lpMask) << lc) +
173 (dic[(dicPos == 0 ? dicBufSize : dicPos) - 1] >> (8 - lc))));
175 if (state < kNumLitStates)
177 state -= (state < 4) ? state : 3;
182 do { GET_BIT(prob + symbol, symbol) } while (symbol < 0x100);
186 unsigned matchByte = p->dic[(dicPos - rep0) + ((dicPos < rep0) ? dicBufSize : 0)];
187 unsigned offs = 0x100;
188 state -= (state < 10) ? 3 : 6;
198 bit = (matchByte & offs);
199 probLit = prob + offs + bit + symbol;
200 GET_BIT2(probLit, symbol, offs &= ~bit, offs &= bit)
202 while (symbol < 0x100);
204 dic[dicPos++] = (Byte)symbol;
211 prob = probs + IsRep + state;
216 prob = probs + LenCoder;
221 if (checkDicSize == 0 && processedPos == 0)
222 return SZ_ERROR_DATA;
223 prob = probs + IsRepG0 + state;
227 prob = probs + IsRep0Long + (state << kNumPosBitsMax) + posState;
231 dic[dicPos] = dic[(dicPos - rep0) + ((dicPos < rep0) ? dicBufSize : 0)];
234 state = state < kNumLitStates ? 9 : 11;
243 prob = probs + IsRepG1 + state;
252 prob = probs + IsRepG2 + state;
269 state = state < kNumLitStates ? 8 : 11;
270 prob = probs + RepLenCoder;
273 unsigned limit, offset;
274 CLzmaProb *probLen = prob + LenChoice;
278 probLen = prob + LenLow + (posState << kLenNumLowBits);
280 limit = (1 << kLenNumLowBits);
285 probLen = prob + LenChoice2;
289 probLen = prob + LenMid + (posState << kLenNumMidBits);
290 offset = kLenNumLowSymbols;
291 limit = (1 << kLenNumMidBits);
296 probLen = prob + LenHigh;
297 offset = kLenNumLowSymbols + kLenNumMidSymbols;
298 limit = (1 << kLenNumHighBits);
301 TREE_DECODE(probLen, limit, len);
305 if (state >= kNumStates)
308 prob = probs + PosSlot +
309 ((len < kNumLenToPosStates ? len : kNumLenToPosStates - 1) << kNumPosSlotBits);
310 TREE_6_DECODE(prob, distance);
311 if (distance >= kStartPosModelIndex)
313 unsigned posSlot = (unsigned)distance;
314 int numDirectBits = (int)(((distance >> 1) - 1));
315 distance = (2 | (distance & 1));
316 if (posSlot < kEndPosModelIndex)
318 distance <<= numDirectBits;
319 prob = probs + SpecPos + distance - posSlot - 1;
328 GET_BIT2(prob + i, i, ; , distance |= mask);
331 while (--numDirectBits != 0);
336 numDirectBits -= kNumAlignBits;
348 t = (0 - ((UInt32)code >> 31)); /* (UInt32)((Int32)code >> 31) */
349 distance = (distance << 1) + (t + 1);
361 while (--numDirectBits != 0);
362 prob = probs + Align;
363 distance <<= kNumAlignBits;
366 GET_BIT2(prob + i, i, ; , distance |= 1);
367 GET_BIT2(prob + i, i, ; , distance |= 2);
368 GET_BIT2(prob + i, i, ; , distance |= 4);
369 GET_BIT2(prob + i, i, ; , distance |= 8);
371 if (distance == (UInt32)0xFFFFFFFF)
373 len += kMatchSpecLenStart;
383 if (checkDicSize == 0)
385 if (distance >= processedPos)
386 return SZ_ERROR_DATA;
388 else if (distance >= checkDicSize)
389 return SZ_ERROR_DATA;
390 state = (state < kNumStates + kNumLitStates) ? kNumLitStates : kNumLitStates + 3;
396 return SZ_ERROR_DATA;
398 SizeT rem = limit - dicPos;
399 unsigned curLen = ((rem < len) ? (unsigned)rem : len);
400 SizeT pos = (dicPos - rep0) + ((dicPos < rep0) ? dicBufSize : 0);
402 processedPos += curLen;
405 if (pos + curLen <= dicBufSize)
407 Byte *dest = dic + dicPos;
408 ptrdiff_t src = (ptrdiff_t)pos - (ptrdiff_t)dicPos;
409 const Byte *lim = dest + curLen;
415 *(dest) = (Byte)*(dest + src);
416 while (++dest != lim);
425 dic[dicPos++] = dic[pos];
426 if (++pos == dicBufSize)
429 while (--curLen != 0);
434 while (dicPos < limit && buf < bufLimit);
444 p->processedPos = processedPos;
454 static void MY_FAST_CALL LzmaDec_WriteRem(CLzmaDec *p, SizeT limit)
456 if (p->remainLen != 0 && p->remainLen < kMatchSpecLenStart)
459 SizeT dicPos = p->dicPos;
460 SizeT dicBufSize = p->dicBufSize;
461 unsigned len = p->remainLen;
462 UInt32 rep0 = p->reps[0];
463 if (limit - dicPos < len)
464 len = (unsigned)(limit - dicPos);
466 if (p->checkDicSize == 0 && p->prop.dicSize - p->processedPos <= len)
467 p->checkDicSize = p->prop.dicSize;
469 p->processedPos += len;
473 dic[dicPos] = dic[(dicPos - rep0) + ((dicPos < rep0) ? dicBufSize : 0)];
480 static int MY_FAST_CALL LzmaDec_DecodeReal2(CLzmaDec *p, SizeT limit, const Byte *bufLimit)
484 SizeT limit2 = limit;
485 if (p->checkDicSize == 0)
487 UInt32 rem = p->prop.dicSize - p->processedPos;
488 if (limit - p->dicPos > rem)
489 limit2 = p->dicPos + rem;
491 RINOK(LzmaDec_DecodeReal(p, limit2, bufLimit));
492 if (p->processedPos >= p->prop.dicSize)
493 p->checkDicSize = p->prop.dicSize;
494 LzmaDec_WriteRem(p, limit);
496 while (p->dicPos < limit && p->buf < bufLimit && p->remainLen < kMatchSpecLenStart);
498 if (p->remainLen > kMatchSpecLenStart)
500 p->remainLen = kMatchSpecLenStart;
507 DUMMY_ERROR, /* unexpected end of input stream */
513 static ELzmaDummy LzmaDec_TryDummy(const CLzmaDec *p, const Byte *buf, SizeT inSize)
515 UInt32 range = p->range;
516 UInt32 code = p->code;
517 const Byte *bufLimit = buf + inSize;
518 CLzmaProb *probs = p->probs;
519 unsigned state = p->state;
526 unsigned posState = (p->processedPos) & ((1 << p->prop.pb) - 1);
528 prob = probs + IsMatch + (state << kNumPosBitsMax) + posState;
533 /* if (bufLimit - buf >= 7) return DUMMY_LIT; */
535 prob = probs + Literal;
536 if (p->checkDicSize != 0 || p->processedPos != 0)
537 prob += (LZMA_LIT_SIZE *
538 ((((p->processedPos) & ((1 << (p->prop.lp)) - 1)) << p->prop.lc) +
539 (p->dic[(p->dicPos == 0 ? p->dicBufSize : p->dicPos) - 1] >> (8 - p->prop.lc))));
541 if (state < kNumLitStates)
544 do { GET_BIT_CHECK(prob + symbol, symbol) } while (symbol < 0x100);
548 unsigned matchByte = p->dic[p->dicPos - p->reps[0] +
549 ((p->dicPos < p->reps[0]) ? p->dicBufSize : 0)];
550 unsigned offs = 0x100;
557 bit = (matchByte & offs);
558 probLit = prob + offs + bit + symbol;
559 GET_BIT2_CHECK(probLit, symbol, offs &= ~bit, offs &= bit)
561 while (symbol < 0x100);
570 prob = probs + IsRep + state;
575 prob = probs + LenCoder;
582 prob = probs + IsRepG0 + state;
586 prob = probs + IsRep0Long + (state << kNumPosBitsMax) + posState;
601 prob = probs + IsRepG1 + state;
609 prob = probs + IsRepG2 + state;
621 prob = probs + RepLenCoder;
624 unsigned limit, offset;
625 CLzmaProb *probLen = prob + LenChoice;
626 IF_BIT_0_CHECK(probLen)
629 probLen = prob + LenLow + (posState << kLenNumLowBits);
631 limit = 1 << kLenNumLowBits;
636 probLen = prob + LenChoice2;
637 IF_BIT_0_CHECK(probLen)
640 probLen = prob + LenMid + (posState << kLenNumMidBits);
641 offset = kLenNumLowSymbols;
642 limit = 1 << kLenNumMidBits;
647 probLen = prob + LenHigh;
648 offset = kLenNumLowSymbols + kLenNumMidSymbols;
649 limit = 1 << kLenNumHighBits;
652 TREE_DECODE_CHECK(probLen, limit, len);
659 prob = probs + PosSlot +
660 ((len < kNumLenToPosStates ? len : kNumLenToPosStates - 1) <<
662 TREE_DECODE_CHECK(prob, 1 << kNumPosSlotBits, posSlot);
663 if (posSlot >= kStartPosModelIndex)
665 int numDirectBits = ((posSlot >> 1) - 1);
667 /* if (bufLimit - buf >= 8) return DUMMY_MATCH; */
669 if (posSlot < kEndPosModelIndex)
671 prob = probs + SpecPos + ((2 | (posSlot & 1)) << numDirectBits) - posSlot - 1;
675 numDirectBits -= kNumAlignBits;
680 code -= range & (((code - range) >> 31) - 1);
681 /* if (code >= range) code -= range; */
683 while (--numDirectBits != 0);
684 prob = probs + Align;
685 numDirectBits = kNumAlignBits;
691 GET_BIT_CHECK(prob + i, i);
693 while (--numDirectBits != 0);
704 static void LzmaDec_InitRc(CLzmaDec *p, const Byte *data)
706 p->code = ((UInt32)data[1] << 24) | ((UInt32)data[2] << 16) | ((UInt32)data[3] << 8) | ((UInt32)data[4]);
707 p->range = 0xFFFFFFFF;
711 void LzmaDec_InitDicAndState(CLzmaDec *p, Bool initDic, Bool initState)
721 p->needInitState = 1;
724 p->needInitState = 1;
727 void LzmaDec_Init(CLzmaDec *p)
730 LzmaDec_InitDicAndState(p, True, True);
733 static void LzmaDec_InitStateReal(CLzmaDec *p)
735 UInt32 numProbs = Literal + ((UInt32)LZMA_LIT_SIZE << (p->prop.lc + p->prop.lp));
737 CLzmaProb *probs = p->probs;
738 for (i = 0; i < numProbs; i++)
739 probs[i] = kBitModelTotal >> 1;
740 p->reps[0] = p->reps[1] = p->reps[2] = p->reps[3] = 1;
742 p->needInitState = 0;
745 SRes LzmaDec_DecodeToDic(CLzmaDec *p, SizeT dicLimit, const Byte *src, SizeT *srcLen,
746 ELzmaFinishMode finishMode, ELzmaStatus *status)
748 SizeT inSize = *srcLen;
750 LzmaDec_WriteRem(p, dicLimit);
752 *status = LZMA_STATUS_NOT_SPECIFIED;
754 while (p->remainLen != kMatchSpecLenStart)
758 if (p->needFlush != 0)
760 for (; inSize > 0 && p->tempBufSize < RC_INIT_SIZE; (*srcLen)++, inSize--)
761 p->tempBuf[p->tempBufSize++] = *src++;
762 if (p->tempBufSize < RC_INIT_SIZE)
764 *status = LZMA_STATUS_NEEDS_MORE_INPUT;
767 if (p->tempBuf[0] != 0)
768 return SZ_ERROR_DATA;
770 LzmaDec_InitRc(p, p->tempBuf);
775 if (p->dicPos >= dicLimit)
777 if (p->remainLen == 0 && p->code == 0)
779 *status = LZMA_STATUS_MAYBE_FINISHED_WITHOUT_MARK;
782 if (finishMode == LZMA_FINISH_ANY)
784 *status = LZMA_STATUS_NOT_FINISHED;
787 if (p->remainLen != 0)
789 *status = LZMA_STATUS_NOT_FINISHED;
790 return SZ_ERROR_DATA;
795 if (p->needInitState)
796 LzmaDec_InitStateReal(p);
798 if (p->tempBufSize == 0)
801 const Byte *bufLimit;
802 if (inSize < LZMA_REQUIRED_INPUT_MAX || checkEndMarkNow)
804 int dummyRes = LzmaDec_TryDummy(p, src, inSize);
805 if (dummyRes == DUMMY_ERROR)
807 memcpy(p->tempBuf, src, inSize);
808 p->tempBufSize = (unsigned)inSize;
810 *status = LZMA_STATUS_NEEDS_MORE_INPUT;
813 if (checkEndMarkNow && dummyRes != DUMMY_MATCH)
815 *status = LZMA_STATUS_NOT_FINISHED;
816 return SZ_ERROR_DATA;
821 bufLimit = src + inSize - LZMA_REQUIRED_INPUT_MAX;
823 if (LzmaDec_DecodeReal2(p, dicLimit, bufLimit) != 0)
824 return SZ_ERROR_DATA;
825 processed = (SizeT)(p->buf - src);
826 (*srcLen) += processed;
832 unsigned rem = p->tempBufSize, lookAhead = 0;
833 while (rem < LZMA_REQUIRED_INPUT_MAX && lookAhead < inSize)
834 p->tempBuf[rem++] = src[lookAhead++];
835 p->tempBufSize = rem;
836 if (rem < LZMA_REQUIRED_INPUT_MAX || checkEndMarkNow)
838 int dummyRes = LzmaDec_TryDummy(p, p->tempBuf, rem);
839 if (dummyRes == DUMMY_ERROR)
841 (*srcLen) += lookAhead;
842 *status = LZMA_STATUS_NEEDS_MORE_INPUT;
845 if (checkEndMarkNow && dummyRes != DUMMY_MATCH)
847 *status = LZMA_STATUS_NOT_FINISHED;
848 return SZ_ERROR_DATA;
852 if (LzmaDec_DecodeReal2(p, dicLimit, p->buf) != 0)
853 return SZ_ERROR_DATA;
854 lookAhead -= (rem - (unsigned)(p->buf - p->tempBuf));
855 (*srcLen) += lookAhead;
862 *status = LZMA_STATUS_FINISHED_WITH_MARK;
863 return (p->code == 0) ? SZ_OK : SZ_ERROR_DATA;
866 SRes LzmaDec_DecodeToBuf(CLzmaDec *p, Byte *dest, SizeT *destLen, const Byte *src, SizeT *srcLen, ELzmaFinishMode finishMode, ELzmaStatus *status)
868 SizeT outSize = *destLen;
869 SizeT inSize = *srcLen;
870 *srcLen = *destLen = 0;
873 SizeT inSizeCur = inSize, outSizeCur, dicPos;
874 ELzmaFinishMode curFinishMode;
876 if (p->dicPos == p->dicBufSize)
879 if (outSize > p->dicBufSize - dicPos)
881 outSizeCur = p->dicBufSize;
882 curFinishMode = LZMA_FINISH_ANY;
886 outSizeCur = dicPos + outSize;
887 curFinishMode = finishMode;
890 res = LzmaDec_DecodeToDic(p, outSizeCur, src, &inSizeCur, curFinishMode, status);
893 *srcLen += inSizeCur;
894 outSizeCur = p->dicPos - dicPos;
895 memcpy(dest, p->dic + dicPos, outSizeCur);
897 outSize -= outSizeCur;
898 *destLen += outSizeCur;
901 if (outSizeCur == 0 || outSize == 0)
906 void LzmaDec_FreeProbs(CLzmaDec *p, ISzAlloc *alloc)
908 alloc->Free(alloc, p->probs);
912 static void LzmaDec_FreeDict(CLzmaDec *p, ISzAlloc *alloc)
914 alloc->Free(alloc, p->dic);
918 void LzmaDec_Free(CLzmaDec *p, ISzAlloc *alloc)
920 LzmaDec_FreeProbs(p, alloc);
921 LzmaDec_FreeDict(p, alloc);
924 SRes LzmaProps_Decode(CLzmaProps *p, const Byte *data, unsigned size)
929 if (size < LZMA_PROPS_SIZE)
930 return SZ_ERROR_UNSUPPORTED;
932 dicSize = data[1] | ((UInt32)data[2] << 8) | ((UInt32)data[3] << 16) | ((UInt32)data[4] << 24);
934 if (dicSize < LZMA_DIC_MIN)
935 dicSize = LZMA_DIC_MIN;
936 p->dicSize = dicSize;
939 if (d >= (9 * 5 * 5))
940 return SZ_ERROR_UNSUPPORTED;
950 static SRes LzmaDec_AllocateProbs2(CLzmaDec *p, const CLzmaProps *propNew, ISzAlloc *alloc)
952 UInt32 numProbs = LzmaProps_GetNumProbs(propNew);
953 if (p->probs == 0 || numProbs != p->numProbs)
955 LzmaDec_FreeProbs(p, alloc);
956 p->probs = (CLzmaProb *)alloc->Alloc(alloc, numProbs * sizeof(CLzmaProb));
957 p->numProbs = numProbs;
964 SRes LzmaDec_AllocateProbs(CLzmaDec *p, const Byte *props, unsigned propsSize, ISzAlloc *alloc)
967 RINOK(LzmaProps_Decode(&propNew, props, propsSize));
968 RINOK(LzmaDec_AllocateProbs2(p, &propNew, alloc));
973 SRes LzmaDec_Allocate(CLzmaDec *p, const Byte *props, unsigned propsSize, ISzAlloc *alloc)
977 RINOK(LzmaProps_Decode(&propNew, props, propsSize));
978 RINOK(LzmaDec_AllocateProbs2(p, &propNew, alloc));
979 dicBufSize = propNew.dicSize;
980 if (p->dic == 0 || dicBufSize != p->dicBufSize)
982 LzmaDec_FreeDict(p, alloc);
983 p->dic = (Byte *)alloc->Alloc(alloc, dicBufSize);
986 LzmaDec_FreeProbs(p, alloc);
990 p->dicBufSize = dicBufSize;
995 SRes LzmaDecode(Byte *dest, SizeT *destLen, const Byte *src, SizeT *srcLen,
996 const Byte *propData, unsigned propSize, ELzmaFinishMode finishMode,
997 ELzmaStatus *status, ISzAlloc *alloc)
1001 SizeT inSize = *srcLen;
1002 SizeT outSize = *destLen;
1003 *srcLen = *destLen = 0;
1004 if (inSize < RC_INIT_SIZE)
1005 return SZ_ERROR_INPUT_EOF;
1007 LzmaDec_Construct(&p);
1008 res = LzmaDec_AllocateProbs(&p, propData, propSize, alloc);
1012 p.dicBufSize = outSize;
1017 res = LzmaDec_DecodeToDic(&p, outSize, src, srcLen, finishMode, status);
1019 if (res == SZ_OK && *status == LZMA_STATUS_NEEDS_MORE_INPUT)
1020 res = SZ_ERROR_INPUT_EOF;
1022 (*destLen) = p.dicPos;
1023 LzmaDec_FreeProbs(&p, alloc);