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
20 #ifndef INVALID_HANDLE_VALUE
21 #define INVALID_HANDLE_VALUE (0xFFFFFFFF)
24 #ifndef INVALID_SET_FILE_POINTER
25 #define INVALID_SET_FILE_POINTER ((DWORD)-1)
29 #if defined(WINAPI_FAMILY_PARTITION) && (!(defined(IOWIN32_USING_WINRT_API)))
30 #if WINAPI_FAMILY_PARTITION(WINAPI_PARTITION_APP)
31 #define IOWIN32_USING_WINRT_API 1
35 voidpf ZCALLBACK win32_open_file_func
OF((voidpf opaque
, const char* filename
, int mode
));
36 uLong ZCALLBACK win32_read_file_func
OF((voidpf opaque
, voidpf stream
, void* buf
, uLong size
));
37 uLong ZCALLBACK win32_write_file_func
OF((voidpf opaque
, voidpf stream
, const void* buf
, uLong size
));
38 ZPOS64_T ZCALLBACK win32_tell64_file_func
OF((voidpf opaque
, voidpf stream
));
39 long ZCALLBACK win32_seek64_file_func
OF((voidpf opaque
, voidpf stream
, ZPOS64_T offset
, int origin
));
40 int ZCALLBACK win32_close_file_func
OF((voidpf opaque
, voidpf stream
));
41 int ZCALLBACK win32_error_file_func
OF((voidpf opaque
, voidpf stream
));
50 static void win32_translate_open_mode(int mode
,
51 DWORD
* lpdwDesiredAccess
,
52 DWORD
* lpdwCreationDisposition
,
54 DWORD
* lpdwFlagsAndAttributes
)
56 *lpdwDesiredAccess
= *lpdwShareMode
= *lpdwFlagsAndAttributes
= *lpdwCreationDisposition
= 0;
58 if ((mode
& ZLIB_FILEFUNC_MODE_READWRITEFILTER
)==ZLIB_FILEFUNC_MODE_READ
)
60 *lpdwDesiredAccess
= GENERIC_READ
;
61 *lpdwCreationDisposition
= OPEN_EXISTING
;
62 *lpdwShareMode
= FILE_SHARE_READ
;
64 else if (mode
& ZLIB_FILEFUNC_MODE_EXISTING
)
66 *lpdwDesiredAccess
= GENERIC_WRITE
| GENERIC_READ
;
67 *lpdwCreationDisposition
= OPEN_EXISTING
;
69 else if (mode
& ZLIB_FILEFUNC_MODE_CREATE
)
71 *lpdwDesiredAccess
= GENERIC_WRITE
| GENERIC_READ
;
72 *lpdwCreationDisposition
= CREATE_ALWAYS
;
76 static voidpf
win32_build_iowin(HANDLE hFile
)
80 if ((hFile
!= NULL
) && (hFile
!= INVALID_HANDLE_VALUE
))
82 WIN32FILE_IOWIN w32fiow
;
85 ret
= malloc(sizeof(WIN32FILE_IOWIN
));
90 *((WIN32FILE_IOWIN
*)ret
) = w32fiow
;
95 voidpf ZCALLBACK
win32_open64_file_func (voidpf opaque
,const void* filename
,int mode
)
97 const char* mode_fopen
= NULL
;
98 DWORD dwDesiredAccess
,dwCreationDisposition
,dwShareMode
,dwFlagsAndAttributes
;
101 win32_translate_open_mode(mode
,&dwDesiredAccess
,&dwCreationDisposition
,&dwShareMode
,&dwFlagsAndAttributes
);
103 #ifdef IOWIN32_USING_WINRT_API
105 if ((filename
!=NULL
) && (dwDesiredAccess
!= 0))
106 hFile
= CreateFile2((LPCTSTR
)filename
, dwDesiredAccess
, dwShareMode
, dwCreationDisposition
, NULL
);
108 if ((filename
!=NULL
) && (dwDesiredAccess
!= 0))
110 WCHAR filenameW
[FILENAME_MAX
+ 0x200 + 1];
111 MultiByteToWideChar(CP_ACP
,0,(const char*)filename
,-1,filenameW
,FILENAME_MAX
+ 0x200);
112 hFile
= CreateFile2(filenameW
, dwDesiredAccess
, dwShareMode
, dwCreationDisposition
, NULL
);
116 if ((filename
!=NULL
) && (dwDesiredAccess
!= 0))
117 hFile
= CreateFile((LPCTSTR
)filename
, dwDesiredAccess
, dwShareMode
, NULL
, dwCreationDisposition
, dwFlagsAndAttributes
, NULL
);
120 return win32_build_iowin(hFile
);
124 voidpf ZCALLBACK
win32_open64_file_funcA (voidpf opaque
,const void* filename
,int mode
)
126 const char* mode_fopen
= NULL
;
127 DWORD dwDesiredAccess
,dwCreationDisposition
,dwShareMode
,dwFlagsAndAttributes
;
130 win32_translate_open_mode(mode
,&dwDesiredAccess
,&dwCreationDisposition
,&dwShareMode
,&dwFlagsAndAttributes
);
132 #ifdef IOWIN32_USING_WINRT_API
133 if ((filename
!=NULL
) && (dwDesiredAccess
!= 0))
135 WCHAR filenameW
[FILENAME_MAX
+ 0x200 + 1];
136 MultiByteToWideChar(CP_ACP
,0,(const char*)filename
,-1,filenameW
,FILENAME_MAX
+ 0x200);
137 hFile
= CreateFile2(filenameW
, dwDesiredAccess
, dwShareMode
, dwCreationDisposition
, NULL
);
140 if ((filename
!=NULL
) && (dwDesiredAccess
!= 0))
141 hFile
= CreateFileA((LPCSTR
)filename
, dwDesiredAccess
, dwShareMode
, NULL
, dwCreationDisposition
, dwFlagsAndAttributes
, NULL
);
144 return win32_build_iowin(hFile
);
148 voidpf ZCALLBACK
win32_open64_file_funcW (voidpf opaque
,const void* filename
,int mode
)
150 const char* mode_fopen
= NULL
;
151 DWORD dwDesiredAccess
,dwCreationDisposition
,dwShareMode
,dwFlagsAndAttributes
;
154 win32_translate_open_mode(mode
,&dwDesiredAccess
,&dwCreationDisposition
,&dwShareMode
,&dwFlagsAndAttributes
);
156 #ifdef IOWIN32_USING_WINRT_API
157 if ((filename
!=NULL
) && (dwDesiredAccess
!= 0))
158 hFile
= CreateFile2((LPCWSTR
)filename
, dwDesiredAccess
, dwShareMode
, dwCreationDisposition
,NULL
);
160 if ((filename
!=NULL
) && (dwDesiredAccess
!= 0))
161 hFile
= CreateFileW((LPCWSTR
)filename
, dwDesiredAccess
, dwShareMode
, NULL
, dwCreationDisposition
, dwFlagsAndAttributes
, NULL
);
164 return win32_build_iowin(hFile
);
168 voidpf ZCALLBACK
win32_open_file_func (voidpf opaque
,const char* filename
,int mode
)
170 const char* mode_fopen
= NULL
;
171 DWORD dwDesiredAccess
,dwCreationDisposition
,dwShareMode
,dwFlagsAndAttributes
;
174 win32_translate_open_mode(mode
,&dwDesiredAccess
,&dwCreationDisposition
,&dwShareMode
,&dwFlagsAndAttributes
);
176 #ifdef IOWIN32_USING_WINRT_API
178 if ((filename
!=NULL
) && (dwDesiredAccess
!= 0))
179 hFile
= CreateFile2((LPCTSTR
)filename
, dwDesiredAccess
, dwShareMode
, dwCreationDisposition
, NULL
);
181 if ((filename
!=NULL
) && (dwDesiredAccess
!= 0))
183 WCHAR filenameW
[FILENAME_MAX
+ 0x200 + 1];
184 MultiByteToWideChar(CP_ACP
,0,(const char*)filename
,-1,filenameW
,FILENAME_MAX
+ 0x200);
185 hFile
= CreateFile2(filenameW
, dwDesiredAccess
, dwShareMode
, dwCreationDisposition
, NULL
);
189 if ((filename
!=NULL
) && (dwDesiredAccess
!= 0))
190 hFile
= CreateFile((LPCTSTR
)filename
, dwDesiredAccess
, dwShareMode
, NULL
, dwCreationDisposition
, dwFlagsAndAttributes
, NULL
);
193 return win32_build_iowin(hFile
);
197 uLong ZCALLBACK
win32_read_file_func (voidpf opaque
, voidpf stream
, void* buf
,uLong size
)
202 hFile
= ((WIN32FILE_IOWIN
*)stream
) -> hf
;
206 if (!ReadFile(hFile
, buf
, size
, &ret
, NULL
))
208 DWORD dwErr
= GetLastError();
209 if (dwErr
== ERROR_HANDLE_EOF
)
211 ((WIN32FILE_IOWIN
*)stream
) -> error
=(int)dwErr
;
219 uLong ZCALLBACK
win32_write_file_func (voidpf opaque
,voidpf stream
,const void* buf
,uLong size
)
224 hFile
= ((WIN32FILE_IOWIN
*)stream
) -> hf
;
228 if (!WriteFile(hFile
, buf
, size
, &ret
, NULL
))
230 DWORD dwErr
= GetLastError();
231 if (dwErr
== ERROR_HANDLE_EOF
)
233 ((WIN32FILE_IOWIN
*)stream
) -> error
=(int)dwErr
;
240 static BOOL
MySetFilePointerEx(HANDLE hFile
, LARGE_INTEGER pos
, LARGE_INTEGER
*newPos
, DWORD dwMoveMethod
)
242 #ifdef IOWIN32_USING_WINRT_API
243 return SetFilePointerEx(hFile
, pos
, newPos
, dwMoveMethod
);
245 LONG lHigh
= pos
.HighPart
;
246 DWORD dwNewPos
= SetFilePointer(hFile
, pos
.LowPart
, &lHigh
, FILE_CURRENT
);
248 if (dwNewPos
== 0xFFFFFFFF)
249 if (GetLastError() != NO_ERROR
)
251 if ((newPos
!= NULL
) && (fOk
))
253 newPos
->LowPart
= dwNewPos
;
254 newPos
->HighPart
= lHigh
;
260 long ZCALLBACK
win32_tell_file_func (voidpf opaque
,voidpf stream
)
265 hFile
= ((WIN32FILE_IOWIN
*)stream
) -> hf
;
271 if (!MySetFilePointerEx(hFile
, pos
, &pos
, FILE_CURRENT
))
273 DWORD dwErr
= GetLastError();
274 ((WIN32FILE_IOWIN
*)stream
) -> error
=(int)dwErr
;
278 ret
=(long)pos
.LowPart
;
283 ZPOS64_T ZCALLBACK
win32_tell64_file_func (voidpf opaque
, voidpf stream
)
285 ZPOS64_T ret
= (ZPOS64_T
)-1;
288 hFile
= ((WIN32FILE_IOWIN
*)stream
)->hf
;
295 if (!MySetFilePointerEx(hFile
, pos
, &pos
, FILE_CURRENT
))
297 DWORD dwErr
= GetLastError();
298 ((WIN32FILE_IOWIN
*)stream
) -> error
=(int)dwErr
;
308 long ZCALLBACK
win32_seek_file_func (voidpf opaque
,voidpf stream
,uLong offset
,int origin
)
310 DWORD dwMoveMethod
=0xFFFFFFFF;
315 hFile
= ((WIN32FILE_IOWIN
*)stream
) -> hf
;
318 case ZLIB_FILEFUNC_SEEK_CUR
:
319 dwMoveMethod
= FILE_CURRENT
;
321 case ZLIB_FILEFUNC_SEEK_END
:
322 dwMoveMethod
= FILE_END
;
324 case ZLIB_FILEFUNC_SEEK_SET
:
325 dwMoveMethod
= FILE_BEGIN
;
333 pos
.QuadPart
= offset
;
334 if (!MySetFilePointerEx(hFile
, pos
, NULL
, dwMoveMethod
))
336 DWORD dwErr
= GetLastError();
337 ((WIN32FILE_IOWIN
*)stream
) -> error
=(int)dwErr
;
346 long ZCALLBACK
win32_seek64_file_func (voidpf opaque
, voidpf stream
,ZPOS64_T offset
,int origin
)
348 DWORD dwMoveMethod
=0xFFFFFFFF;
353 hFile
= ((WIN32FILE_IOWIN
*)stream
)->hf
;
357 case ZLIB_FILEFUNC_SEEK_CUR
:
358 dwMoveMethod
= FILE_CURRENT
;
360 case ZLIB_FILEFUNC_SEEK_END
:
361 dwMoveMethod
= FILE_END
;
363 case ZLIB_FILEFUNC_SEEK_SET
:
364 dwMoveMethod
= FILE_BEGIN
;
372 pos
.QuadPart
= offset
;
373 if (!MySetFilePointerEx(hFile
, pos
, NULL
, FILE_CURRENT
))
375 DWORD dwErr
= GetLastError();
376 ((WIN32FILE_IOWIN
*)stream
) -> error
=(int)dwErr
;
385 int ZCALLBACK
win32_close_file_func (voidpf opaque
, voidpf stream
)
392 hFile
= ((WIN32FILE_IOWIN
*)stream
) -> hf
;
403 int ZCALLBACK
win32_error_file_func (voidpf opaque
,voidpf stream
)
408 ret
= ((WIN32FILE_IOWIN
*)stream
) -> error
;
413 void fill_win32_filefunc (zlib_filefunc_def
* pzlib_filefunc_def
)
415 pzlib_filefunc_def
->zopen_file
= win32_open_file_func
;
416 pzlib_filefunc_def
->zread_file
= win32_read_file_func
;
417 pzlib_filefunc_def
->zwrite_file
= win32_write_file_func
;
418 pzlib_filefunc_def
->ztell_file
= win32_tell_file_func
;
419 pzlib_filefunc_def
->zseek_file
= win32_seek_file_func
;
420 pzlib_filefunc_def
->zclose_file
= win32_close_file_func
;
421 pzlib_filefunc_def
->zerror_file
= win32_error_file_func
;
422 pzlib_filefunc_def
->opaque
= NULL
;
425 void fill_win32_filefunc64(zlib_filefunc64_def
* pzlib_filefunc_def
)
427 pzlib_filefunc_def
->zopen64_file
= win32_open64_file_func
;
428 pzlib_filefunc_def
->zread_file
= win32_read_file_func
;
429 pzlib_filefunc_def
->zwrite_file
= win32_write_file_func
;
430 pzlib_filefunc_def
->ztell64_file
= win32_tell64_file_func
;
431 pzlib_filefunc_def
->zseek64_file
= win32_seek64_file_func
;
432 pzlib_filefunc_def
->zclose_file
= win32_close_file_func
;
433 pzlib_filefunc_def
->zerror_file
= win32_error_file_func
;
434 pzlib_filefunc_def
->opaque
= NULL
;
438 void fill_win32_filefunc64A(zlib_filefunc64_def
* pzlib_filefunc_def
)
440 pzlib_filefunc_def
->zopen64_file
= win32_open64_file_funcA
;
441 pzlib_filefunc_def
->zread_file
= win32_read_file_func
;
442 pzlib_filefunc_def
->zwrite_file
= win32_write_file_func
;
443 pzlib_filefunc_def
->ztell64_file
= win32_tell64_file_func
;
444 pzlib_filefunc_def
->zseek64_file
= win32_seek64_file_func
;
445 pzlib_filefunc_def
->zclose_file
= win32_close_file_func
;
446 pzlib_filefunc_def
->zerror_file
= win32_error_file_func
;
447 pzlib_filefunc_def
->opaque
= NULL
;
451 void fill_win32_filefunc64W(zlib_filefunc64_def
* pzlib_filefunc_def
)
453 pzlib_filefunc_def
->zopen64_file
= win32_open64_file_funcW
;
454 pzlib_filefunc_def
->zread_file
= win32_read_file_func
;
455 pzlib_filefunc_def
->zwrite_file
= win32_write_file_func
;
456 pzlib_filefunc_def
->ztell64_file
= win32_tell64_file_func
;
457 pzlib_filefunc_def
->zseek64_file
= win32_seek64_file_func
;
458 pzlib_filefunc_def
->zclose_file
= win32_close_file_func
;
459 pzlib_filefunc_def
->zerror_file
= win32_error_file_func
;
460 pzlib_filefunc_def
->opaque
= NULL
;