a7d7e0d167fa8461ba1bb86b998114738b38e8df
[pspp] / dump.c
1 #include <stdbool.h>
2 #include <stdint.h>
3 #include <stdio.h>
4 #include <stdlib.h>
5 #include <string.h>
6 #include <sys/stat.h>
7 #include <unistd.h>
8
9 static uint8_t *data;
10 static size_t n;
11
12 static bool
13 all_ascii(const uint8_t *p, size_t n)
14 {
15   for (size_t i = 0; i < n; i++)
16     if (p[i] < 32 || p[i] > 126)
17       return false;
18   return true;
19 }
20
21 static size_t
22 try_find(const char *target, size_t target_len)
23 {
24   const uint8_t *pos = (const uint8_t *) memmem (data, n, target, target_len);
25   return pos ? pos - data : 0;
26 }
27
28 static size_t
29 try_find_tail(const char *target, size_t target_len)
30 {
31   size_t pos = try_find(target, target_len);
32   return pos ? pos + target_len : 0;
33 }
34
35 static size_t
36 find(const char *target, size_t target_len)
37 {
38   size_t pos = try_find(target, target_len);
39   if (!pos)
40     {
41       fprintf (stderr, "not found\n");
42       exit(1);
43     }
44   return pos;
45 }
46
47 static size_t
48 find_tail(const char *target, size_t target_len)
49 {
50   size_t pos = try_find_tail(target, target_len);
51   if (!pos)
52     {
53       fprintf (stderr, "not found\n");
54       exit(1);
55     }
56   return pos;
57 }
58
59 size_t pos;
60
61 #define XSTR(x) #x
62 #define STR(x) XSTR(x)
63 #define WHERE __FILE__":" STR(__LINE__)
64
65 static unsigned int
66 get_u32(void)
67 {
68   uint32_t x;
69   memcpy(&x, &data[pos], 4);
70   pos += 4;
71   return x;
72 }
73
74 static double
75 get_double(void)
76 {
77   double x;
78   memcpy(&x, &data[pos], 8);
79   pos += 8;
80   return x;
81 }
82
83 static bool
84 match_u32(uint32_t x)
85 {
86   if (get_u32() == x)
87     return true;
88   pos -= 4;
89   return false;
90 }
91
92 static void
93 match_u32_assert(uint32_t x, const char *where)
94 {
95   unsigned int y = get_u32();
96   if (x != y)
97     {
98       fprintf(stderr, "%s: 0x%x: expected i%u, got i%u\n", where, pos - 4, x, y);
99       exit(1);
100     }
101 }
102 #define match_u32_assert(x) match_u32_assert(x, WHERE)
103
104 static bool
105 match_byte(uint8_t b)
106 {
107   if (pos < n && data[pos] == b)
108     {
109       pos++;
110       return true;
111     }
112   else
113     return false;
114 }
115
116 static void
117 match_byte_assert(uint8_t b, const char *where)
118 {
119   if (!match_byte(b))
120     {
121       fprintf(stderr, "%s: 0x%x: expected %02x, got %02x\n", where, pos, b, data[pos]);
122       exit(1);
123     }
124 }
125 #define match_byte_assert(b) match_byte_assert(b, WHERE)
126
127 static char *
128 get_string(const char *where)
129 {
130   if (1
131       /*data[pos + 1] == 0 && data[pos + 2] == 0 && data[pos + 3] == 0*/
132       /*&& all_ascii(&data[pos + 4], data[pos])*/)
133     {
134       int len = data[pos] + data[pos + 1] * 256;
135       char *s = malloc(len + 1);
136
137       memcpy(s, &data[pos + 4], len);
138       s[len] = 0;
139       pos += 4 + len;
140       return s;
141     }
142   else
143     {
144       fprintf(stderr, "%s: 0x%x: expected string\n", where, pos);
145       exit(1);
146     }
147 }
148 #define get_string() get_string(WHERE)
149
150 static void
151 dump_value(int level)
152 {
153   for (int i = 0; i <= level; i++)
154     printf ("    ");
155
156   match_byte (0);
157   match_byte (0);
158   match_byte (0);
159   match_byte (0);
160   if (match_byte (3))
161     {
162       char *s1 = get_string();
163       if (match_byte (0x31))
164         {
165           if (match_u32 (1))
166             {
167               printf("(footnote %d) ", get_u32());
168               match_byte_assert (0);
169               match_byte_assert (0);
170               int subn = get_u32 ();
171               printf ("nested %d bytes", subn);
172               pos += subn;
173             }
174           else if (match_u32 (2))
175             {
176               printf("(special 2)");
177               match_byte_assert(0);
178               match_byte_assert(0);
179               match_u32_assert(1);
180               match_byte_assert(0);
181               match_byte_assert(0);
182               int subn = get_u32 ();
183               printf ("nested %d bytes", subn);
184               pos += subn;
185             }
186           else
187             {
188               match_u32_assert(3);
189               printf("(special 3)");
190               match_byte_assert(0);
191               match_byte_assert(0);
192               match_byte_assert(1);
193               match_byte_assert(0);
194               int subn = get_u32 ();
195               printf ("nested %d bytes, ", subn);
196               pos += subn;
197               subn = get_u32 ();
198               printf ("nested %d bytes, ", subn);
199               pos += subn;
200             }
201         }
202       else
203         match_byte_assert (0x58);
204       char *s2 = get_string();
205       char *s3 = get_string();
206       if (strcmp(s1, s3))
207         printf("strings \"%s\", \"%s\" and \"%s\"", s1, s2, s3);
208       else
209         printf("string \"%s\" and \"%s\"", s1, s2);
210       match_byte (0);
211       match_byte (0);
212       match_byte (0);
213       match_byte (1);
214       match_byte (1);
215       match_byte (0);
216       match_byte (0);
217       match_byte (0);
218       match_byte (1);
219     }
220   else if (match_byte (5))
221     {
222       match_byte_assert (0x58);
223       printf ("variable \"%s\"", get_string());
224       get_string();
225       if (!match_byte(1) && !match_byte(2))
226         match_byte_assert(3);
227       match_byte (0);
228       match_byte (0);
229       match_byte (0);
230       match_byte (0);
231     }
232   else if (match_byte (2))
233     {
234       unsigned int format;
235       char *var, *vallab;
236       double value;
237
238       match_byte_assert (0x58);
239       format = get_u32 ();
240       value = get_double ();
241       var = get_string ();
242       vallab = get_string ();
243       printf ("value %g format %d(%d.%d) var \"%s\" vallab \"%s\"",
244               value, format >> 16, (format >> 8) & 0xff, format & 0xff, var, vallab);
245       if (!match_byte (1) && !match_byte(2))
246         match_byte_assert (3);
247       match_byte (0);
248       match_byte (0);
249       match_byte (0);
250       match_byte (0);
251       match_byte (0);
252       match_byte (0);
253       match_byte (0);
254     }
255   else if (match_byte (4))
256     {
257       unsigned int format;
258       char *var, *vallab, *value;
259
260       match_byte_assert (0x58);
261       format = get_u32 ();
262       vallab = get_string ();
263       var = get_string ();
264       if (!match_byte(1) && !match_byte(2))
265         match_byte_assert (3);
266       value = get_string ();
267       printf ("value \"%s\" format %d(%d.%d) var \"%s\" vallab \"%s\"",
268               value, format >> 16, (format >> 8) & 0xff, format & 0xff, var, vallab);
269       match_byte (0);
270       match_byte (0);
271       match_byte (0);
272       match_byte (0);
273     }
274   else if (match_byte (1))
275     {
276       unsigned int format;
277       double value;
278
279       if (match_byte (0x31))
280         {
281           if (match_u32 (1))
282             {
283               printf("(footnote %d) ", get_u32());
284               match_byte_assert (0);
285               match_byte_assert (0);
286               int subn = get_u32 ();
287               printf ("nested %d bytes", subn);
288               pos += subn;
289             }
290         }
291       else
292         match_byte_assert (0x58);
293       format = get_u32 ();
294       value = get_double ();
295       printf ("value %g format %d(%d.%d)", value, format >> 16, (format >> 8) & 0xff, format & 0xff);
296       match_byte (1);
297       match_byte (0);
298       match_byte (0);
299       match_byte (0);
300       match_byte (1);
301     }
302   else if (match_byte (0x31))
303     {
304       if (match_u32 (1))
305         {
306           printf("(footnote %d) ", get_u32());
307           match_byte_assert (0);
308           match_byte_assert (0);
309           int subn = get_u32 ();
310           printf ("nested %d bytes", subn);
311           pos += subn;
312         }
313       else
314         {
315           match_u32_assert (0);
316           match_u32_assert (0);
317           int subn = get_u32 ();
318           printf ("nested %d bytes", subn);
319           pos += subn;
320         }
321       char *base = get_string();
322       int x = get_u32();
323       printf ("\"%s\"; %d variables:\n", base, x);
324       if (match_u32(0))
325         {
326           for (int i = 0; i < x; i++)
327             {
328               dump_value (level+1);
329               putchar('\n');
330             }
331         }
332       else
333         {
334           for (int i = 0; i < x; i++)
335             {
336               int y = get_u32();
337               match_u32_assert(0);
338               for (int j = 0; j <= level; j++)
339                 printf ("    ");
340               printf("variable %d has %d values:\n", i, y);
341               for (int j = 0; j < y; j++)
342                 {
343                   if (match_byte(3))
344                     {
345                       char *a = get_string();
346                       match_byte_assert(0x58);
347                       char *b = get_string();
348                       char *c = get_string();
349                       for (int k = 0; k <= level + 1; k++)
350                         printf ("    ");
351                       printf ("\"%s\", \"%s\", \"%s\"", a, b, c);
352                       match_byte(0);
353                       match_byte(0);
354                       match_byte(0);
355                       match_byte(0);
356                       match_byte(0);
357                     }
358                   else
359                     dump_value (level+1);
360                   putchar('\n');
361                 }
362             }
363         }
364     }
365   else
366     {
367
368       match_byte_assert (0x58);
369       char *base = get_string();
370       int x = get_u32();
371       printf ("\"%s\" with %d variables:\n", base, x);
372       if (match_u32(0))
373         {
374           for (int i = 0; i < x; i++)
375             {
376               dump_value (level+1);
377               putchar('\n');
378             }
379         }
380       else
381         {
382           for (int i = 0; i < x; i++)
383             {
384               int y = get_u32();
385               match_u32_assert(0);
386               for (int j = 0; j <= level; j++)
387                 printf ("    ");
388               printf("variable %d has %d values:\n", i, y);
389               for (int j = 0; j < y; j++)
390                 {
391                   if (match_byte(3))
392                     {
393                       char *a = get_string();
394                       match_byte_assert(0x58);
395                       char *b = get_string();
396                       char *c = get_string();
397                       for (int k = 0; k <= level + 1; k++)
398                         printf ("    ");
399                       printf ("\"%s\", \"%s\", \"%s\"", a, b, c);
400                       match_byte(0);
401                       match_byte(0);
402                       match_byte(0);
403                       match_byte(0);
404                       match_byte(0);
405                     }
406                   else
407                     dump_value (level+1);
408                   putchar('\n');
409                 }
410             }
411         }
412     }
413 }
414
415 static void
416 dump_dim_value(int level)
417 {
418   for (int i = 0; i <= level; i++)
419     printf ("    ");
420
421   if (match_byte (3))
422     {
423       get_string();
424       if (match_byte (0x31))
425         {
426           match_u32 (1);
427           printf("(footnote %d) ", get_u32());
428           match_byte_assert (0);
429           match_byte_assert (0);
430           int subn = get_u32 ();
431           printf ("nested %d bytes", subn);
432           pos += subn;
433         }
434       else
435         match_byte_assert (0x58);
436       get_string();
437       printf("string \"%s\"", get_string());
438       match_byte (0);
439       match_byte_assert (1);
440       match_byte (0);
441       match_byte (0);
442       match_byte (0);
443       match_byte (1);
444     }
445   else if (match_byte (5))
446     {
447       match_byte_assert (0x58);
448       printf ("variable \"%s\"", get_string());
449       get_string();
450       match_byte_assert (2);
451     }
452   else if (match_byte (2))
453     {
454       unsigned int format;
455       char *var, *vallab;
456       double value;
457
458       match_byte_assert (0x58);
459       format = get_u32 ();
460       value = get_double ();
461       var = get_string ();
462       vallab = get_string ();
463       printf ("value %g format %d(%d.%d) var \"%s\" vallab \"%s\"",
464               value, format >> 16, (format >> 8) & 0xff, format & 0xff, var, vallab);
465       if (!match_u32 (3))
466         match_u32_assert (2);
467       match_byte (0);
468     }
469   else if (match_byte (1))
470     {
471       unsigned int format;
472       double value;
473
474       match_byte_assert (0x58);
475       format = get_u32 ();
476       value = get_double ();
477       printf ("value %g format %d(%d.%d)", value, format >> 16, (format >> 8) & 0xff, format & 0xff);
478       match_byte (1);
479       match_byte (0);
480       match_byte (0);
481       match_byte (0);
482       match_byte (1);
483     }
484   else
485     {
486       int subn;
487
488       match_byte (0);
489       match_byte_assert (0x31);
490       match_u32_assert (0);
491       match_u32_assert (0);
492       subn = get_u32 ();
493       printf ("nested %d bytes", subn);
494       pos += subn;
495       printf ("; \"%s\", substitutions:", get_string());
496       int total_subs = get_u32();
497       int x = get_u32();
498       if (x)
499         {
500           total_subs = (total_subs - 1) + x;
501           match_u32_assert (0);
502         }
503       printf (" (total %d)", total_subs);
504
505       for (int i = 0; i < total_subs; i++)
506         {
507           putc ('\n', stdout);
508           dump_value (level + 1);
509         }
510     }
511 }
512
513 static void
514 dump_category(int level)
515 {
516   match_byte (0);
517   match_byte (0);
518   match_byte (0);
519   match_byte (0);
520   dump_value (level);
521
522   if (match_u32 (2))
523     get_u32 ();
524   else if (match_u32 (1))
525     {
526       match_byte (0);
527       match_byte (0);
528       match_byte (0);
529       get_u32 ();
530     }
531   else if (match_byte (1))
532     {
533       match_byte (0);
534       if (!match_u32 (2))
535         match_u32_assert (1);
536       match_byte (0);
537       get_u32();
538     }
539   else
540     {
541       match_u32_assert (0);
542       get_u32 ();
543     }
544
545   int n_categories = get_u32();
546   if (n_categories > 0)
547     printf (", %d subcategories:", n_categories);
548   printf("\n");
549   for (int i = 0; i < n_categories; i++)
550     dump_category (level + 1);
551 }
552
553 static void
554 dump_dim(void)
555 {
556   int n_categories;
557   printf("next dim\n");
558   match_byte(0);
559   if (match_byte(3))
560     {
561       get_string();
562       match_byte_assert(0x58);
563       get_string();
564       printf("string \"%s\": ", get_string());
565       match_byte(1) || match_byte(0);
566     }
567   else if (match_byte(5)) 
568     {
569       match_byte_assert(0x58);
570       printf("variable \"%s\": ", get_string());
571       get_string();
572       if (!match_byte(2))
573         match_byte_assert(3);
574     }
575   else if (match_byte(0x31))
576     {
577       int subn;
578       int total_subs = 1;
579
580       match_u32_assert (0);
581       match_u32_assert (0);
582       subn = get_u32 ();
583       printf ("nested %d bytes", subn);
584       pos += subn;
585       printf ("; \"%s\", substitutions:", get_string());
586       for (;;)
587         {
588           int n_subst = get_u32();
589           if (!n_subst)
590             break;
591           printf (" %d", n_subst);
592           total_subs *= n_subst;
593         }
594
595       for (int i = 0; i < total_subs; i++)
596         {
597           putc ('\n', stdout);
598           dump_dim_value (0);
599         }
600     }
601   else
602     {
603       int total_subs = 1;
604
605       match_byte_assert (0x58);
606       printf ("\"%s\" with substitutions:", get_string());
607       for (;;)
608         {
609           int n_subst = get_u32();
610           if (!n_subst)
611             break;
612           printf (" %d", n_subst);
613           total_subs *= n_subst;
614         }
615
616       for (int i = 0; i < total_subs; i++)
617         {
618           putc ('\n', stdout);
619           dump_dim_value (0);
620         }
621     }
622
623   /* This byte is usually 0x02 but 0x00 and 0x75 (!) have also been spotted. */
624   pos++;
625
626   if (!match_byte(0) && !match_byte(1))
627     match_byte_assert(2);
628   if (!match_u32(0))
629     match_u32_assert(2);
630   if (!match_byte(0))
631     match_byte_assert(1);
632   match_byte(0);
633   match_byte(0);
634   match_byte(0);
635   match_byte(0);
636   get_u32();
637   match_byte(0);
638   match_byte(0);
639   match_byte(0);
640   match_byte(0);
641   n_categories = get_u32();
642   printf("%d nested categories\n", n_categories);
643   for (int i = 0; i < n_categories; i++)
644     dump_category (0);
645 }
646
647 int n_dims;
648 static void
649 dump_dims(void)
650 {
651   n_dims = get_u32();
652   printf ("%u dimensions\n", n_dims);
653   for (int i = 0; i < n_dims; i++)
654     {
655       printf("\n");
656       dump_dim ();
657     }
658 }
659
660 static void
661 dump_data_value_31(void)
662 {
663   if (match_byte (0x31))
664     {
665       if (match_u32 (0))
666         {
667           if (match_u32 (1))
668             get_string();
669           else
670             match_u32_assert (0);
671           int subn = get_u32 ();
672           printf ("nested %d bytes", subn);
673           pos += subn;
674         }
675       else if (match_u32 (1))
676         {
677           printf("(footnote %d) ", get_u32());
678           match_byte_assert (0);
679           match_byte_assert (0);
680           int subn = get_u32 ();
681           printf ("nested %d bytes", subn);
682           pos += subn;
683         }
684       else if (match_u32 (2))
685         {
686           printf("(special 2)");
687           match_byte_assert(0);
688           match_byte_assert(0);
689           match_u32_assert(1);
690           match_byte_assert(0);
691           match_byte_assert(0);
692           int subn = get_u32 ();
693           printf ("nested %d bytes", subn);
694           pos += subn;
695         }
696       else
697         {
698           match_u32_assert(3);
699           printf("(special 3)");
700           match_byte_assert(0);
701           match_byte_assert(0);
702           match_byte_assert(1);
703           match_byte_assert(0);
704           int subn = get_u32 ();
705           printf ("nested %d bytes, ", subn);
706           pos += subn;
707           subn = get_u32 ();
708           printf ("nested %d bytes, ", subn);
709           pos += subn;
710         }
711     }
712   else
713     match_byte_assert (0x58);
714 }
715
716 static void
717 dump_data(void)
718 {
719 #if 1
720   int a[16];
721   for (int i = 0; i < 3 + n_dims; i++)
722     a[i] = get_u32();
723   printf ("data intro:");
724   for (int i = 0; i < 3 + n_dims; i++)
725     printf(" %d", a[i]);
726   printf("\n");
727 #else
728   fprintf (stderr,"data intro (%d dims):", n_dims);
729   for (int i = 0; i < 3+n_dims; i++)
730     fprintf (stderr," %d", get_u32());
731   fprintf(stderr,"\n");
732 #endif
733   int x = get_u32();
734   printf ("%d data values, starting at %08x\n", x, pos);
735   for (int i = 0; i < x; i++)
736     {
737       printf("%08x, index %d:\n", pos, get_u32());
738       match_u32_assert(0);
739       match_byte(0);
740       match_byte(0);
741       match_byte(0);
742       match_byte(0);
743       if (match_byte (1))
744         {
745           unsigned int format;
746           double value;
747
748           dump_data_value_31();
749           format = get_u32 ();
750           value = get_double ();
751           printf ("    value %g format %d(%d.%d)", value, format >> 16, (format >> 8) & 0xff, format & 0xff);
752         }
753       else if (match_byte (3))
754         {
755           get_string();
756           dump_data_value_31();
757           get_string();
758           printf("string \"%s\"", get_string());
759           match_byte (0);
760         }
761       else if (match_byte (2))
762         {
763           unsigned int format;
764           char *var, *vallab;
765           double value;
766
767           match_byte_assert (0x58);
768           format = get_u32 ();
769           value = get_double ();
770           var = get_string ();
771           vallab = get_string ();
772           printf ("value %g format %d(%d.%d) var \"%s\" vallab \"%s\"",
773                   value, format >> 16, (format >> 8) & 0xff, format & 0xff, var, vallab);
774           if (!match_byte (1) && !match_byte(2))
775             match_byte_assert (3);
776         }
777       else if (match_byte (4))
778         {
779           unsigned int format;
780           char *var, *vallab, *value;
781
782           match_byte_assert (0x58);
783           format = get_u32 ();
784           vallab = get_string ();
785           var = get_string ();
786           if (!match_byte(1) && !match_byte(2))
787             match_byte_assert (3);
788           value = get_string ();
789           printf ("value \"%s\" format %d(%d.%d) var \"%s\" vallab \"%s\"",
790                   value, format >> 16, (format >> 8) & 0xff, format & 0xff, var, vallab);
791         }
792       else if (match_byte (5))
793         {
794           match_byte_assert (0x58);
795           printf ("variable \"%s\"", get_string());
796           get_string();
797           if (!match_byte(1) && !match_byte(2))
798             match_byte_assert(3);
799           match_byte (0);
800           match_byte (0);
801           match_byte (0);
802           match_byte (0);
803         }
804       else if (match_byte(0x31))
805         {
806           if (match_u32 (1))
807             {
808               printf("(footnote %d) ", get_u32());
809               match_byte_assert (0);
810               match_byte_assert (0);
811               int subn = get_u32 ();
812               printf ("nested %d bytes", subn);
813               pos += subn;
814             }
815           else
816             {
817               match_u32_assert (0);
818               match_u32_assert (0);
819               int subn = get_u32 ();
820               printf ("nested %d bytes", subn);
821               pos += subn;
822             }
823           char *base = get_string();
824           int x = get_u32();
825           printf ("\"%s\"; %d variables:\n", base, x);
826           for (int i = 0; i < x; i++)
827             {
828               int y = get_u32();
829               if (!y)
830                 y = 1;
831               else
832                 match_u32_assert(0);
833               for (int j = 0; j <= 0; j++)
834                 printf ("    ");
835               printf("variable %d has %d values:\n", i, y);
836               for (int j = 0; j < y; j++)
837                 {
838                   if (match_byte (1))
839                     {
840                       unsigned int format;
841                       double value;
842
843                       dump_data_value_31();
844                       format = get_u32 ();
845                       value = get_double ();
846                       printf ("    value %g format %d(%d.%d)", value, format >> 16, (format >> 8) & 0xff, format & 0xff);
847                     }
848                   else if (match_byte(3))
849                     {
850                       char *a = get_string();
851                       match_byte_assert(0x58);
852                       char *b = get_string();
853                       char *c = get_string();
854                       for (int k = 0; k <= 1; k++)
855                         printf ("    ");
856                       printf ("\"%s\", \"%s\", \"%s\"", a, b, c);
857                       match_byte(0);
858                       match_byte(0);
859                       match_byte(0);
860                       match_byte(0);
861                     }
862                   else if (match_byte(5))
863                     {
864                       match_byte_assert (0x58);
865                       printf ("variable \"%s\"", get_string());
866                       get_string();
867                       if (!match_byte(1) && !match_byte(2))
868                         match_byte_assert(3);
869                       match_byte (0);
870                       match_byte (0);
871                       match_byte (0);
872                       match_byte (0);
873                     }
874                   else
875                     dump_value (0);
876                   putchar('\n');
877                 }
878             }
879         }
880       else
881         {
882           match_byte_assert (0x58);
883           char *base = get_string();
884           int x = get_u32();
885           printf ("\"%s\" with %d variables:\n", base, x);
886           for (int i = 0; i < x; i++)
887             {
888               int y = get_u32();
889               if (!y)
890                 y = 1;
891               else
892                 match_u32_assert(0);
893               for (int j = 0; j <= 0; j++)
894                 printf ("    ");
895               printf("variable %d has %d values:\n", i, y);
896               for (int j = 0; j < y; j++)
897                 {
898                   if (match_byte(3))
899                     {
900                       char *a = get_string();
901                       match_byte_assert(0x58);
902                       char *b = get_string();
903                       char *c = get_string();
904                       for (int k = 0; k <= 1; k++)
905                         printf ("    ");
906                       printf ("\"%s\", \"%s\", \"%s\"", a, b, c);
907                       match_byte(0);
908                       match_byte(0);
909                       match_byte(0);
910                       match_byte(0);
911                       match_byte(0);
912                     }
913                   else if (match_byte (1))
914                     {
915                       unsigned int format;
916                       double value;
917
918                       dump_data_value_31();
919                       format = get_u32 ();
920                       value = get_double ();
921                       printf ("value %g format %d(%d.%d)", value, format >> 16, (format >> 8) & 0xff, format & 0xff);
922                     }
923                   else
924                     dump_value (1);
925                   putchar('\n');
926                 }
927             }
928         }
929       putchar('\n');
930     }
931 }
932
933 static void
934 dump_title_value_31(int level)
935 {
936   if (match_byte (0x31))
937     {
938       if (match_u32 (0))
939         {
940           match_u32_assert (0);
941           int subn = get_u32 ();
942           printf ("nested %d bytes", subn);
943           pos += subn;
944         }
945       else if (match_u32 (1))
946         {
947           printf("(footnote %d) ", get_u32());
948           match_byte_assert (0);
949           match_byte_assert (0);
950           int subn = get_u32 ();
951           printf ("nested %d bytes", subn);
952           pos += subn;
953         }
954       else if (match_u32 (2))
955         {
956           printf("(special 2)");
957           match_byte_assert(0);
958           match_byte_assert(0);
959           if (!match_u32(2))
960             match_u32_assert(1);
961           match_byte_assert(0);
962           match_byte_assert(0);
963           int subn = get_u32 ();
964           printf ("nested %d bytes", subn);
965           pos += subn;
966         }
967       else
968         {
969           match_u32_assert(3);
970           printf("(special 3)");
971           match_byte_assert(0);
972           match_byte_assert(0);
973           match_byte_assert(1);
974           match_byte_assert(0);
975           int subn = get_u32 ();
976           printf ("nested %d bytes, ", subn);
977           pos += subn;
978           subn = get_u32 ();
979           printf ("nested %d bytes, ", subn);
980           pos += subn;
981         }
982     }
983   else
984     match_byte_assert (0x58);
985 }
986
987 static void
988 dump_title_value(int level)
989 {
990   for (int i = 0; i <= level; i++)
991     printf ("    ");
992
993   match_byte (0);
994   match_byte (0);
995   match_byte (0);
996   match_byte (0);
997   match_byte (0);
998   if (match_byte (3))
999     {
1000       get_string();
1001       dump_title_value_31(level);
1002       get_string();
1003       printf("string \"%s\"", get_string());
1004       match_byte (0);
1005       match_byte (0);
1006       match_byte (0);
1007       match_byte (1);
1008       match_byte (1);
1009       match_byte (0);
1010       match_byte (0);
1011       match_byte (0);
1012       match_byte (1);
1013     }
1014   else if (match_byte (5))
1015     {
1016       dump_title_value_31(level);
1017       printf ("variable \"%s\"", get_string());
1018       get_string();
1019       if (!match_byte(1) && !match_byte(2))
1020         match_byte_assert(3);
1021     }
1022   else if (match_byte (2))
1023     {
1024       unsigned int format;
1025       char *var, *vallab;
1026       double value;
1027
1028       match_byte_assert (0x58);
1029       format = get_u32 ();
1030       value = get_double ();
1031       var = get_string ();
1032       vallab = get_string ();
1033       printf ("value %g format %d(%d.%d) var \"%s\" vallab \"%s\"",
1034               value, format >> 16, (format >> 8) & 0xff, format & 0xff, var, vallab);
1035       if (!match_byte (1) && !match_byte(2))
1036         match_byte_assert (3);
1037       match_byte (0);
1038       match_byte (0);
1039       match_byte (0);
1040       match_byte (0);
1041       match_byte (0);
1042       match_byte (0);
1043       match_byte (0);
1044     }
1045   else if (match_byte (4))
1046     {
1047       unsigned int format;
1048       char *var, *vallab, *value;
1049
1050       match_byte_assert (0x58);
1051       format = get_u32 ();
1052       vallab = get_string ();
1053       var = get_string ();
1054       if (!match_byte(1) && !match_byte(2))
1055         match_byte_assert (3);
1056       value = get_string ();
1057       printf ("value \"%s\" format %d(%d.%d) var \"%s\" vallab \"%s\"",
1058               value, format >> 16, (format >> 8) & 0xff, format & 0xff, var, vallab);
1059       match_byte (0);
1060       match_byte (0);
1061       match_byte (0);
1062       match_byte (0);
1063     }
1064   else if (match_byte (1))
1065     {
1066       unsigned int format;
1067       double value;
1068
1069       dump_title_value_31(level);
1070       format = get_u32 ();
1071       value = get_double ();
1072       printf ("value %g format %d(%d.%d)", value, format >> 16, (format >> 8) & 0xff, format & 0xff);
1073       match_byte (1);
1074       match_byte (0);
1075       match_byte (0);
1076       match_byte (0);
1077       match_byte (1);
1078     }
1079   else
1080     {
1081       dump_title_value_31(level);
1082
1083       char *base = get_string();
1084       int x = get_u32();
1085       printf ("\"%s\" with %d variables:\n", base, x);
1086       for (int i = 0; i < x; i++)
1087         {
1088           int y = get_u32();
1089           if (!y)
1090             y = 1;
1091           else
1092             match_u32_assert(0);
1093           for (int j = 0; j <= level; j++)
1094             printf ("    ");
1095           printf("variable %d has %d values:\n", i, y);
1096           for (int j = 0; j < y; j++)
1097             {
1098               match_byte(0);
1099               if (match_byte(3))
1100                 {
1101                   char *a = get_string();
1102                   match_byte_assert(0x58);
1103                   char *b = get_string();
1104                   char *c = get_string();
1105                   for (int k = 0; k <= level + 1; k++)
1106                     printf ("    ");
1107                   printf ("\"%s\", \"%s\", \"%s\"", a, b, c);
1108                 }
1109               else
1110                 dump_title_value (level+1);
1111               putchar('\n');
1112             }
1113         }
1114     }
1115 }
1116
1117 static void
1118 dump_footnote_value(int level)
1119 {
1120   for (int i = 0; i <= level; i++)
1121     printf ("    ");
1122
1123   match_byte (0);
1124   match_byte (0);
1125   match_byte (0);
1126   match_byte (0);
1127   if (match_byte (3))
1128     {
1129       get_string();
1130       if (match_byte (0x31))
1131         {
1132           if (match_u32 (1))
1133             {
1134               printf("(footnote %d) ", get_u32());
1135               match_byte_assert (0);
1136               match_byte_assert (0);
1137               int subn = get_u32 ();
1138               printf ("nested %d bytes", subn);
1139               pos += subn;
1140             }
1141           else if (match_u32 (2))
1142             {
1143               printf("(special 2)");
1144               match_byte_assert(0);
1145               match_byte_assert(0);
1146               match_u32_assert(1);
1147               match_byte_assert(0);
1148               match_byte_assert(0);
1149               int subn = get_u32 ();
1150               printf ("nested %d bytes", subn);
1151               pos += subn;
1152             }
1153           else
1154             {
1155               match_u32_assert(3);
1156               printf("(special 3)");
1157               match_byte_assert(0);
1158               match_byte_assert(0);
1159               match_byte_assert(1);
1160               match_byte_assert(0);
1161               int subn = get_u32 ();
1162               printf ("nested %d bytes, ", subn);
1163               pos += subn;
1164               subn = get_u32 ();
1165               printf ("nested %d bytes, ", subn);
1166               pos += subn;
1167             }
1168         }
1169       else
1170         match_byte_assert (0x58);
1171       get_string();
1172       printf("string \"%s\"", get_string());
1173       if (!match_byte (0))
1174         match_byte_assert (1);
1175     }
1176   else if (match_byte (5))
1177     {
1178       match_byte_assert (0x58);
1179       printf ("variable \"%s\"", get_string());
1180       get_string();
1181       if (!match_byte(1) && !match_byte(2))
1182         match_byte_assert(3);
1183     }
1184   else if (match_byte (2))
1185     {
1186       unsigned int format;
1187       char *var, *vallab;
1188       double value;
1189
1190       match_byte_assert (0x58);
1191       format = get_u32 ();
1192       value = get_double ();
1193       var = get_string ();
1194       vallab = get_string ();
1195       printf ("value %g format %d(%d.%d) var \"%s\" vallab \"%s\"",
1196               value, format >> 16, (format >> 8) & 0xff, format & 0xff, var, vallab);
1197       if (!match_byte (1) && !match_byte(2))
1198         match_byte_assert (3);
1199       match_byte (0);
1200       match_byte (0);
1201       match_byte (0);
1202       match_byte (0);
1203       match_byte (0);
1204       match_byte (0);
1205       match_byte (0);
1206     }
1207   else if (match_byte (4))
1208     {
1209       unsigned int format;
1210       char *var, *vallab, *value;
1211
1212       match_byte_assert (0x58);
1213       format = get_u32 ();
1214       vallab = get_string ();
1215       var = get_string ();
1216       if (!match_byte(1) && !match_byte(2))
1217         match_byte_assert (3);
1218       value = get_string ();
1219       printf ("value \"%s\" format %d(%d.%d) var \"%s\" vallab \"%s\"",
1220               value, format >> 16, (format >> 8) & 0xff, format & 0xff, var, vallab);
1221       match_byte (0);
1222       match_byte (0);
1223       match_byte (0);
1224       match_byte (0);
1225     }
1226   else if (match_byte (1))
1227     {
1228       unsigned int format;
1229       double value;
1230
1231       if (match_byte (0x31))
1232         {
1233           if (match_u32 (1))
1234             {
1235               printf("(footnote %d) ", get_u32());
1236               match_byte_assert (0);
1237               match_byte_assert (0);
1238               int subn = get_u32 ();
1239               printf ("nested %d bytes", subn);
1240               pos += subn;
1241             }
1242         }
1243       else
1244         match_byte_assert (0x58);
1245       format = get_u32 ();
1246       value = get_double ();
1247       printf ("value %g format %d(%d.%d)", value, format >> 16, (format >> 8) & 0xff, format & 0xff);
1248     }
1249   else if (match_byte (0x31))
1250     {
1251       if (match_u32 (1))
1252         {
1253           printf("(footnote %d) ", get_u32());
1254           match_byte_assert (0);
1255           match_byte_assert (0);
1256           int subn = get_u32 ();
1257           printf ("nested %d bytes", subn);
1258           pos += subn;
1259         }
1260       else
1261         {
1262           match_u32_assert (0);
1263           match_u32_assert (0);
1264           int subn = get_u32 ();
1265           printf ("nested %d bytes", subn);
1266           pos += subn;
1267         }
1268       char *base = get_string();
1269       int x = get_u32();
1270       printf ("\"%s\"; %d variables:\n", base, x);
1271       for (int i = 0; i < x; i++)
1272         {
1273           int y = get_u32();
1274           if (!y)
1275             y = 1;
1276           else
1277             match_u32_assert(0);
1278           for (int j = 0; j <= level; j++)
1279             printf ("    ");
1280           printf("variable %d has %d values:\n", i, y);
1281           for (int j = 0; j < y; j++)
1282             {
1283               if (match_byte(3))
1284                 {
1285                   char *a = get_string();
1286                   match_byte_assert(0x58);
1287                   char *b = get_string();
1288                   char *c = get_string();
1289                   for (int k = 0; k <= level + 1; k++)
1290                     printf ("    ");
1291                   printf ("\"%s\", \"%s\", \"%s\"", a, b, c);
1292                   if (!match_byte(1))
1293                     match_byte_assert(0);
1294                 }
1295               else
1296                 dump_footnote_value (level+1);
1297               putchar('\n');
1298             }
1299         }
1300     }
1301   else
1302     {
1303
1304       match_byte_assert (0x58);
1305       char *base = get_string();
1306       int x = get_u32();
1307       printf ("\"%s\" with %d variables:\n", base, x);
1308       for (int i = 0; i < x; i++)
1309         {
1310           int y = get_u32();
1311           if (!y)
1312             y = 1;
1313           else
1314             match_u32_assert(0);
1315           for (int j = 0; j <= level; j++)
1316             printf ("    ");
1317           printf("variable %d has %d values:\n", i, y);
1318           for (int j = 0; j < y; j++)
1319             {
1320               if (match_byte(3))
1321                 {
1322                   char *a = get_string();
1323                   match_byte_assert(0x58);
1324                   char *b = get_string();
1325                   char *c = get_string();
1326                   for (int k = 0; k <= level + 1; k++)
1327                     printf ("    ");
1328                   printf ("\"%s\", \"%s\", \"%s\"", a, b, c);
1329                   match_byte_assert(0);
1330                 }
1331               else
1332                 dump_footnote_value (level+1);
1333               putchar('\n');
1334             }
1335         }
1336     }
1337 }
1338
1339 static void
1340 dump_title(void)
1341 {
1342   pos = 0x27;
1343   dump_title_value(0); putchar('\n');
1344   dump_title_value(0); putchar('\n');
1345   match_byte_assert(0x31);
1346   dump_title_value(0); putchar('\n');
1347   match_byte(0);
1348   match_byte_assert(0x58);
1349   if (match_byte(0x31))
1350     {
1351       dump_footnote_value(0); putchar('\n');
1352     }
1353   else
1354     match_byte_assert(0x58);
1355
1356
1357   int n_footnotes = get_u32();
1358   if (n_footnotes >= 20)
1359     {
1360       fprintf(stderr, "%08x: %d footnotes\n", pos - 4, n_footnotes);
1361       exit(1);
1362     }
1363
1364   printf("------\n%d footnotes\n", n_footnotes);
1365   if (n_footnotes < 20)
1366     {
1367       for (int i = 0; i < n_footnotes; i++)
1368         {
1369           printf("footnote %d:\n", i);
1370           dump_footnote_value(0);
1371           match_byte(0);
1372           match_byte(0);
1373           match_byte(0);
1374           match_byte(0);
1375           if (match_byte (1))
1376             {
1377               unsigned int format;
1378               double value;
1379
1380               if (match_byte (0x31))
1381                 {
1382                   if (match_u32 (1))
1383                     {
1384                       printf("(footnote %d) ", get_u32());
1385                       match_byte_assert (0);
1386                       match_byte_assert (0);
1387                       int subn = get_u32 ();
1388                       printf ("nested %d bytes", subn);
1389                       pos += subn;
1390                     }
1391                 }
1392               else
1393                 match_byte_assert (0x58);
1394               format = get_u32 ();
1395               value = get_double ();
1396               printf ("value %g format %d(%d.%d)", value, format >> 16, (format >> 8) & 0xff, format & 0xff);
1397               match_byte (1);
1398               match_byte (0);
1399               match_byte (0);
1400               match_byte (0);
1401               match_byte (1);
1402             }
1403           else if (match_byte (0x31))
1404             {
1405               match_byte_assert(3);
1406               get_string();
1407               match_byte_assert(0x58);
1408               match_u32_assert(0);
1409               get_string();
1410               match_byte(0);
1411             }
1412           else
1413             match_byte_assert (0x58);
1414           printf("(%d)\n", get_u32());
1415         }
1416     }
1417 }
1418
1419 static int
1420 find_dimensions(void)
1421 {
1422   {
1423     const char dimensions[] = "-,,,.\0";
1424     int x = try_find_tail(dimensions, sizeof dimensions - 1);
1425     if (x)
1426       return x;
1427   }
1428
1429   const char dimensions[] = "-,,, .\0";
1430   return find_tail(dimensions, sizeof dimensions - 1);
1431 }
1432
1433 static void
1434 dump_fonts(void)
1435 {
1436   printf("fonts: offset=%08x\n", pos);
1437   match_byte(0);
1438   for (int i = 1; i <= 8; i++)
1439     {
1440       printf("%08x: font %d, ", pos, i);
1441       match_byte_assert(i);
1442       match_byte_assert(0x31);
1443       printf("%s, ", get_string());
1444       match_byte_assert(0);
1445       match_byte_assert(0);
1446       if (!match_byte(0x40) && !match_byte(0x20) && !match_byte(0x80) && !match_byte(0x10))
1447         match_byte_assert(0x50);
1448       if (!match_byte(0x41))
1449         match_byte_assert(0x51);
1450       pos += 13;
1451       printf ("%s, ", get_string());
1452       printf ("%s, ", get_string());
1453       match_u32_assert(0);
1454       match_u32_assert(0);
1455       pos++;
1456       get_u32();
1457       get_u32();
1458       get_u32();
1459       get_u32();
1460       putchar('\n');
1461     }
1462
1463   match_u32_assert(240);
1464   pos += 240;
1465
1466   match_u32_assert(18);
1467   pos += 18;
1468
1469   if (match_u32(117))
1470     pos += 117;
1471   else
1472     {
1473       match_u32_assert(142);
1474       pos += 142;
1475     }
1476
1477   int count = get_u32();
1478   pos += 4 * count;
1479
1480   char *encoding = get_string();
1481   printf("encoding=%s\n", encoding);
1482
1483   if (!match_u32(0))
1484     match_u32_assert(UINT32_MAX);
1485   if (!match_byte(0))
1486     match_byte_assert(1);
1487   match_byte_assert(0);
1488   if (!match_byte(0))
1489     match_byte_assert(1);
1490   if (!match_byte(0x99) && !match_byte(0x98))
1491     match_byte_assert(0x97);
1492   match_byte_assert(7);
1493   match_byte_assert(0);
1494   match_byte_assert(0);
1495   if (match_byte('.'))
1496     match_byte_assert(',');
1497   else
1498     {
1499       match_byte_assert(',');
1500       if (!match_byte('.'))
1501         match_byte_assert(' ');
1502     }
1503   match_u32_assert(5);
1504   for (int i = 0; i < 5; i++)
1505     get_string();
1506   pos += get_u32();
1507   if (pos != find_dimensions())
1508     fprintf (stderr, "%08x / %08x\n", pos, find_dimensions());
1509 }
1510
1511 int
1512 main(int argc, char *argv[])
1513 {
1514   size_t start;
1515   struct stat s;
1516
1517   if (isatty(STDIN_FILENO))
1518     {
1519       fprintf(stderr, "redirect stdin from a .bin file\n");
1520       exit(1);
1521     }
1522   if (fstat(STDIN_FILENO, &s))
1523     {
1524       perror("fstat");
1525       exit(1);
1526     }
1527   n = s.st_size;
1528   data = malloc(n);
1529   if (!data)
1530     {
1531       perror("malloc");
1532       exit(1);
1533     }
1534   if (read(STDIN_FILENO, data, n) != n)
1535     {
1536       perror("read");
1537       exit(1);
1538     }
1539
1540   if (argc > 1)
1541     {
1542       if (!strcmp(argv[1], "title0"))
1543         {
1544           pos = 0x27;
1545           if (match_byte (0x03)
1546               || (match_byte (0x05) && match_byte (0x58)))
1547             printf ("%s\n", get_string());
1548           else
1549             printf ("<unknown>\n");
1550           return 0;
1551         }
1552       else if (!strcmp(argv[1], "title"))
1553         {
1554           dump_title();
1555           exit(0);
1556         }
1557       else if (!strcmp(argv[1], "titleraw"))
1558         {
1559           const char fonts[] = "\x01\x31\x09\0\0\0SansSerif";
1560           start = 0x27;
1561           n = find(fonts, sizeof fonts - 1);
1562         }
1563       else if (!strcmp(argv[1], "fonts"))
1564         {
1565           const char fonts[] = "\x01\x31\x09\0\0\0SansSerif";
1566           const char styles[] = "\xf0\0\0\0";
1567           start = find(fonts, sizeof fonts - 1);
1568           n = find(styles, sizeof styles - 1);
1569         }
1570       else if (!strcmp(argv[1], "styles"))
1571         {
1572           const char styles[] = "\xf0\0\0\0";
1573           const char dimensions[] = "-,,,.\0";
1574           start = find(styles, sizeof styles - 1);
1575           n = find(dimensions, sizeof dimensions - 1) + sizeof dimensions - 1;
1576         }
1577       else if (!strcmp(argv[1], "dimensions") || !strcmp(argv[1], "all"))
1578         {
1579           pos = 0;
1580           match_byte_assert(1);
1581           match_byte_assert(0);
1582           match_u32_assert(3);
1583           match_byte_assert(1);
1584           if (!match_byte(0))
1585             match_byte_assert(1);
1586           match_byte_assert(0);
1587           match_byte_assert(0);
1588           if (!match_byte(0))
1589             match_byte_assert(1);
1590           pos++;
1591           match_byte_assert(0);
1592           match_byte_assert(0);
1593           match_byte_assert(0);
1594           dump_title ();
1595           dump_fonts();
1596           dump_dims ();
1597           printf("\n\ndata:\n");
1598           dump_data ();
1599           if (pos == n - 1)
1600             match_byte_assert (1);
1601           if (pos != n)
1602             {
1603               fprintf (stderr, "%x / %x\n", pos, n);
1604               exit(1);
1605             }
1606           exit(0);
1607         }
1608       else
1609         {
1610           fprintf (stderr, "unknown section %s\n", argv[1]);
1611           exit(1);
1612         }
1613     }
1614   else
1615     start = 0x27;
1616
1617   for (size_t i = start; i < n; )
1618     {
1619       if (i + 5 <= n
1620           && data[i]
1621           //&& !data[i + 1]
1622           && !data[i + 2]
1623           && !data[i + 3]
1624           && i + 4 + data[i] + data[i + 1] * 256 <= n
1625           && all_ascii(&data[i + 4], data[i] + data[i + 1] * 256))
1626         {
1627           fputs("\n\"", stdout);
1628           fwrite(&data[i + 4], 1, data[i] + data[i + 1] * 256, stdout);
1629           fputs("\" ", stdout);
1630
1631           i += 4 + data[i] + data[i + 1] * 256;
1632         }
1633       else if (i + 12 <= n
1634                && data[i + 1] == 40
1635                && data[i + 2] == 5
1636                && data[i + 3] == 0)
1637         {
1638           double d;
1639
1640           memcpy (&d, &data[i + 4], 8);
1641           printf ("F40.%d(%.*f)\n", data[i], data[i], d);
1642           i += 12;
1643         }
1644       else if (i + 12 <= n
1645                && data[i + 1] == 40
1646                && data[i + 2] == 31
1647                && data[i + 3] == 0)
1648         {
1649           double d;
1650
1651           memcpy (&d, &data[i + 4], 8);
1652           printf ("PCT40.%d(%.*f)\n", data[i], data[i], d);
1653           i += 12;
1654         }
1655       else if (i + 4 <= n
1656                && (data[i] && data[i] != 88 && data[i] != 0x41)
1657                && !data[i + 1]
1658                && !data[i + 2]
1659                && !data[i + 3])
1660         {
1661           printf ("i%d ", data[i]);
1662           i += 4;
1663         }
1664       else
1665         {
1666           printf("%02x ", data[i]);
1667           i++;
1668         }
1669     }
1670
1671   return 0;
1672 }