10 #include "u8-mbtouc.h"
18 all_ascii(const uint8_t *p, size_t n)
20 for (size_t i = 0; i < n; i++)
21 if (p[i] < 32 || p[i] > 126)
27 try_find(const char *target, size_t target_len)
29 const uint8_t *pos = (const uint8_t *) memmem (data, n, target, target_len);
30 return pos ? pos - data : 0;
34 find(const char *target, size_t target_len)
36 size_t pos = try_find(target, target_len);
39 fprintf (stderr, "not found\n");
48 #define STR(x) XSTR(x)
49 #define WHERE __FILE__":" STR(__LINE__)
55 memcpy(&x, &data[pos], 4);
64 memcpy(&x, &data[pos], 8);
79 match_u32_assert(uint32_t x, const char *where)
81 unsigned int y = get_u32();
84 fprintf(stderr, "%s: 0x%x: expected i%u, got i%u\n", where, pos - 4, x, y);
88 #define match_u32_assert(x) match_u32_assert(x, WHERE)
93 if (pos < n && data[pos] == b)
103 match_byte_assert(uint8_t b, const char *where)
107 fprintf(stderr, "%s: 0x%x: expected %02x, got %02x\n", where, pos, b, data[pos]);
111 #define match_byte_assert(b) match_byte_assert(b, WHERE)
114 newline(FILE *stream, int pos)
116 fprintf(stream, "\n%08x: ", pos);
120 dump_raw(FILE *stream, int start, int end)
122 for (size_t i = start; i < end; )
129 && i + 4 + data[i] + data[i + 1] * 256 <= end
130 && all_ascii(&data[i + 4], data[i] + data[i + 1] * 256))
133 fprintf(stream, "\"");
134 fwrite(&data[i + 4], 1, data[i] + data[i + 1] * 256, stream);
135 fputs("\" ", stream);
137 i += 4 + data[i] + data[i + 1] * 256;
139 else if (i + 12 <= end
146 memcpy (&d, &data[i + 4], 8);
147 fprintf (stream, "F40.%d(%.*f)", data[i], data[i], d);
151 else if (i + 12 <= end
158 memcpy (&d, &data[i + 4], 8);
159 fprintf (stream, "PCT40.%d(%.*f)", data[i], data[i], d);
163 else if (i + 4 <= end
164 && (data[i] && data[i] != 88 && data[i] != 0x41)
169 fprintf (stream, "i%d ", data[i]);
174 fprintf(stream, "%02x ", data[i]);
182 static bool __attribute__((unused))
183 all_utf8(const char *p_)
185 const uint8_t *p = (const uint8_t *) p_;
186 size_t len = strlen ((char *) p);
187 for (size_t ofs = 0, mblen; ofs < len; ofs += mblen)
191 mblen = u8_mbtouc (&uc, p + ofs, len - ofs);
192 if ((uc < 32 && uc != '\n') || uc == 127 || uc == 0xfffd)
199 get_string(const char *where)
202 /*data[pos + 1] == 0 && data[pos + 2] == 0 && data[pos + 3] == 0*/
203 /*&& all_ascii(&data[pos + 4], data[pos])*/)
205 int len = data[pos] + data[pos + 1] * 256;
206 char *s = malloc(len + 1);
208 memcpy(s, &data[pos + 4], len);
215 fprintf(stderr, "%s: 0x%x: expected string\n", where, pos);
219 #define get_string() get_string(WHERE)
229 dump_counted_string(void)
232 int inner_end = get_end();
233 if (pos != inner_end)
236 if (match_byte(0x31))
239 match_byte_assert(0x58);
240 if (pos != inner_end)
242 fprintf(stderr, "inner end discrepancy\n");
249 static void __attribute__((unused))
250 hex_dump(int ofs, int n)
252 for (int i = 0; i < n; i++)
254 int c = data[ofs + i];
257 fprintf(stderr, "-");
259 fprintf(stderr, " ");
261 fprintf(stderr, "%02x", c);
262 //fprintf(stderr, "%c", c >= 32 && c < 127 ? c : '.');
264 fprintf(stderr, "\n");
274 match_byte_assert(1);
275 get_string(); /* foreground */
276 get_string(); /* background */
277 get_string(); /* font */
279 match_byte_assert(12); /* size? */
283 dump_nested_string(void)
287 match_byte_assert (0);
288 match_byte_assert (0);
289 int outer_end = get_end();
290 s = dump_counted_string();
291 if (match_byte(0x31))
294 match_byte_assert(0x58);
295 match_byte_assert(0x58);
296 if (pos != outer_end)
298 fprintf(stderr, "outer end discrepancy\n");
306 dump_optional_value(FILE *stream)
308 if (match_byte (0x31))
314 /* Corpus frequencies:
322 match_u32_assert (0);
326 /* We only have one SPV file for this version (with many
333 if (!match_u32(0) && !match_u32(1) && !match_u32(2) && !match_u32(3) && !match_u32(4) && !match_u32(5) && !match_u32(6) && !match_u32(7) && !match_u32(8) && !match_u32(9))
334 match_u32_assert(10);
340 int outer_end = get_end();
342 /* This counted-string appears to be a template string,
343 e.g. "Design\: [:^1:]1 Within Subjects Design\: [:^1:]2". */
344 dump_counted_string();
346 if (match_byte(0x31))
349 match_byte_assert(0x58);
350 if (match_byte(0x31))
352 /* Only two SPV files have anything like this, so it's hard to
358 match_byte_assert(1);
359 match_byte_assert(0);
360 if (!match_byte(8) && !match_byte(1))
361 match_byte_assert(2);
362 match_byte_assert(0);
363 match_byte_assert(8);
364 match_byte_assert(0);
365 match_byte_assert(10);
366 match_byte_assert(0);
369 match_byte_assert(0x58);
370 if (pos != outer_end)
372 fprintf(stderr, "outer end discrepancy\n");
376 else if (match_u32 (1))
378 fprintf(stream, "(footnote %d) ", get_u32());
379 dump_nested_string();
381 else if (match_u32 (2))
383 fprintf(stream, "(special 2)");
384 if (!match_byte(0) && !match_byte(1))
385 match_byte_assert(2);
386 match_byte_assert(0);
387 if (!match_u32 (2) && !match_u32(1))
389 dump_nested_string();
394 fprintf(stream, "(special 3)");
395 match_byte_assert(0);
396 match_byte_assert(0);
397 match_byte_assert(1);
398 match_byte_assert(0);
400 dump_nested_string(); /* Our corpus doesn't contain any examples with strings though. */
404 match_byte_assert (0x58);
408 format_to_string (int type)
414 case 2: return "AHEX";
415 case 3: return "COMMA";
416 case 4: return "DOLLAR";
417 case 5: case 40: return "F";
419 case 7: return "PIBHEX";
421 case 9: return "PIB";
422 case 10: return "PK";
423 case 11: return "RB";
424 case 12: return "RBHEX";
428 case 20: return "DATE";
429 case 21: return "TIME";
430 case 22: return "DATETIME";
431 case 23: return "ADATE";
432 case 24: return "JDATE";
433 case 25: return "DTIME";
434 case 26: return "WKDAY";
435 case 27: return "MONTH";
436 case 28: return "MOYR";
437 case 29: return "QYR";
438 case 30: return "WKYR";
439 case 31: return "PCT";
440 case 32: return "DOT";
441 case 33: return "CCA";
442 case 34: return "CCB";
443 case 35: return "CCC";
444 case 36: return "CCD";
445 case 37: return "CCE";
446 case 38: return "EDATE";
447 case 39: return "SDATE";
450 sprintf(tmp, "<%d>", type);
456 dump_value(FILE *stream, int level)
463 for (int i = 0; i <= level; i++)
464 fprintf (stream, " ");
471 dump_optional_value(stream);
473 value = get_double ();
474 fprintf (stream, "<number value=\"%.*g\" format=\"%s%d.%d\"/>\n",
475 DBL_DIG, value, format_to_string(format >> 16), (format >> 8) & 0xff, format & 0xff);
477 else if (match_byte (2))
483 dump_optional_value (stream);
485 value = get_double ();
487 vallab = get_string ();
488 fprintf (stream, "<numeric-datum value=\"%.*g\" format=\"%s%d.%d\"",
489 DBL_DIG, value, format_to_string(format >> 16), (format >> 8) & 0xff, format & 0xff);
491 fprintf (stream, " variable=\"%s\"", var);
493 fprintf (stream, " label=\"%s\"", vallab);
494 fprintf (stream, "/>\n");
495 if (!match_byte (1) && !match_byte(2))
496 match_byte_assert (3);
498 else if (match_byte (3))
500 char *text = get_string();
501 dump_optional_value(stream);
502 char *identifier = get_string();
503 char *text_eng = get_string();
504 fprintf (stream, "<string c=\"%s\"", text_eng);
506 fprintf (stream, " identifier=\"%s\"", identifier);
507 if (strcmp(text_eng, text))
508 fprintf (stream, " local=\"%s\"", text);
509 fprintf (stream, "/>\n");
511 match_byte_assert(1);
513 else if (match_byte (4))
516 char *var, *vallab, *value;
518 dump_optional_value(stream);
520 vallab = get_string ();
522 if (!match_byte(1) && !match_byte(2))
523 match_byte_assert (3);
524 value = get_string ();
525 fprintf (stream, "<string-datum value=\"%s\" format=\"%s%d.%d\"",
526 value, format_to_string(format >> 16), (format >> 8) & 0xff, format & 0xff);
528 fprintf (stream, " variable=\"%s\"", var);
530 fprintf (stream, " label=\"%s\"/>\n", vallab);
531 fprintf (stream, "/>\n");
533 else if (match_byte (5))
535 dump_optional_value(stream);
536 char *name = get_string ();
537 char *label = get_string ();
538 fprintf (stream, "<variable name=\"%s\"", name);
540 fprintf (stream, " label=\"%s\"", label);
541 fprintf (stream, "/>\n");
542 if (!match_byte(1) && !match_byte(2))
543 match_byte_assert(3);
547 dump_optional_value(stream);
549 char *base = get_string();
551 fprintf (stream, "<template format=\"%s\">\n", base);
552 for (int i = 0; i < x; i++)
559 for (int j = 0; j <= level + 1; j++)
560 fprintf (stream, " ");
561 fprintf (stream, "<substitution index=\"%d\">\n", i + 1);
562 for (int j = 0; j < y; j++)
563 dump_value (stream, level + 2);
564 for (int j = 0; j <= level + 1; j++)
565 fprintf (stream, " ");
566 fprintf (stream, "</substitution>\n");
568 for (int j = 0; j <= level; j++)
569 fprintf (stream, " ");
570 fprintf (stream, "</template>\n");
575 compare_int(const void *a_, const void *b_)
579 return *a < *b ? -1 : *a > *b;
583 check_permutation(int *a, int n, const char *name)
586 memcpy(b, a, n * sizeof *a);
587 qsort(b, n, sizeof *b, compare_int);
588 for (int i = 0; i < n; i++)
591 fprintf(stderr, "bad %s permutation:", name);
592 for (int i = 0; i < n; i++)
593 fprintf(stderr, " %d", a[i]);
600 dump_category(FILE *stream, int level, int *indexes, int *n_indexes, int max_indexes)
602 for (int i = 0; i <= level; i++)
603 fprintf (stream, " ");
604 printf ("<category>\n");
605 dump_value (stream, level + 1);
607 int merge = data[pos];
609 match_byte_assert (1);
611 match_byte_assert (0);
613 int unindexed = data[pos];
615 match_byte_assert (1);
620 match_u32_assert (2);
622 int indx = get_u32();
623 int n_categories = get_u32();
628 for (int i = 0; i <= level + 1; i++)
629 fprintf (stream, " ");
630 fprintf (stream, "<merge/>\n");
637 fprintf(stderr, "index not -1 but merged\n");
642 fprintf(stderr, "index not -1 but x != 2\n");
645 if (n_categories != 0)
647 fprintf(stderr, "index not -1 but subcategories\n");
650 if (*n_indexes >= max_indexes)
652 fprintf(stderr, "too many categories (increase max_indexes)\n");
655 indexes[(*n_indexes)++] = indx;
658 int expected_unindexed = indx == -1;
659 if (unindexed != expected_unindexed)
661 fprintf(stderr, "unindexed (%d) mismatch with indx (%d)\n",
666 if (n_categories == 0)
668 for (int i = 0; i <= level + 1; i++)
669 fprintf (stream, " ");
670 fprintf (stream, "<category-index>%d</category-index>\n", indx);
672 for (int i = 0; i < n_categories; i++)
673 dump_category (stream, level + 1, indexes, n_indexes, max_indexes);
674 for (int i = 0; i <= level; i++)
675 fprintf (stream, " ");
676 printf ("</category>\n");
684 printf ("<dimension index=\"%d\">\n", indx);
685 dump_value (stdout, 0);
687 /* This byte is usually 0 but many other values have been spotted. */
690 if (!match_byte(0) && !match_byte(1))
691 match_byte_assert(2);
695 match_byte_assert(1);
697 match_byte_assert(1);
698 match_byte_assert(1);
699 if (!match_u32(UINT32_MAX))
700 match_u32_assert(indx);
701 n_categories = get_u32();
705 for (int i = 0; i < n_categories; i++)
706 dump_category (stdout, 0, indexes, &n_indexes, sizeof indexes / sizeof *indexes);
707 check_permutation(indexes, n_indexes, "categories");
709 fprintf (stdout, "</dimension>\n");
714 static int dim_n_cats[64];
715 #define MAX_DIMS (sizeof dim_n_cats / sizeof *dim_n_cats)
721 assert(n_dims < MAX_DIMS);
722 for (int i = 0; i < n_dims; i++)
723 dim_n_cats[i] = dump_dim (i);
729 /* The first three numbers add to the number of dimensions. */
732 int c = n_dims - l - r;
735 /* The next n_dims numbers are a permutation of the dimension numbers. */
737 for (int i = 0; i < n_dims; i++)
742 const char *name = i < l ? "layer" : i < l + r ? "row" : "column";
743 printf ("<%s dimension=\"%d\"/>\n", name, dim);
745 check_permutation(a, n_dims, "dimensions");
749 for (int i = 0; i < x; i++)
751 unsigned int indx = get_u32();
752 printf (" <datum index=\"%d\" coords=", indx);
754 int coords[MAX_DIMS];
755 for (int i = n_dims; i-- > 0; )
757 coords[i] = indx % dim_n_cats[i];
758 indx /= dim_n_cats[i];
760 for (int i = 0; i < n_dims; i++)
761 printf("%c%d", i ? ',' : '"', coords[i]);
767 dump_value(stdout, 1);
768 fprintf (stdout, " </datum>\n");
770 printf ("</data>\n");
776 printf ("<title-local>\n");
777 dump_value(stdout, 0);
779 printf ("</title-local>\n");
781 printf ("<subtype>\n");
782 dump_value(stdout, 0);
784 printf ("</subtype>\n");
786 match_byte_assert(0x31);
788 printf ("<title-c>\n");
789 dump_value(stdout, 0);
791 printf ("</title-c>\n");
794 match_byte_assert(0x58);
795 if (match_byte(0x31))
797 printf ("<caption>\n");
798 dump_value(stdout, 0);
799 printf ("</caption>\n");
802 match_byte_assert(0x58);
804 int n_footnotes = get_u32();
805 for (int i = 0; i < n_footnotes; i++)
807 printf ("<footnote index=\"%d\">\n", i);
808 dump_value(stdout, 0);
809 /* Custom footnote marker string. */
810 if (match_byte (0x31))
811 dump_value(stdout, 0);
813 match_byte_assert (0x58);
815 printf ("</footnote>\n");
823 for (int i = 1; i <= 8; i++)
825 printf ("<style index=\"%d\"", i);
826 match_byte_assert(i);
827 match_byte_assert(0x31);
828 printf(" font=\"%s\"", get_string());
829 match_byte_assert(0);
830 match_byte_assert(0);
831 if (!match_byte(0x40) && !match_byte(0x20) && !match_byte(0x80) && !match_byte(0x10) && !match_byte(0x70))
832 match_byte_assert(0x50);
833 match_byte_assert(0x41);
834 if (!match_u32(0) && !match_u32(1))
836 match_byte_assert(0);
838 /* OK, this seems really unlikely to be totally correct, but it matches my corpus... */
839 if (!match_u32(0) && !match_u32(2))
842 match_u32_assert(0xfaad);
847 if (!match_u32(0) && !match_u32(1) && !match_u32(2))
849 printf (" fgcolor=\"%s\"", get_string());
850 printf (" bgcolor=\"%s\"", get_string());
853 match_byte_assert(0);
861 if (!match_u32(10) && !match_u32(11) && !match_u32(5))
863 if (!match_u32(0) && !match_u32(1))
869 if (!match_u32(-1) && !match_u32(8))
870 match_u32_assert(24);
871 if (!match_u32(-1) && !match_u32(2))
875 /* Who knows? Ranges from -1 to 8 with no obvious pattern. */
882 match_u32_assert(240);
885 match_u32_assert(18);
892 int len = data[pos + 0x34];
894 printf("<tablelook>%.*s</tablelook>\n", len, &data[pos + 0x35]);
898 int count = get_u32();
901 const char *locale = get_string();
902 printf ("<locale>%s</locale>\n", locale);
905 match_u32_assert(UINT32_MAX);
907 match_byte_assert(1);
908 match_byte_assert(0);
910 match_byte_assert(1);
913 if (!match_byte(0x97) && !match_byte(0x98)
914 && !match_byte(0x99) && !match_byte(0x9a))
915 match_byte_assert(0x9b);
916 match_byte_assert(7);
917 match_byte_assert(0);
918 match_byte_assert(0);
921 match_u32_assert(UINT32_MAX);
923 int decimal = data[pos];
924 int grouping = data[pos + 1];
927 if (!match_byte(',') && !match_byte('\''))
928 match_byte_assert(' ');
932 match_byte_assert(',');
933 if (!match_byte('.') && !match_byte(' '))
934 match_byte_assert(0);
936 printf("<format decimal=\"%c\" grouping=\"", decimal);
942 for (int i = 0; i < 5; i++)
943 printf("<CC%c>%s</CC%c>\n", 'A' + i, get_string(), 'A' + i);
948 /* The last chunk is an outer envelope that contains two inner envelopes.
949 The second inner envelope has some interesting data like the encoding and
953 int outer_end = get_end();
955 /* First inner envelope: byte*33 int[n] int*[n]. */
958 /* Second inner envelope. */
959 assert(get_end() == outer_end);
961 match_byte_assert(1);
962 match_byte_assert(0);
963 if (!match_byte(3) && !match_byte(4))
964 match_byte_assert(5);
965 match_byte_assert(0);
966 match_byte_assert(0);
967 match_byte_assert(0);
969 printf("<command>%s</command>\n", get_string());
970 printf("<subcommand>%s</subcommand>\n", get_string());
971 printf("<language>%s</language>\n", get_string());
972 printf("<charset>%s</charset>\n", get_string());
973 printf("<locale>%s</locale>\n", get_string());
976 match_byte_assert(1);
977 match_byte_assert(0);
979 match_byte_assert(1);
981 match_byte_assert(1);
983 if (!match_byte(0x97) && !match_byte(0x98)
984 && !match_byte(0x99) && !match_byte(0x9a))
985 match_byte_assert(0x9b);
986 match_byte_assert(7);
987 match_byte_assert(0);
988 match_byte_assert(0);
992 if (!match_byte(',') && !match_byte('\''))
993 match_byte_assert(' ');
997 match_byte_assert(',');
998 if (!match_byte('.') && !match_byte(' '))
999 match_byte_assert(0);
1003 match_byte_assert(1);
1005 if (outer_end - pos > 6)
1007 /* There might be a pair of strings representing a dataset and
1008 datafile name, or there might be a set of custom currency strings.
1009 The custom currency strings start with a pair of integers, so we
1010 can distinguish these from a string by checking for a null byte; a
1011 small 32-bit integer will always contain a null and a text string
1014 int len = get_u32();
1015 bool has_dataset = !memchr(&data[pos], '\0', len);
1020 printf("<dataset>%s</dataset>\n", get_string());
1021 printf("<datafile>%s</datafile>\n", get_string());
1023 match_u32_assert(0);
1025 match_u32_assert(0);
1031 for (int i = 0; i < 5; i++)
1032 printf("<CC%c>%s</CC%c>\n", 'A' + i, get_string(), 'A' + i);
1035 match_u32_assert(0);
1037 match_byte_assert(0x2e);
1039 match_byte_assert(1);
1041 if (pos < outer_end)
1043 match_u32_assert(2000000);
1044 match_u32_assert(0);
1046 assert(pos == outer_end);
1051 match_u32_assert(0);
1055 main(int argc, char *argv[])
1060 if (isatty(STDIN_FILENO))
1062 fprintf(stderr, "redirect stdin from a .bin file\n");
1065 if (fstat(STDIN_FILENO, &s))
1077 if (read(STDIN_FILENO, data, n) != n)
1085 fprintf (stderr, "usage: %s TYPE < .bin", argv[0]);
1089 if (!strcmp(argv[1], "title0"))
1092 if (match_byte (0x03)
1093 || (match_byte (0x05) && match_byte (0x58)))
1094 printf ("%s\n", get_string());
1096 printf ("<unknown>\n");
1099 else if (!strcmp(argv[1], "title"))
1105 else if (!strcmp(argv[1], "titleraw"))
1107 const char fonts[] = "\x01\x31\x09\0\0\0SansSerif";
1109 n = find(fonts, sizeof fonts - 1);
1111 else if (!strcmp(argv[1], "fonts"))
1113 const char fonts[] = "\x01\x31\x09\0\0\0SansSerif";
1114 const char styles[] = "\xf0\0\0\0";
1115 start = find(fonts, sizeof fonts - 1);
1116 n = find(styles, sizeof styles - 1);
1118 else if (!strcmp(argv[1], "styles"))
1120 const char styles[] = "\xf0\0\0\0";
1121 const char dimensions[] = "-,,,.\0";
1122 start = find(styles, sizeof styles - 1);
1123 n = find(dimensions, sizeof dimensions - 1) + sizeof dimensions - 1;
1125 else if (!strcmp(argv[1], "dimensions") || !strcmp(argv[1], "all"))
1128 match_byte_assert(1);
1129 match_byte_assert(0);
1131 /* This might be a version number of some kind, because value 1 seems
1132 to only appear in an SPV file that also required its own weird
1133 special cases in dump_optional_value(). */
1134 version = get_u32();
1137 match_u32_assert(3);
1139 match_byte_assert(1);
1141 match_byte_assert(1);
1144 match_byte_assert(0);
1145 match_byte_assert(0);
1147 match_byte_assert(1);
1151 match_byte_assert(0);
1152 match_byte_assert(0);
1153 match_byte_assert(0);
1158 match_byte_assert(1);
1159 match_byte_assert(0);
1160 match_byte_assert(0);
1165 match_byte_assert(1);
1166 match_byte_assert(0);
1167 match_byte_assert(0);
1172 match_byte_assert(1);
1173 match_byte_assert(0);
1174 match_byte_assert(0);
1179 match_byte_assert(0);
1180 match_byte_assert(0);
1184 This is the tableId, e.g. -4154297861994971133 would be 0xdca00003.
1185 We don't have enough context to validate it. */
1198 fprintf (stderr, "%x / %x\n", pos, n);
1203 else if (!strcmp(argv[1], "raw"))
1207 dump_raw(stdout, start, n);
1211 fprintf (stderr, "unknown section %s\n", argv[1]);