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++)
158 if (match_byte (0x31))
161 printf("(footnote %d) ", get_u32());
162 match_byte_assert (0);
163 match_byte_assert (0);
164 int subn = get_u32 ();
165 printf ("nested %d bytes", subn);
169 match_byte_assert (0x58);
171 printf("string \"%s\"", get_string());
173 match_byte_assert (1);
179 else if (match_byte (5))
181 match_byte_assert (0x58);
182 printf ("variable \"%s\"", get_string());
185 match_byte_assert (2);
191 else if (match_byte (2))
197 match_byte_assert (0x58);
199 value = get_double ();
201 vallab = get_string ();
202 printf ("value %g format %d(%d.%d) var \"%s\" vallab \"%s\"",
203 value, format >> 16, (format >> 8) & 0xff, format & 0xff, var, vallab);
205 match_u32_assert (2);
211 else if (match_byte (4))
214 char *var, *vallab, *value;
216 match_byte_assert (0x58);
218 vallab = get_string ();
220 match_byte_assert (2);
221 value = get_string ();
222 printf ("value \"%s\" format %d(%d.%d) var \"%s\" vallab \"%s\"",
223 value, format >> 16, (format >> 8) & 0xff, format & 0xff, var, vallab);
229 else if (match_byte (1))
234 match_byte_assert (0x58);
236 value = get_double ();
237 printf ("value %g format %d(%d.%d)", value, format >> 16, (format >> 8) & 0xff, format & 0xff);
250 match_byte_assert (0x31);
251 match_u32_assert (0);
252 match_u32_assert (0);
254 printf ("nested %d bytes", subn);
256 printf ("; \"%s\", substitutions:", get_string());
259 int n_subst = get_u32();
262 printf (" %d", n_subst);
263 total_subs *= n_subst;
266 for (int i = 0; i < total_subs; i++)
269 dump_value (level + 1);
275 dump_dim_value(int level)
277 for (int i = 0; i <= level; i++)
283 if (match_byte (0x31))
286 printf("(footnote %d) ", get_u32());
287 match_byte_assert (0);
288 match_byte_assert (0);
289 int subn = get_u32 ();
290 printf ("nested %d bytes", subn);
294 match_byte_assert (0x58);
296 printf("string \"%s\"", get_string());
298 match_byte_assert (1);
304 else if (match_byte (5))
306 match_byte_assert (0x58);
307 printf ("variable \"%s\"", get_string());
309 match_byte_assert (2);
311 else if (match_byte (2))
317 match_byte_assert (0x58);
319 value = get_double ();
321 vallab = get_string ();
322 printf ("value %g format %d(%d.%d) var \"%s\" vallab \"%s\"",
323 value, format >> 16, (format >> 8) & 0xff, format & 0xff, var, vallab);
325 match_u32_assert (2);
328 else if (match_byte (1))
333 match_byte_assert (0x58);
335 value = get_double ();
336 printf ("value %g format %d(%d.%d)", value, format >> 16, (format >> 8) & 0xff, format & 0xff);
349 match_byte_assert (0x31);
350 match_u32_assert (0);
351 match_u32_assert (0);
353 printf ("nested %d bytes", subn);
355 printf ("; \"%s\", substitutions:", get_string());
358 int n_subst = get_u32();
361 printf (" %d", n_subst);
362 total_subs *= n_subst;
365 for (int i = 0; i < total_subs; i++)
368 dump_value (level + 1);
374 dump_category(int level)
384 else if (match_u32 (1))
391 else if (match_byte (1))
394 match_u32_assert (1);
400 match_u32_assert (0);
404 int n_categories = get_u32();
405 if (n_categories > 0)
406 printf (", %d subcategories:", n_categories);
408 for (int i = 0; i < n_categories; i++)
409 dump_category (level + 1);
416 printf("next dim\n");
421 match_byte_assert(0x58);
423 printf("string \"%s\": ", get_string());
424 match_byte_assert(1);
426 else if (match_byte(5))
428 match_byte_assert(0x58);
429 printf("variable \"%s\": ", get_string());
432 match_byte_assert(3);
439 match_byte_assert(0x31);
440 match_u32_assert (0);
441 match_u32_assert (0);
443 printf ("nested %d bytes", subn);
445 printf ("; \"%s\", substitutions:", get_string());
448 int n_subst = get_u32();
451 printf (" %d", n_subst);
452 total_subs *= n_subst;
455 for (int i = 0; i < total_subs; i++)
462 match_byte_assert(0);
463 if (!match_byte(0) && !match_byte(1))
464 match_byte_assert(2);
467 match_byte_assert(1);
477 n_categories = get_u32();
478 printf("%d nested categories\n", n_categories);
479 for (int i = 0; i < n_categories; i++)
486 int n_dims = get_u32();
488 printf ("%u dimensions\n", n_dims);
489 for (int i = 0; i < n_dims; i++)
497 main(int argc, char *argv[])
502 if (isatty(STDIN_FILENO))
504 fprintf(stderr, "redirect stdin from a .bin file\n");
507 if (fstat(STDIN_FILENO, &s))
519 if (read(STDIN_FILENO, data, n) != n)
527 if (!strcmp(argv[1], "title0"))
530 if (match_byte (0x03)
531 || (match_byte (0x05) && match_byte (0x58)))
532 printf ("%s\n", get_string());
534 printf ("<unknown>\n");
537 if (!strcmp(argv[1], "title"))
539 const char fonts[] = "\x01\x31\x09\0\0\0SansSerif";
541 n = find(fonts, sizeof fonts - 1);
543 else if (!strcmp(argv[1], "fonts"))
545 const char fonts[] = "\x01\x31\x09\0\0\0SansSerif";
546 const char styles[] = "\xf0\0\0\0";
547 start = find(fonts, sizeof fonts - 1);
548 n = find(styles, sizeof styles - 1);
550 else if (!strcmp(argv[1], "styles"))
552 const char styles[] = "\xf0\0\0\0";
553 const char dimensions[] = "-,,,.\0";
554 start = find(styles, sizeof styles - 1);
555 n = find(dimensions, sizeof dimensions - 1) + sizeof dimensions - 1;
557 else if (!strcmp(argv[1], "dimensions"))
560 const char dimensions[] = "-,,,.\0";
561 start = try_find_tail(dimensions, sizeof dimensions - 1);
566 const char dimensions[] = "-,,, .\0";
567 start = find_tail(dimensions, sizeof dimensions - 1);
576 fprintf (stderr, "unknown section %s\n", argv[1]);
583 for (size_t i = start; i < n; )
590 && i + 4 + data[i] <= n
591 && all_ascii(&data[i + 4], data[i]))
593 fputs("\n\"", stdout);
594 fwrite(&data[i + 4], 1, data[i], stdout);
595 fputs("\" ", stdout);
606 memcpy (&d, &data[i + 4], 8);
607 printf ("F40.%d(%.*f)\n", data[i], data[i], d);
617 memcpy (&d, &data[i + 4], 8);
618 printf ("PCT40.%d(%.*f)\n", data[i], data[i], d);
622 && (data[i] && data[i] != 88 && data[i] != 0x41)
627 printf ("i%d ", data[i]);
632 printf("%02x ", data[i]);