13 all_ascii(const uint8_t *p, size_t n)
15 for (size_t i = 0; i < n; i++)
16 if (p[i] < 32 || p[i] > 126)
22 try_find(const char *target, size_t target_len)
24 const uint8_t *pos = (const uint8_t *) memmem (data, n, target, target_len);
25 return pos ? pos - data : 0;
29 try_find_tail(const char *target, size_t target_len)
31 size_t pos = try_find(target, target_len);
32 return pos ? pos + target_len : 0;
36 find(const char *target, size_t target_len)
38 size_t pos = try_find(target, target_len);
41 fprintf (stderr, "not found\n");
48 find_tail(const char *target, size_t target_len)
50 size_t pos = try_find_tail(target, target_len);
53 fprintf (stderr, "not found\n");
62 #define STR(x) XSTR(x)
63 #define WHERE __FILE__":" STR(__LINE__)
69 memcpy(&x, &data[pos], 4);
78 memcpy(&x, &data[pos], 8);
93 match_u32_assert(uint32_t x, const char *where)
95 unsigned int y = get_u32();
98 fprintf(stderr, "%s: 0x%x: expected i%u, got i%u\n", where, pos - 4, x, y);
102 #define match_u32_assert(x) match_u32_assert(x, WHERE)
105 match_byte(uint8_t b)
107 if (pos < n && data[pos] == b)
117 match_byte_assert(uint8_t b, const char *where)
121 fprintf(stderr, "%s: 0x%x: expected %02x, got %02x\n", where, pos, b, data[pos]);
125 #define match_byte_assert(b) match_byte_assert(b, WHERE)
128 dump_raw(FILE *stream, int start, int end, const char *separator)
130 for (size_t i = start; i < end; )
137 && i + 4 + data[i] + data[i + 1] * 256 <= end
138 && all_ascii(&data[i + 4], data[i] + data[i + 1] * 256))
140 fprintf(stream, "%s\"", separator);
141 fwrite(&data[i + 4], 1, data[i] + data[i + 1] * 256, stream);
142 fputs("\" ", stream);
144 i += 4 + data[i] + data[i + 1] * 256;
146 else if (i + 12 <= end
153 memcpy (&d, &data[i + 4], 8);
154 fprintf (stream, "F40.%d(%.*f)%s", data[i], data[i], d, separator);
157 else if (i + 12 <= end
164 memcpy (&d, &data[i + 4], 8);
165 fprintf (stream, "PCT40.%d(%.*f)%s", data[i], data[i], d, separator);
168 else if (i + 4 <= end
169 && (data[i] && data[i] != 88 && data[i] != 0x41)
174 fprintf (stream, "i%d ", data[i]);
179 fprintf(stream, "%02x ", data[i]);
188 get_string(const char *where)
191 /*data[pos + 1] == 0 && data[pos + 2] == 0 && data[pos + 3] == 0*/
192 /*&& all_ascii(&data[pos + 4], data[pos])*/)
194 int len = data[pos] + data[pos + 1] * 256;
195 char *s = malloc(len + 1);
197 memcpy(s, &data[pos + 4], len);
204 fprintf(stderr, "%s: 0x%x: expected string\n", where, pos);
208 #define get_string() get_string(WHERE)
211 dump_empty_nested(void)
213 int outer_end = pos + get_u32();
214 int inner_end = pos + get_u32();
215 if (pos != inner_end)
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 if (match_byte (0x31))
244 match_u32_assert (0);
246 int outer_end = pos + get_u32();
247 int inner_end = pos + get_u32();
249 if (match_byte(0x31))
252 match_byte_assert(0x58);
253 if (pos != inner_end)
255 fprintf(stderr, "inner end discrepancy\n");
259 if (match_byte(0x31))
264 match_byte_assert(1);
265 get_string(); /* foreground */
266 get_string(); /* background */
267 get_string(); /* font */
268 match_byte_assert(12); /* size? */
271 match_byte_assert(0x58);
272 match_byte_assert(0x58);
273 if (pos != outer_end)
275 fprintf(stderr, "outer end discrepancy\n");
279 else if (match_u32 (1))
281 printf("(footnote %d) ", get_u32());
282 match_byte_assert (0);
283 match_byte_assert (0);
285 int outer_end = pos + get_u32();
286 int inner_end = pos + get_u32();
287 if (pos != inner_end)
290 if (match_byte(0x31))
293 match_byte_assert(0x58);
294 if (pos != inner_end)
296 fprintf(stderr, "inner end discrepancy\n");
300 match_byte_assert(0x58);
301 match_byte_assert(0x58);
302 if (pos != outer_end)
304 fprintf(stderr, "outer end discrepancy\n");
308 else if (match_u32 (2))
310 printf("(special 2)");
311 match_byte_assert(0);
312 match_byte_assert(0);
315 match_byte_assert(0);
316 match_byte_assert(0);
322 printf("(special 3)");
323 match_byte_assert(0);
324 match_byte_assert(0);
325 match_byte_assert(1);
326 match_byte_assert(0);
328 match_byte_assert(0);
329 match_byte_assert(0);
334 match_byte_assert (0x58);
338 dump_value__(int level, bool match1)
340 for (int i = 0; i <= level; i++)
350 char *s1 = get_string();
352 char *s2 = get_string();
353 char *s3 = get_string();
355 printf("strings \"%s\", \"%s\" and \"%s\"", s1, s2, s3);
357 printf("string \"%s\" and \"%s\"", s1, s2);
359 match_byte_assert(1);
363 else if (match_byte (5))
366 printf ("variable \"%s\"", get_string());
368 if (!match_byte(1) && !match_byte(2))
369 match_byte_assert(3);
371 else if (match_byte (2))
377 match_byte_assert (0x58);
379 value = get_double ();
381 vallab = get_string ();
382 printf ("value %g format %d(%d.%d) var \"%s\" vallab \"%s\"",
383 value, format >> 16, (format >> 8) & 0xff, format & 0xff, var, vallab);
384 if (!match_byte (1) && !match_byte(2))
385 match_byte_assert (3);
387 else if (match_byte (4))
390 char *var, *vallab, *value;
392 match_byte_assert (0x58);
394 vallab = get_string ();
396 if (!match_byte(1) && !match_byte(2))
397 match_byte_assert (3);
398 value = get_string ();
399 printf ("value \"%s\" format %d(%d.%d) var \"%s\" vallab \"%s\"",
400 value, format >> 16, (format >> 8) & 0xff, format & 0xff, var, vallab);
402 else if (match_byte (1))
409 value = get_double ();
410 printf ("value %g format %d(%d.%d)", value, format >> 16, (format >> 8) & 0xff, format & 0xff);
418 char *base = get_string();
420 printf ("\"%s\" with %d variables:\n", base, x);
421 for (int i = 0; i < x; i++)
428 for (int j = 0; j <= level; j++)
430 printf("variable %d has %d values:\n", i, y);
431 for (int j = 0; j < y; j++)
433 dump_value__ (level + 1, false);
441 compare_int(const void *a_, const void *b_)
445 return *a < *b ? -1 : *a > *b;
449 check_permutation(int *a, int n, const char *name)
452 memcpy(b, a, n * sizeof *a);
453 qsort(b, n, sizeof *b, compare_int);
454 for (int i = 0; i < n; i++)
457 fprintf(stderr, "bad %s permutation:", name);
458 for (int i = 0; i < n; i++)
459 fprintf(stderr, " %d", a[i]);
466 dump_category(int level, int *indexes, int *n_indexes)
468 dump_value__ (level, true);
475 else if (match_byte (1))
479 match_u32_assert (1);
482 else if (!match_u32(2))
483 match_u32_assert (0);
485 int indx = get_u32();
486 int n_categories = get_u32();
489 if (n_categories != 0)
491 fprintf(stderr, "index not -1 but subcategories\n");
494 indexes[(*n_indexes)++] = indx;
496 if (n_categories > 0)
497 printf (", %d subcategories:", n_categories);
499 printf (", index %d", indx);
501 for (int i = 0; i < n_categories; i++)
502 dump_category (level + 1, indexes, n_indexes);
509 printf("next dim\n");
510 dump_value__ (0, false);
512 /* This byte is usually 0x02 but 0x00 and 0x75 (!) have also been spotted. */
515 if (!match_byte(0) && !match_byte(1))
516 match_byte_assert(2);
520 match_byte_assert(1);
522 match_byte_assert(1);
523 match_byte_assert(1);
524 static int dim_indx = 0;
525 match_u32_assert(dim_indx++);
526 n_categories = get_u32();
527 printf("%d nested categories\n", n_categories);
531 for (int i = 0; i < n_categories; i++)
532 dump_category (0, indexes, &n_indexes);
533 check_permutation(indexes, n_indexes, "categories");
541 printf ("%u dimensions\n", n_dims);
542 for (int i = 0; i < n_dims; i++)
552 /* The first three numbers add to the number of dimensions. */
555 match_u32_assert(n_dims - t);
557 /* The next n_dims numbers are a permutation of the dimension numbers. */
559 for (int i = 0; i < n_dims; i++)
561 check_permutation(a, n_dims, "dimensions");
564 printf ("%d data values, starting at %08x\n", x, pos);
565 for (int i = 0; i < x; i++)
567 printf("%08x, index %d:\n", pos, get_u32());
569 dump_value__(0, false);
578 dump_value__(0, true); putchar('\n');
579 dump_value__(0, true); putchar('\n');
580 match_byte_assert(0x31);
581 dump_value__(0, true); putchar('\n');
583 match_byte_assert(0x58);
584 if (match_byte(0x31))
586 dump_value__(0, false); putchar('\n');
589 match_byte_assert(0x58);
592 int n_footnotes = get_u32();
593 if (n_footnotes >= 20)
595 fprintf(stderr, "%08x: %d footnotes\n", pos - 4, n_footnotes);
599 printf("------\n%d footnotes\n", n_footnotes);
600 if (n_footnotes < 20)
602 for (int i = 0; i < n_footnotes; i++)
604 printf("footnote %d:\n", i);
605 dump_value__(0, false);
606 if (match_byte (0x31))
608 /* Custom footnote marker string. */
609 match_byte_assert(3);
611 match_byte_assert(0x58);
616 match_byte_assert (0x58);
617 printf("(%d)\n", get_u32());
623 find_dimensions(void)
626 const char dimensions[] = "-,,,.\0";
627 int x = try_find_tail(dimensions, sizeof dimensions - 1);
632 const char dimensions[] = "-,,, .\0";
633 return find_tail(dimensions, sizeof dimensions - 1);
639 printf("fonts: offset=%08x\n", pos);
641 for (int i = 1; i <= 8; i++)
643 printf("%08x: font %d, ", pos, i);
644 match_byte_assert(i);
645 match_byte_assert(0x31);
646 printf("%s, ", get_string());
647 match_byte_assert(0);
648 match_byte_assert(0);
649 if (!match_byte(0x40) && !match_byte(0x20) && !match_byte(0x80) && !match_byte(0x10))
650 match_byte_assert(0x50);
651 if (!match_byte(0x41))
652 match_byte_assert(0x51);
655 match_byte_assert(0);
657 /* OK, this seems really unlikely to be totally correct, but it matches my corpus... */
658 if (!match_u32(0) && !match_u32(2))
659 match_u32_assert(0xfaad);
661 if (!match_u32(0) && !match_u32(1) && !match_u32(2))
663 printf ("%s, ", get_string());
664 printf ("%s, ", get_string());
667 match_byte_assert(0);
669 /* These seem unlikely to be correct too. */
674 match_u32_assert(11);
680 if (!match_u32(-1) && !match_u32(8))
681 match_u32_assert(24);
682 if (!match_u32(-1) && !match_u32(2))
686 /* Who knows? Ranges from -1 to 8 with no obvious pattern. */
690 match_u32_assert(240);
693 match_u32_assert(18);
700 match_u32_assert(142);
704 int count = get_u32();
707 char *encoding = get_string();
708 printf("encoding=%s\n", encoding);
711 match_u32_assert(UINT32_MAX);
713 match_byte_assert(1);
714 match_byte_assert(0);
716 match_byte_assert(1);
717 if (!match_byte(0x99) && !match_byte(0x98))
718 match_byte_assert(0x97);
719 match_byte_assert(7);
720 match_byte_assert(0);
721 match_byte_assert(0);
723 match_byte_assert(',');
726 match_byte_assert(',');
727 if (!match_byte('.'))
728 match_byte_assert(' ');
731 for (int i = 0; i < 5; i++)
734 if (pos != find_dimensions())
735 fprintf (stderr, "%08x / %08x\n", pos, find_dimensions());
739 main(int argc, char *argv[])
744 if (isatty(STDIN_FILENO))
746 fprintf(stderr, "redirect stdin from a .bin file\n");
749 if (fstat(STDIN_FILENO, &s))
761 if (read(STDIN_FILENO, data, n) != n)
769 if (!strcmp(argv[1], "title0"))
772 if (match_byte (0x03)
773 || (match_byte (0x05) && match_byte (0x58)))
774 printf ("%s\n", get_string());
776 printf ("<unknown>\n");
779 else if (!strcmp(argv[1], "title"))
784 else if (!strcmp(argv[1], "titleraw"))
786 const char fonts[] = "\x01\x31\x09\0\0\0SansSerif";
788 n = find(fonts, sizeof fonts - 1);
790 else if (!strcmp(argv[1], "fonts"))
792 const char fonts[] = "\x01\x31\x09\0\0\0SansSerif";
793 const char styles[] = "\xf0\0\0\0";
794 start = find(fonts, sizeof fonts - 1);
795 n = find(styles, sizeof styles - 1);
797 else if (!strcmp(argv[1], "styles"))
799 const char styles[] = "\xf0\0\0\0";
800 const char dimensions[] = "-,,,.\0";
801 start = find(styles, sizeof styles - 1);
802 n = find(dimensions, sizeof dimensions - 1) + sizeof dimensions - 1;
804 else if (!strcmp(argv[1], "dimensions") || !strcmp(argv[1], "all"))
807 match_byte_assert(1);
808 match_byte_assert(0);
810 match_byte_assert(1);
812 match_byte_assert(1);
813 match_byte_assert(0);
814 match_byte_assert(0);
816 match_byte_assert(1);
818 match_byte_assert(0);
819 match_byte_assert(0);
820 match_byte_assert(0);
824 printf("\n\ndata:\n");
829 fprintf (stderr, "%x / %x\n", pos, n);
836 fprintf (stderr, "unknown section %s\n", argv[1]);
843 dump_raw(stdout, start, n, "\n");