Page 1 of 1

Does "love.exe" get the "zip" offset by parsing the PE file structure?

Posted: Sat Sep 21, 2019 7:27 am
by 2Ha
Does "love.exe" get the "zip" offset by parsing the PE file structure?
A file like "love.exe+a.zip".
I find this demo.

Code: Select all

#include <windows.h>
#include <stdio.h>

DWORD GetPEFileRealSize(LPCTSTR lpszFile)
{
	DWORD dwSize = 0;
	int i = 0;
	HANDLE hFile = CreateFile(lpszFile, GENERIC_READ, FILE_SHARE_READ, 0, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
	if (hFile != INVALID_HANDLE_VALUE)
	{
		HANDLE hMap = CreateFileMapping(hFile, NULL, PAGE_READONLY, 0, 0, NULL);
		LPBYTE lpBuffer = (LPBYTE)MapViewOfFile(hMap, FILE_MAP_READ, 0, 0, 0);
		PIMAGE_DOS_HEADER pImgDosHeader = (PIMAGE_DOS_HEADER)lpBuffer;
		PIMAGE_NT_HEADERS32 pImgNtHeaders = (PIMAGE_NT_HEADERS32) (lpBuffer + pImgDosHeader->e_lfanew);
		PIMAGE_SECTION_HEADER pImgSecHeader = (PIMAGE_SECTION_HEADER) (pImgNtHeaders + 1);
		DWORD al = pImgNtHeaders->OptionalHeader.FileAlignment;   
		dwSize = pImgDosHeader->e_lfanew + sizeof(IMAGE_NT_HEADERS32) + pImgNtHeaders->FileHeader.NumberOfSections * sizeof(IMAGE_SECTION_HEADER);
		if (dwSize%al)
		dwSize = (dwSize / al) * al + al;
		for (i = 0; i < pImgNtHeaders->FileHeader.NumberOfSections; i++)
		dwSize += pImgSecHeader[i].SizeOfRawData;
		UnmapViewOfFile(lpBuffer);
		CloseHandle(hMap);
		CloseHandle(hFile);
	}
	return dwSize;
}

int main(int argc, char *argv[])
{
	printf("%x\n", GetPEFileRealSize(argv[0]));
	return 0;
}
Is Love2d the same way? Or is it simpler?

Re: Does "love.exe" get the "zip" offset by parsing the PE file structure?

Posted: Sat Sep 21, 2019 8:43 am
by zorg
executable files have headers, so the important stuff is at the start of the file.
zip archives have footers, meaning the important stuff is at the very end of the file.
So...:
[EXE header][EXE data][ZIP data][ZIP footer]

I would say that yeah, it probably needs to parse the file structure found in the footer, otherwise it would literally not be able to read anything from them; i'm just saying it's not hard finding the literal end of the file. :3

Re: Does "love.exe" get the "zip" offset by parsing the PE file structure?

Posted: Sat Sep 21, 2019 9:41 am
by pgimeno
Indeed, LÖVE is completely agnostic about the executable headers of any particular operating system it runs on. It reads the ZIP footer. Fused executables work with Linux ELF too, for example.

Re: Does "love.exe" get the "zip" offset by parsing the PE file structure?

Posted: Mon Sep 23, 2019 12:14 am
by 2Ha
Thanks to everyone, I am currently adding knowledge about the zip structure.