]>
git.cameronkatri.com Git - bsdgames-darwin.git/blob - adventure/save.c
1 /* $NetBSD: save.c,v 1.8 2003/08/07 09:36:51 agc 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.8 2003/08/07 09:36:51 agc Exp $");
57 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 {&closng
, sizeof(closng
)},
69 {&daltlc
, sizeof(daltlc
)},
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 {&holdng
, sizeof(holdng
)},
78 {&iwest
, sizeof(iwest
)},
81 {&knfloc
, sizeof(knfloc
)},
83 {&latncy
, sizeof(latncy
)},
84 {&limit
, sizeof(limit
)},
85 {&lmwarn
, sizeof(lmwarn
)},
87 {&maxdie
, sizeof(maxdie
)},
88 {&mxscor
, sizeof(mxscor
)},
89 {&newloc
, sizeof(newloc
)},
90 {&numdie
, sizeof(numdie
)},
92 {&oldlc2
, sizeof(oldlc2
)},
93 {&oldloc
, sizeof(oldloc
)},
94 {&panic
, sizeof(panic
)},
95 {&saveday
, sizeof(saveday
)},
96 {&savet
, sizeof(savet
)},
97 {&scorng
, sizeof(scorng
)},
99 {&stick
, sizeof(stick
)},
100 {&tally
, sizeof(tally
)},
101 {&tally2
, sizeof(tally2
)},
103 {&turns
, sizeof(turns
)},
104 {&verb
, sizeof(verb
)},
107 {&wzdark
, sizeof(wzdark
)},
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
)},
124 save(outfile
) /* Two passes on data: first to get checksum,
126 const char *outfile
; /* to output the data using checksum to start
130 struct savestruct
*p
;
136 for (p
= save_array
; p
->address
!= NULL
; p
++)
137 sum
= crc(p
->address
, p
->width
);
140 if ((out
= fopen(outfile
, "wb")) == NULL
) {
142 "Hmm. The name \"%s\" appears to be magically blocked.\n",
146 fwrite(&sum
, sizeof(sum
), 1, out
); /* Here's the random() key */
147 for (p
= save_array
; p
->address
!= NULL
; p
++) {
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
);
152 if (fclose(out
) != 0) {
153 warn("writing %s", outfile
);
164 struct savestruct
*p
;
169 if ((in
= fopen(infile
, "rb")) == NULL
) {
171 "Hmm. The file \"%s\" appears to be magically blocked.\n",
175 fread(&sum
, sizeof(sum
), 1, in
); /* Get the seed */
177 for (p
= save_array
; p
->address
!= NULL
; p
++) {
178 fread(p
->address
, p
->width
, 1, in
);
179 for (s
= p
->address
, i
= 0; i
< p
->width
; i
++, s
++)
180 *s
= (*s
^ random()) & 0xFF; /* Lightly decrypt */
184 crc_start(); /* See if she cheated */
185 for (p
= save_array
; p
->address
!= NULL
; p
++)
186 cksum
= crc(p
->address
, p
->width
);
187 if (sum
!= cksum
) /* Tsk tsk */
188 return 2; /* Altered the file */
189 /* We successfully restored, so this really was a save file */
190 /* Get rid of the file, but don't bother checking that we did */