2 * Copyright 1995-2021 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 one 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 = {
54 NULL, /* file_callback_ctrl */
57 BIO *BIO_new_file(const char *filename, const char *mode)
60 FILE *file = openssl_fopen(filename, mode);
61 int fp_flags = BIO_CLOSE;
63 if (strchr(mode, 'b') == NULL)
64 fp_flags |= BIO_FP_TEXT;
67 ERR_raise_data(ERR_LIB_SYS, get_last_sys_error(),
68 "calling fopen(%s, %s)",
75 ERR_raise(ERR_LIB_BIO, BIO_R_NO_SUCH_FILE);
77 ERR_raise(ERR_LIB_BIO, ERR_R_SYS_LIB);
80 if ((ret = BIO_new(BIO_s_file())) == NULL) {
85 /* we did fopen -> we disengage UPLINK */
86 BIO_clear_flags(ret, BIO_FLAGS_UPLINK_INTERNAL);
87 BIO_set_fp(ret, file, fp_flags);
91 BIO *BIO_new_fp(FILE *stream, int close_flag)
95 if ((ret = BIO_new(BIO_s_file())) == NULL)
98 /* redundant flag, left for documentation purposes */
99 BIO_set_flags(ret, BIO_FLAGS_UPLINK_INTERNAL);
100 BIO_set_fp(ret, stream, close_flag);
104 const BIO_METHOD *BIO_s_file(void)
106 return &methods_filep;
109 static int file_new(BIO *bi)
114 bi->flags = BIO_FLAGS_UPLINK_INTERNAL; /* default to UPLINK */
118 static int file_free(BIO *a)
123 if ((a->init) && (a->ptr != NULL)) {
124 if (a->flags & BIO_FLAGS_UPLINK_INTERNAL)
129 a->flags = BIO_FLAGS_UPLINK_INTERNAL;
136 static int file_read(BIO *b, char *out, int outl)
140 if (b->init && (out != NULL)) {
141 if (b->flags & BIO_FLAGS_UPLINK_INTERNAL)
142 ret = UP_fread(out, 1, (int)outl, b->ptr);
144 ret = fread(out, 1, (int)outl, (FILE *)b->ptr);
146 && (b->flags & BIO_FLAGS_UPLINK_INTERNAL
147 ? UP_ferror((FILE *)b->ptr) : ferror((FILE *)b->ptr))) {
148 ERR_raise_data(ERR_LIB_SYS, get_last_sys_error(),
150 ERR_raise(ERR_LIB_BIO, ERR_R_SYS_LIB);
157 static int file_write(BIO *b, const char *in, int inl)
161 if (b->init && (in != NULL)) {
162 if (b->flags & BIO_FLAGS_UPLINK_INTERNAL)
163 ret = UP_fwrite(in, (int)inl, 1, b->ptr);
165 ret = fwrite(in, (int)inl, 1, (FILE *)b->ptr);
168 /* ret=fwrite(in,1,(int)inl,(FILE *)b->ptr); */
170 * according to Tim Hudson <tjh@openssl.org>, the commented out
171 * version above can cause 'inl' write calls under some stupid stdio
172 * implementations (VMS)
178 static long file_ctrl(BIO *b, int cmd, long num, void *ptr)
181 FILE *fp = (FILE *)b->ptr;
187 case BIO_C_FILE_SEEK:
189 if (b->flags & BIO_FLAGS_UPLINK_INTERNAL)
190 ret = (long)UP_fseek(b->ptr, num, 0);
192 ret = (long)fseek(fp, num, 0);
195 if (b->flags & BIO_FLAGS_UPLINK_INTERNAL)
196 ret = (long)UP_feof(fp);
198 ret = (long)feof(fp);
200 case BIO_C_FILE_TELL:
202 if (b->flags & BIO_FLAGS_UPLINK_INTERNAL)
203 ret = UP_ftell(b->ptr);
207 case BIO_C_SET_FILE_PTR:
209 b->shutdown = (int)num & BIO_CLOSE;
212 # if BIO_FLAGS_UPLINK_INTERNAL!=0
213 # if defined(__MINGW32__) && defined(__MSVCRT__) && !defined(_IOB_ENTRIES)
214 # define _IOB_ENTRIES 20
216 /* Safety net to catch purely internal BIO_set_fp calls */
217 # if (defined(_MSC_VER) && _MSC_VER>=1900) || defined(__BORLANDC__)
218 if (ptr == stdin || ptr == stdout || ptr == stderr)
219 BIO_clear_flags(b, BIO_FLAGS_UPLINK_INTERNAL);
220 # elif defined(_IOB_ENTRIES)
221 if ((size_t)ptr >= (size_t)stdin &&
222 (size_t)ptr < (size_t)(stdin + _IOB_ENTRIES))
223 BIO_clear_flags(b, BIO_FLAGS_UPLINK_INTERNAL);
227 if (b->flags & BIO_FLAGS_UPLINK_INTERNAL)
228 UP_fsetmod(b->ptr, (char)((num & BIO_FP_TEXT) ? 't' : 'b'));
232 # if defined(OPENSSL_SYS_WINDOWS)
233 int fd = _fileno((FILE *)ptr);
234 if (num & BIO_FP_TEXT)
235 _setmode(fd, _O_TEXT);
237 _setmode(fd, _O_BINARY);
239 * Reports show that ftell() isn't trustable in text mode.
240 * This has been confirmed as a bug in the Universal C RTL, see
241 * https://developercommunity.visualstudio.com/content/problem/425878/fseek-ftell-fail-in-text-mode-for-unix-style-text.html
242 * The suggested work-around from Microsoft engineering is to
243 * turn off buffering until the bug is resolved.
245 if ((num & BIO_FP_TEXT) != 0)
246 setvbuf((FILE *)ptr, NULL, _IONBF, 0);
247 # elif defined(OPENSSL_SYS_MSDOS)
248 int fd = fileno((FILE *)ptr);
249 /* Set correct text/binary mode */
250 if (num & BIO_FP_TEXT)
251 _setmode(fd, _O_TEXT);
252 /* Dangerous to set stdin/stdout to raw (unless redirected) */
254 if (fd == STDIN_FILENO || fd == STDOUT_FILENO) {
256 _setmode(fd, _O_BINARY);
258 _setmode(fd, _O_BINARY);
260 # elif defined(OPENSSL_SYS_WIN32_CYGWIN)
261 int fd = fileno((FILE *)ptr);
262 if (!(num & BIO_FP_TEXT))
263 setmode(fd, O_BINARY);
267 case BIO_C_SET_FILENAME:
269 b->shutdown = (int)num & BIO_CLOSE;
270 if (num & BIO_FP_APPEND) {
271 if (num & BIO_FP_READ)
272 OPENSSL_strlcpy(p, "a+", sizeof(p));
274 OPENSSL_strlcpy(p, "a", sizeof(p));
275 } else if ((num & BIO_FP_READ) && (num & BIO_FP_WRITE))
276 OPENSSL_strlcpy(p, "r+", sizeof(p));
277 else if (num & BIO_FP_WRITE)
278 OPENSSL_strlcpy(p, "w", sizeof(p));
279 else if (num & BIO_FP_READ)
280 OPENSSL_strlcpy(p, "r", sizeof(p));
282 ERR_raise(ERR_LIB_BIO, BIO_R_BAD_FOPEN_MODE);
286 # if defined(OPENSSL_SYS_MSDOS) || defined(OPENSSL_SYS_WINDOWS)
287 if (!(num & BIO_FP_TEXT))
288 OPENSSL_strlcat(p, "b", sizeof(p));
290 OPENSSL_strlcat(p, "t", sizeof(p));
291 # elif defined(OPENSSL_SYS_WIN32_CYGWIN)
292 if (!(num & BIO_FP_TEXT))
293 OPENSSL_strlcat(p, "b", sizeof(p));
295 fp = openssl_fopen(ptr, p);
297 ERR_raise_data(ERR_LIB_SYS, get_last_sys_error(),
298 "calling fopen(%s, %s)",
300 ERR_raise(ERR_LIB_BIO, ERR_R_SYS_LIB);
306 /* we did fopen -> we disengage UPLINK */
307 BIO_clear_flags(b, BIO_FLAGS_UPLINK_INTERNAL);
309 case BIO_C_GET_FILE_PTR:
310 /* the ptr parameter is actually a FILE ** in this case. */
313 *fpp = (FILE *)b->ptr;
316 case BIO_CTRL_GET_CLOSE:
317 ret = (long)b->shutdown;
319 case BIO_CTRL_SET_CLOSE:
320 b->shutdown = (int)num;
323 st = b->flags & BIO_FLAGS_UPLINK_INTERNAL
324 ? UP_fflush(b->ptr) : fflush((FILE *)b->ptr);
326 ERR_raise_data(ERR_LIB_SYS, get_last_sys_error(),
328 ERR_raise(ERR_LIB_BIO, ERR_R_SYS_LIB);
336 case BIO_CTRL_WPENDING:
337 case BIO_CTRL_PENDING:
347 static int file_gets(BIO *bp, char *buf, int size)
352 if (bp->flags & BIO_FLAGS_UPLINK_INTERNAL) {
353 if (!UP_fgets(buf, size, bp->ptr))
356 if (!fgets(buf, size, (FILE *)bp->ptr))
365 static int file_puts(BIO *bp, const char *str)
370 ret = file_write(bp, str, n);
376 static int file_write(BIO *b, const char *in, int inl)
380 static int file_read(BIO *b, char *out, int outl)
384 static int file_puts(BIO *bp, const char *str)
388 static int file_gets(BIO *bp, char *buf, int size)
392 static long file_ctrl(BIO *b, int cmd, long num, void *ptr)
396 static int file_new(BIO *bi)
400 static int file_free(BIO *a)
405 static const BIO_METHOD methods_filep = {
417 NULL, /* file_callback_ctrl */
420 const BIO_METHOD *BIO_s_file(void)
422 return &methods_filep;
425 BIO *BIO_new_file(const char *filename, const char *mode)
430 #endif /* OPENSSL_NO_STDIO */