2 * Copyright 2009 Sun Microsystems, Inc. All rights reserved.
3 * Use is subject to license terms.
6 /* LzmaDec.c -- LZMA Decoder
7 2008-11-06 : Igor Pavlov : Public domain */
15 #define kNumTopBits 24
16 #define kTopValue ((UInt32)1 << kNumTopBits)
18 #define kNumBitModelTotalBits 11
19 #define kBitModelTotal (1 << kNumBitModelTotalBits)
20 #define kNumMoveBits 5
22 #define RC_INIT_SIZE 5
24 #define NORMALIZE if (range < kTopValue) { range <<= 8; code = (code << 8) | (*buf++); }
26 #define IF_BIT_0(p) ttt = *(p); NORMALIZE; bound = (range >> kNumBitModelTotalBits) * ttt; if (code < bound)
27 #define UPDATE_0(p) range = bound; *(p) = (CLzmaProb)(ttt + ((kBitModelTotal - ttt) >> kNumMoveBits));
28 #define UPDATE_1(p) range -= bound; code -= bound; *(p) = (CLzmaProb)(ttt - (ttt >> kNumMoveBits));
29 #define GET_BIT2(p, i, A0, A1) IF_BIT_0(p) \
30 { UPDATE_0(p); i = (i + i); A0; } else \
31 { UPDATE_1(p); i = (i + i) + 1; A1; }
32 #define GET_BIT(p, i) GET_BIT2(p, i, ; , ;)
34 #define TREE_GET_BIT(probs, i) { GET_BIT((probs + i), i); }
35 #define TREE_DECODE(probs, limit, i) \
36 { i = 1; do { TREE_GET_BIT(probs, i); } while (i < limit); i -= limit; }
38 /* #define _LZMA_SIZE_OPT */
41 #define TREE_6_DECODE(probs, i) TREE_DECODE(probs, (1 << 6), i)
43 #define TREE_6_DECODE(probs, i) \
45 TREE_GET_BIT(probs, i); \
46 TREE_GET_BIT(probs, i); \
47 TREE_GET_BIT(probs, i); \
48 TREE_GET_BIT(probs, i); \
49 TREE_GET_BIT(probs, i); \
50 TREE_GET_BIT(probs, i); \
54 #define NORMALIZE_CHECK if (range < kTopValue) { if (buf >= bufLimit) return DUMMY_ERROR; range <<= 8; code = (code << 8) | (*buf++); }
56 #define IF_BIT_0_CHECK(p) ttt = *(p); NORMALIZE_CHECK; bound = (range >> kNumBitModelTotalBits) * ttt; if (code < bound)
57 #define UPDATE_0_CHECK range = bound;
58 #define UPDATE_1_CHECK range -= bound; code -= bound;
59 #define GET_BIT2_CHECK(p, i, A0, A1) IF_BIT_0_CHECK(p) \
60 { UPDATE_0_CHECK; i = (i + i); A0; } else \
61 { UPDATE_1_CHECK; i = (i + i) + 1; A1; }
62 #define GET_BIT_CHECK(p, i) GET_BIT2_CHECK(p, i, ; , ;)
63 #define TREE_DECODE_CHECK(probs, limit, i) \
64 { i = 1; do { GET_BIT_CHECK(probs + i, i) } while (i < limit); i -= limit; }
67 #define kNumPosBitsMax 4
68 #define kNumPosStatesMax (1 << kNumPosBitsMax)
70 #define kLenNumLowBits 3
71 #define kLenNumLowSymbols (1 << kLenNumLowBits)
72 #define kLenNumMidBits 3
73 #define kLenNumMidSymbols (1 << kLenNumMidBits)
74 #define kLenNumHighBits 8
75 #define kLenNumHighSymbols (1 << kLenNumHighBits)
78 #define LenChoice2 (LenChoice + 1)
79 #define LenLow (LenChoice2 + 1)
80 #define LenMid (LenLow + (kNumPosStatesMax << kLenNumLowBits))
81 #define LenHigh (LenMid + (kNumPosStatesMax << kLenNumMidBits))
82 #define kNumLenProbs (LenHigh + kLenNumHighSymbols)
86 #define kNumLitStates 7
88 #define kStartPosModelIndex 4
89 #define kEndPosModelIndex 14
90 #define kNumFullDistances (1 << (kEndPosModelIndex >> 1))
92 #define kNumPosSlotBits 6
93 #define kNumLenToPosStates 4
95 #define kNumAlignBits 4
96 #define kAlignTableSize (1 << kNumAlignBits)
98 #define kMatchMinLen 2
99 #define kMatchSpecLenStart (kMatchMinLen + kLenNumLowSymbols + kLenNumMidSymbols + kLenNumHighSymbols)
102 #define IsRep (IsMatch + (kNumStates << kNumPosBitsMax))
103 #define IsRepG0 (IsRep + kNumStates)
104 #define IsRepG1 (IsRepG0 + kNumStates)
105 #define IsRepG2 (IsRepG1 + kNumStates)
106 #define IsRep0Long (IsRepG2 + kNumStates)
107 #define PosSlot (IsRep0Long + (kNumStates << kNumPosBitsMax))
108 #define SpecPos (PosSlot + (kNumLenToPosStates << kNumPosSlotBits))
109 #define Align (SpecPos + kNumFullDistances - kEndPosModelIndex)
110 #define LenCoder (Align + kAlignTableSize)
111 #define RepLenCoder (LenCoder + kNumLenProbs)
112 #define Literal (RepLenCoder + kNumLenProbs)
114 #define LZMA_BASE_SIZE 1846
115 #define LZMA_LIT_SIZE 768
117 #define LzmaProps_GetNumProbs(p) ((UInt32)LZMA_BASE_SIZE + (LZMA_LIT_SIZE << ((p)->lc + (p)->lp)))
119 #if Literal != LZMA_BASE_SIZE
123 static const Byte kLiteralNextStates
[kNumStates
* 2] =
125 0, 0, 0, 0, 1, 2, 3, 4, 5, 6, 4, 5,
126 7, 7, 7, 7, 7, 7, 7, 10, 10, 10, 10, 10
130 extern void *memcpy(void *, const void *, size_t);
133 #define LZMA_DIC_MIN (1 << 12)
135 /* First LZMA-symbol is always decoded.
136 And it decodes new LZMA-symbols while (buf < bufLimit), but "buf" is without last normalization
140 SZ_ERROR_DATA - Error
142 < kMatchSpecLenStart : normal remain
143 = kMatchSpecLenStart : finished
144 = kMatchSpecLenStart + 1 : Flush marker
145 = kMatchSpecLenStart + 2 : State Init Marker
148 static int MY_FAST_CALL
LzmaDec_DecodeReal(CLzmaDec
*p
, SizeT limit
, const Byte
*bufLimit
)
150 CLzmaProb
*probs
= p
->probs
;
152 unsigned state
= p
->state
;
153 UInt32 rep0
= p
->reps
[0], rep1
= p
->reps
[1], rep2
= p
->reps
[2], rep3
= p
->reps
[3];
154 unsigned pbMask
= ((unsigned)1 << (p
->prop
.pb
)) - 1;
155 unsigned lpMask
= ((unsigned)1 << (p
->prop
.lp
)) - 1;
156 unsigned lc
= p
->prop
.lc
;
159 SizeT dicBufSize
= p
->dicBufSize
;
160 SizeT dicPos
= p
->dicPos
;
162 UInt32 processedPos
= p
->processedPos
;
163 UInt32 checkDicSize
= p
->checkDicSize
;
166 const Byte
*buf
= p
->buf
;
167 UInt32 range
= p
->range
;
168 UInt32 code
= p
->code
;
175 unsigned posState
= processedPos
& pbMask
;
177 prob
= probs
+ IsMatch
+ (state
<< kNumPosBitsMax
) + posState
;
182 prob
= probs
+ Literal
;
183 if (checkDicSize
!= 0 || processedPos
!= 0)
184 prob
+= (LZMA_LIT_SIZE
* (((processedPos
& lpMask
) << lc
) +
185 (dic
[(dicPos
== 0 ? dicBufSize
: dicPos
) - 1] >> (8 - lc
))));
187 if (state
< kNumLitStates
)
190 do { GET_BIT(prob
+ symbol
, symbol
) } while (symbol
< 0x100);
194 unsigned matchByte
= p
->dic
[(dicPos
- rep0
) + ((dicPos
< rep0
) ? dicBufSize
: 0)];
195 unsigned offs
= 0x100;
202 bit
= (matchByte
& offs
);
203 probLit
= prob
+ offs
+ bit
+ symbol
;
204 GET_BIT2(probLit
, symbol
, offs
&= ~bit
, offs
&= bit
)
206 while (symbol
< 0x100);
208 dic
[dicPos
++] = (Byte
)symbol
;
211 state
= kLiteralNextStates
[state
];
212 /* if (state < 4) state = 0; else if (state < 10) state -= 3; else state -= 6; */
218 prob
= probs
+ IsRep
+ state
;
223 prob
= probs
+ LenCoder
;
228 if (checkDicSize
== 0 && processedPos
== 0)
229 return SZ_ERROR_DATA
;
230 prob
= probs
+ IsRepG0
+ state
;
234 prob
= probs
+ IsRep0Long
+ (state
<< kNumPosBitsMax
) + posState
;
238 dic
[dicPos
] = dic
[(dicPos
- rep0
) + ((dicPos
< rep0
) ? dicBufSize
: 0)];
241 state
= state
< kNumLitStates
? 9 : 11;
250 prob
= probs
+ IsRepG1
+ state
;
259 prob
= probs
+ IsRepG2
+ state
;
276 state
= state
< kNumLitStates
? 8 : 11;
277 prob
= probs
+ RepLenCoder
;
280 unsigned limit
, offset
;
281 CLzmaProb
*probLen
= prob
+ LenChoice
;
285 probLen
= prob
+ LenLow
+ (posState
<< kLenNumLowBits
);
287 limit
= (1 << kLenNumLowBits
);
292 probLen
= prob
+ LenChoice2
;
296 probLen
= prob
+ LenMid
+ (posState
<< kLenNumMidBits
);
297 offset
= kLenNumLowSymbols
;
298 limit
= (1 << kLenNumMidBits
);
303 probLen
= prob
+ LenHigh
;
304 offset
= kLenNumLowSymbols
+ kLenNumMidSymbols
;
305 limit
= (1 << kLenNumHighBits
);
308 TREE_DECODE(probLen
, limit
, len
);
312 if (state
>= kNumStates
)
315 prob
= probs
+ PosSlot
+
316 ((len
< kNumLenToPosStates
? len
: kNumLenToPosStates
- 1) << kNumPosSlotBits
);
317 TREE_6_DECODE(prob
, distance
);
318 if (distance
>= kStartPosModelIndex
)
320 unsigned posSlot
= (unsigned)distance
;
321 int numDirectBits
= (int)(((distance
>> 1) - 1));
322 distance
= (2 | (distance
& 1));
323 if (posSlot
< kEndPosModelIndex
)
325 distance
<<= numDirectBits
;
326 prob
= probs
+ SpecPos
+ distance
- posSlot
- 1;
332 GET_BIT2(prob
+ i
, i
, ; , distance
|= mask
);
335 while (--numDirectBits
!= 0);
340 numDirectBits
-= kNumAlignBits
;
349 t
= (0 - ((UInt32
)code
>> 31)); /* (UInt32)((Int32)code >> 31) */
350 distance
= (distance
<< 1) + (t
+ 1);
362 while (--numDirectBits
!= 0);
363 prob
= probs
+ Align
;
364 distance
<<= kNumAlignBits
;
367 GET_BIT2(prob
+ i
, i
, ; , distance
|= 1);
368 GET_BIT2(prob
+ i
, i
, ; , distance
|= 2);
369 GET_BIT2(prob
+ i
, i
, ; , distance
|= 4);
370 GET_BIT2(prob
+ i
, i
, ; , distance
|= 8);
372 if (distance
== (UInt32
)0xFFFFFFFF)
374 len
+= kMatchSpecLenStart
;
384 if (checkDicSize
== 0)
386 if (distance
>= processedPos
)
387 return SZ_ERROR_DATA
;
389 else if (distance
>= checkDicSize
)
390 return SZ_ERROR_DATA
;
391 state
= (state
< kNumStates
+ kNumLitStates
) ? kNumLitStates
: kNumLitStates
+ 3;
392 /* state = kLiteralNextStates[state]; */
398 return SZ_ERROR_DATA
;
400 SizeT rem
= limit
- dicPos
;
401 unsigned curLen
= ((rem
< len
) ? (unsigned)rem
: len
);
402 SizeT pos
= (dicPos
- rep0
) + ((dicPos
< rep0
) ? dicBufSize
: 0);
404 processedPos
+= curLen
;
407 if (pos
+ curLen
<= dicBufSize
)
409 Byte
*dest
= dic
+ dicPos
;
410 ptrdiff_t src
= (ptrdiff_t)pos
- (ptrdiff_t)dicPos
;
411 const Byte
*lim
= dest
+ curLen
;
414 *(dest
) = (Byte
)*(dest
+ src
);
415 while (++dest
!= lim
);
421 dic
[dicPos
++] = dic
[pos
];
422 if (++pos
== dicBufSize
)
425 while (--curLen
!= 0);
430 while (dicPos
< limit
&& buf
< bufLimit
);
437 p
->processedPos
= processedPos
;
447 static void MY_FAST_CALL
LzmaDec_WriteRem(CLzmaDec
*p
, SizeT limit
)
449 if (p
->remainLen
!= 0 && p
->remainLen
< kMatchSpecLenStart
)
452 SizeT dicPos
= p
->dicPos
;
453 SizeT dicBufSize
= p
->dicBufSize
;
454 unsigned len
= p
->remainLen
;
455 UInt32 rep0
= p
->reps
[0];
456 if (limit
- dicPos
< len
)
457 len
= (unsigned)(limit
- dicPos
);
459 if (p
->checkDicSize
== 0 && p
->prop
.dicSize
- p
->processedPos
<= len
)
460 p
->checkDicSize
= p
->prop
.dicSize
;
462 p
->processedPos
+= len
;
466 dic
[dicPos
] = dic
[(dicPos
- rep0
) + ((dicPos
< rep0
) ? dicBufSize
: 0)];
473 static int MY_FAST_CALL
LzmaDec_DecodeReal2(CLzmaDec
*p
, SizeT limit
, const Byte
*bufLimit
)
477 SizeT limit2
= limit
;
478 if (p
->checkDicSize
== 0)
480 UInt32 rem
= p
->prop
.dicSize
- p
->processedPos
;
481 if (limit
- p
->dicPos
> rem
)
482 limit2
= p
->dicPos
+ rem
;
484 RINOK(LzmaDec_DecodeReal(p
, limit2
, bufLimit
));
485 if (p
->processedPos
>= p
->prop
.dicSize
)
486 p
->checkDicSize
= p
->prop
.dicSize
;
487 LzmaDec_WriteRem(p
, limit
);
489 while (p
->dicPos
< limit
&& p
->buf
< bufLimit
&& p
->remainLen
< kMatchSpecLenStart
);
491 if (p
->remainLen
> kMatchSpecLenStart
)
493 p
->remainLen
= kMatchSpecLenStart
;
500 DUMMY_ERROR
, /* unexpected end of input stream */
506 static ELzmaDummy
LzmaDec_TryDummy(const CLzmaDec
*p
, const Byte
*buf
, SizeT inSize
)
508 UInt32 range
= p
->range
;
509 UInt32 code
= p
->code
;
510 const Byte
*bufLimit
= buf
+ inSize
;
511 CLzmaProb
*probs
= p
->probs
;
512 unsigned state
= p
->state
;
519 unsigned posState
= (p
->processedPos
) & ((1 << p
->prop
.pb
) - 1);
521 prob
= probs
+ IsMatch
+ (state
<< kNumPosBitsMax
) + posState
;
526 /* if (bufLimit - buf >= 7) return DUMMY_LIT; */
528 prob
= probs
+ Literal
;
529 if (p
->checkDicSize
!= 0 || p
->processedPos
!= 0)
530 prob
+= (LZMA_LIT_SIZE
*
531 ((((p
->processedPos
) & ((1 << (p
->prop
.lp
)) - 1)) << p
->prop
.lc
) +
532 (p
->dic
[(p
->dicPos
== 0 ? p
->dicBufSize
: p
->dicPos
) - 1] >> (8 - p
->prop
.lc
))));
534 if (state
< kNumLitStates
)
537 do { GET_BIT_CHECK(prob
+ symbol
, symbol
) } while (symbol
< 0x100);
541 unsigned matchByte
= p
->dic
[p
->dicPos
- p
->reps
[0] +
542 ((p
->dicPos
< p
->reps
[0]) ? p
->dicBufSize
: 0)];
543 unsigned offs
= 0x100;
550 bit
= (matchByte
& offs
);
551 probLit
= prob
+ offs
+ bit
+ symbol
;
552 GET_BIT2_CHECK(probLit
, symbol
, offs
&= ~bit
, offs
&= bit
)
554 while (symbol
< 0x100);
563 prob
= probs
+ IsRep
+ state
;
568 prob
= probs
+ LenCoder
;
575 prob
= probs
+ IsRepG0
+ state
;
579 prob
= probs
+ IsRep0Long
+ (state
<< kNumPosBitsMax
) + posState
;
594 prob
= probs
+ IsRepG1
+ state
;
602 prob
= probs
+ IsRepG2
+ state
;
614 prob
= probs
+ RepLenCoder
;
617 unsigned limit
, offset
;
618 CLzmaProb
*probLen
= prob
+ LenChoice
;
619 IF_BIT_0_CHECK(probLen
)
622 probLen
= prob
+ LenLow
+ (posState
<< kLenNumLowBits
);
624 limit
= 1 << kLenNumLowBits
;
629 probLen
= prob
+ LenChoice2
;
630 IF_BIT_0_CHECK(probLen
)
633 probLen
= prob
+ LenMid
+ (posState
<< kLenNumMidBits
);
634 offset
= kLenNumLowSymbols
;
635 limit
= 1 << kLenNumMidBits
;
640 probLen
= prob
+ LenHigh
;
641 offset
= kLenNumLowSymbols
+ kLenNumMidSymbols
;
642 limit
= 1 << kLenNumHighBits
;
645 TREE_DECODE_CHECK(probLen
, limit
, len
);
652 prob
= probs
+ PosSlot
+
653 ((len
< kNumLenToPosStates
? len
: kNumLenToPosStates
- 1) <<
655 TREE_DECODE_CHECK(prob
, 1 << kNumPosSlotBits
, posSlot
);
656 if (posSlot
>= kStartPosModelIndex
)
658 int numDirectBits
= ((posSlot
>> 1) - 1);
660 /* if (bufLimit - buf >= 8) return DUMMY_MATCH; */
662 if (posSlot
< kEndPosModelIndex
)
664 prob
= probs
+ SpecPos
+ ((2 | (posSlot
& 1)) << numDirectBits
) - posSlot
- 1;
668 numDirectBits
-= kNumAlignBits
;
673 code
-= range
& (((code
- range
) >> 31) - 1);
674 /* if (code >= range) code -= range; */
676 while (--numDirectBits
!= 0);
677 prob
= probs
+ Align
;
678 numDirectBits
= kNumAlignBits
;
684 GET_BIT_CHECK(prob
+ i
, i
);
686 while (--numDirectBits
!= 0);
697 static void LzmaDec_InitRc(CLzmaDec
*p
, const Byte
*data
)
699 p
->code
= ((UInt32
)data
[1] << 24) | ((UInt32
)data
[2] << 16) | ((UInt32
)data
[3] << 8) | ((UInt32
)data
[4]);
700 p
->range
= 0xFFFFFFFF;
704 void LzmaDec_InitDicAndState(CLzmaDec
*p
, Bool initDic
, Bool initState
)
714 p
->needInitState
= 1;
717 p
->needInitState
= 1;
720 void LzmaDec_Init(CLzmaDec
*p
)
723 LzmaDec_InitDicAndState(p
, True
, True
);
726 static void LzmaDec_InitStateReal(CLzmaDec
*p
)
728 UInt32 numProbs
= Literal
+ ((UInt32
)LZMA_LIT_SIZE
<< (p
->prop
.lc
+ p
->prop
.lp
));
730 CLzmaProb
*probs
= p
->probs
;
731 for (i
= 0; i
< numProbs
; i
++)
732 probs
[i
] = kBitModelTotal
>> 1;
733 p
->reps
[0] = p
->reps
[1] = p
->reps
[2] = p
->reps
[3] = 1;
735 p
->needInitState
= 0;
738 SRes
LzmaDec_DecodeToDic(CLzmaDec
*p
, SizeT dicLimit
, const Byte
*src
, SizeT
*srcLen
,
739 ELzmaFinishMode finishMode
, ELzmaStatus
*status
)
741 SizeT inSize
= *srcLen
;
743 LzmaDec_WriteRem(p
, dicLimit
);
745 *status
= LZMA_STATUS_NOT_SPECIFIED
;
747 while (p
->remainLen
!= kMatchSpecLenStart
)
751 if (p
->needFlush
!= 0)
753 for (; inSize
> 0 && p
->tempBufSize
< RC_INIT_SIZE
; (*srcLen
)++, inSize
--)
754 p
->tempBuf
[p
->tempBufSize
++] = *src
++;
755 if (p
->tempBufSize
< RC_INIT_SIZE
)
757 *status
= LZMA_STATUS_NEEDS_MORE_INPUT
;
760 if (p
->tempBuf
[0] != 0)
761 return SZ_ERROR_DATA
;
763 LzmaDec_InitRc(p
, p
->tempBuf
);
768 if (p
->dicPos
>= dicLimit
)
770 if (p
->remainLen
== 0 && p
->code
== 0)
772 *status
= LZMA_STATUS_MAYBE_FINISHED_WITHOUT_MARK
;
775 if (finishMode
== LZMA_FINISH_ANY
)
777 *status
= LZMA_STATUS_NOT_FINISHED
;
780 if (p
->remainLen
!= 0)
782 *status
= LZMA_STATUS_NOT_FINISHED
;
783 return SZ_ERROR_DATA
;
788 if (p
->needInitState
)
789 LzmaDec_InitStateReal(p
);
791 if (p
->tempBufSize
== 0)
794 const Byte
*bufLimit
;
795 if (inSize
< LZMA_REQUIRED_INPUT_MAX
|| checkEndMarkNow
)
797 int dummyRes
= LzmaDec_TryDummy(p
, src
, inSize
);
798 if (dummyRes
== DUMMY_ERROR
)
800 (void) memcpy(p
->tempBuf
, src
, inSize
);
801 p
->tempBufSize
= (unsigned)inSize
;
803 *status
= LZMA_STATUS_NEEDS_MORE_INPUT
;
806 if (checkEndMarkNow
&& dummyRes
!= DUMMY_MATCH
)
808 *status
= LZMA_STATUS_NOT_FINISHED
;
809 return SZ_ERROR_DATA
;
814 bufLimit
= src
+ inSize
- LZMA_REQUIRED_INPUT_MAX
;
816 if (LzmaDec_DecodeReal2(p
, dicLimit
, bufLimit
) != 0)
817 return SZ_ERROR_DATA
;
819 processed
= (SizeT
)(p
->buf
- src
);
820 (*srcLen
) += processed
;
826 unsigned rem
= p
->tempBufSize
, lookAhead
= 0;
827 while (rem
< LZMA_REQUIRED_INPUT_MAX
&& lookAhead
< inSize
)
828 p
->tempBuf
[rem
++] = src
[lookAhead
++];
829 p
->tempBufSize
= rem
;
830 if (rem
< LZMA_REQUIRED_INPUT_MAX
|| checkEndMarkNow
)
832 int dummyRes
= LzmaDec_TryDummy(p
, p
->tempBuf
, rem
);
833 if (dummyRes
== DUMMY_ERROR
)
835 (*srcLen
) += lookAhead
;
836 *status
= LZMA_STATUS_NEEDS_MORE_INPUT
;
839 if (checkEndMarkNow
&& dummyRes
!= DUMMY_MATCH
)
841 *status
= LZMA_STATUS_NOT_FINISHED
;
842 return SZ_ERROR_DATA
;
846 if (LzmaDec_DecodeReal2(p
, dicLimit
, p
->buf
) != 0)
847 return SZ_ERROR_DATA
;
849 lookAhead
-= (rem
- (unsigned)(p
->buf
- p
->tempBuf
));
850 (*srcLen
) += lookAhead
;
857 *status
= LZMA_STATUS_FINISHED_WITH_MARK
;
858 return (p
->code
== 0) ? SZ_OK
: SZ_ERROR_DATA
;
861 SRes
LzmaDec_DecodeToBuf(CLzmaDec
*p
, Byte
*dest
, SizeT
*destLen
, const Byte
*src
, SizeT
*srcLen
, ELzmaFinishMode finishMode
, ELzmaStatus
*status
)
863 SizeT outSize
= *destLen
;
864 SizeT inSize
= *srcLen
;
865 *srcLen
= *destLen
= 0;
868 SizeT inSizeCur
= inSize
, outSizeCur
, dicPos
;
869 ELzmaFinishMode curFinishMode
;
871 if (p
->dicPos
== p
->dicBufSize
)
874 if (outSize
> p
->dicBufSize
- dicPos
)
876 outSizeCur
= p
->dicBufSize
;
877 curFinishMode
= LZMA_FINISH_ANY
;
881 outSizeCur
= dicPos
+ outSize
;
882 curFinishMode
= finishMode
;
885 res
= LzmaDec_DecodeToDic(p
, outSizeCur
, src
, &inSizeCur
, curFinishMode
, status
);
888 *srcLen
+= inSizeCur
;
889 outSizeCur
= p
->dicPos
- dicPos
;
890 (void) memcpy(dest
, p
->dic
+ dicPos
, outSizeCur
);
892 outSize
-= outSizeCur
;
893 *destLen
+= outSizeCur
;
896 if (outSizeCur
== 0 || outSize
== 0)
901 void LzmaDec_FreeProbs(CLzmaDec
*p
, ISzAlloc
*alloc
)
904 alloc
->Free(alloc
, p
->probs
, (p
->numProbs
* sizeof (*p
->probs
)));
908 static void LzmaDec_FreeDict(CLzmaDec
*p
, ISzAlloc
*alloc
)
911 alloc
->Free(alloc
, p
->dic
, ((p
->prop
).dicSize
* sizeof (*p
->dic
)));
915 void LzmaDec_Free(CLzmaDec
*p
, ISzAlloc
*alloc
)
917 LzmaDec_FreeProbs(p
, alloc
);
918 LzmaDec_FreeDict(p
, alloc
);
921 SRes
LzmaProps_Decode(CLzmaProps
*p
, const Byte
*data
, unsigned size
)
926 if (size
< LZMA_PROPS_SIZE
)
927 return SZ_ERROR_UNSUPPORTED
;
929 dicSize
= data
[1] | ((UInt32
)data
[2] << 8) | ((UInt32
)data
[3] << 16) | ((UInt32
)data
[4] << 24);
931 if (dicSize
< LZMA_DIC_MIN
)
932 dicSize
= LZMA_DIC_MIN
;
933 p
->dicSize
= dicSize
;
936 if (d
>= (9 * 5 * 5))
937 return SZ_ERROR_UNSUPPORTED
;
947 static SRes
LzmaDec_AllocateProbs2(CLzmaDec
*p
, const CLzmaProps
*propNew
, ISzAlloc
*alloc
)
949 UInt32 numProbs
= LzmaProps_GetNumProbs(propNew
);
950 if (p
->probs
== 0 || numProbs
!= p
->numProbs
)
952 LzmaDec_FreeProbs(p
, alloc
);
953 p
->probs
= (CLzmaProb
*)alloc
->Alloc(alloc
, numProbs
* sizeof(CLzmaProb
));
954 p
->numProbs
= numProbs
;
961 SRes
LzmaDec_AllocateProbs(CLzmaDec
*p
, const Byte
*props
, unsigned propsSize
, ISzAlloc
*alloc
)
964 RINOK(LzmaProps_Decode(&propNew
, props
, propsSize
));
965 RINOK(LzmaDec_AllocateProbs2(p
, &propNew
, alloc
));
970 SRes
LzmaDec_Allocate(CLzmaDec
*p
, const Byte
*props
, unsigned propsSize
, ISzAlloc
*alloc
)
974 RINOK(LzmaProps_Decode(&propNew
, props
, propsSize
));
975 RINOK(LzmaDec_AllocateProbs2(p
, &propNew
, alloc
));
976 dicBufSize
= propNew
.dicSize
;
977 if (p
->dic
== 0 || dicBufSize
!= p
->dicBufSize
)
979 LzmaDec_FreeDict(p
, alloc
);
980 p
->dic
= (Byte
*)alloc
->Alloc(alloc
, dicBufSize
);
983 LzmaDec_FreeProbs(p
, alloc
);
987 p
->dicBufSize
= dicBufSize
;
992 SRes
LzmaDecode(Byte
*dest
, SizeT
*destLen
, const Byte
*src
, SizeT
*srcLen
,
993 const Byte
*propData
, unsigned propSize
, ELzmaFinishMode finishMode
,
994 ELzmaStatus
*status
, ISzAlloc
*alloc
)
998 SizeT inSize
= *srcLen
;
999 SizeT outSize
= *destLen
;
1000 *srcLen
= *destLen
= 0;
1001 if (inSize
< RC_INIT_SIZE
)
1002 return SZ_ERROR_INPUT_EOF
;
1004 LzmaDec_Construct(&p
);
1005 res
= LzmaDec_AllocateProbs(&p
, propData
, propSize
, alloc
);
1009 p
.dicBufSize
= outSize
;
1014 res
= LzmaDec_DecodeToDic(&p
, outSize
, src
, srcLen
, finishMode
, status
);
1016 if (res
== SZ_OK
&& *status
== LZMA_STATUS_NEEDS_MORE_INPUT
)
1017 res
= SZ_ERROR_INPUT_EOF
;
1019 (*destLen
) = p
.dicPos
;
1020 LzmaDec_FreeProbs(&p
, alloc
);