# HG changeset patch # User Timo Sirainen # Date 1478018637 -7200 # Node ID c00c65eb641a12055b7554439053b741b8c7fbe9 # Parent ce8b35e407124e70b7ef77754fe42c0f9b884c05 lib-mail: istream-attachment-connector now allows msg_size=-1 for "unknown". diff -r ce8b35e40712 -r c00c65eb641a src/lib-mail/istream-attachment-connector.c --- a/src/lib-mail/istream-attachment-connector.c Tue Nov 01 18:42:31 2016 +0200 +++ b/src/lib-mail/istream-attachment-connector.c Tue Nov 01 18:43:57 2016 +0200 @@ -119,12 +119,18 @@ if (conn->base_input_offset != conn->msg_size) { i_assert(conn->base_input_offset < conn->msg_size); - trailer_size = conn->msg_size - conn->encoded_offset; - input = i_stream_create_sized_range(conn->base_input, - conn->base_input_offset, - trailer_size); - i_stream_set_name(input, t_strdup_printf( - "%s trailer", i_stream_get_name(conn->base_input))); + if (conn->msg_size != (uoff_t)-1) { + trailer_size = conn->msg_size - conn->encoded_offset; + input = i_stream_create_sized_range(conn->base_input, + conn->base_input_offset, + trailer_size); + i_stream_set_name(input, t_strdup_printf( + "%s trailer", i_stream_get_name(conn->base_input))); + } else { + input = i_stream_create_range(conn->base_input, + conn->base_input_offset, + (uoff_t)-1); + } array_append(&conn->streams, &input, 1); } array_append_zero(&conn->streams); diff -r ce8b35e40712 -r c00c65eb641a src/lib-mail/istream-attachment-connector.h --- a/src/lib-mail/istream-attachment-connector.h Tue Nov 01 18:42:31 2016 +0200 +++ b/src/lib-mail/istream-attachment-connector.h Tue Nov 01 18:43:57 2016 +0200 @@ -2,7 +2,8 @@ #define ISTREAM_ATTACHMENT_CONNECTOR_H /* Start building a message stream. The base_input contains the message - without attachments. The final stream must be exactly msg_size bytes. */ + without attachments. The final stream must be exactly msg_size bytes. + If the original msg_size isn't known, it can be set to (uoff_t)-1. */ struct istream_attachment_connector * istream_attachment_connector_begin(struct istream *base_input, uoff_t msg_size); diff -r ce8b35e40712 -r c00c65eb641a src/lib-mail/test-istream-attachment.c --- a/src/lib-mail/test-istream-attachment.c Tue Nov 01 18:42:31 2016 +0200 +++ b/src/lib-mail/test-istream-attachment.c Tue Nov 01 18:43:57 2016 +0200 @@ -248,7 +248,7 @@ const unsigned char *data; size_t size; struct sha1_ctxt hash; - uoff_t msg_size; + uoff_t msg_size, orig_msg_size; buffer_t *base_buf; unsigned char hash_file[SHA1_RESULTLEN], hash_attached[SHA1_RESULTLEN]; int ret = 0; @@ -261,7 +261,7 @@ i_stream_skip(input, size); } sha1_result(&hash, hash_file); - msg_size = input->v_offset; + msg_size = orig_msg_size = input->v_offset; i_stream_unref(&input); /* read through attachment extractor */ @@ -279,7 +279,7 @@ i_stream_unref(&input2); /* rebuild the original stream and see if the hash matches */ - { + for (unsigned int i = 0; i < 2; i++) { input2 = i_stream_create_from_data(base_buf->data, base_buf->used); input = test_build_original_istream(input2, msg_size); i_stream_unref(&input2); @@ -289,18 +289,21 @@ sha1_loop(&hash, data, size); i_stream_skip(input, size); } - test_assert(input->eof && input->stream_errno == 0); + test_assert_idx(input->eof && input->stream_errno == 0, i); sha1_result(&hash, hash_attached); i_stream_unref(&input); if (memcmp(hash_file, hash_attached, SHA1_RESULTLEN) != 0) ret = -1; + + /* try again without knowing the message's size */ + msg_size = (uoff_t)-1; } /* try with a wrong message size */ for (int i = 0; i < 2; i++) { input2 = i_stream_create_from_data(base_buf->data, base_buf->used); - input = test_build_original_istream(input2, msg_size + + input = test_build_original_istream(input2, orig_msg_size + (i == 0 ? 1 : -1)); i_stream_unref(&input2); while (i_stream_read_more(input, &data, &size) > 0)