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 /* All the examples in the corpus, all from one SPV file, are
302 exactly like this. */
307 match_byte_assert(1);
308 match_byte_assert(0);
309 match_byte_assert(8);
310 match_byte_assert(0);
311 match_byte_assert(8);
312 match_byte_assert(0);
313 match_byte_assert(10);
314 match_byte_assert(0);
317 match_byte_assert(0x58);
318 if (pos != outer_end)
320 fprintf(stderr, "outer end discrepancy\n");
324 else if (match_u32 (1))
326 fprintf(stream, "(footnote %d) ", get_u32());
327 dump_nested_string();
329 else if (match_u32 (2))
331 fprintf(stream, "(special 2)");
332 match_byte_assert(0);
333 match_byte_assert(0);
336 dump_nested_string(); /* Our corpus doesn't contain any examples with strings though. */
341 fprintf(stream, "(special 3)");
342 match_byte_assert(0);
343 match_byte_assert(0);
344 match_byte_assert(1);
345 match_byte_assert(0);
347 dump_nested_string(); /* Our corpus doesn't contain any examples with strings though. */
351 match_byte_assert (0x58);
355 format_to_string (int type)
361 case 2: return "AHEX";
362 case 3: return "COMMA";
363 case 4: return "DOLLAR";
364 case 5: case 40: return "F";
366 case 7: return "PIBHEX";
368 case 9: return "PIB";
369 case 10: return "PK";
370 case 11: return "RB";
371 case 12: return "RBHEX";
375 case 20: return "DATE";
376 case 21: return "TIME";
377 case 22: return "DATETIME";
378 case 23: return "ADATE";
379 case 24: return "JDATE";
380 case 25: return "DTIME";
381 case 26: return "WKDAY";
382 case 27: return "MONTH";
383 case 28: return "MOYR";
384 case 29: return "QYR";
385 case 30: return "WKYR";
386 case 31: return "PCT";
387 case 32: return "DOT";
388 case 33: return "CCA";
389 case 34: return "CCB";
390 case 35: return "CCC";
391 case 36: return "CCD";
392 case 37: return "CCE";
393 case 38: return "EDATE";
394 case 39: return "SDATE";
397 sprintf(tmp, "<%d>", type);
403 dump_value(FILE *stream, int level, bool match1)
410 for (int i = 0; i <= level; i++)
411 fprintf (stream, " ");
415 char *text = get_string();
416 dump_value_31(stream);
417 char *identifier = get_string();
418 char *text_eng = get_string();
419 fprintf (stream, "<string c=\"%s\"", text_eng);
421 fprintf (stream, " identifier=\"%s\"", identifier);
422 if (strcmp(text_eng, text))
423 fprintf (stream, " local=\"%s\"", text);
424 fprintf (stream, "/>\n");
426 match_byte_assert(1);
430 else if (match_byte (5))
432 dump_value_31(stream);
433 char *name = get_string ();
434 char *label = get_string ();
435 fprintf (stream, "<variable name=\"%s\"", name);
437 fprintf (stream, " label=\"%s\"", label);
438 fprintf (stream, "/>\n");
439 if (!match_byte(1) && !match_byte(2))
440 match_byte_assert(3);
442 else if (match_byte (2))
448 match_byte_assert (0x58);
450 value = get_double ();
452 vallab = get_string ();
453 fprintf (stream, "<numeric-datum value=\"%.*g\" format=\"%s%d.%d\"",
454 DBL_DIG, value, format_to_string(format >> 16), (format >> 8) & 0xff, format & 0xff);
456 fprintf (stream, " variable=\"%s\"", var);
458 fprintf (stream, " label=\"%s\"/>\n", vallab);
459 fprintf (stream, "/>\n");
460 if (!match_byte (1) && !match_byte(2))
461 match_byte_assert (3);
463 else if (match_byte (4))
466 char *var, *vallab, *value;
468 match_byte_assert (0x58);
470 vallab = get_string ();
472 if (!match_byte(1) && !match_byte(2))
473 match_byte_assert (3);
474 value = get_string ();
475 fprintf (stream, "<string-datum value=\"%s\" format=\"%s%d.%d\"",
476 value, format_to_string(format >> 16), (format >> 8) & 0xff, format & 0xff);
478 fprintf (stream, " variable=\"%s\"", var);
480 fprintf (stream, " label=\"%s\"/>\n", vallab);
481 fprintf (stream, "/>\n");
483 else if (match_byte (1))
488 dump_value_31(stream);
490 value = get_double ();
491 fprintf (stream, "<number value=\"%.*g\" format=\"%s%d.%d\"/>\n",
492 DBL_DIG, value, format_to_string(format >> 16), (format >> 8) & 0xff, format & 0xff);
498 dump_value_31(stream);
500 char *base = get_string();
502 fprintf (stream, "<template format=\"%s\">\n", base);
503 for (int i = 0; i < x; i++)
510 for (int j = 0; j <= level + 1; j++)
511 fprintf (stream, " ");
512 fprintf (stream, "<substitution index=\"%d\">\n", i + 1);
513 for (int j = 0; j < y; j++)
514 dump_value (stream, level + 2, false);
515 for (int j = 0; j <= level + 1; j++)
516 fprintf (stream, " ");
517 fprintf (stream, "</substitution>\n");
519 for (int j = 0; j <= level; j++)
520 fprintf (stream, " ");
521 fprintf (stream, "</template>\n");
526 compare_int(const void *a_, const void *b_)
530 return *a < *b ? -1 : *a > *b;
534 check_permutation(int *a, int n, const char *name)
537 memcpy(b, a, n * sizeof *a);
538 qsort(b, n, sizeof *b, compare_int);
539 for (int i = 0; i < n; i++)
542 fprintf(stderr, "bad %s permutation:", name);
543 for (int i = 0; i < n; i++)
544 fprintf(stderr, " %d", a[i]);
551 dump_category(int level, int *indexes, int *n_indexes)
553 for (int i = 0; i <= level; i++)
554 fprintf (stdout, " ");
555 printf ("<category>\n");
556 dump_value (stdout, level + 1, true);
563 else if (match_byte (1))
567 match_u32_assert (1);
570 else if (!match_u32(2))
571 match_u32_assert (0);
573 int indx = get_u32();
574 int n_categories = get_u32();
577 if (n_categories != 0)
579 fprintf(stderr, "index not -1 but subcategories\n");
582 indexes[(*n_indexes)++] = indx;
584 if (n_categories == 0)
586 for (int i = 0; i <= level + 1; i++)
587 fprintf (stdout, " ");
588 fprintf (stdout, "<category-index>%d</category-index>\n", indx);
590 for (int i = 0; i < n_categories; i++)
591 dump_category (level + 1, indexes, n_indexes);
592 for (int i = 0; i <= level; i++)
593 fprintf (stdout, " ");
594 printf ("</category>\n");
602 printf ("<dimension index=\"%d\">\n", indx);
603 dump_value (stdout, 0, false);
605 /* This byte is usually 0x02 but 0x00 and 0x75 (!) have also been spotted. */
608 if (!match_byte(0) && !match_byte(1))
609 match_byte_assert(2);
613 match_byte_assert(1);
615 match_byte_assert(1);
616 match_byte_assert(1);
617 match_u32_assert(indx);
618 n_categories = get_u32();
622 for (int i = 0; i < n_categories; i++)
623 dump_category (0, indexes, &n_indexes);
624 check_permutation(indexes, n_indexes, "categories");
626 fprintf (stdout, "</dimension>\n");
634 for (int i = 0; i < n_dims; i++)
641 /* The first three numbers add to the number of dimensions. */
644 match_u32_assert(n_dims - t);
646 /* The next n_dims numbers are a permutation of the dimension numbers. */
648 for (int i = 0; i < n_dims; i++)
650 check_permutation(a, n_dims, "dimensions");
654 for (int i = 0; i < x; i++)
656 printf (" <datum index=\"%d\">\n", get_u32());
658 dump_value(stdout, 1, false);
659 fprintf (stdout, " </datum>\n");
661 printf ("</data>\n");
668 printf ("<title-local>\n");
669 dump_value(stdout, 0, true);
670 printf ("</title-local>\n");
672 printf ("<subtype>\n");
673 dump_value(stdout, 0, true);
674 printf ("</subtype>\n");
676 match_byte_assert(0x31);
678 printf ("<title-c>\n");
679 dump_value(stdout, 0, true);
680 printf ("</title-c>\n");
683 match_byte_assert(0x58);
684 if (match_byte(0x31))
686 printf ("<caption>\n");
687 dump_value(stdout, 0, false);
688 printf ("</caption>\n");
691 match_byte_assert(0x58);
694 int n_footnotes = get_u32();
695 for (int i = 0; i < n_footnotes; i++)
697 printf ("<footnote index=\"%d\">\n", i);
698 dump_value(stdout, 0, false);
699 if (match_byte (0x31))
701 /* Custom footnote marker string. */
702 match_byte_assert(3);
704 match_byte_assert(0x58);
709 match_byte_assert (0x58);
710 printf("(%d)\n", get_u32());
711 printf ("</footnote>\n");
719 for (int i = 1; i <= 8; i++)
721 printf ("<style index=\"%d\"", i);
722 match_byte_assert(i);
723 match_byte_assert(0x31);
724 printf(" font=\"%s\"", get_string());
725 match_byte_assert(0);
726 match_byte_assert(0);
727 if (!match_byte(0x40) && !match_byte(0x20) && !match_byte(0x80) && !match_byte(0x10) && !match_byte(0x70))
728 match_byte_assert(0x50);
729 if (!match_byte(0x41))
730 match_byte_assert(0x51);
731 if (!match_u32(0) && !match_u32(1))
733 match_byte_assert(0);
735 /* OK, this seems really unlikely to be totally correct, but it matches my corpus... */
736 if (!match_u32(0) && !match_u32(2))
737 match_u32_assert(0xfaad);
739 if (!match_u32(0) && !match_u32(1) && !match_u32(2))
741 printf (" fgcolor=\"%s\"", get_string());
742 printf (" bgcolor=\"%s\"", get_string());
745 match_byte_assert(0);
749 /* These seem unlikely to be correct too. */
754 if (!match_u32(10) && !match_u32(11) && !match_u32(5))
762 if (!match_u32(-1) && !match_u32(8))
763 match_u32_assert(24);
764 if (!match_u32(-1) && !match_u32(2))
768 /* Who knows? Ranges from -1 to 8 with no obvious pattern. */
775 match_u32_assert(240);
778 match_u32_assert(18);
783 else if (match_u32(142))
785 else if (match_u32(143))
787 else if (match_u32(150))
791 match_u32_assert(16);
795 int count = get_u32();
798 printf ("<encoding>%s</encoding>\n", get_string ());
801 match_u32_assert(UINT32_MAX);
803 match_byte_assert(1);
804 match_byte_assert(0);
806 match_byte_assert(1);
809 if (!match_byte(0x97) && !match_byte(0x98) && !match_byte(0x99))
810 match_byte_assert(0x9a);
811 match_byte_assert(7);
812 match_byte_assert(0);
813 match_byte_assert(0);
816 match_u32_assert(UINT32_MAX);
819 if (!match_byte(','))
820 match_byte_assert(' ');
824 match_byte_assert(',');
825 if (!match_byte('.') && !match_byte(' '))
826 match_byte_assert(0);
830 for (int i = 0; i < 5; i++)
835 int skip = get_u32();
840 main(int argc, char *argv[])
845 if (isatty(STDIN_FILENO))
847 fprintf(stderr, "redirect stdin from a .bin file\n");
850 if (fstat(STDIN_FILENO, &s))
862 if (read(STDIN_FILENO, data, n) != n)
870 if (!strcmp(argv[1], "title0"))
873 if (match_byte (0x03)
874 || (match_byte (0x05) && match_byte (0x58)))
875 printf ("%s\n", get_string());
877 printf ("<unknown>\n");
880 else if (!strcmp(argv[1], "title"))
885 else if (!strcmp(argv[1], "titleraw"))
887 const char fonts[] = "\x01\x31\x09\0\0\0SansSerif";
889 n = find(fonts, sizeof fonts - 1);
891 else if (!strcmp(argv[1], "fonts"))
893 const char fonts[] = "\x01\x31\x09\0\0\0SansSerif";
894 const char styles[] = "\xf0\0\0\0";
895 start = find(fonts, sizeof fonts - 1);
896 n = find(styles, sizeof styles - 1);
898 else if (!strcmp(argv[1], "styles"))
900 const char styles[] = "\xf0\0\0\0";
901 const char dimensions[] = "-,,,.\0";
902 start = find(styles, sizeof styles - 1);
903 n = find(dimensions, sizeof dimensions - 1) + sizeof dimensions - 1;
905 else if (!strcmp(argv[1], "dimensions") || !strcmp(argv[1], "all"))
908 match_byte_assert(1);
909 match_byte_assert(0);
911 /* This might be a version number of some kind, because value 1 seems
912 to only appear in an SPV file that also required its own weird
913 special cases in dump_value_31(). */
919 match_byte_assert(1);
921 match_byte_assert(1);
922 match_byte_assert(0);
923 match_byte_assert(0);
925 match_byte_assert(1);
927 match_byte_assert(0);
928 match_byte_assert(0);
929 match_byte_assert(0);
937 fprintf (stderr, "%x / %x\n", pos, n);
944 fprintf (stderr, "unknown section %s\n", argv[1]);
951 dump_raw(stdout, start, n);