2 * Copyright 1995-2016 The OpenSSL Project Authors. All Rights Reserved.
4 * Licensed under the OpenSSL license (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
11 * 03-Dec-1997 rdenny@dc3.com Fix bug preventing use of stdin/stdout
12 * with binary data (e.g. asn1parse -inform DER < xxx) under
16 #ifndef HEADER_BSS_FILE_C
17 # define HEADER_BSS_FILE_C
19 # if defined(__linux) || defined(__sun) || defined(__hpux)
21 * Following definition aliases fopen to fopen64 on above mentioned
22 * platforms. This makes it possible to open and sequentially access files
23 * larger than 2GB from 32-bit application. It does not allow to traverse
24 * them beyond 2GB with fseek/ftell, but on the other hand *no* 32-bit
25 * platform permits that, not with fseek/ftell. Not to mention that breaking
26 * 2GB limit for seeking would require surgery to *our* API. But sequential
27 * access suffices for practical cases when you can run into large files,
28 * such as fingerprinting, so we can let API alone. For reference, the list
29 * of 32-bit platforms which allow for sequential access of large files
30 * without extra "magic" comprise *BSD, Darwin, IRIX...
32 # ifndef _FILE_OFFSET_BITS
33 # define _FILE_OFFSET_BITS 64
40 # include <openssl/err.h>
42 # if !defined(OPENSSL_NO_STDIO)
44 static int file_write(BIO *h, const char *buf, int num);
45 static int file_read(BIO *h, char *buf, int size);
46 static int file_puts(BIO *h, const char *str);
47 static int file_gets(BIO *h, char *str, int size);
48 static long file_ctrl(BIO *h, int cmd, long arg1, void *arg2);
49 static int file_new(BIO *h);
50 static int file_free(BIO *data);
51 static const BIO_METHOD methods_filep = {
54 /* TODO: Convert to new style write function */
57 /* TODO: Convert to new style read function */
68 BIO *BIO_new_file(const char *filename, const char *mode)
71 FILE *file = openssl_fopen(filename, mode);
72 int fp_flags = BIO_CLOSE;
74 if (strchr(mode, 'b') == NULL)
75 fp_flags |= BIO_FP_TEXT;
78 SYSerr(SYS_F_FOPEN, get_last_sys_error());
79 ERR_add_error_data(5, "fopen('", filename, "','", mode, "')");
85 BIOerr(BIO_F_BIO_NEW_FILE, BIO_R_NO_SUCH_FILE);
87 BIOerr(BIO_F_BIO_NEW_FILE, ERR_R_SYS_LIB);
90 if ((ret = BIO_new(BIO_s_file())) == NULL) {
95 BIO_clear_flags(ret, BIO_FLAGS_UPLINK); /* we did fopen -> we disengage
97 BIO_set_fp(ret, file, fp_flags);
101 BIO *BIO_new_fp(FILE *stream, int close_flag)
105 if ((ret = BIO_new(BIO_s_file())) == NULL)
108 /* redundant flag, left for documentation purposes */
109 BIO_set_flags(ret, BIO_FLAGS_UPLINK);
110 BIO_set_fp(ret, stream, close_flag);
114 const BIO_METHOD *BIO_s_file(void)
116 return (&methods_filep);
119 static int file_new(BIO *bi)
124 bi->flags = BIO_FLAGS_UPLINK; /* default to UPLINK */
128 static int file_free(BIO *a)
133 if ((a->init) && (a->ptr != NULL)) {
134 if (a->flags & BIO_FLAGS_UPLINK)
139 a->flags = BIO_FLAGS_UPLINK;
146 static int file_read(BIO *b, char *out, int outl)
150 if (b->init && (out != NULL)) {
151 if (b->flags & BIO_FLAGS_UPLINK)
152 ret = UP_fread(out, 1, (int)outl, b->ptr);
154 ret = fread(out, 1, (int)outl, (FILE *)b->ptr);
156 && (b->flags & BIO_FLAGS_UPLINK) ? UP_ferror((FILE *)b->ptr) :
157 ferror((FILE *)b->ptr)) {
158 SYSerr(SYS_F_FREAD, get_last_sys_error());
159 BIOerr(BIO_F_FILE_READ, ERR_R_SYS_LIB);
166 static int file_write(BIO *b, const char *in, int inl)
170 if (b->init && (in != NULL)) {
171 if (b->flags & BIO_FLAGS_UPLINK)
172 ret = UP_fwrite(in, (int)inl, 1, b->ptr);
174 ret = fwrite(in, (int)inl, 1, (FILE *)b->ptr);
177 /* ret=fwrite(in,1,(int)inl,(FILE *)b->ptr); */
179 * according to Tim Hudson <tjh@cryptsoft.com>, the commented out
180 * version above can cause 'inl' write calls under some stupid stdio
181 * implementations (VMS)
187 static long file_ctrl(BIO *b, int cmd, long num, void *ptr)
190 FILE *fp = (FILE *)b->ptr;
195 case BIO_C_FILE_SEEK:
197 if (b->flags & BIO_FLAGS_UPLINK)
198 ret = (long)UP_fseek(b->ptr, num, 0);
200 ret = (long)fseek(fp, num, 0);
203 if (b->flags & BIO_FLAGS_UPLINK)
204 ret = (long)UP_feof(fp);
206 ret = (long)feof(fp);
208 case BIO_C_FILE_TELL:
210 if (b->flags & BIO_FLAGS_UPLINK)
211 ret = UP_ftell(b->ptr);
215 case BIO_C_SET_FILE_PTR:
217 b->shutdown = (int)num & BIO_CLOSE;
220 # if BIO_FLAGS_UPLINK!=0
221 # if defined(__MINGW32__) && defined(__MSVCRT__) && !defined(_IOB_ENTRIES)
222 # define _IOB_ENTRIES 20
224 /* Safety net to catch purely internal BIO_set_fp calls */
225 # if defined(_MSC_VER) && _MSC_VER>=1900
226 if (ptr == stdin || ptr == stdout || ptr == stderr)
227 BIO_clear_flags(b, BIO_FLAGS_UPLINK);
228 # elif defined(_IOB_ENTRIES)
229 if ((size_t)ptr >= (size_t)stdin &&
230 (size_t)ptr < (size_t)(stdin + _IOB_ENTRIES))
231 BIO_clear_flags(b, BIO_FLAGS_UPLINK);
235 if (b->flags & BIO_FLAGS_UPLINK)
236 UP_fsetmod(b->ptr, (char)((num & BIO_FP_TEXT) ? 't' : 'b'));
240 # if defined(OPENSSL_SYS_WINDOWS)
241 int fd = _fileno((FILE *)ptr);
242 if (num & BIO_FP_TEXT)
243 _setmode(fd, _O_TEXT);
245 _setmode(fd, _O_BINARY);
246 # elif defined(OPENSSL_SYS_MSDOS)
247 int fd = fileno((FILE *)ptr);
248 /* Set correct text/binary mode */
249 if (num & BIO_FP_TEXT)
250 _setmode(fd, _O_TEXT);
251 /* Dangerous to set stdin/stdout to raw (unless redirected) */
253 if (fd == STDIN_FILENO || fd == STDOUT_FILENO) {
255 _setmode(fd, _O_BINARY);
257 _setmode(fd, _O_BINARY);
259 # elif defined(OPENSSL_SYS_WIN32_CYGWIN)
260 int fd = fileno((FILE *)ptr);
261 if (num & BIO_FP_TEXT)
264 setmode(fd, O_BINARY);
268 case BIO_C_SET_FILENAME:
270 b->shutdown = (int)num & BIO_CLOSE;
271 if (num & BIO_FP_APPEND) {
272 if (num & BIO_FP_READ)
273 OPENSSL_strlcpy(p, "a+", sizeof p);
275 OPENSSL_strlcpy(p, "a", sizeof p);
276 } else if ((num & BIO_FP_READ) && (num & BIO_FP_WRITE))
277 OPENSSL_strlcpy(p, "r+", sizeof p);
278 else if (num & BIO_FP_WRITE)
279 OPENSSL_strlcpy(p, "w", sizeof p);
280 else if (num & BIO_FP_READ)
281 OPENSSL_strlcpy(p, "r", sizeof p);
283 BIOerr(BIO_F_FILE_CTRL, BIO_R_BAD_FOPEN_MODE);
287 # if defined(OPENSSL_SYS_MSDOS) || defined(OPENSSL_SYS_WINDOWS) || defined(OPENSSL_SYS_WIN32_CYGWIN)
288 if (!(num & BIO_FP_TEXT))
293 fp = openssl_fopen(ptr, p);
295 SYSerr(SYS_F_FOPEN, get_last_sys_error());
296 ERR_add_error_data(5, "fopen('", ptr, "','", p, "')");
297 BIOerr(BIO_F_FILE_CTRL, ERR_R_SYS_LIB);
303 BIO_clear_flags(b, BIO_FLAGS_UPLINK); /* we did fopen -> we disengage
306 case BIO_C_GET_FILE_PTR:
307 /* the ptr parameter is actually a FILE ** in this case. */
310 *fpp = (FILE *)b->ptr;
313 case BIO_CTRL_GET_CLOSE:
314 ret = (long)b->shutdown;
316 case BIO_CTRL_SET_CLOSE:
317 b->shutdown = (int)num;
320 if (b->flags & BIO_FLAGS_UPLINK)
323 fflush((FILE *)b->ptr);
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) {
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 */
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 */
427 #endif /* HEADER_BSS_FILE_C */