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)
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)
151 dump_value(int level)
153 for (int i = 0; i <= level; i++)
163 if (match_byte (0x31))
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);
180 match_byte_assert(0);
181 match_byte_assert(0);
182 int subn = get_u32 ();
183 printf ("nested %d bytes", subn);
189 printf("(special 3)");
190 match_byte_assert(0);
191 match_byte_assert(0);
192 match_byte_assert(1);
193 match_byte_assert(0);
194 int subn = get_u32 ();
195 printf ("nested %d bytes, ", subn);
198 printf ("nested %d bytes, ", subn);
203 match_byte_assert (0x58);
205 printf("string \"%s\"", get_string());
216 else if (match_byte (5))
218 match_byte_assert (0x58);
219 printf ("variable \"%s\"", get_string());
221 if (!match_byte(1) && !match_byte(2))
222 match_byte_assert(3);
228 else if (match_byte (2))
234 match_byte_assert (0x58);
236 value = get_double ();
238 vallab = get_string ();
239 printf ("value %g format %d(%d.%d) var \"%s\" vallab \"%s\"",
240 value, format >> 16, (format >> 8) & 0xff, format & 0xff, var, vallab);
241 if (!match_byte (1) && !match_byte(2))
242 match_byte_assert (3);
251 else if (match_byte (4))
254 char *var, *vallab, *value;
256 match_byte_assert (0x58);
258 vallab = get_string ();
260 if (!match_byte(1) && !match_byte(2))
261 match_byte_assert (3);
262 value = get_string ();
263 printf ("value \"%s\" format %d(%d.%d) var \"%s\" vallab \"%s\"",
264 value, format >> 16, (format >> 8) & 0xff, format & 0xff, var, vallab);
270 else if (match_byte (1))
275 if (match_byte (0x31))
279 printf("(footnote %d) ", get_u32());
280 match_byte_assert (0);
281 match_byte_assert (0);
282 int subn = get_u32 ();
283 printf ("nested %d bytes", subn);
288 match_byte_assert (0x58);
290 value = get_double ();
291 printf ("value %g format %d(%d.%d)", value, format >> 16, (format >> 8) & 0xff, format & 0xff);
298 else if (match_byte (0x31))
302 printf("(footnote %d) ", get_u32());
303 match_byte_assert (0);
304 match_byte_assert (0);
305 int subn = get_u32 ();
306 printf ("nested %d bytes", subn);
311 match_u32_assert (0);
312 match_u32_assert (0);
313 int subn = get_u32 ();
314 printf ("nested %d bytes", subn);
317 char *base = get_string();
319 printf ("\"%s\"; %d variables:\n", base, x);
322 for (int i = 0; i < x; i++)
324 dump_value (level+1);
330 for (int i = 0; i < x; i++)
334 for (int j = 0; j <= level; j++)
336 printf("variable %d has %d values:\n", i, y);
337 for (int j = 0; j < y; j++)
341 char *a = get_string();
342 match_byte_assert(0x58);
343 char *b = get_string();
344 char *c = get_string();
345 for (int k = 0; k <= level + 1; k++)
347 printf ("\"%s\", \"%s\", \"%s\"", a, b, c);
355 dump_value (level+1);
364 match_byte_assert (0x58);
365 char *base = get_string();
367 printf ("\"%s\" with %d variables:\n", base, x);
370 for (int i = 0; i < x; i++)
372 dump_value (level+1);
378 for (int i = 0; i < x; i++)
382 for (int j = 0; j <= level; j++)
384 printf("variable %d has %d values:\n", i, y);
385 for (int j = 0; j < y; j++)
389 char *a = get_string();
390 match_byte_assert(0x58);
391 char *b = get_string();
392 char *c = get_string();
393 for (int k = 0; k <= level + 1; k++)
395 printf ("\"%s\", \"%s\", \"%s\"", a, b, c);
403 dump_value (level+1);
412 dump_dim_value(int level)
414 for (int i = 0; i <= level; i++)
420 if (match_byte (0x31))
423 printf("(footnote %d) ", get_u32());
424 match_byte_assert (0);
425 match_byte_assert (0);
426 int subn = get_u32 ();
427 printf ("nested %d bytes", subn);
431 match_byte_assert (0x58);
433 printf("string \"%s\"", get_string());
435 match_byte_assert (1);
441 else if (match_byte (5))
443 match_byte_assert (0x58);
444 printf ("variable \"%s\"", get_string());
446 match_byte_assert (2);
448 else if (match_byte (2))
454 match_byte_assert (0x58);
456 value = get_double ();
458 vallab = get_string ();
459 printf ("value %g format %d(%d.%d) var \"%s\" vallab \"%s\"",
460 value, format >> 16, (format >> 8) & 0xff, format & 0xff, var, vallab);
462 match_u32_assert (2);
465 else if (match_byte (1))
470 match_byte_assert (0x58);
472 value = get_double ();
473 printf ("value %g format %d(%d.%d)", value, format >> 16, (format >> 8) & 0xff, format & 0xff);
485 match_byte_assert (0x31);
486 match_u32_assert (0);
487 match_u32_assert (0);
489 printf ("nested %d bytes", subn);
491 printf ("; \"%s\", substitutions:", get_string());
492 int total_subs = get_u32();
496 total_subs = (total_subs - 1) + x;
497 match_u32_assert (0);
499 printf (" (total %d)", total_subs);
501 for (int i = 0; i < total_subs; i++)
504 dump_value (level + 1);
510 dump_category(int level)
520 else if (match_u32 (1))
527 else if (match_byte (1))
531 match_u32_assert (1);
537 match_u32_assert (0);
541 int n_categories = get_u32();
542 if (n_categories > 0)
543 printf (", %d subcategories:", n_categories);
545 for (int i = 0; i < n_categories; i++)
546 dump_category (level + 1);
553 printf("next dim\n");
558 match_byte_assert(0x58);
560 printf("string \"%s\": ", get_string());
561 match_byte(1) || match_byte(0);
563 else if (match_byte(5))
565 match_byte_assert(0x58);
566 printf("variable \"%s\": ", get_string());
569 match_byte_assert(3);
571 else if (match_byte(0x31))
576 match_u32_assert (0);
577 match_u32_assert (0);
579 printf ("nested %d bytes", subn);
581 printf ("; \"%s\", substitutions:", get_string());
584 int n_subst = get_u32();
587 printf (" %d", n_subst);
588 total_subs *= n_subst;
591 for (int i = 0; i < total_subs; i++)
601 match_byte_assert (0x58);
602 printf ("\"%s\" with substitutions:", get_string());
605 int n_subst = get_u32();
608 printf (" %d", n_subst);
609 total_subs *= n_subst;
612 for (int i = 0; i < total_subs; i++)
619 /* This byte is usually 0x02 but 0x00 and 0x75 (!) have also been spotted. */
622 if (!match_byte(0) && !match_byte(1))
623 match_byte_assert(2);
627 match_byte_assert(1);
637 n_categories = get_u32();
638 printf("%d nested categories\n", n_categories);
639 for (int i = 0; i < n_categories; i++)
648 printf ("%u dimensions\n", n_dims);
649 for (int i = 0; i < n_dims; i++)
661 for (int i = 0; i < 3 + n_dims; i++)
663 printf ("data intro:");
664 for (int i = 0; i < 3 + n_dims; i++)
668 fprintf (stderr,"data intro (%d dims):", n_dims);
669 for (int i = 0; i < 3+n_dims; i++)
670 fprintf (stderr," %d", get_u32());
671 fprintf(stderr,"\n");
674 printf ("%d data values, starting at %08x\n", x, pos);
675 for (int i = 0; i < x; i++)
677 printf("%08x, index %d:\n", pos, get_u32());
688 if (match_byte (0x31))
695 match_u32_assert (0);
696 int subn = get_u32 ();
697 printf ("nested %d bytes", subn);
700 else if (match_u32 (1))
702 printf("(footnote %d) ", get_u32());
703 match_byte_assert (0);
704 match_byte_assert (0);
705 int subn = get_u32 ();
706 printf ("nested %d bytes", subn);
712 printf("(special 2)");
713 match_byte_assert(0);
714 match_byte_assert(0);
716 match_byte_assert(0);
717 match_byte_assert(0);
718 int subn = get_u32 ();
719 printf ("nested %d bytes", subn);
724 match_byte_assert (0x58);
726 value = get_double ();
727 printf (" value %g format %d(%d.%d)", value, format >> 16, (format >> 8) & 0xff, format & 0xff);
729 else if (match_byte (3))
732 if (match_byte (0x31))
736 match_u32_assert (1);
738 int subn = get_u32 ();
739 printf ("nested %d bytes", subn);
742 else if (match_u32 (1))
744 printf("(footnote %d) ", get_u32());
745 match_byte_assert (0);
746 match_byte_assert (0);
747 int subn = get_u32 ();
748 printf ("nested %d bytes", subn);
751 else if (match_u32 (2))
753 printf("(special 2)");
754 match_byte_assert(0);
755 match_byte_assert(0);
757 match_byte_assert(0);
758 match_byte_assert(0);
759 int subn = get_u32 ();
760 printf ("nested %d bytes", subn);
766 printf("(special 3)");
767 match_byte_assert(0);
768 match_byte_assert(0);
769 match_byte_assert(1);
770 match_byte_assert(0);
771 int subn = get_u32 ();
772 printf ("nested %d bytes, ", subn);
775 printf ("nested %d bytes, ", subn);
780 match_byte_assert (0x58);
782 printf("string \"%s\"", get_string());
785 else if (match_byte (2))
791 match_byte_assert (0x58);
793 value = get_double ();
795 vallab = get_string ();
796 printf ("value %g format %d(%d.%d) var \"%s\" vallab \"%s\"",
797 value, format >> 16, (format >> 8) & 0xff, format & 0xff, var, vallab);
798 if (!match_byte (1) && !match_byte(2))
799 match_byte_assert (3);
801 else if (match_byte (4))
804 char *var, *vallab, *value;
806 match_byte_assert (0x58);
808 vallab = get_string ();
810 if (!match_byte(1) && !match_byte(2))
811 match_byte_assert (3);
812 value = get_string ();
813 printf ("value \"%s\" format %d(%d.%d) var \"%s\" vallab \"%s\"",
814 value, format >> 16, (format >> 8) & 0xff, format & 0xff, var, vallab);
816 else if (match_byte (5))
818 match_byte_assert (0x58);
819 printf ("variable \"%s\"", get_string());
821 if (!match_byte(1) && !match_byte(2))
822 match_byte_assert(3);
828 else if (match_byte(0x31))
832 printf("(footnote %d) ", get_u32());
833 match_byte_assert (0);
834 match_byte_assert (0);
835 int subn = get_u32 ();
836 printf ("nested %d bytes", subn);
841 match_u32_assert (0);
842 match_u32_assert (0);
843 int subn = get_u32 ();
844 printf ("nested %d bytes", subn);
847 char *base = get_string();
849 printf ("\"%s\"; %d variables:\n", base, x);
850 for (int i = 0; i < x; i++)
857 for (int j = 0; j <= 0; j++)
859 printf("variable %d has %d values:\n", i, y);
860 for (int j = 0; j < y; j++)
867 if (match_byte (0x31))
874 match_u32_assert (0);
875 int subn = get_u32 ();
876 printf ("nested %d bytes", subn);
879 else if (match_u32 (1))
881 printf("(footnote %d) ", get_u32());
882 match_byte_assert (0);
883 match_byte_assert (0);
884 int subn = get_u32 ();
885 printf ("nested %d bytes", subn);
891 printf("(special 2)");
892 match_byte_assert(0);
893 match_byte_assert(0);
895 match_byte_assert(0);
896 match_byte_assert(0);
897 int subn = get_u32 ();
898 printf ("nested %d bytes", subn);
903 match_byte_assert (0x58);
905 value = get_double ();
906 printf (" value %g format %d(%d.%d)", value, format >> 16, (format >> 8) & 0xff, format & 0xff);
908 else if (match_byte(3))
910 char *a = get_string();
911 match_byte_assert(0x58);
912 char *b = get_string();
913 char *c = get_string();
914 for (int k = 0; k <= 1; k++)
916 printf ("\"%s\", \"%s\", \"%s\"", a, b, c);
922 else if (match_byte(5))
924 match_byte_assert (0x58);
925 printf ("variable \"%s\"", get_string());
927 if (!match_byte(1) && !match_byte(2))
928 match_byte_assert(3);
942 match_byte_assert (0x58);
943 char *base = get_string();
945 printf ("\"%s\" with %d variables:\n", base, x);
946 for (int i = 0; i < x; i++)
953 for (int j = 0; j <= 0; j++)
955 printf("variable %d has %d values:\n", i, y);
956 for (int j = 0; j < y; j++)
960 char *a = get_string();
961 match_byte_assert(0x58);
962 char *b = get_string();
963 char *c = get_string();
964 for (int k = 0; k <= 1; k++)
966 printf ("\"%s\", \"%s\", \"%s\"", a, b, c);
973 else if (match_byte (1))
978 if (match_byte (0x31))
982 printf("(footnote %d) ", get_u32());
983 match_byte_assert (0);
984 match_byte_assert (0);
985 int subn = get_u32 ();
986 printf ("nested %d bytes", subn);
991 match_byte_assert (0x58);
993 value = get_double ();
994 printf ("value %g format %d(%d.%d)", value, format >> 16, (format >> 8) & 0xff, format & 0xff);
1007 dump_title_value(int level)
1009 for (int i = 0; i <= level; i++)
1020 if (match_byte (0x31))
1024 printf("(footnote %d) ", get_u32());
1025 match_byte_assert (0);
1026 match_byte_assert (0);
1027 int subn = get_u32 ();
1028 printf ("nested %d bytes", subn);
1031 else if (match_u32 (2))
1033 printf("(special 2)");
1034 match_byte_assert(0);
1035 match_byte_assert(0);
1037 match_u32_assert(1);
1038 match_byte_assert(0);
1039 match_byte_assert(0);
1040 int subn = get_u32 ();
1041 printf ("nested %d bytes", subn);
1046 match_u32_assert(3);
1047 printf("(special 3)");
1048 match_byte_assert(0);
1049 match_byte_assert(0);
1050 match_byte_assert(1);
1051 match_byte_assert(0);
1052 int subn = get_u32 ();
1053 printf ("nested %d bytes, ", subn);
1056 printf ("nested %d bytes, ", subn);
1061 match_byte_assert (0x58);
1063 printf("string \"%s\"", get_string());
1074 else if (match_byte (5))
1076 if (match_byte (0x31))
1080 printf("(footnote %d) ", get_u32());
1081 match_byte_assert (0);
1082 match_byte_assert (0);
1083 int subn = get_u32 ();
1084 printf ("nested %d bytes", subn);
1089 match_byte_assert (0x58);
1090 printf ("variable \"%s\"", get_string());
1092 if (!match_byte(1) && !match_byte(2))
1093 match_byte_assert(3);
1095 else if (match_byte (2))
1097 unsigned int format;
1101 match_byte_assert (0x58);
1102 format = get_u32 ();
1103 value = get_double ();
1104 var = get_string ();
1105 vallab = get_string ();
1106 printf ("value %g format %d(%d.%d) var \"%s\" vallab \"%s\"",
1107 value, format >> 16, (format >> 8) & 0xff, format & 0xff, var, vallab);
1108 if (!match_byte (1) && !match_byte(2))
1109 match_byte_assert (3);
1118 else if (match_byte (4))
1120 unsigned int format;
1121 char *var, *vallab, *value;
1123 match_byte_assert (0x58);
1124 format = get_u32 ();
1125 vallab = get_string ();
1126 var = get_string ();
1127 if (!match_byte(1) && !match_byte(2))
1128 match_byte_assert (3);
1129 value = get_string ();
1130 printf ("value \"%s\" format %d(%d.%d) var \"%s\" vallab \"%s\"",
1131 value, format >> 16, (format >> 8) & 0xff, format & 0xff, var, vallab);
1137 else if (match_byte (1))
1139 unsigned int format;
1142 if (match_byte (0x31))
1146 printf("(footnote %d) ", get_u32());
1147 match_byte_assert (0);
1148 match_byte_assert (0);
1149 int subn = get_u32 ();
1150 printf ("nested %d bytes", subn);
1155 match_byte_assert (0x58);
1156 format = get_u32 ();
1157 value = get_double ();
1158 printf ("value %g format %d(%d.%d)", value, format >> 16, (format >> 8) & 0xff, format & 0xff);
1165 else if (match_byte (0x31))
1169 printf("(footnote %d) ", get_u32());
1170 match_byte_assert (0);
1171 match_byte_assert (0);
1172 int subn = get_u32 ();
1173 printf ("nested %d bytes", subn);
1176 else if (match_u32 (0))
1178 match_u32_assert (0);
1179 int subn = get_u32 ();
1180 printf ("nested %d bytes", subn);
1185 match_u32_assert(3);
1186 printf("(special 3)");
1187 match_byte_assert(0);
1188 match_byte_assert(0);
1189 match_byte_assert(1);
1190 match_byte_assert(0);
1191 int subn = get_u32 ();
1192 printf ("nested %d bytes, ", subn);
1195 printf ("nested %d bytes, ", subn);
1199 char *base = get_string();
1201 printf ("\"%s\"; %d variables:\n", base, x);
1202 for (int i = 0; i < x; i++)
1208 match_u32_assert(0);
1209 for (int j = 0; j <= level; j++)
1211 printf("variable %d has %d values:\n", i, y);
1212 for (int j = 0; j < y; j++)
1216 char *a = get_string();
1217 match_byte_assert(0x58);
1218 char *b = get_string();
1219 char *c = get_string();
1220 for (int k = 0; k <= level + 1; k++)
1222 printf ("\"%s\", \"%s\", \"%s\"", a, b, c);
1226 dump_title_value (level+1);
1233 match_byte_assert (0x58);
1234 char *base = get_string();
1236 printf ("\"%s\" with %d variables:\n", base, x);
1237 for (int i = 0; i < x; i++)
1243 match_u32_assert(0);
1244 for (int j = 0; j <= level; j++)
1246 printf("variable %d has %d values:\n", i, y);
1247 for (int j = 0; j < y; j++)
1251 char *a = get_string();
1252 match_byte_assert(0x58);
1253 char *b = get_string();
1254 char *c = get_string();
1255 for (int k = 0; k <= level + 1; k++)
1257 printf ("\"%s\", \"%s\", \"%s\"", a, b, c);
1260 dump_title_value (level+1);
1271 dump_title_value(0); putchar('\n'); putchar('\n');
1272 dump_title_value(0); putchar('\n'); putchar('\n');
1273 match_byte_assert(0x31);
1274 dump_title_value(0); putchar('\n'); putchar('\n');
1276 match_byte_assert(0x58);
1277 if (match_byte(0x31))
1279 dump_title_value(0); putchar('\n');
1282 match_byte_assert(0x58);
1285 int n_footnotes = get_u32();
1286 fprintf(stderr, "%d footnotes\n", n_footnotes);
1287 printf("------\n%d footnotes\n", n_footnotes);
1288 if (n_footnotes < 10)
1290 for (int i = 0; i < n_footnotes; i++)
1292 printf("footnote %d:\n", i);
1294 if (match_byte (0x31))
1296 match_byte_assert(3);
1298 match_byte_assert(0x58);
1299 match_u32_assert(0);
1304 match_byte_assert (0x58);
1305 printf("(%d)\n", get_u32());
1311 main(int argc, char *argv[])
1316 if (isatty(STDIN_FILENO))
1318 fprintf(stderr, "redirect stdin from a .bin file\n");
1321 if (fstat(STDIN_FILENO, &s))
1333 if (read(STDIN_FILENO, data, n) != n)
1341 if (!strcmp(argv[1], "title0"))
1344 if (match_byte (0x03)
1345 || (match_byte (0x05) && match_byte (0x58)))
1346 printf ("%s\n", get_string());
1348 printf ("<unknown>\n");
1351 else if (!strcmp(argv[1], "title"))
1356 else if (!strcmp(argv[1], "titleraw"))
1358 const char fonts[] = "\x01\x31\x09\0\0\0SansSerif";
1360 n = find(fonts, sizeof fonts - 1);
1362 else if (!strcmp(argv[1], "fonts"))
1364 const char fonts[] = "\x01\x31\x09\0\0\0SansSerif";
1365 const char styles[] = "\xf0\0\0\0";
1366 start = find(fonts, sizeof fonts - 1);
1367 n = find(styles, sizeof styles - 1);
1369 else if (!strcmp(argv[1], "styles"))
1371 const char styles[] = "\xf0\0\0\0";
1372 const char dimensions[] = "-,,,.\0";
1373 start = find(styles, sizeof styles - 1);
1374 n = find(dimensions, sizeof dimensions - 1) + sizeof dimensions - 1;
1376 else if (!strcmp(argv[1], "dimensions") || !strcmp(argv[1], "all"))
1379 const char dimensions[] = "-,,,.\0";
1380 start = try_find_tail(dimensions, sizeof dimensions - 1);
1385 const char dimensions[] = "-,,, .\0";
1386 start = find_tail(dimensions, sizeof dimensions - 1);
1392 if (!strcmp(argv[1], "all"))
1398 fprintf (stderr, "unknown section %s\n", argv[1]);
1405 for (size_t i = start; i < n; )
1412 && i + 4 + data[i] + data[i + 1] * 256 <= n
1413 && all_ascii(&data[i + 4], data[i] + data[i + 1] * 256))
1415 fputs("\n\"", stdout);
1416 fwrite(&data[i + 4], 1, data[i] + data[i + 1] * 256, stdout);
1417 fputs("\" ", stdout);
1419 i += 4 + data[i] + data[i + 1] * 256;
1421 else if (i + 12 <= n
1422 && data[i + 1] == 40
1424 && data[i + 3] == 0)
1428 memcpy (&d, &data[i + 4], 8);
1429 printf ("F40.%d(%.*f)\n", data[i], data[i], d);
1432 else if (i + 12 <= n
1433 && data[i + 1] == 40
1434 && data[i + 2] == 31
1435 && data[i + 3] == 0)
1439 memcpy (&d, &data[i + 4], 8);
1440 printf ("PCT40.%d(%.*f)\n", data[i], data[i], d);
1444 && (data[i] && data[i] != 88 && data[i] != 0x41)
1449 printf ("i%d ", data[i]);
1454 printf("%02x ", data[i]);