| 1 | /* Mednafen - Multi-system Emulator |
| 2 | * |
| 3 | * This program is free software; you can redistribute it and/or modify |
| 4 | * it under the terms of the GNU General Public License as published by |
| 5 | * the Free Software Foundation; either version 2 of the License, or |
| 6 | * (at your option) any later version. |
| 7 | * |
| 8 | * This program is distributed in the hope that it will be useful, |
| 9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 11 | * GNU General Public License for more details. |
| 12 | * |
| 13 | * You should have received a copy of the GNU General Public License |
| 14 | * along with this program; if not, write to the Free Software |
| 15 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 16 | */ |
| 17 | |
| 18 | #include <stdint.h> |
| 19 | #include <stdlib.h> |
| 20 | #include <string.h> |
| 21 | |
| 22 | #include <boolean.h> |
| 23 | #include <retro_inline.h> |
| 24 | #include <compat/msvc.h> |
| 25 | #include <compat/strl.h> |
| 26 | |
| 27 | #include "state.h" |
| 28 | |
| 29 | #define SSEEK_END 2 |
| 30 | #define SSEEK_CUR 1 |
| 31 | #define SSEEK_SET 0 |
| 32 | |
| 33 | #define RLSB MDFNSTATE_RLSB //0x80000000 |
| 34 | |
| 35 | /* Forward declaration */ |
| 36 | int StateAction(StateMem *sm, int load, int data_only); |
| 37 | |
| 38 | static INLINE void MDFN_en32lsb(uint8_t *buf, uint32_t morp) |
| 39 | { |
| 40 | buf[0]=morp; |
| 41 | buf[1]=morp>>8; |
| 42 | buf[2]=morp>>16; |
| 43 | buf[3]=morp>>24; |
| 44 | } |
| 45 | |
| 46 | static INLINE uint32_t MDFN_de32lsb(const uint8_t *morp) |
| 47 | { |
| 48 | return(morp[0]|(morp[1]<<8)|(morp[2]<<16)|(morp[3]<<24)); |
| 49 | } |
| 50 | |
| 51 | #ifdef MSB_FIRST |
| 52 | static INLINE void Endian_A64_Swap(void *src, uint32_t nelements) |
| 53 | { |
| 54 | uint32_t i; |
| 55 | uint8_t *nsrc = (uint8_t *)src; |
| 56 | |
| 57 | for(i = 0; i < nelements; i++) |
| 58 | { |
| 59 | unsigned z; |
| 60 | uint8_t *base = &nsrc[i * 8]; |
| 61 | |
| 62 | for(z = 0; z < 4; z++) |
| 63 | { |
| 64 | uint8_t tmp = base[z]; |
| 65 | |
| 66 | base[z] = base[7 - z]; |
| 67 | base[7 - z] = tmp; |
| 68 | } |
| 69 | } |
| 70 | } |
| 71 | |
| 72 | static INLINE void Endian_A32_Swap(void *src, uint32_t nelements) |
| 73 | { |
| 74 | uint32_t i; |
| 75 | uint8_t *nsrc = (uint8_t *)src; |
| 76 | |
| 77 | for(i = 0; i < nelements; i++) |
| 78 | { |
| 79 | uint8_t tmp1 = nsrc[i * 4]; |
| 80 | uint8_t tmp2 = nsrc[i * 4 + 1]; |
| 81 | |
| 82 | nsrc[i * 4] = nsrc[i * 4 + 3]; |
| 83 | nsrc[i * 4 + 1] = nsrc[i * 4 + 2]; |
| 84 | |
| 85 | nsrc[i * 4 + 2] = tmp2; |
| 86 | nsrc[i * 4 + 3] = tmp1; |
| 87 | } |
| 88 | } |
| 89 | |
| 90 | void Endian_A16_Swap(void *src, uint32_t nelements) |
| 91 | { |
| 92 | uint32_t i; |
| 93 | uint8_t *nsrc = (uint8_t *)src; |
| 94 | |
| 95 | for(i = 0; i < nelements; i++) |
| 96 | { |
| 97 | uint8_t tmp = nsrc[i * 2]; |
| 98 | |
| 99 | nsrc[i * 2] = nsrc[i * 2 + 1]; |
| 100 | nsrc[i * 2 + 1] = tmp; |
| 101 | } |
| 102 | } |
| 103 | |
| 104 | static INLINE void FlipByteOrder(uint8_t *src, uint32_t count) |
| 105 | { |
| 106 | uint8_t *start=src; |
| 107 | uint8_t *end=src+count-1; |
| 108 | |
| 109 | if((count&1) || !count) |
| 110 | return; /* This shouldn't happen. */ |
| 111 | |
| 112 | count >>= 1; |
| 113 | |
| 114 | while(count--) |
| 115 | { |
| 116 | uint8_t tmp; |
| 117 | |
| 118 | tmp=*end; |
| 119 | *end=*start; |
| 120 | *start=tmp; |
| 121 | end--; |
| 122 | start++; |
| 123 | } |
| 124 | } |
| 125 | #endif |
| 126 | |
| 127 | static int32_t smem_read(StateMem *st, void *buffer, uint32_t len) |
| 128 | { |
| 129 | if ((len + st->loc) > st->len) |
| 130 | return 0; |
| 131 | |
| 132 | memcpy(buffer, st->data + st->loc, len); |
| 133 | st->loc += len; |
| 134 | |
| 135 | return(len); |
| 136 | } |
| 137 | |
| 138 | static int32_t smem_write(StateMem *st, void *buffer, uint32_t len) |
| 139 | { |
| 140 | if ((len + st->loc) > st->malloced) |
| 141 | { |
| 142 | uint32_t newsize = (st->malloced >= 32768) ? st->malloced : (st->initial_malloc ? st->initial_malloc : 32768); |
| 143 | |
| 144 | while(newsize < (len + st->loc)) |
| 145 | newsize *= 2; |
| 146 | st->data = (uint8_t *)realloc(st->data, newsize); |
| 147 | st->malloced = newsize; |
| 148 | } |
| 149 | memcpy(st->data + st->loc, buffer, len); |
| 150 | st->loc += len; |
| 151 | |
| 152 | if (st->loc > st->len) |
| 153 | st->len = st->loc; |
| 154 | |
| 155 | return(len); |
| 156 | } |
| 157 | |
| 158 | static int32_t smem_seek(StateMem *st, uint32_t offset, int whence) |
| 159 | { |
| 160 | switch(whence) |
| 161 | { |
| 162 | case SSEEK_SET: |
| 163 | st->loc = offset; |
| 164 | break; |
| 165 | case SSEEK_END: |
| 166 | st->loc = st->len - offset; |
| 167 | break; |
| 168 | case SSEEK_CUR: |
| 169 | st->loc += offset; |
| 170 | break; |
| 171 | } |
| 172 | |
| 173 | if(st->loc > st->len) |
| 174 | { |
| 175 | st->loc = st->len; |
| 176 | return(-1); |
| 177 | } |
| 178 | |
| 179 | return(0); |
| 180 | } |
| 181 | |
| 182 | static int smem_write32le(StateMem *st, uint32_t b) |
| 183 | { |
| 184 | uint8_t s[4]; |
| 185 | s[0]=b; |
| 186 | s[1]=b>>8; |
| 187 | s[2]=b>>16; |
| 188 | s[3]=b>>24; |
| 189 | return((smem_write(st, s, 4)<4)?0:4); |
| 190 | } |
| 191 | |
| 192 | static int smem_read32le(StateMem *st, uint32_t *b) |
| 193 | { |
| 194 | uint8_t s[4]; |
| 195 | |
| 196 | if(smem_read(st, s, 4) < 4) |
| 197 | return(0); |
| 198 | |
| 199 | *b = s[0] | (s[1] << 8) | (s[2] << 16) | (s[3] << 24); |
| 200 | |
| 201 | return(4); |
| 202 | } |
| 203 | |
| 204 | static bool SubWrite(StateMem *st, SFORMAT *sf) |
| 205 | { |
| 206 | /* Size can sometimes be zero, so also check for the text name. |
| 207 | * These two should both be zero only at the end of a struct. */ |
| 208 | while(sf->size || sf->name) |
| 209 | { |
| 210 | int32_t bytesize; |
| 211 | char nameo[1 + 256]; |
| 212 | |
| 213 | if(!sf->size || !sf->v) |
| 214 | { |
| 215 | sf++; |
| 216 | continue; |
| 217 | } |
| 218 | |
| 219 | /* Link to another struct. */ |
| 220 | if(sf->size == (uint32_t)~0) |
| 221 | { |
| 222 | if(!SubWrite(st, (SFORMAT *)sf->v)) |
| 223 | return(0); |
| 224 | |
| 225 | sf++; |
| 226 | continue; |
| 227 | } |
| 228 | |
| 229 | bytesize = sf->size; |
| 230 | nameo[0] = strlcpy(nameo + 1, sf->name, 256); |
| 231 | |
| 232 | smem_write(st, nameo, 1 + nameo[0]); |
| 233 | smem_write32le(st, bytesize); |
| 234 | |
| 235 | #ifdef MSB_FIRST |
| 236 | /* Flip the byte order... */ |
| 237 | if(sf->flags & MDFNSTATE_BOOL) { } |
| 238 | else if(sf->flags & MDFNSTATE_RLSB64) |
| 239 | Endian_A64_Swap(sf->v, bytesize / sizeof(uint64_t)); |
| 240 | else if(sf->flags & MDFNSTATE_RLSB32) |
| 241 | Endian_A32_Swap(sf->v, bytesize / sizeof(uint32_t)); |
| 242 | else if(sf->flags & MDFNSTATE_RLSB16) |
| 243 | Endian_A16_Swap(sf->v, bytesize / sizeof(uint16_t)); |
| 244 | else if(sf->flags & RLSB) |
| 245 | FlipByteOrder((uint8_t*)sf->v, bytesize); |
| 246 | #endif |
| 247 | |
| 248 | /* Special case for the evil bool type, |
| 249 | * to convert bool to 1-byte elements. |
| 250 | * Don't do it if we're only saving the raw data. */ |
| 251 | if(sf->flags & MDFNSTATE_BOOL) |
| 252 | { |
| 253 | int32_t bool_monster; |
| 254 | for (bool_monster = 0; bool_monster < bytesize; bool_monster++) |
| 255 | { |
| 256 | uint8_t tmp_bool = ((bool *)sf->v)[bool_monster]; |
| 257 | smem_write(st, &tmp_bool, 1); |
| 258 | } |
| 259 | } |
| 260 | else |
| 261 | smem_write(st, (uint8_t *)sf->v, bytesize); |
| 262 | |
| 263 | #ifdef MSB_FIRST |
| 264 | /* Now restore the original byte order. */ |
| 265 | if(sf->flags & MDFNSTATE_BOOL) |
| 266 | { |
| 267 | |
| 268 | } |
| 269 | else if(sf->flags & MDFNSTATE_RLSB64) |
| 270 | Endian_A64_Swap(sf->v, bytesize / sizeof(uint64_t)); |
| 271 | else if(sf->flags & MDFNSTATE_RLSB32) |
| 272 | Endian_A32_Swap(sf->v, bytesize / sizeof(uint32_t)); |
| 273 | else if(sf->flags & MDFNSTATE_RLSB16) |
| 274 | Endian_A16_Swap(sf->v, bytesize / sizeof(uint16_t)); |
| 275 | else if(sf->flags & RLSB) |
| 276 | FlipByteOrder((uint8_t*)sf->v, bytesize); |
| 277 | #endif |
| 278 | sf++; |
| 279 | } |
| 280 | |
| 281 | return true; |
| 282 | } |
| 283 | |
| 284 | static int WriteStateChunk(StateMem *st, const char *sname, SFORMAT *sf) |
| 285 | { |
| 286 | int32_t data_start_pos; |
| 287 | int32_t end_pos; |
| 288 | |
| 289 | uint8_t sname_tmp[32]; |
| 290 | size_t sname_len = strlen(sname); |
| 291 | |
| 292 | memset(sname_tmp, 0, sizeof(sname_tmp)); |
| 293 | memcpy((char *)sname_tmp, sname, (sname_len < 32) ? sname_len : 32); |
| 294 | |
| 295 | smem_write(st, sname_tmp, 32); |
| 296 | |
| 297 | smem_write32le(st, 0); // We'll come back and write this later. |
| 298 | |
| 299 | data_start_pos = st->loc; |
| 300 | |
| 301 | if(!SubWrite(st, sf)) |
| 302 | return(0); |
| 303 | |
| 304 | end_pos = st->loc; |
| 305 | |
| 306 | smem_seek(st, data_start_pos - 4, SSEEK_SET); |
| 307 | smem_write32le(st, end_pos - data_start_pos); |
| 308 | smem_seek(st, end_pos, SSEEK_SET); |
| 309 | |
| 310 | return(end_pos - data_start_pos); |
| 311 | } |
| 312 | |
| 313 | static SFORMAT *FindSF(const char *name, SFORMAT *sf) |
| 314 | { |
| 315 | /* Size can sometimes be zero, so also check for the text name. |
| 316 | * These two should both be zero only at the end of a struct. */ |
| 317 | while(sf->size || sf->name) |
| 318 | { |
| 319 | if(!sf->size || !sf->v) |
| 320 | { |
| 321 | sf++; |
| 322 | continue; |
| 323 | } |
| 324 | |
| 325 | /* Link to another SFORMAT structure. */ |
| 326 | if (sf->size == (uint32_t)~0) |
| 327 | { |
| 328 | SFORMAT *temp_sf = FindSF(name, (SFORMAT*)sf->v); |
| 329 | if (temp_sf) |
| 330 | return temp_sf; |
| 331 | } |
| 332 | else |
| 333 | { |
| 334 | if (!strcmp(sf->name, name)) |
| 335 | return sf; |
| 336 | } |
| 337 | |
| 338 | sf++; |
| 339 | } |
| 340 | |
| 341 | return NULL; |
| 342 | } |
| 343 | |
| 344 | static int ReadStateChunk(StateMem *st, SFORMAT *sf, int size) |
| 345 | { |
| 346 | int temp = st->loc; |
| 347 | |
| 348 | while (st->loc < (temp + size)) |
| 349 | { |
| 350 | /* Don't change to char unless cast |
| 351 | * toa[0] to unsigned to smem_read() |
| 352 | * and other places. */ |
| 353 | uint8_t toa[1 + 256]; |
| 354 | uint32_t recorded_size = 0; /* In bytes */ |
| 355 | SFORMAT *tmp = NULL; |
| 356 | |
| 357 | if(smem_read(st, toa, 1) != 1) |
| 358 | return(0); |
| 359 | |
| 360 | if(smem_read(st, toa + 1, toa[0]) != toa[0]) |
| 361 | return 0; |
| 362 | |
| 363 | toa[1 + toa[0]] = 0; |
| 364 | |
| 365 | smem_read32le(st, &recorded_size); |
| 366 | |
| 367 | tmp = FindSF((char*)toa + 1, sf); |
| 368 | |
| 369 | if(tmp) |
| 370 | { |
| 371 | uint32_t expected_size = tmp->size; /* In bytes */ |
| 372 | |
| 373 | if(recorded_size != expected_size) |
| 374 | { |
| 375 | if(smem_seek(st, recorded_size, SSEEK_CUR) < 0) |
| 376 | return(0); |
| 377 | } |
| 378 | else |
| 379 | { |
| 380 | smem_read(st, (uint8_t *)tmp->v, expected_size); |
| 381 | |
| 382 | if(tmp->flags & MDFNSTATE_BOOL) |
| 383 | { |
| 384 | int32_t bool_monster; |
| 385 | /* Converting downwards is necessary |
| 386 | * for the case of sizeof(bool) > 1 */ |
| 387 | for(bool_monster = expected_size - 1; bool_monster >= 0; bool_monster--) |
| 388 | ((bool *)tmp->v)[bool_monster] = ((uint8_t *)tmp->v)[bool_monster]; |
| 389 | } |
| 390 | |
| 391 | #ifdef MSB_FIRST |
| 392 | if(tmp->flags & MDFNSTATE_RLSB64) |
| 393 | Endian_A64_Swap(tmp->v, expected_size / sizeof(uint64_t)); |
| 394 | else if(tmp->flags & MDFNSTATE_RLSB32) |
| 395 | Endian_A32_Swap(tmp->v, expected_size / sizeof(uint32_t)); |
| 396 | else if(tmp->flags & MDFNSTATE_RLSB16) |
| 397 | Endian_A16_Swap(tmp->v, expected_size / sizeof(uint16_t)); |
| 398 | else if(tmp->flags & RLSB) |
| 399 | FlipByteOrder((uint8_t*)tmp->v, expected_size); |
| 400 | #endif |
| 401 | } |
| 402 | } |
| 403 | else |
| 404 | { |
| 405 | if(smem_seek(st, recorded_size, SSEEK_CUR) < 0) |
| 406 | return(0); |
| 407 | } |
| 408 | } |
| 409 | |
| 410 | return 1; |
| 411 | } |
| 412 | |
| 413 | static int MDFNSS_StateAction_internal(StateMem *st, int load, int data_only, |
| 414 | struct SSDescriptor *section) |
| 415 | { |
| 416 | if(load) |
| 417 | { |
| 418 | char sname[32]; |
| 419 | |
| 420 | uint32_t tmp_size = 0; |
| 421 | int found = 0; |
| 422 | uint32_t total = 0; |
| 423 | |
| 424 | while(smem_read(st, (uint8_t *)sname, 32) == 32) |
| 425 | { |
| 426 | if(smem_read32le(st, &tmp_size) != 4) |
| 427 | return(0); |
| 428 | |
| 429 | total += tmp_size + 32 + 4; |
| 430 | |
| 431 | // Yay, we found the section |
| 432 | if(!strncmp(sname, section->name, 32)) |
| 433 | { |
| 434 | if(!ReadStateChunk(st, section->sf, tmp_size)) |
| 435 | return(0); |
| 436 | found = 1; |
| 437 | break; |
| 438 | } |
| 439 | else |
| 440 | { |
| 441 | if(smem_seek(st, tmp_size, SSEEK_CUR) < 0) |
| 442 | return(0); |
| 443 | } |
| 444 | } |
| 445 | |
| 446 | if(smem_seek(st, -total, SSEEK_CUR) < 0) |
| 447 | return(0); |
| 448 | |
| 449 | if(!found) // Not found. We are sad! |
| 450 | return(0); |
| 451 | } |
| 452 | else |
| 453 | { |
| 454 | if(!WriteStateChunk(st, section->name, section->sf)) |
| 455 | return(0); |
| 456 | } |
| 457 | |
| 458 | return(1); |
| 459 | } |
| 460 | |
| 461 | int MDFNSS_StateAction(void *st_p, int load, int data_only, SFORMAT *sf, const char *name, bool optional) |
| 462 | { |
| 463 | struct SSDescriptor love; |
| 464 | StateMem *st = (StateMem*)st_p; |
| 465 | |
| 466 | love.sf = sf; |
| 467 | love.name = name; |
| 468 | |
| 469 | return MDFNSS_StateAction_internal(st, load, 0, &love); |
| 470 | } |
| 471 | |
| 472 | int MDFNSS_SaveSM(void *st_p, int a, int b, const void*c, const void*d, const void*e) |
| 473 | { |
| 474 | uint32_t sizy; |
| 475 | uint8_t header[32]; |
| 476 | StateMem *st = (StateMem*)st_p; |
| 477 | static const char *header_magic = "MDFNSVST"; |
| 478 | int neowidth = 0, neoheight = 0; |
| 479 | |
| 480 | memset(header, 0, sizeof(header)); |
| 481 | memcpy(header, header_magic, 8); |
| 482 | |
| 483 | MDFN_en32lsb(header + 16, MEDNAFEN_VERSION_NUMERIC); |
| 484 | MDFN_en32lsb(header + 24, neowidth); |
| 485 | MDFN_en32lsb(header + 28, neoheight); |
| 486 | smem_write(st, header, 32); |
| 487 | |
| 488 | if(!StateAction(st, 0, 0)) |
| 489 | return(0); |
| 490 | |
| 491 | sizy = st->loc; |
| 492 | smem_seek(st, 16 + 4, SSEEK_SET); |
| 493 | smem_write32le(st, sizy); |
| 494 | |
| 495 | return(1); |
| 496 | } |
| 497 | |
| 498 | int MDFNSS_LoadSM(void *st_p, int a, int b) |
| 499 | { |
| 500 | uint8_t header[32]; |
| 501 | uint32_t stateversion; |
| 502 | StateMem *st = (StateMem*)st_p; |
| 503 | |
| 504 | smem_read(st, header, 32); |
| 505 | |
| 506 | if(memcmp(header, "MEDNAFENSVESTATE", 16) && memcmp(header, "MDFNSVST", 8)) |
| 507 | return(0); |
| 508 | |
| 509 | stateversion = MDFN_de32lsb(header + 16); |
| 510 | |
| 511 | return(StateAction(st, stateversion, 0)); |
| 512 | } |