Refine knowledge of data intro.
[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   get_u32 ();
331
332   int n_categories = get_u32();
333   if (n_categories > 0)
334     printf (", %d subcategories:", n_categories);
335   printf("\n");
336   for (int i = 0; i < n_categories; i++)
337     dump_category (level + 1);
338 }
339
340 static void
341 dump_dim(void)
342 {
343   int n_categories;
344   printf("next dim\n");
345   dump_value__ (0, false);
346
347   /* This byte is usually 0x02 but 0x00 and 0x75 (!) have also been spotted. */
348   pos++;
349
350   if (!match_byte(0) && !match_byte(1))
351     match_byte_assert(2);
352   if (!match_u32(0))
353     match_u32_assert(2);
354   if (!match_byte(0))
355     match_byte_assert(1);
356   get_u32();
357   match_byte(0);
358   match_byte(0);
359   n_categories = get_u32();
360   printf("%d nested categories\n", n_categories);
361   for (int i = 0; i < n_categories; i++)
362     dump_category (0);
363 }
364
365 int n_dims;
366 static void
367 dump_dims(void)
368 {
369   n_dims = get_u32();
370   printf ("%u dimensions\n", n_dims);
371   for (int i = 0; i < n_dims; i++)
372     {
373       printf("\n");
374       dump_dim ();
375     }
376 }
377
378 static int
379 compare_int(const void *a_, const void *b_)
380 {
381   const int *a = a_;
382   const int *b = b_;
383   return *a < *b ? -1 : *a > *b;
384 }
385
386 static void
387 dump_data(void)
388 {
389   /* The first three numbers add to the number of dimensions. */
390   int t = get_u32();
391   t += get_u32();
392   match_u32_assert(n_dims - t);
393
394   /* The next n_dims numbers are a permutation of the dimension numbers. */
395   int a[n_dims], b[n_dims];
396   for (int i = 0; i < n_dims; i++)
397     a[i] = b[i] = get_u32();
398   qsort(b, n_dims, sizeof *b, compare_int);
399   for (int i = 0; i < n_dims; i++)
400     if (b[i] != i)
401       {
402         fprintf(stderr, "bad dimension permutation:");
403         for (int i = 0; i < n_dims; i++)
404           fprintf(stderr, " %d", a[i]);
405         putc('\n', stderr);
406         exit(1);
407       }
408
409   int x = get_u32();
410   printf ("%d data values, starting at %08x\n", x, pos);
411   for (int i = 0; i < x; i++)
412     {
413       printf("%08x, index %d:\n", pos, get_u32());
414       match_u32_assert(0);
415       dump_value__(0, false);
416       putchar('\n');
417     }
418 }
419
420 static void
421 dump_title(void)
422 {
423   pos = 0x27;
424   dump_value__(0, true); putchar('\n');
425   dump_value__(0, true); putchar('\n');
426   match_byte_assert(0x31);
427   dump_value__(0, true); putchar('\n');
428   match_byte(0);
429   match_byte_assert(0x58);
430   if (match_byte(0x31))
431     {
432       dump_value__(0, false); putchar('\n');
433     }
434   else
435     match_byte_assert(0x58);
436
437
438   int n_footnotes = get_u32();
439   if (n_footnotes >= 20)
440     {
441       fprintf(stderr, "%08x: %d footnotes\n", pos - 4, n_footnotes);
442       exit(1);
443     }
444
445   printf("------\n%d footnotes\n", n_footnotes);
446   if (n_footnotes < 20)
447     {
448       for (int i = 0; i < n_footnotes; i++)
449         {
450           printf("footnote %d:\n", i);
451           dump_value__(0, false);
452           if (match_byte (0x31))
453             {
454               /* Custom footnote marker string. */
455               match_byte_assert(3);
456               get_string();
457               match_byte_assert(0x58);
458               match_u32_assert(0);
459               get_string();
460             }
461           else
462             match_byte_assert (0x58);
463           printf("(%d)\n", get_u32());
464         }
465     }
466 }
467
468 static int
469 find_dimensions(void)
470 {
471   {
472     const char dimensions[] = "-,,,.\0";
473     int x = try_find_tail(dimensions, sizeof dimensions - 1);
474     if (x)
475       return x;
476   }
477
478   const char dimensions[] = "-,,, .\0";
479   return find_tail(dimensions, sizeof dimensions - 1);
480 }
481
482 static void
483 dump_fonts(void)
484 {
485   printf("fonts: offset=%08x\n", pos);
486   match_byte(0);
487   for (int i = 1; i <= 8; i++)
488     {
489       printf("%08x: font %d, ", pos, i);
490       match_byte_assert(i);
491       match_byte_assert(0x31);
492       printf("%s, ", get_string());
493       match_byte_assert(0);
494       match_byte_assert(0);
495       if (!match_byte(0x40) && !match_byte(0x20) && !match_byte(0x80) && !match_byte(0x10))
496         match_byte_assert(0x50);
497       if (!match_byte(0x41))
498         match_byte_assert(0x51);
499       pos += 13;
500       printf ("%s, ", get_string());
501       printf ("%s, ", get_string());
502       match_u32_assert(0);
503       match_u32_assert(0);
504       pos++;
505       get_u32();
506       get_u32();
507       get_u32();
508       get_u32();
509       putchar('\n');
510     }
511
512   match_u32_assert(240);
513   pos += 240;
514
515   match_u32_assert(18);
516   pos += 18;
517
518   if (match_u32(117))
519     pos += 117;
520   else
521     {
522       match_u32_assert(142);
523       pos += 142;
524     }
525
526   int count = get_u32();
527   pos += 4 * count;
528
529   char *encoding = get_string();
530   printf("encoding=%s\n", encoding);
531
532   if (!match_u32(0))
533     match_u32_assert(UINT32_MAX);
534   if (!match_byte(0))
535     match_byte_assert(1);
536   match_byte_assert(0);
537   if (!match_byte(0))
538     match_byte_assert(1);
539   if (!match_byte(0x99) && !match_byte(0x98))
540     match_byte_assert(0x97);
541   match_byte_assert(7);
542   match_byte_assert(0);
543   match_byte_assert(0);
544   if (match_byte('.'))
545     match_byte_assert(',');
546   else
547     {
548       match_byte_assert(',');
549       if (!match_byte('.'))
550         match_byte_assert(' ');
551     }
552   match_u32_assert(5);
553   for (int i = 0; i < 5; i++)
554     get_string();
555   pos += get_u32();
556   if (pos != find_dimensions())
557     fprintf (stderr, "%08x / %08x\n", pos, find_dimensions());
558 }
559
560 int
561 main(int argc, char *argv[])
562 {
563   size_t start;
564   struct stat s;
565
566   if (isatty(STDIN_FILENO))
567     {
568       fprintf(stderr, "redirect stdin from a .bin file\n");
569       exit(1);
570     }
571   if (fstat(STDIN_FILENO, &s))
572     {
573       perror("fstat");
574       exit(1);
575     }
576   n = s.st_size;
577   data = malloc(n);
578   if (!data)
579     {
580       perror("malloc");
581       exit(1);
582     }
583   if (read(STDIN_FILENO, data, n) != n)
584     {
585       perror("read");
586       exit(1);
587     }
588
589   if (argc > 1)
590     {
591       if (!strcmp(argv[1], "title0"))
592         {
593           pos = 0x27;
594           if (match_byte (0x03)
595               || (match_byte (0x05) && match_byte (0x58)))
596             printf ("%s\n", get_string());
597           else
598             printf ("<unknown>\n");
599           return 0;
600         }
601       else if (!strcmp(argv[1], "title"))
602         {
603           dump_title();
604           exit(0);
605         }
606       else if (!strcmp(argv[1], "titleraw"))
607         {
608           const char fonts[] = "\x01\x31\x09\0\0\0SansSerif";
609           start = 0x27;
610           n = find(fonts, sizeof fonts - 1);
611         }
612       else if (!strcmp(argv[1], "fonts"))
613         {
614           const char fonts[] = "\x01\x31\x09\0\0\0SansSerif";
615           const char styles[] = "\xf0\0\0\0";
616           start = find(fonts, sizeof fonts - 1);
617           n = find(styles, sizeof styles - 1);
618         }
619       else if (!strcmp(argv[1], "styles"))
620         {
621           const char styles[] = "\xf0\0\0\0";
622           const char dimensions[] = "-,,,.\0";
623           start = find(styles, sizeof styles - 1);
624           n = find(dimensions, sizeof dimensions - 1) + sizeof dimensions - 1;
625         }
626       else if (!strcmp(argv[1], "dimensions") || !strcmp(argv[1], "all"))
627         {
628           pos = 0;
629           match_byte_assert(1);
630           match_byte_assert(0);
631           match_u32_assert(3);
632           match_byte_assert(1);
633           if (!match_byte(0))
634             match_byte_assert(1);
635           match_byte_assert(0);
636           match_byte_assert(0);
637           if (!match_byte(0))
638             match_byte_assert(1);
639           pos++;
640           match_byte_assert(0);
641           match_byte_assert(0);
642           match_byte_assert(0);
643           dump_title ();
644           dump_fonts();
645           dump_dims ();
646           printf("\n\ndata:\n");
647           dump_data ();
648           match_byte (1);
649           if (pos != n)
650             {
651               fprintf (stderr, "%x / %x\n", pos, n);
652               exit(1);
653             }
654           exit(0);
655         }
656       else
657         {
658           fprintf (stderr, "unknown section %s\n", argv[1]);
659           exit(1);
660         }
661     }
662   else
663     start = 0x27;
664
665   for (size_t i = start; i < n; )
666     {
667       if (i + 5 <= n
668           && data[i]
669           //&& !data[i + 1]
670           && !data[i + 2]
671           && !data[i + 3]
672           && i + 4 + data[i] + data[i + 1] * 256 <= n
673           && all_ascii(&data[i + 4], data[i] + data[i + 1] * 256))
674         {
675           fputs("\n\"", stdout);
676           fwrite(&data[i + 4], 1, data[i] + data[i + 1] * 256, stdout);
677           fputs("\" ", stdout);
678
679           i += 4 + data[i] + data[i + 1] * 256;
680         }
681       else if (i + 12 <= n
682                && data[i + 1] == 40
683                && data[i + 2] == 5
684                && data[i + 3] == 0)
685         {
686           double d;
687
688           memcpy (&d, &data[i + 4], 8);
689           printf ("F40.%d(%.*f)\n", data[i], data[i], d);
690           i += 12;
691         }
692       else if (i + 12 <= n
693                && data[i + 1] == 40
694                && data[i + 2] == 31
695                && data[i + 3] == 0)
696         {
697           double d;
698
699           memcpy (&d, &data[i + 4], 8);
700           printf ("PCT40.%d(%.*f)\n", data[i], data[i], d);
701           i += 12;
702         }
703       else if (i + 4 <= n
704                && (data[i] && data[i] != 88 && data[i] != 0x41)
705                && !data[i + 1]
706                && !data[i + 2]
707                && !data[i + 3])
708         {
709           printf ("i%d ", data[i]);
710           i += 4;
711         }
712       else
713         {
714           printf("%02x ", data[i]);
715           i++;
716         }
717     }
718
719   return 0;
720 }