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