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);
228 match_byte_assert(1);
231 else if (match_byte (5))
234 printf ("variable \"%s\"", get_string());
236 if (!match_byte(1) && !match_byte(2))
237 match_byte_assert(3);
239 else if (match_byte (2))
245 match_byte_assert (0x58);
247 value = get_double ();
249 vallab = get_string ();
250 printf ("value %g format %d(%d.%d) var \"%s\" vallab \"%s\"",
251 value, format >> 16, (format >> 8) & 0xff, format & 0xff, var, vallab);
252 if (!match_byte (1) && !match_byte(2))
253 match_byte_assert (3);
255 else if (match_byte (4))
258 char *var, *vallab, *value;
260 match_byte_assert (0x58);
262 vallab = get_string ();
264 if (!match_byte(1) && !match_byte(2))
265 match_byte_assert (3);
266 value = get_string ();
267 printf ("value \"%s\" format %d(%d.%d) var \"%s\" vallab \"%s\"",
268 value, format >> 16, (format >> 8) & 0xff, format & 0xff, var, vallab);
270 else if (match_byte (1))
277 value = get_double ();
278 printf ("value %g format %d(%d.%d)", value, format >> 16, (format >> 8) & 0xff, format & 0xff);
284 char *base = get_string();
287 printf ("\"%s\" with %d variables:\n", base, x);
290 for (int i = 0; i < x; i++)
292 dump_value (level+1);
298 for (int i = 0; i < x; i++)
302 for (int j = 0; j <= level; j++)
304 printf("variable %d has %d values:\n", i, y);
305 for (int j = 0; j < y; j++)
310 char *a = get_string();
311 match_byte_assert(0x58);
312 char *b = get_string();
313 char *c = get_string();
314 for (int k = 0; k <= level + 1; k++)
316 printf ("\"%s\", \"%s\", \"%s\"", a, b, c);
319 dump_value (level+1);
336 dump_dim_value(int level)
338 for (int i = 0; i <= level; i++)
346 printf("string \"%s\"", get_string());
348 match_byte_assert(1);
350 else if (match_byte (5))
352 match_byte_assert (0x58);
353 printf ("variable \"%s\"", get_string());
355 if (!match_byte (1) && !match_byte (2))
356 match_byte_assert(3);
363 dump_category(int level)
373 else if (match_u32 (1))
380 else if (match_byte (1))
384 match_u32_assert (1);
390 match_u32_assert (0);
394 int n_categories = get_u32();
395 if (n_categories > 0)
396 printf (", %d subcategories:", n_categories);
398 for (int i = 0; i < n_categories; i++)
399 dump_category (level + 1);
406 printf("next dim\n");
410 /* This byte is usually 0x02 but 0x00 and 0x75 (!) have also been spotted. */
413 if (!match_byte(0) && !match_byte(1))
414 match_byte_assert(2);
418 match_byte_assert(1);
428 n_categories = get_u32();
429 printf("%d nested categories\n", n_categories);
430 for (int i = 0; i < n_categories; i++)
439 printf ("%u dimensions\n", n_dims);
440 for (int i = 0; i < n_dims; i++)
448 dump_data_value(void)
461 value = get_double ();
462 printf (" value %g format %d(%d.%d)", value, format >> 16, (format >> 8) & 0xff, format & 0xff);
464 else if (match_byte (3))
469 printf("string \"%s\"", get_string());
471 match_byte_assert(1);
473 else if (match_byte (2))
479 match_byte_assert (0x58);
481 value = get_double ();
483 vallab = get_string ();
484 printf ("value %g format %d(%d.%d) var \"%s\" vallab \"%s\"",
485 value, format >> 16, (format >> 8) & 0xff, format & 0xff, var, vallab);
486 if (!match_byte (1) && !match_byte(2))
487 match_byte_assert (3);
489 else if (match_byte (4))
492 char *var, *vallab, *value;
494 match_byte_assert (0x58);
496 vallab = get_string ();
498 if (!match_byte(1) && !match_byte(2))
499 match_byte_assert (3);
500 value = get_string ();
501 printf ("value \"%s\" format %d(%d.%d) var \"%s\" vallab \"%s\"",
502 value, format >> 16, (format >> 8) & 0xff, format & 0xff, var, vallab);
504 else if (data[pos] == 5)
509 char *base = get_string();
511 printf ("\"%s\"; %d variables:\n", base, x);
512 for (int i = 0; i < x; i++)
519 for (int j = 0; j <= 0; j++)
521 printf("variable %d has %d values:\n", i, y);
522 for (int j = 0; j < y; j++)
533 for (int i = 0; i < 3 + n_dims; i++)
535 printf ("data intro:");
536 for (int i = 0; i < 3 + n_dims; i++)
540 fprintf (stderr,"data intro (%d dims):", n_dims);
541 for (int i = 0; i < 3+n_dims; i++)
542 fprintf (stderr," %d", get_u32());
543 fprintf(stderr,"\n");
546 printf ("%d data values, starting at %08x\n", x, pos);
547 for (int i = 0; i < x; i++)
549 printf("%08x, index %d:\n", pos, get_u32());
557 dump_title_value(int level)
559 for (int i = 0; i <= level; i++)
566 if (data[pos] == 3 || data[pos] == 2 || data[pos] == 4 || data[pos] == 1)
568 else if (match_byte (5))
571 printf ("variable \"%s\"", get_string());
573 if (!match_byte(1) && !match_byte(2))
574 match_byte_assert(3);
580 char *base = get_string();
582 printf ("\"%s\" with %d variables:\n", base, x);
583 for (int i = 0; i < x; i++)
590 for (int j = 0; j <= level; j++)
592 printf("variable %d has %d values:\n", i, y);
593 for (int j = 0; j < y; j++)
598 char *a = get_string();
599 match_byte_assert(0x58);
600 char *b = get_string();
601 char *c = get_string();
602 for (int k = 0; k <= level + 1; k++)
604 printf ("\"%s\", \"%s\", \"%s\"", a, b, c);
606 match_byte_assert(1);
609 dump_title_value (level+1);
617 dump_footnote_value(int level)
619 for (int i = 0; i <= level; i++)
626 if (data[pos] == 2 || data[pos] == 4)
628 else if (match_byte (3))
633 printf("string \"%s\"", get_string());
635 match_byte_assert (1);
637 else if (match_byte (5))
639 match_byte_assert (0x58);
640 printf ("variable \"%s\"", get_string());
642 if (!match_byte(1) && !match_byte(2))
643 match_byte_assert(3);
645 else if (match_byte (1))
652 value = get_double ();
653 printf ("value %g format %d(%d.%d)", value, format >> 16, (format >> 8) & 0xff, format & 0xff);
658 char *base = get_string();
660 printf ("\"%s\"; %d variables:\n", base, x);
661 for (int i = 0; i < x; i++)
668 for (int j = 0; j <= level; j++)
670 printf("variable %d has %d values:\n", i, y);
671 for (int j = 0; j < y; j++)
673 dump_footnote_value (level+1);
684 dump_title_value(0); putchar('\n');
685 dump_title_value(0); putchar('\n');
686 match_byte_assert(0x31);
687 dump_title_value(0); putchar('\n');
689 match_byte_assert(0x58);
690 if (match_byte(0x31))
692 dump_footnote_value(0); putchar('\n');
695 match_byte_assert(0x58);
698 int n_footnotes = get_u32();
699 if (n_footnotes >= 20)
701 fprintf(stderr, "%08x: %d footnotes\n", pos - 4, n_footnotes);
705 printf("------\n%d footnotes\n", n_footnotes);
706 if (n_footnotes < 20)
708 for (int i = 0; i < n_footnotes; i++)
710 printf("footnote %d:\n", i);
711 dump_footnote_value(0);
716 if (match_byte (0x31))
718 /* Custom footnote marker string. */
719 match_byte_assert(3);
721 match_byte_assert(0x58);
726 match_byte_assert (0x58);
727 printf("(%d)\n", get_u32());
733 find_dimensions(void)
736 const char dimensions[] = "-,,,.\0";
737 int x = try_find_tail(dimensions, sizeof dimensions - 1);
742 const char dimensions[] = "-,,, .\0";
743 return find_tail(dimensions, sizeof dimensions - 1);
749 printf("fonts: offset=%08x\n", pos);
751 for (int i = 1; i <= 8; i++)
753 printf("%08x: font %d, ", pos, i);
754 match_byte_assert(i);
755 match_byte_assert(0x31);
756 printf("%s, ", get_string());
757 match_byte_assert(0);
758 match_byte_assert(0);
759 if (!match_byte(0x40) && !match_byte(0x20) && !match_byte(0x80) && !match_byte(0x10))
760 match_byte_assert(0x50);
761 if (!match_byte(0x41))
762 match_byte_assert(0x51);
764 printf ("%s, ", get_string());
765 printf ("%s, ", get_string());
776 match_u32_assert(240);
779 match_u32_assert(18);
786 match_u32_assert(142);
790 int count = get_u32();
793 char *encoding = get_string();
794 printf("encoding=%s\n", encoding);
797 match_u32_assert(UINT32_MAX);
799 match_byte_assert(1);
800 match_byte_assert(0);
802 match_byte_assert(1);
803 if (!match_byte(0x99) && !match_byte(0x98))
804 match_byte_assert(0x97);
805 match_byte_assert(7);
806 match_byte_assert(0);
807 match_byte_assert(0);
809 match_byte_assert(',');
812 match_byte_assert(',');
813 if (!match_byte('.'))
814 match_byte_assert(' ');
817 for (int i = 0; i < 5; i++)
820 if (pos != find_dimensions())
821 fprintf (stderr, "%08x / %08x\n", pos, find_dimensions());
825 main(int argc, char *argv[])
830 if (isatty(STDIN_FILENO))
832 fprintf(stderr, "redirect stdin from a .bin file\n");
835 if (fstat(STDIN_FILENO, &s))
847 if (read(STDIN_FILENO, data, n) != n)
855 if (!strcmp(argv[1], "title0"))
858 if (match_byte (0x03)
859 || (match_byte (0x05) && match_byte (0x58)))
860 printf ("%s\n", get_string());
862 printf ("<unknown>\n");
865 else if (!strcmp(argv[1], "title"))
870 else if (!strcmp(argv[1], "titleraw"))
872 const char fonts[] = "\x01\x31\x09\0\0\0SansSerif";
874 n = find(fonts, sizeof fonts - 1);
876 else if (!strcmp(argv[1], "fonts"))
878 const char fonts[] = "\x01\x31\x09\0\0\0SansSerif";
879 const char styles[] = "\xf0\0\0\0";
880 start = find(fonts, sizeof fonts - 1);
881 n = find(styles, sizeof styles - 1);
883 else if (!strcmp(argv[1], "styles"))
885 const char styles[] = "\xf0\0\0\0";
886 const char dimensions[] = "-,,,.\0";
887 start = find(styles, sizeof styles - 1);
888 n = find(dimensions, sizeof dimensions - 1) + sizeof dimensions - 1;
890 else if (!strcmp(argv[1], "dimensions") || !strcmp(argv[1], "all"))
893 match_byte_assert(1);
894 match_byte_assert(0);
896 match_byte_assert(1);
898 match_byte_assert(1);
899 match_byte_assert(0);
900 match_byte_assert(0);
902 match_byte_assert(1);
904 match_byte_assert(0);
905 match_byte_assert(0);
906 match_byte_assert(0);
910 printf("\n\ndata:\n");
913 match_byte_assert (1);
916 fprintf (stderr, "%x / %x\n", pos, n);
923 fprintf (stderr, "unknown section %s\n", argv[1]);
930 for (size_t i = start; i < n; )
937 && i + 4 + data[i] + data[i + 1] * 256 <= n
938 && all_ascii(&data[i + 4], data[i] + data[i + 1] * 256))
940 fputs("\n\"", stdout);
941 fwrite(&data[i + 4], 1, data[i] + data[i + 1] * 256, stdout);
942 fputs("\" ", stdout);
944 i += 4 + data[i] + data[i + 1] * 256;
953 memcpy (&d, &data[i + 4], 8);
954 printf ("F40.%d(%.*f)\n", data[i], data[i], d);
964 memcpy (&d, &data[i + 4], 8);
965 printf ("PCT40.%d(%.*f)\n", data[i], data[i], d);
969 && (data[i] && data[i] != 88 && data[i] != 0x41)
974 printf ("i%d ", data[i]);
979 printf("%02x ", data[i]);