home *** CD-ROM | disk | FTP | other *** search
/ Oakland CPM Archive / oakcpm.iso / sigm / vol158 / yam10.c < prev    next >
Encoding:
C/C++ Source or Header  |  1984-04-29  |  4.8 KB  |  273 lines

  1. /*
  2. >> yam10.c 12-20-82
  3.  * File unsqueezer module from Richard Greenlaw's USQ
  4.  *  and other vital functions
  5. */
  6. #define EXTERN
  7. #include "yamc86.h"
  8.  
  9. #ifdef XMODEM
  10. #define CICHAR BCICHAR
  11. #define CIREADY BCIREADY
  12. #endif
  13.  
  14. /* *** Stuff for first translation module *** */
  15. #define USDLE 0x90
  16. /* *** Stuff for second translation module *** */
  17. #define SPEOF 256    /* special endfile token */
  18. #define LARGE 30000
  19. int bpos;    /* last bit position read */
  20. int curin;    /* last byte value read */
  21. /* Variables associated with repetition decoding */
  22. int repct;    /*Number of times to return value*/
  23. int value;    /*current byte value or EOF */
  24.  
  25. unsqueeze()
  26. {
  27.     int i, c;
  28.     int numnodes;        /* size of decoding tree */
  29.  
  30.     /* Initialization */
  31.     init_cr();
  32.     init_huff();
  33.  
  34.     /* Process rest of header (SQMAGIC already read) */
  35.     getw(fin);    /* ignore checksum ... */
  36.  
  37.     /* List current and original file names */
  38.     fprintf(stderr, "%s -> ", Tname);
  39.     while(c = getc(fin))
  40.         putc(c, stderr);
  41.     putc('\n', stderr);
  42.  
  43.     numnodes = getw(fin);
  44.     if(numnodes < 0 || numnodes >= NUMVALS) {
  45.         printf(stderr, "%s has invalid decode tree size\n", Tname);
  46.         return ERROR;
  47.     }
  48.  
  49.     /* Initialize for possible empty tree (SPEOF only) */
  50.     Utility.dnode[0].children[0] = -(SPEOF + 1);
  51.     Utility.dnode[0].children[1] = -(SPEOF + 1);
  52.  
  53.     /* Get decoding tree from file */
  54.     for(i = 0; i < numnodes; ++i) {
  55.         Utility.dnode[i].children[0] = getw(fin);
  56.         Utility.dnode[i].children[1] = getw(fin);
  57.     }
  58.  
  59.     while((c = getcr()) != CPMEOF) {
  60.         if( !(c=putcty(c)))
  61.             continue;
  62.         if(c==003 || c==CAN || c==013) {
  63.             return c;
  64.         }
  65.     }
  66.     return CPMEOF;
  67. }
  68.  
  69.  
  70. /* initialize decoding functions */
  71.  
  72. init_cr()
  73. {
  74.     repct = 0;
  75. }
  76.  
  77. init_huff()
  78. {
  79.     bpos = 99;    /* force initial read */
  80. }
  81.  
  82. /* Get bytes with decoding - this decodes repetition,
  83.  * calls getuhuff to decode file stream into byte
  84.  * level code with only repetition encoding.
  85.  *
  86.  * The code is simple passing through of bytes except
  87.  * that USDLE is encoded as USDLE-zero and other values
  88.  * repeated more than twice are encoded as value-USDLE-count.
  89.  */
  90.  
  91. int
  92. getcr()
  93. {
  94.     int c;
  95.  
  96.     if(repct > 0) {
  97.         /* Expanding a repeated char */
  98.         --repct;
  99.         return value;
  100.     } else {
  101.         /* Nothing unusual */
  102.         if((c = getuhuff()) != USDLE) {
  103.             /* It's not the special delimiter */
  104.             value = c;
  105.             if(value == CPMEOF)
  106.                 repct = LARGE;
  107.             return value;
  108.         } else {
  109.             /* Special token */
  110.             if((repct = getuhuff()) == 0)
  111.                 /* USDLE, zero represents USDLE */
  112.                 return USDLE;
  113.             else {
  114.                 /* Begin expanding repetition */
  115.                 repct -= 2;    /* 2nd time */
  116.                 return value;
  117.             }
  118.         }
  119.     }
  120. }
  121.  
  122. /* Decode file stream into a byte level code with only
  123.  * repetition encoding remaining.
  124.  */
  125.  
  126. int
  127. getuhuff()
  128. {
  129.     int i;
  130.  
  131.     /* Follow bit stream in tree to a leaf*/
  132.     i = 0;    /* Start at root of tree */
  133.     do {
  134.         if(++bpos > 7) {
  135.             if((curin = getc(fin)) == ERROR)
  136.                 return ERROR;
  137.             bpos = 0;
  138.             /* move a level deeper in tree */
  139.             i = Utility.dnode[i].children[1 & curin];
  140.         } else
  141.             i = Utility.dnode[i].children[1 & (curin >>= 1)];
  142.     } while(i >= 0);
  143.  
  144.     /* Decode fake node index to original data value */
  145.     i = -(i + 1);
  146.     /* Decode special endfile token to normal EOF */
  147.     i = (i == SPEOF) ? CPMEOF : i;
  148.     return i;
  149. }
  150.  
  151. char putcty(c)
  152. char c;
  153. {
  154. #ifdef RXNONO
  155.     if(index(c, RXNONO))
  156.         return 0;
  157. #endif
  158.     if(UsePutchar)
  159.         putchar(c);
  160.     else {
  161. #ifdef BTTYOUT
  162.         BTTYOUT(c);
  163. #else
  164. #ifdef COREADY
  165.         while (!COREADY)
  166.             ;
  167. #endif
  168.         TTYOUT(c);
  169. #endif
  170.     }
  171.     if(CIREADY) {
  172.         if((c=(CICHAR&KBMASK))==XOFF) {
  173.             if((c=getcty())==003 || c==CAN || c==013)
  174.                 return c;
  175.         } else
  176.             return c;
  177.     }
  178.     return FALSE;
  179. }
  180.  
  181. char getcty()
  182. {
  183.     while (!CIREADY)
  184.         ;
  185.     return (CICHAR&KBMASK);
  186. }
  187.  
  188. listfile(name)
  189. char *name;
  190. {
  191.     int c;
  192.  
  193.     closetx(TRUE);
  194. #ifndef XMODEM
  195.     putcty('\f');        /* line printer form-feed if ^P */
  196. #endif
  197.     if(opentx(name)==ERROR)
  198.         return ERROR;
  199. #ifdef XMODEM
  200.     sendline(022);        /* bracket with ^R and ^T for squelch */
  201. #endif
  202. #ifdef USQ
  203.     if((c=getw(fin))==SQMAGIC)
  204.         c=unsqueeze();
  205.     else if(c != ERROR) {
  206.         putcty(c); putcty(c>>8);    /* pdp-11 sequence */
  207. #else
  208.     else {
  209. #endif
  210.         while((c=getc(fin))!=EOF && c != CPMEOF) {
  211.             if( !(c=putcty(c)))
  212.                 continue;
  213.             if(c==003 || c==CAN || c==013) {
  214.                 break;
  215.             }
  216.         }
  217.     }
  218.     if(c != CPMEOF)
  219.         putchar('\n');
  220.     /* record complete xmsn iff terminated by (CPM)EOF */
  221.     closetx(c != CPMEOF);
  222. #ifdef XMODEM
  223.     sendline(024);    /* squelch in case user downloading */
  224. #endif        
  225.     /* cancel rest of files if ^C or ^X */
  226.     if(c==003 || c==CAN)
  227.         return ERROR;
  228.     else
  229.         return OK;
  230. }
  231.  
  232. /* fill buf with count chars padding with ^Z for CPM */
  233. filbuf(buf, count)
  234. char *buf;
  235. {
  236.     register c, m;
  237.     m=count;
  238.     while((c=getc(fin))!=EOF) {
  239.         *buf++ =c;
  240.         if(--m == 0)
  241.             break;
  242.     }
  243.     if(m==count)
  244.         return 0;
  245.     else
  246.         while(--m>=0)
  247.             *buf++ = 032;
  248.     return count;
  249. }
  250.  
  251.  
  252. cmdeq(s,p)
  253. char *s, *p;
  254. {
  255.     while(*p)
  256.         if(*s++ != *p++)
  257.             return 0;
  258.     return 1;
  259. }
  260.  
  261. /* local tell-it-like-it-is string write function with no kbd check */
  262. lputs(s)
  263. char *s;
  264. {
  265.     while(*s) {
  266. #ifdef COREADY
  267.         while (!COREADY)
  268.             ;
  269. #endif
  270.         TTYOUT(*s++);
  271.     }
  272. }
  273.