MinizipExtensions.cpp 7.3 KB

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