packvfs
diff test/zipcat/src/minizip/iowin32.c @ 3:ef6c1472607f
jesus fucking christ that was easy... written a test prog "zipcat" to try out
zlib's contrib library "minizip", to list and read files out of zip archives
directly...
author | John Tsiombikas <nuclear@member.fsf.org> |
---|---|
date | Mon, 04 Nov 2013 06:46:17 +0200 |
parents | |
children |
line diff
1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/test/zipcat/src/minizip/iowin32.c Mon Nov 04 06:46:17 2013 +0200 1.3 @@ -0,0 +1,467 @@ 1.4 +/* iowin32.c -- IO base function header for compress/uncompress .zip 1.5 + Version 1.1, February 14h, 2010 1.6 + part of the MiniZip project - ( http://www.winimage.com/zLibDll/minizip.html ) 1.7 + 1.8 + Copyright (C) 1998-2010 Gilles Vollant (minizip) ( http://www.winimage.com/zLibDll/minizip.html ) 1.9 + 1.10 + Modifications for Zip64 support 1.11 + Copyright (C) 2009-2010 Mathias Svensson ( http://result42.com ) 1.12 + 1.13 + For more info read MiniZip_info.txt 1.14 + 1.15 +*/ 1.16 + 1.17 +#ifdef WIN32 1.18 + 1.19 +#include <stdlib.h> 1.20 + 1.21 +#include "zlib.h" 1.22 +#include "ioapi.h" 1.23 +#include "iowin32.h" 1.24 + 1.25 +#ifndef INVALID_HANDLE_VALUE 1.26 +#define INVALID_HANDLE_VALUE (0xFFFFFFFF) 1.27 +#endif 1.28 + 1.29 +#ifndef INVALID_SET_FILE_POINTER 1.30 +#define INVALID_SET_FILE_POINTER ((DWORD)-1) 1.31 +#endif 1.32 + 1.33 + 1.34 +#if defined(WINAPI_FAMILY_PARTITION) && (!(defined(IOWIN32_USING_WINRT_API))) 1.35 +#if WINAPI_FAMILY_PARTITION(WINAPI_PARTITION_APP) 1.36 +#define IOWIN32_USING_WINRT_API 1 1.37 +#endif 1.38 +#endif 1.39 + 1.40 +voidpf ZCALLBACK win32_open_file_func OF((voidpf opaque, const char* filename, int mode)); 1.41 +uLong ZCALLBACK win32_read_file_func OF((voidpf opaque, voidpf stream, void* buf, uLong size)); 1.42 +uLong ZCALLBACK win32_write_file_func OF((voidpf opaque, voidpf stream, const void* buf, uLong size)); 1.43 +ZPOS64_T ZCALLBACK win32_tell64_file_func OF((voidpf opaque, voidpf stream)); 1.44 +long ZCALLBACK win32_seek64_file_func OF((voidpf opaque, voidpf stream, ZPOS64_T offset, int origin)); 1.45 +int ZCALLBACK win32_close_file_func OF((voidpf opaque, voidpf stream)); 1.46 +int ZCALLBACK win32_error_file_func OF((voidpf opaque, voidpf stream)); 1.47 + 1.48 +typedef struct 1.49 +{ 1.50 + HANDLE hf; 1.51 + int error; 1.52 +} WIN32FILE_IOWIN; 1.53 + 1.54 + 1.55 +static void win32_translate_open_mode(int mode, 1.56 + DWORD* lpdwDesiredAccess, 1.57 + DWORD* lpdwCreationDisposition, 1.58 + DWORD* lpdwShareMode, 1.59 + DWORD* lpdwFlagsAndAttributes) 1.60 +{ 1.61 + *lpdwDesiredAccess = *lpdwShareMode = *lpdwFlagsAndAttributes = *lpdwCreationDisposition = 0; 1.62 + 1.63 + if ((mode & ZLIB_FILEFUNC_MODE_READWRITEFILTER)==ZLIB_FILEFUNC_MODE_READ) 1.64 + { 1.65 + *lpdwDesiredAccess = GENERIC_READ; 1.66 + *lpdwCreationDisposition = OPEN_EXISTING; 1.67 + *lpdwShareMode = FILE_SHARE_READ; 1.68 + } 1.69 + else if (mode & ZLIB_FILEFUNC_MODE_EXISTING) 1.70 + { 1.71 + *lpdwDesiredAccess = GENERIC_WRITE | GENERIC_READ; 1.72 + *lpdwCreationDisposition = OPEN_EXISTING; 1.73 + } 1.74 + else if (mode & ZLIB_FILEFUNC_MODE_CREATE) 1.75 + { 1.76 + *lpdwDesiredAccess = GENERIC_WRITE | GENERIC_READ; 1.77 + *lpdwCreationDisposition = CREATE_ALWAYS; 1.78 + } 1.79 +} 1.80 + 1.81 +static voidpf win32_build_iowin(HANDLE hFile) 1.82 +{ 1.83 + voidpf ret=NULL; 1.84 + 1.85 + if ((hFile != NULL) && (hFile != INVALID_HANDLE_VALUE)) 1.86 + { 1.87 + WIN32FILE_IOWIN w32fiow; 1.88 + w32fiow.hf = hFile; 1.89 + w32fiow.error = 0; 1.90 + ret = malloc(sizeof(WIN32FILE_IOWIN)); 1.91 + 1.92 + if (ret==NULL) 1.93 + CloseHandle(hFile); 1.94 + else 1.95 + *((WIN32FILE_IOWIN*)ret) = w32fiow; 1.96 + } 1.97 + return ret; 1.98 +} 1.99 + 1.100 +voidpf ZCALLBACK win32_open64_file_func (voidpf opaque,const void* filename,int mode) 1.101 +{ 1.102 + const char* mode_fopen = NULL; 1.103 + DWORD dwDesiredAccess,dwCreationDisposition,dwShareMode,dwFlagsAndAttributes ; 1.104 + HANDLE hFile = NULL; 1.105 + 1.106 + win32_translate_open_mode(mode,&dwDesiredAccess,&dwCreationDisposition,&dwShareMode,&dwFlagsAndAttributes); 1.107 + 1.108 +#ifdef IOWIN32_USING_WINRT_API 1.109 +#ifdef UNICODE 1.110 + if ((filename!=NULL) && (dwDesiredAccess != 0)) 1.111 + hFile = CreateFile2((LPCTSTR)filename, dwDesiredAccess, dwShareMode, dwCreationDisposition, NULL); 1.112 +#else 1.113 + if ((filename!=NULL) && (dwDesiredAccess != 0)) 1.114 + { 1.115 + WCHAR filenameW[FILENAME_MAX + 0x200 + 1]; 1.116 + MultiByteToWideChar(CP_ACP,0,(const char*)filename,-1,filenameW,FILENAME_MAX + 0x200); 1.117 + hFile = CreateFile2(filenameW, dwDesiredAccess, dwShareMode, dwCreationDisposition, NULL); 1.118 + } 1.119 +#endif 1.120 +#else 1.121 + if ((filename!=NULL) && (dwDesiredAccess != 0)) 1.122 + hFile = CreateFile((LPCTSTR)filename, dwDesiredAccess, dwShareMode, NULL, dwCreationDisposition, dwFlagsAndAttributes, NULL); 1.123 +#endif 1.124 + 1.125 + return win32_build_iowin(hFile); 1.126 +} 1.127 + 1.128 + 1.129 +voidpf ZCALLBACK win32_open64_file_funcA (voidpf opaque,const void* filename,int mode) 1.130 +{ 1.131 + const char* mode_fopen = NULL; 1.132 + DWORD dwDesiredAccess,dwCreationDisposition,dwShareMode,dwFlagsAndAttributes ; 1.133 + HANDLE hFile = NULL; 1.134 + 1.135 + win32_translate_open_mode(mode,&dwDesiredAccess,&dwCreationDisposition,&dwShareMode,&dwFlagsAndAttributes); 1.136 + 1.137 +#ifdef IOWIN32_USING_WINRT_API 1.138 + if ((filename!=NULL) && (dwDesiredAccess != 0)) 1.139 + { 1.140 + WCHAR filenameW[FILENAME_MAX + 0x200 + 1]; 1.141 + MultiByteToWideChar(CP_ACP,0,(const char*)filename,-1,filenameW,FILENAME_MAX + 0x200); 1.142 + hFile = CreateFile2(filenameW, dwDesiredAccess, dwShareMode, dwCreationDisposition, NULL); 1.143 + } 1.144 +#else 1.145 + if ((filename!=NULL) && (dwDesiredAccess != 0)) 1.146 + hFile = CreateFileA((LPCSTR)filename, dwDesiredAccess, dwShareMode, NULL, dwCreationDisposition, dwFlagsAndAttributes, NULL); 1.147 +#endif 1.148 + 1.149 + return win32_build_iowin(hFile); 1.150 +} 1.151 + 1.152 + 1.153 +voidpf ZCALLBACK win32_open64_file_funcW (voidpf opaque,const void* filename,int mode) 1.154 +{ 1.155 + const char* mode_fopen = NULL; 1.156 + DWORD dwDesiredAccess,dwCreationDisposition,dwShareMode,dwFlagsAndAttributes ; 1.157 + HANDLE hFile = NULL; 1.158 + 1.159 + win32_translate_open_mode(mode,&dwDesiredAccess,&dwCreationDisposition,&dwShareMode,&dwFlagsAndAttributes); 1.160 + 1.161 +#ifdef IOWIN32_USING_WINRT_API 1.162 + if ((filename!=NULL) && (dwDesiredAccess != 0)) 1.163 + hFile = CreateFile2((LPCWSTR)filename, dwDesiredAccess, dwShareMode, dwCreationDisposition,NULL); 1.164 +#else 1.165 + if ((filename!=NULL) && (dwDesiredAccess != 0)) 1.166 + hFile = CreateFileW((LPCWSTR)filename, dwDesiredAccess, dwShareMode, NULL, dwCreationDisposition, dwFlagsAndAttributes, NULL); 1.167 +#endif 1.168 + 1.169 + return win32_build_iowin(hFile); 1.170 +} 1.171 + 1.172 + 1.173 +voidpf ZCALLBACK win32_open_file_func (voidpf opaque,const char* filename,int mode) 1.174 +{ 1.175 + const char* mode_fopen = NULL; 1.176 + DWORD dwDesiredAccess,dwCreationDisposition,dwShareMode,dwFlagsAndAttributes ; 1.177 + HANDLE hFile = NULL; 1.178 + 1.179 + win32_translate_open_mode(mode,&dwDesiredAccess,&dwCreationDisposition,&dwShareMode,&dwFlagsAndAttributes); 1.180 + 1.181 +#ifdef IOWIN32_USING_WINRT_API 1.182 +#ifdef UNICODE 1.183 + if ((filename!=NULL) && (dwDesiredAccess != 0)) 1.184 + hFile = CreateFile2((LPCTSTR)filename, dwDesiredAccess, dwShareMode, dwCreationDisposition, NULL); 1.185 +#else 1.186 + if ((filename!=NULL) && (dwDesiredAccess != 0)) 1.187 + { 1.188 + WCHAR filenameW[FILENAME_MAX + 0x200 + 1]; 1.189 + MultiByteToWideChar(CP_ACP,0,(const char*)filename,-1,filenameW,FILENAME_MAX + 0x200); 1.190 + hFile = CreateFile2(filenameW, dwDesiredAccess, dwShareMode, dwCreationDisposition, NULL); 1.191 + } 1.192 +#endif 1.193 +#else 1.194 + if ((filename!=NULL) && (dwDesiredAccess != 0)) 1.195 + hFile = CreateFile((LPCTSTR)filename, dwDesiredAccess, dwShareMode, NULL, dwCreationDisposition, dwFlagsAndAttributes, NULL); 1.196 +#endif 1.197 + 1.198 + return win32_build_iowin(hFile); 1.199 +} 1.200 + 1.201 + 1.202 +uLong ZCALLBACK win32_read_file_func (voidpf opaque, voidpf stream, void* buf,uLong size) 1.203 +{ 1.204 + uLong ret=0; 1.205 + HANDLE hFile = NULL; 1.206 + if (stream!=NULL) 1.207 + hFile = ((WIN32FILE_IOWIN*)stream) -> hf; 1.208 + 1.209 + if (hFile != NULL) 1.210 + { 1.211 + if (!ReadFile(hFile, buf, size, &ret, NULL)) 1.212 + { 1.213 + DWORD dwErr = GetLastError(); 1.214 + if (dwErr == ERROR_HANDLE_EOF) 1.215 + dwErr = 0; 1.216 + ((WIN32FILE_IOWIN*)stream) -> error=(int)dwErr; 1.217 + } 1.218 + } 1.219 + 1.220 + return ret; 1.221 +} 1.222 + 1.223 + 1.224 +uLong ZCALLBACK win32_write_file_func (voidpf opaque,voidpf stream,const void* buf,uLong size) 1.225 +{ 1.226 + uLong ret=0; 1.227 + HANDLE hFile = NULL; 1.228 + if (stream!=NULL) 1.229 + hFile = ((WIN32FILE_IOWIN*)stream) -> hf; 1.230 + 1.231 + if (hFile != NULL) 1.232 + { 1.233 + if (!WriteFile(hFile, buf, size, &ret, NULL)) 1.234 + { 1.235 + DWORD dwErr = GetLastError(); 1.236 + if (dwErr == ERROR_HANDLE_EOF) 1.237 + dwErr = 0; 1.238 + ((WIN32FILE_IOWIN*)stream) -> error=(int)dwErr; 1.239 + } 1.240 + } 1.241 + 1.242 + return ret; 1.243 +} 1.244 + 1.245 +static BOOL MySetFilePointerEx(HANDLE hFile, LARGE_INTEGER pos, LARGE_INTEGER *newPos, DWORD dwMoveMethod) 1.246 +{ 1.247 +#ifdef IOWIN32_USING_WINRT_API 1.248 + return SetFilePointerEx(hFile, pos, newPos, dwMoveMethod); 1.249 +#else 1.250 + LONG lHigh = pos.HighPart; 1.251 + DWORD dwNewPos = SetFilePointer(hFile, pos.LowPart, &lHigh, FILE_CURRENT); 1.252 + BOOL fOk = TRUE; 1.253 + if (dwNewPos == 0xFFFFFFFF) 1.254 + if (GetLastError() != NO_ERROR) 1.255 + fOk = FALSE; 1.256 + if ((newPos != NULL) && (fOk)) 1.257 + { 1.258 + newPos->LowPart = dwNewPos; 1.259 + newPos->HighPart = lHigh; 1.260 + } 1.261 + return fOk; 1.262 +#endif 1.263 +} 1.264 + 1.265 +long ZCALLBACK win32_tell_file_func (voidpf opaque,voidpf stream) 1.266 +{ 1.267 + long ret=-1; 1.268 + HANDLE hFile = NULL; 1.269 + if (stream!=NULL) 1.270 + hFile = ((WIN32FILE_IOWIN*)stream) -> hf; 1.271 + if (hFile != NULL) 1.272 + { 1.273 + LARGE_INTEGER pos; 1.274 + pos.QuadPart = 0; 1.275 + 1.276 + if (!MySetFilePointerEx(hFile, pos, &pos, FILE_CURRENT)) 1.277 + { 1.278 + DWORD dwErr = GetLastError(); 1.279 + ((WIN32FILE_IOWIN*)stream) -> error=(int)dwErr; 1.280 + ret = -1; 1.281 + } 1.282 + else 1.283 + ret=(long)pos.LowPart; 1.284 + } 1.285 + return ret; 1.286 +} 1.287 + 1.288 +ZPOS64_T ZCALLBACK win32_tell64_file_func (voidpf opaque, voidpf stream) 1.289 +{ 1.290 + ZPOS64_T ret= (ZPOS64_T)-1; 1.291 + HANDLE hFile = NULL; 1.292 + if (stream!=NULL) 1.293 + hFile = ((WIN32FILE_IOWIN*)stream)->hf; 1.294 + 1.295 + if (hFile) 1.296 + { 1.297 + LARGE_INTEGER pos; 1.298 + pos.QuadPart = 0; 1.299 + 1.300 + if (!MySetFilePointerEx(hFile, pos, &pos, FILE_CURRENT)) 1.301 + { 1.302 + DWORD dwErr = GetLastError(); 1.303 + ((WIN32FILE_IOWIN*)stream) -> error=(int)dwErr; 1.304 + ret = (ZPOS64_T)-1; 1.305 + } 1.306 + else 1.307 + ret=pos.QuadPart; 1.308 + } 1.309 + return ret; 1.310 +} 1.311 + 1.312 + 1.313 +long ZCALLBACK win32_seek_file_func (voidpf opaque,voidpf stream,uLong offset,int origin) 1.314 +{ 1.315 + DWORD dwMoveMethod=0xFFFFFFFF; 1.316 + HANDLE hFile = NULL; 1.317 + 1.318 + long ret=-1; 1.319 + if (stream!=NULL) 1.320 + hFile = ((WIN32FILE_IOWIN*)stream) -> hf; 1.321 + switch (origin) 1.322 + { 1.323 + case ZLIB_FILEFUNC_SEEK_CUR : 1.324 + dwMoveMethod = FILE_CURRENT; 1.325 + break; 1.326 + case ZLIB_FILEFUNC_SEEK_END : 1.327 + dwMoveMethod = FILE_END; 1.328 + break; 1.329 + case ZLIB_FILEFUNC_SEEK_SET : 1.330 + dwMoveMethod = FILE_BEGIN; 1.331 + break; 1.332 + default: return -1; 1.333 + } 1.334 + 1.335 + if (hFile != NULL) 1.336 + { 1.337 + LARGE_INTEGER pos; 1.338 + pos.QuadPart = offset; 1.339 + if (!MySetFilePointerEx(hFile, pos, NULL, dwMoveMethod)) 1.340 + { 1.341 + DWORD dwErr = GetLastError(); 1.342 + ((WIN32FILE_IOWIN*)stream) -> error=(int)dwErr; 1.343 + ret = -1; 1.344 + } 1.345 + else 1.346 + ret=0; 1.347 + } 1.348 + return ret; 1.349 +} 1.350 + 1.351 +long ZCALLBACK win32_seek64_file_func (voidpf opaque, voidpf stream,ZPOS64_T offset,int origin) 1.352 +{ 1.353 + DWORD dwMoveMethod=0xFFFFFFFF; 1.354 + HANDLE hFile = NULL; 1.355 + long ret=-1; 1.356 + 1.357 + if (stream!=NULL) 1.358 + hFile = ((WIN32FILE_IOWIN*)stream)->hf; 1.359 + 1.360 + switch (origin) 1.361 + { 1.362 + case ZLIB_FILEFUNC_SEEK_CUR : 1.363 + dwMoveMethod = FILE_CURRENT; 1.364 + break; 1.365 + case ZLIB_FILEFUNC_SEEK_END : 1.366 + dwMoveMethod = FILE_END; 1.367 + break; 1.368 + case ZLIB_FILEFUNC_SEEK_SET : 1.369 + dwMoveMethod = FILE_BEGIN; 1.370 + break; 1.371 + default: return -1; 1.372 + } 1.373 + 1.374 + if (hFile) 1.375 + { 1.376 + LARGE_INTEGER pos; 1.377 + pos.QuadPart = offset; 1.378 + if (!MySetFilePointerEx(hFile, pos, NULL, FILE_CURRENT)) 1.379 + { 1.380 + DWORD dwErr = GetLastError(); 1.381 + ((WIN32FILE_IOWIN*)stream) -> error=(int)dwErr; 1.382 + ret = -1; 1.383 + } 1.384 + else 1.385 + ret=0; 1.386 + } 1.387 + return ret; 1.388 +} 1.389 + 1.390 +int ZCALLBACK win32_close_file_func (voidpf opaque, voidpf stream) 1.391 +{ 1.392 + int ret=-1; 1.393 + 1.394 + if (stream!=NULL) 1.395 + { 1.396 + HANDLE hFile; 1.397 + hFile = ((WIN32FILE_IOWIN*)stream) -> hf; 1.398 + if (hFile != NULL) 1.399 + { 1.400 + CloseHandle(hFile); 1.401 + ret=0; 1.402 + } 1.403 + free(stream); 1.404 + } 1.405 + return ret; 1.406 +} 1.407 + 1.408 +int ZCALLBACK win32_error_file_func (voidpf opaque,voidpf stream) 1.409 +{ 1.410 + int ret=-1; 1.411 + if (stream!=NULL) 1.412 + { 1.413 + ret = ((WIN32FILE_IOWIN*)stream) -> error; 1.414 + } 1.415 + return ret; 1.416 +} 1.417 + 1.418 +void fill_win32_filefunc (zlib_filefunc_def* pzlib_filefunc_def) 1.419 +{ 1.420 + pzlib_filefunc_def->zopen_file = win32_open_file_func; 1.421 + pzlib_filefunc_def->zread_file = win32_read_file_func; 1.422 + pzlib_filefunc_def->zwrite_file = win32_write_file_func; 1.423 + pzlib_filefunc_def->ztell_file = win32_tell_file_func; 1.424 + pzlib_filefunc_def->zseek_file = win32_seek_file_func; 1.425 + pzlib_filefunc_def->zclose_file = win32_close_file_func; 1.426 + pzlib_filefunc_def->zerror_file = win32_error_file_func; 1.427 + pzlib_filefunc_def->opaque = NULL; 1.428 +} 1.429 + 1.430 +void fill_win32_filefunc64(zlib_filefunc64_def* pzlib_filefunc_def) 1.431 +{ 1.432 + pzlib_filefunc_def->zopen64_file = win32_open64_file_func; 1.433 + pzlib_filefunc_def->zread_file = win32_read_file_func; 1.434 + pzlib_filefunc_def->zwrite_file = win32_write_file_func; 1.435 + pzlib_filefunc_def->ztell64_file = win32_tell64_file_func; 1.436 + pzlib_filefunc_def->zseek64_file = win32_seek64_file_func; 1.437 + pzlib_filefunc_def->zclose_file = win32_close_file_func; 1.438 + pzlib_filefunc_def->zerror_file = win32_error_file_func; 1.439 + pzlib_filefunc_def->opaque = NULL; 1.440 +} 1.441 + 1.442 + 1.443 +void fill_win32_filefunc64A(zlib_filefunc64_def* pzlib_filefunc_def) 1.444 +{ 1.445 + pzlib_filefunc_def->zopen64_file = win32_open64_file_funcA; 1.446 + pzlib_filefunc_def->zread_file = win32_read_file_func; 1.447 + pzlib_filefunc_def->zwrite_file = win32_write_file_func; 1.448 + pzlib_filefunc_def->ztell64_file = win32_tell64_file_func; 1.449 + pzlib_filefunc_def->zseek64_file = win32_seek64_file_func; 1.450 + pzlib_filefunc_def->zclose_file = win32_close_file_func; 1.451 + pzlib_filefunc_def->zerror_file = win32_error_file_func; 1.452 + pzlib_filefunc_def->opaque = NULL; 1.453 +} 1.454 + 1.455 + 1.456 +void fill_win32_filefunc64W(zlib_filefunc64_def* pzlib_filefunc_def) 1.457 +{ 1.458 + pzlib_filefunc_def->zopen64_file = win32_open64_file_funcW; 1.459 + pzlib_filefunc_def->zread_file = win32_read_file_func; 1.460 + pzlib_filefunc_def->zwrite_file = win32_write_file_func; 1.461 + pzlib_filefunc_def->ztell64_file = win32_tell64_file_func; 1.462 + pzlib_filefunc_def->zseek64_file = win32_seek64_file_func; 1.463 + pzlib_filefunc_def->zclose_file = win32_close_file_func; 1.464 + pzlib_filefunc_def->zerror_file = win32_error_file_func; 1.465 + pzlib_filefunc_def->opaque = NULL; 1.466 +} 1.467 + 1.468 +#else 1.469 +int __minizip_iowin32_c_avoid_empty_unit_warning; 1.470 +#endif /* WIN32 */