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++)
1019 if (match_byte (0x31))
1023 printf("(footnote %d) ", get_u32());
1024 match_byte_assert (0);
1025 match_byte_assert (0);
1026 int subn = get_u32 ();
1027 printf ("nested %d bytes", subn);
1030 else if (match_u32 (2))
1032 printf("(special 2)");
1033 match_byte_assert(0);
1034 match_byte_assert(0);
1035 match_u32_assert(1);
1036 match_byte_assert(0);
1037 match_byte_assert(0);
1038 int subn = get_u32 ();
1039 printf ("nested %d bytes", subn);
1044 match_u32_assert(3);
1045 printf("(special 3)");
1046 match_byte_assert(0);
1047 match_byte_assert(0);
1048 match_byte_assert(1);
1049 match_byte_assert(0);
1050 int subn = get_u32 ();
1051 printf ("nested %d bytes, ", subn);
1054 printf ("nested %d bytes, ", subn);
1059 match_byte_assert (0x58);
1061 printf("string \"%s\"", get_string());
1072 else if (match_byte (5))
1074 match_byte_assert (0x58);
1075 printf ("variable \"%s\"", get_string());
1077 if (!match_byte(1) && !match_byte(2))
1078 match_byte_assert(3);
1084 else if (match_byte (2))
1086 unsigned int format;
1090 match_byte_assert (0x58);
1091 format = get_u32 ();
1092 value = get_double ();
1093 var = get_string ();
1094 vallab = get_string ();
1095 printf ("value %g format %d(%d.%d) var \"%s\" vallab \"%s\"",
1096 value, format >> 16, (format >> 8) & 0xff, format & 0xff, var, vallab);
1097 if (!match_byte (1) && !match_byte(2))
1098 match_byte_assert (3);
1107 else if (match_byte (4))
1109 unsigned int format;
1110 char *var, *vallab, *value;
1112 match_byte_assert (0x58);
1113 format = get_u32 ();
1114 vallab = get_string ();
1115 var = get_string ();
1116 if (!match_byte(1) && !match_byte(2))
1117 match_byte_assert (3);
1118 value = get_string ();
1119 printf ("value \"%s\" format %d(%d.%d) var \"%s\" vallab \"%s\"",
1120 value, format >> 16, (format >> 8) & 0xff, format & 0xff, var, vallab);
1126 else if (match_byte (1))
1128 unsigned int format;
1131 if (match_byte (0x31))
1135 printf("(footnote %d) ", get_u32());
1136 match_byte_assert (0);
1137 match_byte_assert (0);
1138 int subn = get_u32 ();
1139 printf ("nested %d bytes", subn);
1144 match_byte_assert (0x58);
1145 format = get_u32 ();
1146 value = get_double ();
1147 printf ("value %g format %d(%d.%d)", value, format >> 16, (format >> 8) & 0xff, format & 0xff);
1154 else if (match_byte (0x31))
1158 printf("(footnote %d) ", get_u32());
1159 match_byte_assert (0);
1160 match_byte_assert (0);
1161 int subn = get_u32 ();
1162 printf ("nested %d bytes", subn);
1165 else if (match_u32 (0))
1167 match_u32_assert (0);
1168 int subn = get_u32 ();
1169 printf ("nested %d bytes", subn);
1174 match_u32_assert(3);
1175 printf("(special 3)");
1176 match_byte_assert(0);
1177 match_byte_assert(0);
1178 match_byte_assert(1);
1179 match_byte_assert(0);
1180 int subn = get_u32 ();
1181 printf ("nested %d bytes, ", subn);
1184 printf ("nested %d bytes, ", subn);
1188 char *base = get_string();
1190 printf ("\"%s\"; %d variables:\n", base, x);
1193 for (int i = 0; i < x; i++)
1195 dump_title_value (level+1);
1201 for (int i = 0; i < x; i++)
1204 match_u32_assert(0);
1205 for (int j = 0; j <= level; j++)
1207 printf("variable %d has %d values:\n", i, y);
1208 for (int j = 0; j < y; j++)
1212 char *a = get_string();
1213 match_byte_assert(0x58);
1214 char *b = get_string();
1215 char *c = get_string();
1216 for (int k = 0; k <= level + 1; k++)
1218 printf ("\"%s\", \"%s\", \"%s\"", a, b, c);
1226 dump_title_value (level+1);
1234 match_byte_assert (0x58);
1235 char *base = get_string();
1237 printf ("\"%s\" with %d variables:\n", base, x);
1238 for (int i = 0; i < x; i++)
1244 match_u32_assert(0);
1245 for (int j = 0; j <= level; j++)
1247 printf("variable %d has %d values:\n", i, y);
1248 for (int j = 0; j < y; j++)
1252 char *a = get_string();
1253 match_byte_assert(0x58);
1254 char *b = get_string();
1255 char *c = get_string();
1256 for (int k = 0; k <= level + 1; k++)
1258 printf ("\"%s\", \"%s\", \"%s\"", a, b, c);
1261 dump_title_value (level+1);
1272 dump_title_value(0); putchar('\n');
1273 dump_title_value(0); putchar('\n');
1274 match_byte_assert(0x31);
1275 dump_title_value(0); putchar('\n');
1276 match_byte_assert(0x58);
1277 match_byte_assert(0x31);
1278 dump_title_value(0); putchar('\n');
1282 main(int argc, char *argv[])
1287 if (isatty(STDIN_FILENO))
1289 fprintf(stderr, "redirect stdin from a .bin file\n");
1292 if (fstat(STDIN_FILENO, &s))
1304 if (read(STDIN_FILENO, data, n) != n)
1312 if (!strcmp(argv[1], "title0"))
1315 if (match_byte (0x03)
1316 || (match_byte (0x05) && match_byte (0x58)))
1317 printf ("%s\n", get_string());
1319 printf ("<unknown>\n");
1322 else if (!strcmp(argv[1], "title"))
1327 else if (!strcmp(argv[1], "titleraw"))
1329 const char fonts[] = "\x01\x31\x09\0\0\0SansSerif";
1331 n = find(fonts, sizeof fonts - 1);
1333 else if (!strcmp(argv[1], "fonts"))
1335 const char fonts[] = "\x01\x31\x09\0\0\0SansSerif";
1336 const char styles[] = "\xf0\0\0\0";
1337 start = find(fonts, sizeof fonts - 1);
1338 n = find(styles, sizeof styles - 1);
1340 else if (!strcmp(argv[1], "styles"))
1342 const char styles[] = "\xf0\0\0\0";
1343 const char dimensions[] = "-,,,.\0";
1344 start = find(styles, sizeof styles - 1);
1345 n = find(dimensions, sizeof dimensions - 1) + sizeof dimensions - 1;
1347 else if (!strcmp(argv[1], "dimensions") || !strcmp(argv[1], "all"))
1350 const char dimensions[] = "-,,,.\0";
1351 start = try_find_tail(dimensions, sizeof dimensions - 1);
1356 const char dimensions[] = "-,,, .\0";
1357 start = find_tail(dimensions, sizeof dimensions - 1);
1363 if (!strcmp(argv[1], "all"))
1369 fprintf (stderr, "unknown section %s\n", argv[1]);
1376 for (size_t i = start; i < n; )
1383 && i + 4 + data[i] + data[i + 1] * 256 <= n
1384 && all_ascii(&data[i + 4], data[i] + data[i + 1] * 256))
1386 fputs("\n\"", stdout);
1387 fwrite(&data[i + 4], 1, data[i] + data[i + 1] * 256, stdout);
1388 fputs("\" ", stdout);
1390 i += 4 + data[i] + data[i + 1] * 256;
1392 else if (i + 12 <= n
1393 && data[i + 1] == 40
1395 && data[i + 3] == 0)
1399 memcpy (&d, &data[i + 4], 8);
1400 printf ("F40.%d(%.*f)\n", data[i], data[i], d);
1403 else if (i + 12 <= n
1404 && data[i + 1] == 40
1405 && data[i + 2] == 31
1406 && data[i + 3] == 0)
1410 memcpy (&d, &data[i + 4], 8);
1411 printf ("PCT40.%d(%.*f)\n", data[i], data[i], d);
1415 && (data[i] && data[i] != 88 && data[i] != 0x41)
1420 printf ("i%d ", data[i]);
1425 printf("%02x ", data[i]);