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)
210 for (int i = 0; i <= level; i++)
219 char *s1 = get_string();
221 char *s2 = get_string();
222 char *s3 = get_string();
224 printf("strings \"%s\", \"%s\" and \"%s\"", s1, s2, s3);
226 printf("string \"%s\" and \"%s\"", s1, s2);
234 else if (match_byte (5))
236 match_byte_assert (0x58);
237 printf ("variable \"%s\"", get_string());
239 if (!match_byte(1) && !match_byte(2))
240 match_byte_assert(3);
245 else if (match_byte (2))
251 match_byte_assert (0x58);
253 value = get_double ();
255 vallab = get_string ();
256 printf ("value %g format %d(%d.%d) var \"%s\" vallab \"%s\"",
257 value, format >> 16, (format >> 8) & 0xff, format & 0xff, var, vallab);
258 if (!match_byte (1) && !match_byte(2))
259 match_byte_assert (3);
264 else if (match_byte (4))
267 char *var, *vallab, *value;
269 match_byte_assert (0x58);
271 vallab = get_string ();
273 if (!match_byte(1) && !match_byte(2))
274 match_byte_assert (3);
275 value = get_string ();
276 printf ("value \"%s\" format %d(%d.%d) var \"%s\" vallab \"%s\"",
277 value, format >> 16, (format >> 8) & 0xff, format & 0xff, var, vallab);
282 else if (match_byte (1))
289 value = get_double ();
290 printf ("value %g format %d(%d.%d)", value, format >> 16, (format >> 8) & 0xff, format & 0xff);
299 char *base = get_string();
301 printf ("\"%s\" with %d variables:\n", base, x);
304 for (int i = 0; i < x; i++)
306 dump_value (level+1);
312 for (int i = 0; i < x; i++)
316 for (int j = 0; j <= level; j++)
318 printf("variable %d has %d values:\n", i, y);
319 for (int j = 0; j < y; j++)
323 char *a = get_string();
324 match_byte_assert(0x58);
325 char *b = get_string();
326 char *c = get_string();
327 for (int k = 0; k <= level + 1; k++)
329 printf ("\"%s\", \"%s\", \"%s\"", a, b, c);
337 dump_value (level+1);
346 dump_dim_value(int level)
348 for (int i = 0; i <= level; i++)
356 printf("string \"%s\"", get_string());
360 else if (match_byte (5))
362 match_byte_assert (0x58);
363 printf ("variable \"%s\"", get_string());
366 match_byte_assert(3);
368 else if (match_byte (2))
374 match_byte_assert (0x58);
376 value = get_double ();
378 vallab = get_string ();
379 printf ("value %g format %d(%d.%d) var \"%s\" vallab \"%s\"",
380 value, format >> 16, (format >> 8) & 0xff, format & 0xff, var, vallab);
382 match_u32_assert (2);
385 else if (match_byte (1))
390 match_byte_assert (0x58);
392 value = get_double ();
393 printf ("value %g format %d(%d.%d)", value, format >> 16, (format >> 8) & 0xff, format & 0xff);
404 printf ("; \"%s\", substitutions:", get_string());
405 int total_subs = get_u32();
409 total_subs = (total_subs - 1) + x;
410 match_u32_assert (0);
412 printf (" (total %d)", total_subs);
414 for (int i = 0; i < total_subs; i++)
417 dump_value (level + 1);
423 dump_category(int level)
433 else if (match_u32 (1))
440 else if (match_byte (1))
444 match_u32_assert (1);
450 match_u32_assert (0);
454 int n_categories = get_u32();
455 if (n_categories > 0)
456 printf (", %d subcategories:", n_categories);
458 for (int i = 0; i < n_categories; i++)
459 dump_category (level + 1);
466 printf("next dim\n");
470 /* This byte is usually 0x02 but 0x00 and 0x75 (!) have also been spotted. */
473 if (!match_byte(0) && !match_byte(1))
474 match_byte_assert(2);
478 match_byte_assert(1);
488 n_categories = get_u32();
489 printf("%d nested categories\n", n_categories);
490 for (int i = 0; i < n_categories; i++)
499 printf ("%u dimensions\n", n_dims);
500 for (int i = 0; i < n_dims; i++)
508 dump_data_value(void)
521 value = get_double ();
522 printf (" value %g format %d(%d.%d)", value, format >> 16, (format >> 8) & 0xff, format & 0xff);
524 else if (match_byte (3))
529 printf("string \"%s\"", get_string());
532 else if (match_byte (2))
538 match_byte_assert (0x58);
540 value = get_double ();
542 vallab = get_string ();
543 printf ("value %g format %d(%d.%d) var \"%s\" vallab \"%s\"",
544 value, format >> 16, (format >> 8) & 0xff, format & 0xff, var, vallab);
545 if (!match_byte (1) && !match_byte(2))
546 match_byte_assert (3);
548 else if (match_byte (4))
551 char *var, *vallab, *value;
553 match_byte_assert (0x58);
555 vallab = get_string ();
557 if (!match_byte(1) && !match_byte(2))
558 match_byte_assert (3);
559 value = get_string ();
560 printf ("value \"%s\" format %d(%d.%d) var \"%s\" vallab \"%s\"",
561 value, format >> 16, (format >> 8) & 0xff, format & 0xff, var, vallab);
563 else if (match_byte (5))
565 match_byte_assert (0x58);
566 printf ("variable \"%s\"", get_string());
568 if (!match_byte(1) && !match_byte(2))
569 match_byte_assert(3);
578 char *base = get_string();
580 printf ("\"%s\"; %d variables:\n", base, x);
581 for (int i = 0; i < x; i++)
588 for (int j = 0; j <= 0; j++)
590 printf("variable %d has %d values:\n", i, y);
591 for (int j = 0; j < y; j++)
602 for (int i = 0; i < 3 + n_dims; i++)
604 printf ("data intro:");
605 for (int i = 0; i < 3 + n_dims; i++)
609 fprintf (stderr,"data intro (%d dims):", n_dims);
610 for (int i = 0; i < 3+n_dims; i++)
611 fprintf (stderr," %d", get_u32());
612 fprintf(stderr,"\n");
615 printf ("%d data values, starting at %08x\n", x, pos);
616 for (int i = 0; i < x; i++)
618 printf("%08x, index %d:\n", pos, get_u32());
626 dump_title_value(int level)
628 for (int i = 0; i <= level; i++)
641 printf("string \"%s\"", get_string());
652 else if (match_byte (5))
655 printf ("variable \"%s\"", get_string());
657 if (!match_byte(1) && !match_byte(2))
658 match_byte_assert(3);
660 else if (match_byte (2))
666 match_byte_assert (0x58);
668 value = get_double ();
670 vallab = get_string ();
671 printf ("value %g format %d(%d.%d) var \"%s\" vallab \"%s\"",
672 value, format >> 16, (format >> 8) & 0xff, format & 0xff, var, vallab);
673 if (!match_byte (1) && !match_byte(2))
674 match_byte_assert (3);
683 else if (match_byte (4))
686 char *var, *vallab, *value;
688 match_byte_assert (0x58);
690 vallab = get_string ();
692 if (!match_byte(1) && !match_byte(2))
693 match_byte_assert (3);
694 value = get_string ();
695 printf ("value \"%s\" format %d(%d.%d) var \"%s\" vallab \"%s\"",
696 value, format >> 16, (format >> 8) & 0xff, format & 0xff, var, vallab);
702 else if (match_byte (1))
709 value = get_double ();
710 printf ("value %g format %d(%d.%d)", value, format >> 16, (format >> 8) & 0xff, format & 0xff);
721 char *base = get_string();
723 printf ("\"%s\" with %d variables:\n", base, x);
724 for (int i = 0; i < x; i++)
731 for (int j = 0; j <= level; j++)
733 printf("variable %d has %d values:\n", i, y);
734 for (int j = 0; j < y; j++)
739 char *a = get_string();
740 match_byte_assert(0x58);
741 char *b = get_string();
742 char *c = get_string();
743 for (int k = 0; k <= level + 1; k++)
745 printf ("\"%s\", \"%s\", \"%s\"", a, b, c);
748 dump_title_value (level+1);
756 dump_footnote_value(int level)
758 for (int i = 0; i <= level; i++)
770 printf("string \"%s\"", get_string());
772 match_byte_assert (1);
774 else if (match_byte (5))
776 match_byte_assert (0x58);
777 printf ("variable \"%s\"", get_string());
779 if (!match_byte(1) && !match_byte(2))
780 match_byte_assert(3);
782 else if (match_byte (2))
788 match_byte_assert (0x58);
790 value = get_double ();
792 vallab = get_string ();
793 printf ("value %g format %d(%d.%d) var \"%s\" vallab \"%s\"",
794 value, format >> 16, (format >> 8) & 0xff, format & 0xff, var, vallab);
795 if (!match_byte (1) && !match_byte(2))
796 match_byte_assert (3);
805 else if (match_byte (4))
808 char *var, *vallab, *value;
810 match_byte_assert (0x58);
812 vallab = get_string ();
814 if (!match_byte(1) && !match_byte(2))
815 match_byte_assert (3);
816 value = get_string ();
817 printf ("value \"%s\" format %d(%d.%d) var \"%s\" vallab \"%s\"",
818 value, format >> 16, (format >> 8) & 0xff, format & 0xff, var, vallab);
824 else if (match_byte (1))
831 value = get_double ();
832 printf ("value %g format %d(%d.%d)", value, format >> 16, (format >> 8) & 0xff, format & 0xff);
837 char *base = get_string();
839 printf ("\"%s\"; %d variables:\n", base, x);
840 for (int i = 0; i < x; i++)
847 for (int j = 0; j <= level; j++)
849 printf("variable %d has %d values:\n", i, y);
850 for (int j = 0; j < y; j++)
854 char *a = get_string();
855 match_byte_assert(0x58);
856 char *b = get_string();
857 char *c = get_string();
858 for (int k = 0; k <= level + 1; k++)
860 printf ("\"%s\", \"%s\", \"%s\"", a, b, c);
862 match_byte_assert(0);
865 dump_footnote_value (level+1);
876 dump_title_value(0); putchar('\n');
877 dump_title_value(0); putchar('\n');
878 match_byte_assert(0x31);
879 dump_title_value(0); putchar('\n');
881 match_byte_assert(0x58);
882 if (match_byte(0x31))
884 dump_footnote_value(0); putchar('\n');
887 match_byte_assert(0x58);
890 int n_footnotes = get_u32();
891 if (n_footnotes >= 20)
893 fprintf(stderr, "%08x: %d footnotes\n", pos - 4, n_footnotes);
897 printf("------\n%d footnotes\n", n_footnotes);
898 if (n_footnotes < 20)
900 for (int i = 0; i < n_footnotes; i++)
902 printf("footnote %d:\n", i);
903 dump_footnote_value(0);
908 if (match_byte (0x31))
910 /* Custom footnote marker string. */
911 match_byte_assert(3);
913 match_byte_assert(0x58);
918 match_byte_assert (0x58);
919 printf("(%d)\n", get_u32());
925 find_dimensions(void)
928 const char dimensions[] = "-,,,.\0";
929 int x = try_find_tail(dimensions, sizeof dimensions - 1);
934 const char dimensions[] = "-,,, .\0";
935 return find_tail(dimensions, sizeof dimensions - 1);
941 printf("fonts: offset=%08x\n", pos);
943 for (int i = 1; i <= 8; i++)
945 printf("%08x: font %d, ", pos, i);
946 match_byte_assert(i);
947 match_byte_assert(0x31);
948 printf("%s, ", get_string());
949 match_byte_assert(0);
950 match_byte_assert(0);
951 if (!match_byte(0x40) && !match_byte(0x20) && !match_byte(0x80) && !match_byte(0x10))
952 match_byte_assert(0x50);
953 if (!match_byte(0x41))
954 match_byte_assert(0x51);
956 printf ("%s, ", get_string());
957 printf ("%s, ", get_string());
968 match_u32_assert(240);
971 match_u32_assert(18);
978 match_u32_assert(142);
982 int count = get_u32();
985 char *encoding = get_string();
986 printf("encoding=%s\n", encoding);
989 match_u32_assert(UINT32_MAX);
991 match_byte_assert(1);
992 match_byte_assert(0);
994 match_byte_assert(1);
995 if (!match_byte(0x99) && !match_byte(0x98))
996 match_byte_assert(0x97);
997 match_byte_assert(7);
998 match_byte_assert(0);
999 match_byte_assert(0);
1000 if (match_byte('.'))
1001 match_byte_assert(',');
1004 match_byte_assert(',');
1005 if (!match_byte('.'))
1006 match_byte_assert(' ');
1008 match_u32_assert(5);
1009 for (int i = 0; i < 5; i++)
1012 if (pos != find_dimensions())
1013 fprintf (stderr, "%08x / %08x\n", pos, find_dimensions());
1017 main(int argc, char *argv[])
1022 if (isatty(STDIN_FILENO))
1024 fprintf(stderr, "redirect stdin from a .bin file\n");
1027 if (fstat(STDIN_FILENO, &s))
1039 if (read(STDIN_FILENO, data, n) != n)
1047 if (!strcmp(argv[1], "title0"))
1050 if (match_byte (0x03)
1051 || (match_byte (0x05) && match_byte (0x58)))
1052 printf ("%s\n", get_string());
1054 printf ("<unknown>\n");
1057 else if (!strcmp(argv[1], "title"))
1062 else if (!strcmp(argv[1], "titleraw"))
1064 const char fonts[] = "\x01\x31\x09\0\0\0SansSerif";
1066 n = find(fonts, sizeof fonts - 1);
1068 else if (!strcmp(argv[1], "fonts"))
1070 const char fonts[] = "\x01\x31\x09\0\0\0SansSerif";
1071 const char styles[] = "\xf0\0\0\0";
1072 start = find(fonts, sizeof fonts - 1);
1073 n = find(styles, sizeof styles - 1);
1075 else if (!strcmp(argv[1], "styles"))
1077 const char styles[] = "\xf0\0\0\0";
1078 const char dimensions[] = "-,,,.\0";
1079 start = find(styles, sizeof styles - 1);
1080 n = find(dimensions, sizeof dimensions - 1) + sizeof dimensions - 1;
1082 else if (!strcmp(argv[1], "dimensions") || !strcmp(argv[1], "all"))
1085 match_byte_assert(1);
1086 match_byte_assert(0);
1087 match_u32_assert(3);
1088 match_byte_assert(1);
1090 match_byte_assert(1);
1091 match_byte_assert(0);
1092 match_byte_assert(0);
1094 match_byte_assert(1);
1096 match_byte_assert(0);
1097 match_byte_assert(0);
1098 match_byte_assert(0);
1102 printf("\n\ndata:\n");
1105 match_byte_assert (1);
1108 fprintf (stderr, "%x / %x\n", pos, n);
1115 fprintf (stderr, "unknown section %s\n", argv[1]);
1122 for (size_t i = start; i < n; )
1129 && i + 4 + data[i] + data[i + 1] * 256 <= n
1130 && all_ascii(&data[i + 4], data[i] + data[i + 1] * 256))
1132 fputs("\n\"", stdout);
1133 fwrite(&data[i + 4], 1, data[i] + data[i + 1] * 256, stdout);
1134 fputs("\" ", stdout);
1136 i += 4 + data[i] + data[i + 1] * 256;
1138 else if (i + 12 <= n
1139 && data[i + 1] == 40
1141 && data[i + 3] == 0)
1145 memcpy (&d, &data[i + 4], 8);
1146 printf ("F40.%d(%.*f)\n", data[i], data[i], d);
1149 else if (i + 12 <= n
1150 && data[i + 1] == 40
1151 && data[i + 2] == 31
1152 && data[i + 3] == 0)
1156 memcpy (&d, &data[i + 4], 8);
1157 printf ("PCT40.%d(%.*f)\n", data[i], data[i], d);
1161 && (data[i] && data[i] != 88 && data[i] != 0x41)
1166 printf ("i%d ", data[i]);
1171 printf("%02x ", data[i]);