Skip to content

Commit

Permalink
Fix crash with large sddata.bin files
Browse files Browse the repository at this point in the history
  • Loading branch information
elishacloud committed Dec 4, 2017
1 parent 144fe8f commit 6c0e888
Show file tree
Hide file tree
Showing 7 changed files with 69 additions and 12 deletions.
2 changes: 1 addition & 1 deletion BuildNo.rc
Original file line number Diff line number Diff line change
@@ -1 +1 @@
#define BUILD_NUMBER 154
#define BUILD_NUMBER 255
14 changes: 10 additions & 4 deletions Common/Utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,16 @@

#define WIN32_LEAN_AND_MEAN
#include <Windows.h>
#include "Utils.h"
#include "..\Common\Logging.h"

// Search memory for byte array
void *GetAddressOfData(const void *data, size_t len)
{
return GetAddressOfData(data, len, 1);
}

// Search memory for byte array
void *GetAddressOfData(const void *data, size_t len, DWORD step, DWORD start)
{
HANDLE hProcess = GetCurrentProcess();
if (hProcess)
Expand All @@ -29,8 +35,8 @@ void *GetAddressOfData(const void *data, size_t len)

MEMORY_BASIC_INFORMATION info;
std::string chunk;
BYTE* p = 0;
while (p < si.lpMaximumApplicationAddress)
BYTE* p = (BYTE*)start;
while (p < si.lpMaximumApplicationAddress && ((DWORD)p) < start + ((DWORD)0x0FFFFFFF))
{
if (VirtualQueryEx(hProcess, p, &info, sizeof(info)) == sizeof(info))
{
Expand All @@ -39,7 +45,7 @@ void *GetAddressOfData(const void *data, size_t len)
SIZE_T bytesRead;
if (ReadProcessMemory(hProcess, p, &chunk[0], info.RegionSize, &bytesRead))
{
for (size_t i = 0; i < (bytesRead - len); i += 4)
for (size_t i = 0; i < (bytesRead - len); i += step)
{
if (memcmp(data, &chunk[i], len) == 0)
{
Expand Down
1 change: 1 addition & 0 deletions Common/Utils.h
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#pragma once

void *GetAddressOfData(const void *data, size_t len);
void *GetAddressOfData(const void *data, size_t len, DWORD step, DWORD start = 0);
6 changes: 3 additions & 3 deletions NoCDPatch/nocd.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,11 @@

void DisableCDCheck()
{
// Find address for SFX indexes
void *CDCheckAddr = GetAddressOfData(FuncBlock, 7);
// Find address for CD check
void *CDCheckAddr = GetAddressOfData(CDFuncBlock, 7, 4);

// Address found
if (CDCheckAddr && (DWORD)CDCheckAddr < 0x00FFFFFF)
if ((CDCheckAddr) ? (memcmp(CDBlockTest, ((DWORD *)((DWORD)CDCheckAddr + 11)), 8) == 0) : false)
{
// Log message
Log() << "Found CD check function at address: " << CDCheckAddr;
Expand Down
7 changes: 6 additions & 1 deletion NoCDPatch/nocd.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@

void DisableCDCheck();

constexpr BYTE FuncBlock[] = {
constexpr BYTE CDFuncBlock[] = {
0x81, 0xEC, 0x08, 0x04,
0x00, 0x00, 0xA1 };

constexpr BYTE CDBlockTest[] = {
0x33, 0x84, 0x24, 0x08,
0x04, 0x00, 0x00, 0x53 };

42 changes: 39 additions & 3 deletions SFX/sfx.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,10 @@
void UpdateSFXAddr()
{
// Find address for SFX indexes
void *sfxAddr = GetAddressOfData(sfxBlock, 24);
void *sfxAddr = GetAddressOfData(sfxBlock, 24, 4);

// Address found
if (sfxAddr && (DWORD)sfxAddr < 0x00FFFFFF)
if (sfxAddr)
{
// Log message
Log() << "Found SFX pointer address at: " << sfxAddr;
Expand Down Expand Up @@ -103,7 +103,7 @@ void UpdateSFXAddr()
Log() << "Could not find all the indexes in sddata.bin! Found " << IndexCount;
}

// Make memory writeable
// Update SFX address array
DWORD oldProtect;
if (VirtualProtect(sfxAddr, 700 * sizeof(DWORD), PAGE_EXECUTE_READWRITE, &oldProtect))
{
Expand All @@ -122,6 +122,42 @@ void UpdateSFXAddr()
{
Log() << "Could not write to memory!";
}

// Find address for sddata.bin file pointer
void *sfxAddr = GetAddressOfData(sfxPtrvDC, 5, 1, 0x00401000); // Directors Cut
sfxAddr = (!sfxAddr || (DWORD)sfxAddr > (DWORD)0x00628FFF) ? GetAddressOfData(sfxPtrv10, 5, 1, 0x00401000) : sfxAddr; // v1.0
sfxAddr = (!sfxAddr || (DWORD)sfxAddr > (DWORD)0x00628FFF) ? GetAddressOfData(sfxPtrv11, 5, 1, 0x00401000) : sfxAddr; // v1.1
sfxAddr = (!sfxAddr || (DWORD)sfxAddr > (DWORD)0x00628FFF) ? nullptr : sfxAddr;

if (sfxAddr)
{
// Log message
Log() << "Found sddata.bin pointer at address: " << sfxAddr;

// Alocate memory
char *PtrBytes;
PtrBytes = new char[size + 1];

// Update sddata.bin pointer address
if (VirtualProtect(sfxAddr, 4, PAGE_EXECUTE_READWRITE, &oldProtect))
{
Log() << "Updating sddata.bin pointer memory addresses";

// Write to memory
*((DWORD *)((DWORD)sfxAddr + 1)) = (DWORD)PtrBytes;

// Restore protection
VirtualProtect(sfxAddr, 4, oldProtect, &oldProtect);
}
else
{
Log() << "Could not write to memory!";
}
}
else
{
Log() << "Could not find sddata.bin pointer address in memory!";
}
}
else
{
Expand Down
9 changes: 9 additions & 0 deletions SFX/sfx.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,15 @@

void UpdateSFXAddr();

constexpr BYTE sfxPtrvDC[] = {
0x68, 0x40, 0x09, 0xBE, 0x00 };

constexpr BYTE sfxPtrv10[] = {
0x68, 0x40, 0xDD, 0xBD, 0x00 };

constexpr BYTE sfxPtrv11[] = {
0x68, 0x40, 0x19, 0xBE, 0x00 };

constexpr BYTE sfxBlock[] = {
0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00,
Expand Down

0 comments on commit 6c0e888

Please sign in to comment.