ioapi.c 8.3 KB

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