24804ea13d3e9f31453d1f4aa58854d701b8d084
[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 if (match_byte(0x31))
490     {
491       int subn;
492       int total_subs = 1;
493
494       match_u32_assert (0);
495       match_u32_assert (0);
496       subn = get_u32 ();
497       printf ("nested %d bytes", subn);
498       pos += subn;
499       printf ("; \"%s\", substitutions:", get_string());
500       for (;;)
501         {
502           int n_subst = get_u32();
503           if (!n_subst)
504             break;
505           printf (" %d", n_subst);
506           total_subs *= n_subst;
507         }
508
509       for (int i = 0; i < total_subs; i++)
510         {
511           putc ('\n', stdout);
512           dump_dim_value (0);
513         }
514     }
515   else
516     {
517       int total_subs = 1;
518
519       match_byte_assert (0x58);
520       printf ("\"%s\" with substitutions:", get_string());
521       for (;;)
522         {
523           int n_subst = get_u32();
524           if (!n_subst)
525             break;
526           printf (" %d", n_subst);
527           total_subs *= n_subst;
528         }
529
530       for (int i = 0; i < total_subs; i++)
531         {
532           putc ('\n', stdout);
533           dump_dim_value (0);
534         }
535     }
536
537   match_byte_assert(0);
538   if (!match_byte(0) && !match_byte(1))
539     match_byte_assert(2);
540   match_u32_assert(2);
541   if (!match_byte(0))
542     match_byte_assert(1);
543   match_byte(0);
544   match_byte(0);
545   match_byte(0);
546   match_byte(0);
547   get_u32();
548   match_byte(0);
549   match_byte(0);
550   match_byte(0);
551   match_byte(0);
552   n_categories = get_u32();
553   printf("%d nested categories\n", n_categories);
554   for (int i = 0; i < n_categories; i++)
555     dump_category (0);
556 }
557
558 static void
559 dump_dims(void)
560 {
561   int n_dims = get_u32();
562
563   printf ("%u dimensions\n", n_dims);
564   for (int i = 0; i < n_dims; i++)
565     {
566       printf("\n");
567       dump_dim ();
568     }
569 }
570
571 int
572 main(int argc, char *argv[])
573 {
574   size_t start;
575   struct stat s;
576
577   if (isatty(STDIN_FILENO))
578     {
579       fprintf(stderr, "redirect stdin from a .bin file\n");
580       exit(1);
581     }
582   if (fstat(STDIN_FILENO, &s))
583     {
584       perror("fstat");
585       exit(1);
586     }
587   n = s.st_size;
588   data = malloc(n);
589   if (!data)
590     {
591       perror("malloc");
592       exit(1);
593     }
594   if (read(STDIN_FILENO, data, n) != n)
595     {
596       perror("read");
597       exit(1);
598     }
599
600   if (argc > 1)
601     {
602       if (!strcmp(argv[1], "title0"))
603         {
604           pos = 0x27;
605           if (match_byte (0x03)
606               || (match_byte (0x05) && match_byte (0x58)))
607             printf ("%s\n", get_string());
608           else
609             printf ("<unknown>\n");
610           return 0;
611         }
612       if (!strcmp(argv[1], "title"))
613         {
614           const char fonts[] = "\x01\x31\x09\0\0\0SansSerif";
615           start = 0x27;
616           n = find(fonts, sizeof fonts - 1);
617         }
618       else if (!strcmp(argv[1], "fonts"))
619         {
620           const char fonts[] = "\x01\x31\x09\0\0\0SansSerif";
621           const char styles[] = "\xf0\0\0\0";
622           start = find(fonts, sizeof fonts - 1);
623           n = find(styles, sizeof styles - 1);
624         }
625       else if (!strcmp(argv[1], "styles"))
626         {
627           const char styles[] = "\xf0\0\0\0";
628           const char dimensions[] = "-,,,.\0";
629           start = find(styles, sizeof styles - 1);
630           n = find(dimensions, sizeof dimensions - 1) + sizeof dimensions - 1;
631         }
632       else if (!strcmp(argv[1], "dimensions"))
633         {
634           {
635             const char dimensions[] = "-,,,.\0";
636             start = try_find_tail(dimensions, sizeof dimensions - 1);
637           }
638
639           if (!start)
640             {
641               const char dimensions[] = "-,,, .\0";
642               start = find_tail(dimensions, sizeof dimensions - 1);
643             }
644
645           pos = start;
646           dump_dims ();
647           return 0;
648         }
649       else
650         {
651           fprintf (stderr, "unknown section %s\n", argv[1]);
652           exit(1);
653         }
654     }
655   else
656     start = 0x27;
657
658   for (size_t i = start; i < n; )
659     {
660       if (i + 5 <= n
661           && data[i]
662           && !data[i + 1]
663           && !data[i + 2]
664           && !data[i + 3]
665           && i + 4 + data[i] <= n
666           && all_ascii(&data[i + 4], data[i]))
667         {
668           fputs("\n\"", stdout);
669           fwrite(&data[i + 4], 1, data[i], stdout);
670           fputs("\" ", stdout);
671
672           i += 4 + data[i];
673         }
674       else if (i + 12 <= n
675                && data[i + 1] == 40
676                && data[i + 2] == 5
677                && data[i + 3] == 0)
678         {
679           double d;
680
681           memcpy (&d, &data[i + 4], 8);
682           printf ("F40.%d(%.*f)\n", data[i], data[i], d);
683           i += 12;
684         }
685       else if (i + 12 <= n
686                && data[i + 1] == 40
687                && data[i + 2] == 31
688                && data[i + 3] == 0)
689         {
690           double d;
691
692           memcpy (&d, &data[i + 4], 8);
693           printf ("PCT40.%d(%.*f)\n", data[i], data[i], d);
694           i += 12;
695         }
696       else if (i + 4 <= n
697                && (data[i] && data[i] != 88 && data[i] != 0x41)
698                && !data[i + 1]
699                && !data[i + 2]
700                && !data[i + 3])
701         {
702           printf ("i%d ", data[i]);
703           i += 4;
704         }
705       else
706         {
707           printf("%02x ", data[i]);
708           i++;
709         }
710     }
711
712   return 0;
713 }