Base64 type BIO
--- Translation according to OpenSSL DOC / CRYPTO / BIO_F_BASE64.POD and its own understanding
(Author: DragonKing, Mail: wzhah@263.net, released in:
Http://openssl.126.com OpenSSL professional forum)
This type is filter type BIO, which is defined as follows (OpenSSL / BIO.H, OpenSSL / EVP.H):
BIO_METHOD * BIO_F_BASE64 (VOID);
[BIO_F_BASE64]]
This function returns a Base64 type Bio_Method structure that is defined as follows (EVP / BIO_B64.C):
Static Bio_Method Methods_B64 =
{
BIO_TYPE_BASE64,
"Base64 Encoding",
B64_WRITE,
B64_READ,
NULL, / * B64_PUTS, * /
NULL, / * B64_GETS, * /
B64_ctrl,
B64_New,
B64_free,
B64_callback_ctrl,
}
It should be noted that this type of BIO is not in the Bio directory, but in the EVP directory.
When the BIO is written to the data, the data is encoded by the base64. When reading data from the BIO, the data is decoded by Base64. This Bio does not support the functionality of Bio_Gets and Bio_PUTS.
Bio_flush is called when this type of BIO is called, indicating that the data that needs to be written has been written, used to write the last data to the BIO.
[BIO_SET_FLAGS]
This function can be used to set the tag BIO_FLAGS_BASE64_NO_NL, and after the tag is set, all data encodes will be a row or expect all data to be on one line. It should be noted that since the Base64 encoded its format, it cannot accurately determine the end position of the encoded data block. When you use it, you need to pay attention to the length of the data.
【example】
The following program uses the string "Hello World / N" encoded and writes to the standard output device.
BIO * BIO, * B64;
Char message [] = "Hello WORLD / N";
B64 = BIO_NEW (BIO_F_BASE64 ());
BIO = BIO_NEW_FP (stdout, bio_noclose);
BIO = BIO_PUSH (B64, BIO);
Bio_write (Bio, Message, Strlen (Message);
BIO_FLUSH (BIO);
BIO_FREE_ALL (BIO);
The following program reads the base64 encoded from the standard input device and outputs the decoded data to the standard output device:
BIO * BIO, * B64, BIO_OUT
Char inbuf [512];
INT INLEN
Char message [] = "Hello WORLD / N";
B64 = BIO_NEW (BIO_F_BASE64 ());
BIO = BIO_NEW_FP (stdin, bio_noclose);
BIO_OUT = BIO_NEW_FP (stdout, bio_noclose);
BIO = BIO_PUSH (B64, BIO);
While ((inlen = BIO_READ (Bio, Inbuf, Strlen (Message))> 0)
Bio_write (Bio_OUT, Inbuf, INLEN); BIO_FREE_ALL (BIO);