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 get_string(const char *where)
131 /*data[pos + 1] == 0 && data[pos + 2] == 0 && data[pos + 3] == 0*/
132 /*&& all_ascii(&data[pos + 4], data[pos])*/)
134 int len = data[pos] + data[pos + 1] * 256;
135 char *s = malloc(len + 1);
137 memcpy(s, &data[pos + 4], len);
144 fprintf(stderr, "%s: 0x%x: expected string\n", where, pos);
148 #define get_string() get_string(WHERE)
153 if (match_byte (0x31))
160 match_u32_assert (0);
161 int subn = get_u32 ();
162 printf ("nested %d bytes", subn);
165 else if (match_u32 (1))
167 printf("(footnote %d) ", get_u32());
168 match_byte_assert (0);
169 match_byte_assert (0);
170 int subn = get_u32 ();
171 printf ("nested %d bytes", subn);
174 else if (match_u32 (2))
176 printf("(special 2)");
177 match_byte_assert(0);
178 match_byte_assert(0);
181 match_byte_assert(0);
182 match_byte_assert(0);
183 int subn = get_u32 ();
184 printf ("nested %d bytes", subn);
190 printf("(special 3)");
191 match_byte_assert(0);
192 match_byte_assert(0);
193 match_byte_assert(1);
194 match_byte_assert(0);
195 int subn = get_u32 ();
196 printf ("nested %d bytes, ", subn);
199 printf ("nested %d bytes, ", subn);
204 match_byte_assert (0x58);
208 dump_value__(int level, bool match1)
210 for (int i = 0; i <= level; i++)
220 char *s1 = get_string();
222 char *s2 = get_string();
223 char *s3 = get_string();
225 printf("strings \"%s\", \"%s\" and \"%s\"", s1, s2, s3);
227 printf("string \"%s\" and \"%s\"", s1, s2);
229 match_byte_assert(1);
233 else if (match_byte (5))
236 printf ("variable \"%s\"", get_string());
238 if (!match_byte(1) && !match_byte(2))
239 match_byte_assert(3);
241 else if (match_byte (2))
247 match_byte_assert (0x58);
249 value = get_double ();
251 vallab = get_string ();
252 printf ("value %g format %d(%d.%d) var \"%s\" vallab \"%s\"",
253 value, format >> 16, (format >> 8) & 0xff, format & 0xff, var, vallab);
254 if (!match_byte (1) && !match_byte(2))
255 match_byte_assert (3);
257 else if (match_byte (4))
260 char *var, *vallab, *value;
262 match_byte_assert (0x58);
264 vallab = get_string ();
266 if (!match_byte(1) && !match_byte(2))
267 match_byte_assert (3);
268 value = get_string ();
269 printf ("value \"%s\" format %d(%d.%d) var \"%s\" vallab \"%s\"",
270 value, format >> 16, (format >> 8) & 0xff, format & 0xff, var, vallab);
272 else if (match_byte (1))
279 value = get_double ();
280 printf ("value %g format %d(%d.%d)", value, format >> 16, (format >> 8) & 0xff, format & 0xff);
288 char *base = get_string();
290 printf ("\"%s\" with %d variables:\n", base, x);
291 for (int i = 0; i < x; i++)
298 for (int j = 0; j <= level; j++)
300 printf("variable %d has %d values:\n", i, y);
301 for (int j = 0; j < y; j++)
303 dump_value__ (level + 1, false);
311 compare_int(const void *a_, const void *b_)
315 return *a < *b ? -1 : *a > *b;
319 check_permutation(int *a, int n, const char *name)
322 memcpy(b, a, n * sizeof *a);
323 qsort(b, n, sizeof *b, compare_int);
324 for (int i = 0; i < n; i++)
327 fprintf(stderr, "bad %s permutation:", name);
328 for (int i = 0; i < n; i++)
329 fprintf(stderr, " %d", a[i]);
336 dump_category(int level, int *indexes, int *n_indexes)
338 dump_value__ (level, true);
345 else if (match_byte (1))
349 match_u32_assert (1);
352 else if (!match_u32(2))
353 match_u32_assert (0);
355 int indx = get_u32();
356 int n_categories = get_u32();
359 if (n_categories != 0)
361 fprintf(stderr, "index not -1 but subcategories\n");
364 indexes[(*n_indexes)++] = indx;
366 if (n_categories > 0)
367 printf (", %d subcategories:", n_categories);
369 printf (", index %d", indx);
371 for (int i = 0; i < n_categories; i++)
372 dump_category (level + 1, indexes, n_indexes);
379 printf("next dim\n");
380 dump_value__ (0, false);
382 /* This byte is usually 0x02 but 0x00 and 0x75 (!) have also been spotted. */
385 if (!match_byte(0) && !match_byte(1))
386 match_byte_assert(2);
390 match_byte_assert(1);
394 n_categories = get_u32();
395 printf("%d nested categories\n", n_categories);
399 for (int i = 0; i < n_categories; i++)
400 dump_category (0, indexes, &n_indexes);
401 check_permutation(indexes, n_indexes, "categories");
409 printf ("%u dimensions\n", n_dims);
410 for (int i = 0; i < n_dims; i++)
420 /* The first three numbers add to the number of dimensions. */
423 match_u32_assert(n_dims - t);
425 /* The next n_dims numbers are a permutation of the dimension numbers. */
427 for (int i = 0; i < n_dims; i++)
429 check_permutation(a, n_dims, "dimensions");
432 printf ("%d data values, starting at %08x\n", x, pos);
433 for (int i = 0; i < x; i++)
435 printf("%08x, index %d:\n", pos, get_u32());
437 dump_value__(0, false);
446 dump_value__(0, true); putchar('\n');
447 dump_value__(0, true); putchar('\n');
448 match_byte_assert(0x31);
449 dump_value__(0, true); putchar('\n');
451 match_byte_assert(0x58);
452 if (match_byte(0x31))
454 dump_value__(0, false); putchar('\n');
457 match_byte_assert(0x58);
460 int n_footnotes = get_u32();
461 if (n_footnotes >= 20)
463 fprintf(stderr, "%08x: %d footnotes\n", pos - 4, n_footnotes);
467 printf("------\n%d footnotes\n", n_footnotes);
468 if (n_footnotes < 20)
470 for (int i = 0; i < n_footnotes; i++)
472 printf("footnote %d:\n", i);
473 dump_value__(0, false);
474 if (match_byte (0x31))
476 /* Custom footnote marker string. */
477 match_byte_assert(3);
479 match_byte_assert(0x58);
484 match_byte_assert (0x58);
485 printf("(%d)\n", get_u32());
491 find_dimensions(void)
494 const char dimensions[] = "-,,,.\0";
495 int x = try_find_tail(dimensions, sizeof dimensions - 1);
500 const char dimensions[] = "-,,, .\0";
501 return find_tail(dimensions, sizeof dimensions - 1);
507 printf("fonts: offset=%08x\n", pos);
509 for (int i = 1; i <= 8; i++)
511 printf("%08x: font %d, ", pos, i);
512 match_byte_assert(i);
513 match_byte_assert(0x31);
514 printf("%s, ", get_string());
515 match_byte_assert(0);
516 match_byte_assert(0);
517 if (!match_byte(0x40) && !match_byte(0x20) && !match_byte(0x80) && !match_byte(0x10))
518 match_byte_assert(0x50);
519 if (!match_byte(0x41))
520 match_byte_assert(0x51);
522 printf ("%s, ", get_string());
523 printf ("%s, ", get_string());
534 match_u32_assert(240);
537 match_u32_assert(18);
544 match_u32_assert(142);
548 int count = get_u32();
551 char *encoding = get_string();
552 printf("encoding=%s\n", encoding);
555 match_u32_assert(UINT32_MAX);
557 match_byte_assert(1);
558 match_byte_assert(0);
560 match_byte_assert(1);
561 if (!match_byte(0x99) && !match_byte(0x98))
562 match_byte_assert(0x97);
563 match_byte_assert(7);
564 match_byte_assert(0);
565 match_byte_assert(0);
567 match_byte_assert(',');
570 match_byte_assert(',');
571 if (!match_byte('.'))
572 match_byte_assert(' ');
575 for (int i = 0; i < 5; i++)
578 if (pos != find_dimensions())
579 fprintf (stderr, "%08x / %08x\n", pos, find_dimensions());
583 main(int argc, char *argv[])
588 if (isatty(STDIN_FILENO))
590 fprintf(stderr, "redirect stdin from a .bin file\n");
593 if (fstat(STDIN_FILENO, &s))
605 if (read(STDIN_FILENO, data, n) != n)
613 if (!strcmp(argv[1], "title0"))
616 if (match_byte (0x03)
617 || (match_byte (0x05) && match_byte (0x58)))
618 printf ("%s\n", get_string());
620 printf ("<unknown>\n");
623 else if (!strcmp(argv[1], "title"))
628 else if (!strcmp(argv[1], "titleraw"))
630 const char fonts[] = "\x01\x31\x09\0\0\0SansSerif";
632 n = find(fonts, sizeof fonts - 1);
634 else if (!strcmp(argv[1], "fonts"))
636 const char fonts[] = "\x01\x31\x09\0\0\0SansSerif";
637 const char styles[] = "\xf0\0\0\0";
638 start = find(fonts, sizeof fonts - 1);
639 n = find(styles, sizeof styles - 1);
641 else if (!strcmp(argv[1], "styles"))
643 const char styles[] = "\xf0\0\0\0";
644 const char dimensions[] = "-,,,.\0";
645 start = find(styles, sizeof styles - 1);
646 n = find(dimensions, sizeof dimensions - 1) + sizeof dimensions - 1;
648 else if (!strcmp(argv[1], "dimensions") || !strcmp(argv[1], "all"))
651 match_byte_assert(1);
652 match_byte_assert(0);
654 match_byte_assert(1);
656 match_byte_assert(1);
657 match_byte_assert(0);
658 match_byte_assert(0);
660 match_byte_assert(1);
662 match_byte_assert(0);
663 match_byte_assert(0);
664 match_byte_assert(0);
668 printf("\n\ndata:\n");
673 fprintf (stderr, "%x / %x\n", pos, n);
680 fprintf (stderr, "unknown section %s\n", argv[1]);
687 for (size_t i = start; i < n; )
694 && i + 4 + data[i] + data[i + 1] * 256 <= n
695 && all_ascii(&data[i + 4], data[i] + data[i + 1] * 256))
697 fputs("\n\"", stdout);
698 fwrite(&data[i + 4], 1, data[i] + data[i + 1] * 256, stdout);
699 fputs("\" ", stdout);
701 i += 4 + data[i] + data[i + 1] * 256;
710 memcpy (&d, &data[i + 4], 8);
711 printf ("F40.%d(%.*f)\n", data[i], data[i], d);
721 memcpy (&d, &data[i + 4], 8);
722 printf ("PCT40.%d(%.*f)\n", data[i], data[i], d);
726 && (data[i] && data[i] != 88 && data[i] != 0x41)
731 printf ("i%d ", data[i]);
736 printf("%02x ", data[i]);