* Use id(1) instead of grep(1) to detect the presence of the smmsp
[dragonfly.git] / lib / libz / infblock.c
1 /* infblock.c -- interpret and process block types to last block
2  * Copyright (C) 1995-2002 Mark Adler
3  * For conditions of distribution and use, see copyright notice in zlib.h 
4  *
5  * $FreeBSD: src/lib/libz/infblock.c,v 1.1.1.4.6.2 2003/02/01 13:33:12 sobomax Exp $
6  * $DragonFly: src/lib/libz/Attic/infblock.c,v 1.2 2003/06/17 04:26:52 dillon Exp $
7  */
8
9 #include "zutil.h"
10 #include "infblock.h"
11 #include "inftrees.h"
12 #include "infcodes.h"
13 #include "infutil.h"
14
15 struct inflate_codes_state {int dummy;}; /* for buggy compilers */
16
17 /* simplify the use of the inflate_huft type with some defines */
18 #define exop word.what.Exop
19 #define bits word.what.Bits
20
21 /* Table for deflate from PKZIP's appnote.txt. */
22 local const uInt border[] = { /* Order of the bit length code lengths */
23         16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15};
24
25 /*
26    Notes beyond the 1.93a appnote.txt:
27
28    1. Distance pointers never point before the beginning of the output
29       stream.
30    2. Distance pointers can point back across blocks, up to 32k away.
31    3. There is an implied maximum of 7 bits for the bit length table and
32       15 bits for the actual data.
33    4. If only one code exists, then it is encoded using one bit.  (Zero
34       would be more efficient, but perhaps a little confusing.)  If two
35       codes exist, they are coded using one bit each (0 and 1).
36    5. There is no way of sending zero distance codes--a dummy must be
37       sent if there are none.  (History: a pre 2.0 version of PKZIP would
38       store blocks with no distance codes, but this was discovered to be
39       too harsh a criterion.)  Valid only for 1.93a.  2.04c does allow
40       zero distance codes, which is sent as one code of zero bits in
41       length.
42    6. There are up to 286 literal/length codes.  Code 256 represents the
43       end-of-block.  Note however that the static length tree defines
44       288 codes just to fill out the Huffman codes.  Codes 286 and 287
45       cannot be used though, since there is no length base or extra bits
46       defined for them.  Similarily, there are up to 30 distance codes.
47       However, static trees define 32 codes (all 5 bits) to fill out the
48       Huffman codes, but the last two had better not show up in the data.
49    7. Unzip can check dynamic Huffman blocks for complete code sets.
50       The exception is that a single code would not be complete (see #4).
51    8. The five bits following the block type is really the number of
52       literal codes sent minus 257.
53    9. Length codes 8,16,16 are interpreted as 13 length codes of 8 bits
54       (1+6+6).  Therefore, to output three times the length, you output
55       three codes (1+1+1), whereas to output four times the same length,
56       you only need two codes (1+3).  Hmm.
57   10. In the tree reconstruction algorithm, Code = Code + Increment
58       only if BitLength(i) is not zero.  (Pretty obvious.)
59   11. Correction: 4 Bits: # of Bit Length codes - 4     (4 - 19)
60   12. Note: length code 284 can represent 227-258, but length code 285
61       really is 258.  The last length deserves its own, short code
62       since it gets used a lot in very redundant files.  The length
63       258 is special since 258 - 3 (the min match length) is 255.
64   13. The literal/length and distance code bit lengths are read as a
65       single stream of lengths.  It is possible (and advantageous) for
66       a repeat code (16, 17, or 18) to go across the boundary between
67       the two sets of lengths.
68  */
69
70
71 void inflate_blocks_reset(s, z, c)
72 inflate_blocks_statef *s;
73 z_streamp z;
74 uLongf *c;
75 {
76   if (c != Z_NULL)
77     *c = s->check;
78   if (s->mode == BTREE || s->mode == DTREE)
79     ZFREE(z, s->sub.trees.blens);
80   if (s->mode == CODES)
81     inflate_codes_free(s->sub.decode.codes, z);
82   s->mode = TYPE;
83   s->bitk = 0;
84   s->bitb = 0;
85   s->read = s->write = s->window;
86   if (s->checkfn != Z_NULL)
87     z->adler = s->check = (*s->checkfn)(0L, (const Bytef *)Z_NULL, 0);
88   Tracev((stderr, "inflate:   blocks reset\n"));
89 }
90
91
92 inflate_blocks_statef *inflate_blocks_new(z, c, w)
93 z_streamp z;
94 check_func c;
95 uInt w;
96 {
97   inflate_blocks_statef *s;
98
99   if ((s = (inflate_blocks_statef *)ZALLOC
100        (z,1,sizeof(struct inflate_blocks_state))) == Z_NULL)
101     return s;
102   if ((s->hufts =
103        (inflate_huft *)ZALLOC(z, sizeof(inflate_huft), MANY)) == Z_NULL)
104   {
105     ZFREE(z, s);
106     return Z_NULL;
107   }
108   if ((s->window = (Bytef *)ZALLOC(z, 1, w)) == Z_NULL)
109   {
110     ZFREE(z, s->hufts);
111     ZFREE(z, s);
112     return Z_NULL;
113   }
114   s->end = s->window + w;
115   s->checkfn = c;
116   s->mode = TYPE;
117   Tracev((stderr, "inflate:   blocks allocated\n"));
118   inflate_blocks_reset(s, z, Z_NULL);
119   return s;
120 }
121
122
123 int inflate_blocks(s, z, r)
124 inflate_blocks_statef *s;
125 z_streamp z;
126 int r;
127 {
128   uInt t;               /* temporary storage */
129   uLong b;              /* bit buffer */
130   uInt k;               /* bits in bit buffer */
131   Bytef *p;             /* input data pointer */
132   uInt n;               /* bytes available there */
133   Bytef *q;             /* output window write pointer */
134   uInt m;               /* bytes to end of window or read pointer */
135
136   /* copy input/output information to locals (UPDATE macro restores) */
137   LOAD
138
139   /* process input based on current state */
140   while (1) switch (s->mode)
141   {
142     case TYPE:
143       NEEDBITS(3)
144       t = (uInt)b & 7;
145       s->last = t & 1;
146       switch (t >> 1)
147       {
148         case 0:                         /* stored */
149           Tracev((stderr, "inflate:     stored block%s\n",
150                  s->last ? " (last)" : ""));
151           DUMPBITS(3)
152           t = k & 7;                    /* go to byte boundary */
153           DUMPBITS(t)
154           s->mode = LENS;               /* get length of stored block */
155           break;
156         case 1:                         /* fixed */
157           Tracev((stderr, "inflate:     fixed codes block%s\n",
158                  s->last ? " (last)" : ""));
159           {
160             uInt bl, bd;
161             inflate_huft *tl, *td;
162
163             inflate_trees_fixed(&bl, &bd, &tl, &td, z);
164             s->sub.decode.codes = inflate_codes_new(bl, bd, tl, td, z);
165             if (s->sub.decode.codes == Z_NULL)
166             {
167               r = Z_MEM_ERROR;
168               LEAVE
169             }
170           }
171           DUMPBITS(3)
172           s->mode = CODES;
173           break;
174         case 2:                         /* dynamic */
175           Tracev((stderr, "inflate:     dynamic codes block%s\n",
176                  s->last ? " (last)" : ""));
177           DUMPBITS(3)
178           s->mode = TABLE;
179           break;
180         case 3:                         /* illegal */
181           DUMPBITS(3)
182           s->mode = BAD;
183           z->msg = (char*)"invalid block type";
184           r = Z_DATA_ERROR;
185           LEAVE
186       }
187       break;
188     case LENS:
189       NEEDBITS(32)
190       if ((((~b) >> 16) & 0xffff) != (b & 0xffff))
191       {
192         s->mode = BAD;
193         z->msg = (char*)"invalid stored block lengths";
194         r = Z_DATA_ERROR;
195         LEAVE
196       }
197       s->sub.left = (uInt)b & 0xffff;
198       b = k = 0;                      /* dump bits */
199       Tracev((stderr, "inflate:       stored length %u\n", s->sub.left));
200       s->mode = s->sub.left ? STORED : (s->last ? DRY : TYPE);
201       break;
202     case STORED:
203       if (n == 0)
204         LEAVE
205       NEEDOUT
206       t = s->sub.left;
207       if (t > n) t = n;
208       if (t > m) t = m;
209       zmemcpy(q, p, t);
210       p += t;  n -= t;
211       q += t;  m -= t;
212       if ((s->sub.left -= t) != 0)
213         break;
214       Tracev((stderr, "inflate:       stored end, %lu total out\n",
215               z->total_out + (q >= s->read ? q - s->read :
216               (s->end - s->read) + (q - s->window))));
217       s->mode = s->last ? DRY : TYPE;
218       break;
219     case TABLE:
220       NEEDBITS(14)
221       s->sub.trees.table = t = (uInt)b & 0x3fff;
222 #ifndef PKZIP_BUG_WORKAROUND
223       if ((t & 0x1f) > 29 || ((t >> 5) & 0x1f) > 29)
224       {
225         s->mode = BAD;
226         z->msg = (char*)"too many length or distance symbols";
227         r = Z_DATA_ERROR;
228         LEAVE
229       }
230 #endif
231       t = 258 + (t & 0x1f) + ((t >> 5) & 0x1f);
232       if ((s->sub.trees.blens = (uIntf*)ZALLOC(z, t, sizeof(uInt))) == Z_NULL)
233       {
234         r = Z_MEM_ERROR;
235         LEAVE
236       }
237       DUMPBITS(14)
238       s->sub.trees.index = 0;
239       Tracev((stderr, "inflate:       table sizes ok\n"));
240       s->mode = BTREE;
241     case BTREE:
242       while (s->sub.trees.index < 4 + (s->sub.trees.table >> 10))
243       {
244         NEEDBITS(3)
245         s->sub.trees.blens[border[s->sub.trees.index++]] = (uInt)b & 7;
246         DUMPBITS(3)
247       }
248       while (s->sub.trees.index < 19)
249         s->sub.trees.blens[border[s->sub.trees.index++]] = 0;
250       s->sub.trees.bb = 7;
251       t = inflate_trees_bits(s->sub.trees.blens, &s->sub.trees.bb,
252                              &s->sub.trees.tb, s->hufts, z);
253       if (t != Z_OK)
254       {
255         r = t;
256         if (r == Z_DATA_ERROR)
257         {
258           ZFREE(z, s->sub.trees.blens);
259           s->mode = BAD;
260         }
261         LEAVE
262       }
263       s->sub.trees.index = 0;
264       Tracev((stderr, "inflate:       bits tree ok\n"));
265       s->mode = DTREE;
266     case DTREE:
267       while (t = s->sub.trees.table,
268              s->sub.trees.index < 258 + (t & 0x1f) + ((t >> 5) & 0x1f))
269       {
270         inflate_huft *h;
271         uInt i, j, c;
272
273         t = s->sub.trees.bb;
274         NEEDBITS(t)
275         h = s->sub.trees.tb + ((uInt)b & inflate_mask[t]);
276         t = h->bits;
277         c = h->base;
278         if (c < 16)
279         {
280           DUMPBITS(t)
281           s->sub.trees.blens[s->sub.trees.index++] = c;
282         }
283         else /* c == 16..18 */
284         {
285           i = c == 18 ? 7 : c - 14;
286           j = c == 18 ? 11 : 3;
287           NEEDBITS(t + i)
288           DUMPBITS(t)
289           j += (uInt)b & inflate_mask[i];
290           DUMPBITS(i)
291           i = s->sub.trees.index;
292           t = s->sub.trees.table;
293           if (i + j > 258 + (t & 0x1f) + ((t >> 5) & 0x1f) ||
294               (c == 16 && i < 1))
295           {
296             ZFREE(z, s->sub.trees.blens);
297             s->mode = BAD;
298             z->msg = (char*)"invalid bit length repeat";
299             r = Z_DATA_ERROR;
300             LEAVE
301           }
302           c = c == 16 ? s->sub.trees.blens[i - 1] : 0;
303           do {
304             s->sub.trees.blens[i++] = c;
305           } while (--j);
306           s->sub.trees.index = i;
307         }
308       }
309       s->sub.trees.tb = Z_NULL;
310       {
311         uInt bl, bd;
312         inflate_huft *tl, *td;
313         inflate_codes_statef *c;
314
315         bl = 9;         /* must be <= 9 for lookahead assumptions */
316         bd = 6;         /* must be <= 9 for lookahead assumptions */
317         t = s->sub.trees.table;
318         t = inflate_trees_dynamic(257 + (t & 0x1f), 1 + ((t >> 5) & 0x1f),
319                                   s->sub.trees.blens, &bl, &bd, &tl, &td,
320                                   s->hufts, z);
321         if (t != Z_OK)
322         {
323           if (t == (uInt)Z_DATA_ERROR)
324           {
325             ZFREE(z, s->sub.trees.blens);
326             s->mode = BAD;
327           }
328           r = t;
329           LEAVE
330         }
331         Tracev((stderr, "inflate:       trees ok\n"));
332         if ((c = inflate_codes_new(bl, bd, tl, td, z)) == Z_NULL)
333         {
334           r = Z_MEM_ERROR;
335           LEAVE
336         }
337         s->sub.decode.codes = c;
338       }
339       ZFREE(z, s->sub.trees.blens);
340       s->mode = CODES;
341     case CODES:
342       UPDATE
343       if ((r = inflate_codes(s, z, r)) != Z_STREAM_END)
344         return inflate_flush(s, z, r);
345       r = Z_OK;
346       inflate_codes_free(s->sub.decode.codes, z);
347       LOAD
348       Tracev((stderr, "inflate:       codes end, %lu total out\n",
349               z->total_out + (q >= s->read ? q - s->read :
350               (s->end - s->read) + (q - s->window))));
351       if (!s->last)
352       {
353         s->mode = TYPE;
354         break;
355       }
356       s->mode = DRY;
357     case DRY:
358       FLUSH
359       if (s->read != s->write)
360         LEAVE
361       s->mode = DONE;
362     case DONE:
363       r = Z_STREAM_END;
364       LEAVE
365     case BAD:
366       r = Z_DATA_ERROR;
367       LEAVE
368     default:
369       r = Z_STREAM_ERROR;
370       LEAVE
371   }
372 }
373
374
375 int inflate_blocks_free(s, z)
376 inflate_blocks_statef *s;
377 z_streamp z;
378 {
379   inflate_blocks_reset(s, z, Z_NULL);
380   ZFREE(z, s->window);
381   ZFREE(z, s->hufts);
382   ZFREE(z, s);
383   Tracev((stderr, "inflate:   blocks freed\n"));
384   return Z_OK;
385 }
386
387
388 void inflate_set_dictionary(s, d, n)
389 inflate_blocks_statef *s;
390 const Bytef *d;
391 uInt  n;
392 {
393   zmemcpy(s->window, d, n);
394   s->read = s->write = s->window + n;
395 }
396
397
398 /* Returns true if inflate is currently at the end of a block generated
399  * by Z_SYNC_FLUSH or Z_FULL_FLUSH. 
400  * IN assertion: s != Z_NULL
401  */
402 int inflate_blocks_sync_point(s)
403 inflate_blocks_statef *s;
404 {
405   return s->mode == LENS;
406 }