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/zutil.h>
10 int zlib_inflate_workspacesize(void)
12 return sizeof(struct inflate_workspace);
16 int zlib_inflateReset(
20 if (z == NULL || z->state == NULL || z->workspace == 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, NULL);
34 if (z == NULL || z->state == NULL || z->workspace == NULL)
35 return Z_STREAM_ERROR;
36 if (z->state->blocks != NULL)
37 zlib_inflate_blocks_free(z->state->blocks, z);
43 int zlib_inflateInit2_(
50 if (version == NULL || version[0] != ZLIB_VERSION[0] ||
51 stream_size != sizeof(z_stream) || z->workspace == NULL)
52 return Z_VERSION_ERROR;
54 /* initialize state */
56 z->state = &WS(z)->internal_state;
57 z->state->blocks = 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 ? 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 zlib_inflateInit_(
109 return zlib_inflateInit2_(z, DEF_WBITS, version, stream_size);
114 #define NEEDBYTE {if(z->avail_in==0)goto empty;r=trv;}
115 #define NEXTBYTE (z->avail_in--,z->total_in++,*z->next_in++)
125 if (z == NULL || z->state == NULL || z->next_in == NULL)
126 return Z_STREAM_ERROR;
127 trv = f == Z_FINISH ? Z_BUF_ERROR : Z_OK;
129 while (1) switch (z->state->mode)
133 if (((z->state->sub.method = NEXTBYTE) & 0xf) != Z_DEFLATED)
135 z->state->mode = I_BAD;
136 z->msg = (char*)"unknown compression method";
137 z->state->sub.marker = 5; /* can't try inflateSync */
140 if ((z->state->sub.method >> 4) + 8 > z->state->wbits)
142 z->state->mode = I_BAD;
143 z->msg = (char*)"invalid window size";
144 z->state->sub.marker = 5; /* can't try inflateSync */
147 z->state->mode = FLAG;
151 if (((z->state->sub.method << 8) + b) % 31)
153 z->state->mode = I_BAD;
154 z->msg = (char*)"incorrect header check";
155 z->state->sub.marker = 5; /* can't try inflateSync */
158 if (!(b & PRESET_DICT))
160 z->state->mode = BLOCKS;
163 z->state->mode = DICT4;
166 z->state->sub.check.need = (uLong)NEXTBYTE << 24;
167 z->state->mode = DICT3;
170 z->state->sub.check.need += (uLong)NEXTBYTE << 16;
171 z->state->mode = DICT2;
174 z->state->sub.check.need += (uLong)NEXTBYTE << 8;
175 z->state->mode = DICT1;
178 z->state->sub.check.need += (uLong)NEXTBYTE;
179 z->adler = z->state->sub.check.need;
180 z->state->mode = DICT0;
183 z->state->mode = I_BAD;
184 z->msg = (char*)"need dictionary";
185 z->state->sub.marker = 0; /* can try inflateSync */
186 return Z_STREAM_ERROR;
188 r = zlib_inflate_blocks(z->state->blocks, z, r);
189 if (f == Z_PACKET_FLUSH && z->avail_in == 0 && z->avail_out != 0)
190 r = zlib_inflate_packet_flush(z->state->blocks);
191 if (r == Z_DATA_ERROR)
193 z->state->mode = I_BAD;
194 z->state->sub.marker = 0; /* can try inflateSync */
199 if (r != Z_STREAM_END)
202 zlib_inflate_blocks_reset(z->state->blocks, z, &z->state->sub.check.was);
203 if (z->state->nowrap)
205 z->state->mode = I_DONE;
208 z->state->mode = CHECK4;
211 z->state->sub.check.need = (uLong)NEXTBYTE << 24;
212 z->state->mode = CHECK3;
215 z->state->sub.check.need += (uLong)NEXTBYTE << 16;
216 z->state->mode = CHECK2;
219 z->state->sub.check.need += (uLong)NEXTBYTE << 8;
220 z->state->mode = CHECK1;
223 z->state->sub.check.need += (uLong)NEXTBYTE;
225 if (z->state->sub.check.was != z->state->sub.check.need)
227 z->state->mode = I_BAD;
228 z->msg = (char*)"incorrect data check";
229 z->state->sub.marker = 5; /* can't try inflateSync */
232 z->state->mode = I_DONE;
238 return Z_STREAM_ERROR;
241 if (f != Z_PACKET_FLUSH)
243 z->state->mode = I_BAD;
244 z->msg = (char *)"need more for packet flush";
245 z->state->sub.marker = 0; /* can try inflateSync */