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 ZEXPORT zlib_inflate_workspacesize(void)
13 return sizeof(struct inflate_workspace);
17 int ZEXPORT zlib_inflateReset(z)
20 if (z == Z_NULL || z->state == Z_NULL || z->workspace == Z_NULL)
21 return Z_STREAM_ERROR;
22 z->total_in = z->total_out = 0;
24 z->state->mode = z->state->nowrap ? BLOCKS : METHOD;
25 zlib_inflate_blocks_reset(z->state->blocks, z, Z_NULL);
30 int ZEXPORT zlib_inflateEnd(z)
33 if (z == Z_NULL || z->state == Z_NULL || z->workspace == Z_NULL)
34 return Z_STREAM_ERROR;
35 if (z->state->blocks != Z_NULL)
36 zlib_inflate_blocks_free(z->state->blocks, z);
42 int ZEXPORT zlib_inflateInit2_(z, w, version, stream_size)
48 if (version == Z_NULL || version[0] != ZLIB_VERSION[0] ||
49 stream_size != sizeof(z_stream) || z->workspace == Z_NULL)
50 return Z_VERSION_ERROR;
52 /* initialize state */
54 return Z_STREAM_ERROR;
56 z->state = &WS(z)->internal_state;
57 z->state->blocks = Z_NULL;
59 /* handle undocumented nowrap option (no zlib header or check) */
71 return Z_STREAM_ERROR;
73 z->state->wbits = (uInt)w;
75 /* create inflate_blocks state */
76 if ((z->state->blocks =
77 zlib_inflate_blocks_new(z, z->state->nowrap ? Z_NULL : zlib_adler32, (uInt)1 << w))
91 * At the end of a Deflate-compressed PPP packet, we expect to have seen
92 * a `stored' block type value but not the (zero) length bytes.
94 static int zlib_inflate_packet_flush(inflate_blocks_statef *s)
103 int ZEXPORT zlib_inflateInit_(z, version, stream_size)
108 return zlib_inflateInit2_(z, DEF_WBITS, version, stream_size);
113 #define NEEDBYTE {if(z->avail_in==0)goto empty;r=trv;}
114 #define NEXTBYTE (z->avail_in--,z->total_in++,*z->next_in++)
116 int ZEXPORT zlib_inflate(z, f)
123 if (z == Z_NULL || z->state == Z_NULL || z->next_in == Z_NULL)
124 return Z_STREAM_ERROR;
125 trv = f == Z_FINISH ? Z_BUF_ERROR : Z_OK;
127 while (1) switch (z->state->mode)
131 if (((z->state->sub.method = NEXTBYTE) & 0xf) != Z_DEFLATED)
133 z->state->mode = I_BAD;
134 z->msg = (char*)"unknown compression method";
135 z->state->sub.marker = 5; /* can't try inflateSync */
138 if ((z->state->sub.method >> 4) + 8 > z->state->wbits)
140 z->state->mode = I_BAD;
141 z->msg = (char*)"invalid window size";
142 z->state->sub.marker = 5; /* can't try inflateSync */
145 z->state->mode = FLAG;
149 if (((z->state->sub.method << 8) + b) % 31)
151 z->state->mode = I_BAD;
152 z->msg = (char*)"incorrect header check";
153 z->state->sub.marker = 5; /* can't try inflateSync */
156 if (!(b & PRESET_DICT))
158 z->state->mode = BLOCKS;
161 z->state->mode = DICT4;
164 z->state->sub.check.need = (uLong)NEXTBYTE << 24;
165 z->state->mode = DICT3;
168 z->state->sub.check.need += (uLong)NEXTBYTE << 16;
169 z->state->mode = DICT2;
172 z->state->sub.check.need += (uLong)NEXTBYTE << 8;
173 z->state->mode = DICT1;
176 z->state->sub.check.need += (uLong)NEXTBYTE;
177 z->adler = z->state->sub.check.need;
178 z->state->mode = DICT0;
181 z->state->mode = I_BAD;
182 z->msg = (char*)"need dictionary";
183 z->state->sub.marker = 0; /* can try inflateSync */
184 return Z_STREAM_ERROR;
186 r = zlib_inflate_blocks(z->state->blocks, z, r);
187 if (f == Z_PACKET_FLUSH && z->avail_in == 0 && z->avail_out != 0)
188 r = zlib_inflate_packet_flush(z->state->blocks);
189 if (r == Z_DATA_ERROR)
191 z->state->mode = I_BAD;
192 z->state->sub.marker = 0; /* can try inflateSync */
197 if (r != Z_STREAM_END)
200 zlib_inflate_blocks_reset(z->state->blocks, z, &z->state->sub.check.was);
201 if (z->state->nowrap)
203 z->state->mode = I_DONE;
206 z->state->mode = CHECK4;
209 z->state->sub.check.need = (uLong)NEXTBYTE << 24;
210 z->state->mode = CHECK3;
213 z->state->sub.check.need += (uLong)NEXTBYTE << 16;
214 z->state->mode = CHECK2;
217 z->state->sub.check.need += (uLong)NEXTBYTE << 8;
218 z->state->mode = CHECK1;
221 z->state->sub.check.need += (uLong)NEXTBYTE;
223 if (z->state->sub.check.was != z->state->sub.check.need)
225 z->state->mode = I_BAD;
226 z->msg = (char*)"incorrect data check";
227 z->state->sub.marker = 5; /* can't try inflateSync */
230 z->state->mode = I_DONE;
236 return Z_STREAM_ERROR;
239 if (f != Z_PACKET_FLUSH)
241 z->state->mode = I_BAD;
242 z->msg = (char *)"need more for packet flush";
243 z->state->sub.marker = 0; /* can try inflateSync */
248 int ZEXPORT zlib_inflateSync(z)
251 uInt n; /* number of bytes to look at */
252 Bytef *p; /* pointer to bytes */
253 uInt m; /* number of marker bytes found in a row */
254 uLong r, w; /* temporaries to save total_in and total_out */
257 if (z == Z_NULL || z->state == Z_NULL)
258 return Z_STREAM_ERROR;
259 if (z->state->mode != I_BAD)
261 z->state->mode = I_BAD;
262 z->state->sub.marker = 0;
264 if ((n = z->avail_in) == 0)
267 m = z->state->sub.marker;
272 static const Byte mark[4] = {0, 0, 0xff, 0xff};
283 z->total_in += p - z->next_in;
286 z->state->sub.marker = m;
288 /* return no joy or set up to restart on a new block */
291 r = z->total_in; w = z->total_out;
292 zlib_inflateReset(z);
293 z->total_in = r; z->total_out = w;
294 z->state->mode = BLOCKS;
299 /* Returns true if inflate is currently at the end of a block generated
300 * by Z_SYNC_FLUSH or Z_FULL_FLUSH. This function is used by one PPP
301 * implementation to provide an additional safety check. PPP uses Z_SYNC_FLUSH
302 * but removes the length bytes of the resulting empty stored block. When
303 * decompressing, PPP checks that at the end of input packet, inflate is
304 * waiting for these length bytes.
306 int ZEXPORT zlib_inflateSyncPoint(z)
309 if (z == Z_NULL || z->state == Z_NULL || z->state->blocks == Z_NULL)
310 return Z_STREAM_ERROR;
311 return zlib_inflate_blocks_sync_point(z->state->blocks);
315 * This subroutine adds the data at next_in/avail_in to the output history
316 * without performing any output. The output buffer must be "caught up";
317 * i.e. no pending output (hence s->read equals s->write), and the state must
318 * be BLOCKS (i.e. we should be willing to see the start of a series of
319 * BLOCKS). On exit, the output will also be caught up, and the checksum
320 * will have been updated if need be.
322 static int zlib_inflate_addhistory(inflate_blocks_statef *s,
325 uLong b; /* bit buffer */ /* NOT USED HERE */
326 uInt k; /* bits in bit buffer */ /* NOT USED HERE */
327 uInt t; /* temporary storage */
328 Bytef *p; /* input data pointer */
329 uInt n; /* bytes available there */
330 Bytef *q; /* output window write pointer */
331 uInt m; /* bytes to end of window or read pointer */
333 if (s->read != s->write)
334 return Z_STREAM_ERROR;
338 /* we're ready to rock */
340 /* while there is input ready, copy to output buffer, moving
341 * pointers as needed.
344 t = n; /* how many to do */
345 /* is there room until end of buffer? */
347 /* update check information */
348 if (s->checkfn != Z_NULL)
349 s->check = (*s->checkfn)(s->check, q, t);
355 s->read = q; /* drag read pointer forward */
356 /* WWRAP */ /* expand WWRAP macro by hand to handle s->read */
358 s->read = q = s->window;
368 * This subroutine adds the data at next_in/avail_in to the output history
369 * without performing any output. The output buffer must be "caught up";
370 * i.e. no pending output (hence s->read equals s->write), and the state must
371 * be BLOCKS (i.e. we should be willing to see the start of a series of
372 * BLOCKS). On exit, the output will also be caught up, and the checksum
373 * will have been updated if need be.
376 int ZEXPORT zlib_inflateIncomp(z)
379 if (z->state->mode != BLOCKS)
381 return zlib_inflate_addhistory(z->state->blocks, z);