make all the dump_*_() functions much more similar
[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 dump_value(int level, bool match1);
208 static void dump_substs(void (*dump)(int level), int level);
209 static void dump_value__(int level, bool match1);
210
211 static void
212 dump_value__with_preskip(int level)
213 {
214   match_byte(0);
215   match_byte(0);
216   match_byte(0);
217   match_byte(0);
218   dump_value__ (level, false);
219   putchar('\n');
220 }
221
222 static void
223 dump_value__(int level, bool match1)
224 {
225   if (match_byte (3))
226     {
227       char *s1 = get_string();
228       dump_value_31();
229       char *s2 = get_string();
230       char *s3 = get_string();
231       if (strcmp(s1, s3))
232         printf("strings \"%s\", \"%s\" and \"%s\"", s1, s2, s3);
233       else
234         printf("string \"%s\" and \"%s\"", s1, s2);
235       if (!match_byte (0))
236         match_byte_assert(1);
237       if (match1)
238         match_byte (1);
239     }
240   else if (match_byte (5))
241     {
242       dump_value_31();
243       printf ("variable \"%s\"", get_string());
244       get_string();
245       if (!match_byte(1) && !match_byte(2))
246         match_byte_assert(3);
247     }
248   else if (match_byte (2))
249     {
250       unsigned int format;
251       char *var, *vallab;
252       double value;
253
254       match_byte_assert (0x58);
255       format = get_u32 ();
256       value = get_double ();
257       var = get_string ();
258       vallab = get_string ();
259       printf ("value %g format %d(%d.%d) var \"%s\" vallab \"%s\"",
260               value, format >> 16, (format >> 8) & 0xff, format & 0xff, var, vallab);
261       if (!match_byte (1) && !match_byte(2))
262         match_byte_assert (3);
263     }
264   else if (match_byte (4))
265     {
266       unsigned int format;
267       char *var, *vallab, *value;
268
269       match_byte_assert (0x58);
270       format = get_u32 ();
271       vallab = get_string ();
272       var = get_string ();
273       if (!match_byte(1) && !match_byte(2))
274         match_byte_assert (3);
275       value = get_string ();
276       printf ("value \"%s\" format %d(%d.%d) var \"%s\" vallab \"%s\"",
277               value, format >> 16, (format >> 8) & 0xff, format & 0xff, var, vallab);
278     }
279   else if (match_byte (1))
280     {
281       unsigned int format;
282       double value;
283
284       dump_value_31();
285       format = get_u32 ();
286       value = get_double ();
287       printf ("value %g format %d(%d.%d)", value, format >> 16, (format >> 8) & 0xff, format & 0xff);
288       if (match1)
289         match_byte (1);
290     }
291   else
292     dump_substs(dump_value__with_preskip, level + 1);
293 }
294
295 static void
296 dump_value(int level, bool match1)
297 {
298   for (int i = 0; i <= level; i++)
299     printf ("    ");
300
301   match_byte (0);
302   match_byte (0);
303   match_byte (0);
304   match_byte (0);
305   dump_value__(level, match1);
306   match_byte(0);
307   match_byte(0);
308   match_byte(0);
309 }
310
311 static void
312 dump_dim_value(int level)
313 {
314   for (int i = 0; i <= level; i++)
315     printf ("    ");
316
317   match_byte(0);
318   match_byte(0);
319   match_byte(0);
320   match_byte(0);
321   dump_value__(level, true);
322 }
323
324 static void
325 dump_category(int level)
326 {
327   dump_value (level, true);
328
329   if (match_u32 (1))
330     match_byte (0);
331   else if (match_byte (1))
332     {
333       match_byte (0);
334       if (!match_u32 (2))
335         match_u32_assert (1);
336       match_byte (0);
337     }
338   else if (!match_u32(2))
339     match_u32_assert (0);
340
341   get_u32 ();
342
343   int n_categories = get_u32();
344   if (n_categories > 0)
345     printf (", %d subcategories:", n_categories);
346   printf("\n");
347   for (int i = 0; i < n_categories; i++)
348     dump_category (level + 1);
349 }
350
351 static void
352 dump_dim(void)
353 {
354   int n_categories;
355   printf("next dim\n");
356   dump_dim_value(0);
357
358   /* This byte is usually 0x02 but 0x00 and 0x75 (!) have also been spotted. */
359   pos++;
360
361   if (!match_byte(0) && !match_byte(1))
362     match_byte_assert(2);
363   if (!match_u32(0))
364     match_u32_assert(2);
365   if (!match_byte(0))
366     match_byte_assert(1);
367   get_u32();
368   match_byte(0);
369   match_byte(0);
370   n_categories = get_u32();
371   printf("%d nested categories\n", n_categories);
372   for (int i = 0; i < n_categories; i++)
373     dump_category (0);
374 }
375
376 int n_dims;
377 static void
378 dump_dims(void)
379 {
380   n_dims = get_u32();
381   printf ("%u dimensions\n", n_dims);
382   for (int i = 0; i < n_dims; i++)
383     {
384       printf("\n");
385       dump_dim ();
386     }
387 }
388
389 static void
390 dump_substs(void (*dump)(int level), int level)
391 {
392   dump_value_31();
393
394   char *base = get_string();
395   int x = get_u32();
396   printf ("\"%s\" with %d variables:\n", base, x);
397   for (int i = 0; i < x; i++)
398     {
399       int y = get_u32();
400       if (!y)
401         y = 1;
402       else
403         match_u32_assert(0);
404       for (int j = 0; j <= level; j++)
405         printf ("    ");
406       printf("variable %d has %d values:\n", i, y);
407       for (int j = 0; j < y; j++)
408         {
409           dump (level+1);
410           putchar('\n');
411         }
412     }
413 }
414
415 static void
416 dump_data_value(int level)
417 {
418   for (int i = 0; i <= level; i++)
419     printf ("    ");
420
421   match_byte(0);
422   match_byte(0);
423   match_byte(0);
424   match_byte(0);
425   if (data[pos] <= 5)
426     dump_value__(0, false);
427   else
428     dump_substs (dump_data_value, level + 1);
429 }
430
431 static void
432 dump_data(void)
433 {
434 #if 1
435   int a[16];
436   for (int i = 0; i < 3 + n_dims; i++)
437     a[i] = get_u32();
438   printf ("data intro:");
439   for (int i = 0; i < 3 + n_dims; i++)
440     printf(" %d", a[i]);
441   printf("\n");
442 #else
443   fprintf (stderr,"data intro (%d dims):", n_dims);
444   for (int i = 0; i < 3+n_dims; i++)
445     fprintf (stderr," %d", get_u32());
446   fprintf(stderr,"\n");
447 #endif
448   int x = get_u32();
449   printf ("%d data values, starting at %08x\n", x, pos);
450   for (int i = 0; i < x; i++)
451     {
452       printf("%08x, index %d:\n", pos, get_u32());
453       match_u32_assert(0);
454       dump_data_value(0);
455       putchar('\n');
456     }
457 }
458
459 static void
460 dump_title_value(int level)
461 {
462   for (int i = 0; i <= level; i++)
463     printf ("    ");
464
465   match_byte (0);
466   match_byte (0);
467   match_byte (0);
468   match_byte (0);
469   if (data[pos] <= 5)
470     dump_value__(level, true);
471   else
472     dump_substs(dump_title_value, level + 1);
473 }
474
475 static void
476 dump_footnote_value(int level)
477 {
478   for (int i = 0; i <= level; i++)
479     printf ("    ");
480
481   match_byte (0);
482   match_byte (0);
483   match_byte (0);
484   match_byte (0);
485   if (data[pos] <= 5)
486     dump_value__(level, false);
487   else
488     dump_substs(dump_footnote_value, level + 1);
489 }
490
491 static void
492 dump_title(void)
493 {
494   pos = 0x27;
495   dump_title_value(0); putchar('\n');
496   dump_title_value(0); putchar('\n');
497   match_byte_assert(0x31);
498   dump_title_value(0); putchar('\n');
499   match_byte(0);
500   match_byte_assert(0x58);
501   if (match_byte(0x31))
502     {
503       dump_footnote_value(0); putchar('\n');
504     }
505   else
506     match_byte_assert(0x58);
507
508
509   int n_footnotes = get_u32();
510   if (n_footnotes >= 20)
511     {
512       fprintf(stderr, "%08x: %d footnotes\n", pos - 4, n_footnotes);
513       exit(1);
514     }
515
516   printf("------\n%d footnotes\n", n_footnotes);
517   if (n_footnotes < 20)
518     {
519       for (int i = 0; i < n_footnotes; i++)
520         {
521           printf("footnote %d:\n", i);
522           dump_footnote_value(0);
523           if (match_byte (0x31))
524             {
525               /* Custom footnote marker string. */
526               match_byte_assert(3);
527               get_string();
528               match_byte_assert(0x58);
529               match_u32_assert(0);
530               get_string();
531             }
532           else
533             match_byte_assert (0x58);
534           printf("(%d)\n", get_u32());
535         }
536     }
537 }
538
539 static int
540 find_dimensions(void)
541 {
542   {
543     const char dimensions[] = "-,,,.\0";
544     int x = try_find_tail(dimensions, sizeof dimensions - 1);
545     if (x)
546       return x;
547   }
548
549   const char dimensions[] = "-,,, .\0";
550   return find_tail(dimensions, sizeof dimensions - 1);
551 }
552
553 static void
554 dump_fonts(void)
555 {
556   printf("fonts: offset=%08x\n", pos);
557   match_byte(0);
558   for (int i = 1; i <= 8; i++)
559     {
560       printf("%08x: font %d, ", pos, i);
561       match_byte_assert(i);
562       match_byte_assert(0x31);
563       printf("%s, ", get_string());
564       match_byte_assert(0);
565       match_byte_assert(0);
566       if (!match_byte(0x40) && !match_byte(0x20) && !match_byte(0x80) && !match_byte(0x10))
567         match_byte_assert(0x50);
568       if (!match_byte(0x41))
569         match_byte_assert(0x51);
570       pos += 13;
571       printf ("%s, ", get_string());
572       printf ("%s, ", get_string());
573       match_u32_assert(0);
574       match_u32_assert(0);
575       pos++;
576       get_u32();
577       get_u32();
578       get_u32();
579       get_u32();
580       putchar('\n');
581     }
582
583   match_u32_assert(240);
584   pos += 240;
585
586   match_u32_assert(18);
587   pos += 18;
588
589   if (match_u32(117))
590     pos += 117;
591   else
592     {
593       match_u32_assert(142);
594       pos += 142;
595     }
596
597   int count = get_u32();
598   pos += 4 * count;
599
600   char *encoding = get_string();
601   printf("encoding=%s\n", encoding);
602
603   if (!match_u32(0))
604     match_u32_assert(UINT32_MAX);
605   if (!match_byte(0))
606     match_byte_assert(1);
607   match_byte_assert(0);
608   if (!match_byte(0))
609     match_byte_assert(1);
610   if (!match_byte(0x99) && !match_byte(0x98))
611     match_byte_assert(0x97);
612   match_byte_assert(7);
613   match_byte_assert(0);
614   match_byte_assert(0);
615   if (match_byte('.'))
616     match_byte_assert(',');
617   else
618     {
619       match_byte_assert(',');
620       if (!match_byte('.'))
621         match_byte_assert(' ');
622     }
623   match_u32_assert(5);
624   for (int i = 0; i < 5; i++)
625     get_string();
626   pos += get_u32();
627   if (pos != find_dimensions())
628     fprintf (stderr, "%08x / %08x\n", pos, find_dimensions());
629 }
630
631 int
632 main(int argc, char *argv[])
633 {
634   size_t start;
635   struct stat s;
636
637   if (isatty(STDIN_FILENO))
638     {
639       fprintf(stderr, "redirect stdin from a .bin file\n");
640       exit(1);
641     }
642   if (fstat(STDIN_FILENO, &s))
643     {
644       perror("fstat");
645       exit(1);
646     }
647   n = s.st_size;
648   data = malloc(n);
649   if (!data)
650     {
651       perror("malloc");
652       exit(1);
653     }
654   if (read(STDIN_FILENO, data, n) != n)
655     {
656       perror("read");
657       exit(1);
658     }
659
660   if (argc > 1)
661     {
662       if (!strcmp(argv[1], "title0"))
663         {
664           pos = 0x27;
665           if (match_byte (0x03)
666               || (match_byte (0x05) && match_byte (0x58)))
667             printf ("%s\n", get_string());
668           else
669             printf ("<unknown>\n");
670           return 0;
671         }
672       else if (!strcmp(argv[1], "title"))
673         {
674           dump_title();
675           exit(0);
676         }
677       else if (!strcmp(argv[1], "titleraw"))
678         {
679           const char fonts[] = "\x01\x31\x09\0\0\0SansSerif";
680           start = 0x27;
681           n = find(fonts, sizeof fonts - 1);
682         }
683       else if (!strcmp(argv[1], "fonts"))
684         {
685           const char fonts[] = "\x01\x31\x09\0\0\0SansSerif";
686           const char styles[] = "\xf0\0\0\0";
687           start = find(fonts, sizeof fonts - 1);
688           n = find(styles, sizeof styles - 1);
689         }
690       else if (!strcmp(argv[1], "styles"))
691         {
692           const char styles[] = "\xf0\0\0\0";
693           const char dimensions[] = "-,,,.\0";
694           start = find(styles, sizeof styles - 1);
695           n = find(dimensions, sizeof dimensions - 1) + sizeof dimensions - 1;
696         }
697       else if (!strcmp(argv[1], "dimensions") || !strcmp(argv[1], "all"))
698         {
699           pos = 0;
700           match_byte_assert(1);
701           match_byte_assert(0);
702           match_u32_assert(3);
703           match_byte_assert(1);
704           if (!match_byte(0))
705             match_byte_assert(1);
706           match_byte_assert(0);
707           match_byte_assert(0);
708           if (!match_byte(0))
709             match_byte_assert(1);
710           pos++;
711           match_byte_assert(0);
712           match_byte_assert(0);
713           match_byte_assert(0);
714           dump_title ();
715           dump_fonts();
716           dump_dims ();
717           printf("\n\ndata:\n");
718           dump_data ();
719           match_byte (1);
720           if (pos != n)
721             {
722               fprintf (stderr, "%x / %x\n", pos, n);
723               exit(1);
724             }
725           exit(0);
726         }
727       else
728         {
729           fprintf (stderr, "unknown section %s\n", argv[1]);
730           exit(1);
731         }
732     }
733   else
734     start = 0x27;
735
736   for (size_t i = start; i < n; )
737     {
738       if (i + 5 <= n
739           && data[i]
740           //&& !data[i + 1]
741           && !data[i + 2]
742           && !data[i + 3]
743           && i + 4 + data[i] + data[i + 1] * 256 <= n
744           && all_ascii(&data[i + 4], data[i] + data[i + 1] * 256))
745         {
746           fputs("\n\"", stdout);
747           fwrite(&data[i + 4], 1, data[i] + data[i + 1] * 256, stdout);
748           fputs("\" ", stdout);
749
750           i += 4 + data[i] + data[i + 1] * 256;
751         }
752       else if (i + 12 <= n
753                && data[i + 1] == 40
754                && data[i + 2] == 5
755                && data[i + 3] == 0)
756         {
757           double d;
758
759           memcpy (&d, &data[i + 4], 8);
760           printf ("F40.%d(%.*f)\n", data[i], data[i], d);
761           i += 12;
762         }
763       else if (i + 12 <= n
764                && data[i + 1] == 40
765                && data[i + 2] == 31
766                && data[i + 3] == 0)
767         {
768           double d;
769
770           memcpy (&d, &data[i + 4], 8);
771           printf ("PCT40.%d(%.*f)\n", data[i], data[i], d);
772           i += 12;
773         }
774       else if (i + 4 <= n
775                && (data[i] && data[i] != 88 && data[i] != 0x41)
776                && !data[i + 1]
777                && !data[i + 2]
778                && !data[i + 3])
779         {
780           printf ("i%d ", data[i]);
781           i += 4;
782         }
783       else
784         {
785           printf("%02x ", data[i]);
786           i++;
787         }
788     }
789
790   return 0;
791 }