| 1 | /* |
|---|
| 2 | * $Id: //suprahd/releases/suprahd_163/suprahd_ztvapp640_163/drivers/graphics/ZLIB/infcodes.c#1 $ |
|---|
| 3 | * $Revision: #1 $ |
|---|
| 4 | * $DateTime: 2006/02/24 17:51:46 $ |
|---|
| 5 | * $Change: 42566 $ |
|---|
| 6 | * $Author: pryush.sharma $ |
|---|
| 7 | */ |
|---|
| 8 | |
|---|
| 9 | /* infcodes.c -- process literals and length/distance pairs |
|---|
| 10 | * Copyright (C) 1995-1998 Mark Adler |
|---|
| 11 | * For conditions of distribution and use, see copyright notice in zlib.h |
|---|
| 12 | */ |
|---|
| 13 | |
|---|
| 14 | #include "zutil.h" |
|---|
| 15 | #include "inftrees.h" |
|---|
| 16 | #include "infblock.h" |
|---|
| 17 | #include "infcodes.h" |
|---|
| 18 | #include "infutil.h" |
|---|
| 19 | #include "inffast.h" |
|---|
| 20 | |
|---|
| 21 | /* simplify the use of the inflate_huft type with some defines */ |
|---|
| 22 | #define exop word.what.Exop |
|---|
| 23 | #define bits word.what.Bits |
|---|
| 24 | |
|---|
| 25 | typedef enum { /* waiting for "i:"=input, "o:"=output, "x:"=nothing */ |
|---|
| 26 | START, /* x: set up for LEN */ |
|---|
| 27 | LEN, /* i: get length/literal/eob next */ |
|---|
| 28 | LENEXT, /* i: getting length extra (have base) */ |
|---|
| 29 | DIST, /* i: get distance next */ |
|---|
| 30 | DISTEXT, /* i: getting distance extra */ |
|---|
| 31 | COPY, /* o: copying bytes in window, waiting for space */ |
|---|
| 32 | LIT, /* o: got literal, waiting for output space */ |
|---|
| 33 | WASH, /* o: got eob, possibly still output waiting */ |
|---|
| 34 | END, /* x: got eob and all data flushed */ |
|---|
| 35 | BADCODE} /* x: got error */ |
|---|
| 36 | inflate_codes_mode; |
|---|
| 37 | |
|---|
| 38 | /* inflate codes private state */ |
|---|
| 39 | struct inflate_codes_state { |
|---|
| 40 | |
|---|
| 41 | /* mode */ |
|---|
| 42 | inflate_codes_mode mode; /* current inflate_codes mode */ |
|---|
| 43 | |
|---|
| 44 | /* mode dependent information */ |
|---|
| 45 | uInt len; |
|---|
| 46 | union { |
|---|
| 47 | struct { |
|---|
| 48 | inflate_huft *tree; /* pointer into tree */ |
|---|
| 49 | uInt need; /* bits needed */ |
|---|
| 50 | } code; /* if LEN or DIST, where in tree */ |
|---|
| 51 | uInt lit; /* if LIT, literal */ |
|---|
| 52 | struct { |
|---|
| 53 | uInt get; /* bits to get for extra */ |
|---|
| 54 | uInt dist; /* distance back to copy from */ |
|---|
| 55 | } copy; /* if EXT or COPY, where and how much */ |
|---|
| 56 | } sub; /* submode */ |
|---|
| 57 | |
|---|
| 58 | /* mode independent information */ |
|---|
| 59 | Byte lbits; /* ltree bits decoded per branch */ |
|---|
| 60 | Byte dbits; /* dtree bits decoder per branch */ |
|---|
| 61 | inflate_huft *ltree; /* literal/length/eob tree */ |
|---|
| 62 | inflate_huft *dtree; /* distance tree */ |
|---|
| 63 | |
|---|
| 64 | }; |
|---|
| 65 | |
|---|
| 66 | |
|---|
| 67 | inflate_codes_statef *inflate_codes_new( |
|---|
| 68 | uInt bl, uInt bd, |
|---|
| 69 | inflate_huft *tl, |
|---|
| 70 | inflate_huft *td, /* need separate declaration for Borland C++ */ |
|---|
| 71 | z_streamp z) |
|---|
| 72 | { |
|---|
| 73 | inflate_codes_statef *c; |
|---|
| 74 | |
|---|
| 75 | if ((c = (inflate_codes_statef *) |
|---|
| 76 | ZALLOC(z,1,sizeof(struct inflate_codes_state))) != Z_NULL) |
|---|
| 77 | { |
|---|
| 78 | c->mode = START; |
|---|
| 79 | c->lbits = (Byte)bl; |
|---|
| 80 | c->dbits = (Byte)bd; |
|---|
| 81 | c->ltree = tl; |
|---|
| 82 | c->dtree = td; |
|---|
| 83 | Tracev((stderr, "inflate: codes new\n")); |
|---|
| 84 | } |
|---|
| 85 | return c; |
|---|
| 86 | } |
|---|
| 87 | |
|---|
| 88 | |
|---|
| 89 | int inflate_codes( |
|---|
| 90 | inflate_blocks_statef *s, |
|---|
| 91 | z_streamp z, |
|---|
| 92 | int r) |
|---|
| 93 | { |
|---|
| 94 | uInt j; /* temporary storage */ |
|---|
| 95 | inflate_huft *t; /* temporary pointer */ |
|---|
| 96 | uInt e; /* extra bits or operation */ |
|---|
| 97 | uLong b; /* bit buffer */ |
|---|
| 98 | uInt k; /* bits in bit buffer */ |
|---|
| 99 | Bytef *p; /* input data pointer */ |
|---|
| 100 | uInt n; /* bytes available there */ |
|---|
| 101 | Bytef *q; /* output window write pointer */ |
|---|
| 102 | uInt m; /* bytes to end of window or read pointer */ |
|---|
| 103 | Bytef *f; /* pointer to copy strings from */ |
|---|
| 104 | inflate_codes_statef *c = s->sub.decode.codes; /* codes state */ |
|---|
| 105 | |
|---|
| 106 | /* copy input/output information to locals (UPDATE_ZLIB macro restores) */ |
|---|
| 107 | LOAD |
|---|
| 108 | |
|---|
| 109 | /* process input and output based on current state */ |
|---|
| 110 | while (1) switch (c->mode) |
|---|
| 111 | { /* waiting for "i:"=input, "o:"=output, "x:"=nothing */ |
|---|
| 112 | case START: /* x: set up for LEN */ |
|---|
| 113 | #ifndef SLOW |
|---|
| 114 | if (m >= 258 && n >= 10) |
|---|
| 115 | { |
|---|
| 116 | UPDATE_ZLIB |
|---|
| 117 | r = inflate_fast(c->lbits, c->dbits, c->ltree, c->dtree, s, z); |
|---|
| 118 | LOAD |
|---|
| 119 | if (r != Z_OK) |
|---|
| 120 | { |
|---|
| 121 | c->mode = r == Z_STREAM_END ? WASH : BADCODE; |
|---|
| 122 | break; |
|---|
| 123 | } |
|---|
| 124 | } |
|---|
| 125 | #endif /* !SLOW */ |
|---|
| 126 | c->sub.code.need = c->lbits; |
|---|
| 127 | c->sub.code.tree = c->ltree; |
|---|
| 128 | c->mode = LEN; |
|---|
| 129 | case LEN: /* i: get length/literal/eob next */ |
|---|
| 130 | j = c->sub.code.need; |
|---|
| 131 | NEEDBITS(j) |
|---|
| 132 | t = c->sub.code.tree + ((uInt)b & inflate_mask[j]); |
|---|
| 133 | DUMPBITS(t->bits) |
|---|
| 134 | e = (uInt)(t->exop); |
|---|
| 135 | if (e == 0) /* literal */ |
|---|
| 136 | { |
|---|
| 137 | c->sub.lit = t->base; |
|---|
| 138 | Tracevv((stderr, t->base >= 0x20 && t->base < 0x7f ? |
|---|
| 139 | "inflate: literal '%c'\n" : |
|---|
| 140 | "inflate: literal 0x%02x\n", t->base)); |
|---|
| 141 | c->mode = LIT; |
|---|
| 142 | break; |
|---|
| 143 | } |
|---|
| 144 | if (e & 16) /* length */ |
|---|
| 145 | { |
|---|
| 146 | c->sub.copy.get = e & 15; |
|---|
| 147 | c->len = t->base; |
|---|
| 148 | c->mode = LENEXT; |
|---|
| 149 | break; |
|---|
| 150 | } |
|---|
| 151 | if ((e & 64) == 0) /* next table */ |
|---|
| 152 | { |
|---|
| 153 | c->sub.code.need = e; |
|---|
| 154 | c->sub.code.tree = t + t->base; |
|---|
| 155 | break; |
|---|
| 156 | } |
|---|
| 157 | if (e & 32) /* end of block */ |
|---|
| 158 | { |
|---|
| 159 | Tracevv((stderr, "inflate: end of block\n")); |
|---|
| 160 | c->mode = WASH; |
|---|
| 161 | break; |
|---|
| 162 | } |
|---|
| 163 | c->mode = BADCODE; /* invalid code */ |
|---|
| 164 | z->msg = (char*)"invalid literal/length code"; |
|---|
| 165 | r = Z_DATA_ERROR; |
|---|
| 166 | LEAVE |
|---|
| 167 | case LENEXT: /* i: getting length extra (have base) */ |
|---|
| 168 | j = c->sub.copy.get; |
|---|
| 169 | NEEDBITS(j) |
|---|
| 170 | c->len += (uInt)b & inflate_mask[j]; |
|---|
| 171 | DUMPBITS(j) |
|---|
| 172 | c->sub.code.need = c->dbits; |
|---|
| 173 | c->sub.code.tree = c->dtree; |
|---|
| 174 | Tracevv((stderr, "inflate: length %u\n", c->len)); |
|---|
| 175 | c->mode = DIST; |
|---|
| 176 | case DIST: /* i: get distance next */ |
|---|
| 177 | j = c->sub.code.need; |
|---|
| 178 | NEEDBITS(j) |
|---|
| 179 | t = c->sub.code.tree + ((uInt)b & inflate_mask[j]); |
|---|
| 180 | DUMPBITS(t->bits) |
|---|
| 181 | e = (uInt)(t->exop); |
|---|
| 182 | if (e & 16) /* distance */ |
|---|
| 183 | { |
|---|
| 184 | c->sub.copy.get = e & 15; |
|---|
| 185 | c->sub.copy.dist = t->base; |
|---|
| 186 | c->mode = DISTEXT; |
|---|
| 187 | break; |
|---|
| 188 | } |
|---|
| 189 | if ((e & 64) == 0) /* next table */ |
|---|
| 190 | { |
|---|
| 191 | c->sub.code.need = e; |
|---|
| 192 | c->sub.code.tree = t + t->base; |
|---|
| 193 | break; |
|---|
| 194 | } |
|---|
| 195 | c->mode = BADCODE; /* invalid code */ |
|---|
| 196 | z->msg = (char*)"invalid distance code"; |
|---|
| 197 | r = Z_DATA_ERROR; |
|---|
| 198 | LEAVE |
|---|
| 199 | case DISTEXT: /* i: getting distance extra */ |
|---|
| 200 | j = c->sub.copy.get; |
|---|
| 201 | NEEDBITS(j) |
|---|
| 202 | c->sub.copy.dist += (uInt)b & inflate_mask[j]; |
|---|
| 203 | DUMPBITS(j) |
|---|
| 204 | Tracevv((stderr, "inflate: distance %u\n", c->sub.copy.dist)); |
|---|
| 205 | c->mode = COPY; |
|---|
| 206 | case COPY: /* o: copying bytes in window, waiting for space */ |
|---|
| 207 | #ifndef __TURBOC__ /* Turbo C bug for following expression */ |
|---|
| 208 | f = (uInt)(q - s->window) < c->sub.copy.dist ? |
|---|
| 209 | s->end - (c->sub.copy.dist - (q - s->window)) : |
|---|
| 210 | q - c->sub.copy.dist; |
|---|
| 211 | #else |
|---|
| 212 | f = q - c->sub.copy.dist; |
|---|
| 213 | if ((uInt)(q - s->window) < c->sub.copy.dist) |
|---|
| 214 | f = s->end - (c->sub.copy.dist - (uInt)(q - s->window)); |
|---|
| 215 | #endif |
|---|
| 216 | while (c->len) |
|---|
| 217 | { |
|---|
| 218 | NEEDOUT |
|---|
| 219 | OUTBYTE(*f++) |
|---|
| 220 | if (f == s->end) |
|---|
| 221 | f = s->window; |
|---|
| 222 | c->len--; |
|---|
| 223 | } |
|---|
| 224 | c->mode = START; |
|---|
| 225 | break; |
|---|
| 226 | case LIT: /* o: got literal, waiting for output space */ |
|---|
| 227 | NEEDOUT |
|---|
| 228 | OUTBYTE(c->sub.lit) |
|---|
| 229 | c->mode = START; |
|---|
| 230 | break; |
|---|
| 231 | case WASH: /* o: got eob, possibly more output */ |
|---|
| 232 | if (k > 7) /* return unused byte, if any */ |
|---|
| 233 | { |
|---|
| 234 | Assert(k < 16, "inflate_codes grabbed too many bytes") |
|---|
| 235 | k -= 8; |
|---|
| 236 | n++; |
|---|
| 237 | p--; /* can always return one */ |
|---|
| 238 | } |
|---|
| 239 | FLUSH |
|---|
| 240 | if (s->read != s->write) |
|---|
| 241 | LEAVE |
|---|
| 242 | c->mode = END; |
|---|
| 243 | case END: |
|---|
| 244 | r = Z_STREAM_END; |
|---|
| 245 | LEAVE |
|---|
| 246 | case BADCODE: /* x: got error */ |
|---|
| 247 | r = Z_DATA_ERROR; |
|---|
| 248 | LEAVE |
|---|
| 249 | default: |
|---|
| 250 | r = Z_STREAM_ERROR; |
|---|
| 251 | LEAVE |
|---|
| 252 | } |
|---|
| 253 | #ifdef NEED_DUMMY_RETURN |
|---|
| 254 | return Z_STREAM_ERROR; /* Some dumb compilers complain without this */ |
|---|
| 255 | #endif |
|---|
| 256 | } |
|---|
| 257 | |
|---|
| 258 | |
|---|
| 259 | void inflate_codes_free( |
|---|
| 260 | inflate_codes_statef *c, |
|---|
| 261 | z_streamp z) |
|---|
| 262 | { |
|---|
| 263 | ZFREE(z, c); |
|---|
| 264 | Tracev((stderr, "inflate: codes free\n")); |
|---|
| 265 | } |
|---|