start moving beyond PMModelItemInfo
[pspp] / dump.c
diff --git a/dump.c b/dump.c
index c737c01d1dd4f8252967d8e8b66785f992cf9a70..7f3c2b8c75174a045f9666ff379af99efaef5aa3 100644 (file)
--- a/dump.c
+++ b/dump.c
@@ -1,4 +1,6 @@
 #include <assert.h>
+#include <errno.h>
+#include <fcntl.h>
 #include <float.h>
 #include <stdbool.h>
 #include <stdint.h>
@@ -6,42 +8,16 @@
 #include <stdlib.h>
 #include <string.h>
 #include <sys/stat.h>
+#include <time.h>
 #include <unistd.h>
 #include "u8-mbtouc.h"
 
+static const char *filename;
 static uint8_t *data;
 static size_t n;
 
 int version;
 
-static bool
-all_ascii(const uint8_t *p, size_t n)
-{
-  for (size_t i = 0; i < n; i++)
-    if (p[i] < 32 || p[i] > 126)
-      return false;
-  return true;
-}
-
-static size_t
-try_find(const char *target, size_t target_len)
-{
-  const uint8_t *pos = (const uint8_t *) memmem (data, n, target, target_len);
-  return pos ? pos - data : 0;
-}
-
-static size_t
-find(const char *target, size_t target_len)
-{
-  size_t pos = try_find(target, target_len);
-  if (!pos)
-    {
-      fprintf (stderr, "not found\n");
-      exit(1);
-    }
-  return pos;
-}
-
 size_t pos;
 
 #define XSTR(x) #x
@@ -63,6 +39,15 @@ get_u32(void)
   return x;
 }
 
+static unsigned long long int
+get_u64(void)
+{
+  uint64_t x;
+  memcpy(&x, &data[pos], 8);
+  pos += 8;
+  return x;
+}
+
 static unsigned int
 get_be32(void)
 {
@@ -120,6 +105,27 @@ match_u32_assert(uint32_t x, const char *where)
 }
 #define match_u32_assert(x) match_u32_assert(x, WHERE)
 
+static bool __attribute__((unused))
+match_u64(uint64_t x)
+{
+  if (get_u64() == x)
+    return true;
+  pos -= 8;
+  return false;
+}
+
+static void __attribute__((unused))
+match_u64_assert(uint64_t x, const char *where)
+{
+  unsigned long long int y = get_u64();
+  if (x != y)
+    {
+      fprintf(stderr, "%s: 0x%x: expected u64:%llu, got u64:%llu\n", where, pos - 8, x, y);
+      exit(1);
+    }
+}
+#define match_u64_assert(x) match_u64_assert(x, WHERE)
+
 static bool __attribute__((unused))
 match_be32(uint32_t x)
 {
@@ -164,73 +170,13 @@ match_byte_assert(uint8_t b, const char *where)
 }
 #define match_byte_assert(b) match_byte_assert(b, WHERE)
 
-static void
-newline(FILE *stream, int pos)
-{
-  fprintf(stream, "\n%08x: ", pos);
-}
-
-static void
-dump_raw(FILE *stream, int start, int end)
+static bool
+get_bool(void)
 {
-  for (size_t i = start; i < end; )
-    {
-      if (i + 5 <= n
-          && data[i]
-          //&& !data[i + 1]
-          && !data[i + 2]
-          && !data[i + 3]
-          && i + 4 + data[i] + data[i + 1] * 256 <= end
-          && all_ascii(&data[i + 4], data[i] + data[i + 1] * 256))
-        {
-          newline(stream, i);
-          fprintf(stream, "\"");
-          fwrite(&data[i + 4], 1, data[i] + data[i + 1] * 256, stream);
-          fputs("\" ", stream);
-
-          i += 4 + data[i] + data[i + 1] * 256;
-        }
-      else if (i + 12 <= end
-               && data[i + 1] == 40
-               && data[i + 2] == 5
-               && data[i + 3] == 0)
-        {
-          double d;
-
-          memcpy (&d, &data[i + 4], 8);
-          fprintf (stream, "F40.%d(%.*f)", data[i], data[i], d);
-          i += 12;
-          newline (stream, i);
-        }
-      else if (i + 12 <= end
-               && data[i + 1] == 40
-               && data[i + 2] == 31
-               && data[i + 3] == 0)
-        {
-          double d;
-
-          memcpy (&d, &data[i + 4], 8);
-          fprintf (stream, "PCT40.%d(%.*f)", data[i], data[i], d);
-          i += 12;
-          newline(stream, i);
-        }
-      else if (i + 4 <= end
-               && (data[i] && data[i] != 88 && data[i] != 0x41)
-               && !data[i + 1]
-               && !data[i + 2]
-               && !data[i + 3])
-        {
-          fprintf (stream, "i%d ", data[i]);
-          i += 4;
-        }
-      else
-        {
-          fprintf(stream, "%02x ", data[i]);
-          i++;
-        }
-    }
-
-
+  if (match_byte(0))
+    return false;
+  match_byte_assert(1);
+  return true;
 }
 
 static bool __attribute__((unused))
@@ -303,50 +249,51 @@ get_end(void)
 }
 
 static void __attribute__((unused))
-hex_dump(int ofs, int n)
+hex_dump(FILE *stream, int ofs, int n)
 {
   for (int i = 0; i < n; i++)
     {
       int c = data[ofs + i];
 #if 1
       if (i && !(i % 16))
-        fprintf(stderr, "-");
+        putc('-', stream);
       else
-        fprintf(stderr, " ");
+        putc(' ', stream);
 #endif
-      fprintf(stderr, "%02x", c);
+      fprintf(stream, "%02x", c);
     }
   for (int i = 0; i < n; i++)
     {
       int c = data[ofs + i];
-      fprintf(stderr, "%c", c >= 32 && c < 127 ? c : '.');
+      putc(c >= 32 && c < 127 ? c : '.', stream);
     }
-  fprintf(stderr, "\n");
+  putc('\n', stream);
 }
 
 static char *
 dump_counted_string(void)
 {
-  char *s = NULL;
   int inner_end = get_end();
+  if (pos == inner_end)
+    return NULL;
+
+  if (match_u32(5))
+    {
+      match_u32_assert(0);
+      match_byte_assert(0x58);
+    }
+  else
+    match_u32_assert(0);
+
+  char *s = NULL;
+  if (match_byte(0x31))
+    s = get_string();
+  else
+    match_byte_assert(0x58);
   if (pos != inner_end)
     {
-      if (match_u32(5))
-        {
-          match_u32_assert(0);
-          match_byte_assert(0x58);
-        }
-      else
-        match_u32_assert(0);
-      if (match_byte(0x31))
-        s = get_string();
-      else
-        match_byte_assert(0x58);
-      if (pos != inner_end)
-        {
-          fprintf(stderr, "inner end discrepancy\n");
-          exit(1);
-        }
+      fprintf(stderr, "inner end discrepancy\n");
+      exit(1);
     }
   return s;
 }
