MinizipExtensions.cpp 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  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 "FileStream.h"
  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. zlib_filefunc64_def CDefaultIOApi::getApiStructure()
  74. {
  75. return * FileStream::GetMinizipFilefunc();
  76. }
  77. ///CProxyIOApi
  78. CProxyIOApi::CProxyIOApi(CInputOutputStream * buffer):
  79. data(buffer)
  80. {
  81. }
  82. //must be instantiated in .cpp file for access to complete types of all member fields
  83. CProxyIOApi::~CProxyIOApi() = default;
  84. zlib_filefunc64_def CProxyIOApi::getApiStructure()
  85. {
  86. zlib_filefunc64_def api;
  87. api.opaque = this;
  88. api.zopen64_file = &openFileProxy;
  89. api.zread_file = &readFileProxy;
  90. api.zwrite_file = &writeFileProxy;
  91. api.ztell64_file = &tellFileProxy;
  92. api.zseek64_file = &seekFileProxy;
  93. api.zclose_file = &closeFileProxy;
  94. api.zerror_file = &errorFileProxy;
  95. return api;
  96. }
  97. voidpf ZCALLBACK CProxyIOApi::openFileProxy(voidpf opaque, const void * filename, int mode)
  98. {
  99. assert(opaque != nullptr);
  100. boost::filesystem::path path;
  101. if(filename != nullptr)
  102. path = static_cast<const boost::filesystem::path::value_type *>(filename);
  103. return (static_cast<CProxyIOApi *>(opaque))->openFile(path, mode);
  104. }
  105. uLong ZCALLBACK CProxyIOApi::readFileProxy(voidpf opaque, voidpf stream, void * buf, uLong size)
  106. {
  107. return streamRead<CInputOutputStream>(opaque, stream, buf, size);
  108. }
  109. uLong ZCALLBACK CProxyIOApi::writeFileProxy(voidpf opaque, voidpf stream, const void * buf, uLong size)
  110. {
  111. assert(opaque != nullptr);
  112. assert(stream != nullptr);
  113. auto * actualStream = static_cast<CInputOutputStream *>(stream);
  114. return static_cast<uLong>(actualStream->write(static_cast<const ui8 *>(buf), size));
  115. }
  116. ZPOS64_T ZCALLBACK CProxyIOApi::tellFileProxy(voidpf opaque, voidpf stream)
  117. {
  118. return streamTell<CInputOutputStream>(opaque, stream);
  119. }
  120. long ZCALLBACK CProxyIOApi::seekFileProxy(voidpf opaque, voidpf stream, ZPOS64_T offset, int origin)
  121. {
  122. return streamSeek<CInputOutputStream>(opaque, stream, offset, origin);
  123. }
  124. int ZCALLBACK CProxyIOApi::closeFileProxy(voidpf opaque, voidpf stream)
  125. {
  126. return streamProxyClose<CInputOutputStream>(opaque, stream);
  127. }
  128. int ZCALLBACK CProxyIOApi::errorFileProxy(voidpf opaque, voidpf stream)
  129. {
  130. return 0;
  131. }
  132. CInputOutputStream * CProxyIOApi::openFile(const boost::filesystem::path & filename, int mode)
  133. {
  134. logGlobal->trace("CProxyIOApi: stream opened for %s with mode %d", filename.string(), mode);
  135. data->seek(0);
  136. return data;
  137. }
  138. ///CProxyROIOApi
  139. CProxyROIOApi::CProxyROIOApi(CInputStream * buffer):
  140. data(buffer)
  141. {
  142. }
  143. //must be instantiated in .cpp file for access to complete types of all member fields
  144. CProxyROIOApi::~CProxyROIOApi() = default;
  145. zlib_filefunc64_def CProxyROIOApi::getApiStructure()
  146. {
  147. zlib_filefunc64_def api;
  148. api.opaque = this;
  149. api.zopen64_file = &openFileProxy;
  150. api.zread_file = &readFileProxy;
  151. api.zwrite_file = &writeFileProxy;
  152. api.ztell64_file = &tellFileProxy;
  153. api.zseek64_file = &seekFileProxy;
  154. api.zclose_file = &closeFileProxy;
  155. api.zerror_file = &errorFileProxy;
  156. return api;
  157. }
  158. CInputStream * CProxyROIOApi::openFile(const boost::filesystem::path& filename, int mode)
  159. {
  160. logGlobal->trace("CProxyROIOApi: stream opened for %s with mode %d", filename.string(), mode);
  161. data->seek(0);
  162. return data;
  163. }
  164. voidpf ZCALLBACK CProxyROIOApi::openFileProxy(voidpf opaque, const void* filename, int mode)
  165. {
  166. assert(opaque != nullptr);
  167. boost::filesystem::path path;
  168. if(filename != nullptr)
  169. path = static_cast<const boost::filesystem::path::value_type *>(filename);
  170. return (static_cast<CProxyROIOApi *>(opaque))->openFile(path, mode);
  171. }
  172. uLong ZCALLBACK CProxyROIOApi::readFileProxy(voidpf opaque, voidpf stream, void * buf, uLong size)
  173. {
  174. return streamRead<CInputStream>(opaque, stream, buf, size);
  175. }
  176. uLong ZCALLBACK CProxyROIOApi::writeFileProxy(voidpf opaque, voidpf stream, const void* buf, uLong size)
  177. {
  178. logGlobal->error("Attempt to write to read-only stream");
  179. return 0;
  180. }
  181. ZPOS64_T ZCALLBACK CProxyROIOApi::tellFileProxy(voidpf opaque, voidpf stream)
  182. {
  183. return streamTell<CInputStream>(opaque, stream);
  184. }
  185. long ZCALLBACK CProxyROIOApi::seekFileProxy(voidpf opaque, voidpf stream, ZPOS64_T offset, int origin)
  186. {
  187. return streamSeek<CInputStream>(opaque, stream, offset, origin);
  188. }
  189. int ZCALLBACK CProxyROIOApi::closeFileProxy(voidpf opaque, voidpf stream)
  190. {
  191. return streamProxyClose<CInputStream>(opaque, stream);
  192. }
  193. int ZCALLBACK CProxyROIOApi::errorFileProxy(voidpf opaque, voidpf stream)
  194. {
  195. return 0;
  196. }
  197. VCMI_LIB_NAMESPACE_END