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 */
278 match_byte_assert(12); /* size? */
281 match_byte_assert(0x58);
282 match_byte_assert(0x58);
283 if (pos != outer_end)
285 fprintf(stderr, "outer end discrepancy\n");
289 else if (match_u32 (1))
291 fprintf(stream, "(footnote %d) ", get_u32());
292 dump_nested_string();
294 else if (match_u32 (2))
296 fprintf(stream, "(special 2)");
297 match_byte_assert(0);
298 match_byte_assert(0);
301 dump_nested_string(); /* Our corpus doesn't contain any examples with strings though. */
306 fprintf(stream, "(special 3)");
307 match_byte_assert(0);
308 match_byte_assert(0);
309 match_byte_assert(1);
310 match_byte_assert(0);
312 dump_nested_string(); /* Our corpus doesn't contain any examples with strings though. */
316 match_byte_assert (0x58);
320 format_to_string (int type)
326 case 2: return "AHEX";
327 case 3: return "COMMA";
328 case 4: return "DOLLAR";
329 case 5: case 40: return "F";
331 case 7: return "PIBHEX";
333 case 9: return "PIB";
334 case 10: return "PK";
335 case 11: return "RB";
336 case 12: return "RBHEX";
340 case 20: return "DATE";
341 case 21: return "TIME";
342 case 22: return "DATETIME";
343 case 23: return "ADATE";
344 case 24: return "JDATE";
345 case 25: return "DTIME";
346 case 26: return "WKDAY";
347 case 27: return "MONTH";
348 case 28: return "MOYR";
349 case 29: return "QYR";
350 case 30: return "WKYR";
351 case 31: return "PCT";
352 case 32: return "DOT";
353 case 33: return "CCA";
354 case 34: return "CCB";
355 case 35: return "CCC";
356 case 36: return "CCD";
357 case 37: return "CCE";
358 case 38: return "EDATE";
359 case 39: return "SDATE";
362 sprintf(tmp, "<%d>", type);
368 dump_value(FILE *stream, int level, bool match1)
375 for (int i = 0; i <= level; i++)
376 fprintf (stream, " ");
380 char *text = get_string();
381 dump_value_31(stream);
382 char *identifier = get_string();
383 char *text_eng = get_string();
384 fprintf (stream, "<string c=\"%s\"", text_eng);
386 fprintf (stream, " identifier=\"%s\"", identifier);
387 if (strcmp(text_eng, text))
388 fprintf (stream, " local=\"%s\"", text);
389 fprintf (stream, "/>\n");
391 match_byte_assert(1);
395 else if (match_byte (5))
397 dump_value_31(stream);
398 char *name = get_string ();
399 char *label = get_string ();
400 fprintf (stream, "<variable name=\"%s\"", name);
402 fprintf (stream, " label=\"%s\"", label);
403 fprintf (stream, "/>\n");
404 if (!match_byte(1) && !match_byte(2))
405 match_byte_assert(3);
407 else if (match_byte (2))
413 match_byte_assert (0x58);
415 value = get_double ();
417 vallab = get_string ();
418 fprintf (stream, "<numeric-datum value=\"%.*g\" format=\"%s%d.%d\"",
419 DBL_DIG, value, format_to_string(format >> 16), (format >> 8) & 0xff, format & 0xff);
421 fprintf (stream, " variable=\"%s\"", var);
423 fprintf (stream, " label=\"%s\"/>\n", vallab);
424 fprintf (stream, "/>\n");
425 if (!match_byte (1) && !match_byte(2))
426 match_byte_assert (3);
428 else if (match_byte (4))
431 char *var, *vallab, *value;
433 match_byte_assert (0x58);
435 vallab = get_string ();
437 if (!match_byte(1) && !match_byte(2))
438 match_byte_assert (3);
439 value = get_string ();
440 fprintf (stream, "<string-datum value=\"%s\" format=\"%s%d.%d\"",
441 value, format_to_string(format >> 16), (format >> 8) & 0xff, format & 0xff);
443 fprintf (stream, " variable=\"%s\"", var);
445 fprintf (stream, " label=\"%s\"/>\n", vallab);
446 fprintf (stream, "/>\n");
448 else if (match_byte (1))
453 dump_value_31(stream);
455 value = get_double ();
456 fprintf (stream, "<number value=\"%.*g\" format=\"%s%d.%d\"/>\n",
457 DBL_DIG, value, format_to_string(format >> 16), (format >> 8) & 0xff, format & 0xff);
463 dump_value_31(stream);
465 char *base = get_string();
467 fprintf (stream, "<template format=\"%s\">\n", base);
468 for (int i = 0; i < x; i++)
475 for (int j = 0; j <= level + 1; j++)
476 fprintf (stream, " ");
477 fprintf (stream, "<substitution index=\"%d\">\n", i + 1);
478 for (int j = 0; j < y; j++)
479 dump_value (stream, level + 2, false);
480 for (int j = 0; j <= level + 1; j++)
481 fprintf (stream, " ");
482 fprintf (stream, "</substitution>\n");
484 for (int j = 0; j <= level; j++)
485 fprintf (stream, " ");
486 fprintf (stream, "</template>\n");
491 compare_int(const void *a_, const void *b_)
495 return *a < *b ? -1 : *a > *b;
499 check_permutation(int *a, int n, const char *name)
502 memcpy(b, a, n * sizeof *a);
503 qsort(b, n, sizeof *b, compare_int);
504 for (int i = 0; i < n; i++)
507 fprintf(stderr, "bad %s permutation:", name);
508 for (int i = 0; i < n; i++)
509 fprintf(stderr, " %d", a[i]);
516 dump_category(int level, int *indexes, int *n_indexes)
518 for (int i = 0; i <= level; i++)
519 fprintf (stdout, " ");
520 printf ("<category>\n");
521 dump_value (stdout, level + 1, true);
528 else if (match_byte (1))
532 match_u32_assert (1);
535 else if (!match_u32(2))
536 match_u32_assert (0);
538 int indx = get_u32();
539 int n_categories = get_u32();
542 if (n_categories != 0)
544 fprintf(stderr, "index not -1 but subcategories\n");
547 indexes[(*n_indexes)++] = indx;
549 if (n_categories == 0)
551 for (int i = 0; i <= level + 1; i++)
552 fprintf (stdout, " ");
553 fprintf (stdout, "<category-index>%d</category-index>\n", indx);
555 for (int i = 0; i < n_categories; i++)
556 dump_category (level + 1, indexes, n_indexes);
557 for (int i = 0; i <= level; i++)
558 fprintf (stdout, " ");
559 printf ("</category>\n");
567 printf ("<dimension index=\"%d\">\n", indx);
568 dump_value (stdout, 0, false);
570 /* This byte is usually 0x02 but 0x00 and 0x75 (!) have also been spotted. */
573 if (!match_byte(0) && !match_byte(1))
574 match_byte_assert(2);
578 match_byte_assert(1);
580 match_byte_assert(1);
581 match_byte_assert(1);
582 match_u32_assert(indx);
583 n_categories = get_u32();
587 for (int i = 0; i < n_categories; i++)
588 dump_category (0, indexes, &n_indexes);
589 check_permutation(indexes, n_indexes, "categories");
591 fprintf (stdout, "</dimension>\n");
599 for (int i = 0; i < n_dims; i++)
606 /* The first three numbers add to the number of dimensions. */
609 match_u32_assert(n_dims - t);
611 /* The next n_dims numbers are a permutation of the dimension numbers. */
613 for (int i = 0; i < n_dims; i++)
615 check_permutation(a, n_dims, "dimensions");
619 for (int i = 0; i < x; i++)
621 printf (" <datum index=\"%d\">\n", get_u32());
623 dump_value(stdout, 1, false);
624 fprintf (stdout, " </datum>\n");
626 printf ("</data>\n");
633 printf ("<title-local>\n");
634 dump_value(stdout, 0, true);
635 printf ("</title-local>\n");
637 printf ("<subtype>\n");
638 dump_value(stdout, 0, true);
639 printf ("</subtype>\n");
641 match_byte_assert(0x31);
643 printf ("<title-c>\n");
644 dump_value(stdout, 0, true);
645 printf ("</title-c>\n");
648 match_byte_assert(0x58);
649 if (match_byte(0x31))
651 printf ("<caption>\n");
652 dump_value(stdout, 0, false);
653 printf ("</caption>\n");
656 match_byte_assert(0x58);
659 int n_footnotes = get_u32();
660 for (int i = 0; i < n_footnotes; i++)
662 printf ("<footnote index=\"%d\">\n", i);
663 dump_value(stdout, 0, false);
664 if (match_byte (0x31))
666 /* Custom footnote marker string. */
667 match_byte_assert(3);
669 match_byte_assert(0x58);
674 match_byte_assert (0x58);
675 printf("(%d)\n", get_u32());
676 printf ("</footnote>\n");
684 for (int i = 1; i <= 8; i++)
686 printf ("<style index=\"%d\"", i);
687 match_byte_assert(i);
688 match_byte_assert(0x31);
689 printf(" font=\"%s\"", get_string());
690 match_byte_assert(0);
691 match_byte_assert(0);
692 if (!match_byte(0x40) && !match_byte(0x20) && !match_byte(0x80) && !match_byte(0x10))
693 match_byte_assert(0x50);
694 if (!match_byte(0x41))
695 match_byte_assert(0x51);
698 match_byte_assert(0);
700 /* OK, this seems really unlikely to be totally correct, but it matches my corpus... */
701 if (!match_u32(0) && !match_u32(2))
702 match_u32_assert(0xfaad);
704 if (!match_u32(0) && !match_u32(1) && !match_u32(2))
706 printf (" fgcolor=\"%s\"", get_string());
707 printf (" bgcolor=\"%s\"", get_string());
710 match_byte_assert(0);
712 /* These seem unlikely to be correct too. */
717 match_u32_assert(11);
723 if (!match_u32(-1) && !match_u32(8))
724 match_u32_assert(24);
725 if (!match_u32(-1) && !match_u32(2))
729 /* Who knows? Ranges from -1 to 8 with no obvious pattern. */
735 match_u32_assert(240);
738 match_u32_assert(18);
745 match_u32_assert(142);
749 int count = get_u32();
752 printf ("<encoding>%s</encoding>\n", get_string ());
755 match_u32_assert(UINT32_MAX);
757 match_byte_assert(1);
758 match_byte_assert(0);
760 match_byte_assert(1);
761 if (!match_byte(0x97) && !match_byte(0x98) && !match_byte(0x99))
762 match_byte_assert(0x9a);
763 match_byte_assert(7);
764 match_byte_assert(0);
765 match_byte_assert(0);
767 match_byte_assert(',');
770 match_byte_assert(',');
771 if (!match_byte('.'))
772 match_byte_assert(' ');
775 for (int i = 0; i < 5; i++)
781 main(int argc, char *argv[])
786 if (isatty(STDIN_FILENO))
788 fprintf(stderr, "redirect stdin from a .bin file\n");
791 if (fstat(STDIN_FILENO, &s))
803 if (read(STDIN_FILENO, data, n) != n)
811 if (!strcmp(argv[1], "title0"))
814 if (match_byte (0x03)
815 || (match_byte (0x05) && match_byte (0x58)))
816 printf ("%s\n", get_string());
818 printf ("<unknown>\n");
821 else if (!strcmp(argv[1], "title"))
826 else if (!strcmp(argv[1], "titleraw"))
828 const char fonts[] = "\x01\x31\x09\0\0\0SansSerif";
830 n = find(fonts, sizeof fonts - 1);
832 else if (!strcmp(argv[1], "fonts"))
834 const char fonts[] = "\x01\x31\x09\0\0\0SansSerif";
835 const char styles[] = "\xf0\0\0\0";
836 start = find(fonts, sizeof fonts - 1);
837 n = find(styles, sizeof styles - 1);
839 else if (!strcmp(argv[1], "styles"))
841 const char styles[] = "\xf0\0\0\0";
842 const char dimensions[] = "-,,,.\0";
843 start = find(styles, sizeof styles - 1);
844 n = find(dimensions, sizeof dimensions - 1) + sizeof dimensions - 1;
846 else if (!strcmp(argv[1], "dimensions") || !strcmp(argv[1], "all"))
849 match_byte_assert(1);
850 match_byte_assert(0);
852 match_byte_assert(1);
854 match_byte_assert(1);
855 match_byte_assert(0);
856 match_byte_assert(0);
858 match_byte_assert(1);
860 match_byte_assert(0);
861 match_byte_assert(0);
862 match_byte_assert(0);
870 fprintf (stderr, "%x / %x\n", pos, n);
877 fprintf (stderr, "unknown section %s\n", argv[1]);
884 dump_raw(stdout, start, n);