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);
207 static void dump_substs(void (*dump)(int level), int level);
208 static void dump_value__(int level, bool match1);
211 dump_value__with_preskip(int level)
217 dump_value__ (level, false);
222 dump_value__(int level, bool match1)
224 for (int i = 0; i <= level; i++)
229 char *s1 = get_string();
231 char *s2 = get_string();
232 char *s3 = get_string();
234 printf("strings \"%s\", \"%s\" and \"%s\"", s1, s2, s3);
236 printf("string \"%s\" and \"%s\"", s1, s2);
238 match_byte_assert(1);
242 else if (match_byte (5))
245 printf ("variable \"%s\"", get_string());
247 if (!match_byte(1) && !match_byte(2))
248 match_byte_assert(3);
250 else if (match_byte (2))
256 match_byte_assert (0x58);
258 value = get_double ();
260 vallab = get_string ();
261 printf ("value %g format %d(%d.%d) var \"%s\" vallab \"%s\"",
262 value, format >> 16, (format >> 8) & 0xff, format & 0xff, var, vallab);
263 if (!match_byte (1) && !match_byte(2))
264 match_byte_assert (3);
266 else if (match_byte (4))
269 char *var, *vallab, *value;
271 match_byte_assert (0x58);
273 vallab = get_string ();
275 if (!match_byte(1) && !match_byte(2))
276 match_byte_assert (3);
277 value = get_string ();
278 printf ("value \"%s\" format %d(%d.%d) var \"%s\" vallab \"%s\"",
279 value, format >> 16, (format >> 8) & 0xff, format & 0xff, var, vallab);
281 else if (match_byte (1))
288 value = get_double ();
289 printf ("value %g format %d(%d.%d)", value, format >> 16, (format >> 8) & 0xff, format & 0xff);
294 dump_substs(dump_value__with_preskip, level + 1);
298 dump_category(int level)
304 dump_value__ (level, true);
311 else if (match_byte (1))
315 match_u32_assert (1);
318 else if (!match_u32(2))
319 match_u32_assert (0);
323 int n_categories = get_u32();
324 if (n_categories > 0)
325 printf (", %d subcategories:", n_categories);
327 for (int i = 0; i < n_categories; i++)
328 dump_category (level + 1);
331 static void dump_title_value(int level);
337 printf("next dim\n");
340 /* This byte is usually 0x02 but 0x00 and 0x75 (!) have also been spotted. */
343 if (!match_byte(0) && !match_byte(1))
344 match_byte_assert(2);
348 match_byte_assert(1);
352 n_categories = get_u32();
353 printf("%d nested categories\n", n_categories);
354 for (int i = 0; i < n_categories; i++)
363 printf ("%u dimensions\n", n_dims);
364 for (int i = 0; i < n_dims; i++)
372 dump_substs(void (*dump)(int level), int level)
376 char *base = get_string();
378 printf ("\"%s\" with %d variables:\n", base, x);
379 for (int i = 0; i < x; i++)
386 for (int j = 0; j <= level; j++)
388 printf("variable %d has %d values:\n", i, y);
389 for (int j = 0; j < y; j++)
398 dump_data_value(int level)
400 for (int i = 0; i <= level; i++)
408 dump_value__(0, false);
410 dump_substs (dump_data_value, level + 1);
418 for (int i = 0; i < 3 + n_dims; i++)
420 printf ("data intro:");
421 for (int i = 0; i < 3 + n_dims; i++)
425 fprintf (stderr,"data intro (%d dims):", n_dims);
426 for (int i = 0; i < 3+n_dims; i++)
427 fprintf (stderr," %d", get_u32());
428 fprintf(stderr,"\n");
431 printf ("%d data values, starting at %08x\n", x, pos);
432 for (int i = 0; i < x; i++)
434 printf("%08x, index %d:\n", pos, get_u32());
442 dump_title_value(int level)
444 for (int i = 0; i <= level; i++)
452 dump_value__(level, true);
454 dump_substs(dump_title_value, level + 1);
461 dump_title_value(0); putchar('\n');
462 dump_title_value(0); putchar('\n');
463 match_byte_assert(0x31);
464 dump_title_value(0); putchar('\n');
466 match_byte_assert(0x58);
467 if (match_byte(0x31))
469 dump_data_value(0); putchar('\n');
472 match_byte_assert(0x58);
475 int n_footnotes = get_u32();
476 if (n_footnotes >= 20)
478 fprintf(stderr, "%08x: %d footnotes\n", pos - 4, n_footnotes);
482 printf("------\n%d footnotes\n", n_footnotes);
483 if (n_footnotes < 20)
485 for (int i = 0; i < n_footnotes; i++)
487 printf("footnote %d:\n", i);
489 if (match_byte (0x31))
491 /* Custom footnote marker string. */
492 match_byte_assert(3);
494 match_byte_assert(0x58);
499 match_byte_assert (0x58);
500 printf("(%d)\n", get_u32());
506 find_dimensions(void)
509 const char dimensions[] = "-,,,.\0";
510 int x = try_find_tail(dimensions, sizeof dimensions - 1);
515 const char dimensions[] = "-,,, .\0";
516 return find_tail(dimensions, sizeof dimensions - 1);
522 printf("fonts: offset=%08x\n", pos);
524 for (int i = 1; i <= 8; i++)
526 printf("%08x: font %d, ", pos, i);
527 match_byte_assert(i);
528 match_byte_assert(0x31);
529 printf("%s, ", get_string());
530 match_byte_assert(0);
531 match_byte_assert(0);
532 if (!match_byte(0x40) && !match_byte(0x20) && !match_byte(0x80) && !match_byte(0x10))
533 match_byte_assert(0x50);
534 if (!match_byte(0x41))
535 match_byte_assert(0x51);
537 printf ("%s, ", get_string());
538 printf ("%s, ", get_string());
549 match_u32_assert(240);
552 match_u32_assert(18);
559 match_u32_assert(142);
563 int count = get_u32();
566 char *encoding = get_string();
567 printf("encoding=%s\n", encoding);
570 match_u32_assert(UINT32_MAX);
572 match_byte_assert(1);
573 match_byte_assert(0);
575 match_byte_assert(1);
576 if (!match_byte(0x99) && !match_byte(0x98))
577 match_byte_assert(0x97);
578 match_byte_assert(7);
579 match_byte_assert(0);
580 match_byte_assert(0);
582 match_byte_assert(',');
585 match_byte_assert(',');
586 if (!match_byte('.'))
587 match_byte_assert(' ');
590 for (int i = 0; i < 5; i++)
593 if (pos != find_dimensions())
594 fprintf (stderr, "%08x / %08x\n", pos, find_dimensions());
598 main(int argc, char *argv[])
603 if (isatty(STDIN_FILENO))
605 fprintf(stderr, "redirect stdin from a .bin file\n");
608 if (fstat(STDIN_FILENO, &s))
620 if (read(STDIN_FILENO, data, n) != n)
628 if (!strcmp(argv[1], "title0"))
631 if (match_byte (0x03)
632 || (match_byte (0x05) && match_byte (0x58)))
633 printf ("%s\n", get_string());
635 printf ("<unknown>\n");
638 else if (!strcmp(argv[1], "title"))
643 else if (!strcmp(argv[1], "titleraw"))
645 const char fonts[] = "\x01\x31\x09\0\0\0SansSerif";
647 n = find(fonts, sizeof fonts - 1);
649 else if (!strcmp(argv[1], "fonts"))
651 const char fonts[] = "\x01\x31\x09\0\0\0SansSerif";
652 const char styles[] = "\xf0\0\0\0";
653 start = find(fonts, sizeof fonts - 1);
654 n = find(styles, sizeof styles - 1);
656 else if (!strcmp(argv[1], "styles"))
658 const char styles[] = "\xf0\0\0\0";
659 const char dimensions[] = "-,,,.\0";
660 start = find(styles, sizeof styles - 1);
661 n = find(dimensions, sizeof dimensions - 1) + sizeof dimensions - 1;
663 else if (!strcmp(argv[1], "dimensions") || !strcmp(argv[1], "all"))
666 match_byte_assert(1);
667 match_byte_assert(0);
669 match_byte_assert(1);
671 match_byte_assert(1);
672 match_byte_assert(0);
673 match_byte_assert(0);
675 match_byte_assert(1);
677 match_byte_assert(0);
678 match_byte_assert(0);
679 match_byte_assert(0);
683 printf("\n\ndata:\n");
688 fprintf (stderr, "%x / %x\n", pos, n);
695 fprintf (stderr, "unknown section %s\n", argv[1]);
702 for (size_t i = start; i < n; )
709 && i + 4 + data[i] + data[i + 1] * 256 <= n
710 && all_ascii(&data[i + 4], data[i] + data[i + 1] * 256))
712 fputs("\n\"", stdout);
713 fwrite(&data[i + 4], 1, data[i] + data[i + 1] * 256, stdout);
714 fputs("\" ", stdout);
716 i += 4 + data[i] + data[i + 1] * 256;
725 memcpy (&d, &data[i + 4], 8);
726 printf ("F40.%d(%.*f)\n", data[i], data[i], d);
736 memcpy (&d, &data[i + 4], 8);
737 printf ("PCT40.%d(%.*f)\n", data[i], data[i], d);
741 && (data[i] && data[i] != 88 && data[i] != 0x41)
746 printf ("i%d ", data[i]);
751 printf("%02x ", data[i]);