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))
290 printf("(footnote %d) ", get_u32());
291 match_byte_assert (0);
292 match_byte_assert (0);
293 int subn = get_u32 ();
294 printf ("nested %d bytes", subn);
299 match_u32_assert (0);
300 match_u32_assert (0);
302 printf ("nested %d bytes", subn);
305 char *base = get_string();
307 printf ("\"%s\"; %d variables:\n", base, x);
310 for (int i = 0; i < x; i++)
312 dump_value (level+1);
318 for (int i = 0; i < x; i++)
322 for (int j = 0; j <= level; j++)
324 printf("variable %d has %d values:\n", i, y);
325 for (int j = 0; j < y; j++)
329 char *a = get_string();
330 match_byte_assert(0x58);
331 char *b = get_string();
332 char *c = get_string();
333 for (int k = 0; k <= level + 1; k++)
335 printf ("\"%s\", \"%s\", \"%s\"", a, b, c);
343 dump_value (level+1);
352 match_byte_assert (0x58);
353 char *base = get_string();
355 printf ("\"%s\" with %d variables:\n", base, x);
358 for (int i = 0; i < x; i++)
360 dump_value (level+1);
366 for (int i = 0; i < x; i++)
370 for (int j = 0; j <= level; j++)
372 printf("variable %d has %d values:\n", i, y);
373 for (int j = 0; j < y; j++)
377 char *a = get_string();
378 match_byte_assert(0x58);
379 char *b = get_string();
380 char *c = get_string();
381 for (int k = 0; k <= level + 1; k++)
383 printf ("\"%s\", \"%s\", \"%s\"", a, b, c);
391 dump_value (level+1);
400 dump_dim_value(int level)
402 for (int i = 0; i <= level; i++)
408 if (match_byte (0x31))
411 printf("(footnote %d) ", get_u32());
412 match_byte_assert (0);
413 match_byte_assert (0);
414 int subn = get_u32 ();
415 printf ("nested %d bytes", subn);
419 match_byte_assert (0x58);
421 printf("string \"%s\"", get_string());
423 match_byte_assert (1);
429 else if (match_byte (5))
431 match_byte_assert (0x58);
432 printf ("variable \"%s\"", get_string());
434 match_byte_assert (2);
436 else if (match_byte (2))
442 match_byte_assert (0x58);
444 value = get_double ();
446 vallab = get_string ();
447 printf ("value %g format %d(%d.%d) var \"%s\" vallab \"%s\"",
448 value, format >> 16, (format >> 8) & 0xff, format & 0xff, var, vallab);
450 match_u32_assert (2);
453 else if (match_byte (1))
458 match_byte_assert (0x58);
460 value = get_double ();
461 printf ("value %g format %d(%d.%d)", value, format >> 16, (format >> 8) & 0xff, format & 0xff);
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());
480 int total_subs = get_u32();
484 total_subs = (total_subs - 1) + x;
485 match_u32_assert (0);
487 printf (" (total %d)", total_subs);
489 for (int i = 0; i < total_subs; i++)
492 dump_value (level + 1);
498 dump_category(int level)
508 else if (match_u32 (1))
515 else if (match_byte (1))
519 match_u32_assert (1);
525 match_u32_assert (0);
529 int n_categories = get_u32();
530 if (n_categories > 0)
531 printf (", %d subcategories:", n_categories);
533 for (int i = 0; i < n_categories; i++)
534 dump_category (level + 1);
541 printf("next dim\n");
546 match_byte_assert(0x58);
548 printf("string \"%s\": ", get_string());
549 match_byte(1) || match_byte(0);
551 else if (match_byte(5))
553 match_byte_assert(0x58);
554 printf("variable \"%s\": ", get_string());
557 match_byte_assert(3);
559 else if (match_byte(0x31))
564 match_u32_assert (0);
565 match_u32_assert (0);
567 printf ("nested %d bytes", subn);
569 printf ("; \"%s\", substitutions:", get_string());
572 int n_subst = get_u32();
575 printf (" %d", n_subst);
576 total_subs *= n_subst;
579 for (int i = 0; i < total_subs; i++)
589 match_byte_assert (0x58);
590 printf ("\"%s\" with substitutions:", get_string());
593 int n_subst = get_u32();
596 printf (" %d", n_subst);
597 total_subs *= n_subst;
600 for (int i = 0; i < total_subs; i++)
607 /* This byte is usually 0x02 but 0x00 and 0x75 (!) have also been spotted. */
610 if (!match_byte(0) && !match_byte(1))
611 match_byte_assert(2);
615 match_byte_assert(1);
625 n_categories = get_u32();
626 printf("%d nested categories\n", n_categories);
627 for (int i = 0; i < n_categories; i++)
634 int n_dims = get_u32();
636 printf ("%u dimensions\n", n_dims);
637 for (int i = 0; i < n_dims; i++)
645 main(int argc, char *argv[])
650 if (isatty(STDIN_FILENO))
652 fprintf(stderr, "redirect stdin from a .bin file\n");
655 if (fstat(STDIN_FILENO, &s))
667 if (read(STDIN_FILENO, data, n) != n)
675 if (!strcmp(argv[1], "title0"))
678 if (match_byte (0x03)
679 || (match_byte (0x05) && match_byte (0x58)))
680 printf ("%s\n", get_string());
682 printf ("<unknown>\n");
685 if (!strcmp(argv[1], "title"))
687 const char fonts[] = "\x01\x31\x09\0\0\0SansSerif";
689 n = find(fonts, sizeof fonts - 1);
691 else if (!strcmp(argv[1], "fonts"))
693 const char fonts[] = "\x01\x31\x09\0\0\0SansSerif";
694 const char styles[] = "\xf0\0\0\0";
695 start = find(fonts, sizeof fonts - 1);
696 n = find(styles, sizeof styles - 1);
698 else if (!strcmp(argv[1], "styles"))
700 const char styles[] = "\xf0\0\0\0";
701 const char dimensions[] = "-,,,.\0";
702 start = find(styles, sizeof styles - 1);
703 n = find(dimensions, sizeof dimensions - 1) + sizeof dimensions - 1;
705 else if (!strcmp(argv[1], "dimensions"))
708 const char dimensions[] = "-,,,.\0";
709 start = try_find_tail(dimensions, sizeof dimensions - 1);
714 const char dimensions[] = "-,,, .\0";
715 start = find_tail(dimensions, sizeof dimensions - 1);
724 fprintf (stderr, "unknown section %s\n", argv[1]);
731 for (size_t i = start; i < n; )
738 && i + 4 + data[i] <= n
739 && all_ascii(&data[i + 4], data[i]))
741 fputs("\n\"", stdout);
742 fwrite(&data[i + 4], 1, data[i], stdout);
743 fputs("\" ", stdout);
754 memcpy (&d, &data[i + 4], 8);
755 printf ("F40.%d(%.*f)\n", data[i], data[i], d);
765 memcpy (&d, &data[i + 4], 8);
766 printf ("PCT40.%d(%.*f)\n", data[i], data[i], d);
770 && (data[i] && data[i] != 88 && data[i] != 0x41)
775 printf ("i%d ", data[i]);
780 printf("%02x ", data[i]);