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))
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();
302 printf ("\"%s\" with %d variables:\n", base, x);
305 for (int i = 0; i < x; i++)
307 dump_value (level+1);
313 for (int i = 0; i < x; i++)
317 for (int j = 0; j <= level; j++)
319 printf("variable %d has %d values:\n", i, y);
320 for (int j = 0; j < y; j++)
324 char *a = get_string();
325 match_byte_assert(0x58);
326 char *b = get_string();
327 char *c = get_string();
328 for (int k = 0; k <= level + 1; k++)
330 printf ("\"%s\", \"%s\", \"%s\"", a, b, c);
338 dump_value (level+1);
347 dump_dim_value(int level)
349 for (int i = 0; i <= level; i++)
357 printf("string \"%s\"", get_string());
361 else if (match_byte (5))
363 match_byte_assert (0x58);
364 printf ("variable \"%s\"", get_string());
367 match_byte_assert(3);
369 else if (match_byte (2))
375 match_byte_assert (0x58);
377 value = get_double ();
379 vallab = get_string ();
380 printf ("value %g format %d(%d.%d) var \"%s\" vallab \"%s\"",
381 value, format >> 16, (format >> 8) & 0xff, format & 0xff, var, vallab);
383 match_u32_assert (2);
386 else if (match_byte (1))
391 match_byte_assert (0x58);
393 value = get_double ();
394 printf ("value %g format %d(%d.%d)", value, format >> 16, (format >> 8) & 0xff, format & 0xff);
405 printf ("; \"%s\", substitutions:", get_string());
406 int total_subs = get_u32();
410 total_subs = (total_subs - 1) + x;
411 match_u32_assert (0);
413 printf (" (total %d)", total_subs);
415 for (int i = 0; i < total_subs; i++)
418 dump_value (level + 1);
424 dump_category(int level)
434 else if (match_u32 (1))
441 else if (match_byte (1))
445 match_u32_assert (1);
451 match_u32_assert (0);
455 int n_categories = get_u32();
456 if (n_categories > 0)
457 printf (", %d subcategories:", n_categories);
459 for (int i = 0; i < n_categories; i++)
460 dump_category (level + 1);
467 printf("next dim\n");
471 /* This byte is usually 0x02 but 0x00 and 0x75 (!) have also been spotted. */
474 if (!match_byte(0) && !match_byte(1))
475 match_byte_assert(2);
479 match_byte_assert(1);
489 n_categories = get_u32();
490 printf("%d nested categories\n", n_categories);
491 for (int i = 0; i < n_categories; i++)
500 printf ("%u dimensions\n", n_dims);
501 for (int i = 0; i < n_dims; i++)
509 dump_data_value(void)
522 value = get_double ();
523 printf (" value %g format %d(%d.%d)", value, format >> 16, (format >> 8) & 0xff, format & 0xff);
525 else if (match_byte (3))
530 printf("string \"%s\"", get_string());
533 else if (match_byte (2))
539 match_byte_assert (0x58);
541 value = get_double ();
543 vallab = get_string ();
544 printf ("value %g format %d(%d.%d) var \"%s\" vallab \"%s\"",
545 value, format >> 16, (format >> 8) & 0xff, format & 0xff, var, vallab);
546 if (!match_byte (1) && !match_byte(2))
547 match_byte_assert (3);
549 else if (match_byte (4))
552 char *var, *vallab, *value;
554 match_byte_assert (0x58);
556 vallab = get_string ();
558 if (!match_byte(1) && !match_byte(2))
559 match_byte_assert (3);
560 value = get_string ();
561 printf ("value \"%s\" format %d(%d.%d) var \"%s\" vallab \"%s\"",
562 value, format >> 16, (format >> 8) & 0xff, format & 0xff, var, vallab);
564 else if (match_byte (5))
566 match_byte_assert (0x58);
567 printf ("variable \"%s\"", get_string());
569 if (!match_byte(1) && !match_byte(2))
570 match_byte_assert(3);
579 char *base = get_string();
581 printf ("\"%s\"; %d variables:\n", base, x);
582 for (int i = 0; i < x; i++)
589 for (int j = 0; j <= 0; j++)
591 printf("variable %d has %d values:\n", i, y);
592 for (int j = 0; j < y; j++)
603 for (int i = 0; i < 3 + n_dims; i++)
605 printf ("data intro:");
606 for (int i = 0; i < 3 + n_dims; i++)
610 fprintf (stderr,"data intro (%d dims):", n_dims);
611 for (int i = 0; i < 3+n_dims; i++)
612 fprintf (stderr," %d", get_u32());
613 fprintf(stderr,"\n");
616 printf ("%d data values, starting at %08x\n", x, pos);
617 for (int i = 0; i < x; i++)
619 printf("%08x, index %d:\n", pos, get_u32());
627 dump_title_value(int level)
629 for (int i = 0; i <= level; i++)
641 printf("string \"%s\"", get_string());
649 else if (match_byte (5))
652 printf ("variable \"%s\"", get_string());
654 if (!match_byte(1) && !match_byte(2))
655 match_byte_assert(3);
657 else if (match_byte (2))
663 match_byte_assert (0x58);
665 value = get_double ();
667 vallab = get_string ();
668 printf ("value %g format %d(%d.%d) var \"%s\" vallab \"%s\"",
669 value, format >> 16, (format >> 8) & 0xff, format & 0xff, var, vallab);
670 if (!match_byte (1) && !match_byte(2))
671 match_byte_assert (3);
676 else if (match_byte (4))
679 char *var, *vallab, *value;
681 match_byte_assert (0x58);
683 vallab = get_string ();
685 if (!match_byte(1) && !match_byte(2))
686 match_byte_assert (3);
687 value = get_string ();
688 printf ("value \"%s\" format %d(%d.%d) var \"%s\" vallab \"%s\"",
689 value, format >> 16, (format >> 8) & 0xff, format & 0xff, var, vallab);
694 else if (match_byte (1))
701 value = get_double ();
702 printf ("value %g format %d(%d.%d)", value, format >> 16, (format >> 8) & 0xff, format & 0xff);
712 char *base = get_string();
714 printf ("\"%s\" with %d variables:\n", base, x);
715 for (int i = 0; i < x; i++)
722 for (int j = 0; j <= level; j++)
724 printf("variable %d has %d values:\n", i, y);
725 for (int j = 0; j < y; j++)
730 char *a = get_string();
731 match_byte_assert(0x58);
732 char *b = get_string();
733 char *c = get_string();
734 for (int k = 0; k <= level + 1; k++)
736 printf ("\"%s\", \"%s\", \"%s\"", a, b, c);
739 dump_title_value (level+1);
747 dump_footnote_value(int level)
749 for (int i = 0; i <= level; i++)
761 printf("string \"%s\"", get_string());
763 match_byte_assert (1);
765 else if (match_byte (5))
767 match_byte_assert (0x58);
768 printf ("variable \"%s\"", get_string());
770 if (!match_byte(1) && !match_byte(2))
771 match_byte_assert(3);
773 else if (match_byte (2))
779 match_byte_assert (0x58);
781 value = get_double ();
783 vallab = get_string ();
784 printf ("value %g format %d(%d.%d) var \"%s\" vallab \"%s\"",
785 value, format >> 16, (format >> 8) & 0xff, format & 0xff, var, vallab);
786 if (!match_byte (1) && !match_byte(2))
787 match_byte_assert (3);
796 else if (match_byte (4))
799 char *var, *vallab, *value;
801 match_byte_assert (0x58);
803 vallab = get_string ();
805 if (!match_byte(1) && !match_byte(2))
806 match_byte_assert (3);
807 value = get_string ();
808 printf ("value \"%s\" format %d(%d.%d) var \"%s\" vallab \"%s\"",
809 value, format >> 16, (format >> 8) & 0xff, format & 0xff, var, vallab);
815 else if (match_byte (1))
822 value = get_double ();
823 printf ("value %g format %d(%d.%d)", value, format >> 16, (format >> 8) & 0xff, format & 0xff);
828 char *base = get_string();
830 printf ("\"%s\"; %d variables:\n", base, x);
831 for (int i = 0; i < x; i++)
838 for (int j = 0; j <= level; j++)
840 printf("variable %d has %d values:\n", i, y);
841 for (int j = 0; j < y; j++)
845 char *a = get_string();
846 match_byte_assert(0x58);
847 char *b = get_string();
848 char *c = get_string();
849 for (int k = 0; k <= level + 1; k++)
851 printf ("\"%s\", \"%s\", \"%s\"", a, b, c);
853 match_byte_assert(0);
856 dump_footnote_value (level+1);
867 dump_title_value(0); putchar('\n');
868 dump_title_value(0); putchar('\n');
869 match_byte_assert(0x31);
870 dump_title_value(0); putchar('\n');
872 match_byte_assert(0x58);
873 if (match_byte(0x31))
875 dump_footnote_value(0); putchar('\n');
878 match_byte_assert(0x58);
881 int n_footnotes = get_u32();
882 if (n_footnotes >= 20)
884 fprintf(stderr, "%08x: %d footnotes\n", pos - 4, n_footnotes);
888 printf("------\n%d footnotes\n", n_footnotes);
889 if (n_footnotes < 20)
891 for (int i = 0; i < n_footnotes; i++)
893 printf("footnote %d:\n", i);
894 dump_footnote_value(0);
899 if (match_byte (0x31))
901 /* Custom footnote marker string. */
902 match_byte_assert(3);
904 match_byte_assert(0x58);
909 match_byte_assert (0x58);
910 printf("(%d)\n", get_u32());
916 find_dimensions(void)
919 const char dimensions[] = "-,,,.\0";
920 int x = try_find_tail(dimensions, sizeof dimensions - 1);
925 const char dimensions[] = "-,,, .\0";
926 return find_tail(dimensions, sizeof dimensions - 1);
932 printf("fonts: offset=%08x\n", pos);
934 for (int i = 1; i <= 8; i++)
936 printf("%08x: font %d, ", pos, i);
937 match_byte_assert(i);
938 match_byte_assert(0x31);
939 printf("%s, ", get_string());
940 match_byte_assert(0);
941 match_byte_assert(0);
942 if (!match_byte(0x40) && !match_byte(0x20) && !match_byte(0x80) && !match_byte(0x10))
943 match_byte_assert(0x50);
944 if (!match_byte(0x41))
945 match_byte_assert(0x51);
947 printf ("%s, ", get_string());
948 printf ("%s, ", get_string());
959 match_u32_assert(240);
962 match_u32_assert(18);
969 match_u32_assert(142);
973 int count = get_u32();
976 char *encoding = get_string();
977 printf("encoding=%s\n", encoding);
980 match_u32_assert(UINT32_MAX);
982 match_byte_assert(1);
983 match_byte_assert(0);
985 match_byte_assert(1);
986 if (!match_byte(0x99) && !match_byte(0x98))
987 match_byte_assert(0x97);
988 match_byte_assert(7);
989 match_byte_assert(0);
990 match_byte_assert(0);
992 match_byte_assert(',');
995 match_byte_assert(',');
996 if (!match_byte('.'))
997 match_byte_assert(' ');
1000 for (int i = 0; i < 5; i++)
1003 if (pos != find_dimensions())
1004 fprintf (stderr, "%08x / %08x\n", pos, find_dimensions());
1008 main(int argc, char *argv[])
1013 if (isatty(STDIN_FILENO))
1015 fprintf(stderr, "redirect stdin from a .bin file\n");
1018 if (fstat(STDIN_FILENO, &s))
1030 if (read(STDIN_FILENO, data, n) != n)
1038 if (!strcmp(argv[1], "title0"))
1041 if (match_byte (0x03)
1042 || (match_byte (0x05) && match_byte (0x58)))
1043 printf ("%s\n", get_string());
1045 printf ("<unknown>\n");
1048 else if (!strcmp(argv[1], "title"))
1053 else if (!strcmp(argv[1], "titleraw"))
1055 const char fonts[] = "\x01\x31\x09\0\0\0SansSerif";
1057 n = find(fonts, sizeof fonts - 1);
1059 else if (!strcmp(argv[1], "fonts"))
1061 const char fonts[] = "\x01\x31\x09\0\0\0SansSerif";
1062 const char styles[] = "\xf0\0\0\0";
1063 start = find(fonts, sizeof fonts - 1);
1064 n = find(styles, sizeof styles - 1);
1066 else if (!strcmp(argv[1], "styles"))
1068 const char styles[] = "\xf0\0\0\0";
1069 const char dimensions[] = "-,,,.\0";
1070 start = find(styles, sizeof styles - 1);
1071 n = find(dimensions, sizeof dimensions - 1) + sizeof dimensions - 1;
1073 else if (!strcmp(argv[1], "dimensions") || !strcmp(argv[1], "all"))
1076 match_byte_assert(1);
1077 match_byte_assert(0);
1078 match_u32_assert(3);
1079 match_byte_assert(1);
1081 match_byte_assert(1);
1082 match_byte_assert(0);
1083 match_byte_assert(0);
1085 match_byte_assert(1);
1087 match_byte_assert(0);
1088 match_byte_assert(0);
1089 match_byte_assert(0);
1093 printf("\n\ndata:\n");
1096 match_byte_assert (1);
1099 fprintf (stderr, "%x / %x\n", pos, n);
1106 fprintf (stderr, "unknown section %s\n", argv[1]);
1113 for (size_t i = start; i < n; )
1120 && i + 4 + data[i] + data[i + 1] * 256 <= n
1121 && all_ascii(&data[i + 4], data[i] + data[i + 1] * 256))
1123 fputs("\n\"", stdout);
1124 fwrite(&data[i + 4], 1, data[i] + data[i + 1] * 256, stdout);
1125 fputs("\" ", stdout);
1127 i += 4 + data[i] + data[i + 1] * 256;
1129 else if (i + 12 <= n
1130 && data[i + 1] == 40
1132 && data[i + 3] == 0)
1136 memcpy (&d, &data[i + 4], 8);
1137 printf ("F40.%d(%.*f)\n", data[i], data[i], d);
1140 else if (i + 12 <= n
1141 && data[i + 1] == 40
1142 && data[i + 2] == 31
1143 && data[i + 3] == 0)
1147 memcpy (&d, &data[i + 4], 8);
1148 printf ("PCT40.%d(%.*f)\n", data[i], data[i], d);
1152 && (data[i] && data[i] != 88 && data[i] != 0x41)
1157 printf ("i%d ", data[i]);
1162 printf("%02x ", data[i]);