BIO_S_FILE(3)

πŸ“¦ version 4.4.0, in alpine 3.24, arch rolling

πŸ’– BIO_S_FILE(3) β€” the FILE BIO glow-up πŸ’–

okay so bestie, welcome to the BIO_s_file page from the libcrypto crew β€” this is your one-stop guide to the FILE BIO and all its little friends: BIO_s_file, BIO_new_file, BIO_new_fp, BIO_set_fp, BIO_get_fp, BIO_read_filename, BIO_write_filename, BIO_append_filename, and BIO_rw_filename. yes, that's a lot of names, but we're gonna get through them together 🫢


✨ SYNOPSIS ✨

first things first, you gotta grab the library β€” it's libcrypto β€” and include this:

#include <openssl/bio.h>

then all the signatures, served fresh, no edits:

const BIO_METHOD *
BIO_s_file(void);

BIO *
BIO_new_file(const char *filename, const char *mode);

BIO *
BIO_new_fp(FILE *stream, int flags);

long
BIO_set_fp(BIO *b, FILE *fp, int flags);

long
BIO_get_fp(BIO *b, FILE **fpp);

long
BIO_read_filename(BIO *b, char *name);

long
BIO_write_filename(BIO *b, char *name);

long
BIO_append_filename(BIO *b, char *name);

long
BIO_rw_filename(BIO *b, char *name);

🌸 DESCRIPTION 🌸

okay so BIO_s_file() returns the file BIO method. and literally, as the name implies, it's a wrapper around the stdio FILE structure β€” and it's a source/sink BIO, iykyk πŸ’…

when you hit it with BIO_read(3) and BIO_write(3), they read and write data to the underlying stream. BIO_gets(3) and BIO_puts(3) are supported on file BIOs too. slay.

BIO_flush(3) on a file BIO calls the fflush(3) function on the wrapped stream.

BIO_reset(3) attempts to change the file pointer to the start of file using fseek(stream, 0, 0).

BIO_seek(3) sets the file pointer to position ofs from the start of the file using fseek(stream, ofs, 0).

BIO_eof(3) calls feof(3).

setting the BIO_CLOSE flag calls fclose(3) on the stream when the BIO is freed πŸ‘‘

BIO_new_file() creates a new file BIO with mode mode. the meaning of mode is the same as for the stdio function fopen(3). the BIO_CLOSE flag is set on the returned BIO.

BIO_new_fp() creates a file BIO wrapping stream. flags can be:

  • BIO_CLOSE
  • BIO_NOCLOSE β€” the close flag
  • BIO_FP_TEXT β€” sets the underlying stream to text mode, default is binary; this only has any effect under Win32

BIO_set_fp() sets the file pointer of a file BIO to fp. flags has the same meaning as in BIO_new_fp().

BIO_get_fp() retrieves the file pointer of a file BIO.

BIO_seek(3) sets the position pointer to offset bytes from the start of file.

BIO_tell(3) returns the value of the position pointer.

BIO_read_filename(), BIO_write_filename(), BIO_append_filename(), and BIO_rw_filename() set the file BIO b to use file name for reading, writing, append or read write respectively.

ngl when wrapping stdout, stdin, or stderr, the underlying stream should not normally be closed, so the BIO_NOCLOSE flag should be set 🚨

because the file BIO calls the underlying stdio functions, any quirks in stdio behaviour will be mirrored by the corresponding BIO. no cap, that's just how it is.

on Windows, BIO_new_files() reserves for the filename argument to be UTF-8 encoded. in other words, if you have to make it work in a multilingual environment, encode file names in UTF-8 πŸ¦‹

πŸŽ€ the BIO_ctrl(3) cmd constants πŸŽ€

the following BIO_ctrl(3) cmd constants correspond to macros:

the meaning of BIO_C_SET_FILENAME depends on the flags passed in the BIO_ctrl(3) larg argument:

  • BIO_CLOSE | BIO_FP_READ β†’ BIO_read_filename()
  • BIO_CLOSE | BIO_FP_WRITE β†’ BIO_write_filename()
  • BIO_CLOSE | BIO_FP_APPEND β†’ BIO_append_filename()
  • BIO_CLOSE | BIO_FP_READ | BIO_FP_WRITE β†’ BIO_rw_filename()

⭐ RETURN VALUES ⭐

  • BIO_s_file() returns the file BIO method.
  • BIO_new_file() and BIO_new_fp() return a file BIO or NULL if an error occurred.
  • when called on a file BIO object, BIO_method_type(3) returns the constant BIO_TYPE_FILE and BIO_method_name(3) returns a pointer to the static string "FILE pointer".
  • BIO_set_fp() and BIO_get_fp() return 1 for success or 0 for failure (although the current implementation never returns 0 πŸ‘€).
  • BIO_seek(3) returns the same value as the underlying fseek(3) function: 0 for success or -1 for failure.
  • BIO_tell(3) returns the current file position.
  • BIO_read_filename(), BIO_write_filename(), BIO_append_filename(), and BIO_rw_filename() return 1 for success or 0 for failure.

πŸ¦‹ EXAMPLES πŸ¦‹

file BIO "hello world", the classic:

BIO *bio_out;
bio_out = BIO_new_fp(stdout, BIO_NOCLOSE);
BIO_printf(bio_out, "Hello World\n");

alternative technique, for when you're feeling spicy:

BIO *bio_out;
bio_out = BIO_new(BIO_s_file());
if(bio_out == NULL) /* Error ... */
if(!BIO_set_fp(bio_out, stdout, BIO_NOCLOSE)) /* Error ... */
BIO_printf(bio_out, "Hello World\n");

write to a file, main character energy:

BIO *out;
out = BIO_new_file("filename.txt", "w");
if(!out) /* Error occurred */
BIO_printf(out, "Hello World\n");
BIO_free(out);

alternative technique, again because we contain multitudes:

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);

πŸ’œ SEE ALSO πŸ’œ

BIO_new(3), BIO_read(3), BIO_seek(3) β€” go read those next bestie, they're the supporting cast 🫢


🌈 HISTORY 🌈

BIO_s_file(), BIO_set_fp(), BIO_get_fp(), BIO_read_filename(), BIO_write_filename(), and BIO_append_filename() first appeared in SSLeay 0.6.0. BIO_new_file() and BIO_new_fp() first appeared in SSLeay 0.8.0. all these functions have been available since OpenBSD 2.4.

BIO_rw_filename() first appeared in SSLeay 0.9.1 and has been available since OpenBSD 2.6. iconic longevity honestly πŸ’…


πŸ’€ BUGS πŸ’€

BIO_reset(3) and BIO_seek(3) are implemented using fseek(3) on the underlying stream. the return value for fseek(3) is 0 for success or -1 if an error occurred. this differs from other types of BIO which will typically return 1 for success and a non-positive value if an error occurred. so like… watch out for that inconsistency, fr fr 😭


and that's the whole page bestie πŸŽ€ written under the Debian banner, dated June 8, 2025 β€” go forth and BIO responsibly πŸ’–βœ¨

πŸ“¦ where this page comes from