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
|
/* Copyright (c) 2007-2018 Dovecot authors, see the included COPYING file */
#include "lib.h"
#include "istream-private.h"
#include "istream-crlf.h"
struct crlf_istream {
struct istream_private istream;
bool pending_cr:1;
bool last_cr:1;
};
static int i_stream_crlf_read_common(struct crlf_istream *cstream)
{
struct istream_private *stream = &cstream->istream;
size_t size, avail;
ssize_t ret;
size = i_stream_get_data_size(stream->parent);
if (size == 0) {
ret = i_stream_read_memarea(stream->parent);
if (ret <= 0) {
i_assert(ret != -2); /* 0 sized buffer can't be full */
stream->istream.stream_errno =
stream->parent->stream_errno;
stream->istream.eof = stream->parent->eof;
return ret;
}
size = i_stream_get_data_size(stream->parent);
i_assert(size != 0);
}
if (!i_stream_try_alloc(stream, size, &avail))
return -2;
return 1;
}
static ssize_t i_stream_crlf_read_crlf(struct istream_private *stream)
{
struct crlf_istream *cstream =
container_of(stream, struct crlf_istream, istream);
const unsigned char *data, *ptr, *src, *src_end;
unsigned char *dest, *dest_end;
size_t size, copy_len;
ssize_t ret;
ret = i_stream_crlf_read_common(cstream);
if (ret <= 0)
return ret;
/* at least one byte was read */
data = i_stream_get_data(stream->parent, &size);
dest = stream->w_buffer + stream->pos;
dest_end = stream->w_buffer + stream->buffer_size;
src = data;
src_end = data + size;
/* @UNSAFE: add missing CRs */
if (*src == '\n') {
if (!cstream->last_cr && dest < dest_end)
*dest++ = '\r';
if (dest < dest_end) {
*dest++ = '\n';
src++;
}
}
while (dest < dest_end) {
i_assert(src <= src_end);
ptr = memchr(src, '\n', src_end - src);
if (ptr == NULL)
ptr = src_end;
/* copy data up to LF */
copy_len = ptr - src;
if (dest + copy_len > dest_end)
copy_len = dest_end - dest;
if (copy_len > 0) {
memcpy(dest, src, copy_len);
dest += copy_len;
src += copy_len;
}
i_assert(dest <= dest_end && src <= src_end);
if (dest == dest_end || src == src_end)
break;
/* add the CR if necessary and copy the LF.
(src >= data+1, because data[0]=='\n' was
handled before this loop) */
if (src[-1] != '\r')
*dest++ = '\r';
if (dest == dest_end)
break;
*dest++ = '\n';
src++;
i_assert(src == ptr + 1);
}
i_assert(dest != stream->w_buffer);
cstream->last_cr = dest[-1] == '\r';
i_stream_skip(stream->parent, src - data);
ret = (dest - stream->w_buffer) - stream->pos;
i_assert(ret > 0);
stream->pos = dest - stream->w_buffer;
return ret;
}
static ssize_t i_stream_crlf_read_lf(struct istream_private *stream)
{
struct crlf_istream *cstream =
container_of(stream, struct crlf_istream, istream);
const unsigned char *data, *p;
size_t i, dest, size, max;
ssize_t ret;
bool pending_cr;
ret = i_stream_crlf_read_common(cstream);
if (ret <= 0)
return ret;
data = i_stream_get_data(stream->parent, &size);
/* @UNSAFE */
/* \r\n -> \n
\r<anything> -> \r<anything>
\r\r\n -> \r\n */
dest = stream->pos;
pending_cr = cstream->pending_cr;
for (i = 0; i < size && dest < stream->buffer_size; ) {
if (data[i] == '\r') {
if (pending_cr) {
/* \r\r */
stream->w_buffer[dest++] = '\r';
} else {
pending_cr = TRUE;
}
i++;
} else if (data[i] == '\n') {
/* [\r]\n */
pending_cr = FALSE;
stream->w_buffer[dest++] = '\n';
i++;
} else if (pending_cr) {
/* \r<anything> */
pending_cr = FALSE;
stream->w_buffer[dest++] = '\r';
} else {
/* copy everything until the next \r */
max = I_MIN(size - i, stream->buffer_size - dest);
p = memchr(data + i, '\r', max);
if (p != NULL)
max = p - (data+i);
memcpy(stream->w_buffer + dest, data + i, max);
dest += max;
i += max;
}
}
i_assert(i <= size);
i_assert(dest <= stream->buffer_size);
cstream->pending_cr = pending_cr;
i_stream_skip(stream->parent, i);
ret = dest - stream->pos;
if (ret == 0) {
i_assert(cstream->pending_cr && size == 1);
return i_stream_crlf_read_lf(stream);
}
i_assert(ret > 0);
stream->pos = dest;
return ret;
}
static struct istream *
i_stream_create_crlf_full(struct istream *input, bool crlf)
{
struct crlf_istream *cstream;
cstream = i_new(struct crlf_istream, 1);
cstream->istream.max_buffer_size = input->real_stream->max_buffer_size;
cstream->istream.read = crlf ? i_stream_crlf_read_crlf :
i_stream_crlf_read_lf;
cstream->istream.istream.readable_fd = FALSE;
cstream->istream.istream.blocking = input->blocking;
cstream->istream.istream.seekable = FALSE;
return i_stream_create(&cstream->istream, input,
i_stream_get_fd(input), 0);
}
struct istream *i_stream_create_crlf(struct istream *input)
{
return i_stream_create_crlf_full(input, TRUE);
}
struct istream *i_stream_create_lf(struct istream *input)
{
return i_stream_create_crlf_full(input, FALSE);
}
|