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);
207 static void dump_value(int level);
210 dump_value__(int level)
214 char *s1 = get_string();
216 char *s2 = get_string();
217 char *s3 = get_string();
219 printf("strings \"%s\", \"%s\" and \"%s\"", s1, s2, s3);
221 printf("string \"%s\" and \"%s\"", s1, s2);
223 match_byte_assert(1);
226 else if (match_byte (5))
229 printf ("variable \"%s\"", get_string());
231 if (!match_byte(1) && !match_byte(2))
232 match_byte_assert(3);
234 else if (match_byte (2))
240 match_byte_assert (0x58);
242 value = get_double ();
244 vallab = get_string ();
245 printf ("value %g format %d(%d.%d) var \"%s\" vallab \"%s\"",
246 value, format >> 16, (format >> 8) & 0xff, format & 0xff, var, vallab);
247 if (!match_byte (1) && !match_byte(2))
248 match_byte_assert (3);
250 else if (match_byte (4))
253 char *var, *vallab, *value;
255 match_byte_assert (0x58);
257 vallab = get_string ();
259 if (!match_byte(1) && !match_byte(2))
260 match_byte_assert (3);
261 value = get_string ();
262 printf ("value \"%s\" format %d(%d.%d) var \"%s\" vallab \"%s\"",
263 value, format >> 16, (format >> 8) & 0xff, format & 0xff, var, vallab);
265 else if (match_byte (1))
272 value = get_double ();
273 printf ("value %g format %d(%d.%d)", value, format >> 16, (format >> 8) & 0xff, format & 0xff);
279 char *base = get_string();
282 printf ("\"%s\" with %d variables:\n", base, x);
285 for (int i = 0; i < x; i++)
287 dump_value (level+1);
293 for (int i = 0; i < x; i++)
297 for (int j = 0; j <= level; j++)
299 printf("variable %d has %d values:\n", i, y);
300 for (int j = 0; j < y; j++)
305 char *a = get_string();
306 match_byte_assert(0x58);
307 char *b = get_string();
308 char *c = get_string();
309 for (int k = 0; k <= level + 1; k++)
311 printf ("\"%s\", \"%s\", \"%s\"", a, b, c);
314 dump_value (level+1);
328 dump_value(int level)
330 for (int i = 0; i <= level; i++)
344 dump_dim_value(int level)
346 for (int i = 0; i <= level; i++)
349 if (data[pos] == 3 || data[pos] == 5)
356 dump_category(int level)
366 else if (match_u32 (1))
373 else if (match_byte (1))
377 match_u32_assert (1);
383 match_u32_assert (0);
387 int n_categories = get_u32();
388 if (n_categories > 0)
389 printf (", %d subcategories:", n_categories);
391 for (int i = 0; i < n_categories; i++)
392 dump_category (level + 1);
399 printf("next dim\n");
403 /* This byte is usually 0x02 but 0x00 and 0x75 (!) have also been spotted. */
406 if (!match_byte(0) && !match_byte(1))
407 match_byte_assert(2);
411 match_byte_assert(1);
421 n_categories = get_u32();
422 printf("%d nested categories\n", n_categories);
423 for (int i = 0; i < n_categories; i++)
432 printf ("%u dimensions\n", n_dims);
433 for (int i = 0; i < n_dims; i++)
441 dump_data_value(void)
454 value = get_double ();
455 printf (" value %g format %d(%d.%d)", value, format >> 16, (format >> 8) & 0xff, format & 0xff);
457 else if (match_byte (3))
462 printf("string \"%s\"", get_string());
464 match_byte_assert(1);
466 else if (data[pos] == 2 || data[pos] == 4)
468 else if (data[pos] == 5)
473 char *base = get_string();
475 printf ("\"%s\"; %d variables:\n", base, x);
476 for (int i = 0; i < x; i++)
483 for (int j = 0; j <= 0; j++)
485 printf("variable %d has %d values:\n", i, y);
486 for (int j = 0; j < y; j++)
497 for (int i = 0; i < 3 + n_dims; i++)
499 printf ("data intro:");
500 for (int i = 0; i < 3 + n_dims; i++)
504 fprintf (stderr,"data intro (%d dims):", n_dims);
505 for (int i = 0; i < 3+n_dims; i++)
506 fprintf (stderr," %d", get_u32());
507 fprintf(stderr,"\n");
510 printf ("%d data values, starting at %08x\n", x, pos);
511 for (int i = 0; i < x; i++)
513 printf("%08x, index %d:\n", pos, get_u32());
521 dump_title_value(int level)
523 for (int i = 0; i <= level; i++)
530 if (data[pos] == 3 || data[pos] == 2 || data[pos] == 4 || data[pos] == 1)
532 else if (data[pos] == 5)
538 char *base = get_string();
540 printf ("\"%s\" with %d variables:\n", base, x);
541 for (int i = 0; i < x; i++)
548 for (int j = 0; j <= level; j++)
550 printf("variable %d has %d values:\n", i, y);
551 for (int j = 0; j < y; j++)
553 dump_title_value (level+1);
561 dump_footnote_value(int level)
563 for (int i = 0; i <= level; i++)
570 if (data[pos] == 2 || data[pos] == 4)
572 else if (data[pos] == 5)
574 else if (match_byte (3))
579 printf("string \"%s\"", get_string());
581 match_byte_assert (1);
583 else if (match_byte (1))
590 value = get_double ();
591 printf ("value %g format %d(%d.%d)", value, format >> 16, (format >> 8) & 0xff, format & 0xff);
596 char *base = get_string();
598 printf ("\"%s\"; %d variables:\n", base, x);
599 for (int i = 0; i < x; i++)
606 for (int j = 0; j <= level; j++)
608 printf("variable %d has %d values:\n", i, y);
609 for (int j = 0; j < y; j++)
611 dump_footnote_value (level+1);
622 dump_title_value(0); putchar('\n');
623 dump_title_value(0); putchar('\n');
624 match_byte_assert(0x31);
625 dump_title_value(0); putchar('\n');
627 match_byte_assert(0x58);
628 if (match_byte(0x31))
630 dump_footnote_value(0); putchar('\n');
633 match_byte_assert(0x58);
636 int n_footnotes = get_u32();
637 if (n_footnotes >= 20)
639 fprintf(stderr, "%08x: %d footnotes\n", pos - 4, n_footnotes);
643 printf("------\n%d footnotes\n", n_footnotes);
644 if (n_footnotes < 20)
646 for (int i = 0; i < n_footnotes; i++)
648 printf("footnote %d:\n", i);
649 dump_footnote_value(0);
654 if (match_byte (0x31))
656 /* Custom footnote marker string. */
657 match_byte_assert(3);
659 match_byte_assert(0x58);
664 match_byte_assert (0x58);
665 printf("(%d)\n", get_u32());
671 find_dimensions(void)
674 const char dimensions[] = "-,,,.\0";
675 int x = try_find_tail(dimensions, sizeof dimensions - 1);
680 const char dimensions[] = "-,,, .\0";
681 return find_tail(dimensions, sizeof dimensions - 1);
687 printf("fonts: offset=%08x\n", pos);
689 for (int i = 1; i <= 8; i++)
691 printf("%08x: font %d, ", pos, i);
692 match_byte_assert(i);
693 match_byte_assert(0x31);
694 printf("%s, ", get_string());
695 match_byte_assert(0);
696 match_byte_assert(0);
697 if (!match_byte(0x40) && !match_byte(0x20) && !match_byte(0x80) && !match_byte(0x10))
698 match_byte_assert(0x50);
699 if (!match_byte(0x41))
700 match_byte_assert(0x51);
702 printf ("%s, ", get_string());
703 printf ("%s, ", get_string());
714 match_u32_assert(240);
717 match_u32_assert(18);
724 match_u32_assert(142);
728 int count = get_u32();
731 char *encoding = get_string();
732 printf("encoding=%s\n", encoding);
735 match_u32_assert(UINT32_MAX);
737 match_byte_assert(1);
738 match_byte_assert(0);
740 match_byte_assert(1);
741 if (!match_byte(0x99) && !match_byte(0x98))
742 match_byte_assert(0x97);
743 match_byte_assert(7);
744 match_byte_assert(0);
745 match_byte_assert(0);
747 match_byte_assert(',');
750 match_byte_assert(',');
751 if (!match_byte('.'))
752 match_byte_assert(' ');
755 for (int i = 0; i < 5; i++)
758 if (pos != find_dimensions())
759 fprintf (stderr, "%08x / %08x\n", pos, find_dimensions());
763 main(int argc, char *argv[])
768 if (isatty(STDIN_FILENO))
770 fprintf(stderr, "redirect stdin from a .bin file\n");
773 if (fstat(STDIN_FILENO, &s))
785 if (read(STDIN_FILENO, data, n) != n)
793 if (!strcmp(argv[1], "title0"))
796 if (match_byte (0x03)
797 || (match_byte (0x05) && match_byte (0x58)))
798 printf ("%s\n", get_string());
800 printf ("<unknown>\n");
803 else if (!strcmp(argv[1], "title"))
808 else if (!strcmp(argv[1], "titleraw"))
810 const char fonts[] = "\x01\x31\x09\0\0\0SansSerif";
812 n = find(fonts, sizeof fonts - 1);
814 else if (!strcmp(argv[1], "fonts"))
816 const char fonts[] = "\x01\x31\x09\0\0\0SansSerif";
817 const char styles[] = "\xf0\0\0\0";
818 start = find(fonts, sizeof fonts - 1);
819 n = find(styles, sizeof styles - 1);
821 else if (!strcmp(argv[1], "styles"))
823 const char styles[] = "\xf0\0\0\0";
824 const char dimensions[] = "-,,,.\0";
825 start = find(styles, sizeof styles - 1);
826 n = find(dimensions, sizeof dimensions - 1) + sizeof dimensions - 1;
828 else if (!strcmp(argv[1], "dimensions") || !strcmp(argv[1], "all"))
831 match_byte_assert(1);
832 match_byte_assert(0);
834 match_byte_assert(1);
836 match_byte_assert(1);
837 match_byte_assert(0);
838 match_byte_assert(0);
840 match_byte_assert(1);
842 match_byte_assert(0);
843 match_byte_assert(0);
844 match_byte_assert(0);
848 printf("\n\ndata:\n");
851 match_byte_assert (1);
854 fprintf (stderr, "%x / %x\n", pos, n);
861 fprintf (stderr, "unknown section %s\n", argv[1]);
868 for (size_t i = start; i < n; )
875 && i + 4 + data[i] + data[i + 1] * 256 <= n
876 && all_ascii(&data[i + 4], data[i] + data[i + 1] * 256))
878 fputs("\n\"", stdout);
879 fwrite(&data[i + 4], 1, data[i] + data[i + 1] * 256, stdout);
880 fputs("\" ", stdout);
882 i += 4 + data[i] + data[i + 1] * 256;
891 memcpy (&d, &data[i + 4], 8);
892 printf ("F40.%d(%.*f)\n", data[i], data[i], d);
902 memcpy (&d, &data[i + 4], 8);
903 printf ("PCT40.%d(%.*f)\n", data[i], data[i], d);
907 && (data[i] && data[i] != 88 && data[i] != 0x41)
912 printf ("i%d ", data[i]);
917 printf("%02x ", data[i]);