1
- /* ioapi.c -- IO base function header for compress/uncompress .zip
2
- files using zlib + zip or unzip API
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
3
4
- Version 1.01e, February 12th, 2005
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
5
10
6
- Copyright (C) 1998-2005 Gilles Vollant
7
11
*/
8
12
9
- #include <stdio.h>
10
- #include <stdlib.h>
11
- #include <string.h>
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
+ #define FOPEN_FUNC (filename , mode ) fopen64(filename, mode)
24
+ #define FTELLO_FUNC (stream ) ftello64(stream)
25
+ #define FSEEKO_FUNC (stream , offset , origin ) fseeko64(stream, offset, origin)
26
+ #endif
27
+
12
28
13
- #include "zlib.h"
14
29
#include "ioapi.h"
15
30
31
+ voidpf call_zopen64 (const zlib_filefunc64_32_def * pfilefunc , const void * filename , int mode ) {
32
+ if (pfilefunc -> zfile_func64 .zopen64_file != NULL )
33
+ return (* (pfilefunc -> zfile_func64 .zopen64_file ))(pfilefunc -> zfile_func64 .opaque , filename , mode );
34
+ else {
35
+ return (* (pfilefunc -> zopen32_file ))(pfilefunc -> zfile_func64 .opaque , (const char * )filename , mode );
36
+ }
37
+ }
16
38
39
+ long call_zseek64 (const zlib_filefunc64_32_def * pfilefunc , voidpf filestream , ZPOS64_T offset , int origin ) {
40
+ if (pfilefunc -> zfile_func64 .zseek64_file != NULL )
41
+ return (* (pfilefunc -> zfile_func64 .zseek64_file ))(pfilefunc -> zfile_func64 .opaque , filestream , offset , origin );
42
+ else {
43
+ uLong offsetTruncated = (uLong )offset ;
44
+ if (offsetTruncated != offset )
45
+ return -1 ;
46
+ else
47
+ return (* (pfilefunc -> zseek32_file ))(pfilefunc -> zfile_func64 .opaque , filestream , offsetTruncated , origin );
48
+ }
49
+ }
17
50
18
- /* I've found an old Unix (a SunOS 4.1.3_U1) without all SEEK_* defined.... */
51
+ ZPOS64_T call_ztell64 (const zlib_filefunc64_32_def * pfilefunc , voidpf filestream ) {
52
+ if (pfilefunc -> zfile_func64 .zseek64_file != NULL )
53
+ return (* (pfilefunc -> zfile_func64 .ztell64_file ))(pfilefunc -> zfile_func64 .opaque , filestream );
54
+ else {
55
+ uLong tell_uLong = (uLong )(* (pfilefunc -> ztell32_file ))(pfilefunc -> zfile_func64 .opaque , filestream );
56
+ if ((tell_uLong ) == MAXU32 )
57
+ return (ZPOS64_T )- 1 ;
58
+ else
59
+ return tell_uLong ;
60
+ }
61
+ }
19
62
20
- #ifndef SEEK_CUR
21
- #define SEEK_CUR 1
22
- #endif
63
+ void fill_zlib_filefunc64_32_def_from_filefunc32 (zlib_filefunc64_32_def * p_filefunc64_32 ,
64
+ const zlib_filefunc_def * p_filefunc32 ) {
65
+ p_filefunc64_32 -> zfile_func64 .zopen64_file = NULL ;
66
+ p_filefunc64_32 -> zopen32_file = p_filefunc32 -> zopen_file ;
67
+ p_filefunc64_32 -> zfile_func64 .zread_file = p_filefunc32 -> zread_file ;
68
+ p_filefunc64_32 -> zfile_func64 .zwrite_file = p_filefunc32 -> zwrite_file ;
69
+ p_filefunc64_32 -> zfile_func64 .ztell64_file = NULL ;
70
+ p_filefunc64_32 -> zfile_func64 .zseek64_file = NULL ;
71
+ p_filefunc64_32 -> zfile_func64 .zclose_file = p_filefunc32 -> zclose_file ;
72
+ p_filefunc64_32 -> zfile_func64 .zerror_file = p_filefunc32 -> zerror_file ;
73
+ p_filefunc64_32 -> zfile_func64 .opaque = p_filefunc32 -> opaque ;
74
+ p_filefunc64_32 -> zseek32_file = p_filefunc32 -> zseek_file ;
75
+ p_filefunc64_32 -> ztell32_file = p_filefunc32 -> ztell_file ;
76
+ }
23
77
24
- #ifndef SEEK_END
25
- #define SEEK_END 2
26
- #endif
27
78
28
- #ifndef SEEK_SET
29
- #define SEEK_SET 0
30
- #endif
79
+ static voidpf ZCALLBACK fopen_file_func (voidpf opaque , const char * filename , int mode ) {
80
+ FILE * file = NULL ;
81
+ const char * mode_fopen = NULL ;
82
+ (void )opaque ;
83
+ if ((mode & ZLIB_FILEFUNC_MODE_READWRITEFILTER ) == ZLIB_FILEFUNC_MODE_READ )
84
+ mode_fopen = "rb" ;
85
+ else if (mode & ZLIB_FILEFUNC_MODE_EXISTING )
86
+ mode_fopen = "r+b" ;
87
+ else if (mode & ZLIB_FILEFUNC_MODE_CREATE )
88
+ mode_fopen = "wb" ;
31
89
32
- voidpf ZCALLBACK fopen_file_func OF ( (
33
- voidpf opaque ,
34
- const char * filename ,
35
- int mode ));
36
-
37
- uLong ZCALLBACK fread_file_func OF ( (
38
- voidpf opaque ,
39
- voidpf stream ,
40
- void * buf ,
41
- uLong size ));
42
-
43
- uLong ZCALLBACK fwrite_file_func OF ( (
44
- voidpf opaque ,
45
- voidpf stream ,
46
- const void * buf ,
47
- uLong size ));
48
-
49
- long ZCALLBACK ftell_file_func OF ((
50
- voidpf opaque ,
51
- voidpf stream ));
52
-
53
- long ZCALLBACK fseek_file_func OF ((
54
- voidpf opaque ,
55
- voidpf stream ,
56
- uLong offset ,
57
- int origin ));
58
-
59
- int ZCALLBACK fclose_file_func OF ( (
60
- voidpf opaque ,
61
- voidpf stream ));
62
-
63
- int ZCALLBACK ferror_file_func OF ( (
64
- voidpf opaque ,
65
- voidpf stream ));
66
-
67
-
68
- voidpf ZCALLBACK fopen_file_func (opaque , filename , mode )
69
- voidpf opaque ;
70
- const char * filename ;
71
- int mode ;
72
- {
90
+ if ((filename != NULL ) && (mode_fopen != NULL ))
91
+ file = fopen (filename , mode_fopen );
92
+ return file ;
93
+ }
94
+
95
+ static voidpf ZCALLBACK fopen64_file_func (voidpf opaque , const void * filename , int mode ) {
73
96
FILE * file = NULL ;
74
97
const char * mode_fopen = NULL ;
98
+ (void )opaque ;
75
99
if ((mode & ZLIB_FILEFUNC_MODE_READWRITEFILTER )== ZLIB_FILEFUNC_MODE_READ )
76
100
mode_fopen = "rb" ;
77
101
else
@@ -82,51 +106,67 @@ voidpf ZCALLBACK fopen_file_func (opaque, filename, mode)
82
106
mode_fopen = "wb" ;
83
107
84
108
if ((filename != NULL ) && (mode_fopen != NULL ))
85
- file = fopen ( filename , mode_fopen );
109
+ file = FOPEN_FUNC (( const char * ) filename , mode_fopen );
86
110
return file ;
87
111
}
88
112
89
113
90
- uLong ZCALLBACK fread_file_func (opaque , stream , buf , size )
91
- voidpf opaque ;
92
- voidpf stream ;
93
- void * buf ;
94
- uLong size ;
95
- {
114
+ static uLong ZCALLBACK fread_file_func (voidpf opaque , voidpf stream , void * buf , uLong size ) {
96
115
uLong ret ;
116
+ (void )opaque ;
97
117
ret = (uLong )fread (buf , 1 , (size_t )size , (FILE * )stream );
98
118
return ret ;
99
119
}
100
120
101
-
102
- uLong ZCALLBACK fwrite_file_func (opaque , stream , buf , size )
103
- voidpf opaque ;
104
- voidpf stream ;
105
- const void * buf ;
106
- uLong size ;
107
- {
121
+ static uLong ZCALLBACK fwrite_file_func (voidpf opaque , voidpf stream , const void * buf , uLong size ) {
108
122
uLong ret ;
123
+ (void )opaque ;
109
124
ret = (uLong )fwrite (buf , 1 , (size_t )size , (FILE * )stream );
110
125
return ret ;
111
126
}
112
127
113
- long ZCALLBACK ftell_file_func (opaque , stream )
114
- voidpf opaque ;
115
- voidpf stream ;
116
- {
128
+ static long ZCALLBACK ftell_file_func (voidpf opaque , voidpf stream ) {
117
129
long ret ;
130
+ (void )opaque ;
118
131
ret = ftell ((FILE * )stream );
119
132
return ret ;
120
133
}
121
134
122
- long ZCALLBACK fseek_file_func (opaque , stream , offset , origin )
123
- voidpf opaque ;
124
- voidpf stream ;
125
- uLong offset ;
126
- int origin ;
127
- {
135
+
136
+ static ZPOS64_T ZCALLBACK ftell64_file_func (voidpf opaque , voidpf stream ) {
137
+ ZPOS64_T ret ;
138
+ (void )opaque ;
139
+ ret = (ZPOS64_T )FTELLO_FUNC ((FILE * )stream );
140
+ return ret ;
141
+ }
142
+
143
+ static long ZCALLBACK fseek_file_func (voidpf opaque , voidpf stream , uLong offset , int origin ) {
144
+ int fseek_origin = 0 ;
145
+ long ret ;
146
+ (void )opaque ;
147
+ switch (origin ) {
148
+ case ZLIB_FILEFUNC_SEEK_CUR :
149
+ fseek_origin = SEEK_CUR ;
150
+ break ;
151
+ case ZLIB_FILEFUNC_SEEK_END :
152
+ fseek_origin = SEEK_END ;
153
+ break ;
154
+ case ZLIB_FILEFUNC_SEEK_SET :
155
+ fseek_origin = SEEK_SET ;
156
+ break ;
157
+ default :
158
+ return -1 ;
159
+ }
160
+ ret = 0 ;
161
+ if (fseek ((FILE * )stream , (long )offset , fseek_origin ) != 0 )
162
+ ret = -1 ;
163
+ return ret ;
164
+ }
165
+
166
+ static long ZCALLBACK fseek64_file_func (voidpf opaque , voidpf stream , ZPOS64_T offset , int origin ) {
128
167
int fseek_origin = 0 ;
129
168
long ret ;
169
+ (void )opaque ;
130
170
switch (origin )
131
171
{
132
172
case ZLIB_FILEFUNC_SEEK_CUR :
@@ -141,31 +181,29 @@ long ZCALLBACK fseek_file_func (opaque, stream, offset, origin)
141
181
default : return -1 ;
142
182
}
143
183
ret = 0 ;
144
- fseek ((FILE * )stream , offset , fseek_origin );
184
+
185
+ if (FSEEKO_FUNC ((FILE * )stream , (z_off64_t )offset , fseek_origin ) != 0 )
186
+ ret = -1 ;
187
+
145
188
return ret ;
146
189
}
147
190
148
- int ZCALLBACK fclose_file_func (opaque , stream )
149
- voidpf opaque ;
150
- voidpf stream ;
151
- {
191
+
192
+ static int ZCALLBACK fclose_file_func (voidpf opaque , voidpf stream ) {
152
193
int ret ;
194
+ (void )opaque ;
153
195
ret = fclose ((FILE * )stream );
154
196
return ret ;
155
197
}
156
198
157
- int ZCALLBACK ferror_file_func (opaque , stream )
158
- voidpf opaque ;
159
- voidpf stream ;
160
- {
199
+ static int ZCALLBACK ferror_file_func (voidpf opaque , voidpf stream ) {
161
200
int ret ;
201
+ (void )opaque ;
162
202
ret = ferror ((FILE * )stream );
163
203
return ret ;
164
204
}
165
205
166
- void fill_fopen_filefunc (pzlib_filefunc_def )
167
- zlib_filefunc_def * pzlib_filefunc_def ;
168
- {
206
+ void fill_fopen_filefunc (zlib_filefunc_def * pzlib_filefunc_def ) {
169
207
pzlib_filefunc_def -> zopen_file = fopen_file_func ;
170
208
pzlib_filefunc_def -> zread_file = fread_file_func ;
171
209
pzlib_filefunc_def -> zwrite_file = fwrite_file_func ;
@@ -175,3 +213,14 @@ void fill_fopen_filefunc (pzlib_filefunc_def)
175
213
pzlib_filefunc_def -> zerror_file = ferror_file_func ;
176
214
pzlib_filefunc_def -> opaque = NULL ;
177
215
}
216
+
217
+ void fill_fopen64_filefunc (zlib_filefunc64_def * pzlib_filefunc_def ) {
218
+ pzlib_filefunc_def -> zopen64_file = fopen64_file_func ;
219
+ pzlib_filefunc_def -> zread_file = fread_file_func ;
220
+ pzlib_filefunc_def -> zwrite_file = fwrite_file_func ;
221
+ pzlib_filefunc_def -> ztell64_file = ftell64_file_func ;
222
+ pzlib_filefunc_def -> zseek64_file = fseek64_file_func ;
223
+ pzlib_filefunc_def -> zclose_file = fclose_file_func ;
224
+ pzlib_filefunc_def -> zerror_file = ferror_file_func ;
225
+ pzlib_filefunc_def -> opaque = NULL ;
226
+ }
0 commit comments