source: umon/main/zlib/infblock.c @ 87db514

Last change on this file since 87db514 was 87db514, checked in by Amar Takhar <amar@…>, on 04/16/15 at 19:26:21

Initial commit of the umon repository.

Prior to this three changes were made:

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