This commit is contained in:
twinaphex 2017-12-17 18:25:05 +01:00
parent dde1208a5f
commit 7c7a568a2e

View File

@ -1924,23 +1924,43 @@ static size_t serialize_size;
size_t retro_serialize_size(void) size_t retro_serialize_size(void)
{ {
StateMem st; StateMem st;
memset(&st, 0, sizeof(st));
st.data = NULL;
st.loc = 0;
st.len = 0;
st.malloced = 0;
st.initial_malloc = 0;
if (!MDFNSS_SaveSM(&st, 0, 0, NULL, NULL, NULL)) if (!MDFNSS_SaveSM(&st, 0, 0, NULL, NULL, NULL))
return 0; return 0;
free(st.data); free(st.data);
return serialize_size = st.len; return serialize_size = st.len;
} }
bool retro_serialize(void *data, size_t size) bool retro_serialize(void *data, size_t size)
{ {
StateMem st; StateMem st;
memset(&st, 0, sizeof(st)); bool ret = false;
st.data = (uint8_t*)data; uint8_t *_dat = (uint8_t*)malloc(size);
st.malloced = size;
return MDFNSS_SaveSM(&st, 0, 0, NULL, NULL, NULL); if (!_dat)
return false;
/* Mednafen can realloc the buffer so we need to ensure this is safe. */
st.data = _dat;
st.loc = 0;
st.len = 0;
st.malloced = size;
st.initial_malloc = 0;
ret = MDFNSS_SaveSM(&st, 0, 0, NULL, NULL, NULL);
memcpy(data, st.data, size);
free(st.data);
return ret;
} }
bool retro_unserialize(const void *data, size_t size) bool retro_unserialize(const void *data, size_t size)