learned some things about spos
[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_string1(void)
254 {
255   int len = data[pos++];
256   char *s = xmemdup0(&data[pos], len);
257   pos += len;
258   return s;
259 }
260
261 static void
262 match_string1_assert(const char *exp, const char *where)
263 {
264   int start = pos;
265   char *act = get_string1();
266   if (strcmp(act, exp)) 
267     {
268       fprintf(stderr, "%s: 0x%x: expected \"%s\", got \"%s\"\n",
269               where, start, exp, act);
270       exit(1);
271     }
272 }
273 #define match_string1_assert(x) match_string1_assert(x, WHERE)
274
275 static char *
276 get_string2(void)
277 {
278   int len = data[pos] + data[pos + 1] * 256;
279   char *s = xmemdup0(&data[pos + 2], len);
280   pos += 2 + len;
281   return s;
282 }
283
284 static void
285 match_string2_assert(const char *exp, const char *where)
286 {
287   int start = pos;
288   char *act = get_string2();
289   if (strcmp(act, exp)) 
290     {
291       fprintf(stderr, "%s: 0x%x: expected \"%s\", got \"%s\"\n",
292               where, start, exp, act);
293       exit(1);
294     }
295 }
296 #define match_string2_assert(x) match_string2_assert(x, WHERE)
297
298 static char *
299 get_string(const char *where)
300 {
301   if (1
302       /*data[pos + 1] == 0 && data[pos + 2] == 0 && data[pos + 3] == 0*/
303       /*&& all_ascii(&data[pos + 4], data[pos])*/)
304     {
305       int len = data[pos] + data[pos + 1] * 256;
306       char *s = malloc(len + 1);
307
308       memcpy(s, &data[pos + 4], len);
309       s[len] = 0;
310       pos += 4 + len;
311       return s;
312     }
313   else
314     {
315       fprintf(stderr, "%s: 0x%x: expected string\n", where, pos);
316       exit(1);
317     }
318 }
319 #define get_string() get_string(WHERE)
320
321 static char *
322 get_string_be(const char *where)
323 {
324   if (1
325       /*data[pos + 1] == 0 && data[pos + 2] == 0 && data[pos + 3] == 0*/
326       /*&& all_ascii(&data[pos + 4], data[pos])*/)
327     {
328       int len = data[pos + 2] * 256 + data[pos + 3];
329       char *s = malloc(len + 1);
330
331       memcpy(s, &data[pos + 4], len);
332       s[len] = 0;
333       pos += 4 + len;
334       return s;
335     }
336   else
337     {
338       fprintf(stderr, "%s: 0x%x: expected string\n", where, pos);
339       exit(1);
340     }
341 }
342 #define get_string_be() get_string_be(WHERE)
343
344 static int
345 get_end(void)
346 {
347   int len = get_u32();
348   return pos + len;
349 }
350
351 static void __attribute__((unused))
352 hex_dump(FILE *stream, int ofs, int n)
353 {
354   int n_ascii = 0;
355   for (int i = 0; i < n; i++)
356     {
357       int c = data[ofs + i];
358       n_ascii += is_ascii(c);
359       fprintf(stream, " %02x", c);
360     }
361   if (n_ascii >= 3)
362     {
363       putc(' ', stream);
364       for (int i = 0; i < n; i++)
365         {
366           int c = data[ofs + i];
367           putc(c >= 32 && c < 127 ? c : '.', stream);
368         }
369     }
370   putc('\n', stream);
371 }
372
373 static void __attribute__((unused))
374 char_dump(FILE *stream, int ofs, int n)
375 {
376   for (int i = 0; i < n; i++)
377     {
378       int c = data[ofs + i];
379       putc(c >= 32 && c < 127 ? c : '.', stream);
380     }
381   putc('\n', stream);
382 }
383
384
385 static int
386 compare_int(const void *a_, const void *b_)
387 {
388   const int *a = a_;
389   const int *b = b_;
390   return *a < *b ? -1 : *a > *b;
391 }
392
393
394 static const char *
395 format_name (int format, char *buf)
396 {
397   switch (format)
398     {
399     case 1: return "A";
400     case 2: return "AHEX";
401     case 3: return "COMMA";
402     case 4: return "DOLLAR";
403     case 5: case 40: return "F";
404     case 6: return "IB";
405     case 7: return "PIBHEX";
406     case 8: return "P";
407     case 9: return "PIB";
408     case 10: return "PK";
409     case 11: return "RB";
410     case 12: return "RBHEX";
411     case 15: return "Z";
412     case 16: return "N";
413     case 17: return "E";
414     case 20: return "DATE";
415     case 21: return "TIME";
416     case 22: return "DATETIME";
417     case 23: return "ADATE";
418     case 24: return "JDATE";
419     case 25: return "DTIME";
420     case 26: return "WKDAY";
421     case 27: return "MONTH";
422     case 28: return "MOYR";
423     case 29: return "QYR";
424     case 30: return "WKYR";
425     case 31: return "PCT";
426     case 32: return "DOT";
427     case 33: return "CCA";
428     case 34: return "CCB";
429     case 35: return "CCC";
430     case 36: return "CCD";
431     case 37: return "CCE";
432     case 38: return "EDATE";
433     case 39: return "SDATE";
434     default: sprintf(buf, "(%d)", format); return buf;
435     }
436 }
437
438 static void
439 parse_heading(const char *name)
440 {
441   match_u16_assert(0xffff);
442   match_u16_assert(0);
443   match_string2_assert(name);
444 }
445
446 static void
447 match_zeros_assert(int count)
448 {
449   for (int i = 0; i < count; i++)
450     if (data[pos + i])
451       {
452         fprintf (stderr,
453                  "%#x: expected %d zeros here but offset %d is %#"PRIx8"\n",
454                  pos, count, i, data[pos + i]);
455         exit (1);
456       }
457   pos += count;
458 }
459
460 static void
461 parse_DspString(void)
462 {
463   match_byte_assert(1);
464   match_byte_assert(2);
465   match_byte_assert(40);
466   if (!match_byte(0))
467     match_byte_assert(5);
468   match_byte_assert(0);
469   match_byte_assert(1);
470   printf ("DspString(\"%s\")\n", get_string1());
471 }
472
473 int
474 main(int argc, char *argv[])
475 {
476   bool print_offsets = false;
477   for (;;)
478     {
479       int c = getopt (argc, argv, "o");
480       if (c == -1)
481         break;
482
483       switch (c)
484         {
485         case 'o':
486           print_offsets = true;
487           break;
488
489         case '?':
490           exit (-1);
491         }
492     }
493   if (argc - optind != 1)
494     {
495       fprintf (stderr, "usage: %s FILE.bin", argv[0]);
496       exit (1);
497     }
498
499   const char *filename = argv[optind];
500   int fd = open(filename, O_RDONLY);
501   if (fd < 0)
502     {
503       fprintf (stderr, "%s: open failed (%s)", filename, strerror (errno));
504       exit (1);
505     }
506
507   struct stat s;
508   if (fstat(fd, &s))
509     {
510       perror("fstat");
511       exit(1);
512     }
513   n = s.st_size;
514   data = malloc(n);
515   if (!data)
516     {
517       perror("malloc");
518       exit(1);
519     }
520   if (read(fd, data, n) != n)
521     {
522       perror("read");
523       exit(1);
524     }
525   close(fd);
526
527   setvbuf (stdout, NULL, _IOLBF, 0);
528
529   match_byte_assert(4);
530   match_u32_assert(0);
531   match_string1_assert("SPSS Output Document");
532   match_u32_assert(1);
533   match_byte_assert(0x63);
534
535   parse_heading("NavRoot");
536   match_byte_assert(2);
537   match_zeros_assert(32);
538
539   parse_heading("DspSimpleText");
540   match_zeros_assert(10);
541
542   parse_heading("DspString");
543   parse_DspString();
544
545   parse_heading("NavTreeViewItem");
546   
547
548   printf ("%#x: end of successful parse\n", pos);
549
550   return 0;
551 }