16 all_ascii(const uint8_t *p, size_t n)
18 for (size_t i = 0; i < n; i++)
19 if (p[i] < 32 || p[i] > 126)
25 try_find(const char *target, size_t target_len)
27 const uint8_t *pos = (const uint8_t *) memmem (data, n, target, target_len);
28 return pos ? pos - data : 0;
32 find(const char *target, size_t target_len)
34 size_t pos = try_find(target, target_len);
37 fprintf (stderr, "not found\n");
46 #define STR(x) XSTR(x)
47 #define WHERE __FILE__":" STR(__LINE__)
53 memcpy(&x, &data[pos], 4);
62 memcpy(&x, &data[pos], 8);
77 match_u32_assert(uint32_t x, const char *where)
79 unsigned int y = get_u32();
82 fprintf(stderr, "%s: 0x%x: expected i%u, got i%u\n", where, pos - 4, x, y);
86 #define match_u32_assert(x) match_u32_assert(x, WHERE)
91 if (pos < n && data[pos] == b)
101 match_byte_assert(uint8_t b, const char *where)
105 fprintf(stderr, "%s: 0x%x: expected %02x, got %02x\n", where, pos, b, data[pos]);
109 #define match_byte_assert(b) match_byte_assert(b, WHERE)
112 newline(FILE *stream, int pos)
114 fprintf(stream, "\n%08x: ", pos);
118 dump_raw(FILE *stream, int start, int end)
120 for (size_t i = start; i < end; )
127 && i + 4 + data[i] + data[i + 1] * 256 <= end
128 && all_ascii(&data[i + 4], data[i] + data[i + 1] * 256))
131 fprintf(stream, "\"");
132 fwrite(&data[i + 4], 1, data[i] + data[i + 1] * 256, stream);
133 fputs("\" ", stream);
135 i += 4 + data[i] + data[i + 1] * 256;
137 else if (i + 12 <= end
144 memcpy (&d, &data[i + 4], 8);
145 fprintf (stream, "F40.%d(%.*f)", data[i], data[i], d);
149 else if (i + 12 <= end
156 memcpy (&d, &data[i + 4], 8);
157 fprintf (stream, "PCT40.%d(%.*f)", data[i], data[i], d);
161 else if (i + 4 <= end
162 && (data[i] && data[i] != 88 && data[i] != 0x41)
167 fprintf (stream, "i%d ", data[i]);
172 fprintf(stream, "%02x ", data[i]);
181 get_string(const char *where)
184 /*data[pos + 1] == 0 && data[pos + 2] == 0 && data[pos + 3] == 0*/
185 /*&& all_ascii(&data[pos + 4], data[pos])*/)
187 int len = data[pos] + data[pos + 1] * 256;
188 char *s = malloc(len + 1);
190 memcpy(s, &data[pos + 4], len);
197 fprintf(stderr, "%s: 0x%x: expected string\n", where, pos);
201 #define get_string() get_string(WHERE)
204 dump_nested_string(void)
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)
215 if (match_byte(0x31))
218 match_byte_assert(0x58);
219 if (pos != inner_end)
221 fprintf(stderr, "inner end discrepancy\n");
225 match_byte_assert(0x58);
226 match_byte_assert(0x58);
227 if (pos != outer_end)
229 fprintf(stderr, "outer end discrepancy\n");
237 dump_value_31(FILE *stream)
239 if (match_byte (0x31))
245 /* Only "a" observed as a sample value (although it appears 44 times in the corpus). */
249 match_u32_assert (0);
253 /* We only have one SPV file for this version (with many
260 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))
261 match_u32_assert(10);
267 int outer_end = pos + get_u32();
268 int inner_end = pos + get_u32();
269 if (pos != inner_end)
272 if (match_byte(0x31))
274 /* Appears to be a template string, e.g. '^1 cells (^2) expf < 5. Min exp = ^3...'.
275 Probably doesn't actually appear in output because many examples look unpolished,
276 e.g. 'partial list cases value ^1 shown upper...' */
280 match_byte_assert(0x58);
281 if (pos != inner_end)
283 fprintf(stderr, "inner end discrepancy\n");
288 if (match_byte(0x31))
290 /* Only one example in the corpus. */
295 match_byte_assert(1);
296 get_string(); /* foreground */
297 get_string(); /* background */
298 get_string(); /* font */
300 match_byte_assert(12); /* size? */
303 match_byte_assert(0x58);
304 if (match_byte(0x31))
306 /* Only two SPV files have anything like this, so it's hard to
312 match_byte_assert(1);
313 match_byte_assert(0);
314 if (!match_byte(8) && !match_byte(1))
315 match_byte_assert(2);
316 match_byte_assert(0);
317 match_byte_assert(8);
318 match_byte_assert(0);
319 match_byte_assert(10);
320 match_byte_assert(0);
323 match_byte_assert(0x58);
324 if (pos != outer_end)
326 fprintf(stderr, "outer end discrepancy\n");
330 else if (match_u32 (1))
332 fprintf(stream, "(footnote %d) ", get_u32());
333 dump_nested_string();
335 else if (match_u32 (2))
337 fprintf(stream, "(special 2)");
339 match_byte_assert(2);
340 match_byte_assert(0);
341 if (!match_u32 (2) && !match_u32(1))
343 dump_nested_string(); /* Our corpus doesn't contain any examples with strings though. */
348 fprintf(stream, "(special 3)");
349 match_byte_assert(0);
350 match_byte_assert(0);
351 match_byte_assert(1);
352 match_byte_assert(0);
354 dump_nested_string(); /* Our corpus doesn't contain any examples with strings though. */
358 match_byte_assert (0x58);
362 format_to_string (int type)
368 case 2: return "AHEX";
369 case 3: return "COMMA";
370 case 4: return "DOLLAR";
371 case 5: case 40: return "F";
373 case 7: return "PIBHEX";
375 case 9: return "PIB";
376 case 10: return "PK";
377 case 11: return "RB";
378 case 12: return "RBHEX";
382 case 20: return "DATE";
383 case 21: return "TIME";
384 case 22: return "DATETIME";
385 case 23: return "ADATE";
386 case 24: return "JDATE";
387 case 25: return "DTIME";
388 case 26: return "WKDAY";
389 case 27: return "MONTH";
390 case 28: return "MOYR";
391 case 29: return "QYR";
392 case 30: return "WKYR";
393 case 31: return "PCT";
394 case 32: return "DOT";
395 case 33: return "CCA";
396 case 34: return "CCB";
397 case 35: return "CCC";
398 case 36: return "CCD";
399 case 37: return "CCE";
400 case 38: return "EDATE";
401 case 39: return "SDATE";
404 sprintf(tmp, "<%d>", type);
410 dump_value(FILE *stream, int level, bool match1)
417 for (int i = 0; i <= level; i++)
418 fprintf (stream, " ");
422 char *text = get_string();
423 dump_value_31(stream);
424 char *identifier = get_string();
425 char *text_eng = get_string();
426 fprintf (stream, "<string c=\"%s\"", text_eng);
428 fprintf (stream, " identifier=\"%s\"", identifier);
429 if (strcmp(text_eng, text))
430 fprintf (stream, " local=\"%s\"", text);
431 fprintf (stream, "/>\n");
433 match_byte_assert(1);
437 else if (match_byte (5))
439 dump_value_31(stream);
440 char *name = get_string ();
441 char *label = get_string ();
442 fprintf (stream, "<variable name=\"%s\"", name);
444 fprintf (stream, " label=\"%s\"", label);
445 fprintf (stream, "/>\n");
446 if (!match_byte(1) && !match_byte(2))
447 match_byte_assert(3);
449 else if (match_byte (2))
455 dump_value_31 (stream);
457 value = get_double ();
459 vallab = get_string ();
460 fprintf (stream, "<numeric-datum value=\"%.*g\" format=\"%s%d.%d\"",
461 DBL_DIG, value, format_to_string(format >> 16), (format >> 8) & 0xff, format & 0xff);
463 fprintf (stream, " variable=\"%s\"", var);
465 fprintf (stream, " label=\"%s\"/>\n", vallab);
466 fprintf (stream, "/>\n");
467 if (!match_byte (1) && !match_byte(2))
468 match_byte_assert (3);
470 else if (match_byte (4))
473 char *var, *vallab, *value;
475 match_byte_assert (0x58);
477 vallab = get_string ();
479 if (!match_byte(1) && !match_byte(2))
480 match_byte_assert (3);
481 value = get_string ();
482 fprintf (stream, "<string-datum value=\"%s\" format=\"%s%d.%d\"",
483 value, format_to_string(format >> 16), (format >> 8) & 0xff, format & 0xff);
485 fprintf (stream, " variable=\"%s\"", var);
487 fprintf (stream, " label=\"%s\"/>\n", vallab);
488 fprintf (stream, "/>\n");
490 else if (match_byte (1))
495 dump_value_31(stream);
497 value = get_double ();
498 fprintf (stream, "<number value=\"%.*g\" format=\"%s%d.%d\"/>\n",
499 DBL_DIG, value, format_to_string(format >> 16), (format >> 8) & 0xff, format & 0xff);
505 dump_value_31(stream);
507 char *base = get_string();
509 fprintf (stream, "<template format=\"%s\">\n", base);
510 for (int i = 0; i < x; i++)
517 for (int j = 0; j <= level + 1; j++)
518 fprintf (stream, " ");
519 fprintf (stream, "<substitution index=\"%d\">\n", i + 1);
520 for (int j = 0; j < y; j++)
521 dump_value (stream, level + 2, false);
522 for (int j = 0; j <= level + 1; j++)
523 fprintf (stream, " ");
524 fprintf (stream, "</substitution>\n");
526 for (int j = 0; j <= level; j++)
527 fprintf (stream, " ");
528 fprintf (stream, "</template>\n");
533 compare_int(const void *a_, const void *b_)
537 return *a < *b ? -1 : *a > *b;
541 check_permutation(int *a, int n, const char *name)
544 memcpy(b, a, n * sizeof *a);
545 qsort(b, n, sizeof *b, compare_int);
546 for (int i = 0; i < n; i++)
549 fprintf(stderr, "bad %s permutation:", name);
550 for (int i = 0; i < n; i++)
551 fprintf(stderr, " %d", a[i]);
558 dump_category(int level, int *indexes, int *n_indexes, int max_indexes)
560 for (int i = 0; i <= level; i++)
561 fprintf (stdout, " ");
562 printf ("<category>\n");
563 dump_value (stdout, level + 1, true);
570 else if (match_byte (1))
574 match_u32_assert (1);
577 else if (!match_u32(2))
578 match_u32_assert (0);
580 int indx = get_u32();
581 int n_categories = get_u32();
584 if (n_categories != 0)
586 fprintf(stderr, "index not -1 but subcategories\n");
589 if (*n_indexes >= max_indexes)
591 fprintf(stderr, "too many categories (increase max_indexes)\n");
594 indexes[(*n_indexes)++] = indx;
596 if (n_categories == 0)
598 for (int i = 0; i <= level + 1; i++)
599 fprintf (stdout, " ");
600 fprintf (stdout, "<category-index>%d</category-index>\n", indx);
602 for (int i = 0; i < n_categories; i++)
603 dump_category (level + 1, indexes, n_indexes, max_indexes);
604 for (int i = 0; i <= level; i++)
605 fprintf (stdout, " ");
606 printf ("</category>\n");
614 printf ("<dimension index=\"%d\">\n", indx);
615 dump_value (stdout, 0, false);
617 /* This byte is usually 0x02 but many other values have been spotted. */
620 if (!match_byte(0) && !match_byte(1))
621 match_byte_assert(2);
625 match_byte_assert(1);
627 match_byte_assert(1);
628 match_byte_assert(1);
629 if (!match_u32(UINT32_MAX))
630 match_u32_assert(indx);
631 n_categories = get_u32();
635 for (int i = 0; i < n_categories; i++)
636 dump_category (0, indexes, &n_indexes, sizeof indexes / sizeof *indexes);
637 check_permutation(indexes, n_indexes, "categories");
639 fprintf (stdout, "</dimension>\n");
647 for (int i = 0; i < n_dims; i++)
654 /* The first three numbers add to the number of dimensions. */
657 match_u32_assert(n_dims - t);
659 /* The next n_dims numbers are a permutation of the dimension numbers. */
661 for (int i = 0; i < n_dims; i++)
663 check_permutation(a, n_dims, "dimensions");
667 for (int i = 0; i < x; i++)
669 printf (" <datum index=\"%d\">\n", get_u32());
673 dump_value(stdout, 1, false);
674 fprintf (stdout, " </datum>\n");
676 printf ("</data>\n");
683 printf ("<title-local>\n");
684 dump_value(stdout, 0, true);
685 printf ("</title-local>\n");
687 printf ("<subtype>\n");
688 dump_value(stdout, 0, true);
689 printf ("</subtype>\n");
691 match_byte_assert(0x31);
693 printf ("<title-c>\n");
694 dump_value(stdout, 0, true);
695 printf ("</title-c>\n");
698 match_byte_assert(0x58);
699 if (match_byte(0x31))
701 printf ("<caption>\n");
702 dump_value(stdout, 0, false);
703 printf ("</caption>\n");
706 match_byte_assert(0x58);
709 int n_footnotes = get_u32();
710 for (int i = 0; i < n_footnotes; i++)
712 printf ("<footnote index=\"%d\">\n", i);
713 dump_value(stdout, 0, false);
714 if (match_byte (0x31))
716 /* Custom footnote marker string. */
717 match_byte_assert(3);
719 match_byte_assert(0x58);
724 match_byte_assert (0x58);
725 printf("(%d)\n", get_u32());
726 printf ("</footnote>\n");
734 for (int i = 1; i <= 8; i++)
736 printf ("<style index=\"%d\"", i);
737 match_byte_assert(i);
738 match_byte_assert(0x31);
739 printf(" font=\"%s\"", get_string());
740 match_byte_assert(0);
741 match_byte_assert(0);
742 if (!match_byte(0x40) && !match_byte(0x20) && !match_byte(0x80) && !match_byte(0x10) && !match_byte(0x70))
743 match_byte_assert(0x50);
744 if (!match_byte(0x41))
745 match_byte_assert(0x51);
746 if (!match_u32(0) && !match_u32(1))
748 match_byte_assert(0);
750 /* OK, this seems really unlikely to be totally correct, but it matches my corpus... */
751 if (!match_u32(0) && !match_u32(2))
752 match_u32_assert(0xfaad);
754 if (!match_u32(0) && !match_u32(1) && !match_u32(2))
756 printf (" fgcolor=\"%s\"", get_string());
757 printf (" bgcolor=\"%s\"", get_string());
760 match_byte_assert(0);
764 /* These seem unlikely to be correct too. */
769 if (!match_u32(10) && !match_u32(11) && !match_u32(5))
777 if (!match_u32(-1) && !match_u32(8))
778 match_u32_assert(24);
779 if (!match_u32(-1) && !match_u32(2))
783 /* Who knows? Ranges from -1 to 8 with no obvious pattern. */
790 match_u32_assert(240);
793 match_u32_assert(18);
798 else if (match_u32(142))
800 else if (match_u32(143))
802 else if (match_u32(150))
806 match_u32_assert(16);
810 int count = get_u32();
813 printf ("<encoding>%s</encoding>\n", get_string ());
816 match_u32_assert(UINT32_MAX);
818 match_byte_assert(1);
819 match_byte_assert(0);
821 match_byte_assert(1);
824 if (!match_byte(0x97) && !match_byte(0x98) && !match_byte(0x99))
825 match_byte_assert(0x9a);
826 match_byte_assert(7);
827 match_byte_assert(0);
828 match_byte_assert(0);
831 match_u32_assert(UINT32_MAX);
834 if (!match_byte(',') && !match_byte('\''))
835 match_byte_assert(' ');
839 match_byte_assert(',');
840 if (!match_byte('.') && !match_byte(' '))
841 match_byte_assert(0);
845 for (int i = 0; i < 5; i++)
850 int skip = get_u32();
855 main(int argc, char *argv[])
860 if (isatty(STDIN_FILENO))
862 fprintf(stderr, "redirect stdin from a .bin file\n");
865 if (fstat(STDIN_FILENO, &s))
877 if (read(STDIN_FILENO, data, n) != n)
885 fprintf (stderr, "usage: %s TYPE < .bin", argv[0]);
889 if (!strcmp(argv[1], "title0"))
892 if (match_byte (0x03)
893 || (match_byte (0x05) && match_byte (0x58)))
894 printf ("%s\n", get_string());
896 printf ("<unknown>\n");
899 else if (!strcmp(argv[1], "title"))
904 else if (!strcmp(argv[1], "titleraw"))
906 const char fonts[] = "\x01\x31\x09\0\0\0SansSerif";
908 n = find(fonts, sizeof fonts - 1);
910 else if (!strcmp(argv[1], "fonts"))
912 const char fonts[] = "\x01\x31\x09\0\0\0SansSerif";
913 const char styles[] = "\xf0\0\0\0";
914 start = find(fonts, sizeof fonts - 1);
915 n = find(styles, sizeof styles - 1);
917 else if (!strcmp(argv[1], "styles"))
919 const char styles[] = "\xf0\0\0\0";
920 const char dimensions[] = "-,,,.\0";
921 start = find(styles, sizeof styles - 1);
922 n = find(dimensions, sizeof dimensions - 1) + sizeof dimensions - 1;
924 else if (!strcmp(argv[1], "dimensions") || !strcmp(argv[1], "all"))
927 match_byte_assert(1);
928 match_byte_assert(0);
930 /* This might be a version number of some kind, because value 1 seems
931 to only appear in an SPV file that also required its own weird
932 special cases in dump_value_31(). */
938 match_byte_assert(1);
940 match_byte_assert(1);
941 match_byte_assert(0);
942 match_byte_assert(0);
944 match_byte_assert(1);
946 match_byte_assert(0);
947 match_byte_assert(0);
948 match_byte_assert(0);
956 fprintf (stderr, "%x / %x\n", pos, n);
961 else if (!strcmp(argv[1], "raw"))
965 dump_raw(stdout, start, n);
969 fprintf (stderr, "unknown section %s\n", argv[1]);