version 1.12, 2012/01/07 22:23:16 |
version 1.13, 2012/01/08 18:16:00 |
Line 60 struct savefile { |
|
Line 60 struct savefile { |
|
bool warned; |
bool warned; |
unsigned bintextpos; |
unsigned bintextpos; |
uint32_t key; |
uint32_t key; |
uint32_t sum; |
struct crcstate crc; |
unsigned char pad[8]; |
unsigned char pad[8]; |
unsigned padpos; |
unsigned padpos; |
}; |
}; |
|
|
#define BINTEXT_WIDTH 60 |
#define BINTEXT_WIDTH 60 |
#define FORMAT_VERSION 1 |
#define FORMAT_VERSION 2 |
|
#define FORMAT_VERSION_NOSUM 1 |
static const char header[] = "Adventure save file\n"; |
static const char header[] = "Adventure save file\n"; |
|
|
//////////////////////////////////////////////////////////// |
//////////////////////////////////////////////////////////// |
Line 133 savefile_open(const char *name, bool for |
|
Line 134 savefile_open(const char *name, bool for |
|
sf->warned = false; |
sf->warned = false; |
sf->bintextpos = 0; |
sf->bintextpos = 0; |
sf->key = 0; |
sf->key = 0; |
sf->sum = 0; |
crc_start(&sf->crc); |
memset(sf->pad, 0, sizeof(sf->pad)); |
memset(sf->pad, 0, sizeof(sf->pad)); |
sf->padpos = 0; |
sf->padpos = 0; |
return sf; |
return sf; |
|
|
savefile_key(struct savefile *sf, uint32_t key) |
savefile_key(struct savefile *sf, uint32_t key) |
{ |
{ |
sf->key = 0; |
sf->key = 0; |
sf->sum = 0; |
crc_start(&sf->crc); |
hash(&sf->key, sizeof(sf->key), sf->pad, sizeof(sf->pad)); |
hash(&sf->key, sizeof(sf->key), sf->pad, sizeof(sf->pad)); |
sf->padpos = 0; |
sf->padpos = 0; |
} |
} |
Line 412 savefile_cread(struct savefile *sf, void |
|
Line 413 savefile_cread(struct savefile *sf, void |
|
} |
} |
pos += amt; |
pos += amt; |
} |
} |
|
crc_add(&sf->crc, data, len); |
return 0; |
return 0; |
} |
} |
|
|
Line 443 savefile_cwrite(struct savefile *sf, con |
|
Line 445 savefile_cwrite(struct savefile *sf, con |
|
} |
} |
pos += amt; |
pos += amt; |
} |
} |
|
crc_add(&sf->crc, data, len); |
return 0; |
return 0; |
} |
} |
|
|
Line 528 compat_restore(const char *infile) |
|
Line 531 compat_restore(const char *infile) |
|
char *s; |
char *s; |
long sum, cksum = 0; |
long sum, cksum = 0; |
int i; |
int i; |
|
struct crcstate crc; |
|
|
if ((in = fopen(infile, "rb")) == NULL) { |
if ((in = fopen(infile, "rb")) == NULL) { |
fprintf(stderr, |
fprintf(stderr, |
Line 544 compat_restore(const char *infile) |
|
Line 548 compat_restore(const char *infile) |
|
} |
} |
fclose(in); |
fclose(in); |
|
|
crc_start(); /* See if she cheated */ |
crc_start(&crc); /* See if she cheated */ |
for (p = compat_savearray; p->address != NULL; p++) |
for (p = compat_savearray; p->address != NULL; p++) |
cksum = crc(p->address, p->width); |
crc_add(&crc, p->address, p->width); |
|
cksum = crc_get(&crc); |
if (sum != cksum) /* Tsk tsk */ |
if (sum != cksum) /* Tsk tsk */ |
return 2; /* Altered the file */ |
return 2; /* Altered the file */ |
/* We successfully restored, so this really was a save file */ |
/* We successfully restored, so this really was a save file */ |
Line 661 save(const char *outfile) |
|
Line 666 save(const char *outfile) |
|
uint32_t key, writeable_key; |
uint32_t key, writeable_key; |
uint32_t version; |
uint32_t version; |
unsigned i, j, n; |
unsigned i, j, n; |
uint32_t val; |
uint32_t val, sum; |
|
|
sf = savefile_open(outfile, true); |
sf = savefile_open(outfile, true); |
if (sf == NULL) { |
if (sf == NULL) { |
Line 732 save(const char *outfile) |
|
Line 737 save(const char *outfile) |
|
} |
} |
#endif |
#endif |
|
|
sf->sum = htonl(sf->sum); |
sum = htonl(crc_get(&sf->crc)); |
if (savefile_binwrite(sf, &sf->sum, sizeof(&sf->sum))) { |
if (savefile_binwrite(sf, &sum, sizeof(&sum))) { |
savefile_close(sf); |
savefile_close(sf); |
return 1; |
return 1; |
} |
} |
Line 753 restore(const char *infile) |
|
Line 758 restore(const char *infile) |
|
uint32_t version, key, sum; |
uint32_t version, key, sum; |
unsigned i, j, n; |
unsigned i, j, n; |
uint32_t val; |
uint32_t val; |
|
bool skipsum = false; |
|
|
sf = savefile_open(infile, false); |
sf = savefile_open(infile, false); |
if (sf == NULL) { |
if (sf == NULL) { |
Line 777 restore(const char *infile) |
|
Line 783 restore(const char *infile) |
|
return 1; |
return 1; |
} |
} |
version = ntohl(version); |
version = ntohl(version); |
if (version != FORMAT_VERSION) { |
switch (version) { |
|
case FORMAT_VERSION: |
|
break; |
|
case FORMAT_VERSION_NOSUM: |
|
skipsum = true; |
|
break; |
|
default: |
savefile_close(sf); |
savefile_close(sf); |
fprintf(stderr, |
fprintf(stderr, |
"Oh dear, that file must be from the future. I don't know" |
"Oh dear, that file must be from the future. I don't know" |
Line 840 restore(const char *infile) |
|
Line 852 restore(const char *infile) |
|
} |
} |
sum = ntohl(sum); |
sum = ntohl(sum); |
/* See if she cheated */ |
/* See if she cheated */ |
if (sum != sf->sum) { |
if (!skipsum && sum != crc_get(&sf->crc)) { |
/* Tsk tsk, altered the file */ |
/* Tsk tsk, altered the file */ |
savefile_close(sf); |
savefile_close(sf); |
return 2; |
return 2; |