Skip to content

Commit 158b740

Browse files
committed
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.
1 parent cdbdbc7 commit 158b740

File tree

3 files changed

+65
-38
lines changed

3 files changed

+65
-38
lines changed

games/adventure/crc.c

Lines changed: 31 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/* $NetBSD: crc.c,v 1.12 2012/01/07 18:08:35 dholland Exp $ */
1+
/* $NetBSD: crc.c,v 1.13 2012/01/08 18:16:00 dholland Exp $ */
22

33
/*-
44
* Copyright (c) 1993
@@ -38,13 +38,13 @@
3838
static char sccsid[] = "@(#)crc.c 8.1 (Berkeley) 5/31/93";
3939
static char ORIGINAL_sccsid[] = "@(#)crc.c 5.2 (Berkeley) 4/4/91";
4040
#else
41-
__RCSID("$NetBSD: crc.c,v 1.12 2012/01/07 18:08:35 dholland Exp $");
41+
__RCSID("$NetBSD: crc.c,v 1.13 2012/01/08 18:16:00 dholland Exp $");
4242
#endif
4343
#endif /* not lint */
4444

4545
#include "extern.h"
4646

47-
static const unsigned long crctab[] = {
47+
static const uint32_t crctab[256] = {
4848
0x7fffffff,
4949
0x77073096, 0xee0e612c, 0x990951ba, 0x076dc419, 0x706af48f,
5050
0xe963a535, 0x9e6495a3, 0x0edb8832, 0x79dcb8a4, 0xe0d5e91e,
@@ -98,6 +98,7 @@ static const unsigned long crctab[] = {
9898
0x54de5729, 0x23d967bf, 0xb3667a2e, 0xc4614ab8, 0x5d681b02,
9999
0x2a6f2b94, 0xb40bbe37, 0xc30c8ea1, 0x5a05df1b, 0x2d02ef8d
100100
};
101+
101102
/*
102103
* crc --
103104
* Compute a POSIX.2 checksum. This routine modified by Jim Gillogly
@@ -106,31 +107,39 @@ static const unsigned long crctab[] = {
106107
* it.
107108
*/
108109

109-
static unsigned long crcval;
110-
static unsigned int step;
111-
112110
void
113-
crc_start(void)
111+
crc_start(struct crcstate *c)
114112
{
115-
crcval = step = 0;
113+
c->crcval = 0;
114+
c->step = 0;
116115
}
117116

118-
/* Process nr bytes at a time; ptr points to them */
119-
unsigned long
120-
crc(const char *ptr, int nr)
117+
/*
118+
* Process NUM bytes pointed to by DATA
119+
*/
120+
void
121+
crc_add(struct crcstate *c, const void *data, size_t num)
121122
{
122-
int i;
123-
const char *p;
123+
const unsigned char *udata;
124+
size_t pos;
125+
unsigned x;
124126

125-
while (nr > 0)
126-
for (p = ptr; nr--; ++p) {
127-
i = (crcval >> 24 ^ (unsigned char)*p) & 0xff;
128-
if (i == 0) {
129-
i = step++;
130-
if (step >= sizeof(crctab) / sizeof(crctab[0]))
131-
step = 0;
127+
udata = data;
128+
pos = 0;
129+
while (pos < num) {
130+
x = (c->crcval >> 24 ^ udata[pos++]) & 0xff;
131+
if (x == 0) {
132+
x = c->step++;
133+
if (c->step >= __arraycount(crctab)) {
134+
c->step = 0;
132135
}
133-
crcval = (crcval << 8) ^ crctab[i];
134136
}
135-
return crcval & 0xffffffff; /* Mask to 32 bits. */
137+
c->crcval = (c->crcval << 8) ^ crctab[x];
138+
}
139+
}
140+
141+
uint32_t
142+
crc_get(struct crcstate *c)
143+
{
144+
return c->crcval;
136145
}

games/adventure/extern.h

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/* $NetBSD: extern.h,v 1.14 2009/10/21 01:07:44 snj Exp $ */
1+
/* $NetBSD: extern.h,v 1.15 2012/01/08 18:16:00 dholland Exp $ */
22

33
/*
44
* Copyright (c) 1997 Christos Zoulas. All rights reserved.
@@ -27,8 +27,14 @@
2727
#include <string.h>
2828

2929
/* crc.c */
30-
void crc_start(void);
31-
unsigned long crc(const char *, int);
30+
struct crcstate {
31+
uint32_t crcval;
32+
unsigned step;
33+
};
34+
35+
void crc_start(struct crcstate *);
36+
void crc_add(struct crcstate *, const void *, size_t);
37+
uint32_t crc_get(struct crcstate *);
3238

3339
/* done.c */
3440
int score(void);

games/adventure/save.c

Lines changed: 25 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/* $NetBSD: save.c,v 1.12 2012/01/07 22:23:16 dholland Exp $ */
1+
/* $NetBSD: save.c,v 1.13 2012/01/08 18:16:00 dholland Exp $ */
22

33
/*-
44
* Copyright (c) 1991, 1993
@@ -39,7 +39,7 @@
3939
#if 0
4040
static char sccsid[] = "@(#)save.c 8.1 (Berkeley) 5/31/93";
4141
#else
42-
__RCSID("$NetBSD: save.c,v 1.12 2012/01/07 22:23:16 dholland Exp $");
42+
__RCSID("$NetBSD: save.c,v 1.13 2012/01/08 18:16:00 dholland Exp $");
4343
#endif
4444
#endif /* not lint */
4545

@@ -60,13 +60,14 @@ struct savefile {
6060
bool warned;
6161
unsigned bintextpos;
6262
uint32_t key;
63-
uint32_t sum;
63+
struct crcstate crc;
6464
unsigned char pad[8];
6565
unsigned padpos;
6666
};
6767

6868
#define BINTEXT_WIDTH 60
69-
#define FORMAT_VERSION 1
69+
#define FORMAT_VERSION 2
70+
#define FORMAT_VERSION_NOSUM 1
7071
static const char header[] = "Adventure save file\n";
7172

7273
////////////////////////////////////////////////////////////
@@ -133,7 +134,7 @@ savefile_open(const char *name, bool forwrite)
133134
sf->warned = false;
134135
sf->bintextpos = 0;
135136
sf->key = 0;
136-
sf->sum = 0;
137+
crc_start(&sf->crc);
137138
memset(sf->pad, 0, sizeof(sf->pad));
138139
sf->padpos = 0;
139140
return sf;
@@ -362,7 +363,7 @@ static void
362363
savefile_key(struct savefile *sf, uint32_t key)
363364
{
364365
sf->key = 0;
365-
sf->sum = 0;
366+
crc_start(&sf->crc);
366367
hash(&sf->key, sizeof(sf->key), sf->pad, sizeof(sf->pad));
367368
sf->padpos = 0;
368369
}
@@ -412,6 +413,7 @@ savefile_cread(struct savefile *sf, void *data, size_t len)
412413
}
413414
pos += amt;
414415
}
416+
crc_add(&sf->crc, data, len);
415417
return 0;
416418
}
417419

@@ -443,6 +445,7 @@ savefile_cwrite(struct savefile *sf, const void *data, size_t len)
443445
}
444446
pos += amt;
445447
}
448+
crc_add(&sf->crc, data, len);
446449
return 0;
447450
}
448451

