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 return Z_STREAM_ERROR
;
59 z
->state
= &WS(z
)->internal_state
;
60 z
->state
->blocks
= NULL
;
62 /* handle undocumented nowrap option (no zlib header or check) */
74 return Z_STREAM_ERROR
;
76 z
->state
->wbits
= (uInt
)w
;
78 /* create inflate_blocks state */
79 if ((z
->state
->blocks
=
80 zlib_inflate_blocks_new(z
, z
->state
->nowrap
? NULL
: zlib_adler32
, (uInt
)1 << w
))
94 * At the end of a Deflate-compressed PPP packet, we expect to have seen
95 * a `stored' block type value but not the (zero) length bytes.
97 static int zlib_inflate_packet_flush(inflate_blocks_statef
*s
)
106 int zlib_inflateInit_(
112 return zlib_inflateInit2_(z
, DEF_WBITS
, version
, stream_size
);
117 #define NEEDBYTE {if(z->avail_in==0)goto empty;r=trv;}
118 #define NEXTBYTE (z->avail_in--,z->total_in++,*z->next_in++)
128 if (z
== NULL
|| z
->state
== NULL
|| z
->next_in
== NULL
)
129 return Z_STREAM_ERROR
;
130 trv
= f
== Z_FINISH
? Z_BUF_ERROR
: Z_OK
;
132 while (1) switch (z
->state
->mode
)
136 if (((z
->state
->sub
.method
= NEXTBYTE
) & 0xf) != Z_DEFLATED
)
138 z
->state
->mode
= I_BAD
;
139 z
->msg
= (char*)"unknown compression method";
140 z
->state
->sub
.marker
= 5; /* can't try inflateSync */
143 if ((z
->state
->sub
.method
>> 4) + 8 > z
->state
->wbits
)
145 z
->state
->mode
= I_BAD
;
146 z
->msg
= (char*)"invalid window size";
147 z
->state
->sub
.marker
= 5; /* can't try inflateSync */
150 z
->state
->mode
= FLAG
;
154 if (((z
->state
->sub
.method
<< 8) + b
) % 31)
156 z
->state
->mode
= I_BAD
;
157 z
->msg
= (char*)"incorrect header check";
158 z
->state
->sub
.marker
= 5; /* can't try inflateSync */
161 if (!(b
& PRESET_DICT
))
163 z
->state
->mode
= BLOCKS
;
166 z
->state
->mode
= DICT4
;
169 z
->state
->sub
.check
.need
= (uLong
)NEXTBYTE
<< 24;
170 z
->state
->mode
= DICT3
;
173 z
->state
->sub
.check
.need
+= (uLong
)NEXTBYTE
<< 16;
174 z
->state
->mode
= DICT2
;
177 z
->state
->sub
.check
.need
+= (uLong
)NEXTBYTE
<< 8;
178 z
->state
->mode
= DICT1
;
181 z
->state
->sub
.check
.need
+= (uLong
)NEXTBYTE
;
182 z
->adler
= z
->state
->sub
.check
.need
;
183 z
->state
->mode
= DICT0
;
186 z
->state
->mode
= I_BAD
;
187 z
->msg
= (char*)"need dictionary";
188 z
->state
->sub
.marker
= 0; /* can try inflateSync */
189 return Z_STREAM_ERROR
;
191 r
= zlib_inflate_blocks(z
->state
->blocks
, z
, r
);
192 if (f
== Z_PACKET_FLUSH
&& z
->avail_in
== 0 && z
->avail_out
!= 0)
193 r
= zlib_inflate_packet_flush(z
->state
->blocks
);
194 if (r
== Z_DATA_ERROR
)
196 z
->state
->mode
= I_BAD
;
197 z
->state
->sub
.marker
= 0; /* can try inflateSync */
202 if (r
!= Z_STREAM_END
)
205 zlib_inflate_blocks_reset(z
->state
->blocks
, z
, &z
->state
->sub
.check
.was
);
206 if (z
->state
->nowrap
)
208 z
->state
->mode
= I_DONE
;
211 z
->state
->mode
= CHECK4
;
214 z
->state
->sub
.check
.need
= (uLong
)NEXTBYTE
<< 24;
215 z
->state
->mode
= CHECK3
;
218 z
->state
->sub
.check
.need
+= (uLong
)NEXTBYTE
<< 16;
219 z
->state
->mode
= CHECK2
;
222 z
->state
->sub
.check
.need
+= (uLong
)NEXTBYTE
<< 8;
223 z
->state
->mode
= CHECK1
;
226 z
->state
->sub
.check
.need
+= (uLong
)NEXTBYTE
;
228 if (z
->state
->sub
.check
.was
!= z
->state
->sub
.check
.need
)
230 z
->state
->mode
= I_BAD
;
231 z
->msg
= (char*)"incorrect data check";
232 z
->state
->sub
.marker
= 5; /* can't try inflateSync */
235 z
->state
->mode
= I_DONE
;
241 return Z_STREAM_ERROR
;
244 if (f
!= Z_PACKET_FLUSH
)
246 z
->state
->mode
= I_BAD
;
247 z
->msg
= (char *)"need more for packet flush";
248 z
->state
->sub
.marker
= 0; /* can try inflateSync */
253 int zlib_inflateSync(
257 uInt n
; /* number of bytes to look at */
258 Byte
*p
; /* pointer to bytes */
259 uInt m
; /* number of marker bytes found in a row */
260 uLong r
, w
; /* temporaries to save total_in and total_out */
263 if (z
== NULL
|| z
->state
== NULL
)
264 return Z_STREAM_ERROR
;
265 if (z
->state
->mode
!= I_BAD
)
267 z
->state
->mode
= I_BAD
;
268 z
->state
->sub
.marker
= 0;
270 if ((n
= z
->avail_in
) == 0)
273 m
= z
->state
->sub
.marker
;
278 static const Byte mark
[4] = {0, 0, 0xff, 0xff};
289 z
->total_in
+= p
- z
->next_in
;
292 z
->state
->sub
.marker
= m
;
294 /* return no joy or set up to restart on a new block */
297 r
= z
->total_in
; w
= z
->total_out
;
298 zlib_inflateReset(z
);
299 z
->total_in
= r
; z
->total_out
= w
;
300 z
->state
->mode
= BLOCKS
;
305 /* Returns true if inflate is currently at the end of a block generated
306 * by Z_SYNC_FLUSH or Z_FULL_FLUSH. This function is used by one PPP
307 * implementation to provide an additional safety check. PPP uses Z_SYNC_FLUSH
308 * but removes the length bytes of the resulting empty stored block. When
309 * decompressing, PPP checks that at the end of input packet, inflate is
310 * waiting for these length bytes.
312 int zlib_inflateSyncPoint(
316 if (z
== NULL
|| z
->state
== NULL
|| z
->state
->blocks
== NULL
)
317 return Z_STREAM_ERROR
;
318 return zlib_inflate_blocks_sync_point(z
->state
->blocks
);
322 * This subroutine adds the data at next_in/avail_in to the output history
323 * without performing any output. The output buffer must be "caught up";
324 * i.e. no pending output (hence s->read equals s->write), and the state must
325 * be BLOCKS (i.e. we should be willing to see the start of a series of
326 * BLOCKS). On exit, the output will also be caught up, and the checksum
327 * will have been updated if need be.
329 static int zlib_inflate_addhistory(inflate_blocks_statef
*s
,
332 uLong b
; /* bit buffer */ /* NOT USED HERE */
333 uInt k
; /* bits in bit buffer */ /* NOT USED HERE */
334 uInt t
; /* temporary storage */
335 Byte
*p
; /* input data pointer */
336 uInt n
; /* bytes available there */
337 Byte
*q
; /* output window write pointer */
338 uInt m
; /* bytes to end of window or read pointer */
340 if (s
->read
!= s
->write
)
341 return Z_STREAM_ERROR
;
345 /* we're ready to rock */
347 /* while there is input ready, copy to output buffer, moving
348 * pointers as needed.
351 t
= n
; /* how many to do */
352 /* is there room until end of buffer? */
354 /* update check information */
355 if (s
->checkfn
!= NULL
)
356 s
->check
= (*s
->checkfn
)(s
->check
, q
, t
);
362 s
->read
= q
; /* drag read pointer forward */
363 /* WWRAP */ /* expand WWRAP macro by hand to handle s->read */
365 s
->read
= q
= s
->window
;
375 * This subroutine adds the data at next_in/avail_in to the output history
376 * without performing any output. The output buffer must be "caught up";
377 * i.e. no pending output (hence s->read equals s->write), and the state must
378 * be BLOCKS (i.e. we should be willing to see the start of a series of
379 * BLOCKS). On exit, the output will also be caught up, and the checksum
380 * will have been updated if need be.
383 int zlib_inflateIncomp(
388 if (z
->state
->mode
!= BLOCKS
)
390 return zlib_inflate_addhistory(z
->state
->blocks
, z
);