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 dump_raw(FILE *stream, int start, int end, const char *separator)
112 for (size_t i = start; i < end; )
119 && i + 4 + data[i] + data[i + 1] * 256 <= end
120 && all_ascii(&data[i + 4], data[i] + data[i + 1] * 256))
122 fprintf(stream, "%s\"", separator);
123 fwrite(&data[i + 4], 1, data[i] + data[i + 1] * 256, stream);
124 fputs("\" ", stream);
126 i += 4 + data[i] + data[i + 1] * 256;
128 else if (i + 12 <= end
135 memcpy (&d, &data[i + 4], 8);
136 fprintf (stream, "F40.%d(%.*f)%s", data[i], data[i], d, separator);
139 else if (i + 12 <= end
146 memcpy (&d, &data[i + 4], 8);
147 fprintf (stream, "PCT40.%d(%.*f)%s", data[i], data[i], d, separator);
150 else if (i + 4 <= end
151 && (data[i] && data[i] != 88 && data[i] != 0x41)
156 fprintf (stream, "i%d ", data[i]);
161 fprintf(stream, "%02x ", data[i]);
170 get_string(const char *where)
173 /*data[pos + 1] == 0 && data[pos + 2] == 0 && data[pos + 3] == 0*/
174 /*&& all_ascii(&data[pos + 4], data[pos])*/)
176 int len = data[pos] + data[pos + 1] * 256;
177 char *s = malloc(len + 1);
179 memcpy(s, &data[pos + 4], len);
186 fprintf(stderr, "%s: 0x%x: expected string\n", where, pos);
190 #define get_string() get_string(WHERE)
193 dump_nested_string(void)
197 match_byte_assert (0);
198 match_byte_assert (0);
199 int outer_end = pos + get_u32();
200 int inner_end = pos + get_u32();
201 if (pos != inner_end)
204 if (match_byte(0x31))
207 match_byte_assert(0x58);
208 if (pos != inner_end)
210 fprintf(stderr, "inner end discrepancy\n");
214 match_byte_assert(0x58);
215 match_byte_assert(0x58);
216 if (pos != outer_end)
218 fprintf(stderr, "outer end discrepancy\n");
226 dump_value_31(FILE *stream)
228 if (match_byte (0x31))
234 /* Only "a" observed as a sample value (although it appears 44 times in the corpus). */
238 match_u32_assert (0);
240 int outer_end = pos + get_u32();
241 int inner_end = pos + get_u32();
243 if (match_byte(0x31))
245 /* Appears to be a template string, e.g. '^1 cells (^2) expf < 5. Min exp = ^3...'.
246 Probably doesn't actually appear in output because many examples look unpolished,
247 e.g. 'partial list cases value ^1 shown upper...' */
251 match_byte_assert(0x58);
252 if (pos != inner_end)
254 fprintf(stderr, "inner end discrepancy\n");
258 if (match_byte(0x31))
260 /* Only one example in the corpus. */
264 match_byte_assert(1);
265 get_string(); /* foreground */
266 get_string(); /* background */
267 get_string(); /* font */
268 match_byte_assert(12); /* size? */
271 match_byte_assert(0x58);
272 match_byte_assert(0x58);
273 if (pos != outer_end)
275 fprintf(stderr, "outer end discrepancy\n");
279 else if (match_u32 (1))
281 fprintf(stream, "(footnote %d) ", get_u32());
282 dump_nested_string();
284 else if (match_u32 (2))
286 fprintf(stream, "(special 2)");
287 match_byte_assert(0);
288 match_byte_assert(0);
291 dump_nested_string(); /* Our corpus doesn't contain any examples with strings though. */
296 fprintf(stream, "(special 3)");
297 match_byte_assert(0);
298 match_byte_assert(0);
299 match_byte_assert(1);
300 match_byte_assert(0);
302 dump_nested_string(); /* Our corpus doesn't contain any examples with strings though. */
306 match_byte_assert (0x58);
310 format_to_string (int type)
316 case 2: return "AHEX";
317 case 3: return "COMMA";
318 case 4: return "DOLLAR";
319 case 5: case 40: return "F";
321 case 7: return "PIBHEX";
323 case 9: return "PIB";
324 case 10: return "PK";
325 case 11: return "RB";
326 case 12: return "RBHEX";
330 case 20: return "DATE";
331 case 21: return "TIME";
332 case 22: return "DATETIME";
333 case 23: return "ADATE";
334 case 24: return "JDATE";
335 case 25: return "DTIME";
336 case 26: return "WKDAY";
337 case 27: return "MONTH";
338 case 28: return "MOYR";
339 case 29: return "QYR";
340 case 30: return "WKYR";
341 case 31: return "PCT";
342 case 32: return "DOT";
343 case 33: return "CCA";
344 case 34: return "CCB";
345 case 35: return "CCC";
346 case 36: return "CCD";
347 case 37: return "CCE";
348 case 38: return "EDATE";
349 case 39: return "SDATE";
352 sprintf(tmp, "<%d>", type);
358 dump_value(FILE *stream, int level, bool match1)
365 for (int i = 0; i <= level; i++)
366 fprintf (stream, " ");
370 char *text = get_string();
371 dump_value_31(stream);
372 char *identifier = get_string();
373 char *text_eng = get_string();
374 fprintf (stream, "<string c=\"%s\"", text_eng);
376 fprintf (stream, " identifier=\"%s\"", identifier);
377 if (strcmp(text_eng, text))
378 fprintf (stream, " local=\"%s\"", text);
379 fprintf (stream, "/>\n");
381 match_byte_assert(1);
385 else if (match_byte (5))
387 dump_value_31(stream);
388 char *name = get_string ();
389 char *label = get_string ();
390 fprintf (stream, "<variable name=\"%s\"", name);
392 fprintf (stream, " label=\"%s\"", label);
393 fprintf (stream, "/>\n");
394 if (!match_byte(1) && !match_byte(2))
395 match_byte_assert(3);
397 else if (match_byte (2))
403 match_byte_assert (0x58);
405 value = get_double ();
407 vallab = get_string ();
408 fprintf (stream, "<numeric-datum value=\"%.*g\" format=\"%s%d.%d\"",
409 DBL_DIG, value, format_to_string(format >> 16), (format >> 8) & 0xff, format & 0xff);
411 fprintf (stream, " variable=\"%s\"", var);
413 fprintf (stream, " label=\"%s\"/>\n", vallab);
414 fprintf (stream, "/>\n");
415 if (!match_byte (1) && !match_byte(2))
416 match_byte_assert (3);
418 else if (match_byte (4))
421 char *var, *vallab, *value;
423 match_byte_assert (0x58);
425 vallab = get_string ();
427 if (!match_byte(1) && !match_byte(2))
428 match_byte_assert (3);
429 value = get_string ();
430 fprintf (stream, "<string-datum value=\"%s\" format=\"%s%d.%d\"",
431 value, format_to_string(format >> 16), (format >> 8) & 0xff, format & 0xff);
433 fprintf (stream, " variable=\"%s\"", var);
435 fprintf (stream, " label=\"%s\"/>\n", vallab);
436 fprintf (stream, "/>\n");
438 else if (match_byte (1))
443 dump_value_31(stream);
445 value = get_double ();
446 fprintf (stream, "<number value=\"%.*g\" format=\"%s%d.%d\"/>\n",
447 DBL_DIG, value, format_to_string(format >> 16), (format >> 8) & 0xff, format & 0xff);
453 dump_value_31(stream);
455 char *base = get_string();
457 fprintf (stream, "<template format=\"%s\">\n", base);
458 for (int i = 0; i < x; i++)
465 for (int j = 0; j <= level + 1; j++)
466 fprintf (stream, " ");
467 fprintf (stream, "<substitution index=\"%d\">\n", i + 1);
468 for (int j = 0; j < y; j++)
469 dump_value (stream, level + 2, false);
470 for (int j = 0; j <= level + 1; j++)
471 fprintf (stream, " ");
472 fprintf (stream, "</substitution>\n");
474 for (int j = 0; j <= level; j++)
475 fprintf (stream, " ");
476 fprintf (stream, "</template>\n");
481 compare_int(const void *a_, const void *b_)
485 return *a < *b ? -1 : *a > *b;
489 check_permutation(int *a, int n, const char *name)
492 memcpy(b, a, n * sizeof *a);
493 qsort(b, n, sizeof *b, compare_int);
494 for (int i = 0; i < n; i++)
497 fprintf(stderr, "bad %s permutation:", name);
498 for (int i = 0; i < n; i++)
499 fprintf(stderr, " %d", a[i]);
506 dump_category(int level, int *indexes, int *n_indexes)
508 for (int i = 0; i <= level; i++)
509 fprintf (stdout, " ");
510 printf ("<category>\n");
511 dump_value (stdout, level + 1, true);
518 else if (match_byte (1))
522 match_u32_assert (1);
525 else if (!match_u32(2))
526 match_u32_assert (0);
528 int indx = get_u32();
529 int n_categories = get_u32();
532 if (n_categories != 0)
534 fprintf(stderr, "index not -1 but subcategories\n");
537 indexes[(*n_indexes)++] = indx;
539 if (n_categories == 0)
541 for (int i = 0; i <= level + 1; i++)
542 fprintf (stdout, " ");
543 fprintf (stdout, "<category-index>%d</category-index>\n", indx);
545 for (int i = 0; i < n_categories; i++)
546 dump_category (level + 1, indexes, n_indexes);
547 for (int i = 0; i <= level; i++)
548 fprintf (stdout, " ");
549 printf ("</category>\n");
557 printf ("<dimension index=\"%d\">\n", indx);
558 dump_value (stdout, 0, false);
560 /* This byte is usually 0x02 but 0x00 and 0x75 (!) have also been spotted. */
563 if (!match_byte(0) && !match_byte(1))
564 match_byte_assert(2);
568 match_byte_assert(1);
570 match_byte_assert(1);
571 match_byte_assert(1);
572 match_u32_assert(indx);
573 n_categories = get_u32();
577 for (int i = 0; i < n_categories; i++)
578 dump_category (0, indexes, &n_indexes);
579 check_permutation(indexes, n_indexes, "categories");
581 fprintf (stdout, "</dimension>\n");
589 for (int i = 0; i < n_dims; i++)
596 /* The first three numbers add to the number of dimensions. */
599 match_u32_assert(n_dims - t);
601 /* The next n_dims numbers are a permutation of the dimension numbers. */
603 for (int i = 0; i < n_dims; i++)
605 check_permutation(a, n_dims, "dimensions");
609 for (int i = 0; i < x; i++)
611 printf (" <datum index=\"%d\">\n", get_u32());
613 dump_value(stdout, 1, false);
614 fprintf (stdout, " </datum>\n");
616 printf ("</data>\n");
623 printf ("<title-local>\n");
624 dump_value(stdout, 0, true);
625 printf ("</title-local>\n");
627 printf ("<subtype>\n");
628 dump_value(stdout, 0, true);
629 printf ("</subtype>\n");
631 match_byte_assert(0x31);
633 printf ("<title-c>\n");
634 dump_value(stdout, 0, true);
635 printf ("</title-c>\n");
638 match_byte_assert(0x58);
639 if (match_byte(0x31))
641 printf ("<caption>\n");
642 dump_value(stdout, 0, false);
643 printf ("</caption>\n");
646 match_byte_assert(0x58);
649 int n_footnotes = get_u32();
650 for (int i = 0; i < n_footnotes; i++)
652 printf ("<footnote index=\"%d\">\n", i);
653 dump_value(stdout, 0, false);
654 if (match_byte (0x31))
656 /* Custom footnote marker string. */
657 match_byte_assert(3);
659 match_byte_assert(0x58);
664 match_byte_assert (0x58);
665 printf("(%d)\n", get_u32());
666 printf ("</footnote>\n");
674 for (int i = 1; i <= 8; i++)
676 printf ("<style index=\"%d\"", i);
677 match_byte_assert(i);
678 match_byte_assert(0x31);
679 printf(" font=\"%s\"", get_string());
680 match_byte_assert(0);
681 match_byte_assert(0);
682 if (!match_byte(0x40) && !match_byte(0x20) && !match_byte(0x80) && !match_byte(0x10))
683 match_byte_assert(0x50);
684 if (!match_byte(0x41))
685 match_byte_assert(0x51);
688 match_byte_assert(0);
690 /* OK, this seems really unlikely to be totally correct, but it matches my corpus... */
691 if (!match_u32(0) && !match_u32(2))
692 match_u32_assert(0xfaad);
694 if (!match_u32(0) && !match_u32(1) && !match_u32(2))
696 printf (" fgcolor=\"%s\"", get_string());
697 printf (" bgcolor=\"%s\"", get_string());
700 match_byte_assert(0);
702 /* These seem unlikely to be correct too. */
707 match_u32_assert(11);
713 if (!match_u32(-1) && !match_u32(8))
714 match_u32_assert(24);
715 if (!match_u32(-1) && !match_u32(2))
719 /* Who knows? Ranges from -1 to 8 with no obvious pattern. */
725 match_u32_assert(240);
728 match_u32_assert(18);
735 match_u32_assert(142);
739 int count = get_u32();
742 printf ("<encoding>%s</encoding>\n", get_string ());
745 match_u32_assert(UINT32_MAX);
747 match_byte_assert(1);
748 match_byte_assert(0);
750 match_byte_assert(1);
751 if (!match_byte(0x99) && !match_byte(0x98))
752 match_byte_assert(0x97);
753 match_byte_assert(7);
754 match_byte_assert(0);
755 match_byte_assert(0);
757 match_byte_assert(',');
760 match_byte_assert(',');
761 if (!match_byte('.'))
762 match_byte_assert(' ');
765 for (int i = 0; i < 5; i++)
771 main(int argc, char *argv[])
776 if (isatty(STDIN_FILENO))
778 fprintf(stderr, "redirect stdin from a .bin file\n");
781 if (fstat(STDIN_FILENO, &s))
793 if (read(STDIN_FILENO, data, n) != n)
801 if (!strcmp(argv[1], "title0"))
804 if (match_byte (0x03)
805 || (match_byte (0x05) && match_byte (0x58)))
806 printf ("%s\n", get_string());
808 printf ("<unknown>\n");
811 else if (!strcmp(argv[1], "title"))
816 else if (!strcmp(argv[1], "titleraw"))
818 const char fonts[] = "\x01\x31\x09\0\0\0SansSerif";
820 n = find(fonts, sizeof fonts - 1);
822 else if (!strcmp(argv[1], "fonts"))
824 const char fonts[] = "\x01\x31\x09\0\0\0SansSerif";
825 const char styles[] = "\xf0\0\0\0";
826 start = find(fonts, sizeof fonts - 1);
827 n = find(styles, sizeof styles - 1);
829 else if (!strcmp(argv[1], "styles"))
831 const char styles[] = "\xf0\0\0\0";
832 const char dimensions[] = "-,,,.\0";
833 start = find(styles, sizeof styles - 1);
834 n = find(dimensions, sizeof dimensions - 1) + sizeof dimensions - 1;
836 else if (!strcmp(argv[1], "dimensions") || !strcmp(argv[1], "all"))
839 match_byte_assert(1);
840 match_byte_assert(0);
842 match_byte_assert(1);
844 match_byte_assert(1);
845 match_byte_assert(0);
846 match_byte_assert(0);
848 match_byte_assert(1);
850 match_byte_assert(0);
851 match_byte_assert(0);
852 match_byte_assert(0);
860 fprintf (stderr, "%x / %x\n", pos, n);
867 fprintf (stderr, "unknown section %s\n", argv[1]);
874 dump_raw(stdout, start, n, "\n");