OpenSSL BIO Series 15 --- Memory (MEM) Type BIO

zhaozj2021-02-16  64

MEM type BIO

--- Translation according to OpenSSL DOC / CRYPTO / BIO_S_MEM.POD and its own understanding

(Author: DragonKing, Mail: wzhah@263.net, released in:

Http://gdwzh.126.com OpenSSL Professional Forum)

The relevant series functions defined by the memory (MEM) type BIO (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);

Memory BIO is Source / Sink type Bio that uses memory as its I / O. The data writes into this type of BIO is stored in the BUF_MEM structure, which is defined as a structure that is suitable for storing data, and the 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 that the memory data length, data storage space, and the maximum length of three variables to express memory data. However, it is worth noting that memory BIO's memory can be expanded, that is, how much data you write is, but you can successfully execute.

In general, any data written to the memory type BIO can be read unless the memory BIO is a read-only type, then if the read-only memory type BIO is read, the relevant data will Delete from this Bio (actually not deleted, just point the needle to move back, the access is not the case, if you call the Bio_Reset to access it).

[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,

}

The BIO_WRITE and BIO_READ functions are supported. The write operation to the memory type BIO is always successful because the memory BIO's memory can be expanded. Any read operation of the readable write-writable memory Bio will use internal copy operation from the BIO from the BIO, so that if there is a lot of data in the BIO, read only some pieces, Then lead to a very slow operation. Use only read-only memory BIO to avoid this problem. In use, if the memory BIO must be readable, you can add a buffer BIO to the BIO chain, which makes the operating speed faster. In the later version (this document is 0.9.6A), the problem of speed operation may be optimized.

BIO_GETs and BIO_PUTS operations are supported in this type of BIO.

If the BIO_CLOSE flag is set, the BUF_MEM type BIO of its underlying Bio is also released while the memory Bio is released.

When the BIO_RESET function is called, if the BIO is readily writable, all data of the BIO will be empty; if the BIO is read-only, then the action will only point the pointer to the original position, the data inside can read again. . This document The version (0.9.6A) memory type BIO does not provide a method of resetting the pointer but does not destroy the original data, which may increase in the later version of the method. Bio_eo returns True, indicating that there is no readable data on the BIO.

Bio_ctrl_pending returns the number of bytes (BYTE) of the data stored in the current BIO.

[BIO_SET_MEM_EOF_RETURN]

This function sets a behavior of performing reading actions without the memory-type BIO of the data. If the parameter V is 0, then the empty memory BIO will return EOF, that is, return to 0 at this time, if you call Bio_Shold_Retry, you will return false. If the parameter V is non-zero, then V is returned, and the retry flag will be set, that is, calling BIO_READ_RETRY at this time will return True. In order to avoid confusion with normal return positive, V should be set to a negative value, typically -1.

[BIO_GET_MEM_DATA]

This function is a macro definition function, which points the pointer of the parameter PP to the start of the memory-type BIO, and returns all 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 closing flag to parameter c, c may be BIO_CLOSE or BIO_NOCLOSE, which is also a macro definition.

[BIO_GET_MEM_PTR]

This function is also a macro definition function that puts the underlying BUF_MEM structure in the pointer PP.

[BIO_NEW_MEM_BUF]

This function creates a memory type Bio, which is the data in which the length is LEN (by Byte), if the parameter len is -1, then the default BUF is ended with NULL, using Strlen to solve the length. At this time, the BIO is set to read only and cannot be performed. It is used for data to store in a static memory and exist in the form of a BIO. The required data is read directly from memory, not first to perform copy operation (read and write memory BIO is to copy first), this is required to be read-only, can not change, continue to maintain The BIO is released.

【example】

1. Create a memory-type 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. Take 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 buf_mem structure * /

BIO_FREE (MEM);

转载请注明原文地址:https://www.9cbs.com/read-24010.html

New Post(0)