From 38ec64f181318ba87481fe820abba6caa86d1ca6 Mon Sep 17 00:00:00 2001 From: Timo Sirainen Date: Wed, 7 May 2014 12:26:38 +0300 Subject: [PATCH] lib-compression: Fixed LZMA compression. The code now looks more like the doc/examples/01_compress_easy.c distributed with xz-utils. Most importantly this changes LZMA_OK to be allowed as a result for lzma_code(zs, LZMA_FINISH). --- src/lib-compression/ostream-lzma.c | 34 ++++++++++++++++-------------- 1 file changed, 18 insertions(+), 16 deletions(-) diff --git a/src/lib-compression/ostream-lzma.c b/src/lib-compression/ostream-lzma.c index 095f7d231c..f25dd7a065 100644 --- a/src/lib-compression/ostream-lzma.c +++ b/src/lib-compression/ostream-lzma.c @@ -83,7 +83,8 @@ o_stream_lzma_send_chunk(struct lzma_ostream *zstream, } } - switch (lzma_code(zs, LZMA_RUN)) { + ret = lzma_code(zs, LZMA_RUN); + switch (ret) { case LZMA_OK: break; case LZMA_MEM_ERROR: @@ -91,7 +92,8 @@ o_stream_lzma_send_chunk(struct lzma_ostream *zstream, "lzma.write(%s): Out of memory", o_stream_get_name(&zstream->ostream.ostream)); default: - i_unreached(); + i_panic("lzma.write(%s) failed with unexpected code %d", + o_stream_get_name(&zstream->ostream.ostream), ret); } } size -= zs->avail_in; @@ -122,20 +124,10 @@ static int o_stream_lzma_send_flush(struct lzma_ostream *zstream) i_assert(zstream->outbuf_used == 0); do { - len = sizeof(zstream->outbuf) - zs->avail_out; - if (len != 0) { - zs->next_out = zstream->outbuf; - zs->avail_out = sizeof(zstream->outbuf); - - zstream->outbuf_used = len; - if ((ret = o_stream_zlib_send_outbuf(zstream)) <= 0) - return ret; - if (done) - break; - } - ret = lzma_code(zs, LZMA_FINISH); switch (ret) { + case LZMA_OK: + break; case LZMA_STREAM_END: done = TRUE; break; @@ -144,9 +136,19 @@ static int o_stream_lzma_send_flush(struct lzma_ostream *zstream) "lzma.write(%s): Out of memory", o_stream_get_name(&zstream->ostream.ostream)); default: - i_unreached(); + i_panic("lzma.write(%s) flush failed with unexpected code %d", + o_stream_get_name(&zstream->ostream.ostream), ret); + } + if (zs->avail_out == 0 || done) { + len = sizeof(zstream->outbuf) - zs->avail_out; + zs->next_out = zstream->outbuf; + zs->avail_out = sizeof(zstream->outbuf); + + zstream->outbuf_used = len; + if ((ret = o_stream_zlib_send_outbuf(zstream)) <= 0) + return ret; } - } while (zs->avail_out != sizeof(zstream->outbuf)); + } while (!done); zstream->flushed = TRUE; return 0; -- 2.47.3