Home | History | Annotate | Line # | Download | only in adventure
save.c revision 1.5
      1  1.5   hubertf /*	$NetBSD: save.c,v 1.5 1998/09/13 00:07:24 hubertf Exp $	*/
      2  1.2       cgd 
      3  1.1       jtc /*-
      4  1.1       jtc  * Copyright (c) 1991, 1993
      5  1.1       jtc  *	The Regents of the University of California.  All rights reserved.
      6  1.1       jtc  *
      7  1.1       jtc  * The game adventure was originally written in Fortran by Will Crowther
      8  1.1       jtc  * and Don Woods.  It was later translated to C and enhanced by Jim
      9  1.1       jtc  * Gillogly.  This code is derived from software contributed to Berkeley
     10  1.1       jtc  * by Jim Gillogly at The Rand Corporation.
     11  1.1       jtc  *
     12  1.1       jtc  * Redistribution and use in source and binary forms, with or without
     13  1.1       jtc  * modification, are permitted provided that the following conditions
     14  1.1       jtc  * are met:
     15  1.1       jtc  * 1. Redistributions of source code must retain the above copyright
     16  1.1       jtc  *    notice, this list of conditions and the following disclaimer.
     17  1.1       jtc  * 2. Redistributions in binary form must reproduce the above copyright
     18  1.1       jtc  *    notice, this list of conditions and the following disclaimer in the
     19  1.1       jtc  *    documentation and/or other materials provided with the distribution.
     20  1.1       jtc  * 3. All advertising materials mentioning features or use of this software
     21  1.1       jtc  *    must display the following acknowledgement:
     22  1.1       jtc  *	This product includes software developed by the University of
     23  1.1       jtc  *	California, Berkeley and its contributors.
     24  1.1       jtc  * 4. Neither the name of the University nor the names of its contributors
     25  1.1       jtc  *    may be used to endorse or promote products derived from this software
     26  1.1       jtc  *    without specific prior written permission.
     27  1.1       jtc  *
     28  1.1       jtc  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     29  1.1       jtc  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     30  1.1       jtc  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     31  1.1       jtc  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     32  1.1       jtc  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     33  1.1       jtc  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     34  1.1       jtc  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     35  1.1       jtc  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     36  1.1       jtc  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     37  1.1       jtc  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     38  1.1       jtc  * SUCH DAMAGE.
     39  1.1       jtc  */
     40  1.1       jtc 
     41  1.3  christos #include <sys/cdefs.h>
     42  1.1       jtc #ifndef lint
     43  1.2       cgd #if 0
     44  1.1       jtc static char sccsid[] = "@(#)save.c	8.1 (Berkeley) 5/31/93";
     45  1.2       cgd #else
     46  1.5   hubertf __RCSID("$NetBSD: save.c,v 1.5 1998/09/13 00:07:24 hubertf Exp $");
     47  1.2       cgd #endif
     48  1.4     lukem #endif				/* not lint */
     49  1.1       jtc 
     50  1.1       jtc #include <stdio.h>
     51  1.3  christos #include <stdlib.h>
     52  1.1       jtc #include "hdr.h"
     53  1.3  christos #include "extern.h"
     54  1.1       jtc 
     55  1.4     lukem struct savestruct {
     56  1.4     lukem 	void   *address;
     57  1.4     lukem 	int     width;
     58  1.1       jtc };
     59  1.1       jtc 
     60  1.1       jtc struct savestruct save_array[] =
     61  1.1       jtc {
     62  1.4     lukem 	{&abbnum, sizeof(abbnum)},
     63  1.4     lukem 	{&attack, sizeof(attack)},
     64  1.4     lukem 	{&blklin, sizeof(blklin)},
     65  1.4     lukem 	{&bonus, sizeof(bonus)},
     66  1.4     lukem 	{&chloc, sizeof(chloc)},
     67  1.4     lukem 	{&chloc2, sizeof(chloc2)},
     68  1.4     lukem 	{&clock1, sizeof(clock1)},
     69  1.4     lukem 	{&clock2, sizeof(clock2)},
     70  1.4     lukem 	{&closed, sizeof(closed)},
     71  1.4     lukem 	{&closng, sizeof(closng)},
     72  1.4     lukem 	{&daltlc, sizeof(daltlc)},
     73  1.4     lukem 	{&demo, sizeof(demo)},
     74  1.4     lukem 	{&detail, sizeof(detail)},
     75  1.4     lukem 	{&dflag, sizeof(dflag)},
     76  1.4     lukem 	{&dkill, sizeof(dkill)},
     77  1.4     lukem 	{&dtotal, sizeof(dtotal)},
     78  1.4     lukem 	{&foobar, sizeof(foobar)},
     79  1.4     lukem 	{&gaveup, sizeof(gaveup)},
     80  1.4     lukem 	{&holdng, sizeof(holdng)},
     81  1.4     lukem 	{&iwest, sizeof(iwest)},
     82  1.4     lukem 	{&k, sizeof(k)},
     83  1.4     lukem 	{&k2, sizeof(k2)},
     84  1.4     lukem 	{&knfloc, sizeof(knfloc)},
     85  1.4     lukem 	{&kq, sizeof(kq)},
     86  1.4     lukem 	{&latncy, sizeof(latncy)},
     87  1.4     lukem 	{&limit, sizeof(limit)},
     88  1.4     lukem 	{&lmwarn, sizeof(lmwarn)},
     89  1.4     lukem 	{&loc, sizeof(loc)},
     90  1.4     lukem 	{&maxdie, sizeof(maxdie)},
     91  1.4     lukem 	{&mxscor, sizeof(mxscor)},
     92  1.4     lukem 	{&newloc, sizeof(newloc)},
     93  1.4     lukem 	{&numdie, sizeof(numdie)},
     94  1.4     lukem 	{&obj, sizeof(obj)},
     95  1.4     lukem 	{&oldlc2, sizeof(oldlc2)},
     96  1.4     lukem 	{&oldloc, sizeof(oldloc)},
     97  1.4     lukem 	{&panic, sizeof(panic)},
     98  1.4     lukem 	{&saved, sizeof(saved)},
     99  1.4     lukem 	{&savet, sizeof(savet)},
    100  1.4     lukem 	{&scorng, sizeof(scorng)},
    101  1.4     lukem 	{&spk, sizeof(spk)},
    102  1.4     lukem 	{&stick, sizeof(stick)},
    103  1.4     lukem 	{&tally, sizeof(tally)},
    104  1.4     lukem 	{&tally2, sizeof(tally2)},
    105  1.4     lukem 	{&tkk, sizeof(tkk)},
    106  1.4     lukem 	{&turns, sizeof(turns)},
    107  1.4     lukem 	{&verb, sizeof(verb)},
    108  1.4     lukem 	{&wd1, sizeof(wd1)},
    109  1.4     lukem 	{&wd2, sizeof(wd2)},
    110  1.4     lukem 	{&wzdark, sizeof(wzdark)},
    111  1.4     lukem 	{&yea, sizeof(yea)},
    112  1.4     lukem 	{atloc, sizeof(atloc)},
    113  1.4     lukem 	{dloc, sizeof(dloc)},
    114  1.4     lukem 	{dseen, sizeof(dseen)},
    115  1.4     lukem 	{fixed, sizeof(fixed)},
    116  1.4     lukem 	{hinted, sizeof(hinted)},
    117  1.4     lukem 	{links, sizeof(links)},
    118  1.4     lukem 	{odloc, sizeof(odloc)},
    119  1.4     lukem 	{place, sizeof(place)},
    120  1.4     lukem 	{prop, sizeof(prop)},
    121  1.4     lukem 	{tk, sizeof(tk)},
    122  1.1       jtc 
    123  1.4     lukem 	{NULL, 0}
    124  1.1       jtc };
    125  1.1       jtc 
    126  1.3  christos int
    127  1.4     lukem save(outfile)			/* Two passes on data: first to get checksum,
    128  1.4     lukem 				 * second */
    129  1.5   hubertf 	const char   *outfile;	/* to output the data using checksum to start
    130  1.4     lukem 				 * random #s */
    131  1.1       jtc {
    132  1.4     lukem 	FILE   *out;
    133  1.1       jtc 	struct savestruct *p;
    134  1.4     lukem 	char   *s;
    135  1.4     lukem 	long    sum;
    136  1.4     lukem 	int     i;
    137  1.1       jtc 
    138  1.1       jtc 	crc_start();
    139  1.1       jtc 	for (p = save_array; p->address != NULL; p++)
    140  1.1       jtc 		sum = crc(p->address, p->width);
    141  1.1       jtc 	srandom((int) sum);
    142  1.1       jtc 
    143  1.4     lukem 	if ((out = fopen(outfile, "wb")) == NULL) {
    144  1.4     lukem 		fprintf(stderr,
    145  1.4     lukem 		    "Hmm.  The name \"%s\" appears to be magically blocked.\n",
    146  1.4     lukem 		    outfile);
    147  1.4     lukem 		return 1;
    148  1.1       jtc 	}
    149  1.4     lukem 	fwrite(&sum, sizeof(sum), 1, out);	/* Here's the random() key */
    150  1.4     lukem 	for (p = save_array; p->address != NULL; p++) {
    151  1.1       jtc 		for (s = p->address, i = 0; i < p->width; i++, s++)
    152  1.4     lukem 			*s = (*s ^ random()) & 0xFF;	/* Lightly encrypt */
    153  1.1       jtc 		fwrite(p->address, p->width, 1, out);
    154  1.1       jtc 	}
    155  1.1       jtc 	fclose(out);
    156  1.1       jtc 	return 0;
    157  1.1       jtc }
    158  1.1       jtc 
    159  1.3  christos int
    160  1.1       jtc restore(infile)
    161  1.5   hubertf 	const char   *infile;
    162  1.1       jtc {
    163  1.4     lukem 	FILE   *in;
    164  1.1       jtc 	struct savestruct *p;
    165  1.4     lukem 	char   *s;
    166  1.4     lukem 	long    sum, cksum = 0;
    167  1.4     lukem 	int     i;
    168  1.4     lukem 
    169  1.4     lukem 	if ((in = fopen(infile, "rb")) == NULL) {
    170  1.4     lukem 		fprintf(stderr,
    171  1.4     lukem 		    "Hmm.  The file \"%s\" appears to be magically blocked.\n",
    172  1.4     lukem 		    infile);
    173  1.4     lukem 		return 1;
    174  1.1       jtc 	}
    175  1.4     lukem 	fread(&sum, sizeof(sum), 1, in);	/* Get the seed */
    176  1.1       jtc 	srandom((int) sum);
    177  1.4     lukem 	for (p = save_array; p->address != NULL; p++) {
    178  1.1       jtc 		fread(p->address, p->width, 1, in);
    179  1.1       jtc 		for (s = p->address, i = 0; i < p->width; i++, s++)
    180  1.4     lukem 			*s = (*s ^ random()) & 0xFF;	/* Lightly decrypt */
    181  1.1       jtc 	}
    182  1.1       jtc 	fclose(in);
    183  1.1       jtc 
    184  1.4     lukem 	crc_start();		/* See if she cheated */
    185  1.1       jtc 	for (p = save_array; p->address != NULL; p++)
    186  1.1       jtc 		cksum = crc(p->address, p->width);
    187  1.4     lukem 	if (sum != cksum)	/* Tsk tsk */
    188  1.4     lukem 		return 2;	/* Altered the file */
    189  1.1       jtc 	/* We successfully restored, so this really was a save file */
    190  1.1       jtc 	/* Get rid of the file, but don't bother checking that we did */
    191  1.1       jtc 	return 0;
    192  1.1       jtc }
    193