1 /* inffast.c -- process literals and length/distance pairs fast
2 * Copyright (C) 1995-2002 Mark Adler
3 * For conditions of distribution and use, see copyright notice in zlib.h
5 * $FreeBSD: src/lib/libz/inffast.c,v 1.1.1.3.6.1 2003/02/01 13:33:12 sobomax Exp $
6 * $DragonFly: src/lib/libz/Attic/inffast.c,v 1.2 2003/06/17 04:26:52 dillon Exp $
16 struct inflate_codes_state {int dummy;}; /* for buggy compilers */
18 /* simplify the use of the inflate_huft type with some defines */
19 #define exop word.what.Exop
20 #define bits word.what.Bits
22 /* macros for bit input with no checking and for returning unused bytes */
23 #define GRABBITS(j) {while(k<(j)){b|=((uLong)NEXTBYTE)<<k;k+=8;}}
24 #define UNGRAB {c=z->avail_in-n;c=(k>>3)<c?k>>3:c;n+=c;p-=c;k-=c<<3;}
26 /* Called with number of bytes left to write in window at least 258
27 (the maximum string length) and number of input bytes available
28 at least ten. The ten bytes are six bytes for the longest length/
29 distance pair plus four bytes for overloading the bit buffer. */
31 int inflate_fast(bl, bd, tl, td, s, z)
34 inflate_huft *td; /* need separate declaration for Borland C++ */
35 inflate_blocks_statef *s;
38 inflate_huft *t; /* temporary pointer */
39 uInt e; /* extra bits or operation */
40 uLong b; /* bit buffer */
41 uInt k; /* bits in bit buffer */
42 Bytef *p; /* input data pointer */
43 uInt n; /* bytes available there */
44 Bytef *q; /* output window write pointer */
45 uInt m; /* bytes to end of window or read pointer */
46 uInt ml; /* mask for literal/length tree */
47 uInt md; /* mask for distance tree */
48 uInt c; /* bytes to copy */
49 uInt d; /* distance back to copy from */
50 Bytef *r; /* copy source pointer */
52 /* load input, output, bit values */
55 /* initialize masks */
56 ml = inflate_mask[bl];
57 md = inflate_mask[bd];
59 /* do until not enough input or output space for fast loop */
60 do { /* assume called with m >= 258 && n >= 10 */
61 /* get literal/length code */
62 GRABBITS(20) /* max bits for literal/length code */
63 if ((e = (t = tl + ((uInt)b & ml))->exop) == 0)
66 Tracevv((stderr, t->base >= 0x20 && t->base < 0x7f ?
67 "inflate: * literal '%c'\n" :
68 "inflate: * literal 0x%02x\n", t->base));
77 /* get extra bits for length */
79 c = t->base + ((uInt)b & inflate_mask[e]);
81 Tracevv((stderr, "inflate: * length %u\n", c));
83 /* decode distance base of block to copy */
84 GRABBITS(15); /* max bits for distance code */
85 e = (t = td + ((uInt)b & md))->exop;
90 /* get extra bits to add to distance base */
92 GRABBITS(e) /* get extra bits (up to 13) */
93 d = t->base + ((uInt)b & inflate_mask[e]);
95 Tracevv((stderr, "inflate: * distance %u\n", d));
100 if (r < s->window) /* wrap if needed */
103 r += s->end - s->window; /* force pointer in window */
104 } while (r < s->window); /* covers invalid distances */
108 c -= e; /* wrapped copy */
117 else /* normal copy */
126 else /* normal copy */
136 else if ((e & 64) == 0)
139 e = (t += ((uInt)b & inflate_mask[e]))->exop;
143 z->msg = (char*)"invalid distance code";
154 if ((e = (t += ((uInt)b & inflate_mask[e]))->exop) == 0)
157 Tracevv((stderr, t->base >= 0x20 && t->base < 0x7f ?
158 "inflate: * literal '%c'\n" :
159 "inflate: * literal 0x%02x\n", t->base));
160 *q++ = (Byte)t->base;
167 Tracevv((stderr, "inflate: * end of block\n"));
174 z->msg = (char*)"invalid literal/length code";
180 } while (m >= 258 && n >= 10);
182 /* not enough input or output--restore pointers and return */