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++)
158 if (match_byte (0x31))
162 printf("(footnote %d) ", get_u32());
163 match_byte_assert (0);
164 match_byte_assert (0);
168 match_u32_assert (2);
169 printf("(special 2)");
170 match_byte_assert(0);
171 match_byte_assert(0);
173 match_byte_assert(0);
174 match_byte_assert(0);
176 int subn = get_u32 ();
177 printf ("nested %d bytes", subn);
181 match_byte_assert (0x58);
183 printf("string \"%s\"", get_string());
193 else if (match_byte (5))
195 match_byte_assert (0x58);
196 printf ("variable \"%s\"", get_string());
199 match_byte_assert (2);
205 else if (match_byte (2))
211 match_byte_assert (0x58);
213 value = get_double ();
215 vallab = get_string ();
216 printf ("value %g format %d(%d.%d) var \"%s\" vallab \"%s\"",
217 value, format >> 16, (format >> 8) & 0xff, format & 0xff, var, vallab);
219 match_u32_assert (2);
225 else if (match_byte (4))
228 char *var, *vallab, *value;
230 match_byte_assert (0x58);
232 vallab = get_string ();
234 match_byte_assert (2);
235 value = get_string ();
236 printf ("value \"%s\" format %d(%d.%d) var \"%s\" vallab \"%s\"",
237 value, format >> 16, (format >> 8) & 0xff, format & 0xff, var, vallab);
243 else if (match_byte (1))
248 match_byte_assert (0x58);
250 value = get_double ();
251 printf ("value %g format %d(%d.%d)", value, format >> 16, (format >> 8) & 0xff, format & 0xff);
264 match_byte_assert (0x31);
265 match_u32_assert (0);
266 match_u32_assert (0);
268 printf ("nested %d bytes", subn);
270 printf ("; \"%s\", substitutions:", get_string());
273 int n_subst = get_u32();
276 printf (" %d", n_subst);
277 total_subs *= n_subst;
280 for (int i = 0; i < total_subs; i++)
283 dump_value (level + 1);
289 dump_dim_value(int level)
291 for (int i = 0; i <= level; i++)
297 if (match_byte (0x31))
300 printf("(footnote %d) ", get_u32());
301 match_byte_assert (0);
302 match_byte_assert (0);
303 int subn = get_u32 ();
304 printf ("nested %d bytes", subn);
308 match_byte_assert (0x58);
310 printf("string \"%s\"", get_string());
312 match_byte_assert (1);
318 else if (match_byte (5))
320 match_byte_assert (0x58);
321 printf ("variable \"%s\"", get_string());
323 match_byte_assert (2);
325 else if (match_byte (2))
331 match_byte_assert (0x58);
333 value = get_double ();
335 vallab = get_string ();
336 printf ("value %g format %d(%d.%d) var \"%s\" vallab \"%s\"",
337 value, format >> 16, (format >> 8) & 0xff, format & 0xff, var, vallab);
339 match_u32_assert (2);
342 else if (match_byte (1))
347 match_byte_assert (0x58);
349 value = get_double ();
350 printf ("value %g format %d(%d.%d)", value, format >> 16, (format >> 8) & 0xff, format & 0xff);
363 match_byte_assert (0x31);
364 match_u32_assert (0);
365 match_u32_assert (0);
367 printf ("nested %d bytes", subn);
369 printf ("; \"%s\", substitutions:", get_string());
372 int n_subst = get_u32();
375 printf (" %d", n_subst);
376 total_subs *= n_subst;
379 for (int i = 0; i < total_subs; i++)
382 dump_value (level + 1);
388 dump_category(int level)
398 else if (match_u32 (1))
405 else if (match_byte (1))
408 match_u32_assert (1);
414 match_u32_assert (0);
418 int n_categories = get_u32();
419 if (n_categories > 0)
420 printf (", %d subcategories:", n_categories);
422 for (int i = 0; i < n_categories; i++)
423 dump_category (level + 1);
430 printf("next dim\n");
435 match_byte_assert(0x58);
437 printf("string \"%s\": ", get_string());
438 match_byte_assert(1);
440 else if (match_byte(5))
442 match_byte_assert(0x58);
443 printf("variable \"%s\": ", get_string());
446 match_byte_assert(3);
453 match_byte_assert(0x31);
454 match_u32_assert (0);
455 match_u32_assert (0);
457 printf ("nested %d bytes", subn);
459 printf ("; \"%s\", substitutions:", get_string());
462 int n_subst = get_u32();
465 printf (" %d", n_subst);
466 total_subs *= n_subst;
469 for (int i = 0; i < total_subs; i++)
476 match_byte_assert(0);
477 if (!match_byte(0) && !match_byte(1))
478 match_byte_assert(2);
481 match_byte_assert(1);
491 n_categories = get_u32();
492 printf("%d nested categories\n", n_categories);
493 for (int i = 0; i < n_categories; i++)
500 int n_dims = get_u32();
502 printf ("%u dimensions\n", n_dims);
503 for (int i = 0; i < n_dims; i++)
511 main(int argc, char *argv[])
516 if (isatty(STDIN_FILENO))
518 fprintf(stderr, "redirect stdin from a .bin file\n");
521 if (fstat(STDIN_FILENO, &s))
533 if (read(STDIN_FILENO, data, n) != n)
541 if (!strcmp(argv[1], "title0"))
544 if (match_byte (0x03)
545 || (match_byte (0x05) && match_byte (0x58)))
546 printf ("%s\n", get_string());
548 printf ("<unknown>\n");
551 if (!strcmp(argv[1], "title"))
553 const char fonts[] = "\x01\x31\x09\0\0\0SansSerif";
555 n = find(fonts, sizeof fonts - 1);
557 else if (!strcmp(argv[1], "fonts"))
559 const char fonts[] = "\x01\x31\x09\0\0\0SansSerif";
560 const char styles[] = "\xf0\0\0\0";
561 start = find(fonts, sizeof fonts - 1);
562 n = find(styles, sizeof styles - 1);
564 else if (!strcmp(argv[1], "styles"))
566 const char styles[] = "\xf0\0\0\0";
567 const char dimensions[] = "-,,,.\0";
568 start = find(styles, sizeof styles - 1);
569 n = find(dimensions, sizeof dimensions - 1) + sizeof dimensions - 1;
571 else if (!strcmp(argv[1], "dimensions"))
574 const char dimensions[] = "-,,,.\0";
575 start = try_find_tail(dimensions, sizeof dimensions - 1);
580 const char dimensions[] = "-,,, .\0";
581 start = find_tail(dimensions, sizeof dimensions - 1);
590 fprintf (stderr, "unknown section %s\n", argv[1]);
597 for (size_t i = start; i < n; )
604 && i + 4 + data[i] <= n
605 && all_ascii(&data[i + 4], data[i]))
607 fputs("\n\"", stdout);
608 fwrite(&data[i + 4], 1, data[i], stdout);
609 fputs("\" ", stdout);
620 memcpy (&d, &data[i + 4], 8);
621 printf ("F40.%d(%.*f)\n", data[i], data[i], d);
631 memcpy (&d, &data[i + 4], 8);
632 printf ("PCT40.%d(%.*f)\n", data[i], data[i], d);
636 && (data[i] && data[i] != 88 && data[i] != 0x41)
641 printf ("i%d ", data[i]);
646 printf("%02x ", data[i]);