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], 2);
73 memcpy(&x, &data[pos], 8);
88 match_u32_assert(uint32_t x, const char *where)
90 unsigned int y = get_u32();
93 fprintf(stderr, "%s: 0x%x: expected i%u, got i%u\n", where, pos - 4, x, y);
97 #define match_u32_assert(x) match_u32_assert(x, WHERE)
100 match_byte(uint8_t b)
102 if (pos < n && data[pos] == b)
112 match_byte_assert(uint8_t b, const char *where)
116 fprintf(stderr, "%s: 0x%x: expected %02x, got %02x\n", where, pos, b, data[pos]);
120 #define match_byte_assert(b) match_byte_assert(b, WHERE)
123 newline(FILE *stream, int pos)
125 fprintf(stream, "\n%08x: ", pos);
129 dump_raw(FILE *stream, int start, int end)
131 for (size_t i = start; i < end; )
138 && i + 4 + data[i] + data[i + 1] * 256 <= end
139 && all_ascii(&data[i + 4], data[i] + data[i + 1] * 256))
142 fprintf(stream, "\"");
143 fwrite(&data[i + 4], 1, data[i] + data[i + 1] * 256, stream);
144 fputs("\" ", stream);
146 i += 4 + data[i] + data[i + 1] * 256;
148 else if (i + 12 <= end
155 memcpy (&d, &data[i + 4], 8);
156 fprintf (stream, "F40.%d(%.*f)", data[i], data[i], d);
160 else if (i + 12 <= end
167 memcpy (&d, &data[i + 4], 8);
168 fprintf (stream, "PCT40.%d(%.*f)", data[i], data[i], d);
172 else if (i + 4 <= end
173 && (data[i] && data[i] != 88 && data[i] != 0x41)
178 fprintf (stream, "i%d ", data[i]);
183 fprintf(stream, "%02x ", data[i]);
191 static bool __attribute__((unused))
192 all_utf8(const char *p_)
194 const uint8_t *p = (const uint8_t *) p_;
195 size_t len = strlen ((char *) p);
196 for (size_t ofs = 0, mblen; ofs < len; ofs += mblen)
200 mblen = u8_mbtouc (&uc, p + ofs, len - ofs);
201 if ((uc < 32 && uc != '\n') || uc == 127 || uc == 0xfffd)
208 get_string(const char *where)
211 /*data[pos + 1] == 0 && data[pos + 2] == 0 && data[pos + 3] == 0*/
212 /*&& all_ascii(&data[pos + 4], data[pos])*/)
214 int len = data[pos] + data[pos + 1] * 256;
215 char *s = malloc(len + 1);
217 memcpy(s, &data[pos + 4], len);
224 fprintf(stderr, "%s: 0x%x: expected string\n", where, pos);
228 #define get_string() get_string(WHERE)
237 static void __attribute__((unused))
238 hex_dump(int ofs, int n)
240 for (int i = 0; i < n; i++)
242 int c = data[ofs + i];
245 fprintf(stderr, "-");
247 fprintf(stderr, " ");
249 fprintf(stderr, "%02x", c);
250 //fprintf(stderr, "%c", c >= 32 && c < 127 ? c : '.');
252 fprintf(stderr, "\n");
256 dump_counted_string(void)
259 int inner_end = get_end();
260 if (pos != inner_end)
265 match_byte_assert(0x58);
269 if (match_byte(0x31))
272 match_byte_assert(0x58);
273 if (pos != inner_end)
275 fprintf(stderr, "inner end discrepancy\n");
283 dump_style(FILE *stream)
289 match_byte_assert(1);
290 char *fg = get_string(); /* foreground */
291 char *bg = get_string(); /* background */
292 char *font = get_string(); /* font */
293 int size = data[pos];
295 match_byte_assert(12); /* size? */
296 fprintf(stream, " fgcolor=\"%s\" bgcolor=\"%s\" font=\"%s\" size=\"%d\"",
301 dump_nested_string(FILE *stream)
305 match_byte_assert (0);
306 match_byte_assert (0);
307 int outer_end = get_end();
308 s = dump_counted_string();
310 fprintf(stream, " \"%s\"", s);
311 if (match_byte(0x31))
314 match_byte_assert(0x58);
315 match_byte_assert(0x58);
316 if (pos != outer_end)
318 fprintf(stderr, "outer end discrepancy\n");
326 dump_value_modifier(FILE *stream)
328 if (match_byte (0x31))
332 fprintf(stream, "<special0");
335 /* Corpus frequencies:
340 The given text is appended to the cell in a subscript font.
342 fprintf(stream, " subscript=\"%s\"", get_string());
345 match_u32_assert (0);
349 /* We only have one SPV file for this version (with many
356 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))
357 match_u32_assert(10);
360 fprintf(stream, "/>\n");
364 int outer_end = get_end();
366 /* This counted-string appears to be a template string,
367 e.g. "Design\: [:^1:]1 Within Subjects Design\: [:^1:]2". */
368 char *template = dump_counted_string();
370 fprintf(stream, " template=\"%s\"", template);
372 if (match_byte(0x31))
375 match_byte_assert(0x58);
376 if (match_byte(0x31))
378 /* Only two SPV files have anything like this, so it's hard to
384 match_byte_assert(1);
385 match_byte_assert(0);
386 if (!match_byte(8) && !match_byte(1))
387 match_byte_assert(2);
388 match_byte_assert(0);
389 match_byte_assert(8);
390 match_byte_assert(0);
391 match_byte_assert(10);
392 match_byte_assert(0);
395 match_byte_assert(0x58);
396 if (pos != outer_end)
398 fprintf(stderr, "outer end discrepancy\n");
401 fprintf(stream, "/>\n");
405 int count = get_u32();
406 fprintf(stream, "<footnote-ref indexes=\"");
407 for (int i = 0; i < count; i++)
411 fprintf(stream, "%d", get_u16());
414 match_byte_assert(0);
415 match_byte_assert(0);
416 dump_nested_string(stream);
417 fprintf(stream, "/>\n");
421 match_byte_assert (0x58);
425 format_to_string (int type)
431 case 2: return "AHEX";
432 case 3: return "COMMA";
433 case 4: return "DOLLAR";
434 case 5: case 40: return "F";
436 case 7: return "PIBHEX";
438 case 9: return "PIB";
439 case 10: return "PK";
440 case 11: return "RB";
441 case 12: return "RBHEX";
445 case 20: return "DATE";
446 case 21: return "TIME";
447 case 22: return "DATETIME";
448 case 23: return "ADATE";
449 case 24: return "JDATE";
450 case 25: return "DTIME";
451 case 26: return "WKDAY";
452 case 27: return "MONTH";
453 case 28: return "MOYR";
454 case 29: return "QYR";
455 case 30: return "WKYR";
456 case 31: return "PCT";
457 case 32: return "DOT";
458 case 33: return "CCA";
459 case 34: return "CCB";
460 case 35: return "CCC";
461 case 36: return "CCD";
462 case 37: return "CCE";
463 case 38: return "EDATE";
464 case 39: return "SDATE";
467 sprintf(tmp, "<%d>", type);
473 dump_value(FILE *stream, int level)
480 for (int i = 0; i <= level; i++)
481 fprintf (stream, " ");
488 dump_value_modifier(stream);
490 value = get_double ();
491 fprintf (stream, "<number value=\"%.*g\" format=\"%s%d.%d\"/>\n",
492 DBL_DIG, value, format_to_string(format >> 16), (format >> 8) & 0xff, format & 0xff);
494 else if (match_byte (2))
500 dump_value_modifier (stream);
502 value = get_double ();
504 vallab = get_string ();
505 fprintf (stream, "<numeric-datum value=\"%.*g\" format=\"%s%d.%d\"",
506 DBL_DIG, value, format_to_string(format >> 16), (format >> 8) & 0xff, format & 0xff);
508 fprintf (stream, " variable=\"%s\"", var);
510 fprintf (stream, " label=\"%s\"", vallab);
511 fprintf (stream, "/>\n");
512 if (!match_byte (1) && !match_byte(2))
513 match_byte_assert (3);
515 else if (match_byte (3))
517 char *text = get_string();
518 dump_value_modifier(stream);
519 char *identifier = get_string();
520 char *text_eng = get_string();
521 fprintf (stream, "<string c=\"%s\"", text_eng);
523 fprintf (stream, " identifier=\"%s\"", identifier);
524 if (strcmp(text_eng, text))
525 fprintf (stream, " local=\"%s\"", text);
526 fprintf (stream, "/>\n");
528 match_byte_assert(1);
530 else if (match_byte (4))
533 char *var, *vallab, *value;
535 dump_value_modifier(stream);
537 vallab = get_string ();
539 if (!match_byte(1) && !match_byte(2))
540 match_byte_assert (3);
541 value = get_string ();
542 fprintf (stream, "<string-datum value=\"%s\" format=\"%s%d.%d\"",
543 value, format_to_string(format >> 16), (format >> 8) & 0xff, format & 0xff);
545 fprintf (stream, " variable=\"%s\"", var);
547 fprintf (stream, " label=\"%s\"/>\n", vallab);
548 fprintf (stream, "/>\n");
550 else if (match_byte (5))
552 dump_value_modifier(stream);
553 char *name = get_string ();
554 char *label = get_string ();
555 fprintf (stream, "<variable name=\"%s\"", name);
557 fprintf (stream, " label=\"%s\"", label);
558 fprintf (stream, "/>\n");
559 if (!match_byte(1) && !match_byte(2))
560 match_byte_assert(3);
564 dump_value_modifier(stream);
566 char *base = get_string();
568 fprintf (stream, "<template format=\"%s\">\n", base);
569 for (int i = 0; i < x; i++)
576 for (int j = 0; j <= level + 1; j++)
577 fprintf (stream, " ");
578 fprintf (stream, "<substitution index=\"%d\">\n", i + 1);
579 for (int j = 0; j < y; j++)
580 dump_value (stream, level + 2);
581 for (int j = 0; j <= level + 1; j++)
582 fprintf (stream, " ");
583 fprintf (stream, "</substitution>\n");
585 for (int j = 0; j <= level; j++)
586 fprintf (stream, " ");
587 fprintf (stream, "</template>\n");
592 compare_int(const void *a_, const void *b_)
596 return *a < *b ? -1 : *a > *b;
600 check_permutation(int *a, int n, const char *name)
603 memcpy(b, a, n * sizeof *a);
604 qsort(b, n, sizeof *b, compare_int);
605 for (int i = 0; i < n; i++)
608 fprintf(stderr, "bad %s permutation:", name);
609 for (int i = 0; i < n; i++)
610 fprintf(stderr, " %d", a[i]);
617 dump_category(FILE *stream, int level, int **indexes, int *allocated_indexes,
620 for (int i = 0; i <= level; i++)
621 fprintf (stream, " ");
622 printf ("<category>\n");
623 dump_value (stream, level + 1);
625 int merge = data[pos];
627 match_byte_assert (1);
629 match_byte_assert (0);
631 int unindexed = data[pos];
633 match_byte_assert (1);
638 match_u32_assert (2);
640 int indx = get_u32();
641 int n_categories = get_u32();
646 for (int i = 0; i <= level + 1; i++)
647 fprintf (stream, " ");
648 fprintf (stream, "<merge/>\n");
655 fprintf(stderr, "index not -1 but merged\n");
660 fprintf(stderr, "index not -1 but x != 2\n");
663 if (n_categories != 0)
665 fprintf(stderr, "index not -1 but subcategories\n");
668 if (*n_indexes >= *allocated_indexes)
670 *allocated_indexes = *allocated_indexes ? 2 * *allocated_indexes : 16;
671 *indexes = realloc(*indexes, *allocated_indexes * sizeof **indexes);
673 (*indexes)[(*n_indexes)++] = indx;
676 int expected_unindexed = indx == -1;
677 if (unindexed != expected_unindexed)
679 fprintf(stderr, "unindexed (%d) mismatch with indx (%d)\n",
684 if (n_categories == 0)
686 for (int i = 0; i <= level + 1; i++)
687 fprintf (stream, " ");
688 fprintf (stream, "<category-index>%d</category-index>\n", indx);
690 for (int i = 0; i < n_categories; i++)
691 dump_category (stream, level + 1, indexes, allocated_indexes, n_indexes);
692 for (int i = 0; i <= level; i++)
693 fprintf (stream, " ");
694 printf ("</category>\n");
702 printf ("<dimension index=\"%d\">\n", indx);
703 dump_value (stdout, 0);
705 /* This byte is usually 0 but many other values have been spotted. */
708 if (!match_byte(0) && !match_byte(1))
709 match_byte_assert(2);
713 match_byte_assert(1);
715 match_byte_assert(1);
716 match_byte_assert(1);
717 if (!match_u32(UINT32_MAX))
718 match_u32_assert(indx);
719 n_categories = get_u32();
723 int allocated_indexes = 0;
724 for (int i = 0; i < n_categories; i++)
725 dump_category (stdout, 0, &indexes, &allocated_indexes, &n_indexes);
726 check_permutation(indexes, n_indexes, "categories");
728 fprintf (stdout, "</dimension>\n");
733 static int dim_n_cats[64];
734 #define MAX_DIMS (sizeof dim_n_cats / sizeof *dim_n_cats)
740 assert(n_dims < MAX_DIMS);
741 for (int i = 0; i < n_dims; i++)
742 dim_n_cats[i] = dump_dim (i);
748 /* The first three numbers add to the number of dimensions. */
751 int c = n_dims - l - r;
754 /* The next n_dims numbers are a permutation of the dimension numbers. */
756 for (int i = 0; i < n_dims; i++)
761 const char *name = i < l ? "layer" : i < l + r ? "row" : "column";
762 printf ("<%s dimension=\"%d\"/>\n", name, dim);
764 check_permutation(a, n_dims, "dimensions");
768 for (int i = 0; i < x; i++)
770 unsigned int indx = get_u32();
771 printf (" <datum index=\"%d\" coords=", indx);
773 int coords[MAX_DIMS];
774 for (int i = n_dims; i-- > 0; )
776 coords[i] = indx % dim_n_cats[i];
777 indx /= dim_n_cats[i];
779 for (int i = 0; i < n_dims; i++)
780 printf("%c%d", i ? ',' : '"', coords[i]);
786 dump_value(stdout, 1);
787 fprintf (stdout, " </datum>\n");
789 printf ("</data>\n");
795 printf ("<title-local>\n");
796 dump_value(stdout, 0);
798 printf ("</title-local>\n");
800 printf ("<subtype>\n");
801 dump_value(stdout, 0);
803 printf ("</subtype>\n");
805 match_byte_assert(0x31);
807 printf ("<title-c>\n");
808 dump_value(stdout, 0);
810 printf ("</title-c>\n");
812 if (match_byte(0x31))
814 printf ("<user-caption>\n");
815 dump_value(stdout, 0);
816 printf ("</user-caption>\n");
819 match_byte_assert(0x58);
820 if (match_byte(0x31))
822 printf ("<caption>\n");
823 dump_value(stdout, 0);
824 printf ("</caption>\n");
827 match_byte_assert(0x58);
829 int n_footnotes = get_u32();
830 for (int i = 0; i < n_footnotes; i++)
832 printf ("<footnote index=\"%d\">\n", i);
833 dump_value(stdout, 0);
834 /* Custom footnote marker string. */
835 if (match_byte (0x31))
836 dump_value(stdout, 0);
838 match_byte_assert (0x58);
840 printf ("</footnote>\n");
848 for (int i = 1; i <= 8; i++)
850 printf ("<style index=\"%d\"", i);
851 match_byte_assert(i);
852 match_byte_assert(0x31);
853 printf(" font=\"%s\"", get_string());
854 match_byte_assert(0);
855 match_byte_assert(0);
856 if (!match_byte(0x40) && !match_byte(0x20) && !match_byte(0x80) && !match_byte(0x10) && !match_byte(0x70))
857 match_byte_assert(0x50);
858 match_byte_assert(0x41);
859 if (!match_u32(0) && !match_u32(1))
861 match_byte_assert(0);
863 /* OK, this seems really unlikely to be totally correct, but it matches my corpus... */
864 if (!match_u32(0) && !match_u32(2) && !match_u32(4))
867 match_u32_assert(0xfaad);
872 if (!match_u32(0) && !match_u32(1) && !match_u32(2))
874 printf (" fgcolor=\"%s\"", get_string());
875 printf (" bgcolor=\"%s\"", get_string());
877 match_byte_assert(1);
879 char *othercolor = get_string();
881 printf(" othercolor=\"%s\"", othercolor);
892 if (!match_u32(-1) && !match_u32(8))
893 match_u32_assert(24);
894 if (!match_u32(-1) && !match_u32(2))
898 /* Who knows? Ranges from -1 to 8 with no obvious pattern. */
905 match_u32_assert(240);
908 int skip = get_u32();
909 assert(skip == 18 || skip == 25);
916 int len = data[pos + 0x34];
918 printf("<tablelook>%.*s</tablelook>\n", len, &data[pos + 0x35]);
922 int count = get_u32();
925 const char *locale = get_string();
926 printf ("<locale>%s</locale>\n", locale);
928 get_u32(); /* Seen: 0, UINT32_MAX, 2, 3, 4, 5, 6, 8, 9, 21, 24. */
930 match_byte_assert(1);
931 match_byte_assert(0);
933 match_byte_assert(1);
936 if (!match_byte(0x97) && !match_byte(0x98)
937 && !match_byte(0x99) && !match_byte(0x9a) && !match_byte(0x9b))
938 match_byte_assert(0x9c);
939 match_byte_assert(7);
940 match_byte_assert(0);
941 match_byte_assert(0);
945 printf("%x\n", get_u32());
948 int decimal = data[pos];
949 int grouping = data[pos + 1];
952 if (!match_byte(',') && !match_byte('\''))
953 match_byte_assert(' ');
957 match_byte_assert(',');
958 if (!match_byte('.') && !match_byte(' ') && !match_byte(','))
959 match_byte_assert(0);
961 printf("<format decimal=\"%c\" grouping=\"", decimal);
967 for (int i = 0; i < 5; i++)
968 printf("<CC%c>%s</CC%c>\n", 'A' + i, get_string(), 'A' + i);
973 /* The last chunk is an outer envelope that contains two inner envelopes.
974 The second inner envelope has some interesting data like the encoding and
978 int outer_end = get_end();
980 /* First inner envelope: byte*33 int[n] int*[n]. */
983 /* Second inner envelope. */
984 assert(get_end() == outer_end);
986 match_byte_assert(1);
987 match_byte_assert(0);
988 if (!match_byte(3) && !match_byte(4))
989 match_byte_assert(5);
990 match_byte_assert(0);
991 match_byte_assert(0);
992 match_byte_assert(0);
994 printf("<command>%s</command>\n", get_string());
995 printf("<subcommand>%s</subcommand>\n", get_string());
996 printf("<language>%s</language>\n", get_string());
997 printf("<charset>%s</charset>\n", get_string());
998 printf("<locale>%s</locale>\n", get_string());
1001 match_byte_assert(1);
1002 match_byte_assert(0);
1004 match_byte_assert(1);
1006 match_byte_assert(1);
1008 if (!match_byte(0x97) && !match_byte(0x98)
1009 && !match_byte(0x99) && !match_byte(0x9a) && !match_byte(0x9b))
1010 match_byte_assert(0x9c);
1011 match_byte_assert(7);
1012 match_byte_assert(0);
1013 match_byte_assert(0);
1015 if (match_byte('.'))
1017 if (!match_byte(',') && !match_byte('\''))
1018 match_byte_assert(' ');
1022 match_byte_assert(',');
1023 if (!match_byte('.') && !match_byte(' ') && !match_byte(','))
1024 match_byte_assert(0);
1028 match_byte_assert(1);
1030 if (outer_end - pos > 6)
1032 /* There might be a pair of strings representing a dataset and
1033 datafile name, or there might be a set of custom currency strings.
1034 The custom currency strings start with a pair of integers, so we
1035 can distinguish these from a string by checking for a null byte; a
1036 small 32-bit integer will always contain a null and a text string
1039 int len = get_u32();
1040 bool has_dataset = !memchr(&data[pos], '\0', len);
1045 printf("<dataset>%s</dataset>\n", get_string());
1046 printf("<datafile>%s</datafile>\n", get_string());
1048 match_u32_assert(0);
1050 match_u32_assert(0);
1056 for (int i = 0; i < 5; i++)
1057 printf("<CC%c>%s</CC%c>\n", 'A' + i, get_string(), 'A' + i);
1060 match_u32_assert(0);
1062 match_byte_assert(0x2e);
1064 match_byte_assert(1);
1066 if (pos < outer_end)
1068 printf("<seed>%d</seed>\n", get_u32());
1069 match_u32_assert(0);
1071 assert(pos == outer_end);
1082 main(int argc, char *argv[])
1087 if (isatty(STDIN_FILENO))
1089 fprintf(stderr, "redirect stdin from a .bin file\n");
1092 if (fstat(STDIN_FILENO, &s))
1104 if (read(STDIN_FILENO, data, n) != n)
1112 fprintf (stderr, "usage: %s TYPE < .bin", argv[0]);
1116 if (!strcmp(argv[1], "title0"))
1119 if (match_byte (0x03)
1120 || (match_byte (0x05) && match_byte (0x58)))
1121 printf ("%s\n", get_string());
1123 printf ("<unknown>\n");
1126 else if (!strcmp(argv[1], "title"))
1132 else if (!strcmp(argv[1], "titleraw"))
1134 const char fonts[] = "\x01\x31\x09\0\0\0SansSerif";
1136 n = find(fonts, sizeof fonts - 1);
1138 else if (!strcmp(argv[1], "fonts"))
1140 const char fonts[] = "\x01\x31\x09\0\0\0SansSerif";
1141 const char styles[] = "\xf0\0\0\0";
1142 start = find(fonts, sizeof fonts - 1);
1143 n = find(styles, sizeof styles - 1);
1145 else if (!strcmp(argv[1], "styles"))
1147 const char styles[] = "\xf0\0\0\0";
1148 const char dimensions[] = "-,,,.\0";
1149 start = find(styles, sizeof styles - 1);
1150 n = find(dimensions, sizeof dimensions - 1) + sizeof dimensions - 1;
1152 else if (!strcmp(argv[1], "dimensions") || !strcmp(argv[1], "all"))
1155 match_byte_assert(1);
1156 match_byte_assert(0);
1158 /* This might be a version number of some kind, because value 1 seems
1159 to only appear in an SPV file that also required its own weird
1160 special cases in dump_value_modifier(). */
1161 version = get_u32();
1164 match_u32_assert(3);
1166 match_byte_assert(1);
1168 match_byte_assert(1);
1171 match_byte_assert(0);
1173 match_byte_assert(1);
1175 match_byte_assert(1);
1179 match_byte_assert(0);
1180 match_byte_assert(0);
1181 match_byte_assert(0);
1186 match_byte_assert(1);
1187 match_byte_assert(0);
1188 match_byte_assert(0);
1193 match_byte_assert(1);
1194 match_byte_assert(0);
1195 match_byte_assert(0);
1200 match_byte_assert(1);
1201 match_byte_assert(0);
1202 match_byte_assert(0);
1212 This is the tableId, e.g. -4154297861994971133 would be 0xdca00003.
1213 We don't have enough context to validate it. */
1226 fprintf (stderr, "%x / %x\n", pos, n);
1231 else if (!strcmp(argv[1], "raw"))
1235 dump_raw(stdout, start, n);
1239 fprintf (stderr, "unknown section %s\n", argv[1]);