~ubuntu-branches/ubuntu/feisty/clamav/feisty

« back to all changes in this revision

Viewing changes to libclamav/nsis/infblock.c

  • Committer: Bazaar Package Importer
  • Author(s): Kees Cook
  • Date: 2007-02-20 10:33:44 UTC
  • mto: This revision was merged to the branch mainline in revision 16.
  • Revision ID: james.westby@ubuntu.com-20070220103344-zgcu2psnx9d98fpa
Tags: upstream-0.90
ImportĀ upstreamĀ versionĀ 0.90

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*
2
 
 * This file is a part of the zlib compression module for NSIS.
3
 
 * 
4
 
 * Copyright and license information can be found below.
5
 
 * Modifications Copyright (C) 1999-2007 Nullsoft and Contributors
6
 
 * 
7
 
 * The original zlib source code is available at
8
 
 * http://www.zlib.net/
9
 
 * 
10
 
 * This software is provided 'as-is', without any express or implied
11
 
 * warranty.
12
 
 */
13
 
 
14
 
/*
15
 
 * Copyright (C) 1995-1998 Jean-loup Gailly.
16
 
 * For conditions of distribution and use, see copyright notice in COPYING.nsis
17
 
 */
18
 
 
19
 
#include "nsis_zutil.h"
20
 
#include <string.h>
21
 
 
22
 
#ifndef min
23
 
#  define min(x,y) ((x<y)?x:y)
24
 
#endif
25
 
 
26
 
/* defines for inflate input/output */
27
 
/*   update pointers and return */
28
 
#define UPDBITS {s->bitb=b;s->bitk=k;}
29
 
#define UPDIN {z->avail_in=n;z->next_in=p;}
30
 
#define UPDOUT {s->write=q;}
31
 
#define UPDATE {UPDBITS UPDIN UPDOUT}
32
 
#define LEAVE(r) {UPDATE inflate_flush(z); return r;}
33
 
 
34
 
/*   get bytes and bits */
35
 
#define LOADIN {p=z->next_in;n=z->avail_in;b=s->bitb;k=s->bitk;}
36
 
 
37
 
 
38
 
#define NEEDBYTE {if(!n)LEAVE(Z_OK)}
39
 
#define NEXTBYTE (n--,*p++)
40
 
#define NEEDBITS(j) {while(k<(j)){NEEDBYTE;b|=((uLong)NEXTBYTE)<<k;k+=8;}}
41
 
 
42
 
#define DUMPBITS(j) {b>>=(j);k-=(j);}
43
 
/*   output bytes */
44
 
#define WAVAIL (uInt)(q<s->read?s->read-q-1:s->end-q)
45
 
#define LOADOUT {q=s->write;m=(uInt)WAVAIL;}
46
 
#define WRAP {if(q==s->end&&s->read!=s->window){q=s->window;m=(uInt)WAVAIL;}}
47
 
#define FLUSH {UPDOUT inflate_flush(z); LOADOUT}
48
 
#define NEEDOUT {if(m==0){WRAP if(m==0){FLUSH WRAP if(m==0) LEAVE(Z_OK)}}}
49
 
#define OUTBYTE(a) {*q++=(Byte)(a);m--;}
50
 
/*   load local pointers */
51
 
#define LOAD {LOADIN LOADOUT}
52
 
 
53
 
#define LAST (s->last == DRY)
54
 
 
55
 
 
56
 
typedef struct inflate_blocks_state FAR inflate_blocks_statef;
57
 
#define exop word.what.Exop
58
 
#define bits word.what.Bits
59
 
 
60
 
/* And'ing with mask[n] masks the lower n bits */
61
 
local const unsigned short inflate_mask[17] = {
62
 
    0x0000,
63
 
    0x0001, 0x0003, 0x0007, 0x000f, 0x001f, 0x003f, 0x007f, 0x00ff,
64
 
    0x01ff, 0x03ff, 0x07ff, 0x0fff, 0x1fff, 0x3fff, 0x7fff, 0xffff
65
 
}; /* use to reduce .data #define INFLATE_MASK(x, n) (x & (~((unsigned short) 0xFFFF << n))) */
66
 
