summaryrefslogtreecommitdiffstats
path: root/src/lib/istream-base64-encoder.c
blob: 22d2786f467ab1b92f0ab327d2e96dc0112fe9cd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
/* Copyright (c) 2003-2018 Dovecot authors, see the included COPYING file */

#include "lib.h"
#include "buffer.h"
#include "base64.h"
#include "istream-private.h"
#include "istream-base64.h"

struct base64_encoder_istream {
	struct istream_private istream;

	struct base64_encoder encoder;
};

static int i_stream_read_parent(struct istream_private *stream)
{
	size_t size;
	ssize_t ret;

	size = i_stream_get_data_size(stream->parent);
	if (size > 0)
		return 1;

	ret = i_stream_read_memarea(stream->parent);
	if (ret <= 0) {
		stream->istream.stream_errno = stream->parent->stream_errno;
		return ret;
	}
	size = i_stream_get_data_size(stream->parent);
	i_assert(size != 0);
	return 1;
}

static int
i_stream_base64_try_encode(struct base64_encoder_istream *bstream)
{
	struct istream_private *stream = &bstream->istream;
	struct base64_encoder *b64enc = &bstream->encoder;
	const unsigned char *data;
	size_t size, pos, out_size, avail;
	buffer_t buf;

	data = i_stream_get_data(stream->parent, &size);
	if (size == 0)
		return 0;

	out_size = base64_encode_get_size(b64enc, size);
	if (!i_stream_try_alloc(stream, out_size, &avail))
		return -2;

	buffer_create_from_data(&buf, stream->w_buffer + stream->pos, avail);
	base64_encode_more(b64enc, data, size, &pos, &buf);
	i_assert(buf.used > 0);

	stream->pos += buf.used;
	i_stream_skip(stream->parent, pos);
	return 1;
}

static int
i_stream_base64_finish_encode(struct base64_encoder_istream *bstream)
{
	struct istream_private *stream = &bstream->istream;
	struct base64_encoder *b64enc = &bstream->encoder;
	size_t out_size, buffer_avail;
	buffer_t buf;

	out_size = base64_encode_get_size(b64enc, 0);
	if (out_size == 0) {
		if (base64_encode_finish(b64enc, NULL))
			stream->istream.eof = TRUE;
		return 1;
	}

	if (!i_stream_try_alloc(stream, out_size, &buffer_avail))
		return -2;

	buffer_create_from_data(&buf, stream->w_buffer + stream->pos,
				buffer_avail);
	if (base64_encode_finish(b64enc, &buf))
		stream->istream.eof = TRUE;
	i_assert(buf.used > 0);

	stream->pos += buf.used;
	return 1;
}

static ssize_t i_stream_base64_encoder_read(struct istream_private *stream)
{
	struct base64_encoder_istream *bstream =
		container_of(stream, struct base64_encoder_istream, istream);
	size_t pre_count, post_count;
	int ret;

	if (base64_encode_is_finished(&bstream->encoder)) {
		stream->istream.eof = TRUE;
		return -1;
	}

	pre_count = post_count = 0;
	do {
		ret = i_stream_read_parent(stream);
		if (ret == 0)
			return 0;
		if (ret < 0) {
			if (stream->istream.stream_errno != 0)
				return -1;
			if (i_stream_get_data_size(stream->parent) == 0)
				break;
			/* add the final partial block */
		}

		/* encode as many lines as fits into destination buffer */
		pre_count = stream->pos - stream->skip;
		while ((ret = i_stream_base64_try_encode(bstream)) > 0) ;
		post_count = stream->pos - stream->skip;
	} while (ret == 0 && pre_count == post_count);

	if (ret == -2) {
		if (pre_count == post_count)
			return -2;
	} else if (ret < 0) {
		if (i_stream_get_data_size(stream->parent) == 0) {
			i_assert(post_count == pre_count);
			pre_count = stream->pos - stream->skip;
			ret = i_stream_base64_finish_encode(bstream);
			post_count = stream->pos - stream->skip;
			if (ret <= 0)
				return ret;
		}
		if (pre_count == post_count) {
			stream->istream.eof = TRUE;
			return -1;
		}
	}

	i_assert(post_count > pre_count);
	return post_count - pre_count;
}

static void
i_stream_base64_encoder_seek(struct istream_private *stream,
			     uoff_t v_offset, bool mark)
{
	struct base64_encoder_istream *bstream =
		container_of(stream, struct base64_encoder_istream, istream);

	if (v_offset < stream->istream.v_offset) {
		/* seeking backwards - go back to beginning and seek
		   forward from there. */
		stream->parent_expected_offset = stream->parent_start_offset;
		stream->skip = stream->pos = 0;
		stream->istream.v_offset = 0;
		i_stream_seek(stream->parent, 0);

		base64_encode_reset(&bstream->encoder);
	}
	i_stream_default_seek_nonseekable(stream, v_offset, mark);
}

static int
i_stream_base64_encoder_stat(struct istream_private *stream,
	bool exact ATTR_UNUSED)
{
	struct base64_encoder_istream *bstream =
		container_of(stream, struct base64_encoder_istream, istream);
	const struct stat *st;

	if (i_stream_stat(stream->parent, exact, &st) < 0) {
		stream->istream.stream_errno = stream->parent->stream_errno;
		return -1;
	}

	stream->statbuf = *st;
	if (st->st_size == 0)
		return 0;

	stream->statbuf.st_size =
		base64_get_full_encoded_size(&bstream->encoder, st->st_size);
	return 0;
}

static struct istream *
i_stream_create_base64_encoder_common(const struct base64_scheme *b64,
				      struct istream *input,
				      unsigned int chars_per_line, bool crlf)
{
	struct base64_encoder_istream *bstream;
	enum base64_encode_flags b64_flags = 0;

	i_assert(chars_per_line % 4 == 0);

	bstream = i_new(struct base64_encoder_istream, 1);
	bstream->istream.max_buffer_size = input->real_stream->max_buffer_size;

	bstream->istream.read = i_stream_base64_encoder_read;
	bstream->istream.seek = i_stream_base64_encoder_seek;
	bstream->istream.stat = i_stream_base64_encoder_stat;

	bstream->istream.istream.readable_fd = FALSE;
	bstream->istream.istream.blocking = input->blocking;
	bstream->istream.istream.seekable = input->seekable;

	if (crlf)
		b64_flags |= BASE64_ENCODE_FLAG_CRLF;
	base64_encode_init(&bstream->encoder, b64, b64_flags, chars_per_line);

	return i_stream_create(&bstream->istream, input,
			       i_stream_get_fd(input), 0);
}

struct istream *
i_stream_create_base64_encoder(struct istream *input,
			       unsigned int chars_per_line, bool crlf)
{
	return i_stream_create_base64_encoder_common(&base64_scheme, input,
						     chars_per_line, crlf);
}

struct istream *
i_stream_create_base64url_encoder(struct istream *input,
				  unsigned int chars_per_line, bool crlf)
{
	return i_stream_create_base64_encoder_common(&base64url_scheme, input,
						     chars_per_line, crlf);
}