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
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
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 $
41 #include <sys/types.h>
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
)},
73 {&knfloc
, sizeof(knfloc
)},
75 {&latncy
, sizeof(latncy
)},
76 {&limit
, sizeof(limit
)},
77 {&lmwarn
, sizeof(lmwarn
)},
79 {&maxdie
, sizeof(maxdie
)},
80 {&mxscor
, sizeof(mxscor
)},
81 {&newloc
, sizeof(newloc
)},
82 {&numdie
, sizeof(numdie
)},
84 {&oldlc2
, sizeof(oldlc2
)},
85 {&oldloc
, sizeof(oldloc
)},
86 {&panic
, sizeof(panic
)},
87 {&saved
, sizeof(saved
)},
88 {&savet
, sizeof(savet
)},
89 {&scorng
, sizeof(scorng
)},
91 {&stick
, sizeof(stick
)},
92 {&tally
, sizeof(tally
)},
93 {&tally2
, sizeof(tally2
)},
95 {&turns
, sizeof(turns
)},
96 {&verb
, sizeof(verb
)},
99 {&wzdark
, sizeof(wzdark
)},
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
)},
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
)
123 struct savestruct
*p
;
129 for (p
= save_array
; p
->address
!= NULL
; p
++)
130 sum
= crc(p
->address
, p
->width
);
133 if ((out
= fopen(outfile
, "wb")) == NULL
) {
135 "Hmm. The name \"%s\" appears to be magically blocked.\n",
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
);
150 restore(const char *infile
)
153 struct savestruct
*p
;
159 if ((in
= fopen(infile
, "rb")) == NULL
) {
161 "Hmm. The file \"%s\" appears to be magically blocked.\n",
165 fread(&sum
, sizeof(sum
), 1, in
); /* Get the seed */
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 */
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 */