r23795: more v2->v3 conversion
[metze/samba/wip.git] / source4 / lib / compression / mszip.c
1 /* mszip decompression - based on cabextract.c code from
2  * Stuart Caie
3  *
4  * adapted for Samba by Andrew Tridgell and Stefan Metzmacher 2005
5  *
6  * (C) 2000-2001 Stuart Caie <kyzer@4u.net>
7  * reaktivate-specifics by Malte Starostik <malte@kde.org>
8  *
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.
13  *
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.
18  *
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.
22  */
23
24 #include "includes.h"
25 #include "lib/compression/mszip.h"
26
27 /*--------------------------------------------------------------------------*/
28 /* our archiver information / state */
29
30 /* MSZIP stuff */
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 */
36
37 struct Ziphuft {
38   uint8_t e;                /* number of extra bits or operation */
39   uint8_t b;                /* number of bits in this code or subcode */
40   union {
41     uint16_t n;              /* literal, length base, or distance base */
42     struct Ziphuft *t;    /* pointer to next level of table */
43   } v;
44 };
45
46 struct ZIPstate {
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 */
56     uint8_t *inpos;
57 };
58
59 /* generic stuff */
60 #define CAB(x) (decomp_state->x)
61 #define ZIP(x) (decomp_state->methods.zip.x)
62
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.
67  */
68 #define CAB_BLOCKMAX (32768)
69 #define CAB_INPUTMAX (CAB_BLOCKMAX+6144)
70
71 struct decomp_state {
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];
80   union {
81     struct ZIPstate zip;
82   } methods;
83 };
84
85
86 /* MSZIP decruncher */
87
88 /* Dirk Stoecker wrote the ZIP decoder, based on the InfoZip deflate code */
89
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};
105
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
110 };
111
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);}
115
116 static void Ziphuft_free(struct Ziphuft *t)
117 {
118   register struct Ziphuft *p, *q;
119
120   /* Go through linked list, freeing from the allocated (t[-1]) address. */
121   p = t;
122   while (p != (struct Ziphuft *)NULL)
123   {
124     q = (--p)->v.t;
125     free(p);
126     p = q;
127   } 
128 }
129
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)
133 {
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 */
150
151   l = ZIP(lx)+1;
152
153   /* Generate counts for each bit length */
154   el = n > 256 ? b[256] : ZIPBMAX; /* set length of EOB code, if any */
155
156   for(i = 0; i < ZIPBMAX+1; ++i)
157     ZIP(c)[i] = 0;
158   p = b;  i = n;
159   do
160   {
161     ZIP(c)[*p]++; p++;               /* assume all entries <= ZIPBMAX */
162   } while (--i);
163   if (ZIP(c)[0] == n)                /* null input--all zero length codes */
164   {
165     *t = (struct Ziphuft *)NULL;
166     *m = 0;
167     return 0;
168   }
169
170   /* Find minimum and maximum length, bound *m by those */
171   for (j = 1; j <= ZIPBMAX; j++)
172     if (ZIP(c)[j])
173       break;
174   k = j;                        /* minimum code length */
175   if ((uint32_t)*m < j)
176     *m = j;
177   for (i = ZIPBMAX; i; i--)
178     if (ZIP(c)[i])
179       break;
180   g = i;                        /* maximum code length */
181   if ((uint32_t)*m > i)
182     *m = i;
183
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)
189     return 2;
190   ZIP(c)[i] += y;
191
192   /* Generate starting offsets int32_to the value table for each length */
193   ZIP(x)[1] = j = 0;
194   p = ZIP(c) + 1;  xp = ZIP(x) + 2;
195   while (--i)
196   {                 /* note that i == g from above */
197     *xp++ = (j += *p++);
198   }
199
200   /* Make a table of values in order of bit lengths */
201   p = b;  i = 0;
202   do{
203     if ((j = *p++) != 0)
204       ZIP(v)[ZIP(x)[j]++] = i;
205   } while (++i < n);
206
207
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 */
215   z = 0;                        /* ditto */
216
217   /* go through the bit lengths (k already is bits in shortest code) */
218   for (; k <= g; k++)
219   {
220     a = ZIP(c)[k];
221     while (a--)
222     {
223       /* here i is the Huffman code of length k bits for value *p */
224       /* make tables up to required level */
225       while (k > w + l[h])
226       {
227         w += l[h++];            /* add bits already decoded */
228
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 */
234           xp = ZIP(c) + k;
235           while (++j < z)       /* try smaller tables up to z bits */
236           {
237             if ((f <<= 1) <= *++xp)
238               break;            /* enough codes to use up j bits */
239             f -= *xp;           /* else deduct codes from patterns */
240           }
241         }
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 */
246
247         /* allocate and link in new table */
248         if (!(q = (struct Ziphuft *) malloc((z + 1)*sizeof(struct Ziphuft))))
249         {
250           if(h)
251             Ziphuft_free(ZIP(u)[0]);
252           return 3;             /* not enough memory */
253         }
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 */
257
258         /* connect to last table, if there is one */
259         if (h)
260         {
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 */
267         }
268       }
269
270       /* set up table entry in r */
271       r.b = (uint8_t)(k - w);
272       if (p >= ZIP(v) + n)
273         r.e = 99;               /* out of values--invalid code */
274       else if (*p < s)
275       {
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 */
278       }
279       else
280       {
281         r.e = (uint8_t)e[*p - s];   /* non-simple--look up in lists */
282         r.v.n = d[*p++ - s];
283       }
284
285       /* fill code-like entries with r */
286       f = 1 << (k - w);
287       for (j = i >> w; j < z; j += f)
288         q[j] = r;
289
290       /* backwards increment the k-bit code i */
291       for (j = 1 << (k - 1); i & j; j >>= 1)
292         i ^= j;
293       i ^= j;
294
295       /* backup over finished tables */
296       while ((i & ((1 << w) - 1)) != ZIP(x)[h])
297         w -= l[--h];            /* don't need to update q */
298     }
299   }
300
301   /* return actual size of base table */
302   *m = l[0];
303
304   /* Return true (1) if we were given an incomplete table */
305   return y != 0 && g != 1;
306 }
307
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)
311 {
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 */
319
320   DEBUG(10,("Zipinflate_codes\n"));
321
322   /* make local copies of globals */
323   b = ZIP(bb);                       /* initialize bit buffer */
324   k = ZIP(bk);
325   w = ZIP(window_posn);                       /* initialize window position */
326
327   /* inflate the coded data */
328   ml = Zipmask[bl];             /* precompute masks for speed */
329   md = Zipmask[bd];
330
331   for(;;)
332   {
333     ZIPNEEDBITS((uint32_t)bl)
334     if((e = (t = tl + ((uint32_t)b & ml))->e) > 16)
335       do
336       {
337         if (e == 99)
338           return 1;
339         ZIPDUMPBITS(t->b)
340         e -= 16;
341         ZIPNEEDBITS(e)
342       } while ((e = (t = t->v.t + ((uint32_t)b & Zipmask[e]))->e) > 16);
343     ZIPDUMPBITS(t->b)
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 */
348     {
349       /* exit if end of block */
350       if(e == 15)
351         break;
352
353       /* get length of block to copy */
354       ZIPNEEDBITS(e)
355       n = t->v.n + ((uint32_t)b & Zipmask[e]);
356       ZIPDUMPBITS(e);
357
358       /* decode distance of block to copy */
359       ZIPNEEDBITS((uint32_t)bd)
360       if ((e = (t = td + ((uint32_t)b & md))->e) > 16)
361         do {
362           if (e == 99)
363             return 1;
364           ZIPDUMPBITS(t->b)
365           e -= 16;
366           ZIPNEEDBITS(e)
367         } while ((e = (t = t->v.t + ((uint32_t)b & Zipmask[e]))->e) > 16);
368       ZIPDUMPBITS(t->b)
369       ZIPNEEDBITS(e)
370       d = w - t->v.n - ((uint32_t)b & Zipmask[e]);
371       ZIPDUMPBITS(e)
372       do
373       {
374         n -= (e = (e = ZIPWSIZE - ((d &= ZIPWSIZE-1) > w ? d : w)) > n ?n:e);
375         do
376         {
377           CAB(outbuf)[w++] = CAB(outbuf)[d++];
378         } while (--e);
379       } while (n);
380     }
381   }
382
383   /* restore the globals from the locals */
384   ZIP(window_posn) = w;              /* restore global window pointer */
385   ZIP(bb) = b;                       /* restore global bit buffer */
386   ZIP(bk) = k;
387
388   /* done */
389   return 0;
390 }
391
392 /* "decompress" an inflated type 0 (stored) block. */
393 static int32_t Zipinflate_stored(struct decomp_state *decomp_state)
394 {
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 */
399
400   /* make local copies of globals */
401   b = ZIP(bb);                       /* initialize bit buffer */
402   k = ZIP(bk);
403   w = ZIP(window_posn);              /* initialize window position */
404
405   /* go to byte boundary */
406   n = k & 7;
407   ZIPDUMPBITS(n);
408
409   /* get the length and its complement */
410   ZIPNEEDBITS(16)
411   n = ((uint32_t)b & 0xffff);
412   ZIPDUMPBITS(16)
413   ZIPNEEDBITS(16)
414   if (n != (uint32_t)((~b) & 0xffff))
415     return 1;                   /* error in compressed data */
416   ZIPDUMPBITS(16)
417
418   /* read and output the compressed data */
419   while(n--)
420   {
421     ZIPNEEDBITS(8)
422     CAB(outbuf)[w++] = (uint8_t)b;
423     ZIPDUMPBITS(8)
424   }
425
426   /* restore the globals from the locals */
427   ZIP(window_posn) = w;              /* restore global window pointer */
428   ZIP(bb) = b;                       /* restore global bit buffer */
429   ZIP(bk) = k;
430   return 0;
431 }
432
433 static int32_t Zipinflate_fixed(struct decomp_state *decomp_state)
434 {
435   struct Ziphuft *fixed_tl;
436   struct Ziphuft *fixed_td;
437   int32_t fixed_bl, fixed_bd;
438   int32_t i;                /* temporary variable */
439   uint32_t *l;
440
441   l = ZIP(ll);
442
443   /* literal table */
444   for(i = 0; i < 144; i++)
445     l[i] = 8;
446   for(; i < 256; i++)
447     l[i] = 9;
448   for(; i < 280; i++)
449     l[i] = 7;
450   for(; i < 288; i++)          /* make a complete, but wrong code set */
451     l[i] = 8;
452   fixed_bl = 7;
453   if((i = Ziphuft_build(decomp_state, l, 288, 257, Zipcplens, Zipcplext, &fixed_tl, &fixed_bl)))
454     return i;
455
456   /* distance table */
457   for(i = 0; i < 30; i++)      /* make an incomplete code set */
458     l[i] = 5;
459   fixed_bd = 5;
460   if((i = Ziphuft_build(decomp_state, l, 30, 0, Zipcpdist, Zipcpdext, &fixed_td, &fixed_bd)) > 1)
461   {
462     Ziphuft_free(fixed_tl);
463     return i;
464   }
465
466   /* decompress until an end-of-block code */
467   i = Zipinflate_codes(decomp_state, fixed_tl, fixed_td, fixed_bl, fixed_bd);
468
469   Ziphuft_free(fixed_td);
470   Ziphuft_free(fixed_tl);
471   return i;
472 }
473
474 /* decompress an inflated type 2 (dynamic Huffman codes) block. */
475 static int32_t Zipinflate_dynamic(struct decomp_state *decomp_state)
476 {
477   int32_t i;            /* temporary variables */
478   uint32_t j;
479   uint32_t *ll;
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 */
492
493   /* make local bit buffer */
494   b = ZIP(bb);
495   k = ZIP(bk);
496   ll = ZIP(ll);
497
498   /* read in table lengths */
499   ZIPNEEDBITS(5)
500   nl = 257 + ((uint32_t)b & 0x1f);      /* number of literal/length codes */
501   ZIPDUMPBITS(5)
502   ZIPNEEDBITS(5)
503   nd = 1 + ((uint32_t)b & 0x1f);        /* number of distance codes */
504   ZIPDUMPBITS(5)
505   ZIPNEEDBITS(4)
506   nb = 4 + ((uint32_t)b & 0xf);         /* number of bit length codes */
507   ZIPDUMPBITS(4)
508   if(nl > 288 || nd > 32)
509     return 1;                   /* bad lengths */
510
511   /* read in bit-length-code lengths */
512   for(j = 0; j < nb; j++)
513   {
514     ZIPNEEDBITS(3)
515     ll[Zipborder[j]] = (uint32_t)b & 7;
516     ZIPDUMPBITS(3)
517   }
518   for(; j < 19; j++)
519     ll[Zipborder[j]] = 0;
520
521   /* build decoding table for trees--single level, 7 bit lookup */
522   bl = 7;
523   if((i = Ziphuft_build(decomp_state, ll, 19, 19, NULL, NULL, &tl, &bl)) != 0)
524   {
525     if(i == 1)
526       Ziphuft_free(tl);
527     return i;                   /* incomplete code set */
528   }
529
530   /* read in literal and distance code lengths */
531   n = nl + nd;
532   m = Zipmask[bl];
533   i = l = 0;
534   while((uint32_t)i < n)
535   {
536     ZIPNEEDBITS((uint32_t)bl)
537     j = (td = tl + ((uint32_t)b & m))->b;
538     ZIPDUMPBITS(j)
539     j = td->v.n;
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 */
543     {
544       ZIPNEEDBITS(2)
545       j = 3 + ((uint32_t)b & 3);
546       ZIPDUMPBITS(2)
547       if((uint32_t)i + j > n)
548         return 1;
549       while (j--)
550         ll[i++] = l;
551     }
552     else if (j == 17)           /* 3 to 10 zero length codes */
553     {
554       ZIPNEEDBITS(3)
555       j = 3 + ((uint32_t)b & 7);
556       ZIPDUMPBITS(3)
557       if ((uint32_t)i + j > n)
558         return 1;
559       while (j--)
560         ll[i++] = 0;
561       l = 0;
562     }
563     else                        /* j == 18: 11 to 138 zero length codes */
564     {
565       ZIPNEEDBITS(7)
566       j = 11 + ((uint32_t)b & 0x7f);
567       ZIPDUMPBITS(7)
568       if ((uint32_t)i + j > n)
569         return 1;
570       while (j--)
571         ll[i++] = 0;
572       l = 0;
573     }
574   }
575
576   /* free decoding table for trees */
577   Ziphuft_free(tl);
578
579   /* restore the global bit buffer */
580   ZIP(bb) = b;
581   ZIP(bk) = k;
582
583   /* build the decoding tables for literal/length and distance codes */
584   bl = ZIPLBITS;
585   if((i = Ziphuft_build(decomp_state, ll, nl, 257, Zipcplens, Zipcplext, &tl, &bl)) != 0)
586   {
587     if(i == 1)
588       Ziphuft_free(tl);
589     return i;                   /* incomplete code set */
590   }
591   bd = ZIPDBITS;
592   Ziphuft_build(decomp_state, ll + nl, nd, 0, Zipcpdist, Zipcpdext, &td, &bd);
593
594   /* decompress until an end-of-block code */
595   if(Zipinflate_codes(decomp_state, tl, td, bl, bd))
596     return 1;
597
598   /* free the decoding tables, return */
599   Ziphuft_free(tl);
600   Ziphuft_free(td);
601   return 0;
602 }
603
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 */
610
611   DEBUG(10,("Zipinflate_block\n"));
612
613   /* make local bit buffer */
614   b = ZIP(bb);
615   k = ZIP(bk);
616
617   /* read in last block bit */
618   ZIPNEEDBITS(1)
619   *e = (int32_t)b & 1;
620   ZIPDUMPBITS(1)
621
622   /* read in block type */
623   ZIPNEEDBITS(2)
624   t = (uint32_t)b & 3;
625   ZIPDUMPBITS(2)
626
627   /* restore the global bit buffer */
628   ZIP(bb) = b;
629   ZIP(bk) = k;
630
631   DEBUG(10,("inflate type %d\n", t));
632
633   /* inflate that block type */
634   if(t == 2)
635     return Zipinflate_dynamic(decomp_state);
636   if(t == 0)
637     return Zipinflate_stored(decomp_state);
638   if(t == 1)
639     return Zipinflate_fixed(decomp_state);
640   /* bad block type */
641   return 2;
642 }
643
644 _PUBLIC_ struct decomp_state *ZIPdecomp_state(TALLOC_CTX *mem_ctx)
645 {
646         return talloc_zero(mem_ctx, struct decomp_state);
647 }
648
649 int ZIPdecompress(struct decomp_state *decomp_state, DATA_BLOB *inbuf, DATA_BLOB *outbuf)
650 {
651         int32_t e = 0;/* last block flag */
652
653         ZIP(inpos) = CAB(inbuf);
654         ZIP(bb) = ZIP(bk) = ZIP(window_posn) = 0;
655
656         if (inbuf->length > sizeof(decomp_state->inbuf)) return DECR_INPUT;
657
658         if (outbuf->length > sizeof(decomp_state->outbuf)) return DECR_OUTPUT;
659
660         if (outbuf->length > ZIPWSIZE) return DECR_DATAFORMAT;
661
662         memcpy(decomp_state->inbuf, inbuf->data, inbuf->length);
663
664         /* CK = Chris Kirmse, official Microsoft purloiner */
665         if (ZIP(inpos)[0] != 'C' || ZIP(inpos)[1] != 'K') return DECR_ILLEGALDATA;
666         ZIP(inpos) += 2;
667
668         while (!e) {
669                 if (Zipinflate_block(decomp_state, &e)) {
670                         return DECR_ILLEGALDATA;
671                 }
672         }
673
674         memcpy(outbuf->data, decomp_state->outbuf, outbuf->length);
675
676         return DECR_OK;
677 }