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++)
159 if (match_byte (0x31))
163 printf("(footnote %d) ", get_u32());
164 match_byte_assert (0);
165 match_byte_assert (0);
169 match_u32_assert (2);
170 printf("(special 2)");
171 match_byte_assert(0);
172 match_byte_assert(0);
174 match_byte_assert(0);
175 match_byte_assert(0);
177 int subn = get_u32 ();
178 printf ("nested %d bytes", subn);
182 match_byte_assert (0x58);
184 printf("string \"%s\"", get_string());
194 else if (match_byte (5))
196 match_byte_assert (0x58);
197 printf ("variable \"%s\"", get_string());
200 match_byte_assert (2);
206 else if (match_byte (2))
212 match_byte_assert (0x58);
214 value = get_double ();
216 vallab = get_string ();
217 printf ("value %g format %d(%d.%d) var \"%s\" vallab \"%s\"",
218 value, format >> 16, (format >> 8) & 0xff, format & 0xff, var, vallab);
220 match_u32_assert (2);
226 else if (match_byte (4))
229 char *var, *vallab, *value;
231 match_byte_assert (0x58);
233 vallab = get_string ();
235 match_byte_assert (2);
236 value = get_string ();
237 printf ("value \"%s\" format %d(%d.%d) var \"%s\" vallab \"%s\"",
238 value, format >> 16, (format >> 8) & 0xff, format & 0xff, var, vallab);
244 else if (match_byte (1))
249 match_byte_assert (0x58);
251 value = get_double ();
252 printf ("value %g format %d(%d.%d)", value, format >> 16, (format >> 8) & 0xff, format & 0xff);
259 else if (match_byte (0x31))
264 match_u32_assert (0);
265 match_u32_assert (0);
267 printf ("nested %d bytes", subn);
269 printf ("; \"%s\", substitutions:", get_string());
272 int n_subst = get_u32();
275 printf (" %d", n_subst);
276 total_subs *= n_subst;
279 for (int i = 0; i < total_subs; i++)
282 dump_value (level + 1);
289 match_byte_assert (0x58);
290 printf ("\"%s\" with substitutions:", get_string());
293 int n_subst = get_u32();
296 printf (" %d", n_subst);
297 total_subs *= n_subst;
300 for (int i = 0; i < total_subs; i++)
303 dump_value (level + 1);
309 dump_dim_value(int level)
311 for (int i = 0; i <= level; i++)
317 if (match_byte (0x31))
320 printf("(footnote %d) ", get_u32());
321 match_byte_assert (0);
322 match_byte_assert (0);
323 int subn = get_u32 ();
324 printf ("nested %d bytes", subn);
328 match_byte_assert (0x58);
330 printf("string \"%s\"", get_string());
332 match_byte_assert (1);
338 else if (match_byte (5))
340 match_byte_assert (0x58);
341 printf ("variable \"%s\"", get_string());
343 match_byte_assert (2);
345 else if (match_byte (2))
351 match_byte_assert (0x58);
353 value = get_double ();
355 vallab = get_string ();
356 printf ("value %g format %d(%d.%d) var \"%s\" vallab \"%s\"",
357 value, format >> 16, (format >> 8) & 0xff, format & 0xff, var, vallab);
359 match_u32_assert (2);
362 else if (match_byte (1))
367 match_byte_assert (0x58);
369 value = get_double ();
370 printf ("value %g format %d(%d.%d)", value, format >> 16, (format >> 8) & 0xff, format & 0xff);
383 match_byte_assert (0x31);
384 match_u32_assert (0);
385 match_u32_assert (0);
387 printf ("nested %d bytes", subn);
389 printf ("; \"%s\", substitutions:", get_string());
392 int n_subst = get_u32();
395 printf (" %d", n_subst);
396 total_subs *= n_subst;
399 for (int i = 0; i < total_subs; i++)
402 dump_value (level + 1);
408 dump_category(int level)
418 else if (match_u32 (1))
425 else if (match_byte (1))
428 match_u32_assert (1);
434 match_u32_assert (0);
438 int n_categories = get_u32();
439 if (n_categories > 0)
440 printf (", %d subcategories:", n_categories);
442 for (int i = 0; i < n_categories; i++)
443 dump_category (level + 1);
450 printf("next dim\n");
455 match_byte_assert(0x58);
457 printf("string \"%s\": ", get_string());
458 match_byte_assert(1);
460 else if (match_byte(5))
462 match_byte_assert(0x58);
463 printf("variable \"%s\": ", get_string());
466 match_byte_assert(3);
473 match_byte_assert(0x31);
474 match_u32_assert (0);
475 match_u32_assert (0);
477 printf ("nested %d bytes", subn);
479 printf ("; \"%s\", substitutions:", get_string());
482 int n_subst = get_u32();
485 printf (" %d", n_subst);
486 total_subs *= n_subst;
489 for (int i = 0; i < total_subs; i++)
496 match_byte_assert(0);
497 if (!match_byte(0) && !match_byte(1))
498 match_byte_assert(2);
501 match_byte_assert(1);
511 n_categories = get_u32();
512 printf("%d nested categories\n", n_categories);
513 for (int i = 0; i < n_categories; i++)
520 int n_dims = get_u32();
522 printf ("%u dimensions\n", n_dims);
523 for (int i = 0; i < n_dims; i++)
531 main(int argc, char *argv[])
536 if (isatty(STDIN_FILENO))
538 fprintf(stderr, "redirect stdin from a .bin file\n");
541 if (fstat(STDIN_FILENO, &s))
553 if (read(STDIN_FILENO, data, n) != n)
561 if (!strcmp(argv[1], "title0"))
564 if (match_byte (0x03)
565 || (match_byte (0x05) && match_byte (0x58)))
566 printf ("%s\n", get_string());
568 printf ("<unknown>\n");
571 if (!strcmp(argv[1], "title"))
573 const char fonts[] = "\x01\x31\x09\0\0\0SansSerif";
575 n = find(fonts, sizeof fonts - 1);
577 else if (!strcmp(argv[1], "fonts"))
579 const char fonts[] = "\x01\x31\x09\0\0\0SansSerif";
580 const char styles[] = "\xf0\0\0\0";
581 start = find(fonts, sizeof fonts - 1);
582 n = find(styles, sizeof styles - 1);
584 else if (!strcmp(argv[1], "styles"))
586 const char styles[] = "\xf0\0\0\0";
587 const char dimensions[] = "-,,,.\0";
588 start = find(styles, sizeof styles - 1);
589 n = find(dimensions, sizeof dimensions - 1) + sizeof dimensions - 1;
591 else if (!strcmp(argv[1], "dimensions"))
594 const char dimensions[] = "-,,,.\0";
595 start = try_find_tail(dimensions, sizeof dimensions - 1);
600 const char dimensions[] = "-,,, .\0";
601 start = find_tail(dimensions, sizeof dimensions - 1);
610 fprintf (stderr, "unknown section %s\n", argv[1]);
617 for (size_t i = start; i < n; )
624 && i + 4 + data[i] <= n
625 && all_ascii(&data[i + 4], data[i]))
627 fputs("\n\"", stdout);
628 fwrite(&data[i + 4], 1, data[i], stdout);
629 fputs("\" ", stdout);
640 memcpy (&d, &data[i + 4], 8);
641 printf ("F40.%d(%.*f)\n", data[i], data[i], d);
651 memcpy (&d, &data[i + 4], 8);
652 printf ("PCT40.%d(%.*f)\n", data[i], data[i], d);
656 && (data[i] && data[i] != 88 && data[i] != 0x41)
661 printf ("i%d ", data[i]);
666 printf("%02x ", data[i]);