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 = {
64 static FILE *file_fopen(const char *filename, const char *mode)
68 # if defined(_WIN32) && defined(CP_UTF8)
69 int sz, len_0 = (int)strlen(filename) + 1;
73 * Basically there are three cases to cover: a) filename is
74 * pure ASCII string; b) actual UTF-8 encoded string and
75 * c) locale-ized string, i.e. one containing 8-bit
76 * characters that are meaningful in current system locale.
77 * If filename is pure ASCII or real UTF-8 encoded string,
78 * MultiByteToWideChar succeeds and _wfopen works. If
79 * filename is locale-ized string, chances are that
80 * MultiByteToWideChar fails reporting
81 * ERROR_NO_UNICODE_TRANSLATION, in which case we fall
84 if ((sz = MultiByteToWideChar(CP_UTF8, (flags = MB_ERR_INVALID_CHARS),
85 filename, len_0, NULL, 0)) > 0 ||
86 (GetLastError() == ERROR_INVALID_FLAGS &&
87 (sz = MultiByteToWideChar(CP_UTF8, (flags = 0),
88 filename, len_0, NULL, 0)) > 0)
91 WCHAR *wfilename = _alloca(sz * sizeof(WCHAR));
93 if (MultiByteToWideChar(CP_UTF8, flags,
94 filename, len_0, wfilename, sz) &&
95 MultiByteToWideChar(CP_UTF8, 0, mode, strlen(mode) + 1,
96 wmode, OSSL_NELEM(wmode)) &&
97 (file = _wfopen(wfilename, wmode)) == NULL &&
98 (errno == ENOENT || errno == EBADF)
101 * UTF-8 decode succeeded, but no file, filename
102 * could still have been locale-ized...
104 file = fopen(filename, mode);
106 } else if (GetLastError() == ERROR_NO_UNICODE_TRANSLATION) {
107 file = fopen(filename, mode);
109 # elif defined(__DJGPP__)
111 char *newname = NULL;
113 if (!HAS_LFN_SUPPORT(filename)) {
117 newname = OPENSSL_malloc(strlen(filename) + 1);
121 for(iterator = newname, lastchar = '\0';
122 *filename; filename++, iterator++) {
123 if (lastchar == '/' && filename[0] == '.'
124 && filename[1] != '.' && filename[1] != '/') {
125 /* Leading dots are not permitted in plain DOS. */
128 *iterator = *filename;
130 lastchar = *filename;
135 file = fopen(filename, mode);
137 OPENSSL_free(newname);
140 file = fopen(filename, mode);
145 BIO *BIO_new_file(const char *filename, const char *mode)
148 FILE *file = file_fopen(filename, mode);
149 int fp_flags = BIO_CLOSE;
151 if (strchr(mode, 'b') == NULL)
152 fp_flags |= BIO_FP_TEXT;
155 SYSerr(SYS_F_FOPEN, get_last_sys_error());
156 ERR_add_error_data(5, "fopen('", filename, "','", mode, "')");
158 BIOerr(BIO_F_BIO_NEW_FILE, BIO_R_NO_SUCH_FILE);
160 BIOerr(BIO_F_BIO_NEW_FILE, ERR_R_SYS_LIB);
163 if ((ret = BIO_new(BIO_s_file())) == NULL) {
168 BIO_clear_flags(ret, BIO_FLAGS_UPLINK); /* we did fopen -> we disengage
170 BIO_set_fp(ret, file, fp_flags);
174 BIO *BIO_new_fp(FILE *stream, int close_flag)
178 if ((ret = BIO_new(BIO_s_file())) == NULL)
181 /* redundant flag, left for documentation purposes */
182 BIO_set_flags(ret, BIO_FLAGS_UPLINK);
183 BIO_set_fp(ret, stream, close_flag);
187 const BIO_METHOD *BIO_s_file(void)
189 return (&methods_filep);
192 static int file_new(BIO *bi)
197 bi->flags = BIO_FLAGS_UPLINK; /* default to UPLINK */
201 static int file_free(BIO *a)
206 if ((a->init) && (a->ptr != NULL)) {
207 if (a->flags & BIO_FLAGS_UPLINK)
212 a->flags = BIO_FLAGS_UPLINK;
219 static int file_read(BIO *b, char *out, int outl)
223 if (b->init && (out != NULL)) {
224 if (b->flags & BIO_FLAGS_UPLINK)
225 ret = UP_fread(out, 1, (int)outl, b->ptr);
227 ret = fread(out, 1, (int)outl, (FILE *)b->ptr);
229 && (b->flags & BIO_FLAGS_UPLINK) ? UP_ferror((FILE *)b->ptr) :
230 ferror((FILE *)b->ptr)) {
231 SYSerr(SYS_F_FREAD, get_last_sys_error());
232 BIOerr(BIO_F_FILE_READ, ERR_R_SYS_LIB);
239 static int file_write(BIO *b, const char *in, int inl)
243 if (b->init && (in != NULL)) {
244 if (b->flags & BIO_FLAGS_UPLINK)
245 ret = UP_fwrite(in, (int)inl, 1, b->ptr);
247 ret = fwrite(in, (int)inl, 1, (FILE *)b->ptr);
250 /* ret=fwrite(in,1,(int)inl,(FILE *)b->ptr); */
252 * according to Tim Hudson <tjh@cryptsoft.com>, the commented out
253 * version above can cause 'inl' write calls under some stupid stdio
254 * implementations (VMS)
260 static long file_ctrl(BIO *b, int cmd, long num, void *ptr)
263 FILE *fp = (FILE *)b->ptr;
268 case BIO_C_FILE_SEEK:
270 if (b->flags & BIO_FLAGS_UPLINK)
271 ret = (long)UP_fseek(b->ptr, num, 0);
273 ret = (long)fseek(fp, num, 0);
276 if (b->flags & BIO_FLAGS_UPLINK)
277 ret = (long)UP_feof(fp);
279 ret = (long)feof(fp);
281 case BIO_C_FILE_TELL:
283 if (b->flags & BIO_FLAGS_UPLINK)
284 ret = UP_ftell(b->ptr);
288 case BIO_C_SET_FILE_PTR:
290 b->shutdown = (int)num & BIO_CLOSE;
293 # if BIO_FLAGS_UPLINK!=0
294 # if defined(__MINGW32__) && defined(__MSVCRT__) && !defined(_IOB_ENTRIES)
295 # define _IOB_ENTRIES 20
297 /* Safety net to catch purely internal BIO_set_fp calls */
298 # if defined(_MSC_VER) && _MSC_VER>=1900
299 if (ptr == stdin || ptr == stdout || ptr == stderr)
300 BIO_clear_flags(b, BIO_FLAGS_UPLINK);
301 # elif defined(_IOB_ENTRIES)
302 if ((size_t)ptr >= (size_t)stdin &&
303 (size_t)ptr < (size_t)(stdin + _IOB_ENTRIES))
304 BIO_clear_flags(b, BIO_FLAGS_UPLINK);
308 if (b->flags & BIO_FLAGS_UPLINK)
309 UP_fsetmod(b->ptr, (char)((num & BIO_FP_TEXT) ? 't' : 'b'));
313 # if defined(OPENSSL_SYS_WINDOWS)
314 int fd = _fileno((FILE *)ptr);
315 if (num & BIO_FP_TEXT)
316 _setmode(fd, _O_TEXT);
318 _setmode(fd, _O_BINARY);
319 # elif defined(OPENSSL_SYS_MSDOS)
320 int fd = fileno((FILE *)ptr);
321 /* Set correct text/binary mode */
322 if (num & BIO_FP_TEXT)
323 _setmode(fd, _O_TEXT);
324 /* Dangerous to set stdin/stdout to raw (unless redirected) */
326 if (fd == STDIN_FILENO || fd == STDOUT_FILENO) {
328 _setmode(fd, _O_BINARY);
330 _setmode(fd, _O_BINARY);
332 # elif defined(OPENSSL_SYS_WIN32_CYGWIN)
333 int fd = fileno((FILE *)ptr);
334 if (num & BIO_FP_TEXT)
337 setmode(fd, O_BINARY);
341 case BIO_C_SET_FILENAME:
343 b->shutdown = (int)num & BIO_CLOSE;
344 if (num & BIO_FP_APPEND) {
345 if (num & BIO_FP_READ)
346 OPENSSL_strlcpy(p, "a+", sizeof p);
348 OPENSSL_strlcpy(p, "a", sizeof p);
349 } else if ((num & BIO_FP_READ) && (num & BIO_FP_WRITE))
350 OPENSSL_strlcpy(p, "r+", sizeof p);
351 else if (num & BIO_FP_WRITE)
352 OPENSSL_strlcpy(p, "w", sizeof p);
353 else if (num & BIO_FP_READ)
354 OPENSSL_strlcpy(p, "r", sizeof p);
356 BIOerr(BIO_F_FILE_CTRL, BIO_R_BAD_FOPEN_MODE);
360 # if defined(OPENSSL_SYS_MSDOS) || defined(OPENSSL_SYS_WINDOWS) || defined(OPENSSL_SYS_WIN32_CYGWIN)
361 if (!(num & BIO_FP_TEXT))
366 fp = file_fopen(ptr, p);
368 SYSerr(SYS_F_FOPEN, get_last_sys_error());
369 ERR_add_error_data(5, "fopen('", ptr, "','", p, "')");
370 BIOerr(BIO_F_FILE_CTRL, ERR_R_SYS_LIB);
376 BIO_clear_flags(b, BIO_FLAGS_UPLINK); /* we did fopen -> we disengage
379 case BIO_C_GET_FILE_PTR:
380 /* the ptr parameter is actually a FILE ** in this case. */
383 *fpp = (FILE *)b->ptr;
386 case BIO_CTRL_GET_CLOSE:
387 ret = (long)b->shutdown;
389 case BIO_CTRL_SET_CLOSE:
390 b->shutdown = (int)num;
393 if (b->flags & BIO_FLAGS_UPLINK)
396 fflush((FILE *)b->ptr);
402 case BIO_CTRL_WPENDING:
403 case BIO_CTRL_PENDING:
413 static int file_gets(BIO *bp, char *buf, int size)
418 if (bp->flags & BIO_FLAGS_UPLINK) {
419 if (!UP_fgets(buf, size, bp->ptr))
422 if (!fgets(buf, size, (FILE *)bp->ptr))
431 static int file_puts(BIO *bp, const char *str)
436 ret = file_write(bp, str, n);
442 static int file_write(BIO *b, const char *in, int inl)
446 static int file_read(BIO *b, char *out, int outl)
450 static int file_puts(BIO *bp, const char *str)
454 static int file_gets(BIO *bp, char *buf, int size)
458 static long file_ctrl(BIO *b, int cmd, long num, void *ptr)
462 static int file_new(BIO *bi)
466 static int file_free(BIO *a)
471 static const BIO_METHOD methods_filep = {
484 const BIO_METHOD *BIO_s_file(void)
486 return (&methods_filep);
489 BIO *BIO_new_file(const char *filename, const char *mode)
494 # endif /* OPENSSL_NO_STDIO */
496 #endif /* HEADER_BSS_FILE_C */