bool CAICHHashSet::LoadHashSet() { if (m_eStatus != AICH_HASHSETCOMPLETE) { wxFAIL; return false; } if ( !m_pHashTree.m_bHashValid || m_pHashTree.m_nDataSize != m_pOwner->GetFileSize() || m_pHashTree.m_nDataSize == 0) { wxFAIL; return false; } wxString fullpath = theApp->ConfigDir + KNOWN2_MET_FILENAME; CFile file(fullpath, CFile::read); if (!file.IsOpened()) { if (wxFileExists(fullpath)) { wxString strError(wxT("Failed to load ") KNOWN2_MET_FILENAME wxT(" file")); AddDebugLogLineM( true, logSHAHashSet, strError); } return false; } try { uint8 header = file.ReadUInt8(); if (header != KNOWN2_MET_VERSION) { AddDebugLogLineM( true, logSHAHashSet, wxT("Loading failed: Current file is not a met-file!")); return false; } CAICHHash CurrentHash; uint64 nExistingSize = file.GetLength(); uint32 nHashCount; while (file.GetPosition() < nExistingSize) { CurrentHash.Read(&file); if (m_pHashTree.m_Hash == CurrentHash) { // found Hashset uint32 nExpectedCount = (PARTSIZE/EMBLOCKSIZE + ((PARTSIZE % EMBLOCKSIZE != 0)? 1 : 0)) * (m_pHashTree.m_nDataSize/PARTSIZE); if (m_pHashTree.m_nDataSize % PARTSIZE != 0) { nExpectedCount += (m_pHashTree.m_nDataSize % PARTSIZE)/EMBLOCKSIZE + (((m_pHashTree.m_nDataSize % PARTSIZE) % EMBLOCKSIZE != 0)? 1 : 0); } nHashCount = file.ReadUInt32(); if (nHashCount != nExpectedCount) { AddDebugLogLineM( true, logSHAHashSet, wxT("Failed to load HashSet: Available Hashs and expected hashcount differ!")); return false; } if (!m_pHashTree.LoadLowestLevelHashs(&file)) { AddDebugLogLineM( true, logSHAHashSet, wxT("Failed to load HashSet: LoadLowestLevelHashs failed!")); return false; } if (!ReCalculateHash(false)) { AddDebugLogLineM( true, logSHAHashSet, wxT("Failed to load HashSet: Calculating loaded hashs failed!")); return false; } if (CurrentHash != m_pHashTree.m_Hash) { AddDebugLogLineM( true, logSHAHashSet, wxT("Failed to load HashSet: Calculated Masterhash differs from given Masterhash - hashset corrupt!")); return false; } return true; } nHashCount = file.ReadUInt32(); if (file.GetPosition() + nHashCount*HASHSIZE > nExistingSize) { AddDebugLogLineM( true, logSHAHashSet, wxT("Saving failed: File contains fewer entries than specified!")); return false; } // skip the rest of this hashset file.Seek(nHashCount*HASHSIZE, wxFromCurrent); } AddDebugLogLineM( true, logSHAHashSet, wxT("Failed to load HashSet: HashSet not found!")); } catch (const CSafeIOException& e) { AddDebugLogLineM(true, logSHAHashSet, wxT("IO error while loading AICH HashSet: ") + e.what()); } return false; }
bool CAICHRecoveryHashSet::LoadHashSet(){ if (m_eStatus != AICH_HASHSETCOMPLETE){ ASSERT( false ); return false; } if ( !m_pHashTree.m_bHashValid || m_pHashTree.m_nDataSize != m_pOwner->GetFileSize() || m_pHashTree.m_nDataSize == 0){ ASSERT( false ); return false; } CString fullpath = thePrefs.GetMuleDirectory(EMULE_CONFIGDIR); fullpath.Append(KNOWN2_MET_FILENAME); CSafeFile file; CFileException fexp; if (!file.Open(fullpath,CFile::modeCreate|CFile::modeRead|CFile::modeNoTruncate|CFile::osSequentialScan|CFile::typeBinary|CFile::shareDenyNone, &fexp)){ if (fexp.m_cause != CFileException::fileNotFound){ CString strError(_T("Failed to load ") KNOWN2_MET_FILENAME _T(" file")); TCHAR szError[MAX_CFEXP_ERRORMSG]; if (fexp.GetErrorMessage(szError, ARRSIZE(szError))){ strError += _T(" - "); strError += szError; } theApp.QueueLogLine(true, _T("%s"), strError); } return false; } try { //setvbuf(file.m_pStream, NULL, _IOFBF, 16384); uint8 header = file.ReadUInt8(); if (header != KNOWN2_MET_VERSION){ AfxThrowFileException(CFileException::endOfFile, 0, file.GetFileName()); } CAICHHash CurrentHash; uint32 nExistingSize = (UINT)file.GetLength(); uint32 nHashCount; while (file.GetPosition() < nExistingSize){ CurrentHash.Read(&file); if (m_pHashTree.m_Hash == CurrentHash){ // found Hashset uint32 nExpectedCount = (uint32)((PARTSIZE/EMBLOCKSIZE + ((PARTSIZE % EMBLOCKSIZE != 0)? 1 : 0)) * (m_pHashTree.m_nDataSize/PARTSIZE)); if (m_pHashTree.m_nDataSize % PARTSIZE != 0) nExpectedCount += (uint32)((m_pHashTree.m_nDataSize % PARTSIZE)/EMBLOCKSIZE + (((m_pHashTree.m_nDataSize % PARTSIZE) % EMBLOCKSIZE != 0)? 1 : 0)); nHashCount = file.ReadUInt32(); if (nHashCount != nExpectedCount){ theApp.QueueDebugLogLine(true, _T("Failed to load HashSet: Available Hashs and expected hashcount differ!")); return false; } //uint32 dbgPos = file.GetPosition(); if (!m_pHashTree.LoadLowestLevelHashs(&file)){ theApp.QueueDebugLogLine(true, _T("Failed to load HashSet: LoadLowestLevelHashs failed!")); return false; } //uint32 dbgHashRead = (file.GetPosition()-dbgPos)/HASHSIZE; if (!ReCalculateHash(false)){ theApp.QueueDebugLogLine(true, _T("Failed to load HashSet: Calculating loaded hashs failed!")); return false; } if (CurrentHash != m_pHashTree.m_Hash){ theApp.QueueDebugLogLine(true, _T("Failed to load HashSet: Calculated Masterhash differs from given Masterhash - hashset corrupt!")); return false; } return true; } nHashCount = file.ReadUInt32(); if (file.GetPosition() + nHashCount*HASHSIZE > nExistingSize){ AfxThrowFileException(CFileException::endOfFile, 0, file.GetFileName()); } // skip the rest of this hashset file.Seek(nHashCount*HASHSIZE, CFile::current); } theApp.QueueDebugLogLine(true, _T("Failed to load HashSet: HashSet not found!")); } catch(CFileException* error){ if (error->m_cause == CFileException::endOfFile) theApp.QueueLogLine(true, GetResString(IDS_ERR_MET_BAD), KNOWN2_MET_FILENAME); else{ TCHAR buffer[MAX_CFEXP_ERRORMSG]; error->GetErrorMessage(buffer, ARRSIZE(buffer)); theApp.QueueLogLine(true,GetResString(IDS_ERR_SERVERMET_UNKNOWN),buffer); } error->Delete(); } return false; }
// this function is only allowed to be called right after successfully calculating the hashset (!) // will delete the hashset, after saving to free the memory bool CAICHHashSet::SaveHashSet() { if (m_eStatus != AICH_HASHSETCOMPLETE) { wxFAIL; return false; } if ( !m_pHashTree.m_bHashValid || m_pHashTree.m_nDataSize != m_pOwner->GetFileSize()) { wxFAIL; return false; } try { const wxString fullpath = theApp->ConfigDir + KNOWN2_MET_FILENAME; const bool exists = wxFile::Exists(fullpath); CFile file(fullpath, exists ? CFile::read_write : CFile::write); if (!file.IsOpened()) { AddDebugLogLineM( true, logSHAHashSet, wxT("Failed to save HashSet: opening met file failed!")); return false; } uint64 nExistingSize = file.GetLength(); if (nExistingSize) { uint8 header = file.ReadUInt8(); if (header != KNOWN2_MET_VERSION) { AddDebugLogLineM( true, logSHAHashSet, wxT("Saving failed: Current file is not a met-file!")); return false; } AddDebugLogLineM( false, logSHAHashSet, wxString::Format(wxT("Met file is version 0x%2.2x."),header)); } else { file.WriteUInt8(KNOWN2_MET_VERSION); // Update the recorded size, in order for the sanity check below to work. nExistingSize += 1; } // first we check if the hashset we want to write is already stored CAICHHash CurrentHash; while (file.GetPosition() < nExistingSize) { CurrentHash.Read(&file); if (m_pHashTree.m_Hash == CurrentHash) { // this hashset if already available, no need to save it again return true; } uint32 nHashCount = file.ReadUInt32(); if (file.GetPosition() + nHashCount*HASHSIZE > nExistingSize) { AddDebugLogLineM( true, logSHAHashSet, wxT("Saving failed: File contains fewer entries than specified!")); return false; } // skip the rest of this hashset file.Seek(nHashCount*HASHSIZE, wxFromCurrent); } // write hashset m_pHashTree.m_Hash.Write(&file); uint32 nHashCount = (PARTSIZE/EMBLOCKSIZE + ((PARTSIZE % EMBLOCKSIZE != 0)? 1 : 0)) * (m_pHashTree.m_nDataSize/PARTSIZE); if (m_pHashTree.m_nDataSize % PARTSIZE != 0) { nHashCount += (m_pHashTree.m_nDataSize % PARTSIZE)/EMBLOCKSIZE + (((m_pHashTree.m_nDataSize % PARTSIZE) % EMBLOCKSIZE != 0)? 1 : 0); } file.WriteUInt32(nHashCount); if (!m_pHashTree.WriteLowestLevelHashs(&file, 0, true, true)) { // thats bad... really file.SetLength(nExistingSize); AddDebugLogLineM( true, logSHAHashSet, wxT("Failed to save HashSet: WriteLowestLevelHashs() failed!")); return false; } if (file.GetLength() != nExistingSize + (nHashCount+1)*HASHSIZE + 4) { // thats even worse file.SetLength(nExistingSize); AddDebugLogLineM( true, logSHAHashSet, wxT("Failed to save HashSet: Calculated and real size of hashset differ!")); return false; } AddDebugLogLineM( false, logSHAHashSet, wxString::Format(wxT("Sucessfully saved eMuleAC Hashset, %u Hashs + 1 Masterhash written"), nHashCount)); } catch (const CSafeIOException& e) { AddDebugLogLineM(true, logSHAHashSet, wxT("IO error while saving AICH HashSet: ") + e.what()); return false; } FreeHashSet(); return true; }