1=pod 2 3=head1 NAME 4 5BIO_f_base64 - base64 BIO filter 6 7=head1 SYNOPSIS 8 9=for openssl multiple includes 10 11 #include <openssl/bio.h> 12 #include <openssl/evp.h> 13 14 const BIO_METHOD *BIO_f_base64(void); 15 16=head1 DESCRIPTION 17 18BIO_f_base64() returns the base64 BIO method. This is a filter 19BIO that base64 encodes any data written through it and decodes 20any data read through it. 21 22Base64 BIOs do not support BIO_gets() or BIO_puts(). 23 24For writing, by default output is divided to lines of length 64 25characters and there is a newline at the end of output. 26This behavior can be changed with B<BIO_FLAGS_BASE64_NO_NL> flag. 27 28For reading, the first line of base64 content should be at most 1024 bytes long 29including newline unless the flag B<BIO_FLAGS_BASE64_NO_NL> is set. 30Subsequent input lines can be of any length (i.e., newlines may appear anywhere 31in the input) and a newline at the end of input is not needed. 32 33Also when reading, unless the flag B<BIO_FLAGS_BASE64_NO_NL> is set, initial 34lines that contain non-base64 content (whitespace is tolerated and ignored) are 35skipped, as are lines longer than 1024 bytes. 36Decoding starts with the first line that is shorter than 1024 bytes (including 37the newline) and consists of only (at least one) valid base64 characters plus 38optional whitespace. 39Decoding stops when base64 padding is encountered, a soft end-of-input 40character (B<->, see L<EVP_DecodeUpdate(3)>) occurs as the first byte after a 41complete group of 4 valid base64 characters is decoded, or when an error occurs 42(e.g. due to input characters other than valid base64 or whitespace). 43 44If decoding stops as a result of an error, the first L<BIO_read(3)> that 45returns no decoded data will typically return a negative result, rather 46than 0 (which indicates normal end of input). 47However, a negative return value can also occur if the underlying BIO 48supports retries, see L<BIO_should_read(3)> and L<BIO_set_mem_eof_return(3)>. 49 50BIO_flush() on a base64 BIO that is being written through is 51used to signal that no more data is to be encoded: this is used 52to flush the final block through the BIO. 53 54The flag B<BIO_FLAGS_BASE64_NO_NL> can be set with BIO_set_flags(). 55For writing, it causes all data to be written on one line without 56newline at the end. 57For reading, it removes all expectations on newlines in the input data. 58 59=head1 NOTES 60 61Because of the format of base64 encoding the end of the encoded 62block cannot always be reliably determined. 63 64=head1 RETURN VALUES 65 66BIO_f_base64() returns the base64 BIO method. 67 68=head1 EXAMPLES 69 70Base64 encode the string "Hello World\n" and write the result 71to standard output: 72 73 BIO *bio, *b64; 74 char message[] = "Hello World \n"; 75 76 b64 = BIO_new(BIO_f_base64()); 77 bio = BIO_new_fp(stdout, BIO_NOCLOSE); 78 BIO_push(b64, bio); 79 BIO_write(b64, message, strlen(message)); 80 BIO_flush(b64); 81 82 BIO_free_all(b64); 83 84Read base64 encoded data from standard input and write the decoded 85data to standard output: 86 87 BIO *bio, *b64, *bio_out; 88 char inbuf[512]; 89 int inlen; 90 91 b64 = BIO_new(BIO_f_base64()); 92 bio = BIO_new_fp(stdin, BIO_NOCLOSE); 93 bio_out = BIO_new_fp(stdout, BIO_NOCLOSE); 94 BIO_push(b64, bio); 95 while ((inlen = BIO_read(b64, inbuf, 512)) > 0) 96 BIO_write(bio_out, inbuf, inlen); 97 98 BIO_flush(bio_out); 99 BIO_free_all(b64); 100 101=head1 BUGS 102 103The hyphen character (B<->) is treated as an ad hoc soft end-of-input 104character when it occurs at the start of a base64 group of 4 encoded 105characters. 106 107This heuristic works to detect the ends of base64 blocks in PEM or 108multi-part MIME, provided there are no stray hyphens in the middle 109input. 110But it is just a heuristic, and sufficiently unusual input could produce 111unexpected results. 112 113There should perhaps be some way of specifying a test that the BIO can perform 114to reliably determine EOF (for example a MIME boundary). 115 116It may be possible for L<BIO_read(3)> to return zero, rather than -1, even if 117an error has been detected, more tests are needed to cover all the potential 118error paths. 119 120=head1 SEE ALSO 121 122L<BIO_read(3)>, 123L<BIO_should_read(3)>, 124L<BIO_set_mem_eof_return(3)>, 125L<EVP_DecodeUpdate(3)>. 126 127=head1 COPYRIGHT 128 129Copyright 2000-2024 The OpenSSL Project Authors. All Rights Reserved. 130 131Licensed under the Apache License 2.0 (the "License"). You may not use 132this file except in compliance with the License. You can obtain a copy 133in the file LICENSE in the source distribution or at 134L<https://www.openssl.org/source/license.html>. 135 136=cut 137