@@ -354,21 +301,55 @@ dump_counted_string(void)
 static void
 dump_style(FILE *stream)
 {
-  match_byte(1);
-  match_byte(0);
-  match_byte(0);
-  match_byte(0);
-  match_byte_assert(1);
+  if (match_byte(0x58))
+    return;
+
+  match_byte_assert(0x31);
+  if (get_bool())
+    printf (" bold=\"yes\"");
+  if (get_bool())
+    printf (" italic=\"yes\"");
+  if (get_bool())
+    printf (" underline=\"yes\"");
+  if (!get_bool())
+    printf (" show=\"no\"");
   char *fg = get_string();     /* foreground */
   char *bg = get_string();     /* background */
   char *font = get_string();     /* font */
-  int size = data[pos];
-  if (!match_byte(14))
-    match_byte_assert(12); /* size? */
-  fprintf(stream, " fgcolor=\"%s\" bgcolor=\"%s\" font=\"%s\" size=\"%d\"",
+  int size = get_byte() * (72. / 96.);
+  fprintf(stream, " fgcolor=\"%s\" bgcolor=\"%s\" font=\"%s\" size=\"%dpt\"",
           fg, bg, font, size);
 }
 
+static void
+dump_style2(FILE *stream)
+{
+  if (match_byte(0x58))
+    return;
+
+  match_byte_assert(0x31);
+  uint32_t halign = get_u32();
+  printf (" halign=\"%s\"",
+          halign == 0 ? "center"
+          : halign == 2 ? "left"
+          : halign == 4 ? "right"
+          : halign == 6 ? "decimal"
+          : halign == 0xffffffad ? "mixed"
+          : "<error>");
+  int valign = get_u32();
+  printf (" valign=\"%s\"",
+          valign == 0 ? "center"
+          : valign == 1 ? "top"
+          : valign == 3 ? "bottom"
+          : "<error>");
+  printf (" offset=\"%gpt\"", get_double());
+  int l = get_u16();
+  int r = get_u16();
+  int t = get_u16();
+  int b = get_u16();
+  printf (" margins=\"%d %d %d %d\"", l, r, t, b);
+}
+
 static char *
 dump_nested_string(FILE *stream)
 {
@@ -380,10 +361,7 @@ dump_nested_string(FILE *stream)
   s = dump_counted_string();
   if (s)
     fprintf(stream, " \"%s\"", s);
-  if (match_byte(0x31))
-    dump_style(stream);
-  else
-    match_byte_assert(0x58);
+  dump_style(stream);
   match_byte_assert(0x58);
   if (pos != outer_end)
     {
@@ -441,30 +419,8 @@ dump_value_modifier(FILE *stream)
           if (template)
             fprintf(stream, " template=\"%s\"", template);
 
-          if (match_byte(0x31))
-            dump_style(stream);
-          else
-            match_byte_assert(0x58);
-          if (match_byte(0x31))
-            {
-              /* Only two SPV files have anything like this, so it's hard to
-                 generalize. */
-              match_u32_assert(0);
-              match_u32_assert(0);
-              match_u32_assert(0);
-              match_u32_assert(0);
-              match_byte_assert(1);
-              match_byte_assert(0);
-              if (!match_byte(8) && !match_byte(1))
-                match_byte_assert(2);
-              match_byte_assert(0);
-              match_byte_assert(8);
-              match_byte_assert(0);
-              match_byte_assert(10);
-              match_byte_assert(0);
-            }
-          else
-            match_byte_assert(0x58);
+          dump_style(stream);
+          dump_style2(stream);
           if (pos != outer_end)
             {
               fprintf(stderr, "outer end discrepancy\n");
@@ -552,6 +508,7 @@ dump_value(FILE *stream, int level)
   for (int i = 0; i <= level; i++)
     fprintf (stream, "    ");
 
+  printf ("%02x: value (%d)\n", pos, data[pos]);
   if (match_byte (1))
     {
       unsigned int format;
@@ -633,6 +590,7 @@ dump_value(FILE *stream, int level)
     }
   else
     {
+      printf ("else %#x\n", pos);
       dump_value_modifier(stream);
 
       char *base = get_string();
@@ -694,15 +652,9 @@ dump_category(FILE *stream, int level, int **indexes, int *allocated_indexes,
   printf ("<category>\n");
   dump_value (stream, level + 1);
 
-  int merge = data[pos];
-  if (!match_byte(0))
-    match_byte_assert (1);
-
+  bool merge = get_bool();
   match_byte_assert (0);
-
-  int unindexed = data[pos];
-  if (!match_byte(0))
-    match_byte_assert (1);
+  int unindexed = get_bool();
 
   int x = get_u32 ();
   pos -= 4;
@@ -719,24 +671,14 @@ dump_category(FILE *stream, int level, int **indexes, int *allocated_indexes,
             fprintf (stream, "    ");
           fprintf (stream, "<merge/>\n");
         }
+      assert (unindexed);
     }
   else
     {
-      if (merge)
-        {
-          fprintf(stderr, "index not -1 but merged\n");
-          exit(1);
-        }
-      if (x != 2)
-        {
-          fprintf(stderr, "index not -1 but x != 2\n");
-          exit(1);
-        }
-      if (n_categories != 0)
-        {
-          fprintf(stderr, "index not -1 but subcategories\n");
-          exit(1);
-        }
+      assert (!merge);
+      assert (!unindexed);
+      assert (x == 2);
+      assert (n_categories == 0);
       if (*n_indexes >= *allocated_indexes)
         {
           *allocated_indexes = *allocated_indexes ? 2 * *allocated_indexes : 16;
@@ -745,14 +687,6 @@ dump_category(FILE *stream, int level, int **indexes, int *allocated_indexes,
       (*indexes)[(*n_indexes)++] = indx;
     }
 
-  int expected_unindexed = indx == -1;
-  if (unindexed != expected_unindexed)
-    {
-      fprintf(stderr, "unindexed (%d) mismatch with indx (%d)\n",
-              unindexed, indx);
-      exit(1);
-    }
-
   if (n_categories == 0)
     {
       for (int i = 0; i <= level + 1; i++)
@@ -774,20 +708,29 @@ dump_dim(int indx)
   printf ("<dimension index=\"%d\">\n", indx);
   dump_value (stdout, 0);
 
-  /* This byte is usually 0 but many other values have been spotted. */
+  /* This byte is usually 0 but many other values have been spotted.
+     No visible effect. */
   pos++;
 
+  /* This byte can cause data to be oddly replicated. */
   if (!match_byte(0) && !match_byte(1))
     match_byte_assert(2);
+
   if (!match_u32(0))
     match_u32_assert(2);
-  if (!match_byte(0))
-    match_byte_assert(1);
-  if (!match_byte(0))
-    match_byte_assert(1);
+
+  bool show_dim_label = get_bool();
+  if (show_dim_label)
+    printf("  <show-dim-label/>\n");
+
+  bool hide_all_labels = get_bool();
+  if (hide_all_labels)
+    printf("  <hide-all-labels/>\n");
+
   match_byte_assert(1);
   if (!match_u32(UINT32_MAX))
     match_u32_assert(indx);
+
   n_categories = get_u32();
 
   int *indexes = NULL;
@@ -908,7 +851,19 @@ dump_title(void)
         dump_value(stdout, 0);
       else
         match_byte_assert (0x58);
-      get_u32 ();
+      int n = get_u32();
+      if (n >= 0)
+        {
+          /* Appears to be the number of references to a footnote. */
+          printf ("  <references n=\"%d\"/>\n", n);
+        }
+      else if (n == -2)
+        {
+          /* The user deleted the footnote references. */
+          printf ("  <deleted/>\n");
+        }
+      else
+        assert(0);
       printf ("</footnote>\n");
     }
 }
@@ -937,27 +892,34 @@ dump_fonts(void)
         printf(" underline=\"true\"");
 
       int halign = get_u32();
-      printf("\nhalign=%d\n", halign);
+      printf(" halign=%d", halign);
 
       int valign = get_u32();
-      printf("\nvalign=%d\n", valign);
+      printf(" valign=%d", valign);
 
       printf (" fgcolor=\"%s\"", get_string());
       printf (" bgcolor=\"%s\"", get_string());
 
       if (!match_byte(0))
         match_byte_assert(1);
-      match_u32_assert(0);
-      char *othercolor = get_string();
-      if (othercolor[0])
-        printf(" othercolor=\"%s\"", othercolor);
+
+      char *alt_fgcolor = get_string();
+      if (alt_fgcolor[0])
+        printf (" altfg=\"%s\"", alt_fgcolor);
+      char *alt_bgcolor = get_string();
+      if (alt_bgcolor[0])
+        printf (" altbg=\"%s\"", alt_bgcolor);
 
       if (version > 1)
         {
-          printf("\nfonts:");
+          printf(" margins=\"");
           for (int i = 0; i < 4; i++)
-            printf(" %2d", get_u32());
-          printf("\n");
+            {
+              if (i)
+                putchar(' ');
+              printf("%d", get_u32());
+            }
+          putchar('"');
         }
 
       printf ("/>\n");
@@ -986,7 +948,7 @@ dump_fonts(void)
     }
   bool grid = get_byte();
   pos += 3;
-  printf("  <grid show=\"%s\">\n", grid ? "yes" : "no");
+  printf("  <grid show=\"%s\"/>\n", grid ? "yes" : "no");
   printf("</borders>\n");
   assert(pos == x1_end);
 
@@ -1001,17 +963,26 @@ dump_fonts(void)
       match_be32_assert(1);
       get_be32();
       printf("<settings layer=\"%d\"", get_be32());
-      if (!get_byte())
+      if (!get_bool())
         printf(" skipempty=\"false\"");
-      if (!get_byte())
+      if (!get_bool())
         printf(" showdimensionincorner=\"false\"");
-      if (!get_byte())
+      if (!get_bool())
         printf(" markers=\"numeric\"");
-      if (!get_byte())
+      if (!get_bool())
         printf(" footnoteposition=\"subscript\"");
       get_byte();
-      pos += get_be32();
-      get_string_be();
+      int nbytes = get_be32();
+      int end = pos + nbytes;
+      printf("\n");
+      while (pos + 4 <= end)
+        printf(" %d", get_be32());
+      pos = end;
+      printf("\n");
+      pos += nbytes;
+      char *notes = get_string_be();
+      if (notes[0])
+        printf(" notes=\"%s\"", notes);
       char *look = get_string_be();
       if (look[0])
         printf(" look=\"%s\"", look);
@@ -1019,16 +990,28 @@ dump_fonts(void)
     }
   pos = x3_end;
 
+  /* Manual column widths, if present. */
   int count = get_u32();
-  pos += 4 * count;
+  if (count > 0)
+    {
+      printf("<columnwidths>");
+      for (int i = 0; i < count; i++)
+        {
+          if (i)
+            putchar(' ');
+          printf("%d", get_u32());
+        }
+      printf("</columnwidths>\n");
+    }
 
   const char *locale = get_string();
   printf ("<locale>%s</locale>\n", locale);
 
-  get_u32();            /* Seen: 0, UINT32_MAX, 2, 3, 4, 5, 6, 8, 9, 21, 24. */
+  printf ("<layer>%d</layer>\n", get_u32());
+  if (!match_byte(0))
+    match_byte_assert(1);
   if (!match_byte(0))
     match_byte_assert(1);
-  match_byte_assert(0);
   if (!match_byte(0))
     match_byte_assert(1);
   printf("<epoch>%d</epoch>\n", get_u32());
@@ -1046,9 +1029,9 @@ dump_fonts(void)
       if (!match_byte('.') && !match_byte(' ') && !match_byte(','))
         match_byte_assert(0);
     }
-  printf("<format decimal=\"%c\" grouping=\"", decimal);
+  printf("<format decimal=\"%c\"", decimal);
   if (grouping)
-    putchar(grouping);
+    printf(" grouping=\"%c\"", grouping);
   printf("\"/>\n");
   if (match_u32(5))
     {
@@ -1061,12 +1044,63 @@ dump_fonts(void)
   /* The last chunk is an outer envelope that contains two inner envelopes.
      The second inner envelope has some interesting data like the encoding and
      the locale. */
+  int outer_end = get_end();
   if (version == 3)
     {
-      int outer_end = get_end();
-
       /* First inner envelope: byte*33 int[n] int*[n]. */
+      int inner_len = get_u32();
+      int inner_end = pos + inner_len;
+      int array_start = pos + 33;
+      match_byte_assert(0);
+      pos++;                    /* 0, 1, 10 seen. */
+      get_bool();
+
+      /* 0=en 1=de 2=es 3=it 5=ko 6=pl 8=zh-tw 10=pt_BR 11=fr */
+      printf("lang=%d ", get_byte());
+
+      printf ("variable_mode=%d\n", get_byte());
+      printf ("value_mode=%d\n", get_byte());
+      if (!match_u64(0))
+        match_u64_assert(UINT64_MAX);
+      match_u32_assert(0);
+      match_u32_assert(0);
+      match_u32_assert(0);
+      match_u32_assert(0);
+      match_byte_assert(0);
+      get_bool();
+      match_byte_assert(1);
+      pos = array_start;
+
+      assert(get_end() == inner_end);
+      printf("<heights>");
+      int n_heights = get_u32();
+      for (int i = 0; i < n_heights; i++)
+        {
+          if (i)
+            putchar(' ');
+          printf("%d", get_u32());
+        }
+      printf("</heights>\n");
+
+      int n_style_map = get_u32();
+      for (int i = 0; i < n_style_map; i++)
+        {
+          uint64_t cell = get_u64();
+          int style = get_u16();
+          printf("<style-map cell=\"%llu\" style=\"%d\"/>\n", cell, style);
+        }
+
+      int n_styles = get_u32();
+      for (int i = 0; i < n_styles; i++)
+        {
+          printf("<cell-style index=\"%d\"", i);
+          dump_style(stdout);
+          dump_style2(stdout);
+          printf("/>\n");
+        }
+
       pos = get_end();
+      assert(pos == inner_end);
 
       /* Second inner envelope. */
       assert(get_end() == outer_end);
@@ -1080,18 +1114,15 @@ dump_fonts(void)
       match_byte_assert(0);
 
       printf("<command>%s</command>\n", get_string());
-      printf("<subcommand>%s</subcommand>\n", get_string());
+      printf("<command-local>%s</command-local>\n", get_string());
       printf("<language>%s</language>\n", get_string());
       printf("<charset>%s</charset>\n", get_string());
       printf("<locale>%s</locale>\n", get_string());
 
-      if (!match_byte(0))
-        match_byte_assert(1);
-      match_byte_assert(0);
-      if (!match_byte(0))
-        match_byte_assert(1);
-      if (!match_byte(0))
-        match_byte_assert(1);
+      get_bool();
+      get_bool();
+      get_bool();
+      get_bool();
 
       printf("<epoch2>%d</epoch2>\n", get_u32());
 
@@ -1107,9 +1138,9 @@ dump_fonts(void)
             match_byte_assert(0);
         }
 
-      pos += 8;
-      match_byte_assert(1);
+      printf ("small: %g\n", get_double());
 
+      match_byte_assert(1);
       if (outer_end - pos > 6)
         {
           /* There might be a pair of strings representing a dataset and
@@ -1129,7 +1160,13 @@ dump_fonts(void)
               printf("<datafile>%s</datafile>\n", get_string());
 
               match_u32_assert(0);
-              get_u32();
+
+              time_t date = get_u32();
+              struct tm tm = *localtime(&date);
+              char s[128];
+              strftime(s, sizeof s, "%a, %d %b %Y %H:%M:%S %z", &tm);
+              printf("<date>%s</date>\n", s);
+
               match_u32_assert(0);
             }
         }
@@ -1142,37 +1179,84 @@ dump_fonts(void)
       else
         match_u32_assert(0);
 
-      match_byte_assert(0x2e);
-      if (!match_byte(0))
-        match_byte_assert(1);
+      match_byte_assert('.');
+      get_bool();
 
       if (pos < outer_end)
         {
-          printf("<seed>%d</seed>\n", get_u32());
+          get_u32();
           match_u32_assert(0);
         }
       assert(pos == outer_end);
 
       pos = outer_end;
     }
-  else
+  else if (outer_end != pos)
     {
-      pos = get_end();
+      pos += 14;
+      printf("<command>%s</command>\n", get_string());
+      printf("<command-local>%s</command-local>\n", get_string());
+      printf("<language>%s</command>\n", get_string());
+      printf("<charset>%s</charset>\n", get_string());
+      printf("<locale>%s</locale>\n", get_string());
+      get_bool();
+      match_byte_assert(0);
+      get_bool();
+      get_bool();
+
+      printf("<epoch2>%d</epoch2>\n", get_u32());
+      int decimal = data[pos];
+      int grouping = data[pos + 1];
+      if (match_byte('.'))
+        {
+          if (!match_byte(',') && !match_byte('\''))
+            match_byte_assert(' ');
+        }
+      else
+        {
+          match_byte_assert(',');
+          if (!match_byte('.') && !match_byte(' ') && !match_byte(','))
+            match_byte_assert(0);
+        }
+      printf("<format decimal=\"%c\"", decimal);
+      if (grouping)
+        printf(" grouping=\"%c\"", grouping);
+      printf("\"/>\n");
+      if (match_u32(5))
+        {
+          for (int i = 0; i < 5; i++)
+            printf("<CC%c>%s</CC%c>\n", 'A' + i, get_string(), 'A' + i);
+        }
+      else
+        match_u32_assert(0);
+
+      match_byte_assert('.');
+      get_bool();
+
+      assert(pos == outer_end);
+      pos = outer_end;
     }
 }
 
 int
 main(int argc, char *argv[])
 {
-  size_t start;
-  struct stat s;
+  if (argc != 2)
+    {
+      fprintf (stderr, "usage: %s FILE.bin", argv[0]);
+      exit (1);
+    }
 
-  if (isatty(STDIN_FILENO))
+  filename = argv[1];
+  int fd = open(filename, O_RDONLY);
+  if (fd < 0)
     {
-      fprintf(stderr, "redirect stdin from a .bin file\n");
-      exit(1);
+      fprintf (stderr, "%s: open failed (%s)", filename, strerror (errno));
+      exit (1);
     }
-  if (fstat(STDIN_FILENO, &s))
+
+  struct stat s;
+  if (fstat(fd, &s))
     {
       perror("fstat");
       exit(1);
@@ -1184,144 +1268,56 @@ main(int argc, char *argv[])
       perror("malloc");
       exit(1);
     }
-  if (read(STDIN_FILENO, data, n) != n)
+  if (read(fd, data, n) != n)
     {
       perror("read");
       exit(1);
     }
+  close(fd);
 
-  if (argc != 2)
-    {
-      fprintf (stderr, "usage: %s TYPE < .bin", argv[0]);
-      exit (1);
-    }
-
-  if (!strcmp(argv[1], "title0"))
-    {
-      pos = 0x27;
-      if (match_byte (0x03)
-          || (match_byte (0x05) && match_byte (0x58)))
-        printf ("%s\n", get_string());
-      else
-        printf ("<unknown>\n");
-      return 0;
-    }
-  else if (!strcmp(argv[1], "title"))
-    {
-      pos = 0x27;
-      dump_title();
-      exit(0);
-    }
-  else if (!strcmp(argv[1], "titleraw"))
-    {
-      const char fonts[] = "\x01\x31\x09\0\0\0SansSerif";
-      start = 0x27;
-      n = find(fonts, sizeof fonts - 1);
-    }
-  else if (!strcmp(argv[1], "fonts"))
-    {
-      const char fonts[] = "\x01\x31\x09\0\0\0SansSerif";
-      const char styles[] = "\xf0\0\0\0";
-      start = find(fonts, sizeof fonts - 1);
-      n = find(styles, sizeof styles - 1);
-    }
-  else if (!strcmp(argv[1], "styles"))
-    {
-      const char styles[] = "\xf0\0\0\0";
-      const char dimensions[] = "-,,,.\0";
-      start = find(styles, sizeof styles - 1);
-      n = find(dimensions, sizeof dimensions - 1) + sizeof dimensions - 1;
-    }
-  else if (!strcmp(argv[1], "dimensions") || !strcmp(argv[1], "all"))
-    {
-      pos = 0;
-      match_byte_assert(1);
-      match_byte_assert(0);
-
-      /* This might be a version number of some kind, because value 1 seems
-         to only appear in an SPV file that also required its own weird
-         special cases in dump_value_modifier(). */
-      version = get_u32();
-      pos -= 4;
-      if (!match_u32(1))
-        match_u32_assert(3);
-
-      match_byte_assert(1);
-      if (!match_byte(0))
-        match_byte_assert(1);
-
-      /* Offset 8. */
-      match_byte_assert(0);
-      if (!match_byte(0))
-        match_byte_assert(1);
-      if (!match_byte(0))
-        match_byte_assert(1);
-
-      /* Offset 11. */
-      pos++;
-      match_byte_assert(0);
-      match_byte_assert(0);
-      match_byte_assert(0);
-
-      /* Offset 15. */
-      pos++;
-      if (!match_byte(0))
-        match_byte_assert(1);
-      match_byte_assert(0);
-      match_byte_assert(0);
-
-      /* Offset 19. */
-      pos++;
-      if (!match_byte(0))
-        match_byte_assert(1);
-      match_byte_assert(0);
-      match_byte_assert(0);
-
-      /* Offset 23. */
-      pos++;
-      if (!match_byte(0))
-        match_byte_assert(1);
-      match_byte_assert(0);
-      match_byte_assert(0);
-
-      /* Offset 27. */
-      pos++;
-      pos++;
-      pos++;
-      pos++;
-
-      /* Offset 31.
-
-         This is the tableId, e.g. -4154297861994971133 would be 0xdca00003.
-         We don't have enough context to validate it. */
-      pos += 4;
-
-      /* Offset 35. */
-      pos += 4;
+  pos = 0;
+  match_byte_assert(1);
+  match_byte_assert(0);
 
-      dump_title ();
-      dump_fonts();
-      dump_dims ();
-      dump_data ();
-      match_byte (1);
-      if (pos != n)
-        {
-          fprintf (stderr, "%x / %x\n", pos, n);
-          exit(1);
-        }
-      exit(0);
-    }
-  else if (!strcmp(argv[1], "raw"))
-    {
-      start = 0x27;
+  version = get_u32();
+  assert(version == 1 || version == 3);
 
-      dump_raw(stdout, start, n);
-    }
-  else
+  match_byte_assert(1);
+  bool number_footnotes = get_bool();
+  printf("<footnote markers=\"%s\"/>\n",
+         number_footnotes ? "number" : "letter");
+  bool rotate_inner_column_labels = get_bool();
+  bool rotate_outer_row_labels = get_bool();
+  printf("x=%d\n", get_bool());
+  printf("<rotate-labels inner-column=\"%s\" outer-row=\"%s\"/>",
+         rotate_inner_column_labels ? "yes" : "no",
+         rotate_outer_row_labels ? "yes" : "no");
+  //fprintf(stderr, "option-number=%d\n", get_u32());
+  get_u32();
+
+  int min_col_width = get_u32();
+  int max_col_width = get_u32();
+  int min_row_width = get_u32();
+  int max_row_width = get_u32();
+  printf("<label-width min-col=\"%d\" max-col=\"%d\" min-row=\"%d\" "
+         "max-row=\"%d\"/>\n",
+         min_col_width, max_col_width,
+         min_row_width, max_row_width);
+
+  /* Offset 31. */
+  printf("<tableid>%lld</tableid>", get_u64());
+
+  dump_title ();
+  dump_fonts();
+  dump_dims ();
+  dump_data ();
+  match_byte (1);
+  if (pos != n)
     {
-      fprintf (stderr, "unknown section %s\n", argv[1]);
+      fprintf (stderr, "%x / %x\n", pos, n);
       exit(1);
     }
+  exit(0);
 
   return 0;
 }