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
pull/2545/head
RichardTea 2019-07-25 09:53:18 +01:00
parent c6620478ec
commit 9e04df810e
1 changed files with 22 additions and 11 deletions

View File

@ -45,7 +45,7 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#include <assimp/ai_assert.h>
#include <unordered_map>
#include <map>
#include <memory>
#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<std::string, ZipFileInfo> ZipFileInfoMap;
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) {
@ -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;