nuclear@14: /* LzmaDec.c -- LZMA Decoder nuclear@14: 2008-11-06 : Igor Pavlov : Public domain */ nuclear@14: nuclear@14: #include "LzmaDec.h" nuclear@14: nuclear@14: #include nuclear@14: nuclear@14: #define kNumTopBits 24 nuclear@14: #define kTopValue ((UInt32)1 << kNumTopBits) nuclear@14: nuclear@14: #define kNumBitModelTotalBits 11 nuclear@14: #define kBitModelTotal (1 << kNumBitModelTotalBits) nuclear@14: #define kNumMoveBits 5 nuclear@14: nuclear@14: #define RC_INIT_SIZE 5 nuclear@14: nuclear@14: #define NORMALIZE if (range < kTopValue) { range <<= 8; code = (code << 8) | (*buf++); } nuclear@14: nuclear@14: #define IF_BIT_0(p) ttt = *(p); NORMALIZE; bound = (range >> kNumBitModelTotalBits) * ttt; if (code < bound) nuclear@14: #define UPDATE_0(p) range = bound; *(p) = (CLzmaProb)(ttt + ((kBitModelTotal - ttt) >> kNumMoveBits)); nuclear@14: #define UPDATE_1(p) range -= bound; code -= bound; *(p) = (CLzmaProb)(ttt - (ttt >> kNumMoveBits)); nuclear@14: #define GET_BIT2(p, i, A0, A1) IF_BIT_0(p) \ nuclear@14: { UPDATE_0(p); i = (i + i); A0; } else \ nuclear@14: { UPDATE_1(p); i = (i + i) + 1; A1; } nuclear@14: #define GET_BIT(p, i) GET_BIT2(p, i, ; , ;) nuclear@14: nuclear@14: #define TREE_GET_BIT(probs, i) { GET_BIT((probs + i), i); } nuclear@14: #define TREE_DECODE(probs, limit, i) \ nuclear@14: { i = 1; do { TREE_GET_BIT(probs, i); } while (i < limit); i -= limit; } nuclear@14: nuclear@14: /* #define _LZMA_SIZE_OPT */ nuclear@14: nuclear@14: #ifdef _LZMA_SIZE_OPT nuclear@14: #define TREE_6_DECODE(probs, i) TREE_DECODE(probs, (1 << 6), i) nuclear@14: #else nuclear@14: #define TREE_6_DECODE(probs, i) \ nuclear@14: { i = 1; \ nuclear@14: TREE_GET_BIT(probs, i); \ nuclear@14: TREE_GET_BIT(probs, i); \ nuclear@14: TREE_GET_BIT(probs, i); \ nuclear@14: TREE_GET_BIT(probs, i); \ nuclear@14: TREE_GET_BIT(probs, i); \ nuclear@14: TREE_GET_BIT(probs, i); \ nuclear@14: i -= 0x40; } nuclear@14: #endif nuclear@14: nuclear@14: #define NORMALIZE_CHECK if (range < kTopValue) { if (buf >= bufLimit) return DUMMY_ERROR; range <<= 8; code = (code << 8) | (*buf++); } nuclear@14: nuclear@14: #define IF_BIT_0_CHECK(p) ttt = *(p); NORMALIZE_CHECK; bound = (range >> kNumBitModelTotalBits) * ttt; if (code < bound) nuclear@14: #define UPDATE_0_CHECK range = bound; nuclear@14: #define UPDATE_1_CHECK range -= bound; code -= bound; nuclear@14: #define GET_BIT2_CHECK(p, i, A0, A1) IF_BIT_0_CHECK(p) \ nuclear@14: { UPDATE_0_CHECK; i = (i + i); A0; } else \ nuclear@14: { UPDATE_1_CHECK; i = (i + i) + 1; A1; } nuclear@14: #define GET_BIT_CHECK(p, i) GET_BIT2_CHECK(p, i, ; , ;) nuclear@14: #define TREE_DECODE_CHECK(probs, limit, i) \ nuclear@14: { i = 1; do { GET_BIT_CHECK(probs + i, i) } while (i < limit); i -= limit; } nuclear@14: nuclear@14: nuclear@14: #define kNumPosBitsMax 4 nuclear@14: #define kNumPosStatesMax (1 << kNumPosBitsMax) nuclear@14: nuclear@14: #define kLenNumLowBits 3 nuclear@14: #define kLenNumLowSymbols (1 << kLenNumLowBits) nuclear@14: #define kLenNumMidBits 3 nuclear@14: #define kLenNumMidSymbols (1 << kLenNumMidBits) nuclear@14: #define kLenNumHighBits 8 nuclear@14: #define kLenNumHighSymbols (1 << kLenNumHighBits) nuclear@14: nuclear@14: #define LenChoice 0 nuclear@14: #define LenChoice2 (LenChoice + 1) nuclear@14: #define LenLow (LenChoice2 + 1) nuclear@14: #define LenMid (LenLow + (kNumPosStatesMax << kLenNumLowBits)) nuclear@14: #define LenHigh (LenMid + (kNumPosStatesMax << kLenNumMidBits)) nuclear@14: #define kNumLenProbs (LenHigh + kLenNumHighSymbols) nuclear@14: nuclear@14: nuclear@14: #define kNumStates 12 nuclear@14: #define kNumLitStates 7 nuclear@14: nuclear@14: #define kStartPosModelIndex 4 nuclear@14: #define kEndPosModelIndex 14 nuclear@14: #define kNumFullDistances (1 << (kEndPosModelIndex >> 1)) nuclear@14: nuclear@14: #define kNumPosSlotBits 6 nuclear@14: #define kNumLenToPosStates 4 nuclear@14: nuclear@14: #define kNumAlignBits 4 nuclear@14: #define kAlignTableSize (1 << kNumAlignBits) nuclear@14: nuclear@14: #define kMatchMinLen 2 nuclear@14: #define kMatchSpecLenStart (kMatchMinLen + kLenNumLowSymbols + kLenNumMidSymbols + kLenNumHighSymbols) nuclear@14: nuclear@14: #define IsMatch 0 nuclear@14: #define IsRep (IsMatch + (kNumStates << kNumPosBitsMax)) nuclear@14: #define IsRepG0 (IsRep + kNumStates) nuclear@14: #define IsRepG1 (IsRepG0 + kNumStates) nuclear@14: #define IsRepG2 (IsRepG1 + kNumStates) nuclear@14: #define IsRep0Long (IsRepG2 + kNumStates) nuclear@14: #define PosSlot (IsRep0Long + (kNumStates << kNumPosBitsMax)) nuclear@14: #define SpecPos (PosSlot + (kNumLenToPosStates << kNumPosSlotBits)) nuclear@14: #define Align (SpecPos + kNumFullDistances - kEndPosModelIndex) nuclear@14: #define LenCoder (Align + kAlignTableSize) nuclear@14: #define RepLenCoder (LenCoder + kNumLenProbs) nuclear@14: #define Literal (RepLenCoder + kNumLenProbs) nuclear@14: nuclear@14: #define LZMA_BASE_SIZE 1846 nuclear@14: #define LZMA_LIT_SIZE 768 nuclear@14: nuclear@14: #define LzmaProps_GetNumProbs(p) ((UInt32)LZMA_BASE_SIZE + (LZMA_LIT_SIZE << ((p)->lc + (p)->lp))) nuclear@14: nuclear@14: #if Literal != LZMA_BASE_SIZE nuclear@14: StopCompilingDueBUG nuclear@14: #endif nuclear@14: nuclear@14: static const Byte kLiteralNextStates[kNumStates * 2] = nuclear@14: { nuclear@14: 0, 0, 0, 0, 1, 2, 3, 4, 5, 6, 4, 5, nuclear@14: 7, 7, 7, 7, 7, 7, 7, 10, 10, 10, 10, 10 nuclear@14: }; nuclear@14: nuclear@14: #define LZMA_DIC_MIN (1 << 12) nuclear@14: nuclear@14: /* First LZMA-symbol is always decoded. nuclear@14: And it decodes new LZMA-symbols while (buf < bufLimit), but "buf" is without last normalization nuclear@14: Out: nuclear@14: Result: nuclear@14: SZ_OK - OK nuclear@14: SZ_ERROR_DATA - Error nuclear@14: p->remainLen: nuclear@14: < kMatchSpecLenStart : normal remain nuclear@14: = kMatchSpecLenStart : finished nuclear@14: = kMatchSpecLenStart + 1 : Flush marker nuclear@14: = kMatchSpecLenStart + 2 : State Init Marker nuclear@14: */ nuclear@14: nuclear@14: static int MY_FAST_CALL LzmaDec_DecodeReal(CLzmaDec *p, SizeT limit, const Byte *bufLimit) nuclear@14: { nuclear@14: CLzmaProb *probs = p->probs; nuclear@14: nuclear@14: unsigned state = p->state; nuclear@14: UInt32 rep0 = p->reps[0], rep1 = p->reps[1], rep2 = p->reps[2], rep3 = p->reps[3]; nuclear@14: unsigned pbMask = ((unsigned)1 << (p->prop.pb)) - 1; nuclear@14: unsigned lpMask = ((unsigned)1 << (p->prop.lp)) - 1; nuclear@14: unsigned lc = p->prop.lc; nuclear@14: nuclear@14: Byte *dic = p->dic; nuclear@14: SizeT dicBufSize = p->dicBufSize; nuclear@14: SizeT dicPos = p->dicPos; nuclear@14: nuclear@14: UInt32 processedPos = p->processedPos; nuclear@14: UInt32 checkDicSize = p->checkDicSize; nuclear@14: unsigned len = 0; nuclear@14: nuclear@14: const Byte *buf = p->buf; nuclear@14: UInt32 range = p->range; nuclear@14: UInt32 code = p->code; nuclear@14: nuclear@14: do nuclear@14: { nuclear@14: CLzmaProb *prob; nuclear@14: UInt32 bound; nuclear@14: unsigned ttt; nuclear@14: unsigned posState = processedPos & pbMask; nuclear@14: nuclear@14: prob = probs + IsMatch + (state << kNumPosBitsMax) + posState; nuclear@14: IF_BIT_0(prob) nuclear@14: { nuclear@14: unsigned symbol; nuclear@14: UPDATE_0(prob); nuclear@14: prob = probs + Literal; nuclear@14: if (checkDicSize != 0 || processedPos != 0) nuclear@14: prob += (LZMA_LIT_SIZE * (((processedPos & lpMask) << lc) + nuclear@14: (dic[(dicPos == 0 ? dicBufSize : dicPos) - 1] >> (8 - lc)))); nuclear@14: nuclear@14: if (state < kNumLitStates) nuclear@14: { nuclear@14: symbol = 1; nuclear@14: do { GET_BIT(prob + symbol, symbol) } while (symbol < 0x100); nuclear@14: } nuclear@14: else nuclear@14: { nuclear@14: unsigned matchByte = p->dic[(dicPos - rep0) + ((dicPos < rep0) ? dicBufSize : 0)]; nuclear@14: unsigned offs = 0x100; nuclear@14: symbol = 1; nuclear@14: do nuclear@14: { nuclear@14: unsigned bit; nuclear@14: CLzmaProb *probLit; nuclear@14: matchByte <<= 1; nuclear@14: bit = (matchByte & offs); nuclear@14: probLit = prob + offs + bit + symbol; nuclear@14: GET_BIT2(probLit, symbol, offs &= ~bit, offs &= bit) nuclear@14: } nuclear@14: while (symbol < 0x100); nuclear@14: } nuclear@14: dic[dicPos++] = (Byte)symbol; nuclear@14: processedPos++; nuclear@14: nuclear@14: state = kLiteralNextStates[state]; nuclear@14: /* if (state < 4) state = 0; else if (state < 10) state -= 3; else state -= 6; */ nuclear@14: continue; nuclear@14: } nuclear@14: else nuclear@14: { nuclear@14: UPDATE_1(prob); nuclear@14: prob = probs + IsRep + state; nuclear@14: IF_BIT_0(prob) nuclear@14: { nuclear@14: UPDATE_0(prob); nuclear@14: state += kNumStates; nuclear@14: prob = probs + LenCoder; nuclear@14: } nuclear@14: else nuclear@14: { nuclear@14: UPDATE_1(prob); nuclear@14: if (checkDicSize == 0 && processedPos == 0) nuclear@14: return SZ_ERROR_DATA; nuclear@14: prob = probs + IsRepG0 + state; nuclear@14: IF_BIT_0(prob) nuclear@14: { nuclear@14: UPDATE_0(prob); nuclear@14: prob = probs + IsRep0Long + (state << kNumPosBitsMax) + posState; nuclear@14: IF_BIT_0(prob) nuclear@14: { nuclear@14: UPDATE_0(prob); nuclear@14: dic[dicPos] = dic[(dicPos - rep0) + ((dicPos < rep0) ? dicBufSize : 0)]; nuclear@14: dicPos++; nuclear@14: processedPos++; nuclear@14: state = state < kNumLitStates ? 9 : 11; nuclear@14: continue; nuclear@14: } nuclear@14: UPDATE_1(prob); nuclear@14: } nuclear@14: else nuclear@14: { nuclear@14: UInt32 distance; nuclear@14: UPDATE_1(prob); nuclear@14: prob = probs + IsRepG1 + state; nuclear@14: IF_BIT_0(prob) nuclear@14: { nuclear@14: UPDATE_0(prob); nuclear@14: distance = rep1; nuclear@14: } nuclear@14: else nuclear@14: { nuclear@14: UPDATE_1(prob); nuclear@14: prob = probs + IsRepG2 + state; nuclear@14: IF_BIT_0(prob) nuclear@14: { nuclear@14: UPDATE_0(prob); nuclear@14: distance = rep2; nuclear@14: } nuclear@14: else nuclear@14: { nuclear@14: UPDATE_1(prob); nuclear@14: distance = rep3; nuclear@14: rep3 = rep2; nuclear@14: } nuclear@14: rep2 = rep1; nuclear@14: } nuclear@14: rep1 = rep0; nuclear@14: rep0 = distance; nuclear@14: } nuclear@14: state = state < kNumLitStates ? 8 : 11; nuclear@14: prob = probs + RepLenCoder; nuclear@14: } nuclear@14: { nuclear@14: unsigned limit, offset; nuclear@14: CLzmaProb *probLen = prob + LenChoice; nuclear@14: IF_BIT_0(probLen) nuclear@14: { nuclear@14: UPDATE_0(probLen); nuclear@14: probLen = prob + LenLow + (posState << kLenNumLowBits); nuclear@14: offset = 0; nuclear@14: limit = (1 << kLenNumLowBits); nuclear@14: } nuclear@14: else nuclear@14: { nuclear@14: UPDATE_1(probLen); nuclear@14: probLen = prob + LenChoice2; nuclear@14: IF_BIT_0(probLen) nuclear@14: { nuclear@14: UPDATE_0(probLen); nuclear@14: probLen = prob + LenMid + (posState << kLenNumMidBits); nuclear@14: offset = kLenNumLowSymbols; nuclear@14: limit = (1 << kLenNumMidBits); nuclear@14: } nuclear@14: else nuclear@14: { nuclear@14: UPDATE_1(probLen); nuclear@14: probLen = prob + LenHigh; nuclear@14: offset = kLenNumLowSymbols + kLenNumMidSymbols; nuclear@14: limit = (1 << kLenNumHighBits); nuclear@14: } nuclear@14: } nuclear@14: TREE_DECODE(probLen, limit, len); nuclear@14: len += offset; nuclear@14: } nuclear@14: nuclear@14: if (state >= kNumStates) nuclear@14: { nuclear@14: UInt32 distance; nuclear@14: prob = probs + PosSlot + nuclear@14: ((len < kNumLenToPosStates ? len : kNumLenToPosStates - 1) << kNumPosSlotBits); nuclear@14: TREE_6_DECODE(prob, distance); nuclear@14: if (distance >= kStartPosModelIndex) nuclear@14: { nuclear@14: unsigned posSlot = (unsigned)distance; nuclear@14: int numDirectBits = (int)(((distance >> 1) - 1)); nuclear@14: distance = (2 | (distance & 1)); nuclear@14: if (posSlot < kEndPosModelIndex) nuclear@14: { nuclear@14: distance <<= numDirectBits; nuclear@14: prob = probs + SpecPos + distance - posSlot - 1; nuclear@14: { nuclear@14: UInt32 mask = 1; nuclear@14: unsigned i = 1; nuclear@14: do nuclear@14: { nuclear@14: GET_BIT2(prob + i, i, ; , distance |= mask); nuclear@14: mask <<= 1; nuclear@14: } nuclear@14: while (--numDirectBits != 0); nuclear@14: } nuclear@14: } nuclear@14: else nuclear@14: { nuclear@14: numDirectBits -= kNumAlignBits; nuclear@14: do nuclear@14: { nuclear@14: NORMALIZE nuclear@14: range >>= 1; nuclear@14: nuclear@14: { nuclear@14: UInt32 t; nuclear@14: code -= range; nuclear@14: t = (0 - ((UInt32)code >> 31)); /* (UInt32)((Int32)code >> 31) */ nuclear@14: distance = (distance << 1) + (t + 1); nuclear@14: code += range & t; nuclear@14: } nuclear@14: /* nuclear@14: distance <<= 1; nuclear@14: if (code >= range) nuclear@14: { nuclear@14: code -= range; nuclear@14: distance |= 1; nuclear@14: } nuclear@14: */ nuclear@14: } nuclear@14: while (--numDirectBits != 0); nuclear@14: prob = probs + Align; nuclear@14: distance <<= kNumAlignBits; nuclear@14: { nuclear@14: unsigned i = 1; nuclear@14: GET_BIT2(prob + i, i, ; , distance |= 1); nuclear@14: GET_BIT2(prob + i, i, ; , distance |= 2); nuclear@14: GET_BIT2(prob + i, i, ; , distance |= 4); nuclear@14: GET_BIT2(prob + i, i, ; , distance |= 8); nuclear@14: } nuclear@14: if (distance == (UInt32)0xFFFFFFFF) nuclear@14: { nuclear@14: len += kMatchSpecLenStart; nuclear@14: state -= kNumStates; nuclear@14: break; nuclear@14: } nuclear@14: } nuclear@14: } nuclear@14: rep3 = rep2; nuclear@14: rep2 = rep1; nuclear@14: rep1 = rep0; nuclear@14: rep0 = distance + 1; nuclear@14: if (checkDicSize == 0) nuclear@14: { nuclear@14: if (distance >= processedPos) nuclear@14: return SZ_ERROR_DATA; nuclear@14: } nuclear@14: else if (distance >= checkDicSize) nuclear@14: return SZ_ERROR_DATA; nuclear@14: state = (state < kNumStates + kNumLitStates) ? kNumLitStates : kNumLitStates + 3; nuclear@14: /* state = kLiteralNextStates[state]; */ nuclear@14: } nuclear@14: nuclear@14: len += kMatchMinLen; nuclear@14: nuclear@14: if (limit == dicPos) nuclear@14: return SZ_ERROR_DATA; nuclear@14: { nuclear@14: SizeT rem = limit - dicPos; nuclear@14: unsigned curLen = ((rem < len) ? (unsigned)rem : len); nuclear@14: SizeT pos = (dicPos - rep0) + ((dicPos < rep0) ? dicBufSize : 0); nuclear@14: nuclear@14: processedPos += curLen; nuclear@14: nuclear@14: len -= curLen; nuclear@14: if (pos + curLen <= dicBufSize) nuclear@14: { nuclear@14: Byte *dest = dic + dicPos; nuclear@14: ptrdiff_t src = (ptrdiff_t)pos - (ptrdiff_t)dicPos; nuclear@14: const Byte *lim = dest + curLen; nuclear@14: dicPos += curLen; nuclear@14: do nuclear@14: *(dest) = (Byte)*(dest + src); nuclear@14: while (++dest != lim); nuclear@14: } nuclear@14: else nuclear@14: { nuclear@14: do nuclear@14: { nuclear@14: dic[dicPos++] = dic[pos]; nuclear@14: if (++pos == dicBufSize) nuclear@14: pos = 0; nuclear@14: } nuclear@14: while (--curLen != 0); nuclear@14: } nuclear@14: } nuclear@14: } nuclear@14: } nuclear@14: while (dicPos < limit && buf < bufLimit); nuclear@14: NORMALIZE; nuclear@14: p->buf = buf; nuclear@14: p->range = range; nuclear@14: p->code = code; nuclear@14: p->remainLen = len; nuclear@14: p->dicPos = dicPos; nuclear@14: p->processedPos = processedPos; nuclear@14: p->reps[0] = rep0; nuclear@14: p->reps[1] = rep1; nuclear@14: p->reps[2] = rep2; nuclear@14: p->reps[3] = rep3; nuclear@14: p->state = state; nuclear@14: nuclear@14: return SZ_OK; nuclear@14: } nuclear@14: nuclear@14: static void MY_FAST_CALL LzmaDec_WriteRem(CLzmaDec *p, SizeT limit) nuclear@14: { nuclear@14: if (p->remainLen != 0 && p->remainLen < kMatchSpecLenStart) nuclear@14: { nuclear@14: Byte *dic = p->dic; nuclear@14: SizeT dicPos = p->dicPos; nuclear@14: SizeT dicBufSize = p->dicBufSize; nuclear@14: unsigned len = p->remainLen; nuclear@14: UInt32 rep0 = p->reps[0]; nuclear@14: if (limit - dicPos < len) nuclear@14: len = (unsigned)(limit - dicPos); nuclear@14: nuclear@14: if (p->checkDicSize == 0 && p->prop.dicSize - p->processedPos <= len) nuclear@14: p->checkDicSize = p->prop.dicSize; nuclear@14: nuclear@14: p->processedPos += len; nuclear@14: p->remainLen -= len; nuclear@14: while (len-- != 0) nuclear@14: { nuclear@14: dic[dicPos] = dic[(dicPos - rep0) + ((dicPos < rep0) ? dicBufSize : 0)]; nuclear@14: dicPos++; nuclear@14: } nuclear@14: p->dicPos = dicPos; nuclear@14: } nuclear@14: } nuclear@14: nuclear@14: static int MY_FAST_CALL LzmaDec_DecodeReal2(CLzmaDec *p, SizeT limit, const Byte *bufLimit) nuclear@14: { nuclear@14: do nuclear@14: { nuclear@14: SizeT limit2 = limit; nuclear@14: if (p->checkDicSize == 0) nuclear@14: { nuclear@14: UInt32 rem = p->prop.dicSize - p->processedPos; nuclear@14: if (limit - p->dicPos > rem) nuclear@14: limit2 = p->dicPos + rem; nuclear@14: } nuclear@14: RINOK(LzmaDec_DecodeReal(p, limit2, bufLimit)); nuclear@14: if (p->processedPos >= p->prop.dicSize) nuclear@14: p->checkDicSize = p->prop.dicSize; nuclear@14: LzmaDec_WriteRem(p, limit); nuclear@14: } nuclear@14: while (p->dicPos < limit && p->buf < bufLimit && p->remainLen < kMatchSpecLenStart); nuclear@14: nuclear@14: if (p->remainLen > kMatchSpecLenStart) nuclear@14: { nuclear@14: p->remainLen = kMatchSpecLenStart; nuclear@14: } nuclear@14: return 0; nuclear@14: } nuclear@14: nuclear@14: typedef enum nuclear@14: { nuclear@14: DUMMY_ERROR, /* unexpected end of input stream */ nuclear@14: DUMMY_LIT, nuclear@14: DUMMY_MATCH, nuclear@14: DUMMY_REP nuclear@14: } ELzmaDummy; nuclear@14: nuclear@14: static ELzmaDummy LzmaDec_TryDummy(const CLzmaDec *p, const Byte *buf, SizeT inSize) nuclear@14: { nuclear@14: UInt32 range = p->range; nuclear@14: UInt32 code = p->code; nuclear@14: const Byte *bufLimit = buf + inSize; nuclear@14: CLzmaProb *probs = p->probs; nuclear@14: unsigned state = p->state; nuclear@14: ELzmaDummy res; nuclear@14: nuclear@14: { nuclear@14: CLzmaProb *prob; nuclear@14: UInt32 bound; nuclear@14: unsigned ttt; nuclear@14: unsigned posState = (p->processedPos) & ((1 << p->prop.pb) - 1); nuclear@14: nuclear@14: prob = probs + IsMatch + (state << kNumPosBitsMax) + posState; nuclear@14: IF_BIT_0_CHECK(prob) nuclear@14: { nuclear@14: UPDATE_0_CHECK nuclear@14: nuclear@14: /* if (bufLimit - buf >= 7) return DUMMY_LIT; */ nuclear@14: nuclear@14: prob = probs + Literal; nuclear@14: if (p->checkDicSize != 0 || p->processedPos != 0) nuclear@14: prob += (LZMA_LIT_SIZE * nuclear@14: ((((p->processedPos) & ((1 << (p->prop.lp)) - 1)) << p->prop.lc) + nuclear@14: (p->dic[(p->dicPos == 0 ? p->dicBufSize : p->dicPos) - 1] >> (8 - p->prop.lc)))); nuclear@14: nuclear@14: if (state < kNumLitStates) nuclear@14: { nuclear@14: unsigned symbol = 1; nuclear@14: do { GET_BIT_CHECK(prob + symbol, symbol) } while (symbol < 0x100); nuclear@14: } nuclear@14: else nuclear@14: { nuclear@14: unsigned matchByte = p->dic[p->dicPos - p->reps[0] + nuclear@14: ((p->dicPos < p->reps[0]) ? p->dicBufSize : 0)]; nuclear@14: unsigned offs = 0x100; nuclear@14: unsigned symbol = 1; nuclear@14: do nuclear@14: { nuclear@14: unsigned bit; nuclear@14: CLzmaProb *probLit; nuclear@14: matchByte <<= 1; nuclear@14: bit = (matchByte & offs); nuclear@14: probLit = prob + offs + bit + symbol; nuclear@14: GET_BIT2_CHECK(probLit, symbol, offs &= ~bit, offs &= bit) nuclear@14: } nuclear@14: while (symbol < 0x100); nuclear@14: } nuclear@14: res = DUMMY_LIT; nuclear@14: } nuclear@14: else nuclear@14: { nuclear@14: unsigned len; nuclear@14: UPDATE_1_CHECK; nuclear@14: nuclear@14: prob = probs + IsRep + state; nuclear@14: IF_BIT_0_CHECK(prob) nuclear@14: { nuclear@14: UPDATE_0_CHECK; nuclear@14: state = 0; nuclear@14: prob = probs + LenCoder; nuclear@14: res = DUMMY_MATCH; nuclear@14: } nuclear@14: else nuclear@14: { nuclear@14: UPDATE_1_CHECK; nuclear@14: res = DUMMY_REP; nuclear@14: prob = probs + IsRepG0 + state; nuclear@14: IF_BIT_0_CHECK(prob) nuclear@14: { nuclear@14: UPDATE_0_CHECK; nuclear@14: prob = probs + IsRep0Long + (state << kNumPosBitsMax) + posState; nuclear@14: IF_BIT_0_CHECK(prob) nuclear@14: { nuclear@14: UPDATE_0_CHECK; nuclear@14: NORMALIZE_CHECK; nuclear@14: return DUMMY_REP; nuclear@14: } nuclear@14: else nuclear@14: { nuclear@14: UPDATE_1_CHECK; nuclear@14: } nuclear@14: } nuclear@14: else nuclear@14: { nuclear@14: UPDATE_1_CHECK; nuclear@14: prob = probs + IsRepG1 + state; nuclear@14: IF_BIT_0_CHECK(prob) nuclear@14: { nuclear@14: UPDATE_0_CHECK; nuclear@14: } nuclear@14: else nuclear@14: { nuclear@14: UPDATE_1_CHECK; nuclear@14: prob = probs + IsRepG2 + state; nuclear@14: IF_BIT_0_CHECK(prob) nuclear@14: { nuclear@14: UPDATE_0_CHECK; nuclear@14: } nuclear@14: else nuclear@14: { nuclear@14: UPDATE_1_CHECK; nuclear@14: } nuclear@14: } nuclear@14: } nuclear@14: state = kNumStates; nuclear@14: prob = probs + RepLenCoder; nuclear@14: } nuclear@14: { nuclear@14: unsigned limit, offset; nuclear@14: CLzmaProb *probLen = prob + LenChoice; nuclear@14: IF_BIT_0_CHECK(probLen) nuclear@14: { nuclear@14: UPDATE_0_CHECK; nuclear@14: probLen = prob + LenLow + (posState << kLenNumLowBits); nuclear@14: offset = 0; nuclear@14: limit = 1 << kLenNumLowBits; nuclear@14: } nuclear@14: else nuclear@14: { nuclear@14: UPDATE_1_CHECK; nuclear@14: probLen = prob + LenChoice2; nuclear@14: IF_BIT_0_CHECK(probLen) nuclear@14: { nuclear@14: UPDATE_0_CHECK; nuclear@14: probLen = prob + LenMid + (posState << kLenNumMidBits); nuclear@14: offset = kLenNumLowSymbols; nuclear@14: limit = 1 << kLenNumMidBits; nuclear@14: } nuclear@14: else nuclear@14: { nuclear@14: UPDATE_1_CHECK; nuclear@14: probLen = prob + LenHigh; nuclear@14: offset = kLenNumLowSymbols + kLenNumMidSymbols; nuclear@14: limit = 1 << kLenNumHighBits; nuclear@14: } nuclear@14: } nuclear@14: TREE_DECODE_CHECK(probLen, limit, len); nuclear@14: len += offset; nuclear@14: } nuclear@14: nuclear@14: if (state < 4) nuclear@14: { nuclear@14: unsigned posSlot; nuclear@14: prob = probs + PosSlot + nuclear@14: ((len < kNumLenToPosStates ? len : kNumLenToPosStates - 1) << nuclear@14: kNumPosSlotBits); nuclear@14: TREE_DECODE_CHECK(prob, 1 << kNumPosSlotBits, posSlot); nuclear@14: if (posSlot >= kStartPosModelIndex) nuclear@14: { nuclear@14: int numDirectBits = ((posSlot >> 1) - 1); nuclear@14: nuclear@14: /* if (bufLimit - buf >= 8) return DUMMY_MATCH; */ nuclear@14: nuclear@14: if (posSlot < kEndPosModelIndex) nuclear@14: { nuclear@14: prob = probs + SpecPos + ((2 | (posSlot & 1)) << numDirectBits) - posSlot - 1; nuclear@14: } nuclear@14: else nuclear@14: { nuclear@14: numDirectBits -= kNumAlignBits; nuclear@14: do nuclear@14: { nuclear@14: NORMALIZE_CHECK nuclear@14: range >>= 1; nuclear@14: code -= range & (((code - range) >> 31) - 1); nuclear@14: /* if (code >= range) code -= range; */ nuclear@14: } nuclear@14: while (--numDirectBits != 0); nuclear@14: prob = probs + Align; nuclear@14: numDirectBits = kNumAlignBits; nuclear@14: } nuclear@14: { nuclear@14: unsigned i = 1; nuclear@14: do nuclear@14: { nuclear@14: GET_BIT_CHECK(prob + i, i); nuclear@14: } nuclear@14: while (--numDirectBits != 0); nuclear@14: } nuclear@14: } nuclear@14: } nuclear@14: } nuclear@14: } nuclear@14: NORMALIZE_CHECK; nuclear@14: return res; nuclear@14: } nuclear@14: nuclear@14: nuclear@14: static void LzmaDec_InitRc(CLzmaDec *p, const Byte *data) nuclear@14: { nuclear@14: p->code = ((UInt32)data[1] << 24) | ((UInt32)data[2] << 16) | ((UInt32)data[3] << 8) | ((UInt32)data[4]); nuclear@14: p->range = 0xFFFFFFFF; nuclear@14: p->needFlush = 0; nuclear@14: } nuclear@14: nuclear@14: void LzmaDec_InitDicAndState(CLzmaDec *p, Bool initDic, Bool initState) nuclear@14: { nuclear@14: p->needFlush = 1; nuclear@14: p->remainLen = 0; nuclear@14: p->tempBufSize = 0; nuclear@14: nuclear@14: if (initDic) nuclear@14: { nuclear@14: p->processedPos = 0; nuclear@14: p->checkDicSize = 0; nuclear@14: p->needInitState = 1; nuclear@14: } nuclear@14: if (initState) nuclear@14: p->needInitState = 1; nuclear@14: } nuclear@14: nuclear@14: void LzmaDec_Init(CLzmaDec *p) nuclear@14: { nuclear@14: p->dicPos = 0; nuclear@14: LzmaDec_InitDicAndState(p, True, True); nuclear@14: } nuclear@14: nuclear@14: static void LzmaDec_InitStateReal(CLzmaDec *p) nuclear@14: { nuclear@14: UInt32 numProbs = Literal + ((UInt32)LZMA_LIT_SIZE << (p->prop.lc + p->prop.lp)); nuclear@14: UInt32 i; nuclear@14: CLzmaProb *probs = p->probs; nuclear@14: for (i = 0; i < numProbs; i++) nuclear@14: probs[i] = kBitModelTotal >> 1; nuclear@14: p->reps[0] = p->reps[1] = p->reps[2] = p->reps[3] = 1; nuclear@14: p->state = 0; nuclear@14: p->needInitState = 0; nuclear@14: } nuclear@14: nuclear@14: SRes LzmaDec_DecodeToDic(CLzmaDec *p, SizeT dicLimit, const Byte *src, SizeT *srcLen, nuclear@14: ELzmaFinishMode finishMode, ELzmaStatus *status) nuclear@14: { nuclear@14: SizeT inSize = *srcLen; nuclear@14: (*srcLen) = 0; nuclear@14: LzmaDec_WriteRem(p, dicLimit); nuclear@14: nuclear@14: *status = LZMA_STATUS_NOT_SPECIFIED; nuclear@14: nuclear@14: while (p->remainLen != kMatchSpecLenStart) nuclear@14: { nuclear@14: int checkEndMarkNow; nuclear@14: nuclear@14: if (p->needFlush != 0) nuclear@14: { nuclear@14: for (; inSize > 0 && p->tempBufSize < RC_INIT_SIZE; (*srcLen)++, inSize--) nuclear@14: p->tempBuf[p->tempBufSize++] = *src++; nuclear@14: if (p->tempBufSize < RC_INIT_SIZE) nuclear@14: { nuclear@14: *status = LZMA_STATUS_NEEDS_MORE_INPUT; nuclear@14: return SZ_OK; nuclear@14: } nuclear@14: if (p->tempBuf[0] != 0) nuclear@14: return SZ_ERROR_DATA; nuclear@14: nuclear@14: LzmaDec_InitRc(p, p->tempBuf); nuclear@14: p->tempBufSize = 0; nuclear@14: } nuclear@14: nuclear@14: checkEndMarkNow = 0; nuclear@14: if (p->dicPos >= dicLimit) nuclear@14: { nuclear@14: if (p->remainLen == 0 && p->code == 0) nuclear@14: { nuclear@14: *status = LZMA_STATUS_MAYBE_FINISHED_WITHOUT_MARK; nuclear@14: return SZ_OK; nuclear@14: } nuclear@14: if (finishMode == LZMA_FINISH_ANY) nuclear@14: { nuclear@14: *status = LZMA_STATUS_NOT_FINISHED; nuclear@14: return SZ_OK; nuclear@14: } nuclear@14: if (p->remainLen != 0) nuclear@14: { nuclear@14: *status = LZMA_STATUS_NOT_FINISHED; nuclear@14: return SZ_ERROR_DATA; nuclear@14: } nuclear@14: checkEndMarkNow = 1; nuclear@14: } nuclear@14: nuclear@14: if (p->needInitState) nuclear@14: LzmaDec_InitStateReal(p); nuclear@14: nuclear@14: if (p->tempBufSize == 0) nuclear@14: { nuclear@14: SizeT processed; nuclear@14: const Byte *bufLimit; nuclear@14: if (inSize < LZMA_REQUIRED_INPUT_MAX || checkEndMarkNow) nuclear@14: { nuclear@14: int dummyRes = LzmaDec_TryDummy(p, src, inSize); nuclear@14: if (dummyRes == DUMMY_ERROR) nuclear@14: { nuclear@14: memcpy(p->tempBuf, src, inSize); nuclear@14: p->tempBufSize = (unsigned)inSize; nuclear@14: (*srcLen) += inSize; nuclear@14: *status = LZMA_STATUS_NEEDS_MORE_INPUT; nuclear@14: return SZ_OK; nuclear@14: } nuclear@14: if (checkEndMarkNow && dummyRes != DUMMY_MATCH) nuclear@14: { nuclear@14: *status = LZMA_STATUS_NOT_FINISHED; nuclear@14: return SZ_ERROR_DATA; nuclear@14: } nuclear@14: bufLimit = src; nuclear@14: } nuclear@14: else nuclear@14: bufLimit = src + inSize - LZMA_REQUIRED_INPUT_MAX; nuclear@14: p->buf = src; nuclear@14: if (LzmaDec_DecodeReal2(p, dicLimit, bufLimit) != 0) nuclear@14: return SZ_ERROR_DATA; nuclear@14: processed = (SizeT)(p->buf - src); nuclear@14: (*srcLen) += processed; nuclear@14: src += processed; nuclear@14: inSize -= processed; nuclear@14: } nuclear@14: else nuclear@14: { nuclear@14: unsigned rem = p->tempBufSize, lookAhead = 0; nuclear@14: while (rem < LZMA_REQUIRED_INPUT_MAX && lookAhead < inSize) nuclear@14: p->tempBuf[rem++] = src[lookAhead++]; nuclear@14: p->tempBufSize = rem; nuclear@14: if (rem < LZMA_REQUIRED_INPUT_MAX || checkEndMarkNow) nuclear@14: { nuclear@14: int dummyRes = LzmaDec_TryDummy(p, p->tempBuf, rem); nuclear@14: if (dummyRes == DUMMY_ERROR) nuclear@14: { nuclear@14: (*srcLen) += lookAhead; nuclear@14: *status = LZMA_STATUS_NEEDS_MORE_INPUT; nuclear@14: return SZ_OK; nuclear@14: } nuclear@14: if (checkEndMarkNow && dummyRes != DUMMY_MATCH) nuclear@14: { nuclear@14: *status = LZMA_STATUS_NOT_FINISHED; nuclear@14: return SZ_ERROR_DATA; nuclear@14: } nuclear@14: } nuclear@14: p->buf = p->tempBuf; nuclear@14: if (LzmaDec_DecodeReal2(p, dicLimit, p->buf) != 0) nuclear@14: return SZ_ERROR_DATA; nuclear@14: lookAhead -= (rem - (unsigned)(p->buf - p->tempBuf)); nuclear@14: (*srcLen) += lookAhead; nuclear@14: src += lookAhead; nuclear@14: inSize -= lookAhead; nuclear@14: p->tempBufSize = 0; nuclear@14: } nuclear@14: } nuclear@14: if (p->code == 0) nuclear@14: *status = LZMA_STATUS_FINISHED_WITH_MARK; nuclear@14: return (p->code == 0) ? SZ_OK : SZ_ERROR_DATA; nuclear@14: } nuclear@14: nuclear@14: SRes LzmaDec_DecodeToBuf(CLzmaDec *p, Byte *dest, SizeT *destLen, const Byte *src, SizeT *srcLen, ELzmaFinishMode finishMode, ELzmaStatus *status) nuclear@14: { nuclear@14: SizeT outSize = *destLen; nuclear@14: SizeT inSize = *srcLen; nuclear@14: *srcLen = *destLen = 0; nuclear@14: for (;;) nuclear@14: { nuclear@14: SizeT inSizeCur = inSize, outSizeCur, dicPos; nuclear@14: ELzmaFinishMode curFinishMode; nuclear@14: SRes res; nuclear@14: if (p->dicPos == p->dicBufSize) nuclear@14: p->dicPos = 0; nuclear@14: dicPos = p->dicPos; nuclear@14: if (outSize > p->dicBufSize - dicPos) nuclear@14: { nuclear@14: outSizeCur = p->dicBufSize; nuclear@14: curFinishMode = LZMA_FINISH_ANY; nuclear@14: } nuclear@14: else nuclear@14: { nuclear@14: outSizeCur = dicPos + outSize; nuclear@14: curFinishMode = finishMode; nuclear@14: } nuclear@14: nuclear@14: res = LzmaDec_DecodeToDic(p, outSizeCur, src, &inSizeCur, curFinishMode, status); nuclear@14: src += inSizeCur; nuclear@14: inSize -= inSizeCur; nuclear@14: *srcLen += inSizeCur; nuclear@14: outSizeCur = p->dicPos - dicPos; nuclear@14: memcpy(dest, p->dic + dicPos, outSizeCur); nuclear@14: dest += outSizeCur; nuclear@14: outSize -= outSizeCur; nuclear@14: *destLen += outSizeCur; nuclear@14: if (res != 0) nuclear@14: return res; nuclear@14: if (outSizeCur == 0 || outSize == 0) nuclear@14: return SZ_OK; nuclear@14: } nuclear@14: } nuclear@14: nuclear@14: void LzmaDec_FreeProbs(CLzmaDec *p, ISzAlloc *alloc) nuclear@14: { nuclear@14: alloc->Free(alloc, p->probs); nuclear@14: p->probs = 0; nuclear@14: } nuclear@14: nuclear@14: static void LzmaDec_FreeDict(CLzmaDec *p, ISzAlloc *alloc) nuclear@14: { nuclear@14: alloc->Free(alloc, p->dic); nuclear@14: p->dic = 0; nuclear@14: } nuclear@14: nuclear@14: void LzmaDec_Free(CLzmaDec *p, ISzAlloc *alloc) nuclear@14: { nuclear@14: LzmaDec_FreeProbs(p, alloc); nuclear@14: LzmaDec_FreeDict(p, alloc); nuclear@14: } nuclear@14: nuclear@14: SRes LzmaProps_Decode(CLzmaProps *p, const Byte *data, unsigned size) nuclear@14: { nuclear@14: UInt32 dicSize; nuclear@14: Byte d; nuclear@14: nuclear@14: if (size < LZMA_PROPS_SIZE) nuclear@14: return SZ_ERROR_UNSUPPORTED; nuclear@14: else nuclear@14: dicSize = data[1] | ((UInt32)data[2] << 8) | ((UInt32)data[3] << 16) | ((UInt32)data[4] << 24); nuclear@14: nuclear@14: if (dicSize < LZMA_DIC_MIN) nuclear@14: dicSize = LZMA_DIC_MIN; nuclear@14: p->dicSize = dicSize; nuclear@14: nuclear@14: d = data[0]; nuclear@14: if (d >= (9 * 5 * 5)) nuclear@14: return SZ_ERROR_UNSUPPORTED; nuclear@14: nuclear@14: p->lc = d % 9; nuclear@14: d /= 9; nuclear@14: p->pb = d / 5; nuclear@14: p->lp = d % 5; nuclear@14: nuclear@14: return SZ_OK; nuclear@14: } nuclear@14: nuclear@14: static SRes LzmaDec_AllocateProbs2(CLzmaDec *p, const CLzmaProps *propNew, ISzAlloc *alloc) nuclear@14: { nuclear@14: UInt32 numProbs = LzmaProps_GetNumProbs(propNew); nuclear@14: if (p->probs == 0 || numProbs != p->numProbs) nuclear@14: { nuclear@14: LzmaDec_FreeProbs(p, alloc); nuclear@14: p->probs = (CLzmaProb *)alloc->Alloc(alloc, numProbs * sizeof(CLzmaProb)); nuclear@14: p->numProbs = numProbs; nuclear@14: if (p->probs == 0) nuclear@14: return SZ_ERROR_MEM; nuclear@14: } nuclear@14: return SZ_OK; nuclear@14: } nuclear@14: nuclear@14: SRes LzmaDec_AllocateProbs(CLzmaDec *p, const Byte *props, unsigned propsSize, ISzAlloc *alloc) nuclear@14: { nuclear@14: CLzmaProps propNew; nuclear@14: RINOK(LzmaProps_Decode(&propNew, props, propsSize)); nuclear@14: RINOK(LzmaDec_AllocateProbs2(p, &propNew, alloc)); nuclear@14: p->prop = propNew; nuclear@14: return SZ_OK; nuclear@14: } nuclear@14: nuclear@14: SRes LzmaDec_Allocate(CLzmaDec *p, const Byte *props, unsigned propsSize, ISzAlloc *alloc) nuclear@14: { nuclear@14: CLzmaProps propNew; nuclear@14: SizeT dicBufSize; nuclear@14: RINOK(LzmaProps_Decode(&propNew, props, propsSize)); nuclear@14: RINOK(LzmaDec_AllocateProbs2(p, &propNew, alloc)); nuclear@14: dicBufSize = propNew.dicSize; nuclear@14: if (p->dic == 0 || dicBufSize != p->dicBufSize) nuclear@14: { nuclear@14: LzmaDec_FreeDict(p, alloc); nuclear@14: p->dic = (Byte *)alloc->Alloc(alloc, dicBufSize); nuclear@14: if (p->dic == 0) nuclear@14: { nuclear@14: LzmaDec_FreeProbs(p, alloc); nuclear@14: return SZ_ERROR_MEM; nuclear@14: } nuclear@14: } nuclear@14: p->dicBufSize = dicBufSize; nuclear@14: p->prop = propNew; nuclear@14: return SZ_OK; nuclear@14: } nuclear@14: nuclear@14: SRes LzmaDecode(Byte *dest, SizeT *destLen, const Byte *src, SizeT *srcLen, nuclear@14: const Byte *propData, unsigned propSize, ELzmaFinishMode finishMode, nuclear@14: ELzmaStatus *status, ISzAlloc *alloc) nuclear@14: { nuclear@14: CLzmaDec p; nuclear@14: SRes res; nuclear@14: SizeT inSize = *srcLen; nuclear@14: SizeT outSize = *destLen; nuclear@14: *srcLen = *destLen = 0; nuclear@14: if (inSize < RC_INIT_SIZE) nuclear@14: return SZ_ERROR_INPUT_EOF; nuclear@14: nuclear@14: LzmaDec_Construct(&p); nuclear@14: res = LzmaDec_AllocateProbs(&p, propData, propSize, alloc); nuclear@14: if (res != 0) nuclear@14: return res; nuclear@14: p.dic = dest; nuclear@14: p.dicBufSize = outSize; nuclear@14: nuclear@14: LzmaDec_Init(&p); nuclear@14: nuclear@14: *srcLen = inSize; nuclear@14: res = LzmaDec_DecodeToDic(&p, outSize, src, srcLen, finishMode, status); nuclear@14: nuclear@14: if (res == SZ_OK && *status == LZMA_STATUS_NEEDS_MORE_INPUT) nuclear@14: res = SZ_ERROR_INPUT_EOF; nuclear@14: nuclear@14: (*destLen) = p.dicPos; nuclear@14: LzmaDec_FreeProbs(&p, alloc); nuclear@14: return res; nuclear@14: }