1 /* $NetBSD: save.c,v 1.10 2009/08/12 04:28:27 dholland Exp $ */
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
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
37 #include <sys/cdefs.h>
40 static char sccsid
[] = "@(#)save.c 8.1 (Berkeley) 5/31/93";
42 __RCSID("$NetBSD: save.c,v 1.10 2009/08/12 04:28:27 dholland Exp $");
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
)},
81 {&knfloc
, sizeof(knfloc
)},
83 {&latency
, sizeof(latency
)},
84 {&limit
, sizeof(limit
)},
85 {&lmwarn
, sizeof(lmwarn
)},
87 {&maxdie
, sizeof(maxdie
)},
88 {&maxscore
, sizeof(maxscore
)},
89 {&newloc
, sizeof(newloc
)},
90 {&numdie
, sizeof(numdie
)},
92 {&oldloc2
, sizeof(oldloc2
)},
93 {&oldloc
, sizeof(oldloc
)},
94 {&panic
, sizeof(panic
)},
95 {&saveday
, sizeof(saveday
)},
96 {&savet
, sizeof(savet
)},
97 {&scoring
, sizeof(scoring
)},
99 {&stick
, sizeof(stick
)},
100 {&tally
, sizeof(tally
)},
101 {&tally2
, sizeof(tally2
)},
103 {&turns
, sizeof(turns
)},
104 {&verb
, sizeof(verb
)},
107 {&wasdark
, sizeof(wasdark
)},
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
)},
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
)
129 const struct savestruct
*p
;
135 for (p
= save_array
; p
->address
!= NULL
; p
++)
136 sum
= crc(p
->address
, p
->width
);
139 if ((out
= fopen(outfile
, "wb")) == NULL
) {
141 "Hmm. The name \"%s\" appears to be magically blocked.\n",
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
);
159 restore(const char *infile
)
162 const struct savestruct
*p
;
167 if ((in
= fopen(infile
, "rb")) == NULL
) {
169 "Hmm. The file \"%s\" appears to be magically blocked.\n",
173 fread(&sum
, sizeof(sum
), 1, in
); /* Get the seed */
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 */
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 */