65bd4831cecba3a820e3fe7eb003d8db56ba91e3
[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 (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 (data[pos + 1] == 0 && data[pos + 2] == 0 && data[pos + 3] == 0
131       /*&& all_ascii(&data[pos + 4], data[pos])*/)
132     {
133       int len = data[pos];
134       char *s = malloc(len + 1);
135
136       memcpy(s, &data[pos + 4], len);
137       s[len] = 0;
138       pos += 4 + data[pos];
139       return s;
140     }
141   else
142     {
143       fprintf(stderr, "%s: 0x%x: expected string\n", where, pos);
144       exit(1);
145     }
146 }
147 #define get_string() get_string(WHERE)
148
149 static void
150 dump_value(int level)
151 {
152   for (int i = 0; i <= level; i++)
153     printf ("    ");
154
155   match_byte (0);
156   match_byte (0);
157   match_byte (0);
158   match_byte (0);
159   if (match_byte (3))
160     {
161       get_string();
162       if (match_byte (0x31))
163         {
164           if (match_u32 (1))
165             {
166               printf("(footnote %d) ", get_u32());
167               match_byte_assert (0);
168               match_byte_assert (0);
169               int subn = get_u32 ();
170               printf ("nested %d bytes", subn);
171               pos += subn;
172             }
173           else if (match_u32 (2))
174             {
175               printf("(special 2)");
176               match_byte_assert(0);
177               match_byte_assert(0);
178               match_u32_assert(1);
179               match_byte_assert(0);
180               match_byte_assert(0);
181               int subn = get_u32 ();
182               printf ("nested %d bytes", subn);
183               pos += subn;
184             }
185           else
186             {
187               match_u32_assert(3);
188               printf("(special 3)");
189               match_byte_assert(0);
190               match_byte_assert(0);
191               match_byte_assert(1);
192               match_byte_assert(0);
193               int subn = get_u32 ();
194               printf ("nested %d bytes, ", subn);
195               pos += subn;
196               subn = get_u32 ();
197               printf ("nested %d bytes, ", subn);
198               pos += subn;
199             }
200         }
201       else
202         match_byte_assert (0x58);
203       get_string();
204       printf("string \"%s\"", get_string());
205       match_byte (0);
206       match_byte (0);
207       match_byte (0);
208       match_byte (1);
209       match_byte (1);
210       match_byte (0);
211       match_byte (0);
212       match_byte (0);
213       match_byte (1);
214     }
215   else if (match_byte (5))
216     {
217       match_byte_assert (0x58);
218       printf ("variable \"%s\"", get_string());
219       get_string();
220       if (!match_byte(1) && !match_byte(2))
221         match_byte_assert(3);
222       match_byte (0);
223       match_byte (0);
224       match_byte (0);
225       match_byte (0);
226     }
227   else if (match_byte (2))
228     {
229       unsigned int format;
230       char *var, *vallab;
231       double value;
232
233       match_byte_assert (0x58);
234       format = get_u32 ();
235       value = get_double ();
236       var = get_string ();
237       vallab = get_string ();
238       printf ("value %g format %d(%d.%d) var \"%s\" vallab \"%s\"",
239               value, format >> 16, (format >> 8) & 0xff, format & 0xff, var, vallab);
240       if (!match_byte (1) && !match_byte(2))
241         match_byte_assert (3);
242       match_byte (0);
243       match_byte (0);
244       match_byte (0);
245       match_byte (0);
246       match_byte (0);
247       match_byte (0);
248       match_byte (0);
249     }
250   else if (match_byte (4))
251     {
252       unsigned int format;
253       char *var, *vallab, *value;
254
255       match_byte_assert (0x58);
256       format = get_u32 ();
257       vallab = get_string ();
258       var = get_string ();
259       if (!match_byte(1) && !match_byte(2))
260         match_byte_assert (3);
261       value = get_string ();
262       printf ("value \"%s\" format %d(%d.%d) var \"%s\" vallab \"%s\"",
263               value, format >> 16, (format >> 8) & 0xff, format & 0xff, var, vallab);
264       match_byte (0);
265       match_byte (0);
266       match_byte (0);
267       match_byte (0);
268     }
269   else if (match_byte (1))
270     {
271       unsigned int format;
272       double value;
273
274       match_byte_assert (0x58);
275       format = get_u32 ();
276       value = get_double ();
277       printf ("value %g format %d(%d.%d)", value, format >> 16, (format >> 8) & 0xff, format & 0xff);
278       match_byte (1);
279       match_byte (0);
280       match_byte (0);
281       match_byte (0);
282       match_byte (1);
283     }
284   else if (match_byte (0x31))
285     {
286       int subn;
287
288       if (match_u32 (1))
289         {
290           printf("(footnote %d) ", get_u32());
291           match_byte_assert (0);
292           match_byte_assert (0);
293           int subn = get_u32 ();
294           printf ("nested %d bytes", subn);
295           pos += subn;
296         }
297       else
298         {
299           match_u32_assert (0);
300           match_u32_assert (0);
301           subn = get_u32 ();
302           printf ("nested %d bytes", subn);
303           pos += subn;
304         }
305       printf ("; \"%s\", substitutions:", get_string());
306       int total_subs = get_u32();
307       int x = get_u32();
308       if (x)
309         {
310           total_subs = (total_subs - 1) + x;
311           match_u32_assert (0);
312         }
313       printf (" (total %d)", total_subs);
314
315       for (int i = 0; i < total_subs; i++)
316         {
317           putc ('\n', stdout);
318           dump_value (level + 1);
319         }
320     }
321   else
322     {
323
324       match_byte_assert (0x58);
325       char *base = get_string();
326       int x = get_u32();
327       printf ("\"%s\" with %d variables:\n", base, x);
328       if (match_u32(0))
329         {
330           for (int i = 0; i < x; i++)
331             {
332               dump_value (level+1);
333               putchar('\n');
334             }
335         }
336       else
337         {
338           for (int i = 0; i < x; i++)
339             {
340               int y = get_u32();
341               match_u32_assert(0);
342               for (int j = 0; j <= level; j++)
343                 printf ("    ");
344               printf("variable %d has %d values:\n", i, y);
345               for (int j = 0; j < y; j++)
346                 {
347                   if (match_byte(3))
348                     {
349                       char *a = get_string();
350                       match_byte_assert(0x58);
351                       char *b = get_string();
352                       char *c = get_string();
353                       for (int k = 0; k <= level + 1; k++)
354                         printf ("    ");
355                       printf ("\"%s\", \"%s\", \"%s\"", a, b, c);
356                       match_byte(0);
357                       match_byte(0);
358                       match_byte(0);
359                       match_byte(0);
360                       match_byte(0);
361                     }
362                   else
363                     dump_value (level+1);
364                   putchar('\n');
365                 }
366             }
367         }
368     }
369 }
370
371 static void
372 dump_dim_value(int level)
373 {
374   for (int i = 0; i <= level; i++)
375     printf ("    ");
376
377   if (match_byte (3))
378     {
379       get_string();
380       if (match_byte (0x31))
381         {
382           match_u32 (1);
383           printf("(footnote %d) ", get_u32());
384           match_byte_assert (0);
385           match_byte_assert (0);
386           int subn = get_u32 ();
387           printf ("nested %d bytes", subn);
388           pos += subn;
389         }
390       else
391         match_byte_assert (0x58);
392       get_string();
393       printf("string \"%s\"", get_string());
394       match_byte (0);
395       match_byte_assert (1);
396       match_byte (0);
397       match_byte (0);
398       match_byte (0);
399       match_byte (1);
400     }
401   else if (match_byte (5))
402     {
403       match_byte_assert (0x58);
404       printf ("variable \"%s\"", get_string());
405       get_string();
406       match_byte_assert (2);
407     }
408   else if (match_byte (2))
409     {
410       unsigned int format;
411       char *var, *vallab;
412       double value;
413
414       match_byte_assert (0x58);
415       format = get_u32 ();
416       value = get_double ();
417       var = get_string ();
418       vallab = get_string ();
419       printf ("value %g format %d(%d.%d) var \"%s\" vallab \"%s\"",
420               value, format >> 16, (format >> 8) & 0xff, format & 0xff, var, vallab);
421       if (!match_u32 (3))
422         match_u32_assert (2);
423       match_byte (0);
424     }
425   else if (match_byte (1))
426     {
427       unsigned int format;
428       double value;
429
430       match_byte_assert (0x58);
431       format = get_u32 ();
432       value = get_double ();
433       printf ("value %g format %d(%d.%d)", value, format >> 16, (format >> 8) & 0xff, format & 0xff);
434       match_byte (1);
435       match_byte (0);
436       match_byte (0);
437       match_byte (0);
438       match_byte (1);
439     }
440   else
441     {
442       int subn;
443
444       match_byte (0);
445       match_byte_assert (0x31);
446       match_u32_assert (0);
447       match_u32_assert (0);
448       subn = get_u32 ();
449       printf ("nested %d bytes", subn);
450       pos += subn;
451       printf ("; \"%s\", substitutions:", get_string());
452       int total_subs = get_u32();
453       int x = get_u32();
454       if (x)
455         {
456           total_subs = (total_subs - 1) + x;
457           match_u32_assert (0);
458         }
459       printf (" (total %d)", total_subs);
460
461       for (int i = 0; i < total_subs; i++)
462         {
463           putc ('\n', stdout);
464           dump_value (level + 1);
465         }
466     }
467 }
468
469 static void
470 dump_category(int level)
471 {
472   match_byte (0);
473   match_byte (0);
474   match_byte (0);
475   match_byte (0);
476   dump_value (level);
477
478   if (match_u32 (2))
479     get_u32 ();
480   else if (match_u32 (1))
481     {
482       match_byte (0);
483       match_byte (0);
484       match_byte (0);
485       get_u32 ();
486     }
487   else if (match_byte (1))
488     {
489       match_byte (0);
490       if (!match_u32 (2))
491         match_u32_assert (1);
492       match_byte (0);
493       get_u32();
494     }
495   else
496     {
497       match_u32_assert (0);
498       get_u32 ();
499     }
500
501   int n_categories = get_u32();
502   if (n_categories > 0)
503     printf (", %d subcategories:", n_categories);
504   printf("\n");
505   for (int i = 0; i < n_categories; i++)
506     dump_category (level + 1);
507 }
508
509 static void
510 dump_dim(void)
511 {
512   int n_categories;
513   printf("next dim\n");
514   match_byte(0);
515   if (match_byte(3))
516     {
517       get_string();
518       match_byte_assert(0x58);
519       get_string();
520       printf("string \"%s\": ", get_string());
521       match_byte(1) || match_byte(0);
522     }
523   else if (match_byte(5)) 
524     {
525       match_byte_assert(0x58);
526       printf("variable \"%s\": ", get_string());
527       get_string();
528       if (!match_byte(2))
529         match_byte_assert(3);
530     }
531   else if (match_byte(0x31))
532     {
533       int subn;
534       int total_subs = 1;
535
536       match_u32_assert (0);
537       match_u32_assert (0);
538       subn = get_u32 ();
539       printf ("nested %d bytes", subn);
540       pos += subn;
541       printf ("; \"%s\", substitutions:", get_string());
542       for (;;)
543         {
544           int n_subst = get_u32();
545           if (!n_subst)
546             break;
547           printf (" %d", n_subst);
548           total_subs *= n_subst;
549         }
550
551       for (int i = 0; i < total_subs; i++)
552         {
553           putc ('\n', stdout);
554           dump_dim_value (0);
555         }
556     }
557   else
558     {
559       int total_subs = 1;
560
561       match_byte_assert (0x58);
562       printf ("\"%s\" with substitutions:", get_string());
563       for (;;)
564         {
565           int n_subst = get_u32();
566           if (!n_subst)
567             break;
568           printf (" %d", n_subst);
569           total_subs *= n_subst;
570         }
571
572       for (int i = 0; i < total_subs; i++)
573         {
574           putc ('\n', stdout);
575           dump_dim_value (0);
576         }
577     }
578
579   /* This byte is usually 0x02 but 0x00 and 0x75 (!) have also been spotted. */
580   pos++;
581
582   if (!match_byte(0) && !match_byte(1))
583     match_byte_assert(2);
584   if (!match_u32(0))
585     match_u32_assert(2);
586   if (!match_byte(0))
587     match_byte_assert(1);
588   match_byte(0);
589   match_byte(0);
590   match_byte(0);
591   match_byte(0);
592   get_u32();
593   match_byte(0);
594   match_byte(0);
595   match_byte(0);
596   match_byte(0);
597   n_categories = get_u32();
598   printf("%d nested categories\n", n_categories);
599   for (int i = 0; i < n_categories; i++)
600     dump_category (0);
601 }
602
603 static void
604 dump_dims(void)
605 {
606   int n_dims = get_u32();
607
608   printf ("%u dimensions\n", n_dims);
609   for (int i = 0; i < n_dims; i++)
610     {
611       printf("\n");
612       dump_dim ();
613     }
614 }
615
616 int
617 main(int argc, char *argv[])
618 {
619   size_t start;
620   struct stat s;
621
622   if (isatty(STDIN_FILENO))
623     {
624       fprintf(stderr, "redirect stdin from a .bin file\n");
625       exit(1);
626     }
627   if (fstat(STDIN_FILENO, &s))
628     {
629       perror("fstat");
630       exit(1);
631     }
632   n = s.st_size;
633   data = malloc(n);
634   if (!data)
635     {
636       perror("malloc");
637       exit(1);
638     }
639   if (read(STDIN_FILENO, data, n) != n)
640     {
641       perror("read");
642       exit(1);
643     }
644
645   if (argc > 1)
646     {
647       if (!strcmp(argv[1], "title0"))
648         {
649           pos = 0x27;
650           if (match_byte (0x03)
651               || (match_byte (0x05) && match_byte (0x58)))
652             printf ("%s\n", get_string());
653           else
654             printf ("<unknown>\n");
655           return 0;
656         }
657       if (!strcmp(argv[1], "title"))
658         {
659           const char fonts[] = "\x01\x31\x09\0\0\0SansSerif";
660           start = 0x27;
661           n = find(fonts, sizeof fonts - 1);
662         }
663       else if (!strcmp(argv[1], "fonts"))
664         {
665           const char fonts[] = "\x01\x31\x09\0\0\0SansSerif";
666           const char styles[] = "\xf0\0\0\0";
667           start = find(fonts, sizeof fonts - 1);
668           n = find(styles, sizeof styles - 1);
669         }
670       else if (!strcmp(argv[1], "styles"))
671         {
672           const char styles[] = "\xf0\0\0\0";
673           const char dimensions[] = "-,,,.\0";
674           start = find(styles, sizeof styles - 1);
675           n = find(dimensions, sizeof dimensions - 1) + sizeof dimensions - 1;
676         }
677       else if (!strcmp(argv[1], "dimensions"))
678         {
679           {
680             const char dimensions[] = "-,,,.\0";
681             start = try_find_tail(dimensions, sizeof dimensions - 1);
682           }
683
684           if (!start)
685             {
686               const char dimensions[] = "-,,, .\0";
687               start = find_tail(dimensions, sizeof dimensions - 1);
688             }
689
690           pos = start;
691           dump_dims ();
692           return 0;
693         }
694       else
695         {
696           fprintf (stderr, "unknown section %s\n", argv[1]);
697           exit(1);
698         }
699     }
700   else
701     start = 0x27;
702
703   for (size_t i = start; i < n; )
704     {
705       if (i + 5 <= n
706           && data[i]
707           && !data[i + 1]
708           && !data[i + 2]
709           && !data[i + 3]
710           && i + 4 + data[i] <= n
711           && all_ascii(&data[i + 4], data[i]))
712         {
713           fputs("\n\"", stdout);
714           fwrite(&data[i + 4], 1, data[i], stdout);
715           fputs("\" ", stdout);
716
717           i += 4 + data[i];
718         }
719       else if (i + 12 <= n
720                && data[i + 1] == 40
721                && data[i + 2] == 5
722                && data[i + 3] == 0)
723         {
724           double d;
725
726           memcpy (&d, &data[i + 4], 8);
727           printf ("F40.%d(%.*f)\n", data[i], data[i], d);
728           i += 12;
729         }
730       else if (i + 12 <= n
731                && data[i + 1] == 40
732                && data[i + 2] == 31
733                && data[i + 3] == 0)
734         {
735           double d;
736
737           memcpy (&d, &data[i + 4], 8);
738           printf ("PCT40.%d(%.*f)\n", data[i], data[i], d);
739           i += 12;
740         }
741       else if (i + 4 <= n
742                && (data[i] && data[i] != 88 && data[i] != 0x41)
743                && !data[i + 1]
744                && !data[i + 2]
745                && !data[i + 3])
746         {
747           printf ("i%d ", data[i]);
748           i += 4;
749         }
750       else
751         {
752           printf("%02x ", data[i]);
753           i++;
754         }
755     }
756
757   return 0;
758 }