12 * The Cochran file format is designed to be annoying to read. It's roughly:
14 * 0x00000: room for 65534 4-byte words, giving the starting offsets
15 * of the dives themselves.
17 * 0x3fff8: the size of the file + 1
18 * 0x3ffff: 0 (high 32 bits of filesize? Bogus: the offsets into the file
19 * are 32-bit, so it can't be a large file anyway)
21 * 0x40000: "block 0": the decoding block. The first byte is some random
22 * value (0x46 in the files I have access to), the next 200+ bytes or so
23 * are the "scrambling array" that needs to be added into the file
24 * contents to make sense of them.
26 * The descrambling array seems to be of some random size which is likely
27 * determinable from the array somehow, the two test files I have it as
28 * 230 bytes and 234 bytes respectively.
30 static unsigned int partial_decode(unsigned int start, unsigned int end,
31 const unsigned char *decode, unsigned offset, unsigned mod,
32 const unsigned char *buf, unsigned int size, unsigned char *dst)
36 for (i = start ; i < end; i++) {
37 unsigned char d = decode[offset++];
51 * The decode buffer size can be figured out by simply trying our the
52 * decode: we expect that the scrambled contents are largely random, and
53 * thus tend to have half the bits set. Summing over the bytes is going
54 * to give an average of 0x80 per byte.
56 * The decoded array is mostly full of zeroes, so the sum is lower.
60 static int figure_out_modulus(const unsigned char *decode, const unsigned char *dive, unsigned int size)
63 unsigned int min = ~0u;
68 for (mod = 50; mod < 300; mod++) {
71 sum = partial_decode(0, 0x0fff, decode, 1, mod, dive, size, NULL);
80 #define hexchar(n) ("0123456789abcdef"[(n)&15])
82 static void show_line(unsigned offset, const unsigned char *data, unsigned size)
92 memset(buffer, ' ', sizeof(buffer));
93 off = sprintf(buffer, "%06x ", offset);
94 for (i = 0; i < size; i++) {
95 char *hex = buffer + off + 3*i;
96 char *asc = buffer + off + 50 + i;
97 unsigned char byte = data[i];
99 hex[0] = hexchar(byte>>4);
100 hex[1] = hexchar(byte);
102 if (byte < 32 || byte > 126)
112 static void cochran_debug_write(const char *filename, int dive, const unsigned char *data, unsigned size)
115 printf("\n%s, dive %d\n\n", filename, dive);
117 for (i = 0; i < size; i += 16) {
118 show_line(i, data + i, size - i);
122 static void parse_cochran_dive(const char *filename, int dive,
123 const unsigned char *decode, unsigned mod,
124 const unsigned char *in, unsigned size)
126 char *buf = malloc(size);
129 * The scrambling has odd boundaries. I think the boundaries
130 * match some data structure size, but I don't know. They were
131 * discovered the same way we dynamically discover the decode
132 * size: automatically looking for least random output.
134 * The boundaries are also this confused "off-by-one" thing,
135 * the same way the file size is off by one. It's as if the
136 * cochran software forgot to write one byte at the beginning.
138 partial_decode(0 , 0x0fff, decode, 1, mod, in, size, buf);
139 partial_decode(0x0fff, 0x1fff, decode, 0, mod, in, size, buf);
140 partial_decode(0x1fff, 0x2fff, decode, 0, mod, in, size, buf);
141 partial_decode(0x2fff, 0x48ff, decode, 0, mod, in, size, buf);
144 * This is not all the descrambling you need - the above are just
145 * what appears to be the fixed-size blocks. The rest is also
146 * scrambled, but there seems to be size differences in the data,
147 * so this just descrambles part of it:
149 partial_decode(0x48ff, size, decode, 0, mod, in, size, buf);
151 cochran_debug_write(filename, dive, buf, size);
156 int try_to_open_cochran(const char *filename, struct memblock *mem, GError **error)
160 unsigned int *offsets, dive1, dive2;
161 unsigned char *decode = mem->buffer + 0x40001;
163 if (mem->size < 0x40000)
165 offsets = mem->buffer;
168 if (dive1 < 0x40000 || dive2 < dive1 || dive2 > mem->size)
171 mod = figure_out_modulus(decode, mem->buffer + dive1, dive2 - dive1);
173 for (i = 0; i < 65534; i++) {
175 dive2 = offsets[i+1];
178 if (dive2 > mem->size)
180 parse_cochran_dive(filename, i, decode, mod, mem->buffer + dive1, dive2 - dive1);