From ad01f507d1b35c656c781a50ba3445bca52ea1af Mon Sep 17 00:00:00 2001 From: Franklin Wei Date: Thu, 21 May 2015 21:38:24 -0400 Subject: add some amount of redundancy to save files --- src/save.c | 26 +++++++++++++++++++++++++- 1 file changed, 25 insertions(+), 1 deletion(-) (limited to 'src/save.c') diff --git a/src/save.c b/src/save.c index 27c5e8e..48dd069 100644 --- a/src/save.c +++ b/src/save.c @@ -1,9 +1,15 @@ #include "globals.h" -/* NOTE: integers are represented internally by long long ints, but in the save they are always 64 bits */ +/* NOTE: integers are represented internally as long long ints, but in the save they are always 64 bits */ + +static uint64_t cksum; + +#define ADD_CKSUM(x) (cksum += (x*x) + 1) static bool write_be64(FILE *f, uint64_t n) { + ADD_CKSUM(n); + n = to_be64(n); if(fwrite(&n, sizeof(n), 1, f) != 1) return false; @@ -12,6 +18,16 @@ static bool write_be64(FILE *f, uint64_t n) static bool write_be32(FILE *f, uint32_t n) { + ADD_CKSUM(n); + + n = to_be32(n); + if(fwrite(&n, sizeof(n), 1, f) != 1) + return false; + return true; +} + +static bool write_be32_noupdate(FILE *f, uint32_t n) +{ n = to_be32(n); if(fwrite(&n, sizeof(n), 1, f) != 1) return false; @@ -20,6 +36,8 @@ static bool write_be32(FILE *f, uint32_t n) static bool write_be16(FILE *f, uint16_t n) { + ADD_CKSUM(n); + n = to_be16(n); if(fwrite(&n, sizeof(n), 1, f) != 1) return false; @@ -28,6 +46,8 @@ static bool write_be16(FILE *f, uint16_t n) static bool write_int8(FILE *f, uint8_t n) { + ADD_CKSUM(n); + if(fwrite(&n, sizeof(n), 1, f) != 1) return false; return true; @@ -44,6 +64,8 @@ void save_handler(struct player_t *player) free(filename); + cksum = 0; + const char *magic = "PORTv2"; fwrite(magic, strlen(magic), 1, f); @@ -78,6 +100,8 @@ void save_handler(struct player_t *player) hist = hist->next; } + + write_be32_noupdate(f, cksum); } fclose(f); -- cgit v1.1