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))
303 printf("(footnote %d) ", get_u32());
304 match_byte_assert (0);
305 match_byte_assert (0);
306 int subn = get_u32 ();
307 printf ("nested %d bytes", subn);
312 match_u32_assert (0);
313 match_u32_assert (0);
315 printf ("nested %d bytes", subn);
318 char *base = get_string();
320 printf ("\"%s\"; %d variables:\n", base, x);
323 for (int i = 0; i < x; i++)
325 dump_value (level+1);
331 for (int i = 0; i < x; i++)
335 for (int j = 0; j <= level; j++)
337 printf("variable %d has %d values:\n", i, y);
338 for (int j = 0; j < y; j++)
342 char *a = get_string();
343 match_byte_assert(0x58);
344 char *b = get_string();
345 char *c = get_string();
346 for (int k = 0; k <= level + 1; k++)
348 printf ("\"%s\", \"%s\", \"%s\"", a, b, c);
356 dump_value (level+1);
365 match_byte_assert (0x58);
366 char *base = get_string();
368 printf ("\"%s\" with %d variables:\n", base, x);
371 for (int i = 0; i < x; i++)
373 dump_value (level+1);
379 for (int i = 0; i < x; i++)
383 for (int j = 0; j <= level; j++)
385 printf("variable %d has %d values:\n", i, y);
386 for (int j = 0; j < y; j++)
390 char *a = get_string();
391 match_byte_assert(0x58);
392 char *b = get_string();
393 char *c = get_string();
394 for (int k = 0; k <= level + 1; k++)
396 printf ("\"%s\", \"%s\", \"%s\"", a, b, c);
404 dump_value (level+1);
413 dump_dim_value(int level)
415 for (int i = 0; i <= level; i++)
421 if (match_byte (0x31))
424 printf("(footnote %d) ", get_u32());
425 match_byte_assert (0);
426 match_byte_assert (0);
427 int subn = get_u32 ();
428 printf ("nested %d bytes", subn);
432 match_byte_assert (0x58);
434 printf("string \"%s\"", get_string());
436 match_byte_assert (1);
442 else if (match_byte (5))
444 match_byte_assert (0x58);
445 printf ("variable \"%s\"", get_string());
447 match_byte_assert (2);
449 else if (match_byte (2))
455 match_byte_assert (0x58);
457 value = get_double ();
459 vallab = get_string ();
460 printf ("value %g format %d(%d.%d) var \"%s\" vallab \"%s\"",
461 value, format >> 16, (format >> 8) & 0xff, format & 0xff, var, vallab);
463 match_u32_assert (2);
466 else if (match_byte (1))
471 match_byte_assert (0x58);
473 value = get_double ();
474 printf ("value %g format %d(%d.%d)", value, format >> 16, (format >> 8) & 0xff, format & 0xff);
486 match_byte_assert (0x31);
487 match_u32_assert (0);
488 match_u32_assert (0);
490 printf ("nested %d bytes", subn);
492 printf ("; \"%s\", substitutions:", get_string());
493 int total_subs = get_u32();
497 total_subs = (total_subs - 1) + x;
498 match_u32_assert (0);
500 printf (" (total %d)", total_subs);
502 for (int i = 0; i < total_subs; i++)
505 dump_value (level + 1);
511 dump_category(int level)
521 else if (match_u32 (1))
528 else if (match_byte (1))
532 match_u32_assert (1);
538 match_u32_assert (0);
542 int n_categories = get_u32();
543 if (n_categories > 0)
544 printf (", %d subcategories:", n_categories);
546 for (int i = 0; i < n_categories; i++)
547 dump_category (level + 1);
554 printf("next dim\n");
559 match_byte_assert(0x58);
561 printf("string \"%s\": ", get_string());
562 match_byte(1) || match_byte(0);
564 else if (match_byte(5))
566 match_byte_assert(0x58);
567 printf("variable \"%s\": ", get_string());
570 match_byte_assert(3);
572 else if (match_byte(0x31))
577 match_u32_assert (0);
578 match_u32_assert (0);
580 printf ("nested %d bytes", subn);
582 printf ("; \"%s\", substitutions:", get_string());
585 int n_subst = get_u32();
588 printf (" %d", n_subst);
589 total_subs *= n_subst;
592 for (int i = 0; i < total_subs; i++)
602 match_byte_assert (0x58);
603 printf ("\"%s\" with substitutions:", get_string());
606 int n_subst = get_u32();
609 printf (" %d", n_subst);
610 total_subs *= n_subst;
613 for (int i = 0; i < total_subs; i++)
620 /* This byte is usually 0x02 but 0x00 and 0x75 (!) have also been spotted. */
623 if (!match_byte(0) && !match_byte(1))
624 match_byte_assert(2);
628 match_byte_assert(1);
638 n_categories = get_u32();
639 printf("%d nested categories\n", n_categories);
640 for (int i = 0; i < n_categories; i++)
647 int n_dims = get_u32();
649 printf ("%u dimensions\n", n_dims);
650 for (int i = 0; i < n_dims; i++)
660 if (!match_u32 (0) || !match_u32 (1) || !match_u32(1) || !match_u32(0) || !match_u32(1))
662 printf ("skipping data\n");
666 fprintf (stderr,"data intro:");
667 for (int i = 0; i < 5; i++)
668 fprintf (stderr," %d", get_u32());
669 fprintf(stderr,"\n");
672 printf ("%d data values, starting at %08x\n", x, pos);
673 for (int i = 0; i < x; i++)
675 printf("%08x, index %d:\n", pos, get_u32());
682 if (match_byte (0x31))
686 printf("(footnote %d) ", get_u32());
687 match_byte_assert (0);
688 match_byte_assert (0);
689 int subn = get_u32 ();
690 printf ("nested %d bytes", subn);
695 match_byte_assert (0x58);
697 value = get_double ();
698 printf (" value %g format %d(%d.%d)", value, format >> 16, (format >> 8) & 0xff, format & 0xff);
700 else if (match_byte (3))
703 if (match_byte (0x31))
707 printf("(footnote %d) ", get_u32());
708 match_byte_assert (0);
709 match_byte_assert (0);
710 int subn = get_u32 ();
711 printf ("nested %d bytes", subn);
714 else if (match_u32 (2))
716 printf("(special 2)");
717 match_byte_assert(0);
718 match_byte_assert(0);
720 match_byte_assert(0);
721 match_byte_assert(0);
722 int subn = get_u32 ();
723 printf ("nested %d bytes", subn);
729 printf("(special 3)");
730 match_byte_assert(0);
731 match_byte_assert(0);
732 match_byte_assert(1);
733 match_byte_assert(0);
734 int subn = get_u32 ();
735 printf ("nested %d bytes, ", subn);
738 printf ("nested %d bytes, ", subn);
743 match_byte_assert (0x58);
745 printf("string \"%s\"", get_string());
748 else if (match_byte (2))
754 match_byte_assert (0x58);
756 value = get_double ();
758 vallab = get_string ();
759 printf ("value %g format %d(%d.%d) var \"%s\" vallab \"%s\"",
760 value, format >> 16, (format >> 8) & 0xff, format & 0xff, var, vallab);
761 if (!match_byte (1) && !match_byte(2))
762 match_byte_assert (3);
764 else if (match_byte (4))
767 char *var, *vallab, *value;
769 match_byte_assert (0x58);
771 vallab = get_string ();
773 if (!match_byte(1) && !match_byte(2))
774 match_byte_assert (3);
775 value = get_string ();
776 printf ("value \"%s\" format %d(%d.%d) var \"%s\" vallab \"%s\"",
777 value, format >> 16, (format >> 8) & 0xff, format & 0xff, var, vallab);
786 main(int argc, char *argv[])
791 if (isatty(STDIN_FILENO))
793 fprintf(stderr, "redirect stdin from a .bin file\n");
796 if (fstat(STDIN_FILENO, &s))
808 if (read(STDIN_FILENO, data, n) != n)
816 if (!strcmp(argv[1], "title0"))
819 if (match_byte (0x03)
820 || (match_byte (0x05) && match_byte (0x58)))
821 printf ("%s\n", get_string());
823 printf ("<unknown>\n");
826 if (!strcmp(argv[1], "title"))
828 const char fonts[] = "\x01\x31\x09\0\0\0SansSerif";
830 n = find(fonts, sizeof fonts - 1);
832 else if (!strcmp(argv[1], "fonts"))
834 const char fonts[] = "\x01\x31\x09\0\0\0SansSerif";
835 const char styles[] = "\xf0\0\0\0";
836 start = find(fonts, sizeof fonts - 1);
837 n = find(styles, sizeof styles - 1);
839 else if (!strcmp(argv[1], "styles"))
841 const char styles[] = "\xf0\0\0\0";
842 const char dimensions[] = "-,,,.\0";
843 start = find(styles, sizeof styles - 1);
844 n = find(dimensions, sizeof dimensions - 1) + sizeof dimensions - 1;
846 else if (!strcmp(argv[1], "dimensions"))
849 const char dimensions[] = "-,,,.\0";
850 start = try_find_tail(dimensions, sizeof dimensions - 1);
855 const char dimensions[] = "-,,, .\0";
856 start = find_tail(dimensions, sizeof dimensions - 1);
866 fprintf (stderr, "unknown section %s\n", argv[1]);
873 for (size_t i = start; i < n; )
880 && i + 4 + data[i] <= n
881 && all_ascii(&data[i + 4], data[i]))
883 fputs("\n\"", stdout);
884 fwrite(&data[i + 4], 1, data[i], stdout);
885 fputs("\" ", stdout);
896 memcpy (&d, &data[i + 4], 8);
897 printf ("F40.%d(%.*f)\n", data[i], data[i], d);
907 memcpy (&d, &data[i + 4], 8);
908 printf ("PCT40.%d(%.*f)\n", data[i], data[i], d);
912 && (data[i] && data[i] != 88 && data[i] != 0x41)
917 printf ("i%d ", data[i]);
922 printf("%02x ", data[i]);