iowin32.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461
  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 )
  4. Copyright (C) 1998-2010 Gilles Vollant (minizip) ( http://www.winimage.com/zLibDll/minizip.html )
  5. Modifications for Zip64 support
  6. Copyright (C) 2009-2010 Mathias Svensson ( http://result42.com )
  7. For more info read MiniZip_info.txt
  8. */
  9. #include <stdlib.h>
  10. #include "zlib.h"
  11. #include "ioapi.h"
  12. #include "iowin32.h"
  13. #ifndef INVALID_HANDLE_VALUE
  14. #define INVALID_HANDLE_VALUE (0xFFFFFFFF)
  15. #endif
  16. #ifndef INVALID_SET_FILE_POINTER
  17. #define INVALID_SET_FILE_POINTER ((DWORD)-1)
  18. #endif
  19. #if defined(WINAPI_FAMILY_PARTITION) && (!(defined(IOWIN32_USING_WINRT_API)))
  20. #if WINAPI_FAMILY_PARTITION(WINAPI_PARTITION_APP)
  21. #define IOWIN32_USING_WINRT_API 1
  22. #endif
  23. #endif
  24. voidpf ZCALLBACK win32_open_file_func OF((voidpf opaque, const char* filename, int mode));
  25. uLong ZCALLBACK win32_read_file_func OF((voidpf opaque, voidpf stream, void* buf, uLong size));
  26. uLong ZCALLBACK win32_write_file_func OF((voidpf opaque, voidpf stream, const void* buf, uLong size));
  27. ZPOS64_T ZCALLBACK win32_tell64_file_func OF((voidpf opaque, voidpf stream));
  28. long ZCALLBACK win32_seek64_file_func OF((voidpf opaque, voidpf stream, ZPOS64_T offset, int origin));
  29. int ZCALLBACK win32_close_file_func OF((voidpf opaque, voidpf stream));
  30. int ZCALLBACK win32_error_file_func OF((voidpf opaque, voidpf stream));
  31. typedef struct
  32. {
  33. HANDLE hf;
  34. int error;
  35. } WIN32FILE_IOWIN;
  36. static void win32_translate_open_mode(int mode,
  37. DWORD* lpdwDesiredAccess,
  38. DWORD* lpdwCreationDisposition,
  39. DWORD* lpdwShareMode,
  40. DWORD* lpdwFlagsAndAttributes)
  41. {
  42. *lpdwDesiredAccess = *lpdwShareMode = *lpdwFlagsAndAttributes = *lpdwCreationDisposition = 0;
  43. if ((mode & ZLIB_FILEFUNC_MODE_READWRITEFILTER)==ZLIB_FILEFUNC_MODE_READ)
  44. {
  45. *lpdwDesiredAccess = GENERIC_READ;
  46. *lpdwCreationDisposition = OPEN_EXISTING;
  47. *lpdwShareMode = FILE_SHARE_READ;
  48. }
  49. else if (mode & ZLIB_FILEFUNC_MODE_EXISTING)
  50. {
  51. *lpdwDesiredAccess = GENERIC_WRITE | GENERIC_READ;
  52. *lpdwCreationDisposition = OPEN_EXISTING;
  53. }
  54. else if (mode & ZLIB_FILEFUNC_MODE_CREATE)
  55. {
  56. *lpdwDesiredAccess = GENERIC_WRITE | GENERIC_READ;
  57. *lpdwCreationDisposition = CREATE_ALWAYS;
  58. }
  59. }
  60. static voidpf win32_build_iowin(HANDLE hFile)
  61. {
  62. voidpf ret=NULL;
  63. if ((hFile != NULL) && (hFile != INVALID_HANDLE_VALUE))
  64. {
  65. WIN32FILE_IOWIN w32fiow;
  66. w32fiow.hf = hFile;
  67. w32fiow.error = 0;
  68. ret = malloc(sizeof(WIN32FILE_IOWIN));
  69. if (ret==NULL)
  70. CloseHandle(hFile);
  71. else
  72. *((WIN32FILE_IOWIN*)ret) = w32fiow;
  73. }
  74. return ret;
  75. }
  76. voidpf ZCALLBACK win32_open64_file_func (voidpf opaque,const void* filename,int mode)
  77. {
  78. const char* mode_fopen = NULL;
  79. DWORD dwDesiredAccess,dwCreationDisposition,dwShareMode,dwFlagsAndAttributes ;
  80. HANDLE hFile = NULL;
  81. win32_translate_open_mode(mode,&dwDesiredAccess,&dwCreationDisposition,&dwShareMode,&dwFlagsAndAttributes);
  82. #ifdef IOWIN32_USING_WINRT_API
  83. #ifdef UNICODE
  84. if ((filename!=NULL) && (dwDesiredAccess != 0))
  85. hFile = CreateFile2((LPCTSTR)filename, dwDesiredAccess, dwShareMode, dwCreationDisposition, NULL);
  86. #else
  87. if ((filename!=NULL) && (dwDesiredAccess != 0))
  88. {
  89. WCHAR filenameW[FILENAME_MAX + 0x200 + 1];
  90. MultiByteToWideChar(CP_ACP,0,(const char*)filename,-1,filenameW,FILENAME_MAX + 0x200);
  91. hFile = CreateFile2(filenameW, dwDesiredAccess, dwShareMode, dwCreationDisposition, NULL);
  92. }
  93. #endif
  94. #else
  95. if ((filename!=NULL) && (dwDesiredAccess != 0))
  96. hFile = CreateFile((LPCTSTR)filename, dwDesiredAccess, dwShareMode, NULL, dwCreationDisposition, dwFlagsAndAttributes, NULL);
  97. #endif
  98. return win32_build_iowin(hFile);
  99. }
  100. voidpf ZCALLBACK win32_open64_file_funcA (voidpf opaque,const void* filename,int mode)
  101. {
  102. const char* mode_fopen = NULL;
  103. DWORD dwDesiredAccess,dwCreationDisposition,dwShareMode,dwFlagsAndAttributes ;
  104. HANDLE hFile = NULL;
  105. win32_translate_open_mode(mode,&dwDesiredAccess,&dwCreationDisposition,&dwShareMode,&dwFlagsAndAttributes);
  106. #ifdef IOWIN32_USING_WINRT_API
  107. if ((filename!=NULL) && (dwDesiredAccess != 0))
  108. {
  109. WCHAR filenameW[FILENAME_MAX + 0x200 + 1];
  110. MultiByteToWideChar(CP_ACP,0,(const char*)filename,-1,filenameW,FILENAME_MAX + 0x200);
  111. hFile = CreateFile2(filenameW, dwDesiredAccess, dwShareMode, dwCreationDisposition, NULL);
  112. }
  113. #else
  114. if ((filename!=NULL) && (dwDesiredAccess != 0))
  115. hFile = CreateFileA((LPCSTR)filename, dwDesiredAccess, dwShareMode, NULL, dwCreationDisposition, dwFlagsAndAttributes, NULL);
  116. #endif
  117. return win32_build_iowin(hFile);
  118. }
  119. voidpf ZCALLBACK win32_open64_file_funcW (voidpf opaque,const void* filename,int mode)
  120. {
  121. const char* mode_fopen = NULL;
  122. DWORD dwDesiredAccess,dwCreationDisposition,dwShareMode,dwFlagsAndAttributes ;
  123. HANDLE hFile = NULL;
  124. win32_translate_open_mode(mode,&dwDesiredAccess,&dwCreationDisposition,&dwShareMode,&dwFlagsAndAttributes);
  125. #ifdef IOWIN32_USING_WINRT_API
  126. if ((filename!=NULL) && (dwDesiredAccess != 0))
  127. hFile = CreateFile2((LPCWSTR)filename, dwDesiredAccess, dwShareMode, dwCreationDisposition,NULL);
  128. #else
  129. if ((filename!=NULL) && (dwDesiredAccess != 0))
  130. hFile = CreateFileW((LPCWSTR)filename, dwDesiredAccess, dwShareMode, NULL, dwCreationDisposition, dwFlagsAndAttributes, NULL);
  131. #endif
  132. return win32_build_iowin(hFile);
  133. }
  134. voidpf ZCALLBACK win32_open_file_func (voidpf opaque,const char* filename,int mode)
  135. {
  136. const char* mode_fopen = NULL;
  137. DWORD dwDesiredAccess,dwCreationDisposition,dwShareMode,dwFlagsAndAttributes ;
  138. HANDLE hFile = NULL;
  139. win32_translate_open_mode(mode,&dwDesiredAccess,&dwCreationDisposition,&dwShareMode,&dwFlagsAndAttributes);
  140. #ifdef IOWIN32_USING_WINRT_API
  141. #ifdef UNICODE
  142. if ((filename!=NULL) && (dwDesiredAccess != 0))
  143. hFile = CreateFile2((LPCTSTR)filename, dwDesiredAccess, dwShareMode, dwCreationDisposition, NULL);
  144. #else
  145. if ((filename!=NULL) && (dwDesiredAccess != 0))
  146. {
  147. WCHAR filenameW[FILENAME_MAX + 0x200 + 1];
  148. MultiByteToWideChar(CP_ACP,0,(const char*)filename,-1,filenameW,FILENAME_MAX + 0x200);
  149. hFile = CreateFile2(filenameW, dwDesiredAccess, dwShareMode, dwCreationDisposition, NULL);
  150. }
  151. #endif
  152. #else
  153. if ((filename!=NULL) && (dwDesiredAccess != 0))
  154. hFile = CreateFile((LPCTSTR)filename, dwDesiredAccess, dwShareMode, NULL, dwCreationDisposition, dwFlagsAndAttributes, NULL);
  155. #endif
  156. return win32_build_iowin(hFile);
  157. }
  158. uLong ZCALLBACK win32_read_file_func (voidpf opaque, voidpf stream, void* buf,uLong size)
  159. {
  160. uLong ret=0;
  161. HANDLE hFile = NULL;
  162. if (stream!=NULL)
  163. hFile = ((WIN32FILE_IOWIN*)stream) -> hf;
  164. if (hFile != NULL)
  165. {
  166. if (!ReadFile(hFile, buf, size, &ret, NULL))
  167. {
  168. DWORD dwErr = GetLastError();
  169. if (dwErr == ERROR_HANDLE_EOF)
  170. dwErr = 0;
  171. ((WIN32FILE_IOWIN*)stream) -> error=(int)dwErr;
  172. }
  173. }
  174. return ret;
  175. }
  176. uLong ZCALLBACK win32_write_file_func (voidpf opaque,voidpf stream,const void* buf,uLong size)
  177. {
  178. uLong ret=0;
  179. HANDLE hFile = NULL;
  180. if (stream!=NULL)
  181. hFile = ((WIN32FILE_IOWIN*)stream) -> hf;
  182. if (hFile != NULL)
  183. {
  184. if (!WriteFile(hFile, buf, size, &ret, NULL))
  185. {
  186. DWORD dwErr = GetLastError();
  187. if (dwErr == ERROR_HANDLE_EOF)
  188. dwErr = 0;
  189. ((WIN32FILE_IOWIN*)stream) -> error=(int)dwErr;
  190. }
  191. }
  192. return ret;
  193. }
  194. static BOOL MySetFilePointerEx(HANDLE hFile, LARGE_INTEGER pos, LARGE_INTEGER *newPos, DWORD dwMoveMethod)
  195. {
  196. #ifdef IOWIN32_USING_WINRT_API
  197. return SetFilePointerEx(hFile, pos, newPos, dwMoveMethod);
  198. #else
  199. LONG lHigh = pos.HighPart;
  200. DWORD dwNewPos = SetFilePointer(hFile, pos.LowPart, &lHigh, FILE_CURRENT);
  201. BOOL fOk = TRUE;
  202. if (dwNewPos == 0xFFFFFFFF)
  203. if (GetLastError() != NO_ERROR)
  204. fOk = FALSE;
  205. if ((newPos != NULL) && (fOk))
  206. {
  207. newPos->LowPart = dwNewPos;
  208. newPos->HighPart = lHigh;
  209. }
  210. return fOk;
  211. #endif
  212. }
  213. long ZCALLBACK win32_tell_file_func (voidpf opaque,voidpf stream)
  214. {
  215. long ret=-1;
  216. HANDLE hFile = NULL;
  217. if (stream!=NULL)
  218. hFile = ((WIN32FILE_IOWIN*)stream) -> hf;
  219. if (hFile != NULL)
  220. {
  221. LARGE_INTEGER pos;
  222. pos.QuadPart = 0;
  223. if (!MySetFilePointerEx(hFile, pos, &pos, FILE_CURRENT))
  224. {
  225. DWORD dwErr = GetLastError();
  226. ((WIN32FILE_IOWIN*)stream) -> error=(int)dwErr;
  227. ret = -1;
  228. }
  229. else
  230. ret=(long)pos.LowPart;
  231. }
  232. return ret;
  233. }
  234. ZPOS64_T ZCALLBACK win32_tell64_file_func (voidpf opaque, voidpf stream)
  235. {
  236. ZPOS64_T ret= (ZPOS64_T)-1;
  237. HANDLE hFile = NULL;
  238. if (stream!=NULL)
  239. hFile = ((WIN32FILE_IOWIN*)stream)->hf;
  240. if (hFile)
  241. {
  242. LARGE_INTEGER pos;
  243. pos.QuadPart = 0;
  244. if (!MySetFilePointerEx(hFile, pos, &pos, FILE_CURRENT))
  245. {
  246. DWORD dwErr = GetLastError();
  247. ((WIN32FILE_IOWIN*)stream) -> error=(int)dwErr;
  248. ret = (ZPOS64_T)-1;
  249. }
  250. else
  251. ret=pos.QuadPart;
  252. }
  253. return ret;
  254. }
  255. long ZCALLBACK win32_seek_file_func (voidpf opaque,voidpf stream,uLong offset,int origin)
  256. {
  257. DWORD dwMoveMethod=0xFFFFFFFF;
  258. HANDLE hFile = NULL;
  259. long ret=-1;
  260. if (stream!=NULL)
  261. hFile = ((WIN32FILE_IOWIN*)stream) -> hf;
  262. switch (origin)
  263. {
  264. case ZLIB_FILEFUNC_SEEK_CUR :
  265. dwMoveMethod = FILE_CURRENT;
  266. break;
  267. case ZLIB_FILEFUNC_SEEK_END :
  268. dwMoveMethod = FILE_END;
  269. break;
  270. case ZLIB_FILEFUNC_SEEK_SET :
  271. dwMoveMethod = FILE_BEGIN;
  272. break;
  273. default: return -1;
  274. }
  275. if (hFile != NULL)
  276. {
  277. LARGE_INTEGER pos;
  278. pos.QuadPart = offset;
  279. if (!MySetFilePointerEx(hFile, pos, NULL, dwMoveMethod))
  280. {
  281. DWORD dwErr = GetLastError();
  282. ((WIN32FILE_IOWIN*)stream) -> error=(int)dwErr;
  283. ret = -1;
  284. }
  285. else
  286. ret=0;
  287. }
  288. return ret;
  289. }
  290. long ZCALLBACK win32_seek64_file_func (voidpf opaque, voidpf stream,ZPOS64_T offset,int origin)
  291. {
  292. DWORD dwMoveMethod=0xFFFFFFFF;
  293. HANDLE hFile = NULL;
  294. long ret=-1;
  295. if (stream!=NULL)
  296. hFile = ((WIN32FILE_IOWIN*)stream)->hf;
  297. switch (origin)
  298. {
  299. case ZLIB_FILEFUNC_SEEK_CUR :
  300. dwMoveMethod = FILE_CURRENT;
  301. break;
  302. case ZLIB_FILEFUNC_SEEK_END :
  303. dwMoveMethod = FILE_END;
  304. break;
  305. case ZLIB_FILEFUNC_SEEK_SET :
  306. dwMoveMethod = FILE_BEGIN;
  307. break;
  308. default: return -1;
  309. }
  310. if (hFile)
  311. {
  312. LARGE_INTEGER pos;
  313. pos.QuadPart = offset;
  314. if (!MySetFilePointerEx(hFile, pos, NULL, FILE_CURRENT))
  315. {
  316. DWORD dwErr = GetLastError();
  317. ((WIN32FILE_IOWIN*)stream) -> error=(int)dwErr;
  318. ret = -1;
  319. }
  320. else
  321. ret=0;
  322. }
  323. return ret;
  324. }
  325. int ZCALLBACK win32_close_file_func (voidpf opaque, voidpf stream)
  326. {
  327. int ret=-1;
  328. if (stream!=NULL)
  329. {
  330. HANDLE hFile;
  331. hFile = ((WIN32FILE_IOWIN*)stream) -> hf;
  332. if (hFile != NULL)
  333. {
  334. CloseHandle(hFile);
  335. ret=0;
  336. }
  337. free(stream);
  338. }
  339. return ret;
  340. }
  341. int ZCALLBACK win32_error_file_func (voidpf opaque,voidpf stream)
  342. {
  343. int ret=-1;
  344. if (stream!=NULL)
  345. {
  346. ret = ((WIN32FILE_IOWIN*)stream) -> error;
  347. }
  348. return ret;
  349. }
  350. void fill_win32_filefunc (zlib_filefunc_def* pzlib_filefunc_def)
  351. {
  352. pzlib_filefunc_def->zopen_file = win32_open_file_func;
  353. pzlib_filefunc_def->zread_file = win32_read_file_func;
  354. pzlib_filefunc_def->zwrite_file = win32_write_file_func;
  355. pzlib_filefunc_def->ztell_file = win32_tell_file_func;
  356. pzlib_filefunc_def->zseek_file = win32_seek_file_func;
  357. pzlib_filefunc_def->zclose_file = win32_close_file_func;
  358. pzlib_filefunc_def->zerror_file = win32_error_file_func;
  359. pzlib_filefunc_def->opaque = NULL;
  360. }
  361. void fill_win32_filefunc64(zlib_filefunc64_def* pzlib_filefunc_def)
  362. {
  363. pzlib_filefunc_def->zopen64_file = win32_open64_file_func;
  364. pzlib_filefunc_def->zread_file = win32_read_file_func;
  365. pzlib_filefunc_def->zwrite_file = win32_write_file_func;
  366. pzlib_filefunc_def->ztell64_file = win32_tell64_file_func;
  367. pzlib_filefunc_def->zseek64_file = win32_seek64_file_func;
  368. pzlib_filefunc_def->zclose_file = win32_close_file_func;
  369. pzlib_filefunc_def->zerror_file = win32_error_file_func;
  370. pzlib_filefunc_def->opaque = NULL;
  371. }
  372. void fill_win32_filefunc64A(zlib_filefunc64_def* pzlib_filefunc_def)
  373. {
  374. pzlib_filefunc_def->zopen64_file = win32_open64_file_funcA;
  375. pzlib_filefunc_def->zread_file = win32_read_file_func;
  376. pzlib_filefunc_def->zwrite_file = win32_write_file_func;
  377. pzlib_filefunc_def->ztell64_file = win32_tell64_file_func;
  378. pzlib_filefunc_def->zseek64_file = win32_seek64_file_func;
  379. pzlib_filefunc_def->zclose_file = win32_close_file_func;
  380. pzlib_filefunc_def->zerror_file = win32_error_file_func;
  381. pzlib_filefunc_def->opaque = NULL;
  382. }
  383. void fill_win32_filefunc64W(zlib_filefunc64_def* pzlib_filefunc_def)
  384. {
  385. pzlib_filefunc_def->zopen64_file = win32_open64_file_funcW;
  386. pzlib_filefunc_def->zread_file = win32_read_file_func;
  387. pzlib_filefunc_def->zwrite_file = win32_write_file_func;
  388. pzlib_filefunc_def->ztell64_file = win32_tell64_file_func;
  389. pzlib_filefunc_def->zseek64_file = win32_seek64_file_func;
  390. pzlib_filefunc_def->zclose_file = win32_close_file_func;
  391. pzlib_filefunc_def->zerror_file = win32_error_file_func;
  392. pzlib_filefunc_def->opaque = NULL;
  393. }