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))
157 match_u32_assert (0);
158 int subn = get_u32 ();
159 printf ("nested %d bytes", subn);
162 else if (match_u32 (1))
164 printf("(footnote %d) ", get_u32());
165 match_byte_assert (0);
166 match_byte_assert (0);
167 int subn = get_u32 ();
168 printf ("nested %d bytes", subn);
171 else if (match_u32 (2))
173 printf("(special 2)");
174 match_byte_assert(0);
175 match_byte_assert(0);
177 match_byte_assert(0);
178 match_byte_assert(0);
179 int subn = get_u32 ();
180 printf ("nested %d bytes", subn);
186 printf("(special 3)");
187 match_byte_assert(0);
188 match_byte_assert(0);
189 match_byte_assert(1);
190 match_byte_assert(0);
191 int subn = get_u32 ();
192 printf ("nested %d bytes, ", subn);
195 printf ("nested %d bytes, ", subn);
200 match_byte_assert (0x58);
204 dump_value(int level)
206 for (int i = 0; i <= level; i++)
215 char *s1 = get_string();
217 char *s2 = get_string();
218 char *s3 = get_string();
220 printf("strings \"%s\", \"%s\" and \"%s\"", s1, s2, s3);
222 printf("string \"%s\" and \"%s\"", s1, s2);
233 else if (match_byte (5))
235 match_byte_assert (0x58);
236 printf ("variable \"%s\"", get_string());
238 if (!match_byte(1) && !match_byte(2))
239 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);
268 else if (match_byte (4))
271 char *var, *vallab, *value;
273 match_byte_assert (0x58);
275 vallab = get_string ();
277 if (!match_byte(1) && !match_byte(2))
278 match_byte_assert (3);
279 value = get_string ();
280 printf ("value \"%s\" format %d(%d.%d) var \"%s\" vallab \"%s\"",
281 value, format >> 16, (format >> 8) & 0xff, format & 0xff, var, vallab);
287 else if (match_byte (1))
294 value = get_double ();
295 printf ("value %g format %d(%d.%d)", value, format >> 16, (format >> 8) & 0xff, format & 0xff);
305 char *base = get_string();
307 printf ("\"%s\" with %d variables:\n", base, x);
310 for (int i = 0; i < x; i++)
312 dump_value (level+1);
318 for (int i = 0; i < x; i++)
322 for (int j = 0; j <= level; j++)
324 printf("variable %d has %d values:\n", i, y);
325 for (int j = 0; j < y; j++)
329 char *a = get_string();
330 match_byte_assert(0x58);
331 char *b = get_string();
332 char *c = get_string();
333 for (int k = 0; k <= level + 1; k++)
335 printf ("\"%s\", \"%s\", \"%s\"", a, b, c);
343 dump_value (level+1);
352 dump_dim_value_31(void)
354 if (match_byte (0x31))
358 match_u32_assert (0);
359 int subn = get_u32 ();
360 printf ("nested %d bytes", subn);
363 else if (match_u32 (1))
365 printf("(footnote %d) ", get_u32());
366 match_byte_assert (0);
367 match_byte_assert (0);
368 int subn = get_u32 ();
369 printf ("nested %d bytes", subn);
374 match_byte_assert (0x58);
378 dump_dim_value(int level)
380 for (int i = 0; i <= level; i++)
388 printf("string \"%s\"", get_string());
392 else if (match_byte (5))
394 match_byte_assert (0x58);
395 printf ("variable \"%s\"", get_string());
398 match_byte_assert(3);
400 else if (match_byte (2))
406 match_byte_assert (0x58);
408 value = get_double ();
410 vallab = get_string ();
411 printf ("value %g format %d(%d.%d) var \"%s\" vallab \"%s\"",
412 value, format >> 16, (format >> 8) & 0xff, format & 0xff, var, vallab);
414 match_u32_assert (2);
417 else if (match_byte (1))
422 match_byte_assert (0x58);
424 value = get_double ();
425 printf ("value %g format %d(%d.%d)", value, format >> 16, (format >> 8) & 0xff, format & 0xff);
436 printf ("; \"%s\", substitutions:", get_string());
437 int total_subs = get_u32();
441 total_subs = (total_subs - 1) + x;
442 match_u32_assert (0);
444 printf (" (total %d)", total_subs);
446 for (int i = 0; i < total_subs; i++)
449 dump_value (level + 1);
455 dump_category(int level)
465 else if (match_u32 (1))
472 else if (match_byte (1))
476 match_u32_assert (1);
482 match_u32_assert (0);
486 int n_categories = get_u32();
487 if (n_categories > 0)
488 printf (", %d subcategories:", n_categories);
490 for (int i = 0; i < n_categories; i++)
491 dump_category (level + 1);
498 printf("next dim\n");
502 /* This byte is usually 0x02 but 0x00 and 0x75 (!) have also been spotted. */
505 if (!match_byte(0) && !match_byte(1))
506 match_byte_assert(2);
510 match_byte_assert(1);
520 n_categories = get_u32();
521 printf("%d nested categories\n", n_categories);
522 for (int i = 0; i < n_categories; i++)
531 printf ("%u dimensions\n", n_dims);
532 for (int i = 0; i < n_dims; i++)
540 dump_data_value_31(void)
542 if (match_byte (0x31))
549 match_u32_assert (0);
550 int subn = get_u32 ();
551 printf ("nested %d bytes", subn);
554 else if (match_u32 (1))
556 printf("(footnote %d) ", get_u32());
557 match_byte_assert (0);
558 match_byte_assert (0);
559 int subn = get_u32 ();
560 printf ("nested %d bytes", subn);
563 else if (match_u32 (2))
565 printf("(special 2)");
566 match_byte_assert(0);
567 match_byte_assert(0);
569 match_byte_assert(0);
570 match_byte_assert(0);
571 int subn = get_u32 ();
572 printf ("nested %d bytes", subn);
578 printf("(special 3)");
579 match_byte_assert(0);
580 match_byte_assert(0);
581 match_byte_assert(1);
582 match_byte_assert(0);
583 int subn = get_u32 ();
584 printf ("nested %d bytes, ", subn);
587 printf ("nested %d bytes, ", subn);
592 match_byte_assert (0x58);
596 dump_data_value(void)
607 dump_data_value_31();
609 value = get_double ();
610 printf (" value %g format %d(%d.%d)", value, format >> 16, (format >> 8) & 0xff, format & 0xff);
612 else if (match_byte (3))
615 dump_data_value_31();
617 printf("string \"%s\"", get_string());
620 else if (match_byte (2))
626 match_byte_assert (0x58);
628 value = get_double ();
630 vallab = get_string ();
631 printf ("value %g format %d(%d.%d) var \"%s\" vallab \"%s\"",
632 value, format >> 16, (format >> 8) & 0xff, format & 0xff, var, vallab);
633 if (!match_byte (1) && !match_byte(2))
634 match_byte_assert (3);
636 else if (match_byte (4))
639 char *var, *vallab, *value;
641 match_byte_assert (0x58);
643 vallab = get_string ();
645 if (!match_byte(1) && !match_byte(2))
646 match_byte_assert (3);
647 value = get_string ();
648 printf ("value \"%s\" format %d(%d.%d) var \"%s\" vallab \"%s\"",
649 value, format >> 16, (format >> 8) & 0xff, format & 0xff, var, vallab);
651 else if (match_byte (5))
653 match_byte_assert (0x58);
654 printf ("variable \"%s\"", get_string());
656 if (!match_byte(1) && !match_byte(2))
657 match_byte_assert(3);
665 dump_data_value_31();
666 char *base = get_string();
668 printf ("\"%s\"; %d variables:\n", base, x);
669 for (int i = 0; i < x; i++)
676 for (int j = 0; j <= 0; j++)
678 printf("variable %d has %d values:\n", i, y);
679 for (int j = 0; j < y; j++)
690 for (int i = 0; i < 3 + n_dims; i++)
692 printf ("data intro:");
693 for (int i = 0; i < 3 + n_dims; i++)
697 fprintf (stderr,"data intro (%d dims):", n_dims);
698 for (int i = 0; i < 3+n_dims; i++)
699 fprintf (stderr," %d", get_u32());
700 fprintf(stderr,"\n");
703 printf ("%d data values, starting at %08x\n", x, pos);
704 for (int i = 0; i < x; i++)
706 printf("%08x, index %d:\n", pos, get_u32());
714 dump_title_value_31(int level)
716 if (match_byte (0x31))
720 match_u32_assert (0);
721 int subn = get_u32 ();
722 printf ("nested %d bytes", subn);
725 else if (match_u32 (1))
727 printf("(footnote %d) ", get_u32());
728 match_byte_assert (0);
729 match_byte_assert (0);
730 int subn = get_u32 ();
731 printf ("nested %d bytes", subn);
734 else if (match_u32 (2))
736 printf("(special 2)");
737 match_byte_assert(0);
738 match_byte_assert(0);
741 match_byte_assert(0);
742 match_byte_assert(0);
743 int subn = get_u32 ();
744 printf ("nested %d bytes", subn);
750 printf("(special 3)");
751 match_byte_assert(0);
752 match_byte_assert(0);
753 match_byte_assert(1);
754 match_byte_assert(0);
755 int subn = get_u32 ();
756 printf ("nested %d bytes, ", subn);
759 printf ("nested %d bytes, ", subn);
764 match_byte_assert (0x58);
768 dump_title_value(int level)
770 for (int i = 0; i <= level; i++)
781 dump_title_value_31(level);
783 printf("string \"%s\"", get_string());
794 else if (match_byte (5))
796 dump_title_value_31(level);
797 printf ("variable \"%s\"", get_string());
799 if (!match_byte(1) && !match_byte(2))
800 match_byte_assert(3);
802 else if (match_byte (2))
808 match_byte_assert (0x58);
810 value = get_double ();
812 vallab = get_string ();
813 printf ("value %g format %d(%d.%d) var \"%s\" vallab \"%s\"",
814 value, format >> 16, (format >> 8) & 0xff, format & 0xff, var, vallab);
815 if (!match_byte (1) && !match_byte(2))
816 match_byte_assert (3);
825 else if (match_byte (4))
828 char *var, *vallab, *value;
830 match_byte_assert (0x58);
832 vallab = get_string ();
834 if (!match_byte(1) && !match_byte(2))
835 match_byte_assert (3);
836 value = get_string ();
837 printf ("value \"%s\" format %d(%d.%d) var \"%s\" vallab \"%s\"",
838 value, format >> 16, (format >> 8) & 0xff, format & 0xff, var, vallab);
844 else if (match_byte (1))
849 dump_title_value_31(level);
851 value = get_double ();
852 printf ("value %g format %d(%d.%d)", value, format >> 16, (format >> 8) & 0xff, format & 0xff);
861 dump_title_value_31(level);
863 char *base = get_string();
865 printf ("\"%s\" with %d variables:\n", base, x);
866 for (int i = 0; i < x; i++)
873 for (int j = 0; j <= level; j++)
875 printf("variable %d has %d values:\n", i, y);
876 for (int j = 0; j < y; j++)
881 char *a = get_string();
882 match_byte_assert(0x58);
883 char *b = get_string();
884 char *c = get_string();
885 for (int k = 0; k <= level + 1; k++)
887 printf ("\"%s\", \"%s\", \"%s\"", a, b, c);
890 dump_title_value (level+1);
898 dump_footnote_value_31(void)
900 if (match_byte (0x31))
905 match_u32_assert (0);
906 int subn = get_u32 ();
907 printf ("nested %d bytes", subn);
910 else if (match_u32 (1))
912 printf("(footnote %d) ", get_u32());
913 match_byte_assert (0);
914 match_byte_assert (0);
915 int subn = get_u32 ();
916 printf ("nested %d bytes", subn);
919 else if (match_u32 (2))
921 printf("(special 2)");
922 match_byte_assert(0);
923 match_byte_assert(0);
925 match_byte_assert(0);
926 match_byte_assert(0);
927 int subn = get_u32 ();
928 printf ("nested %d bytes", subn);
934 printf("(special 3)");
935 match_byte_assert(0);
936 match_byte_assert(0);
937 match_byte_assert(1);
938 match_byte_assert(0);
939 int subn = get_u32 ();
940 printf ("nested %d bytes, ", subn);
943 printf ("nested %d bytes, ", subn);
948 match_byte_assert (0x58);
952 dump_footnote_value(int level)
954 for (int i = 0; i <= level; i++)
964 dump_footnote_value_31();
966 printf("string \"%s\"", get_string());
968 match_byte_assert (1);
970 else if (match_byte (5))
972 match_byte_assert (0x58);
973 printf ("variable \"%s\"", get_string());
975 if (!match_byte(1) && !match_byte(2))
976 match_byte_assert(3);
978 else if (match_byte (2))
984 match_byte_assert (0x58);
986 value = get_double ();
988 vallab = get_string ();
989 printf ("value %g format %d(%d.%d) var \"%s\" vallab \"%s\"",
990 value, format >> 16, (format >> 8) & 0xff, format & 0xff, var, vallab);
991 if (!match_byte (1) && !match_byte(2))
992 match_byte_assert (3);
1001 else if (match_byte (4))
1003 unsigned int format;
1004 char *var, *vallab, *value;
1006 match_byte_assert (0x58);
1007 format = get_u32 ();
1008 vallab = get_string ();
1009 var = get_string ();
1010 if (!match_byte(1) && !match_byte(2))
1011 match_byte_assert (3);
1012 value = get_string ();
1013 printf ("value \"%s\" format %d(%d.%d) var \"%s\" vallab \"%s\"",
1014 value, format >> 16, (format >> 8) & 0xff, format & 0xff, var, vallab);
1020 else if (match_byte (1))
1022 unsigned int format;
1025 dump_footnote_value_31();
1026 format = get_u32 ();
1027 value = get_double ();
1028 printf ("value %g format %d(%d.%d)", value, format >> 16, (format >> 8) & 0xff, format & 0xff);
1032 dump_footnote_value_31();
1033 char *base = get_string();
1035 printf ("\"%s\"; %d variables:\n", base, x);
1036 for (int i = 0; i < x; i++)
1042 match_u32_assert(0);
1043 for (int j = 0; j <= level; j++)
1045 printf("variable %d has %d values:\n", i, y);
1046 for (int j = 0; j < y; j++)
1050 char *a = get_string();
1051 match_byte_assert(0x58);
1052 char *b = get_string();
1053 char *c = get_string();
1054 for (int k = 0; k <= level + 1; k++)
1056 printf ("\"%s\", \"%s\", \"%s\"", a, b, c);
1058 match_byte_assert(0);
1061 dump_footnote_value (level+1);
1072 dump_title_value(0); putchar('\n');
1073 dump_title_value(0); putchar('\n');
1074 match_byte_assert(0x31);
1075 dump_title_value(0); putchar('\n');
1077 match_byte_assert(0x58);
1078 if (match_byte(0x31))
1080 dump_footnote_value(0); putchar('\n');
1083 match_byte_assert(0x58);
1086 int n_footnotes = get_u32();
1087 if (n_footnotes >= 20)
1089 fprintf(stderr, "%08x: %d footnotes\n", pos - 4, n_footnotes);
1093 printf("------\n%d footnotes\n", n_footnotes);
1094 if (n_footnotes < 20)
1096 for (int i = 0; i < n_footnotes; i++)
1098 printf("footnote %d:\n", i);
1099 dump_footnote_value(0);
1104 if (match_byte (0x31))
1106 /* Custom footnote marker string. */
1107 match_byte_assert(3);
1109 match_byte_assert(0x58);
1110 match_u32_assert(0);
1114 match_byte_assert (0x58);
1115 printf("(%d)\n", get_u32());
1121 find_dimensions(void)
1124 const char dimensions[] = "-,,,.\0";
1125 int x = try_find_tail(dimensions, sizeof dimensions - 1);
1130 const char dimensions[] = "-,,, .\0";
1131 return find_tail(dimensions, sizeof dimensions - 1);
1137 printf("fonts: offset=%08x\n", pos);
1139 for (int i = 1; i <= 8; i++)
1141 printf("%08x: font %d, ", pos, i);
1142 match_byte_assert(i);
1143 match_byte_assert(0x31);
1144 printf("%s, ", get_string());
1145 match_byte_assert(0);
1146 match_byte_assert(0);
1147 if (!match_byte(0x40) && !match_byte(0x20) && !match_byte(0x80) && !match_byte(0x10))
1148 match_byte_assert(0x50);
1149 if (!match_byte(0x41))
1150 match_byte_assert(0x51);
1152 printf ("%s, ", get_string());
1153 printf ("%s, ", get_string());
1154 match_u32_assert(0);
1155 match_u32_assert(0);
1164 match_u32_assert(240);
1167 match_u32_assert(18);
1174 match_u32_assert(142);
1178 int count = get_u32();
1181 char *encoding = get_string();
1182 printf("encoding=%s\n", encoding);
1185 match_u32_assert(UINT32_MAX);
1187 match_byte_assert(1);
1188 match_byte_assert(0);
1190 match_byte_assert(1);
1191 if (!match_byte(0x99) && !match_byte(0x98))
1192 match_byte_assert(0x97);
1193 match_byte_assert(7);
1194 match_byte_assert(0);
1195 match_byte_assert(0);
1196 if (match_byte('.'))
1197 match_byte_assert(',');
1200 match_byte_assert(',');
1201 if (!match_byte('.'))
1202 match_byte_assert(' ');
1204 match_u32_assert(5);
1205 for (int i = 0; i < 5; i++)
1208 if (pos != find_dimensions())
1209 fprintf (stderr, "%08x / %08x\n", pos, find_dimensions());
1213 main(int argc, char *argv[])
1218 if (isatty(STDIN_FILENO))
1220 fprintf(stderr, "redirect stdin from a .bin file\n");
1223 if (fstat(STDIN_FILENO, &s))
1235 if (read(STDIN_FILENO, data, n) != n)
1243 if (!strcmp(argv[1], "title0"))
1246 if (match_byte (0x03)
1247 || (match_byte (0x05) && match_byte (0x58)))
1248 printf ("%s\n", get_string());
1250 printf ("<unknown>\n");
1253 else if (!strcmp(argv[1], "title"))
1258 else if (!strcmp(argv[1], "titleraw"))
1260 const char fonts[] = "\x01\x31\x09\0\0\0SansSerif";
1262 n = find(fonts, sizeof fonts - 1);
1264 else if (!strcmp(argv[1], "fonts"))
1266 const char fonts[] = "\x01\x31\x09\0\0\0SansSerif";
1267 const char styles[] = "\xf0\0\0\0";
1268 start = find(fonts, sizeof fonts - 1);
1269 n = find(styles, sizeof styles - 1);
1271 else if (!strcmp(argv[1], "styles"))
1273 const char styles[] = "\xf0\0\0\0";
1274 const char dimensions[] = "-,,,.\0";
1275 start = find(styles, sizeof styles - 1);
1276 n = find(dimensions, sizeof dimensions - 1) + sizeof dimensions - 1;
1278 else if (!strcmp(argv[1], "dimensions") || !strcmp(argv[1], "all"))
1281 match_byte_assert(1);
1282 match_byte_assert(0);
1283 match_u32_assert(3);
1284 match_byte_assert(1);
1286 match_byte_assert(1);
1287 match_byte_assert(0);
1288 match_byte_assert(0);
1290 match_byte_assert(1);
1292 match_byte_assert(0);
1293 match_byte_assert(0);
1294 match_byte_assert(0);
1298 printf("\n\ndata:\n");
1301 match_byte_assert (1);
1304 fprintf (stderr, "%x / %x\n", pos, n);
1311 fprintf (stderr, "unknown section %s\n", argv[1]);
1318 for (size_t i = start; i < n; )
1325 && i + 4 + data[i] + data[i + 1] * 256 <= n
1326 && all_ascii(&data[i + 4], data[i] + data[i + 1] * 256))
1328 fputs("\n\"", stdout);
1329 fwrite(&data[i + 4], 1, data[i] + data[i + 1] * 256, stdout);
1330 fputs("\" ", stdout);
1332 i += 4 + data[i] + data[i + 1] * 256;
1334 else if (i + 12 <= n
1335 && data[i + 1] == 40
1337 && data[i + 3] == 0)
1341 memcpy (&d, &data[i + 4], 8);
1342 printf ("F40.%d(%.*f)\n", data[i], data[i], d);
1345 else if (i + 12 <= n
1346 && data[i + 1] == 40
1347 && data[i + 2] == 31
1348 && data[i + 3] == 0)
1352 memcpy (&d, &data[i + 4], 8);
1353 printf ("PCT40.%d(%.*f)\n", data[i], data[i], d);
1357 && (data[i] && data[i] != 88 && data[i] != 0x41)
1362 printf ("i%d ", data[i]);
1367 printf("%02x ", data[i]);