Debian Codegraph @

terps/glulxe/serial.c

raw symbols SCIP
1 /* serial.c: Glulxe code for saving and restoring the VM state.
2 Designed by Andrew Plotkin <[email protected]>
3 http://eblong.com/zarf/glulx/index.html
4 */
5
6 #include <string.h>
7 #include "glk.h"
8 #include "glulxe.h"
9
10 /* This structure allows us to write either to a Glk stream or to
11 a dynamically-allocated memory chunk. */
12 typedef struct dest_struct {
13 int ismem;
14
15 /* If it's a Glk stream: */
16 strid_t str;
17
18 /* If it's a block of memory: */
19 unsigned char *ptr;
20 glui32 pos;
21 glui32 size;
22 } dest_t;
23
24 #define IFFID(c1, c2, c3, c4) \
25 ( (((glui32)c1) << 24) \
26 | (((glui32)c2) << 16) \
27 | (((glui32)c3) << 8) \
28 | (((glui32)c4)) )
29
30 /* This can be adjusted before startup by platform-specific startup
31 code -- that is, preference code. */
32 int max_undo_level = 8;
33
34 static int undo_chain_size = 0;
35 static int undo_chain_num = 0;
36 static unsigned char **undo_chain = NULL;
37
38 #ifdef SERIALIZE_CACHE_RAM
39 /* This will contain a copy of RAM (ramstate to endmem) as it exists
40 in the game file. */
41 static unsigned char *ramcache = NULL;
42 #endif /* SERIALIZE_CACHE_RAM */
43
44 static glui32 write_memstate(dest_t *dest);
45 static glui32 write_heapstate(dest_t *dest, int portable);
46 static glui32 write_stackstate(dest_t *dest, int portable);
47 static glui32 read_memstate(dest_t *dest, glui32 chunklen);
48 static glui32 read_heapstate(dest_t *dest, glui32 chunklen, int portable,
49 glui32 *sumlen, glui32 **summary);
50 static glui32 read_stackstate(dest_t *dest, glui32 chunklen, int portable);
51 static glui32 write_heapstate_sub(glui32 sumlen, glui32 *sumarray,
52 dest_t *dest, int portable);
53 static int sort_heap_summary(void *p1, void *p2);
54 static int write_long(dest_t *dest, glui32 val);
55 static int read_long(dest_t *dest, glui32 *val);
56 static int write_byte(dest_t *dest, unsigned char val);
57 static int read_byte(dest_t *dest, unsigned char *val);
58 static int reposition_write(dest_t *dest, glui32 pos);
59
60 /* init_serial():
61 Set up the undo chain and anything else that needs to be set up.
62 */
63 int init_serial()
64 {
65 undo_chain_num = 0;
66 undo_chain_size = 0;
67 undo_chain = NULL;
68 if (max_undo_level > 0) {
69 undo_chain_size = max_undo_level;
70 undo_chain = (unsigned char **)glulx_malloc(sizeof(unsigned char *) * undo_chain_size);
71 if (!undo_chain)
72 return FALSE;
73 }
74
75 #ifdef SERIALIZE_CACHE_RAM
76 {
77 glui32 len = (endmem - ramstart);
78 glui32 res;
79 ramcache = (unsigned char *)glulx_malloc(sizeof(unsigned char *) * len);
80 if (!ramcache)
81 return FALSE;
82 glk_stream_set_position(gamefile, gamefile_start+ramstart, seekmode_Start);
83 res = glk_get_buffer_stream(gamefile, (char *)ramcache, len);
84 if (res != len)
85 return FALSE;
86 }
87 #endif /* SERIALIZE_CACHE_RAM */
88
89 return TRUE;
90 }
91
92 /* final_serial():
93 Clean up memory when the VM shuts down.
94 */
95 void final_serial()
96 {
97 if (undo_chain) {
98 int ix;
99 for (ix=0; ix<undo_chain_num; ix++) {
100 glulx_free(undo_chain[ix]);
101 }
102 glulx_free(undo_chain);
103 }
104 undo_chain = NULL;
105 undo_chain_size = 0;
106 undo_chain_num = 0;
107
108 #ifdef SERIALIZE_CACHE_RAM
109 if (ramcache) {
110 glulx_free(ramcache);
111 ramcache = NULL;
112 }
113 #endif /* SERIALIZE_CACHE_RAM */
114 }
115
116 /* perform_saveundo():
117 Add a state pointer to the undo chain. This returns 0 on success,
118 1 on failure.
119 */
120 glui32 perform_saveundo()
121 {
122 dest_t dest;
123 glui32 res;
124 glui32 memstart=0, memlen=0, heapstart=0, heaplen=0, stackstart=0, stacklen=0;
125
126 /* The format for undo-saves is simpler than for saves on disk. We
127 just have a memory chunk, a heap chunk, and a stack chunk, in
128 that order. We skip the IFF chunk headers (although the size
129 fields are still there.) We also don't bother with IFF's 16-bit
130 alignment. */
131
132 if (undo_chain_size == 0)
133 return 1;
134
135 dest.ismem = TRUE;
136 dest.size = 0;
137 dest.pos = 0;
138 dest.ptr = NULL;
139 dest.str = NULL;
140
141 res = 0;
142 if (res == 0) {
143 res = write_long(&dest, 0); /* space for chunk length */
144 }
145 if (res == 0) {
146 memstart = dest.pos;
147 res = write_memstate(&dest);
148 memlen = dest.pos - memstart;
149 }
150 if (res == 0) {
151 res = write_long(&dest, 0); /* space for chunk length */
152 }
153 if (res == 0) {
154 heapstart = dest.pos;
155 res = write_heapstate(&dest, FALSE);
156 heaplen = dest.pos - heapstart;
157 }
158 if (res == 0) {
159 res = write_long(&dest, 0); /* space for chunk length */
160 }
161 if (res == 0) {
162 stackstart = dest.pos;
163 res = write_stackstate(&dest, FALSE);
164 stacklen = dest.pos - stackstart;
165 }
166
167 if (res == 0) {
168 /* Trim it down to the perfect size. */
169 dest.ptr = glulx_realloc(dest.ptr, dest.pos);
170 if (!dest.ptr)
171 res = 1;
172 }
173 if (res == 0) {
174 res = reposition_write(&dest, memstart-4);
175 }
176 if (res == 0) {
177 res = write_long(&dest, memlen);
178 }
179 if (res == 0) {
180 res = reposition_write(&dest, heapstart-4);
181 }
182 if (res == 0) {
183 res = write_long(&dest, heaplen);
184 }
185 if (res == 0) {
186 res = reposition_write(&dest, stackstart-4);
187 }
188 if (res == 0) {
189 res = write_long(&dest, stacklen);
190 }
191
192 if (res == 0) {
193 /* It worked. */
194 if (undo_chain_num >= undo_chain_size) {
195 glulx_free(undo_chain[undo_chain_num-1]);
196 undo_chain[undo_chain_num-1] = NULL;
197 }
198 if (undo_chain_size > 1)
199 memmove(undo_chain+1, undo_chain,
200 (undo_chain_size-1) * sizeof(unsigned char *));
201 undo_chain[0] = dest.ptr;
202 if (undo_chain_num < undo_chain_size)
203 undo_chain_num += 1;
204 dest.ptr = NULL;
205 }
206 else {
207 /* It didn't work. */
208 if (dest.ptr) {
209 glulx_free(dest.ptr);
210 dest.ptr = NULL;
211 }
212 }
213
214 return res;
215 }
216
217 /* perform_restoreundo():
218 Pull a state pointer from the undo chain. This returns 0 on success,
219 1 on failure. Note that if it succeeds, the frameptr, localsbase,
220 and valstackbase registers are invalid; they must be rebuilt from
221 the stack.
222 */
223 glui32 perform_restoreundo()
224 {
225 dest_t dest;
226 glui32 res, val;
227 glui32 heapsumlen = 0;
228 glui32 *heapsumarr = NULL;
229
230 /* If profiling is enabled and active then fail. */
231 #if VM_PROFILING
232 if (profile_profiling_active())
233 return 1;
234 #endif /* VM_PROFILING */
235
236 if (undo_chain_size == 0 || undo_chain_num == 0)
237 return 1;
238
239 dest.ismem = TRUE;
240 dest.size = 0;
241 dest.pos = 0;
242 dest.ptr = undo_chain[0];
243 dest.str = NULL;
244
245 val = 0;
246 res = 0;
247 if (res == 0) {
248 res = read_long(&dest, &val);
249 }
250 if (res == 0) {
251 res = read_memstate(&dest, val);
252 }
253 if (res == 0) {
254 res = read_long(&dest, &val);
255 }
256 if (res == 0) {
257 res = read_heapstate(&dest, val, FALSE, &heapsumlen, &heapsumarr);
258 }
259 if (res == 0) {
260 res = read_long(&dest, &val);
261 }
262 if (res == 0) {
263 res = read_stackstate(&dest, val, FALSE);
264 }
265 /* ### really, many of the failure modes of those calls ought to
266 cause fatal errors. The stack or main memory may be damaged now. */
267
268 if (res == 0) {
269 if (heapsumarr)
270 res = heap_apply_summary(heapsumlen, heapsumarr);
271 }
272
273 if (res == 0) {
274 /* It worked. */
275 if (undo_chain_size > 1)
276 memmove(undo_chain, undo_chain+1,
277 (undo_chain_size-1) * sizeof(unsigned char *));
278 undo_chain_num -= 1;
279 glulx_free(dest.ptr);
280 dest.ptr = NULL;
281 }
282 else {
283 /* It didn't work. */
284 dest.ptr = NULL;
285 }
286
287 if (heapsumarr)
288 glulx_free(heapsumarr);
289
290 return res;
291 }
292
293 /* has_undo():
294 Return 0 if an undo state is available, 1 if not.
295 */
296 glui32 has_undo()
297 {
298 if (undo_chain_size == 0 || undo_chain_num == 0)
299 return 1;
300 return 0;
301 }
302
303 /* discard_undo():
304 Drop the most recent undo state, if there are any.
305 */
306 void discard_undo()
307 {
308 if (undo_chain_size == 0 || undo_chain_num == 0)
309 return;
310
311 unsigned char *destptr = undo_chain[0];
312
313 if (undo_chain_size > 1)
314 memmove(undo_chain, undo_chain+1,
315 (undo_chain_size-1) * sizeof(unsigned char *));
316 undo_chain_num -= 1;
317 glulx_free(destptr);
318 }
319
320 /* perform_save():
321 Write the state to the output stream. This returns 0 on success,
322 1 on failure.
323 */
324 glui32 perform_save(strid_t str)
325 {
326 dest_t dest;
327 int ix;
328 glui32 res, lx, val;
329 glui32 memstart=0, memlen=0, stackstart=0, stacklen=0, heapstart=0, heaplen=0;
330 glui32 filestart = 0, filelen = 0;
331
332 stream_get_iosys(&val, &lx);
333 if (val != 2) {
334 /* Not using the Glk I/O system, so bail. This function only
335 knows how to write to a Glk stream. */
336 fatal_error("Streams are only available in Glk I/O system.");
337 }
338
339 if (str == 0)
340 return 1;
341
342 dest.ismem = FALSE;
343 dest.size = 0;
344 dest.pos = 0;
345 dest.ptr = NULL;
346 dest.str = str;
347
348 res = 0;
349
350 /* Quetzal header. */
351 if (res == 0) {
352 res = write_long(&dest, IFFID('F', 'O', 'R', 'M'));
353 }
354 if (res == 0) {
355 res = write_long(&dest, 0); /* space for file length */
356 filestart = dest.pos;
357 }
358
359 if (res == 0) {
360 res = write_long(&dest, IFFID('I', 'F', 'Z', 'S')); /* ### ? */
361 }
362
363 /* Header chunk. This is the first 128 bytes of memory. */
364 if (res == 0) {
365 res = write_long(&dest, IFFID('I', 'F', 'h', 'd'));
366 }
367 if (res == 0) {
368 res = write_long(&dest, 128);
369 }
370 for (ix=0; res==0 && ix<128; ix++) {
371 res = write_byte(&dest, Mem1(ix));
372 }
373 /* Always even, so no padding necessary. */
374
375 /* Memory chunk. */
376 if (res == 0) {
377 res = write_long(&dest, IFFID('C', 'M', 'e', 'm'));
378 }
379 if (res == 0) {
380 res = write_long(&dest, 0); /* space for chunk length */
381 }
382 if (res == 0) {
383 memstart = dest.pos;
384 res = write_memstate(&dest);
385 memlen = dest.pos - memstart;
386 }
387 if (res == 0 && (memlen & 1) != 0) {
388 res = write_byte(&dest, 0);
389 }
390
391 /* Heap chunk. */
392 if (res == 0) {
393 res = write_long(&dest, IFFID('M', 'A', 'l', 'l'));
394 }
395 if (res == 0) {
396 res = write_long(&dest, 0); /* space for chunk length */
397 }
398 if (res == 0) {
399 heapstart = dest.pos;
400 res = write_heapstate(&dest, TRUE);
401 heaplen = dest.pos - heapstart;
402 }
403 /* Always even, so no padding necessary. */
404
405 /* Stack chunk. */
406 if (res == 0) {
407 res = write_long(&dest, IFFID('S', 't', 'k', 's'));
408 }
409 if (res == 0) {
410 res = write_long(&dest, 0); /* space for chunk length */
411 }
412 if (res == 0) {
413 stackstart = dest.pos;
414 res = write_stackstate(&dest, TRUE);
415 stacklen = dest.pos - stackstart;
416 }
417 if (res == 0 && (stacklen & 1) != 0) {
418 res = write_byte(&dest, 0);
419 }
420
421 filelen = dest.pos - filestart;
422
423 /* Okay, fill in all the lengths. */
424 if (res == 0) {
425 res = reposition_write(&dest, memstart-4);
426 }
427 if (res == 0) {
428 res = write_long(&dest, memlen);
429 }
430 if (res == 0) {
431 res = reposition_write(&dest, heapstart-4);
432 }
433 if (res == 0) {
434 res = write_long(&dest, heaplen);
435 }
436 if (res == 0) {
437 res = reposition_write(&dest, stackstart-4);
438 }
439 if (res == 0) {
440 res = write_long(&dest, stacklen);
441 }
442 if (res == 0) {
443 res = reposition_write(&dest, filestart-4);
444 }
445 if (res == 0) {
446 res = write_long(&dest, filelen);
447 }
448
449 /* All done. */
450
451 return res;
452 }
453
454 /* perform_restore():
455 Pull a state pointer from a stream. This returns 0 on success,
456 1 on failure. Note that if it succeeds, the frameptr, localsbase,
457 and valstackbase registers are invalid; they must be rebuilt from
458 the stack.
459
460 If fromshell is true, the restore is being invoked by the library
461 shell (an autorestore of some kind). This currently happens only in
462 iosglk and remglk.
463 */
464 glui32 perform_restore(strid_t str, int fromshell)
465 {
466 dest_t dest;
467 int ix;
468 glui32 lx, res, val;
469 glui32 filestart = 0, filelen = 0;
470 glui32 heapsumlen = 0;
471 glui32 *heapsumarr = NULL;
472
473 /* If profiling is enabled and active then fail. */
474 #if VM_PROFILING
475 if (profile_profiling_active())
476 return 1;
477 #endif /* VM_PROFILING */
478
479 stream_get_iosys(&val, &lx);
480 if (val != 2 && !fromshell) {
481 /* Not using the Glk I/O system, so bail. This function only
482 knows how to read from a Glk stream. (But in the autorestore
483 case, iosys hasn't been set yet, so ignore this test.) */
484 fatal_error("Streams are only available in Glk I/O system.");
485 }
486
487 if (str == 0)
488 return 1;
489
490 dest.ismem = FALSE;
491 dest.size = 0;
492 dest.pos = 0;
493 dest.ptr = NULL;
494 dest.str = str;
495
496 res = 0;
497
498 /* ### the format errors checked below should send error messages to
499 the current stream. */
500
501 if (res == 0) {
502 res = read_long(&dest, &val);
503 }
504 if (res == 0 && val != IFFID('F', 'O', 'R', 'M')) {
505 /* ### bad header */
506 return 1;
507 }
508 if (res == 0) {
509 res = read_long(&dest, &filelen);
510 }
511 filestart = dest.pos;
512
513 if (res == 0) {
514 res = read_long(&dest, &val);
515 }
516 if (res == 0 && val != IFFID('I', 'F', 'Z', 'S')) { /* ### ? */
517 /* ### bad header */
518 return 1;
519 }
520
521 while (res == 0 && dest.pos < filestart+filelen) {
522 /* Read a chunk and deal with it. */
523 glui32 chunktype=0, chunkstart=0, chunklen=0;
524 unsigned char dummy;
525
526 if (res == 0) {
527 res = read_long(&dest, &chunktype);
528 }
529 if (res == 0) {
530 res = read_long(&dest, &chunklen);
531 }
532 chunkstart = dest.pos;
533
534 if (chunktype == IFFID('I', 'F', 'h', 'd')) {
535 for (ix=0; res==0 && ix<128; ix++) {
536 res = read_byte(&dest, &dummy);
537 if (res == 0 && Mem1(ix) != dummy) {
538 /* ### non-matching header */
539 return 1;
540 }
541 }
542 }
543 else if (chunktype == IFFID('C', 'M', 'e', 'm')) {
544 res = read_memstate(&dest, chunklen);
545 }
546 else if (chunktype == IFFID('M', 'A', 'l', 'l')) {
547 res = read_heapstate(&dest, chunklen, TRUE, &heapsumlen, &heapsumarr);
548 }
549 else if (chunktype == IFFID('S', 't', 'k', 's')) {
550 res = read_stackstate(&dest, chunklen, TRUE);
551 }
552 else {
553 /* Unknown chunk type. Skip it. */
554 for (lx=0; res==0 && lx<chunklen; lx++) {
555 res = read_byte(&dest, &dummy);
556 }
557 }
558
559 if (chunkstart+chunklen != dest.pos) {
560 /* ### funny chunk length */
561 return 1;
562 }
563
564 if ((chunklen & 1) != 0) {
565 if (res == 0) {
566 res = read_byte(&dest, &dummy);
567 }
568 }
569 }
570
571 if (res == 0) {
572 if (heapsumarr) {
573 /* The summary might have come from any interpreter, so it could
574 be out of order. We'll sort it. */
575 glulx_sort(heapsumarr+2, (heapsumlen-2)/2, 2*sizeof(glui32),
576 &sort_heap_summary);
577 res = heap_apply_summary(heapsumlen, heapsumarr);
578 }
579 }
580
581 if (heapsumarr)
582 glulx_free(heapsumarr);
583
584 if (res)
585 return 1;
586
587 return 0;
588 }
589
590 static int reposition_write(dest_t *dest, glui32 pos)
591 {
592 if (dest->ismem) {
593 dest->pos = pos;
594 }
595 else {
596 glk_stream_set_position(dest->str, pos, seekmode_Start);
597 dest->pos = pos;
598 }
599
600 return 0;
601 }
602
603 static int write_buffer(dest_t *dest, unsigned char *ptr, glui32 len)
604 {
605 if (dest->ismem) {
606 if (dest->pos+len > dest->size) {
607 dest->size = dest->pos+len+1024;
608 if (!dest->ptr) {
609 dest->ptr = glulx_malloc(dest->size);
610 }
611 else {
612 dest->ptr = glulx_realloc(dest->ptr, dest->size);
613 }
614 if (!dest->ptr)
615 return 1;
616 }
617 memcpy(dest->ptr+dest->pos, ptr, len);
618 }
619 else {
620 glk_put_buffer_stream(dest->str, (char *)ptr, len);
621 }
622
623 dest->pos += len;
624
625 return 0;
626 }
627
628 static int read_buffer(dest_t *dest, unsigned char *ptr, glui32 len)
629 {
630 glui32 newlen;
631
632 if (dest->ismem) {
633 memcpy(ptr, dest->ptr+dest->pos, len);
634 }
635 else {
636 newlen = glk_get_buffer_stream(dest->str, (char *)ptr, len);
637 if (newlen != len)
638 return 1;
639 }
640
641 dest->pos += len;
642
643 return 0;
644 }
645
646 static int write_long(dest_t *dest, glui32 val)
647 {
648 unsigned char buf[4];
649 Write4(buf, val);
650 return write_buffer(dest, buf, 4);
651 }
652
653 static int write_short(dest_t *dest, glui16 val)
654 {
655 unsigned char buf[2];
656 Write2(buf, val);
657 return write_buffer(dest, buf, 2);
658 }
659
660 static int write_byte(dest_t *dest, unsigned char val)
661 {
662 return write_buffer(dest, &val, 1);
663 }
664
665 static int read_long(dest_t *dest, glui32 *val)
666 {
667 unsigned char buf[4];
668 int res = read_buffer(dest, buf, 4);
669 if (res)
670 return res;
671 *val = Read4(buf);
672 return 0;
673 }
674
675 static int read_short(dest_t *dest, glui16 *val)
676 {
677 unsigned char buf[2];
678 int res = read_buffer(dest, buf, 2);
679 if (res)
680 return res;
681 *val = Read2(buf);
682 return 0;
683 }
684
685 static int read_byte(dest_t *dest, unsigned char *val)
686 {
687 return read_buffer(dest, val, 1);
688 }
689
690 static glui32 write_memstate(dest_t *dest)
691 {
692 glui32 res, pos;
693 int val;
694 int runlen;
695 unsigned char ch;
696 #ifdef SERIALIZE_CACHE_RAM
697 glui32 cachepos;
698 #endif /* SERIALIZE_CACHE_RAM */
699
700 res = write_long(dest, endmem);
701 if (res)
702 return res;
703
704 runlen = 0;
705
706 #ifdef SERIALIZE_CACHE_RAM
707 cachepos = 0;
708 #else /* SERIALIZE_CACHE_RAM */
709 glk_stream_set_position(gamefile, gamefile_start+ramstart, seekmode_Start);
710 #endif /* SERIALIZE_CACHE_RAM */
711
712 for (pos=ramstart; pos<endmem; pos++) {
713 ch = Mem1(pos);
714 if (pos < endgamefile) {
715 #ifdef SERIALIZE_CACHE_RAM
716 val = ramcache[cachepos];
717 cachepos++;
718 #else /* SERIALIZE_CACHE_RAM */
719 val = glk_get_char_stream(gamefile);
720 if (val == -1) {
721 fatal_error("The game file ended unexpectedly while saving.");
722 }
723 #endif /* SERIALIZE_CACHE_RAM */
724 ch ^= (unsigned char)val;
725 }
726 if (ch == 0) {
727 runlen++;
728 }
729 else {
730 /* Write any run we've got. */
731 while (runlen) {
732 if (runlen >= 0x100)
733 val = 0x100;
734 else
735 val = runlen;
736 res = write_byte(dest, 0);
737 if (res)
738 return res;
739 res = write_byte(dest, (val-1));
740 if (res)
741 return res;
742 runlen -= val;
743 }
744 /* Write the byte we got. */
745 res = write_byte(dest, ch);
746 if (res)
747 return res;
748 }
749 }
750 /* It's possible we've got a run left over, but we don't write it. */
751
752 return 0;
753 }
754
755 static glui32 read_memstate(dest_t *dest, glui32 chunklen)
756 {
757 glui32 chunkend = dest->pos + chunklen;
758 glui32 newlen;
759 glui32 res, pos;
760 int val;
761 int runlen;
762 unsigned char ch, ch2;
763 #ifdef SERIALIZE_CACHE_RAM
764 glui32 cachepos;
765 #endif /* SERIALIZE_CACHE_RAM */
766
767 heap_clear();
768
769 res = read_long(dest, &newlen);
770 if (res)
771 return res;
772
773 res = change_memsize(newlen, FALSE);
774 if (res)
775 return res;
776
777 runlen = 0;
778
779 #ifdef SERIALIZE_CACHE_RAM
780 cachepos = 0;
781 #else /* SERIALIZE_CACHE_RAM */
782 glk_stream_set_position(gamefile, gamefile_start+ramstart, seekmode_Start);
783 #endif /* SERIALIZE_CACHE_RAM */
784
785 for (pos=ramstart; pos<endmem; pos++) {
786 if (pos < endgamefile) {
787 #ifdef SERIALIZE_CACHE_RAM
788 val = ramcache[cachepos];
789 cachepos++;
790 #else /* SERIALIZE_CACHE_RAM */
791 val = glk_get_char_stream(gamefile);
792 if (val == -1) {
793 fatal_error("The game file ended unexpectedly while restoring.");
794 }
795 #endif /* SERIALIZE_CACHE_RAM */
796 ch = (unsigned char)val;
797 }
798 else {
799 ch = 0;
800 }
801
802 if (dest->pos >= chunkend) {
803 /* we're into the final, unstored run. */
804 }
805 else if (runlen) {
806 runlen--;
807 }
808 else {
809 res = read_byte(dest, &ch2);
810 if (res)
811 return res;
812 if (ch2 == 0) {
813 res = read_byte(dest, &ch2);
814 if (res)
815 return res;
816 runlen = (glui32)ch2;
817 }
818 else {
819 ch ^= ch2;
820 }
821 }
822
823 if (pos >= protectstart && pos < protectend)
824 continue;
825
826 MemW1(pos, ch);
827 }
828
829 return 0;
830 }
831
832 static glui32 write_heapstate(dest_t *dest, int portable)
833 {
834 glui32 res;
835 glui32 sumlen;
836 glui32 *sumarray;
837
838 res = heap_get_summary(&sumlen, &sumarray);
839 if (res)
840 return res;
841
842 if (!sumarray)
843 return 0; /* no heap */
844
845 res = write_heapstate_sub(sumlen, sumarray, dest, portable);
846
847 glulx_free(sumarray);
848 return res;
849 }
850
851 static glui32 write_heapstate_sub(glui32 sumlen, glui32 *sumarray,
852 dest_t *dest, int portable)
853 {
854 glui32 res, lx;
855
856 /* If we're storing for the purpose of undo, we don't need to do any
857 byte-swapping, because the result will only be used by this session. */
858 if (!portable) {
859 res = write_buffer(dest, (void *)sumarray, sumlen*sizeof(glui32));
860 if (res)
861 return res;
862 return 0;
863 }
864
865 for (lx=0; lx<sumlen; lx++) {
866 res = write_long(dest, sumarray[lx]);
867 if (res)
868 return res;
869 }
870
871 return 0;
872 }
873
874 static int sort_heap_summary(void *p1, void *p2)
875 {
876 glui32 v1 = *(glui32 *)p1;
877 glui32 v2 = *(glui32 *)p2;
878
879 if (v1 < v2)
880 return -1;
881 if (v1 > v2)
882 return 1;
883 return 0;
884 }
885
886 static glui32 read_heapstate(dest_t *dest, glui32 chunklen, int portable,
887 glui32 *sumlen, glui32 **summary)
888 {
889 glui32 res, count, lx;
890 glui32 *arr;
891
892 *sumlen = 0;
893 *summary = NULL;
894
895 if (chunklen == 0)
896 return 0; /* no heap */
897
898 if (!portable) {
899 count = chunklen / sizeof(glui32);
900
901 arr = glulx_malloc(chunklen);
902 if (!arr)
903 return 1;
904
905 res = read_buffer(dest, (void *)arr, chunklen);
906 if (res)
907 return res;
908
909 *sumlen = count;
910 *summary = arr;
911
912 return 0;
913 }
914
915 count = chunklen / 4;
916
917 arr = glulx_malloc(count * sizeof(glui32));
918 if (!arr)
919 return 1;
920
921 for (lx=0; lx<count; lx++) {
922 res = read_long(dest, arr+lx);
923 if (res)
924 return res;
925 }
926
927 *sumlen = count;
928 *summary = arr;
929
930 return 0;
931 }
932
933 static glui32 write_stackstate(dest_t *dest, int portable)
934 {
935 glui32 res;
936 glui32 lx;
937 glui32 lastframe;
938
939 /* If we're storing for the purpose of undo, we don't need to do any
940 byte-swapping, because the result will only be used by this session. */
941 if (!portable) {
942 res = write_buffer(dest, stack, stackptr);
943 if (res)
944 return res;
945 return 0;
946 }
947
948 /* Write a portable stack image. To do this, we have to write stack
949 frames in order, bottom to top. Remember that the last word of
950 every stack frame is a pointer to the beginning of that stack frame.
951 (This includes the last frame, because the save opcode pushes on
952 a call stub before it calls perform_save().) */
953
954 lastframe = (glui32)(-1);
955 while (1) {
956 glui32 frameend, frm, frm2, frm3;
957 unsigned char loctype, loccount;
958 glui32 numlocals, frlen, locpos;
959
960 /* Find the next stack frame (after the one in lastframe). Sadly,
961 this requires searching the stack from the top down. We have to
962 do this for *every* frame, which takes N^2 time overall. But
963 save routines usually aren't nested very deep.
964 If it becomes a practical problem, we can build a stack-frame
965 array, which requires dynamic allocation. */
966 for (frm = stackptr, frameend = stackptr;
967 frm != 0 && (frm2 = Stk4(frm-4)) != lastframe;
968 frameend = frm, frm = frm2) { };
969
970 /* Write out the frame. */
971 frm2 = frm;
972
973 frlen = Stk4(frm2);
974 frm2 += 4;
975 res = write_long(dest, frlen);
976 if (res)
977 return res;
978 locpos = Stk4(frm2);
979 frm2 += 4;
980 res = write_long(dest, locpos);
981 if (res)
982 return res;
983
984 frm3 = frm2;
985
986 numlocals = 0;
987 while (1) {
988 loctype = Stk1(frm2);
989 frm2 += 1;
990 loccount = Stk1(frm2);
991 frm2 += 1;
992
993 res = write_byte(dest, loctype);
994 if (res)
995 return res;
996 res = write_byte(dest, loccount);
997 if (res)
998 return res;
999
1000 if (loctype == 0 && loccount == 0)
1001 break;
1002
1003 numlocals++;
1004 }
1005
1006 if ((numlocals & 1) == 0) {
1007 res = write_byte(dest, 0);
1008 if (res)
1009 return res;
1010 res = write_byte(dest, 0);
1011 if (res)
1012 return res;
1013 frm2 += 2;
1014 }
1015
1016 if (frm2 != frm+locpos)
1017 fatal_error("Inconsistent stack frame during save.");
1018
1019 /* Write out the locals. */
1020 for (lx=0; lx<numlocals; lx++) {
1021 loctype = Stk1(frm3);
1022 frm3 += 1;
1023 loccount = Stk1(frm3);
1024 frm3 += 1;
1025
1026 if (loctype == 0 && loccount == 0)
1027 break;
1028
1029 /* Put in up to 0, 1, or 3 bytes of padding, depending on loctype. */
1030 while (frm2 & (loctype-1)) {
1031 res = write_byte(dest, 0);
1032 if (res)
1033 return res;
1034 frm2 += 1;
1035 }
1036
1037 /* Put in this set of locals. */
1038 switch (loctype) {
1039
1040 case 1:
1041 do {
1042 res = write_byte(dest, Stk1(frm2));
1043 if (res)
1044 return res;
1045 frm2 += 1;
1046 loccount--;
1047 } while (loccount);
1048 break;
1049
1050 case 2:
1051 do {
1052 res = write_short(dest, Stk2(frm2));
1053 if (res)
1054 return res;
1055 frm2 += 2;
1056 loccount--;
1057 } while (loccount);
1058 break;
1059
1060 case 4:
1061 do {
1062 res = write_long(dest, Stk4(frm2));
1063 if (res)
1064 return res;
1065 frm2 += 4;
1066 loccount--;
1067 } while (loccount);
1068 break;
1069
1070 }
1071 }
1072
1073 if (frm2 != frm+frlen)
1074 fatal_error("Inconsistent stack frame during save.");
1075
1076 while (frm2 < frameend) {
1077 res = write_long(dest, Stk4(frm2));
1078 if (res)
1079 return res;
1080 frm2 += 4;
1081 }
1082
1083 /* Go on to the next frame. */
1084 if (frameend == stackptr)
1085 break; /* All done. */
1086 lastframe = frm;
1087 }
1088
1089 return 0;
1090 }
1091
1092 static glui32 read_stackstate(dest_t *dest, glui32 chunklen, int portable)
1093 {
1094 glui32 res;
1095 glui32 frameend, frm, frm2, frm3, locpos, frlen, numlocals;
1096
1097 if (chunklen > stacksize)
1098 return 1;
1099
1100 stackptr = chunklen;
1101 frameptr = 0;
1102 valstackbase = 0;
1103 localsbase = 0;
1104
1105 if (!portable) {
1106 res = read_buffer(dest, stack, stackptr);
1107 if (res)
1108 return res;
1109 return 0;
1110 }
1111
1112 /* This isn't going to be pleasant; we're going to read the data in
1113 as a block, and then convert it in-place. */
1114 res = read_buffer(dest, stack, stackptr);
1115 if (res)
1116 return res;
1117
1118 frameend = stackptr;
1119 while (frameend != 0) {
1120 /* Read the beginning-of-frame pointer. Remember, right now, the
1121 whole frame is stored big-endian. So we have to read with the
1122 Read*() macros, and then write with the StkW*() macros. */
1123 frm = Read4(stack+(frameend-4));
1124
1125 frm2 = frm;
1126
1127 frlen = Read4(stack+frm2);
1128 StkW4(frm2, frlen);
1129 frm2 += 4;
1130 locpos = Read4(stack+frm2);
1131 StkW4(frm2, locpos);
1132 frm2 += 4;
1133
1134 /* The locals-format list is in bytes, so we don't have to convert it. */
1135 frm3 = frm2;
1136 frm2 = frm+locpos;
1137
1138 numlocals = 0;
1139
1140 while (1) {
1141 unsigned char loctype, loccount;
1142 loctype = Read1(stack+frm3);
1143 frm3 += 1;
1144 loccount = Read1(stack+frm3);
1145 frm3 += 1;
1146
1147 if (loctype == 0 && loccount == 0)
1148 break;
1149
1150 /* Skip up to 0, 1, or 3 bytes of padding, depending on loctype. */
1151 while (frm2 & (loctype-1)) {
1152 StkW1(frm2, 0);
1153 frm2++;
1154 }
1155
1156 /* Convert this set of locals. */
1157 switch (loctype) {
1158
1159 case 1:
1160 do {
1161 /* Don't need to convert bytes. */
1162 frm2 += 1;
1163 loccount--;
1164 } while (loccount);
1165 break;
1166
1167 case 2:
1168 do {
1169 glui16 loc = Read2(stack+frm2);
1170 StkW2(frm2, loc);
1171 frm2 += 2;
1172 loccount--;
1173 } while (loccount);
1174 break;
1175
1176 case 4:
1177 do {
1178 glui32 loc = Read4(stack+frm2);
1179 StkW4(frm2, loc);
1180 frm2 += 4;
1181 loccount--;
1182 } while (loccount);
1183 break;
1184
1185 }
1186
1187 numlocals++;
1188 }
1189
1190 if ((numlocals & 1) == 0) {
1191 StkW1(frm3, 0);
1192 frm3++;
1193 StkW1(frm3, 0);
1194 frm3++;
1195 }
1196
1197 if (frm3 != frm+locpos) {
1198 return 1;
1199 }
1200
1201 while (frm2 & 3) {
1202 StkW1(frm2, 0);
1203 frm2++;
1204 }
1205
1206 if (frm2 != frm+frlen) {
1207 return 1;
1208 }
1209
1210 /* Now, the values pushed on the stack after the call frame itself.
1211 This includes the stub. */
1212 while (frm2 < frameend) {
1213 glui32 loc = Read4(stack+frm2);
1214 StkW4(frm2, loc);
1215 frm2 += 4;
1216 }
1217
1218 frameend = frm;
1219 }
1220
1221 return 0;
1222 }
1223
1224 glui32 perform_verify()
1225 {
1226 glui32 len, checksum, newlen;
1227 unsigned char buf[4];
1228 glui32 val, newsum, ix;
1229
1230 len = gamefile_len;
1231 checksum = 0;
1232
1233 if (len < 256 || (len & 0xFF) != 0)
1234 return 1;
1235
1236 glk_stream_set_position(gamefile, gamefile_start, seekmode_Start);
1237 newsum = 0;
1238
1239 /* Read the header */
1240 for (ix=0; ix<9; ix++) {
1241 newlen = glk_get_buffer_stream(gamefile, (char *)buf, 4);
1242 if (newlen != 4)
1243 return 1;
1244 val = Read4(buf);
1245 if (ix == 3) {
1246 if (len != val)
1247 return 1;
1248 }
1249 if (ix == 8)
1250 checksum = val;
1251 else
1252 newsum += val;
1253 }
1254
1255 /* Read everything else */
1256 for (; ix < len/4; ix++) {
1257 newlen = glk_get_buffer_stream(gamefile, (char *)buf, 4);
1258 if (newlen != 4)
1259 return 1;
1260 val = Read4(buf);
1261 newsum += val;
1262 }
1263
1264 if (newsum != checksum)
1265 return 1;
1266
1267 return 0;
1268 }