2 * Copyright 1995-2017 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
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 BIOerr(BIO_F_BIO_NEW_FILE
, BIO_R_NO_SUCH_FILE
);
79 BIOerr(BIO_F_BIO_NEW_FILE
, 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 BIOerr(BIO_F_FILE_READ
, 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
);
240 # elif defined(OPENSSL_SYS_MSDOS)
241 int fd
= fileno((FILE *)ptr
);
242 /* Set correct text/binary mode */
243 if (num
& BIO_FP_TEXT
)
244 _setmode(fd
, _O_TEXT
);
245 /* Dangerous to set stdin/stdout to raw (unless redirected) */
247 if (fd
== STDIN_FILENO
|| fd
== STDOUT_FILENO
) {
249 _setmode(fd
, _O_BINARY
);
251 _setmode(fd
, _O_BINARY
);
253 # elif defined(OPENSSL_SYS_WIN32_CYGWIN)
254 int fd
= fileno((FILE *)ptr
);
255 if (!(num
& BIO_FP_TEXT
))
256 setmode(fd
, O_BINARY
);
260 case BIO_C_SET_FILENAME
:
262 b
->shutdown
= (int)num
& BIO_CLOSE
;
263 if (num
& BIO_FP_APPEND
) {
264 if (num
& BIO_FP_READ
)
265 OPENSSL_strlcpy(p
, "a+", sizeof(p
));
267 OPENSSL_strlcpy(p
, "a", sizeof(p
));
268 } else if ((num
& BIO_FP_READ
) && (num
& BIO_FP_WRITE
))
269 OPENSSL_strlcpy(p
, "r+", sizeof(p
));
270 else if (num
& BIO_FP_WRITE
)
271 OPENSSL_strlcpy(p
, "w", sizeof(p
));
272 else if (num
& BIO_FP_READ
)
273 OPENSSL_strlcpy(p
, "r", sizeof(p
));
275 BIOerr(BIO_F_FILE_CTRL
, BIO_R_BAD_FOPEN_MODE
);
279 # if defined(OPENSSL_SYS_MSDOS) || defined(OPENSSL_SYS_WINDOWS)
280 if (!(num
& BIO_FP_TEXT
))
281 OPENSSL_strlcat(p
, "b", sizeof(p
));
283 OPENSSL_strlcat(p
, "t", sizeof(p
));
284 # elif defined(OPENSSL_SYS_WIN32_CYGWIN)
285 if (!(num
& BIO_FP_TEXT
))
286 OPENSSL_strlcat(p
, "b", sizeof(p
));
288 fp
= openssl_fopen(ptr
, p
);
290 ERR_raise_data(ERR_LIB_SYS
, get_last_sys_error(),
291 "calling fopen(%s, %s)",
293 BIOerr(BIO_F_FILE_CTRL
, ERR_R_SYS_LIB
);
299 /* we did fopen -> we disengage UPLINK */
300 BIO_clear_flags(b
, BIO_FLAGS_UPLINK_INTERNAL
);
302 case BIO_C_GET_FILE_PTR
:
303 /* the ptr parameter is actually a FILE ** in this case. */
306 *fpp
= (FILE *)b
->ptr
;
309 case BIO_CTRL_GET_CLOSE
:
310 ret
= (long)b
->shutdown
;
312 case BIO_CTRL_SET_CLOSE
:
313 b
->shutdown
= (int)num
;
316 st
= b
->flags
& BIO_FLAGS_UPLINK_INTERNAL
317 ? UP_fflush(b
->ptr
) : fflush((FILE *)b
->ptr
);
319 ERR_raise_data(ERR_LIB_SYS
, get_last_sys_error(),
321 BIOerr(BIO_F_FILE_CTRL
, ERR_R_SYS_LIB
);
329 case BIO_CTRL_WPENDING
:
330 case BIO_CTRL_PENDING
:
340 static int file_gets(BIO
*bp
, char *buf
, int size
)
345 if (bp
->flags
& BIO_FLAGS_UPLINK_INTERNAL
) {
346 if (!UP_fgets(buf
, size
, bp
->ptr
))
349 if (!fgets(buf
, size
, (FILE *)bp
->ptr
))
358 static int file_puts(BIO
*bp
, const char *str
)
363 ret
= file_write(bp
, str
, n
);
369 static int file_write(BIO
*b
, const char *in
, int inl
)
373 static int file_read(BIO
*b
, char *out
, int outl
)
377 static int file_puts(BIO
*bp
, const char *str
)
381 static int file_gets(BIO
*bp
, char *buf
, int size
)
385 static long file_ctrl(BIO
*b
, int cmd
, long num
, void *ptr
)
389 static int file_new(BIO
*bi
)
393 static int file_free(BIO
*a
)
398 static const BIO_METHOD methods_filep
= {
401 /* TODO: Convert to new style write function */
404 /* TODO: Convert to new style read function */
412 NULL
, /* file_callback_ctrl */
415 const BIO_METHOD
*BIO_s_file(void)
417 return &methods_filep
;
420 BIO
*BIO_new_file(const char *filename
, const char *mode
)
425 #endif /* OPENSSL_NO_STDIO */