MCPcopy Create free account
hub / github.com/NtQuery/Scylla / validatePE

Method validatePE

Scylla/PeRebuild.cpp:423–480  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

421}
422
423bool PeRebuild::validatePE(void* pPEImage, DWORD dwFileSize)
424{
425 PIMAGE_NT_HEADERS pNTh;
426 PIMAGE_SECTION_HEADER pSech,pSH, pSH2, pLastSH;
427 UINT i;
428 DWORD dwHeaderSize;
429
430 // get PE base information
431 pNTh = ImageNtHeader(pPEImage);
432
433 if (!pNTh)
434 return FALSE;
435
436
437 pSech = IMAGE_FIRST_SECTION(pNTh);
438
439 // FIX:
440 // ... the SizeOfHeaders
441 pSH = pSech;
442 dwHeaderSize = 0xFFFFFFFF;
443 for(i=0; i < pNTh->FileHeader.NumberOfSections; i++)
444 {
445 if (pSH->PointerToRawData && pSH->PointerToRawData < dwHeaderSize)
446 {
447 dwHeaderSize = pSH->PointerToRawData;
448 }
449 ++pSH;
450 }
451 pNTh->OptionalHeader.SizeOfHeaders = dwHeaderSize;
452
453 // ...Virtual Sizes
454 pSH2 = pSH = pSech;
455 ++pSH2;
456 for (i=0; i < (DWORD)pNTh->FileHeader.NumberOfSections-1; i++)
457 {
458 pSH->Misc.VirtualSize = pSH2->VirtualAddress - pSH->VirtualAddress;
459 ++pSH;
460 ++pSH2;
461 }
462
463 // (pSH -> pointer to last section)
464 pLastSH = pSH;
465
466 // ...RawSize of the last section
467 pLastSH->SizeOfRawData = dwFileSize - pLastSH->PointerToRawData;
468
469 // ...SizeOfImage
470 if (pLastSH->Misc.PhysicalAddress)
471 {
472 pNTh->OptionalHeader.SizeOfImage = pLastSH->VirtualAddress + pLastSH->Misc.VirtualSize;
473 }
474 else // WATCOM is always a bit special >:-)
475 {
476 pNTh->OptionalHeader.SizeOfImage = pLastSH->VirtualAddress + pLastSH->SizeOfRawData;
477 }
478
479 return true;
480}

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected