annotate src/zlib-1.2.8/contrib/blast/blast.c @ 155:54abead6ecce

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