home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 9 Archive / 09-Archive.zip / OS2ARC_S.ZIP / ARCLZW.C < prev    next >
Text File  |  1987-10-15  |  28KB  |  806 lines

  1. /*  ARC - Archive utility - ARCLZW
  2.  
  3. $define(tag,$$segment(@1,$$index(@1,=)+1))#
  4. $define(version,Version $tag(
  5. TED_VERSION DB =1.88), created on $tag(
  6. TED_DATE DB =01/20/86) at $tag(
  7. TED_TIME DB =16:47:04))#
  8. $undefine(tag)#
  9.     $version
  10.  
  11. (C) COPYRIGHT 1985 by System Enhancement Associates; ALL RIGHTS RESERVED
  12.  
  13.     By:  Thom Henderson
  14.  
  15.     Description:
  16.          This file contains the routines used to implement Lempel-Zev
  17.          data compression, which calls for building a coding table on
  18.          the fly.  This form of compression is especially good for encoding
  19.          files which contain repeated strings, and can often give dramatic
  20.          improvements over traditional Huffman SQueezing.
  21.  
  22.     Language:
  23.          Computer Innovations Optimizing C86
  24.  
  25.     Programming notes:
  26.          In this section I am drawing heavily on the COMPRESS program
  27.          from UNIX.  The basic method is taken from "A Technique for High
  28.          Performance Data Compression", Terry A. Welch, IEEE Computer
  29.          Vol 17, No 6 (June 1984), pp 8-19.  Also see "Knuth's Fundamental
  30.          Algorithms", Donald Knuth, Vol 3, Section 6.4.
  31.  
  32.          As best as I can tell, this method works by tracing down a hash
  33.          table of code strings where each entry has the property:
  34.  
  35.               if <string> <char> is in the table
  36.               then <string> is in the table.
  37. */
  38. #include <stdio.h>
  39. #include "arc.h"
  40.  
  41. /* definitions for older style crunching */
  42.  
  43. #define FALSE    0
  44. #define TRUE     !FALSE
  45. #define TABSIZE  4096
  46. #define NO_PRED  0xFFFF
  47. #define EMPTY    0xFFFF
  48. #define NOT_FND  0xFFFF
  49.  
  50. static unsigned int inbuf;             /* partial input code storage */
  51. static int sp;                         /* current stack pointer */
  52.  
  53. static struct entry                    /* string table entry format */
  54. {   char used;                         /* true when this entry is in use */
  55.     unsigned int next;                 /* ptr to next in collision list */
  56.     unsigned int predecessor;          /* code for preceeding string */
  57.     unsigned char follower;            /* char following string */
  58. }   string_tab[TABSIZE];               /* the code string table */
  59.  
  60.  
  61. /* definitions for the new dynamic Lempel-Zev crunching */
  62.  
  63. #define BITS   12                      /* maximum bits per code */
  64. #define HSIZE  5003                    /* 80% occupancy */
  65. #define INIT_BITS 9                    /* initial number of bits/code */
  66.  
  67. static int n_bits;                     /* number of bits/code */
  68. static int maxcode;                    /* maximum code, given n_bits */
  69. #define MAXCODE(n)      ((1<<(n)) - 1) /* maximum code calculation */
  70. static int maxcodemax =  1 << BITS;    /* largest possible code (+1) */
  71.  
  72. static char buf[BITS];                 /* input/output buffer */
  73.  
  74. static unsigned char lmask[9] =        /* left side masks */
  75. {   0xff, 0xfe, 0xfc, 0xf8, 0xf0, 0xe0, 0xc0, 0x80, 0x00 };
  76. static unsigned char rmask[9] =        /* right side masks */
  77. {   0x00, 0x01, 0x03, 0x07, 0x0f, 0x1f, 0x3f, 0x7f, 0xff};
  78.  
  79. static int offset;                     /* byte offset for code output */
  80. static long in_count;                  /* length of input */
  81. static long bytes_out;                 /* length of compressed output */
  82. static unsigned int ent;
  83.  
  84. /* To save much memory (which we badly need at this point), we overlay
  85.  * the table used by the previous version of Lempel-Zev with those used
  86.  * by the new version.  Since no two of these routines will be used
  87.  * together, we can safely do this.  Note that the tables used for Huffman
  88.  * squeezing may NOT overlay these, since squeezing and crunching are done
  89.  * in parallel.
  90.  */
  91.  
  92. static long *htab = (long *) string_tab; /* hash code table   (crunch) */ /*mpl*/
  93. static unsigned int codetab[HSIZE];    /* string code table (crunch) */
  94.  
  95. static unsigned int *prefix = codetab; /* prefix code table (uncrunch) */
  96. static unsigned char *suffix = (char *)string_tab;  /* suffix table (uncrunch) */ /*mpl*/
  97.  
  98. static int free_ent;                   /* first unused entry */
  99. static int firstcmp;                   /* true at start of compression */
  100. static unsigned char stack[HSIZE];     /* local push/pop stack */
  101.  
  102. /*
  103.  * block compression parameters -- after all codes are used up,
  104.  * and compression rate changes, start over.
  105.  */
  106.  
  107. static int clear_flg;
  108. static long ratio;
  109. #define CHECK_GAP 10000                /* ratio check interval */
  110. static long checkpoint;
  111.  
  112. /*
  113.  * the next two codes should not be changed lightly, as they must not
  114.  * lie within the contiguous general code space.
  115.  */
  116. #define FIRST   257                    /* first free entry */
  117. #define CLEAR   256                    /* table clear output code */
  118.  
  119. static cl_block(t)                     /* table clear for block compress */
  120. FILE *t;                               /* our output file */
  121. {
  122.     long int rat;
  123.  
  124.     checkpoint = in_count + CHECK_GAP;
  125.  
  126.     if(in_count > 0x007fffff)          /* shift will overflow */
  127.     {    rat = bytes_out >> 8;
  128.          if(rat == 0)                  /* Don't divide by zero */
  129.               rat = 0x7fffffff;
  130.          else rat = in_count / rat;
  131.     }
  132.     else rat = (in_count<<8)/bytes_out;/* 8 fractional bits */
  133.  
  134.     if(rat > ratio)
  135.          ratio = rat;
  136.     else
  137.     {    ratio = 0;
  138.          /*setmem(htab,HSIZE*sizeof(long),0xff);*/
  139.      memset(htab,0xff,HSIZE*sizeof(long));        /* mpl MSC uses memset */
  140.          free_ent = FIRST;
  141.          clear_flg = 1;
  142.          putcode(CLEAR,t);
  143.     }
  144. }
  145.  
  146. /*****************************************************************
  147.  *
  148.  * Output a given code.
  149.  * Inputs:
  150.  *      code:   A n_bits-bit integer.  If == -1, then EOF.  This assumes
  151.  *              that n_bits =< (long)wordsize - 1.
  152.  * Outputs:
  153.  *      Outputs code to the file.
  154.  * Assumptions:
  155.  *      Chars are 8 bits long.
  156.  * Algorithm:
  157.  *      Maintain a BITS character long buffer (so that 8 codes will
  158.  * fit in it exactly).  When the buffer fills up empty it and start over.
  159.  */
  160.  
  161. static putcode(code,t)                 /* output a code */
  162. int code;                              /* code to output */
  163. FILE *t;                               /* where to put it */
  164. {
  165.     int r_off = offset;                /* right offset */
  166.     int bits = n_bits;                 /* bits to go */
  167.     char *bp = buf;                    /* buffer pointer */
  168.     int n;                             /* index */
  169.  
  170.     if(code >= 0)                      /* if a real code */
  171.     {    /*
  172.           * Get to the first byte.
  173.           */
  174.          bp += (r_off >> 3);
  175.          r_off &= 7;
  176.  
  177.          /*
  178.           * Since code is always >= 8 bits, only need to mask the first
  179.           * hunk on the left.
  180.           */
  181.          *bp = (*bp&rmask[r_off]) | (code<<r_off) & lmask[r_off];
  182.          bp++;
  183.          bits -= (8 - r_off);
  184.          code >>= (8 - r_off);
  185.  
  186.          /* Get any 8 bit parts in the middle (<=1 for up to 16 bits). */
  187.          if(bits >= 8)
  188.          {    *bp++ = code;
  189.               code >>= 8;
  190.               bits -= 8;
  191.          }
  192.  
  193.          /* Last bits. */
  194.          if(bits)
  195.               *bp = code;
  196.  
  197.          offset += n_bits;
  198.  
  199.          if(offset == (n_bits << 3))
  200.          {    bp = buf;
  201.               bits = n_bits;
  202.               bytes_out += bits;
  203.               do
  204.                    putc_pak(*bp++,t);
  205.               while(--bits);
  206.               offset = 0;
  207.          }
  208.  
  209.          /*
  210.           * If the next entry is going to be too big for the code size,
  211.           * then increase it, if possible.
  212.           */
  213.          if(free_ent>maxcode || clear_flg>0)
  214.          {    /*
  215.                * Write the whole buffer, because the input side won't
  216.                * discover the size increase until after it has read it.
  217.                */
  218.               if(offset > 0)
  219.               {    bp = buf;           /* reset pointer for writing */
  220.                    bytes_out += n = n_bits;
  221.                    while(n--)
  222.                         putc_pak(*bp++,t);
  223.               }
  224.               offset = 0;
  225.  
  226.               if(clear_flg)            /* reset if clearing */
  227.               {    maxcode = MAXCODE(n_bits = INIT_BITS);
  228.                    clear_flg = 0;
  229.               }
  230.               else                     /* else use more bits */
  231.               {    n_bits++;
  232.                    if(n_bits == BITS)
  233.                         maxcode = maxcodemax;
  234.                    else
  235.                         maxcode = MAXCODE(n_bits);
  236.               }
  237.          }
  238.     }
  239.  
  240.     else                               /* dump the buffer on EOF */
  241.     {    bytes_out += n = (offset+7) / 8;
  242.  
  243.          if(offset > 0)
  244.               while(n--)
  245.                    putc_pak(*bp++,t);
  246.          offset = 0;
  247.     }
  248. }
  249.  
  250. /*****************************************************************
  251.  *
  252.  * Read one code from the standard input.  If EOF, return -1.
  253.  * Inputs:
  254.  *      cmpin
  255.  * Outputs:
  256.  *      code or -1 is returned.
  257.  */
  258.  
  259. static int getcode(f)                  /* get a code */
  260. FILE *f;                               /* file to get from */
  261. {
  262.     int code;
  263.     static int offset = 0, size = 0;
  264.     int r_off, bits;
  265.     unsigned char *bp = buf;
  266.  
  267.     if(clear_flg > 0 || offset >= size || free_ent > maxcode)
  268.     {    /*
  269.           * If the next entry will be too big for the current code
  270.           * size, then we must increase the size.  This implies reading
  271.           * a new buffer full, too.
  272.           */
  273.          if(free_ent > maxcode)
  274.          {    n_bits++;
  275.               if(n_bits == BITS)
  276.                    maxcode = maxcodemax;    /* won't get any bigger now */
  277.               else maxcode = MAXCODE(n_bits);
  278.          }
  279.          if(clear_flg > 0)
  280.          {    maxcode = MAXCODE(n_bits = INIT_BITS);
  281.               clear_flg = 0;
  282.          }
  283.  
  284.          for(size=0; size<n_bits; size++)
  285.          {    if((code=getc_unp(f))==EOF)
  286.                    break;
  287.               else buf[size] = code;
  288.          }
  289.          if(size <= 0)
  290.               return -1;               /* end of file */
  291.  
  292.          offset = 0;
  293.          /* Round size down to integral number of codes */
  294.          size = (size << 3)-(n_bits - 1);
  295.     }
  296.     r_off = offset;
  297.     bits = n_bits;
  298.  
  299.     /*
  300.      * Get to the first byte.
  301.      */
  302.     bp +=(r_off >> 3);
  303.     r_off &= 7;
  304.  
  305.     /* Get first part (low order bits) */
  306.     code = (*bp++ >> r_off);
  307.     bits -= 8 - r_off;
  308.     r_off = 8 - r_off;                 /* now, offset into code word */
  309.  
  310.     /* Get any 8 bit parts in the middle (<=1 for up to 16 bits). */
  311.     if(bits >= 8)
  312.     {    code |= *bp++ << r_off;
  313.          r_off += 8;
  314.          bits -= 8;
  315.     }
  316.     /* high order bits. */
  317.     code |= (*bp & rmask[bits]) << r_off;
  318.     offset += n_bits;
  319.  
  320.     return code;
  321. }
  322.  
  323. /*
  324.  * compress a file
  325.  *
  326.  * Algorithm:  use open addressing double hashing (no chaining) on the
  327.  * prefix code / next character combination.  We do a variant of Knuth's
  328.  * algorithm D (vol. 3, sec. 6.4) along with G. Knott's relatively-prime
  329.  * secondary probe.  Here, the modular division first probe is gives way
  330.  * to a faster exclusive-or manipulation.  Also do block compression with
  331.  * an adaptive reset, where the code table is cleared when the compression
  332.  * ratio decreases, but after the table fills.  The variable-length output
  333.  * codes are re-sized at this point, and a special CLEAR code is generated
  334.  * for the decompressor.
  335.  */
  336.  
  337. init_cm(f,t)                           /* initialize for compression */
  338. FILE *f;                               /* file we will be compressing */
  339. FILE *t;                               /* where we will put it */
  340. {
  341.     offset = 0;
  342.     bytes_out = 1;
  343.     clear_flg = 0;
  344.     ratio = 0;
  345.     in_count = 1;
  346.     checkpoint = CHECK_GAP;
  347.     maxcode = MAXCODE(n_bits = INIT_BITS);
  348.     free_ent = FIRST;
  349.     memset(htab,0xff,HSIZE*sizeof(long));    /*mpl*/
  350.     n_bits = INIT_BITS;                /* set starting code size */
  351.  
  352.     putc_pak(BITS,t);                  /* note our max code length */
  353.  
  354.     firstcmp = 1;                      /* next byte will be first */
  355. }
  356.  
  357. putc_cm(c,t)                           /* compress a character */
  358. unsigned char c;                       /* character to compress */
  359. FILE *t;                               /* where to put it */
  360. {
  361.     static long fcode;
  362.     static int hshift;
  363.     int i;
  364.     int disp;
  365.  
  366.     if(firstcmp)                       /* special case for first byte */
  367.     {    ent = c;                      /* remember first byte */
  368.  
  369.          hshift = 0;
  370.          for(fcode=(long)HSIZE;  fcode<65536L; fcode*=2L)
  371.               hshift++;
  372.          hshift = 8 - hshift;          /* set hash code range bound */
  373.  
  374.          firstcmp = 0;                 /* no longer first */
  375.          return;
  376.     }
  377.  
  378.     in_count++;
  379.     fcode =(long)(((long)c << BITS)+ent);
  380.     i = (c<<hshift)^ent;               /* xor hashing */
  381.  
  382.     if(htab[i]==fcode)
  383.     {    ent = codetab[i];
  384.          return;
  385.     }
  386.     else if(htab[i]<0)                 /* empty slot */
  387.          goto nomatch;
  388.     disp = HSIZE - i;                  /* secondary hash (after G.Knott) */
  389.     if(i == 0)
  390.          disp = 1;
  391.  
  392. probe:
  393.     if((i -= disp) < 0)
  394.          i += HSIZE;
  395.  
  396.     if(htab[i] == fcode)
  397.     {    ent = codetab[i];
  398.          return;
  399.     }
  400.     if(htab[i] > 0)
  401.          goto probe;
  402.  
  403. nomatch:
  404.     putcode(ent,t);
  405.     ent = c;
  406.     if(free_ent < maxcodemax)
  407.     {    codetab[i] = free_ent++;      /* code -> hashtable */
  408.          htab[i] = fcode;
  409.     }
  410.     else if((long int)in_count >= checkpoint)
  411.          cl_block(t);
  412. }
  413.  
  414. long pred_cm(t)                        /* finish compressing a file */
  415. FILE *t;                               /* where to put it */
  416. {
  417.     putcode(ent,t);                    /* put out the final code */
  418.     putcode(-1,t);                     /* tell output we are done */
  419.  
  420.     return bytes_out;                  /* say how big it got */
  421. }
  422.  
  423. /*
  424.  * Decompress a file.  This routine adapts to the codes in the file
  425.  * building the string table on-the-fly; requiring no table to be stored
  426.  * in the compressed file.  The tables used herein are shared with those of
  427.  * the compress() routine.  See the definitions above.
  428.  */
  429.  
  430. decomp(f,t)                            /* decompress a file */
  431. FILE *f;                               /* file to read codes from */
  432. FILE *t;                               /* file to write text to */
  433. {
  434.     unsigned char *stackp;
  435.     int finchar;
  436.     int code, oldcode, incode;
  437.  
  438.     if((code=getc_unp(f))!=BITS)
  439.          abort("File packed with %d bits, I can only handle %d",code,BITS);
  440.  
  441.     n_bits = INIT_BITS;                /* set starting code size */
  442.     clear_flg = 0;
  443.  
  444.     /*
  445.      * As above, initialize the first 256 entries in the table.
  446.      */
  447.     maxcode = MAXCODE(n_bits=INIT_BITS);
  448.     for(code = 255; code >= 0; code--)
  449.     {    prefix[code] = 0;
  450.          suffix[code] = (unsigned char)code;
  451.     }
  452.     free_ent = FIRST;
  453.  
  454.     finchar = oldcode = getcode(f);
  455.     if(oldcode == -1)                  /* EOF already? */
  456.          return;                       /* Get out of here */
  457.     putc_ncr((char)finchar,t);         /* first code must be 8 bits=char */
  458.     stackp = stack;
  459.  
  460.     while((code = getcode(f))> -1)
  461.     {    if(code==CLEAR)
  462.          {    for(code = 255; code >= 0; code--)
  463.                    prefix[code] = 0;
  464.               clear_flg = 1;
  465.               free_ent = FIRST - 1;
  466.               if((code=getcode(f))==-1)/* O, untimely death! */
  467.                    break;
  468.          }
  469.          incode = code;
  470.          /*
  471.           * Special case for KwKwK string.
  472.           */
  473.          if(code >= free_ent)
  474.          {    *stackp++ = finchar;
  475.               code = oldcode;
  476.          }
  477.  
  478.          /*
  479.           * Generate output characters in reverse order
  480.           */
  481.          while(code >= 256)
  482.          {    *stackp++ = suffix[code];
  483.               code = prefix[code];
  484.          }
  485.          *stackp++ = finchar = suffix[code];
  486.  
  487.          /*
  488.           * And put them out in forward order
  489.           */
  490.          do
  491.               putc_ncr(*--stackp,t);
  492.          while(stackp > stack);
  493.  
  494.          /*
  495.           * Generate the new entry.
  496.           */
  497.          if((code=free_ent) < maxcodemax)
  498.          {    prefix[code] = (unsigned short)oldcode;
  499.               suffix[code] = finchar;
  500.               free_ent = code+1;
  501.          }
  502.          /*
  503.           * Remember previous code.
  504.           */
  505.          oldcode = incode;
  506.     }
  507. }
  508.  
  509.  
  510. /*************************************************************************
  511.  * Please note how much trouble it can be to maintain upwards            *
  512.  * compatibility.  All that follows is for the sole purpose of unpacking *
  513.  * files which were packed using an older method.                        *
  514.  *************************************************************************/
  515.  
  516.  
  517. /*  The h() pointer points to the routine to use for calculating a hash
  518.     value.  It is set in the init routines to point to either of oldh()
  519.     or newh().
  520.  
  521.     oldh() calculates a hash value by taking the middle twelve bits
  522.     of the square of the key.
  523.  
  524.     newh() works somewhat differently, and was tried because it makes
  525.     ARC about 23% faster.  This approach was abandoned because dynamic
  526.     Lempel-Zev (above) works as well, and packs smaller also.  However,
  527.     inadvertent release of a developmental copy forces us to leave this in.
  528. */
  529.  
  530. static unsigned (*h)();                /* pointer to hash function */
  531.  
  532. static unsigned oldh(pred,foll)        /* old hash function */
  533. unsigned int pred;                     /* code for preceeding string */
  534. unsigned char foll;                    /* value of following char */
  535. {
  536.     long local;                        /* local hash value */
  537.  
  538.     local = (pred + foll) | 0x0800;    /* create the hash key */
  539.     local *= local;                    /* square it */
  540.     return (local >> 6) & 0x0FFF;      /* return the middle 12 bits */
  541. }
  542.  
  543. static unsigned newh(pred,foll)        /* new hash function */
  544. unsigned int pred;                     /* code for preceeding string */
  545. unsigned char foll;                    /* value of following char */
  546. {
  547.     return ((pred+foll)*15073)&0xFFF;  /* faster hash */
  548. }
  549.  
  550. /*  The eolist() function is used to trace down a list of entries with
  551.     duplicate keys until the last duplicate is found.
  552. */
  553.  
  554. static unsigned eolist(index)          /* find last duplicate */
  555. unsigned int index;
  556. {
  557.     int temp;
  558.  
  559.     while(temp=string_tab[index].next) /* while more duplicates */
  560.          index = temp;
  561.  
  562.     return index;
  563. }
  564.  
  565. /*  The hash() routine is used to find a spot in the hash table for a new
  566.     entry.  It performs a "hash and linear probe" lookup, using h() to
  567.     calculate the starting hash value and eolist() to perform the linear
  568.     probe.  This routine DOES NOT detect a table full condition.  That
  569.     MUST be checked for elsewhere.
  570. */
  571.  
  572. static unsigned hash(pred,foll)        /* find spot in the string table */
  573. unsigned int pred;                     /* code for preceeding string */
  574. unsigned char foll;                    /* char following string */
  575. {
  576.     unsigned int local, tempnext;      /* scratch storage */
  577.     struct entry *ep;                  /* allows faster table handling */
  578.  
  579.     local = (*h)(pred,foll);           /* get initial hash value */
  580.  
  581.     if(!string_tab[local].used)        /* if that spot is free */
  582.          return local;                 /* then that's all we need */
  583.  
  584.     else                               /* else a collision has occured */
  585.     {    local = eolist(local);        /* move to last duplicate */
  586.  
  587.          /*   We must find an empty spot. We start looking 101 places
  588.               down the table from the last duplicate.
  589.          */
  590.  
  591.          tempnext = (local+101) & 0x0FFF;
  592.          ep = &string_tab[tempnext];   /* initialize pointer */
  593.  
  594.          while(ep->used)               /* while empty spot not found */
  595.          {    if(++tempnext==TABSIZE)  /* if we are at the end */
  596.               {    tempnext = 0;       /* wrap to beginning of table*/
  597.                    ep = string_tab;
  598.               }
  599.               else ++ep;               /* point to next element in table */
  600.          }
  601.  
  602.          /*   local still has the pointer to the last duplicate, while
  603.               tempnext has the pointer to the spot we found.  We use
  604.               this to maintain the chain of pointers to duplicates.
  605.          */
  606.  
  607.          string_tab[local].next = tempnext;
  608.  
  609.          return tempnext;
  610.     }
  611. }
  612.  
  613. /*  The unhash() function is used to search the hash table for a given key.
  614.     Like hash(), it performs a hash and linear probe search.  It returns
  615.     either the number of the entry (if found) or NOT_FND (if not found).
  616. */
  617.  
  618. static unsigned unhash(pred,foll)      /* search string table for a key */
  619. unsigned int pred;                     /* code of preceeding string */
  620. unsigned char foll;                    /* character following string */
  621. {
  622.     unsigned int local, offset;        /* scratch storage */
  623.     struct entry *ep;                  /* this speeds up access */
  624.  
  625.     local = (*h)(pred,foll);           /* initial hash */
  626.  
  627.     while(1)
  628.     {    ep = &string_tab[local];      /* speed up table access */
  629.  
  630.          if((ep->predecessor==pred) && (ep->follower==foll))
  631.               return local;            /* we have a match */
  632.  
  633.          if(!ep->next)                 /* if no more duplicates */
  634.               return NOT_FND;          /* then key is not listed */
  635.  
  636.          local = ep->next;             /* move on to next duplicate */
  637.     }
  638. }
  639.  
  640. /*  The init_tab() routine is used to initialize our hash table.
  641.     You realize, of course, that "initialize" is a complete misnomer.
  642. */
  643.  
  644. static init_tab()                      /* set ground state in hash table */
  645. {
  646.     unsigned int i;                    /* table index */
  647.  
  648.     memset((char *)string_tab,0,sizeof(string_tab)); /*mpl*/
  649.  
  650.     for(i=0; i<256; i++)               /* list all single byte strings */
  651.          upd_tab(NO_PRED,i);
  652.  
  653.     inbuf = EMPTY;                     /* nothing is in our buffer */
  654. }
  655.  
  656. /*  The upd_tab routine is used to add a new entry to the string table.
  657.     As previously stated, no checks are made to ensure that the table
  658.     has any room.  This must be done elsewhere.
  659. */
  660.  
  661. upd_tab(pred,foll)                     /* add an entry to the table */
  662. unsigned int pred;                     /* code for preceeding string */
  663. unsigned int foll;                     /* character which follows string */
  664. {
  665.     struct entry *ep;                  /* pointer to current entry */
  666.  
  667.     /* calculate offset just once */
  668.  
  669.     ep = &string_tab[hash(pred,foll)];
  670.  
  671.     ep->used = TRUE;                   /* this spot is now in use */
  672.     ep->next = 0;                      /* no duplicates after this yet */
  673.     ep->predecessor = pred;            /* note code of preceeding string */
  674.     ep->follower = foll;               /* note char after string */
  675. }
  676.  
  677. /*  This algorithm encoded a file into twelve bit strings (three nybbles).
  678.     The gocode() routine is used to read these strings a byte (or two)
  679.     at a time.
  680. */
  681.  
  682. static gocode(fd)                      /* read in a twelve bit code */
  683. FILE *fd;                              /* file to get code from */
  684. {
  685. #ifdef NOSYSVPATCH
  686.     unsigned int localbuf,returnval;
  687. #else
  688.     int localbuf, returnval, intemp;    /* mpl - was unsigned */
  689. #endif
  690.  
  691.     if(inbuf==EMPTY)                   /* if on a code boundary */
  692.     {    if((localbuf=getc_unp(fd))==EOF)   /* get start of next code */
  693.               return EOF;              /* pass back end of file status */
  694.          localbuf &= 0xFF;             /* mask down to true byte value */
  695. /* mpl - sys-v patch here */
  696. #ifdef NOSYSVPATCH
  697.          if((inbuf=getc_unp(fd))==EOF) /* get end of code, start of next */
  698.               return EOF;              /* this should never happen */
  699.          inbuf &= 0xFF;                /* mask down to true byte value */
  700. #else
  701.           if((intemp=getc_unp(fd))==EOF) /* get end of code, start of next */
  702.                {inbuf=intemp; return EOF;}     /* this should never happen */
  703.           inbuf = intemp & 0xFF;           /* mask down to true byte value */
  704. #endif
  705.  
  706.          returnval = ((localbuf<<4)&0xFF0) + ((inbuf>>4)&0x00F);
  707.          inbuf &= 0x000F;              /* leave partial code pending */
  708.     }
  709.  
  710.     else                               /* buffer contains first nybble */
  711.     {    if((localbuf=getc_unp(fd))==EOF)
  712.               return EOF;
  713.          localbuf &= 0xFF;
  714.  
  715.          returnval = localbuf + ((inbuf<<8)&0xF00);
  716.          inbuf = EMPTY;                /* note no hanging nybbles */
  717.     }
  718.     return returnval;                  /* pass back assembled code */
  719. }
  720.  
  721. static push(c)                         /* push char onto stack */
  722. int c;                                 /* character to push */
  723. {
  724.     stack[sp] = ((char) c);            /* coerce integer into a char */
  725.  
  726.     if(++sp >= TABSIZE)
  727.          abort("Stack overflow\n");
  728. }
  729.  
  730. static int pop()                       /* pop character from stack */
  731. {
  732.     if(sp>0)
  733.          return ((int) stack[--sp]);   /* leave ptr at next empty slot */
  734.  
  735.     else return EMPTY;
  736. }
  737.  
  738. /***** LEMPEL-ZEV DECOMPRESSION *****/
  739.  
  740. static int code_count;                 /* needed to detect table full */
  741. static unsigned code;                  /* where we are so far */
  742. static int firstc;                     /* true only on first character */
  743.  
  744. init_ucr(new)                          /* get set for uncrunching */
  745. int new;                               /* true to use new hash function */
  746. {
  747.     if(new)                            /* set proper hash function */
  748.          h = newh;
  749.     else h = oldh;
  750.  
  751.     sp = 0;                            /* clear out the stack */
  752.     init_tab();                        /* set up atomic code definitions */
  753.     code_count = TABSIZE - 256;        /* note space left in table */
  754.     firstc = 1;                        /* true only on first code */
  755. }
  756.  
  757. int getc_ucr(f)                        /* get next uncrunched byte */
  758. FILE *f;                               /* file containing crunched data */
  759. {
  760.     unsigned int c;                    /* a character of input */
  761.     int code, newcode;
  762.     static int oldcode, finchar;
  763.     struct entry *ep;                  /* allows faster table handling */
  764.  
  765.     if(firstc)                         /* first code is always known */
  766.     {    firstc = FALSE;               /* but next will not be first */
  767.          oldcode = gocode(f);
  768.          return finchar = string_tab[oldcode].follower;
  769.     }
  770.  
  771.     if(!sp)                            /* if stack is empty */
  772.     {    if((code=newcode=gocode(f))==EOF)
  773.               return EOF;
  774.  
  775.          ep = &string_tab[code];       /* initialize pointer */
  776.  
  777.          if(!ep->used)                 /* if code isn't known */
  778.          {    code = oldcode;
  779.               ep = &string_tab[code];  /* re-initialize pointer */
  780.               push(finchar);
  781.          }
  782.  
  783.          while(ep->predecessor!=NO_PRED)
  784.          {    push(ep->follower);      /* decode string backwards */
  785.               code = ep->predecessor;
  786.               ep = &string_tab[code];
  787.          }
  788.  
  789.          push(finchar=ep->follower);   /* save first character also */
  790.  
  791.          /*   The above loop will terminate, one way or another,
  792.               with string_tab[code].follower equal to the first
  793.               character in the string.
  794.          */
  795.  
  796.          if(code_count)                /* if room left in string table */
  797.          {    upd_tab(oldcode,finchar);
  798.               --code_count;
  799.          }
  800.  
  801.          oldcode = newcode;
  802.     }
  803.  
  804.     return pop();                      /* return saved character */
  805. }
  806.