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. */
274 match_byte_assert(1);
275 get_string(); /* foreground */
276 get_string(); /* background */
277 get_string(); /* font */
279 match_byte_assert(12); /* size? */
282 match_byte_assert(0x58);
283 if (match_byte(0x31))
285 /* All the examples in the corpus, all from one SPV file, are
286 exactly like this. */
291 match_byte_assert(1);
292 match_byte_assert(0);
293 match_byte_assert(8);
294 match_byte_assert(0);
295 match_byte_assert(8);
296 match_byte_assert(0);
297 match_byte_assert(10);
298 match_byte_assert(0);
301 match_byte_assert(0x58);
302 if (pos != outer_end)
304 fprintf(stderr, "outer end discrepancy\n");
308 else if (match_u32 (1))
310 fprintf(stream, "(footnote %d) ", get_u32());
311 dump_nested_string();
313 else if (match_u32 (2))
315 fprintf(stream, "(special 2)");
316 match_byte_assert(0);
317 match_byte_assert(0);
320 dump_nested_string(); /* Our corpus doesn't contain any examples with strings though. */
325 fprintf(stream, "(special 3)");
326 match_byte_assert(0);
327 match_byte_assert(0);
328 match_byte_assert(1);
329 match_byte_assert(0);
331 dump_nested_string(); /* Our corpus doesn't contain any examples with strings though. */
335 match_byte_assert (0x58);
339 format_to_string (int type)
345 case 2: return "AHEX";
346 case 3: return "COMMA";
347 case 4: return "DOLLAR";
348 case 5: case 40: return "F";
350 case 7: return "PIBHEX";
352 case 9: return "PIB";
353 case 10: return "PK";
354 case 11: return "RB";
355 case 12: return "RBHEX";
359 case 20: return "DATE";
360 case 21: return "TIME";
361 case 22: return "DATETIME";
362 case 23: return "ADATE";
363 case 24: return "JDATE";
364 case 25: return "DTIME";
365 case 26: return "WKDAY";
366 case 27: return "MONTH";
367 case 28: return "MOYR";
368 case 29: return "QYR";
369 case 30: return "WKYR";
370 case 31: return "PCT";
371 case 32: return "DOT";
372 case 33: return "CCA";
373 case 34: return "CCB";
374 case 35: return "CCC";
375 case 36: return "CCD";
376 case 37: return "CCE";
377 case 38: return "EDATE";
378 case 39: return "SDATE";
381 sprintf(tmp, "<%d>", type);
387 dump_value(FILE *stream, int level, bool match1)
394 for (int i = 0; i <= level; i++)
395 fprintf (stream, " ");
399 char *text = get_string();
400 dump_value_31(stream);
401 char *identifier = get_string();
402 char *text_eng = get_string();
403 fprintf (stream, "<string c=\"%s\"", text_eng);
405 fprintf (stream, " identifier=\"%s\"", identifier);
406 if (strcmp(text_eng, text))
407 fprintf (stream, " local=\"%s\"", text);
408 fprintf (stream, "/>\n");
410 match_byte_assert(1);
414 else if (match_byte (5))
416 dump_value_31(stream);
417 char *name = get_string ();
418 char *label = get_string ();
419 fprintf (stream, "<variable name=\"%s\"", name);
421 fprintf (stream, " label=\"%s\"", label);
422 fprintf (stream, "/>\n");
423 if (!match_byte(1) && !match_byte(2))
424 match_byte_assert(3);
426 else if (match_byte (2))
432 match_byte_assert (0x58);
434 value = get_double ();
436 vallab = get_string ();
437 fprintf (stream, "<numeric-datum value=\"%.*g\" format=\"%s%d.%d\"",
438 DBL_DIG, value, format_to_string(format >> 16), (format >> 8) & 0xff, format & 0xff);
440 fprintf (stream, " variable=\"%s\"", var);
442 fprintf (stream, " label=\"%s\"/>\n", vallab);
443 fprintf (stream, "/>\n");
444 if (!match_byte (1) && !match_byte(2))
445 match_byte_assert (3);
447 else if (match_byte (4))
450 char *var, *vallab, *value;
452 match_byte_assert (0x58);
454 vallab = get_string ();
456 if (!match_byte(1) && !match_byte(2))
457 match_byte_assert (3);
458 value = get_string ();
459 fprintf (stream, "<string-datum value=\"%s\" format=\"%s%d.%d\"",
460 value, format_to_string(format >> 16), (format >> 8) & 0xff, format & 0xff);
462 fprintf (stream, " variable=\"%s\"", var);
464 fprintf (stream, " label=\"%s\"/>\n", vallab);
465 fprintf (stream, "/>\n");
467 else if (match_byte (1))
472 dump_value_31(stream);
474 value = get_double ();
475 fprintf (stream, "<number value=\"%.*g\" format=\"%s%d.%d\"/>\n",
476 DBL_DIG, value, format_to_string(format >> 16), (format >> 8) & 0xff, format & 0xff);
482 dump_value_31(stream);
484 char *base = get_string();
486 fprintf (stream, "<template format=\"%s\">\n", base);
487 for (int i = 0; i < x; i++)
494 for (int j = 0; j <= level + 1; j++)
495 fprintf (stream, " ");
496 fprintf (stream, "<substitution index=\"%d\">\n", i + 1);
497 for (int j = 0; j < y; j++)
498 dump_value (stream, level + 2, false);
499 for (int j = 0; j <= level + 1; j++)
500 fprintf (stream, " ");
501 fprintf (stream, "</substitution>\n");
503 for (int j = 0; j <= level; j++)
504 fprintf (stream, " ");
505 fprintf (stream, "</template>\n");
510 compare_int(const void *a_, const void *b_)
514 return *a < *b ? -1 : *a > *b;
518 check_permutation(int *a, int n, const char *name)
521 memcpy(b, a, n * sizeof *a);
522 qsort(b, n, sizeof *b, compare_int);
523 for (int i = 0; i < n; i++)
526 fprintf(stderr, "bad %s permutation:", name);
527 for (int i = 0; i < n; i++)
528 fprintf(stderr, " %d", a[i]);
535 dump_category(int level, int *indexes, int *n_indexes)
537 for (int i = 0; i <= level; i++)
538 fprintf (stdout, " ");
539 printf ("<category>\n");
540 dump_value (stdout, level + 1, true);
547 else if (match_byte (1))
551 match_u32_assert (1);
554 else if (!match_u32(2))
555 match_u32_assert (0);
557 int indx = get_u32();
558 int n_categories = get_u32();
561 if (n_categories != 0)
563 fprintf(stderr, "index not -1 but subcategories\n");
566 indexes[(*n_indexes)++] = indx;
568 if (n_categories == 0)
570 for (int i = 0; i <= level + 1; i++)
571 fprintf (stdout, " ");
572 fprintf (stdout, "<category-index>%d</category-index>\n", indx);
574 for (int i = 0; i < n_categories; i++)
575 dump_category (level + 1, indexes, n_indexes);
576 for (int i = 0; i <= level; i++)
577 fprintf (stdout, " ");
578 printf ("</category>\n");
586 printf ("<dimension index=\"%d\">\n", indx);
587 dump_value (stdout, 0, false);
589 /* This byte is usually 0x02 but 0x00 and 0x75 (!) have also been spotted. */
592 if (!match_byte(0) && !match_byte(1))
593 match_byte_assert(2);
597 match_byte_assert(1);
599 match_byte_assert(1);
600 match_byte_assert(1);
601 match_u32_assert(indx);
602 n_categories = get_u32();
606 for (int i = 0; i < n_categories; i++)
607 dump_category (0, indexes, &n_indexes);
608 check_permutation(indexes, n_indexes, "categories");
610 fprintf (stdout, "</dimension>\n");
618 for (int i = 0; i < n_dims; i++)
625 /* The first three numbers add to the number of dimensions. */
628 match_u32_assert(n_dims - t);
630 /* The next n_dims numbers are a permutation of the dimension numbers. */
632 for (int i = 0; i < n_dims; i++)
634 check_permutation(a, n_dims, "dimensions");
638 for (int i = 0; i < x; i++)
640 printf (" <datum index=\"%d\">\n", get_u32());
642 dump_value(stdout, 1, false);
643 fprintf (stdout, " </datum>\n");
645 printf ("</data>\n");
652 printf ("<title-local>\n");
653 dump_value(stdout, 0, true);
654 printf ("</title-local>\n");
656 printf ("<subtype>\n");
657 dump_value(stdout, 0, true);
658 printf ("</subtype>\n");
660 match_byte_assert(0x31);
662 printf ("<title-c>\n");
663 dump_value(stdout, 0, true);
664 printf ("</title-c>\n");
667 match_byte_assert(0x58);
668 if (match_byte(0x31))
670 printf ("<caption>\n");
671 dump_value(stdout, 0, false);
672 printf ("</caption>\n");
675 match_byte_assert(0x58);
678 int n_footnotes = get_u32();
679 for (int i = 0; i < n_footnotes; i++)
681 printf ("<footnote index=\"%d\">\n", i);
682 dump_value(stdout, 0, false);
683 if (match_byte (0x31))
685 /* Custom footnote marker string. */
686 match_byte_assert(3);
688 match_byte_assert(0x58);
693 match_byte_assert (0x58);
694 printf("(%d)\n", get_u32());
695 printf ("</footnote>\n");
703 for (int i = 1; i <= 8; i++)
705 printf ("<style index=\"%d\"", i);
706 match_byte_assert(i);
707 match_byte_assert(0x31);
708 printf(" font=\"%s\"", get_string());
709 match_byte_assert(0);
710 match_byte_assert(0);
711 if (!match_byte(0x40) && !match_byte(0x20) && !match_byte(0x80) && !match_byte(0x10))
712 match_byte_assert(0x50);
713 if (!match_byte(0x41))
714 match_byte_assert(0x51);
717 match_byte_assert(0);
719 /* OK, this seems really unlikely to be totally correct, but it matches my corpus... */
720 if (!match_u32(0) && !match_u32(2))
721 match_u32_assert(0xfaad);
723 if (!match_u32(0) && !match_u32(1) && !match_u32(2))
725 printf (" fgcolor=\"%s\"", get_string());
726 printf (" bgcolor=\"%s\"", get_string());
729 match_byte_assert(0);
731 /* These seem unlikely to be correct too. */
736 match_u32_assert(11);
742 if (!match_u32(-1) && !match_u32(8))
743 match_u32_assert(24);
744 if (!match_u32(-1) && !match_u32(2))
748 /* Who knows? Ranges from -1 to 8 with no obvious pattern. */
754 match_u32_assert(240);
757 match_u32_assert(18);
764 match_u32_assert(142);
768 int count = get_u32();
771 printf ("<encoding>%s</encoding>\n", get_string ());
774 match_u32_assert(UINT32_MAX);
776 match_byte_assert(1);
777 match_byte_assert(0);
779 match_byte_assert(1);
780 if (!match_byte(0x97) && !match_byte(0x98) && !match_byte(0x99))
781 match_byte_assert(0x9a);
782 match_byte_assert(7);
783 match_byte_assert(0);
784 match_byte_assert(0);
786 match_byte_assert(',');
789 match_byte_assert(',');
790 if (!match_byte('.'))
791 match_byte_assert(' ');
794 for (int i = 0; i < 5; i++)
800 main(int argc, char *argv[])
805 if (isatty(STDIN_FILENO))
807 fprintf(stderr, "redirect stdin from a .bin file\n");
810 if (fstat(STDIN_FILENO, &s))
822 if (read(STDIN_FILENO, data, n) != n)
830 if (!strcmp(argv[1], "title0"))
833 if (match_byte (0x03)
834 || (match_byte (0x05) && match_byte (0x58)))
835 printf ("%s\n", get_string());
837 printf ("<unknown>\n");
840 else if (!strcmp(argv[1], "title"))
845 else if (!strcmp(argv[1], "titleraw"))
847 const char fonts[] = "\x01\x31\x09\0\0\0SansSerif";
849 n = find(fonts, sizeof fonts - 1);
851 else if (!strcmp(argv[1], "fonts"))
853 const char fonts[] = "\x01\x31\x09\0\0\0SansSerif";
854 const char styles[] = "\xf0\0\0\0";
855 start = find(fonts, sizeof fonts - 1);
856 n = find(styles, sizeof styles - 1);
858 else if (!strcmp(argv[1], "styles"))
860 const char styles[] = "\xf0\0\0\0";
861 const char dimensions[] = "-,,,.\0";
862 start = find(styles, sizeof styles - 1);
863 n = find(dimensions, sizeof dimensions - 1) + sizeof dimensions - 1;
865 else if (!strcmp(argv[1], "dimensions") || !strcmp(argv[1], "all"))
868 match_byte_assert(1);
869 match_byte_assert(0);
871 match_byte_assert(1);
873 match_byte_assert(1);
874 match_byte_assert(0);
875 match_byte_assert(0);
877 match_byte_assert(1);
879 match_byte_assert(0);
880 match_byte_assert(0);
881 match_byte_assert(0);
889 fprintf (stderr, "%x / %x\n", pos, n);
896 fprintf (stderr, "unknown section %s\n", argv[1]);
903 dump_raw(stdout, start, n);