annotate src/zlib-1.2.7/contrib/blast/blast.c @ 4:e13257ea84a4

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