istream-chain.c revision d730192e34fbedbc590a5abc7351e5af5e120c5f
a11689fe3fbb3bca11b9cb4ae5faf27db96401ccTimo Sirainen/* Copyright (c) 2003-2013 Dovecot authors, see the included COPYING file */
a64adf62fa33f2463a86f990217b0c9078531a40Timo Sirainen
a64adf62fa33f2463a86f990217b0c9078531a40Timo Sirainen#include "lib.h"
a64adf62fa33f2463a86f990217b0c9078531a40Timo Sirainen#include "llist.h"
a64adf62fa33f2463a86f990217b0c9078531a40Timo Sirainen#include "istream-private.h"
a64adf62fa33f2463a86f990217b0c9078531a40Timo Sirainen#include "istream-chain.h"
a64adf62fa33f2463a86f990217b0c9078531a40Timo Sirainen
a64adf62fa33f2463a86f990217b0c9078531a40Timo Sirainenstruct chain_istream;
a64adf62fa33f2463a86f990217b0c9078531a40Timo Sirainen
ec1a4f4306496380e9d96ee08a3718a669d0875aTimo Sirainenstruct istream_chain_link {
1358e2c58ce29231485a5cfa454756d429ad3d2cTimo Sirainen struct istream_chain_link *prev, *next;
a64adf62fa33f2463a86f990217b0c9078531a40Timo Sirainen
a64adf62fa33f2463a86f990217b0c9078531a40Timo Sirainen struct istream *stream;
a64adf62fa33f2463a86f990217b0c9078531a40Timo Sirainen bool eof;
a64adf62fa33f2463a86f990217b0c9078531a40Timo Sirainen};
a78d5bd9772681a232de56b3dd6acefee66cc71bTimo Sirainen
a78d5bd9772681a232de56b3dd6acefee66cc71bTimo Sirainenstruct istream_chain {
a78d5bd9772681a232de56b3dd6acefee66cc71bTimo Sirainen struct istream_chain_link *head, *tail;
a64adf62fa33f2463a86f990217b0c9078531a40Timo Sirainen
4e0204dbac352e271c79cfa6c74a71145a058de5Timo Sirainen struct chain_istream *stream;
4e0204dbac352e271c79cfa6c74a71145a058de5Timo Sirainen};
4e0204dbac352e271c79cfa6c74a71145a058de5Timo Sirainen
4e0204dbac352e271c79cfa6c74a71145a058de5Timo Sirainenstruct chain_istream {
a64adf62fa33f2463a86f990217b0c9078531a40Timo Sirainen struct istream_private istream;
a64adf62fa33f2463a86f990217b0c9078531a40Timo Sirainen
637ec4c33b4715737a41f7e58c9b6d1f693c27e2Timo Sirainen size_t prev_stream_left, prev_skip;
637ec4c33b4715737a41f7e58c9b6d1f693c27e2Timo Sirainen
637ec4c33b4715737a41f7e58c9b6d1f693c27e2Timo Sirainen struct istream_chain chain;
1358e2c58ce29231485a5cfa454756d429ad3d2cTimo Sirainen};
1cfdcb36985904eff281fc6d7ea2d13b3c375980Timo Sirainen
637ec4c33b4715737a41f7e58c9b6d1f693c27e2Timo Sirainenstatic void ATTR_NULL(2)
1358e2c58ce29231485a5cfa454756d429ad3d2cTimo Siraineni_stream_chain_append_internal(struct istream_chain *chain,
1358e2c58ce29231485a5cfa454756d429ad3d2cTimo Sirainen struct istream *stream)
1358e2c58ce29231485a5cfa454756d429ad3d2cTimo Sirainen{
d595049948579def2d82718dbce0a6b49a281402Timo Sirainen struct istream_chain_link *link;
d595049948579def2d82718dbce0a6b49a281402Timo Sirainen
d595049948579def2d82718dbce0a6b49a281402Timo Sirainen if (stream == NULL && chain->tail != NULL && chain->tail->stream == NULL)
d595049948579def2d82718dbce0a6b49a281402Timo Sirainen return;
d595049948579def2d82718dbce0a6b49a281402Timo Sirainen
d595049948579def2d82718dbce0a6b49a281402Timo Sirainen link = i_new(struct istream_chain_link, 1);
d595049948579def2d82718dbce0a6b49a281402Timo Sirainen link->stream = stream;
d595049948579def2d82718dbce0a6b49a281402Timo Sirainen link->eof = stream == NULL;
1358e2c58ce29231485a5cfa454756d429ad3d2cTimo Sirainen
1358e2c58ce29231485a5cfa454756d429ad3d2cTimo Sirainen if (stream != NULL)
1358e2c58ce29231485a5cfa454756d429ad3d2cTimo Sirainen i_stream_ref(stream);
1358e2c58ce29231485a5cfa454756d429ad3d2cTimo Sirainen
637ec4c33b4715737a41f7e58c9b6d1f693c27e2Timo Sirainen if (chain->head == NULL && stream != NULL) {
94163c620a8880024d0e8bbb503c788159fa222bTimo Sirainen if (chain->stream->istream.max_buffer_size == 0) {
637ec4c33b4715737a41f7e58c9b6d1f693c27e2Timo Sirainen chain->stream->istream.max_buffer_size =
1358e2c58ce29231485a5cfa454756d429ad3d2cTimo Sirainen stream->real_stream->max_buffer_size;
1358e2c58ce29231485a5cfa454756d429ad3d2cTimo Sirainen } else {
1358e2c58ce29231485a5cfa454756d429ad3d2cTimo Sirainen i_stream_set_max_buffer_size(stream,
1358e2c58ce29231485a5cfa454756d429ad3d2cTimo Sirainen chain->stream->istream.max_buffer_size);
1358e2c58ce29231485a5cfa454756d429ad3d2cTimo Sirainen }
637ec4c33b4715737a41f7e58c9b6d1f693c27e2Timo Sirainen }
637ec4c33b4715737a41f7e58c9b6d1f693c27e2Timo Sirainen DLLIST2_APPEND(&chain->head, &chain->tail, link);
637ec4c33b4715737a41f7e58c9b6d1f693c27e2Timo Sirainen}
637ec4c33b4715737a41f7e58c9b6d1f693c27e2Timo Sirainen
94163c620a8880024d0e8bbb503c788159fa222bTimo Sirainenvoid i_stream_chain_append(struct istream_chain *chain, struct istream *stream)
1358e2c58ce29231485a5cfa454756d429ad3d2cTimo Sirainen{
1358e2c58ce29231485a5cfa454756d429ad3d2cTimo Sirainen i_stream_chain_append_internal(chain, stream);
28cb56e6957f06717e876cecb7aabc820fdf632eTimo Sirainen}
28cb56e6957f06717e876cecb7aabc820fdf632eTimo Sirainen
28cb56e6957f06717e876cecb7aabc820fdf632eTimo Sirainenvoid i_stream_chain_append_eof(struct istream_chain *chain)
28cb56e6957f06717e876cecb7aabc820fdf632eTimo Sirainen{
28cb56e6957f06717e876cecb7aabc820fdf632eTimo Sirainen i_stream_chain_append_internal(chain, NULL);
28cb56e6957f06717e876cecb7aabc820fdf632eTimo Sirainen}
28cb56e6957f06717e876cecb7aabc820fdf632eTimo Sirainen
28cb56e6957f06717e876cecb7aabc820fdf632eTimo Sirainenstatic void
28cb56e6957f06717e876cecb7aabc820fdf632eTimo Siraineni_stream_chain_set_max_buffer_size(struct iostream_private *stream,
28cb56e6957f06717e876cecb7aabc820fdf632eTimo Sirainen size_t max_size)
28cb56e6957f06717e876cecb7aabc820fdf632eTimo Sirainen{
28cb56e6957f06717e876cecb7aabc820fdf632eTimo Sirainen struct chain_istream *cstream = (struct chain_istream *)stream;
28cb56e6957f06717e876cecb7aabc820fdf632eTimo Sirainen struct istream_chain_link *link = cstream->chain.head;
28cb56e6957f06717e876cecb7aabc820fdf632eTimo Sirainen
28cb56e6957f06717e876cecb7aabc820fdf632eTimo Sirainen cstream->istream.max_buffer_size = max_size;
28cb56e6957f06717e876cecb7aabc820fdf632eTimo Sirainen while (link != NULL) {
28cb56e6957f06717e876cecb7aabc820fdf632eTimo Sirainen if (link->stream != NULL)
28cb56e6957f06717e876cecb7aabc820fdf632eTimo Sirainen i_stream_set_max_buffer_size(link->stream, max_size);
28cb56e6957f06717e876cecb7aabc820fdf632eTimo Sirainen link = link->next;
28cb56e6957f06717e876cecb7aabc820fdf632eTimo Sirainen }
28cb56e6957f06717e876cecb7aabc820fdf632eTimo Sirainen}
28cb56e6957f06717e876cecb7aabc820fdf632eTimo Sirainen
28cb56e6957f06717e876cecb7aabc820fdf632eTimo Sirainenstatic void i_stream_chain_destroy(struct iostream_private *stream)
28cb56e6957f06717e876cecb7aabc820fdf632eTimo Sirainen{
28cb56e6957f06717e876cecb7aabc820fdf632eTimo Sirainen struct chain_istream *cstream = (struct chain_istream *)stream;
28cb56e6957f06717e876cecb7aabc820fdf632eTimo Sirainen struct istream_chain_link *link = cstream->chain.head;
28cb56e6957f06717e876cecb7aabc820fdf632eTimo Sirainen
28cb56e6957f06717e876cecb7aabc820fdf632eTimo Sirainen while (link != NULL) {
28cb56e6957f06717e876cecb7aabc820fdf632eTimo Sirainen struct istream_chain_link *next = link->next;
28cb56e6957f06717e876cecb7aabc820fdf632eTimo Sirainen
28cb56e6957f06717e876cecb7aabc820fdf632eTimo Sirainen if (link->stream != NULL)
28cb56e6957f06717e876cecb7aabc820fdf632eTimo Sirainen i_stream_unref(&link->stream);
28cb56e6957f06717e876cecb7aabc820fdf632eTimo Sirainen i_free(link);
28cb56e6957f06717e876cecb7aabc820fdf632eTimo Sirainen link = next;
28cb56e6957f06717e876cecb7aabc820fdf632eTimo Sirainen }
28cb56e6957f06717e876cecb7aabc820fdf632eTimo Sirainen}
28cb56e6957f06717e876cecb7aabc820fdf632eTimo Sirainen
28cb56e6957f06717e876cecb7aabc820fdf632eTimo Sirainenstatic void i_stream_chain_read_next(struct chain_istream *cstream)
3c3777721b56e065fac99a0f34e4cef4f293b517Timo Sirainen{
637ec4c33b4715737a41f7e58c9b6d1f693c27e2Timo Sirainen struct istream_chain_link *link = cstream->chain.head;
637ec4c33b4715737a41f7e58c9b6d1f693c27e2Timo Sirainen struct istream *prev_input;
637ec4c33b4715737a41f7e58c9b6d1f693c27e2Timo Sirainen const unsigned char *data;
a64adf62fa33f2463a86f990217b0c9078531a40Timo Sirainen size_t data_size, size;
94163c620a8880024d0e8bbb503c788159fa222bTimo Sirainen
a64adf62fa33f2463a86f990217b0c9078531a40Timo Sirainen i_assert(link != NULL && link->stream != NULL);
a64adf62fa33f2463a86f990217b0c9078531a40Timo Sirainen i_assert(link->stream->eof);
a64adf62fa33f2463a86f990217b0c9078531a40Timo Sirainen
1cfdcb36985904eff281fc6d7ea2d13b3c375980Timo Sirainen prev_input = link->stream;
a64adf62fa33f2463a86f990217b0c9078531a40Timo Sirainen data = i_stream_get_data(prev_input, &data_size);
a64adf62fa33f2463a86f990217b0c9078531a40Timo Sirainen
a64adf62fa33f2463a86f990217b0c9078531a40Timo Sirainen DLLIST2_REMOVE(&cstream->chain.head, &cstream->chain.tail, link);
28cb56e6957f06717e876cecb7aabc820fdf632eTimo Sirainen i_free(link);
28cb56e6957f06717e876cecb7aabc820fdf632eTimo Sirainen
3c3777721b56e065fac99a0f34e4cef4f293b517Timo Sirainen /* a) we have more streams, b) we have EOF, c) we need to wait
3c3777721b56e065fac99a0f34e4cef4f293b517Timo Sirainen for more streams */
3c3777721b56e065fac99a0f34e4cef4f293b517Timo Sirainen link = cstream->chain.head;
3c3777721b56e065fac99a0f34e4cef4f293b517Timo Sirainen if (link != NULL && link->stream != NULL)
a64adf62fa33f2463a86f990217b0c9078531a40Timo Sirainen i_stream_seek(link->stream, 0);
3c3777721b56e065fac99a0f34e4cef4f293b517Timo Sirainen
3c3777721b56e065fac99a0f34e4cef4f293b517Timo Sirainen if (cstream->istream.buffer == cstream->istream.w_buffer) {
3c3777721b56e065fac99a0f34e4cef4f293b517Timo Sirainen /* we've already buffered the prev_input */
3c3777721b56e065fac99a0f34e4cef4f293b517Timo Sirainen } else {
3c3777721b56e065fac99a0f34e4cef4f293b517Timo Sirainen /* we already verified that the data size is less than the
3c3777721b56e065fac99a0f34e4cef4f293b517Timo Sirainen maximum buffer size */
1358e2c58ce29231485a5cfa454756d429ad3d2cTimo Sirainen cstream->istream.pos = 0;
1358e2c58ce29231485a5cfa454756d429ad3d2cTimo Sirainen if (data_size > 0) {
bbc30fd4fa86723f6a72309ad3a2a96f34eabd6cTimo Sirainen if (!i_stream_try_alloc(&cstream->istream, data_size, &size))
bbc30fd4fa86723f6a72309ad3a2a96f34eabd6cTimo Sirainen i_unreached();
bbc30fd4fa86723f6a72309ad3a2a96f34eabd6cTimo Sirainen i_assert(size >= data_size);
bbc30fd4fa86723f6a72309ad3a2a96f34eabd6cTimo Sirainen }
bbc30fd4fa86723f6a72309ad3a2a96f34eabd6cTimo Sirainen memcpy(cstream->istream.w_buffer, data, data_size);
bbc30fd4fa86723f6a72309ad3a2a96f34eabd6cTimo Sirainen cstream->istream.skip = 0;
bbc30fd4fa86723f6a72309ad3a2a96f34eabd6cTimo Sirainen cstream->istream.pos = data_size;
bbc30fd4fa86723f6a72309ad3a2a96f34eabd6cTimo Sirainen cstream->prev_stream_left = data_size;
bbc30fd4fa86723f6a72309ad3a2a96f34eabd6cTimo Sirainen }
bbc30fd4fa86723f6a72309ad3a2a96f34eabd6cTimo Sirainen i_stream_skip(prev_input, data_size);
bbc30fd4fa86723f6a72309ad3a2a96f34eabd6cTimo Sirainen i_stream_unref(&prev_input);
bbc30fd4fa86723f6a72309ad3a2a96f34eabd6cTimo Sirainen}
bbc30fd4fa86723f6a72309ad3a2a96f34eabd6cTimo Sirainen
bbc30fd4fa86723f6a72309ad3a2a96f34eabd6cTimo Sirainenstatic ssize_t i_stream_chain_read(struct istream_private *stream)
bbc30fd4fa86723f6a72309ad3a2a96f34eabd6cTimo Sirainen{
94163c620a8880024d0e8bbb503c788159fa222bTimo Sirainen struct chain_istream *cstream = (struct chain_istream *)stream;
637ec4c33b4715737a41f7e58c9b6d1f693c27e2Timo Sirainen struct istream_chain_link *link = cstream->chain.head;
1358e2c58ce29231485a5cfa454756d429ad3d2cTimo Sirainen const unsigned char *data;
94163c620a8880024d0e8bbb503c788159fa222bTimo Sirainen size_t size, data_size, cur_data_pos, new_pos, bytes_skipped;
1358e2c58ce29231485a5cfa454756d429ad3d2cTimo Sirainen size_t new_bytes_count;
1358e2c58ce29231485a5cfa454756d429ad3d2cTimo Sirainen ssize_t ret;
1cfdcb36985904eff281fc6d7ea2d13b3c375980Timo Sirainen
1358e2c58ce29231485a5cfa454756d429ad3d2cTimo Sirainen if (link != NULL && link->eof) {
94163c620a8880024d0e8bbb503c788159fa222bTimo Sirainen stream->istream.eof = TRUE;
1358e2c58ce29231485a5cfa454756d429ad3d2cTimo Sirainen return -1;
1cfdcb36985904eff281fc6d7ea2d13b3c375980Timo Sirainen }
1cfdcb36985904eff281fc6d7ea2d13b3c375980Timo Sirainen
637ec4c33b4715737a41f7e58c9b6d1f693c27e2Timo Sirainen i_assert(stream->skip >= cstream->prev_skip);
1358e2c58ce29231485a5cfa454756d429ad3d2cTimo Sirainen bytes_skipped = stream->skip - cstream->prev_skip;
1358e2c58ce29231485a5cfa454756d429ad3d2cTimo Sirainen
1358e2c58ce29231485a5cfa454756d429ad3d2cTimo Sirainen if (cstream->prev_stream_left == 0) {
1358e2c58ce29231485a5cfa454756d429ad3d2cTimo Sirainen /* no need to worry about buffers, skip everything */
637ec4c33b4715737a41f7e58c9b6d1f693c27e2Timo Sirainen } else if (bytes_skipped < cstream->prev_stream_left) {
1358e2c58ce29231485a5cfa454756d429ad3d2cTimo Sirainen /* we're still skipping inside buffer */
1358e2c58ce29231485a5cfa454756d429ad3d2cTimo Sirainen cstream->prev_stream_left -= bytes_skipped;
637ec4c33b4715737a41f7e58c9b6d1f693c27e2Timo Sirainen bytes_skipped = 0;
637ec4c33b4715737a41f7e58c9b6d1f693c27e2Timo Sirainen } else {
1358e2c58ce29231485a5cfa454756d429ad3d2cTimo Sirainen /* done with the buffer */
637ec4c33b4715737a41f7e58c9b6d1f693c27e2Timo Sirainen bytes_skipped -= cstream->prev_stream_left;
637ec4c33b4715737a41f7e58c9b6d1f693c27e2Timo Sirainen cstream->prev_stream_left = 0;
04ce187ba9c7c98b4f3fbaa833a2dc929e4281b8Timo Sirainen }
04ce187ba9c7c98b4f3fbaa833a2dc929e4281b8Timo Sirainen stream->pos -= bytes_skipped;
04ce187ba9c7c98b4f3fbaa833a2dc929e4281b8Timo Sirainen stream->skip -= bytes_skipped;
04ce187ba9c7c98b4f3fbaa833a2dc929e4281b8Timo Sirainen stream->buffer += bytes_skipped;
04ce187ba9c7c98b4f3fbaa833a2dc929e4281b8Timo Sirainen cstream->prev_skip = stream->skip;
04ce187ba9c7c98b4f3fbaa833a2dc929e4281b8Timo Sirainen
04ce187ba9c7c98b4f3fbaa833a2dc929e4281b8Timo Sirainen if (link == NULL) {
04ce187ba9c7c98b4f3fbaa833a2dc929e4281b8Timo Sirainen i_assert(bytes_skipped == 0);
04ce187ba9c7c98b4f3fbaa833a2dc929e4281b8Timo Sirainen return 0;
637ec4c33b4715737a41f7e58c9b6d1f693c27e2Timo Sirainen }
637ec4c33b4715737a41f7e58c9b6d1f693c27e2Timo Sirainen i_stream_skip(link->stream, bytes_skipped);
637ec4c33b4715737a41f7e58c9b6d1f693c27e2Timo Sirainen
1358e2c58ce29231485a5cfa454756d429ad3d2cTimo Sirainen i_assert(stream->pos >= stream->skip + cstream->prev_stream_left);
637ec4c33b4715737a41f7e58c9b6d1f693c27e2Timo Sirainen cur_data_pos = stream->pos - (stream->skip + cstream->prev_stream_left);
637ec4c33b4715737a41f7e58c9b6d1f693c27e2Timo Sirainen
1358e2c58ce29231485a5cfa454756d429ad3d2cTimo Sirainen data = i_stream_get_data(link->stream, &data_size);
637ec4c33b4715737a41f7e58c9b6d1f693c27e2Timo Sirainen if (data_size > cur_data_pos)
637ec4c33b4715737a41f7e58c9b6d1f693c27e2Timo Sirainen ret = 0;
637ec4c33b4715737a41f7e58c9b6d1f693c27e2Timo Sirainen else {
637ec4c33b4715737a41f7e58c9b6d1f693c27e2Timo Sirainen /* need to read more */
637ec4c33b4715737a41f7e58c9b6d1f693c27e2Timo Sirainen i_assert(cur_data_pos == data_size);
637ec4c33b4715737a41f7e58c9b6d1f693c27e2Timo Sirainen ret = i_stream_read(link->stream);
637ec4c33b4715737a41f7e58c9b6d1f693c27e2Timo Sirainen if (ret == -2 || ret == 0)
637ec4c33b4715737a41f7e58c9b6d1f693c27e2Timo Sirainen return ret;
637ec4c33b4715737a41f7e58c9b6d1f693c27e2Timo Sirainen
637ec4c33b4715737a41f7e58c9b6d1f693c27e2Timo Sirainen if (ret == -1) {
1358e2c58ce29231485a5cfa454756d429ad3d2cTimo Sirainen if (link->stream->stream_errno != 0) {
1358e2c58ce29231485a5cfa454756d429ad3d2cTimo Sirainen stream->istream.stream_errno =
637ec4c33b4715737a41f7e58c9b6d1f693c27e2Timo Sirainen link->stream->stream_errno;
637ec4c33b4715737a41f7e58c9b6d1f693c27e2Timo Sirainen return -1;
637ec4c33b4715737a41f7e58c9b6d1f693c27e2Timo Sirainen }
1358e2c58ce29231485a5cfa454756d429ad3d2cTimo Sirainen /* EOF of this stream, go to next stream */
637ec4c33b4715737a41f7e58c9b6d1f693c27e2Timo Sirainen i_stream_chain_read_next(cstream);
637ec4c33b4715737a41f7e58c9b6d1f693c27e2Timo Sirainen cstream->prev_skip = stream->skip;
1358e2c58ce29231485a5cfa454756d429ad3d2cTimo Sirainen return i_stream_chain_read(stream);
637ec4c33b4715737a41f7e58c9b6d1f693c27e2Timo Sirainen }
637ec4c33b4715737a41f7e58c9b6d1f693c27e2Timo Sirainen /* we read something */
637ec4c33b4715737a41f7e58c9b6d1f693c27e2Timo Sirainen data = i_stream_get_data(link->stream, &data_size);
637ec4c33b4715737a41f7e58c9b6d1f693c27e2Timo Sirainen }
637ec4c33b4715737a41f7e58c9b6d1f693c27e2Timo Sirainen
637ec4c33b4715737a41f7e58c9b6d1f693c27e2Timo Sirainen if (cstream->prev_stream_left == 0) {
1358e2c58ce29231485a5cfa454756d429ad3d2cTimo Sirainen /* we can point directly to the current stream's buffers */
1358e2c58ce29231485a5cfa454756d429ad3d2cTimo Sirainen stream->buffer = data;
94163c620a8880024d0e8bbb503c788159fa222bTimo Sirainen stream->pos -= stream->skip;
637ec4c33b4715737a41f7e58c9b6d1f693c27e2Timo Sirainen stream->skip = 0;
94163c620a8880024d0e8bbb503c788159fa222bTimo Sirainen new_pos = data_size;
94163c620a8880024d0e8bbb503c788159fa222bTimo Sirainen } else if (data_size == cur_data_pos) {
94163c620a8880024d0e8bbb503c788159fa222bTimo Sirainen /* nothing new read */
637ec4c33b4715737a41f7e58c9b6d1f693c27e2Timo Sirainen i_assert(ret == 0 || ret == -1);
dbd93ca6319a01fdea82853e8b4bd4b226d796f0Timo Sirainen stream->buffer = stream->w_buffer;
637ec4c33b4715737a41f7e58c9b6d1f693c27e2Timo Sirainen new_pos = stream->pos;
94163c620a8880024d0e8bbb503c788159fa222bTimo Sirainen } else {
94163c620a8880024d0e8bbb503c788159fa222bTimo Sirainen /* we still have some of the previous stream left. merge the
dbd93ca6319a01fdea82853e8b4bd4b226d796f0Timo Sirainen new data with it. */
dbd93ca6319a01fdea82853e8b4bd4b226d796f0Timo Sirainen i_assert(data_size > cur_data_pos);
dbd93ca6319a01fdea82853e8b4bd4b226d796f0Timo Sirainen new_bytes_count = data_size - cur_data_pos;
dbd93ca6319a01fdea82853e8b4bd4b226d796f0Timo Sirainen if (!i_stream_try_alloc(stream, new_bytes_count, &size)) {
94163c620a8880024d0e8bbb503c788159fa222bTimo Sirainen stream->buffer = stream->w_buffer;
dbd93ca6319a01fdea82853e8b4bd4b226d796f0Timo Sirainen return -2;
dbd93ca6319a01fdea82853e8b4bd4b226d796f0Timo Sirainen }
dbd93ca6319a01fdea82853e8b4bd4b226d796f0Timo Sirainen stream->buffer = stream->w_buffer;
dbd93ca6319a01fdea82853e8b4bd4b226d796f0Timo Sirainen
dbd93ca6319a01fdea82853e8b4bd4b226d796f0Timo Sirainen if (new_bytes_count > size)
dbd93ca6319a01fdea82853e8b4bd4b226d796f0Timo Sirainen new_bytes_count = size;
94163c620a8880024d0e8bbb503c788159fa222bTimo Sirainen memcpy(stream->w_buffer + stream->pos,
dbd93ca6319a01fdea82853e8b4bd4b226d796f0Timo Sirainen data + cur_data_pos, new_bytes_count);
dbd93ca6319a01fdea82853e8b4bd4b226d796f0Timo Sirainen new_pos = stream->pos + new_bytes_count;
dbd93ca6319a01fdea82853e8b4bd4b226d796f0Timo Sirainen }
dbd93ca6319a01fdea82853e8b4bd4b226d796f0Timo Sirainen
dbd93ca6319a01fdea82853e8b4bd4b226d796f0Timo Sirainen ret = new_pos > stream->pos ? (ssize_t)(new_pos - stream->pos) :
dbd93ca6319a01fdea82853e8b4bd4b226d796f0Timo Sirainen (ret == 0 ? 0 : -1);
94163c620a8880024d0e8bbb503c788159fa222bTimo Sirainen stream->pos = new_pos;
dbd93ca6319a01fdea82853e8b4bd4b226d796f0Timo Sirainen cstream->prev_skip = stream->skip;
dbd93ca6319a01fdea82853e8b4bd4b226d796f0Timo Sirainen return ret;
dbd93ca6319a01fdea82853e8b4bd4b226d796f0Timo Sirainen}
dbd93ca6319a01fdea82853e8b4bd4b226d796f0Timo Sirainen
94163c620a8880024d0e8bbb503c788159fa222bTimo Sirainenstruct istream *i_stream_create_chain(struct istream_chain **chain_r)
dbd93ca6319a01fdea82853e8b4bd4b226d796f0Timo Sirainen{
dbd93ca6319a01fdea82853e8b4bd4b226d796f0Timo Sirainen struct chain_istream *cstream;
dbd93ca6319a01fdea82853e8b4bd4b226d796f0Timo Sirainen
dbd93ca6319a01fdea82853e8b4bd4b226d796f0Timo Sirainen cstream = i_new(struct chain_istream, 1);
dbd93ca6319a01fdea82853e8b4bd4b226d796f0Timo Sirainen cstream->chain.stream = cstream;
dbd93ca6319a01fdea82853e8b4bd4b226d796f0Timo Sirainen cstream->istream.max_buffer_size = 256;
94163c620a8880024d0e8bbb503c788159fa222bTimo Sirainen
94163c620a8880024d0e8bbb503c788159fa222bTimo Sirainen cstream->istream.iostream.destroy = i_stream_chain_destroy;
94163c620a8880024d0e8bbb503c788159fa222bTimo Sirainen cstream->istream.iostream.set_max_buffer_size =
94163c620a8880024d0e8bbb503c788159fa222bTimo Sirainen i_stream_chain_set_max_buffer_size;
637ec4c33b4715737a41f7e58c9b6d1f693c27e2Timo Sirainen
637ec4c33b4715737a41f7e58c9b6d1f693c27e2Timo Sirainen cstream->istream.read = i_stream_chain_read;
637ec4c33b4715737a41f7e58c9b6d1f693c27e2Timo Sirainen
637ec4c33b4715737a41f7e58c9b6d1f693c27e2Timo Sirainen cstream->istream.istream.readable_fd = FALSE;
637ec4c33b4715737a41f7e58c9b6d1f693c27e2Timo Sirainen cstream->istream.istream.blocking = FALSE;
94163c620a8880024d0e8bbb503c788159fa222bTimo Sirainen cstream->istream.istream.seekable = FALSE;
94163c620a8880024d0e8bbb503c788159fa222bTimo Sirainen
94163c620a8880024d0e8bbb503c788159fa222bTimo Sirainen *chain_r = &cstream->chain;
ec1a4f4306496380e9d96ee08a3718a669d0875aTimo Sirainen return i_stream_create(&cstream->istream, NULL, -1);
637ec4c33b4715737a41f7e58c9b6d1f693c27e2Timo Sirainen}
637ec4c33b4715737a41f7e58c9b6d1f693c27e2Timo Sirainen