We don't display ads so we rely on your Bitcoin donations to 1KWEk9QaiJb2NwP5YFmR24LyUBa4JyuKqZ
Post date: Sep 6, 2010 6:53:58 PM
This code, written by steve10120, optimizes PE files which have appended data (EOF) such as settings.
program FixEOF;
uses
Windows;
type
TByteArray = array of Byte;
{$R *.res}
function FixFile(bFile: TByteArray):Boolean;
var
IDH: TImageDosHeader;
INH: TImageNtHeaders;
ISH: TImageSectionHeader;
i: DWORD;
begin
try
CopyMemory(@IDH, @bFile[0], SizeOf(IDH));
if IDH.e_magic <> IMAGE_DOS_SIGNATURE then Exit;
CopyMemory(@INH, @bFile[IDH._lfanew], SizeOf(INH));
CopyMemory(@ISH, @bFile[IDH._lfanew + SizeOf(INH) + (INH.FileHeader.NumberOfSections - 1) * SizeOf(ISH)], SizeOf(ISH));
i := (ISH.PointerToRawData + ISH.SizeOfRawData);
ISH.SizeOfRawData := ISH.SizeOfRawData + (Length(bFile) - i);
CopyMemory(@bFile[IDH._lfanew + SizeOf(INH) + (INH.FileHeader.NumberOfSections - 1) * SizeOf(ISH)], @ISH, SizeOf(ISH));
Result := TRUE;
except
Result := FALSE;
end;
end;
function ReadAndFix(sFile:string; sDestFile:string):Boolean;
var
hFile: THandle;
bFile: TByteArray;
dSize: DWORD;
dRead: DWORD;
dWritten: DWORD;
begin
Result := FALSE;
hFile := CreateFile(PChar(sFile), GENERIC_READ, FILE_SHARE_READ, nil, OPEN_EXISTING, 0, 0);
if hFile <> INVALID_HANDLE_VALUE then
begin
dSize := GetFileSize(hFile, nil);
SetLength(bFile, dSize);
SetFilePointer(hFile, 0, nil, FILE_BEGIN);
ReadFile(hFile, bFile[0], dSize, dRead, nil);
CloseHandle(hFile);
if FixFile(bFile) then
begin
if sDestFile = '' then sDestFile := sFile;
hFile := CreateFile(PChar(sDestFile), GENERIC_WRITE, FILE_SHARE_WRITE, nil, CREATE_ALWAYS, 0, 0);
if hFile <> INVALID_HANDLE_VALUE then
begin
SetFilePointer(hFile, 0, nil, FILE_BEGIN);
WriteFile(hFile, bFile[0], dSize, dWritten, nil);
CloseHandle(hFile);
Result := TRUE;
end;
end;
end;
end;
end.