00001
00002
00003
00004
00005
00006 #include "zutil.h"
00007 #include "inftrees.h"
00008
00009 #if !defined(BUILDFIXED) && !defined(STDC)
00010 # define BUILDFIXED
00011 #endif
00012
00013 const char inflate_copyright[] =
00014 " inflate 1.1.4 Copyright 1995-2002 Mark Adler ";
00015
00016
00017
00018
00019
00020
00021 struct internal_state {int dummy;};
00022
00023
00024 #define exop word.what.Exop
00025 #define bits word.what.Bits
00026
00027
00028 local int huft_build OF((
00029 uIntf *,
00030 uInt,
00031 uInt,
00032 const uIntf *,
00033 const uIntf *,
00034 inflate_huft * FAR*,
00035 uIntf *,
00036 inflate_huft *,
00037 uInt *,
00038 uIntf * ));
00039
00040
00041 local const uInt cplens[31] = {
00042 3, 4, 5, 6, 7, 8, 9, 10, 11, 13, 15, 17, 19, 23, 27, 31,
00043 35, 43, 51, 59, 67, 83, 99, 115, 131, 163, 195, 227, 258, 0, 0};
00044
00045 local const uInt cplext[31] = {
00046 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2,
00047 3, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5, 0, 112, 112};
00048 local const uInt cpdist[30] = {
00049 1, 2, 3, 4, 5, 7, 9, 13, 17, 25, 33, 49, 65, 97, 129, 193,
00050 257, 385, 513, 769, 1025, 1537, 2049, 3073, 4097, 6145,
00051 8193, 12289, 16385, 24577};
00052 local const uInt cpdext[30] = {
00053 0, 0, 0, 0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6,
00054 7, 7, 8, 8, 9, 9, 10, 10, 11, 11,
00055 12, 12, 13, 13};
00056
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066
00067
00068
00069
00070
00071
00072
00073
00074
00075
00076
00077
00078
00079
00080
00081
00082
00083
00084
00085
00086
00087
00088
00089
00090
00091 #define BMAX 15
00092
00093 local int huft_build(b, n, s, d, e, t, m, hp, hn, v)
00094 uIntf *b;
00095 uInt n;
00096 uInt s;
00097 const uIntf *d;
00098 const uIntf *e;
00099 inflate_huft * FAR *t;
00100 uIntf *m;
00101 inflate_huft *hp;
00102 uInt *hn;
00103 uIntf *v;
00104
00105
00106
00107
00108 {
00109
00110 uInt a;
00111 uInt c[BMAX+1];
00112 uInt f;
00113 int g;
00114 int h;
00115 register uInt i;
00116 register uInt j;
00117 register int k;
00118 int l;
00119 uInt mask;
00120 register uIntf *p;
00121 inflate_huft *q;
00122 struct inflate_huft_s r;
00123 inflate_huft *u[BMAX];
00124 register int w;
00125 uInt x[BMAX+1];
00126 uIntf *xp;
00127 int y;
00128 uInt z;
00129
00130
00131
00132 p = c;
00133 #define C0 *p++ = 0;
00134 #define C2 C0 C0 C0 C0
00135 #define C4 C2 C2 C2 C2
00136 C4
00137 p = b; i = n;
00138 do {
00139 c[*p++]++;
00140 } while (--i);
00141 if (c[0] == n)
00142 {
00143 *t = (inflate_huft *)Z_NULL;
00144 *m = 0;
00145 return Z_OK;
00146 }
00147
00148
00149
00150 l = *m;
00151 for (j = 1; j <= BMAX; j++)
00152 if (c[j])
00153 break;
00154 k = j;
00155 if ((uInt)l < j)
00156 l = j;
00157 for (i = BMAX; i; i--)
00158 if (c[i])
00159 break;
00160 g = i;
00161 if ((uInt)l > i)
00162 l = i;
00163 *m = l;
00164
00165
00166
00167 for (y = 1 << j; j < i; j++, y <<= 1)
00168 if ((y -= c[j]) < 0)
00169 return Z_DATA_ERROR;
00170 if ((y -= c[i]) < 0)
00171 return Z_DATA_ERROR;
00172 c[i] += y;
00173
00174
00175
00176 x[1] = j = 0;
00177 p = c + 1; xp = x + 2;
00178 while (--i) {
00179 *xp++ = (j += *p++);
00180 }
00181
00182
00183
00184 p = b; i = 0;
00185 do {
00186 if ((j = *p++) != 0)
00187 v[x[j]++] = i;
00188 } while (++i < n);
00189 n = x[g];
00190
00191
00192
00193 x[0] = i = 0;
00194 p = v;
00195 h = -1;
00196 w = -l;
00197 u[0] = (inflate_huft *)Z_NULL;
00198 q = (inflate_huft *)Z_NULL;
00199 z = 0;
00200
00201
00202 for (; k <= g; k++)
00203 {
00204 a = c[k];
00205 while (a--)
00206 {
00207
00208
00209 while (k > w + l)
00210 {
00211 h++;
00212 w += l;
00213
00214
00215 z = g - w;
00216 z = z > (uInt)l ? l : z;
00217 if ((f = 1 << (j = k - w)) > a + 1)
00218 {
00219 f -= a + 1;
00220 xp = c + k;
00221 if (j < z)
00222 while (++j < z)
00223 {
00224 if ((f <<= 1) <= *++xp)
00225 break;
00226 f -= *xp;
00227 }
00228 }
00229 z = 1 << j;
00230
00231
00232 if (*hn + z > MANY)
00233 return Z_DATA_ERROR;
00234 u[h] = q = hp + *hn;
00235 *hn += z;
00236
00237
00238 if (h)
00239 {
00240 x[h] = i;
00241 r.bits = (Byte)l;
00242 r.exop = (Byte)j;
00243 j = i >> (w - l);
00244 r.base = (uInt)(q - u[h-1] - j);
00245 u[h-1][j] = r;
00246 }
00247 else
00248 *t = q;
00249 }
00250
00251
00252 r.bits = (Byte)(k - w);
00253 if (p >= v + n)
00254 r.exop = 128 + 64;
00255 else if (*p < s)
00256 {
00257 r.exop = (Byte)(*p < 256 ? 0 : 32 + 64);
00258 r.base = *p++;
00259 }
00260 else
00261 {
00262 r.exop = (Byte)(e[*p - s] + 16 + 64);
00263 r.base = d[*p++ - s];
00264 }
00265
00266
00267 f = 1 << (k - w);
00268 for (j = i >> w; j < z; j += f)
00269 q[j] = r;
00270
00271
00272 for (j = 1 << (k - 1); i & j; j >>= 1)
00273 i ^= j;
00274 i ^= j;
00275
00276
00277 mask = (1 << w) - 1;
00278 while ((i & mask) != x[h])
00279 {
00280 h--;
00281 w -= l;
00282 mask = (1 << w) - 1;
00283 }
00284 }
00285 }
00286
00287
00288
00289 return y != 0 && g != 1 ? Z_BUF_ERROR : Z_OK;
00290 }
00291
00292
00293 int inflate_trees_bits(c, bb, tb, hp, z)
00294 uIntf *c;
00295 uIntf *bb;
00296 inflate_huft * FAR *tb;
00297 inflate_huft *hp;
00298 z_streamp z;
00299 {
00300 int r;
00301 uInt hn = 0;
00302 uIntf *v;
00303
00304 if ((v = (uIntf*)ZALLOC(z, 19, sizeof(uInt))) == Z_NULL)
00305 return Z_MEM_ERROR;
00306 r = huft_build(c, 19, 19, (uIntf*)Z_NULL, (uIntf*)Z_NULL,
00307 tb, bb, hp, &hn, v);
00308 if (r == Z_DATA_ERROR)
00309 z->msg = (char*)"oversubscribed dynamic bit lengths tree";
00310 else if (r == Z_BUF_ERROR || *bb == 0)
00311 {
00312 z->msg = (char*)"incomplete dynamic bit lengths tree";
00313 r = Z_DATA_ERROR;
00314 }
00315 ZFREE(z, v);
00316 return r;
00317 }
00318
00319
00320 int inflate_trees_dynamic(nl, nd, c, bl, bd, tl, td, hp, z)
00321 uInt nl;
00322 uInt nd;
00323 uIntf *c;
00324 uIntf *bl;
00325 uIntf *bd;
00326 inflate_huft * FAR *tl;
00327 inflate_huft * FAR *td;
00328 inflate_huft *hp;
00329 z_streamp z;
00330 {
00331 int r;
00332 uInt hn = 0;
00333 uIntf *v;
00334
00335
00336 if ((v = (uIntf*)ZALLOC(z, 288, sizeof(uInt))) == Z_NULL)
00337 return Z_MEM_ERROR;
00338
00339
00340 r = huft_build(c, nl, 257, cplens, cplext, tl, bl, hp, &hn, v);
00341 if (r != Z_OK || *bl == 0)
00342 {
00343 if (r == Z_DATA_ERROR)
00344 z->msg = (char*)"oversubscribed literal/length tree";
00345 else if (r != Z_MEM_ERROR)
00346 {
00347 z->msg = (char*)"incomplete literal/length tree";
00348 r = Z_DATA_ERROR;
00349 }
00350 ZFREE(z, v);
00351 return r;
00352 }
00353
00354
00355 r = huft_build(c + nl, nd, 0, cpdist, cpdext, td, bd, hp, &hn, v);
00356 if (r != Z_OK || (*bd == 0 && nl > 257))
00357 {
00358 if (r == Z_DATA_ERROR)
00359 z->msg = (char*)"oversubscribed distance tree";
00360 else if (r == Z_BUF_ERROR) {
00361 #ifdef PKZIP_BUG_WORKAROUND
00362 r = Z_OK;
00363 }
00364 #else
00365 z->msg = (char*)"incomplete distance tree";
00366 r = Z_DATA_ERROR;
00367 }
00368 else if (r != Z_MEM_ERROR)
00369 {
00370 z->msg = (char*)"empty distance tree with lengths";
00371 r = Z_DATA_ERROR;
00372 }
00373 ZFREE(z, v);
00374 return r;
00375 #endif
00376 }
00377
00378
00379 ZFREE(z, v);
00380 return Z_OK;
00381 }
00382
00383
00384
00385 #ifdef BUILDFIXED
00386 local int fixed_built = 0;
00387 #define FIXEDH 544
00388 local inflate_huft fixed_mem[FIXEDH];
00389 local uInt fixed_bl;
00390 local uInt fixed_bd;
00391 local inflate_huft *fixed_tl;
00392 local inflate_huft *fixed_td;
00393 #else
00394 #include "inffixed.h"
00395 #endif
00396
00397
00398 int inflate_trees_fixed(bl, bd, tl, td, z)
00399 uIntf *bl;
00400 uIntf *bd;
00401 inflate_huft * FAR *tl;
00402 inflate_huft * FAR *td;
00403 z_streamp z;
00404 {
00405 #ifdef BUILDFIXED
00406
00407 if (!fixed_built)
00408 {
00409 int k;
00410 uInt f = 0;
00411 uIntf *c;
00412 uIntf *v;
00413
00414
00415 if ((c = (uIntf*)ZALLOC(z, 288, sizeof(uInt))) == Z_NULL)
00416 return Z_MEM_ERROR;
00417 if ((v = (uIntf*)ZALLOC(z, 288, sizeof(uInt))) == Z_NULL)
00418 {
00419 ZFREE(z, c);
00420 return Z_MEM_ERROR;
00421 }
00422
00423
00424 for (k = 0; k < 144; k++)
00425 c[k] = 8;
00426 for (; k < 256; k++)
00427 c[k] = 9;
00428 for (; k < 280; k++)
00429 c[k] = 7;
00430 for (; k < 288; k++)
00431 c[k] = 8;
00432 fixed_bl = 9;
00433 huft_build(c, 288, 257, cplens, cplext, &fixed_tl, &fixed_bl,
00434 fixed_mem, &f, v);
00435
00436
00437 for (k = 0; k < 30; k++)
00438 c[k] = 5;
00439 fixed_bd = 5;
00440 huft_build(c, 30, 0, cpdist, cpdext, &fixed_td, &fixed_bd,
00441 fixed_mem, &f, v);
00442
00443
00444 ZFREE(z, v);
00445 ZFREE(z, c);
00446 fixed_built = 1;
00447 }
00448 #endif
00449 *bl = fixed_bl;
00450 *bd = fixed_bd;
00451 *tl = fixed_tl;
00452 *td = fixed_td;
00453 return Z_OK;
00454 }