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_category(int level)
263 else if (match_u32 (1))
272 match_u32_assert (0);
276 int n_categories = get_u32();
277 if (n_categories > 0)
278 printf (", %d subcategories:", n_categories);
280 for (int i = 0; i < n_categories; i++)
281 dump_category (level + 1);
291 match_byte_assert(0x58);
293 printf("string \"%s\": ", get_string());
294 match_byte_assert(1);
296 else if (match_byte(5))
298 match_byte_assert(0x58);
299 printf("variable \"%s\": ", get_string());
302 match_byte_assert(3);
306 fprintf(stderr, "%08x: unexpected byte\n", pos);
310 match_byte_assert(0);
311 if (!match_byte(0) && !match_byte(1))
312 match_byte_assert(2);
315 match_byte_assert(1);
325 n_categories = get_u32();
326 printf("%d nested categories\n", n_categories);
327 for (int i = 0; i < n_categories; i++)
334 int n_dims = get_u32();
336 printf ("%u dimensions\n", n_dims);
337 for (int i = 0; i < n_dims; i++)
345 main(int argc, char *argv[])
350 if (isatty(STDIN_FILENO))
352 fprintf(stderr, "redirect stdin from a .bin file\n");
355 if (fstat(STDIN_FILENO, &s))
367 if (read(STDIN_FILENO, data, n) != n)
375 if (!strcmp(argv[1], "title0"))
378 if (match_byte (0x03)
379 || (match_byte (0x05) && match_byte (0x58)))
380 printf ("%s\n", get_string());
382 printf ("<unknown>\n");
385 if (!strcmp(argv[1], "title"))
387 const char fonts[] = "\x01\x31\x09\0\0\0SansSerif";
389 n = find(fonts, sizeof fonts - 1);
391 else if (!strcmp(argv[1], "fonts"))
393 const char fonts[] = "\x01\x31\x09\0\0\0SansSerif";
394 const char styles[] = "\xf0\0\0\0";
395 start = find(fonts, sizeof fonts - 1);
396 n = find(styles, sizeof styles - 1);
398 else if (!strcmp(argv[1], "styles"))
400 const char styles[] = "\xf0\0\0\0";
401 const char dimensions[] = "-,,,.\0";
402 start = find(styles, sizeof styles - 1);
403 n = find(dimensions, sizeof dimensions - 1) + sizeof dimensions - 1;
405 else if (!strcmp(argv[1], "dimensions"))
408 const char dimensions[] = "-,,,.\0";
409 start = try_find_tail(dimensions, sizeof dimensions - 1);
414 const char dimensions[] = "-,,, .\0";
415 start = find_tail(dimensions, sizeof dimensions - 1);
424 fprintf (stderr, "unknown section %s\n", argv[1]);
431 for (size_t i = start; i < n; )
438 && i + 4 + data[i] <= n
439 && all_ascii(&data[i + 4], data[i]))
441 fputs("\n\"", stdout);
442 fwrite(&data[i + 4], 1, data[i], stdout);
443 fputs("\" ", stdout);
454 memcpy (&d, &data[i + 4], 8);
455 printf ("F40.%d(%.*f)\n", data[i], data[i], d);
465 memcpy (&d, &data[i + 4], 8);
466 printf ("PCT40.%d(%.*f)\n", data[i], data[i], d);
470 && (data[i] && data[i] != 88 && data[i] != 0x41)
475 printf ("i%d ", data[i]);
480 printf("%02x ", data[i]);