1 /* inffast.c -- process literals and length/distance pairs fast
2 * Copyright (C) 1995-1998 Mark Adler
3 * For conditions of distribution and use, see copyright notice in zlib.h
6 #include <linux/zutil.h>
13 struct inflate_codes_state
;
15 /* simplify the use of the inflate_huft type with some defines */
16 #define exop word.what.Exop
17 #define bits word.what.Bits
19 /* macros for bit input with no checking and for returning unused bytes */
20 #define GRABBITS(j) {while(k<(j)){b|=((uLong)NEXTBYTE)<<k;k+=8;}}
21 #define UNGRAB {c=z->avail_in-n;c=(k>>3)<c?k>>3:c;n+=c;p-=c;k-=c<<3;}
23 /* Called with number of bytes left to write in window at least 258
24 (the maximum string length) and number of input bytes available
25 at least ten. The ten bytes are six bytes for the longest length/
26 distance pair plus four bytes for overloading the bit buffer. */
28 int zlib_inflate_fast(
32 inflate_huft
*td
, /* need separate declaration for Borland C++ */
33 inflate_blocks_statef
*s
,
37 inflate_huft
*t
; /* temporary pointer */
38 uInt e
; /* extra bits or operation */
39 uLong b
; /* bit buffer */
40 uInt k
; /* bits in bit buffer */
41 Byte
*p
; /* input data pointer */
42 uInt n
; /* bytes available there */
43 Byte
*q
; /* output window write pointer */
44 uInt m
; /* bytes to end of window or read pointer */
45 uInt ml
; /* mask for literal/length tree */
46 uInt md
; /* mask for distance tree */
47 uInt c
; /* bytes to copy */
48 uInt d
; /* distance back to copy from */
49 Byte
*r
; /* copy source pointer */
51 /* load input, output, bit values */
54 /* initialize masks */
55 ml
= zlib_inflate_mask
[bl
];
56 md
= zlib_inflate_mask
[bd
];
58 /* do until not enough input or output space for fast loop */
59 do { /* assume called with m >= 258 && n >= 10 */
60 /* get literal/length code */
61 GRABBITS(20) /* max bits for literal/length code */
62 if ((e
= (t
= tl
+ ((uInt
)b
& ml
))->exop
) == 0)
73 /* get extra bits for length */
75 c
= t
->base
+ ((uInt
)b
& zlib_inflate_mask
[e
]);
78 /* decode distance base of block to copy */
79 GRABBITS(15); /* max bits for distance code */
80 e
= (t
= td
+ ((uInt
)b
& md
))->exop
;
85 /* get extra bits to add to distance base */
87 GRABBITS(e
) /* get extra bits (up to 13) */
88 d
= t
->base
+ ((uInt
)b
& zlib_inflate_mask
[e
]);
94 if (r
< s
->window
) /* wrap if needed */
97 r
+= s
->end
- s
->window
; /* force pointer in window */
98 } while (r
< s
->window
); /* covers invalid distances */
102 c
-= e
; /* wrapped copy */
111 else /* normal copy */
120 else /* normal copy */
130 else if ((e
& 64) == 0)
133 e
= (t
+= ((uInt
)b
& zlib_inflate_mask
[e
]))->exop
;
137 z
->msg
= (char*)"invalid distance code";
148 if ((e
= (t
+= ((uInt
)b
& zlib_inflate_mask
[e
]))->exop
) == 0)
151 *q
++ = (Byte
)t
->base
;
164 z
->msg
= (char*)"invalid literal/length code";
170 } while (m
>= 258 && n
>= 10);
172 /* not enough input or output--restore pointers and return */