[ARM] Fix ARM suspend-to-RAM.
[linux-2.6/history.git] / lib / zlib_inflate / inflate.c
blob974a0fe85a9c139e00b9e8aae589b9db6bdbf993
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 if (z == NULL)
57 return Z_STREAM_ERROR;
58 z->msg = NULL;
59 z->state = &WS(z)->internal_state;
60 z->state->blocks = NULL;
62 /* handle undocumented nowrap option (no zlib header or check) */
63 z->state->nowrap = 0;
64 if (w < 0)
66 w = - w;
67 z->state->nowrap = 1;
70 /* set window size */
71 if (w < 8 || w > 15)
73 zlib_inflateEnd(z);
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))
81 == NULL)
83 zlib_inflateEnd(z);
84 return Z_MEM_ERROR;
87 /* reset state */
88 zlib_inflateReset(z);
89 return Z_OK;
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)
99 if (s->mode != LENS)
100 return Z_DATA_ERROR;
101 s->mode = TYPE;
102 return Z_OK;
106 int zlib_inflateInit_(
107 z_streamp z,
108 const char *version,
109 int stream_size
112 return zlib_inflateInit2_(z, DEF_WBITS, version, stream_size);
115 #undef NEEDBYTE
116 #undef NEXTBYTE
117 #define NEEDBYTE {if(z->avail_in==0)goto empty;r=trv;}
118 #define NEXTBYTE (z->avail_in--,z->total_in++,*z->next_in++)
120 int zlib_inflate(
121 z_streamp z,
122 int f
125 int r, trv;
126 uInt b;
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;
131 r = Z_BUF_ERROR;
132 while (1) switch (z->state->mode)
134 case METHOD:
135 NEEDBYTE
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 */
141 break;
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 */
148 break;
150 z->state->mode = FLAG;
151 case FLAG:
152 NEEDBYTE
153 b = NEXTBYTE;
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 */
159 break;
161 if (!(b & PRESET_DICT))
163 z->state->mode = BLOCKS;
164 break;
166 z->state->mode = DICT4;
167 case DICT4:
168 NEEDBYTE
169 z->state->sub.check.need = (uLong)NEXTBYTE << 24;
170 z->state->mode = DICT3;
171 case DICT3:
172 NEEDBYTE
173 z->state->sub.check.need += (uLong)NEXTBYTE << 16;
174 z->state->mode = DICT2;
175 case DICT2:
176 NEEDBYTE
177 z->state->sub.check.need += (uLong)NEXTBYTE << 8;
178 z->state->mode = DICT1;
179 case DICT1:
180 NEEDBYTE
181 z->state->sub.check.need += (uLong)NEXTBYTE;
182 z->adler = z->state->sub.check.need;
183 z->state->mode = DICT0;
184 return Z_NEED_DICT;
185 case 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;
190 case BLOCKS:
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 */
198 break;
200 if (r == Z_OK)
201 r = trv;
202 if (r != Z_STREAM_END)
203 return r;
204 r = trv;
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;
209 break;
211 z->state->mode = CHECK4;
212 case CHECK4:
213 NEEDBYTE
214 z->state->sub.check.need = (uLong)NEXTBYTE << 24;
215 z->state->mode = CHECK3;
216 case CHECK3:
217 NEEDBYTE
218 z->state->sub.check.need += (uLong)NEXTBYTE << 16;
219 z->state->mode = CHECK2;
220 case CHECK2:
221 NEEDBYTE
222 z->state->sub.check.need += (uLong)NEXTBYTE << 8;
223 z->state->mode = CHECK1;
224 case CHECK1:
225 NEEDBYTE
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 */
233 break;
235 z->state->mode = I_DONE;
236 case I_DONE:
237 return Z_STREAM_END;
238 case I_BAD:
239 return Z_DATA_ERROR;
240 default:
241 return Z_STREAM_ERROR;
243 empty:
244 if (f != Z_PACKET_FLUSH)
245 return r;
246 z->state->mode = I_BAD;
247 z->msg = (char *)"need more for packet flush";
248 z->state->sub.marker = 0; /* can try inflateSync */
249 return Z_DATA_ERROR;
253 int zlib_inflateSync(
254 z_streamp z
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 */
262 /* set up */
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)
271 return Z_BUF_ERROR;
272 p = z->next_in;
273 m = z->state->sub.marker;
275 /* search */
276 while (n && m < 4)
278 static const Byte mark[4] = {0, 0, 0xff, 0xff};
279 if (*p == mark[m])
280 m++;
281 else if (*p)
282 m = 0;
283 else
284 m = 4 - m;
285 p++, n--;
288 /* restore */
289 z->total_in += p - z->next_in;
290 z->next_in = p;
291 z->avail_in = n;
292 z->state->sub.marker = m;
294 /* return no joy or set up to restart on a new block */
295 if (m != 4)
296 return Z_DATA_ERROR;
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;
301 return Z_OK;
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(
313 z_streamp z
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,
330 z_stream *z)
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;
342 if (s->mode != TYPE)
343 return Z_DATA_ERROR;
345 /* we're ready to rock */
346 LOAD
347 /* while there is input ready, copy to output buffer, moving
348 * pointers as needed.
350 while (n) {
351 t = n; /* how many to do */
352 /* is there room until end of buffer? */
353 if (t > m) t = m;
354 /* update check information */
355 if (s->checkfn != NULL)
356 s->check = (*s->checkfn)(s->check, q, t);
357 memcpy(q, p, t);
358 q += t;
359 p += t;
360 n -= t;
361 z->total_out += t;
362 s->read = q; /* drag read pointer forward */
363 /* WWRAP */ /* expand WWRAP macro by hand to handle s->read */
364 if (q == s->end) {
365 s->read = q = s->window;
366 m = WAVAIL;
369 UPDATE
370 return Z_OK;
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(
384 z_stream *z
388 if (z->state->mode != BLOCKS)
389 return Z_DATA_ERROR;
390 return zlib_inflate_addhistory(z->state->blocks, z);