Lines Matching +full:- +full:p

1 /* Lzma2Dec.c -- LZMA2 Decoder
2 2018-07-04 : Igor Pavlov : Public domain */
17 00000000 - End of data
18 00000001 U U - Uncompressed, reset dic, need reset state and set new prop
19 00000010 U U - Uncompressed, no reset
20 100uuuuu U U P P - LZMA, no reset
21 101uuuuu U U P P - LZMA, reset state
22 110uuuuu U U P P S - LZMA, reset state + set new prop
23 111uuuuu U U P P S - LZMA, reset state + set new prop, reset dic
25 u, U - Unpack Size
26 P - Pack Size
27 S - Props
32 #define LZMA2_IS_UNCOMPRESSED_STATE(p) (((p)->control & (1 << 7)) == 0) argument
35 #define LZMA2_DIC_SIZE_FROM_PROP(p) (((UInt32)2 | ((p) & 1)) << ((p) / 2 + 11)) argument
71 SRes Lzma2Dec_AllocateProbs(CLzma2Dec *p, Byte prop, ISzAllocPtr alloc) in Lzma2Dec_AllocateProbs() argument
75 return LzmaDec_AllocateProbs(&p->decoder, props, LZMA_PROPS_SIZE, alloc); in Lzma2Dec_AllocateProbs()
78 SRes Lzma2Dec_Allocate(CLzma2Dec *p, Byte prop, ISzAllocPtr alloc) in Lzma2Dec_Allocate() argument
82 return LzmaDec_Allocate(&p->decoder, props, LZMA_PROPS_SIZE, alloc); in Lzma2Dec_Allocate()
85 void Lzma2Dec_Init(CLzma2Dec *p) in Lzma2Dec_Init() argument
87 p->state = LZMA2_STATE_CONTROL; in Lzma2Dec_Init()
88 p->needInitLevel = 0xE0; in Lzma2Dec_Init()
89 p->isExtraMode = False; in Lzma2Dec_Init()
90 p->unpackSize = 0; in Lzma2Dec_Init()
92 // p->decoder.dicPos = 0; // we can use it instead of full init in Lzma2Dec_Init()
93 LzmaDec_Init(&p->decoder); in Lzma2Dec_Init()
96 static ELzma2State Lzma2Dec_UpdateState(CLzma2Dec *p, Byte b) in Lzma2Dec_UpdateState() argument
98 switch (p->state) in Lzma2Dec_UpdateState()
101 p->isExtraMode = False; in Lzma2Dec_UpdateState()
102 p->control = b; in Lzma2Dec_UpdateState()
103 PRF(printf("\n %8X", (unsigned)p->decoder.dicPos)); in Lzma2Dec_UpdateState()
107 if (LZMA2_IS_UNCOMPRESSED_STATE(p)) in Lzma2Dec_UpdateState()
110 p->needInitLevel = 0xC0; in Lzma2Dec_UpdateState()
111 else if (b > 2 || p->needInitLevel == 0xE0) in Lzma2Dec_UpdateState()
116 if (b < p->needInitLevel) in Lzma2Dec_UpdateState()
118 p->needInitLevel = 0; in Lzma2Dec_UpdateState()
119 p->unpackSize = (UInt32)(b & 0x1F) << 16; in Lzma2Dec_UpdateState()
124 p->unpackSize |= (UInt32)b << 8; in Lzma2Dec_UpdateState()
128 p->unpackSize |= (UInt32)b; in Lzma2Dec_UpdateState()
129 p->unpackSize++; in Lzma2Dec_UpdateState()
130 PRF(printf(" %7u", (unsigned)p->unpackSize)); in Lzma2Dec_UpdateState()
131 return LZMA2_IS_UNCOMPRESSED_STATE(p) ? LZMA2_STATE_DATA : LZMA2_STATE_PACK0; in Lzma2Dec_UpdateState()
134 p->packSize = (UInt32)b << 8; in Lzma2Dec_UpdateState()
138 p->packSize |= (UInt32)b; in Lzma2Dec_UpdateState()
139 p->packSize++; in Lzma2Dec_UpdateState()
140 // if (p->packSize < 5) return LZMA2_STATE_ERROR; in Lzma2Dec_UpdateState()
141 PRF(printf(" %5u", (unsigned)p->packSize)); in Lzma2Dec_UpdateState()
142 return (p->control & 0x40) ? LZMA2_STATE_PROP : LZMA2_STATE_DATA; in Lzma2Dec_UpdateState()
151 p->decoder.prop.pb = (Byte)(b / 5); in Lzma2Dec_UpdateState()
155 p->decoder.prop.lc = (Byte)lc; in Lzma2Dec_UpdateState()
156 p->decoder.prop.lp = (Byte)lp; in Lzma2Dec_UpdateState()
163 static void LzmaDec_UpdateWithUncompressed(CLzmaDec *p, const Byte *src, SizeT size) in LzmaDec_UpdateWithUncompressed() argument
165 memcpy(p->dic + p->dicPos, src, size); in LzmaDec_UpdateWithUncompressed()
166 p->dicPos += size; in LzmaDec_UpdateWithUncompressed()
167 if (p->checkDicSize == 0 && p->prop.dicSize - p->processedPos <= size) in LzmaDec_UpdateWithUncompressed()
168 p->checkDicSize = p->prop.dicSize; in LzmaDec_UpdateWithUncompressed()
169 p->processedPos += (UInt32)size; in LzmaDec_UpdateWithUncompressed()
172 void LzmaDec_InitDicAndState(CLzmaDec *p, BoolInt initDic, BoolInt initState);
175 SRes Lzma2Dec_DecodeToDic(CLzma2Dec *p, SizeT dicLimit, in Lzma2Dec_DecodeToDic() argument
182 while (p->state != LZMA2_STATE_ERROR) in Lzma2Dec_DecodeToDic()
186 if (p->state == LZMA2_STATE_FINISHED) in Lzma2Dec_DecodeToDic()
192 dicPos = p->decoder.dicPos; in Lzma2Dec_DecodeToDic()
200 if (p->state != LZMA2_STATE_DATA && p->state != LZMA2_STATE_DATA_CONT) in Lzma2Dec_DecodeToDic()
208 p->state = Lzma2Dec_UpdateState(p, *src++); in Lzma2Dec_DecodeToDic()
209 if (dicPos == dicLimit && p->state != LZMA2_STATE_FINISHED) in Lzma2Dec_DecodeToDic()
215 SizeT inCur = inSize - *srcLen; in Lzma2Dec_DecodeToDic()
216 SizeT outCur = dicLimit - dicPos; in Lzma2Dec_DecodeToDic()
219 if (outCur >= p->unpackSize) in Lzma2Dec_DecodeToDic()
221 outCur = (SizeT)p->unpackSize; in Lzma2Dec_DecodeToDic()
225 if (LZMA2_IS_UNCOMPRESSED_STATE(p)) in Lzma2Dec_DecodeToDic()
233 if (p->state == LZMA2_STATE_DATA) in Lzma2Dec_DecodeToDic()
235 BoolInt initDic = (p->control == LZMA2_CONTROL_COPY_RESET_DIC); in Lzma2Dec_DecodeToDic()
236 LzmaDec_InitDicAndState(&p->decoder, initDic, False); in Lzma2Dec_DecodeToDic()
244 LzmaDec_UpdateWithUncompressed(&p->decoder, src, inCur); in Lzma2Dec_DecodeToDic()
248 p->unpackSize -= (UInt32)inCur; in Lzma2Dec_DecodeToDic()
249 p->state = (p->unpackSize == 0) ? LZMA2_STATE_CONTROL : LZMA2_STATE_DATA_CONT; in Lzma2Dec_DecodeToDic()
255 if (p->state == LZMA2_STATE_DATA) in Lzma2Dec_DecodeToDic()
257 BoolInt initDic = (p->control >= 0xE0); in Lzma2Dec_DecodeToDic()
258 BoolInt initState = (p->control >= 0xA0); in Lzma2Dec_DecodeToDic()
259 LzmaDec_InitDicAndState(&p->decoder, initDic, initState); in Lzma2Dec_DecodeToDic()
260 p->state = LZMA2_STATE_DATA_CONT; in Lzma2Dec_DecodeToDic()
263 if (inCur > p->packSize) in Lzma2Dec_DecodeToDic()
264 inCur = (SizeT)p->packSize; in Lzma2Dec_DecodeToDic()
266 res = LzmaDec_DecodeToDic(&p->decoder, dicPos + outCur, src, &inCur, curFinishMode, status); in Lzma2Dec_DecodeToDic()
270 p->packSize -= (UInt32)inCur; in Lzma2Dec_DecodeToDic()
271 outCur = p->decoder.dicPos - dicPos; in Lzma2Dec_DecodeToDic()
272 p->unpackSize -= (UInt32)outCur; in Lzma2Dec_DecodeToDic()
279 if (p->packSize == 0) in Lzma2Dec_DecodeToDic()
287 || p->unpackSize != 0 in Lzma2Dec_DecodeToDic()
288 || p->packSize != 0) in Lzma2Dec_DecodeToDic()
290 p->state = LZMA2_STATE_CONTROL; in Lzma2Dec_DecodeToDic()
299 p->state = LZMA2_STATE_ERROR; in Lzma2Dec_DecodeToDic()
306 ELzma2ParseStatus Lzma2Dec_Parse(CLzma2Dec *p, in Lzma2Dec_Parse() argument
314 while (p->state != LZMA2_STATE_ERROR) in Lzma2Dec_Parse()
316 if (p->state == LZMA2_STATE_FINISHED) in Lzma2Dec_Parse()
322 if (p->state != LZMA2_STATE_DATA && p->state != LZMA2_STATE_DATA_CONT) in Lzma2Dec_Parse()
328 p->state = Lzma2Dec_UpdateState(p, *src++); in Lzma2Dec_Parse()
330 if (p->state == LZMA2_STATE_UNPACK0) in Lzma2Dec_Parse()
332 // if (p->decoder.dicPos != 0) in Lzma2Dec_Parse()
333 if (p->control == LZMA2_CONTROL_COPY_RESET_DIC || p->control >= 0xE0) in Lzma2Dec_Parse()
342 if (outSize == 0 && p->state != LZMA2_STATE_FINISHED) in Lzma2Dec_Parse()
350 if (p->state == LZMA2_STATE_DATA) in Lzma2Dec_Parse()
360 SizeT inCur = inSize - *srcLen; in Lzma2Dec_Parse()
362 if (LZMA2_IS_UNCOMPRESSED_STATE(p)) in Lzma2Dec_Parse()
366 if (inCur > p->unpackSize) in Lzma2Dec_Parse()
367 inCur = p->unpackSize; in Lzma2Dec_Parse()
370 p->decoder.dicPos += inCur; in Lzma2Dec_Parse()
373 outSize -= inCur; in Lzma2Dec_Parse()
374 p->unpackSize -= (UInt32)inCur; in Lzma2Dec_Parse()
375 p->state = (p->unpackSize == 0) ? LZMA2_STATE_CONTROL : LZMA2_STATE_DATA_CONT; in Lzma2Dec_Parse()
379 p->isExtraMode = True; in Lzma2Dec_Parse()
383 if (p->packSize != 0) in Lzma2Dec_Parse()
386 else if (p->state == LZMA2_STATE_DATA) in Lzma2Dec_Parse()
388 p->state = LZMA2_STATE_DATA_CONT; in Lzma2Dec_Parse()
393 p->packSize--; in Lzma2Dec_Parse()
398 if (inCur > p->packSize) in Lzma2Dec_Parse()
399 inCur = (SizeT)p->packSize; in Lzma2Dec_Parse()
403 p->packSize -= (UInt32)inCur; in Lzma2Dec_Parse()
405 if (p->packSize == 0) in Lzma2Dec_Parse()
408 if (rem > p->unpackSize) in Lzma2Dec_Parse()
409 rem = p->unpackSize; in Lzma2Dec_Parse()
410 p->decoder.dicPos += rem; in Lzma2Dec_Parse()
411 p->unpackSize -= (UInt32)rem; in Lzma2Dec_Parse()
412 outSize -= rem; in Lzma2Dec_Parse()
413 if (p->unpackSize == 0) in Lzma2Dec_Parse()
414 p->state = LZMA2_STATE_CONTROL; in Lzma2Dec_Parse()
420 p->state = LZMA2_STATE_ERROR; in Lzma2Dec_Parse()
427 SRes Lzma2Dec_DecodeToBuf(CLzma2Dec *p, Byte *dest, SizeT *destLen, const Byte *src, SizeT *srcLen,… in Lzma2Dec_DecodeToBuf() argument
438 if (p->decoder.dicPos == p->decoder.dicBufSize) in Lzma2Dec_DecodeToBuf()
439 p->decoder.dicPos = 0; in Lzma2Dec_DecodeToBuf()
440 dicPos = p->decoder.dicPos; in Lzma2Dec_DecodeToBuf()
442 outCur = p->decoder.dicBufSize - dicPos; in Lzma2Dec_DecodeToBuf()
450 res = Lzma2Dec_DecodeToDic(p, dicPos + outCur, src, &inCur, curFinishMode, status); in Lzma2Dec_DecodeToBuf()
453 inSize -= inCur; in Lzma2Dec_DecodeToBuf()
455 outCur = p->decoder.dicPos - dicPos; in Lzma2Dec_DecodeToBuf()
456 memcpy(dest, p->decoder.dic + dicPos, outCur); in Lzma2Dec_DecodeToBuf()
458 outSize -= outCur; in Lzma2Dec_DecodeToBuf()
471 CLzma2Dec p; in Lzma2Decode() local
476 Lzma2Dec_Construct(&p); in Lzma2Decode()
477 RINOK(Lzma2Dec_AllocateProbs(&p, prop, alloc)); in Lzma2Decode()
478 p.decoder.dic = dest; in Lzma2Decode()
479 p.decoder.dicBufSize = outSize; in Lzma2Decode()
480 Lzma2Dec_Init(&p); in Lzma2Decode()
482 res = Lzma2Dec_DecodeToDic(&p, outSize, src, srcLen, finishMode, status); in Lzma2Decode()
483 *destLen = p.decoder.dicPos; in Lzma2Decode()
486 Lzma2Dec_FreeProbs(&p, alloc); in Lzma2Decode()