mirror of
				https://github.com/SoftFever/OrcaSlicer.git
				synced 2025-10-30 20:21:12 -06:00 
			
		
		
		
	Adjust miniz usage to use nowide FILE streams everywhere.
This commit is contained in:
		
							parent
							
								
									15620a2fc2
								
							
						
					
					
						commit
						460887cdde
					
				
					 4 changed files with 83 additions and 34 deletions
				
			
		|  | @ -13,6 +13,7 @@ | |||
| #include <boost/algorithm/string/predicate.hpp> | ||||
| #include <boost/filesystem/operations.hpp> | ||||
| #include <boost/nowide/fstream.hpp> | ||||
| #include <boost/nowide/cstdio.hpp> | ||||
| 
 | ||||
| #include <expat.h> | ||||
| #include <Eigen/Dense> | ||||
|  | @ -187,6 +188,24 @@ bool is_valid_object_type(const std::string& type) | |||
| 
 | ||||
| namespace Slic3r { | ||||
| 
 | ||||
| namespace { | ||||
| void close_archive_reader(mz_zip_archive *arch) { | ||||
|     if (arch) { | ||||
|         FILE *f = mz_zip_get_cfile(arch); | ||||
|         mz_zip_reader_end(arch); | ||||
|         if (f) fclose(f); | ||||
|     } | ||||
| } | ||||
| 
 | ||||
| void close_archive_writer(mz_zip_archive *arch) { | ||||
|     if (arch) { | ||||
|         FILE *f = mz_zip_get_cfile(arch); | ||||
|         mz_zip_writer_end(arch); | ||||
|         if (f) fclose(f); | ||||
|     } | ||||
| } | ||||
| } | ||||
| 
 | ||||
|     // Base class with error messages management
 | ||||
|     class _3MF_Base | ||||
|     { | ||||
|  | @ -503,7 +522,8 @@ namespace Slic3r { | |||
|         mz_zip_archive archive; | ||||
|         mz_zip_zero_struct(&archive); | ||||
| 
 | ||||
|         mz_bool res = mz_zip_reader_init_file(&archive, filename.c_str(), 0); | ||||
|         FILE *f = boost::nowide::fopen(filename.c_str(), "rb"); | ||||
|         auto res = mz_bool(f != nullptr && mz_zip_reader_init_cfile(&archive, f, -1, 0)); | ||||
|         if (res == 0) | ||||
|         { | ||||
|             add_error("Unable to open the file"); | ||||
|  | @ -529,7 +549,7 @@ namespace Slic3r { | |||
|                     // valid model name -> extract model
 | ||||
|                     if (!_extract_model_from_archive(archive, stat)) | ||||
|                     { | ||||
|                         mz_zip_reader_end(&archive); | ||||
|                         close_archive_reader(&archive); | ||||
|                         add_error("Archive does not contain a valid model"); | ||||
|                         return false; | ||||
|                     } | ||||
|  | @ -565,7 +585,7 @@ namespace Slic3r { | |||
|                     // extract slic3r model config file
 | ||||
|                     if (!_extract_model_config_from_archive(archive, stat, model)) | ||||
|                     { | ||||
|                         mz_zip_reader_end(&archive); | ||||
|                         close_archive_reader(&archive); | ||||
|                         add_error("Archive does not contain a valid model config"); | ||||
|                         return false; | ||||
|                     } | ||||
|  | @ -573,7 +593,7 @@ namespace Slic3r { | |||
|             } | ||||
|         } | ||||
| 
 | ||||
|         mz_zip_reader_end(&archive); | ||||
|         close_archive_reader(&archive); | ||||
| 
 | ||||
|         for (const IdToModelObjectMap::value_type& object : m_objects) | ||||
|         { | ||||
|  | @ -1639,7 +1659,8 @@ namespace Slic3r { | |||
|         mz_zip_archive archive; | ||||
|         mz_zip_zero_struct(&archive); | ||||
| 
 | ||||
|         mz_bool res = mz_zip_writer_init_file(&archive, filename.c_str(), 0); | ||||
|         FILE *f = boost::nowide::fopen(filename.c_str(), "wb"); | ||||
|         auto res = mz_bool(f != nullptr && mz_zip_writer_init_cfile(&archive, f, 0)); | ||||
|         if (res == 0) | ||||
|         { | ||||
|             add_error("Unable to open the file"); | ||||
|  | @ -1650,7 +1671,7 @@ namespace Slic3r { | |||
|         // The content of this file is the same for each PrusaSlicer 3mf.
 | ||||
|         if (!_add_content_types_file_to_archive(archive)) | ||||
|         { | ||||
|             mz_zip_writer_end(&archive); | ||||
|             close_archive_writer(&archive); | ||||
|             boost::filesystem::remove(filename); | ||||
|             return false; | ||||
|         } | ||||
|  | @ -1660,7 +1681,7 @@ namespace Slic3r { | |||
|         // The relationshis file contains a reference to the geometry file "3D/3dmodel.model", the name was chosen to be compatible with CURA.
 | ||||
|         if (!_add_relationships_file_to_archive(archive)) | ||||
|         { | ||||
|             mz_zip_writer_end(&archive); | ||||
|             close_archive_writer(&archive); | ||||
|             boost::filesystem::remove(filename); | ||||
|             return false; | ||||
|         } | ||||
|  | @ -1670,7 +1691,7 @@ namespace Slic3r { | |||
|         IdToObjectDataMap objects_data; | ||||
|         if (!_add_model_file_to_archive(archive, model, objects_data)) | ||||
|         { | ||||
|             mz_zip_writer_end(&archive); | ||||
|             close_archive_writer(&archive); | ||||
|             boost::filesystem::remove(filename); | ||||
|             return false; | ||||
|         } | ||||
|  | @ -1680,7 +1701,7 @@ namespace Slic3r { | |||
|         // The index differes from the index of an object ID of an object instance of a 3MF file!
 | ||||
|         if (!_add_layer_height_profile_file_to_archive(archive, model)) | ||||
|         { | ||||
|             mz_zip_writer_end(&archive); | ||||
|             close_archive_writer(&archive); | ||||
|             boost::filesystem::remove(filename); | ||||
|             return false; | ||||
|         } | ||||
|  | @ -1690,7 +1711,7 @@ namespace Slic3r { | |||
|         // The index differes from the index of an object ID of an object instance of a 3MF file!
 | ||||
|         if (!_add_sla_support_points_file_to_archive(archive, model)) | ||||
|         { | ||||
|             mz_zip_writer_end(&archive); | ||||
|             close_archive_writer(&archive); | ||||
|             boost::filesystem::remove(filename); | ||||
|             return false; | ||||
|         } | ||||
|  | @ -1701,7 +1722,7 @@ namespace Slic3r { | |||
|         { | ||||
|             if (!_add_print_config_file_to_archive(archive, *config)) | ||||
|             { | ||||
|                 mz_zip_writer_end(&archive); | ||||
|                 close_archive_writer(&archive); | ||||
|                 boost::filesystem::remove(filename); | ||||
|                 return false; | ||||
|             } | ||||
|  | @ -1713,20 +1734,20 @@ namespace Slic3r { | |||
|         // is stored here as well.
 | ||||
|         if (!_add_model_config_file_to_archive(archive, model, objects_data)) | ||||
|         { | ||||
|             mz_zip_writer_end(&archive); | ||||
|             close_archive_writer(&archive); | ||||
|             boost::filesystem::remove(filename); | ||||
|             return false; | ||||
|         } | ||||
| 
 | ||||
|         if (!mz_zip_writer_finalize_archive(&archive)) | ||||
|         { | ||||
|             mz_zip_writer_end(&archive); | ||||
|             close_archive_writer(&archive); | ||||
|             boost::filesystem::remove(filename); | ||||
|             add_error("Unable to finalize the archive"); | ||||
|             return false; | ||||
|         } | ||||
| 
 | ||||
|         mz_zip_writer_end(&archive); | ||||
|         close_archive_writer(&archive); | ||||
| 
 | ||||
|         return true; | ||||
|     } | ||||
|  |  | |||
|  | @ -712,19 +712,37 @@ bool load_amf_file(const char *path, DynamicPrintConfig *config, Model *model) | |||
|     return result; | ||||
| } | ||||
| 
 | ||||
| namespace { | ||||
| void close_archive_reader(mz_zip_archive *arch) { | ||||
|     if (arch) { | ||||
|         FILE *f = mz_zip_get_cfile(arch); | ||||
|         mz_zip_reader_end(arch); | ||||
|         if (f) fclose(f); | ||||
|     } | ||||
| } | ||||
| 
 | ||||
| void close_archive_writer(mz_zip_archive *arch) { | ||||
|     if (arch) { | ||||
|         FILE *f = mz_zip_get_cfile(arch); | ||||
|         mz_zip_writer_end(arch); | ||||
|         if (f) fclose(f); | ||||
|     } | ||||
| } | ||||
| } | ||||
| 
 | ||||
| bool extract_model_from_archive(mz_zip_archive& archive, const mz_zip_archive_file_stat& stat, DynamicPrintConfig* config, Model* model, unsigned int& version) | ||||
| { | ||||
|     if (stat.m_uncomp_size == 0) | ||||
|     { | ||||
|         printf("Found invalid size\n"); | ||||
|         mz_zip_reader_end(&archive); | ||||
|         close_archive_reader(&archive); | ||||
|         return false; | ||||
|     } | ||||
| 
 | ||||
|     XML_Parser parser = XML_ParserCreate(nullptr); // encoding
 | ||||
|     if (!parser) { | ||||
|         printf("Couldn't allocate memory for parser\n"); | ||||
|         mz_zip_reader_end(&archive); | ||||
|         close_archive_reader(&archive); | ||||
|         return false; | ||||
|     } | ||||
| 
 | ||||
|  | @ -737,7 +755,7 @@ bool extract_model_from_archive(mz_zip_archive& archive, const mz_zip_archive_fi | |||
|     if (parser_buffer == nullptr) | ||||
|     { | ||||
|         printf("Unable to create buffer\n"); | ||||
|         mz_zip_reader_end(&archive); | ||||
|         close_archive_reader(&archive); | ||||
|         return false; | ||||
|     } | ||||
| 
 | ||||
|  | @ -745,14 +763,14 @@ bool extract_model_from_archive(mz_zip_archive& archive, const mz_zip_archive_fi | |||
|     if (res == 0) | ||||
|     { | ||||
|         printf("Error while reading model data to buffer\n"); | ||||
|         mz_zip_reader_end(&archive); | ||||
|         close_archive_reader(&archive); | ||||
|         return false; | ||||
|     } | ||||
| 
 | ||||
|     if (!XML_ParseBuffer(parser, (int)stat.m_uncomp_size, 1)) | ||||
|     { | ||||
|         printf("Error (%s) while parsing xml file at line %d\n", XML_ErrorString(XML_GetErrorCode(parser)), XML_GetCurrentLineNumber(parser)); | ||||
|         mz_zip_reader_end(&archive); | ||||
|         close_archive_reader(&archive); | ||||
|         return false; | ||||
|     } | ||||
| 
 | ||||
|  | @ -774,8 +792,10 @@ bool load_amf_archive(const char *path, DynamicPrintConfig *config, Model *model | |||
|     mz_zip_archive archive; | ||||
|     mz_zip_zero_struct(&archive); | ||||
| 
 | ||||
|     mz_bool res = mz_zip_reader_init_file(&archive, path, 0); | ||||
|     if (res == 0) | ||||
|     FILE * f = boost::nowide::fopen(path, "rb"); | ||||
|     auto res = mz_bool(f == nullptr ? MZ_FALSE : MZ_TRUE); | ||||
|     res = res && mz_zip_reader_init_cfile(&archive, f, -1, 0); | ||||
|     if (res == MZ_FALSE) | ||||
|     { | ||||
|         printf("Unable to init zip reader\n"); | ||||
|         return false; | ||||
|  | @ -793,7 +813,7 @@ bool load_amf_archive(const char *path, DynamicPrintConfig *config, Model *model | |||
|             { | ||||
|                 if (!extract_model_from_archive(archive, stat, config, model, version)) | ||||
|                 { | ||||
|                     mz_zip_reader_end(&archive); | ||||
|                     close_archive_reader(&archive); | ||||
|                     printf("Archive does not contain a valid model"); | ||||
|                     return false; | ||||
|                 } | ||||
|  | @ -814,7 +834,7 @@ bool load_amf_archive(const char *path, DynamicPrintConfig *config, Model *model | |||
|     } | ||||
| #endif // forward compatibility
 | ||||
| 
 | ||||
|     mz_zip_reader_end(&archive); | ||||
|     close_archive_reader(&archive); | ||||
|     return true; | ||||
| } | ||||
| 
 | ||||
|  | @ -854,7 +874,8 @@ bool store_amf(const char *path, Model *model, const DynamicPrintConfig *config) | |||
|     mz_zip_archive archive; | ||||
|     mz_zip_zero_struct(&archive); | ||||
| 
 | ||||
|     mz_bool res = mz_zip_writer_init_file(&archive, export_path.c_str(), 0); | ||||
|     FILE *f = boost::nowide::fopen(export_path.c_str(), "wb"); | ||||
|     auto res = mz_bool(f != nullptr && mz_zip_writer_init_cfile(&archive, f, 0)); | ||||
|     if (res == 0) | ||||
|         return false; | ||||
| 
 | ||||
|  | @ -1018,19 +1039,20 @@ bool store_amf(const char *path, Model *model, const DynamicPrintConfig *config) | |||
| 
 | ||||
|     if (!mz_zip_writer_add_mem(&archive, internal_amf_filename.c_str(), (const void*)out.data(), out.length(), MZ_DEFAULT_COMPRESSION)) | ||||
|     { | ||||
|         mz_zip_writer_end(&archive); | ||||
|         close_archive_writer(&archive); | ||||
|         if (f) fclose(f); | ||||
|         boost::filesystem::remove(export_path); | ||||
|         return false; | ||||
|     } | ||||
| 
 | ||||
|     if (!mz_zip_writer_finalize_archive(&archive)) | ||||
|     { | ||||
|         mz_zip_writer_end(&archive); | ||||
|         close_archive_writer(&archive); | ||||
|         boost::filesystem::remove(export_path); | ||||
|         return false; | ||||
|     } | ||||
| 
 | ||||
|     mz_zip_writer_end(&archive); | ||||
|     close_archive_writer(&archive); | ||||
| 
 | ||||
|     return true; | ||||
| } | ||||
|  |  | |||
|  | @ -3,6 +3,7 @@ | |||
| 
 | ||||
| #include <boost/algorithm/string.hpp> | ||||
| #include <boost/nowide/convert.hpp> | ||||
| #include <boost/nowide/cstdio.hpp> | ||||
| 
 | ||||
| #include <miniz.h> | ||||
| 
 | ||||
|  | @ -298,7 +299,9 @@ bool load_prus(const char *path, Model *model) | |||
| { | ||||
|     mz_zip_archive archive; | ||||
|     mz_zip_zero_struct(&archive); | ||||
|     mz_bool res = mz_zip_reader_init_file(&archive, path, 0); | ||||
| 
 | ||||
|     FILE *f = boost::nowide::fopen(path, "rb"); | ||||
|     auto res = mz_bool(f != nullptr && mz_zip_reader_init_cfile(&archive, f, -1, 0)); | ||||
|     size_t  n_models_initial = model->objects.size(); | ||||
|     try { | ||||
|         if (res == MZ_FALSE) | ||||
|  | @ -327,10 +330,12 @@ bool load_prus(const char *path, Model *model) | |||
|         } | ||||
|     } catch (std::exception &ex) { | ||||
|         mz_zip_reader_end(&archive); | ||||
|         if(f) fclose(f); | ||||
|         throw ex; | ||||
|     } | ||||
| 
 | ||||
|     mz_zip_reader_end(&archive); | ||||
|     if(f) fclose(f); | ||||
|     return model->objects.size() > n_models_initial; | ||||
| } | ||||
| 
 | ||||
|  |  | |||
|  | @ -22,7 +22,6 @@ class Zipper::Impl { | |||
| public: | ||||
|     mz_zip_archive arch; | ||||
|     std::string m_zipname; | ||||
|     FILE *cfile = nullptr; | ||||
| 
 | ||||
|     static std::string get_errorstr(mz_zip_error mz_err) | ||||
|     { | ||||
|  | @ -125,15 +124,15 @@ Zipper::Zipper(const std::string &zipfname, e_compression compression) | |||
| 
 | ||||
|     memset(&m_impl->arch, 0, sizeof(m_impl->arch)); | ||||
| 
 | ||||
|     m_impl->cfile = boost::nowide::fopen(zipfname.c_str(), "wb"); | ||||
|     FILE *f = boost::nowide::fopen(zipfname.c_str(), "wb"); | ||||
| 
 | ||||
|     if (m_impl->cfile == nullptr) { | ||||
|     if (f == nullptr) { | ||||
|         m_impl->arch.m_last_error = MZ_ZIP_FILE_OPEN_FAILED; | ||||
|         m_impl->blow_up(); | ||||
|     } | ||||
| 
 | ||||
|     // Initialize the archive data
 | ||||
|     if(!mz_zip_writer_init_cfile(&m_impl->arch, m_impl->cfile, 0)) | ||||
|     if(!mz_zip_writer_init_cfile(&m_impl->arch, f, 0)) | ||||
|         m_impl->blow_up(); | ||||
| } | ||||
| 
 | ||||
|  | @ -149,11 +148,13 @@ Zipper::~Zipper() | |||
|             BOOST_LOG_TRIVIAL(error) << m_impl->formatted_errorstr(); | ||||
|     } | ||||
| 
 | ||||
|     FILE *f = mz_zip_get_cfile(&m_impl->arch); | ||||
| 
 | ||||
|     // The file should be closed no matter what...
 | ||||
|     if(!mz_zip_writer_end(&m_impl->arch)) | ||||
|         BOOST_LOG_TRIVIAL(error) << m_impl->formatted_errorstr(); | ||||
| 
 | ||||
|     if(m_impl->cfile) fclose(m_impl->cfile); | ||||
|     if(f != nullptr) fclose(f); | ||||
| } | ||||
| 
 | ||||
| Zipper::Zipper(Zipper &&m): | ||||
|  |  | |||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue
	
	 tamasmeszaros
						tamasmeszaros