Home | History | Annotate | Line # | Download | only in adventure
save.c revision 1.3
      1 /*	$NetBSD: save.c,v 1.3 1997/08/11 14:06:17 christos Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 1991, 1993
      5  *	The Regents of the University of California.  All rights reserved.
      6  *
      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.
     11  *
     12  * Redistribution and use in source and binary forms, with or without
     13  * modification, are permitted provided that the following conditions
     14  * are met:
     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.
     27  *
     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
     38  * SUCH DAMAGE.
     39  */
     40 
     41 #include <sys/cdefs.h>
     42 #ifndef lint
     43 #if 0
     44 static char sccsid[] = "@(#)save.c	8.1 (Berkeley) 5/31/93";
     45 #else
     46 __RCSID("$NetBSD: save.c,v 1.3 1997/08/11 14:06:17 christos Exp $");
     47 #endif
     48 #endif /* not lint */
     49 
     50 #include <stdio.h>
     51 #include <stdlib.h>
     52 #include "hdr.h"
     53 #include "extern.h"
     54 
     55 struct savestruct
     56 {
     57 	void *address;
     58 	int width;
     59 };
     60 
     61 struct savestruct save_array[] =
     62 {
     63 	{ &abbnum,        sizeof(abbnum) },
     64 	{ &attack,        sizeof(attack) },
     65 	{ &blklin,        sizeof(blklin) },
     66 	{ &bonus,         sizeof(bonus) },
     67 	{ &chloc,         sizeof(chloc) },
     68 	{ &chloc2,        sizeof(chloc2) },
     69 	{ &clock1,        sizeof(clock1) },
     70 	{ &clock2,        sizeof(clock2) },
     71 	{ &closed,        sizeof(closed) },
     72 	{ &closng,        sizeof(closng) },
     73 	{ &daltlc,        sizeof(daltlc) },
     74 	{ &demo,          sizeof(demo) },
     75 	{ &detail,        sizeof(detail) },
     76 	{ &dflag,         sizeof(dflag) },
     77 	{ &dkill,         sizeof(dkill) },
     78 	{ &dtotal,        sizeof(dtotal) },
     79 	{ &foobar,        sizeof(foobar) },
     80 	{ &gaveup,        sizeof(gaveup) },
     81 	{ &holdng,        sizeof(holdng) },
     82 	{ &iwest,         sizeof(iwest) },
     83 	{ &k,             sizeof(k) },
     84 	{ &k2,            sizeof(k2) },
     85 	{ &knfloc,        sizeof(knfloc) },
     86 	{ &kq,            sizeof(kq) },
     87 	{ &latncy,        sizeof(latncy) },
     88 	{ &limit,         sizeof(limit) },
     89 	{ &lmwarn,        sizeof(lmwarn) },
     90 	{ &loc,           sizeof(loc) },
     91 	{ &maxdie,        sizeof(maxdie) },
     92 	{ &mxscor,        sizeof(mxscor) },
     93 	{ &newloc,        sizeof(newloc) },
     94 	{ &numdie,        sizeof(numdie) },
     95 	{ &obj,           sizeof(obj) },
     96 	{ &oldlc2,        sizeof(oldlc2) },
     97 	{ &oldloc,        sizeof(oldloc) },
     98 	{ &panic,         sizeof(panic) },
     99 	{ &saved,         sizeof(saved) },
    100 	{ &savet,         sizeof(savet) },
    101 	{ &scorng,        sizeof(scorng) },
    102 	{ &spk,           sizeof(spk) },
    103 	{ &stick,         sizeof(stick) },
    104 	{ &tally,         sizeof(tally) },
    105 	{ &tally2,        sizeof(tally2) },
    106 	{ &tkk,           sizeof(tkk) },
    107 	{ &turns,         sizeof(turns) },
    108 	{ &verb,          sizeof(verb) },
    109 	{ &wd1,           sizeof(wd1) },
    110 	{ &wd2,           sizeof(wd2) },
    111 	{ &wzdark,        sizeof(wzdark) },
    112 	{ &yea,           sizeof(yea) },
    113 	{ atloc,          sizeof(atloc) },
    114 	{ dloc,           sizeof(dloc) },
    115 	{ dseen,          sizeof(dseen) },
    116 	{ fixed,          sizeof(fixed) },
    117 	{ hinted,         sizeof(hinted) },
    118 	{ links,          sizeof(links) },
    119 	{ odloc,          sizeof(odloc) },
    120 	{ place,          sizeof(place) },
    121 	{ prop,           sizeof(prop) },
    122 	{ tk,             sizeof(tk) },
    123 
    124 	{ NULL,   0 }
    125 };
    126 
    127 int
    128 save(outfile)   /* Two passes on data: first to get checksum, second */
    129 char *outfile;  /* to output the data using checksum to start random #s */
    130 {
    131 	FILE *out;
    132 	struct savestruct *p;
    133 	char *s;
    134 	long sum;
    135 	int i;
    136 
    137 	crc_start();
    138 	for (p = save_array; p->address != NULL; p++)
    139 		sum = crc(p->address, p->width);
    140 	srandom((int) sum);
    141 
    142 	if ((out = fopen(outfile, "wb")) == NULL)
    143 	{
    144 	    fprintf(stderr,
    145 		"Hmm.  The name \"%s\" appears to be magically blocked.\n",
    146 		outfile);
    147 	    return 1;
    148 	}
    149 	fwrite(&sum, sizeof(sum), 1, out);      /* Here's the random() key */
    150 	for (p = save_array; p->address != NULL; p++)
    151 	{
    152 		for (s = p->address, i = 0; i < p->width; i++, s++)
    153 			*s = (*s ^ random()) & 0xFF;      /* Lightly encrypt */
    154 		fwrite(p->address, p->width, 1, out);
    155 	}
    156 	fclose(out);
    157 	return 0;
    158 }
    159 
    160 int
    161 restore(infile)
    162 char *infile;
    163 {
    164 	FILE *in;
    165 	struct savestruct *p;
    166 	char *s;
    167 	long sum, cksum = 0;
    168 	int i;
    169 
    170 	if ((in = fopen(infile, "rb")) == NULL)
    171 	{
    172 	    fprintf(stderr,
    173 		"Hmm.  The file \"%s\" appears to be magically blocked.\n",
    174 		infile);
    175 	    return 1;
    176 	}
    177 	fread(&sum, sizeof(sum), 1, in);        /* Get the seed */
    178 	srandom((int) sum);
    179 	for (p = save_array; p->address != NULL; p++)
    180 	{
    181 		fread(p->address, p->width, 1, in);
    182 		for (s = p->address, i = 0; i < p->width; i++, s++)
    183 			*s = (*s ^ random()) & 0xFF;  /* Lightly decrypt */
    184 	}
    185 	fclose(in);
    186 
    187 	crc_start();                            /* See if she cheated */
    188 	for (p = save_array; p->address != NULL; p++)
    189 		cksum = crc(p->address, p->width);
    190 	if (sum != cksum)                       /* Tsk tsk */
    191 	    return 2;                           /* Altered the file */
    192 	/* We successfully restored, so this really was a save file */
    193 	/* Get rid of the file, but don't bother checking that we did */
    194 	return 0;
    195 }
    196