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. On a read only BIO or if
43 the flag BIO_FLAGS_NONCLEAR_RST is set it restores the BIO to its
44 original state and the data can be read again.
45
46 BIO_eof() is true if no data is in the BIO.
47
48 BIO_ctrl_pending() returns the number of bytes currently stored.
49
50 BIO_set_mem_eof_return() sets the behaviour of memory BIO b when it is
51 empty. If the v is zero then an empty memory BIO will return EOF (that
52 is it will return zero and BIO_should_retry(b) will be false. If v is
53 non zero then it will return v when it is empty and it will set the
54 read retry flag (that is BIO_read_retry(b) is true). To avoid ambiguity
55 with a normal positive return value v should be set to a negative
56 value, typically -1.
57
58 BIO_get_mem_data() sets *pp to a pointer to the start of the memory
59 BIOs data and returns the total amount of data available. It is
60 implemented as a macro.
61
62 BIO_set_mem_buf() sets the internal BUF_MEM structure to bm and sets
63 the close flag to c, that is c should be either BIO_CLOSE or
64 BIO_NOCLOSE. It is a macro.
65
66 BIO_get_mem_ptr() places the underlying BUF_MEM structure in *pp. It is
67 a macro.
68
69 BIO_new_mem_buf() creates a memory BIO using len bytes of data at buf,
70 if len is -1 then the buf is assumed to be nul terminated and its
71 length is determined by strlen. The BIO is set to a read only state and
72 as a result cannot be written to. This is useful when some data needs
73 to be made available from a static area of memory in the form of a BIO.
74 The supplied data is read directly from the supplied buffer: it is not
75 copied first, so the supplied area of memory must be unchanged until
76 the BIO is freed.
77
79 Writes to memory BIOs will always succeed if memory is available: that
80 is their size can grow indefinitely.
81
82 Every read from a read write memory BIO will remove the data just read
83 with an internal copy operation, if a BIO contains a lot of data and it
84 is read in small chunks the operation can be very slow. The use of a
85 read only memory BIO avoids this problem. If the BIO must be read write
86 then adding a buffering BIO to the chain will speed up the process.
87
88 Calling BIO_set_mem_buf() on a BIO created with BIO_new_secmem() will
89 give undefined results, including perhaps a program crash.
90
91 Switching the memory BIO from read write to read only is not supported
92 and can give undefined results including a program crash. There are two
93 notable exceptions to the rule. The first one is to assign a static
94 memory buffer immediately after BIO creation and set the BIO as read
95 only.
96
97 The other supported sequence is to start with read write BIO then
98 temporarily switch it to read only and call BIO_reset() on the read
99 only BIO immediately before switching it back to read write. Before the
100 BIO is freed it must be switched back to the read write mode.
101
102 Calling BIO_get_mem_ptr() on read only BIO will return a BUF_MEM that
103 contains only the remaining data to be read. If the close status of the
104 BIO is set to BIO_NOCLOSE, before freeing the BUF_MEM the data pointer
105 in it must be set to NULL as the data pointer does not point to an
106 allocated memory.
107
109 There should be an option to set the maximum size of a memory BIO.
110
112 Create a memory BIO and write some data to it:
113
114 BIO *mem = BIO_new(BIO_s_mem());
115
116 BIO_puts(mem, "Hello World\n");
117
118 Create a read only memory BIO:
119
120 char data[] = "Hello World";
121 BIO *mem = BIO_new_mem_buf(data, -1);
122
123 Extract the BUF_MEM structure from a memory BIO and then free up the
124 BIO:
125
126 BUF_MEM *bptr;
127
128 BIO_get_mem_ptr(mem, &bptr);
129 BIO_set_close(mem, BIO_NOCLOSE); /* So BIO_free() leaves BUF_MEM alone */
130 BIO_free(mem);
131
133 BIO_s_mem() and BIO_s_secmem() return a valid memory BIO_METHOD
134 structure.
135
136 BIO_set_mem_eof_return(), BIO_get_mem_data(), BIO_set_mem_buf() and
137 BIO_get_mem_ptr() return 1 on success or a value which is less than or
138 equal to 0 if an error occurred.
139
140 BIO_new_mem_buf() returns a valid BIO structure on success or NULL on
141 error.
142
144 Copyright 2000-2019 The OpenSSL Project Authors. All Rights Reserved.
145
146 Licensed under the OpenSSL license (the "License"). You may not use
147 this file except in compliance with the License. You can obtain a copy
148 in the file LICENSE in the source distribution or at
149 <https://www.openssl.org/source/license.html>.
150
151
152
1531.1.1c 2019-05-28 BIO_S_MEM(3)