Coverage Report

Created: 2025-09-05 07:09

/src/libxlsxwriter/third_party/minizip/ioapi.c
Line
Count
Source (jump to first uncovered line)
1
/* ioapi.h -- IO base function header for compress/uncompress .zip
2
   part of the MiniZip project - ( http://www.winimage.com/zLibDll/minizip.html )
3
4
         Copyright (C) 1998-2010 Gilles Vollant (minizip) ( http://www.winimage.com/zLibDll/minizip.html )
5
6
         Modifications for Zip64 support
7
         Copyright (C) 2009-2010 Mathias Svensson ( http://result42.com )
8
9
         For more info read MiniZip_info.txt
10
11
*/
12
13
#if defined(_WIN32) && (!(defined(_CRT_SECURE_NO_WARNINGS)))
14
        #define _CRT_SECURE_NO_WARNINGS
15
#endif
16
17
#if defined(__APPLE__) || defined(IOAPI_NO_64) || defined(__HAIKU__) || defined(MINIZIP_FOPEN_NO_64)
18
// In darwin and perhaps other BSD variants off_t is a 64 bit value, hence no need for specific 64 bit functions
19
#define FOPEN_FUNC(filename, mode) fopen(filename, mode)
20
#define FTELLO_FUNC(stream) ftello(stream)
21
#define FSEEKO_FUNC(stream, offset, origin) fseeko(stream, offset, origin)
22
#else
23
1.30k
#define FOPEN_FUNC(filename, mode) fopen64(filename, mode)
24
28.5k
#define FTELLO_FUNC(stream) ftello64(stream)
25
25.9k
#define FSEEKO_FUNC(stream, offset, origin) fseeko64(stream, offset, origin)
26
#endif
27
28
29
#include "ioapi.h"
30
31
1.30k
voidpf call_zopen64 (const zlib_filefunc64_32_def* pfilefunc, const void*filename, int mode) {
32
1.30k
    if (pfilefunc->zfile_func64.zopen64_file != NULL)
33
1.30k
        return (*(pfilefunc->zfile_func64.zopen64_file)) (pfilefunc->zfile_func64.opaque,filename,mode);
34
0
    else
35
0
    {
36
0
        return (*(pfilefunc->zopen32_file))(pfilefunc->zfile_func64.opaque,(const char*)filename,mode);
37
0
    }
38
1.30k
}
39
40
25.9k
long call_zseek64 (const zlib_filefunc64_32_def* pfilefunc,voidpf filestream, ZPOS64_T offset, int origin) {
41
25.9k
    if (pfilefunc->zfile_func64.zseek64_file != NULL)
42
25.9k
        return (*(pfilefunc->zfile_func64.zseek64_file)) (pfilefunc->zfile_func64.opaque,filestream,offset,origin);
43
0
    else
44
0
    {
45
0
        uLong offsetTruncated = (uLong)offset;
46
0
        if (offsetTruncated != offset)
47
0
            return -1;
48
0
        else
49
0
            return (*(pfilefunc->zseek32_file))(pfilefunc->zfile_func64.opaque,filestream,offsetTruncated,origin);
50
0
    }
51
25.9k
}
52
53
28.5k
ZPOS64_T call_ztell64 (const zlib_filefunc64_32_def* pfilefunc, voidpf filestream) {
54
28.5k
    if (pfilefunc->zfile_func64.zseek64_file != NULL)
55
28.5k
        return (*(pfilefunc->zfile_func64.ztell64_file)) (pfilefunc->zfile_func64.opaque,filestream);
56
0
    else
57
0
    {
58
0
        uLong tell_uLong = (uLong)(*(pfilefunc->ztell32_file))(pfilefunc->zfile_func64.opaque,filestream);
59
0
        if ((tell_uLong) == MAXU32)
60
0
            return (ZPOS64_T)-1;
61
0
        else
62
0
            return tell_uLong;
63
0
    }
64
28.5k
}
65
66
0
void fill_zlib_filefunc64_32_def_from_filefunc32(zlib_filefunc64_32_def* p_filefunc64_32, const zlib_filefunc_def* p_filefunc32) {
67
0
    p_filefunc64_32->zfile_func64.zopen64_file = NULL;
68
0
    p_filefunc64_32->zopen32_file = p_filefunc32->zopen_file;
69
0
    p_filefunc64_32->zfile_func64.zread_file = p_filefunc32->zread_file;
70
0
    p_filefunc64_32->zfile_func64.zwrite_file = p_filefunc32->zwrite_file;
71
0
    p_filefunc64_32->zfile_func64.ztell64_file = NULL;
72
0
    p_filefunc64_32->zfile_func64.zseek64_file = NULL;
73
0
    p_filefunc64_32->zfile_func64.zclose_file = p_filefunc32->zclose_file;
74
0
    p_filefunc64_32->zfile_func64.zerror_file = p_filefunc32->zerror_file;
75
0
    p_filefunc64_32->zfile_func64.opaque = p_filefunc32->opaque;
76
0
    p_filefunc64_32->zseek32_file = p_filefunc32->zseek_file;
77
0
    p_filefunc64_32->ztell32_file = p_filefunc32->ztell_file;
78
0
}
79
80
81
82
0
static voidpf ZCALLBACK fopen_file_func(voidpf opaque, const char* filename, int mode) {
83
0
    FILE* file = NULL;
84
0
    const char* mode_fopen = NULL;
85
0
    (void)opaque;
86
0
    if ((mode & ZLIB_FILEFUNC_MODE_READWRITEFILTER)==ZLIB_FILEFUNC_MODE_READ)
87
0
        mode_fopen = "rb";
88
0
    else
89
0
    if (mode & ZLIB_FILEFUNC_MODE_EXISTING)
90
0
        mode_fopen = "r+b";
91
0
    else
92
0
    if (mode & ZLIB_FILEFUNC_MODE_CREATE)
93
0
        mode_fopen = "wb";
94
95
0
    if ((filename!=NULL) && (mode_fopen != NULL))
96
0
        file = fopen(filename, mode_fopen);
97
0
    return file;
98
0
}
99
100
1.30k
static voidpf ZCALLBACK fopen64_file_func(voidpf opaque, const void* filename, int mode) {
101
1.30k
    FILE* file = NULL;
102
1.30k
    const char* mode_fopen = NULL;
103
1.30k
    (void)opaque;
104
1.30k
    if ((mode & ZLIB_FILEFUNC_MODE_READWRITEFILTER)==ZLIB_FILEFUNC_MODE_READ)
105
0
        mode_fopen = "rb";
106
1.30k
    else
107
1.30k
    if (mode & ZLIB_FILEFUNC_MODE_EXISTING)
108
0
        mode_fopen = "r+b";
109
1.30k
    else
110
1.30k
    if (mode & ZLIB_FILEFUNC_MODE_CREATE)
111
1.30k
        mode_fopen = "wb";
112
113
1.30k
    if ((filename!=NULL) && (mode_fopen != NULL))
114
1.30k
        file = FOPEN_FUNC((const char*)filename, mode_fopen);
115
1.30k
    return file;
116
1.30k
}
117
118
119
0
static uLong ZCALLBACK fread_file_func(voidpf opaque, voidpf stream, void* buf, uLong size) {
120
0
    uLong ret;
121
0
    (void)opaque;
122
0
    ret = (uLong)fread(buf, 1, (size_t)size, (FILE *)stream);
123
0
    return ret;
124
0
}
125
126
206k
static uLong ZCALLBACK fwrite_file_func(voidpf opaque, voidpf stream, const void* buf, uLong size) {
127
206k
    uLong ret;
128
206k
    (void)opaque;
129
206k
    ret = (uLong)fwrite(buf, 1, (size_t)size, (FILE *)stream);
130
206k
    return ret;
131
206k
}
132
133
0
static long ZCALLBACK ftell_file_func(voidpf opaque, voidpf stream) {
134
0
    long ret;
135
0
    (void)opaque;
136
0
    ret = ftell((FILE *)stream);
137
0
    return ret;
138
0
}
139
140
141
28.5k
static ZPOS64_T ZCALLBACK ftell64_file_func(voidpf opaque, voidpf stream) {
142
28.5k
    ZPOS64_T ret;
143
28.5k
    (void)opaque;
144
28.5k
    ret = (ZPOS64_T)FTELLO_FUNC((FILE *)stream);
145
28.5k
    return ret;
146
28.5k
}
147
148
0
static long ZCALLBACK fseek_file_func(voidpf opaque, voidpf stream, uLong offset, int origin) {
149
0
    int fseek_origin=0;
150
0
    long ret;
151
0
    (void)opaque;
152
0
    switch (origin)
153
0
    {
154
0
    case ZLIB_FILEFUNC_SEEK_CUR :
155
0
        fseek_origin = SEEK_CUR;
156
0
        break;
157
0
    case ZLIB_FILEFUNC_SEEK_END :
158
0
        fseek_origin = SEEK_END;
159
0
        break;
160
0
    case ZLIB_FILEFUNC_SEEK_SET :
161
0
        fseek_origin = SEEK_SET;
162
0
        break;
163
0
    default: return -1;
164
0
    }
165
0
    ret = 0;
166
0
    if (fseek((FILE *)stream, (long)offset, fseek_origin) != 0)
167
0
        ret = -1;
168
0
    return ret;
169
0
}
170
171
25.9k
static long ZCALLBACK fseek64_file_func(voidpf opaque, voidpf stream, ZPOS64_T offset, int origin) {
172
25.9k
    int fseek_origin=0;
173
25.9k
    long ret;
174
25.9k
    (void)opaque;
175
25.9k
    switch (origin)
176
25.9k
    {
177
0
    case ZLIB_FILEFUNC_SEEK_CUR :
178
0
        fseek_origin = SEEK_CUR;
179
0
        break;
180
0
    case ZLIB_FILEFUNC_SEEK_END :
181
0
        fseek_origin = SEEK_END;
182
0
        break;
183
25.9k
    case ZLIB_FILEFUNC_SEEK_SET :
184
25.9k
        fseek_origin = SEEK_SET;
185
25.9k
        break;
186
0
    default: return -1;
187
25.9k
    }
188
25.9k
    ret = 0;
189
190
25.9k
    if(FSEEKO_FUNC((FILE *)stream, (z_off64_t)offset, fseek_origin) != 0)
191
0
                        ret = -1;
192
193
25.9k
    return ret;
194
25.9k
}
195
196
197
1.30k
static int ZCALLBACK fclose_file_func(voidpf opaque, voidpf stream) {
198
1.30k
    int ret;
199
1.30k
    (void)opaque;
200
1.30k
    ret = fclose((FILE *)stream);
201
1.30k
    return ret;
202
1.30k
}
203
204
0
static int ZCALLBACK ferror_file_func(voidpf opaque, voidpf stream) {
205
0
    int ret;
206
0
    (void)opaque;
207
0
    ret = ferror((FILE *)stream);
208
0
    return ret;
209
0
}
210
211
0
void fill_fopen_filefunc(zlib_filefunc_def* pzlib_filefunc_def) {
212
0
    pzlib_filefunc_def->zopen_file = fopen_file_func;
213
0
    pzlib_filefunc_def->zread_file = fread_file_func;
214
0
    pzlib_filefunc_def->zwrite_file = fwrite_file_func;
215
0
    pzlib_filefunc_def->ztell_file = ftell_file_func;
216
0
    pzlib_filefunc_def->zseek_file = fseek_file_func;
217
0
    pzlib_filefunc_def->zclose_file = fclose_file_func;
218
0
    pzlib_filefunc_def->zerror_file = ferror_file_func;
219
0
    pzlib_filefunc_def->opaque = NULL;
220
0
}
221
222
1.30k
void fill_fopen64_filefunc(zlib_filefunc64_def* pzlib_filefunc_def) {
223
1.30k
    pzlib_filefunc_def->zopen64_file = fopen64_file_func;
224
1.30k
    pzlib_filefunc_def->zread_file = fread_file_func;
225
1.30k
    pzlib_filefunc_def->zwrite_file = fwrite_file_func;
226
1.30k
    pzlib_filefunc_def->ztell64_file = ftell64_file_func;
227
1.30k
    pzlib_filefunc_def->zseek64_file = fseek64_file_func;
228
1.30k
    pzlib_filefunc_def->zclose_file = fclose_file_func;
229
1.30k
    pzlib_filefunc_def->zerror_file = ferror_file_func;
230
1.30k
    pzlib_filefunc_def->opaque = NULL;
231
1.30k
}