1 /* infcodes.c -- process literals and length/distance pairs
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 /* simplify the use of the inflate_huft type with some defines */
14 #define exop word.what.Exop
15 #define bits word.what.Bits
17 inflate_codes_statef
*zlib_inflate_codes_new(
21 inflate_huft
*td
, /* need separate declaration for Borland C++ */
25 inflate_codes_statef
*c
;
27 c
= &WS(z
)->working_state
;
39 int zlib_inflate_codes(
40 inflate_blocks_statef
*s
,
45 uInt j
; /* temporary storage */
46 inflate_huft
*t
; /* temporary pointer */
47 uInt e
; /* extra bits or operation */
48 uLong b
; /* bit buffer */
49 uInt k
; /* bits in bit buffer */
50 Byte
*p
; /* input data pointer */
51 uInt n
; /* bytes available there */
52 Byte
*q
; /* output window write pointer */
53 uInt m
; /* bytes to end of window or read pointer */
54 Byte
*f
; /* pointer to copy strings from */
55 inflate_codes_statef
*c
= s
->sub
.decode
.codes
; /* codes state */
57 /* copy input/output information to locals (UPDATE macro restores) */
60 /* process input and output based on current state */
61 while (1) switch (c
->mode
)
62 { /* waiting for "i:"=input, "o:"=output, "x:"=nothing */
63 case START
: /* x: set up for LEN */
65 if (m
>= 258 && n
>= 10)
68 r
= zlib_inflate_fast(c
->lbits
, c
->dbits
, c
->ltree
, c
->dtree
, s
, z
);
72 c
->mode
= r
== Z_STREAM_END
? WASH
: BADCODE
;
77 c
->sub
.code
.need
= c
->lbits
;
78 c
->sub
.code
.tree
= c
->ltree
;
80 case LEN
: /* i: get length/literal/eob next */
83 t
= c
->sub
.code
.tree
+ ((uInt
)b
& zlib_inflate_mask
[j
]);
86 if (e
== 0) /* literal */
92 if (e
& 16) /* length */
94 c
->sub
.copy
.get
= e
& 15;
99 if ((e
& 64) == 0) /* next table */
101 c
->sub
.code
.need
= e
;
102 c
->sub
.code
.tree
= t
+ t
->base
;
105 if (e
& 32) /* end of block */
110 c
->mode
= BADCODE
; /* invalid code */
111 z
->msg
= (char*)"invalid literal/length code";
114 case LENEXT
: /* i: getting length extra (have base) */
117 c
->len
+= (uInt
)b
& zlib_inflate_mask
[j
];
119 c
->sub
.code
.need
= c
->dbits
;
120 c
->sub
.code
.tree
= c
->dtree
;
122 case DIST
: /* i: get distance next */
123 j
= c
->sub
.code
.need
;
125 t
= c
->sub
.code
.tree
+ ((uInt
)b
& zlib_inflate_mask
[j
]);
128 if (e
& 16) /* distance */
130 c
->sub
.copy
.get
= e
& 15;
131 c
->sub
.copy
.dist
= t
->base
;
135 if ((e
& 64) == 0) /* next table */
137 c
->sub
.code
.need
= e
;
138 c
->sub
.code
.tree
= t
+ t
->base
;
141 c
->mode
= BADCODE
; /* invalid code */
142 z
->msg
= (char*)"invalid distance code";
145 case DISTEXT
: /* i: getting distance extra */
148 c
->sub
.copy
.dist
+= (uInt
)b
& zlib_inflate_mask
[j
];
151 case COPY
: /* o: copying bytes in window, waiting for space */
152 f
= q
- c
->sub
.copy
.dist
;
153 while (f
< s
->window
) /* modulo window size-"while" instead */
154 f
+= s
->end
- s
->window
; /* of "if" handles invalid distances */
165 case LIT
: /* o: got literal, waiting for output space */
170 case WASH
: /* o: got eob, possibly more output */
171 if (k
> 7) /* return unused byte, if any */
175 p
--; /* can always return one */
178 if (s
->read
!= s
->write
)
184 case BADCODE
: /* x: got error */
191 #ifdef NEED_DUMMY_RETURN
192 return Z_STREAM_ERROR
; /* Some dumb compilers complain without this */
197 void zlib_inflate_codes_free(
198 inflate_codes_statef
*c
,