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