X-Git-Url: https://jxself.org/git/?a=blobdiff_plain;f=saveresume.c;h=d66b52c928285b52bb88d12f41bebc8234054a1a;hb=ff9c73a37d898d824aa9f8ecd4b670acd84d37e7;hp=858498235fae8d5835e4638edf02f334911ec596;hpb=3640e5cb96db9057dec008b05e69d27676ecbfbb;p=open-adventure.git diff --git a/saveresume.c b/saveresume.c index 8584982..d66b52c 100644 --- a/saveresume.c +++ b/saveresume.c @@ -18,16 +18,23 @@ #include "advent.h" #include "dungeon.h" +/* + * Use this to detect endianness mismatch. Can't be unchanged by byte-swapping. + */ +#define ENDIAN_MAGIC 2317 + struct save_t save; #define IGNORE(r) do{if (r){}}while(0) -int savefile(FILE *fp, int32_t version) +int savefile(FILE *fp) /* Save game to file. No input or output from user. */ { - save.savetime = time(NULL); - save.mode = -1; - save.version = (version == 0) ? SAVE_VERSION : version; + memcpy(&save.magic, ADVENT_MAGIC, sizeof(ADVENT_MAGIC)); + if (save.version == 0) + save.version = SAVE_VERSION; + if (save.canary == 0) + save.canary = ENDIAN_MAGIC; save.game = game; IGNORE(fwrite(&save, sizeof(struct save_t), 1, fp)); @@ -85,7 +92,7 @@ int suspend(void) free(name); } - savefile(fp, SAVE_VERSION); + savefile(fp); fclose(fp); rspeak(RESUME_HELP); exit(EXIT_SUCCESS); @@ -136,7 +143,9 @@ int restore(FILE* fp) IGNORE(fread(&save, sizeof(struct save_t), 1, fp)); fclose(fp); - if (save.version != SAVE_VERSION) { + if (memcmp(save.magic, ADVENT_MAGIC, sizeof(ADVENT_MAGIC)) != 0 || save.canary != ENDIAN_MAGIC) + rspeak(BAD_SAVE); + else if (save.version != SAVE_VERSION) { rspeak(VERSION_SKEW, save.version / 10, MOD(save.version, 10), SAVE_VERSION / 10, MOD(SAVE_VERSION, 10)); } else if (!is_valid(save.game)) { rspeak(SAVE_TAMPERING); @@ -181,8 +190,8 @@ bool is_valid(struct game_t valgame) } /* Bounds check for location arrays */ for (int i = 0; i <= NDWARVES; i++) { - if (valgame.dloc[i] < -1 || valgame.dloc[i] > NLOCATIONS || - valgame.odloc[i] < -1 || valgame.odloc[i] > NLOCATIONS) { + if (valgame.dwarves[i].loc < -1 || valgame.dwarves[i].loc > NLOCATIONS || + valgame.dwarves[i].oldloc < -1 || valgame.dwarves[i].oldloc > NLOCATIONS) { return false; // LCOV_EXCL_LINE } }