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