istream-crlf.c revision bf3fb941cc1deb06786449b89c77d9a56a07c251
2454dfa32c93c20a8522c6ed42fe057baaac9f9aStephan Bosch/* Copyright (c) 2007-2008 Dovecot authors, see the included COPYING file */
06ff2a72c39cb34cc6425f17fc82c5e93fef2018Timo Sirainenstatic void i_stream_crlf_destroy(struct iostream_private *stream)
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen struct crlf_istream *cstream = (struct crlf_istream *)stream;
61530b48694398df42744204e35535dbe3f745c4Timo Siraineni_stream_crlf_set_max_buffer_size(struct iostream_private *stream,
2767104d81e97a109f0aa9758792bfa1da325a97Timo Sirainen struct crlf_istream *cstream = (struct crlf_istream *)stream;
6789ed17e7ca4021713507baf0dcf6979bb42e0cTimo Sirainen i_stream_set_max_buffer_size(cstream->istream.parent, max_size);
6789ed17e7ca4021713507baf0dcf6979bb42e0cTimo Sirainenstatic int i_stream_crlf_read_common(struct crlf_istream *cstream)
2767104d81e97a109f0aa9758792bfa1da325a97Timo Sirainen struct istream_private *stream = &cstream->istream;
2767104d81e97a109f0aa9758792bfa1da325a97Timo Sirainen const unsigned char *data;
6789ed17e7ca4021713507baf0dcf6979bb42e0cTimo Sirainen data = i_stream_get_data(stream->parent, &size);
67c25cb4af273aff7384d5028d459cc9afdf8712Timo Sirainen if (ret <= 0 && (ret != -2 || stream->skip == 0)) {
67c25cb4af273aff7384d5028d459cc9afdf8712Timo Sirainen data = i_stream_get_data(stream->parent, &size);
5ce2084ada06ade9f44fc2914c34658e9a842dc1Timo Sirainen if (!i_stream_get_buffer_space(stream, size, NULL))
67c25cb4af273aff7384d5028d459cc9afdf8712Timo Sirainenstatic ssize_t i_stream_crlf_read_crlf(struct istream_private *stream)
67c25cb4af273aff7384d5028d459cc9afdf8712Timo Sirainen struct crlf_istream *cstream = (struct crlf_istream *)stream;
67c25cb4af273aff7384d5028d459cc9afdf8712Timo Sirainen const unsigned char *data;
d244c6cadd5f077f5d0f1e00c3652d0108a2d908Timo Sirainen data = i_stream_get_data(stream->parent, &size);
fde0b1793a2842da00eaa105d5e13fec465f0443Timo Sirainen /* @UNSAFE: add missing CRs */
d6c5ceea8521b92d10e51a59da00c792f6140b1dTimo Sirainen for (i = 0; i < size && dest < stream->buffer_size; i++) {
baf1148108b7d9739626b47cc57298c36929586aTimo Sirainen if (i == 0) {
d6c5ceea8521b92d10e51a59da00c792f6140b1dTimo Sirainen cstream->last_cr = stream->w_buffer[dest-1] == '\r';
d6c5ceea8521b92d10e51a59da00c792f6140b1dTimo Sirainenstatic ssize_t i_stream_crlf_read_lf(struct istream_private *stream)
51cbc45fc1ac5dde29bc2adbb175945df1b4f7d4Timo Sirainen struct crlf_istream *cstream = (struct crlf_istream *)stream;
51cbc45fc1ac5dde29bc2adbb175945df1b4f7d4Timo Sirainen const unsigned char *data;
51cbc45fc1ac5dde29bc2adbb175945df1b4f7d4Timo Sirainen data = i_stream_get_data(stream->parent, &size);
51cbc45fc1ac5dde29bc2adbb175945df1b4f7d4Timo Sirainen /* @UNSAFE */
51cbc45fc1ac5dde29bc2adbb175945df1b4f7d4Timo Sirainen /* CR without LF */
923eb3dde28e4d8841c14fd6b4a69635b7070c3eTimo Sirainen for (i = 1; i < size && dest < stream->buffer_size; i++) {
2767104d81e97a109f0aa9758792bfa1da325a97Timo Sirainen } else if (data[i-1] == '\r' && data[i] != '\n') {
d10a370b2614712d9cb6a1dd8625f62a071b6377Timo Siraineni_stream_crlf_seek(struct istream_private *stream ATTR_UNUSED,
d10a370b2614712d9cb6a1dd8625f62a071b6377Timo Sirainen uoff_t v_offset ATTR_UNUSED, bool mark ATTR_UNUSED)
88ea893b45d3ed8d68000921db9156c03cbe1b00Timo Sirainen i_panic("crlf-istream: seeking unsupported currently");
88ea893b45d3ed8d68000921db9156c03cbe1b00Timo Sirainenstatic const struct stat *
e20e638805c4bd54e039891a3e92760b1dfa189aTimo Siraineni_stream_crlf_stat(struct istream_private *stream, bool exact)
d6c5ceea8521b92d10e51a59da00c792f6140b1dTimo Sirainenstatic struct istream *
e20e638805c4bd54e039891a3e92760b1dfa189aTimo Siraineni_stream_create_crlf_full(struct istream *input, bool crlf)
2767104d81e97a109f0aa9758792bfa1da325a97Timo Sirainen cstream->istream.max_buffer_size = input->real_stream->max_buffer_size;
d6c5ceea8521b92d10e51a59da00c792f6140b1dTimo Sirainen cstream->istream.iostream.destroy = i_stream_crlf_destroy;
d6c5ceea8521b92d10e51a59da00c792f6140b1dTimo Sirainen cstream->istream.iostream.set_max_buffer_size =
2767104d81e97a109f0aa9758792bfa1da325a97Timo Sirainen cstream->istream.read = crlf ? i_stream_crlf_read_crlf :
d6c5ceea8521b92d10e51a59da00c792f6140b1dTimo Sirainen cstream->istream.istream.blocking = input->blocking;
88ea893b45d3ed8d68000921db9156c03cbe1b00Timo Sirainen return i_stream_create(&cstream->istream, input,
e82e363e7a6917f470412d629db6c5b1f5891a35Timo Sirainenstruct istream *i_stream_create_crlf(struct istream *input)
eef4ba0cc3e78f8c26804c1c9251a76580a41f0cTimo Sirainen return i_stream_create_crlf_full(input, TRUE);
905951e448e0d0f0778f43ce7673d0cac60b9b61Timo Sirainenstruct istream *i_stream_create_lf(struct istream *input)