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 find(const char *target, size_t target_len)
24 const uint8_t *pos = (const uint8_t *) memmem (data, n, target, target_len);
27 fprintf (stderr, "not found\n");
36 #define STR(x) XSTR(x)
37 #define WHERE __FILE__":" STR(__LINE__)
43 memcpy(&x, &data[pos], 4);
52 memcpy(&x, &data[pos], 8);
67 match_u32_assert(uint32_t x, const char *where)
69 unsigned int y = get_u32();
72 fprintf(stderr, "%s: 0x%x: expected i%u, got i%u\n", where, pos - 4, x, y);
76 #define match_u32_assert(x) match_u32_assert(x, WHERE)
91 match_byte_assert(uint8_t b, const char *where)
95 fprintf(stderr, "%s: 0x%x: expected %02x, got %02x\n", where, pos, b, data[pos]);
99 #define match_byte_assert(b) match_byte_assert(b, WHERE)
104 if (data[pos + 1] == 0 && data[pos + 2] == 0 && data[pos + 3] == 0
105 && all_ascii(&data[pos + 4], data[pos]))
108 char *s = malloc(len + 1);
110 memcpy(s, &data[pos + 4], len);
112 pos += 4 + data[pos];
117 fprintf(stderr, "0x%x: expected string\n", pos);
123 dump_value(int level)
125 for (int i = 0; i <= level; i++)
131 if (match_byte (0x31))
134 printf("(footnote %d) ", get_u32());
135 match_byte_assert (0);
136 match_byte_assert (0);
137 int subn = get_u32 ();
138 printf ("nested %d bytes", subn);
142 match_byte_assert (0x58);
144 printf("string \"%s\"", get_string());
146 match_byte_assert (1);
152 else if (match_byte (5))
154 match_byte_assert (0x58);
155 printf ("variable \"%s\"", get_string());
158 match_byte_assert (2);
164 else if (match_byte (2))
170 match_byte_assert (0x58);
172 value = get_double ();
174 vallab = get_string ();
175 printf ("value %g format %d(%d.%d) var \"%s\" vallab \"%s\"",
176 value, format >> 16, (format >> 8) & 0xff, format & 0xff, var, vallab);
178 match_u32_assert (2);
181 else if (match_byte (1))
186 match_byte_assert (0x58);
188 value = get_double ();
189 printf ("value %g format %d(%d.%d)", value, format >> 16, (format >> 8) & 0xff, format & 0xff);
202 match_byte_assert (0x31);
203 match_u32_assert (0);
204 match_u32_assert (0);
206 printf ("nested %d bytes", subn);
208 printf ("; \"%s\", substitutions:", get_string());
209 fprintf (stderr, "substitutions:");
212 int n_subst = get_u32();
215 printf (" %d", n_subst);
216 fprintf (stderr, " %d", n_subst);
217 total_subs *= n_subst;
221 for (int i = 0; i < total_subs; i++)
224 dump_value (level + 1);
230 dump_category(int level)
240 else if (match_u32 (1))
249 match_u32_assert (0);
253 int n_categories = get_u32();
254 if (n_categories > 0)
255 printf (", %d subcategories:", n_categories);
257 for (int i = 0; i < n_categories; i++)
258 dump_category (level + 1);
268 match_byte_assert(0x58);
270 printf("string \"%s\": ", get_string());
271 match_byte_assert(1);
273 else if (match_byte(5))
275 match_byte_assert(0x58);
276 printf("variable \"%s\": ", get_string());
279 match_byte_assert(3);
283 fprintf(stderr, "%08x: unexpected byte\n", pos);
287 match_byte_assert(0);
288 if (!match_byte(0) && !match_byte(1))
289 match_byte_assert(2);
292 match_byte_assert(1);
302 n_categories = get_u32();
303 printf("%d nested categories\n", n_categories);
304 for (int i = 0; i < n_categories; i++)
311 int n_dims = get_u32();
313 printf ("%u dimensions\n", n_dims);
314 for (int i = 0; i < n_dims; i++)
322 main(int argc, char *argv[])
327 if (isatty(STDIN_FILENO))
329 fprintf(stderr, "redirect stdin from a .bin file\n");
332 if (fstat(STDIN_FILENO, &s))
344 if (read(STDIN_FILENO, data, n) != n)
352 if (!strcmp(argv[1], "title0"))
355 if (match_byte (0x03)
356 || (match_byte (0x05) && match_byte (0x58)))
357 printf ("%s\n", get_string());
359 printf ("<unknown>\n");
362 if (!strcmp(argv[1], "title"))
364 const char fonts[] = "\x01\x31\x09\0\0\0SansSerif";
366 n = find(fonts, sizeof fonts - 1);
368 else if (!strcmp(argv[1], "fonts"))
370 const char fonts[] = "\x01\x31\x09\0\0\0SansSerif";
371 const char styles[] = "\xf0\0\0\0";
372 start = find(fonts, sizeof fonts - 1);
373 n = find(styles, sizeof styles - 1);
375 else if (!strcmp(argv[1], "styles"))
377 const char styles[] = "\xf0\0\0\0";
378 const char dimensions[] = "-,,,.\0";
379 start = find(styles, sizeof styles - 1);
380 n = find(dimensions, sizeof dimensions - 1) + sizeof dimensions - 1;
382 else if (!strcmp(argv[1], "dimensions"))
384 const char dimensions[] = "-,,,.\0";
385 start = find(dimensions, sizeof dimensions - 1) + sizeof dimensions - 1;
392 fprintf (stderr, "unknown section %s\n", argv[1]);
399 for (size_t i = start; i < n; )
406 && i + 4 + data[i] <= n
407 && all_ascii(&data[i + 4], data[i]))
409 fputs("\n\"", stdout);
410 fwrite(&data[i + 4], 1, data[i], stdout);
411 fputs("\" ", stdout);
422 memcpy (&d, &data[i + 4], 8);
423 printf ("F40.%d(%.*f)\n", data[i], data[i], d);
433 memcpy (&d, &data[i + 4], 8);
434 printf ("PCT40.%d(%.*f)\n", data[i], data[i], d);
438 && (data[i] && data[i] != 88 && data[i] != 0x41)
443 printf ("i%d ", data[i]);
448 printf("%02x ", data[i]);