2 * Copyright 1995-2020 The OpenSSL Project Authors. All Rights Reserved.
4 * Licensed under the Apache License 2.0 (the "License"). You may not use
5 * this file except in compliance with the License. You can obtain a copy
6 * in the file LICENSE in the source distribution or at
7 * https://www.openssl.org/source/license.html
10 #if defined(__linux) || defined(__sun) || defined(__hpux)
12 * Following definition aliases fopen to fopen64 on above mentioned
13 * platforms. This makes it possible to open and sequentially access files
14 * larger than 2GB from 32-bit application. It does not allow to traverse
15 * them beyond 2GB with fseek/ftell, but on the other hand *no* 32-bit
16 * platform permits that, not with fseek/ftell. Not to mention that breaking
17 * 2GB limit for seeking would require surgery to *our* API. But sequential
18 * access suffices for practical cases when you can run into large files,
19 * such as fingerprinting, so we can let API alone. For reference, the list
20 * of 32-bit platforms which allow for sequential access of large files
21 * without extra "magic" comprise *BSD, Darwin, IRIX...
23 # ifndef _FILE_OFFSET_BITS
24 # define _FILE_OFFSET_BITS 64
30 #include "bio_local.h"
31 #include <openssl/err.h>
33 #if !defined(OPENSSL_NO_STDIO)
35 static int file_write(BIO *h, const char *buf, int num);
36 static int file_read(BIO *h, char *buf, int size);
37 static int file_puts(BIO *h, const char *str);
38 static int file_gets(BIO *h, char *str, int size);
39 static long file_ctrl(BIO *h, int cmd, long arg1, void *arg2);
40 static int file_new(BIO *h);
41 static int file_free(BIO *data);
42 static const BIO_METHOD methods_filep = {
45 /* TODO: Convert to new style write function */
48 /* TODO: Convert to new style read function */
56 NULL, /* file_callback_ctrl */
59 BIO *BIO_new_file(const char *filename, const char *mode)
62 FILE *file = openssl_fopen(filename, mode);
63 int fp_flags = BIO_CLOSE;
65 if (strchr(mode, 'b') == NULL)
66 fp_flags |= BIO_FP_TEXT;
69 ERR_raise_data(ERR_LIB_SYS, get_last_sys_error(),
70 "calling fopen(%s, %s)",
77 ERR_raise(ERR_LIB_BIO, BIO_R_NO_SUCH_FILE);
79 ERR_raise(ERR_LIB_BIO, ERR_R_SYS_LIB);
82 if ((ret = BIO_new(BIO_s_file())) == NULL) {
87 /* we did fopen -> we disengage UPLINK */
88 BIO_clear_flags(ret, BIO_FLAGS_UPLINK_INTERNAL);
89 BIO_set_fp(ret, file, fp_flags);
93 BIO *BIO_new_fp(FILE *stream, int close_flag)
97 if ((ret = BIO_new(BIO_s_file())) == NULL)
100 /* redundant flag, left for documentation purposes */
101 BIO_set_flags(ret, BIO_FLAGS_UPLINK_INTERNAL);
102 BIO_set_fp(ret, stream, close_flag);
106 const BIO_METHOD *BIO_s_file(void)
108 return &methods_filep;
111 static int file_new(BIO *bi)
116 bi->flags = BIO_FLAGS_UPLINK_INTERNAL; /* default to UPLINK */
120 static int file_free(BIO *a)
125 if ((a->init) && (a->ptr != NULL)) {
126 if (a->flags & BIO_FLAGS_UPLINK_INTERNAL)
131 a->flags = BIO_FLAGS_UPLINK_INTERNAL;
138 static int file_read(BIO *b, char *out, int outl)
142 if (b->init && (out != NULL)) {
143 if (b->flags & BIO_FLAGS_UPLINK_INTERNAL)
144 ret = UP_fread(out, 1, (int)outl, b->ptr);
146 ret = fread(out, 1, (int)outl, (FILE *)b->ptr);
148 && (b->flags & BIO_FLAGS_UPLINK_INTERNAL
149 ? UP_ferror((FILE *)b->ptr) : ferror((FILE *)b->ptr))) {
150 ERR_raise_data(ERR_LIB_SYS, get_last_sys_error(),
152 ERR_raise(ERR_LIB_BIO, ERR_R_SYS_LIB);
159 static int file_write(BIO *b, const char *in, int inl)
163 if (b->init && (in != NULL)) {
164 if (b->flags & BIO_FLAGS_UPLINK_INTERNAL)
165 ret = UP_fwrite(in, (int)inl, 1, b->ptr);
167 ret = fwrite(in, (int)inl, 1, (FILE *)b->ptr);
170 /* ret=fwrite(in,1,(int)inl,(FILE *)b->ptr); */
172 * according to Tim Hudson <tjh@openssl.org>, the commented out
173 * version above can cause 'inl' write calls under some stupid stdio
174 * implementations (VMS)
180 static long file_ctrl(BIO *b, int cmd, long num, void *ptr)
183 FILE *fp = (FILE *)b->ptr;
189 case BIO_C_FILE_SEEK:
191 if (b->flags & BIO_FLAGS_UPLINK_INTERNAL)
192 ret = (long)UP_fseek(b->ptr, num, 0);
194 ret = (long)fseek(fp, num, 0);
197 if (b->flags & BIO_FLAGS_UPLINK_INTERNAL)
198 ret = (long)UP_feof(fp);
200 ret = (long)feof(fp);
202 case BIO_C_FILE_TELL:
204 if (b->flags & BIO_FLAGS_UPLINK_INTERNAL)
205 ret = UP_ftell(b->ptr);
209 case BIO_C_SET_FILE_PTR:
211 b->shutdown = (int)num & BIO_CLOSE;
214 # if BIO_FLAGS_UPLINK_INTERNAL!=0
215 # if defined(__MINGW32__) && defined(__MSVCRT__) && !defined(_IOB_ENTRIES)
216 # define _IOB_ENTRIES 20
218 /* Safety net to catch purely internal BIO_set_fp calls */
219 # if defined(_MSC_VER) && _MSC_VER>=1900
220 if (ptr == stdin || ptr == stdout || ptr == stderr)
221 BIO_clear_flags(b, BIO_FLAGS_UPLINK_INTERNAL);
222 # elif defined(_IOB_ENTRIES)
223 if ((size_t)ptr >= (size_t)stdin &&
224 (size_t)ptr < (size_t)(stdin + _IOB_ENTRIES))
225 BIO_clear_flags(b, BIO_FLAGS_UPLINK_INTERNAL);
229 if (b->flags & BIO_FLAGS_UPLINK_INTERNAL)
230 UP_fsetmod(b->ptr, (char)((num & BIO_FP_TEXT) ? 't' : 'b'));
234 # if defined(OPENSSL_SYS_WINDOWS)
235 int fd = _fileno((FILE *)ptr);
236 if (num & BIO_FP_TEXT)
237 _setmode(fd, _O_TEXT);
239 _setmode(fd, _O_BINARY);
241 * Reports show that ftell() isn't trustable in text mode.
242 * This has been confirmed as a bug in the Universal C RTL, see
243 * https://developercommunity.visualstudio.com/content/problem/425878/fseek-ftell-fail-in-text-mode-for-unix-style-text.html
244 * The suggested work-around from Microsoft engineering is to
245 * turn off buffering until the bug is resolved.
247 if ((num & BIO_FP_TEXT) != 0)
248 setvbuf((FILE *)ptr, NULL, _IONBF, 0);
249 # elif defined(OPENSSL_SYS_MSDOS)
250 int fd = fileno((FILE *)ptr);
251 /* Set correct text/binary mode */
252 if (num & BIO_FP_TEXT)
253 _setmode(fd, _O_TEXT);
254 /* Dangerous to set stdin/stdout to raw (unless redirected) */
256 if (fd == STDIN_FILENO || fd == STDOUT_FILENO) {
258 _setmode(fd, _O_BINARY);
260 _setmode(fd, _O_BINARY);
262 # elif defined(OPENSSL_SYS_WIN32_CYGWIN)
263 int fd = fileno((FILE *)ptr);
264 if (!(num & BIO_FP_TEXT))
265 setmode(fd, O_BINARY);
269 case BIO_C_SET_FILENAME:
271 b->shutdown = (int)num & BIO_CLOSE;
272 if (num & BIO_FP_APPEND) {
273 if (num & BIO_FP_READ)
274 OPENSSL_strlcpy(p, "a+", sizeof(p));
276 OPENSSL_strlcpy(p, "a", sizeof(p));
277 } else if ((num & BIO_FP_READ) && (num & BIO_FP_WRITE))
278 OPENSSL_strlcpy(p, "r+", sizeof(p));
279 else if (num & BIO_FP_WRITE)
280 OPENSSL_strlcpy(p, "w", sizeof(p));
281 else if (num & BIO_FP_READ)
282 OPENSSL_strlcpy(p, "r", sizeof(p));
284 ERR_raise(ERR_LIB_BIO, BIO_R_BAD_FOPEN_MODE);
288 # if defined(OPENSSL_SYS_MSDOS) || defined(OPENSSL_SYS_WINDOWS)
289 if (!(num & BIO_FP_TEXT))
290 OPENSSL_strlcat(p, "b", sizeof(p));
292 OPENSSL_strlcat(p, "t", sizeof(p));
293 # elif defined(OPENSSL_SYS_WIN32_CYGWIN)
294 if (!(num & BIO_FP_TEXT))
295 OPENSSL_strlcat(p, "b", sizeof(p));
297 fp = openssl_fopen(ptr, p);
299 ERR_raise_data(ERR_LIB_SYS, get_last_sys_error(),
300 "calling fopen(%s, %s)",
302 ERR_raise(ERR_LIB_BIO, ERR_R_SYS_LIB);
308 /* we did fopen -> we disengage UPLINK */
309 BIO_clear_flags(b, BIO_FLAGS_UPLINK_INTERNAL);
311 case BIO_C_GET_FILE_PTR:
312 /* the ptr parameter is actually a FILE ** in this case. */
315 *fpp = (FILE *)b->ptr;
318 case BIO_CTRL_GET_CLOSE:
319 ret = (long)b->shutdown;
321 case BIO_CTRL_SET_CLOSE:
322 b->shutdown = (int)num;
325 st = b->flags & BIO_FLAGS_UPLINK_INTERNAL
326 ? UP_fflush(b->ptr) : fflush((FILE *)b->ptr);
328 ERR_raise_data(ERR_LIB_SYS, get_last_sys_error(),
330 ERR_raise(ERR_LIB_BIO, ERR_R_SYS_LIB);
338 case BIO_CTRL_WPENDING:
339 case BIO_CTRL_PENDING:
349 static int file_gets(BIO *bp, char *buf, int size)
354 if (bp->flags & BIO_FLAGS_UPLINK_INTERNAL) {
355 if (!UP_fgets(buf, size, bp->ptr))
358 if (!fgets(buf, size, (FILE *)bp->ptr))
367 static int file_puts(BIO *bp, const char *str)
372 ret = file_write(bp, str, n);
378 static int file_write(BIO *b, const char *in, int inl)
382 static int file_read(BIO *b, char *out, int outl)
386 static int file_puts(BIO *bp, const char *str)
390 static int file_gets(BIO *bp, char *buf, int size)
394 static long file_ctrl(BIO *b, int cmd, long num, void *ptr)
398 static int file_new(BIO *bi)
402 static int file_free(BIO *a)
407 static const BIO_METHOD methods_filep = {
410 /* TODO: Convert to new style write function */
413 /* TODO: Convert to new style read function */
421 NULL, /* file_callback_ctrl */
424 const BIO_METHOD *BIO_s_file(void)
426 return &methods_filep;
429 BIO *BIO_new_file(const char *filename, const char *mode)
434 #endif /* OPENSSL_NO_STDIO */