]> git.ipfire.org Git - thirdparty/dovecot/core.git/commitdiff
lib-mail: istream-attachment-connector now allows msg_size=-1 for "unknown".
authorTimo Sirainen <timo.sirainen@dovecot.fi>
Tue, 1 Nov 2016 16:43:57 +0000 (18:43 +0200)
committerGitLab <gitlab@git.dovecot.net>
Wed, 22 Feb 2017 18:10:48 +0000 (20:10 +0200)
src/lib-mail/istream-attachment-connector.c
src/lib-mail/istream-attachment-connector.h
src/lib-mail/test-istream-attachment.c

index a4332d2d2445004ce5ffd7680ea89410e9a1764b..bcccf2ac324775c8ce2bc010404c1005c9769819 100644 (file)
@@ -119,12 +119,18 @@ istream_attachment_connector_finish(struct istream_attachment_connector **_conn)
        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);
index 9a0d53f282a841ea047d14d7a6e7deefa50839c7..bf54aecea28b15a56dab72dca3639bc2ba73bd4b 100644 (file)
@@ -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);
 
index effa1ad63507535f0cd34a01a779f80b41b4cbbd..9089046ab6c9e105cf96944800a63b373e4ebcf0 100644 (file)
@@ -248,7 +248,7 @@ static int test_input_stream(struct istream *file_input)
        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 @@ static int test_input_stream(struct istream *file_input)
                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 @@ static int test_input_stream(struct istream *file_input)
        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 @@ static int test_input_stream(struct istream *file_input)
                        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)