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());
208 match_byte (1) || match_byte(3);
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 printf ("\"%s\" with substitutions:", get_string());
326 int total_subs = get_u32();
330 total_subs = (total_subs - 1) + x;
331 match_u32_assert (0);
333 printf (" (total %d)", total_subs);
335 for (int i = 0; i < total_subs; i++)
338 dump_value (level + 1);
344 dump_dim_value(int level)
346 for (int i = 0; i <= level; i++)
352 if (match_byte (0x31))
355 printf("(footnote %d) ", get_u32());
356 match_byte_assert (0);
357 match_byte_assert (0);
358 int subn = get_u32 ();
359 printf ("nested %d bytes", subn);
363 match_byte_assert (0x58);
365 printf("string \"%s\"", get_string());
367 match_byte_assert (1);
373 else if (match_byte (5))
375 match_byte_assert (0x58);
376 printf ("variable \"%s\"", get_string());
378 match_byte_assert (2);
380 else if (match_byte (2))
386 match_byte_assert (0x58);
388 value = get_double ();
390 vallab = get_string ();
391 printf ("value %g format %d(%d.%d) var \"%s\" vallab \"%s\"",
392 value, format >> 16, (format >> 8) & 0xff, format & 0xff, var, vallab);
394 match_u32_assert (2);
397 else if (match_byte (1))
402 match_byte_assert (0x58);
404 value = get_double ();
405 printf ("value %g format %d(%d.%d)", value, format >> 16, (format >> 8) & 0xff, format & 0xff);
417 match_byte_assert (0x31);
418 match_u32_assert (0);
419 match_u32_assert (0);
421 printf ("nested %d bytes", subn);
423 printf ("; \"%s\", substitutions:", get_string());
424 int total_subs = get_u32();
428 total_subs = (total_subs - 1) + x;
429 match_u32_assert (0);
431 printf (" (total %d)", total_subs);
433 for (int i = 0; i < total_subs; i++)
436 dump_value (level + 1);
442 dump_category(int level)
452 else if (match_u32 (1))
459 else if (match_byte (1))
463 match_u32_assert (1);
469 match_u32_assert (0);
473 int n_categories = get_u32();
474 if (n_categories > 0)
475 printf (", %d subcategories:", n_categories);
477 for (int i = 0; i < n_categories; i++)
478 dump_category (level + 1);
485 printf("next dim\n");
490 match_byte_assert(0x58);
492 printf("string \"%s\": ", get_string());
493 match_byte(1) || match_byte(0);
495 else if (match_byte(5))
497 match_byte_assert(0x58);
498 printf("variable \"%s\": ", get_string());
501 match_byte_assert(3);
503 else if (match_byte(0x31))
508 match_u32_assert (0);
509 match_u32_assert (0);
511 printf ("nested %d bytes", subn);
513 printf ("; \"%s\", substitutions:", get_string());
516 int n_subst = get_u32();
519 printf (" %d", n_subst);
520 total_subs *= n_subst;
523 for (int i = 0; i < total_subs; i++)
533 match_byte_assert (0x58);
534 printf ("\"%s\" with substitutions:", get_string());
537 int n_subst = get_u32();
540 printf (" %d", n_subst);
541 total_subs *= n_subst;
544 for (int i = 0; i < total_subs; i++)
551 /* This byte is usually 0x02 but 0x00 and 0x75 (!) have also been spotted. */
554 if (!match_byte(0) && !match_byte(1))
555 match_byte_assert(2);
559 match_byte_assert(1);
569 n_categories = get_u32();
570 printf("%d nested categories\n", n_categories);
571 for (int i = 0; i < n_categories; i++)
578 int n_dims = get_u32();
580 printf ("%u dimensions\n", n_dims);
581 for (int i = 0; i < n_dims; i++)
589 main(int argc, char *argv[])
594 if (isatty(STDIN_FILENO))
596 fprintf(stderr, "redirect stdin from a .bin file\n");
599 if (fstat(STDIN_FILENO, &s))
611 if (read(STDIN_FILENO, data, n) != n)
619 if (!strcmp(argv[1], "title0"))
622 if (match_byte (0x03)
623 || (match_byte (0x05) && match_byte (0x58)))
624 printf ("%s\n", get_string());
626 printf ("<unknown>\n");
629 if (!strcmp(argv[1], "title"))
631 const char fonts[] = "\x01\x31\x09\0\0\0SansSerif";
633 n = find(fonts, sizeof fonts - 1);
635 else if (!strcmp(argv[1], "fonts"))
637 const char fonts[] = "\x01\x31\x09\0\0\0SansSerif";
638 const char styles[] = "\xf0\0\0\0";
639 start = find(fonts, sizeof fonts - 1);
640 n = find(styles, sizeof styles - 1);
642 else if (!strcmp(argv[1], "styles"))
644 const char styles[] = "\xf0\0\0\0";
645 const char dimensions[] = "-,,,.\0";
646 start = find(styles, sizeof styles - 1);
647 n = find(dimensions, sizeof dimensions - 1) + sizeof dimensions - 1;
649 else if (!strcmp(argv[1], "dimensions"))
652 const char dimensions[] = "-,,,.\0";
653 start = try_find_tail(dimensions, sizeof dimensions - 1);
658 const char dimensions[] = "-,,, .\0";
659 start = find_tail(dimensions, sizeof dimensions - 1);
668 fprintf (stderr, "unknown section %s\n", argv[1]);
675 for (size_t i = start; i < n; )
682 && i + 4 + data[i] <= n
683 && all_ascii(&data[i + 4], data[i]))
685 fputs("\n\"", stdout);
686 fwrite(&data[i + 4], 1, data[i], stdout);
687 fputs("\" ", stdout);
698 memcpy (&d, &data[i + 4], 8);
699 printf ("F40.%d(%.*f)\n", data[i], data[i], d);
709 memcpy (&d, &data[i + 4], 8);
710 printf ("PCT40.%d(%.*f)\n", data[i], data[i], d);
714 && (data[i] && data[i] != 88 && data[i] != 0x41)
719 printf ("i%d ", data[i]);
724 printf("%02x ", data[i]);