1 /* quetzal.c - Saving and restoring of Quetzal files.
2 * Written by Martin Frost <mdf@doc.ic.ac.uk>
4 * This file is part of Frotz.
6 * Frotz is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * Frotz is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
22 #include <libchimara/glk.h>
28 typedef unsigned long zlong;
31 * This is used only by save_quetzal. It probably should be allocated
32 * dynamically rather than statically.
35 static zword frames[STACK_SIZE/4+1];
41 #define makeid(a,b,c,d) ((zlong) (((a)<<24) | ((b)<<16) | ((c)<<8) | (d)))
43 #define ID_FORM makeid ('F','O','R','M')
44 #define ID_IFZS makeid ('I','F','Z','S')
45 #define ID_IFhd makeid ('I','F','h','d')
46 #define ID_UMem makeid ('U','M','e','m')
47 #define ID_CMem makeid ('C','M','e','m')
48 #define ID_Stks makeid ('S','t','k','s')
49 #define ID_ANNO makeid ('A','N','N','O')
52 * Various parsing states within restoration.
55 #define GOT_HEADER 0x01
56 #define GOT_STACK 0x02
57 #define GOT_MEMORY 0x04
60 #define GOT_ERROR 0x80
63 * Macros used to write the files.
66 #define write_byte(fp,b) (put_c (b, fp) != EOF)
67 #define write_bytx(fp,b) write_byte (fp, (b) & 0xFF)
68 #define write_word(fp,w) \
69 (write_bytx (fp, (w) >> 8) && write_bytx (fp, (w)))
70 #define write_long(fp,l) \
71 (write_bytx (fp, (l) >> 24) && write_bytx (fp, (l) >> 16) && \
72 write_bytx (fp, (l) >> 8) && write_bytx (fp, (l)))
73 #define write_chnk(fp,id,len) \
74 (write_long (fp, (id)) && write_long (fp, (len)))
75 #define write_run(fp,run) \
76 (write_byte (fp, 0) && write_byte (fp, (run)))
78 /* Read one word from file; return TRUE if OK. */
79 static bool read_word (FILE *f, zword *result)
83 if ((a = get_c (f)) == EOF) return FALSE;
84 if ((b = get_c (f)) == EOF) return FALSE;
86 *result = ((zword) a << 8) | (zword) b;
90 /* Read one long from file; return TRUE if OK. */
91 static bool read_long (FILE *f, zlong *result)
95 if ((a = get_c (f)) == EOF) return FALSE;
96 if ((b = get_c (f)) == EOF) return FALSE;
97 if ((c = get_c (f)) == EOF) return FALSE;
98 if ((d = get_c (f)) == EOF) return FALSE;
100 *result = ((zlong) a << 24) | ((zlong) b << 16) |
101 ((zlong) c << 8) | (zlong) d;
106 * Restore a saved game using Quetzal format. Return 2 if OK, 0 if an error
107 * occurred before any damage was done, -1 on a fatal error.
110 zword restore_quetzal (FILE *svf, FILE *stf, int blorb_ofs)
112 zlong ifzslen, currlen, tmpl;
115 zword fatal = 0; /* Set to -1 when errors must be fatal. */
116 zbyte skip, progress = GOT_NONE;
119 /* Check it's really an `IFZS' file. */
120 if (!read_long (svf, &tmpl)
121 || !read_long (svf, &ifzslen)
122 || !read_long (svf, &currlen)) return 0;
123 if (tmpl != ID_FORM || currlen != ID_IFZS)
125 print_string ("This is not a saved game file!\n");
128 if ((ifzslen & 1) || ifzslen<4) /* Sanity checks. */ return 0;
131 /* Read each chunk and process it. */
134 /* Read chunk header. */
135 if (ifzslen < 8) /* Couldn't contain a chunk. */ return 0;
136 if (!read_long (svf, &tmpl)
137 || !read_long (svf, &currlen)) return 0;
138 ifzslen -= 8; /* Reduce remaining by size of header. */
140 /* Handle chunk body. */
141 if (ifzslen < currlen) /* Chunk goes past EOF?! */ return 0;
143 ifzslen -= currlen + (zlong) skip;
147 /* `IFhd' header chunk; must be first in file. */
149 if (progress & GOT_HEADER)
151 print_string ("Save file has two IFZS chunks!\n");
154 progress |= GOT_HEADER;
156 || !read_word (svf, &tmpw)) return fatal;
157 if (tmpw != h_release)
158 progress = GOT_ERROR;
160 for (i=H_SERIAL; i<H_SERIAL+6; ++i)
162 if ((x = get_c (svf)) == EOF) return fatal;
164 progress = GOT_ERROR;
167 if (!read_word (svf, &tmpw)) return fatal;
168 if (tmpw != h_checksum)
169 progress = GOT_ERROR;
171 if (progress & GOT_ERROR)
173 print_string ("File was not saved from this story!\n");
176 if ((x = get_c (svf)) == EOF) return fatal;
177 pc = (zlong) x << 16;
178 if ((x = get_c (svf)) == EOF) return fatal;
179 pc |= (zlong) x << 8;
180 if ((x = get_c (svf)) == EOF) return fatal;
182 fatal = -1; /* Setting PC means errors must be fatal. */
185 for (i=13; i<currlen; ++i)
186 (void) get_c (svf); /* Skip rest of chunk. */
188 /* `Stks' stacks chunk; restoring this is quite complex. ;) */
190 if (progress & GOT_STACK)
192 print_string ("File contains two stack chunks!\n");
195 progress |= GOT_STACK;
197 fatal = -1; /* Setting SP means errors must be fatal. */
198 sp = stack + STACK_SIZE;
201 * All versions other than V6 may use evaluation stack outside
202 * any function context. As a result a faked function context
203 * will be present in the file here. We skip this context, but
204 * load the associated stack onto the stack proper...
208 if (currlen < 8) return fatal;
210 if (get_c (svf) != 0) return fatal;
211 if (!read_word (svf, &tmpw)) return fatal;
212 if (tmpw > STACK_SIZE)
214 print_string ("Save-file has too much stack (and I can't cope).\n");
218 if (currlen < tmpw*2) return fatal;
219 for (i=0; i<tmpw; ++i)
220 if (!read_word (svf, --sp)) return fatal;
224 /* We now proceed to load the main block of stack frames. */
225 for (fp = stack+STACK_SIZE, frame_count = 0;
227 currlen -= 8, ++frame_count)
229 if (currlen < 8) return fatal;
230 if (sp - stack < 4) /* No space for frame. */
232 print_string ("Save-file has too much stack (and I can't cope).\n");
236 /* Read PC, procedure flag and formal param count. */
237 if (!read_long (svf, &tmpl)) return fatal;
238 y = (int) (tmpl & 0x0F); /* Number of formals. */
241 /* Read result variable. */
242 if ((x = get_c (svf)) == EOF) return fatal;
244 /* Check the procedure flag... */
247 tmpw |= 0x1000; /* It's a procedure. */
248 tmpl >>= 8; /* Shift to get PC value. */
252 /* Functions have type 0, so no need to or anything. */
253 tmpl >>= 8; /* Shift to get PC value. */
254 --tmpl; /* Point at result byte. */
255 /* Sanity check on result variable... */
256 if (zmp[tmpl] != (zbyte) x)
258 print_string ("Save-file has wrong variable number on stack (possibly wrong game version?)\n");
262 *--sp = (zword) (tmpl >> 9); /* High part of PC */
263 *--sp = (zword) (tmpl & 0x1FF); /* Low part of PC */
264 *--sp = (zword) (fp - stack - 1); /* FP */
266 /* Read and process argument mask. */
267 if ((x = get_c (svf)) == EOF) return fatal;
268 ++x; /* Should now be a power of 2 */
272 if (x ^ (1<<i)) /* Not a power of 2 */
274 print_string ("Save-file uses incomplete argument lists (which I can't handle)\n");
278 fp = sp; /* FP for next frame. */
280 /* Read amount of eval stack used. */
281 if (!read_word (svf, &tmpw)) return fatal;
283 tmpw += y; /* Amount of stack + number of locals. */
284 if (sp - stack <= tmpw)
286 print_string ("Save-file has too much stack (and I can't cope).\n");
289 if (currlen < tmpw*2) return fatal;
290 for (i=0; i<tmpw; ++i)
291 if (!read_word (svf, --sp)) return fatal;
294 /* End of `Stks' processing... */
296 /* Any more special chunk types must go in HERE or ABOVE. */
297 /* `CMem' compressed memory chunk; uncompress it. */
299 if (!(progress & GOT_MEMORY)) /* Don't complain if two. */
301 (void) fseek (stf, blorb_ofs, SEEK_SET);
302 i=0; /* Bytes written to data area. */
303 for (; currlen > 0; --currlen)
305 if ((x = get_c (svf)) == EOF) return fatal;
306 if (x == 0) /* Start run. */
308 /* Check for bogus run. */
311 print_string ("File contains bogus `CMem' chunk.\n");
312 for (; currlen > 0; --currlen)
313 (void) get_c (svf); /* Skip rest. */
316 break; /* Keep going; may be a `UMem' too. */
318 /* Copy story file to memory during the run. */
320 if ((x = get_c (svf)) == EOF) return fatal;
321 for (; x >= 0 && i<h_dynamic_size; --x, ++i)
322 if ((y = get_c (stf)) == EOF) return fatal;
326 else /* Not a run. */
328 if ((y = get_c (stf)) == EOF) return fatal;
329 zmp[i] = (zbyte) (x ^ y);
332 /* Make sure we don't load too much. */
333 if (i > h_dynamic_size)
335 print_string ("warning: `CMem' chunk too long!\n");
336 for (; currlen > 1; --currlen)
337 (void) get_c (svf); /* Skip rest. */
338 break; /* Keep going; there may be a `UMem' too. */
341 /* If chunk is short, assume a run. */
342 for (; i<h_dynamic_size; ++i)
343 if ((y = get_c (stf)) == EOF) return fatal;
347 progress |= GOT_MEMORY; /* Only if succeeded. */
350 /* Fall right thru (to default) if already GOT_MEMORY */
351 /* `UMem' uncompressed memory chunk; load it. */
353 if (!(progress & GOT_MEMORY)) /* Don't complain if two. */
355 /* Must be exactly the right size. */
356 if (currlen == h_dynamic_size)
358 if (fread (zmp, currlen, 1, svf) == 1)
360 progress |= GOT_MEMORY; /* Only on success. */
365 print_string ("`UMem' chunk wrong size!\n");
366 /* Fall into default action (skip chunk) on errors. */
368 /* Fall thru (to default) if already GOT_MEMORY */
369 /* Unrecognised chunk type; skip it. */
371 (void) fseek (svf, currlen, SEEK_CUR); /* Skip chunk. */
375 (void) get_c (svf); /* Skip pad byte. */
379 * We've reached the end of the file. For the restoration to have been a
380 * success, we must have had one of each of the required chunks.
382 if (!(progress & GOT_HEADER))
383 print_string ("error: no valid header (`IFhd') chunk in file.\n");
384 if (!(progress & GOT_STACK))
385 print_string ("error: no valid stack (`Stks') chunk in file.\n");
386 if (!(progress & GOT_MEMORY))
387 print_string ("error: no valid memory (`CMem' or `UMem') chunk in file.\n");
389 return (progress == GOT_ALL ? 2 : fatal);
393 * Save a game using Quetzal format. Return 1 if OK, 0 if failed.
396 zword save_quetzal (FILE *svf, FILE *stf, int blorb_ofs)
398 zlong ifzslen = 0, cmemlen = 0, stkslen = 0;
401 zword nvars, nargs, nstk, *p;
403 long cmempos, stkspos;
406 /* Write `IFZS' header. */
407 if (!write_chnk (svf, ID_FORM, 0)) return 0;
408 if (!write_long (svf, ID_IFZS)) return 0;
410 /* Write `IFhd' chunk. */
412 if (!write_chnk (svf, ID_IFhd, 13)) return 0;
413 if (!write_word (svf, h_release)) return 0;
414 for (i=H_SERIAL; i<H_SERIAL+6; ++i)
415 if (!write_byte (svf, zmp[i])) return 0;
416 if (!write_word (svf, h_checksum)) return 0;
417 if (!write_long (svf, pc << 8)) /* Includes pad. */ return 0;
419 /* Write `CMem' chunk. */
420 if ((cmempos = ftell (svf)) < 0) return 0;
421 if (!write_chnk (svf, ID_CMem, 0)) return 0;
422 (void) fseek (stf, blorb_ofs, SEEK_SET);
423 /* j holds current run length. */
424 for (i=0, j=0, cmemlen=0; i < h_dynamic_size; ++i)
426 if ((c = get_c (stf)) == EOF) return 0;
429 ++j; /* It's a run of equal bytes. */
432 /* Write out any run there may be. */
435 for (; j > 0x100; j -= 0x100)
437 if (!write_run (svf, 0xFF)) return 0;
440 if (!write_run (svf, j-1)) return 0;
444 /* Any runs are now written. Write this (nonzero) byte. */
445 if (!write_byte (svf, (zbyte) c)) return 0;
450 * Reached end of dynamic memory. We ignore any unwritten run there may be
453 if (cmemlen & 1) /* Chunk length must be even. */
454 if (!write_byte (svf, 0)) return 0;
456 /* Write `Stks' chunk. You are not expected to understand this. ;) */
457 if ((stkspos = ftell (svf)) < 0) return 0;
458 if (!write_chnk (svf, ID_Stks, 0)) return 0;
461 * We construct a list of frame indices, most recent first, in `frames'.
462 * These indices are the offsets into the `stack' array of the word before
463 * the first word pushed in each frame.
465 frames[0] = sp - stack; /* The frame we'd get by doing a call now. */
466 for (i = fp - stack + 4, n=0; i < STACK_SIZE+4; i = stack[i-3] + 5)
470 * All versions other than V6 can use evaluation stack outside a function
471 * context. We write a faked stack frame (most fields zero) to cater for
477 if (!write_byte (svf, 0)) return 0;
478 nstk = STACK_SIZE - frames[n];
479 if (!write_word (svf, nstk)) return 0;
480 for (j=STACK_SIZE-1; j >= frames[n]; --j)
481 if (!write_word (svf, stack[j])) return 0;
482 stkslen = 8 + 2*nstk;
485 /* Write out the rest of the stack frames. */
488 p = stack + frames[i] - 4; /* Points to call frame. */
489 nvars = (p[0] & 0x0F00) >> 8;
490 nargs = p[0] & 0x00FF;
491 nstk = frames[i] - frames[i-1] - nvars - 4;
492 pc = ((zlong) p[3] << 9) | p[2];
494 switch (p[0] & 0xF000) /* Check type of call. */
496 case 0x0000: /* Function. */
498 pc = ((pc + 1) << 8) | nvars;
500 case 0x1000: /* Procedure. */
502 pc = (pc << 8) | 0x10 | nvars; /* Set procedure flag. */
506 runtime_error (ERR_SAVE_IN_INTER);
510 nargs = (1 << nargs) - 1; /* Make args into bitmap. */
512 /* Write the main part of the frame... */
513 if (!write_long (svf, pc)
514 || !write_byte (svf, var)
515 || !write_byte (svf, nargs)
516 || !write_word (svf, nstk)) return 0;
518 /* Write the variables and eval stack. */
519 for (j=0, --p; j<nvars+nstk; ++j, --p)
520 if (!write_word (svf, *p)) return 0;
522 /* Calculate length written thus far. */
523 stkslen += 8 + 2 * (nvars + nstk);
526 /* Fill in variable chunk lengths. */
527 ifzslen = 3*8 + 4 + 14 + cmemlen + stkslen;
530 (void) fseek (svf, 4, SEEK_SET);
531 if (!write_long (svf, ifzslen)) return 0;
532 (void) fseek (svf, cmempos+4, SEEK_SET);
533 if (!write_long (svf, cmemlen)) return 0;
534 (void) fseek (svf, stkspos+4, SEEK_SET);
535 if (!write_long (svf, stkslen)) return 0;
537 /* After all that, still nothing went wrong! */