BIO_READ(3ossl)
πΈ NAME πΈ
okay bestie, welcome to the OpenSSL page for BIO_READ(3ossl) β the home of the iconic BIO I/O functions: BIO_read_ex, BIO_write_ex, BIO_read, BIO_write, BIO_gets, BIO_get_line, and BIO_puts. if you're moving data through a BIO, this is your main character energy list π
β¨ SYNOPSIS β¨
#include <openssl/bio.h>
int BIO_read_ex(BIO *b, void *data, size_t dlen, size_t *readbytes);
int BIO_write_ex(BIO *b, const void *data, size_t dlen, size_t *written);
int BIO_read(BIO *b, void *data, int dlen);
int BIO_gets(BIO *b, char *buf, int size);
int BIO_get_line(BIO *b, char *buf, int size);
int BIO_write(BIO *b, const void *data, int dlen);
int BIO_puts(BIO *b, const char *buf);
π¦ DESCRIPTION π¦
okay so here's the tea, function by function π«β¨
BIO_read_ex()β attempts to readdlenbytes from BIOband places the data indata. If any bytes were successfully read then the number of bytes read is stored in*readbytes.BIO_write_ex()β attempts to writedlenbytes fromdatato BIOb. If successful then the number of bytes written is stored in*writtenunlesswrittenisNULL.BIO_read()β attempts to readlenbytes from BIOband places the data inbuf.BIO_gets()β performs the BIOs"gets"operation and places the data inbuf. Usually this operation will attempt to read a line of data from the BIO of maximum lengthsize-1. There are exceptions to this, however; for example,BIO_gets()on a digest BIO will calculate and return the digest and other BIOs may not supportBIO_gets()at all. The returned string is always NUL-terminated and the'\n'is preserved if present in the input data. On binary input there may be NUL characters within the string; in this case the return value (if nonnegative) may give an incorrect length. iykyk πBIO_get_line()β attempts to read from BIOba line of data up to the next'\n'or the maximum lengthsize-1is reached and places the data inbuf. The returned string is always NUL-terminated and the'\n'is preserved if present in the input data. On binary input there may be NUL characters within the string; in this case the return value (if nonnegative) gives the actual length read. For implementing this, unfortunately the data needs to be read byte-by-byte. not the byte-by-byte πBIO_write()β attempts to writelenbytes frombufto BIOb.BIO_puts()β attempts to write a NUL-terminated stringbufto BIOb, without the terminating NUL byte and without appending'\n'(so, similar tofputs(3), and notputs(3)).
π RETURN VALUES π
vibe check on the return values, bestie π
BIO_read_ex()returns1if data was successfully read, and0otherwise.BIO_write_ex()returns1if no error was encountered writing data,0otherwise. Requesting to write0bytes is not considered an error.BIO_write()returns-2if the"write"operation is not implemented by the BIO or-1on other errors. Otherwise it returns the number of bytes written. This may be0if the BIObisNULLordlen <= 0.BIO_gets()returns-2if the"gets"operation is not implemented by the BIO or-1on other errors. Otherwise it typically returns the amount of data read, but depending on the implementation it may return only the length up to the first NUL character contained in the data read. In any case the trailing NUL that is added after the data read is not included in the length returned.- All other functions return either the amount of data successfully read or written (if the return value is positive) or that no data was successfully read or written if the result is
0or-1. If the return value is-2then the operation is not implemented in the specific BIO type.
π§ NOTES π§
a0or-1return is not necessarily an indication of an error. fr fr π
In particular when the source/sink is nonblocking or of a certain type it may merely be an indication that no data is currently available and that the application should retry the operation later. not me refreshing for data π
One technique sometimes used with blocking sockets is to use a system call (such as select(), poll() or equivalent) to determine when data is available and then call read() to read the data. The equivalent with BIOs (that is call select() on the underlying I/O structure and then call BIO_read() to read the data) should not be used because a single call to BIO_read() can cause several reads (and writes in the case of SSL BIOs) on the underlying I/O structure and may block as a result. Instead select() (or equivalent) should be combined with non blocking I/O so successive reads will request a retry instead of blocking. do not do the cursed version π«
See BIO_should_retry(3) for details of how to determine the cause of a retry and other I/O issues. that's your retry bible, bestie πβ¨
If the "gets" method is not supported by a BIO then BIO_get_line() can be used. It is also possible to make BIO_gets() usable even if the "gets" method is not supported by adding a buffering BIO BIO_f_buffer(3) to the chain. love that for us π«Ά
π SEE ALSO π
BIO_should_retry(3) β that's the one. β¨
β HISTORY β
BIO_gets()on1.1.0and older when called onBIO_fd()based BIO did not keep the'\n'at the end of the line in the buffer.BIO_get_line()was added in OpenSSL3.0.- Since OpenSSL
3.0,BIO_write_ex()returns1if the size of the data to write is0, and thewrittenparameter of the function can beNULL.
π COPYRIGHT π
Copyright 2000-2026 The OpenSSL Project Authors. All Rights Reserved.
Licensed under the Apache License 2.0 (the "License"). You may not use this file except in compliance with the License. You can obtain a copy in the file LICENSE in the source distribution or at <https://www.openssl.org/source/license.html>.
okay byeee bestie π β this page is OpenSSL 3.5.6, dated 2026-04-07. periodt. π«Ά
π¦ where this page comes from
- freebsd 15.1: base 15.1
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms