From 520d365f74b77496c2b5f7897463e1ddbaf5dfb9 Mon Sep 17 00:00:00 2001 From: "Eric S. Raymond" Date: Thu, 6 Apr 2023 19:35:56 -0400 Subject: [PATCH] Resume now detects if a save has incompatible endianness. --- NEWS.adoc | 4 ++++ saveresume.c | 9 ++++++++- 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/NEWS.adoc b/NEWS.adoc index f01d4f3..da58c79 100644 --- a/NEWS.adoc +++ b/NEWS.adoc @@ -2,6 +2,10 @@ // SPDX-FileCopyrightText: Eric S. Raymond // SPDX-License-Identifier: CC-BY-4.0 +Reoisitory head:: + Savefiles now have an identigting magic cookie at the front. + Resume detects if a save has incompatible endianness. + 1.15: 2023-04-03:: Commands in magic-word sequence now interrupt it, as in original. Bug fix for bird not starting caged in endgame. diff --git a/saveresume.c b/saveresume.c index 0b71319..c6d0ba0 100644 --- a/saveresume.c +++ b/saveresume.c @@ -18,6 +18,11 @@ #include "advent.h" #include "dungeon.h" +/* + * Use this to detect endianness mismatch. Can't be unchanges by byte-swapping. + */ +#define ENDIAN_MAGIC 2317 + struct save_t save; #define IGNORE(r) do{if (r){}}while(0) @@ -28,6 +33,8 @@ int savefile(FILE *fp) 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)); @@ -136,7 +143,7 @@ int restore(FILE* fp) IGNORE(fread(&save, sizeof(struct save_t), 1, fp)); fclose(fp); - if (memcmp(save.magic, ADVENT_MAGIC, sizeof(ADVENT_MAGIC)) != 0) + 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)); -- 2.31.1