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);
166 int subn = get_u32 ();
167 printf ("nested %d bytes", subn);
170 else if (match_u32 (2))
172 printf("(special 2)");
173 match_byte_assert(0);
174 match_byte_assert(0);
176 match_byte_assert(0);
177 match_byte_assert(0);
178 int subn = get_u32 ();
179 printf ("nested %d bytes", subn);
185 printf("(special 3)");
186 match_byte_assert(0);
187 match_byte_assert(0);
188 match_byte_assert(1);
189 match_byte_assert(0);
190 int subn = get_u32 ();
191 printf ("nested %d bytes, ", subn);
194 printf ("nested %d bytes, ", subn);
199 match_byte_assert (0x58);
201 printf("string \"%s\"", get_string());
212 else if (match_byte (5))
214 match_byte_assert (0x58);
215 printf ("variable \"%s\"", get_string());
218 match_byte_assert (2);
224 else if (match_byte (2))
230 match_byte_assert (0x58);
232 value = get_double ();
234 vallab = get_string ();
235 printf ("value %g format %d(%d.%d) var \"%s\" vallab \"%s\"",
236 value, format >> 16, (format >> 8) & 0xff, format & 0xff, var, vallab);
238 match_u32_assert (2);
244 else if (match_byte (4))
247 char *var, *vallab, *value;
249 match_byte_assert (0x58);
251 vallab = get_string ();
253 match_byte_assert (2);
254 value = get_string ();
255 printf ("value \"%s\" format %d(%d.%d) var \"%s\" vallab \"%s\"",
256 value, format >> 16, (format >> 8) & 0xff, format & 0xff, var, vallab);
262 else if (match_byte (1))
267 match_byte_assert (0x58);
269 value = get_double ();
270 printf ("value %g format %d(%d.%d)", value, format >> 16, (format >> 8) & 0xff, format & 0xff);
277 else if (match_byte (0x31))
282 match_u32_assert (0);
283 match_u32_assert (0);
285 printf ("nested %d bytes", subn);
287 printf ("; \"%s\", substitutions:", get_string());
290 int n_subst = get_u32();
293 printf (" %d", n_subst);
294 total_subs *= n_subst;
297 for (int i = 0; i < total_subs; i++)
300 dump_value (level + 1);
307 match_byte_assert (0x58);
308 printf ("\"%s\" with substitutions:", get_string());
311 int n_subst = get_u32();
314 printf (" %d", n_subst);
315 total_subs *= n_subst;
318 for (int i = 0; i < total_subs; i++)
321 dump_value (level + 1);
327 dump_dim_value(int level)
329 for (int i = 0; i <= level; i++)
335 if (match_byte (0x31))
338 printf("(footnote %d) ", get_u32());
339 match_byte_assert (0);
340 match_byte_assert (0);
341 int subn = get_u32 ();
342 printf ("nested %d bytes", subn);
346 match_byte_assert (0x58);
348 printf("string \"%s\"", get_string());
350 match_byte_assert (1);
356 else if (match_byte (5))
358 match_byte_assert (0x58);
359 printf ("variable \"%s\"", get_string());
361 match_byte_assert (2);
363 else if (match_byte (2))
369 match_byte_assert (0x58);
371 value = get_double ();
373 vallab = get_string ();
374 printf ("value %g format %d(%d.%d) var \"%s\" vallab \"%s\"",
375 value, format >> 16, (format >> 8) & 0xff, format & 0xff, var, vallab);
377 match_u32_assert (2);
380 else if (match_byte (1))
385 match_byte_assert (0x58);
387 value = get_double ();
388 printf ("value %g format %d(%d.%d)", value, format >> 16, (format >> 8) & 0xff, format & 0xff);
401 match_byte_assert (0x31);
402 match_u32_assert (0);
403 match_u32_assert (0);
405 printf ("nested %d bytes", subn);
407 printf ("; \"%s\", substitutions:", get_string());
410 int n_subst = get_u32();
413 printf (" %d", n_subst);
414 total_subs *= n_subst;
417 for (int i = 0; i < total_subs; i++)
420 dump_value (level + 1);
426 dump_category(int level)
436 else if (match_u32 (1))
443 else if (match_byte (1))
446 match_u32_assert (1);
452 match_u32_assert (0);
456 int n_categories = get_u32();
457 if (n_categories > 0)
458 printf (", %d subcategories:", n_categories);
460 for (int i = 0; i < n_categories; i++)
461 dump_category (level + 1);
468 printf("next dim\n");
473 match_byte_assert(0x58);
475 printf("string \"%s\": ", get_string());
476 match_byte_assert(1);
478 else if (match_byte(5))
480 match_byte_assert(0x58);
481 printf("variable \"%s\": ", get_string());
484 match_byte_assert(3);
491 match_byte_assert(0x31);
492 match_u32_assert (0);
493 match_u32_assert (0);
495 printf ("nested %d bytes", subn);
497 printf ("; \"%s\", substitutions:", get_string());
500 int n_subst = get_u32();
503 printf (" %d", n_subst);
504 total_subs *= n_subst;
507 for (int i = 0; i < total_subs; i++)
514 match_byte_assert(0);
515 if (!match_byte(0) && !match_byte(1))
516 match_byte_assert(2);
519 match_byte_assert(1);
529 n_categories = get_u32();
530 printf("%d nested categories\n", n_categories);
531 for (int i = 0; i < n_categories; i++)
538 int n_dims = get_u32();
540 printf ("%u dimensions\n", n_dims);
541 for (int i = 0; i < n_dims; i++)
549 main(int argc, char *argv[])
554 if (isatty(STDIN_FILENO))
556 fprintf(stderr, "redirect stdin from a .bin file\n");
559 if (fstat(STDIN_FILENO, &s))
571 if (read(STDIN_FILENO, data, n) != n)
579 if (!strcmp(argv[1], "title0"))
582 if (match_byte (0x03)
583 || (match_byte (0x05) && match_byte (0x58)))
584 printf ("%s\n", get_string());
586 printf ("<unknown>\n");
589 if (!strcmp(argv[1], "title"))
591 const char fonts[] = "\x01\x31\x09\0\0\0SansSerif";
593 n = find(fonts, sizeof fonts - 1);
595 else if (!strcmp(argv[1], "fonts"))
597 const char fonts[] = "\x01\x31\x09\0\0\0SansSerif";
598 const char styles[] = "\xf0\0\0\0";
599 start = find(fonts, sizeof fonts - 1);
600 n = find(styles, sizeof styles - 1);
602 else if (!strcmp(argv[1], "styles"))
604 const char styles[] = "\xf0\0\0\0";
605 const char dimensions[] = "-,,,.\0";
606 start = find(styles, sizeof styles - 1);
607 n = find(dimensions, sizeof dimensions - 1) + sizeof dimensions - 1;
609 else if (!strcmp(argv[1], "dimensions"))
612 const char dimensions[] = "-,,,.\0";
613 start = try_find_tail(dimensions, sizeof dimensions - 1);
618 const char dimensions[] = "-,,, .\0";
619 start = find_tail(dimensions, sizeof dimensions - 1);
628 fprintf (stderr, "unknown section %s\n", argv[1]);
635 for (size_t i = start; i < n; )
642 && i + 4 + data[i] <= n
643 && all_ascii(&data[i + 4], data[i]))
645 fputs("\n\"", stdout);
646 fwrite(&data[i + 4], 1, data[i], stdout);
647 fputs("\" ", stdout);
658 memcpy (&d, &data[i + 4], 8);
659 printf ("F40.%d(%.*f)\n", data[i], data[i], d);
669 memcpy (&d, &data[i + 4], 8);
670 printf ("PCT40.%d(%.*f)\n", data[i], data[i], d);
674 && (data[i] && data[i] != 88 && data[i] != 0x41)
679 printf ("i%d ", data[i]);
684 printf("%02x ", data[i]);