简体   繁体   English

使用BIO_printf()代替printf()有什么好处?

[英]What are the benefits to using BIO_printf() instead of printf()?

I have been reviewing example code for using OpenSSL and in every example I locate, the creator has chosen to use BIO_printf() to write things to stdout instead of printf(). 我一直在审查使用OpenSSL的示例代码,在我找到的每个示例中,创建者都选择使用BIO_printf()将内容写入stdout而不是printf()。

I have taken their code, removed the openssl/bio.h header declaration, and changed all calls to BIO_printf() to regular printf() statements. 我已经获取了他们的代码,删除了openssl / bio.h标头声明,并将对BIO_printf()的所有调用更改为常规的printf()语句。 The programs ran with identical results. 这些程序以相同的结果运行。

The problem I'm grasping with is why these coders use BIO_printf() when it takes a lot more to setup than just using printf(). 我要解决的问题是,为什么这些编码员在设置所需的时间比仅仅使用printf()还要多的时候使用BIO_printf()。 You have to include another header (which will increase program size), you need to set the file pointer to the stream you want to write to. 您必须包括另一个标头(这将增加程序大小),您需要将文件指针设置为要写入的流。 Then you can print your message to stdout. 然后,您可以将消息打印到stdout。 It seems a lot more complicated than using printf(). 似乎比使用printf()要复杂得多。

When I do a search on BIO_printf() it lists possible man pages for BIO_printf (3), but none of the pages actually contain any information! 当我在BIO_printf()上进行搜索时,它会列出BIO_printf(3)的可能的手册页,但实际上这些页中都没有任何信息!

I decided to do a benchmark test on both methods. 我决定对这两种方法进行基准测试。 I looped printf("Hey\\n"); 我循环了printf("Hey\\n"); 1,000,000 times. 1,000,000次。 Then I did it for BIO_printf(fp, "Hey\\n"); 然后我为BIO_printf(fp, "Hey\\n"); . I only timed the BIO_printf() statement and not the setting up of the file pointer (which would have increased the time). 我只给BIO_printf()语句计时,而不给文件指针设置时间(这会增加时间)。 The difference came out to printf() being ~4.7x faster than using BIO_printf(). 区别在于,printf()比使用BIO_printf()快约4.7倍。

Why are they using it? 他们为什么要使用它? What is the benefit? 有什么好处? It's my understanding that in programming you either want code to be simple or efficient, and in the case of BIO_printf() it's neither. 据我了解,在编程中您要么希望代码简单或高效,要么就BIO_printf()而言都不是。

In general, a BIO might not be writing to stdout. 通常,BIO可能不会写入stdout。

You can have a BIO that writes to a file, or null, or a socket, or a network drive, or another BIO, etc. 您可以拥有一个写入文件的BIO,或者为null,或者是一个套接字,或者是网络驱动器,或者是另一个BIO等。

By using the BIO_printf family, the code can easily be changed to have its output sent to a different location or another BIO which might do some further filtering and then pass the output onto wherever else. 通过使用BIO_printf系列,可以轻松地更改代码,以将其输出发送到其他位置或另一个BIO,后者可以进行进一步的过滤,然后将输出传递到其他任何地方。

As pointed by others, BIO can be stacked contrary to FILE . 正如其他人指出的那样, BIO可以与FILE相反地​​进行堆叠。 snprintf() and vnsprintf() were added in C99. 在C99中添加了snprintf()vnsprintf() OpenSSL/SSLeay is older than this. OpenSSL / SSLeay早于此。 Hence, the SSLeay developpers had to write their own implementation. 因此,SSLeay开发人员必须编写自己的实现。 Unfortunately, having a little used implementation leads to the performance issues described by the OP or to CVE-2016-0799 . 不幸的是,很少使用实现会导致OP或CVE-2016-0799所描述的性能问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM