1 /*
2     Copyright (C) 2000 Masanao Izumo <mo@goice.co.jp>
3 
4     This program is free software; you can redistribute it and/or modify
5     it under the terms of the GNU General Public License as published by
6     the Free Software Foundation; either version 2 of the License, or
7     (at your option) any later version.
8 
9     This program is distributed in the hope that it will be useful,
10     but WITHOUT ANY WARRANTY; without even the implied warranty of
11     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12     GNU General Public License for more details.
13 
14     You should have received a copy of the GNU General Public License
15     along with this program; if not, write to the Free Software
16     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17 */
18 
19 /* inflate.c -- Not copyrighted 1992 by Mark Adler
20    version c10p1, 10 January 1993 */
21 
22 /* You can do whatever you like with this source file, though I would
23    prefer that if you modify it and redistribute it that you include
24    comments to that effect with your name and the date.	 Thank you.
25    [The history has been moved to the file ChangeLog.]
26  */
27 
28 /*
29    Inflate deflated (PKZIP's method 8 compressed) data.	 The compression
30    method searches for as much of the current string of bytes (up to a
31    length of 258) in the previous 32K bytes.  If it doesn't find any
32    matches (of at least length 3), it codes the next byte.  Otherwise, it
33    codes the length of the matched string and its distance backwards from
34    the current position.  There is a single Huffman code that codes both
35    single bytes (called "literals") and match lengths.	A second Huffman
36    code codes the distance information, which follows a length code.  Each
37    length or distance code actually represents a base value and a number
38    of "extra" (sometimes zero) bits to get to add to the base value.  At
39    the end of each deflated block is a special end-of-block (EOB) literal/
40    length code.	 The decoding process is basically: get a literal/length
41    code; if EOB then done; if a literal, emit the decoded byte; if a
42    length then get the distance and emit the referred-to bytes from the
43    sliding window of previously emitted data.
44 
45    There are (currently) three kinds of inflate blocks: stored, fixed, and
46    dynamic.  The compressor outputs a chunk of data at a time and decides
47    which method to use on a chunk-by-chunk basis.  A chunk might typically
48    be 32K to 64K, uncompressed.	 If the chunk is uncompressible, then the
49    "stored" method is used.  In this case, the bytes are simply stored as
50    is, eight bits per byte, with none of the above coding.  The bytes are
51    preceded by a count, since there is no longer an EOB code.
52 
53    If the data are compressible, then either the fixed or dynamic methods
54    are used.  In the dynamic method, the compressed data are preceded by
55    an encoding of the literal/length and distance Huffman codes that are
56    to be used to decode this block.  The representation is itself Huffman
57    coded, and so is preceded by a description of that code.  These code
58    descriptions take up a little space, and so for small blocks, there is
59    a predefined set of codes, called the fixed codes.  The fixed method is
60    used if the block ends up smaller that way (usually for quite small
61    chunks); otherwise the dynamic method is used.  In the latter case, the
62    codes are customized to the probabilities in the current block and so
63    can code it much better than the pre-determined fixed codes can.
64 
65    The Huffman codes themselves are decoded using a multi-level table
66    lookup, in order to maximize the speed of decoding plus the speed of
67    building the decoding tables.  See the comments below that precede the
68    lbits and dbits tuning parameters.
69  */
70 
71 
72 /*
73    Notes beyond the 1.93a appnote.txt:
74 
75    1. Distance pointers never point before the beginning of the output
76       stream.
77    2. Distance pointers can point back across blocks, up to 32k away.
78    3. There is an implied maximum of 7 bits for the bit length table and
79       15 bits for the actual data.
80    4. If only one code exists, then it is encoded using one bit.  (Zero
81       would be more efficient, but perhaps a little confusing.)	 If two
82       codes exist, they are coded using one bit each (0 and 1).
83    5. There is no way of sending zero distance codes--a dummy must be
84       sent if there are none.  (History: a pre 2.0 version of PKZIP would
85       store blocks with no distance codes, but this was discovered to be
86       too harsh a criterion.)  Valid only for 1.93a.  2.04c does allow
87       zero distance codes, which is sent as one code of zero bits in
88       length.
89    6. There are up to 286 literal/length codes.	 Code 256 represents the
90       end-of-block.  Note however that the static length tree defines
91       288 codes just to fill out the Huffman codes.  Codes 286 and 287
92       cannot be used though, since there is no length base or extra bits
93       defined for them.	 Similarily, there are up to 30 distance codes.
94       However, static trees define 32 codes (all 5 bits) to fill out the
95       Huffman codes, but the last two had better not show up in the data.
96    7. Unzip can check dynamic Huffman blocks for complete code sets.
97       The exception is that a single code would not be complete (see #4).
98    8. The five bits following the block type is really the number of
99       literal codes sent minus 257.
100    9. Length codes 8,16,16 are interpreted as 13 length codes of 8 bits
101       (1+6+6).	Therefore, to output three times the length, you output
102       three codes (1+1+1), whereas to output four times the same length,
103       you only need two codes (1+3).  Hmm.
104   10. In the tree reconstruction algorithm, Code = Code + Increment
105       only if BitLength(i) is not zero.	 (Pretty obvious.)
106   11. Correction: 4 Bits: # of Bit Length codes - 4	(4 - 19)
107   12. Note: length code 284 can represent 227-258, but length code 285
108       really is 258.  The last length deserves its own, short code
109       since it gets used a lot in very redundant files.	 The length
110       258 is special since 258 - 3 (the min match length) is 255.
111   13. The literal/length and distance code bit lengths are read as a
112       single stream of lengths.	 It is possible (and advantageous) for
113       a repeat code (16, 17, or 18) to go across the boundary between
114       the two sets of lengths.
115  */
116 
117 #include "config.h"
118 #include <stdio.h>
119 #include <stdlib.h>
120 #ifndef NO_STRING_H
121 #include <string.h>
122 #else
123 #include <strings.h>
124 #endif
125 #include "libarc/mblock.h"
126 #include "zip.h"
127 #define local static
128 
129 /* Save to local */
130 #define BITS_SAVE \
131   ulg bit_buf = decoder->bit_buf; \
132   ulg bit_len = decoder->bit_len;
133 
134 /* Restore to decoder */
135 #define BITS_RESTORE \
136   decoder->bit_buf = bit_buf; \
137   decoder->bit_len = bit_len;
138 
139 #define MASK_BITS(n) ((((ulg)1)<<(n))-1)
140 #define GET_BYTE()  (decoder->inptr < decoder->insize ? decoder->inbuf[decoder->inptr++] : fill_inbuf(decoder))
141 #define NEEDBITS(n) {while(bit_len<(n)){bit_buf|=((ulg)GET_BYTE())<<bit_len;bit_len+=8;}}
142 #define GETBITS(n)  (bit_buf & MASK_BITS(n))
143 #define DUMPBITS(n) {bit_buf>>=(n);bit_len-=(n);}
144 
145 /* variables */
146 struct _InflateHandler
147 {
148     void *user_val;
149     long (* read_func)(char *buf, long size, void *user_val);
150 
151     uch slide[2L * WSIZE];
152     uch inbuf[INBUFSIZ + INBUF_EXTRA];
153     unsigned wp;	/* current position in slide */
154     unsigned insize;	/* valid bytes in inbuf */
155     unsigned inptr;	/* index of next byte to be processed in inbuf */
156     struct huft *fixed_tl;	/* inflate static */
157     struct huft *fixed_td;	/* inflate static */
158     int fixed_bl, fixed_bd;	/* inflate static */
159     ulg bit_buf;	/* bit buffer */
160     ulg bit_len;	/* bits in bit buffer */
161     int method;
162     int eof;
163     unsigned copy_leng;
164     unsigned copy_dist;
165     struct huft *tl, *td; /* literal/length and distance decoder tables */
166     int bl, bd;		/* number of bits decoded by tl[] and td[] */
167     MBlockList pool;	/* memory buffer for tl, td */
168 };
169 
170 /* Function prototypes */
171 local int fill_inbuf(InflateHandler);
172 local int huft_free(struct huft *);
173 local long inflate_codes(InflateHandler, char *, long);
174 local long inflate_stored(InflateHandler, char *, long);
175 local long inflate_fixed(InflateHandler, char *, long);
176 local long inflate_dynamic(InflateHandler, char *, long);
177 local void inflate_start(InflateHandler);
178 
179 /* The inflate algorithm uses a sliding 32K byte window on the uncompressed
180    stream to find repeated byte strings.  This is implemented here as a
181    circular buffer.  The index is updated simply by incrementing and then
182    and'ing with 0x7fff (32K-1). */
183 /* It is left to other modules to supply the 32K area.	It is assumed
184    to be usable as if it were declared "uch slide[32768];" or as just
185    "uch *slide;" and then malloc'ed in the latter case.	 The definition
186    must be in unzip.h, included above. */
187 
188 #define lbits 9			/* bits in base literal/length lookup table */
189 #define dbits 6			/* bits in base distance lookup table */
190 
191 /* Tables for deflate from PKZIP's appnote.txt. */
192 local ush cplens[] = {		/* Copy lengths for literal codes 257..285 */
193 	3, 4, 5, 6, 7, 8, 9, 10, 11, 13, 15, 17, 19, 23, 27, 31,
194 	35, 43, 51, 59, 67, 83, 99, 115, 131, 163, 195, 227, 258, 0, 0};
195 	/* note: see note #13 above about the 258 in this list. */
196 local ush cplext[] = {		/* Extra bits for literal codes 257..285 */
197 	0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2,
198 	3, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5, 0, 99, 99}; /* 99==invalid */
199 local ush cpdist[] = {		/* Copy offsets for distance codes 0..29 */
200 	1, 2, 3, 4, 5, 7, 9, 13, 17, 25, 33, 49, 65, 97, 129, 193,
201 	257, 385, 513, 769, 1025, 1537, 2049, 3073, 4097, 6145,
202 	8193, 12289, 16385, 24577};
203 local ush cpdext[] = {		/* Extra bits for distance codes */
204 	0, 0, 0, 0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6,
205 	7, 7, 8, 8, 9, 9, 10, 10, 11, 11,
206 	12, 12, 13, 13};
207 
208 /*
209    Huffman code decoding is performed using a multi-level table lookup.
210    The fastest way to decode is to simply build a lookup table whose
211    size is determined by the longest code.  However, the time it takes
212    to build this table can also be a factor if the data being decoded
213    are not very long.  The most common codes are necessarily the
214    shortest codes, so those codes dominate the decoding time, and hence
215    the speed.  The idea is you can have a shorter table that decodes the
216    shorter, more probable codes, and then point to subsidiary tables for
217    the longer codes.  The time it costs to decode the longer codes is
218    then traded against the time it takes to make longer tables.
219 
220    This results of this trade are in the variables lbits and dbits
221    below.  lbits is the number of bits the first level table for literal/
222    length codes can decode in one step, and dbits is the same thing for
223    the distance codes.	Subsequent tables are also less than or equal to
224    those sizes.	 These values may be adjusted either when all of the
225    codes are shorter than that, in which case the longest code length in
226    bits is used, or when the shortest code is *longer* than the requested
227    table size, in which case the length of the shortest code in bits is
228    used.
229 
230    There are two different values for the two tables, since they code a
231    different number of possibilities each.  The literal/length table
232    codes 286 possible values, or in a flat code, a little over eight
233    bits.  The distance table codes 30 possible values, or a little less
234    than five bits, flat.  The optimum values for speed end up being
235    about one bit more than those, so lbits is 8+1 and dbits is 5+1.
236    The optimum values may differ though from machine to machine, and
237    possibly even between compilers.  Your mileage may vary.
238  */
239 
240 /* If BMAX needs to be larger than 16, then h and x[] should be ulg. */
241 #define BMAX 16		/* maximum bit length of any code (16 for explode) */
242 #define N_MAX 288	/* maximum number of codes in any set */
243 
huft_build(unsigned * b,unsigned n,unsigned s,ush * d,ush * e,struct huft ** t,int * m,MBlockList * pool)244 int huft_build(
245     unsigned *b,	/* code lengths in bits (all assumed <= BMAX) */
246     unsigned n,		/* number of codes (assumed <= N_MAX) */
247     unsigned s,		/* number of simple-valued codes (0..s-1) */
248     ush *d,		/* list of base values for non-simple codes */
249     ush *e,		/* list of extra bits for non-simple codes */
250     struct huft **t,	/* result: starting table */
251     int *m,		/* maximum lookup bits, returns actual */
252     MBlockList *pool)	/* memory pool */
253 /* Given a list of code lengths and a maximum table size, make a set of
254    tables to decode that set of codes.	Return zero on success, one if
255    the given code set is incomplete (the tables are still built in this
256    case), two if the input is invalid (all zero length codes or an
257    oversubscribed set of lengths), and three if not enough memory.
258    The code with value 256 is special, and the tables are constructed
259    so that no bits beyond that code are fetched when that code is
260    decoded. */
261 {
262     unsigned a;			/* counter for codes of length k */
263     unsigned c[BMAX+1];		/* bit length count table */
264     unsigned el;		/* length of EOB code (value 256) */
265     unsigned f;			/* i repeats in table every f entries */
266     int g;			/* maximum code length */
267     int h;			/* table level */
268     register unsigned i;	/* counter, current code */
269     register unsigned j;	/* counter */
270     register int k;		/* number of bits in current code */
271     int lx[BMAX+1];		/* memory for l[-1..BMAX-1] */
272     int *l = lx+1;		/* stack of bits per table */
273     register unsigned *p;	/* pointer into c[], b[], or v[] */
274     register struct huft *q;	/* points to current table */
275     struct huft r;		/* table entry for structure assignment */
276     struct huft *u[BMAX];	/* table stack */
277     unsigned v[N_MAX];		/* values in order of bit length */
278     register int w;		/* bits before this table == (l * h) */
279     unsigned x[BMAX+1];		/* bit offsets, then code stack */
280     unsigned *xp;		/* pointer into x */
281     int y;			/* number of dummy codes added */
282     unsigned z;			/* number of entries in current table */
283 
284     /* Generate counts for each bit length */
285     el = n > 256 ? b[256] : BMAX; /* set length of EOB code, if any */
286     memset(c, 0, sizeof(c));
287     p = b;
288     i = n;
289     do
290     {
291 	Tracecv(*p, (stderr, (n-i >= ' ' && n-i <= '~' ? "%c %d\n" :
292 			      "0x%x %d\n"), n-i, *p));
293 	c[*p]++;	/* assume all entries <= BMAX */
294 	p++;		/* Can't combine with above line (Solaris bug) */
295     } while(--i);
296     if(c[0] == n)	/* null input--all zero length codes */
297     {
298 	*t = (struct huft *)NULL;
299 	*m = 0;
300 	return 0;
301     }
302 
303     /* Find minimum and maximum length, bound *m by those */
304     for(j = 1; j <= BMAX; j++)
305 	if(c[j])
306 	    break;
307     k = j;			/* minimum code length */
308     if((unsigned)*m < j)
309 	*m = j;
310     for(i = BMAX; i; i--)
311 	if(c[i])
312 	    break;
313     g = i;			/* maximum code length */
314     if((unsigned)*m > i)
315 	*m = i;
316 
317     /* Adjust last length count to fill out codes, if needed */
318     for(y = 1 << j; j < i; j++, y <<= 1)
319 	if((y -= c[j]) < 0)
320 	    return 2;		/* bad input: more codes than bits */
321     if((y -= c[i]) < 0)
322 	return 2;
323     c[i] += y;
324 
325     /* Generate starting offsets into the value table for each length */
326     x[1] = j = 0;
327     p = c + 1;  xp = x + 2;
328     while(--i)			/* note that i == g from above */
329 	*xp++ = (j += *p++);
330 
331     /* Make a table of values in order of bit lengths */
332     memset(v, 0, sizeof(v));
333     p = b;
334     i = 0;
335     do
336     {
337 	if((j = *p++) != 0)
338 	    v[x[j]++] = i;
339     } while(++i < n);
340     n = x[g];			/* set n to length of v */
341 
342     /* Generate the Huffman codes and for each, make the table entries */
343     x[0] = i = 0;		/* first Huffman code is zero */
344     p = v;			/* grab values in bit order */
345     h = -1;			/* no tables yet--level -1 */
346     w = l[-1] = 0;		/* no bits decoded yet */
347     u[0] = (struct huft *)NULL;	/* just to keep compilers happy */
348     q = (struct huft *)NULL;	/* ditto */
349     z = 0;			/* ditto */
350 
351     /* go through the bit lengths (k already is bits in shortest code) */
352     for(; k <= g; k++)
353     {
354 	a = c[k];
355 	while(a--)
356 	{
357 	    /* here i is the Huffman code of length k bits for value *p */
358 	    /* make tables up to required level */
359 	    while(k > w + l[h])
360 	    {
361 		w += l[h++];	/* add bits already decoded */
362 
363 		/* compute minimum size table less than or equal to *m bits */
364 		z = (z = g - w) > (unsigned)*m ? *m : z; /* upper limit */
365 		if((f = 1 << (j = k - w)) > a + 1) /* try a k-w bit table */
366 		{		/* too few codes for k-w bit table */
367 		    f -= a + 1;	/* deduct codes from patterns left */
368 		    xp = c + k;
369 		    while(++j < z)/* try smaller tables up to z bits */
370 		    {
371 			if((f <<= 1) <= *++xp)
372 			    break;	/* enough codes to use up j bits */
373 			f -= *xp;	/* else deduct codes from patterns */
374 		    }
375 		}
376 		if((unsigned)w + j > el && (unsigned)w < el)
377 		    j = el - w;	/* make EOB code end at table */
378 		z = 1 << j;	/* table entries for j-bit table */
379 		l[h] = j;	/* set table size in stack */
380 
381 		/* allocate and link in new table */
382 		if(pool == NULL)
383 		    q = (struct huft *)malloc((z + 1)*sizeof(struct huft));
384 		else
385 		    q = (struct huft *)
386 			new_segment(pool, (z + 1)*sizeof(struct huft));
387 		if(q == NULL)
388 		{
389 		    if(h && pool == NULL)
390 			huft_free(u[0]);
391 		    return 3;	/* not enough memory */
392 		}
393 
394 		*t = q + 1;	/* link to list for huft_free() */
395 		*(t = &(q->v.t)) = (struct huft *)NULL;
396 		u[h] = ++q;	/* table starts after link */
397 
398 		/* connect to last table, if there is one */
399 		if(h)
400 		{
401 		    x[h] = i;		/* save pattern for backing up */
402 		    r.b = (uch)l[h-1];	/* bits to dump before this table */
403 		    r.e = (uch)(16 + j);/* bits in this table */
404 		    r.v.t = q;		/* pointer to this table */
405 		    j = (i & ((1 << w) - 1)) >> (w - l[h-1]);
406 		    u[h-1][j] = r;	/* connect to last table */
407 		}
408 	    }
409 
410 	    /* set up table entry in r */
411 	    r.b = (uch)(k - w);
412 	    if(p >= v + n)
413 		r.e = 99;		/* out of values--invalid code */
414 	    else if(*p < s)
415 	    {
416 		r.e = (uch)(*p < 256 ? 16 : 15); /* 256 is end-of-block code */
417 		r.v.n = (ush)*p++;	/* simple code is just the value */
418 	    }
419 	    else
420 	    {
421 		r.e = (uch)e[*p - s];	/* non-simple--look up in lists */
422 		r.v.n = d[*p++ - s];
423 	    }
424 
425 	    /* fill code-like entries with r */
426 	    f = 1 << (k - w);
427 	    for(j = i >> w; j < z; j += f)
428 		q[j] = r;
429 
430 	    /* backwards increment the k-bit code i */
431 	    for(j = 1 << (k - 1); i & j; j >>= 1)
432 		i ^= j;
433 	    i ^= j;
434 
435 	    /* backup over finished tables */
436 	    while((i & ((1 << w) - 1)) != x[h])
437 		w -= l[--h];		/* don't need to update q */
438 	}
439     }
440 
441     /* return actual size of base table */
442     *m = l[0];
443 
444     /* Return true (1) if we were given an incomplete table */
445     return y != 0 && g != 1;
446 }
447 
huft_free(struct huft * t)448 local int huft_free(struct huft *t)
449 /* Free the malloc'ed tables built by huft_build(), which makes a linked
450    list of the tables it made, with the links in a dummy first entry of
451    each table. */
452 {
453     register struct huft *p, *q;
454 
455     /* Go through linked list, freeing from the malloced (t[-1]) address. */
456     p = t;
457     while(p != (struct huft *)NULL)
458     {
459 	q = (--p)->v.t;
460 	free((char*)p);
461 	p = q;
462     }
463     return 0;
464 }
465 
inflate_codes(InflateHandler decoder,char * buff,long size)466 local long inflate_codes(InflateHandler decoder, char *buff, long size)
467 /* inflate (decompress) the codes in a deflated (compressed) block.
468    Return an error code or zero if it all goes ok. */
469 {
470     register unsigned e;/* table entry flag/number of extra bits */
471     struct huft *t;	/* pointer to table entry */
472     int n;
473     struct huft *tl, *td;/* literal/length and distance decoder tables */
474     int bl, bd;		/* number of bits decoded by tl[] and td[] */
475     unsigned l, w, d;
476     uch *slide;
477 
478     BITS_SAVE;
479 
480     if(size == 0)
481 	return 0;
482 
483     slide = decoder->slide;
484     tl = decoder->tl;
485     td = decoder->td;
486     bl = decoder->bl;
487     bd = decoder->bd;
488 
489 #ifdef DEBUG
490     if(decoder->copy_leng != 0)
491     {
492 	fprintf(stderr, "What ? (decoder->copy_leng = %d)\n",
493 		decoder->copy_leng);
494 	abort();
495     }
496 #endif /* DEBUG */
497     w = decoder->wp;
498 
499     /* inflate the coded data */
500     n = 0;
501     for(;;)			/* do until end of block */
502     {
503 	NEEDBITS((unsigned)bl);
504 	t = tl + GETBITS(bl);
505 	e = t->e;
506 	while(e > 16)
507 	{
508 	    if(e == 99)
509 		return -1;
510 	    DUMPBITS(t->b);
511 	    e -= 16;
512 	    NEEDBITS(e);
513 	    t = t->v.t + GETBITS(e);
514 	    e = t->e;
515 	}
516 	DUMPBITS(t->b);
517 
518 	if(e == 16)		/* then it's a literal */
519 	{
520 	    w &= WSIZE - 1;
521 	    buff[n++] = slide[w++] = (uch)t->v.n;
522 	    if(n == size)
523 	    {
524 		decoder->wp = w;
525 		BITS_RESTORE;
526 		return size;
527 	    }
528 	    continue;
529 	}
530 
531 	/* exit if end of block */
532 	if(e == 15)
533 	    break;
534 
535 	/* it's an EOB or a length */
536 
537 	/* get length of block to copy */
538 	NEEDBITS(e);
539 	l = t->v.n + GETBITS(e);
540 	DUMPBITS(e);
541 
542 	/* decode distance of block to copy */
543 	NEEDBITS((unsigned)bd);
544 	t = td + GETBITS(bd);
545 	e = t->e;
546 	while(e > 16)
547 	{
548 	    if(e == 99)
549 		return -1;
550 	    DUMPBITS(t->b);
551 	    e -= 16;
552 	    NEEDBITS(e);
553 	    t = t->v.t + GETBITS(e);
554 	    e = t->e;
555 	}
556 	DUMPBITS(t->b);
557 	NEEDBITS(e);
558 	d = w - t->v.n - GETBITS(e);
559 	DUMPBITS(e);
560 
561 	/* do the copy */
562 	while(l > 0 && n < size)
563 	{
564 	    l--;
565 	    d &= WSIZE - 1;
566 	    w &= WSIZE - 1;
567 	    buff[n++] = slide[w++] = slide[d++];
568 	}
569 
570 	if(n == size)
571 	{
572 	    decoder->copy_leng = l;
573 	    decoder->wp = w;
574 	    decoder->copy_dist = d;
575 	    BITS_RESTORE;
576 	    return n;
577 	}
578     }
579 
580     decoder->wp = w;
581     decoder->method = -1; /* done */
582     BITS_RESTORE;
583     return n;
584 }
585 
inflate_stored(InflateHandler decoder,char * buff,long size)586 local long inflate_stored(InflateHandler decoder, char *buff, long size)
587 /* "decompress" an inflated type 0 (stored) block. */
588 {
589     unsigned n, l, w;
590     BITS_SAVE;
591 
592     /* go to byte boundary */
593     n = bit_len & 7;
594     DUMPBITS(n);
595 
596     /* get the length and its complement */
597     NEEDBITS(16);
598     n = GETBITS(16);
599     DUMPBITS(16);
600     NEEDBITS(16);
601     if(n != (unsigned)((~bit_buf) & 0xffff))
602     {
603 	BITS_RESTORE;
604 	return -1;			/* error in compressed data */
605     }
606     DUMPBITS(16);
607 
608     /* read and output the compressed data */
609     decoder->copy_leng = n;
610 
611     n = 0;
612     l = decoder->copy_leng;
613     w = decoder->wp;
614     while(l > 0 && n < size)
615     {
616 	l--;
617 	w &= WSIZE - 1;
618 	NEEDBITS(8);
619 	buff[n++] = decoder->slide[w++] = (uch)GETBITS(8);
620 	DUMPBITS(8);
621     }
622     if(l == 0)
623 	decoder->method = -1; /* done */
624     decoder->copy_leng = l;
625     decoder->wp = w;
626     BITS_RESTORE;
627     return (long)n;
628 }
629 
inflate_fixed(InflateHandler decoder,char * buff,long size)630 local long inflate_fixed(InflateHandler decoder, char *buff, long size)
631 /* decompress an inflated type 1 (fixed Huffman codes) block.  We should
632    either replace this with a custom decoder, or at least precompute the
633    Huffman tables. */
634 {
635     /* if first time, set up tables for fixed blocks */
636     if(decoder->fixed_tl == NULL)
637     {
638 	int i;		  /* temporary variable */
639 	unsigned l[288];	  /* length list for huft_build */
640 
641 	/* literal table */
642 	for(i = 0; i < 144; i++)
643 	    l[i] = 8;
644 	for(; i < 256; i++)
645 	    l[i] = 9;
646 	for(; i < 280; i++)
647 	    l[i] = 7;
648 	for(; i < 288; i++)	  /* make a complete, but wrong code set */
649 	    l[i] = 8;
650 	decoder->fixed_bl = 7;
651 	if((i = huft_build(l, 288, 257, cplens, cplext,
652 			   &decoder->fixed_tl, &decoder->fixed_bl, NULL))
653 	    != 0)
654 	{
655 	    decoder->fixed_tl = NULL;
656 	    return -1;
657 	}
658 
659 	/* distance table */
660 	for(i = 0; i < 30; i++)	  /* make an incomplete code set */
661 	    l[i] = 5;
662 	decoder->fixed_bd = 5;
663 	if((i = huft_build(l, 30, 0, cpdist, cpdext,
664 			   &decoder->fixed_td, &decoder->fixed_bd, NULL)) > 1)
665 	{
666 	    huft_free(decoder->fixed_tl);
667 	    decoder->fixed_tl = NULL;
668 	    return -1;
669 	}
670     }
671 
672     decoder->tl = decoder->fixed_tl;
673     decoder->td = decoder->fixed_td;
674     decoder->bl = decoder->fixed_bl;
675     decoder->bd = decoder->fixed_bd;
676     return inflate_codes(decoder, buff, size);
677 }
678 
inflate_dynamic(InflateHandler decoder,char * buff,long size)679 local long inflate_dynamic(InflateHandler decoder, char *buff, long size)
680 /* decompress an inflated type 2 (dynamic Huffman codes) block. */
681 {
682     int i;		/* temporary variables */
683     unsigned j;
684     unsigned l;		/* last length */
685     unsigned n;		/* number of lengths to get */
686     struct huft *tl;	/* literal/length code table */
687     struct huft *td;	/* distance code table */
688     int bl;		/* lookup bits for tl */
689     int bd;		/* lookup bits for td */
690     unsigned nb;	/* number of bit length codes */
691     unsigned nl;	/* number of literal/length codes */
692     unsigned nd;	/* number of distance codes */
693 #ifdef PKZIP_BUG_WORKAROUND
694     unsigned ll[288+32];/* literal/length and distance code lengths */
695 #else
696     unsigned ll[286+30];/* literal/length and distance code lengths */
697 #endif
698     static unsigned border[] = {  /* Order of the bit length code lengths */
699 	16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15};
700     BITS_SAVE;
701 
702     reuse_mblock(&decoder->pool);
703 
704     /* read in table lengths */
705     NEEDBITS(5);
706     nl = 257 + GETBITS(5);	/* number of literal/length codes */
707     DUMPBITS(5);
708     NEEDBITS(5);
709     nd = 1 + GETBITS(5);	/* number of distance codes */
710     DUMPBITS(5);
711     NEEDBITS(4);
712     nb = 4 + GETBITS(4);	/* number of bit length codes */
713     DUMPBITS(4);
714 #ifdef PKZIP_BUG_WORKAROUND
715     if(nl > 288 || nd > 32)
716 #else
717     if(nl > 286 || nd > 30)
718 #endif
719     {
720 	BITS_RESTORE;
721 	return -1;		/* bad lengths */
722     }
723 
724     /* read in bit-length-code lengths */
725     for(j = 0; j < nb; j++)
726     {
727 	NEEDBITS(3);
728 	ll[border[j]] = GETBITS(3);
729 	DUMPBITS(3);
730     }
731     for(; j < 19; j++)
732 	ll[border[j]] = 0;
733 
734     /* build decoding table for trees--single level, 7 bit lookup */
735     bl = 7;
736     if((i = huft_build(ll, 19, 19, NULL, NULL, &tl, &bl, &decoder->pool)) != 0)
737     {
738 	reuse_mblock(&decoder->pool);
739 	BITS_RESTORE;
740 	return -1;		/* incomplete code set */
741     }
742 
743     /* read in literal and distance code lengths */
744     n = nl + nd;
745     i = l = 0;
746     while((unsigned)i < n)
747     {
748 	NEEDBITS((unsigned)bl);
749 	j = (td = tl + (GETBITS(bl)))->b;
750 	DUMPBITS(j);
751 	j = td->v.n;
752 	if(j < 16)		/* length of code in bits (0..15) */
753 	    ll[i++] = l = j;	/* save last length in l */
754 	else if(j == 16)	/* repeat last length 3 to 6 times */
755 	{
756 	    NEEDBITS(2);
757 	    j = 3 + GETBITS(2);
758 	    DUMPBITS(2);
759 	    if((unsigned)i + j > n)
760 	    {
761 		BITS_RESTORE;
762 		return -1;
763 	    }
764 	    while(j--)
765 		ll[i++] = l;
766 	}
767 	else if(j == 17)	/* 3 to 10 zero length codes */
768 	{
769 	    NEEDBITS(3);
770 	    j = 3 + GETBITS(3);
771 	    DUMPBITS(3);
772 	    if((unsigned)i + j > n)
773 	    {
774 		BITS_RESTORE;
775 		return -1;
776 	    }
777 	    while(j--)
778 		ll[i++] = 0;
779 	    l = 0;
780 	}
781 	else			/* j == 18: 11 to 138 zero length codes */
782 	{
783 	    NEEDBITS(7);
784 	    j = 11 + GETBITS(7);
785 	    DUMPBITS(7);
786 	    if((unsigned)i + j > n)
787 	    {
788 		BITS_RESTORE;
789 		return -1;
790 	    }
791 	    while(j--)
792 		ll[i++] = 0;
793 	    l = 0;
794 	}
795     }
796 
797     BITS_RESTORE;
798 
799     /* free decoding table for trees */
800     reuse_mblock(&decoder->pool);
801 
802     /* build the decoding tables for literal/length and distance codes */
803     bl = lbits;
804     i = huft_build(ll, nl, 257, cplens, cplext, &tl, &bl, &decoder->pool);
805     if(bl == 0)			      /* no literals or lengths */
806       i = 1;
807     if(i)
808     {
809 	if(i == 1)
810 	    fprintf(stderr, " incomplete literal tree\n");
811 	reuse_mblock(&decoder->pool);
812 	return -1;		/* incomplete code set */
813     }
814     bd = dbits;
815     i = huft_build(ll + nl, nd, 0, cpdist, cpdext, &td, &bd, &decoder->pool);
816     if(bd == 0 && nl > 257)    /* lengths but no distances */
817     {
818 	fprintf(stderr, " incomplete distance tree\n");
819 	reuse_mblock(&decoder->pool);
820 	return -1;
821     }
822 
823     if(i == 1) {
824 #ifdef PKZIP_BUG_WORKAROUND
825 	i = 0;
826 #else
827 	fprintf(stderr, " incomplete distance tree\n");
828 #endif
829     }
830     if(i)
831     {
832 	reuse_mblock(&decoder->pool);
833 	return -1;
834     }
835 
836     /* decompress until an end-of-block code */
837     decoder->tl = tl;
838     decoder->td = td;
839     decoder->bl = bl;
840     decoder->bd = bd;
841 
842     i = inflate_codes(decoder, buff, size);
843 
844     if(i == -1) /* error */
845     {
846 	reuse_mblock(&decoder->pool);
847 	return -1;
848     }
849 
850     /* free the decoding tables, return */
851     return i;
852 }
853 
inflate_start(InflateHandler decoder)854 local void inflate_start(InflateHandler decoder)
855 /* initialize window, bit buffer */
856 {
857     decoder->wp = 0;
858     decoder->bit_buf = 0;
859     decoder->bit_len = 0;
860     decoder->insize = decoder->inptr = 0;
861     decoder->fixed_td = decoder->fixed_tl = NULL;
862     decoder->method = -1;
863     decoder->eof = 0;
864     decoder->copy_leng = decoder->copy_dist = 0;
865     decoder->tl = NULL;
866 
867     init_mblock(&decoder->pool);
868 }
869 
870 /*ARGSUSED*/
default_read_func(char * buf,long size,void * v)871 static long default_read_func(char *buf, long size, void *v)
872 {
873     return (long)fread(buf, 1, size, stdin);
874 }
875 
open_inflate_handler(long (* read_func)(char * buf,long size,void * user_val),void * user_val)876 InflateHandler open_inflate_handler(
877     long (* read_func)(char *buf, long size, void *user_val),
878     void *user_val)
879 {
880     InflateHandler decoder;
881 
882     decoder = (InflateHandler)
883 	malloc(sizeof(struct _InflateHandler));
884     inflate_start(decoder);
885     decoder->user_val = user_val;
886     if(read_func == NULL)
887 	decoder->read_func = default_read_func;
888     else
889 	decoder->read_func = read_func;
890     return decoder;
891 }
892 
close_inflate_handler(InflateHandler decoder)893 void close_inflate_handler(InflateHandler decoder)
894 {
895     if(decoder->fixed_tl != NULL)
896     {
897 	huft_free(decoder->fixed_td);
898 	huft_free(decoder->fixed_tl);
899 	decoder->fixed_td = decoder->fixed_tl = NULL;
900     }
901     reuse_mblock(&decoder->pool);
902     free(decoder);
903 }
904 
905 /* decompress an inflated entry */
zip_inflate(InflateHandler decoder,char * buff,long size)906 long zip_inflate(
907     InflateHandler decoder,
908     char *buff,
909     long size)
910 {
911     long n, i;
912 
913     n = 0;
914     while(n < size)
915     {
916 	if(decoder->eof && decoder->method == -1)
917 	    return n;
918 
919 	if(decoder->copy_leng > 0)
920 	{
921 	    unsigned l, w, d;
922 
923 	    l = decoder->copy_leng;
924 	    w = decoder->wp;
925 	    if(decoder->method != STORED_BLOCK)
926 	    {
927 		/* STATIC_TREES or DYN_TREES */
928 		d = decoder->copy_dist;
929 		while(l > 0 && n < size)
930 		{
931 		    l--;
932 		    d &= WSIZE - 1;
933 		    w &= WSIZE - 1;
934 		    buff[n++] = decoder->slide[w++] = decoder->slide[d++];
935 		}
936 		decoder->copy_dist = d;
937 	    }
938 	    else /* STATIC_TREES or DYN_TREES */
939 	    {
940 		BITS_SAVE;
941 		while(l > 0 && n < size)
942 		{
943 		    l--;
944 		    w &= WSIZE - 1;
945 		    NEEDBITS(8);
946 		    buff[n++] = decoder->slide[w++] = (uch)GETBITS(8);
947 		    DUMPBITS(8);
948 		}
949 		BITS_RESTORE;
950 		if(l == 0)
951 		    decoder->method = -1; /* done */
952 	    }
953 	    decoder->copy_leng = l;
954 	    decoder->wp = w;
955 	    if(n == size)
956 		return n;
957 	}
958 
959 	if(decoder->method == -1)
960 	{
961 	    BITS_SAVE;
962 	    if(decoder->eof)
963 	    {
964 		BITS_RESTORE;
965 		break;
966 	    }
967 	    /* read in last block bit */
968 	    NEEDBITS(1);
969 	    if(GETBITS(1))
970 		decoder->eof = 1;
971 	    DUMPBITS(1);
972 
973 	    /* read in block type */
974 	    NEEDBITS(2);
975 	    decoder->method = (int)GETBITS(2);
976 	    DUMPBITS(2);
977 	    decoder->tl = NULL;
978 	    decoder->copy_leng = 0;
979 	    BITS_RESTORE;
980 	}
981 
982 	switch(decoder->method)
983 	{
984 	  case STORED_BLOCK:
985 	    i = inflate_stored(decoder, buff + n, size - n);
986 	    break;
987 
988 	  case STATIC_TREES:
989 	    if(decoder->tl != NULL)
990 		i = inflate_codes(decoder, buff + n, size - n);
991 	    else
992 		i = inflate_fixed(decoder, buff + n, size - n);
993 	    break;
994 
995 	  case DYN_TREES:
996 	    if(decoder->tl != NULL)
997 		i = inflate_codes(decoder, buff + n, size - n);
998 	    else
999 		i = inflate_dynamic(decoder, buff + n, size - n);
1000 	    break;
1001 
1002 	  default: /* error */
1003 	    i = -1;
1004 	    break;
1005 	}
1006 
1007 	if(i == -1)
1008 	{
1009 	    if(decoder->eof)
1010 		return 0;
1011 	    return -1; /* error */
1012 	}
1013 	n += i;
1014     }
1015     return n;
1016 }
1017 
1018 /* ===========================================================================
1019  * Fill the input buffer. This is called only when the buffer is empty.
1020  */
fill_inbuf(InflateHandler decoder)1021 local int fill_inbuf(InflateHandler decoder)
1022 {
1023     int len;
1024 
1025     /* Read as much as possible */
1026     decoder->insize = 0;
1027     errno = 0;
1028     do {
1029 	len = decoder->read_func((char*)decoder->inbuf + decoder->insize,
1030 				 (long)(INBUFSIZ - decoder->insize),
1031 				 decoder->user_val);
1032 	if(len == 0 || len == EOF) break;
1033 	decoder->insize += len;
1034     } while(decoder->insize < INBUFSIZ);
1035 
1036     if(decoder->insize == 0)
1037 	return EOF;
1038     decoder->inptr = 1;
1039     return decoder->inbuf[0];
1040 }
1041