| 1 |
/* |
| 2 |
* This file is a part of the zlib compression module for NSIS. |
| 3 |
* |
| 4 |
* Copyright and license information can be found below. |
| 5 |
* Modifications Copyright (C) 1999-2026 Nullsoft and Contributors |
| 6 |
* |
| 7 |
* The original zlib source code is available at |
| 8 |
* http://www.zlib.net/ |
| 9 |
* |
| 10 |
* This software is provided 'as-is', without any express or implied |
| 11 |
* warranty. |
| 12 |
* |
| 13 |
* Unicode support by Jim Park -- 08/27/2007 |
| 14 |
* All messages and true strings turned into TCHARs for when we |
| 15 |
* create viewable messages. |
| 16 |
*/ |
| 17 |
|
| 18 |
#include "../Platform.h" |
| 19 |
#include "../exehead/config.h" |
| 20 |
|
| 21 |
#include "ZUTIL.H" |
| 22 |
|
| 23 |
#ifndef min |
| 24 |
# define min(x,y) ((x<y)?x:y) |
| 25 |
#endif |
| 26 |
|
| 27 |
/* defines for inflate input/output */ |
| 28 |
/* update pointers and return */ |
| 29 |
#define UPDBITS {s->bitb=b;s->bitk=k;} |
| 30 |
#define UPDIN {z->avail_in=n;z->next_in=p;} |
| 31 |
#define UPDOUT {s->write=q;} |
| 32 |
#define UPDATE {UPDBITS UPDIN UPDOUT} |
| 33 |
#define LEAVE(r) {UPDATE inflate_flush(z); return r;} |
| 34 |
|
| 35 |
/* get bytes and bits */ |
| 36 |
#define LOADIN {p=z->next_in;n=z->avail_in;b=s->bitb;k=s->bitk;} |
| 37 |
|
| 38 |
|
| 39 |
#define NEEDBYTE {if(!n)LEAVE(Z_OK)} |
| 40 |
#define NEXTBYTE (n--,*p++) |
| 41 |
#define NEEDBITS(j) {while(k<(j)){NEEDBYTE;b|=((uLong)NEXTBYTE)<<k;k+=8;}} |
| 42 |
|
| 43 |
#define DUMPBITS(j) {b>>=(j);k-=(j);} |
| 44 |
/* output bytes */ |
| 45 |
#define WAVAIL (uInt)(q<s->read?s->read-q-1:s->end-q) |
| 46 |
#define LOADOUT {q=s->write;m=(uInt)WAVAIL;} |
| 47 |
#define WRAP {if(q==s->end&&s->read!=s->window){q=s->window;m=(uInt)WAVAIL;}} |
| 48 |
#define FLUSH {UPDOUT inflate_flush(z); LOADOUT} |
| 49 |
#define NEEDOUT {if(m==0){WRAP if(m==0){FLUSH WRAP if(m==0) LEAVE(Z_OK)}}} |
| 50 |
#define OUTBYTE(a) {*q++=(Byte)(a);m--;} |
| 51 |
/* load local pointers */ |
| 52 |
#define LOAD {LOADIN LOADOUT} |
| 53 |
|
| 54 |
#define LAST (s->last == DRY) |
| 55 |
|
| 56 |
#define FIXEDH 544 /* number of hufts used by fixed tables */ |
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
typedef struct inflate_blocks_state FAR inflate_blocks_statef; |
| 61 |
#define exop word.what.Exop |
| 62 |
#define bits word.what.Bits |
| 63 |
|
| 64 |
/* And'ing with mask[n] masks the lower n bits */ |
| 65 |
local unsigned short inflate_mask[17] = { |
| 66 |
0x0000, |
| 67 |
0x0001, 0x0003, 0x0007, 0x000f, 0x001f, 0x003f, 0x007f, 0x00ff, |
| 68 |
0x01ff, 0x03ff, 0x07ff, 0x0fff, 0x1fff, 0x3fff, 0x7fff, 0xffff |
| 69 |
}; // use to reduce .data #define INFLATE_MASK(x, n) (x & (~((unsigned short) 0xFFFF << n))) |
| 70 |
local const char border[] = { /* Order of the bit length code lengths */ |
| 71 |
16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15}; |
| 72 |
|
| 73 |
/* Tables for deflate from PKZIP's appnote.txt. */ |
| 74 |
local const unsigned short cplens[31] = { /* Copy lengths for literal codes 257..285 */ |
| 75 |
3, 4, 5, 6, 7, 8, 9, 10, 11, 13, 15, 17, 19, 23, 27, 31, |
| 76 |
35, 43, 51, 59, 67, 83, 99, 115, 131, 163, 195, 227, 258, 0, 0}; |
| 77 |
/* see note #13 above about 258 */ |
| 78 |
local const unsigned short cplext[31] = { /* Extra bits for literal codes 257..285 */ |
| 79 |
0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2, |
| 80 |
3, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5, 0, 112, 112}; /* 112==invalid */ |
| 81 |
local const unsigned short cpdist[30] = { /* Copy offsets for distance codes 0..29 */ |
| 82 |
1, 2, 3, 4, 5, 7, 9, 13, 17, 25, 33, 49, 65, 97, 129, 193, |
| 83 |
257, 385, 513, 769, 1025, 1537, 2049, 3073, 4097, 6145, |
| 84 |
8193, 12289, 16385, 24577}; |
| 85 |
local const unsigned short cpdext[30] = { /* Extra bits for distance codes */ |
| 86 |
0, 0, 0, 0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, |
| 87 |
7, 7, 8, 8, 9, 9, 10, 10, 11, 11, |
| 88 |
12, 12, 13, 13}; |
| 89 |
|
| 90 |
/* build fixed tables only once--keep them here */ |
| 91 |
local char fixed_built = 0; |
| 92 |
local inflate_huft fixed_mem[FIXEDH]; |
| 93 |
local uInt fixed_bl=9; |
| 94 |
local uInt fixed_bd=5; |
| 95 |
local inflate_huft *fixed_tl; |
| 96 |
local inflate_huft *fixed_td; |
| 97 |
|
| 98 |
/* copy as much as possible from the sliding window to the output area */ |
| 99 |
local void ZEXPORT inflate_flush(z) |
| 100 |
z_streamp z; |
| 101 |
{ |
| 102 |
inflate_blocks_statef *s = &z->blocks; |
| 103 |
uInt n; |
| 104 |
Bytef *q; |
| 105 |
|
| 106 |
/* local copies of source and destination pointers */ |
| 107 |
q = s->read; |
| 108 |
|
| 109 |
again: |
| 110 |
/* compute number of bytes to copy as far as end of window */ |
| 111 |
n = (uInt)((q <= s->write ? s->write : s->end) - q); |
| 112 |
n = min(n, z->avail_out); |
| 113 |
|
| 114 |
/* update counters */ |
| 115 |
z->avail_out -= n; |
| 116 |
//z->total_out += n; |
| 117 |
|
| 118 |
/* copy as far as end of window */ |
| 119 |
zmemcpy(z->next_out, q, n); |
| 120 |
z->next_out += n; |
| 121 |
q += n; |
| 122 |
|
| 123 |
/* see if more to copy at beginning of window */ |
| 124 |
if (q == s->end) |
| 125 |
{ |
| 126 |
/* wrap pointers */ |
| 127 |
q = s->window; |
| 128 |
if (s->write == s->end) |
| 129 |
s->write = s->window; |
| 130 |
|
| 131 |
/* do the same for the beginning of the window */ |
| 132 |
goto again; |
| 133 |
} |
| 134 |
|
| 135 |
/* update pointers */ |
| 136 |
s->read = q; |
| 137 |
} |
| 138 |
|
| 139 |
#define BMAX 15 /* maximum bit length of any code */ |
| 140 |
|
| 141 |
local int ZEXPORT huft_build( |
| 142 |
uIntf *b, /* code lengths in bits (all assumed <= BMAX) */ |
| 143 |
uInt n, /* number of codes (assumed <= 288) */ |
| 144 |
uInt s, /* number of simple-valued codes (0..s-1) */ |
| 145 |
const unsigned short *d, /* list of base values for non-simple codes */ |
| 146 |
const unsigned short *e, /* list of extra bits for non-simple codes */ |
| 147 |
inflate_huft * FAR *t, /* result: starting table */ |
| 148 |
uIntf *m, /* maximum lookup bits, returns actual */ |
| 149 |
inflate_huft *hp, /* space for trees */ |
| 150 |
uInt *hn) /* working area: values in order of bit length */ |
| 151 |
{ |
| 152 |
static uIntf v[288]; /* work area for huft_build */ |
| 153 |
uInt a; /* counter for codes of length k */ |
| 154 |
uInt c[BMAX+1]; /* bit length count table */ |
| 155 |
uInt f; /* i repeats in table every f entries */ |
| 156 |
int g; /* maximum code length */ |
| 157 |
int h; /* table level */ |
| 158 |
uInt i; /* counter, current code */ |
| 159 |
uInt j; /* counter */ |
| 160 |
int k; /* number of bits in current code */ |
| 161 |
int l; /* bits per table (returned in m) */ |
| 162 |
uIntf *p; /* pointer into c[], b[], or v[] */ |
| 163 |
inflate_huft *q; /* points to current table */ |
| 164 |
struct inflate_huft_s r; /* table entry for structure assignment */ |
| 165 |
inflate_huft *u[BMAX]; /* table stack */ |
| 166 |
int w; /* bits before this table == (l * h) */ |
| 167 |
uInt x[BMAX+1]; /* bit offsets, then code stack */ |
| 168 |
uIntf *xp; /* pointer into x */ |
| 169 |
int y; /* number of dummy codes added */ |
| 170 |
uInt z; /* number of entries in current table */ |
| 171 |
|
| 172 |
#ifdef __GNUC__ |
| 173 |
r.base = 0; // Avoid GCC "may be used uninitialized in this function" warning |
| 174 |
#endif |
| 175 |
|
| 176 |
/* Generate counts for each bit length */ |
| 177 |
p=c; |
| 178 |
y=16; while (y--) *p++ = 0; |
| 179 |
p = b; |
| 180 |
i = n; |
| 181 |
do { |
| 182 |
c[*p++]++; /* assume all entries <= BMAX */ |
| 183 |
} while (--i); |
| 184 |
if (c[0] == n) /* null input--all zero length codes */ |
| 185 |
{ |
| 186 |
*t = (inflate_huft *)Z_NULL; |
| 187 |
*m = 0; |
| 188 |
return Z_OK; |
| 189 |
} |
| 190 |
|
| 191 |
|
| 192 |
/* Find minimum and maximum length, bound *m by those */ |
| 193 |
l = *m; |
| 194 |
for (j = 1; j <= BMAX; j++) |
| 195 |
if (c[j]) |
| 196 |
break; |
| 197 |
k = j; /* minimum code length */ |
| 198 |
if ((uInt)l < j) |
| 199 |
l = j; |
| 200 |
for (i = BMAX; i; i--) |
| 201 |
if (c[i]) |
| 202 |
break; |
| 203 |
g = i; /* maximum code length */ |
| 204 |
if ((uInt)l > i) |
| 205 |
l = i; |
| 206 |
*m = l; |
| 207 |
|
| 208 |
|
| 209 |
/* Adjust last length count to fill out codes, if needed */ |
| 210 |
for (y = 1 << j; j < i; j++, y <<= 1) |
| 211 |
if ((y -= c[j]) < 0) |
| 212 |
return Z_DATA_ERROR; |
| 213 |
if ((y -= c[i]) < 0) |
| 214 |
return Z_DATA_ERROR; |
| 215 |
c[i] += y; |
| 216 |
|
| 217 |
|
| 218 |
/* Generate starting offsets into the value table for each length */ |
| 219 |
x[1] = j = 0; |
| 220 |
p = c + 1; xp = x + 2; |
| 221 |
while (--i) { /* note that i == g from above */ |
| 222 |
*xp++ = (j += *p++); |
| 223 |
} |
| 224 |
|
| 225 |
|
| 226 |
/* Make a table of values in order of bit lengths */ |
| 227 |
p = b; i = 0; |
| 228 |
do { |
| 229 |
if ((j = *p++) != 0) |
| 230 |
v[x[j]++] = i; |
| 231 |
} while (++i < n); |
| 232 |
n = x[g]; /* set n to length of v */ |
| 233 |
|
| 234 |
|
| 235 |
/* Generate the Huffman codes and for each, make the table entries */ |
| 236 |
x[0] = i = 0; /* first Huffman code is zero */ |
| 237 |
p = v; /* grab values in bit order */ |
| 238 |
h = -1; /* no tables yet--level -1 */ |
| 239 |
w = -l; /* bits decoded == (l * h) */ |
| 240 |
u[0] = (inflate_huft *)Z_NULL; /* just to keep compilers happy */ |
| 241 |
q = (inflate_huft *)Z_NULL; /* ditto */ |
| 242 |
z = 0; /* ditto */ |
| 243 |
|
| 244 |
/* go through the bit lengths (k already is bits in shortest code) */ |
| 245 |
for (; k <= g; k++) |
| 246 |
{ |
| 247 |
a = c[k]; |
| 248 |
while (a--) |
| 249 |
{ |
| 250 |
int nextw=w; |
| 251 |
/* here i is the Huffman code of length k bits for value *p */ |
| 252 |
/* make tables up to required level */ |
| 253 |
while (k > (nextw=w + l)) |
| 254 |
{ |
| 255 |
h++; |
| 256 |
|
| 257 |
/* compute minimum size table less than or equal to l bits */ |
| 258 |
z = g - nextw; |
| 259 |
z = z > (uInt)l ? l : z; /* table size upper limit */ |
| 260 |
if ((f = 1 << (j = k - nextw)) > a + 1) /* try a k-w bit table */ |
| 261 |
{ /* too few codes for k-w bit table */ |
| 262 |
f -= a + 1; /* deduct codes from patterns left */ |
| 263 |
xp = c + k; |
| 264 |
if (j < z) |
| 265 |
while (++j < z && (f <<= 1) > *++xp) /* try smaller tables up to z bits */ |
| 266 |
{ |
| 267 |
f -= *xp; /* else deduct codes from patterns */ |
| 268 |
} |
| 269 |
} |
| 270 |
z = 1 << j; /* table entries for j-bit table */ |
| 271 |
|
| 272 |
/* allocate new table */ |
| 273 |
if (*hn + z > MANY) /* (note: doesn't matter for fixed) */ |
| 274 |
return Z_MEM_ERROR; /* not enough memory */ |
| 275 |
u[h] = q = hp + *hn; |
| 276 |
*hn += z; |
| 277 |
|
| 278 |
/* connect to last table, if there is one */ |
| 279 |
if (h) |
| 280 |
{ |
| 281 |
x[h] = i; /* save pattern for backing up */ |
| 282 |
r.bits = (Byte)l; /* bits to dump before this table */ |
| 283 |
r.exop = (Byte)j; /* bits in this table */ |
| 284 |
j = i >> w; |
| 285 |
r.base = (uInt)(q - u[h-1] - j); /* offset to this table */ |
| 286 |
u[h-1][j] = r; /* connect to last table */ |
| 287 |
} |
| 288 |
else |
| 289 |
*t = q; /* first table is returned result */ |
| 290 |
w=nextw; /* previous table always l bits */ |
| 291 |
} |
| 292 |
|
| 293 |
/* set up table entry in r */ |
| 294 |
r.bits = (Byte)(k - w); |
| 295 |
if (p >= v + n) |
| 296 |
r.exop = 128 + 64; /* out of values--invalid code */ |
| 297 |
else if (*p < s) |
| 298 |
{ |
| 299 |
r.exop = (Byte)(*p < 256 ? 0 : 32 + 64); /* 256 is end-of-block */ |
| 300 |
r.base = *p++; /* simple code is just the value */ |
| 301 |
} |
| 302 |
else |
| 303 |
{ |
| 304 |
r.exop = (Byte)(e[*p - s] + 16 + 64);/* non-simple--look up in lists */ |
| 305 |
r.base = d[*p++ - s]; |
| 306 |
} |
| 307 |
|
| 308 |
/* fill code-like entries with r */ |
| 309 |
f = 1 << (k - w); |
| 310 |
for (j = i >> w; j < z; j += f) |
| 311 |
q[j] = r; |
| 312 |
|
| 313 |
/* backwards increment the k-bit code i */ |
| 314 |
for (j = 1 << (k - 1); i & j; j >>= 1) |
| 315 |
i ^= j; |
| 316 |
i ^= j; |
| 317 |
|
| 318 |
/* backup over finished tables */ |
| 319 |
while ((i & ((1 << w) - 1)) != x[h]) |
| 320 |
{ |
| 321 |
h--; /* don't need to update q */ |
| 322 |
w -= l; |
| 323 |
} |
| 324 |
} |
| 325 |
} |
| 326 |
|
| 327 |
|
| 328 |
/* Return Z_BUF_ERROR if we were given an incomplete table */ |
| 329 |
return (y != 0 && g != 1) ? Z_BUF_ERROR : Z_OK; |
| 330 |
} |
| 331 |
|
| 332 |
int ZEXPORT inflate(z_streamp z) |
| 333 |
{ |
| 334 |
inflate_blocks_statef *s = &z->blocks; |
| 335 |
inflate_codes_statef *c = &s->sub.decode.t_codes; /* codes state */ |
| 336 |
|
| 337 |
// lousy two bytes saved by doing this |
| 338 |
struct |
| 339 |
{ |
| 340 |
uInt t; /* temporary storage */ |
| 341 |
uLong b; /* bit buffer */ |
| 342 |
uInt k; /* bits in bit buffer */ |
| 343 |
Bytef *p; /* input data pointer */ |
| 344 |
uInt n; /* bytes available there */ |
| 345 |
Bytef *q; /* output window write pointer */ |
| 346 |
uInt m; /* bytes to end of window or read pointer */ |
| 347 |
|
| 348 |
/* CODES variables */ |
| 349 |
|
| 350 |
inflate_huft *j; /* temporary pointer */ |
| 351 |
uInt e; /* extra bits or operation */ |
| 352 |
Bytef *f; /* pointer to copy strings from */ |
| 353 |
} _state; |
| 354 |
|
| 355 |
#define t _state.t |
| 356 |
#define b _state.b |
| 357 |
#define k _state.k |
| 358 |
#define p _state.p |
| 359 |
#define n _state.n |
| 360 |
#define q _state.q |
| 361 |
#define m _state.m |
| 362 |
|
| 363 |
/* copy input/output information to locals (UPDATE macro restores) */ |
| 364 |
LOAD |
| 365 |
|
| 366 |
/* process input based on current state */ |
| 367 |
for (;;) switch (s->mode) |
| 368 |
{ |
| 369 |
case TYPE: |
| 370 |
NEEDBITS(3) |
| 371 |
t = (uInt)b & 7; |
| 372 |
DUMPBITS(3) |
| 373 |
s->last = (t & 1) ? DRY : TYPE; |
| 374 |
switch (t >> 1) |
| 375 |
{ |
| 376 |
case 0: /* stored */ |
| 377 |
Tracev((stderr, _T("inflate: stored block%s\n"), |
| 378 |
LAST ? _T(" (last)") : _T(""))); |
| 379 |
DUMPBITS(k&7) |
| 380 |
s->mode = LENS; /* get length of stored block */ |
| 381 |
break; |
| 382 |
case 1: /* fixed */ |
| 383 |
Tracev((stderr, _T("inflate: fixed codes block%s\n"), |
| 384 |
LAST ? _T(" (last)") : _T(""))); |
| 385 |
{ |
| 386 |
if (!fixed_built) |
| 387 |
{ |
| 388 |
int _k; /* temporary variable */ |
| 389 |
uInt f = 0; /* number of hufts used in fixed_mem */ |
| 390 |
static uIntf c[288]; /* length list for huft_build */ |
| 391 |
|
| 392 |
/* literal table */ |
| 393 |
for (_k = 0; _k < 288; _k++) |
| 394 |
{ |
| 395 |
char v=8; |
| 396 |
if (_k > 143) |
| 397 |
{ |
| 398 |
if (_k < 256) v++; |
| 399 |
else if (_k < 280) v--; |
| 400 |
} |
| 401 |
c[_k] = v; |
| 402 |
} |
| 403 |
|
| 404 |
huft_build(c, 288, 257, cplens, cplext, &fixed_tl, &fixed_bl, fixed_mem, &f); |
| 405 |
|
| 406 |
/* distance table */ |
| 407 |
for (_k = 0; _k < 30; _k++) c[_k] = 5; |
| 408 |
|
| 409 |
huft_build(c, 30, 0, cpdist, cpdext, &fixed_td, &fixed_bd, fixed_mem, &f); |
| 410 |
|
| 411 |
/* done */ |
| 412 |
fixed_built++; |
| 413 |
} |
| 414 |
|
| 415 |
//s->sub.decode.t_codes.mode = CODES_START; |
| 416 |
s->sub.decode.t_codes.lbits = (Byte)fixed_bl; |
| 417 |
s->sub.decode.t_codes.dbits = (Byte)fixed_bd; |
| 418 |
s->sub.decode.t_codes.ltree = fixed_tl; |
| 419 |
s->sub.decode.t_codes.dtree = fixed_td; |
| 420 |
} |
| 421 |
s->mode = CODES_START; |
| 422 |
break; |
| 423 |
case 2: /* dynamic */ |
| 424 |
Tracev((stderr, _T("inflate: dynamic codes block%s\n"), |
| 425 |
LAST ? _T(" (last)") : _T(""))); |
| 426 |
s->mode = TABLE; |
| 427 |
break; |
| 428 |
case 3: /* illegal */ |
| 429 |
/* the only illegal value possible is 3 because we check only 2 bits */ |
| 430 |
goto bad; |
| 431 |
} |
| 432 |
break; |
| 433 |
case LENS: |
| 434 |
NEEDBITS(32) |
| 435 |
s->sub.left = (uInt)b & 0xffff; |
| 436 |
b = k = 0; /* dump bits */ |
| 437 |
Tracev((stderr, _T("inflate: stored length %u\n"), s->sub.left)); |
| 438 |
s->mode = s->sub.left ? STORED : s->last; |
| 439 |
break; |
| 440 |
case STORED: |
| 441 |
{ |
| 442 |
uInt mn; |
| 443 |
|
| 444 |
if (n == 0) |
| 445 |
LEAVE(Z_OK) |
| 446 |
NEEDOUT |
| 447 |
mn = min(m, n); |
| 448 |
t = min(s->sub.left, mn); |
| 449 |
zmemcpy(q, p, t); |
| 450 |
p += t; n -= t; |
| 451 |
q += t; m -= t; |
| 452 |
if (!(s->sub.left -= t)) |
| 453 |
s->mode = s->last; |
| 454 |
break; |
| 455 |
} |
| 456 |
case TABLE: |
| 457 |
NEEDBITS(14) |
| 458 |
s->sub.trees.table = t = (uInt)b & 0x3fff; |
| 459 |
if ((t & 0x1f) > 29 || ((t >> 5) & 0x1f) > 29) |
| 460 |
{ |
| 461 |
s->mode = BAD; |
| 462 |
LEAVE(Z_DATA_ERROR); |
| 463 |
} |
| 464 |
//t = 258 + (t & 0x1f) + ((t >> 5) & 0x1f); |
| 465 |
DUMPBITS(14) |
| 466 |
s->sub.trees.index = 0; |
| 467 |
Tracev((stderr, _T("inflate: table sizes ok\n"))); |
| 468 |
s->mode = BTREE; |
| 469 |
case BTREE: |
| 470 |
while (s->sub.trees.index < 4 + (s->sub.trees.table >> 10)) |
| 471 |
{ |
| 472 |
NEEDBITS(3) |
| 473 |
s->sub.trees.t_blens[(int)border[s->sub.trees.index++]] = (uInt)b & 7; |
| 474 |
DUMPBITS(3) |
| 475 |
} |
| 476 |
while (s->sub.trees.index < 19) |
| 477 |
s->sub.trees.t_blens[(int)border[s->sub.trees.index++]] = 0; |
| 478 |
s->sub.trees.bb = 7; |
| 479 |
|
| 480 |
{ |
| 481 |
uInt hn = 0; /* hufts used in space */ |
| 482 |
|
| 483 |
t = huft_build(s->sub.trees.t_blens, 19, 19, (unsigned short*)Z_NULL, (unsigned short*)Z_NULL, |
| 484 |
&s->sub.trees.tb, &s->sub.trees.bb, s->hufts, &hn); |
| 485 |
if (t != Z_OK || !s->sub.trees.bb) |
| 486 |
{ |
| 487 |
s->mode = BAD; |
| 488 |
break; |
| 489 |
} |
| 490 |
} |
| 491 |
|
| 492 |
s->sub.trees.index = 0; |
| 493 |
Tracev((stderr, _T("inflate: bits tree ok\n"))); |
| 494 |
s->mode = DTREE; |
| 495 |
case DTREE: |
| 496 |
while (t = s->sub.trees.table, |
| 497 |
s->sub.trees.index < 258 + (t & 0x1f) + ((t >> 5) & 0x1f)) |
| 498 |
{ |
| 499 |
inflate_huft *h; |
| 500 |
uInt i, j, c; |
| 501 |
|
| 502 |
t = s->sub.trees.bb; |
| 503 |
NEEDBITS(t) |
| 504 |
h = s->sub.trees.tb + ((uInt)b & (uInt)inflate_mask[t]); |
| 505 |
t = h->bits; |
| 506 |
c = h->base; |
| 507 |
if (c < 16) |
| 508 |
{ |
| 509 |
DUMPBITS(t) |
| 510 |
s->sub.trees.t_blens[s->sub.trees.index++] = c; |
| 511 |
} |
| 512 |
else /* c == 16..18 */ |
| 513 |
{ |
| 514 |
if (c == 18) |
| 515 |
{ |
| 516 |
i=7; |
| 517 |
j=11; |
| 518 |
} |
| 519 |
else |
| 520 |
{ |
| 521 |
i=c-14; |
| 522 |
j=3; |
| 523 |
} |
| 524 |
NEEDBITS(t+i) |
| 525 |
DUMPBITS(t) |
| 526 |
j += (uInt)b & (uInt)inflate_mask[i]; |
| 527 |
DUMPBITS(i) |
| 528 |
i = s->sub.trees.index; |
| 529 |
t = s->sub.trees.table; |
| 530 |
if (i + j > 258 + (t & 0x1f) + ((t >> 5) & 0x1f) || |
| 531 |
(c == 16 && i < 1)) |
| 532 |
{ |
| 533 |
s->mode = BAD; |
| 534 |
LEAVE(Z_DATA_ERROR); |
| 535 |
} |
| 536 |
c = c == 16 ? s->sub.trees.t_blens[i - 1] : 0; |
| 537 |
do { |
| 538 |
s->sub.trees.t_blens[i++] = c; |
| 539 |
} while (--j); |
| 540 |
s->sub.trees.index = i; |
| 541 |
} |
| 542 |
} |
| 543 |
s->sub.trees.tb = Z_NULL; |
| 544 |
{ |
| 545 |
uInt hn = 0; /* hufts used in space */ |
| 546 |
uInt bl, bd; |
| 547 |
inflate_huft *tl, *td; |
| 548 |
int nl,nd; |
| 549 |
t = s->sub.trees.table; |
| 550 |
|
| 551 |
nl = 257 + (t & 0x1f); |
| 552 |
nd = 1 + ((t >> 5) & 0x1f); |
| 553 |
bl = 9; /* must be <= 9 for lookahead assumptions */ |
| 554 |
bd = 6; /* must be <= 9 for lookahead assumptions */ |
| 555 |
|
| 556 |
t = huft_build(s->sub.trees.t_blens, nl, 257, cplens, cplext, &tl, &bl, s->hufts, &hn); |
| 557 |
if (bl == 0) t = Z_DATA_ERROR; |
| 558 |
if (t == Z_OK) |
| 559 |
{ |
| 560 |
/* build distance tree */ |
| 561 |
t = huft_build(s->sub.trees.t_blens + nl, nd, 0, cpdist, cpdext, &td, &bd, s->hufts, &hn); |
| 562 |
} |
| 563 |
if (t != Z_OK || (bd == 0 && nl > 257)) |
| 564 |
{ |
| 565 |
s->mode = BAD; |
| 566 |
LEAVE(Z_DATA_ERROR); |
| 567 |
} |
| 568 |
Tracev((stderr, _T("inflate: trees ok\n"))); |
| 569 |
|
| 570 |
//s->sub.decode.t_codes.mode = CODES_START; |
| 571 |
s->sub.decode.t_codes.lbits = (Byte)bl; |
| 572 |
s->sub.decode.t_codes.dbits = (Byte)bd; |
| 573 |
s->sub.decode.t_codes.ltree = tl; |
| 574 |
s->sub.decode.t_codes.dtree = td; |
| 575 |
} |
| 576 |
s->mode = CODES_START; |
| 577 |
|
| 578 |
#define j (_state.j) |
| 579 |
#define e (_state.e) |
| 580 |
#define f (_state.f) |
| 581 |
|
| 582 |
/* waiting for "i:"=input, "o:"=output, "x:"=nothing */ |
| 583 |
|
| 584 |
case CODES_START: /* x: set up for LEN */ |
| 585 |
c->sub.code.need = c->lbits; |
| 586 |
c->sub.code.tree = c->ltree; |
| 587 |
s->mode = CODES_LEN; |
| 588 |
case CODES_LEN: /* i: get length/literal/eob next */ |
| 589 |
t = c->sub.code.need; |
| 590 |
NEEDBITS(t) |
| 591 |
j = c->sub.code.tree + ((uInt)b & (uInt)inflate_mask[t]); |
| 592 |
DUMPBITS(j->bits) |
| 593 |
e = (uInt)(j->exop); |
| 594 |
if (e == 0) /* literal */ |
| 595 |
{ |
| 596 |
c->sub.lit = j->base; |
| 597 |
s->mode = CODES_LIT; |
| 598 |
break; |
| 599 |
} |
| 600 |
if (e & 16) /* length */ |
| 601 |
{ |
| 602 |
c->sub.copy.get = e & 15; |
| 603 |
c->len = j->base; |
| 604 |
s->mode = CODES_LENEXT; |
| 605 |
break; |
| 606 |
} |
| 607 |
if ((e & 64) == 0) /* next table */ |
| 608 |
{ |
| 609 |
c->sub.code.need = e; |
| 610 |
c->sub.code.tree = j + j->base; |
| 611 |
break; |
| 612 |
} |
| 613 |
if (e & 32) /* end of block */ |
| 614 |
{ |
| 615 |
s->mode = CODES_WASH; |
| 616 |
break; |
| 617 |
} |
| 618 |
goto bad; |
| 619 |
case CODES_LENEXT: /* i: getting length extra (have base) */ |
| 620 |
t = c->sub.copy.get; |
| 621 |
NEEDBITS(t) |
| 622 |
c->len += (uInt)b & (uInt)inflate_mask[t]; |
| 623 |
DUMPBITS(t) |
| 624 |
c->sub.code.need = c->dbits; |
| 625 |
c->sub.code.tree = c->dtree; |
| 626 |
s->mode = CODES_DIST; |
| 627 |
case CODES_DIST: /* i: get distance next */ |
| 628 |
t = c->sub.code.need; |
| 629 |
NEEDBITS(t) |
| 630 |
j = c->sub.code.tree + ((uInt)b & (uInt)inflate_mask[t]); |
| 631 |
DUMPBITS(j->bits) |
| 632 |
e = (uInt)(j->exop); |
| 633 |
if (e & 16) /* distance */ |
| 634 |
{ |
| 635 |
c->sub.copy.get = e & 15; |
| 636 |
c->sub.copy.dist = j->base; |
| 637 |
s->mode = CODES_DISTEXT; |
| 638 |
break; |
| 639 |
} |
| 640 |
if ((e & 64) == 0) /* next table */ |
| 641 |
{ |
| 642 |
c->sub.code.need = e; |
| 643 |
c->sub.code.tree = j + j->base; |
| 644 |
break; |
| 645 |
} |
| 646 |
goto bad; /* invalid code */ |
| 647 |
case CODES_DISTEXT: /* i: getting distance extra */ |
| 648 |
t = c->sub.copy.get; |
| 649 |
NEEDBITS(t) |
| 650 |
c->sub.copy.dist += (uInt)b & (uInt)inflate_mask[t]; |
| 651 |
DUMPBITS(t) |
| 652 |
s->mode = CODES_COPY; |
| 653 |
case CODES_COPY: /* o: copying bytes in window, waiting for space */ |
| 654 |
f = (uInt)(q - s->window) < c->sub.copy.dist ? |
| 655 |
s->end - (c->sub.copy.dist - (q - s->window)) : |
| 656 |
q - c->sub.copy.dist; |
| 657 |
|
| 658 |
while (c->len) |
| 659 |
{ |
| 660 |
NEEDOUT |
| 661 |
OUTBYTE(*f++) |
| 662 |
if (f == s->end) |
| 663 |
f = s->window; |
| 664 |
c->len--; |
| 665 |
} |
| 666 |
s->mode = CODES_START; |
| 667 |
break; |
| 668 |
case CODES_LIT: /* o: got literal, waiting for output space */ |
| 669 |
NEEDOUT |
| 670 |
OUTBYTE(c->sub.lit) |
| 671 |
s->mode = CODES_START; |
| 672 |
break; |
| 673 |
case CODES_WASH: /* o: got eob, possibly more output */ |
| 674 |
if (k > 7) /* return unused byte, if any */ |
| 675 |
{ |
| 676 |
k -= 8; |
| 677 |
n++; |
| 678 |
p--; /* can always return one */ |
| 679 |
} |
| 680 |
/* flushing will be done in DRY */ |
| 681 |
|
| 682 |
#undef j |
| 683 |
#undef e |
| 684 |
#undef f |
| 685 |
|
| 686 |
case DRY: |
| 687 |
FLUSH |
| 688 |
if (s->write != s->read) |
| 689 |
LEAVE(Z_OK) |
| 690 |
if (s->mode == CODES_WASH) |
| 691 |
{ |
| 692 |
Tracev((stderr, _T("inflate: codes end, %lu total out\n"), |
| 693 |
z->total_out + (q >= s->read ? q - s->read : |
| 694 |
(s->end - s->read) + (q - s->window)))); |
| 695 |
} |
| 696 |
/* DRY if last, TYPE if not */ |
| 697 |
s->mode = s->last; |
| 698 |
if (s->mode == TYPE) |
| 699 |
break; |
| 700 |
LEAVE(Z_STREAM_END) |
| 701 |
//case BAD: |
| 702 |
//r = Z_DATA_ERROR; |
| 703 |
//LEAVE |
| 704 |
default: // we'll call Z_STREAM_ERROR if BAD anyway |
| 705 |
bad: |
| 706 |
s->mode = BAD; |
| 707 |
LEAVE(Z_STREAM_ERROR) |
| 708 |
} |
| 709 |
} |
| 710 |
|
| 711 |
#undef t |
| 712 |
#undef b |
| 713 |
#undef k |
| 714 |
#undef p |
| 715 |
#undef n |
| 716 |
#undef q |
| 717 |
#undef m |