dovecot-2.2: Various fixes to istream-base64-encoder/decoder

dovecot at dovecot.org dovecot at dovecot.org
Fri Aug 10 07:00:26 EEST 2012


details:   http://hg.dovecot.org/dovecot-2.2/rev/4d13da5c571b
changeset: 14846:4d13da5c571b
user:      Timo Sirainen <tss at iki.fi>
date:      Fri Aug 10 07:00:20 2012 +0300
description:
Various fixes to istream-base64-encoder/decoder

diffstat:

 src/lib/istream-base64-decoder.c      |   2 +-
 src/lib/istream-base64-encoder.c      |  45 ++++++++++++++++++----------------
 src/lib/test-istream-base64-encoder.c |  43 +++++++++++++++++++++++++-------
 3 files changed, 58 insertions(+), 32 deletions(-)

diffs (191 lines):

diff -r 39b1b519c033 -r 4d13da5c571b src/lib/istream-base64-decoder.c
--- a/src/lib/istream-base64-decoder.c	Fri Aug 10 06:45:25 2012 +0300
+++ b/src/lib/istream-base64-decoder.c	Fri Aug 10 07:00:20 2012 +0300
@@ -88,7 +88,7 @@
 		pre_count = stream->pos - stream->skip;
 		while ((ret = i_stream_base64_try_decode_block(bstream)) > 0) ;
 		post_count = stream->pos - stream->skip;
-	} while (ret == 0);
+	} while (ret == 0 && pre_count == post_count);
 
 	if (ret < 0)
 		return ret;
diff -r 39b1b519c033 -r 4d13da5c571b src/lib/istream-base64-encoder.c
--- a/src/lib/istream-base64-encoder.c	Fri Aug 10 06:45:25 2012 +0300
+++ b/src/lib/istream-base64-encoder.c	Fri Aug 10 07:00:20 2012 +0300
@@ -31,14 +31,14 @@
 	if (ret <= 0) {
 		stream->istream.stream_errno = stream->parent->stream_errno;
 		stream->istream.eof = stream->parent->eof;
-		return size > 0 ? 1 : ret;
+		return ret;
 	}
 	size = i_stream_get_data_size(stream->parent);
 	i_assert(size != 0);
 	return 1;
 }
 
