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 / ENTERPRS / CPM / TERMS / ZMP-SRC.LZH / ZZM2.C < prev   
Text File  |  2000-06-30  |  6KB  |  343 lines

  1.  
  2. /* ZZM.C Part 2 */
  3.  
  4. #include "zmp.h"
  5. #include "zmodem.h"
  6.  
  7. #undef DEBUG
  8.  
  9. #ifdef   AZTEC_C
  10. #include "libc.h"
  11. #else
  12. #include <stdio.h>
  13. #endif
  14.  
  15. extern long updc32();
  16.  
  17. /* Receive a binary style header (type and position) */
  18. zrbhdr(hdr)
  19. char *hdr;
  20. {
  21.     static int c, n;
  22.     static unsigned crc;
  23.  
  24.     if ((c = zdlread()) & ~0377)
  25.         return c;
  26.     Rxtype = c;
  27.     crc = updcrc(c, 0);
  28.  
  29.     for (n=4; --n >= 0; ++hdr) {
  30.         if ((c = zdlread()) & ~0377)
  31.             return c;
  32.         crc = updcrc(c, crc);
  33.         *hdr = c;
  34.     }
  35.     if ((c = zdlread()) & ~0377)
  36.         return c;
  37.     crc = updcrc(c, crc);
  38.     if ((c = zdlread()) & ~0377)
  39.         return c;
  40.     crc = updcrc(c, crc);
  41.     if (crc & 0xFFFF) {
  42.         zperr("Bad Header CRC",TRUE); 
  43.         return NERROR;
  44.     }
  45.     Zmodem = 1;
  46.  
  47. #ifdef DEBUG
  48.     printf("\nReceived BINARY header type %d: ",Rxtype);
  49.     for (n = -4; n < 0; n++)
  50.         prhex( *(hdr + n));
  51.     printf("\n");
  52. #endif
  53.  
  54.     return Rxtype;
  55. }
  56.  
  57. /* Receive a binary style header (type and position) with 32 bit FCS */
  58. zrb32hdr(hdr)
  59. char *hdr;
  60. {
  61.     static int c, n;
  62.     static long crc;
  63.  
  64.     if ((c = zdlread()) & ~0377)
  65.         return c;
  66.     Rxtype = c;
  67.     crc = 0xFFFFFFFFL; 
  68.     crc = updc32(c, crc);
  69. #ifdef DEBUGZ
  70. #endif
  71.  
  72.     for (n=4; --n >= 0; ++hdr) {
  73.         if ((c = zdlread()) & ~0377)
  74.             return c;
  75.         crc = updc32(c, crc);
  76.         *hdr = c;
  77. #ifdef DEBUGZ
  78. #endif
  79.     }
  80.     for (n=4; --n >= 0;) {
  81.         if ((c = zdlread()) & ~0377)
  82.             return c;
  83.         crc = updc32(c, crc);
  84. #ifdef DEBUGZ
  85. #endif
  86.     }
  87.     if (crc != 0xDEBB20E3) {
  88.         zperr("Bad Header CRC",TRUE); 
  89.         return NERROR;
  90.     }
  91.     Zmodem = 1;
  92.  
  93. #ifdef DEBUG
  94.     printf("\nReceived 32-bit FCS BINARY header type %d: ",Rxtype);
  95.     for (n = -4; n < 0; n++)
  96.         prhex( *(hdr + n));
  97.     printf("\n");
  98. #endif
  99.  
  100.     return Rxtype;
  101. }
  102.  
  103.  
  104. /* Receive a hex style header (type and position) */
  105. zrhhdr(hdr)
  106. char *hdr;
  107. {
  108.     static int c;
  109.     static unsigned crc;
  110.     static int n;
  111.  
  112.     if ((c = zgethex()) < 0)
  113.         return c;
  114.     Rxtype = c;
  115.     crc = updcrc(c, 0);
  116.  
  117.     for (n=4; --n >= 0; ++hdr) {
  118.         if ((c = zgethex()) < 0)
  119.             return c;
  120.         crc = updcrc(c, crc);
  121.         *hdr = c;
  122.     }
  123.     if ((c = zgethex()) < 0)
  124.         return c;
  125.     crc = updcrc(c, crc);
  126.     if ((c = zgethex()) < 0)
  127.         return c;
  128.     crc = updcrc(c, crc);
  129.     if (crc & 0xFFFF) {
  130.         zperr("Bad Header CRC",TRUE); 
  131.         return NERROR;
  132.     }
  133.     if (readline(INTRATIME) == '\r')    /* Throw away possible cr/lf */
  134.         readline(INTRATIME);
  135.     Zmodem = 1; 
  136.  
  137. #ifdef DEBUG
  138.     printf("\nReceived HEX header type %d: ",Rxtype);
  139.     for (n = -4; n < 0; n++)
  140.         prhex( *(hdr + n));
  141.     printf("\n");
  142. #endif
  143.  
  144.     return Rxtype;
  145. }
  146.  
  147. /* Send a byte as two hex digits */
  148. zputhex(c)
  149. int c;
  150. {
  151.     static char    digits[]    = "0123456789abcdef";
  152.  
  153.    xmchout(digits[(c&0xF0)>>4]);
  154.    xmchout(digits[(c)&0xF]);
  155. }
  156.  
  157. /*
  158.  * Send character c with ZMODEM escape sequence encoding.
  159.  *  Escape XON, XOFF. Escape CR following @ (Telenet net escape)
  160.  */
  161. zsendline(c)
  162. int c;
  163. {
  164.     static lastsent;
  165.  
  166.     switch (c &= 0377) {
  167.     case ZDLE:
  168.         xmchout(ZDLE);
  169.         xmchout (lastsent = (c ^= 0100));
  170.         break;
  171.     case 015:
  172.     case 0215:
  173.         if (!Zctlesc && (lastsent & 0177) != '@')
  174.             goto sendit;
  175.         /* **** FALL THRU TO **** */
  176.     case 020:
  177.     case 021:
  178.     case 023:
  179.     case 0220:
  180.     case 0221:
  181.     case 0223:
  182.         xmchout(ZDLE);
  183.         c ^= 0100;
  184. sendit:
  185.         xmchout(lastsent = c);
  186.         break;
  187.     default:
  188.         if (Zctlesc && ! (c & 0140)) {
  189.             xmchout(ZDLE);
  190.             c ^= 0100;
  191.         }
  192.         xmchout(lastsent = c);
  193.     }
  194. }
  195.  
  196. /* Decode two lower case hex digits into an 8 bit byte value */
  197. zgethex()
  198. {
  199.     int c;
  200.  
  201.     c = zgeth1();
  202.     return c;
  203. }
  204. zgeth1()
  205. {
  206.     static int c, n;
  207.  
  208.     if ((c = noxrd7()) < 0)
  209.         return c;
  210.     n = c - '0';
  211.     if (n > 9)
  212.         n -= ('a' - ':');
  213.     if (n & ~0xF)
  214.         return NERROR;
  215.     if ((c = noxrd7()) < 0)
  216.         return c;
  217.     c -= '0';
  218.     if (c > 9)
  219.         c -= ('a' - ':');
  220.     if (c & ~0xF)
  221.         return NERROR;
  222.     c += (n<<4);
  223.     return c;
  224. }
  225.  
  226. /*
  227.  * Read a byte, checking for ZMODEM escape encoding
  228.  *  including CAN*5 which represents a quick abort
  229.  */
  230.  
  231. zdlread()
  232. {
  233.     static int c;
  234.  
  235. again:
  236.     switch (c = readline(Rxtimeout)) {
  237.     case ZDLE:
  238.         break;
  239.     case 023:
  240.     case 0223:
  241.     case 021:
  242.     case 0221:
  243.         goto again;
  244.     default:
  245.         if (Zctlesc && !(c & 0140)) {
  246.             goto again;
  247.         }
  248.         return c;
  249.     }
  250. again2:
  251.     if ((c = readline(Rxtimeout)) < 0)
  252.         return c;
  253.     if (c == CAN && (c = readline(Rxtimeout)) < 0)
  254.         return c;
  255.     if (c == CAN && (c = readline(Rxtimeout)) < 0)
  256.         return c;
  257.     if (c == CAN && (c = readline(Rxtimeout)) < 0)
  258.         return c;
  259.     switch (c) {
  260.     case CAN:
  261.         return GOTCAN;
  262.     case ZCRCE:
  263.     case ZCRCG:
  264.     case ZCRCQ:
  265.     case ZCRCW:
  266.         return (c | GOTOR);
  267.     case ZRUB0:
  268.         return 0177;
  269.     case ZRUB1:
  270.         return 0377;
  271.     case 023:
  272.     case 0223:
  273.     case 021:
  274.     case 0221:
  275.         goto again2;
  276.     default:
  277.         if (Zctlesc && ! (c & 0140)) {
  278.             goto again2;
  279.         }
  280.         if ((c & 0140) ==  0100)
  281.             return (c ^ 0100);
  282.         break;
  283.     }
  284.    sprintf(Buf,"Bad escape %x", c);
  285.     zperr(Buf,TRUE);
  286.     return NERROR;
  287. }
  288.  
  289. /*
  290.  * Read a character from the modem line with timeout.
  291.  *  Eat parity, XON and XOFF characters.
  292.  */
  293. noxrd7()
  294. {
  295.     static int c;
  296.  
  297.     for (;;) {
  298.         if ((c = readline(Rxtimeout)) < 0)
  299.             return c;
  300.         switch (c &= 0177) {
  301.         case XON:
  302.         case XOFF:
  303.             continue;
  304.         default:
  305.             if (Zctlesc && !(c & 0140))
  306.                 continue;
  307.         case '\r':
  308.         case '\n':
  309.         case ZDLE:
  310.             return c;
  311.         }
  312.     }
  313. }
  314.  
  315. /* Store long integer pos in Txhdr */
  316. stohdr(pos)
  317. long pos;
  318. {
  319.     Txhdr[ZP0] = pos;
  320.     Txhdr[ZP1] = (pos>>8);
  321.     Txhdr[ZP2] = (pos>>16);
  322.     Txhdr[ZP3] = (pos>>24);
  323. }
  324.  
  325. /* Recover a long integer from a header */
  326. long
  327. rclhdr(hdr)
  328. char *hdr;
  329. {
  330.     static long l;
  331.  
  332.     l = (unsigned)(hdr[ZP3] & 0377);
  333.     l = (l << 8) | (unsigned)(hdr[ZP2] & 0377);
  334.     l = (l << 8) | (unsigned)(hdr[ZP1] & 0377);
  335.     l = (l << 8) | (unsigned)(hdr[ZP0] & 0377);
  336. #ifdef DEBUG
  337.    lreport(FBLOCKS,l);
  338. #endif
  339.     return l;
  340. }
  341.  
  342. /***************************** End of hzm2.c *********************************/
  343.