save.c revision 1.2 1 1.2 cgd /* $NetBSD: save.c,v 1.2 1995/03/21 12:05:08 cgd 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.1 jtc #ifndef lint
42 1.2 cgd #if 0
43 1.1 jtc static char sccsid[] = "@(#)save.c 8.1 (Berkeley) 5/31/93";
44 1.2 cgd #else
45 1.2 cgd static char rcsid[] = "$NetBSD: save.c,v 1.2 1995/03/21 12:05:08 cgd Exp $";
46 1.2 cgd #endif
47 1.1 jtc #endif /* not lint */
48 1.1 jtc
49 1.1 jtc #include <stdio.h>
50 1.1 jtc #include "hdr.h"
51 1.1 jtc
52 1.1 jtc struct savestruct
53 1.1 jtc {
54 1.1 jtc void *address;
55 1.1 jtc int width;
56 1.1 jtc };
57 1.1 jtc
58 1.1 jtc struct savestruct save_array[] =
59 1.1 jtc {
60 1.1 jtc &abbnum, sizeof(abbnum),
61 1.1 jtc &attack, sizeof(attack),
62 1.1 jtc &blklin, sizeof(blklin),
63 1.1 jtc &bonus, sizeof(bonus),
64 1.1 jtc &chloc, sizeof(chloc),
65 1.1 jtc &chloc2, sizeof(chloc2),
66 1.1 jtc &clock1, sizeof(clock1),
67 1.1 jtc &clock2, sizeof(clock2),
68 1.1 jtc &closed, sizeof(closed),
69 1.1 jtc &closng, sizeof(closng),
70 1.1 jtc &daltlc, sizeof(daltlc),
71 1.1 jtc &demo, sizeof(demo),
72 1.1 jtc &detail, sizeof(detail),
73 1.1 jtc &dflag, sizeof(dflag),
74 1.1 jtc &dkill, sizeof(dkill),
75 1.1 jtc &dtotal, sizeof(dtotal),
76 1.1 jtc &foobar, sizeof(foobar),
77 1.1 jtc &gaveup, sizeof(gaveup),
78 1.1 jtc &holdng, sizeof(holdng),
79 1.1 jtc &iwest, sizeof(iwest),
80 1.1 jtc &k, sizeof(k),
81 1.1 jtc &k2, sizeof(k2),
82 1.1 jtc &knfloc, sizeof(knfloc),
83 1.1 jtc &kq, sizeof(kq),
84 1.1 jtc &latncy, sizeof(latncy),
85 1.1 jtc &limit, sizeof(limit),
86 1.1 jtc &lmwarn, sizeof(lmwarn),
87 1.1 jtc &loc, sizeof(loc),
88 1.1 jtc &maxdie, sizeof(maxdie),
89 1.1 jtc &mxscor, sizeof(mxscor),
90 1.1 jtc &newloc, sizeof(newloc),
91 1.1 jtc &numdie, sizeof(numdie),
92 1.1 jtc &obj, sizeof(obj),
93 1.1 jtc &oldlc2, sizeof(oldlc2),
94 1.1 jtc &oldloc, sizeof(oldloc),
95 1.1 jtc &panic, sizeof(panic),
96 1.1 jtc &saved, sizeof(saved),
97 1.1 jtc &savet, sizeof(savet),
98 1.1 jtc &scorng, sizeof(scorng),
99 1.1 jtc &spk, sizeof(spk),
100 1.1 jtc &stick, sizeof(stick),
101 1.1 jtc &tally, sizeof(tally),
102 1.1 jtc &tally2, sizeof(tally2),
103 1.1 jtc &tkk, sizeof(tkk),
104 1.1 jtc &turns, sizeof(turns),
105 1.1 jtc &verb, sizeof(verb),
106 1.1 jtc &wd1, sizeof(wd1),
107 1.1 jtc &wd2, sizeof(wd2),
108 1.1 jtc &wzdark, sizeof(wzdark),
109 1.1 jtc &yea, sizeof(yea),
110 1.1 jtc atloc, sizeof(atloc),
111 1.1 jtc dloc, sizeof(dloc),
112 1.1 jtc dseen, sizeof(dseen),
113 1.1 jtc fixed, sizeof(fixed),
114 1.1 jtc hinted, sizeof(hinted),
115 1.1 jtc link, sizeof(link),
116 1.1 jtc odloc, sizeof(odloc),
117 1.1 jtc place, sizeof(place),
118 1.1 jtc prop, sizeof(prop),
119 1.1 jtc tk, sizeof(tk),
120 1.1 jtc
121 1.1 jtc NULL, 0
122 1.1 jtc };
123 1.1 jtc
124 1.1 jtc save(outfile) /* Two passes on data: first to get checksum, second */
125 1.1 jtc char *outfile; /* to output the data using checksum to start random #s */
126 1.1 jtc {
127 1.1 jtc FILE *out;
128 1.1 jtc struct savestruct *p;
129 1.1 jtc char *s;
130 1.1 jtc long sum;
131 1.1 jtc int i;
132 1.1 jtc
133 1.1 jtc crc_start();
134 1.1 jtc for (p = save_array; p->address != NULL; p++)
135 1.1 jtc sum = crc(p->address, p->width);
136 1.1 jtc srandom((int) sum);
137 1.1 jtc
138 1.1 jtc if ((out = fopen(outfile, "wb")) == NULL)
139 1.1 jtc {
140 1.1 jtc fprintf(stderr,
141 1.1 jtc "Hmm. The name \"%s\" appears to be magically blocked.\n",
142 1.1 jtc outfile);
143 1.1 jtc return 1;
144 1.1 jtc }
145 1.1 jtc fwrite(&sum, sizeof(sum), 1, out); /* Here's the random() key */
146 1.1 jtc for (p = save_array; p->address != NULL; p++)
147 1.1 jtc {
148 1.1 jtc for (s = p->address, i = 0; i < p->width; i++, s++)
149 1.1 jtc *s = (*s ^ random()) & 0xFF; /* Lightly encrypt */
150 1.1 jtc fwrite(p->address, p->width, 1, out);
151 1.1 jtc }
152 1.1 jtc fclose(out);
153 1.1 jtc return 0;
154 1.1 jtc }
155 1.1 jtc
156 1.1 jtc restore(infile)
157 1.1 jtc char *infile;
158 1.1 jtc {
159 1.1 jtc FILE *in;
160 1.1 jtc struct savestruct *p;
161 1.1 jtc char *s;
162 1.1 jtc long sum, cksum;
163 1.1 jtc int i;
164 1.1 jtc
165 1.1 jtc if ((in = fopen(infile, "rb")) == NULL)
166 1.1 jtc {
167 1.1 jtc fprintf(stderr,
168 1.1 jtc "Hmm. The file \"%s\" appears to be magically blocked.\n",
169 1.1 jtc infile);
170 1.1 jtc return 1;
171 1.1 jtc }
172 1.1 jtc fread(&sum, sizeof(sum), 1, in); /* Get the seed */
173 1.1 jtc srandom((int) sum);
174 1.1 jtc for (p = save_array; p->address != NULL; p++)
175 1.1 jtc {
176 1.1 jtc fread(p->address, p->width, 1, in);
177 1.1 jtc for (s = p->address, i = 0; i < p->width; i++, s++)
178 1.1 jtc *s = (*s ^ random()) & 0xFF; /* Lightly decrypt */
179 1.1 jtc }
180 1.1 jtc fclose(in);
181 1.1 jtc
182 1.1 jtc crc_start(); /* See if she cheated */
183 1.1 jtc for (p = save_array; p->address != NULL; p++)
184 1.1 jtc cksum = crc(p->address, p->width);
185 1.1 jtc if (sum != cksum) /* Tsk tsk */
186 1.1 jtc return 2; /* Altered the file */
187 1.1 jtc /* We successfully restored, so this really was a save file */
188 1.1 jtc /* Get rid of the file, but don't bother checking that we did */
189 1.1 jtc return 0;
190 1.1 jtc }
191