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);
241 match_u32_assert (2);
247 else if (match_byte (4))
250 char *var, *vallab, *value;
252 match_byte_assert (0x58);
254 vallab = get_string ();
256 match_byte_assert (2);
257 value = get_string ();
258 printf ("value \"%s\" format %d(%d.%d) var \"%s\" vallab \"%s\"",
259 value, format >> 16, (format >> 8) & 0xff, format & 0xff, var, vallab);
265 else if (match_byte (1))
270 match_byte_assert (0x58);
272 value = get_double ();
273 printf ("value %g format %d(%d.%d)", value, format >> 16, (format >> 8) & 0xff, format & 0xff);
280 else if (match_byte (0x31))
285 match_u32_assert (0);
286 match_u32_assert (0);
288 printf ("nested %d bytes", subn);
290 printf ("; \"%s\", 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);
310 match_byte_assert (0x58);
311 printf ("\"%s\" with substitutions:", get_string());
314 int n_subst = get_u32();
317 printf (" %d", n_subst);
318 total_subs *= n_subst;
321 for (int i = 0; i < total_subs; i++)
324 dump_value (level + 1);
330 dump_dim_value(int level)
332 for (int i = 0; i <= level; i++)
338 if (match_byte (0x31))
341 printf("(footnote %d) ", get_u32());
342 match_byte_assert (0);
343 match_byte_assert (0);
344 int subn = get_u32 ();
345 printf ("nested %d bytes", subn);
349 match_byte_assert (0x58);
351 printf("string \"%s\"", get_string());
353 match_byte_assert (1);
359 else if (match_byte (5))
361 match_byte_assert (0x58);
362 printf ("variable \"%s\"", get_string());
364 match_byte_assert (2);
366 else if (match_byte (2))
372 match_byte_assert (0x58);
374 value = get_double ();
376 vallab = get_string ();
377 printf ("value %g format %d(%d.%d) var \"%s\" vallab \"%s\"",
378 value, format >> 16, (format >> 8) & 0xff, format & 0xff, var, vallab);
380 match_u32_assert (2);
383 else if (match_byte (1))
388 match_byte_assert (0x58);
390 value = get_double ();
391 printf ("value %g format %d(%d.%d)", value, format >> 16, (format >> 8) & 0xff, format & 0xff);
404 match_byte_assert (0x31);
405 match_u32_assert (0);
406 match_u32_assert (0);
408 printf ("nested %d bytes", subn);
410 printf ("; \"%s\", substitutions:", get_string());
413 int n_subst = get_u32();
416 printf (" %d", n_subst);
417 total_subs *= n_subst;
420 for (int i = 0; i < total_subs; i++)
423 dump_value (level + 1);
429 dump_category(int level)
439 else if (match_u32 (1))
446 else if (match_byte (1))
449 match_u32_assert (1);
455 match_u32_assert (0);
459 int n_categories = get_u32();
460 if (n_categories > 0)
461 printf (", %d subcategories:", n_categories);
463 for (int i = 0; i < n_categories; i++)
464 dump_category (level + 1);
471 printf("next dim\n");
476 match_byte_assert(0x58);
478 printf("string \"%s\": ", get_string());
479 match_byte(1) || match_byte(0);
481 else if (match_byte(5))
483 match_byte_assert(0x58);
484 printf("variable \"%s\": ", get_string());
487 match_byte_assert(3);
494 match_byte_assert(0x31);
495 match_u32_assert (0);
496 match_u32_assert (0);
498 printf ("nested %d bytes", subn);
500 printf ("; \"%s\", substitutions:", get_string());
503 int n_subst = get_u32();
506 printf (" %d", n_subst);
507 total_subs *= n_subst;
510 for (int i = 0; i < total_subs; i++)
517 match_byte_assert(0);
518 if (!match_byte(0) && !match_byte(1))
519 match_byte_assert(2);
522 match_byte_assert(1);
532 n_categories = get_u32();
533 printf("%d nested categories\n", n_categories);
534 for (int i = 0; i < n_categories; i++)
541 int n_dims = get_u32();
543 printf ("%u dimensions\n", n_dims);
544 for (int i = 0; i < n_dims; i++)
552 main(int argc, char *argv[])
557 if (isatty(STDIN_FILENO))
559 fprintf(stderr, "redirect stdin from a .bin file\n");
562 if (fstat(STDIN_FILENO, &s))
574 if (read(STDIN_FILENO, data, n) != n)
582 if (!strcmp(argv[1], "title0"))
585 if (match_byte (0x03)
586 || (match_byte (0x05) && match_byte (0x58)))
587 printf ("%s\n", get_string());
589 printf ("<unknown>\n");
592 if (!strcmp(argv[1], "title"))
594 const char fonts[] = "\x01\x31\x09\0\0\0SansSerif";
596 n = find(fonts, sizeof fonts - 1);
598 else if (!strcmp(argv[1], "fonts"))
600 const char fonts[] = "\x01\x31\x09\0\0\0SansSerif";
601 const char styles[] = "\xf0\0\0\0";
602 start = find(fonts, sizeof fonts - 1);
603 n = find(styles, sizeof styles - 1);
605 else if (!strcmp(argv[1], "styles"))
607 const char styles[] = "\xf0\0\0\0";
608 const char dimensions[] = "-,,,.\0";
609 start = find(styles, sizeof styles - 1);
610 n = find(dimensions, sizeof dimensions - 1) + sizeof dimensions - 1;
612 else if (!strcmp(argv[1], "dimensions"))
615 const char dimensions[] = "-,,,.\0";
616 start = try_find_tail(dimensions, sizeof dimensions - 1);
621 const char dimensions[] = "-,,, .\0";
622 start = find_tail(dimensions, sizeof dimensions - 1);
631 fprintf (stderr, "unknown section %s\n", argv[1]);
638 for (size_t i = start; i < n; )
645 && i + 4 + data[i] <= n
646 && all_ascii(&data[i + 4], data[i]))
648 fputs("\n\"", stdout);
649 fwrite(&data[i + 4], 1, data[i], stdout);
650 fputs("\" ", stdout);
661 memcpy (&d, &data[i + 4], 8);
662 printf ("F40.%d(%.*f)\n", data[i], data[i], d);
672 memcpy (&d, &data[i + 4], 8);
673 printf ("PCT40.%d(%.*f)\n", data[i], data[i], d);
677 && (data[i] && data[i] != 88 && data[i] != 0x41)
682 printf ("i%d ", data[i]);
687 printf("%02x ", data[i]);