Uniform BN_bn2binpad() and BN_bn2lebinpad() implementations
[openssl.git] / crypto / o_fopen.c
1 /*
2  * Copyright 2016-2018 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 # 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 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/cryptlib.h"
29
30 #if !defined(OPENSSL_NO_STDIO)
31
32 # include <stdio.h>
33
34 FILE *openssl_fopen(const char *filename, const char *mode)
35 {
36     FILE *file = NULL;
37 # if defined(_WIN32) && defined(CP_UTF8)
38     int sz, len_0 = (int)strlen(filename) + 1;
39     DWORD flags;
40
41     /*
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
51      * back to fopen...
52      */
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)
58         ) {
59         WCHAR wmode[8];
60         WCHAR *wfilename = _alloca(sz * sizeof(WCHAR));
61
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)
68             ) {
69             /*
70              * UTF-8 decode succeeded, but no file, filename
71              * could still have been locale-ized...
72              */
73             file = fopen(filename, mode);
74         }
75     } else if (GetLastError() == ERROR_NO_UNICODE_TRANSLATION) {
76         file = fopen(filename, mode);
77     }
78 # elif defined(__DJGPP__)
79     {
80         char *newname = NULL;
81
82         if (!HAS_LFN_SUPPORT(filename)) {
83             char *iterator;
84             char lastchar;
85
86             newname = OPENSSL_malloc(strlen(filename) + 1);
87             if (newname == NULL)
88                 return NULL;
89
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. */
95                     *iterator = '_';
96                 } else {
97                     *iterator = *filename;
98                 }
99                 lastchar = *filename;
100             }
101             *iterator = '\0';
102             filename = newname;
103         }
104         file = fopen(filename, mode);
105
106         OPENSSL_free(newname);
107     }
108 # else
109     file = fopen(filename, mode);
110 # endif
111     return file;
112 }
113
114 #else
115
116 void *openssl_fopen(const char *filename, const char *mode)
117 {
118     return NULL;
119 }
120
121 #endif