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 /* This special case is really unsatisfying, but there are several
250 examples in the corpus, all from one SPV file. */
252 if (match_u32 (0x200) && match_u32(0x1000000) && match_u32(0)
257 int outer_end = pos + get_u32();
258 int inner_end = pos + get_u32();
260 if (match_byte(0x31))
262 /* Appears to be a template string, e.g. '^1 cells (^2) expf < 5. Min exp = ^3...'.
263 Probably doesn't actually appear in output because many examples look unpolished,
264 e.g. 'partial list cases value ^1 shown upper...' */
268 match_byte_assert(0x58);
269 if (pos != inner_end)
271 fprintf(stderr, "inner end discrepancy\n");
275 if (match_byte(0x31))
277 /* Only one example in the corpus. */
282 match_byte_assert(1);
283 get_string(); /* foreground */
284 get_string(); /* background */
285 get_string(); /* font */
287 match_byte_assert(12); /* size? */
290 match_byte_assert(0x58);
291 if (match_byte(0x31))
293 /* All the examples in the corpus, all from one SPV file, are
294 exactly like this. */
299 match_byte_assert(1);
300 match_byte_assert(0);
301 match_byte_assert(8);
302 match_byte_assert(0);
303 match_byte_assert(8);
304 match_byte_assert(0);
305 match_byte_assert(10);
306 match_byte_assert(0);
309 match_byte_assert(0x58);
310 if (pos != outer_end)
312 fprintf(stderr, "outer end discrepancy\n");
316 else if (match_u32 (1))
318 fprintf(stream, "(footnote %d) ", get_u32());
319 dump_nested_string();
321 else if (match_u32 (2))
323 fprintf(stream, "(special 2)");
324 match_byte_assert(0);
325 match_byte_assert(0);
328 dump_nested_string(); /* Our corpus doesn't contain any examples with strings though. */
333 fprintf(stream, "(special 3)");
334 match_byte_assert(0);
335 match_byte_assert(0);
336 match_byte_assert(1);
337 match_byte_assert(0);
339 dump_nested_string(); /* Our corpus doesn't contain any examples with strings though. */
343 match_byte_assert (0x58);
347 format_to_string (int type)
353 case 2: return "AHEX";
354 case 3: return "COMMA";
355 case 4: return "DOLLAR";
356 case 5: case 40: return "F";
358 case 7: return "PIBHEX";
360 case 9: return "PIB";
361 case 10: return "PK";
362 case 11: return "RB";
363 case 12: return "RBHEX";
367 case 20: return "DATE";
368 case 21: return "TIME";
369 case 22: return "DATETIME";
370 case 23: return "ADATE";
371 case 24: return "JDATE";
372 case 25: return "DTIME";
373 case 26: return "WKDAY";
374 case 27: return "MONTH";
375 case 28: return "MOYR";
376 case 29: return "QYR";
377 case 30: return "WKYR";
378 case 31: return "PCT";
379 case 32: return "DOT";
380 case 33: return "CCA";
381 case 34: return "CCB";
382 case 35: return "CCC";
383 case 36: return "CCD";
384 case 37: return "CCE";
385 case 38: return "EDATE";
386 case 39: return "SDATE";
389 sprintf(tmp, "<%d>", type);
395 dump_value(FILE *stream, int level, bool match1)
402 for (int i = 0; i <= level; i++)
403 fprintf (stream, " ");
407 char *text = get_string();
408 dump_value_31(stream);
409 char *identifier = get_string();
410 char *text_eng = get_string();
411 fprintf (stream, "<string c=\"%s\"", text_eng);
413 fprintf (stream, " identifier=\"%s\"", identifier);
414 if (strcmp(text_eng, text))
415 fprintf (stream, " local=\"%s\"", text);
416 fprintf (stream, "/>\n");
418 match_byte_assert(1);
422 else if (match_byte (5))
424 dump_value_31(stream);
425 char *name = get_string ();
426 char *label = get_string ();
427 fprintf (stream, "<variable name=\"%s\"", name);
429 fprintf (stream, " label=\"%s\"", label);
430 fprintf (stream, "/>\n");
431 if (!match_byte(1) && !match_byte(2))
432 match_byte_assert(3);
434 else if (match_byte (2))
440 match_byte_assert (0x58);
442 value = get_double ();
444 vallab = get_string ();
445 fprintf (stream, "<numeric-datum value=\"%.*g\" format=\"%s%d.%d\"",
446 DBL_DIG, value, format_to_string(format >> 16), (format >> 8) & 0xff, format & 0xff);
448 fprintf (stream, " variable=\"%s\"", var);
450 fprintf (stream, " label=\"%s\"/>\n", vallab);
451 fprintf (stream, "/>\n");
452 if (!match_byte (1) && !match_byte(2))
453 match_byte_assert (3);
455 else if (match_byte (4))
458 char *var, *vallab, *value;
460 match_byte_assert (0x58);
462 vallab = get_string ();
464 if (!match_byte(1) && !match_byte(2))
465 match_byte_assert (3);
466 value = get_string ();
467 fprintf (stream, "<string-datum value=\"%s\" format=\"%s%d.%d\"",
468 value, format_to_string(format >> 16), (format >> 8) & 0xff, format & 0xff);
470 fprintf (stream, " variable=\"%s\"", var);
472 fprintf (stream, " label=\"%s\"/>\n", vallab);
473 fprintf (stream, "/>\n");
475 else if (match_byte (1))
480 dump_value_31(stream);
482 value = get_double ();
483 fprintf (stream, "<number value=\"%.*g\" format=\"%s%d.%d\"/>\n",
484 DBL_DIG, value, format_to_string(format >> 16), (format >> 8) & 0xff, format & 0xff);
490 dump_value_31(stream);
492 char *base = get_string();
494 fprintf (stream, "<template format=\"%s\">\n", base);
495 for (int i = 0; i < x; i++)
502 for (int j = 0; j <= level + 1; j++)
503 fprintf (stream, " ");
504 fprintf (stream, "<substitution index=\"%d\">\n", i + 1);
505 for (int j = 0; j < y; j++)
506 dump_value (stream, level + 2, false);
507 for (int j = 0; j <= level + 1; j++)
508 fprintf (stream, " ");
509 fprintf (stream, "</substitution>\n");
511 for (int j = 0; j <= level; j++)
512 fprintf (stream, " ");
513 fprintf (stream, "</template>\n");
518 compare_int(const void *a_, const void *b_)
522 return *a < *b ? -1 : *a > *b;
526 check_permutation(int *a, int n, const char *name)
529 memcpy(b, a, n * sizeof *a);
530 qsort(b, n, sizeof *b, compare_int);
531 for (int i = 0; i < n; i++)
534 fprintf(stderr, "bad %s permutation:", name);
535 for (int i = 0; i < n; i++)
536 fprintf(stderr, " %d", a[i]);
543 dump_category(int level, int *indexes, int *n_indexes)
545 for (int i = 0; i <= level; i++)
546 fprintf (stdout, " ");
547 printf ("<category>\n");
548 dump_value (stdout, level + 1, true);
555 else if (match_byte (1))
559 match_u32_assert (1);
562 else if (!match_u32(2))
563 match_u32_assert (0);
565 int indx = get_u32();
566 int n_categories = get_u32();
569 if (n_categories != 0)
571 fprintf(stderr, "index not -1 but subcategories\n");
574 indexes[(*n_indexes)++] = indx;
576 if (n_categories == 0)
578 for (int i = 0; i <= level + 1; i++)
579 fprintf (stdout, " ");
580 fprintf (stdout, "<category-index>%d</category-index>\n", indx);
582 for (int i = 0; i < n_categories; i++)
583 dump_category (level + 1, indexes, n_indexes);
584 for (int i = 0; i <= level; i++)
585 fprintf (stdout, " ");
586 printf ("</category>\n");
594 printf ("<dimension index=\"%d\">\n", indx);
595 dump_value (stdout, 0, false);
597 /* This byte is usually 0x02 but 0x00 and 0x75 (!) have also been spotted. */
600 if (!match_byte(0) && !match_byte(1))
601 match_byte_assert(2);
605 match_byte_assert(1);
607 match_byte_assert(1);
608 match_byte_assert(1);
609 match_u32_assert(indx);
610 n_categories = get_u32();
614 for (int i = 0; i < n_categories; i++)
615 dump_category (0, indexes, &n_indexes);
616 check_permutation(indexes, n_indexes, "categories");
618 fprintf (stdout, "</dimension>\n");
626 for (int i = 0; i < n_dims; i++)
633 /* The first three numbers add to the number of dimensions. */
636 match_u32_assert(n_dims - t);
638 /* The next n_dims numbers are a permutation of the dimension numbers. */
640 for (int i = 0; i < n_dims; i++)
642 check_permutation(a, n_dims, "dimensions");
646 for (int i = 0; i < x; i++)
648 printf (" <datum index=\"%d\">\n", get_u32());
650 dump_value(stdout, 1, false);
651 fprintf (stdout, " </datum>\n");
653 printf ("</data>\n");
660 printf ("<title-local>\n");
661 dump_value(stdout, 0, true);
662 printf ("</title-local>\n");
664 printf ("<subtype>\n");
665 dump_value(stdout, 0, true);
666 printf ("</subtype>\n");
668 match_byte_assert(0x31);
670 printf ("<title-c>\n");
671 dump_value(stdout, 0, true);
672 printf ("</title-c>\n");
675 match_byte_assert(0x58);
676 if (match_byte(0x31))
678 printf ("<caption>\n");
679 dump_value(stdout, 0, false);
680 printf ("</caption>\n");
683 match_byte_assert(0x58);
686 int n_footnotes = get_u32();
687 for (int i = 0; i < n_footnotes; i++)
689 printf ("<footnote index=\"%d\">\n", i);
690 dump_value(stdout, 0, false);
691 if (match_byte (0x31))
693 /* Custom footnote marker string. */
694 match_byte_assert(3);
696 match_byte_assert(0x58);
701 match_byte_assert (0x58);
702 printf("(%d)\n", get_u32());
703 printf ("</footnote>\n");
711 for (int i = 1; i <= 8; i++)
713 printf ("<style index=\"%d\"", i);
714 match_byte_assert(i);
715 match_byte_assert(0x31);
716 printf(" font=\"%s\"", get_string());
717 match_byte_assert(0);
718 match_byte_assert(0);
719 if (!match_byte(0x40) && !match_byte(0x20) && !match_byte(0x80) && !match_byte(0x10))
720 match_byte_assert(0x50);
721 if (!match_byte(0x41))
722 match_byte_assert(0x51);
725 match_byte_assert(0);
727 /* OK, this seems really unlikely to be totally correct, but it matches my corpus... */
728 if (!match_u32(0) && !match_u32(2))
729 match_u32_assert(0xfaad);
731 if (!match_u32(0) && !match_u32(1) && !match_u32(2))
733 printf (" fgcolor=\"%s\"", get_string());
734 printf (" bgcolor=\"%s\"", get_string());
737 match_byte_assert(0);
739 /* These seem unlikely to be correct too. */
744 match_u32_assert(11);
750 if (!match_u32(-1) && !match_u32(8))
751 match_u32_assert(24);
752 if (!match_u32(-1) && !match_u32(2))
756 /* Who knows? Ranges from -1 to 8 with no obvious pattern. */
762 match_u32_assert(240);
765 match_u32_assert(18);
772 match_u32_assert(142);
776 int count = get_u32();
779 printf ("<encoding>%s</encoding>\n", get_string ());
782 match_u32_assert(UINT32_MAX);
784 match_byte_assert(1);
785 match_byte_assert(0);
787 match_byte_assert(1);
788 if (!match_byte(0x97) && !match_byte(0x98) && !match_byte(0x99))
789 match_byte_assert(0x9a);
790 match_byte_assert(7);
791 match_byte_assert(0);
792 match_byte_assert(0);
794 match_byte_assert(',');
797 match_byte_assert(',');
798 if (!match_byte('.'))
799 match_byte_assert(' ');
802 for (int i = 0; i < 5; i++)
808 main(int argc, char *argv[])
813 if (isatty(STDIN_FILENO))
815 fprintf(stderr, "redirect stdin from a .bin file\n");
818 if (fstat(STDIN_FILENO, &s))
830 if (read(STDIN_FILENO, data, n) != n)
838 if (!strcmp(argv[1], "title0"))
841 if (match_byte (0x03)
842 || (match_byte (0x05) && match_byte (0x58)))
843 printf ("%s\n", get_string());
845 printf ("<unknown>\n");
848 else if (!strcmp(argv[1], "title"))
853 else if (!strcmp(argv[1], "titleraw"))
855 const char fonts[] = "\x01\x31\x09\0\0\0SansSerif";
857 n = find(fonts, sizeof fonts - 1);
859 else if (!strcmp(argv[1], "fonts"))
861 const char fonts[] = "\x01\x31\x09\0\0\0SansSerif";
862 const char styles[] = "\xf0\0\0\0";
863 start = find(fonts, sizeof fonts - 1);
864 n = find(styles, sizeof styles - 1);
866 else if (!strcmp(argv[1], "styles"))
868 const char styles[] = "\xf0\0\0\0";
869 const char dimensions[] = "-,,,.\0";
870 start = find(styles, sizeof styles - 1);
871 n = find(dimensions, sizeof dimensions - 1) + sizeof dimensions - 1;
873 else if (!strcmp(argv[1], "dimensions") || !strcmp(argv[1], "all"))
876 match_byte_assert(1);
877 match_byte_assert(0);
879 match_byte_assert(1);
881 match_byte_assert(1);
882 match_byte_assert(0);
883 match_byte_assert(0);
885 match_byte_assert(1);
887 match_byte_assert(0);
888 match_byte_assert(0);
889 match_byte_assert(0);
897 fprintf (stderr, "%x / %x\n", pos, n);
904 fprintf (stderr, "unknown section %s\n", argv[1]);
911 dump_raw(stdout, start, n);