aboutsummaryrefslogtreecommitdiff
path: root/src/save.c
diff options
context:
space:
mode:
authorFranklin Wei <git@fwei.tk>2015-05-21 21:38:24 -0400
committerFranklin Wei <git@fwei.tk>2015-05-21 21:38:24 -0400
commitad01f507d1b35c656c781a50ba3445bca52ea1af (patch)
tree508de0b44a6acfaa425ab27fed20efb7d99aa4df /src/save.c
parent06c23ee7f9950c7fe6f8ffda137f0db437f7a92e (diff)
downloadmarket-sim-ad01f507d1b35c656c781a50ba3445bca52ea1af.zip
market-sim-ad01f507d1b35c656c781a50ba3445bca52ea1af.tar.gz
market-sim-ad01f507d1b35c656c781a50ba3445bca52ea1af.tar.bz2
market-sim-ad01f507d1b35c656c781a50ba3445bca52ea1af.tar.xz
add some amount of redundancy to save files
Diffstat (limited to 'src/save.c')
-rw-r--r--src/save.c26
1 files changed, 25 insertions, 1 deletions
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);