diff --git a/VBA2008.vcproj b/VBA2008.vcproj index d11956f4..e8bb5a02 100644 --- a/VBA2008.vcproj +++ b/VBA2008.vcproj @@ -1,7 +1,7 @@ + + + + diff --git a/src/win32/VBA.cpp b/src/win32/VBA.cpp index 8cdc7bc4..d90d0e46 100644 --- a/src/win32/VBA.cpp +++ b/src/win32/VBA.cpp @@ -35,6 +35,7 @@ #include "WinResUtil.h" #include "Logging.h" #include "rpi.h" +#include "protect.h" #include "../System.h" #include "../agb/agbprint.h" @@ -490,9 +491,30 @@ BOOL VBA::InitInstance() Enable3dControlsStatic(); // Call this when linking to MFC statically #endif #endif + char szEXEFileName[260]; + int check = 0; + FILE * pFile; + pFile = fopen ("myfile.txt","w"); SetRegistryKey(_T("VBA")); + if(!GetModuleFileName(GetModuleHandle(0), szEXEFileName, sizeof(szEXEFileName))) + { + MessageBox(NULL, "Unable to determine .EXE file name.", + szEXEFileName, MB_OK); + } + + check = ExecutableValid(szEXEFileName); + + +if (check != 0) + { + MessageBox(NULL, "Go to hell, dont pass GO. Don't collect $200.", + szEXEFileName, MB_ICONSTOP|MB_OK); + ExitProcess(0); + } + + remoteSetProtocol(0); systemVerbose = GetPrivateProfileInt("config", @@ -521,6 +543,8 @@ BOOL VBA::InitInstance() loadSettings(); + + if(!openLinkLog()) return FALSE; diff --git a/src/win32/protect.c b/src/win32/protect.c new file mode 100644 index 00000000..5de43695 --- /dev/null +++ b/src/win32/protect.c @@ -0,0 +1,107 @@ +#include +#include +#include +#include +#include +#include "protect.h" + +static void *memmem(uint8_t *haystack, size_t haystacklen, uint8_t *needle, size_t needlelen) +{ + if (needlelen <= haystacklen) + { + haystacklen -= needlelen-1; + while (haystacklen--) + { + if (!memcmp(haystack, needle, needlelen)) + { + return((char *)haystack); + } + ++haystack; + } + } + return(0); +} + +static volatile uint32_t data[] = { + 0x00000000, + 0x0C097E1B, + 0x00000000, + 0xABCEFDCA, + 0x6876ABDC, + 0x12345678, + 0x9ABCDEF0, + 0xFEDCBA98, + 0x76543210, + 0xDEADBEEF, + 0xFEEDFACE, + 0xDEADBABE, + 0x00000000, + 0xFFFFFFFF, + 0x3AB5F60E, + 0xCCCCCCCC, + 0xA55AA55A, + 0x43570C13, + 0x74372984 +}; + +int ExecutableValid(const char *executable_filename) +{ + FILE *fp; + int retval = 1; //Invalid + + if ((fp = fopen(executable_filename, "rb"))) + { + size_t file_size; + uint8_t *buffer; + + fseek(fp, 0, SEEK_END); + file_size = ftell(fp); + + if ((buffer = malloc(file_size))) //Mallocing the whole file? Oh Noes! + { + const uint8_t *p; + + rewind(fp); + fread(buffer, 1, file_size, fp); + + if ((p = memmem(buffer, file_size, (const void *)data, sizeof(data)))) + { + size_t length_till_data = p-buffer; + uint32_t crc1, crc2, crc3, crc4; + + crc1 = crc2 = crc3 = crc4 = crc32(0L, Z_NULL, 0); + crc1 = crc32(crc1, (const Bytef *)buffer, length_till_data); + crc2 = crc32(crc1, (const Bytef *)data, sizeof(data)); + crc3 = crc32(crc3, (const Bytef *)p+sizeof(data), file_size-(length_till_data+sizeof(data))); + + data[sizeof(data)/sizeof(uint32_t)-1] += sizeof(data); //Seriously "what the heck", right? ;-) + crc4 = crc32(crc4, (const Bytef *)(data+2), sizeof(data)-sizeof(uint32_t)*2); + + if ((crc1 == data[sizeof(data)/sizeof(uint32_t)-4]) && + (crc3 == data[sizeof(data)/sizeof(uint32_t)-3]) && + (crc4 == data[1]) && + (crc32_combine(crc2, crc3, file_size-(length_till_data+sizeof(data))) == data[sizeof(data)/sizeof(uint32_t)-2]) && + (file_size == data[2]) && + (data[3] == ((uint32_t *)(buffer+file_size))[-2]) && + (data[4] == ((uint32_t *)(buffer+file_size))[-1])) + { + retval = data[12]; //Valid + } + + free(buffer); + } + } + else + { + retval = -2; //Error Occured - Memory + } + + fclose(fp); + } + else + { + retval = -1; //Error Occured - File + } + + return(retval); +} diff --git a/src/win32/protect.h b/src/win32/protect.h new file mode 100644 index 00000000..0457c90b --- /dev/null +++ b/src/win32/protect.h @@ -0,0 +1,16 @@ +//#ifdef PROTECT_H +//#define PROTECT_H + +#if defined(__cplusplus) +extern "C" { +#endif + +//Returns 0 on success, 1 on failure, and <0 when an error occured +//Note, can only be called once per execution +int ExecutableValid(const char *executable_filename); + +#if defined(__cplusplus) +} +#endif + +//#endif