ioapi.cpp 8.2 KB

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