ioapi.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. /* ioapi.h -- IO base function header for compress/uncompress .zip
  2. part of the MiniZip project - ( http://www.winimage.com/zLibDll/minizip.html )
  3. Copyright (C) 1998-2010 Gilles Vollant (minizip) ( http://www.winimage.com/zLibDll/minizip.html )
  4. Modifications for Zip64 support
  5. Copyright (C) 2009-2010 Mathias Svensson ( http://result42.com )
  6. For more info read MiniZip_info.txt
  7. */
  8. #if (defined(_WIN32))
  9. #define _CRT_SECURE_NO_WARNINGS
  10. #endif
  11. #include "ioapi.h"
  12. voidpf call_zopen64 (const zlib_filefunc64_32_def* pfilefunc,const void*filename,int mode)
  13. {
  14. if (pfilefunc->zfile_func64.zopen64_file != NULL)
  15. return (*(pfilefunc->zfile_func64.zopen64_file)) (pfilefunc->zfile_func64.opaque,filename,mode);
  16. else
  17. {
  18. return (*(pfilefunc->zopen32_file))(pfilefunc->zfile_func64.opaque,(const char*)filename,mode);
  19. }
  20. }
  21. long call_zseek64 (const zlib_filefunc64_32_def* pfilefunc,voidpf filestream, ZPOS64_T offset, int origin)
  22. {
  23. if (pfilefunc->zfile_func64.zseek64_file != NULL)
  24. return (*(pfilefunc->zfile_func64.zseek64_file)) (pfilefunc->zfile_func64.opaque,filestream,offset,origin);
  25. else
  26. {
  27. uLong offsetTruncated = (uLong)offset;
  28. if (offsetTruncated != offset)
  29. return -1;
  30. else
  31. return (*(pfilefunc->zseek32_file))(pfilefunc->zfile_func64.opaque,filestream,offsetTruncated,origin);
  32. }
  33. }
  34. ZPOS64_T call_ztell64 (const zlib_filefunc64_32_def* pfilefunc,voidpf filestream)
  35. {
  36. if (pfilefunc->zfile_func64.zseek64_file != NULL)
  37. return (*(pfilefunc->zfile_func64.ztell64_file)) (pfilefunc->zfile_func64.opaque,filestream);
  38. else
  39. {
  40. uLong tell_uLong = (*(pfilefunc->ztell32_file))(pfilefunc->zfile_func64.opaque,filestream);
  41. if ((tell_uLong) == ((uLong)-1))
  42. return (ZPOS64_T)-1;
  43. else
  44. return tell_uLong;
  45. }
  46. }
  47. void fill_zlib_filefunc64_32_def_from_filefunc32(zlib_filefunc64_32_def* p_filefunc64_32,const zlib_filefunc_def* p_filefunc32)
  48. {
  49. p_filefunc64_32->zfile_func64.zopen64_file = NULL;
  50. p_filefunc64_32->zopen32_file = p_filefunc32->zopen_file;
  51. p_filefunc64_32->zfile_func64.zerror_file = p_filefunc32->zerror_file;
  52. p_filefunc64_32->zfile_func64.zread_file = p_filefunc32->zread_file;
  53. p_filefunc64_32->zfile_func64.zwrite_file = p_filefunc32->zwrite_file;
  54. p_filefunc64_32->zfile_func64.ztell64_file = NULL;
  55. p_filefunc64_32->zfile_func64.zseek64_file = NULL;
  56. p_filefunc64_32->zfile_func64.zclose_file = p_filefunc32->zclose_file;
  57. p_filefunc64_32->zfile_func64.zerror_file = p_filefunc32->zerror_file;
  58. p_filefunc64_32->zfile_func64.opaque = p_filefunc32->opaque;
  59. p_filefunc64_32->zseek32_file = p_filefunc32->zseek_file;
  60. p_filefunc64_32->ztell32_file = p_filefunc32->ztell_file;
  61. p_filefunc64_32->zfile_func64.alloc_mem = p_filefunc32->alloc_mem;
  62. p_filefunc64_32->zfile_func64.free_mem = p_filefunc32->free_mem;
  63. }
  64. /*
  65. static voidpf ZCALLBACK fopen_file_func OF((voidpf opaque, const char* filename, int mode));
  66. static uLong ZCALLBACK fread_file_func OF((voidpf opaque, voidpf stream, void* buf, uLong size));
  67. static uLong ZCALLBACK fwrite_file_func OF((voidpf opaque, voidpf stream, const void* buf,uLong size));
  68. static ZPOS64_T ZCALLBACK ftell64_file_func OF((voidpf opaque, voidpf stream));
  69. static long ZCALLBACK fseek64_file_func OF((voidpf opaque, voidpf stream, ZPOS64_T offset, int origin));
  70. static int ZCALLBACK fclose_file_func OF((voidpf opaque, voidpf stream));
  71. static int ZCALLBACK ferror_file_func OF((voidpf opaque, voidpf stream));
  72. static voidpf ZCALLBACK fopen_file_func (voidpf opaque, const char* filename, int mode)
  73. {
  74. FILE* file = NULL;
  75. const char* mode_fopen = NULL;
  76. if ((mode & ZLIB_FILEFUNC_MODE_READWRITEFILTER)==ZLIB_FILEFUNC_MODE_READ)
  77. mode_fopen = "rb";
  78. else
  79. if (mode & ZLIB_FILEFUNC_MODE_EXISTING)
  80. mode_fopen = "r+b";
  81. else
  82. if (mode & ZLIB_FILEFUNC_MODE_CREATE)
  83. mode_fopen = "wb";
  84. if ((filename!=NULL) && (mode_fopen != NULL))
  85. file = fopen(filename, mode_fopen);
  86. return file;
  87. }
  88. static voidpf ZCALLBACK fopen64_file_func (voidpf opaque, const void* filename, int mode)
  89. {
  90. FILE* file = NULL;
  91. const char* mode_fopen = NULL;
  92. if ((mode & ZLIB_FILEFUNC_MODE_READWRITEFILTER)==ZLIB_FILEFUNC_MODE_READ)
  93. mode_fopen = "rb";
  94. else
  95. if (mode & ZLIB_FILEFUNC_MODE_EXISTING)
  96. mode_fopen = "r+b";
  97. else
  98. if (mode & ZLIB_FILEFUNC_MODE_CREATE)
  99. mode_fopen = "wb";
  100. if ((filename!=NULL) && (mode_fopen != NULL))
  101. file = fopen64((const char*)filename, mode_fopen);
  102. return file;
  103. }
  104. static uLong ZCALLBACK fread_file_func (voidpf opaque, voidpf stream, void* buf, uLong size)
  105. {
  106. uLong ret;
  107. ret = (uLong)fread(buf, 1, (size_t)size, (FILE *)stream);
  108. return ret;
  109. }
  110. static uLong ZCALLBACK fwrite_file_func (voidpf opaque, voidpf stream, const void* buf, uLong size)
  111. {
  112. uLong ret;
  113. ret = (uLong)fwrite(buf, 1, (size_t)size, (FILE *)stream);
  114. return ret;
  115. }
  116. static long ZCALLBACK ftell_file_func (voidpf opaque, voidpf stream)
  117. {
  118. long ret;
  119. ret = ftell((FILE *)stream);
  120. return ret;
  121. }
  122. static ZPOS64_T ZCALLBACK ftell64_file_func (voidpf opaque, voidpf stream)
  123. {
  124. ZPOS64_T ret;
  125. ret = ftello64((FILE *)stream);
  126. return ret;
  127. }
  128. static long ZCALLBACK fseek_file_func (voidpf opaque, voidpf stream, uLong offset, int origin)
  129. {
  130. int fseek_origin=0;
  131. long ret;
  132. switch (origin)
  133. {
  134. case ZLIB_FILEFUNC_SEEK_CUR :
  135. fseek_origin = SEEK_CUR;
  136. break;
  137. case ZLIB_FILEFUNC_SEEK_END :
  138. fseek_origin = SEEK_END;
  139. break;
  140. case ZLIB_FILEFUNC_SEEK_SET :
  141. fseek_origin = SEEK_SET;
  142. break;
  143. default: return -1;
  144. }
  145. ret = 0;
  146. if (fseek((FILE *)stream, offset, fseek_origin) != 0)
  147. ret = -1;
  148. return ret;
  149. }
  150. static long ZCALLBACK fseek64_file_func (voidpf opaque, voidpf stream, ZPOS64_T offset, int origin)
  151. {
  152. int fseek_origin=0;
  153. long ret;
  154. switch (origin)
  155. {
  156. case ZLIB_FILEFUNC_SEEK_CUR :
  157. fseek_origin = SEEK_CUR;
  158. break;
  159. case ZLIB_FILEFUNC_SEEK_END :
  160. fseek_origin = SEEK_END;
  161. break;
  162. case ZLIB_FILEFUNC_SEEK_SET :
  163. fseek_origin = SEEK_SET;
  164. break;
  165. default: return -1;
  166. }
  167. ret = 0;
  168. if(fseeko64((FILE *)stream, offset, fseek_origin) != 0)
  169. ret = -1;
  170. return ret;
  171. }
  172. static int ZCALLBACK fclose_file_func (voidpf opaque, voidpf stream)
  173. {
  174. int ret;
  175. ret = fclose((FILE *)stream);
  176. return ret;
  177. }
  178. static int ZCALLBACK ferror_file_func (voidpf opaque, voidpf stream)
  179. {
  180. int ret;
  181. ret = ferror((FILE *)stream);
  182. return ret;
  183. }
  184. void fill_fopen_filefunc (pzlib_filefunc_def)
  185. zlib_filefunc_def* pzlib_filefunc_def;
  186. {
  187. pzlib_filefunc_def->zopen_file = fopen_file_func;
  188. pzlib_filefunc_def->zread_file = fread_file_func;
  189. pzlib_filefunc_def->zwrite_file = fwrite_file_func;
  190. pzlib_filefunc_def->ztell_file = ftell_file_func;
  191. pzlib_filefunc_def->zseek_file = fseek_file_func;
  192. pzlib_filefunc_def->zclose_file = fclose_file_func;
  193. pzlib_filefunc_def->zerror_file = ferror_file_func;
  194. pzlib_filefunc_def->opaque = NULL;
  195. }
  196. void fill_fopen64_filefunc (zlib_filefunc64_def* pzlib_filefunc_def)
  197. {
  198. pzlib_filefunc_def->zopen64_file = fopen64_file_func;
  199. pzlib_filefunc_def->zread_file = fread_file_func;
  200. pzlib_filefunc_def->zwrite_file = fwrite_file_func;
  201. pzlib_filefunc_def->ztell64_file = ftell64_file_func;
  202. pzlib_filefunc_def->zseek64_file = fseek64_file_func;
  203. pzlib_filefunc_def->zclose_file = fclose_file_func;
  204. pzlib_filefunc_def->zerror_file = ferror_file_func;
  205. pzlib_filefunc_def->opaque = NULL;
  206. }
  207. */