Get a few more tables' dimensions reading OK.
[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       double value;
156       char *var;
157
158       match_byte_assert (0x58);
159       format = get_u32 ();
160       value = get_double ();
161       var = get_string ();
162       get_string ();
163       printf ("value %g format %d(%d.%d) var \"%s\"", value, format >> 16, (format >> 8) & 0xff, format & 0xff, var);
164       match_u32_assert (3);
165     }
166   else
167     {
168       unsigned int format;
169       double value;
170
171       match_byte_assert (1);
172       match_byte_assert (0x58);
173       format = get_u32 ();
174       value = get_double ();
175       printf ("value %g format %d(%d.%d)", value, format >> 16, (format >> 8) & 0xff, format & 0xff);
176       match_byte (0);
177       match_byte (0);
178       match_byte (0);
179     }
180
181   if (match_u32 (2))
182     get_u32 ();
183   else
184     {
185       match_u32_assert (1);
186       match_byte (0);
187       match_byte (0);
188       match_byte (0);
189       get_u32 ();
190     }
191   int n_categories = get_u32();
192   if (n_categories > 0)
193     printf (", %d subcategories:", n_categories);
194   printf("\n");
195   for (int i = 0; i < n_categories; i++)
196     dump_category (level + 1);
197 }
198
199 static void
200 dump_dim(void)
201 {
202   int n_categories;
203   if (match_byte(3))
204     {
205       get_string();
206       match_byte_assert(0x58);
207       get_string();
208       printf("string \"%s\": ", get_string());
209       match_byte_assert(1);
210     }
211   else if (match_byte(5))
212     {
213       match_byte_assert(0x58);
214       printf("variable \"%s\": ", get_string());
215       get_string();
216       if (!match_byte(2))
217         match_byte_assert(3);
218     }
219   else
220     {
221       fprintf(stderr, "%08x: unexpected byte\n", pos);
222       exit(1);
223     }
224
225   match_byte_assert(0);
226   if (!match_byte(0) && !match_byte(1))
227     match_byte_assert(2);
228   match_u32_assert(2);
229   if (!match_byte(0))
230     match_byte_assert(1);
231   match_byte(0);
232   match_byte(0);
233   match_byte(0);
234   match_byte(0);
235   get_u32();
236   match_byte(0);
237   match_byte(0);
238   match_byte(0);
239   match_byte(0);
240   n_categories = get_u32();
241   printf("%d nested categories\n", n_categories);
242   for (int i = 0; i < n_categories; i++)
243     dump_category (0);
244 }
245
246 static void
247 dump_dims(void)
248 {
249   int n_dims = get_u32();
250
251   printf ("%u dimensions\n", n_dims);
252   for (int i = 0; i < n_dims; i++)
253     {
254       printf("\n");
255       dump_dim ();
256     }
257 }
258
259 int
260 main(int argc, char *argv[])
261 {
262   size_t start;
263   struct stat s;
264
265   if (isatty(STDIN_FILENO))
266     {
267       fprintf(stderr, "redirect stdin from a .bin file\n");
268       exit(1);
269     }
270   if (fstat(STDIN_FILENO, &s))
271     {
272       perror("fstat");
273       exit(1);
274     }
275   n = s.st_size;
276   data = malloc(n);
277   if (!data)
278     {
279       perror("malloc");
280       exit(1);
281     }
282   if (read(STDIN_FILENO, data, n) != n)
283     {
284       perror("read");
285       exit(1);
286     }
287
288   if (argc > 1)
289     {
290       if (!strcmp(argv[1], "title"))
291         {
292           const char fonts[] = "\x01\x31\x09\0\0\0SansSerif";
293           start = 0x27;
294           n = find(fonts, sizeof fonts - 1);
295         }
296       else if (!strcmp(argv[1], "fonts"))
297         {
298           const char fonts[] = "\x01\x31\x09\0\0\0SansSerif";
299           const char styles[] = "\xf0\0\0\0";
300           start = find(fonts, sizeof fonts - 1);
301           n = find(styles, sizeof styles - 1);
302         }
303       else if (!strcmp(argv[1], "styles"))
304         {
305           const char styles[] = "\xf0\0\0\0";
306           const char dimensions[] = "-,,,.\0";
307           start = find(styles, sizeof styles - 1);
308           n = find(dimensions, sizeof dimensions - 1) + sizeof dimensions - 1;
309         }
310       else if (!strcmp(argv[1], "dimensions"))
311         {
312           const char dimensions[] = "-,,,.\0";
313           start = find(dimensions, sizeof dimensions - 1) + sizeof dimensions - 1;
314           pos = start;
315           dump_dims ();
316           return 0;
317         }
318       else
319         {
320           fprintf (stderr, "unknown section %s\n", argv[1]);
321           exit(1);
322         }
323     }
324   else
325     start = 0x27;
326
327   for (size_t i = start; i < n; )
328     {
329       if (i + 5 <= n
330           && data[i]
331           && !data[i + 1]
332           && !data[i + 2]
333           && !data[i + 3]
334           && i + 4 + data[i] <= n
335           && all_ascii(&data[i + 4], data[i]))
336         {
337           fputs("\n\"", stdout);
338           fwrite(&data[i + 4], 1, data[i], stdout);
339           fputs("\" ", stdout);
340
341           i += 4 + data[i];
342         }
343       else if (i + 12 <= n
344                && data[i + 1] == 40
345                && data[i + 2] == 5
346                && data[i + 3] == 0)
347         {
348           double d;
349
350           memcpy (&d, &data[i + 4], 8);
351           printf ("F40.%d(%.*f)\n", data[i], data[i], d);
352           i += 12;
353         }
354       else if (i + 12 <= n
355                && data[i + 1] == 40
356                && data[i + 2] == 31
357                && data[i + 3] == 0)
358         {
359           double d;
360
361           memcpy (&d, &data[i + 4], 8);
362           printf ("PCT40.%d(%.*f)\n", data[i], data[i], d);
363           i += 12;
364         }
365       else if (i + 4 <= n
366                && (data[i] && data[i] != 88 && data[i] != 0x41)
367                && !data[i + 1]
368                && !data[i + 2]
369                && !data[i + 3])
370         {
371           printf ("i%d ", data[i]);
372           i += 4;
373         }
374       else
375         {
376           printf("%02x ", data[i]);
377           i++;
378         }
379     }
380
381   return 0;
382 }