(deps/7zip) Cleanups

This commit is contained in:
twinaphex 2020-07-01 16:50:54 +02:00
parent 14e20cea51
commit 867daa7efb
6 changed files with 1510 additions and 1106 deletions

215
deps/7zip/7zDec.c vendored
View File

@ -23,14 +23,20 @@
#define k_SPARC 0x03030805
#define k_BCJ2 0x0303011B
static SRes SzDecodeLzma(CSzCoderInfo *coder, uint64_t inSize, ILookInStream *inStream,
static SRes SzDecodeLzma(CSzCoderInfo *coder,
uint64_t inSize, ILookInStream *inStream,
uint8_t *outBuffer, size_t outSize, ISzAlloc *allocMain)
{
int result;
CLzmaDec state;
SRes res = SZ_OK;
LzmaDec_Construct(&state);
RINOK(LzmaDec_AllocateProbs(&state, coder->Props.data, (unsigned)coder->Props.size, allocMain));
result = LzmaDec_AllocateProbs(
&state, coder->Props.data,
(unsigned)coder->Props.size, allocMain);
if (result != 0)
return result;
state.dic = outBuffer;
state.dicBufSize = outSize;
LzmaDec_Init(&state);
@ -41,26 +47,36 @@ static SRes SzDecodeLzma(CSzCoderInfo *coder, uint64_t inSize, ILookInStream *in
size_t lookahead = (1 << 18);
if (lookahead > inSize)
lookahead = (size_t)inSize;
res = inStream->Look((void *)inStream, (const void **)&inBuf, &lookahead);
res = inStream->Look(
(void *)inStream, (const void **)&inBuf, &lookahead);
if (res != SZ_OK)
break;
{
size_t inProcessed = (size_t)lookahead, dicPos = state.dicPos;
ELzmaStatus status;
res = LzmaDec_DecodeToDic(&state, outSize, inBuf, &inProcessed, LZMA_FINISH_END, &status);
size_t inProcessed = (size_t)lookahead, dicPos = state.dicPos;
res = LzmaDec_DecodeToDic(
&state, outSize, inBuf, &inProcessed, LZMA_FINISH_END, &status);
lookahead -= inProcessed;
inSize -= inProcessed;
if (res != SZ_OK)
break;
if (state.dicPos == state.dicBufSize || (inProcessed == 0 && dicPos == state.dicPos))
if (
state.dicPos == state.dicBufSize ||
(inProcessed == 0 && dicPos == state.dicPos))
{
if (state.dicBufSize != outSize || lookahead != 0 ||
if (
state.dicBufSize != outSize ||
lookahead != 0 ||
(status != LZMA_STATUS_FINISHED_WITH_MARK &&
status != LZMA_STATUS_MAYBE_FINISHED_WITHOUT_MARK))
res = SZ_ERROR_DATA;
break;
}
res = inStream->Skip((void *)inStream, inProcessed);
if (res != SZ_OK)
break;
@ -71,16 +87,22 @@ static SRes SzDecodeLzma(CSzCoderInfo *coder, uint64_t inSize, ILookInStream *in
return res;
}
static SRes SzDecodeLzma2(CSzCoderInfo *coder, uint64_t inSize, ILookInStream *inStream,
static SRes SzDecodeLzma2(CSzCoderInfo *coder,
uint64_t inSize, ILookInStream *inStream,
uint8_t *outBuffer, size_t outSize, ISzAlloc *allocMain)
{
int result;
CLzma2Dec state;
SRes res = SZ_OK;
Lzma2Dec_Construct(&state);
if (coder->Props.size != 1)
return SZ_ERROR_DATA;
RINOK(Lzma2Dec_AllocateProbs(&state, coder->Props.data[0], allocMain));
result = Lzma2Dec_AllocateProbs(&state, coder->Props.data[0], allocMain);
if (result != 0)
return result;
state.decoder.dic = outBuffer;
state.decoder.dicBufSize = outSize;
Lzma2Dec_Init(&state);
@ -91,19 +113,24 @@ static SRes SzDecodeLzma2(CSzCoderInfo *coder, uint64_t inSize, ILookInStream *i
size_t lookahead = (1 << 18);
if (lookahead > inSize)
lookahead = (size_t)inSize;
res = inStream->Look((void *)inStream, (const void **)&inBuf, &lookahead);
res = inStream->Look(
(void *)inStream, (const void **)&inBuf, &lookahead);
if (res != SZ_OK)
break;
{
size_t inProcessed = (size_t)lookahead, dicPos = state.decoder.dicPos;
ELzmaStatus status;
res = Lzma2Dec_DecodeToDic(&state, outSize, inBuf, &inProcessed, LZMA_FINISH_END, &status);
size_t inProcessed = (size_t)lookahead, dicPos = state.decoder.dicPos;
res = Lzma2Dec_DecodeToDic(
&state, outSize, inBuf, &inProcessed,
LZMA_FINISH_END, &status);
lookahead -= inProcessed;
inSize -= inProcessed;
if (res != SZ_OK)
break;
if (state.decoder.dicPos == state.decoder.dicBufSize || (inProcessed == 0 && dicPos == state.decoder.dicPos))
if (
state.decoder.dicPos == state.decoder.dicBufSize ||
(inProcessed == 0 && dicPos == state.decoder.dicPos))
{
if (state.decoder.dicBufSize != outSize || lookahead != 0 ||
(status != LZMA_STATUS_FINISHED_WITH_MARK))
@ -120,26 +147,36 @@ static SRes SzDecodeLzma2(CSzCoderInfo *coder, uint64_t inSize, ILookInStream *i
return res;
}
static SRes SzDecodeCopy(uint64_t inSize, ILookInStream *inStream, uint8_t *outBuffer)
static SRes SzDecodeCopy(uint64_t inSize,
ILookInStream *inStream, uint8_t *outBuffer)
{
while (inSize > 0)
{
void *inBuf;
int result;
void *inBuf = NULL;
size_t curSize = (1 << 18);
if (curSize > inSize)
curSize = (size_t)inSize;
RINOK(inStream->Look((void *)inStream, (const void **)&inBuf, &curSize));
result = inStream->Look(
(void *)inStream, (const void **)&inBuf, &curSize);
if (result != 0)
return result;
if (curSize == 0)
return SZ_ERROR_INPUT_EOF;
memcpy(outBuffer, inBuf, curSize);
outBuffer += curSize;
inSize -= curSize;
RINOK(inStream->Skip((void *)inStream, curSize));
result = inStream->Skip((void *)inStream, curSize);
if (result != 0)
return result;
}
return SZ_OK;
}
static bool IS_MAIN_METHOD(uint32_t m)
static bool is_main_method(uint32_t m)
{
switch(m)
{
@ -147,36 +184,43 @@ static bool IS_MAIN_METHOD(uint32_t m)
case k_LZMA:
case k_LZMA2:
return true;
default:
break;
}
return false;
}
static bool IS_SUPPORTED_CODER(const CSzCoderInfo *c)
static bool is_supported_coder(const CSzCoderInfo *c)
{
return
c->NumInStreams == 1 &&
c->NumOutStreams == 1 &&
c->MethodID <= (uint32_t)0xFFFFFFFF &&
IS_MAIN_METHOD((uint32_t)c->MethodID);
is_main_method((uint32_t)c->MethodID);
}
#define IS_BCJ2(c) ((c)->MethodID == k_BCJ2 && (c)->NumInStreams == 4 && (c)->NumOutStreams == 1)
static SRes CheckSupportedFolder(const CSzFolder *f)
static SRes check_supported_folder(const CSzFolder *f)
{
if (f->NumCoders < 1 || f->NumCoders > 4)
return SZ_ERROR_UNSUPPORTED;
if (!IS_SUPPORTED_CODER(&f->Coders[0]))
if (!is_supported_coder(&f->Coders[0]))
return SZ_ERROR_UNSUPPORTED;
if (f->NumCoders == 1)
switch (f->NumCoders)
{
if (f->NumPackStreams != 1 || f->PackStreams[0] != 0 || f->NumBindPairs != 0)
case 1:
if ( f->NumPackStreams != 1
|| f->PackStreams[0] != 0
|| f->NumBindPairs != 0)
return SZ_ERROR_UNSUPPORTED;
return SZ_OK;
}
if (f->NumCoders == 2)
case 2:
{
CSzCoderInfo *c = &f->Coders[1];
if (c->MethodID > (uint32_t)0xFFFFFFFF ||
c->NumInStreams != 1 ||
c->NumOutStreams != 1 ||
@ -186,6 +230,7 @@ static SRes CheckSupportedFolder(const CSzFolder *f)
f->BindPairs[0].InIndex != 1 ||
f->BindPairs[0].OutIndex != 0)
return SZ_ERROR_UNSUPPORTED;
switch ((uint32_t)c->MethodID)
{
case k_BCJ:
@ -194,30 +239,33 @@ static SRes CheckSupportedFolder(const CSzFolder *f)
default:
return SZ_ERROR_UNSUPPORTED;
}
return SZ_OK;
}
if (f->NumCoders == 4)
{
if (!IS_SUPPORTED_CODER(&f->Coders[1]) ||
!IS_SUPPORTED_CODER(&f->Coders[2]) ||
return SZ_OK;
case 4:
if (!is_supported_coder(&f->Coders[1]) ||
!is_supported_coder(&f->Coders[2]) ||
!IS_BCJ2(&f->Coders[3]))
return SZ_ERROR_UNSUPPORTED;
if (f->NumPackStreams != 4 ||
f->PackStreams[0] != 2 ||
f->PackStreams[1] != 6 ||
f->PackStreams[2] != 1 ||
f->PackStreams[3] != 0 ||
f->NumBindPairs != 3 ||
f->BindPairs[0].InIndex != 5 || f->BindPairs[0].OutIndex != 0 ||
f->BindPairs[1].InIndex != 4 || f->BindPairs[1].OutIndex != 1 ||
f->BindPairs[2].InIndex != 3 || f->BindPairs[2].OutIndex != 2)
f->BindPairs[0].InIndex != 5 ||
f->BindPairs[0].OutIndex != 0 ||
f->BindPairs[1].InIndex != 4 ||
f->BindPairs[1].OutIndex != 1 ||
f->BindPairs[2].InIndex != 3 ||
f->BindPairs[2].OutIndex != 2)
return SZ_ERROR_UNSUPPORTED;
return SZ_OK;
}
return SZ_ERROR_UNSUPPORTED;
}
static uint64_t GetSum(const uint64_t *values, uint32_t idx)
static uint64_t get_sum(const uint64_t *values, uint32_t idx)
{
uint64_t sum = 0;
uint32_t i;
@ -226,9 +274,8 @@ static uint64_t GetSum(const uint64_t *values, uint32_t idx)
return sum;
}
#define CASE_BRA_CONV(isa) case k_ ## isa: isa ## _Convert(outBuffer, outSize, 0, 0); break;
static SRes SzFolder_Decode2(const CSzFolder *folder, const uint64_t *packSizes,
static SRes SzFolder_Decode2(const CSzFolder *folder,
const uint64_t *packSizes,
ILookInStream *inStream, uint64_t startPos,
uint8_t *outBuffer, size_t outSize, ISzAlloc *allocMain,
uint8_t *tempBuf[])
@ -237,25 +284,30 @@ static SRes SzFolder_Decode2(const CSzFolder *folder, const uint64_t *packSizes,
size_t tempSizes[3] = { 0, 0, 0};
size_t tempSize3 = 0;
uint8_t *tempBuf3 = 0;
int result = check_supported_folder(folder);
RINOK(CheckSupportedFolder(folder));
if (result != 0)
return result;
for (ci = 0; ci < folder->NumCoders; ci++)
{
CSzCoderInfo *coder = &folder->Coders[ci];
if (IS_MAIN_METHOD((uint32_t)coder->MethodID))
if (is_main_method((uint32_t)coder->MethodID))
{
int result;
uint64_t offset = 0;
uint64_t inSize = 0;
uint32_t si = 0;
uint64_t offset;
uint64_t inSize;
uint8_t *outBufCur = outBuffer;
size_t outSizeCur = outSize;
if (folder->NumCoders == 4)
{
uint32_t indices[] = { 3, 2, 0 };
uint64_t unpackSize = folder->UnpackSizes[ci];
si = indices[ci];
if (ci < 2)
{
uint8_t *temp;
@ -272,49 +324,74 @@ static SRes SzFolder_Decode2(const CSzFolder *folder, const uint64_t *packSizes,
{
if (unpackSize > outSize) /* check it */
return SZ_ERROR_PARAM;
tempBuf3 = outBufCur = outBuffer + (outSize - (size_t)unpackSize);
tempBuf3 = outBufCur = outBuffer
+ (outSize - (size_t)unpackSize);
tempSize3 = outSizeCur = (size_t)unpackSize;
}
else
return SZ_ERROR_UNSUPPORTED;
}
offset = GetSum(packSizes, si);
inSize = packSizes[si];
RINOK(LookInStream_SeekTo(inStream, startPos + offset));
if (coder->MethodID == k_Copy)
offset = get_sum(packSizes, si);
inSize = packSizes[si];
result = LookInStream_SeekTo(inStream, startPos + offset);
if (result != 0)
return result;
switch (coder->MethodID)
{
case k_Copy:
if (inSize != outSizeCur) /* check it */
return SZ_ERROR_DATA;
RINOK(SzDecodeCopy(inSize, inStream, outBufCur));
}
else if (coder->MethodID == k_LZMA)
{
RINOK(SzDecodeLzma(coder, inSize, inStream, outBufCur, outSizeCur, allocMain));
}
else if (coder->MethodID == k_LZMA2)
{
RINOK(SzDecodeLzma2(coder, inSize, inStream, outBufCur, outSizeCur, allocMain));
}
else
result = SzDecodeCopy(inSize, inStream, outBufCur);
if (result != 0)
return result;
break;
case k_LZMA:
result = SzDecodeLzma(
coder, inSize, inStream,
outBufCur, outSizeCur, allocMain);
if (result != 0)
return result;
break;
case k_LZMA2:
result = SzDecodeLzma2(
coder, inSize, inStream,
outBufCur, outSizeCur, allocMain);
if (result != 0)
return result;
break;
default:
return SZ_ERROR_UNSUPPORTED;
}
}
else if (coder->MethodID == k_BCJ2)
{
uint64_t offset = GetSum(packSizes, 1);
uint64_t s3Size = packSizes[1];
SRes res;
int result;
uint64_t offset = get_sum(packSizes, 1);
uint64_t s3Size = packSizes[1];
if (ci != 3)
return SZ_ERROR_UNSUPPORTED;
RINOK(LookInStream_SeekTo(inStream, startPos + offset));
result = LookInStream_SeekTo(inStream, startPos + offset);
if (result != 0)
return result;
tempSizes[2] = (size_t)s3Size;
if (tempSizes[2] != s3Size)
return SZ_ERROR_MEM;
tempBuf[2] = (uint8_t *)IAlloc_Alloc(allocMain, tempSizes[2]);
if (tempBuf[2] == 0 && tempSizes[2] != 0)
return SZ_ERROR_MEM;
res = SzDecodeCopy(s3Size, inStream, tempBuf[2]);
RINOK(res)
if (res != 0)
return res;
res = Bcj2_Decode(
tempBuf3, tempSize3,
@ -322,7 +399,9 @@ static SRes SzFolder_Decode2(const CSzFolder *folder, const uint64_t *packSizes,
tempBuf[1], tempSizes[1],
tempBuf[2], tempSizes[2],
outBuffer, outSize);
RINOK(res)
if (res != 0)
return res;
}
else
{
@ -337,7 +416,9 @@ static SRes SzFolder_Decode2(const CSzFolder *folder, const uint64_t *packSizes,
x86_Convert(outBuffer, outSize, 0, &state, 0);
break;
}
CASE_BRA_CONV(ARM)
case k_ARM:
ARM_Convert(outBuffer, outSize, 0, 0);
break;
default:
return SZ_ERROR_UNSUPPORTED;
}
@ -350,10 +431,12 @@ SRes SzFolder_Decode(const CSzFolder *folder, const uint64_t *packSizes,
ILookInStream *inStream, uint64_t startPos,
uint8_t *outBuffer, size_t outSize, ISzAlloc *allocMain)
{
uint8_t *tempBuf[3] = { 0, 0, 0};
int i;
SRes res = SzFolder_Decode2(folder, packSizes, inStream, startPos,
uint8_t *tempBuf[3] = { 0, 0, 0};
SRes res = SzFolder_Decode2(folder,
packSizes, inStream, startPos,
outBuffer, (size_t)outSize, allocMain, tempBuf);
for (i = 0; i < 3; i++)
IAlloc_Free(allocMain, tempBuf[i]);
return res;

427
deps/7zip/7zIn.c vendored
View File

@ -10,8 +10,6 @@
uint8_t k7zSignature[k7zSignatureSize] = {'7', 'z', 0xBC, 0xAF, 0x27, 0x1C};
#define RINOM(x) { if ((x) == 0) return SZ_ERROR_MEM; }
#define NUM_FOLDER_CODERS_MAX 32
#define NUM_CODER_STREAMS_MAX 32
@ -320,7 +318,9 @@ static SRes SzReaduint8_ts(CSzData *sd, uint8_t *data, size_t size)
size_t i;
for (i = 0; i < size; i++)
{
RINOK(SzReaduint8_t(sd, data + i));
int result = SzReaduint8_t(sd, data + i);
if (result != 0)
return result;
}
return SZ_OK;
}
@ -332,7 +332,9 @@ static SRes SzReaduint32_t(CSzData *sd, uint32_t *value)
for (i = 0; i < 4; i++)
{
uint8_t b = 0;
RINOK(SzReaduint8_t(sd, &b));
int result = SzReaduint8_t(sd, &b);
if (result != 0)
return result;
*value |= ((uint32_t)(b) << (8 * i));
}
return SZ_OK;
@ -343,13 +345,16 @@ static SRes SzReadNumber(CSzData *sd, uint64_t *value)
int i;
uint8_t firstuint8_t = 0;
uint8_t mask = 0x80;
int result = SzReaduint8_t(sd, &firstuint8_t);
RINOK(SzReaduint8_t(sd, &firstuint8_t));
if (result != 0)
return result;
*value = 0;
for (i = 0; i < 8; i++)
{
int result;
uint8_t b = 0;
if ((firstuint8_t & mask) == 0)
{
@ -357,7 +362,10 @@ static SRes SzReadNumber(CSzData *sd, uint64_t *value)
*value += (highPart << (8 * i));
return SZ_OK;
}
RINOK(SzReaduint8_t(sd, &b));
result = SzReaduint8_t(sd, &b);
if (result != 0)
return result;
*value |= ((uint64_t)b << (8 * i));
mask >>= 1;
}
@ -367,7 +375,9 @@ static SRes SzReadNumber(CSzData *sd, uint64_t *value)
static SRes SzReadNumber32(CSzData *sd, uint32_t *value)
{
uint64_t value64;
RINOK(SzReadNumber(sd, &value64));
int result = SzReadNumber(sd, &value64);
if (result != 0)
return result;
if (value64 >= 0x80000000)
return SZ_ERROR_UNSUPPORTED;
if (value64 >= ((uint64_t)(1) << ((sizeof(size_t) - 1) * 8 + 2)))
@ -393,7 +403,9 @@ static SRes SzSkeepDataSize(CSzData *sd, uint64_t size)
static SRes SzSkeepData(CSzData *sd)
{
uint64_t size;
RINOK(SzReadNumber(sd, &size));
int result = SzReadNumber(sd, &size);
if (result != 0)
return result;
return SzSkeepDataSize(sd, size);
}
@ -402,7 +414,9 @@ static SRes SzReadArchiveProperties(CSzData *sd)
for (;;)
{
uint64_t type;
RINOK(SzReadID(sd, &type));
int result = SzReadID(sd, &type);
if (result != 0)
return result;
if (type == k7zIdEnd)
break;
SzSkeepData(sd);
@ -415,12 +429,16 @@ static SRes SzWaitAttribute(CSzData *sd, uint64_t attribute)
for (;;)
{
uint64_t type;
RINOK(SzReadID(sd, &type));
int result = SzReadID(sd, &type);
if (result != 0)
return result;
if (type == attribute)
return SZ_OK;
if (type == k7zIdEnd)
return SZ_ERROR_ARCHIVE;
RINOK(SzSkeepData(sd));
result = SzSkeepData(sd);
if (result != 0)
return result;
}
}
@ -435,7 +453,9 @@ static SRes SzReadBoolVector(
{
if (mask == 0)
{
RINOK(SzReaduint8_t(sd, &b));
int result = SzReaduint8_t(sd, &b);
if (result != 0)
return result;
mask = 0x80;
}
(*v)[i] = (uint8_t)(((b & mask) != 0) ? 1 : 0);
@ -448,7 +468,10 @@ static SRes SzReadBoolVector2(CSzData *sd, size_t numItems, uint8_t **v, ISzAllo
{
size_t i;
uint8_t allAreDefined = 0;
RINOK(SzReaduint8_t(sd, &allAreDefined));
int result = SzReaduint8_t(sd, &allAreDefined);
if (result != 0)
return result;
if (allAreDefined == 0)
return SzReadBoolVector(sd, numItems, v, alloc);
MY_ALLOC(uint8_t, *v, numItems, alloc);
@ -465,12 +488,21 @@ static SRes SzReadHashDigests(
ISzAlloc *alloc)
{
size_t i;
RINOK(SzReadBoolVector2(sd, numItems, digestsDefined, alloc));
int result = SzReadBoolVector2(sd, numItems, digestsDefined, alloc);
if (result != 0)
return result;
MY_ALLOC(uint32_t, *digests, numItems, alloc);
for (i = 0; i < numItems; i++)
{
if ((*digestsDefined)[i])
{
RINOK(SzReaduint32_t(sd, (*digests) + i));
int result = SzReaduint32_t(sd, (*digests) + i);
if (result != 0)
return result;
}
}
return SZ_OK;
}
@ -485,30 +517,44 @@ static SRes SzReadPackInfo(
ISzAlloc *alloc)
{
uint32_t i;
RINOK(SzReadNumber(sd, dataOffset));
RINOK(SzReadNumber32(sd, numPackStreams));
int result = SzReadNumber(sd, dataOffset);
RINOK(SzWaitAttribute(sd, k7zIdSize));
if (result != 0)
return result;
result = SzReadNumber32(sd, numPackStreams);
if (result != 0)
return result;
result = SzWaitAttribute(sd, k7zIdSize);
if (result != 0)
return result;
MY_ALLOC(uint64_t, *packSizes, (size_t)*numPackStreams, alloc);
for (i = 0; i < *numPackStreams; i++)
{
RINOK(SzReadNumber(sd, (*packSizes) + i));
result = SzReadNumber(sd, (*packSizes) + i);
if (result != 0)
return result;
}
for (;;)
{
uint64_t type;
RINOK(SzReadID(sd, &type));
result = SzReadID(sd, &type);
if (result != 0)
return result;
if (type == k7zIdEnd)
break;
if (type == k7zIdCRC)
{
RINOK(SzReadHashDigests(sd, (size_t)*numPackStreams, packCRCsDefined, packCRCs, alloc));
result = SzReadHashDigests(sd, (size_t)*numPackStreams, packCRCsDefined, packCRCs, alloc);
if (result != 0)
return result;
continue;
}
RINOK(SzSkeepData(sd));
result = SzSkeepData(sd);
if (result != 0)
return result;
}
if (*packCRCsDefined == 0)
{
@ -526,8 +572,12 @@ static SRes SzReadPackInfo(
static SRes SzReadSwitch(CSzData *sd)
{
uint8_t external = 0;
RINOK(SzReaduint8_t(sd, &external));
return (external == 0) ? SZ_OK: SZ_ERROR_UNSUPPORTED;
int result = SzReaduint8_t(sd, &external);
if (result != 0)
return result;
if (external != 0)
return SZ_ERROR_UNSUPPORTED;
return SZ_OK;
}
static SRes SzGetNextFolderItem(CSzData *sd, CSzFolder *folder, ISzAlloc *alloc)
@ -538,8 +588,10 @@ static SRes SzGetNextFolderItem(CSzData *sd, CSzFolder *folder, ISzAlloc *alloc)
uint32_t numInStreams = 0;
uint32_t numOutStreams = 0;
uint32_t numCoders = 0;
int result = SzReadNumber32(sd, &numCoders);
RINOK(SzReadNumber32(sd, &numCoders));
if (result != 0)
return result;
if (numCoders > NUM_FOLDER_CODERS_MAX)
return SZ_ERROR_UNSUPPORTED;
folder->NumCoders = numCoders;
@ -555,19 +607,36 @@ static SRes SzGetNextFolderItem(CSzData *sd, CSzFolder *folder, ISzAlloc *alloc)
uint8_t longID[15];
uint8_t mainuint8_t = 0;
CSzCoderInfo *coder = folder->Coders + i;
RINOK(SzReaduint8_t(sd, &mainuint8_t));
int result = SzReaduint8_t(sd, &mainuint8_t);
if (result != 0)
return result;
idSize = (unsigned)(mainuint8_t & 0xF);
RINOK(SzReaduint8_ts(sd, longID, idSize));
result = SzReaduint8_ts(sd, longID, idSize);
if (result != 0)
return result;
if (idSize > sizeof(coder->MethodID))
return SZ_ERROR_UNSUPPORTED;
coder->MethodID = 0;
for (j = 0; j < idSize; j++)
coder->MethodID |= (uint64_t)longID[idSize - 1 - j] << (8 * j);
if ((mainuint8_t & 0x10) != 0)
{
RINOK(SzReadNumber32(sd, &coder->NumInStreams));
RINOK(SzReadNumber32(sd, &coder->NumOutStreams));
int result = SzReadNumber32(sd, &coder->NumInStreams);
if (result != 0)
return result;
result = SzReadNumber32(sd, &coder->NumOutStreams);
if (result != 0)
return result;
if (coder->NumInStreams > NUM_CODER_STREAMS_MAX ||
coder->NumOutStreams > NUM_CODER_STREAMS_MAX)
return SZ_ERROR_UNSUPPORTED;
@ -580,26 +649,43 @@ static SRes SzGetNextFolderItem(CSzData *sd, CSzFolder *folder, ISzAlloc *alloc)
if ((mainuint8_t & 0x20) != 0)
{
uint64_t propertiesSize = 0;
RINOK(SzReadNumber(sd, &propertiesSize));
int result = SzReadNumber(sd, &propertiesSize);
if (result != 0)
return result;
if (!Buf_Create(&coder->Props, (size_t)propertiesSize, alloc))
return SZ_ERROR_MEM;
RINOK(SzReaduint8_ts(sd, coder->Props.data, (size_t)propertiesSize));
result = SzReaduint8_ts(sd, coder->Props.data, (size_t)propertiesSize);
if (result != 0)
return result;
}
while ((mainuint8_t & 0x80) != 0)
{
RINOK(SzReaduint8_t(sd, &mainuint8_t));
RINOK(SzSkeepDataSize(sd, (mainuint8_t & 0xF)));
result = SzReaduint8_t(sd, &mainuint8_t);
if (result != 0)
return result;
result = SzSkeepDataSize(sd, (mainuint8_t & 0xF));
if (result != 0)
return result;
if ((mainuint8_t & 0x10) != 0)
{
uint32_t n;
RINOK(SzReadNumber32(sd, &n));
RINOK(SzReadNumber32(sd, &n));
int result = SzReadNumber32(sd, &n);
if (result != 0)
return result;
result = SzReadNumber32(sd, &n);
if (result != 0)
return result;
}
if ((mainuint8_t & 0x20) != 0)
{
uint64_t propertiesSize = 0;
RINOK(SzReadNumber(sd, &propertiesSize));
RINOK(SzSkeepDataSize(sd, propertiesSize));
int result = SzReadNumber(sd, &propertiesSize);
if (result != 0)
return result;
result = SzSkeepDataSize(sd, propertiesSize);
if (result != 0)
return result;
}
}
numInStreams += coder->NumInStreams;
@ -615,8 +701,13 @@ static SRes SzGetNextFolderItem(CSzData *sd, CSzFolder *folder, ISzAlloc *alloc)
for (i = 0; i < numBindPairs; i++)
{
CSzBindPair *bp = folder->BindPairs + i;
RINOK(SzReadNumber32(sd, &bp->InIndex));
RINOK(SzReadNumber32(sd, &bp->OutIndex));
int result = SzReadNumber32(sd, &bp->InIndex);
if (result != 0)
return result;
result = SzReadNumber32(sd, &bp->OutIndex);
if (result != 0)
return result;
}
if (numInStreams < numBindPairs)
@ -637,7 +728,9 @@ static SRes SzGetNextFolderItem(CSzData *sd, CSzFolder *folder, ISzAlloc *alloc)
else
for (i = 0; i < numPackStreams; i++)
{
RINOK(SzReadNumber32(sd, folder->PackStreams + i));
int result = SzReadNumber32(sd, folder->PackStreams + i);
if (result != 0)
return result;
}
return SZ_OK;
}
@ -650,10 +743,21 @@ static SRes SzReadUnpackInfo(
ISzAlloc *allocTemp)
{
uint32_t i;
RINOK(SzWaitAttribute(sd, k7zIdFolder));
RINOK(SzReadNumber32(sd, numFolders));
int result = SzWaitAttribute(sd, k7zIdFolder);
if (result != 0)
return result;
result = SzReadNumber32(sd, numFolders);
if (result != 0)
return result;
{
RINOK(SzReadSwitch(sd));
result = SzReadSwitch(sd);
if (result != 0)
return result;
MY_ALLOC(CSzFolder, *folders, (size_t)*numFolders, alloc);
@ -662,11 +766,17 @@ static SRes SzReadUnpackInfo(
for (i = 0; i < *numFolders; i++)
{
RINOK(SzGetNextFolderItem(sd, (*folders) + i, alloc));
result = SzGetNextFolderItem(sd, (*folders) + i, alloc);
if (result != 0)
return result;
}
}
RINOK(SzWaitAttribute(sd, k7zIdCodersUnpackSize));
result = SzWaitAttribute(sd, k7zIdCodersUnpackSize);
if (result != 0)
return result;
for (i = 0; i < *numFolders; i++)
{
@ -678,22 +788,27 @@ static SRes SzReadUnpackInfo(
for (j = 0; j < numOutStreams; j++)
{
RINOK(SzReadNumber(sd, folder->UnpackSizes + j));
int result = SzReadNumber(sd, folder->UnpackSizes + j);
if (result != 0)
return result;
}
}
for (;;)
{
uint64_t type;
RINOK(SzReadID(sd, &type));
int result = SzReadID(sd, &type);
if (result != 0)
return result;
if (type == k7zIdEnd)
return SZ_OK;
if (type == k7zIdCRC)
{
SRes res;
uint8_t *crcsDefined = 0;
uint32_t *crcs = 0;
res = SzReadHashDigests(sd, *numFolders, &crcsDefined, &crcs, allocTemp);
SRes res = SzReadHashDigests(
sd, *numFolders, &crcsDefined, &crcs, allocTemp);
if (res == SZ_OK)
{
for (i = 0; i < *numFolders; i++)
@ -705,10 +820,13 @@ static SRes SzReadUnpackInfo(
}
IAlloc_Free(allocTemp, crcs);
IAlloc_Free(allocTemp, crcsDefined);
RINOK(res);
if (res != 0)
return res;
continue;
}
RINOK(SzSkeepData(sd));
result = SzSkeepData(sd);
if (result != 0)
return result;
}
}
@ -722,6 +840,7 @@ static SRes SzReadSubStreamsInfo(
uint32_t **digests,
ISzAlloc *allocTemp)
{
int result;
uint32_t i;
uint64_t type = 0;
uint32_t si = 0;
@ -733,14 +852,19 @@ static SRes SzReadSubStreamsInfo(
for (;;)
{
RINOK(SzReadID(sd, &type));
int result = SzReadID(sd, &type);
if (result != 0)
return result;
if (type == k7zIdNumUnpackStream)
{
*numUnpackStreams = 0;
for (i = 0; i < numFolders; i++)
{
uint32_t numStreams = 0;
RINOK(SzReadNumber32(sd, &numStreams));
int result = SzReadNumber32(sd, &numStreams);
if (result != 0)
return result;
folders[i].NumUnpackStreams = numStreams;
*numUnpackStreams += numStreams;
}
@ -750,7 +874,9 @@ static SRes SzReadSubStreamsInfo(
break;
if (type == k7zIdEnd)
break;
RINOK(SzSkeepData(sd));
result = SzSkeepData(sd);
if (result != 0)
return result;
}
if (*numUnpackStreams == 0)
@ -762,11 +888,14 @@ static SRes SzReadSubStreamsInfo(
else
{
*unpackSizes = (uint64_t *)IAlloc_Alloc(allocTemp, (size_t)*numUnpackStreams * sizeof(uint64_t));
RINOM(*unpackSizes);
if (*unpackSizes == 0)
return SZ_ERROR_MEM;
*digestsDefined = (uint8_t *)IAlloc_Alloc(allocTemp, (size_t)*numUnpackStreams * sizeof(uint8_t));
RINOM(*digestsDefined);
if (*digestsDefined == 0)
return SZ_ERROR_MEM;
*digests = (uint32_t *)IAlloc_Alloc(allocTemp, (size_t)*numUnpackStreams * sizeof(uint32_t));
RINOM(*digests);
if (*digests == 0)
return SZ_ERROR_MEM;
}
for (i = 0; i < numFolders; i++)
@ -784,7 +913,10 @@ static SRes SzReadSubStreamsInfo(
for (j = 1; j < numSubstreams; j++)
{
uint64_t size;
RINOK(SzReadNumber(sd, &size));
int result = SzReadNumber(sd, &size);
if (result != 0)
return result;
(*unpackSizes)[si++] = size;
sum += size;
}
@ -792,7 +924,9 @@ static SRes SzReadSubStreamsInfo(
}
if (type == k7zIdSize)
{
RINOK(SzReadID(sd, &type));
result = SzReadID(sd, &type);
if (result != 0)
return result;
}
for (i = 0; i < *numUnpackStreams; i++)
@ -845,15 +979,20 @@ static SRes SzReadSubStreamsInfo(
}
IAlloc_Free(allocTemp, digestsDefined2);
IAlloc_Free(allocTemp, digests2);
RINOK(res);
if (res != 0)
return res;
}
else if (type == k7zIdEnd)
return SZ_OK;
else
{
RINOK(SzSkeepData(sd));
result = SzSkeepData(sd);
if (result != 0)
return result;
}
RINOK(SzReadID(sd, &type));
result = SzReadID(sd, &type);
if (result != 0)
return result;
}
}
@ -872,7 +1011,10 @@ static SRes SzReadStreamsInfo(
for (;;)
{
uint64_t type;
RINOK(SzReadID(sd, &type));
int result = SzReadID(sd, &type);
if (result != 0)
return result;
if ((uint64_t)(int)type != type)
return SZ_ERROR_UNSUPPORTED;
switch((int)type)
@ -881,19 +1023,26 @@ static SRes SzReadStreamsInfo(
return SZ_OK;
case k7zIdPackInfo:
{
RINOK(SzReadPackInfo(sd, dataOffset, &p->NumPackStreams,
&p->PackSizes, &p->PackCRCsDefined, &p->PackCRCs, alloc));
int result = SzReadPackInfo(sd, dataOffset, &p->NumPackStreams,
&p->PackSizes, &p->PackCRCsDefined, &p->PackCRCs, alloc);
if (result != 0)
return result;
break;
}
case k7zIdUnpackInfo:
{
RINOK(SzReadUnpackInfo(sd, &p->NumFolders, &p->Folders, alloc, allocTemp));
int result = SzReadUnpackInfo(sd, &p->NumFolders, &p->Folders, alloc, allocTemp);
if (result != 0)
return result;
break;
}
case k7zIdSubStreamsInfo:
{
RINOK(SzReadSubStreamsInfo(sd, p->NumFolders, p->Folders,
numUnpackStreams, unpackSizes, digestsDefined, digests, allocTemp));
int result = SzReadSubStreamsInfo(sd, p->NumFolders, p->Folders,
numUnpackStreams, unpackSizes, digestsDefined, digests, allocTemp);
if (result != 0)
return result;
break;
}
default:
@ -958,27 +1107,37 @@ static SRes SzReadHeader2(
uint32_t numFiles = 0;
CSzFileItem *files = 0;
uint32_t numEmptyStreams = 0;
int result = SzReadID(sd, &type);
RINOK(SzReadID(sd, &type));
if (result != 0)
return result;
if (type == k7zIdArchiveProperties)
{
RINOK(SzReadArchiveProperties(sd));
RINOK(SzReadID(sd, &type));
result = SzReadArchiveProperties(sd);
if (result != 0)
return result;
result = SzReadID(sd, &type);
if (result != 0)
return result;
}
if (type == k7zIdMainStreamsInfo)
{
RINOK(SzReadStreamsInfo(sd,
result = SzReadStreamsInfo(sd,
&p->dataPos,
&p->db,
&numUnpackStreams,
unpackSizes,
digestsDefined,
digests, allocMain, allocTemp));
digests, allocMain, allocTemp);
if (result != 0)
return result;
p->dataPos += p->startPosAfterHeader;
RINOK(SzReadID(sd, &type));
result = SzReadID(sd, &type);
if (result != 0)
return result;
}
if (type == k7zIdEnd)
@ -986,7 +1145,9 @@ static SRes SzReadHeader2(
if (type != k7zIdFilesInfo)
return SZ_ERROR_ARCHIVE;
RINOK(SzReadNumber32(sd, &numFiles));
result = SzReadNumber32(sd, &numFiles);
if (result != 0)
return result;
p->db.NumFiles = numFiles;
MY_ALLOC(CSzFileItem, files, (size_t)numFiles, allocMain);
@ -998,41 +1159,54 @@ static SRes SzReadHeader2(
for (;;)
{
uint64_t size;
RINOK(SzReadID(sd, &type));
int result = SzReadID(sd, &type);
if (result != 0)
return result;
if (type == k7zIdEnd)
break;
RINOK(SzReadNumber(sd, &size));
result = SzReadNumber(sd, &size);
if (result != 0)
return result;
if (size > sd->Size)
return SZ_ERROR_ARCHIVE;
if ((uint64_t)(int)type != type)
{
RINOK(SzSkeepDataSize(sd, size));
int result = SzSkeepDataSize(sd, size);
if (result != 0)
return result;
}
else
switch((int)type)
{
case k7zIdName:
{
size_t namesSize;
RINOK(SzReadSwitch(sd));
namesSize = (size_t)size - 1;
int result = SzReadSwitch(sd);
size_t namesSize = (size_t)size - 1;
if (result != 0)
return result;
if ((namesSize & 1) != 0)
return SZ_ERROR_ARCHIVE;
if (!Buf_Create(&p->FileNames, namesSize, allocMain))
return SZ_ERROR_MEM;
MY_ALLOC(size_t, p->FileNameOffsets, numFiles + 1, allocMain);
memcpy(p->FileNames.data, sd->Data, namesSize);
RINOK(SzReadFileNames(sd->Data, namesSize >> 1, numFiles, p->FileNameOffsets))
RINOK(SzSkeepDataSize(sd, namesSize));
result = SzReadFileNames(sd->Data, namesSize >> 1, numFiles, p->FileNameOffsets);
if (result != 0)
return result;
result = SzSkeepDataSize(sd, namesSize);
if (result != 0)
return result;
break;
}
case k7zIdEmptyStream:
{
RINOK(SzReadBoolVector(sd, numFiles, emptyStreamVector, allocTemp));
int result = SzReadBoolVector(sd, numFiles, emptyStreamVector, allocTemp);
if (result != 0)
return result;
numEmptyStreams = 0;
for (i = 0; i < numFiles; i++)
if ((*emptyStreamVector)[i])
@ -1041,13 +1215,20 @@ static SRes SzReadHeader2(
}
case k7zIdEmptyFile:
{
RINOK(SzReadBoolVector(sd, numEmptyStreams, emptyFileVector, allocTemp));
int result = SzReadBoolVector(sd, numEmptyStreams, emptyFileVector, allocTemp);
if (result != 0)
return result;
break;
}
case k7zIdWinAttributes:
{
RINOK(SzReadBoolVector2(sd, numFiles, lwtVector, allocTemp));
RINOK(SzReadSwitch(sd));
int result = SzReadBoolVector2(sd, numFiles, lwtVector, allocTemp);
if (result != 0)
return result;
result = SzReadSwitch(sd);
if (result != 0)
return result;
for (i = 0; i < numFiles; i++)
{
CSzFileItem *f = &files[i];
@ -1056,7 +1237,9 @@ static SRes SzReadHeader2(
f->Attrib = 0;
if (defined)
{
RINOK(SzReaduint32_t(sd, &f->Attrib));
result = SzReaduint32_t(sd, &f->Attrib);
if (result != 0)
return result;
}
}
IAlloc_Free(allocTemp, *lwtVector);
@ -1065,9 +1248,14 @@ static SRes SzReadHeader2(
}
case k7zIdMTime:
{
RINOK(SzReadBoolVector2(sd,
numFiles, lwtVector, allocTemp));
RINOK(SzReadSwitch(sd));
int result = SzReadBoolVector2(sd,
numFiles, lwtVector, allocTemp);
if (result != 0)
return result;
result = SzReadSwitch(sd);
if (result != 0)
return result;
for (i = 0; i < numFiles; i++)
{
CSzFileItem *f = &files[i];
@ -1076,8 +1264,12 @@ static SRes SzReadHeader2(
f->MTime.Low = f->MTime.High = 0;
if (defined)
{
RINOK(SzReaduint32_t(sd, &f->MTime.Low));
RINOK(SzReaduint32_t(sd, &f->MTime.High));
result = SzReaduint32_t(sd, &f->MTime.Low);
if (result != 0)
return result;
result = SzReaduint32_t(sd, &f->MTime.High);
if (result != 0)
return result;
}
}
IAlloc_Free(allocTemp, *lwtVector);
@ -1086,7 +1278,9 @@ static SRes SzReadHeader2(
}
default:
{
RINOK(SzSkeepDataSize(sd, size));
result = SzSkeepDataSize(sd, size);
if (result != 0)
return result;
}
}
}
@ -1170,10 +1364,12 @@ static SRes SzReadAndDecodePackedStreams2(
uint64_t dataStartPos = 0;
uint64_t unpackSize = 0;
uint32_t numUnpackStreams = 0;
RINOK(SzReadStreamsInfo(sd, &dataStartPos, p,
int result = SzReadStreamsInfo(sd, &dataStartPos, p,
&numUnpackStreams, unpackSizes, digestsDefined, digests,
allocTemp, allocTemp));
allocTemp, allocTemp);
if (result != 0)
return result;
dataStartPos += baseOffset;
if (p->NumFolders != 1)
@ -1182,7 +1378,10 @@ static SRes SzReadAndDecodePackedStreams2(
folder = p->Folders;
unpackSize = SzFolder_GetUnpackSize(folder);
RINOK(LookInStream_SeekTo(inStream, dataStartPos));
result = LookInStream_SeekTo(inStream, dataStartPos);
if (result != 0)
return result;
if (!Buf_Create(outBuffer, (size_t)unpackSize, allocTemp))
return SZ_ERROR_MEM;
@ -1190,7 +1389,8 @@ static SRes SzReadAndDecodePackedStreams2(
res = SzFolder_Decode(folder, p->PackSizes,
inStream, dataStartPos,
outBuffer->data, (size_t)unpackSize, allocTemp);
RINOK(res);
if (res != 0)
return res;
if (folder->UnpackCRCDefined)
if (CrcCalc(outBuffer->data, (size_t)unpackSize) != folder->UnpackCRC)
return SZ_ERROR_CRC;
@ -1236,10 +1436,17 @@ static SRes SzArEx_Open2(
size_t nextHeaderSizeT;
uint32_t nextHeaderCRC;
int64_t startArcPos = 0;
int result = inStream->Seek(inStream, &startArcPos, SZ_SEEK_CUR);
RINOK(inStream->Seek(inStream, &startArcPos, SZ_SEEK_CUR));
RINOK(LookInStream_Read2(inStream, header, k7zStartHeaderSize,
SZ_ERROR_NO_ARCHIVE));
if (result != 0)
return result;
result = LookInStream_Read2(inStream, header,
k7zStartHeaderSize,
SZ_ERROR_NO_ARCHIVE);
if (result != 0)
return result;
if (!TestSignatureCandidate(header))
return SZ_ERROR_NO_ARCHIVE;
@ -1266,14 +1473,19 @@ static SRes SzArEx_Open2(
{
int64_t pos = 0;
RINOK(inStream->Seek(inStream, &pos, SZ_SEEK_END));
int result = inStream->Seek(inStream, &pos, SZ_SEEK_END);
if (result != 0)
return result;
if ((uint64_t)pos < startArcPos + nextHeaderOffset ||
(uint64_t)pos < startArcPos + k7zStartHeaderSize + nextHeaderOffset ||
(uint64_t)pos < startArcPos + k7zStartHeaderSize + nextHeaderOffset + nextHeaderSize)
return SZ_ERROR_INPUT_EOF;
}
RINOK(LookInStream_SeekTo(inStream, startArcPos + k7zStartHeaderSize + nextHeaderOffset));
result = LookInStream_SeekTo(inStream, startArcPos + k7zStartHeaderSize + nextHeaderOffset);
if (result != 0)
return result;
if (!Buf_Create(&buffer, nextHeaderSizeT, allocTemp))
return SZ_ERROR_MEM;
@ -1359,6 +1571,7 @@ SRes SzArEx_Extract(
if (*outBuffer == 0 || *blockIndex != folderIndex)
{
int result;
CSzFolder *folder = p->db.Folders + folderIndex;
uint64_t unpackSizeSpec = SzFolder_GetUnpackSize(folder);
size_t unpackSize = (size_t)unpackSizeSpec;
@ -1369,8 +1582,9 @@ SRes SzArEx_Extract(
*blockIndex = folderIndex;
IAlloc_Free(allocMain, *outBuffer);
*outBuffer = 0;
RINOK(LookInStream_SeekTo(inStream, startOffset));
result = LookInStream_SeekTo(inStream, startOffset);
if (result != 0)
return result;
if (res == SZ_OK)
{
@ -1381,6 +1595,7 @@ SRes SzArEx_Extract(
if (*outBuffer == 0)
res = SZ_ERROR_MEM;
}
if (res == SZ_OK)
{
res = SzFolder_Decode(folder,

33
deps/7zip/7zStream.c vendored
View File

@ -7,14 +7,25 @@
#include "7zTypes.h"
SRes SeqInStream_Readuint8_t(ISeqInStream *stream, uint8_t *buf);
SRes SeqInStream_Readuint8_t(ISeqInStream *stream, uint8_t *buf)
{
size_t processed = 1;
int result = stream->Read(stream, buf, &processed);
if (result != 0)
return result;
if (processed != 1)
return SZ_ERROR_INPUT_EOF;
return SZ_OK;
}
SRes SeqInStream_Read2(ISeqInStream *stream, void *buf, size_t size, SRes errorType)
{
while (size != 0)
{
size_t processed = size;
RINOK(stream->Read(stream, buf, &processed));
int result = stream->Read(stream, buf, &processed);
if (result != 0)
return result;
if (processed == 0)
return errorType;
buf = (void *)((uint8_t *)buf + processed);
@ -28,12 +39,6 @@ SRes SeqInStream_Read(ISeqInStream *stream, void *buf, size_t size)
return SeqInStream_Read2(stream, buf, size, SZ_ERROR_INPUT_EOF);
}
SRes SeqInStream_Readuint8_t(ISeqInStream *stream, uint8_t *buf)
{
size_t processed = 1;
RINOK(stream->Read(stream, buf, &processed));
return (processed == 1) ? SZ_OK : SZ_ERROR_INPUT_EOF;
}
SRes LookInStream_SeekTo(ILookInStream *stream, uint64_t offset)
{
@ -43,20 +48,26 @@ SRes LookInStream_SeekTo(ILookInStream *stream, uint64_t offset)
SRes LookInStream_LookRead(ILookInStream *stream, void *buf, size_t *size)
{
int result;
const void *lookBuf;
if (*size == 0)
return SZ_OK;
RINOK(stream->Look(stream, &lookBuf, size));
result = stream->Look(stream, &lookBuf, size);
if (result != 0)
return result;
memcpy(buf, lookBuf, *size);
return stream->Skip(stream, *size);
}
SRes LookInStream_Read2(ILookInStream *stream, void *buf, size_t size, SRes errorType)
SRes LookInStream_Read2(ILookInStream *stream,
void *buf, size_t size, SRes errorType)
{
while (size != 0)
{
size_t processed = size;
RINOK(stream->Read(stream, buf, &processed));
int result = stream->Read(stream, buf, &processed);
if (result != 0)
return result;
if (processed == 0)
return errorType;
buf = (void *)((uint8_t *)buf + processed);

22
deps/7zip/Lzma2Dec.c vendored
View File

@ -66,14 +66,18 @@ static SRes Lzma2Dec_GetOldProps(uint8_t prop, uint8_t *props)
SRes Lzma2Dec_AllocateProbs(CLzma2Dec *p, uint8_t prop, ISzAlloc *alloc)
{
uint8_t props[LZMA_PROPS_SIZE];
RINOK(Lzma2Dec_GetOldProps(prop, props));
int result = Lzma2Dec_GetOldProps(prop, props);
if (result != 0)
return result;
return LzmaDec_AllocateProbs(&p->decoder, props, LZMA_PROPS_SIZE, alloc);
}
SRes Lzma2Dec_Allocate(CLzma2Dec *p, uint8_t prop, ISzAlloc *alloc)
{
uint8_t props[LZMA_PROPS_SIZE];
RINOK(Lzma2Dec_GetOldProps(prop, props));
int result = Lzma2Dec_GetOldProps(prop, props);
if (result != 0)
return result;
return LzmaDec_Allocate(&p->decoder, props, LZMA_PROPS_SIZE, alloc);
}
@ -259,7 +263,8 @@ SRes Lzma2Dec_DecodeToDic(CLzma2Dec *p, size_t dicLimit,
outSizeProcessed = p->decoder.dicPos - dicPos;
p->unpackSize -= (uint32_t)outSizeProcessed;
RINOK(res);
if (res != 0)
return res;
if (*status == LZMA_STATUS_NEEDS_MORE_INPUT)
return res;
@ -321,8 +326,9 @@ SRes Lzma2Dec_DecodeToBuf(CLzma2Dec *p, uint8_t *dest, size_t *destLen, const ui
SRes Lzma2Decode(uint8_t *dest, size_t *destLen, const uint8_t *src, size_t *srcLen,
uint8_t prop, ELzmaFinishMode finishMode, ELzmaStatus *status, ISzAlloc *alloc)
{
CLzma2Dec decoder;
SRes res;
int result;
CLzma2Dec decoder;
size_t outSize = *destLen, inSize = *srcLen;
uint8_t props[LZMA_PROPS_SIZE];
@ -333,8 +339,12 @@ SRes Lzma2Decode(uint8_t *dest, size_t *destLen, const uint8_t *src, size_t *src
decoder.decoder.dic = dest;
decoder.decoder.dicBufSize = outSize;
RINOK(Lzma2Dec_GetOldProps(prop, props));
RINOK(LzmaDec_AllocateProbs(&decoder.decoder, props, LZMA_PROPS_SIZE, alloc));
result = Lzma2Dec_GetOldProps(prop, props);
if (result != 0)
return result;
result = LzmaDec_AllocateProbs(&decoder.decoder, props, LZMA_PROPS_SIZE, alloc);
if (result != 0)
return result;
*srcLen = inSize;
res = Lzma2Dec_DecodeToDic(&decoder, outSize, src, srcLen, finishMode, status);

89
deps/7zip/LzmaDec.c vendored
View File

@ -494,6 +494,7 @@ static int MY_FAST_CALL LzmaDec_DecodeReal2(CLzmaDec *p, size_t limit, const uin
{
do
{
int result;
size_t limit2 = limit;
if (p->checkDicSize == 0)
{
@ -501,12 +502,17 @@ static int MY_FAST_CALL LzmaDec_DecodeReal2(CLzmaDec *p, size_t limit, const uin
if (limit - p->dicPos > rem)
limit2 = p->dicPos + rem;
}
RINOK(LzmaDec_DecodeReal(p, limit2, bufLimit));
result = LzmaDec_DecodeReal(p, limit2, bufLimit);
if (result != 0)
return result;
if (p->processedPos >= p->prop.dicSize)
p->checkDicSize = p->prop.dicSize;
LzmaDec_WriteRem(p, limit);
}
while (p->dicPos < limit && p->buf < bufLimit && p->remainLen < kMatchSpecLenStart);
while (
p->dicPos < limit &&
p->buf < bufLimit &&
p->remainLen < kMatchSpecLenStart);
if (p->remainLen > kMatchSpecLenStart)
{
@ -517,28 +523,27 @@ static int MY_FAST_CALL LzmaDec_DecodeReal2(CLzmaDec *p, size_t limit, const uin
typedef enum
{
DUMMY_ERROR, /* unexpected end of input stream */
DUMMY_ERROR = 0, /* unexpected end of input stream */
DUMMY_LIT,
DUMMY_MATCH,
DUMMY_REP
} ELzmaDummy;
static ELzmaDummy LzmaDec_TryDummy(const CLzmaDec *p, const uint8_t *buf, size_t inSize)
static ELzmaDummy LzmaDec_TryDummy(
const CLzmaDec *p, const uint8_t *buf, size_t inSize)
{
ELzmaDummy res;
uint32_t bound;
unsigned ttt;
uint32_t range = p->range;
uint32_t codes = p->code;
const uint8_t *bufLimit = buf + inSize;
uint16_t *probs = p->probs;
unsigned state = p->state;
ELzmaDummy res;
{
uint16_t *prob;
uint32_t bound;
unsigned ttt;
unsigned posState = (p->processedPos) & ((1 << p->prop.pb) - 1);
uint16_t *prob = probs + IsMatch +
(state << kNumPosBitsMax) + posState;
prob = probs + IsMatch + (state << kNumPosBitsMax) + posState;
IF_BIT_0_CHECK(prob)
{
UPDATE_0_CHECK
@ -708,21 +713,21 @@ static ELzmaDummy LzmaDec_TryDummy(const CLzmaDec *p, const uint8_t *buf, size_t
}
}
}
}
NORMALIZE_CHECK;
return res;
}
static void LzmaDec_InitRc(CLzmaDec *p, const uint8_t *data)
{
p->code = ((uint32_t)data[1] << 24) | ((uint32_t)data[2] << 16) | ((uint32_t)data[3] << 8) | ((uint32_t)data[4]);
p->code = (
(uint32_t)data[1] << 24) |
((uint32_t)data[2] << 16) |
((uint32_t)data[3] << 8) |
((uint32_t)data[4]);
p->range = 0xFFFFFFFF;
p->needFlush = 0;
}
void LzmaDec_InitDicAndState(CLzmaDec *p, bool initDic, bool initState);
void LzmaDec_InitDicAndState(CLzmaDec *p, bool initDic, bool initState)
{
p->needFlush = 1;
@ -747,8 +752,9 @@ void LzmaDec_Init(CLzmaDec *p)
static void LzmaDec_InitStateReal(CLzmaDec *p)
{
uint32_t numProbs = Literal + ((uint32_t)LZMA_LIT_SIZE << (p->prop.lc + p->prop.lp));
uint32_t i;
uint32_t numProbs = Literal +
((uint32_t)LZMA_LIT_SIZE << (p->prop.lc + p->prop.lp));
uint16_t *probs = p->probs;
for (i = 0; i < numProbs; i++)
probs[i] = kBitModelTotal >> 1;
@ -757,7 +763,8 @@ static void LzmaDec_InitStateReal(CLzmaDec *p)
p->needInitState = 0;
}
SRes LzmaDec_DecodeToDic(CLzmaDec *p, size_t dicLimit, const uint8_t *src, size_t *srcLen,
SRes LzmaDec_DecodeToDic(CLzmaDec *p, size_t dicLimit,
const uint8_t *src, size_t *srcLen,
ELzmaFinishMode finishMode, ELzmaStatus *status)
{
size_t inSize = *srcLen;
@ -772,7 +779,8 @@ SRes LzmaDec_DecodeToDic(CLzmaDec *p, size_t dicLimit, const uint8_t *src, size_
if (p->needFlush != 0)
{
for (; inSize > 0 && p->tempBufSize < RC_INIT_SIZE; (*srcLen)++, inSize--)
for (; inSize > 0 && p->tempBufSize < RC_INIT_SIZE;
(*srcLen)++, inSize--)
p->tempBuf[p->tempBufSize++] = *src++;
if (p->tempBufSize < RC_INIT_SIZE)
{
@ -883,11 +891,12 @@ SRes LzmaDec_DecodeToBuf(CLzmaDec *p, uint8_t *dest, size_t *destLen, const uint
size_t outSize = *destLen;
size_t inSize = *srcLen;
*srcLen = *destLen = 0;
for (;;)
{
size_t inSizeCur = inSize, outSizeCur, dicPos;
ELzmaFinishMode curFinishMode;
SRes res;
ELzmaFinishMode curFinishMode;
size_t inSizeCur = inSize, outSizeCur, dicPos;
if (p->dicPos == p->dicBufSize)
p->dicPos = 0;
dicPos = p->dicPos;
@ -944,7 +953,10 @@ SRes LzmaProps_Decode(CLzmaProps *p, const uint8_t *data, unsigned size)
if (size < LZMA_PROPS_SIZE)
return SZ_ERROR_UNSUPPORTED;
else
dicSize = data[1] | ((uint32_t)data[2] << 8) | ((uint32_t)data[3] << 16) | ((uint32_t)data[4] << 24);
dicSize = data[1] |
((uint32_t)data[2] << 8) |
((uint32_t)data[3] << 16) |
((uint32_t)data[4] << 24);
if (dicSize < LZMA_DIC_MIN)
dicSize = LZMA_DIC_MIN;
@ -962,7 +974,8 @@ SRes LzmaProps_Decode(CLzmaProps *p, const uint8_t *data, unsigned size)
return SZ_OK;
}
static SRes LzmaDec_AllocateProbs2(CLzmaDec *p, const CLzmaProps *propNew, ISzAlloc *alloc)
static SRes LzmaDec_AllocateProbs2(
CLzmaDec *p, const CLzmaProps *propNew, ISzAlloc *alloc)
{
uint32_t numProbs = LzmaProps_GetNumProbs(propNew);
if (p->probs == 0 || numProbs != p->numProbs)
@ -976,22 +989,37 @@ static SRes LzmaDec_AllocateProbs2(CLzmaDec *p, const CLzmaProps *propNew, ISzAl
return SZ_OK;
}
SRes LzmaDec_AllocateProbs(CLzmaDec *p, const uint8_t *props, unsigned propsSize, ISzAlloc *alloc)
SRes LzmaDec_AllocateProbs(CLzmaDec *p,
const uint8_t *props, unsigned propsSize, ISzAlloc *alloc)
{
CLzmaProps propNew;
RINOK(LzmaProps_Decode(&propNew, props, propsSize));
RINOK(LzmaDec_AllocateProbs2(p, &propNew, alloc));
int result = LzmaProps_Decode(&propNew, props, propsSize);
if (result != 0)
return result;
result = LzmaDec_AllocateProbs2(p, &propNew, alloc);
if (result != 0)
return result;
p->prop = propNew;
return SZ_OK;
}
SRes LzmaDec_Allocate(CLzmaDec *p, const uint8_t *props, unsigned propsSize, ISzAlloc *alloc)
SRes LzmaDec_Allocate(CLzmaDec *p, const uint8_t *props,
unsigned propsSize, ISzAlloc *alloc)
{
CLzmaProps propNew;
size_t dicBufSize;
RINOK(LzmaProps_Decode(&propNew, props, propsSize));
RINOK(LzmaDec_AllocateProbs2(p, &propNew, alloc));
int result = LzmaProps_Decode(&propNew, props, propsSize);
if (result != 0)
return result;
result = LzmaDec_AllocateProbs2(p, &propNew, alloc);
if (result != 0)
return result;
dicBufSize = propNew.dicSize;
if (p->dic == 0 || dicBufSize != p->dicBufSize)
{
LzmaDec_FreeDict(p, alloc);
@ -1007,7 +1035,8 @@ SRes LzmaDec_Allocate(CLzmaDec *p, const uint8_t *props, unsigned propsSize, ISz
return SZ_OK;
}
SRes LzmaDecode(uint8_t *dest, size_t *destLen, const uint8_t *src, size_t *srcLen,
SRes LzmaDecode(uint8_t *dest, size_t *destLen,
const uint8_t *src, size_t *srcLen,
const uint8_t *propData, unsigned propSize, ELzmaFinishMode finishMode,
ELzmaStatus *status, ISzAlloc *alloc)
{

198
deps/7zip/LzmaEnc.c vendored
View File

@ -48,37 +48,56 @@ void LzmaEncProps_Init(CLzmaEncProps *p)
void LzmaEncProps_Normalize(CLzmaEncProps *p)
{
int level = p->level;
if (level < 0) level = 5;
if (level < 0)
level = 5;
p->level = level;
if (p->dictSize == 0) p->dictSize = (level <= 5 ? (1 << (level * 2 + 14)) : (level == 6 ? (1 << 25) : (1 << 26)));
if (p->dictSize == 0)
p->dictSize = (level <= 5 ? (1 << (level * 2 + 14)) : (level == 6 ? (1 << 25) : (1 << 26)));
if (p->dictSize > p->reduceSize)
{
unsigned i;
for (i = 11; i <= 30; i++)
{
if ((uint32_t)p->reduceSize <= ((uint32_t)2 << i)) { p->dictSize = ((uint32_t)2 << i); break; }
if ((uint32_t)p->reduceSize <= ((uint32_t)3 << i)) { p->dictSize = ((uint32_t)3 << i); break; }
if ((uint32_t)p->reduceSize <= ((uint32_t)2 << i))
{
p->dictSize = ((uint32_t)2 << i);
break;
}
if ((uint32_t)p->reduceSize <= ((uint32_t)3 << i))
{
p->dictSize = ((uint32_t)3 << i);
break;
}
}
}
if (p->lc < 0) p->lc = 3;
if (p->lp < 0) p->lp = 0;
if (p->pb < 0) p->pb = 2;
if (p->lc < 0)
p->lc = 3;
if (p->lp < 0)
p->lp = 0;
if (p->pb < 0)
p->pb = 2;
if (p->algo < 0) p->algo = (level < 5 ? 0 : 1);
if (p->fb < 0) p->fb = (level < 7 ? 32 : 64);
if (p->btMode < 0) p->btMode = (p->algo == 0 ? 0 : 1);
if (p->numHashBytes < 0) p->numHashBytes = 4;
if (p->mc == 0) p->mc = (16 + (p->fb >> 1)) >> (p->btMode ? 0 : 1);
if (p->algo < 0)
p->algo = (level < 5 ? 0 : 1);
if (p->fb < 0)
p->fb = (level < 7 ? 32 : 64);
if (p->btMode < 0)
p->btMode = (p->algo == 0 ? 0 : 1);
if (p->numHashBytes < 0)
p->numHashBytes = 4;
if (p->mc == 0)
p->mc = (16 + (p->fb >> 1)) >> (p->btMode ? 0 : 1);
if (p->numThreads < 0)
p->numThreads =
#ifndef _7ZIP_ST
((p->btMode && p->algo) ? 2 : 1);
#else
1;
#endif
{
#ifndef _7ZIP_ST
p->numThreads = ((p->btMode && p->algo) ? 2 : 1);
#else
p->numThreads = 1;
#endif
}
}
uint32_t LzmaEncProps_GetDictSize(const CLzmaEncProps *props2)
@ -102,8 +121,8 @@ static void LzmaEnc_FastPosInit(unsigned char *g_FastPos)
for (slot = 2; slot < kNumLogBits * 2; slot++)
{
size_t k = ((size_t)1 << ((slot >> 1) - 1));
size_t j;
size_t k = ((size_t)1 << ((slot >> 1) - 1));
for (j = 0; j < k; j++)
g_FastPos[j] = (unsigned char)slot;
g_FastPos += k;
@ -283,16 +302,16 @@ typedef struct
CRangeEnc rc;
#ifndef _7ZIP_ST
#ifndef _7ZIP_ST
bool mtMode;
CMatchFinderMt matchFinderMt;
#endif
#endif
CMatchFinder matchFinderBase;
#ifndef _7ZIP_ST
#ifndef _7ZIP_ST
unsigned char pad[128];
#endif
#endif
COptimal opt[kNumOpts];
@ -321,17 +340,17 @@ typedef struct
CSaveState saveState;
#ifndef _7ZIP_ST
#ifndef _7ZIP_ST
unsigned char pad2[128];
#endif
#endif
} CLzmaEnc;
void LzmaEnc_SaveState(CLzmaEncHandle pp)
{
int i;
CLzmaEnc *p = (CLzmaEnc *)pp;
CSaveState *dest = &p->saveState;
int i;
dest->lenEnc = p->lenEnc;
dest->repLenEnc = p->repLenEnc;
dest->state = p->state;
@ -355,9 +374,9 @@ void LzmaEnc_SaveState(CLzmaEncHandle pp)
void LzmaEnc_RestoreState(CLzmaEncHandle pp)
{
int i;
CLzmaEnc *dest = (CLzmaEnc *)pp;
const CSaveState *p = &dest->saveState;
int i;
dest->lenEnc = p->lenEnc;
dest->repLenEnc = p->repLenEnc;
dest->state = p->state;
@ -383,9 +402,11 @@ SRes LzmaEnc_SetProps(CLzmaEncHandle pp, const CLzmaEncProps *props2)
{
CLzmaEnc *p = (CLzmaEnc *)pp;
CLzmaEncProps props = *props2;
LzmaEncProps_Normalize(&props);
if (props.lc > LZMA_LC_MAX
if (
props.lc > LZMA_LC_MAX
|| props.lp > LZMA_LP_MAX
|| props.pb > LZMA_PB_MAX
|| props.dictSize > ((uint64_t)1 << kDicLogSizeMaxCompress)
@ -422,16 +443,16 @@ SRes LzmaEnc_SetProps(CLzmaEncHandle pp, const CLzmaEncProps *props2)
p->writeEndMark = props.writeEndMark;
#ifndef _7ZIP_ST
/*
#ifndef _7ZIP_ST
#if 0
if (newMultiThread != _multiThread)
{
ReleaseMatchFinder();
_multiThread = newMultiThread;
}
*/
#endif
p->multiThread = (props.numThreads > 1);
#endif
#endif
return SZ_OK;
}
@ -528,7 +549,8 @@ static void RangeEnc_FlushData(CRangeEnc *p)
RangeEnc_ShiftLow(p);
}
static void RangeEnc_EncodeDirectBits(CRangeEnc *p, uint32_t value, unsigned numBits)
static void RangeEnc_EncodeDirectBits(
CRangeEnc *p, uint32_t value, unsigned numBits)
{
do
{
@ -627,7 +649,9 @@ static void LzmaEnc_InitPriceTables(uint32_t *ProbPrices)
#define GET_PRICE_0a(prob) ProbPrices[(prob) >> kNumMoveReducingBits]
#define GET_PRICE_1a(prob) ProbPrices[((prob) ^ (kBitModelTotal - 1)) >> kNumMoveReducingBits]
static uint32_t LitEnc_GetPrice(const CLzmaProb *probs, uint32_t symbol, const uint32_t *ProbPrices)
static uint32_t LitEnc_GetPrice(
const CLzmaProb *probs, uint32_t symbol,
const uint32_t *ProbPrices)
{
uint32_t price = 0;
symbol |= 0x100;
@ -635,8 +659,7 @@ static uint32_t LitEnc_GetPrice(const CLzmaProb *probs, uint32_t symbol, const u
{
price += GET_PRICEa(probs[symbol >> 8], (symbol >> 7) & 1);
symbol <<= 1;
}
while (symbol < 0x10000);
}while (symbol < 0x10000);
return price;
}
@ -651,13 +674,13 @@ static uint32_t LitEnc_GetPriceMatched(const CLzmaProb *probs, uint32_t symbol,
price += GET_PRICEa(probs[offs + (matchByte & offs) + (symbol >> 8)], (symbol >> 7) & 1);
symbol <<= 1;
offs &= ~(matchByte ^ symbol);
}
while (symbol < 0x10000);
}while (symbol < 0x10000);
return price;
}
static void RcTree_Encode(CRangeEnc *rc, CLzmaProb *probs, int numBitLevels, uint32_t symbol)
static void RcTree_Encode(CRangeEnc *rc, CLzmaProb *probs,
int numBitLevels, uint32_t symbol)
{
uint32_t m = 1;
int i;
@ -671,7 +694,8 @@ static void RcTree_Encode(CRangeEnc *rc, CLzmaProb *probs, int numBitLevels, uin
}
}
static void RcTree_ReverseEncode(CRangeEnc *rc, CLzmaProb *probs, int numBitLevels, uint32_t symbol)
static void RcTree_ReverseEncode(CRangeEnc *rc, CLzmaProb *probs,
int numBitLevels, uint32_t symbol)
{
uint32_t m = 1;
int i;
@ -684,7 +708,8 @@ static void RcTree_ReverseEncode(CRangeEnc *rc, CLzmaProb *probs, int numBitLeve
}
}
static uint32_t RcTree_GetPrice(const CLzmaProb *probs, int numBitLevels, uint32_t symbol, const uint32_t *ProbPrices)
static uint32_t RcTree_GetPrice(const CLzmaProb *probs, int numBitLevels,
uint32_t symbol, const uint32_t *ProbPrices)
{
uint32_t price = 0;
symbol |= (1 << numBitLevels);
@ -711,7 +736,6 @@ static uint32_t RcTree_ReverseGetPrice(const CLzmaProb *probs, int numBitLevels,
return price;
}
static void LenEnc_Init(CLenEnc *p)
{
unsigned i;
@ -1715,6 +1739,7 @@ void LzmaEnc_Destroy(CLzmaEncHandle p, ISzAlloc *alloc, ISzAlloc *allocBig)
static SRes LzmaEnc_CodeOneBlock(CLzmaEnc *p, bool useLimits, uint32_t maxPackSize, uint32_t maxUnpackSize)
{
int result;
uint32_t nowPos32, startPos32;
if (p->needInit)
{
@ -1724,7 +1749,10 @@ static SRes LzmaEnc_CodeOneBlock(CLzmaEnc *p, bool useLimits, uint32_t maxPackSi
if (p->finished)
return p->result;
RINOK(CheckErrors(p));
result = CheckErrors(p);
if (result != 0)
return result;
nowPos32 = (uint32_t)p->nowPos64;
startPos32 = nowPos32;
@ -1904,17 +1932,23 @@ static SRes LzmaEnc_Alloc(CLzmaEnc *p, uint32_t keepWindowSize, ISzAlloc *alloc,
if (beforeSize + p->dictSize < keepWindowSize)
beforeSize = keepWindowSize - p->dictSize;
#ifndef _7ZIP_ST
#ifndef _7ZIP_ST
if (p->mtMode)
{
RINOK(MatchFinderMt_Create(&p->matchFinderMt, p->dictSize, beforeSize, p->numFastBytes, LZMA_MATCH_LEN_MAX, allocBig));
int result = MatchFinderMt_Create(&p->matchFinderMt,
p->dictSize, beforeSize, p->numFastBytes,
LZMA_MATCH_LEN_MAX, allocBig);
if (result != 0)
return result;
p->matchFinderObj = &p->matchFinderMt;
MatchFinderMt_CreateVTable(&p->matchFinderMt, &p->matchFinder);
}
else
#endif
#endif
{
if (!MatchFinder_Create(&p->matchFinderBase, p->dictSize, beforeSize, p->numFastBytes, LZMA_MATCH_LEN_MAX, allocBig))
if (!MatchFinder_Create(&p->matchFinderBase,
p->dictSize, beforeSize, p->numFastBytes,
LZMA_MATCH_LEN_MAX, allocBig))
return SZ_ERROR_MEM;
p->matchFinderObj = &p->matchFinderBase;
MatchFinder_CreateVTable(&p->matchFinderBase, &p->matchFinder);
@ -1932,7 +1966,6 @@ void LzmaEnc_Init(CLzmaEnc *p)
RangeEnc_Init(&p->rc);
for (i = 0; i < kNumStates; i++)
{
uint32_t j;
@ -1997,9 +2030,11 @@ void LzmaEnc_InitPrices(CLzmaEnc *p)
LenPriceEnc_UpdateTables(&p->repLenEnc, 1 << p->pb, p->ProbPrices);
}
static SRes LzmaEnc_AllocAndInit(CLzmaEnc *p, uint32_t keepWindowSize, ISzAlloc *alloc, ISzAlloc *allocBig)
static SRes LzmaEnc_AllocAndInit(CLzmaEnc *p,
uint32_t keepWindowSize, ISzAlloc *alloc, ISzAlloc *allocBig)
{
uint32_t i;
int result;
for (i = 0; i < (uint32_t)kDicLogSizeMaxCompress; i++)
if (p->dictSize <= ((uint32_t)1 << i))
break;
@ -2007,14 +2042,20 @@ static SRes LzmaEnc_AllocAndInit(CLzmaEnc *p, uint32_t keepWindowSize, ISzAlloc
p->finished = false;
p->result = SZ_OK;
RINOK(LzmaEnc_Alloc(p, keepWindowSize, alloc, allocBig));
result = LzmaEnc_Alloc(p, keepWindowSize, alloc, allocBig);
if (result != 0)
return result;
LzmaEnc_Init(p);
LzmaEnc_InitPrices(p);
p->nowPos64 = 0;
return SZ_OK;
}
static SRes LzmaEnc_Prepare(CLzmaEncHandle pp, ISeqOutStream *outStream, ISeqInStream *inStream,
static SRes LzmaEnc_Prepare(CLzmaEncHandle pp,
ISeqOutStream *outStream, ISeqInStream *inStream,
ISzAlloc *alloc, ISzAlloc *allocBig)
{
CLzmaEnc *p = (CLzmaEnc *)pp;
@ -2034,14 +2075,16 @@ SRes LzmaEnc_PrepareForLzma2(CLzmaEncHandle pp,
return LzmaEnc_AllocAndInit(p, keepWindowSize, alloc, allocBig);
}
static void LzmaEnc_SetInputBuf(CLzmaEnc *p, const unsigned char *src, size_t srcLen)
static void LzmaEnc_SetInputBuf(CLzmaEnc *p,
const unsigned char *src, size_t srcLen)
{
p->matchFinderBase.directInput = 1;
p->matchFinderBase.bufferBase = (unsigned char *)src;
p->matchFinderBase.directInputRem = srcLen;
}
SRes LzmaEnc_MemPrepare(CLzmaEncHandle pp, const unsigned char *src, size_t srcLen,
SRes LzmaEnc_MemPrepare(CLzmaEncHandle pp,
const unsigned char *src, size_t srcLen,
uint32_t keepWindowSize, ISzAlloc *alloc, ISzAlloc *allocBig)
{
CLzmaEnc *p = (CLzmaEnc *)pp;
@ -2060,7 +2103,6 @@ void LzmaEnc_Finish(CLzmaEncHandle pp)
#endif
}
typedef struct
{
ISeqOutStream funcTable;
@ -2122,7 +2164,8 @@ SRes LzmaEnc_CodeOneMemBlock(CLzmaEncHandle pp, bool reInit,
RangeEnc_Init(&p->rc);
p->rc.outStream = &outStream.funcTable;
res = LzmaEnc_CodeOneBlock(p, true, desiredPackSize, *unpackSize);
res = LzmaEnc_CodeOneBlock(p, true,
desiredPackSize, *unpackSize);
*unpackSize = (uint32_t)(p->nowPos64 - nowPos64);
*destLen -= outStream.rem;
@ -2136,12 +2179,11 @@ SRes LzmaEnc_CodeOneMemBlock(CLzmaEncHandle pp, bool reInit,
static SRes LzmaEnc_Encode2(CLzmaEnc *p, ICompressProgress *progress)
{
SRes res = SZ_OK;
#ifndef _7ZIP_ST
#ifndef _7ZIP_ST
unsigned char allocaDummy[0x300];
allocaDummy[0] = 0;
allocaDummy[1] = allocaDummy[0];
#endif
#endif
for (;;)
{
@ -2150,7 +2192,9 @@ static SRes LzmaEnc_Encode2(CLzmaEnc *p, ICompressProgress *progress)
break;
if (progress)
{
res = progress->Progress(progress, p->nowPos64, RangeEnc_GetProcessed(&p->rc));
res = progress->Progress(
progress, p->nowPos64, RangeEnc_GetProcessed(&p->rc));
if (res != SZ_OK)
{
res = SZ_ERROR_PROGRESS;
@ -2165,21 +2209,28 @@ static SRes LzmaEnc_Encode2(CLzmaEnc *p, ICompressProgress *progress)
}
SRes LzmaEnc_Encode(CLzmaEncHandle pp, ISeqOutStream *outStream, ISeqInStream *inStream, ICompressProgress *progress,
SRes LzmaEnc_Encode(CLzmaEncHandle pp,
ISeqOutStream *outStream,
ISeqInStream *inStream, ICompressProgress *progress,
ISzAlloc *alloc, ISzAlloc *allocBig)
{
RINOK(LzmaEnc_Prepare(pp, outStream, inStream, alloc, allocBig));
int result = LzmaEnc_Prepare(pp, outStream, inStream, alloc, allocBig);
if (result != 0)
return result;
return LzmaEnc_Encode2((CLzmaEnc *)pp, progress);
}
SRes LzmaEnc_WriteProperties(CLzmaEncHandle pp, unsigned char *props, size_t *size)
SRes LzmaEnc_WriteProperties(
CLzmaEncHandle pp, unsigned char *props, size_t *size)
{
CLzmaEnc *p = (CLzmaEnc *)pp;
unsigned i;
CLzmaEnc *p = (CLzmaEnc *)pp;
uint32_t dictSize = p->dictSize;
if (*size < LZMA_PROPS_SIZE)
return SZ_ERROR_PARAM;
*size = LZMA_PROPS_SIZE;
props[0] = (unsigned char)((p->pb * 5 + p->lp) * 9 + p->lc);
@ -2201,13 +2252,15 @@ SRes LzmaEnc_WriteProperties(CLzmaEncHandle pp, unsigned char *props, size_t *si
}
SRes LzmaEnc_MemEncode(CLzmaEncHandle pp, unsigned char *dest, size_t *destLen, const unsigned char *src, size_t srcLen,
int writeEndMark, ICompressProgress *progress, ISzAlloc *alloc, ISzAlloc *allocBig)
SRes LzmaEnc_MemEncode(CLzmaEncHandle pp,
unsigned char *dest, size_t *destLen,
const unsigned char *src, size_t srcLen,
int writeEndMark, ICompressProgress *progress,
ISzAlloc *alloc, ISzAlloc *allocBig)
{
SRes res;
CLzmaEnc *p = (CLzmaEnc *)pp;
CSeqOutStreamBuf outStream;
CLzmaEnc *p = (CLzmaEnc *)pp;
outStream.funcTable.Write = MyWrite;
outStream.data = dest;
@ -2217,7 +2270,8 @@ SRes LzmaEnc_MemEncode(CLzmaEncHandle pp, unsigned char *dest, size_t *destLen,
p->writeEndMark = writeEndMark;
p->rc.outStream = &outStream.funcTable;
res = LzmaEnc_MemPrepare(pp, src, srcLen, 0, alloc, allocBig);
res = LzmaEnc_MemPrepare(
pp, src, srcLen, 0, alloc, allocBig);
if (res == SZ_OK)
{
@ -2233,12 +2287,14 @@ SRes LzmaEnc_MemEncode(CLzmaEncHandle pp, unsigned char *dest, size_t *destLen,
}
SRes LzmaEncode(unsigned char *dest, size_t *destLen, const unsigned char *src, size_t srcLen,
const CLzmaEncProps *props, unsigned char *propsEncoded, size_t *propsSize, int writeEndMark,
SRes LzmaEncode(unsigned char *dest, size_t *destLen,
const unsigned char *src, size_t srcLen,
const CLzmaEncProps *props,
unsigned char *propsEncoded, size_t *propsSize, int writeEndMark,
ICompressProgress *progress, ISzAlloc *alloc, ISzAlloc *allocBig)
{
CLzmaEnc *p = (CLzmaEnc *)LzmaEnc_Create(alloc);
SRes res;
CLzmaEnc *p = (CLzmaEnc *)LzmaEnc_Create(alloc);
if (!p)
return SZ_ERROR_MEM;