From 35235772357dc7fefafcee2a3c52ee365c4f73f1 Mon Sep 17 00:00:00 2001 From: Robert Osfield Date: Wed, 10 Nov 2004 21:18:31 +0000 Subject: [PATCH] Added check for endianess of .osga archives --- include/osgDB/Archive | 2 +- src/osgDB/Archive.cpp | 44 +++++++++++++++++++++++++------------- src/osgTerrain/DataSet.cpp | 5 +---- 3 files changed, 31 insertions(+), 20 deletions(-) diff --git a/include/osgDB/Archive b/include/osgDB/Archive index 8c1ef8436..cff8c69d6 100644 --- a/include/osgDB/Archive +++ b/include/osgDB/Archive @@ -102,7 +102,7 @@ class OSGDB_EXPORT Archive : public ReaderWriter inline pos_type getPositionNextIndexBlock() const { return _filePositionNextIndexBlock; } - static IndexBlock* read(std::istream& in); + static IndexBlock* read(std::istream& in, bool doEndianSwap); std::string getFirstFileName() const; diff --git a/src/osgDB/Archive.cpp b/src/osgDB/Archive.cpp index 9f74b3a49..bab32777f 100644 --- a/src/osgDB/Archive.cpp +++ b/src/osgDB/Archive.cpp @@ -12,6 +12,7 @@ */ #include +#include #include #include @@ -22,6 +23,7 @@ using namespace osgDB; float Archive::s_currentSupportedVersion = 0.0; +const unsigned int ENDIAN_TEST_NUMBER = 0x00000001; osgDB::Archive* osgDB::openArchive(const std::string& filename, Archive::ArchiveStatus status, unsigned int indexBlockSizeHint) { @@ -66,7 +68,7 @@ void Archive::IndexBlock::allocateData(unsigned int blockSize) } } -Archive::IndexBlock* Archive::IndexBlock::read(std::istream& in) +Archive::IndexBlock* Archive::IndexBlock::read(std::istream& in, bool doEndianSwap) { if (!in) return 0; @@ -75,6 +77,14 @@ Archive::IndexBlock* Archive::IndexBlock::read(std::istream& in) in.read(reinterpret_cast(&indexBlock->_blockSize), sizeof(indexBlock->_blockSize)); in.read(reinterpret_cast(&indexBlock->_filePositionNextIndexBlock), sizeof(indexBlock->_filePositionNextIndexBlock)); in.read(reinterpret_cast(&indexBlock->_offsetOfNextAvailableSpace), sizeof(indexBlock-> _offsetOfNextAvailableSpace)); + + if (doEndianSwap) + { + osg::swapBytes(reinterpret_cast(&indexBlock->_blockSize), sizeof(indexBlock->_blockSize)); + osg::swapBytes(reinterpret_cast(&indexBlock->_blockSize), sizeof(indexBlock->_blockSize)); + osg::swapBytes(reinterpret_cast(&indexBlock->_filePositionNextIndexBlock), sizeof(indexBlock->_filePositionNextIndexBlock)); + osg::swapBytes(reinterpret_cast(&indexBlock->_offsetOfNextAvailableSpace), sizeof(indexBlock-> _offsetOfNextAvailableSpace)); + } indexBlock->allocateData(indexBlock->_blockSize); if (indexBlock->_data) @@ -217,6 +227,7 @@ Archive::~Archive() close(); } + bool Archive::open(const std::string& filename, ArchiveStatus status, unsigned int indexBlockSize) { if (status==READ) @@ -230,15 +241,24 @@ bool Archive::open(const std::string& filename, ArchiveStatus status, unsigned i char identifier[4]; _input.read(identifier,4); + bool validArchive = (identifier[0]=='o' && identifier[1]=='s' && identifier[2]=='g' && identifier[3]=='a'); - if (validArchive) { + + unsigned int endianTestWord; + _input.read(reinterpret_cast(&endianTestWord),4); + bool doEndianSwap = (endianTestWord!=ENDIAN_TEST_NUMBER); + _input.read(reinterpret_cast(&_version),sizeof(_version)); + if (doEndianSwap) + { + osg::swapBytes(reinterpret_cast(&_version),sizeof(_version)); + } IndexBlock *indexBlock = 0; - while ( (indexBlock=Archive::IndexBlock::read(_input)) != 0) + while ( (indexBlock=Archive::IndexBlock::read(_input, doEndianSwap)) != 0) { _indexBlockList.push_back(indexBlock); if (indexBlock->getPositionNextIndexBlock()==pos_type(0)) break; @@ -280,7 +300,7 @@ bool Archive::open(const std::string& filename, ArchiveStatus status, unsigned i _input.close(); return false; } - else if (status==WRITE) + else { if (status==WRITE && open(filename,READ)) { @@ -288,14 +308,11 @@ bool Archive::open(const std::string& filename, ArchiveStatus status, unsigned i _status = WRITE; _output.open(filename.c_str(), std::ios_base::binary | std::ios_base::in | std::ios_base::out); - - osg::notify(osg::INFO)<<"File position after open = "<<(int)_output.tellp()<<" is_open "<<_output.is_open()<(&ENDIAN_TEST_NUMBER),4); _output.write(reinterpret_cast(&s_currentSupportedVersion),sizeof(float)); IndexBlock *indexBlock = new IndexBlock(indexBlockSize); @@ -330,11 +348,6 @@ bool Archive::open(const std::string& filename, ArchiveStatus status, unsigned i } } - else - { - osg::notify(osg::NOTICE)<<"Archive::open("<getReaderWriterForExtension(getLowerCaseFileExtension(writeFunctor._filename)); if (!rw) { - osg::notify(osg::NOTICE)<<"Archive::writeObject(obj, "<tile->_imagery_maxSourceResolutionY) needToDivideY = true; } - osg::notify(osg::NOTICE)<<"texture_dx="<_imagery_maxSourceResolutionX<