1 // $Id: unzip.cpp,v 1.12 2002/11/27 00:18:32 ericb Exp $
2
3 //
4 // NOTE: Jikes incorporates compression code from the Info-ZIP
5 // group. There are no extra charges or costs due to the use of
6 // this code, and the original compression sources are freely
7 // available from http://www.cdrom/com/pub/infozip/ or
8 // ftp://ftp.cdrom.com/pub/infozip/ on the Internet.
9 // The sole use by Jikes of this compression code is contained in the
10 // files unzip.h and unzip.cpp, which are based on Info-ZIP's inflate.c and
11 // associated header files.
12 //
13
14 //
15 // You can do whatever you like with this source file, though I would
16 // prefer that if you modify it and redistribute it that you include
17 // comments to that effect with your name and the date. Thank you.
18 // The abbreviated History list below includes the work of the
19 // following:
20 // M. Adler, G. Roelofs, J-l. Failly, J. Bush, C. Ghisler, A. Verheijen,
21 // P. Kienitz, C. Spieler, S. Maxwell, J. Altman
22 // Only the first and last entries from the original inflate.c are
23 // reproduced here.
24 //
25
26 //
27 // History:
28 // vers date who what
29 // ---- --------- -------------- ------------------------------------
30 // a ~~ Feb 92 M. Adler used full (large, one-step) lookup table
31 // ...
32 // c16 20 Apr 97 J. Altman added memzero(v[]) in huft_build()
33 //
34
35 #include "unzip.h"
36
37 #ifdef HAVE_JIKES_NAMESPACE
38 namespace Jikes { // Open namespace Jikes block
39 #endif
40
41 unsigned long Unzip::global_bb; /* bit buffer */
42 unsigned Unzip::global_bk; /* bits in bit buffer */
43
44 unsigned Unzip::global_wp; /* current position in slide */
45 unsigned Unzip::global_hufts; /* huff memory usage */
46 unsigned char Unzip::slide_buffer[32768];
47 struct huft *Unzip::global_fixed_tl; /* inflate static */
48 struct huft *Unzip::global_fixed_td; /* inflate static */
49 int Unzip::global_fixed_bl,
50 Unzip::global_fixed_bd;
51 #ifdef UNIX_FILE_SYSTEM
52 FILE *Unzip::global_file; /* file pointer for zip file */
53 #elif defined(WIN32_FILE_SYSTEM)
54 char *Unzip::global_file; /* file pointer for zip file */
55 #endif
56 char *Unzip::global_bufferp; /* current position in output buffer */
57
58 /* Tables for deflate from PKZIP's appnote.txt. */
59 unsigned Unzip::border[] = { /* Order of the bit length code lengths */
60 16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15};
61 unsigned short Unzip::cplens[] = { /* Copy lengths for literal codes 257..285 */
62 3, 4, 5, 6, 7, 8, 9, 10, 11, 13, 15, 17, 19, 23, 27, 31,
63 35, 43, 51, 59, 67, 83, 99, 115, 131, 163, 195, 227, 258, 0, 0};
64 /* note: see note #13 above about the 258 in this list. */
65 unsigned short Unzip::cplext[] = { /* Extra bits for literal codes 257..285 */
66 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2,
67 3, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5, 0, 99, 99}; /* 99==invalid */
68 unsigned short Unzip::cpdist[] = { /* Copy offsets for distance codes 0..29 */
69 1, 2, 3, 4, 5, 7, 9, 13, 17, 25, 33, 49, 65, 97, 129, 193,
70 257, 385, 513, 769, 1025, 1537, 2049, 3073, 4097, 6145,
71 8193, 12289, 16385, 24577};
72 unsigned short Unzip::cpdext[] = { /* Extra bits for distance codes */
73 0, 0, 0, 0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6,
74 7, 7, 8, 8, 9, 9, 10, 10, 11, 11,
75 12, 12, 13, 13};
76
77
78 /* moved to consts.h (included in unzip.c), resp. funzip.c */
79 /* And'ing with mask_bits[n] masks the lower n bits */
80 unsigned short Unzip::mask_bits[] = {
81 0x0000,
82 0x0001, 0x0003, 0x0007, 0x000f, 0x001f, 0x003f, 0x007f, 0x00ff,
83 0x01ff, 0x03ff, 0x07ff, 0x0fff, 0x1fff, 0x3fff, 0x7fff, 0xffff
84 };
85
86 int Unzip::lbits = 9; /* bits in base literal/length lookup table */
87 int Unzip::dbits = 6; /* bits in base distance lookup table */
88
89 struct huft *fixed_tl = (struct huft *) 0;
90
huft_build(unsigned * b,unsigned n,unsigned s,unsigned short * d,unsigned short * e,struct huft ** t,int * m)91 int Unzip::huft_build(unsigned *b,unsigned n, unsigned s, unsigned short *d, unsigned short *e, struct huft **t, int *m)
92 /*unsigned *b code lengths in bits (all assumed <= BMAX) */
93 /*unsigned n number of codes (assumed <= N_MAX) */
94 /*unsigned s number of simple-valued codes (0..s-1) */
95 /* unsigned short *d list of base values for non-simple codes */
96 /*ush *e list of extra bits for non-simple codes */
97 /*struct huft **t result: starting table */
98 /*int *m maximum lookup bits, returns actual */
99 /* Given a list of code lengths and a maximum table size, make a set of
100 tables to decode that set of codes. Return zero on success, one if
101 the given code set is incomplete (the tables are still built in this
102 case), two if the input is invalid (all zero length codes or an
103 oversubscribed set of lengths), and three if not enough memory.
104 The code with value 256 is special, and the tables are constructed
105 so that no bits beyond that code are fetched when that code is
106 decoded. */
107 {
108 unsigned a; /* counter for codes of length k */
109 unsigned c[BMAX+1]; /* bit length count table */
110 unsigned el; /* length of EOB code (value 256) */
111 unsigned f; /* i repeats in table every f entries */
112 int g; /* maximum code length */
113 int h; /* table level */
114 register unsigned i; /* counter, current code */
115 register unsigned j; /* counter */
116 register int k; /* number of bits in current code */
117 int lx[BMAX+1]; /* memory for l[-1..BMAX-1] */
118 int *l = lx+1; /* stack of bits per table */
119 register unsigned *p; /* pointer into c[], b[], or v[] */
120 register struct huft *q; /* points to current table */
121 struct huft r; /* table entry for structure assignment */
122 struct huft *u[BMAX]; /* table stack */
123 unsigned v[N_MAX]; /* values in order of bit length */
124 register int w; /* bits before this table == (l * h) */
125 unsigned x[BMAX+1]; /* bit offsets, then code stack */
126 unsigned *xp; /* pointer into x */
127 int y; /* number of dummy codes added */
128 unsigned z; /* number of entries in current table */
129
130
131 /* Generate counts for each bit length */
132 el = n > 256 ? b[256] : BMAX; /* set length of EOB code, if any */
133 memset((char *)c, 0, sizeof(c));
134 p = b;
135 i = n;
136 do {
137 c[*p]++;
138 p++; /* assume all entries <= BMAX */
139 } while (--i);
140 if (c[0] == n) /* null input--all zero length codes */
141 {
142 *t = (struct huft *)0;
143 *m = 0;
144 return 0;
145 }
146
147
148 /* Find minimum and maximum length, bound *m by those */
149 for (j = 1; j <= BMAX; j++)
150 if (c[j])
151 break;
152 k = j; /* minimum code length */
153 if ((unsigned)*m < j)
154 *m = j;
155 for (i = BMAX; i; i--)
156 if (c[i])
157 break;
158 g = i; /* maximum code length */
159 if ((unsigned)*m > i)
160 *m = i;
161
162
163 /* Adjust last length count to fill out codes, if needed */
164 for (y = 1 << j; j < i; j++, y <<= 1)
165 if ((y -= c[j]) < 0)
166 return 2; /* bad input: more codes than bits */
167 if ((y -= c[i]) < 0)
168 return 2;
169 c[i] += y;
170
171
172 /* Generate starting offsets into the value table for each length */
173 x[1] = j = 0;
174 p = c + 1; xp = x + 2;
175 while (--i) { /* note that i == g from above */
176 *xp++ = (j += *p++);
177 }
178
179
180 /* Make a table of values in order of bit lengths */
181 memset((char *) v, 0, sizeof(v));
182 p = b;
183 i = 0;
184 do {
185 if ((j = *p++) != 0)
186 v[x[j]++] = i;
187 } while (++i < n);
188 n = x[g]; /* set n to length of v */
189
190
191 /* Generate the Huffman codes and for each, make the table entries */
192 x[0] = i = 0; /* first Huffman code is zero */
193 p = v; /* grab values in bit order */
194 h = -1; /* no tables yet--level -1 */
195 w = l[-1] = 0; /* no bits decoded yet */
196 u[0] = (struct huft *)0; /* just to keep compilers happy */
197 q = (struct huft *)0; /* ditto */
198 z = 0; /* ditto */
199
200 /* go through the bit lengths (k already is bits in shortest code) */
201 for (; k <= g; k++)
202 {
203 a = c[k];
204 while (a--)
205 {
206 /* here i is the Huffman code of length k bits for value *p */
207 /* make tables up to required level */
208 while (k > w + l[h])
209 {
210 w += l[h++]; /* add bits already decoded */
211
212 /* compute minimum size table less than or equal to *m bits */
213 z = (z = g - w) > (unsigned)*m ? *m : z; /* upper limit */
214 if ((f = 1 << (j = k - w)) > a + 1) /* try a k-w bit table */
215 { /* too few codes for k-w bit table */
216 f -= a + 1; /* deduct codes from patterns left */
217 xp = c + k;
218 while (++j < z) /* try smaller tables up to z bits */
219 {
220 if ((f <<= 1) <= *++xp)
221 break; /* enough codes to use up j bits */
222 f -= *xp; /* else deduct codes from patterns */
223 }
224 }
225 if ((unsigned)w + j > el && (unsigned)w < el)
226 j = el - w; /* make EOB code end at table */
227 z = 1 << j; /* table entries for j-bit table */
228 l[h] = j; /* set table size in stack */
229
230 /* allocate and link in new table */
231 if ((q = (struct huft *)malloc((z + 1)*sizeof(struct huft))) == (struct huft *)0)
232 {
233 if (h)
234 huft_free(u[0]);
235 return 3; /* not enough memory */
236 }
237 global_hufts += z + 1; /* track memory usage */
238 *t = q + 1; /* link to list for huft_free() */
239 *(t = &(q->v.t)) = (struct huft *)0;
240 u[h] = ++q; /* table starts after link */
241
242 /* connect to last table, if there is one */
243 if (h)
244 {
245 x[h] = i; /* save pattern for backing up */
246 r.b = (unsigned char)l[h-1]; /* bits to dump before this table */
247 r.e = (unsigned char)(16 + j); /* bits in this table */
248 r.v.t = q; /* pointer to this table */
249 j = (i & ((1 << w) - 1)) >> (w - l[h-1]);
250 u[h-1][j] = r; /* connect to last table */
251 }
252 }
253
254 /* set up table entry in r */
255 r.b = (unsigned char)(k - w);
256 if (p >= v + n)
257 r.e = 99; /* out of values--invalid code */
258 else if (*p < s)
259 {
260 r.e = (unsigned char)(*p < 256 ? 16 : 15); /* 256 is end-of-block code */
261 r.v.n = (unsigned short)*p++; /* simple code is just the value */
262 }
263 else
264 {
265 r.e = (unsigned char)e[*p - s]; /* non-simple--look up in lists */
266 r.v.n = d[*p++ - s];
267 }
268
269 /* fill code-like entries with r */
270 f = 1 << (k - w);
271 for (j = i >> w; j < z; j += f)
272 q[j] = r;
273
274 /* backwards increment the k-bit code i */
275 for (j = 1 << (k - 1); i & j; j >>= 1)
276 i ^= j;
277 i ^= j;
278
279 /* backup over finished tables */
280 while ((i & ((1 << w) - 1)) != x[h])
281 w -= l[--h]; /* don't need to update q */
282 }
283 }
284
285
286 /* return actual size of base table */
287 *m = l[0];
288
289
290 /* Return true (1) if we were given an incomplete table */
291 return y != 0 && g != 1;
292 }
293
294
huft_free(struct huft * t)295 int Unzip::huft_free(struct huft *t)
296 /*struct huft *t table to free */
297 /* Free the malloc'ed tables built by huft_build(), which makes a linked
298 list of the tables it made, with the links in a dummy first entry of
299 each table. */
300 {
301 register struct huft *p, *q;
302
303
304 /* Go through linked list, freeing from the malloced (t[-1]) address. */
305 p = t;
306 while (p != (struct huft *)0)
307 {
308 q = (--p)->v.t;
309 free(p);
310 p = q;
311 }
312 return 0;
313 }
314
315
inflate_codes(struct huft * tl,struct huft * td,int bl,int bd)316 int Unzip::inflate_codes(struct huft *tl, struct huft * td, int bl, int bd)
317 /*struct huft *tl, *td literal/length and distance decoder tables */
318 /* int bl, bd; number of bits decoded by tl[] and td[] */
319 /* inflate (decompress) the codes in a deflated (compressed) block.
320 Return an error code or zero if it all goes ok. */
321 {
322 register unsigned e; /* table entry flag/number of extra bits */
323 unsigned n, d; /* length and index for copy */
324 unsigned w; /* current window position */
325 struct huft *t; /* pointer to table entry */
326 unsigned ml, md; /* masks for bl and bd bits */
327 register unsigned long b; /* bit buffer */
328 register unsigned k; /* number of bits in bit buffer */
329
330
331 /* make local copies of globals */
332 b = global_bb; /* initialize bit buffer */
333 k = global_bk;
334 w = global_wp; /* initialize window position */
335
336
337 /* inflate the coded data */
338 ml = mask_bits[bl]; /* precompute masks for speed */
339 md = mask_bits[bd];
340 while (1) /* do until end of block */
341 {
342 NEEDBITS((unsigned)bl)
343 if ((e = (t = tl + ((unsigned)b & ml))->e) > 16)
344 do {
345 if (e == 99)
346 return 1;
347 DUMPBITS(t->b)
348 e -= 16;
349 NEEDBITS(e)
350 } while ((e = (t = t->v.t + ((unsigned)b & mask_bits[e]))->e) > 16);
351 DUMPBITS(t->b)
352 if (e == 16) /* then it's a literal */
353 {
354 slide_buffer[w++] = (unsigned char)t->v.n;
355 if (w == wsize)
356 {
357 FLUSH(w);
358 w = 0;
359 }
360 }
361 else /* it's an EOB or a length */
362 {
363 /* exit if end of block */
364 if (e == 15)
365 break;
366
367 /* get length of block to copy */
368 NEEDBITS(e)
369 n = t->v.n + ((unsigned)b & mask_bits[e]);
370 DUMPBITS(e);
371
372 /* decode distance of block to copy */
373 NEEDBITS((unsigned)bd)
374 if ((e = (t = td + ((unsigned)b & md))->e) > 16)
375 do {
376 if (e == 99)
377 return 1;
378 DUMPBITS(t->b)
379 e -= 16;
380 NEEDBITS(e)
381 } while ((e = (t = t->v.t + ((unsigned)b & mask_bits[e]))->e) > 16);
382 DUMPBITS(t->b)
383 NEEDBITS(e)
384 d = w - t->v.n - ((unsigned)b & mask_bits[e]);
385 DUMPBITS(e)
386
387 /* do the copy */
388 do {
389 n -= (e = (e = wsize - ((d &= wsize-1) > w ? d : w)) > n ? n : e);
390 #ifndef NOMEMCPY
391 if (w - d >= e) /* (this test assumes unsigned comparison) */
392 {
393 memmove(slide_buffer + w, slide_buffer + d, e);
394 w += e;
395 d += e;
396 }
397 else /* do it slowly to avoid memcpy() overlap */
398 #endif /* !NOMEMCPY */
399 do {
400 slide_buffer[w++] = slide_buffer[d++];
401 } while (--e);
402 if (w == wsize)
403 {
404 FLUSH(w);
405 w = 0;
406 }
407 } while (n);
408 }
409 }
410
411
412 /* restore the globals from the locals */
413 global_wp = w; /* restore global window pointer */
414 global_bb = b; /* restore global bit buffer */
415 global_bk = k;
416
417
418 /* done */
419 return 0;
420 }
421
422
inflate_stored()423 int Unzip::inflate_stored()
424 /* "decompress" an inflated type 0 (stored) block. */
425 {
426 unsigned n; /* number of bytes in block */
427 unsigned w; /* current window position */
428 register unsigned long b; /* bit buffer */
429 register unsigned k; /* number of bits in bit buffer */
430
431
432 /* make local copies of globals */
433 Trace((stderr, "\nstored block"));
434 b = global_bb; /* initialize bit buffer */
435 k = global_bk;
436 w = global_wp; /* initialize window position */
437
438
439 /* go to byte boundary */
440 n = k & 7;
441 DUMPBITS(n);
442
443
444 /* get the length and its complement */
445 NEEDBITS(16)
446 n = ((unsigned)b & 0xffff);
447 DUMPBITS(16)
448 NEEDBITS(16)
449 if (n != (unsigned)((~b) & 0xffff))
450 return 1; /* error in compressed data */
451 DUMPBITS(16)
452
453
454 /* read and output the compressed data */
455 while (n--)
456 {
457 NEEDBITS(8)
458 slide_buffer[w++] = (unsigned char)b;
459 if (w == wsize)
460 {
461 FLUSH(w);
462 w = 0;
463 }
464 DUMPBITS(8)
465 }
466
467
468 /* restore the globals from the locals */
469 global_wp = w; /* restore global window pointer */
470 global_bb = b; /* restore global bit buffer */
471 global_bk = k;
472 return 0;
473 }
474
475
inflate_fixed()476 int Unzip::inflate_fixed()
477 /* decompress an inflated type 1 (fixed Huffman codes) block. We should
478 either replace this with a custom decoder, or at least precompute the
479 Huffman tables. */
480 {
481 /* if first time, set up tables for fixed blocks */
482 Trace((stderr, "\nliteral block"));
483 if (global_fixed_tl == (struct huft *)0)
484 {
485 int i; /* temporary variable */
486 unsigned l[288]; /* length list for huft_build */
487
488 /* literal table */
489 for (i = 0; i < 144; i++)
490 l[i] = 8;
491 for (; i < 256; i++)
492 l[i] = 9;
493 for (; i < 280; i++)
494 l[i] = 7;
495 for (; i < 288; i++) /* make a complete, but wrong code set */
496 l[i] = 8;
497 global_fixed_bl = 7;
498 if ((i = huft_build(l, 288, 257, cplens, cplext,
499 &global_fixed_tl, &global_fixed_bl)) != 0)
500 {
501 global_fixed_tl = (struct huft *)0;
502 return i;
503 }
504
505 /* distance table */
506 for (i = 0; i < 30; i++) /* make an incomplete code set */
507 l[i] = 5;
508 global_fixed_bd = 5;
509 if ((i = huft_build(l, 30, 0, cpdist, cpdext,
510 &global_fixed_td, &global_fixed_bd)) > 1)
511 {
512 huft_free(global_fixed_tl);
513 global_fixed_tl = (struct huft *)0;
514 return i;
515 }
516 }
517
518 /* decompress until an end-of-block code */
519 return inflate_codes(global_fixed_tl, global_fixed_td,
520 global_fixed_bl, global_fixed_bd) != 0;
521 }
522
523
524
inflate_dynamic()525 int Unzip::inflate_dynamic()
526 /* decompress an inflated type 2 (dynamic Huffman codes) block. */
527 {
528 int i; /* temporary variables */
529 unsigned j;
530 unsigned l; /* last length */
531 unsigned m; /* mask for bit lengths table */
532 unsigned n; /* number of lengths to get */
533 struct huft *tl; /* literal/length code table */
534 struct huft *td; /* distance code table */
535 int bl; /* lookup bits for tl */
536 int bd; /* lookup bits for td */
537 unsigned nb; /* number of bit length codes */
538 unsigned nl; /* number of literal/length codes */
539 unsigned nd; /* number of distance codes */
540 #ifdef PKZIP_BUG_WORKAROUND
541 unsigned ll[288+32]; /* literal/length and distance code lengths */
542 #else
543 unsigned ll[286+30]; /* literal/length and distance code lengths */
544 #endif
545 register unsigned long b; /* bit buffer */
546 register unsigned k; /* number of bits in bit buffer */
547
548
549 /* make local bit buffer */
550 Trace((stderr, "\ndynamic block"));
551 b = global_bb;
552 k = global_bk;
553
554
555 /* read in table lengths */
556 NEEDBITS(5)
557 nl = 257 + ((unsigned)b & 0x1f); /* number of literal/length codes */
558 DUMPBITS(5)
559 NEEDBITS(5)
560 nd = 1 + ((unsigned)b & 0x1f); /* number of distance codes */
561 DUMPBITS(5)
562 NEEDBITS(4)
563 nb = 4 + ((unsigned)b & 0xf); /* number of bit length codes */
564 DUMPBITS(4)
565 #ifdef PKZIP_BUG_WORKAROUND
566 if (nl > 288 || nd > 32)
567 #else
568 if (nl > 286 || nd > 30)
569 #endif
570 return 1; /* bad lengths */
571
572
573 /* read in bit-length-code lengths */
574 for (j = 0; j < nb; j++)
575 {
576 NEEDBITS(3)
577 ll[border[j]] = (unsigned)b & 7;
578 DUMPBITS(3)
579 }
580 for (; j < 19; j++)
581 ll[border[j]] = 0;
582
583
584 /* build decoding table for trees--single level, 7 bit lookup */
585 bl = 7;
586 i = huft_build(ll, 19, 19, 0, 0, &tl, &bl);
587 if (bl == 0) /* no bit lengths */
588 i = 1;
589 if (i)
590 {
591 if (i == 1)
592 huft_free(tl);
593 return i; /* incomplete code set */
594 }
595
596
597 /* read in literal and distance code lengths */
598 n = nl + nd;
599 m = mask_bits[bl];
600 i = l = 0;
601 while ((unsigned)i < n)
602 {
603 NEEDBITS((unsigned)bl)
604 j = (td = tl + ((unsigned)b & m))->b;
605 DUMPBITS(j)
606 j = td->v.n;
607 if (j < 16) /* length of code in bits (0..15) */
608 ll[i++] = l = j; /* save last length in l */
609 else if (j == 16) /* repeat last length 3 to 6 times */
610 {
611 NEEDBITS(2)
612 j = 3 + ((unsigned)b & 3);
613 DUMPBITS(2)
614 if ((unsigned)i + j > n)
615 return 1;
616 while (j--)
617 ll[i++] = l;
618 }
619 else if (j == 17) /* 3 to 10 zero length codes */
620 {
621 NEEDBITS(3)
622 j = 3 + ((unsigned)b & 7);
623 DUMPBITS(3)
624 if ((unsigned)i + j > n)
625 return 1;
626 while (j--)
627 ll[i++] = 0;
628 l = 0;
629 }
630 else /* j == 18: 11 to 138 zero length codes */
631 {
632 NEEDBITS(7)
633 j = 11 + ((unsigned)b & 0x7f);
634 DUMPBITS(7)
635 if ((unsigned)i + j > n)
636 return 1;
637 while (j--)
638 ll[i++] = 0;
639 l = 0;
640 }
641 }
642
643
644 /* free decoding table for trees */
645 huft_free(tl);
646
647
648 /* restore the global bit buffer */
649 global_bb = b;
650 global_bk = k;
651
652
653 /* build the decoding tables for literal/length and distance codes */
654 bl = lbits;
655 i = huft_build(ll, nl, 257, cplens, cplext, &tl, &bl);
656 if (bl == 0) /* no literals or lengths */
657 i = 1;
658 if (i)
659 {
660 if (i == 1) {
661 MESSAGE((unsigned char *)"(incomplete l-tree) ", 21L, 1);
662 huft_free(tl);
663 }
664 return i; /* incomplete code set */
665 }
666 bd = dbits;
667 i = huft_build(ll + nl, nd, 0, cpdist, cpdext, &td, &bd);
668 if (bd == 0 && nl > 257) /* lengths but no distances */
669 {
670 MESSAGE((unsigned char *)"(incomplete d-tree) ", 21L, 1);
671 huft_free(tl);
672 return 1;
673 }
674 if (i == 1) {
675 #ifdef PKZIP_BUG_WORKAROUND
676 i = 0;
677 #else
678 if (!G.qflag)
679 MESSAGE((unsigned char *)"(incomplete d-tree) ", 21L, 1);
680 huft_free(td);
681 #endif
682 }
683 if (i)
684 {
685 huft_free(tl);
686 return i;
687 }
688
689
690 /* decompress until an end-of-block code */
691 if (inflate_codes(tl, td, bl, bd))
692 return 1;
693
694
695 /* free the decoding tables, return */
696 huft_free(tl);
697 huft_free(td);
698 return 0;
699 }
700
701
702
inflate_block(int * e)703 int Unzip::inflate_block(int *e)
704 /* int *e last block flag */
705 /* decompress an inflated block */
706 {
707 unsigned t; /* block type */
708 register unsigned long b; /* bit buffer */
709 register unsigned k; /* number of bits in bit buffer */
710
711
712 /* make local bit buffer */
713 b = global_bb;
714 k = global_bk;
715
716
717 /* read in last block bit */
718 NEEDBITS(1)
719 *e = (int)b & 1;
720 DUMPBITS(1)
721
722
723 /* read in block type */
724 NEEDBITS(2)
725 t = (unsigned)b & 3;
726 DUMPBITS(2)
727
728
729 /* restore the global bit buffer */
730 global_bb = b;
731 global_bk = k;
732
733
734 /* inflate that block type */
735 if (t == 2)
736 return inflate_dynamic();
737 if (t == 0)
738 return inflate_stored();
739 if (t == 1)
740 return inflate_fixed();
741
742
743 /* bad block type */
744 return 2;
745 }
746
747
inflate_free()748 int Unzip::inflate_free()
749 {
750 if (global_fixed_tl != (struct huft *)0)
751 {
752 huft_free(global_fixed_td);
753 huft_free(global_fixed_tl);
754 global_fixed_td = global_fixed_tl = (struct huft *)0;
755 }
756 return 0;
757 }
758
759 #ifdef UNIX_FILE_SYSTEM
unzip8(FILE * zipfile,char * buffer)760 int Unzip::unzip8(FILE * zipfile, char *buffer)
761 #elif defined(WIN32_FILE_SYSTEM)
762 int Unzip::unzip8(char *zipfile, char *buffer)
763 #endif
764 /* decompress an inflated entry */
765 {
766 int e; /* last block flag */
767 int r; /* result code */
768 unsigned h; /* maximum struct huft's malloc'ed */
769
770 /* initialize window, bit buffer */
771 global_wp = 0;
772 global_bk = 0;
773 global_bb = 0;
774 global_file = zipfile;
775 global_bufferp = buffer;
776
777
778 /* decompress until the last block */
779 h = 0;
780 do {
781 global_hufts = 0;
782 if ((r = inflate_block(&e)) != 0)
783 return r;
784 if (global_hufts > h)
785 h = global_hufts;
786 } while (!e);
787
788
789 /* flush out slide_buffer */
790 FLUSH(global_wp);
791
792
793 /* return success */
794 Trace((stderr, "\n%u bytes in Huffman tables (%d/entry)\n",
795 h * sizeof(struct huft), sizeof(struct huft)));
796 return 0;
797 }
798
799
800 #ifdef UNIX_FILE_SYSTEM
UncompressFile0(FILE * zipfile,char * buffer,long buffer_length)801 int Unzip::UncompressFile0(FILE* zipfile, char* buffer, long buffer_length)
802 {
803 SystemFread(buffer, sizeof(char), buffer_length, zipfile);
804 return 1;
805 }
806
UncompressFile1(FILE *,char *,long)807 int Unzip::UncompressFile1(FILE*, char*, long) { return 0; }
UncompressFile2(FILE *,char *,long)808 int Unzip::UncompressFile2(FILE*, char*, long) { return 0; }
UncompressFile3(FILE *,char *,long)809 int Unzip::UncompressFile3(FILE*, char*, long) { return 0; }
UncompressFile4(FILE *,char *,long)810 int Unzip::UncompressFile4(FILE*, char*, long) { return 0; }
UncompressFile5(FILE *,char *,long)811 int Unzip::UncompressFile5(FILE*, char*, long) { return 0; }
UncompressFile6(FILE *,char *,long)812 int Unzip::UncompressFile6(FILE*, char*, long) { return 0; }
UncompressFile7(FILE *,char *,long)813 int Unzip::UncompressFile7(FILE*, char*, long) { return 0; }
814
UncompressFile8(FILE * zipfile,char * buffer,long)815 int Unzip::UncompressFile8(FILE* zipfile, char* buffer, long)
816 {
817 int rc = Unzip::unzip8(zipfile, buffer); /* Use Unzip routine to unpack */
818 return (rc == 0);
819 }
820
UncompressFile9(FILE *,char *,long)821 int Unzip::UncompressFile9(FILE*, char*, long) { return 0; }
822
823 #elif defined(WIN32_FILE_SYSTEM)
UncompressFile0(char * zipfile_buffer,char * buffer,long buffer_length)824 int Unzip::UncompressFile0(char* zipfile_buffer, char* buffer,
825 long buffer_length)
826 {
827 memmove(buffer, zipfile_buffer, buffer_length * sizeof(char));
828 return 1;
829 }
830
UncompressFile1(char *,char *,long)831 int Unzip::UncompressFile1(char*, char*, long) { return 0; }
UncompressFile2(char *,char *,long)832 int Unzip::UncompressFile2(char*, char*, long) { return 0; }
UncompressFile3(char *,char *,long)833 int Unzip::UncompressFile3(char*, char*, long) { return 0; }
UncompressFile4(char *,char *,long)834 int Unzip::UncompressFile4(char*, char*, long) { return 0; }
UncompressFile5(char *,char *,long)835 int Unzip::UncompressFile5(char*, char*, long) { return 0; }
UncompressFile6(char *,char *,long)836 int Unzip::UncompressFile6(char*, char*, long) { return 0; }
UncompressFile7(char *,char *,long)837 int Unzip::UncompressFile7(char*, char*, long) { return 0; }
838
UncompressFile8(char * zipfile_buffer,char * buffer,long)839 int Unzip::UncompressFile8(char* zipfile_buffer, char* buffer, long)
840 {
841 /* Use Unzip routine to unpack */
842 int rc = Unzip::unzip8(zipfile_buffer, buffer);
843 return (rc == 0);
844 }
845
UncompressFile9(char *,char *,long)846 int Unzip::UncompressFile9(char*, char*, long) { return 0; }
847 #endif // WIN32_FILE_SYSTEM
848
849
850 #ifdef HAVE_JIKES_NAMESPACE
851 } // Close namespace Jikes block
852 #endif
853
854