source: rtems/c/src/lib/libbsp/powerpc/shared/bootloader/zlib.c @ e79a1947

4.104.114.84.95
Last change on this file since e79a1947 was f05b2ac, checked in by Ralf Corsepius <ralf.corsepius@…>, on 04/21/04 at 16:01:48

Remove duplicate white lines.

  • Property mode set to 100644
File size: 64.4 KB
Line 
1/*
2 * This file is derived from various .h and .c files from the zlib-0.95
3 * distribution by Jean-loup Gailly and Mark Adler, with some additions
4 * by Paul Mackerras to aid in implementing Deflate compression and
5 * decompression for PPP packets.  See zlib.h for conditions of
6 * distribution and use.
7 *
8 * Changes that have been made include:
9 * - changed functions not used outside this file to "local"
10 * - added minCompression parameter to deflateInit2
11 * - added Z_PACKET_FLUSH (see zlib.h for details)
12 * - added inflateIncomp
13 *
14 * $Id$
15 */
16
17/*+++++*/
18/* zutil.h -- internal interface and configuration of the compression library
19 * Copyright (C) 1995 Jean-loup Gailly.
20 * For conditions of distribution and use, see copyright notice in zlib.h
21 */
22
23/* WARNING: this file should *not* be used by applications. It is
24   part of the implementation of the compression library and is
25   subject to change. Applications should only use zlib.h.
26 */
27
28/* From: zutil.h,v 1.9 1995/05/03 17:27:12 jloup Exp */
29
30#define _Z_UTIL_H
31
32#include "zlib.h"
33
34#ifndef local
35#  define local static
36#endif
37/* compile with -Dlocal if your debugger can't find static symbols */
38
39#define FAR
40
41typedef unsigned char  uch;
42typedef uch FAR uchf;
43typedef unsigned short ush;
44typedef ush FAR ushf;
45typedef unsigned long  ulg;
46
47extern char *z_errmsg[]; /* indexed by 1-zlib_error */
48
49#define ERR_RETURN(strm,err) return (strm->msg=z_errmsg[1-err], err)
50/* To be used only when the state is known to be valid */
51
52#ifndef NULL
53#define NULL    ((void *) 0)
54#endif
55
56        /* common constants */
57
58#define DEFLATED   8
59
60#ifndef DEF_WBITS
61#  define DEF_WBITS MAX_WBITS
62#endif
63/* default windowBits for decompression. MAX_WBITS is for compression only */
64
65#if MAX_MEM_LEVEL >= 8
66#  define DEF_MEM_LEVEL 8
67#else
68#  define DEF_MEM_LEVEL  MAX_MEM_LEVEL
69#endif
70/* default memLevel */
71
72#define STORED_BLOCK 0
73#define STATIC_TREES 1
74#define DYN_TREES    2
75/* The three kinds of block type */
76
77#define MIN_MATCH  3
78#define MAX_MATCH  258
79/* The minimum and maximum match lengths */
80
81         /* functions */
82
83#include <string.h>
84#define zmemcpy memcpy
85#define zmemzero(dest, len)     memset(dest, 0, len)
86
87/* Diagnostic functions */
88#ifdef DEBUG_ZLIB
89#  include <stdio.h>
90#  ifndef verbose
91#    define verbose 0
92#  endif
93#  define Assert(cond, msg) {if(!(cond)) Trace(msg);}
94#  define Trace(x) printk(x)
95#  define Tracev(x) {if (verbose) printk x ;}
96#  define Tracevv(x) {if (verbose>1) printk x ;}
97#  define Tracec(c,x) {if (verbose && (c)) printk x ;}
98#  define Tracecv(c,x) {if (verbose>1 && (c)) printk x ;}
99#else
100#  define Assert(cond,msg)
101#  define Trace(x)
102#  define Tracev(x)
103#  define Tracevv(x)
104#  define Tracec(c,x)
105#  define Tracecv(c,x)
106#endif
107
108typedef uLong (*check_func) OF((uLong check, Bytef *buf, uInt len));
109
110/* voidpf zcalloc OF((voidpf opaque, unsigned items, unsigned size)); */
111/* void   zcfree  OF((voidpf opaque, voidpf ptr)); */
112
113#define ZALLOC(strm, items, size) \
114           (*((strm)->zalloc))((strm)->opaque, (items), (size))
115#define ZFREE(strm, addr, size) \
116           (*((strm)->zfree))((strm)->opaque, (voidpf)(addr), (size))
117#define TRY_FREE(s, p, n) {if (p) ZFREE(s, p, n);}
118
119/* deflate.h -- internal compression state
120 * Copyright (C) 1995 Jean-loup Gailly
121 * For conditions of distribution and use, see copyright notice in zlib.h
122 */
123
124/* WARNING: this file should *not* be used by applications. It is
125   part of the implementation of the compression library and is
126   subject to change. Applications should only use zlib.h.
127 */
128
129/*+++++*/
130/* infblock.h -- header to use infblock.c
131 * Copyright (C) 1995 Mark Adler
132 * For conditions of distribution and use, see copyright notice in zlib.h
133 */
134
135/* WARNING: this file should *not* be used by applications. It is
136   part of the implementation of the compression library and is
137   subject to change. Applications should only use zlib.h.
138 */
139
140struct inflate_blocks_state;
141typedef struct inflate_blocks_state FAR inflate_blocks_statef;
142
143local inflate_blocks_statef * inflate_blocks_new OF((
144    z_stream *z,
145    check_func c,               /* check function */
146    uInt w));                   /* window size */
147
148local int inflate_blocks OF((
149    inflate_blocks_statef *,
150    z_stream *,
151    int));                      /* initial return code */
152
153local void inflate_blocks_reset OF((
154    inflate_blocks_statef *,
155    z_stream *,
156    uLongf *));                  /* check value on output */
157
158local int inflate_blocks_free OF((
159    inflate_blocks_statef *,
160    z_stream *,
161    uLongf *));                  /* check value on output */
162
163local int inflate_addhistory OF((
164    inflate_blocks_statef *,
165    z_stream *));
166
167local int inflate_packet_flush OF((
168    inflate_blocks_statef *));
169
170/*+++++*/
171/* inftrees.h -- header to use inftrees.c
172 * Copyright (C) 1995 Mark Adler
173 * For conditions of distribution and use, see copyright notice in zlib.h
174 */
175
176/* WARNING: this file should *not* be used by applications. It is
177   part of the implementation of the compression library and is
178   subject to change. Applications should only use zlib.h.
179 */
180
181/* Huffman code lookup table entry--this entry is four bytes for machines
182   that have 16-bit pointers (e.g. PC's in the small or medium model). */
183
184typedef struct inflate_huft_s FAR inflate_huft;
185
186struct inflate_huft_s {
187  union {
188    struct {
189      Byte Exop;        /* number of extra bits or operation */
190      Byte Bits;        /* number of bits in this code or subcode */
191    } what;
192    uInt Nalloc;        /* number of these allocated here */
193    Bytef *pad;         /* pad structure to a power of 2 (4 bytes for */
194  } word;               /*  16-bit, 8 bytes for 32-bit machines) */
195  union {
196    uInt Base;          /* literal, length base, or distance base */
197    inflate_huft *Next; /* pointer to next level of table */
198  } more;
199};
200
201#ifdef DEBUG_ZLIB
202  local uInt inflate_hufts;
203#endif
204
205local int inflate_trees_bits OF((
206    uIntf *,                    /* 19 code lengths */
207    uIntf *,                    /* bits tree desired/actual depth */
208    inflate_huft * FAR *,       /* bits tree result */
209    z_stream *));               /* for zalloc, zfree functions */
210
211local int inflate_trees_dynamic OF((
212    uInt,                       /* number of literal/length codes */
213    uInt,                       /* number of distance codes */
214    uIntf *,                    /* that many (total) code lengths */
215    uIntf *,                    /* literal desired/actual bit depth */
216    uIntf *,                    /* distance desired/actual bit depth */
217    inflate_huft * FAR *,       /* literal/length tree result */
218    inflate_huft * FAR *,       /* distance tree result */
219    z_stream *));               /* for zalloc, zfree functions */
220
221local int inflate_trees_fixed OF((
222    uIntf *,                    /* literal desired/actual bit depth */
223    uIntf *,                    /* distance desired/actual bit depth */
224    inflate_huft * FAR *,       /* literal/length tree result */
225    inflate_huft * FAR *));     /* distance tree result */
226
227local int inflate_trees_free OF((
228    inflate_huft *,             /* tables to free */
229    z_stream *));               /* for zfree function */
230
231/*+++++*/
232/* infcodes.h -- header to use infcodes.c
233 * Copyright (C) 1995 Mark Adler
234 * For conditions of distribution and use, see copyright notice in zlib.h
235 */
236
237/* WARNING: this file should *not* be used by applications. It is
238   part of the implementation of the compression library and is
239   subject to change. Applications should only use zlib.h.
240 */
241
242struct inflate_codes_state;
243typedef struct inflate_codes_state FAR inflate_codes_statef;
244
245local inflate_codes_statef *inflate_codes_new OF((
246    uInt, uInt,
247    inflate_huft *, inflate_huft *,
248    z_stream *));
249
250local int inflate_codes OF((
251    inflate_blocks_statef *,
252    z_stream *,
253    int));
254
255local void inflate_codes_free OF((
256    inflate_codes_statef *,
257    z_stream *));
258
259/*+++++*/
260/* inflate.c -- zlib interface to inflate modules
261 * Copyright (C) 1995 Mark Adler
262 * For conditions of distribution and use, see copyright notice in zlib.h
263 */
264
265/* inflate private state */
266struct internal_state {
267
268  /* mode */
269  enum {
270      METHOD,   /* waiting for method byte */
271      FLAG,     /* waiting for flag byte */
272      BLOCKS,   /* decompressing blocks */
273      CHECK4,   /* four check bytes to go */
274      CHECK3,   /* three check bytes to go */
275      CHECK2,   /* two check bytes to go */
276      CHECK1,   /* one check byte to go */
277      DONE,     /* finished check, done */
278      BAD}      /* got an error--stay here */
279    mode;               /* current inflate mode */
280
281  /* mode dependent information */
282  union {
283    uInt method;        /* if FLAGS, method byte */
284    struct {
285      uLong was;                /* computed check value */
286      uLong need;               /* stream check value */
287    } check;            /* if CHECK, check values to compare */
288    uInt marker;        /* if BAD, inflateSync's marker bytes count */
289  } sub;        /* submode */
290
291  /* mode independent information */
292  int  nowrap;          /* flag for no wrapper */
293  uInt wbits;           /* log2(window size)  (8..15, defaults to 15) */
294  inflate_blocks_statef
295    *blocks;            /* current inflate_blocks state */
296
297};
298
299int inflateReset(z)
300z_stream *z;
301{
302  uLong c;
303
304  if (z == Z_NULL || z->state == Z_NULL)
305    return Z_STREAM_ERROR;
306  z->total_in = z->total_out = 0;
307  z->msg = Z_NULL;
308  z->state->mode = z->state->nowrap ? BLOCKS : METHOD;
309  inflate_blocks_reset(z->state->blocks, z, &c);
310  Trace("inflate: reset\n");
311  return Z_OK;
312}
313
314int inflateEnd(z)
315z_stream *z;
316{
317  uLong c;
318
319  if (z == Z_NULL || z->state == Z_NULL || z->zfree == Z_NULL)
320    return Z_STREAM_ERROR;
321  if (z->state->blocks != Z_NULL)
322    inflate_blocks_free(z->state->blocks, z, &c);
323  ZFREE(z, z->state, sizeof(struct internal_state));
324  z->state = Z_NULL;
325  Trace("inflate: end\n");
326  return Z_OK;
327}
328
329int inflateInit2(z, w)
330z_stream *z;
331int w;
332{
333  /* initialize state */
334  if (z == Z_NULL)
335    return Z_STREAM_ERROR;
336/*  if (z->zalloc == Z_NULL) z->zalloc = zcalloc; */
337/*  if (z->zfree == Z_NULL) z->zfree = zcfree; */
338  if ((z->state = (struct internal_state FAR *)
339       ZALLOC(z,1,sizeof(struct internal_state))) == Z_NULL)
340    return Z_MEM_ERROR;
341  z->state->blocks = Z_NULL;
342
343  /* handle undocumented nowrap option (no zlib header or check) */
344  z->state->nowrap = 0;
345  if (w < 0)
346  {
347    w = - w;
348    z->state->nowrap = 1;
349  }
350
351  /* set window size */
352  if (w < 8 || w > 15)
353  {
354    inflateEnd(z);
355    return Z_STREAM_ERROR;
356  }
357  z->state->wbits = (uInt)w;
358
359  /* create inflate_blocks state */
360  if ((z->state->blocks =
361       inflate_blocks_new(z, z->state->nowrap ? Z_NULL : adler32, 1 << w))
362      == Z_NULL)
363  {
364    inflateEnd(z);
365    return Z_MEM_ERROR;
366  }
367  Trace("inflate: allocated\n");
368
369  /* reset state */
370  inflateReset(z);
371  return Z_OK;
372}
373
374int inflateInit(z)
375z_stream *z;
376{
377  return inflateInit2(z, DEF_WBITS);
378}
379
380#define NEEDBYTE {if(z->avail_in==0)goto empty;r=Z_OK;}
381#define NEXTBYTE (z->avail_in--,z->total_in++,*z->next_in++)
382
383int inflate(z, f)
384z_stream *z;
385int f;
386{
387  int r;
388  uInt b;
389
390  if (z == Z_NULL || z->next_in == Z_NULL)
391    return Z_STREAM_ERROR;
392  r = Z_BUF_ERROR;
393  while (1) switch (z->state->mode)
394  {
395    case METHOD:
396      NEEDBYTE
397      if (((z->state->sub.method = NEXTBYTE) & 0xf) != DEFLATED)
398      {
399        z->state->mode = BAD;
400        z->msg = "unknown compression method";
401        z->state->sub.marker = 5;       /* can't try inflateSync */
402        break;
403      }
404      if ((z->state->sub.method >> 4) + 8 > z->state->wbits)
405      {
406        z->state->mode = BAD;
407        z->msg = "invalid window size";
408        z->state->sub.marker = 5;       /* can't try inflateSync */
409        break;
410      }
411      z->state->mode = FLAG;
412    case FLAG:
413      NEEDBYTE
414      if ((b = NEXTBYTE) & 0x20)
415      {
416        z->state->mode = BAD;
417        z->msg = "invalid reserved bit";
418        z->state->sub.marker = 5;       /* can't try inflateSync */
419        break;
420      }
421      if (((z->state->sub.method << 8) + b) % 31)
422      {
423        z->state->mode = BAD;
424        z->msg = "incorrect header check";
425        z->state->sub.marker = 5;       /* can't try inflateSync */
426        break;
427      }
428      Trace("inflate: zlib header ok\n");
429      z->state->mode = BLOCKS;
430    case BLOCKS:
431      r = inflate_blocks(z->state->blocks, z, r);
432      if (f == Z_PACKET_FLUSH && z->avail_in == 0 && z->avail_out != 0)
433          r = inflate_packet_flush(z->state->blocks);
434      if (r == Z_DATA_ERROR)
435      {
436        z->state->mode = BAD;
437        z->state->sub.marker = 0;       /* can try inflateSync */
438        break;
439      }
440      if (r != Z_STREAM_END)
441        return r;
442      r = Z_OK;
443      inflate_blocks_reset(z->state->blocks, z, &z->state->sub.check.was);
444      if (z->state->nowrap)
445      {
446        z->state->mode = DONE;
447        break;
448      }
449      z->state->mode = CHECK4;
450    case CHECK4:
451      NEEDBYTE
452      z->state->sub.check.need = (uLong)NEXTBYTE << 24;
453      z->state->mode = CHECK3;
454    case CHECK3:
455      NEEDBYTE
456      z->state->sub.check.need += (uLong)NEXTBYTE << 16;
457      z->state->mode = CHECK2;
458    case CHECK2:
459      NEEDBYTE
460      z->state->sub.check.need += (uLong)NEXTBYTE << 8;
461      z->state->mode = CHECK1;
462    case CHECK1:
463      NEEDBYTE
464      z->state->sub.check.need += (uLong)NEXTBYTE;
465
466      if (z->state->sub.check.was != z->state->sub.check.need)
467      {
468        z->state->mode = BAD;
469        z->msg = "incorrect data check";
470        z->state->sub.marker = 5;       /* can't try inflateSync */
471        break;
472      }
473      Trace( "inflate: zlib check ok\n");
474      z->state->mode = DONE;
475    case DONE:
476      return Z_STREAM_END;
477    case BAD:
478      return Z_DATA_ERROR;
479    default:
480      return Z_STREAM_ERROR;
481  }
482
483 empty:
484  if (f != Z_PACKET_FLUSH)
485    return r;
486  z->state->mode = BAD;
487  z->state->sub.marker = 0;       /* can try inflateSync */
488  return Z_DATA_ERROR;
489}
490
491/*
492 * This subroutine adds the data at next_in/avail_in to the output history
493 * without performing any output.  The output buffer must be "caught up";
494 * i.e. no pending output (hence s->read equals s->write), and the state must
495 * be BLOCKS (i.e. we should be willing to see the start of a series of
496 * BLOCKS).  On exit, the output will also be caught up, and the checksum
497 * will have been updated if need be.
498 */
499
500int inflateIncomp(z)
501z_stream *z;
502{
503    if (z->state->mode != BLOCKS)
504        return Z_DATA_ERROR;
505    return inflate_addhistory(z->state->blocks, z);
506}
507
508int inflateSync(z)
509z_stream *z;
510{
511  uInt n;       /* number of bytes to look at */
512  Bytef *p;     /* pointer to bytes */
513  uInt m;       /* number of marker bytes found in a row */
514  uLong r, w;   /* temporaries to save total_in and total_out */
515
516  /* set up */
517  if (z == Z_NULL || z->state == Z_NULL)
518    return Z_STREAM_ERROR;
519  if (z->state->mode != BAD)
520  {
521    z->state->mode = BAD;
522    z->state->sub.marker = 0;
523  }
524  if ((n = z->avail_in) == 0)
525    return Z_BUF_ERROR;
526  p = z->next_in;
527  m = z->state->sub.marker;
528
529  /* search */
530  while (n && m < 4)
531  {
532    if (*p == (Byte)(m < 2 ? 0 : 0xff))
533      m++;
534    else if (*p)
535      m = 0;
536    else
537      m = 4 - m;
538    p++, n--;
539  }
540
541  /* restore */
542  z->total_in += p - z->next_in;
543  z->next_in = p;
544  z->avail_in = n;
545  z->state->sub.marker = m;
546
547  /* return no joy or set up to restart on a new block */
548  if (m != 4)
549    return Z_DATA_ERROR;
550  r = z->total_in;  w = z->total_out;
551  inflateReset(z);
552  z->total_in = r;  z->total_out = w;
553  z->state->mode = BLOCKS;
554  return Z_OK;
555}
556
557#undef NEEDBYTE
558#undef NEXTBYTE
559
560/*+++++*/
561/* infutil.h -- types and macros common to blocks and codes
562 * Copyright (C) 1995 Mark Adler
563 * For conditions of distribution and use, see copyright notice in zlib.h
564 */
565
566/* WARNING: this file should *not* be used by applications. It is
567   part of the implementation of the compression library and is
568   subject to change. Applications should only use zlib.h.
569 */
570
571/* inflate blocks semi-private state */
572struct inflate_blocks_state {
573
574  /* mode */
575  enum {
576      TYPE,     /* get type bits (3, including end bit) */
577      LENS,     /* get lengths for stored */
578      STORED,   /* processing stored block */
579      TABLE,    /* get table lengths */
580      BTREE,    /* get bit lengths tree for a dynamic block */
581      DTREE,    /* get length, distance trees for a dynamic block */
582      CODES,    /* processing fixed or dynamic block */
583      DRY,      /* output remaining window bytes */
584      DONEB,     /* finished last block, done */
585      BADB}      /* got a data error--stuck here */
586    mode;               /* current inflate_block mode */
587
588  /* mode dependent information */
589  union {
590    uInt left;          /* if STORED, bytes left to copy */
591    struct {
592      uInt table;               /* table lengths (14 bits) */
593      uInt index;               /* index into blens (or border) */
594      uIntf *blens;             /* bit lengths of codes */
595      uInt bb;                  /* bit length tree depth */
596      inflate_huft *tb;         /* bit length decoding tree */
597      int nblens;               /* # elements allocated at blens */
598    } trees;            /* if DTREE, decoding info for trees */
599    struct {
600      inflate_huft *tl, *td;    /* trees to free */
601      inflate_codes_statef
602         *codes;
603    } decode;           /* if CODES, current state */
604  } sub;                /* submode */
605  uInt last;            /* true if this block is the last block */
606
607  /* mode independent information */
608  uInt bitk;            /* bits in bit buffer */
609  uLong bitb;           /* bit buffer */
610  Bytef *window;        /* sliding window */
611  Bytef *end;           /* one byte after sliding window */
612  Bytef *read;          /* window read pointer */
613  Bytef *write;         /* window write pointer */
614  check_func checkfn;   /* check function */
615  uLong check;          /* check on output */
616
617};
618
619/* defines for inflate input/output */
620/*   update pointers and return */
621#define UPDBITS {s->bitb=b;s->bitk=k;}
622#define UPDIN {z->avail_in=n;z->total_in+=p-z->next_in;z->next_in=p;}
623#define UPDOUT {s->write=q;}
624#define UPDATE {UPDBITS UPDIN UPDOUT}
625#define LEAVE {UPDATE return inflate_flush(s,z,r);}
626/*   get bytes and bits */
627#define LOADIN {p=z->next_in;n=z->avail_in;b=s->bitb;k=s->bitk;}
628#define NEEDBYTE {if(n)r=Z_OK;else LEAVE}
629#define NEXTBYTE (n--,*p++)
630#define NEEDBITS(j) {while(k<(j)){NEEDBYTE;b|=((uLong)NEXTBYTE)<<k;k+=8;}}
631#define DUMPBITS(j) {b>>=(j);k-=(j);}
632/*   output bytes */
633#define WAVAIL (q<s->read?s->read-q-1:s->end-q)
634#define LOADOUT {q=s->write;m=WAVAIL;}
635#define WRAP {if(q==s->end&&s->read!=s->window){q=s->window;m=WAVAIL;}}
636#define FLUSH {UPDOUT r=inflate_flush(s,z,r); LOADOUT}
637#define NEEDOUT {if(m==0){WRAP if(m==0){FLUSH WRAP if(m==0) LEAVE}}r=Z_OK;}
638#define OUTBYTE(a) {*q++=(Byte)(a);m--;}
639/*   load local pointers */
640#define LOAD {LOADIN LOADOUT}
641
642/* And'ing with mask[n] masks the lower n bits */
643local uInt inflate_mask[] = {
644    0x0000,
645    0x0001, 0x0003, 0x0007, 0x000f, 0x001f, 0x003f, 0x007f, 0x00ff,
646    0x01ff, 0x03ff, 0x07ff, 0x0fff, 0x1fff, 0x3fff, 0x7fff, 0xffff
647};
648
649/* copy as much as possible from the sliding window to the output area */
650local int inflate_flush OF((
651    inflate_blocks_statef *,
652    z_stream *,
653    int));
654
655/*+++++*/
656/* inffast.h -- header to use inffast.c
657 * Copyright (C) 1995 Mark Adler
658 * For conditions of distribution and use, see copyright notice in zlib.h
659 */
660
661/* WARNING: this file should *not* be used by applications. It is
662   part of the implementation of the compression library and is
663   subject to change. Applications should only use zlib.h.
664 */
665
666local int inflate_fast OF((
667    uInt,
668    uInt,
669    inflate_huft *,
670    inflate_huft *,
671    inflate_blocks_statef *,
672    z_stream *));
673
674/*+++++*/
675/* infblock.c -- interpret and process block types to last block
676 * Copyright (C) 1995 Mark Adler
677 * For conditions of distribution and use, see copyright notice in zlib.h
678 */
679
680/* Table for deflate from PKZIP's appnote.txt. */
681local uInt border[] = { /* Order of the bit length code lengths */
682        16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15};
683
684/*
685   Notes beyond the 1.93a appnote.txt:
686
687   1. Distance pointers never point before the beginning of the output
688      stream.
689   2. Distance pointers can point back across blocks, up to 32k away.
690   3. There is an implied maximum of 7 bits for the bit length table and
691      15 bits for the actual data.
692   4. If only one code exists, then it is encoded using one bit.  (Zero
693      would be more efficient, but perhaps a little confusing.)  If two
694      codes exist, they are coded using one bit each (0 and 1).
695   5. There is no way of sending zero distance codes--a dummy must be
696      sent if there are none.  (History: a pre 2.0 version of PKZIP would
697      store blocks with no distance codes, but this was discovered to be
698      too harsh a criterion.)  Valid only for 1.93a.  2.04c does allow
699      zero distance codes, which is sent as one code of zero bits in
700      length.
701   6. There are up to 286 literal/length codes.  Code 256 represents the
702      end-of-block.  Note however that the static length tree defines
703      288 codes just to fill out the Huffman codes.  Codes 286 and 287
704      cannot be used though, since there is no length base or extra bits
705      defined for them.  Similarily, there are up to 30 distance codes.
706      However, static trees define 32 codes (all 5 bits) to fill out the
707      Huffman codes, but the last two had better not show up in the data.
708   7. Unzip can check dynamic Huffman blocks for complete code sets.
709      The exception is that a single code would not be complete (see #4).
710   8. The five bits following the block type is really the number of
711      literal codes sent minus 257.
712   9. Length codes 8,16,16 are interpreted as 13 length codes of 8 bits
713      (1+6+6).  Therefore, to output three times the length, you output
714      three codes (1+1+1), whereas to output four times the same length,
715      you only need two codes (1+3).  Hmm.
716  10. In the tree reconstruction algorithm, Code = Code + Increment
717      only if BitLength(i) is not zero.  (Pretty obvious.)
718  11. Correction: 4 Bits: # of Bit Length codes - 4     (4 - 19)
719  12. Note: length code 284 can represent 227-258, but length code 285
720      really is 258.  The last length deserves its own, short code
721      since it gets used a lot in very redundant files.  The length
722      258 is special since 258 - 3 (the min match length) is 255.
723  13. The literal/length and distance code bit lengths are read as a
724      single stream of lengths.  It is possible (and advantageous) for
725      a repeat code (16, 17, or 18) to go across the boundary between
726      the two sets of lengths.
727 */
728
729local void inflate_blocks_reset(s, z, c)
730inflate_blocks_statef *s;
731z_stream *z;
732uLongf *c;
733{
734  if (s->checkfn != Z_NULL)
735    *c = s->check;
736  if (s->mode == BTREE || s->mode == DTREE)
737    ZFREE(z, s->sub.trees.blens, s->sub.trees.nblens * sizeof(uInt));
738  if (s->mode == CODES)
739  {
740    inflate_codes_free(s->sub.decode.codes, z);
741    inflate_trees_free(s->sub.decode.td, z);
742    inflate_trees_free(s->sub.decode.tl, z);
743  }
744  s->mode = TYPE;
745  s->bitk = 0;
746  s->bitb = 0;
747  s->read = s->write = s->window;
748  if (s->checkfn != Z_NULL)
749    s->check = (*s->checkfn)(0L, Z_NULL, 0);
750  Trace("inflate:   blocks reset\n");
751}
752
753local inflate_blocks_statef *inflate_blocks_new(z, c, w)
754z_stream *z;
755check_func c;
756uInt w;
757{
758  inflate_blocks_statef *s;
759
760  if ((s = (inflate_blocks_statef *)ZALLOC
761       (z,1,sizeof(struct inflate_blocks_state))) == Z_NULL)
762    return s;
763  if ((s->window = (Bytef *)ZALLOC(z, 1, w)) == Z_NULL)
764  {
765    ZFREE(z, s, sizeof(struct inflate_blocks_state));
766    return Z_NULL;
767  }
768  s->end = s->window + w;
769  s->checkfn = c;
770  s->mode = TYPE;
771  Trace("inflate:   blocks allocated\n");
772  inflate_blocks_reset(s, z, &s->check);
773  return s;
774}
775
776local int inflate_blocks(s, z, r)
777inflate_blocks_statef *s;
778z_stream *z;
779int r;
780{
781  uInt t;               /* temporary storage */
782  uLong b;              /* bit buffer */
783  uInt k;               /* bits in bit buffer */
784  Bytef *p;             /* input data pointer */
785  uInt n;               /* bytes available there */
786  Bytef *q;             /* output window write pointer */
787  uInt m;               /* bytes to end of window or read pointer */
788
789  /* copy input/output information to locals (UPDATE macro restores) */
790  LOAD
791
792  /* process input based on current state */
793  while (1) switch (s->mode)
794  {
795    case TYPE:
796      NEEDBITS(3)
797      t = (uInt)b & 7;
798      s->last = t & 1;
799      switch (t >> 1)
800      {
801        case 0:                         /* stored */
802          Trace(("inflate:     stored block%s\n",
803                 s->last ? " (last)" : ""));
804          DUMPBITS(3)
805          t = k & 7;                    /* go to byte boundary */
806          DUMPBITS(t)
807          s->mode = LENS;               /* get length of stored block */
808          break;
809        case 1:                         /* fixed */
810          Trace(( "inflate:     fixed codes block%s\n",
811                 s->last ? " (last)" : ""));
812          {
813            uInt bl, bd;
814            inflate_huft *tl, *td;
815
816            inflate_trees_fixed(&bl, &bd, &tl, &td);
817            s->sub.decode.codes = inflate_codes_new(bl, bd, tl, td, z);
818            if (s->sub.decode.codes == Z_NULL)
819            {
820              r = Z_MEM_ERROR;
821              LEAVE
822            }
823            s->sub.decode.tl = Z_NULL;  /* don't try to free these */
824            s->sub.decode.td = Z_NULL;
825          }
826          DUMPBITS(3)
827          s->mode = CODES;
828          break;
829        case 2:                         /* dynamic */
830          Trace(( "inflate:     dynamic codes block%s\n",
831                 s->last ? " (last)" : ""));
832          DUMPBITS(3)
833          s->mode = TABLE;
834          break;
835        case 3:                         /* illegal */
836          DUMPBITS(3)
837          s->mode = BADB;
838          z->msg = "invalid block type";
839          r = Z_DATA_ERROR;
840          LEAVE
841      }
842      break;
843    case LENS:
844      NEEDBITS(32)
845      if (((~b) >> 16) != (b & 0xffff))
846      {
847        s->mode = BADB;
848        z->msg = "invalid stored block lengths";
849        r = Z_DATA_ERROR;
850        LEAVE
851      }
852      s->sub.left = (uInt)b & 0xffff;
853      b = k = 0;                      /* dump bits */
854      Tracev(( "inflate:       stored length %u\n", s->sub.left));
855      s->mode = s->sub.left ? STORED : TYPE;
856      break;
857    case STORED:
858      if (n == 0)
859        LEAVE
860      NEEDOUT
861      t = s->sub.left;
862      if (t > n) t = n;
863      if (t > m) t = m;
864      zmemcpy(q, p, t);
865      p += t;  n -= t;
866      q += t;  m -= t;
867      if ((s->sub.left -= t) != 0)
868        break;
869      Tracev(( "inflate:       stored end, %lu total out\n",
870              z->total_out + (q >= s->read ? q - s->read :
871              (s->end - s->read) + (q - s->window))));
872      s->mode = s->last ? DRY : TYPE;
873      break;
874    case TABLE:
875      NEEDBITS(14)
876      s->sub.trees.table = t = (uInt)b & 0x3fff;
877#ifndef PKZIP_BUG_WORKAROUND
878      if ((t & 0x1f) > 29 || ((t >> 5) & 0x1f) > 29)
879      {
880        s->mode = BADB;
881        z->msg = "too many length or distance symbols";
882        r = Z_DATA_ERROR;
883        LEAVE
884      }
885#endif
886      t = 258 + (t & 0x1f) + ((t >> 5) & 0x1f);
887      if (t < 19)
888        t = 19;
889      if ((s->sub.trees.blens = (uIntf*)ZALLOC(z, t, sizeof(uInt))) == Z_NULL)
890      {
891        r = Z_MEM_ERROR;
892        LEAVE
893      }
894      s->sub.trees.nblens = t;
895      DUMPBITS(14)
896      s->sub.trees.index = 0;
897      Tracev(( "inflate:       table sizes ok\n"));
898      s->mode = BTREE;
899    case BTREE:
900      while (s->sub.trees.index < 4 + (s->sub.trees.table >> 10))
901      {
902        NEEDBITS(3)
903        s->sub.trees.blens[border[s->sub.trees.index++]] = (uInt)b & 7;
904        DUMPBITS(3)
905      }
906      while (s->sub.trees.index < 19)
907        s->sub.trees.blens[border[s->sub.trees.index++]] = 0;
908      s->sub.trees.bb = 7;
909      t = inflate_trees_bits(s->sub.trees.blens, &s->sub.trees.bb,
910                             &s->sub.trees.tb, z);
911      if (t != Z_OK)
912      {
913        r = t;
914        if (r == Z_DATA_ERROR)
915          s->mode = BADB;
916        LEAVE
917      }
918      s->sub.trees.index = 0;
919      Tracev(( "inflate:       bits tree ok\n"));
920      s->mode = DTREE;
921    case DTREE:
922      while (t = s->sub.trees.table,
923             s->sub.trees.index < 258 + (t & 0x1f) + ((t >> 5) & 0x1f))
924      {
925        inflate_huft *h;
926        uInt i, j, c;
927
928        t = s->sub.trees.bb;
929        NEEDBITS(t)
930        h = s->sub.trees.tb + ((uInt)b & inflate_mask[t]);
931        t = h->word.what.Bits;
932        c = h->more.Base;
933        if (c < 16)
934        {
935          DUMPBITS(t)
936          s->sub.trees.blens[s->sub.trees.index++] = c;
937        }
938        else /* c == 16..18 */
939        {
940          i = c == 18 ? 7 : c - 14;
941          j = c == 18 ? 11 : 3;
942          NEEDBITS(t + i)
943          DUMPBITS(t)
944          j += (uInt)b & inflate_mask[i];
945          DUMPBITS(i)
946          i = s->sub.trees.index;
947          t = s->sub.trees.table;
948          if (i + j > 258 + (t & 0x1f) + ((t >> 5) & 0x1f) ||
949              (c == 16 && i < 1))
950          {
951            s->mode = BADB;
952            z->msg = "invalid bit length repeat";
953            r = Z_DATA_ERROR;
954            LEAVE
955          }
956          c = c == 16 ? s->sub.trees.blens[i - 1] : 0;
957          do {
958            s->sub.trees.blens[i++] = c;
959          } while (--j);
960          s->sub.trees.index = i;
961        }
962      }
963      inflate_trees_free(s->sub.trees.tb, z);
964      s->sub.trees.tb = Z_NULL;
965      {
966        uInt bl, bd;
967        inflate_huft *tl, *td;
968        inflate_codes_statef *c;
969
970        bl = 9;         /* must be <= 9 for lookahead assumptions */
971        bd = 6;         /* must be <= 9 for lookahead assumptions */
972        t = s->sub.trees.table;
973        t = inflate_trees_dynamic(257 + (t & 0x1f), 1 + ((t >> 5) & 0x1f),
974                                  s->sub.trees.blens, &bl, &bd, &tl, &td, z);
975        if (t != Z_OK)
976        {
977          if (t == (uInt)Z_DATA_ERROR)
978            s->mode = BADB;
979          r = t;
980          LEAVE
981        }
982        Tracev(( "inflate:       trees ok\n"));
983        if ((c = inflate_codes_new(bl, bd, tl, td, z)) == Z_NULL)
984        {
985          inflate_trees_free(td, z);
986          inflate_trees_free(tl, z);
987          r = Z_MEM_ERROR;
988          LEAVE
989        }
990        ZFREE(z, s->sub.trees.blens, s->sub.trees.nblens * sizeof(uInt));
991        s->sub.decode.codes = c;
992        s->sub.decode.tl = tl;
993        s->sub.decode.td = td;
994      }
995      s->mode = CODES;
996    case CODES:
997      UPDATE
998      if ((r = inflate_codes(s, z, r)) != Z_STREAM_END)
999        return inflate_flush(s, z, r);
1000      r = Z_OK;
1001      inflate_codes_free(s->sub.decode.codes, z);
1002      inflate_trees_free(s->sub.decode.td, z);
1003      inflate_trees_free(s->sub.decode.tl, z);
1004      LOAD
1005      Tracev(( "inflate:       codes end, %lu total out\n",
1006              z->total_out + (q >= s->read ? q - s->read :
1007              (s->end - s->read) + (q - s->window))));
1008      if (!s->last)
1009      {
1010        s->mode = TYPE;
1011        break;
1012      }
1013      if (k > 7)              /* return unused byte, if any */
1014      {
1015        Assert(k < 16, "inflate_codes grabbed too many bytes")
1016        k -= 8;
1017        n++;
1018        p--;                    /* can always return one */
1019      }
1020      s->mode = DRY;
1021    case DRY:
1022      FLUSH
1023      if (s->read != s->write)
1024        LEAVE
1025      s->mode = DONEB;
1026    case DONEB:
1027      r = Z_STREAM_END;
1028      LEAVE
1029    case BADB:
1030      r = Z_DATA_ERROR;
1031      LEAVE
1032    default:
1033      r = Z_STREAM_ERROR;
1034      LEAVE
1035  }
1036}
1037
1038local int inflate_blocks_free(s, z, c)
1039inflate_blocks_statef *s;
1040z_stream *z;
1041uLongf *c;
1042{
1043  inflate_blocks_reset(s, z, c);
1044  ZFREE(z, s->window, s->end - s->window);
1045  ZFREE(z, s, sizeof(struct inflate_blocks_state));
1046  Trace(( "inflate:   blocks freed\n"));
1047  return Z_OK;
1048}
1049
1050/*
1051 * This subroutine adds the data at next_in/avail_in to the output history
1052 * without performing any output.  The output buffer must be "caught up";
1053 * i.e. no pending output (hence s->read equals s->write), and the state must
1054 * be BLOCKS (i.e. we should be willing to see the start of a series of
1055 * BLOCKS).  On exit, the output will also be caught up, and the checksum
1056 * will have been updated if need be.
1057 */
1058local int inflate_addhistory(s, z)
1059inflate_blocks_statef *s;
1060z_stream *z;
1061{
1062    uLong b;              /* bit buffer */  /* NOT USED HERE */
1063    uInt k;               /* bits in bit buffer */ /* NOT USED HERE */
1064    uInt t;               /* temporary storage */
1065    Bytef *p;             /* input data pointer */
1066    uInt n;               /* bytes available there */
1067    Bytef *q;             /* output window write pointer */
1068    uInt m;               /* bytes to end of window or read pointer */
1069
1070    if (s->read != s->write)
1071        return Z_STREAM_ERROR;
1072    if (s->mode != TYPE)
1073        return Z_DATA_ERROR;
1074
1075    /* we're ready to rock */
1076    LOAD
1077    /* while there is input ready, copy to output buffer, moving
1078     * pointers as needed.
1079     */
1080    while (n) {
1081        t = n;  /* how many to do */
1082        /* is there room until end of buffer? */
1083        if (t > m) t = m;
1084        /* update check information */
1085        if (s->checkfn != Z_NULL)
1086            s->check = (*s->checkfn)(s->check, q, t);
1087        zmemcpy(q, p, t);
1088        q += t;
1089        p += t;
1090        n -= t;
1091        z->total_out += t;
1092        s->read = q;    /* drag read pointer forward */
1093/*      WRAP  */        /* expand WRAP macro by hand to handle s->read */
1094        if (q == s->end) {
1095            s->read = q = s->window;
1096            m = WAVAIL;
1097        }
1098    }
1099    UPDATE
1100    return Z_OK;
1101}
1102
1103/*
1104 * At the end of a Deflate-compressed PPP packet, we expect to have seen
1105 * a `stored' block type value but not the (zero) length bytes.
1106 */
1107local int inflate_packet_flush(s)
1108    inflate_blocks_statef *s;
1109{
1110    if (s->mode != LENS)
1111        return Z_DATA_ERROR;
1112    s->mode = TYPE;
1113    return Z_OK;
1114}
1115
1116/*+++++*/
1117/* inftrees.c -- generate Huffman trees for efficient decoding
1118 * Copyright (C) 1995 Mark Adler
1119 * For conditions of distribution and use, see copyright notice in zlib.h
1120 */
1121
1122/* simplify the use of the inflate_huft type with some defines */
1123#define base more.Base
1124#define next more.Next
1125#define exop word.what.Exop
1126#define bits word.what.Bits
1127
1128local int huft_build OF((
1129    uIntf *,            /* code lengths in bits */
1130    uInt,               /* number of codes */
1131    uInt,               /* number of "simple" codes */
1132    uIntf *,            /* list of base values for non-simple codes */
1133    uIntf *,            /* list of extra bits for non-simple codes */
1134    inflate_huft * FAR*,/* result: starting table */
1135    uIntf *,            /* maximum lookup bits (returns actual) */
1136    z_stream *));       /* for zalloc function */
1137
1138local voidpf falloc OF((
1139    voidpf,             /* opaque pointer (not used) */
1140    uInt,               /* number of items */
1141    uInt));             /* size of item */
1142
1143local void ffree OF((
1144    voidpf q,           /* opaque pointer (not used) */
1145    voidpf p,           /* what to free (not used) */
1146    uInt n));           /* number of bytes (not used) */
1147
1148/* Tables for deflate from PKZIP's appnote.txt. */
1149local uInt cplens[] = { /* Copy lengths for literal codes 257..285 */
1150        3, 4, 5, 6, 7, 8, 9, 10, 11, 13, 15, 17, 19, 23, 27, 31,
1151        35, 43, 51, 59, 67, 83, 99, 115, 131, 163, 195, 227, 258, 0, 0};
1152        /* actually lengths - 2; also see note #13 above about 258 */
1153local uInt cplext[] = { /* Extra bits for literal codes 257..285 */
1154        0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2,
1155        3, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5, 0, 192, 192}; /* 192==invalid */
1156local uInt cpdist[] = { /* Copy offsets for distance codes 0..29 */
1157        1, 2, 3, 4, 5, 7, 9, 13, 17, 25, 33, 49, 65, 97, 129, 193,
1158        257, 385, 513, 769, 1025, 1537, 2049, 3073, 4097, 6145,
1159        8193, 12289, 16385, 24577};
1160local uInt cpdext[] = { /* Extra bits for distance codes */
1161        0, 0, 0, 0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6,
1162        7, 7, 8, 8, 9, 9, 10, 10, 11, 11,
1163        12, 12, 13, 13};
1164
1165/*
1166   Huffman code decoding is performed using a multi-level table lookup.
1167   The fastest way to decode is to simply build a lookup table whose
1168   size is determined by the longest code.  However, the time it takes
1169   to build this table can also be a factor if the data being decoded
1170   is not very long.  The most common codes are necessarily the
1171   shortest codes, so those codes dominate the decoding time, and hence
1172   the speed.  The idea is you can have a shorter table that decodes the
1173   shorter, more probable codes, and then point to subsidiary tables for
1174   the longer codes.  The time it costs to decode the longer codes is
1175   then traded against the time it takes to make longer tables.
1176
1177   This results of this trade are in the variables lbits and dbits
1178   below.  lbits is the number of bits the first level table for literal/
1179   length codes can decode in one step, and dbits is the same thing for
1180   the distance codes.  Subsequent tables are also less than or equal to
1181   those sizes.  These values may be adjusted either when all of the
1182   codes are shorter than that, in which case the longest code length in
1183   bits is used, or when the shortest code is *longer* than the requested
1184   table size, in which case the length of the shortest code in bits is
1185   used.
1186
1187   There are two different values for the two tables, since they code a
1188   different number of possibilities each.  The literal/length table
1189   codes 286 possible values, or in a flat code, a little over eight
1190   bits.  The distance table codes 30 possible values, or a little less
1191   than five bits, flat.  The optimum values for speed end up being
1192   about one bit more than those, so lbits is 8+1 and dbits is 5+1.
1193   The optimum values may differ though from machine to machine, and
1194   possibly even between compilers.  Your mileage may vary.
1195 */
1196
1197/* If BMAX needs to be larger than 16, then h and x[] should be uLong. */
1198#define BMAX 15         /* maximum bit length of any code */
1199#define N_MAX 288       /* maximum number of codes in any set */
1200
1201#ifdef DEBUG_ZLIB
1202  uInt inflate_hufts;
1203#endif
1204
1205local int huft_build(b, n, s, d, e, t, m, zs)
1206uIntf *b;               /* code lengths in bits (all assumed <= BMAX) */
1207uInt n;                 /* number of codes (assumed <= N_MAX) */
1208uInt s;                 /* number of simple-valued codes (0..s-1) */
1209uIntf *d;               /* list of base values for non-simple codes */
1210uIntf *e;               /* list of extra bits for non-simple codes */
1211inflate_huft * FAR *t;  /* result: starting table */
1212uIntf *m;               /* maximum lookup bits, returns actual */
1213z_stream *zs;           /* for zalloc function */
1214/* Given a list of code lengths and a maximum table size, make a set of
1215   tables to decode that set of codes.  Return Z_OK on success, Z_BUF_ERROR
1216   if the given code set is incomplete (the tables are still built in this
1217   case), Z_DATA_ERROR if the input is invalid (all zero length codes or an
1218   over-subscribed set of lengths), or Z_MEM_ERROR if not enough memory. */
1219{
1220
1221  uInt a;                       /* counter for codes of length k */
1222  uInt c[BMAX+1];               /* bit length count table */
1223  uInt f;                       /* i repeats in table every f entries */
1224  int g;                        /* maximum code length */
1225  int h;                        /* table level */
1226  register uInt i;              /* counter, current code */
1227  register uInt j;              /* counter */
1228  register int k;               /* number of bits in current code */
1229  int l;                        /* bits per table (returned in m) */
1230  register uIntf *p;            /* pointer into c[], b[], or v[] */
1231  inflate_huft *q;              /* points to current table */
1232  struct inflate_huft_s r;      /* table entry for structure assignment */
1233  inflate_huft *u[BMAX];        /* table stack */
1234  uInt v[N_MAX];                /* values in order of bit length */
1235  register int w;               /* bits before this table == (l * h) */
1236  uInt x[BMAX+1];               /* bit offsets, then code stack */
1237  uIntf *xp;                    /* pointer into x */
1238  int y;                        /* number of dummy codes added */
1239  uInt z;                       /* number of entries in current table */
1240
1241  /* Generate counts for each bit length */
1242  p = c;
1243#define C0 *p++ = 0;
1244#define C2 C0 C0 C0 C0
1245#define C4 C2 C2 C2 C2
1246  C4                            /* clear c[]--assume BMAX+1 is 16 */
1247  p = b;  i = n;
1248  do {
1249    c[*p++]++;                  /* assume all entries <= BMAX */
1250  } while (--i);
1251  if (c[0] == n)                /* null input--all zero length codes */
1252  {
1253    *t = (inflate_huft *)Z_NULL;
1254    *m = 0;
1255    return Z_OK;
1256  }
1257
1258  /* Find minimum and maximum length, bound *m by those */
1259  l = *m;
1260  for (j = 1; j <= BMAX; j++)
1261    if (c[j])
1262      break;
1263  k = j;                        /* minimum code length */
1264  if ((uInt)l < j)
1265    l = j;
1266  for (i = BMAX; i; i--)
1267    if (c[i])
1268      break;
1269  g = i;                        /* maximum code length */
1270  if ((uInt)l > i)
1271    l = i;
1272  *m = l;
1273
1274  /* Adjust last length count to fill out codes, if needed */
1275  for (y = 1 << j; j < i; j++, y <<= 1)
1276    if ((y -= c[j]) < 0)
1277      return Z_DATA_ERROR;
1278  if ((y -= c[i]) < 0)
1279    return Z_DATA_ERROR;
1280  c[i] += y;
1281
1282  /* Generate starting offsets into the value table for each length */
1283  x[1] = j = 0;
1284  p = c + 1;  xp = x + 2;
1285  while (--i) {                 /* note that i == g from above */
1286    *xp++ = (j += *p++);
1287  }
1288
1289  /* Make a table of values in order of bit lengths */
1290  p = b;  i = 0;
1291  do {
1292    if ((j = *p++) != 0)
1293      v[x[j]++] = i;
1294  } while (++i < n);
1295
1296  /* Generate the Huffman codes and for each, make the table entries */
1297  x[0] = i = 0;                 /* first Huffman code is zero */
1298  p = v;                        /* grab values in bit order */
1299  h = -1;                       /* no tables yet--level -1 */
1300  w = -l;                       /* bits decoded == (l * h) */
1301  u[0] = (inflate_huft *)Z_NULL;        /* just to keep compilers happy */
1302  q = (inflate_huft *)Z_NULL;   /* ditto */
1303  z = 0;                        /* ditto */
1304
1305  /* go through the bit lengths (k already is bits in shortest code) */
1306  for (; k <= g; k++)
1307  {
1308    a = c[k];
1309    while (a--)
1310    {
1311      /* here i is the Huffman code of length k bits for value *p */
1312      /* make tables up to required level */
1313      while (k > w + l)
1314      {
1315        h++;
1316        w += l;                 /* previous table always l bits */
1317
1318        /* compute minimum size table less than or equal to l bits */
1319        z = (z = g - w) > (uInt)l ? l : z;      /* table size upper limit */
1320        if ((f = 1 << (j = k - w)) > a + 1)     /* try a k-w bit table */
1321        {                       /* too few codes for k-w bit table */
1322          f -= a + 1;           /* deduct codes from patterns left */
1323          xp = c + k;
1324          if (j < z)
1325            while (++j < z)     /* try smaller tables up to z bits */
1326            {
1327              if ((f <<= 1) <= *++xp)
1328                break;          /* enough codes to use up j bits */
1329              f -= *xp;         /* else deduct codes from patterns */
1330            }
1331        }
1332        z = 1 << j;             /* table entries for j-bit table */
1333
1334        /* allocate and link in new table */
1335        if ((q = (inflate_huft *)ZALLOC
1336             (zs,z + 1,sizeof(inflate_huft))) == Z_NULL)
1337        {
1338          if (h)
1339            inflate_trees_free(u[0], zs);
1340          return Z_MEM_ERROR;   /* not enough memory */
1341        }
1342        q->word.Nalloc = z + 1;
1343#ifdef DEBUG_ZLIB
1344        inflate_hufts += z + 1;
1345#endif
1346        *t = q + 1;             /* link to list for huft_free() */
1347        *(t = &(q->next)) = Z_NULL;
1348        u[h] = ++q;             /* table starts after link */
1349
1350        /* connect to last table, if there is one */
1351        if (h)
1352        {
1353          x[h] = i;             /* save pattern for backing up */
1354          r.bits = (Byte)l;     /* bits to dump before this table */
1355          r.exop = (Byte)j;     /* bits in this table */
1356          r.next = q;           /* pointer to this table */
1357          j = i >> (w - l);     /* (get around Turbo C bug) */
1358          u[h-1][j] = r;        /* connect to last table */
1359        }
1360      }
1361
1362      /* set up table entry in r */
1363      r.bits = (Byte)(k - w);
1364      if (p >= v + n)
1365        r.exop = 128 + 64;      /* out of values--invalid code */
1366      else if (*p < s)
1367      {
1368        r.exop = (Byte)(*p < 256 ? 0 : 32 + 64);     /* 256 is end-of-block */
1369        r.base = *p++;          /* simple code is just the value */
1370      }
1371      else
1372      {
1373        r.exop = (Byte)e[*p - s] + 16 + 64; /* non-simple--look up in lists */
1374        r.base = d[*p++ - s];
1375      }
1376
1377      /* fill code-like entries with r */
1378      f = 1 << (k - w);
1379      for (j = i >> w; j < z; j += f)
1380        q[j] = r;
1381
1382      /* backwards increment the k-bit code i */
1383      for (j = 1 << (k - 1); i & j; j >>= 1)
1384        i ^= j;
1385      i ^= j;
1386
1387      /* backup over finished tables */
1388      while ((i & ((1 << w) - 1)) != x[h])
1389      {
1390        h--;                    /* don't need to update q */
1391        w -= l;
1392      }
1393    }
1394  }
1395
1396  /* Return Z_BUF_ERROR if we were given an incomplete table */
1397  return y != 0 && g != 1 ? Z_BUF_ERROR : Z_OK;
1398}
1399
1400local int inflate_trees_bits(c, bb, tb, z)
1401uIntf *c;               /* 19 code lengths */
1402uIntf *bb;              /* bits tree desired/actual depth */
1403inflate_huft * FAR *tb; /* bits tree result */
1404z_stream *z;            /* for zfree function */
1405{
1406  int r;
1407
1408  r = huft_build(c, 19, 19, (uIntf*)Z_NULL, (uIntf*)Z_NULL, tb, bb, z);
1409  if (r == Z_DATA_ERROR)
1410    z->msg = "oversubscribed dynamic bit lengths tree";
1411  else if (r == Z_BUF_ERROR)
1412  {
1413    inflate_trees_free(*tb, z);
1414    z->msg = "incomplete dynamic bit lengths tree";
1415    r = Z_DATA_ERROR;
1416  }
1417  return r;
1418}
1419
1420local int inflate_trees_dynamic(nl, nd, c, bl, bd, tl, td, z)
1421uInt nl;                /* number of literal/length codes */
1422uInt nd;                /* number of distance codes */
1423uIntf *c;               /* that many (total) code lengths */
1424uIntf *bl;              /* literal desired/actual bit depth */
1425uIntf *bd;              /* distance desired/actual bit depth */
1426inflate_huft * FAR *tl; /* literal/length tree result */
1427inflate_huft * FAR *td; /* distance tree result */
1428z_stream *z;            /* for zfree function */
1429{
1430  int r;
1431
1432  /* build literal/length tree */
1433  if ((r = huft_build(c, nl, 257, cplens, cplext, tl, bl, z)) != Z_OK)
1434  {
1435    if (r == Z_DATA_ERROR)
1436      z->msg = "oversubscribed literal/length tree";
1437    else if (r == Z_BUF_ERROR)
1438    {
1439      inflate_trees_free(*tl, z);
1440      z->msg = "incomplete literal/length tree";
1441      r = Z_DATA_ERROR;
1442    }
1443    return r;
1444  }
1445
1446  /* build distance tree */
1447  if ((r = huft_build(c + nl, nd, 0, cpdist, cpdext, td, bd, z)) != Z_OK)
1448  {
1449    if (r == Z_DATA_ERROR)
1450      z->msg = "oversubscribed literal/length tree";
1451    else if (r == Z_BUF_ERROR) {
1452#ifdef PKZIP_BUG_WORKAROUND
1453      r = Z_OK;
1454    }
1455#else
1456      inflate_trees_free(*td, z);
1457      z->msg = "incomplete literal/length tree";
1458      r = Z_DATA_ERROR;
1459    }
1460    inflate_trees_free(*tl, z);
1461    return r;
1462#endif
1463  }
1464
1465  /* done */
1466  return Z_OK;
1467}
1468
1469/* build fixed tables only once--keep them here */
1470local int fixed_lock = 0;
1471local int fixed_built = 0;
1472#define FIXEDH 530      /* number of hufts used by fixed tables */
1473local uInt fixed_left = FIXEDH;
1474local inflate_huft fixed_mem[FIXEDH];
1475local uInt fixed_bl;
1476local uInt fixed_bd;
1477local inflate_huft *fixed_tl;
1478local inflate_huft *fixed_td;
1479
1480local voidpf falloc(q, n, s)
1481voidpf q;        /* opaque pointer (not used) */
1482uInt n;         /* number of items */
1483uInt s;         /* size of item */
1484{
1485  Assert(s == sizeof(inflate_huft) && n <= fixed_left,
1486         "inflate_trees falloc overflow");
1487  if (q) s++; /* to make some compilers happy */
1488  fixed_left -= n;
1489  return (voidpf)(fixed_mem + fixed_left);
1490}
1491
1492local void ffree(q, p, n)
1493voidpf q;
1494voidpf p;
1495uInt n;
1496{
1497  Assert(0, "inflate_trees ffree called!");
1498  if (q) q = p; /* to make some compilers happy */
1499}
1500
1501local int inflate_trees_fixed(bl, bd, tl, td)
1502uIntf *bl;               /* literal desired/actual bit depth */
1503uIntf *bd;               /* distance desired/actual bit depth */
1504inflate_huft * FAR *tl;  /* literal/length tree result */
1505inflate_huft * FAR *td;  /* distance tree result */
1506{
1507  /* build fixed tables if not built already--lock out other instances */
1508  while (++fixed_lock > 1)
1509    fixed_lock--;
1510  if (!fixed_built)
1511  {
1512    int k;              /* temporary variable */
1513    unsigned c[288];    /* length list for huft_build */
1514    z_stream z;         /* for falloc function */
1515
1516    /* set up fake z_stream for memory routines */
1517    z.zalloc = falloc;
1518    z.zfree = ffree;
1519    z.opaque = Z_NULL;
1520
1521    /* literal table */
1522    for (k = 0; k < 144; k++)
1523      c[k] = 8;
1524    for (; k < 256; k++)
1525      c[k] = 9;
1526    for (; k < 280; k++)
1527      c[k] = 7;
1528    for (; k < 288; k++)
1529      c[k] = 8;
1530    fixed_bl = 7;
1531    huft_build(c, 288, 257, cplens, cplext, &fixed_tl, &fixed_bl, &z);
1532
1533    /* distance table */
1534    for (k = 0; k < 30; k++)
1535      c[k] = 5;
1536    fixed_bd = 5;
1537    huft_build(c, 30, 0, cpdist, cpdext, &fixed_td, &fixed_bd, &z);
1538
1539    /* done */
1540    fixed_built = 1;
1541  }
1542  fixed_lock--;
1543  *bl = fixed_bl;
1544  *bd = fixed_bd;
1545  *tl = fixed_tl;
1546  *td = fixed_td;
1547  return Z_OK;
1548}
1549
1550local int inflate_trees_free(t, z)
1551inflate_huft *t;        /* table to free */
1552z_stream *z;            /* for zfree function */
1553/* Free the malloc'ed tables built by huft_build(), which makes a linked
1554   list of the tables it made, with the links in a dummy first entry of
1555   each table. */
1556{
1557  register inflate_huft *p, *q;
1558
1559  /* Go through linked list, freeing from the malloced (t[-1]) address. */
1560  p = t;
1561  while (p != Z_NULL)
1562  {
1563    q = (--p)->next;
1564    ZFREE(z, p, p->word.Nalloc * sizeof(inflate_huft));
1565    p = q;
1566  }
1567  return Z_OK;
1568}
1569
1570/*+++++*/
1571/* infcodes.c -- process literals and length/distance pairs
1572 * Copyright (C) 1995 Mark Adler
1573 * For conditions of distribution and use, see copyright notice in zlib.h
1574 */
1575
1576/* simplify the use of the inflate_huft type with some defines */
1577#define base more.Base
1578#define next more.Next
1579#define exop word.what.Exop
1580#define bits word.what.Bits
1581
1582/* inflate codes private state */
1583struct inflate_codes_state {
1584
1585  /* mode */
1586  enum {        /* waiting for "i:"=input, "o:"=output, "x:"=nothing */
1587      START,    /* x: set up for LEN */
1588      LEN,      /* i: get length/literal/eob next */
1589      LENEXT,   /* i: getting length extra (have base) */
1590      DIST,     /* i: get distance next */
1591      DISTEXT,  /* i: getting distance extra */
1592      COPY,     /* o: copying bytes in window, waiting for space */
1593      LIT,      /* o: got literal, waiting for output space */
1594      WASH,     /* o: got eob, possibly still output waiting */
1595      END,      /* x: got eob and all data flushed */
1596      BADCODE}  /* x: got error */
1597    mode;               /* current inflate_codes mode */
1598
1599  /* mode dependent information */
1600  uInt len;
1601  union {
1602    struct {
1603      inflate_huft *tree;       /* pointer into tree */
1604      uInt need;                /* bits needed */
1605    } code;             /* if LEN or DIST, where in tree */
1606    uInt lit;           /* if LIT, literal */
1607    struct {
1608      uInt get;                 /* bits to get for extra */
1609      uInt dist;                /* distance back to copy from */
1610    } copy;             /* if EXT or COPY, where and how much */
1611  } sub;                /* submode */
1612
1613  /* mode independent information */
1614  Byte lbits;           /* ltree bits decoded per branch */
1615  Byte dbits;           /* dtree bits decoder per branch */
1616  inflate_huft *ltree;          /* literal/length/eob tree */
1617  inflate_huft *dtree;          /* distance tree */
1618
1619};
1620
1621local inflate_codes_statef *inflate_codes_new(bl, bd, tl, td, z)
1622uInt bl, bd;
1623inflate_huft *tl, *td;
1624z_stream *z;
1625{
1626  inflate_codes_statef *c;
1627
1628  if ((c = (inflate_codes_statef *)
1629       ZALLOC(z,1,sizeof(struct inflate_codes_state))) != Z_NULL)
1630  {
1631    c->mode = START;
1632    c->lbits = (Byte)bl;
1633    c->dbits = (Byte)bd;
1634    c->ltree = tl;
1635    c->dtree = td;
1636    Tracev(( "inflate:       codes new\n"));
1637  }
1638  return c;
1639}
1640
1641local int inflate_codes(s, z, r)
1642inflate_blocks_statef *s;
1643z_stream *z;
1644int r;
1645{
1646  uInt j;               /* temporary storage */
1647  inflate_huft *t;      /* temporary pointer */
1648  uInt e;               /* extra bits or operation */
1649  uLong b;              /* bit buffer */
1650  uInt k;               /* bits in bit buffer */
1651  Bytef *p;             /* input data pointer */
1652  uInt n;               /* bytes available there */
1653  Bytef *q;             /* output window write pointer */
1654  uInt m;               /* bytes to end of window or read pointer */
1655  Bytef *f;             /* pointer to copy strings from */
1656  inflate_codes_statef *c = s->sub.decode.codes;  /* codes state */
1657
1658  /* copy input/output information to locals (UPDATE macro restores) */
1659  LOAD
1660
1661  /* process input and output based on current state */
1662  while (1) switch (c->mode)
1663  {             /* waiting for "i:"=input, "o:"=output, "x:"=nothing */
1664    case START:         /* x: set up for LEN */
1665#ifndef SLOW
1666      if (m >= 258 && n >= 10)
1667      {
1668        UPDATE
1669        r = inflate_fast(c->lbits, c->dbits, c->ltree, c->dtree, s, z);
1670        LOAD
1671        if (r != Z_OK)
1672        {
1673          c->mode = r == Z_STREAM_END ? WASH : BADCODE;
1674          break;
1675        }
1676      }
1677#endif /* !SLOW */
1678      c->sub.code.need = c->lbits;
1679      c->sub.code.tree = c->ltree;
1680      c->mode = LEN;
1681    case LEN:           /* i: get length/literal/eob next */
1682      j = c->sub.code.need;
1683      NEEDBITS(j)
1684      t = c->sub.code.tree + ((uInt)b & inflate_mask[j]);
1685      DUMPBITS(t->bits)
1686      e = (uInt)(t->exop);
1687      if (e == 0)               /* literal */
1688      {
1689        c->sub.lit = t->base;
1690        Tracevv(( t->base >= 0x20 && t->base < 0x7f ?
1691                 "inflate:         literal '%c'\n" :
1692                 "inflate:         literal 0x%02x\n", t->base));
1693        c->mode = LIT;
1694        break;
1695      }
1696      if (e & 16)               /* length */
1697      {
1698        c->sub.copy.get = e & 15;
1699        c->len = t->base;
1700        c->mode = LENEXT;
1701        break;
1702      }
1703      if ((e & 64) == 0)        /* next table */
1704      {
1705        c->sub.code.need = e;
1706        c->sub.code.tree = t->next;
1707        break;
1708      }
1709      if (e & 32)               /* end of block */
1710      {
1711        Tracevv(( "inflate:         end of block\n"));
1712        c->mode = WASH;
1713        break;
1714      }
1715      c->mode = BADCODE;        /* invalid code */
1716      z->msg = "invalid literal/length code";
1717      r = Z_DATA_ERROR;
1718      LEAVE
1719    case LENEXT:        /* i: getting length extra (have base) */
1720      j = c->sub.copy.get;
1721      NEEDBITS(j)
1722      c->len += (uInt)b & inflate_mask[j];
1723      DUMPBITS(j)
1724      c->sub.code.need = c->dbits;
1725      c->sub.code.tree = c->dtree;
1726      Tracevv(( "inflate:         length %u\n", c->len));
1727      c->mode = DIST;
1728    case DIST:          /* i: get distance next */
1729      j = c->sub.code.need;
1730      NEEDBITS(j)
1731      t = c->sub.code.tree + ((uInt)b & inflate_mask[j]);
1732      DUMPBITS(t->bits)
1733      e = (uInt)(t->exop);
1734      if (e & 16)               /* distance */
1735      {
1736        c->sub.copy.get = e & 15;
1737        c->sub.copy.dist = t->base;
1738        c->mode = DISTEXT;
1739        break;
1740      }
1741      if ((e & 64) == 0)        /* next table */
1742      {
1743        c->sub.code.need = e;
1744        c->sub.code.tree = t->next;
1745        break;
1746      }
1747      c->mode = BADCODE;        /* invalid code */
1748      z->msg = "invalid distance code";
1749      r = Z_DATA_ERROR;
1750      LEAVE
1751    case DISTEXT:       /* i: getting distance extra */
1752      j = c->sub.copy.get;
1753      NEEDBITS(j)
1754      c->sub.copy.dist += (uInt)b & inflate_mask[j];
1755      DUMPBITS(j)
1756      Tracevv(( "inflate:         distance %u\n", c->sub.copy.dist));
1757      c->mode = COPY;
1758    case COPY:          /* o: copying bytes in window, waiting for space */
1759#ifndef __TURBOC__ /* Turbo C bug for following expression */
1760      f = (uInt)(q - s->window) < c->sub.copy.dist ?
1761          s->end - (c->sub.copy.dist - (q - s->window)) :
1762          q - c->sub.copy.dist;
1763#else
1764      f = q - c->sub.copy.dist;
1765      if ((uInt)(q - s->window) < c->sub.copy.dist)
1766        f = s->end - (c->sub.copy.dist - (q - s->window));
1767#endif
1768      while (c->len)
1769      {
1770        NEEDOUT
1771        OUTBYTE(*f++)
1772        if (f == s->end)
1773          f = s->window;
1774        c->len--;
1775      }
1776      c->mode = START;
1777      break;
1778    case LIT:           /* o: got literal, waiting for output space */
1779      NEEDOUT
1780      OUTBYTE(c->sub.lit)
1781      c->mode = START;
1782      break;
1783    case WASH:          /* o: got eob, possibly more output */
1784      FLUSH
1785      if (s->read != s->write)
1786        LEAVE
1787      c->mode = END;
1788    case END:
1789      r = Z_STREAM_END;
1790      LEAVE
1791    case BADCODE:       /* x: got error */
1792      r = Z_DATA_ERROR;
1793      LEAVE
1794    default:
1795      r = Z_STREAM_ERROR;
1796      LEAVE
1797  }
1798}
1799
1800local void inflate_codes_free(c, z)
1801inflate_codes_statef *c;
1802z_stream *z;
1803{
1804  ZFREE(z, c, sizeof(struct inflate_codes_state));
1805  Tracev(( "inflate:       codes free\n"));
1806}
1807
1808/*+++++*/
1809/* inflate_util.c -- data and routines common to blocks and codes
1810 * Copyright (C) 1995 Mark Adler
1811 * For conditions of distribution and use, see copyright notice in zlib.h
1812 */
1813
1814/* copy as much as possible from the sliding window to the output area */
1815local int inflate_flush(s, z, r)
1816inflate_blocks_statef *s;
1817z_stream *z;
1818int r;
1819{
1820  uInt n;
1821  Bytef *p, *q;
1822
1823  /* local copies of source and destination pointers */
1824  p = z->next_out;
1825  q = s->read;
1826
1827  /* compute number of bytes to copy as far as end of window */
1828  n = (uInt)((q <= s->write ? s->write : s->end) - q);
1829  if (n > z->avail_out) n = z->avail_out;
1830  if (n && r == Z_BUF_ERROR) r = Z_OK;
1831
1832  /* update counters */
1833  z->avail_out -= n;
1834  z->total_out += n;
1835
1836  /* update check information */
1837  if (s->checkfn != Z_NULL)
1838    s->check = (*s->checkfn)(s->check, q, n);
1839
1840  /* copy as far as end of window */
1841  zmemcpy(p, q, n);
1842  p += n;
1843  q += n;
1844
1845  /* see if more to copy at beginning of window */
1846  if (q == s->end)
1847  {
1848    /* wrap pointers */
1849    q = s->window;
1850    if (s->write == s->end)
1851      s->write = s->window;
1852
1853    /* compute bytes to copy */
1854    n = (uInt)(s->write - q);
1855    if (n > z->avail_out) n = z->avail_out;
1856    if (n && r == Z_BUF_ERROR) r = Z_OK;
1857
1858    /* update counters */
1859    z->avail_out -= n;
1860    z->total_out += n;
1861
1862    /* update check information */
1863    if (s->checkfn != Z_NULL)
1864      s->check = (*s->checkfn)(s->check, q, n);
1865
1866    /* copy */
1867    zmemcpy(p, q, n);
1868    p += n;
1869    q += n;
1870  }
1871
1872  /* update pointers */
1873  z->next_out = p;
1874  s->read = q;
1875
1876  /* done */
1877  return r;
1878}
1879
1880/*+++++*/
1881/* inffast.c -- process literals and length/distance pairs fast
1882 * Copyright (C) 1995 Mark Adler
1883 * For conditions of distribution and use, see copyright notice in zlib.h
1884 */
1885
1886/* simplify the use of the inflate_huft type with some defines */
1887#define base more.Base
1888#define next more.Next
1889#define exop word.what.Exop
1890#define bits word.what.Bits
1891
1892/* macros for bit input with no checking and for returning unused bytes */
1893#define GRABBITS(j) {while(k<(j)){b|=((uLong)NEXTBYTE)<<k;k+=8;}}
1894#define UNGRAB {n+=(c=k>>3);p-=c;k&=7;}
1895
1896/* Called with number of bytes left to write in window at least 258
1897   (the maximum string length) and number of input bytes available
1898   at least ten.  The ten bytes are six bytes for the longest length/
1899   distance pair plus four bytes for overloading the bit buffer. */
1900
1901local int inflate_fast(bl, bd, tl, td, s, z)
1902uInt bl, bd;
1903inflate_huft *tl, *td;
1904inflate_blocks_statef *s;
1905z_stream *z;
1906{
1907  inflate_huft *t;      /* temporary pointer */
1908  uInt e;               /* extra bits or operation */
1909  uLong b;              /* bit buffer */
1910  uInt k;               /* bits in bit buffer */
1911  Bytef *p;             /* input data pointer */
1912  uInt n;               /* bytes available there */
1913  Bytef *q;             /* output window write pointer */
1914  uInt m;               /* bytes to end of window or read pointer */
1915  uInt ml;              /* mask for literal/length tree */
1916  uInt md;              /* mask for distance tree */
1917  uInt c;               /* bytes to copy */
1918  uInt d;               /* distance back to copy from */
1919  Bytef *r;             /* copy source pointer */
1920
1921  /* load input, output, bit values */
1922  LOAD
1923
1924  /* initialize masks */
1925  ml = inflate_mask[bl];
1926  md = inflate_mask[bd];
1927
1928  /* do until not enough input or output space for fast loop */
1929  do {                          /* assume called with m >= 258 && n >= 10 */
1930    /* get literal/length code */
1931    GRABBITS(20)                /* max bits for literal/length code */
1932    if ((e = (t = tl + ((uInt)b & ml))->exop) == 0)
1933    {
1934      DUMPBITS(t->bits)
1935      Tracevv(( t->base >= 0x20 && t->base < 0x7f ?
1936                "inflate:         * literal '%c'\n" :
1937                "inflate:         * literal 0x%02x\n", t->base));
1938      *q++ = (Byte)t->base;
1939      m--;
1940      continue;
1941    }
1942    do {
1943      DUMPBITS(t->bits)
1944      if (e & 16)
1945      {
1946        /* get extra bits for length */
1947        e &= 15;
1948        c = t->base + ((uInt)b & inflate_mask[e]);
1949        DUMPBITS(e)
1950        Tracevv(( "inflate:         * length %u\n", c));
1951
1952        /* decode distance base of block to copy */
1953        GRABBITS(15);           /* max bits for distance code */
1954        e = (t = td + ((uInt)b & md))->exop;
1955        do {
1956          DUMPBITS(t->bits)
1957          if (e & 16)
1958          {
1959            /* get extra bits to add to distance base */
1960            e &= 15;
1961            GRABBITS(e)         /* get extra bits (up to 13) */
1962            d = t->base + ((uInt)b & inflate_mask[e]);
1963            DUMPBITS(e)
1964            Tracevv(( "inflate:         * distance %u\n", d));
1965
1966            /* do the copy */
1967            m -= c;
1968            if ((uInt)(q - s->window) >= d)     /* offset before dest */
1969            {                                   /*  just copy */
1970              r = q - d;
1971              *q++ = *r++;  c--;        /* minimum count is three, */
1972              *q++ = *r++;  c--;        /*  so unroll loop a little */
1973            }
1974            else                        /* else offset after destination */
1975            {
1976              e = d - (q - s->window);  /* bytes from offset to end */
1977              r = s->end - e;           /* pointer to offset */
1978              if (c > e)                /* if source crosses, */
1979              {
1980                c -= e;                 /* copy to end of window */
1981                do {
1982                  *q++ = *r++;
1983                } while (--e);
1984                r = s->window;          /* copy rest from start of window */
1985              }
1986            }
1987            do {                        /* copy all or what's left */
1988              *q++ = *r++;
1989            } while (--c);
1990            break;
1991          }
1992          else if ((e & 64) == 0)
1993            e = (t = t->next + ((uInt)b & inflate_mask[e]))->exop;
1994          else
1995          {
1996            z->msg = "invalid distance code";
1997            UNGRAB
1998            UPDATE
1999            return Z_DATA_ERROR;
2000          }
2001        } while (1);
2002        break;
2003      }
2004      if ((e & 64) == 0)
2005      {
2006        if ((e = (t = t->next + ((uInt)b & inflate_mask[e]))->exop) == 0)
2007        {
2008          DUMPBITS(t->bits)
2009          Tracevv(( t->base >= 0x20 && t->base < 0x7f ?
2010                    "inflate:         * literal '%c'\n" :
2011                    "inflate:         * literal 0x%02x\n", t->base));
2012          *q++ = (Byte)t->base;
2013          m--;
2014          break;
2015        }
2016      }
2017      else if (e & 32)
2018      {
2019        Tracevv(( "inflate:         * end of block\n"));
2020        UNGRAB
2021        UPDATE
2022        return Z_STREAM_END;
2023      }
2024      else
2025      {
2026        z->msg = "invalid literal/length code";
2027        UNGRAB
2028        UPDATE
2029        return Z_DATA_ERROR;
2030      }
2031    } while (1);
2032  } while (m >= 258 && n >= 10);
2033
2034  /* not enough input or output--restore pointers and return */
2035  UNGRAB
2036  UPDATE
2037  return Z_OK;
2038}
2039
2040/*+++++*/
2041/* zutil.c -- target dependent utility functions for the compression library
2042 * Copyright (C) 1995 Jean-loup Gailly.
2043 * For conditions of distribution and use, see copyright notice in zlib.h
2044 */
2045
2046/* From: zutil.c,v 1.8 1995/05/03 17:27:12 jloup Exp */
2047
2048char *zlib_version = ZLIB_VERSION;
2049
2050char *z_errmsg[] = {
2051"stream end",          /* Z_STREAM_END    1 */
2052"",                    /* Z_OK            0 */
2053"file error",          /* Z_ERRNO        (-1) */
2054"stream error",        /* Z_STREAM_ERROR (-2) */
2055"data error",          /* Z_DATA_ERROR   (-3) */
2056"insufficient memory", /* Z_MEM_ERROR    (-4) */
2057"buffer error",        /* Z_BUF_ERROR    (-5) */
2058""};
2059
2060/*+++++*/
2061/* adler32.c -- compute the Adler-32 checksum of a data stream
2062 * Copyright (C) 1995 Mark Adler
2063 * For conditions of distribution and use, see copyright notice in zlib.h
2064 */
2065
2066/* From: adler32.c,v 1.6 1995/05/03 17:27:08 jloup Exp */
2067
2068#define BASE 65521L /* largest prime smaller than 65536 */
2069#define NMAX 5552
2070/* NMAX is the largest n such that 255n(n+1)/2 + (n+1)(BASE-1) <= 2^32-1 */
2071
2072#define DO1(buf)  {s1 += *buf++; s2 += s1;}
2073#define DO2(buf)  DO1(buf); DO1(buf);
2074#define DO4(buf)  DO2(buf); DO2(buf);
2075#define DO8(buf)  DO4(buf); DO4(buf);
2076#define DO16(buf) DO8(buf); DO8(buf);
2077
2078/* ========================================================================= */
2079uLong adler32(adler, buf, len)
2080    uLong adler;
2081    Bytef *buf;
2082    uInt len;
2083{
2084    unsigned long s1 = adler & 0xffff;
2085    unsigned long s2 = (adler >> 16) & 0xffff;
2086    int k;
2087
2088    if (buf == Z_NULL) return 1L;
2089
2090    while (len > 0) {
2091        k = len < NMAX ? len : NMAX;
2092        len -= k;
2093        while (k >= 16) {
2094            DO16(buf);
2095            k -= 16;
2096        }
2097        if (k != 0) do {
2098            DO1(buf);
2099        } while (--k);
2100        s1 %= BASE;
2101        s2 %= BASE;
2102    }
2103    return (s2 << 16) | s1;
2104}
Note: See TracBrowser for help on using the repository browser.