Coverage Report

Created: 2018-08-29 13:53

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