-static bool
+static int
 i_stream_base64_try_encode_line(struct base64_encoder_istream *bstream)
 {
 	struct istream_private *stream = &bstream->istream;
@@ -47,13 +47,13 @@
 	buffer_t buf;
 
 	data = i_stream_get_data(stream->parent, &size);
-	if (size == 0)
-		return FALSE;
+	if (size == 0 || (size < 3 && !stream->parent->eof))
+		return 0;
 
 	if (bstream->cur_line_len == bstream->chars_per_line) {
 		/* @UNSAFE: end of line, add newline */
 		if (!i_stream_try_alloc(stream, bstream->crlf ? 2 : 1, &avail))
-			return FALSE;
+			return -2;
 
 		if (bstream->crlf)
 			stream->w_buffer[stream->pos++] = '\r';
@@ -68,13 +68,14 @@
 		/* can't fit everything to destination buffer.
 		   write as much as we can. */
 		size = (buffer_avail / 4) * 3;
+		if (size == 0)
+			return -2;
 	} else if (!stream->parent->eof && size % 3 != 0) {
 		/* encode 3 chars at a time, so base64_encode() doesn't
 		   add '=' characters in the middle of the stream */
 		size -= (size % 3);
 	}
-	if (size == 0)
-		return FALSE;
+	i_assert(size != 0);
 
 	if (bstream->cur_line_len + (size+2)/3*4 > bstream->chars_per_line) {
 		size = (bstream->chars_per_line - bstream->cur_line_len)/4 * 3;
@@ -89,32 +90,34 @@
 	i_assert(bstream->cur_line_len <= bstream->chars_per_line);
 	stream->pos += buf.used;
 	i_stream_skip(stream->parent, size);
-	return TRUE;
+	return 1;
 }
 
 static ssize_t i_stream_base64_encoder_read(struct istream_private *stream)
 {
 	struct base64_encoder_istream *bstream =
 		(struct base64_encoder_istream *)stream;
-	size_t pre_count, post_count, size;
+	size_t pre_count, post_count;
 	int ret;
 
 	do {
 		ret = i_stream_read_parent(stream);
-		if (ret <= 0)
-			return ret;
-		size = i_stream_get_data_size(stream->parent);
-	} while (size < 3 && !stream->parent->eof);
+		if (ret == 0)
+			return 0;
+		if (ret < 0) {
+			if (i_stream_get_data_size(stream->parent) == 0)
+				return -1;
+			/* add the final partial block */
+		}
 
-	/* encode as many lines as fits into destination buffer */
-	pre_count = stream->pos - stream->skip;
-	while (i_stream_base64_try_encode_line(bstream)) ;
-	post_count = stream->pos - stream->skip;
+		/* encode as many lines as fits into destination buffer */
+		pre_count = stream->pos - stream->skip;
+		while ((ret = i_stream_base64_try_encode_line(bstream)) > 0) ;
+		post_count = stream->pos - stream->skip;
+	} while (ret == 0 && pre_count == post_count);
 
-	if (pre_count == post_count) {
-		i_assert(stream->buffer_size - stream->pos < 4);
-		return -2;
-	}
+	if (ret < 0)
+		return ret;
 
 	i_assert(post_count > pre_count);
 	return post_count - pre_count;
diff -r 39b1b519c033 -r 4d13da5c571b src/lib/test-istream-base64-encoder.c
--- a/src/lib/test-istream-base64-encoder.c	Fri Aug 10 06:45:25 2012 +0300
+++ b/src/lib/test-istream-base64-encoder.c	Fri Aug 10 07:00:20 2012 +0300
@@ -5,28 +5,46 @@
 #include "istream-private.h"
 #include "istream-base64.h"
 
+static struct test {
+	const char *input;
+	unsigned int chars_per_line;
+	bool crlf;
+	const char *output;
+} tests[] = {
+	{ "hello world", 80, FALSE, "aGVsbG8gd29ybGQ=" },
+	{ "hello world", 4, FALSE, "aGVs\nbG8g\nd29y\nbGQ=" },
+	{ "hello world", 4, TRUE, "aGVs\r\nbG8g\r\nd29y\r\nbGQ=", },
+};
+
 static const char *hello = "hello world";
 
-static const char *
-encode(const char *text, unsigned int chars_per_line, bool crlf)
+static void encode_test(const char *text, unsigned int chars_per_line,
+			bool crlf, const char *output)
 {
+	unsigned int i, text_len = strlen(text);
 	struct istream *input, *input_data;
-	const char *reply;
 	const unsigned char *data;
 	size_t size;
 	ssize_t ret;
 
-	input_data = i_stream_create_from_data(text, strlen(text));
+	input_data = test_istream_create_data(text, text_len);
+	test_istream_set_allow_eof(input_data, FALSE);
 	input = i_stream_create_base64_encoder(input_data, chars_per_line, crlf);
+
+	for (i = 1; i <= text_len; i++) {
+		test_istream_set_size(input_data, i);
+		while ((ret = i_stream_read(input)) > 0) ;
+		test_assert(ret == 0);
+	}
+	test_istream_set_allow_eof(input_data, TRUE);
 	while ((ret = i_stream_read(input)) > 0) ;
 	test_assert(ret == -1);
 
 	data = i_stream_get_data(input, &size);
-	reply = t_strndup(data, size);
+	test_assert(size == strlen(output) && memcmp(data, output, size) == 0);
 
 	i_stream_unref(&input);
 	i_stream_unref(&input_data);
-	return reply;
 }
 
 static void
@@ -61,10 +79,15 @@
 
 void test_istream_base64_encoder(void)
 {
-	test_begin("istream base64 encoder");
-	test_assert(strcmp(encode(hello, 80, FALSE), "aGVsbG8gd29ybGQ=") == 0);
-	test_assert(strcmp(encode(hello, 4, FALSE), "aGVs\nbG8g\nd29y\nbGQ=") == 0);
-	test_assert(strcmp(encode(hello, 4, TRUE), "aGVs\r\nbG8g\r\nd29y\r\nbGQ=") == 0);
+	unsigned int i;
+
+	for (i = 0; i < N_ELEMENTS(tests); i++) {
+		test_begin(t_strdup_printf("istream base64 decoder %u", i+1));
+		encode_test(tests[i].input, tests[i].chars_per_line,
+			    tests[i].crlf, tests[i].output);
+		test_end();
+	}
+	test_begin("istream base64 encoder seek");
 	test_istream_base64_encoder_seek(hello, "aGVs\r\nbG8g\r\nd29y\r\nbGQ=");
 	test_end();
 }


More information about the dovecot-cvs mailing list