home *** CD-ROM | disk | FTP | other *** search
/ Programmer 7500 / MAX_PROGRAMMERS.iso / INFO / MODEM / YAM.ZIP / YAM10.C < prev    next >
Encoding:
C/C++ Source or Header  |  1988-07-28  |  3.4 KB  |  180 lines

  1. /*
  2. >> yam10.c 04-Mar-84
  3.     File unsqueezer module taken from Richard Greenlaw's USQ
  4. */
  5. #include "yam.h"
  6.  
  7. /* *** Stuff for first translation module *** */
  8. #define DLE 0x90
  9. /* *** Stuff for second translation module *** */
  10. #define SPEOF 256    /* special endfile token */
  11. #define LARGE 30000
  12.  
  13. unsqueeze()
  14.  
  15. {
  16.     int i, c;
  17.     char cc;
  18.  
  19.     char *p;
  20.     int numnodes;        /* size of decoding tree */
  21.     unsigned linect;    /* count of number of lines previewed */
  22.     char origname[14];    /* Original file name without drive */
  23.  
  24.     /* Initialization */
  25.     linect = 0;
  26.     init_cr();
  27.     init_huff();
  28.  
  29.     /* Process rest of header (RECOGNIZE already read) */
  30.     getw(&fin);    /* ignore checksum ... */
  31.  
  32.     /* Get original file name */
  33.     p = origname;    /* send it to array */
  34.     do
  35.     {
  36.         *p = getc(&fin);
  37.     } while(*p++ != '\0');
  38.  
  39.     printf("%s -> %s\n", Tname, origname);
  40.  
  41.     numnodes = getw(&fin);
  42.     if(numnodes < 0 || numnodes >= NUMVALS)
  43.     {
  44.         printf("%s has invalid decode tree size\n", Tname);
  45.         return 1;
  46.     }
  47.  
  48.     /* Initialize for possible empty tree (SPEOF only) */
  49.     dnode[0].children[0] = -(SPEOF + 1);
  50.     dnode[0].children[1] = -(SPEOF + 1);
  51.  
  52.     /* Get decoding tree from file */
  53.     for(i = 0; i < numnodes; ++i)
  54.     {
  55.         dnode[i].children[0] = getw(&fin);
  56.         dnode[i].children[1] = getw(&fin);
  57.     }
  58.  
  59.     while((c = getcr()) != EOF)
  60.     {
  61. #ifdef RESTRICTED
  62.         if (c == '\n')  /* new line? */
  63.         {
  64.             if (++linect >= MAXLINES)
  65.             {    printf("\n\n\t[== Squeezed file %d-line limit exceeded ==]",MAXLINES);
  66.                 printf("\n\t[Use S mode to transfer file to your system]\n\n");
  67.                 return CNTRLK;
  68.             }
  69.         }
  70. #endif
  71.         if( !(c=putcty(c)))
  72.             continue;
  73.         if(c == CNTRLC || c == CAN || c == CNTRLK)
  74.         {
  75.             return c;
  76.         }
  77.     }
  78.     return CPMEOF;
  79. }
  80.  
  81.  
  82. /* initialize decoding functions */
  83.  
  84. init_cr()
  85.  
  86. {
  87.     repct = 0;
  88. }
  89.  
  90. init_huff()
  91. {
  92.     bpos = 99;    /* force initial read */
  93. }
  94.  
  95. /* Get bytes with decoding - this decodes repetition,
  96.  * calls getuhuff to decode file stream into byte
  97.  * level code with only repetition encoding.
  98.  *
  99.  * The code is simple passing through of bytes except
  100.  * that DLE is encoded as DLE-zero and other values
  101.  * repeated more than twice are encoded as value-DLE-count.
  102.  */
  103.  
  104. int
  105. getcr()
  106.  
  107. {
  108.     int c;
  109.  
  110.     if(repct > 0)
  111.     {
  112.         /* Expanding a repeated char */
  113.         --repct;
  114.         return value;
  115.     }
  116.     else
  117.     {
  118.         /* Nothing unusual */
  119.         if((c = getuhuff()) != DLE)
  120.         {
  121.             /* It's not the special delimiter */
  122.             value = c;
  123.             if(value == EOF)
  124.                 repct = LARGE;
  125.             return value;
  126.         }
  127.         else
  128.         {
  129.             /* Special token */
  130.             if((repct = getuhuff()) == 0)
  131.             {
  132.                 /* DLE, zero represents DLE */
  133.                 return DLE;
  134.             }
  135.             else
  136.             {
  137.                 /* Begin expanding repetition */
  138.                 repct -= 2;    /* 2nd time */
  139.                 return value;
  140.             }
  141.         }
  142.     }
  143. }
  144.  
  145. /* Decode file stream into a byte level code with only
  146.  * repetition encoding remaining.
  147.  */
  148.  
  149. int
  150. getuhuff()
  151.  
  152. {
  153.     int i;
  154.     int bitval;
  155.  
  156.     /* Follow bit stream in tree to a leaf*/
  157.     i = 0;    /* Start at root of tree */
  158.     do
  159.     {
  160.         if(++bpos > 7)
  161.         {
  162.             if((curin = getc(&fin)) == ERROR)
  163.                 return ERROR;
  164.             bpos = 0;
  165.             /* move a level deeper in tree */
  166.             i = dnode[i].children[1 & curin];
  167.         }
  168.         else
  169.         {
  170.             i = dnode[i].children[1 & (curin >>= 1)];
  171.         }
  172.     } while(i >= 0);
  173.  
  174.     /* Decode fake node index to original data value */
  175.     i = -(i + 1);
  176.     /* Decode special endfile token to normal EOF */
  177.     i = (i == SPEOF) ? EOF : i;
  178.     return i;
  179. }
  180.