Sync usage with man page.
[netbsd-mini2440.git] / games / adventure / save.c
blob49bf73b97213bd75f1a1d98ecd8cd846344142ee
1 /* $NetBSD: save.c,v 1.10 2009/08/12 04:28:27 dholland Exp $ */
3 /*-
4 * Copyright (c) 1991, 1993
5 * The Regents of the University of California. All rights reserved.
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.
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.
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.
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.10 2009/08/12 04:28:27 dholland Exp $");
43 #endif
44 #endif /* not lint */
46 #include <err.h>
47 #include <stdio.h>
48 #include <stdlib.h>
49 #include "hdr.h"
50 #include "extern.h"
52 struct savestruct {
53 void *address;
54 int width;
57 static const struct savestruct save_array[] =
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 {&isclosing, sizeof(isclosing)},
69 {&daltloc, sizeof(daltloc)},
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 {&holding, sizeof(holding)},
78 {&iwest, sizeof(iwest)},
79 {&k, sizeof(k)},
80 {&k2, sizeof(k2)},
81 {&knfloc, sizeof(knfloc)},
82 {&kq, sizeof(kq)},
83 {&latency, sizeof(latency)},
84 {&limit, sizeof(limit)},
85 {&lmwarn, sizeof(lmwarn)},
86 {&loc, sizeof(loc)},
87 {&maxdie, sizeof(maxdie)},
88 {&maxscore, sizeof(maxscore)},
89 {&newloc, sizeof(newloc)},
90 {&numdie, sizeof(numdie)},
91 {&obj, sizeof(obj)},
92 {&oldloc2, sizeof(oldloc2)},
93 {&oldloc, sizeof(oldloc)},
94 {&panic, sizeof(panic)},
95 {&saveday, sizeof(saveday)},
96 {&savet, sizeof(savet)},
97 {&scoring, sizeof(scoring)},
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 {&wasdark, sizeof(wasdark)},
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)},
120 {NULL, 0}
123 /* Two passes on data: first to get checksum, second */
124 /* to output the data using checksum to start random #s */
126 save(const char *outfile)
128 FILE *out;
129 const struct savestruct *p;
130 char *s;
131 long sum;
132 int i;
134 crc_start();
135 for (p = save_array; p->address != NULL; p++)
136 sum = crc(p->address, p->width);
137 srandom((int) sum);
139 if ((out = fopen(outfile, "wb")) == NULL) {
140 fprintf(stderr,
141 "Hmm. The name \"%s\" appears to be magically blocked.\n",
142 outfile);
143 return 1;
145 fwrite(&sum, sizeof(sum), 1, out); /* Here's the random() key */
146 for (p = save_array; p->address != NULL; p++) {
147 for (s = p->address, i = 0; i < p->width; i++, s++)
148 *s = (*s ^ random()) & 0xFF; /* Lightly encrypt */
149 fwrite(p->address, p->width, 1, out);
151 if (fclose(out) != 0) {
152 warn("writing %s", outfile);
153 return 1;
155 return 0;
159 restore(const char *infile)
161 FILE *in;
162 const struct savestruct *p;
163 char *s;
164 long sum, cksum = 0;
165 int i;
167 if ((in = fopen(infile, "rb")) == NULL) {
168 fprintf(stderr,
169 "Hmm. The file \"%s\" appears to be magically blocked.\n",
170 infile);
171 return 1;
173 fread(&sum, sizeof(sum), 1, in); /* Get the seed */
174 srandom((int) sum);
175 for (p = save_array; p->address != NULL; p++) {
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 */
180 fclose(in);
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;