home *** CD-ROM | disk | FTP | other *** search
/ Assassins - Ultimate CD Games Collection 4 / Assassins 4 (1999)(Weird Science).iso / misc / omega / source / compress.c < prev    next >
Encoding:
C/C++ Source or Header  |  1997-05-02  |  37.6 KB  |  1,408 lines

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