Back to home page

LXR

 
 

    


File indexing completed on 2025-05-11 08:23:54

0001 /*
0002  * This file is derived from various .h and .c files from the zlib-0.95
0003  * distribution by Jean-loup Gailly and Mark Adler, with some additions
0004  * by Paul Mackerras to aid in implementing Deflate compression and
0005  * decompression for PPP packets.  See zlib.h for conditions of
0006  * distribution and use.
0007  *
0008  * Changes that have been made include:
0009  * - changed functions not used outside this file to "local"
0010  * - added minCompression parameter to deflateInit2
0011  * - added Z_PACKET_FLUSH (see zlib.h for details)
0012  * - added inflateIncomp
0013  */
0014 
0015 /*+++++*/
0016 /* zutil.h -- internal interface and configuration of the compression library
0017  * Copyright (C) 1995 Jean-loup Gailly.
0018  * For conditions of distribution and use, see copyright notice in zlib.h
0019  */
0020 
0021 /* WARNING: this file should *not* be used by applications. It is
0022    part of the implementation of the compression library and is
0023    subject to change. Applications should only use zlib.h.
0024  */
0025 
0026 /* From: zutil.h,v 1.9 1995/05/03 17:27:12 jloup Exp */
0027 
0028 #define _Z_UTIL_H
0029 
0030 #include "zlib.h"
0031 
0032 #ifndef local
0033 #  define local static
0034 #endif
0035 /* compile with -Dlocal if your debugger can't find static symbols */
0036 
0037 #define FAR
0038 
0039 typedef unsigned char  uch;
0040 typedef uch FAR uchf;
0041 typedef unsigned short ush;
0042 typedef ush FAR ushf;
0043 typedef unsigned long  ulg;
0044 
0045 extern char *z_errmsg[]; /* indexed by 1-zlib_error */
0046 
0047 #define ERR_RETURN(strm,err) return (strm->msg=z_errmsg[1-err], err)
0048 /* To be used only when the state is known to be valid */
0049 
0050 #ifndef NULL
0051 #define NULL    ((void *) 0)
0052 #endif
0053 
0054         /* common constants */
0055 
0056 #define DEFLATED   8
0057 
0058 #ifndef DEF_WBITS
0059 #  define DEF_WBITS MAX_WBITS
0060 #endif
0061 /* default windowBits for decompression. MAX_WBITS is for compression only */
0062 
0063 #if MAX_MEM_LEVEL >= 8
0064 #  define DEF_MEM_LEVEL 8
0065 #else
0066 #  define DEF_MEM_LEVEL  MAX_MEM_LEVEL
0067 #endif
0068 /* default memLevel */
0069 
0070 #define STORED_BLOCK 0
0071 #define STATIC_TREES 1
0072 #define DYN_TREES    2
0073 /* The three kinds of block type */
0074 
0075 #define MIN_MATCH  3
0076 #define MAX_MATCH  258
0077 /* The minimum and maximum match lengths */
0078 
0079          /* functions */
0080 
0081 #include <string.h>
0082 #define zmemcpy memcpy
0083 #define zmemzero(dest, len) memset(dest, 0, len)
0084 
0085 /* Diagnostic functions */
0086 #ifdef DEBUG_ZLIB
0087 #  include <stdio.h>
0088 #  ifndef verbose
0089 #    define verbose 0
0090 #  endif
0091 #  define Assert(cond, msg) {if(!(cond)) Trace(msg);}
0092 #  define Trace(x) printk(x)
0093 #  define Tracev(x) {if (verbose) printk x ;}
0094 #  define Tracevv(x) {if (verbose>1) printk x ;}
0095 #  define Tracec(c,x) {if (verbose && (c)) printk x ;}
0096 #  define Tracecv(c,x) {if (verbose>1 && (c)) printk x ;}
0097 #else
0098 #  define Assert(cond,msg)
0099 #  define Trace(x)
0100 #  define Tracev(x)
0101 #  define Tracevv(x)
0102 #  define Tracec(c,x)
0103 #  define Tracecv(c,x)
0104 #endif
0105 
0106 typedef uLong (*check_func) OF((uLong check, Bytef *buf, uInt len));
0107 
0108 /* voidpf zcalloc OF((voidpf opaque, unsigned items, unsigned size)); */
0109 /* void   zcfree  OF((voidpf opaque, voidpf ptr)); */
0110 
0111 #define ZALLOC(strm, items, size) \
0112            (*((strm)->zalloc))((strm)->opaque, (items), (size))
0113 #define ZFREE(strm, addr, size) \
0114        (*((strm)->zfree))((strm)->opaque, (voidpf)(addr), (size))
0115 #define TRY_FREE(s, p, n) {if (p) ZFREE(s, p, n);}
0116 
0117 /* deflate.h -- internal compression state
0118  * Copyright (C) 1995 Jean-loup Gailly
0119  * For conditions of distribution and use, see copyright notice in zlib.h
0120  */
0121 
0122 /* WARNING: this file should *not* be used by applications. It is
0123    part of the implementation of the compression library and is
0124    subject to change. Applications should only use zlib.h.
0125  */
0126 
0127 /*+++++*/
0128 /* infblock.h -- header to use infblock.c
0129  * Copyright (C) 1995 Mark Adler
0130  * For conditions of distribution and use, see copyright notice in zlib.h
0131  */
0132 
0133 /* WARNING: this file should *not* be used by applications. It is
0134    part of the implementation of the compression library and is
0135    subject to change. Applications should only use zlib.h.
0136  */
0137 
0138 struct inflate_blocks_state;
0139 typedef struct inflate_blocks_state FAR inflate_blocks_statef;
0140 
0141 local inflate_blocks_statef * inflate_blocks_new OF((
0142     z_stream *z,
0143     check_func c,               /* check function */
0144     uInt w));                   /* window size */
0145 
0146 local int inflate_blocks OF((
0147     inflate_blocks_statef *,
0148     z_stream *,
0149     int));                      /* initial return code */
0150 
0151 local void inflate_blocks_reset OF((
0152     inflate_blocks_statef *,
0153     z_stream *,
0154     uLongf *));                  /* check value on output */
0155 
0156 local int inflate_blocks_free OF((
0157     inflate_blocks_statef *,
0158     z_stream *,
0159     uLongf *));                  /* check value on output */
0160 
0161 local int inflate_addhistory OF((
0162     inflate_blocks_statef *,
0163     z_stream *));
0164 
0165 local int inflate_packet_flush OF((
0166     inflate_blocks_statef *));
0167 
0168 /*+++++*/
0169 /* inftrees.h -- header to use inftrees.c
0170  * Copyright (C) 1995 Mark Adler
0171  * For conditions of distribution and use, see copyright notice in zlib.h
0172  */
0173 
0174 /* WARNING: this file should *not* be used by applications. It is
0175    part of the implementation of the compression library and is
0176    subject to change. Applications should only use zlib.h.
0177  */
0178 
0179 /* Huffman code lookup table entry--this entry is four bytes for machines
0180    that have 16-bit pointers (e.g. PC's in the small or medium model). */
0181 
0182 typedef struct inflate_huft_s FAR inflate_huft;
0183 
0184 struct inflate_huft_s {
0185   union {
0186     struct {
0187       Byte Exop;        /* number of extra bits or operation */
0188       Byte Bits;        /* number of bits in this code or subcode */
0189     } what;
0190     uInt Nalloc;    /* number of these allocated here */
0191     Bytef *pad;         /* pad structure to a power of 2 (4 bytes for */
0192   } word;               /*  16-bit, 8 bytes for 32-bit machines) */
0193   union {
0194     uInt Base;          /* literal, length base, or distance base */
0195     inflate_huft *Next; /* pointer to next level of table */
0196   } more;
0197 };
0198 
0199 #ifdef DEBUG_ZLIB
0200   local uInt inflate_hufts;
0201 #endif
0202 
0203 local int inflate_trees_bits OF((
0204     uIntf *,                    /* 19 code lengths */
0205     uIntf *,                    /* bits tree desired/actual depth */
0206     inflate_huft * FAR *,       /* bits tree result */
0207     z_stream *));               /* for zalloc, zfree functions */
0208 
0209 local int inflate_trees_dynamic OF((
0210     uInt,                       /* number of literal/length codes */
0211     uInt,                       /* number of distance codes */
0212     uIntf *,                    /* that many (total) code lengths */
0213     uIntf *,                    /* literal desired/actual bit depth */
0214     uIntf *,                    /* distance desired/actual bit depth */
0215     inflate_huft * FAR *,       /* literal/length tree result */
0216     inflate_huft * FAR *,       /* distance tree result */
0217     z_stream *));               /* for zalloc, zfree functions */
0218 
0219 local int inflate_trees_fixed OF((
0220     uIntf *,                    /* literal desired/actual bit depth */
0221     uIntf *,                    /* distance desired/actual bit depth */
0222     inflate_huft * FAR *,       /* literal/length tree result */
0223     inflate_huft * FAR *));     /* distance tree result */
0224 
0225 local int inflate_trees_free OF((
0226     inflate_huft *,             /* tables to free */
0227     z_stream *));               /* for zfree function */
0228 
0229 /*+++++*/
0230 /* infcodes.h -- header to use infcodes.c
0231  * Copyright (C) 1995 Mark Adler
0232  * For conditions of distribution and use, see copyright notice in zlib.h
0233  */
0234 
0235 /* WARNING: this file should *not* be used by applications. It is
0236    part of the implementation of the compression library and is
0237    subject to change. Applications should only use zlib.h.
0238  */
0239 
0240 struct inflate_codes_state;
0241 typedef struct inflate_codes_state FAR inflate_codes_statef;
0242 
0243 local inflate_codes_statef *inflate_codes_new OF((
0244     uInt, uInt,
0245     inflate_huft *, inflate_huft *,
0246     z_stream *));
0247 
0248 local int inflate_codes OF((
0249     inflate_blocks_statef *,
0250     z_stream *,
0251     int));
0252 
0253 local void inflate_codes_free OF((
0254     inflate_codes_statef *,
0255     z_stream *));
0256 
0257 /*+++++*/
0258 /* inflate.c -- zlib interface to inflate modules
0259  * Copyright (C) 1995 Mark Adler
0260  * For conditions of distribution and use, see copyright notice in zlib.h
0261  */
0262 
0263 /* inflate private state */
0264 struct internal_state {
0265 
0266   /* mode */
0267   enum {
0268       METHOD,   /* waiting for method byte */
0269       FLAG,     /* waiting for flag byte */
0270       BLOCKS,   /* decompressing blocks */
0271       CHECK4,   /* four check bytes to go */
0272       CHECK3,   /* three check bytes to go */
0273       CHECK2,   /* two check bytes to go */
0274       CHECK1,   /* one check byte to go */
0275       DONE,     /* finished check, done */
0276       BAD}      /* got an error--stay here */
0277     mode;               /* current inflate mode */
0278 
0279   /* mode dependent information */
0280   union {
0281     uInt method;        /* if FLAGS, method byte */
0282     struct {
0283       uLong was;                /* computed check value */
0284       uLong need;               /* stream check value */
0285     } check;            /* if CHECK, check values to compare */
0286     uInt marker;        /* if BAD, inflateSync's marker bytes count */
0287   } sub;        /* submode */
0288 
0289   /* mode independent information */
0290   int  nowrap;          /* flag for no wrapper */
0291   uInt wbits;           /* log2(window size)  (8..15, defaults to 15) */
0292   inflate_blocks_statef
0293     *blocks;            /* current inflate_blocks state */
0294 
0295 };
0296 
0297 int inflateReset(z)
0298 z_stream *z;
0299 {
0300   uLong c;
0301 
0302   if (z == Z_NULL || z->state == Z_NULL)
0303     return Z_STREAM_ERROR;
0304   z->total_in = z->total_out = 0;
0305   z->msg = Z_NULL;
0306   z->state->mode = z->state->nowrap ? BLOCKS : METHOD;
0307   inflate_blocks_reset(z->state->blocks, z, &c);
0308   Trace("inflate: reset\n");
0309   return Z_OK;
0310 }
0311 
0312 int inflateEnd(z)
0313 z_stream *z;
0314 {
0315   uLong c;
0316 
0317   if (z == Z_NULL || z->state == Z_NULL || z->zfree == Z_NULL)
0318     return Z_STREAM_ERROR;
0319   if (z->state->blocks != Z_NULL)
0320     inflate_blocks_free(z->state->blocks, z, &c);
0321   ZFREE(z, z->state, sizeof(struct internal_state));
0322   z->state = Z_NULL;
0323   Trace("inflate: end\n");
0324   return Z_OK;
0325 }
0326 
0327 int inflateInit2(z, w)
0328 z_stream *z;
0329 int w;
0330 {
0331   /* initialize state */
0332   if (z == Z_NULL)
0333     return Z_STREAM_ERROR;
0334 /*  if (z->zalloc == Z_NULL) z->zalloc = zcalloc; */
0335 /*  if (z->zfree == Z_NULL) z->zfree = zcfree; */
0336   if ((z->state = (struct internal_state FAR *)
0337        ZALLOC(z,1,sizeof(struct internal_state))) == Z_NULL)
0338     return Z_MEM_ERROR;
0339   z->state->blocks = Z_NULL;
0340 
0341   /* handle undocumented nowrap option (no zlib header or check) */
0342   z->state->nowrap = 0;
0343   if (w < 0)
0344   {
0345     w = - w;
0346     z->state->nowrap = 1;
0347   }
0348 
0349   /* set window size */
0350   if (w < 8 || w > 15)
0351   {
0352     inflateEnd(z);
0353     return Z_STREAM_ERROR;
0354   }
0355   z->state->wbits = (uInt)w;
0356 
0357   /* create inflate_blocks state */
0358   if ((z->state->blocks =
0359        inflate_blocks_new(z, z->state->nowrap ? Z_NULL : adler32, 1 << w))
0360       == Z_NULL)
0361   {
0362     inflateEnd(z);
0363     return Z_MEM_ERROR;
0364   }
0365   Trace("inflate: allocated\n");
0366 
0367   /* reset state */
0368   inflateReset(z);
0369   return Z_OK;
0370 }
0371 
0372 int inflateInit(z)
0373 z_stream *z;
0374 {
0375   return inflateInit2(z, DEF_WBITS);
0376 }
0377 
0378 #define NEEDBYTE {if(z->avail_in==0)goto empty;r=Z_OK;}
0379 #define NEXTBYTE (z->avail_in--,z->total_in++,*z->next_in++)
0380 
0381 int inflate(z, f)
0382 z_stream *z;
0383 int f;
0384 {
0385   int r;
0386   uInt b;
0387 
0388   if (z == Z_NULL || z->next_in == Z_NULL)
0389     return Z_STREAM_ERROR;
0390   r = Z_BUF_ERROR;
0391   while (1) switch (z->state->mode)
0392   {
0393     case METHOD:
0394       NEEDBYTE
0395       if (((z->state->sub.method = NEXTBYTE) & 0xf) != DEFLATED)
0396       {
0397         z->state->mode = BAD;
0398         z->msg = "unknown compression method";
0399         z->state->sub.marker = 5;       /* can't try inflateSync */
0400         break;
0401       }
0402       if ((z->state->sub.method >> 4) + 8 > z->state->wbits)
0403       {
0404         z->state->mode = BAD;
0405         z->msg = "invalid window size";
0406         z->state->sub.marker = 5;       /* can't try inflateSync */
0407         break;
0408       }
0409       z->state->mode = FLAG;
0410     case FLAG:
0411       NEEDBYTE
0412       if ((b = NEXTBYTE) & 0x20)
0413       {
0414         z->state->mode = BAD;
0415         z->msg = "invalid reserved bit";
0416         z->state->sub.marker = 5;       /* can't try inflateSync */
0417         break;
0418       }
0419       if (((z->state->sub.method << 8) + b) % 31)
0420       {
0421         z->state->mode = BAD;
0422         z->msg = "incorrect header check";
0423         z->state->sub.marker = 5;       /* can't try inflateSync */
0424         break;
0425       }
0426       Trace("inflate: zlib header ok\n");
0427       z->state->mode = BLOCKS;
0428     case BLOCKS:
0429       r = inflate_blocks(z->state->blocks, z, r);
0430       if (f == Z_PACKET_FLUSH && z->avail_in == 0 && z->avail_out != 0)
0431       r = inflate_packet_flush(z->state->blocks);
0432       if (r == Z_DATA_ERROR)
0433       {
0434         z->state->mode = BAD;
0435         z->state->sub.marker = 0;       /* can try inflateSync */
0436         break;
0437       }
0438       if (r != Z_STREAM_END)
0439         return r;
0440       r = Z_OK;
0441       inflate_blocks_reset(z->state->blocks, z, &z->state->sub.check.was);
0442       if (z->state->nowrap)
0443       {
0444         z->state->mode = DONE;
0445         break;
0446       }
0447       z->state->mode = CHECK4;
0448     case CHECK4:
0449       NEEDBYTE
0450       z->state->sub.check.need = (uLong)NEXTBYTE << 24;
0451       z->state->mode = CHECK3;
0452     case CHECK3:
0453       NEEDBYTE
0454       z->state->sub.check.need += (uLong)NEXTBYTE << 16;
0455       z->state->mode = CHECK2;
0456     case CHECK2:
0457       NEEDBYTE
0458       z->state->sub.check.need += (uLong)NEXTBYTE << 8;
0459       z->state->mode = CHECK1;
0460     case CHECK1:
0461       NEEDBYTE
0462       z->state->sub.check.need += (uLong)NEXTBYTE;
0463 
0464       if (z->state->sub.check.was != z->state->sub.check.need)
0465       {
0466         z->state->mode = BAD;
0467         z->msg = "incorrect data check";
0468         z->state->sub.marker = 5;       /* can't try inflateSync */
0469         break;
0470       }
0471       Trace( "inflate: zlib check ok\n");
0472       z->state->mode = DONE;
0473     case DONE:
0474       return Z_STREAM_END;
0475     case BAD:
0476       return Z_DATA_ERROR;
0477     default:
0478       return Z_STREAM_ERROR;
0479   }
0480 
0481  empty:
0482   if (f != Z_PACKET_FLUSH)
0483     return r;
0484   z->state->mode = BAD;
0485   z->state->sub.marker = 0;       /* can try inflateSync */
0486   return Z_DATA_ERROR;
0487 }
0488 
0489 /*
0490  * This subroutine adds the data at next_in/avail_in to the output history
0491  * without performing any output.  The output buffer must be "caught up";
0492  * i.e. no pending output (hence s->read equals s->write), and the state must
0493  * be BLOCKS (i.e. we should be willing to see the start of a series of
0494  * BLOCKS).  On exit, the output will also be caught up, and the checksum
0495  * will have been updated if need be.
0496  */
0497 
0498 int inflateIncomp(z)
0499 z_stream *z;
0500 {
0501     if (z->state->mode != BLOCKS)
0502     return Z_DATA_ERROR;
0503     return inflate_addhistory(z->state->blocks, z);
0504 }
0505 
0506 int inflateSync(z)
0507 z_stream *z;
0508 {
0509   uInt n;       /* number of bytes to look at */
0510   Bytef *p;     /* pointer to bytes */
0511   uInt m;       /* number of marker bytes found in a row */
0512   uLong r, w;   /* temporaries to save total_in and total_out */
0513 
0514   /* set up */
0515   if (z == Z_NULL || z->state == Z_NULL)
0516     return Z_STREAM_ERROR;
0517   if (z->state->mode != BAD)
0518   {
0519     z->state->mode = BAD;
0520     z->state->sub.marker = 0;
0521   }
0522   if ((n = z->avail_in) == 0)
0523     return Z_BUF_ERROR;
0524   p = z->next_in;
0525   m = z->state->sub.marker;
0526 
0527   /* search */
0528   while (n && m < 4)
0529   {
0530     if (*p == (Byte)(m < 2 ? 0 : 0xff))
0531       m++;
0532     else if (*p)
0533       m = 0;
0534     else
0535       m = 4 - m;
0536     p++, n--;
0537   }
0538 
0539   /* restore */
0540   z->total_in += p - z->next_in;
0541   z->next_in = p;
0542   z->avail_in = n;
0543   z->state->sub.marker = m;
0544 
0545   /* return no joy or set up to restart on a new block */
0546   if (m != 4)
0547     return Z_DATA_ERROR;
0548   r = z->total_in;  w = z->total_out;
0549   inflateReset(z);
0550   z->total_in = r;  z->total_out = w;
0551   z->state->mode = BLOCKS;
0552   return Z_OK;
0553 }
0554 
0555 #undef NEEDBYTE
0556 #undef NEXTBYTE
0557 
0558 /*+++++*/
0559 /* infutil.h -- types and macros common to blocks and codes
0560  * Copyright (C) 1995 Mark Adler
0561  * For conditions of distribution and use, see copyright notice in zlib.h
0562  */
0563 
0564 /* WARNING: this file should *not* be used by applications. It is
0565    part of the implementation of the compression library and is
0566    subject to change. Applications should only use zlib.h.
0567  */
0568 
0569 /* inflate blocks semi-private state */
0570 struct inflate_blocks_state {
0571 
0572   /* mode */
0573   enum {
0574       TYPE,     /* get type bits (3, including end bit) */
0575       LENS,     /* get lengths for stored */
0576       STORED,   /* processing stored block */
0577       TABLE,    /* get table lengths */
0578       BTREE,    /* get bit lengths tree for a dynamic block */
0579       DTREE,    /* get length, distance trees for a dynamic block */
0580       CODES,    /* processing fixed or dynamic block */
0581       DRY,      /* output remaining window bytes */
0582       DONEB,     /* finished last block, done */
0583       BADB}      /* got a data error--stuck here */
0584     mode;               /* current inflate_block mode */
0585 
0586   /* mode dependent information */
0587   union {
0588     uInt left;          /* if STORED, bytes left to copy */
0589     struct {
0590       uInt table;               /* table lengths (14 bits) */
0591       uInt index;               /* index into blens (or border) */
0592       uIntf *blens;             /* bit lengths of codes */
0593       uInt bb;                  /* bit length tree depth */
0594       inflate_huft *tb;         /* bit length decoding tree */
0595       int nblens;       /* # elements allocated at blens */
0596     } trees;            /* if DTREE, decoding info for trees */
0597     struct {
0598       inflate_huft *tl, *td;    /* trees to free */
0599       inflate_codes_statef
0600          *codes;
0601     } decode;           /* if CODES, current state */
0602   } sub;                /* submode */
0603   uInt last;            /* true if this block is the last block */
0604 
0605   /* mode independent information */
0606   uInt bitk;            /* bits in bit buffer */
0607   uLong bitb;           /* bit buffer */
0608   Bytef *window;        /* sliding window */
0609   Bytef *end;           /* one byte after sliding window */
0610   Bytef *read;          /* window read pointer */
0611   Bytef *write;         /* window write pointer */
0612   check_func checkfn;   /* check function */
0613   uLong check;          /* check on output */
0614 
0615 };
0616 
0617 /* defines for inflate input/output */
0618 /*   update pointers and return */
0619 #define UPDBITS {s->bitb=b;s->bitk=k;}
0620 #define UPDIN {z->avail_in=n;z->total_in+=p-z->next_in;z->next_in=p;}
0621 #define UPDOUT {s->write=q;}
0622 #define UPDATE {UPDBITS UPDIN UPDOUT}
0623 #define LEAVE {UPDATE return inflate_flush(s,z,r);}
0624 /*   get bytes and bits */
0625 #define LOADIN {p=z->next_in;n=z->avail_in;b=s->bitb;k=s->bitk;}
0626 #define NEEDBYTE {if(n)r=Z_OK;else LEAVE}
0627 #define NEXTBYTE (n--,*p++)
0628 #define NEEDBITS(j) {while(k<(j)){NEEDBYTE;b|=((uLong)NEXTBYTE)<<k;k+=8;}}
0629 #define DUMPBITS(j) {b>>=(j);k-=(j);}
0630 /*   output bytes */
0631 #define WAVAIL (q<s->read?s->read-q-1:s->end-q)
0632 #define LOADOUT {q=s->write;m=WAVAIL;}
0633 #define WRAP {if(q==s->end&&s->read!=s->window){q=s->window;m=WAVAIL;}}
0634 #define FLUSH {UPDOUT r=inflate_flush(s,z,r); LOADOUT}
0635 #define NEEDOUT {if(m==0){WRAP if(m==0){FLUSH WRAP if(m==0) LEAVE}}r=Z_OK;}
0636 #define OUTBYTE(a) {*q++=(Byte)(a);m--;}
0637 /*   load local pointers */
0638 #define LOAD {LOADIN LOADOUT}
0639 
0640 /* And'ing with mask[n] masks the lower n bits */
0641 local uInt inflate_mask[] = {
0642     0x0000,
0643     0x0001, 0x0003, 0x0007, 0x000f, 0x001f, 0x003f, 0x007f, 0x00ff,
0644     0x01ff, 0x03ff, 0x07ff, 0x0fff, 0x1fff, 0x3fff, 0x7fff, 0xffff
0645 };
0646 
0647 /* copy as much as possible from the sliding window to the output area */
0648 local int inflate_flush OF((
0649     inflate_blocks_statef *,
0650     z_stream *,
0651     int));
0652 
0653 /*+++++*/
0654 /* inffast.h -- header to use inffast.c
0655  * Copyright (C) 1995 Mark Adler
0656  * For conditions of distribution and use, see copyright notice in zlib.h
0657  */
0658 
0659 /* WARNING: this file should *not* be used by applications. It is
0660    part of the implementation of the compression library and is
0661    subject to change. Applications should only use zlib.h.
0662  */
0663 
0664 local int inflate_fast OF((
0665     uInt,
0666     uInt,
0667     inflate_huft *,
0668     inflate_huft *,
0669     inflate_blocks_statef *,
0670     z_stream *));
0671 
0672 /*+++++*/
0673 /* infblock.c -- interpret and process block types to last block
0674  * Copyright (C) 1995 Mark Adler
0675  * For conditions of distribution and use, see copyright notice in zlib.h
0676  */
0677 
0678 /* Table for deflate from PKZIP's appnote.txt. */
0679 local uInt border[] = { /* Order of the bit length code lengths */
0680         16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15};
0681 
0682 /*
0683    Notes beyond the 1.93a appnote.txt:
0684 
0685    1. Distance pointers never point before the beginning of the output
0686       stream.
0687    2. Distance pointers can point back across blocks, up to 32k away.
0688    3. There is an implied maximum of 7 bits for the bit length table and
0689       15 bits for the actual data.
0690    4. If only one code exists, then it is encoded using one bit.  (Zero
0691       would be more efficient, but perhaps a little confusing.)  If two
0692       codes exist, they are coded using one bit each (0 and 1).
0693    5. There is no way of sending zero distance codes--a dummy must be
0694       sent if there are none.  (History: a pre 2.0 version of PKZIP would
0695       store blocks with no distance codes, but this was discovered to be
0696       too harsh a criterion.)  Valid only for 1.93a.  2.04c does allow
0697       zero distance codes, which is sent as one code of zero bits in
0698       length.
0699    6. There are up to 286 literal/length codes.  Code 256 represents the
0700       end-of-block.  Note however that the static length tree defines
0701       288 codes just to fill out the Huffman codes.  Codes 286 and 287
0702       cannot be used though, since there is no length base or extra bits
0703       defined for them.  Similarily, there are up to 30 distance codes.
0704       However, static trees define 32 codes (all 5 bits) to fill out the
0705       Huffman codes, but the last two had better not show up in the data.
0706    7. Unzip can check dynamic Huffman blocks for complete code sets.
0707       The exception is that a single code would not be complete (see #4).
0708    8. The five bits following the block type is really the number of
0709       literal codes sent minus 257.
0710    9. Length codes 8,16,16 are interpreted as 13 length codes of 8 bits
0711       (1+6+6).  Therefore, to output three times the length, you output
0712       three codes (1+1+1), whereas to output four times the same length,
0713       you only need two codes (1+3).  Hmm.
0714   10. In the tree reconstruction algorithm, Code = Code + Increment
0715       only if BitLength(i) is not zero.  (Pretty obvious.)
0716   11. Correction: 4 Bits: # of Bit Length codes - 4     (4 - 19)
0717   12. Note: length code 284 can represent 227-258, but length code 285
0718       really is 258.  The last length deserves its own, short code
0719       since it gets used a lot in very redundant files.  The length
0720       258 is special since 258 - 3 (the min match length) is 255.
0721   13. The literal/length and distance code bit lengths are read as a
0722       single stream of lengths.  It is possible (and advantageous) for
0723       a repeat code (16, 17, or 18) to go across the boundary between
0724       the two sets of lengths.
0725  */
0726 
0727 local void inflate_blocks_reset(s, z, c)
0728 inflate_blocks_statef *s;
0729 z_stream *z;
0730 uLongf *c;
0731 {
0732   if (s->checkfn != Z_NULL)
0733     *c = s->check;
0734   if (s->mode == BTREE || s->mode == DTREE)
0735     ZFREE(z, s->sub.trees.blens, s->sub.trees.nblens * sizeof(uInt));
0736   if (s->mode == CODES)
0737   {
0738     inflate_codes_free(s->sub.decode.codes, z);
0739     inflate_trees_free(s->sub.decode.td, z);
0740     inflate_trees_free(s->sub.decode.tl, z);
0741   }
0742   s->mode = TYPE;
0743   s->bitk = 0;
0744   s->bitb = 0;
0745   s->read = s->write = s->window;
0746   if (s->checkfn != Z_NULL)
0747     s->check = (*s->checkfn)(0L, Z_NULL, 0);
0748   Trace("inflate:   blocks reset\n");
0749 }
0750 
0751 local inflate_blocks_statef *inflate_blocks_new(z, c, w)
0752 z_stream *z;
0753 check_func c;
0754 uInt w;
0755 {
0756   inflate_blocks_statef *s;
0757 
0758   if ((s = (inflate_blocks_statef *)ZALLOC
0759        (z,1,sizeof(struct inflate_blocks_state))) == Z_NULL)
0760     return s;
0761   if ((s->window = (Bytef *)ZALLOC(z, 1, w)) == Z_NULL)
0762   {
0763     ZFREE(z, s, sizeof(struct inflate_blocks_state));
0764     return Z_NULL;
0765   }
0766   s->end = s->window + w;
0767   s->checkfn = c;
0768   s->mode = TYPE;
0769   Trace("inflate:   blocks allocated\n");
0770   inflate_blocks_reset(s, z, &s->check);
0771   return s;
0772 }
0773 
0774 local int inflate_blocks(s, z, r)
0775 inflate_blocks_statef *s;
0776 z_stream *z;
0777 int r;
0778 {
0779   uInt t;               /* temporary storage */
0780   uLong b;              /* bit buffer */
0781   uInt k;               /* bits in bit buffer */
0782   Bytef *p;             /* input data pointer */
0783   uInt n;               /* bytes available there */
0784   Bytef *q;             /* output window write pointer */
0785   uInt m;               /* bytes to end of window or read pointer */
0786 
0787   /* copy input/output information to locals (UPDATE macro restores) */
0788   LOAD
0789 
0790   /* process input based on current state */
0791   while (1) switch (s->mode)
0792   {
0793     case TYPE:
0794       NEEDBITS(3)
0795       t = (uInt)b & 7;
0796       s->last = t & 1;
0797       switch (t >> 1)
0798       {
0799         case 0:                         /* stored */
0800           Trace(("inflate:     stored block%s\n",
0801                  s->last ? " (last)" : ""));
0802           DUMPBITS(3)
0803           t = k & 7;                    /* go to byte boundary */
0804           DUMPBITS(t)
0805           s->mode = LENS;               /* get length of stored block */
0806           break;
0807         case 1:                         /* fixed */
0808           Trace(( "inflate:     fixed codes block%s\n",
0809                  s->last ? " (last)" : ""));
0810           {
0811             uInt bl, bd;
0812             inflate_huft *tl, *td;
0813 
0814             inflate_trees_fixed(&bl, &bd, &tl, &td);
0815             s->sub.decode.codes = inflate_codes_new(bl, bd, tl, td, z);
0816             if (s->sub.decode.codes == Z_NULL)
0817             {
0818               r = Z_MEM_ERROR;
0819               LEAVE
0820             }
0821             s->sub.decode.tl = Z_NULL;  /* don't try to free these */
0822             s->sub.decode.td = Z_NULL;
0823           }
0824           DUMPBITS(3)
0825           s->mode = CODES;
0826           break;
0827         case 2:                         /* dynamic */
0828           Trace(( "inflate:     dynamic codes block%s\n",
0829                  s->last ? " (last)" : ""));
0830           DUMPBITS(3)
0831           s->mode = TABLE;
0832           break;
0833         case 3:                         /* illegal */
0834           DUMPBITS(3)
0835           s->mode = BADB;
0836           z->msg = "invalid block type";
0837           r = Z_DATA_ERROR;
0838           LEAVE
0839       }
0840       break;
0841     case LENS:
0842       NEEDBITS(32)
0843       if (((~b) >> 16) != (b & 0xffff))
0844       {
0845         s->mode = BADB;
0846         z->msg = "invalid stored block lengths";
0847         r = Z_DATA_ERROR;
0848         LEAVE
0849       }
0850       s->sub.left = (uInt)b & 0xffff;
0851       b = k = 0;                      /* dump bits */
0852       Tracev(( "inflate:       stored length %u\n", s->sub.left));
0853       s->mode = s->sub.left ? STORED : TYPE;
0854       break;
0855     case STORED:
0856       if (n == 0)
0857         LEAVE
0858       NEEDOUT
0859       t = s->sub.left;
0860       if (t > n) t = n;
0861       if (t > m) t = m;
0862       zmemcpy(q, p, t);
0863       p += t;  n -= t;
0864       q += t;  m -= t;
0865       if ((s->sub.left -= t) != 0)
0866         break;
0867       Tracev(( "inflate:       stored end, %lu total out\n",
0868               z->total_out + (q >= s->read ? q - s->read :
0869               (s->end - s->read) + (q - s->window))));
0870       s->mode = s->last ? DRY : TYPE;
0871       break;
0872     case TABLE:
0873       NEEDBITS(14)
0874       s->sub.trees.table = t = (uInt)b & 0x3fff;
0875 #ifndef PKZIP_BUG_WORKAROUND
0876       if ((t & 0x1f) > 29 || ((t >> 5) & 0x1f) > 29)
0877       {
0878         s->mode = BADB;
0879         z->msg = "too many length or distance symbols";
0880         r = Z_DATA_ERROR;
0881         LEAVE
0882       }
0883 #endif
0884       t = 258 + (t & 0x1f) + ((t >> 5) & 0x1f);
0885       if (t < 19)
0886         t = 19;
0887       if ((s->sub.trees.blens = (uIntf*)ZALLOC(z, t, sizeof(uInt))) == Z_NULL)
0888       {
0889         r = Z_MEM_ERROR;
0890         LEAVE
0891       }
0892       s->sub.trees.nblens = t;
0893       DUMPBITS(14)
0894       s->sub.trees.index = 0;
0895       Tracev(( "inflate:       table sizes ok\n"));
0896       s->mode = BTREE;
0897     case BTREE:
0898       while (s->sub.trees.index < 4 + (s->sub.trees.table >> 10))
0899       {
0900         NEEDBITS(3)
0901         s->sub.trees.blens[border[s->sub.trees.index++]] = (uInt)b & 7;
0902         DUMPBITS(3)
0903       }
0904       while (s->sub.trees.index < 19)
0905         s->sub.trees.blens[border[s->sub.trees.index++]] = 0;
0906       s->sub.trees.bb = 7;
0907       t = inflate_trees_bits(s->sub.trees.blens, &s->sub.trees.bb,
0908                              &s->sub.trees.tb, z);
0909       if (t != Z_OK)
0910       {
0911         r = t;
0912         if (r == Z_DATA_ERROR)
0913           s->mode = BADB;
0914         LEAVE
0915       }
0916       s->sub.trees.index = 0;
0917       Tracev(( "inflate:       bits tree ok\n"));
0918       s->mode = DTREE;
0919     case DTREE:
0920       while (t = s->sub.trees.table,
0921              s->sub.trees.index < 258 + (t & 0x1f) + ((t >> 5) & 0x1f))
0922       {
0923         inflate_huft *h;
0924         uInt i, j, c;
0925 
0926         t = s->sub.trees.bb;
0927         NEEDBITS(t)
0928         h = s->sub.trees.tb + ((uInt)b & inflate_mask[t]);
0929         t = h->word.what.Bits;
0930         c = h->more.Base;
0931         if (c < 16)
0932         {
0933           DUMPBITS(t)
0934           s->sub.trees.blens[s->sub.trees.index++] = c;
0935         }
0936         else /* c == 16..18 */
0937         {
0938           i = c == 18 ? 7 : c - 14;
0939           j = c == 18 ? 11 : 3;
0940           NEEDBITS(t + i)
0941           DUMPBITS(t)
0942           j += (uInt)b & inflate_mask[i];
0943           DUMPBITS(i)
0944           i = s->sub.trees.index;
0945           t = s->sub.trees.table;
0946           if (i + j > 258 + (t & 0x1f) + ((t >> 5) & 0x1f) ||
0947               (c == 16 && i < 1))
0948           {
0949             s->mode = BADB;
0950             z->msg = "invalid bit length repeat";
0951             r = Z_DATA_ERROR;
0952             LEAVE
0953           }
0954           c = c == 16 ? s->sub.trees.blens[i - 1] : 0;
0955           do {
0956             s->sub.trees.blens[i++] = c;
0957           } while (--j);
0958           s->sub.trees.index = i;
0959         }
0960       }
0961       inflate_trees_free(s->sub.trees.tb, z);
0962       s->sub.trees.tb = Z_NULL;
0963       {
0964         uInt bl, bd;
0965         inflate_huft *tl, *td;
0966         inflate_codes_statef *c;
0967 
0968         bl = 9;         /* must be <= 9 for lookahead assumptions */
0969         bd = 6;         /* must be <= 9 for lookahead assumptions */
0970         t = s->sub.trees.table;
0971         t = inflate_trees_dynamic(257 + (t & 0x1f), 1 + ((t >> 5) & 0x1f),
0972                                   s->sub.trees.blens, &bl, &bd, &tl, &td, z);
0973         if (t != Z_OK)
0974         {
0975           if (t == (uInt)Z_DATA_ERROR)
0976             s->mode = BADB;
0977           r = t;
0978           LEAVE
0979         }
0980         Tracev(( "inflate:       trees ok\n"));
0981         if ((c = inflate_codes_new(bl, bd, tl, td, z)) == Z_NULL)
0982         {
0983           inflate_trees_free(td, z);
0984           inflate_trees_free(tl, z);
0985           r = Z_MEM_ERROR;
0986           LEAVE
0987         }
0988         ZFREE(z, s->sub.trees.blens, s->sub.trees.nblens * sizeof(uInt));
0989         s->sub.decode.codes = c;
0990         s->sub.decode.tl = tl;
0991         s->sub.decode.td = td;
0992       }
0993       s->mode = CODES;
0994     case CODES:
0995       UPDATE
0996       if ((r = inflate_codes(s, z, r)) != Z_STREAM_END)
0997         return inflate_flush(s, z, r);
0998       r = Z_OK;
0999       inflate_codes_free(s->sub.decode.codes, z);
1000       inflate_trees_free(s->sub.decode.td, z);
1001       inflate_trees_free(s->sub.decode.tl, z);
1002       LOAD
1003       Tracev(( "inflate:       codes end, %lu total out\n",
1004               z->total_out + (q >= s->read ? q - s->read :
1005               (s->end - s->read) + (q - s->window))));
1006       if (!s->last)
1007       {
1008         s->mode = TYPE;
1009         break;
1010       }
1011       if (k > 7)              /* return unused byte, if any */
1012       {
1013         Assert(k < 16, "inflate_codes grabbed too many bytes")
1014         k -= 8;
1015         n++;
1016         p--;                    /* can always return one */
1017       }
1018       s->mode = DRY;
1019     case DRY:
1020       FLUSH
1021       if (s->read != s->write)
1022         LEAVE
1023       s->mode = DONEB;
1024     case DONEB:
1025       r = Z_STREAM_END;
1026       LEAVE
1027     case BADB:
1028       r = Z_DATA_ERROR;
1029       LEAVE
1030     default:
1031       r = Z_STREAM_ERROR;
1032       LEAVE
1033   }
1034 }
1035 
1036 local int inflate_blocks_free(s, z, c)
1037 inflate_blocks_statef *s;
1038 z_stream *z;
1039 uLongf *c;
1040 {
1041   inflate_blocks_reset(s, z, c);
1042   ZFREE(z, s->window, s->end - s->window);
1043   ZFREE(z, s, sizeof(struct inflate_blocks_state));
1044   Trace(( "inflate:   blocks freed\n"));
1045   return Z_OK;
1046 }
1047 
1048 /*
1049  * This subroutine adds the data at next_in/avail_in to the output history
1050  * without performing any output.  The output buffer must be "caught up";
1051  * i.e. no pending output (hence s->read equals s->write), and the state must
1052  * be BLOCKS (i.e. we should be willing to see the start of a series of
1053  * BLOCKS).  On exit, the output will also be caught up, and the checksum
1054  * will have been updated if need be.
1055  */
1056 local int inflate_addhistory(s, z)
1057 inflate_blocks_statef *s;
1058 z_stream *z;
1059 {
1060     uLong b;              /* bit buffer */  /* NOT USED HERE */
1061     uInt k;               /* bits in bit buffer */ /* NOT USED HERE */
1062     uInt t;               /* temporary storage */
1063     Bytef *p;             /* input data pointer */
1064     uInt n;               /* bytes available there */
1065     Bytef *q;             /* output window write pointer */
1066     uInt m;               /* bytes to end of window or read pointer */
1067 
1068     if (s->read != s->write)
1069     return Z_STREAM_ERROR;
1070     if (s->mode != TYPE)
1071     return Z_DATA_ERROR;
1072 
1073     /* we're ready to rock */
1074     LOAD
1075     /* while there is input ready, copy to output buffer, moving
1076      * pointers as needed.
1077      */
1078     while (n) {
1079     t = n;  /* how many to do */
1080     /* is there room until end of buffer? */
1081     if (t > m) t = m;
1082     /* update check information */
1083     if (s->checkfn != Z_NULL)
1084         s->check = (*s->checkfn)(s->check, q, t);
1085     zmemcpy(q, p, t);
1086     q += t;
1087     p += t;
1088     n -= t;
1089     z->total_out += t;
1090     s->read = q;    /* drag read pointer forward */
1091 /*      WRAP  */    /* expand WRAP macro by hand to handle s->read */
1092     if (q == s->end) {
1093         s->read = q = s->window;
1094         m = WAVAIL;
1095     }
1096     }
1097     UPDATE
1098     return Z_OK;
1099 }
1100 
1101 /*
1102  * At the end of a Deflate-compressed PPP packet, we expect to have seen
1103  * a `stored' block type value but not the (zero) length bytes.
1104  */
1105 local int inflate_packet_flush(s)
1106     inflate_blocks_statef *s;
1107 {
1108     if (s->mode != LENS)
1109     return Z_DATA_ERROR;
1110     s->mode = TYPE;
1111     return Z_OK;
1112 }
1113 
1114 /*+++++*/
1115 /* inftrees.c -- generate Huffman trees for efficient decoding
1116  * Copyright (C) 1995 Mark Adler
1117  * For conditions of distribution and use, see copyright notice in zlib.h
1118  */
1119 
1120 /* simplify the use of the inflate_huft type with some defines */
1121 #define base more.Base
1122 #define next more.Next
1123 #define exop word.what.Exop
1124 #define bits word.what.Bits
1125 
1126 local int huft_build OF((
1127     uIntf *,            /* code lengths in bits */
1128     uInt,               /* number of codes */
1129     uInt,               /* number of "simple" codes */
1130     uIntf *,            /* list of base values for non-simple codes */
1131     uIntf *,            /* list of extra bits for non-simple codes */
1132     inflate_huft * FAR*,/* result: starting table */
1133     uIntf *,            /* maximum lookup bits (returns actual) */
1134     z_stream *));       /* for zalloc function */
1135 
1136 local voidpf falloc OF((
1137     voidpf,             /* opaque pointer (not used) */
1138     uInt,               /* number of items */
1139     uInt));             /* size of item */
1140 
1141 local void ffree OF((
1142     voidpf q,           /* opaque pointer (not used) */
1143     voidpf p,           /* what to free (not used) */
1144     uInt n));       /* number of bytes (not used) */
1145 
1146 /* Tables for deflate from PKZIP's appnote.txt. */
1147 local uInt cplens[] = { /* Copy lengths for literal codes 257..285 */
1148         3, 4, 5, 6, 7, 8, 9, 10, 11, 13, 15, 17, 19, 23, 27, 31,
1149         35, 43, 51, 59, 67, 83, 99, 115, 131, 163, 195, 227, 258, 0, 0};
1150         /* actually lengths - 2; also see note #13 above about 258 */
1151 local uInt cplext[] = { /* Extra bits for literal codes 257..285 */
1152         0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2,
1153         3, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5, 0, 192, 192}; /* 192==invalid */
1154 local uInt cpdist[] = { /* Copy offsets for distance codes 0..29 */
1155         1, 2, 3, 4, 5, 7, 9, 13, 17, 25, 33, 49, 65, 97, 129, 193,
1156         257, 385, 513, 769, 1025, 1537, 2049, 3073, 4097, 6145,
1157         8193, 12289, 16385, 24577};
1158 local uInt cpdext[] = { /* Extra bits for distance codes */
1159         0, 0, 0, 0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6,
1160         7, 7, 8, 8, 9, 9, 10, 10, 11, 11,
1161         12, 12, 13, 13};
1162 
1163 /*
1164    Huffman code decoding is performed using a multi-level table lookup.
1165    The fastest way to decode is to simply build a lookup table whose
1166    size is determined by the longest code.  However, the time it takes
1167    to build this table can also be a factor if the data being decoded
1168    is not very long.  The most common codes are necessarily the
1169    shortest codes, so those codes dominate the decoding time, and hence
1170    the speed.  The idea is you can have a shorter table that decodes the
1171    shorter, more probable codes, and then point to subsidiary tables for
1172    the longer codes.  The time it costs to decode the longer codes is
1173    then traded against the time it takes to make longer tables.
1174 
1175    This results of this trade are in the variables lbits and dbits
1176    below.  lbits is the number of bits the first level table for literal/
1177    length codes can decode in one step, and dbits is the same thing for
1178    the distance codes.  Subsequent tables are also less than or equal to
1179    those sizes.  These values may be adjusted either when all of the
1180    codes are shorter than that, in which case the longest code length in
1181    bits is used, or when the shortest code is *longer* than the requested
1182    table size, in which case the length of the shortest code in bits is
1183    used.
1184 
1185    There are two different values for the two tables, since they code a
1186    different number of possibilities each.  The literal/length table
1187    codes 286 possible values, or in a flat code, a little over eight
1188    bits.  The distance table codes 30 possible values, or a little less
1189    than five bits, flat.  The optimum values for speed end up being
1190    about one bit more than those, so lbits is 8+1 and dbits is 5+1.
1191    The optimum values may differ though from machine to machine, and
1192    possibly even between compilers.  Your mileage may vary.
1193  */
1194 
1195 /* If BMAX needs to be larger than 16, then h and x[] should be uLong. */
1196 #define BMAX 15         /* maximum bit length of any code */
1197 #define N_MAX 288       /* maximum number of codes in any set */
1198 
1199 #ifdef DEBUG_ZLIB
1200   uInt inflate_hufts;
1201 #endif
1202 
1203 local int huft_build(b, n, s, d, e, t, m, zs)
1204 uIntf *b;               /* code lengths in bits (all assumed <= BMAX) */
1205 uInt n;                 /* number of codes (assumed <= N_MAX) */
1206 uInt s;                 /* number of simple-valued codes (0..s-1) */
1207 uIntf *d;               /* list of base values for non-simple codes */
1208 uIntf *e;               /* list of extra bits for non-simple codes */
1209 inflate_huft * FAR *t;  /* result: starting table */
1210 uIntf *m;               /* maximum lookup bits, returns actual */
1211 z_stream *zs;           /* for zalloc function */
1212 /* Given a list of code lengths and a maximum table size, make a set of
1213    tables to decode that set of codes.  Return Z_OK on success, Z_BUF_ERROR
1214    if the given code set is incomplete (the tables are still built in this
1215    case), Z_DATA_ERROR if the input is invalid (all zero length codes or an
1216    over-subscribed set of lengths), or Z_MEM_ERROR if not enough memory. */
1217 {
1218 
1219   uInt a;                       /* counter for codes of length k */
1220   uInt c[BMAX+1];               /* bit length count table */
1221   uInt f;                       /* i repeats in table every f entries */
1222   int g;                        /* maximum code length */
1223   int h;                        /* table level */
1224   register uInt i;              /* counter, current code */
1225   register uInt j;              /* counter */
1226   register int k;               /* number of bits in current code */
1227   int l;                        /* bits per table (returned in m) */
1228   register uIntf *p;            /* pointer into c[], b[], or v[] */
1229   inflate_huft *q;              /* points to current table */
1230   struct inflate_huft_s r;      /* table entry for structure assignment */
1231   inflate_huft *u[BMAX];        /* table stack */
1232   uInt v[N_MAX];                /* values in order of bit length */
1233   register int w;               /* bits before this table == (l * h) */
1234   uInt x[BMAX+1];               /* bit offsets, then code stack */
1235   uIntf *xp;                    /* pointer into x */
1236   int y;                        /* number of dummy codes added */
1237   uInt z;                       /* number of entries in current table */
1238 
1239   /* Generate counts for each bit length */
1240   p = c;
1241 #define C0 *p++ = 0;
1242 #define C2 C0 C0 C0 C0
1243 #define C4 C2 C2 C2 C2
1244   C4                            /* clear c[]--assume BMAX+1 is 16 */
1245   p = b;  i = n;
1246   do {
1247     c[*p++]++;                  /* assume all entries <= BMAX */
1248   } while (--i);
1249   if (c[0] == n)                /* null input--all zero length codes */
1250   {
1251     *t = (inflate_huft *)Z_NULL;
1252     *m = 0;
1253     return Z_OK;
1254   }
1255 
1256   /* Find minimum and maximum length, bound *m by those */
1257   l = *m;
1258   for (j = 1; j <= BMAX; j++)
1259     if (c[j])
1260       break;
1261   k = j;                        /* minimum code length */
1262   if ((uInt)l < j)
1263     l = j;
1264   for (i = BMAX; i; i--)
1265     if (c[i])
1266       break;
1267   g = i;                        /* maximum code length */
1268   if ((uInt)l > i)
1269     l = i;
1270   *m = l;
1271 
1272   /* Adjust last length count to fill out codes, if needed */
1273   for (y = 1 << j; j < i; j++, y <<= 1)
1274     if ((y -= c[j]) < 0)
1275       return Z_DATA_ERROR;
1276   if ((y -= c[i]) < 0)
1277     return Z_DATA_ERROR;
1278   c[i] += y;
1279 
1280   /* Generate starting offsets into the value table for each length */
1281   x[1] = j = 0;
1282   p = c + 1;  xp = x + 2;
1283   while (--i) {                 /* note that i == g from above */
1284     *xp++ = (j += *p++);
1285   }
1286 
1287   /* Make a table of values in order of bit lengths */
1288   p = b;  i = 0;
1289   do {
1290     if ((j = *p++) != 0)
1291       v[x[j]++] = i;
1292   } while (++i < n);
1293 
1294   /* Generate the Huffman codes and for each, make the table entries */
1295   x[0] = i = 0;                 /* first Huffman code is zero */
1296   p = v;                        /* grab values in bit order */
1297   h = -1;                       /* no tables yet--level -1 */
1298   w = -l;                       /* bits decoded == (l * h) */
1299   u[0] = (inflate_huft *)Z_NULL;        /* just to keep compilers happy */
1300   q = (inflate_huft *)Z_NULL;   /* ditto */
1301   z = 0;                        /* ditto */
1302 
1303   /* go through the bit lengths (k already is bits in shortest code) */
1304   for (; k <= g; k++)
1305   {
1306     a = c[k];
1307     while (a--)
1308     {
1309       /* here i is the Huffman code of length k bits for value *p */
1310       /* make tables up to required level */
1311       while (k > w + l)
1312       {
1313         h++;
1314         w += l;                 /* previous table always l bits */
1315 
1316         /* compute minimum size table less than or equal to l bits */
1317         z = (z = g - w) > (uInt)l ? l : z;      /* table size upper limit */
1318         if ((f = 1 << (j = k - w)) > a + 1)     /* try a k-w bit table */
1319         {                       /* too few codes for k-w bit table */
1320           f -= a + 1;           /* deduct codes from patterns left */
1321           xp = c + k;
1322           if (j < z)
1323             while (++j < z)     /* try smaller tables up to z bits */
1324             {
1325               if ((f <<= 1) <= *++xp)
1326                 break;          /* enough codes to use up j bits */
1327               f -= *xp;         /* else deduct codes from patterns */
1328             }
1329         }
1330         z = 1 << j;             /* table entries for j-bit table */
1331 
1332         /* allocate and link in new table */
1333         if ((q = (inflate_huft *)ZALLOC
1334              (zs,z + 1,sizeof(inflate_huft))) == Z_NULL)
1335         {
1336           if (h)
1337             inflate_trees_free(u[0], zs);
1338           return Z_MEM_ERROR;   /* not enough memory */
1339         }
1340     q->word.Nalloc = z + 1;
1341 #ifdef DEBUG_ZLIB
1342         inflate_hufts += z + 1;
1343 #endif
1344         *t = q + 1;             /* link to list for huft_free() */
1345         *(t = &(q->next)) = Z_NULL;
1346         u[h] = ++q;             /* table starts after link */
1347 
1348         /* connect to last table, if there is one */
1349         if (h)
1350         {
1351           x[h] = i;             /* save pattern for backing up */
1352           r.bits = (Byte)l;     /* bits to dump before this table */
1353           r.exop = (Byte)j;     /* bits in this table */
1354           r.next = q;           /* pointer to this table */
1355           j = i >> (w - l);     /* (get around Turbo C bug) */
1356           u[h-1][j] = r;        /* connect to last table */
1357         }
1358       }
1359 
1360       /* set up table entry in r */
1361       r.bits = (Byte)(k - w);
1362       if (p >= v + n)
1363         r.exop = 128 + 64;      /* out of values--invalid code */
1364       else if (*p < s)
1365       {
1366         r.exop = (Byte)(*p < 256 ? 0 : 32 + 64);     /* 256 is end-of-block */
1367         r.base = *p++;          /* simple code is just the value */
1368       }
1369       else
1370       {
1371         r.exop = (Byte)e[*p - s] + 16 + 64; /* non-simple--look up in lists */
1372         r.base = d[*p++ - s];
1373       }
1374 
1375       /* fill code-like entries with r */
1376       f = 1 << (k - w);
1377       for (j = i >> w; j < z; j += f)
1378         q[j] = r;
1379 
1380       /* backwards increment the k-bit code i */
1381       for (j = 1 << (k - 1); i & j; j >>= 1)
1382         i ^= j;
1383       i ^= j;
1384 
1385       /* backup over finished tables */
1386       while ((i & ((1 << w) - 1)) != x[h])
1387       {
1388         h--;                    /* don't need to update q */
1389         w -= l;
1390       }
1391     }
1392   }
1393 
1394   /* Return Z_BUF_ERROR if we were given an incomplete table */
1395   return y != 0 && g != 1 ? Z_BUF_ERROR : Z_OK;
1396 }
1397 
1398 local int inflate_trees_bits(c, bb, tb, z)
1399 uIntf *c;               /* 19 code lengths */
1400 uIntf *bb;              /* bits tree desired/actual depth */
1401 inflate_huft * FAR *tb; /* bits tree result */
1402 z_stream *z;            /* for zfree function */
1403 {
1404   int r;
1405 
1406   r = huft_build(c, 19, 19, (uIntf*)Z_NULL, (uIntf*)Z_NULL, tb, bb, z);
1407   if (r == Z_DATA_ERROR)
1408     z->msg = "oversubscribed dynamic bit lengths tree";
1409   else if (r == Z_BUF_ERROR)
1410   {
1411     inflate_trees_free(*tb, z);
1412     z->msg = "incomplete dynamic bit lengths tree";
1413     r = Z_DATA_ERROR;
1414   }
1415   return r;
1416 }
1417 
1418 local int inflate_trees_dynamic(nl, nd, c, bl, bd, tl, td, z)
1419 uInt nl;                /* number of literal/length codes */
1420 uInt nd;                /* number of distance codes */
1421 uIntf *c;               /* that many (total) code lengths */
1422 uIntf *bl;              /* literal desired/actual bit depth */
1423 uIntf *bd;              /* distance desired/actual bit depth */
1424 inflate_huft * FAR *tl; /* literal/length tree result */
1425 inflate_huft * FAR *td; /* distance tree result */
1426 z_stream *z;            /* for zfree function */
1427 {
1428   int r;
1429 
1430   /* build literal/length tree */
1431   if ((r = huft_build(c, nl, 257, cplens, cplext, tl, bl, z)) != Z_OK)
1432   {
1433     if (r == Z_DATA_ERROR)
1434       z->msg = "oversubscribed literal/length tree";
1435     else if (r == Z_BUF_ERROR)
1436     {
1437       inflate_trees_free(*tl, z);
1438       z->msg = "incomplete literal/length tree";
1439       r = Z_DATA_ERROR;
1440     }
1441     return r;
1442   }
1443 
1444   /* build distance tree */
1445   if ((r = huft_build(c + nl, nd, 0, cpdist, cpdext, td, bd, z)) != Z_OK)
1446   {
1447     if (r == Z_DATA_ERROR)
1448       z->msg = "oversubscribed literal/length tree";
1449     else if (r == Z_BUF_ERROR) {
1450 #ifdef PKZIP_BUG_WORKAROUND
1451       r = Z_OK;
1452     }
1453 #else
1454       inflate_trees_free(*td, z);
1455       z->msg = "incomplete literal/length tree";
1456       r = Z_DATA_ERROR;
1457     }
1458     inflate_trees_free(*tl, z);
1459     return r;
1460 #endif
1461   }
1462 
1463   /* done */
1464   return Z_OK;
1465 }
1466 
1467 /* build fixed tables only once--keep them here */
1468 local int fixed_lock = 0;
1469 local int fixed_built = 0;
1470 #define FIXEDH 530      /* number of hufts used by fixed tables */
1471 local uInt fixed_left = FIXEDH;
1472 local inflate_huft fixed_mem[FIXEDH];
1473 local uInt fixed_bl;
1474 local uInt fixed_bd;
1475 local inflate_huft *fixed_tl;
1476 local inflate_huft *fixed_td;
1477 
1478 local voidpf falloc(q, n, s)
1479 voidpf q;        /* opaque pointer (not used) */
1480 uInt n;         /* number of items */
1481 uInt s;         /* size of item */
1482 {
1483   Assert(s == sizeof(inflate_huft) && n <= fixed_left,
1484          "inflate_trees falloc overflow");
1485   if (q) s++; /* to make some compilers happy */
1486   fixed_left -= n;
1487   return (voidpf)(fixed_mem + fixed_left);
1488 }
1489 
1490 local void ffree(q, p, n)
1491 voidpf q;
1492 voidpf p;
1493 uInt n;
1494 {
1495   Assert(0, "inflate_trees ffree called!");
1496   if (q) q = p; /* to make some compilers happy */
1497 }
1498 
1499 local int inflate_trees_fixed(bl, bd, tl, td)
1500 uIntf *bl;               /* literal desired/actual bit depth */
1501 uIntf *bd;               /* distance desired/actual bit depth */
1502 inflate_huft * FAR *tl;  /* literal/length tree result */
1503 inflate_huft * FAR *td;  /* distance tree result */
1504 {
1505   /* build fixed tables if not built already--lock out other instances */
1506   while (++fixed_lock > 1)
1507     fixed_lock--;
1508   if (!fixed_built)
1509   {
1510     int k;              /* temporary variable */
1511     unsigned c[288];    /* length list for huft_build */
1512     z_stream z;         /* for falloc function */
1513 
1514     /* set up fake z_stream for memory routines */
1515     z.zalloc = falloc;
1516     z.zfree = ffree;
1517     z.opaque = Z_NULL;
1518 
1519     /* literal table */
1520     for (k = 0; k < 144; k++)
1521       c[k] = 8;
1522     for (; k < 256; k++)
1523       c[k] = 9;
1524     for (; k < 280; k++)
1525       c[k] = 7;
1526     for (; k < 288; k++)
1527       c[k] = 8;
1528     fixed_bl = 7;
1529     huft_build(c, 288, 257, cplens, cplext, &fixed_tl, &fixed_bl, &z);
1530 
1531     /* distance table */
1532     for (k = 0; k < 30; k++)
1533       c[k] = 5;
1534     fixed_bd = 5;
1535     huft_build(c, 30, 0, cpdist, cpdext, &fixed_td, &fixed_bd, &z);
1536 
1537     /* done */
1538     fixed_built = 1;
1539   }
1540   fixed_lock--;
1541   *bl = fixed_bl;
1542   *bd = fixed_bd;
1543   *tl = fixed_tl;
1544   *td = fixed_td;
1545   return Z_OK;
1546 }
1547 
1548 local int inflate_trees_free(t, z)
1549 inflate_huft *t;        /* table to free */
1550 z_stream *z;            /* for zfree function */
1551 /* Free the malloc'ed tables built by huft_build(), which makes a linked
1552    list of the tables it made, with the links in a dummy first entry of
1553    each table. */
1554 {
1555   register inflate_huft *p, *q;
1556 
1557   /* Go through linked list, freeing from the malloced (t[-1]) address. */
1558   p = t;
1559   while (p != Z_NULL)
1560   {
1561     q = (--p)->next;
1562     ZFREE(z, p, p->word.Nalloc * sizeof(inflate_huft));
1563     p = q;
1564   }
1565   return Z_OK;
1566 }
1567 
1568 /*+++++*/
1569 /* infcodes.c -- process literals and length/distance pairs
1570  * Copyright (C) 1995 Mark Adler
1571  * For conditions of distribution and use, see copyright notice in zlib.h
1572  */
1573 
1574 /* simplify the use of the inflate_huft type with some defines */
1575 #define base more.Base
1576 #define next more.Next
1577 #define exop word.what.Exop
1578 #define bits word.what.Bits
1579 
1580 /* inflate codes private state */
1581 struct inflate_codes_state {
1582 
1583   /* mode */
1584   enum {        /* waiting for "i:"=input, "o:"=output, "x:"=nothing */
1585       START,    /* x: set up for LEN */
1586       LEN,      /* i: get length/literal/eob next */
1587       LENEXT,   /* i: getting length extra (have base) */
1588       DIST,     /* i: get distance next */
1589       DISTEXT,  /* i: getting distance extra */
1590       COPY,     /* o: copying bytes in window, waiting for space */
1591       LIT,      /* o: got literal, waiting for output space */
1592       WASH,     /* o: got eob, possibly still output waiting */
1593       END,      /* x: got eob and all data flushed */
1594       BADCODE}  /* x: got error */
1595     mode;               /* current inflate_codes mode */
1596 
1597   /* mode dependent information */
1598   uInt len;
1599   union {
1600     struct {
1601       inflate_huft *tree;       /* pointer into tree */
1602       uInt need;                /* bits needed */
1603     } code;             /* if LEN or DIST, where in tree */
1604     uInt lit;           /* if LIT, literal */
1605     struct {
1606       uInt get;                 /* bits to get for extra */
1607       uInt dist;                /* distance back to copy from */
1608     } copy;             /* if EXT or COPY, where and how much */
1609   } sub;                /* submode */
1610 
1611   /* mode independent information */
1612   Byte lbits;           /* ltree bits decoded per branch */
1613   Byte dbits;           /* dtree bits decoder per branch */
1614   inflate_huft *ltree;          /* literal/length/eob tree */
1615   inflate_huft *dtree;          /* distance tree */
1616 
1617 };
1618 
1619 local inflate_codes_statef *inflate_codes_new(bl, bd, tl, td, z)
1620 uInt bl, bd;
1621 inflate_huft *tl, *td;
1622 z_stream *z;
1623 {
1624   inflate_codes_statef *c;
1625 
1626   if ((c = (inflate_codes_statef *)
1627        ZALLOC(z,1,sizeof(struct inflate_codes_state))) != Z_NULL)
1628   {
1629     c->mode = START;
1630     c->lbits = (Byte)bl;
1631     c->dbits = (Byte)bd;
1632     c->ltree = tl;
1633     c->dtree = td;
1634     Tracev(( "inflate:       codes new\n"));
1635   }
1636   return c;
1637 }
1638 
1639 local int inflate_codes(s, z, r)
1640 inflate_blocks_statef *s;
1641 z_stream *z;
1642 int r;
1643 {
1644   uInt j;               /* temporary storage */
1645   inflate_huft *t;      /* temporary pointer */
1646   uInt e;               /* extra bits or operation */
1647   uLong b;              /* bit buffer */
1648   uInt k;               /* bits in bit buffer */
1649   Bytef *p;             /* input data pointer */
1650   uInt n;               /* bytes available there */
1651   Bytef *q;             /* output window write pointer */
1652   uInt m;               /* bytes to end of window or read pointer */
1653   Bytef *f;             /* pointer to copy strings from */
1654   inflate_codes_statef *c = s->sub.decode.codes;  /* codes state */
1655 
1656   /* copy input/output information to locals (UPDATE macro restores) */
1657   LOAD
1658 
1659   /* process input and output based on current state */
1660   while (1) switch (c->mode)
1661   {             /* waiting for "i:"=input, "o:"=output, "x:"=nothing */
1662     case START:         /* x: set up for LEN */
1663 #ifndef SLOW
1664       if (m >= 258 && n >= 10)
1665       {
1666         UPDATE
1667         r = inflate_fast(c->lbits, c->dbits, c->ltree, c->dtree, s, z);
1668         LOAD
1669         if (r != Z_OK)
1670         {
1671           c->mode = r == Z_STREAM_END ? WASH : BADCODE;
1672           break;
1673         }
1674       }
1675 #endif /* !SLOW */
1676       c->sub.code.need = c->lbits;
1677       c->sub.code.tree = c->ltree;
1678       c->mode = LEN;
1679     case LEN:           /* i: get length/literal/eob next */
1680       j = c->sub.code.need;
1681       NEEDBITS(j)
1682       t = c->sub.code.tree + ((uInt)b & inflate_mask[j]);
1683       DUMPBITS(t->bits)
1684       e = (uInt)(t->exop);
1685       if (e == 0)               /* literal */
1686       {
1687         c->sub.lit = t->base;
1688         Tracevv(( t->base >= 0x20 && t->base < 0x7f ?
1689                  "inflate:         literal '%c'\n" :
1690                  "inflate:         literal 0x%02x\n", t->base));
1691         c->mode = LIT;
1692         break;
1693       }
1694       if (e & 16)               /* length */
1695       {
1696         c->sub.copy.get = e & 15;
1697         c->len = t->base;
1698         c->mode = LENEXT;
1699         break;
1700       }
1701       if ((e & 64) == 0)        /* next table */
1702       {
1703         c->sub.code.need = e;
1704         c->sub.code.tree = t->next;
1705         break;
1706       }
1707       if (e & 32)               /* end of block */
1708       {
1709         Tracevv(( "inflate:         end of block\n"));
1710         c->mode = WASH;
1711         break;
1712       }
1713       c->mode = BADCODE;        /* invalid code */
1714       z->msg = "invalid literal/length code";
1715       r = Z_DATA_ERROR;
1716       LEAVE
1717     case LENEXT:        /* i: getting length extra (have base) */
1718       j = c->sub.copy.get;
1719       NEEDBITS(j)
1720       c->len += (uInt)b & inflate_mask[j];
1721       DUMPBITS(j)
1722       c->sub.code.need = c->dbits;
1723       c->sub.code.tree = c->dtree;
1724       Tracevv(( "inflate:         length %u\n", c->len));
1725       c->mode = DIST;
1726     case DIST:          /* i: get distance next */
1727       j = c->sub.code.need;
1728       NEEDBITS(j)
1729       t = c->sub.code.tree + ((uInt)b & inflate_mask[j]);
1730       DUMPBITS(t->bits)
1731       e = (uInt)(t->exop);
1732       if (e & 16)               /* distance */
1733       {
1734         c->sub.copy.get = e & 15;
1735         c->sub.copy.dist = t->base;
1736         c->mode = DISTEXT;
1737         break;
1738       }
1739       if ((e & 64) == 0)        /* next table */
1740       {
1741         c->sub.code.need = e;
1742         c->sub.code.tree = t->next;
1743         break;
1744       }
1745       c->mode = BADCODE;        /* invalid code */
1746       z->msg = "invalid distance code";
1747       r = Z_DATA_ERROR;
1748       LEAVE
1749     case DISTEXT:       /* i: getting distance extra */
1750       j = c->sub.copy.get;
1751       NEEDBITS(j)
1752       c->sub.copy.dist += (uInt)b & inflate_mask[j];
1753       DUMPBITS(j)
1754       Tracevv(( "inflate:         distance %u\n", c->sub.copy.dist));
1755       c->mode = COPY;
1756     case COPY:          /* o: copying bytes in window, waiting for space */
1757 #ifndef __TURBOC__ /* Turbo C bug for following expression */
1758       f = (uInt)(q - s->window) < c->sub.copy.dist ?
1759           s->end - (c->sub.copy.dist - (q - s->window)) :
1760           q - c->sub.copy.dist;
1761 #else
1762       f = q - c->sub.copy.dist;
1763       if ((uInt)(q - s->window) < c->sub.copy.dist)
1764         f = s->end - (c->sub.copy.dist - (q - s->window));
1765 #endif
1766       while (c->len)
1767       {
1768         NEEDOUT
1769         OUTBYTE(*f++)
1770         if (f == s->end)
1771           f = s->window;
1772         c->len--;
1773       }
1774       c->mode = START;
1775       break;
1776     case LIT:           /* o: got literal, waiting for output space */
1777       NEEDOUT
1778       OUTBYTE(c->sub.lit)
1779       c->mode = START;
1780       break;
1781     case WASH:          /* o: got eob, possibly more output */
1782       FLUSH
1783       if (s->read != s->write)
1784         LEAVE
1785       c->mode = END;
1786     case END:
1787       r = Z_STREAM_END;
1788       LEAVE
1789     case BADCODE:       /* x: got error */
1790       r = Z_DATA_ERROR;
1791       LEAVE
1792     default:
1793       r = Z_STREAM_ERROR;
1794       LEAVE
1795   }
1796 }
1797 
1798 local void inflate_codes_free(c, z)
1799 inflate_codes_statef *c;
1800 z_stream *z;
1801 {
1802   ZFREE(z, c, sizeof(struct inflate_codes_state));
1803   Tracev(( "inflate:       codes free\n"));
1804 }
1805 
1806 /*+++++*/
1807 /* inflate_util.c -- data and routines common to blocks and codes
1808  * Copyright (C) 1995 Mark Adler
1809  * For conditions of distribution and use, see copyright notice in zlib.h
1810  */
1811 
1812 /* copy as much as possible from the sliding window to the output area */
1813 local int inflate_flush(s, z, r)
1814 inflate_blocks_statef *s;
1815 z_stream *z;
1816 int r;
1817 {
1818   uInt n;
1819   Bytef *p, *q;
1820 
1821   /* local copies of source and destination pointers */
1822   p = z->next_out;
1823   q = s->read;
1824 
1825   /* compute number of bytes to copy as far as end of window */
1826   n = (uInt)((q <= s->write ? s->write : s->end) - q);
1827   if (n > z->avail_out) n = z->avail_out;
1828   if (n && r == Z_BUF_ERROR) r = Z_OK;
1829 
1830   /* update counters */
1831   z->avail_out -= n;
1832   z->total_out += n;
1833 
1834   /* update check information */
1835   if (s->checkfn != Z_NULL)
1836     s->check = (*s->checkfn)(s->check, q, n);
1837 
1838   /* copy as far as end of window */
1839   zmemcpy(p, q, n);
1840   p += n;
1841   q += n;
1842 
1843   /* see if more to copy at beginning of window */
1844   if (q == s->end)
1845   {
1846     /* wrap pointers */
1847     q = s->window;
1848     if (s->write == s->end)
1849       s->write = s->window;
1850 
1851     /* compute bytes to copy */
1852     n = (uInt)(s->write - q);
1853     if (n > z->avail_out) n = z->avail_out;
1854     if (n && r == Z_BUF_ERROR) r = Z_OK;
1855 
1856     /* update counters */
1857     z->avail_out -= n;
1858     z->total_out += n;
1859 
1860     /* update check information */
1861     if (s->checkfn != Z_NULL)
1862       s->check = (*s->checkfn)(s->check, q, n);
1863 
1864     /* copy */
1865     zmemcpy(p, q, n);
1866     p += n;
1867     q += n;
1868   }
1869 
1870   /* update pointers */
1871   z->next_out = p;
1872   s->read = q;
1873 
1874   /* done */
1875   return r;
1876 }
1877 
1878 /*+++++*/
1879 /* inffast.c -- process literals and length/distance pairs fast
1880  * Copyright (C) 1995 Mark Adler
1881  * For conditions of distribution and use, see copyright notice in zlib.h
1882  */
1883 
1884 /* simplify the use of the inflate_huft type with some defines */
1885 #define base more.Base
1886 #define next more.Next
1887 #define exop word.what.Exop
1888 #define bits word.what.Bits
1889 
1890 /* macros for bit input with no checking and for returning unused bytes */
1891 #define GRABBITS(j) {while(k<(j)){b|=((uLong)NEXTBYTE)<<k;k+=8;}}
1892 #define UNGRAB {n+=(c=k>>3);p-=c;k&=7;}
1893 
1894 /* Called with number of bytes left to write in window at least 258
1895    (the maximum string length) and number of input bytes available
1896    at least ten.  The ten bytes are six bytes for the longest length/
1897    distance pair plus four bytes for overloading the bit buffer. */
1898 
1899 local int inflate_fast(bl, bd, tl, td, s, z)
1900 uInt bl, bd;
1901 inflate_huft *tl, *td;
1902 inflate_blocks_statef *s;
1903 z_stream *z;
1904 {
1905   inflate_huft *t;      /* temporary pointer */
1906   uInt e;               /* extra bits or operation */
1907   uLong b;              /* bit buffer */
1908   uInt k;               /* bits in bit buffer */
1909   Bytef *p;             /* input data pointer */
1910   uInt n;               /* bytes available there */
1911   Bytef *q;             /* output window write pointer */
1912   uInt m;               /* bytes to end of window or read pointer */
1913   uInt ml;              /* mask for literal/length tree */
1914   uInt md;              /* mask for distance tree */
1915   uInt c;               /* bytes to copy */
1916   uInt d;               /* distance back to copy from */
1917   Bytef *r;             /* copy source pointer */
1918 
1919   /* load input, output, bit values */
1920   LOAD
1921 
1922   /* initialize masks */
1923   ml = inflate_mask[bl];
1924   md = inflate_mask[bd];
1925 
1926   /* do until not enough input or output space for fast loop */
1927   do {                          /* assume called with m >= 258 && n >= 10 */
1928     /* get literal/length code */
1929     GRABBITS(20)                /* max bits for literal/length code */
1930     if ((e = (t = tl + ((uInt)b & ml))->exop) == 0)
1931     {
1932       DUMPBITS(t->bits)
1933       Tracevv(( t->base >= 0x20 && t->base < 0x7f ?
1934                 "inflate:         * literal '%c'\n" :
1935                 "inflate:         * literal 0x%02x\n", t->base));
1936       *q++ = (Byte)t->base;
1937       m--;
1938       continue;
1939     }
1940     do {
1941       DUMPBITS(t->bits)
1942       if (e & 16)
1943       {
1944         /* get extra bits for length */
1945         e &= 15;
1946         c = t->base + ((uInt)b & inflate_mask[e]);
1947         DUMPBITS(e)
1948         Tracevv(( "inflate:         * length %u\n", c));
1949 
1950         /* decode distance base of block to copy */
1951         GRABBITS(15);           /* max bits for distance code */
1952         e = (t = td + ((uInt)b & md))->exop;
1953         do {
1954           DUMPBITS(t->bits)
1955           if (e & 16)
1956           {
1957             /* get extra bits to add to distance base */
1958             e &= 15;
1959             GRABBITS(e)         /* get extra bits (up to 13) */
1960             d = t->base + ((uInt)b & inflate_mask[e]);
1961             DUMPBITS(e)
1962             Tracevv(( "inflate:         * distance %u\n", d));
1963 
1964             /* do the copy */
1965             m -= c;
1966             if ((uInt)(q - s->window) >= d)     /* offset before dest */
1967             {                                   /*  just copy */
1968               r = q - d;
1969               *q++ = *r++;  c--;        /* minimum count is three, */
1970               *q++ = *r++;  c--;        /*  so unroll loop a little */
1971             }
1972             else                        /* else offset after destination */
1973             {
1974               e = d - (q - s->window);  /* bytes from offset to end */
1975               r = s->end - e;           /* pointer to offset */
1976               if (c > e)                /* if source crosses, */
1977               {
1978                 c -= e;                 /* copy to end of window */
1979                 do {
1980                   *q++ = *r++;
1981                 } while (--e);
1982                 r = s->window;          /* copy rest from start of window */
1983               }
1984             }
1985             do {                        /* copy all or what's left */
1986               *q++ = *r++;
1987             } while (--c);
1988             break;
1989           }
1990           else if ((e & 64) == 0)
1991             e = (t = t->next + ((uInt)b & inflate_mask[e]))->exop;
1992           else
1993           {
1994             z->msg = "invalid distance code";
1995             UNGRAB
1996             UPDATE
1997             return Z_DATA_ERROR;
1998           }
1999         } while (1);
2000         break;
2001       }
2002       if ((e & 64) == 0)
2003       {
2004         if ((e = (t = t->next + ((uInt)b & inflate_mask[e]))->exop) == 0)
2005         {
2006           DUMPBITS(t->bits)
2007           Tracevv(( t->base >= 0x20 && t->base < 0x7f ?
2008                     "inflate:         * literal '%c'\n" :
2009                     "inflate:         * literal 0x%02x\n", t->base));
2010           *q++ = (Byte)t->base;
2011           m--;
2012           break;
2013         }
2014       }
2015       else if (e & 32)
2016       {
2017         Tracevv(( "inflate:         * end of block\n"));
2018         UNGRAB
2019         UPDATE
2020         return Z_STREAM_END;
2021       }
2022       else
2023       {
2024         z->msg = "invalid literal/length code";
2025         UNGRAB
2026         UPDATE
2027         return Z_DATA_ERROR;
2028       }
2029     } while (1);
2030   } while (m >= 258 && n >= 10);
2031 
2032   /* not enough input or output--restore pointers and return */
2033   UNGRAB
2034   UPDATE
2035   return Z_OK;
2036 }
2037 
2038 /*+++++*/
2039 /* zutil.c -- target dependent utility functions for the compression library
2040  * Copyright (C) 1995 Jean-loup Gailly.
2041  * For conditions of distribution and use, see copyright notice in zlib.h
2042  */
2043 
2044 /* From: zutil.c,v 1.8 1995/05/03 17:27:12 jloup Exp */
2045 
2046 char *zlib_version = ZLIB_VERSION;
2047 
2048 char *z_errmsg[] = {
2049 "stream end",          /* Z_STREAM_END    1 */
2050 "",                    /* Z_OK            0 */
2051 "file error",          /* Z_ERRNO        (-1) */
2052 "stream error",        /* Z_STREAM_ERROR (-2) */
2053 "data error",          /* Z_DATA_ERROR   (-3) */
2054 "insufficient memory", /* Z_MEM_ERROR    (-4) */
2055 "buffer error",        /* Z_BUF_ERROR    (-5) */
2056 ""};
2057 
2058 /*+++++*/
2059 /* adler32.c -- compute the Adler-32 checksum of a data stream
2060  * Copyright (C) 1995 Mark Adler
2061  * For conditions of distribution and use, see copyright notice in zlib.h
2062  */
2063 
2064 /* From: adler32.c,v 1.6 1995/05/03 17:27:08 jloup Exp */
2065 
2066 #define BASE 65521L /* largest prime smaller than 65536 */
2067 #define NMAX 5552
2068 /* NMAX is the largest n such that 255n(n+1)/2 + (n+1)(BASE-1) <= 2^32-1 */
2069 
2070 #define DO1(buf)  {s1 += *buf++; s2 += s1;}
2071 #define DO2(buf)  DO1(buf); DO1(buf);
2072 #define DO4(buf)  DO2(buf); DO2(buf);
2073 #define DO8(buf)  DO4(buf); DO4(buf);
2074 #define DO16(buf) DO8(buf); DO8(buf);
2075 
2076 /* ========================================================================= */
2077 uLong adler32(adler, buf, len)
2078     uLong adler;
2079     Bytef *buf;
2080     uInt len;
2081 {
2082     unsigned long s1 = adler & 0xffff;
2083     unsigned long s2 = (adler >> 16) & 0xffff;
2084     int k;
2085 
2086     if (buf == Z_NULL) return 1L;
2087 
2088     while (len > 0) {
2089         k = len < NMAX ? len : NMAX;
2090         len -= k;
2091         while (k >= 16) {
2092             DO16(buf);
2093             k -= 16;
2094         }
2095         if (k != 0) do {
2096             DO1(buf);
2097         } while (--k);
2098         s1 %= BASE;
2099         s2 %= BASE;
2100     }
2101     return (s2 << 16) | s1;
2102 }