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