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_u32 (1) && !match_u32(2))
241 match_u32_assert (3);
247 else if (match_byte (4))
250 char *var, *vallab, *value;
252 match_byte_assert (0x58);
254 vallab = get_string ();
257 match_byte_assert (2);
258 value = get_string ();
259 printf ("value \"%s\" format %d(%d.%d) var \"%s\" vallab \"%s\"",
260 value, format >> 16, (format >> 8) & 0xff, format & 0xff, var, vallab);
266 else if (match_byte (1))
271 match_byte_assert (0x58);
273 value = get_double ();
274 printf ("value %g format %d(%d.%d)", value, format >> 16, (format >> 8) & 0xff, format & 0xff);
281 else if (match_byte (0x31))
286 match_u32_assert (0);
287 match_u32_assert (0);
289 printf ("nested %d bytes", subn);
291 printf ("; \"%s\", substitutions:", get_string());
294 int n_subst = get_u32();
297 printf (" %d", n_subst);
298 total_subs *= n_subst;
301 for (int i = 0; i < total_subs; i++)
304 dump_value (level + 1);
311 match_byte_assert (0x58);
312 printf ("\"%s\" with substitutions:", get_string());
315 int n_subst = get_u32();
318 printf (" %d", n_subst);
319 total_subs *= n_subst;
322 for (int i = 0; i < total_subs; i++)
325 dump_value (level + 1);
331 dump_dim_value(int level)
333 for (int i = 0; i <= level; i++)
339 if (match_byte (0x31))
342 printf("(footnote %d) ", get_u32());
343 match_byte_assert (0);
344 match_byte_assert (0);
345 int subn = get_u32 ();
346 printf ("nested %d bytes", subn);
350 match_byte_assert (0x58);
352 printf("string \"%s\"", get_string());
354 match_byte_assert (1);
360 else if (match_byte (5))
362 match_byte_assert (0x58);
363 printf ("variable \"%s\"", get_string());
365 match_byte_assert (2);
367 else if (match_byte (2))
373 match_byte_assert (0x58);
375 value = get_double ();
377 vallab = get_string ();
378 printf ("value %g format %d(%d.%d) var \"%s\" vallab \"%s\"",
379 value, format >> 16, (format >> 8) & 0xff, format & 0xff, var, vallab);
381 match_u32_assert (2);
384 else if (match_byte (1))
389 match_byte_assert (0x58);
391 value = get_double ();
392 printf ("value %g format %d(%d.%d)", value, format >> 16, (format >> 8) & 0xff, format & 0xff);
405 match_byte_assert (0x31);
406 match_u32_assert (0);
407 match_u32_assert (0);
409 printf ("nested %d bytes", subn);
411 printf ("; \"%s\", substitutions:", get_string());
414 int n_subst = get_u32();
417 printf (" %d", n_subst);
418 total_subs *= n_subst;
421 for (int i = 0; i < total_subs; i++)
424 dump_value (level + 1);
430 dump_category(int level)
440 else if (match_u32 (1))
447 else if (match_byte (1))
451 match_u32_assert (1);
457 match_u32_assert (0);
461 int n_categories = get_u32();
462 if (n_categories > 0)
463 printf (", %d subcategories:", n_categories);
465 for (int i = 0; i < n_categories; i++)
466 dump_category (level + 1);
473 printf("next dim\n");
478 match_byte_assert(0x58);
480 printf("string \"%s\": ", get_string());
481 match_byte(1) || match_byte(0);
483 else if (match_byte(5))
485 match_byte_assert(0x58);
486 printf("variable \"%s\": ", get_string());
489 match_byte_assert(3);
491 else if (match_byte(0x31))
496 match_u32_assert (0);
497 match_u32_assert (0);
499 printf ("nested %d bytes", subn);
501 printf ("; \"%s\", substitutions:", get_string());
504 int n_subst = get_u32();
507 printf (" %d", n_subst);
508 total_subs *= n_subst;
511 for (int i = 0; i < total_subs; i++)
521 match_byte_assert (0x58);
522 printf ("\"%s\" with substitutions:", get_string());
525 int n_subst = get_u32();
528 printf (" %d", n_subst);
529 total_subs *= n_subst;
532 for (int i = 0; i < total_subs; i++)
539 match_byte_assert(0);
540 if (!match_byte(0) && !match_byte(1))
541 match_byte_assert(2);
544 match_byte_assert(1);
554 n_categories = get_u32();
555 printf("%d nested categories\n", n_categories);
556 for (int i = 0; i < n_categories; i++)
563 int n_dims = get_u32();
565 printf ("%u dimensions\n", n_dims);
566 for (int i = 0; i < n_dims; i++)
574 main(int argc, char *argv[])
579 if (isatty(STDIN_FILENO))
581 fprintf(stderr, "redirect stdin from a .bin file\n");
584 if (fstat(STDIN_FILENO, &s))
596 if (read(STDIN_FILENO, data, n) != n)
604 if (!strcmp(argv[1], "title0"))
607 if (match_byte (0x03)
608 || (match_byte (0x05) && match_byte (0x58)))
609 printf ("%s\n", get_string());
611 printf ("<unknown>\n");
614 if (!strcmp(argv[1], "title"))
616 const char fonts[] = "\x01\x31\x09\0\0\0SansSerif";
618 n = find(fonts, sizeof fonts - 1);
620 else if (!strcmp(argv[1], "fonts"))
622 const char fonts[] = "\x01\x31\x09\0\0\0SansSerif";
623 const char styles[] = "\xf0\0\0\0";
624 start = find(fonts, sizeof fonts - 1);
625 n = find(styles, sizeof styles - 1);
627 else if (!strcmp(argv[1], "styles"))
629 const char styles[] = "\xf0\0\0\0";
630 const char dimensions[] = "-,,,.\0";
631 start = find(styles, sizeof styles - 1);
632 n = find(dimensions, sizeof dimensions - 1) + sizeof dimensions - 1;
634 else if (!strcmp(argv[1], "dimensions"))
637 const char dimensions[] = "-,,,.\0";
638 start = try_find_tail(dimensions, sizeof dimensions - 1);
643 const char dimensions[] = "-,,, .\0";
644 start = find_tail(dimensions, sizeof dimensions - 1);
653 fprintf (stderr, "unknown section %s\n", argv[1]);
660 for (size_t i = start; i < n; )
667 && i + 4 + data[i] <= n
668 && all_ascii(&data[i + 4], data[i]))
670 fputs("\n\"", stdout);
671 fwrite(&data[i + 4], 1, data[i], stdout);
672 fputs("\" ", stdout);
683 memcpy (&d, &data[i + 4], 8);
684 printf ("F40.%d(%.*f)\n", data[i], data[i], d);
694 memcpy (&d, &data[i + 4], 8);
695 printf ("PCT40.%d(%.*f)\n", data[i], data[i], d);
699 && (data[i] && data[i] != 88 && data[i] != 0x41)
704 printf ("i%d ", data[i]);
709 printf("%02x ", data[i]);