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