home *** CD-ROM | disk | FTP | other *** search
/ Frozen Fish 1: Amiga / FrozenFish-Apr94.iso / bbs / alib / d5xx / d534 / term.lha / Term / Libs.LZH / XprQuickB / quickb.c < prev    next >
C/C++ Source or Header  |  1991-07-26  |  28KB  |  1,157 lines

  1. /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
  2. /* |_o_o|\\ Copyright (c) 1990 The Software Distillery.  All Rights Reserved */
  3. /* |. o.| || This program may not be distributed without the permission of   */
  4. /* | .    | || the authors:                  BBS: (919) 382-8265    */
  5. /* | o    | ||   Dave Baker      Alan Beale      Jim Cooper             */
  6. /* |  . |//    Jay Denebeim    Bruce Drake      Gordon Keener          */
  7. /* ======      John Mainwaring Andy Mercier      Jack Rouse             */
  8. /*           John Toebes     Mary Ellen Toebes  Doug Walker  Mike Whitcher */
  9. /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
  10.    
  11. /*
  12.  *  QUICKB.C - Quick B Protocol Support routines
  13.  *
  14.  *    converted to C by Paul M. Resch
  15.  *    modified to function as an Amiga XPR protocol library by
  16.  *        Jack Rouse of the Software Distillery.
  17.  */
  18.  
  19. /*
  20.  * This module implements the B-Protocol Functions.
  21.  *
  22.  * bp_DLE should be invoked whenever a <DLE> is received.
  23.  * bp_ENQ should be called whenever an <ENQ> is received.
  24.  * bp_ESC_I should be called when the sequence <ESC><I> is received.
  25.  *
  26.  * cgetc is an external procedure which returns a character from the
  27.  *    communications port or -1 if no character is received within the time
  28.  *    specified (in microseconds).
  29.  * cputc is an external procedure which sends a character to the communication
  30.  *    port.
  31.  *
  32.  * This source was originally derived from QUICKB.INC, version 121687, written
  33.  * by Russ Ranshaw, CompuServe Incorporated.
  34.  *
  35.  */
  36.  
  37. #include "quickb.h"
  38.  
  39. /* Table of control characters that need to be masked */
  40.  
  41. static    char    mask_table[] = {
  42.                 0, 0, 0, 1, 0, 1, 0, 0,   /* NUL SOH SOB ETX EOT ENQ SYN BEL */
  43.                 0, 0, 0, 0, 0, 0, 0, 0,   /* BS  HT  LF  VT  FF  CR  SO  SI  */
  44.                 1, 1, 0, 1, 0, 1, 0, 0,   /* DLE DC1 DC2 DC3 DC4 NAK ^V  ^W  */
  45.                 0, 0, 0, 0, 0, 0, 0, 0};  /* CAN ^Y  ^Z  ESC ?   ?   ?   ?   */
  46.  
  47. static    char    hex_digit[] = "0123456789ABCDEF";
  48.  
  49. static int SA_Flush(struct XPR_DATA *xd);
  50. static int send_packet(struct XPR_DATA *xd, int size);
  51.  
  52. /*
  53.  * crc
  54.  *
  55.  * Calculates XMODEM-style CRC (uses the CCITT V.41 polynomial but
  56.  * completely backwards from the normal bit ordering).
  57.  */
  58.  
  59.  
  60. static    unsigned short    crc_table[] = {
  61.         0x0000, 0x1021, 0x2042, 0x3063, 0x4084, 0x50A5, 0x60C6, 0x70E7,
  62.         0x8108, 0x9129, 0xA14A, 0xB16B, 0xC18C, 0xD1AD, 0xE1CE, 0xF1EF,
  63.         0x1231, 0x0210, 0x3273, 0x2252, 0x52B5, 0x4294, 0x72F7, 0x62D6,
  64.         0x9339, 0x8318, 0xB37B, 0xA35A, 0xD3BD, 0xC39C, 0xF3FF, 0xE3DE,
  65.         0x2462, 0x3443, 0x0420, 0x1401, 0x64E6, 0x74C7, 0x44A4, 0x5485,
  66.         0xA56A, 0xB54B, 0x8528, 0x9509, 0xE5EE, 0xF5CF, 0xC5AC, 0xD58D,
  67.         0x3653, 0x2672, 0x1611, 0x0630, 0x76D7, 0x66F6, 0x5695, 0x46B4,
  68.         0xB75B, 0xA77A, 0x9719, 0x8738, 0xF7DF, 0xE7FE, 0xD79D, 0xC7BC,
  69.         0x48C4, 0x58E5, 0x6886, 0x78A7, 0x0840, 0x1861, 0x2802, 0x3823,
  70.         0xC9CC, 0xD9ED, 0xE98E, 0xF9AF, 0x8948, 0x9969, 0xA90A, 0xB92B,
  71.         0x5AF5, 0x4AD4, 0x7AB7, 0x6A96, 0x1A71, 0x0A50, 0x3A33, 0x2A12,
  72.         0xDBFD, 0xCBDC, 0xFBBF, 0xEB9E, 0x9B79, 0x8B58, 0xBB3B, 0xAB1A,
  73.         0x6CA6, 0x7C87, 0x4CE4, 0x5CC5, 0x2C22, 0x3C03, 0x0C60, 0x1C41,
  74.         0xEDAE, 0xFD8F, 0xCDEC, 0xDDCD, 0xAD2A, 0xBD0B, 0x8D68, 0x9D49,
  75.         0x7E97, 0x6EB6, 0x5ED5, 0x4EF4, 0x3E13, 0x2E32, 0x1E51, 0x0E70,
  76.         0xFF9F, 0xEFBE, 0xDFDD, 0xCFFC, 0xBF1B, 0xAF3A, 0x9F59, 0x8F78,
  77.         0x9188, 0x81A9, 0xB1CA, 0xA1EB, 0xD10C, 0xC12D, 0xF14E, 0xE16F,
  78.         0x1080, 0x00A1, 0x30C2, 0x20E3, 0x5004, 0x4025, 0x7046, 0x6067,
  79.         0x83B9, 0x9398, 0xA3FB, 0xB3DA, 0xC33D, 0xD31C, 0xE37F, 0xF35E,
  80.         0x02B1, 0x1290, 0x22F3, 0x32D2, 0x4235, 0x5214, 0x6277, 0x7256,
  81.         0xB5EA, 0xA5CB, 0x95A8, 0x8589, 0xF56E, 0xE54F, 0xD52C, 0xC50D,
  82.         0x34E2, 0x24C3, 0x14A0, 0x0481, 0x7466, 0x6447, 0x5424, 0x4405,
  83.         0xA7DB, 0xB7FA, 0x8799, 0x97B8, 0xE75F, 0xF77E, 0xC71D, 0xD73C,
  84.         0x26D3, 0x36F2, 0x0691, 0x16B0, 0x6657, 0x7676, 0x4615, 0x5634,
  85.         0xD94C, 0xC96D, 0xF90E, 0xE92F, 0x99C8, 0x89E9, 0xB98A, 0xA9AB,
  86.         0x5844, 0x4865, 0x7806, 0x6827, 0x18C0, 0x08E1, 0x3882, 0x28A3,
  87.         0xCB7D, 0xDB5C, 0xEB3F, 0xFB1E, 0x8BF9, 0x9BD8, 0xABBB, 0xBB9A,
  88.         0x4A75, 0x5A54, 0x6A37, 0x7A16, 0x0AF1, 0x1AD0, 0x2AB3, 0x3A92,
  89.         0xFD2E, 0xED0F, 0xDD6C, 0xCD4D, 0xBDAA, 0xAD8B, 0x9DE8, 0x8DC9,
  90.         0x7C26, 0x6C07, 0x5C64, 0x4C45, 0x3CA2, 0x2C83, 0x1CE0, 0x0CC1,
  91.         0xEF1F, 0xFF3E, 0xCF5D, 0xDF7C, 0xAF9B, 0xBFBA, 0x8FD9, 0x9FF8,
  92.         0x6E17, 0x7E36, 0x4E55, 0x5E74, 0x2E93, 0x3EB2, 0x0ED1, 0x1EF0
  93.     };
  94.  
  95.  
  96. /*
  97.  * Init_CRC initializes for XMODEM style CRC calculation by setting
  98.  * crc_16 to value.  Typically value is 0 for XMODEM and -1 for
  99.  * B+ Protocl.  It returns the initial value.
  100.  */
  101. static    int    init_CRC (struct XPR_DATA *xd, int value)
  102. {
  103.     return (int)( xd->crc_16 = value );
  104. }
  105.  
  106. /*
  107.   Upd_CRC updates crc_16 and returns the updated value. */
  108.  
  109. static    unsigned int    upd_CRC (struct XPR_DATA *xd, unsigned int value)
  110. {
  111.     xd->crc_16 = crc_table [((xd->crc_16 >> 8) ^ (value)) & 0xff] ^    (xd->crc_16 << 8);
  112.     return( (unsigned int)xd->crc_16 );
  113. }
  114.  
  115. /* Update the checksum/CRC */
  116.  
  117. static    void    do_checksum(struct XPR_DATA *xd, int c)
  118. {
  119.     if (xd->Quick_B && xd->Use_CRC)
  120.         xd->checksum = upd_CRC (xd, c);
  121.     else
  122.     {
  123.         xd->checksum = xd->checksum << 1;
  124.  
  125.         if (xd->checksum > 255)
  126.             xd->checksum = (xd->checksum & 0xFF) + 1;
  127.  
  128.         xd->checksum = xd->checksum + c;
  129.  
  130.         if (xd->checksum > 255)
  131.             xd->checksum = (xd->checksum & 0xFF) + 1;
  132.     }
  133. }
  134.  
  135. static    void    send_failure(struct XPR_DATA *xd, int code )
  136. {
  137.     register PACKET    *p;
  138.  
  139.     xd->ack_SA = 0;
  140.     xd->fill_SA = 0;
  141.     xd->SA_Waiting = 0;
  142.     xd->Aborting = TRUE;        /* inform get_ACK we're aborting */
  143.  
  144.     p = &xd->SA_Buf [0];
  145.     p->buf[0] = 'F';
  146.     p->buf[1] = code;
  147.  
  148.     if ( send_packet (xd, 1))
  149.         SA_Flush(xd);   /* Gotta wait for the host to ACK it */
  150. }
  151.  
  152. /*
  153.  * bp_ENQ is called when the terminal emulator receives the character <ENQ>
  154.  * from the host.  Its purpose is to initialize for B Protocol and tell the
  155.  * host that we support Quick B.
  156.  */
  157.  
  158. void    bp_ENQ(struct XPR_DATA *xd)
  159. {
  160.     xd->seq_num = 0;
  161.     xd->buffer_size = 511;               /* Set up defaults */
  162.     xd->Quick_B     = FALSE;             /* Not Quick B Protocol */
  163.     xd->Use_CRC     = FALSE;             /* Not CRC_16      */
  164.     xd->SA_Enabled  = FALSE;             /* No Send-Ahead by us */
  165.     xd->SA_Max      = 1;                 /* = single packet sent */
  166.  
  167.     cputc (xd, DLE);
  168.     cputc (xd, '+');
  169.  
  170.     cputc (xd, DLE);
  171.     cputc (xd, '0');
  172. }
  173.  
  174. /*
  175.  * bp_ESC_I is called when <ESC><I> is received by the terminal emulator.
  176.  * Note that Quick B allows +XX to be added to the end of the response, where
  177.  * XX is the two hex digits of the standard B Protocol checksum of the
  178.  * preceeding characters in the response.
  179.  */
  180. static    char    esc_I_response[] = "#VCO,PB,DT,+";
  181.  
  182. void    bp_ESC_I(struct XPR_DATA *xd)
  183. {
  184.     int    save_Use_CRC;
  185.     register int    i;
  186.  
  187.     save_Use_CRC = xd->Use_CRC;
  188.     xd->Use_CRC = FALSE;
  189.     xd->checksum = 0;
  190.  
  191.     i = 0;
  192.     while( esc_I_response[i] )
  193.     {
  194.         cputc (xd, esc_I_response [i]);
  195.         do_checksum (xd, esc_I_response [i]);
  196.         i++;
  197.     }
  198.  
  199. /* Append two hex digits of checksum to response */
  200.  
  201.     cputc (xd,  hex_digit[ (xd->checksum >> 4) & 0x0F ]);
  202.     cputc (xd,  hex_digit[ xd->checksum & 0x0F ] );
  203.     cputc (xd, CR);
  204.  
  205.     xd->Use_CRC = save_Use_CRC;
  206. }
  207.  
  208.  
  209. static    void    send_masked_byte(struct XPR_DATA *xd, int c)
  210. {
  211.     c = c & 0xFF;
  212.  
  213.     if (c < 32)
  214.     {
  215.         if (mask_table [c] != 0)
  216.         {
  217.             cputc (xd, DLE);
  218.             cputc (xd, c + '@');
  219.         }
  220.         else
  221.             cputc (xd, c);
  222.     }
  223.     else
  224.         cputc (xd, c);
  225.  
  226.     xd->s_counter = (xd->s_counter + 1) % 512;
  227. }
  228.  
  229. static    void    send_ack(struct XPR_DATA *xd)
  230. {
  231.     cputc(xd, DLE);
  232.     cputc(xd, xd->seq_num + '0');
  233. }
  234.  
  235. static    void    send_nak(struct XPR_DATA *xd)
  236. {
  237.     cputc(xd, NAK);
  238. }
  239.  
  240.  
  241. static    void    send_enq(struct XPR_DATA *xd)
  242. {
  243.     cputc(xd, ENQ);
  244. }
  245.  
  246. static    int    read_byte(struct XPR_DATA *xd)
  247. {
  248.     xd->timed_out = FALSE;
  249.  
  250.     xd->cchar = cgetc(xd, 10000000L);
  251.  
  252.     if (xd->cchar < 0 )
  253.         return( FALSE );
  254.  
  255.     xd->r_counter = (xd->r_counter + 1) % 512;
  256.     return( TRUE );
  257. }
  258.  
  259.  
  260. static    int    read_masked_byte(struct XPR_DATA *xd)
  261. {
  262.     xd->masked = FALSE;
  263.  
  264.     if (read_byte(xd) == FALSE)
  265.         return( FALSE );
  266.  
  267.     if (xd->cchar == DLE)
  268.     {
  269.         if (read_byte(xd) == FALSE)
  270.             return( FALSE );
  271.         xd->cchar &= 0x1F;
  272.         xd->masked = TRUE;
  273.     }
  274.  
  275.     return( TRUE );
  276. }
  277.  
  278. /* Increment Sequence Number */
  279.  
  280. static    int    incr_seq (int value)
  281. {
  282.     return( value == 9 ? 0 : value + 1 );
  283. }
  284.  
  285.  
  286. static    int    read_packet (struct XPR_DATA *xd,
  287.     int lead_in_seen, int from_send_packet)
  288. /*   Lead_in_Seen is TRUE if the <DLE><B> has been seen already.  */
  289.  
  290. /*   from_send_packet is TRUE if called from Send_Packet          */
  291. /*        (causes exit on first error detected)                   */
  292.  
  293. /*   Returns True if packet is available from host. */
  294. {
  295.     int    State, next_seq, block_num, errors, new_cks;
  296.     int    i;
  297.  
  298.     xd->packet_received = FALSE;
  299.     for(i=0; i<xd->buffer_size; i++ )
  300.         xd->r_buffer[i] = 0;
  301.     next_seq = (xd->seq_num +  1) % 10;
  302.     errors = 0;
  303.  
  304.     if (lead_in_seen)        /* Start off on the correct foot */
  305.         State = R_GET_SEQ;
  306.     else
  307.         State = R_GET_DLE;
  308.  
  309.     while (TRUE)
  310.     {
  311.         switch  (State) {
  312.         case R_GET_DLE :
  313.             (*xd->xpr_chkmisc)();
  314.             if (!xd->Aborting && (*xd->xpr_chkabort)())
  315.             {
  316.                 send_failure (xd, 'A');
  317.                 return( FALSE );
  318.             }
  319.  
  320.             if (!read_byte(xd))
  321.                         State = R_TIMED_OUT;
  322.             else if ((xd->cchar & 0x7F) == DLE)
  323.                         State = R_GET_B;
  324.             else if ((xd->cchar & 0x7F) == ENQ)
  325.                     State = R_SEND_ACK;
  326.             break;
  327.  
  328.         case R_GET_B :
  329.             if (!read_byte(xd))
  330.                 State = R_TIMED_OUT;
  331.             else if ((xd->cchar & 0x7F) == 'B')
  332.                 State = R_GET_SEQ;
  333.             else if (xd->cchar == ENQ)
  334.                 State = R_SEND_ACK;
  335.             else
  336.                 State = R_GET_DLE;
  337.             break;
  338.  
  339.         case R_GET_SEQ :
  340.             if (!read_byte(xd))
  341.                 State = R_TIMED_OUT;
  342.             else if (xd->cchar == ENQ)
  343.                 State = R_SEND_ACK;
  344.             else
  345.             {
  346.                 if (xd->Quick_B && xd->Use_CRC)
  347.                     xd->checksum = init_CRC (xd, -1);
  348.                 else xd->checksum = 0;
  349.  
  350.                 block_num = xd->cchar - '0';
  351.  
  352.                 do_checksum(xd, xd->cchar);
  353.  
  354.                 i = 0;
  355.                 State = R_GET_DATA;
  356.             }
  357.             break;
  358.  
  359.         case R_GET_DATA :
  360.             xd->r_counter = 0;
  361.             if (!read_masked_byte(xd))
  362.                 State = R_TIMED_OUT;
  363.             else if ((xd->cchar == ETX) && !xd->masked)
  364.             {
  365.                 do_checksum(xd, ETX);
  366.                 State = R_GET_CHECKSUM;
  367.             }
  368.             else if (i >= MAX_BUF_SIZE)
  369.                 State = R_TIMED_OUT;
  370.             else
  371.             {
  372.                 xd->r_buffer[i] = xd->cchar;
  373.                 i = i + 1;
  374.                 do_checksum(xd, xd->cchar);
  375.             }
  376.             break;
  377.  
  378.         case R_GET_CHECKSUM :
  379.             if (!read_masked_byte(xd))
  380.                 State = R_TIMED_OUT;
  381.             else
  382.             {
  383.                 if (xd->Quick_B && xd->Use_CRC)
  384.                 {
  385.                     xd->checksum = upd_CRC (xd, xd->cchar);
  386.  
  387.                     if (!read_masked_byte(xd))
  388.                         new_cks = xd->checksum ^ 0xFF;
  389.                     else
  390.                     {
  391.                         xd->checksum = upd_CRC (xd, xd->cchar);
  392.                         new_cks = 0;
  393.                     }
  394.                 }
  395.                 else new_cks = xd->cchar;
  396.  
  397.                 if (new_cks != xd->checksum)
  398.                     State = R_TIMED_OUT;
  399.                 else if (xd->r_buffer[0] == 'F') /* Watch for Failure Packet */
  400.                     State = R_SUCCESS;       /* which is accepted regardless */
  401.                 else if (block_num == xd->seq_num)    /* Watch for duplicate block */
  402.                     State = R_SEND_ACK;
  403.                 else if (block_num != next_seq)
  404.                     State = R_TIMED_OUT;        /* Bad sequence number */
  405.                 else
  406.                     State = R_SUCCESS;
  407.             }
  408.             break;
  409.  
  410.         case R_TIMED_OUT :
  411.             xd->xpru.xpru_updatemask = XPRU_TIMEOUTS;
  412.             ++xd->xpru.xpru_timeouts;
  413.             (*xd->xpr_update)(&xd->xpru);
  414.  
  415.             if ((++errors > MAX_ERRORS) || (from_send_packet))
  416.                 return( FALSE );
  417.  
  418.             send_nak(xd);
  419.  
  420.             if (from_send_packet)
  421.                 return( FALSE );
  422.  
  423.             State = R_GET_DLE;
  424.             break;
  425.  
  426.         case R_SEND_ACK :
  427.             send_ack(xd);
  428.             State = R_GET_DLE;        /* wait for the next block */
  429.             break;
  430.  
  431.         case R_SUCCESS :
  432.             xd->seq_num = block_num;
  433.             xd->r_size = i;
  434.             xd->packet_received = TRUE;
  435. #if 0
  436.             xd->xpru.xpru_updatemask = XPRU_PACKETTYPE;
  437.             xd->xpru.xpru_packettype = xd->r_buffer[0];
  438.             (*xd->xpr_update)(&xd->xpru);
  439. #endif
  440.             return( TRUE );
  441.             break;
  442.         }
  443.     }
  444. }
  445.  
  446. static    void    send_data (struct XPR_DATA *xd, int Buffer_Number)
  447. {
  448.     int    i;
  449.     register PACKET    *p;
  450.  
  451.     xd->s_counter = 0;
  452.     p = &xd->SA_Buf [Buffer_Number];
  453.     if (xd->Quick_B && xd->Use_CRC)
  454.         xd->checksum = init_CRC (xd, -1);
  455.     else
  456.         xd->checksum = 0;
  457.  
  458.     cputc(xd, DLE);
  459.     cputc(xd, 'B');
  460.  
  461.     cputc(xd, p->seq + '0');
  462.         do_checksum(xd, p->seq + '0');
  463.  
  464.     for (i = 0; i<=p->num; i++ )
  465.     {
  466.         send_masked_byte(xd, p->buf[i]);
  467.         do_checksum(xd, p->buf[i]);
  468.     }
  469.  
  470.     cputc(xd, ETX);
  471.     do_checksum (xd, ETX);
  472.  
  473.     if (xd->Quick_B && xd->Use_CRC)
  474.         send_masked_byte (xd, xd->checksum >> 8);
  475.  
  476.     send_masked_byte(xd, xd->checksum);
  477. }
  478.  
  479. static    int    incr_SA (int old_value)
  480. {
  481.     return( old_value == MAX_SA ? 0 : old_value + 1 );
  482. }
  483.  
  484. /*
  485.  * ReSync is called to restablish syncronism with the remote.  This is
  486.  * accomplished by sending <ENQ><ENQ> and waiting for the sequence
  487.  * <DLE><d><DLE><d> to be received, ignoring everything else.
  488.  *
  489.  * Return is -1 on time out, else the digit <d>.
  490.  */
  491. #define    GET_FIRST_DLE        1
  492. #define    GET_FIRST_DIGIT        2
  493. #define    GET_SECOND_DLE        3
  494. #define    GET_SECOND_DIGIT    4
  495.  
  496. static    int    ReSync(struct XPR_DATA *xd)
  497. {
  498.     int    State, Digit_1;
  499.  
  500.     cputc (xd, ENQ);     /* Send <ENQ><ENQ> */
  501.     cputc (xd, ENQ);
  502.     State = GET_FIRST_DLE;
  503.  
  504.     while(1)
  505.     {
  506.         switch (State) {
  507.         case GET_FIRST_DLE :
  508.             if( !read_byte(xd) )
  509.                 return( -1 );
  510.             if( xd->cchar == DLE )
  511.                 State = GET_FIRST_DIGIT;
  512.             break;
  513.         case GET_FIRST_DIGIT :
  514.             if( !read_byte(xd) )
  515.                 return( -1 );
  516.             if( (xd->cchar >= '0') && (xd->cchar <= '9') )
  517.             {
  518.                 Digit_1 = xd->cchar;
  519.                 State = GET_SECOND_DLE;
  520.             }
  521.             break;
  522.         case GET_SECOND_DLE :
  523.             if( !read_byte(xd) )
  524.                 return( -1 );
  525.             if( xd->cchar == DLE )
  526.                 State = GET_SECOND_DIGIT;
  527.             break;
  528.         case GET_SECOND_DIGIT :
  529.             if( !read_byte(xd) )
  530.                 return( -1 );
  531.             if( (xd->cchar >= '0') && (xd->cchar <= '9') )
  532.             {
  533.                 if( Digit_1 == xd->cchar )
  534.                     return( xd->cchar );
  535.                 else
  536.                 {
  537.                     Digit_1 = xd->cchar;
  538.                     State = GET_SECOND_DLE;
  539.                 }
  540.             }
  541.             else
  542.                 State = GET_SECOND_DLE;
  543.             break;
  544.         }
  545.     }
  546. }
  547.  
  548. /*
  549.  * get_ACK is called to wait until the SA_Buf indicated by ack_SA
  550.  * has been ACKed by the host.
  551.  */
  552. static    int    get_ACK(struct XPR_DATA *xd)
  553. {
  554.     int    State, errors, block_num, i;
  555.     int    Sent_ENQ;
  556.     int    SA_Index;
  557.  
  558.     xd->packet_received = FALSE;
  559.     errors = 0;
  560.     Sent_ENQ = FALSE;
  561.     State = S_GET_DLE;
  562.  
  563.     while( TRUE )
  564.     {
  565.         switch (State) {
  566.         case S_GET_DLE :
  567.             (*xd->xpr_chkmisc)();
  568.             if (!xd->Aborting && (*xd->xpr_chkabort)())
  569.             {
  570.                 send_failure (xd, 'A');
  571.                 return( FALSE );
  572.             }
  573.  
  574.             if (!read_byte(xd))
  575.                 State = S_TIMED_OUT;
  576.             else if (xd->cchar == DLE)
  577.                 State = S_GET_NUM;
  578.             else if (xd->cchar == NAK)
  579.             {
  580.                 xd->xpru.xpru_updatemask = XPRU_ERRORS;
  581.                 ++xd->xpru.xpru_errors;
  582.                 (*xd->xpr_update)(&xd->xpru);
  583.                 if (++errors > MAX_ERRORS)
  584.                     return( FALSE );
  585.                 State = S_SEND_ENQ;
  586.             }
  587.             else if (xd->cchar == ETX)
  588.                 State = S_SEND_NAK;
  589.             break;
  590.  
  591.         case S_GET_NUM :
  592.             if (!read_byte(xd))
  593.                 State = S_TIMED_OUT;
  594.             else if ((xd->cchar >= '0') && (xd->cchar <= '9'))
  595.                 State = S_HAVE_ACK;    /* Received ACK */
  596.             else if (xd->cchar == 'B')
  597.                 State = S_GET_PACKET; /* Try to get packet */
  598.             else if (xd->cchar == NAK)
  599.             {
  600.                 xd->xpru.xpru_updatemask = XPRU_ERRORS;
  601.                 ++xd->xpru.xpru_errors;
  602.                 (*xd->xpr_update)(&xd->xpru);
  603.                 if (++errors > MAX_ERRORS)
  604.                     return( FALSE );
  605.                 State = S_SEND_ENQ;
  606.             }
  607.             else
  608.                 State = S_TIMED_OUT;
  609.             break;
  610.  
  611.         case S_GET_PACKET :
  612.             if (read_packet (xd, TRUE, TRUE))
  613.             {
  614.                 if (xd->r_buffer [0] == 'F')
  615.                 {
  616.                     send_ack(xd);
  617.                     return( FALSE );
  618.                 }
  619.                 else
  620.                     return( TRUE );
  621.             }
  622.  
  623.             State = S_TIMED_OUT; /* On a bad receive, try again */
  624.             break;
  625.         case S_HAVE_ACK:
  626.             block_num = xd->cchar - '0';
  627.             if (xd->SA_Buf [xd->ack_SA].seq == block_num)
  628.             {    /* This is the one we're waiting for */
  629.                 xd->ack_SA = incr_SA(xd->ack_SA);
  630.                 xd->SA_Waiting--;
  631.                 return( TRUE );
  632.             }
  633.             else if (xd->SA_Buf [incr_SA (xd->ack_SA)].seq == block_num &&
  634.                 (xd->SA_Waiting == 2) )
  635.             {    /* Must have missed an ACK */
  636.                 xd->ack_SA = incr_SA (xd->ack_SA);
  637.                 xd->ack_SA = incr_SA (xd->ack_SA);
  638.                 xd->SA_Waiting -= 2;
  639.                 return( TRUE );
  640.             }
  641.             else if (xd->SA_Buf [xd->ack_SA].seq == incr_seq (block_num))
  642.             {
  643.                 if( Sent_ENQ )
  644.                     State = S_SEND_DATA;
  645.                 else
  646.                     State = S_GET_DLE;
  647.             }
  648.             else
  649.                 State = xd->Aborting ? S_GET_DLE : S_TIMED_OUT;
  650.             Sent_ENQ = FALSE;
  651.             break;
  652.         case S_TIMED_OUT :
  653.             xd->xpru.xpru_updatemask = XPRU_TIMEOUTS;
  654.             ++xd->xpru.xpru_timeouts;
  655.             (*xd->xpr_update)(&xd->xpru);
  656.  
  657.             if (++errors > MAX_ERRORS)
  658.                 return( FALSE );
  659.             else
  660.             {
  661.                 if( xd->Aborting && (errors > 3) )
  662.                     return( FALSE );
  663.             }
  664.             State = S_SEND_ENQ;
  665.             break;
  666.  
  667.         case S_SEND_NAK :
  668.             xd->xpru.xpru_updatemask = XPRU_ERRORS;
  669.             ++xd->xpru.xpru_errors;
  670.             (*xd->xpr_update)(&xd->xpru);
  671.  
  672.             if (++errors > MAX_ERRORS)
  673.                 return( FALSE );
  674.  
  675.             send_nak(xd);
  676.  
  677.             State = S_GET_DLE;
  678.             break;
  679.  
  680.         case S_SEND_ENQ :
  681.             xd->xpru.xpru_updatemask = XPRU_ERRORS;
  682.             ++xd->xpru.xpru_errors;
  683.             (*xd->xpr_update)(&xd->xpru);
  684.  
  685.             if (++errors > MAX_ERRORS)
  686.                 return( FALSE );
  687.             xd->cchar = ReSync(xd);
  688.             if( xd->cchar == -1 )
  689.                 State = S_SEND_ENQ;
  690.             else
  691.                 State = S_HAVE_ACK;
  692.             Sent_ENQ = TRUE;
  693.             break;
  694.  
  695.         case S_SEND_DATA :
  696.             SA_Index = xd->ack_SA;
  697.  
  698.             for (i = 1; i<=xd->SA_Waiting; i++ )
  699.             {
  700.                 send_data (xd, SA_Index);
  701.                 SA_Index = incr_SA (SA_Index);
  702.             }
  703.  
  704.             State = S_GET_DLE;
  705.             Sent_ENQ = FALSE;
  706.             break;
  707.         }
  708.     }
  709. } /* get_ACK */
  710.  
  711. static    int    send_packet (struct XPR_DATA *xd, int size)
  712. {
  713.     (*xd->xpr_chkmisc)();
  714.  
  715.     if (xd->SA_Waiting == xd->SA_Max)
  716.         if (!get_ACK(xd))
  717.             return( FALSE );
  718.  
  719.     xd->seq_num = incr_seq (xd->seq_num);
  720.     xd->SA_Buf [xd->fill_SA].seq = xd->seq_num;
  721.     xd->SA_Buf [xd->fill_SA].num = size;
  722.     send_data (xd, xd->fill_SA);
  723.     xd->fill_SA = incr_SA (xd->fill_SA);
  724.     xd->SA_Waiting = xd->SA_Waiting + 1;
  725.     return( TRUE );
  726. }
  727. /*
  728.  * SA_Flush is called after sending the last packet to get host's
  729.  * ACKs on outstanding packets.
  730.  */
  731. static    int    SA_Flush(struct XPR_DATA *xd)
  732. {
  733.     while( xd->SA_Waiting > 0 )
  734.         if (!get_ACK(xd))
  735.             return( FALSE );
  736.     return( TRUE );
  737. }
  738.  
  739. /* Send_File is called to send a file to the host */
  740. static    int    send_file(struct XPR_DATA *xd, char *name)
  741. {
  742.     long    fd;
  743.     int    n;
  744.     register PACKET    *p;
  745.  
  746.     xd->xpru.xpru_updatemask = XPRU_FILENAME;
  747.     xd->xpru.xpru_filename = name;
  748.     xd->xpru.xpru_filesize = (*xd->xpr_finfo)(name, 1L);
  749.     if (xd->xpru.xpru_filesize != 0) /* inform the user of the size */
  750.         xd->xpru.xpru_updatemask |= XPRU_FILESIZE;
  751.     (*xd->xpr_update)(&xd->xpru);
  752.  
  753.     fd = bfopen(xd, name, "r");
  754.     xd->xpru.xpru_bytes = 0L;
  755.  
  756.     if (fd == 0)
  757.       {
  758.         errormsg(xd, "** Cannot find that file **");
  759.         send_failure(xd, 'E');
  760.         return( FALSE );
  761.     }
  762.  
  763.     do
  764.     {
  765.         p = &xd->SA_Buf [xd->fill_SA];
  766.         p->buf[0] = 'N';
  767.         n = xbfread(xd, &p->buf[1], (long)xd->buffer_size, fd);
  768.  
  769.         if (n > 0)
  770.         {
  771.             if (send_packet (xd, n) == FALSE)
  772.             {
  773.                 bfclose(xd, fd);
  774.                 return( FALSE );
  775.             }
  776.             xd->xpru.xpru_updatemask = XPRU_BYTES;
  777.             xd->xpru.xpru_bytes += n;
  778.             (*xd->xpr_update)(&xd->xpru);
  779.         }
  780.     } while( n == xd->buffer_size );
  781.  
  782.     bfclose(xd, fd);
  783.  
  784.     if (n < 0)
  785.     {    /* dead code?  Fread does not distinguish EOF and error */
  786.         send_failure (xd, 'E');
  787.  
  788.         errormsg(xd, "** Read failure...aborting **");
  789.         return(FALSE);
  790.     }
  791.  
  792. /* Inform host that the file was sent */
  793.     p = &xd->SA_Buf [xd->fill_SA];
  794.     p->buf[0] = 'T';
  795.     p->buf[1] = 'C';
  796.  
  797.     if (send_packet(xd, 2) == FALSE)
  798.         return( FALSE );
  799.     else
  800.     {
  801.         xd->xpru.xpru_updatemask = XPRU_MSG;
  802.         xd->xpru.xpru_msg = "Waiting for host...";
  803.         (*xd->xpr_update)(&xd->xpru);
  804.         if (!SA_Flush(xd))
  805.             return( FALSE );
  806.         /* delete the file if requested */
  807.         if (xd->optDelt == 'Y' && xd->xpr_unlink)
  808.             (*xd->xpr_unlink)(name);
  809.         return( TRUE );
  810.     }
  811. }
  812.  
  813. /*
  814.  * do_transport_parameters is called when a Packet type of + is received.
  815.  * It sends a packet of our local Quick B parameters and sets the Our_xx
  816.  * parameters to the minimum of the sender's and our own parameters.
  817.  */
  818. static void do_transport_parameters(struct XPR_DATA *xd)
  819. {
  820.     register PACKET    *p;
  821.  
  822.     xd->His_WS = xd->r_buffer [1];     /* Pick out Sender's parameters */
  823.     xd->His_WR = xd->r_buffer [2];
  824.     xd->His_BS = xd->r_buffer [3];
  825.     xd->His_CM = xd->r_buffer [4];
  826.  
  827.     p = &xd->SA_Buf [xd->fill_SA];
  828.     p->buf [0] = '+';  /* Prepare to return our own parameters */
  829.     p->buf [1] = DEF_WS;
  830.     p->buf [2] = DEF_WR;
  831.     p->buf [3] = DEF_BS;
  832.     p->buf [4] = DEF_CM;
  833.     p->buf [5] = DEF_DQ;
  834.  
  835.     if (!send_packet (xd, 5))
  836.         return;
  837.  
  838.     if (SA_Flush(xd))                 /* Wait for host's ACK on our packet */
  839.     {
  840. /* Take minimal subset of Transport Params. */
  841. /* If he can send ahead, we can receive it. */
  842.         xd->Our_WR = min(xd->His_WS, DEF_WR);
  843.  
  844. /* If he can receive send ahead, we can send it. */
  845.         xd->Our_WS = min(xd->His_WR, DEF_WS);
  846.  
  847.         xd->Our_BS = min(xd->His_BS, DEF_BS);
  848.         xd->Our_CM = min(xd->His_CM, DEF_CM);
  849.  
  850.         if (xd->Our_BS == 0)
  851.             xd->Our_BS = 4;    /* Default */
  852.  
  853.         xd->buffer_size = xd->Our_BS * 128;
  854.  
  855.         xd->Quick_B = TRUE;
  856.  
  857.         if (xd->Our_CM == 1)
  858.             xd->Use_CRC = TRUE;
  859.  
  860.         if (xd->Our_WS != 0)
  861.         {
  862.             xd->SA_Enabled = TRUE;
  863.             xd->SA_Max     = MAX_SA;
  864.         }
  865.     }
  866. }
  867.  
  868. /*
  869.   do_application_parameters is called when a ? packet is received.
  870.   This version ignores the host's packet and returns a ? packet
  871.   saying that normal B Protocol File Transfer is supported.
  872.   (Well, actually it says that no extended application packets are
  873.    supported.  The T packet is assumed to be standard.) */
  874.  
  875. static    void    do_application_parameters(struct XPR_DATA *xd)
  876. {
  877.     register PACKET    *p;
  878.  
  879.     p = &xd->SA_Buf [xd->fill_SA];
  880.     p->buf[0] = '?';     /* Build the ? packet */
  881.     p->buf[1] = 1;             /* The T packet flag  */
  882.  
  883.     if (send_packet (xd, 1))            /* Send the packet    */
  884.         SA_Flush(xd);
  885. }
  886.  
  887. /* Receive_File is called to receive a file from the host */
  888. static    int    receive_file (register struct XPR_DATA *xd, char *name)
  889. {
  890.     long    fd;
  891.     long    bytes;
  892.  
  893.     if (xd->optOwrt == 'N')
  894.     {    /* avoid overwriting existing files */
  895.         char *end = name + strlen(name);
  896.         int dupno = 0;
  897.         while ((fd = (*xd->xpr_fopen)(name, "r")) != 0)
  898.         {
  899.             (*xd->xpr_fclose)(fd);
  900.             if (++dupno == 1)
  901.             {
  902.                 strcpy(end, ".dup");
  903.                 end += 4;
  904.             }
  905.             else if (dupno < 100)
  906.                 stci_d(end, dupno);    /* ".dupN" suffix */
  907.             else
  908.                 break;    /* give up, overwrite .dup99 */
  909.         }
  910.     }
  911.     else if (xd->optOwrt == 'S' && (fd = (*xd->xpr_fopen)(name, "r")) != 0)
  912.     {
  913.         (*xd->xpr_fclose)(fd);
  914.         errormsg(xd, "File already exists, skipping");
  915.         send_failure(xd, 'E');
  916.         return FALSE;
  917.     }
  918.  
  919.     xd->xpru.xpru_updatemask = XPRU_FILENAME;
  920.     xd->xpru.xpru_filename = name;
  921.     (*xd->xpr_update)(&xd->xpru);
  922.  
  923.     fd = bfopen(xd, name, "w");
  924.     xd->xpru.xpru_bytes = 0L;
  925.  
  926.     if (fd == 0)
  927.     {
  928.         errormsg(xd, "** Cannot open file...aborting **");
  929.         send_failure(xd, 'E');
  930.         return( FALSE );
  931.     }
  932.  
  933.     send_ack(xd);
  934.  
  935. /* Process each incoming packet until 'TC' packet received or failure */
  936.  
  937.     while( TRUE )
  938.     {
  939.         if (read_packet (xd, FALSE, FALSE))
  940.         {
  941.             switch (xd->r_buffer[0]) {
  942.             case 'N' :
  943.                 bytes = xd->r_size - 1;
  944.  
  945.                 if (xbfwrite(xd, &xd->r_buffer[1], bytes, fd) != bytes )
  946.                 {
  947.                     errormsg(xd, "** Write failure...aborting **");
  948.                     send_failure (xd, 'E');
  949.                     goto failed;
  950.                 }
  951.                 send_ack(xd);
  952.                 xd->xpru.xpru_updatemask = XPRU_BYTES|XPRU_BLOCKSIZE|XPRU_BLOCKS;
  953.                 xd->xpru.xpru_bytes += bytes;
  954.                 xd->xpru.xpru_blocksize = bytes;
  955.                 ++xd->xpru.xpru_blocks;
  956.                 (*xd->xpr_update)(&xd->xpru);
  957.                 break;
  958.  
  959.             case 'T' :
  960.                 if (xd->r_buffer[1] != 'C')
  961.                 {
  962.                     errormsg(xd, "** Invalid termination packet...aborting **");
  963.                     send_failure (xd, 'N');
  964.                     goto failed;
  965.                 }
  966.                 bfclose(xd, fd);
  967.  
  968.                 send_ack(xd);
  969.                 return( TRUE );
  970.  
  971.             case 'F' :
  972.                 send_ack(xd);
  973.                 errormsg(xd, "** Failure packet received...aborting **");
  974.                 goto failed;
  975.             }
  976.         }
  977.          else
  978.         {
  979.             errormsg(xd, "** Failed to receive packet...aborting **");
  980.             goto failed;
  981.         }
  982.     }
  983.  
  984. failed: /* transfer failed, delete partial file if requested */
  985.     bfclose(xd, fd);
  986.     if (xd->optKeep == 'N' && xd->xpr_unlink)
  987.         (*xd->xpr_unlink)(name);
  988.     return( FALSE );
  989. }
  990.  
  991. /*
  992.  * bp_DLE is called from the main program when the character <DLE> is
  993.  * received from the host.
  994.  *
  995.  * This routine calls read_packet and dispatches to the appropriate
  996.  * handler for the incoming packet.
  997.  */
  998. void    bp_DLE(struct XPR_DATA *xd)
  999. {
  1000.     int    xoff_flag;
  1001.     char    filename[300];
  1002.     long    oldserial;        /* old serial port settings */
  1003.     long    newserial;
  1004.     long    info;
  1005. /*
  1006.  * Begin by getting the next character.  If it is <B> then enter the
  1007.  * B_Protocol state.  Otherwise simply return.
  1008.  */
  1009.  
  1010.     if (xd->xio->xpr_setserial)
  1011.     {    /* set transparent mode 8-N-1 */
  1012.         oldserial = (*xd->xio->xpr_setserial)(-1L);
  1013.         if (oldserial != -1)
  1014.         {
  1015.             newserial = oldserial & 0xFFFFE0BC;
  1016.             if (newserial != oldserial)
  1017.                 (*xd->xio->xpr_setserial)(newserial);
  1018.             else
  1019.                 oldserial = -1L;
  1020.         }
  1021.     }
  1022.     else
  1023.         oldserial = -1L;
  1024.  
  1025.     if (cgetc (xd, 1000000L) != 'B')
  1026.         goto do_return;
  1027.  
  1028.     xd->ack_SA  = 0;    /* Initialize Send-ahead variables */
  1029.     xd->fill_SA = 0;
  1030.     xd->SA_Waiting = 0;
  1031.     xd->Aborting = FALSE;    /* not aborting ... yet */
  1032.     xd->Done = FALSE;
  1033. /*  <DLE><B> received; begin B Protocol */
  1034.  
  1035.     xoff_flag   = TRUE;
  1036.  
  1037.     xd->r_counter   = 0;
  1038.     xd->s_counter   = 0;
  1039.  
  1040.     memset(&xd->xpru, 0, sizeof(xd->xpru));
  1041.  
  1042.     xd->xpru.xpru_updatemask = XPRU_MSG;
  1043.     xd->xpru.xpru_msg = "Entering protocol";
  1044.     (*xd->xpr_update)(&xd->xpru);
  1045.  
  1046.     if (read_packet (xd, TRUE, FALSE))
  1047.     {
  1048. /* Dispatch on the type of packet just received */
  1049.  
  1050.             switch (xd->r_buffer[0]) {
  1051.         case 'T':     /* File Transfer Application */
  1052.             xd->xpru.xpru_updatemask = XPRU_PROTOCOL|XPRU_BLOCKCHECK|XPRU_ERRORS|XPRU_TIMEOUTS;
  1053.             xd->xpru.xpru_protocol = "B protocol";
  1054.             xd->xpru.xpru_blockcheck = "Checksum";
  1055.             if (xd->Quick_B)
  1056.             {
  1057.                 xd->xpru.xpru_protocol = filename;
  1058.                 strcpy(filename, "Quick B");
  1059.  
  1060.                 if (xd->Use_CRC)
  1061.                     xd->xpru.xpru_blockcheck = "CRC-16";
  1062.  
  1063.                 if (xd->Our_WS != 0) /* Allow send-ahead if other end agrees */
  1064.                     strcat(filename, " Send-Ahead");
  1065.             }
  1066.             (*xd->xpr_update)(&xd->xpru);
  1067.  
  1068.             switch (xd->r_buffer[1]) {
  1069.             case 'D' :    /* downloading */
  1070.                 break;
  1071.             case 'U' :    /* uploading */
  1072.                 break;
  1073.             default :
  1074.                 send_failure(xd, 'N');
  1075.                 goto do_return;
  1076.             }
  1077.  
  1078.             switch (xd->r_buffer[2]) {
  1079.             case 'A':    /* ascii file */
  1080.                 break;
  1081.             case 'B':    /* binary file */
  1082.                 break;
  1083.             default :
  1084.                 send_failure(xd, 'N');        /* not implemented */
  1085.                 goto do_return;
  1086.             }
  1087.  
  1088.             /* get the file name */
  1089.             xd->r_buffer[xd->r_size] = '\0';
  1090.             strcpy(filename, &xd->r_buffer[3]);
  1091.  
  1092.             /* display file size for uploads */
  1093.             xd->xpru.xpru_updatemask = XPRU_MSG;
  1094.  
  1095.             /* determine text/binary status */
  1096.             xd->TextC = -1;
  1097.             if (xd->optText == 'N')
  1098.                 xd->TextMode = FALSE;
  1099.             else if (xd->optText == 'Y')
  1100.                 xd->TextMode = TRUE;
  1101.             else if (xd->optText == 'H')
  1102.                 xd->TextMode = (xd->r_buffer[2] == 'A');
  1103.             else
  1104.             {    /* use comm program suggestion */
  1105.                 info = (*xd->xpr_finfo)(filename, 2L);
  1106.                 /* if the call failed, default to binary */
  1107.                 xd->TextMode = (info == 2L);
  1108.             }
  1109.             /* tell the user what we decided */
  1110.             xd->xpru.xpru_msg = xd->r_buffer[1] == 'D'
  1111.                 ? (xd->TextMode ? "Receiving text..."
  1112.                         : "Receiving binary...")
  1113.                 : (xd->TextMode ? "Sending text..."
  1114.                         : "Sending binary...");
  1115.             (*xd->xpr_update)(&xd->xpru);
  1116.  
  1117.             if (xd->r_buffer[1] == 'U')
  1118.             {
  1119.                 if( send_file(xd, filename))
  1120.                 {
  1121.                     xd->xpru.xpru_updatemask = XPRU_MSG;
  1122.                     xd->xpru.xpru_msg = "Transfer completed!";
  1123.                     (*xd->xpr_update)(&xd->xpru);
  1124.                 }
  1125.             }
  1126.             else
  1127.             {
  1128.                 if( receive_file(xd, filename))
  1129.                 {
  1130.                     xd->xpru.xpru_updatemask = XPRU_MSG;
  1131.                     xd->xpru.xpru_msg = "Transfer completed!";
  1132.                     (*xd->xpr_update)(&xd->xpru);
  1133.                 }
  1134.             }
  1135.             xd->Done = TRUE;
  1136.             break;
  1137.  
  1138.         case '+':          /* Received Transport Parameters Packet */
  1139.             do_transport_parameters(xd);
  1140.             break;
  1141.  
  1142.         case '?':          /* Received Application Parameters Packet */
  1143.             do_application_parameters(xd);
  1144.             break;
  1145.  
  1146.         default:    /* Unknown packet; tell host we don't know */
  1147.             send_failure (xd, 'N');
  1148.                     break;
  1149.  
  1150.         }  /* of case */
  1151.     }     /* of if read_packet the */
  1152.  
  1153. do_return:
  1154.     if (oldserial != -1)
  1155.         (*xd->xio->xpr_setserial)(oldserial);
  1156. }
  1157.