ioapi.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  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) && (!(defined(_CRT_SECURE_NO_WARNINGS)))
  9. #define _CRT_SECURE_NO_WARNINGS
  10. #endif
  11. #if defined(__APPLE__) || defined(IOAPI_NO_64)
  12. // In darwin and perhaps other BSD variants off_t is a 64 bit value, hence no need for specific 64 bit functions
  13. #define FOPEN_FUNC(filename, mode) fopen(filename, mode)
  14. #define FTELLO_FUNC(stream) ftello(stream)
  15. #define FSEEKO_FUNC(stream, offset, origin) fseeko(stream, offset, origin)
  16. #else
  17. #define FOPEN_FUNC(filename, mode) ff_fopen(filename, mode)
  18. #define FTELLO_FUNC(stream) ff_ftell(stream)
  19. #define FSEEKO_FUNC(stream, offset, origin) ff_fseek(stream, offset, origin)
  20. #endif
  21. #include "ff_stdio.h"
  22. #include "ioapi.h"
  23. voidpf call_zopen64 (const zlib_filefunc64_32_def* pfilefunc,const void*filename,int mode)
  24. {
  25. if (pfilefunc->zfile_func64.zopen64_file != NULL)
  26. return (*(pfilefunc->zfile_func64.zopen64_file)) (pfilefunc->zfile_func64.opaque,filename,mode);
  27. else
  28. {
  29. return (*(pfilefunc->zopen32_file))(pfilefunc->zfile_func64.opaque,(const char*)filename,mode);
  30. }
  31. }
  32. long call_zseek64 (const zlib_filefunc64_32_def* pfilefunc,voidpf filestream, ZPOS64_T offset, int origin)
  33. {
  34. if (pfilefunc->zfile_func64.zseek64_file != NULL)
  35. return (*(pfilefunc->zfile_func64.zseek64_file)) (pfilefunc->zfile_func64.opaque,filestream,offset,origin);
  36. else
  37. {
  38. uLong offsetTruncated = (uLong)offset;
  39. if (offsetTruncated != offset)
  40. return -1;
  41. else
  42. return (*(pfilefunc->zseek32_file))(pfilefunc->zfile_func64.opaque,filestream,offsetTruncated,origin);
  43. }
  44. }
  45. ZPOS64_T call_ztell64 (const zlib_filefunc64_32_def* pfilefunc,voidpf filestream)
  46. {
  47. if (pfilefunc->zfile_func64.zseek64_file != NULL)
  48. return (*(pfilefunc->zfile_func64.ztell64_file)) (pfilefunc->zfile_func64.opaque,filestream);
  49. else
  50. {
  51. uLong tell_uLong = (uLong)(*(pfilefunc->ztell32_file))(pfilefunc->zfile_func64.opaque,filestream);
  52. if ((tell_uLong) == MAXU32)
  53. return (ZPOS64_T)-1;
  54. else
  55. return tell_uLong;
  56. }
  57. }
  58. void fill_zlib_filefunc64_32_def_from_filefunc32(zlib_filefunc64_32_def* p_filefunc64_32,const zlib_filefunc_def* p_filefunc32)
  59. {
  60. p_filefunc64_32->zfile_func64.zopen64_file = NULL;
  61. p_filefunc64_32->zopen32_file = p_filefunc32->zopen_file;
  62. p_filefunc64_32->zfile_func64.zerror_file = p_filefunc32->zerror_file;
  63. p_filefunc64_32->zfile_func64.zread_file = p_filefunc32->zread_file;
  64. p_filefunc64_32->zfile_func64.zwrite_file = p_filefunc32->zwrite_file;
  65. p_filefunc64_32->zfile_func64.ztell64_file = NULL;
  66. p_filefunc64_32->zfile_func64.zseek64_file = NULL;
  67. p_filefunc64_32->zfile_func64.zclose_file = p_filefunc32->zclose_file;
  68. p_filefunc64_32->zfile_func64.zerror_file = p_filefunc32->zerror_file;
  69. p_filefunc64_32->zfile_func64.opaque = p_filefunc32->opaque;
  70. p_filefunc64_32->zseek32_file = p_filefunc32->zseek_file;
  71. p_filefunc64_32->ztell32_file = p_filefunc32->ztell_file;
  72. }
  73. static voidpf ZCALLBACK fopen_file_func OF((voidpf opaque, const char* filename, int mode));
  74. static uLong ZCALLBACK fread_file_func OF((voidpf opaque, voidpf stream, void* buf, uLong size));
  75. static uLong ZCALLBACK fwrite_file_func OF((voidpf opaque, voidpf stream, const void* buf,uLong size));
  76. static ZPOS64_T ZCALLBACK ftell64_file_func OF((voidpf opaque, voidpf stream));
  77. static long ZCALLBACK fseek64_file_func OF((voidpf opaque, voidpf stream, ZPOS64_T offset, int origin));
  78. static int ZCALLBACK fclose_file_func OF((voidpf opaque, voidpf stream));
  79. static int ZCALLBACK ferror_file_func OF((voidpf opaque, voidpf stream));
  80. static voidpf ZCALLBACK fopen_file_func (voidpf opaque, const char* filename, int mode)
  81. {
  82. FF_FILE* file = NULL;
  83. const char* mode_fopen = NULL;
  84. (void)opaque;
  85. if ((mode & ZLIB_FILEFUNC_MODE_READWRITEFILTER)==ZLIB_FILEFUNC_MODE_READ)
  86. mode_fopen = "rb";
  87. else
  88. if (mode & ZLIB_FILEFUNC_MODE_EXISTING)
  89. mode_fopen = "r+b";
  90. else
  91. if (mode & ZLIB_FILEFUNC_MODE_CREATE)
  92. mode_fopen = "wb";
  93. if ((filename!=NULL) && (mode_fopen != NULL))
  94. file = ff_fopen(filename, mode_fopen);
  95. return file;
  96. }
  97. static voidpf ZCALLBACK fopen64_file_func (voidpf opaque, const void* filename, int mode)
  98. {
  99. FF_FILE* file = NULL;
  100. const char* mode_fopen = NULL;
  101. (void)opaque;
  102. if ((mode & ZLIB_FILEFUNC_MODE_READWRITEFILTER)==ZLIB_FILEFUNC_MODE_READ)
  103. mode_fopen = "rb";
  104. else
  105. if (mode & ZLIB_FILEFUNC_MODE_EXISTING)
  106. mode_fopen = "r+b";
  107. else
  108. if (mode & ZLIB_FILEFUNC_MODE_CREATE)
  109. mode_fopen = "wb";
  110. if ((filename!=NULL) && (mode_fopen != NULL))
  111. file = FOPEN_FUNC((const char*)filename, mode_fopen);
  112. return file;
  113. }
  114. static uLong ZCALLBACK fread_file_func (voidpf opaque, voidpf stream, void* buf, uLong size)
  115. {
  116. uLong ret;
  117. (void)opaque;
  118. ret = (uLong)ff_fread(buf, 1, (size_t)size, (FF_FILE *)stream);
  119. return ret;
  120. }
  121. static uLong ZCALLBACK fwrite_file_func (voidpf opaque, voidpf stream, const void* buf, uLong size)
  122. {
  123. uLong ret;
  124. (void)opaque;
  125. ret = (uLong)ff_fwrite(buf, 1, (size_t)size, (FF_FILE *)stream);
  126. return ret;
  127. }
  128. static long ZCALLBACK ftell_file_func (voidpf opaque, voidpf stream)
  129. {
  130. long ret;
  131. (void)opaque;
  132. ret = ff_ftell((FF_FILE *)stream);
  133. return ret;
  134. }
  135. static ZPOS64_T ZCALLBACK ftell64_file_func (voidpf opaque, voidpf stream)
  136. {
  137. ZPOS64_T ret;
  138. (void)opaque;
  139. ret = (ZPOS64_T)FTELLO_FUNC((FF_FILE *)stream);
  140. return ret;
  141. }
  142. static long ZCALLBACK fseek_file_func (voidpf opaque, voidpf stream, uLong offset, int origin)
  143. {
  144. int fseek_origin=0;
  145. long ret;
  146. (void)opaque;
  147. switch (origin)
  148. {
  149. case ZLIB_FILEFUNC_SEEK_CUR :
  150. fseek_origin = SEEK_CUR;
  151. break;
  152. case ZLIB_FILEFUNC_SEEK_END :
  153. fseek_origin = SEEK_END;
  154. break;
  155. case ZLIB_FILEFUNC_SEEK_SET :
  156. fseek_origin = SEEK_SET;
  157. break;
  158. default: return -1;
  159. }
  160. ret = 0;
  161. if (ff_fseek((FF_FILE *)stream, (long)offset, fseek_origin) != 0)
  162. ret = -1;
  163. return ret;
  164. }
  165. static long ZCALLBACK fseek64_file_func (voidpf opaque, voidpf stream, ZPOS64_T offset, int origin)
  166. {
  167. int fseek_origin=0;
  168. long ret;
  169. (void)opaque;
  170. switch (origin)
  171. {
  172. case ZLIB_FILEFUNC_SEEK_CUR :
  173. fseek_origin = SEEK_CUR;
  174. break;
  175. case ZLIB_FILEFUNC_SEEK_END :
  176. fseek_origin = SEEK_END;
  177. break;
  178. case ZLIB_FILEFUNC_SEEK_SET :
  179. fseek_origin = SEEK_SET;
  180. break;
  181. default: return -1;
  182. }
  183. ret = 0;
  184. if(FSEEKO_FUNC((FF_FILE *)stream, (z_off_t)offset, fseek_origin) != 0)
  185. ret = -1;
  186. return ret;
  187. }
  188. static int ZCALLBACK fclose_file_func (voidpf opaque, voidpf stream)
  189. {
  190. int ret;
  191. (void)opaque;
  192. ret = ff_fclose((FF_FILE *)stream);
  193. return ret;
  194. }
  195. static int ZCALLBACK ferror_file_func (voidpf opaque, voidpf stream)
  196. {
  197. int ret;
  198. (void)opaque;
  199. ret = -1;//ferror((FF_FILE *)stream);
  200. return ret;
  201. }
  202. void fill_fopen_filefunc (pzlib_filefunc_def)
  203. zlib_filefunc_def* pzlib_filefunc_def;
  204. {
  205. pzlib_filefunc_def->zopen_file = fopen_file_func;
  206. pzlib_filefunc_def->zread_file = fread_file_func;
  207. pzlib_filefunc_def->zwrite_file = fwrite_file_func;
  208. pzlib_filefunc_def->ztell_file = ftell_file_func;
  209. pzlib_filefunc_def->zseek_file = fseek_file_func;
  210. pzlib_filefunc_def->zclose_file = fclose_file_func;
  211. pzlib_filefunc_def->zerror_file = ferror_file_func;
  212. pzlib_filefunc_def->opaque = NULL;
  213. }
  214. void fill_fopen64_filefunc (zlib_filefunc64_def* pzlib_filefunc_def)
  215. {
  216. pzlib_filefunc_def->zopen64_file = fopen64_file_func;
  217. pzlib_filefunc_def->zread_file = fread_file_func;
  218. pzlib_filefunc_def->zwrite_file = fwrite_file_func;
  219. pzlib_filefunc_def->ztell64_file = ftell64_file_func;
  220. pzlib_filefunc_def->zseek64_file = fseek64_file_func;
  221. pzlib_filefunc_def->zclose_file = fclose_file_func;
  222. pzlib_filefunc_def->zerror_file = ferror_file_func;
  223. pzlib_filefunc_def->opaque = NULL;
  224. }