summaryrefslogtreecommitdiffstats
path: root/adventure
diff options
context:
space:
mode:
authordholland <dholland@NetBSD.org>2012-01-08 18:16:00 +0000
committerdholland <dholland@NetBSD.org>2012-01-08 18:16:00 +0000
commit12be3ddea55e32be7272a4f2e5a4b3c5e39239a7 (patch)
tree2a37d6e3ebed32df01983536df8301f7b34a8be9 /adventure
parentc7181d82c389743f54a91288f6c871b280f1cf71 (diff)
downloadbsdgames-darwin-12be3ddea55e32be7272a4f2e5a4b3c5e39239a7.tar.gz
bsdgames-darwin-12be3ddea55e32be7272a4f2e5a4b3c5e39239a7.tar.zst
bsdgames-darwin-12be3ddea55e32be7272a4f2e5a4b3c5e39239a7.zip
Oops, I forgot to actually implement the checksumming code for the new
savefile format, so any savefiles generated yesterday can be tampered with. Oh well. While here, tidy up the crc code.
Diffstat (limited to 'adventure')
-rw-r--r--adventure/crc.c53
-rw-r--r--adventure/extern.h12
-rw-r--r--adventure/save.c38
3 files changed, 65 insertions, 38 deletions
diff --git a/adventure/crc.c b/adventure/crc.c
index 8626f348..dafd7626 100644
--- a/adventure/crc.c
+++ b/adventure/crc.c
@@ -1,4 +1,4 @@
-/* $NetBSD: crc.c,v 1.12 2012/01/07 18:08:35 dholland Exp $ */
+/* $NetBSD: crc.c,v 1.13 2012/01/08 18:16:00 dholland Exp $ */
/*-
* Copyright (c) 1993
@@ -38,13 +38,13 @@
static char sccsid[] = "@(#)crc.c 8.1 (Berkeley) 5/31/93";
static char ORIGINAL_sccsid[] = "@(#)crc.c 5.2 (Berkeley) 4/4/91";
#else
-__RCSID("$NetBSD: crc.c,v 1.12 2012/01/07 18:08:35 dholland Exp $");
+__RCSID("$NetBSD: crc.c,v 1.13 2012/01/08 18:16:00 dholland Exp $");
#endif
#endif /* not lint */
#include "extern.h"
-static const unsigned long crctab[] = {
+static const uint32_t crctab[256] = {
0x7fffffff,
0x77073096, 0xee0e612c, 0x990951ba, 0x076dc419, 0x706af48f,
0xe963a535, 0x9e6495a3, 0x0edb8832, 0x79dcb8a4, 0xe0d5e91e,
@@ -98,6 +98,7 @@ static const unsigned long crctab[] = {
0x54de5729, 0x23d967bf, 0xb3667a2e, 0xc4614ab8, 0x5d681b02,
0x2a6f2b94, 0xb40bbe37, 0xc30c8ea1, 0x5a05df1b, 0x2d02ef8d
};
+
/*
* crc --
* Compute a POSIX.2 checksum. This routine modified by Jim Gillogly
@@ -106,31 +107,39 @@ static const unsigned long crctab[] = {
* it.
*/
-static unsigned long crcval;
-static unsigned int step;
-
void
-crc_start(void)
+crc_start(struct crcstate *c)
{
- crcval = step = 0;
+ c->crcval = 0;
+ c->step = 0;
}
-/* Process nr bytes at a time; ptr points to them */
-unsigned long
-crc(const char *ptr, int nr)
+/*
+ * Process NUM bytes pointed to by DATA
+ */
+void
+crc_add(struct crcstate *c, const void *data, size_t num)
{
- int i;
- const char *p;
+ const unsigned char *udata;
+ size_t pos;
+ unsigned x;
- while (nr > 0)
- for (p = ptr; nr--; ++p) {
- i = (crcval >> 24 ^ (unsigned char)*p) & 0xff;
- if (i == 0) {
- i = step++;
- if (step >= sizeof(crctab) / sizeof(crctab[0]))
- step = 0;
+ udata = data;
+ pos = 0;
+ while (pos < num) {
+ x = (c->crcval >> 24 ^ udata[pos++]) & 0xff;
+ if (x == 0) {
+ x = c->step++;
+ if (c->step >= __arraycount(crctab)) {
+ c->step = 0;
}
- crcval = (crcval << 8) ^ crctab[i];
}
- return crcval & 0xffffffff; /* Mask to 32 bits. */
+ c->crcval = (c->crcval << 8) ^ crctab[x];
+ }
+}
+
+uint32_t
+crc_get(struct crcstate *c)
+{
+ return c->crcval;
}
diff --git a/adventure/extern.h b/adventure/extern.h
index 0e87e070..548a270c 100644
--- a/adventure/extern.h
+++ b/adventure/extern.h
@@ -1,4 +1,4 @@
-/* $NetBSD: extern.h,v 1.14 2009/10/21 01:07:44 snj Exp $ */
+/* $NetBSD: extern.h,v 1.15 2012/01/08 18:16:00 dholland Exp $ */
/*
* Copyright (c) 1997 Christos Zoulas. All rights reserved.
@@ -27,8 +27,14 @@
#include <string.h>
/* crc.c */
-void crc_start(void);
-unsigned long crc(const char *, int);
+struct crcstate {
+ uint32_t crcval;
+ unsigned step;
+};
+
+void crc_start(struct crcstate *);
+void crc_add(struct crcstate *, const void *, size_t);
+uint32_t crc_get(struct crcstate *);
/* done.c */
int score(void);
diff --git a/adventure/save.c b/adventure/save.c
index b0c92c2b..7392c695 100644
--- a/adventure/save.c
+++ b/adventure/save.c
@@ -1,4 +1,4 @@
-/* $NetBSD: save.c,v 1.12 2012/01/07 22:23:16 dholland Exp $ */
+/* $NetBSD: save.c,v 1.13 2012/01/08 18:16:00 dholland Exp $ */
/*-
* Copyright (c) 1991, 1993
@@ -39,7 +39,7 @@
#if 0
static char sccsid[] = "@(#)save.c 8.1 (Berkeley) 5/31/93";
#else
-__RCSID("$NetBSD: save.c,v 1.12 2012/01/07 22:23:16 dholland Exp $");
+__RCSID("$NetBSD: save.c,v 1.13 2012/01/08 18:16:00 dholland Exp $");
#endif
#endif /* not lint */
@@ -60,13 +60,14 @@ struct savefile {
bool warned;
unsigned bintextpos;
uint32_t key;
- uint32_t sum;
+ struct crcstate crc;
unsigned char pad[8];
unsigned padpos;
};
#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";
////////////////////////////////////////////////////////////
@@ -133,7 +134,7 @@ savefile_open(const char *name, bool forwrite)
sf->warned = false;
sf->bintextpos = 0;
sf->key = 0;
- sf->sum = 0;
+ crc_start(&sf->crc);
memset(sf->pad, 0, sizeof(sf->pad));
sf->padpos = 0;
return sf;
@@ -362,7 +363,7 @@ static void
savefile_key(struct savefile *sf, uint32_t key)
{
sf->key = 0;
- sf->sum = 0;
+ crc_start(&sf->crc);
hash(&sf->key, sizeof(sf->key), sf->pad, sizeof(sf->pad));
sf->padpos = 0;
}
@@ -412,6 +413,7 @@ savefile_cread(struct savefile *sf, void *data, size_t len)
}
pos += amt;
}
+ crc_add(&sf->crc, data, len);
return 0;
}
@@ -443,6 +445,7 @@ savefile_cwrite(struct savefile *sf, const void *data, size_t len)
}
pos += amt;
}
+ crc_add(&sf->crc, data, len);
return 0;
}
@@ -528,6 +531,7 @@ compat_restore(const char *infile)
char *s;
long sum, cksum = 0;
int i;
+ struct crcstate crc;
if ((in = fopen(infile, "rb")) == NULL) {
fprintf(stderr,
@@ -544,9 +548,10 @@ compat_restore(const char *infile)
}
fclose(in);
- crc_start(); /* See if she cheated */
+ crc_start(&crc); /* See if she cheated */
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 */
return 2; /* Altered the file */
/* We successfully restored, so this really was a save file */
@@ -661,7 +666,7 @@ save(const char *outfile)
uint32_t key, writeable_key;
uint32_t version;
unsigned i, j, n;
- uint32_t val;
+ uint32_t val, sum;
sf = savefile_open(outfile, true);
if (sf == NULL) {
@@ -732,8 +737,8 @@ save(const char *outfile)
}
#endif
- sf->sum = htonl(sf->sum);
- if (savefile_binwrite(sf, &sf->sum, sizeof(&sf->sum))) {
+ sum = htonl(crc_get(&sf->crc));
+ if (savefile_binwrite(sf, &sum, sizeof(&sum))) {
savefile_close(sf);
return 1;
}
@@ -753,6 +758,7 @@ restore(const char *infile)
uint32_t version, key, sum;
unsigned i, j, n;
uint32_t val;
+ bool skipsum = false;
sf = savefile_open(infile, false);
if (sf == NULL) {
@@ -777,7 +783,13 @@ restore(const char *infile)
return 1;
}
version = ntohl(version);
- if (version != FORMAT_VERSION) {
+ switch (version) {
+ case FORMAT_VERSION:
+ break;
+ case FORMAT_VERSION_NOSUM:
+ skipsum = true;
+ break;
+ default:
savefile_close(sf);
fprintf(stderr,
"Oh dear, that file must be from the future. I don't know"
@@ -840,7 +852,7 @@ restore(const char *infile)
}
sum = ntohl(sum);
/* See if she cheated */
- if (sum != sf->sum) {
+ if (!skipsum && sum != crc_get(&sf->crc)) {
/* Tsk tsk, altered the file */
savefile_close(sf);
return 2;