2016-03-01 15:47:10 +00:00
|
|
|
/* Lzma2Dec.c -- LZMA2 Decoder
|
2019-03-26 15:44:26 +00:00
|
|
|
2019-02-02 : Igor Pavlov : Public domain */
|
2016-03-01 15:47:10 +00:00
|
|
|
|
|
|
|
/* #define SHOW_DEBUG_INFO */
|
|
|
|
|
|
|
|
#include "Precomp.h"
|
|
|
|
|
|
|
|
#ifdef SHOW_DEBUG_INFO
|
|
|
|
#include <stdio.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
#include "Lzma2Dec.h"
|
|
|
|
|
|
|
|
/*
|
2018-05-04 08:24:15 +00:00
|
|
|
00000000 - End of data
|
|
|
|
00000001 U U - Uncompressed, reset dic, need reset state and set new prop
|
|
|
|
00000010 U U - Uncompressed, no reset
|
|
|
|
100uuuuu U U P P - LZMA, no reset
|
|
|
|
101uuuuu U U P P - LZMA, reset state
|
|
|
|
110uuuuu U U P P S - LZMA, reset state + set new prop
|
|
|
|
111uuuuu U U P P S - LZMA, reset state + set new prop, reset dic
|
2016-03-01 15:47:10 +00:00
|
|
|
|
|
|
|
u, U - Unpack Size
|
|
|
|
P - Pack Size
|
|
|
|
S - Props
|
|
|
|
*/
|
|
|
|
|
|
|
|
#define LZMA2_CONTROL_COPY_RESET_DIC 1
|
|
|
|
|
2018-05-04 08:24:15 +00:00
|
|
|
#define LZMA2_IS_UNCOMPRESSED_STATE(p) (((p)->control & (1 << 7)) == 0)
|
2016-03-01 15:47:10 +00:00
|
|
|
|
|
|
|
#define LZMA2_LCLP_MAX 4
|
|
|
|
#define LZMA2_DIC_SIZE_FROM_PROP(p) (((UInt32)2 | ((p) & 1)) << ((p) / 2 + 11))
|
|
|
|
|
|
|
|
#ifdef SHOW_DEBUG_INFO
|
|
|
|
#define PRF(x) x
|
|
|
|
#else
|
|
|
|
#define PRF(x)
|
|
|
|
#endif
|
|
|
|
|
|
|
|
typedef enum
|
|
|
|
{
|
|
|
|
LZMA2_STATE_CONTROL,
|
|
|
|
LZMA2_STATE_UNPACK0,
|
|
|
|
LZMA2_STATE_UNPACK1,
|
|
|
|
LZMA2_STATE_PACK0,
|
|
|
|
LZMA2_STATE_PACK1,
|
|
|
|
LZMA2_STATE_PROP,
|
|
|
|
LZMA2_STATE_DATA,
|
|
|
|
LZMA2_STATE_DATA_CONT,
|
|
|
|
LZMA2_STATE_FINISHED,
|
|
|
|
LZMA2_STATE_ERROR
|
|
|
|
} ELzma2State;
|
|
|
|
|
|
|
|
static SRes Lzma2Dec_GetOldProps(Byte prop, Byte *props)
|
|
|
|
{
|
|
|
|
UInt32 dicSize;
|
|
|
|
if (prop > 40)
|
|
|
|
return SZ_ERROR_UNSUPPORTED;
|
|
|
|
dicSize = (prop == 40) ? 0xFFFFFFFF : LZMA2_DIC_SIZE_FROM_PROP(prop);
|
|
|
|
props[0] = (Byte)LZMA2_LCLP_MAX;
|
|
|
|
props[1] = (Byte)(dicSize);
|
|
|
|
props[2] = (Byte)(dicSize >> 8);
|
|
|
|
props[3] = (Byte)(dicSize >> 16);
|
|
|
|
props[4] = (Byte)(dicSize >> 24);
|
|
|
|
return SZ_OK;
|
|
|
|
}
|
|
|
|
|
2018-01-08 12:00:01 +00:00
|
|
|
SRes Lzma2Dec_AllocateProbs(CLzma2Dec *p, Byte prop, ISzAllocPtr alloc)
|
2016-03-01 15:47:10 +00:00
|
|
|
{
|
|
|
|
Byte props[LZMA_PROPS_SIZE];
|
|
|
|
RINOK(Lzma2Dec_GetOldProps(prop, props));
|
|
|
|
return LzmaDec_AllocateProbs(&p->decoder, props, LZMA_PROPS_SIZE, alloc);
|
|
|
|
}
|
|
|
|
|
2018-01-08 12:00:01 +00:00
|
|
|
SRes Lzma2Dec_Allocate(CLzma2Dec *p, Byte prop, ISzAllocPtr alloc)
|
2016-03-01 15:47:10 +00:00
|
|
|
{
|
|
|
|
Byte props[LZMA_PROPS_SIZE];
|
|
|
|
RINOK(Lzma2Dec_GetOldProps(prop, props));
|
|
|
|
return LzmaDec_Allocate(&p->decoder, props, LZMA_PROPS_SIZE, alloc);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Lzma2Dec_Init(CLzma2Dec *p)
|
|
|
|
{
|
|
|
|
p->state = LZMA2_STATE_CONTROL;
|
2018-05-04 08:24:15 +00:00
|
|
|
p->needInitLevel = 0xE0;
|
|
|
|
p->isExtraMode = False;
|
|
|
|
p->unpackSize = 0;
|
|
|
|
|
|
|
|
// p->decoder.dicPos = 0; // we can use it instead of full init
|
2016-03-01 15:47:10 +00:00
|
|
|
LzmaDec_Init(&p->decoder);
|
|
|
|
}
|
|
|
|
|
|
|
|
static ELzma2State Lzma2Dec_UpdateState(CLzma2Dec *p, Byte b)
|
|
|
|
{
|
|
|
|
switch (p->state)
|
|
|
|
{
|
|
|
|
case LZMA2_STATE_CONTROL:
|
2018-05-04 08:24:15 +00:00
|
|
|
p->isExtraMode = False;
|
2016-03-01 15:47:10 +00:00
|
|
|
p->control = b;
|
2018-05-04 08:24:15 +00:00
|
|
|
PRF(printf("\n %8X", (unsigned)p->decoder.dicPos));
|
|
|
|
PRF(printf(" %02X", (unsigned)b));
|
2018-01-08 12:00:01 +00:00
|
|
|
if (b == 0)
|
2016-03-01 15:47:10 +00:00
|
|
|
return LZMA2_STATE_FINISHED;
|
|
|
|
if (LZMA2_IS_UNCOMPRESSED_STATE(p))
|
|
|
|
{
|
2018-05-04 08:24:15 +00:00
|
|
|
if (b == LZMA2_CONTROL_COPY_RESET_DIC)
|
|
|
|
p->needInitLevel = 0xC0;
|
|
|
|
else if (b > 2 || p->needInitLevel == 0xE0)
|
2016-03-01 15:47:10 +00:00
|
|
|
return LZMA2_STATE_ERROR;
|
|
|
|
}
|
|
|
|
else
|
2018-05-04 08:24:15 +00:00
|
|
|
{
|
|
|
|
if (b < p->needInitLevel)
|
|
|
|
return LZMA2_STATE_ERROR;
|
|
|
|
p->needInitLevel = 0;
|
2018-01-08 12:00:01 +00:00
|
|
|
p->unpackSize = (UInt32)(b & 0x1F) << 16;
|
2018-05-04 08:24:15 +00:00
|
|
|
}
|
2016-03-01 15:47:10 +00:00
|
|
|
return LZMA2_STATE_UNPACK0;
|
|
|
|
|
|
|
|
case LZMA2_STATE_UNPACK0:
|
|
|
|
p->unpackSize |= (UInt32)b << 8;
|
|
|
|
return LZMA2_STATE_UNPACK1;
|
|
|
|
|
|
|
|
case LZMA2_STATE_UNPACK1:
|
|
|
|
p->unpackSize |= (UInt32)b;
|
|
|
|
p->unpackSize++;
|
2018-05-04 08:24:15 +00:00
|
|
|
PRF(printf(" %7u", (unsigned)p->unpackSize));
|
|
|
|
return LZMA2_IS_UNCOMPRESSED_STATE(p) ? LZMA2_STATE_DATA : LZMA2_STATE_PACK0;
|
2016-03-01 15:47:10 +00:00
|
|
|
|
|
|
|
case LZMA2_STATE_PACK0:
|
|
|
|
p->packSize = (UInt32)b << 8;
|
|
|
|
return LZMA2_STATE_PACK1;
|
|
|
|
|
|
|
|
case LZMA2_STATE_PACK1:
|
|
|
|
p->packSize |= (UInt32)b;
|
|
|
|
p->packSize++;
|
2018-05-04 08:24:15 +00:00
|
|
|
// if (p->packSize < 5) return LZMA2_STATE_ERROR;
|
|
|
|
PRF(printf(" %5u", (unsigned)p->packSize));
|
|
|
|
return (p->control & 0x40) ? LZMA2_STATE_PROP : LZMA2_STATE_DATA;
|
2016-03-01 15:47:10 +00:00
|
|
|
|
|
|
|
case LZMA2_STATE_PROP:
|
|
|
|
{
|
|
|
|
unsigned lc, lp;
|
|
|
|
if (b >= (9 * 5 * 5))
|
|
|
|
return LZMA2_STATE_ERROR;
|
|
|
|
lc = b % 9;
|
|
|
|
b /= 9;
|
2018-05-04 08:24:15 +00:00
|
|
|
p->decoder.prop.pb = (Byte)(b / 5);
|
2016-03-01 15:47:10 +00:00
|
|
|
lp = b % 5;
|
|
|
|
if (lc + lp > LZMA2_LCLP_MAX)
|
|
|
|
return LZMA2_STATE_ERROR;
|
2018-05-04 08:24:15 +00:00
|
|
|
p->decoder.prop.lc = (Byte)lc;
|
|
|
|
p->decoder.prop.lp = (Byte)lp;
|
2016-03-01 15:47:10 +00:00
|
|
|
return LZMA2_STATE_DATA;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return LZMA2_STATE_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void LzmaDec_UpdateWithUncompressed(CLzmaDec *p, const Byte *src, SizeT size)
|
|
|
|
{
|
|
|
|
memcpy(p->dic + p->dicPos, src, size);
|
|
|
|
p->dicPos += size;
|
|
|
|
if (p->checkDicSize == 0 && p->prop.dicSize - p->processedPos <= size)
|
|
|
|
p->checkDicSize = p->prop.dicSize;
|
|
|
|
p->processedPos += (UInt32)size;
|
|
|
|
}
|
|
|
|
|
2019-01-22 09:37:23 +00:00
|
|
|
void LzmaDec_InitDicAndState(CLzmaDec *p, BoolInt initDic, BoolInt initState);
|
2016-03-01 15:47:10 +00:00
|
|
|
|
2018-01-08 12:00:01 +00:00
|
|
|
|
2016-03-01 15:47:10 +00:00
|
|
|
SRes Lzma2Dec_DecodeToDic(CLzma2Dec *p, SizeT dicLimit,
|
|
|
|
const Byte *src, SizeT *srcLen, ELzmaFinishMode finishMode, ELzmaStatus *status)
|
|
|
|
{
|
|
|
|
SizeT inSize = *srcLen;
|
|
|
|
*srcLen = 0;
|
|
|
|
*status = LZMA_STATUS_NOT_SPECIFIED;
|
|
|
|
|
2018-01-08 12:00:01 +00:00
|
|
|
while (p->state != LZMA2_STATE_ERROR)
|
2016-03-01 15:47:10 +00:00
|
|
|
{
|
2018-01-08 12:00:01 +00:00
|
|
|
SizeT dicPos;
|
|
|
|
|
|
|
|
if (p->state == LZMA2_STATE_FINISHED)
|
|
|
|
{
|
|
|
|
*status = LZMA_STATUS_FINISHED_WITH_MARK;
|
|
|
|
return SZ_OK;
|
|
|
|
}
|
2016-03-01 15:47:10 +00:00
|
|
|
|
2018-01-08 12:00:01 +00:00
|
|
|
dicPos = p->decoder.dicPos;
|
2016-03-01 15:47:10 +00:00
|
|
|
|
|
|
|
if (dicPos == dicLimit && finishMode == LZMA_FINISH_ANY)
|
|
|
|
{
|
|
|
|
*status = LZMA_STATUS_NOT_FINISHED;
|
|
|
|
return SZ_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (p->state != LZMA2_STATE_DATA && p->state != LZMA2_STATE_DATA_CONT)
|
|
|
|
{
|
|
|
|
if (*srcLen == inSize)
|
|
|
|
{
|
|
|
|
*status = LZMA_STATUS_NEEDS_MORE_INPUT;
|
|
|
|
return SZ_OK;
|
|
|
|
}
|
|
|
|
(*srcLen)++;
|
|
|
|
p->state = Lzma2Dec_UpdateState(p, *src++);
|
|
|
|
if (dicPos == dicLimit && p->state != LZMA2_STATE_FINISHED)
|
2018-01-08 12:00:01 +00:00
|
|
|
break;
|
2016-03-01 15:47:10 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
2018-01-08 12:00:01 +00:00
|
|
|
SizeT inCur = inSize - *srcLen;
|
|
|
|
SizeT outCur = dicLimit - dicPos;
|
2016-03-01 15:47:10 +00:00
|
|
|
ELzmaFinishMode curFinishMode = LZMA_FINISH_ANY;
|
|
|
|
|
2018-01-08 12:00:01 +00:00
|
|
|
if (outCur >= p->unpackSize)
|
2016-03-01 15:47:10 +00:00
|
|
|
{
|
2018-01-08 12:00:01 +00:00
|
|
|
outCur = (SizeT)p->unpackSize;
|
2016-03-01 15:47:10 +00:00
|
|
|
curFinishMode = LZMA_FINISH_END;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (LZMA2_IS_UNCOMPRESSED_STATE(p))
|
|
|
|
{
|
2018-01-08 12:00:01 +00:00
|
|
|
if (inCur == 0)
|
2016-03-01 15:47:10 +00:00
|
|
|
{
|
|
|
|
*status = LZMA_STATUS_NEEDS_MORE_INPUT;
|
|
|
|
return SZ_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (p->state == LZMA2_STATE_DATA)
|
|
|
|
{
|
2019-01-22 09:37:23 +00:00
|
|
|
BoolInt initDic = (p->control == LZMA2_CONTROL_COPY_RESET_DIC);
|
2016-03-01 15:47:10 +00:00
|
|
|
LzmaDec_InitDicAndState(&p->decoder, initDic, False);
|
|
|
|
}
|
|
|
|
|
2018-01-08 12:00:01 +00:00
|
|
|
if (inCur > outCur)
|
|
|
|
inCur = outCur;
|
|
|
|
if (inCur == 0)
|
|
|
|
break;
|
2016-03-01 15:47:10 +00:00
|
|
|
|
2018-01-08 12:00:01 +00:00
|
|
|
LzmaDec_UpdateWithUncompressed(&p->decoder, src, inCur);
|
2016-03-01 15:47:10 +00:00
|
|
|
|
2018-01-08 12:00:01 +00:00
|
|
|
src += inCur;
|
|
|
|
*srcLen += inCur;
|
|
|
|
p->unpackSize -= (UInt32)inCur;
|
2016-03-01 15:47:10 +00:00
|
|
|
p->state = (p->unpackSize == 0) ? LZMA2_STATE_CONTROL : LZMA2_STATE_DATA_CONT;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
SRes res;
|
|
|
|
|
|
|
|
if (p->state == LZMA2_STATE_DATA)
|
|
|
|
{
|
2019-01-22 09:37:23 +00:00
|
|
|
BoolInt initDic = (p->control >= 0xE0);
|
|
|
|
BoolInt initState = (p->control >= 0xA0);
|
2016-03-01 15:47:10 +00:00
|
|
|
LzmaDec_InitDicAndState(&p->decoder, initDic, initState);
|
|
|
|
p->state = LZMA2_STATE_DATA_CONT;
|
|
|
|
}
|
|
|
|
|
2018-01-08 12:00:01 +00:00
|
|
|
if (inCur > p->packSize)
|
|
|
|
inCur = (SizeT)p->packSize;
|
|
|
|
|
2018-05-04 08:24:15 +00:00
|
|
|
res = LzmaDec_DecodeToDic(&p->decoder, dicPos + outCur, src, &inCur, curFinishMode, status);
|
|
|
|
|
2018-01-08 12:00:01 +00:00
|
|
|
src += inCur;
|
|
|
|
*srcLen += inCur;
|
|
|
|
p->packSize -= (UInt32)inCur;
|
|
|
|
outCur = p->decoder.dicPos - dicPos;
|
|
|
|
p->unpackSize -= (UInt32)outCur;
|
|
|
|
|
|
|
|
if (res != 0)
|
|
|
|
break;
|
2016-03-01 15:47:10 +00:00
|
|
|
|
|
|
|
if (*status == LZMA_STATUS_NEEDS_MORE_INPUT)
|
2018-01-08 12:00:01 +00:00
|
|
|
{
|
|
|
|
if (p->packSize == 0)
|
|
|
|
break;
|
|
|
|
return SZ_OK;
|
|
|
|
}
|
2016-03-01 15:47:10 +00:00
|
|
|
|
2018-01-08 12:00:01 +00:00
|
|
|
if (inCur == 0 && outCur == 0)
|
2016-03-01 15:47:10 +00:00
|
|
|
{
|
|
|
|
if (*status != LZMA_STATUS_MAYBE_FINISHED_WITHOUT_MARK
|
|
|
|
|| p->unpackSize != 0
|
|
|
|
|| p->packSize != 0)
|
2018-01-08 12:00:01 +00:00
|
|
|
break;
|
2016-03-01 15:47:10 +00:00
|
|
|
p->state = LZMA2_STATE_CONTROL;
|
|
|
|
}
|
|
|
|
|
2018-01-08 12:00:01 +00:00
|
|
|
*status = LZMA_STATUS_NOT_SPECIFIED;
|
2016-03-01 15:47:10 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-08 12:00:01 +00:00
|
|
|
*status = LZMA_STATUS_NOT_SPECIFIED;
|
|
|
|
p->state = LZMA2_STATE_ERROR;
|
|
|
|
return SZ_ERROR_DATA;
|
2016-03-01 15:47:10 +00:00
|
|
|
}
|
|
|
|
|
2018-01-08 12:00:01 +00:00
|
|
|
|
2018-05-04 08:24:15 +00:00
|
|
|
|
|
|
|
|
|
|
|
ELzma2ParseStatus Lzma2Dec_Parse(CLzma2Dec *p,
|
|
|
|
SizeT outSize,
|
|
|
|
const Byte *src, SizeT *srcLen,
|
|
|
|
int checkFinishBlock)
|
|
|
|
{
|
|
|
|
SizeT inSize = *srcLen;
|
|
|
|
*srcLen = 0;
|
|
|
|
|
|
|
|
while (p->state != LZMA2_STATE_ERROR)
|
|
|
|
{
|
|
|
|
if (p->state == LZMA2_STATE_FINISHED)
|
2019-03-26 15:44:26 +00:00
|
|
|
return (ELzma2ParseStatus)LZMA_STATUS_FINISHED_WITH_MARK;
|
2018-05-04 08:24:15 +00:00
|
|
|
|
|
|
|
if (outSize == 0 && !checkFinishBlock)
|
2019-03-26 15:44:26 +00:00
|
|
|
return (ELzma2ParseStatus)LZMA_STATUS_NOT_FINISHED;
|
2018-05-04 08:24:15 +00:00
|
|
|
|
|
|
|
if (p->state != LZMA2_STATE_DATA && p->state != LZMA2_STATE_DATA_CONT)
|
|
|
|
{
|
|
|
|
if (*srcLen == inSize)
|
2019-03-26 15:44:26 +00:00
|
|
|
return (ELzma2ParseStatus)LZMA_STATUS_NEEDS_MORE_INPUT;
|
2018-05-04 08:24:15 +00:00
|
|
|
(*srcLen)++;
|
|
|
|
|
|
|
|
p->state = Lzma2Dec_UpdateState(p, *src++);
|
|
|
|
|
|
|
|
if (p->state == LZMA2_STATE_UNPACK0)
|
|
|
|
{
|
|
|
|
// if (p->decoder.dicPos != 0)
|
|
|
|
if (p->control == LZMA2_CONTROL_COPY_RESET_DIC || p->control >= 0xE0)
|
|
|
|
return LZMA2_PARSE_STATUS_NEW_BLOCK;
|
|
|
|
// if (outSize == 0) return LZMA_STATUS_NOT_FINISHED;
|
|
|
|
}
|
|
|
|
|
|
|
|
// The following code can be commented.
|
|
|
|
// It's not big problem, if we read additional input bytes.
|
|
|
|
// It will be stopped later in LZMA2_STATE_DATA / LZMA2_STATE_DATA_CONT state.
|
|
|
|
|
|
|
|
if (outSize == 0 && p->state != LZMA2_STATE_FINISHED)
|
|
|
|
{
|
|
|
|
// checkFinishBlock is true. So we expect that block must be finished,
|
|
|
|
// We can return LZMA_STATUS_NOT_SPECIFIED or LZMA_STATUS_NOT_FINISHED here
|
|
|
|
// break;
|
2019-03-26 15:44:26 +00:00
|
|
|
return (ELzma2ParseStatus)LZMA_STATUS_NOT_FINISHED;
|
2018-05-04 08:24:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (p->state == LZMA2_STATE_DATA)
|
|
|
|
return LZMA2_PARSE_STATUS_NEW_CHUNK;
|
|
|
|
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (outSize == 0)
|
2019-03-26 15:44:26 +00:00
|
|
|
return (ELzma2ParseStatus)LZMA_STATUS_NOT_FINISHED;
|
2018-05-04 08:24:15 +00:00
|
|
|
|
|
|
|
{
|
|
|
|
SizeT inCur = inSize - *srcLen;
|
|
|
|
|
|
|
|
if (LZMA2_IS_UNCOMPRESSED_STATE(p))
|
|
|
|
{
|
|
|
|
if (inCur == 0)
|
2019-03-26 15:44:26 +00:00
|
|
|
return (ELzma2ParseStatus)LZMA_STATUS_NEEDS_MORE_INPUT;
|
2018-05-04 08:24:15 +00:00
|
|
|
if (inCur > p->unpackSize)
|
|
|
|
inCur = p->unpackSize;
|
|
|
|
if (inCur > outSize)
|
|
|
|
inCur = outSize;
|
|
|
|
p->decoder.dicPos += inCur;
|
|
|
|
src += inCur;
|
|
|
|
*srcLen += inCur;
|
|
|
|
outSize -= inCur;
|
|
|
|
p->unpackSize -= (UInt32)inCur;
|
|
|
|
p->state = (p->unpackSize == 0) ? LZMA2_STATE_CONTROL : LZMA2_STATE_DATA_CONT;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
p->isExtraMode = True;
|
|
|
|
|
|
|
|
if (inCur == 0)
|
|
|
|
{
|
|
|
|
if (p->packSize != 0)
|
2019-03-26 15:44:26 +00:00
|
|
|
return (ELzma2ParseStatus)LZMA_STATUS_NEEDS_MORE_INPUT;
|
2018-05-04 08:24:15 +00:00
|
|
|
}
|
|
|
|
else if (p->state == LZMA2_STATE_DATA)
|
|
|
|
{
|
|
|
|
p->state = LZMA2_STATE_DATA_CONT;
|
|
|
|
if (*src != 0)
|
|
|
|
{
|
|
|
|
// first byte of lzma chunk must be Zero
|
|
|
|
*srcLen += 1;
|
|
|
|
p->packSize--;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (inCur > p->packSize)
|
|
|
|
inCur = (SizeT)p->packSize;
|
|
|
|
|
|
|
|
src += inCur;
|
|
|
|
*srcLen += inCur;
|
|
|
|
p->packSize -= (UInt32)inCur;
|
|
|
|
|
|
|
|
if (p->packSize == 0)
|
|
|
|
{
|
|
|
|
SizeT rem = outSize;
|
|
|
|
if (rem > p->unpackSize)
|
|
|
|
rem = p->unpackSize;
|
|
|
|
p->decoder.dicPos += rem;
|
|
|
|
p->unpackSize -= (UInt32)rem;
|
|
|
|
outSize -= rem;
|
|
|
|
if (p->unpackSize == 0)
|
|
|
|
p->state = LZMA2_STATE_CONTROL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
p->state = LZMA2_STATE_ERROR;
|
2019-03-26 15:44:26 +00:00
|
|
|
return (ELzma2ParseStatus)LZMA_STATUS_NOT_SPECIFIED;
|
2018-05-04 08:24:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2016-03-01 15:47:10 +00:00
|
|
|
SRes Lzma2Dec_DecodeToBuf(CLzma2Dec *p, Byte *dest, SizeT *destLen, const Byte *src, SizeT *srcLen, ELzmaFinishMode finishMode, ELzmaStatus *status)
|
|
|
|
{
|
|
|
|
SizeT outSize = *destLen, inSize = *srcLen;
|
|
|
|
*srcLen = *destLen = 0;
|
2018-01-08 12:00:01 +00:00
|
|
|
|
2016-03-01 15:47:10 +00:00
|
|
|
for (;;)
|
|
|
|
{
|
2018-01-08 12:00:01 +00:00
|
|
|
SizeT inCur = inSize, outCur, dicPos;
|
2016-03-01 15:47:10 +00:00
|
|
|
ELzmaFinishMode curFinishMode;
|
|
|
|
SRes res;
|
2018-01-08 12:00:01 +00:00
|
|
|
|
2016-03-01 15:47:10 +00:00
|
|
|
if (p->decoder.dicPos == p->decoder.dicBufSize)
|
|
|
|
p->decoder.dicPos = 0;
|
|
|
|
dicPos = p->decoder.dicPos;
|
2018-01-08 12:00:01 +00:00
|
|
|
curFinishMode = LZMA_FINISH_ANY;
|
|
|
|
outCur = p->decoder.dicBufSize - dicPos;
|
|
|
|
|
|
|
|
if (outCur >= outSize)
|
2016-03-01 15:47:10 +00:00
|
|
|
{
|
2018-01-08 12:00:01 +00:00
|
|
|
outCur = outSize;
|
2016-03-01 15:47:10 +00:00
|
|
|
curFinishMode = finishMode;
|
|
|
|
}
|
|
|
|
|
2018-01-08 12:00:01 +00:00
|
|
|
res = Lzma2Dec_DecodeToDic(p, dicPos + outCur, src, &inCur, curFinishMode, status);
|
|
|
|
|
|
|
|
src += inCur;
|
|
|
|
inSize -= inCur;
|
|
|
|
*srcLen += inCur;
|
|
|
|
outCur = p->decoder.dicPos - dicPos;
|
|
|
|
memcpy(dest, p->decoder.dic + dicPos, outCur);
|
|
|
|
dest += outCur;
|
|
|
|
outSize -= outCur;
|
|
|
|
*destLen += outCur;
|
2016-03-01 15:47:10 +00:00
|
|
|
if (res != 0)
|
|
|
|
return res;
|
2018-01-08 12:00:01 +00:00
|
|
|
if (outCur == 0 || outSize == 0)
|
2016-03-01 15:47:10 +00:00
|
|
|
return SZ_OK;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-08 12:00:01 +00:00
|
|
|
|
2016-03-01 15:47:10 +00:00
|
|
|
SRes Lzma2Decode(Byte *dest, SizeT *destLen, const Byte *src, SizeT *srcLen,
|
2018-01-08 12:00:01 +00:00
|
|
|
Byte prop, ELzmaFinishMode finishMode, ELzmaStatus *status, ISzAllocPtr alloc)
|
2016-03-01 15:47:10 +00:00
|
|
|
{
|
|
|
|
CLzma2Dec p;
|
|
|
|
SRes res;
|
|
|
|
SizeT outSize = *destLen, inSize = *srcLen;
|
|
|
|
*destLen = *srcLen = 0;
|
|
|
|
*status = LZMA_STATUS_NOT_SPECIFIED;
|
|
|
|
Lzma2Dec_Construct(&p);
|
|
|
|
RINOK(Lzma2Dec_AllocateProbs(&p, prop, alloc));
|
|
|
|
p.decoder.dic = dest;
|
|
|
|
p.decoder.dicBufSize = outSize;
|
|
|
|
Lzma2Dec_Init(&p);
|
|
|
|
*srcLen = inSize;
|
|
|
|
res = Lzma2Dec_DecodeToDic(&p, outSize, src, srcLen, finishMode, status);
|
|
|
|
*destLen = p.decoder.dicPos;
|
|
|
|
if (res == SZ_OK && *status == LZMA_STATUS_NEEDS_MORE_INPUT)
|
|
|
|
res = SZ_ERROR_INPUT_EOF;
|
|
|
|
Lzma2Dec_FreeProbs(&p, alloc);
|
|
|
|
return res;
|
|
|
|
}
|