Aucune description

ioapi.c 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370
  1. /* ioapi.h -- IO base function header for compress/uncompress .zip
  2. part of the MiniZip project
  3. Copyright (C) 1998-2010 Gilles Vollant
  4. http://www.winimage.com/zLibDll/minizip.html
  5. Modifications for Zip64 support
  6. Copyright (C) 2009-2010 Mathias Svensson
  7. http://result42.com
  8. This program is distributed under the terms of the same license as zlib.
  9. See the accompanying LICENSE file for the full text of the license.
  10. */
  11. #include <stdlib.h>
  12. #include <string.h>
  13. #include "ioapi.h"
  14. #if defined(_WIN32)
  15. # define snprintf _snprintf
  16. #endif
  17. #ifdef __APPLE__
  18. /* In darwin and perhaps other BSD variants off_t is a 64 bit value, hence no need for specific 64 bit functions */
  19. # define FOPEN_FUNC(filename, mode) fopen(filename, mode)
  20. # define FTELLO_FUNC(stream) ftello(stream)
  21. # define FSEEKO_FUNC(stream, offset, origin) fseeko(stream, offset, origin)
  22. #else
  23. # define FOPEN_FUNC(filename, mode) fopen64(filename, mode)
  24. # define FTELLO_FUNC(stream) ftello64(stream)
  25. # define FSEEKO_FUNC(stream, offset, origin) fseeko64(stream, offset, origin)
  26. #endif
  27. /* I've found an old Unix (a SunOS 4.1.3_U1) without all SEEK_* defined.... */
  28. #ifndef SEEK_CUR
  29. # define SEEK_CUR 1
  30. #endif
  31. #ifndef SEEK_END
  32. # define SEEK_END 2
  33. #endif
  34. #ifndef SEEK_SET
  35. # define SEEK_SET 0
  36. #endif
  37. voidpf call_zopen64 (const zlib_filefunc64_32_def* pfilefunc,const void*filename,int mode)
  38. {
  39. if (pfilefunc->zfile_func64.zopen64_file != NULL)
  40. return (*(pfilefunc->zfile_func64.zopen64_file)) (pfilefunc->zfile_func64.opaque,filename,mode);
  41. return (*(pfilefunc->zopen32_file))(pfilefunc->zfile_func64.opaque,(const char*)filename,mode);
  42. }
  43. voidpf call_zopendisk64 OF((const zlib_filefunc64_32_def* pfilefunc, voidpf filestream, int number_disk, int mode))
  44. {
  45. if (pfilefunc->zfile_func64.zopendisk64_file != NULL)
  46. return (*(pfilefunc->zfile_func64.zopendisk64_file)) (pfilefunc->zfile_func64.opaque,filestream,number_disk,mode);
  47. return (*(pfilefunc->zopendisk32_file))(pfilefunc->zfile_func64.opaque,filestream,number_disk,mode);
  48. }
  49. long call_zseek64 (const zlib_filefunc64_32_def* pfilefunc,voidpf filestream, ZPOS64_T offset, int origin)
  50. {
  51. uLong offsetTruncated;
  52. if (pfilefunc->zfile_func64.zseek64_file != NULL)
  53. return (*(pfilefunc->zfile_func64.zseek64_file)) (pfilefunc->zfile_func64.opaque,filestream,offset,origin);
  54. offsetTruncated = (uLong)offset;
  55. if (offsetTruncated != offset)
  56. return -1;
  57. return (*(pfilefunc->zseek32_file))(pfilefunc->zfile_func64.opaque,filestream,offsetTruncated,origin);
  58. }
  59. ZPOS64_T call_ztell64 (const zlib_filefunc64_32_def* pfilefunc,voidpf filestream)
  60. {
  61. uLong tell_uLong;
  62. if (pfilefunc->zfile_func64.zseek64_file != NULL)
  63. return (*(pfilefunc->zfile_func64.ztell64_file)) (pfilefunc->zfile_func64.opaque,filestream);
  64. tell_uLong = (*(pfilefunc->ztell32_file))(pfilefunc->zfile_func64.opaque,filestream);
  65. if ((tell_uLong) == 0xffffffff)
  66. return (ZPOS64_T)-1;
  67. return tell_uLong;
  68. }
  69. void fill_zlib_filefunc64_32_def_from_filefunc32(zlib_filefunc64_32_def* p_filefunc64_32,const zlib_filefunc_def* p_filefunc32)
  70. {
  71. p_filefunc64_32->zfile_func64.zopen64_file = NULL;
  72. p_filefunc64_32->zfile_func64.zopendisk64_file = NULL;
  73. p_filefunc64_32->zopen32_file = p_filefunc32->zopen_file;
  74. p_filefunc64_32->zopendisk32_file = p_filefunc32->zopendisk_file;
  75. p_filefunc64_32->zfile_func64.zerror_file = p_filefunc32->zerror_file;
  76. p_filefunc64_32->zfile_func64.zread_file = p_filefunc32->zread_file;
  77. p_filefunc64_32->zfile_func64.zwrite_file = p_filefunc32->zwrite_file;
  78. p_filefunc64_32->zfile_func64.ztell64_file = NULL;
  79. p_filefunc64_32->zfile_func64.zseek64_file = NULL;
  80. p_filefunc64_32->zfile_func64.zclose_file = p_filefunc32->zclose_file;
  81. p_filefunc64_32->zfile_func64.zerror_file = p_filefunc32->zerror_file;
  82. p_filefunc64_32->zfile_func64.opaque = p_filefunc32->opaque;
  83. p_filefunc64_32->zseek32_file = p_filefunc32->zseek_file;
  84. p_filefunc64_32->ztell32_file = p_filefunc32->ztell_file;
  85. }
  86. static voidpf ZCALLBACK fopen_file_func OF((voidpf opaque, const char* filename, int mode));
  87. static uLong ZCALLBACK fread_file_func OF((voidpf opaque, voidpf stream, void* buf, uLong size));
  88. static uLong ZCALLBACK fwrite_file_func OF((voidpf opaque, voidpf stream, const void* buf,uLong size));
  89. static ZPOS64_T ZCALLBACK ftell64_file_func OF((voidpf opaque, voidpf stream));
  90. static long ZCALLBACK fseek64_file_func OF((voidpf opaque, voidpf stream, ZPOS64_T offset, int origin));
  91. static int ZCALLBACK fclose_file_func OF((voidpf opaque, voidpf stream));
  92. static int ZCALLBACK ferror_file_func OF((voidpf opaque, voidpf stream));
  93. typedef struct
  94. {
  95. FILE *file;
  96. int filenameLength;
  97. void *filename;
  98. } FILE_IOPOSIX;
  99. static voidpf file_build_ioposix(FILE *file, const char *filename)
  100. {
  101. FILE_IOPOSIX *ioposix = NULL;
  102. if (file == NULL)
  103. return NULL;
  104. ioposix = (FILE_IOPOSIX*)malloc(sizeof(FILE_IOPOSIX));
  105. ioposix->file = file;
  106. ioposix->filenameLength = (int)strlen(filename) + 1;
  107. ioposix->filename = (char*)malloc(ioposix->filenameLength * sizeof(char));
  108. strncpy(ioposix->filename, filename, ioposix->filenameLength);
  109. return (voidpf)ioposix;
  110. }
  111. static voidpf ZCALLBACK fopen_file_func (voidpf opaque, const char* filename, int mode)
  112. {
  113. FILE* file = NULL;
  114. const char* mode_fopen = NULL;
  115. if ((mode & ZLIB_FILEFUNC_MODE_READWRITEFILTER) == ZLIB_FILEFUNC_MODE_READ)
  116. mode_fopen = "rb";
  117. else if (mode & ZLIB_FILEFUNC_MODE_EXISTING)
  118. mode_fopen = "r+b";
  119. else if (mode & ZLIB_FILEFUNC_MODE_CREATE)
  120. mode_fopen = "wb";
  121. if ((filename != NULL) && (mode_fopen != NULL))
  122. {
  123. file = fopen(filename, mode_fopen);
  124. return file_build_ioposix(file, filename);
  125. }
  126. return file;
  127. }
  128. static voidpf ZCALLBACK fopen64_file_func (voidpf opaque, const void* filename, int mode)
  129. {
  130. FILE* file = NULL;
  131. const char* mode_fopen = NULL;
  132. if ((mode & ZLIB_FILEFUNC_MODE_READWRITEFILTER) == ZLIB_FILEFUNC_MODE_READ)
  133. mode_fopen = "rb";
  134. else if (mode & ZLIB_FILEFUNC_MODE_EXISTING)
  135. mode_fopen = "r+b";
  136. else if (mode & ZLIB_FILEFUNC_MODE_CREATE)
  137. mode_fopen = "wb";
  138. if ((filename != NULL) && (mode_fopen != NULL))
  139. {
  140. file = FOPEN_FUNC((const char*)filename, mode_fopen);
  141. return file_build_ioposix(file, (const char*)filename);
  142. }
  143. return file;
  144. }
  145. static voidpf ZCALLBACK fopendisk64_file_func (voidpf opaque, voidpf stream, int number_disk, int mode)
  146. {
  147. FILE_IOPOSIX *ioposix = NULL;
  148. char *diskFilename = NULL;
  149. voidpf ret = NULL;
  150. int i = 0;
  151. if (stream == NULL)
  152. return NULL;
  153. ioposix = (FILE_IOPOSIX*)stream;
  154. diskFilename = (char*)malloc(ioposix->filenameLength * sizeof(char));
  155. strncpy(diskFilename, ioposix->filename, ioposix->filenameLength);
  156. for (i = ioposix->filenameLength - 1; i >= 0; i -= 1)
  157. {
  158. if (diskFilename[i] != '.')
  159. continue;
  160. snprintf(&diskFilename[i], ioposix->filenameLength - i, ".z%02d", number_disk + 1);
  161. break;
  162. }
  163. if (i >= 0)
  164. ret = fopen64_file_func(opaque, diskFilename, mode);
  165. free(diskFilename);
  166. return ret;
  167. }
  168. static voidpf ZCALLBACK fopendisk_file_func (voidpf opaque, voidpf stream, int number_disk, int mode)
  169. {
  170. FILE_IOPOSIX *ioposix = NULL;
  171. char *diskFilename = NULL;
  172. voidpf ret = NULL;
  173. int i = 0;
  174. if (stream == NULL)
  175. return NULL;
  176. ioposix = (FILE_IOPOSIX*)stream;
  177. diskFilename = (char*)malloc(ioposix->filenameLength * sizeof(char));
  178. strncpy(diskFilename, ioposix->filename, ioposix->filenameLength);
  179. for (i = ioposix->filenameLength - 1; i >= 0; i -= 1)
  180. {
  181. if (diskFilename[i] != '.')
  182. continue;
  183. snprintf(&diskFilename[i], ioposix->filenameLength - i, ".z%02d", number_disk + 1);
  184. break;
  185. }
  186. if (i >= 0)
  187. ret = fopen_file_func(opaque, diskFilename, mode);
  188. free(diskFilename);
  189. return ret;
  190. }
  191. static uLong ZCALLBACK fread_file_func (voidpf opaque, voidpf stream, void* buf, uLong size)
  192. {
  193. FILE_IOPOSIX *ioposix = NULL;
  194. uLong ret;
  195. if (stream == NULL)
  196. return -1;
  197. ioposix = (FILE_IOPOSIX*)stream;
  198. ret = (uLong)fread(buf, 1, (size_t)size, ioposix->file);
  199. return ret;
  200. }
  201. static uLong ZCALLBACK fwrite_file_func (voidpf opaque, voidpf stream, const void* buf, uLong size)
  202. {
  203. FILE_IOPOSIX *ioposix = NULL;
  204. uLong ret;
  205. if (stream == NULL)
  206. return -1;
  207. ioposix = (FILE_IOPOSIX*)stream;
  208. ret = (uLong)fwrite(buf, 1, (size_t)size, ioposix->file);
  209. return ret;
  210. }
  211. static long ZCALLBACK ftell_file_func (voidpf opaque, voidpf stream)
  212. {
  213. FILE_IOPOSIX *ioposix = NULL;
  214. long ret = -1;
  215. if (stream == NULL)
  216. return ret;
  217. ioposix = (FILE_IOPOSIX*)stream;
  218. ret = ftell(ioposix->file);
  219. return ret;
  220. }
  221. static ZPOS64_T ZCALLBACK ftell64_file_func (voidpf opaque, voidpf stream)
  222. {
  223. FILE_IOPOSIX *ioposix = NULL;
  224. ZPOS64_T ret = -1;
  225. if (stream == NULL)
  226. return ret;
  227. ioposix = (FILE_IOPOSIX*)stream;
  228. ret = FTELLO_FUNC(ioposix->file);
  229. return ret;
  230. }
  231. static long ZCALLBACK fseek_file_func (voidpf opaque, voidpf stream, uLong offset, int origin)
  232. {
  233. FILE_IOPOSIX *ioposix = NULL;
  234. int fseek_origin = 0;
  235. long ret = 0;
  236. if (stream == NULL)
  237. return -1;
  238. ioposix = (FILE_IOPOSIX*)stream;
  239. switch (origin)
  240. {
  241. case ZLIB_FILEFUNC_SEEK_CUR:
  242. fseek_origin = SEEK_CUR;
  243. break;
  244. case ZLIB_FILEFUNC_SEEK_END:
  245. fseek_origin = SEEK_END;
  246. break;
  247. case ZLIB_FILEFUNC_SEEK_SET:
  248. fseek_origin = SEEK_SET;
  249. break;
  250. default:
  251. return -1;
  252. }
  253. if (fseek(ioposix->file, offset, fseek_origin) != 0)
  254. ret = -1;
  255. return ret;
  256. }
  257. static long ZCALLBACK fseek64_file_func (voidpf opaque, voidpf stream, ZPOS64_T offset, int origin)
  258. {
  259. FILE_IOPOSIX *ioposix = NULL;
  260. int fseek_origin = 0;
  261. long ret = 0;
  262. if (stream == NULL)
  263. return -1;
  264. ioposix = (FILE_IOPOSIX*)stream;
  265. switch (origin)
  266. {
  267. case ZLIB_FILEFUNC_SEEK_CUR:
  268. fseek_origin = SEEK_CUR;
  269. break;
  270. case ZLIB_FILEFUNC_SEEK_END:
  271. fseek_origin = SEEK_END;
  272. break;
  273. case ZLIB_FILEFUNC_SEEK_SET:
  274. fseek_origin = SEEK_SET;
  275. break;
  276. default:
  277. return -1;
  278. }
  279. if(FSEEKO_FUNC(ioposix->file, offset, fseek_origin) != 0)
  280. ret = -1;
  281. return ret;
  282. }
  283. static int ZCALLBACK fclose_file_func (voidpf opaque, voidpf stream)
  284. {
  285. FILE_IOPOSIX *ioposix = NULL;
  286. int ret = -1;
  287. if (stream == NULL)
  288. return ret;
  289. ioposix = (FILE_IOPOSIX*)stream;
  290. if (ioposix->filename != NULL)
  291. free(ioposix->filename);
  292. ret = fclose(ioposix->file);
  293. free(ioposix);
  294. return ret;
  295. }
  296. static int ZCALLBACK ferror_file_func (voidpf opaque, voidpf stream)
  297. {
  298. FILE_IOPOSIX *ioposix = NULL;
  299. int ret = -1;
  300. if (stream == NULL)
  301. return ret;
  302. ioposix = (FILE_IOPOSIX*)stream;
  303. ret = ferror(ioposix->file);
  304. return ret;
  305. }
  306. void fill_fopen_filefunc (zlib_filefunc_def* pzlib_filefunc_def)
  307. {
  308. pzlib_filefunc_def->zopen_file = fopen_file_func;
  309. pzlib_filefunc_def->zopendisk_file = fopendisk_file_func;
  310. pzlib_filefunc_def->zread_file = fread_file_func;
  311. pzlib_filefunc_def->zwrite_file = fwrite_file_func;
  312. pzlib_filefunc_def->ztell_file = ftell_file_func;
  313. pzlib_filefunc_def->zseek_file = fseek_file_func;
  314. pzlib_filefunc_def->zclose_file = fclose_file_func;
  315. pzlib_filefunc_def->zerror_file = ferror_file_func;
  316. pzlib_filefunc_def->opaque = NULL;
  317. }
  318. void fill_fopen64_filefunc (zlib_filefunc64_def* pzlib_filefunc_def)
  319. {
  320. pzlib_filefunc_def->zopen64_file = fopen64_file_func;
  321. pzlib_filefunc_def->zopendisk64_file = fopendisk64_file_func;
  322. pzlib_filefunc_def->zread_file = fread_file_func;
  323. pzlib_filefunc_def->zwrite_file = fwrite_file_func;
  324. pzlib_filefunc_def->ztell64_file = ftell64_file_func;
  325. pzlib_filefunc_def->zseek64_file = fseek64_file_func;
  326. pzlib_filefunc_def->zclose_file = fclose_file_func;
  327. pzlib_filefunc_def->zerror_file = ferror_file_func;
  328. pzlib_filefunc_def->opaque = NULL;
  329. }