local const char border[] = { /* Order of the bit length code lengths */
67
 
        16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15};
68
 
 
69
 
/* Tables for deflate from PKZIP's appnote.txt. */
70
 
local const unsigned short  cplens[31] = { /* Copy lengths for literal codes 257..285 */
71
 
        3, 4, 5, 6, 7, 8, 9, 10, 11, 13, 15, 17, 19, 23, 27, 31,
72
 
        35, 43, 51, 59, 67, 83, 99, 115, 131, 163, 195, 227, 258, 0, 0};
73
 
        /* see note #13 above about 258 */
74
 
local const unsigned short  cplext[31] = { /* Extra bits for literal codes 257..285 */
75
 
        0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2,
76
 
        3, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5, 0, 112, 112}; /* 112==invalid */
77
 
local const unsigned short  cpdist[30] = { /* Copy offsets for distance codes 0..29 */
78
 
        1, 2, 3, 4, 5, 7, 9, 13, 17, 25, 33, 49, 65, 97, 129, 193,
79
 
        257, 385, 513, 769, 1025, 1537, 2049, 3073, 4097, 6145,
80
 
        8193, 12289, 16385, 24577};
81
 
local const unsigned short  cpdext[30] = { /* Extra bits for distance codes */
82
 
        0, 0, 0, 0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6,
83
 
        7, 7, 8, 8, 9, 9, 10, 10, 11, 11,
84
 
        12, 12, 13, 13};
85
 
 
86
 
/* build fixed tables only once--keep them here */
87
 
/* local char fixed_built = 0; */
88
 
/* local inflate_huft fixed_mem[FIXEDH]; */
89
 
/* local uInt fixed_bl=9; */
90
 
/* local uInt fixed_bd=5; */
91
 
/* local inflate_huft *fixed_tl; */
92
 
/* local inflate_huft *fixed_td; */
93
 
 
94
 
 
95
 
/* copy as much as possible from the sliding window to the output area */
96
 
local void ZEXPORT inflate_flush(nsis_z_streamp z)
97
 
{
98
 
  inflate_blocks_statef *s = &z->blocks;
99
 
  uInt n;
100
 
  Bytef *q;
101
 
 
102
 
  /* local copies of source and destination pointers */
103
 
  q = s->read;
104
 
 
105
 
again:
106
 
  /* compute number of bytes to copy as far as end of window */
107
 
  n = (uInt)((q <= s->write ? s->write : s->end) - q);
108
 
  n = min(n, z->avail_out);
109
 
 
110
 
  /* update counters */
111
 
  z->avail_out -= n;
112
 
  /* z->total_out += n; */
113
 
 
114
 
  /* copy as far as end of window */
115
 
  zmemcpy(z->next_out, q, n);
116
 
  z->next_out += n;
117
 
  q += n;
118
 
 
119
 
  /* see if more to copy at beginning of window */
120
 
  if (q == s->end)
121
 
  {
122
 
    /* wrap pointers */
123
 
    q = s->window;
124
 
    if (s->write == s->end)
125
 
      s->write = s->window;
126
 
 
127
 
    /* do the same for the beginning of the window */
128
 
    goto again;
129
 
  }
130
 
 
131
 
  /* update pointers */
132
 
  s->read = q;
133
 
}
134
 
 
135
 
#define BMAX 15         /* maximum bit length of any code */
136
 
 
137
 
local int ZEXPORT huft_build(
138
 
uIntf *b,               /* code lengths in bits (all assumed <= BMAX) */
139
 
uInt n,                 /* number of codes (assumed <= 288) */
140
 
uInt s,                 /* number of simple-valued codes (0..s-1) */
141
 
const unsigned short *d,         /* list of base values for non-simple codes */
142
 
const unsigned short *e,         /* list of extra bits for non-simple codes */
143
 
inflate_huft * FAR *t,  /* result: starting table */
144
 
uIntf *m,               /* maximum lookup bits, returns actual */
145
 
inflate_huft *hp,       /* space for trees */
146
 
uInt *hn,               /* working area: values in order of bit length */
147
 
uIntf *v)             /* work area for huft_build */
148
 
