Coverage Report

Created: 2025-12-31 06:58

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/openssl30/crypto/o_fopen.c
Line
Count
Source
1
/*
2
 * Copyright 2016-2020 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 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 "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
38
FILE *openssl_fopen(const char *filename, const char *mode)
39
5.04k
{
40
5.04k
    FILE *file = NULL;
41
#if defined(_WIN32) && defined(CP_UTF8)
42
    int sz, len_0 = (int)strlen(filename) + 1;
43
    DWORD flags;
44
45
    /*
46
     * Basically there are three cases to cover: a) filename is
47
     * pure ASCII string; b) actual UTF-8 encoded string and
48
     * c) locale-ized string, i.e. one containing 8-bit
49
     * characters that are meaningful in current system locale.
50
     * If filename is pure ASCII or real UTF-8 encoded string,
51
     * MultiByteToWideChar succeeds and _wfopen works. If
52
     * filename is locale-ized string, chances are that
53
     * MultiByteToWideChar fails reporting
54
     * ERROR_NO_UNICODE_TRANSLATION, in which case we fall
55
     * back to fopen...
56
     */
57
    if ((sz = MultiByteToWideChar(CP_UTF8, (flags = MB_ERR_INVALID_CHARS),
58
             filename, len_0, NULL, 0))
59
            > 0
60
        || (GetLastError() == ERROR_INVALID_FLAGS && (sz = MultiByteToWideChar(CP_UTF8, (flags = 0), filename, len_0, NULL, 0)) > 0)) {
61
        WCHAR wmode[8];
62
        WCHAR *wfilename = _alloca(sz * sizeof(WCHAR));
63
64
        if (MultiByteToWideChar(CP_UTF8, flags,
65
                filename, len_0, wfilename, sz)
66
            && MultiByteToWideChar(CP_UTF8, 0, mode, strlen(mode) + 1,
67
                wmode, OSSL_NELEM(wmode))
68
            && (file = _wfopen(wfilename, wmode)) == NULL && (errno == ENOENT || errno == EBADF)) {
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 (pathconf(filename, _PC_NAME_MAX) <= 12) { /* 8.3 file system? */
83
            char *iterator;
84
            char lastchar;
85
86
            if ((newname = OPENSSL_malloc(strlen(filename) + 1)) == NULL) {
87
                ERR_raise(ERR_LIB_CRYPTO, ERR_R_MALLOC_FAILURE);
88
                return NULL;
89
            }
90
91
            for (iterator = newname, lastchar = '\0';
92
                *filename; filename++, iterator++) {
93
                if (lastchar == '/' && filename[0] == '.'
94
                    && filename[1] != '.' && filename[1] != '/') {
95
                    /* Leading dots are not permitted in plain DOS. */
96
                    *iterator = '_';
97
                } else {
98
                    *iterator = *filename;
99
                }
100
                lastchar = *filename;
101
            }
102
            *iterator = '\0';
103
            filename = newname;
104
        }
105
        file = fopen(filename, mode);
106
107
        OPENSSL_free(newname);
108
    }
109
#else
110
5.04k
    file = fopen(filename, mode);
111
5.04k
#endif
112
5.04k
    return file;
113
5.04k
}
114
115
#else
116
117
void *openssl_fopen(const char *filename, const char *mode)
118
{
119
    return NULL;
120
}
121
122
#endif