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))
291 printf("(footnote %d) ", get_u32());
292 match_byte_assert (0);
293 match_byte_assert (0);
294 int subn = get_u32 ();
295 printf ("nested %d bytes", subn);
300 match_u32_assert (0);
301 match_u32_assert (0);
303 printf ("nested %d bytes", subn);
306 printf ("; \"%s\", substitutions:", get_string());
309 int n_subst = get_u32();
312 printf (" %d", n_subst);
313 total_subs *= n_subst;
316 for (int i = 0; i < total_subs; i++)
319 dump_value (level + 1);
326 match_byte_assert (0x58);
327 printf ("\"%s\" with substitutions:", get_string());
330 int n_subst = get_u32();
333 printf (" %d", n_subst);
334 total_subs *= n_subst;
337 for (int i = 0; i < total_subs; i++)
340 dump_value (level + 1);
346 dump_dim_value(int level)
348 for (int i = 0; i <= level; i++)
354 if (match_byte (0x31))
357 printf("(footnote %d) ", get_u32());
358 match_byte_assert (0);
359 match_byte_assert (0);
360 int subn = get_u32 ();
361 printf ("nested %d bytes", subn);
365 match_byte_assert (0x58);
367 printf("string \"%s\"", get_string());
369 match_byte_assert (1);
375 else if (match_byte (5))
377 match_byte_assert (0x58);
378 printf ("variable \"%s\"", get_string());
380 match_byte_assert (2);
382 else if (match_byte (2))
388 match_byte_assert (0x58);
390 value = get_double ();
392 vallab = get_string ();
393 printf ("value %g format %d(%d.%d) var \"%s\" vallab \"%s\"",
394 value, format >> 16, (format >> 8) & 0xff, format & 0xff, var, vallab);
396 match_u32_assert (2);
399 else if (match_byte (1))
404 match_byte_assert (0x58);
406 value = get_double ();
407 printf ("value %g format %d(%d.%d)", value, format >> 16, (format >> 8) & 0xff, format & 0xff);
420 match_byte_assert (0x31);
421 match_u32_assert (0);
422 match_u32_assert (0);
424 printf ("nested %d bytes", subn);
426 printf ("; \"%s\", substitutions:", get_string());
429 int n_subst = get_u32();
432 printf (" %d", n_subst);
433 total_subs *= n_subst;
436 for (int i = 0; i < total_subs; i++)
439 dump_value (level + 1);
445 dump_category(int level)
455 else if (match_u32 (1))
462 else if (match_byte (1))
466 match_u32_assert (1);
472 match_u32_assert (0);
476 int n_categories = get_u32();
477 if (n_categories > 0)
478 printf (", %d subcategories:", n_categories);
480 for (int i = 0; i < n_categories; i++)
481 dump_category (level + 1);
488 printf("next dim\n");
493 match_byte_assert(0x58);
495 printf("string \"%s\": ", get_string());
496 match_byte(1) || match_byte(0);
498 else if (match_byte(5))
500 match_byte_assert(0x58);
501 printf("variable \"%s\": ", get_string());
504 match_byte_assert(3);
506 else if (match_byte(0x31))
511 match_u32_assert (0);
512 match_u32_assert (0);
514 printf ("nested %d bytes", subn);
516 printf ("; \"%s\", substitutions:", get_string());
519 int n_subst = get_u32();
522 printf (" %d", n_subst);
523 total_subs *= n_subst;
526 for (int i = 0; i < total_subs; i++)
536 match_byte_assert (0x58);
537 printf ("\"%s\" with substitutions:", get_string());
540 int n_subst = get_u32();
543 printf (" %d", n_subst);
544 total_subs *= n_subst;
547 for (int i = 0; i < total_subs; i++)
554 /* This byte is usually 0x02 but 0x00 and 0x75 (!) have also been spotted. */
557 if (!match_byte(0) && !match_byte(1))
558 match_byte_assert(2);
562 match_byte_assert(1);
572 n_categories = get_u32();
573 printf("%d nested categories\n", n_categories);
574 for (int i = 0; i < n_categories; i++)
581 int n_dims = get_u32();
583 printf ("%u dimensions\n", n_dims);
584 for (int i = 0; i < n_dims; i++)
592 main(int argc, char *argv[])
597 if (isatty(STDIN_FILENO))
599 fprintf(stderr, "redirect stdin from a .bin file\n");
602 if (fstat(STDIN_FILENO, &s))
614 if (read(STDIN_FILENO, data, n) != n)
622 if (!strcmp(argv[1], "title0"))
625 if (match_byte (0x03)
626 || (match_byte (0x05) && match_byte (0x58)))
627 printf ("%s\n", get_string());
629 printf ("<unknown>\n");
632 if (!strcmp(argv[1], "title"))
634 const char fonts[] = "\x01\x31\x09\0\0\0SansSerif";
636 n = find(fonts, sizeof fonts - 1);
638 else if (!strcmp(argv[1], "fonts"))
640 const char fonts[] = "\x01\x31\x09\0\0\0SansSerif";
641 const char styles[] = "\xf0\0\0\0";
642 start = find(fonts, sizeof fonts - 1);
643 n = find(styles, sizeof styles - 1);
645 else if (!strcmp(argv[1], "styles"))
647 const char styles[] = "\xf0\0\0\0";
648 const char dimensions[] = "-,,,.\0";
649 start = find(styles, sizeof styles - 1);
650 n = find(dimensions, sizeof dimensions - 1) + sizeof dimensions - 1;
652 else if (!strcmp(argv[1], "dimensions"))
655 const char dimensions[] = "-,,,.\0";
656 start = try_find_tail(dimensions, sizeof dimensions - 1);
661 const char dimensions[] = "-,,, .\0";
662 start = find_tail(dimensions, sizeof dimensions - 1);
671 fprintf (stderr, "unknown section %s\n", argv[1]);
678 for (size_t i = start; i < n; )
685 && i + 4 + data[i] <= n
686 && all_ascii(&data[i + 4], data[i]))
688 fputs("\n\"", stdout);
689 fwrite(&data[i + 4], 1, data[i], stdout);
690 fputs("\" ", stdout);
701 memcpy (&d, &data[i + 4], 8);
702 printf ("F40.%d(%.*f)\n", data[i], data[i], d);
712 memcpy (&d, &data[i + 4], 8);
713 printf ("PCT40.%d(%.*f)\n", data[i], data[i], d);
717 && (data[i] && data[i] != 88 && data[i] != 0x41)
722 printf ("i%d ", data[i]);
727 printf("%02x ", data[i]);