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