{
149
 
  uInt a;                       /* counter for codes of length k */
150
 
  uInt c[BMAX+1];               /* bit length count table */
151
 
  uInt f;                       /* i repeats in table every f entries */
152
 
  int g;                        /* maximum code length */
153
 
  int h;                        /* table level */
154
 
  uInt i;              /* counter, current code */
155
 
  uInt j;              /* counter */
156
 
  int k;               /* number of bits in current code */
157
 
  int l;                        /* bits per table (returned in m) */
158
 
  uIntf *p;            /* pointer into c[], b[], or v[] */
159
 
  inflate_huft *q;              /* points to current table */
160
 
  struct inflate_huft_s r;      /* table entry for structure assignment */
161
 
  inflate_huft *u[BMAX];        /* table stack */
162
 
  int w;               /* bits before this table == (l * h) */
163
 
  uInt x[BMAX+1];               /* bit offsets, then code stack */
164
 
  uIntf *xp;                    /* pointer into x */
165
 
  int y;                        /* number of dummy codes added */
166
 
  uInt z;                       /* number of entries in current table */
167
 
 
168
 
 
169
 
  /* Generate counts for each bit length */
170
 
  p=c;
171
 
  y=16; while (y--) *p++ = 0;
172
 
  p = b;
173
 
  i = n;
174
 
  do {
175
 
    c[*p++]++;                  /* assume all entries <= BMAX */
176
 
  } while (--i);
177
 
  if (c[0] == n)                /* null input--all zero length codes */
178
 
  {
179
 
    *t = (inflate_huft *)Z_NULL;
180
 
    *m = 0;
181
 
    return Z_OK;
182
 
  }
183
 
 
184
 
 
185
 
  /* Find minimum and maximum length, bound *m by those */
186
 
  l = *m;
187
 
  for (j = 1; j <= BMAX; j++)
188
 
    if (c[j])
189
 
      break;
190
 
  k = j;                        /* minimum code length */
191
 
  if ((uInt)l < j)
192
 
    l = j;
193
 
  for (i = BMAX; i; i--)
194
 
    if (c[i])
195
 
      break;
196
 
  g = i;                        /* maximum code length */
197
 
  if ((uInt)l > i)
198
 
    l = i;
199
 
  *m = l;
200
 
 
201
 
 
202
 
  /* Adjust last length count to fill out codes, if needed */
203
 
  for (y = 1 << j; j < i; j++, y <<= 1)
204
 
    if ((y -= c[j]) < 0)
205
 
      return Z_DATA_ERROR;
206
 
  if ((y -= c[i]) < 0)
207
 
    return Z_DATA_ERROR;
208
 
  c[i] += y;
209
 
 
210
 
 
211
 
  /* Generate starting offsets into the value table for each length */
212
 
  x[1] = j = 0;
213
 
  p = c + 1;  xp = x + 2;
214
 
  while (--i) {                 /* note that i == g from above */
215
 
    *xp++ = (j += *p++);
216
 
  }
217
 
 
218
 
 
219
 
  /* Make a table of values in order of bit lengths */
220
 
  p = b;  i = 0;
221
 
  do {
222
 
    if ((j = *p++) != 0)
223
 
      v[x[j]++] = i;
224
 
  } while (++i < n);
225
 
  n = x[g];                     /* set n to length of v */
226
 
 
227
 
 
228
 
  /* Generate the Huffman codes and for each, make the table entries */
229
 
  x[0] = i = 0;                 /* first Huffman code is zero */
230
 
  p = v;                        /* grab values in bit order */
231
 
  h = -1;                       /* no tables yet--level -1 */
232
 
  w = -l;                       /* bits decoded == (l * h) */
233
 
  u[0] = (inflate_huft *)Z_NULL;        /* just to keep compilers happy */
234
 
  q = (inflate_huft *)Z_NULL;   /* ditto */
235
 
  z = 0;                        /* ditto */
236
 
 
237
 
  r.base = 0;
238
 
 
239
 
  /* go through the bit lengths (k already is bits in shortest code) */
240
 
  for (; k <= g; k++)
241
 
  {
242
 
    a = c[k];
243
 
    while (a--)
244
 
    {
245
 
      int nextw=w;
246
 
      /* here i is the Huffman code of length k bits for value *p */
247
 
      /* make tables up to required level */
248
 
      while (k > (nextw=w + l))
249
 
      {
250
 
        h++;
251
 
 
252
 
        /* compute minimum size table less than or equal to l bits */
253
 
        z = g - nextw;
254
 
        z = z > (uInt)l ? (uInt)l : z;        /* table size upper limit */
255
 
        if ((f = 1 << (j = k - nextw)) > a + 1)     /* try a k-w bit table */
256
 
        {                       /* too few codes for k-w bit table */
257
 
          f -= a + 1;           /* deduct codes from patterns left */
258
 
          xp = c + k;
259
 
          if (j < z)
260
 
            while (++j < z && (f <<= 1) > *++xp)     /* try smaller tables up to z bits */
261
 
            {
262
 
              f -= *xp;         /* else deduct codes from patterns */
263
 
            }
264
 
        }
265
 
        z = 1 << j;             /* table entries for j-bit table */
266
 
 
267
 
        /* allocate new table */
268
 
        if (*hn + z > MANY)     /* (note: doesn't matter for fixed) */
269
 
          return Z_MEM_ERROR;   /* not enough memory */
270
 
        u[h] = q = hp + *hn;
271
 
        *hn += z;
272
 
 
273
 
        /* connect to last table, if there is one */
274
 
        if (h)
275
 
        {
276
 
          x[h] = i;             /* save pattern for backing up */
277
 
          r.bits = (Byte)l;     /* bits to dump before this table */
278
 
          r.exop = (Byte)j;     /* bits in this table */
279
 
          j = i >> w;
280
 
          r.base = (uInt)(q - u[h-1] - j);   /* offset to this table */
281
 
          u[h-1][j] = r;        /* connect to last table */
282
 
        }
283
 
        else
284
 
          *t = q;               /* first table is returned result */
285
 
        w=nextw;                 /* previous table always l bits */
286
 
      }
287
 
 
288
 
      /* set up table entry in r */
289
 
      r.bits = (Byte)(k - w);
290
 
      if (p >= v + n)
291
 
        r.exop = 128 + 64;      /* out of values--invalid code */
292
 
      else if (*p < s)
293
 
      {
294
 
        r.exop = (Byte)(*p < 256 ? 0 : 32 + 64);     /* 256 is end-of-block */
295
 
        r.base = *p++;          /* simple code is just the value */
296
 
      }
297
 
      else
298
 
      {
299
 
        r.exop = (Byte)(e[*p - s] + 16 + 64);/* non-simple--look up in lists */
300
 
        r.base = d[*p++ - s];
301
 
      }
302
 
 
303
 
      /* fill code-like entries with r */
304
 
      f = 1 << (k - w);
305
 
      for (j = i >> w; j < z; j += f)
306
 
        q[j] = r;
307
 
 
308
 
      /* backwards increment the k-bit code i */
309
 
      for (j = 1 << (k - 1); i & j; j >>= 1)
310
 
        i ^= j;
311
 
      i ^= j;
312
 
 
313
 
      /* backup over finished tables */
314
 
      while ((i & ((1 << w) - 1)) != x[h])
315
 
      {
316
 
        h--;                    /* don't need to update q */
317
 
        w -= l;
318
 
      }
319
 
    }
320
 
  }
321
 
 
322
 
 
323
 
  /* Return Z_BUF_ERROR if we were given an incomplete table */
324
 
  return (y != 0 && g != 1) ? Z_BUF_ERROR : Z_OK;
325
 
}
326
 
 
327
 
