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)
28 voidpf ZCALLBACK win32_open_file_func
OF((voidpf opaque
, const char* filename
, int mode
));
29 uLong ZCALLBACK win32_read_file_func
OF((voidpf opaque
, voidpf stream
, void* buf
, uLong size
));
30 uLong ZCALLBACK win32_write_file_func
OF((voidpf opaque
, voidpf stream
, const void* buf
, uLong size
));
31 ZPOS64_T ZCALLBACK win32_tell64_file_func
OF((voidpf opaque
, voidpf stream
));
32 long ZCALLBACK win32_seek64_file_func
OF((voidpf opaque
, voidpf stream
, ZPOS64_T offset
, int origin
));
33 int ZCALLBACK win32_close_file_func
OF((voidpf opaque
, voidpf stream
));
34 int ZCALLBACK win32_error_file_func
OF((voidpf opaque
, voidpf stream
));
43 static void win32_translate_open_mode(int mode
,
44 DWORD
* lpdwDesiredAccess
,
45 DWORD
* lpdwCreationDisposition
,
47 DWORD
* lpdwFlagsAndAttributes
)
49 *lpdwDesiredAccess
= *lpdwShareMode
= *lpdwFlagsAndAttributes
= *lpdwCreationDisposition
= 0;
51 if ((mode
& ZLIB_FILEFUNC_MODE_READWRITEFILTER
)==ZLIB_FILEFUNC_MODE_READ
)
53 *lpdwDesiredAccess
= GENERIC_READ
;
54 *lpdwCreationDisposition
= OPEN_EXISTING
;
55 *lpdwShareMode
= FILE_SHARE_READ
;
57 else if (mode
& ZLIB_FILEFUNC_MODE_EXISTING
)
59 *lpdwDesiredAccess
= GENERIC_WRITE
| GENERIC_READ
;
60 *lpdwCreationDisposition
= OPEN_EXISTING
;
62 else if (mode
& ZLIB_FILEFUNC_MODE_CREATE
)
64 *lpdwDesiredAccess
= GENERIC_WRITE
| GENERIC_READ
;
65 *lpdwCreationDisposition
= CREATE_ALWAYS
;
69 static voidpf
win32_build_iowin(HANDLE hFile
)
73 if ((hFile
!= NULL
) && (hFile
!= INVALID_HANDLE_VALUE
))
75 WIN32FILE_IOWIN w32fiow
;
78 ret
= malloc(sizeof(WIN32FILE_IOWIN
));
83 *((WIN32FILE_IOWIN
*)ret
) = w32fiow
;
88 voidpf ZCALLBACK
win32_open64_file_func (voidpf opaque
,const void* filename
,int mode
)
90 const char* mode_fopen
= NULL
;
91 DWORD dwDesiredAccess
,dwCreationDisposition
,dwShareMode
,dwFlagsAndAttributes
;
94 win32_translate_open_mode(mode
,&dwDesiredAccess
,&dwCreationDisposition
,&dwShareMode
,&dwFlagsAndAttributes
);
96 if ((filename
!=NULL
) && (dwDesiredAccess
!= 0))
97 hFile
= CreateFile((LPCTSTR
)filename
, dwDesiredAccess
, dwShareMode
, NULL
, dwCreationDisposition
, dwFlagsAndAttributes
, NULL
);
99 return win32_build_iowin(hFile
);
103 voidpf ZCALLBACK
win32_open64_file_funcA (voidpf opaque
,const void* filename
,int mode
)
105 const char* mode_fopen
= NULL
;
106 DWORD dwDesiredAccess
,dwCreationDisposition
,dwShareMode
,dwFlagsAndAttributes
;
109 win32_translate_open_mode(mode
,&dwDesiredAccess
,&dwCreationDisposition
,&dwShareMode
,&dwFlagsAndAttributes
);
111 if ((filename
!=NULL
) && (dwDesiredAccess
!= 0))
112 hFile
= CreateFileA((LPCSTR
)filename
, dwDesiredAccess
, dwShareMode
, NULL
, dwCreationDisposition
, dwFlagsAndAttributes
, NULL
);
114 return win32_build_iowin(hFile
);
118 voidpf ZCALLBACK
win32_open64_file_funcW (voidpf opaque
,const void* filename
,int mode
)
120 const char* mode_fopen
= NULL
;
121 DWORD dwDesiredAccess
,dwCreationDisposition
,dwShareMode
,dwFlagsAndAttributes
;
124 win32_translate_open_mode(mode
,&dwDesiredAccess
,&dwCreationDisposition
,&dwShareMode
,&dwFlagsAndAttributes
);
126 if ((filename
!=NULL
) && (dwDesiredAccess
!= 0))
127 hFile
= CreateFileW((LPCWSTR
)filename
, dwDesiredAccess
, dwShareMode
, NULL
, dwCreationDisposition
, dwFlagsAndAttributes
, NULL
);
129 return win32_build_iowin(hFile
);
133 voidpf ZCALLBACK
win32_open_file_func (voidpf opaque
,const char* filename
,int mode
)
135 const char* mode_fopen
= NULL
;
136 DWORD dwDesiredAccess
,dwCreationDisposition
,dwShareMode
,dwFlagsAndAttributes
;
139 win32_translate_open_mode(mode
,&dwDesiredAccess
,&dwCreationDisposition
,&dwShareMode
,&dwFlagsAndAttributes
);
141 if ((filename
!=NULL
) && (dwDesiredAccess
!= 0))
142 hFile
= CreateFile((LPCTSTR
)filename
, dwDesiredAccess
, dwShareMode
, NULL
, dwCreationDisposition
, dwFlagsAndAttributes
, NULL
);
144 return win32_build_iowin(hFile
);
148 uLong ZCALLBACK
win32_read_file_func (voidpf opaque
, voidpf stream
, void* buf
,uLong size
)
153 hFile
= ((WIN32FILE_IOWIN
*)stream
) -> hf
;
157 if (!ReadFile(hFile
, buf
, size
, &ret
, NULL
))
159 DWORD dwErr
= GetLastError();
160 if (dwErr
== ERROR_HANDLE_EOF
)
162 ((WIN32FILE_IOWIN
*)stream
) -> error
=(int)dwErr
;
170 uLong ZCALLBACK
win32_write_file_func (voidpf opaque
,voidpf stream
,const void* buf
,uLong size
)
175 hFile
= ((WIN32FILE_IOWIN
*)stream
) -> hf
;
179 if (!WriteFile(hFile
, buf
, size
, &ret
, NULL
))
181 DWORD dwErr
= GetLastError();
182 if (dwErr
== ERROR_HANDLE_EOF
)
184 ((WIN32FILE_IOWIN
*)stream
) -> error
=(int)dwErr
;
191 long ZCALLBACK
win32_tell_file_func (voidpf opaque
,voidpf stream
)
196 hFile
= ((WIN32FILE_IOWIN
*)stream
) -> hf
;
199 DWORD dwSet
= SetFilePointer(hFile
, 0, NULL
, FILE_CURRENT
);
200 if (dwSet
== INVALID_SET_FILE_POINTER
)
202 DWORD dwErr
= GetLastError();
203 ((WIN32FILE_IOWIN
*)stream
) -> error
=(int)dwErr
;
212 ZPOS64_T ZCALLBACK
win32_tell64_file_func (voidpf opaque
, voidpf stream
)
214 ZPOS64_T ret
= (ZPOS64_T
)-1;
217 hFile
= ((WIN32FILE_IOWIN
*)stream
)->hf
;
223 li
.u
.LowPart
= SetFilePointer(hFile
, li
.u
.LowPart
, &li
.u
.HighPart
, FILE_CURRENT
);
224 if ( (li
.LowPart
== 0xFFFFFFFF) && (GetLastError() != NO_ERROR
))
226 DWORD dwErr
= GetLastError();
227 ((WIN32FILE_IOWIN
*)stream
) -> error
=(int)dwErr
;
237 long ZCALLBACK
win32_seek_file_func (voidpf opaque
,voidpf stream
,uLong offset
,int origin
)
239 DWORD dwMoveMethod
=0xFFFFFFFF;
244 hFile
= ((WIN32FILE_IOWIN
*)stream
) -> hf
;
247 case ZLIB_FILEFUNC_SEEK_CUR
:
248 dwMoveMethod
= FILE_CURRENT
;
250 case ZLIB_FILEFUNC_SEEK_END
:
251 dwMoveMethod
= FILE_END
;
253 case ZLIB_FILEFUNC_SEEK_SET
:
254 dwMoveMethod
= FILE_BEGIN
;
261 DWORD dwSet
= SetFilePointer(hFile
, offset
, NULL
, dwMoveMethod
);
262 if (dwSet
== INVALID_SET_FILE_POINTER
)
264 DWORD dwErr
= GetLastError();
265 ((WIN32FILE_IOWIN
*)stream
) -> error
=(int)dwErr
;
274 long ZCALLBACK
win32_seek64_file_func (voidpf opaque
, voidpf stream
,ZPOS64_T offset
,int origin
)
276 DWORD dwMoveMethod
=0xFFFFFFFF;
281 hFile
= ((WIN32FILE_IOWIN
*)stream
)->hf
;
285 case ZLIB_FILEFUNC_SEEK_CUR
:
286 dwMoveMethod
= FILE_CURRENT
;
288 case ZLIB_FILEFUNC_SEEK_END
:
289 dwMoveMethod
= FILE_END
;
291 case ZLIB_FILEFUNC_SEEK_SET
:
292 dwMoveMethod
= FILE_BEGIN
;
299 LARGE_INTEGER
* li
= (LARGE_INTEGER
*)&offset
;
300 DWORD dwSet
= SetFilePointer(hFile
, li
->u
.LowPart
, &li
->u
.HighPart
, dwMoveMethod
);
301 if (dwSet
== INVALID_SET_FILE_POINTER
)
303 DWORD dwErr
= GetLastError();
304 ((WIN32FILE_IOWIN
*)stream
) -> error
=(int)dwErr
;
313 int ZCALLBACK
win32_close_file_func (voidpf opaque
, voidpf stream
)
320 hFile
= ((WIN32FILE_IOWIN
*)stream
) -> hf
;
331 int ZCALLBACK
win32_error_file_func (voidpf opaque
,voidpf stream
)
336 ret
= ((WIN32FILE_IOWIN
*)stream
) -> error
;
341 void fill_win32_filefunc (zlib_filefunc_def
* pzlib_filefunc_def
)
343 pzlib_filefunc_def
->zopen_file
= win32_open_file_func
;
344 pzlib_filefunc_def
->zread_file
= win32_read_file_func
;
345 pzlib_filefunc_def
->zwrite_file
= win32_write_file_func
;
346 pzlib_filefunc_def
->ztell_file
= win32_tell_file_func
;
347 pzlib_filefunc_def
->zseek_file
= win32_seek_file_func
;
348 pzlib_filefunc_def
->zclose_file
= win32_close_file_func
;
349 pzlib_filefunc_def
->zerror_file
= win32_error_file_func
;
350 pzlib_filefunc_def
->opaque
= NULL
;
353 void fill_win32_filefunc64(zlib_filefunc64_def
* pzlib_filefunc_def
)
355 pzlib_filefunc_def
->zopen64_file
= win32_open64_file_func
;
356 pzlib_filefunc_def
->zread_file
= win32_read_file_func
;
357 pzlib_filefunc_def
->zwrite_file
= win32_write_file_func
;
358 pzlib_filefunc_def
->ztell64_file
= win32_tell64_file_func
;
359 pzlib_filefunc_def
->zseek64_file
= win32_seek64_file_func
;
360 pzlib_filefunc_def
->zclose_file
= win32_close_file_func
;
361 pzlib_filefunc_def
->zerror_file
= win32_error_file_func
;
362 pzlib_filefunc_def
->opaque
= NULL
;
366 void fill_win32_filefunc64A(zlib_filefunc64_def
* pzlib_filefunc_def
)
368 pzlib_filefunc_def
->zopen64_file
= win32_open64_file_funcA
;
369 pzlib_filefunc_def
->zread_file
= win32_read_file_func
;
370 pzlib_filefunc_def
->zwrite_file
= win32_write_file_func
;
371 pzlib_filefunc_def
->ztell64_file
= win32_tell64_file_func
;
372 pzlib_filefunc_def
->zseek64_file
= win32_seek64_file_func
;
373 pzlib_filefunc_def
->zclose_file
= win32_close_file_func
;
374 pzlib_filefunc_def
->zerror_file
= win32_error_file_func
;
375 pzlib_filefunc_def
->opaque
= NULL
;
379 void fill_win32_filefunc64W(zlib_filefunc64_def
* pzlib_filefunc_def
)
381 pzlib_filefunc_def
->zopen64_file
= win32_open64_file_funcW
;
382 pzlib_filefunc_def
->zread_file
= win32_read_file_func
;
383 pzlib_filefunc_def
->zwrite_file
= win32_write_file_func
;
384 pzlib_filefunc_def
->ztell64_file
= win32_tell64_file_func
;
385 pzlib_filefunc_def
->zseek64_file
= win32_seek64_file_func
;
386 pzlib_filefunc_def
->zclose_file
= win32_close_file_func
;
387 pzlib_filefunc_def
->zerror_file
= win32_error_file_func
;
388 pzlib_filefunc_def
->opaque
= NULL
;