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