dab02a412079dd5487ba0fa772556f8bedb41c71
[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 find(const char *target, size_t target_len)
23 {
24   const uint8_t *pos = (const uint8_t *) memmem (data, n, target, target_len);
25   if (!pos)
26     {
27       fprintf (stderr, "not found\n");
28       exit(1);
29     }
30   return pos - data;
31 }
32
33 size_t pos;
34
35 #define XSTR(x) #x
36 #define STR(x) XSTR(x)
37 #define WHERE __FILE__":" STR(__LINE__)
38
39 static unsigned int
40 get_u32(void)
41 {
42   uint32_t x;
43   memcpy(&x, &data[pos], 4);
44   pos += 4;
45   return x;
46 }
47
48 static double
49 get_double(void)
50 {
51   double x;
52   memcpy(&x, &data[pos], 8);
53   pos += 8;
54   return x;
55 }
56
57 static bool
58 match_u32(uint32_t x)
59 {
60   if (get_u32() == x)
61     return true;
62   pos -= 4;
63   return false;
64 }
65
66 static void
67 match_u32_assert(uint32_t x, const char *where)
68 {
69   unsigned int y = get_u32();
70   if (x != y)
71     {
72       fprintf(stderr, "%s: 0x%x: expected i%u, got i%u\n", where, pos - 4, x, y);
73       exit(1);
74     }
75 }
76 #define match_u32_assert(x) match_u32_assert(x, WHERE)
77
78 static bool
79 match_byte(uint8_t b)
80 {
81   if (data[pos] == b)
82     {
83       pos++;
84       return true;
85     }
86   else
87     return false;
88 }
89
90 static void
91 match_byte_assert(uint8_t b, const char *where)
92 {
93   if (!match_byte(b))
94     {
95       fprintf(stderr, "%s: 0x%x: expected %02x, got %02x\n", where, pos, b, data[pos]);
96       exit(1);
97     }
98 }
99 #define match_byte_assert(b) match_byte_assert(b, WHERE)
100
101 static char *
102 get_string(void)
103 {
104   if (data[pos + 1] == 0 && data[pos + 2] == 0 && data[pos + 3] == 0
105       && all_ascii(&data[pos + 4], data[pos]))
106     {
107       int len = data[pos];
108       char *s = malloc(len + 1);
109
110       memcpy(s, &data[pos + 4], len);
111       s[len] = 0;
112       pos += 4 + data[pos];
113       return s;
114     }
115   else
116     {
117       fprintf(stderr, "0x%x: expected string\n", pos);
118       exit(1);
119     }
120 }
121
122 static void
123 dump_category(int level)
124 {
125   for (int i = 0; i <= level; i++)
126     printf ("    ");
127
128   match_byte (0);
129   if (match_byte (3))
130     {
131       get_string();
132       match_byte_assert (0x58);
133       get_string();
134       printf("string \"%s\"", get_string());
135       match_byte_assert (1);
136       match_byte (0);
137       match_byte (0);
138       match_byte (0);
139       match_byte (1);
140     }
141   else if (match_byte (5))
142     {
143       match_byte_assert (0x58);
144       printf ("variable \"%s\"", get_string());
145       get_string();
146       if (!match_byte (3))
147         match_byte_assert (2);
148       match_byte (0);
149       match_byte (0);
150       match_byte (0);
151     }
152   else if (match_byte (2))
153     {
154       unsigned int format;
155       char *var, *vallab;
156       double value;
157
158       match_byte_assert (0x58);
159       format = get_u32 ();
160       value = get_double ();
161       var = get_string ();
162       vallab = get_string ();
163       printf ("value %g format %d(%d.%d) var \"%s\" vallab \"%s\"",
164               value, format >> 16, (format >> 8) & 0xff, format & 0xff, var, vallab);
165       if (!match_u32 (3))
166         match_u32_assert (2);
167     }
168   else
169     {
170       unsigned int format;
171       double value;
172
173       match_byte_assert (1);
174       match_byte_assert (0x58);
175       format = get_u32 ();
176       value = get_double ();
177       printf ("value %g format %d(%d.%d)", value, format >> 16, (format >> 8) & 0xff, format & 0xff);
178       match_byte (1);
179       match_byte (0);
180       match_byte (0);
181       match_byte (0);
182       match_byte (1);
183     }
184
185   if (match_u32 (2))
186     get_u32 ();
187   else if (match_u32 (1))
188     {
189       match_byte (0);
190       match_byte (0);
191       match_byte (0);
192       get_u32 ();
193     }
194   else
195     {
196       match_u32_assert (0);
197       get_u32 ();
198     }
199   int n_categories = get_u32();
200   if (n_categories > 0)
201     printf (", %d subcategories:", n_categories);
202   printf("\n");
203   for (int i = 0; i < n_categories; i++)
204     dump_category (level + 1);
205 }
206
207 static void
208 dump_dim(void)
209 {
210   int n_categories;
211   if (match_byte(3))
212     {
213       get_string();
214       match_byte_assert(0x58);
215       get_string();
216       printf("string \"%s\": ", get_string());
217       match_byte_assert(1);
218     }
219   else if (match_byte(5))
220     {
221       match_byte_assert(0x58);
222       printf("variable \"%s\": ", get_string());
223       get_string();
224       if (!match_byte(2))
225         match_byte_assert(3);
226     }
227   else
228     {
229       fprintf(stderr, "%08x: unexpected byte\n", pos);
230       exit(1);
231     }
232
233   match_byte_assert(0);
234   if (!match_byte(0) && !match_byte(1))
235     match_byte_assert(2);
236   match_u32_assert(2);
237   if (!match_byte(0))
238     match_byte_assert(1);
239   match_byte(0);
240   match_byte(0);
241   match_byte(0);
242   match_byte(0);
243   get_u32();
244   match_byte(0);
245   match_byte(0);
246   match_byte(0);
247   match_byte(0);
248   n_categories = get_u32();
249   printf("%d nested categories\n", n_categories);
250   for (int i = 0; i < n_categories; i++)
251     dump_category (0);
252 }
253
254 static void
255 dump_dims(void)
256 {
257   int n_dims = get_u32();
258
259   printf ("%u dimensions\n", n_dims);
260   for (int i = 0; i < n_dims; i++)
261     {
262       printf("\n");
263       dump_dim ();
264     }
265 }
266
267 int
268 main(int argc, char *argv[])
269 {
270   size_t start;
271   struct stat s;
272
273   if (isatty(STDIN_FILENO))
274     {
275       fprintf(stderr, "redirect stdin from a .bin file\n");
276       exit(1);
277     }
278   if (fstat(STDIN_FILENO, &s))
279     {
280       perror("fstat");
281       exit(1);
282     }
283   n = s.st_size;
284   data = malloc(n);
285   if (!data)
286     {
287       perror("malloc");
288       exit(1);
289     }
290   if (read(STDIN_FILENO, data, n) != n)
291     {
292       perror("read");
293       exit(1);
294     }
295
296   if (argc > 1)
297     {
298       if (!strcmp(argv[1], "title"))
299         {
300           const char fonts[] = "\x01\x31\x09\0\0\0SansSerif";
301           start = 0x27;
302           n = find(fonts, sizeof fonts - 1);
303         }
304       else if (!strcmp(argv[1], "fonts"))
305         {
306           const char fonts[] = "\x01\x31\x09\0\0\0SansSerif";
307           const char styles[] = "\xf0\0\0\0";
308           start = find(fonts, sizeof fonts - 1);
309           n = find(styles, sizeof styles - 1);
310         }
311       else if (!strcmp(argv[1], "styles"))
312         {
313           const char styles[] = "\xf0\0\0\0";
314           const char dimensions[] = "-,,,.\0";
315           start = find(styles, sizeof styles - 1);
316           n = find(dimensions, sizeof dimensions - 1) + sizeof dimensions - 1;
317         }
318       else if (!strcmp(argv[1], "dimensions"))
319         {
320           const char dimensions[] = "-,,,.\0";
321           start = find(dimensions, sizeof dimensions - 1) + sizeof dimensions - 1;
322           pos = start;
323           dump_dims ();
324           return 0;
325         }
326       else
327         {
328           fprintf (stderr, "unknown section %s\n", argv[1]);
329           exit(1);
330         }
331     }
332   else
333     start = 0x27;
334
335   for (size_t i = start; i < n; )
336     {
337       if (i + 5 <= n
338           && data[i]
339           && !data[i + 1]
340           && !data[i + 2]
341           && !data[i + 3]
342           && i + 4 + data[i] <= n
343           && all_ascii(&data[i + 4], data[i]))
344         {
345           fputs("\n\"", stdout);
346           fwrite(&data[i + 4], 1, data[i], stdout);
347           fputs("\" ", stdout);
348
349           i += 4 + data[i];
350         }
351       else if (i + 12 <= n
352                && data[i + 1] == 40
353                && data[i + 2] == 5
354                && data[i + 3] == 0)
355         {
356           double d;
357
358           memcpy (&d, &data[i + 4], 8);
359           printf ("F40.%d(%.*f)\n", data[i], data[i], d);
360           i += 12;
361         }
362       else if (i + 12 <= n
363                && data[i + 1] == 40
364                && data[i + 2] == 31
365                && data[i + 3] == 0)
366         {
367           double d;
368
369           memcpy (&d, &data[i + 4], 8);
370           printf ("PCT40.%d(%.*f)\n", data[i], data[i], d);
371           i += 12;
372         }
373       else if (i + 4 <= n
374                && (data[i] && data[i] != 88 && data[i] != 0x41)
375                && !data[i + 1]
376                && !data[i + 2]
377                && !data[i + 3])
378         {
379           printf ("i%d ", data[i]);
380           i += 4;
381         }
382       else
383         {
384           printf("%02x ", data[i]);
385           i++;
386         }
387     }
388
389   return 0;
390 }