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