dump: Fix everything up through dims for version-3 SPV files.
[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_u32_assert(0x200);
256               match_u32_assert(0x1000000);
257               match_u32_assert(0);
258               match_byte_assert(0);
259               return;
260             }
261
262           int outer_end = pos + get_u32();
263           int inner_end = pos + get_u32();
264           if (pos != inner_end)
265             {
266               match_u32_assert(0);
267               if (match_byte(0x31))
268                 {
269                   /* Appears to be a template string, e.g. '^1 cells (^2) expf < 5. Min exp = ^3...'.
270                      Probably doesn't actually appear in output because many examples look unpolished,
271                      e.g. 'partial list cases value ^1 shown upper...' */
272                   get_string();
273                 }
274               else
275                 match_byte_assert(0x58);
276               if (pos != inner_end)
277                 {
278                   fprintf(stderr, "inner end discrepancy\n");
279                   exit(1);
280                 }
281             }
282
283           if (match_byte(0x31))
284             {
285               /* Only one example in the corpus. */
286               match_byte(1);
287               match_byte(0);
288               match_byte(0);
289               match_byte(0);
290               match_byte_assert(1);
291               get_string();     /* foreground */
292               get_string();     /* background */
293               get_string();     /* font */
294               if (!match_byte(14))
295                 match_byte_assert(12); /* size? */
296             }
297           else
298             match_byte_assert(0x58);
299           if (match_byte(0x31))
300             {
301               /* Only two SPV files have anything like this, so it's hard to
302                  generalize. */
303               match_u32_assert(0);
304               match_u32_assert(0);
305               match_u32_assert(0);
306               match_u32_assert(0);
307               match_byte_assert(1);
308               match_byte_assert(0);
309               if (!match_byte(8) && !match_byte(1))
310                 match_byte_assert(2);
311               match_byte_assert(0);
312               match_byte_assert(8);
313               match_byte_assert(0);
314               match_byte_assert(10);
315               match_byte_assert(0);
316             }
317           else
318             match_byte_assert(0x58);
319           if (pos != outer_end)
320             {
321               fprintf(stderr, "outer end discrepancy\n");
322               exit(1);
323             }
324         }
325       else if (match_u32 (1))
326         {
327           fprintf(stream, "(footnote %d) ", get_u32());
328           dump_nested_string();
329         }
330       else if (match_u32 (2))
331         {
332           fprintf(stream, "(special 2)");
333           match_byte_assert(0);
334           match_byte_assert(0);
335           if (!match_u32 (2))
336             match_u32_assert(1);
337           dump_nested_string(); /* Our corpus doesn't contain any examples with strings though. */
338         }
339       else
340         {
341           match_u32_assert(3);
342           fprintf(stream, "(special 3)");
343           match_byte_assert(0);
344           match_byte_assert(0);
345           match_byte_assert(1);
346           match_byte_assert(0);
347           match_u32_assert(2);
348           dump_nested_string(); /* Our corpus doesn't contain any examples with strings though. */
349         }
350     }
351   else
352     match_byte_assert (0x58);
353 }
354
355 static const char *
356 format_to_string (int type)
357 {
358   static char tmp[16];
359   switch (type)
360     {
361     case 1: return "A";
362     case 2: return "AHEX";
363     case 3: return "COMMA";
364     case 4: return "DOLLAR";
365     case 5: case 40: return "F";
366     case 6: return "IB";
367     case 7: return "PIBHEX";
368     case 8: return "P";
369     case 9: return "PIB";
370     case 10: return "PK";
371     case 11: return "RB";
372     case 12: return "RBHEX";
373     case 15: return "Z";
374     case 16: return "N";
375     case 17: return "E";
376     case 20: return "DATE";
377     case 21: return "TIME";
378     case 22: return "DATETIME";
379     case 23: return "ADATE";
380     case 24: return "JDATE";
381     case 25: return "DTIME";
382     case 26: return "WKDAY";
383     case 27: return "MONTH";
384     case 28: return "MOYR";
385     case 29: return "QYR";
386     case 30: return "WKYR";
387     case 31: return "PCT";
388     case 32: return "DOT";
389     case 33: return "CCA";
390     case 34: return "CCB";
391     case 35: return "CCC";
392     case 36: return "CCD";
393     case 37: return "CCE";
394     case 38: return "EDATE";
395     case 39: return "SDATE";
396     default:
397       abort();
398       sprintf(tmp, "<%d>", type);
399       return tmp;
400     }
401 }
402
403 static void
404 dump_value(FILE *stream, int level, bool match1)
405 {
406   match_byte(0);
407   match_byte(0);
408   match_byte(0);
409   match_byte(0);
410
411   for (int i = 0; i <= level; i++)
412     fprintf (stream, "    ");
413
414   if (match_byte (3))
415     {
416       char *text = get_string();
417       dump_value_31(stream);
418       char *identifier = get_string();
419       char *text_eng = get_string();
420       fprintf (stream, "<string c=\"%s\"", text_eng);
421       if (identifier[0])
422         fprintf (stream, " identifier=\"%s\"", identifier);
423       if (strcmp(text_eng, text))
424         fprintf (stream, " local=\"%s\"", text);
425       fprintf (stream, "/>\n");
426       if (!match_byte (0))
427         match_byte_assert(1);
428       if (match1)
429         match_byte (1);
430     }
431   else if (match_byte (5))
432     {
433       dump_value_31(stream);
434       char *name = get_string ();
435       char *label = get_string ();
436       fprintf (stream, "<variable name=\"%s\"", name);
437       if (label[0])
438         fprintf (stream, " label=\"%s\"", label);
439       fprintf (stream, "/>\n");
440       if (!match_byte(1) && !match_byte(2))
441         match_byte_assert(3);
442     }
443   else if (match_byte (2))
444     {
445       unsigned int format;
446       char *var, *vallab;
447       double value;
448
449       dump_value_31 (stream);
450       format = get_u32 ();
451       value = get_double ();
452       var = get_string ();
453       vallab = get_string ();
454       fprintf (stream, "<numeric-datum value=\"%.*g\" format=\"%s%d.%d\"",
455               DBL_DIG, value, format_to_string(format >> 16), (format >> 8) & 0xff, format & 0xff);
456       if (var[0])
457         fprintf (stream, " variable=\"%s\"", var);
458       if (vallab[0])
459         fprintf (stream, " label=\"%s\"/>\n", vallab);
460       fprintf (stream, "/>\n");
461       if (!match_byte (1) && !match_byte(2))
462         match_byte_assert (3);
463     }
464   else if (match_byte (4))
465     {
466       unsigned int format;
467       char *var, *vallab, *value;
468
469       match_byte_assert (0x58);
470       format = get_u32 ();
471       vallab = get_string ();
472       var = get_string ();
473       if (!match_byte(1) && !match_byte(2))
474         match_byte_assert (3);
475       value = get_string ();
476       fprintf (stream, "<string-datum value=\"%s\" format=\"%s%d.%d\"",
477               value, format_to_string(format >> 16), (format >> 8) & 0xff, format & 0xff);
478       if (var[0])
479         fprintf (stream, " variable=\"%s\"", var);
480       if (vallab[0])
481         fprintf (stream, " label=\"%s\"/>\n", vallab);
482       fprintf (stream, "/>\n");
483     }
484   else if (match_byte (1))
485     {
486       unsigned int format;
487       double value;
488
489       dump_value_31(stream);
490       format = get_u32 ();
491       value = get_double ();
492       fprintf (stream, "<number value=\"%.*g\" format=\"%s%d.%d\"/>\n",
493                DBL_DIG, value, format_to_string(format >> 16), (format >> 8) & 0xff, format & 0xff);
494       if (match1)
495         match_byte (1);
496     }
497   else
498     {
499       dump_value_31(stream);
500
501       char *base = get_string();
502       int x = get_u32();
503       fprintf (stream, "<template format=\"%s\">\n", base);
504       for (int i = 0; i < x; i++)
505         {
506           int y = get_u32();
507           if (!y)
508             y = 1;
509           else
510             match_u32_assert(0);
511           for (int j = 0; j <= level + 1; j++)
512             fprintf (stream, "    ");
513           fprintf (stream, "<substitution index=\"%d\">\n", i + 1);
514           for (int j = 0; j < y; j++)
515             dump_value (stream, level + 2, false);
516           for (int j = 0; j <= level + 1; j++)
517             fprintf (stream, "    ");
518           fprintf (stream, "</substitution>\n");
519         }
520       for (int j = 0; j <= level; j++)
521         fprintf (stream, "    ");
522       fprintf (stream, "</template>\n");
523     }
524 }
525
526 static int
527 compare_int(const void *a_, const void *b_)
528 {
529   const int *a = a_;
530   const int *b = b_;
531   return *a < *b ? -1 : *a > *b;
532 }
533
534 static void
535 check_permutation(int *a, int n, const char *name)
536 {
537   int b[n];
538   memcpy(b, a, n * sizeof *a);
539   qsort(b, n, sizeof *b, compare_int);
540   for (int i = 0; i < n; i++)
541     if (b[i] != i)
542       {
543         fprintf(stderr, "bad %s permutation:", name);
544         for (int i = 0; i < n; i++)
545           fprintf(stderr, " %d", a[i]);
546         putc('\n', stderr);
547         exit(1);
548       }
549 }
550
551 static void
552 dump_category(int level, int *indexes, int *n_indexes, int max_indexes)
553 {
554   for (int i = 0; i <= level; i++)
555     fprintf (stdout, "    ");
556   printf ("<category>\n");
557   dump_value (stdout, level + 1, true);
558   match_byte(0);
559   match_byte(0);
560   match_byte(0);
561
562   if (match_u32 (1))
563     match_byte (0);
564   else if (match_byte (1))
565     {
566       match_byte (0);
567       if (!match_u32 (2))
568         match_u32_assert (1);
569       match_byte (0);
570     }
571   else if (!match_u32(2))
572     match_u32_assert (0);
573
574   int indx = get_u32();
575   int n_categories = get_u32();
576   if (indx != -1)
577     {
578       if (n_categories != 0)
579         {
580           fprintf(stderr, "index not -1 but subcategories\n");
581           exit(1);
582         }
583       if (*n_indexes >= max_indexes)
584         {
585           fprintf(stderr, "too many categories (increase max_indexes)\n");
586           exit(1);
587         }
588       indexes[(*n_indexes)++] = indx;
589     }
590   if (n_categories == 0)
591     {
592       for (int i = 0; i <= level + 1; i++)
593         fprintf (stdout, "    ");
594       fprintf (stdout, "<category-index>%d</category-index>\n", indx);
595     }
596   for (int i = 0; i < n_categories; i++)
597     dump_category (level + 1, indexes, n_indexes, max_indexes);
598   for (int i = 0; i <= level; i++)
599     fprintf (stdout, "    ");
600   printf ("</category>\n");
601 }
602
603 static void
604 dump_dim(int indx)
605 {
606   int n_categories;
607
608   printf ("<dimension index=\"%d\">\n", indx);
609   dump_value (stdout, 0, false);
610
611   /* This byte is usually 0x02 but many other values have been spotted. */
612   pos++;
613
614   if (!match_byte(0) && !match_byte(1))
615     match_byte_assert(2);
616   if (!match_u32(0))
617     match_u32_assert(2);
618   if (!match_byte(0))
619     match_byte_assert(1);
620   if (!match_byte(0))
621     match_byte_assert(1);
622   match_byte_assert(1);
623   match_u32_assert(indx);
624   n_categories = get_u32();
625
626   int indexes[2048];
627   int n_indexes = 0;
628   for (int i = 0; i < n_categories; i++)
629     dump_category (0, indexes, &n_indexes, sizeof indexes / sizeof *indexes);
630   check_permutation(indexes, n_indexes, "categories");
631
632   fprintf (stdout, "</dimension>\n");
633 }
634
635 int n_dims;
636 static void
637 dump_dims(void)
638 {
639   n_dims = get_u32();
640   for (int i = 0; i < n_dims; i++)
641     dump_dim (i);
642 }
643
644 static void
645 dump_data(void)
646 {
647   /* The first three numbers add to the number of dimensions. */
648   int t = get_u32();
649   t += get_u32();
650   match_u32_assert(n_dims - t);
651
652   /* The next n_dims numbers are a permutation of the dimension numbers. */
653   int a[n_dims];
654   for (int i = 0; i < n_dims; i++)
655     a[i] = get_u32();
656   check_permutation(a, n_dims, "dimensions");
657
658   int x = get_u32();
659   printf ("<data>\n");
660   for (int i = 0; i < x; i++)
661     {
662       printf ("    <datum index=\"%d\">\n", get_u32());
663       match_u32_assert(0);
664       dump_value(stdout, 1, false);
665       fprintf (stdout, "    </datum>\n");
666     }
667   printf ("</data>\n");
668 }
669
670 static void
671 dump_title(void)
672 {
673   pos = 0x27;
674   printf ("<title-local>\n");
675   dump_value(stdout, 0, true);
676   printf ("</title-local>\n");
677
678   printf ("<subtype>\n");
679   dump_value(stdout, 0, true);
680   printf ("</subtype>\n");
681
682   match_byte_assert(0x31);
683
684   printf ("<title-c>\n");
685   dump_value(stdout, 0, true);
686   printf ("</title-c>\n");
687
688   match_byte(0);
689   match_byte_assert(0x58);
690   if (match_byte(0x31))
691     {
692       printf ("<caption>\n");
693       dump_value(stdout, 0, false);
694       printf ("</caption>\n");
695     }
696   else
697     match_byte_assert(0x58);
698
699
700   int n_footnotes = get_u32();
701   for (int i = 0; i < n_footnotes; i++)
702     {
703       printf ("<footnote index=\"%d\">\n", i);
704       dump_value(stdout, 0, false);
705       if (match_byte (0x31))
706         {
707           /* Custom footnote marker string. */
708           match_byte_assert(3);
709           get_string();
710           match_byte_assert(0x58);
711           match_u32_assert(0);
712           get_string();
713         }
714       else
715         match_byte_assert (0x58);
716       printf("(%d)\n", get_u32());
717       printf ("</footnote>\n");
718     }
719 }
720
721 static void
722 dump_fonts(void)
723 {
724   match_byte(0);
725   for (int i = 1; i <= 8; i++)
726     {
727       printf ("<style index=\"%d\"", i);
728       match_byte_assert(i);
729       match_byte_assert(0x31);
730       printf(" font=\"%s\"", get_string());
731       match_byte_assert(0);
732       match_byte_assert(0);
733       if (!match_byte(0x40) && !match_byte(0x20) && !match_byte(0x80) && !match_byte(0x10) && !match_byte(0x70))
734         match_byte_assert(0x50);
735       if (!match_byte(0x41))
736         match_byte_assert(0x51);
737       if (!match_u32(0) && !match_u32(1))
738         match_u32_assert(2);
739       match_byte_assert(0);
740
741       /* OK, this seems really unlikely to be totally correct, but it matches my corpus... */
742       if (!match_u32(0) && !match_u32(2))
743         match_u32_assert(0xfaad);
744
745       if (!match_u32(0) && !match_u32(1) && !match_u32(2))
746         match_u32_assert(3);
747       printf (" fgcolor=\"%s\"", get_string());
748       printf (" bgcolor=\"%s\"", get_string());
749       match_u32_assert(0);
750       match_u32_assert(0);
751       match_byte_assert(0);
752
753       if (version > 1)
754         {
755           /* These seem unlikely to be correct too. */
756           if (i != 3)
757             {
758               if (!match_u32(8))
759                 match_u32_assert(5);
760               if (!match_u32(10) && !match_u32(11) && !match_u32(5))
761                 match_u32_assert(9);
762               if (!match_u32(0))
763                 match_u32_assert(1);
764             }
765           else
766             {
767               get_u32();
768               if (!match_u32(-1) && !match_u32(8))
769                 match_u32_assert(24);
770               if (!match_u32(-1) && !match_u32(2))
771                 match_u32_assert(3);
772             }
773
774           /* Who knows? Ranges from -1 to 8 with no obvious pattern. */
775           get_u32();
776         }
777
778       printf ("/>\n");
779     }
780
781   match_u32_assert(240);
782   pos += 240;
783
784   match_u32_assert(18);
785   pos += 18;
786
787   if (match_u32(117))
788     pos += 117;
789   else if (match_u32(142))
790     pos += 142;
791   else if (match_u32(143))
792     pos += 143;
793   else if (match_u32(150))
794     pos += 150;
795   else
796     {
797       match_u32_assert(16);
798       pos += 16;
799     }
800
801   int count = get_u32();
802   pos += 4 * count;
803
804   printf ("<encoding>%s</encoding>\n", get_string ());
805
806   if (!match_u32(0))
807     match_u32_assert(UINT32_MAX);
808   if (!match_byte(0))
809     match_byte_assert(1);
810   match_byte_assert(0);
811   if (!match_byte(0))
812     match_byte_assert(1);
813   if (version > 1)
814     {
815       if (!match_byte(0x97) && !match_byte(0x98) && !match_byte(0x99))
816         match_byte_assert(0x9a);
817       match_byte_assert(7);
818       match_byte_assert(0);
819       match_byte_assert(0);
820     }
821   else
822     match_u32_assert(UINT32_MAX);
823   if (match_byte('.'))
824     {
825       if (!match_byte(','))
826         match_byte_assert(' ');
827     }
828   else
829     {
830       match_byte_assert(',');
831       if (!match_byte('.') && !match_byte(' '))
832         match_byte_assert(0);
833     }
834   if (match_u32(5))
835     {
836       for (int i = 0; i < 5; i++)
837         get_string();
838     }
839   else
840     match_u32_assert(0);
841   int skip = get_u32();
842   pos += skip;
843 }
844
845 int
846 main(int argc, char *argv[])
847 {
848   size_t start;
849   struct stat s;
850
851   if (isatty(STDIN_FILENO))
852     {
853       fprintf(stderr, "redirect stdin from a .bin file\n");
854       exit(1);
855     }
856   if (fstat(STDIN_FILENO, &s))
857     {
858       perror("fstat");
859       exit(1);
860     }
861   n = s.st_size;
862   data = malloc(n);
863   if (!data)
864     {
865       perror("malloc");
866       exit(1);
867     }
868   if (read(STDIN_FILENO, data, n) != n)
869     {
870       perror("read");
871       exit(1);
872     }
873
874   if (argc > 1)
875     {
876       if (!strcmp(argv[1], "title0"))
877         {
878           pos = 0x27;
879           if (match_byte (0x03)
880               || (match_byte (0x05) && match_byte (0x58)))
881             printf ("%s\n", get_string());
882           else
883             printf ("<unknown>\n");
884           return 0;
885         }
886       else if (!strcmp(argv[1], "title"))
887         {
888           dump_title();
889           exit(0);
890         }
891       else if (!strcmp(argv[1], "titleraw"))
892         {
893           const char fonts[] = "\x01\x31\x09\0\0\0SansSerif";
894           start = 0x27;
895           n = find(fonts, sizeof fonts - 1);
896         }
897       else if (!strcmp(argv[1], "fonts"))
898         {
899           const char fonts[] = "\x01\x31\x09\0\0\0SansSerif";
900           const char styles[] = "\xf0\0\0\0";
901           start = find(fonts, sizeof fonts - 1);
902           n = find(styles, sizeof styles - 1);
903         }
904       else if (!strcmp(argv[1], "styles"))
905         {
906           const char styles[] = "\xf0\0\0\0";
907           const char dimensions[] = "-,,,.\0";
908           start = find(styles, sizeof styles - 1);
909           n = find(dimensions, sizeof dimensions - 1) + sizeof dimensions - 1;
910         }
911       else if (!strcmp(argv[1], "dimensions") || !strcmp(argv[1], "all"))
912         {
913           pos = 0;
914           match_byte_assert(1);
915           match_byte_assert(0);
916
917           /* This might be a version number of some kind, because value 1 seems
918              to only appear in an SPV file that also required its own weird
919              special cases in dump_value_31(). */
920           version = get_u32();
921           pos -= 4;
922           if (!match_u32(1))
923             match_u32_assert(3);
924
925           match_byte_assert(1);
926           if (!match_byte(0))
927             match_byte_assert(1);
928           match_byte_assert(0);
929           match_byte_assert(0);
930           if (!match_byte(0))
931             match_byte_assert(1);
932           pos++;
933           match_byte_assert(0);
934           match_byte_assert(0);
935           match_byte_assert(0);
936           dump_title ();
937           dump_fonts();
938           dump_dims ();
939           dump_data ();
940           match_byte (1);
941           if (pos != n)
942             {
943               fprintf (stderr, "%x / %x\n", pos, n);
944               exit(1);
945             }
946           exit(0);
947         }
948       else
949         {
950           fprintf (stderr, "unknown section %s\n", argv[1]);
951           exit(1);
952         }
953     }
954   else
955     start = 0x27;
956
957   dump_raw(stdout, start, n);
958
959   return 0;
960 }