Fix huge chunk handling in zlib compression routines.

Fix Zlib error messages.
Remove extra variable in Bzip2 decompress routine.
This commit is contained in:
Moinak Ghosh 2012-07-27 00:11:01 +05:30
parent b586d30359
commit 9c3423530c
2 changed files with 102 additions and 31 deletions

View file

@ -159,7 +159,7 @@ bzip2_decompress(void *src, size_t srclen, void *dst, size_t *dstlen,
int level, uchar_t chdr, void *data) int level, uchar_t chdr, void *data)
{ {
bz_stream bzs; bz_stream bzs;
int ret, ending; int ret;
unsigned int slen, dlen; unsigned int slen, dlen;
size_t _srclen = srclen; size_t _srclen = srclen;
size_t _dstlen = *dstlen; size_t _dstlen = *dstlen;
@ -181,7 +181,6 @@ bzip2_decompress(void *src, size_t srclen, void *dst, size_t *dstlen,
slen = SINGLE_CALL_MAX; slen = SINGLE_CALL_MAX;
} else { } else {
slen = _srclen; slen = _srclen;
ending = 1;
} }
if (_dstlen > SINGLE_CALL_MAX) { if (_dstlen > SINGLE_CALL_MAX) {
dlen = SINGLE_CALL_MAX; dlen = SINGLE_CALL_MAX;

View file

@ -29,6 +29,11 @@
#include <pcompress.h> #include <pcompress.h>
#include <allocator.h> #include <allocator.h>
/*
* Max buffer size allowed for a single zlib compress/decompress call.
*/
#define SINGLE_CALL_MAX (2147483648UL)
static void * static void *
slab_alloc_ui(void *p, unsigned int items, unsigned int size) { slab_alloc_ui(void *p, unsigned int items, unsigned int size) {
void *ptr; void *ptr;
@ -58,7 +63,7 @@ void zerr(int ret)
perror(" "); perror(" ");
break; break;
case Z_STREAM_ERROR: case Z_STREAM_ERROR:
fprintf(stderr, "Zlib: Invalid compression level\n"); fprintf(stderr, "Zlib: Invalid stream structure\n");
break; break;
case Z_DATA_ERROR: case Z_DATA_ERROR:
fprintf(stderr, "Zlib: Invalid or incomplete deflate data\n"); fprintf(stderr, "Zlib: Invalid or incomplete deflate data\n");
@ -69,6 +74,12 @@ void zerr(int ret)
case Z_VERSION_ERROR: case Z_VERSION_ERROR:
fprintf(stderr, "Zlib: Version mismatch!\n"); fprintf(stderr, "Zlib: Version mismatch!\n");
break; break;
case Z_BUF_ERROR:
fprintf(stderr, "Zlib: Buffer error decompression failed.\n");
break;
case Z_NEED_DICT:
fprintf(stderr, "Zlib: Need present dictionary.\n");
break;
default: default:
fprintf(stderr, "Zlib: Unknown error code: %d\n", ret); fprintf(stderr, "Zlib: Unknown error code: %d\n", ret);
} }
@ -79,12 +90,12 @@ zlib_compress(void *src, size_t srclen, void *dst, size_t *dstlen,
int level, uchar_t chdr, void *data) int level, uchar_t chdr, void *data)
{ {
z_stream zs; z_stream zs;
int ret; int ret, ending;
unsigned int slen, dlen;
zs.next_in = src; size_t _srclen = srclen;
zs.avail_in = srclen; size_t _dstlen = *dstlen;
zs.next_out = dst; uchar_t *dst1 = dst;
zs.avail_out = *dstlen; uchar_t *src1 = src;
zs.zalloc = slab_alloc_ui; zs.zalloc = slab_alloc_ui;
zs.zfree = slab_free; zs.zfree = slab_free;
@ -96,16 +107,49 @@ zlib_compress(void *src, size_t srclen, void *dst, size_t *dstlen,
return (-1); return (-1);
} }
ret = deflate(&zs, Z_FINISH); ending = 0;
if (ret != Z_STREAM_END) { while (_srclen > 0) {
deflateEnd(&zs); if (_srclen > SINGLE_CALL_MAX) {
if (ret == Z_OK) slen = SINGLE_CALL_MAX;
zerr(Z_BUF_ERROR); } else {
else slen = _srclen;
zerr(ret); ending = 1;
return (-1); }
if (_dstlen > SINGLE_CALL_MAX) {
dlen = SINGLE_CALL_MAX;
} else {
dlen = _dstlen;
}
zs.next_in = src1;
zs.avail_in = slen;
zs.next_out = dst1;
zs.avail_out = dlen;
if (!ending) {
ret = deflate(&zs, Z_NO_FLUSH);
if (ret != Z_OK) {
deflateEnd(&zs);
zerr(ret);
return (-1);
}
} else {
ret = deflate(&zs, Z_FINISH);
if (ret != Z_STREAM_END) {
deflateEnd(&zs);
if (ret == Z_OK)
zerr(Z_BUF_ERROR);
else
zerr(ret);
return (-1);
}
}
dst1 += (dlen - zs.avail_out);
_dstlen -= (dlen - zs.avail_out);
src1 += slen;
_srclen -= slen;
} }
*dstlen = zs.total_out;
*dstlen = *dstlen - _dstlen;
ret = deflateEnd(&zs); ret = deflateEnd(&zs);
if (ret != Z_OK) { if (ret != Z_OK) {
zerr(ret); zerr(ret);
@ -120,12 +164,11 @@ zlib_decompress(void *src, size_t srclen, void *dst, size_t *dstlen,
{ {
z_stream zs; z_stream zs;
int err; int err;
unsigned int slen, dlen;
bzero(&zs, sizeof (zs)); size_t _srclen = srclen;
zs.next_in = (unsigned char *)src; size_t _dstlen = *dstlen;
zs.avail_in = srclen; uchar_t *dst1 = dst;
zs.next_out = dst; uchar_t *src1 = src;
zs.avail_out = *dstlen;
zs.zalloc = slab_alloc_ui; zs.zalloc = slab_alloc_ui;
zs.zfree = slab_free; zs.zfree = slab_free;
@ -136,16 +179,45 @@ zlib_decompress(void *src, size_t srclen, void *dst, size_t *dstlen,
return (-1); return (-1);
} }
if ((err = inflate(&zs, Z_FINISH)) != Z_STREAM_END) { while (_srclen > 0) {
inflateEnd(&zs); if (_srclen > SINGLE_CALL_MAX) {
if (err == Z_OK) slen = SINGLE_CALL_MAX;
zerr(Z_BUF_ERROR); } else {
else slen = _srclen;
}
if (_dstlen > SINGLE_CALL_MAX) {
dlen = SINGLE_CALL_MAX;
} else {
dlen = _dstlen;
}
zs.next_in = src1;
zs.avail_in = slen;
zs.next_out = dst1;
zs.avail_out = dlen;
err = inflate(&zs, Z_NO_FLUSH);
if (err != Z_OK && err != Z_STREAM_END) {
zerr(err); zerr(err);
return (-1); return (-1);
}
dst1 += (dlen - zs.avail_out);
_dstlen -= (dlen - zs.avail_out);
src1 += (slen - zs.avail_in);
_srclen -= (slen - zs.avail_in);
if (err == Z_STREAM_END) {
if (_srclen > 0) {
zerr(Z_DATA_ERROR);
return (-1);
} else {
break;
}
}
} }
*dstlen = zs.total_out; *dstlen = _dstlen;
inflateEnd(&zs); inflateEnd(&zs);
return (0); return (0);
} }