mirror of
https://github.com/Xevion/easy7zip.git
synced 2025-12-14 10:11:38 -06:00
4.23
This commit is contained in:
committed by
Kornel Lesiński
parent
3c510ba80b
commit
ac2b563958
@@ -1,7 +1,9 @@
|
||||
/*
|
||||
LzmaTest.c
|
||||
Test application for LZMA Decoder
|
||||
LZMA SDK 4.16 Copyright (c) 1999-2004 Igor Pavlov (2005-03-18)
|
||||
|
||||
This file written and distributed to public domain by Igor Pavlov.
|
||||
This file is part of LZMA SDK 4.22 (2005-06-10)
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
@@ -10,231 +12,314 @@ LZMA SDK 4.16 Copyright (c) 1999-2004 Igor Pavlov (2005-03-18)
|
||||
|
||||
#include "LzmaDecode.h"
|
||||
|
||||
const char *kCantReadMessage = "Can not read input file";
|
||||
const char *kCantWriteMessage = "Can not write output file";
|
||||
const char *kCantAllocateMessage = "Can not allocate memory";
|
||||
|
||||
size_t MyReadFile(FILE *file, void *data, size_t size)
|
||||
{
|
||||
return (fread(data, 1, size, file) == size);
|
||||
}
|
||||
{ return fread(data, 1, size, file); }
|
||||
|
||||
int MyReadFileAndCheck(FILE *file, void *data, size_t size)
|
||||
{ return (MyReadFile(file, data, size) == size);}
|
||||
|
||||
size_t MyWriteFile(FILE *file, const void *data, size_t size)
|
||||
{ return fwrite(data, 1, size, file); }
|
||||
|
||||
int MyWriteFileAndCheck(FILE *file, const void *data, size_t size)
|
||||
{ return (MyWriteFile(file, data, size) == size); }
|
||||
|
||||
#ifdef _LZMA_IN_CB
|
||||
#define kInBufferSize (1 << 15)
|
||||
typedef struct _CBuffer
|
||||
{
|
||||
ILzmaInCallback InCallback;
|
||||
unsigned char *Buffer;
|
||||
unsigned int Size;
|
||||
FILE *File;
|
||||
unsigned char Buffer[kInBufferSize];
|
||||
} CBuffer;
|
||||
|
||||
int LzmaReadCompressed(void *object, unsigned char **buffer, unsigned int *size)
|
||||
int LzmaReadCompressed(void *object, const unsigned char **buffer, SizeT *size)
|
||||
{
|
||||
CBuffer *bo = (CBuffer *)object;
|
||||
*size = bo->Size; /* You can specify any available size here */
|
||||
*buffer = bo->Buffer;
|
||||
bo->Buffer += *size;
|
||||
bo->Size -= *size;
|
||||
CBuffer *b = (CBuffer *)object;
|
||||
*buffer = b->Buffer;
|
||||
*size = (SizeT)MyReadFile(b->File, b->Buffer, kInBufferSize);
|
||||
return LZMA_RESULT_OK;
|
||||
}
|
||||
CBuffer g_InBuffer;
|
||||
|
||||
#endif
|
||||
|
||||
int main2(int numargs, const char *args[], char *rs)
|
||||
#ifdef _LZMA_OUT_READ
|
||||
#define kOutBufferSize (1 << 15)
|
||||
unsigned char g_OutBuffer[kOutBufferSize];
|
||||
#endif
|
||||
|
||||
int PrintError(char *buffer, const char *message)
|
||||
{
|
||||
FILE *inputHandle, *outputHandle;
|
||||
unsigned int length, processedSize;
|
||||
unsigned int compressedSize, outSize, outSizeProcessed, lzmaInternalSize;
|
||||
void *inStream, *outStream, *lzmaInternalData;
|
||||
unsigned char properties[5];
|
||||
unsigned char prop0;
|
||||
int ii;
|
||||
int lc, lp, pb;
|
||||
int res;
|
||||
#ifdef _LZMA_IN_CB
|
||||
CBuffer bo;
|
||||
sprintf(buffer + strlen(buffer), "\nError: ");
|
||||
sprintf(buffer + strlen(buffer), message);
|
||||
return 1;
|
||||
}
|
||||
|
||||
int main3(FILE *inFile, FILE *outFile, char *rs)
|
||||
{
|
||||
/* We use two 32-bit integers to construct 64-bit integer for file size.
|
||||
You can remove outSizeHigh, if you don't need >= 4GB supporting,
|
||||
or you can use UInt64 outSize, if your compiler supports 64-bit integers*/
|
||||
UInt32 outSize = 0;
|
||||
UInt32 outSizeHigh = 0;
|
||||
#ifndef _LZMA_OUT_READ
|
||||
SizeT outSizeFull;
|
||||
unsigned char *outStream;
|
||||
#endif
|
||||
|
||||
sprintf(rs + strlen(rs), "\nLZMA Decoder 4.16 Copyright (c) 1999-2005 Igor Pavlov 2005-03-18\n");
|
||||
if (numargs < 2 || numargs > 3)
|
||||
{
|
||||
sprintf(rs + strlen(rs), "\nUsage: lzmaDec file.lzma [outFile]\n");
|
||||
return 1;
|
||||
}
|
||||
|
||||
inputHandle = fopen(args[1], "rb");
|
||||
if (inputHandle == 0)
|
||||
{
|
||||
sprintf(rs + strlen(rs), "\n Open input file error");
|
||||
return 1;
|
||||
}
|
||||
|
||||
fseek(inputHandle, 0, SEEK_END);
|
||||
length = ftell(inputHandle);
|
||||
fseek(inputHandle, 0, SEEK_SET);
|
||||
|
||||
if (!MyReadFile(inputHandle, properties, sizeof(properties)))
|
||||
return 1;
|
||||
|
||||
outSize = 0;
|
||||
for (ii = 0; ii < 4; ii++)
|
||||
{
|
||||
unsigned char b;
|
||||
if (!MyReadFile(inputHandle, &b, sizeof(b)))
|
||||
return 1;
|
||||
outSize += (unsigned int)(b) << (ii * 8);
|
||||
}
|
||||
int waitEOS = 1;
|
||||
/* waitEOS = 1, if there is no uncompressed size in headers,
|
||||
so decoder will wait EOS (End of Stream Marker) in compressed stream */
|
||||
|
||||
if (outSize == 0xFFFFFFFF)
|
||||
{
|
||||
sprintf(rs + strlen(rs), "\nstream version is not supported");
|
||||
return 1;
|
||||
}
|
||||
|
||||
for (ii = 0; ii < 4; ii++)
|
||||
{
|
||||
unsigned char b;
|
||||
if (!MyReadFile(inputHandle, &b, sizeof(b)))
|
||||
return 1;
|
||||
if (b != 0)
|
||||
{
|
||||
sprintf(rs + strlen(rs), "\n too long file");
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
|
||||
compressedSize = length - 13;
|
||||
inStream = malloc(compressedSize);
|
||||
if (inStream == 0)
|
||||
{
|
||||
sprintf(rs + strlen(rs), "\n can't allocate");
|
||||
return 1;
|
||||
}
|
||||
if (!MyReadFile(inputHandle, inStream, compressedSize))
|
||||
{
|
||||
sprintf(rs + strlen(rs), "\n can't read");
|
||||
return 1;
|
||||
}
|
||||
|
||||
fclose(inputHandle);
|
||||
|
||||
prop0 = properties[0];
|
||||
if (prop0 >= (9*5*5))
|
||||
{
|
||||
sprintf(rs + strlen(rs), "\n Properties error");
|
||||
return 1;
|
||||
}
|
||||
for (pb = 0; prop0 >= (9 * 5);
|
||||
pb++, prop0 -= (9 * 5));
|
||||
for (lp = 0; prop0 >= 9;
|
||||
lp++, prop0 -= 9);
|
||||
lc = prop0;
|
||||
|
||||
lzmaInternalSize =
|
||||
(LZMA_BASE_SIZE + (LZMA_LIT_SIZE << (lc + lp)))* sizeof(CProb);
|
||||
|
||||
#ifdef _LZMA_OUT_READ
|
||||
lzmaInternalSize += 100;
|
||||
#ifndef _LZMA_IN_CB
|
||||
SizeT compressedSize;
|
||||
unsigned char *inStream;
|
||||
#endif
|
||||
|
||||
outStream = malloc(outSize);
|
||||
lzmaInternalData = malloc(lzmaInternalSize);
|
||||
if (outStream == 0 || lzmaInternalData == 0)
|
||||
{
|
||||
sprintf(rs + strlen(rs), "\n can't allocate");
|
||||
return 1;
|
||||
}
|
||||
CLzmaDecoderState state; /* it's about 24-80 bytes structure, if int is 32-bit */
|
||||
unsigned char properties[LZMA_PROPERTIES_SIZE];
|
||||
|
||||
int res;
|
||||
|
||||
#ifdef _LZMA_IN_CB
|
||||
bo.InCallback.Read = LzmaReadCompressed;
|
||||
bo.Buffer = (unsigned char *)inStream;
|
||||
bo.Size = compressedSize;
|
||||
g_InBuffer.File = inFile;
|
||||
#endif
|
||||
|
||||
if (sizeof(UInt32) < 4)
|
||||
return PrintError(rs, "LZMA decoder needs correct UInt32");
|
||||
|
||||
#ifndef _LZMA_IN_CB
|
||||
{
|
||||
long length;
|
||||
fseek(inFile, 0, SEEK_END);
|
||||
length = ftell(inFile);
|
||||
fseek(inFile, 0, SEEK_SET);
|
||||
if ((long)(SizeT)length != length)
|
||||
return PrintError(rs, "Too big compressed stream");
|
||||
compressedSize = (SizeT)(length - (LZMA_PROPERTIES_SIZE + 8));
|
||||
}
|
||||
#endif
|
||||
|
||||
/* Read LZMA properties for compressed stream */
|
||||
|
||||
if (!MyReadFileAndCheck(inFile, properties, sizeof(properties)))
|
||||
return PrintError(rs, kCantReadMessage);
|
||||
|
||||
/* Read uncompressed size */
|
||||
|
||||
{
|
||||
int i;
|
||||
for (i = 0; i < 8; i++)
|
||||
{
|
||||
unsigned char b;
|
||||
if (!MyReadFileAndCheck(inFile, &b, 1))
|
||||
return PrintError(rs, kCantReadMessage);
|
||||
if (b != 0xFF)
|
||||
waitEOS = 0;
|
||||
if (i < 4)
|
||||
outSize += (UInt32)(b) << (i * 8);
|
||||
else
|
||||
outSizeHigh += (UInt32)(b) << ((i - 4) * 8);
|
||||
}
|
||||
|
||||
#ifndef _LZMA_OUT_READ
|
||||
if (waitEOS)
|
||||
return PrintError(rs, "Stream with EOS marker is not supported");
|
||||
outSizeFull = (SizeT)outSize;
|
||||
if (sizeof(SizeT) >= 8)
|
||||
outSizeFull |= (((SizeT)outSizeHigh << 16) << 16);
|
||||
else if (outSizeHigh != 0 || (UInt32)(SizeT)outSize != outSize)
|
||||
return PrintError(rs, "Too big uncompressed stream");
|
||||
#endif
|
||||
}
|
||||
|
||||
/* Decode LZMA properties and allocate memory */
|
||||
|
||||
if (LzmaDecodeProperties(&state.Properties, properties, LZMA_PROPERTIES_SIZE) != LZMA_RESULT_OK)
|
||||
return PrintError(rs, "Incorrect stream properties");
|
||||
state.Probs = (CProb *)malloc(LzmaGetNumProbs(&state.Properties) * sizeof(CProb));
|
||||
|
||||
#ifdef _LZMA_OUT_READ
|
||||
state.Dictionary = (unsigned char *)malloc(state.Properties.DictionarySize);
|
||||
#else
|
||||
outStream = (unsigned char *)malloc(outSizeFull);
|
||||
#endif
|
||||
|
||||
#ifndef _LZMA_IN_CB
|
||||
inStream = (unsigned char *)malloc(compressedSize);
|
||||
#endif
|
||||
|
||||
if (state.Probs == 0
|
||||
#ifdef _LZMA_OUT_READ
|
||||
|| state.Dictionary == 0
|
||||
#else
|
||||
|| outStream == 0
|
||||
#endif
|
||||
#ifndef _LZMA_IN_CB
|
||||
|| inStream == 0
|
||||
#endif
|
||||
)
|
||||
{
|
||||
free(state.Probs);
|
||||
#ifdef _LZMA_OUT_READ
|
||||
free(state.Dictionary);
|
||||
#else
|
||||
free(outStream);
|
||||
#endif
|
||||
#ifndef _LZMA_IN_CB
|
||||
free(inStream);
|
||||
#endif
|
||||
return PrintError(rs, kCantAllocateMessage);
|
||||
}
|
||||
|
||||
/* Decompress */
|
||||
|
||||
#ifdef _LZMA_IN_CB
|
||||
g_InBuffer.InCallback.Read = LzmaReadCompressed;
|
||||
#else
|
||||
if (!MyReadFileAndCheck(inFile, inStream, compressedSize))
|
||||
return PrintError(rs, kCantReadMessage);
|
||||
#endif
|
||||
|
||||
#ifdef _LZMA_OUT_READ
|
||||
{
|
||||
UInt32 nowPos;
|
||||
unsigned char *dictionary;
|
||||
UInt32 dictionarySize = 0;
|
||||
int i;
|
||||
for (i = 0; i < 4; i++)
|
||||
dictionarySize += (UInt32)(properties[1 + i]) << (i * 8);
|
||||
if (dictionarySize == 0)
|
||||
dictionarySize = 1; /* LZMA decoder can not work with dictionarySize = 0 */
|
||||
dictionary = (unsigned char *)malloc(dictionarySize);
|
||||
if (dictionary == 0)
|
||||
#ifndef _LZMA_IN_CB
|
||||
SizeT inAvail = compressedSize;
|
||||
const unsigned char *inBuffer = inStream;
|
||||
#endif
|
||||
LzmaDecoderInit(&state);
|
||||
do
|
||||
{
|
||||
sprintf(rs + strlen(rs), "\n can't allocate");
|
||||
return 1;
|
||||
}
|
||||
res = LzmaDecoderInit((unsigned char *)lzmaInternalData, lzmaInternalSize,
|
||||
lc, lp, pb,
|
||||
dictionary, dictionarySize,
|
||||
#ifndef _LZMA_IN_CB
|
||||
SizeT inProcessed;
|
||||
#endif
|
||||
SizeT outProcessed;
|
||||
SizeT outAvail = kOutBufferSize;
|
||||
if (!waitEOS && outSizeHigh == 0 && outAvail > outSize)
|
||||
outAvail = (SizeT)outSize;
|
||||
res = LzmaDecode(&state,
|
||||
#ifdef _LZMA_IN_CB
|
||||
&bo.InCallback
|
||||
&g_InBuffer.InCallback,
|
||||
#else
|
||||
(unsigned char *)inStream, compressedSize
|
||||
inBuffer, inAvail, &inProcessed,
|
||||
#endif
|
||||
);
|
||||
if (res == 0)
|
||||
for (nowPos = 0; nowPos < outSize;)
|
||||
{
|
||||
UInt32 blockSize = outSize - nowPos;
|
||||
UInt32 kBlockSize = 0x10000;
|
||||
if (blockSize > kBlockSize)
|
||||
blockSize = kBlockSize;
|
||||
res = LzmaDecode((unsigned char *)lzmaInternalData,
|
||||
((unsigned char *)outStream) + nowPos, blockSize, &outSizeProcessed);
|
||||
g_OutBuffer, outAvail, &outProcessed);
|
||||
if (res != 0)
|
||||
break;
|
||||
if (outSizeProcessed == 0)
|
||||
{
|
||||
outSize = nowPos;
|
||||
sprintf(rs + strlen(rs), "\nDecoding error = %d\n", res);
|
||||
res = 1;
|
||||
break;
|
||||
}
|
||||
#ifndef _LZMA_IN_CB
|
||||
inAvail -= inProcessed;
|
||||
inBuffer += inProcessed;
|
||||
#endif
|
||||
|
||||
if (outFile != 0)
|
||||
if (!MyWriteFileAndCheck(outFile, g_OutBuffer, (size_t)outProcessed))
|
||||
{
|
||||
PrintError(rs, kCantWriteMessage);
|
||||
res = 1;
|
||||
break;
|
||||
}
|
||||
|
||||
if (outSize < outProcessed)
|
||||
outSizeHigh--;
|
||||
outSize -= (UInt32)outProcessed;
|
||||
outSize &= 0xFFFFFFFF;
|
||||
|
||||
if (outProcessed == 0)
|
||||
{
|
||||
if (!waitEOS && (outSize != 0 || outSizeHigh != 0))
|
||||
res = 1;
|
||||
break;
|
||||
}
|
||||
nowPos += outSizeProcessed;
|
||||
}
|
||||
free(dictionary);
|
||||
while ((outSize != 0 && outSizeHigh == 0) || outSizeHigh != 0 || waitEOS);
|
||||
}
|
||||
|
||||
#else
|
||||
res = LzmaDecode((unsigned char *)lzmaInternalData, lzmaInternalSize,
|
||||
lc, lp, pb,
|
||||
{
|
||||
#ifndef _LZMA_IN_CB
|
||||
SizeT inProcessed;
|
||||
#endif
|
||||
SizeT outProcessed;
|
||||
res = LzmaDecode(&state,
|
||||
#ifdef _LZMA_IN_CB
|
||||
&bo.InCallback,
|
||||
&g_InBuffer.InCallback,
|
||||
#else
|
||||
(unsigned char *)inStream, compressedSize,
|
||||
inStream, compressedSize, &inProcessed,
|
||||
#endif
|
||||
(unsigned char *)outStream, outSize, &outSizeProcessed);
|
||||
outSize = outSizeProcessed;
|
||||
outStream, outSizeFull, &outProcessed);
|
||||
if (res != 0)
|
||||
{
|
||||
sprintf(rs + strlen(rs), "\nDecoding error = %d\n", res);
|
||||
res = 1;
|
||||
}
|
||||
else if (outFile != 0)
|
||||
{
|
||||
if (!MyWriteFileAndCheck(outFile, outStream, (size_t)outProcessed))
|
||||
{
|
||||
PrintError(rs, kCantWriteMessage);
|
||||
res = 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
if (res != 0)
|
||||
free(state.Probs);
|
||||
#ifdef _LZMA_OUT_READ
|
||||
free(state.Dictionary);
|
||||
#else
|
||||
free(outStream);
|
||||
#endif
|
||||
#ifndef _LZMA_IN_CB
|
||||
free(inStream);
|
||||
#endif
|
||||
return res;
|
||||
}
|
||||
|
||||
int main2(int numArgs, const char *args[], char *rs)
|
||||
{
|
||||
FILE *inFile = 0;
|
||||
FILE *outFile = 0;
|
||||
int res;
|
||||
|
||||
sprintf(rs + strlen(rs), "\nLZMA Decoder 4.21 Copyright (c) 1999-2005 Igor Pavlov 2005-06-08\n");
|
||||
if (numArgs < 2 || numArgs > 3)
|
||||
{
|
||||
sprintf(rs + strlen(rs), "\nerror = %d\n", res);
|
||||
sprintf(rs + strlen(rs), "\nUsage: lzmadec file.lzma [outFile]\n");
|
||||
return 1;
|
||||
}
|
||||
|
||||
if (numargs > 2)
|
||||
inFile = fopen(args[1], "rb");
|
||||
if (inFile == 0)
|
||||
return PrintError(rs, "Can not open input file");
|
||||
|
||||
if (numArgs > 2)
|
||||
{
|
||||
outputHandle = fopen(args[2], "wb+");
|
||||
if (outputHandle == 0)
|
||||
{
|
||||
sprintf(rs + strlen(rs), "\n Open output file error");
|
||||
return 1;
|
||||
}
|
||||
processedSize = fwrite(outStream, 1, outSize, outputHandle);
|
||||
if (processedSize != outSize)
|
||||
{
|
||||
sprintf(rs + strlen(rs), "\n can't write");
|
||||
return 1;
|
||||
}
|
||||
fclose(outputHandle);
|
||||
outFile = fopen(args[2], "wb+");
|
||||
if (outFile == 0)
|
||||
return PrintError(rs, "Can not open output file");
|
||||
}
|
||||
free(lzmaInternalData);
|
||||
free(outStream);
|
||||
free(inStream);
|
||||
return 0;
|
||||
|
||||
res = main3(inFile, outFile, rs);
|
||||
|
||||
if (outFile != 0)
|
||||
fclose(outFile);
|
||||
fclose(inFile);
|
||||
return res;
|
||||
}
|
||||
|
||||
int main(int numargs, const char *args[])
|
||||
int main(int numArgs, const char *args[])
|
||||
{
|
||||
char sz[800] = { 0 };
|
||||
int code = main2(numargs, args, sz);
|
||||
printf(sz);
|
||||
return code;
|
||||
char rs[800] = { 0 };
|
||||
int res = main2(numArgs, args, rs);
|
||||
printf(rs);
|
||||
return res;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user