4 LZMA SDK 4.01 Copyright (c) 1999-2004 Igor Pavlov (2004-02-15)
7 #include "LzmaDecode.h"
10 #define Byte unsigned char
13 #define kNumTopBits 24
14 #define kTopValue ((UInt32)1 << kNumTopBits)
16 #define kNumBitModelTotalBits 11
17 #define kBitModelTotal (1 << kNumBitModelTotalBits)
18 #define kNumMoveBits 5
20 typedef struct _CRangeDecoder
27 ILzmaInCallback
*InCallback
;
33 Byte
RangeDecoderReadByte(CRangeDecoder
*rd
)
35 if (rd
->Buffer
== rd
->BufferLim
)
39 rd
->Result
= rd
->InCallback
->Read(rd
->InCallback
, &rd
->Buffer
, &size
);
40 rd
->BufferLim
= rd
->Buffer
+ size
;
48 return (*rd
->Buffer
++);
51 /* #define ReadByte (*rd->Buffer++) */
52 #define ReadByte (RangeDecoderReadByte(rd))
54 void RangeDecoderInit(CRangeDecoder
*rd
,
56 ILzmaInCallback
*inCallback
58 Byte
*stream
, UInt32 bufferSize
64 rd
->InCallback
= inCallback
;
65 rd
->Buffer
= rd
->BufferLim
= 0;
68 rd
->BufferLim
= stream
+ bufferSize
;
72 rd
->Range
= (0xFFFFFFFF);
73 for(i
= 0; i
< 5; i
++)
74 rd
->Code
= (rd
->Code
<< 8) | ReadByte
;
77 #define RC_INIT_VAR UInt32 range = rd->Range; UInt32 code = rd->Code;
78 #define RC_FLUSH_VAR rd->Range = range; rd->Code = code;
79 #define RC_NORMALIZE if (range < kTopValue) { range <<= 8; code = (code << 8) | ReadByte; }
81 UInt32
RangeDecoderDecodeDirectBits(CRangeDecoder
*rd
, int numTotalBits
)
86 for (i
= numTotalBits
; i
> 0; i
--)
98 t = (code - range) >> 31;
100 code -= range & (t - 1);
101 result = (result + result) | (1 - t);
109 int RangeDecoderBitDecode(CProb
*prob
, CRangeDecoder
*rd
)
111 UInt32 bound
= (rd
->Range
>> kNumBitModelTotalBits
) * *prob
;
112 if (rd
->Code
< bound
)
115 *prob
+= (kBitModelTotal
- *prob
) >> kNumMoveBits
;
116 if (rd
->Range
< kTopValue
)
118 rd
->Code
= (rd
->Code
<< 8) | ReadByte
;
127 *prob
-= (*prob
) >> kNumMoveBits
;
128 if (rd
->Range
< kTopValue
)
130 rd
->Code
= (rd
->Code
<< 8) | ReadByte
;
137 #define RC_GET_BIT2(prob, mi, A0, A1) \
138 UInt32 bound = (range >> kNumBitModelTotalBits) * *prob; \
140 { A0; range = bound; *prob += (kBitModelTotal - *prob) >> kNumMoveBits; mi <<= 1; } \
142 { A1; range -= bound; code -= bound; *prob -= (*prob) >> kNumMoveBits; mi = (mi + mi) + 1; } \
145 #define RC_GET_BIT(prob, mi) RC_GET_BIT2(prob, mi, ; , ;)
147 int RangeDecoderBitTreeDecode(CProb
*probs
, int numLevels
, CRangeDecoder
*rd
)
154 for(i
= numLevels
; i
> 0; i
--)
157 CProb
*prob
= probs
+ mi
;
160 mi
= (mi
+ mi
) + RangeDecoderBitDecode(probs
+ mi
, rd
);
166 return mi
- (1 << numLevels
);
169 int RangeDecoderReverseBitTreeDecode(CProb
*probs
, int numLevels
, CRangeDecoder
*rd
)
177 for(i
= 0; i
< numLevels
; i
++)
180 CProb
*prob
= probs
+ mi
;
181 RC_GET_BIT2(prob
, mi
, ; , symbol
|= (1 << i
))
183 int bit
= RangeDecoderBitDecode(probs
+ mi
, rd
);
185 symbol
|= (bit
<< i
);
194 Byte
LzmaLiteralDecode(CProb
*probs
, CRangeDecoder
*rd
)
203 CProb
*prob
= probs
+ symbol
;
204 RC_GET_BIT(prob
, symbol
)
206 symbol
= (symbol
+ symbol
) | RangeDecoderBitDecode(probs
+ symbol
, rd
);
209 while (symbol
< 0x100);
216 Byte
LzmaLiteralDecodeMatch(CProb
*probs
, CRangeDecoder
*rd
, Byte matchByte
)
225 int matchBit
= (matchByte
>> 7) & 1;
229 CProb
*prob
= probs
+ ((1 + matchBit
) << 8) + symbol
;
230 RC_GET_BIT2(prob
, symbol
, bit
= 0, bit
= 1)
233 bit
= RangeDecoderBitDecode(probs
+ ((1 + matchBit
) << 8) + symbol
, rd
);
234 symbol
= (symbol
<< 1) | bit
;
238 while (symbol
< 0x100)
241 CProb
*prob
= probs
+ symbol
;
242 RC_GET_BIT(prob
, symbol
)
244 symbol
= (symbol
+ symbol
) | RangeDecoderBitDecode(probs
+ symbol
, rd
);
250 while (symbol
< 0x100);
257 #define kNumPosBitsMax 4
258 #define kNumPosStatesMax (1 << kNumPosBitsMax)
260 #define kLenNumLowBits 3
261 #define kLenNumLowSymbols (1 << kLenNumLowBits)
262 #define kLenNumMidBits 3
263 #define kLenNumMidSymbols (1 << kLenNumMidBits)
264 #define kLenNumHighBits 8
265 #define kLenNumHighSymbols (1 << kLenNumHighBits)
268 #define LenChoice2 (LenChoice + 1)
269 #define LenLow (LenChoice2 + 1)
270 #define LenMid (LenLow + (kNumPosStatesMax << kLenNumLowBits))
271 #define LenHigh (LenMid + (kNumPosStatesMax << kLenNumMidBits))
272 #define kNumLenProbs (LenHigh + kLenNumHighSymbols)
274 int LzmaLenDecode(CProb
*p
, CRangeDecoder
*rd
, int posState
)
276 if(RangeDecoderBitDecode(p
+ LenChoice
, rd
) == 0)
277 return RangeDecoderBitTreeDecode(p
+ LenLow
+
278 (posState
<< kLenNumLowBits
), kLenNumLowBits
, rd
);
279 if(RangeDecoderBitDecode(p
+ LenChoice2
, rd
) == 0)
280 return kLenNumLowSymbols
+ RangeDecoderBitTreeDecode(p
+ LenMid
+
281 (posState
<< kLenNumMidBits
), kLenNumMidBits
, rd
);
282 return kLenNumLowSymbols
+ kLenNumMidSymbols
+
283 RangeDecoderBitTreeDecode(p
+ LenHigh
, kLenNumHighBits
, rd
);
286 #define kNumStates 12
288 #define kStartPosModelIndex 4
289 #define kEndPosModelIndex 14
290 #define kNumFullDistances (1 << (kEndPosModelIndex >> 1))
292 #define kNumPosSlotBits 6
293 #define kNumLenToPosStates 4
295 #define kNumAlignBits 4
296 #define kAlignTableSize (1 << kNumAlignBits)
298 #define kMatchMinLen 2
301 #define IsRep (IsMatch + (kNumStates << kNumPosBitsMax))
302 #define IsRepG0 (IsRep + kNumStates)
303 #define IsRepG1 (IsRepG0 + kNumStates)
304 #define IsRepG2 (IsRepG1 + kNumStates)
305 #define IsRep0Long (IsRepG2 + kNumStates)
306 #define PosSlot (IsRep0Long + (kNumStates << kNumPosBitsMax))
307 #define SpecPos (PosSlot + (kNumLenToPosStates << kNumPosSlotBits))
308 #define Align (SpecPos + kNumFullDistances - kEndPosModelIndex)
309 #define LenCoder (Align + kAlignTableSize)
310 #define RepLenCoder (LenCoder + kNumLenProbs)
311 #define Literal (RepLenCoder + kNumLenProbs)
313 #if Literal != LZMA_BASE_SIZE
317 #ifdef _LZMA_OUT_READ
319 typedef struct _LzmaVarState
321 CRangeDecoder RangeDecoder
;
323 UInt32 DictionarySize
;
324 UInt32 DictionaryPos
;
336 unsigned char *buffer
, UInt32 bufferSize
,
337 int lc
, int lp
, int pb
,
338 unsigned char *dictionary
, UInt32 dictionarySize
,
340 ILzmaInCallback
*inCallback
342 unsigned char *inStream
, UInt32 inSize
346 LzmaVarState
*vs
= (LzmaVarState
*)buffer
;
347 CProb
*p
= (CProb
*)(buffer
+ sizeof(LzmaVarState
));
348 UInt32 numProbs
= Literal
+ ((UInt32
)LZMA_LIT_SIZE
<< (lc
+ lp
));
350 if (bufferSize
< numProbs
* sizeof(CProb
) + sizeof(LzmaVarState
))
351 return LZMA_RESULT_NOT_ENOUGH_MEM
;
352 vs
->Dictionary
= dictionary
;
353 vs
->DictionarySize
= dictionarySize
;
354 vs
->DictionaryPos
= 0;
356 vs
->Reps
[0] = vs
->Reps
[1] = vs
->Reps
[2] = vs
->Reps
[3] = 1;
361 vs
->PreviousIsMatch
= 0;
363 dictionary
[dictionarySize
- 1] = 0;
364 for (i
= 0; i
< numProbs
; i
++)
365 p
[i
] = kBitModelTotal
>> 1;
366 RangeDecoderInit(&vs
->RangeDecoder
,
373 return LZMA_RESULT_OK
;
376 int LzmaDecode(unsigned char *buffer
,
377 unsigned char *outStream
, UInt32 outSize
,
378 UInt32
*outSizeProcessed
)
380 LzmaVarState
*vs
= (LzmaVarState
*)buffer
;
381 CProb
*p
= (CProb
*)(buffer
+ sizeof(LzmaVarState
));
382 CRangeDecoder rd
= vs
->RangeDecoder
;
383 int state
= vs
->State
;
384 int previousIsMatch
= vs
->PreviousIsMatch
;
386 UInt32 rep0
= vs
->Reps
[0], rep1
= vs
->Reps
[1], rep2
= vs
->Reps
[2], rep3
= vs
->Reps
[3];
388 UInt32 posStateMask
= (1 << (vs
->pb
)) - 1;
389 UInt32 literalPosMask
= (1 << (vs
->lp
)) - 1;
391 int len
= vs
->RemainLen
;
392 UInt32 globalPos
= vs
->GlobalPos
;
394 Byte
*dictionary
= vs
->Dictionary
;
395 UInt32 dictionarySize
= vs
->DictionarySize
;
396 UInt32 dictionaryPos
= vs
->DictionaryPos
;
400 *outSizeProcessed
= 0;
401 return LZMA_RESULT_OK
;
404 while(len
> 0 && nowPos
< outSize
)
406 UInt32 pos
= dictionaryPos
- rep0
;
407 if (pos
>= dictionarySize
)
408 pos
+= dictionarySize
;
409 outStream
[nowPos
++] = dictionary
[dictionaryPos
] = dictionary
[pos
];
410 if (++dictionaryPos
== dictionarySize
)
414 if (dictionaryPos
== 0)
415 previousByte
= dictionary
[dictionarySize
- 1];
417 previousByte
= dictionary
[dictionaryPos
- 1];
421 Byte
*buffer
, UInt32 bufferSize
,
422 int lc
, int lp
, int pb
,
424 ILzmaInCallback
*inCallback
,
426 unsigned char *inStream
, UInt32 inSize
,
428 unsigned char *outStream
, UInt32 outSize
,
429 UInt32
*outSizeProcessed
)
431 UInt32 numProbs
= Literal
+ ((UInt32
)LZMA_LIT_SIZE
<< (lc
+ lp
));
432 CProb
*p
= (CProb
*)buffer
;
436 int previousIsMatch
= 0;
437 Byte previousByte
= 0;
438 UInt32 rep0
= 1, rep1
= 1, rep2
= 1, rep3
= 1;
440 UInt32 posStateMask
= (1 << pb
) - 1;
441 UInt32 literalPosMask
= (1 << lp
) - 1;
443 if (bufferSize
< numProbs
* sizeof(CProb
))
444 return LZMA_RESULT_NOT_ENOUGH_MEM
;
445 for (i
= 0; i
< numProbs
; i
++)
446 p
[i
] = kBitModelTotal
>> 1;
447 RangeDecoderInit(&rd
,
456 *outSizeProcessed
= 0;
457 while(nowPos
< outSize
)
459 int posState
= (int)(
461 #ifdef _LZMA_OUT_READ
467 if (rd
.Result
!= LZMA_RESULT_OK
)
470 if (rd
.ExtraBytes
!= 0)
471 return LZMA_RESULT_DATA_ERROR
;
472 if (RangeDecoderBitDecode(p
+ IsMatch
+ (state
<< kNumPosBitsMax
) + posState
, &rd
) == 0)
474 CProb
*probs
= p
+ Literal
+ (LZMA_LIT_SIZE
*
477 #ifdef _LZMA_OUT_READ
481 & literalPosMask
) << lc
) + (previousByte
>> (8 - lc
))));
483 if (state
< 4) state
= 0;
484 else if (state
< 10) state
-= 3;
489 #ifdef _LZMA_OUT_READ
490 UInt32 pos
= dictionaryPos
- rep0
;
491 if (pos
>= dictionarySize
)
492 pos
+= dictionarySize
;
493 matchByte
= dictionary
[pos
];
495 matchByte
= outStream
[nowPos
- rep0
];
497 previousByte
= LzmaLiteralDecodeMatch(probs
, &rd
, matchByte
);
501 previousByte
= LzmaLiteralDecode(probs
, &rd
);
502 outStream
[nowPos
++] = previousByte
;
503 #ifdef _LZMA_OUT_READ
504 dictionary
[dictionaryPos
] = previousByte
;
505 if (++dictionaryPos
== dictionarySize
)
512 if (RangeDecoderBitDecode(p
+ IsRep
+ state
, &rd
) == 1)
514 if (RangeDecoderBitDecode(p
+ IsRepG0
+ state
, &rd
) == 0)
516 if (RangeDecoderBitDecode(p
+ IsRep0Long
+ (state
<< kNumPosBitsMax
) + posState
, &rd
) == 0)
518 #ifdef _LZMA_OUT_READ
523 #ifdef _LZMA_OUT_READ
528 return LZMA_RESULT_DATA_ERROR
;
529 state
= state
< 7 ? 9 : 11;
530 #ifdef _LZMA_OUT_READ
531 pos
= dictionaryPos
- rep0
;
532 if (pos
>= dictionarySize
)
533 pos
+= dictionarySize
;
534 previousByte
= dictionary
[pos
];
535 dictionary
[dictionaryPos
] = previousByte
;
536 if (++dictionaryPos
== dictionarySize
)
539 previousByte
= outStream
[nowPos
- rep0
];
541 outStream
[nowPos
++] = previousByte
;
548 if(RangeDecoderBitDecode(p
+ IsRepG1
+ state
, &rd
) == 0)
552 if(RangeDecoderBitDecode(p
+ IsRepG2
+ state
, &rd
) == 0)
564 len
= LzmaLenDecode(p
+ RepLenCoder
, &rd
, posState
);
565 state
= state
< 7 ? 8 : 11;
573 state
= state
< 7 ? 7 : 10;
574 len
= LzmaLenDecode(p
+ LenCoder
, &rd
, posState
);
575 posSlot
= RangeDecoderBitTreeDecode(p
+ PosSlot
+
576 ((len
< kNumLenToPosStates
? len
: kNumLenToPosStates
- 1) <<
577 kNumPosSlotBits
), kNumPosSlotBits
, &rd
);
578 if (posSlot
>= kStartPosModelIndex
)
580 int numDirectBits
= ((posSlot
>> 1) - 1);
581 rep0
= ((2 | ((UInt32
)posSlot
& 1)) << numDirectBits
);
582 if (posSlot
< kEndPosModelIndex
)
584 rep0
+= RangeDecoderReverseBitTreeDecode(
585 p
+ SpecPos
+ rep0
- posSlot
- 1, numDirectBits
, &rd
);
589 rep0
+= RangeDecoderDecodeDirectBits(&rd
,
590 numDirectBits
- kNumAlignBits
) << kNumAlignBits
;
591 rep0
+= RangeDecoderReverseBitTreeDecode(p
+ Align
, kNumAlignBits
, &rd
);
598 if (rep0
== (UInt32
)(0))
600 /* it's for stream version */
605 #ifdef _LZMA_OUT_READ
610 return LZMA_RESULT_DATA_ERROR
;
615 #ifdef _LZMA_OUT_READ
616 UInt32 pos
= dictionaryPos
- rep0
;
617 if (pos
>= dictionarySize
)
618 pos
+= dictionarySize
;
619 previousByte
= dictionary
[pos
];
620 dictionary
[dictionaryPos
] = previousByte
;
621 if (++dictionaryPos
== dictionarySize
)
624 previousByte
= outStream
[nowPos
- rep0
];
626 outStream
[nowPos
++] = previousByte
;
629 while(len
> 0 && nowPos
< outSize
);
633 #ifdef _LZMA_OUT_READ
634 vs
->RangeDecoder
= rd
;
635 vs
->DictionaryPos
= dictionaryPos
;
636 vs
->GlobalPos
= globalPos
+ nowPos
;
642 vs
->PreviousIsMatch
= previousIsMatch
;
646 *outSizeProcessed
= nowPos
;
647 return LZMA_RESULT_OK
;