Get rid of dump_value__() wrappers.
[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 (pos < n && 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(const char *where)
129 {
130   if (1
131       /*data[pos + 1] == 0 && data[pos + 2] == 0 && data[pos + 3] == 0*/
132       /*&& all_ascii(&data[pos + 4], data[pos])*/)
133     {
134       int len = data[pos] + data[pos + 1] * 256;
135       char *s = malloc(len + 1);
136
137       memcpy(s, &data[pos + 4], len);
138       s[len] = 0;
139       pos += 4 + len;
140       return s;
141     }
142   else
143     {
144       fprintf(stderr, "%s: 0x%x: expected string\n", where, pos);
145       exit(1);
146     }
147 }
148 #define get_string() get_string(WHERE)
149
150 static void
151 dump_value_31(void)
152 {
153   if (match_byte (0x31))
154     {
155       if (match_u32 (0))
156         {
157           if (match_u32 (1))
158             get_string();
159           else
160             match_u32_assert (0);
161           int subn = get_u32 ();
162           printf ("nested %d bytes", subn);
163           pos += subn;
164         }
165       else if (match_u32 (1))
166         {
167           printf("(footnote %d) ", get_u32());
168           match_byte_assert (0);
169           match_byte_assert (0);
170           int subn = get_u32 ();
171           printf ("nested %d bytes", subn);
172           pos += subn;
173         }
174       else if (match_u32 (2))
175         {
176           printf("(special 2)");
177           match_byte_assert(0);
178           match_byte_assert(0);
179           if (!match_u32 (2))
180             match_u32_assert(1);
181           match_byte_assert(0);
182           match_byte_assert(0);
183           int subn = get_u32 ();
184           printf ("nested %d bytes", subn);
185           pos += subn;
186         }
187       else
188         {
189           match_u32_assert(3);
190           printf("(special 3)");
191           match_byte_assert(0);
192           match_byte_assert(0);
193           match_byte_assert(1);
194           match_byte_assert(0);
195           int subn = get_u32 ();
196           printf ("nested %d bytes, ", subn);
197           pos += subn;
198           subn = get_u32 ();
199           printf ("nested %d bytes, ", subn);
200           pos += subn;
201         }
202     }
203   else
204     match_byte_assert (0x58);
205 }
206
207 static void
208 dump_value__(int level, bool match1)
209 {
210   for (int i = 0; i <= level; i++)
211     printf ("    ");
212
213   match_byte(0);
214   match_byte(0);
215   match_byte(0);
216   match_byte(0);
217
218   if (match_byte (3))
219     {
220       char *s1 = get_string();
221       dump_value_31();
222       char *s2 = get_string();
223       char *s3 = get_string();
224       if (strcmp(s1, s3))
225         printf("strings \"%s\", \"%s\" and \"%s\"", s1, s2, s3);
226       else
227         printf("string \"%s\" and \"%s\"", s1, s2);
228       if (!match_byte (0))
229         match_byte_assert(1);
230       if (match1)
231         match_byte (1);
232     }
233   else if (match_byte (5))
234     {
235       dump_value_31();
236       printf ("variable \"%s\"", get_string());
237       get_string();
238       if (!match_byte(1) && !match_byte(2))
239         match_byte_assert(3);
240     }
241   else if (match_byte (2))
242     {
243       unsigned int format;
244       char *var, *vallab;
245       double value;
246
247       match_byte_assert (0x58);
248       format = get_u32 ();
249       value = get_double ();
250       var = get_string ();
251       vallab = get_string ();
252       printf ("value %g format %d(%d.%d) var \"%s\" vallab \"%s\"",
253               value, format >> 16, (format >> 8) & 0xff, format & 0xff, var, vallab);
254       if (!match_byte (1) && !match_byte(2))
255         match_byte_assert (3);
256     }
257   else if (match_byte (4))
258     {
259       unsigned int format;
260       char *var, *vallab, *value;
261
262       match_byte_assert (0x58);
263       format = get_u32 ();
264       vallab = get_string ();
265       var = get_string ();
266       if (!match_byte(1) && !match_byte(2))
267         match_byte_assert (3);
268       value = get_string ();
269       printf ("value \"%s\" format %d(%d.%d) var \"%s\" vallab \"%s\"",
270               value, format >> 16, (format >> 8) & 0xff, format & 0xff, var, vallab);
271     }
272   else if (match_byte (1))
273     {
274       unsigned int format;
275       double value;
276
277       dump_value_31();
278       format = get_u32 ();
279       value = get_double ();
280       printf ("value %g format %d(%d.%d)", value, format >> 16, (format >> 8) & 0xff, format & 0xff);
281       if (match1)
282         match_byte (1);
283     }
284   else
285     {
286       dump_value_31();
287
288       char *base = get_string();
289       int x = get_u32();
290       printf ("\"%s\" with %d variables:\n", base, x);
291       for (int i = 0; i < x; i++)
292         {
293           int y = get_u32();
294           if (!y)
295             y = 1;
296           else
297             match_u32_assert(0);
298           for (int j = 0; j <= level; j++)
299             printf ("    ");
300           printf("variable %d has %d values:\n", i, y);
301           for (int j = 0; j < y; j++)
302             {
303               dump_value__ (level + 1, false);
304               putchar('\n');
305             }
306         }
307     }
308 }
309
310 static void
311 dump_category(int level)
312 {
313   dump_value__ (level, true);
314   match_byte(0);
315   match_byte(0);
316   match_byte(0);
317
318   if (match_u32 (1))
319     match_byte (0);
320   else if (match_byte (1))
321     {
322       match_byte (0);
323       if (!match_u32 (2))
324         match_u32_assert (1);
325       match_byte (0);
326     }
327   else if (!match_u32(2))
328     match_u32_assert (0);
329
330   get_u32 ();
331
332   int n_categories = get_u32();
333   if (n_categories > 0)
334     printf (", %d subcategories:", n_categories);
335   printf("\n");
336   for (int i = 0; i < n_categories; i++)
337     dump_category (level + 1);
338 }
339
340 static void
341 dump_dim(void)
342 {
343   int n_categories;
344   printf("next dim\n");
345   dump_value__ (0, false);
346
347   /* This byte is usually 0x02 but 0x00 and 0x75 (!) have also been spotted. */
348   pos++;
349
350   if (!match_byte(0) && !match_byte(1))
351     match_byte_assert(2);
352   if (!match_u32(0))
353     match_u32_assert(2);
354   if (!match_byte(0))
355     match_byte_assert(1);
356   get_u32();
357   match_byte(0);
358   match_byte(0);
359   n_categories = get_u32();
360   printf("%d nested categories\n", n_categories);
361   for (int i = 0; i < n_categories; i++)
362     dump_category (0);
363 }
364
365 int n_dims;
366 static void
367 dump_dims(void)
368 {
369   n_dims = get_u32();
370   printf ("%u dimensions\n", n_dims);
371   for (int i = 0; i < n_dims; i++)
372     {
373       printf("\n");
374       dump_dim ();
375     }
376 }
377
378 static void
379 dump_data(void)
380 {
381 #if 1
382   int a[16];
383   for (int i = 0; i < 3 + n_dims; i++)
384     a[i] = get_u32();
385   printf ("data intro:");
386   for (int i = 0; i < 3 + n_dims; i++)
387     printf(" %d", a[i]);
388   printf("\n");
389 #else
390   fprintf (stderr,"data intro (%d dims):", n_dims);
391   for (int i = 0; i < 3+n_dims; i++)
392     fprintf (stderr," %d", get_u32());
393   fprintf(stderr,"\n");
394 #endif
395   int x = get_u32();
396   printf ("%d data values, starting at %08x\n", x, pos);
397   for (int i = 0; i < x; i++)
398     {
399       printf("%08x, index %d:\n", pos, get_u32());
400       match_u32_assert(0);
401       dump_value__(0, false);
402       putchar('\n');
403     }
404 }
405
406 static void
407 dump_title(void)
408 {
409   pos = 0x27;
410   dump_value__(0, true); putchar('\n');
411   dump_value__(0, true); putchar('\n');
412   match_byte_assert(0x31);
413   dump_value__(0, true); putchar('\n');
414   match_byte(0);
415   match_byte_assert(0x58);
416   if (match_byte(0x31))
417     {
418       dump_value__(0, false); putchar('\n');
419     }
420   else
421     match_byte_assert(0x58);
422
423
424   int n_footnotes = get_u32();
425   if (n_footnotes >= 20)
426     {
427       fprintf(stderr, "%08x: %d footnotes\n", pos - 4, n_footnotes);
428       exit(1);
429     }
430
431   printf("------\n%d footnotes\n", n_footnotes);
432   if (n_footnotes < 20)
433     {
434       for (int i = 0; i < n_footnotes; i++)
435         {
436           printf("footnote %d:\n", i);
437           dump_value__(0, false);
438           if (match_byte (0x31))
439             {
440               /* Custom footnote marker string. */
441               match_byte_assert(3);
442               get_string();
443               match_byte_assert(0x58);
444               match_u32_assert(0);
445               get_string();
446             }
447           else
448             match_byte_assert (0x58);
449           printf("(%d)\n", get_u32());
450         }
451     }
452 }
453
454 static int
455 find_dimensions(void)
456 {
457   {
458     const char dimensions[] = "-,,,.\0";
459     int x = try_find_tail(dimensions, sizeof dimensions - 1);
460     if (x)
461       return x;
462   }
463
464   const char dimensions[] = "-,,, .\0";
465   return find_tail(dimensions, sizeof dimensions - 1);
466 }
467
468 static void
469 dump_fonts(void)
470 {
471   printf("fonts: offset=%08x\n", pos);
472   match_byte(0);
473   for (int i = 1; i <= 8; i++)
474     {
475       printf("%08x: font %d, ", pos, i);
476       match_byte_assert(i);
477       match_byte_assert(0x31);
478       printf("%s, ", get_string());
479       match_byte_assert(0);
480       match_byte_assert(0);
481       if (!match_byte(0x40) && !match_byte(0x20) && !match_byte(0x80) && !match_byte(0x10))
482         match_byte_assert(0x50);
483       if (!match_byte(0x41))
484         match_byte_assert(0x51);
485       pos += 13;
486       printf ("%s, ", get_string());
487       printf ("%s, ", get_string());
488       match_u32_assert(0);
489       match_u32_assert(0);
490       pos++;
491       get_u32();
492       get_u32();
493       get_u32();
494       get_u32();
495       putchar('\n');
496     }
497
498   match_u32_assert(240);
499   pos += 240;
500
501   match_u32_assert(18);
502   pos += 18;
503
504   if (match_u32(117))
505     pos += 117;
506   else
507     {
508       match_u32_assert(142);
509       pos += 142;
510     }
511
512   int count = get_u32();
513   pos += 4 * count;
514
515   char *encoding = get_string();
516   printf("encoding=%s\n", encoding);
517
518   if (!match_u32(0))
519     match_u32_assert(UINT32_MAX);
520   if (!match_byte(0))
521     match_byte_assert(1);
522   match_byte_assert(0);
523   if (!match_byte(0))
524     match_byte_assert(1);
525   if (!match_byte(0x99) && !match_byte(0x98))
526     match_byte_assert(0x97);
527   match_byte_assert(7);
528   match_byte_assert(0);
529   match_byte_assert(0);
530   if (match_byte('.'))
531     match_byte_assert(',');
532   else
533     {
534       match_byte_assert(',');
535       if (!match_byte('.'))
536         match_byte_assert(' ');
537     }
538   match_u32_assert(5);
539   for (int i = 0; i < 5; i++)
540     get_string();
541   pos += get_u32();
542   if (pos != find_dimensions())
543     fprintf (stderr, "%08x / %08x\n", pos, find_dimensions());
544 }
545
546 int
547 main(int argc, char *argv[])
548 {
549   size_t start;
550   struct stat s;
551
552   if (isatty(STDIN_FILENO))
553     {
554       fprintf(stderr, "redirect stdin from a .bin file\n");
555       exit(1);
556     }
557   if (fstat(STDIN_FILENO, &s))
558     {
559       perror("fstat");
560       exit(1);
561     }
562   n = s.st_size;
563   data = malloc(n);
564   if (!data)
565     {
566       perror("malloc");
567       exit(1);
568     }
569   if (read(STDIN_FILENO, data, n) != n)
570     {
571       perror("read");
572       exit(1);
573     }
574
575   if (argc > 1)
576     {
577       if (!strcmp(argv[1], "title0"))
578         {
579           pos = 0x27;
580           if (match_byte (0x03)
581               || (match_byte (0x05) && match_byte (0x58)))
582             printf ("%s\n", get_string());
583           else
584             printf ("<unknown>\n");
585           return 0;
586         }
587       else if (!strcmp(argv[1], "title"))
588         {
589           dump_title();
590           exit(0);
591         }
592       else if (!strcmp(argv[1], "titleraw"))
593         {
594           const char fonts[] = "\x01\x31\x09\0\0\0SansSerif";
595           start = 0x27;
596           n = find(fonts, sizeof fonts - 1);
597         }
598       else if (!strcmp(argv[1], "fonts"))
599         {
600           const char fonts[] = "\x01\x31\x09\0\0\0SansSerif";
601           const char styles[] = "\xf0\0\0\0";
602           start = find(fonts, sizeof fonts - 1);
603           n = find(styles, sizeof styles - 1);
604         }
605       else if (!strcmp(argv[1], "styles"))
606         {
607           const char styles[] = "\xf0\0\0\0";
608           const char dimensions[] = "-,,,.\0";
609           start = find(styles, sizeof styles - 1);
610           n = find(dimensions, sizeof dimensions - 1) + sizeof dimensions - 1;
611         }
612       else if (!strcmp(argv[1], "dimensions") || !strcmp(argv[1], "all"))
613         {
614           pos = 0;
615           match_byte_assert(1);
616           match_byte_assert(0);
617           match_u32_assert(3);
618           match_byte_assert(1);
619           if (!match_byte(0))
620             match_byte_assert(1);
621           match_byte_assert(0);
622           match_byte_assert(0);
623           if (!match_byte(0))
624             match_byte_assert(1);
625           pos++;
626           match_byte_assert(0);
627           match_byte_assert(0);
628           match_byte_assert(0);
629           dump_title ();
630           dump_fonts();
631           dump_dims ();
632           printf("\n\ndata:\n");
633           dump_data ();
634           match_byte (1);
635           if (pos != n)
636             {
637               fprintf (stderr, "%x / %x\n", pos, n);
638               exit(1);
639             }
640           exit(0);
641         }
642       else
643         {
644           fprintf (stderr, "unknown section %s\n", argv[1]);
645           exit(1);
646         }
647     }
648   else
649     start = 0x27;
650
651   for (size_t i = start; i < n; )
652     {
653       if (i + 5 <= n
654           && data[i]
655           //&& !data[i + 1]
656           && !data[i + 2]
657           && !data[i + 3]
658           && i + 4 + data[i] + data[i + 1] * 256 <= n
659           && all_ascii(&data[i + 4], data[i] + data[i + 1] * 256))
660         {
661           fputs("\n\"", stdout);
662           fwrite(&data[i + 4], 1, data[i] + data[i + 1] * 256, stdout);
663           fputs("\" ", stdout);
664
665           i += 4 + data[i] + data[i + 1] * 256;
666         }
667       else if (i + 12 <= n
668                && data[i + 1] == 40
669                && data[i + 2] == 5
670                && data[i + 3] == 0)
671         {
672           double d;
673
674           memcpy (&d, &data[i + 4], 8);
675           printf ("F40.%d(%.*f)\n", data[i], data[i], d);
676           i += 12;
677         }
678       else if (i + 12 <= n
679                && data[i + 1] == 40
680                && data[i + 2] == 31
681                && data[i + 3] == 0)
682         {
683           double d;
684
685           memcpy (&d, &data[i + 4], 8);
686           printf ("PCT40.%d(%.*f)\n", data[i], data[i], d);
687           i += 12;
688         }
689       else if (i + 4 <= n
690                && (data[i] && data[i] != 88 && data[i] != 0x41)
691                && !data[i + 1]
692                && !data[i + 2]
693                && !data[i + 3])
694         {
695           printf ("i%d ", data[i]);
696           i += 4;
697         }
698       else
699         {
700           printf("%02x ", data[i]);
701           i++;
702         }
703     }
704
705   return 0;
706 }