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());
195 else if (match_byte (5))
197 match_byte_assert (0x58);
198 printf ("variable \"%s\"", get_string());
201 match_byte_assert (2);
207 else if (match_byte (2))
213 match_byte_assert (0x58);
215 value = get_double ();
217 vallab = get_string ();
218 printf ("value %g format %d(%d.%d) var \"%s\" vallab \"%s\"",
219 value, format >> 16, (format >> 8) & 0xff, format & 0xff, var, vallab);
221 match_u32_assert (2);
227 else if (match_byte (4))
230 char *var, *vallab, *value;
232 match_byte_assert (0x58);
234 vallab = get_string ();
236 match_byte_assert (2);
237 value = get_string ();
238 printf ("value \"%s\" format %d(%d.%d) var \"%s\" vallab \"%s\"",
239 value, format >> 16, (format >> 8) & 0xff, format & 0xff, var, vallab);
245 else if (match_byte (1))
250 match_byte_assert (0x58);
252 value = get_double ();
253 printf ("value %g format %d(%d.%d)", value, format >> 16, (format >> 8) & 0xff, format & 0xff);
260 else if (match_byte (0x31))
265 match_u32_assert (0);
266 match_u32_assert (0);
268 printf ("nested %d bytes", subn);
270 printf ("; \"%s\", substitutions:", get_string());
273 int n_subst = get_u32();
276 printf (" %d", n_subst);
277 total_subs *= n_subst;
280 for (int i = 0; i < total_subs; i++)
283 dump_value (level + 1);
290 match_byte_assert (0x58);
291 printf ("\"%s\" with substitutions:", get_string());
294 int n_subst = get_u32();
297 printf (" %d", n_subst);
298 total_subs *= n_subst;
301 for (int i = 0; i < total_subs; i++)
304 dump_value (level + 1);
310 dump_dim_value(int level)
312 for (int i = 0; i <= level; i++)
318 if (match_byte (0x31))
321 printf("(footnote %d) ", get_u32());
322 match_byte_assert (0);
323 match_byte_assert (0);
324 int subn = get_u32 ();
325 printf ("nested %d bytes", subn);
329 match_byte_assert (0x58);
331 printf("string \"%s\"", get_string());
333 match_byte_assert (1);
339 else if (match_byte (5))
341 match_byte_assert (0x58);
342 printf ("variable \"%s\"", get_string());
344 match_byte_assert (2);
346 else if (match_byte (2))
352 match_byte_assert (0x58);
354 value = get_double ();
356 vallab = get_string ();
357 printf ("value %g format %d(%d.%d) var \"%s\" vallab \"%s\"",
358 value, format >> 16, (format >> 8) & 0xff, format & 0xff, var, vallab);
360 match_u32_assert (2);
363 else if (match_byte (1))
368 match_byte_assert (0x58);
370 value = get_double ();
371 printf ("value %g format %d(%d.%d)", value, format >> 16, (format >> 8) & 0xff, format & 0xff);
384 match_byte_assert (0x31);
385 match_u32_assert (0);
386 match_u32_assert (0);
388 printf ("nested %d bytes", subn);
390 printf ("; \"%s\", substitutions:", get_string());
393 int n_subst = get_u32();
396 printf (" %d", n_subst);
397 total_subs *= n_subst;
400 for (int i = 0; i < total_subs; i++)
403 dump_value (level + 1);
409 dump_category(int level)
419 else if (match_u32 (1))
426 else if (match_byte (1))
429 match_u32_assert (1);
435 match_u32_assert (0);
439 int n_categories = get_u32();
440 if (n_categories > 0)
441 printf (", %d subcategories:", n_categories);
443 for (int i = 0; i < n_categories; i++)
444 dump_category (level + 1);
451 printf("next dim\n");
456 match_byte_assert(0x58);
458 printf("string \"%s\": ", get_string());
459 match_byte_assert(1);
461 else if (match_byte(5))
463 match_byte_assert(0x58);
464 printf("variable \"%s\": ", get_string());
467 match_byte_assert(3);
474 match_byte_assert(0x31);
475 match_u32_assert (0);
476 match_u32_assert (0);
478 printf ("nested %d bytes", subn);
480 printf ("; \"%s\", substitutions:", get_string());
483 int n_subst = get_u32();
486 printf (" %d", n_subst);
487 total_subs *= n_subst;
490 for (int i = 0; i < total_subs; i++)
497 match_byte_assert(0);
498 if (!match_byte(0) && !match_byte(1))
499 match_byte_assert(2);
502 match_byte_assert(1);
512 n_categories = get_u32();
513 printf("%d nested categories\n", n_categories);
514 for (int i = 0; i < n_categories; i++)
521 int n_dims = get_u32();
523 printf ("%u dimensions\n", n_dims);
524 for (int i = 0; i < n_dims; i++)
532 main(int argc, char *argv[])
537 if (isatty(STDIN_FILENO))
539 fprintf(stderr, "redirect stdin from a .bin file\n");
542 if (fstat(STDIN_FILENO, &s))
554 if (read(STDIN_FILENO, data, n) != n)
562 if (!strcmp(argv[1], "title0"))
565 if (match_byte (0x03)
566 || (match_byte (0x05) && match_byte (0x58)))
567 printf ("%s\n", get_string());
569 printf ("<unknown>\n");
572 if (!strcmp(argv[1], "title"))
574 const char fonts[] = "\x01\x31\x09\0\0\0SansSerif";
576 n = find(fonts, sizeof fonts - 1);
578 else if (!strcmp(argv[1], "fonts"))
580 const char fonts[] = "\x01\x31\x09\0\0\0SansSerif";
581 const char styles[] = "\xf0\0\0\0";
582 start = find(fonts, sizeof fonts - 1);
583 n = find(styles, sizeof styles - 1);
585 else if (!strcmp(argv[1], "styles"))
587 const char styles[] = "\xf0\0\0\0";
588 const char dimensions[] = "-,,,.\0";
589 start = find(styles, sizeof styles - 1);
590 n = find(dimensions, sizeof dimensions - 1) + sizeof dimensions - 1;
592 else if (!strcmp(argv[1], "dimensions"))
595 const char dimensions[] = "-,,,.\0";
596 start = try_find_tail(dimensions, sizeof dimensions - 1);
601 const char dimensions[] = "-,,, .\0";
602 start = find_tail(dimensions, sizeof dimensions - 1);
611 fprintf (stderr, "unknown section %s\n", argv[1]);
618 for (size_t i = start; i < n; )
625 && i + 4 + data[i] <= n
626 && all_ascii(&data[i + 4], data[i]))
628 fputs("\n\"", stdout);
629 fwrite(&data[i + 4], 1, data[i], stdout);
630 fputs("\" ", stdout);
641 memcpy (&d, &data[i + 4], 8);
642 printf ("F40.%d(%.*f)\n", data[i], data[i], d);
652 memcpy (&d, &data[i + 4], 8);
653 printf ("PCT40.%d(%.*f)\n", data[i], data[i], d);
657 && (data[i] && data[i] != 88 && data[i] != 0x41)
662 printf ("i%d ", data[i]);
667 printf("%02x ", data[i]);