14 all_ascii(const uint8_t *p, size_t n)
16 for (size_t i = 0; i < n; i++)
17 if (p[i] < 32 || p[i] > 126)
23 try_find(const char *target, size_t target_len)
25 const uint8_t *pos = (const uint8_t *) memmem (data, n, target, target_len);
26 return pos ? pos - data : 0;
30 find(const char *target, size_t target_len)
32 size_t pos = try_find(target, target_len);
35 fprintf (stderr, "not found\n");
44 #define STR(x) XSTR(x)
45 #define WHERE __FILE__":" STR(__LINE__)
51 memcpy(&x, &data[pos], 4);
60 memcpy(&x, &data[pos], 8);
75 match_u32_assert(uint32_t x, const char *where)
77 unsigned int y = get_u32();
80 fprintf(stderr, "%s: 0x%x: expected i%u, got i%u\n", where, pos - 4, x, y);
84 #define match_u32_assert(x) match_u32_assert(x, WHERE)
89 if (pos < n && data[pos] == b)
99 match_byte_assert(uint8_t b, const char *where)
103 fprintf(stderr, "%s: 0x%x: expected %02x, got %02x\n", where, pos, b, data[pos]);
107 #define match_byte_assert(b) match_byte_assert(b, WHERE)
110 newline(FILE *stream, int pos)
112 fprintf(stream, "\n%08x: ", pos);
116 dump_raw(FILE *stream, int start, int end)
118 for (size_t i = start; i < end; )
125 && i + 4 + data[i] + data[i + 1] * 256 <= end
126 && all_ascii(&data[i + 4], data[i] + data[i + 1] * 256))
129 fprintf(stream, "\"");
130 fwrite(&data[i + 4], 1, data[i] + data[i + 1] * 256, stream);
131 fputs("\" ", stream);
133 i += 4 + data[i] + data[i + 1] * 256;
135 else if (i + 12 <= end
142 memcpy (&d, &data[i + 4], 8);
143 fprintf (stream, "F40.%d(%.*f)", data[i], data[i], d);
147 else if (i + 12 <= end
154 memcpy (&d, &data[i + 4], 8);
155 fprintf (stream, "PCT40.%d(%.*f)", data[i], data[i], d);
159 else if (i + 4 <= end
160 && (data[i] && data[i] != 88 && data[i] != 0x41)
165 fprintf (stream, "i%d ", data[i]);
170 fprintf(stream, "%02x ", data[i]);
179 get_string(const char *where)
182 /*data[pos + 1] == 0 && data[pos + 2] == 0 && data[pos + 3] == 0*/
183 /*&& all_ascii(&data[pos + 4], data[pos])*/)
185 int len = data[pos] + data[pos + 1] * 256;
186 char *s = malloc(len + 1);
188 memcpy(s, &data[pos + 4], len);
195 fprintf(stderr, "%s: 0x%x: expected string\n", where, pos);
199 #define get_string() get_string(WHERE)
202 dump_nested_string(void)
206 match_byte_assert (0);
207 match_byte_assert (0);
208 int outer_end = pos + get_u32();
209 int inner_end = pos + get_u32();
210 if (pos != inner_end)
213 if (match_byte(0x31))
216 match_byte_assert(0x58);
217 if (pos != inner_end)
219 fprintf(stderr, "inner end discrepancy\n");
223 match_byte_assert(0x58);
224 match_byte_assert(0x58);
225 if (pos != outer_end)
227 fprintf(stderr, "outer end discrepancy\n");
235 dump_value_31(FILE *stream)
237 if (match_byte (0x31))
243 /* Only "a" observed as a sample value (although it appears 44 times in the corpus). */
247 match_u32_assert (0);
249 int outer_end = pos + get_u32();
250 int inner_end = pos + get_u32();
252 if (match_byte(0x31))
254 /* Appears to be a template string, e.g. '^1 cells (^2) expf < 5. Min exp = ^3...'.
255 Probably doesn't actually appear in output because many examples look unpolished,
256 e.g. 'partial list cases value ^1 shown upper...' */
260 match_byte_assert(0x58);
261 if (pos != inner_end)
263 fprintf(stderr, "inner end discrepancy\n");
267 if (match_byte(0x31))
269 /* Only one example in the corpus. */
273 match_byte_assert(1);
274 get_string(); /* foreground */
275 get_string(); /* background */
276 get_string(); /* font */
277 match_byte_assert(12); /* size? */
280 match_byte_assert(0x58);
281 match_byte_assert(0x58);
282 if (pos != outer_end)
284 fprintf(stderr, "outer end discrepancy\n");
288 else if (match_u32 (1))
290 fprintf(stream, "(footnote %d) ", get_u32());
291 dump_nested_string();
293 else if (match_u32 (2))
295 fprintf(stream, "(special 2)");
296 match_byte_assert(0);
297 match_byte_assert(0);
300 dump_nested_string(); /* Our corpus doesn't contain any examples with strings though. */
305 fprintf(stream, "(special 3)");
306 match_byte_assert(0);
307 match_byte_assert(0);
308 match_byte_assert(1);
309 match_byte_assert(0);
311 dump_nested_string(); /* Our corpus doesn't contain any examples with strings though. */
315 match_byte_assert (0x58);
319 format_to_string (int type)
325 case 2: return "AHEX";
326 case 3: return "COMMA";
327 case 4: return "DOLLAR";
328 case 5: case 40: return "F";
330 case 7: return "PIBHEX";
332 case 9: return "PIB";
333 case 10: return "PK";
334 case 11: return "RB";
335 case 12: return "RBHEX";
339 case 20: return "DATE";
340 case 21: return "TIME";
341 case 22: return "DATETIME";
342 case 23: return "ADATE";
343 case 24: return "JDATE";
344 case 25: return "DTIME";
345 case 26: return "WKDAY";
346 case 27: return "MONTH";
347 case 28: return "MOYR";
348 case 29: return "QYR";
349 case 30: return "WKYR";
350 case 31: return "PCT";
351 case 32: return "DOT";
352 case 33: return "CCA";
353 case 34: return "CCB";
354 case 35: return "CCC";
355 case 36: return "CCD";
356 case 37: return "CCE";
357 case 38: return "EDATE";
358 case 39: return "SDATE";
361 sprintf(tmp, "<%d>", type);
367 dump_value(FILE *stream, int level, bool match1)
374 for (int i = 0; i <= level; i++)
375 fprintf (stream, " ");
379 char *text = get_string();
380 dump_value_31(stream);
381 char *identifier = get_string();
382 char *text_eng = get_string();
383 fprintf (stream, "<string c=\"%s\"", text_eng);
385 fprintf (stream, " identifier=\"%s\"", identifier);
386 if (strcmp(text_eng, text))
387 fprintf (stream, " local=\"%s\"", text);
388 fprintf (stream, "/>\n");
390 match_byte_assert(1);
394 else if (match_byte (5))
396 dump_value_31(stream);
397 char *name = get_string ();
398 char *label = get_string ();
399 fprintf (stream, "<variable name=\"%s\"", name);
401 fprintf (stream, " label=\"%s\"", label);
402 fprintf (stream, "/>\n");
403 if (!match_byte(1) && !match_byte(2))
404 match_byte_assert(3);
406 else if (match_byte (2))
412 match_byte_assert (0x58);
414 value = get_double ();
416 vallab = get_string ();
417 fprintf (stream, "<numeric-datum value=\"%.*g\" format=\"%s%d.%d\"",
418 DBL_DIG, value, format_to_string(format >> 16), (format >> 8) & 0xff, format & 0xff);
420 fprintf (stream, " variable=\"%s\"", var);
422 fprintf (stream, " label=\"%s\"/>\n", vallab);
423 fprintf (stream, "/>\n");
424 if (!match_byte (1) && !match_byte(2))
425 match_byte_assert (3);
427 else if (match_byte (4))
430 char *var, *vallab, *value;
432 match_byte_assert (0x58);
434 vallab = get_string ();
436 if (!match_byte(1) && !match_byte(2))
437 match_byte_assert (3);
438 value = get_string ();
439 fprintf (stream, "<string-datum value=\"%s\" format=\"%s%d.%d\"",
440 value, format_to_string(format >> 16), (format >> 8) & 0xff, format & 0xff);
442 fprintf (stream, " variable=\"%s\"", var);
444 fprintf (stream, " label=\"%s\"/>\n", vallab);
445 fprintf (stream, "/>\n");
447 else if (match_byte (1))
452 dump_value_31(stream);
454 value = get_double ();
455 fprintf (stream, "<number value=\"%.*g\" format=\"%s%d.%d\"/>\n",
456 DBL_DIG, value, format_to_string(format >> 16), (format >> 8) & 0xff, format & 0xff);
462 dump_value_31(stream);
464 char *base = get_string();
466 fprintf (stream, "<template format=\"%s\">\n", base);
467 for (int i = 0; i < x; i++)
474 for (int j = 0; j <= level + 1; j++)
475 fprintf (stream, " ");
476 fprintf (stream, "<substitution index=\"%d\">\n", i + 1);
477 for (int j = 0; j < y; j++)
478 dump_value (stream, level + 2, false);
479 for (int j = 0; j <= level + 1; j++)
480 fprintf (stream, " ");
481 fprintf (stream, "</substitution>\n");
483 for (int j = 0; j <= level; j++)
484 fprintf (stream, " ");
485 fprintf (stream, "</template>\n");
490 compare_int(const void *a_, const void *b_)
494 return *a < *b ? -1 : *a > *b;
498 check_permutation(int *a, int n, const char *name)
501 memcpy(b, a, n * sizeof *a);
502 qsort(b, n, sizeof *b, compare_int);
503 for (int i = 0; i < n; i++)
506 fprintf(stderr, "bad %s permutation:", name);
507 for (int i = 0; i < n; i++)
508 fprintf(stderr, " %d", a[i]);
515 dump_category(int level, int *indexes, int *n_indexes)
517 for (int i = 0; i <= level; i++)
518 fprintf (stdout, " ");
519 printf ("<category>\n");
520 dump_value (stdout, level + 1, true);
527 else if (match_byte (1))
531 match_u32_assert (1);
534 else if (!match_u32(2))
535 match_u32_assert (0);
537 int indx = get_u32();
538 int n_categories = get_u32();
541 if (n_categories != 0)
543 fprintf(stderr, "index not -1 but subcategories\n");
546 indexes[(*n_indexes)++] = indx;
548 if (n_categories == 0)
550 for (int i = 0; i <= level + 1; i++)
551 fprintf (stdout, " ");
552 fprintf (stdout, "<category-index>%d</category-index>\n", indx);
554 for (int i = 0; i < n_categories; i++)
555 dump_category (level + 1, indexes, n_indexes);
556 for (int i = 0; i <= level; i++)
557 fprintf (stdout, " ");
558 printf ("</category>\n");
566 printf ("<dimension index=\"%d\">\n", indx);
567 dump_value (stdout, 0, false);
569 /* This byte is usually 0x02 but 0x00 and 0x75 (!) have also been spotted. */
572 if (!match_byte(0) && !match_byte(1))
573 match_byte_assert(2);
577 match_byte_assert(1);
579 match_byte_assert(1);
580 match_byte_assert(1);
581 match_u32_assert(indx);
582 n_categories = get_u32();
586 for (int i = 0; i < n_categories; i++)
587 dump_category (0, indexes, &n_indexes);
588 check_permutation(indexes, n_indexes, "categories");
590 fprintf (stdout, "</dimension>\n");
598 for (int i = 0; i < n_dims; i++)
605 /* The first three numbers add to the number of dimensions. */
608 match_u32_assert(n_dims - t);
610 /* The next n_dims numbers are a permutation of the dimension numbers. */
612 for (int i = 0; i < n_dims; i++)
614 check_permutation(a, n_dims, "dimensions");
618 for (int i = 0; i < x; i++)
620 printf (" <datum index=\"%d\">\n", get_u32());
622 dump_value(stdout, 1, false);
623 fprintf (stdout, " </datum>\n");
625 printf ("</data>\n");
632 printf ("<title-local>\n");
633 dump_value(stdout, 0, true);
634 printf ("</title-local>\n");
636 printf ("<subtype>\n");
637 dump_value(stdout, 0, true);
638 printf ("</subtype>\n");
640 match_byte_assert(0x31);
642 printf ("<title-c>\n");
643 dump_value(stdout, 0, true);
644 printf ("</title-c>\n");
647 match_byte_assert(0x58);
648 if (match_byte(0x31))
650 printf ("<caption>\n");
651 dump_value(stdout, 0, false);
652 printf ("</caption>\n");
655 match_byte_assert(0x58);
658 int n_footnotes = get_u32();
659 for (int i = 0; i < n_footnotes; i++)
661 printf ("<footnote index=\"%d\">\n", i);
662 dump_value(stdout, 0, false);
663 if (match_byte (0x31))
665 /* Custom footnote marker string. */
666 match_byte_assert(3);
668 match_byte_assert(0x58);
673 match_byte_assert (0x58);
674 printf("(%d)\n", get_u32());
675 printf ("</footnote>\n");
683 for (int i = 1; i <= 8; i++)
685 printf ("<style index=\"%d\"", i);
686 match_byte_assert(i);
687 match_byte_assert(0x31);
688 printf(" font=\"%s\"", get_string());
689 match_byte_assert(0);
690 match_byte_assert(0);
691 if (!match_byte(0x40) && !match_byte(0x20) && !match_byte(0x80) && !match_byte(0x10))
692 match_byte_assert(0x50);
693 if (!match_byte(0x41))
694 match_byte_assert(0x51);
697 match_byte_assert(0);
699 /* OK, this seems really unlikely to be totally correct, but it matches my corpus... */
700 if (!match_u32(0) && !match_u32(2))
701 match_u32_assert(0xfaad);
703 if (!match_u32(0) && !match_u32(1) && !match_u32(2))
705 printf (" fgcolor=\"%s\"", get_string());
706 printf (" bgcolor=\"%s\"", get_string());
709 match_byte_assert(0);
711 /* These seem unlikely to be correct too. */
716 match_u32_assert(11);
722 if (!match_u32(-1) && !match_u32(8))
723 match_u32_assert(24);
724 if (!match_u32(-1) && !match_u32(2))
728 /* Who knows? Ranges from -1 to 8 with no obvious pattern. */
734 match_u32_assert(240);
737 match_u32_assert(18);
744 match_u32_assert(142);
748 int count = get_u32();
751 printf ("<encoding>%s</encoding>\n", get_string ());
754 match_u32_assert(UINT32_MAX);
756 match_byte_assert(1);
757 match_byte_assert(0);
759 match_byte_assert(1);
760 if (!match_byte(0x97) && !match_byte(0x98) && !match_byte(0x99))
761 match_byte_assert(0x9a);
762 match_byte_assert(7);
763 match_byte_assert(0);
764 match_byte_assert(0);
766 match_byte_assert(',');
769 match_byte_assert(',');
770 if (!match_byte('.'))
771 match_byte_assert(' ');
774 for (int i = 0; i < 5; i++)
780 main(int argc, char *argv[])
785 if (isatty(STDIN_FILENO))
787 fprintf(stderr, "redirect stdin from a .bin file\n");
790 if (fstat(STDIN_FILENO, &s))
802 if (read(STDIN_FILENO, data, n) != n)
810 if (!strcmp(argv[1], "title0"))
813 if (match_byte (0x03)
814 || (match_byte (0x05) && match_byte (0x58)))
815 printf ("%s\n", get_string());
817 printf ("<unknown>\n");
820 else if (!strcmp(argv[1], "title"))
825 else if (!strcmp(argv[1], "titleraw"))
827 const char fonts[] = "\x01\x31\x09\0\0\0SansSerif";
829 n = find(fonts, sizeof fonts - 1);
831 else if (!strcmp(argv[1], "fonts"))
833 const char fonts[] = "\x01\x31\x09\0\0\0SansSerif";
834 const char styles[] = "\xf0\0\0\0";
835 start = find(fonts, sizeof fonts - 1);
836 n = find(styles, sizeof styles - 1);
838 else if (!strcmp(argv[1], "styles"))
840 const char styles[] = "\xf0\0\0\0";
841 const char dimensions[] = "-,,,.\0";
842 start = find(styles, sizeof styles - 1);
843 n = find(dimensions, sizeof dimensions - 1) + sizeof dimensions - 1;
845 else if (!strcmp(argv[1], "dimensions") || !strcmp(argv[1], "all"))
848 match_byte_assert(1);
849 match_byte_assert(0);
851 match_byte_assert(1);
853 match_byte_assert(1);
854 match_byte_assert(0);
855 match_byte_assert(0);
857 match_byte_assert(1);
859 match_byte_assert(0);
860 match_byte_assert(0);
861 match_byte_assert(0);
869 fprintf (stderr, "%x / %x\n", pos, n);
876 fprintf (stderr, "unknown section %s\n", argv[1]);
883 dump_raw(stdout, start, n);