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