From c718500c55c9ae96e4338a7b9e900ba9a88b9ab1 Mon Sep 17 00:00:00 2001 From: Kim Kulling Date: Mon, 14 Feb 2022 20:25:18 +0100 Subject: [PATCH] Fix last review findings and finish windows bits --- code/AssetLib/Blender/BlenderLoader.cpp | 2 +- code/AssetLib/FBX/FBXParser.cpp | 26 +----------------- code/AssetLib/X/XFileParser.cpp | 6 ++--- code/AssetLib/XGL/XGLLoader.cpp | 12 ++++----- code/Common/Compression.cpp | 18 +++++++++---- code/Common/Compression.h | 35 +++++++++++++++++-------- 6 files changed, 48 insertions(+), 51 deletions(-) diff --git a/code/AssetLib/Blender/BlenderLoader.cpp b/code/AssetLib/Blender/BlenderLoader.cpp index c67879be5..b3591b4f1 100644 --- a/code/AssetLib/Blender/BlenderLoader.cpp +++ b/code/AssetLib/Blender/BlenderLoader.cpp @@ -175,7 +175,7 @@ void BlenderImporter::InternReadFile(const std::string &pFile, size_t total = 0; Compression compression; - if (compression.open(Compression::Format::Binary, Compression::FlushMode::NoFlush, 16 + MAX_WBITS)) { + if (compression.open(Compression::Format::Binary, Compression::FlushMode::NoFlush, 16 + Compression::MaxWBits)) { total = compression.decompress((unsigned char *)reader->GetPtr(), reader->GetRemainingSize(), uncompressed); compression.close(); } diff --git a/code/AssetLib/FBX/FBXParser.cpp b/code/AssetLib/FBX/FBXParser.cpp index c453647e0..e20377a3c 100644 --- a/code/AssetLib/FBX/FBXParser.cpp +++ b/code/AssetLib/FBX/FBXParser.cpp @@ -567,34 +567,10 @@ void ReadBinaryDataArray(char type, uint32_t count, const char*& data, const cha // zlib/deflate, next comes ZIP head (0x78 0x01) // see http://www.ietf.org/rfc/rfc1950.txt Compression compress; - if (compress.open(Compression::Format::Binary, Compression::FlushMode::Finish,0)) { + if (compress.open(Compression::Format::Binary, Compression::FlushMode::Finish, 0)) { compress.decompress(data, comp_len, buff); compress.close(); } - /* z_stream zstream = {}; - zstream.opaque = Z_NULL; - zstream.zalloc = Z_NULL; - zstream.zfree = Z_NULL; - zstream.data_type = Z_BINARY; - - // http://hewgill.com/journal/entries/349-how-to-decompress-gzip-stream-with-zlib - if(Z_OK != inflateInit(&zstream)) { - ParseError("failure initializing zlib"); - } - - zstream.next_in = reinterpret_cast(const_cast(data)); - zstream.avail_in = comp_len; - - zstream.avail_out = static_cast(buff.size()); - zstream.next_out = reinterpret_cast(&*buff.begin()); - const int ret = inflate(&zstream, Z_FINISH); - - if (ret != Z_STREAM_END && ret != Z_OK) { - ParseError("failure decompressing compressed data section"); - } - - // terminate zlib - inflateEnd(&zstream);*/ } #ifdef ASSIMP_BUILD_DEBUG else { diff --git a/code/AssetLib/X/XFileParser.cpp b/code/AssetLib/X/XFileParser.cpp index 90a257dfa..65f4023a7 100644 --- a/code/AssetLib/X/XFileParser.cpp +++ b/code/AssetLib/X/XFileParser.cpp @@ -117,13 +117,13 @@ XFileParser::XFileParser(const std::vector &pBuffer) : mIsBinaryFormat = true; compressed = true; } else - ThrowException("Unsupported xfile format '", mP[8], mP[9], mP[10], mP[11], "'"); + ThrowException("Unsupported x-file format '", mP[8], mP[9], mP[10], mP[11], "'"); // float size mBinaryFloatSize = (unsigned int)(mP[12] - 48) * 1000 + (unsigned int)(mP[13] - 48) * 100 + (unsigned int)(mP[14] - 48) * 10 + (unsigned int)(mP[15] - 48); if (mBinaryFloatSize != 32 && mBinaryFloatSize != 64) - ThrowException("Unknown float size ", mBinaryFloatSize, " specified in xfile header."); + ThrowException("Unknown float size ", mBinaryFloatSize, " specified in x-file header."); // The x format specifies size in bits, but we work in bytes mBinaryFloatSize /= 8; @@ -188,7 +188,7 @@ XFileParser::XFileParser(const std::vector &pBuffer) : Compression compression; uncompressed.resize(est_out + 1); char *out = &uncompressed.front(); - if (compression.open(mIsBinaryFormat ? Compression::Format::Binary : Compression::Format::ASCII, Compression::FlushMode::SyncFlush, -MAX_WBITS)) { + if (compression.open(mIsBinaryFormat ? Compression::Format::Binary : Compression::Format::ASCII, Compression::FlushMode::SyncFlush, -Compression::MAX_WBITS)) { while (mP + 3 < mEnd) { uint16_t ofs = *((uint16_t *)mP); AI_SWAP2(ofs); diff --git a/code/AssetLib/XGL/XGLLoader.cpp b/code/AssetLib/XGL/XGLLoader.cpp index 4fbee6eea..154af9854 100644 --- a/code/AssetLib/XGL/XGLLoader.cpp +++ b/code/AssetLib/XGL/XGLLoader.cpp @@ -53,8 +53,8 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. #include #include #include -#include -#include +//#include +//#include using namespace Assimp; @@ -132,14 +132,14 @@ void XGLImporter::InternReadFile(const std::string &pFile, aiScene *pScene, IOSy Compression compression; size_t total = 0l; - if (compression.open(Compression::Format::Binary, Compression::FlushMode::NoFlush, -MAX_WBITS)) { + if (compression.open(Compression::Format::Binary, Compression::FlushMode::NoFlush, -Compression::MaxWBits)) { // skip two extra bytes, zgl files do carry a crc16 upfront (I think) raw_reader->IncPtr(2); total = compression.decompress((unsigned char *)raw_reader->GetPtr(), raw_reader->GetRemainingSize(), uncompressed); compression.close(); } // replace the input stream with a memory stream - stream.reset(new MemoryIOStream(reinterpret_cast(uncompressed.data()), total)); + stream.reset(new MemoryIOStream(reinterpret_cast(uncompressed.data()), total)); #endif } @@ -200,7 +200,7 @@ void XGLImporter::ReadWorld(XmlNode &node, TempScope &scope) { if (!nd) { ThrowException("failure reading "); } - if (!nd->mName.length) { + if (nd->mName.length == 0) { nd->mName.Set("WORLD"); } @@ -784,4 +784,4 @@ aiColor3D XGLImporter::ReadCol3(XmlNode &node) { return aiColor3D(v.x, v.y, v.z); } -#endif +#endif // ASSIMP_BUILD_NO_XGL_IMPORTER diff --git a/code/Common/Compression.cpp b/code/Common/Compression.cpp index 86f1e9ec0..1249746b4 100644 --- a/code/Common/Compression.cpp +++ b/code/Common/Compression.cpp @@ -43,15 +43,16 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. #include #include - namespace Assimp { struct Compression::impl { bool mOpen; z_stream mZSstream; FlushMode mFlushMode; + impl() : mOpen(false), + mZSstream(), mFlushMode(Compression::FlushMode::NoFlush) { // empty } @@ -97,8 +98,6 @@ bool Compression::open(Format format, FlushMode flush, int windowBits) { return mImpl->mOpen; } -constexpr size_t MYBLOCK = 32786; - static int getFlushMode(Compression::FlushMode flush) { int z_flush = 0; switch (flush) { @@ -119,15 +118,19 @@ static int getFlushMode(Compression::FlushMode flush) { return z_flush; } +constexpr size_t MYBLOCK = 32786; + size_t Compression::decompress(const void *data, size_t in, std::vector &uncompressed) { ai_assert(mImpl != nullptr); + if (data == nullptr || in == 0) { + return 0l; + } mImpl->mZSstream.next_in = (Bytef*)(data); mImpl->mZSstream.avail_in = (uInt)in; int ret = 0; size_t total = 0l; - const int flushMode = getFlushMode(mImpl->mFlushMode); if (flushMode == Z_FINISH) { mImpl->mZSstream.avail_out = static_cast(uncompressed.size()); @@ -160,6 +163,10 @@ size_t Compression::decompress(const void *data, size_t in, std::vector &u } size_t Compression::decompressBlock(const void *data, size_t in, char *out, size_t availableOut) { + ai_assert(mImpl != nullptr); + if (data == nullptr || in == 0 || out == nullptr || availableOut == 0) { + return 0l; + } // push data to the stream mImpl->mZSstream.next_in = (Bytef *)data; @@ -169,8 +176,9 @@ size_t Compression::decompressBlock(const void *data, size_t in, char *out, size // and decompress the data .... int ret = ::inflate(&mImpl->mZSstream, Z_SYNC_FLUSH); - if (ret != Z_OK && ret != Z_STREAM_END) + if (ret != Z_OK && ret != Z_STREAM_END) { throw DeadlyImportError("X: Failed to decompress MSZIP-compressed data"); + } ::inflateReset(&mImpl->mZSstream); ::inflateSetDictionary(&mImpl->mZSstream, (const Bytef *)out, (uInt)availableOut - mImpl->mZSstream.avail_out); diff --git a/code/Common/Compression.h b/code/Common/Compression.h index 05beed7f7..fa0fc9e40 100644 --- a/code/Common/Compression.h +++ b/code/Common/Compression.h @@ -54,21 +54,25 @@ namespace Assimp { /// @brief This class provides the decompression of zlib-compressed data. class Compression { public: - enum class Format { - InvalidFormat = -1, - Binary = 0, - ASCII, + static const int MaxWBits = MAX_WBITS; - NumFormats + /// @brief Describes the format data type + enum class Format { + InvalidFormat = -1, ///< Invalid enum type. + Binary = 0, ///< Binary format. + ASCII, ///< ASCII format. + + NumFormats ///< The number of supported formats. }; + /// @brief The supported flush mode, used for blocked access. enum class FlushMode { - InvalidFormat = -1, - NoFlush = 0, - SyncFlush, - Finish, + InvalidFormat = -1, ///< Invalid enum type. + NoFlush = 0, ///< No flush, will be done on inflate end. + SyncFlush, ///< Synced flush mode. + Finish, ///< Finish mode, all in once, no block access. - NumModes + NumModes ///< The number of supported modes. }; /// @brief The class constructor. @@ -78,6 +82,9 @@ public: ~Compression(); /// @brief Will open the access to the compression. + /// @param[in] format The format type + /// @param[in] flush The flush mode. + /// @param[in] windowBits The windows history working size, shall be between 8 and 15. /// @return true if close was successful, false if not. bool open(Format format, FlushMode flush, int windowBits); @@ -89,12 +96,18 @@ public: /// @return true if close was successful, false if not. bool close(); - /// @brief Will decompress the data buffer. + /// @brief Will decompress the data buffer in one step. /// @param[in] data The data to decompress /// @param[in] in The size of the data. /// @param[out uncompressed A std::vector containing the decompressed data. size_t decompress(const void *data, size_t in, std::vector &uncompressed); + /// @brief Will decompress the data buffer block-wise. + /// @param[in] data The compressed data + /// @param[in] in The size of the data buffer + /// @param[out] out The output buffer + /// @param[out] availableOut The upper limit of the output buffer. + /// @return The size of the decompressed data buffer. size_t decompressBlock(const void *data, size_t in, char *out, size_t availableOut); private: