/thirdparty/minizip/ioapi.c

http://crashrpt.googlecode.com/ · C · 236 lines · 197 code · 28 blank · 11 comment · 28 complexity · 50f1be176b4a28c94adef7d151ab540c MD5 · raw file

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