MinizipExtensions.cpp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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. ///CIOApi
  15. voidpf ZCALLBACK CIOApi::openFileProxy(voidpf opaque, const void * filename, int mode)
  16. {
  17. assert(opaque != nullptr);
  18. boost::filesystem::path path;
  19. if(filename != nullptr)
  20. path = static_cast<const boost::filesystem::path::value_type *>(filename);
  21. return ((CIOApi *)opaque)->openFile(path, mode);
  22. }
  23. uLong ZCALLBACK CIOApi::readFileProxy(voidpf opaque, voidpf stream, void * buf, uLong size)
  24. {
  25. assert(opaque != nullptr);
  26. assert(stream != nullptr);
  27. CInputOutputStream * actualStream = static_cast<CInputOutputStream *>(stream);
  28. return actualStream->read((ui8 *)buf, size);
  29. }
  30. uLong ZCALLBACK CIOApi::writeFileProxy(voidpf opaque, voidpf stream, const void * buf, uLong size)
  31. {
  32. assert(opaque != nullptr);
  33. assert(stream != nullptr);
  34. CInputOutputStream * actualStream = static_cast<CInputOutputStream *>(stream);
  35. return (uLong)actualStream->write((const ui8 *)buf, size);
  36. }
  37. ZPOS64_T ZCALLBACK CIOApi::tellFileProxy(voidpf opaque, voidpf stream)
  38. {
  39. assert(opaque != nullptr);
  40. assert(stream != nullptr);
  41. CInputOutputStream * actualStream = static_cast<CInputOutputStream *>(stream);
  42. return actualStream->tell();
  43. }
  44. long ZCALLBACK CIOApi::seekFileProxy(voidpf opaque, voidpf stream, ZPOS64_T offset, int origin)
  45. {
  46. assert(opaque != nullptr);
  47. assert(stream != nullptr);
  48. CInputOutputStream * actualStream = static_cast<CInputOutputStream *>(stream);
  49. long ret = 0;
  50. switch (origin)
  51. {
  52. case ZLIB_FILEFUNC_SEEK_CUR :
  53. if(actualStream->skip(offset) != offset)
  54. ret = -1;
  55. break;
  56. case ZLIB_FILEFUNC_SEEK_END:
  57. {
  58. const si64 pos = actualStream->getSize() - offset;
  59. if(actualStream->seek(pos) != pos)
  60. ret = -1;
  61. }
  62. break;
  63. case ZLIB_FILEFUNC_SEEK_SET :
  64. if(actualStream->seek(offset) != offset)
  65. ret = -1;
  66. break;
  67. default: ret = -1;
  68. }
  69. if(ret == -1)
  70. logGlobal->error("CIOApi::seekFileProxy: seek failed");
  71. return ret;
  72. }
  73. int ZCALLBACK CIOApi::closeFileProxy(voidpf opaque, voidpf stream)
  74. {
  75. assert(opaque != nullptr);
  76. assert(stream != nullptr);
  77. CInputOutputStream * actualStream = static_cast<CInputOutputStream *>(stream);
  78. ((CIOApi *)opaque)->closeFile(actualStream);
  79. return 0;
  80. }
  81. int ZCALLBACK CIOApi::errorFileProxy(voidpf opaque, voidpf stream)
  82. {
  83. return 0;
  84. }
  85. zlib_filefunc64_def CIOApi::getApiStructure() const
  86. {
  87. zlib_filefunc64_def api;
  88. api.opaque = (void *) this;
  89. api.zopen64_file = &openFileProxy;
  90. api.zread_file = &readFileProxy;
  91. api.zwrite_file = &writeFileProxy;
  92. api.ztell64_file = &tellFileProxy;
  93. api.zseek64_file = &seekFileProxy;
  94. api.zclose_file = &closeFileProxy;
  95. api.zerror_file = &errorFileProxy;
  96. return api;
  97. }
  98. void CIOApi::closeFile(CInputOutputStream * stream) const
  99. {
  100. delete stream;
  101. }
  102. ///CDefaultIOApi
  103. CDefaultIOApi::CDefaultIOApi()
  104. {
  105. }
  106. CDefaultIOApi::~CDefaultIOApi()
  107. {
  108. }
  109. zlib_filefunc64_def CDefaultIOApi::getApiStructure() const
  110. {
  111. return * FileStream::GetMinizipFilefunc();
  112. }
  113. CInputOutputStream * CDefaultIOApi::openFile(const boost::filesystem::path & filename, int mode) const
  114. {
  115. throw new std::runtime_error("CDefaultIOApi::openFile call not expected.");
  116. }
  117. ///CProxyIOApi
  118. CProxyIOApi::CProxyIOApi(CInputOutputStream * buffer):
  119. data(buffer)
  120. {
  121. }
  122. CProxyIOApi::~CProxyIOApi()
  123. {
  124. }
  125. CInputOutputStream * CProxyIOApi::openFile(const boost::filesystem::path & filename, int mode) const
  126. {
  127. logGlobal->traceStream() << "CProxyIOApi: stream opened for " <<filename.string() <<" with mode "<<mode;
  128. data->seek(0);
  129. return data;
  130. }
  131. void CProxyIOApi::closeFile(CInputOutputStream * stream) const
  132. {
  133. logGlobal->traceStream() << "CProxyIOApi: stream closed";
  134. stream->seek(0);//stream is local singleton and even not owned, just seek
  135. }