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