spo: learned things
[pspp] / dump-spo.c
index f2d33a1449c3475250df54c3c02819aab338e149..ac53ece628894626236a24d2c2d9a91537d33b12 100644 (file)
@@ -265,16 +265,21 @@ get_end(void)
 static void __attribute__((unused))
 hex_dump(FILE *stream, int ofs, int n)
 {
+  int n_ascii = 0;
   for (int i = 0; i < n; i++)
     {
       int c = data[ofs + i];
+      n_ascii += is_ascii(c);
       fprintf(stream, " %02x", c);
     }
-  putc(' ', stream);
-  for (int i = 0; i < n; i++)
+  if (n_ascii >= 3)
     {
-      int c = data[ofs + i];
-      putc(c >= 32 && c < 127 ? c : '.', stream);
+      putc(' ', stream);
+      for (int i = 0; i < n; i++)
+        {
+          int c = data[ofs + i];
+          putc(c >= 32 && c < 127 ? c : '.', stream);
+        }
     }
   putc('\n', stream);
 }
@@ -1258,6 +1263,52 @@ dump_fonts(void)
     }
 }
 
+static const char *
+format_name (int format, char *buf)
+{
+  switch (format)
+    {
+    case 1: return "A";
+    case 2: return "AHEX";
+    case 3: return "COMMA";
+    case 4: return "DOLLAR";
+    case 5: return "F";
+    case 6: return "IB";
+    case 7: return "PIBHEX";
+    case 8: return "P";
+    case 9: return "PIB";
+    case 10: return "PK";
+    case 11: return "RB";
+    case 12: return "RBHEX";
+    case 15: return "Z";
+    case 16: return "N";
+    case 17: return "E";
+    case 20: return "DATE";
+    case 21: return "TIME";
+    case 22: return "DATETIME";
+    case 23: return "ADATE";
+    case 24: return "JDATE";
+    case 25: return "DTIME";
+    case 26: return "WKDAY";
+    case 27: return "MONTH";
+    case 28: return "MOYR";
+    case 29: return "QYR";
+    case 30: return "WKYR";
+    case 31: return "PCT";
+    case 32: return "DOT";
+    case 33: return "CCA";
+    case 34: return "CCB";
+    case 35: return "CCC";
+    case 36: return "CCD";
+    case 37: return "CCE";
+    case 38: return "EDATE";
+    case 39: return "SDATE";
+    case 40: return "MTIME";
+    case 41: return "YMDHMS";
+    default: sprintf(buf, "(%d)", format); return buf;
+    }
+}
+
 int
 main(int argc, char *argv[])
 {
@@ -1336,31 +1387,34 @@ main(int argc, char *argv[])
   for (pos = 2; pos + 50 < n; pos++)
     {
       static const int cell_prefix[] = {
-        0x00, 0x03, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00,
-        0x00, 0x00, 0x00, 0x00, 0x00, -1, 0x80, 0x01, -1, -1,
-        -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
+        0x00, 0x03,
+        0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, -1,
+        0x80, 0x01, -1, -1, -1, -1,
       };
       size_t cell_prefix_len = sizeof cell_prefix / sizeof *cell_prefix;
       if (match_bytes(pos, cell_prefix, cell_prefix_len))
         {
           if (prev_end != pos)
             {
-              printf ("%04x ", prev_end);
+              //printf ("%04x ", prev_end);
               hex_dump (stdout, prev_end, pos - prev_end);
             }
 
-          printf ("cell %04x %d %d %d %d %d %d ", pos,
-                  data[pos + 13], data[pos + 16], data[pos + 17],
-                  data[pos + 18], data[pos + 19], data[pos + 20]);
+          char buf[64];
+          printf ("cell %s%d.%d ",
+                  format_name (data[pos + 18], buf),
+                  data[pos + 17],
+                  data[pos + 16]);
 
           int len = cell_prefix_len;
           if (data[pos + 19] == 0)
             {
+              assert (data[pos + 13] == 5);
               if (data[pos + 20] == 0)
                 {
                   int count = (data[pos + 22]);
-                  printf ("/ %d %d %d \"%.*s\"\n",
-                          data[pos + 20], data[pos + 21], data[pos + 22],
+                  printf ("%d %d \"%.*s\"\n",
+                          data[pos + 21], data[pos + 22],
                           count, &data[pos + 23]);
                   len = 23 + count;
                 }
@@ -1369,14 +1423,14 @@ main(int argc, char *argv[])
                        && data[pos + 22] == 0xff)
                 {
                   int count = 255;
-                  printf ("/ \"%.*s\"\n", count, &data[pos + 24]);
+                  printf ("%d \"%.*s\"\n", count, data[pos + 23],
+                          &data[pos + 24]);
                   len = 23 + count;
                 }
-              else if (data[pos + 20] == 1 )
+              else if (data[pos + 20] == 1)
                 {
                   int count = (data[pos + 21]);
-                  printf ("/ %d %d %d \"%.*s\"\n",
-                          data[pos + 20], data[pos + 21], data[pos + 22],
+                  printf ("\"%.*s\"\n",
                           count, &data[pos + 22]);
                   len = 22 + count;
                 }
@@ -1385,7 +1439,11 @@ main(int argc, char *argv[])
             }
           else if (data[pos + 19] == 128)
             {
-              double d = *(double *) &data[pos + cell_prefix_len - 8];
+              /* pos + 13 is usually 22...53 but also 5 or 69 */
+              /* pos + 20 is 2 most of the time, occasionally 1 */
+              printf ("%d %d ", data[pos + 13], data[pos + 20]);
+              double d = *(double *) &data[pos + 21];
+              len = 29;
               const union
                 {
                   uint8_t b[8];
@@ -1412,7 +1470,7 @@ main(int argc, char *argv[])
         {
           if (prev_end != pos)
             {
-              printf ("%04x ", prev_end);
+              //printf ("%04x ", prev_end);
               hex_dump (stdout, prev_end, pos - prev_end);
             }
 
@@ -1422,6 +1480,87 @@ main(int argc, char *argv[])
           continue;
         }
 
+      static const int record_prefix[] = {
+        0xff, 0xff, 0x00, 0x00,
+      };
+      size_t record_prefix_len = sizeof record_prefix / sizeof *record_prefix;
+      if (match_bytes(pos, record_prefix, record_prefix_len))
+        {
+          if (prev_end != pos)
+            {
+              //printf ("%04x ", prev_end);
+              hex_dump (stdout, prev_end, pos - prev_end);
+            }
+
+          putchar ('\n');
+
+          //printf ("%d ", pos % 4);
+          int len = record_prefix_len;
+          int slen = data[pos + 4] + (data[pos + 5] << 8);
+          if (slen > 2 && slen < 256 && all_utf8((char *) &data[pos + 6], slen))
+            {
+              printf ("%.*s ", slen, &data[pos + 6]);
+              len = slen + 6;
+            }
+          else
+            printf ("notitle ");
+
+          pos += len - 1;
+          prev_end = pos + 1;
+          continue;
+        }
+
+      static const int number_prefix[] = {
+        0x80, 0x02
+      };
+      size_t number_prefix_len = sizeof number_prefix / sizeof *number_prefix;
+      if (match_bytes(pos, number_prefix, number_prefix_len))
+        {
+          if (prev_end != pos)
+            {
+              //printf ("%04x ", prev_end);
+              hex_dump (stdout, prev_end, pos - prev_end);
+            }
+          prev_end = pos;
+
+          double d = *(double *) &data[pos + number_prefix_len];
+          printf ("float%f ", d);
+          pos += 10 - 9;
+          prev_end = pos + 1;
+          continue;
+        }
+
+      static const int string_prefix[] = {
+        0x80, 0x01, 0x02, 0x28, 0x05, 0x00, 0x01
+      };
+      size_t string_prefix_len = sizeof string_prefix / sizeof *string_prefix;
+      if (match_bytes(pos, string_prefix, string_prefix_len) && data[pos + string_prefix_len] != 255)
+        {
+          if (prev_end != pos)
+            {
+              //printf ("%04x ", prev_end);
+              hex_dump (stdout, prev_end, pos - prev_end);
+            }
+          prev_end = pos;
+
+          printf ("\nstring %.*s\n", (int) data[pos + 7], &data[pos + 8]);
+        }
+      if (match_bytes(pos, string_prefix, string_prefix_len) && data[pos + string_prefix_len] == 255)
+        {
+          if (prev_end != pos)
+            {
+              //printf ("%04x ", prev_end);
+              hex_dump (stdout, prev_end, pos - prev_end);
+            }
+          prev_end = pos;
+
+          int len = data[pos + 8] + (data[pos + 9] << 8);
+          printf ("\nlongstring %.*s\n", len, &data[pos + 10]);
+        }
+
+      
+
+#if 0
       static const int heading_prefix[] = {
         -1, 0x00, 0x00, 0x00, 0x50, 0x80, 0x00, 0x52, 0x80, 0x00, -1, 0x00,
         0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff, 0x00,
@@ -1433,7 +1572,7 @@ main(int argc, char *argv[])
         {
           if (prev_end != pos)
             {
-              printf ("%04x ", prev_end);
+              //printf ("%04x ", prev_end);
               hex_dump (stdout, prev_end, pos - prev_end);
             }
 
@@ -1457,7 +1596,7 @@ main(int argc, char *argv[])
         {
           if (prev_end != pos)
             {
-              printf ("%04x", prev_end);
+              //printf ("%04x", prev_end);
               hex_dump (stdout, prev_end, pos - prev_end);
             }
 
@@ -1485,7 +1624,7 @@ main(int argc, char *argv[])
         {
           if (prev_end != pos)
             {
-              printf ("%04x", prev_end);
+              //printf ("%04x", prev_end);
               hex_dump (stdout, prev_end, pos - prev_end);
             }
 
@@ -1494,6 +1633,7 @@ main(int argc, char *argv[])
           prev_end = pos + 1;
           continue;
         }
+#endif
 
       static const int dim_prefix[] = {
         0x00, 0x03, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, -1,
@@ -1505,7 +1645,7 @@ main(int argc, char *argv[])
         {
           if (prev_end != pos)
             {
-              printf ("%04x", prev_end);
+              //printf ("%04x", prev_end);
               hex_dump (stdout, prev_end, pos - prev_end);
             }
 
@@ -1525,7 +1665,7 @@ main(int argc, char *argv[])
         {
           if (prev_end != pos)
             {
-              printf ("%04x", prev_end);
+              //printf ("%04x", prev_end);
               hex_dump (stdout, prev_end, pos - prev_end);
             }
 
@@ -1571,10 +1711,11 @@ main(int argc, char *argv[])
       unsigned real_start = start - length_bytes;
       if (prev_end != real_start)
         {
-          printf ("%04x ", prev_end);
+          //printf ("%04x ", prev_end);
           hex_dump (stdout, prev_end, real_start - prev_end);
         }
-      printf ("%04x \"%.*s\"\n", real_start,
+      //printf ("%04x ", real_start);
+      printf ("\"%.*s\"\n", 
               (int) end - start, (char *) &data[start]);
       prev_end = end;
       pos = end - 1;