1 /* inflate.c -- zlib interface to inflate modules
2 * Copyright (C) 1995-1998 Mark Adler
3 * For conditions of distribution and use, see copyright notice in zlib.h
6 #include <linux/module.h>
7 #include <linux/zutil.h>
11 int zlib_inflate_workspacesize(void)
13 return sizeof(struct inflate_workspace
);
17 int zlib_inflateReset(
21 if (z
== NULL
|| z
->state
== NULL
|| z
->workspace
== NULL
)
22 return Z_STREAM_ERROR
;
23 z
->total_in
= z
->total_out
= 0;
25 z
->state
->mode
= z
->state
->nowrap
? BLOCKS
: METHOD
;
26 zlib_inflate_blocks_reset(z
->state
->blocks
, z
, NULL
);
35 if (z
== NULL
|| z
->state
== NULL
|| z
->workspace
== NULL
)
36 return Z_STREAM_ERROR
;
37 if (z
->state
->blocks
!= NULL
)
38 zlib_inflate_blocks_free(z
->state
->blocks
, z
);
44 int zlib_inflateInit2_(
51 if (version
== NULL
|| version
[0] != ZLIB_VERSION
[0] ||
52 stream_size
!= sizeof(z_stream
) || z
->workspace
== NULL
)
53 return Z_VERSION_ERROR
;
55 /* initialize state */
57 z
->state
= &WS(z
)->internal_state
;
58 z
->state
->blocks
= NULL
;
60 /* handle undocumented nowrap option (no zlib header or check) */
72 return Z_STREAM_ERROR
;
74 z
->state
->wbits
= (uInt
)w
;
76 /* create inflate_blocks state */
77 if ((z
->state
->blocks
=
78 zlib_inflate_blocks_new(z
, z
->state
->nowrap
? NULL
: zlib_adler32
, (uInt
)1 << w
))
92 * At the end of a Deflate-compressed PPP packet, we expect to have seen
93 * a `stored' block type value but not the (zero) length bytes.
95 static int zlib_inflate_packet_flush(inflate_blocks_statef
*s
)
104 int zlib_inflateInit_(
110 return zlib_inflateInit2_(z
, DEF_WBITS
, version
, stream_size
);
115 #define NEEDBYTE {if(z->avail_in==0)goto empty;r=trv;}
116 #define NEXTBYTE (z->avail_in--,z->total_in++,*z->next_in++)
126 if (z
== NULL
|| z
->state
== NULL
|| z
->next_in
== NULL
)
127 return Z_STREAM_ERROR
;
128 trv
= f
== Z_FINISH
? Z_BUF_ERROR
: Z_OK
;
130 while (1) switch (z
->state
->mode
)
134 if (((z
->state
->sub
.method
= NEXTBYTE
) & 0xf) != Z_DEFLATED
)
136 z
->state
->mode
= I_BAD
;
137 z
->msg
= (char*)"unknown compression method";
138 z
->state
->sub
.marker
= 5; /* can't try inflateSync */
141 if ((z
->state
->sub
.method
>> 4) + 8 > z
->state
->wbits
)
143 z
->state
->mode
= I_BAD
;
144 z
->msg
= (char*)"invalid window size";
145 z
->state
->sub
.marker
= 5; /* can't try inflateSync */
148 z
->state
->mode
= FLAG
;
152 if (((z
->state
->sub
.method
<< 8) + b
) % 31)
154 z
->state
->mode
= I_BAD
;
155 z
->msg
= (char*)"incorrect header check";
156 z
->state
->sub
.marker
= 5; /* can't try inflateSync */
159 if (!(b
& PRESET_DICT
))
161 z
->state
->mode
= BLOCKS
;
164 z
->state
->mode
= DICT4
;
167 z
->state
->sub
.check
.need
= (uLong
)NEXTBYTE
<< 24;
168 z
->state
->mode
= DICT3
;
171 z
->state
->sub
.check
.need
+= (uLong
)NEXTBYTE
<< 16;
172 z
->state
->mode
= DICT2
;
175 z
->state
->sub
.check
.need
+= (uLong
)NEXTBYTE
<< 8;
176 z
->state
->mode
= DICT1
;
179 z
->state
->sub
.check
.need
+= (uLong
)NEXTBYTE
;
180 z
->adler
= z
->state
->sub
.check
.need
;
181 z
->state
->mode
= DICT0
;
184 z
->state
->mode
= I_BAD
;
185 z
->msg
= (char*)"need dictionary";
186 z
->state
->sub
.marker
= 0; /* can try inflateSync */
187 return Z_STREAM_ERROR
;
189 r
= zlib_inflate_blocks(z
->state
->blocks
, z
, r
);
190 if (f
== Z_PACKET_FLUSH
&& z
->avail_in
== 0 && z
->avail_out
!= 0)
191 r
= zlib_inflate_packet_flush(z
->state
->blocks
);
192 if (r
== Z_DATA_ERROR
)
194 z
->state
->mode
= I_BAD
;
195 z
->state
->sub
.marker
= 0; /* can try inflateSync */
200 if (r
!= Z_STREAM_END
)
203 zlib_inflate_blocks_reset(z
->state
->blocks
, z
, &z
->state
->sub
.check
.was
);
204 if (z
->state
->nowrap
)
206 z
->state
->mode
= I_DONE
;
209 z
->state
->mode
= CHECK4
;
212 z
->state
->sub
.check
.need
= (uLong
)NEXTBYTE
<< 24;
213 z
->state
->mode
= CHECK3
;
216 z
->state
->sub
.check
.need
+= (uLong
)NEXTBYTE
<< 16;
217 z
->state
->mode
= CHECK2
;
220 z
->state
->sub
.check
.need
+= (uLong
)NEXTBYTE
<< 8;
221 z
->state
->mode
= CHECK1
;
224 z
->state
->sub
.check
.need
+= (uLong
)NEXTBYTE
;
226 if (z
->state
->sub
.check
.was
!= z
->state
->sub
.check
.need
)
228 z
->state
->mode
= I_BAD
;
229 z
->msg
= (char*)"incorrect data check";
230 z
->state
->sub
.marker
= 5; /* can't try inflateSync */
233 z
->state
->mode
= I_DONE
;
239 return Z_STREAM_ERROR
;
242 if (f
!= Z_PACKET_FLUSH
)
244 z
->state
->mode
= I_BAD
;
245 z
->msg
= (char *)"need more for packet flush";
246 z
->state
->sub
.marker
= 0; /* can try inflateSync */