14 #include "u8-mbtouc.h"
16 static const char *filename;
25 #define STR(x) XSTR(x)
26 #define WHERE __FILE__":" STR(__LINE__)
38 memcpy(&x, &data[pos], 4);
43 static unsigned long long int
47 memcpy(&x, &data[pos], 8);
56 x = (data[pos] << 24) | (data[pos + 1] << 16) | (data[pos + 2] << 8) | data[pos + 3];
65 memcpy(&x, &data[pos], 2);
74 memcpy(&x, &data[pos], 8);
79 static double __attribute__((unused))
83 memcpy(&x, &data[pos], 4);
107 match_u32_assert(uint32_t x, const char *where)
109 unsigned int y = get_u32();
112 fprintf(stderr, "%s: 0x%x: expected i%u, got i%u\n", where, pos - 4, x, y);
116 #define match_u32_assert(x) match_u32_assert(x, WHERE)
119 match_u16_assert(uint16_t x, const char *where)
121 unsigned int y = get_u16();
124 fprintf(stderr, "%s: 0x%x: expected u16:%u, got u16:%u\n", where, pos - 2, x, y);
128 #define match_u16_assert(x) match_u16_assert(x, WHERE)
130 static bool __attribute__((unused))
131 match_u64(uint64_t x)
139 static void __attribute__((unused))
140 match_u64_assert(uint64_t x, const char *where)
142 unsigned long long int y = get_u64();
145 fprintf(stderr, "%s: 0x%x: expected u64:%lu, got u64:%llu\n", where, pos - 8, x, y);
149 #define match_u64_assert(x) match_u64_assert(x, WHERE)
151 static bool __attribute__((unused))
152 match_be32(uint32_t x)
161 match_be32_assert(uint32_t x, const char *where)
163 unsigned int y = get_be32();
166 fprintf(stderr, "%s: 0x%x: expected be%u, got be%u\n", where, pos - 4, x, y);
170 #define match_be32_assert(x) match_be32_assert(x, WHERE)
173 match_byte(uint8_t b)
175 if (pos < n && data[pos] == b)
185 match_byte_assert(uint8_t b, const char *where)
189 fprintf(stderr, "%s: 0x%x: expected %02x, got %02x\n", where, pos, b, data[pos]);
193 #define match_byte_assert(b) match_byte_assert(b, WHERE)
196 match_bytes(int start, const int *bytes, size_t n_bytes)
198 for (size_t i = 0; i < n_bytes; i++)
199 if (bytes[i] >= 0 && data[start + i] != bytes[i])
205 xmemdup0(const void *p, size_t n)
207 char *s = malloc(n + 1);
218 match_byte_assert(1);
222 static bool __attribute__((unused))
225 return (p >= ' ' && p < 127) || p == '\r' || p == '\n' || p == '\t';
229 count_zeros(const uint8_t *p)
237 static bool __attribute__((unused))
238 all_utf8(const char *p_, size_t len)
240 const uint8_t *p = (const uint8_t *) p_;
241 for (size_t ofs = 0, mblen; ofs < len; ofs += mblen)
245 mblen = u8_mbtouc (&uc, p + ofs, len - ofs);
246 if ((uc < 32 && uc != '\n') || uc == 127 || uc == 0xfffd)
255 int len = data[pos++];
256 char *s = xmemdup0(&data[pos], len);
262 match_string1_assert(const char *exp, const char *where)
265 char *act = get_string1();
266 if (strcmp(act, exp))
268 fprintf(stderr, "%s: 0x%x: expected \"%s\", got \"%s\"\n",
269 where, start, exp, act);
273 #define match_string1_assert(x) match_string1_assert(x, WHERE)
278 int len = data[pos] + data[pos + 1] * 256;
279 char *s = xmemdup0(&data[pos + 2], len);
285 match_string2_assert(const char *exp, const char *where)
288 char *act = get_string2();
289 if (strcmp(act, exp))
291 fprintf(stderr, "%s: 0x%x: expected \"%s\", got \"%s\"\n",
292 where, start, exp, act);
296 #define match_string2_assert(x) match_string2_assert(x, WHERE)
299 get_string4(const char *where)
302 /*data[pos + 1] == 0 && data[pos + 2] == 0 && data[pos + 3] == 0*/
303 /*&& all_ascii(&data[pos + 4], data[pos])*/)
305 int len = data[pos] + data[pos + 1] * 256;
306 char *s = malloc(len + 1);
308 memcpy(s, &data[pos + 4], len);
315 fprintf(stderr, "%s: 0x%x: expected string\n", where, pos);
319 #define get_string4() get_string4(WHERE)
322 get_padded_string(int len)
324 char *s = xmemdup0(&data[pos], len);
330 get_string_be(const char *where)
333 /*data[pos + 1] == 0 && data[pos + 2] == 0 && data[pos + 3] == 0*/
334 /*&& all_ascii(&data[pos + 4], data[pos])*/)
336 int len = data[pos + 2] * 256 + data[pos + 3];
337 char *s = malloc(len + 1);
339 memcpy(s, &data[pos + 4], len);
346 fprintf(stderr, "%s: 0x%x: expected string\n", where, pos);
350 #define get_string_be() get_string_be(WHERE)
359 static void __attribute__((unused))
360 hex_dump(FILE *stream, int ofs, int n)
363 for (int i = 0; i < n; i++)
365 int c = data[ofs + i];
366 n_ascii += is_ascii(c);
367 fprintf(stream, " %02x", c);
372 for (int i = 0; i < n; i++)
374 int c = data[ofs + i];
375 putc(c >= 32 && c < 127 ? c : '.', stream);
381 static void __attribute__((unused))
382 char_dump(FILE *stream, int ofs, int n)
384 for (int i = 0; i < n; i++)
386 int c = data[ofs + i];
387 putc(c >= 32 && c < 127 ? c : '.', stream);
394 compare_int(const void *a_, const void *b_)
398 return *a < *b ? -1 : *a > *b;
403 format_name (int format, char *buf)
408 case 2: return "AHEX";
409 case 3: return "COMMA";
410 case 4: return "DOLLAR";
411 case 5: case 40: return "F";
413 case 7: return "PIBHEX";
415 case 9: return "PIB";
416 case 10: return "PK";
417 case 11: return "RB";
418 case 12: return "RBHEX";
422 case 20: return "DATE";
423 case 21: return "TIME";
424 case 22: return "DATETIME";
425 case 23: return "ADATE";
426 case 24: return "JDATE";
427 case 25: return "DTIME";
428 case 26: return "WKDAY";
429 case 27: return "MONTH";
430 case 28: return "MOYR";
431 case 29: return "QYR";
432 case 30: return "WKYR";
433 case 31: return "PCT";
434 case 32: return "DOT";
435 case 33: return "CCA";
436 case 34: return "CCB";
437 case 35: return "CCC";
438 case 36: return "CCD";
439 case 37: return "CCE";
440 case 38: return "EDATE";
441 case 39: return "SDATE";
442 default: sprintf(buf, "(%d)", format); return buf;
451 int fmt = data[pos++];
453 printf ("%s%d.%d", format_name(fmt, buf), w, d);
457 parse_heading(const char *name)
459 match_u16_assert(0xffff);
461 match_string2_assert(name);
465 match_zeros_assert(int count, const char *where)
467 for (int i = 0; i < count; i++)
471 "%s: %#x: expected %d zeros here but offset %d is %#"PRIx8"\n",
472 where, pos, count, i, data[pos + i]);
477 #define match_zeros_assert(count) match_zeros_assert(count, WHERE)
480 put_safe(const char *s)
488 else if (*s < 0x20 || *s > 0x7e)
489 printf ("\\x%02"PRIx8, (uint8_t) *s);
497 parse_DspString(void)
499 match_byte_assert(1);
500 match_byte_assert(2);
501 match_byte_assert(40);
503 match_byte_assert(5);
504 match_byte_assert(0);
505 match_byte_assert(1);
506 printf ("DspString(\"");
507 put_safe(get_string1());
512 match_DspString(void)
514 match_byte_assert(5);
515 match_byte_assert(0x80);
520 match_DspSimpleText(void)
522 match_byte_assert(3);
523 match_byte_assert(0x80);
524 match_zeros_assert(5);
525 if (!match_byte(0x10))
526 match_byte_assert(0);
527 match_zeros_assert(4);
531 match_NavTreeViewItem(void)
533 match_byte_assert(7);
534 match_byte_assert(0x80);
535 match_zeros_assert(1);
536 if (!match_byte(0) && !match_byte(7))
537 match_byte_assert(8);
538 match_zeros_assert(3);
540 match_byte_assert(0);
541 match_byte_assert(1);
542 match_zeros_assert(3);
544 match_byte_assert(1);
545 match_zeros_assert(5);
546 match_byte_assert(1);
547 match_zeros_assert(5);
549 put_safe(get_string1());
554 parse_DspNumber(void)
556 match_byte_assert(1);
558 match_byte_assert(0x80);
560 printf (" %f", get_double());
561 printf (" \"%s\"\n", get_string1());
565 match_DspNumber(void)
567 match_byte_assert(0x2a);
568 match_byte_assert(0x80);
575 match_byte_assert(0x27);
576 match_byte_assert(0x80);
577 match_byte_assert(0);
578 match_DspSimpleText();
581 else if (data[pos] == 0x2a)
588 parse_PMModelItemInfo(void)
590 match_byte_assert(0);
591 pos += 1; /* Counter */
592 match_zeros_assert(7);
595 match_byte_assert(0xe);
596 match_byte_assert(0);
600 match_PMModelItemInfo(void)
602 match_byte_assert(0x54);
603 match_byte_assert(0x80);
604 parse_PMModelItemInfo();
605 match_DspSimpleText();
610 match_PMPivotItemTree(void)
612 match_byte_assert(0x52);
613 match_byte_assert(0x80);
614 match_byte_assert(0);
615 match_PMModelItemInfo();
619 main(int argc, char *argv[])
621 bool print_offsets = false;
624 int c = getopt (argc, argv, "o");
631 print_offsets = true;
638 if (argc - optind != 1)
640 fprintf (stderr, "usage: %s FILE.bin", argv[0]);
644 const char *filename = argv[optind];
645 int fd = open(filename, O_RDONLY);
648 fprintf (stderr, "%s: open failed (%s)", filename, strerror (errno));
665 if (read(fd, data, n) != n)
672 setvbuf (stdout, NULL, _IOLBF, 0);
674 match_byte_assert(4);
676 match_string1_assert("SPSS Output Document");
678 match_byte_assert(0x63);
680 parse_heading("NavRoot");
681 match_byte_assert(2);
682 match_zeros_assert(32);
684 parse_heading("DspSimpleText");
685 match_zeros_assert(10);
687 parse_heading("DspString");
690 parse_heading("NavTreeViewItem");
691 match_byte_assert(0);
693 match_byte_assert(2);
694 match_byte_assert(0);
695 match_byte_assert(1);
696 match_zeros_assert(9);
698 assert (pos == 0xb0);
701 match_zeros_assert(5);
703 match_u32_assert(11000);
706 match_u32_assert(11000);
707 match_u32_assert(8500);
710 match_string1_assert("(Continued)");
711 match_byte_assert(1);
712 match_byte_assert(1);
713 match_zeros_assert(3);
714 get_string4(); /* page title */
715 match_byte_assert(1);
716 match_byte_assert(1);
717 match_zeros_assert(3);
718 get_string4(); /* page number */
719 match_byte_assert(0);
723 parse_heading("NavLog");
725 puts(get_padded_string(32));
727 match_u32_assert(132);
728 match_zeros_assert(8);
731 match_byte_assert(0);
733 parse_heading("NavHead");
734 match_byte_assert(2);
735 match_zeros_assert(24);
738 match_DspSimpleText();
740 match_NavTreeViewItem();
741 match_zeros_assert(3);
743 parse_heading("NavTitle");
745 match_DspSimpleText();
747 match_NavTreeViewItem();
749 match_byte_assert(1);
750 match_byte_assert(1);
751 match_u32_assert(-19);
752 match_zeros_assert(12);
753 match_byte_assert(0xbc);
754 match_byte_assert(2);
755 match_zeros_assert(9);
756 match_byte_assert(0x22);
757 puts(get_padded_string(32));
758 match_u32_assert(80);
759 match_zeros_assert(8);
762 match_byte_assert(0);
764 parse_heading("NavNote");
765 match_byte_assert(2);
766 match_zeros_assert(8);
767 match_u32_assert(24);
769 match_u32_assert(-40);
773 match_DspSimpleText();
775 match_NavTreeViewItem();
776 match_byte_assert(1);
778 parse_heading("PTPivotController");
779 match_byte_assert(2);
781 match_u32_assert(100);
782 match_u32_assert(100);
783 match_u32_assert(100);
784 match_u32_assert(100);
786 parse_heading("PVPivotView");
788 match_byte_assert(0);
790 parse_heading("PMPivotModel");
791 match_byte_assert(3);
793 parse_heading("NDimensional__DspCell");
794 match_byte_assert(0);
797 parse_heading("IndexedCollection");
798 match_byte_assert(0);
800 match_zeros_assert(3);
801 match_byte_assert(1);
802 match_byte_assert(0);
804 parse_heading("DspCell");
805 match_byte_assert(0);
806 match_DspSimpleText();
808 parse_heading("DspNumber");
818 while (data[pos] == 0)
823 while (data[pos] == 0)
829 match_byte_assert(1);
830 match_byte_assert(0);
835 match_byte_assert(0);
836 match_byte_assert(1);
837 match_byte_assert(0);
838 match_byte_assert(0);
839 match_byte_assert(0);
840 match_byte_assert(1);
841 match_byte_assert(0);
843 parse_heading("PMPivotItemTree");
844 match_byte_assert(0);
846 parse_heading("AbstractTreeBranch");
847 match_byte_assert(0);
849 parse_heading("PMModelItemInfo");
850 parse_PMModelItemInfo();
851 match_DspSimpleText();
855 match_PMPivotItemTree();
858 match_PMPivotItemTree();
861 match_PMPivotItemTree();
864 match_PMPivotItemTree();
867 match_PMPivotItemTree();
870 match_PMPivotItemTree();
873 match_PMPivotItemTree();
876 match_PMPivotItemTree();
879 match_PMPivotItemTree();
882 match_PMPivotItemTree();
885 match_PMPivotItemTree();
888 match_PMPivotItemTree();
891 match_PMPivotItemTree();
894 match_PMPivotItemTree();
897 match_PMPivotItemTree();
900 match_PMPivotItemTree();
903 match_PMPivotItemTree();
909 while (data[pos] != 0xff || data[pos + 1] != 0xff)
911 parse_heading("PVViewDimension");
913 printf ("%#x: end of successful parse\n", pos);