xref: /freebsd/crypto/openssl/doc/man3/BIO_s_file.pod (revision e71b70530d95c4f34d8bdbd78d1242df1ba4a945)
1*e71b7053SJung-uk Kim=pod
2*e71b7053SJung-uk Kim
3*e71b7053SJung-uk Kim=head1 NAME
4*e71b7053SJung-uk Kim
5*e71b7053SJung-uk KimBIO_s_file, BIO_new_file, BIO_new_fp, BIO_set_fp, BIO_get_fp,
6*e71b7053SJung-uk KimBIO_read_filename, BIO_write_filename, BIO_append_filename,
7*e71b7053SJung-uk KimBIO_rw_filename - FILE bio
8*e71b7053SJung-uk Kim
9*e71b7053SJung-uk Kim=head1 SYNOPSIS
10*e71b7053SJung-uk Kim
11*e71b7053SJung-uk Kim #include <openssl/bio.h>
12*e71b7053SJung-uk Kim
13*e71b7053SJung-uk Kim const BIO_METHOD *BIO_s_file(void);
14*e71b7053SJung-uk Kim BIO *BIO_new_file(const char *filename, const char *mode);
15*e71b7053SJung-uk Kim BIO *BIO_new_fp(FILE *stream, int flags);
16*e71b7053SJung-uk Kim
17*e71b7053SJung-uk Kim BIO_set_fp(BIO *b, FILE *fp, int flags);
18*e71b7053SJung-uk Kim BIO_get_fp(BIO *b, FILE **fpp);
19*e71b7053SJung-uk Kim
20*e71b7053SJung-uk Kim int BIO_read_filename(BIO *b, char *name)
21*e71b7053SJung-uk Kim int BIO_write_filename(BIO *b, char *name)
22*e71b7053SJung-uk Kim int BIO_append_filename(BIO *b, char *name)
23*e71b7053SJung-uk Kim int BIO_rw_filename(BIO *b, char *name)
24*e71b7053SJung-uk Kim
25*e71b7053SJung-uk Kim=head1 DESCRIPTION
26*e71b7053SJung-uk Kim
27*e71b7053SJung-uk KimBIO_s_file() returns the BIO file method. As its name implies it
28*e71b7053SJung-uk Kimis a wrapper round the stdio FILE structure and it is a
29*e71b7053SJung-uk Kimsource/sink BIO.
30*e71b7053SJung-uk Kim
31*e71b7053SJung-uk KimCalls to BIO_read_ex() and BIO_write_ex() read and write data to the
32*e71b7053SJung-uk Kimunderlying stream. BIO_gets() and BIO_puts() are supported on file BIOs.
33*e71b7053SJung-uk Kim
34*e71b7053SJung-uk KimBIO_flush() on a file BIO calls the fflush() function on the wrapped
35*e71b7053SJung-uk Kimstream.
36*e71b7053SJung-uk Kim
37*e71b7053SJung-uk KimBIO_reset() attempts to change the file pointer to the start of file
38*e71b7053SJung-uk Kimusing fseek(stream, 0, 0).
39*e71b7053SJung-uk Kim
40*e71b7053SJung-uk KimBIO_seek() sets the file pointer to position B<ofs> from start of file
41*e71b7053SJung-uk Kimusing fseek(stream, ofs, 0).
42*e71b7053SJung-uk Kim
43*e71b7053SJung-uk KimBIO_eof() calls feof().
44*e71b7053SJung-uk Kim
45*e71b7053SJung-uk KimSetting the BIO_CLOSE flag calls fclose() on the stream when the BIO
46*e71b7053SJung-uk Kimis freed.
47*e71b7053SJung-uk Kim
48*e71b7053SJung-uk KimBIO_new_file() creates a new file BIO with mode B<mode> the meaning
49*e71b7053SJung-uk Kimof B<mode> is the same as the stdio function fopen(). The BIO_CLOSE
50*e71b7053SJung-uk Kimflag is set on the returned BIO.
51*e71b7053SJung-uk Kim
52*e71b7053SJung-uk KimBIO_new_fp() creates a file BIO wrapping B<stream>. Flags can be:
53*e71b7053SJung-uk KimBIO_CLOSE, BIO_NOCLOSE (the close flag) BIO_FP_TEXT (sets the underlying
54*e71b7053SJung-uk Kimstream to text mode, default is binary: this only has any effect under
55*e71b7053SJung-uk KimWin32).
56*e71b7053SJung-uk Kim
57*e71b7053SJung-uk KimBIO_set_fp() sets the fp of a file BIO to B<fp>. B<flags> has the same
58*e71b7053SJung-uk Kimmeaning as in BIO_new_fp(), it is a macro.
59*e71b7053SJung-uk Kim
60*e71b7053SJung-uk KimBIO_get_fp() retrieves the fp of a file BIO, it is a macro.
61*e71b7053SJung-uk Kim
62*e71b7053SJung-uk KimBIO_seek() is a macro that sets the position pointer to B<offset> bytes
63*e71b7053SJung-uk Kimfrom the start of file.
64*e71b7053SJung-uk Kim
65*e71b7053SJung-uk KimBIO_tell() returns the value of the position pointer.
66*e71b7053SJung-uk Kim
67*e71b7053SJung-uk KimBIO_read_filename(), BIO_write_filename(), BIO_append_filename() and
68*e71b7053SJung-uk KimBIO_rw_filename() set the file BIO B<b> to use file B<name> for
69*e71b7053SJung-uk Kimreading, writing, append or read write respectively.
70*e71b7053SJung-uk Kim
71*e71b7053SJung-uk Kim=head1 NOTES
72*e71b7053SJung-uk Kim
73*e71b7053SJung-uk KimWhen wrapping stdout, stdin or stderr the underlying stream should not
74*e71b7053SJung-uk Kimnormally be closed so the BIO_NOCLOSE flag should be set.
75*e71b7053SJung-uk Kim
76*e71b7053SJung-uk KimBecause the file BIO calls the underlying stdio functions any quirks
77*e71b7053SJung-uk Kimin stdio behaviour will be mirrored by the corresponding BIO.
78*e71b7053SJung-uk Kim
79*e71b7053SJung-uk KimOn Windows BIO_new_files reserves for the filename argument to be
80*e71b7053SJung-uk KimUTF-8 encoded. In other words if you have to make it work in multi-
81*e71b7053SJung-uk Kimlingual environment, encode file names in UTF-8.
82*e71b7053SJung-uk Kim
83*e71b7053SJung-uk Kim=head1 EXAMPLES
84*e71b7053SJung-uk Kim
85*e71b7053SJung-uk KimFile BIO "hello world":
86*e71b7053SJung-uk Kim
87*e71b7053SJung-uk Kim BIO *bio_out;
88*e71b7053SJung-uk Kim
89*e71b7053SJung-uk Kim bio_out = BIO_new_fp(stdout, BIO_NOCLOSE);
90*e71b7053SJung-uk Kim BIO_printf(bio_out, "Hello World\n");
91*e71b7053SJung-uk Kim
92*e71b7053SJung-uk KimAlternative technique:
93*e71b7053SJung-uk Kim
94*e71b7053SJung-uk Kim BIO *bio_out;
95*e71b7053SJung-uk Kim
96*e71b7053SJung-uk Kim bio_out = BIO_new(BIO_s_file());
97*e71b7053SJung-uk Kim if (bio_out == NULL)
98*e71b7053SJung-uk Kim     /* Error */
99*e71b7053SJung-uk Kim if (!BIO_set_fp(bio_out, stdout, BIO_NOCLOSE))
100*e71b7053SJung-uk Kim     /* Error */
101*e71b7053SJung-uk Kim BIO_printf(bio_out, "Hello World\n");
102*e71b7053SJung-uk Kim
103*e71b7053SJung-uk KimWrite to a file:
104*e71b7053SJung-uk Kim
105*e71b7053SJung-uk Kim BIO *out;
106*e71b7053SJung-uk Kim
107*e71b7053SJung-uk Kim out = BIO_new_file("filename.txt", "w");
108*e71b7053SJung-uk Kim if (!out)
109*e71b7053SJung-uk Kim     /* Error */
110*e71b7053SJung-uk Kim BIO_printf(out, "Hello World\n");
111*e71b7053SJung-uk Kim BIO_free(out);
112*e71b7053SJung-uk Kim
113*e71b7053SJung-uk KimAlternative technique:
114*e71b7053SJung-uk Kim
115*e71b7053SJung-uk Kim BIO *out;
116*e71b7053SJung-uk Kim
117*e71b7053SJung-uk Kim out = BIO_new(BIO_s_file());
118*e71b7053SJung-uk Kim if (out == NULL)
119*e71b7053SJung-uk Kim     /* Error */
120*e71b7053SJung-uk Kim if (!BIO_write_filename(out, "filename.txt"))
121*e71b7053SJung-uk Kim     /* Error */
122*e71b7053SJung-uk Kim BIO_printf(out, "Hello World\n");
123*e71b7053SJung-uk Kim BIO_free(out);
124*e71b7053SJung-uk Kim
125*e71b7053SJung-uk Kim=head1 RETURN VALUES
126*e71b7053SJung-uk Kim
127*e71b7053SJung-uk KimBIO_s_file() returns the file BIO method.
128*e71b7053SJung-uk Kim
129*e71b7053SJung-uk KimBIO_new_file() and BIO_new_fp() return a file BIO or NULL if an error
130*e71b7053SJung-uk Kimoccurred.
131*e71b7053SJung-uk Kim
132*e71b7053SJung-uk KimBIO_set_fp() and BIO_get_fp() return 1 for success or 0 for failure
133*e71b7053SJung-uk Kim(although the current implementation never return 0).
134*e71b7053SJung-uk Kim
135*e71b7053SJung-uk KimBIO_seek() returns the same value as the underlying fseek() function:
136*e71b7053SJung-uk Kim0 for success or -1 for failure.
137*e71b7053SJung-uk Kim
138*e71b7053SJung-uk KimBIO_tell() returns the current file position.
139*e71b7053SJung-uk Kim
140*e71b7053SJung-uk KimBIO_read_filename(), BIO_write_filename(), BIO_append_filename() and
141*e71b7053SJung-uk KimBIO_rw_filename() return 1 for success or 0 for failure.
142*e71b7053SJung-uk Kim
143*e71b7053SJung-uk Kim=head1 BUGS
144*e71b7053SJung-uk Kim
145*e71b7053SJung-uk KimBIO_reset() and BIO_seek() are implemented using fseek() on the underlying
146*e71b7053SJung-uk Kimstream. The return value for fseek() is 0 for success or -1 if an error
147*e71b7053SJung-uk Kimoccurred this differs from other types of BIO which will typically return
148*e71b7053SJung-uk Kim1 for success and a non positive value if an error occurred.
149*e71b7053SJung-uk Kim
150*e71b7053SJung-uk Kim=head1 SEE ALSO
151*e71b7053SJung-uk Kim
152*e71b7053SJung-uk KimL<BIO_seek(3)>, L<BIO_tell(3)>,
153*e71b7053SJung-uk KimL<BIO_reset(3)>, L<BIO_flush(3)>,
154*e71b7053SJung-uk KimL<BIO_read_ex(3)>,
155*e71b7053SJung-uk KimL<BIO_write_ex(3)>, L<BIO_puts(3)>,
156*e71b7053SJung-uk KimL<BIO_gets(3)>, L<BIO_printf(3)>,
157*e71b7053SJung-uk KimL<BIO_set_close(3)>, L<BIO_get_close(3)>
158*e71b7053SJung-uk Kim
159*e71b7053SJung-uk Kim=head1 COPYRIGHT
160*e71b7053SJung-uk Kim
161*e71b7053SJung-uk KimCopyright 2000-2018 The OpenSSL Project Authors. All Rights Reserved.
162*e71b7053SJung-uk Kim
163*e71b7053SJung-uk KimLicensed under the OpenSSL license (the "License").  You may not use
164*e71b7053SJung-uk Kimthis file except in compliance with the License.  You can obtain a copy
165*e71b7053SJung-uk Kimin the file LICENSE in the source distribution or at
166*e71b7053SJung-uk KimL<https://www.openssl.org/source/license.html>.
167*e71b7053SJung-uk Kim
168*e71b7053SJung-uk Kim=cut
169