@@ -528,6 +531,7 @@ compat_restore(const char *infile)
528531
char *s;
529532
long sum, cksum = 0;
530533
int i;
534+
struct crcstate crc;
531535

532536
if ((in = fopen(infile, "rb")) == NULL) {
533537
fprintf(stderr,
@@ -544,9 +548,10 @@ compat_restore(const char *infile)
544548
}
545549
fclose(in);
546550

547-
crc_start(); /* See if she cheated */
551+
crc_start(&crc); /* See if she cheated */
548552
for (p = compat_savearray; p->address != NULL; p++)
549-
cksum = crc(p->address, p->width);
553+
crc_add(&crc, p->address, p->width);
554+
cksum = crc_get(&crc);
550555
if (sum != cksum) /* Tsk tsk */
551556
return 2; /* Altered the file */
552557
/* We successfully restored, so this really was a save file */
@@ -661,7 +666,7 @@ save(const char *outfile)
661666
uint32_t key, writeable_key;
662667
uint32_t version;
663668
unsigned i, j, n;
664-
uint32_t val;
669+
uint32_t val, sum;
665670

666671
sf = savefile_open(outfile, true);
667672
if (sf == NULL) {
@@ -732,8 +737,8 @@ save(const char *outfile)
732737
}
733738
#endif
734739

735-
sf->sum = htonl(sf->sum);
736-
if (savefile_binwrite(sf, &sf->sum, sizeof(&sf->sum))) {
740+
sum = htonl(crc_get(&sf->crc));
741+
if (savefile_binwrite(sf, &sum, sizeof(&sum))) {
737742
savefile_close(sf);
738743
return 1;
739744
}
@@ -753,6 +758,7 @@ restore(const char *infile)
753758
uint32_t version, key, sum;
754759
unsigned i, j, n;
755760
uint32_t val;
761+
bool skipsum = false;
756762

757763
sf = savefile_open(infile, false);
758764
if (sf == NULL) {
@@ -777,7 +783,13 @@ restore(const char *infile)
777783
return 1;
778784
}
779785
version = ntohl(version);
780-
if (version != FORMAT_VERSION) {
786+
switch (version) {
787+
case FORMAT_VERSION:
788+
break;
789+
case FORMAT_VERSION_NOSUM:
790+
skipsum = true;
791+
break;
792+
default:
781793
savefile_close(sf);
782794
fprintf(stderr,
783795
"Oh dear, that file must be from the future. I don't know"
@@ -840,7 +852,7 @@ restore(const char *infile)
840852
}
841853
sum = ntohl(sum);
842854
/* See if she cheated */
843-
if (sum != sf->sum) {
855+
if (!skipsum && sum != crc_get(&sf->crc)) {
844856
/* Tsk tsk, altered the file */
845857
savefile_close(sf);
846858
return 2;

0 commit comments

Comments
 (0)