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 match_byte_assert (0x58);
276 value = get_double ();
277 printf ("value %g format %d(%d.%d)", value, format >> 16, (format >> 8) & 0xff, format & 0xff);
284 else if (match_byte (0x31))
290 printf("(footnote %d) ", get_u32());
291 match_byte_assert (0);
292 match_byte_assert (0);
293 int subn = get_u32 ();
294 printf ("nested %d bytes", subn);
299 match_u32_assert (0);
300 match_u32_assert (0);
302 printf ("nested %d bytes", subn);
305 printf ("; \"%s\", substitutions:", get_string());
306 int total_subs = get_u32();
310 total_subs = (total_subs - 1) + x;
311 match_u32_assert (0);
313 printf (" (total %d)", total_subs);
315 for (int i = 0; i < total_subs; i++)
318 dump_value (level + 1);
324 match_byte_assert (0x58);
325 char *base = get_string();
327 printf ("\"%s\" with %d variables:\n", base, x);
330 for (int i = 0; i < x; i++)
332 dump_value (level+1);
338 for (int i = 0; i < x; i++)
342 for (int j = 0; j <= level; j++)
344 printf("variable %d has %d values:\n", i, y);
345 for (int j = 0; j < y; j++)
349 char *a = get_string();
350 match_byte_assert(0x58);
351 char *b = get_string();
352 char *c = get_string();
353 for (int k = 0; k <= level + 1; k++)
355 printf ("\"%s\", \"%s\", \"%s\"", a, b, c);
363 dump_value (level+1);
372 dump_dim_value(int level)
374 for (int i = 0; i <= level; i++)
380 if (match_byte (0x31))
383 printf("(footnote %d) ", get_u32());
384 match_byte_assert (0);
385 match_byte_assert (0);
386 int subn = get_u32 ();
387 printf ("nested %d bytes", subn);
391 match_byte_assert (0x58);
393 printf("string \"%s\"", get_string());
395 match_byte_assert (1);
401 else if (match_byte (5))
403 match_byte_assert (0x58);
404 printf ("variable \"%s\"", get_string());
406 match_byte_assert (2);
408 else if (match_byte (2))
414 match_byte_assert (0x58);
416 value = get_double ();
418 vallab = get_string ();
419 printf ("value %g format %d(%d.%d) var \"%s\" vallab \"%s\"",
420 value, format >> 16, (format >> 8) & 0xff, format & 0xff, var, vallab);
422 match_u32_assert (2);
425 else if (match_byte (1))
430 match_byte_assert (0x58);
432 value = get_double ();
433 printf ("value %g format %d(%d.%d)", value, format >> 16, (format >> 8) & 0xff, format & 0xff);
445 match_byte_assert (0x31);
446 match_u32_assert (0);
447 match_u32_assert (0);
449 printf ("nested %d bytes", subn);
451 printf ("; \"%s\", substitutions:", get_string());
452 int total_subs = get_u32();
456 total_subs = (total_subs - 1) + x;
457 match_u32_assert (0);
459 printf (" (total %d)", total_subs);
461 for (int i = 0; i < total_subs; i++)
464 dump_value (level + 1);
470 dump_category(int level)
480 else if (match_u32 (1))
487 else if (match_byte (1))
491 match_u32_assert (1);
497 match_u32_assert (0);
501 int n_categories = get_u32();
502 if (n_categories > 0)
503 printf (", %d subcategories:", n_categories);
505 for (int i = 0; i < n_categories; i++)
506 dump_category (level + 1);
513 printf("next dim\n");
518 match_byte_assert(0x58);
520 printf("string \"%s\": ", get_string());
521 match_byte(1) || match_byte(0);
523 else if (match_byte(5))
525 match_byte_assert(0x58);
526 printf("variable \"%s\": ", get_string());
529 match_byte_assert(3);
531 else if (match_byte(0x31))
536 match_u32_assert (0);
537 match_u32_assert (0);
539 printf ("nested %d bytes", subn);
541 printf ("; \"%s\", substitutions:", get_string());
544 int n_subst = get_u32();
547 printf (" %d", n_subst);
548 total_subs *= n_subst;
551 for (int i = 0; i < total_subs; i++)
561 match_byte_assert (0x58);
562 printf ("\"%s\" with substitutions:", get_string());
565 int n_subst = get_u32();
568 printf (" %d", n_subst);
569 total_subs *= n_subst;
572 for (int i = 0; i < total_subs; i++)
579 /* This byte is usually 0x02 but 0x00 and 0x75 (!) have also been spotted. */
582 if (!match_byte(0) && !match_byte(1))
583 match_byte_assert(2);
587 match_byte_assert(1);
597 n_categories = get_u32();
598 printf("%d nested categories\n", n_categories);
599 for (int i = 0; i < n_categories; i++)
606 int n_dims = get_u32();
608 printf ("%u dimensions\n", n_dims);
609 for (int i = 0; i < n_dims; i++)
617 main(int argc, char *argv[])
622 if (isatty(STDIN_FILENO))
624 fprintf(stderr, "redirect stdin from a .bin file\n");
627 if (fstat(STDIN_FILENO, &s))
639 if (read(STDIN_FILENO, data, n) != n)
647 if (!strcmp(argv[1], "title0"))
650 if (match_byte (0x03)
651 || (match_byte (0x05) && match_byte (0x58)))
652 printf ("%s\n", get_string());
654 printf ("<unknown>\n");
657 if (!strcmp(argv[1], "title"))
659 const char fonts[] = "\x01\x31\x09\0\0\0SansSerif";
661 n = find(fonts, sizeof fonts - 1);
663 else if (!strcmp(argv[1], "fonts"))
665 const char fonts[] = "\x01\x31\x09\0\0\0SansSerif";
666 const char styles[] = "\xf0\0\0\0";
667 start = find(fonts, sizeof fonts - 1);
668 n = find(styles, sizeof styles - 1);
670 else if (!strcmp(argv[1], "styles"))
672 const char styles[] = "\xf0\0\0\0";
673 const char dimensions[] = "-,,,.\0";
674 start = find(styles, sizeof styles - 1);
675 n = find(dimensions, sizeof dimensions - 1) + sizeof dimensions - 1;
677 else if (!strcmp(argv[1], "dimensions"))
680 const char dimensions[] = "-,,,.\0";
681 start = try_find_tail(dimensions, sizeof dimensions - 1);
686 const char dimensions[] = "-,,, .\0";
687 start = find_tail(dimensions, sizeof dimensions - 1);
696 fprintf (stderr, "unknown section %s\n", argv[1]);
703 for (size_t i = start; i < n; )
710 && i + 4 + data[i] <= n
711 && all_ascii(&data[i + 4], data[i]))
713 fputs("\n\"", stdout);
714 fwrite(&data[i + 4], 1, data[i], stdout);
715 fputs("\" ", stdout);
726 memcpy (&d, &data[i + 4], 8);
727 printf ("F40.%d(%.*f)\n", data[i], data[i], d);
737 memcpy (&d, &data[i + 4], 8);
738 printf ("PCT40.%d(%.*f)\n", data[i], data[i], d);
742 && (data[i] && data[i] != 88 && data[i] != 0x41)
747 printf ("i%d ", data[i]);
752 printf("%02x ", data[i]);