MinizipExtensions.cpp 3.9 KB

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