forked from notaz/gpsp
-
Notifications
You must be signed in to change notification settings - Fork 54
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Improve savestate loading to avoid corrupting the current state
It should not happen since the magic value and version would normally discard incompatible savestates, however it's preferrable to check before loading the state (it's just some minor sanity check).
- Loading branch information
1 parent
1d1c719
commit 2352adc
Showing
12 changed files
with
252 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,48 @@ | ||
/* gameplaySP | ||
* | ||
* Copyright (C) 2023 David Guillen Fandos <[email protected]> | ||
* | ||
* This program is free software; you can redistribute it and/or | ||
* modify it under the terms of the GNU General Public License as | ||
* published by the Free Software Foundation; either version 2 of | ||
* the License, or (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
* General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License | ||
* along with this program; if not, write to the Free Software | ||
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA | ||
*/ | ||
|
||
#include "common.h" | ||
|
||
const u8 *state_mem_read_ptr; | ||
u8 *state_mem_write_ptr; | ||
|
||
bool bson_contains_key(const u8 *srcp, const char *key, u8 keytype) | ||
{ | ||
unsigned keyl = strlen(key) + 1; | ||
unsigned doclen = bson_read_u32(srcp); | ||
const u8* p = &srcp[4]; | ||
while (*p != 0 && (p - srcp) < doclen) { | ||
u8 tp = *p; | ||
unsigned tlen = strlen((char*)&p[1]) + 1; | ||
if (keyl == tlen && !memcmp(key, &p[1], tlen)) | ||
return tp == keytype; // Found it, check type | ||
p += 1 + tlen; | ||
if (tp == BSON_TYPE_DOC || tp == BSON_TYPE_ARR) | ||
p += bson_read_u32(p); | ||
else if (tp == BSON_TYPE_BIN) | ||
p += bson_read_u32(p) + 1 + 4; | ||
else if (tp == BSON_TYPE_INT32) | ||
p += 4; | ||
} | ||
return false; | ||
} | ||
|
||
const u8* bson_find_key(const u8 *srcp, const char *key) | ||
{ | ||
unsigned keyl = strlen(key) + 1; | ||
|
@@ -15,11 +54,11 @@ const u8* bson_find_key(const u8 *srcp, const char *key) | |
if (keyl == tlen && !memcmp(key, &p[1], tlen)) | ||
return &p[tlen + 1]; | ||
p += 1 + tlen; | ||
if (tp == 3 || tp == 4) | ||
if (tp == BSON_TYPE_DOC || tp == BSON_TYPE_ARR) | ||
p += bson_read_u32(p); | ||
else if (tp == 5) | ||
else if (tp == BSON_TYPE_BIN) | ||
p += bson_read_u32(p) + 1 + 4; | ||
else if (tp == 0x10) | ||
else if (tp == BSON_TYPE_INT32) | ||
p += 4; | ||
} | ||
return NULL; | ||
|
@@ -82,13 +121,21 @@ bool gba_load_state(const void* src) | |
if (!bson_read_int32(srcptr, "info-version", &tmp) || tmp != GBA_STATE_VERSION) | ||
return false; | ||
|
||
// Validate that the state file makes sense before unconditionally reading it. | ||
if (!cpu_check_savestate(srcptr) || | ||
!input_check_savestate(srcptr) || | ||
!main_check_savestate(srcptr) || | ||
!memory_check_savestate(srcptr) || | ||
!sound_check_savestate(srcptr)) | ||
return false; | ||
|
||
if (!(cpu_read_savestate(srcptr) && | ||
input_read_savestate(srcptr) && | ||
main_read_savestate(srcptr) && | ||
memory_read_savestate(srcptr) && | ||
sound_read_savestate(srcptr))) | ||
{ | ||
// TODO: reset state instead! Should revert instead?? | ||
// TODO: this should not happen if the validation above is accurate. | ||
return false; | ||
} | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,31 @@ | ||
/* gameplaySP | ||
* | ||
* Copyright (C) 2023 David Guillen Fandos <[email protected]> | ||
* | ||
* This program is free software; you can redistribute it and/or | ||
* modify it under the terms of the GNU General Public License as | ||
* published by the Free Software Foundation; either version 2 of | ||
* the License, or (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
* General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License | ||
* along with this program; if not, write to the Free Software | ||
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA | ||
*/ | ||
|
||
#ifndef SAVESTATE_H | ||
#define SAVESTATE_H | ||
|
||
#define BSON_TYPE_STR 0x02 | ||
#define BSON_TYPE_DOC 0x03 | ||
#define BSON_TYPE_ARR 0x04 | ||
#define BSON_TYPE_BIN 0x05 | ||
#define BSON_TYPE_INT32 0x10 | ||
|
||
#define bson_write_u32(p, value) \ | ||
{ \ | ||
u32 __tval = (value); \ | ||
|
@@ -59,6 +83,7 @@ | |
bson_write_u32(hdrptr, _sz); \ | ||
} | ||
|
||
bool bson_contains_key(const u8 *srcp, const char *key, u8 keytype); | ||
const u8* bson_find_key(const u8 *srcp, const char *key); | ||
bool bson_read_int32(const u8 *srcp, const char *key, u32* value); | ||
bool bson_read_int32_array(const u8 *srcp, const char *key, u32* value, unsigned cnt); | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters