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