summaryrefslogtreecommitdiff
path: root/lib/libcrypto/man/BIO_s_file.3
blob: 14950cad13c298fa0329c3f843369ebe75604496 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
.\" $OpenBSD: BIO_s_file.3,v 1.17 2023/11/16 20:19:23 schwarze Exp $
.\" full merge up to: OpenSSL 99d63d46 Oct 26 13:56:48 2016 -0400
.\" selective merge up to: OpenSSL 1212818e Sep 11 13:22:14 2018 +0100
.\"
.\" This file is a derived work.
.\" The changes are covered by the following Copyright and license:
.\"
.\" Copyright (c) 2023 Ingo Schwarze <schwarze@openbsd.org>
.\"
.\" Permission to use, copy, modify, and distribute this software for any
.\" purpose with or without fee is hereby granted, provided that the above
.\" copyright notice and this permission notice appear in all copies.
.\"
.\" THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
.\" WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
.\" MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
.\" ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
.\" WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
.\" ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
.\" OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
.\"
.\" The original file was written by Dr. Stephen Henson <steve@openssl.org>.
.\" Copyright (c) 2000, 2010 The OpenSSL Project.  All rights reserved.
.\"
.\" Redistribution and use in source and binary forms, with or without
.\" modification, are permitted provided that the following conditions
.\" are met:
.\"
.\" 1. Redistributions of source code must retain the above copyright
.\"    notice, this list of conditions and the following disclaimer.
.\"
.\" 2. Redistributions in binary form must reproduce the above copyright
.\"    notice, this list of conditions and the following disclaimer in
.\"    the documentation and/or other materials provided with the
.\"    distribution.
.\"
.\" 3. All advertising materials mentioning features or use of this
.\"    software must display the following acknowledgment:
.\"    "This product includes software developed by the OpenSSL Project
.\"    for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
.\"
.\" 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
.\"    endorse or promote products derived from this software without
.\"    prior written permission. For written permission, please contact
.\"    openssl-core@openssl.org.
.\"
.\" 5. Products derived from this software may not be called "OpenSSL"
.\"    nor may "OpenSSL" appear in their names without prior written
.\"    permission of the OpenSSL Project.
.\"
.\" 6. Redistributions of any form whatsoever must retain the following
.\"    acknowledgment:
.\"    "This product includes software developed by the OpenSSL Project
.\"    for use in the OpenSSL Toolkit (http://www.openssl.org/)"
.\"
.\" THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
.\" EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
.\" PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OpenSSL PROJECT OR
.\" ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
.\" SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
.\" NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
.\" LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
.\" STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
.\" ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
.\" OF THE POSSIBILITY OF SUCH DAMAGE.
.\"
.Dd $Mdocdate: November 16 2023 $
.Dt BIO_S_FILE 3
.Os
.Sh NAME
.Nm BIO_s_file ,
.Nm BIO_new_file ,
.Nm BIO_new_fp ,
.Nm BIO_set_fp ,
.Nm BIO_get_fp ,
.Nm BIO_read_filename ,
.Nm BIO_write_filename ,
.Nm BIO_append_filename ,
.Nm BIO_rw_filename
.\" Nm BIO_CTRL_SET_FILENAME is unused and intentionally undocumented.
.Nd FILE BIO
.Sh SYNOPSIS
.In openssl/bio.h
.Ft const BIO_METHOD *
.Fo BIO_s_file
.Fa void
.Fc
.Ft BIO *
.Fo BIO_new_file
.Fa "const char *filename"
.Fa "const char *mode"
.Fc
.Ft BIO *
.Fo BIO_new_fp
.Fa "FILE *stream"
.Fa "int flags"
.Fc
.Ft long
.Fo BIO_set_fp
.Fa "BIO *b"
.Fa "FILE *fp"
.Fa "int flags"
.Fc
.Ft long
.Fo BIO_get_fp
.Fa "BIO *b"
.Fa "FILE **fpp"
.Fc
.Ft long
.Fo BIO_read_filename
.Fa "BIO *b"
.Fa "char *name"
.Fc
.Ft long
.Fo BIO_write_filename
.Fa "BIO *b"
.Fa "char *name"
.Fc
.Ft long
.Fo BIO_append_filename
.Fa "BIO *b"
.Fa "char *name"
.Fc
.Ft long
.Fo BIO_rw_filename
.Fa "BIO *b"
.Fa "char *name"
.Fc
.Sh DESCRIPTION
.Fn BIO_s_file
returns the BIO file method.
As its name implies, it is a wrapper around the stdio
.Vt FILE
structure and it is a source/sink BIO.
.Pp
Calls to
.Xr BIO_read 3
and
.Xr BIO_write 3
read and write data to the underlying stream.
.Xr BIO_gets 3
and
.Xr BIO_puts 3
are supported on file BIOs.
.Pp
.Xr BIO_flush 3
on a file BIO calls the
.Xr fflush 3
function on the wrapped stream.
.Pp
.Xr BIO_reset 3
attempts to change the file pointer to the start of file using
.Fn fseek stream 0 0 .
.Pp
.Xr BIO_seek 3
sets the file pointer to position
.Fa ofs
from the start of the file using
.Fn fseek stream ofs 0 .
.Pp
.Xr BIO_eof 3
calls
.Xr feof 3 .
.Pp
Setting the
.Dv BIO_CLOSE
flag calls
.Xr fclose 3
on the stream when the BIO is freed.
.Pp
.Fn BIO_new_file
creates a new file BIO with mode
.Fa mode .
The meaning of
.Fa mode
is the same as for the stdio function
.Xr fopen 3 .
The
.Dv BIO_CLOSE
flag is set on the returned BIO.
.Pp
.Fn BIO_new_fp
creates a file BIO wrapping
.Fa stream .
Flags can be:
.Dv BIO_CLOSE , BIO_NOCLOSE Pq the close flag ,
.Dv BIO_FP_TEXT
(sets the underlying stream to text mode, default is binary:
this only has any effect under Win32).
.Pp
.Fn BIO_set_fp
sets the file pointer of a file BIO to
.Fa fp .
.Fa flags
has the same meaning as in
.Fn BIO_new_fp .
.Pp
.Fn BIO_get_fp
retrieves the file pointer of a file BIO.
.Pp
.Xr BIO_seek 3
sets the position pointer to
.Fa offset
bytes from the start of file.
.Pp
.Xr BIO_tell 3
returns the value of the position pointer.
.Pp
.Fn BIO_read_filename ,
.Fn BIO_write_filename ,
.Fn BIO_append_filename ,
and
.Fn BIO_rw_filename
set the file BIO
.Fa b
to use file
.Fa name
for reading, writing, append or read write respectively.
.Pp
When wrapping stdout, stdin, or stderr, the underlying stream
should not normally be closed, so the
.Dv BIO_NOCLOSE
flag should be set.
.Pp
Because the file BIO calls the underlying stdio functions, any quirks
in stdio behaviour will be mirrored by the corresponding BIO.
.Pp
On Windows,
.Fn BIO_new_files
reserves for the filename argument to be UTF-8 encoded.
In other words, if you have to make it work in a multi-lingual
environment, encode file names in UTF-8.
.Pp
The following
.Xr BIO_ctrl 3
.Fa cmd
constants correspond to macros:
.Bl -column BIO_C_GET_FILE_PTR "corresponding macro" -offset 3n
.It Fa cmd No constant    Ta corresponding macro
.It Dv BIO_C_FILE_SEEK    Ta Xr BIO_seek 3
.It Dv BIO_C_FILE_TELL    Ta Xr BIO_tell 3
.It Dv BIO_C_GET_FILE_PTR Ta Fn BIO_get_fp
.It Dv BIO_C_SET_FILE_PTR Ta Fn BIO_set_fp
.It Dv BIO_C_SET_FILENAME Ta various, see below
.It Dv BIO_CTRL_EOF       Ta Xr BIO_eof 3
.It Dv BIO_CTRL_FLUSH     Ta Xr BIO_flush 3
.It Dv BIO_CTRL_GET_CLOSE Ta Xr BIO_get_close 3
.It Dv BIO_CTRL_RESET     Ta Xr BIO_reset 3
.It Dv BIO_CTRL_SET_CLOSE Ta Xr BIO_set_close 3
.El
.Pp
The meaning of
.Dv BIO_C_SET_FILENAME
depends on the flags passed in the
.Xr BIO_ctrl 3
.Fa larg
argument:
.Bl -column "BIO_CLOSE | BIO_FP_READ | BIO_FP_WRITE" "BIO_append_filename()"\
 -offset 3n
