Cheats: Add check for valid game genie code format

- Partially reverts changes in 0b362afcc0
- Checks the codestring if its a valid game genie code before attempting
  to decode it. If code fails, then assume it to be a pro action replay
cheat.

Issue: Cheats where failing because a value of 0 in the decoder does not
mean an error.

Fix https://github.com/libretro/libretro-fceumm/issues/384
This commit is contained in:
negativeExponent
2020-09-09 11:33:36 +08:00
parent 96744ba2ab
commit f4ef507222
2 changed files with 28 additions and 10 deletions

View File

@@ -1958,6 +1958,31 @@ bool retro_unserialize(const void * data, size_t size)
return true;
}
static int checkGG(char c)
{
static const char lets[16] = { 'A', 'P', 'Z', 'L', 'G', 'I', 'T', 'Y', 'E', 'O', 'X', 'U', 'K', 'S', 'V', 'N' };
int x;
for (x = 0; x < 16; x++)
if (lets[x] == toupper(c))
return 1;
return 0;
}
static int GGisvalid(const char *code)
{
size_t len = strlen(code);
int i;
if (len != 6 && len != 8)
return 0;
for (i = 0; i < len; i++)
if (!checkGG(code[i]))
return 0;
return 1;
}
void retro_cheat_reset(void)
{
FCEU_ResetCheats();
@@ -2010,7 +2035,7 @@ void retro_cheat_set(unsigned index, bool enabled, const char *code)
if (a < 0x0100) type = 0;
FCEUI_AddCheat(name, a, v, c, type);
}
else if (FCEUI_DecodeGG(codepart, &a, &v, &c))
else if (GGisvalid(codepart) && FCEUI_DecodeGG(codepart, &a, &v, &c))
{
FCEUI_AddCheat(name, a, v, c, type);
log_cb.log(RETRO_LOG_DEBUG, "Cheat code added: '%s' (GG)\n", codepart);
@@ -2020,6 +2045,8 @@ void retro_cheat_set(unsigned index, bool enabled, const char *code)
FCEUI_AddCheat(name, a, v, c, type);
log_cb.log(RETRO_LOG_DEBUG, "Cheat code added: '%s' (PAR)\n", codepart);
}
else
log_cb.log(RETRO_LOG_DEBUG, "Invalid or unknown code: '%s'\n", codepart);
codepart = strtok(NULL,"+,;._ ");
}
}