#define NDEBUG
#include "../include/debug.h"
-
/* FUNCTIONS ****************************************************************/
/* Check whether a file is an OS/2 or a very old Windows executable
* FIXME: is reading the module imports the only way of discerning
* old Windows binaries from OS/2 ones ? At least it seems so...
*/
-STATIC DWORD STDCALL
+static DWORD STDCALL
InternalIsOS2OrOldWin(HANDLE hFile, IMAGE_DOS_HEADER *mz, IMAGE_OS2_HEADER *ne)
{
DWORD CurPos;
LPSTR nametab = NULL;
DWORD Read, Ret;
int i;
-
+
Ret = BINARY_OS216;
CurPos = SetFilePointer(hFile, 0, NULL, FILE_CURRENT);
-
+
/* read modref table */
- if((SetFilePointer(hFile, mz->e_lfanew + ne->ne_modtab, NULL, FILE_BEGIN) == -1) ||
+ if((SetFilePointer(hFile, mz->e_lfanew + ne->ne_modtab, NULL, FILE_BEGIN) == (DWORD)-1) ||
(!(modtab = HeapAlloc(GetProcessHeap(), 0, ne->ne_cmod * sizeof(WORD)))) ||
(!(ReadFile(hFile, modtab, ne->ne_cmod * sizeof(WORD), &Read, NULL))) ||
- (Read != ne->ne_cmod * sizeof(WORD)))
+ (Read != (DWORD)ne->ne_cmod * sizeof(WORD)))
{
goto broken;
}
-
+
/* read imported names table */
- if((SetFilePointer(hFile, mz->e_lfanew + ne->ne_imptab, NULL, FILE_BEGIN) == -1) ||
+ if((SetFilePointer(hFile, mz->e_lfanew + ne->ne_imptab, NULL, FILE_BEGIN) == (DWORD)-1) ||
(!(nametab = HeapAlloc(GetProcessHeap(), 0, ne->ne_enttab - ne->ne_imptab))) ||
(!(ReadFile(hFile, nametab, ne->ne_enttab - ne->ne_imptab, &Read, NULL))) ||
- (Read != ne->ne_enttab - ne->ne_imptab))
+ (Read != (DWORD)ne->ne_enttab - ne->ne_imptab))
{
goto broken;
}
-
+
for(i = 0; i < ne->ne_cmod; i++)
{
LPSTR module;
goto done;
}
}
-
+
broken:
DPRINT("InternalIsOS2OrOldWin(): Binary file seems to be broken\n");
-
+
done:
HeapFree(GetProcessHeap(), 0, modtab);
HeapFree(GetProcessHeap(), 0, nametab);
return Ret;
}
-STATIC DWORD STDCALL
+static DWORD STDCALL
InternalGetBinaryType(HANDLE hFile)
{
union
} Header;
char magic[4];
DWORD Read;
-
- if((SetFilePointer(hFile, 0, NULL, FILE_BEGIN) == -1) ||
+
+ if((SetFilePointer(hFile, 0, NULL, FILE_BEGIN) == (DWORD)-1) ||
(!ReadFile(hFile, &Header, sizeof(Header), &Read, NULL) ||
(Read != sizeof(Header))))
{
return BINARY_UNKNOWN;
}
-
+
if(!memcmp(Header.elf.magic, "\177ELF", sizeof(Header.elf.magic)))
{
/* FIXME: we don't bother to check byte order, architecture, etc. */
}
return BINARY_UNKNOWN;
}
-
+
/* Mach-o File with Endian set to Big Endian or Little Endian*/
if(Header.macho.magic == 0xFEEDFACE ||
- Header.macho.magic == 0xECAFDEEF)
+ Header.macho.magic == 0xCEFAEDFE)
{
switch(Header.macho.filetype)
{
}
return BINARY_UNKNOWN;
}
-
+
/* Not ELF, try DOS */
if(Header.mz.e_magic == IMAGE_DOS_SIGNATURE)
{
* This will tell us if there is more header information
* to read or not.
*/
- if((SetFilePointer(hFile, Header.mz.e_lfanew, NULL, FILE_BEGIN) == -1) ||
+ if((SetFilePointer(hFile, Header.mz.e_lfanew, NULL, FILE_BEGIN) == (DWORD)-1) ||
(!ReadFile(hFile, magic, sizeof(magic), &Read, NULL) ||
(Read != sizeof(magic))))
{
return BINARY_DOS;
}
-
+
/* Reading the magic field succeeded so
* we will try to determine what type it is.
*/
{
return BINARY_DOS;
}
-
+
/* FIXME - detect 32/64 bit */
-
+
if(FileHeader.Characteristics & IMAGE_FILE_DLL)
return BINARY_PE_DLL32;
return BINARY_PE_EXE32;
}
-
+
if(!memcmp(magic, "NE", 1))
{
/* This is a Windows executable (NE) header. This can
/* Couldn't read header, so abort. */
return BINARY_DOS;
}
-
+
switch(ne.ne_exetyp)
{
case 2:
{
HANDLE hFile;
DWORD BinType;
-
+
if(!lpApplicationName || !lpBinaryType)
{
SetLastError(ERROR_INVALID_PARAMETER);
return FALSE;
}
-
+
hFile = CreateFileW(lpApplicationName, GENERIC_READ, FILE_SHARE_READ, NULL,
OPEN_EXISTING, 0, 0);
if(hFile == INVALID_HANDLE_VALUE)
{
return FALSE;
}
-
+
BinType = InternalGetBinaryType(hFile);
CloseHandle(hFile);
-
+
switch(BinType)
{
case BINARY_UNKNOWN:
{
WCHAR *dot;
-
+
/*
* guess from filename
*/
return FALSE;
}
}
-
+
DPRINT1("Invalid binary type returned!\n", BinType);
return FALSE;
}
)
{
PWCHAR ApplicationNameW;
-
+
if(!lpApplicationName || !lpBinaryType)
{
SetLastError(ERROR_INVALID_PARAMETER);
return FALSE;
}
-
+
if (!(ApplicationNameW = FilenameA2W(lpApplicationName, FALSE)))
return FALSE;
-
+
return GetBinaryTypeW(ApplicationNameW, lpBinaryType);
}