1 /* $NetBSD: iowin32.c,v 1.1.1.1 2006/01/14 20:10:57 christos Exp $ */
3 /* iowin32.c -- IO base function header for compress/uncompress .zip
4 files using zlib + zip or unzip API
5 This IO API version uses the Win32 API (for Microsoft Windows)
7 Version 1.01e, February 12th, 2005
9 Copyright (C) 1998-2005 Gilles Vollant
18 #ifndef INVALID_HANDLE_VALUE
19 #define INVALID_HANDLE_VALUE (0xFFFFFFFF)
22 #ifndef INVALID_SET_FILE_POINTER
23 #define INVALID_SET_FILE_POINTER ((DWORD)-1)
26 voidpf ZCALLBACK win32_open_file_func
OF((
31 uLong ZCALLBACK win32_read_file_func
OF((
37 uLong ZCALLBACK win32_write_file_func
OF((
43 long ZCALLBACK win32_tell_file_func
OF((
47 long ZCALLBACK win32_seek_file_func
OF((
53 int ZCALLBACK win32_close_file_func
OF((
57 int ZCALLBACK win32_error_file_func
OF((
67 voidpf ZCALLBACK
win32_open_file_func (opaque
, filename
, mode
)
72 const char* mode_fopen
= NULL
;
73 DWORD dwDesiredAccess
,dwCreationDisposition
,dwShareMode
,dwFlagsAndAttributes
;
77 dwDesiredAccess
= dwShareMode
= dwFlagsAndAttributes
= 0;
79 if ((mode
& ZLIB_FILEFUNC_MODE_READWRITEFILTER
)==ZLIB_FILEFUNC_MODE_READ
)
81 dwDesiredAccess
= GENERIC_READ
;
82 dwCreationDisposition
= OPEN_EXISTING
;
83 dwShareMode
= FILE_SHARE_READ
;
86 if (mode
& ZLIB_FILEFUNC_MODE_EXISTING
)
88 dwDesiredAccess
= GENERIC_WRITE
| GENERIC_READ
;
89 dwCreationDisposition
= OPEN_EXISTING
;
92 if (mode
& ZLIB_FILEFUNC_MODE_CREATE
)
94 dwDesiredAccess
= GENERIC_WRITE
| GENERIC_READ
;
95 dwCreationDisposition
= CREATE_ALWAYS
;
98 if ((filename
!=NULL
) && (dwDesiredAccess
!= 0))
99 hFile
= CreateFile((LPCTSTR
)filename
, dwDesiredAccess
, dwShareMode
, NULL
,
100 dwCreationDisposition
, dwFlagsAndAttributes
, NULL
);
102 if (hFile
== INVALID_HANDLE_VALUE
)
107 WIN32FILE_IOWIN w32fiow
;
110 ret
= malloc(sizeof(WIN32FILE_IOWIN
));
113 else *((WIN32FILE_IOWIN
*)ret
) = w32fiow
;
119 uLong ZCALLBACK
win32_read_file_func (opaque
, stream
, buf
, size
)
128 hFile
= ((WIN32FILE_IOWIN
*)stream
) -> hf
;
130 if (!ReadFile(hFile
, buf
, size
, &ret
, NULL
))
132 DWORD dwErr
= GetLastError();
133 if (dwErr
== ERROR_HANDLE_EOF
)
135 ((WIN32FILE_IOWIN
*)stream
) -> error
=(int)dwErr
;
142 uLong ZCALLBACK
win32_write_file_func (opaque
, stream
, buf
, size
)
151 hFile
= ((WIN32FILE_IOWIN
*)stream
) -> hf
;
154 if (!WriteFile(hFile
, buf
, size
, &ret
, NULL
))
156 DWORD dwErr
= GetLastError();
157 if (dwErr
== ERROR_HANDLE_EOF
)
159 ((WIN32FILE_IOWIN
*)stream
) -> error
=(int)dwErr
;
165 long ZCALLBACK
win32_tell_file_func (opaque
, stream
)
172 hFile
= ((WIN32FILE_IOWIN
*)stream
) -> hf
;
175 DWORD dwSet
= SetFilePointer(hFile
, 0, NULL
, FILE_CURRENT
);
176 if (dwSet
== INVALID_SET_FILE_POINTER
)
178 DWORD dwErr
= GetLastError();
179 ((WIN32FILE_IOWIN
*)stream
) -> error
=(int)dwErr
;
188 long ZCALLBACK
win32_seek_file_func (opaque
, stream
, offset
, origin
)
194 DWORD dwMoveMethod
=0xFFFFFFFF;
199 hFile
= ((WIN32FILE_IOWIN
*)stream
) -> hf
;
202 case ZLIB_FILEFUNC_SEEK_CUR
:
203 dwMoveMethod
= FILE_CURRENT
;
205 case ZLIB_FILEFUNC_SEEK_END
:
206 dwMoveMethod
= FILE_END
;
208 case ZLIB_FILEFUNC_SEEK_SET
:
209 dwMoveMethod
= FILE_BEGIN
;
216 DWORD dwSet
= SetFilePointer(hFile
, offset
, NULL
, dwMoveMethod
);
217 if (dwSet
== INVALID_SET_FILE_POINTER
)
219 DWORD dwErr
= GetLastError();
220 ((WIN32FILE_IOWIN
*)stream
) -> error
=(int)dwErr
;
229 int ZCALLBACK
win32_close_file_func (opaque
, stream
)
238 hFile
= ((WIN32FILE_IOWIN
*)stream
) -> hf
;
249 int ZCALLBACK
win32_error_file_func (opaque
, stream
)
256 ret
= ((WIN32FILE_IOWIN
*)stream
) -> error
;
261 void fill_win32_filefunc (pzlib_filefunc_def
)
262 zlib_filefunc_def
* pzlib_filefunc_def
;
264 pzlib_filefunc_def
->zopen_file
= win32_open_file_func
;
265 pzlib_filefunc_def
->zread_file
= win32_read_file_func
;
266 pzlib_filefunc_def
->zwrite_file
= win32_write_file_func
;
267 pzlib_filefunc_def
->ztell_file
= win32_tell_file_func
;
268 pzlib_filefunc_def
->zseek_file
= win32_seek_file_func
;
269 pzlib_filefunc_def
->zclose_file
= win32_close_file_func
;
270 pzlib_filefunc_def
->zerror_file
= win32_error_file_func
;
271 pzlib_filefunc_def
->opaque
=NULL
;