xref: /netbsd-src/games/adventure/save.c (revision f5d3fbbc6ff4a77159fb268d247bd94cb7d7e332)
1 /*	$NetBSD: save.c,v 1.4 1997/10/11 01:53:33 lukem Exp $	*/
2 
3 /*-
4  * Copyright (c) 1991, 1993
5  *	The Regents of the University of California.  All rights reserved.
6  *
7  * The game adventure was originally written in Fortran by Will Crowther
8  * and Don Woods.  It was later translated to C and enhanced by Jim
9  * Gillogly.  This code is derived from software contributed to Berkeley
10  * by Jim Gillogly at The Rand Corporation.
11  *
12  * Redistribution and use in source and binary forms, with or without
13  * modification, are permitted provided that the following conditions
14  * are met:
15  * 1. Redistributions of source code must retain the above copyright
16  *    notice, this list of conditions and the following disclaimer.
17  * 2. Redistributions in binary form must reproduce the above copyright
18  *    notice, this list of conditions and the following disclaimer in the
19  *    documentation and/or other materials provided with the distribution.
20  * 3. All advertising materials mentioning features or use of this software
21  *    must display the following acknowledgement:
22  *	This product includes software developed by the University of
23  *	California, Berkeley and its contributors.
24  * 4. Neither the name of the University nor the names of its contributors
25  *    may be used to endorse or promote products derived from this software
26  *    without specific prior written permission.
27  *
28  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
29  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
30  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
31  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
32  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
33  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
34  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
35  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
36  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
37  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
38  * SUCH DAMAGE.
39  */
40 
41 #include <sys/cdefs.h>
42 #ifndef lint
43 #if 0
44 static char sccsid[] = "@(#)save.c	8.1 (Berkeley) 5/31/93";
45 #else
46 __RCSID("$NetBSD: save.c,v 1.4 1997/10/11 01:53:33 lukem Exp $");
47 #endif
48 #endif				/* not lint */
49 
50 #include <stdio.h>
51 #include <stdlib.h>
52 #include "hdr.h"
53 #include "extern.h"
54 
55 struct savestruct {
56 	void   *address;
57 	int     width;
58 };
59 
60 struct savestruct save_array[] =
61 {
62 	{&abbnum, sizeof(abbnum)},
63 	{&attack, sizeof(attack)},
64 	{&blklin, sizeof(blklin)},
65 	{&bonus, sizeof(bonus)},
66 	{&chloc, sizeof(chloc)},
67 	{&chloc2, sizeof(chloc2)},
68 	{&clock1, sizeof(clock1)},
69 	{&clock2, sizeof(clock2)},
70 	{&closed, sizeof(closed)},
71 	{&closng, sizeof(closng)},
72 	{&daltlc, sizeof(daltlc)},
73 	{&demo, sizeof(demo)},
74 	{&detail, sizeof(detail)},
75 	{&dflag, sizeof(dflag)},
76 	{&dkill, sizeof(dkill)},
77 	{&dtotal, sizeof(dtotal)},
78 	{&foobar, sizeof(foobar)},
79 	{&gaveup, sizeof(gaveup)},
80 	{&holdng, sizeof(holdng)},
81 	{&iwest, sizeof(iwest)},
82 	{&k, sizeof(k)},
83 	{&k2, sizeof(k2)},
84 	{&knfloc, sizeof(knfloc)},
85 	{&kq, sizeof(kq)},
86 	{&latncy, sizeof(latncy)},
87 	{&limit, sizeof(limit)},
88 	{&lmwarn, sizeof(lmwarn)},
89 	{&loc, sizeof(loc)},
90 	{&maxdie, sizeof(maxdie)},
91 	{&mxscor, sizeof(mxscor)},
92 	{&newloc, sizeof(newloc)},
93 	{&numdie, sizeof(numdie)},
94 	{&obj, sizeof(obj)},
95 	{&oldlc2, sizeof(oldlc2)},
96 	{&oldloc, sizeof(oldloc)},
97 	{&panic, sizeof(panic)},
98 	{&saved, sizeof(saved)},
99 	{&savet, sizeof(savet)},
100 	{&scorng, sizeof(scorng)},
101 	{&spk, sizeof(spk)},
102 	{&stick, sizeof(stick)},
103 	{&tally, sizeof(tally)},
104 	{&tally2, sizeof(tally2)},
105 	{&tkk, sizeof(tkk)},
106 	{&turns, sizeof(turns)},
107 	{&verb, sizeof(verb)},
108 	{&wd1, sizeof(wd1)},
109 	{&wd2, sizeof(wd2)},
110 	{&wzdark, sizeof(wzdark)},
111 	{&yea, sizeof(yea)},
112 	{atloc, sizeof(atloc)},
113 	{dloc, sizeof(dloc)},
114 	{dseen, sizeof(dseen)},
115 	{fixed, sizeof(fixed)},
116 	{hinted, sizeof(hinted)},
117 	{links, sizeof(links)},
118 	{odloc, sizeof(odloc)},
119 	{place, sizeof(place)},
120 	{prop, sizeof(prop)},
121 	{tk, sizeof(tk)},
122 
123 	{NULL, 0}
124 };
125 
126 int
127 save(outfile)			/* Two passes on data: first to get checksum,
128 				 * second */
129 	char   *outfile;	/* to output the data using checksum to start
130 				 * random #s */
131 {
132 	FILE   *out;
133 	struct savestruct *p;
134 	char   *s;
135 	long    sum;
136 	int     i;
137 
138 	crc_start();
139 	for (p = save_array; p->address != NULL; p++)
140 		sum = crc(p->address, p->width);
141 	srandom((int) sum);
142 
143 	if ((out = fopen(outfile, "wb")) == NULL) {
144 		fprintf(stderr,
145 		    "Hmm.  The name \"%s\" appears to be magically blocked.\n",
146 		    outfile);
147 		return 1;
148 	}
149 	fwrite(&sum, sizeof(sum), 1, out);	/* Here's the random() key */
150 	for (p = save_array; p->address != NULL; p++) {
151 		for (s = p->address, i = 0; i < p->width; i++, s++)
152 			*s = (*s ^ random()) & 0xFF;	/* Lightly encrypt */
153 		fwrite(p->address, p->width, 1, out);
154 	}
155 	fclose(out);
156 	return 0;
157 }
158 
159 int
160 restore(infile)
161 	char   *infile;
162 {
163 	FILE   *in;
164 	struct savestruct *p;
165 	char   *s;
166 	long    sum, cksum = 0;
167 	int     i;
168 
169 	if ((in = fopen(infile, "rb")) == NULL) {
170 		fprintf(stderr,
171 		    "Hmm.  The file \"%s\" appears to be magically blocked.\n",
172 		    infile);
173 		return 1;
174 	}
175 	fread(&sum, sizeof(sum), 1, in);	/* Get the seed */
176 	srandom((int) sum);
177 	for (p = save_array; p->address != NULL; p++) {
178 		fread(p->address, p->width, 1, in);
179 		for (s = p->address, i = 0; i < p->width; i++, s++)
180 			*s = (*s ^ random()) & 0xFF;	/* Lightly decrypt */
181 	}
182 	fclose(in);
183 
184 	crc_start();		/* See if she cheated */
185 	for (p = save_array; p->address != NULL; p++)
186 		cksum = crc(p->address, p->width);
187 	if (sum != cksum)	/* Tsk tsk */
188 		return 2;	/* Altered the file */
189 	/* We successfully restored, so this really was a save file */
190 	/* Get rid of the file, but don't bother checking that we did */
191 	return 0;
192 }
193