Skip to content

Commit

Permalink
Fixed more bugs reading lzma, bzip2, and zlib streams.
Browse files Browse the repository at this point in the history
  • Loading branch information
nmoinvaz committed Oct 18, 2017
1 parent 1892617 commit 68fae99
Show file tree
Hide file tree
Showing 3 changed files with 114 additions and 92 deletions.
74 changes: 40 additions & 34 deletions src/mz_strm_bzip.c
Expand Up @@ -112,10 +112,10 @@ int32_t mz_stream_bzip_read(void *stream, void *buf, int32_t size)
uint64_t total_out_before = 0;
uint64_t total_in_after = 0;
uint64_t total_out_after = 0;
uint32_t in_bytes = 0;
uint32_t out_bytes = 0;
uint32_t total_in = 0;
uint32_t total_out = 0;
uint32_t in_bytes = 0;
uint32_t out_bytes = 0;
int32_t bytes_to_read = 0;
int32_t read = 0;
int16_t err = BZ_OK;
Expand All @@ -139,7 +139,8 @@ int32_t mz_stream_bzip_read(void *stream, void *buf, int32_t size)
}

read = mz_stream_read(bzip->stream.base, bzip->buffer, bytes_to_read);
if (mz_stream_error(bzip->stream.base))

if (read < 0)
{
bzip->error = BZ_IO_ERROR;
break;
Expand All @@ -161,25 +162,28 @@ int32_t mz_stream_bzip_read(void *stream, void *buf, int32_t size)
total_out_after = bzip->bzstream.total_out_lo32 +
(((uint64_t)bzip->bzstream.total_out_hi32) << 32);

total_in += (uint32_t)(total_in_before - total_in_after);
total_out += (uint32_t)(total_out_after - total_out_before);
in_bytes = (uint32_t)(total_in_before - total_in_after);
out_bytes = (uint32_t)(total_out_after - total_out_before);

total_in += in_bytes;
total_out += out_bytes;

bzip->total_in += in_bytes;
bzip->total_out += out_bytes;

if (err == BZ_STREAM_END)
{
bzip->stream_end = 1;
break;
}
if (err != BZ_RUN_OK)
if (err != BZ_OK && err != BZ_RUN_OK)
{
bzip->error = err;
break;
}
}
while (bzip->bzstream.avail_out > 0);

bzip->total_in += total_in;
bzip->total_out += total_out;

if (bzip->error != 0)
return bzip->error;

Expand All @@ -202,39 +206,42 @@ uint32_t mz_stream_bzip_compress(void *stream, int flush)
uint32_t out_bytes = 0;
int16_t err = BZ_OK;


if (bzip->bzstream.avail_out == 0)
do
{
if (mz_stream_bzip_flush(bzip) != MZ_OK)
if (bzip->bzstream.avail_out == 0)
{
bzip->error = BZ_DATA_ERROR;
return MZ_STREAM_ERROR;
}
if (mz_stream_bzip_flush(bzip) != MZ_OK)
{
bzip->error = BZ_DATA_ERROR;
return MZ_STREAM_ERROR;
}

bzip->bzstream.avail_out = sizeof(bzip->buffer);
bzip->bzstream.next_out = (char *)bzip->buffer;
bzip->bzstream.avail_out = sizeof(bzip->buffer);
bzip->bzstream.next_out = (char *)bzip->buffer;

bzip->buffer_len = 0;
}
bzip->buffer_len = 0;
}

total_out_before = bzip->bzstream.total_out_lo32 +
(((uint64_t)bzip->bzstream.total_out_hi32) << 32);
total_out_before = bzip->bzstream.total_out_lo32 +
(((uint64_t)bzip->bzstream.total_out_hi32) << 32);

err = BZ2_bzCompress(&bzip->bzstream, flush);
err = BZ2_bzCompress(&bzip->bzstream, flush);

total_out_after = bzip->bzstream.total_out_lo32 +
(((uint64_t)bzip->bzstream.total_out_hi32) << 32);
total_out_after = bzip->bzstream.total_out_lo32 +
(((uint64_t)bzip->bzstream.total_out_hi32) << 32);

out_bytes = (uint32_t)(total_out_after - total_out_before);
out_bytes = (uint32_t)(total_out_after - total_out_before);

if (err < 0)
{
bzip->error = err;
return MZ_STREAM_ERROR;
}
if (err < 0)
{
bzip->error = err;
return MZ_STREAM_ERROR;
}

bzip->buffer_len += out_bytes;
bzip->total_out += out_bytes;
bzip->buffer_len += out_bytes;
bzip->total_out += out_bytes;
}
while ((bzip->bzstream.avail_in > 0) || (flush == BZ_FINISH && err == BZ_FINISH_OK));

return MZ_OK;
}
Expand All @@ -247,8 +254,7 @@ int32_t mz_stream_bzip_write(void *stream, const void *buf, int32_t size)
bzip->bzstream.next_in = (char *)buf;
bzip->bzstream.avail_in = size;

