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