1.\" $OpenBSD: BIO_s_mem.3,v 1.14 2019/06/06 01:06:58 schwarze Exp $ 2.\" full merge up to: OpenSSL 8711efb4 Mon Apr 20 11:33:12 2009 +0000 3.\" selective merge up to: OpenSSL 36359cec Mar 7 14:37:23 2018 +0100 4.\" 5.\" This file was written by Dr. Stephen Henson <steve@openssl.org>. 6.\" Copyright (c) 2000 The OpenSSL Project. All rights reserved. 7.\" 8.\" Redistribution and use in source and binary forms, with or without 9.\" modification, are permitted provided that the following conditions 10.\" are met: 11.\" 12.\" 1. Redistributions of source code must retain the above copyright 13.\" notice, this list of conditions and the following disclaimer. 14.\" 15.\" 2. Redistributions in binary form must reproduce the above copyright 16.\" notice, this list of conditions and the following disclaimer in 17.\" the documentation and/or other materials provided with the 18.\" distribution. 19.\" 20.\" 3. All advertising materials mentioning features or use of this 21.\" software must display the following acknowledgment: 22.\" "This product includes software developed by the OpenSSL Project 23.\" for use in the OpenSSL Toolkit. (http://www.openssl.org/)" 24.\" 25.\" 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to 26.\" endorse or promote products derived from this software without 27.\" prior written permission. For written permission, please contact 28.\" openssl-core@openssl.org. 29.\" 30.\" 5. Products derived from this software may not be called "OpenSSL" 31.\" nor may "OpenSSL" appear in their names without prior written 32.\" permission of the OpenSSL Project. 33.\" 34.\" 6. Redistributions of any form whatsoever must retain the following 35.\" acknowledgment: 36.\" "This product includes software developed by the OpenSSL Project 37.\" for use in the OpenSSL Toolkit (http://www.openssl.org/)" 38.\" 39.\" THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY 40.\" EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 41.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 42.\" PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR 43.\" ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 44.\" SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 45.\" NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 46.\" LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 47.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 48.\" STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 49.\" ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 50.\" OF THE POSSIBILITY OF SUCH DAMAGE. 51.\" 52.Dd $Mdocdate: June 6 2019 $ 53.Dt BIO_S_MEM 3 54.Os 55.Sh NAME 56.Nm BIO_s_mem , 57.Nm BIO_set_mem_eof_return , 58.Nm BIO_get_mem_data , 59.Nm BIO_set_mem_buf , 60.Nm BIO_get_mem_ptr , 61.Nm BIO_new_mem_buf 62.Nd memory BIO 63.Sh SYNOPSIS 64.In openssl/bio.h 65.Ft const BIO_METHOD * 66.Fo BIO_s_mem 67.Fa "void" 68.Fc 69.Ft long 70.Fo BIO_set_mem_eof_return 71.Fa "BIO *b" 72.Fa "int v" 73.Fc 74.Ft long 75.Fo BIO_get_mem_data 76.Fa "BIO *b" 77.Fa "char **pp" 78.Fc 79.Ft long 80.Fo BIO_set_mem_buf 81.Fa "BIO *b" 82.Fa "BUF_MEM *bm" 83.Fa "int c" 84.Fc 85.Ft long 86.Fo BIO_get_mem_ptr 87.Fa "BIO *b" 88.Fa "BUF_MEM **pp" 89.Fc 90.Ft BIO * 91.Fo BIO_new_mem_buf 92.Fa "const void *buf" 93.Fa "int len" 94.Fc 95.Sh DESCRIPTION 96.Fn BIO_s_mem 97returns the memory BIO method function. 98.Pp 99A memory BIO is a source/sink BIO which uses memory for its I/O. 100Data written to a memory BIO is stored in a 101.Vt BUF_MEM 102structure which is extended as appropriate to accommodate the stored data. 103.Pp 104Any data written to a memory BIO can be recalled by reading from it. 105Unless the memory BIO is read only, 106any data read from it is deleted from the BIO. 107.Pp 108Memory BIOs support 109.Xr BIO_gets 3 110and 111.Xr BIO_puts 3 . 112.Pp 113If the 114.Dv BIO_CLOSE 115flag is set when a memory BIO is freed, the underlying 116.Dv BUF_MEM 117structure is also freed. 118.Pp 119Calling 120.Xr BIO_reset 3 121on a read/write memory BIO clears any data in it. 122On a read only BIO it restores the BIO to its original state 123and the read only data can be read again. 124.Pp 125.Xr BIO_eof 3 126is true if no data is in the BIO. 127.Pp 128.Xr BIO_ctrl_pending 3 129returns the number of bytes currently stored. 130.Pp 131.Fn BIO_set_mem_eof_return 132sets the behaviour of memory BIO 133.Fa b 134when it is empty. 135If 136.Fa v 137is zero, then an empty memory BIO will return EOF: 138it will return zero and 139.Fn BIO_should_retry 140will be false. 141If 142.Fa v 143is non-zero then it will return 144.Fa v 145when it is empty and it will set the read retry flag: 146.Fn BIO_read_retry 147is true. 148To avoid ambiguity with a normal positive return value 149.Fa v 150should be set to a negative value, typically -1. 151.Pp 152.Fn BIO_get_mem_data 153sets 154.Pf * Fa pp 155to a pointer to the start of the memory BIO's data 156and returns the total amount of data available. 157It is implemented as a macro. 158.Pp 159.Fn BIO_set_mem_buf 160sets the internal BUF_MEM structure to 161.Fa bm 162and sets the close flag to 163.Fa c . 164That is, 165.Fa c 166should be either 167.Dv BIO_CLOSE 168or 169.Dv BIO_NOCLOSE . 170.Fn BIO_set_mem_buf 171is a macro. 172.Pp 173.Fn BIO_get_mem_ptr 174places the underlying 175.Vt BUF_MEM 176structure in 177.Pf * Fa pp . 178It is a macro. 179.Pp 180.Fn BIO_new_mem_buf 181creates a memory BIO using 182.Fa len 183bytes of data at 184.Fa buf . 185If 186.Fa len 187is -1, then 188.Fa buf 189is assumed to be NUL terminated and its length is determined by 190.Xr strlen 3 . 191The BIO is set to a read only state and as a result cannot be written to. 192This is useful when some data needs to be made available 193from a static area of memory in the form of a BIO. 194The supplied data is read directly from the supplied buffer: 195it is 196.Em not 197copied first, so the supplied area of memory must be unchanged 198until the BIO is freed. 199.Pp 200Writes to memory BIOs will always succeed if memory is available: 201their size can grow indefinitely. 202.Pp 203Every read from a read/write memory BIO will remove the data just read 204with an internal copy operation. 205If a BIO contains a lot of data and it is read in small chunks, 206the operation can be very slow. 207The use of a read only memory BIO avoids this problem. 208If the BIO must be read/write then adding a buffering BIO 209to the chain will speed up the process. 210.Sh RETURN VALUES 211.Fn BIO_s_mem 212returns a pointer to a static object. 213.Pp 214.Fn BIO_set_mem_eof_return , 215.Fn BIO_get_mem_data , 216.Fn BIO_set_mem_buf , 217and 218.Fn BIO_get_mem_ptr 219return 1 on success or a value less than or equal to 0 if an error occurred. 220.Pp 221.Fn BIO_new_mem_buf 222returns a newly allocated 223.Vt BIO 224object on success or 225.Dv NULL 226on error. 227.Sh EXAMPLES 228Create a memory BIO and write some data to it: 229.Bd -literal -offset indent 230BIO *mem = BIO_new(BIO_s_mem()); 231BIO_puts(mem, "Hello World\en"); 232.Ed 233.Pp 234Create a read only memory BIO: 235.Bd -literal -offset indent 236char data[] = "Hello World"; 237BIO *mem; 238mem = BIO_new_mem_buf(data, -1); 239.Ed 240.Pp 241Extract the 242.Vt BUF_MEM 243structure from a memory BIO and then free up the BIO: 244.Bd -literal -offset indent 245BUF_MEM *bptr; 246BIO_get_mem_ptr(mem, &bptr); 247/* Make sure BIO_free() leaves BUF_MEM alone. */ 248BIO_set_close(mem, BIO_NOCLOSE); 249BIO_free(mem); 250.Ed 251.Sh SEE ALSO 252.Xr BIO_new 3 , 253.Xr BUF_MEM_new 3 254.Sh HISTORY 255.Fn BIO_s_mem 256first appeared in SSLeay 0.6.0. 257.Fn BIO_set_mem_buf 258and 259.Fn BIO_get_mem_ptr 260first appeared in SSLeay 0.6.5. 261These functions have been available since 262.Ox 2.4 . 263.Pp 264.Fn BIO_set_mem_eof_return 265and 266.Fn BIO_get_mem_data 267first appeared in SSLeay 0.9.1 and have been available since 268.Ox 2.6 . 269.Pp 270.Fn BIO_new_mem_buf 271first appeared in OpenSSL 0.9.5 and has been available since 272.Ox 2.7 . 273.Sh BUGS 274There should be an option to set the maximum size of a memory BIO. 275.Pp 276There should be a way to "rewind" a read/write BIO without destroying 277its contents. 278.Pp 279The copying operation should not occur after every small read 280of a large BIO to improve efficiency. 281