2
0

MinizipExtensions.cpp 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  1. /*
  2. * MinizipExtensions.cpp, part of VCMI engine
  3. *
  4. * Authors: listed in file AUTHORS in main folder
  5. *
  6. * License: GNU General Public License v2.0 or later
  7. * Full text of license available in license.txt file, in main folder
  8. *
  9. */
  10. #include "StdInc.h"
  11. #include "MinizipExtensions.h"
  12. #include "CMemoryBuffer.h"
  13. #include <mutex>
  14. VCMI_LIB_NAMESPACE_BEGIN
  15. template<class Stream>
  16. inline uLong streamRead(voidpf opaque, voidpf stream, void * buf, uLong size)
  17. {
  18. assert(opaque != nullptr);
  19. assert(stream != nullptr);
  20. auto * actualStream = static_cast<Stream *>(stream);
  21. return static_cast<uLong>(actualStream->read(static_cast<ui8 *>(buf), size));
  22. }
  23. template<class Stream>
  24. inline ZPOS64_T streamTell(voidpf opaque, voidpf stream)
  25. {
  26. assert(opaque != nullptr);
  27. assert(stream != nullptr);
  28. auto * actualStream = static_cast<Stream *>(stream);
  29. return actualStream->tell();
  30. }
  31. template<class Stream>
  32. inline long streamSeek(voidpf opaque, voidpf stream, ZPOS64_T offset, int origin)
  33. {
  34. assert(opaque != nullptr);
  35. assert(stream != nullptr);
  36. auto * actualStream = static_cast<Stream *>(stream);
  37. long ret = 0;
  38. switch(origin)
  39. {
  40. case ZLIB_FILEFUNC_SEEK_CUR:
  41. if(actualStream->skip(offset) != offset)
  42. ret = -1;
  43. break;
  44. case ZLIB_FILEFUNC_SEEK_END:
  45. {
  46. const si64 pos = actualStream->getSize() - offset;
  47. if(actualStream->seek(pos) != pos)
  48. ret = -1;
  49. }
  50. break;
  51. case ZLIB_FILEFUNC_SEEK_SET:
  52. if(actualStream->seek(offset) != offset)
  53. ret = -1;
  54. break;
  55. default:
  56. ret = -1;
  57. }
  58. if(ret == -1)
  59. logGlobal->error("Stream seek failed");
  60. return 0;
  61. }
  62. template<class Stream>
  63. inline int streamProxyClose(voidpf opaque, voidpf stream)
  64. {
  65. assert(opaque != nullptr);
  66. assert(stream != nullptr);
  67. auto * actualStream = static_cast<Stream *>(stream);
  68. logGlobal->trace("Proxy stream closed");
  69. actualStream->seek(0);
  70. return 0;
  71. }
  72. ///CDefaultIOApi
  73. #define GETFILE static_cast<std::FILE*>(filePtr)
  74. #ifdef VCMI_WINDOWS
  75. #ifndef _CRT_SECURE_NO_WARNINGS
  76. #define _CRT_SECURE_NO_WARNINGS
  77. #endif
  78. #include <cwchar>
  79. #define CHAR_LITERAL(s) L##s
  80. using CharType = wchar_t;
  81. #else
  82. #define CHAR_LITERAL(s) s
  83. using CharType = char;
  84. #endif
  85. static inline FILE* do_open(const CharType* name, const CharType* mode)
  86. {
  87. #ifdef VCMI_WINDOWS
  88. return _wfopen(name, mode);
  89. #else
  90. return std::fopen(name, mode);
  91. #endif
  92. }
  93. static voidpf ZCALLBACK MinizipOpenFunc(voidpf opaque, const void* filename, int mode)
  94. {
  95. const CharType* mode_fopen = [mode]() -> const CharType*
  96. {
  97. if ((mode & ZLIB_FILEFUNC_MODE_READWRITEFILTER) == ZLIB_FILEFUNC_MODE_READ)
  98. return CHAR_LITERAL("rb");
  99. else if (mode & ZLIB_FILEFUNC_MODE_EXISTING)
  100. return CHAR_LITERAL("r+b");
  101. else if (mode & ZLIB_FILEFUNC_MODE_CREATE)
  102. return CHAR_LITERAL("wb");
  103. return nullptr;
  104. }();
  105. if (filename != nullptr && mode_fopen != nullptr)
  106. return do_open(static_cast<const CharType*>(filename), mode_fopen);
  107. else
  108. return nullptr;
  109. }
  110. zlib_filefunc64_def CDefaultIOApi::getApiStructure()
  111. {
  112. static zlib_filefunc64_def MinizipFilefunc;
  113. static std::once_flag flag;
  114. std::call_once(flag, []
  115. {
  116. fill_fopen64_filefunc(&MinizipFilefunc);
  117. MinizipFilefunc.zopen64_file = &MinizipOpenFunc;
  118. });
  119. return MinizipFilefunc;
  120. }
  121. #if MINIZIP_NEEDS_32BIT_FUNCS
  122. zlib_filefunc_def CDefaultIOApi::getApiStructure32()
  123. {
  124. static zlib_filefunc_def MinizipFilefunc;
  125. static std::once_flag flag;
  126. std::call_once(flag, []
  127. {
  128. fill_fopen_filefunc(&MinizipFilefunc);
  129. MinizipFilefunc.zopen_file = reinterpret_cast<void*(*)(void*, const char*, int)>(&MinizipOpenFunc);
  130. });
  131. return MinizipFilefunc;
  132. }
  133. #endif
  134. ///CProxyIOApi
  135. CProxyIOApi::CProxyIOApi(CInputOutputStream * buffer):
  136. data(buffer)
  137. {
  138. }
  139. //must be instantiated in .cpp file for access to complete types of all member fields
  140. CProxyIOApi::~CProxyIOApi() = default;
  141. zlib_filefunc64_def CProxyIOApi::getApiStructure()
  142. {
  143. zlib_filefunc64_def api;
  144. api.opaque = this;
  145. api.zopen64_file = &openFileProxy;
  146. api.zread_file = &readFileProxy;
  147. api.zwrite_file = &writeFileProxy;
  148. api.ztell64_file = &tellFileProxy;
  149. api.zseek64_file = &seekFileProxy;
  150. api.zclose_file = &closeFileProxy;
  151. api.zerror_file = &errorFileProxy;
  152. return api;
  153. }
  154. voidpf ZCALLBACK CProxyIOApi::openFileProxy(voidpf opaque, const void * filename, int mode)
  155. {
  156. assert(opaque != nullptr);
  157. boost::filesystem::path path;
  158. if(filename != nullptr)
  159. path = static_cast<const boost::filesystem::path::value_type *>(filename);
  160. return (static_cast<CProxyIOApi *>(opaque))->openFile(path, mode);
  161. }
  162. uLong ZCALLBACK CProxyIOApi::readFileProxy(voidpf opaque, voidpf stream, void * buf, uLong size)
  163. {
  164. return streamRead<CInputOutputStream>(opaque, stream, buf, size);
  165. }
  166. uLong ZCALLBACK CProxyIOApi::writeFileProxy(voidpf opaque, voidpf stream, const void * buf, uLong size)
  167. {
  168. assert(opaque != nullptr);
  169. assert(stream != nullptr);
  170. auto * actualStream = static_cast<CInputOutputStream *>(stream);
  171. return static_cast<uLong>(actualStream->write(static_cast<const ui8 *>(buf), size));
  172. }
  173. ZPOS64_T ZCALLBACK CProxyIOApi::tellFileProxy(voidpf opaque, voidpf stream)
  174. {
  175. return streamTell<CInputOutputStream>(opaque, stream);
  176. }
  177. long ZCALLBACK CProxyIOApi::seekFileProxy(voidpf opaque, voidpf stream, ZPOS64_T offset, int origin)
  178. {
  179. return streamSeek<CInputOutputStream>(opaque, stream, offset, origin);
  180. }
  181. int ZCALLBACK CProxyIOApi::closeFileProxy(voidpf opaque, voidpf stream)
  182. {
  183. return streamProxyClose<CInputOutputStream>(opaque, stream);
  184. }
  185. int ZCALLBACK CProxyIOApi::errorFileProxy(voidpf opaque, voidpf stream)
  186. {
  187. return 0;
  188. }
  189. CInputOutputStream * CProxyIOApi::openFile(const boost::filesystem::path & filename, int mode)
  190. {
  191. logGlobal->trace("CProxyIOApi: stream opened for %s with mode %d", filename.string(), mode);
  192. data->seek(0);
  193. return data;
  194. }
  195. ///CProxyROIOApi
  196. CProxyROIOApi::CProxyROIOApi(CInputStream * buffer):
  197. data(buffer)
  198. {
  199. }
  200. //must be instantiated in .cpp file for access to complete types of all member fields
  201. CProxyROIOApi::~CProxyROIOApi() = default;
  202. zlib_filefunc64_def CProxyROIOApi::getApiStructure()
  203. {
  204. zlib_filefunc64_def api;
  205. api.opaque = this;
  206. api.zopen64_file = &openFileProxy;
  207. api.zread_file = &readFileProxy;
  208. api.zwrite_file = &writeFileProxy;
  209. api.ztell64_file = &tellFileProxy;
  210. api.zseek64_file = &seekFileProxy;
  211. api.zclose_file = &closeFileProxy;
  212. api.zerror_file = &errorFileProxy;
  213. return api;
  214. }
  215. CInputStream * CProxyROIOApi::openFile(const boost::filesystem::path& filename, int mode)
  216. {
  217. logGlobal->trace("CProxyROIOApi: stream opened for %s with mode %d", filename.string(), mode);
  218. data->seek(0);
  219. return data;
  220. }
  221. voidpf ZCALLBACK CProxyROIOApi::openFileProxy(voidpf opaque, const void* filename, int mode)
  222. {
  223. assert(opaque != nullptr);
  224. boost::filesystem::path path;
  225. if(filename != nullptr)
  226. path = static_cast<const boost::filesystem::path::value_type *>(filename);
  227. return (static_cast<CProxyROIOApi *>(opaque))->openFile(path, mode);
  228. }
  229. uLong ZCALLBACK CProxyROIOApi::readFileProxy(voidpf opaque, voidpf stream, void * buf, uLong size)
  230. {
  231. return streamRead<CInputStream>(opaque, stream, buf, size);
  232. }
  233. uLong ZCALLBACK CProxyROIOApi::writeFileProxy(voidpf opaque, voidpf stream, const void* buf, uLong size)
  234. {
  235. logGlobal->error("Attempt to write to read-only stream");
  236. return 0;
  237. }
  238. ZPOS64_T ZCALLBACK CProxyROIOApi::tellFileProxy(voidpf opaque, voidpf stream)
  239. {
  240. return streamTell<CInputStream>(opaque, stream);
  241. }
  242. long ZCALLBACK CProxyROIOApi::seekFileProxy(voidpf opaque, voidpf stream, ZPOS64_T offset, int origin)
  243. {
  244. return streamSeek<CInputStream>(opaque, stream, offset, origin);
  245. }
  246. int ZCALLBACK CProxyROIOApi::closeFileProxy(voidpf opaque, voidpf stream)
  247. {
  248. return streamProxyClose<CInputStream>(opaque, stream);
  249. }
  250. int ZCALLBACK CProxyROIOApi::errorFileProxy(voidpf opaque, voidpf stream)
  251. {
  252. return 0;
  253. }
  254. VCMI_LIB_NAMESPACE_END