istream-crlf.c revision 9511a40d933181045343110c8101b75887062aae
e59faf65ce864fe95dc00f5d52b8323cdbd0608aTimo Sirainen/* Copyright (c) 2007 Dovecot authors, see the included COPYING file */
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainenstatic void i_stream_crlf_destroy(struct iostream_private *stream)
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen struct crlf_istream *cstream = (struct crlf_istream *)stream;
4c1deab456fe8877bf025d11843167ac1f36327aTimo Siraineni_stream_crlf_set_max_buffer_size(struct iostream_private *stream,
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen struct crlf_istream *cstream = (struct crlf_istream *)stream;
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen i_stream_set_max_buffer_size(cstream->istream.parent, max_size);
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainenstatic int i_stream_crlf_read_common(struct crlf_istream *cstream)
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen struct istream_private *stream = &cstream->istream;
010affb9f4b9cca9b94e1d8d570cf179daff08d7Timo Sirainen const unsigned char *data;
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen data = i_stream_get_data(stream->parent, &size);
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen if (ret <= 0 && (ret != -2 || stream->skip == 0)) {
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen data = i_stream_get_data(stream->parent, &size);
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen if (!i_stream_get_buffer_space(stream, size, NULL))
b58aafbd21b365117538f73f306d22f75acd91f1Timo Sirainenstatic ssize_t i_stream_crlf_read_crlf(struct istream_private *stream)
b58aafbd21b365117538f73f306d22f75acd91f1Timo Sirainen struct crlf_istream *cstream = (struct crlf_istream *)stream;
b58aafbd21b365117538f73f306d22f75acd91f1Timo Sirainen const unsigned char *data;
b58aafbd21b365117538f73f306d22f75acd91f1Timo Sirainen data = i_stream_get_data(stream->parent, &size);
b58aafbd21b365117538f73f306d22f75acd91f1Timo Sirainen /* @UNSAFE: add missing CRs */
b58aafbd21b365117538f73f306d22f75acd91f1Timo Sirainen for (i = 0; i < size && dest < stream->buffer_size; i++) {
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen if (i == 0) {
d7b205394bd07152718152976dfa089e13253d7eTimo Sirainen cstream->last_cr = stream->w_buffer[dest-1] == '\r';
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainenstatic ssize_t i_stream_crlf_read_lf(struct istream_private *stream)
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen struct crlf_istream *cstream = (struct crlf_istream *)stream;
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen const unsigned char *data;
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen data = i_stream_get_data(stream->parent, &size);
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen /* @UNSAFE */
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen /* CR without LF */
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen for (i = 1; i < size && dest < stream->buffer_size; i++) {
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen } else if (data[i-1] == '\r' && data[i] != '\n') {
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Siraineni_stream_crlf_seek(struct istream_private *stream ATTR_UNUSED,
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen uoff_t v_offset ATTR_UNUSED, bool mark ATTR_UNUSED)
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen i_panic("crlf-istream: seeking unsupported currently");
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainenstatic const struct stat *
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Siraineni_stream_crlf_stat(struct istream_private *stream, bool exact)
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainenstatic struct istream *
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Siraineni_stream_create_crlf_full(struct istream *input, bool crlf)
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen cstream->istream.max_buffer_size = input->real_stream->max_buffer_size;
b58aafbd21b365117538f73f306d22f75acd91f1Timo Sirainen cstream->istream.iostream.destroy = i_stream_crlf_destroy;
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen cstream->istream.iostream.set_max_buffer_size =
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen cstream->istream.read = crlf ? i_stream_crlf_read_crlf :
0dc7a67fb62b2aac82ff1c03319bd4976c56dbc1Timo Sirainen cstream->istream.istream.blocking = input->blocking;
0dc7a67fb62b2aac82ff1c03319bd4976c56dbc1Timo Sirainen cstream->istream.istream.seekable = input->seekable;
0dc7a67fb62b2aac82ff1c03319bd4976c56dbc1Timo Sirainen return i_stream_create(&cstream->istream, input,
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainenstruct istream *i_stream_create_crlf(struct istream *input)
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen return i_stream_create_crlf_full(input, TRUE);
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainenstruct istream *i_stream_create_lf(struct istream *input)