]> git.tdb.fi Git - ext/subsurface.git/blobdiff - cochran.c
updated/corrected comment
[ext/subsurface.git] / cochran.c
index 907dc765aa03cd9944252cd559457acffa09424c..360f2eb7c9d836a1f77688821f6ad33872442b51 100644 (file)
--- a/cochran.c
+++ b/cochran.c
@@ -1,4 +1,5 @@
 #include <stdio.h>
+#include <string.h>
 #include <unistd.h>
 #include <sys/types.h>
 #include <sys/stat.h>
@@ -7,6 +8,8 @@
 #include "dive.h"
 #include "file.h"
 
+#define DON
+
 /*
  * The Cochran file format is designed to be annoying to read. It's roughly:
  *
@@ -76,24 +79,88 @@ static int figure_out_modulus(const unsigned char *decode, const unsigned char *
        return best;
 }
 
-static void cochran_debug_write(int dive, const unsigned char *data, unsigned size)
+#define hexchar(n) ("0123456789abcdef"[(n)&15])
+
+static int show_line(unsigned offset, const unsigned char *data, unsigned size, int show_empty)
 {
-       char buffer[60];
-       int fd;
-
-       snprintf(buffer, sizeof(buffer), "cochran.%d.out", dive);
-       fd = open(buffer, O_CREAT | O_TRUNC | O_WRONLY, 0666);
-       if (fd >= 0) {
-               write(fd, data, size);
-               close(fd);
+       unsigned char bits;
+       int i, off;
+       char buffer[120];
+
+       if (size > 16)
+               size = 16;
+
+       bits = 0;
+       memset(buffer, ' ', sizeof(buffer));
+       off = sprintf(buffer, "%06x ", offset);
+       for (i = 0; i < size; i++) {
+               char *hex = buffer + off + 3*i;
+               char *asc = buffer + off + 50 + i;
+               unsigned char byte = data[i];
+
+               hex[0] = hexchar(byte>>4);
+               hex[1] = hexchar(byte);
+               bits |= byte;
+               if (byte < 32 || byte > 126)
+                       byte = '.';
+               asc[0] = byte;
+               asc[1] = 0;
+       }
+
+       if (bits) {
+               puts(buffer);
+               return 1;
        }
+       if (show_empty)
+               puts("...");
+       return 0;
 }
 
-static void parse_cochran_dive(int dive, const unsigned char *decode, unsigned mod,
+static void cochran_debug_write(const char *filename, const unsigned char *data, unsigned size)
+{
+       int i, show = 1;
+
+       for (i = 0; i < size; i += 16)
+               show = show_line(i, data + i, size - i, show);
+}
+
+static void parse_cochran_header(const char *filename,
+               const unsigned char *decode, unsigned mod,
                const unsigned char *in, unsigned size)
 {
        char *buf = malloc(size);
 
+       /* Do the "null decode" using a one-byte decode array of '\0' */
+       partial_decode(0    , 0x0b14, "", 0, 1, in, size, buf);
+
+       /*
+        * The header scrambling is different form the dive
+        * scrambling. Oh yay!
+        */
+       partial_decode(0x010e, 0x0b14, decode, 0, mod, in, size, buf);
+       partial_decode(0x0b14, 0x1b14, decode, 0, mod, in, size, buf);
+       partial_decode(0x1b14, 0x2b14, decode, 0, mod, in, size, buf);
+       partial_decode(0x2b14, 0x3b14, decode, 0, mod, in, size, buf);
+       partial_decode(0x3b14, 0x5414, decode, 0, mod, in, size, buf);
+       partial_decode(0x5414,   size, decode, 0, mod, in, size, buf);
+
+       printf("\n%s, header\n\n", filename);
+       cochran_debug_write(filename, buf, size);
+
+       free(buf);
+}
+
+static void parse_cochran_dive(const char *filename, int dive,
+               const unsigned char *decode, unsigned mod,
+               const unsigned char *in, unsigned size)
+{
+       char *buf = malloc(size);
+#ifdef DON
+       unsigned int offset = 0x4a14;
+#else
+       unsigned int offset = 0x4b14;
+#endif
+
        /*
         * The scrambling has odd boundaries. I think the boundaries
         * match some data structure size, but I don't know. They were
@@ -115,9 +182,11 @@ static void parse_cochran_dive(int dive, const unsigned char *decode, unsigned m
         * scrambled, but there seems to be size differences in the data,
         * so this just descrambles part of it:
         */
-       partial_decode(0x48ff, size, decode, 0, mod, in, size, buf);
+       partial_decode(0x48ff, offset, decode, 0, mod, in, size, buf);
+       partial_decode(offset,   size, decode, 0, mod, in, size, buf);
 
-       cochran_debug_write(dive, buf, size);
+       printf("\n%s, dive %d\n\n", filename, dive);
+       cochran_debug_write(filename, buf, size);
 
        free(buf);
 }
@@ -139,6 +208,8 @@ int try_to_open_cochran(const char *filename, struct memblock *mem, GError **err
 
        mod = figure_out_modulus(decode, mem->buffer + dive1, dive2 - dive1);
 
+       parse_cochran_header(filename, decode, mod, mem->buffer + 0x40000, dive1 - 0x40000);
+
        for (i = 0; i < 65534; i++) {
                dive1 = offsets[i];
                dive2 = offsets[i+1];
@@ -146,8 +217,8 @@ int try_to_open_cochran(const char *filename, struct memblock *mem, GError **err
                        break;
                if (dive2 > mem->size)
                        break;
-               parse_cochran_dive(i, decode, mod, mem->buffer + dive1, dive2 - dive1);
+               parse_cochran_dive(filename, i+1, decode, mod, mem->buffer + dive1, dive2 - dive1);
        }
 
-       return 1;
+       exit(0);
 }