BIO_READ(3ossl)
π BIO_READ(3ossl) β the OpenSSL girlies' guide to BIO I/O π
okay so bestie, welcome to BIO_READ(3ossl) from the OpenSSL manual, and lemme tell you, this one is the glow-up page for all your read/write needs. it's giving "I talk to file descriptors AND sockets AND memory buffers" energy. let's get into it. β¨
πΈ NAME πΈ
BIO_read_ex, BIO_write_ex, BIO_read, BIO_write, BIO_gets, BIO_get_line, BIO_puts β the BIO I/O functions, aka the whole friend group π«Ά
β¨ 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);
no cap, that's the whole lineup. π
π¦ DESCRIPTION π¦
okay so let's go one by one, bestie, because each one has a vibe.
BIO_read_ex() tries to read dlen bytes from the BIO b and drops that data into data. and if any bytes actually got read? then the number of bytes read gets stored in *readbytes. slay.
BIO_write_ex() tries to write dlen bytes from data to the BIO b. if it succeeds, the number of bytes written goes into *written β unless written is NULL, in which case, whatever, no biggie. π
BIO_read() tries to read len bytes from BIO b and puts the data in buf.
BIO_gets() does the BIOs' "gets" operation and places the data in buf. usually this is gonna try to read a line of data from the BIO of maximum length size-1. BUT β and this is the plot twist β there are exceptions, fr fr. for example, BIO_gets() on a digest BIO will literally calculate and return the digest, and other BIOs might not support BIO_gets() at all. rude, but okay. the returned string is always NUL-terminated and the '\n' is preserved if it's present in the input data. and on binary input? there may be NUL characters chillin' inside the string; in that case the return value (if it's nonnegative) may give an incorrect length. so like, watch out for that one. π
BIO_get_line() tries to read from BIO b a line of data up to the next '\n', or until the maximum length size-1 is reached, and places the data in buf. the returned string is always NUL-terminated and the '\n' is preserved if it's 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. and to implement this? unfortunately the data needs to be read byte-by-byte. π yes, byte. by. byte. it's giving slow but honest.
BIO_write() tries to write len bytes from buf to BIO b.
BIO_puts() tries to write a NUL-terminated string buf to BIO b β without the terminating NUL byte and without appending '\n'. so, similar to fputs(3), and not puts(3). periodt. π
π RETURN VALUES π
BIO_read_ex() returns 1 if data was successfully read, and 0 otherwise.
BIO_write_ex() returns 1 if no error was encountered writing data, 0 otherwise. and requesting to write 0 bytes? not considered an error, bestie. it's fine. it's valid. it's iconic.
BIO_write() returns -2 if the "write" operation is not implemented by the BIO, or -1 on other errors. otherwise it returns the number of bytes written. this may be 0 if the BIO b is NULL or dlen <= 0.
BIO_gets() returns -2 if the "gets" operation is not implemented by the BIO, or -1 on 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 gets added after the data read is not included in the length returned.
all the 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 0 or -1. and if the return value is -2, then the operation is simply not implemented in that specific BIO type. vibe check: failed. π
π NOTES π
a 0 or -1 return is not necessarily an indication of an error. 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. so like, don't panic. breathe. π§
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. okay? okay.
see BIO_should_retry(3) for details of how to determine the cause of a retry and other I/O issues. go read it, bestie, it's important. π«Ά
if the "gets" method is not supported by a BIO, then BIO_get_line() can be used. it's 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. cute little workaround, honestly. β¨
π SEE ALSO π
π°οΈ 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. (old behavior, we don't claim it. π )BIO_get_line()was added in OpenSSL3.0.BIO_write_ex()returns1if the size of the data to write is0, and thewrittenparameter of the function can beNULLsince OpenSSL3.0.
π 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>.
and that's the whole page, bestie!! this one's from the OpenSSL 3.5.9 manual, dated 2026-09-29, and honestly? she ate. go forth and BIO_read responsibly. mwah π β¨π¦π
π¦ where this page comes from
- fedora 43: openssl-devel 1:3.5.9-1.fc43 Β· Apache-2.0
- fedora 43: openssl-devel 1:3.5.9-1.fc43 Β· Apache-2.0
- fedora 44: openssl-devel 1:3.5.9-1.fc44 Β· Apache-2.0
- fedora 44: openssl-devel 1:3.5.9-1.fc44 Β· Apache-2.0
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms