BIO series of openssl 15 --- memory (mem) Type BIO, openssl15 ---

Source: Internet
Author: User

BIO series of openssl 15 --- memory (mem) Type BIO, openssl15 ---

Mem Type BIO

--- Based on openssl doc \ crypto \ bio_s_mem.pod translation and your own understanding, write

(Author: DragonKing, Mail: wzhah@263.net, published on: http://gdwzh.126.com open

Ssl Professional Forum)

The related series of functions defined by memory (mem) Type BIO are as follows (openssl \ bio. h ):

BIO_METHOD * BIO_s_mem (void );

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)

BIO * BIO_new_mem_buf (void * buf, int len );

The memory Type BIO is source/sink Type BIO, which uses memory as its I/O. The data written into this type of BIO is

Stored in the BUF_MEM structure, this structure is defined as a structure suitable for storing data. Its structure is defined as follows:

Typedef struct buf_mem_st

{

Int length;/* current number of bytes */

Char * data;

Int max;/* size of buffer */

} BUF_MEM;

It can be seen that this structure defines three variables: Memory Data Length, data storage space, and maximum length.

Block memory storage data. However, it is worth noting that the memory of the memory Type BIO can be infinitely expanded, that is, no

And how much data you write to it.

Generally, any data written into the memory Type BIO can be read, unless the memory Type BIO is read-only.

In this case, if the read operation is performed on the read-only memory BIO, the related data will be deleted from the BIO.

(In fact, the pointer is not deleted, but the pointer is moved to the backend. If you call BIO_reset, you can access it again .)

).

[BIO_s_mem]

This function returns a memory-type BIO_METHOD structure, which is defined as follows:

Static BIO_METHOD mem_method =

{

BIO_TYPE_MEM,

"Memory buffer ",

Mem_write,

Mem_read,

Mem_puts,

Mem_gets,

Mem_ctrl,

Mem_new,

Mem_free,

NULL,

};

BIO_write and BIO_read functions are supported. Write operations on memory BIO are always successful because

The memory of the storage Type BIO can be infinitely expanded. Any read operation on the read/write memory BIO will be in use

The copy operation deletes the data segment from BIO. In this way, if BIO contains a large amount of data, only

It is a small part, which causes the operation to be very slow. Read-only memory BIO is used to avoid this problem. In

If the memory Type BIO must be read and written, you can add a Buffer Type BIO to the BIO chain.

To make the operation faster. In a later version (0.9.6a in this document), the speed may be optimized.

Operation Problems.

BIO_gets and BIO_puts operations are supported in this type of BIO.

If the BIO_CLOSE flag is set, when the memory Type BIO is released, the underlying BUF_MEM Type BIO will also

Released at the same time.

When the BIO_reset function is called, if the BIO is readable and writable, all data of the BIO will be cleared;

If the BIO is read-only, the operation simply points the pointer to the original position, and the data in it can be read again.

In this document version (0.9.6a), the memory Type BIO does not provide a read/write mode BIO that can weight the pointer.

Methods that do not destroy the original data may be added in later versions.

BIO_eof returns true, indicating that only BIO contains no readable data.

BIO_ctrl_pending returns the number of bytes of data stored in BIO.

[BIO_set_mem_eof_return]

This function sets a read Action for memory BIO with no data. If the value of v is 0

If the memory BIO is empty, the EOF is returned, that is, the returned value is 0. If BIO_should_retry is called

Returns false. If the value of v is non-zero, the system returns v and sets the retry flag.

If you call BIO_read_retry, true is returned. To avoid confusion with normal return values, v should be set to negative.

Value, typically-1.

[BIO_get_mem_data]

This function is a macro-defined function that points the pointer of the pp parameter to the start of the data of the memory Type BIO and returns

Valid data.

[BIO_set_mem_buf]

This function uses the BUF_MEM structure represented by the parameter bm as the underlying structure of the BIO, and sets the flag

The parameters c and c can be BIO_CLOSE or BIO_NOCLOSE. This function is also a macro definition.

[BIO_get_mem_ptr]

This function is also a macro-defined function that places the underlying BUF_MEM structure in the pointer pp.

[BIO_new_mem_buf]

This function creates a memory Type BIO with the data in the buf as len (unit: byte), as shown in

If the len parameter is-1, the buf ends with null by default. Use strlen to solve the length. BIO is

If it is set to read-only, write operations cannot be performed. It is used to store data in a static memory and in BIO format.

. The required data is read directly from the memory, rather than the copy operation (read/write mode ).

The memory BIO must be copied first), which requires the memory to be read-only and cannot be changed.

Released.

[Example]

1. Create a memory BIO and write data

BIO * mem = BIO_new (BIO_s_mem ());

BIO_puts (mem, "Hello World \ n ");

2. Create a read-only memory BIO

Char data [] = "Hello World ";

BIO * mem;

Mem = BIO_new_mem_buf (data,-1 );

3. Extract A BUF_MEM structure from a BIO and release the BIO.

BUF_MEM * bptr;

BIO_get_mem_ptr (mem, & bptr );

BIO_set_close (mem, BIO_NOCLOSE);/* BIO_free () does not release the BUF_MEM structure */

BIO_free (mem );

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.