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, bool match1);
210 dump_value__(int level, bool match1)
212 for (int i = 0; i <= level; i++)
217 char *s1 = get_string();
219 char *s2 = get_string();
220 char *s3 = get_string();
222 printf("strings \"%s\", \"%s\" and \"%s\"", s1, s2, s3);
224 printf("string \"%s\" and \"%s\"", s1, s2);
226 match_byte_assert(1);
230 else if (match_byte (5))
233 printf ("variable \"%s\"", get_string());
235 if (!match_byte(1) && !match_byte(2))
236 match_byte_assert(3);
238 else if (match_byte (2))
244 match_byte_assert (0x58);
246 value = get_double ();
248 vallab = get_string ();
249 printf ("value %g format %d(%d.%d) var \"%s\" vallab \"%s\"",
250 value, format >> 16, (format >> 8) & 0xff, format & 0xff, var, vallab);
251 if (!match_byte (1) && !match_byte(2))
252 match_byte_assert (3);
254 else if (match_byte (4))
257 char *var, *vallab, *value;
259 match_byte_assert (0x58);
261 vallab = get_string ();
263 if (!match_byte(1) && !match_byte(2))
264 match_byte_assert (3);
265 value = get_string ();
266 printf ("value \"%s\" format %d(%d.%d) var \"%s\" vallab \"%s\"",
267 value, format >> 16, (format >> 8) & 0xff, format & 0xff, var, vallab);
269 else if (match_byte (1))
276 value = get_double ();
277 printf ("value %g format %d(%d.%d)", value, format >> 16, (format >> 8) & 0xff, format & 0xff);
285 char *base = get_string();
287 printf ("\"%s\" with %d variables:\n", base, x);
288 for (int i = 0; i < x; i++)
295 for (int j = 0; j <= level; j++)
297 printf("variable %d has %d values:\n", i, y);
298 for (int j = 0; j < y; j++)
304 dump_value__ (level + 1, false);
312 dump_category(int level)
318 dump_value__ (level, true);
325 else if (match_byte (1))
329 match_u32_assert (1);
332 else if (!match_u32(2))
333 match_u32_assert (0);
337 int n_categories = get_u32();
338 if (n_categories > 0)
339 printf (", %d subcategories:", n_categories);
341 for (int i = 0; i < n_categories; i++)
342 dump_category (level + 1);
345 static void dump_title_value(int level);
351 printf("next dim\n");
354 /* This byte is usually 0x02 but 0x00 and 0x75 (!) have also been spotted. */
357 if (!match_byte(0) && !match_byte(1))
358 match_byte_assert(2);
362 match_byte_assert(1);
366 n_categories = get_u32();
367 printf("%d nested categories\n", n_categories);
368 for (int i = 0; i < n_categories; i++)
377 printf ("%u dimensions\n", n_dims);
378 for (int i = 0; i < n_dims; i++)
386 dump_data_value(int level)
388 for (int i = 0; i <= level; i++)
395 dump_value__(0, false);
403 for (int i = 0; i < 3 + n_dims; i++)
405 printf ("data intro:");
406 for (int i = 0; i < 3 + n_dims; i++)
410 fprintf (stderr,"data intro (%d dims):", n_dims);
411 for (int i = 0; i < 3+n_dims; i++)
412 fprintf (stderr," %d", get_u32());
413 fprintf(stderr,"\n");
416 printf ("%d data values, starting at %08x\n", x, pos);
417 for (int i = 0; i < x; i++)
419 printf("%08x, index %d:\n", pos, get_u32());
427 dump_title_value(int level)
429 for (int i = 0; i <= level; i++)
436 dump_value__(level, true);
443 dump_title_value(0); putchar('\n');
444 dump_title_value(0); putchar('\n');
445 match_byte_assert(0x31);
446 dump_title_value(0); putchar('\n');
448 match_byte_assert(0x58);
449 if (match_byte(0x31))
451 dump_data_value(0); putchar('\n');
454 match_byte_assert(0x58);
457 int n_footnotes = get_u32();
458 if (n_footnotes >= 20)
460 fprintf(stderr, "%08x: %d footnotes\n", pos - 4, n_footnotes);
464 printf("------\n%d footnotes\n", n_footnotes);
465 if (n_footnotes < 20)
467 for (int i = 0; i < n_footnotes; i++)
469 printf("footnote %d:\n", i);
471 if (match_byte (0x31))
473 /* Custom footnote marker string. */
474 match_byte_assert(3);
476 match_byte_assert(0x58);
481 match_byte_assert (0x58);
482 printf("(%d)\n", get_u32());
488 find_dimensions(void)
491 const char dimensions[] = "-,,,.\0";
492 int x = try_find_tail(dimensions, sizeof dimensions - 1);
497 const char dimensions[] = "-,,, .\0";
498 return find_tail(dimensions, sizeof dimensions - 1);
504 printf("fonts: offset=%08x\n", pos);
506 for (int i = 1; i <= 8; i++)
508 printf("%08x: font %d, ", pos, i);
509 match_byte_assert(i);
510 match_byte_assert(0x31);
511 printf("%s, ", get_string());
512 match_byte_assert(0);
513 match_byte_assert(0);
514 if (!match_byte(0x40) && !match_byte(0x20) && !match_byte(0x80) && !match_byte(0x10))
515 match_byte_assert(0x50);
516 if (!match_byte(0x41))
517 match_byte_assert(0x51);
519 printf ("%s, ", get_string());
520 printf ("%s, ", get_string());
531 match_u32_assert(240);
534 match_u32_assert(18);
541 match_u32_assert(142);
545 int count = get_u32();
548 char *encoding = get_string();
549 printf("encoding=%s\n", encoding);
552 match_u32_assert(UINT32_MAX);
554 match_byte_assert(1);
555 match_byte_assert(0);
557 match_byte_assert(1);
558 if (!match_byte(0x99) && !match_byte(0x98))
559 match_byte_assert(0x97);
560 match_byte_assert(7);
561 match_byte_assert(0);
562 match_byte_assert(0);
564 match_byte_assert(',');
567 match_byte_assert(',');
568 if (!match_byte('.'))
569 match_byte_assert(' ');
572 for (int i = 0; i < 5; i++)
575 if (pos != find_dimensions())
576 fprintf (stderr, "%08x / %08x\n", pos, find_dimensions());
580 main(int argc, char *argv[])
585 if (isatty(STDIN_FILENO))
587 fprintf(stderr, "redirect stdin from a .bin file\n");
590 if (fstat(STDIN_FILENO, &s))
602 if (read(STDIN_FILENO, data, n) != n)
610 if (!strcmp(argv[1], "title0"))
613 if (match_byte (0x03)
614 || (match_byte (0x05) && match_byte (0x58)))
615 printf ("%s\n", get_string());
617 printf ("<unknown>\n");
620 else if (!strcmp(argv[1], "title"))
625 else if (!strcmp(argv[1], "titleraw"))
627 const char fonts[] = "\x01\x31\x09\0\0\0SansSerif";
629 n = find(fonts, sizeof fonts - 1);
631 else if (!strcmp(argv[1], "fonts"))
633 const char fonts[] = "\x01\x31\x09\0\0\0SansSerif";
634 const char styles[] = "\xf0\0\0\0";
635 start = find(fonts, sizeof fonts - 1);
636 n = find(styles, sizeof styles - 1);
638 else if (!strcmp(argv[1], "styles"))
640 const char styles[] = "\xf0\0\0\0";
641 const char dimensions[] = "-,,,.\0";
642 start = find(styles, sizeof styles - 1);
643 n = find(dimensions, sizeof dimensions - 1) + sizeof dimensions - 1;
645 else if (!strcmp(argv[1], "dimensions") || !strcmp(argv[1], "all"))
648 match_byte_assert(1);
649 match_byte_assert(0);
651 match_byte_assert(1);
653 match_byte_assert(1);
654 match_byte_assert(0);
655 match_byte_assert(0);
657 match_byte_assert(1);
659 match_byte_assert(0);
660 match_byte_assert(0);
661 match_byte_assert(0);
665 printf("\n\ndata:\n");
670 fprintf (stderr, "%x / %x\n", pos, n);
677 fprintf (stderr, "unknown section %s\n", argv[1]);
684 for (size_t i = start; i < n; )
691 && i + 4 + data[i] + data[i + 1] * 256 <= n
692 && all_ascii(&data[i + 4], data[i] + data[i + 1] * 256))
694 fputs("\n\"", stdout);
695 fwrite(&data[i + 4], 1, data[i] + data[i + 1] * 256, stdout);
696 fputs("\" ", stdout);
698 i += 4 + data[i] + data[i + 1] * 256;
707 memcpy (&d, &data[i + 4], 8);
708 printf ("F40.%d(%.*f)\n", data[i], data[i], d);
718 memcpy (&d, &data[i + 4], 8);
719 printf ("PCT40.%d(%.*f)\n", data[i], data[i], d);
723 && (data[i] && data[i] != 88 && data[i] != 0x41)
728 printf ("i%d ", data[i]);
733 printf("%02x ", data[i]);