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_value(int level);
210 dump_value__(int level)
214 char *s1 = get_string();
216 char *s2 = get_string();
217 char *s3 = get_string();
219 printf("strings \"%s\", \"%s\" and \"%s\"", s1, s2, s3);
221 printf("string \"%s\" and \"%s\"", s1, s2);
223 match_byte_assert(1);
226 else if (match_byte (5))
229 printf ("variable \"%s\"", get_string());
231 if (!match_byte(1) && !match_byte(2))
232 match_byte_assert(3);
234 else if (match_byte (2))
240 match_byte_assert (0x58);
242 value = get_double ();
244 vallab = get_string ();
245 printf ("value %g format %d(%d.%d) var \"%s\" vallab \"%s\"",
246 value, format >> 16, (format >> 8) & 0xff, format & 0xff, var, vallab);
247 if (!match_byte (1) && !match_byte(2))
248 match_byte_assert (3);
250 else if (match_byte (4))
253 char *var, *vallab, *value;
255 match_byte_assert (0x58);
257 vallab = get_string ();
259 if (!match_byte(1) && !match_byte(2))
260 match_byte_assert (3);
261 value = get_string ();
262 printf ("value \"%s\" format %d(%d.%d) var \"%s\" vallab \"%s\"",
263 value, format >> 16, (format >> 8) & 0xff, format & 0xff, var, vallab);
265 else if (match_byte (1))
272 value = get_double ();
273 printf ("value %g format %d(%d.%d)", value, format >> 16, (format >> 8) & 0xff, format & 0xff);
279 char *base = get_string();
282 printf ("\"%s\" with %d variables:\n", base, x);
285 for (int i = 0; i < x; i++)
287 dump_value (level+1);
293 for (int i = 0; i < x; i++)
297 for (int j = 0; j <= level; j++)
299 printf("variable %d has %d values:\n", i, y);
300 for (int j = 0; j < y; j++)
305 char *a = get_string();
306 match_byte_assert(0x58);
307 char *b = get_string();
308 char *c = get_string();
309 for (int k = 0; k <= level + 1; k++)
311 printf ("\"%s\", \"%s\", \"%s\"", a, b, c);
314 dump_value (level+1);
328 dump_value(int level)
330 for (int i = 0; i <= level; i++)
344 dump_dim_value(int level)
346 for (int i = 0; i <= level; i++)
349 if (data[pos] == 3 || data[pos] == 5)
356 dump_category(int level)
362 else if (match_byte (1))
366 match_u32_assert (1);
369 else if (!match_u32(2))
370 match_u32_assert (0);
374 int n_categories = get_u32();
375 if (n_categories > 0)
376 printf (", %d subcategories:", n_categories);
378 for (int i = 0; i < n_categories; i++)
379 dump_category (level + 1);
386 printf("next dim\n");
389 /* This byte is usually 0x02 but 0x00 and 0x75 (!) have also been spotted. */
392 if (!match_byte(0) && !match_byte(1))
393 match_byte_assert(2);
397 match_byte_assert(1);
407 n_categories = get_u32();
408 printf("%d nested categories\n", n_categories);
409 for (int i = 0; i < n_categories; i++)
418 printf ("%u dimensions\n", n_dims);
419 for (int i = 0; i < n_dims; i++)
427 dump_data_value(void)
440 value = get_double ();
441 printf (" value %g format %d(%d.%d)", value, format >> 16, (format >> 8) & 0xff, format & 0xff);
443 else if (match_byte (3))
448 printf("string \"%s\"", get_string());
450 match_byte_assert(1);
452 else if (data[pos] == 2 || data[pos] == 4)
454 else if (data[pos] == 5)
459 char *base = get_string();
461 printf ("\"%s\"; %d variables:\n", base, x);
462 for (int i = 0; i < x; i++)
469 for (int j = 0; j <= 0; j++)
471 printf("variable %d has %d values:\n", i, y);
472 for (int j = 0; j < y; j++)
483 for (int i = 0; i < 3 + n_dims; i++)
485 printf ("data intro:");
486 for (int i = 0; i < 3 + n_dims; i++)
490 fprintf (stderr,"data intro (%d dims):", n_dims);
491 for (int i = 0; i < 3+n_dims; i++)
492 fprintf (stderr," %d", get_u32());
493 fprintf(stderr,"\n");
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());
507 dump_title_value(int level)
509 for (int i = 0; i <= level; i++)
516 if (data[pos] == 3 || data[pos] == 2 || data[pos] == 4 || data[pos] == 1)
518 else if (data[pos] == 5)
524 char *base = get_string();
526 printf ("\"%s\" with %d variables:\n", base, x);
527 for (int i = 0; i < x; i++)
534 for (int j = 0; j <= level; j++)
536 printf("variable %d has %d values:\n", i, y);
537 for (int j = 0; j < y; j++)
539 dump_title_value (level+1);
547 dump_footnote_value(int level)
549 for (int i = 0; i <= level; i++)
556 if (data[pos] == 2 || data[pos] == 4)
558 else if (data[pos] == 5)
560 else if (match_byte (3))
565 printf("string \"%s\"", get_string());
567 match_byte_assert (1);
569 else if (match_byte (1))
576 value = get_double ();
577 printf ("value %g format %d(%d.%d)", value, format >> 16, (format >> 8) & 0xff, format & 0xff);
582 char *base = get_string();
584 printf ("\"%s\"; %d variables:\n", base, x);
585 for (int i = 0; i < x; i++)
592 for (int j = 0; j <= level; j++)
594 printf("variable %d has %d values:\n", i, y);
595 for (int j = 0; j < y; j++)
597 dump_footnote_value (level+1);
608 dump_title_value(0); putchar('\n');
609 dump_title_value(0); putchar('\n');
610 match_byte_assert(0x31);
611 dump_title_value(0); putchar('\n');
613 match_byte_assert(0x58);
614 if (match_byte(0x31))
616 dump_footnote_value(0); putchar('\n');
619 match_byte_assert(0x58);
622 int n_footnotes = get_u32();
623 if (n_footnotes >= 20)
625 fprintf(stderr, "%08x: %d footnotes\n", pos - 4, n_footnotes);
629 printf("------\n%d footnotes\n", n_footnotes);
630 if (n_footnotes < 20)
632 for (int i = 0; i < n_footnotes; i++)
634 printf("footnote %d:\n", i);
635 dump_footnote_value(0);
640 if (match_byte (0x31))
642 /* Custom footnote marker string. */
643 match_byte_assert(3);
645 match_byte_assert(0x58);
650 match_byte_assert (0x58);
651 printf("(%d)\n", get_u32());
657 find_dimensions(void)
660 const char dimensions[] = "-,,,.\0";
661 int x = try_find_tail(dimensions, sizeof dimensions - 1);
666 const char dimensions[] = "-,,, .\0";
667 return find_tail(dimensions, sizeof dimensions - 1);
673 printf("fonts: offset=%08x\n", pos);
675 for (int i = 1; i <= 8; i++)
677 printf("%08x: font %d, ", pos, i);
678 match_byte_assert(i);
679 match_byte_assert(0x31);
680 printf("%s, ", get_string());
681 match_byte_assert(0);
682 match_byte_assert(0);
683 if (!match_byte(0x40) && !match_byte(0x20) && !match_byte(0x80) && !match_byte(0x10))
684 match_byte_assert(0x50);
685 if (!match_byte(0x41))
686 match_byte_assert(0x51);
688 printf ("%s, ", get_string());
689 printf ("%s, ", get_string());
700 match_u32_assert(240);
703 match_u32_assert(18);
710 match_u32_assert(142);
714 int count = get_u32();
717 char *encoding = get_string();
718 printf("encoding=%s\n", encoding);
721 match_u32_assert(UINT32_MAX);
723 match_byte_assert(1);
724 match_byte_assert(0);
726 match_byte_assert(1);
727 if (!match_byte(0x99) && !match_byte(0x98))
728 match_byte_assert(0x97);
729 match_byte_assert(7);
730 match_byte_assert(0);
731 match_byte_assert(0);
733 match_byte_assert(',');
736 match_byte_assert(',');
737 if (!match_byte('.'))
738 match_byte_assert(' ');
741 for (int i = 0; i < 5; i++)
744 if (pos != find_dimensions())
745 fprintf (stderr, "%08x / %08x\n", pos, find_dimensions());
749 main(int argc, char *argv[])
754 if (isatty(STDIN_FILENO))
756 fprintf(stderr, "redirect stdin from a .bin file\n");
759 if (fstat(STDIN_FILENO, &s))
771 if (read(STDIN_FILENO, data, n) != n)
779 if (!strcmp(argv[1], "title0"))
782 if (match_byte (0x03)
783 || (match_byte (0x05) && match_byte (0x58)))
784 printf ("%s\n", get_string());
786 printf ("<unknown>\n");
789 else if (!strcmp(argv[1], "title"))
794 else if (!strcmp(argv[1], "titleraw"))
796 const char fonts[] = "\x01\x31\x09\0\0\0SansSerif";
798 n = find(fonts, sizeof fonts - 1);
800 else if (!strcmp(argv[1], "fonts"))
802 const char fonts[] = "\x01\x31\x09\0\0\0SansSerif";
803 const char styles[] = "\xf0\0\0\0";
804 start = find(fonts, sizeof fonts - 1);
805 n = find(styles, sizeof styles - 1);
807 else if (!strcmp(argv[1], "styles"))
809 const char styles[] = "\xf0\0\0\0";
810 const char dimensions[] = "-,,,.\0";
811 start = find(styles, sizeof styles - 1);
812 n = find(dimensions, sizeof dimensions - 1) + sizeof dimensions - 1;
814 else if (!strcmp(argv[1], "dimensions") || !strcmp(argv[1], "all"))
817 match_byte_assert(1);
818 match_byte_assert(0);
820 match_byte_assert(1);
822 match_byte_assert(1);
823 match_byte_assert(0);
824 match_byte_assert(0);
826 match_byte_assert(1);
828 match_byte_assert(0);
829 match_byte_assert(0);
830 match_byte_assert(0);
834 printf("\n\ndata:\n");
837 match_byte_assert (1);
840 fprintf (stderr, "%x / %x\n", pos, n);
847 fprintf (stderr, "unknown section %s\n", argv[1]);
854 for (size_t i = start; i < n; )
861 && i + 4 + data[i] + data[i + 1] * 256 <= n
862 && all_ascii(&data[i + 4], data[i] + data[i + 1] * 256))
864 fputs("\n\"", stdout);
865 fwrite(&data[i + 4], 1, data[i] + data[i + 1] * 256, stdout);
866 fputs("\" ", stdout);
868 i += 4 + data[i] + data[i + 1] * 256;
877 memcpy (&d, &data[i + 4], 8);
878 printf ("F40.%d(%.*f)\n", data[i], data[i], d);
888 memcpy (&d, &data[i + 4], 8);
889 printf ("PCT40.%d(%.*f)\n", data[i], data[i], d);
893 && (data[i] && data[i] != 88 && data[i] != 0x41)
898 printf ("i%d ", data[i]);
903 printf("%02x ", data[i]);