1 /* LzmaDec.c -- LZMA Decoder
2 2009-09-20 : Igor Pavlov : Public domain */
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
17 #define NORMALIZE if (range < kTopValue) { range <<= 8; code = (code << 8) | (*buf++); }
19 #define IF_BIT_0(p) ttt = *(p); NORMALIZE; bound = (range >> kNumBitModelTotalBits) * ttt; if (code < bound)
20 #define UPDATE_0(p) range = bound; *(p) = (CLzmaProb)(ttt + ((kBitModelTotal - ttt) >> kNumMoveBits));
21 #define UPDATE_1(p) range -= bound; code -= bound; *(p) = (CLzmaProb)(ttt - (ttt >> kNumMoveBits));
22 #define GET_BIT2(p, i, A0, A1) IF_BIT_0(p) \
23 { UPDATE_0(p); i = (i + i); A0; } else \
24 { UPDATE_1(p); i = (i + i) + 1; A1; }
25 #define GET_BIT(p, i) GET_BIT2(p, i, ; , ;)
27 #define TREE_GET_BIT(probs, i) { GET_BIT((probs + i), i); }
28 #define TREE_DECODE(probs, limit, i) \
29 { i = 1; do { TREE_GET_BIT(probs, i); } while (i < limit); i -= limit; }
31 /* #define _LZMA_SIZE_OPT */
34 #define TREE_6_DECODE(probs, i) TREE_DECODE(probs, (1 << 6), i)
36 #define TREE_6_DECODE(probs, i) \
38 TREE_GET_BIT(probs, i); \
39 TREE_GET_BIT(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); \
47 #define NORMALIZE_CHECK if (range < kTopValue) { if (buf >= bufLimit) return DUMMY_ERROR; range <<= 8; code = (code << 8) | (*buf++); }
49 #define IF_BIT_0_CHECK(p) ttt = *(p); NORMALIZE_CHECK; bound = (range >> kNumBitModelTotalBits) * ttt; if (code < bound)
50 #define UPDATE_0_CHECK range = bound;
51 #define UPDATE_1_CHECK range -= bound; code -= bound;
52 #define GET_BIT2_CHECK(p, i, A0, A1) IF_BIT_0_CHECK(p) \
53 { UPDATE_0_CHECK; i = (i + i); A0; } else \
54 { UPDATE_1_CHECK; i = (i + i) + 1; A1; }
55 #define GET_BIT_CHECK(p, i) GET_BIT2_CHECK(p, i, ; , ;)
56 #define TREE_DECODE_CHECK(probs, limit, i) \
57 { i = 1; do { GET_BIT_CHECK(probs + i, i) } while (i < limit); i -= limit; }
60 #define kNumPosBitsMax 4
61 #define kNumPosStatesMax (1 << kNumPosBitsMax)
63 #define kLenNumLowBits 3
64 #define kLenNumLowSymbols (1 << kLenNumLowBits)
65 #define kLenNumMidBits 3
66 #define kLenNumMidSymbols (1 << kLenNumMidBits)
67 #define kLenNumHighBits 8
68 #define kLenNumHighSymbols (1 << kLenNumHighBits)
71 #define LenChoice2 (LenChoice + 1)
72 #define LenLow (LenChoice2 + 1)
73 #define LenMid (LenLow + (kNumPosStatesMax << kLenNumLowBits))
74 #define LenHigh (LenMid + (kNumPosStatesMax << kLenNumMidBits))
75 #define kNumLenProbs (LenHigh + kLenNumHighSymbols)
79 #define kNumLitStates 7
81 #define kStartPosModelIndex 4
82 #define kEndPosModelIndex 14
83 #define kNumFullDistances (1 << (kEndPosModelIndex >> 1))
85 #define kNumPosSlotBits 6
86 #define kNumLenToPosStates 4
88 #define kNumAlignBits 4
89 #define kAlignTableSize (1 << kNumAlignBits)
91 #define kMatchMinLen 2
92 #define kMatchSpecLenStart (kMatchMinLen + kLenNumLowSymbols + kLenNumMidSymbols + kLenNumHighSymbols)
95 #define IsRep (IsMatch + (kNumStates << kNumPosBitsMax))
96 #define IsRepG0 (IsRep + kNumStates)
97 #define IsRepG1 (IsRepG0 + kNumStates)
98 #define IsRepG2 (IsRepG1 + kNumStates)
99 #define IsRep0Long (IsRepG2 + kNumStates)
100 #define PosSlot (IsRep0Long + (kNumStates << kNumPosBitsMax))
101 #define SpecPos (PosSlot + (kNumLenToPosStates << kNumPosSlotBits))
102 #define Align (SpecPos + kNumFullDistances - kEndPosModelIndex)
103 #define LenCoder (Align + kAlignTableSize)
104 #define RepLenCoder (LenCoder + kNumLenProbs)
105 #define Literal (RepLenCoder + kNumLenProbs)
107 #define LZMA_BASE_SIZE 1846
108 #define LZMA_LIT_SIZE 768
110 #define LzmaProps_GetNumProbs(p) ((UInt32)LZMA_BASE_SIZE + (LZMA_LIT_SIZE << ((p)->lc + (p)->lp)))
112 #if Literal != LZMA_BASE_SIZE
116 #define LZMA_DIC_MIN (1 << 12)
118 /* First LZMA-symbol is always decoded.
119 And it decodes new LZMA-symbols while (buf < bufLimit), but "buf" is without last normalization
123 SZ_ERROR_DATA - Error
125 < kMatchSpecLenStart : normal remain
126 = kMatchSpecLenStart : finished
127 = kMatchSpecLenStart + 1 : Flush marker
128 = kMatchSpecLenStart + 2 : State Init Marker
131 static int MY_FAST_CALL
LzmaDec_DecodeReal(CLzmaDec
*p
, SizeT limit
, const Byte
*bufLimit
)
133 CLzmaProb
*probs
= p
->probs
;
135 unsigned state
= p
->state
;
136 UInt32 rep0
= p
->reps
[0], rep1
= p
->reps
[1], rep2
= p
->reps
[2], rep3
= p
->reps
[3];
137 unsigned pbMask
= ((unsigned)1 << (p
->prop
.pb
)) - 1;
138 unsigned lpMask
= ((unsigned)1 << (p
->prop
.lp
)) - 1;
139 unsigned lc
= p
->prop
.lc
;
142 SizeT dicBufSize
= p
->dicBufSize
;
143 SizeT dicPos
= p
->dicPos
;
145 UInt32 processedPos
= p
->processedPos
;
146 UInt32 checkDicSize
= p
->checkDicSize
;
149 const Byte
*buf
= p
->buf
;
150 UInt32 range
= p
->range
;
151 UInt32 code
= p
->code
;
158 unsigned posState
= processedPos
& pbMask
;
160 prob
= probs
+ IsMatch
+ (state
<< kNumPosBitsMax
) + posState
;
165 prob
= probs
+ Literal
;
166 if (checkDicSize
!= 0 || processedPos
!= 0)
167 prob
+= (LZMA_LIT_SIZE
* (((processedPos
& lpMask
) << lc
) +
168 (dic
[(dicPos
== 0 ? dicBufSize
: dicPos
) - 1] >> (8 - lc
))));
170 if (state
< kNumLitStates
)
172 state
-= (state
< 4) ? state
: 3;
174 do { GET_BIT(prob
+ symbol
, symbol
) } while (symbol
< 0x100);
178 unsigned matchByte
= p
->dic
[(dicPos
- rep0
) + ((dicPos
< rep0
) ? dicBufSize
: 0)];
179 unsigned offs
= 0x100;
180 state
-= (state
< 10) ? 3 : 6;
187 bit
= (matchByte
& offs
);
188 probLit
= prob
+ offs
+ bit
+ symbol
;
189 GET_BIT2(probLit
, symbol
, offs
&= ~bit
, offs
&= bit
)
191 while (symbol
< 0x100);
193 dic
[dicPos
++] = (Byte
)symbol
;
200 prob
= probs
+ IsRep
+ state
;
205 prob
= probs
+ LenCoder
;
210 if (checkDicSize
== 0 && processedPos
== 0)
211 return SZ_ERROR_DATA
;
212 prob
= probs
+ IsRepG0
+ state
;
216 prob
= probs
+ IsRep0Long
+ (state
<< kNumPosBitsMax
) + posState
;
220 dic
[dicPos
] = dic
[(dicPos
- rep0
) + ((dicPos
< rep0
) ? dicBufSize
: 0)];
223 state
= state
< kNumLitStates
? 9 : 11;
232 prob
= probs
+ IsRepG1
+ state
;
241 prob
= probs
+ IsRepG2
+ state
;
258 state
= state
< kNumLitStates
? 8 : 11;
259 prob
= probs
+ RepLenCoder
;
262 unsigned limit
, offset
;
263 CLzmaProb
*probLen
= prob
+ LenChoice
;
267 probLen
= prob
+ LenLow
+ (posState
<< kLenNumLowBits
);
269 limit
= (1 << kLenNumLowBits
);
274 probLen
= prob
+ LenChoice2
;
278 probLen
= prob
+ LenMid
+ (posState
<< kLenNumMidBits
);
279 offset
= kLenNumLowSymbols
;
280 limit
= (1 << kLenNumMidBits
);
285 probLen
= prob
+ LenHigh
;
286 offset
= kLenNumLowSymbols
+ kLenNumMidSymbols
;
287 limit
= (1 << kLenNumHighBits
);
290 TREE_DECODE(probLen
, limit
, len
);
294 if (state
>= kNumStates
)
297 prob
= probs
+ PosSlot
+
298 ((len
< kNumLenToPosStates
? len
: kNumLenToPosStates
- 1) << kNumPosSlotBits
);
299 TREE_6_DECODE(prob
, distance
);
300 if (distance
>= kStartPosModelIndex
)
302 unsigned posSlot
= (unsigned)distance
;
303 int numDirectBits
= (int)(((distance
>> 1) - 1));
304 distance
= (2 | (distance
& 1));
305 if (posSlot
< kEndPosModelIndex
)
307 distance
<<= numDirectBits
;
308 prob
= probs
+ SpecPos
+ distance
- posSlot
- 1;
314 GET_BIT2(prob
+ i
, i
, ; , distance
|= mask
);
317 while (--numDirectBits
!= 0);
322 numDirectBits
-= kNumAlignBits
;
331 t
= (0 - ((UInt32
)code
>> 31)); /* (UInt32)((Int32)code >> 31) */
332 distance
= (distance
<< 1) + (t
+ 1);
344 while (--numDirectBits
!= 0);
345 prob
= probs
+ Align
;
346 distance
<<= kNumAlignBits
;
349 GET_BIT2(prob
+ i
, i
, ; , distance
|= 1);
350 GET_BIT2(prob
+ i
, i
, ; , distance
|= 2);
351 GET_BIT2(prob
+ i
, i
, ; , distance
|= 4);
352 GET_BIT2(prob
+ i
, i
, ; , distance
|= 8);
354 if (distance
== (UInt32
)0xFFFFFFFF)
356 len
+= kMatchSpecLenStart
;
366 if (checkDicSize
== 0)
368 if (distance
>= processedPos
)
369 return SZ_ERROR_DATA
;
371 else if (distance
>= checkDicSize
)
372 return SZ_ERROR_DATA
;
373 state
= (state
< kNumStates
+ kNumLitStates
) ? kNumLitStates
: kNumLitStates
+ 3;
379 return SZ_ERROR_DATA
;
381 SizeT rem
= limit
- dicPos
;
382 unsigned curLen
= ((rem
< len
) ? (unsigned)rem
: len
);
383 SizeT pos
= (dicPos
- rep0
) + ((dicPos
< rep0
) ? dicBufSize
: 0);
385 processedPos
+= curLen
;
388 if (pos
+ curLen
<= dicBufSize
)
390 Byte
*dest
= dic
+ dicPos
;
391 ptrdiff_t src
= (ptrdiff_t)pos
- (ptrdiff_t)dicPos
;
392 const Byte
*lim
= dest
+ curLen
;
395 *(dest
) = (Byte
)*(dest
+ src
);
396 while (++dest
!= lim
);
402 dic
[dicPos
++] = dic
[pos
];
403 if (++pos
== dicBufSize
)
406 while (--curLen
!= 0);
411 while (dicPos
< limit
&& buf
< bufLimit
);
418 p
->processedPos
= processedPos
;
428 static void MY_FAST_CALL
LzmaDec_WriteRem(CLzmaDec
*p
, SizeT limit
)
430 if (p
->remainLen
!= 0 && p
->remainLen
< kMatchSpecLenStart
)
433 SizeT dicPos
= p
->dicPos
;
434 SizeT dicBufSize
= p
->dicBufSize
;
435 unsigned len
= p
->remainLen
;
436 UInt32 rep0
= p
->reps
[0];
437 if (limit
- dicPos
< len
)
438 len
= (unsigned)(limit
- dicPos
);
440 if (p
->checkDicSize
== 0 && p
->prop
.dicSize
- p
->processedPos
<= len
)
441 p
->checkDicSize
= p
->prop
.dicSize
;
443 p
->processedPos
+= len
;
447 dic
[dicPos
] = dic
[(dicPos
- rep0
) + ((dicPos
< rep0
) ? dicBufSize
: 0)];
454 static int MY_FAST_CALL
LzmaDec_DecodeReal2(CLzmaDec
*p
, SizeT limit
, const Byte
*bufLimit
)
458 SizeT limit2
= limit
;
459 if (p
->checkDicSize
== 0)
461 UInt32 rem
= p
->prop
.dicSize
- p
->processedPos
;
462 if (limit
- p
->dicPos
> rem
)
463 limit2
= p
->dicPos
+ rem
;
465 RINOK(LzmaDec_DecodeReal(p
, limit2
, bufLimit
));
466 if (p
->processedPos
>= p
->prop
.dicSize
)
467 p
->checkDicSize
= p
->prop
.dicSize
;
468 LzmaDec_WriteRem(p
, limit
);
470 while (p
->dicPos
< limit
&& p
->buf
< bufLimit
&& p
->remainLen
< kMatchSpecLenStart
);
472 if (p
->remainLen
> kMatchSpecLenStart
)
474 p
->remainLen
= kMatchSpecLenStart
;
481 DUMMY_ERROR
, /* unexpected end of input stream */
487 static ELzmaDummy
LzmaDec_TryDummy(const CLzmaDec
*p
, const Byte
*buf
, SizeT inSize
)
489 UInt32 range
= p
->range
;
490 UInt32 code
= p
->code
;
491 const Byte
*bufLimit
= buf
+ inSize
;
492 CLzmaProb
*probs
= p
->probs
;
493 unsigned state
= p
->state
;
500 unsigned posState
= (p
->processedPos
) & ((1 << p
->prop
.pb
) - 1);
502 prob
= probs
+ IsMatch
+ (state
<< kNumPosBitsMax
) + posState
;
507 /* if (bufLimit - buf >= 7) return DUMMY_LIT; */
509 prob
= probs
+ Literal
;
510 if (p
->checkDicSize
!= 0 || p
->processedPos
!= 0)
511 prob
+= (LZMA_LIT_SIZE
*
512 ((((p
->processedPos
) & ((1 << (p
->prop
.lp
)) - 1)) << p
->prop
.lc
) +
513 (p
->dic
[(p
->dicPos
== 0 ? p
->dicBufSize
: p
->dicPos
) - 1] >> (8 - p
->prop
.lc
))));
515 if (state
< kNumLitStates
)
518 do { GET_BIT_CHECK(prob
+ symbol
, symbol
) } while (symbol
< 0x100);
522 unsigned matchByte
= p
->dic
[p
->dicPos
- p
->reps
[0] +
523 ((p
->dicPos
< p
->reps
[0]) ? p
->dicBufSize
: 0)];
524 unsigned offs
= 0x100;
531 bit
= (matchByte
& offs
);
532 probLit
= prob
+ offs
+ bit
+ symbol
;
533 GET_BIT2_CHECK(probLit
, symbol
, offs
&= ~bit
, offs
&= bit
)
535 while (symbol
< 0x100);
544 prob
= probs
+ IsRep
+ state
;
549 prob
= probs
+ LenCoder
;
556 prob
= probs
+ IsRepG0
+ state
;
560 prob
= probs
+ IsRep0Long
+ (state
<< kNumPosBitsMax
) + posState
;
575 prob
= probs
+ IsRepG1
+ state
;
583 prob
= probs
+ IsRepG2
+ state
;
595 prob
= probs
+ RepLenCoder
;
598 unsigned limit
, offset
;
599 CLzmaProb
*probLen
= prob
+ LenChoice
;
600 IF_BIT_0_CHECK(probLen
)
603 probLen
= prob
+ LenLow
+ (posState
<< kLenNumLowBits
);
605 limit
= 1 << kLenNumLowBits
;
610 probLen
= prob
+ LenChoice2
;
611 IF_BIT_0_CHECK(probLen
)
614 probLen
= prob
+ LenMid
+ (posState
<< kLenNumMidBits
);
615 offset
= kLenNumLowSymbols
;
616 limit
= 1 << kLenNumMidBits
;
621 probLen
= prob
+ LenHigh
;
622 offset
= kLenNumLowSymbols
+ kLenNumMidSymbols
;
623 limit
= 1 << kLenNumHighBits
;
626 TREE_DECODE_CHECK(probLen
, limit
, len
);
633 prob
= probs
+ PosSlot
+
634 ((len
< kNumLenToPosStates
? len
: kNumLenToPosStates
- 1) <<
636 TREE_DECODE_CHECK(prob
, 1 << kNumPosSlotBits
, posSlot
);
637 if (posSlot
>= kStartPosModelIndex
)
639 int numDirectBits
= ((posSlot
>> 1) - 1);
641 /* if (bufLimit - buf >= 8) return DUMMY_MATCH; */
643 if (posSlot
< kEndPosModelIndex
)
645 prob
= probs
+ SpecPos
+ ((2 | (posSlot
& 1)) << numDirectBits
) - posSlot
- 1;
649 numDirectBits
-= kNumAlignBits
;
654 code
-= range
& (((code
- range
) >> 31) - 1);
655 /* if (code >= range) code -= range; */
657 while (--numDirectBits
!= 0);
658 prob
= probs
+ Align
;
659 numDirectBits
= kNumAlignBits
;
665 GET_BIT_CHECK(prob
+ i
, i
);
667 while (--numDirectBits
!= 0);
678 static void LzmaDec_InitRc(CLzmaDec
*p
, const Byte
*data
)
680 p
->code
= ((UInt32
)data
[1] << 24) | ((UInt32
)data
[2] << 16) | ((UInt32
)data
[3] << 8) | ((UInt32
)data
[4]);
681 p
->range
= 0xFFFFFFFF;
685 void LzmaDec_InitDicAndState(CLzmaDec
*p
, Bool initDic
, Bool initState
)
695 p
->needInitState
= 1;
698 p
->needInitState
= 1;
701 void LzmaDec_Init(CLzmaDec
*p
)
704 LzmaDec_InitDicAndState(p
, True
, True
);
707 static void LzmaDec_InitStateReal(CLzmaDec
*p
)
709 UInt32 numProbs
= Literal
+ ((UInt32
)LZMA_LIT_SIZE
<< (p
->prop
.lc
+ p
->prop
.lp
));
711 CLzmaProb
*probs
= p
->probs
;
712 for (i
= 0; i
< numProbs
; i
++)
713 probs
[i
] = kBitModelTotal
>> 1;
714 p
->reps
[0] = p
->reps
[1] = p
->reps
[2] = p
->reps
[3] = 1;
716 p
->needInitState
= 0;
719 SRes
LzmaDec_DecodeToDic(CLzmaDec
*p
, SizeT dicLimit
, const Byte
*src
, SizeT
*srcLen
,
720 ELzmaFinishMode finishMode
, ELzmaStatus
*status
)
722 SizeT inSize
= *srcLen
;
724 LzmaDec_WriteRem(p
, dicLimit
);
726 *status
= LZMA_STATUS_NOT_SPECIFIED
;
728 while (p
->remainLen
!= kMatchSpecLenStart
)
732 if (p
->needFlush
!= 0)
734 for (; inSize
> 0 && p
->tempBufSize
< RC_INIT_SIZE
; (*srcLen
)++, inSize
--)
735 p
->tempBuf
[p
->tempBufSize
++] = *src
++;
736 if (p
->tempBufSize
< RC_INIT_SIZE
)
738 *status
= LZMA_STATUS_NEEDS_MORE_INPUT
;
741 if (p
->tempBuf
[0] != 0)
742 return SZ_ERROR_DATA
;
744 LzmaDec_InitRc(p
, p
->tempBuf
);
749 if (p
->dicPos
>= dicLimit
)
751 if (p
->remainLen
== 0 && p
->code
== 0)
753 *status
= LZMA_STATUS_MAYBE_FINISHED_WITHOUT_MARK
;
756 if (finishMode
== LZMA_FINISH_ANY
)
758 *status
= LZMA_STATUS_NOT_FINISHED
;
761 if (p
->remainLen
!= 0)
763 *status
= LZMA_STATUS_NOT_FINISHED
;
764 return SZ_ERROR_DATA
;
769 if (p
->needInitState
)
770 LzmaDec_InitStateReal(p
);
772 if (p
->tempBufSize
== 0)
775 const Byte
*bufLimit
;
776 if (inSize
< LZMA_REQUIRED_INPUT_MAX
|| checkEndMarkNow
)
778 int dummyRes
= LzmaDec_TryDummy(p
, src
, inSize
);
779 if (dummyRes
== DUMMY_ERROR
)
781 memcpy(p
->tempBuf
, src
, inSize
);
782 p
->tempBufSize
= (unsigned)inSize
;
784 *status
= LZMA_STATUS_NEEDS_MORE_INPUT
;
787 if (checkEndMarkNow
&& dummyRes
!= DUMMY_MATCH
)
789 *status
= LZMA_STATUS_NOT_FINISHED
;
790 return SZ_ERROR_DATA
;
795 bufLimit
= src
+ inSize
- LZMA_REQUIRED_INPUT_MAX
;
797 if (LzmaDec_DecodeReal2(p
, dicLimit
, bufLimit
) != 0)
798 return SZ_ERROR_DATA
;
799 processed
= (SizeT
)(p
->buf
- src
);
800 (*srcLen
) += processed
;
806 unsigned rem
= p
->tempBufSize
, lookAhead
= 0;
807 while (rem
< LZMA_REQUIRED_INPUT_MAX
&& lookAhead
< inSize
)
808 p
->tempBuf
[rem
++] = src
[lookAhead
++];
809 p
->tempBufSize
= rem
;
810 if (rem
< LZMA_REQUIRED_INPUT_MAX
|| checkEndMarkNow
)
812 int dummyRes
= LzmaDec_TryDummy(p
, p
->tempBuf
, rem
);
813 if (dummyRes
== DUMMY_ERROR
)
815 (*srcLen
) += lookAhead
;
816 *status
= LZMA_STATUS_NEEDS_MORE_INPUT
;
819 if (checkEndMarkNow
&& dummyRes
!= DUMMY_MATCH
)
821 *status
= LZMA_STATUS_NOT_FINISHED
;
822 return SZ_ERROR_DATA
;
826 if (LzmaDec_DecodeReal2(p
, dicLimit
, p
->buf
) != 0)
827 return SZ_ERROR_DATA
;
828 lookAhead
-= (rem
- (unsigned)(p
->buf
- p
->tempBuf
));
829 (*srcLen
) += lookAhead
;
836 *status
= LZMA_STATUS_FINISHED_WITH_MARK
;
837 return (p
->code
== 0) ? SZ_OK
: SZ_ERROR_DATA
;
840 SRes
LzmaDec_DecodeToBuf(CLzmaDec
*p
, Byte
*dest
, SizeT
*destLen
, const Byte
*src
, SizeT
*srcLen
, ELzmaFinishMode finishMode
, ELzmaStatus
*status
)
842 SizeT outSize
= *destLen
;
843 SizeT inSize
= *srcLen
;
844 *srcLen
= *destLen
= 0;
847 SizeT inSizeCur
= inSize
, outSizeCur
, dicPos
;
848 ELzmaFinishMode curFinishMode
;
850 if (p
->dicPos
== p
->dicBufSize
)
853 if (outSize
> p
->dicBufSize
- dicPos
)
855 outSizeCur
= p
->dicBufSize
;
856 curFinishMode
= LZMA_FINISH_ANY
;
860 outSizeCur
= dicPos
+ outSize
;
861 curFinishMode
= finishMode
;
864 res
= LzmaDec_DecodeToDic(p
, outSizeCur
, src
, &inSizeCur
, curFinishMode
, status
);
867 *srcLen
+= inSizeCur
;
868 outSizeCur
= p
->dicPos
- dicPos
;
869 memcpy(dest
, p
->dic
+ dicPos
, outSizeCur
);
871 outSize
-= outSizeCur
;
872 *destLen
+= outSizeCur
;
875 if (outSizeCur
== 0 || outSize
== 0)
880 void LzmaDec_FreeProbs(CLzmaDec
*p
, ISzAlloc
*alloc
)
882 alloc
->Free(alloc
, p
->probs
);
886 static void LzmaDec_FreeDict(CLzmaDec
*p
, ISzAlloc
*alloc
)
888 alloc
->Free(alloc
, p
->dic
);
892 void LzmaDec_Free(CLzmaDec
*p
, ISzAlloc
*alloc
)
894 LzmaDec_FreeProbs(p
, alloc
);
895 LzmaDec_FreeDict(p
, alloc
);
898 SRes
LzmaProps_Decode(CLzmaProps
*p
, const Byte
*data
, unsigned size
)
903 if (size
< LZMA_PROPS_SIZE
)
904 return SZ_ERROR_UNSUPPORTED
;
906 dicSize
= data
[1] | ((UInt32
)data
[2] << 8) | ((UInt32
)data
[3] << 16) | ((UInt32
)data
[4] << 24);
908 if (dicSize
< LZMA_DIC_MIN
)
909 dicSize
= LZMA_DIC_MIN
;
910 p
->dicSize
= dicSize
;
913 if (d
>= (9 * 5 * 5))
914 return SZ_ERROR_UNSUPPORTED
;
924 static SRes
LzmaDec_AllocateProbs2(CLzmaDec
*p
, const CLzmaProps
*propNew
, ISzAlloc
*alloc
)
926 UInt32 numProbs
= LzmaProps_GetNumProbs(propNew
);
927 if (p
->probs
== 0 || numProbs
!= p
->numProbs
)
929 LzmaDec_FreeProbs(p
, alloc
);
930 p
->probs
= (CLzmaProb
*)alloc
->Alloc(alloc
, numProbs
* sizeof(CLzmaProb
));
931 p
->numProbs
= numProbs
;
938 SRes
LzmaDec_AllocateProbs(CLzmaDec
*p
, const Byte
*props
, unsigned propsSize
, ISzAlloc
*alloc
)
941 RINOK(LzmaProps_Decode(&propNew
, props
, propsSize
));
942 RINOK(LzmaDec_AllocateProbs2(p
, &propNew
, alloc
));
947 SRes
LzmaDec_Allocate(CLzmaDec
*p
, const Byte
*props
, unsigned propsSize
, ISzAlloc
*alloc
)
951 RINOK(LzmaProps_Decode(&propNew
, props
, propsSize
));
952 RINOK(LzmaDec_AllocateProbs2(p
, &propNew
, alloc
));
953 dicBufSize
= propNew
.dicSize
;
954 if (p
->dic
== 0 || dicBufSize
!= p
->dicBufSize
)
956 LzmaDec_FreeDict(p
, alloc
);
957 p
->dic
= (Byte
*)alloc
->Alloc(alloc
, dicBufSize
);
960 LzmaDec_FreeProbs(p
, alloc
);
964 p
->dicBufSize
= dicBufSize
;
969 SRes
LzmaDecode(Byte
*dest
, SizeT
*destLen
, const Byte
*src
, SizeT
*srcLen
,
970 const Byte
*propData
, unsigned propSize
, ELzmaFinishMode finishMode
,
971 ELzmaStatus
*status
, ISzAlloc
*alloc
)
975 SizeT inSize
= *srcLen
;
976 SizeT outSize
= *destLen
;
977 *srcLen
= *destLen
= 0;
978 if (inSize
< RC_INIT_SIZE
)
979 return SZ_ERROR_INPUT_EOF
;
981 LzmaDec_Construct(&p
);
982 res
= LzmaDec_AllocateProbs(&p
, propData
, propSize
, alloc
);
986 p
.dicBufSize
= outSize
;
991 res
= LzmaDec_DecodeToDic(&p
, outSize
, src
, srcLen
, finishMode
, status
);
993 if (res
== SZ_OK
&& *status
== LZMA_STATUS_NEEDS_MORE_INPUT
)
994 res
= SZ_ERROR_INPUT_EOF
;
996 (*destLen
) = p
.dicPos
;
997 LzmaDec_FreeProbs(&p
, alloc
);