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_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))
289 match_u32_assert (0);
290 match_u32_assert (0);
292 printf ("nested %d bytes", subn);
294 printf ("; \"%s\", substitutions:", get_string());
297 int n_subst = get_u32();
300 printf (" %d", n_subst);
301 total_subs *= n_subst;
304 for (int i = 0; i < total_subs; i++)
307 dump_value (level + 1);
314 match_byte_assert (0x58);
315 printf ("\"%s\" with substitutions:", get_string());
318 int n_subst = get_u32();
321 printf (" %d", n_subst);
322 total_subs *= n_subst;
325 for (int i = 0; i < total_subs; i++)
328 dump_value (level + 1);
334 dump_dim_value(int level)
336 for (int i = 0; i <= level; i++)
342 if (match_byte (0x31))
345 printf("(footnote %d) ", get_u32());
346 match_byte_assert (0);
347 match_byte_assert (0);
348 int subn = get_u32 ();
349 printf ("nested %d bytes", subn);
353 match_byte_assert (0x58);
355 printf("string \"%s\"", get_string());
357 match_byte_assert (1);
363 else if (match_byte (5))
365 match_byte_assert (0x58);
366 printf ("variable \"%s\"", get_string());
368 match_byte_assert (2);
370 else if (match_byte (2))
376 match_byte_assert (0x58);
378 value = get_double ();
380 vallab = get_string ();
381 printf ("value %g format %d(%d.%d) var \"%s\" vallab \"%s\"",
382 value, format >> 16, (format >> 8) & 0xff, format & 0xff, var, vallab);
384 match_u32_assert (2);
387 else if (match_byte (1))
392 match_byte_assert (0x58);
394 value = get_double ();
395 printf ("value %g format %d(%d.%d)", value, format >> 16, (format >> 8) & 0xff, format & 0xff);
408 match_byte_assert (0x31);
409 match_u32_assert (0);
410 match_u32_assert (0);
412 printf ("nested %d bytes", subn);
414 printf ("; \"%s\", substitutions:", get_string());
417 int n_subst = get_u32();
420 printf (" %d", n_subst);
421 total_subs *= n_subst;
424 for (int i = 0; i < total_subs; i++)
427 dump_value (level + 1);
433 dump_category(int level)
443 else if (match_u32 (1))
450 else if (match_byte (1))
454 match_u32_assert (1);
460 match_u32_assert (0);
464 int n_categories = get_u32();
465 if (n_categories > 0)
466 printf (", %d subcategories:", n_categories);
468 for (int i = 0; i < n_categories; i++)
469 dump_category (level + 1);
476 printf("next dim\n");
481 match_byte_assert(0x58);
483 printf("string \"%s\": ", get_string());
484 match_byte(1) || match_byte(0);
486 else if (match_byte(5))
488 match_byte_assert(0x58);
489 printf("variable \"%s\": ", get_string());
492 match_byte_assert(3);
494 else if (match_byte(0x31))
499 match_u32_assert (0);
500 match_u32_assert (0);
502 printf ("nested %d bytes", subn);
504 printf ("; \"%s\", substitutions:", get_string());
507 int n_subst = get_u32();
510 printf (" %d", n_subst);
511 total_subs *= n_subst;
514 for (int i = 0; i < total_subs; i++)
524 match_byte_assert (0x58);
525 printf ("\"%s\" with substitutions:", get_string());
528 int n_subst = get_u32();
531 printf (" %d", n_subst);
532 total_subs *= n_subst;
535 for (int i = 0; i < total_subs; i++)
542 /* This byte is usually 0x02 but 0x00 and 0x75 (!) have also been spotted. */
545 if (!match_byte(0) && !match_byte(1))
546 match_byte_assert(2);
550 match_byte_assert(1);
560 n_categories = get_u32();
561 printf("%d nested categories\n", n_categories);
562 for (int i = 0; i < n_categories; i++)
569 int n_dims = get_u32();
571 printf ("%u dimensions\n", n_dims);
572 for (int i = 0; i < n_dims; i++)
580 main(int argc, char *argv[])
585 if (isatty(STDIN_FILENO))
587 fprintf(stderr, "redirect stdin from a .bin file\n");
590 if (fstat(STDIN_FILENO, &s))
602 if (read(STDIN_FILENO, data, n) != n)
610 if (!strcmp(argv[1], "title0"))
613 if (match_byte (0x03)
614 || (match_byte (0x05) && match_byte (0x58)))
615 printf ("%s\n", get_string());
617 printf ("<unknown>\n");
620 if (!strcmp(argv[1], "title"))
622 const char fonts[] = "\x01\x31\x09\0\0\0SansSerif";
624 n = find(fonts, sizeof fonts - 1);
626 else if (!strcmp(argv[1], "fonts"))
628 const char fonts[] = "\x01\x31\x09\0\0\0SansSerif";
629 const char styles[] = "\xf0\0\0\0";
630 start = find(fonts, sizeof fonts - 1);
631 n = find(styles, sizeof styles - 1);
633 else if (!strcmp(argv[1], "styles"))
635 const char styles[] = "\xf0\0\0\0";
636 const char dimensions[] = "-,,,.\0";
637 start = find(styles, sizeof styles - 1);
638 n = find(dimensions, sizeof dimensions - 1) + sizeof dimensions - 1;
640 else if (!strcmp(argv[1], "dimensions"))
643 const char dimensions[] = "-,,,.\0";
644 start = try_find_tail(dimensions, sizeof dimensions - 1);
649 const char dimensions[] = "-,,, .\0";
650 start = find_tail(dimensions, sizeof dimensions - 1);
659 fprintf (stderr, "unknown section %s\n", argv[1]);
666 for (size_t i = start; i < n; )
673 && i + 4 + data[i] <= n
674 && all_ascii(&data[i + 4], data[i]))
676 fputs("\n\"", stdout);
677 fwrite(&data[i + 4], 1, data[i], stdout);
678 fputs("\" ", stdout);
689 memcpy (&d, &data[i + 4], 8);
690 printf ("F40.%d(%.*f)\n", data[i], data[i], d);
700 memcpy (&d, &data[i + 4], 8);
701 printf ("PCT40.%d(%.*f)\n", data[i], data[i], d);
705 && (data[i] && data[i] != 88 && data[i] != 0x41)
710 printf ("i%d ", data[i]);
715 printf("%02x ", data[i]);