while ((bzip->error == BZ_OK) && (bzip->bzstream.avail_in > 0))
mz_stream_bzip_compress(stream, BZ_RUN);
mz_stream_bzip_compress(stream, BZ_RUN);

bzip->total_in += size;

Expand Down
68 changes: 38 additions & 30 deletions src/mz_strm_lzma.c
Expand Up @@ -110,11 +110,11 @@ int32_t mz_stream_lzma_open(void *stream, const char *path, int32_t mode)

mz_stream_read_uint8(lzma->stream.base, &major);
mz_stream_read_uint8(lzma->stream.base, &minor);
mz_stream_read_uint16(lzma->stream.base, &size);
mz_stream_read_uint16(lzma->stream.base, (uint16_t *)&size);

lzma->total_in += MZ_LZMA_HEADER_SIZE;

lzma->error = lzma_alone_decoder(&lzma->lstream, UINT64_MAX, LZMA_CONCATENATED);
lzma->error = lzma_alone_decoder(&lzma->lstream, UINT64_MAX);
}

if (lzma->error != LZMA_OK)
Expand Down Expand Up @@ -142,6 +142,8 @@ int32_t mz_stream_lzma_read(void *stream, void *buf, int32_t size)
uint64_t total_out_after = 0;
uint32_t total_in = 0;
uint32_t total_out = 0;
uint32_t in_bytes = 0;
uint32_t out_bytes = 0;
int32_t bytes_to_read = 0;
int32_t read = 0;
int16_t err = LZMA_OK;
Expand Down Expand Up @@ -183,8 +185,14 @@ int32_t mz_stream_lzma_read(void *stream, void *buf, int32_t size)
total_in_after = lzma->lstream.avail_in;
total_out_after = lzma->lstream.total_out;

total_in += (uint32_t)(total_in_before - total_in_after);
total_out += (uint32_t)(total_out_after - total_out_before);
in_bytes = (uint32_t)(total_in_before - total_in_after);
out_bytes = (uint32_t)(total_out_after - total_out_before);

total_in += in_bytes;
total_out += out_bytes;

lzma->total_in += in_bytes;
lzma->total_out += out_bytes;

if (err == LZMA_STREAM_END)
break;
Expand All @@ -196,9 +204,6 @@ int32_t mz_stream_lzma_read(void *stream, void *buf, int32_t size)
}
while (lzma->lstream.avail_out > 0);

lzma->total_in += total_in;
lzma->total_out += total_out;

if (lzma->error != 0)
return lzma->error;

Expand All @@ -222,34 +227,38 @@ uint32_t mz_stream_lzma_code(void *stream, int32_t flush)
int16_t err = LZMA_OK;


if (lzma->lstream.avail_out == 0)
do
{
if (mz_stream_lzma_flush(lzma) != MZ_OK)
if (lzma->lstream.avail_out == 0)
{
lzma->error = MZ_STREAM_ERROR;
return MZ_STREAM_ERROR;
}
if (mz_stream_lzma_flush(lzma) != MZ_OK)
{
lzma->error = MZ_STREAM_ERROR;
return MZ_STREAM_ERROR;
}

lzma->lstream.avail_out = sizeof(lzma->buffer);
lzma->lstream.next_out = lzma->buffer;
lzma->lstream.avail_out = sizeof(lzma->buffer);
lzma->lstream.next_out = lzma->buffer;

lzma->buffer_len = 0;
}
lzma->buffer_len = 0;
}

total_out_before = lzma->lstream.total_out;
err = lzma_code(&lzma->lstream, flush);
total_out_after = lzma->lstream.total_out;
total_out_before = lzma->lstream.total_out;
err = lzma_code(&lzma->lstream, flush);
total_out_after = lzma->lstream.total_out;

out_bytes = (uint32_t)(total_out_after - total_out_before);
out_bytes = (uint32_t)(total_out_after - total_out_before);

if (err != LZMA_OK && err != LZMA_STREAM_END)
{
lzma->error = err;
return MZ_STREAM_ERROR;
}
if (err != LZMA_OK && err != LZMA_STREAM_END)
{
lzma->error = err;
return MZ_STREAM_ERROR;
}

lzma->buffer_len += out_bytes;
lzma->total_out += out_bytes;
lzma->buffer_len += out_bytes;
lzma->total_out += out_bytes;
}
while (lzma->lstream.avail_in > 0);

return MZ_OK;
}
Expand All @@ -262,8 +271,7 @@ int32_t mz_stream_lzma_write(void *stream, const void *buf, int32_t size)
lzma->lstream.next_in = (uint8_t*)buf;
lzma->lstream.avail_in = size;

while ((lzma->error == LZMA_OK) && (lzma->lstream.avail_in > 0))
mz_stream_lzma_code(stream, LZMA_RUN);
mz_stream_lzma_code(stream, LZMA_RUN);

