BIO_F_BUFFER(3ossl)

✨ BIO_F_BUFFER πŸ’… (the OpenSSL manual, bestie edition)

okay so. you're in the OpenSSL docs, section 3ossl, and today's main character is the buffering BIO. yes bestie, the one that holds your data in a lil internal cushion so you don't have to think about it. slay. let's get into it πŸŽ€


🌸 NAME 🌸

  • BIO_get_buffer_num_lines β€” tells you how many lines are sitting in the buffer
  • BIO_set_read_buffer_size β€” sets the read buffer size
  • BIO_set_write_buffer_size β€” sets the write buffer size
  • BIO_set_buffer_size β€” sets both, together, iconic
  • BIO_set_buffer_read_data β€” forces data into the read buffer
  • BIO_f_buffer β€” returns the buffering BIO method itself

it's giving "buffering BIO" energy. periodt. πŸ’–


✨ SYNOPSIS ✨

#include <openssl/bio.h>

const BIO_METHOD *BIO_f_buffer(void);

long BIO_get_buffer_num_lines(BIO *b);
long BIO_set_read_buffer_size(BIO *b, long size);
long BIO_set_write_buffer_size(BIO *b, long size);
long BIO_set_buffer_size(BIO *b, long size);
long BIO_set_buffer_read_data(BIO *b, void *buf, long num);

πŸ’– DESCRIPTION πŸ’–

so BIO_f_buffer() returns the buffering BIO method. that's the whole vibe check.

when you write data to a buffering BIO, it gets buffered and then periodically flushed out to the next BIO in the chain. when you read from it, the bytes come out of an internal buffer which got filled from the next BIO in the chain. and yes, both BIO_gets() and BIO_puts() are supported. we love a versatile queen πŸ‘‘

calling BIO_reset() on a buffering BIO clears any buffered data. poof. gone. πŸ’€

  • BIO_get_buffer_num_lines() β€” returns how many lines are currently buffered, i.e. how many are chillin in there
  • BIO_set_read_buffer_size(), BIO_set_write_buffer_size() and BIO_set_buffer_size() β€” these set the read, write, or both read and write buffer sizes to size. the initial buffer size is DEFAULT_BUFFER_SIZE, which is currently 4096. any attempt to reduce the buffer size below DEFAULT_BUFFER_SIZE gets ignored, no cap, it simply will not go lower. also, any buffered data gets cleared when the buffer is resized, so be aware 🫢
  • BIO_set_buffer_read_data() β€” clears the read buffer and fills it with num bytes of buf. if num is larger than the current buffer size, the buffer gets expanded. growth mindset fr fr 🌱

πŸ¦‹ NOTES πŸ¦‹

these functions, other than BIO_f_buffer(), are implemented as macros. yes really, iykyk.

buffering BIOs implement BIO_read_ex() and BIO_gets() by doing BIO_read_ex() operations on the next BIO in the chain and storing the result in an internal buffer, from which bytes are handed back to the caller as appropriate for the call. a BIO_gets() is guaranteed to give the caller a whole line, and BIO_read_ex() is guaranteed to give the caller the number of bytes it asks for β€” unless there's an error or end of communication is reached in the next BIO. so by prepending a buffering BIO to a chain, you can provide BIO_gets() or exact-size BIO_read_ex() functionality even if the following BIOs don't support it. main character energy honestly πŸ’…

🚨 do NOT add more than one BIO_f_buffer() to a BIO chain. the result of doing so will force a full read of the size of the internal buffer of the top BIO_f_buffer(), which is 4 KiB at a minimum. one is enough bestie, pls 😭

data is only written to the next BIO in the chain when the write buffer fills or when BIO_flush() is called. so it's super important to call BIO_flush() whenever any pending data should be written β€” like when you're removing a buffering BIO using BIO_pop(). also BIO_flush() may need to be retried if the ultimate source/sink BIO is non blocking. just keep retrying, you got this 🌈


πŸ‘‘ RETURN VALUES πŸ‘‘

  • BIO_f_buffer() β€” returns the buffering BIO method
  • BIO_get_buffer_num_lines() β€” returns the number of lines buffered (may be 0) or a negative value in case of errors
  • BIO_set_read_buffer_size(), BIO_set_write_buffer_size() and BIO_set_buffer_size() β€” return 1 if the buffer was successfully resized, or <=0 for failure
  • BIO_set_buffer_read_data() β€” returns 1 if the data was set correctly, or <=0 if there was an error

🌈 SEE ALSO 🌈

bio(7), BIO_reset(3), BIO_flush(3), BIO_pop(3), BIO_ctrl(3).

go read those next, bestie, they're all connected 🧚


Copyright 2000-2020 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 that's the whole page, mwah πŸ’‹ this was OpenSSL version 3.5.9, dated 2026-09-29, and it was lowkey iconic of you to read the whole thing. stan a buffering BIO. bye bestie πŸŽ€βœ¨πŸ¦‹

πŸ“¦ where this page comes from