1 #ifndef lint 2 static char sccsid[] = "@(#)compress.c 5.8 (Berkeley) 01/10/86"; 3 #endif not lint 4 5 /* 6 * Compress - data compression program 7 */ 8 #define min(a,b) ((a>b) ? b : a) 9 10 /* 11 * machine variants which require cc -Dmachine: pdp11, z8000, pcxt 12 */ 13 14 /* 15 * Set USERMEM to the maximum amount of physical user memory available 16 * in bytes. USERMEM is used to determine the maximum BITS that can be used 17 * for compression. 18 * 19 * SACREDMEM is the amount of physical memory saved for others; compress 20 * will hog the rest. 21 */ 22 #ifndef SACREDMEM 23 #define SACREDMEM 0 24 #endif 25 26 #ifndef USERMEM 27 # define USERMEM 450000 /* default user memory */ 28 #endif 29 30 #ifdef interdata /* (Perkin-Elmer) */ 31 #define SIGNED_COMPARE_SLOW /* signed compare is slower than unsigned */ 32 #endif 33 34 #ifdef pdp11 35 # define BITS 12 /* max bits/code for 16-bit machine */ 36 # define NO_UCHAR /* also if "unsigned char" functions as signed char */ 37 # undef USERMEM 38 #endif /* pdp11 */ /* don't forget to compile with -i */ 39 40 #ifdef z8000 41 # define BITS 12 42 # undef vax /* weird preprocessor */ 43 # undef USERMEM 44 #endif /* z8000 */ 45 46 #ifdef pcxt 47 # define BITS 12 48 # undef USERMEM 49 #endif /* pcxt */ 50 51 #ifdef USERMEM 52 # if USERMEM >= (433484+SACREDMEM) 53 # define PBITS 16 54 # else 55 # if USERMEM >= (229600+SACREDMEM) 56 # define PBITS 15 57 # else 58 # if USERMEM >= (127536+SACREDMEM) 59 # define PBITS 14 60 # else 61 # if USERMEM >= (73464+SACREDMEM) 62 # define PBITS 13 63 # else 64 # define PBITS 12 65 # endif 66 # endif 67 # endif 68 # endif 69 # undef USERMEM 70 #endif /* USERMEM */ 71 72 #ifdef PBITS /* Preferred BITS for this memory size */ 73 # ifndef BITS 74 # define BITS PBITS 75 # endif BITS 76 #endif /* PBITS */ 77 78 #if BITS == 16 79 # define HSIZE 69001 /* 95% occupancy */ 80 #endif 81 #if BITS == 15 82 # define HSIZE 35023 /* 94% occupancy */ 83 #endif 84 #if BITS == 14 85 # define HSIZE 18013 /* 91% occupancy */ 86 #endif 87 #if BITS == 13 88 # define HSIZE 9001 /* 91% occupancy */ 89 #endif 90 #if BITS <= 12 91 # define HSIZE 5003 /* 80% occupancy */ 92 #endif 93 94 #ifdef M_XENIX /* Stupid compiler can't handle arrays with */ 95 # if BITS == 16 /* more than 65535 bytes - so we fake it */ 96 # define XENIX_16 97 # else 98 # if BITS > 13 /* Code only handles BITS = 12, 13, or 16 */ 99 # define BITS 13 100 # endif 101 # endif 102 #endif 103 104 /* 105 * a code_int must be able to hold 2**BITS values of type int, and also -1 106 */ 107 #if BITS > 15 108 typedef long int code_int; 109 #else 110 typedef int code_int; 111 #endif 112 113 #ifdef SIGNED_COMPARE_SLOW 114 typedef unsigned long int count_int; 115 typedef unsigned short int count_short; 116 #else 117 typedef long int count_int; 118 #endif 119 120 #ifdef NO_UCHAR 121 typedef char char_type; 122 #else 123 typedef unsigned char char_type; 124 #endif /* UCHAR */ 125 char_type magic_header[] = { "\037\235" }; /* 1F 9D */ 126 127 /* Defines for third byte of header */ 128 #define BIT_MASK 0x1f 129 #define BLOCK_MASK 0x80 130 /* Masks 0x40 and 0x20 are free. I think 0x20 should mean that there is 131 a fourth header byte (for expansion). 132 */ 133 #define INIT_BITS 9 /* initial number of bits/code */ 134 135 /* 136 * compress.c - File compression ala IEEE Computer, June 1984. 137 * 138 * Authors: Spencer W. Thomas (decvax!harpo!utah-cs!utah-gr!thomas) 139 * Jim McKie (decvax!mcvax!jim) 140 * Steve Davies (decvax!vax135!petsd!peora!srd) 141 * Ken Turkowski (decvax!decwrl!turtlevax!ken) 142 * James A. Woods (decvax!ihnp4!ames!jaw) 143 * Joe Orost (decvax!vax135!petsd!joe) 144 * 145 * $Header: compress.c,v 4.0 85/07/30 12:50:00 joe Release $ 146 * $Log: compress.c,v $ 147 * Revision 4.0 85/07/30 12:50:00 joe 148 * Removed ferror() calls in output routine on every output except first. 149 * Prepared for release to the world. 150 * 151 * Revision 3.6 85/07/04 01:22:21 joe 152 * Remove much wasted storage by overlaying hash table with the tables 153 * used by decompress: tab_suffix[1<<BITS], stack[8000]. Updated USERMEM 154 * computations. Fixed dump_tab() DEBUG routine. 155 * 156 * Revision 3.5 85/06/30 20:47:21 jaw 157 * Change hash function to use exclusive-or. Rip out hash cache. These 158 * speedups render the megamemory version defunct, for now. Make decoder 159 * stack global. Parts of the RCS trunks 2.7, 2.6, and 2.1 no longer apply. 160 * 161 * Revision 3.4 85/06/27 12:00:00 ken 162 * Get rid of all floating-point calculations by doing all compression ratio 163 * calculations in fixed point. 164 * 165 * Revision 3.3 85/06/24 21:53:24 joe 166 * Incorporate portability suggestion for M_XENIX. Got rid of text on #else 167 * and #endif lines. Cleaned up #ifdefs for vax and interdata. 168 * 169 * Revision 3.2 85/06/06 21:53:24 jaw 170 * Incorporate portability suggestions for Z8000, IBM PC/XT from mailing list. 171 * Default to "quiet" output (no compression statistics). 172 * 173 * Revision 3.1 85/05/12 18:56:13 jaw 174 * Integrate decompress() stack speedups (from early pointer mods by McKie). 175 * Repair multi-file USERMEM gaffe. Unify 'force' flags to mimic semantics 176 * of SVR2 'pack'. Streamline block-compress table clear logic. Increase 177 * output byte count by magic number size. 178 * 179 * Revision 3.0 84/11/27 11:50:00 petsd!joe 180 * Set HSIZE depending on BITS. Set BITS depending on USERMEM. Unrolled 181 * loops in clear routines. Added "-C" flag for 2.0 compatibility. Used 182 * unsigned compares on Perkin-Elmer. Fixed foreground check. 183 * 184 * Revision 2.7 84/11/16 19:35:39 ames!jaw 185 * Cache common hash codes based on input statistics; this improves 186 * performance for low-density raster images. Pass on #ifdef bundle 187 * from Turkowski. 188 * 189 * Revision 2.6 84/11/05 19:18:21 ames!jaw 190 * Vary size of hash tables to reduce time for small files. 191 * Tune PDP-11 hash function. 192 * 193 * Revision 2.5 84/10/30 20:15:14 ames!jaw 194 * Junk chaining; replace with the simpler (and, on the VAX, faster) 195 * double hashing, discussed within. Make block compression standard. 196 * 197 * Revision 2.4 84/10/16 11:11:11 ames!jaw 198 * Introduce adaptive reset for block compression, to boost the rate 199 * another several percent. (See mailing list notes.) 200 * 201 * Revision 2.3 84/09/22 22:00:00 petsd!joe 202 * Implemented "-B" block compress. Implemented REVERSE sorting of tab_next. 203 * Bug fix for last bits. Changed fwrite to putchar loop everywhere. 204 * 205 * Revision 2.2 84/09/18 14:12:21 ames!jaw 206 * Fold in news changes, small machine typedef from thomas, 207 * #ifdef interdata from joe. 208 * 209 * Revision 2.1 84/09/10 12:34:56 ames!jaw 210 * Configured fast table lookup for 32-bit machines. 211 * This cuts user time in half for b <= FBITS, and is useful for news batching 212 * from VAX to PDP sites. Also sped up decompress() [fwrite->putc] and 213 * added signal catcher [plus beef in writeerr()] to delete effluvia. 214 * 215 * Revision 2.0 84/08/28 22:00:00 petsd!joe 216 * Add check for foreground before prompting user. Insert maxbits into 217 * compressed file. Force file being uncompressed to end with ".Z". 218 * Added "-c" flag and "zcat". Prepared for release. 219 * 220 * Revision 1.10 84/08/24 18:28:00 turtlevax!ken 221 * Will only compress regular files (no directories), added a magic number 222 * header (plus an undocumented -n flag to handle old files without headers), 223 * added -f flag to force overwriting of possibly existing destination file, 224 * otherwise the user is prompted for a response. Will tack on a .Z to a 225 * filename if it doesn't have one when decompressing. Will only replace 226 * file if it was compressed. 227 * 228 * Revision 1.9 84/08/16 17:28:00 turtlevax!ken 229 * Removed scanargs(), getopt(), added .Z extension and unlimited number of 230 * filenames to compress. Flags may be clustered (-Ddvb12) or separated 231 * (-D -d -v -b 12), or combination thereof. Modes and other status is 232 * copied with copystat(). -O bug for 4.2 seems to have disappeared with 233 * 1.8. 234 * 235 * Revision 1.8 84/08/09 23:15:00 joe 236 * Made it compatible with vax version, installed jim's fixes/enhancements 237 * 238 * Revision 1.6 84/08/01 22:08:00 joe 239 * Sped up algorithm significantly by sorting the compress chain. 240 * 241 * Revision 1.5 84/07/13 13:11:00 srd 242 * Added C version of vax asm routines. Changed structure to arrays to 243 * save much memory. Do unsigned compares where possible (faster on 244 * Perkin-Elmer) 245 * 246 * Revision 1.4 84/07/05 03:11:11 thomas 247 * Clean up the code a little and lint it. (Lint complains about all 248 * the regs used in the asm, but I'm not going to "fix" this.) 249 * 250 * Revision 1.3 84/07/05 02:06:54 thomas 251 * Minor fixes. 252 * 253 * Revision 1.2 84/07/05 00:27:27 thomas 254 * Add variable bit length output. 255 * 256 */ 257 static char rcs_ident[] = "$Header: compress.c,v 4.0 85/07/30 12:50:00 joe Release $"; 258 259 #include <stdio.h> 260 #include <ctype.h> 261 #include <signal.h> 262 #include <sys/types.h> 263 #include <sys/stat.h> 264 265 #define ARGVAL() (*++(*argv) || (--argc && *++argv)) 266 267 int n_bits; /* number of bits/code */ 268 int maxbits = BITS; /* user settable max # bits/code */ 269 code_int maxcode; /* maximum code, given n_bits */ 270 code_int maxmaxcode = 1 << BITS; /* should NEVER generate this code */ 271 #ifdef COMPATIBLE /* But wrong! */ 272 # define MAXCODE(n_bits) (1 << (n_bits) - 1) 273 #else 274 # define MAXCODE(n_bits) ((1 << (n_bits)) - 1) 275 #endif /* COMPATIBLE */ 276 277 #ifdef XENIX_16 278 count_int htab0[8192]; 279 count_int htab1[8192]; 280 count_int htab2[8192]; 281 count_int htab3[8192]; 282 count_int htab4[8192]; 283 count_int htab5[8192]; 284 count_int htab6[8192]; 285 count_int htab7[8192]; 286 count_int htab8[HSIZE-65536]; 287 count_int * htab[9] = { 288 htab0, htab1, htab2, htab3, htab4, htab5, htab6, htab7, htab8 }; 289 290 #define htabof(i) (htab[(i) >> 13][(i) & 0x1fff]) 291 unsigned short code0tab[16384]; 292 unsigned short code1tab[16384]; 293 unsigned short code2tab[16384]; 294 unsigned short code3tab[16384]; 295 unsigned short code4tab[16384]; 296 unsigned short * codetab[5] = { 297 code0tab, code1tab, code2tab, code3tab, code4tab }; 298 299 #define codetabof(i) (codetab[(i) >> 14][(i) & 0x3fff]) 300 301 #else /* Normal machine */ 302 count_int htab [HSIZE]; 303 unsigned short codetab [HSIZE]; 304 #define htabof(i) htab[i] 305 #define codetabof(i) codetab[i] 306 #endif /* XENIX_16 */ 307 code_int hsize = HSIZE; /* for dynamic table sizing */ 308 count_int fsize; 309 310 /* 311 * To save much memory, we overlay the table used by compress() with those 312 * used by decompress(). The tab_prefix table is the same size and type 313 * as the codetab. The tab_suffix table needs 2**BITS characters. We 314 * get this from the beginning of htab. The output stack uses the rest 315 * of htab, and contains characters. There is plenty of room for any 316 * possible stack (stack used to be 8000 characters). 317 */ 318 319 #define tab_prefixof(i) codetabof(i) 320 #ifdef XENIX_16 321 # define tab_suffixof(i) ((char_type *)htab[(i)>>15])[(i) & 0x7fff] 322 # define de_stack ((char_type *)(htab2)) 323 #else /* Normal machine */ 324 # define tab_suffixof(i) ((char_type *)(htab))[i] 325 # define de_stack ((char_type *)&tab_suffixof(1<<BITS)) 326 #endif /* XENIX_16 */ 327 328 code_int free_ent = 0; /* first unused entry */ 329 int exit_stat = 0; 330 331 code_int getcode(); 332 333 Usage() { 334 #ifdef DEBUG 335 fprintf(stderr,"Usage: compress [-dDVfc] [-b maxbits] [file ...]\n"); 336 } 337 int debug = 0; 338 #else 339 fprintf(stderr,"Usage: compress [-fvc] [-b maxbits] [file ...]\n"); 340 } 341 #endif /* DEBUG */ 342 int nomagic = 0; /* Use a 3-byte magic number header, unless old file */ 343 int zcat_flg = 0; /* Write output on stdout, suppress messages */ 344 int precious = 1; /* Don't unlink output file on interrupt */ 345 int quiet = 1; /* don't tell me about compression */ 346 347 /* 348 * block compression parameters -- after all codes are used up, 349 * and compression rate changes, start over. 350 */ 351 int block_compress = BLOCK_MASK; 352 int clear_flg = 0; 353 long int ratio = 0; 354 #define CHECK_GAP 10000 /* ratio check interval */ 355 count_int checkpoint = CHECK_GAP; 356 /* 357 * the next two codes should not be changed lightly, as they must not 358 * lie within the contiguous general code space. 359 */ 360 #define FIRST 257 /* first free entry */ 361 #define CLEAR 256 /* table clear output code */ 362 363 int force = 0; 364 char ofname [100]; 365 #ifdef DEBUG 366 int verbose = 0; 367 #endif /* DEBUG */ 368 int (*bgnd_flag)(); 369 370 int do_decomp = 0; 371 372 /***************************************************************** 373 * TAG( main ) 374 * 375 * Algorithm from "A Technique for High Performance Data Compression", 376 * Terry A. Welch, IEEE Computer Vol 17, No 6 (June 1984), pp 8-19. 377 * 378 * Usage: compress [-dfvc] [-b bits] [file ...] 379 * Inputs: 380 * -d: If given, decompression is done instead. 381 * 382 * -c: Write output on stdout, don't remove original. 383 * 384 * -b: Parameter limits the max number of bits/code. 385 * 386 * -f: Forces output file to be generated, even if one already 387 * exists, and even if no space is saved by compressing. 388 * If -f is not used, the user will be prompted if stdin is 389 * a tty, otherwise, the output file will not be overwritten. 390 * 391 * -v: Write compression statistics 392 * 393 * file ...: Files to be compressed. If none specified, stdin 394 * is used. 395 * Outputs: 396 * file.Z: Compressed form of file with same mode, owner, and utimes 397 * or stdout (if stdin used as input) 398 * 399 * Assumptions: 400 * When filenames are given, replaces with the compressed version 401 * (.Z suffix) only if the file decreases in size. 402 * Algorithm: 403 * Modified Lempel-Ziv method (LZW). Basically finds common 404 * substrings and replaces them with a variable size code. This is 405 * deterministic, and can be done on the fly. Thus, the decompression 406 * procedure needs no input table, but tracks the way the table was built. 407 */ 408 409 main( argc, argv ) 410 register int argc; char **argv; 411 { 412 int overwrite = 0; /* Do not overwrite unless given -f flag */ 413 char tempname[100]; 414 char **filelist, **fileptr; 415 char *cp, *rindex(), *malloc(); 416 struct stat statbuf; 417 extern onintr(), oops(); 418 419 420 if ( (bgnd_flag = signal ( SIGINT, SIG_IGN )) != SIG_IGN ) { 421 signal ( SIGINT, onintr ); 422 signal ( SIGSEGV, oops ); 423 } 424 425 #ifdef COMPATIBLE 426 nomagic = 1; /* Original didn't have a magic number */ 427 #endif /* COMPATIBLE */ 428 429 filelist = fileptr = (char **)(malloc(argc * sizeof(*argv))); 430 *filelist = NULL; 431 432 if((cp = rindex(argv[0], '/')) != 0) { 433 cp++; 434 } else { 435 cp = argv[0]; 436 } 437 if(strcmp(cp, "uncompress") == 0) { 438 do_decomp = 1; 439 } else if(strcmp(cp, "zcat") == 0) { 440 do_decomp = 1; 441 zcat_flg = 1; 442 } 443 444 #ifdef BSD4_2 445 /* 4.2BSD dependent - take it out if not */ 446 setlinebuf( stderr ); 447 #endif /* BSD4_2 */ 448 449 /* Argument Processing 450 * All flags are optional. 451 * -D => debug 452 * -V => print Version; debug verbose 453 * -d => do_decomp 454 * -v => unquiet 455 * -f => force overwrite of output file 456 * -n => no header: useful to uncompress old files 457 * -b maxbits => maxbits. If -b is specified, then maxbits MUST be 458 * given also. 459 * -c => cat all output to stdout 460 * -C => generate output compatible with compress 2.0. 461 * if a string is left, must be an input filename. 462 */ 463 for (argc--, argv++; argc > 0; argc--, argv++) { 464 if (**argv == '-') { /* A flag argument */ 465 while (*++(*argv)) { /* Process all flags in this arg */ 466 switch (**argv) { 467 #ifdef DEBUG 468 case 'D': 469 debug = 1; 470 break; 471 case 'V': 472 verbose = 1; 473 version(); 474 break; 475 #else 476 case 'V': 477 version(); 478 break; 479 #endif /* DEBUG */ 480 case 'v': 481 quiet = 0; 482 break; 483 case 'd': 484 do_decomp = 1; 485 break; 486 case 'f': 487 case 'F': 488 overwrite = 1; 489 force = 1; 490 break; 491 case 'n': 492 nomagic = 1; 493 break; 494 case 'C': 495 block_compress = 0; 496 break; 497 case 'b': 498 if (!ARGVAL()) { 499 fprintf(stderr, "Missing maxbits\n"); 500 Usage(); 501 exit(1); 502 } 503 maxbits = atoi(*argv); 504 goto nextarg; 505 case 'c': 506 zcat_flg = 1; 507 break; 508 case 'q': 509 quiet = 1; 510 break; 511 default: 512 fprintf(stderr, "Unknown flag: '%c'; ", **argv); 513 Usage(); 514 exit(1); 515 } 516 } 517 } 518 else { /* Input file name */ 519 *fileptr++ = *argv; /* Build input file list */ 520 *fileptr = NULL; 521 /* process nextarg; */ 522 } 523 nextarg: continue; 524 } 525 526 if(maxbits < INIT_BITS) maxbits = INIT_BITS; 527 if (maxbits > BITS) maxbits = BITS; 528 maxmaxcode = 1 << maxbits; 529 530 if (*filelist != NULL) { 531 for (fileptr = filelist; *fileptr; fileptr++) { 532 exit_stat = 0; 533 if (do_decomp != 0) { /* DECOMPRESSION */ 534 /* Check for .Z suffix */ 535 if (strcmp(*fileptr + strlen(*fileptr) - 2, ".Z") != 0) { 536 /* No .Z: tack one on */ 537 strcpy(tempname, *fileptr); 538 strcat(tempname, ".Z"); 539 *fileptr = tempname; 540 } 541 /* Open input file */ 542 if ((freopen(*fileptr, "r", stdin)) == NULL) { 543 perror(*fileptr); continue; 544 } 545 /* Check the magic number */ 546 if (nomagic == 0) { 547 if ((getchar() != (magic_header[0] & 0xFF)) 548 || (getchar() != (magic_header[1] & 0xFF))) { 549 fprintf(stderr, "%s: not in compressed format\n", 550 *fileptr); 551 continue; 552 } 553 maxbits = getchar(); /* set -b from file */ 554 block_compress = maxbits & BLOCK_MASK; 555 maxbits &= BIT_MASK; 556 maxmaxcode = 1 << maxbits; 557 if(maxbits > BITS) { 558 fprintf(stderr, 559 "%s: compressed with %d bits, can only handle %d bits\n", 560 *fileptr, maxbits, BITS); 561 continue; 562 } 563 } 564 /* Generate output filename */ 565 strcpy(ofname, *fileptr); 566 ofname[strlen(*fileptr) - 2] = '\0'; /* Strip off .Z */ 567 } else { /* COMPRESSION */ 568 if (strcmp(*fileptr + strlen(*fileptr) - 2, ".Z") == 0) { 569 fprintf(stderr, "%s: already has .Z suffix -- no change\n", 570 *fileptr); 571 continue; 572 } 573 /* Open input file */ 574 if ((freopen(*fileptr, "r", stdin)) == NULL) { 575 perror(*fileptr); continue; 576 } 577 stat ( *fileptr, &statbuf ); 578 fsize = (long) statbuf.st_size; 579 /* 580 * tune hash table size for small files -- ad hoc, 581 * but the sizes match earlier #defines, which 582 * serve as upper bounds on the number of output codes. 583 */ 584 hsize = HSIZE; 585 if ( fsize < (1 << 12) ) 586 hsize = min ( 5003, HSIZE ); 587 else if ( fsize < (1 << 13) ) 588 hsize = min ( 9001, HSIZE ); 589 else if ( fsize < (1 << 14) ) 590 hsize = min ( 18013, HSIZE ); 591 else if ( fsize < (1 << 15) ) 592 hsize = min ( 35023, HSIZE ); 593 else if ( fsize < 47000 ) 594 hsize = min ( 50021, HSIZE ); 595 596 /* Generate output filename */ 597 strcpy(ofname, *fileptr); 598 #ifndef BSD4_2 /* Short filenames */ 599 if ((cp=rindex(ofname,'/')) != NULL) cp++; 600 else cp = ofname; 601 if (strlen(cp) > 12) { 602 fprintf(stderr,"%s: filename too long to tack on .Z\n",cp); 603 continue; 604 } 605 #endif /* BSD4_2 Long filenames allowed */ 606 strcat(ofname, ".Z"); 607 } 608 /* Check for overwrite of existing file */ 609 if (overwrite == 0 && zcat_flg == 0) { 610 if (stat(ofname, &statbuf) == 0) { 611 char response[2]; 612 response[0] = 'n'; 613 fprintf(stderr, "%s already exists;", ofname); 614 if (foreground()) { 615 fprintf(stderr, " do you wish to overwrite %s (y or n)? ", 616 ofname); 617 fflush(stderr); 618 read(2, response, 2); 619 while (response[1] != '\n') { 620 if (read(2, response+1, 1) < 0) { /* Ack! */ 621 perror("stderr"); break; 622 } 623 } 624 } 625 if (response[0] != 'y') { 626 fprintf(stderr, "\tnot overwritten\n"); 627 continue; 628 } 629 } 630 } 631 if(zcat_flg == 0) { /* Open output file */ 632 if (freopen(ofname, "w", stdout) == NULL) { 633 perror(ofname); 634 continue; 635 } 636 precious = 0; 637 if(!quiet) 638 fprintf(stderr, "%s: ", *fileptr); 639 } 640 641 /* Actually do the compression/decompression */ 642 if (do_decomp == 0) compress(); 643 #ifndef DEBUG 644 else decompress(); 645 #else 646 else if (debug == 0) decompress(); 647 else printcodes(); 648 if (verbose) dump_tab(); 649 #endif /* DEBUG */ 650 if(zcat_flg == 0) { 651 copystat(*fileptr, ofname); /* Copy stats */ 652 precious = 1; 653 if((exit_stat == 1) || (!quiet)) 654 putc('\n', stderr); 655 } 656 } 657 } else { /* Standard input */ 658 if (do_decomp == 0) { 659 compress(); 660 #ifdef DEBUG 661 if(verbose) dump_tab(); 662 #endif /* DEBUG */ 663 if(!quiet) 664 putc('\n', stderr); 665 } else { 666 /* Check the magic number */ 667 if (nomagic == 0) { 668 if ((getchar()!=(magic_header[0] & 0xFF)) 669 || (getchar()!=(magic_header[1] & 0xFF))) { 670 fprintf(stderr, "stdin: not in compressed format\n"); 671 exit(1); 672 } 673 maxbits = getchar(); /* set -b from file */ 674 block_compress = maxbits & BLOCK_MASK; 675 maxbits &= BIT_MASK; 676 maxmaxcode = 1 << maxbits; 677 fsize = 100000; /* assume stdin large for USERMEM */ 678 if(maxbits > BITS) { 679 fprintf(stderr, 680 "stdin: compressed with %d bits, can only handle %d bits\n", 681 maxbits, BITS); 682 exit(1); 683 } 684 } 685 #ifndef DEBUG 686 decompress(); 687 #else 688 if (debug == 0) decompress(); 689 else printcodes(); 690 if (verbose) dump_tab(); 691 #endif /* DEBUG */ 692 } 693 } 694 exit(exit_stat); 695 } 696 697 static int offset; 698 long int in_count = 1; /* length of input */ 699 long int bytes_out; /* length of compressed output */ 700 long int out_count = 0; /* # of codes output (for debugging) */ 701 702 /* 703 * compress stdin to stdout 704 * 705 * Algorithm: use open addressing double hashing (no chaining) on the 706 * prefix code / next character combination. We do a variant of Knuth's 707 * algorithm D (vol. 3, sec. 6.4) along with G. Knott's relatively-prime 708 * secondary probe. Here, the modular division first probe is gives way 709 * to a faster exclusive-or manipulation. Also do block compression with 710 * an adaptive reset, whereby the code table is cleared when the compression 711 * ratio decreases, but after the table fills. The variable-length output 712 * codes are re-sized at this point, and a special CLEAR code is generated 713 * for the decompressor. Late addition: construct the table according to 714 * file size for noticeable speed improvement on small files. Please direct 715 * questions about this implementation to ames!jaw. 716 */ 717 718 compress() { 719 register long fcode; 720 register code_int i = 0; 721 register int c; 722 register code_int ent; 723 #ifdef XENIX_16 724 register code_int disp; 725 #else /* Normal machine */ 726 register int disp; 727 #endif 728 register code_int hsize_reg; 729 register int hshift; 730 731 #ifndef COMPATIBLE 732 if (nomagic == 0) { 733 putchar(magic_header[0]); putchar(magic_header[1]); 734 putchar((char)(maxbits | block_compress)); 735 if(ferror(stdout)) 736 writeerr(); 737 } 738 #endif /* COMPATIBLE */ 739 740 offset = 0; 741 bytes_out = 3; /* includes 3-byte header mojo */ 742 out_count = 0; 743 clear_flg = 0; 744 ratio = 0; 745 in_count = 1; 746 checkpoint = CHECK_GAP; 747 maxcode = MAXCODE(n_bits = INIT_BITS); 748 free_ent = ((block_compress) ? FIRST : 256 ); 749 750 ent = getchar (); 751 752 hshift = 0; 753 for ( fcode = (long) hsize; fcode < 65536L; fcode *= 2L ) 754 hshift++; 755 hshift = 8 - hshift; /* set hash code range bound */ 756 757 hsize_reg = hsize; 758 cl_hash( (count_int) hsize_reg); /* clear hash table */ 759 760 #ifdef SIGNED_COMPARE_SLOW 761 while ( (c = getchar()) != (unsigned) EOF ) { 762 #else 763 while ( (c = getchar()) != EOF ) { 764 #endif 765 in_count++; 766 fcode = (long) (((long) c << maxbits) + ent); 767 i = ((c << hshift) ^ ent); /* xor hashing */ 768 769 if ( htabof (i) == fcode ) { 770 ent = codetabof (i); 771 continue; 772 } else if ( (long)htabof (i) < 0 ) /* empty slot */ 773 goto nomatch; 774 disp = hsize_reg - i; /* secondary hash (after G. Knott) */ 775 if ( i == 0 ) 776 disp = 1; 777 probe: 778 if ( (i -= disp) < 0 ) 779 i += hsize_reg; 780 781 if ( htabof (i) == fcode ) { 782 ent = codetabof (i); 783 continue; 784 } 785 if ( (long)htabof (i) > 0 ) 786 goto probe; 787 nomatch: 788 output ( (code_int) ent ); 789 out_count++; 790 ent = c; 791 #ifdef SIGNED_COMPARE_SLOW 792 if ( (unsigned) free_ent < (unsigned) maxmaxcode) { 793 #else 794 if ( free_ent < maxmaxcode ) { 795 #endif 796 codetabof (i) = free_ent++; /* code -> hashtable */ 797 htabof (i) = fcode; 798 } 799 else if ( (count_int)in_count >= checkpoint && block_compress ) 800 cl_block (); 801 } 802 /* 803 * Put out the final code. 804 */ 805 output( (code_int)ent ); 806 out_count++; 807 output( (code_int)-1 ); 808 809 /* 810 * Print out stats on stderr 811 */ 812 if(zcat_flg == 0 && !quiet) { 813 #ifdef DEBUG 814 fprintf( stderr, 815 "%ld chars in, %ld codes (%ld bytes) out, compression factor: ", 816 in_count, out_count, bytes_out ); 817 prratio( stderr, in_count, bytes_out ); 818 fprintf( stderr, "\n"); 819 fprintf( stderr, "\tCompression as in compact: " ); 820 prratio( stderr, in_count-bytes_out, in_count ); 821 fprintf( stderr, "\n"); 822 fprintf( stderr, "\tLargest code (of last block) was %d (%d bits)\n", 823 free_ent - 1, n_bits ); 824 #else /* !DEBUG */ 825 fprintf( stderr, "Compression: " ); 826 prratio( stderr, in_count-bytes_out, in_count ); 827 #endif /* DEBUG */ 828 } 829 if(bytes_out > in_count) /* exit(2) if no savings */ 830 exit_stat = 2; 831 return; 832 } 833 834 /***************************************************************** 835 * TAG( output ) 836 * 837 * Output the given code. 838 * Inputs: 839 * code: A n_bits-bit integer. If == -1, then EOF. This assumes 840 * that n_bits =< (long)wordsize - 1. 841 * Outputs: 842 * Outputs code to the file. 843 * Assumptions: 844 * Chars are 8 bits long. 845 * Algorithm: 846 * Maintain a BITS character long buffer (so that 8 codes will 847 * fit in it exactly). Use the VAX insv instruction to insert each 848 * code in turn. When the buffer fills up empty it and start over. 849 */ 850 851 static char buf[BITS]; 852 853 #ifndef vax 854 char_type lmask[9] = {0xff, 0xfe, 0xfc, 0xf8, 0xf0, 0xe0, 0xc0, 0x80, 0x00}; 855 char_type rmask[9] = {0x00, 0x01, 0x03, 0x07, 0x0f, 0x1f, 0x3f, 0x7f, 0xff}; 856 #endif /* vax */ 857 858 output( code ) 859 code_int code; 860 { 861 #ifdef DEBUG 862 static int col = 0; 863 #endif /* DEBUG */ 864 865 /* 866 * On the VAX, it is important to have the register declarations 867 * in exactly the order given, or the asm will break. 868 */ 869 register int r_off = offset, bits= n_bits; 870 register char * bp = buf; 871 872 #ifdef DEBUG 873 if ( verbose ) 874 fprintf( stderr, "%5d%c", code, 875 (col+=6) >= 74 ? (col = 0, '\n') : ' ' ); 876 #endif /* DEBUG */ 877 if ( code >= 0 ) { 878 #ifdef vax 879 /* VAX DEPENDENT!! Implementation on other machines is below. 880 * 881 * Translation: Insert BITS bits from the argument starting at 882 * offset bits from the beginning of buf. 883 */ 884 0; /* Work around for pcc -O bug with asm and if stmt */ 885 asm( "insv 4(ap),r11,r10,(r9)" ); 886 #else /* not a vax */ 887 /* 888 * byte/bit numbering on the VAX is simulated by the following code 889 */ 890 /* 891 * Get to the first byte. 892 */ 893 bp += (r_off >> 3); 894 r_off &= 7; 895 /* 896 * Since code is always >= 8 bits, only need to mask the first 897 * hunk on the left. 898 */ 899 *bp = (*bp & rmask[r_off]) | (code << r_off) & lmask[r_off]; 900 bp++; 901 bits -= (8 - r_off); 902 code >>= 8 - r_off; 903 /* Get any 8 bit parts in the middle (<=1 for up to 16 bits). */ 904 if ( bits >= 8 ) { 905 *bp++ = code; 906 code >>= 8; 907 bits -= 8; 908 } 909 /* Last bits. */ 910 if(bits) 911 *bp = code; 912 #endif /* vax */ 913 offset += n_bits; 914 if ( offset == (n_bits << 3) ) { 915 bp = buf; 916 bits = n_bits; 917 bytes_out += bits; 918 do 919 putchar(*bp++); 920 while(--bits); 921 offset = 0; 922 } 923 924 /* 925 * If the next entry is going to be too big for the code size, 926 * then increase it, if possible. 927 */ 928 if ( free_ent > maxcode || (clear_flg > 0)) 929 { 930 /* 931 * Write the whole buffer, because the input side won't 932 * discover the size increase until after it has read it. 933 */ 934 if ( offset > 0 ) { 935 if( fwrite( buf, 1, n_bits, stdout ) != n_bits) 936 writeerr(); 937 bytes_out += n_bits; 938 } 939 offset = 0; 940 941 if ( clear_flg ) { 942 maxcode = MAXCODE (n_bits = INIT_BITS); 943 clear_flg = 0; 944 } 945 else { 946 n_bits++; 947 if ( n_bits == maxbits ) 948 maxcode = maxmaxcode; 949 else 950 maxcode = MAXCODE(n_bits); 951 } 952 #ifdef DEBUG 953 if ( debug ) { 954 fprintf( stderr, "\nChange to %d bits\n", n_bits ); 955 col = 0; 956 } 957 #endif /* DEBUG */ 958 } 959 } else { 960 /* 961 * At EOF, write the rest of the buffer. 962 */ 963 if ( offset > 0 ) 964 fwrite( buf, 1, (offset + 7) / 8, stdout ); 965 bytes_out += (offset + 7) / 8; 966 offset = 0; 967 fflush( stdout ); 968 #ifdef DEBUG 969 if ( verbose ) 970 fprintf( stderr, "\n" ); 971 #endif /* DEBUG */ 972 if( ferror( stdout ) ) 973 writeerr(); 974 } 975 } 976 977 /* 978 * Decompress stdin to stdout. This routine adapts to the codes in the 979 * file building the "string" table on-the-fly; requiring no table to 980 * be stored in the compressed file. The tables used herein are shared 981 * with those of the compress() routine. See the definitions above. 982 */ 983 984 decompress() { 985 register char_type *stackp; 986 register int finchar; 987 register code_int code, oldcode, incode; 988 989 /* 990 * As above, initialize the first 256 entries in the table. 991 */ 992 maxcode = MAXCODE(n_bits = INIT_BITS); 993 for ( code = 255; code >= 0; code-- ) { 994 tab_prefixof(code) = 0; 995 tab_suffixof(code) = (char_type)code; 996 } 997 free_ent = ((block_compress) ? FIRST : 256 ); 998 999 finchar = oldcode = getcode(); 1000 if(oldcode == -1) /* EOF already? */ 1001 return; /* Get out of here */ 1002 putchar( (char)finchar ); /* first code must be 8 bits = char */ 1003 if(ferror(stdout)) /* Crash if can't write */ 1004 writeerr(); 1005 stackp = de_stack; 1006 1007 while ( (code = getcode()) > -1 ) { 1008 1009 if ( (code == CLEAR) && block_compress ) { 1010 for ( code = 255; code >= 0; code-- ) 1011 tab_prefixof(code) = 0; 1012 clear_flg = 1; 1013 free_ent = FIRST - 1; 1014 if ( (code = getcode ()) == -1 ) /* O, untimely death! */ 1015 break; 1016 } 1017 incode = code; 1018 /* 1019 * Special case for KwKwK string. 1020 */ 1021 if ( code >= free_ent ) { 1022 *stackp++ = finchar; 1023 code = oldcode; 1024 } 1025 1026 /* 1027 * Generate output characters in reverse order 1028 */ 1029 #ifdef SIGNED_COMPARE_SLOW 1030 while ( ((unsigned long)code) >= ((unsigned long)256) ) { 1031 #else 1032 while ( code >= 256 ) { 1033 #endif 1034 *stackp++ = tab_suffixof(code); 1035 code = tab_prefixof(code); 1036 } 1037 *stackp++ = finchar = tab_suffixof(code); 1038 1039 /* 1040 * And put them out in forward order 1041 */ 1042 do 1043 putchar ( *--stackp ); 1044 while ( stackp > de_stack ); 1045 1046 /* 1047 * Generate the new entry. 1048 */ 1049 if ( (code=free_ent) < maxmaxcode ) { 1050 tab_prefixof(code) = (unsigned short)oldcode; 1051 tab_suffixof(code) = finchar; 1052 free_ent = code+1; 1053 } 1054 /* 1055 * Remember previous code. 1056 */ 1057 oldcode = incode; 1058 } 1059 fflush( stdout ); 1060 if(ferror(stdout)) 1061 writeerr(); 1062 } 1063 1064 /***************************************************************** 1065 * TAG( getcode ) 1066 * 1067 * Read one code from the standard input. If EOF, return -1. 1068 * Inputs: 1069 * stdin 1070 * Outputs: 1071 * code or -1 is returned. 1072 */ 1073 1074 code_int 1075 getcode() { 1076 /* 1077 * On the VAX, it is important to have the register declarations 1078 * in exactly the order given, or the asm will break. 1079 */ 1080 register code_int code; 1081 static int offset = 0, size = 0; 1082 static char_type buf[BITS]; 1083 register int r_off, bits; 1084 register char_type *bp = buf; 1085 1086 if ( clear_flg > 0 || offset >= size || free_ent > maxcode ) { 1087 /* 1088 * If the next entry will be too big for the current code 1089 * size, then we must increase the size. This implies reading 1090 * a new buffer full, too. 1091 */ 1092 if ( free_ent > maxcode ) { 1093 n_bits++; 1094 if ( n_bits == maxbits ) 1095 maxcode = maxmaxcode; /* won't get any bigger now */ 1096 else 1097 maxcode = MAXCODE(n_bits); 1098 } 1099 if ( clear_flg > 0) { 1100 maxcode = MAXCODE (n_bits = INIT_BITS); 1101 clear_flg = 0; 1102 } 1103 size = fread( buf, 1, n_bits, stdin ); 1104 if ( size <= 0 ) 1105 return -1; /* end of file */ 1106 offset = 0; 1107 /* Round size down to integral number of codes */ 1108 size = (size << 3) - (n_bits - 1); 1109 } 1110 r_off = offset; 1111 bits = n_bits; 1112 #ifdef vax 1113 asm( "extzv r10,r9,(r8),r11" ); 1114 #else /* not a vax */ 1115 /* 1116 * Get to the first byte. 1117 */ 1118 bp += (r_off >> 3); 1119 r_off &= 7; 1120 /* Get first part (low order bits) */ 1121 #ifdef NO_UCHAR 1122 code = ((*bp++ >> r_off) & rmask[8 - r_off]) & 0xff; 1123 #else 1124 code = (*bp++ >> r_off); 1125 #endif /* NO_UCHAR */ 1126 bits -= (8 - r_off); 1127 r_off = 8 - r_off; /* now, offset into code word */ 1128 /* Get any 8 bit parts in the middle (<=1 for up to 16 bits). */ 1129 if ( bits >= 8 ) { 1130 #ifdef NO_UCHAR 1131 code |= (*bp++ & 0xff) << r_off; 1132 #else 1133 code |= *bp++ << r_off; 1134 #endif /* NO_UCHAR */ 1135 r_off += 8; 1136 bits -= 8; 1137 } 1138 /* high order bits. */ 1139 code |= (*bp & rmask[bits]) << r_off; 1140 #endif /* vax */ 1141 offset += n_bits; 1142 1143 return code; 1144 } 1145 1146 char * 1147 rindex(s, c) /* For those who don't have it in libc.a */ 1148 register char *s, c; 1149 { 1150 char *p; 1151 for (p = NULL; *s; s++) 1152 if (*s == c) 1153 p = s; 1154 return(p); 1155 } 1156 1157 #ifdef DEBUG 1158 printcodes() 1159 { 1160 /* 1161 * Just print out codes from input file. For debugging. 1162 */ 1163 code_int code; 1164 int col = 0, bits; 1165 1166 bits = n_bits = INIT_BITS; 1167 maxcode = MAXCODE(n_bits); 1168 free_ent = ((block_compress) ? FIRST : 256 ); 1169 while ( ( code = getcode() ) >= 0 ) { 1170 if ( (code == CLEAR) && block_compress ) { 1171 free_ent = FIRST - 1; 1172 clear_flg = 1; 1173 } 1174 else if ( free_ent < maxmaxcode ) 1175 free_ent++; 1176 if ( bits != n_bits ) { 1177 fprintf(stderr, "\nChange to %d bits\n", n_bits ); 1178 bits = n_bits; 1179 col = 0; 1180 } 1181 fprintf(stderr, "%5d%c", code, (col+=6) >= 74 ? (col = 0, '\n') : ' ' ); 1182 } 1183 putc( '\n', stderr ); 1184 exit( 0 ); 1185 } 1186 1187 code_int sorttab[1<<BITS]; /* sorted pointers into htab */ 1188 1189 dump_tab() /* dump string table */ 1190 { 1191 register int i, first; 1192 register ent; 1193 #define STACK_SIZE 15000 1194 int stack_top = STACK_SIZE; 1195 register c; 1196 1197 if(do_decomp == 0) { /* compressing */ 1198 register int flag = 1; 1199 1200 for(i=0; i<hsize; i++) { /* build sort pointers */ 1201 if((long)htabof(i) >= 0) { 1202 sorttab[codetabof(i)] = i; 1203 } 1204 } 1205 first = block_compress ? FIRST : 256; 1206 for(i = first; i < free_ent; i++) { 1207 fprintf(stderr, "%5d: \"", i); 1208 de_stack[--stack_top] = '\n'; 1209 de_stack[--stack_top] = '"'; 1210 stack_top = in_stack((htabof(sorttab[i])>>maxbits)&0xff, 1211 stack_top); 1212 for(ent=htabof(sorttab[i]) & ((1<<maxbits)-1); 1213 ent > 256; 1214 ent=htabof(sorttab[ent]) & ((1<<maxbits)-1)) { 1215 stack_top = in_stack(htabof(sorttab[ent]) >> maxbits, 1216 stack_top); 1217 } 1218 stack_top = in_stack(ent, stack_top); 1219 fwrite( &de_stack[stack_top], 1, STACK_SIZE-stack_top, stderr); 1220 stack_top = STACK_SIZE; 1221 } 1222 } else if(!debug) { /* decompressing */ 1223 1224 for ( i = 0; i < free_ent; i++ ) { 1225 ent = i; 1226 c = tab_suffixof(ent); 1227 if ( isascii(c) && isprint(c) ) 1228 fprintf( stderr, "%5d: %5d/'%c' \"", 1229 ent, tab_prefixof(ent), c ); 1230 else 1231 fprintf( stderr, "%5d: %5d/\\%03o \"", 1232 ent, tab_prefixof(ent), c ); 1233 de_stack[--stack_top] = '\n'; 1234 de_stack[--stack_top] = '"'; 1235 for ( ; ent != NULL; 1236 ent = (ent >= FIRST ? tab_prefixof(ent) : NULL) ) { 1237 stack_top = in_stack(tab_suffixof(ent), stack_top); 1238 } 1239 fwrite( &de_stack[stack_top], 1, STACK_SIZE - stack_top, stderr ); 1240 stack_top = STACK_SIZE; 1241 } 1242 } 1243 } 1244 1245 int 1246 in_stack(c, stack_top) 1247 register c, stack_top; 1248 { 1249 if ( (isascii(c) && isprint(c) && c != '\\') || c == ' ' ) { 1250 de_stack[--stack_top] = c; 1251 } else { 1252 switch( c ) { 1253 case '\n': de_stack[--stack_top] = 'n'; break; 1254 case '\t': de_stack[--stack_top] = 't'; break; 1255 case '\b': de_stack[--stack_top] = 'b'; break; 1256 case '\f': de_stack[--stack_top] = 'f'; break; 1257 case '\r': de_stack[--stack_top] = 'r'; break; 1258 case '\\': de_stack[--stack_top] = '\\'; break; 1259 default: 1260 de_stack[--stack_top] = '0' + c % 8; 1261 de_stack[--stack_top] = '0' + (c / 8) % 8; 1262 de_stack[--stack_top] = '0' + c / 64; 1263 break; 1264 } 1265 de_stack[--stack_top] = '\\'; 1266 } 1267 return stack_top; 1268 } 1269 #endif /* DEBUG */ 1270 1271 writeerr() 1272 { 1273 perror ( ofname ); 1274 unlink ( ofname ); 1275 exit ( 1 ); 1276 } 1277 1278 copystat(ifname, ofname) 1279 char *ifname, *ofname; 1280 { 1281 struct stat statbuf; 1282 int mode; 1283 time_t timep[2]; 1284 1285 fclose(stdout); 1286 if (stat(ifname, &statbuf)) { /* Get stat on input file */ 1287 perror(ifname); 1288 return; 1289 } 1290 if ((statbuf.st_mode & S_IFMT/*0170000*/) != S_IFREG/*0100000*/) { 1291 if(quiet) 1292 fprintf(stderr, "%s: ", ifname); 1293 fprintf(stderr, " -- not a regular file: unchanged"); 1294 exit_stat = 1; 1295 } else if (statbuf.st_nlink > 1) { 1296 if(quiet) 1297 fprintf(stderr, "%s: ", ifname); 1298 fprintf(stderr, " -- has %d other links: unchanged", 1299 statbuf.st_nlink - 1); 1300 exit_stat = 1; 1301 } else if (exit_stat == 2 && (!force)) { /* No compression: remove file.Z */ 1302 if(!quiet) 1303 fprintf(stderr, " -- file unchanged"); 1304 } else { /* ***** Successful Compression ***** */ 1305 exit_stat = 0; 1306 mode = statbuf.st_mode & 07777; 1307 if (chmod(ofname, mode)) /* Copy modes */ 1308 perror(ofname); 1309 chown(ofname, statbuf.st_uid, statbuf.st_gid); /* Copy ownership */ 1310 timep[0] = statbuf.st_atime; 1311 timep[1] = statbuf.st_mtime; 1312 utime(ofname, timep); /* Update last accessed and modified times */ 1313 if (unlink(ifname)) /* Remove input file */ 1314 perror(ifname); 1315 if(!quiet) 1316 fprintf(stderr, " -- replaced with %s", ofname); 1317 return; /* Successful return */ 1318 } 1319 1320 /* Unsuccessful return -- one of the tests failed */ 1321 if (unlink(ofname)) 1322 perror(ofname); 1323 } 1324 /* 1325 * This routine returns 1 if we are running in the foreground and stderr 1326 * is a tty. 1327 */ 1328 foreground() 1329 { 1330 if(bgnd_flag) { /* background? */ 1331 return(0); 1332 } else { /* foreground */ 1333 if(isatty(2)) { /* and stderr is a tty */ 1334 return(1); 1335 } else { 1336 return(0); 1337 } 1338 } 1339 } 1340 1341 onintr ( ) 1342 { 1343 if (!precious) 1344 unlink ( ofname ); 1345 exit ( 1 ); 1346 } 1347 1348 oops ( ) /* wild pointer -- assume bad input */ 1349 { 1350 if ( do_decomp == 1 ) 1351 fprintf ( stderr, "uncompress: corrupt input\n" ); 1352 unlink ( ofname ); 1353 exit ( 1 ); 1354 } 1355 1356 cl_block () /* table clear for block compress */ 1357 { 1358 register long int rat; 1359 1360 checkpoint = in_count + CHECK_GAP; 1361 #ifdef DEBUG 1362 if ( debug ) { 1363 fprintf ( stderr, "count: %ld, ratio: ", in_count ); 1364 prratio ( stderr, in_count, bytes_out ); 1365 fprintf ( stderr, "\n"); 1366 } 1367 #endif /* DEBUG */ 1368 1369 if(in_count > 0x007fffff) { /* shift will overflow */ 1370 rat = bytes_out >> 8; 1371 if(rat == 0) { /* Don't divide by zero */ 1372 rat = 0x7fffffff; 1373 } else { 1374 rat = in_count / rat; 1375 } 1376 } else { 1377 rat = (in_count << 8) / bytes_out; /* 8 fractional bits */ 1378 } 1379 if ( rat > ratio ) { 1380 ratio = rat; 1381 } else { 1382 ratio = 0; 1383 #ifdef DEBUG 1384 if(verbose) 1385 dump_tab(); /* dump string table */ 1386 #endif 1387 cl_hash ( (count_int) hsize ); 1388 free_ent = FIRST; 1389 clear_flg = 1; 1390 output ( (code_int) CLEAR ); 1391 #ifdef DEBUG 1392 if(debug) 1393 fprintf ( stderr, "clear\n" ); 1394 #endif /* DEBUG */ 1395 } 1396 } 1397 1398 cl_hash(hsize) /* reset code table */ 1399 register count_int hsize; 1400 { 1401 #ifndef XENIX_16 /* Normal machine */ 1402 register count_int *htab_p = htab+hsize; 1403 #else 1404 register j; 1405 register long k = hsize; 1406 register count_int *htab_p; 1407 #endif 1408 register long i; 1409 register long m1 = -1; 1410 1411 #ifdef XENIX_16 1412 for(j=0; j<=8 && k>=0; j++,k-=8192) { 1413 i = 8192; 1414 if(k < 8192) { 1415 i = k; 1416 } 1417 htab_p = &(htab[j][i]); 1418 i -= 16; 1419 if(i > 0) { 1420 #else 1421 i = hsize - 16; 1422 #endif 1423 do { /* might use Sys V memset(3) here */ 1424 *(htab_p-16) = m1; 1425 *(htab_p-15) = m1; 1426 *(htab_p-14) = m1; 1427 *(htab_p-13) = m1; 1428 *(htab_p-12) = m1; 1429 *(htab_p-11) = m1; 1430 *(htab_p-10) = m1; 1431 *(htab_p-9) = m1; 1432 *(htab_p-8) = m1; 1433 *(htab_p-7) = m1; 1434 *(htab_p-6) = m1; 1435 *(htab_p-5) = m1; 1436 *(htab_p-4) = m1; 1437 *(htab_p-3) = m1; 1438 *(htab_p-2) = m1; 1439 *(htab_p-1) = m1; 1440 htab_p -= 16; 1441 } while ((i -= 16) >= 0); 1442 #ifdef XENIX_16 1443 } 1444 } 1445 #endif 1446 for ( i += 16; i > 0; i-- ) 1447 *--htab_p = m1; 1448 } 1449 1450 prratio(stream, num, den) 1451 FILE *stream; 1452 long int num, den; 1453 { 1454 register int q; /* Doesn't need to be long */ 1455 1456 if(num > 214748L) { /* 2147483647/10000 */ 1457 q = num / (den / 10000L); 1458 } else { 1459 q = 10000L * num / den; /* Long calculations, though */ 1460 } 1461 if (q < 0) { 1462 putc('-', stream); 1463 q = -q; 1464 } 1465 fprintf(stream, "%d.%02d%%", q / 100, q % 100); 1466 } 1467 1468 version() 1469 { 1470 fprintf(stderr, "%s, Berkeley 5.8 01/10/86\n", rcs_ident); 1471 fprintf(stderr, "Options: "); 1472 #ifdef vax 1473 fprintf(stderr, "vax, "); 1474 #endif 1475 #ifdef NO_UCHAR 1476 fprintf(stderr, "NO_UCHAR, "); 1477 #endif 1478 #ifdef SIGNED_COMPARE_SLOW 1479 fprintf(stderr, "SIGNED_COMPARE_SLOW, "); 1480 #endif 1481 #ifdef XENIX_16 1482 fprintf(stderr, "XENIX_16, "); 1483 #endif 1484 #ifdef COMPATIBLE 1485 fprintf(stderr, "COMPATIBLE, "); 1486 #endif 1487 #ifdef DEBUG 1488 fprintf(stderr, "DEBUG, "); 1489 #endif 1490 #ifdef BSD4_2 1491 fprintf(stderr, "BSD4_2, "); 1492 #endif 1493 fprintf(stderr, "BITS = %d\n", BITS); 1494 } 1495