packvfs
view test/zipcat/src/minizip/iowin32.c @ 3:ef6c1472607f
jesus fucking christ that was easy... written a test prog "zipcat" to try out
zlib's contrib library "minizip", to list and read files out of zip archives
directly...
author | John Tsiombikas <nuclear@member.fsf.org> |
---|---|
date | Mon, 04 Nov 2013 06:46:17 +0200 |
parents | |
children |
line source
1 /* iowin32.c -- IO base function header for compress/uncompress .zip
2 Version 1.1, February 14h, 2010
3 part of the MiniZip project - ( http://www.winimage.com/zLibDll/minizip.html )
5 Copyright (C) 1998-2010 Gilles Vollant (minizip) ( http://www.winimage.com/zLibDll/minizip.html )
7 Modifications for Zip64 support
8 Copyright (C) 2009-2010 Mathias Svensson ( http://result42.com )
10 For more info read MiniZip_info.txt
12 */
14 #ifdef WIN32
16 #include <stdlib.h>
18 #include "zlib.h"
19 #include "ioapi.h"
20 #include "iowin32.h"
22 #ifndef INVALID_HANDLE_VALUE
23 #define INVALID_HANDLE_VALUE (0xFFFFFFFF)
24 #endif
26 #ifndef INVALID_SET_FILE_POINTER
27 #define INVALID_SET_FILE_POINTER ((DWORD)-1)
28 #endif
31 #if defined(WINAPI_FAMILY_PARTITION) && (!(defined(IOWIN32_USING_WINRT_API)))
32 #if WINAPI_FAMILY_PARTITION(WINAPI_PARTITION_APP)
33 #define IOWIN32_USING_WINRT_API 1
34 #endif
35 #endif
37 voidpf ZCALLBACK win32_open_file_func OF((voidpf opaque, const char* filename, int mode));
38 uLong ZCALLBACK win32_read_file_func OF((voidpf opaque, voidpf stream, void* buf, uLong size));
39 uLong ZCALLBACK win32_write_file_func OF((voidpf opaque, voidpf stream, const void* buf, uLong size));
40 ZPOS64_T ZCALLBACK win32_tell64_file_func OF((voidpf opaque, voidpf stream));
41 long ZCALLBACK win32_seek64_file_func OF((voidpf opaque, voidpf stream, ZPOS64_T offset, int origin));
42 int ZCALLBACK win32_close_file_func OF((voidpf opaque, voidpf stream));
43 int ZCALLBACK win32_error_file_func OF((voidpf opaque, voidpf stream));
45 typedef struct
46 {
47 HANDLE hf;
48 int error;
49 } WIN32FILE_IOWIN;
52 static void win32_translate_open_mode(int mode,
53 DWORD* lpdwDesiredAccess,
54 DWORD* lpdwCreationDisposition,
55 DWORD* lpdwShareMode,
56 DWORD* lpdwFlagsAndAttributes)
57 {
58 *lpdwDesiredAccess = *lpdwShareMode = *lpdwFlagsAndAttributes = *lpdwCreationDisposition = 0;
60 if ((mode & ZLIB_FILEFUNC_MODE_READWRITEFILTER)==ZLIB_FILEFUNC_MODE_READ)
61 {
62 *lpdwDesiredAccess = GENERIC_READ;
63 *lpdwCreationDisposition = OPEN_EXISTING;
64 *lpdwShareMode = FILE_SHARE_READ;
65 }
66 else if (mode & ZLIB_FILEFUNC_MODE_EXISTING)
67 {
68 *lpdwDesiredAccess = GENERIC_WRITE | GENERIC_READ;
69 *lpdwCreationDisposition = OPEN_EXISTING;
70 }
71 else if (mode & ZLIB_FILEFUNC_MODE_CREATE)
72 {
73 *lpdwDesiredAccess = GENERIC_WRITE | GENERIC_READ;
74 *lpdwCreationDisposition = CREATE_ALWAYS;
75 }
76 }
78 static voidpf win32_build_iowin(HANDLE hFile)
79 {
80 voidpf ret=NULL;
82 if ((hFile != NULL) && (hFile != INVALID_HANDLE_VALUE))
83 {
84 WIN32FILE_IOWIN w32fiow;
85 w32fiow.hf = hFile;
86 w32fiow.error = 0;
87 ret = malloc(sizeof(WIN32FILE_IOWIN));
89 if (ret==NULL)
90 CloseHandle(hFile);
91 else
92 *((WIN32FILE_IOWIN*)ret) = w32fiow;
93 }
94 return ret;
95 }
97 voidpf ZCALLBACK win32_open64_file_func (voidpf opaque,const void* filename,int mode)
98 {
99 const char* mode_fopen = NULL;
100 DWORD dwDesiredAccess,dwCreationDisposition,dwShareMode,dwFlagsAndAttributes ;
101 HANDLE hFile = NULL;
103 win32_translate_open_mode(mode,&dwDesiredAccess,&dwCreationDisposition,&dwShareMode,&dwFlagsAndAttributes);
105 #ifdef IOWIN32_USING_WINRT_API
106 #ifdef UNICODE
107 if ((filename!=NULL) && (dwDesiredAccess != 0))
108 hFile = CreateFile2((LPCTSTR)filename, dwDesiredAccess, dwShareMode, dwCreationDisposition, NULL);
109 #else
110 if ((filename!=NULL) && (dwDesiredAccess != 0))
111 {
112 WCHAR filenameW[FILENAME_MAX + 0x200 + 1];
113 MultiByteToWideChar(CP_ACP,0,(const char*)filename,-1,filenameW,FILENAME_MAX + 0x200);
114 hFile = CreateFile2(filenameW, dwDesiredAccess, dwShareMode, dwCreationDisposition, NULL);
115 }
116 #endif
117 #else
118 if ((filename!=NULL) && (dwDesiredAccess != 0))
119 hFile = CreateFile((LPCTSTR)filename, dwDesiredAccess, dwShareMode, NULL, dwCreationDisposition, dwFlagsAndAttributes, NULL);
120 #endif
122 return win32_build_iowin(hFile);
123 }
126 voidpf ZCALLBACK win32_open64_file_funcA (voidpf opaque,const void* filename,int mode)
127 {
128 const char* mode_fopen = NULL;
129 DWORD dwDesiredAccess,dwCreationDisposition,dwShareMode,dwFlagsAndAttributes ;
130 HANDLE hFile = NULL;
132 win32_translate_open_mode(mode,&dwDesiredAccess,&dwCreationDisposition,&dwShareMode,&dwFlagsAndAttributes);
134 #ifdef IOWIN32_USING_WINRT_API
135 if ((filename!=NULL) && (dwDesiredAccess != 0))
136 {
137 WCHAR filenameW[FILENAME_MAX + 0x200 + 1];
138 MultiByteToWideChar(CP_ACP,0,(const char*)filename,-1,filenameW,FILENAME_MAX + 0x200);
139 hFile = CreateFile2(filenameW, dwDesiredAccess, dwShareMode, dwCreationDisposition, NULL);
140 }
141 #else
142 if ((filename!=NULL) && (dwDesiredAccess != 0))
143 hFile = CreateFileA((LPCSTR)filename, dwDesiredAccess, dwShareMode, NULL, dwCreationDisposition, dwFlagsAndAttributes, NULL);
144 #endif
146 return win32_build_iowin(hFile);
147 }
150 voidpf ZCALLBACK win32_open64_file_funcW (voidpf opaque,const void* filename,int mode)
151 {
152 const char* mode_fopen = NULL;
153 DWORD dwDesiredAccess,dwCreationDisposition,dwShareMode,dwFlagsAndAttributes ;
154 HANDLE hFile = NULL;
156 win32_translate_open_mode(mode,&dwDesiredAccess,&dwCreationDisposition,&dwShareMode,&dwFlagsAndAttributes);
158 #ifdef IOWIN32_USING_WINRT_API
159 if ((filename!=NULL) && (dwDesiredAccess != 0))
160 hFile = CreateFile2((LPCWSTR)filename, dwDesiredAccess, dwShareMode, dwCreationDisposition,NULL);
161 #else
162 if ((filename!=NULL) && (dwDesiredAccess != 0))
163 hFile = CreateFileW((LPCWSTR)filename, dwDesiredAccess, dwShareMode, NULL, dwCreationDisposition, dwFlagsAndAttributes, NULL);
164 #endif
166 return win32_build_iowin(hFile);
167 }
170 voidpf ZCALLBACK win32_open_file_func (voidpf opaque,const char* filename,int mode)
171 {
172 const char* mode_fopen = NULL;
173 DWORD dwDesiredAccess,dwCreationDisposition,dwShareMode,dwFlagsAndAttributes ;
174 HANDLE hFile = NULL;
176 win32_translate_open_mode(mode,&dwDesiredAccess,&dwCreationDisposition,&dwShareMode,&dwFlagsAndAttributes);
178 #ifdef IOWIN32_USING_WINRT_API
179 #ifdef UNICODE
180 if ((filename!=NULL) && (dwDesiredAccess != 0))
181 hFile = CreateFile2((LPCTSTR)filename, dwDesiredAccess, dwShareMode, dwCreationDisposition, NULL);
182 #else
183 if ((filename!=NULL) && (dwDesiredAccess != 0))
184 {
185 WCHAR filenameW[FILENAME_MAX + 0x200 + 1];
186 MultiByteToWideChar(CP_ACP,0,(const char*)filename,-1,filenameW,FILENAME_MAX + 0x200);
187 hFile = CreateFile2(filenameW, dwDesiredAccess, dwShareMode, dwCreationDisposition, NULL);
188 }
189 #endif
190 #else
191 if ((filename!=NULL) && (dwDesiredAccess != 0))
192 hFile = CreateFile((LPCTSTR)filename, dwDesiredAccess, dwShareMode, NULL, dwCreationDisposition, dwFlagsAndAttributes, NULL);
193 #endif
195 return win32_build_iowin(hFile);
196 }
199 uLong ZCALLBACK win32_read_file_func (voidpf opaque, voidpf stream, void* buf,uLong size)
200 {
201 uLong ret=0;
202 HANDLE hFile = NULL;
203 if (stream!=NULL)
204 hFile = ((WIN32FILE_IOWIN*)stream) -> hf;
206 if (hFile != NULL)
207 {
208 if (!ReadFile(hFile, buf, size, &ret, NULL))
209 {
210 DWORD dwErr = GetLastError();
211 if (dwErr == ERROR_HANDLE_EOF)
212 dwErr = 0;
213 ((WIN32FILE_IOWIN*)stream) -> error=(int)dwErr;
214 }
215 }
217 return ret;
218 }
221 uLong ZCALLBACK win32_write_file_func (voidpf opaque,voidpf stream,const void* buf,uLong size)
222 {
223 uLong ret=0;
224 HANDLE hFile = NULL;
225 if (stream!=NULL)
226 hFile = ((WIN32FILE_IOWIN*)stream) -> hf;
228 if (hFile != NULL)
229 {
230 if (!WriteFile(hFile, buf, size, &ret, NULL))
231 {
232 DWORD dwErr = GetLastError();
233 if (dwErr == ERROR_HANDLE_EOF)
234 dwErr = 0;
235 ((WIN32FILE_IOWIN*)stream) -> error=(int)dwErr;
236 }
237 }
239 return ret;
240 }
242 static BOOL MySetFilePointerEx(HANDLE hFile, LARGE_INTEGER pos, LARGE_INTEGER *newPos, DWORD dwMoveMethod)
243 {
244 #ifdef IOWIN32_USING_WINRT_API
245 return SetFilePointerEx(hFile, pos, newPos, dwMoveMethod);
246 #else
247 LONG lHigh = pos.HighPart;
248 DWORD dwNewPos = SetFilePointer(hFile, pos.LowPart, &lHigh, FILE_CURRENT);
249 BOOL fOk = TRUE;
250 if (dwNewPos == 0xFFFFFFFF)
251 if (GetLastError() != NO_ERROR)
252 fOk = FALSE;
253 if ((newPos != NULL) && (fOk))
254 {
255 newPos->LowPart = dwNewPos;
256 newPos->HighPart = lHigh;
257 }
258 return fOk;
259 #endif
260 }
262 long ZCALLBACK win32_tell_file_func (voidpf opaque,voidpf stream)
263 {
264 long ret=-1;
265 HANDLE hFile = NULL;
266 if (stream!=NULL)
267 hFile = ((WIN32FILE_IOWIN*)stream) -> hf;
268 if (hFile != NULL)
269 {
270 LARGE_INTEGER pos;
271 pos.QuadPart = 0;
273 if (!MySetFilePointerEx(hFile, pos, &pos, FILE_CURRENT))
274 {
275 DWORD dwErr = GetLastError();
276 ((WIN32FILE_IOWIN*)stream) -> error=(int)dwErr;
277 ret = -1;
278 }
279 else
280 ret=(long)pos.LowPart;
281 }
282 return ret;
283 }
285 ZPOS64_T ZCALLBACK win32_tell64_file_func (voidpf opaque, voidpf stream)
286 {
287 ZPOS64_T ret= (ZPOS64_T)-1;
288 HANDLE hFile = NULL;
289 if (stream!=NULL)
290 hFile = ((WIN32FILE_IOWIN*)stream)->hf;
292 if (hFile)
293 {
294 LARGE_INTEGER pos;
295 pos.QuadPart = 0;
297 if (!MySetFilePointerEx(hFile, pos, &pos, FILE_CURRENT))
298 {
299 DWORD dwErr = GetLastError();
300 ((WIN32FILE_IOWIN*)stream) -> error=(int)dwErr;
301 ret = (ZPOS64_T)-1;
302 }
303 else
304 ret=pos.QuadPart;
305 }
306 return ret;
307 }
310 long ZCALLBACK win32_seek_file_func (voidpf opaque,voidpf stream,uLong offset,int origin)
311 {
312 DWORD dwMoveMethod=0xFFFFFFFF;
313 HANDLE hFile = NULL;
315 long ret=-1;
316 if (stream!=NULL)
317 hFile = ((WIN32FILE_IOWIN*)stream) -> hf;
318 switch (origin)
319 {
320 case ZLIB_FILEFUNC_SEEK_CUR :
321 dwMoveMethod = FILE_CURRENT;
322 break;
323 case ZLIB_FILEFUNC_SEEK_END :
324 dwMoveMethod = FILE_END;
325 break;
326 case ZLIB_FILEFUNC_SEEK_SET :
327 dwMoveMethod = FILE_BEGIN;
328 break;
329 default: return -1;
330 }
332 if (hFile != NULL)
333 {
334 LARGE_INTEGER pos;
335 pos.QuadPart = offset;
336 if (!MySetFilePointerEx(hFile, pos, NULL, dwMoveMethod))
337 {
338 DWORD dwErr = GetLastError();
339 ((WIN32FILE_IOWIN*)stream) -> error=(int)dwErr;
340 ret = -1;
341 }
342 else
343 ret=0;
344 }
345 return ret;
346 }
348 long ZCALLBACK win32_seek64_file_func (voidpf opaque, voidpf stream,ZPOS64_T offset,int origin)
349 {
350 DWORD dwMoveMethod=0xFFFFFFFF;
351 HANDLE hFile = NULL;
352 long ret=-1;
354 if (stream!=NULL)
355 hFile = ((WIN32FILE_IOWIN*)stream)->hf;
357 switch (origin)
358 {
359 case ZLIB_FILEFUNC_SEEK_CUR :
360 dwMoveMethod = FILE_CURRENT;
361 break;
362 case ZLIB_FILEFUNC_SEEK_END :
363 dwMoveMethod = FILE_END;
364 break;
365 case ZLIB_FILEFUNC_SEEK_SET :
366 dwMoveMethod = FILE_BEGIN;
367 break;
368 default: return -1;
369 }
371 if (hFile)
372 {
373 LARGE_INTEGER pos;
374 pos.QuadPart = offset;
375 if (!MySetFilePointerEx(hFile, pos, NULL, FILE_CURRENT))
376 {
377 DWORD dwErr = GetLastError();
378 ((WIN32FILE_IOWIN*)stream) -> error=(int)dwErr;
379 ret = -1;
380 }
381 else
382 ret=0;
383 }
384 return ret;
385 }
387 int ZCALLBACK win32_close_file_func (voidpf opaque, voidpf stream)
388 {
389 int ret=-1;
391 if (stream!=NULL)
392 {
393 HANDLE hFile;
394 hFile = ((WIN32FILE_IOWIN*)stream) -> hf;
395 if (hFile != NULL)
396 {
397 CloseHandle(hFile);
398 ret=0;
399 }
400 free(stream);
401 }
402 return ret;
403 }
405 int ZCALLBACK win32_error_file_func (voidpf opaque,voidpf stream)
406 {
407 int ret=-1;
408 if (stream!=NULL)
409 {
410 ret = ((WIN32FILE_IOWIN*)stream) -> error;
411 }
412 return ret;
413 }
415 void fill_win32_filefunc (zlib_filefunc_def* pzlib_filefunc_def)
416 {
417 pzlib_filefunc_def->zopen_file = win32_open_file_func;
418 pzlib_filefunc_def->zread_file = win32_read_file_func;
419 pzlib_filefunc_def->zwrite_file = win32_write_file_func;
420 pzlib_filefunc_def->ztell_file = win32_tell_file_func;
421 pzlib_filefunc_def->zseek_file = win32_seek_file_func;
422 pzlib_filefunc_def->zclose_file = win32_close_file_func;
423 pzlib_filefunc_def->zerror_file = win32_error_file_func;
424 pzlib_filefunc_def->opaque = NULL;
425 }
427 void fill_win32_filefunc64(zlib_filefunc64_def* pzlib_filefunc_def)
428 {
429 pzlib_filefunc_def->zopen64_file = win32_open64_file_func;
430 pzlib_filefunc_def->zread_file = win32_read_file_func;
431 pzlib_filefunc_def->zwrite_file = win32_write_file_func;
432 pzlib_filefunc_def->ztell64_file = win32_tell64_file_func;
433 pzlib_filefunc_def->zseek64_file = win32_seek64_file_func;
434 pzlib_filefunc_def->zclose_file = win32_close_file_func;
435 pzlib_filefunc_def->zerror_file = win32_error_file_func;
436 pzlib_filefunc_def->opaque = NULL;
437 }
440 void fill_win32_filefunc64A(zlib_filefunc64_def* pzlib_filefunc_def)
441 {
442 pzlib_filefunc_def->zopen64_file = win32_open64_file_funcA;
443 pzlib_filefunc_def->zread_file = win32_read_file_func;
444 pzlib_filefunc_def->zwrite_file = win32_write_file_func;
445 pzlib_filefunc_def->ztell64_file = win32_tell64_file_func;
446 pzlib_filefunc_def->zseek64_file = win32_seek64_file_func;
447 pzlib_filefunc_def->zclose_file = win32_close_file_func;
448 pzlib_filefunc_def->zerror_file = win32_error_file_func;
449 pzlib_filefunc_def->opaque = NULL;
450 }
453 void fill_win32_filefunc64W(zlib_filefunc64_def* pzlib_filefunc_def)
454 {
455 pzlib_filefunc_def->zopen64_file = win32_open64_file_funcW;
456 pzlib_filefunc_def->zread_file = win32_read_file_func;
457 pzlib_filefunc_def->zwrite_file = win32_write_file_func;
458 pzlib_filefunc_def->ztell64_file = win32_tell64_file_func;
459 pzlib_filefunc_def->zseek64_file = win32_seek64_file_func;
460 pzlib_filefunc_def->zclose_file = win32_close_file_func;
461 pzlib_filefunc_def->zerror_file = win32_error_file_func;
462 pzlib_filefunc_def->opaque = NULL;
463 }
465 #else
466 int __minizip_iowin32_c_avoid_empty_unit_warning;
467 #endif /* WIN32 */