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)");
334 match_byte_assert(2);
335 match_byte_assert(0);
336 if (!match_u32 (2) && !match_u32(1))
338 dump_nested_string(); /* Our corpus doesn't contain any examples with strings though. */
343 fprintf(stream, "(special 3)");
344 match_byte_assert(0);
345 match_byte_assert(0);
346 match_byte_assert(1);
347 match_byte_assert(0);
349 dump_nested_string(); /* Our corpus doesn't contain any examples with strings though. */
353 match_byte_assert (0x58);
357 format_to_string (int type)
363 case 2: return "AHEX";
364 case 3: return "COMMA";
365 case 4: return "DOLLAR";
366 case 5: case 40: return "F";
368 case 7: return "PIBHEX";
370 case 9: return "PIB";
371 case 10: return "PK";
372 case 11: return "RB";
373 case 12: return "RBHEX";
377 case 20: return "DATE";
378 case 21: return "TIME";
379 case 22: return "DATETIME";
380 case 23: return "ADATE";
381 case 24: return "JDATE";
382 case 25: return "DTIME";
383 case 26: return "WKDAY";
384 case 27: return "MONTH";
385 case 28: return "MOYR";
386 case 29: return "QYR";
387 case 30: return "WKYR";
388 case 31: return "PCT";
389 case 32: return "DOT";
390 case 33: return "CCA";
391 case 34: return "CCB";
392 case 35: return "CCC";
393 case 36: return "CCD";
394 case 37: return "CCE";
395 case 38: return "EDATE";
396 case 39: return "SDATE";
399 sprintf(tmp, "<%d>", type);
405 dump_value(FILE *stream, int level, bool match1)
412 for (int i = 0; i <= level; i++)
413 fprintf (stream, " ");
417 char *text = get_string();
418 dump_value_31(stream);
419 char *identifier = get_string();
420 char *text_eng = get_string();
421 fprintf (stream, "<string c=\"%s\"", text_eng);
423 fprintf (stream, " identifier=\"%s\"", identifier);
424 if (strcmp(text_eng, text))
425 fprintf (stream, " local=\"%s\"", text);
426 fprintf (stream, "/>\n");
428 match_byte_assert(1);
432 else if (match_byte (5))
434 dump_value_31(stream);
435 char *name = get_string ();
436 char *label = get_string ();
437 fprintf (stream, "<variable name=\"%s\"", name);
439 fprintf (stream, " label=\"%s\"", label);
440 fprintf (stream, "/>\n");
441 if (!match_byte(1) && !match_byte(2))
442 match_byte_assert(3);
444 else if (match_byte (2))
450 dump_value_31 (stream);
452 value = get_double ();
454 vallab = get_string ();
455 fprintf (stream, "<numeric-datum value=\"%.*g\" format=\"%s%d.%d\"",
456 DBL_DIG, value, format_to_string(format >> 16), (format >> 8) & 0xff, format & 0xff);
458 fprintf (stream, " variable=\"%s\"", var);
460 fprintf (stream, " label=\"%s\"/>\n", vallab);
461 fprintf (stream, "/>\n");
462 if (!match_byte (1) && !match_byte(2))
463 match_byte_assert (3);
465 else if (match_byte (4))
468 char *var, *vallab, *value;
470 match_byte_assert (0x58);
472 vallab = get_string ();
474 if (!match_byte(1) && !match_byte(2))
475 match_byte_assert (3);
476 value = get_string ();
477 fprintf (stream, "<string-datum value=\"%s\" format=\"%s%d.%d\"",
478 value, format_to_string(format >> 16), (format >> 8) & 0xff, format & 0xff);
480 fprintf (stream, " variable=\"%s\"", var);
482 fprintf (stream, " label=\"%s\"/>\n", vallab);
483 fprintf (stream, "/>\n");
485 else if (match_byte (1))
490 dump_value_31(stream);
492 value = get_double ();
493 fprintf (stream, "<number value=\"%.*g\" format=\"%s%d.%d\"/>\n",
494 DBL_DIG, value, format_to_string(format >> 16), (format >> 8) & 0xff, format & 0xff);
500 dump_value_31(stream);
502 char *base = get_string();
504 fprintf (stream, "<template format=\"%s\">\n", base);
505 for (int i = 0; i < x; i++)
512 for (int j = 0; j <= level + 1; j++)
513 fprintf (stream, " ");
514 fprintf (stream, "<substitution index=\"%d\">\n", i + 1);
515 for (int j = 0; j < y; j++)
516 dump_value (stream, level + 2, false);
517 for (int j = 0; j <= level + 1; j++)
518 fprintf (stream, " ");
519 fprintf (stream, "</substitution>\n");
521 for (int j = 0; j <= level; j++)
522 fprintf (stream, " ");
523 fprintf (stream, "</template>\n");
528 compare_int(const void *a_, const void *b_)
532 return *a < *b ? -1 : *a > *b;
536 check_permutation(int *a, int n, const char *name)
539 memcpy(b, a, n * sizeof *a);
540 qsort(b, n, sizeof *b, compare_int);
541 for (int i = 0; i < n; i++)
544 fprintf(stderr, "bad %s permutation:", name);
545 for (int i = 0; i < n; i++)
546 fprintf(stderr, " %d", a[i]);
553 dump_category(int level, int *indexes, int *n_indexes, int max_indexes)
555 for (int i = 0; i <= level; i++)
556 fprintf (stdout, " ");
557 printf ("<category>\n");
558 dump_value (stdout, level + 1, true);
565 else if (match_byte (1))
569 match_u32_assert (1);
572 else if (!match_u32(2))
573 match_u32_assert (0);
575 int indx = get_u32();
576 int n_categories = get_u32();
579 if (n_categories != 0)
581 fprintf(stderr, "index not -1 but subcategories\n");
584 if (*n_indexes >= max_indexes)
586 fprintf(stderr, "too many categories (increase max_indexes)\n");
589 indexes[(*n_indexes)++] = indx;
591 if (n_categories == 0)
593 for (int i = 0; i <= level + 1; i++)
594 fprintf (stdout, " ");
595 fprintf (stdout, "<category-index>%d</category-index>\n", indx);
597 for (int i = 0; i < n_categories; i++)
598 dump_category (level + 1, indexes, n_indexes, max_indexes);
599 for (int i = 0; i <= level; i++)
600 fprintf (stdout, " ");
601 printf ("</category>\n");
609 printf ("<dimension index=\"%d\">\n", indx);
610 dump_value (stdout, 0, false);
612 /* This byte is usually 0x02 but many other values have been spotted. */
615 if (!match_byte(0) && !match_byte(1))
616 match_byte_assert(2);
620 match_byte_assert(1);
622 match_byte_assert(1);
623 match_byte_assert(1);
624 if (!match_u32(UINT32_MAX))
625 match_u32_assert(indx);
626 n_categories = get_u32();
630 for (int i = 0; i < n_categories; i++)
631 dump_category (0, indexes, &n_indexes, sizeof indexes / sizeof *indexes);
632 check_permutation(indexes, n_indexes, "categories");
634 fprintf (stdout, "</dimension>\n");
642 for (int i = 0; i < n_dims; i++)
649 /* The first three numbers add to the number of dimensions. */
652 match_u32_assert(n_dims - t);
654 /* The next n_dims numbers are a permutation of the dimension numbers. */
656 for (int i = 0; i < n_dims; i++)
658 check_permutation(a, n_dims, "dimensions");
662 for (int i = 0; i < x; i++)
664 printf (" <datum index=\"%d\">\n", get_u32());
666 dump_value(stdout, 1, false);
667 fprintf (stdout, " </datum>\n");
669 printf ("</data>\n");
676 printf ("<title-local>\n");
677 dump_value(stdout, 0, true);
678 printf ("</title-local>\n");
680 printf ("<subtype>\n");
681 dump_value(stdout, 0, true);
682 printf ("</subtype>\n");
684 match_byte_assert(0x31);
686 printf ("<title-c>\n");
687 dump_value(stdout, 0, true);
688 printf ("</title-c>\n");
691 match_byte_assert(0x58);
692 if (match_byte(0x31))
694 printf ("<caption>\n");
695 dump_value(stdout, 0, false);
696 printf ("</caption>\n");
699 match_byte_assert(0x58);
702 int n_footnotes = get_u32();
703 for (int i = 0; i < n_footnotes; i++)
705 printf ("<footnote index=\"%d\">\n", i);
706 dump_value(stdout, 0, false);
707 if (match_byte (0x31))
709 /* Custom footnote marker string. */
710 match_byte_assert(3);
712 match_byte_assert(0x58);
717 match_byte_assert (0x58);
718 printf("(%d)\n", get_u32());
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 printf ("<encoding>%s</encoding>\n", get_string ());
809 match_u32_assert(UINT32_MAX);
811 match_byte_assert(1);
812 match_byte_assert(0);
814 match_byte_assert(1);
817 if (!match_byte(0x97) && !match_byte(0x98) && !match_byte(0x99))
818 match_byte_assert(0x9a);
819 match_byte_assert(7);
820 match_byte_assert(0);
821 match_byte_assert(0);
824 match_u32_assert(UINT32_MAX);
827 if (!match_byte(','))
828 match_byte_assert(' ');
832 match_byte_assert(',');
833 if (!match_byte('.') && !match_byte(' '))
834 match_byte_assert(0);
838 for (int i = 0; i < 5; i++)
843 int skip = get_u32();
848 main(int argc, char *argv[])
853 if (isatty(STDIN_FILENO))
855 fprintf(stderr, "redirect stdin from a .bin file\n");
858 if (fstat(STDIN_FILENO, &s))
870 if (read(STDIN_FILENO, data, n) != n)
878 if (!strcmp(argv[1], "title0"))
881 if (match_byte (0x03)
882 || (match_byte (0x05) && match_byte (0x58)))
883 printf ("%s\n", get_string());
885 printf ("<unknown>\n");
888 else if (!strcmp(argv[1], "title"))
893 else if (!strcmp(argv[1], "titleraw"))
895 const char fonts[] = "\x01\x31\x09\0\0\0SansSerif";
897 n = find(fonts, sizeof fonts - 1);
899 else if (!strcmp(argv[1], "fonts"))
901 const char fonts[] = "\x01\x31\x09\0\0\0SansSerif";
902 const char styles[] = "\xf0\0\0\0";
903 start = find(fonts, sizeof fonts - 1);
904 n = find(styles, sizeof styles - 1);
906 else if (!strcmp(argv[1], "styles"))
908 const char styles[] = "\xf0\0\0\0";
909 const char dimensions[] = "-,,,.\0";
910 start = find(styles, sizeof styles - 1);
911 n = find(dimensions, sizeof dimensions - 1) + sizeof dimensions - 1;
913 else if (!strcmp(argv[1], "dimensions") || !strcmp(argv[1], "all"))
916 match_byte_assert(1);
917 match_byte_assert(0);
919 /* This might be a version number of some kind, because value 1 seems
920 to only appear in an SPV file that also required its own weird
921 special cases in dump_value_31(). */
927 match_byte_assert(1);
929 match_byte_assert(1);
930 match_byte_assert(0);
931 match_byte_assert(0);
933 match_byte_assert(1);
935 match_byte_assert(0);
936 match_byte_assert(0);
937 match_byte_assert(0);
945 fprintf (stderr, "%x / %x\n", pos, n);
952 fprintf (stderr, "unknown section %s\n", argv[1]);
959 dump_raw(stdout, start, n);