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)
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)
130 if (data[pos + 1] == 0 && data[pos + 2] == 0 && data[pos + 3] == 0
131 /*&& all_ascii(&data[pos + 4], data[pos])*/)
134 char *s = malloc(len + 1);
136 memcpy(s, &data[pos + 4], len);
138 pos += 4 + data[pos];
143 fprintf(stderr, "%s: 0x%x: expected string\n", where, pos);
147 #define get_string() get_string(WHERE)
150 dump_value(int level)
152 for (int i = 0; i <= level; i++)
162 if (match_byte (0x31))
166 printf("(footnote %d) ", get_u32());
167 match_byte_assert (0);
168 match_byte_assert (0);
169 int subn = get_u32 ();
170 printf ("nested %d bytes", subn);
173 else if (match_u32 (2))
175 printf("(special 2)");
176 match_byte_assert(0);
177 match_byte_assert(0);
179 match_byte_assert(0);
180 match_byte_assert(0);
181 int subn = get_u32 ();
182 printf ("nested %d bytes", subn);
188 printf("(special 3)");
189 match_byte_assert(0);
190 match_byte_assert(0);
191 match_byte_assert(1);
192 match_byte_assert(0);
193 int subn = get_u32 ();
194 printf ("nested %d bytes, ", subn);
197 printf ("nested %d bytes, ", subn);
202 match_byte_assert (0x58);
204 printf("string \"%s\"", get_string());
215 else if (match_byte (5))
217 match_byte_assert (0x58);
218 printf ("variable \"%s\"", get_string());
220 if (!match_byte(1) && !match_byte(2))
221 match_byte_assert(3);
227 else if (match_byte (2))
233 match_byte_assert (0x58);
235 value = get_double ();
237 vallab = get_string ();
238 printf ("value %g format %d(%d.%d) var \"%s\" vallab \"%s\"",
239 value, format >> 16, (format >> 8) & 0xff, format & 0xff, var, vallab);
240 if (!match_byte (1) && !match_byte(2))
241 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);
269 else if (match_byte (1))
274 if (match_byte (0x31))
278 printf("(footnote %d) ", get_u32());
279 match_byte_assert (0);
280 match_byte_assert (0);
281 int subn = get_u32 ();
282 printf ("nested %d bytes", subn);
287 match_byte_assert (0x58);
289 value = get_double ();
290 printf ("value %g format %d(%d.%d)", value, format >> 16, (format >> 8) & 0xff, format & 0xff);
297 else if (match_byte (0x31))
301 printf("(footnote %d) ", get_u32());
302 match_byte_assert (0);
303 match_byte_assert (0);
304 int subn = get_u32 ();
305 printf ("nested %d bytes", subn);
310 match_u32_assert (0);
311 match_u32_assert (0);
312 int subn = get_u32 ();
313 printf ("nested %d bytes", subn);
316 char *base = get_string();
318 printf ("\"%s\"; %d variables:\n", base, x);
321 for (int i = 0; i < x; i++)
323 dump_value (level+1);
329 for (int i = 0; i < x; i++)
333 for (int j = 0; j <= level; j++)
335 printf("variable %d has %d values:\n", i, y);
336 for (int j = 0; j < y; j++)
340 char *a = get_string();
341 match_byte_assert(0x58);
342 char *b = get_string();
343 char *c = get_string();
344 for (int k = 0; k <= level + 1; k++)
346 printf ("\"%s\", \"%s\", \"%s\"", a, b, c);
354 dump_value (level+1);
363 match_byte_assert (0x58);
364 char *base = get_string();
366 printf ("\"%s\" with %d variables:\n", base, x);
369 for (int i = 0; i < x; i++)
371 dump_value (level+1);
377 for (int i = 0; i < x; i++)
381 for (int j = 0; j <= level; j++)
383 printf("variable %d has %d values:\n", i, y);
384 for (int j = 0; j < y; j++)
388 char *a = get_string();
389 match_byte_assert(0x58);
390 char *b = get_string();
391 char *c = get_string();
392 for (int k = 0; k <= level + 1; k++)
394 printf ("\"%s\", \"%s\", \"%s\"", a, b, c);
402 dump_value (level+1);
411 dump_dim_value(int level)
413 for (int i = 0; i <= level; i++)
419 if (match_byte (0x31))
422 printf("(footnote %d) ", get_u32());
423 match_byte_assert (0);
424 match_byte_assert (0);
425 int subn = get_u32 ();
426 printf ("nested %d bytes", subn);
430 match_byte_assert (0x58);
432 printf("string \"%s\"", get_string());
434 match_byte_assert (1);
440 else if (match_byte (5))
442 match_byte_assert (0x58);
443 printf ("variable \"%s\"", get_string());
445 match_byte_assert (2);
447 else if (match_byte (2))
453 match_byte_assert (0x58);
455 value = get_double ();
457 vallab = get_string ();
458 printf ("value %g format %d(%d.%d) var \"%s\" vallab \"%s\"",
459 value, format >> 16, (format >> 8) & 0xff, format & 0xff, var, vallab);
461 match_u32_assert (2);
464 else if (match_byte (1))
469 match_byte_assert (0x58);
471 value = get_double ();
472 printf ("value %g format %d(%d.%d)", value, format >> 16, (format >> 8) & 0xff, format & 0xff);
484 match_byte_assert (0x31);
485 match_u32_assert (0);
486 match_u32_assert (0);
488 printf ("nested %d bytes", subn);
490 printf ("; \"%s\", substitutions:", get_string());
491 int total_subs = get_u32();
495 total_subs = (total_subs - 1) + x;
496 match_u32_assert (0);
498 printf (" (total %d)", total_subs);
500 for (int i = 0; i < total_subs; i++)
503 dump_value (level + 1);
509 dump_category(int level)
519 else if (match_u32 (1))
526 else if (match_byte (1))
530 match_u32_assert (1);
536 match_u32_assert (0);
540 int n_categories = get_u32();
541 if (n_categories > 0)
542 printf (", %d subcategories:", n_categories);
544 for (int i = 0; i < n_categories; i++)
545 dump_category (level + 1);
552 printf("next dim\n");
557 match_byte_assert(0x58);
559 printf("string \"%s\": ", get_string());
560 match_byte(1) || match_byte(0);
562 else if (match_byte(5))
564 match_byte_assert(0x58);
565 printf("variable \"%s\": ", get_string());
568 match_byte_assert(3);
570 else if (match_byte(0x31))
575 match_u32_assert (0);
576 match_u32_assert (0);
578 printf ("nested %d bytes", subn);
580 printf ("; \"%s\", substitutions:", get_string());
583 int n_subst = get_u32();
586 printf (" %d", n_subst);
587 total_subs *= n_subst;
590 for (int i = 0; i < total_subs; i++)
600 match_byte_assert (0x58);
601 printf ("\"%s\" with substitutions:", get_string());
604 int n_subst = get_u32();
607 printf (" %d", n_subst);
608 total_subs *= n_subst;
611 for (int i = 0; i < total_subs; i++)
618 /* This byte is usually 0x02 but 0x00 and 0x75 (!) have also been spotted. */
621 if (!match_byte(0) && !match_byte(1))
622 match_byte_assert(2);
626 match_byte_assert(1);
636 n_categories = get_u32();
637 printf("%d nested categories\n", n_categories);
638 for (int i = 0; i < n_categories; i++)
647 printf ("%u dimensions\n", n_dims);
648 for (int i = 0; i < n_dims; i++)
660 for (int i = 0; i < 3 + n_dims; i++)
662 printf ("data intro:");
663 for (int i = 0; i < 3 + n_dims; i++)
667 fprintf (stderr,"data intro (%d dims):", n_dims);
668 for (int i = 0; i < 3+n_dims; i++)
669 fprintf (stderr," %d", get_u32());
670 fprintf(stderr,"\n");
673 printf ("%d data values, starting at %08x\n", x, pos);
674 for (int i = 0; i < x; i++)
676 printf("%08x, index %d:\n", pos, get_u32());
687 if (match_byte (0x31))
691 match_u32_assert (0);
692 int subn = get_u32 ();
693 printf ("nested %d bytes", subn);
696 else if (match_u32 (1))
698 printf("(footnote %d) ", get_u32());
699 match_byte_assert (0);
700 match_byte_assert (0);
701 int subn = get_u32 ();
702 printf ("nested %d bytes", subn);
708 printf("(special 2)");
709 match_byte_assert(0);
710 match_byte_assert(0);
712 match_byte_assert(0);
713 match_byte_assert(0);
714 int subn = get_u32 ();
715 printf ("nested %d bytes", subn);
720 match_byte_assert (0x58);
722 value = get_double ();
723 printf (" value %g format %d(%d.%d)", value, format >> 16, (format >> 8) & 0xff, format & 0xff);
725 else if (match_byte (3))
728 if (match_byte (0x31))
732 match_u32_assert (1);
733 int subn = get_u32 ();
734 printf ("nested %d bytes", subn);
737 else if (match_u32 (1))
739 printf("(footnote %d) ", get_u32());
740 match_byte_assert (0);
741 match_byte_assert (0);
742 int subn = get_u32 ();
743 printf ("nested %d bytes", subn);
746 else if (match_u32 (2))
748 printf("(special 2)");
749 match_byte_assert(0);
750 match_byte_assert(0);
752 match_byte_assert(0);
753 match_byte_assert(0);
754 int subn = get_u32 ();
755 printf ("nested %d bytes", subn);
761 printf("(special 3)");
762 match_byte_assert(0);
763 match_byte_assert(0);
764 match_byte_assert(1);
765 match_byte_assert(0);
766 int subn = get_u32 ();
767 printf ("nested %d bytes, ", subn);
770 printf ("nested %d bytes, ", subn);
775 match_byte_assert (0x58);
777 printf("string \"%s\"", get_string());
780 else if (match_byte (2))
786 match_byte_assert (0x58);
788 value = get_double ();
790 vallab = get_string ();
791 printf ("value %g format %d(%d.%d) var \"%s\" vallab \"%s\"",
792 value, format >> 16, (format >> 8) & 0xff, format & 0xff, var, vallab);
793 if (!match_byte (1) && !match_byte(2))
794 match_byte_assert (3);
796 else if (match_byte (4))
799 char *var, *vallab, *value;
801 match_byte_assert (0x58);
803 vallab = get_string ();
805 if (!match_byte(1) && !match_byte(2))
806 match_byte_assert (3);
807 value = get_string ();
808 printf ("value \"%s\" format %d(%d.%d) var \"%s\" vallab \"%s\"",
809 value, format >> 16, (format >> 8) & 0xff, format & 0xff, var, vallab);
818 main(int argc, char *argv[])
823 if (isatty(STDIN_FILENO))
825 fprintf(stderr, "redirect stdin from a .bin file\n");
828 if (fstat(STDIN_FILENO, &s))
840 if (read(STDIN_FILENO, data, n) != n)
848 if (!strcmp(argv[1], "title0"))
851 if (match_byte (0x03)
852 || (match_byte (0x05) && match_byte (0x58)))
853 printf ("%s\n", get_string());
855 printf ("<unknown>\n");
858 if (!strcmp(argv[1], "title"))
860 const char fonts[] = "\x01\x31\x09\0\0\0SansSerif";
862 n = find(fonts, sizeof fonts - 1);
864 else if (!strcmp(argv[1], "fonts"))
866 const char fonts[] = "\x01\x31\x09\0\0\0SansSerif";
867 const char styles[] = "\xf0\0\0\0";
868 start = find(fonts, sizeof fonts - 1);
869 n = find(styles, sizeof styles - 1);
871 else if (!strcmp(argv[1], "styles"))
873 const char styles[] = "\xf0\0\0\0";
874 const char dimensions[] = "-,,,.\0";
875 start = find(styles, sizeof styles - 1);
876 n = find(dimensions, sizeof dimensions - 1) + sizeof dimensions - 1;
878 else if (!strcmp(argv[1], "dimensions"))
881 const char dimensions[] = "-,,,.\0";
882 start = try_find_tail(dimensions, sizeof dimensions - 1);
887 const char dimensions[] = "-,,, .\0";
888 start = find_tail(dimensions, sizeof dimensions - 1);
898 fprintf (stderr, "unknown section %s\n", argv[1]);
905 for (size_t i = start; i < n; )
912 && i + 4 + data[i] <= n
913 && all_ascii(&data[i + 4], data[i]))
915 fputs("\n\"", stdout);
916 fwrite(&data[i + 4], 1, data[i], stdout);
917 fputs("\" ", stdout);
928 memcpy (&d, &data[i + 4], 8);
929 printf ("F40.%d(%.*f)\n", data[i], data[i], d);
939 memcpy (&d, &data[i + 4], 8);
940 printf ("PCT40.%d(%.*f)\n", data[i], data[i], d);
944 && (data[i] && data[i] != 88 && data[i] != 0x41)
949 printf ("i%d ", data[i]);
954 printf("%02x ", data[i]);