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