unsigned int isIMYPackage(PG::STREAM::InByteFile& reader){ const unsigned int start = reader.pos(); const unsigned int number_of_files = reader.readUnsignedInt(); if(number_of_files > 9000) return 0; const unsigned int commpress_size = reader.readUnsignedInt(); const unsigned int first_offset = reader.readUnsignedInt(); if(first_offset > commpress_size) return 0; reader.seek(start+first_offset); if(isIMY(reader)){ return number_of_files*commpress_size; } return 0; }
bool StartDAT::open(const PG::UTIL::File& file, PercentIndicator* percent){ clear(); m_file = file; if(!m_file.exists() ){ PG_ERROR_STREAM("Target file '"<<file<<"' does not exist!"); m_file.clear(); return true; } readFileNames(); try{ PG::STREAM::InByteFile reader(m_file); if(!reader.isopen()) return true; const unsigned int file_size = reader.size(); const unsigned int number_of_files = reader.readUnsignedInt(); //test if this is a IMY file { const unsigned int v1 = reader.readUnsignedInt(); const unsigned int v2 = reader.readUnsignedInt(); if(v1 > v2){ m_isCompressed = true; }else{ m_isCompressed = false; } reader.seek(4); } const unsigned int header_size = (m_isCompressed)? 8+number_of_files*sizeof(int) : 4+number_of_files*sizeof(int); if(m_isCompressed) reader.skip(4); if(number_of_files > 9000){ PG_ERROR_STREAM("START.DAT is too big!"); return true; } //reader.skip(sizeof(int)); unsigned int lastOffset = reader.readUnsignedInt(); for(unsigned int i = 0; i < number_of_files; ++i){ fileInfo info; info.offset = lastOffset; if(i != number_of_files-1) lastOffset = reader.readUnsignedInt(); else lastOffset = file_size; if(lastOffset < info.offset){ PG_ERROR_STREAM("File offset order is wrong! ("<<lastOffset <<" < "<<info.offset<<") at: "<<reader.pos()); return true; } info.size = lastOffset-info.offset; if(info.offset < header_size){ PG_ERROR_STREAM("File offset is smaller then the header! ("<<info.offset <<" < "<<header_size<<")"); return true; } if((info.offset+info.size) > file_size){ PG_ERROR_STREAM("File is outside the range ("<< (info.offset+info.size) << " > " << file_size <<")!"); return true; } const unsigned int currentPos = reader.pos(); if( !m_isCompressed && i < m_namesTable.size() && !m_namesTable[i].empty() ){ info.name = m_namesTable[i]; reader.seek(info.offset); if(isTX2(reader)){ if(info.name.getFileExtension() != "TX2") info.name = info.name.getName()+".TX2"; info.fileType = fileInfo::TX2; }else if(info.name.getFileExtension() == "TX2") info.name = info.name.getName()+".NOT"; }else{ std::stringstream o; o<<"FILE"; o.fill('0'); o.width(4); o<<i<<"."; reader.seek(info.offset); if(isIMY(reader)){ o<<"IMY"; info.fileType = fileInfo::IMY; }else{ reader.seek(info.offset); if(isTX2(reader)){ o<<"TX2"; info.fileType = fileInfo::TX2; }else o<<"DAT"; } info.name = o.str(); } reader.seek(currentPos); m_fileInfos.push_back(info); } reader.close(); if(m_fileInfos.empty()){ PG_ERROR_STREAM("No files found inside!"); clear(); return true; } }catch (PG::UTIL::Exception& e) { PG_ERROR_STREAM("Couldn't read given PSPFS file '"<<file<<"': "<<e.what()); clear(); return true; }catch (...) { PG_ERROR_STREAM("Couldn't read given PSPFS file '"<<file<<"'!"); clear();; return true; } PG_INFO_STREAM("Opening done"); return false; }