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