Browse Source

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
RichardTea 6 years ago
parent
commit
9e04df810e
1 changed files with 22 additions and 11 deletions
  1. 22 11
      code/Common/ZipArchiveIOSystem.cpp

+ 22 - 11
code/Common/ZipArchiveIOSystem.cpp

@@ -45,7 +45,7 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 
 
 #include <assimp/ai_assert.h>
 #include <assimp/ai_assert.h>
 
 
-#include <unordered_map>
+#include <map>
 #include <memory>
 #include <memory>
 
 
 #ifdef ASSIMP_USE_HUNTER
 #ifdef ASSIMP_USE_HUNTER
@@ -55,7 +55,6 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 #endif
 #endif
 
 
 namespace Assimp {
 namespace Assimp {
-
     // ----------------------------------------------------------------
     // ----------------------------------------------------------------
     // Wraps an existing Assimp::IOSystem for unzip
     // Wraps an existing Assimp::IOSystem for unzip
     class IOSystem2Unzip {
     class IOSystem2Unzip {
@@ -247,14 +246,25 @@ namespace Assimp {
     }
     }
 
 
     size_t ZipFile::Read(void* pvBuffer, size_t pSize, size_t pCount) {
     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 {
     size_t ZipFile::FileSize() const {
@@ -312,9 +322,10 @@ namespace Assimp {
         void MapArchive();
         void MapArchive();
 
 
     private:
     private:
+        typedef std::map<std::string, ZipFileInfo> ZipFileInfoMap;
+
         unzFile m_ZipFileHandle = nullptr;
         unzFile m_ZipFileHandle = nullptr;
-        typedef std::unordered_map<std::string, ZipFileInfo> ZipFileMap;
-        ZipFileMap m_ArchiveMap;
+        ZipFileInfoMap m_ArchiveMap;
     };
     };
 
 
     ZipArchiveIOSystem::Implement::Implement(IOSystem* pIOHandler, const char* pFilename, const char* pMode) {
     ZipArchiveIOSystem::Implement::Implement(IOSystem* pIOHandler, const char* pFilename, const char* pMode) {
@@ -388,7 +399,7 @@ namespace Assimp {
     bool ZipArchiveIOSystem::Implement::Exists(std::string& filename) {
     bool ZipArchiveIOSystem::Implement::Exists(std::string& filename) {
         MapArchive();
         MapArchive();
 
 
-        ZipFileMap::const_iterator it = m_ArchiveMap.find(filename);
+        ZipFileInfoMap::const_iterator it = m_ArchiveMap.find(filename);
         return (it != m_ArchiveMap.end());
         return (it != m_ArchiveMap.end());
     }
     }
 
 
@@ -398,7 +409,7 @@ namespace Assimp {
         SimplifyFilename(filename);
         SimplifyFilename(filename);
 
 
         // Find in the map
         // 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())
         if (zip_it == m_ArchiveMap.cend())
             return nullptr;
             return nullptr;