[TG3]: Add tagged status support.
[linux-2.6/verdex.git] / lib / zlib_inflate / inflate.c
blob3d94cb90c1d353ff2e4cdb708417ddfa62187796
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
4 */
6 #include <linux/module.h>
7 #include <linux/zutil.h>
8 #include "infblock.h"
9 #include "infutil.h"
11 int zlib_inflate_workspacesize(void)
13 return sizeof(struct inflate_workspace);
17 int zlib_inflateReset(
18 z_streamp z
21 if (z == NULL || z->state == NULL || z->workspace == NULL)
22 return Z_STREAM_ERROR;
23 z->total_in = z->total_out = 0;
24 z->msg = NULL;
25 z->state->mode = z->state->nowrap ? BLOCKS : METHOD;
26 zlib_inflate_blocks_reset(z->state->blocks, z, NULL);
27 return Z_OK;
31 int zlib_inflateEnd(
32 z_streamp z
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);
39 z->state = NULL;
40 return Z_OK;
44 int zlib_inflateInit2_(
45 z_streamp z,
46 int w,
47 const char *version,
48 int stream_size
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 */
56 z->msg = NULL;
57 z->state = &WS(z)->internal_state;
58 z->state->blocks = NULL;
60 /* handle undocumented nowrap option (no zlib header or check) */
61 z->state->nowrap = 0;
62 if (w < 0)
64 w = - w;
65 z->state->nowrap = 1;
68 /* set window size */
69 if (w < 8 || w > 15)
71 zlib_inflateEnd(z);
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))
79 == NULL)
81 zlib_inflateEnd(z);
82 return Z_MEM_ERROR;
85 /* reset state */
86 zlib_inflateReset(z);
87 return Z_OK;
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)
97 if (s->mode != LENS)
98 return Z_DATA_ERROR;
99 s->mode = TYPE;
100 return Z_OK;
104 int zlib_inflateInit_(
105 z_streamp z,
106 const char *version,
107 int stream_size
110 return zlib_inflateInit2_(z, DEF_WBITS, version, stream_size);
113 #undef NEEDBYTE
114 #undef NEXTBYTE
115 #define NEEDBYTE {if(z->avail_in==0)goto empty;r=trv;}
116 #define NEXTBYTE (z->avail_in--,z->total_in++,*z->next_in++)
118 int zlib_inflate(
119 z_streamp z,
120 int f
123 int r, trv;
124 uInt b;
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;
129 r = Z_BUF_ERROR;
130 while (1) switch (z->state->mode)
132 case METHOD:
133 NEEDBYTE
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 */
139 break;
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 */
146 break;
148 z->state->mode = FLAG;
149 case FLAG:
150 NEEDBYTE
151 b = NEXTBYTE;
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 */
157 break;
159 if (!(b & PRESET_DICT))
161 z->state->mode = BLOCKS;
162 break;
164 z->state->mode = DICT4;
165 case DICT4:
166 NEEDBYTE
167 z->state->sub.check.need = (uLong)NEXTBYTE << 24;
168 z->state->mode = DICT3;
169 case DICT3:
170 NEEDBYTE
171 z->state->sub.check.need += (uLong)NEXTBYTE << 16;
172 z->state->mode = DICT2;
173 case DICT2:
174 NEEDBYTE
175 z->state->sub.check.need += (uLong)NEXTBYTE << 8;
176 z->state->mode = DICT1;
177 case DICT1:
178 NEEDBYTE
179 z->state->sub.check.need += (uLong)NEXTBYTE;
180 z->adler = z->state->sub.check.need;
181 z->state->mode = DICT0;
182 return Z_NEED_DICT;
183 case 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;
188 case BLOCKS:
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 */
196 break;
198 if (r == Z_OK)
199 r = trv;
200 if (r != Z_STREAM_END)
201 return r;
202 r = trv;
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;
207 break;
209 z->state->mode = CHECK4;
210 case CHECK4:
211 NEEDBYTE
212 z->state->sub.check.need = (uLong)NEXTBYTE << 24;
213 z->state->mode = CHECK3;
214 case CHECK3:
215 NEEDBYTE
216 z->state->sub.check.need += (uLong)NEXTBYTE << 16;
217 z->state->mode = CHECK2;
218 case CHECK2:
219 NEEDBYTE
220 z->state->sub.check.need += (uLong)NEXTBYTE << 8;
221 z->state->mode = CHECK1;
222 case CHECK1:
223 NEEDBYTE
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 */
231 break;
233 z->state->mode = I_DONE;
234 case I_DONE:
235 return Z_STREAM_END;
236 case I_BAD:
237 return Z_DATA_ERROR;
238 default:
239 return Z_STREAM_ERROR;
241 empty:
242 if (f != Z_PACKET_FLUSH)
243 return r;
244 z->state->mode = I_BAD;
245 z->msg = (char *)"need more for packet flush";
246 z->state->sub.marker = 0; /* can try inflateSync */
247 return Z_DATA_ERROR;