move dcerpc.idl to main directory.
[Samba/gbeck.git] / lib / compression / mszip.c
blob9aa1772414bcbd0bc376e234799338654b161f4f
1 /* mszip decompression - based on cabextract.c code from
2 * Stuart Caie
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/>.
23 #include "includes.h"
24 #include "../compression/mszip.h"
26 /*--------------------------------------------------------------------------*/
27 /* our archiver information / state */
29 /* MSZIP stuff */
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 */
36 struct Ziphuft {
37 uint8_t e; /* number of extra bits or operation */
38 uint8_t b; /* number of bits in this code or subcode */
39 union {
40 uint16_t n; /* literal, length base, or distance base */
41 struct Ziphuft *t; /* pointer to next level of table */
42 } v;
45 struct ZIPstate {
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 */
55 uint8_t *inpos;
58 /* generic stuff */
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)
70 struct decomp_state {
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];
79 union {
80 struct ZIPstate zip;
81 } methods;
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. */
120 p = t;
121 while (p != (struct Ziphuft *)NULL)
123 q = (--p)->v.t;
124 free(p);
125 p = q;
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 */
150 l = ZIP(lx)+1;
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)
156 ZIP(c)[i] = 0;
157 p = b; i = n;
160 ZIP(c)[*p]++; p++; /* assume all entries <= ZIPBMAX */
161 } while (--i);
162 if (ZIP(c)[0] == n) /* null input--all zero length codes */
164 *t = (struct Ziphuft *)NULL;
165 *m = 0;
166 return 0;
169 /* Find minimum and maximum length, bound *m by those */
170 for (j = 1; j <= ZIPBMAX; j++)
171 if (ZIP(c)[j])
172 break;
173 k = j; /* minimum code length */
174 if ((uint32_t)*m < j)
175 *m = j;
176 for (i = ZIPBMAX; i; i--)
177 if (ZIP(c)[i])
178 break;
179 g = i; /* maximum code length */
180 if ((uint32_t)*m > i)
181 *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)
188 return 2;
189 ZIP(c)[i] += y;
191 /* Generate starting offsets int32_to the value table for each length */
192 ZIP(x)[1] = j = 0;
193 p = ZIP(c) + 1; xp = ZIP(x) + 2;
194 while (--i)
195 { /* note that i == g from above */
196 *xp++ = (j += *p++);
199 /* Make a table of values in order of bit lengths */
200 p = b; i = 0;
202 if ((j = *p++) != 0)
203 ZIP(v)[ZIP(x)[j]++] = i;
204 } while (++i < n);
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 */
214 z = 0; /* ditto */
216 /* go through the bit lengths (k already is bits in shortest code) */
217 for (; k <= g; k++)
219 a = ZIP(c)[k];
220 while (a--)
222 /* here i is the Huffman code of length k bits for value *p */
223 /* make tables up to required level */
224 while (k > w + l[h])
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 */
233 xp = ZIP(c) + k;
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))))
249 if(h)
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 */
258 if (h)
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);
271 if (p >= ZIP(v) + n)
272 r.e = 99; /* out of values--invalid code */
273 else if (*p < s)
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 */
278 else
280 r.e = (uint8_t)e[*p - s]; /* non-simple--look up in lists */
281 r.v.n = d[*p++ - s];
284 /* fill code-like entries with r */
285 f = 1 << (k - w);
286 for (j = i >> w; j < z; j += f)
287 q[j] = r;
289 /* backwards increment the k-bit code i */
290 for (j = 1 << (k - 1); i & j; j >>= 1)
291 i ^= j;
292 i ^= j;
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 */
301 *m = l[0];
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 */
323 k = ZIP(bk);
324 w = ZIP(window_posn); /* initialize window position */
326 /* inflate the coded data */
327 ml = Zipmask[bl]; /* precompute masks for speed */
328 md = Zipmask[bd];
330 for(;;)
332 ZIPNEEDBITS((uint32_t)bl)
333 if((e = (t = tl + ((uint32_t)b & ml))->e) > 16)
336 if (e == 99)
337 return 1;
338 ZIPDUMPBITS(t->b)
339 e -= 16;
340 ZIPNEEDBITS(e)
341 } while ((e = (t = t->v.t + ((uint32_t)b & Zipmask[e]))->e) > 16);
342 ZIPDUMPBITS(t->b)
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 */
349 if(e == 15)
350 break;
352 /* get length of block to copy */
353 ZIPNEEDBITS(e)
354 n = t->v.n + ((uint32_t)b & Zipmask[e]);
355 ZIPDUMPBITS(e);
357 /* decode distance of block to copy */
358 ZIPNEEDBITS((uint32_t)bd)
359 if ((e = (t = td + ((uint32_t)b & md))->e) > 16)
360 do {
361 if (e == 99)
362 return 1;
363 ZIPDUMPBITS(t->b)
364 e -= 16;
365 ZIPNEEDBITS(e)
366 } while ((e = (t = t->v.t + ((uint32_t)b & Zipmask[e]))->e) > 16);
367 ZIPDUMPBITS(t->b)
368 ZIPNEEDBITS(e)
369 d = w - t->v.n - ((uint32_t)b & Zipmask[e]);
370 ZIPDUMPBITS(e)
373 n -= (e = (e = ZIPWSIZE - ((d &= ZIPWSIZE-1) > w ? d : w)) > n ?n:e);
376 CAB(outbuf)[w++] = CAB(outbuf)[d++];
377 } while (--e);
378 } while (n);
382 /* restore the globals from the locals */
383 ZIP(window_posn) = w; /* restore global window pointer */
384 ZIP(bb) = b; /* restore global bit buffer */
385 ZIP(bk) = k;
387 /* done */
388 return 0;
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 */
401 k = ZIP(bk);
402 w = ZIP(window_posn); /* initialize window position */
404 /* go to byte boundary */
405 n = k & 7;
406 ZIPDUMPBITS(n);
408 /* get the length and its complement */
409 ZIPNEEDBITS(16)
410 n = ((uint32_t)b & 0xffff);
411 ZIPDUMPBITS(16)
412 ZIPNEEDBITS(16)
413 if (n != (uint32_t)((~b) & 0xffff))
414 return 1; /* error in compressed data */
415 ZIPDUMPBITS(16)
417 /* read and output the compressed data */
418 while(n--)
420 ZIPNEEDBITS(8)
421 CAB(outbuf)[w++] = (uint8_t)b;
422 ZIPDUMPBITS(8)
425 /* restore the globals from the locals */
426 ZIP(window_posn) = w; /* restore global window pointer */
427 ZIP(bb) = b; /* restore global bit buffer */
428 ZIP(bk) = k;
429 return 0;
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 */
438 uint32_t *l;
440 l = ZIP(ll);
442 /* literal table */
443 for(i = 0; i < 144; i++)
444 l[i] = 8;
445 for(; i < 256; i++)
446 l[i] = 9;
447 for(; i < 280; i++)
448 l[i] = 7;
449 for(; i < 288; i++) /* make a complete, but wrong code set */
450 l[i] = 8;
451 fixed_bl = 7;
452 if((i = Ziphuft_build(decomp_state, l, 288, 257, Zipcplens, Zipcplext, &fixed_tl, &fixed_bl)))
453 return i;
455 /* distance table */
456 for(i = 0; i < 30; i++) /* make an incomplete code set */
457 l[i] = 5;
458 fixed_bd = 5;
459 if((i = Ziphuft_build(decomp_state, l, 30, 0, Zipcpdist, Zipcpdext, &fixed_td, &fixed_bd)) > 1)
461 Ziphuft_free(fixed_tl);
462 return i;
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);
470 return i;
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 */
477 uint32_t j;
478 uint32_t *ll;
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 */
493 b = ZIP(bb);
494 k = ZIP(bk);
495 ll = ZIP(ll);
497 /* read in table lengths */
498 ZIPNEEDBITS(5)
499 nl = 257 + ((uint32_t)b & 0x1f); /* number of literal/length codes */
500 ZIPDUMPBITS(5)
501 ZIPNEEDBITS(5)
502 nd = 1 + ((uint32_t)b & 0x1f); /* number of distance codes */
503 ZIPDUMPBITS(5)
504 ZIPNEEDBITS(4)
505 nb = 4 + ((uint32_t)b & 0xf); /* number of bit length codes */
506 ZIPDUMPBITS(4)
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++)
513 ZIPNEEDBITS(3)
514 ll[Zipborder[j]] = (uint32_t)b & 7;
515 ZIPDUMPBITS(3)
517 for(; j < 19; j++)
518 ll[Zipborder[j]] = 0;
520 /* build decoding table for trees--single level, 7 bit lookup */
521 bl = 7;
522 if((i = Ziphuft_build(decomp_state, ll, 19, 19, NULL, NULL, &tl, &bl)) != 0)
524 if(i == 1)
525 Ziphuft_free(tl);
526 return i; /* incomplete code set */
529 /* read in literal and distance code lengths */
530 n = nl + nd;
531 m = Zipmask[bl];
532 i = l = 0;
533 while((uint32_t)i < n)
535 ZIPNEEDBITS((uint32_t)bl)
536 j = (td = tl + ((uint32_t)b & m))->b;
537 ZIPDUMPBITS(j)
538 j = td->v.n;
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 */
543 ZIPNEEDBITS(2)
544 j = 3 + ((uint32_t)b & 3);
545 ZIPDUMPBITS(2)
546 if((uint32_t)i + j > n)
547 return 1;
548 while (j--)
549 ll[i++] = l;
551 else if (j == 17) /* 3 to 10 zero length codes */
553 ZIPNEEDBITS(3)
554 j = 3 + ((uint32_t)b & 7);
555 ZIPDUMPBITS(3)
556 if ((uint32_t)i + j > n)
557 return 1;
558 while (j--)
559 ll[i++] = 0;
560 l = 0;
562 else /* j == 18: 11 to 138 zero length codes */
564 ZIPNEEDBITS(7)
565 j = 11 + ((uint32_t)b & 0x7f);
566 ZIPDUMPBITS(7)
567 if ((uint32_t)i + j > n)
568 return 1;
569 while (j--)
570 ll[i++] = 0;
571 l = 0;
575 /* free decoding table for trees */
576 Ziphuft_free(tl);
578 /* restore the global bit buffer */
579 ZIP(bb) = b;
580 ZIP(bk) = k;
582 /* build the decoding tables for literal/length and distance codes */
583 bl = ZIPLBITS;
584 if((i = Ziphuft_build(decomp_state, ll, nl, 257, Zipcplens, Zipcplext, &tl, &bl)) != 0)
586 if(i == 1)
587 Ziphuft_free(tl);
588 return i; /* incomplete code set */
590 bd = ZIPDBITS;
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))
595 return 1;
597 /* free the decoding tables, return */
598 Ziphuft_free(tl);
599 Ziphuft_free(td);
600 return 0;
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 */
613 b = ZIP(bb);
614 k = ZIP(bk);
616 /* read in last block bit */
617 ZIPNEEDBITS(1)
618 *e = (int32_t)b & 1;
619 ZIPDUMPBITS(1)
621 /* read in block type */
622 ZIPNEEDBITS(2)
623 t = (uint32_t)b & 3;
624 ZIPDUMPBITS(2)
626 /* restore the global bit buffer */
627 ZIP(bb) = b;
628 ZIP(bk) = k;
630 DEBUG(10,("inflate type %d\n", t));
632 /* inflate that block type */
633 if(t == 2)
634 return Zipinflate_dynamic(decomp_state);
635 if(t == 0)
636 return Zipinflate_stored(decomp_state);
637 if(t == 1)
638 return Zipinflate_fixed(decomp_state);
639 /* bad block type */
640 return 2;
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;
665 ZIP(inpos) += 2;
667 while (!e) {
668 if (Zipinflate_block(decomp_state, &e)) {
669 return DECR_ILLEGALDATA;
673 memcpy(outbuf->data, decomp_state->outbuf, outbuf->length);
675 return DECR_OK;