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