.It Fa larg No argument                        Ta corresponding macro
.It Dv BIO_CLOSE | BIO_FP_READ                 Ta Fn BIO_read_filename
.It Dv BIO_CLOSE | BIO_FP_WRITE                Ta Fn BIO_write_filename
.It Dv BIO_CLOSE | BIO_FP_APPEND               Ta Fn BIO_append_filename
.It Dv BIO_CLOSE | BIO_FP_READ  | BIO_FP_WRITE Ta Fn BIO_rw_filename
.El
.Sh RETURN VALUES
.Fn BIO_s_file
returns the file BIO method.
.Pp
.Fn BIO_new_file
and
.Fn BIO_new_fp
return a file BIO or
.Dv NULL
if an error occurred.
.Pp
When called on a file BIO object,
.Xr BIO_method_type 3
returns the constant
.Dv BIO_TYPE_FILE
and
.Xr BIO_method_name 3
returns a pointer to the static string
.Qq FILE pointer .
.Pp
.Fn BIO_set_fp
and
.Fn BIO_get_fp
return 1 for success or 0 for failure (although the current
implementation never returns 0).
.Pp
.Xr BIO_seek 3
returns the same value as the underlying
.Xr fseek 3
function: 0 for success or -1 for failure.
.Pp
.Xr BIO_tell 3
returns the current file position.
.Pp
.Fn BIO_read_filename ,
.Fn BIO_write_filename ,
.Fn BIO_append_filename ,
and
.Fn BIO_rw_filename
return 1 for success or 0 for failure.
.Sh EXAMPLES
File BIO "hello world":
.Bd -literal -offset indent
BIO *bio_out;
bio_out = BIO_new_fp(stdout, BIO_NOCLOSE);
BIO_printf(bio_out, "Hello World\en");
.Ed
.Pp
Alternative technique:
.Bd -literal -offset indent
BIO *bio_out;
bio_out = BIO_new(BIO_s_file());
if(bio_out == NULL) /* Error ... */
if(!BIO_set_fp(bio_out, stdout, BIO_NOCLOSE)) /* Error ... */
BIO_printf(bio_out, "Hello World\en");
.Ed
.Pp
Write to a file:
.Bd -literal -offset indent
BIO *out;
out = BIO_new_file("filename.txt", "w");
if(!out) /* Error occurred */
BIO_printf(out, "Hello World\en");
BIO_free(out);
.Ed
.Pp
Alternative technique:
.Bd -literal -offset indent
BIO *out;
out = BIO_new(BIO_s_file());
if(out == NULL) /* Error ... */
if(!BIO_write_filename(out, "filename.txt")) /* Error ... */
BIO_printf(out, "Hello World\en");
BIO_free(out);
.Ed
.Sh SEE ALSO
.Xr BIO_new 3 ,
.Xr BIO_read 3 ,
.Xr BIO_seek 3
.Sh HISTORY
.Fn BIO_s_file ,
.Fn BIO_set_fp ,
.Fn BIO_get_fp ,
.Fn BIO_read_filename ,
.Fn BIO_write_filename ,
and
.Fn BIO_append_filename
first appeared in SSLeay 0.6.0.
.Fn BIO_new_file
and
.Fn BIO_new_fp
first appeared in SSLeay 0.8.0.
All these functions have been available since
.Ox 2.4 .
.Pp
.Fn BIO_rw_filename
first appeared in SSLeay 0.9.1 and has been available since
.Ox 2.6 .
.Sh BUGS
.Xr BIO_reset 3
and
.Xr BIO_seek 3
are implemented using
.Xr fseek 3
on the underlying stream.
The return value for
.Xr fseek 3
is 0 for success or -1 if an error occurred.
This differs from other types of BIO which will typically return
1 for success and a non-positive value if an error occurred.