diff options
Diffstat (limited to 'contrib/minizip/iowin32.c')
-rw-r--r-- | contrib/minizip/iowin32.c | 271 |
1 files changed, 271 insertions, 0 deletions
diff --git a/contrib/minizip/iowin32.c b/contrib/minizip/iowin32.c new file mode 100644 index 0000000..f1f5546 --- /dev/null +++ b/contrib/minizip/iowin32.c | |||
@@ -0,0 +1,271 @@ | |||
1 | /* iowin32.c -- IO base function header for compress/uncompress .zip | ||
2 | files using zlib + zip or unzip API | ||
3 | This IO API version uses the Win32 API (for Microsoft Windows) | ||
4 | |||
5 | Version 0.21, March 10th, 2003 | ||
6 | |||
7 | Copyright (C) 1998-2003 Gilles Vollant | ||
8 | */ | ||
9 | |||
10 | #include <windows.h> | ||
11 | #include <stdlib.h> | ||
12 | |||
13 | #include "zlib.h" | ||
14 | #include "ioapi.h" | ||
15 | #include "iowin32.h" | ||
16 | |||
17 | #ifndef INVALID_HANDLE_VALUE | ||
18 | #define INVALID_HANDLE_VALUE (0xFFFFFFFF) | ||
19 | #endif | ||
20 | |||
21 | #ifndef INVALID_SET_FILE_POINTER | ||
22 | #define INVALID_SET_FILE_POINTER ((DWORD)-1) | ||
23 | #endif | ||
24 | |||
25 | voidpf ZCALLBACK win32_open_file_func OF(( | ||
26 | voidpf opaque, | ||
27 | const char* filename, | ||
28 | int mode)); | ||
29 | |||
30 | uLong ZCALLBACK win32_read_file_func OF(( | ||
31 | voidpf opaque, | ||
32 | voidpf stream, | ||
33 | void* buf, | ||
34 | uLong size)); | ||
35 | |||
36 | uLong ZCALLBACK win32_write_file_func OF(( | ||
37 | voidpf opaque, | ||
38 | voidpf stream, | ||
39 | const void* buf, | ||
40 | uLong size)); | ||
41 | |||
42 | long ZCALLBACK win32_tell_file_func OF(( | ||
43 | voidpf opaque, | ||
44 | voidpf stream)); | ||
45 | |||
46 | long ZCALLBACK win32_seek_file_func OF(( | ||
47 | voidpf opaque, | ||
48 | voidpf stream, | ||
49 | uLong offset, | ||
50 | int origin)); | ||
51 | |||
52 | long ZCALLBACK win32_close_file_func OF(( | ||
53 | voidpf opaque, | ||
54 | voidpf stream)); | ||
55 | |||
56 | int ZCALLBACK win32_error_file_func OF(( | ||
57 | voidpf opaque, | ||
58 | voidpf stream)); | ||
59 | |||
60 | typedef struct | ||
61 | { | ||
62 | HANDLE hf; | ||
63 | int error; | ||
64 | } WIN32FILE_IOWIN; | ||
65 | |||
66 | voidpf ZCALLBACK win32_open_file_func (opaque, filename, mode) | ||
67 | voidpf opaque; | ||
68 | const char* filename; | ||
69 | int mode; | ||
70 | { | ||
71 | const char* mode_fopen = NULL; | ||
72 | DWORD dwDesiredAccess,dwCreationDisposition,dwShareMode,dwFlagsAndAttributes ; | ||
73 | HANDLE hFile = 0; | ||
74 | voidpf ret=NULL; | ||
75 | |||
76 | dwDesiredAccess = dwShareMode = dwFlagsAndAttributes = 0; | ||
77 | |||
78 | if ((mode & ZLIB_FILEFUNC_MODE_READWRITEFILTER)==ZLIB_FILEFUNC_MODE_READ) | ||
79 | { | ||
80 | dwDesiredAccess = GENERIC_READ; | ||
81 | dwCreationDisposition = OPEN_EXISTING; | ||
82 | dwShareMode = FILE_SHARE_READ; | ||
83 | } | ||
84 | else | ||
85 | if (mode & ZLIB_FILEFUNC_MODE_EXISTING) | ||
86 | { | ||
87 | dwDesiredAccess = GENERIC_WRITE | GENERIC_READ; | ||
88 | dwCreationDisposition = OPEN_EXISTING; | ||
89 | } | ||
90 | else | ||
91 | if (mode & ZLIB_FILEFUNC_MODE_CREATE) | ||
92 | { | ||
93 | dwDesiredAccess = GENERIC_WRITE | GENERIC_READ; | ||
94 | dwCreationDisposition = CREATE_ALWAYS; | ||
95 | } | ||
96 | |||
97 | if ((filename!=NULL) && (dwDesiredAccess != 0)) | ||
98 | hFile = CreateFile((LPCTSTR)filename, dwDesiredAccess, dwShareMode, NULL, | ||
99 | dwCreationDisposition, dwFlagsAndAttributes, NULL); | ||
100 | |||
101 | if (hFile == INVALID_HANDLE_VALUE) | ||
102 | hFile = NULL; | ||
103 | |||
104 | if (hFile != NULL) | ||
105 | { | ||
106 | WIN32FILE_IOWIN w32fiow; | ||
107 | w32fiow.hf = hFile; | ||
108 | w32fiow.error = 0; | ||
109 | ret = malloc(sizeof(WIN32FILE_IOWIN)); | ||
110 | if (ret==NULL) | ||
111 | CloseHandle(hFile); | ||
112 | else *((WIN32FILE_IOWIN*)ret) = w32fiow; | ||
113 | } | ||
114 | return ret; | ||
115 | } | ||
116 | |||
117 | |||
118 | uLong ZCALLBACK win32_read_file_func (opaque, stream, buf, size) | ||
119 | voidpf opaque; | ||
120 | voidpf stream; | ||
121 | void* buf; | ||
122 | uLong size; | ||
123 | { | ||
124 | uLong ret=0; | ||
125 | HANDLE hFile = NULL; | ||
126 | if (stream!=NULL) | ||
127 | hFile = ((WIN32FILE_IOWIN*)stream) -> hf; | ||
128 | if (hFile != NULL) | ||
129 | if (!ReadFile(hFile, buf, size, &ret, NULL)) | ||
130 | { | ||
131 | DWORD dwErr = GetLastError(); | ||
132 | if (dwErr == ERROR_HANDLE_EOF) | ||
133 | dwErr = 0; | ||
134 | ((WIN32FILE_IOWIN*)stream) -> error=(int)dwErr; | ||
135 | } | ||
136 | |||
137 | return ret; | ||
138 | } | ||
139 | |||
140 | |||
141 | uLong ZCALLBACK win32_write_file_func (opaque, stream, buf, size) | ||
142 | voidpf opaque; | ||
143 | voidpf stream; | ||
144 | const void* buf; | ||
145 | uLong size; | ||
146 | { | ||
147 | uLong ret=0; | ||
148 | HANDLE hFile = NULL; | ||
149 | if (stream!=NULL) | ||
150 | hFile = ((WIN32FILE_IOWIN*)stream) -> hf; | ||
151 | |||
152 | if (hFile !=NULL) | ||
153 | if (!WriteFile(hFile, buf, size, &ret, NULL)) | ||
154 | { | ||
155 | DWORD dwErr = GetLastError(); | ||
156 | if (dwErr == ERROR_HANDLE_EOF) | ||
157 | dwErr = 0; | ||
158 | ((WIN32FILE_IOWIN*)stream) -> error=(int)dwErr; | ||
159 | } | ||
160 | |||
161 | return ret; | ||
162 | } | ||
163 | |||
164 | long ZCALLBACK win32_tell_file_func (opaque, stream) | ||
165 | voidpf opaque; | ||
166 | voidpf stream; | ||
167 | { | ||
168 | long ret=-1; | ||
169 | HANDLE hFile = NULL; | ||
170 | if (stream!=NULL) | ||
171 | hFile = ((WIN32FILE_IOWIN*)stream) -> hf; | ||
172 | if (hFile != NULL) | ||
173 | { | ||
174 | DWORD dwSet = SetFilePointer(hFile, 0, NULL, FILE_CURRENT); | ||
175 | if (dwSet == INVALID_SET_FILE_POINTER) | ||
176 | { | ||
177 | DWORD dwErr = GetLastError(); | ||
178 | ((WIN32FILE_IOWIN*)stream) -> error=(int)dwErr; | ||
179 | ret = -1; | ||
180 | } | ||
181 | else | ||
182 | ret=(long)dwSet; | ||
183 | } | ||
184 | return ret; | ||
185 | } | ||
186 | |||
187 | long ZCALLBACK win32_seek_file_func (opaque, stream, offset, origin) | ||
188 | voidpf opaque; | ||
189 | voidpf stream; | ||
190 | uLong offset; | ||
191 | int origin; | ||
192 | { | ||
193 | DWORD dwMoveMethod=0xFFFFFFFF; | ||
194 | HANDLE hFile = NULL; | ||
195 | |||
196 | long ret=-1; | ||
197 | if (stream!=NULL) | ||
198 | hFile = ((WIN32FILE_IOWIN*)stream) -> hf; | ||
199 | switch (origin) | ||
200 | { | ||
201 | case ZLIB_FILEFUNC_SEEK_CUR : | ||
202 | dwMoveMethod = FILE_CURRENT; | ||
203 | break; | ||
204 | case ZLIB_FILEFUNC_SEEK_END : | ||
205 | dwMoveMethod = FILE_END; | ||
206 | break; | ||
207 | case ZLIB_FILEFUNC_SEEK_SET : | ||
208 | dwMoveMethod = FILE_BEGIN; | ||
209 | break; | ||
210 | default: return -1; | ||
211 | } | ||
212 | |||
213 | if (hFile != NULL) | ||
214 | { | ||
215 | DWORD dwSet = SetFilePointer(hFile, offset, NULL, dwMoveMethod); | ||
216 | if (dwSet == INVALID_SET_FILE_POINTER) | ||
217 | { | ||
218 | DWORD dwErr = GetLastError(); | ||
219 | ((WIN32FILE_IOWIN*)stream) -> error=(int)dwErr; | ||
220 | ret = -1; | ||
221 | } | ||
222 | else | ||
223 | ret=0; | ||
224 | } | ||
225 | return ret; | ||
226 | } | ||
227 | |||
228 | long ZCALLBACK win32_close_file_func (opaque, stream) | ||
229 | voidpf opaque; | ||
230 | voidpf stream; | ||
231 | { | ||
232 | long ret=-1; | ||
233 | |||
234 | if (stream!=NULL) | ||
235 | { | ||
236 | HANDLE hFile; | ||
237 | hFile = ((WIN32FILE_IOWIN*)stream) -> hf; | ||
238 | if (hFile != NULL) | ||
239 | { | ||
240 | CloseHandle(hFile); | ||
241 | ret=0; | ||
242 | } | ||
243 | free(stream); | ||
244 | } | ||
245 | return ret; | ||
246 | } | ||
247 | |||
248 | int ZCALLBACK win32_error_file_func (opaque, stream) | ||
249 | voidpf opaque; | ||
250 | voidpf stream; | ||
251 | { | ||
252 | int ret=-1; | ||
253 | if (stream!=NULL) | ||
254 | { | ||
255 | ret = ((WIN32FILE_IOWIN*)stream) -> error; | ||
256 | } | ||
257 | return ret; | ||
258 | } | ||
259 | |||
260 | void fill_win32_filefunc (pzlib_filefunc_def) | ||
261 | zlib_filefunc_def* pzlib_filefunc_def; | ||
262 | { | ||
263 | pzlib_filefunc_def->zopen_file = win32_open_file_func; | ||
264 | pzlib_filefunc_def->zread_file = win32_read_file_func; | ||
265 | pzlib_filefunc_def->zwrite_file = win32_write_file_func; | ||
266 | pzlib_filefunc_def->ztell_file = win32_tell_file_func; | ||
267 | pzlib_filefunc_def->zseek_file = win32_seek_file_func; | ||
268 | pzlib_filefunc_def->zclose_file = win32_close_file_func; | ||
269 | pzlib_filefunc_def->zerror_file = win32_error_file_func; | ||
270 | pzlib_filefunc_def->opaque=NULL; | ||
271 | } | ||