xref: /freebsd/crypto/openssl/doc/man3/BIO_f_base64.pod (revision a7148ab39c03abd4d1a84997c70bf96f15dd2a09)
1e71b7053SJung-uk Kim=pod
2e71b7053SJung-uk Kim
3e71b7053SJung-uk Kim=head1 NAME
4e71b7053SJung-uk Kim
5e71b7053SJung-uk KimBIO_f_base64 - base64 BIO filter
6e71b7053SJung-uk Kim
7e71b7053SJung-uk Kim=head1 SYNOPSIS
8e71b7053SJung-uk Kim
9b077aed3SPierre Pronchery=for openssl multiple includes
10e71b7053SJung-uk Kim
11e71b7053SJung-uk Kim #include <openssl/bio.h>
12e71b7053SJung-uk Kim #include <openssl/evp.h>
13e71b7053SJung-uk Kim
14e71b7053SJung-uk Kim const BIO_METHOD *BIO_f_base64(void);
15e71b7053SJung-uk Kim
16e71b7053SJung-uk Kim=head1 DESCRIPTION
17e71b7053SJung-uk Kim
18e71b7053SJung-uk KimBIO_f_base64() returns the base64 BIO method. This is a filter
19e71b7053SJung-uk KimBIO that base64 encodes any data written through it and decodes
20e71b7053SJung-uk Kimany data read through it.
21e71b7053SJung-uk Kim
22e71b7053SJung-uk KimBase64 BIOs do not support BIO_gets() or BIO_puts().
23e71b7053SJung-uk Kim
24*a7148ab3SEnji CooperFor writing, by default output is divided to lines of length 64
25*a7148ab3SEnji Coopercharacters and there is a newline at the end of output.
26*a7148ab3SEnji CooperThis behavior can be changed with B<BIO_FLAGS_BASE64_NO_NL> flag.
275ac766abSJung-uk Kim
28*a7148ab3SEnji CooperFor reading, first line should be at most 1024 bytes long including newline
29*a7148ab3SEnji Cooperunless the flag B<BIO_FLAGS_BASE64_NO_NL> is set.
30*a7148ab3SEnji CooperFurther input lines can be of any length (i.e., newlines may appear anywhere
31*a7148ab3SEnji Cooperin the input) and a newline at the end of input is not needed.
325ac766abSJung-uk Kim
33e71b7053SJung-uk KimBIO_flush() on a base64 BIO that is being written through is
34e71b7053SJung-uk Kimused to signal that no more data is to be encoded: this is used
35e71b7053SJung-uk Kimto flush the final block through the BIO.
36e71b7053SJung-uk Kim
37*a7148ab3SEnji CooperThe flag B<BIO_FLAGS_BASE64_NO_NL> can be set with BIO_set_flags().
385ac766abSJung-uk KimFor writing, it causes all data to be written on one line without
395ac766abSJung-uk Kimnewline at the end.
40*a7148ab3SEnji CooperFor reading, it removes all expectations on newlines in the input data.
41e71b7053SJung-uk Kim
42e71b7053SJung-uk Kim=head1 NOTES
43e71b7053SJung-uk Kim
44e71b7053SJung-uk KimBecause of the format of base64 encoding the end of the encoded
45e71b7053SJung-uk Kimblock cannot always be reliably determined.
46e71b7053SJung-uk Kim
47e71b7053SJung-uk Kim=head1 RETURN VALUES
48e71b7053SJung-uk Kim
49e71b7053SJung-uk KimBIO_f_base64() returns the base64 BIO method.
50e71b7053SJung-uk Kim
51e71b7053SJung-uk Kim=head1 EXAMPLES
52e71b7053SJung-uk Kim
53e71b7053SJung-uk KimBase64 encode the string "Hello World\n" and write the result
54e71b7053SJung-uk Kimto standard output:
55e71b7053SJung-uk Kim
56e71b7053SJung-uk Kim BIO *bio, *b64;
57e71b7053SJung-uk Kim char message[] = "Hello World \n";
58e71b7053SJung-uk Kim
59e71b7053SJung-uk Kim b64 = BIO_new(BIO_f_base64());
60e71b7053SJung-uk Kim bio = BIO_new_fp(stdout, BIO_NOCLOSE);
61e71b7053SJung-uk Kim BIO_push(b64, bio);
62e71b7053SJung-uk Kim BIO_write(b64, message, strlen(message));
63e71b7053SJung-uk Kim BIO_flush(b64);
64e71b7053SJung-uk Kim
65e71b7053SJung-uk Kim BIO_free_all(b64);
66e71b7053SJung-uk Kim
67e71b7053SJung-uk KimRead Base64 encoded data from standard input and write the decoded
68e71b7053SJung-uk Kimdata to standard output:
69e71b7053SJung-uk Kim
70e71b7053SJung-uk Kim BIO *bio, *b64, *bio_out;
71e71b7053SJung-uk Kim char inbuf[512];
72e71b7053SJung-uk Kim int inlen;
73e71b7053SJung-uk Kim
74e71b7053SJung-uk Kim b64 = BIO_new(BIO_f_base64());
75e71b7053SJung-uk Kim bio = BIO_new_fp(stdin, BIO_NOCLOSE);
76e71b7053SJung-uk Kim bio_out = BIO_new_fp(stdout, BIO_NOCLOSE);
77e71b7053SJung-uk Kim BIO_push(b64, bio);
78e71b7053SJung-uk Kim while ((inlen = BIO_read(b64, inbuf, 512)) > 0)
79e71b7053SJung-uk Kim     BIO_write(bio_out, inbuf, inlen);
80e71b7053SJung-uk Kim
81e71b7053SJung-uk Kim BIO_flush(bio_out);
82e71b7053SJung-uk Kim BIO_free_all(b64);
83e71b7053SJung-uk Kim
84e71b7053SJung-uk Kim=head1 BUGS
85e71b7053SJung-uk Kim
86*a7148ab3SEnji CooperOn decoding, if the flag B<BIO_FLAGS_BASE64_NO_NL> is not set and
87*a7148ab3SEnji Cooperthe first 1024 bytes of input do not include a newline character
88*a7148ab3SEnji Cooperthe first two lines of input are ignored.
89*a7148ab3SEnji Cooper
90e71b7053SJung-uk KimThe ambiguity of EOF in base64 encoded data can cause additional
91e71b7053SJung-uk Kimdata following the base64 encoded block to be misinterpreted.
92e71b7053SJung-uk Kim
93e71b7053SJung-uk KimThere should be some way of specifying a test that the BIO can perform
94e71b7053SJung-uk Kimto reliably determine EOF (for example a MIME boundary).
95e71b7053SJung-uk Kim
96e71b7053SJung-uk Kim=head1 COPYRIGHT
97e71b7053SJung-uk Kim
98*a7148ab3SEnji CooperCopyright 2000-2024 The OpenSSL Project Authors. All Rights Reserved.
99e71b7053SJung-uk Kim
100b077aed3SPierre ProncheryLicensed under the Apache License 2.0 (the "License").  You may not use
101e71b7053SJung-uk Kimthis file except in compliance with the License.  You can obtain a copy
102e71b7053SJung-uk Kimin the file LICENSE in the source distribution or at
103e71b7053SJung-uk KimL<https://www.openssl.org/source/license.html>.
104e71b7053SJung-uk Kim
105e71b7053SJung-uk Kim=cut
106