Work on "format" element.
[pspp] / dump2.c
1 #include <assert.h>
2 #include <float.h>
3 #include <stdbool.h>
4 #include <stdint.h>
5 #include <stdio.h>
6 #include <stdlib.h>
7 #include <string.h>
8 #include <sys/stat.h>
9 #include <unistd.h>
10 #include "u8-mbtouc.h"
11
12 static uint8_t *data;
13 static size_t n, pos;
14
15 #define XSTR(x) #x
16 #define STR(x) XSTR(x)
17 #define WHERE __FILE__":" STR(__LINE__)
18
19 static unsigned int
20 get_u32(void)
21 {
22   uint32_t x;
23   memcpy(&x, &data[pos], 4);
24   pos += 4;
25   return x;
26 }
27
28 static double
29 get_double(void)
30 {
31   double x;
32   memcpy(&x, &data[pos], 8);
33   pos += 8;
34   return x;
35 }
36
37 static bool
38 match_u32(uint32_t x)
39 {
40   if (get_u32() == x)
41     return true;
42   pos -= 4;
43   return false;
44 }
45
46 static void
47 match_u32_assert(uint32_t x, const char *where)
48 {
49   unsigned int y = get_u32();
50   if (x != y)
51     {
52       fprintf(stderr, "%s: 0x%x: expected i%u, got i%u\n", where, pos - 4, x, y);
53       exit(1);
54     }
55 }
56 #define match_u32_assert(x) match_u32_assert(x, WHERE)
57
58 static bool
59 match_byte(uint8_t b)
60 {
61   if (pos < n && data[pos] == b)
62     {
63       pos++;
64       return true;
65     }
66   else
67     return false;
68 }
69
70 static void
71 match_byte_assert(uint8_t b, const char *where)
72 {
73   if (!match_byte(b))
74     {
75       fprintf(stderr, "%s: 0x%x: expected %02x, got %02x\n", where, pos, b, data[pos]);
76       exit(1);
77     }
78 }
79 #define match_byte_assert(b) match_byte_assert(b, WHERE)
80
81 static bool
82 all_ascii(const uint8_t *p)
83 {
84   for (; *p; p++)
85     if (*p < 32 || *p > 126)
86       return false;
87   return true;
88 }
89
90 static bool
91 all_utf8(const uint8_t *p)
92 {
93   size_t len = strlen ((char *) p);
94   for (size_t ofs = 0, mblen; ofs < len; ofs += mblen)
95     {
96       ucs4_t uc;
97
98       mblen = u8_mbtouc (&uc, p + ofs, len - ofs);
99       if (uc < 32 || uc == 127 || uc == 0xfffd)
100         return false;
101     }
102   return true;
103 }
104
105 static char *
106 get_fixed_string(int len, const char *where)
107 {
108   if (pos + len > n || !memchr(&data[pos], 0, len) || !all_utf8(&data[pos]))
109     {
110       fprintf(stderr, "%s: 0x%x: bad fixed-width string\n", where, pos);
111       exit(1);
112     }
113   char *s = (char *) &data[pos];
114   pos += len;
115   return s;
116 }
117 #define get_fixed_string(len) get_fixed_string(len, WHERE)
118
119 static bool
120 all_ascii2(const uint8_t *p, size_t n)
121 {
122   for (size_t i = 0; i < n; i++)
123     if (p[i] < 32 || p[i] > 126)
124       return false;
125   return true;
126 }
127
128 static char *
129 get_string(const char *where)
130 {
131   if (1
132       /*data[pos + 1] == 0 && data[pos + 2] == 0 && data[pos + 3] == 0*/
133       /*&& all_ascii(&data[pos + 4], data[pos])*/)
134     {
135       int len = data[pos] + data[pos + 1] * 256;
136       char *s = malloc(len + 1);
137
138       memcpy(s, &data[pos + 4], len);
139       s[len] = 0;
140       pos += 4 + len;
141       return s;
142     }
143   else
144     {
145       fprintf(stderr, "%s: 0x%x: expected string\n", where, pos);
146       exit(1);
147     }
148 }
149 #define get_string() get_string(WHERE)
150
151 static void
152 dump_raw(FILE *stream, int start, int end, const char *separator)
153 {
154   for (size_t i = start; i < end; )
155     {
156       if (i + 5 <= n
157           && data[i] > 0
158           //&& !data[i + 1]
159           && !data[i + 2]
160           && !data[i + 3]
161           && i + 4 + data[i] + data[i + 1] * 256 <= end
162           && all_ascii2(&data[i + 4], data[i] + data[i + 1] * 256))
163         {
164           fprintf(stream, "%s\"", separator);
165           fwrite(&data[i + 4], 1, data[i] + data[i + 1] * 256, stream);
166           fputs("\" ", stream);
167
168           i += 4 + data[i] + data[i + 1] * 256;
169         }
170       else if (i + 12 <= end
171                && data[i + 1] == 40
172                && data[i + 2] == 5
173                && data[i + 3] == 0)
174         {
175           double d;
176
177           memcpy (&d, &data[i + 4], 8);
178           fprintf (stream, "F40.%d(%.*f)%s", data[i], data[i], d, separator);
179           i += 12;
180         }
181       else if (i + 12 <= end
182                && data[i + 1] == 40
183                && data[i + 2] == 31
184                && data[i + 3] == 0)
185         {
186           double d;
187
188           memcpy (&d, &data[i + 4], 8);
189           fprintf (stream, "PCT40.%d(%.*f)%s", data[i], data[i], d, separator);
190           i += 12;
191         }
192       else if (i + 4 <= end
193                && !data[i + 1]
194                && !data[i + 2]
195                && !data[i + 3])
196         {
197           fprintf (stream, "i%d ", data[i]);
198           i += 4;
199         }
200       else
201         {
202           fprintf(stream, "%02x ", data[i]);
203           i++;
204         }
205     }
206 }
207
208 static void
209 dump_source(int end, int count, int n_series, const char *name)
210 {
211   const union
212     {
213       uint8_t b[8];
214       double d;
215     }
216   sysmis = {.b = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xef, 0xff}};
217   int n_sysmis = 0;
218   for (int i = 0; i < n_series; i++)
219     {
220       printf ("    series %d: \"%s\", %d values:\n        ",
221               i, get_fixed_string(288), count);
222       for (int i = 0; i < count; i++)
223         {
224           double d = get_double();
225           if (d == sysmis.d)
226             {
227               printf (" .");
228               n_sysmis++;
229             }
230           else
231             printf (" %.*g", DBL_DIG, d);
232         }
233       printf ("\n");
234     }
235
236   if (pos >= end)
237     return;
238
239   match_u32_assert(1);
240   char *name2 = get_string();
241   assert(!strcmp(name, name2));
242
243   printf ("\n    %08x:", pos);
244   int n_more_series = get_u32();
245   if (n_series != n_more_series)
246     printf("different series counts: %d %d\n", n_series, n_more_series);
247   assert(n_more_series <= n_series);
248   printf (" %d series to come\n", n_more_series);
249
250   int max1 = -1;
251   int ofs = pos;
252   for (int i = 0; i < n_more_series; i++)
253     {
254       printf ("%08x:", pos);
255       printf (" \"%s\"", get_string());
256       int n_pairs = get_u32();
257       for (int j = 0; j < n_pairs; j++)
258         {
259           int x = get_u32();
260           int y = get_u32();
261           printf (" (%d, %d)", x, y);
262           if (y > max1)
263             max1 = y;
264         }
265       printf ("\n");
266     }
267
268   printf ("\n%08x:", pos);
269   int n_strings = get_u32();
270   assert(n_strings == max1 + 1);
271   printf (" %d strings\n", n_strings);
272
273   char **strings = malloc(n_strings * sizeof *strings);
274   for (int i = 0; i < n_strings; i++)
275     {
276       int frequency = get_u32();
277       char *s = get_string();
278       printf ("%d: \"%s\" (%d)\n", i, s, frequency);
279       strings[i] = s;
280     }
281   printf ("\n");
282
283   assert (pos == end);
284   pos = ofs;
285   printf("Strings:\n");
286   for (int i = 0; i < n_more_series; i++)
287     {
288       printf ("  \"%s\"\n", get_string());
289       int n_pairs = get_u32();
290       for (int j = 0; j < n_pairs; j++)
291         {
292           int x = get_u32();
293           //assert (x == j);
294           int y = get_u32();
295           printf ("    %d: \"%s\"\n", x, strings[y]);
296         }
297       printf ("\n");
298     }
299   pos = end;
300 }
301
302 int
303 main(int argc, char **argv)
304 {
305   size_t start;
306   struct stat s;
307
308   if (isatty(STDIN_FILENO))
309     {
310       fprintf(stderr, "redirect stdin from a .bin file\n");
311       exit(1);
312     }
313   if (fstat(STDIN_FILENO, &s))
314     {
315       perror("fstat");
316       exit(1);
317     }
318   n = s.st_size;
319   data = malloc(n);
320   if (!data)
321     {
322       perror("malloc");
323       exit(1);
324     }
325   if (read(STDIN_FILENO, data, n) != n)
326     {
327       perror("read");
328       exit(1);
329     }
330
331   pos = 0;
332   match_byte_assert(0);
333   int version = data[pos];
334   if (!match_byte(0xaf))
335     match_byte_assert(0xb0);
336   int n_sources = data[pos++];
337   match_byte_assert(0);
338
339   match_u32_assert(s.st_size);
340   printf ("%d sources\n", n_sources);
341
342   struct source
343     {
344       int offset, count, n_series;
345       char *name;
346     }
347   sources[n_sources];
348   for (int i = 0; i < n_sources; i++)
349     {
350       int count = get_u32();
351       int n_series = get_u32();
352       int offset = get_u32();
353       char *name = get_fixed_string(version == 0xb0 ? 64 : 28);
354       int dunno = version == 0xb0 ? get_u32() : 0;
355       printf ("source %d: %d series, %d observations per series, offset %08x, \"%s\", %x\n",
356               i, n_series, count, offset, name, dunno);
357       sources[i].offset = offset;
358       sources[i].count = count;
359       sources[i].n_series = n_series;
360       sources[i].name = name;
361     }
362
363   for (int i = 0; i < n_sources; i++)
364     {
365       if (pos != sources[i].offset)
366         {
367           fprintf (stderr, "pos=0x%x expected=0x%x reading source %d\n", pos, sources[i].offset, i);
368           exit(1);
369         }
370       dump_source(i + 1 >= n_sources ? n : sources[i + 1].offset, sources[i].count, sources[i].n_series, sources[i].name);
371     }
372   assert(pos == n);
373
374   return 0;
375 }