]>
git.cameronkatri.com Git - bsdgames-darwin.git/blob - adventure/save.c
8eed7e141b4b801f6388566fe3be25eeb30fa490
1 /* $NetBSD: save.c,v 1.4 1997/10/11 01:53:33 lukem 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. All advertising materials mentioning features or use of this software
21 * must display the following acknowledgement:
22 * This product includes software developed by the University of
23 * California, Berkeley and its contributors.
24 * 4. Neither the name of the University nor the names of its contributors
25 * may be used to endorse or promote products derived from this software
26 * without specific prior written permission.
28 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
29 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
30 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
31 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
32 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
33 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
34 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
35 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
36 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
37 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
41 #include <sys/cdefs.h>
44 static char sccsid
[] = "@(#)save.c 8.1 (Berkeley) 5/31/93";
46 __RCSID("$NetBSD: save.c,v 1.4 1997/10/11 01:53:33 lukem Exp $");
60 struct savestruct save_array
[] =
62 {&abbnum
, sizeof(abbnum
)},
63 {&attack
, sizeof(attack
)},
64 {&blklin
, sizeof(blklin
)},
65 {&bonus
, sizeof(bonus
)},
66 {&chloc
, sizeof(chloc
)},
67 {&chloc2
, sizeof(chloc2
)},
68 {&clock1
, sizeof(clock1
)},
69 {&clock2
, sizeof(clock2
)},
70 {&closed
, sizeof(closed
)},
71 {&closng
, sizeof(closng
)},
72 {&daltlc
, sizeof(daltlc
)},
73 {&demo
, sizeof(demo
)},
74 {&detail
, sizeof(detail
)},
75 {&dflag
, sizeof(dflag
)},
76 {&dkill
, sizeof(dkill
)},
77 {&dtotal
, sizeof(dtotal
)},
78 {&foobar
, sizeof(foobar
)},
79 {&gaveup
, sizeof(gaveup
)},
80 {&holdng
, sizeof(holdng
)},
81 {&iwest
, sizeof(iwest
)},
84 {&knfloc
, sizeof(knfloc
)},
86 {&latncy
, sizeof(latncy
)},
87 {&limit
, sizeof(limit
)},
88 {&lmwarn
, sizeof(lmwarn
)},
90 {&maxdie
, sizeof(maxdie
)},
91 {&mxscor
, sizeof(mxscor
)},
92 {&newloc
, sizeof(newloc
)},
93 {&numdie
, sizeof(numdie
)},
95 {&oldlc2
, sizeof(oldlc2
)},
96 {&oldloc
, sizeof(oldloc
)},
97 {&panic
, sizeof(panic
)},
98 {&saved
, sizeof(saved
)},
99 {&savet
, sizeof(savet
)},
100 {&scorng
, sizeof(scorng
)},
102 {&stick
, sizeof(stick
)},
103 {&tally
, sizeof(tally
)},
104 {&tally2
, sizeof(tally2
)},
106 {&turns
, sizeof(turns
)},
107 {&verb
, sizeof(verb
)},
110 {&wzdark
, sizeof(wzdark
)},
112 {atloc
, sizeof(atloc
)},
113 {dloc
, sizeof(dloc
)},
114 {dseen
, sizeof(dseen
)},
115 {fixed
, sizeof(fixed
)},
116 {hinted
, sizeof(hinted
)},
117 {links
, sizeof(links
)},
118 {odloc
, sizeof(odloc
)},
119 {place
, sizeof(place
)},
120 {prop
, sizeof(prop
)},
127 save(outfile
) /* Two passes on data: first to get checksum,
129 char *outfile
; /* to output the data using checksum to start
133 struct savestruct
*p
;
139 for (p
= save_array
; p
->address
!= NULL
; p
++)
140 sum
= crc(p
->address
, p
->width
);
143 if ((out
= fopen(outfile
, "wb")) == NULL
) {
145 "Hmm. The name \"%s\" appears to be magically blocked.\n",
149 fwrite(&sum
, sizeof(sum
), 1, out
); /* Here's the random() key */
150 for (p
= save_array
; p
->address
!= NULL
; p
++) {
151 for (s
= p
->address
, i
= 0; i
< p
->width
; i
++, s
++)
152 *s
= (*s
^ random()) & 0xFF; /* Lightly encrypt */
153 fwrite(p
->address
, p
->width
, 1, out
);
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 */