17 all_ascii(const uint8_t *p, size_t n)
19 for (size_t i = 0; i < n; i++)
20 if (p[i] < 32 || p[i] > 126)
26 try_find(const char *target, size_t target_len)
28 const uint8_t *pos = (const uint8_t *) memmem (data, n, target, target_len);
29 return pos ? pos - data : 0;
33 find(const char *target, size_t target_len)
35 size_t pos = try_find(target, target_len);
38 fprintf (stderr, "not found\n");
47 #define STR(x) XSTR(x)
48 #define WHERE __FILE__":" STR(__LINE__)
54 memcpy(&x, &data[pos], 4);
63 memcpy(&x, &data[pos], 8);
78 match_u32_assert(uint32_t x, const char *where)
80 unsigned int y = get_u32();
83 fprintf(stderr, "%s: 0x%x: expected i%u, got i%u\n", where, pos - 4, x, y);
87 #define match_u32_assert(x) match_u32_assert(x, WHERE)
92 if (pos < n && data[pos] == b)
102 match_byte_assert(uint8_t b, const char *where)
106 fprintf(stderr, "%s: 0x%x: expected %02x, got %02x\n", where, pos, b, data[pos]);
110 #define match_byte_assert(b) match_byte_assert(b, WHERE)
113 newline(FILE *stream, int pos)
115 fprintf(stream, "\n%08x: ", pos);
119 dump_raw(FILE *stream, int start, int end)
121 for (size_t i = start; i < end; )
128 && i + 4 + data[i] + data[i + 1] * 256 <= end
129 && all_ascii(&data[i + 4], data[i] + data[i + 1] * 256))
132 fprintf(stream, "\"");
133 fwrite(&data[i + 4], 1, data[i] + data[i + 1] * 256, stream);
134 fputs("\" ", stream);
136 i += 4 + data[i] + data[i + 1] * 256;
138 else if (i + 12 <= end
145 memcpy (&d, &data[i + 4], 8);
146 fprintf (stream, "F40.%d(%.*f)", data[i], data[i], d);
150 else if (i + 12 <= end
157 memcpy (&d, &data[i + 4], 8);
158 fprintf (stream, "PCT40.%d(%.*f)", data[i], data[i], d);
162 else if (i + 4 <= end
163 && (data[i] && data[i] != 88 && data[i] != 0x41)
168 fprintf (stream, "i%d ", data[i]);
173 fprintf(stream, "%02x ", data[i]);
182 get_string(const char *where)
185 /*data[pos + 1] == 0 && data[pos + 2] == 0 && data[pos + 3] == 0*/
186 /*&& all_ascii(&data[pos + 4], data[pos])*/)
188 int len = data[pos] + data[pos + 1] * 256;
189 char *s = malloc(len + 1);
191 memcpy(s, &data[pos + 4], len);
198 fprintf(stderr, "%s: 0x%x: expected string\n", where, pos);
202 #define get_string() get_string(WHERE)
205 dump_nested_string(void)
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)
216 if (match_byte(0x31))
219 match_byte_assert(0x58);
220 if (pos != inner_end)
222 fprintf(stderr, "inner end discrepancy\n");
226 match_byte_assert(0x58);
227 match_byte_assert(0x58);
228 if (pos != outer_end)
230 fprintf(stderr, "outer end discrepancy\n");
238 dump_optional_value(FILE *stream)
240 if (match_byte (0x31))
246 /* Only "a" observed as a sample value (although it appears 44 times in the corpus). */
250 match_u32_assert (0);
254 /* We only have one SPV file for this version (with many
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);
268 int outer_end = pos + get_u32();
269 int inner_end = pos + get_u32();
270 if (pos != inner_end)
273 if (match_byte(0x31))
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...' */
281 match_byte_assert(0x58);
282 if (pos != inner_end)
284 fprintf(stderr, "inner end discrepancy\n");
289 if (match_byte(0x31))
291 /* Only one example in the corpus. */
296 match_byte_assert(1);
297 get_string(); /* foreground */
298 get_string(); /* background */
299 get_string(); /* font */
301 match_byte_assert(12); /* size? */
304 match_byte_assert(0x58);
305 if (match_byte(0x31))
307 /* Only two SPV files have anything like this, so it's hard to
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);
324 match_byte_assert(0x58);
325 if (pos != outer_end)
327 fprintf(stderr, "outer end discrepancy\n");
331 else if (match_u32 (1))
333 fprintf(stream, "(footnote %d) ", get_u32());
334 dump_nested_string();
336 else if (match_u32 (2))
338 fprintf(stream, "(special 2)");
340 match_byte_assert(2);
341 match_byte_assert(0);
342 if (!match_u32 (2) && !match_u32(1))
344 dump_nested_string(); /* Our corpus doesn't contain any examples with strings though. */
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);
355 dump_nested_string(); /* Our corpus doesn't contain any examples with strings though. */
359 match_byte_assert (0x58);
363 format_to_string (int type)
369 case 2: return "AHEX";
370 case 3: return "COMMA";
371 case 4: return "DOLLAR";
372 case 5: case 40: return "F";
374 case 7: return "PIBHEX";
376 case 9: return "PIB";
377 case 10: return "PK";
378 case 11: return "RB";
379 case 12: return "RBHEX";
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";
405 sprintf(tmp, "<%d>", type);
411 dump_value(FILE *stream, int level)
418 for (int i = 0; i <= level; i++)
419 fprintf (stream, " ");
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);
429 fprintf (stream, " identifier=\"%s\"", identifier);
430 if (strcmp(text_eng, text))
431 fprintf (stream, " local=\"%s\"", text);
432 fprintf (stream, "/>\n");
434 match_byte_assert(1);
436 else if (match_byte (5))
438 dump_optional_value(stream);
439 char *name = get_string ();
440 char *label = get_string ();
441 fprintf (stream, "<variable name=\"%s\"", name);
443 fprintf (stream, " label=\"%s\"", label);
444 fprintf (stream, "/>\n");
445 if (!match_byte(1) && !match_byte(2))
446 match_byte_assert(3);
448 else if (match_byte (2))
454 dump_optional_value (stream);
456 value = get_double ();
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);
462 fprintf (stream, " variable=\"%s\"", var);
464 fprintf (stream, " label=\"%s\"/>\n", vallab);
465 fprintf (stream, "/>\n");
466 if (!match_byte (1) && !match_byte(2))
467 match_byte_assert (3);
469 else if (match_byte (4))
472 char *var, *vallab, *value;
474 match_byte_assert (0x58);
476 vallab = 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);
484 fprintf (stream, " variable=\"%s\"", var);
486 fprintf (stream, " label=\"%s\"/>\n", vallab);
487 fprintf (stream, "/>\n");
489 else if (match_byte (1))
494 dump_optional_value(stream);
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);
502 dump_optional_value(stream);
504 char *base = get_string();
506 fprintf (stream, "<template format=\"%s\">\n", base);
507 for (int i = 0; i < x; i++)
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");
523 for (int j = 0; j <= level; j++)
524 fprintf (stream, " ");
525 fprintf (stream, "</template>\n");
530 compare_int(const void *a_, const void *b_)
534 return *a < *b ? -1 : *a > *b;
538 check_permutation(int *a, int n, const char *name)
541 memcpy(b, a, n * sizeof *a);
542 qsort(b, n, sizeof *b, compare_int);
543 for (int i = 0; i < n; i++)
546 fprintf(stderr, "bad %s permutation:", name);
547 for (int i = 0; i < n; i++)
548 fprintf(stderr, " %d", a[i]);
555 dump_category(int level, int *indexes, int *n_indexes, int max_indexes)
557 for (int i = 0; i <= level; i++)
558 fprintf (stdout, " ");
559 printf ("<category>\n");
560 dump_value (stdout, level + 1);
568 else if (match_byte (1))
572 match_u32_assert (1);
575 else if (!match_u32(2))
576 match_u32_assert (0);
578 int indx = get_u32();
579 int n_categories = get_u32();
582 if (n_categories != 0)
584 fprintf(stderr, "index not -1 but subcategories\n");
587 if (*n_indexes >= max_indexes)
589 fprintf(stderr, "too many categories (increase max_indexes)\n");
592 indexes[(*n_indexes)++] = indx;
594 if (n_categories == 0)
596 for (int i = 0; i <= level + 1; i++)
597 fprintf (stdout, " ");
598 fprintf (stdout, "<category-index>%d</category-index>\n", indx);
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");
612 printf ("<dimension index=\"%d\">\n", indx);
613 dump_value (stdout, 0);
615 /* This byte is usually 0x02 but many other values have been spotted. */
618 if (!match_byte(0) && !match_byte(1))
619 match_byte_assert(2);
623 match_byte_assert(1);
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();
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");
637 fprintf (stdout, "</dimension>\n");
645 for (int i = 0; i < n_dims; i++)
652 /* The first three numbers add to the number of dimensions. */
655 match_u32_assert(n_dims - t);
657 /* The next n_dims numbers are a permutation of the dimension numbers. */
659 for (int i = 0; i < n_dims; i++)
661 check_permutation(a, n_dims, "dimensions");
665 for (int i = 0; i < x; i++)
667 printf (" <datum index=\"%d\">\n", get_u32());
671 dump_value(stdout, 1);
672 fprintf (stdout, " </datum>\n");
674 printf ("</data>\n");
680 printf ("<title-local>\n");
681 dump_value(stdout, 0);
683 printf ("</title-local>\n");
685 printf ("<subtype>\n");
686 dump_value(stdout, 0);
688 printf ("</subtype>\n");
690 match_byte_assert(0x31);
692 printf ("<title-c>\n");
693 dump_value(stdout, 0);
695 printf ("</title-c>\n");
698 match_byte_assert(0x58);
699 if (match_byte(0x31))
701 printf ("<caption>\n");
702 dump_value(stdout, 0);
703 printf ("</caption>\n");
706 match_byte_assert(0x58);
708 int n_footnotes = get_u32();
709 for (int i = 0; i < n_footnotes; i++)
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);
717 match_byte_assert (0x58);
719 printf ("</footnote>\n");
727 for (int i = 1; i <= 8; i++)
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))
741 match_byte_assert(0);
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);
747 if (!match_u32(0) && !match_u32(1) && !match_u32(2))
749 printf (" fgcolor=\"%s\"", get_string());
750 printf (" bgcolor=\"%s\"", get_string());
753 match_byte_assert(0);
757 /* These seem unlikely to be correct too. */
762 if (!match_u32(10) && !match_u32(11) && !match_u32(5))
770 if (!match_u32(-1) && !match_u32(8))
771 match_u32_assert(24);
772 if (!match_u32(-1) && !match_u32(2))
776 /* Who knows? Ranges from -1 to 8 with no obvious pattern. */
783 match_u32_assert(240);
786 match_u32_assert(18);
791 else if (match_u32(142))
793 else if (match_u32(143))
795 else if (match_u32(150))
799 match_u32_assert(16);
803 int count = get_u32();
806 const char *encoding = get_string();
807 printf ("<encoding>%s</encoding>\n", encoding);
810 match_u32_assert(UINT32_MAX);
812 match_byte_assert(1);
813 match_byte_assert(0);
815 match_byte_assert(1);
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);
825 match_u32_assert(UINT32_MAX);
827 int decimal = data[pos];
828 int grouping = data[pos + 1];
831 if (!match_byte(',') && !match_byte('\''))
832 match_byte_assert(' ');
836 match_byte_assert(',');
837 if (!match_byte('.') && !match_byte(' '))
838 match_byte_assert(0);
840 printf("<format decimal=\"%c\" grouping=\"", decimal);
846 for (int i = 0; i < 5; i++)
847 printf("<CC%c>%s</CC%c>\n", 'A' + i, get_string(), 'A' + i);
851 int skip = get_u32();
856 main(int argc, char *argv[])
861 if (isatty(STDIN_FILENO))
863 fprintf(stderr, "redirect stdin from a .bin file\n");
866 if (fstat(STDIN_FILENO, &s))
878 if (read(STDIN_FILENO, data, n) != n)
886 fprintf (stderr, "usage: %s TYPE < .bin", argv[0]);
890 if (!strcmp(argv[1], "title0"))
893 if (match_byte (0x03)
894 || (match_byte (0x05) && match_byte (0x58)))
895 printf ("%s\n", get_string());
897 printf ("<unknown>\n");
900 else if (!strcmp(argv[1], "title"))
906 else if (!strcmp(argv[1], "titleraw"))
908 const char fonts[] = "\x01\x31\x09\0\0\0SansSerif";
910 n = find(fonts, sizeof fonts - 1);
912 else if (!strcmp(argv[1], "fonts"))
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);
919 else if (!strcmp(argv[1], "styles"))
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;
926 else if (!strcmp(argv[1], "dimensions") || !strcmp(argv[1], "all"))
929 match_byte_assert(1);
930 match_byte_assert(0);
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(). */
940 match_byte_assert(1);
942 match_byte_assert(1);
945 match_byte_assert(0);
946 match_byte_assert(0);
948 match_byte_assert(1);
952 match_byte_assert(0);
953 match_byte_assert(0);
954 match_byte_assert(0);
959 match_byte_assert(1);
960 match_byte_assert(0);
961 match_byte_assert(0);
966 match_byte_assert(1);
967 match_byte_assert(0);
968 match_byte_assert(0);
973 match_byte_assert(1);
974 match_byte_assert(0);
975 match_byte_assert(0);
980 match_byte_assert(0);
981 match_byte_assert(0);
985 This is the tableId, e.g. -4154297861994971133 would be 0xdca00003.
986 We don't have enough context to validate it. */
999 fprintf (stderr, "%x / %x\n", pos, n);
1004 else if (!strcmp(argv[1], "raw"))
1008 dump_raw(stdout, start, n);
1012 fprintf (stderr, "unknown section %s\n", argv[1]);