inflate.c (3418) | inflate.c (3507) |
---|---|
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 * | 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.4 1994/10/04 06:51:42 phk Exp $ | 10 * $Id: inflate.c,v 1.2 1994/10/07 23:18:09 csgr 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> | 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/inflate.h> 33#include <sys/mman.h> 34#include <sys/malloc.h> | 30#include <sys/inflate.h> 31#include <sys/mman.h> 32#include <sys/malloc.h> |
35#include <sys/imgact.h> 36#include <sys/imgact_aout.h> 37#include <sys/kernel.h> 38#include <sys/sysent.h> | |
39 40#include <vm/vm.h> 41#include <vm/vm_kern.h> 42 43/* Stuff to make inflate() work */ 44# define memzero(dest,len) bzero(dest,len) 45# define NOMEMCPY 46#define FPRINTF printf --- 337 unchanged lines hidden (view full) --- 384#endif 385static int huft_build OF((struct gzip *,unsigned *, unsigned, unsigned, const ush *, const ush *, struct huft **, int *, struct gz_global *)); 386static int huft_free OF((struct gzip *,struct huft *)); 387static int inflate_codes OF((struct gzip *,struct huft *, struct huft *, int, int, struct gz_global *)); 388static int inflate_stored OF((struct gzip *, struct gz_global *)); 389static int inflate_fixed OF((struct gzip *, struct gz_global *)); 390static int inflate_dynamic OF((struct gzip *, struct gz_global *)); 391static int inflate_block OF((struct gzip *,int *, struct gz_global *)); | 33 34#include <vm/vm.h> 35#include <vm/vm_kern.h> 36 37/* Stuff to make inflate() work */ 38# define memzero(dest,len) bzero(dest,len) 39# define NOMEMCPY 40#define FPRINTF printf --- 337 unchanged lines hidden (view full) --- 378#endif 379static int huft_build OF((struct gzip *,unsigned *, unsigned, unsigned, const ush *, const ush *, struct huft **, int *, struct gz_global *)); 380static int huft_free OF((struct gzip *,struct huft *)); 381static int inflate_codes OF((struct gzip *,struct huft *, struct huft *, int, int, struct gz_global *)); 382static int inflate_stored OF((struct gzip *, struct gz_global *)); 383static int inflate_fixed OF((struct gzip *, struct gz_global *)); 384static int inflate_dynamic OF((struct gzip *, struct gz_global *)); 385static int inflate_block OF((struct gzip *,int *, struct gz_global *)); |
386 387#if 0 388/* never used - why not ? */ |
|
392static int inflate_free OF((struct gzip *, struct gz_global *)); | 389static int inflate_free OF((struct gzip *, struct gz_global *)); |
390#endif |
|
393 394 395/* The inflate algorithm uses a sliding 32K byte window on the uncompressed 396 stream to find repeated byte strings. This is implemented here as a 397 circular buffer. The index is updated simply by incrementing and then 398 and'ing with 0x7fff (32K-1). */ 399/* It is left to other modules to supply the 32K area. It is assumed 400 to be usable as if it were declared "uch slide[32768];" or as just --- 47 unchanged lines hidden (view full) --- 448 bytes need to be "returned" to the buffer at the end of the last 449 block. See the huft_build() routine. 450 */ 451 452/* 453 * The following 2 were global variables. 454 * They are now fields of the gz_global structure. 455 */ | 391 392 393/* The inflate algorithm uses a sliding 32K byte window on the uncompressed 394 stream to find repeated byte strings. This is implemented here as a 395 circular buffer. The index is updated simply by incrementing and then 396 and'ing with 0x7fff (32K-1). */ 397/* It is left to other modules to supply the 32K area. It is assumed 398 to be usable as if it were declared "uch slide[32768];" or as just --- 47 unchanged lines hidden (view full) --- 446 bytes need to be "returned" to the buffer at the end of the last 447 block. See the huft_build() routine. 448 */ 449 450/* 451 * The following 2 were global variables. 452 * They are now fields of the gz_global structure. 453 */ |
456#define bb (glbl->bb) /* bit buffer */ 457#define bk (glbl->bk) /* bits in bit buffer */ | 454#define bb (glbl->gz_bb) /* bit buffer */ 455#define bk (glbl->gz_bk) /* bits in bit buffer */ |
458 459#ifndef CHECK_EOF 460# define NEEDBITS(n) {while(k<(n)){b|=((ulg)NEXTBYTE)<<k;k+=8;}} 461#else 462# define NEEDBITS(n) {while(k<(n)){int c=NEXTBYTE;if(c==EOF)return 1;\ 463 b|=((ulg)c)<<k;k+=8;}} 464#endif /* Piet Plomp: change "return 1" to "break" */ 465 --- 39 unchanged lines hidden (view full) --- 505 506/* If BMAX needs to be larger than 16, then h and x[] should be ulg. */ 507#define BMAX 16 /* maximum bit length of any code (16 for explode) */ 508#define N_MAX 288 /* maximum number of codes in any set */ 509 510/* 511 * This also used to be a global variable 512 */ | 456 457#ifndef CHECK_EOF 458# define NEEDBITS(n) {while(k<(n)){b|=((ulg)NEXTBYTE)<<k;k+=8;}} 459#else 460# define NEEDBITS(n) {while(k<(n)){int c=NEXTBYTE;if(c==EOF)return 1;\ 461 b|=((ulg)c)<<k;k+=8;}} 462#endif /* Piet Plomp: change "return 1" to "break" */ 463 --- 39 unchanged lines hidden (view full) --- 503 504/* If BMAX needs to be larger than 16, then h and x[] should be ulg. */ 505#define BMAX 16 /* maximum bit length of any code (16 for explode) */ 506#define N_MAX 288 /* maximum number of codes in any set */ 507 508/* 509 * This also used to be a global variable 510 */ |
513#define hufts (glbl->hufts) | 511#define hufts (glbl->gz_hufts) |
514 515 516static int huft_build(gz,b, n, s, d, e, t, m, glbl) 517struct gzip *gz; 518unsigned *b; /* code lengths in bits (all assumed <= BMAX) */ 519unsigned n; /* number of codes (assumed <= N_MAX) */ 520unsigned s; /* number of simple-valued codes (0..s-1) */ 521const ush *d; /* list of base values for non-simple codes */ --- 20 unchanged lines hidden (view full) --- 542 register unsigned j; /* counter */ 543 register int k; /* number of bits in current code */ 544 int lx[BMAX+1]; /* memory for l[-1..BMAX-1] */ 545 int *l = lx+1; /* stack of bits per table */ 546 register unsigned *p; /* pointer into c[], b[], or v[] */ 547 register struct huft *q; /* points to current table */ 548 struct huft r; /* table entry for structure assignment */ 549 struct huft *u[BMAX]; /* table stack */ | 512 513 514static int huft_build(gz,b, n, s, d, e, t, m, glbl) 515struct gzip *gz; 516unsigned *b; /* code lengths in bits (all assumed <= BMAX) */ 517unsigned n; /* number of codes (assumed <= N_MAX) */ 518unsigned s; /* number of simple-valued codes (0..s-1) */ 519const ush *d; /* list of base values for non-simple codes */ --- 20 unchanged lines hidden (view full) --- 540 register unsigned j; /* counter */ 541 register int k; /* number of bits in current code */ 542 int lx[BMAX+1]; /* memory for l[-1..BMAX-1] */ 543 int *l = lx+1; /* stack of bits per table */ 544 register unsigned *p; /* pointer into c[], b[], or v[] */ 545 register struct huft *q; /* points to current table */ 546 struct huft r; /* table entry for structure assignment */ 547 struct huft *u[BMAX]; /* table stack */ |
550 static unsigned v[N_MAX]; /* values in order of bit length */ | 548 unsigned v[N_MAX]; /* values in order of bit length */ |
551 register int w; /* bits before this table == (l * h) */ 552 unsigned x[BMAX+1]; /* bit offsets, then code stack */ 553 unsigned *xp; /* pointer into x */ 554 int y; /* number of dummy codes added */ 555 unsigned z; /* number of entries in current table */ 556 557 558 /* Generate counts for each bit length */ --- 350 unchanged lines hidden (view full) --- 909 bb = b; /* restore global bit buffer */ 910 bk = k; 911 return 0; 912} 913 914 915/* Globals for literal tables (built once) */ 916/* The real variables are now in the struct gz_global */ | 549 register int w; /* bits before this table == (l * h) */ 550 unsigned x[BMAX+1]; /* bit offsets, then code stack */ 551 unsigned *xp; /* pointer into x */ 552 int y; /* number of dummy codes added */ 553 unsigned z; /* number of entries in current table */ 554 555 556 /* Generate counts for each bit length */ --- 350 unchanged lines hidden (view full) --- 907 bb = b; /* restore global bit buffer */ 908 bk = k; 909 return 0; 910} 911 912 913/* Globals for literal tables (built once) */ 914/* The real variables are now in the struct gz_global */ |
917#define fixed_tl (glbl->fixed_tl) 918#define fixed_td (glbl->fixed_td) 919#define fixed_bl (glbl->fixed_bl) 920#define fixed_bd (glbl->fixed_bd) | 915#define fixed_tl (glbl->gz_fixed_tl) 916#define fixed_td (glbl->gz_fixed_td) 917#define fixed_bl (glbl->gz_fixed_bl) 918#define fixed_bd (glbl->gz_fixed_bd) |
921 922static int inflate_fixed(gz, glbl) 923struct gzip *gz; 924struct gz_global *glbl; 925/* decompress an inflated type 1 (fixed Huffman codes) block. We should 926 either replace this with a custom decoder, or at least precompute the 927 Huffman tables. */ 928{ --- 54 unchanged lines hidden (view full) --- 983 struct huft *tl; /* literal/length code table */ 984 struct huft *td; /* distance code table */ 985 int bl; /* lookup bits for tl */ 986 int bd; /* lookup bits for td */ 987 unsigned nb; /* number of bit length codes */ 988 unsigned nl; /* number of literal/length codes */ 989 unsigned nd; /* number of distance codes */ 990#ifdef PKZIP_BUG_WORKAROUND | 919 920static int inflate_fixed(gz, glbl) 921struct gzip *gz; 922struct gz_global *glbl; 923/* decompress an inflated type 1 (fixed Huffman codes) block. We should 924 either replace this with a custom decoder, or at least precompute the 925 Huffman tables. */ 926{ --- 54 unchanged lines hidden (view full) --- 981 struct huft *tl; /* literal/length code table */ 982 struct huft *td; /* distance code table */ 983 int bl; /* lookup bits for tl */ 984 int bd; /* lookup bits for td */ 985 unsigned nb; /* number of bit length codes */ 986 unsigned nl; /* number of literal/length codes */ 987 unsigned nd; /* number of distance codes */ 988#ifdef PKZIP_BUG_WORKAROUND |
991 static unsigned ll[288+32]; /* literal/length and distance code lengths */ | 989 unsigned ll[288+32]; /* literal/length and distance code lengths */ |
992#else | 990#else |
993 static unsigned ll[286+30]; /* literal/length and distance code lengths */ | 991 unsigned ll[286+30]; /* literal/length and distance code lengths */ |
994#endif 995 register ulg b; /* bit buffer */ 996 register unsigned k; /* number of bits in bit buffer */ 997 998 999 /* make local bit buffer */ 1000 Trace((stderr, "\ndynamic block")); 1001 b = bb; --- 218 unchanged lines hidden (view full) --- 1220 1221 /* return success */ 1222 Trace((stderr, "\n%u bytes in Huffman tables (%d/entry)\n", 1223 h * sizeof(struct huft), sizeof(struct huft))); 1224 return 0; 1225} 1226 1227 | 992#endif 993 register ulg b; /* bit buffer */ 994 register unsigned k; /* number of bits in bit buffer */ 995 996 997 /* make local bit buffer */ 998 Trace((stderr, "\ndynamic block")); 999 b = bb; --- 218 unchanged lines hidden (view full) --- 1218 1219 /* return success */ 1220 Trace((stderr, "\n%u bytes in Huffman tables (%d/entry)\n", 1221 h * sizeof(struct huft), sizeof(struct huft))); 1222 return 0; 1223} 1224 1225 |
1228 | 1226#if 0 1227/* Nobody uses this - why not? */ |
1229static int inflate_free(gz, glbl) 1230struct gzip *gz; 1231struct gz_global * glbl; 1232{ 1233 if (fixed_tl != (struct huft *)NULL) 1234 { 1235 huft_free(gz,fixed_td); 1236 huft_free(gz,fixed_tl); 1237 fixed_td = fixed_tl = (struct huft *)NULL; 1238 } 1239 return 0; 1240} | 1228static int inflate_free(gz, glbl) 1229struct gzip *gz; 1230struct gz_global * glbl; 1231{ 1232 if (fixed_tl != (struct huft *)NULL) 1233 { 1234 huft_free(gz,fixed_td); 1235 huft_free(gz,fixed_tl); 1236 fixed_td = fixed_tl = (struct huft *)NULL; 1237 } 1238 return 0; 1239} |
1240#endif |
|