File (File) type BIO
--- Translation according to OpenSSL DOC / CRYPTO / BIO / BIO_S_FILE.POD and its own understanding
(Author: DragonKing Mail: wzhah@263.net Posted: http: //gdwzh.126.com of openssl professional forum)
We have already introduced a lot of Bio's basic construction and operation, and now we have begun to further introduce each type of Bio. These introductions are basically OpenSSL-based help documents. I will join my own understanding, clarify Idea. Before starting this, I have been thinking about which type of BIO begins to be relatively appropriate, because some of these BIO types still have interrelated, such as the BIO_S_BIO type and BIO_F_SSL, and finally, consider everyone File operation is more familiar, and this type of BIO is independent, I decided to introduce from this BIO, then gradually introduce other Source / Sink Bio, and then introduce the Filter type BIO.
File (file) type BIO's related functions and definitions are as follows (OpenSSL / BIO.H):
BIO_METHOD * BIO_S_FILE (VOID);
BIO * BIO_NEW_FILE (Const Char * filename, const char * mode);
BIO * BIO_NEW_FP (File * Street, INT FLAGS);
BIO_SET_FP (BIO * B, FILE * FP, INT FLAGS);
BIO_GET_FP (BIO * B, File ** FPP);
INT BIO_READ_FILENAME (Bio * B, Char * Name)
INT BIO_WRITE_FILENAME (BIO * B, Char * Name)
INT BIO_APPEND_FILENAME (BIO * B, Char * Name)
INT BIO_RW_FILENAME (Bio * B, Char * Name)
The following will be described one by one to their roles and usage.
[BIO_S_FILE]
After the introduction, everyone should be familiar with this type of function structure. They are the basic constructor of the BIO type, BIO_S_FILE returns the file type Bio, the file type Bio encapsulates a standard file structure, it is one Source / Sink type BIO. File type BIO_METHOD structure is defined as follows:
Static Bio_Method Methods_FileP =
{
Bio_Type_File,
File Pointer,
File_write,
FILE_READ,
File_puts,
FILE_GETS,
FILE_CTRL,
FILE_NEW,
FILE_FREE,
NULL,
}
It can be seen that the FILE type of BIO defines 7 functions. These functions are implemented inside CRYPTO / BIO / BSS_FILE.C. If you want to understand the function implementation of this type of BIO, you can refer to this file. In fact, BIO_S_FILE is just a pointer to the structure of a FILE type Bio_Method, which is implemented as follows:
BIO_METHOD * BIO_S_FILE (VOID)
{
Return (& Methods_FileP);
}
In fact, from this structure, the BIO's implementation can be accessed, that is, all of the Actions of the BIO determines its actions and behavior based on its Bio_Method type (first parameter), so that BIO is used for various types. Polymorphism.
In the FILE type, use the BIO_READ and BIO_WRITE previously described BIO_READ and BIO_WRITE to read and write the bottom File data stream, and the file type BIO is support BIO_GETS and BIO_PUTS functions (if you forget the role of these functions, please refer to the front "Bio The IO Operation Function of the Series 6 - BIO "). The BIO_FLUSH function is just simply invoked the API function fflush in the file type BIO.
The BIO_RESET function redirects the file pointer to the beginning of the file, which calls the FSEEK (Stream, 0, 0) function to implement this function.
The BIO_SEEK function will file the file pointer to the defined position OFS (offset OFS that starts from the beginning of the file), which calls the file's operation function FSeek (Stream, OFS, 0), is a macro definition form, need Note that because the function calls the FSEEK function, return 0 when successful, return -1 when it fails, this is different from the standard BIO_SEEK function definition, because the standard definition is successful to return 1, failed to return non-positive value.
BIO_EOF calls the FeOf function.
If the Bio_Close flag is set in the BIO structure, the FCLOSE function is automatically called when BIO is released.
[BIO_NEW_FILE]
This function creates a file BIO based on a given MODE type, the function of the Mode parameter is the same as the meaning of the Mode parameter in the FOPEN function. The returned BIO sets the BIO_CLOSE flag. The call successfully returns a BIO, otherwise returns NULL.
In fact, the function calls the FOPEN function to open a file, then call the BIO_NEW function to create a FILE type Bio, and finally call the function BIO_SET_FP function to the BIO structure with the associated File.
[BIO_NEW_FP]
Create a file descriptor to create a FILE type BIO, parameter Flags can be BIO_CLOSE, BIO_NOCLOSE (off flag), and BIO_FP_TEXT (set the file to text mode, the default is binary mode, which is only valid for the Win32 platform). In fact, the function calls the BIO_NEW function to create a FILE type BIO, then call the function BIO_SET_FP function to the BIO structure with the associated File. It should be noted that if the next layer is packaged, stdout, stdin, and stderr, they should set the BIO_NOCLOSE flag because it is not close to it. The call successfully returns a BIO, otherwise returns NULL.
[BIO_SET_FP]
This function gaves the BIO with the file descriptor FP, and its parameter FLAGS is the same as BIO_NEW_FP. This function is a macro definition function. The call successfully returns 1, otherwise it returns 0, but the current implementation has never failed.
[BIO_GET_FP]
This function returns a file descriptor in the file type BIO and a macro definition. The call successfully returns 1, otherwise it returns 0, but the current implementation has never failed.
[BIO_TELL]
Returns the value of the position pointer. Is a macro definition function.
[BIO_READ_FILENAME, BIO_WRITE_FILENAME, BIO_APPEND_FILENAME, BIO_RW_FILENAME]
These four functions set the BIO read text name, write file name, attached file name, and file name read and written. They are all macro definition functions. The call successfully returns 1, otherwise it returns 0.
As can be seen from the above functions, because the BIO calls the various operation functions of the underlying, if there is any abnormality of the underlying function, it will be reflected in the call of the BIO. The following is a simple example of a few BIO file types:
1. The simplest example program
BIO * BIO_OUT;
BIO_OUT = BIO_NEW_FP (stdout, bio_noclose);
Bio_Printf (Bio_Out, "Hello World / N");
2. Another implementation method of the above example
BIO * BIO_OUT;
BIO_OUT = BIO_NEW (BIO_S_FILE ());
IF (Bio_Out == NULL) / * error * /
If (! bio_set, stdout, bio_noclose) / * error is wrong to standard output * /
Bio_Printf (Bio_Out, "Hello World / N");
3. Write file operations
BIO * OUT;
OUT = BIO_NEW_FILE ("FileName.txt", "W");
IF (! out) / * error * /
Bio_Printf (Out, "Hello World / N");
BIO_FREE (OUT);
4. Another implementation method of the above example
BIO * OUT;
OUT = BIO_NEW (BIO_S_FILE ());
IF (out == null) / * error ... * /
IF (! bio_write_filename (out, "filename.txt") / * error ... * /
Bio_Printf (Out, "Hello World / N");
BIO_FREE (OUT);