Skip to content

Commit

Permalink
fix nt header check crashing when export dir is missing
Browse files Browse the repository at this point in the history
  • Loading branch information
spitfirex86 committed Oct 20, 2024
1 parent d9395d9 commit 65962ca
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 21 deletions.
35 changes: 20 additions & 15 deletions GliFixVf/config.c
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@ BOOL CFG_bIsFixEnabled = TRUE;
BOOL CFG_bPatchWidescreen = FALSE;
BOOL CFG_bIsWidescreen = FALSE;

char CFG_ModuleName[MAX_PATH] = "";
char CFG_ModuleDate[20] = "";
char CFG_szModuleName[MAX_PATH] = "";
char CFG_szModuleDate[20] = "";

/*
* Functions
Expand Down Expand Up @@ -170,32 +170,37 @@ BOOL CFG_fn_bOpenConfigTool( void )
BOOL CFG_fn_bDetermineMainModule( void )
{
char szModuleName[MAX_PATH];
GetModuleFileName(GetModuleHandle(NULL), szModuleName, MAX_PATH);
HMODULE hModule = GetModuleHandle(NULL);

GetModuleFileName(hModule, szModuleName, MAX_PATH);
char *pBaseName = strrchr(szModuleName, '\\') + 1;
strcpy_s(CFG_szModuleName, sizeof(CFG_szModuleName), pBaseName);

if ( !strcmp(pBaseName, "Rayman2.exe") )
return TRUE;

// check the header for the original name

unsigned char *pBase = (unsigned char *)GetModuleHandle(NULL);
// query the nt header
unsigned char *pBase = (unsigned char *)hModule;
IMAGE_DOS_HEADER *pDosHeader = (IMAGE_DOS_HEADER *)pBase;
IMAGE_NT_HEADERS *pNtHeader = (IMAGE_NT_HEADERS *)(pBase + pDosHeader->e_lfanew);

// module date
time_t timestamp = pNtHeader->FileHeader.TimeDateStamp;
struct tm const *pTime = gmtime(&timestamp);
strftime(CFG_szModuleDate, sizeof(CFG_szModuleDate), "%F", pTime);

// module name
if ( !pNtHeader->OptionalHeader.NumberOfRvaAndSizes )
return FALSE;

IMAGE_EXPORT_DIRECTORY *pExports = (IMAGE_EXPORT_DIRECTORY *)(pBase + pNtHeader->OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_EXPORT].VirtualAddress);
if ( !pExports->Name )
IMAGE_DATA_DIRECTORY *pExpDir = &pNtHeader->OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_EXPORT];
IMAGE_EXPORT_DIRECTORY *pExports = (IMAGE_EXPORT_DIRECTORY *)(pBase + pExpDir->VirtualAddress);
if ( !pExpDir->Size || !pExports->Name )
return FALSE;

// module name
// got original module name
char const *pName = (char *)(pBase + pExports->Name);
strcpy_s(CFG_ModuleName, sizeof(CFG_ModuleName), pName);

// module date
time_t timestamp = pNtHeader->FileHeader.TimeDateStamp;
struct tm const *pTime = gmtime(&timestamp);
strftime(CFG_ModuleDate, sizeof(CFG_ModuleDate), "%F", pTime);
strcpy_s(CFG_szModuleName, sizeof(CFG_szModuleName), pName);

if ( !_stricmp(pName, "MainWinf.exe") ) // retail RII
return TRUE;
Expand Down
4 changes: 2 additions & 2 deletions GliFixVf/config.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ extern BOOL CFG_bIsFixEnabled;
extern BOOL CFG_bPatchWidescreen;
extern BOOL CFG_bIsWidescreen;

extern char CFG_ModuleName[MAX_PATH];
extern char CFG_ModuleDate[20];
extern char CFG_szModuleName[MAX_PATH];
extern char CFG_szModuleDate[20];


/*
Expand Down
8 changes: 4 additions & 4 deletions GliFixVf/dllmain.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,16 @@ void fn_vInitDll( void )
/* Loaded in the game, patch all */
FIX_fn_vAttachHooks();
}
else if ( !_stricmp(CFG_ModuleName, "MainWinR.exe") ) /* most likely some other flavor of RII */
else if ( !_stricmp(CFG_szModuleName, "MainWinR.exe") ) /* most likely some other flavor of RII */
{
if ( !CFG_DEBUG_lWaitFrame ) /* alt framerate fix */
CFG_DEBUG_lWaitFrame = 1;

if ( !_stricmp(CFG_ModuleDate, "1999-08-18") ) /* Demo 1 */
if ( !_stricmp(CFG_szModuleDate, "1999-08-18") ) /* Demo 1 */
{
FIX_fn_vAttachHooksMin((void *)0x502250, (void *)0x401A10);
}
else if ( !_stricmp(CFG_ModuleDate, "1999-09-04") ) /* Demo 2 */
else if ( !_stricmp(CFG_szModuleDate, "1999-09-04") ) /* Demo 2 */
{
FIX_fn_vAttachHooksMin((void *)0x5129E0, (void *)0x401A10);
}
Expand All @@ -41,7 +41,7 @@ void fn_vDeInitDll( void )
{
FIX_fn_vDetachHooks();
}
else if ( !_stricmp(CFG_ModuleName, "MainWinR.exe") )
else if ( !_stricmp(CFG_szModuleName, "MainWinR.exe") )
{
FIX_fn_vDetachHooksMin();
}
Expand Down

0 comments on commit 65962ca

Please sign in to comment.