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_nested_string(void)
213 match_byte_assert (0);
214 match_byte_assert (0);
215 int outer_end = pos + get_u32();
216 int inner_end = pos + get_u32();
217 if (pos != inner_end)
220 if (match_byte(0x31))
223 match_byte_assert(0x58);
224 if (pos != inner_end)
226 fprintf(stderr, "inner end discrepancy\n");
230 match_byte_assert(0x58);
231 match_byte_assert(0x58);
232 if (pos != outer_end)
234 fprintf(stderr, "outer end discrepancy\n");
242 if (match_byte (0x31))
249 match_u32_assert (0);
251 int outer_end = pos + get_u32();
252 int inner_end = pos + get_u32();
254 if (match_byte(0x31))
257 match_byte_assert(0x58);
258 if (pos != inner_end)
260 fprintf(stderr, "inner end discrepancy\n");
264 if (match_byte(0x31))
269 match_byte_assert(1);
270 get_string(); /* foreground */
271 get_string(); /* background */
272 get_string(); /* font */
273 match_byte_assert(12); /* size? */
276 match_byte_assert(0x58);
277 match_byte_assert(0x58);
278 if (pos != outer_end)
280 fprintf(stderr, "outer end discrepancy\n");
284 else if (match_u32 (1))
286 printf("(footnote %d) ", get_u32());
287 dump_nested_string();
289 else if (match_u32 (2))
291 printf("(special 2)");
292 match_byte_assert(0);
293 match_byte_assert(0);
296 dump_nested_string(); /* Our corpus doesn't contain any examples with strings though. */
301 printf("(special 3)");
302 match_byte_assert(0);
303 match_byte_assert(0);
304 match_byte_assert(1);
305 match_byte_assert(0);
307 dump_nested_string(); /* Our corpus doesn't contain any examples with strings though. */
311 match_byte_assert (0x58);
315 dump_value__(int level, bool match1)
317 for (int i = 0; i <= level; i++)
327 char *s1 = get_string();
329 char *s2 = get_string();
330 char *s3 = get_string();
332 printf("strings \"%s\", \"%s\" and \"%s\"", s1, s2, s3);
334 printf("string \"%s\" and \"%s\"", s1, s2);
336 match_byte_assert(1);
340 else if (match_byte (5))
343 printf ("variable \"%s\"", get_string());
345 if (!match_byte(1) && !match_byte(2))
346 match_byte_assert(3);
348 else if (match_byte (2))
354 match_byte_assert (0x58);
356 value = get_double ();
358 vallab = get_string ();
359 printf ("value %g format %d(%d.%d) var \"%s\" vallab \"%s\"",
360 value, format >> 16, (format >> 8) & 0xff, format & 0xff, var, vallab);
361 if (!match_byte (1) && !match_byte(2))
362 match_byte_assert (3);
364 else if (match_byte (4))
367 char *var, *vallab, *value;
369 match_byte_assert (0x58);
371 vallab = get_string ();
373 if (!match_byte(1) && !match_byte(2))
374 match_byte_assert (3);
375 value = get_string ();
376 printf ("value \"%s\" format %d(%d.%d) var \"%s\" vallab \"%s\"",
377 value, format >> 16, (format >> 8) & 0xff, format & 0xff, var, vallab);
379 else if (match_byte (1))
386 value = get_double ();
387 printf ("value %g format %d(%d.%d)", value, format >> 16, (format >> 8) & 0xff, format & 0xff);
395 char *base = get_string();
397 printf ("\"%s\" with %d variables:\n", base, x);
398 for (int i = 0; i < x; i++)
405 for (int j = 0; j <= level; j++)
407 printf("variable %d has %d values:\n", i, y);
408 for (int j = 0; j < y; j++)
410 dump_value__ (level + 1, false);
418 compare_int(const void *a_, const void *b_)
422 return *a < *b ? -1 : *a > *b;
426 check_permutation(int *a, int n, const char *name)
429 memcpy(b, a, n * sizeof *a);
430 qsort(b, n, sizeof *b, compare_int);
431 for (int i = 0; i < n; i++)
434 fprintf(stderr, "bad %s permutation:", name);
435 for (int i = 0; i < n; i++)
436 fprintf(stderr, " %d", a[i]);
443 dump_category(int level, int *indexes, int *n_indexes)
445 dump_value__ (level, true);
452 else if (match_byte (1))
456 match_u32_assert (1);
459 else if (!match_u32(2))
460 match_u32_assert (0);
462 int indx = get_u32();
463 int n_categories = get_u32();
466 if (n_categories != 0)
468 fprintf(stderr, "index not -1 but subcategories\n");
471 indexes[(*n_indexes)++] = indx;
473 if (n_categories > 0)
474 printf (", %d subcategories:", n_categories);
476 printf (", index %d", indx);
478 for (int i = 0; i < n_categories; i++)
479 dump_category (level + 1, indexes, n_indexes);
486 printf("next dim\n");
487 dump_value__ (0, false);
489 /* This byte is usually 0x02 but 0x00 and 0x75 (!) have also been spotted. */
492 if (!match_byte(0) && !match_byte(1))
493 match_byte_assert(2);
497 match_byte_assert(1);
499 match_byte_assert(1);
500 match_byte_assert(1);
501 static int dim_indx = 0;
502 match_u32_assert(dim_indx++);
503 n_categories = get_u32();
504 printf("%d nested categories\n", n_categories);
508 for (int i = 0; i < n_categories; i++)
509 dump_category (0, indexes, &n_indexes);
510 check_permutation(indexes, n_indexes, "categories");
518 printf ("%u dimensions\n", n_dims);
519 for (int i = 0; i < n_dims; i++)
529 /* The first three numbers add to the number of dimensions. */
532 match_u32_assert(n_dims - t);
534 /* The next n_dims numbers are a permutation of the dimension numbers. */
536 for (int i = 0; i < n_dims; i++)
538 check_permutation(a, n_dims, "dimensions");
541 printf ("%d data values, starting at %08x\n", x, pos);
542 for (int i = 0; i < x; i++)
544 printf("%08x, index %d:\n", pos, get_u32());
546 dump_value__(0, false);
555 dump_value__(0, true); putchar('\n');
556 dump_value__(0, true); putchar('\n');
557 match_byte_assert(0x31);
558 dump_value__(0, true); putchar('\n');
560 match_byte_assert(0x58);
561 if (match_byte(0x31))
563 dump_value__(0, false); putchar('\n');
566 match_byte_assert(0x58);
569 int n_footnotes = get_u32();
570 if (n_footnotes >= 20)
572 fprintf(stderr, "%08x: %d footnotes\n", pos - 4, n_footnotes);
576 printf("------\n%d footnotes\n", n_footnotes);
577 if (n_footnotes < 20)
579 for (int i = 0; i < n_footnotes; i++)
581 printf("footnote %d:\n", i);
582 dump_value__(0, false);
583 if (match_byte (0x31))
585 /* Custom footnote marker string. */
586 match_byte_assert(3);
588 match_byte_assert(0x58);
593 match_byte_assert (0x58);
594 printf("(%d)\n", get_u32());
600 find_dimensions(void)
603 const char dimensions[] = "-,,,.\0";
604 int x = try_find_tail(dimensions, sizeof dimensions - 1);
609 const char dimensions[] = "-,,, .\0";
610 return find_tail(dimensions, sizeof dimensions - 1);
616 printf("fonts: offset=%08x\n", pos);
618 for (int i = 1; i <= 8; i++)
620 printf("%08x: font %d, ", pos, i);
621 match_byte_assert(i);
622 match_byte_assert(0x31);
623 printf("%s, ", get_string());
624 match_byte_assert(0);
625 match_byte_assert(0);
626 if (!match_byte(0x40) && !match_byte(0x20) && !match_byte(0x80) && !match_byte(0x10))
627 match_byte_assert(0x50);
628 if (!match_byte(0x41))
629 match_byte_assert(0x51);
632 match_byte_assert(0);
634 /* OK, this seems really unlikely to be totally correct, but it matches my corpus... */
635 if (!match_u32(0) && !match_u32(2))
636 match_u32_assert(0xfaad);
638 if (!match_u32(0) && !match_u32(1) && !match_u32(2))
640 printf ("%s, ", get_string());
641 printf ("%s, ", get_string());
644 match_byte_assert(0);
646 /* These seem unlikely to be correct too. */
651 match_u32_assert(11);
657 if (!match_u32(-1) && !match_u32(8))
658 match_u32_assert(24);
659 if (!match_u32(-1) && !match_u32(2))
663 /* Who knows? Ranges from -1 to 8 with no obvious pattern. */
667 match_u32_assert(240);
670 match_u32_assert(18);
677 match_u32_assert(142);
681 int count = get_u32();
684 char *encoding = get_string();
685 printf("encoding=%s\n", encoding);
688 match_u32_assert(UINT32_MAX);
690 match_byte_assert(1);
691 match_byte_assert(0);
693 match_byte_assert(1);
694 if (!match_byte(0x99) && !match_byte(0x98))
695 match_byte_assert(0x97);
696 match_byte_assert(7);
697 match_byte_assert(0);
698 match_byte_assert(0);
700 match_byte_assert(',');
703 match_byte_assert(',');
704 if (!match_byte('.'))
705 match_byte_assert(' ');
708 for (int i = 0; i < 5; i++)
711 if (pos != find_dimensions())
712 fprintf (stderr, "%08x / %08x\n", pos, find_dimensions());
716 main(int argc, char *argv[])
721 if (isatty(STDIN_FILENO))
723 fprintf(stderr, "redirect stdin from a .bin file\n");
726 if (fstat(STDIN_FILENO, &s))
738 if (read(STDIN_FILENO, data, n) != n)
746 if (!strcmp(argv[1], "title0"))
749 if (match_byte (0x03)
750 || (match_byte (0x05) && match_byte (0x58)))
751 printf ("%s\n", get_string());
753 printf ("<unknown>\n");
756 else if (!strcmp(argv[1], "title"))
761 else if (!strcmp(argv[1], "titleraw"))
763 const char fonts[] = "\x01\x31\x09\0\0\0SansSerif";
765 n = find(fonts, sizeof fonts - 1);
767 else if (!strcmp(argv[1], "fonts"))
769 const char fonts[] = "\x01\x31\x09\0\0\0SansSerif";
770 const char styles[] = "\xf0\0\0\0";
771 start = find(fonts, sizeof fonts - 1);
772 n = find(styles, sizeof styles - 1);
774 else if (!strcmp(argv[1], "styles"))
776 const char styles[] = "\xf0\0\0\0";
777 const char dimensions[] = "-,,,.\0";
778 start = find(styles, sizeof styles - 1);
779 n = find(dimensions, sizeof dimensions - 1) + sizeof dimensions - 1;
781 else if (!strcmp(argv[1], "dimensions") || !strcmp(argv[1], "all"))
784 match_byte_assert(1);
785 match_byte_assert(0);
787 match_byte_assert(1);
789 match_byte_assert(1);
790 match_byte_assert(0);
791 match_byte_assert(0);
793 match_byte_assert(1);
795 match_byte_assert(0);
796 match_byte_assert(0);
797 match_byte_assert(0);
801 printf("\n\ndata:\n");
806 fprintf (stderr, "%x / %x\n", pos, n);
813 fprintf (stderr, "unknown section %s\n", argv[1]);
820 dump_raw(stdout, start, n, "\n");