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)
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, "0x%x: expected string\n", pos);
149 dump_value(int level)
151 for (int i = 0; i <= level; i++)
157 if (match_byte (0x31))
160 printf("(footnote %d) ", get_u32());
161 match_byte_assert (0);
162 match_byte_assert (0);
163 int subn = get_u32 ();
164 printf ("nested %d bytes", subn);
168 match_byte_assert (0x58);
170 printf("string \"%s\"", get_string());
172 match_byte_assert (1);
178 else if (match_byte (5))
180 match_byte_assert (0x58);
181 printf ("variable \"%s\"", get_string());
184 match_byte_assert (2);
190 else if (match_byte (2))
196 match_byte_assert (0x58);
198 value = get_double ();
200 vallab = get_string ();
201 printf ("value %g format %d(%d.%d) var \"%s\" vallab \"%s\"",
202 value, format >> 16, (format >> 8) & 0xff, format & 0xff, var, vallab);
204 match_u32_assert (2);
207 else if (match_byte (1))
212 match_byte_assert (0x58);
214 value = get_double ();
215 printf ("value %g format %d(%d.%d)", value, format >> 16, (format >> 8) & 0xff, format & 0xff);
228 match_byte_assert (0x31);
229 match_u32_assert (0);
230 match_u32_assert (0);
232 printf ("nested %d bytes", subn);
234 printf ("; \"%s\", substitutions:", get_string());
237 int n_subst = get_u32();
240 printf (" %d", n_subst);
241 total_subs *= n_subst;
244 for (int i = 0; i < total_subs; i++)
247 dump_value (level + 1);
253 dump_dim_value(int level)
255 for (int i = 0; i <= level; i++)
261 if (match_byte (0x31))
264 printf("(footnote %d) ", get_u32());
265 match_byte_assert (0);
266 match_byte_assert (0);
267 int subn = get_u32 ();
268 printf ("nested %d bytes", subn);
272 match_byte_assert (0x58);
274 printf("string \"%s\"", get_string());
276 match_byte_assert (1);
282 else if (match_byte (5))
284 match_byte_assert (0x58);
285 printf ("variable \"%s\"", get_string());
287 match_byte_assert (2);
289 else if (match_byte (2))
295 match_byte_assert (0x58);
297 value = get_double ();
299 vallab = get_string ();
300 printf ("value %g format %d(%d.%d) var \"%s\" vallab \"%s\"",
301 value, format >> 16, (format >> 8) & 0xff, format & 0xff, var, vallab);
303 match_u32_assert (2);
306 else if (match_byte (1))
311 match_byte_assert (0x58);
313 value = get_double ();
314 printf ("value %g format %d(%d.%d)", value, format >> 16, (format >> 8) & 0xff, format & 0xff);
327 match_byte_assert (0x31);
328 match_u32_assert (0);
329 match_u32_assert (0);
331 printf ("nested %d bytes", subn);
333 printf ("; \"%s\", substitutions:", get_string());
336 int n_subst = get_u32();
339 printf (" %d", n_subst);
340 total_subs *= n_subst;
343 for (int i = 0; i < total_subs; i++)
346 dump_value (level + 1);
352 dump_category(int level)
362 else if (match_u32 (1))
371 match_u32_assert (0);
375 int n_categories = get_u32();
376 if (n_categories > 0)
377 printf (", %d subcategories:", n_categories);
379 for (int i = 0; i < n_categories; i++)
380 dump_category (level + 1);
387 printf("next dim\n");
392 match_byte_assert(0x58);
394 printf("string \"%s\": ", get_string());
395 match_byte_assert(1);
397 else if (match_byte(5))
399 match_byte_assert(0x58);
400 printf("variable \"%s\": ", get_string());
403 match_byte_assert(3);
410 match_byte_assert(0x31);
411 match_u32_assert (0);
412 match_u32_assert (0);
414 printf ("nested %d bytes", subn);
416 printf ("; \"%s\", substitutions:", get_string());
419 int n_subst = get_u32();
422 printf (" %d", n_subst);
423 total_subs *= n_subst;
426 for (int i = 0; i < total_subs; i++)
433 match_byte_assert(0);
434 if (!match_byte(0) && !match_byte(1))
435 match_byte_assert(2);
438 match_byte_assert(1);
448 n_categories = get_u32();
449 printf("%d nested categories\n", n_categories);
450 for (int i = 0; i < n_categories; i++)
457 int n_dims = get_u32();
459 printf ("%u dimensions\n", n_dims);
460 for (int i = 0; i < n_dims; i++)
468 main(int argc, char *argv[])
473 if (isatty(STDIN_FILENO))
475 fprintf(stderr, "redirect stdin from a .bin file\n");
478 if (fstat(STDIN_FILENO, &s))
490 if (read(STDIN_FILENO, data, n) != n)
498 if (!strcmp(argv[1], "title0"))
501 if (match_byte (0x03)
502 || (match_byte (0x05) && match_byte (0x58)))
503 printf ("%s\n", get_string());
505 printf ("<unknown>\n");
508 if (!strcmp(argv[1], "title"))
510 const char fonts[] = "\x01\x31\x09\0\0\0SansSerif";
512 n = find(fonts, sizeof fonts - 1);
514 else if (!strcmp(argv[1], "fonts"))
516 const char fonts[] = "\x01\x31\x09\0\0\0SansSerif";
517 const char styles[] = "\xf0\0\0\0";
518 start = find(fonts, sizeof fonts - 1);
519 n = find(styles, sizeof styles - 1);
521 else if (!strcmp(argv[1], "styles"))
523 const char styles[] = "\xf0\0\0\0";
524 const char dimensions[] = "-,,,.\0";
525 start = find(styles, sizeof styles - 1);
526 n = find(dimensions, sizeof dimensions - 1) + sizeof dimensions - 1;
528 else if (!strcmp(argv[1], "dimensions"))
531 const char dimensions[] = "-,,,.\0";
532 start = try_find_tail(dimensions, sizeof dimensions - 1);
537 const char dimensions[] = "-,,, .\0";
538 start = find_tail(dimensions, sizeof dimensions - 1);
547 fprintf (stderr, "unknown section %s\n", argv[1]);
554 for (size_t i = start; i < n; )
561 && i + 4 + data[i] <= n
562 && all_ascii(&data[i + 4], data[i]))
564 fputs("\n\"", stdout);
565 fwrite(&data[i + 4], 1, data[i], stdout);
566 fputs("\" ", stdout);
577 memcpy (&d, &data[i + 4], 8);
578 printf ("F40.%d(%.*f)\n", data[i], data[i], d);
588 memcpy (&d, &data[i + 4], 8);
589 printf ("PCT40.%d(%.*f)\n", data[i], data[i], d);
593 && (data[i] && data[i] != 88 && data[i] != 0x41)
598 printf ("i%d ", data[i]);
603 printf("%02x ", data[i]);