5 LZMA SDK 4.05 Copyright (c) 1999-2004 Igor Pavlov (2004-08-25)
8 LZMA SDK is licensed under two licenses:
9 1) GNU Lesser General Public License (GNU LGPL)
10 2) Common Public License (CPL)
11 It means that you can select one of these two licenses and
12 follow rules of that license.
15 Igor Pavlov, as the author of this code, expressly permits you to
16 statically or dynamically link your code (or bind by name) to the
17 interfaces of this file without subjecting your linked code to the
18 terms of the CPL or GNU LGPL. Any modifications or additions
19 to this file, however, are subject to the LGPL or CPL terms.
22 #include "LzmaDecode.h"
25 #define Byte unsigned char
28 #define kNumTopBits 24
29 #define kTopValue ((UInt32)1 << kNumTopBits)
31 #define kNumBitModelTotalBits 11
32 #define kBitModelTotal (1 << kNumBitModelTotalBits)
33 #define kNumMoveBits 5
35 typedef struct _CRangeDecoder
42 ILzmaInCallback *InCallback;
48 Byte RangeDecoderReadByte(CRangeDecoder *rd)
50 if (rd->Buffer == rd->BufferLim)
54 rd->Result = rd->InCallback->Read(rd->InCallback, &rd->Buffer, &size);
55 rd->BufferLim = rd->Buffer + size;
63 return (*rd->Buffer++);
66 /* #define ReadByte (*rd->Buffer++) */
67 #define ReadByte (RangeDecoderReadByte(rd))
69 void RangeDecoderInit(CRangeDecoder *rd,
71 ILzmaInCallback *inCallback
73 Byte *stream, UInt32 bufferSize
79 rd->InCallback = inCallback;
80 rd->Buffer = rd->BufferLim = 0;
83 rd->BufferLim = stream + bufferSize;
87 rd->Range = (0xFFFFFFFF);
88 for(i = 0; i < 5; i++)
89 rd->Code = (rd->Code << 8) | ReadByte;
92 #define RC_INIT_VAR UInt32 range = rd->Range; UInt32 code = rd->Code;
93 #define RC_FLUSH_VAR rd->Range = range; rd->Code = code;
94 #define RC_NORMALIZE if (range < kTopValue) { range <<= 8; code = (code << 8) | ReadByte; }
96 UInt32 RangeDecoderDecodeDirectBits(CRangeDecoder *rd, int numTotalBits)
101 for (i = numTotalBits; i > 0; i--)
113 t = (code - range) >> 31;
115 code -= range & (t - 1);
116 result = (result + result) | (1 - t);
124 int RangeDecoderBitDecode(CProb *prob, CRangeDecoder *rd)
126 UInt32 bound = (rd->Range >> kNumBitModelTotalBits) * *prob;
127 if (rd->Code < bound)
130 *prob += (kBitModelTotal - *prob) >> kNumMoveBits;
131 if (rd->Range < kTopValue)
133 rd->Code = (rd->Code << 8) | ReadByte;
142 *prob -= (*prob) >> kNumMoveBits;
143 if (rd->Range < kTopValue)
145 rd->Code = (rd->Code << 8) | ReadByte;
152 #define RC_GET_BIT2(prob, mi, A0, A1) \
153 UInt32 bound = (range >> kNumBitModelTotalBits) * *prob; \
155 { A0; range = bound; *prob += (kBitModelTotal - *prob) >> kNumMoveBits; mi <<= 1; } \
157 { A1; range -= bound; code -= bound; *prob -= (*prob) >> kNumMoveBits; mi = (mi + mi) + 1; } \
160 #define RC_GET_BIT(prob, mi) RC_GET_BIT2(prob, mi, ; , ;)
162 int RangeDecoderBitTreeDecode(CProb *probs, int numLevels, CRangeDecoder *rd)
169 for(i = numLevels; i > 0; i--)
172 CProb *prob = probs + mi;
175 mi = (mi + mi) + RangeDecoderBitDecode(probs + mi, rd);
181 return mi - (1 << numLevels);
184 int RangeDecoderReverseBitTreeDecode(CProb *probs, int numLevels, CRangeDecoder *rd)
192 for(i = 0; i < numLevels; i++)
195 CProb *prob = probs + mi;
196 RC_GET_BIT2(prob, mi, ; , symbol |= (1 << i))
198 int bit = RangeDecoderBitDecode(probs + mi, rd);
200 symbol |= (bit << i);
209 Byte LzmaLiteralDecode(CProb *probs, CRangeDecoder *rd)
218 CProb *prob = probs + symbol;
219 RC_GET_BIT(prob, symbol)
221 symbol = (symbol + symbol) | RangeDecoderBitDecode(probs + symbol, rd);
224 while (symbol < 0x100);
231 Byte LzmaLiteralDecodeMatch(CProb *probs, CRangeDecoder *rd, Byte matchByte)
240 int matchBit = (matchByte >> 7) & 1;
244 CProb *prob = probs + ((1 + matchBit) << 8) + symbol;
245 RC_GET_BIT2(prob, symbol, bit = 0, bit = 1)
248 bit = RangeDecoderBitDecode(probs + ((1 + matchBit) << 8) + symbol, rd);
249 symbol = (symbol << 1) | bit;
253 while (symbol < 0x100)
256 CProb *prob = probs + symbol;
257 RC_GET_BIT(prob, symbol)
259 symbol = (symbol + symbol) | RangeDecoderBitDecode(probs + symbol, rd);
265 while (symbol < 0x100);
272 #define kNumPosBitsMax 4
273 #define kNumPosStatesMax (1 << kNumPosBitsMax)
275 #define kLenNumLowBits 3
276 #define kLenNumLowSymbols (1 << kLenNumLowBits)
277 #define kLenNumMidBits 3
278 #define kLenNumMidSymbols (1 << kLenNumMidBits)
279 #define kLenNumHighBits 8
280 #define kLenNumHighSymbols (1 << kLenNumHighBits)
283 #define LenChoice2 (LenChoice + 1)
284 #define LenLow (LenChoice2 + 1)
285 #define LenMid (LenLow + (kNumPosStatesMax << kLenNumLowBits))
286 #define LenHigh (LenMid + (kNumPosStatesMax << kLenNumMidBits))
287 #define kNumLenProbs (LenHigh + kLenNumHighSymbols)
289 int LzmaLenDecode(CProb *p, CRangeDecoder *rd, int posState)
291 if(RangeDecoderBitDecode(p + LenChoice, rd) == 0)
292 return RangeDecoderBitTreeDecode(p + LenLow +
293 (posState << kLenNumLowBits), kLenNumLowBits, rd);
294 if(RangeDecoderBitDecode(p + LenChoice2, rd) == 0)
295 return kLenNumLowSymbols + RangeDecoderBitTreeDecode(p + LenMid +
296 (posState << kLenNumMidBits), kLenNumMidBits, rd);
297 return kLenNumLowSymbols + kLenNumMidSymbols +
298 RangeDecoderBitTreeDecode(p + LenHigh, kLenNumHighBits, rd);
301 #define kNumStates 12
303 #define kStartPosModelIndex 4
304 #define kEndPosModelIndex 14
305 #define kNumFullDistances (1 << (kEndPosModelIndex >> 1))
307 #define kNumPosSlotBits 6
308 #define kNumLenToPosStates 4
310 #define kNumAlignBits 4
311 #define kAlignTableSize (1 << kNumAlignBits)
313 #define kMatchMinLen 2
316 #define IsRep (IsMatch + (kNumStates << kNumPosBitsMax))
317 #define IsRepG0 (IsRep + kNumStates)
318 #define IsRepG1 (IsRepG0 + kNumStates)
319 #define IsRepG2 (IsRepG1 + kNumStates)
320 #define IsRep0Long (IsRepG2 + kNumStates)
321 #define PosSlot (IsRep0Long + (kNumStates << kNumPosBitsMax))
322 #define SpecPos (PosSlot + (kNumLenToPosStates << kNumPosSlotBits))
323 #define Align (SpecPos + kNumFullDistances - kEndPosModelIndex)
324 #define LenCoder (Align + kAlignTableSize)
325 #define RepLenCoder (LenCoder + kNumLenProbs)
326 #define Literal (RepLenCoder + kNumLenProbs)
328 #if Literal != LZMA_BASE_SIZE
332 #ifdef _LZMA_OUT_READ
334 typedef struct _LzmaVarState
336 CRangeDecoder RangeDecoder;
338 UInt32 DictionarySize;
339 UInt32 DictionaryPos;
351 unsigned char *buffer, UInt32 bufferSize,
352 int lc, int lp, int pb,
353 unsigned char *dictionary, UInt32 dictionarySize,
355 ILzmaInCallback *inCallback
357 unsigned char *inStream, UInt32 inSize
361 LzmaVarState *vs = (LzmaVarState *)buffer;
362 CProb *p = (CProb *)(buffer + sizeof(LzmaVarState));
363 UInt32 numProbs = Literal + ((UInt32)LZMA_LIT_SIZE << (lc + lp));
365 if (bufferSize < numProbs * sizeof(CProb) + sizeof(LzmaVarState))
366 return LZMA_RESULT_NOT_ENOUGH_MEM;
367 vs->Dictionary = dictionary;
368 vs->DictionarySize = dictionarySize;
369 vs->DictionaryPos = 0;
371 vs->Reps[0] = vs->Reps[1] = vs->Reps[2] = vs->Reps[3] = 1;
376 vs->PreviousIsMatch = 0;
378 dictionary[dictionarySize - 1] = 0;
379 for (i = 0; i < numProbs; i++)
380 p[i] = kBitModelTotal >> 1;
381 RangeDecoderInit(&vs->RangeDecoder,
388 return LZMA_RESULT_OK;
391 int LzmaDecode(unsigned char *buffer,
392 unsigned char *outStream, UInt32 outSize,
393 UInt32 *outSizeProcessed)
395 LzmaVarState *vs = (LzmaVarState *)buffer;
396 CProb *p = (CProb *)(buffer + sizeof(LzmaVarState));
397 CRangeDecoder rd = vs->RangeDecoder;
398 int state = vs->State;
399 int previousIsMatch = vs->PreviousIsMatch;
401 UInt32 rep0 = vs->Reps[0], rep1 = vs->Reps[1], rep2 = vs->Reps[2], rep3 = vs->Reps[3];
403 UInt32 posStateMask = (1 << (vs->pb)) - 1;
404 UInt32 literalPosMask = (1 << (vs->lp)) - 1;
406 int len = vs->RemainLen;
407 UInt32 globalPos = vs->GlobalPos;
409 Byte *dictionary = vs->Dictionary;
410 UInt32 dictionarySize = vs->DictionarySize;
411 UInt32 dictionaryPos = vs->DictionaryPos;
415 *outSizeProcessed = 0;
416 return LZMA_RESULT_OK;
419 while(len > 0 && nowPos < outSize)
421 UInt32 pos = dictionaryPos - rep0;
422 if (pos >= dictionarySize)
423 pos += dictionarySize;
424 outStream[nowPos++] = dictionary[dictionaryPos] = dictionary[pos];
425 if (++dictionaryPos == dictionarySize)
429 if (dictionaryPos == 0)
430 previousByte = dictionary[dictionarySize - 1];
432 previousByte = dictionary[dictionaryPos - 1];
436 Byte *buffer, UInt32 bufferSize,
437 int lc, int lp, int pb,
439 ILzmaInCallback *inCallback,
441 unsigned char *inStream, UInt32 inSize,
443 unsigned char *outStream, UInt32 outSize,
444 UInt32 *outSizeProcessed)
446 UInt32 numProbs = Literal + ((UInt32)LZMA_LIT_SIZE << (lc + lp));
447 CProb *p = (CProb *)buffer;
451 int previousIsMatch = 0;
452 Byte previousByte = 0;
453 UInt32 rep0 = 1, rep1 = 1, rep2 = 1, rep3 = 1;
455 UInt32 posStateMask = (1 << pb) - 1;
456 UInt32 literalPosMask = (1 << lp) - 1;
458 if (bufferSize < numProbs * sizeof(CProb))
459 return LZMA_RESULT_NOT_ENOUGH_MEM;
460 for (i = 0; i < numProbs; i++)
461 p[i] = kBitModelTotal >> 1;
462 RangeDecoderInit(&rd,
471 *outSizeProcessed = 0;
472 while(nowPos < outSize)
474 int posState = (int)(
476 #ifdef _LZMA_OUT_READ
482 if (rd.Result != LZMA_RESULT_OK)
485 if (rd.ExtraBytes != 0)
486 return LZMA_RESULT_DATA_ERROR;
487 if (RangeDecoderBitDecode(p + IsMatch + (state << kNumPosBitsMax) + posState, &rd) == 0)
489 CProb *probs = p + Literal + (LZMA_LIT_SIZE *
492 #ifdef _LZMA_OUT_READ
496 & literalPosMask) << lc) + (previousByte >> (8 - lc))));
498 if (state < 4) state = 0;
499 else if (state < 10) state -= 3;
504 #ifdef _LZMA_OUT_READ
505 UInt32 pos = dictionaryPos - rep0;
506 if (pos >= dictionarySize)
507 pos += dictionarySize;
508 matchByte = dictionary[pos];
510 matchByte = outStream[nowPos - rep0];
512 previousByte = LzmaLiteralDecodeMatch(probs, &rd, matchByte);
516 previousByte = LzmaLiteralDecode(probs, &rd);
517 outStream[nowPos++] = previousByte;
518 #ifdef _LZMA_OUT_READ
519 dictionary[dictionaryPos] = previousByte;
520 if (++dictionaryPos == dictionarySize)
527 if (RangeDecoderBitDecode(p + IsRep + state, &rd) == 1)
529 if (RangeDecoderBitDecode(p + IsRepG0 + state, &rd) == 0)
531 if (RangeDecoderBitDecode(p + IsRep0Long + (state << kNumPosBitsMax) + posState, &rd) == 0)
533 #ifdef _LZMA_OUT_READ
538 #ifdef _LZMA_OUT_READ
543 return LZMA_RESULT_DATA_ERROR;
544 state = state < 7 ? 9 : 11;
545 #ifdef _LZMA_OUT_READ
546 pos = dictionaryPos - rep0;
547 if (pos >= dictionarySize)
548 pos += dictionarySize;
549 previousByte = dictionary[pos];
550 dictionary[dictionaryPos] = previousByte;
551 if (++dictionaryPos == dictionarySize)
554 previousByte = outStream[nowPos - rep0];
556 outStream[nowPos++] = previousByte;
563 if(RangeDecoderBitDecode(p + IsRepG1 + state, &rd) == 0)
567 if(RangeDecoderBitDecode(p + IsRepG2 + state, &rd) == 0)
579 len = LzmaLenDecode(p + RepLenCoder, &rd, posState);
580 state = state < 7 ? 8 : 11;
588 state = state < 7 ? 7 : 10;
589 len = LzmaLenDecode(p + LenCoder, &rd, posState);
590 posSlot = RangeDecoderBitTreeDecode(p + PosSlot +
591 ((len < kNumLenToPosStates ? len : kNumLenToPosStates - 1) <<
592 kNumPosSlotBits), kNumPosSlotBits, &rd);
593 if (posSlot >= kStartPosModelIndex)
595 int numDirectBits = ((posSlot >> 1) - 1);
596 rep0 = ((2 | ((UInt32)posSlot & 1)) << numDirectBits);
597 if (posSlot < kEndPosModelIndex)
599 rep0 += RangeDecoderReverseBitTreeDecode(
600 p + SpecPos + rep0 - posSlot - 1, numDirectBits, &rd);
604 rep0 += RangeDecoderDecodeDirectBits(&rd,
605 numDirectBits - kNumAlignBits) << kNumAlignBits;
606 rep0 += RangeDecoderReverseBitTreeDecode(p + Align, kNumAlignBits, &rd);
613 if (rep0 == (UInt32)(0))
615 /* it's for stream version */
620 #ifdef _LZMA_OUT_READ
625 return LZMA_RESULT_DATA_ERROR;
630 #ifdef _LZMA_OUT_READ
631 UInt32 pos = dictionaryPos - rep0;
632 if (pos >= dictionarySize)
633 pos += dictionarySize;
634 previousByte = dictionary[pos];
635 dictionary[dictionaryPos] = previousByte;
636 if (++dictionaryPos == dictionarySize)
639 previousByte = outStream[nowPos - rep0];
641 outStream[nowPos++] = previousByte;
644 while(len > 0 && nowPos < outSize);
648 #ifdef _LZMA_OUT_READ
649 vs->RangeDecoder = rd;
650 vs->DictionaryPos = dictionaryPos;
651 vs->GlobalPos = globalPos + nowPos;
657 vs->PreviousIsMatch = previousIsMatch;
661 *outSizeProcessed = nowPos;
662 return LZMA_RESULT_OK;