From f0237b844028884117f386b43bce719931be7e22 Mon Sep 17 00:00:00 2001 From: Shane Alcock Date: Tue, 19 Sep 2023 11:39:59 +1200 Subject: [PATCH] Fix issue where bzip2 files containing multiple streams were being truncated Thanks to Bill Herrin for pointing this out (#54) --- lib/ior-bzip.c | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/lib/ior-bzip.c b/lib/ior-bzip.c index 5afabf2..5eca49a 100644 --- a/lib/ior-bzip.c +++ b/lib/ior-bzip.c @@ -44,6 +44,7 @@ struct bz_t { int outoffset; io_t *parent; enum err_t err; + uint8_t streamopen; }; extern io_source_t bz_source; @@ -72,7 +73,7 @@ DLLEXPORT io_t *bz_open(io_t *parent) { BZ2_bzDecompressInit(&DATA(io)->strm, 0, /* Verbosity */ 0); /* small */ - + DATA(io)->streamopen = 1; return io; } @@ -114,7 +115,11 @@ static int64_t bz_read(io_t *io, void *buffer, int64_t len) { DATA(io)->err = ERR_OK; break; case BZ_STREAM_END: - DATA(io)->err = ERR_EOF; + /* Stream is over, but there could be more stream to + * follow (e.g. if the file was compressed by pbzip2) + */ + BZ2_bzDecompressEnd(&DATA(io)->strm); + BZ2_bzDecompressInit(&DATA(io)->strm, 0, 0); break; default: errno = EIO; @@ -126,7 +131,9 @@ static int64_t bz_read(io_t *io, void *buffer, int64_t len) { } static void bz_close(io_t *io) { - BZ2_bzDecompressEnd(&DATA(io)->strm); + if (DATA(io)->streamopen) { + BZ2_bzDecompressEnd(&DATA(io)->strm); + } wandio_destroy(DATA(io)->parent); free(io->data); free(io);