Debian Codegraph @

terps/taylor/dumper.c

raw symbols SCIP
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4 #include <ctype.h>
5 #include <time.h>
6 #include <unistd.h>
7
8 #include "taylormade.h"
9
10 static unsigned char Flag[128];
11 static unsigned char Object[256];
12 static unsigned char Word[5];
13
14 static unsigned char Image[131072];
15 static int ImageLen;
16 static int VerbBase;
17 static unsigned int TokenBase;
18 static unsigned int MessageBase;
19 static unsigned int Message2Base;
20 static unsigned int RoomBase;
21 static unsigned int ObjectBase;
22 static unsigned int ExitBase;
23 static unsigned int ObjLocBase;
24 static unsigned int StatusBase;
25 static unsigned int ActionBase;
26 static unsigned int FlagBase;
27 static unsigned char *TopText;
28
29 static int NumLowObjects;
30 static int MaxRoom;
31 static int MaxMessage;
32 static int MaxMessage2;
33
34 static int GameVersion;
35 static int Blizzard;
36
37 /*
38 * Debugging
39 */
40 static unsigned char WordMap[256][5];
41
42 static char *Condition[]={
43 "<ERROR>",
44 "AT",
45 "NOTAT",
46 "ATGT",
47 "ATLT",
48 "PRESENT",
49 "HERE",
50 "ABSENT",
51 "NOTHERE",
52 "CARRIED",
53 "NOTCARRIED",
54 "WORN",
55 "NOTWORN",
56 "NOTDESTROYED",
57 "DESTROYED",
58 "ZERO",
59 "NOTZERO",
60 "WORD1",
61 "WORD2",
62 "WORD3",
63 "CHANCE",
64 "LT",
65 "GT",
66 "EQ",
67 "NE",
68 "OBJECTAT",
69 "COND26",
70 "COND27",
71 "COND28",
72 "COND29",
73 "COND30",
74 "COND31",
75 };
76
77 static char *Action[]={
78 "<ERROR>",
79 "LOAD?",
80 "QUIT",
81 "INVENTORY",
82 "ANYKEY",
83 "SAVE",
84 "DROPALL",
85 "LOOK",
86 "OK", /* Guess */
87 "GET",
88 "DROP",
89 "GOTO",
90 "GOBY",
91 "SET",
92 "CLEAR",
93 "MESSAGE",
94 "CREATE",
95 "DESTROY",
96 "PRINT",
97 "DELAY",
98 "WEAR",
99 "REMOVE",
100 "LET",
101 "ADD",
102 "SUB",
103 "PUT", /* ?? */
104 "SWAP",
105 "SWAPF",
106 "MEANS",
107 "PUTWITH",
108 "BEEP", /* Rebel Planet at least */
109 "REFRESH?",
110 "RAMSAVE",
111 "RAMLOAD",
112 "CLSLOW?",
113 "OOPS",
114 "ACT36",
115 "ACT37",
116 "ACT38",
117 "ACT39",
118 "ACT40",
119 "ACT41",
120 "ACT42",
121 "ACT43",
122 "ACT44",
123 "ACT45",
124 "ACT46",
125 "ACT47",
126 "ACT48",
127 "ACT49",
128 "ACT50",
129 };
130
131 static void LoadWordTable(void)
132 {
133 unsigned char *p = Image + VerbBase;
134
135 printf("Words\n\n");
136 while(1) {
137 if(p[4] == 255)
138 break;
139 if(WordMap[p[4]][0] == 0)
140 memcpy(WordMap[p[4]], p, 4);
141 printf("%-4.4s %d\n", p, p[4]);
142 p+=5;
143 }
144 printf("\n\n");
145 }
146
147 static void PrintWord(unsigned char word)
148 {
149 if(word == 126)
150 printf("* ");
151 else if(word == 0 || WordMap[word][0] == 0)
152 printf("%-4d ", word);
153 else {
154 printf("%c%c%c%c ",
155 WordMap[word][0],
156 WordMap[word][1],
157 WordMap[word][2],
158 WordMap[word][3]);
159 }
160 }
161
162
163 static int FindCode(char *x, int base)
164 {
165 unsigned char *p = Image + base;
166 int len = strlen(x);
167 while(p < Image + ImageLen - len) {
168 if(memcmp(p, x, len) == 0)
169 return p - Image;
170 p++;
171 }
172 return -1;
173 }
174
175 static int FindFlags(void)
176 {
177 /* Look for the flag initial block copy */
178 int pos = FindCode("\x01\x06\x00\xED\xB0\xC9\x00\xFD", 0);
179 if(pos == -1) {
180 fprintf(stderr, "Cannot find initial flag data.\n");
181 exit(1);
182 }
183 return pos + 6;
184 }
185
186 static int FindObjectLocations(void)
187 {
188 int pos = FindCode("\x01\x06\x00\xED\xB0\xC9\x00\xFD", 0);
189 if(pos == -1) {
190 fprintf(stderr, "Cannot find initial object data.\n");
191 exit(1);
192 }
193 pos = Image[pos - 16] + (Image[pos - 15] << 8);
194 return pos - 0x4000;
195 }
196
197 static int FindExits(void)
198 {
199 int pos = 0;
200
201 while((pos = FindCode("\x1A\xBE\x28\x0B\x13", pos+1)) != -1)
202 {
203 pos = Image[pos - 5] + (Image[pos - 4] << 8);
204 pos -= 0x4000;
205 return pos;
206 }
207 fprintf(stderr, "Cannot find initial flag data.\n");
208 exit(1);
209 }
210
211 static int LooksLikeTokens(int pos)
212 {
213 unsigned char *p = Image + pos;
214 int n = 0;
215 int t = 0;
216 while(n < 512) {
217 unsigned char c = p[n] & 0x7F;
218 if(c >= 'a' && c <= 'z')
219 t++;
220 n++;
221 }
222 if(t > 300)
223 return 1;
224 return 0;
225 }
226
227 static void TokenClassify(int pos)
228 {
229 unsigned char *p = Image + pos;
230 int n = 0;
231 while(n++ < 256) {
232 do {
233 if(*p == 0x5E || *p == 0x7E)
234 GameVersion = 0;
235 } while(!(*p++ & 0x80));
236 }
237 }
238
239 static int FindTokens(void)
240 {
241 int addr;
242 int pos = 0;
243 do {
244 pos = FindCode("\x47\xB7\x28\x0B\x2B\x23\xCB\x7E", pos + 1);
245 if(pos == -1) {
246 /* Last resort */
247 addr = FindCode("You are in ", 0) - 1;
248 if(addr == -1) {
249 fprintf(stderr, "Unable to find token table.\n");
250 exit(1);
251 }
252 break;
253 }
254 addr = (Image[pos-1] <<8 | Image[pos-2]) - 0x4000;
255 }
256 while(LooksLikeTokens(addr) == 0);
257 TokenClassify(addr);
258 return addr;
259 }
260
261 static void OutChar(char c)
262 {
263 putchar(c);
264 }
265
266 static unsigned char *TokenText(unsigned char n)
267 {
268 unsigned char *p = Image + TokenBase;
269
270 p = Image + TokenBase;
271
272 while(n > 0) {
273 while((*p & 0x80) == 0)
274 p++;
275 n--;
276 p++;
277 }
278 return p;
279 }
280
281 static void PrintToken(unsigned char n)
282 {
283 unsigned char *p = TokenText(n);
284 unsigned char c;
285 do {
286 c = *p++;
287 OutChar(c & 0x7F);
288 } while(!(c & 0x80));
289 }
290
291 static void PrintText1(unsigned char *p, int n)
292 {
293 while(n > 0) {
294 while(*p != 0x7E && *p != 0x5E)
295 p++;
296 n--;
297 p++;
298 }
299 while(*p != 0x7E && *p != 0x5E)
300 PrintToken(*p++);
301 if(*p == 0x5E)
302 OutChar(' ');
303 if (p > TopText)
304 TopText = p;
305 }
306
307 /*
308 * Version 0 is different
309 */
310
311 static void PrintText0(unsigned char *p, int n)
312 {
313 unsigned char *t = NULL;
314 unsigned char c;
315 while(1) {
316 if(t == NULL)
317 t = TokenText(*p++);
318 c = *t & 0x7F;
319 if(c == 0x5E || c == 0x7E) {
320 if(n == 0) {
321 if(c == 0x5E)
322 OutChar(' ');
323 if (p > TopText)
324 TopText = p;
325 return;
326 }
327 n--;
328 }
329 else if(n == 0)
330 OutChar(c);
331 if(*t++ & 0x80)
332 t = NULL;
333 }
334 }
335
336 static void PrintText(unsigned char *p, int n)
337 {
338 if(GameVersion == 0) /* In stream end markers */
339 PrintText0(p, n);
340 else /* Out of stream end markers (faster) */
341 PrintText1(p, n);
342 }
343
344 static int FindMessages(void)
345 {
346 int pos = 0;
347 /* Newer game format */
348 while((pos = FindCode("\xF5\xE5\xC5\xD5\x3E\x2E", pos+1)) != -1) {
349 if(Image[pos + 6] != 0x32)
350 continue;
351 if(Image[pos + 9] != 0x78)
352 continue;
353 if(Image[pos + 10] != 0x32)
354 continue;
355 if(Image[pos + 13] != 0x21)
356 continue;
357 return (Image[pos+14] + (Image[pos+15] << 8)) - 0x4000;
358 }
359 /* Try now for older game format */
360 while((pos = FindCode("\xF5\xE5\xC5\xD5\x78\x32", pos+1)) != -1) {
361 if(Image[pos + 8] != 0x21)
362 continue;
363 if(Image[pos + 11] != 0xCD)
364 continue;
365 /* End markers in compressed blocks */
366 GameVersion = 0;
367 return (Image[pos+9] + (Image[pos+10] << 8)) - 0x4000;
368 }
369 fprintf(stderr, "Unable to locate messages.\n");
370 exit(1);
371 }
372
373 static int FindMessages2(void)
374 {
375 int pos = 0;
376 while((pos = FindCode("\xF5\xE5\xC5\xD5\x78\x32", pos+1)) != -1) {
377 if(Image[pos + 8] != 0x21)
378 continue;
379 if(Image[pos + 11] != 0xC3)
380 continue;
381 return (Image[pos+9] + (Image[pos+10] << 8)) - 0x4000;
382 }
383 fprintf(stderr, "No second message block ?\n");
384 return 0;
385 }
386
387 static void Message(unsigned int m)
388 {
389 unsigned char *p = Image + MessageBase;
390 PrintText(p, m);
391 }
392
393 static void Message2(unsigned int m)
394 {
395 unsigned char *p = Image + Message2Base;
396 PrintText(p, m);
397 }
398
399 static int FindObjects(void)
400 {
401 int pos = 0;
402 while((pos = FindCode("\xF5\xE5\xC5\xD5\x32", pos+1)) != -1) {
403 if(Image[pos + 10] != 0xCD)
404 continue;
405 if(Image[pos +7] != 0x21)
406 continue;
407 return (Image[pos+8] + (Image[pos+9] << 8)) - 0x4000;
408 }
409 fprintf(stderr, "Unable to locate objects.\n");
410 exit(1);
411 }
412
413 static void PrintObject(unsigned char obj)
414 {
415 unsigned char *p = Image + ObjectBase;
416 PrintText(p, obj);
417 }
418
419 /* Standard format */
420 static int FindRooms(void)
421 {
422 int pos = 0;
423 while((pos = FindCode("\x3E\x19\xCD", pos+1)) != -1) {
424 if(Image[pos + 5] != 0xC3)
425 continue;
426 if(Image[pos + 8] != 0x21)
427 continue;
428 return (Image[pos+9] + (Image[pos+10] << 8)) - 0x4000;
429 }
430 fprintf(stderr, "Unable to locate rooms.\n");
431 exit(1);
432 }
433
434
435 static void PrintRoom(unsigned char room)
436 {
437 unsigned char *p = Image + RoomBase;
438 PrintText(p, room);
439 }
440
441
442 static void NewGame(void)
443 {
444 memset(Flag, 0, 128);
445 memcpy(Flag + 1, Image + FlagBase, 6);
446 memcpy(Object, Image + ObjLocBase, Flag[6]);
447 }
448
449 static void ExecuteLineCode(unsigned char *p)
450 {
451 unsigned char arg1, arg2;
452 int n;
453 do {
454 unsigned char op = *p;
455
456 if(op & 0x80)
457 break;
458 p++;
459 arg1 = *p++;
460 printf("%s %d ", Condition[op], arg1);
461 if(op > 20)
462 {
463 arg2 = *p++;
464 printf("%d ", arg2);
465 }
466 if (op < 5 && arg1 > MaxRoom)
467 MaxRoom = arg1;
468 if (op == 25 && arg2 > MaxRoom)
469 MaxRoom = arg2;
470 } while(1);
471
472 do {
473 unsigned char op = *p;
474 if(!(op & 0x80))
475 break;
476
477 printf("%s ", Action[op & 0x3F], op & 0x3F);
478 p++;
479
480
481 if((op & 0x3F) > 8) {
482 arg1 = *p++;
483 printf("%d ", arg1);
484 }
485 if((op & 0x3F) > 21) {
486 arg2 = *p++;
487 printf("%d ", arg2);
488 }
489 if(op & 0x40)
490 printf("DONE");
491 op &= 0x3F;
492 if (op == 11 && MaxRoom < arg1)
493 MaxRoom = arg1;
494 if (op == 12 && MaxMessage2 < arg1)
495 MaxMessage2 = arg1;
496 if (op == 15 && MaxMessage < arg1)
497 MaxMessage = arg1;
498 if (op == 25 && arg2 < 252 && arg2 > MaxRoom)
499 MaxRoom = arg2;
500
501 }
502 while(1);
503 printf("\n");
504 }
505
506 static unsigned char *NextLine(unsigned char *p)
507 {
508 unsigned char op;
509 while(!((op = *p) & 0x80)) {
510 p+=2;
511 if(op > 20)
512 p++;
513 }
514 while(((op = *p) & 0x80)) {
515 op &= 0x3F;
516 p++;
517 if(op > 8)
518 p++;
519 if(op > 21)
520 p++;
521 }
522 return p;
523 }
524
525 static int FindStatusTable(void)
526 {
527 int pos = 0;
528 while((pos = FindCode("\x3E\xFF\x32", pos+1)) != -1) {
529 if(Image[pos + 5] != 0x18)
530 continue;
531 if(Image[pos + 6] != 0x07)
532 continue;
533 if(Image[pos + 7] != 0x21)
534 continue;
535 return (Image[pos-2] + (Image[pos-1] << 8)) - 0x4000;
536 }
537 fprintf(stderr, "Unable to find automatics.\n");
538 exit(1);
539 }
540
541 static void DumpStatusTable(void)
542 {
543 unsigned char *p = Image + StatusBase;
544
545 while(*p != 0x7F) {
546 ExecuteLineCode(p);
547 p = NextLine(p);
548 }
549 printf("\n\n");
550 }
551
552 int FindCommandTable(void)
553 {
554 int pos = 0;
555 while((pos = FindCode("\x3E\xFF\x32", pos+1)) != -1) {
556 if(Image[pos + 5] != 0x18)
557 continue;
558 if(Image[pos + 6] != 0x07)
559 continue;
560 if(Image[pos + 7] != 0x21)
561 continue;
562 return (Image[pos+8] + (Image[pos+9] << 8)) - 0x4000;
563 }
564 fprintf(stderr, "Unable to find commands.\n");
565 exit(1);
566 }
567
568 static void DumpCommandTable(void)
569 {
570 unsigned char *p = Image + ActionBase;
571
572 while(*p != 0x7F) {
573 PrintWord(p[0]);
574 PrintWord(p[1]);
575 ExecuteLineCode(p + 2);
576 p = NextLine(p + 2);
577 }
578 printf("\n\n");
579 }
580
581 static int DumpExits(unsigned char v)
582 {
583 unsigned char *p = Image + ExitBase;
584 unsigned char want = v | 0x80;
585 while(*p != want) {
586 if(*p == 0xFE)
587 return 0;
588 p++;
589 }
590 p++;
591 while(*p < 0x80) {
592 printf(" ");
593 PrintWord(*p);
594 printf(" %d\n", p[1]);
595 p+=2;
596 }
597 return 0;
598 }
599
600 static void FindTables(void)
601 {
602 printf("Verbs at %04X\n", VerbBase + 0x4000);
603 TokenBase = FindTokens();
604 printf("Tokens at %04X\n", TokenBase + 0x4000);
605 RoomBase = FindRooms();
606 printf("Rooms at %04X\n", RoomBase + 0x4000);
607 ObjectBase = FindObjects();
608 printf("Objects at %04X\n", ObjectBase + 0x4000);
609 StatusBase = FindStatusTable();
610 printf("Status at %04X\n", StatusBase + 0x4000);
611 ActionBase = FindCommandTable();
612 printf("Actions at %04X\n", ActionBase + 0x4000);
613 ExitBase = FindExits();
614 printf("Exits at %04X\n", ExitBase + 0x4000);
615 FlagBase = FindFlags();
616 printf("Flags at %04X\n", FlagBase + 0x4000);
617 ObjLocBase = FindObjectLocations();
618 printf("Object Locations at %04X\n", ObjLocBase + 0x4000);
619 MessageBase = FindMessages();
620 printf("Messages at %04X\n", MessageBase + 0x4000);
621 Message2Base = FindMessages2();
622 printf("Messages Block 2 at %04X\n", Message2Base + 0x4000);
623 printf("Game version %d\n", GameVersion);
624 printf("\n\n\n");
625 }
626
627 /*
628 * Version 0 is different
629 */
630
631 static int GuessLowObjectEnd0(void)
632 {
633 unsigned char *p = Image + ObjectBase;
634 unsigned char *t = NULL;
635 unsigned char c = 0, lc;
636 int n = 0;
637
638 while(1) {
639 if(t == NULL)
640 t = TokenText(*p++);
641 lc = c;
642 c = *t & 0x7F;
643 if(c == 0x5E || c == 0x7E) {
644 if(lc == ',' && n > 20)
645 return n;
646 n++;
647 }
648 if(*t++ & 0x80)
649 t = NULL;
650 }
651 }
652
653
654 static int GuessLowObjectEnd(void)
655 {
656 unsigned char *p = Image + ObjectBase;
657 unsigned char *x;
658 int n = 0;
659
660 if (Blizzard)
661 return 70;
662
663 if(GameVersion == 0)
664 return GuessLowObjectEnd0();
665
666 while(n < Flag[6]) {
667 while(*p != 0x7E && *p != 0x5E) {
668 p++;
669 }
670 x = TokenText(p[-1]);
671 while(!(*x & 0x80)) {
672 x++;
673 }
674 if((*x & 0x7F) == ',')
675 return n;
676 n++;
677 p++;
678 }
679 fprintf(stderr, "Unable to guess the last description object.\n");
680 return 0;
681 }
682
683 void DumpMessages(void)
684 {
685 int i;
686 printf("%d Messages\n\n", MaxMessage);
687 for (i = 0; i < MaxMessage; i++) {
688 printf("%d: ", i);
689 Message(i);
690 printf("\n");
691 }
692 printf("\n\n");
693 }
694
695 void DumpMessages2(void)
696 {
697 int i;
698 if (Message2Base == 0 || MaxMessage2 == 0)
699 return;
700 printf("%d Messages2\n\n", MaxMessage2);
701 for (i = 0; i < MaxMessage2; i++) {
702 printf("%d: ", i);
703 Message2(i);
704 printf("\n");
705 }
706 printf("\n\n");
707 }
708
709 void DumpObjects(void)
710 {
711 int n = Flag[6];
712 int i;
713
714 printf("%d Objects\n\n", n);
715 for (i = 0; i < n; i++) {
716 printf("%d: ", i);
717 PrintObject(i);
718 printf(" %d\n", (int)Object[i]);
719 }
720 printf("\n\n");
721 }
722
723 static int NumRooms(void)
724 {
725 unsigned char *p = Image + ExitBase;
726 unsigned char high = 0;
727
728 if (Blizzard)
729 return 107;
730 while(1) {
731 if(*p == 0xFE) {
732 high &= 0x7F;
733 if (high > MaxRoom)
734 return high;
735 return MaxRoom;
736 }
737 /* Found an entry for a higher room */
738 if (*p & 0x80) {
739 if (*p > high)
740 high = *p;
741 p++;
742 continue;
743 }
744 /* Found an exit to a higher room */
745 if ((p[1] | 0x80) > high) {
746 high = p[1] | 0x80;
747 }
748 p += 2;
749 }
750 }
751
752 void DumpRooms(void)
753 {
754 int n = NumRooms();
755 int i;
756
757 for (i = 0; i < n; i++) {
758 printf("%d: ", i);
759 PrintRoom(i);
760 printf("\n");
761 DumpExits(i);
762 printf("\n");
763 }
764 }
765
766 int main(int argc, char *argv[])
767 {
768 FILE *f;
769
770 if(argv[1] == NULL)
771 {
772 fprintf(stderr, "%s: <file>.\n", argv[0]);
773 exit(1);
774 }
775
776 f = fopen(argv[1], "r");
777 if(f == NULL)
778 {
779 perror(argv[1]);
780 exit(1);
781 }
782 fseek(f, 27L, 0);
783 ImageLen = fread(Image, 1, 131072, f);
784 fclose(f);
785
786 /* Guess initially at He-man style */
787 GameVersion = 2;
788 /* Blizzard Pass */
789 if(ImageLen > 49152) {
790 Blizzard = 1;
791 GameVersion = 1;
792 }
793 /* The message analyser will look for version 0 games */
794
795 printf("Loaded %d bytes.\n", ImageLen);
796
797 VerbBase = FindCode("NORT\001N", 0);
798 if(VerbBase == -1) {
799 fprintf(stderr, "No verb table!\n");
800 exit(1);
801 }
802
803 FindTables();
804 if (GameVersion > 1)
805 Action[12] = "MESSAGE2";
806 LoadWordTable();
807 NewGame();
808 NumLowObjects = GuessLowObjectEnd();
809 DumpObjects();
810 DumpStatusTable();
811 DumpCommandTable();
812 DumpRooms();
813 DumpMessages();
814 DumpMessages2();
815 printf("Text End: %04X\n", TopText - Image + 0x4000);
816 if (GameVersion == 2)
817 printf("Working size needed: %04X\n", TopText - Image - ActionBase);
818 else if (GameVersion == 1)
819 printf("Working size needed: %04X (%04X - %04X)\n", ObjLocBase - RoomBase, ObjLocBase, RoomBase);
820 else
821 printf("Working size needed: %04X\n", TopText - Image - ExitBase);
822 }