diff options
author | Dirk Engling <erdgeist@erdgeist.org> | 2015-04-27 02:14:11 +0200 |
---|---|---|
committer | Dirk Engling <erdgeist@erdgeist.org> | 2015-04-27 02:14:11 +0200 |
commit | 09764f5e271ae64358c8f4f262735d7eb4922ee8 (patch) | |
tree | 7ee827dea074c519fdead94c5bd14edd896da0f7 | |
parent | 1bcb181505b544c336b9cc7774822a71789b0dc4 (diff) |
experimental code to extract white_1995_Q0 and white_1996_Q0
-rw-r--r-- | src/export/extract_version_15.c | 404 |
1 files changed, 404 insertions, 0 deletions
diff --git a/src/export/extract_version_15.c b/src/export/extract_version_15.c new file mode 100644 index 0000000..68b05af --- /dev/null +++ b/src/export/extract_version_15.c | |||
@@ -0,0 +1,404 @@ | |||
1 | /* | ||
2 | This decompressor uses modified code from zlib. Below is it's original copyright | ||
3 | notice. | ||
4 | |||
5 | Copyright (C) 2003, 2012 Mark Adler | ||
6 | version 1.2, 24 Oct 2012 | ||
7 | |||
8 | This software is provided 'as-is', without any express or implied | ||
9 | warranty. In no event will the author be held liable for any damages | ||
10 | arising from the use of this software. | ||
11 | |||
12 | Permission is granted to anyone to use this software for any purpose, | ||
13 | including commercial applications, and to alter it and redistribute it | ||
14 | freely, subject to the following restrictions: | ||
15 | |||
16 | 1. The origin of this software must not be misrepresented; you must not | ||
17 | claim that you wrote the original software. If you use this software | ||
18 | in a product, an acknowledgment in the product documentation would be | ||
19 | appreciated but is not required. | ||
20 | 2. Altered source versions must be plainly marked as such, and must not be | ||
21 | misrepresented as being the original software. | ||
22 | 3. This notice may not be removed or altered from any source distribution. | ||
23 | |||
24 | Mark Adler madler@alumni.caltech.edu | ||
25 | */ | ||
26 | |||
27 | #include <stdlib.h> | ||
28 | #include <stdio.h> | ||
29 | #include <stdint.h> | ||
30 | #include "mystdlib.h" | ||
31 | #include <string.h> | ||
32 | |||
33 | /* | ||
34 | uint8_t table[] = { | ||
35 | 0x00, 0x39, 0x20, 0x15, 0x24, 0x04, 0x61, 0x80, 0xc4, 0xc0, 0x1f, 0xa4, 0x04, 0xca, 0x40, 0xb4, | ||
36 | 0x08, 0x19, 0xe7, 0x03, 0xab, 0xe0, 0x83, 0xac, 0x12, 0x92, 0x82, 0x95, 0x10, 0x5b, 0x24, 0x0c, | ||
37 | 0x75, 0x81, 0xaf, 0xe0, 0x3a, 0x24, 0x07, 0xc7, 0xc1, 0x09, 0x88, 0x23, 0x55, 0x84, 0xac, 0x50, | ||
38 | 0x9d, 0xc6, 0x14, 0xc3, 0x82, 0xb9, 0x30, 0x5b, 0x54, 0x0b, 0xf1, 0x81, 0x8e, 0xbc, 0x33, 0xce, | ||
39 | 0x86, 0xb9, 0xa0, 0xdf, 0xae, 0x1d, 0x04, 0x03, 0xc0, 0xf8, 0x7c, 0x13, 0x0f, 0xf0, 0x22, 0x0b, | ||
40 | 0xa8, 0x43, 0x3e, 0x88, 0xa0, 0xd1, 0x1b, 0x48, 0x24, 0x61, 0x84, 0xad, 0xb0, 0x99, 0xe0, 0x13, | ||
41 | 0xc0, 0x62, 0x87, 0xdc, 0x52, 0xba, 0x8a, 0x8f, 0x31, 0x59, 0x0e, 0x2c, 0x16, 0x85, 0xa0, 0x38, | ||
42 | 0xb7, 0x8a, 0x17, 0x66, 0xc2, 0xfb, 0x50, 0x61, 0x3f, 0x8c, 0x5f, 0x01, 0x93, 0x76, 0x33, 0x6f, | ||
43 | 0x46, 0x8e, 0x18, 0xd5, 0x6f, 0x1b, 0x1e, 0x83, 0x71, 0x6c, 0x6f, 0xfc, 0x0e, 0x3d, 0x91, 0xcf, | ||
44 | 0x08, 0x3a, 0xc4, 0x87, 0x75, 0x80, 0xf2, 0x23, 0x1e, 0xb4, 0x83, 0xe4, 0xe0, 0x7e, 0x6d, 0x10, | ||
45 | 0x06, 0x22, 0x08, 0x5a, 0x41, 0xf8, 0x48, 0x5b, 0x11, 0x0e, 0xce, 0x22, 0x46, 0x44, 0x56, 0x84, | ||
46 | 0x8c, 0xc9, 0x91, 0xdc, 0x22, 0x44, 0x26, 0x49, 0x9f, 0x49, 0x56, 0x71, 0x2f, 0x18, 0x26, 0x6d, | ||
47 | 0xa4, 0xde, 0xdc, 0x9e, 0x02, 0x94, 0x04, 0xc2, 0x89, 0x38, 0x52, 0x31, 0x8a, 0x68, 0x39, 0x51, | ||
48 | 0x64, 0x2a, 0xb0, 0x45, 0x65, 0x80, 0xae, 0xbf, 0x16, 0x19, 0x12, 0xcb, 0x48, 0x5a, 0x6b, 0xcb, | ||
49 | 0x6a, 0x11, 0x70, 0xa3, 0x2e, 0x80, 0x85, 0xdd, 0xf0, 0xbd, 0x7a, 0x97, 0xe5, 0xb3, 0x03, 0xbc, | ||
50 | 0x61, 0x67, 0xcc, 0x49, 0xe1, 0x8d, 0x11, 0x32, 0x28, 0x66, 0x53, 0xd8, 0xcc, 0x2b, 0x19, 0xbd, | ||
51 | 0x93, 0x3e, 0x54, 0x68, 0xae, 0x4d, 0x35, 0xe1, 0xaa, 0xbe, 0x35, 0xd5, 0x46, 0xc8, 0x30, 0xda, | ||
52 | 0xd0, 0x9b, 0x95, 0x73, 0x7a, 0xc8, 0x70, 0x43, 0x4e, 0x23, 0x19, 0xc8, 0x41, 0x39, 0x76, 0x07, | ||
53 | 0x3d, 0xb8, 0xe9, 0x79, 0x9d, 0x65, 0xe3, 0xb3, 0x94, 0x77, 0x47, 0xcf, 0x04, 0xf1, 0xe3, 0xfc, | ||
54 | 0x3c, 0xf0, 0x27, 0xac, 0x30, 0xf7, 0x4b, 0x1f, 0x25, 0x03, 0xec, 0x0a, 0x7e, 0x65, 0xcf, 0xe8, | ||
55 | 0x1a, 0x00, 0xa7, 0x40, 0x8d, 0xc8, 0x21, 0x45, 0x05, 0xf5, 0xa0, 0xf7, 0x34, 0x25, 0xd0, 0x85, | ||
56 | 0x9a, 0x50, 0xcf, 0x02, 0x1d, 0xd2, 0x44, 0x2c, 0x28, 0x93, 0xdd, 0x14, 0x36, 0x22, 0xbf, 0x24, | ||
57 | 0x5e, 0x86, 0x8c, 0xa9, 0x91, 0xb1, 0x8a, 0x39, 0xd3, 0x47, 0xab, 0x29, 0x03, 0x29, 0x22, 0x28, | ||
58 | 0xa4, 0x7b, 0xc4, 0x96, 0x68, 0x93, 0xb9, 0x12, 0x92, 0xfa, 0x55, 0xd2, 0x4b, 0x2d, 0x29, 0x74, | ||
59 | 0x05, 0x30, 0x38, 0xa6, 0x40, 0xd4, 0xcf, 0x6e, 0x9a, 0xcc, 0x13, 0x74, 0xda, 0x72, 0x0a, 0x4e, | ||
60 | 0xb3, 0x29, 0xe4, 0xa9, 0x3e, 0x60, 0xa8, 0x04, 0xa5, 0x07, 0xce, 0xa1, 0xe7, 0xc0 | ||
61 | }; | ||
62 | */ | ||
63 | |||
64 | #define MAXBITS 13 /* maximum code length */ | ||
65 | #define MAXWIN 8192 /* maximum window size */ | ||
66 | |||
67 | struct state { | ||
68 | uint8_t *in; /* next input location */ | ||
69 | uint8_t *out; /* output buffer and sliding window */ | ||
70 | |||
71 | unsigned left; /* available input at in */ | ||
72 | int bitbuf; /* bit buffer */ | ||
73 | int bitcnt; /* number of bits in bit buffer */ | ||
74 | |||
75 | unsigned next; /* index of next write location in out[] */ | ||
76 | int first; /* true to check distances (for first 4K) */ | ||
77 | }; | ||
78 | |||
79 | static int bits(struct state *s, int need) | ||
80 | { | ||
81 | int val; /* bit accumulator */ | ||
82 | |||
83 | /* load at least need bits into val */ | ||
84 | val = s->bitbuf; | ||
85 | while (s->bitcnt < need) { | ||
86 | val |= (int)(*(s->in)++) << s->bitcnt; /* load eight bits */ | ||
87 | s->left--; | ||
88 | s->bitcnt += 8; | ||
89 | } | ||
90 | |||
91 | /* drop need bits and update buffer, always zero to seven bits left */ | ||
92 | s->bitbuf = val >> need; | ||
93 | s->bitcnt -= need; | ||
94 | |||
95 | /* return need bits, zeroing the bits above that */ | ||
96 | return val & ((1 << need) - 1); | ||
97 | } | ||
98 | |||
99 | struct huffman { | ||
100 | short *count; /* number of symbols of each length */ | ||
101 | short *symbol; /* canonically ordered symbols */ | ||
102 | }; | ||
103 | |||
104 | static int decode(struct state *s, struct huffman *h) | ||
105 | { | ||
106 | int len; /* current number of bits in code */ | ||
107 | int code; /* len bits being decoded */ | ||
108 | int first; /* first code of length len */ | ||
109 | int count; /* number of codes of length len */ | ||
110 | int index; /* index of first code of length len in symbol table */ | ||
111 | int bitbuf; /* bits from stream */ | ||
112 | int left; /* bits left in next or left to process */ | ||
113 | short *next; /* next number of codes */ | ||
114 | |||
115 | bitbuf = s->bitbuf; | ||
116 | left = s->bitcnt; | ||
117 | code = first = index = 0; | ||
118 | len = 1; | ||
119 | next = h->count + 1; | ||
120 | while (1) { | ||
121 | while (left--) { | ||
122 | code |= (bitbuf & 1) ^ 1; /* invert code */ | ||
123 | bitbuf >>= 1; | ||
124 | count = *next++; | ||
125 | if (code < first + count) { /* if length len, return symbol */ | ||
126 | s->bitbuf = bitbuf; | ||
127 | s->bitcnt = (s->bitcnt - len) & 7; | ||
128 | return h->symbol[index + (code - first)]; | ||
129 | } | ||
130 | index += count; /* else update for next length */ | ||
131 | first += count; | ||
132 | first <<= 1; | ||
133 | code <<= 1; | ||
134 | len++; | ||
135 | } | ||
136 | left = (MAXBITS+1) - len; | ||
137 | if (left == 0) break; | ||
138 | bitbuf = *(s->in)++; | ||
139 | s->left--; | ||
140 | if (left > 8) left = 8; | ||
141 | } | ||
142 | return -9; /* ran out of codes */ | ||
143 | } | ||
144 | |||
145 | static int construct(struct huffman *h, const unsigned char *rep, int n) | ||
146 | { | ||
147 | int symbol; /* current symbol when stepping through length[] */ | ||
148 | int len; /* current length when stepping through h->count[] */ | ||
149 | int left; /* number of possible codes left of current length */ | ||
150 | short offs[MAXBITS+1]; /* offsets in symbol table for each length */ | ||
151 | short length[256]; /* code lengths */ | ||
152 | |||
153 | /* convert compact repeat counts into symbol bit length list */ | ||
154 | symbol = 0; | ||
155 | do { | ||
156 | len = *rep++; | ||
157 | left = (len >> 4) + 1; | ||
158 | len &= 15; | ||
159 | do { | ||
160 | length[symbol++] = len; | ||
161 | } while (--left); | ||
162 | } while (--n); | ||
163 | n = symbol; | ||
164 | |||
165 | /* count number of codes of each length */ | ||
166 | for (len = 0; len <= MAXBITS; len++) | ||
167 | h->count[len] = 0; | ||
168 | for (symbol = 0; symbol < n; symbol++) | ||
169 | (h->count[length[symbol]])++; /* assumes lengths are within bounds */ | ||
170 | if (h->count[0] == n) /* no codes! */ | ||
171 | return 0; /* complete, but decode() will fail */ | ||
172 | |||
173 | /* check for an over-subscribed or incomplete set of lengths */ | ||
174 | left = 1; /* one possible code of zero length */ | ||
175 | for (len = 1; len <= MAXBITS; len++) { | ||
176 | left <<= 1; /* one more bit, double codes left */ | ||
177 | left -= h->count[len]; /* deduct count from possible codes */ | ||
178 | if (left < 0) return left; /* over-subscribed--return negative */ | ||
179 | } /* left > 0 means incomplete */ | ||
180 | |||
181 | /* generate offsets into symbol table for each length for sorting */ | ||
182 | offs[1] = 0; | ||
183 | for (len = 1; len < MAXBITS; len++) | ||
184 | offs[len + 1] = offs[len] + h->count[len]; | ||
185 | |||
186 | /* | ||
187 | * put symbols in table sorted by length, by symbol order within each | ||
188 | * length | ||
189 | */ | ||
190 | for (symbol = 0; symbol < n; symbol++) | ||
191 | if (length[symbol] != 0) | ||
192 | h->symbol[offs[length[symbol]]++] = symbol; | ||
193 | |||
194 | /* return zero for complete set, positive for incomplete set */ | ||
195 | return left; | ||
196 | } | ||
197 | |||
198 | /* | ||
199 | * Decode PKWare Compression Library stream. | ||
200 | * | ||
201 | * Format notes: | ||
202 | * | ||
203 | * - First byte is 0 if literals are uncoded or 1 if they are coded. Second | ||
204 | * byte is 4, 5, or 6 for the number of extra bits in the distance code. | ||
205 | * This is the base-2 logarithm of the dictionary size minus six. | ||
206 | * | ||
207 | * - Compressed data is a combination of literals and length/distance pairs | ||
208 | * terminated by an end code. Literals are either Huffman coded or | ||
209 | * uncoded bytes. A length/distance pair is a coded length followed by a | ||
210 | * coded distance to represent a string that occurs earlier in the | ||
211 | * uncompressed data that occurs again at the current location. | ||
212 | * | ||
213 | * - A bit preceding a literal or length/distance pair indicates which comes | ||
214 | * next, 0 for literals, 1 for length/distance. | ||
215 | * | ||
216 | * - If literals are uncoded, then the next eight bits are the literal, in the | ||
217 | * normal bit order in th stream, i.e. no bit-reversal is needed. Similarly, | ||
218 | * no bit reversal is needed for either the length extra bits or the distance | ||
219 | * extra bits. | ||
220 | * | ||
221 | * - Literal bytes are simply written to the output. A length/distance pair is | ||
222 | * an instruction to copy previously uncompressed bytes to the output. The | ||
223 | * copy is from distance bytes back in the output stream, copying for length | ||
224 | * bytes. | ||
225 | * | ||
226 | * - Distances pointing before the beginning of the output data are not | ||
227 | * permitted. | ||
228 | * | ||
229 | * - Overlapped copies, where the length is greater than the distance, are | ||
230 | * allowed and common. For example, a distance of one and a length of 518 | ||
231 | * simply copies the last byte 518 times. A distance of four and a length of | ||
232 | * twelve copies the last four bytes three times. A simple forward copy | ||
233 | * ignoring whether the length is greater than the distance or not implements | ||
234 | * this correctly. | ||
235 | */ | ||
236 | static int decomp(struct state *s) | ||
237 | { | ||
238 | int lit; /* true if literals are coded */ | ||
239 | int dict; /* log2(dictionary size) - 6 */ | ||
240 | int symbol; /* decoded symbol, extra bits for distance */ | ||
241 | int len; /* length for copy */ | ||
242 | unsigned dist; /* distance for copy */ | ||
243 | int copy; /* copy counter */ | ||
244 | unsigned char *from, *to; /* copy pointers */ | ||
245 | static int virgin = 1; /* build tables once */ | ||
246 | static short litcnt[MAXBITS+1], litsym[256]; /* litcode memory */ | ||
247 | static short lencnt[MAXBITS+1], lensym[16]; /* lencode memory */ | ||
248 | static short distcnt[MAXBITS+1], distsym[64]; /* distcode memory */ | ||
249 | static struct huffman litcode = {litcnt, litsym}; /* length code */ | ||
250 | static struct huffman lencode = {lencnt, lensym}; /* length code */ | ||
251 | static struct huffman distcode = {distcnt, distsym};/* distance code */ | ||
252 | /* bit lengths of literal codes */ | ||
253 | static const unsigned char litlen[] = { | ||
254 | 11, 124, 8, 7, 28, 7, 188, 13, 76, 4, 10, 8, 12, 10, 12, 10, 8, 23, 8, | ||
255 | 9, 7, 6, 7, 8, 7, 6, 55, 8, 23, 24, 12, 11, 7, 9, 11, 12, 6, 7, 22, 5, | ||
256 | 7, 24, 6, 11, 9, 6, 7, 22, 7, 11, 38, 7, 9, 8, 25, 11, 8, 11, 9, 12, | ||
257 | 8, 12, 5, 38, 5, 38, 5, 11, 7, 5, 6, 21, 6, 10, 53, 8, 7, 24, 10, 27, | ||
258 | 44, 253, 253, 253, 252, 252, 252, 13, 12, 45, 12, 45, 12, 61, 12, 45, | ||
259 | 44, 173}; | ||
260 | /* bit lengths of length codes 0..15 */ | ||
261 | static const unsigned char lenlen[] = {2, 35, 36, 53, 38, 23}; | ||
262 | /* bit lengths of distance codes 0..63 */ | ||
263 | static const unsigned char distlen[] = {2, 20, 53, 230, 247, 151, 248}; | ||
264 | static const short base[16] = { /* base for length codes */ | ||
265 | 3, 2, 4, 5, 6, 7, 8, 9, 10, 12, 16, 24, 40, 72, 136, 264}; | ||
266 | static const char extra[16] = { /* extra bits for length codes */ | ||
267 | 0, 0, 0, 0, 0, 0, 0, 0, 1, 2, 3, 4, 5, 6, 7, 8}; | ||
268 | |||
269 | /* set up decoding tables (once--might not be thread-safe) */ | ||
270 | if (virgin) { | ||
271 | construct(&litcode, litlen, sizeof(litlen)); | ||
272 | construct(&lencode, lenlen, sizeof(lenlen)); | ||
273 | construct(&distcode, distlen, sizeof(distlen)); | ||
274 | virgin = 0; | ||
275 | } | ||
276 | |||
277 | /* read header */ | ||
278 | lit = bits(s, 8); | ||
279 | if (lit > 1) return -1; | ||
280 | dict = bits(s, 8); | ||
281 | if (dict < 4 || dict > 6) return -2; | ||
282 | |||
283 | /* decode literals and length/distance pairs */ | ||
284 | do { | ||
285 | if (bits(s, 1)) { | ||
286 | /* get length */ | ||
287 | symbol = decode(s, &lencode); | ||
288 | len = base[symbol] + bits(s, extra[symbol]); | ||
289 | if (len == 519) break; /* end code */ | ||
290 | |||
291 | /* get distance */ | ||
292 | symbol = len == 2 ? 2 : dict; | ||
293 | dist = decode(s, &distcode) << symbol; | ||
294 | dist += bits(s, symbol); | ||
295 | dist++; | ||
296 | if (s->first && dist > s->next) | ||
297 | return -3; /* distance too far back */ | ||
298 | |||
299 | /* copy length bytes from distance bytes back */ | ||
300 | do { | ||
301 | to = s->out + s->next; | ||
302 | from = to - dist; | ||
303 | copy = MAXWIN; | ||
304 | if (s->next < dist) { | ||
305 | from += copy; | ||
306 | copy = dist; | ||
307 | } | ||
308 | copy -= s->next; | ||
309 | if (copy > len) copy = len; | ||
310 | len -= copy; | ||
311 | s->next += copy; | ||
312 | do { | ||
313 | *to++ = *from++; | ||
314 | } while (--copy); | ||
315 | } while (len != 0); | ||
316 | } | ||
317 | else { | ||
318 | /* get literal and write it */ | ||
319 | symbol = lit ? decode(s, &litcode) : bits(s, 8); | ||
320 | s->out[s->next++] = symbol; | ||
321 | } | ||
322 | } while (1); | ||
323 | return 0; | ||
324 | } | ||
325 | |||
326 | static void decompress_subchunk( uint8_t * subchunk, size_t subchunk_size, int chunks, size_t outchunk_size ) { | ||
327 | uint8_t output[ outchunk_size ]; | ||
328 | struct state s; /* input/output state */ | ||
329 | int err; /* return value */ | ||
330 | |||
331 | memset( output, 0, outchunk_size ); | ||
332 | |||
333 | /* initialize input state */ | ||
334 | s.in = subchunk; | ||
335 | s.left = subchunk_size; | ||
336 | while( chunks-- ) { | ||
337 | int i; | ||
338 | |||
339 | /* (Re-)initialize output state */ | ||
340 | s.out = output; | ||
341 | s.next = 0; | ||
342 | s.first = 1; | ||
343 | s.bitbuf = 0; | ||
344 | s.bitcnt = 0; | ||
345 | |||
346 | err = decomp(&s); | ||
347 | if( err ) { | ||
348 | for( i=0; i<32; ++i ) fprintf( stderr, "%02X ", s.in[i] ); | ||
349 | fprintf( stderr, "\nError: %d\n", err ); | ||
350 | return; | ||
351 | } | ||
352 | |||
353 | /* Dump to stdout for now */ | ||
354 | fwrite( output, outchunk_size, 1, stdout ); | ||
355 | } | ||
356 | } | ||
357 | |||
358 | static void decode_19bit_address( uint8_t const *source, uint32_t *dest, size_t length ) | ||
359 | { | ||
360 | uint32_t acc_bits = 0, acc = 0; | ||
361 | while( 1 ) | ||
362 | { | ||
363 | acc = acc*256+*(source++); acc_bits+=8; | ||
364 | if( acc_bits >= 19 ) { | ||
365 | uint32_t tmp = acc >> (acc_bits-19); | ||
366 | *(dest++) = (tmp & 0x7ffff) << 11; | ||
367 | acc_bits -= 19; | ||
368 | if( !length-- ) return; | ||
369 | } | ||
370 | } | ||
371 | } | ||
372 | |||
373 | int main( int args, char **argv ) { | ||
374 | MAP file; | ||
375 | uint32_t offsets[256]; | ||
376 | uint16_t num_subchunks, subchunk_rest_count, subchunk_one_count; | ||
377 | uint8_t *fp, *subchunk; | ||
378 | int i; | ||
379 | |||
380 | if( args < 2 ) { | ||
381 | fprintf( stderr, "Syntax: %s FILENAME\n", argv[0] ); | ||
382 | exit(1); | ||
383 | } | ||
384 | |||
385 | file = map_file( argv[1], 1 ); | ||
386 | if( !file ) exit( 1 ); | ||
387 | fp = file->addr; | ||
388 | |||
389 | num_subchunks = *(uint16_t*)(fp+0x14); | ||
390 | subchunk_rest_count = *(uint16_t*)(fp+0x1c); | ||
391 | subchunk_one_count = *(uint16_t*)(fp+0x1e); | ||
392 | subchunk = fp + 0x20 + ( 19*num_subchunks +7 )/ 8; | ||
393 | |||
394 | decode_19bit_address ( fp + 0x20, offsets, num_subchunks ); | ||
395 | offsets[num_subchunks] = file->size; | ||
396 | |||
397 | decompress_subchunk( subchunk, offsets[i], subchunk_one_count, MAXWIN ); | ||
398 | |||
399 | for( i=0; i< num_subchunks; ++i ) | ||
400 | if( offsets[i] + 0x800 < file->size ) | ||
401 | decompress_subchunk( fp + offsets[i] + 0x800, offsets[i+1] - offsets[i], subchunk_rest_count, MAXWIN ); | ||
402 | |||
403 | return 0; | ||
404 | } | ||