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)
213 int subn = get_u32 ();
215 fprintf (stderr, "nested %d bytes: ", subn);
216 dump_raw(stderr, pos, pos + subn, "");
225 if (match_byte (0x31))
232 match_u32_assert (0);
235 else if (match_u32 (1))
237 printf("(footnote %d) ", get_u32());
238 match_byte_assert (0);
239 match_byte_assert (0);
242 else if (match_u32 (2))
244 printf("(special 2)");
245 match_byte_assert(0);
246 match_byte_assert(0);
249 match_byte_assert(0);
250 match_byte_assert(0);
256 printf("(special 3)");
257 match_byte_assert(0);
258 match_byte_assert(0);
259 match_byte_assert(1);
260 match_byte_assert(0);
266 match_byte_assert (0x58);
270 dump_value__(int level, bool match1)
272 for (int i = 0; i <= level; i++)
282 char *s1 = get_string();
284 char *s2 = get_string();
285 char *s3 = get_string();
287 printf("strings \"%s\", \"%s\" and \"%s\"", s1, s2, s3);
289 printf("string \"%s\" and \"%s\"", s1, s2);
291 match_byte_assert(1);
295 else if (match_byte (5))
298 printf ("variable \"%s\"", get_string());
300 if (!match_byte(1) && !match_byte(2))
301 match_byte_assert(3);
303 else if (match_byte (2))
309 match_byte_assert (0x58);
311 value = get_double ();
313 vallab = get_string ();
314 printf ("value %g format %d(%d.%d) var \"%s\" vallab \"%s\"",
315 value, format >> 16, (format >> 8) & 0xff, format & 0xff, var, vallab);
316 if (!match_byte (1) && !match_byte(2))
317 match_byte_assert (3);
319 else if (match_byte (4))
322 char *var, *vallab, *value;
324 match_byte_assert (0x58);
326 vallab = get_string ();
328 if (!match_byte(1) && !match_byte(2))
329 match_byte_assert (3);
330 value = get_string ();
331 printf ("value \"%s\" format %d(%d.%d) var \"%s\" vallab \"%s\"",
332 value, format >> 16, (format >> 8) & 0xff, format & 0xff, var, vallab);
334 else if (match_byte (1))
341 value = get_double ();
342 printf ("value %g format %d(%d.%d)", value, format >> 16, (format >> 8) & 0xff, format & 0xff);
350 char *base = get_string();
352 printf ("\"%s\" with %d variables:\n", base, x);
353 for (int i = 0; i < x; i++)
360 for (int j = 0; j <= level; j++)
362 printf("variable %d has %d values:\n", i, y);
363 for (int j = 0; j < y; j++)
365 dump_value__ (level + 1, false);
373 compare_int(const void *a_, const void *b_)
377 return *a < *b ? -1 : *a > *b;
381 check_permutation(int *a, int n, const char *name)
384 memcpy(b, a, n * sizeof *a);
385 qsort(b, n, sizeof *b, compare_int);
386 for (int i = 0; i < n; i++)
389 fprintf(stderr, "bad %s permutation:", name);
390 for (int i = 0; i < n; i++)
391 fprintf(stderr, " %d", a[i]);
398 dump_category(int level, int *indexes, int *n_indexes)
400 dump_value__ (level, true);
407 else if (match_byte (1))
411 match_u32_assert (1);
414 else if (!match_u32(2))
415 match_u32_assert (0);
417 int indx = get_u32();
418 int n_categories = get_u32();
421 if (n_categories != 0)
423 fprintf(stderr, "index not -1 but subcategories\n");
426 indexes[(*n_indexes)++] = indx;
428 if (n_categories > 0)
429 printf (", %d subcategories:", n_categories);
431 printf (", index %d", indx);
433 for (int i = 0; i < n_categories; i++)
434 dump_category (level + 1, indexes, n_indexes);
441 printf("next dim\n");
442 dump_value__ (0, false);
444 /* This byte is usually 0x02 but 0x00 and 0x75 (!) have also been spotted. */
447 if (!match_byte(0) && !match_byte(1))
448 match_byte_assert(2);
452 match_byte_assert(1);
454 match_byte_assert(1);
455 match_byte_assert(1);
456 static int dim_indx = 0;
457 match_u32_assert(dim_indx++);
458 n_categories = get_u32();
459 printf("%d nested categories\n", n_categories);
463 for (int i = 0; i < n_categories; i++)
464 dump_category (0, indexes, &n_indexes);
465 check_permutation(indexes, n_indexes, "categories");
473 printf ("%u dimensions\n", n_dims);
474 for (int i = 0; i < n_dims; i++)
484 /* The first three numbers add to the number of dimensions. */
487 match_u32_assert(n_dims - t);
489 /* The next n_dims numbers are a permutation of the dimension numbers. */
491 for (int i = 0; i < n_dims; i++)
493 check_permutation(a, n_dims, "dimensions");
496 printf ("%d data values, starting at %08x\n", x, pos);
497 for (int i = 0; i < x; i++)
499 printf("%08x, index %d:\n", pos, get_u32());
501 dump_value__(0, false);
510 dump_value__(0, true); putchar('\n');
511 dump_value__(0, true); putchar('\n');
512 match_byte_assert(0x31);
513 dump_value__(0, true); putchar('\n');
515 match_byte_assert(0x58);
516 if (match_byte(0x31))
518 dump_value__(0, false); putchar('\n');
521 match_byte_assert(0x58);
524 int n_footnotes = get_u32();
525 if (n_footnotes >= 20)
527 fprintf(stderr, "%08x: %d footnotes\n", pos - 4, n_footnotes);
531 printf("------\n%d footnotes\n", n_footnotes);
532 if (n_footnotes < 20)
534 for (int i = 0; i < n_footnotes; i++)
536 printf("footnote %d:\n", i);
537 dump_value__(0, false);
538 if (match_byte (0x31))
540 /* Custom footnote marker string. */
541 match_byte_assert(3);
543 match_byte_assert(0x58);
548 match_byte_assert (0x58);
549 printf("(%d)\n", get_u32());
555 find_dimensions(void)
558 const char dimensions[] = "-,,,.\0";
559 int x = try_find_tail(dimensions, sizeof dimensions - 1);
564 const char dimensions[] = "-,,, .\0";
565 return find_tail(dimensions, sizeof dimensions - 1);
571 printf("fonts: offset=%08x\n", pos);
573 for (int i = 1; i <= 8; i++)
575 printf("%08x: font %d, ", pos, i);
576 match_byte_assert(i);
577 match_byte_assert(0x31);
578 printf("%s, ", get_string());
579 match_byte_assert(0);
580 match_byte_assert(0);
581 if (!match_byte(0x40) && !match_byte(0x20) && !match_byte(0x80) && !match_byte(0x10))
582 match_byte_assert(0x50);
583 if (!match_byte(0x41))
584 match_byte_assert(0x51);
587 match_byte_assert(0);
589 /* OK, this seems really unlikely to be totally correct, but it matches my corpus... */
590 if (!match_u32(0) && !match_u32(2))
591 match_u32_assert(0xfaad);
593 if (!match_u32(0) && !match_u32(1) && !match_u32(2))
595 printf ("%s, ", get_string());
596 printf ("%s, ", get_string());
599 match_byte_assert(0);
607 match_u32_assert(240);
610 match_u32_assert(18);
617 match_u32_assert(142);
621 int count = get_u32();
624 char *encoding = get_string();
625 printf("encoding=%s\n", encoding);
628 match_u32_assert(UINT32_MAX);
630 match_byte_assert(1);
631 match_byte_assert(0);
633 match_byte_assert(1);
634 if (!match_byte(0x99) && !match_byte(0x98))
635 match_byte_assert(0x97);
636 match_byte_assert(7);
637 match_byte_assert(0);
638 match_byte_assert(0);
640 match_byte_assert(',');
643 match_byte_assert(',');
644 if (!match_byte('.'))
645 match_byte_assert(' ');
648 for (int i = 0; i < 5; i++)
651 if (pos != find_dimensions())
652 fprintf (stderr, "%08x / %08x\n", pos, find_dimensions());
656 main(int argc, char *argv[])
661 if (isatty(STDIN_FILENO))
663 fprintf(stderr, "redirect stdin from a .bin file\n");
666 if (fstat(STDIN_FILENO, &s))
678 if (read(STDIN_FILENO, data, n) != n)
686 if (!strcmp(argv[1], "title0"))
689 if (match_byte (0x03)
690 || (match_byte (0x05) && match_byte (0x58)))
691 printf ("%s\n", get_string());
693 printf ("<unknown>\n");
696 else if (!strcmp(argv[1], "title"))
701 else if (!strcmp(argv[1], "titleraw"))
703 const char fonts[] = "\x01\x31\x09\0\0\0SansSerif";
705 n = find(fonts, sizeof fonts - 1);
707 else if (!strcmp(argv[1], "fonts"))
709 const char fonts[] = "\x01\x31\x09\0\0\0SansSerif";
710 const char styles[] = "\xf0\0\0\0";
711 start = find(fonts, sizeof fonts - 1);
712 n = find(styles, sizeof styles - 1);
714 else if (!strcmp(argv[1], "styles"))
716 const char styles[] = "\xf0\0\0\0";
717 const char dimensions[] = "-,,,.\0";
718 start = find(styles, sizeof styles - 1);
719 n = find(dimensions, sizeof dimensions - 1) + sizeof dimensions - 1;
721 else if (!strcmp(argv[1], "dimensions") || !strcmp(argv[1], "all"))
724 match_byte_assert(1);
725 match_byte_assert(0);
727 match_byte_assert(1);
729 match_byte_assert(1);
730 match_byte_assert(0);
731 match_byte_assert(0);
733 match_byte_assert(1);
735 match_byte_assert(0);
736 match_byte_assert(0);
737 match_byte_assert(0);
741 printf("\n\ndata:\n");
746 fprintf (stderr, "%x / %x\n", pos, n);
753 fprintf (stderr, "unknown section %s\n", argv[1]);
760 dump_raw(stdout, start, n, "\n");