9b90fe651d62db98b82be547cb8618e99abeb384
[pspp] / dump.c
1 #include <float.h>
2 #include <stdbool.h>
3 #include <stdint.h>
4 #include <stdio.h>
5 #include <stdlib.h>
6 #include <string.h>
7 #include <sys/stat.h>
8 #include <unistd.h>
9
10 static uint8_t *data;
11 static size_t n;
12
13 int version;
14
15 static bool
16 all_ascii(const uint8_t *p, size_t n)
17 {
18   for (size_t i = 0; i < n; i++)
19     if (p[i] < 32 || p[i] > 126)
20       return false;
21   return true;
22 }
23
24 static size_t
25 try_find(const char *target, size_t target_len)
26 {
27   const uint8_t *pos = (const uint8_t *) memmem (data, n, target, target_len);
28   return pos ? pos - data : 0;
29 }
30
31 static size_t
32 find(const char *target, size_t target_len)
33 {
34   size_t pos = try_find(target, target_len);
35   if (!pos)
36     {
37       fprintf (stderr, "not found\n");
38       exit(1);
39     }
40   return pos;
41 }
42
43 size_t pos;
44
45 #define XSTR(x) #x
46 #define STR(x) XSTR(x)
47 #define WHERE __FILE__":" STR(__LINE__)
48
49 static unsigned int
50 get_u32(void)
51 {
52   uint32_t x;
53   memcpy(&x, &data[pos], 4);
54   pos += 4;
55   return x;
56 }
57
58 static double
59 get_double(void)
60 {
61   double x;
62   memcpy(&x, &data[pos], 8);
63   pos += 8;
64   return x;
65 }
66
67 static bool
68 match_u32(uint32_t x)
69 {
70   if (get_u32() == x)
71     return true;
72   pos -= 4;
73   return false;
74 }
75
76 static void
77 match_u32_assert(uint32_t x, const char *where)
78 {
79   unsigned int y = get_u32();
80   if (x != y)
81     {
82       fprintf(stderr, "%s: 0x%x: expected i%u, got i%u\n", where, pos - 4, x, y);
83       exit(1);
84     }
85 }
86 #define match_u32_assert(x) match_u32_assert(x, WHERE)
87
88 static bool
89 match_byte(uint8_t b)
90 {
91   if (pos < n && data[pos] == b)
92     {
93       pos++;
94       return true;
95     }
96   else
97     return false;
98 }
99
100 static void
101 match_byte_assert(uint8_t b, const char *where)
102 {
103   if (!match_byte(b))
104     {
105       fprintf(stderr, "%s: 0x%x: expected %02x, got %02x\n", where, pos, b, data[pos]);
106       exit(1);
107     }
108 }
109 #define match_byte_assert(b) match_byte_assert(b, WHERE)
110
111 static void
112 newline(FILE *stream, int pos)
113 {
114   fprintf(stream, "\n%08x: ", pos);
115 }
116
117 static void
118 dump_raw(FILE *stream, int start, int end)
119 {
120   for (size_t i = start; i < end; )
121     {
122       if (i + 5 <= n
123           && data[i]
124           //&& !data[i + 1]
125           && !data[i + 2]
126           && !data[i + 3]
127           && i + 4 + data[i] + data[i + 1] * 256 <= end
128           && all_ascii(&data[i + 4], data[i] + data[i + 1] * 256))
129         {
130           newline(stream, i);
131           fprintf(stream, "\"");
132           fwrite(&data[i + 4], 1, data[i] + data[i + 1] * 256, stream);
133           fputs("\" ", stream);
134
135           i += 4 + data[i] + data[i + 1] * 256;
136         }
137       else if (i + 12 <= end
138                && data[i + 1] == 40
139                && data[i + 2] == 5
140                && data[i + 3] == 0)
141         {
142           double d;
143
144           memcpy (&d, &data[i + 4], 8);
145           fprintf (stream, "F40.%d(%.*f)", data[i], data[i], d);
146           i += 12;
147           newline (stream, i);
148         }
149       else if (i + 12 <= end
150                && data[i + 1] == 40
151                && data[i + 2] == 31
152                && data[i + 3] == 0)
153         {
154           double d;
155
156           memcpy (&d, &data[i + 4], 8);
157           fprintf (stream, "PCT40.%d(%.*f)", data[i], data[i], d);
158           i += 12;
159           newline(stream, i);
160         }
161       else if (i + 4 <= end
162                && (data[i] && data[i] != 88 && data[i] != 0x41)
163                && !data[i + 1]
164                && !data[i + 2]
165                && !data[i + 3])
166         {
167           fprintf (stream, "i%d ", data[i]);
168           i += 4;
169         }
170       else
171         {
172           fprintf(stream, "%02x ", data[i]);
173           i++;
174         }
175     }
176
177
178 }
179
180 static char *
181 get_string(const char *where)
182 {
183   if (1
184       /*data[pos + 1] == 0 && data[pos + 2] == 0 && data[pos + 3] == 0*/
185       /*&& all_ascii(&data[pos + 4], data[pos])*/)
186     {
187       int len = data[pos] + data[pos + 1] * 256;
188       char *s = malloc(len + 1);
189
190       memcpy(s, &data[pos + 4], len);
191       s[len] = 0;
192       pos += 4 + len;
193       return s;
194     }
195   else
196     {
197       fprintf(stderr, "%s: 0x%x: expected string\n", where, pos);
198       exit(1);
199     }
200 }
201 #define get_string() get_string(WHERE)
202
203 static char *
204 dump_nested_string(void)
205 {
206   char *s = NULL;
207
208   match_byte_assert (0);
209   match_byte_assert (0);
210   int outer_end = pos + get_u32();
211   int inner_end = pos + get_u32();
212   if (pos != inner_end)
213     {
214       match_u32_assert(0);
215       if (match_byte(0x31))
216         s = get_string();
217       else
218         match_byte_assert(0x58);
219       if (pos != inner_end)
220         {
221           fprintf(stderr, "inner end discrepancy\n");
222           exit(1);
223         }
224     }
225   match_byte_assert(0x58);
226   match_byte_assert(0x58);
227   if (pos != outer_end)
228     {
229       fprintf(stderr, "outer end discrepancy\n");
230       exit(1);
231     }
232
233   return s;
234 }
235
236 static void
237 dump_value_31(FILE *stream)
238 {
239   if (match_byte (0x31))
240     {
241       if (match_u32 (0))
242         {
243           if (match_u32 (1))
244             {
245               /* Only "a" observed as a sample value (although it appears 44 times in the corpus). */
246               get_string();
247             }
248           else
249             match_u32_assert (0);
250
251           if (version == 1)
252             {
253               /* We only have one SPV file for this version (with many
254                  tables). */
255               match_byte(0);
256               if (!match_u32(1))
257                 match_u32_assert(2);
258               match_byte(0);
259               match_byte(0);
260               if (!match_u32(0) && !match_u32(1) && !match_u32(2) && !match_u32(3) && !match_u32(4) && !match_u32(5) && !match_u32(6) && !match_u32(7) && !match_u32(8) && !match_u32(9))
261                 match_u32_assert(10);
262               match_byte(0);
263               match_byte(0);
264               return;
265             }
266
267           int outer_end = pos + get_u32();
268           int inner_end = pos + get_u32();
269           if (pos != inner_end)
270             {
271               match_u32_assert(0);
272               if (match_byte(0x31))
273                 {
274                   /* Appears to be a template string, e.g. '^1 cells (^2) expf < 5. Min exp = ^3...'.
275                      Probably doesn't actually appear in output because many examples look unpolished,
276                      e.g. 'partial list cases value ^1 shown upper...' */
277                   get_string();
278                 }
279               else
280                 match_byte_assert(0x58);
281               if (pos != inner_end)
282                 {
283                   fprintf(stderr, "inner end discrepancy\n");
284                   exit(1);
285                 }
286             }
287
288           if (match_byte(0x31))
289             {
290               /* Only one example in the corpus. */
291               match_byte(1);
292               match_byte(0);
293               match_byte(0);
294               match_byte(0);
295               match_byte_assert(1);
296               get_string();     /* foreground */
297               get_string();     /* background */
298               get_string();     /* font */
299               if (!match_byte(14))
300                 match_byte_assert(12); /* size? */
301             }
302           else
303             match_byte_assert(0x58);
304           if (match_byte(0x31))
305             {
306               /* Only two SPV files have anything like this, so it's hard to
307                  generalize. */
308               match_u32_assert(0);
309               match_u32_assert(0);
310               match_u32_assert(0);
311               match_u32_assert(0);
312               match_byte_assert(1);
313               match_byte_assert(0);
314               if (!match_byte(8) && !match_byte(1))
315                 match_byte_assert(2);
316               match_byte_assert(0);
317               match_byte_assert(8);
318               match_byte_assert(0);
319               match_byte_assert(10);
320               match_byte_assert(0);
321             }
322           else
323             match_byte_assert(0x58);
324           if (pos != outer_end)
325             {
326               fprintf(stderr, "outer end discrepancy\n");
327               exit(1);
328             }
329         }
330       else if (match_u32 (1))
331         {
332           fprintf(stream, "(footnote %d) ", get_u32());
333           dump_nested_string();
334         }
335       else if (match_u32 (2))
336         {
337           fprintf(stream, "(special 2)");
338           if (!match_byte(0))
339             match_byte_assert(2);
340           match_byte_assert(0);
341           if (!match_u32 (2) && !match_u32(1))
342             match_u32_assert(3);
343           dump_nested_string(); /* Our corpus doesn't contain any examples with strings though. */
344         }
345       else
346         {
347           match_u32_assert(3);
348           fprintf(stream, "(special 3)");
349           match_byte_assert(0);
350           match_byte_assert(0);
351           match_byte_assert(1);
352           match_byte_assert(0);
353           match_u32_assert(2);
354           dump_nested_string(); /* Our corpus doesn't contain any examples with strings though. */
355         }
356     }
357   else
358     match_byte_assert (0x58);
359 }
360
361 static const char *
362 format_to_string (int type)
363 {
364   static char tmp[16];
365   switch (type)
366     {
367     case 1: return "A";
368     case 2: return "AHEX";
369     case 3: return "COMMA";
370     case 4: return "DOLLAR";
371     case 5: case 40: return "F";
372     case 6: return "IB";
373     case 7: return "PIBHEX";
374     case 8: return "P";
375     case 9: return "PIB";
376     case 10: return "PK";
377     case 11: return "RB";
378     case 12: return "RBHEX";
379     case 15: return "Z";
380     case 16: return "N";
381     case 17: return "E";
382     case 20: return "DATE";
383     case 21: return "TIME";
384     case 22: return "DATETIME";
385     case 23: return "ADATE";
386     case 24: return "JDATE";
387     case 25: return "DTIME";
388     case 26: return "WKDAY";
389     case 27: return "MONTH";
390     case 28: return "MOYR";
391     case 29: return "QYR";
392     case 30: return "WKYR";
393     case 31: return "PCT";
394     case 32: return "DOT";
395     case 33: return "CCA";
396     case 34: return "CCB";
397     case 35: return "CCC";
398     case 36: return "CCD";
399     case 37: return "CCE";
400     case 38: return "EDATE";
401     case 39: return "SDATE";
402     default:
403       abort();
404       sprintf(tmp, "<%d>", type);
405       return tmp;
406     }
407 }
408
409 static void
410 dump_value(FILE *stream, int level)
411 {
412   match_byte(0);
413   match_byte(0);
414   match_byte(0);
415   match_byte(0);
416
417   for (int i = 0; i <= level; i++)
418     fprintf (stream, "    ");
419
420   if (match_byte (3))
421     {
422       char *text = get_string();
423       dump_value_31(stream);
424       char *identifier = get_string();
425       char *text_eng = get_string();
426       fprintf (stream, "<string c=\"%s\"", text_eng);
427       if (identifier[0])
428         fprintf (stream, " identifier=\"%s\"", identifier);
429       if (strcmp(text_eng, text))
430         fprintf (stream, " local=\"%s\"", text);
431       fprintf (stream, "/>\n");
432       if (!match_byte (0))
433         match_byte_assert(1);
434     }
435   else if (match_byte (5))
436     {
437       dump_value_31(stream);
438       char *name = get_string ();
439       char *label = get_string ();
440       fprintf (stream, "<variable name=\"%s\"", name);
441       if (label[0])
442         fprintf (stream, " label=\"%s\"", label);
443       fprintf (stream, "/>\n");
444       if (!match_byte(1) && !match_byte(2))
445         match_byte_assert(3);
446     }
447   else if (match_byte (2))
448     {
449       unsigned int format;
450       char *var, *vallab;
451       double value;
452
453       dump_value_31 (stream);
454       format = get_u32 ();
455       value = get_double ();
456       var = get_string ();
457       vallab = get_string ();
458       fprintf (stream, "<numeric-datum value=\"%.*g\" format=\"%s%d.%d\"",
459               DBL_DIG, value, format_to_string(format >> 16), (format >> 8) & 0xff, format & 0xff);
460       if (var[0])
461         fprintf (stream, " variable=\"%s\"", var);
462       if (vallab[0])
463         fprintf (stream, " label=\"%s\"/>\n", vallab);
464       fprintf (stream, "/>\n");
465       if (!match_byte (1) && !match_byte(2))
466         match_byte_assert (3);
467     }
468   else if (match_byte (4))
469     {
470       unsigned int format;
471       char *var, *vallab, *value;
472
473       match_byte_assert (0x58);
474       format = get_u32 ();
475       vallab = get_string ();
476       var = get_string ();
477       if (!match_byte(1) && !match_byte(2))
478         match_byte_assert (3);
479       value = get_string ();
480       fprintf (stream, "<string-datum value=\"%s\" format=\"%s%d.%d\"",
481               value, format_to_string(format >> 16), (format >> 8) & 0xff, format & 0xff);
482       if (var[0])
483         fprintf (stream, " variable=\"%s\"", var);
484       if (vallab[0])
485         fprintf (stream, " label=\"%s\"/>\n", vallab);
486       fprintf (stream, "/>\n");
487     }
488   else if (match_byte (1))
489     {
490       unsigned int format;
491       double value;
492
493       dump_value_31(stream);
494       format = get_u32 ();
495       value = get_double ();
496       fprintf (stream, "<number value=\"%.*g\" format=\"%s%d.%d\"/>\n",
497                DBL_DIG, value, format_to_string(format >> 16), (format >> 8) & 0xff, format & 0xff);
498     }
499   else
500     {
501       dump_value_31(stream);
502
503       char *base = get_string();
504       int x = get_u32();
505       fprintf (stream, "<template format=\"%s\">\n", base);
506       for (int i = 0; i < x; i++)
507         {
508           int y = get_u32();
509           if (!y)
510             y = 1;
511           else
512             match_u32_assert(0);
513           for (int j = 0; j <= level + 1; j++)
514             fprintf (stream, "    ");
515           fprintf (stream, "<substitution index=\"%d\">\n", i + 1);
516           for (int j = 0; j < y; j++)
517             dump_value (stream, level + 2);
518           for (int j = 0; j <= level + 1; j++)
519             fprintf (stream, "    ");
520           fprintf (stream, "</substitution>\n");
521         }
522       for (int j = 0; j <= level; j++)
523         fprintf (stream, "    ");
524       fprintf (stream, "</template>\n");
525     }
526 }
527
528 static int
529 compare_int(const void *a_, const void *b_)
530 {
531   const int *a = a_;
532   const int *b = b_;
533   return *a < *b ? -1 : *a > *b;
534 }
535
536 static void
537 check_permutation(int *a, int n, const char *name)
538 {
539   int b[n];
540   memcpy(b, a, n * sizeof *a);
541   qsort(b, n, sizeof *b, compare_int);
542   for (int i = 0; i < n; i++)
543     if (b[i] != i)
544       {
545         fprintf(stderr, "bad %s permutation:", name);
546         for (int i = 0; i < n; i++)
547           fprintf(stderr, " %d", a[i]);
548         putc('\n', stderr);
549         exit(1);
550       }
551 }
552
553 static void
554 dump_category(int level, int *indexes, int *n_indexes, int max_indexes)
555 {
556   for (int i = 0; i <= level; i++)
557     fprintf (stdout, "    ");
558   printf ("<category>\n");
559   dump_value (stdout, level + 1);
560   match_byte(1);
561   match_byte(0);
562   match_byte(0);
563   match_byte(0);
564
565   if (match_u32 (1))
566     match_byte (0);
567   else if (match_byte (1))
568     {
569       match_byte (0);
570       if (!match_u32 (2))
571         match_u32_assert (1);
572       match_byte (0);
573     }
574   else if (!match_u32(2))
575     match_u32_assert (0);
576
577   int indx = get_u32();
578   int n_categories = get_u32();
579   if (indx != -1)
580     {
581       if (n_categories != 0)
582         {
583           fprintf(stderr, "index not -1 but subcategories\n");
584           exit(1);
585         }
586       if (*n_indexes >= max_indexes)
587         {
588           fprintf(stderr, "too many categories (increase max_indexes)\n");
589           exit(1);
590         }
591       indexes[(*n_indexes)++] = indx;
592     }
593   if (n_categories == 0)
594     {
595       for (int i = 0; i <= level + 1; i++)
596         fprintf (stdout, "    ");
597       fprintf (stdout, "<category-index>%d</category-index>\n", indx);
598     }
599   for (int i = 0; i < n_categories; i++)
600     dump_category (level + 1, indexes, n_indexes, max_indexes);
601   for (int i = 0; i <= level; i++)
602     fprintf (stdout, "    ");
603   printf ("</category>\n");
604 }
605
606 static void
607 dump_dim(int indx)
608 {
609   int n_categories;
610
611   printf ("<dimension index=\"%d\">\n", indx);
612   dump_value (stdout, 0);
613
614   /* This byte is usually 0x02 but many other values have been spotted. */
615   pos++;
616
617   if (!match_byte(0) && !match_byte(1))
618     match_byte_assert(2);
619   if (!match_u32(0))
620     match_u32_assert(2);
621   if (!match_byte(0))
622     match_byte_assert(1);
623   if (!match_byte(0))
624     match_byte_assert(1);
625   match_byte_assert(1);
626   if (!match_u32(UINT32_MAX))
627     match_u32_assert(indx);
628   n_categories = get_u32();
629
630   int indexes[2048];
631   int n_indexes = 0;
632   for (int i = 0; i < n_categories; i++)
633     dump_category (0, indexes, &n_indexes, sizeof indexes / sizeof *indexes);
634   check_permutation(indexes, n_indexes, "categories");
635
636   fprintf (stdout, "</dimension>\n");
637 }
638
639 int n_dims;
640 static void
641 dump_dims(void)
642 {
643   n_dims = get_u32();
644   for (int i = 0; i < n_dims; i++)
645     dump_dim (i);
646 }
647
648 static void
649 dump_data(void)
650 {
651   /* The first three numbers add to the number of dimensions. */
652   int t = get_u32();
653   t += get_u32();
654   match_u32_assert(n_dims - t);
655
656   /* The next n_dims numbers are a permutation of the dimension numbers. */
657   int a[n_dims];
658   for (int i = 0; i < n_dims; i++)
659     a[i] = get_u32();
660   check_permutation(a, n_dims, "dimensions");
661
662   int x = get_u32();
663   printf ("<data>\n");
664   for (int i = 0; i < x; i++)
665     {
666       printf ("    <datum index=\"%d\">\n", get_u32());
667       match_u32_assert(0);
668       if (version == 1)
669         match_byte(0);
670       dump_value(stdout, 1);
671       fprintf (stdout, "    </datum>\n");
672     }
673   printf ("</data>\n");
674 }
675
676 static void
677 dump_title(void)
678 {
679   pos = 0x27;
680   printf ("<title-local>\n");
681   dump_value(stdout, 0);
682   match_byte(1);
683   printf ("</title-local>\n");
684
685   printf ("<subtype>\n");
686   dump_value(stdout, 0);
687   match_byte(1);
688   printf ("</subtype>\n");
689
690   match_byte_assert(0x31);
691
692   printf ("<title-c>\n");
693   dump_value(stdout, 0);
694   match_byte(1);
695   printf ("</title-c>\n");
696
697   match_byte(0);
698   match_byte_assert(0x58);
699   if (match_byte(0x31))
700     {
701       printf ("<caption>\n");
702       dump_value(stdout, 0);
703       printf ("</caption>\n");
704     }
705   else
706     match_byte_assert(0x58);
707
708
709   int n_footnotes = get_u32();
710   for (int i = 0; i < n_footnotes; i++)
711     {
712       printf ("<footnote index=\"%d\">\n", i);
713       dump_value(stdout, 0);
714       if (match_byte (0x31))
715         {
716           /* Custom footnote marker string. */
717           match_byte_assert(3);
718           get_string();
719           match_byte_assert(0x58);
720           match_u32_assert(0);
721           get_string();
722         }
723       else
724         match_byte_assert (0x58);
725       printf("(%d)\n", get_u32());
726       printf ("</footnote>\n");
727     }
728 }
729
730 static void
731 dump_fonts(void)
732 {
733   match_byte(0);
734   for (int i = 1; i <= 8; i++)
735     {
736       printf ("<style index=\"%d\"", i);
737       match_byte_assert(i);
738       match_byte_assert(0x31);
739       printf(" font=\"%s\"", get_string());
740       match_byte_assert(0);
741       match_byte_assert(0);
742       if (!match_byte(0x40) && !match_byte(0x20) && !match_byte(0x80) && !match_byte(0x10) && !match_byte(0x70))
743         match_byte_assert(0x50);
744       if (!match_byte(0x41))
745         match_byte_assert(0x51);
746       if (!match_u32(0) && !match_u32(1))
747         match_u32_assert(2);
748       match_byte_assert(0);
749
750       /* OK, this seems really unlikely to be totally correct, but it matches my corpus... */
751       if (!match_u32(0) && !match_u32(2))
752         match_u32_assert(0xfaad);
753
754       if (!match_u32(0) && !match_u32(1) && !match_u32(2))
755         match_u32_assert(3);
756       printf (" fgcolor=\"%s\"", get_string());
757       printf (" bgcolor=\"%s\"", get_string());
758       match_u32_assert(0);
759       match_u32_assert(0);
760       match_byte_assert(0);
761
762       if (version > 1)
763         {
764           /* These seem unlikely to be correct too. */
765           if (i != 3)
766             {
767               if (!match_u32(8))
768                 match_u32_assert(5);
769               if (!match_u32(10) && !match_u32(11) && !match_u32(5))
770                 match_u32_assert(9);
771               if (!match_u32(0))
772                 match_u32_assert(1);
773             }
774           else
775             {
776               get_u32();
777               if (!match_u32(-1) && !match_u32(8))
778                 match_u32_assert(24);
779               if (!match_u32(-1) && !match_u32(2))
780                 match_u32_assert(3);
781             }
782
783           /* Who knows? Ranges from -1 to 8 with no obvious pattern. */
784           get_u32();
785         }
786
787       printf ("/>\n");
788     }
789
790   match_u32_assert(240);
791   pos += 240;
792
793   match_u32_assert(18);
794   pos += 18;
795
796   if (match_u32(117))
797     pos += 117;
798   else if (match_u32(142))
799     pos += 142;
800   else if (match_u32(143))
801     pos += 143;
802   else if (match_u32(150))
803     pos += 150;
804   else
805     {
806       match_u32_assert(16);
807       pos += 16;
808     }
809
810   int count = get_u32();
811   pos += 4 * count;
812
813   printf ("<encoding>%s</encoding>\n", get_string ());
814
815   if (!match_u32(0))
816     match_u32_assert(UINT32_MAX);
817   if (!match_byte(0))
818     match_byte_assert(1);
819   match_byte_assert(0);
820   if (!match_byte(0))
821     match_byte_assert(1);
822   if (version > 1)
823     {
824       if (!match_byte(0x97) && !match_byte(0x98) && !match_byte(0x99))
825         match_byte_assert(0x9a);
826       match_byte_assert(7);
827       match_byte_assert(0);
828       match_byte_assert(0);
829     }
830   else
831     match_u32_assert(UINT32_MAX);
832   if (match_byte('.'))
833     {
834       if (!match_byte(',') && !match_byte('\''))
835         match_byte_assert(' ');
836     }
837   else
838     {
839       match_byte_assert(',');
840       if (!match_byte('.') && !match_byte(' '))
841         match_byte_assert(0);
842     }
843   if (match_u32(5))
844     {
845       for (int i = 0; i < 5; i++)
846         get_string();
847     }
848   else
849     match_u32_assert(0);
850   int skip = get_u32();
851   pos += skip;
852 }
853
854 int
855 main(int argc, char *argv[])
856 {
857   size_t start;
858   struct stat s;
859
860   if (isatty(STDIN_FILENO))
861     {
862       fprintf(stderr, "redirect stdin from a .bin file\n");
863       exit(1);
864     }
865   if (fstat(STDIN_FILENO, &s))
866     {
867       perror("fstat");
868       exit(1);
869     }
870   n = s.st_size;
871   data = malloc(n);
872   if (!data)
873     {
874       perror("malloc");
875       exit(1);
876     }
877   if (read(STDIN_FILENO, data, n) != n)
878     {
879       perror("read");
880       exit(1);
881     }
882
883   if (argc != 2)
884     {
885       fprintf (stderr, "usage: %s TYPE < .bin", argv[0]);
886       exit (1);
887     }
888
889   if (!strcmp(argv[1], "title0"))
890     {
891       pos = 0x27;
892       if (match_byte (0x03)
893           || (match_byte (0x05) && match_byte (0x58)))
894         printf ("%s\n", get_string());
895       else
896         printf ("<unknown>\n");
897       return 0;
898     }
899   else if (!strcmp(argv[1], "title"))
900     {
901       dump_title();
902       exit(0);
903     }
904   else if (!strcmp(argv[1], "titleraw"))
905     {
906       const char fonts[] = "\x01\x31\x09\0\0\0SansSerif";
907       start = 0x27;
908       n = find(fonts, sizeof fonts - 1);
909     }
910   else if (!strcmp(argv[1], "fonts"))
911     {
912       const char fonts[] = "\x01\x31\x09\0\0\0SansSerif";
913       const char styles[] = "\xf0\0\0\0";
914       start = find(fonts, sizeof fonts - 1);
915       n = find(styles, sizeof styles - 1);
916     }
917   else if (!strcmp(argv[1], "styles"))
918     {
919       const char styles[] = "\xf0\0\0\0";
920       const char dimensions[] = "-,,,.\0";
921       start = find(styles, sizeof styles - 1);
922       n = find(dimensions, sizeof dimensions - 1) + sizeof dimensions - 1;
923     }
924   else if (!strcmp(argv[1], "dimensions") || !strcmp(argv[1], "all"))
925     {
926       pos = 0;
927       match_byte_assert(1);
928       match_byte_assert(0);
929
930       /* This might be a version number of some kind, because value 1 seems
931          to only appear in an SPV file that also required its own weird
932          special cases in dump_value_31(). */
933       version = get_u32();
934       pos -= 4;
935       if (!match_u32(1))
936         match_u32_assert(3);
937
938       match_byte_assert(1);
939       if (!match_byte(0))
940         match_byte_assert(1);
941       match_byte_assert(0);
942       match_byte_assert(0);
943       if (!match_byte(0))
944         match_byte_assert(1);
945       pos++;
946       match_byte_assert(0);
947       match_byte_assert(0);
948       match_byte_assert(0);
949       dump_title ();
950       dump_fonts();
951       dump_dims ();
952       dump_data ();
953       match_byte (1);
954       if (pos != n)
955         {
956           fprintf (stderr, "%x / %x\n", pos, n);
957           exit(1);
958         }
959       exit(0);
960     }
961   else if (!strcmp(argv[1], "raw"))
962     {
963       start = 0x27;
964
965       dump_raw(stdout, start, n);
966     }
967   else
968     {
969       fprintf (stderr, "unknown section %s\n", argv[1]);
970       exit(1);
971     }
972
973   return 0;
974 }