2
0

MinizipExtensions.cpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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. actualStream->skip(offset);//TODO: should we check actual skipped?
  53. break;
  54. case ZLIB_FILEFUNC_SEEK_END :
  55. actualStream->seek(actualStream->getSize() - offset);
  56. break;
  57. case ZLIB_FILEFUNC_SEEK_SET :
  58. actualStream->seek(offset);
  59. break;
  60. default: ret = -1;
  61. }
  62. return ret;
  63. }
  64. int ZCALLBACK CIOApi::closeFileProxy(voidpf opaque, voidpf stream)
  65. {
  66. assert(opaque != nullptr);
  67. assert(stream != nullptr);
  68. CInputOutputStream * actualStream = static_cast<CInputOutputStream *>(stream);
  69. ((CIOApi *)opaque)->closeFile(actualStream);
  70. return 0;
  71. }
  72. int ZCALLBACK CIOApi::errorFileProxy(voidpf opaque, voidpf stream)
  73. {
  74. return 0;
  75. }
  76. zlib_filefunc64_def CIOApi::getApiStructure() const
  77. {
  78. zlib_filefunc64_def api;
  79. api.opaque = (void *) this;
  80. api.zopen64_file = &openFileProxy;
  81. api.zread_file = &readFileProxy;
  82. api.zwrite_file = &writeFileProxy;
  83. api.ztell64_file = &tellFileProxy;
  84. api.zseek64_file = &seekFileProxy;
  85. api.zclose_file = &closeFileProxy;
  86. api.zerror_file = &errorFileProxy;
  87. return api;
  88. }
  89. void CIOApi::closeFile(CInputOutputStream * stream) const
  90. {
  91. delete stream;
  92. }
  93. ///CDefaultIOApi
  94. CDefaultIOApi::CDefaultIOApi()
  95. {
  96. }
  97. CDefaultIOApi::~CDefaultIOApi()
  98. {
  99. }
  100. zlib_filefunc64_def CDefaultIOApi::getApiStructure() const
  101. {
  102. zlib_filefunc64_def api;
  103. fill_fopen64_filefunc(&api);
  104. return api;
  105. }
  106. CInputOutputStream * CDefaultIOApi::openFile(const std::string& filename, int mode) const
  107. {
  108. throw new std::runtime_error("CDefaultIOApi::openFile call not expected.");
  109. }
  110. ///CProxyIOApi
  111. CProxyIOApi::CProxyIOApi(CInputOutputStream * buffer):
  112. data(buffer)
  113. {
  114. }
  115. CProxyIOApi::~CProxyIOApi()
  116. {
  117. }
  118. CInputOutputStream * CProxyIOApi::openFile(const std::string& filename, int mode) const
  119. {
  120. logGlobal->traceStream() << "CProxyIOApi: stream opened for " <<filename<<" with mode "<<mode;
  121. data->seek(0);
  122. return data;//todo: check that only one "copy" is opened
  123. }
  124. void CProxyIOApi::closeFile(CInputOutputStream * stream) const
  125. {
  126. logGlobal->traceStream() << "CProxyIOApi: stream closed";
  127. stream->seek(0);//stream is local singleton and even not owned, just seek
  128. }