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)
362 else if (match_byte (1))
366 match_u32_assert (1);
369 else if (!match_u32(2))
370 match_u32_assert (0);
374 int n_categories = get_u32();
375 if (n_categories > 0)
376 printf (", %d subcategories:", n_categories);
378 for (int i = 0; i < n_categories; i++)
379 dump_category (level + 1);
386 printf("next dim\n");
390 /* This byte is usually 0x02 but 0x00 and 0x75 (!) have also been spotted. */
393 if (!match_byte(0) && !match_byte(1))
394 match_byte_assert(2);
398 match_byte_assert(1);
408 n_categories = get_u32();
409 printf("%d nested categories\n", n_categories);
410 for (int i = 0; i < n_categories; i++)
419 printf ("%u dimensions\n", n_dims);
420 for (int i = 0; i < n_dims; i++)
428 dump_data_value(void)
441 value = get_double ();
442 printf (" value %g format %d(%d.%d)", value, format >> 16, (format >> 8) & 0xff, format & 0xff);
444 else if (match_byte (3))
449 printf("string \"%s\"", get_string());
451 match_byte_assert(1);
453 else if (data[pos] == 2 || data[pos] == 4)
455 else if (data[pos] == 5)
460 char *base = get_string();
462 printf ("\"%s\"; %d variables:\n", base, x);
463 for (int i = 0; i < x; i++)
470 for (int j = 0; j <= 0; j++)
472 printf("variable %d has %d values:\n", i, y);
473 for (int j = 0; j < y; j++)
484 for (int i = 0; i < 3 + n_dims; i++)
486 printf ("data intro:");
487 for (int i = 0; i < 3 + n_dims; i++)
491 fprintf (stderr,"data intro (%d dims):", n_dims);
492 for (int i = 0; i < 3+n_dims; i++)
493 fprintf (stderr," %d", get_u32());
494 fprintf(stderr,"\n");
497 printf ("%d data values, starting at %08x\n", x, pos);
498 for (int i = 0; i < x; i++)
500 printf("%08x, index %d:\n", pos, get_u32());
508 dump_title_value(int level)
510 for (int i = 0; i <= level; i++)
517 if (data[pos] == 3 || data[pos] == 2 || data[pos] == 4 || data[pos] == 1)
519 else if (data[pos] == 5)
525 char *base = get_string();
527 printf ("\"%s\" with %d variables:\n", base, x);
528 for (int i = 0; i < x; i++)
535 for (int j = 0; j <= level; j++)
537 printf("variable %d has %d values:\n", i, y);
538 for (int j = 0; j < y; j++)
540 dump_title_value (level+1);
548 dump_footnote_value(int level)
550 for (int i = 0; i <= level; i++)
557 if (data[pos] == 2 || data[pos] == 4)
559 else if (data[pos] == 5)
561 else if (match_byte (3))
566 printf("string \"%s\"", get_string());
568 match_byte_assert (1);
570 else if (match_byte (1))
577 value = get_double ();
578 printf ("value %g format %d(%d.%d)", value, format >> 16, (format >> 8) & 0xff, format & 0xff);
583 char *base = get_string();
585 printf ("\"%s\"; %d variables:\n", base, x);
586 for (int i = 0; i < x; i++)
593 for (int j = 0; j <= level; j++)
595 printf("variable %d has %d values:\n", i, y);
596 for (int j = 0; j < y; j++)
598 dump_footnote_value (level+1);
609 dump_title_value(0); putchar('\n');
610 dump_title_value(0); putchar('\n');
611 match_byte_assert(0x31);
612 dump_title_value(0); putchar('\n');
614 match_byte_assert(0x58);
615 if (match_byte(0x31))
617 dump_footnote_value(0); putchar('\n');
620 match_byte_assert(0x58);
623 int n_footnotes = get_u32();
624 if (n_footnotes >= 20)
626 fprintf(stderr, "%08x: %d footnotes\n", pos - 4, n_footnotes);
630 printf("------\n%d footnotes\n", n_footnotes);
631 if (n_footnotes < 20)
633 for (int i = 0; i < n_footnotes; i++)
635 printf("footnote %d:\n", i);
636 dump_footnote_value(0);
641 if (match_byte (0x31))
643 /* Custom footnote marker string. */
644 match_byte_assert(3);
646 match_byte_assert(0x58);
651 match_byte_assert (0x58);
652 printf("(%d)\n", get_u32());
658 find_dimensions(void)
661 const char dimensions[] = "-,,,.\0";
662 int x = try_find_tail(dimensions, sizeof dimensions - 1);
667 const char dimensions[] = "-,,, .\0";
668 return find_tail(dimensions, sizeof dimensions - 1);
674 printf("fonts: offset=%08x\n", pos);
676 for (int i = 1; i <= 8; i++)
678 printf("%08x: font %d, ", pos, i);
679 match_byte_assert(i);
680 match_byte_assert(0x31);
681 printf("%s, ", get_string());
682 match_byte_assert(0);
683 match_byte_assert(0);
684 if (!match_byte(0x40) && !match_byte(0x20) && !match_byte(0x80) && !match_byte(0x10))
685 match_byte_assert(0x50);
686 if (!match_byte(0x41))
687 match_byte_assert(0x51);
689 printf ("%s, ", get_string());
690 printf ("%s, ", get_string());
701 match_u32_assert(240);
704 match_u32_assert(18);
711 match_u32_assert(142);
715 int count = get_u32();
718 char *encoding = get_string();
719 printf("encoding=%s\n", encoding);
722 match_u32_assert(UINT32_MAX);
724 match_byte_assert(1);
725 match_byte_assert(0);
727 match_byte_assert(1);
728 if (!match_byte(0x99) && !match_byte(0x98))
729 match_byte_assert(0x97);
730 match_byte_assert(7);
731 match_byte_assert(0);
732 match_byte_assert(0);
734 match_byte_assert(',');
737 match_byte_assert(',');
738 if (!match_byte('.'))
739 match_byte_assert(' ');
742 for (int i = 0; i < 5; i++)
745 if (pos != find_dimensions())
746 fprintf (stderr, "%08x / %08x\n", pos, find_dimensions());
750 main(int argc, char *argv[])
755 if (isatty(STDIN_FILENO))
757 fprintf(stderr, "redirect stdin from a .bin file\n");
760 if (fstat(STDIN_FILENO, &s))
772 if (read(STDIN_FILENO, data, n) != n)
780 if (!strcmp(argv[1], "title0"))
783 if (match_byte (0x03)
784 || (match_byte (0x05) && match_byte (0x58)))
785 printf ("%s\n", get_string());
787 printf ("<unknown>\n");
790 else if (!strcmp(argv[1], "title"))
795 else if (!strcmp(argv[1], "titleraw"))
797 const char fonts[] = "\x01\x31\x09\0\0\0SansSerif";
799 n = find(fonts, sizeof fonts - 1);
801 else if (!strcmp(argv[1], "fonts"))
803 const char fonts[] = "\x01\x31\x09\0\0\0SansSerif";
804 const char styles[] = "\xf0\0\0\0";
805 start = find(fonts, sizeof fonts - 1);
806 n = find(styles, sizeof styles - 1);
808 else if (!strcmp(argv[1], "styles"))
810 const char styles[] = "\xf0\0\0\0";
811 const char dimensions[] = "-,,,.\0";
812 start = find(styles, sizeof styles - 1);
813 n = find(dimensions, sizeof dimensions - 1) + sizeof dimensions - 1;
815 else if (!strcmp(argv[1], "dimensions") || !strcmp(argv[1], "all"))
818 match_byte_assert(1);
819 match_byte_assert(0);
821 match_byte_assert(1);
823 match_byte_assert(1);
824 match_byte_assert(0);
825 match_byte_assert(0);
827 match_byte_assert(1);
829 match_byte_assert(0);
830 match_byte_assert(0);
831 match_byte_assert(0);
835 printf("\n\ndata:\n");
838 match_byte_assert (1);
841 fprintf (stderr, "%x / %x\n", pos, n);
848 fprintf (stderr, "unknown section %s\n", argv[1]);
855 for (size_t i = start; i < n; )
862 && i + 4 + data[i] + data[i + 1] * 256 <= n
863 && all_ascii(&data[i + 4], data[i] + data[i + 1] * 256))
865 fputs("\n\"", stdout);
866 fwrite(&data[i + 4], 1, data[i] + data[i + 1] * 256, stdout);
867 fputs("\" ", stdout);
869 i += 4 + data[i] + data[i + 1] * 256;
878 memcpy (&d, &data[i + 4], 8);
879 printf ("F40.%d(%.*f)\n", data[i], data[i], d);
889 memcpy (&d, &data[i + 4], 8);
890 printf ("PCT40.%d(%.*f)\n", data[i], data[i], d);
894 && (data[i] && data[i] != 88 && data[i] != 0x41)
899 printf ("i%d ", data[i]);
904 printf("%02x ", data[i]);