write the hash, specified by wHashIdent, with Data from fileInput.
| 395 | |
| 396 | // write the hash, specified by wHashIdent, with Data from fileInput. |
| 397 | bool CAICHHashTree::SetHash(CFileDataIO* fileInput, uint32 wHashIdent, sint8 nLevel, bool bAllowOverwrite) |
| 398 | { |
| 399 | if (nLevel == (-1)) { |
| 400 | // first call, check how many level we need to go |
| 401 | uint8 i = 0; |
| 402 | for (; i != 32 && (wHashIdent & 0x80000000) == 0; ++i) { |
| 403 | wHashIdent <<= 1; |
| 404 | } |
| 405 | if (i > 31) { |
| 406 | AddDebugLogLineN(logSHAHashSet, "CAICHHashTree::SetHash - found invalid HashIdent (0)"); |
| 407 | return false; |
| 408 | } else { |
| 409 | nLevel = 31 - i; |
| 410 | } |
| 411 | } |
| 412 | if (nLevel == 0) { |
| 413 | // this is the searched hash |
| 414 | if (m_bHashValid && !bAllowOverwrite) { |
| 415 | // not allowed to overwrite this hash, however move the filepointer as if we read a hash |
| 416 | fileInput->Seek(HASHSIZE, wxFromCurrent); |
| 417 | return true; |
| 418 | } |
| 419 | m_Hash.Read(fileInput); |
| 420 | m_bHashValid = true; |
| 421 | return true; |
| 422 | } else if (m_nDataSize <= m_nBaseSize) { // sanity |
| 423 | // this is already the last level, can't go deeper |
| 424 | wxFAIL; |
| 425 | return false; |
| 426 | } else { |
| 427 | // adjust ident to point the path to the next node |
| 428 | wHashIdent <<= 1; |
| 429 | nLevel--; |
| 430 | uint64 nBlocks = m_nDataSize / m_nBaseSize + ((m_nDataSize % m_nBaseSize != 0 )? 1:0); |
| 431 | uint64 nLeft = ( ((m_bIsLeftBranch) ? nBlocks+1:nBlocks) / 2)* m_nBaseSize; |
| 432 | uint64 nRight = m_nDataSize - nLeft; |
| 433 | if ((wHashIdent & 0x80000000) > 0) { |
| 434 | if (m_pLeftTree == NULL) { |
| 435 | m_pLeftTree = new CAICHHashTree(nLeft, true, (nLeft <= PARTSIZE) ? EMBLOCKSIZE : PARTSIZE); |
| 436 | } else { |
| 437 | wxASSERT( m_pLeftTree->m_nDataSize == nLeft ); |
| 438 | } |
| 439 | return m_pLeftTree->SetHash(fileInput, wHashIdent, nLevel); |
| 440 | } else { |
| 441 | if (m_pRightTree == NULL) { |
| 442 | m_pRightTree = new CAICHHashTree(nRight, false, (nRight <= PARTSIZE) ? EMBLOCKSIZE : PARTSIZE); |
| 443 | } else { |
| 444 | wxASSERT( m_pRightTree->m_nDataSize == nRight ); |
| 445 | } |
| 446 | return m_pRightTree->SetHash(fileInput, wHashIdent, nLevel); |
| 447 | } |
| 448 | } |
| 449 | } |
| 450 | |
| 451 | |
| 452 | ///////////////////////////////////////////////////////////////////////////////////////// |
no test coverage detected