Back to index

avfs  1.0.1
infcodes.c
Go to the documentation of this file.
00001 /* infcodes.c -- process literals and length/distance pairs
00002  * Copyright (C) 1995-2002 Mark Adler
00003  * For conditions of distribution and use, see copyright notice in zlib.h 
00004  */
00005 
00006 #include "zutil.h"
00007 #include "inftrees.h"
00008 #include "infblock.h"
00009 #include "infcodes.h"
00010 #include "infutil.h"
00011 #include "inffast.h"
00012 
00013 #include <assert.h>
00014 
00015 /* simplify the use of the inflate_huft type with some defines */
00016 #define exop word.what.Exop
00017 #define bits word.what.Bits
00018 
00019 typedef enum {        /* waiting for "i:"=input, "o:"=output, "x:"=nothing */
00020       START,    /* x: set up for LEN */
00021       LEN,      /* i: get length/literal/eob next */
00022       LENEXT,   /* i: getting length extra (have base) */
00023       DIST,     /* i: get distance next */
00024       DISTEXT,  /* i: getting distance extra */
00025       COPY,     /* o: copying bytes in window, waiting for space */
00026       LIT,      /* o: got literal, waiting for output space */
00027       WASH,     /* o: got eob, possibly still output waiting */
00028       END,      /* x: got eob and all data flushed */
00029       BADCODE}  /* x: got error */
00030 inflate_codes_mode;
00031 
00032 /* inflate codes private state */
00033 struct inflate_codes_state {
00034 
00035   /* mode */
00036   inflate_codes_mode mode;      /* current inflate_codes mode */
00037 
00038   /* mode dependent information */
00039   uInt len;
00040   union {
00041     struct {
00042       inflate_huft *tree;       /* pointer into tree */
00043       uInt need;                /* bits needed */
00044     } code;             /* if LEN or DIST, where in tree */
00045     uInt lit;           /* if LIT, literal */
00046     struct {
00047       uInt get;                 /* bits to get for extra */
00048       uInt dist;                /* distance back to copy from */
00049     } copy;             /* if EXT or COPY, where and how much */
00050   } sub;                /* submode */
00051 
00052   /* mode independent information */
00053   Byte lbits;           /* ltree bits decoded per branch */
00054   Byte dbits;           /* dtree bits decoder per branch */
00055   inflate_huft *ltree;          /* literal/length/eob tree */
00056   inflate_huft *dtree;          /* distance tree */
00057 
00058 };
00059 
00060 
00061 inflate_codes_statef *inflate_codes_new(bl, bd, tl, td, z)
00062 uInt bl, bd;
00063 inflate_huft *tl;
00064 inflate_huft *td; /* need separate declaration for Borland C++ */
00065 z_streamp z;
00066 {
00067   inflate_codes_statef *c;
00068 
00069   if ((c = (inflate_codes_statef *)
00070        ZALLOC(z,1,sizeof(struct inflate_codes_state))) != Z_NULL)
00071   {
00072     c->mode = START;
00073     c->lbits = (Byte)bl;
00074     c->dbits = (Byte)bd;
00075     c->ltree = tl;
00076     c->dtree = td;
00077     Tracev((stderr, "inflate:       codes new\n"));
00078   }
00079   return c;
00080 }
00081 
00082 
00083 int inflate_codes(s, z, r)
00084 inflate_blocks_statef *s;
00085 z_streamp z;
00086 int r;
00087 {
00088   uInt j;               /* temporary storage */
00089   inflate_huft *t;      /* temporary pointer */
00090   uInt e;               /* extra bits or operation */
00091   uLong b;              /* bit buffer */
00092   uInt k;               /* bits in bit buffer */
00093   Bytef *p;             /* input data pointer */
00094   uInt n;               /* bytes available there */
00095   Bytef *q;             /* output window write pointer */
00096   uInt m;               /* bytes to end of window or read pointer */
00097   Bytef *f;             /* pointer to copy strings from */
00098   inflate_codes_statef *c = s->sub.decode.codes;  /* codes state */
00099 
00100   /* copy input/output information to locals (UPDATE macro restores) */
00101   LOAD
00102 
00103   /* process input and output based on current state */
00104   while (1) switch (c->mode)
00105   {             /* waiting for "i:"=input, "o:"=output, "x:"=nothing */
00106     case START:         /* x: set up for LEN */
00107 #ifndef SLOW
00108       if (m >= 258 && n >= 10)
00109       {
00110         UPDATE
00111         r = inflate_fast(c->lbits, c->dbits, c->ltree, c->dtree, s, z);
00112         LOAD
00113         if (r != Z_OK)
00114         {
00115           c->mode = r == Z_STREAM_END ? WASH : BADCODE;
00116           break;
00117         }
00118       }
00119 #endif /* !SLOW */
00120       c->sub.code.need = c->lbits;
00121       c->sub.code.tree = c->ltree;
00122       c->mode = LEN;
00123     case LEN:           /* i: get length/literal/eob next */
00124       j = c->sub.code.need;
00125       NEEDBITS(j)
00126       t = c->sub.code.tree + ((uInt)b & inflate_mask[j]);
00127       DUMPBITS(t->bits)
00128       e = (uInt)(t->exop);
00129       if (e == 0)               /* literal */
00130       {
00131         c->sub.lit = t->base;
00132         Tracevv((stderr, t->base >= 0x20 && t->base < 0x7f ?
00133                  "inflate:         literal '%c'\n" :
00134                  "inflate:         literal 0x%02x\n", t->base));
00135         c->mode = LIT;
00136         break;
00137       }
00138       if (e & 16)               /* length */
00139       {
00140         c->sub.copy.get = e & 15;
00141         c->len = t->base;
00142         c->mode = LENEXT;
00143         break;
00144       }
00145       if ((e & 64) == 0)        /* next table */
00146       {
00147         c->sub.code.need = e;
00148         c->sub.code.tree = t + t->base;
00149         break;
00150       }
00151       if (e & 32)               /* end of block */
00152       {
00153         Tracevv((stderr, "inflate:         end of block\n"));
00154         c->mode = WASH;
00155         break;
00156       }
00157       c->mode = BADCODE;        /* invalid code */
00158       z->msg = (char*)"invalid literal/length code";
00159       r = Z_DATA_ERROR;
00160       LEAVE
00161     case LENEXT:        /* i: getting length extra (have base) */
00162       j = c->sub.copy.get;
00163       NEEDBITS(j)
00164       c->len += (uInt)b & inflate_mask[j];
00165       DUMPBITS(j)
00166       c->sub.code.need = c->dbits;
00167       c->sub.code.tree = c->dtree;
00168       Tracevv((stderr, "inflate:         length %u\n", c->len));
00169       c->mode = DIST;
00170     case DIST:          /* i: get distance next */
00171       j = c->sub.code.need;
00172       NEEDBITS(j)
00173       t = c->sub.code.tree + ((uInt)b & inflate_mask[j]);
00174       DUMPBITS(t->bits)
00175       e = (uInt)(t->exop);
00176       if (e & 16)               /* distance */
00177       {
00178         c->sub.copy.get = e & 15;
00179         c->sub.copy.dist = t->base;
00180         c->mode = DISTEXT;
00181         break;
00182       }
00183       if ((e & 64) == 0)        /* next table */
00184       {
00185         c->sub.code.need = e;
00186         c->sub.code.tree = t + t->base;
00187         break;
00188       }
00189       c->mode = BADCODE;        /* invalid code */
00190       z->msg = (char*)"invalid distance code";
00191       r = Z_DATA_ERROR;
00192       LEAVE
00193     case DISTEXT:       /* i: getting distance extra */
00194       j = c->sub.copy.get;
00195       NEEDBITS(j)
00196       c->sub.copy.dist += (uInt)b & inflate_mask[j];
00197       DUMPBITS(j)
00198       Tracevv((stderr, "inflate:         distance %u\n", c->sub.copy.dist));
00199       c->mode = COPY;
00200     case COPY:          /* o: copying bytes in window, waiting for space */
00201       f = q - c->sub.copy.dist;
00202       while (f < s->window)             /* modulo window size-"while" instead */
00203         f += s->end - s->window;        /* of "if" handles invalid distances */
00204       while (c->len)
00205       {
00206         NEEDOUT
00207         OUTBYTE(*f++)
00208         if (f == s->end)
00209           f = s->window;
00210         c->len--;
00211       }
00212       c->mode = START;
00213       break;
00214     case LIT:           /* o: got literal, waiting for output space */
00215       NEEDOUT
00216       OUTBYTE(c->sub.lit)
00217       c->mode = START;
00218       break;
00219     case WASH:          /* o: got eob, possibly more output */
00220       if (k > 7)        /* return unused byte, if any */
00221       {
00222         Assert(k < 16, "inflate_codes grabbed too many bytes")
00223         k -= 8;
00224         n++;
00225         p--;            /* can always return one */
00226       }
00227       FLUSH
00228       if (s->read != s->write)
00229         LEAVE
00230       c->mode = END;
00231     case END:
00232       r = Z_STREAM_END;
00233       LEAVE
00234     case BADCODE:       /* x: got error */
00235       r = Z_DATA_ERROR;
00236       LEAVE
00237     default:
00238       r = Z_STREAM_ERROR;
00239       LEAVE
00240   }
00241 #ifdef NEED_DUMMY_RETURN
00242   return Z_STREAM_ERROR;  /* Some dumb compilers complain without this */
00243 #endif
00244 }
00245 
00246 
00247 void inflate_codes_free(c, z)
00248 inflate_codes_statef *c;
00249 z_streamp z;
00250 {
00251   ZFREE(z, c);
00252   Tracev((stderr, "inflate:       codes free\n"));
00253 }
00254 
00255 int inflate_codes_save(bufp, at, s, z)
00256 char **bufp;
00257 int at;
00258 inflate_blocks_statef *s;
00259 z_streamp z;
00260 {
00261   char *buf = *bufp;
00262   inflate_codes_statef *c = s->sub.decode.codes;
00263   struct inflate_codes_state ccpy = *c;
00264   char fixed_tree = 0;
00265 
00266   if(c->mode == LEN || c->mode == DIST) {
00267     assert(c->sub.code.tree >= s->hufts && c->sub.code.tree < s->hufts + MANY);
00268     ccpy.sub.code.tree = (inflate_huft *) ((long) c->sub.code.tree - (long) s->hufts);
00269   }
00270 
00271   if(c->ltree >= s->hufts && c->ltree < s->hufts + MANY) {
00272     ccpy.ltree = (inflate_huft *) ((long) c->ltree - (long) s->hufts);
00273     
00274     assert(c->dtree >= s->hufts && c->dtree < s->hufts + MANY);
00275     ccpy.dtree = (inflate_huft *) ((long) c->dtree - (long) s->hufts);
00276   } else {
00277     /* this is an assumption, it will be checked in restore */
00278     fixed_tree = 1;
00279   }
00280   
00281   *bufp = buf = realloc(buf, at + sizeof(struct inflate_codes_state) + 1);
00282   if(buf == NULL)
00283     return Z_MEM_ERROR;
00284   
00285   memcpy(buf + at, &ccpy, sizeof(struct inflate_codes_state));
00286   at += sizeof(struct inflate_codes_state);
00287 
00288   buf[at++] = fixed_tree;
00289   
00290   return at;
00291 }
00292 
00293 inflate_codes_statef *inflate_codes_restore(bufp, s, z)
00294   char **bufp;
00295   inflate_blocks_statef *s;
00296   z_streamp z;
00297 {
00298   char *buf = *bufp;
00299   inflate_codes_statef *c;
00300   char fixed_tree = 0;
00301 
00302   if ((c = (inflate_codes_statef *)
00303        ZALLOC(z,1,sizeof(struct inflate_codes_state))) == Z_NULL)
00304     return c;
00305   
00306   memcpy(c, buf, sizeof(struct inflate_codes_state));
00307 
00308   fixed_tree = buf[sizeof(struct inflate_codes_state)];
00309 
00310   if(fixed_tree == 0) {
00311     c->ltree = (inflate_huft *) ((long) s->hufts + (long) c->ltree);
00312     c->dtree = (inflate_huft *) ((long) s->hufts + (long) c->dtree);
00313   } else if(fixed_tree == 1) {
00314     Byte old_lbits = c->lbits;
00315     Byte old_dbits = c->dbits;
00316     inflate_huft *old_ltree = c->ltree;
00317     inflate_huft *old_dtree = c->dtree;
00318     uInt bl, bd;
00319 
00320     if(inflate_trees_fixed(&bl, &bd, &c->ltree, &c->dtree, z) != Z_OK) {
00321       ZFREE(z, c);
00322       return Z_NULL;
00323     }
00324 
00325     c->lbits = (Byte)bl;
00326     c->dbits = (Byte)bd;
00327 
00328     /* it was an assumption that fixed trees are used
00329        if one of the following conditions is false we were wrong */
00330     if(c->lbits != old_lbits || c->dbits != old_dbits ||
00331        c->ltree != old_ltree || c->dtree != old_dtree) {
00332       ZFREE(z, c);
00333       return Z_NULL;
00334     }
00335   } else {
00336     return Z_NULL;
00337   }
00338 
00339   buf += sizeof(struct inflate_codes_state) + 1;
00340   
00341   *bufp = buf;
00342 
00343   if(c->mode == LEN || c->mode == DIST)
00344     c->sub.code.tree = (inflate_huft *) ((long) s->hufts + (long) c->sub.code.tree);
00345 
00346   return c;
00347 }