int ZEXPORT nsis_inflate(nsis_z_streamp z)
328
 
{
329
 
  inflate_blocks_statef *s = &z->blocks;
330
 
  inflate_codes_statef *c = &s->sub.decode.t_codes;  /* codes state */
331
 
 
332
 
  /* lousy two bytes saved by doing this */
333
 
  struct
334
 
  {
335
 
    uInt t;               /* temporary storage */
336
 
    uLong b;              /* bit buffer */
337
 
    uInt k;               /* bits in bit buffer */
338
 
    Bytef *p;             /* input data pointer */
339
 
    uInt n;               /* bytes available there */
340
 
    Bytef *q;             /* output window write pointer */
341
 
    uInt m;               /* bytes to end of window or read pointer */
342
 
 
343
 
    /* CODES variables */
344
 
 
345
 
    inflate_huft *j;      /* temporary pointer */
346
 
    uInt e;               /* extra bits or operation */
347
 
    Bytef *f;             /* pointer to copy strings from */
348
 
  } _state;
349
 
 
350
 
#define t _state.t
351
 
#define b _state.b
352
 
#define k _state.k
353
 
#define p _state.p
354
 
#define n _state.n
355
 
#define q _state.q
356
 
#define m _state.m
357
 
 
358
 
  /* copy input/output information to locals (UPDATE macro restores) */
359
 
  LOAD
360
 
 
361
 
  /* process input based on current state */
362
 
  for (;;) switch (s->mode)
363
 
  {
364
 
    case TYPE:
365
 
      NEEDBITS(3)
366
 
      t = (uInt)b & 7;
367
 
      DUMPBITS(3)
368
 
      s->last = (t & 1) ? DRY : TYPE;
369
 
      switch (t >> 1)
370
 
      {
371
 
        case 0:                         /* stored */
372
 
          Tracev((stderr, "inflate:     stored block%s\n",
373
 
                 LAST ? " (last)" : ""));
374
 
          DUMPBITS(k&7)
375
 
          s->mode = LENS;               /* get length of stored block */
376
 
          break;
377
 
        case 1:                         /* fixed */
378
 
          Tracev((stderr, "inflate:     fixed codes block%s\n",
379
 
                 LAST ? " (last)" : ""));
380
 
          {
381
 
            if (!s->zs.fixed_built)
382
 
            {
383
 
              int _k;              /* temporary variable */
384
 
              uInt f = 0;         /* number of hufts used in fixed_mem */
385
 
              
386
 
              /* literal table */
387
 
              for (_k = 0; _k < 288; _k++)
388
 
              {
389
 
                char v=8;
390
 
                if (_k > 143)
391
 
                {
392
 
                  if (_k < 256) v++;
393
 
                  else if (_k < 280) v--;
394
 
                }
395
 
                s->zs.lc[_k] = v;
396
 
              }
397
 
 
398
 
              huft_build(s->zs.lc, 288, 257, cplens, cplext, &s->zs.fixed_tl, &s->zs.fixed_bl, s->zs.fixed_mem, &f, s->zs.v);
399
 
 
400
 
              /* distance table */
401
 
              for (_k = 0; _k < 30; _k++) s->zs.lc[_k] = 5;
402
 
 
403
 
              huft_build(s->zs.lc, 30, 0, cpdist, cpdext, &s->zs.fixed_td, &s->zs.fixed_bd, s->zs.fixed_mem, &f, s->zs.v);
404
 
 
405
 
              /* done */
406
 
              s->zs.fixed_built++;
407
 
            }
408
 
 
409
 
            /* s->sub.decode.t_codes.mode = CODES_START; */
410
 
            s->sub.decode.t_codes.lbits = (Byte)s->zs.fixed_bl;
411
 
            s->sub.decode.t_codes.dbits = (Byte)s->zs.fixed_bd;
412
 
            s->sub.decode.t_codes.ltree = s->zs.fixed_tl;
413
 
            s->sub.decode.t_codes.dtree = s->zs.fixed_td;
414
 
          }
415
 
          s->mode = CODES_START;
416
 
          break;
417
 
        case 2:                         /* dynamic */
418
 
          Tracev((stderr, "inflate:     dynamic codes block%s\n",
419
 
                 LAST ? " (last)" : ""));
420
 
          s->mode = TABLE;
421
 
          break;
422
 
        case 3:                         /* illegal */
423
 
          /* the only illegal value possible is 3 because we check only 2 bits */
424
 
          goto bad;
425
 
      }
426
 
      break;
427
 
    case LENS:
428
 
      NEEDBITS(16)
429
 
      s->sub.left = (uInt)b & 0xffff;
430
 
      b = k = 0;                      /* dump bits */
431
 
      Tracev((stderr, "inflate:       stored length %u\n", s->sub.left));
432
 
      s->mode = s->sub.left ? STORED : (inflate_mode)s->last;
433
 
      break;
434
 
    case STORED:
435
 
    {
436
 
      uInt mn;
437
 
 
438
 
      if (n == 0)
439
 
        LEAVE(Z_OK)
440
 
      NEEDOUT
441
 
      mn = min(m, n);
442
 
      t = min(s->sub.left, mn);
443
 
      zmemcpy(q, p, t);
444
 
      p += t;  n -= t;
445
 
      q += t;  m -= t;
446
 
      if (!(s->sub.left -= t))
447
 
        s->mode = (inflate_mode)s->last;
448
 
      break;
449
 
    }
450
 
    case TABLE:
451
 
      NEEDBITS(14)
452
 
      s->sub.trees.table = t = (uInt)b & 0x3fff;
453
 
      if ((t & 0x1f) > 29 || ((t >> 5) & 0x1f) > 29)
454
 
      {
455
 
        s->mode = NZ_BAD;
456
 
        LEAVE(Z_DATA_ERROR);
457
 
      }
458
 
      /* t = 258 + (t & 0x1f) + ((t >> 5) & 0x1f); */
459
 
      DUMPBITS(14)
460
 
      s->sub.trees.index = 0;
461
 
      Tracev((stderr, "inflate:       table sizes ok\n"));
462
 
      s->mode = BTREE;
463
 
    case BTREE:
464
 
      while (s->sub.trees.index < 4 + (s->sub.trees.table >> 10))
465
 
      {
466
 
        NEEDBITS(3)
467
 
        s->sub.trees.t_blens[(int)border[s->sub.trees.index++]] = (uInt)b & 7;
468
 
        DUMPBITS(3)
469
 
      }
470
 
      while (s->sub.trees.index < 19)
471
 
        s->sub.trees.t_blens[(int)border[s->sub.trees.index++]] = 0;
472
 
      s->sub.trees.bb = 7;
473
 
 
474
 
      {
475
 
        uInt hn = 0;          /* hufts used in space */
476
 
 
477
 
        t = huft_build(s->sub.trees.t_blens, 19, 19, Z_NULL, Z_NULL,
478
 
                       &s->sub.trees.tb, &s->sub.trees.bb, s->hufts, &hn, s->zs.v);
479
 
        if (t != Z_OK || !s->sub.trees.bb)
480
 
        {
481
 
          s->mode = NZ_BAD;
482
 
          break;
483
 
        }
484
 
      }
485
 
 
486
 
      s->sub.trees.index = 0;
487
 
      Tracev((stderr, "inflate:       bits tree ok\n"));
488
 
      s->mode = DTREE;
489
 
    case DTREE:
490
 
      while (t = s->sub.trees.table,
491
 
             s->sub.trees.index < 258 + (t & 0x1f) + ((t >> 5) & 0x1f))
492
 
      {
493
 
        inflate_huft *h;
494
 
        uInt i, j, d;
495
 
 
496
 
        t = s->sub.trees.bb;
497
 
        NEEDBITS(t)
498
 
        h = s->sub.trees.tb + ((uInt)b & (uInt)inflate_mask[t]);
499
 
        t = h->bits;
500
 
        d = h->base;
501
 
        if (d < 16)
502
 
        {
503
 
          DUMPBITS(t)
504
 
          s->sub.trees.t_blens[s->sub.trees.index++] = d;
505
 
        }
506
 
        else /* d == 16..18 */
507
 
        {
508
 
          if (d == 18)
509
 
          {
510
 
            i=7;
511
 
            j=11;
512
 
          }
513
 
          else
514
 
          {
515
 
            i=d-14;
516
 
            j=3;
517
 
          }
518
 
          NEEDBITS(t+i)
519
 
          DUMPBITS(t)
520
 
          j += (uInt)b & (uInt)inflate_mask[i];
521
 
          DUMPBITS(i)
522
 
          i = s->sub.trees.index;
523
 
          t = s->sub.trees.table;
524
 
          if (i + j > 258 + (t & 0x1f) + ((t >> 5) & 0x1f) ||
525
 
              (d == 16 && i < 1))
526
 
          {
527
 
            s->mode = NZ_BAD;
528
 
            LEAVE(Z_DATA_ERROR);
529
 
          }
530
 
          d = d == 16 ? s->sub.trees.t_blens[i - 1] : 0;
531
 
          do {
532
 
            s->sub.trees.t_blens[i++] = d;
533
 
          } while (--j);
534
 
          s->sub.trees.index = i;
535
 
        }
536
 
      }
537
 
      s->sub.trees.tb = Z_NULL;
538
 
      {
539
 
        uInt hn = 0;          /* hufts used in space */
540
 
        uInt bl, bd;
541
 
        inflate_huft *tl, *td;
542
 
        int nl,nd;
543
 
        t = s->sub.trees.table;
544
 
 
545
 
        nl = 257 + (t & 0x1f);
546
 
        nd = 1 + ((t >> 5) & 0x1f);
547
 
        bl = 9;         /* must be <= 9 for lookahead assumptions */
548
 
        bd = 6;         /* must be <= 9 for lookahead assumptions */
549
 
 
550
 
        t = huft_build(s->sub.trees.t_blens, nl, 257, cplens, cplext, &tl, &bl, s->hufts, &hn, s->zs.v);
551
 
        if (bl == 0) t = Z_DATA_ERROR;
552
 
        if (t == Z_OK)
553
 
        {
554
 
          /* build distance tree */
555
 
          t = huft_build(s->sub.trees.t_blens + nl, nd, 0, cpdist, cpdext, &td, &bd, s->hufts, &hn, s->zs.v);
556
 
        }
557
 
        if (t != Z_OK || (bd == 0 && nl > 257))
558
 
        {
559
 
          s->mode = NZ_BAD;
560
 
          LEAVE(Z_DATA_ERROR);
561
 
        }
562
 
        Tracev((stderr, "inflate:       trees ok\n"));
563
 
 
564
 
        /* s->sub.decode.t_codes.mode = CODES_START; */
565
 
        s->sub.decode.t_codes.lbits = (Byte)bl;
566
 
        s->sub.decode.t_codes.dbits = (Byte)bd;
567
 
        s->sub.decode.t_codes.ltree = tl;
568
 
        s->sub.decode.t_codes.dtree = td;
569
 
      }
570
 
      s->mode = CODES_START;
571
 
 
572
 
#define j (_state.j)
573
 
#define e (_state.e)
574
 
#define f (_state.f)
575
 
 
576
 
    /* waiting for "i:"=input, "o:"=output, "x:"=nothing */
577
 
 
578
 
    case CODES_START:         /* x: set up for LEN */
579
 
      c->sub.code.need = c->lbits;
580
 
      c->sub.code.tree = c->ltree;
581
 
      s->mode = CODES_LEN;
582
 
    case CODES_LEN:           /* i: get length/literal/eob next */
583
 
      t = c->sub.code.need;
584
 
      NEEDBITS(t)
585
 
      j = c->sub.code.tree + ((uInt)b & (uInt)inflate_mask[t]);
586
 
      DUMPBITS(j->bits)
587
 
      e = (uInt)(j->exop);
588
 
      if (e == 0)               /* literal */
589
 
      {
590
 
        c->sub.lit = j->base;
591
 
        s->mode = CODES_LIT;
592
 
        break;
593
 
      }
594
 
      if (e & 16)               /* length */
595
 
      {
596
 
        c->sub.copy.get = e & 15;
597
 
        c->len = j->base;
598
 
        s->mode = CODES_LENEXT;
599
 
        break;
600
 
      }
601
 
      if ((e & 64) == 0)        /* next table */
602
 
      {
603
 
        c->sub.code.need = e;
604
 
        c->sub.code.tree = j + j->base;
605
 
        break;
606
 
      }
607
 
      if (e & 32)               /* end of block */
608
 
      {
609
 
        s->mode = CODES_WASH;
610
 
        break;
611
 
      }
612
 
    goto bad;
613
 
    case CODES_LENEXT:        /* i: getting length extra (have base) */
614
 
      t = c->sub.copy.get;
615
 
      NEEDBITS(t)
616
 
      c->len += (uInt)b & (uInt)inflate_mask[t];
617
 
      DUMPBITS(t)
618
 
      c->sub.code.need = c->dbits;
619
 
      c->sub.code.tree = c->dtree;
620
 
      s->mode = CODES_DIST;
621
 
    case CODES_DIST:          /* i: get distance next */
622
 
      t = c->sub.code.need;
623
 
      NEEDBITS(t)
624
 
      j = c->sub.code.tree + ((uInt)b & (uInt)inflate_mask[t]);
625
 
      DUMPBITS(j->bits)
626
 
      e = (uInt)(j->exop);
627
 
      if (e & 16)               /* distance */
628
 
      {
629
 
        c->sub.copy.get = e & 15;
630
 
        c->sub.copy.dist = j->base;
631
 
        s->mode = CODES_DISTEXT;
632
 
        break;
633
 
      }
634
 
      if ((e & 64) == 0)        /* next table */
635
 
      {
636
 
        c->sub.code.need = e;
637
 
        c->sub.code.tree = j + j->base;
638
 
        break;
639
 
      }
640
 
      goto bad;        /* invalid code */
641
 
    case CODES_DISTEXT:       /* i: getting distance extra */
642
 
      t = c->sub.copy.get;
643
 
      NEEDBITS(t)
644
 
      c->sub.copy.dist += (uInt)b & (uInt)inflate_mask[t];
645
 
      DUMPBITS(t)
646
 
      s->mode = CODES_COPY;
647
 
    case CODES_COPY:          /* o: copying bytes in window, waiting for space */
648
 
      f = (uInt)(q - s->window) < c->sub.copy.dist ?
649
 
          s->end - (c->sub.copy.dist - (q - s->window)) :
650
 
          q - c->sub.copy.dist;
651
 
 
652
 
      while (c->len)
653
 
      {
654
 
        NEEDOUT
655
 
        OUTBYTE(*f++)
656
 
        if (f == s->end)
657
 
          f = s->window;
658
 
        c->len--;
659
 
      }
660
 
      s->mode = CODES_START;
661
 
      break;
662
 
    case CODES_LIT:           /* o: got literal, waiting for output space */
663
 
      NEEDOUT
664
 
      OUTBYTE(c->sub.lit)
665
 
      s->mode = CODES_START;
666
 
      break;
667
 
    case CODES_WASH:          /* o: got eob, possibly more output */
668
 
      if (k > 7)        /* return unused byte, if any */
669
 
      {
670
 
        k -= 8;
671
 
        n++;
672
 
        p--;            /* can always return one */
673
 
      }
674
 
      /* flushing will be done in DRY */
675
 
 
676
 
#undef j
677
 
#undef e
678
 
#undef f
679
 
 
680
 
    case DRY:
681
 
      FLUSH
682
 
      if (s->write != s->read)
683
 
        LEAVE(Z_OK)
684
 
      if (s->mode == CODES_WASH)
685
 
      {
686
 
        Tracev((stderr, "inflate:       codes end, %lu total out\n",
687
 
               z->total_out + (q >= s->read ? q - s->read :
688
 
               (s->end - s->read) + (q - s->window))));
689
 
      }
690
 
      /* DRY if last, TYPE if not */
691
 
      s->mode = (inflate_mode)s->last;
692
 
      if (s->mode == TYPE)
693
 
        break;
694
 
      LEAVE(Z_STREAM_END)
695
 
    /*case BAD:
696
 
      r = Z_DATA_ERROR;
697
 
      LEAVE
698
 
    */
699
 
    default: /* we'll call Z_STREAM_ERROR if BAD anyway */
700
 
    bad:
701
 
      s->mode = NZ_BAD;
702
 
      LEAVE(Z_STREAM_ERROR)
703
 
  }
704
 
}
705
 
 
706
 
#undef t
707
 
#undef b
708
 
#undef k
709
 
#undef p
710
 
#undef n
711
 
#undef q
712
 
#undef m