--- compress-871020.c.old +++ compress-871020.c @@ -1,11 +1,21 @@ -#ifdef SCCSID -static char *SccsId = "@(#)compress.c 1.14 9/24/87"; -#endif /* SCCSID */ -static char rcs_ident[] = "Based on compress.c,v 4.0 85/07/30 12:50:00 joe Release"; +/* compress.c - Ancient "compress" utility. */ + +/* ------------------------------------------------------------ */ + +/* This is a Linux-compatible version of the ancient "compress" */ +/* utility. This version includes some minor compatibility */ +/* changes and buffer-overflow fixes. It also removes the */ +/* annoying 12-character filename-length limit that's present */ +/* in the original version. */ + +/* Note: These days, "compress" is used primarily during distro */ +/* bootstrap "build" procedures. */ + +/* ------------------------------------------------------------ */ + +static char rcs_ident[] = + "Based on compress.c,v 4.0 85/07/30 12:50:00 joe Release"; -/* - * Compress - data compression program - */ #define min(a,b) ((a>b) ? b : a) /* @@ -20,6 +30,7 @@ * SACREDMEM is the amount of physical memory saved for others; compress * will hog the rest. */ + #ifndef SACREDMEM #define SACREDMEM 0 #endif @@ -103,7 +114,8 @@ #endif /* - * a code_int must be able to hold 2**BITS values of type int, and also -1 + * a code_int must be able to hold 2**BITS values of type int, and + * also -1 */ #if BITS > 15 typedef long int code_int; @@ -123,14 +135,16 @@ #else typedef unsigned char char_type; #endif /* UCHAR */ + char_type magic_header[] = { "\037\235" }; /* 1F 9D */ /* Defines for third byte of header */ #define BIT_MASK 0x1f #define BLOCK_MASK 0x80 -/* Masks 0x40 and 0x20 are free. I think 0x20 should mean that there is - a fourth header byte (for expansion). -*/ + +/* Masks 0x40 and 0x20 are free. I think 0x20 should mean that + there is a fourth header byte (for expansion). */ + #define INIT_BITS 9 /* initial number of bits/code */ /* @@ -142,7 +156,6 @@ * Ken Turkowski (decvax!decwrl!turtlevax!ken) * James A. Woods (decvax!ihnp4!ames!jaw) * Joe Orost (decvax!vax135!petsd!joe) - * */ #include @@ -173,17 +186,24 @@ count_int htab6[8192]; count_int htab7[8192]; count_int htab8[HSIZE-65536]; -count_int * htab[9] = { - htab0, htab1, htab2, htab3, htab4, htab5, htab6, htab7, htab8 }; + +count_int *htab [9] = +{ + htab0, htab1, htab2, htab3, htab4, htab5, htab6, htab7, htab8 +}; #define htabof(i) (htab[(i) >> 13][(i) & 0x1fff]) + unsigned short code0tab[16384]; unsigned short code1tab[16384]; unsigned short code2tab[16384]; unsigned short code3tab[16384]; unsigned short code4tab[16384]; -unsigned short * codetab[5] = { - code0tab, code1tab, code2tab, code3tab, code4tab }; + +unsigned short *codetab [5] = +{ + code0tab, code1tab, code2tab, code3tab, code4tab +}; #define codetabof(i) (codetab[(i) >> 14][(i) & 0x3fff]) @@ -227,15 +247,17 @@ code_int getcode(); -Usage() { +Usage() +{ #ifdef DEBUG -fprintf(stderr,"Usage: compress [-dDVfc] [-b maxbits] [file ...]\n"); + fprintf(stderr,"Usage: compress [-dDVfc] [-b maxbits] [file ...]\n"); } int debug = 0; #else -fprintf(stderr,"Usage: compress [-dfvcV] [-b maxbits] [file ...]\n"); + fprintf(stderr,"Usage: compress [-dfvcV] [-b maxbits] [file ...]\n"); } #endif /* DEBUG */ + int nomagic = 0; /* Use a 3-byte magic number header, unless old file */ int zcat_flg = 0; /* Write output on stdout, suppress messages */ int quiet = 1; /* don't tell me about compression */ @@ -255,16 +277,39 @@ */ #define FIRST 257 /* first free entry */ #define CLEAR 256 /* table clear output code */ +#define OFSIZE 256 +#define OFMAXL (OFSIZE-10) int force = 0; -char ofname [100]; +char ofname [OFSIZE]; #ifdef DEBUG int verbose = 0; #endif /* DEBUG */ + +#if defined (linux) && !defined (__musl__) +__sighandler_t bgnd_flag; +#else int (*bgnd_flag)(); +#endif int do_decomp = 0; +/* ------------------------------------------------------------ */ + +void onintr (int n) +{ + unlink ( ofname ); + exit ( 1 ); +} + +void oops (int n) /* wild pointer -- assume bad input */ +{ + if ( do_decomp == 1 ) + fprintf ( stderr, "uncompress: corrupt input\n" ); + unlink ( ofname ); + exit ( 1 ); +} + /***************************************************************** * TAG( main ) * @@ -302,16 +347,14 @@ * procedure needs no input table, but tracks the way the table was built. */ -main( argc, argv ) +main (argc, argv) register int argc; char **argv; { int overwrite = 0; /* Do not overwrite unless given -f flag */ - char tempname[100]; + char tempname[OFSIZE]; char **filelist, **fileptr; - char *cp, *rindex(), *malloc(); + char *cp, *rindex(); struct stat statbuf; - extern onintr(), oops(); - if ( (bgnd_flag = signal ( SIGINT, SIG_IGN )) != SIG_IGN ) { signal ( SIGINT, onintr ); @@ -425,6 +468,14 @@ if (*filelist != NULL) { for (fileptr = filelist; *fileptr; fileptr++) { + + if (strlen (*fileptr) > OFMAXL) + { + fprintf (stderr, + "Error: Filename is too long:\n%s\n", *fileptr); + exit (1); + } + exit_stat = 0; if (do_decomp != 0) { /* DECOMPRESSION */ /* Check for .Z suffix */ @@ -491,6 +542,8 @@ /* Generate output filename */ strcpy(ofname, *fileptr); + +#ifdef NOTDEF /* Not used */ #ifndef BSD4_2 /* Short filenames */ if ((cp=rindex(ofname,'/')) != NULL) cp++; else cp = ofname; @@ -499,6 +552,8 @@ continue; } #endif /* BSD4_2 Long filenames allowed */ +#endif /* Endif NOTDEF */ + strcat(ofname, ".Z"); } /* Check for overwrite of existing file */ @@ -1252,20 +1307,6 @@ } } -onintr ( ) -{ - unlink ( ofname ); - exit ( 1 ); -} - -oops ( ) /* wild pointer -- assume bad input */ -{ - if ( do_decomp == 1 ) - fprintf ( stderr, "uncompress: corrupt input\n" ); - unlink ( ofname ); - exit ( 1 ); -} - cl_block () /* table clear for block compress */ { register long int rat;