/* Slightly modified from its original form so as not to exit the * program on errors. The resulting file remains in the public * domain for all to use. */ /* --- GZIP file format uncompression routines --- */ /* The following routines (notably the unzip()) function below * uncompress gzipped data. They are terribly slow at the task, but * it is presumed that they work reasonably well. They don't do any * error checking, but they're probably not too vulnerable to buggy * data either. Another important limitation (but it would be pretty * easy to get around) is that the data must reside in memory, it is * not read as a stream. They have been very little tested. Anyway, * whatever these functions are good for, I put them in the public * domain. -- David Madore 1999/11/21 */ #include "rockmacros.h" static unsigned int peek_bits (const unsigned char *data, long p, int q) /* Read q bits starting from bit p from the data pointed to by * data. Data is in little-endian format. */ { unsigned int answer; int cnt; /* Number of bits already placed in answer */ char ob, lb; /* Offset and length of bit field within current byte */ answer = 0; for ( cnt=0 ; cnt q ) lb = q-cnt; answer |= ((unsigned int)((data[(p+cnt)/8]>>ob)&((1U<>(maxbits-size_table[j])) == code_table[j] ) ) { *p += size_table[j]; return j; } } return -1; } /* I don't know what these should be. The rfc1951 doesn't seem to say * (it only mentions them in the last paragraph of section 3.2.1). 15 * is almost certainly safe, and it is the largest I can put given the * constraints on the size of integers in the C standard. */ #define CLEN_MAXBITS 15 #define HLIT_MAXBITS 15 #define HDIST_MAXBITS 15 /* The magical table sizes... */ #define CLEN_TSIZE 19 #define HLIT_TSIZE 288 #define HDIST_TSIZE 30 static int get_tables (const unsigned char *data, long *p, char hlit_size_table[HLIT_TSIZE], unsigned int hlit_code_table[HLIT_TSIZE], char hdist_size_table[HDIST_TSIZE], unsigned int hdist_code_table[HDIST_TSIZE]) /* Fill the Huffman tables (first the code lengths table, and * then, using it, the literal/length table and the distance * table). See section 3.2.7 of rfc1951 for details. */ { char hlit, hdist, hclen; const int clen_weird_tangle[CLEN_TSIZE] = { 16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15 }; char clen_size_table[CLEN_TSIZE]; unsigned int clen_code_table[CLEN_TSIZE]; int j; unsigned int b; int remainder; /* See note at end of section 3.2.7 of rfc1951. */ char rem_val; hlit = read_bits (data, p, 5); hdist = read_bits (data, p, 5); hclen = read_bits (data, p, 4); for ( j=0 ; j<4+hclen ; j++ ) clen_size_table[clen_weird_tangle[j]] = read_bits (data, p, 3); for ( ; j= 257 ) /* Back reference */ { unsigned int bb; unsigned int length, dist; unsigned int l; switch ( b ) { case 257: length = 3; break; case 258: length = 4; break; case 259: length = 5; break; case 260: length = 6; break; case 261: length = 7; break; case 262: length = 8; break; case 263: length = 9; break; case 264: length = 10; break; case 265: length = 11 + read_bits (data, p, 1); break; case 266: length = 13 + read_bits (data, p, 1); break; case 267: length = 15 + read_bits (data, p, 1); break; case 268: length = 17 + read_bits (data, p, 1); break; case 269: length = 19 + read_bits (data, p, 2); break; case 270: length = 23 + read_bits (data, p, 2); break; case 271: length = 27 + read_bits (data, p, 2); break; case 272: length = 31 + read_bits (data, p, 2); break; case 273: length = 35 + read_bits (data, p, 3); break; case 274: length = 43 + read_bits (data, p, 3); break; case 275: length = 51 + read_bits (data, p, 3); break; case 276: length = 59 + read_bits (data, p, 3); break; case 277: length = 67 + read_bits (data, p, 4); break; case 278: length = 83 + read_bits (data, p, 4); break; case 279: length = 99 + read_bits (data, p, 4); break; case 280: length = 115 + read_bits (data, p, 4); break; case 281: length = 131 + read_bits (data, p, 5); break; case 282: length = 163 + read_bits (data, p, 5); break; case 283: length = 195 + read_bits (data, p, 5); break; case 284: length = 227 + read_bits (data, p, 5); break; case 285: length = 258; break; default: return -1; } bb = decode_one (data, p, hdist_size_table, HDIST_TSIZE, hdist_code_table, HDIST_MAXBITS); switch ( bb ) { case 0: dist = 1; break; case 1: dist = 2; break; case 2: dist = 3; break; case 3: dist = 4; break; case 4: dist = 5 + read_bits (data, p, 1); break; case 5: dist = 7 + read_bits (data, p, 1); break; case 6: dist = 9 + read_bits (data, p, 2); break; case 7: dist = 13 + read_bits (data, p, 2); break; case 8: dist = 17 + read_bits (data, p, 3); break; case 9: dist = 25 + read_bits (data, p, 3); break; case 10: dist = 33 + read_bits (data, p, 4); break; case 11: dist = 49 + read_bits (data, p, 4); break; case 12: dist = 65 + read_bits (data, p, 5); break; case 13: dist = 97 + read_bits (data, p, 5); break; case 14: dist = 129 + read_bits (data, p, 6); break; case 15: dist = 193 + read_bits (data, p, 6); break; case 16: dist = 257 + read_bits (data, p, 7); break; case 17: dist = 385 + read_bits (data, p, 7); break; case 18: dist = 513 + read_bits (data, p, 8); break; case 19: dist = 769 + read_bits (data, p, 8); break; case 20: dist = 1025 + read_bits (data, p, 9); break; case 21: dist = 1537 + read_bits (data, p, 9); break; case 22: dist = 2049 + read_bits (data, p, 10); break; case 23: dist = 3073 + read_bits (data, p, 10); break; case 24: dist = 4097 + read_bits (data, p, 11); break; case 25: dist = 6145 + read_bits (data, p, 11); break; case 26: dist = 8193 + read_bits (data, p, 12); break; case 27: dist = 12289 + read_bits (data, p, 12); break; case 28: dist = 16385 + read_bits (data, p, 13); break; case 29: dist = 24577 + read_bits (data, p, 13); break; default: return -1; } for ( l=0 ; l