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 2 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, write to the Free Software
21 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
25 #include "lib/compression/mszip.h"
27 /*--------------------------------------------------------------------------*/
28 /* our archiver information / state */
31 #define ZIPWSIZE 0x8000 /* window size */
32 #define ZIPLBITS 9 /* bits in base literal/length lookup table */
33 #define ZIPDBITS 6 /* bits in base distance lookup table */
34 #define ZIPBMAX 16 /* maximum bit length of any code */
35 #define ZIPN_MAX 288 /* maximum number of codes in any set */
38 uint8_t e; /* number of extra bits or operation */
39 uint8_t b; /* number of bits in this code or subcode */
41 uint16_t n; /* literal, length base, or distance base */
42 struct Ziphuft *t; /* pointer to next level of table */
47 uint32_t window_posn; /* current offset within the window */
48 uint32_t bb; /* bit buffer */
49 uint32_t bk; /* bits in bit buffer */
50 uint32_t ll[288+32]; /* literal/length and distance code lengths */
51 uint32_t c[ZIPBMAX+1]; /* bit length count table */
52 int32_t lx[ZIPBMAX+1]; /* memory for l[-1..ZIPBMAX-1] */
53 struct Ziphuft *u[ZIPBMAX]; /* table stack */
54 uint32_t v[ZIPN_MAX]; /* values in order of bit length */
55 uint32_t x[ZIPBMAX+1]; /* bit offsets, then code stack */
60 #define CAB(x) (decomp_state->x)
61 #define ZIP(x) (decomp_state->methods.zip.x)
63 /* CAB data blocks are <= 32768 bytes in uncompressed form. Uncompressed
64 * blocks have zero growth. MSZIP guarantees that it won't grow above
65 * uncompressed size by more than 12 bytes. LZX guarantees it won't grow
66 * more than 6144 bytes.
68 #define CAB_BLOCKMAX (32768)
69 #define CAB_INPUTMAX (CAB_BLOCKMAX+6144)
72 struct folder *current; /* current folder we're extracting from */
73 uint32_t offset; /* uncompressed offset within folder */
74 uint8_t *outpos; /* (high level) start of data to use up */
75 uint16_t outlen; /* (high level) amount of data to use up */
76 uint16_t split; /* at which split in current folder? */
77 int (*decompress)(int, int); /* the chosen compression func */
78 uint8_t inbuf[CAB_INPUTMAX+2]; /* +2 for lzx bitbuffer overflows! */
79 uint8_t outbuf[CAB_BLOCKMAX];
86 /* MSZIP decruncher */
88 /* Dirk Stoecker wrote the ZIP decoder, based on the InfoZip deflate code */
90 /* Tables for deflate from PKZIP's appnote.txt. */
91 static const uint8_t Zipborder[] = /* Order of the bit length code lengths */
92 { 16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15};
93 static const uint16_t Zipcplens[] = /* Copy lengths for literal codes 257..285 */
94 { 3, 4, 5, 6, 7, 8, 9, 10, 11, 13, 15, 17, 19, 23, 27, 31, 35, 43, 51,
95 59, 67, 83, 99, 115, 131, 163, 195, 227, 258, 0, 0};
96 static const uint16_t Zipcplext[] = /* Extra bits for literal codes 257..285 */
97 { 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 4, 4, 4,
98 4, 5, 5, 5, 5, 0, 99, 99}; /* 99==invalid */
99 static const uint16_t Zipcpdist[] = /* Copy offsets for distance codes 0..29 */
100 { 1, 2, 3, 4, 5, 7, 9, 13, 17, 25, 33, 49, 65, 97, 129, 193, 257, 385,
101 513, 769, 1025, 1537, 2049, 3073, 4097, 6145, 8193, 12289, 16385, 24577};
102 static const uint16_t Zipcpdext[] = /* Extra bits for distance codes */
103 { 0, 0, 0, 0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 7, 7, 8, 8, 9, 9, 10,
104 10, 11, 11, 12, 12, 13, 13};
106 /* And'ing with Zipmask[n] masks the lower n bits */
107 static const uint16_t Zipmask[17] = {
108 0x0000, 0x0001, 0x0003, 0x0007, 0x000f, 0x001f, 0x003f, 0x007f, 0x00ff,
109 0x01ff, 0x03ff, 0x07ff, 0x0fff, 0x1fff, 0x3fff, 0x7fff, 0xffff
112 #define ZIPNEEDBITS(n) {while(k<(n)){int32_t c=*(ZIP(inpos)++);\
113 b|=((uint32_t)c)<<k;k+=8;}}
114 #define ZIPDUMPBITS(n) {b>>=(n);k-=(n);}
116 static void Ziphuft_free(struct Ziphuft *t)
118 register struct Ziphuft *p, *q;
120 /* Go through linked list, freeing from the allocated (t[-1]) address. */
122 while (p != (struct Ziphuft *)NULL)
130 static int32_t Ziphuft_build(struct decomp_state *decomp_state,
131 uint32_t *b, uint32_t n, uint32_t s, const uint16_t *d, const uint16_t *e,
132 struct Ziphuft **t, int32_t *m)
134 uint32_t a; /* counter for codes of length k */
135 uint32_t el; /* length of EOB code (value 256) */
136 uint32_t f; /* i repeats in table every f entries */
137 int32_t g; /* maximum code length */
138 int32_t h; /* table level */
139 register uint32_t i; /* counter, current code */
140 register uint32_t j; /* counter */
141 register int32_t k; /* number of bits in current code */
142 int32_t *l; /* stack of bits per table */
143 register uint32_t *p; /* pointer into ZIP(c)[],ZIP(b)[],ZIP(v)[] */
144 register struct Ziphuft *q; /* points to current table */
145 struct Ziphuft r; /* table entry for structure assignment */
146 register int32_t w; /* bits before this table == (l * h) */
147 uint32_t *xp; /* pointer into x */
148 int32_t y; /* number of dummy codes added */
149 uint32_t z; /* number of entries in current table */
153 /* Generate counts for each bit length */
154 el = n > 256 ? b[256] : ZIPBMAX; /* set length of EOB code, if any */
156 for(i = 0; i < ZIPBMAX+1; ++i)
161 ZIP(c)[*p]++; p++; /* assume all entries <= ZIPBMAX */
163 if (ZIP(c)[0] == n) /* null input--all zero length codes */
165 *t = (struct Ziphuft *)NULL;
170 /* Find minimum and maximum length, bound *m by those */
171 for (j = 1; j <= ZIPBMAX; j++)
174 k = j; /* minimum code length */
175 if ((uint32_t)*m < j)
177 for (i = ZIPBMAX; i; i--)
180 g = i; /* maximum code length */
181 if ((uint32_t)*m > i)
184 /* Adjust last length count to fill out codes, if needed */
185 for (y = 1 << j; j < i; j++, y <<= 1)
186 if ((y -= ZIP(c)[j]) < 0)
187 return 2; /* bad input: more codes than bits */
188 if ((y -= ZIP(c)[i]) < 0)
192 /* Generate starting offsets int32_to the value table for each length */
194 p = ZIP(c) + 1; xp = ZIP(x) + 2;
196 { /* note that i == g from above */
200 /* Make a table of values in order of bit lengths */
204 ZIP(v)[ZIP(x)[j]++] = i;
208 /* Generate the Huffman codes and for each, make the table entries */
209 ZIP(x)[0] = i = 0; /* first Huffman code is zero */
210 p = ZIP(v); /* grab values in bit order */
211 h = -1; /* no tables yet--level -1 */
212 w = l[-1] = 0; /* no bits decoded yet */
213 ZIP(u)[0] = (struct Ziphuft *)NULL; /* just to keep compilers happy */
214 q = (struct Ziphuft *)NULL; /* ditto */
217 /* go through the bit lengths (k already is bits in shortest code) */
223 /* here i is the Huffman code of length k bits for value *p */
224 /* make tables up to required level */
227 w += l[h++]; /* add bits already decoded */
229 /* compute minimum size table less than or equal to *m bits */
230 z = (z = g - w) > (uint32_t)*m ? *m : z; /* upper limit */
231 if ((f = 1 << (j = k - w)) > a + 1) /* try a k-w bit table */
232 { /* too few codes for k-w bit table */
233 f -= a + 1; /* deduct codes from patterns left */
235 while (++j < z) /* try smaller tables up to z bits */
237 if ((f <<= 1) <= *++xp)
238 break; /* enough codes to use up j bits */
239 f -= *xp; /* else deduct codes from patterns */
242 if ((uint32_t)w + j > el && (uint32_t)w < el)
243 j = el - w; /* make EOB code end at table */
244 z = 1 << j; /* table entries for j-bit table */
245 l[h] = j; /* set table size in stack */
247 /* allocate and link in new table */
248 if (!(q = (struct Ziphuft *) malloc((z + 1)*sizeof(struct Ziphuft))))
251 Ziphuft_free(ZIP(u)[0]);
252 return 3; /* not enough memory */
254 *t = q + 1; /* link to list for Ziphuft_free() */
255 *(t = &(q->v.t)) = (struct Ziphuft *)NULL;
256 ZIP(u)[h] = ++q; /* table starts after link */
258 /* connect to last table, if there is one */
261 ZIP(x)[h] = i; /* save pattern for backing up */
262 r.b = (uint8_t)l[h-1]; /* bits to dump before this table */
263 r.e = (uint8_t)(16 + j); /* bits in this table */
264 r.v.t = q; /* pointer to this table */
265 j = (i & ((1 << w) - 1)) >> (w - l[h-1]);
266 ZIP(u)[h-1][j] = r; /* connect to last table */
270 /* set up table entry in r */
271 r.b = (uint8_t)(k - w);
273 r.e = 99; /* out of values--invalid code */
276 r.e = (uint8_t)(*p < 256 ? 16 : 15); /* 256 is end-of-block code */
277 r.v.n = *p++; /* simple code is just the value */
281 r.e = (uint8_t)e[*p - s]; /* non-simple--look up in lists */
285 /* fill code-like entries with r */
287 for (j = i >> w; j < z; j += f)
290 /* backwards increment the k-bit code i */
291 for (j = 1 << (k - 1); i & j; j >>= 1)
295 /* backup over finished tables */
296 while ((i & ((1 << w) - 1)) != ZIP(x)[h])
297 w -= l[--h]; /* don't need to update q */
301 /* return actual size of base table */
304 /* Return true (1) if we were given an incomplete table */
305 return y != 0 && g != 1;
308 static int32_t Zipinflate_codes(struct decomp_state *decomp_state,
309 struct Ziphuft *tl, struct Ziphuft *td,
310 int32_t bl, int32_t bd)
312 register uint32_t e; /* table entry flag/number of extra bits */
313 uint32_t n, d; /* length and index for copy */
314 uint32_t w; /* current window position */
315 struct Ziphuft *t; /* pointer to table entry */
316 uint32_t ml, md; /* masks for bl and bd bits */
317 register uint32_t b; /* bit buffer */
318 register uint32_t k; /* number of bits in bit buffer */
320 DEBUG(10,("Zipinflate_codes\n"));
322 /* make local copies of globals */
323 b = ZIP(bb); /* initialize bit buffer */
325 w = ZIP(window_posn); /* initialize window position */
327 /* inflate the coded data */
328 ml = Zipmask[bl]; /* precompute masks for speed */
333 ZIPNEEDBITS((uint32_t)bl)
334 if((e = (t = tl + ((uint32_t)b & ml))->e) > 16)
342 } while ((e = (t = t->v.t + ((uint32_t)b & Zipmask[e]))->e) > 16);
344 if (w >= CAB_BLOCKMAX) break;
345 if (e == 16) /* then it's a literal */
346 CAB(outbuf)[w++] = (uint8_t)t->v.n;
347 else /* it's an EOB or a length */
349 /* exit if end of block */
353 /* get length of block to copy */
355 n = t->v.n + ((uint32_t)b & Zipmask[e]);
358 /* decode distance of block to copy */
359 ZIPNEEDBITS((uint32_t)bd)
360 if ((e = (t = td + ((uint32_t)b & md))->e) > 16)
367 } while ((e = (t = t->v.t + ((uint32_t)b & Zipmask[e]))->e) > 16);
370 d = w - t->v.n - ((uint32_t)b & Zipmask[e]);
374 n -= (e = (e = ZIPWSIZE - ((d &= ZIPWSIZE-1) > w ? d : w)) > n ?n:e);
377 CAB(outbuf)[w++] = CAB(outbuf)[d++];
383 /* restore the globals from the locals */
384 ZIP(window_posn) = w; /* restore global window pointer */
385 ZIP(bb) = b; /* restore global bit buffer */
392 /* "decompress" an inflated type 0 (stored) block. */
393 static int32_t Zipinflate_stored(struct decomp_state *decomp_state)
395 uint32_t n; /* number of bytes in block */
396 uint32_t w; /* current window position */
397 register uint32_t b; /* bit buffer */
398 register uint32_t k; /* number of bits in bit buffer */
400 /* make local copies of globals */
401 b = ZIP(bb); /* initialize bit buffer */
403 w = ZIP(window_posn); /* initialize window position */
405 /* go to byte boundary */
409 /* get the length and its complement */
411 n = ((uint32_t)b & 0xffff);
414 if (n != (uint32_t)((~b) & 0xffff))
415 return 1; /* error in compressed data */
418 /* read and output the compressed data */
422 CAB(outbuf)[w++] = (uint8_t)b;
426 /* restore the globals from the locals */
427 ZIP(window_posn) = w; /* restore global window pointer */
428 ZIP(bb) = b; /* restore global bit buffer */
433 static int32_t Zipinflate_fixed(struct decomp_state *decomp_state)
435 struct Ziphuft *fixed_tl;
436 struct Ziphuft *fixed_td;
437 int32_t fixed_bl, fixed_bd;
438 int32_t i; /* temporary variable */
444 for(i = 0; i < 144; i++)
450 for(; i < 288; i++) /* make a complete, but wrong code set */
453 if((i = Ziphuft_build(decomp_state, l, 288, 257, Zipcplens, Zipcplext, &fixed_tl, &fixed_bl)))
457 for(i = 0; i < 30; i++) /* make an incomplete code set */
460 if((i = Ziphuft_build(decomp_state, l, 30, 0, Zipcpdist, Zipcpdext, &fixed_td, &fixed_bd)) > 1)
462 Ziphuft_free(fixed_tl);
466 /* decompress until an end-of-block code */
467 i = Zipinflate_codes(decomp_state, fixed_tl, fixed_td, fixed_bl, fixed_bd);
469 Ziphuft_free(fixed_td);
470 Ziphuft_free(fixed_tl);
474 /* decompress an inflated type 2 (dynamic Huffman codes) block. */
475 static int32_t Zipinflate_dynamic(struct decomp_state *decomp_state)
477 int32_t i; /* temporary variables */
480 uint32_t l; /* last length */
481 uint32_t m; /* mask for bit lengths table */
482 uint32_t n; /* number of lengths to get */
483 struct Ziphuft *tl; /* literal/length code table */
484 struct Ziphuft *td; /* distance code table */
485 int32_t bl; /* lookup bits for tl */
486 int32_t bd; /* lookup bits for td */
487 uint32_t nb; /* number of bit length codes */
488 uint32_t nl; /* number of literal/length codes */
489 uint32_t nd; /* number of distance codes */
490 register uint32_t b; /* bit buffer */
491 register uint32_t k; /* number of bits in bit buffer */
493 /* make local bit buffer */
498 /* read in table lengths */
500 nl = 257 + ((uint32_t)b & 0x1f); /* number of literal/length codes */
503 nd = 1 + ((uint32_t)b & 0x1f); /* number of distance codes */
506 nb = 4 + ((uint32_t)b & 0xf); /* number of bit length codes */
508 if(nl > 288 || nd > 32)
509 return 1; /* bad lengths */
511 /* read in bit-length-code lengths */
512 for(j = 0; j < nb; j++)
515 ll[Zipborder[j]] = (uint32_t)b & 7;
519 ll[Zipborder[j]] = 0;
521 /* build decoding table for trees--single level, 7 bit lookup */
523 if((i = Ziphuft_build(decomp_state, ll, 19, 19, NULL, NULL, &tl, &bl)) != 0)
527 return i; /* incomplete code set */
530 /* read in literal and distance code lengths */
534 while((uint32_t)i < n)
536 ZIPNEEDBITS((uint32_t)bl)
537 j = (td = tl + ((uint32_t)b & m))->b;
540 if (j < 16) /* length of code in bits (0..15) */
541 ll[i++] = l = j; /* save last length in l */
542 else if (j == 16) /* repeat last length 3 to 6 times */
545 j = 3 + ((uint32_t)b & 3);
547 if((uint32_t)i + j > n)
552 else if (j == 17) /* 3 to 10 zero length codes */
555 j = 3 + ((uint32_t)b & 7);
557 if ((uint32_t)i + j > n)
563 else /* j == 18: 11 to 138 zero length codes */
566 j = 11 + ((uint32_t)b & 0x7f);
568 if ((uint32_t)i + j > n)
576 /* free decoding table for trees */
579 /* restore the global bit buffer */
583 /* build the decoding tables for literal/length and distance codes */
585 if((i = Ziphuft_build(decomp_state, ll, nl, 257, Zipcplens, Zipcplext, &tl, &bl)) != 0)
589 return i; /* incomplete code set */
592 Ziphuft_build(decomp_state, ll + nl, nd, 0, Zipcpdist, Zipcpdext, &td, &bd);
594 /* decompress until an end-of-block code */
595 if(Zipinflate_codes(decomp_state, tl, td, bl, bd))
598 /* free the decoding tables, return */
604 /* e == last block flag */
605 static int32_t Zipinflate_block(struct decomp_state *decomp_state, int32_t *e)
606 { /* decompress an inflated block */
607 uint32_t t; /* block type */
608 register uint32_t b; /* bit buffer */
609 register uint32_t k; /* number of bits in bit buffer */
611 DEBUG(10,("Zipinflate_block\n"));
613 /* make local bit buffer */
617 /* read in last block bit */
622 /* read in block type */
627 /* restore the global bit buffer */
631 DEBUG(10,("inflate type %d\n", t));
633 /* inflate that block type */
635 return Zipinflate_dynamic(decomp_state);
637 return Zipinflate_stored(decomp_state);
639 return Zipinflate_fixed(decomp_state);
644 _PUBLIC_ struct decomp_state *ZIPdecomp_state(TALLOC_CTX *mem_ctx)
646 return talloc_zero(mem_ctx, struct decomp_state);
649 int ZIPdecompress(struct decomp_state *decomp_state, DATA_BLOB *inbuf, DATA_BLOB *outbuf)
651 int32_t e = 0;/* last block flag */
653 ZIP(inpos) = CAB(inbuf);
654 ZIP(bb) = ZIP(bk) = ZIP(window_posn) = 0;
656 if (inbuf->length > sizeof(decomp_state->inbuf)) return DECR_INPUT;
658 if (outbuf->length > sizeof(decomp_state->outbuf)) return DECR_OUTPUT;
660 if (outbuf->length > ZIPWSIZE) return DECR_DATAFORMAT;
662 memcpy(decomp_state->inbuf, inbuf->data, inbuf->length);
664 /* CK = Chris Kirmse, official Microsoft purloiner */
665 if (ZIP(inpos)[0] != 'C' || ZIP(inpos)[1] != 'K') return DECR_ILLEGALDATA;
669 if (Zipinflate_block(decomp_state, &e)) {
670 return DECR_ILLEGALDATA;
674 memcpy(outbuf->data, decomp_state->outbuf, outbuf->length);