2 * Copyright 2016-2018 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
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
28 #include "internal/cryptlib.h"
30 #if !defined(OPENSSL_NO_STDIO)
34 FILE *openssl_fopen(const char *filename, const char *mode)
37 # if defined(_WIN32) && defined(CP_UTF8)
38 int sz, len_0 = (int)strlen(filename) + 1;
42 * Basically there are three cases to cover: a) filename is
43 * pure ASCII string; b) actual UTF-8 encoded string and
44 * c) locale-ized string, i.e. one containing 8-bit
45 * characters that are meaningful in current system locale.
46 * If filename is pure ASCII or real UTF-8 encoded string,
47 * MultiByteToWideChar succeeds and _wfopen works. If
48 * filename is locale-ized string, chances are that
49 * MultiByteToWideChar fails reporting
50 * ERROR_NO_UNICODE_TRANSLATION, in which case we fall
53 if ((sz = MultiByteToWideChar(CP_UTF8, (flags = MB_ERR_INVALID_CHARS),
54 filename, len_0, NULL, 0)) > 0 ||
55 (GetLastError() == ERROR_INVALID_FLAGS &&
56 (sz = MultiByteToWideChar(CP_UTF8, (flags = 0),
57 filename, len_0, NULL, 0)) > 0)
60 WCHAR *wfilename = _alloca(sz * sizeof(WCHAR));
62 if (MultiByteToWideChar(CP_UTF8, flags,
63 filename, len_0, wfilename, sz) &&
64 MultiByteToWideChar(CP_UTF8, 0, mode, strlen(mode) + 1,
65 wmode, OSSL_NELEM(wmode)) &&
66 (file = _wfopen(wfilename, wmode)) == NULL &&
67 (errno == ENOENT || errno == EBADF)
70 * UTF-8 decode succeeded, but no file, filename
71 * could still have been locale-ized...
73 file = fopen(filename, mode);
75 } else if (GetLastError() == ERROR_NO_UNICODE_TRANSLATION) {
76 file = fopen(filename, mode);
78 # elif defined(__DJGPP__)
82 if (!HAS_LFN_SUPPORT(filename)) {
86 newname = OPENSSL_malloc(strlen(filename) + 1);
90 for (iterator = newname, lastchar = '\0';
91 *filename; filename++, iterator++) {
92 if (lastchar == '/' && filename[0] == '.'
93 && filename[1] != '.' && filename[1] != '/') {
94 /* Leading dots are not permitted in plain DOS. */
97 *iterator = *filename;
104 file = fopen(filename, mode);
106 OPENSSL_free(newname);
109 file = fopen(filename, mode);
116 void *openssl_fopen(const char *filename, const char *mode)