1 /* Copyright (c) 2003-2018 Dovecot authors, see the included COPYING file */
2 
3 #include "lib.h"
4 #include "istream-private.h"
5 #include "istream-sized.h"
6 
7 struct sized_istream {
8 	struct istream_private istream;
9 
10 	istream_sized_callback_t *error_callback;
11 	void *error_context;
12 
13 	uoff_t size;
14 	bool min_size_only;
15 };
16 
i_stream_sized_destroy(struct iostream_private * stream)17 static void i_stream_sized_destroy(struct iostream_private *stream)
18 {
19 	struct sized_istream *sstream =
20 		container_of(stream, struct sized_istream, istream.iostream);
21 	uoff_t v_offset;
22 
23 	v_offset = sstream->istream.parent_start_offset +
24 		sstream->istream.istream.v_offset;
25 	if (sstream->istream.parent->seekable ||
26 	    v_offset > sstream->istream.parent->v_offset) {
27 		/* get to same position in parent stream */
28 		i_stream_seek(sstream->istream.parent, v_offset);
29 	}
30 }
31 
32 static const char *
i_stream_create_sized_default_error_callback(const struct istream_sized_error_data * data,void * context ATTR_UNUSED)33 i_stream_create_sized_default_error_callback(
34 	const struct istream_sized_error_data *data, void *context ATTR_UNUSED)
35 {
36 	if (data->v_offset + data->new_bytes < data->wanted_size) {
37 		return t_strdup_printf("Stream is smaller than expected "
38 			"(%"PRIuUOFF_T" < %"PRIuUOFF_T")",
39 			data->v_offset + data->new_bytes, data->wanted_size);
40 	} else {
41 		return t_strdup_printf("Stream is larger than expected "
42 			"(%"PRIuUOFF_T" > %"PRIuUOFF_T", eof=%d)",
43 			data->v_offset + data->new_bytes, data->wanted_size,
44 			data->eof ? 1 : 0);
45 	}
46 }
47 
48 static ssize_t
i_stream_sized_parent_read(struct istream_private * stream,size_t * pos_r)49 i_stream_sized_parent_read(struct istream_private *stream, size_t *pos_r)
50 {
51 	ssize_t ret;
52 
53 	do {
54 		ret = i_stream_read_memarea(stream->parent);
55 		stream->istream.stream_errno = stream->parent->stream_errno;
56 		stream->istream.eof = stream->parent->eof;
57 		stream->buffer = i_stream_get_data(stream->parent, pos_r);
58 	} while (*pos_r <= stream->pos && ret > 0);
59 	return ret;
60 }
61 
i_stream_sized_read(struct istream_private * stream)62 static ssize_t i_stream_sized_read(struct istream_private *stream)
63 {
64 	struct sized_istream *sstream =
65 		container_of(stream, struct sized_istream, istream);
66 	struct istream_sized_error_data data;
67 	const char *error;
68 	uoff_t left;
69 	ssize_t ret;
70 	size_t pos;
71 
72 	i_stream_seek(stream->parent, sstream->istream.parent_start_offset +
73 		      stream->istream.v_offset);
74 
75 	stream->pos -= stream->skip;
76 	stream->skip = 0;
77 
78 	stream->buffer = i_stream_get_data(stream->parent, &pos);
79 	if (pos > stream->pos)
80 		ret = 0;
81 	else {
82 		if ((ret = i_stream_sized_parent_read(stream, &pos)) == -2)
83 			return -2;
84 	}
85 
86 	left = sstream->size - stream->istream.v_offset;
87 	if (pos == left && ret != -1) {
88 		/* we have exactly the wanted amount of data left, but we
89 		   don't know yet if there is more data in parent. */
90 		ret = i_stream_sized_parent_read(stream, &pos);
91 	}
92 
93 	i_zero(&data);
94 	data.v_offset = stream->istream.v_offset;
95 	data.new_bytes = pos;
96 	data.wanted_size = sstream->size;
97 	data.eof = stream->istream.eof;
98 
99 	if (pos == left) {
100 		/* we may or may not be finished, depending on whether
101 		   parent is at EOF. */
102 	} else if (pos > left) {
103 		/* parent has more data available than expected */
104 		if (!sstream->min_size_only) {
105 			error = sstream->error_callback(&data, sstream->error_context);
106 			io_stream_set_error(&stream->iostream, "%s", error);
107 			stream->istream.stream_errno = EINVAL;
108 			return -1;
109 		}
110 		pos = left;
111 		if (pos <= stream->pos) {
112 			stream->istream.eof = TRUE;
113 			ret = -1;
114 		}
115 	} else if (!stream->istream.eof) {
116 		/* still more to read */
117 	} else if (stream->istream.stream_errno == ENOENT) {
118 		/* lost the file */
119 	} else {
120 		/* EOF before we reached the wanted size */
121 		error = sstream->error_callback(&data, sstream->error_context);
122 		io_stream_set_error(&stream->iostream, "%s", error);
123 		stream->istream.stream_errno = EPIPE;
124 	}
125 
126 	ret = pos > stream->pos ? (ssize_t)(pos - stream->pos) :
127 		(ret == 0 ? 0 : -1);
128 	stream->pos = pos;
129 	i_assert(ret != -1 || stream->istream.eof ||
130 		 stream->istream.stream_errno != 0);
131 	return ret;
132 }
133 
134 static int
i_stream_sized_stat(struct istream_private * stream,bool exact ATTR_UNUSED)135 i_stream_sized_stat(struct istream_private *stream, bool exact ATTR_UNUSED)
136 {
137 	struct sized_istream *sstream =
138 		container_of(stream, struct sized_istream, istream);
139 	const struct stat *st;
140 
141 	/* parent stream may be base64-decoder. don't waste time decoding the
142 	   entire stream, since we already know what the size is supposed
143 	   to be. */
144 	if (i_stream_stat(stream->parent, FALSE, &st) < 0) {
145 		stream->istream.stream_errno = stream->parent->stream_errno;
146 		return -1;
147 	}
148 
149 	stream->statbuf = *st;
150 	stream->statbuf.st_size = sstream->size;
151 	return 0;
152 }
153 
154 static struct sized_istream *
i_stream_create_sized_common(struct istream * input,uoff_t size)155 i_stream_create_sized_common(struct istream *input, uoff_t size)
156 {
157 	struct sized_istream *sstream;
158 
159 	sstream = i_new(struct sized_istream, 1);
160 	sstream->size = size;
161 	sstream->istream.max_buffer_size = input->real_stream->max_buffer_size;
162 
163 	sstream->istream.iostream.destroy = i_stream_sized_destroy;
164 	sstream->istream.read = i_stream_sized_read;
165 	sstream->istream.stat = i_stream_sized_stat;
166 
167 	sstream->istream.istream.readable_fd = input->readable_fd;
168 	sstream->istream.istream.blocking = input->blocking;
169 	sstream->istream.istream.seekable = input->seekable;
170 	(void)i_stream_create(&sstream->istream, input,
171 			      i_stream_get_fd(input), 0);
172 	return sstream;
173 }
174 
i_stream_create_sized(struct istream * input,uoff_t size)175 struct istream *i_stream_create_sized(struct istream *input, uoff_t size)
176 {
177 	struct sized_istream *sstream;
178 
179 	sstream = i_stream_create_sized_common(input, size);
180 	sstream->error_callback = i_stream_create_sized_default_error_callback;
181 	sstream->error_context = sstream;
182 	return &sstream->istream.istream;
183 }
184 
i_stream_create_sized_range(struct istream * input,uoff_t offset,uoff_t size)185 struct istream *i_stream_create_sized_range(struct istream *input,
186 					    uoff_t offset, uoff_t size)
187 {
188 	uoff_t orig_offset = input->v_offset;
189 	struct istream *ret;
190 
191 	input->v_offset = offset;
192 	ret = i_stream_create_sized(input, size);
193 	input->v_offset = orig_offset;
194 	return ret;
195 }
196 
i_stream_create_min_sized(struct istream * input,uoff_t min_size)197 struct istream *i_stream_create_min_sized(struct istream *input, uoff_t min_size)
198 {
199 	struct istream *ret;
200 
201 	ret= i_stream_create_sized(input, min_size);
202 	struct sized_istream *ret_sstream =
203 		container_of(ret->real_stream, struct sized_istream, istream);
204 	ret_sstream->min_size_only = TRUE;
205 	return ret;
206 }
207 
i_stream_create_min_sized_range(struct istream * input,uoff_t offset,uoff_t min_size)208 struct istream *i_stream_create_min_sized_range(struct istream *input,
209 						uoff_t offset, uoff_t min_size)
210 {
211 	struct istream *ret;
212 
213 	ret = i_stream_create_sized_range(input, offset, min_size);
214 	struct sized_istream *ret_sstream =
215 		container_of(ret->real_stream, struct sized_istream, istream);
216 	ret_sstream->min_size_only = TRUE;
217 	return ret;
218 }
219 
220 #undef i_stream_create_sized_with_callback
221 struct istream *
i_stream_create_sized_with_callback(struct istream * input,uoff_t size,istream_sized_callback_t * error_callback,void * context)222 i_stream_create_sized_with_callback(struct istream *input, uoff_t size,
223 				    istream_sized_callback_t *error_callback,
224 				    void *context)
225 {
226 	struct sized_istream *sstream;
227 
228 	sstream = i_stream_create_sized_common(input, size);
229 	sstream->error_callback = error_callback;
230 	sstream->error_context = context;
231 	return &sstream->istream.istream;
232 }
233