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");
250 dump_nested_string(void)
254 match_byte_assert (0);
255 match_byte_assert (0);
256 int outer_end = get_end();
257 s = dump_counted_string();
258 match_byte_assert(0x58);
259 match_byte_assert(0x58);
260 if (pos != outer_end)
262 fprintf(stderr, "outer end discrepancy\n");
270 dump_optional_value(FILE *stream)
272 if (match_byte (0x31))
278 /* Corpus frequencies:
286 match_u32_assert (0);
290 /* We only have one SPV file for this version (with many
297 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))
298 match_u32_assert(10);
304 int outer_end = get_end();
306 /* This counted-string appears to be a template string,
307 e.g. "Design\: [:^1:]1 Within Subjects Design\: [:^1:]2". */
308 dump_counted_string();
310 if (match_byte(0x31))
312 /* Only one example in the corpus. */
317 match_byte_assert(1);
318 get_string(); /* foreground */
319 get_string(); /* background */
320 get_string(); /* font */
322 match_byte_assert(12); /* size? */
325 match_byte_assert(0x58);
326 if (match_byte(0x31))
328 /* Only two SPV files have anything like this, so it's hard to
334 match_byte_assert(1);
335 match_byte_assert(0);
336 if (!match_byte(8) && !match_byte(1))
337 match_byte_assert(2);
338 match_byte_assert(0);
339 match_byte_assert(8);
340 match_byte_assert(0);
341 match_byte_assert(10);
342 match_byte_assert(0);
345 match_byte_assert(0x58);
346 if (pos != outer_end)
348 fprintf(stderr, "outer end discrepancy\n");
352 else if (match_u32 (1))
354 fprintf(stream, "(footnote %d) ", get_u32());
355 dump_nested_string();
357 else if (match_u32 (2))
359 fprintf(stream, "(special 2)");
361 match_byte_assert(2);
362 match_byte_assert(0);
363 if (!match_u32 (2) && !match_u32(1))
365 dump_nested_string();
370 fprintf(stream, "(special 3)");
371 match_byte_assert(0);
372 match_byte_assert(0);
373 match_byte_assert(1);
374 match_byte_assert(0);
376 dump_nested_string(); /* Our corpus doesn't contain any examples with strings though. */
380 match_byte_assert (0x58);
384 format_to_string (int type)
390 case 2: return "AHEX";
391 case 3: return "COMMA";
392 case 4: return "DOLLAR";
393 case 5: case 40: return "F";
395 case 7: return "PIBHEX";
397 case 9: return "PIB";
398 case 10: return "PK";
399 case 11: return "RB";
400 case 12: return "RBHEX";
404 case 20: return "DATE";
405 case 21: return "TIME";
406 case 22: return "DATETIME";
407 case 23: return "ADATE";
408 case 24: return "JDATE";
409 case 25: return "DTIME";
410 case 26: return "WKDAY";
411 case 27: return "MONTH";
412 case 28: return "MOYR";
413 case 29: return "QYR";
414 case 30: return "WKYR";
415 case 31: return "PCT";
416 case 32: return "DOT";
417 case 33: return "CCA";
418 case 34: return "CCB";
419 case 35: return "CCC";
420 case 36: return "CCD";
421 case 37: return "CCE";
422 case 38: return "EDATE";
423 case 39: return "SDATE";
426 sprintf(tmp, "<%d>", type);
432 dump_value(FILE *stream, int level)
439 for (int i = 0; i <= level; i++)
440 fprintf (stream, " ");
447 dump_optional_value(stream);
449 value = get_double ();
450 fprintf (stream, "<number value=\"%.*g\" format=\"%s%d.%d\"/>\n",
451 DBL_DIG, value, format_to_string(format >> 16), (format >> 8) & 0xff, format & 0xff);
453 else if (match_byte (2))
459 dump_optional_value (stream);
461 value = get_double ();
463 vallab = get_string ();
464 fprintf (stream, "<numeric-datum value=\"%.*g\" format=\"%s%d.%d\"",
465 DBL_DIG, value, format_to_string(format >> 16), (format >> 8) & 0xff, format & 0xff);
467 fprintf (stream, " variable=\"%s\"", var);
469 fprintf (stream, " label=\"%s\"/>\n", vallab);
470 fprintf (stream, "/>\n");
471 if (!match_byte (1) && !match_byte(2))
472 match_byte_assert (3);
474 else if (match_byte (3))
476 char *text = get_string();
477 dump_optional_value(stream);
478 char *identifier = get_string();
479 char *text_eng = get_string();
480 fprintf (stream, "<string c=\"%s\"", text_eng);
482 fprintf (stream, " identifier=\"%s\"", identifier);
483 if (strcmp(text_eng, text))
484 fprintf (stream, " local=\"%s\"", text);
485 fprintf (stream, "/>\n");
487 match_byte_assert(1);
489 else if (match_byte (4))
492 char *var, *vallab, *value;
494 dump_optional_value(stream);
496 vallab = get_string ();
498 if (!match_byte(1) && !match_byte(2))
499 match_byte_assert (3);
500 value = get_string ();
501 fprintf (stream, "<string-datum value=\"%s\" format=\"%s%d.%d\"",
502 value, format_to_string(format >> 16), (format >> 8) & 0xff, format & 0xff);
504 fprintf (stream, " variable=\"%s\"", var);
506 fprintf (stream, " label=\"%s\"/>\n", vallab);
507 fprintf (stream, "/>\n");
509 else if (match_byte (5))
511 dump_optional_value(stream);
512 char *name = get_string ();
513 char *label = get_string ();
514 fprintf (stream, "<variable name=\"%s\"", name);
516 fprintf (stream, " label=\"%s\"", label);
517 fprintf (stream, "/>\n");
518 if (!match_byte(1) && !match_byte(2))
519 match_byte_assert(3);
523 dump_optional_value(stream);
525 char *base = get_string();
527 fprintf (stream, "<template format=\"%s\">\n", base);
528 for (int i = 0; i < x; i++)
535 for (int j = 0; j <= level + 1; j++)
536 fprintf (stream, " ");
537 fprintf (stream, "<substitution index=\"%d\">\n", i + 1);
538 for (int j = 0; j < y; j++)
539 dump_value (stream, level + 2);
540 for (int j = 0; j <= level + 1; j++)
541 fprintf (stream, " ");
542 fprintf (stream, "</substitution>\n");
544 for (int j = 0; j <= level; j++)
545 fprintf (stream, " ");
546 fprintf (stream, "</template>\n");
551 compare_int(const void *a_, const void *b_)
555 return *a < *b ? -1 : *a > *b;
559 check_permutation(int *a, int n, const char *name)
562 memcpy(b, a, n * sizeof *a);
563 qsort(b, n, sizeof *b, compare_int);
564 for (int i = 0; i < n; i++)
567 fprintf(stderr, "bad %s permutation:", name);
568 for (int i = 0; i < n; i++)
569 fprintf(stderr, " %d", a[i]);
576 dump_category(FILE *stream, int level, int *indexes, int *n_indexes, int max_indexes)
578 for (int i = 0; i <= level; i++)
579 fprintf (stream, " ");
580 printf ("<category>\n");
581 dump_value (stream, level + 1);
583 int merge = data[pos];
585 match_byte_assert (1);
587 match_byte_assert (0);
589 int unindexed = data[pos];
591 match_byte_assert (1);
596 match_u32_assert (2);
598 int indx = get_u32();
599 int n_categories = get_u32();
604 for (int i = 0; i <= level + 1; i++)
605 fprintf (stream, " ");
606 fprintf (stream, "<merge/>\n");
613 fprintf(stderr, "index not -1 but merged\n");
618 fprintf(stderr, "index not -1 but x != 2\n");
621 if (n_categories != 0)
623 fprintf(stderr, "index not -1 but subcategories\n");
626 if (*n_indexes >= max_indexes)
628 fprintf(stderr, "too many categories (increase max_indexes)\n");
631 indexes[(*n_indexes)++] = indx;
634 int expected_unindexed = indx == -1;
635 if (unindexed != expected_unindexed)
637 fprintf(stderr, "unindexed (%d) mismatch with indx (%d)\n",
642 if (n_categories == 0)
644 for (int i = 0; i <= level + 1; i++)
645 fprintf (stream, " ");
646 fprintf (stream, "<category-index>%d</category-index>\n", indx);
648 for (int i = 0; i < n_categories; i++)
649 dump_category (stream, level + 1, indexes, n_indexes, max_indexes);
650 for (int i = 0; i <= level; i++)
651 fprintf (stream, " ");
652 printf ("</category>\n");
660 printf ("<dimension index=\"%d\">\n", indx);
661 dump_value (stdout, 0);
663 /* This byte is usually 0 but many other values have been spotted. */
666 if (!match_byte(0) && !match_byte(1))
667 match_byte_assert(2);
671 match_byte_assert(1);
673 match_byte_assert(1);
674 match_byte_assert(1);
675 if (!match_u32(UINT32_MAX))
676 match_u32_assert(indx);
677 n_categories = get_u32();
681 for (int i = 0; i < n_categories; i++)
682 dump_category (stdout, 0, indexes, &n_indexes, sizeof indexes / sizeof *indexes);
683 check_permutation(indexes, n_indexes, "categories");
685 fprintf (stdout, "</dimension>\n");
690 static int dim_n_cats[64];
691 #define MAX_DIMS (sizeof dim_n_cats / sizeof *dim_n_cats)
697 assert(n_dims < MAX_DIMS);
698 for (int i = 0; i < n_dims; i++)
699 dim_n_cats[i] = dump_dim (i);
705 /* The first three numbers add to the number of dimensions. */
708 int c = n_dims - l - r;
711 /* The next n_dims numbers are a permutation of the dimension numbers. */
713 for (int i = 0; i < n_dims; i++)
718 const char *name = i < l ? "layer" : i < l + r ? "row" : "column";
719 printf ("<%s dimension=\"%d\"/>\n", name, dim);
721 check_permutation(a, n_dims, "dimensions");
725 for (int i = 0; i < x; i++)
727 unsigned int indx = get_u32();
728 printf (" <datum index=\"%d\" coords=", indx);
730 int coords[MAX_DIMS];
731 for (int i = n_dims; i-- > 0; )
733 coords[i] = indx % dim_n_cats[i];
734 indx /= dim_n_cats[i];
736 for (int i = 0; i < n_dims; i++)
737 printf("%c%d", i ? ',' : '"', coords[i]);
743 dump_value(stdout, 1);
744 fprintf (stdout, " </datum>\n");
746 printf ("</data>\n");
752 printf ("<title-local>\n");
753 dump_value(stdout, 0);
755 printf ("</title-local>\n");
757 printf ("<subtype>\n");
758 dump_value(stdout, 0);
760 printf ("</subtype>\n");
762 match_byte_assert(0x31);
764 printf ("<title-c>\n");
765 dump_value(stdout, 0);
767 printf ("</title-c>\n");
770 match_byte_assert(0x58);
771 if (match_byte(0x31))
773 printf ("<caption>\n");
774 dump_value(stdout, 0);
775 printf ("</caption>\n");
778 match_byte_assert(0x58);
780 int n_footnotes = get_u32();
781 for (int i = 0; i < n_footnotes; i++)
783 printf ("<footnote index=\"%d\">\n", i);
784 dump_value(stdout, 0);
785 /* Custom footnote marker string. */
786 if (match_byte (0x31))
787 dump_value(stdout, 0);
789 match_byte_assert (0x58);
791 printf ("</footnote>\n");
795 static void __attribute__((unused))
796 hex_dump(int ofs, int n)
798 for (int i = 0; i < n; i++)
800 int c = data[ofs + i];
803 fprintf(stderr, "-");
805 fprintf(stderr, " ");
807 fprintf(stderr, "%02x", c);
808 //fprintf(stderr, "%c", c >= 32 && c < 127 ? c : '.');
810 fprintf(stderr, "\n");
817 for (int i = 1; i <= 8; i++)
819 printf ("<style index=\"%d\"", i);
820 match_byte_assert(i);
821 match_byte_assert(0x31);
822 printf(" font=\"%s\"", get_string());
823 match_byte_assert(0);
824 match_byte_assert(0);
825 if (!match_byte(0x40) && !match_byte(0x20) && !match_byte(0x80) && !match_byte(0x10) && !match_byte(0x70))
826 match_byte_assert(0x50);
827 match_byte_assert(0x41);
828 if (!match_u32(0) && !match_u32(1))
830 match_byte_assert(0);
832 /* OK, this seems really unlikely to be totally correct, but it matches my corpus... */
833 if (!match_u32(0) && !match_u32(2))
836 match_u32_assert(0xfaad);
841 if (!match_u32(0) && !match_u32(1) && !match_u32(2))
843 printf (" fgcolor=\"%s\"", get_string());
844 printf (" bgcolor=\"%s\"", get_string());
847 match_byte_assert(0);
855 if (!match_u32(10) && !match_u32(11) && !match_u32(5))
863 if (!match_u32(-1) && !match_u32(8))
864 match_u32_assert(24);
865 if (!match_u32(-1) && !match_u32(2))
869 /* Who knows? Ranges from -1 to 8 with no obvious pattern. */
876 match_u32_assert(240);
879 match_u32_assert(18);
884 else if (match_u32(142))
886 else if (match_u32(143))
888 else if (match_u32(150))
892 match_u32_assert(16);
896 int count = get_u32();
899 const char *locale = get_string();
900 printf ("<locale>%s</locale>\n", locale);
903 match_u32_assert(UINT32_MAX);
905 match_byte_assert(1);
906 match_byte_assert(0);
908 match_byte_assert(1);
911 if (!match_byte(0x97) && !match_byte(0x98) && !match_byte(0x99))
912 match_byte_assert(0x9a);
913 match_byte_assert(7);
914 match_byte_assert(0);
915 match_byte_assert(0);
918 match_u32_assert(UINT32_MAX);
920 int decimal = data[pos];
921 int grouping = data[pos + 1];
924 if (!match_byte(',') && !match_byte('\''))
925 match_byte_assert(' ');
929 match_byte_assert(',');
930 if (!match_byte('.') && !match_byte(' '))
931 match_byte_assert(0);
933 printf("<format decimal=\"%c\" grouping=\"", decimal);
939 for (int i = 0; i < 5; i++)
940 printf("<CC%c>%s</CC%c>\n", 'A' + i, get_string(), 'A' + i);
945 /* The last chunk is an outer envelope that contains two inner envelopes.
946 The second inner envelope has some interesting data like the encoding and
950 int outer_end = get_end();
952 /* First inner envelope: byte*33 int[n] int*[n]. */
955 /* Second inner envelope. */
956 assert(get_end() == outer_end);
958 match_byte_assert(1);
959 match_byte_assert(0);
961 match_byte_assert(4);
962 match_byte_assert(0);
963 match_byte_assert(0);
964 match_byte_assert(0);
966 printf("<command>%s</command>\n", get_string());
967 printf("<subcommand>%s</subcommand>\n", get_string());
968 printf("<language>%s</language>\n", get_string());
969 printf("<charset>%s</charset>\n", get_string());
970 printf("<locale>%s</locale>\n", get_string());
973 match_byte_assert(1);
974 match_byte_assert(0);
976 match_byte_assert(1);
978 match_byte_assert(1);
980 if (!match_byte(0x97) && !match_byte(0x98) && !match_byte(0x99))
981 match_byte_assert(0x9a);
982 match_byte_assert(7);
983 match_byte_assert(0);
984 match_byte_assert(0);
988 if (!match_byte(',') && !match_byte('\''))
989 match_byte_assert(' ');
993 match_byte_assert(',');
994 if (!match_byte('.') && !match_byte(' '))
995 match_byte_assert(0);
999 match_byte_assert(1);
1001 if (outer_end - pos > 6)
1003 /* There might be a pair of strings representing a dataset and
1004 datafile name, or there might be a set of custom currency strings.
1005 The custom currency strings start with a pair of integers, so we
1006 can distinguish these from a string by checking for a null byte; a
1007 small 32-bit integer will always contain a null and a text string
1010 int len = get_u32();
1011 bool has_dataset = !memchr(&data[pos], '\0', len);
1016 printf("<dataset>%s</dataset>\n", get_string());
1017 printf("<datafile>%s</datafile>\n", get_string());
1019 match_u32_assert(0);
1021 match_u32_assert(0);
1027 for (int i = 0; i < 5; i++)
1028 printf("<CC%c>%s</CC%c>\n", 'A' + i, get_string(), 'A' + i);
1031 match_u32_assert(0);
1033 match_byte_assert(0x2e);
1035 match_byte_assert(1);
1037 assert(pos == outer_end);
1042 match_u32_assert(0);
1046 main(int argc, char *argv[])
1051 if (isatty(STDIN_FILENO))
1053 fprintf(stderr, "redirect stdin from a .bin file\n");
1056 if (fstat(STDIN_FILENO, &s))
1068 if (read(STDIN_FILENO, data, n) != n)
1076 fprintf (stderr, "usage: %s TYPE < .bin", argv[0]);
1080 if (!strcmp(argv[1], "title0"))
1083 if (match_byte (0x03)
1084 || (match_byte (0x05) && match_byte (0x58)))
1085 printf ("%s\n", get_string());
1087 printf ("<unknown>\n");
1090 else if (!strcmp(argv[1], "title"))
1096 else if (!strcmp(argv[1], "titleraw"))
1098 const char fonts[] = "\x01\x31\x09\0\0\0SansSerif";
1100 n = find(fonts, sizeof fonts - 1);
1102 else if (!strcmp(argv[1], "fonts"))
1104 const char fonts[] = "\x01\x31\x09\0\0\0SansSerif";
1105 const char styles[] = "\xf0\0\0\0";
1106 start = find(fonts, sizeof fonts - 1);
1107 n = find(styles, sizeof styles - 1);
1109 else if (!strcmp(argv[1], "styles"))
1111 const char styles[] = "\xf0\0\0\0";
1112 const char dimensions[] = "-,,,.\0";
1113 start = find(styles, sizeof styles - 1);
1114 n = find(dimensions, sizeof dimensions - 1) + sizeof dimensions - 1;
1116 else if (!strcmp(argv[1], "dimensions") || !strcmp(argv[1], "all"))
1119 match_byte_assert(1);
1120 match_byte_assert(0);
1122 /* This might be a version number of some kind, because value 1 seems
1123 to only appear in an SPV file that also required its own weird
1124 special cases in dump_optional_value(). */
1125 version = get_u32();
1128 match_u32_assert(3);
1130 match_byte_assert(1);
1132 match_byte_assert(1);
1135 match_byte_assert(0);
1136 match_byte_assert(0);
1138 match_byte_assert(1);
1142 match_byte_assert(0);
1143 match_byte_assert(0);
1144 match_byte_assert(0);
1149 match_byte_assert(1);
1150 match_byte_assert(0);
1151 match_byte_assert(0);
1156 match_byte_assert(1);
1157 match_byte_assert(0);
1158 match_byte_assert(0);
1163 match_byte_assert(1);
1164 match_byte_assert(0);
1165 match_byte_assert(0);
1170 match_byte_assert(0);
1171 match_byte_assert(0);
1175 This is the tableId, e.g. -4154297861994971133 would be 0xdca00003.
1176 We don't have enough context to validate it. */
1189 fprintf (stderr, "%x / %x\n", pos, n);
1194 else if (!strcmp(argv[1], "raw"))
1198 dump_raw(stdout, start, n);
1202 fprintf (stderr, "unknown section %s\n", argv[1]);