home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / misc / volume1 / 8707 / 67 / arcusq.c < prev    next >
Encoding:
C/C++ Source or Header  |  1990-07-13  |  3.3 KB  |  120 lines

  1. static char *RCSid = "$Header: arcusq.c,v 1.2 86/07/15 07:54:30 turner Exp $";
  2.  
  3. /*
  4.  * $Log:    arcusq.c,v $
  5.  * Hack-attack 1.3  86/12/20  01:23:45  wilhite@usceast.uucp
  6.  *     Bludgeoned into submission for VAX 11/780 BSD4.2
  7.  *    (ugly code, but fewer core dumps)
  8.  *
  9.  * Revision 1.2  86/07/15  07:54:30  turner
  10.  * 
  11.  * 
  12.  * Revision 1.1  86/06/26  15:01:07  turner
  13.  * initial version
  14.  * 
  15.  * 
  16.  */
  17.  
  18. /*  ARC - Archive utility - ARCUSQ
  19.  
  20. $define(tag,$$segment(@1,$$index(@1,=)+1))#
  21. $define(version,Version $tag(
  22. TED_VERSION DB =3.13), created on $tag(
  23. TED_DATE DB =01/30/86) at $tag(
  24. TED_TIME DB =20:11:42))#
  25. $undefine(tag)#
  26.     $version
  27.  
  28. (C) COPYRIGHT 1985 by System Enhancement Associates; ALL RIGHTS RESERVED
  29.  
  30.     By:  Thom Henderson
  31.  
  32.     Description:
  33.          This file contains the routines used to expand a file
  34.          which was packed using Huffman squeezing.
  35.  
  36.          Most of this code is taken from an USQ program by Richard
  37.          Greenlaw, which was adapted to CI-C86 by Robert J. Beilstein.
  38.  
  39.     Language:
  40.          Computer Innovations Optimizing C86
  41. */
  42. #include <stdio.h>
  43. #include "arc.h"
  44.  
  45. /* stuff for Huffman unsqueezing */
  46.  
  47. #define ERROR (-1)
  48.  
  49. #define SPEOF 256                      /* special endfile token */
  50. #define NUMVALS 257                    /* 256 data values plus SPEOF */
  51.  
  52. EXTERN struct nd                       /* decoding tree */
  53. { INT child[2];                      /* left, right */
  54. }   node[NUMVALS];                     /* use large buffer */
  55.  
  56. static INT bpos;                       /* last bit position read */
  57. static INT curin;                      /* last byte value read */
  58. static INT numnodes;                   /* number of nodes in decode tree */
  59.  
  60. static INT get_int(f)                  /* get an integer */
  61. FILE *f;                               /* file to get it from */
  62. {
  63.     INT i;
  64.  
  65.     i = getc_unp(f);
  66.     return (short)(i | (getc_unp(f)<<8));
  67. }
  68.  
  69. INT init_usq(f)                            /* initialize Huffman unsqueezing */
  70. FILE *f;                               /* file containing squeezed data */
  71. {
  72.  INT i;                             /* node index */
  73.  
  74.     bpos = 99;                         /* force initial read */
  75.  
  76.     numnodes = get_int(f);
  77.  
  78.     if(numnodes<0 || numnodes>=NUMVALS)
  79.          abort("File has an invalid decode tree");
  80.  
  81.     /* initialize for possible empty tree (SPEOF only) */
  82.  
  83.     node[0].child[0] = -(SPEOF + 1);
  84.     node[0].child[1] = -(SPEOF + 1);
  85.  
  86.     for(i=0; i<numnodes; ++i)          /* get decoding tree from file */
  87.     {    node[i].child[0] = get_int(f);
  88.          node[i].child[1] = get_int(f);
  89.     }
  90. }
  91.  
  92. INT getc_usq(f)                        /* get byte from squeezed file */
  93. FILE *f;                               /* file containing squeezed data */
  94. {
  95.  INT i;                             /* tree index */
  96.  
  97.     /* follow bit stream in tree to a leaf */
  98.  
  99.     for(i=0; i>=0; )                   /* work down(up?) from root */
  100.     {    if(++bpos>7)
  101.          {    if((curin=getc_unp(f)) == ERROR)
  102.                    return(ERROR);
  103.               bpos = 0;
  104.  
  105.               /* move a level deeper in tree */
  106.               i = node[i].child[1&curin];
  107.          }
  108.          else i = node[i].child[1 & (curin >>= 1)];
  109.     }
  110.  
  111.     /* decode fake node index to original data value */
  112.  
  113.     i = -(i + 1);
  114.  
  115.     /* decode special endfile token to normal EOF */
  116.  
  117.     i = (i==SPEOF) ? EOF : i;
  118.     return i;
  119. }
  120.