From 68fae9970c172f266926ca7d2b79aaf0f1ba9651 Mon Sep 17 00:00:00 2001 From: Nathan Moinvaziri Date: Wed, 18 Oct 2017 15:46:06 -0700 Subject: [PATCH] Fixed more bugs reading lzma, bzip2, and zlib streams. --- src/mz_strm_bzip.c | 74 +++++++++++++++++++++++++--------------------- src/mz_strm_lzma.c | 68 +++++++++++++++++++++++------------------- src/mz_strm_zlib.c | 64 +++++++++++++++++++++------------------ 3 files changed, 114 insertions(+), 92 deletions(-) diff --git a/src/mz_strm_bzip.c b/src/mz_strm_bzip.c index b1f37935..56ef9e9b 100644 --- a/src/mz_strm_bzip.c +++ b/src/mz_strm_bzip.c @@ -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; @@ -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; @@ -161,15 +162,21 @@ 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; @@ -177,9 +184,6 @@ int32_t mz_stream_bzip_read(void *stream, void *buf, int32_t size) } while (bzip->bzstream.avail_out > 0); - bzip->total_in += total_in; - bzip->total_out += total_out; - if (bzip->error != 0) return bzip->error; @@ -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; } @@ -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; diff --git a/src/mz_strm_lzma.c b/src/mz_strm_lzma.c index a5d1336c..8810425e 100644 --- a/src/mz_strm_lzma.c +++ b/src/mz_strm_lzma.c @@ -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) @@ -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; @@ -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; @@ -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; @@ -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; } @@ -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; @@ -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; diff --git a/src/mz_strm_zlib.c b/src/mz_strm_zlib.c index d8cc567a..1880ef85 100644 --- a/src/mz_strm_zlib.c +++ b/src/mz_strm_zlib.c @@ -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; @@ -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) @@ -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; @@ -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; @@ -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; } @@ -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;