1BIO_S_MEM(3) OpenSSL BIO_S_MEM(3)
2
3
4
6 BIO_s_secmem, BIO_s_mem, BIO_set_mem_eof_return, BIO_get_mem_data,
7 BIO_set_mem_buf, BIO_get_mem_ptr, BIO_new_mem_buf - memory BIO
8
10 #include <openssl/bio.h>
11
12 const BIO_METHOD *BIO_s_mem(void);
13 const BIO_METHOD *BIO_s_secmem(void);
14
15 BIO_set_mem_eof_return(BIO *b, int v)
16 long BIO_get_mem_data(BIO *b, char **pp)
17 BIO_set_mem_buf(BIO *b, BUF_MEM *bm, int c)
18 BIO_get_mem_ptr(BIO *b, BUF_MEM **pp)
19
20 BIO *BIO_new_mem_buf(const void *buf, int len);
21
23 BIO_s_mem() returns the memory BIO method function.
24
25 A memory BIO is a source/sink BIO which uses memory for its I/O. Data
26 written to a memory BIO is stored in a BUF_MEM structure which is
27 extended as appropriate to accommodate the stored data.
28
29 BIO_s_secmem() is like BIO_s_mem() except that the secure heap is used
30 for buffer storage.
31
32 Any data written to a memory BIO can be recalled by reading from it.
33 Unless the memory BIO is read only any data read from it is deleted
34 from the BIO.
35
36 Memory BIOs support BIO_gets() and BIO_puts().
37
38 If the BIO_CLOSE flag is set when a memory BIO is freed then the
39 underlying BUF_MEM structure is also freed.
40
41 Calling BIO_reset() on a read write memory BIO clears any data in it if
42 the flag BIO_FLAGS_NONCLEAR_RST is not set, otherwise it just restores
43 the read pointer to the state it was just after the last write was
44 performed and the data can be read again. On a read only BIO it
45 similarly restores the BIO to its original state and the read only data
46 can be read again.
47
48 BIO_eof() is true if no data is in the BIO.
49
50 BIO_ctrl_pending() returns the number of bytes currently stored.
51
52 BIO_set_mem_eof_return() sets the behaviour of memory BIO b when it is
53 empty. If the v is zero then an empty memory BIO will return EOF (that
54 is it will return zero and BIO_should_retry(b) will be false. If v is
55 non zero then it will return v when it is empty and it will set the
56 read retry flag (that is BIO_read_retry(b) is true). To avoid ambiguity
57 with a normal positive return value v should be set to a negative
58 value, typically -1.
59
60 BIO_get_mem_data() sets *pp to a pointer to the start of the memory
61 BIOs data and returns the total amount of data available. It is
62 implemented as a macro.
63
64 BIO_set_mem_buf() sets the internal BUF_MEM structure to bm and sets
65 the close flag to c, that is c should be either BIO_CLOSE or
66 BIO_NOCLOSE. It is a macro.
67
68 BIO_get_mem_ptr() places the underlying BUF_MEM structure in *pp. It is
69 a macro.
70
71 BIO_new_mem_buf() creates a memory BIO using len bytes of data at buf,
72 if len is -1 then the buf is assumed to be nul terminated and its
73 length is determined by strlen. The BIO is set to a read only state and
74 as a result cannot be written to. This is useful when some data needs
75 to be made available from a static area of memory in the form of a BIO.
76 The supplied data is read directly from the supplied buffer: it is not
77 copied first, so the supplied area of memory must be unchanged until
78 the BIO is freed.
79
81 Writes to memory BIOs will always succeed if memory is available: that
82 is their size can grow indefinitely.
83
84 Every write after partial read (not all data in the memory buffer was
85 read) to a read write memory BIO will have to move the unread data with
86 an internal copy operation, if a BIO contains a lot of data and it is
87 read in small chunks intertwined with writes the operation can be very
88 slow. Adding a buffering BIO to the chain can speed up the process.
89
90 Calling BIO_set_mem_buf() on a BIO created with BIO_new_secmem() will
91 give undefined results, including perhaps a program crash.
92
93 Switching the memory BIO from read write to read only is not supported
94 and can give undefined results including a program crash. There are two
95 notable exceptions to the rule. The first one is to assign a static
96 memory buffer immediately after BIO creation and set the BIO as read
97 only.
98
99 The other supported sequence is to start with read write BIO then
100 temporarily switch it to read only and call BIO_reset() on the read
101 only BIO immediately before switching it back to read write. Before the
102 BIO is freed it must be switched back to the read write mode.
103
104 Calling BIO_get_mem_ptr() on read only BIO will return a BUF_MEM that
105 contains only the remaining data to be read. If the close status of the
106 BIO is set to BIO_NOCLOSE, before freeing the BUF_MEM the data pointer
107 in it must be set to NULL as the data pointer does not point to an
108 allocated memory.
109
110 Calling BIO_reset() on a read write memory BIO with
111 BIO_FLAGS_NONCLEAR_RST flag set can have unexpected outcome when the
112 reads and writes to the BIO are intertwined. As documented above the
113 BIO will be reset to the state after the last completed write
114 operation. The effects of reads preceding that write operation cannot
115 be undone.
116
117 Calling BIO_get_mem_ptr() prior to a BIO_reset() call with
118 BIO_FLAGS_NONCLEAR_RST set has the same effect as a write operation.
119
121 There should be an option to set the maximum size of a memory BIO.
122
124 BIO_s_mem() and BIO_s_secmem() return a valid memory BIO_METHOD
125 structure.
126
127 BIO_set_mem_eof_return(), BIO_set_mem_buf() and BIO_get_mem_ptr()
128 return 1 on success or a value which is less than or equal to 0 if an
129 error occurred.
130
131 BIO_get_mem_data() returns the total number of bytes available on
132 success, 0 if b is NULL, or a negative value in case of other errors.
133
134 BIO_new_mem_buf() returns a valid BIO structure on success or NULL on
135 error.
136
138 Create a memory BIO and write some data to it:
139
140 BIO *mem = BIO_new(BIO_s_mem());
141
142 BIO_puts(mem, "Hello World\n");
143
144 Create a read only memory BIO:
145
146 char data[] = "Hello World";
147 BIO *mem = BIO_new_mem_buf(data, -1);
148
149 Extract the BUF_MEM structure from a memory BIO and then free up the
150 BIO:
151
152 BUF_MEM *bptr;
153
154 BIO_get_mem_ptr(mem, &bptr);
155 BIO_set_close(mem, BIO_NOCLOSE); /* So BIO_free() leaves BUF_MEM alone */
156 BIO_free(mem);
157
159 Copyright 2000-2019 The OpenSSL Project Authors. All Rights Reserved.
160
161 Licensed under the OpenSSL license (the "License"). You may not use
162 this file except in compliance with the License. You can obtain a copy
163 in the file LICENSE in the source distribution or at
164 <https://www.openssl.org/source/license.html>.
165
166
167
1681.1.1q 2022-07-21 BIO_S_MEM(3)