]>
git.saurik.com Git - apple/security.git/blob - SecurityServer/MacYarrow/zlib/inffast.c
2 * Copyright (c) 2000-2001 Apple Computer, Inc. All Rights Reserved.
4 * The contents of this file constitute Original Code as defined in and are
5 * subject to the Apple Public Source License Version 1.2 (the 'License').
6 * You may not use this file except in compliance with the License. Please obtain
7 * a copy of the License at http://www.apple.com/publicsource and read it before
10 * This Original Code and all software distributed under the License are
11 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESS
12 * OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, INCLUDING WITHOUT
13 * LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
14 * PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT. Please see the License for the
15 * specific language governing rights and limitations under the License.
19 /* inffast.c -- process literals and length/distance pairs fast
20 * Copyright (C) 1995-1998 Mark Adler
21 * For conditions of distribution and use, see copyright notice in zlib.h
31 struct inflate_codes_state
{int dummy
;}; /* for buggy compilers */
33 /* simplify the use of the inflate_huft type with some defines */
34 #define exop word.what.Exop
35 #define bits word.what.Bits
37 /* macros for bit input with no checking and for returning unused bytes */
38 #define GRABBITS(j) {while(k<(j)){b|=((uLong)NEXTBYTE)<<k;k+=8;}}
39 #define UNGRAB {c=z->avail_in-n;c=(k>>3)<c?k>>3:c;n+=c;p-=c;k-=c<<3;}
41 /* Called with number of bytes left to write in window at least 258
42 (the maximum string length) and number of input bytes available
43 at least ten. The ten bytes are six bytes for the longest length/
44 distance pair plus four bytes for overloading the bit buffer. */
46 int inflate_fast(bl
, bd
, tl
, td
, s
, z
)
49 inflate_huft
*td
; /* need separate declaration for Borland C++ */
50 inflate_blocks_statef
*s
;
53 inflate_huft
*t
; /* temporary pointer */
54 uInt e
; /* extra bits or operation */
55 uLong b
; /* bit buffer */
56 uInt k
; /* bits in bit buffer */
57 Bytef
*p
; /* input data pointer */
58 uInt n
; /* bytes available there */
59 Bytef
*q
; /* output window write pointer */
60 uInt m
; /* bytes to end of window or read pointer */
61 uInt ml
; /* mask for literal/length tree */
62 uInt md
; /* mask for distance tree */
63 uInt c
; /* bytes to copy */
64 uInt d
; /* distance back to copy from */
65 Bytef
*r
; /* copy source pointer */
67 /* load input, output, bit values */
70 /* initialize masks */
71 ml
= inflate_mask
[bl
];
72 md
= inflate_mask
[bd
];
74 /* do until not enough input or output space for fast loop */
75 do { /* assume called with m >= 258 && n >= 10 */
76 /* get literal/length code */
77 GRABBITS(20) /* max bits for literal/length code */
78 if ((e
= (t
= tl
+ ((uInt
)b
& ml
))->exop
) == 0)
81 Tracevv((stderr
, t
->base
>= 0x20 && t
->base
< 0x7f ?
82 "inflate: * literal '%c'\n" :
83 "inflate: * literal 0x%02x\n", t
->base
));
92 /* get extra bits for length */
94 c
= t
->base
+ ((uInt
)b
& inflate_mask
[e
]);
96 Tracevv((stderr
, "inflate: * length %u\n", c
));
98 /* decode distance base of block to copy */
99 GRABBITS(15); /* max bits for distance code */
100 e
= (t
= td
+ ((uInt
)b
& md
))->exop
;
105 /* get extra bits to add to distance base */
107 GRABBITS(e
) /* get extra bits (up to 13) */
108 d
= t
->base
+ ((uInt
)b
& inflate_mask
[e
]);
110 Tracevv((stderr
, "inflate: * distance %u\n", d
));
114 if ((uInt
)(q
- s
->window
) >= d
) /* offset before dest */
117 *q
++ = *r
++; c
--; /* minimum count is three, */
118 *q
++ = *r
++; c
--; /* so unroll loop a little */
120 else /* else offset after destination */
122 e
= d
- (uInt
)(q
- s
->window
); /* bytes from offset to end */
123 r
= s
->end
- e
; /* pointer to offset */
124 if (c
> e
) /* if source crosses, */
126 c
-= e
; /* copy to end of window */
130 r
= s
->window
; /* copy rest from start of window */
133 do { /* copy all or what's left */
138 else if ((e
& 64) == 0)
141 e
= (t
+= ((uInt
)b
& inflate_mask
[e
]))->exop
;
145 z
->msg
= (char*)"invalid distance code";
156 if ((e
= (t
+= ((uInt
)b
& inflate_mask
[e
]))->exop
) == 0)
159 Tracevv((stderr
, t
->base
>= 0x20 && t
->base
< 0x7f ?
160 "inflate: * literal '%c'\n" :
161 "inflate: * literal 0x%02x\n", t
->base
));
162 *q
++ = (Byte
)t
->base
;
169 Tracevv((stderr
, "inflate: * end of block\n"));
176 z
->msg
= (char*)"invalid literal/length code";
182 } while (m
>= 258 && n
>= 10);
184 /* not enough input or output--restore pointers and return */