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 // see Include/shared/winapifamily.h in the Windows Kit
30 #if defined(WINAPI_FAMILY_PARTITION) && (!(defined(IOWIN32_USING_WINRT_API)))
31 #if WINAPI_FAMILY_ONE_PARTITION(WINAPI_FAMILY, WINAPI_PARTITION_APP)
32 #define IOWIN32_USING_WINRT_API 1
36 voidpf ZCALLBACK win32_open_file_func
OF((voidpf opaque
, const char* filename
, int mode
));
37 uLong ZCALLBACK win32_read_file_func
OF((voidpf opaque
, voidpf stream
, void* buf
, uLong size
));
38 uLong ZCALLBACK win32_write_file_func
OF((voidpf opaque
, voidpf stream
, const void* buf
, uLong size
));
39 ZPOS64_T ZCALLBACK win32_tell64_file_func
OF((voidpf opaque
, voidpf stream
));
40 long ZCALLBACK win32_seek64_file_func
OF((voidpf opaque
, voidpf stream
, ZPOS64_T offset
, int origin
));
41 int ZCALLBACK win32_close_file_func
OF((voidpf opaque
, voidpf stream
));
42 int ZCALLBACK win32_error_file_func
OF((voidpf opaque
, voidpf stream
));
51 static void win32_translate_open_mode(int mode
,
52 DWORD
* lpdwDesiredAccess
,
53 DWORD
* lpdwCreationDisposition
,
55 DWORD
* lpdwFlagsAndAttributes
)
57 *lpdwDesiredAccess
= *lpdwShareMode
= *lpdwFlagsAndAttributes
= *lpdwCreationDisposition
= 0;
59 if ((mode
& ZLIB_FILEFUNC_MODE_READWRITEFILTER
)==ZLIB_FILEFUNC_MODE_READ
)
61 *lpdwDesiredAccess
= GENERIC_READ
;
62 *lpdwCreationDisposition
= OPEN_EXISTING
;
63 *lpdwShareMode
= FILE_SHARE_READ
;
65 else if (mode
& ZLIB_FILEFUNC_MODE_EXISTING
)
67 *lpdwDesiredAccess
= GENERIC_WRITE
| GENERIC_READ
;
68 *lpdwCreationDisposition
= OPEN_EXISTING
;
70 else if (mode
& ZLIB_FILEFUNC_MODE_CREATE
)
72 *lpdwDesiredAccess
= GENERIC_WRITE
| GENERIC_READ
;
73 *lpdwCreationDisposition
= CREATE_ALWAYS
;
77 static voidpf
win32_build_iowin(HANDLE hFile
)
81 if ((hFile
!= NULL
) && (hFile
!= INVALID_HANDLE_VALUE
))
83 WIN32FILE_IOWIN w32fiow
;
86 ret
= malloc(sizeof(WIN32FILE_IOWIN
));
91 *((WIN32FILE_IOWIN
*)ret
) = w32fiow
;
96 voidpf ZCALLBACK
win32_open64_file_func (voidpf opaque
,const void* filename
,int mode
)
98 const char* mode_fopen
= NULL
;
99 DWORD dwDesiredAccess
,dwCreationDisposition
,dwShareMode
,dwFlagsAndAttributes
;
102 win32_translate_open_mode(mode
,&dwDesiredAccess
,&dwCreationDisposition
,&dwShareMode
,&dwFlagsAndAttributes
);
104 #ifdef IOWIN32_USING_WINRT_API
106 if ((filename
!=NULL
) && (dwDesiredAccess
!= 0))
107 hFile
= CreateFile2((LPCTSTR
)filename
, dwDesiredAccess
, dwShareMode
, dwCreationDisposition
, NULL
);
109 if ((filename
!=NULL
) && (dwDesiredAccess
!= 0))
111 WCHAR filenameW
[FILENAME_MAX
+ 0x200 + 1];
112 MultiByteToWideChar(CP_ACP
,0,(const char*)filename
,-1,filenameW
,FILENAME_MAX
+ 0x200);
113 hFile
= CreateFile2(filenameW
, dwDesiredAccess
, dwShareMode
, dwCreationDisposition
, NULL
);
117 if ((filename
!=NULL
) && (dwDesiredAccess
!= 0))
118 hFile
= CreateFile((LPCTSTR
)filename
, dwDesiredAccess
, dwShareMode
, NULL
, dwCreationDisposition
, dwFlagsAndAttributes
, NULL
);
121 return win32_build_iowin(hFile
);
125 voidpf ZCALLBACK
win32_open64_file_funcA (voidpf opaque
,const void* filename
,int mode
)
127 const char* mode_fopen
= NULL
;
128 DWORD dwDesiredAccess
,dwCreationDisposition
,dwShareMode
,dwFlagsAndAttributes
;
131 win32_translate_open_mode(mode
,&dwDesiredAccess
,&dwCreationDisposition
,&dwShareMode
,&dwFlagsAndAttributes
);
133 #ifdef IOWIN32_USING_WINRT_API
134 if ((filename
!=NULL
) && (dwDesiredAccess
!= 0))
136 WCHAR filenameW
[FILENAME_MAX
+ 0x200 + 1];
137 MultiByteToWideChar(CP_ACP
,0,(const char*)filename
,-1,filenameW
,FILENAME_MAX
+ 0x200);
138 hFile
= CreateFile2(filenameW
, dwDesiredAccess
, dwShareMode
, dwCreationDisposition
, NULL
);
141 if ((filename
!=NULL
) && (dwDesiredAccess
!= 0))
142 hFile
= CreateFileA((LPCSTR
)filename
, dwDesiredAccess
, dwShareMode
, NULL
, dwCreationDisposition
, dwFlagsAndAttributes
, NULL
);
145 return win32_build_iowin(hFile
);
149 voidpf ZCALLBACK
win32_open64_file_funcW (voidpf opaque
,const void* filename
,int mode
)
151 const char* mode_fopen
= NULL
;
152 DWORD dwDesiredAccess
,dwCreationDisposition
,dwShareMode
,dwFlagsAndAttributes
;
155 win32_translate_open_mode(mode
,&dwDesiredAccess
,&dwCreationDisposition
,&dwShareMode
,&dwFlagsAndAttributes
);
157 #ifdef IOWIN32_USING_WINRT_API
158 if ((filename
!=NULL
) && (dwDesiredAccess
!= 0))
159 hFile
= CreateFile2((LPCWSTR
)filename
, dwDesiredAccess
, dwShareMode
, dwCreationDisposition
,NULL
);
161 if ((filename
!=NULL
) && (dwDesiredAccess
!= 0))
162 hFile
= CreateFileW((LPCWSTR
)filename
, dwDesiredAccess
, dwShareMode
, NULL
, dwCreationDisposition
, dwFlagsAndAttributes
, NULL
);
165 return win32_build_iowin(hFile
);
169 voidpf ZCALLBACK
win32_open_file_func (voidpf opaque
,const char* filename
,int mode
)
171 const char* mode_fopen
= NULL
;
172 DWORD dwDesiredAccess
,dwCreationDisposition
,dwShareMode
,dwFlagsAndAttributes
;
175 win32_translate_open_mode(mode
,&dwDesiredAccess
,&dwCreationDisposition
,&dwShareMode
,&dwFlagsAndAttributes
);
177 #ifdef IOWIN32_USING_WINRT_API
179 if ((filename
!=NULL
) && (dwDesiredAccess
!= 0))
180 hFile
= CreateFile2((LPCTSTR
)filename
, dwDesiredAccess
, dwShareMode
, dwCreationDisposition
, NULL
);
182 if ((filename
!=NULL
) && (dwDesiredAccess
!= 0))
184 WCHAR filenameW
[FILENAME_MAX
+ 0x200 + 1];
185 MultiByteToWideChar(CP_ACP
,0,(const char*)filename
,-1,filenameW
,FILENAME_MAX
+ 0x200);
186 hFile
= CreateFile2(filenameW
, dwDesiredAccess
, dwShareMode
, dwCreationDisposition
, NULL
);
190 if ((filename
!=NULL
) && (dwDesiredAccess
!= 0))
191 hFile
= CreateFile((LPCTSTR
)filename
, dwDesiredAccess
, dwShareMode
, NULL
, dwCreationDisposition
, dwFlagsAndAttributes
, NULL
);
194 return win32_build_iowin(hFile
);
198 uLong ZCALLBACK
win32_read_file_func (voidpf opaque
, voidpf stream
, void* buf
,uLong size
)
203 hFile
= ((WIN32FILE_IOWIN
*)stream
) -> hf
;
207 if (!ReadFile(hFile
, buf
, size
, &ret
, NULL
))
209 DWORD dwErr
= GetLastError();
210 if (dwErr
== ERROR_HANDLE_EOF
)
212 ((WIN32FILE_IOWIN
*)stream
) -> error
=(int)dwErr
;
220 uLong ZCALLBACK
win32_write_file_func (voidpf opaque
,voidpf stream
,const void* buf
,uLong size
)
225 hFile
= ((WIN32FILE_IOWIN
*)stream
) -> hf
;
229 if (!WriteFile(hFile
, buf
, size
, &ret
, NULL
))
231 DWORD dwErr
= GetLastError();
232 if (dwErr
== ERROR_HANDLE_EOF
)
234 ((WIN32FILE_IOWIN
*)stream
) -> error
=(int)dwErr
;
241 static BOOL
MySetFilePointerEx(HANDLE hFile
, LARGE_INTEGER pos
, LARGE_INTEGER
*newPos
, DWORD dwMoveMethod
)
243 #ifdef IOWIN32_USING_WINRT_API
244 return SetFilePointerEx(hFile
, pos
, newPos
, dwMoveMethod
);
246 LONG lHigh
= pos
.HighPart
;
247 DWORD dwNewPos
= SetFilePointer(hFile
, pos
.LowPart
, &lHigh
, dwMoveMethod
);
249 if (dwNewPos
== 0xFFFFFFFF)
250 if (GetLastError() != NO_ERROR
)
252 if ((newPos
!= NULL
) && (fOk
))
254 newPos
->LowPart
= dwNewPos
;
255 newPos
->HighPart
= lHigh
;
261 long ZCALLBACK
win32_tell_file_func (voidpf opaque
,voidpf stream
)
266 hFile
= ((WIN32FILE_IOWIN
*)stream
) -> hf
;
272 if (!MySetFilePointerEx(hFile
, pos
, &pos
, FILE_CURRENT
))
274 DWORD dwErr
= GetLastError();
275 ((WIN32FILE_IOWIN
*)stream
) -> error
=(int)dwErr
;
279 ret
=(long)pos
.LowPart
;
284 ZPOS64_T ZCALLBACK
win32_tell64_file_func (voidpf opaque
, voidpf stream
)
286 ZPOS64_T ret
= (ZPOS64_T
)-1;
289 hFile
= ((WIN32FILE_IOWIN
*)stream
)->hf
;
296 if (!MySetFilePointerEx(hFile
, pos
, &pos
, FILE_CURRENT
))
298 DWORD dwErr
= GetLastError();
299 ((WIN32FILE_IOWIN
*)stream
) -> error
=(int)dwErr
;
309 long ZCALLBACK
win32_seek_file_func (voidpf opaque
,voidpf stream
,uLong offset
,int origin
)
311 DWORD dwMoveMethod
=0xFFFFFFFF;
316 hFile
= ((WIN32FILE_IOWIN
*)stream
) -> hf
;
319 case ZLIB_FILEFUNC_SEEK_CUR
:
320 dwMoveMethod
= FILE_CURRENT
;
322 case ZLIB_FILEFUNC_SEEK_END
:
323 dwMoveMethod
= FILE_END
;
325 case ZLIB_FILEFUNC_SEEK_SET
:
326 dwMoveMethod
= FILE_BEGIN
;
334 pos
.QuadPart
= offset
;
335 if (!MySetFilePointerEx(hFile
, pos
, NULL
, dwMoveMethod
))
337 DWORD dwErr
= GetLastError();
338 ((WIN32FILE_IOWIN
*)stream
) -> error
=(int)dwErr
;
347 long ZCALLBACK
win32_seek64_file_func (voidpf opaque
, voidpf stream
,ZPOS64_T offset
,int origin
)
349 DWORD dwMoveMethod
=0xFFFFFFFF;
354 hFile
= ((WIN32FILE_IOWIN
*)stream
)->hf
;
358 case ZLIB_FILEFUNC_SEEK_CUR
:
359 dwMoveMethod
= FILE_CURRENT
;
361 case ZLIB_FILEFUNC_SEEK_END
:
362 dwMoveMethod
= FILE_END
;
364 case ZLIB_FILEFUNC_SEEK_SET
:
365 dwMoveMethod
= FILE_BEGIN
;
373 pos
.QuadPart
= offset
;
374 if (!MySetFilePointerEx(hFile
, pos
, NULL
, dwMoveMethod
))
376 DWORD dwErr
= GetLastError();
377 ((WIN32FILE_IOWIN
*)stream
) -> error
=(int)dwErr
;
386 int ZCALLBACK
win32_close_file_func (voidpf opaque
, voidpf stream
)
393 hFile
= ((WIN32FILE_IOWIN
*)stream
) -> hf
;
404 int ZCALLBACK
win32_error_file_func (voidpf opaque
,voidpf stream
)
409 ret
= ((WIN32FILE_IOWIN
*)stream
) -> error
;
414 void fill_win32_filefunc (zlib_filefunc_def
* pzlib_filefunc_def
)
416 pzlib_filefunc_def
->zopen_file
= win32_open_file_func
;
417 pzlib_filefunc_def
->zread_file
= win32_read_file_func
;
418 pzlib_filefunc_def
->zwrite_file
= win32_write_file_func
;
419 pzlib_filefunc_def
->ztell_file
= win32_tell_file_func
;
420 pzlib_filefunc_def
->zseek_file
= win32_seek_file_func
;
421 pzlib_filefunc_def
->zclose_file
= win32_close_file_func
;
422 pzlib_filefunc_def
->zerror_file
= win32_error_file_func
;
423 pzlib_filefunc_def
->opaque
= NULL
;
426 void fill_win32_filefunc64(zlib_filefunc64_def
* pzlib_filefunc_def
)
428 pzlib_filefunc_def
->zopen64_file
= win32_open64_file_func
;
429 pzlib_filefunc_def
->zread_file
= win32_read_file_func
;
430 pzlib_filefunc_def
->zwrite_file
= win32_write_file_func
;
431 pzlib_filefunc_def
->ztell64_file
= win32_tell64_file_func
;
432 pzlib_filefunc_def
->zseek64_file
= win32_seek64_file_func
;
433 pzlib_filefunc_def
->zclose_file
= win32_close_file_func
;
434 pzlib_filefunc_def
->zerror_file
= win32_error_file_func
;
435 pzlib_filefunc_def
->opaque
= NULL
;
439 void fill_win32_filefunc64A(zlib_filefunc64_def
* pzlib_filefunc_def
)
441 pzlib_filefunc_def
->zopen64_file
= win32_open64_file_funcA
;
442 pzlib_filefunc_def
->zread_file
= win32_read_file_func
;
443 pzlib_filefunc_def
->zwrite_file
= win32_write_file_func
;
444 pzlib_filefunc_def
->ztell64_file
= win32_tell64_file_func
;
445 pzlib_filefunc_def
->zseek64_file
= win32_seek64_file_func
;
446 pzlib_filefunc_def
->zclose_file
= win32_close_file_func
;
447 pzlib_filefunc_def
->zerror_file
= win32_error_file_func
;
448 pzlib_filefunc_def
->opaque
= NULL
;
452 void fill_win32_filefunc64W(zlib_filefunc64_def
* pzlib_filefunc_def
)
454 pzlib_filefunc_def
->zopen64_file
= win32_open64_file_funcW
;
455 pzlib_filefunc_def
->zread_file
= win32_read_file_func
;
456 pzlib_filefunc_def
->zwrite_file
= win32_write_file_func
;
457 pzlib_filefunc_def
->ztell64_file
= win32_tell64_file_func
;
458 pzlib_filefunc_def
->zseek64_file
= win32_seek64_file_func
;
459 pzlib_filefunc_def
->zclose_file
= win32_close_file_func
;
460 pzlib_filefunc_def
->zerror_file
= win32_error_file_func
;
461 pzlib_filefunc_def
->opaque
= NULL
;