f416bf696210382b4abb676d5f27893b58309265
[pspp] / dump-spo2.c
1 #include <assert.h>
2 #include <errno.h>
3 #include <fcntl.h>
4 #include <float.h>
5 #include <inttypes.h>
6 #include <stdbool.h>
7 #include <stdint.h>
8 #include <stdio.h>
9 #include <stdlib.h>
10 #include <string.h>
11 #include <sys/stat.h>
12 #include <time.h>
13 #include <unistd.h>
14 #include "u8-mbtouc.h"
15
16 static const char *filename;
17 static uint8_t *data;
18 static size_t n;
19
20 int version;
21
22 unsigned int pos;
23
24 #define XSTR(x) #x
25 #define STR(x) XSTR(x)
26 #define WHERE __FILE__":" STR(__LINE__)
27
28 static uint8_t
29 get_byte(void)
30 {
31   return data[pos++];
32 }
33
34 static unsigned int
35 get_u32(void)
36 {
37   uint32_t x;
38   memcpy(&x, &data[pos], 4);
39   pos += 4;
40   return x;
41 }
42
43 static unsigned long long int
44 get_u64(void)
45 {
46   uint64_t x;
47   memcpy(&x, &data[pos], 8);
48   pos += 8;
49   return x;
50 }
51
52 static unsigned int
53 get_be32(void)
54 {
55   uint32_t x;
56   x = (data[pos] << 24) | (data[pos + 1] << 16) | (data[pos + 2] << 8) | data[pos + 3];
57   pos += 4;
58   return x;
59 }
60
61 static unsigned int
62 get_u16(void)
63 {
64   uint16_t x;
65   memcpy(&x, &data[pos], 2);
66   pos += 2;
67   return x;
68 }
69
70 static double
71 get_double(void)
72 {
73   double x;
74   memcpy(&x, &data[pos], 8);
75   pos += 8;
76   return x;
77 }
78
79 static double __attribute__((unused))
80 get_float(void)
81 {
82   float x;
83   memcpy(&x, &data[pos], 4);
84   pos += 4;
85   return x;
86 }
87
88 static bool
89 match_u32(uint32_t x)
90 {
91   if (get_u32() == x)
92     return true;
93   pos -= 4;
94   return false;
95 }
96
97 bool
98 match_u16(uint16_t x)
99 {
100   if (get_u16() == x)
101     return true;
102   pos -= 2;
103   return false;
104 }
105
106 static void
107 match_u32_assert(uint32_t x, const char *where)
108 {
109   unsigned int y = get_u32();
110   if (x != y)
111     {
112       fprintf(stderr, "%s: 0x%x: expected i%u, got i%u\n", where, pos - 4, x, y);
113       exit(1);
114     }
115 }
116 #define match_u32_assert(x) match_u32_assert(x, WHERE)
117
118 static void
119 match_u16_assert(uint16_t x, const char *where)
120 {
121   unsigned int y = get_u16();
122   if (x != y)
123     {
124       fprintf(stderr, "%s: 0x%x: expected u16:%u, got u16:%u\n", where, pos - 2, x, y);
125       exit(1);
126     }
127 }
128 #define match_u16_assert(x) match_u16_assert(x, WHERE)
129
130 static bool __attribute__((unused))
131 match_u64(uint64_t x)
132 {
133   if (get_u64() == x)
134     return true;
135   pos -= 8;
136   return false;
137 }
138
139 static void __attribute__((unused))
140 match_u64_assert(uint64_t x, const char *where)
141 {
142   unsigned long long int y = get_u64();
143   if (x != y)
144     {
145       fprintf(stderr, "%s: 0x%x: expected u64:%lu, got u64:%llu\n", where, pos - 8, x, y);
146       exit(1);
147     }
148 }
149 #define match_u64_assert(x) match_u64_assert(x, WHERE)
150
151 static bool __attribute__((unused))
152 match_be32(uint32_t x)
153 {
154   if (get_be32() == x)
155     return true;
156   pos -= 4;
157   return false;
158 }
159
160 static void
161 match_be32_assert(uint32_t x, const char *where)
162 {
163   unsigned int y = get_be32();
164   if (x != y)
165     {
166       fprintf(stderr, "%s: 0x%x: expected be%u, got be%u\n", where, pos - 4, x, y);
167       exit(1);
168     }
169 }
170 #define match_be32_assert(x) match_be32_assert(x, WHERE)
171
172 static bool
173 match_byte(uint8_t b)
174 {
175   if (pos < n && data[pos] == b)
176     {
177       pos++;
178       return true;
179     }
180   else
181     return false;
182 }
183
184 static void
185 match_byte_assert(uint8_t b, const char *where)
186 {
187   if (!match_byte(b))
188     {
189       fprintf(stderr, "%s: 0x%x: expected %02x, got %02x\n", where, pos, b, data[pos]);
190       exit(1);
191     }
192 }
193 #define match_byte_assert(b) match_byte_assert(b, WHERE)
194
195 static bool
196 match_bytes(int start, const int *bytes, size_t n_bytes)
197 {
198   for (size_t i = 0; i < n_bytes; i++)
199     if (bytes[i] >= 0 && data[start + i] != bytes[i])
200       return false;
201   return true;
202 }
203
204 static char *
205 xmemdup0(const void *p, size_t n)
206 {
207   char *s = malloc(n + 1);
208   memcpy(s, p, n);
209   s[n] = 0;
210   return s;
211 }
212
213 static bool
214 get_bool(void)
215 {
216   if (match_byte(0))
217     return false;
218   match_byte_assert(1);
219   return true;
220 }
221
222 static bool __attribute__((unused))
223 is_ascii(uint8_t p)
224 {
225   return (p >= ' ' && p < 127) || p == '\r' || p == '\n' || p == '\t';
226 }
227
228 static int
229 count_zeros(const uint8_t *p)
230 {
231   size_t n = 0;
232   while (p[n] == 0)
233     n++;
234   return n;
235 }
236
237 static bool __attribute__((unused))
238 all_utf8(const char *p_, size_t len)
239 {
240   const uint8_t *p = (const uint8_t *) p_;
241   for (size_t ofs = 0, mblen; ofs < len; ofs += mblen)
242     {
243       ucs4_t uc;
244
245       mblen = u8_mbtouc (&uc, p + ofs, len - ofs);
246       if ((uc < 32 && uc != '\n') || uc == 127 || uc == 0xfffd)
247         return false;
248     }
249   return true;
250 }
251
252 static char *
253 get_string2(void)
254 {
255   int len = data[pos] + data[pos + 1] * 256;
256   char *s = xmemdup0(&data[pos + 2], len);
257   pos += 2 + len;
258   return s;
259 }
260
261 static char *
262 get_string1(void)
263 {
264   int len = data[pos++];
265   if (len == 0xff)
266     return get_string2();
267   else
268     {
269       char *s = xmemdup0(&data[pos], len);
270       pos += len;
271       return s;
272     }
273 }
274
275 static void
276 match_string1_assert(const char *exp, const char *where)
277 {
278   int start = pos;
279   char *act = get_string1();
280   if (strcmp(act, exp)) 
281     {
282       fprintf(stderr, "%s: 0x%x: expected \"%s\", got \"%s\"\n",
283               where, start, exp, act);
284       exit(1);
285     }
286 }
287 #define match_string1_assert(x) match_string1_assert(x, WHERE)
288
289 static void
290 match_string2_assert(const char *exp, const char *where)
291 {
292   int start = pos;
293   char *act = get_string2();
294   if (strcmp(act, exp)) 
295     {
296       fprintf(stderr, "%s: 0x%x: expected \"%s\", got \"%s\"\n",
297               where, start, exp, act);
298       exit(1);
299     }
300 }
301 #define match_string2_assert(x) match_string2_assert(x, WHERE)
302
303 static char *
304 get_string4(const char *where)
305 {
306   if (1
307       /*data[pos + 1] == 0 && data[pos + 2] == 0 && data[pos + 3] == 0*/
308       /*&& all_ascii(&data[pos + 4], data[pos])*/)
309     {
310       int len = data[pos] + data[pos + 1] * 256;
311       char *s = malloc(len + 1);
312
313       memcpy(s, &data[pos + 4], len);
314       s[len] = 0;
315       pos += 4 + len;
316       return s;
317     }
318   else
319     {
320       fprintf(stderr, "%s: 0x%x: expected string\n", where, pos);
321       exit(1);
322     }
323 }
324 #define get_string4() get_string4(WHERE)
325
326 static char *
327 get_padded_string(int len)
328 {
329   char *s = xmemdup0(&data[pos], len);
330   pos += len;
331   return s;
332 }
333
334 static char *
335 get_string_be(const char *where)
336 {
337   if (1
338       /*data[pos + 1] == 0 && data[pos + 2] == 0 && data[pos + 3] == 0*/
339       /*&& all_ascii(&data[pos + 4], data[pos])*/)
340     {
341       int len = data[pos + 2] * 256 + data[pos + 3];
342       char *s = malloc(len + 1);
343
344       memcpy(s, &data[pos + 4], len);
345       s[len] = 0;
346       pos += 4 + len;
347       return s;
348     }
349   else
350     {
351       fprintf(stderr, "%s: 0x%x: expected string\n", where, pos);
352       exit(1);
353     }
354 }
355 #define get_string_be() get_string_be(WHERE)
356
357 static int
358 get_end(void)
359 {
360   int len = get_u32();
361   return pos + len;
362 }
363
364 static void __attribute__((unused))
365 hex_dump(FILE *stream, int ofs, int n)
366 {
367   int n_ascii = 0;
368   for (int i = 0; i < n; i++)
369     {
370       int c = data[ofs + i];
371       n_ascii += is_ascii(c);
372       fprintf(stream, " %02x", c);
373     }
374   if (n_ascii >= 3)
375     {
376       putc(' ', stream);
377       for (int i = 0; i < n; i++)
378         {
379           int c = data[ofs + i];
380           putc(c >= 32 && c < 127 ? c : '.', stream);
381         }
382     }
383   putc('\n', stream);
384 }
385
386 static void __attribute__((unused))
387 char_dump(FILE *stream, int ofs, int n)
388 {
389   for (int i = 0; i < n; i++)
390     {
391       int c = data[ofs + i];
392       putc(c >= 32 && c < 127 ? c : '.', stream);
393     }
394   putc('\n', stream);
395 }
396
397
398 static int
399 compare_int(const void *a_, const void *b_)
400 {
401   const int *a = a_;
402   const int *b = b_;
403   return *a < *b ? -1 : *a > *b;
404 }
405
406
407 static const char *
408 format_name (int format, char *buf)
409 {
410   switch (format)
411     {
412     case 1: return "A";
413     case 2: return "AHEX";
414     case 3: return "COMMA";
415     case 4: return "DOLLAR";
416     case 5: case 40: return "F";
417     case 6: return "IB";
418     case 7: return "PIBHEX";
419     case 8: return "P";
420     case 9: return "PIB";
421     case 10: return "PK";
422     case 11: return "RB";
423     case 12: return "RBHEX";
424     case 15: return "Z";
425     case 16: return "N";
426     case 17: return "E";
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;
448     }
449 }
450
451 static void
452 parse_format(void)
453 {
454   int d = data[pos++];
455   int w = data[pos++];
456   int fmt = data[pos++];
457   char buf[32];
458   printf ("%s%d.%d", format_name(fmt, buf), w, d);
459 }
460
461 static void
462 parse_heading(const char *name)
463 {
464   match_u16_assert(0xffff);
465   match_u16_assert(0);
466   match_string2_assert(name);
467 }
468
469 static void
470 match_zeros_assert(int count, const char *where)
471 {
472   for (int i = 0; i < count; i++)
473     if (data[pos + i])
474       {
475         fprintf (stderr,
476                  "%s: %#x: expected %d zeros here but offset %d is %#"PRIx8"\n",
477                  where, pos, count, i, data[pos + i]);
478         exit (1);
479       }
480   pos += count;
481 }
482 #define match_zeros_assert(count) match_zeros_assert(count, WHERE)
483
484 static void
485 put_safe(const char *s)
486 {
487   while (*s)
488     {
489       if (*s == '\n')
490         printf ("\\n");
491       else if (*s == '\r')
492         printf ("\\r");
493       else if (*s < 0x20 || *s > 0x7e)
494         printf ("\\x%02"PRIx8, (uint8_t) *s);
495       else
496         putchar (*s);
497       s++;
498     }
499 }
500
501 static void
502 parse_DspString(void)
503 {
504   match_byte_assert(1);
505   match_byte_assert(2);
506   match_byte_assert(40);
507   if (!match_byte(0))
508     match_byte_assert(5);
509   match_byte_assert(0);
510   match_byte_assert(1);
511   printf ("DspString(\"");
512   put_safe(get_string1());
513   printf("\")\n");
514 }
515
516 static void
517 match_DspString(void)
518 {                               /* 05 80 */
519   match_byte_assert(5);
520   match_byte_assert(0x80);
521   parse_DspString();
522 }
523
524 static void
525 match_DspSimpleText(void)
526 {                               /* 03 80 */
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);
533 }
534
535 static void
536 match_NavTreeViewItem(void)
537 {                               /* 07 80 */
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);
544   pos++;
545   match_byte_assert(0);
546   match_byte_assert(1);
547   match_zeros_assert(3);
548   if (!match_byte(0))
549     match_byte_assert(1);
550   match_zeros_assert(5);
551   match_byte_assert(1);
552   match_zeros_assert(5);
553
554   put_safe(get_string1());
555   putc('\n', stdout);
556 }
557
558 static void
559 parse_DspNumber(void)
560 {
561   match_byte_assert(1);
562   printf("DspNumber(");
563   parse_format();
564   match_byte_assert(0x80);
565   match_byte(2);
566   printf (" %f", get_double());
567   printf (" \"%s\")\n", get_string1());
568 }
569
570 static void
571 match_DspNumber(void)
572 {
573   match_byte_assert(0x2a);
574   match_byte_assert(0x80);
575   parse_DspNumber();
576 }
577
578 static void parse_flexible(void);
579
580 static void
581 parse_DspCell(void)
582 {
583   match_byte_assert(0);
584   match_DspSimpleText();
585   parse_flexible();             /* DspString or DspNumber. */
586 }
587
588 static void
589 match_DspCell(void)
590 {                               /* 27 80 */
591   match_byte_assert(0x27);
592   match_byte_assert(0x80);
593   parse_DspCell();
594 }
595
596 static void
597 parse_PMModelItemInfo(void)
598 {                               /* 54 80 */
599   match_byte_assert(0);
600   pos += 1;                     /* Counter */
601   match_zeros_assert(7);
602   pos += 3;
603   if (!match_byte(0))
604     match_byte_assert(0xe);
605   match_byte_assert(0);
606 }
607
608 static void
609 match_PMModelItemInfo(void)
610 {                               /* 54 80 */
611   match_byte_assert(0x54);
612   match_byte_assert(0x80);
613   parse_PMModelItemInfo();
614   match_DspSimpleText();
615   match_DspString();
616 }
617
618 static void
619 match_PMPivotItemTree(void)
620 {                               /* 52 80 */
621   match_byte_assert(0x52);
622   match_byte_assert(0x80);
623   match_byte_assert(0);
624   match_PMModelItemInfo();
625 }
626
627 static void
628 parse_flexible(void)
629 {
630   if (data[pos] == 0xff && data[pos + 1] == 0xff)
631     {
632       match_u16_assert(0xffff);
633       match_u16_assert(0);
634       char *heading = get_string2();
635       if (!strcmp(heading, "DspCell"))
636         parse_DspCell();
637       else if (!strcmp(heading, "DspNumber"))
638         parse_DspNumber();
639       else if (!strcmp(heading, "DspString"))
640         parse_DspString();
641       else
642         assert(0);
643     }
644   else if (data[pos] == 0x2a && data[pos + 1] == 0x80)
645     match_DspNumber();
646   else if (data[pos] == 0x27 && data[pos + 1] == 0x80)
647     match_DspCell();
648   else if (data[pos] == 0x5 && data[pos + 1] == 0x80)
649     match_DspString();
650   else
651     assert(0);
652 }
653
654 int
655 main(int argc, char *argv[])
656 {
657   bool print_offsets = false;
658   for (;;)
659     {
660       int c = getopt (argc, argv, "o");
661       if (c == -1)
662         break;
663
664       switch (c)
665         {
666         case 'o':
667           print_offsets = true;
668           break;
669
670         case '?':
671           exit (-1);
672         }
673     }
674   if (argc - optind != 1)
675     {
676       fprintf (stderr, "usage: %s FILE.bin", argv[0]);
677       exit (1);
678     }
679
680   const char *filename = argv[optind];
681   int fd = open(filename, O_RDONLY);
682   if (fd < 0)
683     {
684       fprintf (stderr, "%s: open failed (%s)", filename, strerror (errno));
685       exit (1);
686     }
687
688   struct stat s;
689   if (fstat(fd, &s))
690     {
691       perror("fstat");
692       exit(1);
693     }
694   n = s.st_size;
695   data = malloc(n);
696   if (!data)
697     {
698       perror("malloc");
699       exit(1);
700     }
701   if (read(fd, data, n) != n)
702     {
703       perror("read");
704       exit(1);
705     }
706   close(fd);
707
708   setvbuf (stdout, NULL, _IOLBF, 0);
709
710   match_byte_assert(4);
711   match_u32_assert(0);
712   match_string1_assert("SPSS Output Document");
713   match_u32_assert(1);
714   match_byte_assert(0x63);
715
716   parse_heading("NavRoot");
717   match_byte_assert(2);
718   match_zeros_assert(32);
719
720   parse_heading("DspSimpleText");
721   match_zeros_assert(10);
722
723   parse_heading("DspString");
724   parse_DspString();
725
726   parse_heading("NavTreeViewItem");
727   match_byte_assert(0);
728   match_u32_assert(0);
729   match_byte_assert(2);
730   match_byte_assert(0);
731   match_byte_assert(1);
732   match_zeros_assert(9);
733   match_u32_assert(1);
734   assert (pos == 0xb0);
735
736   pos += 0x28;
737   match_zeros_assert(5);
738   if (match_u32(8500))
739     match_u32_assert(11000);
740   else
741     {
742       match_u32_assert(11000);
743       match_u32_assert(8500);
744     }
745   pos = 0x105;
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);
756   pos += 2;
757   match_u16_assert(2);
758
759   parse_heading("NavLog");
760   pos = 0x36b;
761   puts(get_padded_string(32));
762   if (!match_u32(80))
763     match_u32_assert(132);
764   match_zeros_assert(8);
765   match_u32_assert(1);
766   get_string4();
767   match_byte_assert(0);
768
769   parse_heading("NavHead");
770   match_byte_assert(2);
771   match_zeros_assert(24);
772   match_u32_assert(1);
773   match_u32_assert(0);
774   match_DspSimpleText();
775   match_DspString();
776   match_NavTreeViewItem();
777   match_zeros_assert(3);
778
779   parse_heading("NavTitle");
780   pos += 33;
781   match_DspSimpleText();
782   match_DspString();
783   match_NavTreeViewItem();
784
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);
796   match_u32_assert(1);
797   get_string4();
798   match_byte_assert(0);
799
800   parse_heading("NavNote");
801   match_byte_assert(2);
802   match_zeros_assert(8);
803   match_u32_assert(24);
804   if (!match_u32(0))
805     match_u32_assert(-40);
806   pos += 8;
807   match_u32_assert(2);
808   match_u32_assert(1);
809   match_DspSimpleText();
810   match_DspString();
811   match_NavTreeViewItem();
812   match_byte_assert(1);
813
814   parse_heading("PTPivotController");
815   match_byte_assert(2);
816   pos += 8;
817   match_u32_assert(100);
818   match_u32_assert(100);
819   match_u32_assert(100);
820   match_u32_assert(100);
821
822   parse_heading("PVPivotView");
823   match_u32_assert(5);
824   match_byte_assert(0);
825
826   parse_heading("PMPivotModel");
827   match_byte_assert(3);
828
829   parse_heading("NDimensional__DspCell");
830   match_byte_assert(0);
831   match_u32_assert(1);
832
833   parse_heading("IndexedCollection");
834   match_byte_assert(0);
835   pos++;
836   match_zeros_assert(3);
837   match_byte_assert(1);
838   match_byte_assert(0);
839
840   while (data[pos] != 1)
841     {
842       if (data[pos] == 0)
843         pos++;
844       else
845         parse_flexible();
846     }
847
848   match_byte_assert(1);
849   match_byte_assert(0);
850   puts(get_string1());
851   if (!match_u32(0))
852     match_u32_assert(2);
853   puts(get_string1());
854
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);
862
863   exit (0);
864
865   parse_heading("PMPivotItemTree");
866   match_byte_assert(0);
867
868   parse_heading("AbstractTreeBranch");
869   match_byte_assert(0);
870
871   parse_heading("PMModelItemInfo");
872   parse_PMModelItemInfo();
873   match_DspSimpleText();
874   match_DspString();
875
876   match_u32_assert(7);
877   match_PMPivotItemTree();
878
879   match_u32_assert(0);
880   match_PMPivotItemTree();
881
882   match_u32_assert(0);
883   match_PMPivotItemTree();
884
885   match_u32_assert(6);
886   match_PMPivotItemTree();
887
888   match_u32_assert(0);
889   match_PMPivotItemTree();
890
891   match_u32_assert(0);
892   match_PMPivotItemTree();
893
894   match_u32_assert(0);
895   match_PMPivotItemTree();
896
897   match_u32_assert(0);
898   match_PMPivotItemTree();
899
900   match_u32_assert(0);
901   match_PMPivotItemTree();
902
903   match_u32_assert(0);
904   match_PMPivotItemTree();
905
906   match_u32_assert(2);
907   match_PMPivotItemTree();
908
909   match_u32_assert(0);
910   match_PMPivotItemTree();
911
912   match_u32_assert(0);
913   match_PMPivotItemTree();
914
915   match_u32_assert(0);
916   match_PMPivotItemTree();
917
918   match_u32_assert(0);
919   match_PMPivotItemTree();
920
921   match_u32_assert(2);
922   match_PMPivotItemTree();
923
924   match_u32_assert(0);
925   match_PMPivotItemTree();
926
927   match_u32_assert(0);
928
929   /* ...unknown... */
930
931   while (data[pos] != 0xff || data[pos + 1] != 0xff)
932     pos++;
933   parse_heading("PVViewDimension");
934
935   int i;
936   for (i = 0; data[pos + i] != 0xff || data[pos + i + 1] != 0xff; i++)
937     ;
938   hex_dump(stdout, pos, i);
939
940   printf ("%#x: end of successful parse\n", pos);
941
942   return 0;
943 }