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, first line should be at most 1024 bytes long including newline
29unless the flag B<BIO_FLAGS_BASE64_NO_NL> is set.
30Further 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
33BIO_flush() on a base64 BIO that is being written through is
34used to signal that no more data is to be encoded: this is used
35to flush the final block through the BIO.
36
37The flag B<BIO_FLAGS_BASE64_NO_NL> can be set with BIO_set_flags().
38For writing, it causes all data to be written on one line without
39newline at the end.
40For reading, it removes all expectations on newlines in the input data.
41
42=head1 NOTES
43
44Because of the format of base64 encoding the end of the encoded
45block cannot always be reliably determined.
46
47=head1 RETURN VALUES
48
49BIO_f_base64() returns the base64 BIO method.
50
51=head1 EXAMPLES
52
53Base64 encode the string "Hello World\n" and write the result
54to standard output:
55
56 BIO *bio, *b64;
57 char message[] = "Hello World \n";
58
59 b64 = BIO_new(BIO_f_base64());
60 bio = BIO_new_fp(stdout, BIO_NOCLOSE);
61 BIO_push(b64, bio);
62 BIO_write(b64, message, strlen(message));
63 BIO_flush(b64);
64
65 BIO_free_all(b64);
66
67Read Base64 encoded data from standard input and write the decoded
68data to standard output:
69
70 BIO *bio, *b64, *bio_out;
71 char inbuf[512];
72 int inlen;
73
74 b64 = BIO_new(BIO_f_base64());
75 bio = BIO_new_fp(stdin, BIO_NOCLOSE);
76 bio_out = BIO_new_fp(stdout, BIO_NOCLOSE);
77 BIO_push(b64, bio);
78 while ((inlen = BIO_read(b64, inbuf, 512)) > 0)
79     BIO_write(bio_out, inbuf, inlen);
80
81 BIO_flush(bio_out);
82 BIO_free_all(b64);
83
84=head1 BUGS
85
86On decoding, if the flag B<BIO_FLAGS_BASE64_NO_NL> is not set and
87the first 1024 bytes of input do not include a newline character
88the first two lines of input are ignored.
89
90The ambiguity of EOF in base64 encoded data can cause additional
91data following the base64 encoded block to be misinterpreted.
92
93There should be some way of specifying a test that the BIO can perform
94to reliably determine EOF (for example a MIME boundary).
95
96=head1 COPYRIGHT
97
98Copyright 2000-2024 The OpenSSL Project Authors. All Rights Reserved.
99
100Licensed under the Apache License 2.0 (the "License").  You may not use
101this file except in compliance with the License.  You can obtain a copy
102in the file LICENSE in the source distribution or at
103L<https://www.openssl.org/source/license.html>.
104
105=cut
106