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