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