2
* This file is a part of the zlib compression module for NSIS.
4
* Copyright and license information can be found below.
5
* Modifications Copyright (C) 1999-2007 Nullsoft and Contributors
7
* The original zlib source code is available at
10
* This software is provided 'as-is', without any express or implied
15
* Copyright (C) 1995-1998 Jean-loup Gailly.
16
* For conditions of distribution and use, see copyright notice in COPYING.nsis
19
#include "nsis_zutil.h"
23
# define min(x,y) ((x<y)?x:y)
26
/* defines for inflate input/output */
27
/* update pointers and return */
28
#define UPDBITS {s->bitb=b;s->bitk=k;}
29
#define UPDIN {z->avail_in=n;z->next_in=p;}
30
#define UPDOUT {s->write=q;}
31
#define UPDATE {UPDBITS UPDIN UPDOUT}
32
#define LEAVE(r) {UPDATE inflate_flush(z); return r;}
34
/* get bytes and bits */
35
#define LOADIN {p=z->next_in;n=z->avail_in;b=s->bitb;k=s->bitk;}
38
#define NEEDBYTE {if(!n)LEAVE(Z_OK)}
39
#define NEXTBYTE (n--,*p++)
40
#define NEEDBITS(j) {while(k<(j)){NEEDBYTE;b|=((uLong)NEXTBYTE)<<k;k+=8;}}
42
#define DUMPBITS(j) {b>>=(j);k-=(j);}
44
#define WAVAIL (uInt)(q<s->read?s->read-q-1:s->end-q)
45
#define LOADOUT {q=s->write;m=(uInt)WAVAIL;}
46
#define WRAP {if(q==s->end&&s->read!=s->window){q=s->window;m=(uInt)WAVAIL;}}
47
#define FLUSH {UPDOUT inflate_flush(z); LOADOUT}
48
#define NEEDOUT {if(m==0){WRAP if(m==0){FLUSH WRAP if(m==0) LEAVE(Z_OK)}}}
49
#define OUTBYTE(a) {*q++=(Byte)(a);m--;}
50
/* load local pointers */
51
#define LOAD {LOADIN LOADOUT}
53
#define LAST (s->last == DRY)
56
typedef struct inflate_blocks_state FAR inflate_blocks_statef;
57
#define exop word.what.Exop
58
#define bits word.what.Bits
60
/* And'ing with mask[n] masks the lower n bits */
61
local const unsigned short inflate_mask[17] = {
63
0x0001, 0x0003, 0x0007, 0x000f, 0x001f, 0x003f, 0x007f, 0x00ff,
64
0x01ff, 0x03ff, 0x07ff, 0x0fff, 0x1fff, 0x3fff, 0x7fff, 0xffff
65
}; /* use to reduce .data #define INFLATE_MASK(x, n) (x & (~((unsigned short) 0xFFFF << n))) */
66
local const char border[] = { /* Order of the bit length code lengths */
67
16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15};
69
/* Tables for deflate from PKZIP's appnote.txt. */
70
local const unsigned short cplens[31] = { /* Copy lengths for literal codes 257..285 */
71
3, 4, 5, 6, 7, 8, 9, 10, 11, 13, 15, 17, 19, 23, 27, 31,
72
35, 43, 51, 59, 67, 83, 99, 115, 131, 163, 195, 227, 258, 0, 0};
73
/* see note #13 above about 258 */
74
local const unsigned short cplext[31] = { /* Extra bits for literal codes 257..285 */
75
0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2,
76
3, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5, 0, 112, 112}; /* 112==invalid */
77
local const unsigned short cpdist[30] = { /* Copy offsets for distance codes 0..29 */
78
1, 2, 3, 4, 5, 7, 9, 13, 17, 25, 33, 49, 65, 97, 129, 193,
79
257, 385, 513, 769, 1025, 1537, 2049, 3073, 4097, 6145,
80
8193, 12289, 16385, 24577};
81
local const unsigned short cpdext[30] = { /* Extra bits for distance codes */
82
0, 0, 0, 0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6,
83
7, 7, 8, 8, 9, 9, 10, 10, 11, 11,
86
/* build fixed tables only once--keep them here */
87
/* local char fixed_built = 0; */
88
/* local inflate_huft fixed_mem[FIXEDH]; */
89
/* local uInt fixed_bl=9; */
90
/* local uInt fixed_bd=5; */
91
/* local inflate_huft *fixed_tl; */
92
/* local inflate_huft *fixed_td; */
95
/* copy as much as possible from the sliding window to the output area */
96
local void ZEXPORT inflate_flush(nsis_z_streamp z)
98
inflate_blocks_statef *s = &z->blocks;
102
/* local copies of source and destination pointers */
106
/* compute number of bytes to copy as far as end of window */
107
n = (uInt)((q <= s->write ? s->write : s->end) - q);
108
n = min(n, z->avail_out);
110
/* update counters */
112
/* z->total_out += n; */
114
/* copy as far as end of window */
115
zmemcpy(z->next_out, q, n);
119
/* see if more to copy at beginning of window */
124
if (s->write == s->end)
125
s->write = s->window;
127
/* do the same for the beginning of the window */
131
/* update pointers */
135
#define BMAX 15 /* maximum bit length of any code */
137
local int ZEXPORT huft_build(
138
uIntf *b, /* code lengths in bits (all assumed <= BMAX) */
139
uInt n, /* number of codes (assumed <= 288) */
140
uInt s, /* number of simple-valued codes (0..s-1) */
141
const unsigned short *d, /* list of base values for non-simple codes */
142
const unsigned short *e, /* list of extra bits for non-simple codes */
143
inflate_huft * FAR *t, /* result: starting table */
144
uIntf *m, /* maximum lookup bits, returns actual */
145
inflate_huft *hp, /* space for trees */
146
uInt *hn, /* working area: values in order of bit length */
147
uIntf *v) /* work area for huft_build */
149
uInt a; /* counter for codes of length k */
150
uInt c[BMAX+1]; /* bit length count table */
151
uInt f; /* i repeats in table every f entries */
152
int g; /* maximum code length */
153
int h; /* table level */
154
uInt i; /* counter, current code */
155
uInt j; /* counter */
156
int k; /* number of bits in current code */
157
int l; /* bits per table (returned in m) */
158
uIntf *p; /* pointer into c[], b[], or v[] */
159
inflate_huft *q; /* points to current table */
160
struct inflate_huft_s r; /* table entry for structure assignment */
161
inflate_huft *u[BMAX]; /* table stack */
162
int w; /* bits before this table == (l * h) */
163
uInt x[BMAX+1]; /* bit offsets, then code stack */
164
uIntf *xp; /* pointer into x */
165
int y; /* number of dummy codes added */
166
uInt z; /* number of entries in current table */
169
/* Generate counts for each bit length */
171
y=16; while (y--) *p++ = 0;
175
c[*p++]++; /* assume all entries <= BMAX */
177
if (c[0] == n) /* null input--all zero length codes */
179
*t = (inflate_huft *)Z_NULL;
185
/* Find minimum and maximum length, bound *m by those */
187
for (j = 1; j <= BMAX; j++)
190
k = j; /* minimum code length */
193
for (i = BMAX; i; i--)
196
g = i; /* maximum code length */
202
/* Adjust last length count to fill out codes, if needed */
203
for (y = 1 << j; j < i; j++, y <<= 1)
211
/* Generate starting offsets into the value table for each length */
213
p = c + 1; xp = x + 2;
214
while (--i) { /* note that i == g from above */
219
/* Make a table of values in order of bit lengths */
225
n = x[g]; /* set n to length of v */
228
/* Generate the Huffman codes and for each, make the table entries */
229
x[0] = i = 0; /* first Huffman code is zero */
230
p = v; /* grab values in bit order */
231
h = -1; /* no tables yet--level -1 */
232
w = -l; /* bits decoded == (l * h) */
233
u[0] = (inflate_huft *)Z_NULL; /* just to keep compilers happy */
234
q = (inflate_huft *)Z_NULL; /* ditto */
239
/* go through the bit lengths (k already is bits in shortest code) */
246
/* here i is the Huffman code of length k bits for value *p */
247
/* make tables up to required level */
248
while (k > (nextw=w + l))
252
/* compute minimum size table less than or equal to l bits */
254
z = z > (uInt)l ? (uInt)l : z; /* table size upper limit */
255
if ((f = 1 << (j = k - nextw)) > a + 1) /* try a k-w bit table */
256
{ /* too few codes for k-w bit table */
257
f -= a + 1; /* deduct codes from patterns left */
260
while (++j < z && (f <<= 1) > *++xp) /* try smaller tables up to z bits */
262
f -= *xp; /* else deduct codes from patterns */
265
z = 1 << j; /* table entries for j-bit table */
267
/* allocate new table */
268
if (*hn + z > MANY) /* (note: doesn't matter for fixed) */
269
return Z_MEM_ERROR; /* not enough memory */
273
/* connect to last table, if there is one */
276
x[h] = i; /* save pattern for backing up */
277
r.bits = (Byte)l; /* bits to dump before this table */
278
r.exop = (Byte)j; /* bits in this table */
280
r.base = (uInt)(q - u[h-1] - j); /* offset to this table */
281
u[h-1][j] = r; /* connect to last table */
284
*t = q; /* first table is returned result */
285
w=nextw; /* previous table always l bits */
288
/* set up table entry in r */
289
r.bits = (Byte)(k - w);
291
r.exop = 128 + 64; /* out of values--invalid code */
294
r.exop = (Byte)(*p < 256 ? 0 : 32 + 64); /* 256 is end-of-block */
295
r.base = *p++; /* simple code is just the value */
299
r.exop = (Byte)(e[*p - s] + 16 + 64);/* non-simple--look up in lists */
300
r.base = d[*p++ - s];
303
/* fill code-like entries with r */
305
for (j = i >> w; j < z; j += f)
308
/* backwards increment the k-bit code i */
309
for (j = 1 << (k - 1); i & j; j >>= 1)
313
/* backup over finished tables */
314
while ((i & ((1 << w) - 1)) != x[h])
316
h--; /* don't need to update q */
323
/* Return Z_BUF_ERROR if we were given an incomplete table */
324
return (y != 0 && g != 1) ? Z_BUF_ERROR : Z_OK;
327
int ZEXPORT nsis_inflate(nsis_z_streamp z)
329
inflate_blocks_statef *s = &z->blocks;
330
inflate_codes_statef *c = &s->sub.decode.t_codes; /* codes state */
332
/* lousy two bytes saved by doing this */
335
uInt t; /* temporary storage */
336
uLong b; /* bit buffer */
337
uInt k; /* bits in bit buffer */
338
Bytef *p; /* input data pointer */
339
uInt n; /* bytes available there */
340
Bytef *q; /* output window write pointer */
341
uInt m; /* bytes to end of window or read pointer */
343
/* CODES variables */
345
inflate_huft *j; /* temporary pointer */
346
uInt e; /* extra bits or operation */
347
Bytef *f; /* pointer to copy strings from */
358
/* copy input/output information to locals (UPDATE macro restores) */
361
/* process input based on current state */
362
for (;;) switch (s->mode)
368
s->last = (t & 1) ? DRY : TYPE;
372
Tracev((stderr, "inflate: stored block%s\n",
373
LAST ? " (last)" : ""));
375
s->mode = LENS; /* get length of stored block */
378
Tracev((stderr, "inflate: fixed codes block%s\n",
379
LAST ? " (last)" : ""));
381
if (!s->zs.fixed_built)
383
int _k; /* temporary variable */
384
uInt f = 0; /* number of hufts used in fixed_mem */
387
for (_k = 0; _k < 288; _k++)
393
else if (_k < 280) v--;
398
huft_build(s->zs.lc, 288, 257, cplens, cplext, &s->zs.fixed_tl, &s->zs.fixed_bl, s->zs.fixed_mem, &f, s->zs.v);
401
for (_k = 0; _k < 30; _k++) s->zs.lc[_k] = 5;
403
huft_build(s->zs.lc, 30, 0, cpdist, cpdext, &s->zs.fixed_td, &s->zs.fixed_bd, s->zs.fixed_mem, &f, s->zs.v);
409
/* s->sub.decode.t_codes.mode = CODES_START; */
410
s->sub.decode.t_codes.lbits = (Byte)s->zs.fixed_bl;
411
s->sub.decode.t_codes.dbits = (Byte)s->zs.fixed_bd;
412
s->sub.decode.t_codes.ltree = s->zs.fixed_tl;
413
s->sub.decode.t_codes.dtree = s->zs.fixed_td;
415
s->mode = CODES_START;
417
case 2: /* dynamic */
418
Tracev((stderr, "inflate: dynamic codes block%s\n",
419
LAST ? " (last)" : ""));
422
case 3: /* illegal */
423
/* the only illegal value possible is 3 because we check only 2 bits */
429
s->sub.left = (uInt)b & 0xffff;
430
b = k = 0; /* dump bits */
431
Tracev((stderr, "inflate: stored length %u\n", s->sub.left));
432
s->mode = s->sub.left ? STORED : (inflate_mode)s->last;
442
t = min(s->sub.left, mn);
446
if (!(s->sub.left -= t))
447
s->mode = (inflate_mode)s->last;
452
s->sub.trees.table = t = (uInt)b & 0x3fff;
453
if ((t & 0x1f) > 29 || ((t >> 5) & 0x1f) > 29)
458
/* t = 258 + (t & 0x1f) + ((t >> 5) & 0x1f); */
460
s->sub.trees.index = 0;
461
Tracev((stderr, "inflate: table sizes ok\n"));
464
while (s->sub.trees.index < 4 + (s->sub.trees.table >> 10))
467
s->sub.trees.t_blens[(int)border[s->sub.trees.index++]] = (uInt)b & 7;
470
while (s->sub.trees.index < 19)
471
s->sub.trees.t_blens[(int)border[s->sub.trees.index++]] = 0;
475
uInt hn = 0; /* hufts used in space */
477
t = huft_build(s->sub.trees.t_blens, 19, 19, Z_NULL, Z_NULL,
478
&s->sub.trees.tb, &s->sub.trees.bb, s->hufts, &hn, s->zs.v);
479
if (t != Z_OK || !s->sub.trees.bb)
486
s->sub.trees.index = 0;
487
Tracev((stderr, "inflate: bits tree ok\n"));
490
while (t = s->sub.trees.table,
491
s->sub.trees.index < 258 + (t & 0x1f) + ((t >> 5) & 0x1f))
498
h = s->sub.trees.tb + ((uInt)b & (uInt)inflate_mask[t]);
504
s->sub.trees.t_blens[s->sub.trees.index++] = d;
506
else /* d == 16..18 */
520
j += (uInt)b & (uInt)inflate_mask[i];
522
i = s->sub.trees.index;
523
t = s->sub.trees.table;
524
if (i + j > 258 + (t & 0x1f) + ((t >> 5) & 0x1f) ||
530
d = d == 16 ? s->sub.trees.t_blens[i - 1] : 0;
532
s->sub.trees.t_blens[i++] = d;
534
s->sub.trees.index = i;
537
s->sub.trees.tb = Z_NULL;
539
uInt hn = 0; /* hufts used in space */
541
inflate_huft *tl, *td;
543
t = s->sub.trees.table;
545
nl = 257 + (t & 0x1f);
546
nd = 1 + ((t >> 5) & 0x1f);
547
bl = 9; /* must be <= 9 for lookahead assumptions */
548
bd = 6; /* must be <= 9 for lookahead assumptions */
550
t = huft_build(s->sub.trees.t_blens, nl, 257, cplens, cplext, &tl, &bl, s->hufts, &hn, s->zs.v);
551
if (bl == 0) t = Z_DATA_ERROR;
554
/* build distance tree */
555
t = huft_build(s->sub.trees.t_blens + nl, nd, 0, cpdist, cpdext, &td, &bd, s->hufts, &hn, s->zs.v);
557
if (t != Z_OK || (bd == 0 && nl > 257))
562
Tracev((stderr, "inflate: trees ok\n"));
564
/* s->sub.decode.t_codes.mode = CODES_START; */
565
s->sub.decode.t_codes.lbits = (Byte)bl;
566
s->sub.decode.t_codes.dbits = (Byte)bd;
567
s->sub.decode.t_codes.ltree = tl;
568
s->sub.decode.t_codes.dtree = td;
570
s->mode = CODES_START;
576
/* waiting for "i:"=input, "o:"=output, "x:"=nothing */
578
case CODES_START: /* x: set up for LEN */
579
c->sub.code.need = c->lbits;
580
c->sub.code.tree = c->ltree;
582
case CODES_LEN: /* i: get length/literal/eob next */
583
t = c->sub.code.need;
585
j = c->sub.code.tree + ((uInt)b & (uInt)inflate_mask[t]);
588
if (e == 0) /* literal */
590
c->sub.lit = j->base;
594
if (e & 16) /* length */
596
c->sub.copy.get = e & 15;
598
s->mode = CODES_LENEXT;
601
if ((e & 64) == 0) /* next table */
603
c->sub.code.need = e;
604
c->sub.code.tree = j + j->base;
607
if (e & 32) /* end of block */
609
s->mode = CODES_WASH;
613
case CODES_LENEXT: /* i: getting length extra (have base) */
616
c->len += (uInt)b & (uInt)inflate_mask[t];
618
c->sub.code.need = c->dbits;
619
c->sub.code.tree = c->dtree;
620
s->mode = CODES_DIST;
621
case CODES_DIST: /* i: get distance next */
622
t = c->sub.code.need;
624
j = c->sub.code.tree + ((uInt)b & (uInt)inflate_mask[t]);
627
if (e & 16) /* distance */
629
c->sub.copy.get = e & 15;
630
c->sub.copy.dist = j->base;
631
s->mode = CODES_DISTEXT;
634
if ((e & 64) == 0) /* next table */
636
c->sub.code.need = e;
637
c->sub.code.tree = j + j->base;
640
goto bad; /* invalid code */
641
case CODES_DISTEXT: /* i: getting distance extra */
644
c->sub.copy.dist += (uInt)b & (uInt)inflate_mask[t];
646
s->mode = CODES_COPY;
647
case CODES_COPY: /* o: copying bytes in window, waiting for space */
648
f = (uInt)(q - s->window) < c->sub.copy.dist ?
649
s->end - (c->sub.copy.dist - (q - s->window)) :
650
q - c->sub.copy.dist;
660
s->mode = CODES_START;
662
case CODES_LIT: /* o: got literal, waiting for output space */
665
s->mode = CODES_START;
667
case CODES_WASH: /* o: got eob, possibly more output */
668
if (k > 7) /* return unused byte, if any */
672
p--; /* can always return one */
674
/* flushing will be done in DRY */
682
if (s->write != s->read)
684
if (s->mode == CODES_WASH)
686
Tracev((stderr, "inflate: codes end, %lu total out\n",
687
z->total_out + (q >= s->read ? q - s->read :
688
(s->end - s->read) + (q - s->window))));
690
/* DRY if last, TYPE if not */
691
s->mode = (inflate_mode)s->last;
699
default: /* we'll call Z_STREAM_ERROR if BAD anyway */
702
LEAVE(Z_STREAM_ERROR)