mirror of
https://github.com/emmanuel-marty/lzsa.git
synced 2025-02-05 21:35:30 +00:00
Add strict block checks when decompressing, comparing
This commit is contained in:
parent
b3268b74eb
commit
88054841cf
16
src/main.c
16
src/main.c
@ -254,6 +254,7 @@ static int lzsa_compress(const char *pszInFilename, const char *pszOutFilename,
|
|||||||
fprintf(stdout, "\rCompressed '%s' in %g seconds, %.02g Mb/s, %d tokens (%lld bytes/token), %lld into %lld bytes ==> %g %%\n",
|
fprintf(stdout, "\rCompressed '%s' in %g seconds, %.02g Mb/s, %d tokens (%lld bytes/token), %lld into %lld bytes ==> %g %%\n",
|
||||||
pszInFilename, fDelta, fSpeed, nCommands, nOriginalSize / ((long long)nCommands),
|
pszInFilename, fDelta, fSpeed, nCommands, nOriginalSize / ((long long)nCommands),
|
||||||
nOriginalSize, nCompressedSize, (double)(nCompressedSize * 100.0 / nOriginalSize));
|
nOriginalSize, nCompressedSize, (double)(nCompressedSize * 100.0 / nOriginalSize));
|
||||||
|
fflush(stdout);
|
||||||
}
|
}
|
||||||
|
|
||||||
lzsa_compressor_destroy(&compressor);
|
lzsa_compressor_destroy(&compressor);
|
||||||
@ -370,7 +371,6 @@ static int lzsa_decompress(const char *pszInFilename, const char *pszOutFilename
|
|||||||
int nPrevDecompressedSize = 0;
|
int nPrevDecompressedSize = 0;
|
||||||
|
|
||||||
while (!feof(pInFile) && !nDecompressionError) {
|
while (!feof(pInFile) && !nDecompressionError) {
|
||||||
unsigned char cBlockSize[3];
|
|
||||||
unsigned int nBlockSize = 0;
|
unsigned int nBlockSize = 0;
|
||||||
|
|
||||||
if (nPrevDecompressedSize != 0) {
|
if (nPrevDecompressedSize != 0) {
|
||||||
@ -378,6 +378,9 @@ static int lzsa_decompress(const char *pszInFilename, const char *pszOutFilename
|
|||||||
}
|
}
|
||||||
|
|
||||||
if ((nOptions & OPT_RAW) == 0) {
|
if ((nOptions & OPT_RAW) == 0) {
|
||||||
|
unsigned char cBlockSize[3];
|
||||||
|
|
||||||
|
memset(cBlockSize, 0, 3);
|
||||||
if (fread(cBlockSize, 1, 3, pInFile) == 3) {
|
if (fread(cBlockSize, 1, 3, pInFile) == 3) {
|
||||||
nBlockSize = ((unsigned int)cBlockSize[0]) |
|
nBlockSize = ((unsigned int)cBlockSize[0]) |
|
||||||
(((unsigned int)cBlockSize[1]) << 8) |
|
(((unsigned int)cBlockSize[1]) << 8) |
|
||||||
@ -388,6 +391,7 @@ static int lzsa_decompress(const char *pszInFilename, const char *pszOutFilename
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
|
if (nFileSize >= 4)
|
||||||
nBlockSize = nFileSize - 4;
|
nBlockSize = nFileSize - 4;
|
||||||
nFileSize = 0;
|
nFileSize = 0;
|
||||||
}
|
}
|
||||||
@ -397,6 +401,10 @@ static int lzsa_decompress(const char *pszInFilename, const char *pszOutFilename
|
|||||||
int nDecompressedSize = 0;
|
int nDecompressedSize = 0;
|
||||||
|
|
||||||
nBlockSize &= 0x7fffff;
|
nBlockSize &= 0x7fffff;
|
||||||
|
if ((int)nBlockSize > BLOCK_SIZE) {
|
||||||
|
fprintf(stderr, "block size %d > max size %d\n", nBlockSize, BLOCK_SIZE);
|
||||||
|
break;
|
||||||
|
}
|
||||||
if (fread(pInBlock, 1, nBlockSize, pInFile) == nBlockSize) {
|
if (fread(pInBlock, 1, nBlockSize, pInFile) == nBlockSize) {
|
||||||
if (bIsUncompressed) {
|
if (bIsUncompressed) {
|
||||||
memcpy(pOutData + BLOCK_SIZE, pInBlock, nBlockSize);
|
memcpy(pOutData + BLOCK_SIZE, pInBlock, nBlockSize);
|
||||||
@ -578,6 +586,7 @@ static int lzsa_compare(const char *pszInFilename, const char *pszOutFilename, c
|
|||||||
if ((nOptions & OPT_RAW) == 0) {
|
if ((nOptions & OPT_RAW) == 0) {
|
||||||
unsigned char cBlockSize[3];
|
unsigned char cBlockSize[3];
|
||||||
|
|
||||||
|
memset(cBlockSize, 0, 3);
|
||||||
if (fread(cBlockSize, 1, 3, pInFile) == 3) {
|
if (fread(cBlockSize, 1, 3, pInFile) == 3) {
|
||||||
nBlockSize = ((unsigned int)cBlockSize[0]) |
|
nBlockSize = ((unsigned int)cBlockSize[0]) |
|
||||||
(((unsigned int)cBlockSize[1]) << 8) |
|
(((unsigned int)cBlockSize[1]) << 8) |
|
||||||
@ -588,6 +597,7 @@ static int lzsa_compare(const char *pszInFilename, const char *pszOutFilename, c
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
|
if (nFileSize >= 4)
|
||||||
nBlockSize = nFileSize - 4;
|
nBlockSize = nFileSize - 4;
|
||||||
nFileSize = 0;
|
nFileSize = 0;
|
||||||
}
|
}
|
||||||
@ -597,6 +607,10 @@ static int lzsa_compare(const char *pszInFilename, const char *pszOutFilename, c
|
|||||||
int nDecompressedSize = 0;
|
int nDecompressedSize = 0;
|
||||||
|
|
||||||
nBlockSize &= 0x7fffff;
|
nBlockSize &= 0x7fffff;
|
||||||
|
if ((int)nBlockSize > BLOCK_SIZE) {
|
||||||
|
fprintf(stderr, "block size %d > max size %d\n", nBlockSize, BLOCK_SIZE);
|
||||||
|
break;
|
||||||
|
}
|
||||||
if (fread(pInBlock, 1, nBlockSize, pInFile) == nBlockSize) {
|
if (fread(pInBlock, 1, nBlockSize, pInFile) == nBlockSize) {
|
||||||
if (bIsUncompressed) {
|
if (bIsUncompressed) {
|
||||||
memcpy(pOutData + BLOCK_SIZE, pInBlock, nBlockSize);
|
memcpy(pOutData + BLOCK_SIZE, pInBlock, nBlockSize);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user