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 ();
214 fprintf (stderr, "nested %d bytes: ", subn);
215 dump_raw(stderr, pos, pos + subn, "");
223 if (match_byte (0x31))
230 match_u32_assert (0);
233 else if (match_u32 (1))
235 printf("(footnote %d) ", get_u32());
236 match_byte_assert (0);
237 match_byte_assert (0);
240 else if (match_u32 (2))
242 printf("(special 2)");
243 match_byte_assert(0);
244 match_byte_assert(0);
247 match_byte_assert(0);
248 match_byte_assert(0);
254 printf("(special 3)");
255 match_byte_assert(0);
256 match_byte_assert(0);
257 match_byte_assert(1);
258 match_byte_assert(0);
264 match_byte_assert (0x58);
268 dump_value__(int level, bool match1)
270 for (int i = 0; i <= level; i++)
280 char *s1 = get_string();
282 char *s2 = get_string();
283 char *s3 = get_string();
285 printf("strings \"%s\", \"%s\" and \"%s\"", s1, s2, s3);
287 printf("string \"%s\" and \"%s\"", s1, s2);
289 match_byte_assert(1);
293 else if (match_byte (5))
296 printf ("variable \"%s\"", get_string());
298 if (!match_byte(1) && !match_byte(2))
299 match_byte_assert(3);
301 else if (match_byte (2))
307 match_byte_assert (0x58);
309 value = get_double ();
311 vallab = get_string ();
312 printf ("value %g format %d(%d.%d) var \"%s\" vallab \"%s\"",
313 value, format >> 16, (format >> 8) & 0xff, format & 0xff, var, vallab);
314 if (!match_byte (1) && !match_byte(2))
315 match_byte_assert (3);
317 else if (match_byte (4))
320 char *var, *vallab, *value;
322 match_byte_assert (0x58);
324 vallab = get_string ();
326 if (!match_byte(1) && !match_byte(2))
327 match_byte_assert (3);
328 value = get_string ();
329 printf ("value \"%s\" format %d(%d.%d) var \"%s\" vallab \"%s\"",
330 value, format >> 16, (format >> 8) & 0xff, format & 0xff, var, vallab);
332 else if (match_byte (1))
339 value = get_double ();
340 printf ("value %g format %d(%d.%d)", value, format >> 16, (format >> 8) & 0xff, format & 0xff);
348 char *base = get_string();
350 printf ("\"%s\" with %d variables:\n", base, x);
351 for (int i = 0; i < x; i++)
358 for (int j = 0; j <= level; j++)
360 printf("variable %d has %d values:\n", i, y);
361 for (int j = 0; j < y; j++)
363 dump_value__ (level + 1, false);
371 compare_int(const void *a_, const void *b_)
375 return *a < *b ? -1 : *a > *b;
379 check_permutation(int *a, int n, const char *name)
382 memcpy(b, a, n * sizeof *a);
383 qsort(b, n, sizeof *b, compare_int);
384 for (int i = 0; i < n; i++)
387 fprintf(stderr, "bad %s permutation:", name);
388 for (int i = 0; i < n; i++)
389 fprintf(stderr, " %d", a[i]);
396 dump_category(int level, int *indexes, int *n_indexes)
398 dump_value__ (level, true);
405 else if (match_byte (1))
409 match_u32_assert (1);
412 else if (!match_u32(2))
413 match_u32_assert (0);
415 int indx = get_u32();
416 int n_categories = get_u32();
419 if (n_categories != 0)
421 fprintf(stderr, "index not -1 but subcategories\n");
424 indexes[(*n_indexes)++] = indx;
426 if (n_categories > 0)
427 printf (", %d subcategories:", n_categories);
429 printf (", index %d", indx);
431 for (int i = 0; i < n_categories; i++)
432 dump_category (level + 1, indexes, n_indexes);
439 printf("next dim\n");
440 dump_value__ (0, false);
442 /* This byte is usually 0x02 but 0x00 and 0x75 (!) have also been spotted. */
445 if (!match_byte(0) && !match_byte(1))
446 match_byte_assert(2);
450 match_byte_assert(1);
452 match_byte_assert(1);
453 match_byte_assert(1);
454 static int dim_indx = 0;
455 match_u32_assert(dim_indx++);
456 n_categories = get_u32();
457 printf("%d nested categories\n", n_categories);
461 for (int i = 0; i < n_categories; i++)
462 dump_category (0, indexes, &n_indexes);
463 check_permutation(indexes, n_indexes, "categories");
471 printf ("%u dimensions\n", n_dims);
472 for (int i = 0; i < n_dims; i++)
482 /* The first three numbers add to the number of dimensions. */
485 match_u32_assert(n_dims - t);
487 /* The next n_dims numbers are a permutation of the dimension numbers. */
489 for (int i = 0; i < n_dims; i++)
491 check_permutation(a, n_dims, "dimensions");
494 printf ("%d data values, starting at %08x\n", x, pos);
495 for (int i = 0; i < x; i++)
497 printf("%08x, index %d:\n", pos, get_u32());
499 dump_value__(0, false);
508 dump_value__(0, true); putchar('\n');
509 dump_value__(0, true); putchar('\n');
510 match_byte_assert(0x31);
511 dump_value__(0, true); putchar('\n');
513 match_byte_assert(0x58);
514 if (match_byte(0x31))
516 dump_value__(0, false); putchar('\n');
519 match_byte_assert(0x58);
522 int n_footnotes = get_u32();
523 if (n_footnotes >= 20)
525 fprintf(stderr, "%08x: %d footnotes\n", pos - 4, n_footnotes);
529 printf("------\n%d footnotes\n", n_footnotes);
530 if (n_footnotes < 20)
532 for (int i = 0; i < n_footnotes; i++)
534 printf("footnote %d:\n", i);
535 dump_value__(0, false);
536 if (match_byte (0x31))
538 /* Custom footnote marker string. */
539 match_byte_assert(3);
541 match_byte_assert(0x58);
546 match_byte_assert (0x58);
547 printf("(%d)\n", get_u32());
553 find_dimensions(void)
556 const char dimensions[] = "-,,,.\0";
557 int x = try_find_tail(dimensions, sizeof dimensions - 1);
562 const char dimensions[] = "-,,, .\0";
563 return find_tail(dimensions, sizeof dimensions - 1);
569 printf("fonts: offset=%08x\n", pos);
571 for (int i = 1; i <= 8; i++)
573 printf("%08x: font %d, ", pos, i);
574 match_byte_assert(i);
575 match_byte_assert(0x31);
576 printf("%s, ", get_string());
577 match_byte_assert(0);
578 match_byte_assert(0);
579 if (!match_byte(0x40) && !match_byte(0x20) && !match_byte(0x80) && !match_byte(0x10))
580 match_byte_assert(0x50);
581 if (!match_byte(0x41))
582 match_byte_assert(0x51);
585 match_byte_assert(0);
588 match_byte_assert(0);
589 match_byte_assert(0);
590 if (!match_u32(0) && !match_u32(1) && !match_u32(2))
592 printf ("%s, ", get_string());
593 printf ("%s, ", get_string());
596 match_byte_assert(0);
604 match_u32_assert(240);
607 match_u32_assert(18);
614 match_u32_assert(142);
618 int count = get_u32();
621 char *encoding = get_string();
622 printf("encoding=%s\n", encoding);
625 match_u32_assert(UINT32_MAX);
627 match_byte_assert(1);
628 match_byte_assert(0);
630 match_byte_assert(1);
631 if (!match_byte(0x99) && !match_byte(0x98))
632 match_byte_assert(0x97);
633 match_byte_assert(7);
634 match_byte_assert(0);
635 match_byte_assert(0);
637 match_byte_assert(',');
640 match_byte_assert(',');
641 if (!match_byte('.'))
642 match_byte_assert(' ');
645 for (int i = 0; i < 5; i++)
648 if (pos != find_dimensions())
649 fprintf (stderr, "%08x / %08x\n", pos, find_dimensions());
653 main(int argc, char *argv[])
658 if (isatty(STDIN_FILENO))
660 fprintf(stderr, "redirect stdin from a .bin file\n");
663 if (fstat(STDIN_FILENO, &s))
675 if (read(STDIN_FILENO, data, n) != n)
683 if (!strcmp(argv[1], "title0"))
686 if (match_byte (0x03)
687 || (match_byte (0x05) && match_byte (0x58)))
688 printf ("%s\n", get_string());
690 printf ("<unknown>\n");
693 else if (!strcmp(argv[1], "title"))
698 else if (!strcmp(argv[1], "titleraw"))
700 const char fonts[] = "\x01\x31\x09\0\0\0SansSerif";
702 n = find(fonts, sizeof fonts - 1);
704 else if (!strcmp(argv[1], "fonts"))
706 const char fonts[] = "\x01\x31\x09\0\0\0SansSerif";
707 const char styles[] = "\xf0\0\0\0";
708 start = find(fonts, sizeof fonts - 1);
709 n = find(styles, sizeof styles - 1);
711 else if (!strcmp(argv[1], "styles"))
713 const char styles[] = "\xf0\0\0\0";
714 const char dimensions[] = "-,,,.\0";
715 start = find(styles, sizeof styles - 1);
716 n = find(dimensions, sizeof dimensions - 1) + sizeof dimensions - 1;
718 else if (!strcmp(argv[1], "dimensions") || !strcmp(argv[1], "all"))
721 match_byte_assert(1);
722 match_byte_assert(0);
724 match_byte_assert(1);
726 match_byte_assert(1);
727 match_byte_assert(0);
728 match_byte_assert(0);
730 match_byte_assert(1);
732 match_byte_assert(0);
733 match_byte_assert(0);
734 match_byte_assert(0);
738 printf("\n\ndata:\n");
743 fprintf (stderr, "%x / %x\n", pos, n);
750 fprintf (stderr, "unknown section %s\n", argv[1]);
757 dump_raw(stdout, start, n, "\n");