11 of BIO series of openssl --- file Type BIO and openssl11 ---

Source: Internet
Author: User

11 of BIO series of openssl --- file Type BIO and openssl11 ---

File Type BIO

--- Based on openssl doc/crypto/bio/bio_s_file.pod translation and your own understanding, write

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

Ssl Professional Forum)

We have already introduced the basic construction and operations of BIO. Now, we start

I/O for further introduction, these introductions are basically based on openssl help documentation, I try to add myself

And clarify the ideas. Before starting this part, I have been wondering which type of BIO to start to compare.

Yes, because some of these BIO types are related to each other. For example, BIO_s_bio and BIO_f_ssl have

Finally, considering that everyone is familiar with file operations and this type of BIO is relatively independent

BIO, then gradually introduce other source/sink BIO, and then introduce the filter BIO.

Related functions and definitions of file Type BIO 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 * stream, 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 describes their functions and usage.

[BIO_s_file]

After the previous introduction, you should be familiar with the function structure of this type. They are generating BIO types.

BIO_s_file returns the file type BIO. The file type BIO encapsulates a standard file.

Component structure, which is a source/sink Type BIO. The BIO_METHOD structure of the file type 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,

};

We can see that the file type BIO defines 7 functions, and these functions are implemented in Crypto \ bio \ bss_f

In ile. c, if you want to know 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. Its function implementation is as follows:

BIO_METHOD * BIO_s_file (void)

{

Return (& methods_filep );

}

In fact, from this structure, we can see the first class of BIO implementation, that is, all BIO actions are based on its BIO _

METHOD (the first parameter) to determine its action and behavior, so as to realize BIO polymorphism for various types

.

In the file type, use BIO_read and BIO_write described earlier to read the underlying file data stream.

Write operation. The file type BIO supports BIO_gets and BIO_puts functions (if you forget the functions of these functions

For more information, see BIO-series I/O operation functions of 6---BIO.

The BIO_flush function simply calls the API function fflush in the file type BIO.

The BIO_reset function then points the file pointer to the start position of the file, which calls fseek (stream)

Function.

The BIO_seek function positions the file pointer to the defined position on ofs (the offset calculated from the beginning of the file ).

Ofs), which calls the file's operation function fseek (stream, ofs, 0). It is a macro-Defined Function and requires

Note that, because this function calls the fseek function, 0 is returned when the function is successful, and-is returned when the function fails-

1. This is different from the definition of the standard BIO_seek function, because the standard definition is that 1 is returned successfully, and the non-

Positive.

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.

Number.

[BIO_new_file]

This function creates a file BIO Based on the given mode type. The mode Parameter Function and the function in the fopen function mo

The meaning of the de parameter is the same. The BIO_CLOSE flag is set for the returned BIO. If the call is successful, a BIO is returned. Otherwise

Returns NULL.

In fact, this function first calls the fopen function to open a file, and then calls the BIO_new function to create

File Type BIO, and finally call the BIO_set_fp function to help BIO structure and related file.

[BIO_new_fp]

Create a file Type BIO with the file descriptor. The Flags parameter can be BIO_CLOSE or BIO_NOCLOSE (Off

Closed flag) and BIO_FP_TEXT (set the file to text mode. The default value is binary mode. This option only applies

Valid for Win32 platforms ). In fact, this function calls the BIO_new function to create a file type BIO, and then calls the Function

The BIO_set_fp function helps the BIO structure and related file. Note that if the underlying encapsulation is std

Out, stdin, and stderr. Because they are not closed normally, BIO_NOCLOSE should be set.

If the call is successful, a BIO is returned. Otherwise, NULL is returned.

[BIO_set_fp]

This function associates BIO with the file descriptor fp. The flags parameter has the same meaning as BIO_new_fp.

. This function is a macro-defined function. If the call is successful, 1 is returned; otherwise, 0 is returned, but the current implementation is never

A failure may occur.

[BIO_get_fp]

This function returns the file descriptor in file type BIO, which is also a macro definition. 1 is returned if the call is successful. Otherwise, 1 is returned.

Returns 0, but the current implementation will never fail.

[BIO_tell]

Returns the value of the position pointer. Is a macro-defined function.

[BIO_read_filename, BIO_write_filename, BIO_append_filename, BIO_rw_fi

Lename]

These four functions respectively set the BIO read file name, Write File Name, additional file name, and read/write file name. He

Are some macro-defined functions. If the call is successful, 1 is returned. Otherwise, 0 is returned.

From the introduction of the above functions, we can see that BIO calls various underlying operation functions.

Any exceptions in calling layer functions are reflected in BIO calls.

Below are some simple examples of BIO file operations:

1. The simplest instance 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_fp (bio_out, stdout, BIO_NOCLOSE)/* if an error occurs, the file stream is directed to

Quasi-output */

BIO_printf (bio_out, "Hello World \ n ");

3. file write 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 );

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.