Ruby: ext/zlib/zlib-1.2.13/contrib/minizip/ioapi.c Source File

Ruby 3.0.5p211 (2022-11-24 revision ba5cf0f7c52d4d35cc6a173c89eda98ceffa2dcf)

ioapi.c
Go to the documentation of this file.
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)
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
28
29#include "ioapi.h"
30
31 voidpf call_zopen64 (const zlib_filefunc64_32_def* pfilefunc,const void*filename,int mode)
32{
33 if (pfilefunc->zfile_func64.zopen64_file != NULL)
34 return (*(pfilefunc->zfile_func64.zopen64_file)) (pfilefunc->zfile_func64.opaque,filename,mode);
35 else
36 {
37 return (*(pfilefunc->zopen32_file))(pfilefunc->zfile_func64.opaque,(const char*)filename,mode);
38 }
39}
40
41 long call_zseek64 (const zlib_filefunc64_32_def* pfilefunc,voidpf filestream, ZPOS64_T offset, int origin)
42{
43 if (pfilefunc->zfile_func64.zseek64_file != NULL)
44 return (*(pfilefunc->zfile_func64.zseek64_file)) (pfilefunc->zfile_func64.opaque,filestream,offset,origin);
45 else
46 {
47 uLong offsetTruncated = (uLong)offset;
48 if (offsetTruncated != offset)
49 return -1;
50 else
51 return (*(pfilefunc->zseek32_file))(pfilefunc->zfile_func64.opaque,filestream,offsetTruncated,origin);
52 }
53}
54
55 ZPOS64_T call_ztell64 (const zlib_filefunc64_32_def* pfilefunc,voidpf filestream)
56{
57 if (pfilefunc->zfile_func64.zseek64_file != NULL)
58 return (*(pfilefunc->zfile_func64.ztell64_file)) (pfilefunc->zfile_func64.opaque,filestream);
59 else
60 {
61 uLong tell_uLong = (uLong)(*(pfilefunc->ztell32_file))(pfilefunc->zfile_func64.opaque,filestream);
62 if ((tell_uLong) == MAXU32)
63 return (ZPOS64_T)-1;
64 else
65 return tell_uLong;
66 }
67}
68
69 void fill_zlib_filefunc64_32_def_from_filefunc32(zlib_filefunc64_32_def* p_filefunc64_32,const zlib_filefunc_def* p_filefunc32)
70{
71 p_filefunc64_32->zfile_func64.zopen64_file = NULL;
72 p_filefunc64_32->zopen32_file = p_filefunc32->zopen_file;
73 p_filefunc64_32->zfile_func64.zerror_file = p_filefunc32->zerror_file;
74 p_filefunc64_32->zfile_func64.zread_file = p_filefunc32->zread_file;
75 p_filefunc64_32->zfile_func64.zwrite_file = p_filefunc32->zwrite_file;
76 p_filefunc64_32->zfile_func64.ztell64_file = NULL;
77 p_filefunc64_32->zfile_func64.zseek64_file = NULL;
78 p_filefunc64_32->zfile_func64.zclose_file = p_filefunc32->zclose_file;
79 p_filefunc64_32->zfile_func64.zerror_file = p_filefunc32->zerror_file;
80 p_filefunc64_32->zfile_func64.opaque = p_filefunc32->opaque;
81 p_filefunc64_32->zseek32_file = p_filefunc32->zseek_file;
82 p_filefunc64_32->ztell32_file = p_filefunc32->ztell_file;
83}
84
85
86
87static voidpf ZCALLBACK fopen_file_func OF((voidpf opaque, const char* filename, int mode));
88static uLong ZCALLBACK fread_file_func OF((voidpf opaque, voidpf stream, void* buf, uLong size));
89static uLong ZCALLBACK fwrite_file_func OF((voidpf opaque, voidpf stream, const void* buf,uLong size));
90static ZPOS64_T ZCALLBACK ftell64_file_func OF((voidpf opaque, voidpf stream));
91static long ZCALLBACK fseek64_file_func OF((voidpf opaque, voidpf stream, ZPOS64_T offset, int origin));
92static int ZCALLBACK fclose_file_func OF((voidpf opaque, voidpf stream));
93static int ZCALLBACK ferror_file_func OF((voidpf opaque, voidpf stream));
94
95static voidpf ZCALLBACK fopen_file_func (voidpf opaque, const char* filename, int mode)
96{
97 FILE* file = NULL;
98 const char* mode_fopen = NULL;
99 (void)opaque;
100 if ((mode & ZLIB_FILEFUNC_MODE_READWRITEFILTER)==ZLIB_FILEFUNC_MODE_READ)
101 mode_fopen = "rb";
102 else
103 if (mode & ZLIB_FILEFUNC_MODE_EXISTING)
104 mode_fopen = "r+b";
105 else
106 if (mode & ZLIB_FILEFUNC_MODE_CREATE)
107 mode_fopen = "wb";
108
109 if ((filename!=NULL) && (mode_fopen != NULL))
110 file = fopen(filename, mode_fopen);
111 return file;
112}
113
114static voidpf ZCALLBACK fopen64_file_func (voidpf opaque, const void* filename, int mode)
115{
116 FILE* file = NULL;
117 const char* mode_fopen = NULL;
118 (void)opaque;
119 if ((mode & ZLIB_FILEFUNC_MODE_READWRITEFILTER)==ZLIB_FILEFUNC_MODE_READ)
120 mode_fopen = "rb";
121 else
122 if (mode & ZLIB_FILEFUNC_MODE_EXISTING)
123 mode_fopen = "r+b";
124 else
125 if (mode & ZLIB_FILEFUNC_MODE_CREATE)
126 mode_fopen = "wb";
127
128 if ((filename!=NULL) && (mode_fopen != NULL))
129 file = FOPEN_FUNC((const char*)filename, mode_fopen);
130 return file;
131}
132
133
134static uLong ZCALLBACK fread_file_func (voidpf opaque, voidpf stream, void* buf, uLong size)
135{
136 uLong ret;
137 (void)opaque;
138 ret = (uLong)fread(buf, 1, (size_t)size, (FILE *)stream);
139 return ret;
140}
141
142static uLong ZCALLBACK fwrite_file_func (voidpf opaque, voidpf stream, const void* buf, uLong size)
143{
144 uLong ret;
145 (void)opaque;
146 ret = (uLong)fwrite(buf, 1, (size_t)size, (FILE *)stream);
147 return ret;
148}
149
150static long ZCALLBACK ftell_file_func (voidpf opaque, voidpf stream)
151{
152 long ret;
153 (void)opaque;
154 ret = ftell((FILE *)stream);
155 return ret;
156}
157
158
159static ZPOS64_T ZCALLBACK ftell64_file_func (voidpf opaque, voidpf stream)
160{
161 ZPOS64_T ret;
162 (void)opaque;
163 ret = (ZPOS64_T)FTELLO_FUNC((FILE *)stream);
164 return ret;
165}
166
167static long ZCALLBACK fseek_file_func (voidpf opaque, voidpf stream, uLong offset, int origin)
168{
169 int fseek_origin=0;
170 long ret;
171 (void)opaque;
172 switch (origin)
173 {
174 case ZLIB_FILEFUNC_SEEK_CUR :
175 fseek_origin = SEEK_CUR;
176 break;
177 case ZLIB_FILEFUNC_SEEK_END :
178 fseek_origin = SEEK_END;
179 break;
180 case ZLIB_FILEFUNC_SEEK_SET :
181 fseek_origin = SEEK_SET;
182 break;
183 default: return -1;
184 }
185 ret = 0;
186 if (fseek((FILE *)stream, (long)offset, fseek_origin) != 0)
187 ret = -1;
188 return ret;
189}
190
191static long ZCALLBACK fseek64_file_func (voidpf opaque, voidpf stream, ZPOS64_T offset, int origin)
192{
193 int fseek_origin=0;
194 long ret;
195 (void)opaque;
196 switch (origin)
197 {
198 case ZLIB_FILEFUNC_SEEK_CUR :
199 fseek_origin = SEEK_CUR;
200 break;
201 case ZLIB_FILEFUNC_SEEK_END :
202 fseek_origin = SEEK_END;
203 break;
204 case ZLIB_FILEFUNC_SEEK_SET :
205 fseek_origin = SEEK_SET;
206 break;
207 default: return -1;
208 }
209 ret = 0;
210
211 if(FSEEKO_FUNC((FILE *)stream, (z_off_t)offset, fseek_origin) != 0)
212 ret = -1;
213
214 return ret;
215}
216
217
218static int ZCALLBACK fclose_file_func (voidpf opaque, voidpf stream)
219{
220 int ret;
221 (void)opaque;
222 ret = fclose((FILE *)stream);
223 return ret;
224}
225
226static int ZCALLBACK ferror_file_func (voidpf opaque, voidpf stream)
227{
228 int ret;
229 (void)opaque;
230 ret = ferror((FILE *)stream);
231 return ret;
232}
233
234 void fill_fopen_filefunc (pzlib_filefunc_def)
235 zlib_filefunc_def* pzlib_filefunc_def;
236{
237 pzlib_filefunc_def->zopen_file = fopen_file_func;
238 pzlib_filefunc_def->zread_file = fread_file_func;
239 pzlib_filefunc_def->zwrite_file = fwrite_file_func;
240 pzlib_filefunc_def->ztell_file = ftell_file_func;
241 pzlib_filefunc_def->zseek_file = fseek_file_func;
242 pzlib_filefunc_def->zclose_file = fclose_file_func;
243 pzlib_filefunc_def->zerror_file = ferror_file_func;
244 pzlib_filefunc_def->opaque = NULL;
245}
246
247 void fill_fopen64_filefunc (zlib_filefunc64_def* pzlib_filefunc_def)
248{
249 pzlib_filefunc_def->zopen64_file = fopen64_file_func;
250 pzlib_filefunc_def->zread_file = fread_file_func;
251 pzlib_filefunc_def->zwrite_file = fwrite_file_func;
252 pzlib_filefunc_def->ztell64_file = ftell64_file_func;
253 pzlib_filefunc_def->zseek64_file = fseek64_file_func;
254 pzlib_filefunc_def->zclose_file = fclose_file_func;
255 pzlib_filefunc_def->zerror_file = ferror_file_func;
256 pzlib_filefunc_def->opaque = NULL;
257}
fill_zlib_filefunc64_32_def_from_filefunc32
void fill_zlib_filefunc64_32_def_from_filefunc32(zlib_filefunc64_32_def *p_filefunc64_32, const zlib_filefunc_def *p_filefunc32)
Definition: ioapi.c:69
FSEEKO_FUNC
#define FSEEKO_FUNC(stream, offset, origin)
Definition: ioapi.c:25
FOPEN_FUNC
#define FOPEN_FUNC(filename, mode)
Definition: ioapi.c:23
call_zseek64
long call_zseek64(const zlib_filefunc64_32_def *pfilefunc, voidpf filestream, ZPOS64_T offset, int origin)
Definition: ioapi.c:41
fill_fopen64_filefunc
void fill_fopen64_filefunc(zlib_filefunc64_def *pzlib_filefunc_def)
Definition: ioapi.c:247
fill_fopen_filefunc
void fill_fopen_filefunc(zlib_filefunc_def *pzlib_filefunc_def)
Definition: ioapi.c:234
call_zopen64
voidpf call_zopen64(const zlib_filefunc64_32_def *pfilefunc, const void *filename, int mode)
Definition: ioapi.c:31
call_ztell64
ZPOS64_T call_ztell64(const zlib_filefunc64_32_def *pfilefunc, voidpf filestream)
Definition: ioapi.c:55
FTELLO_FUNC
#define FTELLO_FUNC(stream)
Definition: ioapi.c:24
ioapi.h
size
voidpf void uLong size
Definition: ioapi.h:138
filename
const char * filename
Definition: ioapi.h:137
ZLIB_FILEFUNC_MODE_READWRITEFILTER
#define ZLIB_FILEFUNC_MODE_READWRITEFILTER
Definition: ioapi.h:120
stream
voidpf stream
Definition: ioapi.h:138
ZPOS64_T
unsigned __int64 ZPOS64_T
Definition: ioapi.h:97
origin
voidpf uLong int origin
Definition: ioapi.h:144
offset
voidpf uLong offset
Definition: ioapi.h:144
ZLIB_FILEFUNC_MODE_EXISTING
#define ZLIB_FILEFUNC_MODE_EXISTING
Definition: ioapi.h:122
ZLIB_FILEFUNC_SEEK_CUR
#define ZLIB_FILEFUNC_SEEK_CUR
Definition: ioapi.h:114
mode
const char int mode
Definition: ioapi.h:137
buf
voidpf void * buf
Definition: ioapi.h:138
ZLIB_FILEFUNC_SEEK_SET
#define ZLIB_FILEFUNC_SEEK_SET
Definition: ioapi.h:116
ZCALLBACK
#define ZCALLBACK
Definition: ioapi.h:130
MAXU32
#define MAXU32
Definition: ioapi.h:106
ZLIB_FILEFUNC_MODE_CREATE
#define ZLIB_FILEFUNC_MODE_CREATE
Definition: ioapi.h:123
ZLIB_FILEFUNC_SEEK_END
#define ZLIB_FILEFUNC_SEEK_END
Definition: ioapi.h:115
ZLIB_FILEFUNC_MODE_READ
#define ZLIB_FILEFUNC_MODE_READ
Definition: ioapi.h:118
NULL
#define NULL
Definition: regenc.h:69
__sFILE
Definition: vsnprintf.c:169
file
Definition: gzappend.c:170
zlib_filefunc64_32_def_s
Definition: ioapi.h:181
zlib_filefunc64_32_def_s::zseek32_file
seek_file_func zseek32_file
Definition: ioapi.h:185
zlib_filefunc64_32_def_s::zfile_func64
zlib_filefunc64_def zfile_func64
Definition: ioapi.h:182
zlib_filefunc64_32_def_s::zopen32_file
open_file_func zopen32_file
Definition: ioapi.h:183
zlib_filefunc64_32_def_s::ztell32_file
tell_file_func ztell32_file
Definition: ioapi.h:184
zlib_filefunc64_def_s
Definition: ioapi.h:165
zlib_filefunc64_def_s::zwrite_file
write_file_func zwrite_file
Definition: ioapi.h:168
zlib_filefunc64_def_s::zopen64_file
open64_file_func zopen64_file
Definition: ioapi.h:166
zlib_filefunc64_def_s::opaque
voidpf opaque
Definition: ioapi.h:173
zlib_filefunc64_def_s::zread_file
read_file_func zread_file
Definition: ioapi.h:167
zlib_filefunc64_def_s::ztell64_file
tell64_file_func ztell64_file
Definition: ioapi.h:169
zlib_filefunc64_def_s::zclose_file
close_file_func zclose_file
Definition: ioapi.h:171
zlib_filefunc64_def_s::zseek64_file
seek64_file_func zseek64_file
Definition: ioapi.h:170
zlib_filefunc64_def_s::zerror_file
testerror_file_func zerror_file
Definition: ioapi.h:172
zlib_filefunc_def_s
Definition: ioapi.h:149
zlib_filefunc_def_s::zseek_file
seek_file_func zseek_file
Definition: ioapi.h:154
zlib_filefunc_def_s::opaque
voidpf opaque
Definition: ioapi.h:157
zlib_filefunc_def_s::zopen_file
open_file_func zopen_file
Definition: ioapi.h:150
zlib_filefunc_def_s::zerror_file
testerror_file_func zerror_file
Definition: ioapi.h:156
zlib_filefunc_def_s::zwrite_file
write_file_func zwrite_file
Definition: ioapi.h:152
zlib_filefunc_def_s::zread_file
read_file_func zread_file
Definition: ioapi.h:151
zlib_filefunc_def_s::zclose_file
close_file_func zclose_file
Definition: ioapi.h:155
zlib_filefunc_def_s::ztell_file
tell_file_func ztell_file
Definition: ioapi.h:153
ferror
#define ferror(p)
Definition: vsnprintf.c:215
uLong
unsigned long uLong
Definition: zconf.h:400
voidpf
void FAR * voidpf
Definition: zconf.h:415
z_off_t
#define z_off_t
Definition: zconf.h:517
OF
#define OF(args)
Definition: zconf.h:293
SEEK_SET
#define SEEK_SET
Definition: zip.c:88
SEEK_CUR
#define SEEK_CUR
Definition: zip.c:80
SEEK_END
#define SEEK_END
Definition: zip.c:84