1 /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
4 * This package is an SSL implementation written
5 * by Eric Young (eay@cryptsoft.com).
6 * The implementation was written so as to conform with Netscapes SSL.
8 * This library is free for commercial and non-commercial use as long as
9 * the following conditions are aheared to. The following conditions
10 * apply to all code found in this distribution, be it the RC4, RSA,
11 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
12 * included with this distribution is covered by the same copyright terms
13 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15 * Copyright remains Eric Young's, and as such any Copyright notices in
16 * the code are not to be removed.
17 * If this package is used in a product, Eric Young should be given attribution
18 * as the author of the parts of the library used.
19 * This can be in the form of a textual message at program startup or
20 * in documentation (online or textual) provided with the package.
22 * Redistribution and use in source and binary forms, with or without
23 * modification, are permitted provided that the following conditions
25 * 1. Redistributions of source code must retain the copyright
26 * notice, this list of conditions and the following disclaimer.
27 * 2. Redistributions in binary form must reproduce the above copyright
28 * notice, this list of conditions and the following disclaimer in the
29 * documentation and/or other materials provided with the distribution.
30 * 3. All advertising materials mentioning features or use of this software
31 * must display the following acknowledgement:
32 * "This product includes cryptographic software written by
33 * Eric Young (eay@cryptsoft.com)"
34 * The word 'cryptographic' can be left out if the rouines from the library
35 * being used are not cryptographic related :-).
36 * 4. If you include any Windows specific code (or a derivative thereof) from
37 * the apps directory (application code) you must include an acknowledgement:
38 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
41 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
42 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
43 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
44 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
45 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
46 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
48 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
49 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
52 * The licence and distribution terms for any publically available version or
53 * derivative of this code cannot be changed. i.e. this code cannot simply be
54 * copied and put under another distribution licence
55 * [including the GNU Public Licence.] */
57 #if defined(__linux) || defined(__sun) || defined(__hpux)
58 // Following definition aliases fopen to fopen64 on above mentioned
59 // platforms. This makes it possible to open and sequentially access
60 // files larger than 2GB from 32-bit application. It does not allow to
61 // traverse them beyond 2GB with fseek/ftell, but on the other hand *no*
62 // 32-bit platform permits that, not with fseek/ftell. Not to mention
63 // that breaking 2GB limit for seeking would require surgery to *our*
64 // API. But sequential access suffices for practical cases when you
65 // can run into large files, such as fingerprinting, so we can let API
66 // alone. For reference, the list of 32-bit platforms which allow for
67 // sequential access of large files without extra "magic" comprise *BSD,
69 #ifndef _FILE_OFFSET_BITS
70 #define _FILE_OFFSET_BITS 64
74 #include <openssl/bio.h>
80 #include <openssl/buf.h>
81 #include <openssl/err.h>
82 #include <openssl/mem.h>
84 #include "../internal.h"
87 #define BIO_FP_READ 0x02
88 #define BIO_FP_WRITE 0x04
89 #define BIO_FP_APPEND 0x08
91 BIO *BIO_new_file(const char *filename, const char *mode) {
95 file = fopen(filename, mode);
97 OPENSSL_PUT_SYSTEM_ERROR();
99 ERR_add_error_data(5, "fopen('", filename, "','", mode, "')");
100 if (errno == ENOENT) {
101 OPENSSL_PUT_ERROR(BIO, BIO_R_NO_SUCH_FILE);
103 OPENSSL_PUT_ERROR(BIO, BIO_R_SYS_LIB);
108 ret = BIO_new(BIO_s_file());
114 BIO_set_fp(ret, file, BIO_CLOSE);
118 BIO *BIO_new_fp(FILE *stream, int close_flag) {
119 BIO *ret = BIO_new(BIO_s_file());
125 BIO_set_fp(ret, stream, close_flag);
129 static int file_new(BIO *bio) { return 1; }
131 static int file_free(BIO *bio) {
136 if (!bio->shutdown) {
140 if (bio->init && bio->ptr != NULL) {
149 static int file_read(BIO *b, char *out, int outl) {
154 size_t ret = fread(out, 1, outl, (FILE *)b->ptr);
155 if (ret == 0 && ferror((FILE *)b->ptr)) {
156 OPENSSL_PUT_SYSTEM_ERROR();
157 OPENSSL_PUT_ERROR(BIO, ERR_R_SYS_LIB);
161 // fread reads at most |outl| bytes, so |ret| fits in an int.
165 static int file_write(BIO *b, const char *in, int inl) {
172 ret = fwrite(in, inl, 1, (FILE *)b->ptr);
179 static long file_ctrl(BIO *b, int cmd, long num, void *ptr) {
181 FILE *fp = (FILE *)b->ptr;
189 case BIO_C_FILE_SEEK:
190 ret = (long)fseek(fp, num, 0);
193 ret = (long)feof(fp);
195 case BIO_C_FILE_TELL:
199 case BIO_C_SET_FILE_PTR:
201 b->shutdown = (int)num & BIO_CLOSE;
205 case BIO_C_SET_FILENAME:
207 b->shutdown = (int)num & BIO_CLOSE;
208 if (num & BIO_FP_APPEND) {
209 if (num & BIO_FP_READ) {
210 BUF_strlcpy(p, "a+", sizeof(p));
212 BUF_strlcpy(p, "a", sizeof(p));
214 } else if ((num & BIO_FP_READ) && (num & BIO_FP_WRITE)) {
215 BUF_strlcpy(p, "r+", sizeof(p));
216 } else if (num & BIO_FP_WRITE) {
217 BUF_strlcpy(p, "w", sizeof(p));
218 } else if (num & BIO_FP_READ) {
219 BUF_strlcpy(p, "r", sizeof(p));
221 OPENSSL_PUT_ERROR(BIO, BIO_R_BAD_FOPEN_MODE);
227 OPENSSL_PUT_SYSTEM_ERROR();
228 ERR_add_error_data(5, "fopen('", ptr, "','", p, "')");
229 OPENSSL_PUT_ERROR(BIO, ERR_R_SYS_LIB);
236 case BIO_C_GET_FILE_PTR:
237 // the ptr parameter is actually a FILE ** in this case.
240 *fpp = (FILE *)b->ptr;
243 case BIO_CTRL_GET_CLOSE:
244 ret = (long)b->shutdown;
246 case BIO_CTRL_SET_CLOSE:
247 b->shutdown = (int)num;
250 ret = 0 == fflush((FILE *)b->ptr);
252 case BIO_CTRL_WPENDING:
253 case BIO_CTRL_PENDING:
261 static int file_gets(BIO *bp, char *buf, int size) {
268 if (!fgets(buf, size, (FILE *)bp->ptr)) {
278 static const BIO_METHOD methods_filep = {
279 BIO_TYPE_FILE, "FILE pointer",
280 file_write, file_read,
281 NULL /* puts */, file_gets,
283 file_free, NULL /* callback_ctrl */,
286 const BIO_METHOD *BIO_s_file(void) { return &methods_filep; }
289 int BIO_get_fp(BIO *bio, FILE **out_file) {
290 return BIO_ctrl(bio, BIO_C_GET_FILE_PTR, 0, (char*) out_file);
293 int BIO_set_fp(BIO *bio, FILE *file, int close_flag) {
294 return BIO_ctrl(bio, BIO_C_SET_FILE_PTR, close_flag, (char *) file);
297 int BIO_read_filename(BIO *bio, const char *filename) {
298 return BIO_ctrl(bio, BIO_C_SET_FILENAME, BIO_CLOSE | BIO_FP_READ,
302 int BIO_write_filename(BIO *bio, const char *filename) {
303 return BIO_ctrl(bio, BIO_C_SET_FILENAME, BIO_CLOSE | BIO_FP_WRITE,
307 int BIO_append_filename(BIO *bio, const char *filename) {
308 return BIO_ctrl(bio, BIO_C_SET_FILENAME, BIO_CLOSE | BIO_FP_APPEND,
312 int BIO_rw_filename(BIO *bio, const char *filename) {
313 return BIO_ctrl(bio, BIO_C_SET_FILENAME,
314 BIO_CLOSE | BIO_FP_READ | BIO_FP_WRITE, (char *)filename);