]> git.cameronkatri.com Git - bsdgames-darwin.git/blob - adventure/save.c
Move UCB-licensed code from 4-clause to 3-clause licence.
[bsdgames-darwin.git] / adventure / save.c
1 /* $NetBSD: save.c,v 1.8 2003/08/07 09:36:51 agc 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. Neither the name of the University nor the names of its contributors
21 * may be used to endorse or promote products derived from this software
22 * without specific prior written permission.
23 *
24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * SUCH DAMAGE.
35 */
36
37 #include <sys/cdefs.h>
38 #ifndef lint
39 #if 0
40 static char sccsid[] = "@(#)save.c 8.1 (Berkeley) 5/31/93";
41 #else
42 __RCSID("$NetBSD: save.c,v 1.8 2003/08/07 09:36:51 agc Exp $");
43 #endif
44 #endif /* not lint */
45
46 #include <err.h>
47 #include <stdio.h>
48 #include <stdlib.h>
49 #include "hdr.h"
50 #include "extern.h"
51
52 struct savestruct {
53 void *address;
54 int width;
55 };
56
57 struct savestruct save_array[] =
58 {
59 {&abbnum, sizeof(abbnum)},
60 {&attack, sizeof(attack)},
61 {&blklin, sizeof(blklin)},
62 {&bonus, sizeof(bonus)},
63 {&chloc, sizeof(chloc)},
64 {&chloc2, sizeof(chloc2)},
65 {&clock1, sizeof(clock1)},
66 {&clock2, sizeof(clock2)},
67 {&closed, sizeof(closed)},
68 {&closng, sizeof(closng)},
69 {&daltlc, sizeof(daltlc)},
70 {&demo, sizeof(demo)},
71 {&detail, sizeof(detail)},
72 {&dflag, sizeof(dflag)},
73 {&dkill, sizeof(dkill)},
74 {&dtotal, sizeof(dtotal)},
75 {&foobar, sizeof(foobar)},
76 {&gaveup, sizeof(gaveup)},
77 {&holdng, sizeof(holdng)},
78 {&iwest, sizeof(iwest)},
79 {&k, sizeof(k)},
80 {&k2, sizeof(k2)},
81 {&knfloc, sizeof(knfloc)},
82 {&kq, sizeof(kq)},
83 {&latncy, sizeof(latncy)},
84 {&limit, sizeof(limit)},
85 {&lmwarn, sizeof(lmwarn)},
86 {&loc, sizeof(loc)},
87 {&maxdie, sizeof(maxdie)},
88 {&mxscor, sizeof(mxscor)},
89 {&newloc, sizeof(newloc)},
90 {&numdie, sizeof(numdie)},
91 {&obj, sizeof(obj)},
92 {&oldlc2, sizeof(oldlc2)},
93 {&oldloc, sizeof(oldloc)},
94 {&panic, sizeof(panic)},
95 {&saveday, sizeof(saveday)},
96 {&savet, sizeof(savet)},
97 {&scorng, sizeof(scorng)},
98 {&spk, sizeof(spk)},
99 {&stick, sizeof(stick)},
100 {&tally, sizeof(tally)},
101 {&tally2, sizeof(tally2)},
102 {&tkk, sizeof(tkk)},
103 {&turns, sizeof(turns)},
104 {&verb, sizeof(verb)},
105 {&wd1, sizeof(wd1)},
106 {&wd2, sizeof(wd2)},
107 {&wzdark, sizeof(wzdark)},
108 {&yea, sizeof(yea)},
109 {atloc, sizeof(atloc)},
110 {dloc, sizeof(dloc)},
111 {dseen, sizeof(dseen)},
112 {fixed, sizeof(fixed)},
113 {hinted, sizeof(hinted)},
114 {links, sizeof(links)},
115 {odloc, sizeof(odloc)},
116 {place, sizeof(place)},
117 {prop, sizeof(prop)},
118 {tk, sizeof(tk)},
119
120 {NULL, 0}
121 };
122
123 int
124 save(outfile) /* Two passes on data: first to get checksum,
125 * second */
126 const char *outfile; /* to output the data using checksum to start
127 * random #s */
128 {
129 FILE *out;
130 struct savestruct *p;
131 char *s;
132 long sum;
133 int i;
134
135 crc_start();
136 for (p = save_array; p->address != NULL; p++)
137 sum = crc(p->address, p->width);
138 srandom((int) sum);
139
140 if ((out = fopen(outfile, "wb")) == NULL) {
141 fprintf(stderr,
142 "Hmm. The name \"%s\" appears to be magically blocked.\n",
143 outfile);
144 return 1;
145 }
146 fwrite(&sum, sizeof(sum), 1, out); /* Here's the random() key */
147 for (p = save_array; p->address != NULL; p++) {
148 for (s = p->address, i = 0; i < p->width; i++, s++)
149 *s = (*s ^ random()) & 0xFF; /* Lightly encrypt */
150 fwrite(p->address, p->width, 1, out);
151 }
152 if (fclose(out) != 0) {
153 warn("writing %s", outfile);
154 return 1;
155 }
156 return 0;
157 }
158
159 int
160 restore(infile)
161 const 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 }