Skip to content

Commit

Permalink
Added support for x64 PE overlays.
Browse files Browse the repository at this point in the history
  • Loading branch information
lazyhamster committed Aug 14, 2022
1 parent f74c7e0 commit b72a19f
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 16 deletions.
36 changes: 21 additions & 15 deletions src/depends/modulecrt/PEHelper.cpp
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
#include "stdafx.h"
#include "PEHelper.h"

bool FindFileOverlay(AStream *inStream, int64_t &nOverlayStartOffset, int64_t &nOverlaySize)
template <typename T>
static bool FindFileOverlayT(AStream *inStream, int64_t &nOverlayStartOffset, int64_t &nOverlaySize, WORD nMachineArch)
{
if (inStream == nullptr)
return false;

int64_t StartOffset = 0;
int64_t DataSize = 0;
int64_t FileSize = inStream->GetSize();
const int64_t FileSize = inStream->GetSize();

inStream->SetPos(0);

Expand All @@ -20,12 +21,12 @@ bool FindFileOverlay(AStream *inStream, int64_t &nOverlayStartOffset, int64_t &n
if (!inStream->SetPos(dosHeader.e_lfanew + sizeof(IMAGE_NT_SIGNATURE)))
return false;

IMAGE_FILE_HEADER fileHeader;
IMAGE_OPTIONAL_HEADER32 optnHeader;
IMAGE_FILE_HEADER fileHeader = { 0 };
T optnHeader = { 0 };

if (!inStream->ReadBuffer(&fileHeader, sizeof(fileHeader)) || !inStream->ReadBuffer(&optnHeader, sizeof(optnHeader)))
return false;
if (fileHeader.Machine != IMAGE_FILE_MACHINE_I386 || fileHeader.NumberOfSections == 0)
if (fileHeader.Machine != nMachineArch || fileHeader.NumberOfSections == 0)
return false;

// Read sections list (save for debug data calc later)
Expand All @@ -39,7 +40,7 @@ bool FindFileOverlay(AStream *inStream, int64_t &nOverlayStartOffset, int64_t &n
if (!inStream->ReadBuffer(&sectionHead, sizeof(sectionHead)))
return false;

__int64 limit = sectionHead.PointerToRawData + sectionHead.SizeOfRawData;
int64_t limit = sectionHead.PointerToRawData + sectionHead.SizeOfRawData;
if (limit > FileSize) return false; // Section data is messed up so exit
if (limit > maxLimit) maxLimit = limit;

Expand Down Expand Up @@ -72,7 +73,7 @@ bool FindFileOverlay(AStream *inStream, int64_t &nOverlayStartOffset, int64_t &n
// Strange debug data
if (dbgSectionIndex < 0) return false;

__int64 debugInfoFileOffset = vSections[dbgSectionIndex].PointerToRawData + (dbgDataDir.VirtualAddress - vSections[dbgSectionIndex].VirtualAddress);
int64_t debugInfoFileOffset = vSections[dbgSectionIndex].PointerToRawData + (dbgDataDir.VirtualAddress - vSections[dbgSectionIndex].VirtualAddress);

if (!inStream->SetPos(debugInfoFileOffset))
return false;
Expand All @@ -83,10 +84,11 @@ bool FindFileOverlay(AStream *inStream, int64_t &nOverlayStartOffset, int64_t &n
if (!inStream->ReadBuffer(&ddir, sizeof(ddir)))
return false;

if (ddir.PointerToRawData + ddir.SizeOfData > StartOffset)
const int64_t dirEnd = static_cast<int64_t>(ddir.PointerToRawData) + ddir.SizeOfData;
if (dirEnd > StartOffset)
{
DataSize -= ddir.PointerToRawData + ddir.SizeOfData - StartOffset;
StartOffset = ddir.PointerToRawData + ddir.SizeOfData;
DataSize -= dirEnd - StartOffset;
StartOffset = dirEnd;
}

if (DataSize <= 0) break;
Expand All @@ -111,6 +113,12 @@ bool FindFileOverlay(AStream *inStream, int64_t &nOverlayStartOffset, int64_t &n
return false;
}

bool FindFileOverlay(AStream* inStream, int64_t& nOverlayStartOffset, int64_t& nOverlaySize)
{
return FindFileOverlayT<IMAGE_OPTIONAL_HEADER32>(inStream, nOverlayStartOffset, nOverlaySize, IMAGE_FILE_MACHINE_I386)
|| FindFileOverlayT<IMAGE_OPTIONAL_HEADER64>(inStream, nOverlayStartOffset, nOverlaySize, IMAGE_FILE_MACHINE_AMD64);
}

std::string GetManifest(const wchar_t* libraryPath)
{
HMODULE hMod = LoadLibraryEx(libraryPath, NULL, LOAD_LIBRARY_AS_DATAFILE);
Expand Down Expand Up @@ -139,8 +147,6 @@ std::string GetManifest(const wchar_t* libraryPath)

bool FindPESection(AStream *inStream, const char* szSectionName, int64_t &nSectionStartOffset, int64_t &nSectionSize)
{
int64_t StartOffset = 0;
int64_t DataSize = 0;
int64_t FileSize = inStream->GetSize();

inStream->SetPos(0);
Expand All @@ -153,8 +159,8 @@ bool FindPESection(AStream *inStream, const char* szSectionName, int64_t &nSecti
if (!inStream->SetPos(dosHeader.e_lfanew + sizeof(IMAGE_NT_SIGNATURE)))
return false;

IMAGE_FILE_HEADER fileHeader;
IMAGE_OPTIONAL_HEADER32 optnHeader;
IMAGE_FILE_HEADER fileHeader = { 0 };
IMAGE_OPTIONAL_HEADER32 optnHeader = { 0 };

if (!inStream->ReadBuffer(&fileHeader, sizeof(fileHeader)) || !inStream->ReadBuffer(&optnHeader, sizeof(optnHeader)))
return false;
Expand All @@ -168,7 +174,7 @@ bool FindPESection(AStream *inStream, const char* szSectionName, int64_t &nSecti
if (!inStream->ReadBuffer(&sectionHead, sizeof(sectionHead)))
return false;

__int64 limit = sectionHead.PointerToRawData + sectionHead.SizeOfRawData;
int64_t limit = sectionHead.PointerToRawData + sectionHead.SizeOfRawData;
if (limit > FileSize) return false; // Section data is messed up so exit
if (limit > maxLimit) maxLimit = limit;

Expand Down
2 changes: 1 addition & 1 deletion src/plugin/text/readme_ru.txt
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ Observer
������� �������� ������ ��� ����� � ���� ����� Enter ��� PgDn. ��� �������������
���� �������� (F11) ��� �������� ������� ������������.
�������� �������� � �������: ModuleName=*.ext1;*.ext2;*.ext3
����� ������� ������� �� ����� [Modules] � ������ ��������. ���������� � ������
����� ������� ������� �� ����� [Modules] � ������ ��������. ���������� � ������
���������� ������ � ������� � ����� ����� �������. ������� ���� �� �����������.
���� ��� ������-���� ������ �� ����� ������ ��� �� ������, �� ���������, ��� ������ ��������� ����� �����.

Expand Down
1 change: 1 addition & 0 deletions src/plugin/text/whatsnew.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
Changes history:

1.12.1
- Added support for Install4j (extracting of internal zip archives).
- SetupFactory: Fixed opening some files from SF 9.5.
- MSI: Fixed regression wtih colon symbol appearing in folder names.
- Minor fixes.
Expand Down
1 change: 1 addition & 0 deletions src/plugin/text/whatsnew_ru.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
������� ���������:

1.12.1
- ��������� ��������� Install4j (���������� ���������� zip-�������).
- SetupFactory: ���������� �������� ��������� ������ �� SF 9.5.
- MSI: ���������� ��������� � ���������� ��������� � ������ ���������.
- ������ ������.
Expand Down

0 comments on commit b72a19f

Please sign in to comment.