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