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
255 match_u32_assert(0x200);
256 match_u32_assert(0x1000000);
258 match_byte_assert(0);
262 int outer_end = pos + get_u32();
263 int inner_end = pos + get_u32();
264 if (pos != inner_end)
267 if (match_byte(0x31))
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...' */
275 match_byte_assert(0x58);
276 if (pos != inner_end)
278 fprintf(stderr, "inner end discrepancy\n");
283 if (match_byte(0x31))
285 /* Only one example in the corpus. */
290 match_byte_assert(1);
291 get_string(); /* foreground */
292 get_string(); /* background */
293 get_string(); /* font */
295 match_byte_assert(12); /* size? */
298 match_byte_assert(0x58);
299 if (match_byte(0x31))
301 /* Only two SPV files have anything like this, so it's hard to
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);
318 match_byte_assert(0x58);
319 if (pos != outer_end)
321 fprintf(stderr, "outer end discrepancy\n");
325 else if (match_u32 (1))
327 fprintf(stream, "(footnote %d) ", get_u32());
328 dump_nested_string();
330 else if (match_u32 (2))
332 fprintf(stream, "(special 2)");
333 match_byte_assert(0);
334 match_byte_assert(0);
337 dump_nested_string(); /* Our corpus doesn't contain any examples with strings though. */
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);
348 dump_nested_string(); /* Our corpus doesn't contain any examples with strings though. */
352 match_byte_assert (0x58);
356 format_to_string (int type)
362 case 2: return "AHEX";
363 case 3: return "COMMA";
364 case 4: return "DOLLAR";
365 case 5: case 40: return "F";
367 case 7: return "PIBHEX";
369 case 9: return "PIB";
370 case 10: return "PK";
371 case 11: return "RB";
372 case 12: return "RBHEX";
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";
398 sprintf(tmp, "<%d>", type);
404 dump_value(FILE *stream, int level, bool match1)
411 for (int i = 0; i <= level; i++)
412 fprintf (stream, " ");
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);
422 fprintf (stream, " identifier=\"%s\"", identifier);
423 if (strcmp(text_eng, text))
424 fprintf (stream, " local=\"%s\"", text);
425 fprintf (stream, "/>\n");
427 match_byte_assert(1);
431 else if (match_byte (5))
433 dump_value_31(stream);
434 char *name = get_string ();
435 char *label = get_string ();
436 fprintf (stream, "<variable name=\"%s\"", name);
438 fprintf (stream, " label=\"%s\"", label);
439 fprintf (stream, "/>\n");
440 if (!match_byte(1) && !match_byte(2))
441 match_byte_assert(3);
443 else if (match_byte (2))
449 dump_value_31 (stream);
451 value = get_double ();
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);
457 fprintf (stream, " variable=\"%s\"", var);
459 fprintf (stream, " label=\"%s\"/>\n", vallab);
460 fprintf (stream, "/>\n");
461 if (!match_byte (1) && !match_byte(2))
462 match_byte_assert (3);
464 else if (match_byte (4))
467 char *var, *vallab, *value;
469 match_byte_assert (0x58);
471 vallab = 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);
479 fprintf (stream, " variable=\"%s\"", var);
481 fprintf (stream, " label=\"%s\"/>\n", vallab);
482 fprintf (stream, "/>\n");
484 else if (match_byte (1))
489 dump_value_31(stream);
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);
499 dump_value_31(stream);
501 char *base = get_string();
503 fprintf (stream, "<template format=\"%s\">\n", base);
504 for (int i = 0; i < x; i++)
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");
520 for (int j = 0; j <= level; j++)
521 fprintf (stream, " ");
522 fprintf (stream, "</template>\n");
527 compare_int(const void *a_, const void *b_)
531 return *a < *b ? -1 : *a > *b;
535 check_permutation(int *a, int n, const char *name)
538 memcpy(b, a, n * sizeof *a);
539 qsort(b, n, sizeof *b, compare_int);
540 for (int i = 0; i < n; i++)
543 fprintf(stderr, "bad %s permutation:", name);
544 for (int i = 0; i < n; i++)
545 fprintf(stderr, " %d", a[i]);
552 dump_category(int level, int *indexes, int *n_indexes, int max_indexes)
554 for (int i = 0; i <= level; i++)
555 fprintf (stdout, " ");
556 printf ("<category>\n");
557 dump_value (stdout, level + 1, true);
564 else if (match_byte (1))
568 match_u32_assert (1);
571 else if (!match_u32(2))
572 match_u32_assert (0);
574 int indx = get_u32();
575 int n_categories = get_u32();
578 if (n_categories != 0)
580 fprintf(stderr, "index not -1 but subcategories\n");
583 if (*n_indexes >= max_indexes)
585 fprintf(stderr, "too many categories (increase max_indexes)\n");
588 indexes[(*n_indexes)++] = indx;
590 if (n_categories == 0)
592 for (int i = 0; i <= level + 1; i++)
593 fprintf (stdout, " ");
594 fprintf (stdout, "<category-index>%d</category-index>\n", indx);
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");
608 printf ("<dimension index=\"%d\">\n", indx);
609 dump_value (stdout, 0, false);
611 /* This byte is usually 0x02 but many other values have been spotted. */
614 if (!match_byte(0) && !match_byte(1))
615 match_byte_assert(2);
619 match_byte_assert(1);
621 match_byte_assert(1);
622 match_byte_assert(1);
623 if (!match_u32(UINT32_MAX))
624 match_u32_assert(indx);
625 n_categories = get_u32();
629 for (int i = 0; i < n_categories; i++)
630 dump_category (0, indexes, &n_indexes, sizeof indexes / sizeof *indexes);
631 check_permutation(indexes, n_indexes, "categories");
633 fprintf (stdout, "</dimension>\n");
641 for (int i = 0; i < n_dims; i++)
648 /* The first three numbers add to the number of dimensions. */
651 match_u32_assert(n_dims - t);
653 /* The next n_dims numbers are a permutation of the dimension numbers. */
655 for (int i = 0; i < n_dims; i++)
657 check_permutation(a, n_dims, "dimensions");
661 for (int i = 0; i < x; i++)
663 printf (" <datum index=\"%d\">\n", get_u32());
665 dump_value(stdout, 1, false);
666 fprintf (stdout, " </datum>\n");
668 printf ("</data>\n");
675 printf ("<title-local>\n");
676 dump_value(stdout, 0, true);
677 printf ("</title-local>\n");
679 printf ("<subtype>\n");
680 dump_value(stdout, 0, true);
681 printf ("</subtype>\n");
683 match_byte_assert(0x31);
685 printf ("<title-c>\n");
686 dump_value(stdout, 0, true);
687 printf ("</title-c>\n");
690 match_byte_assert(0x58);
691 if (match_byte(0x31))
693 printf ("<caption>\n");
694 dump_value(stdout, 0, false);
695 printf ("</caption>\n");
698 match_byte_assert(0x58);
701 int n_footnotes = get_u32();
702 for (int i = 0; i < n_footnotes; i++)
704 printf ("<footnote index=\"%d\">\n", i);
705 dump_value(stdout, 0, false);
706 if (match_byte (0x31))
708 /* Custom footnote marker string. */
709 match_byte_assert(3);
711 match_byte_assert(0x58);
716 match_byte_assert (0x58);
717 printf("(%d)\n", get_u32());
718 printf ("</footnote>\n");
726 for (int i = 1; i <= 8; i++)
728 printf ("<style index=\"%d\"", i);
729 match_byte_assert(i);
730 match_byte_assert(0x31);
731 printf(" font=\"%s\"", get_string());
732 match_byte_assert(0);
733 match_byte_assert(0);
734 if (!match_byte(0x40) && !match_byte(0x20) && !match_byte(0x80) && !match_byte(0x10) && !match_byte(0x70))
735 match_byte_assert(0x50);
736 if (!match_byte(0x41))
737 match_byte_assert(0x51);
738 if (!match_u32(0) && !match_u32(1))
740 match_byte_assert(0);
742 /* OK, this seems really unlikely to be totally correct, but it matches my corpus... */
743 if (!match_u32(0) && !match_u32(2))
744 match_u32_assert(0xfaad);
746 if (!match_u32(0) && !match_u32(1) && !match_u32(2))
748 printf (" fgcolor=\"%s\"", get_string());
749 printf (" bgcolor=\"%s\"", get_string());
752 match_byte_assert(0);
756 /* These seem unlikely to be correct too. */
761 if (!match_u32(10) && !match_u32(11) && !match_u32(5))
769 if (!match_u32(-1) && !match_u32(8))
770 match_u32_assert(24);
771 if (!match_u32(-1) && !match_u32(2))
775 /* Who knows? Ranges from -1 to 8 with no obvious pattern. */
782 match_u32_assert(240);
785 match_u32_assert(18);
790 else if (match_u32(142))
792 else if (match_u32(143))
794 else if (match_u32(150))
798 match_u32_assert(16);
802 int count = get_u32();
805 printf ("<encoding>%s</encoding>\n", get_string ());
808 match_u32_assert(UINT32_MAX);
810 match_byte_assert(1);
811 match_byte_assert(0);
813 match_byte_assert(1);
816 if (!match_byte(0x97) && !match_byte(0x98) && !match_byte(0x99))
817 match_byte_assert(0x9a);
818 match_byte_assert(7);
819 match_byte_assert(0);
820 match_byte_assert(0);
823 match_u32_assert(UINT32_MAX);
826 if (!match_byte(','))
827 match_byte_assert(' ');
831 match_byte_assert(',');
832 if (!match_byte('.') && !match_byte(' '))
833 match_byte_assert(0);
837 for (int i = 0; i < 5; i++)
842 int skip = get_u32();
847 main(int argc, char *argv[])
852 if (isatty(STDIN_FILENO))
854 fprintf(stderr, "redirect stdin from a .bin file\n");
857 if (fstat(STDIN_FILENO, &s))
869 if (read(STDIN_FILENO, data, n) != n)
877 if (!strcmp(argv[1], "title0"))
880 if (match_byte (0x03)
881 || (match_byte (0x05) && match_byte (0x58)))
882 printf ("%s\n", get_string());
884 printf ("<unknown>\n");
887 else if (!strcmp(argv[1], "title"))
892 else if (!strcmp(argv[1], "titleraw"))
894 const char fonts[] = "\x01\x31\x09\0\0\0SansSerif";
896 n = find(fonts, sizeof fonts - 1);
898 else if (!strcmp(argv[1], "fonts"))
900 const char fonts[] = "\x01\x31\x09\0\0\0SansSerif";
901 const char styles[] = "\xf0\0\0\0";
902 start = find(fonts, sizeof fonts - 1);
903 n = find(styles, sizeof styles - 1);
905 else if (!strcmp(argv[1], "styles"))
907 const char styles[] = "\xf0\0\0\0";
908 const char dimensions[] = "-,,,.\0";
909 start = find(styles, sizeof styles - 1);
910 n = find(dimensions, sizeof dimensions - 1) + sizeof dimensions - 1;
912 else if (!strcmp(argv[1], "dimensions") || !strcmp(argv[1], "all"))
915 match_byte_assert(1);
916 match_byte_assert(0);
918 /* This might be a version number of some kind, because value 1 seems
919 to only appear in an SPV file that also required its own weird
920 special cases in dump_value_31(). */
926 match_byte_assert(1);
928 match_byte_assert(1);
929 match_byte_assert(0);
930 match_byte_assert(0);
932 match_byte_assert(1);
934 match_byte_assert(0);
935 match_byte_assert(0);
936 match_byte_assert(0);
944 fprintf (stderr, "%x / %x\n", pos, n);
951 fprintf (stderr, "unknown section %s\n", argv[1]);
958 dump_raw(stdout, start, n);