miniz_extension.cpp 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. #include <exception>
  2. #include "miniz_extension.hpp"
  3. #if defined(_MSC_VER) || defined(__MINGW64__)
  4. #include "boost/nowide/cstdio.hpp"
  5. #endif
  6. #include "I18N.hpp"
  7. //! macro used to mark string used at localization,
  8. //! return same string
  9. #define L(s) Slic3r::I18N::translate(s)
  10. namespace Slic3r {
  11. namespace {
  12. bool open_zip(mz_zip_archive *zip, const char *fname, bool isread)
  13. {
  14. if (!zip) return false;
  15. const char *mode = isread ? "rb" : "wb";
  16. FILE *f = nullptr;
  17. #if defined(_MSC_VER) || defined(__MINGW64__)
  18. f = boost::nowide::fopen(fname, mode);
  19. #elif defined(__GNUC__) && defined(_LARGEFILE64_SOURCE)
  20. f = fopen64(fname, mode);
  21. #else
  22. f = fopen(fname, mode);
  23. #endif
  24. if (!f) {
  25. zip->m_last_error = MZ_ZIP_FILE_OPEN_FAILED;
  26. return false;
  27. }
  28. bool res = false;
  29. if (isread)
  30. {
  31. res = mz_zip_reader_init_cfile(zip, f, 0, 0);
  32. if (!res)
  33. // if we get here it means we tried to open a non-zip file
  34. // we need to close the file here because the call to mz_zip_get_cfile() made into close_zip() returns a null pointer
  35. // see: https://github.com/prusa3d/PrusaSlicer/issues/3536
  36. fclose(f);
  37. }
  38. else
  39. res = mz_zip_writer_init_cfile(zip, f, 0);
  40. return res;
  41. }
  42. bool close_zip(mz_zip_archive *zip, bool isread)
  43. {
  44. bool ret = false;
  45. if (zip) {
  46. FILE *f = mz_zip_get_cfile(zip);
  47. ret = bool(isread ? mz_zip_reader_end(zip)
  48. : mz_zip_writer_end(zip));
  49. if (f) fclose(f);
  50. }
  51. return ret;
  52. }
  53. }
  54. bool open_zip_reader(mz_zip_archive *zip, const std::string &fname)
  55. {
  56. return open_zip(zip, fname.c_str(), true);
  57. }
  58. bool open_zip_writer(mz_zip_archive *zip, const std::string &fname)
  59. {
  60. return open_zip(zip, fname.c_str(), false);
  61. }
  62. bool close_zip_reader(mz_zip_archive *zip) { return close_zip(zip, true); }
  63. bool close_zip_writer(mz_zip_archive *zip) { return close_zip(zip, false); }
  64. MZ_Archive::MZ_Archive()
  65. {
  66. mz_zip_zero_struct(&arch);
  67. }
  68. std::string MZ_Archive::get_errorstr(mz_zip_error mz_err)
  69. {
  70. switch (mz_err)
  71. {
  72. case MZ_ZIP_NO_ERROR:
  73. return "no error";
  74. case MZ_ZIP_UNDEFINED_ERROR:
  75. return L("undefined error");
  76. case MZ_ZIP_TOO_MANY_FILES:
  77. return L("too many files");
  78. case MZ_ZIP_FILE_TOO_LARGE:
  79. return L("file too large");
  80. case MZ_ZIP_UNSUPPORTED_METHOD:
  81. return L("unsupported method");
  82. case MZ_ZIP_UNSUPPORTED_ENCRYPTION:
  83. return L("unsupported encryption");
  84. case MZ_ZIP_UNSUPPORTED_FEATURE:
  85. return L("unsupported feature");
  86. case MZ_ZIP_FAILED_FINDING_CENTRAL_DIR:
  87. return L("failed finding central directory");
  88. case MZ_ZIP_NOT_AN_ARCHIVE:
  89. return L("not a ZIP archive");
  90. case MZ_ZIP_INVALID_HEADER_OR_CORRUPTED:
  91. return L("invalid header or archive is corrupted");
  92. case MZ_ZIP_UNSUPPORTED_MULTIDISK:
  93. return L("unsupported multidisk archive");
  94. case MZ_ZIP_DECOMPRESSION_FAILED:
  95. return L("decompression failed or archive is corrupted");
  96. case MZ_ZIP_COMPRESSION_FAILED:
  97. return L("compression failed");
  98. case MZ_ZIP_UNEXPECTED_DECOMPRESSED_SIZE:
  99. return L("unexpected decompressed size");
  100. case MZ_ZIP_CRC_CHECK_FAILED:
  101. return L("CRC-32 check failed");
  102. case MZ_ZIP_UNSUPPORTED_CDIR_SIZE:
  103. return L("unsupported central directory size");
  104. case MZ_ZIP_ALLOC_FAILED:
  105. return L("allocation failed");
  106. case MZ_ZIP_FILE_OPEN_FAILED:
  107. return L("file open failed");
  108. case MZ_ZIP_FILE_CREATE_FAILED:
  109. return L("file create failed");
  110. case MZ_ZIP_FILE_WRITE_FAILED:
  111. return L("file write failed");
  112. case MZ_ZIP_FILE_READ_FAILED:
  113. return L("file read failed");
  114. case MZ_ZIP_FILE_CLOSE_FAILED:
  115. return L("file close failed");
  116. case MZ_ZIP_FILE_SEEK_FAILED:
  117. return L("file seek failed");
  118. case MZ_ZIP_FILE_STAT_FAILED:
  119. return L("file stat failed");
  120. case MZ_ZIP_INVALID_PARAMETER:
  121. return L("invalid parameter");
  122. case MZ_ZIP_INVALID_FILENAME:
  123. return L("invalid filename");
  124. case MZ_ZIP_BUF_TOO_SMALL:
  125. return L("buffer too small");
  126. case MZ_ZIP_INTERNAL_ERROR:
  127. return L("internal error");
  128. case MZ_ZIP_FILE_NOT_FOUND:
  129. return L("file not found");
  130. case MZ_ZIP_ARCHIVE_TOO_LARGE:
  131. return L("archive is too large");
  132. case MZ_ZIP_VALIDATION_FAILED:
  133. return L("validation failed");
  134. case MZ_ZIP_WRITE_CALLBACK_FAILED:
  135. return L("write calledback failed");
  136. default:
  137. break;
  138. }
  139. return "unknown error";
  140. }
  141. } // namespace Slic3r