iowin32.c 14 KB

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