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] + data[pos + 1] * 256;
256 char *s = xmemdup0(&data[pos + 2], len);
264 int len = data[pos++];
266 return get_string2();
269 char *s = xmemdup0(&data[pos], len);
276 match_string1_assert(const char *exp, const char *where)
279 char *act = get_string1();
280 if (strcmp(act, exp))
282 fprintf(stderr, "%s: 0x%x: expected \"%s\", got \"%s\"\n",
283 where, start, exp, act);
287 #define match_string1_assert(x) match_string1_assert(x, WHERE)
290 match_string2_assert(const char *exp, const char *where)
293 char *act = get_string2();
294 if (strcmp(act, exp))
296 fprintf(stderr, "%s: 0x%x: expected \"%s\", got \"%s\"\n",
297 where, start, exp, act);
301 #define match_string2_assert(x) match_string2_assert(x, WHERE)
304 get_string4(const char *where)
307 /*data[pos + 1] == 0 && data[pos + 2] == 0 && data[pos + 3] == 0*/
308 /*&& all_ascii(&data[pos + 4], data[pos])*/)
310 int len = data[pos] + data[pos + 1] * 256;
311 char *s = malloc(len + 1);
313 memcpy(s, &data[pos + 4], len);
320 fprintf(stderr, "%s: 0x%x: expected string\n", where, pos);
324 #define get_string4() get_string4(WHERE)
327 get_padded_string(int len)
329 char *s = xmemdup0(&data[pos], len);
335 get_string_be(const char *where)
338 /*data[pos + 1] == 0 && data[pos + 2] == 0 && data[pos + 3] == 0*/
339 /*&& all_ascii(&data[pos + 4], data[pos])*/)
341 int len = data[pos + 2] * 256 + data[pos + 3];
342 char *s = malloc(len + 1);
344 memcpy(s, &data[pos + 4], len);
351 fprintf(stderr, "%s: 0x%x: expected string\n", where, pos);
355 #define get_string_be() get_string_be(WHERE)
364 static void __attribute__((unused))
365 hex_dump(FILE *stream, int ofs, int n)
368 for (int i = 0; i < n; i++)
370 int c = data[ofs + i];
371 n_ascii += is_ascii(c);
372 fprintf(stream, " %02x", c);
377 for (int i = 0; i < n; i++)
379 int c = data[ofs + i];
380 putc(c >= 32 && c < 127 ? c : '.', stream);
386 static void __attribute__((unused))
387 char_dump(FILE *stream, int ofs, int n)
389 for (int i = 0; i < n; i++)
391 int c = data[ofs + i];
392 putc(c >= 32 && c < 127 ? c : '.', stream);
399 compare_int(const void *a_, const void *b_)
403 return *a < *b ? -1 : *a > *b;
408 format_name (int format, char *buf)
413 case 2: return "AHEX";
414 case 3: return "COMMA";
415 case 4: return "DOLLAR";
416 case 5: case 40: return "F";
418 case 7: return "PIBHEX";
420 case 9: return "PIB";
421 case 10: return "PK";
422 case 11: return "RB";
423 case 12: return "RBHEX";
427 case 20: return "DATE";
428 case 21: return "TIME";
429 case 22: return "DATETIME";
430 case 23: return "ADATE";
431 case 24: return "JDATE";
432 case 25: return "DTIME";
433 case 26: return "WKDAY";
434 case 27: return "MONTH";
435 case 28: return "MOYR";
436 case 29: return "QYR";
437 case 30: return "WKYR";
438 case 31: return "PCT";
439 case 32: return "DOT";
440 case 33: return "CCA";
441 case 34: return "CCB";
442 case 35: return "CCC";
443 case 36: return "CCD";
444 case 37: return "CCE";
445 case 38: return "EDATE";
446 case 39: return "SDATE";
447 default: sprintf(buf, "(%d)", format); return buf;
456 int fmt = data[pos++];
458 printf ("%s%d.%d", format_name(fmt, buf), w, d);
462 parse_heading(const char *name)
464 match_u16_assert(0xffff);
466 match_string2_assert(name);
470 match_zeros_assert(int count, const char *where)
472 for (int i = 0; i < count; i++)
476 "%s: %#x: expected %d zeros here but offset %d is %#"PRIx8"\n",
477 where, pos, count, i, data[pos + i]);
482 #define match_zeros_assert(count) match_zeros_assert(count, WHERE)
485 put_safe(const char *s)
493 else if (*s < 0x20 || *s > 0x7e)
494 printf ("\\x%02"PRIx8, (uint8_t) *s);
502 parse_DspString(void)
504 match_byte_assert(1);
505 match_byte_assert(2);
506 match_byte_assert(40);
508 match_byte_assert(5);
509 match_byte_assert(0);
510 match_byte_assert(1);
511 printf ("DspString(\"");
512 put_safe(get_string1());
517 match_DspString(void)
519 match_byte_assert(5);
520 match_byte_assert(0x80);
525 match_DspSimpleText(void)
527 match_byte_assert(3);
528 match_byte_assert(0x80);
529 match_zeros_assert(5);
530 if (!match_byte(0x10))
531 match_byte_assert(0);
532 match_zeros_assert(4);
536 match_NavTreeViewItem(void)
538 match_byte_assert(7);
539 match_byte_assert(0x80);
540 match_zeros_assert(1);
541 if (!match_byte(0) && !match_byte(7))
542 match_byte_assert(8);
543 match_zeros_assert(3);
545 match_byte_assert(0);
546 match_byte_assert(1);
547 match_zeros_assert(3);
549 match_byte_assert(1);
550 match_zeros_assert(5);
551 match_byte_assert(1);
552 match_zeros_assert(5);
554 put_safe(get_string1());
559 parse_DspNumber(void)
561 match_byte_assert(1);
562 printf("DspNumber(");
564 match_byte_assert(0x80);
566 printf (" %f", get_double());
567 printf (" \"%s\")\n", get_string1());
571 match_DspNumber(void)
573 match_byte_assert(0x2a);
574 match_byte_assert(0x80);
578 static void parse_flexible(void);
583 match_byte_assert(0);
584 match_DspSimpleText();
585 parse_flexible(); /* DspString or DspNumber. */
591 match_byte_assert(0x27);
592 match_byte_assert(0x80);
597 parse_PMModelItemInfo(void)
599 match_byte_assert(0);
600 pos += 1; /* Counter */
601 match_zeros_assert(7);
604 match_byte_assert(0xe);
605 match_byte_assert(0);
609 match_PMModelItemInfo(void)
611 match_byte_assert(0x54);
612 match_byte_assert(0x80);
613 parse_PMModelItemInfo();
614 match_DspSimpleText();
619 match_PMPivotItemTree(void)
621 match_byte_assert(0x52);
622 match_byte_assert(0x80);
623 match_byte_assert(0);
624 match_PMModelItemInfo();
630 if (data[pos] == 0xff && data[pos + 1] == 0xff)
632 match_u16_assert(0xffff);
634 char *heading = get_string2();
635 if (!strcmp(heading, "DspCell"))
637 else if (!strcmp(heading, "DspNumber"))
639 else if (!strcmp(heading, "DspString"))
644 else if (data[pos] == 0x2a && data[pos + 1] == 0x80)
646 else if (data[pos] == 0x27 && data[pos + 1] == 0x80)
648 else if (data[pos] == 0x5 && data[pos + 1] == 0x80)
655 main(int argc, char *argv[])
657 bool print_offsets = false;
660 int c = getopt (argc, argv, "o");
667 print_offsets = true;
674 if (argc - optind != 1)
676 fprintf (stderr, "usage: %s FILE.bin", argv[0]);
680 const char *filename = argv[optind];
681 int fd = open(filename, O_RDONLY);
684 fprintf (stderr, "%s: open failed (%s)", filename, strerror (errno));
701 if (read(fd, data, n) != n)
708 setvbuf (stdout, NULL, _IOLBF, 0);
710 match_byte_assert(4);
712 match_string1_assert("SPSS Output Document");
714 match_byte_assert(0x63);
716 parse_heading("NavRoot");
717 match_byte_assert(2);
718 match_zeros_assert(32);
720 parse_heading("DspSimpleText");
721 match_zeros_assert(10);
723 parse_heading("DspString");
726 parse_heading("NavTreeViewItem");
727 match_byte_assert(0);
729 match_byte_assert(2);
730 match_byte_assert(0);
731 match_byte_assert(1);
732 match_zeros_assert(9);
734 assert (pos == 0xb0);
737 match_zeros_assert(5);
739 match_u32_assert(11000);
742 match_u32_assert(11000);
743 match_u32_assert(8500);
746 match_string1_assert("(Continued)");
747 match_byte_assert(1);
748 match_byte_assert(1);
749 match_zeros_assert(3);
750 get_string4(); /* page title */
751 match_byte_assert(1);
752 match_byte_assert(1);
753 match_zeros_assert(3);
754 get_string4(); /* page number */
755 match_byte_assert(0);
759 parse_heading("NavLog");
761 puts(get_padded_string(32));
763 match_u32_assert(132);
764 match_zeros_assert(8);
767 match_byte_assert(0);
769 parse_heading("NavHead");
770 match_byte_assert(2);
771 match_zeros_assert(24);
774 match_DspSimpleText();
776 match_NavTreeViewItem();
777 match_zeros_assert(3);
779 parse_heading("NavTitle");
781 match_DspSimpleText();
783 match_NavTreeViewItem();
785 match_byte_assert(1);
786 match_byte_assert(1);
787 match_u32_assert(-19);
788 match_zeros_assert(12);
789 match_byte_assert(0xbc);
790 match_byte_assert(2);
791 match_zeros_assert(9);
792 match_byte_assert(0x22);
793 puts(get_padded_string(32));
794 match_u32_assert(80);
795 match_zeros_assert(8);
798 match_byte_assert(0);
800 parse_heading("NavNote");
801 match_byte_assert(2);
802 match_zeros_assert(8);
803 match_u32_assert(24);
805 match_u32_assert(-40);
809 match_DspSimpleText();
811 match_NavTreeViewItem();
812 match_byte_assert(1);
814 parse_heading("PTPivotController");
815 match_byte_assert(2);
817 match_u32_assert(100);
818 match_u32_assert(100);
819 match_u32_assert(100);
820 match_u32_assert(100);
822 parse_heading("PVPivotView");
824 match_byte_assert(0);
826 parse_heading("PMPivotModel");
827 match_byte_assert(3);
829 parse_heading("NDimensional__DspCell");
830 match_byte_assert(0);
833 parse_heading("IndexedCollection");
834 match_byte_assert(0);
836 match_zeros_assert(3);
837 match_byte_assert(1);
838 match_byte_assert(0);
840 while (data[pos] != 1)
848 match_byte_assert(1);
849 match_byte_assert(0);
855 match_byte_assert(0);
856 match_byte_assert(1);
857 match_byte_assert(0);
858 match_byte_assert(0);
859 match_byte_assert(0);
860 match_byte_assert(1);
861 match_byte_assert(0);
865 parse_heading("PMPivotItemTree");
866 match_byte_assert(0);
868 parse_heading("AbstractTreeBranch");
869 match_byte_assert(0);
871 parse_heading("PMModelItemInfo");
872 parse_PMModelItemInfo();
873 match_DspSimpleText();
877 match_PMPivotItemTree();
880 match_PMPivotItemTree();
883 match_PMPivotItemTree();
886 match_PMPivotItemTree();
889 match_PMPivotItemTree();
892 match_PMPivotItemTree();
895 match_PMPivotItemTree();
898 match_PMPivotItemTree();
901 match_PMPivotItemTree();
904 match_PMPivotItemTree();
907 match_PMPivotItemTree();
910 match_PMPivotItemTree();
913 match_PMPivotItemTree();
916 match_PMPivotItemTree();
919 match_PMPivotItemTree();
922 match_PMPivotItemTree();
925 match_PMPivotItemTree();
931 while (data[pos] != 0xff || data[pos + 1] != 0xff)
933 parse_heading("PVViewDimension");
936 for (i = 0; data[pos + i] != 0xff || data[pos + i + 1] != 0xff; i++)
938 hex_dump(stdout, pos, i);
940 printf ("%#x: end of successful parse\n", pos);