imgact_gzip.c revision 3353
1/* 2 * Parts of this file are not covered by: 3 * ---------------------------------------------------------------------------- 4 * "THE BEER-WARE LICENSE" (Revision 42): 5 * <phk@login.dknet.dk> wrote this file. As long as you retain this notice you 6 * can do whatever you want with this stuff. If we meet some day, and you think 7 * this stuff is worth it, you can buy me a beer in return. Poul-Henning Kamp 8 * ---------------------------------------------------------------------------- 9 * 10 * $Id: imgact_gzip.c,v 1.3 1994/10/04 03:09:13 phk Exp $ 11 * 12 * This module handles execution of a.out files which have been run through 13 * "gzip -9". 14 * 15 * For now you need to use exactly this command to compress the binaries: 16 * 17 * gzip -9 -v < /bin/sh > /tmp/sh 18 * 19 * TODO: 20 * text-segments should be made R/O after being filled 21 * is the vm-stuff safe ? 22 * should handle the entire header of gzip'ed stuff. 23 * inflate isn't quite reentrant yet... 24 * error-handling is a mess... 25 * so is the rest... 26 */ 27 28#include <sys/param.h> 29#include <sys/systm.h> 30#include <sys/resourcevar.h> 31#include <sys/exec.h> 32#include <sys/mman.h> 33#include <sys/malloc.h> 34#include <sys/imgact.h> 35#include <sys/imgact_aout.h> 36#include <sys/kernel.h> 37#include <sys/sysent.h> 38 39#include <vm/vm.h> 40#include <vm/vm_kern.h> 41 42#define WSIZE 0x8000 43 44struct gzip { 45 struct image_params *ip; 46 struct exec a_out; 47 int error; 48 int where; 49 u_char *inbuf; 50 u_long offset; 51 u_long output; 52 u_long len; 53 int idx; 54 u_long virtual_offset, file_offset, file_end, bss_size; 55 unsigned gz_wp; 56 u_char *gz_slide; 57}; 58 59int inflate __P((struct gzip *)); 60 61extern struct sysentvec aout_sysvec; 62 63#define slide (gz->gz_slide) 64#define wp (gz->gz_wp) 65 66int 67exec_gzip_imgact(iparams) 68 struct image_params *iparams; 69{ 70 int error,error2=0; 71 u_char *p = (u_char *) iparams->image_header; 72 struct gzip *gz; 73 74 /* If these four are not OK, it isn't a gzip file */ 75 if (p[0] != 0x1f) return -1; /* 0 Simply magic */ 76 if (p[1] != 0x8b) return -1; /* 1 Simply magic */ 77 if (p[2] != 0x08) return -1; /* 2 Compression method */ 78 if (p[9] != 0x03) return -1; /* 9 OS compressed on */ 79 80 /* If this one contains anything but a comment or a filename 81 * marker, we don't want to chew on it 82 */ 83 if (p[3] & ~(0x18)) return ENOEXEC; /* 3 Flags */ 84 85 /* These are of no use to us */ 86 /* 4-7 Timestamp */ 87 /* 8 Extra flags */ 88 89 gz = malloc(sizeof *gz,M_GZIP,M_NOWAIT); 90 if (!gz) 91 return ENOMEM; 92 bzero(gz,sizeof *gz); /* waste of time ? */ 93 94 gz->gz_slide = malloc(WSIZE,M_TEMP,M_NOWAIT); 95 if (!gz->gz_slide) { 96 free(gz,M_GZIP); 97 return ENOMEM; 98 } 99 100 gz->ip = iparams; 101 gz->error = ENOEXEC; 102 gz->idx = 10; 103 104 if (p[3] & 0x08) { /* skip a filename */ 105 while (p[gz->idx++]) 106 if (gz->idx >= PAGE_SIZE) 107 goto done; 108 } 109 110 if (p[3] & 0x10) { /* skip a comment */ 111 while (p[gz->idx++]) 112 if (gz->idx >= PAGE_SIZE) 113 goto done; 114 } 115 116 gz->len = gz->ip->attr->va_size; 117 118 gz->error = 0; 119 120 error = inflate(gz); 121 122 if (gz->inbuf) { 123 error2 = 124 vm_deallocate(kernel_map, (vm_offset_t)gz->inbuf, PAGE_SIZE); 125 } 126 127 if (gz->error || error || error2) { 128 printf("Output=%lu ",gz->output); 129 printf("Inflate_error=%d gz->error=%d error2=%d where=%d\n", 130 error,gz->error,error2,gz->where); 131 if (gz->error) 132 goto done; 133 if (error) { 134 gz->error = ENOEXEC; 135 goto done; 136 } 137 if (error2) { 138 gz->error = error2; 139 goto done; 140 } 141 } 142 143 done: 144 error = gz->error; 145 free(gz->gz_slide,M_TEMP); 146 free(gz,M_GZIP); 147 return error; 148} 149 150int 151do_aout_hdr(struct gzip *gz) 152{ 153 int error; 154 struct vmspace *vmspace = gz->ip->proc->p_vmspace; 155 u_long vmaddr; 156 157 /* 158 * Set file/virtual offset based on a.out variant. 159 * We do two cases: host byte order and network byte order 160 * (for NetBSD compatibility) 161 */ 162 switch ((int)(gz->a_out.a_magic & 0xffff)) { 163 case ZMAGIC: 164 gz->virtual_offset = 0; 165 if (gz->a_out.a_text) { 166 gz->file_offset = NBPG; 167 } else { 168 /* Bill's "screwball mode" */ 169 gz->file_offset = 0; 170 } 171 break; 172 case QMAGIC: 173 gz->virtual_offset = NBPG; 174 gz->file_offset = 0; 175 break; 176 default: 177 /* NetBSD compatibility */ 178 switch ((int)(ntohl(gz->a_out.a_magic) & 0xffff)) { 179 case ZMAGIC: 180 case QMAGIC: 181 gz->virtual_offset = NBPG; 182 gz->file_offset = 0; 183 break; 184 default: 185 gz->where = __LINE__; 186 return (-1); 187 } 188 } 189 190 gz->bss_size = roundup(gz->a_out.a_bss, NBPG); 191 192 /* 193 * Check various fields in header for validity/bounds. 194 */ 195 if (/* entry point must lay with text region */ 196 gz->a_out.a_entry < gz->virtual_offset || 197 gz->a_out.a_entry >= gz->virtual_offset + gz->a_out.a_text || 198 199 /* text and data size must each be page rounded */ 200 gz->a_out.a_text % NBPG || 201 gz->a_out.a_data % NBPG) { 202 gz->where = __LINE__; 203 return (-1); 204 } 205 206 /* 207 * text/data/bss must not exceed limits 208 */ 209 if (/* text can't exceed maximum text size */ 210 gz->a_out.a_text > MAXTSIZ || 211 212 /* data + bss can't exceed maximum data size */ 213 gz->a_out.a_data + gz->bss_size > MAXDSIZ || 214 215 /* data + bss can't exceed rlimit */ 216 gz->a_out.a_data + gz->bss_size > 217 gz->ip->proc->p_rlimit[RLIMIT_DATA].rlim_cur) { 218 gz->where = __LINE__; 219 return (ENOMEM); 220 } 221 222 /* Find out how far we should go */ 223 gz->file_end = gz->file_offset + gz->a_out.a_text + gz->a_out.a_data; 224 225 /* copy in arguments and/or environment from old process */ 226 error = exec_extract_strings(gz->ip); 227 if (error) { 228 gz->where = __LINE__; 229 return (error); 230 } 231 232 /* 233 * Destroy old process VM and create a new one (with a new stack) 234 */ 235 exec_new_vmspace(gz->ip); 236 237 vmaddr = gz->virtual_offset; 238 239 error = vm_mmap(&vmspace->vm_map, /* map */ 240 &vmaddr, /* address */ 241 gz->a_out.a_text, /* size */ 242 VM_PROT_READ | VM_PROT_EXECUTE | VM_PROT_WRITE, /* protection */ 243 VM_PROT_READ | VM_PROT_EXECUTE | VM_PROT_WRITE, 244 MAP_ANON | MAP_FIXED, /* flags */ 245 0, /* vnode */ 246 0); /* offset */ 247 248 if (error) { 249 gz->where = __LINE__; 250 return (error); 251 } 252 253 vmaddr = gz->virtual_offset + gz->a_out.a_text; 254 255 /* 256 * Map data read/write (if text is 0, assume text is in data area 257 * [Bill's screwball mode]) 258 */ 259 260 error = vm_mmap(&vmspace->vm_map, 261 &vmaddr, 262 gz->a_out.a_data, 263 VM_PROT_READ | VM_PROT_WRITE | (gz->a_out.a_text ? 0 : VM_PROT_EXECUTE), 264 VM_PROT_ALL, MAP_ANON | MAP_FIXED, 265 0, 266 0); 267 268 if (error) { 269 gz->where = __LINE__; 270 return (error); 271 } 272 273 /* 274 * Allocate demand-zeroed area for uninitialized data 275 * "bss" = 'block started by symbol' - named after the IBM 7090 276 * instruction of the same name. 277 */ 278 vmaddr = gz->virtual_offset + gz->a_out.a_text + gz->a_out.a_data; 279 error = vm_allocate(&vmspace->vm_map, &vmaddr, gz->bss_size, FALSE); 280 if (error) { 281 gz->where = __LINE__; 282 return (error); 283 } 284 285 /* Fill in process VM information */ 286 vmspace->vm_tsize = gz->a_out.a_text >> PAGE_SHIFT; 287 vmspace->vm_dsize = (gz->a_out.a_data + gz->bss_size) >> PAGE_SHIFT; 288 vmspace->vm_taddr = (caddr_t) gz->virtual_offset; 289 vmspace->vm_daddr = (caddr_t) gz->virtual_offset + gz->a_out.a_text; 290 291 /* Fill in image_params */ 292 gz->ip->interpreted = 0; 293 gz->ip->entry_addr = gz->a_out.a_entry; 294 295 gz->ip->proc->p_sysent = &aout_sysvec; 296 297 return 0; 298} 299 300/* 301 * Tell kern_execve.c about it, with a little help from the linker. 302 * Since `const' objects end up in the text segment, TEXT_SET is the 303 * correct directive to use. 304 */ 305static const struct execsw gzip_execsw = { exec_gzip_imgact, "gzip" }; 306TEXT_SET(execsw_set, gzip_execsw); 307 308/* Stuff to make inflate() work */ 309# define uch u_char 310# define ush u_short 311# define ulg u_long 312# define memzero(dest,len) bzero(dest,len) 313# define NOMEMCPY 314#define FPRINTF printf 315 316#define EOF -1 317#define CHECK_EOF 318static int 319NextByte(struct gzip *gz) 320{ 321 int error; 322 323 if(gz->idx >= gz->len) { 324 gz->where = __LINE__; 325 return EOF; 326 } 327 328 if((!gz->inbuf) || gz->idx >= (gz->offset+PAGE_SIZE)) { 329 if(gz->inbuf) { 330 error = vm_deallocate(kernel_map, 331 (vm_offset_t)gz->inbuf, PAGE_SIZE); 332 if(error) { 333 gz->where = __LINE__; 334 gz->error = error; 335 return EOF; 336 } 337 } 338 339 gz->offset += PAGE_SIZE; 340 341 error = vm_mmap(kernel_map, /* map */ 342 (vm_offset_t *)&gz->inbuf, /* address */ 343 PAGE_SIZE, /* size */ 344 VM_PROT_READ, /* protection */ 345 VM_PROT_READ, /* max protection */ 346 0, /* flags */ 347 (caddr_t)gz->ip->vnodep, /* vnode */ 348 gz->offset); /* offset */ 349 if(error) { 350 gz->where = __LINE__; 351 gz->error = error; 352 return EOF; 353 } 354 355 } 356 return gz->inbuf[(gz->idx++) - gz->offset]; 357} 358 359#define NEXTBYTE NextByte(gz) 360 361static int 362Flush(struct gzip *gz,u_long siz) 363{ 364 u_char *p = slide,*q; 365 int i; 366 367 /* First, find a a.out-header */ 368 if(gz->output < sizeof gz->a_out) { 369 q = (u_char*) &gz->a_out; 370 i = min(siz,sizeof gz->a_out - gz->output); 371 bcopy(p,q+gz->output,i); 372 gz->output += i; 373 p += i; 374 siz -= i; 375 if(gz->output == sizeof gz->a_out) { 376 i = do_aout_hdr(gz); 377 if (i == -1) { 378 gz->where = __LINE__; 379 gz->error = ENOEXEC; 380 return ENOEXEC; 381 } else if (i) { 382 gz->where = __LINE__; 383 gz->error = i; 384 return ENOEXEC; 385 } 386 if(gz->file_offset < sizeof gz->a_out) { 387 q = (u_char*) gz->virtual_offset + gz->output - gz->file_offset; 388 bcopy(&gz->a_out,q,sizeof gz->a_out - gz->file_offset); 389 } 390 } 391 } 392 if(gz->output >= gz->file_offset && gz->output < gz->file_end) { 393 i = min(siz, gz->file_end - gz->output); 394 q = (u_char*) gz->virtual_offset + gz->output - gz->file_offset; 395 bcopy(p,q,i); 396 gz->output += i; 397 p += i; 398 siz -= i; 399 } 400 gz->output += siz; 401 return 0; 402} 403 404#define FLUSH(x,y) {int foo = Flush(x,y); if (foo) return foo;} 405static 406void * 407myalloc(u_long size) 408{ 409 return malloc(size, M_GZIP, M_NOWAIT); 410} 411#define malloc myalloc 412 413static 414void 415myfree(void * ptr) 416{ 417 free(ptr,M_GZIP); 418} 419#define free myfree 420 421static int qflag; 422#define Trace(x) /* */ 423 424 425/* This came from unzip-5.12. I have changed it to pass a "gz" pointer 426 * around, thus hopefully making it re-entrant. Poul-Henningi 427 */ 428 429/* inflate.c -- put in the public domain by Mark Adler 430 version c14o, 23 August 1994 */ 431 432/* You can do whatever you like with this source file, though I would 433 prefer that if you modify it and redistribute it that you include 434 comments to that effect with your name and the date. Thank you. 435 436 History: 437 vers date who what 438 ---- --------- -------------- ------------------------------------ 439 a ~~ Feb 92 M. Adler used full (large, one-step) lookup table 440 b1 21 Mar 92 M. Adler first version with partial lookup tables 441 b2 21 Mar 92 M. Adler fixed bug in fixed-code blocks 442 b3 22 Mar 92 M. Adler sped up match copies, cleaned up some 443 b4 25 Mar 92 M. Adler added prototypes; removed window[] (now 444 is the responsibility of unzip.h--also 445 changed name to slide[]), so needs diffs 446 for unzip.c and unzip.h (this allows 447 compiling in the small model on MSDOS); 448 fixed cast of q in huft_build(); 449 b5 26 Mar 92 M. Adler got rid of unintended macro recursion. 450 b6 27 Mar 92 M. Adler got rid of nextbyte() routine. fixed 451 bug in inflate_fixed(). 452 c1 30 Mar 92 M. Adler removed lbits, dbits environment variables. 453 changed BMAX to 16 for explode. Removed 454 OUTB usage, and replaced it with flush()-- 455 this was a 20% speed improvement! Added 456 an explode.c (to replace unimplod.c) that 457 uses the huft routines here. Removed 458 register union. 459 c2 4 Apr 92 M. Adler fixed bug for file sizes a multiple of 32k. 460 c3 10 Apr 92 M. Adler reduced memory of code tables made by 461 huft_build significantly (factor of two to 462 three). 463 c4 15 Apr 92 M. Adler added NOMEMCPY do kill use of memcpy(). 464 worked around a Turbo C optimization bug. 465 c5 21 Apr 92 M. Adler added the WSIZE #define to allow reducing 466 the 32K window size for specialized 467 applications. 468 c6 31 May 92 M. Adler added some typecasts to eliminate warnings 469 c7 27 Jun 92 G. Roelofs added some more typecasts (444: MSC bug). 470 c8 5 Oct 92 J-l. Gailly added ifdef'd code to deal with PKZIP bug. 471 c9 9 Oct 92 M. Adler removed a memory error message (~line 416). 472 c10 17 Oct 92 G. Roelofs changed ULONG/UWORD/byte to ulg/ush/uch, 473 removed old inflate, renamed inflate_entry 474 to inflate, added Mark's fix to a comment. 475 c10.5 14 Dec 92 M. Adler fix up error messages for incomplete trees. 476 c11 2 Jan 93 M. Adler fixed bug in detection of incomplete 477 tables, and removed assumption that EOB is 478 the longest code (bad assumption). 479 c12 3 Jan 93 M. Adler make tables for fixed blocks only once. 480 c13 5 Jan 93 M. Adler allow all zero length codes (pkzip 2.04c 481 outputs one zero length code for an empty 482 distance tree). 483 c14 12 Mar 93 M. Adler made inflate.c standalone with the 484 introduction of inflate.h. 485 c14b 16 Jul 93 G. Roelofs added (unsigned) typecast to w at 470. 486 c14c 19 Jul 93 J. Bush changed v[N_MAX], l[288], ll[28x+3x] arrays 487 to static for Amiga. 488 c14d 13 Aug 93 J-l. Gailly de-complicatified Mark's c[*p++]++ thing. 489 c14e 8 Oct 93 G. Roelofs changed memset() to memzero(). 490 c14f 22 Oct 93 G. Roelofs renamed quietflg to qflag; made Trace() 491 conditional; added inflate_free(). 492 c14g 28 Oct 93 G. Roelofs changed l/(lx+1) macro to pointer (Cray bug) 493 c14h 7 Dec 93 C. Ghisler huft_build() optimizations. 494 c14i 9 Jan 94 A. Verheijen set fixed_t{d,l} to NULL after freeing; 495 G. Roelofs check NEXTBYTE macro for EOF. 496 c14j 23 Jan 94 G. Roelofs removed Ghisler "optimizations"; ifdef'd 497 EOF check. 498 c14k 27 Feb 94 G. Roelofs added some typecasts to avoid warnings. 499 c14l 9 Apr 94 G. Roelofs fixed split comments on preprocessor lines 500 to avoid bug in Encore compiler. 501 c14m 7 Jul 94 P. Kienitz modified to allow assembler version of 502 inflate_codes() (define ASM_INFLATECODES) 503 c14n 22 Jul 94 G. Roelofs changed fprintf to FPRINTF for DLL versions 504 c14o 23 Aug 94 C. Spieler added a newline to a debug statement; 505 G. Roelofs added another typecast to avoid MSC warning 506 */ 507 508 509/* 510 Inflate deflated (PKZIP's method 8 compressed) data. The compression 511 method searches for as much of the current string of bytes (up to a 512 length of 258) in the previous 32K bytes. If it doesn't find any 513 matches (of at least length 3), it codes the next byte. Otherwise, it 514 codes the length of the matched string and its distance backwards from 515 the current position. There is a single Huffman code that codes both 516 single bytes (called "literals") and match lengths. A second Huffman 517 code codes the distance information, which follows a length code. Each 518 length or distance code actually represents a base value and a number 519 of "extra" (sometimes zero) bits to get to add to the base value. At 520 the end of each deflated block is a special end-of-block (EOB) literal/ 521 length code. The decoding process is basically: get a literal/length 522 code; if EOB then done; if a literal, emit the decoded byte; if a 523 length then get the distance and emit the referred-to bytes from the 524 sliding window of previously emitted data. 525 526 There are (currently) three kinds of inflate blocks: stored, fixed, and 527 dynamic. The compressor outputs a chunk of data at a time and decides 528 which method to use on a chunk-by-chunk basis. A chunk might typically 529 be 32K to 64K, uncompressed. If the chunk is uncompressible, then the 530 "stored" method is used. In this case, the bytes are simply stored as 531 is, eight bits per byte, with none of the above coding. The bytes are 532 preceded by a count, since there is no longer an EOB code. 533 534 If the data is compressible, then either the fixed or dynamic methods 535 are used. In the dynamic method, the compressed data is preceded by 536 an encoding of the literal/length and distance Huffman codes that are 537 to be used to decode this block. The representation is itself Huffman 538 coded, and so is preceded by a description of that code. These code 539 descriptions take up a little space, and so for small blocks, there is 540 a predefined set of codes, called the fixed codes. The fixed method is 541 used if the block ends up smaller that way (usually for quite small 542 chunks); otherwise the dynamic method is used. In the latter case, the 543 codes are customized to the probabilities in the current block and so 544 can code it much better than the pre-determined fixed codes can. 545 546 The Huffman codes themselves are decoded using a mutli-level table 547 lookup, in order to maximize the speed of decoding plus the speed of 548 building the decoding tables. See the comments below that precede the 549 lbits and dbits tuning parameters. 550 */ 551 552 553/* 554 Notes beyond the 1.93a appnote.txt: 555 556 1. Distance pointers never point before the beginning of the output 557 stream. 558 2. Distance pointers can point back across blocks, up to 32k away. 559 3. There is an implied maximum of 7 bits for the bit length table and 560 15 bits for the actual data. 561 4. If only one code exists, then it is encoded using one bit. (Zero 562 would be more efficient, but perhaps a little confusing.) If two 563 codes exist, they are coded using one bit each (0 and 1). 564 5. There is no way of sending zero distance codes--a dummy must be 565 sent if there are none. (History: a pre 2.0 version of PKZIP would 566 store blocks with no distance codes, but this was discovered to be 567 too harsh a criterion.) Valid only for 1.93a. 2.04c does allow 568 zero distance codes, which is sent as one code of zero bits in 569 length. 570 6. There are up to 286 literal/length codes. Code 256 represents the 571 end-of-block. Note however that the static length tree defines 572 288 codes just to fill out the Huffman codes. Codes 286 and 287 573 cannot be used though, since there is no length base or extra bits 574 defined for them. Similarily, there are up to 30 distance codes. 575 However, static trees define 32 codes (all 5 bits) to fill out the 576 Huffman codes, but the last two had better not show up in the data. 577 7. Unzip can check dynamic Huffman blocks for complete code sets. 578 The exception is that a single code would not be complete (see #4). 579 8. The five bits following the block type is really the number of 580 literal codes sent minus 257. 581 9. Length codes 8,16,16 are interpreted as 13 length codes of 8 bits 582 (1+6+6). Therefore, to output three times the length, you output 583 three codes (1+1+1), whereas to output four times the same length, 584 you only need two codes (1+3). Hmm. 585 10. In the tree reconstruction algorithm, Code = Code + Increment 586 only if BitLength(i) is not zero. (Pretty obvious.) 587 11. Correction: 4 Bits: # of Bit Length codes - 4 (4 - 19) 588 12. Note: length code 284 can represent 227-258, but length code 285 589 really is 258. The last length deserves its own, short code 590 since it gets used a lot in very redundant files. The length 591 258 is special since 258 - 3 (the min match length) is 255. 592 13. The literal/length and distance code bit lengths are read as a 593 single stream of lengths. It is possible (and advantageous) for 594 a repeat code (16, 17, or 18) to go across the boundary between 595 the two sets of lengths. 596 */ 597 598 599#define PKZIP_BUG_WORKAROUND /* PKZIP 1.93a problem--live with it */ 600 601/* 602 inflate.h must supply the uch slide[WSIZE] array and the NEXTBYTE, 603 FLUSH() and memzero macros. If the window size is not 32K, it 604 should also define WSIZE. If INFMOD is defined, it can include 605 compiled functions to support the NEXTBYTE and/or FLUSH() macros. 606 There are defaults for NEXTBYTE and FLUSH() below for use as 607 examples of what those functions need to do. Normally, you would 608 also want FLUSH() to compute a crc on the data. inflate.h also 609 needs to provide these typedefs: 610 611 typedef unsigned char uch; 612 typedef unsigned short ush; 613 typedef unsigned long ulg; 614 615 This module uses the external functions malloc() and free() (and 616 probably memset() or bzero() in the memzero() macro). Their 617 prototypes are normally found in <string.h> and <stdlib.h>. 618 */ 619#define INFMOD /* tell inflate.h to include code to be compiled */ 620 621/* Huffman code lookup table entry--this entry is four bytes for machines 622 that have 16-bit pointers (e.g. PC's in the small or medium model). 623 Valid extra bits are 0..13. e == 15 is EOB (end of block), e == 16 624 means that v is a literal, 16 < e < 32 means that v is a pointer to 625 the next table, which codes e - 16 bits, and lastly e == 99 indicates 626 an unused code. If a code with e == 99 is looked up, this implies an 627 error in the data. */ 628struct huft { 629 uch e; /* number of extra bits or operation */ 630 uch b; /* number of bits in this code or subcode */ 631 union { 632 ush n; /* literal, length base, or distance base */ 633 struct huft *t; /* pointer to next level of table */ 634 } v; 635}; 636 637 638/* Function prototypes */ 639#ifndef OF 640# ifdef __STDC__ 641# define OF(a) a 642# else /* !__STDC__ */ 643# define OF(a) () 644# endif /* ?__STDC__ */ 645#endif 646int huft_build OF((struct gzip *,unsigned *, unsigned, unsigned, ush *, ush *, 647 struct huft **, int *)); 648int huft_free OF((struct gzip *,struct huft *)); 649int inflate_codes OF((struct gzip *,struct huft *, struct huft *, int, int)); 650int inflate_stored OF((struct gzip *)); 651int inflate_fixed OF((struct gzip *)); 652int inflate_dynamic OF((struct gzip *)); 653int inflate_block OF((struct gzip *,int *)); 654int inflate_free OF((struct gzip *)); 655 656 657/* The inflate algorithm uses a sliding 32K byte window on the uncompressed 658 stream to find repeated byte strings. This is implemented here as a 659 circular buffer. The index is updated simply by incrementing and then 660 and'ing with 0x7fff (32K-1). */ 661/* It is left to other modules to supply the 32K area. It is assumed 662 to be usable as if it were declared "uch slide[32768];" or as just 663 "uch *slide;" and then malloc'ed in the latter case. The definition 664 must be in unzip.h, included above. */ 665 666 667/* Tables for deflate from PKZIP's appnote.txt. */ 668static unsigned border[] = { /* Order of the bit length code lengths */ 669 16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15}; 670static ush cplens[] = { /* Copy lengths for literal codes 257..285 */ 671 3, 4, 5, 6, 7, 8, 9, 10, 11, 13, 15, 17, 19, 23, 27, 31, 672 35, 43, 51, 59, 67, 83, 99, 115, 131, 163, 195, 227, 258, 0, 0}; 673 /* note: see note #13 above about the 258 in this list. */ 674static ush cplext[] = { /* Extra bits for literal codes 257..285 */ 675 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2, 676 3, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5, 0, 99, 99}; /* 99==invalid */ 677static ush cpdist[] = { /* Copy offsets for distance codes 0..29 */ 678 1, 2, 3, 4, 5, 7, 9, 13, 17, 25, 33, 49, 65, 97, 129, 193, 679 257, 385, 513, 769, 1025, 1537, 2049, 3073, 4097, 6145, 680 8193, 12289, 16385, 24577}; 681static ush cpdext[] = { /* Extra bits for distance codes */ 682 0, 0, 0, 0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 683 7, 7, 8, 8, 9, 9, 10, 10, 11, 11, 684 12, 12, 13, 13}; 685 686/* And'ing with mask[n] masks the lower n bits */ 687ush mask[] = { 688 0x0000, 689 0x0001, 0x0003, 0x0007, 0x000f, 0x001f, 0x003f, 0x007f, 0x00ff, 690 0x01ff, 0x03ff, 0x07ff, 0x0fff, 0x1fff, 0x3fff, 0x7fff, 0xffff 691}; 692 693 694/* Macros for inflate() bit peeking and grabbing. 695 The usage is: 696 697 NEEDBITS(j) 698 x = b & mask[j]; 699 DUMPBITS(j) 700 701 where NEEDBITS makes sure that b has at least j bits in it, and 702 DUMPBITS removes the bits from b. The macros use the variable k 703 for the number of bits in b. Normally, b and k are register 704 variables for speed, and are initialized at the begining of a 705 routine that uses these macros from a global bit buffer and count. 706 707 In order to not ask for more bits than there are in the compressed 708 stream, the Huffman tables are constructed to only ask for just 709 enough bits to make up the end-of-block code (value 256). Then no 710 bytes need to be "returned" to the buffer at the end of the last 711 block. See the huft_build() routine. 712 */ 713 714ulg bb; /* bit buffer */ 715unsigned bk; /* bits in bit buffer */ 716 717#ifndef CHECK_EOF 718# define NEEDBITS(n) {while(k<(n)){b|=((ulg)NEXTBYTE)<<k;k+=8;}} 719#else 720# define NEEDBITS(n) {while(k<(n)){int c=NEXTBYTE;if(c==EOF)return 1;\ 721 b|=((ulg)c)<<k;k+=8;}} 722#endif /* Piet Plomp: change "return 1" to "break" */ 723 724#define DUMPBITS(n) {b>>=(n);k-=(n);} 725 726 727/* 728 Huffman code decoding is performed using a multi-level table lookup. 729 The fastest way to decode is to simply build a lookup table whose 730 size is determined by the longest code. However, the time it takes 731 to build this table can also be a factor if the data being decoded 732 is not very long. The most common codes are necessarily the 733 shortest codes, so those codes dominate the decoding time, and hence 734 the speed. The idea is you can have a shorter table that decodes the 735 shorter, more probable codes, and then point to subsidiary tables for 736 the longer codes. The time it costs to decode the longer codes is 737 then traded against the time it takes to make longer tables. 738 739 This results of this trade are in the variables lbits and dbits 740 below. lbits is the number of bits the first level table for literal/ 741 length codes can decode in one step, and dbits is the same thing for 742 the distance codes. Subsequent tables are also less than or equal to 743 those sizes. These values may be adjusted either when all of the 744 codes are shorter than that, in which case the longest code length in 745 bits is used, or when the shortest code is *longer* than the requested 746 table size, in which case the length of the shortest code in bits is 747 used. 748 749 There are two different values for the two tables, since they code a 750 different number of possibilities each. The literal/length table 751 codes 286 possible values, or in a flat code, a little over eight 752 bits. The distance table codes 30 possible values, or a little less 753 than five bits, flat. The optimum values for speed end up being 754 about one bit more than those, so lbits is 8+1 and dbits is 5+1. 755 The optimum values may differ though from machine to machine, and 756 possibly even between compilers. Your mileage may vary. 757 */ 758 759 760int lbits = 9; /* bits in base literal/length lookup table */ 761int dbits = 6; /* bits in base distance lookup table */ 762 763 764/* If BMAX needs to be larger than 16, then h and x[] should be ulg. */ 765#define BMAX 16 /* maximum bit length of any code (16 for explode) */ 766#define N_MAX 288 /* maximum number of codes in any set */ 767 768 769unsigned hufts; /* track memory usage */ 770 771 772int huft_build(gz,b, n, s, d, e, t, m) 773struct gzip *gz; 774unsigned *b; /* code lengths in bits (all assumed <= BMAX) */ 775unsigned n; /* number of codes (assumed <= N_MAX) */ 776unsigned s; /* number of simple-valued codes (0..s-1) */ 777ush *d; /* list of base values for non-simple codes */ 778ush *e; /* list of extra bits for non-simple codes */ 779struct huft **t; /* result: starting table */ 780int *m; /* maximum lookup bits, returns actual */ 781/* Given a list of code lengths and a maximum table size, make a set of 782 tables to decode that set of codes. Return zero on success, one if 783 the given code set is incomplete (the tables are still built in this 784 case), two if the input is invalid (all zero length codes or an 785 oversubscribed set of lengths), and three if not enough memory. 786 The code with value 256 is special, and the tables are constructed 787 so that no bits beyond that code are fetched when that code is 788 decoded. */ 789{ 790 unsigned a; /* counter for codes of length k */ 791 unsigned c[BMAX+1]; /* bit length count table */ 792 unsigned el; /* length of EOB code (value 256) */ 793 unsigned f; /* i repeats in table every f entries */ 794 int g; /* maximum code length */ 795 int h; /* table level */ 796 register unsigned i; /* counter, current code */ 797 register unsigned j; /* counter */ 798 register int k; /* number of bits in current code */ 799 int lx[BMAX+1]; /* memory for l[-1..BMAX-1] */ 800 int *l = lx+1; /* stack of bits per table */ 801 register unsigned *p; /* pointer into c[], b[], or v[] */ 802 register struct huft *q; /* points to current table */ 803 struct huft r; /* table entry for structure assignment */ 804 struct huft *u[BMAX]; /* table stack */ 805 static unsigned v[N_MAX]; /* values in order of bit length */ 806 register int w; /* bits before this table == (l * h) */ 807 unsigned x[BMAX+1]; /* bit offsets, then code stack */ 808 unsigned *xp; /* pointer into x */ 809 int y; /* number of dummy codes added */ 810 unsigned z; /* number of entries in current table */ 811 812 813 /* Generate counts for each bit length */ 814 el = n > 256 ? b[256] : BMAX; /* set length of EOB code, if any */ 815 memzero((char *)c, sizeof(c)); 816 p = b; i = n; 817 do { 818 c[*p]++; p++; /* assume all entries <= BMAX */ 819 } while (--i); 820 if (c[0] == n) /* null input--all zero length codes */ 821 { 822 *t = (struct huft *)NULL; 823 *m = 0; 824 return 0; 825 } 826 827 828 /* Find minimum and maximum length, bound *m by those */ 829 for (j = 1; j <= BMAX; j++) 830 if (c[j]) 831 break; 832 k = j; /* minimum code length */ 833 if ((unsigned)*m < j) 834 *m = j; 835 for (i = BMAX; i; i--) 836 if (c[i]) 837 break; 838 g = i; /* maximum code length */ 839 if ((unsigned)*m > i) 840 *m = i; 841 842 843 /* Adjust last length count to fill out codes, if needed */ 844 for (y = 1 << j; j < i; j++, y <<= 1) 845 if ((y -= c[j]) < 0) 846 return 2; /* bad input: more codes than bits */ 847 if ((y -= c[i]) < 0) 848 return 2; 849 c[i] += y; 850 851 852 /* Generate starting offsets into the value table for each length */ 853 x[1] = j = 0; 854 p = c + 1; xp = x + 2; 855 while (--i) { /* note that i == g from above */ 856 *xp++ = (j += *p++); 857 } 858 859 860 /* Make a table of values in order of bit lengths */ 861 p = b; i = 0; 862 do { 863 if ((j = *p++) != 0) 864 v[x[j]++] = i; 865 } while (++i < n); 866 867 868 /* Generate the Huffman codes and for each, make the table entries */ 869 x[0] = i = 0; /* first Huffman code is zero */ 870 p = v; /* grab values in bit order */ 871 h = -1; /* no tables yet--level -1 */ 872 w = l[-1] = 0; /* no bits decoded yet */ 873 u[0] = (struct huft *)NULL; /* just to keep compilers happy */ 874 q = (struct huft *)NULL; /* ditto */ 875 z = 0; /* ditto */ 876 877 /* go through the bit lengths (k already is bits in shortest code) */ 878 for (; k <= g; k++) 879 { 880 a = c[k]; 881 while (a--) 882 { 883 /* here i is the Huffman code of length k bits for value *p */ 884 /* make tables up to required level */ 885 while (k > w + l[h]) 886 { 887 w += l[h++]; /* add bits already decoded */ 888 889 /* compute minimum size table less than or equal to *m bits */ 890 z = (z = g - w) > (unsigned)*m ? *m : z; /* upper limit */ 891 if ((f = 1 << (j = k - w)) > a + 1) /* try a k-w bit table */ 892 { /* too few codes for k-w bit table */ 893 f -= a + 1; /* deduct codes from patterns left */ 894 xp = c + k; 895 while (++j < z) /* try smaller tables up to z bits */ 896 { 897 if ((f <<= 1) <= *++xp) 898 break; /* enough codes to use up j bits */ 899 f -= *xp; /* else deduct codes from patterns */ 900 } 901 } 902 if ((unsigned)w + j > el && (unsigned)w < el) 903 j = el - w; /* make EOB code end at table */ 904 z = 1 << j; /* table entries for j-bit table */ 905 l[h] = j; /* set table size in stack */ 906 907 /* allocate and link in new table */ 908 if ((q = (struct huft *)malloc((z + 1)*sizeof(struct huft))) == 909 (struct huft *)NULL) 910 { 911 if (h) 912 huft_free(gz,u[0]); 913 return 3; /* not enough memory */ 914 } 915 hufts += z + 1; /* track memory usage */ 916 *t = q + 1; /* link to list for huft_free() */ 917 *(t = &(q->v.t)) = (struct huft *)NULL; 918 u[h] = ++q; /* table starts after link */ 919 920 /* connect to last table, if there is one */ 921 if (h) 922 { 923 x[h] = i; /* save pattern for backing up */ 924 r.b = (uch)l[h-1]; /* bits to dump before this table */ 925 r.e = (uch)(16 + j); /* bits in this table */ 926 r.v.t = q; /* pointer to this table */ 927 j = (i & ((1 << w) - 1)) >> (w - l[h-1]); 928 u[h-1][j] = r; /* connect to last table */ 929 } 930 } 931 932 /* set up table entry in r */ 933 r.b = (uch)(k - w); 934 if (p >= v + n) 935 r.e = 99; /* out of values--invalid code */ 936 else if (*p < s) 937 { 938 r.e = (uch)(*p < 256 ? 16 : 15); /* 256 is end-of-block code */ 939 r.v.n = *p++; /* simple code is just the value */ 940 } 941 else 942 { 943 r.e = (uch)e[*p - s]; /* non-simple--look up in lists */ 944 r.v.n = d[*p++ - s]; 945 } 946 947 /* fill code-like entries with r */ 948 f = 1 << (k - w); 949 for (j = i >> w; j < z; j += f) 950 q[j] = r; 951 952 /* backwards increment the k-bit code i */ 953 for (j = 1 << (k - 1); i & j; j >>= 1) 954 i ^= j; 955 i ^= j; 956 957 /* backup over finished tables */ 958 while ((i & ((1 << w) - 1)) != x[h]) 959 w -= l[--h]; /* don't need to update q */ 960 } 961 } 962 963 964 /* return actual size of base table */ 965 *m = l[0]; 966 967 968 /* Return true (1) if we were given an incomplete table */ 969 return y != 0 && g != 1; 970} 971 972 973 974int huft_free(gz,t) 975struct gzip *gz; 976struct huft *t; /* table to free */ 977/* Free the malloc'ed tables built by huft_build(), which makes a linked 978 list of the tables it made, with the links in a dummy first entry of 979 each table. */ 980{ 981 register struct huft *p, *q; 982 983 984 /* Go through linked list, freeing from the malloced (t[-1]) address. */ 985 p = t; 986 while (p != (struct huft *)NULL) 987 { 988 q = (--p)->v.t; 989 free(p); 990 p = q; 991 } 992 return 0; 993} 994 995 996 997#ifdef ASM_INFLATECODES 998# define inflate_codes(tl,td,bl,bd) flate_codes(tl,td,bl,bd,(uch *)slide) 999 int flate_codes OF((struct huft *, struct huft *, int, int, uch *)); 1000 1001#else 1002 1003int inflate_codes(gz,tl, td, bl, bd) 1004struct gzip *gz; 1005struct huft *tl, *td; /* literal/length and distance decoder tables */ 1006int bl, bd; /* number of bits decoded by tl[] and td[] */ 1007/* inflate (decompress) the codes in a deflated (compressed) block. 1008 Return an error code or zero if it all goes ok. */ 1009{ 1010 register unsigned e; /* table entry flag/number of extra bits */ 1011 unsigned n, d; /* length and index for copy */ 1012 unsigned w; /* current window position */ 1013 struct huft *t; /* pointer to table entry */ 1014 unsigned ml, md; /* masks for bl and bd bits */ 1015 register ulg b; /* bit buffer */ 1016 register unsigned k; /* number of bits in bit buffer */ 1017 1018 1019 /* make local copies of globals */ 1020 b = bb; /* initialize bit buffer */ 1021 k = bk; 1022 w = wp; /* initialize window position */ 1023 1024 1025 /* inflate the coded data */ 1026 ml = mask[bl]; /* precompute masks for speed */ 1027 md = mask[bd]; 1028 while (1) /* do until end of block */ 1029 { 1030 NEEDBITS((unsigned)bl) 1031 if ((e = (t = tl + ((unsigned)b & ml))->e) > 16) 1032 do { 1033 if (e == 99) 1034 return 1; 1035 DUMPBITS(t->b) 1036 e -= 16; 1037 NEEDBITS(e) 1038 } while ((e = (t = t->v.t + ((unsigned)b & mask[e]))->e) > 16); 1039 DUMPBITS(t->b) 1040 if (e == 16) /* then it's a literal */ 1041 { 1042 slide[w++] = (uch)t->v.n; 1043 if (w == WSIZE) 1044 { 1045 FLUSH(gz,w); 1046 w = 0; 1047 } 1048 } 1049 else /* it's an EOB or a length */ 1050 { 1051 /* exit if end of block */ 1052 if (e == 15) 1053 break; 1054 1055 /* get length of block to copy */ 1056 NEEDBITS(e) 1057 n = t->v.n + ((unsigned)b & mask[e]); 1058 DUMPBITS(e); 1059 1060 /* decode distance of block to copy */ 1061 NEEDBITS((unsigned)bd) 1062 if ((e = (t = td + ((unsigned)b & md))->e) > 16) 1063 do { 1064 if (e == 99) 1065 return 1; 1066 DUMPBITS(t->b) 1067 e -= 16; 1068 NEEDBITS(e) 1069 } while ((e = (t = t->v.t + ((unsigned)b & mask[e]))->e) > 16); 1070 DUMPBITS(t->b) 1071 NEEDBITS(e) 1072 d = w - t->v.n - ((unsigned)b & mask[e]); 1073 DUMPBITS(e) 1074 1075 /* do the copy */ 1076 do { 1077 n -= (e = (e = WSIZE - ((d &= WSIZE-1) > w ? d : w)) > n ? n : e); 1078#ifndef NOMEMCPY 1079 if (w - d >= e) /* (this test assumes unsigned comparison) */ 1080 { 1081 memcpy(slide + w, slide + d, e); 1082 w += e; 1083 d += e; 1084 } 1085 else /* do it slow to avoid memcpy() overlap */ 1086#endif /* !NOMEMCPY */ 1087 do { 1088 slide[w++] = slide[d++]; 1089 } while (--e); 1090 if (w == WSIZE) 1091 { 1092 FLUSH(gz,w); 1093 w = 0; 1094 } 1095 } while (n); 1096 } 1097 } 1098 1099 1100 /* restore the globals from the locals */ 1101 wp = w; /* restore global window pointer */ 1102 bb = b; /* restore global bit buffer */ 1103 bk = k; 1104 1105 1106 /* done */ 1107 return 0; 1108} 1109 1110#endif /* ASM_INFLATECODES */ 1111 1112 1113 1114int inflate_stored(gz) 1115struct gzip *gz; 1116/* "decompress" an inflated type 0 (stored) block. */ 1117{ 1118 unsigned n; /* number of bytes in block */ 1119 unsigned w; /* current window position */ 1120 register ulg b; /* bit buffer */ 1121 register unsigned k; /* number of bits in bit buffer */ 1122 1123 1124 /* make local copies of globals */ 1125 Trace((stderr, "\nstored block")); 1126 b = bb; /* initialize bit buffer */ 1127 k = bk; 1128 w = wp; /* initialize window position */ 1129 1130 1131 /* go to byte boundary */ 1132 n = k & 7; 1133 DUMPBITS(n); 1134 1135 1136 /* get the length and its complement */ 1137 NEEDBITS(16) 1138 n = ((unsigned)b & 0xffff); 1139 DUMPBITS(16) 1140 NEEDBITS(16) 1141 if (n != (unsigned)((~b) & 0xffff)) 1142 return 1; /* error in compressed data */ 1143 DUMPBITS(16) 1144 1145 1146 /* read and output the compressed data */ 1147 while (n--) 1148 { 1149 NEEDBITS(8) 1150 slide[w++] = (uch)b; 1151 if (w == WSIZE) 1152 { 1153 FLUSH(gz,w); 1154 w = 0; 1155 } 1156 DUMPBITS(8) 1157 } 1158 1159 1160 /* restore the globals from the locals */ 1161 wp = w; /* restore global window pointer */ 1162 bb = b; /* restore global bit buffer */ 1163 bk = k; 1164 return 0; 1165} 1166 1167 1168/* Globals for literal tables (built once) */ 1169struct huft *fixed_tl = (struct huft *)NULL; 1170struct huft *fixed_td; 1171int fixed_bl, fixed_bd; 1172 1173int inflate_fixed(gz) 1174struct gzip *gz; 1175/* decompress an inflated type 1 (fixed Huffman codes) block. We should 1176 either replace this with a custom decoder, or at least precompute the 1177 Huffman tables. */ 1178{ 1179 /* if first time, set up tables for fixed blocks */ 1180 Trace((stderr, "\nliteral block")); 1181 if (fixed_tl == (struct huft *)NULL) 1182 { 1183 int i; /* temporary variable */ 1184 static unsigned l[288]; /* length list for huft_build */ 1185 1186 /* literal table */ 1187 for (i = 0; i < 144; i++) 1188 l[i] = 8; 1189 for (; i < 256; i++) 1190 l[i] = 9; 1191 for (; i < 280; i++) 1192 l[i] = 7; 1193 for (; i < 288; i++) /* make a complete, but wrong code set */ 1194 l[i] = 8; 1195 fixed_bl = 7; 1196 if ((i = huft_build(gz,l, 288, 257, cplens, cplext, 1197 &fixed_tl, &fixed_bl)) != 0) 1198 { 1199 fixed_tl = (struct huft *)NULL; 1200 return i; 1201 } 1202 1203 /* distance table */ 1204 for (i = 0; i < 30; i++) /* make an incomplete code set */ 1205 l[i] = 5; 1206 fixed_bd = 5; 1207 if ((i = huft_build(gz,l, 30, 0, cpdist, cpdext, &fixed_td, &fixed_bd)) > 1) 1208 { 1209 huft_free(gz,fixed_tl); 1210 fixed_tl = (struct huft *)NULL; 1211 return i; 1212 } 1213 } 1214 1215 1216 /* decompress until an end-of-block code */ 1217 return inflate_codes(gz,fixed_tl, fixed_td, fixed_bl, fixed_bd) != 0; 1218} 1219 1220 1221 1222int inflate_dynamic(gz) 1223struct gzip *gz; 1224/* decompress an inflated type 2 (dynamic Huffman codes) block. */ 1225{ 1226 int i; /* temporary variables */ 1227 unsigned j; 1228 unsigned l; /* last length */ 1229 unsigned m; /* mask for bit lengths table */ 1230 unsigned n; /* number of lengths to get */ 1231 struct huft *tl; /* literal/length code table */ 1232 struct huft *td; /* distance code table */ 1233 int bl; /* lookup bits for tl */ 1234 int bd; /* lookup bits for td */ 1235 unsigned nb; /* number of bit length codes */ 1236 unsigned nl; /* number of literal/length codes */ 1237 unsigned nd; /* number of distance codes */ 1238#ifdef PKZIP_BUG_WORKAROUND 1239 static unsigned ll[288+32]; /* literal/length and distance code lengths */ 1240#else 1241 static unsigned ll[286+30]; /* literal/length and distance code lengths */ 1242#endif 1243 register ulg b; /* bit buffer */ 1244 register unsigned k; /* number of bits in bit buffer */ 1245 1246 1247 /* make local bit buffer */ 1248 Trace((stderr, "\ndynamic block")); 1249 b = bb; 1250 k = bk; 1251 1252 1253 /* read in table lengths */ 1254 NEEDBITS(5) 1255 nl = 257 + ((unsigned)b & 0x1f); /* number of literal/length codes */ 1256 DUMPBITS(5) 1257 NEEDBITS(5) 1258 nd = 1 + ((unsigned)b & 0x1f); /* number of distance codes */ 1259 DUMPBITS(5) 1260 NEEDBITS(4) 1261 nb = 4 + ((unsigned)b & 0xf); /* number of bit length codes */ 1262 DUMPBITS(4) 1263#ifdef PKZIP_BUG_WORKAROUND 1264 if (nl > 288 || nd > 32) 1265#else 1266 if (nl > 286 || nd > 30) 1267#endif 1268 return 1; /* bad lengths */ 1269 1270 1271 /* read in bit-length-code lengths */ 1272 for (j = 0; j < nb; j++) 1273 { 1274 NEEDBITS(3) 1275 ll[border[j]] = (unsigned)b & 7; 1276 DUMPBITS(3) 1277 } 1278 for (; j < 19; j++) 1279 ll[border[j]] = 0; 1280 1281 1282 /* build decoding table for trees--single level, 7 bit lookup */ 1283 bl = 7; 1284 if ((i = huft_build(gz,ll, 19, 19, NULL, NULL, &tl, &bl)) != 0) 1285 { 1286 if (i == 1) 1287 huft_free(gz,tl); 1288 return i; /* incomplete code set */ 1289 } 1290 1291 1292 /* read in literal and distance code lengths */ 1293 n = nl + nd; 1294 m = mask[bl]; 1295 i = l = 0; 1296 while ((unsigned)i < n) 1297 { 1298 NEEDBITS((unsigned)bl) 1299 j = (td = tl + ((unsigned)b & m))->b; 1300 DUMPBITS(j) 1301 j = td->v.n; 1302 if (j < 16) /* length of code in bits (0..15) */ 1303 ll[i++] = l = j; /* save last length in l */ 1304 else if (j == 16) /* repeat last length 3 to 6 times */ 1305 { 1306 NEEDBITS(2) 1307 j = 3 + ((unsigned)b & 3); 1308 DUMPBITS(2) 1309 if ((unsigned)i + j > n) 1310 return 1; 1311 while (j--) 1312 ll[i++] = l; 1313 } 1314 else if (j == 17) /* 3 to 10 zero length codes */ 1315 { 1316 NEEDBITS(3) 1317 j = 3 + ((unsigned)b & 7); 1318 DUMPBITS(3) 1319 if ((unsigned)i + j > n) 1320 return 1; 1321 while (j--) 1322 ll[i++] = 0; 1323 l = 0; 1324 } 1325 else /* j == 18: 11 to 138 zero length codes */ 1326 { 1327 NEEDBITS(7) 1328 j = 11 + ((unsigned)b & 0x7f); 1329 DUMPBITS(7) 1330 if ((unsigned)i + j > n) 1331 return 1; 1332 while (j--) 1333 ll[i++] = 0; 1334 l = 0; 1335 } 1336 } 1337 1338 1339 /* free decoding table for trees */ 1340 huft_free(gz,tl); 1341 1342 1343 /* restore the global bit buffer */ 1344 bb = b; 1345 bk = k; 1346 1347 1348 /* build the decoding tables for literal/length and distance codes */ 1349 bl = lbits; 1350 if ((i = huft_build(gz,ll, nl, 257, cplens, cplext, &tl, &bl)) != 0) 1351 { 1352 if (i == 1 && !qflag) { 1353 FPRINTF( "(incomplete l-tree) "); 1354 huft_free(gz,tl); 1355 } 1356 return i; /* incomplete code set */ 1357 } 1358 bd = dbits; 1359 if ((i = huft_build(gz,ll + nl, nd, 0, cpdist, cpdext, &td, &bd)) != 0) 1360 { 1361 if (i == 1 && !qflag) { 1362 FPRINTF( "(incomplete d-tree) "); 1363#ifdef PKZIP_BUG_WORKAROUND 1364 i = 0; 1365 } 1366#else 1367 huft_free(gz,td); 1368 } 1369 huft_free(gz,tl); 1370 return i; /* incomplete code set */ 1371#endif 1372 } 1373 1374 1375 /* decompress until an end-of-block code */ 1376 if (inflate_codes(gz,tl, td, bl, bd)) 1377 return 1; 1378 1379 1380 /* free the decoding tables, return */ 1381 huft_free(gz,tl); 1382 huft_free(gz,td); 1383 return 0; 1384} 1385 1386 1387 1388int inflate_block(gz,e) 1389struct gzip *gz; 1390int *e; /* last block flag */ 1391/* decompress an inflated block */ 1392{ 1393 unsigned t; /* block type */ 1394 register ulg b; /* bit buffer */ 1395 register unsigned k; /* number of bits in bit buffer */ 1396 1397 1398 /* make local bit buffer */ 1399 b = bb; 1400 k = bk; 1401 1402 1403 /* read in last block bit */ 1404 NEEDBITS(1) 1405 *e = (int)b & 1; 1406 DUMPBITS(1) 1407 1408 1409 /* read in block type */ 1410 NEEDBITS(2) 1411 t = (unsigned)b & 3; 1412 DUMPBITS(2) 1413 1414 1415 /* restore the global bit buffer */ 1416 bb = b; 1417 bk = k; 1418 1419 1420 /* inflate that block type */ 1421 if (t == 2) 1422 return inflate_dynamic(gz); 1423 if (t == 0) 1424 return inflate_stored(gz); 1425 if (t == 1) 1426 return inflate_fixed(gz); 1427 1428 1429 /* bad block type */ 1430 return 2; 1431} 1432 1433 1434 1435int inflate(gz) 1436struct gzip *gz; 1437/* decompress an inflated entry */ 1438{ 1439 int e; /* last block flag */ 1440 int r; /* result code */ 1441 unsigned h; /* maximum struct huft's malloc'ed */ 1442 1443 1444 /* initialize window, bit buffer */ 1445 wp = 0; 1446 bk = 0; 1447 bb = 0; 1448 1449 1450 /* decompress until the last block */ 1451 h = 0; 1452 do { 1453 hufts = 0; 1454 if ((r = inflate_block(gz,&e)) != 0) 1455 return r; 1456 if (hufts > h) 1457 h = hufts; 1458 } while (!e); 1459 1460 1461 /* flush out slide */ 1462 FLUSH(gz,wp); 1463 1464 1465 /* return success */ 1466 Trace((stderr, "\n%u bytes in Huffman tables (%d/entry)\n", 1467 h * sizeof(struct huft), sizeof(struct huft))); 1468 return 0; 1469} 1470 1471 1472 1473int inflate_free(gz) 1474struct gzip *gz; 1475{ 1476 if (fixed_tl != (struct huft *)NULL) 1477 { 1478 huft_free(gz,fixed_td); 1479 huft_free(gz,fixed_tl); 1480 fixed_td = fixed_tl = (struct huft *)NULL; 1481 } 1482 return 0; 1483} 1484