Fixes for Android GN build input/outputs
[chromium-blink-merge.git] / third_party / zlib / contrib / minizip / iowin32.c
blob6a2a883be7225417ac14d9496e5604a51bf650f1
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
14 #include <stdlib.h>
16 #include "zlib.h"
17 #include "ioapi.h"
18 #include "iowin32.h"
20 #ifndef INVALID_HANDLE_VALUE
21 #define INVALID_HANDLE_VALUE (0xFFFFFFFF)
22 #endif
24 #ifndef INVALID_SET_FILE_POINTER
25 #define INVALID_SET_FILE_POINTER ((DWORD)-1)
26 #endif
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));
36 typedef struct
38 HANDLE hf;
39 int error;
40 } WIN32FILE_IOWIN;
43 static void win32_translate_open_mode(int mode,
44 DWORD* lpdwDesiredAccess,
45 DWORD* lpdwCreationDisposition,
46 DWORD* lpdwShareMode,
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)
71 voidpf ret=NULL;
73 if ((hFile != NULL) && (hFile != INVALID_HANDLE_VALUE))
75 WIN32FILE_IOWIN w32fiow;
76 w32fiow.hf = hFile;
77 w32fiow.error = 0;
78 ret = malloc(sizeof(WIN32FILE_IOWIN));
80 if (ret==NULL)
81 CloseHandle(hFile);
82 else
83 *((WIN32FILE_IOWIN*)ret) = w32fiow;
85 return ret;
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 ;
92 HANDLE hFile = NULL;
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 ;
107 HANDLE hFile = NULL;
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 ;
122 HANDLE hFile = NULL;
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 ;
137 HANDLE hFile = NULL;
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)
150 uLong ret=0;
151 HANDLE hFile = NULL;
152 if (stream!=NULL)
153 hFile = ((WIN32FILE_IOWIN*)stream) -> hf;
155 if (hFile != NULL)
157 if (!ReadFile(hFile, buf, size, &ret, NULL))
159 DWORD dwErr = GetLastError();
160 if (dwErr == ERROR_HANDLE_EOF)
161 dwErr = 0;
162 ((WIN32FILE_IOWIN*)stream) -> error=(int)dwErr;
166 return ret;
170 uLong ZCALLBACK win32_write_file_func (voidpf opaque,voidpf stream,const void* buf,uLong size)
172 uLong ret=0;
173 HANDLE hFile = NULL;
174 if (stream!=NULL)
175 hFile = ((WIN32FILE_IOWIN*)stream) -> hf;
177 if (hFile != NULL)
179 if (!WriteFile(hFile, buf, size, &ret, NULL))
181 DWORD dwErr = GetLastError();
182 if (dwErr == ERROR_HANDLE_EOF)
183 dwErr = 0;
184 ((WIN32FILE_IOWIN*)stream) -> error=(int)dwErr;
188 return ret;
191 long ZCALLBACK win32_tell_file_func (voidpf opaque,voidpf stream)
193 long ret=-1;
194 HANDLE hFile = NULL;
195 if (stream!=NULL)
196 hFile = ((WIN32FILE_IOWIN*)stream) -> hf;
197 if (hFile != NULL)
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;
204 ret = -1;
206 else
207 ret=(long)dwSet;
209 return ret;
212 ZPOS64_T ZCALLBACK win32_tell64_file_func (voidpf opaque, voidpf stream)
214 ZPOS64_T ret= (ZPOS64_T)-1;
215 HANDLE hFile = NULL;
216 if (stream!=NULL)
217 hFile = ((WIN32FILE_IOWIN*)stream)->hf;
219 if (hFile)
221 LARGE_INTEGER li;
222 li.QuadPart = 0;
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;
228 ret = (ZPOS64_T)-1;
230 else
231 ret=li.QuadPart;
233 return ret;
237 long ZCALLBACK win32_seek_file_func (voidpf opaque,voidpf stream,uLong offset,int origin)
239 DWORD dwMoveMethod=0xFFFFFFFF;
240 HANDLE hFile = NULL;
242 long ret=-1;
243 if (stream!=NULL)
244 hFile = ((WIN32FILE_IOWIN*)stream) -> hf;
245 switch (origin)
247 case ZLIB_FILEFUNC_SEEK_CUR :
248 dwMoveMethod = FILE_CURRENT;
249 break;
250 case ZLIB_FILEFUNC_SEEK_END :
251 dwMoveMethod = FILE_END;
252 break;
253 case ZLIB_FILEFUNC_SEEK_SET :
254 dwMoveMethod = FILE_BEGIN;
255 break;
256 default: return -1;
259 if (hFile != NULL)
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;
266 ret = -1;
268 else
269 ret=0;
271 return ret;
274 long ZCALLBACK win32_seek64_file_func (voidpf opaque, voidpf stream,ZPOS64_T offset,int origin)
276 DWORD dwMoveMethod=0xFFFFFFFF;
277 HANDLE hFile = NULL;
278 long ret=-1;
280 if (stream!=NULL)
281 hFile = ((WIN32FILE_IOWIN*)stream)->hf;
283 switch (origin)
285 case ZLIB_FILEFUNC_SEEK_CUR :
286 dwMoveMethod = FILE_CURRENT;
287 break;
288 case ZLIB_FILEFUNC_SEEK_END :
289 dwMoveMethod = FILE_END;
290 break;
291 case ZLIB_FILEFUNC_SEEK_SET :
292 dwMoveMethod = FILE_BEGIN;
293 break;
294 default: return -1;
297 if (hFile)
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;
305 ret = -1;
307 else
308 ret=0;
310 return ret;
313 int ZCALLBACK win32_close_file_func (voidpf opaque, voidpf stream)
315 int ret=-1;
317 if (stream!=NULL)
319 HANDLE hFile;
320 hFile = ((WIN32FILE_IOWIN*)stream) -> hf;
321 if (hFile != NULL)
323 CloseHandle(hFile);
324 ret=0;
326 free(stream);
328 return ret;
331 int ZCALLBACK win32_error_file_func (voidpf opaque,voidpf stream)
333 int ret=-1;
334 if (stream!=NULL)
336 ret = ((WIN32FILE_IOWIN*)stream) -> error;
338 return ret;
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;