Debian Codegraph @

compat/ioapi.c

raw symbols SCIP
1 #include "mz.h"
2 #include "mz_strm.h"
3 #include "mz_strm_mem.h"
4
5 #include "ioapi.h"
6
7 typedef struct mz_stream_ioapi_s {
8 mz_stream stream;
9 void *handle;
10 zlib_filefunc_def filefunc;
11 zlib_filefunc64_def filefunc64;
12 } mz_stream_ioapi;
13
14 /***************************************************************************/
15
16 static int32_t mz_stream_ioapi_open(void *stream, const char *path, int32_t mode);
17 static int32_t mz_stream_ioapi_is_open(void *stream);
18 static int32_t mz_stream_ioapi_read(void *stream, void *buf, int32_t size);
19 static int32_t mz_stream_ioapi_write(void *stream, const void *buf, int32_t size);
20 static int64_t mz_stream_ioapi_tell(void *stream);
21 static int32_t mz_stream_ioapi_seek(void *stream, int64_t offset, int32_t origin);
22 static int32_t mz_stream_ioapi_close(void *stream);
23 static int32_t mz_stream_ioapi_error(void *stream);
24
25 /***************************************************************************/
26
27 static mz_stream_vtbl mz_stream_ioapi_vtbl = {mz_stream_ioapi_open,
28 mz_stream_ioapi_is_open,
29 mz_stream_ioapi_read,
30 mz_stream_ioapi_write,
31 mz_stream_ioapi_tell,
32 mz_stream_ioapi_seek,
33 mz_stream_ioapi_close,
34 mz_stream_ioapi_error,
35 mz_stream_ioapi_create,
36 mz_stream_ioapi_delete,
37 NULL,
38 NULL};
39
40 /***************************************************************************/
41
42 static int32_t mz_stream_ioapi_open(void *stream, const char *path, int32_t mode) {
43 mz_stream_ioapi *ioapi = (mz_stream_ioapi *)stream;
44 int32_t ioapi_mode = 0;
45
46 if ((mode & MZ_OPEN_MODE_READWRITE) == MZ_OPEN_MODE_READ)
47 ioapi_mode = ZLIB_FILEFUNC_MODE_READ;
48 else if (mode & MZ_OPEN_MODE_APPEND)
49 ioapi_mode = ZLIB_FILEFUNC_MODE_EXISTING;
50 else if (mode & MZ_OPEN_MODE_CREATE)
51 ioapi_mode = ZLIB_FILEFUNC_MODE_CREATE;
52 else
53 return MZ_OPEN_ERROR;
54
55 if (ioapi->filefunc64.zopen64_file)
56 ioapi->handle = ioapi->filefunc64.zopen64_file(ioapi->filefunc64.opaque, path, ioapi_mode);
57 else if (ioapi->filefunc.zopen_file)
58 ioapi->handle = ioapi->filefunc.zopen_file(ioapi->filefunc.opaque, path, ioapi_mode);
59
60 if (!ioapi->handle)
61 return MZ_PARAM_ERROR;
62
63 return MZ_OK;
64 }
65
66 static int32_t mz_stream_ioapi_is_open(void *stream) {
67 mz_stream_ioapi *ioapi = (mz_stream_ioapi *)stream;
68 if (!ioapi->handle)
69 return MZ_OPEN_ERROR;
70 return MZ_OK;
71 }
72
73 static int32_t mz_stream_ioapi_read(void *stream, void *buf, int32_t size) {
74 mz_stream_ioapi *ioapi = (mz_stream_ioapi *)stream;
75 read_file_func zread = NULL;
76 void *opaque = NULL;
77
78 if (mz_stream_ioapi_is_open(stream) != MZ_OK)
79 return MZ_OPEN_ERROR;
80
81 if (ioapi->filefunc64.zread_file) {
82 zread = ioapi->filefunc64.zread_file;
83 opaque = ioapi->filefunc64.opaque;
84 } else if (ioapi->filefunc.zread_file) {
85 zread = ioapi->filefunc.zread_file;
86 opaque = ioapi->filefunc.opaque;
87 } else
88 return MZ_PARAM_ERROR;
89
90 return (int32_t)zread(opaque, ioapi->handle, buf, size);
91 }
92
93 static int32_t mz_stream_ioapi_write(void *stream, const void *buf, int32_t size) {
94 mz_stream_ioapi *ioapi = (mz_stream_ioapi *)stream;
95 write_file_func zwrite = NULL;
96 int32_t written = 0;
97 void *opaque = NULL;
98
99 if (mz_stream_ioapi_is_open(stream) != MZ_OK)
100 return MZ_OPEN_ERROR;
101
102 if (ioapi->filefunc64.zwrite_file) {
103 zwrite = ioapi->filefunc64.zwrite_file;
104 opaque = ioapi->filefunc64.opaque;
105 } else if (ioapi->filefunc.zwrite_file) {
106 zwrite = ioapi->filefunc.zwrite_file;
107 opaque = ioapi->filefunc.opaque;
108 } else
109 return MZ_PARAM_ERROR;
110
111 written = (int32_t)zwrite(opaque, ioapi->handle, buf, size);
112 return written;
113 }
114
115 static int64_t mz_stream_ioapi_tell(void *stream) {
116 mz_stream_ioapi *ioapi = (mz_stream_ioapi *)stream;
117
118 if (mz_stream_ioapi_is_open(stream) != MZ_OK)
119 return MZ_OPEN_ERROR;
120
121 if (ioapi->filefunc64.ztell64_file)
122 return ioapi->filefunc64.ztell64_file(ioapi->filefunc64.opaque, ioapi->handle);
123 else if (ioapi->filefunc.ztell_file)
124 return ioapi->filefunc.ztell_file(ioapi->filefunc.opaque, ioapi->handle);
125
126 return MZ_INTERNAL_ERROR;
127 }
128
129 static int32_t mz_stream_ioapi_seek(void *stream, int64_t offset, int32_t origin) {
130 mz_stream_ioapi *ioapi = (mz_stream_ioapi *)stream;
131
132 if (mz_stream_ioapi_is_open(stream) != MZ_OK)
133 return MZ_OPEN_ERROR;
134
135 if (ioapi->filefunc64.zseek64_file) {
136 if (ioapi->filefunc64.zseek64_file(ioapi->filefunc64.opaque, ioapi->handle, offset, origin) != 0)
137 return MZ_INTERNAL_ERROR;
138 } else if (ioapi->filefunc.zseek_file) {
139 if (ioapi->filefunc.zseek_file(ioapi->filefunc.opaque, ioapi->handle, (int32_t)offset, origin) != 0)
140 return MZ_INTERNAL_ERROR;
141 } else
142 return MZ_PARAM_ERROR;
143
144 return MZ_OK;
145 }
146
147 static int32_t mz_stream_ioapi_close(void *stream) {
148 mz_stream_ioapi *ioapi = (mz_stream_ioapi *)stream;
149 close_file_func zclose = NULL;
150 void *opaque = NULL;
151
152 if (mz_stream_ioapi_is_open(stream) != MZ_OK)
153 return MZ_OPEN_ERROR;
154
155 if (ioapi->filefunc.zclose_file) {
156 zclose = ioapi->filefunc.zclose_file;
157 opaque = ioapi->filefunc.opaque;
158 } else if (ioapi->filefunc64.zclose_file) {
159 zclose = ioapi->filefunc64.zclose_file;
160 opaque = ioapi->filefunc64.opaque;
161 } else
162 return MZ_PARAM_ERROR;
163
164 if (zclose(opaque, ioapi->handle) != 0)
165 return MZ_CLOSE_ERROR;
166 ioapi->handle = NULL;
167 return MZ_OK;
168 }
169
170 static int32_t mz_stream_ioapi_error(void *stream) {
171 mz_stream_ioapi *ioapi = (mz_stream_ioapi *)stream;
172 testerror_file_func zerror = NULL;
173 void *opaque = NULL;
174
175 if (mz_stream_ioapi_is_open(stream) != MZ_OK)
176 return MZ_OPEN_ERROR;
177
178 if (ioapi->filefunc.zerror_file) {
179 zerror = ioapi->filefunc.zerror_file;
180 opaque = ioapi->filefunc.opaque;
181 } else if (ioapi->filefunc64.zerror_file) {
182 zerror = ioapi->filefunc64.zerror_file;
183 opaque = ioapi->filefunc64.opaque;
184 } else
185 return MZ_PARAM_ERROR;
186
187 return zerror(opaque, ioapi->handle);
188 }
189
190 int32_t mz_stream_ioapi_set_filefunc(void *stream, zlib_filefunc_def *filefunc) {
191 mz_stream_ioapi *ioapi = (mz_stream_ioapi *)stream;
192 memcpy(&ioapi->filefunc, filefunc, sizeof(zlib_filefunc_def));
193 return MZ_OK;
194 }
195
196 int32_t mz_stream_ioapi_set_filefunc64(void *stream, zlib_filefunc64_def *filefunc) {
197 mz_stream_ioapi *ioapi = (mz_stream_ioapi *)stream;
198 memcpy(&ioapi->filefunc64, filefunc, sizeof(zlib_filefunc64_def));
199 return MZ_OK;
200 }
201
202 void *mz_stream_ioapi_create(void) {
203 mz_stream_ioapi *ioapi = (mz_stream_ioapi *)calloc(1, sizeof(mz_stream_ioapi));
204 if (ioapi)
205 ioapi->stream.vtbl = &mz_stream_ioapi_vtbl;
206 return ioapi;
207 }
208
209 void mz_stream_ioapi_delete(void **stream) {
210 mz_stream_ioapi *ioapi = NULL;
211 if (!stream)
212 return;
213 ioapi = (mz_stream_ioapi *)*stream;
214 free(ioapi);
215 *stream = NULL;
216 }
217
218 /***************************************************************************/
219
220 void fill_fopen_filefunc(zlib_filefunc_def *pzlib_filefunc_def) {
221 /* For 32-bit file support only, compile with MZ_FILE32_API */
222 if (pzlib_filefunc_def)
223 memset(pzlib_filefunc_def, 0, sizeof(zlib_filefunc_def));
224 }
225
226 void fill_fopen64_filefunc(zlib_filefunc64_def *pzlib_filefunc_def) {
227 /* All mz_stream_os_* support large files if compilation supports it */
228 if (pzlib_filefunc_def)
229 memset(pzlib_filefunc_def, 0, sizeof(zlib_filefunc64_def));
230 }
231
232 void fill_win32_filefunc(zlib_filefunc_def *pzlib_filefunc_def) {
233 /* Handled by mz_stream_os_win32 */
234 if (pzlib_filefunc_def)
235 memset(pzlib_filefunc_def, 0, sizeof(zlib_filefunc_def));
236 }
237
238 void fill_win32_filefunc64(zlib_filefunc64_def *pzlib_filefunc_def) {
239 /* Automatically supported in mz_stream_os_win32 */
240 if (pzlib_filefunc_def)
241 memset(pzlib_filefunc_def, 0, sizeof(zlib_filefunc64_def));
242 }
243
244 void fill_win32_filefunc64A(zlib_filefunc64_def *pzlib_filefunc_def) {
245 /* Automatically supported in mz_stream_os_win32 */
246 if (pzlib_filefunc_def)
247 memset(pzlib_filefunc_def, 0, sizeof(zlib_filefunc64_def));
248 }
249
250 /* NOTE: fill_win32_filefunc64W is no longer necessary since wide-character
251 support is automatically handled by the underlying os stream. Do not
252 pass wide-characters to zipOpen or unzOpen. */
253
254 void fill_memory_filefunc(zlib_filefunc_def *pzlib_filefunc_def) {
255 /* Use opaque to indicate which stream interface to create */
256 if (pzlib_filefunc_def) {
257 memset(pzlib_filefunc_def, 0, sizeof(zlib_filefunc_def));
258 pzlib_filefunc_def->opaque = mz_stream_mem_get_interface();
259 }
260 }