iowin32.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389
  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. voidpf ZCALLBACK win32_open_file_func OF((voidpf opaque, const char* filename, int mode));
  20. uLong ZCALLBACK win32_read_file_func OF((voidpf opaque, voidpf stream, void* buf, uLong size));
  21. uLong ZCALLBACK win32_write_file_func OF((voidpf opaque, voidpf stream, const void* buf, uLong size));
  22. ZPOS64_T ZCALLBACK win32_tell64_file_func OF((voidpf opaque, voidpf stream));
  23. long ZCALLBACK win32_seek64_file_func OF((voidpf opaque, voidpf stream, ZPOS64_T offset, int origin));
  24. int ZCALLBACK win32_close_file_func OF((voidpf opaque, voidpf stream));
  25. int ZCALLBACK win32_error_file_func OF((voidpf opaque, voidpf stream));
  26. typedef struct
  27. {
  28. HANDLE hf;
  29. int error;
  30. } WIN32FILE_IOWIN;
  31. static void win32_translate_open_mode(int mode,
  32. DWORD* lpdwDesiredAccess,
  33. DWORD* lpdwCreationDisposition,
  34. DWORD* lpdwShareMode,
  35. DWORD* lpdwFlagsAndAttributes)
  36. {
  37. *lpdwDesiredAccess = *lpdwShareMode = *lpdwFlagsAndAttributes = *lpdwCreationDisposition = 0;
  38. if ((mode & ZLIB_FILEFUNC_MODE_READWRITEFILTER)==ZLIB_FILEFUNC_MODE_READ)
  39. {
  40. *lpdwDesiredAccess = GENERIC_READ;
  41. *lpdwCreationDisposition = OPEN_EXISTING;
  42. *lpdwShareMode = FILE_SHARE_READ;
  43. }
  44. else if (mode & ZLIB_FILEFUNC_MODE_EXISTING)
  45. {
  46. *lpdwDesiredAccess = GENERIC_WRITE | GENERIC_READ;
  47. *lpdwCreationDisposition = OPEN_EXISTING;
  48. }
  49. else if (mode & ZLIB_FILEFUNC_MODE_CREATE)
  50. {
  51. *lpdwDesiredAccess = GENERIC_WRITE | GENERIC_READ;
  52. *lpdwCreationDisposition = CREATE_ALWAYS;
  53. }
  54. }
  55. static voidpf win32_build_iowin(HANDLE hFile)
  56. {
  57. voidpf ret=NULL;
  58. if ((hFile != NULL) && (hFile != INVALID_HANDLE_VALUE))
  59. {
  60. WIN32FILE_IOWIN w32fiow;
  61. w32fiow.hf = hFile;
  62. w32fiow.error = 0;
  63. ret = malloc(sizeof(WIN32FILE_IOWIN));
  64. if (ret==NULL)
  65. CloseHandle(hFile);
  66. else
  67. *((WIN32FILE_IOWIN*)ret) = w32fiow;
  68. }
  69. return ret;
  70. }
  71. voidpf ZCALLBACK win32_open64_file_func (voidpf opaque,const void* filename,int mode)
  72. {
  73. const char* mode_fopen = NULL;
  74. DWORD dwDesiredAccess,dwCreationDisposition,dwShareMode,dwFlagsAndAttributes ;
  75. HANDLE hFile = NULL;
  76. win32_translate_open_mode(mode,&dwDesiredAccess,&dwCreationDisposition,&dwShareMode,&dwFlagsAndAttributes);
  77. if ((filename!=NULL) && (dwDesiredAccess != 0))
  78. hFile = CreateFile((LPCTSTR)filename, dwDesiredAccess, dwShareMode, NULL, dwCreationDisposition, dwFlagsAndAttributes, NULL);
  79. return win32_build_iowin(hFile);
  80. }
  81. voidpf ZCALLBACK win32_open64_file_funcA (voidpf opaque,const void* filename,int mode)
  82. {
  83. const char* mode_fopen = NULL;
  84. DWORD dwDesiredAccess,dwCreationDisposition,dwShareMode,dwFlagsAndAttributes ;
  85. HANDLE hFile = NULL;
  86. win32_translate_open_mode(mode,&dwDesiredAccess,&dwCreationDisposition,&dwShareMode,&dwFlagsAndAttributes);
  87. if ((filename!=NULL) && (dwDesiredAccess != 0))
  88. hFile = CreateFileA((LPCSTR)filename, dwDesiredAccess, dwShareMode, NULL, dwCreationDisposition, dwFlagsAndAttributes, NULL);
  89. return win32_build_iowin(hFile);
  90. }
  91. voidpf ZCALLBACK win32_open64_file_funcW (voidpf opaque,const void* filename,int mode)
  92. {
  93. const char* mode_fopen = NULL;
  94. DWORD dwDesiredAccess,dwCreationDisposition,dwShareMode,dwFlagsAndAttributes ;
  95. HANDLE hFile = NULL;
  96. win32_translate_open_mode(mode,&dwDesiredAccess,&dwCreationDisposition,&dwShareMode,&dwFlagsAndAttributes);
  97. if ((filename!=NULL) && (dwDesiredAccess != 0))
  98. hFile = CreateFileW((LPCWSTR)filename, dwDesiredAccess, dwShareMode, NULL, dwCreationDisposition, dwFlagsAndAttributes, NULL);
  99. return win32_build_iowin(hFile);
  100. }
  101. voidpf ZCALLBACK win32_open_file_func (voidpf opaque,const char* filename,int mode)
  102. {
  103. const char* mode_fopen = NULL;
  104. DWORD dwDesiredAccess,dwCreationDisposition,dwShareMode,dwFlagsAndAttributes ;
  105. HANDLE hFile = NULL;
  106. win32_translate_open_mode(mode,&dwDesiredAccess,&dwCreationDisposition,&dwShareMode,&dwFlagsAndAttributes);
  107. if ((filename!=NULL) && (dwDesiredAccess != 0))
  108. hFile = CreateFile((LPCTSTR)filename, dwDesiredAccess, dwShareMode, NULL, dwCreationDisposition, dwFlagsAndAttributes, NULL);
  109. return win32_build_iowin(hFile);
  110. }
  111. uLong ZCALLBACK win32_read_file_func (voidpf opaque, voidpf stream, void* buf,uLong size)
  112. {
  113. uLong ret=0;
  114. HANDLE hFile = NULL;
  115. if (stream!=NULL)
  116. hFile = ((WIN32FILE_IOWIN*)stream) -> hf;
  117. if (hFile != NULL)
  118. {
  119. if (!ReadFile(hFile, buf, size, &ret, NULL))
  120. {
  121. DWORD dwErr = GetLastError();
  122. if (dwErr == ERROR_HANDLE_EOF)
  123. dwErr = 0;
  124. ((WIN32FILE_IOWIN*)stream) -> error=(int)dwErr;
  125. }
  126. }
  127. return ret;
  128. }
  129. uLong ZCALLBACK win32_write_file_func (voidpf opaque,voidpf stream,const void* buf,uLong size)
  130. {
  131. uLong ret=0;
  132. HANDLE hFile = NULL;
  133. if (stream!=NULL)
  134. hFile = ((WIN32FILE_IOWIN*)stream) -> hf;
  135. if (hFile != NULL)
  136. {
  137. if (!WriteFile(hFile, buf, size, &ret, NULL))
  138. {
  139. DWORD dwErr = GetLastError();
  140. if (dwErr == ERROR_HANDLE_EOF)
  141. dwErr = 0;
  142. ((WIN32FILE_IOWIN*)stream) -> error=(int)dwErr;
  143. }
  144. }
  145. return ret;
  146. }
  147. long ZCALLBACK win32_tell_file_func (voidpf opaque,voidpf stream)
  148. {
  149. long ret=-1;
  150. HANDLE hFile = NULL;
  151. if (stream!=NULL)
  152. hFile = ((WIN32FILE_IOWIN*)stream) -> hf;
  153. if (hFile != NULL)
  154. {
  155. DWORD dwSet = SetFilePointer(hFile, 0, NULL, FILE_CURRENT);
  156. if (dwSet == INVALID_SET_FILE_POINTER)
  157. {
  158. DWORD dwErr = GetLastError();
  159. ((WIN32FILE_IOWIN*)stream) -> error=(int)dwErr;
  160. ret = -1;
  161. }
  162. else
  163. ret=(long)dwSet;
  164. }
  165. return ret;
  166. }
  167. ZPOS64_T ZCALLBACK win32_tell64_file_func (voidpf opaque, voidpf stream)
  168. {
  169. ZPOS64_T ret= (ZPOS64_T)-1;
  170. HANDLE hFile = NULL;
  171. if (stream!=NULL)
  172. hFile = ((WIN32FILE_IOWIN*)stream)->hf;
  173. if (hFile)
  174. {
  175. LARGE_INTEGER li;
  176. li.QuadPart = 0;
  177. li.u.LowPart = SetFilePointer(hFile, li.u.LowPart, &li.u.HighPart, FILE_CURRENT);
  178. if ( (li.LowPart == 0xFFFFFFFF) && (GetLastError() != NO_ERROR))
  179. {
  180. DWORD dwErr = GetLastError();
  181. ((WIN32FILE_IOWIN*)stream) -> error=(int)dwErr;
  182. ret = (ZPOS64_T)-1;
  183. }
  184. else
  185. ret=li.QuadPart;
  186. }
  187. return ret;
  188. }
  189. long ZCALLBACK win32_seek_file_func (voidpf opaque,voidpf stream,uLong offset,int origin)
  190. {
  191. DWORD dwMoveMethod=0xFFFFFFFF;
  192. HANDLE hFile = NULL;
  193. long ret=-1;
  194. if (stream!=NULL)
  195. hFile = ((WIN32FILE_IOWIN*)stream) -> hf;
  196. switch (origin)
  197. {
  198. case ZLIB_FILEFUNC_SEEK_CUR :
  199. dwMoveMethod = FILE_CURRENT;
  200. break;
  201. case ZLIB_FILEFUNC_SEEK_END :
  202. dwMoveMethod = FILE_END;
  203. break;
  204. case ZLIB_FILEFUNC_SEEK_SET :
  205. dwMoveMethod = FILE_BEGIN;
  206. break;
  207. default: return -1;
  208. }
  209. if (hFile != NULL)
  210. {
  211. DWORD dwSet = SetFilePointer(hFile, offset, NULL, dwMoveMethod);
  212. if (dwSet == INVALID_SET_FILE_POINTER)
  213. {
  214. DWORD dwErr = GetLastError();
  215. ((WIN32FILE_IOWIN*)stream) -> error=(int)dwErr;
  216. ret = -1;
  217. }
  218. else
  219. ret=0;
  220. }
  221. return ret;
  222. }
  223. long ZCALLBACK win32_seek64_file_func (voidpf opaque, voidpf stream,ZPOS64_T offset,int origin)
  224. {
  225. DWORD dwMoveMethod=0xFFFFFFFF;
  226. HANDLE hFile = NULL;
  227. long ret=-1;
  228. if (stream!=NULL)
  229. hFile = ((WIN32FILE_IOWIN*)stream)->hf;
  230. switch (origin)
  231. {
  232. case ZLIB_FILEFUNC_SEEK_CUR :
  233. dwMoveMethod = FILE_CURRENT;
  234. break;
  235. case ZLIB_FILEFUNC_SEEK_END :
  236. dwMoveMethod = FILE_END;
  237. break;
  238. case ZLIB_FILEFUNC_SEEK_SET :
  239. dwMoveMethod = FILE_BEGIN;
  240. break;
  241. default: return -1;
  242. }
  243. if (hFile)
  244. {
  245. LARGE_INTEGER* li = (LARGE_INTEGER*)&offset;
  246. DWORD dwSet = SetFilePointer(hFile, li->u.LowPart, &li->u.HighPart, dwMoveMethod);
  247. if (dwSet == INVALID_SET_FILE_POINTER)
  248. {
  249. DWORD dwErr = GetLastError();
  250. ((WIN32FILE_IOWIN*)stream) -> error=(int)dwErr;
  251. ret = -1;
  252. }
  253. else
  254. ret=0;
  255. }
  256. return ret;
  257. }
  258. int ZCALLBACK win32_close_file_func (voidpf opaque, voidpf stream)
  259. {
  260. int ret=-1;
  261. if (stream!=NULL)
  262. {
  263. HANDLE hFile;
  264. hFile = ((WIN32FILE_IOWIN*)stream) -> hf;
  265. if (hFile != NULL)
  266. {
  267. CloseHandle(hFile);
  268. ret=0;
  269. }
  270. free(stream);
  271. }
  272. return ret;
  273. }
  274. int ZCALLBACK win32_error_file_func (voidpf opaque,voidpf stream)
  275. {
  276. int ret=-1;
  277. if (stream!=NULL)
  278. {
  279. ret = ((WIN32FILE_IOWIN*)stream) -> error;
  280. }
  281. return ret;
  282. }
  283. void fill_win32_filefunc (zlib_filefunc_def* pzlib_filefunc_def)
  284. {
  285. pzlib_filefunc_def->zopen_file = win32_open_file_func;
  286. pzlib_filefunc_def->zread_file = win32_read_file_func;
  287. pzlib_filefunc_def->zwrite_file = win32_write_file_func;
  288. pzlib_filefunc_def->ztell_file = win32_tell_file_func;
  289. pzlib_filefunc_def->zseek_file = win32_seek_file_func;
  290. pzlib_filefunc_def->zclose_file = win32_close_file_func;
  291. pzlib_filefunc_def->zerror_file = win32_error_file_func;
  292. pzlib_filefunc_def->opaque = NULL;
  293. }
  294. void fill_win32_filefunc64(zlib_filefunc64_def* pzlib_filefunc_def)
  295. {
  296. pzlib_filefunc_def->zopen64_file = win32_open64_file_func;
  297. pzlib_filefunc_def->zread_file = win32_read_file_func;
  298. pzlib_filefunc_def->zwrite_file = win32_write_file_func;
  299. pzlib_filefunc_def->ztell64_file = win32_tell64_file_func;
  300. pzlib_filefunc_def->zseek64_file = win32_seek64_file_func;
  301. pzlib_filefunc_def->zclose_file = win32_close_file_func;
  302. pzlib_filefunc_def->zerror_file = win32_error_file_func;
  303. pzlib_filefunc_def->opaque = NULL;
  304. }
  305. void fill_win32_filefunc64A(zlib_filefunc64_def* pzlib_filefunc_def)
  306. {
  307. pzlib_filefunc_def->zopen64_file = win32_open64_file_funcA;
  308. pzlib_filefunc_def->zread_file = win32_read_file_func;
  309. pzlib_filefunc_def->zwrite_file = win32_write_file_func;
  310. pzlib_filefunc_def->ztell64_file = win32_tell64_file_func;
  311. pzlib_filefunc_def->zseek64_file = win32_seek64_file_func;
  312. pzlib_filefunc_def->zclose_file = win32_close_file_func;
  313. pzlib_filefunc_def->zerror_file = win32_error_file_func;
  314. pzlib_filefunc_def->opaque = NULL;
  315. }
  316. void fill_win32_filefunc64W(zlib_filefunc64_def* pzlib_filefunc_def)
  317. {
  318. pzlib_filefunc_def->zopen64_file = win32_open64_file_funcW;
  319. pzlib_filefunc_def->zread_file = win32_read_file_func;
  320. pzlib_filefunc_def->zwrite_file = win32_write_file_func;
  321. pzlib_filefunc_def->ztell64_file = win32_tell64_file_func;
  322. pzlib_filefunc_def->zseek64_file = win32_seek64_file_func;
  323. pzlib_filefunc_def->zclose_file = win32_close_file_func;
  324. pzlib_filefunc_def->zerror_file = win32_error_file_func;
  325. pzlib_filefunc_def->opaque = NULL;
  326. }