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