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++)
325 char *a = get_string();
326 match_byte_assert(0x58);
327 char *b = get_string();
328 char *c = get_string();
329 for (int k = 0; k <= level + 1; k++)
331 printf ("\"%s\", \"%s\", \"%s\"", a, b, c);
334 dump_value (level+1);
348 dump_dim_value(int level)
350 for (int i = 0; i <= level; i++)
358 printf("string \"%s\"", get_string());
362 else if (match_byte (5))
364 match_byte_assert (0x58);
365 printf ("variable \"%s\"", get_string());
367 if (!match_byte (1) && !match_byte (2))
368 match_byte_assert(3);
370 else if (match_byte (2))
376 match_byte_assert (0x58);
378 value = get_double ();
380 vallab = get_string ();
381 printf ("value %g format %d(%d.%d) var \"%s\" vallab \"%s\"",
382 value, format >> 16, (format >> 8) & 0xff, format & 0xff, var, vallab);
383 if (!match_byte (1) && !match_byte (2))
384 match_byte_assert (3);
389 else if (match_byte (1))
396 value = get_double ();
397 printf ("value %g format %d(%d.%d)", value, format >> 16, (format >> 8) & 0xff, format & 0xff);
406 printf ("; \"%s\", substitutions:", get_string());
407 int total_subs = get_u32();
411 total_subs = (total_subs - 1) + x;
412 match_u32_assert (0);
414 printf (" (total %d)", total_subs);
416 for (int i = 0; i < total_subs; i++)
419 dump_value (level + 1);
425 dump_category(int level)
435 else if (match_u32 (1))
442 else if (match_byte (1))
446 match_u32_assert (1);
452 match_u32_assert (0);
456 int n_categories = get_u32();
457 if (n_categories > 0)
458 printf (", %d subcategories:", n_categories);
460 for (int i = 0; i < n_categories; i++)
461 dump_category (level + 1);
468 printf("next dim\n");
472 /* This byte is usually 0x02 but 0x00 and 0x75 (!) have also been spotted. */
475 if (!match_byte(0) && !match_byte(1))
476 match_byte_assert(2);
480 match_byte_assert(1);
490 n_categories = get_u32();
491 printf("%d nested categories\n", n_categories);
492 for (int i = 0; i < n_categories; i++)
501 printf ("%u dimensions\n", n_dims);
502 for (int i = 0; i < n_dims; i++)
510 dump_data_value(void)
523 value = get_double ();
524 printf (" value %g format %d(%d.%d)", value, format >> 16, (format >> 8) & 0xff, format & 0xff);
526 else if (match_byte (3))
531 printf("string \"%s\"", get_string());
534 else if (match_byte (2))
540 match_byte_assert (0x58);
542 value = get_double ();
544 vallab = get_string ();
545 printf ("value %g format %d(%d.%d) var \"%s\" vallab \"%s\"",
546 value, format >> 16, (format >> 8) & 0xff, format & 0xff, var, vallab);
547 if (!match_byte (1) && !match_byte(2))
548 match_byte_assert (3);
550 else if (match_byte (4))
553 char *var, *vallab, *value;
555 match_byte_assert (0x58);
557 vallab = get_string ();
559 if (!match_byte(1) && !match_byte(2))
560 match_byte_assert (3);
561 value = get_string ();
562 printf ("value \"%s\" format %d(%d.%d) var \"%s\" vallab \"%s\"",
563 value, format >> 16, (format >> 8) & 0xff, format & 0xff, var, vallab);
565 else if (match_byte (5))
567 match_byte_assert (0x58);
568 printf ("variable \"%s\"", get_string());
570 if (!match_byte(1) && !match_byte(2))
571 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);
792 else if (match_byte (4))
795 char *var, *vallab, *value;
797 match_byte_assert (0x58);
799 vallab = get_string ();
801 if (!match_byte(1) && !match_byte(2))
802 match_byte_assert (3);
803 value = get_string ();
804 printf ("value \"%s\" format %d(%d.%d) var \"%s\" vallab \"%s\"",
805 value, format >> 16, (format >> 8) & 0xff, format & 0xff, var, vallab);
810 else if (match_byte (1))
817 value = get_double ();
818 printf ("value %g format %d(%d.%d)", value, format >> 16, (format >> 8) & 0xff, format & 0xff);
823 char *base = get_string();
825 printf ("\"%s\"; %d variables:\n", base, x);
826 for (int i = 0; i < x; i++)
833 for (int j = 0; j <= level; j++)
835 printf("variable %d has %d values:\n", i, y);
836 for (int j = 0; j < y; j++)
838 dump_footnote_value (level+1);
849 dump_title_value(0); putchar('\n');
850 dump_title_value(0); putchar('\n');
851 match_byte_assert(0x31);
852 dump_title_value(0); putchar('\n');
854 match_byte_assert(0x58);
855 if (match_byte(0x31))
857 dump_footnote_value(0); putchar('\n');
860 match_byte_assert(0x58);
863 int n_footnotes = get_u32();
864 if (n_footnotes >= 20)
866 fprintf(stderr, "%08x: %d footnotes\n", pos - 4, n_footnotes);
870 printf("------\n%d footnotes\n", n_footnotes);
871 if (n_footnotes < 20)
873 for (int i = 0; i < n_footnotes; i++)
875 printf("footnote %d:\n", i);
876 dump_footnote_value(0);
881 if (match_byte (0x31))
883 /* Custom footnote marker string. */
884 match_byte_assert(3);
886 match_byte_assert(0x58);
891 match_byte_assert (0x58);
892 printf("(%d)\n", get_u32());
898 find_dimensions(void)
901 const char dimensions[] = "-,,,.\0";
902 int x = try_find_tail(dimensions, sizeof dimensions - 1);
907 const char dimensions[] = "-,,, .\0";
908 return find_tail(dimensions, sizeof dimensions - 1);
914 printf("fonts: offset=%08x\n", pos);
916 for (int i = 1; i <= 8; i++)
918 printf("%08x: font %d, ", pos, i);
919 match_byte_assert(i);
920 match_byte_assert(0x31);
921 printf("%s, ", get_string());
922 match_byte_assert(0);
923 match_byte_assert(0);
924 if (!match_byte(0x40) && !match_byte(0x20) && !match_byte(0x80) && !match_byte(0x10))
925 match_byte_assert(0x50);
926 if (!match_byte(0x41))
927 match_byte_assert(0x51);
929 printf ("%s, ", get_string());
930 printf ("%s, ", get_string());
941 match_u32_assert(240);
944 match_u32_assert(18);
951 match_u32_assert(142);
955 int count = get_u32();
958 char *encoding = get_string();
959 printf("encoding=%s\n", encoding);
962 match_u32_assert(UINT32_MAX);
964 match_byte_assert(1);
965 match_byte_assert(0);
967 match_byte_assert(1);
968 if (!match_byte(0x99) && !match_byte(0x98))
969 match_byte_assert(0x97);
970 match_byte_assert(7);
971 match_byte_assert(0);
972 match_byte_assert(0);
974 match_byte_assert(',');
977 match_byte_assert(',');
978 if (!match_byte('.'))
979 match_byte_assert(' ');
982 for (int i = 0; i < 5; i++)
985 if (pos != find_dimensions())
986 fprintf (stderr, "%08x / %08x\n", pos, find_dimensions());
990 main(int argc, char *argv[])
995 if (isatty(STDIN_FILENO))
997 fprintf(stderr, "redirect stdin from a .bin file\n");
1000 if (fstat(STDIN_FILENO, &s))
1012 if (read(STDIN_FILENO, data, n) != n)
1020 if (!strcmp(argv[1], "title0"))
1023 if (match_byte (0x03)
1024 || (match_byte (0x05) && match_byte (0x58)))
1025 printf ("%s\n", get_string());
1027 printf ("<unknown>\n");
1030 else if (!strcmp(argv[1], "title"))
1035 else if (!strcmp(argv[1], "titleraw"))
1037 const char fonts[] = "\x01\x31\x09\0\0\0SansSerif";
1039 n = find(fonts, sizeof fonts - 1);
1041 else if (!strcmp(argv[1], "fonts"))
1043 const char fonts[] = "\x01\x31\x09\0\0\0SansSerif";
1044 const char styles[] = "\xf0\0\0\0";
1045 start = find(fonts, sizeof fonts - 1);
1046 n = find(styles, sizeof styles - 1);
1048 else if (!strcmp(argv[1], "styles"))
1050 const char styles[] = "\xf0\0\0\0";
1051 const char dimensions[] = "-,,,.\0";
1052 start = find(styles, sizeof styles - 1);
1053 n = find(dimensions, sizeof dimensions - 1) + sizeof dimensions - 1;
1055 else if (!strcmp(argv[1], "dimensions") || !strcmp(argv[1], "all"))
1058 match_byte_assert(1);
1059 match_byte_assert(0);
1060 match_u32_assert(3);
1061 match_byte_assert(1);
1063 match_byte_assert(1);
1064 match_byte_assert(0);
1065 match_byte_assert(0);
1067 match_byte_assert(1);
1069 match_byte_assert(0);
1070 match_byte_assert(0);
1071 match_byte_assert(0);
1075 printf("\n\ndata:\n");
1078 match_byte_assert (1);
1081 fprintf (stderr, "%x / %x\n", pos, n);
1088 fprintf (stderr, "unknown section %s\n", argv[1]);
1095 for (size_t i = start; i < n; )
1102 && i + 4 + data[i] + data[i + 1] * 256 <= n
1103 && all_ascii(&data[i + 4], data[i] + data[i + 1] * 256))
1105 fputs("\n\"", stdout);
1106 fwrite(&data[i + 4], 1, data[i] + data[i + 1] * 256, stdout);
1107 fputs("\" ", stdout);
1109 i += 4 + data[i] + data[i + 1] * 256;
1111 else if (i + 12 <= n
1112 && data[i + 1] == 40
1114 && data[i + 3] == 0)
1118 memcpy (&d, &data[i + 4], 8);
1119 printf ("F40.%d(%.*f)\n", data[i], data[i], d);
1122 else if (i + 12 <= n
1123 && data[i + 1] == 40
1124 && data[i + 2] == 31
1125 && data[i + 3] == 0)
1129 memcpy (&d, &data[i + 4], 8);
1130 printf ("PCT40.%d(%.*f)\n", data[i], data[i], d);
1134 && (data[i] && data[i] != 88 && data[i] != 0x41)
1139 printf ("i%d ", data[i]);
1144 printf("%02x ", data[i]);