9c790b98eae76397f86f15a8103eccb63b932f86
[linux-2.4.git] / 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 ZEXPORT zlib_inflate_workspacesize(void)
12 {
13   return sizeof(struct inflate_workspace);
14 }
15
16
17 int ZEXPORT zlib_inflateReset(z)
18 z_streamp z;
19 {
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;
23   z->msg = Z_NULL;
24   z->state->mode = z->state->nowrap ? BLOCKS : METHOD;
25   zlib_inflate_blocks_reset(z->state->blocks, z, Z_NULL);
26   return Z_OK;
27 }
28
29
30 int ZEXPORT zlib_inflateEnd(z)
31 z_streamp z;
32 {
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);
37   z->state = Z_NULL;
38   return Z_OK;
39 }
40
41
42 int ZEXPORT zlib_inflateInit2_(z, w, version, stream_size)
43 z_streamp z;
44 int w;
45 const char *version;
46 int stream_size;
47 {
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;
51
52   /* initialize state */
53   if (z == Z_NULL)
54     return Z_STREAM_ERROR;
55   z->msg = Z_NULL;
56   z->state = &WS(z)->internal_state;
57   z->state->blocks = Z_NULL;
58
59   /* handle undocumented nowrap option (no zlib header or check) */
60   z->state->nowrap = 0;
61   if (w < 0)
62   {
63     w = - w;
64     z->state->nowrap = 1;
65   }
66
67   /* set window size */
68   if (w < 8 || w > 15)
69   {
70     zlib_inflateEnd(z);
71     return Z_STREAM_ERROR;
72   }
73   z->state->wbits = (uInt)w;
74
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))
78       == Z_NULL)
79   {
80     zlib_inflateEnd(z);
81     return Z_MEM_ERROR;
82   }
83
84   /* reset state */
85   zlib_inflateReset(z);
86   return Z_OK;
87 }
88
89
90 /*
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.
93  */
94 static int zlib_inflate_packet_flush(inflate_blocks_statef *s)
95 {
96     if (s->mode != LENS)
97         return Z_DATA_ERROR;
98     s->mode = TYPE;
99     return Z_OK;
100 }
101
102
103 int ZEXPORT zlib_inflateInit_(z, version, stream_size)
104 z_streamp z;
105 const char *version;
106 int stream_size;
107 {
108   return zlib_inflateInit2_(z, DEF_WBITS, version, stream_size);
109 }
110
111 #undef NEEDBYTE
112 #undef NEXTBYTE
113 #define NEEDBYTE {if(z->avail_in==0)goto empty;r=trv;}
114 #define NEXTBYTE (z->avail_in--,z->total_in++,*z->next_in++)
115
116 int ZEXPORT zlib_inflate(z, f)
117 z_streamp z;
118 int f;
119 {
120   int r, trv;
121   uInt b;
122
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;
126   r = Z_BUF_ERROR;
127   while (1) switch (z->state->mode)
128   {
129     case METHOD:
130       NEEDBYTE
131       if (((z->state->sub.method = NEXTBYTE) & 0xf) != Z_DEFLATED)
132       {
133         z->state->mode = I_BAD;
134         z->msg = (char*)"unknown compression method";
135         z->state->sub.marker = 5;       /* can't try inflateSync */
136         break;
137       }
138       if ((z->state->sub.method >> 4) + 8 > z->state->wbits)
139       {
140         z->state->mode = I_BAD;
141         z->msg = (char*)"invalid window size";
142         z->state->sub.marker = 5;       /* can't try inflateSync */
143         break;
144       }
145       z->state->mode = FLAG;
146     case FLAG:
147       NEEDBYTE
148       b = NEXTBYTE;
149       if (((z->state->sub.method << 8) + b) % 31)
150       {
151         z->state->mode = I_BAD;
152         z->msg = (char*)"incorrect header check";
153         z->state->sub.marker = 5;       /* can't try inflateSync */
154         break;
155       }
156       if (!(b & PRESET_DICT))
157       {
158         z->state->mode = BLOCKS;
159         break;
160       }
161       z->state->mode = DICT4;
162     case DICT4:
163       NEEDBYTE
164       z->state->sub.check.need = (uLong)NEXTBYTE << 24;
165       z->state->mode = DICT3;
166     case DICT3:
167       NEEDBYTE
168       z->state->sub.check.need += (uLong)NEXTBYTE << 16;
169       z->state->mode = DICT2;
170     case DICT2:
171       NEEDBYTE
172       z->state->sub.check.need += (uLong)NEXTBYTE << 8;
173       z->state->mode = DICT1;
174     case DICT1:
175       NEEDBYTE
176       z->state->sub.check.need += (uLong)NEXTBYTE;
177       z->adler = z->state->sub.check.need;
178       z->state->mode = DICT0;
179       return Z_NEED_DICT;
180     case 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;
185     case BLOCKS:
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)
190       {
191         z->state->mode = I_BAD;
192         z->state->sub.marker = 0;       /* can try inflateSync */
193         break;
194       }
195       if (r == Z_OK)
196         r = trv;
197       if (r != Z_STREAM_END)
198         return r;
199       r = trv;
200       zlib_inflate_blocks_reset(z->state->blocks, z, &z->state->sub.check.was);
201       if (z->state->nowrap)
202       {
203         z->state->mode = I_DONE;
204         break;
205       }
206       z->state->mode = CHECK4;
207     case CHECK4:
208       NEEDBYTE
209       z->state->sub.check.need = (uLong)NEXTBYTE << 24;
210       z->state->mode = CHECK3;
211     case CHECK3:
212       NEEDBYTE
213       z->state->sub.check.need += (uLong)NEXTBYTE << 16;
214       z->state->mode = CHECK2;
215     case CHECK2:
216       NEEDBYTE
217       z->state->sub.check.need += (uLong)NEXTBYTE << 8;
218       z->state->mode = CHECK1;
219     case CHECK1:
220       NEEDBYTE
221       z->state->sub.check.need += (uLong)NEXTBYTE;
222
223       if (z->state->sub.check.was != z->state->sub.check.need)
224       {
225         z->state->mode = I_BAD;
226         z->msg = (char*)"incorrect data check";
227         z->state->sub.marker = 5;       /* can't try inflateSync */
228         break;
229       }
230       z->state->mode = I_DONE;
231     case I_DONE:
232       return Z_STREAM_END;
233     case I_BAD:
234       return Z_DATA_ERROR;
235     default:
236       return Z_STREAM_ERROR;
237   }
238  empty:
239   if (f != Z_PACKET_FLUSH)
240     return r;
241   z->state->mode = I_BAD;
242   z->msg = (char *)"need more for packet flush";
243   z->state->sub.marker = 0;       /* can try inflateSync */
244   return Z_DATA_ERROR;
245 }
246
247
248 int ZEXPORT zlib_inflateSync(z)
249 z_streamp z;
250 {
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 */
255
256   /* set up */
257   if (z == Z_NULL || z->state == Z_NULL)
258     return Z_STREAM_ERROR;
259   if (z->state->mode != I_BAD)
260   {
261     z->state->mode = I_BAD;
262     z->state->sub.marker = 0;
263   }
264   if ((n = z->avail_in) == 0)
265     return Z_BUF_ERROR;
266   p = z->next_in;
267   m = z->state->sub.marker;
268
269   /* search */
270   while (n && m < 4)
271   {
272     static const Byte mark[4] = {0, 0, 0xff, 0xff};
273     if (*p == mark[m])
274       m++;
275     else if (*p)
276       m = 0;
277     else
278       m = 4 - m;
279     p++, n--;
280   }
281
282   /* restore */
283   z->total_in += p - z->next_in;
284   z->next_in = p;
285   z->avail_in = n;
286   z->state->sub.marker = m;
287
288   /* return no joy or set up to restart on a new block */
289   if (m != 4)
290     return Z_DATA_ERROR;
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;
295   return Z_OK;
296 }
297
298
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.
305  */
306 int ZEXPORT zlib_inflateSyncPoint(z)
307 z_streamp z;
308 {
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);
312 }
313
314 /*
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.
321  */
322 static int zlib_inflate_addhistory(inflate_blocks_statef *s,
323                                       z_stream              *z)
324 {
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 */
332
333     if (s->read != s->write)
334         return Z_STREAM_ERROR;
335     if (s->mode != TYPE)
336         return Z_DATA_ERROR;
337
338     /* we're ready to rock */
339     LOAD
340     /* while there is input ready, copy to output buffer, moving
341      * pointers as needed.
342      */
343     while (n) {
344         t = n;  /* how many to do */
345         /* is there room until end of buffer? */
346         if (t > m) t = m;
347         /* update check information */
348         if (s->checkfn != Z_NULL)
349             s->check = (*s->checkfn)(s->check, q, t);
350         memcpy(q, p, t);
351         q += t;
352         p += t;
353         n -= t;
354         z->total_out += t;
355         s->read = q;    /* drag read pointer forward */
356 /*      WWRAP  */       /* expand WWRAP macro by hand to handle s->read */
357         if (q == s->end) {
358             s->read = q = s->window;
359             m = WAVAIL;
360         }
361     }
362     UPDATE
363     return Z_OK;
364 }
365
366
367 /*
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.
374  */
375
376 int ZEXPORT zlib_inflateIncomp(z)
377 z_stream *z;
378 {
379     if (z->state->mode != BLOCKS)
380         return Z_DATA_ERROR;
381     return zlib_inflate_addhistory(z->state->blocks, z);
382 }