dump: Support common alternate custom currency types.
[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(void)
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, "0x%x: expected string\n", pos);
144       exit(1);
145     }
146 }
147
148 static void
149 dump_value(int level)
150 {
151   for (int i = 0; i <= level; i++)
152     printf ("    ");
153
154   if (match_byte (3))
155     {
156       get_string();
157       if (match_byte (0x31))
158         {
159           match_u32 (1);
160           printf("(footnote %d) ", get_u32());
161           match_byte_assert (0);
162           match_byte_assert (0);
163           int subn = get_u32 ();
164           printf ("nested %d bytes", subn);
165           pos += subn;
166         }
167       else
168         match_byte_assert (0x58);
169       get_string();
170       printf("string \"%s\"", get_string());
171       match_byte (0);
172       match_byte_assert (1);
173       match_byte (0);
174       match_byte (0);
175       match_byte (0);
176       match_byte (1);
177     }
178   else if (match_byte (5))
179     {
180       match_byte_assert (0x58);
181       printf ("variable \"%s\"", get_string());
182       get_string();
183       if (!match_byte (3))
184         match_byte_assert (2);
185       match_byte (0);
186       match_byte (0);
187       match_byte (0);
188       match_byte (0);
189     }
190   else if (match_byte (2))
191     {
192       unsigned int format;
193       char *var, *vallab;
194       double value;
195
196       match_byte_assert (0x58);
197       format = get_u32 ();
198       value = get_double ();
199       var = get_string ();
200       vallab = get_string ();
201       printf ("value %g format %d(%d.%d) var \"%s\" vallab \"%s\"",
202               value, format >> 16, (format >> 8) & 0xff, format & 0xff, var, vallab);
203       if (!match_u32 (3))
204         match_u32_assert (2);
205       match_byte (0);
206     }
207   else if (match_byte (1))
208     {
209       unsigned int format;
210       double value;
211
212       match_byte_assert (0x58);
213       format = get_u32 ();
214       value = get_double ();
215       printf ("value %g format %d(%d.%d)", value, format >> 16, (format >> 8) & 0xff, format & 0xff);
216       match_byte (1);
217       match_byte (0);
218       match_byte (0);
219       match_byte (0);
220       match_byte (1);
221     }
222   else
223     {
224       int subn;
225       int total_subs = 1;
226
227       match_byte (0);
228       match_byte_assert (0x31);
229       match_u32_assert (0);
230       match_u32_assert (0);
231       subn = get_u32 ();
232       printf ("nested %d bytes", subn);
233       pos += subn;
234       printf ("; \"%s\", substitutions:", get_string());
235       for (;;)
236         {
237           int n_subst = get_u32();
238           if (!n_subst)
239             break;
240           printf (" %d", n_subst);
241           total_subs *= n_subst;
242         }
243
244       for (int i = 0; i < total_subs; i++)
245         {
246           putc ('\n', stdout);
247           dump_value (level + 1);
248         }
249     }
250 }
251
252 static void
253 dump_category(int level)
254 {
255   match_byte (0);
256   match_byte (0);
257   match_byte (0);
258   match_byte (0);
259   dump_value (level);
260
261   if (match_u32 (2))
262     get_u32 ();
263   else if (match_u32 (1))
264     {
265       match_byte (0);
266       match_byte (0);
267       match_byte (0);
268       get_u32 ();
269     }
270   else
271     {
272       match_u32_assert (0);
273       get_u32 ();
274     }
275
276   int n_categories = get_u32();
277   if (n_categories > 0)
278     printf (", %d subcategories:", n_categories);
279   printf("\n");
280   for (int i = 0; i < n_categories; i++)
281     dump_category (level + 1);
282 }
283
284 static void
285 dump_dim(void)
286 {
287   int n_categories;
288   if (match_byte(3))
289     {
290       get_string();
291       match_byte_assert(0x58);
292       get_string();
293       printf("string \"%s\": ", get_string());
294       match_byte_assert(1);
295     }
296   else if (match_byte(5))
297     {
298       match_byte_assert(0x58);
299       printf("variable \"%s\": ", get_string());
300       get_string();
301       if (!match_byte(2))
302         match_byte_assert(3);
303     }
304   else
305     {
306       fprintf(stderr, "%08x: unexpected byte\n", pos);
307       exit(1);
308     }
309
310   match_byte_assert(0);
311   if (!match_byte(0) && !match_byte(1))
312     match_byte_assert(2);
313   match_u32_assert(2);
314   if (!match_byte(0))
315     match_byte_assert(1);
316   match_byte(0);
317   match_byte(0);
318   match_byte(0);
319   match_byte(0);
320   get_u32();
321   match_byte(0);
322   match_byte(0);
323   match_byte(0);
324   match_byte(0);
325   n_categories = get_u32();
326   printf("%d nested categories\n", n_categories);
327   for (int i = 0; i < n_categories; i++)
328     dump_category (0);
329 }
330
331 static void
332 dump_dims(void)
333 {
334   int n_dims = get_u32();
335
336   printf ("%u dimensions\n", n_dims);
337   for (int i = 0; i < n_dims; i++)
338     {
339       printf("\n");
340       dump_dim ();
341     }
342 }
343
344 int
345 main(int argc, char *argv[])
346 {
347   size_t start;
348   struct stat s;
349
350   if (isatty(STDIN_FILENO))
351     {
352       fprintf(stderr, "redirect stdin from a .bin file\n");
353       exit(1);
354     }
355   if (fstat(STDIN_FILENO, &s))
356     {
357       perror("fstat");
358       exit(1);
359     }
360   n = s.st_size;
361   data = malloc(n);
362   if (!data)
363     {
364       perror("malloc");
365       exit(1);
366     }
367   if (read(STDIN_FILENO, data, n) != n)
368     {
369       perror("read");
370       exit(1);
371     }
372
373   if (argc > 1)
374     {
375       if (!strcmp(argv[1], "title0"))
376         {
377           pos = 0x27;
378           if (match_byte (0x03)
379               || (match_byte (0x05) && match_byte (0x58)))
380             printf ("%s\n", get_string());
381           else
382             printf ("<unknown>\n");
383           return 0;
384         }
385       if (!strcmp(argv[1], "title"))
386         {
387           const char fonts[] = "\x01\x31\x09\0\0\0SansSerif";
388           start = 0x27;
389           n = find(fonts, sizeof fonts - 1);
390         }
391       else if (!strcmp(argv[1], "fonts"))
392         {
393           const char fonts[] = "\x01\x31\x09\0\0\0SansSerif";
394           const char styles[] = "\xf0\0\0\0";
395           start = find(fonts, sizeof fonts - 1);
396           n = find(styles, sizeof styles - 1);
397         }
398       else if (!strcmp(argv[1], "styles"))
399         {
400           const char styles[] = "\xf0\0\0\0";
401           const char dimensions[] = "-,,,.\0";
402           start = find(styles, sizeof styles - 1);
403           n = find(dimensions, sizeof dimensions - 1) + sizeof dimensions - 1;
404         }
405       else if (!strcmp(argv[1], "dimensions"))
406         {
407           {
408             const char dimensions[] = "-,,,.\0";
409             start = try_find_tail(dimensions, sizeof dimensions - 1);
410           }
411
412           if (!start)
413             {
414               const char dimensions[] = "-,,, .\0";
415               start = find_tail(dimensions, sizeof dimensions - 1);
416             }
417
418           pos = start;
419           dump_dims ();
420           return 0;
421         }
422       else
423         {
424           fprintf (stderr, "unknown section %s\n", argv[1]);
425           exit(1);
426         }
427     }
428   else
429     start = 0x27;
430
431   for (size_t i = start; i < n; )
432     {
433       if (i + 5 <= n
434           && data[i]
435           && !data[i + 1]
436           && !data[i + 2]
437           && !data[i + 3]
438           && i + 4 + data[i] <= n
439           && all_ascii(&data[i + 4], data[i]))
440         {
441           fputs("\n\"", stdout);
442           fwrite(&data[i + 4], 1, data[i], stdout);
443           fputs("\" ", stdout);
444
445           i += 4 + data[i];
446         }
447       else if (i + 12 <= n
448                && data[i + 1] == 40
449                && data[i + 2] == 5
450                && data[i + 3] == 0)
451         {
452           double d;
453
454           memcpy (&d, &data[i + 4], 8);
455           printf ("F40.%d(%.*f)\n", data[i], data[i], d);
456           i += 12;
457         }
458       else if (i + 12 <= n
459                && data[i + 1] == 40
460                && data[i + 2] == 31
461                && data[i + 3] == 0)
462         {
463           double d;
464
465           memcpy (&d, &data[i + 4], 8);
466           printf ("PCT40.%d(%.*f)\n", data[i], data[i], d);
467           i += 12;
468         }
469       else if (i + 4 <= n
470                && (data[i] && data[i] != 88 && data[i] != 0x41)
471                && !data[i + 1]
472                && !data[i + 2]
473                && !data[i + 3])
474         {
475           printf ("i%d ", data[i]);
476           i += 4;
477         }
478       else
479         {
480           printf("%02x ", data[i]);
481           i++;
482         }
483     }
484
485   return 0;
486 }