annotate src/zlib-1.2.7/contrib/blast/blast.c @ 143:e95e00bdc3eb

Further win32 build updates
author Chris Cannam <cannam@all-day-breakfast.com>
date Mon, 09 Jan 2017 13:51:38 +0000
parents 8a15ff55d9af
children
rev   line source
cannam@89 1 /* blast.c
cannam@89 2 * Copyright (C) 2003 Mark Adler
cannam@89 3 * For conditions of distribution and use, see copyright notice in blast.h
cannam@89 4 * version 1.1, 16 Feb 2003
cannam@89 5 *
cannam@89 6 * blast.c decompresses data compressed by the PKWare Compression Library.
cannam@89 7 * This function provides functionality similar to the explode() function of
cannam@89 8 * the PKWare library, hence the name "blast".
cannam@89 9 *
cannam@89 10 * This decompressor is based on the excellent format description provided by
cannam@89 11 * Ben Rudiak-Gould in comp.compression on August 13, 2001. Interestingly, the
cannam@89 12 * example Ben provided in the post is incorrect. The distance 110001 should
cannam@89 13 * instead be 111000. When corrected, the example byte stream becomes:
cannam@89 14 *
cannam@89 15 * 00 04 82 24 25 8f 80 7f
cannam@89 16 *
cannam@89 17 * which decompresses to "AIAIAIAIAIAIA" (without the quotes).
cannam@89 18 */
cannam@89 19
cannam@89 20 /*
cannam@89 21 * Change history:
cannam@89 22 *
cannam@89 23 * 1.0 12 Feb 2003 - First version
cannam@89 24 * 1.1 16 Feb 2003 - Fixed distance check for > 4 GB uncompressed data
cannam@89 25 */
cannam@89 26
cannam@89 27 #include <setjmp.h> /* for setjmp(), longjmp(), and jmp_buf */
cannam@89 28 #include "blast.h" /* prototype for blast() */
cannam@89 29
cannam@89 30 #define local static /* for local function definitions */
cannam@89 31 #define MAXBITS 13 /* maximum code length */
cannam@89 32 #define MAXWIN 4096 /* maximum window size */
cannam@89 33
cannam@89 34 /* input and output state */
cannam@89 35 struct state {
cannam@89 36 /* input state */
cannam@89 37 blast_in infun; /* input function provided by user */
cannam@89 38 void *inhow; /* opaque information passed to infun() */
cannam@89 39 unsigned char *in; /* next input location */
cannam@89 40 unsigned left; /* available input at in */
cannam@89 41 int bitbuf; /* bit buffer */
cannam@89 42 int bitcnt; /* number of bits in bit buffer */
cannam@89 43
cannam@89 44 /* input limit error return state for bits() and decode() */
cannam@89 45 jmp_buf env;
cannam@89 46
cannam@89 47 /* output state */
cannam@89 48 blast_out outfun; /* output function provided by user */
cannam@89 49 void *outhow; /* opaque information passed to outfun() */
cannam@89 50 unsigned next; /* index of next write location in out[] */
cannam@89 51 int first; /* true to check distances (for first 4K) */
cannam@89 52 unsigned char out[MAXWIN]; /* output buffer and sliding window */
cannam@89 53 };
cannam@89 54
cannam@89 55 /*
cannam@89 56 * Return need bits from the input stream. This always leaves less than
cannam@89 57 * eight bits in the buffer. bits() works properly for need == 0.
cannam@89 58 *
cannam@89 59 * Format notes:
cannam@89 60 *
cannam@89 61 * - Bits are stored in bytes from the least significant bit to the most
cannam@89 62 * significant bit. Therefore bits are dropped from the bottom of the bit
cannam@89 63 * buffer, using shift right, and new bytes are appended to the top of the
cannam@89 64 * bit buffer, using shift left.
cannam@89 65 */
cannam@89 66 local int bits(struct state *s, int need)
cannam@89 67 {
cannam@89 68 int val; /* bit accumulator */
cannam@89 69
cannam@89 70 /* load at least need bits into val */
cannam@89 71 val = s->bitbuf;
cannam@89 72 while (s->bitcnt < need) {
cannam@89 73 if (s->left == 0) {
cannam@89 74 s->left = s->infun(s->inhow, &(s->in));
cannam@89 75 if (s->left == 0) longjmp(s->env, 1); /* out of input */
cannam@89 76 }
cannam@89 77 val |= (int)(*(s->in)++) << s->bitcnt; /* load eight bits */
cannam@89 78 s->left--;
cannam@89 79 s->bitcnt += 8;
cannam@89 80 }
cannam@89 81
cannam@89 82 /* drop need bits and update buffer, always zero to seven bits left */
cannam@89 83 s->bitbuf = val >> need;
cannam@89 84 s->bitcnt -= need;
cannam@89 85
cannam@89 86 /* return need bits, zeroing the bits above that */
cannam@89 87 return val & ((1 << need) - 1);
cannam@89 88 }
cannam@89 89
cannam@89 90 /*
cannam@89 91 * Huffman code decoding tables. count[1..MAXBITS] is the number of symbols of
cannam@89 92 * each length, which for a canonical code are stepped through in order.
cannam@89 93 * symbol[] are the symbol values in canonical order, where the number of
cannam@89 94 * entries is the sum of the counts in count[]. The decoding process can be
cannam@89 95 * seen in the function decode() below.
cannam@89 96 */
cannam@89 97 struct huffman {
cannam@89 98 short *count; /* number of symbols of each length */
cannam@89 99 short *symbol; /* canonically ordered symbols */
cannam@89 100 };
cannam@89 101
cannam@89 102 /*
cannam@89 103 * Decode a code from the stream s using huffman table h. Return the symbol or
cannam@89 104 * a negative value if there is an error. If all of the lengths are zero, i.e.
cannam@89 105 * an empty code, or if the code is incomplete and an invalid code is received,
cannam@89 106 * then -9 is returned after reading MAXBITS bits.
cannam@89 107 *
cannam@89 108 * Format notes:
cannam@89 109 *
cannam@89 110 * - The codes as stored in the compressed data are bit-reversed relative to
cannam@89 111 * a simple integer ordering of codes of the same lengths. Hence below the
cannam@89 112 * bits are pulled from the compressed data one at a time and used to
cannam@89 113 * build the code value reversed from what is in the stream in order to
cannam@89 114 * permit simple integer comparisons for decoding.
cannam@89 115 *
cannam@89 116 * - The first code for the shortest length is all ones. Subsequent codes of
cannam@89 117 * the same length are simply integer decrements of the previous code. When
cannam@89 118 * moving up a length, a one bit is appended to the code. For a complete
cannam@89 119 * code, the last code of the longest length will be all zeros. To support
cannam@89 120 * this ordering, the bits pulled during decoding are inverted to apply the
cannam@89 121 * more "natural" ordering starting with all zeros and incrementing.
cannam@89 122 */
cannam@89 123 local int decode(struct state *s, struct huffman *h)
cannam@89 124 {
cannam@89 125 int len; /* current number of bits in code */
cannam@89 126 int code; /* len bits being decoded */
cannam@89 127 int first; /* first code of length len */
cannam@89 128 int count; /* number of codes of length len */
cannam@89 129 int index; /* index of first code of length len in symbol table */
cannam@89 130 int bitbuf; /* bits from stream */
cannam@89 131 int left; /* bits left in next or left to process */
cannam@89 132 short *next; /* next number of codes */
cannam@89 133
cannam@89 134 bitbuf = s->bitbuf;
cannam@89 135 left = s->bitcnt;
cannam@89 136 code = first = index = 0;
cannam@89 137 len = 1;
cannam@89 138 next = h->count + 1;
cannam@89 139 while (1) {
cannam@89 140 while (left--) {
cannam@89 141 code |= (bitbuf & 1) ^ 1; /* invert code */
cannam@89 142 bitbuf >>= 1;
cannam@89 143 count = *next++;
cannam@89 144 if (code < first + count) { /* if length len, return symbol */
cannam@89 145 s->bitbuf = bitbuf;
cannam@89 146 s->bitcnt = (s->bitcnt - len) & 7;
cannam@89 147 return h->symbol[index + (code - first)];
cannam@89 148 }
cannam@89 149 index += count; /* else update for next length */
cannam@89 150 first += count;
cannam@89 151 first <<= 1;
cannam@89 152 code <<= 1;
cannam@89 153 len++;
cannam@89 154 }
cannam@89 155 left = (MAXBITS+1) - len;
cannam@89 156 if (left == 0) break;
cannam@89 157 if (s->left == 0) {
cannam@89 158 s->left = s->infun(s->inhow, &(s->in));
cannam@89 159 if (s->left == 0) longjmp(s->env, 1); /* out of input */
cannam@89 160 }
cannam@89 161 bitbuf = *(s->in)++;
cannam@89 162 s->left--;
cannam@89 163 if (left > 8) left = 8;
cannam@89 164 }
cannam@89 165 return -9; /* ran out of codes */
cannam@89 166 }
cannam@89 167
cannam@89 168 /*
cannam@89 169 * Given a list of repeated code lengths rep[0..n-1], where each byte is a
cannam@89 170 * count (high four bits + 1) and a code length (low four bits), generate the
cannam@89 171 * list of code lengths. This compaction reduces the size of the object code.
cannam@89 172 * Then given the list of code lengths length[0..n-1] representing a canonical
cannam@89 173 * Huffman code for n symbols, construct the tables required to decode those
cannam@89 174 * codes. Those tables are the number of codes of each length, and the symbols
cannam@89 175 * sorted by length, retaining their original order within each length. The
cannam@89 176 * return value is zero for a complete code set, negative for an over-
cannam@89 177 * subscribed code set, and positive for an incomplete code set. The tables
cannam@89 178 * can be used if the return value is zero or positive, but they cannot be used
cannam@89 179 * if the return value is negative. If the return value is zero, it is not
cannam@89 180 * possible for decode() using that table to return an error--any stream of
cannam@89 181 * enough bits will resolve to a symbol. If the return value is positive, then
cannam@89 182 * it is possible for decode() using that table to return an error for received
cannam@89 183 * codes past the end of the incomplete lengths.
cannam@89 184 */
cannam@89 185 local int construct(struct huffman *h, const unsigned char *rep, int n)
cannam@89 186 {
cannam@89 187 int symbol; /* current symbol when stepping through length[] */
cannam@89 188 int len; /* current length when stepping through h->count[] */
cannam@89 189 int left; /* number of possible codes left of current length */
cannam@89 190 short offs[MAXBITS+1]; /* offsets in symbol table for each length */
cannam@89 191 short length[256]; /* code lengths */
cannam@89 192
cannam@89 193 /* convert compact repeat counts into symbol bit length list */
cannam@89 194 symbol = 0;
cannam@89 195 do {
cannam@89 196 len = *rep++;
cannam@89 197 left = (len >> 4) + 1;
cannam@89 198 len &= 15;
cannam@89 199 do {
cannam@89 200 length[symbol++] = len;
cannam@89 201 } while (--left);
cannam@89 202 } while (--n);
cannam@89 203 n = symbol;
cannam@89 204
cannam@89 205 /* count number of codes of each length */
cannam@89 206 for (len = 0; len <= MAXBITS; len++)
cannam@89 207 h->count[len] = 0;
cannam@89 208 for (symbol = 0; symbol < n; symbol++)
cannam@89 209 (h->count[length[symbol]])++; /* assumes lengths are within bounds */
cannam@89 210 if (h->count[0] == n) /* no codes! */
cannam@89 211 return 0; /* complete, but decode() will fail */
cannam@89 212
cannam@89 213 /* check for an over-subscribed or incomplete set of lengths */
cannam@89 214 left = 1; /* one possible code of zero length */
cannam@89 215 for (len = 1; len <= MAXBITS; len++) {
cannam@89 216 left <<= 1; /* one more bit, double codes left */
cannam@89 217 left -= h->count[len]; /* deduct count from possible codes */
cannam@89 218 if (left < 0) return left; /* over-subscribed--return negative */
cannam@89 219 } /* left > 0 means incomplete */
cannam@89 220
cannam@89 221 /* generate offsets into symbol table for each length for sorting */
cannam@89 222 offs[1] = 0;
cannam@89 223 for (len = 1; len < MAXBITS; len++)
cannam@89 224 offs[len + 1] = offs[len] + h->count[len];
cannam@89 225
cannam@89 226 /*
cannam@89 227 * put symbols in table sorted by length, by symbol order within each
cannam@89 228 * length
cannam@89 229 */
cannam@89 230 for (symbol = 0; symbol < n; symbol++)
cannam@89 231 if (length[symbol] != 0)
cannam@89 232 h->symbol[offs[length[symbol]]++] = symbol;
cannam@89 233
cannam@89 234 /* return zero for complete set, positive for incomplete set */
cannam@89 235 return left;
cannam@89 236 }
cannam@89 237
cannam@89 238 /*
cannam@89 239 * Decode PKWare Compression Library stream.
cannam@89 240 *
cannam@89 241 * Format notes:
cannam@89 242 *
cannam@89 243 * - First byte is 0 if literals are uncoded or 1 if they are coded. Second
cannam@89 244 * byte is 4, 5, or 6 for the number of extra bits in the distance code.
cannam@89 245 * This is the base-2 logarithm of the dictionary size minus six.
cannam@89 246 *
cannam@89 247 * - Compressed data is a combination of literals and length/distance pairs
cannam@89 248 * terminated by an end code. Literals are either Huffman coded or
cannam@89 249 * uncoded bytes. A length/distance pair is a coded length followed by a
cannam@89 250 * coded distance to represent a string that occurs earlier in the
cannam@89 251 * uncompressed data that occurs again at the current location.
cannam@89 252 *
cannam@89 253 * - A bit preceding a literal or length/distance pair indicates which comes
cannam@89 254 * next, 0 for literals, 1 for length/distance.
cannam@89 255 *
cannam@89 256 * - If literals are uncoded, then the next eight bits are the literal, in the
cannam@89 257 * normal bit order in th stream, i.e. no bit-reversal is needed. Similarly,
cannam@89 258 * no bit reversal is needed for either the length extra bits or the distance
cannam@89 259 * extra bits.
cannam@89 260 *
cannam@89 261 * - Literal bytes are simply written to the output. A length/distance pair is
cannam@89 262 * an instruction to copy previously uncompressed bytes to the output. The
cannam@89 263 * copy is from distance bytes back in the output stream, copying for length
cannam@89 264 * bytes.
cannam@89 265 *
cannam@89 266 * - Distances pointing before the beginning of the output data are not
cannam@89 267 * permitted.
cannam@89 268 *
cannam@89 269 * - Overlapped copies, where the length is greater than the distance, are
cannam@89 270 * allowed and common. For example, a distance of one and a length of 518
cannam@89 271 * simply copies the last byte 518 times. A distance of four and a length of
cannam@89 272 * twelve copies the last four bytes three times. A simple forward copy
cannam@89 273 * ignoring whether the length is greater than the distance or not implements
cannam@89 274 * this correctly.
cannam@89 275 */
cannam@89 276 local int decomp(struct state *s)
cannam@89 277 {
cannam@89 278 int lit; /* true if literals are coded */
cannam@89 279 int dict; /* log2(dictionary size) - 6 */
cannam@89 280 int symbol; /* decoded symbol, extra bits for distance */
cannam@89 281 int len; /* length for copy */
cannam@89 282 int dist; /* distance for copy */
cannam@89 283 int copy; /* copy counter */
cannam@89 284 unsigned char *from, *to; /* copy pointers */
cannam@89 285 static int virgin = 1; /* build tables once */
cannam@89 286 static short litcnt[MAXBITS+1], litsym[256]; /* litcode memory */
cannam@89 287 static short lencnt[MAXBITS+1], lensym[16]; /* lencode memory */
cannam@89 288 static short distcnt[MAXBITS+1], distsym[64]; /* distcode memory */
cannam@89 289 static struct huffman litcode = {litcnt, litsym}; /* length code */
cannam@89 290 static struct huffman lencode = {lencnt, lensym}; /* length code */
cannam@89 291 static struct huffman distcode = {distcnt, distsym};/* distance code */
cannam@89 292 /* bit lengths of literal codes */
cannam@89 293 static const unsigned char litlen[] = {
cannam@89 294 11, 124, 8, 7, 28, 7, 188, 13, 76, 4, 10, 8, 12, 10, 12, 10, 8, 23, 8,
cannam@89 295 9, 7, 6, 7, 8, 7, 6, 55, 8, 23, 24, 12, 11, 7, 9, 11, 12, 6, 7, 22, 5,
cannam@89 296 7, 24, 6, 11, 9, 6, 7, 22, 7, 11, 38, 7, 9, 8, 25, 11, 8, 11, 9, 12,
cannam@89 297 8, 12, 5, 38, 5, 38, 5, 11, 7, 5, 6, 21, 6, 10, 53, 8, 7, 24, 10, 27,
cannam@89 298 44, 253, 253, 253, 252, 252, 252, 13, 12, 45, 12, 45, 12, 61, 12, 45,
cannam@89 299 44, 173};
cannam@89 300 /* bit lengths of length codes 0..15 */
cannam@89 301 static const unsigned char lenlen[] = {2, 35, 36, 53, 38, 23};
cannam@89 302 /* bit lengths of distance codes 0..63 */
cannam@89 303 static const unsigned char distlen[] = {2, 20, 53, 230, 247, 151, 248};
cannam@89 304 static const short base[16] = { /* base for length codes */
cannam@89 305 3, 2, 4, 5, 6, 7, 8, 9, 10, 12, 16, 24, 40, 72, 136, 264};
cannam@89 306 static const char extra[16] = { /* extra bits for length codes */
cannam@89 307 0, 0, 0, 0, 0, 0, 0, 0, 1, 2, 3, 4, 5, 6, 7, 8};
cannam@89 308
cannam@89 309 /* set up decoding tables (once--might not be thread-safe) */
cannam@89 310 if (virgin) {
cannam@89 311 construct(&litcode, litlen, sizeof(litlen));
cannam@89 312 construct(&lencode, lenlen, sizeof(lenlen));
cannam@89 313 construct(&distcode, distlen, sizeof(distlen));
cannam@89 314 virgin = 0;
cannam@89 315 }
cannam@89 316
cannam@89 317 /* read header */
cannam@89 318 lit = bits(s, 8);
cannam@89 319 if (lit > 1) return -1;
cannam@89 320 dict = bits(s, 8);
cannam@89 321 if (dict < 4 || dict > 6) return -2;
cannam@89 322
cannam@89 323 /* decode literals and length/distance pairs */
cannam@89 324 do {
cannam@89 325 if (bits(s, 1)) {
cannam@89 326 /* get length */
cannam@89 327 symbol = decode(s, &lencode);
cannam@89 328 len = base[symbol] + bits(s, extra[symbol]);
cannam@89 329 if (len == 519) break; /* end code */
cannam@89 330
cannam@89 331 /* get distance */
cannam@89 332 symbol = len == 2 ? 2 : dict;
cannam@89 333 dist = decode(s, &distcode) << symbol;
cannam@89 334 dist += bits(s, symbol);
cannam@89 335 dist++;
cannam@89 336 if (s->first && dist > s->next)
cannam@89 337 return -3; /* distance too far back */
cannam@89 338
cannam@89 339 /* copy length bytes from distance bytes back */
cannam@89 340 do {
cannam@89 341 to = s->out + s->next;
cannam@89 342 from = to - dist;
cannam@89 343 copy = MAXWIN;
cannam@89 344 if (s->next < dist) {
cannam@89 345 from += copy;
cannam@89 346 copy = dist;
cannam@89 347 }
cannam@89 348 copy -= s->next;
cannam@89 349 if (copy > len) copy = len;
cannam@89 350 len -= copy;
cannam@89 351 s->next += copy;
cannam@89 352 do {
cannam@89 353 *to++ = *from++;
cannam@89 354 } while (--copy);
cannam@89 355 if (s->next == MAXWIN) {
cannam@89 356 if (s->outfun(s->outhow, s->out, s->next)) return 1;
cannam@89 357 s->next = 0;
cannam@89 358 s->first = 0;
cannam@89 359 }
cannam@89 360 } while (len != 0);
cannam@89 361 }
cannam@89 362 else {
cannam@89 363 /* get literal and write it */
cannam@89 364 symbol = lit ? decode(s, &litcode) : bits(s, 8);
cannam@89 365 s->out[s->next++] = symbol;
cannam@89 366 if (s->next == MAXWIN) {
cannam@89 367 if (s->outfun(s->outhow, s->out, s->next)) return 1;
cannam@89 368 s->next = 0;
cannam@89 369 s->first = 0;
cannam@89 370 }
cannam@89 371 }
cannam@89 372 } while (1);
cannam@89 373 return 0;
cannam@89 374 }
cannam@89 375
cannam@89 376 /* See comments in blast.h */
cannam@89 377 int blast(blast_in infun, void *inhow, blast_out outfun, void *outhow)
cannam@89 378 {
cannam@89 379 struct state s; /* input/output state */
cannam@89 380 int err; /* return value */
cannam@89 381
cannam@89 382 /* initialize input state */
cannam@89 383 s.infun = infun;
cannam@89 384 s.inhow = inhow;
cannam@89 385 s.left = 0;
cannam@89 386 s.bitbuf = 0;
cannam@89 387 s.bitcnt = 0;
cannam@89 388
cannam@89 389 /* initialize output state */
cannam@89 390 s.outfun = outfun;
cannam@89 391 s.outhow = outhow;
cannam@89 392 s.next = 0;
cannam@89 393 s.first = 1;
cannam@89 394
cannam@89 395 /* return if bits() or decode() tries to read past available input */
cannam@89 396 if (setjmp(s.env) != 0) /* if came back here via longjmp(), */
cannam@89 397 err = 2; /* then skip decomp(), return error */
cannam@89 398 else
cannam@89 399 err = decomp(&s); /* decompress */
cannam@89 400
cannam@89 401 /* write any leftover output and update the error code if needed */
cannam@89 402 if (err != 1 && s.next && s.outfun(s.outhow, s.out, s.next) && err == 0)
cannam@89 403 err = 1;
cannam@89 404 return err;
cannam@89 405 }
cannam@89 406
cannam@89 407 #ifdef TEST
cannam@89 408 /* Example of how to use blast() */
cannam@89 409 #include <stdio.h>
cannam@89 410 #include <stdlib.h>
cannam@89 411
cannam@89 412 #define CHUNK 16384
cannam@89 413
cannam@89 414 local unsigned inf(void *how, unsigned char **buf)
cannam@89 415 {
cannam@89 416 static unsigned char hold[CHUNK];
cannam@89 417
cannam@89 418 *buf = hold;
cannam@89 419 return fread(hold, 1, CHUNK, (FILE *)how);
cannam@89 420 }
cannam@89 421
cannam@89 422 local int outf(void *how, unsigned char *buf, unsigned len)
cannam@89 423 {
cannam@89 424 return fwrite(buf, 1, len, (FILE *)how) != len;
cannam@89 425 }
cannam@89 426
cannam@89 427 /* Decompress a PKWare Compression Library stream from stdin to stdout */
cannam@89 428 int main(void)
cannam@89 429 {
cannam@89 430 int ret, n;
cannam@89 431
cannam@89 432 /* decompress to stdout */
cannam@89 433 ret = blast(inf, stdin, outf, stdout);
cannam@89 434 if (ret != 0) fprintf(stderr, "blast error: %d\n", ret);
cannam@89 435
cannam@89 436 /* see if there are any leftover bytes */
cannam@89 437 n = 0;
cannam@89 438 while (getchar() != EOF) n++;
cannam@89 439 if (n) fprintf(stderr, "blast warning: %d unused bytes of input\n", n);
cannam@89 440
cannam@89 441 /* return blast() error code */
cannam@89 442 return ret;
cannam@89 443 }
cannam@89 444 #endif