home *** CD-ROM | disk | FTP | other *** search
/ Frozen Fish 1: Amiga / FrozenFish-Apr94.iso / bbs / alib / d0xx / d050 / unixarc.lha / UnixArc / arcusq.c < prev    next >
Encoding:
C/C++ Source or Header  |  1987-01-17  |  2.7 KB  |  92 lines

  1. /*  ARC - Archive utility - ARCUSQ
  2.  
  3. (C) COPYRIGHT 1985 by System Enhancement Associates; ALL RIGHTS RESERVED
  4.  
  5.     By:  Thom Henderson
  6.  
  7.     Description:
  8.          This file contains the routines used to expand a file
  9.          which was packed using Huffman squeezing.
  10.  
  11.          Most of this code is taken from an USQ program by Richard
  12.          Greenlaw, which was adapted to CI-C86 by Robert J. Beilstein.
  13.  
  14.     Language:
  15.          Computer Innovations Optimizing C86
  16. */
  17. #include <stdio.h>
  18. #include "arc.h"
  19.  
  20. /* stuff for Huffman unsqueezing */
  21.  
  22. #define ERROR (-1)
  23.  
  24. #define SPEOF 256                      /* special endfile token */
  25. #define NUMVALS 257                    /* 256 data values plus SPEOF */
  26.  
  27. extern struct nd                       /* decoding tree */
  28. {   int child[2];                      /* left, right */
  29. }   node[NUMVALS];                     /* use large buffer */
  30.  
  31. static int bpos;                       /* last bit position read */
  32. static int curin;                      /* last byte value read */
  33. static int numnodes;                   /* number of nodes in decode tree */
  34.  
  35. static int get_int(f)                  /* get an integer */
  36. FILE *f;                               /* file to get it from */
  37. {
  38.     return getc_unp(f) | (getc_unp(f)<<8);
  39. }
  40.  
  41. init_usq(f)                            /* initialize Huffman unsqueezing */
  42. FILE *f;                               /* file containing squeezed data */
  43. {
  44.     int i;                             /* node index */
  45.  
  46.     bpos = 99;                         /* force initial read */
  47.  
  48.     numnodes = get_int(f);
  49.  
  50.     if(numnodes<0 || numnodes>=NUMVALS)
  51.          abort("File has an invalid decode tree");
  52.  
  53.     /* initialize for possible empty tree (SPEOF only) */
  54.  
  55.     node[0].child[0] = -(SPEOF + 1);
  56.     node[0].child[1] = -(SPEOF + 1);
  57.  
  58.     for(i=0; i<numnodes; ++i)          /* get decoding tree from file */
  59.     {    node[i].child[0] = get_int(f);
  60.          node[i].child[1] = get_int(f);
  61.     }
  62. }
  63.  
  64. int getc_usq(f)                        /* get byte from squeezed file */
  65. FILE *f;                               /* file containing squeezed data */
  66. {
  67.     int i;                             /* tree index */
  68.  
  69.     /* follow bit stream in tree to a leaf */
  70.  
  71.     for(i=0; i>=0; )                   /* work down(up?) from root */
  72.     {    if(++bpos>7)
  73.          {    if((curin=getc_unp(f)) == ERROR)
  74.                    return(ERROR);
  75.               bpos = 0;
  76.  
  77.               /* move a level deeper in tree */
  78.               i = node[i].child[1&curin];
  79.          }
  80.          else i = node[i].child[1 & (curin >>= 1)];
  81.     }
  82.  
  83.     /* decode fake node index to original data value */
  84.  
  85.     i = -(i + 1);
  86.  
  87.     /* decode special endfile token to normal EOF */
  88.  
  89.     i = (i==SPEOF) ? EOF : i;
  90.     return i;
  91. }
  92.