BIO_F_BASE64(3)

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

πŸ’… BIO_F_BASE64(3) πŸ’…

okay so this is a page from the Library Functions Manual (libcrypto division, we love her) and it's all about the base64 BIO filter. buckle up bestie, it's actually so good.


✨ NAME ✨

BIO_f_base64 β€” the base64 BIO filter, aka the girly who encodes and decodes for you πŸŽ€


🌸 SYNOPSIS 🌸

library β€œlibcrypto” #include <openssl/bio.h>
#include <openssl/evp.h>

const BIO_METHOD *
BIO_f_base64(void);

πŸ¦‹ DESCRIPTION πŸ¦‹

So BIO_f_base64() returns the base64 BIO method. it's a filter BIO, which means it sits there and base64 encodes any data you write through it, and decodes any data you read through it. it's giving translator. it's giving bilingual icon. periodt πŸ’…

heads up bestie: base64 BIOs do not support BIO_gets(3) or BIO_puts(3). like, at all. not their thing. πŸ™ˆ

BIO_flush(3) on a base64 BIO that you're writing through is how you signal "okay that's it, no more data to encode" β€” it's literally what flushes that final block through the BIO. think of it as the "and that's a wrap" moment ✨

Now, if you want to encode all the data on one line and also expect the data to all be on one line, you set up your base64 BIO like this:

BIO *b64 = BIO_new(BIO_f_base64());
BIO_set_flags(b64, BIO_FLAGS_BASE64_NO_NL);

no newlines, no drama, just vibes 🌈


πŸ‘‘ RETURN VALUES πŸ‘‘

BIO_f_base64() returns the base64 BIO method. that's it, that's the return value πŸ’–

And when you call BIO_method_type(3) on a base64 BIO object, it hands you back the constant BIO_TYPE_BASE64. meanwhile BIO_method_name(3) returns a pointer to the static string "base64 encoding". iconic string, no notes 🫢


πŸ”₯ EXAMPLES πŸ”₯

Example 1: base64 encode the string "hello, world\n" and write the result to standard output. slay:

BIO *bio, *b64;
char message[] = "hello, world\n";

b64 = BIO_new(BIO_f_base64());
bio = BIO_new_fp(stdout, BIO_NOCLOSE);
BIO_push(b64, bio);
BIO_write(b64, message, strlen(message));
BIO_flush(b64);

BIO_free_all(b64);

Example 2: read Base64-encoded data from standard input and write the decoded data to standard output. the way this one just works 😭:

BIO *bio, *b64, *bio_out;
char inbuf[512];
int inlen;

b64 = BIO_new(BIO_f_base64());
bio = BIO_new_fp(stdin, BIO_NOCLOSE);
bio_out = BIO_new_fp(stdout, BIO_NOCLOSE);
BIO_push(b64, bio);
while((inlen = BIO_read(b64, inbuf, 512)) > 0)
        BIO_write(bio_out, inbuf, inlen);

BIO_flush(bio_out);
BIO_free_all(b64);

🧚 SEE ALSO 🧚

go read those next, iykyk πŸ’œ


⭐ HISTORY ⭐

BIO_f_base64() first appeared in SSLeay 0.6.5 and has been available since OpenBSD 2.4. she's been around. she's a legacy queen πŸ‘‘


πŸ’€ BUGS πŸ’€

okay so this part is lowkey a mess, ngl:

  • the ambiguity of EOF in base64-encoded data can cause additional data following the base64-encoded block to be misinterpreted. yeah. that's a real thing. 🫠
  • there should be some way of specifying a test that the BIO can perform to reliably determine EOF (for example, a MIME boundary). as of right now, there just isn't. fr fr.
tl;dr: EOF detection in base64 land is kinda giving chaos and the manpage itself is asking for a fix πŸ™ˆ

and that's the whole page bestie!! this one's from the Debian crew, dated June 8, 2025 β€” go forth and base64 responsibly πŸ’–βœ¨πŸŽ€

πŸ“¦ where this page comes from