xref: /openbsd/games/adventure/save.c (revision ac1fa6a8)
1 /*	$OpenBSD: save.c,v 1.12 2017/01/21 08:22:57 krw Exp $	*/
2 /*	$NetBSD: save.c,v 1.2 1995/03/21 12:05:08 cgd Exp $	*/
3 
4 /*-
5  * Copyright (c) 1991, 1993
6  *	The Regents of the University of California.  All rights reserved.
7  *
8  * The game adventure was originally written in Fortran by Will Crowther
9  * and Don Woods.  It was later translated to C and enhanced by Jim
10  * Gillogly.  This code is derived from software contributed to Berkeley
11  * by Jim Gillogly at The Rand Corporation.
12  *
13  * Redistribution and use in source and binary forms, with or without
14  * modification, are permitted provided that the following conditions
15  * are met:
16  * 1. Redistributions of source code must retain the above copyright
17  *    notice, this list of conditions and the following disclaimer.
18  * 2. Redistributions in binary form must reproduce the above copyright
19  *    notice, this list of conditions and the following disclaimer in the
20  *    documentation and/or other materials provided with the distribution.
21  * 3. Neither the name of the University nor the names of its contributors
22  *    may be used to endorse or promote products derived from this software
23  *    without specific prior written permission.
24  *
25  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
26  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
29  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
30  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
31  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
32  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
34  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35  * SUCH DAMAGE.
36  */
37 
38 #include <stdio.h>
39 #include <stdlib.h>
40 
41 #include "extern.h"
42 #include "hdr.h"
43 
44 struct savestruct
45 {
46 	void *address;
47 	int width;
48 };
49 
50 struct savestruct save_array[] =
51 {
52 	{&abbnum, sizeof(abbnum)},
53 	{&attack, sizeof(attack)},
54 	{&blklin, sizeof(blklin)},
55 	{&bonus,  sizeof(bonus)},
56 	{&chloc,  sizeof(chloc)},
57 	{&chloc2, sizeof(chloc2)},
58 	{&clock1, sizeof(clock1)},
59 	{&clock2, sizeof(clock2)},
60 	{&closed, sizeof(closed)},
61 	{&closng, sizeof(closng)},
62 	{&daltlc, sizeof(daltlc)},
63 	{&demo,   sizeof(demo)},
64 	{&detail, sizeof(detail)},
65 	{&dflag,  sizeof(dflag)},
66 	{&dkill,  sizeof(dkill)},
67 	{&dtotal, sizeof(dtotal)},
68 	{&foobar, sizeof(foobar)},
69 	{&gaveup, sizeof(gaveup)},
70 	{&holdng, sizeof(holdng)},
71 	{&iwest,  sizeof(iwest)},
72 	{&k,      sizeof(k)},
73 	{&k2,     sizeof(k2)},
74 	{&knfloc, sizeof(knfloc)},
75 	{&kq,     sizeof(kq)},
76 	{&latncy, sizeof(latncy)},
77 	{&limit,  sizeof(limit)},
78 	{&lmwarn, sizeof(lmwarn)},
79 	{&loc,    sizeof(loc)},
80 	{&maxdie, sizeof(maxdie)},
81 	{&mxscor, sizeof(mxscor)},
82 	{&newloc, sizeof(newloc)},
83 	{&numdie, sizeof(numdie)},
84 	{&obj,    sizeof(obj)},
85 	{&oldlc2, sizeof(oldlc2)},
86 	{&oldloc, sizeof(oldloc)},
87 	{&panic,  sizeof(panic)},
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)},
111 
112 	{NULL,    0}
113 };
114 
115 /*
116  * Two passes on data: first to get checksum, second
117  * to output the data using checksum to start random #s
118  */
119 int
save(const char * outfile)120 save(const char *outfile)
121 {
122 	FILE   *out;
123 	struct savestruct *p;
124 	char   *s;
125 	long    sum;
126 	int     i;
127 
128 	crc_start();
129 	for (p = save_array; p->address != NULL; p++)
130 		sum = crc(p->address, p->width);
131 	srandom_deterministic((int) sum);
132 
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;
138 	}
139 
140 	fwrite(&sum, sizeof(sum), 1, out);	/* Here's the random() key */
141 	for (p = save_array; p->address != NULL; p++) {
142 		for (s = p->address, i = 0; i < p->width; i++, s++)
143 			*s = (*s ^ random()) & 0xFF;	/* Slightly obfuscate */
144 		fwrite(p->address, p->width, 1, out);
145 	}
146 	fclose(out);
147 	return 0;
148 }
149 
150 int
restore(const char * infile)151 restore(const char *infile)
152 {
153 	FILE   *in;
154 	struct savestruct *p;
155 	char   *s;
156 	long    sum, cksum;
157 	int     i;
158 
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;
164 	}
165 
166 	fread(&sum, sizeof(sum), 1, in);	/* Get the seed */
167 	srandom_deterministic((unsigned int) sum);
168 	for (p = save_array; p->address != NULL; p++) {
169 		fread(p->address, p->width, 1, in);
170 		for (s = p->address, i = 0; i < p->width; i++, s++)
171 			*s = (*s ^ random()) & 0xFF;	/* deobfuscate */
172 	}
173 	fclose(in);
174 
175 	crc_start();			/* See if she cheated */
176 	for (p = save_array; p->address != NULL; p++)
177 		cksum = crc(p->address, p->width);
178 	if (sum != cksum)		/* Tsk tsk */
179 		return 2;		/* Altered the file */
180 	/* We successfully restored, so this really was a save file */
181 	/* Get rid of the file, but don't bother checking that we did */
182 	return 0;
183 }
184