ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[linux-2.6.git] / lib / zlib_inflate / inflate.c
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  */
5
6 #include <linux/module.h>
7 #include <linux/zutil.h>
8 #include "infblock.h"
9 #include "infutil.h"
10
11 int zlib_inflate_workspacesize(void)
12 {
13   return sizeof(struct inflate_workspace);
14 }
15
16
17 int zlib_inflateReset(
18         z_streamp z
19 )
20 {
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;
28 }
29
30
31 int zlib_inflateEnd(
32         z_streamp z
33 )
34 {
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;
41 }
42
43
44 int zlib_inflateInit2_(
45         z_streamp z,
46         int w,
47         const char *version,
48         int stream_size
49 )
50 {
51   if (version == NULL || version[0] != ZLIB_VERSION[0] ||
52       stream_size != sizeof(z_stream) || z->workspace == NULL)
53       return Z_VERSION_ERROR;
54
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;
61
62   /* handle undocumented nowrap option (no zlib header or check) */
63   z->state->nowrap = 0;
64   if (w < 0)
65   {
66     w = - w;
67     z->state->nowrap = 1;
68   }
69
70   /* set window size */
71   if (w < 8 || w > 15)
72   {
73     zlib_inflateEnd(z);
74     return Z_STREAM_ERROR;
75   }
76   z->state->wbits = (uInt)w;
77
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)
82   {
83     zlib_inflateEnd(z);
84     return Z_MEM_ERROR;
85   }
86
87   /* reset state */
88   zlib_inflateReset(z);
89   return Z_OK;
90 }
91
92
93 /*
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.
96  */
97 static int zlib_inflate_packet_flush(inflate_blocks_statef *s)
98 {
99     if (s->mode != LENS)
100         return Z_DATA_ERROR;
101     s->mode = TYPE;
102     return Z_OK;
103 }
104
105
106 int zlib_inflateInit_(
107         z_streamp z,
108         const char *version,
109         int stream_size
110 )
111 {
112   return zlib_inflateInit2_(z, DEF_WBITS, version, stream_size);
113 }
114
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++)
119
120 int zlib_inflate(
121         z_streamp z,
122         int f
123 )
124 {
125   int r, trv;
126   uInt b;
127
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)
133   {
134     case METHOD:
135       NEEDBYTE
136       if (((z->state->sub.method = NEXTBYTE) & 0xf) != Z_DEFLATED)
137       {
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;
142       }
143       if ((z->state->sub.method >> 4) + 8 > z->state->wbits)
144       {
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;
149       }
150       z->state->mode = FLAG;
151     case FLAG:
152       NEEDBYTE
153       b = NEXTBYTE;
154       if (((z->state->sub.method << 8) + b) % 31)
155       {
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;
160       }
161       if (!(b & PRESET_DICT))
162       {
163         z->state->mode = BLOCKS;
164         break;
165       }
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)
195       {
196         z->state->mode = I_BAD;
197         z->state->sub.marker = 0;       /* can try inflateSync */
198         break;
199       }
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)
207       {
208         z->state->mode = I_DONE;
209         break;
210       }
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;
227
228       if (z->state->sub.check.was != z->state->sub.check.need)
229       {
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;
234       }
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;
242   }
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;
250 }
251
252
253 int zlib_inflateSync(
254         z_streamp z
255 )
256 {
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 */
261
262   /* set up */
263   if (z == NULL || z->state == NULL)
264     return Z_STREAM_ERROR;
265   if (z->state->mode != I_BAD)
266   {
267     z->state->mode = I_BAD;
268     z->state->sub.marker = 0;
269   }
270   if ((n = z->avail_in) == 0)
271     return Z_BUF_ERROR;
272   p = z->next_in;
273   m = z->state->sub.marker;
274
275   /* search */
276   while (n && m < 4)
277   {
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--;
286   }
287
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;
293
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;
302 }
303
304
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.
311  */
312 int zlib_inflateSyncPoint(
313         z_streamp z
314 )
315 {
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);
319 }
320
321 /*
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.
328  */
329 static int zlib_inflate_addhistory(inflate_blocks_statef *s,
330                                       z_stream              *z)
331 {
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 */
339
340     if (s->read != s->write)
341         return Z_STREAM_ERROR;
342     if (s->mode != TYPE)
343         return Z_DATA_ERROR;
344
345     /* we're ready to rock */
346     LOAD
347     /* while there is input ready, copy to output buffer, moving
348      * pointers as needed.
349      */
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;
367         }
368     }
369     UPDATE
370     return Z_OK;
371 }
372
373
374 /*
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.
381  */
382
383 int zlib_inflateIncomp(
384         z_stream *z
385         
386 )
387 {
388     if (z->state->mode != BLOCKS)
389         return Z_DATA_ERROR;
390     return zlib_inflate_addhistory(z->state->blocks, z);
391 }