bcb4e51a409d94ae670de96afb8483a4f7855294Stephan Bosch/* Copyright (c) 2010-2018 Dovecot authors, see the included COPYING file */
12983e9d3b4ebdfb1e14f197c153304b3af44b59Timo Sirainenstatic void o_stream_lzma_close(struct iostream_private *stream,
12983e9d3b4ebdfb1e14f197c153304b3af44b59Timo Sirainen struct lzma_ostream *zstream = (struct lzma_ostream *)stream;
12983e9d3b4ebdfb1e14f197c153304b3af44b59Timo Sirainenstatic int o_stream_zlib_send_outbuf(struct lzma_ostream *zstream)
12983e9d3b4ebdfb1e14f197c153304b3af44b59Timo Sirainen size = zstream->outbuf_used - zstream->outbuf_offset;
12983e9d3b4ebdfb1e14f197c153304b3af44b59Timo Sirainen zstream->outbuf + zstream->outbuf_offset, size);
12983e9d3b4ebdfb1e14f197c153304b3af44b59Timo Sirainen o_stream_copy_error_from_parent(&zstream->ostream);
12983e9d3b4ebdfb1e14f197c153304b3af44b59Timo Siraineno_stream_lzma_send_chunk(struct lzma_ostream *zstream,
12983e9d3b4ebdfb1e14f197c153304b3af44b59Timo Sirainen /* previous block was compressed. send it and start
12983e9d3b4ebdfb1e14f197c153304b3af44b59Timo Sirainen compression for a new block. */
12983e9d3b4ebdfb1e14f197c153304b3af44b59Timo Sirainen zstream->outbuf_used = sizeof(zstream->outbuf);
12983e9d3b4ebdfb1e14f197c153304b3af44b59Timo Sirainen if ((ret = o_stream_zlib_send_outbuf(zstream)) < 0)
12983e9d3b4ebdfb1e14f197c153304b3af44b59Timo Sirainen /* parent stream's buffer full */
12983e9d3b4ebdfb1e14f197c153304b3af44b59Timo Sirainen "lzma.write(%s): Out of memory",
12983e9d3b4ebdfb1e14f197c153304b3af44b59Timo Sirainen o_stream_get_name(&zstream->ostream.ostream));
38ec64f181318ba87481fe820abba6caa86d1ca6Timo Sirainen i_panic("lzma.write(%s) failed with unexpected code %d",
38ec64f181318ba87481fe820abba6caa86d1ca6Timo Sirainen o_stream_get_name(&zstream->ostream.ostream), ret);
12983e9d3b4ebdfb1e14f197c153304b3af44b59Timo Sirainenstatic int o_stream_lzma_send_flush(struct lzma_ostream *zstream)
b045cb4df9b00beb812f664c0f98344e743029acTimo Sirainen if ((ret = o_stream_flush_parent_if_needed(&zstream->ostream)) <= 0)
12983e9d3b4ebdfb1e14f197c153304b3af44b59Timo Sirainen if ((ret = o_stream_zlib_send_outbuf(zstream)) <= 0)
12983e9d3b4ebdfb1e14f197c153304b3af44b59Timo Sirainen "lzma.write(%s): Out of memory",
12983e9d3b4ebdfb1e14f197c153304b3af44b59Timo Sirainen o_stream_get_name(&zstream->ostream.ostream));
38ec64f181318ba87481fe820abba6caa86d1ca6Timo Sirainen i_panic("lzma.write(%s) flush failed with unexpected code %d",
38ec64f181318ba87481fe820abba6caa86d1ca6Timo Sirainen o_stream_get_name(&zstream->ostream.ostream), ret);
38ec64f181318ba87481fe820abba6caa86d1ca6Timo Sirainen len = sizeof(zstream->outbuf) - zs->avail_out;
38ec64f181318ba87481fe820abba6caa86d1ca6Timo Sirainen if ((ret = o_stream_zlib_send_outbuf(zstream)) <= 0)
12983e9d3b4ebdfb1e14f197c153304b3af44b59Timo Sirainenstatic int o_stream_lzma_flush(struct ostream_private *stream)
12983e9d3b4ebdfb1e14f197c153304b3af44b59Timo Sirainen struct lzma_ostream *zstream = (struct lzma_ostream *)stream;
12983e9d3b4ebdfb1e14f197c153304b3af44b59Timo Siraineno_stream_lzma_sendv(struct ostream_private *stream,
12983e9d3b4ebdfb1e14f197c153304b3af44b59Timo Sirainen const struct const_iovec *iov, unsigned int iov_count)
12983e9d3b4ebdfb1e14f197c153304b3af44b59Timo Sirainen struct lzma_ostream *zstream = (struct lzma_ostream *)stream;
12983e9d3b4ebdfb1e14f197c153304b3af44b59Timo Sirainen unsigned int i;
12983e9d3b4ebdfb1e14f197c153304b3af44b59Timo Sirainen if ((ret = o_stream_zlib_send_outbuf(zstream)) <= 0) {
12983e9d3b4ebdfb1e14f197c153304b3af44b59Timo Sirainen /* error / we still couldn't flush existing data to
12983e9d3b4ebdfb1e14f197c153304b3af44b59Timo Sirainen parent stream. */
12983e9d3b4ebdfb1e14f197c153304b3af44b59Timo Sirainen for (i = 0; i < iov_count; i++) {
12983e9d3b4ebdfb1e14f197c153304b3af44b59Timo Sirainen ret = o_stream_lzma_send_chunk(zstream, iov[i].iov_base,
12983e9d3b4ebdfb1e14f197c153304b3af44b59Timo Sirainen /* avail_in!=0 check is used to detect errors. if it's non-zero here
12983e9d3b4ebdfb1e14f197c153304b3af44b59Timo Sirainen it simply means we didn't send all the data */
12983e9d3b4ebdfb1e14f197c153304b3af44b59Timo Sirainenstruct ostream *o_stream_create_lzma(struct ostream *output, int level)
12983e9d3b4ebdfb1e14f197c153304b3af44b59Timo Sirainen zstream->ostream.iostream.close = o_stream_lzma_close;
12983e9d3b4ebdfb1e14f197c153304b3af44b59Timo Sirainen ret = lzma_easy_encoder(&zstream->strm, level, LZMA_CHECK_CRC64);
12983e9d3b4ebdfb1e14f197c153304b3af44b59Timo Sirainen i_fatal_status(FATAL_OUTOFMEM, "lzma: Out of memory");
12983e9d3b4ebdfb1e14f197c153304b3af44b59Timo Sirainen i_fatal("lzma_easy_encoder() failed with %d", ret);
12983e9d3b4ebdfb1e14f197c153304b3af44b59Timo Sirainen zstream->strm.avail_out = sizeof(zstream->outbuf);