
#use wml::openssl-macros area=docs page=BIO_s_mem

<title>Documents, BIO_s_mem(3)</title>

<h1>BIO_s_mem(3)</h1>

#use wml::imp::generic

{:
## What's this? [[s|(<STRONG>[^<].+?)</A>(</STRONG><DD>)|$1$2|sg]]
[[s|<P>\s+<P>|<P>|sg]]
[[s|<P>\s+</|</|sg]]
[[s|<DD>\s*<DT>|<DD>&nbsp;<DT>|sg]]
[[s|<DD>\s*</DL>|<DD>&nbsp;</DL>|sg]]
[[s|\[|&#91;|sg]]
[[s|\]|&#93;|sg]]

<!-- INDEX BEGIN -->

<UL>

	<LI><A HREF="#NAME">NAME</A>
	<LI><A HREF="#SYNOPSIS">SYNOPSIS</A>
	<LI><A HREF="#DESCRIPTION">DESCRIPTION</A>
	<LI><A HREF="#NOTES">NOTES</A>
	<LI><A HREF="#BUGS">BUGS</A>
	<LI><A HREF="#EXAMPLE">EXAMPLE</A>
	<LI><A HREF="#SEE_ALSO">SEE ALSO</A>
</UL>
<!-- INDEX END -->

<HR>
<P>
<HR>
<H1><A NAME="NAME">NAME</A></H1>
<P>
BIO_s_mem, BIO_set_mem_eof_return, BIO_get_mem_data, BIO_set_mem_buf,
BIO_get_mem_ptr, BIO_new_mem_buf - memory BIO

</P>
<P>
<HR>
<H1><A NAME="SYNOPSIS">SYNOPSIS</A></H1>
<PRE> #include &lt;openssl/bio.h&gt;
</PRE>
<PRE> BIO_METHOD *   BIO_s_mem(void);
</PRE>
<PRE> BIO_set_mem_eof_return(BIO *b,int v)
 long BIO_get_mem_data(BIO *b, char **pp)
 BIO_set_mem_buf(BIO *b,BUF_MEM *bm,int c)
 BIO_get_mem_ptr(BIO *b,BUF_MEM **pp)
</PRE>
<PRE> BIO *BIO_new_mem_buf(void *buf, int len);
</PRE>
<P>
<HR>
<H1><A NAME="DESCRIPTION">DESCRIPTION</A></H1>
<P>
<CODE>BIO_s_mem()</CODE> return the memory BIO method function. 

</P>
<P>
A memory BIO is a source/sink BIO which uses memory for its I/O. Data
written to a memory BIO is stored in a BUF_MEM structure which is extended
as appropriate to accommodate the stored data.

</P>
<P>
Any data written to a memory BIO can be recalled by reading from it. Unless
the memory BIO is read only any data read from it is deleted from the BIO.

</P>
<P>
Memory BIOs support <CODE>BIO_gets()</CODE> and <CODE>BIO_puts().</CODE>

</P>
<P>
If the BIO_CLOSE flag is set when a memory BIO is freed then the underlying
BUF_MEM structure is also freed.

</P>
<P>
Calling <CODE>BIO_reset()</CODE> on a read write memory BIO clears any data
in it. On a read only BIO it restores the BIO to its original state and the
read only data can be read again.

</P>
<P>
<CODE>BIO_eof()</CODE> is true if no data is in the BIO.

</P>
<P>
<CODE>BIO_ctrl_pending()</CODE> returns the number of bytes currently
stored.

</P>
<P>
<CODE>BIO_set_mem_eof_return()</CODE> sets the behaviour of memory BIO <STRONG>b</STRONG> when it is empty. If the <STRONG>v</STRONG> is zero then an empty memory BIO will return EOF (that is it will return
zero and <CODE>BIO_should_retry(b)</CODE> will be false. If <STRONG>v</STRONG> is non zero then it will return <STRONG>v</STRONG> when it is empty and it will set the read retry flag (that is
<CODE>BIO_read_retry(b)</CODE> is true). To avoid ambiguity with a normal
positive return value <STRONG>v</STRONG> should be set to a negative value, typically -1.

</P>
<P>
<CODE>BIO_get_mem_data()</CODE> sets <STRONG>pp</STRONG> to a pointer to the start of the memory BIOs data and returns the total
amount of data available. It is implemented as a macro.

</P>
<P>
<CODE>BIO_set_mem_buf()</CODE> sets the internal BUF_MEM structure to <STRONG>bm</STRONG> and sets the close flag to <STRONG>c</STRONG>, that is <STRONG>c</STRONG> should be either BIO_CLOSE or BIO_NOCLOSE. It is a macro.

</P>
<P>
<CODE>BIO_get_mem_ptr()</CODE> places the underlying BUF_MEM structure in <STRONG>pp</STRONG>. It is a macro.

</P>
<P>
<CODE>BIO_new_mem_buf()</CODE> creates a memory BIO using <STRONG>len</STRONG> bytes of data at <STRONG>buf</STRONG>, if <STRONG>len</STRONG> is -1 then the <STRONG>buf</STRONG> is assumed to be null terminated and its length is determined by <STRONG>strlen</STRONG>. The BIO is set to a read only state and as a result cannot be written to.
This is useful when some data needs to be made available from a static area
of memory in the form of a BIO. The supplied data is read directly from the
supplied buffer: it is <STRONG>not</STRONG> copied first, so the supplied area of memory must be unchanged until the
BIO is freed.

</P>
<P>
<HR>
<H1><A NAME="NOTES">NOTES</A></H1>
<P>
Writes to memory BIOs will always succeed if memory is available: that is
their size can grow indefinitely.

</P>
<P>
Every read from a read write memory BIO will remove the data just read with
an internal copy operation, if a BIO contains a lot of data and it is read
in small chunks the operation can be very slow. The use of a read only
memory BIO avoids this problem. If the BIO must be read write then adding a
buffering BIO to the chain will speed up the process.

</P>
<P>
<HR>
<H1><A NAME="BUGS">BUGS</A></H1>
<P>
There should be an option to set the maximum size of a memory BIO.

</P>
<P>
There should be a way to ``rewind'' a read write BIO without destroying its
contents.

</P>
<P>
The copying operation should not occur after every small read of a large
BIO to improve efficiency.

</P>
<P>
<HR>
<H1><A NAME="EXAMPLE">EXAMPLE</A></H1>
<P>
Create a memory BIO and write some data to it:

</P>
<PRE> BIO *mem = BIO_new(BIO_s_mem());
 BIO_puts(mem, &quot;Hello World\n&quot;); 
</PRE>
<P>
Create a read only memory BIO:

</P>
<PRE> char data[] = &quot;Hello World&quot;;
 BIO *mem;
 mem = BIO_new_mem_buf(data, -1);
</PRE>
<P>
Extract the BUF_MEM structure from a memory BIO and then free up the BIO:

</P>
<PRE> BUF_MEM *bptr;
 BIO_get_mem_ptr(mem, &amp;bptr);
 BIO_set_close(mem, BIO_NOCLOSE); /* So BIO_free() leaves BUF_MEM alone */
 BIO_free(mem);
 
</PRE>
<P>
<HR>
<H1><A NAME="SEE_ALSO">SEE ALSO</A></H1>
<P>
TBA
</P>
:}

