Tie down category counting a bit better.
[pspp] / dump.c
1 #include <stdbool.h>
2 #include <stdint.h>
3 #include <stdio.h>
4 #include <stdlib.h>
5 #include <string.h>
6 #include <sys/stat.h>
7 #include <unistd.h>
8
9 static uint8_t *data;
10 static size_t n;
11
12 static bool
13 all_ascii(const uint8_t *p, size_t n)
14 {
15   for (size_t i = 0; i < n; i++)
16     if (p[i] < 32 || p[i] > 126)
17       return false;
18   return true;
19 }
20
21 static size_t
22 try_find(const char *target, size_t target_len)
23 {
24   const uint8_t *pos = (const uint8_t *) memmem (data, n, target, target_len);
25   return pos ? pos - data : 0;
26 }
27
28 static size_t
29 try_find_tail(const char *target, size_t target_len)
30 {
31   size_t pos = try_find(target, target_len);
32   return pos ? pos + target_len : 0;
33 }
34
35 static size_t
36 find(const char *target, size_t target_len)
37 {
38   size_t pos = try_find(target, target_len);
39   if (!pos)
40     {
41       fprintf (stderr, "not found\n");
42       exit(1);
43     }
44   return pos;
45 }
46
47 static size_t
48 find_tail(const char *target, size_t target_len)
49 {
50   size_t pos = try_find_tail(target, target_len);
51   if (!pos)
52     {
53       fprintf (stderr, "not found\n");
54       exit(1);
55     }
56   return pos;
57 }
58
59 size_t pos;
60
61 #define XSTR(x) #x
62 #define STR(x) XSTR(x)
63 #define WHERE __FILE__":" STR(__LINE__)
64
65 static unsigned int
66 get_u32(void)
67 {
68   uint32_t x;
69   memcpy(&x, &data[pos], 4);
70   pos += 4;
71   return x;
72 }
73
74 static double
75 get_double(void)
76 {
77   double x;
78   memcpy(&x, &data[pos], 8);
79   pos += 8;
80   return x;
81 }
82
83 static bool
84 match_u32(uint32_t x)
85 {
86   if (get_u32() == x)
87     return true;
88   pos -= 4;
89   return false;
90 }
91
92 static void
93 match_u32_assert(uint32_t x, const char *where)
94 {
95   unsigned int y = get_u32();
96   if (x != y)
97     {
98       fprintf(stderr, "%s: 0x%x: expected i%u, got i%u\n", where, pos - 4, x, y);
99       exit(1);
100     }
101 }
102 #define match_u32_assert(x) match_u32_assert(x, WHERE)
103
104 static bool
105 match_byte(uint8_t b)
106 {
107   if (pos < n && data[pos] == b)
108     {
109       pos++;
110       return true;
111     }
112   else
113     return false;
114 }
115
116 static void
117 match_byte_assert(uint8_t b, const char *where)
118 {
119   if (!match_byte(b))
120     {
121       fprintf(stderr, "%s: 0x%x: expected %02x, got %02x\n", where, pos, b, data[pos]);
122       exit(1);
123     }
124 }
125 #define match_byte_assert(b) match_byte_assert(b, WHERE)
126
127 static char *
128 get_string(const char *where)
129 {
130   if (1
131       /*data[pos + 1] == 0 && data[pos + 2] == 0 && data[pos + 3] == 0*/
132       /*&& all_ascii(&data[pos + 4], data[pos])*/)
133     {
134       int len = data[pos] + data[pos + 1] * 256;
135       char *s = malloc(len + 1);
136
137       memcpy(s, &data[pos + 4], len);
138       s[len] = 0;
139       pos += 4 + len;
140       return s;
141     }
142   else
143     {
144       fprintf(stderr, "%s: 0x%x: expected string\n", where, pos);
145       exit(1);
146     }
147 }
148 #define get_string() get_string(WHERE)
149
150 static void
151 dump_value_31(void)
152 {
153   if (match_byte (0x31))
154     {
155       if (match_u32 (0))
156         {
157           if (match_u32 (1))
158             get_string();
159           else
160             match_u32_assert (0);
161           int subn = get_u32 ();
162           printf ("nested %d bytes", subn);
163           pos += subn;
164         }
165       else if (match_u32 (1))
166         {
167           printf("(footnote %d) ", get_u32());
168           match_byte_assert (0);
169           match_byte_assert (0);
170           int subn = get_u32 ();
171           printf ("nested %d bytes", subn);
172           pos += subn;
173         }
174       else if (match_u32 (2))
175         {
176           printf("(special 2)");
177           match_byte_assert(0);
178           match_byte_assert(0);
179           if (!match_u32 (2))
180             match_u32_assert(1);
181           match_byte_assert(0);
182           match_byte_assert(0);
183           int subn = get_u32 ();
184           printf ("nested %d bytes", subn);
185           pos += subn;
186         }
187       else
188         {
189           match_u32_assert(3);
190           printf("(special 3)");
191           match_byte_assert(0);
192           match_byte_assert(0);
193           match_byte_assert(1);
194           match_byte_assert(0);
195           int subn = get_u32 ();
196           printf ("nested %d bytes, ", subn);
197           pos += subn;
198           subn = get_u32 ();
199           printf ("nested %d bytes, ", subn);
200           pos += subn;
201         }
202     }
203   else
204     match_byte_assert (0x58);
205 }
206
207 static void
208 dump_value__(int level, bool match1)
209 {
210   for (int i = 0; i <= level; i++)
211     printf ("    ");
212
213   match_byte(0);
214   match_byte(0);
215   match_byte(0);
216   match_byte(0);
217
218   if (match_byte (3))
219     {
220       char *s1 = get_string();
221       dump_value_31();
222       char *s2 = get_string();
223       char *s3 = get_string();
224       if (strcmp(s1, s3))
225         printf("strings \"%s\", \"%s\" and \"%s\"", s1, s2, s3);
226       else
227         printf("string \"%s\" and \"%s\"", s1, s2);
228       if (!match_byte (0))
229         match_byte_assert(1);
230       if (match1)
231         match_byte (1);
232     }
233   else if (match_byte (5))
234     {
235       dump_value_31();
236       printf ("variable \"%s\"", get_string());
237       get_string();
238       if (!match_byte(1) && !match_byte(2))
239         match_byte_assert(3);
240     }
241   else if (match_byte (2))
242     {
243       unsigned int format;
244       char *var, *vallab;
245       double value;
246
247       match_byte_assert (0x58);
248       format = get_u32 ();
249       value = get_double ();
250       var = get_string ();
251       vallab = get_string ();
252       printf ("value %g format %d(%d.%d) var \"%s\" vallab \"%s\"",
253               value, format >> 16, (format >> 8) & 0xff, format & 0xff, var, vallab);
254       if (!match_byte (1) && !match_byte(2))
255         match_byte_assert (3);
256     }
257   else if (match_byte (4))
258     {
259       unsigned int format;
260       char *var, *vallab, *value;
261
262       match_byte_assert (0x58);
263       format = get_u32 ();
264       vallab = get_string ();
265       var = get_string ();
266       if (!match_byte(1) && !match_byte(2))
267         match_byte_assert (3);
268       value = get_string ();
269       printf ("value \"%s\" format %d(%d.%d) var \"%s\" vallab \"%s\"",
270               value, format >> 16, (format >> 8) & 0xff, format & 0xff, var, vallab);
271     }
272   else if (match_byte (1))
273     {
274       unsigned int format;
275       double value;
276
277       dump_value_31();
278       format = get_u32 ();
279       value = get_double ();
280       printf ("value %g format %d(%d.%d)", value, format >> 16, (format >> 8) & 0xff, format & 0xff);
281       if (match1)
282         match_byte (1);
283     }
284   else
285     {
286       dump_value_31();
287
288       char *base = get_string();
289       int x = get_u32();
290       printf ("\"%s\" with %d variables:\n", base, x);
291       for (int i = 0; i < x; i++)
292         {
293           int y = get_u32();
294           if (!y)
295             y = 1;
296           else
297             match_u32_assert(0);
298           for (int j = 0; j <= level; j++)
299             printf ("    ");
300           printf("variable %d has %d values:\n", i, y);
301           for (int j = 0; j < y; j++)
302             {
303               dump_value__ (level + 1, false);
304               putchar('\n');
305             }
306         }
307     }
308 }
309
310 static void
311 dump_category(int level)
312 {
313   dump_value__ (level, true);
314   match_byte(0);
315   match_byte(0);
316   match_byte(0);
317
318   if (match_u32 (1))
319     match_byte (0);
320   else if (match_byte (1))
321     {
322       match_byte (0);
323       if (!match_u32 (2))
324         match_u32_assert (1);
325       match_byte (0);
326     }
327   else if (!match_u32(2))
328     match_u32_assert (0);
329
330   int indx = get_u32();
331   int n_categories = get_u32();
332   if (indx != -1 && n_categories != 0)
333     {
334       fprintf(stderr, "index not -1 but subcategories\n");
335       exit(1);
336     }
337   if (n_categories > 0)
338     printf (", %d subcategories:", n_categories);
339   printf("\n");
340   for (int i = 0; i < n_categories; i++)
341     dump_category (level + 1);
342 }
343
344 static void
345 dump_dim(void)
346 {
347   int n_categories;
348   printf("next dim\n");
349   dump_value__ (0, false);
350
351   /* This byte is usually 0x02 but 0x00 and 0x75 (!) have also been spotted. */
352   pos++;
353
354   if (!match_byte(0) && !match_byte(1))
355     match_byte_assert(2);
356   if (!match_u32(0))
357     match_u32_assert(2);
358   if (!match_byte(0))
359     match_byte_assert(1);
360   get_u32();
361   match_byte(0);
362   match_byte(0);
363   n_categories = get_u32();
364   printf("%d nested categories\n", n_categories);
365   for (int i = 0; i < n_categories; i++)
366     dump_category (0);
367 }
368
369 int n_dims;
370 static void
371 dump_dims(void)
372 {
373   n_dims = get_u32();
374   printf ("%u dimensions\n", n_dims);
375   for (int i = 0; i < n_dims; i++)
376     {
377       printf("\n");
378       dump_dim ();
379     }
380 }
381
382 static int
383 compare_int(const void *a_, const void *b_)
384 {
385   const int *a = a_;
386   const int *b = b_;
387   return *a < *b ? -1 : *a > *b;
388 }
389
390 static void
391 dump_data(void)
392 {
393   /* The first three numbers add to the number of dimensions. */
394   int t = get_u32();
395   t += get_u32();
396   match_u32_assert(n_dims - t);
397
398   /* The next n_dims numbers are a permutation of the dimension numbers. */
399   int a[n_dims], b[n_dims];
400   for (int i = 0; i < n_dims; i++)
401     a[i] = b[i] = get_u32();
402   qsort(b, n_dims, sizeof *b, compare_int);
403   for (int i = 0; i < n_dims; i++)
404     if (b[i] != i)
405       {
406         fprintf(stderr, "bad dimension permutation:");
407         for (int i = 0; i < n_dims; i++)
408           fprintf(stderr, " %d", a[i]);
409         putc('\n', stderr);
410         exit(1);
411       }
412
413   int x = get_u32();
414   printf ("%d data values, starting at %08x\n", x, pos);
415   for (int i = 0; i < x; i++)
416     {
417       printf("%08x, index %d:\n", pos, get_u32());
418       match_u32_assert(0);
419       dump_value__(0, false);
420       putchar('\n');
421     }
422 }
423
424 static void
425 dump_title(void)
426 {
427   pos = 0x27;
428   dump_value__(0, true); putchar('\n');
429   dump_value__(0, true); putchar('\n');
430   match_byte_assert(0x31);
431   dump_value__(0, true); putchar('\n');
432   match_byte(0);
433   match_byte_assert(0x58);
434   if (match_byte(0x31))
435     {
436       dump_value__(0, false); putchar('\n');
437     }
438   else
439     match_byte_assert(0x58);
440
441
442   int n_footnotes = get_u32();
443   if (n_footnotes >= 20)
444     {
445       fprintf(stderr, "%08x: %d footnotes\n", pos - 4, n_footnotes);
446       exit(1);
447     }
448
449   printf("------\n%d footnotes\n", n_footnotes);
450   if (n_footnotes < 20)
451     {
452       for (int i = 0; i < n_footnotes; i++)
453         {
454           printf("footnote %d:\n", i);
455           dump_value__(0, false);
456           if (match_byte (0x31))
457             {
458               /* Custom footnote marker string. */
459               match_byte_assert(3);
460               get_string();
461               match_byte_assert(0x58);
462               match_u32_assert(0);
463               get_string();
464             }
465           else
466             match_byte_assert (0x58);
467           printf("(%d)\n", get_u32());
468         }
469     }
470 }
471
472 static int
473 find_dimensions(void)
474 {
475   {
476     const char dimensions[] = "-,,,.\0";
477     int x = try_find_tail(dimensions, sizeof dimensions - 1);
478     if (x)
479       return x;
480   }
481
482   const char dimensions[] = "-,,, .\0";
483   return find_tail(dimensions, sizeof dimensions - 1);
484 }
485
486 static void
487 dump_fonts(void)
488 {
489   printf("fonts: offset=%08x\n", pos);
490   match_byte(0);
491   for (int i = 1; i <= 8; i++)
492     {
493       printf("%08x: font %d, ", pos, i);
494       match_byte_assert(i);
495       match_byte_assert(0x31);
496       printf("%s, ", get_string());
497       match_byte_assert(0);
498       match_byte_assert(0);
499       if (!match_byte(0x40) && !match_byte(0x20) && !match_byte(0x80) && !match_byte(0x10))
500         match_byte_assert(0x50);
501       if (!match_byte(0x41))
502         match_byte_assert(0x51);
503       pos += 13;
504       printf ("%s, ", get_string());
505       printf ("%s, ", get_string());
506       match_u32_assert(0);
507       match_u32_assert(0);
508       pos++;
509       get_u32();
510       get_u32();
511       get_u32();
512       get_u32();
513       putchar('\n');
514     }
515
516   match_u32_assert(240);
517   pos += 240;
518
519   match_u32_assert(18);
520   pos += 18;
521
522   if (match_u32(117))
523     pos += 117;
524   else
525     {
526       match_u32_assert(142);
527       pos += 142;
528     }
529
530   int count = get_u32();
531   pos += 4 * count;
532
533   char *encoding = get_string();
534   printf("encoding=%s\n", encoding);
535
536   if (!match_u32(0))
537     match_u32_assert(UINT32_MAX);
538   if (!match_byte(0))
539     match_byte_assert(1);
540   match_byte_assert(0);
541   if (!match_byte(0))
542     match_byte_assert(1);
543   if (!match_byte(0x99) && !match_byte(0x98))
544     match_byte_assert(0x97);
545   match_byte_assert(7);
546   match_byte_assert(0);
547   match_byte_assert(0);
548   if (match_byte('.'))
549     match_byte_assert(',');
550   else
551     {
552       match_byte_assert(',');
553       if (!match_byte('.'))
554         match_byte_assert(' ');
555     }
556   match_u32_assert(5);
557   for (int i = 0; i < 5; i++)
558     get_string();
559   pos += get_u32();
560   if (pos != find_dimensions())
561     fprintf (stderr, "%08x / %08x\n", pos, find_dimensions());
562 }
563
564 int
565 main(int argc, char *argv[])
566 {
567   size_t start;
568   struct stat s;
569
570   if (isatty(STDIN_FILENO))
571     {
572       fprintf(stderr, "redirect stdin from a .bin file\n");
573       exit(1);
574     }
575   if (fstat(STDIN_FILENO, &s))
576     {
577       perror("fstat");
578       exit(1);
579     }
580   n = s.st_size;
581   data = malloc(n);
582   if (!data)
583     {
584       perror("malloc");
585       exit(1);
586     }
587   if (read(STDIN_FILENO, data, n) != n)
588     {
589       perror("read");
590       exit(1);
591     }
592
593   if (argc > 1)
594     {
595       if (!strcmp(argv[1], "title0"))
596         {
597           pos = 0x27;
598           if (match_byte (0x03)
599               || (match_byte (0x05) && match_byte (0x58)))
600             printf ("%s\n", get_string());
601           else
602             printf ("<unknown>\n");
603           return 0;
604         }
605       else if (!strcmp(argv[1], "title"))
606         {
607           dump_title();
608           exit(0);
609         }
610       else if (!strcmp(argv[1], "titleraw"))
611         {
612           const char fonts[] = "\x01\x31\x09\0\0\0SansSerif";
613           start = 0x27;
614           n = find(fonts, sizeof fonts - 1);
615         }
616       else if (!strcmp(argv[1], "fonts"))
617         {
618           const char fonts[] = "\x01\x31\x09\0\0\0SansSerif";
619           const char styles[] = "\xf0\0\0\0";
620           start = find(fonts, sizeof fonts - 1);
621           n = find(styles, sizeof styles - 1);
622         }
623       else if (!strcmp(argv[1], "styles"))
624         {
625           const char styles[] = "\xf0\0\0\0";
626           const char dimensions[] = "-,,,.\0";
627           start = find(styles, sizeof styles - 1);
628           n = find(dimensions, sizeof dimensions - 1) + sizeof dimensions - 1;
629         }
630       else if (!strcmp(argv[1], "dimensions") || !strcmp(argv[1], "all"))
631         {
632           pos = 0;
633           match_byte_assert(1);
634           match_byte_assert(0);
635           match_u32_assert(3);
636           match_byte_assert(1);
637           if (!match_byte(0))
638             match_byte_assert(1);
639           match_byte_assert(0);
640           match_byte_assert(0);
641           if (!match_byte(0))
642             match_byte_assert(1);
643           pos++;
644           match_byte_assert(0);
645           match_byte_assert(0);
646           match_byte_assert(0);
647           dump_title ();
648           dump_fonts();
649           dump_dims ();
650           printf("\n\ndata:\n");
651           dump_data ();
652           match_byte (1);
653           if (pos != n)
654             {
655               fprintf (stderr, "%x / %x\n", pos, n);
656               exit(1);
657             }
658           exit(0);
659         }
660       else
661         {
662           fprintf (stderr, "unknown section %s\n", argv[1]);
663           exit(1);
664         }
665     }
666   else
667     start = 0x27;
668
669   for (size_t i = start; i < n; )
670     {
671       if (i + 5 <= n
672           && data[i]
673           //&& !data[i + 1]
674           && !data[i + 2]
675           && !data[i + 3]
676           && i + 4 + data[i] + data[i + 1] * 256 <= n
677           && all_ascii(&data[i + 4], data[i] + data[i + 1] * 256))
678         {
679           fputs("\n\"", stdout);
680           fwrite(&data[i + 4], 1, data[i] + data[i + 1] * 256, stdout);
681           fputs("\" ", stdout);
682
683           i += 4 + data[i] + data[i + 1] * 256;
684         }
685       else if (i + 12 <= n
686                && data[i + 1] == 40
687                && data[i + 2] == 5
688                && data[i + 3] == 0)
689         {
690           double d;
691
692           memcpy (&d, &data[i + 4], 8);
693           printf ("F40.%d(%.*f)\n", data[i], data[i], d);
694           i += 12;
695         }
696       else if (i + 12 <= n
697                && data[i + 1] == 40
698                && data[i + 2] == 31
699                && data[i + 3] == 0)
700         {
701           double d;
702
703           memcpy (&d, &data[i + 4], 8);
704           printf ("PCT40.%d(%.*f)\n", data[i], data[i], d);
705           i += 12;
706         }
707       else if (i + 4 <= n
708                && (data[i] && data[i] != 88 && data[i] != 0x41)
709                && !data[i + 1]
710                && !data[i + 2]
711                && !data[i + 3])
712         {
713           printf ("i%d ", data[i]);
714           i += 4;
715         }
716       else
717         {
718           printf("%02x ", data[i]);
719           i++;
720         }
721     }
722
723   return 0;
724 }