GB: Check header for a valid ROM file.

Check some magic numbers in the ROM header to detect Game Genie and Game
Shark ROMs, and check for the Nintendo logo in the header in normal
ROMs.
This commit is contained in:
negativeExponent 2020-02-28 11:03:25 +08:00 committed by Rafael Kitover
parent 2f23467245
commit fd319d2184
No known key found for this signature in database
GPG Key ID: 08AB596679D86240
1 changed files with 31 additions and 0 deletions

View File

@ -778,6 +778,34 @@ static const uint16_t gbColorizationPaletteData[32][3][4] = {
#define GBSAVE_GAME_VERSION_12 12
#define GBSAVE_GAME_VERSION GBSAVE_GAME_VERSION_12
static bool gbCheckRomHeader(void)
{
const uint8_t nlogo[16] = {
0xCE, 0xED, 0x66, 0x66, 0xCC, 0x0D, 0x00, 0x0B,
0x03, 0x73, 0x00, 0x83, 0x00, 0x0C, 0x00, 0x0D
};
// Game Genie
if ((gbRom[2] == 0x6D) && (gbRom[5] == 0x47) && (gbRom[6] == 0x65) && (gbRom[7] == 0x6E) &&
(gbRom[8] == 0x69) && (gbRom[9] == 0x65) && (gbRom[0xA] == 0x28) && (gbRom[0xB] == 0x54)) {
return true;
// Game Shark
} else if (((gbRom[0x104] == 0x44) && (gbRom[0x156] == 0xEA) && (gbRom[0x158] == 0x7F) && (gbRom[0x159] == 0xEA) && (gbRom[0x15B] == 0x7F)) ||
((gbRom[0x165] == 0x3E) && (gbRom[0x166] == 0xD9) && (gbRom[0x16D] == 0xE1) && (gbRom[0x16E] == 0x7F))) {
return true;
// check for 1st 16 bytes of nintendo logo
} else {
uint8_t header[16];
memcpy(header, &gbRom[0x104], 16);
if (!memcmp(header, nlogo, 16))
return true;
}
return false;
}
void setColorizerHack(bool value)
{
allow_colorizer_hack = value;
@ -4154,6 +4182,9 @@ bool gbLoadRom(const char* szFile)
}
bios = (uint8_t*)calloc(1, 0x900);
if (!gbCheckRomHeader())
return false;
return gbUpdateSizes();
}