/src/dovecot/src/lib/istream-data.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* Copyright (c) 2002-2018 Dovecot authors, see the included COPYING file */ |
2 | | |
3 | | #include "lib.h" |
4 | | #include "istream-private.h" |
5 | | |
6 | | static ssize_t i_stream_data_read(struct istream_private *stream) |
7 | 0 | { |
8 | 0 | stream->istream.eof = TRUE; |
9 | 0 | return -1; |
10 | 0 | } |
11 | | |
12 | | static void i_stream_data_seek(struct istream_private *stream, uoff_t v_offset, |
13 | | bool mark ATTR_UNUSED) |
14 | 0 | { |
15 | 0 | stream->skip = v_offset; |
16 | 0 | stream->istream.v_offset = v_offset; |
17 | 0 | } |
18 | | |
19 | | struct istream *i_stream_create_from_data(const void *data, size_t size) |
20 | 0 | { |
21 | 0 | struct istream_private *stream; |
22 | |
|
23 | 0 | stream = i_new(struct istream_private, 1); |
24 | 0 | stream->buffer = data; |
25 | 0 | stream->pos = size; |
26 | 0 | stream->max_buffer_size = SIZE_MAX; |
27 | |
|
28 | 0 | stream->read = i_stream_data_read; |
29 | 0 | stream->seek = i_stream_data_seek; |
30 | |
|
31 | 0 | stream->istream.readable_fd = FALSE; |
32 | 0 | stream->istream.blocking = TRUE; |
33 | 0 | stream->istream.seekable = TRUE; |
34 | 0 | i_stream_create(stream, NULL, -1, ISTREAM_CREATE_FLAG_NOOP_SNAPSHOT); |
35 | 0 | stream->statbuf.st_size = size; |
36 | 0 | i_stream_set_name(&stream->istream, "(buffer)"); |
37 | 0 | return &stream->istream; |
38 | 0 | } |
39 | | |
40 | | static void i_stream_copied_data_free(void *data) |
41 | 0 | { |
42 | 0 | i_free(data); |
43 | 0 | } |
44 | | struct istream * |
45 | | i_stream_create_copy_from_data(const void *data, size_t size) |
46 | 0 | { |
47 | 0 | struct istream *stream; |
48 | 0 | void *buffer; |
49 | |
|
50 | 0 | if (size == 0) { |
51 | 0 | buffer = ""; |
52 | 0 | } else { |
53 | 0 | buffer = i_malloc(size); |
54 | 0 | memcpy(buffer, data, size); |
55 | 0 | } |
56 | 0 | stream = i_stream_create_from_data(buffer, size); |
57 | 0 | if (size > 0) { |
58 | 0 | i_stream_add_destroy_callback |
59 | 0 | (stream, i_stream_copied_data_free, buffer); |
60 | 0 | } |
61 | 0 | return stream; |
62 | 0 | } |