1*37da2899SCharles.Forsyth /* infutil.h -- types and macros common to blocks and codes 2*37da2899SCharles.Forsyth * Copyright (C) 1995-2002 Mark Adler 3*37da2899SCharles.Forsyth * For conditions of distribution and use, see copyright notice in zlib.h 4*37da2899SCharles.Forsyth */ 5*37da2899SCharles.Forsyth 6*37da2899SCharles.Forsyth /* WARNING: this file should *not* be used by applications. It is 7*37da2899SCharles.Forsyth part of the implementation of the compression library and is 8*37da2899SCharles.Forsyth subject to change. Applications should only use zlib.h. 9*37da2899SCharles.Forsyth */ 10*37da2899SCharles.Forsyth 11*37da2899SCharles.Forsyth #ifndef _INFUTIL_H 12*37da2899SCharles.Forsyth #define _INFUTIL_H 13*37da2899SCharles.Forsyth 14*37da2899SCharles.Forsyth typedef enum { 15*37da2899SCharles.Forsyth TYPE, /* get type bits (3, including end bit) */ 16*37da2899SCharles.Forsyth LENS, /* get lengths for stored */ 17*37da2899SCharles.Forsyth STORED, /* processing stored block */ 18*37da2899SCharles.Forsyth TABLE, /* get table lengths */ 19*37da2899SCharles.Forsyth BTREE, /* get bit lengths tree for a dynamic block */ 20*37da2899SCharles.Forsyth DTREE, /* get length, distance trees for a dynamic block */ 21*37da2899SCharles.Forsyth CODES, /* processing fixed or dynamic block */ 22*37da2899SCharles.Forsyth DRY, /* output remaining window bytes */ 23*37da2899SCharles.Forsyth DONE, /* finished last block, done */ 24*37da2899SCharles.Forsyth BAD} /* got a data error--stuck here */ 25*37da2899SCharles.Forsyth inflate_block_mode; 26*37da2899SCharles.Forsyth 27*37da2899SCharles.Forsyth /* inflate blocks semi-private state */ 28*37da2899SCharles.Forsyth struct inflate_blocks_state { 29*37da2899SCharles.Forsyth 30*37da2899SCharles.Forsyth /* mode */ 31*37da2899SCharles.Forsyth inflate_block_mode mode; /* current inflate_block mode */ 32*37da2899SCharles.Forsyth 33*37da2899SCharles.Forsyth /* mode dependent information */ 34*37da2899SCharles.Forsyth union { 35*37da2899SCharles.Forsyth uInt left; /* if STORED, bytes left to copy */ 36*37da2899SCharles.Forsyth struct { 37*37da2899SCharles.Forsyth uInt table; /* table lengths (14 bits) */ 38*37da2899SCharles.Forsyth uInt index; /* index into blens (or border) */ 39*37da2899SCharles.Forsyth uIntf *blens; /* bit lengths of codes */ 40*37da2899SCharles.Forsyth uInt bb; /* bit length tree depth */ 41*37da2899SCharles.Forsyth inflate_huft *tb; /* bit length decoding tree */ 42*37da2899SCharles.Forsyth } trees; /* if DTREE, decoding info for trees */ 43*37da2899SCharles.Forsyth struct { 44*37da2899SCharles.Forsyth inflate_codes_statef 45*37da2899SCharles.Forsyth *codes; 46*37da2899SCharles.Forsyth } decode; /* if CODES, current state */ 47*37da2899SCharles.Forsyth } sub; /* submode */ 48*37da2899SCharles.Forsyth uInt last; /* true if this block is the last block */ 49*37da2899SCharles.Forsyth 50*37da2899SCharles.Forsyth /* mode independent information */ 51*37da2899SCharles.Forsyth uInt bitk; /* bits in bit buffer */ 52*37da2899SCharles.Forsyth uLong bitb; /* bit buffer */ 53*37da2899SCharles.Forsyth inflate_huft *hufts; /* single malloc for tree space */ 54*37da2899SCharles.Forsyth Bytef *window; /* sliding window */ 55*37da2899SCharles.Forsyth Bytef *end; /* one byte after sliding window */ 56*37da2899SCharles.Forsyth Bytef *read; /* window read pointer */ 57*37da2899SCharles.Forsyth Bytef *write; /* window write pointer */ 58*37da2899SCharles.Forsyth check_func checkfn; /* check function */ 59*37da2899SCharles.Forsyth uLong check; /* check on output */ 60*37da2899SCharles.Forsyth 61*37da2899SCharles.Forsyth }; 62*37da2899SCharles.Forsyth 63*37da2899SCharles.Forsyth 64*37da2899SCharles.Forsyth /* defines for inflate input/output */ 65*37da2899SCharles.Forsyth /* update pointers and return */ 66*37da2899SCharles.Forsyth #define UPDBITS {s->bitb=b;s->bitk=k;} 67*37da2899SCharles.Forsyth #define UPDIN {z->avail_in=n;z->total_in+=p-z->next_in;z->next_in=p;} 68*37da2899SCharles.Forsyth #define UPDOUT {s->write=q;} 69*37da2899SCharles.Forsyth #define UPDATE {UPDBITS UPDIN UPDOUT} 70*37da2899SCharles.Forsyth #define LEAVE {UPDATE return inflate_flush(s,z,r);} 71*37da2899SCharles.Forsyth /* get bytes and bits */ 72*37da2899SCharles.Forsyth #define LOADIN {p=z->next_in;n=z->avail_in;b=s->bitb;k=s->bitk;} 73*37da2899SCharles.Forsyth #define NEEDBYTE {if(n)r=Z_OK;else LEAVE} 74*37da2899SCharles.Forsyth #define NEXTBYTE (n--,*p++) 75*37da2899SCharles.Forsyth #define NEEDBITS(j) {while(k<(j)){NEEDBYTE;b|=((uLong)NEXTBYTE)<<k;k+=8;}} 76*37da2899SCharles.Forsyth #define DUMPBITS(j) {b>>=(j);k-=(j);} 77*37da2899SCharles.Forsyth /* output bytes */ 78*37da2899SCharles.Forsyth #define WAVAIL (uInt)(q<s->read?s->read-q-1:s->end-q) 79*37da2899SCharles.Forsyth #define LOADOUT {q=s->write;m=(uInt)WAVAIL;} 80*37da2899SCharles.Forsyth #define WRAP {if(q==s->end&&s->read!=s->window){q=s->window;m=(uInt)WAVAIL;}} 81*37da2899SCharles.Forsyth #define FLUSH {UPDOUT r=inflate_flush(s,z,r); LOADOUT} 82*37da2899SCharles.Forsyth #define NEEDOUT {if(m==0){WRAP if(m==0){FLUSH WRAP if(m==0) LEAVE}}r=Z_OK;} 83*37da2899SCharles.Forsyth #define OUTBYTE(a) {*q++=(Byte)(a);m--;} 84*37da2899SCharles.Forsyth /* load local pointers */ 85*37da2899SCharles.Forsyth #define LOAD {LOADIN LOADOUT} 86*37da2899SCharles.Forsyth 87*37da2899SCharles.Forsyth /* masks for lower bits (size given to avoid silly warnings with Visual C++) */ 88*37da2899SCharles.Forsyth local uInt inflate_mask[17]; 89*37da2899SCharles.Forsyth 90*37da2899SCharles.Forsyth /* copy as much as possible from the sliding window to the output area */ 91*37da2899SCharles.Forsyth local int inflate_flush OF(( 92*37da2899SCharles.Forsyth inflate_blocks_statef *, 93*37da2899SCharles.Forsyth z_streamp , 94*37da2899SCharles.Forsyth int)); 95*37da2899SCharles.Forsyth 96*37da2899SCharles.Forsyth #endif 97