d21e7af0fb5044b8f58bcd547f438462516e1cea
[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_u32 (3))
241         match_u32_assert (2);
242       match_byte (0);
243       match_byte (0);
244       match_byte (0);
245       match_byte (0);
246     }
247   else if (match_byte (4))
248     {
249       unsigned int format;
250       char *var, *vallab, *value;
251
252       match_byte_assert (0x58);
253       format = get_u32 ();
254       vallab = get_string ();
255       var = get_string ();
256       match_byte_assert (2);
257       value = get_string ();
258       printf ("value \"%s\" format %d(%d.%d) var \"%s\" vallab \"%s\"",
259               value, format >> 16, (format >> 8) & 0xff, format & 0xff, var, vallab);
260       match_byte (0);
261       match_byte (0);
262       match_byte (0);
263       match_byte (0);
264     }
265   else if (match_byte (1))
266     {
267       unsigned int format;
268       double value;
269
270       match_byte_assert (0x58);
271       format = get_u32 ();
272       value = get_double ();
273       printf ("value %g format %d(%d.%d)", value, format >> 16, (format >> 8) & 0xff, format & 0xff);
274       match_byte (1);
275       match_byte (0);
276       match_byte (0);
277       match_byte (0);
278       match_byte (1);
279     }
280   else if (match_byte (0x31))
281     {
282       int subn;
283       int total_subs = 1;
284
285       match_u32_assert (0);
286       match_u32_assert (0);
287       subn = get_u32 ();
288       printf ("nested %d bytes", subn);
289       pos += subn;
290       printf ("; \"%s\", substitutions:", get_string());
291       for (;;)
292         {
293           int n_subst = get_u32();
294           if (!n_subst)
295             break;
296           printf (" %d", n_subst);
297           total_subs *= n_subst;
298         }
299
300       for (int i = 0; i < total_subs; i++)
301         {
302           putc ('\n', stdout);
303           dump_value (level + 1);
304         }
305     }
306   else
307     {
308       int total_subs = 1;
309
310       match_byte_assert (0x58);
311       printf ("\"%s\" with substitutions:", get_string());
312       for (;;)
313         {
314           int n_subst = get_u32();
315           if (!n_subst)
316             break;
317           printf (" %d", n_subst);
318           total_subs *= n_subst;
319         }
320
321       for (int i = 0; i < total_subs; i++)
322         {
323           putc ('\n', stdout);
324           dump_value (level + 1);
325         }
326     }
327 }
328
329 static void
330 dump_dim_value(int level)
331 {
332   for (int i = 0; i <= level; i++)
333     printf ("    ");
334
335   if (match_byte (3))
336     {
337       get_string();
338       if (match_byte (0x31))
339         {
340           match_u32 (1);
341           printf("(footnote %d) ", get_u32());
342           match_byte_assert (0);
343           match_byte_assert (0);
344           int subn = get_u32 ();
345           printf ("nested %d bytes", subn);
346           pos += subn;
347         }
348       else
349         match_byte_assert (0x58);
350       get_string();
351       printf("string \"%s\"", get_string());
352       match_byte (0);
353       match_byte_assert (1);
354       match_byte (0);
355       match_byte (0);
356       match_byte (0);
357       match_byte (1);
358     }
359   else if (match_byte (5))
360     {
361       match_byte_assert (0x58);
362       printf ("variable \"%s\"", get_string());
363       get_string();
364       match_byte_assert (2);
365     }
366   else if (match_byte (2))
367     {
368       unsigned int format;
369       char *var, *vallab;
370       double value;
371
372       match_byte_assert (0x58);
373       format = get_u32 ();
374       value = get_double ();
375       var = get_string ();
376       vallab = get_string ();
377       printf ("value %g format %d(%d.%d) var \"%s\" vallab \"%s\"",
378               value, format >> 16, (format >> 8) & 0xff, format & 0xff, var, vallab);
379       if (!match_u32 (3))
380         match_u32_assert (2);
381       match_byte (0);
382     }
383   else if (match_byte (1))
384     {
385       unsigned int format;
386       double value;
387
388       match_byte_assert (0x58);
389       format = get_u32 ();
390       value = get_double ();
391       printf ("value %g format %d(%d.%d)", value, format >> 16, (format >> 8) & 0xff, format & 0xff);
392       match_byte (1);
393       match_byte (0);
394       match_byte (0);
395       match_byte (0);
396       match_byte (1);
397     }
398   else
399     {
400       int subn;
401       int total_subs = 1;
402
403       match_byte (0);
404       match_byte_assert (0x31);
405       match_u32_assert (0);
406       match_u32_assert (0);
407       subn = get_u32 ();
408       printf ("nested %d bytes", subn);
409       pos += subn;
410       printf ("; \"%s\", substitutions:", get_string());
411       for (;;)
412         {
413           int n_subst = get_u32();
414           if (!n_subst)
415             break;
416           printf (" %d", n_subst);
417           total_subs *= n_subst;
418         }
419
420       for (int i = 0; i < total_subs; i++)
421         {
422           putc ('\n', stdout);
423           dump_value (level + 1);
424         }
425     }
426 }
427
428 static void
429 dump_category(int level)
430 {
431   match_byte (0);
432   match_byte (0);
433   match_byte (0);
434   match_byte (0);
435   dump_value (level);
436
437   if (match_u32 (2))
438     get_u32 ();
439   else if (match_u32 (1))
440     {
441       match_byte (0);
442       match_byte (0);
443       match_byte (0);
444       get_u32 ();
445     }
446   else if (match_byte (1))
447     {
448       match_byte (0);
449       match_u32_assert (1);
450       match_byte (0);
451       get_u32();
452     }
453   else
454     {
455       match_u32_assert (0);
456       get_u32 ();
457     }
458
459   int n_categories = get_u32();
460   if (n_categories > 0)
461     printf (", %d subcategories:", n_categories);
462   printf("\n");
463   for (int i = 0; i < n_categories; i++)
464     dump_category (level + 1);
465 }
466
467 static void
468 dump_dim(void)
469 {
470   int n_categories;
471   printf("next dim\n");
472   match_byte(0);
473   if (match_byte(3))
474     {
475       get_string();
476       match_byte_assert(0x58);
477       get_string();
478       printf("string \"%s\": ", get_string());
479       match_byte(1) || match_byte(0);
480     }
481   else if (match_byte(5)) 
482     {
483       match_byte_assert(0x58);
484       printf("variable \"%s\": ", get_string());
485       get_string();
486       if (!match_byte(2))
487         match_byte_assert(3);
488     }
489   else
490     {
491       int subn;
492       int total_subs = 1;
493
494       match_byte_assert(0x31);
495       match_u32_assert (0);
496       match_u32_assert (0);
497       subn = get_u32 ();
498       printf ("nested %d bytes", subn);
499       pos += subn;
500       printf ("; \"%s\", substitutions:", get_string());
501       for (;;)
502         {
503           int n_subst = get_u32();
504           if (!n_subst)
505             break;
506           printf (" %d", n_subst);
507           total_subs *= n_subst;
508         }
509
510       for (int i = 0; i < total_subs; i++)
511         {
512           putc ('\n', stdout);
513           dump_dim_value (0);
514         }
515     }
516
517   match_byte_assert(0);
518   if (!match_byte(0) && !match_byte(1))
519     match_byte_assert(2);
520   match_u32_assert(2);
521   if (!match_byte(0))
522     match_byte_assert(1);
523   match_byte(0);
524   match_byte(0);
525   match_byte(0);
526   match_byte(0);
527   get_u32();
528   match_byte(0);
529   match_byte(0);
530   match_byte(0);
531   match_byte(0);
532   n_categories = get_u32();
533   printf("%d nested categories\n", n_categories);
534   for (int i = 0; i < n_categories; i++)
535     dump_category (0);
536 }
537
538 static void
539 dump_dims(void)
540 {
541   int n_dims = get_u32();
542
543   printf ("%u dimensions\n", n_dims);
544   for (int i = 0; i < n_dims; i++)
545     {
546       printf("\n");
547       dump_dim ();
548     }
549 }
550
551 int
552 main(int argc, char *argv[])
553 {
554   size_t start;
555   struct stat s;
556
557   if (isatty(STDIN_FILENO))
558     {
559       fprintf(stderr, "redirect stdin from a .bin file\n");
560       exit(1);
561     }
562   if (fstat(STDIN_FILENO, &s))
563     {
564       perror("fstat");
565       exit(1);
566     }
567   n = s.st_size;
568   data = malloc(n);
569   if (!data)
570     {
571       perror("malloc");
572       exit(1);
573     }
574   if (read(STDIN_FILENO, data, n) != n)
575     {
576       perror("read");
577       exit(1);
578     }
579
580   if (argc > 1)
581     {
582       if (!strcmp(argv[1], "title0"))
583         {
584           pos = 0x27;
585           if (match_byte (0x03)
586               || (match_byte (0x05) && match_byte (0x58)))
587             printf ("%s\n", get_string());
588           else
589             printf ("<unknown>\n");
590           return 0;
591         }
592       if (!strcmp(argv[1], "title"))
593         {
594           const char fonts[] = "\x01\x31\x09\0\0\0SansSerif";
595           start = 0x27;
596           n = find(fonts, sizeof fonts - 1);
597         }
598       else if (!strcmp(argv[1], "fonts"))
599         {
600           const char fonts[] = "\x01\x31\x09\0\0\0SansSerif";
601           const char styles[] = "\xf0\0\0\0";
602           start = find(fonts, sizeof fonts - 1);
603           n = find(styles, sizeof styles - 1);
604         }
605       else if (!strcmp(argv[1], "styles"))
606         {
607           const char styles[] = "\xf0\0\0\0";
608           const char dimensions[] = "-,,,.\0";
609           start = find(styles, sizeof styles - 1);
610           n = find(dimensions, sizeof dimensions - 1) + sizeof dimensions - 1;
611         }
612       else if (!strcmp(argv[1], "dimensions"))
613         {
614           {
615             const char dimensions[] = "-,,,.\0";
616             start = try_find_tail(dimensions, sizeof dimensions - 1);
617           }
618
619           if (!start)
620             {
621               const char dimensions[] = "-,,, .\0";
622               start = find_tail(dimensions, sizeof dimensions - 1);
623             }
624
625           pos = start;
626           dump_dims ();
627           return 0;
628         }
629       else
630         {
631           fprintf (stderr, "unknown section %s\n", argv[1]);
632           exit(1);
633         }
634     }
635   else
636     start = 0x27;
637
638   for (size_t i = start; i < n; )
639     {
640       if (i + 5 <= n
641           && data[i]
642           && !data[i + 1]
643           && !data[i + 2]
644           && !data[i + 3]
645           && i + 4 + data[i] <= n
646           && all_ascii(&data[i + 4], data[i]))
647         {
648           fputs("\n\"", stdout);
649           fwrite(&data[i + 4], 1, data[i], stdout);
650           fputs("\" ", stdout);
651
652           i += 4 + data[i];
653         }
654       else if (i + 12 <= n
655                && data[i + 1] == 40
656                && data[i + 2] == 5
657                && data[i + 3] == 0)
658         {
659           double d;
660
661           memcpy (&d, &data[i + 4], 8);
662           printf ("F40.%d(%.*f)\n", data[i], data[i], d);
663           i += 12;
664         }
665       else if (i + 12 <= n
666                && data[i + 1] == 40
667                && data[i + 2] == 31
668                && data[i + 3] == 0)
669         {
670           double d;
671
672           memcpy (&d, &data[i + 4], 8);
673           printf ("PCT40.%d(%.*f)\n", data[i], data[i], d);
674           i += 12;
675         }
676       else if (i + 4 <= n
677                && (data[i] && data[i] != 88 && data[i] != 0x41)
678                && !data[i + 1]
679                && !data[i + 2]
680                && !data[i + 3])
681         {
682           printf ("i%d ", data[i]);
683           i += 4;
684         }
685       else
686         {
687           printf("%02x ", data[i]);
688           i++;
689         }
690     }
691
692   return 0;
693 }