MinizipExtensions.cpp 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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. static voidpf ZCALLBACK openFileProxy(voidpf opaque, const void * filename, int mode)
  13. {
  14. assert(opaque != nullptr);
  15. std::string filename_s;
  16. if(filename != nullptr)
  17. filename_s = (const char *)filename;
  18. return ((CIOApi *)opaque)->openFile(filename_s, mode);
  19. }
  20. static uLong ZCALLBACK readFileProxy(voidpf opaque, voidpf stream, void * buf, uLong size)
  21. {
  22. assert(opaque != nullptr);
  23. assert(stream != nullptr);
  24. CInputOutputStream * actualStream = static_cast<CInputOutputStream *>(stream);
  25. return actualStream->read((ui8 *)buf, size);
  26. }
  27. static uLong ZCALLBACK writeFileProxy(voidpf opaque, voidpf stream, const void * buf, uLong size)
  28. {
  29. assert(opaque != nullptr);
  30. assert(stream != nullptr);
  31. CInputOutputStream * actualStream = static_cast<CInputOutputStream *>(stream);
  32. return (uLong)actualStream->write((const ui8 *)buf, size);
  33. }
  34. static ZPOS64_T ZCALLBACK tellFileProxy(voidpf opaque, voidpf stream)
  35. {
  36. assert(opaque != nullptr);
  37. assert(stream != nullptr);
  38. CInputOutputStream * actualStream = static_cast<CInputOutputStream *>(stream);
  39. return actualStream->tell();
  40. }
  41. static long ZCALLBACK seekFileProxy(voidpf opaque, voidpf stream, ZPOS64_T offset, int origin)
  42. {
  43. assert(opaque != nullptr);
  44. assert(stream != nullptr);
  45. CInputOutputStream * actualStream = static_cast<CInputOutputStream *>(stream);
  46. long ret = 0;
  47. switch (origin)
  48. {
  49. case ZLIB_FILEFUNC_SEEK_CUR :
  50. actualStream->skip(offset);//TODO: should we check actual skipped?
  51. break;
  52. case ZLIB_FILEFUNC_SEEK_END :
  53. ret = -1;
  54. break;
  55. case ZLIB_FILEFUNC_SEEK_SET :
  56. ret = actualStream->seek(offset);
  57. break;
  58. default: ret = -1;
  59. }
  60. return ret;
  61. }
  62. static int ZCALLBACK closeFileProxy(voidpf opaque, voidpf stream)
  63. {
  64. assert(opaque != nullptr);
  65. assert(stream != nullptr);
  66. CInputOutputStream * actualStream = static_cast<CInputOutputStream *>(stream);
  67. delete actualStream;
  68. return 0;
  69. }
  70. static int ZCALLBACK errorFileProxy(voidpf opaque, voidpf stream)
  71. {
  72. return 0;
  73. }
  74. void CIOApi::fillApiStructure(zlib_filefunc64_def & api)
  75. {
  76. api.opaque = this;
  77. api.zopen64_file = &openFileProxy;
  78. api.zread_file = &readFileProxy;
  79. api.zwrite_file = &writeFileProxy;
  80. api.ztell64_file = &tellFileProxy;
  81. api.zseek64_file = &seekFileProxy;
  82. api.zclose_file = &closeFileProxy;
  83. api.zerror_file = &errorFileProxy;
  84. }