home *** CD-ROM | disk | FTP | other *** search
/ Programmer 7500 / MAX_PROGRAMMERS.iso / PROGRAMS / UTILS / COMPRESS / ARC520S.ZIP / ARCUSQ.C < prev    next >
Encoding:
C/C++ Source or Header  |  1986-10-23  |  2.8 KB  |  94 lines

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