1
/* mszip decompression - based on cabextract.c code from
4
* adapted for Samba by Andrew Tridgell and Stefan Metzmacher 2005
6
* (C) 2000-2001 Stuart Caie <kyzer@4u.net>
7
* reaktivate-specifics by Malte Starostik <malte@kde.org>
9
* This program is free software; you can redistribute it and/or modify
10
* it under the terms of the GNU General Public License as published by
11
* the Free Software Foundation; either version 3 of the License, or
12
* (at your option) any later version.
14
* This program is distributed in the hope that it will be useful,
15
* but WITHOUT ANY WARRANTY; without even the implied warranty of
16
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17
* GNU General Public License for more details.
19
* You should have received a copy of the GNU General Public License
20
* along with this program; if not, see <http://www.gnu.org/licenses/>.
24
#include "../compression/mszip.h"
26
/*--------------------------------------------------------------------------*/
27
/* our archiver information / state */
30
#define ZIPWSIZE 0x8000 /* window size */
31
#define ZIPLBITS 9 /* bits in base literal/length lookup table */
32
#define ZIPDBITS 6 /* bits in base distance lookup table */
33
#define ZIPBMAX 16 /* maximum bit length of any code */
34
#define ZIPN_MAX 288 /* maximum number of codes in any set */
37
uint8_t e; /* number of extra bits or operation */
38
uint8_t b; /* number of bits in this code or subcode */
40
uint16_t n; /* literal, length base, or distance base */
41
struct Ziphuft *t; /* pointer to next level of table */
46
uint32_t window_posn; /* current offset within the window */
47
uint32_t bb; /* bit buffer */
48
uint32_t bk; /* bits in bit buffer */
49
uint32_t ll[288+32]; /* literal/length and distance code lengths */
50
uint32_t c[ZIPBMAX+1]; /* bit length count table */
51
int32_t lx[ZIPBMAX+1]; /* memory for l[-1..ZIPBMAX-1] */
52
struct Ziphuft *u[ZIPBMAX]; /* table stack */
53
uint32_t v[ZIPN_MAX]; /* values in order of bit length */
54
uint32_t x[ZIPBMAX+1]; /* bit offsets, then code stack */
59
#define CAB(x) (decomp_state->x)
60
#define ZIP(x) (decomp_state->methods.zip.x)
62
/* CAB data blocks are <= 32768 bytes in uncompressed form. Uncompressed
63
* blocks have zero growth. MSZIP guarantees that it won't grow above
64
* uncompressed size by more than 12 bytes. LZX guarantees it won't grow
65
* more than 6144 bytes.
67
#define CAB_BLOCKMAX (32768)
68
#define CAB_INPUTMAX (CAB_BLOCKMAX+6144)
71
struct folder *current; /* current folder we're extracting from */
72
uint32_t offset; /* uncompressed offset within folder */
73
uint8_t *outpos; /* (high level) start of data to use up */
74
uint16_t outlen; /* (high level) amount of data to use up */
75
uint16_t split; /* at which split in current folder? */
76
int (*decompress)(int, int); /* the chosen compression func */
77
uint8_t inbuf[CAB_INPUTMAX+2]; /* +2 for lzx bitbuffer overflows! */
78
uint8_t outbuf[CAB_BLOCKMAX];
85
/* MSZIP decruncher */
87
/* Dirk Stoecker wrote the ZIP decoder, based on the InfoZip deflate code */
89
/* Tables for deflate from PKZIP's appnote.txt. */
90
static const uint8_t Zipborder[] = /* Order of the bit length code lengths */
91
{ 16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15};
92
static const uint16_t Zipcplens[] = /* Copy lengths for literal codes 257..285 */
93
{ 3, 4, 5, 6, 7, 8, 9, 10, 11, 13, 15, 17, 19, 23, 27, 31, 35, 43, 51,
94
59, 67, 83, 99, 115, 131, 163, 195, 227, 258, 0, 0};
95
static const uint16_t Zipcplext[] = /* Extra bits for literal codes 257..285 */
96
{ 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 4, 4, 4,
97
4, 5, 5, 5, 5, 0, 99, 99}; /* 99==invalid */
98
static const uint16_t Zipcpdist[] = /* Copy offsets for distance codes 0..29 */
99
{ 1, 2, 3, 4, 5, 7, 9, 13, 17, 25, 33, 49, 65, 97, 129, 193, 257, 385,
100
513, 769, 1025, 1537, 2049, 3073, 4097, 6145, 8193, 12289, 16385, 24577};
101
static const uint16_t Zipcpdext[] = /* Extra bits for distance codes */
102
{ 0, 0, 0, 0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 7, 7, 8, 8, 9, 9, 10,
103
10, 11, 11, 12, 12, 13, 13};
105
/* And'ing with Zipmask[n] masks the lower n bits */
106
static const uint16_t Zipmask[17] = {
107
0x0000, 0x0001, 0x0003, 0x0007, 0x000f, 0x001f, 0x003f, 0x007f, 0x00ff,
108
0x01ff, 0x03ff, 0x07ff, 0x0fff, 0x1fff, 0x3fff, 0x7fff, 0xffff
111
#define ZIPNEEDBITS(n) {while(k<(n)){int32_t c=*(ZIP(inpos)++);\
112
b|=((uint32_t)c)<<k;k+=8;}}
113
#define ZIPDUMPBITS(n) {b>>=(n);k-=(n);}
115
static void Ziphuft_free(struct Ziphuft *t)
117
register struct Ziphuft *p, *q;
119
/* Go through linked list, freeing from the allocated (t[-1]) address. */
121
while (p != (struct Ziphuft *)NULL)
129
static int32_t Ziphuft_build(struct decomp_state *decomp_state,
130
uint32_t *b, uint32_t n, uint32_t s, const uint16_t *d, const uint16_t *e,
131
struct Ziphuft **t, int32_t *m)
133
uint32_t a; /* counter for codes of length k */
134
uint32_t el; /* length of EOB code (value 256) */
135
uint32_t f; /* i repeats in table every f entries */
136
int32_t g; /* maximum code length */
137
int32_t h; /* table level */
138
register uint32_t i; /* counter, current code */
139
register uint32_t j; /* counter */
140
register int32_t k; /* number of bits in current code */
141
int32_t *l; /* stack of bits per table */
142
register uint32_t *p; /* pointer into ZIP(c)[],ZIP(b)[],ZIP(v)[] */
143
register struct Ziphuft *q; /* points to current table */
144
struct Ziphuft r; /* table entry for structure assignment */
145
register int32_t w; /* bits before this table == (l * h) */
146
uint32_t *xp; /* pointer into x */
147
int32_t y; /* number of dummy codes added */
148
uint32_t z; /* number of entries in current table */
152
/* Generate counts for each bit length */
153
el = n > 256 ? b[256] : ZIPBMAX; /* set length of EOB code, if any */
155
for(i = 0; i < ZIPBMAX+1; ++i)
160
ZIP(c)[*p]++; p++; /* assume all entries <= ZIPBMAX */
162
if (ZIP(c)[0] == n) /* null input--all zero length codes */
164
*t = (struct Ziphuft *)NULL;
169
/* Find minimum and maximum length, bound *m by those */
170
for (j = 1; j <= ZIPBMAX; j++)
173
k = j; /* minimum code length */
174
if ((uint32_t)*m < j)
176
for (i = ZIPBMAX; i; i--)
179
g = i; /* maximum code length */
180
if ((uint32_t)*m > i)
183
/* Adjust last length count to fill out codes, if needed */
184
for (y = 1 << j; j < i; j++, y <<= 1)
185
if ((y -= ZIP(c)[j]) < 0)
186
return 2; /* bad input: more codes than bits */
187
if ((y -= ZIP(c)[i]) < 0)
191
/* Generate starting offsets int32_to the value table for each length */
193
p = ZIP(c) + 1; xp = ZIP(x) + 2;
195
{ /* note that i == g from above */
199
/* Make a table of values in order of bit lengths */
203
ZIP(v)[ZIP(x)[j]++] = i;
207
/* Generate the Huffman codes and for each, make the table entries */
208
ZIP(x)[0] = i = 0; /* first Huffman code is zero */
209
p = ZIP(v); /* grab values in bit order */
210
h = -1; /* no tables yet--level -1 */
211
w = l[-1] = 0; /* no bits decoded yet */
212
ZIP(u)[0] = (struct Ziphuft *)NULL; /* just to keep compilers happy */
213
q = (struct Ziphuft *)NULL; /* ditto */
216
/* go through the bit lengths (k already is bits in shortest code) */
222
/* here i is the Huffman code of length k bits for value *p */
223
/* make tables up to required level */
226
w += l[h++]; /* add bits already decoded */
228
/* compute minimum size table less than or equal to *m bits */
229
z = (z = g - w) > (uint32_t)*m ? *m : z; /* upper limit */
230
if ((f = 1 << (j = k - w)) > a + 1) /* try a k-w bit table */
231
{ /* too few codes for k-w bit table */
232
f -= a + 1; /* deduct codes from patterns left */
234
while (++j < z) /* try smaller tables up to z bits */
236
if ((f <<= 1) <= *++xp)
237
break; /* enough codes to use up j bits */
238
f -= *xp; /* else deduct codes from patterns */
241
if ((uint32_t)w + j > el && (uint32_t)w < el)
242
j = el - w; /* make EOB code end at table */
243
z = 1 << j; /* table entries for j-bit table */
244
l[h] = j; /* set table size in stack */
246
/* allocate and link in new table */
247
if (!(q = (struct Ziphuft *)SMB_MALLOC((z + 1)*sizeof(struct Ziphuft))))
250
Ziphuft_free(ZIP(u)[0]);
251
return 3; /* not enough memory */
253
*t = q + 1; /* link to list for Ziphuft_free() */
254
*(t = &(q->v.t)) = (struct Ziphuft *)NULL;
255
ZIP(u)[h] = ++q; /* table starts after link */
257
/* connect to last table, if there is one */
260
ZIP(x)[h] = i; /* save pattern for backing up */
261
r.b = (uint8_t)l[h-1]; /* bits to dump before this table */
262
r.e = (uint8_t)(16 + j); /* bits in this table */
263
r.v.t = q; /* pointer to this table */
264
j = (i & ((1 << w) - 1)) >> (w - l[h-1]);
265
ZIP(u)[h-1][j] = r; /* connect to last table */
269
/* set up table entry in r */
270
r.b = (uint8_t)(k - w);
272
r.e = 99; /* out of values--invalid code */
275
r.e = (uint8_t)(*p < 256 ? 16 : 15); /* 256 is end-of-block code */
276
r.v.n = *p++; /* simple code is just the value */
280
r.e = (uint8_t)e[*p - s]; /* non-simple--look up in lists */
284
/* fill code-like entries with r */
286
for (j = i >> w; j < z; j += f)
289
/* backwards increment the k-bit code i */
290
for (j = 1 << (k - 1); i & j; j >>= 1)
294
/* backup over finished tables */
295
while ((i & ((1 << w) - 1)) != ZIP(x)[h])
296
w -= l[--h]; /* don't need to update q */
300
/* return actual size of base table */
303
/* Return true (1) if we were given an incomplete table */
304
return y != 0 && g != 1;
307
static int32_t Zipinflate_codes(struct decomp_state *decomp_state,
308
struct Ziphuft *tl, struct Ziphuft *td,
309
int32_t bl, int32_t bd)
311
register uint32_t e; /* table entry flag/number of extra bits */
312
uint32_t n, d; /* length and index for copy */
313
uint32_t w; /* current window position */
314
struct Ziphuft *t; /* pointer to table entry */
315
uint32_t ml, md; /* masks for bl and bd bits */
316
register uint32_t b; /* bit buffer */
317
register uint32_t k; /* number of bits in bit buffer */
319
DEBUG(10,("Zipinflate_codes\n"));
321
/* make local copies of globals */
322
b = ZIP(bb); /* initialize bit buffer */
324
w = ZIP(window_posn); /* initialize window position */
326
/* inflate the coded data */
327
ml = Zipmask[bl]; /* precompute masks for speed */
332
ZIPNEEDBITS((uint32_t)bl)
333
if((e = (t = tl + ((uint32_t)b & ml))->e) > 16)
341
} while ((e = (t = t->v.t + ((uint32_t)b & Zipmask[e]))->e) > 16);
343
if (w >= CAB_BLOCKMAX) break;
344
if (e == 16) /* then it's a literal */
345
CAB(outbuf)[w++] = (uint8_t)t->v.n;
346
else /* it's an EOB or a length */
348
/* exit if end of block */
352
/* get length of block to copy */
354
n = t->v.n + ((uint32_t)b & Zipmask[e]);
357
/* decode distance of block to copy */
358
ZIPNEEDBITS((uint32_t)bd)
359
if ((e = (t = td + ((uint32_t)b & md))->e) > 16)
366
} while ((e = (t = t->v.t + ((uint32_t)b & Zipmask[e]))->e) > 16);
369
d = w - t->v.n - ((uint32_t)b & Zipmask[e]);
373
n -= (e = (e = ZIPWSIZE - ((d &= ZIPWSIZE-1) > w ? d : w)) > n ?n:e);
376
CAB(outbuf)[w++] = CAB(outbuf)[d++];
382
/* restore the globals from the locals */
383
ZIP(window_posn) = w; /* restore global window pointer */
384
ZIP(bb) = b; /* restore global bit buffer */
391
/* "decompress" an inflated type 0 (stored) block. */
392
static int32_t Zipinflate_stored(struct decomp_state *decomp_state)
394
uint32_t n; /* number of bytes in block */
395
uint32_t w; /* current window position */
396
register uint32_t b; /* bit buffer */
397
register uint32_t k; /* number of bits in bit buffer */
399
/* make local copies of globals */
400
b = ZIP(bb); /* initialize bit buffer */
402
w = ZIP(window_posn); /* initialize window position */
404
/* go to byte boundary */
408
/* get the length and its complement */
410
n = ((uint32_t)b & 0xffff);
413
if (n != (uint32_t)((~b) & 0xffff))
414
return 1; /* error in compressed data */
417
/* read and output the compressed data */
421
CAB(outbuf)[w++] = (uint8_t)b;
425
/* restore the globals from the locals */
426
ZIP(window_posn) = w; /* restore global window pointer */
427
ZIP(bb) = b; /* restore global bit buffer */
432
static int32_t Zipinflate_fixed(struct decomp_state *decomp_state)
434
struct Ziphuft *fixed_tl;
435
struct Ziphuft *fixed_td;
436
int32_t fixed_bl, fixed_bd;
437
int32_t i; /* temporary variable */
443
for(i = 0; i < 144; i++)
449
for(; i < 288; i++) /* make a complete, but wrong code set */
452
if((i = Ziphuft_build(decomp_state, l, 288, 257, Zipcplens, Zipcplext, &fixed_tl, &fixed_bl)))
456
for(i = 0; i < 30; i++) /* make an incomplete code set */
459
if((i = Ziphuft_build(decomp_state, l, 30, 0, Zipcpdist, Zipcpdext, &fixed_td, &fixed_bd)) > 1)
461
Ziphuft_free(fixed_tl);
465
/* decompress until an end-of-block code */
466
i = Zipinflate_codes(decomp_state, fixed_tl, fixed_td, fixed_bl, fixed_bd);
468
Ziphuft_free(fixed_td);
469
Ziphuft_free(fixed_tl);
473
/* decompress an inflated type 2 (dynamic Huffman codes) block. */
474
static int32_t Zipinflate_dynamic(struct decomp_state *decomp_state)
476
int32_t i; /* temporary variables */
479
uint32_t l; /* last length */
480
uint32_t m; /* mask for bit lengths table */
481
uint32_t n; /* number of lengths to get */
482
struct Ziphuft *tl; /* literal/length code table */
483
struct Ziphuft *td; /* distance code table */
484
int32_t bl; /* lookup bits for tl */
485
int32_t bd; /* lookup bits for td */
486
uint32_t nb; /* number of bit length codes */
487
uint32_t nl; /* number of literal/length codes */
488
uint32_t nd; /* number of distance codes */
489
register uint32_t b; /* bit buffer */
490
register uint32_t k; /* number of bits in bit buffer */
492
/* make local bit buffer */
497
/* read in table lengths */
499
nl = 257 + ((uint32_t)b & 0x1f); /* number of literal/length codes */
502
nd = 1 + ((uint32_t)b & 0x1f); /* number of distance codes */
505
nb = 4 + ((uint32_t)b & 0xf); /* number of bit length codes */
507
if(nl > 288 || nd > 32)
508
return 1; /* bad lengths */
510
/* read in bit-length-code lengths */
511
for(j = 0; j < nb; j++)
514
ll[Zipborder[j]] = (uint32_t)b & 7;
518
ll[Zipborder[j]] = 0;
520
/* build decoding table for trees--single level, 7 bit lookup */
522
if((i = Ziphuft_build(decomp_state, ll, 19, 19, NULL, NULL, &tl, &bl)) != 0)
526
return i; /* incomplete code set */
529
/* read in literal and distance code lengths */
533
while((uint32_t)i < n)
535
ZIPNEEDBITS((uint32_t)bl)
536
j = (td = tl + ((uint32_t)b & m))->b;
539
if (j < 16) /* length of code in bits (0..15) */
540
ll[i++] = l = j; /* save last length in l */
541
else if (j == 16) /* repeat last length 3 to 6 times */
544
j = 3 + ((uint32_t)b & 3);
546
if((uint32_t)i + j > n)
551
else if (j == 17) /* 3 to 10 zero length codes */
554
j = 3 + ((uint32_t)b & 7);
556
if ((uint32_t)i + j > n)
562
else /* j == 18: 11 to 138 zero length codes */
565
j = 11 + ((uint32_t)b & 0x7f);
567
if ((uint32_t)i + j > n)
575
/* free decoding table for trees */
578
/* restore the global bit buffer */
582
/* build the decoding tables for literal/length and distance codes */
584
if((i = Ziphuft_build(decomp_state, ll, nl, 257, Zipcplens, Zipcplext, &tl, &bl)) != 0)
588
return i; /* incomplete code set */
591
Ziphuft_build(decomp_state, ll + nl, nd, 0, Zipcpdist, Zipcpdext, &td, &bd);
593
/* decompress until an end-of-block code */
594
if(Zipinflate_codes(decomp_state, tl, td, bl, bd))
597
/* free the decoding tables, return */
603
/* e == last block flag */
604
static int32_t Zipinflate_block(struct decomp_state *decomp_state, int32_t *e)
605
{ /* decompress an inflated block */
606
uint32_t t; /* block type */
607
register uint32_t b; /* bit buffer */
608
register uint32_t k; /* number of bits in bit buffer */
610
DEBUG(10,("Zipinflate_block\n"));
612
/* make local bit buffer */
616
/* read in last block bit */
621
/* read in block type */
626
/* restore the global bit buffer */
630
DEBUG(10,("inflate type %d\n", t));
632
/* inflate that block type */
634
return Zipinflate_dynamic(decomp_state);
636
return Zipinflate_stored(decomp_state);
638
return Zipinflate_fixed(decomp_state);
643
_PUBLIC_ struct decomp_state *ZIPdecomp_state(TALLOC_CTX *mem_ctx)
645
return talloc_zero(mem_ctx, struct decomp_state);
648
int ZIPdecompress(struct decomp_state *decomp_state, DATA_BLOB *inbuf, DATA_BLOB *outbuf)
650
int32_t e = 0;/* last block flag */
652
ZIP(inpos) = CAB(inbuf);
653
ZIP(bb) = ZIP(bk) = ZIP(window_posn) = 0;
655
if (inbuf->length > sizeof(decomp_state->inbuf)) return DECR_INPUT;
657
if (outbuf->length > sizeof(decomp_state->outbuf)) return DECR_OUTPUT;
659
if (outbuf->length > ZIPWSIZE) return DECR_DATAFORMAT;
661
memcpy(decomp_state->inbuf, inbuf->data, inbuf->length);
663
/* CK = Chris Kirmse, official Microsoft purloiner */
664
if (ZIP(inpos)[0] != 'C' || ZIP(inpos)[1] != 'K') return DECR_ILLEGALDATA;
668
if (Zipinflate_block(decomp_state, &e)) {
669
return DECR_ILLEGALDATA;
673
memcpy(outbuf->data, decomp_state->outbuf, outbuf->length);