]>
git.saurik.com Git - apple/security.git/blob - SecurityServer/MacYarrow/zlib/infcodes.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 /* infcodes.c -- process literals and length/distance pairs
20 * Copyright (C) 1995-1998 Mark Adler
21 * For conditions of distribution and use, see copyright notice in zlib.h
31 /* simplify the use of the inflate_huft type with some defines */
32 #define exop word.what.Exop
33 #define bits word.what.Bits
35 typedef enum { /* waiting for "i:"=input, "o:"=output, "x:"=nothing */
36 START
, /* x: set up for LEN */
37 LEN
, /* i: get length/literal/eob next */
38 LENEXT
, /* i: getting length extra (have base) */
39 DIST
, /* i: get distance next */
40 DISTEXT
, /* i: getting distance extra */
41 COPY
, /* o: copying bytes in window, waiting for space */
42 LIT
, /* o: got literal, waiting for output space */
43 WASH
, /* o: got eob, possibly still output waiting */
44 END
, /* x: got eob and all data flushed */
45 BADCODE
} /* x: got error */
48 /* inflate codes private state */
49 struct inflate_codes_state
{
52 inflate_codes_mode mode
; /* current inflate_codes mode */
54 /* mode dependent information */
58 inflate_huft
*tree
; /* pointer into tree */
59 uInt need
; /* bits needed */
60 } code
; /* if LEN or DIST, where in tree */
61 uInt lit
; /* if LIT, literal */
63 uInt get
; /* bits to get for extra */
64 uInt dist
; /* distance back to copy from */
65 } copy
; /* if EXT or COPY, where and how much */
68 /* mode independent information */
69 Byte lbits
; /* ltree bits decoded per branch */
70 Byte dbits
; /* dtree bits decoder per branch */
71 inflate_huft
*ltree
; /* literal/length/eob tree */
72 inflate_huft
*dtree
; /* distance tree */
77 inflate_codes_statef
*inflate_codes_new(bl
, bd
, tl
, td
, z
)
80 inflate_huft
*td
; /* need separate declaration for Borland C++ */
83 inflate_codes_statef
*c
;
85 if ((c
= (inflate_codes_statef
*)
86 ZALLOC(z
,1,sizeof(struct inflate_codes_state
))) != Z_NULL
)
93 Tracev((stderr
, "inflate: codes new\n"));
99 int inflate_codes(s
, z
, r
)
100 inflate_blocks_statef
*s
;
104 uInt j
; /* temporary storage */
105 inflate_huft
*t
; /* temporary pointer */
106 uInt e
; /* extra bits or operation */
107 uLong b
; /* bit buffer */
108 uInt k
; /* bits in bit buffer */
109 Bytef
*p
; /* input data pointer */
110 uInt n
; /* bytes available there */
111 Bytef
*q
; /* output window write pointer */
112 uInt m
; /* bytes to end of window or read pointer */
113 Bytef
*f
; /* pointer to copy strings from */
114 inflate_codes_statef
*c
= s
->sub
.decode
.codes
; /* codes state */
116 /* copy input/output information to locals (UPDATE macro restores) */
119 /* process input and output based on current state */
120 while (1) switch (c
->mode
)
121 { /* waiting for "i:"=input, "o:"=output, "x:"=nothing */
122 case START
: /* x: set up for LEN */
124 if (m
>= 258 && n
>= 10)
127 r
= inflate_fast(c
->lbits
, c
->dbits
, c
->ltree
, c
->dtree
, s
, z
);
131 c
->mode
= r
== Z_STREAM_END
? WASH
: BADCODE
;
136 c
->sub
.code
.need
= c
->lbits
;
137 c
->sub
.code
.tree
= c
->ltree
;
139 case LEN
: /* i: get length/literal/eob next */
140 j
= c
->sub
.code
.need
;
142 t
= c
->sub
.code
.tree
+ ((uInt
)b
& inflate_mask
[j
]);
145 if (e
== 0) /* literal */
147 c
->sub
.lit
= t
->base
;
148 Tracevv((stderr
, t
->base
>= 0x20 && t
->base
< 0x7f ?
149 "inflate: literal '%c'\n" :
150 "inflate: literal 0x%02x\n", t
->base
));
154 if (e
& 16) /* length */
156 c
->sub
.copy
.get
= e
& 15;
161 if ((e
& 64) == 0) /* next table */
163 c
->sub
.code
.need
= e
;
164 c
->sub
.code
.tree
= t
+ t
->base
;
167 if (e
& 32) /* end of block */
169 Tracevv((stderr
, "inflate: end of block\n"));
173 c
->mode
= BADCODE
; /* invalid code */
174 z
->msg
= (char*)"invalid literal/length code";
177 case LENEXT
: /* i: getting length extra (have base) */
180 c
->len
+= (uInt
)b
& inflate_mask
[j
];
182 c
->sub
.code
.need
= c
->dbits
;
183 c
->sub
.code
.tree
= c
->dtree
;
184 Tracevv((stderr
, "inflate: length %u\n", c
->len
));
186 case DIST
: /* i: get distance next */
187 j
= c
->sub
.code
.need
;
189 t
= c
->sub
.code
.tree
+ ((uInt
)b
& inflate_mask
[j
]);
192 if (e
& 16) /* distance */
194 c
->sub
.copy
.get
= e
& 15;
195 c
->sub
.copy
.dist
= t
->base
;
199 if ((e
& 64) == 0) /* next table */
201 c
->sub
.code
.need
= e
;
202 c
->sub
.code
.tree
= t
+ t
->base
;
205 c
->mode
= BADCODE
; /* invalid code */
206 z
->msg
= (char*)"invalid distance code";
209 case DISTEXT
: /* i: getting distance extra */
212 c
->sub
.copy
.dist
+= (uInt
)b
& inflate_mask
[j
];
214 Tracevv((stderr
, "inflate: distance %u\n", c
->sub
.copy
.dist
));
216 case COPY
: /* o: copying bytes in window, waiting for space */
217 #ifndef __TURBOC__ /* Turbo C bug for following expression */
218 f
= (uInt
)(q
- s
->window
) < c
->sub
.copy
.dist
?
219 s
->end
- (c
->sub
.copy
.dist
- (q
- s
->window
)) :
220 q
- c
->sub
.copy
.dist
;
222 f
= q
- c
->sub
.copy
.dist
;
223 if ((uInt
)(q
- s
->window
) < c
->sub
.copy
.dist
)
224 f
= s
->end
- (c
->sub
.copy
.dist
- (uInt
)(q
- s
->window
));
236 case LIT
: /* o: got literal, waiting for output space */
241 case WASH
: /* o: got eob, possibly more output */
242 if (k
> 7) /* return unused byte, if any */
244 Assert(k
< 16, "inflate_codes grabbed too many bytes")
247 p
--; /* can always return one */
250 if (s
->read
!= s
->write
)
256 case BADCODE
: /* x: got error */
263 #ifdef NEED_DUMMY_RETURN
264 return Z_STREAM_ERROR
; /* Some dumb compilers complain without this */
269 void inflate_codes_free(c
, z
)
270 inflate_codes_statef
*c
;
274 Tracev((stderr
, "inflate: codes free\n"));