home *** CD-ROM | disk | FTP | other *** search
/ ftp.barnyard.co.uk / 2015.02.ftp.barnyard.co.uk.tar / ftp.barnyard.co.uk / cpm / walnut-creek-CDROM / BEEHIVE / COMMS / YAM.ARC / YAM10.C < prev    next >
Text File  |  1990-09-20  |  3KB  |  147 lines

  1. /*
  2. >> yam10.c 11-14-81
  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.     int i, c;
  16.     char cc;
  17.  
  18.     char *p;
  19.     int numnodes;        /* size of decoding tree */
  20.     unsigned linect;    /* count of number of lines previewed */
  21.     char origname[14];    /* Original file name without drive */
  22.  
  23.     /* Initialization */
  24.     linect = 0;
  25.     init_cr();
  26.     init_huff();
  27.  
  28.     /* Process rest of header (RECOGNIZE already read) */
  29.     getw(&fin);    /* ignore checksum ... */
  30.  
  31.     /* Get original file name */
  32.     p = origname;    /* send it to array */
  33.     do {
  34.         *p = getc(&fin);
  35.     } while(*p++ != '\0');
  36.  
  37.     printf("%s -> %s\n", Tname, origname);
  38.  
  39.     numnodes = getw(&fin);
  40.     if(numnodes < 0 || numnodes >= NUMVALS) {
  41.         printf("%s has invalid decode tree size\n", Tname);
  42.         return 1;
  43.     }
  44.  
  45.     /* Initialize for possible empty tree (SPEOF only) */
  46.     dnode[0].children[0] = -(SPEOF + 1);
  47.     dnode[0].children[1] = -(SPEOF + 1);
  48.  
  49.     /* Get decoding tree from file */
  50.     for(i = 0; i < numnodes; ++i) {
  51.         dnode[i].children[0] = getw(&fin);
  52.         dnode[i].children[1] = getw(&fin);
  53.     }
  54.  
  55.     while((c = getcr()) != EOF) {
  56.         if( !(c=putcty(c)))
  57.             continue;
  58.         if(c==003 || c==CAN || c==013) {
  59.             return c;
  60.         }
  61.     }
  62.     return CPMEOF;
  63. }
  64.  
  65.  
  66. /* initialize decoding functions */
  67.  
  68. init_cr()
  69. {
  70.     repct = 0;
  71. }
  72.  
  73. init_huff()
  74. {
  75.     bpos = 99;    /* force initial read */
  76. }
  77.  
  78. /* Get bytes with decoding - this decodes repetition,
  79.  * calls getuhuff to decode file stream into byte
  80.  * level code with only repetition encoding.
  81.  *
  82.  * The code is simple passing through of bytes except
  83.  * that DLE is encoded as DLE-zero and other values
  84.  * repeated more than twice are encoded as value-DLE-count.
  85.  */
  86.  
  87. int
  88. getcr()
  89. {
  90.     int c;
  91.  
  92.     if(repct > 0) {
  93.         /* Expanding a repeated char */
  94.         --repct;
  95.         return value;
  96.     } else {
  97.         /* Nothing unusual */
  98.         if((c = getuhuff()) != DLE) {
  99.             /* It's not the special delimiter */
  100.             value = c;
  101.             if(value == EOF)
  102.                 repct = LARGE;
  103.             return value;
  104.         } else {
  105.             /* Special token */
  106.             if((repct = getuhuff()) == 0)
  107.                 /* DLE, zero represents DLE */
  108.                 return DLE;
  109.             else {
  110.                 /* Begin expanding repetition */
  111.                 repct -= 2;    /* 2nd time */
  112.                 return value;
  113.             }
  114.         }
  115.     }
  116. }
  117.  
  118. /* Decode file stream into a byte level code with only
  119.  * repetition encoding remaining.
  120.  */
  121.  
  122. int
  123. getuhuff()
  124. {
  125.     int i;
  126.     int bitval;
  127.  
  128.     /* Follow bit stream in tree to a leaf*/
  129.     i = 0;    /* Start at root of tree */
  130.     do {
  131.         if(++bpos > 7) {
  132.             if((curin = getc(&fin)) == ERROR)
  133.                 return ERROR;
  134.             bpos = 0;
  135.             /* move a level deeper in tree */
  136.             i = dnode[i].children[1 & curin];
  137.         } else
  138.             i = dnode[i].children[1 & (curin >>= 1)];
  139.     } while(i >= 0);
  140.  
  141.     /* Decode fake node index to original data value */
  142.     i = -(i + 1);
  143.     /* Decode special endfile token to normal EOF */
  144.     i = (i == SPEOF) ? EOF : i;
  145.     return i;
  146. }
  147. =