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