From 9e04df810e75630bb43de6868111e547b1536d6a Mon Sep 17 00:00:00 2001 From: RichardTea <31507749+RichardTea@users.noreply.github.com> Date: Thu, 25 Jul 2019 09:53:18 +0100 Subject: [PATCH] ZipArchiveIOSystem should be sorted, fix ZipFile::Read() Q3BSP relies on the sort order Read() should return number of elements read, not count of bytes Read() should clip to the file size and return elements actually read, instead of aborting if try to read too much --- code/Common/ZipArchiveIOSystem.cpp | 33 ++++++++++++++++++++---------- 1 file changed, 22 insertions(+), 11 deletions(-) diff --git a/code/Common/ZipArchiveIOSystem.cpp b/code/Common/ZipArchiveIOSystem.cpp index 463c2071b..43c66a921 100644 --- a/code/Common/ZipArchiveIOSystem.cpp +++ b/code/Common/ZipArchiveIOSystem.cpp @@ -45,7 +45,7 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. #include -#include +#include #include #ifdef ASSIMP_USE_HUNTER @@ -55,7 +55,6 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. #endif namespace Assimp { - // ---------------------------------------------------------------- // Wraps an existing Assimp::IOSystem for unzip class IOSystem2Unzip { @@ -247,14 +246,25 @@ namespace Assimp { } size_t ZipFile::Read(void* pvBuffer, size_t pSize, size_t pCount) { - const size_t size = pSize * pCount; - ai_assert((size + m_SeekPtr) <= m_Size); + // Should be impossible + ai_assert(m_Buffer != nullptr); + ai_assert(NULL != pvBuffer && 0 != pSize && 0 != pCount); - std::memcpy(pvBuffer, m_Buffer.get() + m_SeekPtr, size); + // Clip down to file size + size_t byteSize = pSize * pCount; + if ((byteSize + m_SeekPtr) > m_Size) + { + pCount = (m_Size - m_SeekPtr) / pSize; + byteSize = pSize * pCount; + if (byteSize == 0) + return 0; + } - m_SeekPtr += size; + std::memcpy(pvBuffer, m_Buffer.get() + m_SeekPtr, byteSize); - return size; + m_SeekPtr += byteSize; + + return pCount; } size_t ZipFile::FileSize() const { @@ -312,9 +322,10 @@ namespace Assimp { void MapArchive(); private: + typedef std::map ZipFileInfoMap; + unzFile m_ZipFileHandle = nullptr; - typedef std::unordered_map ZipFileMap; - ZipFileMap m_ArchiveMap; + ZipFileInfoMap m_ArchiveMap; }; ZipArchiveIOSystem::Implement::Implement(IOSystem* pIOHandler, const char* pFilename, const char* pMode) { @@ -388,7 +399,7 @@ namespace Assimp { bool ZipArchiveIOSystem::Implement::Exists(std::string& filename) { MapArchive(); - ZipFileMap::const_iterator it = m_ArchiveMap.find(filename); + ZipFileInfoMap::const_iterator it = m_ArchiveMap.find(filename); return (it != m_ArchiveMap.end()); } @@ -398,7 +409,7 @@ namespace Assimp { SimplifyFilename(filename); // Find in the map - ZipFileMap::const_iterator zip_it = m_ArchiveMap.find(filename); + ZipFileInfoMap::const_iterator zip_it = m_ArchiveMap.find(filename); if (zip_it == m_ArchiveMap.cend()) return nullptr;