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