xref: /openssl/crypto/o_fopen.c (revision 7ed6de99)
1 /*
2  * Copyright 2016-2024 The OpenSSL Project Authors. All Rights Reserved.
3  *
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
8  */
9 
10 # if defined(__linux) || defined(__sun) || defined(__hpux)
11 /*
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...
22  */
23 #  ifndef _FILE_OFFSET_BITS
24 #   define _FILE_OFFSET_BITS 64
25 #  endif
26 # endif
27 
28 #include "internal/e_os.h"
29 #include "internal/cryptlib.h"
30 
31 #if !defined(OPENSSL_NO_STDIO)
32 
33 # include <stdio.h>
34 # ifdef __DJGPP__
35 #  include <unistd.h>
36 # endif
37 
openssl_fopen(const char * filename,const char * mode)38 FILE *openssl_fopen(const char *filename, const char *mode)
39 {
40     FILE *file = NULL;
41 # if defined(_WIN32) && defined(CP_UTF8)
42     int sz, len_0;
43     DWORD flags;
44 # endif
45 
46     if (filename == NULL)
47         return NULL;
48 # if defined(_WIN32) && defined(CP_UTF8)
49     len_0 = (int)strlen(filename) + 1;
50 
51     /*
52      * Basically there are three cases to cover: a) filename is
53      * pure ASCII string; b) actual UTF-8 encoded string and
54      * c) locale-ized string, i.e. one containing 8-bit
55      * characters that are meaningful in current system locale.
56      * If filename is pure ASCII or real UTF-8 encoded string,
57      * MultiByteToWideChar succeeds and _wfopen works. If
58      * filename is locale-ized string, chances are that
59      * MultiByteToWideChar fails reporting
60      * ERROR_NO_UNICODE_TRANSLATION, in which case we fall
61      * back to fopen...
62      */
63     if ((sz = MultiByteToWideChar(CP_UTF8, (flags = MB_ERR_INVALID_CHARS),
64                                   filename, len_0, NULL, 0)) > 0 ||
65         (GetLastError() == ERROR_INVALID_FLAGS &&
66          (sz = MultiByteToWideChar(CP_UTF8, (flags = 0),
67                                    filename, len_0, NULL, 0)) > 0)
68         ) {
69         WCHAR wmode[8];
70         WCHAR *wfilename = _alloca(sz * sizeof(WCHAR));
71 
72         if (MultiByteToWideChar(CP_UTF8, flags,
73                                 filename, len_0, wfilename, sz) &&
74             MultiByteToWideChar(CP_UTF8, 0, mode, strlen(mode) + 1,
75                                 wmode, OSSL_NELEM(wmode)) &&
76             (file = _wfopen(wfilename, wmode)) == NULL &&
77             (errno == ENOENT || errno == EBADF)
78             ) {
79             /*
80              * UTF-8 decode succeeded, but no file, filename
81              * could still have been locale-ized...
82              */
83             file = fopen(filename, mode);
84         }
85     } else if (GetLastError() == ERROR_NO_UNICODE_TRANSLATION) {
86         file = fopen(filename, mode);
87     }
88 # elif defined(__DJGPP__)
89     {
90         char *newname = NULL;
91 
92         if (pathconf(filename, _PC_NAME_MAX) <= 12) {  /* 8.3 file system? */
93             char *iterator;
94             char lastchar;
95 
96             if ((newname = OPENSSL_malloc(strlen(filename) + 1)) == NULL)
97                 return NULL;
98 
99             for (iterator = newname, lastchar = '\0';
100                 *filename; filename++, iterator++) {
101                 if (lastchar == '/' && filename[0] == '.'
102                     && filename[1] != '.' && filename[1] != '/') {
103                     /* Leading dots are not permitted in plain DOS. */
104                     *iterator = '_';
105                 } else {
106                     *iterator = *filename;
107                 }
108                 lastchar = *filename;
109             }
110             *iterator = '\0';
111             filename = newname;
112         }
113         file = fopen(filename, mode);
114 
115         OPENSSL_free(newname);
116     }
117 # else
118     file = fopen(filename, mode);
119 # endif
120     return file;
121 }
122 
123 #else
124 
openssl_fopen(const char * filename,const char * mode)125 void *openssl_fopen(const char *filename, const char *mode)
126 {
127     return NULL;
128 }
129 
130 #endif
131