CIMGArchiveTranslatorHandle* imgExtension::OpenArchive( CFileTranslator *srcRoot, const char *srcPath ) { CIMGArchiveTranslatorHandle *transOut = NULL; bool hasValidArchive = false; eIMGArchiveVersion theVersion; CFile *contentFile = srcRoot->Open( srcPath, "rb+" ); if ( !contentFile ) { return NULL; } bool hasUniqueRegistryFile = false; CFile *registryFile = NULL; // Check for version 2. struct mainHeader { union { unsigned char version[4]; unsigned int checksum; }; }; mainHeader imgHeader; bool hasReadMainHeader = contentFile->ReadStruct( imgHeader ); if ( hasReadMainHeader && imgHeader.checksum == '2REV' ) { hasValidArchive = true; theVersion = IMG_VERSION_2; registryFile = contentFile; } if ( !hasValidArchive ) { // Check for version 1. std::string dirOfArchive; std::string extOrig; std::string nameItem = FileSystem::GetFileNameItem( srcPath, false, &dirOfArchive, &extOrig ); if ( nameItem.size() != 0 ) { hasUniqueRegistryFile = true; // Try to open the registry file. std::string regFilePath = dirOfArchive + nameItem + ".DIR"; registryFile = srcRoot->Open( regFilePath.c_str(), "rb+" ); if ( registryFile ) { hasValidArchive = true; theVersion = IMG_VERSION_1; } } } if ( hasValidArchive ) { CIMGArchiveTranslator *translator = new CIMGArchiveTranslator( *this, contentFile, registryFile, theVersion ); if ( translator ) { bool loadingSuccess = translator->ReadArchive(); if ( loadingSuccess ) { transOut = translator; } else { delete translator; contentFile = NULL; registryFile = NULL; } } } if ( !transOut ) { if ( contentFile ) { delete contentFile; contentFile = NULL; } if ( hasUniqueRegistryFile && registryFile ) { delete registryFile; registryFile = NULL; } } return transOut; }
static inline CIMGArchiveTranslatorHandle* GenOpenArchiveTemplate( imgExtension *env, CFileTranslator *srcRoot, const charType *srcPath, constructionHandler constr ) { CIMGArchiveTranslatorHandle *transOut = NULL; bool hasValidArchive = false; eIMGArchiveVersion theVersion; CFile *contentFile = srcRoot->Open( srcPath, GetReadWriteMode <charType> ( false ), FILE_FLAG_WRITESHARE ); if ( !contentFile ) { return NULL; } bool hasUniqueRegistryFile = false; CFile *registryFile = NULL; // Check for version 2. struct mainHeader { union { unsigned char version[4]; fsUInt_t checksum; }; }; mainHeader imgHeader; bool hasReadMainHeader = contentFile->ReadStruct( imgHeader ); if ( hasReadMainHeader && imgHeader.checksum == '2REV' ) { hasValidArchive = true; theVersion = IMG_VERSION_2; registryFile = contentFile; } if ( !hasValidArchive ) { // Check for version 1. hasUniqueRegistryFile = true; registryFile = OpenSeperateIMGRegistryFile( srcRoot, srcPath, false ); if ( registryFile ) { hasValidArchive = true; theVersion = IMG_VERSION_1; } } if ( hasValidArchive ) { CIMGArchiveTranslator *translator = constr( env, registryFile, contentFile, theVersion ); if ( translator ) { bool loadingSuccess = translator->ReadArchive(); if ( loadingSuccess ) { transOut = translator; } else { delete translator; contentFile = NULL; registryFile = NULL; } } } if ( !transOut ) { if ( contentFile ) { delete contentFile; contentFile = NULL; } if ( hasUniqueRegistryFile && registryFile ) { delete registryFile; registryFile = NULL; } } return transOut; }