14 all_ascii(const uint8_t *p, size_t n)
16 for (size_t i = 0; i < n; i++)
17 if (p[i] < 32 || p[i] > 126)
23 try_find(const char *target, size_t target_len)
25 const uint8_t *pos = (const uint8_t *) memmem (data, n, target, target_len);
26 return pos ? pos - data : 0;
30 find(const char *target, size_t target_len)
32 size_t pos = try_find(target, target_len);
35 fprintf (stderr, "not found\n");
44 #define STR(x) XSTR(x)
45 #define WHERE __FILE__":" STR(__LINE__)
51 memcpy(&x, &data[pos], 4);
60 memcpy(&x, &data[pos], 8);
75 match_u32_assert(uint32_t x, const char *where)
77 unsigned int y = get_u32();
80 fprintf(stderr, "%s: 0x%x: expected i%u, got i%u\n", where, pos - 4, x, y);
84 #define match_u32_assert(x) match_u32_assert(x, WHERE)
89 if (pos < n && data[pos] == b)
99 match_byte_assert(uint8_t b, const char *where)
103 fprintf(stderr, "%s: 0x%x: expected %02x, got %02x\n", where, pos, b, data[pos]);
107 #define match_byte_assert(b) match_byte_assert(b, WHERE)
110 newline(FILE *stream, int pos)
112 fprintf(stream, "\n%08x: ", pos);
116 dump_raw(FILE *stream, int start, int end)
118 for (size_t i = start; i < end; )
125 && i + 4 + data[i] + data[i + 1] * 256 <= end
126 && all_ascii(&data[i + 4], data[i] + data[i + 1] * 256))
129 fprintf(stream, "\"");
130 fwrite(&data[i + 4], 1, data[i] + data[i + 1] * 256, stream);
131 fputs("\" ", stream);
133 i += 4 + data[i] + data[i + 1] * 256;
135 else if (i + 12 <= end
142 memcpy (&d, &data[i + 4], 8);
143 fprintf (stream, "F40.%d(%.*f)", data[i], data[i], d);
147 else if (i + 12 <= end
154 memcpy (&d, &data[i + 4], 8);
155 fprintf (stream, "PCT40.%d(%.*f)", data[i], data[i], d);
159 else if (i + 4 <= end
160 && (data[i] && data[i] != 88 && data[i] != 0x41)
165 fprintf (stream, "i%d ", data[i]);
170 fprintf(stream, "%02x ", data[i]);
179 get_string(const char *where)
182 /*data[pos + 1] == 0 && data[pos + 2] == 0 && data[pos + 3] == 0*/
183 /*&& all_ascii(&data[pos + 4], data[pos])*/)
185 int len = data[pos] + data[pos + 1] * 256;
186 char *s = malloc(len + 1);
188 memcpy(s, &data[pos + 4], len);
195 fprintf(stderr, "%s: 0x%x: expected string\n", where, pos);
199 #define get_string() get_string(WHERE)
202 dump_nested_string(void)
206 match_byte_assert (0);
207 match_byte_assert (0);
208 int outer_end = pos + get_u32();
209 int inner_end = pos + get_u32();
210 if (pos != inner_end)
213 if (match_byte(0x31))
216 match_byte_assert(0x58);
217 if (pos != inner_end)
219 fprintf(stderr, "inner end discrepancy\n");
223 match_byte_assert(0x58);
224 match_byte_assert(0x58);
225 if (pos != outer_end)
227 fprintf(stderr, "outer end discrepancy\n");
235 dump_value_31(FILE *stream)
237 if (match_byte (0x31))
243 /* Only "a" observed as a sample value (although it appears 44 times in the corpus). */
247 match_u32_assert (0);
249 /* This special case is really unsatisfying, but there are several
250 examples in the corpus, all from one SPV file. */
252 if (match_u32 (0x200) && match_u32(0x1000000) && match_u32(0)
257 int outer_end = pos + get_u32();
258 int inner_end = pos + get_u32();
259 if (pos != inner_end)
262 if (match_byte(0x31))
264 /* Appears to be a template string, e.g. '^1 cells (^2) expf < 5. Min exp = ^3...'.
265 Probably doesn't actually appear in output because many examples look unpolished,
266 e.g. 'partial list cases value ^1 shown upper...' */
270 match_byte_assert(0x58);
271 if (pos != inner_end)
273 fprintf(stderr, "inner end discrepancy\n");
278 if (match_byte(0x31))
280 /* Only one example in the corpus. */
285 match_byte_assert(1);
286 get_string(); /* foreground */
287 get_string(); /* background */
288 get_string(); /* font */
290 match_byte_assert(12); /* size? */
293 match_byte_assert(0x58);
294 if (match_byte(0x31))
296 /* All the examples in the corpus, all from one SPV file, are
297 exactly like this. */
302 match_byte_assert(1);
303 match_byte_assert(0);
304 match_byte_assert(8);
305 match_byte_assert(0);
306 match_byte_assert(8);
307 match_byte_assert(0);
308 match_byte_assert(10);
309 match_byte_assert(0);
312 match_byte_assert(0x58);
313 if (pos != outer_end)
315 fprintf(stderr, "outer end discrepancy\n");
319 else if (match_u32 (1))
321 fprintf(stream, "(footnote %d) ", get_u32());
322 dump_nested_string();
324 else if (match_u32 (2))
326 fprintf(stream, "(special 2)");
327 match_byte_assert(0);
328 match_byte_assert(0);
331 dump_nested_string(); /* Our corpus doesn't contain any examples with strings though. */
336 fprintf(stream, "(special 3)");
337 match_byte_assert(0);
338 match_byte_assert(0);
339 match_byte_assert(1);
340 match_byte_assert(0);
342 dump_nested_string(); /* Our corpus doesn't contain any examples with strings though. */
346 match_byte_assert (0x58);
350 format_to_string (int type)
356 case 2: return "AHEX";
357 case 3: return "COMMA";
358 case 4: return "DOLLAR";
359 case 5: case 40: return "F";
361 case 7: return "PIBHEX";
363 case 9: return "PIB";
364 case 10: return "PK";
365 case 11: return "RB";
366 case 12: return "RBHEX";
370 case 20: return "DATE";
371 case 21: return "TIME";
372 case 22: return "DATETIME";
373 case 23: return "ADATE";
374 case 24: return "JDATE";
375 case 25: return "DTIME";
376 case 26: return "WKDAY";
377 case 27: return "MONTH";
378 case 28: return "MOYR";
379 case 29: return "QYR";
380 case 30: return "WKYR";
381 case 31: return "PCT";
382 case 32: return "DOT";
383 case 33: return "CCA";
384 case 34: return "CCB";
385 case 35: return "CCC";
386 case 36: return "CCD";
387 case 37: return "CCE";
388 case 38: return "EDATE";
389 case 39: return "SDATE";
392 sprintf(tmp, "<%d>", type);
398 dump_value(FILE *stream, int level, bool match1)
405 for (int i = 0; i <= level; i++)
406 fprintf (stream, " ");
410 char *text = get_string();
411 dump_value_31(stream);
412 char *identifier = get_string();
413 char *text_eng = get_string();
414 fprintf (stream, "<string c=\"%s\"", text_eng);
416 fprintf (stream, " identifier=\"%s\"", identifier);
417 if (strcmp(text_eng, text))
418 fprintf (stream, " local=\"%s\"", text);
419 fprintf (stream, "/>\n");
421 match_byte_assert(1);
425 else if (match_byte (5))
427 dump_value_31(stream);
428 char *name = get_string ();
429 char *label = get_string ();
430 fprintf (stream, "<variable name=\"%s\"", name);
432 fprintf (stream, " label=\"%s\"", label);
433 fprintf (stream, "/>\n");
434 if (!match_byte(1) && !match_byte(2))
435 match_byte_assert(3);
437 else if (match_byte (2))
443 match_byte_assert (0x58);
445 value = get_double ();
447 vallab = get_string ();
448 fprintf (stream, "<numeric-datum value=\"%.*g\" format=\"%s%d.%d\"",
449 DBL_DIG, value, format_to_string(format >> 16), (format >> 8) & 0xff, format & 0xff);
451 fprintf (stream, " variable=\"%s\"", var);
453 fprintf (stream, " label=\"%s\"/>\n", vallab);
454 fprintf (stream, "/>\n");
455 if (!match_byte (1) && !match_byte(2))
456 match_byte_assert (3);
458 else if (match_byte (4))
461 char *var, *vallab, *value;
463 match_byte_assert (0x58);
465 vallab = get_string ();
467 if (!match_byte(1) && !match_byte(2))
468 match_byte_assert (3);
469 value = get_string ();
470 fprintf (stream, "<string-datum value=\"%s\" format=\"%s%d.%d\"",
471 value, format_to_string(format >> 16), (format >> 8) & 0xff, format & 0xff);
473 fprintf (stream, " variable=\"%s\"", var);
475 fprintf (stream, " label=\"%s\"/>\n", vallab);
476 fprintf (stream, "/>\n");
478 else if (match_byte (1))
483 dump_value_31(stream);
485 value = get_double ();
486 fprintf (stream, "<number value=\"%.*g\" format=\"%s%d.%d\"/>\n",
487 DBL_DIG, value, format_to_string(format >> 16), (format >> 8) & 0xff, format & 0xff);
493 dump_value_31(stream);
495 char *base = get_string();
497 fprintf (stream, "<template format=\"%s\">\n", base);
498 for (int i = 0; i < x; i++)
505 for (int j = 0; j <= level + 1; j++)
506 fprintf (stream, " ");
507 fprintf (stream, "<substitution index=\"%d\">\n", i + 1);
508 for (int j = 0; j < y; j++)
509 dump_value (stream, level + 2, false);
510 for (int j = 0; j <= level + 1; j++)
511 fprintf (stream, " ");
512 fprintf (stream, "</substitution>\n");
514 for (int j = 0; j <= level; j++)
515 fprintf (stream, " ");
516 fprintf (stream, "</template>\n");
521 compare_int(const void *a_, const void *b_)
525 return *a < *b ? -1 : *a > *b;
529 check_permutation(int *a, int n, const char *name)
532 memcpy(b, a, n * sizeof *a);
533 qsort(b, n, sizeof *b, compare_int);
534 for (int i = 0; i < n; i++)
537 fprintf(stderr, "bad %s permutation:", name);
538 for (int i = 0; i < n; i++)
539 fprintf(stderr, " %d", a[i]);
546 dump_category(int level, int *indexes, int *n_indexes)
548 for (int i = 0; i <= level; i++)
549 fprintf (stdout, " ");
550 printf ("<category>\n");
551 dump_value (stdout, level + 1, true);
558 else if (match_byte (1))
562 match_u32_assert (1);
565 else if (!match_u32(2))
566 match_u32_assert (0);
568 int indx = get_u32();
569 int n_categories = get_u32();
572 if (n_categories != 0)
574 fprintf(stderr, "index not -1 but subcategories\n");
577 indexes[(*n_indexes)++] = indx;
579 if (n_categories == 0)
581 for (int i = 0; i <= level + 1; i++)
582 fprintf (stdout, " ");
583 fprintf (stdout, "<category-index>%d</category-index>\n", indx);
585 for (int i = 0; i < n_categories; i++)
586 dump_category (level + 1, indexes, n_indexes);
587 for (int i = 0; i <= level; i++)
588 fprintf (stdout, " ");
589 printf ("</category>\n");
597 printf ("<dimension index=\"%d\">\n", indx);
598 dump_value (stdout, 0, false);
600 /* This byte is usually 0x02 but 0x00 and 0x75 (!) have also been spotted. */
603 if (!match_byte(0) && !match_byte(1))
604 match_byte_assert(2);
608 match_byte_assert(1);
610 match_byte_assert(1);
611 match_byte_assert(1);
612 match_u32_assert(indx);
613 n_categories = get_u32();
617 for (int i = 0; i < n_categories; i++)
618 dump_category (0, indexes, &n_indexes);
619 check_permutation(indexes, n_indexes, "categories");
621 fprintf (stdout, "</dimension>\n");
629 for (int i = 0; i < n_dims; i++)
636 /* The first three numbers add to the number of dimensions. */
639 match_u32_assert(n_dims - t);
641 /* The next n_dims numbers are a permutation of the dimension numbers. */
643 for (int i = 0; i < n_dims; i++)
645 check_permutation(a, n_dims, "dimensions");
649 for (int i = 0; i < x; i++)
651 printf (" <datum index=\"%d\">\n", get_u32());
653 dump_value(stdout, 1, false);
654 fprintf (stdout, " </datum>\n");
656 printf ("</data>\n");
663 printf ("<title-local>\n");
664 dump_value(stdout, 0, true);
665 printf ("</title-local>\n");
667 printf ("<subtype>\n");
668 dump_value(stdout, 0, true);
669 printf ("</subtype>\n");
671 match_byte_assert(0x31);
673 printf ("<title-c>\n");
674 dump_value(stdout, 0, true);
675 printf ("</title-c>\n");
678 match_byte_assert(0x58);
679 if (match_byte(0x31))
681 printf ("<caption>\n");
682 dump_value(stdout, 0, false);
683 printf ("</caption>\n");
686 match_byte_assert(0x58);
689 int n_footnotes = get_u32();
690 for (int i = 0; i < n_footnotes; i++)
692 printf ("<footnote index=\"%d\">\n", i);
693 dump_value(stdout, 0, false);
694 if (match_byte (0x31))
696 /* Custom footnote marker string. */
697 match_byte_assert(3);
699 match_byte_assert(0x58);
704 match_byte_assert (0x58);
705 printf("(%d)\n", get_u32());
706 printf ("</footnote>\n");
714 for (int i = 1; i <= 8; i++)
716 printf ("<style index=\"%d\"", i);
717 match_byte_assert(i);
718 match_byte_assert(0x31);
719 printf(" font=\"%s\"", get_string());
720 match_byte_assert(0);
721 match_byte_assert(0);
722 if (!match_byte(0x40) && !match_byte(0x20) && !match_byte(0x80) && !match_byte(0x10))
723 match_byte_assert(0x50);
724 if (!match_byte(0x41))
725 match_byte_assert(0x51);
728 match_byte_assert(0);
730 /* OK, this seems really unlikely to be totally correct, but it matches my corpus... */
731 if (!match_u32(0) && !match_u32(2))
732 match_u32_assert(0xfaad);
734 if (!match_u32(0) && !match_u32(1) && !match_u32(2))
736 printf (" fgcolor=\"%s\"", get_string());
737 printf (" bgcolor=\"%s\"", get_string());
740 match_byte_assert(0);
742 /* These seem unlikely to be correct too. */
747 match_u32_assert(11);
753 if (!match_u32(-1) && !match_u32(8))
754 match_u32_assert(24);
755 if (!match_u32(-1) && !match_u32(2))
759 /* Who knows? Ranges from -1 to 8 with no obvious pattern. */
765 match_u32_assert(240);
768 match_u32_assert(18);
775 match_u32_assert(142);
779 int count = get_u32();
782 printf ("<encoding>%s</encoding>\n", get_string ());
785 match_u32_assert(UINT32_MAX);
787 match_byte_assert(1);
788 match_byte_assert(0);
790 match_byte_assert(1);
791 if (!match_byte(0x97) && !match_byte(0x98) && !match_byte(0x99))
792 match_byte_assert(0x9a);
793 match_byte_assert(7);
794 match_byte_assert(0);
795 match_byte_assert(0);
797 match_byte_assert(',');
800 match_byte_assert(',');
801 if (!match_byte('.'))
802 match_byte_assert(' ');
805 for (int i = 0; i < 5; i++)
811 main(int argc, char *argv[])
816 if (isatty(STDIN_FILENO))
818 fprintf(stderr, "redirect stdin from a .bin file\n");
821 if (fstat(STDIN_FILENO, &s))
833 if (read(STDIN_FILENO, data, n) != n)
841 if (!strcmp(argv[1], "title0"))
844 if (match_byte (0x03)
845 || (match_byte (0x05) && match_byte (0x58)))
846 printf ("%s\n", get_string());
848 printf ("<unknown>\n");
851 else if (!strcmp(argv[1], "title"))
856 else if (!strcmp(argv[1], "titleraw"))
858 const char fonts[] = "\x01\x31\x09\0\0\0SansSerif";
860 n = find(fonts, sizeof fonts - 1);
862 else if (!strcmp(argv[1], "fonts"))
864 const char fonts[] = "\x01\x31\x09\0\0\0SansSerif";
865 const char styles[] = "\xf0\0\0\0";
866 start = find(fonts, sizeof fonts - 1);
867 n = find(styles, sizeof styles - 1);
869 else if (!strcmp(argv[1], "styles"))
871 const char styles[] = "\xf0\0\0\0";
872 const char dimensions[] = "-,,,.\0";
873 start = find(styles, sizeof styles - 1);
874 n = find(dimensions, sizeof dimensions - 1) + sizeof dimensions - 1;
876 else if (!strcmp(argv[1], "dimensions") || !strcmp(argv[1], "all"))
879 match_byte_assert(1);
880 match_byte_assert(0);
882 match_byte_assert(1);
884 match_byte_assert(1);
885 match_byte_assert(0);
886 match_byte_assert(0);
888 match_byte_assert(1);
890 match_byte_assert(0);
891 match_byte_assert(0);
892 match_byte_assert(0);
900 fprintf (stderr, "%x / %x\n", pos, n);
907 fprintf (stderr, "unknown section %s\n", argv[1]);
914 dump_raw(stdout, start, n);