lzma->total_in += size;

Expand Down Expand Up @@ -339,7 +347,7 @@ int32_t mz_stream_lzma_set_prop_int64(void *stream, int32_t prop, int64_t value)
lzma->preset = LZMA_PRESET_DEFAULT;
return MZ_OK;
case MZ_STREAM_PROP_TOTAL_IN_MAX:
lzma->max_total_in = value + MZ_LZMA_HEADER_SIZE;
lzma->max_total_in = value;
return MZ_OK;
}
return MZ_EXIST_ERROR;
Expand Down
64 changes: 36 additions & 28 deletions src/mz_strm_zlib.c
Expand Up @@ -122,6 +122,8 @@ int32_t mz_stream_zlib_read(void *stream, void *buf, int32_t size)
uint64_t total_out_after = 0;
uint32_t total_in = 0;
uint32_t total_out = 0;
uint32_t in_bytes = 0;
uint32_t out_bytes = 0;
int32_t bytes_to_read = 0;
int32_t read = 0;
int16_t err = Z_OK;
Expand All @@ -140,7 +142,7 @@ int32_t mz_stream_zlib_read(void *stream, void *buf, int32_t size)
if ((zlib->max_total_in - zlib->total_in) < sizeof(zlib->buffer))
bytes_to_read = (int32_t)(zlib->max_total_in - zlib->total_in);
}

read = mz_stream_read(zlib->stream.base, zlib->buffer, bytes_to_read);

if (read < 0)
Expand Down Expand Up @@ -168,8 +170,14 @@ int32_t mz_stream_zlib_read(void *stream, void *buf, int32_t size)
total_in_after = zlib->zstream.avail_in;
total_out_after = zlib->zstream.total_out;

total_in += (uint32_t)(total_in_before - total_in_after);
total_out += (uint32_t)(total_out_after - total_out_before);
in_bytes = (uint32_t)(total_in_before - total_in_after);
out_bytes = (uint32_t)(total_out_after - total_out_before);

total_in += in_bytes;
total_out += out_bytes;

zlib->total_in += in_bytes;
zlib->total_out += out_bytes;

if (err == Z_STREAM_END)
break;
Expand All @@ -182,9 +190,6 @@ int32_t mz_stream_zlib_read(void *stream, void *buf, int32_t size)
}
while (zlib->zstream.avail_out > 0);

zlib->total_in += total_in;
zlib->total_out += total_out;

if (zlib->error != 0)
return zlib->error;

Expand All @@ -208,34 +213,38 @@ int32_t mz_stream_zlib_deflate(void *stream, int flush)
int16_t err = Z_OK;


if (zlib->zstream.avail_out == 0)
do
{
if (mz_stream_zlib_flush(zlib) != MZ_OK)
if (zlib->zstream.avail_out == 0)
{
zlib->error = Z_STREAM_ERROR;
return MZ_STREAM_ERROR;
}
if (mz_stream_zlib_flush(zlib) != MZ_OK)
{
zlib->error = Z_STREAM_ERROR;
return MZ_STREAM_ERROR;
}

zlib->zstream.avail_out = sizeof(zlib->buffer);
zlib->zstream.next_out = zlib->buffer;
zlib->zstream.avail_out = sizeof(zlib->buffer);
zlib->zstream.next_out = zlib->buffer;

zlib->buffer_len = 0;
}
zlib->buffer_len = 0;
}

total_out_before = zlib->zstream.total_out;
err = deflate(&zlib->zstream, flush);
total_out_after = zlib->zstream.total_out;
total_out_before = zlib->zstream.total_out;
err = deflate(&zlib->zstream, flush);
total_out_after = zlib->zstream.total_out;

out_bytes = (uint32_t)(total_out_after - total_out_before);
out_bytes = (uint32_t)(total_out_after - total_out_before);

if (err != Z_OK && err != Z_STREAM_END)
{
zlib->error = err;
return MZ_STREAM_ERROR;
}
if (err != Z_OK && err != Z_STREAM_END)
{
zlib->error = err;
return MZ_STREAM_ERROR;
}

zlib->buffer_len += out_bytes;
zlib->total_out += out_bytes;
zlib->buffer_len += out_bytes;
zlib->total_out += out_bytes;
}
while (zlib->zstream.avail_in > 0);

return MZ_OK;
}
Expand All @@ -248,8 +257,7 @@ int32_t mz_stream_zlib_write(void *stream, const void *buf, int32_t size)
zlib->zstream.next_in = (uint8_t*)buf;
zlib->zstream.avail_in = size;

while ((zlib->error == Z_OK) && (zlib->zstream.avail_in > 0))
mz_stream_zlib_deflate(stream, Z_NO_FLUSH);
mz_stream_zlib_deflate(stream, Z_NO_FLUSH);

zlib->total_in += size;

Expand Down

0 comments on commit 68fae99

Please sign in to comment.