home *** CD-ROM | disk | FTP | other *** search
/ Columbia Kermit / kermit.zip / ek / eksw / src / kermit.c-linux < prev    next >
Text File  |  2020-01-01  |  101KB  |  3,389 lines

  1. // todo:
  2. // 1. should we send an I packet before the R-00 packet?
  3. // 2. commands to exit the server and/or logout from the session
  4.  
  5. #define KERMIT_C
  6. /*
  7.  * EKSW: Embedded Kermit with true sliding windows 
  8.  * protocol module Version: 0.94
  9.  * Most Recent Update: March 30, 2010
  10.  * John Dunlap
  11.  * 
  12.  * Author: Frank da Cruz. Copyright (C) 1995, 2004, Trustees of Columbia
  13.  * University in the City of New York. All rights reserved.
  14.  * 
  15.  * No stdio or other runtime library calls, no system calls, no system
  16.  * includes, no static data, and no global variables in this module.
  17.  * 
  18.  * Warning: you cannot use debug() in any routine whose argument list does
  19.  * not include "struct k_data *k".  Thus most routines in this module
  20.  * include this arg, even if they don't use it. 
  21.  *
  22.  * Redistribution and use in source and binary forms, with or without
  23.  * modification, are permitted provided that the following conditions are met:
  24.  *
  25.  * - Redistributions of source code must retain the above copyright notice,
  26.  *   this list of conditions and the following disclaimer.
  27.  *
  28.  * - Redistributions in binary form must reproduce the above copyright notice,
  29.  *   this list of conditions and the following disclaimer in the documentation
  30.  *   and/or other materials provided with the distribution.
  31.  *
  32.  * - Neither the name of Columbia University nor the names of its contributors
  33.  *   may be used to endorse or promote products derived from this software
  34.  *   without specific prior written permission.
  35.  *  
  36.  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  37.  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  38.  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  39.  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
  40.  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  41.  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  42.  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  43.  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  44.  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  45.  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  46.  * POSSIBILITY OF SUCH DAMAGE.
  47.  */
  48.  
  49. # if DEBUG
  50. #include <stdio.h>
  51. #include <stdlib.h>
  52. #include <string.h>
  53. #include <ctype.h>
  54. # endif
  55.  
  56. #include "cdefs.h"              /* C language defs for all modules */
  57. #include "debug.h"              /* Debugging */
  58. #include "kermit.h"             /* Kermit protocol definitions */
  59.  
  60. #define USE_ZGETC_MACRO
  61. #ifdef USE_ZGETC_MACRO
  62. #define zgetc() \
  63. ((--(k->zincnt))>=0)?((int)(*(k->zinptr)++)&0xff):(*(k->readf))(k)
  64. #else // USE_ZGETC_MACRO
  65. STATIC int
  66. zgetc (struct k_data *k)
  67. {
  68.    UCHAR *ptr;
  69.    int kar;
  70.  
  71.    k->zincnt--;
  72.    if (k->zincnt >= 0)
  73.    {
  74.       ptr = k->zinptr;
  75.       k->zinptr++;
  76.       kar = (*ptr) & 0xFF;
  77.    }
  78.    else
  79.    {
  80.       kar = (*(k->readf)) (k);
  81.    }
  82.    return (kar);
  83. }
  84. #endif // USE_ZGETC_MACRO
  85.  
  86. /*
  87.  * See cdefs.h for meaning of STATIC, ULONG, and UCHAR 
  88.  */
  89.  
  90. STATIC ULONG stringnum (UCHAR *, struct k_data *);
  91. STATIC UCHAR *numstring (ULONG, UCHAR *, int, struct k_data *);
  92. STATIC int spkt (char, short, int, UCHAR *, struct k_data *);
  93. STATIC int ack (struct k_data *, short, UCHAR * text);
  94. STATIC int nak (struct k_data *, short, short);
  95. STATIC int chk1 (UCHAR *, struct k_data *);
  96. STATIC USHORT chk2 (UCHAR *, struct k_data *);
  97. STATIC USHORT chk3 (UCHAR *, struct k_data *);
  98.  
  99. STATIC void spar (struct k_data *, UCHAR *, int);
  100. STATIC int rpar (struct k_data *, char);
  101. STATIC int decode (struct k_data *, struct k_response *, short, UCHAR *,
  102.                    int rslot);
  103.  
  104. STATIC int gattr (struct k_data *, UCHAR *, struct k_response *);
  105. STATIC int sattr (struct k_data *, struct k_response *);
  106.  
  107. STATIC int sdata (struct k_data *, struct k_response *);
  108.  
  109. STATIC void epkt (char *, struct k_data *);
  110. STATIC int getpkt (struct k_data *, struct k_response *);
  111. STATIC int encstr (UCHAR *, struct k_data *, struct k_response *);
  112. STATIC void encode (int, int, struct k_data *);
  113. STATIC short nxtpkt (struct k_data *);
  114. STATIC int resend (struct k_data *, short seq);
  115. STATIC int nused_sslots (struct k_data *);
  116. STATIC int nused_rslots (struct k_data *);
  117.  
  118. #if 0
  119. STATIC void decstr (UCHAR *, struct k_data *, struct k_response *);
  120. STATIC short prevpkt (struct k_data *);
  121. #endif
  122.  
  123. STATIC short earliest_sseq (struct k_data *k);
  124.  
  125. #ifdef DEBUG
  126. int xerror (void);
  127. STATIC void show_sslots (struct k_data *);
  128. STATIC void show_rslots (struct k_data *);
  129. #endif /* DEBUG */
  130.  
  131. STATIC int test_rslots (struct k_data *);
  132.  
  133. STATIC short
  134. earliest_rseq (struct k_data *k)
  135. {
  136.    short slot;
  137.    short seq;
  138.    short seq_ref = -1;
  139.    short seq_rot;
  140.    short seq_rot_oldest = -1;
  141.    short seq_oldest = -1;
  142.  
  143.    for (slot = 0; slot < k->wslots; slot++)
  144.    {
  145.       seq = k->ipktinfo[slot].seq;
  146.       if (seq >= 0 && seq < 64)
  147.       {
  148.          seq_ref = seq;
  149.          seq_oldest = seq;
  150.          seq_rot_oldest = 0;
  151.       }
  152.    }
  153.  
  154.    if (seq_ref != -1)
  155.    {
  156.       for (slot = 0; slot < k->wslots; slot++)
  157.       {
  158.          seq = k->ipktinfo[slot].seq;
  159.          if (seq >= 0 && seq < 64)
  160.          {
  161.             seq_rot = seq - seq_ref;
  162.             if (seq_rot < -31)
  163.                seq_rot += 64;
  164.             if (seq_rot > 31)
  165.                seq_rot -= 64;
  166.  
  167.             if (seq_rot < seq_rot_oldest)
  168.             {
  169.                seq_rot_oldest = seq_rot;
  170.                seq_oldest = seq;
  171.             }
  172.          }
  173.       }
  174.    }
  175.  
  176.    debug (DB_LOG, "EARLIEST_RSEQ", 0, seq_oldest);
  177.    return (seq_oldest);
  178. }
  179.  
  180. STATIC short
  181. latest_rseq (struct k_data *k)
  182. {
  183.    short slot;
  184.    short seq;
  185.    short seq_ref = -1;
  186.    short seq_rot;
  187.    short seq_rot_newest = -1;
  188.    short seq_newest = -1;
  189.  
  190.    for (slot = 0; slot < k->wslots; slot++)
  191.    {
  192.       seq = k->ipktinfo[slot].seq;
  193.       if (seq >= 0 && seq < 64)
  194.       {
  195.          seq_ref = seq;
  196.          seq_newest = seq;
  197.          seq_rot_newest = 0;
  198.       }
  199.    }
  200.  
  201.    if (seq_ref != -1)
  202.    {
  203.       for (slot = 0; slot < k->wslots; slot++)
  204.       {
  205.          seq = k->ipktinfo[slot].seq;
  206.          if (seq >= 0 && seq < 64)
  207.          {
  208.             seq_rot = seq - seq_ref;
  209.             if (seq_rot < -31)
  210.                seq_rot += 64;
  211.             if (seq_rot > 31)
  212.                seq_rot -= 64;
  213.  
  214.             if (seq_rot > seq_rot_newest)
  215.             {
  216.                seq_rot_newest = seq_rot;
  217.                seq_newest = seq;
  218.             }
  219.          }
  220.       }
  221.    }
  222.  
  223.    debug (DB_LOG, "LATEST_RSEQ", 0, seq_newest);
  224.    return (seq_newest);
  225. }
  226.  
  227. // nak_oldest_unacked() never finds anything because
  228. // we ACK as soon as we put each packet into its slot.
  229. // Also, rseq for short packets is unvetted and for long packets
  230. // the only test is a simple checksum so it's safer
  231. // to time out.  For Iridium, there are no bad bytes so
  232. // the point is moot.
  233. #undef USE_NAK_OLDEST_UNACKED
  234. #ifdef USE_NAK_OLDEST_UNACKED
  235. STATIC int
  236. nak_oldest_unacked (struct k_data *k, short rseq)
  237. {
  238.    short slot;
  239.    short seq;
  240.    short age;
  241.    short oldest_seq = -1;
  242.    short oldest_age = -99;
  243.    short oldest_rslot = -1;
  244.    int flg;
  245.  
  246.    for (slot = 0; slot < k->wslots; slot++)
  247.    {
  248.       seq = k->ipktinfo[slot].seq;
  249.       flg = k->ipktinfo[slot].flg;
  250.       if (seq >= 0 && seq < 64 && flg == 0)
  251.       {
  252.          age = k->s_seq - seq;
  253.          if (age < 0)
  254.             age += 64;
  255.          if (age > 63)
  256.             age -= 64;
  257.  
  258.          if (age > oldest_age)
  259.          {
  260.             oldest_rslot = slot;
  261.             oldest_age = age;
  262.             oldest_seq = seq;
  263.          }
  264.       }
  265.    }
  266.  
  267.    debug (DB_LOG, "NAK_OLDEST_UNACKED oldest_seq", 0, oldest_seq);
  268.    debug (DB_LOG, "NAK_OLDEST_UNACKED rseq", 0, rseq);
  269.  
  270.    if (oldest_seq != -1)
  271.    {
  272.       nak (k, oldest_seq, oldest_rslot);
  273. //    k->anseq = oldest_seq;
  274.       return (X_OK);
  275.    }
  276.    else
  277.    {
  278. //    nak (k, (k->anseq+1)&63, -1);
  279. //    nak (k, k->anseq, -1);
  280. //    nak (k, rseq, -1);
  281.       return (X_OK);
  282.    }
  283. }
  284. #endif /* USE_NAK_OLDEST_UNACKED */
  285.  
  286. STATIC int
  287. handle_good_rpkt (struct k_data *k, struct k_response *r,
  288.                   short rseq, UCHAR * pbuf)
  289. {
  290.    // true sliding windows for receive see p. 292
  291.    short wsize = k->wslots;     // negotiated window size
  292.    short high = -1;             // latest table entry (chronlogically)
  293.    short low = -1;              // earliest possible table entry
  294.    short eseq = -1;             // expected sequence number
  295. // short psn = rseq;            // just-arrived packet sequence number
  296.  
  297.    short rslot;
  298.    short nused;
  299.    int isnxt = 0;
  300.    int isgap = 0;
  301.    int isold = 0;
  302.    int rc;
  303.    int i;
  304.    short iseq;
  305.    short dseq;
  306.    short nnak;
  307.    int do_add_pkt;
  308.  
  309.    nused = nused_rslots (k);
  310.  
  311.    if (nused == 0)
  312.    {
  313.       eseq = rseq;              // for first time
  314.    }
  315.    else
  316.    {
  317.       high = latest_rseq (k);
  318.       low = high - wsize + 1;
  319.       if (low < 0)
  320.          low += 64;
  321.  
  322.       eseq = (high + 1) & 63;
  323.  
  324.       if (rseq != eseq)
  325.       {
  326.          for (i = 2; i <= wsize; i++)
  327.          {
  328.             iseq = (high + i) & 63;
  329.             if (rseq == iseq)
  330.             {
  331.                isgap = 1;
  332.                break;
  333.             }
  334.          }
  335.       }
  336.  
  337.       if (rseq != eseq && !isgap)
  338.       {
  339.          short hp = (high + 1) & 63;
  340.          for (i = low; i != hp; i++)
  341.          {
  342.             iseq = i & 63;
  343.             if (rseq == iseq)
  344.             {
  345.                isold = 1;
  346.                break;
  347.             }
  348.          }
  349.       }
  350.    }
  351.  
  352.    if (rseq == eseq)
  353.       isnxt = 1;
  354.    else
  355.       isnxt = 0;
  356.  
  357.    debug (DB_LOG, "HANDLE_RPKT rseq", 0, rseq);
  358.    debug (DB_LOG, "  high", 0, high);
  359.    debug (DB_LOG, "  low", 0, low);
  360.    debug (DB_LOG, "  eseq", 0, eseq);
  361.    debug (DB_LOG, "  nused", 0, nused);
  362.    debug (DB_LOG, "  isnxt", 0, isnxt);
  363.    debug (DB_LOG, "  isgap", 0, isgap);
  364.    debug (DB_LOG, "  isold", 0, isold);
  365.  
  366. #ifdef DEBUG
  367.    show_rslots (k);
  368. #endif
  369.  
  370.    // write old packets to file in order to keep
  371.    // our receive window aligned with sender's window
  372.    if (isnxt || isgap)
  373.    {
  374.       debug (DB_LOG, "HANDLE_RPKT align our recv window with sender rseq",
  375.              0, rseq);
  376.       for (i = 0; i < wsize; i++)
  377.       {
  378.          iseq = earliest_rseq (k);
  379.          debug (DB_LOG, "HANDLE_RPKT iseq", 0, iseq);
  380.          if (iseq < 0 || iseq >= 64)
  381.             break;
  382.  
  383.          dseq = rseq - iseq;
  384.          if (dseq < 0)
  385.             dseq += 64;
  386.          debug (DB_LOG, "HANDLE_RPKT dseq", 0, dseq);
  387.          if (dseq < wsize)
  388.             continue;
  389.  
  390.          rslot = k->r_pw[iseq];
  391.          debug (DB_LOG, "HANDLE_RPKT rslot", 0, rslot);
  392.          if (rslot < 0 || rslot >= wsize)
  393.          {
  394.             debug (DB_LOG, "HANDLE_RPKT error 1, rslot", 0, rslot);
  395.             debug (DB_LOG, "HANDLE_RPKT iseq", 0, iseq);
  396.             return (X_ERROR);
  397.          }
  398.  
  399.          if (k->ipktinfo[rslot].flg == 0)
  400.          {
  401.             debug (DB_MSG, "HANDLE_RPKT error 2, flg", 0,
  402.                    k->ipktinfo[rslot].flg);
  403.             debug (DB_LOG, "  rslot", 0, rslot);
  404.             return (X_ERROR);
  405.          }
  406.  
  407.          /* Decode pkt and write file */
  408.          rc = decode (k, r, 1, k->ipktinfo[rslot].buf, rslot);
  409.          debug (DB_LOG, "HANDLE_RPKT decode rc", 0, rc);
  410.          if (rc != X_OK)
  411.          {
  412.             debug (DB_LOG, "decode failed rc", 0, rc);
  413. #ifdef DEBUG
  414.             show_rslots (k);
  415. #endif
  416.             exit (1);
  417.             epkt ("EKSW decode failed", k);
  418.             return (rc);
  419.          }
  420.          free_rslot (k, rslot);
  421. #ifdef DEBUG
  422.          show_rslots (k);
  423. #endif
  424.       }
  425.    }
  426.  
  427.    do_add_pkt = 0;
  428.  
  429.    if (isnxt)
  430.    {
  431.       // 1. PSN = SEQ = HIGH + 1, the usual case
  432.       debug (DB_LOG, "HANDLE_RPKT usual rseq", 0, rseq);
  433.  
  434.       // acknowledge incoming packet
  435.       ack (k, rseq, (UCHAR *) 0);
  436.  
  437.       do_add_pkt = 1;
  438.    }
  439.    else if (isgap)
  440.    {
  441.       // 2. PSN != SEQ, a new packet, but not the next sequential one
  442.       debug (DB_LOG, "HANDLE_RPKT isgap rseq", 0, rseq);
  443.  
  444.       nnak = rseq - high - 1;
  445.       if (nnak < 0)
  446.          nnak += 64;
  447.  
  448.       debug (DB_LOG, "  nnak", 0, nnak);
  449.  
  450.       // acknowledge incoming packet
  451.       ack (k, rseq, (UCHAR *) 0);
  452.  
  453.       // send NAKs for whole gap: HIGH+1 to PSN-1
  454.       for (i = 0; i < nnak; i++)
  455.       {
  456.          iseq = (high + 1 + i) & 63;
  457.          rslot = k->r_pw[iseq];
  458.          debug (DB_LOG, "HANDLE_RPKT isgap nak iseq", 0, iseq);
  459.          debug (DB_LOG, "  rslot", 0, rslot);
  460.          nak (k, iseq, rslot);
  461.       }
  462.  
  463.       do_add_pkt = 2;
  464.    }
  465.    else if (isold)
  466.    {
  467.       // 3. LOW <= PSN <= HIGH, old, possibly missing pkt arrived
  468.       debug (DB_LOG, "HANDLE_RPKT isold rseq", 0, rseq);
  469.  
  470.       ack (k, rseq, (UCHAR *) 0);
  471.  
  472.       // If packet was missing add it to window.
  473.       // There is supposed to be a slot available.
  474.       // Don't store a repeated packet again
  475.       rslot = k->r_pw[rseq];
  476.       if (rslot < 0 || rslot >= wsize)
  477.       {
  478.          do_add_pkt = 3;
  479.       }
  480.    }
  481.    else
  482.    {
  483.       // 4. PSN < LOW || PSN > HIGH -- unexpected, undesired, ignore
  484.       debug (DB_LOG, "HANDLE_RPKT unexpected so ignored rseq", 0, rseq);
  485.    }
  486.  
  487.    if (do_add_pkt > 0)
  488.    {
  489.       test_rslots (k);
  490.  
  491.       get_rslot (k, &rslot);
  492.       if (rslot < 0 || rslot >= wsize)
  493.       {
  494.          debug (DB_LOG, "HANDLE_RPKT error 3, rslot", 0, rslot);
  495.          debug (DB_LOG, "  do_add_pkt", 0, do_add_pkt);
  496.          return (X_ERROR);
  497.       }
  498.  
  499.       test_rslots (k);
  500.  
  501.       // put pbuf into slot in receive table
  502.       for (i = 0; i < P_BUFLEN; i++)
  503.       {
  504.          k->ipktinfo[rslot].buf[i] = pbuf[i];
  505.          if (pbuf[i] == 0)
  506.             break;
  507.       }
  508.       k->ipktinfo[rslot].len = i;
  509.       k->ipktinfo[rslot].flg = 1;
  510.       k->ipktinfo[rslot].seq = rseq;
  511.       k->ipktinfo[rslot].crc = chk3 (pbuf, k);
  512.       k->r_pw[rseq] = rslot;
  513.  
  514.       test_rslots (k);
  515.    }
  516.  
  517. #ifdef DEBUG
  518.    show_rslots (k);
  519. #endif
  520.  
  521.    return (X_OK);
  522. }
  523.  
  524. void
  525. handle_bad_rpkt ()
  526. {
  527. }
  528.  
  529.  
  530. int
  531. ok2rxd (struct k_data *k)
  532. {
  533.    int ok;
  534.    int sw_full;
  535.  
  536.    if (nused_sslots (k) == k->wslots)
  537.       sw_full = 1;
  538.    else
  539.       sw_full = 0;
  540.  
  541.    if ((k->what == W_SEND || k->what == W_GET) &&
  542.        k->state == S_DATA && sw_full == 0)
  543.       ok = 0;
  544.    else
  545.       ok = 1;
  546.  
  547.    debug (DB_LOG, "ok2rxd", 0, ok);
  548.  
  549.    k->do_rxd = ok;
  550.  
  551.    return (ok);
  552. }
  553.  
  554. STATIC void
  555. free_sslot_easca (struct k_data *k)
  556. // Remove earliest & subsequent contiguous ACK'd packets.
  557. // Packet numbers thus remain in order in the sliding window.
  558. // This way we don't send packets which won't fit in their window.
  559. {
  560.    int nfreed = 0;
  561.    int i;
  562.  
  563.    for (i = 0; i < k->wslots; i++)
  564.    {
  565.       short seq_earl, slot_earl;
  566.       seq_earl = earliest_sseq (k);
  567.       if (seq_earl < 0 || seq_earl > 63)
  568.          break;
  569.       slot_earl = k->s_pw[seq_earl];
  570.       if (slot_earl < 0 || slot_earl >= k->wslots)
  571.          break;
  572.       if (k->opktinfo[slot_earl].flg == 0)
  573.          break;
  574.       free_sslot (k, slot_earl);
  575.       nfreed++;
  576.    }
  577.    debug (DB_LOG, "FREE_SSLOT_EASCA nfreed", 0, nfreed);
  578. }
  579.  
  580. #ifdef DEBUG
  581. STATIC void
  582. chk_sseq_nos (struct k_data *k)
  583. // check that sequence numbers are still in order
  584. {
  585.    int i;
  586.    int dif, dif0;
  587.    int ok;
  588.    int nerr;
  589.  
  590.    nerr = 0;
  591.    ok = 0;
  592.    for (i = 0; i < k->wslots; i++)
  593.    {
  594.       short ss;
  595.       if (k->opktinfo[i].seq < 0)
  596.          continue;
  597.       ss = k->opktinfo[i].seq - k->r_seq;
  598.       if (ss < -32)
  599.          ss += 64;
  600.       if (ss > 31)
  601.          ss -= 64;
  602.       dif = ss - i;
  603.       if (ok == 0)
  604.       {
  605.          ok = 1;
  606.          dif0 = dif;
  607.       }
  608.       else if (dif != dif0 && (dif + k->wslots) != dif0)
  609.          nerr++;
  610.    }
  611.  
  612.    if (nerr)
  613.    {
  614.       debug (DB_LOG, "CHK_SEQ nerr", 0, nerr);
  615. #ifdef DEBUG
  616.       show_sslots (k);
  617. #endif
  618.    }
  619. }
  620. #endif
  621.  
  622. STATIC int
  623. flush_to_file (struct k_data *k, struct k_response *r)
  624. {
  625.    short wsize = k->wslots;     // negotiated window size
  626.    short rslot;
  627.    short iseq;
  628.    int rc;
  629.  
  630.    debug (DB_MSG, "FLUSH_TO_FILE begin", 0, 0);
  631.  
  632.    // flush receive window to file
  633.    while ((iseq = earliest_rseq (k)) >= 0)
  634.    {
  635.  
  636.       debug (DB_LOG, "FLUSH_TO_FILE iseq", 0, iseq);
  637.  
  638.       rslot = k->r_pw[iseq];
  639.  
  640.       debug (DB_LOG, "  rslot", 0, rslot);
  641.  
  642.       if (rslot < 0 || rslot >= wsize)
  643.       {
  644.          debug (DB_LOG, "FLUSH_TO_FILE error rslot", 0, rslot);
  645.          return (X_ERROR);
  646.       }
  647.  
  648.       if (k->ipktinfo[rslot].flg == 0)
  649.       {
  650.          debug (DB_MSG, "FLUSH_TO_FILE error flg", 0, k->ipktinfo[rslot].flg);
  651.          return (X_ERROR);
  652.       }
  653.  
  654.       /* Decode pkt and write file */
  655.       rc = decode (k, r, 1, k->ipktinfo[rslot].buf, rslot);
  656.       debug (DB_LOG, "FLUSH_TO_FILE decode rc", 0, rc);
  657.       if (rc != X_OK)
  658.       {
  659.          debug (DB_LOG, "decode failed rc", 0, rc);
  660. #ifdef DEBUG
  661.          show_rslots (k);
  662. #endif
  663.          exit (1);
  664.          epkt ("EKSW flush to file failed", k);
  665.          return (rc);
  666.       }
  667.       free_rslot (k, rslot);
  668. #ifdef DEBUG
  669.       show_rslots (k);
  670. #endif
  671.    }
  672.  
  673.    debug (DB_LOG, "FLUSH_TO_FILE obufpos", 0, k->obufpos);
  674.  
  675.    if (k->obufpos > 0)
  676.    {                            /* Flush output buffer to file */
  677.       rc = (*(k->writef)) (k, k->obuf, k->obufpos);
  678.       debug (DB_LOG, "FLUSH_TO_FILE writef rc", 0, rc);
  679.       r->sofar += k->obufpos;
  680.       r->sofar_rumor += k->obufpos;
  681.       k->obufpos = 0;
  682.    }
  683.  
  684.    return (rc);
  685. }
  686.  
  687. /*
  688.  * Utility routines 
  689.  */
  690.  
  691. UCHAR *
  692. get_rslot (struct k_data * k, short *n)
  693. {                               /* Find a free packet buffer */
  694.    register int slot;
  695.    /*
  696.     * Note: We don't clear the retry count here. It is cleared only after 
  697.     * the NEXT packet arrives, which indicates that the other Kermit got
  698.     * our ACK for THIS packet. 
  699.     */
  700.    for (slot = 0; slot < k->wslots; slot++)
  701.    {                            /* Search */
  702.       if (k->ipktinfo[slot].len < 1)
  703.       {
  704.          *n = slot;             /* Slot number */
  705.          k->ipktinfo[slot].len = -1;    /* Mark it as allocated but not used */
  706.          k->ipktinfo[slot].seq = -1;
  707.          k->ipktinfo[slot].typ = SP;
  708.          k->ipktinfo[slot].crc = 0xFFFF;
  709.          /*
  710.           * k->ipktinfo[slot].rtr = 0; 
  711.           *//*
  712.           * (see comment above) 
  713.           */
  714.          k->ipktinfo[slot].dat = (UCHAR *) 0;
  715.          debug (DB_LOG, "GET_RSLOT slot", 0, slot);
  716.          return (k->ipktinfo[slot].buf);
  717.       }
  718.    }
  719.    *n = -1;
  720.    debug (DB_LOG, "GET_RSLOT slot", 0, -1);
  721.    return ((UCHAR *) 0);
  722. }
  723.  
  724. void                            /* Initialize a window slot */
  725. free_rslot (struct k_data *k, short slot)
  726. {
  727.    short seq;
  728.  
  729.    debug (DB_LOG, "FREE_RSLOT slot", 0, slot);
  730.  
  731.    if (slot < 0 || slot >= P_WSLOTS)
  732.    {
  733.       debug (DB_LOG, "  confused: slot out of bounds", 0, slot);
  734.       epkt ("EKSW free_rslot confused", k);
  735.       exit (1);
  736.       return;
  737.    }
  738.  
  739.    seq = k->ipktinfo[slot].seq;
  740.    debug (DB_LOG, "  seq", 0, seq);
  741.  
  742.    if (seq >= 0 && seq < 64)
  743.       k->r_pw[seq] = -1;
  744.  
  745.    k->ipktinfo[slot].len = 0;   /* Packet length */
  746.    k->ipktinfo[slot].seq = -1;  /* Sequence number */
  747.    k->ipktinfo[slot].typ = (char) 0;    /* Type */
  748.    k->ipktinfo[slot].rtr = 0;   /* Retry count */
  749.    k->ipktinfo[slot].flg = 0;   /* Flags */
  750.    k->ipktinfo[slot].crc = 0xFFFF;
  751. }
  752.  
  753. UCHAR *
  754. get_sslot (struct k_data *k, short *n)
  755. {                               /* Find a free packet buffer */
  756.    register int slot;
  757.    for (slot = 0; slot < k->wslots; slot++)
  758.    {                            /* Search */
  759.       if (k->opktinfo[slot].len < 1)
  760.       {
  761.          *n = slot;             /* Slot number */
  762.          k->opktinfo[slot].len = -1;    /* Mark it as allocated but not used */
  763.          k->opktinfo[slot].seq = -1;
  764.          k->opktinfo[slot].typ = SP;
  765.          k->opktinfo[slot].rtr = 0;
  766.          k->opktinfo[slot].flg = 0;     // ACK'd bit
  767.          k->opktinfo[slot].dat = (UCHAR *) 0;
  768.          debug (DB_LOG, "GET_SSLOT slot", 0, slot);
  769.          return (k->opktinfo[slot].buf);
  770.       }
  771.    }
  772.    *n = -1;
  773.    debug (DB_LOG, "GET_SSLOT slot", 0, -1);
  774.    return ((UCHAR *) 0);
  775. }
  776.  
  777. STATIC int
  778. nused_sslots (struct k_data *k)
  779. {
  780.    int i;
  781.    int n = 0;
  782.  
  783.    for (i = 0; i < k->wslots; i++)
  784.       if (k->opktinfo[i].len > 0)
  785.          n++;
  786.    return (n);
  787. }
  788.  
  789. STATIC int
  790. nused_rslots (struct k_data *k)
  791. {
  792.    int i;
  793.    int n = 0;
  794.  
  795.    for (i = 0; i < k->wslots; i++)
  796.       if (k->ipktinfo[i].len > 0)
  797.          n++;
  798.    return (n);
  799. }
  800.  
  801. #ifdef DEBUG
  802. STATIC void
  803. show_sslots (struct k_data *k)
  804. {
  805.    int slot, j, n;
  806.    char tmp[80], *p;
  807.  
  808.    debug (DB_MSG, "SHOW_SSLOTS", 0, 0);
  809.    for (slot = 0; slot < k->wslots; slot++)
  810.    {
  811.       n = snprintf (tmp, sizeof (tmp) - 1, "  slot=%02d seq=%02d flg=%d "
  812.                     "len=%4d crc=%5d pwi=",
  813.                     slot, k->opktinfo[slot].seq, k->opktinfo[slot].flg,
  814.                     k->opktinfo[slot].len, k->opktinfo[slot].crc);
  815.  
  816.       p = tmp + n;
  817.       for (j = 0; j < 64; j++)
  818.          if (k->s_pw[j] == slot)
  819.          {
  820.             n = snprintf (p, sizeof (tmp) - 1 - n, " %02d", j);
  821.             p += n;
  822.          }
  823.       debug (DB_MSG, tmp, 0, 0);
  824.    }
  825. }
  826. #endif /* DEBUG */
  827. #ifdef DEBUG
  828. STATIC void
  829. show_rslots (struct k_data *k)
  830. {
  831.    int slot, j, n, m;
  832.    char tmp[256], *p;
  833.  
  834.  
  835.    debug (DB_MSG, "SHOW_RSLOTS", 0, 0);
  836.    for (slot = 0; slot < k->wslots; slot++)
  837.    {
  838.       int ok;
  839.       if (k->ipktinfo[slot].seq >= 0 &&
  840.           chk3 (k->ipktinfo[slot].buf, k) != k->ipktinfo[slot].crc)
  841.          ok = 0;
  842.       else
  843.          ok = 1;
  844.  
  845.       p = tmp;
  846.       m = sizeof (tmp) - 1;
  847.       n = snprintf (p, m, "  slot=%02d seq=%02d flg=%d "
  848.                     "len=%4d crc=%5d crcok=%d pwi=",
  849.                     slot, k->ipktinfo[slot].seq, k->ipktinfo[slot].flg,
  850.                     k->ipktinfo[slot].len, k->ipktinfo[slot].crc, ok);
  851.       if (n < 0)
  852.       {
  853.          debug (DB_LOG, "SHOW_RSLOTS 1 n", 0, n);
  854.          debug (DB_LOG, "SHOW_RSLOTS 1 m", 0, m);
  855.          debug (DB_LOG, "SHOW_RSLOTS 1 slot", 0, slot);
  856.          debug (DB_LOG, "SHOW_RSLOTS 1 seq", 0, k->ipktinfo[slot].seq);
  857.          debug (DB_LOG, "SHOW_RSLOTS 1 flg", 0, k->ipktinfo[slot].flg);
  858.          debug (DB_LOG, "SHOW_RSLOTS 1 len", 0, k->ipktinfo[slot].len);
  859.          debug (DB_LOG, "SHOW_RSLOTS 1 crc", 0, k->ipktinfo[slot].crc);
  860.          debug (DB_LOG, "SHOW_RSLOTS ok", 0, ok);
  861.          debug (DB_LOG, "SHOW_RSLOTS 1 p-tmp", 0, p - tmp);
  862.          debug (DB_LOG, "SHOW_RSLOTS 1 tmp", tmp, 0);
  863.          exit (1);
  864.       }
  865.       p += n;
  866.       m -= n;
  867.       for (j = 0; j < 64; j++)
  868.       {
  869.          if (k->r_pw[j] == slot)
  870.          {
  871.             n = snprintf (p, m, " %02d", j);
  872.             if (n < 0)
  873.             {
  874.                debug (DB_LOG, "SHOW_RSLOTS 2 j", 0, j);
  875.                debug (DB_LOG, "SHOW_RSLOTS 2 n", 0, n);
  876.                debug (DB_LOG, "SHOW_RSLOTS 2 m", 0, m);
  877.                debug (DB_LOG, "SHOW_RSLOTS 2 slot", 0, slot);
  878.                debug (DB_LOG, "SHOW_RSLOTS 2 seq", 0, k->ipktinfo[slot].seq);
  879.                debug (DB_LOG, "SHOW_RSLOTS 2 flg", 0, k->ipktinfo[slot].flg);
  880.                debug (DB_LOG, "SHOW_RSLOTS 2 len", 0, k->ipktinfo[slot].len);
  881.                debug (DB_LOG, "SHOW_RSLOTS 2 crc", 0, k->ipktinfo[slot].crc);
  882.                debug (DB_LOG, "SHOW_RSLOTS 2 p-tmp", 0, p - tmp);
  883.                debug (DB_LOG, "SHOW_RSLOTS 2 tmp", tmp, 0);
  884.                exit (1);
  885.             }
  886.             p += n;
  887.             m -= n;
  888.          }
  889.       }
  890.       debug (DB_MSG, tmp, 0, 0);
  891.    }
  892. }
  893. #endif
  894.  
  895. STATIC int
  896. test_rslots (struct k_data *k)
  897. {
  898.    int slot, nbad = 0;
  899.  
  900.    for (slot = 0; slot < k->wslots; slot++)
  901.    {
  902.       if (k->ipktinfo[slot].seq >= 0 &&
  903.           chk3 (k->ipktinfo[slot].buf, k) != k->ipktinfo[slot].crc)
  904.       {
  905.          nbad++;
  906.          debug (DB_HEX, "THEX", k->ipktinfo[slot].buf, k->ipktinfo[slot].len);
  907.       }
  908.    }
  909.    if (nbad > 0)
  910.    {
  911.       debug (DB_LOG, "TEST_RSLOTS nbad", 0, nbad);
  912. #ifdef DEBUG
  913.       show_rslots (k);
  914. # endif
  915.       epkt ("EKSW test rslots failed", k);
  916.       exit (1);
  917. //    return X_ERROR;
  918.    }
  919.    return X_OK;
  920. }
  921.  
  922. void                            /* Initialize a window slot */
  923. free_sslot (struct k_data *k, short slot)
  924. {
  925.    short seq;
  926.  
  927.    debug (DB_LOG, "FREE_SSLOT slot", 0, slot);
  928.    if (slot < 0 || slot >= P_WSLOTS)
  929.       return;
  930.  
  931.    seq = k->opktinfo[slot].seq;
  932.    debug (DB_LOG, "  seq", 0, seq);
  933.  
  934.    if (seq >= 0 && seq < 64)
  935.       k->s_pw[seq] = -1;
  936.  
  937.    k->opktinfo[slot].len = 0;   /* Packet length */
  938.    k->opktinfo[slot].seq = -1;  /* Sequence number */
  939.    k->opktinfo[slot].typ = (char) 0;    /* Type */
  940.    k->opktinfo[slot].rtr = 0;   /* Retry count */
  941.    k->opktinfo[slot].flg = 0;   /* ACK'd bit */
  942. }
  943.  
  944. STATIC short
  945. earliest_sseq (struct k_data *k)
  946. {
  947.    short slot;
  948.    short seq;
  949.    short age;
  950.    short oldest_seq = -1;
  951.    short oldest_age = -99;
  952.  
  953.    for (slot = 0; slot < k->wslots; slot++)
  954.    {
  955.       seq = k->opktinfo[slot].seq;
  956.       if (seq >= 0 && seq < 64)
  957.       {
  958.          age = k->s_seq - seq;
  959.          if (age < 0)
  960.             age += 64;
  961.          if (age > 63)
  962.             age -= 64;
  963.  
  964.          if (age > oldest_age)
  965.          {
  966.             oldest_age = age;
  967.             oldest_seq = seq;
  968.          }
  969.       }
  970.    }
  971.  
  972.    debug (DB_LOG, "EARLIEST_SSEQ", 0, oldest_seq);
  973.    return (oldest_seq);
  974. }
  975.  
  976. /*
  977.  * C H K 1 -- Compute a type-1 Kermit 6-bit checksum.  
  978.  */
  979.  
  980. STATIC int
  981. chk1 (UCHAR * pkt, struct k_data *k)
  982. {
  983.    register unsigned int chk;
  984.    chk = chk2 (pkt, k);
  985.    chk = (((chk & 0300) >> 6) + chk) & 077;
  986.    return ((int) chk);
  987. }
  988.  
  989. /*
  990.  * C H K 2 -- Numeric sum of all the bytes in the packet, 12 bits.  
  991.  */
  992.  
  993. STATIC USHORT
  994. chk2 (UCHAR * pkt, struct k_data *k)
  995. {
  996.    register USHORT chk;
  997.    for (chk = 0; *pkt != '\0'; pkt++)
  998.       chk += *pkt;
  999.    return (chk);
  1000. }
  1001.  
  1002. /*
  1003.  * C H K 3 -- Compute a type-3 Kermit block check.  
  1004.  */
  1005. /*
  1006.  * Calculate the 16-bit CRC-CCITT of a null-terminated string using a
  1007.  * lookup table.  Assumes the argument string contains no embedded nulls. 
  1008.  */
  1009. STATIC USHORT
  1010. chk3 (UCHAR * pkt, struct k_data * k)
  1011. {
  1012.    register USHORT c, crc;
  1013.    for (crc = 0; *pkt != '\0'; pkt++)
  1014.    {
  1015.       c = crc ^ (*pkt);
  1016.       crc = (crc >> 8) ^ ((k->crcta[(c & 0xF0) >> 4]) ^ (k->crctb[c & 0x0F]));
  1017.    }
  1018.    return (crc);
  1019. }
  1020.  
  1021. /*
  1022.  * S P K T -- Send a packet.  
  1023.  */
  1024. /*
  1025.  * Call with packet type, sequence number, data length, data, Kermit
  1026.  * struct. Returns: X_OK on success X_ERROR on i/o error 
  1027.  */
  1028. STATIC int
  1029. spkt (char typ, short seq, int len, UCHAR * data, struct k_data *k)
  1030. {
  1031.    int retc;
  1032.  
  1033.    unsigned int crc;            /* For building CRC */
  1034.    int i, j, lenpos;            /* Workers */
  1035.    UCHAR *s, *buf;
  1036.    int buflen;
  1037.    short slot;
  1038.    UCHAR tmp[100];              // for packets we don't want to resend
  1039.  
  1040.    debug (DB_CHR, "SPKT typ", 0, typ);
  1041.    debug (DB_LOG, "  seq", 0, seq);
  1042.    debug (DB_LOG, "  len", 0, len);
  1043.  
  1044.    if (seq < 0 || seq > 63)
  1045.       return (X_ERROR);
  1046.  
  1047.    if (len < 0)
  1048.    {                            /* Calculate data length ourselves? */
  1049.       len = 0;
  1050.       s = data;
  1051.       while (*s++)
  1052.          len++;
  1053.       debug (DB_LOG, "SPKT calc len", 0, len);
  1054.    }
  1055.    if (typ == 'Y' || typ == 'N' || typ == 'E')
  1056.    {
  1057.       buf = tmp;
  1058.       buflen = sizeof (tmp);
  1059.    }
  1060.    else
  1061.    {
  1062.       debug (DB_LOG, "SPKT k->s_seq", 0, k->s_seq);
  1063.       debug (DB_LOG, "SPKT k->s_pw[seq]", 0, k->s_pw[seq]);
  1064.  
  1065.       get_sslot (k, &slot);     // get a new send slot
  1066.       if (slot < 0 || slot >= k->wslots)
  1067.          return (X_ERROR);
  1068.       k->s_pw[k->s_seq] = slot;
  1069.  
  1070.       // save these for use in resend()
  1071.       k->opktinfo[slot].typ = typ;
  1072.       k->opktinfo[slot].seq = seq;
  1073.       k->opktinfo[slot].len = len;
  1074.  
  1075.       buf = k->opktinfo[slot].buf;
  1076.       buflen = P_BUFLEN;
  1077.    }
  1078.  
  1079.    i = 0;                       /* Packet buffer position */
  1080.    buf[i++] = k->s_soh;         /* SOH */
  1081.    lenpos = i++;                /* Remember this place */
  1082.    buf[i++] = tochar (seq);     /* Sequence number */
  1083.    buf[i++] = typ;              /* Packet type */
  1084.     if(k->bcta3)
  1085.         k->bct = 3;
  1086.    j = len + k->bct;
  1087.    if ((len + k->bct + 2) > 94)
  1088.    {                            /* If long packet */
  1089.       buf[lenpos] = tochar (0); /* Put blank in LEN field */
  1090.       buf[i++] = tochar (j / 95);       /* Make extended header: Big part */
  1091.       buf[i++] = tochar (j % 95);       /* and small part of length. */
  1092.       buf[i] = NUL;             /* Terminate for header checksum */
  1093.       buf[i++] = tochar (chk1 (&buf[lenpos], k));       /* Insert header checksum */
  1094.    }
  1095.    else
  1096.    {                            /* Short packet */
  1097.       buf[lenpos] = tochar (j + 2);     /* Single-byte length in LEN field */
  1098.    }
  1099.    if (data)                    /* Copy data, if any */
  1100.       for (; len--; i++)
  1101.       {
  1102.          if (i < 0 || i >= buflen)
  1103.          {
  1104.             debug (DB_LOG, "confused copy data i", 0, i);
  1105.             epkt ("EKSW spkt confused", k);
  1106.             exit (1);
  1107.             return (X_ERROR);
  1108.          }
  1109.          buf[i] = *data++;
  1110.       }
  1111.    buf[i] = '\0';
  1112.  
  1113.    switch (k->bct)
  1114.    {                            /* Add block check */
  1115.    case 1:                     /* 1 = 6-bit chksum */
  1116.       buf[i++] = tochar (chk1 (&buf[lenpos], k));
  1117.       break;
  1118.    case 2:                     /* 2 = 12-bit chksum */
  1119.       j = chk2 (&buf[lenpos], k);
  1120. #if 0
  1121.       buf[i++] = (unsigned) tochar ((j >> 6) & 077);
  1122.       buf[i++] = (unsigned) tochar (j & 077);
  1123. #else
  1124.       // HiTech's XAC compiler silently ruins the above code.
  1125.       // An intermediate variable provides a work-around.
  1126.       // 2004-06-29 -- JHD
  1127.       {
  1128.          USHORT jj;
  1129.          jj = (j >> 6) & 077;
  1130.          buf[i++] = tochar (jj);
  1131.          jj = j & 077;
  1132.          buf[i++] = tochar (jj);
  1133.       }
  1134. #endif
  1135.  
  1136.       break;
  1137.    case 3:                     /* 3 = 16-bit CRC */
  1138.       crc = chk3 (&buf[lenpos], k);
  1139. #if 0
  1140.       buf[i++] = (unsigned) tochar (((crc & 0170000)) >> 12);
  1141.       buf[i++] = (unsigned) tochar ((crc >> 6) & 077);
  1142.       buf[i++] = (unsigned) tochar (crc & 077);
  1143. #else
  1144.       // HiTech's XAC compiler silently ruins the above code.
  1145.       // An intermediate variable provides a work-around.
  1146.       // 2004-06-29 -- JHD
  1147.       {
  1148.          USHORT jj;
  1149.          jj = (crc >> 12) & 0x0f;
  1150.          buf[i++] = tochar (jj);
  1151.          jj = (crc >> 6) & 0x3f;
  1152.          buf[i++] = tochar (jj);
  1153.          jj = crc & 0x3f;
  1154.          buf[i++] = tochar (jj);
  1155.       }
  1156. #endif
  1157.       break;
  1158.    }
  1159.  
  1160.    buf[i++] = k->s_eom;         /* Packet terminator */
  1161.    buf[i] = '\0';               /* String terminator */
  1162.    // packet ready to go
  1163.  
  1164.    if (i < 0 || i >= buflen)
  1165.    {
  1166.       debug (DB_LOG, "SPKT i", 0, i);
  1167.       debug (DB_LOG, "SPKT buflen", 0, buflen);
  1168.       return (X_ERROR);
  1169.    }
  1170.  
  1171.    k->s_seq = seq;              /* Remember sequence number */
  1172.    k->opktlen = i;              /* Remember length for retransmit */
  1173.  
  1174.    if (typ != 'Y' && typ != 'N' && typ != 'E')
  1175.    {
  1176.       k->opktinfo[slot].len = i;        /* Remember length for retransmit */
  1177.    }
  1178.  
  1179.    debug (DB_LOG, "SPKT opktlen", 0, k->opktlen);
  1180.  
  1181. #ifdef DEBUG
  1182.    /*
  1183.     * CORRUPT THE PACKET SENT BUT NOT THE ONE WE SAVE 
  1184.     */
  1185.    if (xerror ())
  1186.    {
  1187.       UCHAR p[P_BUFLEN];
  1188.       int i;
  1189.       for (i = 0; i < buflen - 8; i++)
  1190.          if (!(p[i] = buf[i]))
  1191.             break;
  1192.       if (xerror ())
  1193.       {
  1194.          p[i - 2] = 'X';
  1195.          debug (DB_PKT, "XPKT", (char *) &p[1], 0);
  1196.       }
  1197.       else if (xerror ())
  1198.       {
  1199.          p[k->opktlen - 1] = 'N';
  1200.          debug (DB_PKT, "NPKT", (char *) &p[1], 0);
  1201.       }
  1202.       else
  1203.       {
  1204.          p[0] = 'A';
  1205.          debug (DB_PKT, "APKT", (char *) &p[1], 0);
  1206.       }
  1207.       return ((*(k->txd)) (k, p, k->opktlen));  /* Send it. */
  1208.    }
  1209. #endif /* DEBUG */
  1210.  
  1211.    debug (DB_PKT, "SPKT", &buf[1], k->opktlen);
  1212.    retc = ((*(k->txd)) (k, buf, k->opktlen));   /* Send buf == whole packet */
  1213.    debug (DB_LOG, "SPKT txd retc", 0, retc);
  1214.  
  1215.    return (retc);
  1216. }
  1217.  
  1218. /*
  1219.  * N A K -- Send a NAK (negative acknowledgement) 
  1220.  */
  1221.  
  1222. STATIC int
  1223. nak (struct k_data *k, short seq, short slot)
  1224. {
  1225.    int rc;
  1226.    debug (DB_LOG, "NAK seq", 0, seq);
  1227.    debug (DB_LOG, "  slot", 0, slot);
  1228. // k->anseq = seq;
  1229.    rc = spkt ('N', seq, 0, (UCHAR *) 0, k);
  1230.    if (slot >= 0 && slot < 64 && k->ipktinfo[slot].rtr++ > k->retry)
  1231.    {
  1232.       debug (DB_MSG, "X_ERROR returned from nak(): too many retries", 0, 0);
  1233.       debug (DB_MSG, "  seq", 0, seq);
  1234.       debug (DB_MSG, "  slot", 0, slot);
  1235.       rc = X_ERROR;
  1236.    }
  1237.    return (rc);
  1238. }
  1239.  
  1240. /*
  1241.  * A C K -- Send an ACK (positive acknowledgement) 
  1242.  */
  1243.  
  1244. STATIC int
  1245. ack (struct k_data *k, short seq, UCHAR * text)
  1246. {
  1247.    int len, rc;
  1248.    debug (DB_LOG, "ACK seq", 0, seq);
  1249. // k->anseq = (seq+1)&63;
  1250.    len = 0;
  1251.    if (text)
  1252.    {                            /* Get length of data */
  1253.       UCHAR *p;
  1254.       p = text;
  1255.       for (; *p++; len++);
  1256.    }
  1257.    debug (DB_LOG, "ACK len", 0, len);
  1258.    rc = spkt ('Y', seq, len, text, k);  /* Send the packet */
  1259.    debug (DB_LOG, "ACK spkt rc", 0, rc);
  1260.    if (rc == X_OK && seq == k->r_seq)   /* If OK */
  1261.       k->r_seq = (k->r_seq + 1) & 63;   /* bump the packet number */
  1262.    debug (DB_LOG, "ACK new k->r_seq", 0, k->r_seq);
  1263.    return (rc);
  1264. }
  1265.  
  1266. /*
  1267.  * S P A R -- Set parameters requested by other Kermit 
  1268.  */
  1269.  
  1270. STATIC void
  1271. spar (struct k_data *k, UCHAR * s, int datalen)
  1272. {
  1273.    int x;
  1274.    int y = 0;
  1275.  
  1276.    debug (DB_MSG, "SPAR", 0, 0);
  1277.  
  1278.    s--;                         /* Line up with field numbers. */
  1279.  
  1280.    if (datalen >= 1)            /* Max packet length to send */
  1281.       k->s_maxlen = xunchar (s[1]);
  1282.  
  1283.    if (datalen >= 2)            /* Timeout on inbound packets */
  1284.       k->r_timo = xunchar (s[2]);
  1285.  
  1286.    /*
  1287.     * No padding 
  1288.     */
  1289.  
  1290.    if (datalen >= 5)            /* Outbound Packet Terminator */
  1291.       k->s_eom = xunchar (s[5]);
  1292.  
  1293.    if (datalen >= 6)            /* Incoming control prefix */
  1294.       k->r_ctlq = s[6];
  1295.  
  1296.    if (datalen >= 7)
  1297.    {                            /* 8th bit prefix */
  1298.       k->ebq = s[7];
  1299.       if ((s[7] > 32 && s[7] < 63) || (s[7] > 95 && s[7] < 127))
  1300.       {
  1301.          if (!k->parity)        /* They want it */
  1302.             k->parity = 1;      /* Set parity to something nonzero */
  1303.          k->ebqflg = 1;
  1304.       }
  1305.       else if (s[7] == 'Y' && k->parity)
  1306.       {                         // they will do 8th bit prefixing if requested
  1307.          k->ebqflg = 1;
  1308.          k->ebq = '&';
  1309.       }
  1310.       else if (s[7] == 'N')
  1311.       {                         // they refuse to do 8th bit prefixing
  1312.          /*
  1313.           * WHAT? 
  1314.           */
  1315.       }
  1316.    }
  1317.    if (datalen >= 8)
  1318.    {                            /* Block check */
  1319.         k->bct = s[8] - '0';
  1320.         if ((k->bct < 1) || (k->bct > 3))
  1321.             k->bct = 1;
  1322.         if(k->bcta3)
  1323.             k->bct = 3;
  1324.    }
  1325.    if (datalen >= 9)
  1326.    {                            /* Repeat counts */
  1327.       if ((s[9] > 32 && s[9] < 63) || (s[9] > 95 && s[9] < 127))
  1328.       {
  1329.          k->rptq = s[9];
  1330.          k->rptflg = 1;
  1331.       }
  1332.    }
  1333.    if (datalen >= 10)
  1334.    {                            /* Capability bits */
  1335.       x = xunchar (s[10]);
  1336.  
  1337.       if (!(x & CAP_LP))
  1338.          k->capas &= ~CAP_LP;
  1339.  
  1340.       if (!(x & CAP_SW))
  1341.          k->capas &= ~CAP_SW;
  1342.  
  1343.       if (!(x & CAP_AT))
  1344.          k->capas &= ~CAP_AT;
  1345.  
  1346. #ifdef F_RS                     /* Recovery */
  1347.       if (!(x & CAP_RS))
  1348. #endif /* F_RS */
  1349.          k->capas &= ~CAP_RS;
  1350.  
  1351. #ifdef F_LS                     /* Locking shifts */
  1352.       if (!(x & CAP_LS))
  1353. #endif /* F_LS */
  1354.          k->capas &= ~CAP_LS;
  1355.  
  1356.       /* In case other Kermit sends addt'l capas fields ...  */
  1357.  
  1358.       for (y = 10; (xunchar (s[y]) & 1) && (datalen >= y); y++);
  1359.    }
  1360.    if (k->capas & CAP_LP)
  1361.    {
  1362.       if (datalen > y + 1)
  1363.       {
  1364.          x = xunchar (s[y + 2]) * 95 + xunchar (s[y + 3]);
  1365.          k->s_maxlen = (x > P_PKTLEN) ? P_PKTLEN : x;
  1366.          if (k->s_maxlen < 10)
  1367.             k->s_maxlen = 60;
  1368.       }
  1369.    }
  1370.  
  1371.    debug (DB_LOG, "  s_maxlen", 0, k->s_maxlen);
  1372.  
  1373.    if (k->capas & CAP_SW)
  1374.    {
  1375.       if (datalen > y)
  1376.       {
  1377.          x = xunchar (s[y + 1]);
  1378.          k->wslots = (x > k->wslots_max) ? k->wslots_max : x;
  1379.          if (k->wslots < 1)     /* Watch out for bad negotiation */
  1380.             k->wslots = 1;
  1381.          if (k->wslots > 1)
  1382.             if (k->wslots > k->retry)   /* Retry limit must be greater */
  1383.                k->retry = k->wslots + 1;        /* than window size. */
  1384.       }
  1385.    }
  1386.  
  1387.    debug (DB_LOG, "  k->capas & CAP_LP", 0, k->capas & CAP_LP);
  1388.    debug (DB_LOG, "  k->capas & CAP_SW", 0, k->capas & CAP_SW);
  1389.    debug (DB_LOG, "  k->capas & CAP_AT", 0, k->capas & CAP_AT);
  1390.    debug (DB_LOG, "  k->capas & CAP_RS", 0, k->capas & CAP_RS);
  1391.    debug (DB_LOG, "  k->capas & CAP_LS", 0, k->capas & CAP_LS);
  1392.    debug (DB_CHR, "  k->ebq           ", 0, k->ebq);
  1393.    debug (DB_LOG, "  k->ebqflg        ", 0, k->ebqflg);
  1394.    debug (DB_LOG, "  k->parity        ", 0, k->parity);
  1395.    debug (DB_LOG, "  k->s_eom         ", 0, k->s_eom);
  1396.    debug (DB_LOG, "  k->r_timo        ", 0, k->r_timo);
  1397.    debug (DB_LOG, "  k->s_timo        ", 0, k->s_timo);
  1398.    debug (DB_CHR, "  k->r_ctlq        ", 0, k->r_ctlq);
  1399.    debug (DB_CHR, "  k->s_ctlq        ", 0, k->s_ctlq);
  1400.    debug (DB_CHR, "  k->rptq          ", 0, k->rptq);
  1401.    debug (DB_LOG, "  k->rptflg        ", 0, k->rptflg);
  1402.    debug (DB_LOG, "  k->bct           ", 0, k->bct);
  1403.    debug (DB_LOG, "  k->bcta3         ", 0, k->bcta3);
  1404.    debug (DB_LOG, "  k->r_maxlen      ", 0, k->r_maxlen);
  1405.    debug (DB_LOG, "  k->s_maxlen      ", 0, k->s_maxlen);
  1406.    debug (DB_LOG, "  k->wslots        ", 0, k->wslots);
  1407.    debug (DB_LOG, "  k->binary        ", 0, k->binary);
  1408.    debug (DB_LOG, "  k->retry         ", 0, k->retry);
  1409. }
  1410.  
  1411. /*
  1412.  * R P A R -- Send my parameters to other Kermit 
  1413.  */
  1414.  
  1415. STATIC int
  1416. rpar (struct k_data *k, char type)
  1417. {
  1418.    UCHAR *d;
  1419.    int rc, len;
  1420.    short bctsv;
  1421.    UCHAR *buf;
  1422.    short s_slot;
  1423.  
  1424.    debug (DB_LOG, "RPAR capas", 0, k->capas);
  1425.    debug (DB_LOG, "  wslots", 0, k->wslots);
  1426.  
  1427.    d = k->ack_s;                /* Where to put it */
  1428.    d[0] = tochar (94);          /* Maximum short-packet length */
  1429.    d[1] = tochar (k->s_timo);   /* When I want to be timed out */
  1430.    d[2] = tochar (0);           /* How much padding I need */
  1431.    d[3] = ctl (0);              /* Padding character I want */
  1432.    d[4] = tochar (k->r_eom);    /* End-of message character I want */
  1433.    d[5] = k->s_ctlq;            /* Control prefix I send */
  1434.    if ((k->ebq == 'Y') && (k->parity))  /* 8th-bit prefix */
  1435.       d[6] = k->ebq = '&';      /* I need to request it */
  1436.    else                         /* else just agree with other Kermit */
  1437.       d[6] = k->ebq;
  1438.     if(k->bcta3)
  1439.         d[7] = '3';
  1440.     else
  1441.         d[7] = k->bct + '0';         /* Block check type */
  1442.    d[8] = k->rptq;              /* Repeat prefix */
  1443.    d[9] = tochar (k->capas);    /* Capability bits */
  1444.    d[10] = tochar (k->wslots);  /* Window size */
  1445.  
  1446.    d[11] = tochar (k->r_maxlen / 95);   /* Long packet size, big part */
  1447.    d[12] = tochar (k->r_maxlen % 95);   /* Long packet size, little part */
  1448.    d[13] = '\0';                /* Terminate the init string */
  1449.    len = 13;
  1450.  
  1451.     if(k->bcta3){
  1452.         bctsv = 3;
  1453.         k->bct = 3;
  1454.     }
  1455.     else{
  1456.         bctsv = k->bct;
  1457.         k->bct = 1;                  /* Always use block check type 1 */
  1458.     }
  1459.    switch (type)
  1460.    {
  1461.    case 'Y':                   /* This is an ACK for packet 0 */
  1462.       rc = ack (k, 0, d);
  1463.       break;
  1464.    case 'S':                   /* It's an S packet */
  1465.       buf = get_sslot (k, &s_slot);     // get a new send slot
  1466.       k->s_pw[k->s_seq] = s_slot;
  1467.       debug (DB_LOG, "sending S pkt s_seq", 0, k->s_seq);
  1468.       debug (DB_LOG, "  s_slot", 0, s_slot);
  1469.       rc = spkt ('S', 0, len, d, k);
  1470.       break;
  1471.    default:
  1472.       rc = -1;
  1473.    }
  1474.    k->bct = bctsv;
  1475.    return (rc);                 /* Pass along return code. */
  1476. }
  1477.  
  1478. /*
  1479.  * D E C O D E -- Decode data field of Kermit packet - binary mode only 
  1480.  */
  1481. /*
  1482. Call with:
  1483.     k = kermit data structure
  1484.     r = kermit response structure
  1485.     f = function code:
  1486.         0: decode filename
  1487.         1: decode file data
  1488.     inbuf = pointer to packet data to be decoded
  1489.     Returns: X_OK on success X_ERROR if output function fails 
  1490.  */
  1491. STATIC int
  1492. decode (struct k_data *k, struct k_response *r, short f, UCHAR * inbuf,
  1493.         int rslot)
  1494. {
  1495.  
  1496.    register unsigned int a, a7; /* Current character */
  1497.    unsigned int b8;             /* 8th bit */
  1498.    int rpt;                     /* Repeat count */
  1499.    int rc;                      /* Return code */
  1500.    UCHAR *ucp = 0;
  1501.    int i;
  1502.    int nobuf;
  1503.    USHORT crc;
  1504.  
  1505.    rc = X_OK;
  1506.    rpt = 0;                     /* Initialize repeat count. */
  1507.    if (f == 0)                  /* Output function... */
  1508.       ucp = r->filename;
  1509.  
  1510.    debug (DB_LOG, "DECODE rslot", 0, rslot);
  1511.    if (rslot >= 0)
  1512.    {
  1513.       k->anseq = k->ipktinfo[rslot].seq;
  1514.       debug (DB_LOG, "DECODE seq", 0, k->ipktinfo[rslot].seq);
  1515.       debug (DB_LOG, "DECODE len", 0, k->ipktinfo[rslot].len);
  1516.       debug (DB_LOG, "DECODE crc", 0, k->ipktinfo[rslot].crc);
  1517.       debug (DB_HEX, "IHEX", inbuf, k->ipktinfo[rslot].len);
  1518.  
  1519.       for (i = 0; i < k->ipktinfo[rslot].len; i++)
  1520.          if (inbuf[i] == 0)
  1521.             break;
  1522.  
  1523.       if (i != k->ipktinfo[rslot].len)
  1524.       {
  1525.          debug (DB_LOG, "DECODE error i", 0, i);
  1526. #ifdef DEBUG
  1527.          show_rslots (k);
  1528. #endif
  1529.          epkt ("EKSW decode len bad", k);
  1530.          exit (1);
  1531.          return (X_ERROR);
  1532.       }
  1533.  
  1534.       crc = chk3 (inbuf, k);
  1535.       if (crc != k->ipktinfo[rslot].crc)
  1536.       {
  1537.          debug (DB_LOG, "DECODE error crc", 0, crc);
  1538. #ifdef DEBUG
  1539.          show_rslots (k);
  1540. #endif
  1541.          epkt ("EKSW decode crc bad", k);
  1542.          exit (1);
  1543.          return (X_ERROR);
  1544.       }
  1545.    }
  1546.  
  1547.    nobuf = 0;
  1548.    while ((a = *inbuf++ & 0xFF) != '\0')
  1549.    {                            /* Character loop */
  1550.       if (k->rptflg && a == k->rptq)
  1551.       {                         /* Got a repeat prefix? */
  1552.          rpt = xunchar (*inbuf++ & 0xFF);       /* Yes, get the repeat count, */
  1553.          a = *inbuf++ & 0xFF;   /* and get the prefixed character. */
  1554.       }
  1555.       b8 = 0;                   /* 8th-bit value */
  1556.       if (k->parity && (a == k->ebq))
  1557.       {                         /* Have 8th-bit prefix? */
  1558.          b8 = 0200;             /* Yes, flag the 8th bit */
  1559.          a = *inbuf++ & 0x7F;   /* and get the prefixed character. */
  1560.       }
  1561.       if (a == k->r_ctlq)
  1562.       {                         /* If control prefix, */
  1563.          a = *inbuf++ & 0xFF;   /* get its operand */
  1564.          a7 = a & 0x7F;         /* and its low 7 bits. */
  1565.          if ((a7 >= 0100 && a7 <= 0137) || a7 == '?')   /* Controllify */
  1566.             a = ctl (a);        /* if in control range. */
  1567.       }
  1568.       a |= b8;                  /* OR in the 8th bit */
  1569.  
  1570. //    debug(DB_LOG,"DECODE rpt",0,rpt);
  1571.       if (rpt == 0)
  1572.          rpt = 1;               /* If no repeats, then one */
  1573.  
  1574.       for (; rpt > 0; rpt--)
  1575.       {                         /* Output the char 'rpt' times */
  1576.          if (f == 0)
  1577.          {
  1578.             *ucp++ = (UCHAR) a; /* to memory */
  1579.          }
  1580.          else
  1581.          {                      /* or to file */
  1582.             k->obuf[k->obufpos++] = (UCHAR) a;  /* Deposit the byte */
  1583.             nobuf++;
  1584.             if (k->obufpos == k->obuflen)
  1585.             {                   /* Buffer full? */
  1586.                rc = (*(k->writef)) (k, k->obuf, k->obuflen);    /* Dump it. */
  1587.                debug (DB_LOG, "DECODE writef rc", 0, rc);
  1588.                r->sofar += k->obuflen;
  1589.                r->sofar_rumor += k->obuflen;
  1590.                if (rc != X_OK)
  1591.                   break;
  1592.                k->obufpos = 0;
  1593.             }
  1594.          }
  1595.       }
  1596.    }
  1597.  
  1598.    debug (DB_LOG, "DECODE nobuf", 0, nobuf);
  1599.    debug (DB_LOG, "DECODE obufpos", 0, k->obufpos);
  1600.  
  1601.    if (f == 0)                  /* If writing to memory */
  1602.       *ucp = '\0';              /* terminate the string */
  1603.  
  1604.    debug (DB_LOG, "DECODE rc", 0, rc);
  1605.    return (rc);
  1606. }
  1607.  
  1608. STATIC ULONG                    /* Convert decimal string to number */
  1609. stringnum (UCHAR * s, struct k_data * k)
  1610. {
  1611.    long n;
  1612.    n = 0L;
  1613.    while (*s == SP)
  1614.       s++;
  1615.    while (*s >= '0' && *s <= '9')
  1616.       n = n * 10 + (*s++ - '0');
  1617.    return (n);
  1618. }
  1619.  
  1620. STATIC UCHAR *                  /* Convert number to string */
  1621. numstring (ULONG n, UCHAR * buf, int buflen, struct k_data * k)
  1622. {
  1623.    int i, x;
  1624.    buf[buflen - 1] = '\0';
  1625.    for (i = buflen - 2; i > 0; i--)
  1626.    {
  1627.       x = n % 10L;
  1628.       buf[i] = x + '0';
  1629.       n /= 10L;
  1630.       if (!n)
  1631.          break;
  1632.    }
  1633.    if (n)
  1634.    {
  1635.       return ((UCHAR *) 0);
  1636.    }
  1637.    if (i > 0)
  1638.    {
  1639.       UCHAR *p, *s;
  1640.       s = &buf[i];
  1641.       p = buf;
  1642.       while ((*p++ = *s++));
  1643.       *(p - 1) = '\0';
  1644.    }
  1645.    return ((UCHAR *) buf);
  1646. }
  1647.  
  1648. /*
  1649.  * G A T T R -- Read incoming attributes.
  1650.  * 
  1651.  * Returns: -1 if no transfer mode (text/binary) was announced. 0 if text
  1652.  * was announced. 1 if binary was announced. 
  1653.  */
  1654.  
  1655. #define SIZEBUFL 32             /* For number conversions */
  1656.  
  1657. STATIC int
  1658. gattr (struct k_data *k, UCHAR * s, struct k_response *r)
  1659. {
  1660.    long fsize = 0, fsizek = 0;  /* File size */
  1661.    UCHAR c;                     /* Workers */
  1662.    int aln, i, rc;
  1663.  
  1664.    UCHAR sizebuf[SIZEBUFL];
  1665.  
  1666.    rc = -1;
  1667.    while ((c = *s++))
  1668.    {                            /* Get attribute tag */
  1669.       aln = xunchar (*s++);     /* Length of attribute string */
  1670.       switch (c)
  1671.       {
  1672.       case '!':                /* File length in K */
  1673.       case '"':                /* File type */
  1674.          for (i = 0; (i < aln) && (i < SIZEBUFL); i++)  /* Copy it */
  1675.             sizebuf[i] = *s++;
  1676.          sizebuf[i] = '\0';     /* Terminate with null */
  1677.          if (i < aln)
  1678.             s += (aln - i);     /* If field was too long for buffer */
  1679.          if (c == '!')
  1680.          {                      /* Length */
  1681.             fsizek = stringnum (sizebuf, k);    /* Convert to number */
  1682.          }
  1683.          else
  1684.          {                      /* Type */
  1685.             if (sizebuf[0] == 'A')      /* Text */
  1686.                rc = 0;
  1687.             else if (sizebuf[0] == 'B') /* Binary */
  1688.                rc = 1;
  1689.             debug (DB_LOG, "GATTR rc", 0, rc);
  1690.             debug (DB_LOG, "GATTR sizebuf", sizebuf, 0);
  1691.          }
  1692.          break;
  1693.  
  1694.       case '#':                /* File creation date */
  1695.          for (i = 0; (i < aln) && (i < DATE_MAX); i++)
  1696.             r->filedate[i] = *s++;      /* save it to a string */
  1697.          if (i < aln)
  1698.             s += (aln - i);
  1699.          r->filedate[i] = '\0';
  1700.          break;
  1701.  
  1702.       case '1':                /* File length in bytes */
  1703.          for (i = 0; (i < aln) && (i < SIZEBUFL); i++)  /* Copy it */
  1704.             sizebuf[i] = *s++;
  1705.          sizebuf[i] = '\0';     /* Terminate with null */
  1706.          if (i < aln)
  1707.             s += (aln - i);
  1708.          fsize = stringnum (sizebuf, k);        /* Convert to number */
  1709.          break;
  1710.  
  1711.       default:                 /* Unknown attribute */
  1712.          s += aln;              /* Just skip past it */
  1713.          break;
  1714.       }
  1715.    }
  1716.    if (fsize > -1L)
  1717.    {                            /* Remember the file size */
  1718.       r->filesize = fsize;
  1719.    }
  1720.    else if (fsizek > -1L)
  1721.    {
  1722.       r->filesize = fsizek * 1024L;
  1723.    }
  1724.    debug (DB_LOG, "gattr r->filesize", 0, (r->filesize));
  1725.    debug (DB_LOG, "gattr r->filedate=", r->filedate, 0);
  1726.    return (rc);
  1727. }
  1728.  
  1729. #define ATTRLEN 48
  1730.  
  1731. STATIC int
  1732. sattr (struct k_data *k, struct k_response *r)
  1733. {                               /* Build and send A packet */
  1734.    // int i, x, aln;
  1735.    int i, x;
  1736.    short tmp;
  1737.    long filelength;
  1738.    UCHAR datebuf[DATE_MAX], *p;
  1739.    UCHAR *buf;
  1740.    short s_slot;
  1741.  
  1742.    debug (DB_PKT, "SATTR k->zincnt 0", 0, (k->zincnt));
  1743.  
  1744.    tmp = k->binary;
  1745.    filelength = (*(k->finfo))
  1746.       (k, k->filename, datebuf, DATE_MAX, &tmp, k->xfermode);
  1747.    k->binary = tmp;
  1748.  
  1749.    debug (DB_LOG, "  filename: ", k->filename, 0);
  1750.    debug (DB_LOG, "  filedate: ", datebuf, 0);
  1751.    debug (DB_LOG, "  filelength", 0, filelength);
  1752.    debug (DB_LOG, "  binary", 0, (k->binary));
  1753.  
  1754.    i = 0;
  1755.  
  1756.    k->xdata[i++] = '"';
  1757.    if (k->binary)
  1758.    {                            /* Binary */
  1759.       k->xdata[i++] = tochar (2);       /* Two characters */
  1760.       k->xdata[i++] = 'B';      /* B for Binary */
  1761.       k->xdata[i++] = '8';      /* 8-bit bytes (note assumption...) */
  1762.    }
  1763.    else
  1764.    {                            /* Text */
  1765.       k->xdata[i++] = tochar (3);       /* Three characters */
  1766.       k->xdata[i++] = 'A';      /* A = (extended) ASCII with CRLFs */
  1767.       k->xdata[i++] = 'M';      /* M for carriage return */
  1768.       k->xdata[i++] = 'J';      /* J for linefeed */
  1769.       k->xdata[i++] = '*';      /* Encoding */
  1770.       k->xdata[i++] = tochar (1);       /* Length of value is 1 */
  1771.       k->xdata[i++] = 'A';      /* A for ASCII */
  1772.    }
  1773.    if (filelength > -1L)
  1774.    {                            /* File length in bytes */
  1775.       UCHAR lenbuf[16];
  1776.       r->filesize = filelength;
  1777.       p = numstring (filelength, lenbuf, 16, k);
  1778.       if (p)
  1779.       {
  1780.          for (x = 0; p[x]; x++);        /* Get length of length string */
  1781.          if (i + x < ATTRLEN - 3)
  1782.          {                      /* Don't overflow buffer */
  1783.             k->xdata[i++] = '1';        /* Length-in-Bytes attribute */
  1784.             k->xdata[i++] = tochar (x);
  1785.             while (*p)
  1786.                k->xdata[i++] = *p++;
  1787.          }
  1788.       }
  1789.    }
  1790.    debug (DB_LOG, "SATTR datebuf: ", datebuf, 0);
  1791.  
  1792.    if (datebuf[0])
  1793.    {                            /* File modtime */
  1794.       p = datebuf;
  1795.       for (x = 0; p[x]; x++);   /* Length of modtime */
  1796.       if (i + x < ATTRLEN - 3)
  1797.       {                         /* If it will fit */
  1798.          k->xdata[i++] = '#';   /* Add modtime attribute */
  1799.          k->xdata[i++] = tochar (x);    /* Its length */
  1800.          while (*p)             /* And itself */
  1801.             k->xdata[i++] = *p++;
  1802.          /*
  1803.           * Also copy modtime to result struct 
  1804.           */
  1805.          for (x = 0; x < DATE_MAX - 1 && datebuf[x]; x++)
  1806.             r->filedate[x] = datebuf[x];
  1807.          r->filedate[x] = '\0';
  1808.       }
  1809.    }
  1810.    k->xdata[i++] = '@';         /* End of Attributes */
  1811.    k->xdata[i++] = ' ';
  1812.    k->xdata[i] = '\0';          /* Terminate attribute string */
  1813.    debug (DB_PKT, "SATTR k->xdata: ", k->xdata, 0);
  1814.    buf = get_sslot (k, &s_slot);        // get a new send slot
  1815.    k->s_pw[k->s_seq] = s_slot;
  1816.    debug (DB_LOG, "SATTR sending A pkt s_seq", 0, k->s_seq);
  1817.    debug (DB_LOG, "  s_slot", 0, s_slot);
  1818.    return (spkt ('A', k->s_seq, -1, k->xdata, k));
  1819. }
  1820.  
  1821. STATIC int
  1822. getpkt (struct k_data *k, struct k_response *r)
  1823. {                               /* Fill a packet from file */
  1824.    int i, next, rpt, maxlen;
  1825. // static int c;                /* PUT THIS IN STRUCT */
  1826.    int c = k->cgetpkt;
  1827.  
  1828.    debug (DB_LOG, "GETPKT k->s_first", 0, k->s_first);
  1829.    debug (DB_PKT, "  k->s_remain=", k->s_remain, 0);
  1830.  
  1831.     if(k->bcta3)
  1832.         k->bct = 3;
  1833.    maxlen = k->s_maxlen - k->bct - 3 - 6;   /* Maximum data length */
  1834.    if (k->s_first == 1)
  1835.    {                            /* If first time thru...  */
  1836.       k->s_first = 0;           /* don't do this next time, */
  1837.       k->s_remain[0] = '\0';    /* discard any old leftovers. */
  1838.       if (k->istring)
  1839.       {                         /* Get first byte. */
  1840.          c = *(k->istring)++;   /* Of memory string... */
  1841.          if (!c)
  1842.             c = -1;
  1843.       }
  1844.       else
  1845.       {                         /* or file... */
  1846. #ifdef USE_ZGETC_MACRO
  1847.          c = zgetc ();
  1848. #else
  1849.          c = zgetc (k);
  1850. #endif
  1851.       }
  1852.  
  1853.       k->cgetpkt = c;
  1854.  
  1855.       if (c < 0)
  1856.       {                         /* Watch out for empty file. */
  1857.          debug (DB_LOG, "GETPKT first c", 0, c);
  1858.          k->s_first = -1;
  1859.          return (k->size = 0);
  1860.       }
  1861.       // r->sofar++; // makes it too big
  1862.       if (k->state == S_DATA)
  1863.          r->sofar_rumor++;
  1864.       debug (DB_LOG, "GETPKT first state", 0, k->state);
  1865.       debug (DB_CHR, "  first c", 0, c);
  1866.    }
  1867.    else if (k->s_first == -1 && !k->s_remain[0])
  1868.    {                            /* EOF from last time? */
  1869.       return (k->size = 0);
  1870.    }
  1871.  
  1872.    // string copy s_remain to xdata
  1873.    for (k->size = 0;
  1874.         (k->xdata[k->size] = k->s_remain[k->size]) != '\0'; (k->size)++);
  1875.  
  1876.    // set s_remain to zero length
  1877.    k->s_remain[0] = '\0';
  1878.  
  1879.    if (k->s_first == -1)
  1880.       return (k->size);
  1881.  
  1882.    rpt = 0;                     /* Initialize repeat counter. */
  1883.    while (k->s_first > -1)
  1884.    {                            /* Until end of file or string... */
  1885.       if (k->istring)
  1886.       {
  1887.          next = *(k->istring)++;
  1888.          if (!next)
  1889.             next = -1;
  1890.       }
  1891.       else
  1892.       {
  1893. #ifdef USE_ZGETC_MACRO
  1894.          next = zgetc ();
  1895. #else
  1896.          next = zgetc (k);
  1897. #endif
  1898.       }
  1899.       if (next < 0)
  1900.       {                         /* If none, we're at EOF. */
  1901.          k->s_first = -1;
  1902.       }
  1903.       else
  1904.       {                         /* Otherwise */
  1905.          r->sofar_rumor++;      /* count this byte */
  1906.       }
  1907.       k->osize = k->size;       /* Remember current size. */
  1908.       encode (c, next, k);      /* Encode the character. */
  1909.       /*
  1910.        * k->xdata[k->size] = '\0'; 
  1911.        */
  1912.       c = next;                 /* Old next char is now current. */
  1913.  
  1914.       k->cgetpkt = c;
  1915.  
  1916.       if (k->size == maxlen)
  1917.       {                         /* Just at end, done. */
  1918.          debug (DB_LOG, "GETPKT size perfect c", 0, c);
  1919.          return (k->size);
  1920.       }
  1921.  
  1922.       if (k->size > maxlen)
  1923.       {                         /* Past end, must save some. */
  1924.          for (i = 0; (k->s_remain[i] = k->xdata[(k->osize) + i]) != '\0';
  1925.               i++);
  1926.          debug (DB_LOG, "GETPKT size past end i", 0, i);
  1927.          k->size = k->osize;
  1928.          k->xdata[k->size] = '\0';
  1929.          return (k->size);      /* Return size. */
  1930.       }
  1931.    }
  1932.    if (k->size > maxlen)
  1933.    {
  1934.       debug (DB_LOG, "GETPKT error confused: from getpkt() k->size",
  1935.              0, k->size);
  1936.       epkt ("EKSW getpkt confused", k);
  1937.       exit (1);
  1938.       return (X_ERROR);
  1939.    }
  1940.    return (k->size);            /* EOF, return size. */
  1941. }
  1942.  
  1943. STATIC int
  1944. sdata (struct k_data *k, struct k_response *r)
  1945. {                               /* Send a data packet */
  1946.    int len, rc;
  1947.    if (k->cancel)
  1948.    {                            /* Interrupted */
  1949.       debug (DB_LOG, "SDATA interrupted k->cancel", 0, (k->cancel));
  1950.       return (0);
  1951.    }
  1952.    len = getpkt (k, r);         /* Fill data field from input file */
  1953.    debug (DB_LOG, "SDATA getpkt len", 0, len);
  1954.    if (len < 1)
  1955.    {
  1956.       debug (DB_LOG, "SDATA getpkt got eof s_seq", 0, k->s_seq);
  1957.       return (0);
  1958.    }
  1959.    debug (DB_LOG, "SDATA sending D pkt s_seq", 0, k->s_seq);
  1960.  
  1961.    rc = spkt ('D', k->s_seq, len, k->xdata, k); /* Send the packet */
  1962.  
  1963.    debug (DB_LOG, "SDATA spkt rc", 0, rc);
  1964.    return ((rc == X_ERROR) ? rc : len);
  1965. }
  1966.  
  1967. /*
  1968.  * E P K T -- Send a (fatal) Error packet with the given message 
  1969.  */
  1970.  
  1971. STATIC void
  1972. epkt (char *msg, struct k_data *k)
  1973. {
  1974.    int bctsv;
  1975.  
  1976.     if(k->bcta3){
  1977.         bctsv = 3;
  1978.         k->bct = 3;
  1979.     }
  1980.     else{
  1981.         bctsv = k->bct;
  1982.         k->bct = 1;
  1983.     }
  1984.    debug (DB_LOG, "EPKT msg:", msg, 0);
  1985.    spkt ('E', 0, -1, (UCHAR *) msg, k);
  1986.    k->bct = bctsv;
  1987. }
  1988.  
  1989. STATIC int                      /* Fill a packet from string s. */
  1990. encstr (UCHAR * s, struct k_data *k, struct k_response *r)
  1991. {
  1992.    k->s_first = 1;              /* Start lookahead. */
  1993.    k->istring = s;              /* Set input string pointer */
  1994.    getpkt (k, r);               /* Fill a packet */
  1995.    k->istring = (UCHAR *) 0;    /* Reset input string pointer */
  1996.    k->s_first = 1;              /* "Rewind" */
  1997.    return (k->size);            /* Return data field length */
  1998. }
  1999.  
  2000. /*
  2001.  * Decode packet data into a string 
  2002.  */
  2003.  
  2004. #if 0                           // never used
  2005. STATIC void
  2006. decstr (UCHAR * s, struct k_data *k, struct k_response *r)
  2007. {
  2008.    k->ostring = s;              /* Set output string pointer */
  2009.    (void) decode (k, r, 0, s, -1);
  2010.    *(k->ostring) = '\0';        /* Terminate with null */
  2011.    k->ostring = (UCHAR *) 0;    /* Reset output string pointer */
  2012. }
  2013. #endif
  2014.  
  2015. STATIC void
  2016. encode (int a, int next, struct k_data *k)
  2017. {                               /* Encode character into packet == k->xdata */
  2018.    int a7, b8, maxlen;
  2019.  
  2020.    maxlen = k->s_maxlen - 4;
  2021.    if (k->rptflg)
  2022.    {                            /* Doing run-length encoding? */
  2023.       if (a == next)
  2024.       {                         /* Yes, got a run? */
  2025.          if (++(k->s_rpt) < 94)
  2026.          {                      /* Yes, count. */
  2027.             return;
  2028.          }
  2029.          else if (k->s_rpt == 94)
  2030.          {                      /* If at maximum */
  2031.             k->xdata[(k->size)++] = k->rptq;    /* Emit prefix, */
  2032.             k->xdata[(k->size)++] = tochar (k->s_rpt);  /* and count, */
  2033.             k->s_rpt = 0;       /* and reset counter. */
  2034.          }
  2035.       }
  2036.       else if (k->s_rpt == 1)
  2037.       {                         /* Run broken, only two? */
  2038.          k->s_rpt = 0;          /* Yes, do the character twice */
  2039.          encode (a, -1, k);     /* by calling self recursively. */
  2040.          if (k->size <= maxlen) /* Watch boundary. */
  2041.             k->osize = k->size;
  2042.          k->s_rpt = 0;          /* Call self second time. */
  2043.          encode (a, -1, k);
  2044.          return;
  2045.       }
  2046.       else if (k->s_rpt > 1)
  2047.       {                         /* Run broken, more than two? */
  2048.          k->xdata[(k->size)++] = k->rptq;       /* Yes, emit prefix and count */
  2049.          k->xdata[(k->size)++] = tochar (++(k->s_rpt));
  2050.          k->s_rpt = 0;          /* and reset counter. */
  2051.       }
  2052.    }
  2053.    a7 = a & 127;                /* Get low 7 bits of character */
  2054.    b8 = a & 128;                /* And "parity" bit */
  2055.  
  2056.    if (k->ebqflg && b8)
  2057.    {                            /* If doing 8th bit prefixing */
  2058.       k->xdata[(k->size)++] = k->ebq;   /* and 8th bit on, insert prefix */
  2059.       a = a7;                   /* and clear the 8th bit. */
  2060.    }
  2061.  
  2062. // if (a7 < 32 || a7 == 127)    /* If in control range -- conservative */
  2063. // if (a7==0 || a7==1 || a7==13)  // 2004-07-04 -- JHD -- need 127 for telnet
  2064.     // this is a bit more conservative than C-Kermit "set prefixing minimal" 
  2065.    if (a7 == 0 || a7 == 1 || a7==3 || a7==4 || a7==10 ||
  2066.         a7 == 13 || a7==21 || a7 == 127) 
  2067.    {
  2068.       k->xdata[(k->size)++] = k->s_ctlq;        /* insert control prefix */
  2069.       a = ctl (a);              /* and make character printable. */
  2070.    }
  2071.    else if (a7 == k->s_ctlq)    /* If data is control prefix, */
  2072.       k->xdata[(k->size)++] = k->s_ctlq;        /* prefix it. */
  2073.    else if (k->ebqflg && a7 == k->ebq)  /* If doing 8th-bit prefixing, */
  2074.       k->xdata[(k->size)++] = k->s_ctlq;        /* ditto for 8th-bit prefix. */
  2075.    else if (k->rptflg && a7 == k->rptq) /* If doing run-length encoding, */
  2076.       k->xdata[(k->size)++] = k->s_ctlq;        /* ditto for repeat prefix. */
  2077.  
  2078.    k->xdata[(k->size)++] = a;   /* Finally, emit the character. */
  2079.    k->xdata[(k->size)] = '\0';  /* Terminate string with null. */
  2080.  
  2081.    if (k->size < 0 || k->size >= P_PKTLEN + 2)
  2082.    {
  2083.       debug (DB_LOG, "confused: from encode() k->size", 0, k->size);
  2084.       epkt ("EKSW encode confused", k);
  2085.       exit (1);
  2086.       return;
  2087.    }
  2088. }
  2089.  
  2090. STATIC short
  2091. nxtpkt (struct k_data *k)
  2092. {                               /* Get next packet to send */
  2093.    k->s_seq = (k->s_seq + 1) & 63;      /* Next sequence number */
  2094.    k->s_cnt++;
  2095.    k->xdata = k->xdatabuf;
  2096.    return (k->s_seq);
  2097. }
  2098.  
  2099. STATIC int
  2100. resend (struct k_data *k, short seq)
  2101. {
  2102.    UCHAR *buf;
  2103.    int ret;
  2104.    short slot;
  2105.  
  2106.    debug (DB_LOG, "RESEND seq", 0, seq);
  2107.  
  2108.    if (seq < 0)
  2109.       seq = earliest_sseq (k);
  2110.    if (seq < 0)
  2111.    {
  2112.       debug (DB_LOG, "RESEND failed: no earliest seq", 0, seq);
  2113. #ifdef DEBUG
  2114.       show_sslots (k);
  2115. #endif
  2116.       return (X_OK);
  2117.    }
  2118.    slot = k->s_pw[seq];
  2119.    if (slot < 0)
  2120.    {
  2121.       debug (DB_LOG, "RESEND failed: no slot for seq", 0, seq);
  2122. #ifdef DEBUG
  2123.       show_sslots (k);
  2124. #endif
  2125.       seq = earliest_sseq (k);
  2126.       if (seq < 0)
  2127.       {
  2128.          debug (DB_LOG, "RESEND failed: still no earliest seq", 0, seq);
  2129. #ifdef DEBUG
  2130.          show_sslots (k);
  2131. #endif
  2132.          return (X_OK);
  2133.       }
  2134.       slot = k->s_pw[seq];
  2135.       debug (DB_LOG, "RESEND sending earliest seq", 0, seq);
  2136.       debug (DB_LOG, "  slot", 0, slot);
  2137.    }
  2138.    if (slot < 0)
  2139.    {
  2140.       debug (DB_LOG, "RESEND failed: still bad slot", 0, slot);
  2141. #ifdef DEBUG
  2142.       show_sslots (k);
  2143. #endif
  2144.       return (X_OK);
  2145.    }
  2146.  
  2147.    debug (DB_MSG, "RESEND opktinfo:", 0, 0);
  2148.    debug (DB_LOG, "  seq", 0, k->opktinfo[slot].seq);
  2149.    debug (DB_CHR, "  typ", 0, k->opktinfo[slot].typ);
  2150.    debug (DB_LOG, "  len", 0, k->opktinfo[slot].len);
  2151.    debug (DB_LOG, "  rtr", 0, k->opktinfo[slot].rtr);
  2152.    debug (DB_LOG, "  crc", 0, k->opktinfo[slot].crc);
  2153.    debug (DB_LOG, "  flg", 0, k->opktinfo[slot].flg);
  2154.  
  2155.    k->opktlen = k->opktinfo[slot].len;
  2156.  
  2157.    if (k->opktlen < 0 || k->opktlen >= P_BUFLEN)
  2158.    {
  2159.       debug (DB_LOG, "RESEND error opktlen", 0, k->opktlen);
  2160.       return (X_ERROR);
  2161.    }
  2162.  
  2163.    // what about .seq, .typ, .rtr, .flg ?
  2164.  
  2165.    if (!k->opktlen)             /* Nothing to resend */
  2166.       return (X_OK);
  2167.  
  2168.    buf = k->opktinfo[slot].buf;
  2169.  
  2170.    k->opktinfo[slot].rtr++;
  2171.    if (k->opktinfo[slot].rtr > k->retry)
  2172.    {
  2173.       debug (DB_LOG, "RESEND error retries", 0, k->opktinfo[slot].rtr);
  2174.       return (X_ERROR);
  2175.    }
  2176.  
  2177.    debug (DB_PKT, ">PKT", &buf[1], k->opktlen);
  2178.    ret = ((*(k->txd)) (k, buf, k->opktlen));
  2179.    debug (DB_LOG, "RESEND txd ret", 0, ret);
  2180.    return (ret);
  2181. }
  2182.  
  2183. int                             /* The kermit() function */
  2184. kermit (short fc,               /* Function code */
  2185.         struct k_data *k,       /* The control struct */
  2186.         int len,                /* Length of packet in buf */
  2187.         char *msg,              /* Message for error packet */
  2188.         struct k_response *r)   /* Response struct */
  2189. {
  2190.    int did_a_pkt = 0;
  2191.    UCHAR *buf = 0;
  2192.    short s_slot;
  2193.  
  2194.    // int i, j, rc; /* Workers */
  2195.    int i, rc;                   /* Workers */
  2196.    int datalen = 0;             /* Length of packet data field */
  2197.    // int bctu; /* Block check type for this packet */
  2198.    UCHAR *pdf = 0;              /* Pointer to packet data field */
  2199.    UCHAR *qdf = 0;              /* Pointer to data to be checked */
  2200.    UCHAR *s = 0;                /* Worker string pointer */
  2201.    // UCHAR c, t; /* Worker chars */
  2202.    UCHAR rtyp = 0;              /* Worker chars */
  2203.    UCHAR c;                     /* Worker chars */
  2204.    UCHAR pbc[4];                /* Copy of packet block check */
  2205.    short rseq = 0;              // actual received packet number
  2206.    short rlen = 0;
  2207.    short chklen;                /* Length of packet block check */
  2208.    unsigned int crc;            /* 16-bit CRC */
  2209.    int ok;
  2210.  
  2211.    /* Mark each entry: */
  2212.    debug (DB_LOG, "KERMIT ---------------------- version", VERSION, 0);
  2213.    debug (DB_LOG, "  fc", 0, fc);
  2214.    debug (DB_LOG, "  state", 0, k->state);
  2215.    debug (DB_LOG, "  zincnt", 0, (k->zincnt));
  2216.    debug (DB_LOG, "  k->wslots", 0, k->wslots);
  2217.  
  2218.    test_rslots (k);
  2219.  
  2220. #ifdef DEBUG
  2221.    show_sslots (k);
  2222. #endif
  2223.  
  2224.    if (fc == K_INIT)
  2225.    {                            /* Initialize packet buffers etc */
  2226.  
  2227.       k->version = (UCHAR *) VERSION;   /* Version of this module */
  2228.       r->filename[0] = '\0';    /* No filename yet. */
  2229.       r->filedate[0] = '\0';    /* No filedate yet. */
  2230.       r->filesize = 0L;         /* No filesize yet. */
  2231.       r->sofar = 0L;            /* No bytes transferred yet */
  2232.       r->sofar_rumor = 0L;      /* No bytes transferred yet */
  2233.  
  2234.  
  2235.       for (i = 0; i < P_WSLOTS; i++)
  2236.       {                         /* Packet info for each window slot */
  2237.          free_rslot (k, i);
  2238.          free_sslot (k, i);
  2239.       }
  2240.       for (i = 0; i < 64; i++)
  2241.       {                         /* Packet finder array */
  2242.          k->r_pw[i] = -1;       /* initialized to "no packets yet" */
  2243.          k->s_pw[i] = -1;       /* initialized to "no packets yet" */
  2244.       }
  2245.  
  2246. /* Initialize the k_data structure */
  2247.  
  2248.       k->sw_full = 0;
  2249.       k->do_rxd = 1;
  2250.       k->s_cnt = 0;
  2251.  
  2252.       for (i = 0; i < 6; i++)
  2253.          k->s_remain[i] = '\0';
  2254.  
  2255.       k->state = R_WAIT;        /* Beginning protocol state */
  2256.       r->rstatus = R_WAIT;
  2257.       k->what = W_RECV;         /* Default action */
  2258.       k->s_first = 1;           /* Beginning of file */
  2259.       k->r_soh = k->s_soh = SOH;        /* Packet start */
  2260.       k->r_eom = k->s_eom = CR; /* Packet end */
  2261.       k->s_seq = k->r_seq = 0;  /* Packet sequence number */
  2262.       k->s_cnt = 0;             /* Packet count */
  2263.       k->s_type = k->r_type = 0;        /* Packet type */
  2264.       k->r_timo = P_R_TIMO;     /* Timeout interval for me to use */
  2265.       k->s_timo = P_S_TIMO;     /* Timeout for other Kermit to use */
  2266.       k->r_maxlen = k->p_maxlen;        /* Maximum packet length */
  2267.       k->s_maxlen = k->p_maxlen;        /* Maximum packet length */
  2268.       k->wslots = k->wslots_max;        /* Current window slots */
  2269.       k->zincnt = 0;
  2270.       k->filename = (UCHAR *) 0;
  2271.  
  2272.       /* Parity must be filled in by the caller */
  2273.  
  2274.       k->retry = P_RETRY;       /* Retransmission limit */
  2275.       k->s_ctlq = k->r_ctlq = '#';      /* Control prefix */
  2276.       k->ebq = 'Y';             /* 8th-bit prefix negotiation */
  2277.       k->ebqflg = 0;            /* 8th-bit prefixing flag */
  2278.       k->rptq = '~';            /* Send repeat prefix */
  2279.       k->rptflg = 0;            /* Repeat counts negotiated */
  2280.       k->s_rpt = 0;             /* Current repeat count */
  2281.       k->capas = 0              /* Capabilities */
  2282.          | CAP_LP               /* Long packets */
  2283.          | CAP_SW               /* Sliding windows */
  2284.          | CAP_AT               /* Attribute packets */
  2285.          ;
  2286.  
  2287.       for (i = 0; i < P_WSLOTS; i++)
  2288.       {
  2289.          k->ipktinfo[i].buf = k->ipktbufs + i * P_BUFLEN;
  2290.          k->ipktinfo[i].buf[0] = '\0';
  2291.          k->ipktinfo[i].len = 0;
  2292.          k->ipktinfo[i].seq = -1;
  2293.          k->ipktinfo[i].typ = SP;
  2294.          k->ipktinfo[i].dat = (UCHAR *) (0);
  2295.          k->ipktinfo[i].crc = 0xFFFF;
  2296.       }
  2297.  
  2298.       for (i = 0; i < P_WSLOTS; i++)
  2299.       {
  2300.          k->opktinfo[i].buf = k->opktbuf + i * P_BUFLEN;
  2301.          k->opktinfo[i].buf[0] = '\0';
  2302.          k->opktinfo[i].len = 0;
  2303.          k->opktinfo[i].seq = -1;
  2304.          k->opktinfo[i].typ = SP;
  2305.          k->opktinfo[i].dat = (UCHAR *) (0);
  2306.       }
  2307.  
  2308.       k->opktlen = 0;
  2309.  
  2310.       /* This is the only way to initialize these tables -- no static data. */
  2311.  
  2312.       k->crcta[0] = 0;          /* CRC generation table A */
  2313.       k->crcta[1] = 010201;
  2314.       k->crcta[2] = 020402;
  2315.       k->crcta[3] = 030603;
  2316.       k->crcta[4] = 041004, k->crcta[5] = 051205;
  2317.       k->crcta[6] = 061406;
  2318.       k->crcta[7] = 071607;
  2319.       k->crcta[8] = 0102010;
  2320.       k->crcta[9] = 0112211;
  2321.       k->crcta[10] = 0122412;
  2322.       k->crcta[11] = 0132613;
  2323.       k->crcta[12] = 0143014, k->crcta[13] = 0153215;
  2324.       k->crcta[14] = 0163416;
  2325.       k->crcta[15] = 0173617;
  2326.  
  2327.       k->crctb[0] = 0;          /* CRC table B */
  2328.       k->crctb[1] = 010611;
  2329.       k->crctb[2] = 021422;
  2330.       k->crctb[3] = 031233;
  2331.       k->crctb[4] = 043044;
  2332.       k->crctb[5] = 053655;
  2333.       k->crctb[6] = 062466;
  2334.       k->crctb[7] = 072277;
  2335.       k->crctb[8] = 0106110;
  2336.       k->crctb[9] = 0116701;
  2337.       k->crctb[10] = 0127532;
  2338.       k->crctb[11] = 0137323;
  2339.       k->crctb[12] = 0145154;
  2340.       k->crctb[13] = 0155745;
  2341.       k->crctb[14] = 0164576;
  2342.       k->crctb[15] = 0174367;
  2343.  
  2344.       return (X_OK);
  2345.  
  2346.    }
  2347.    else if (fc == K_GET)
  2348.    {
  2349.       /*
  2350.        * Send R packet with filenames we want.
  2351.        * Filenames cannot have spaces
  2352.        * since names are separated by spaces.
  2353.        */
  2354.       int i;
  2355.       char *p, *q;
  2356.  
  2357.       debug (DB_LOG, "function code == K_GET fc", 0, fc);
  2358.  
  2359.       p = (char *) (k->obuf);
  2360.       for (i = 0;; i++)
  2361.       {
  2362.          q = (char *) (k->filelist[i]);
  2363.          if (q == 0)
  2364.             break;
  2365.          if (i > 0)
  2366.             *p++ = ' ';
  2367.          while (*q)
  2368.             *p++ = *q++;
  2369.       }
  2370.       *p = 0;
  2371.  
  2372.       debug (DB_LOG, "before encstr k->obuf", k->obuf, 0);
  2373.       k->xdata = k->xdatabuf;
  2374.       encstr (k->obuf, k, r);   // Encode the name for transmission 
  2375.       debug (DB_LOG, "after encstr k->xdata", k->xdata, 0);
  2376.  
  2377.       buf = get_sslot (k, &s_slot);     // get a new send slot
  2378.       k->s_pw[k->s_seq] = s_slot;
  2379.         if(k->bcta3)
  2380.             k->bct = 3;
  2381.         else
  2382.             k->bct = 1;
  2383.       debug (DB_LOG, "K_GET sending R pkt s_seq", 0, k->s_seq);
  2384.       if (spkt ('R', k->s_seq, -1, k->xdata, k) != X_OK)
  2385.       {
  2386.          debug (DB_LOG, "K_GET error spkt(R) failed fc", 0, fc);
  2387.          return (X_ERROR);      /* I/O error, quit. */
  2388.       }
  2389.       k->state = R_WAIT;        /* All OK, switch states */
  2390.       r->rstatus = R_WAIT;
  2391.       k->what = W_GET;
  2392.       return (X_OK);
  2393.    }
  2394.    else if (fc == K_SEND)
  2395.    {
  2396.       if (rpar (k, 'S') != X_OK)        /* Send S packet with my parameters */
  2397.       {
  2398.          debug (DB_LOG, "K_SEND error rpar(S) failed fc", 0, fc);
  2399.          return (X_ERROR);      /* I/O error, quit. */
  2400.       }
  2401.       k->state = S_INIT;        /* All OK, switch states */
  2402.       r->rstatus = S_INIT;
  2403.       k->what = W_SEND;         /* Act like a sender */
  2404.       return (X_OK);
  2405.    }
  2406.    else if (fc == K_STATUS)
  2407.    {                            /* Status report requested. */
  2408.       debug (DB_LOG, "function code == K_STATUS fc", 0, fc);
  2409.       return (X_STATUS);        /* File name, date, size, if any. */
  2410.    }
  2411.    else if (fc == K_QUIT)
  2412.    {                            /* You told me to quit */
  2413.       debug (DB_LOG, "function code == K_QUIT fc", 0, fc);
  2414.       return (X_DONE);          /* so I quit. */
  2415.    }
  2416.    else if (fc == K_SYNC)
  2417.    {
  2418.       debug (DB_LOG, "function code == K_SYNC fc", 0, fc);
  2419.       epkt (msg, k);
  2420.       return (X_OK);
  2421.    }
  2422.    else if (fc == K_ERROR)
  2423.    {                            /* Send an error packet... */
  2424.       debug (DB_LOG, "K_ERROR error fc", 0, fc);
  2425.       epkt (msg, k);
  2426.       k->closef (k, 0, (k->state == S_DATA) ? 1 : 2);   /* Close file */
  2427.       return (X_DONE);          /* and quit. */
  2428.    }
  2429.    else if (fc != K_RUN)
  2430.    {                            /* Anything else is an error. */
  2431.       debug (DB_LOG, "not K_RUN error fc", 0, fc);
  2432.       return (X_ERROR);
  2433.    }
  2434.  
  2435.    if (k->state == R_NONE)      /* (probably unnecessary) */
  2436.       return (X_OK);
  2437.  
  2438.    /* If we're in the protocol, check to make sure we got a new packet */
  2439.    debug (DB_MSG, "In the protocol", 0, 0);
  2440.  
  2441.    if (k->do_rxd)
  2442.    {                            // we tried to read a packet in main loop
  2443.  
  2444.       debug (DB_LOG, "DO_RXD len", 0, len);     // in kermit call list
  2445.  
  2446.       if (len < 4)
  2447.       {                         /* Packet obviously no good? */
  2448.          int ret;
  2449.  
  2450.          if (k->what == W_RECV) /* If receiving */
  2451.          {
  2452.             debug (DB_MSG, "DO_RXD len<4", 0, 0);
  2453. #ifdef USE_NAK_OLDEST_UNACKED
  2454.             nak_oldest_unacked (k, -1);
  2455. #endif
  2456.                 if(k->state == R_FILE)
  2457.                 {
  2458.                     debug (DB_MSG, "calling rpar again", 0, 0);
  2459.                     rc = rpar (k, 'Y');    /* ACK again with my parameters */
  2460.                 }
  2461.             return (X_OK);
  2462.          }
  2463.          else                   /* If W_SEND or W_GET */
  2464.          {
  2465.             debug (DB_MSG, "DO_RXD len<4: resending earliest", 0, 0);
  2466.             ret = resend (k, -1);       /* retransmit earliest packet in queue. */
  2467.             return (ret);
  2468.          }
  2469.       }
  2470.  
  2471.       /* Parse the packet */
  2472.  
  2473.       if (k->what == W_RECV)
  2474.       {                         /* If we're sending ACKs */
  2475.          switch (k->cancel)
  2476.          {                      /* Get cancellation code if any */
  2477.          case 0:
  2478.             s = (UCHAR *) 0;
  2479.             break;
  2480.          case 1:
  2481.             s = (UCHAR *) "X";
  2482.             break;
  2483.          case 2:
  2484.             s = (UCHAR *) "Z";
  2485.             break;
  2486.          }
  2487.       }
  2488.  
  2489.       pdf = k->ipktbuf;
  2490.  
  2491.       qdf = pdf;                /* Pointer to data to be checked */
  2492.       rlen = xunchar (*pdf++);  /* Length field */
  2493.       rseq = xunchar (*pdf++);  /* Received Sequence number */
  2494.       rtyp = *pdf++;            /* Type */
  2495.  
  2496.         if(k->state == S_EOT &&
  2497.            len==4 && rseq==0 && rtyp=='N' && *pdf==0x33)
  2498.         {
  2499.          debug (DB_MSG, "Got NAK for seq=0 after sending B-pkt", 0, 0);
  2500.          debug (DB_MSG, "  so assuming we are done.", 0, 0);
  2501.             return (X_DONE);          /* (or X_ERROR) */
  2502.         }
  2503.  
  2504.       // Really to use rseq the packet must be validated by CRC first.
  2505.       if (rseq < 0 || rseq > 63)
  2506.       {
  2507.          debug (DB_LOG, "WARNING: rseq", 0, rseq);
  2508.          return (X_OK);
  2509.       }
  2510.  
  2511.       if ((k->what == W_RECV) && (rtyp == 'N' || rtyp == 'Y'))
  2512.       {
  2513.          /* Echo (it happens), ignore */
  2514.          return (X_OK);
  2515.       }
  2516.  
  2517.       if (rlen == 0)
  2518.       {                         /* Length 0 means long packet */
  2519.          c = pdf[2];            /* Get header checksum */
  2520.          pdf[2] = '\0';
  2521.          if (xunchar (c) != chk1 (pdf - 3, k))
  2522.          {                      /* Check it */
  2523.             int ret;
  2524.             debug (DB_MSG, "long pkt chk1 bad", 0, 0);
  2525.             if (k->what == W_RECV)
  2526.             {
  2527. #ifdef USE_NAK_OLDEST_UNACKED
  2528.                debug (DB_MSG, "sending NAK for oldest unacked", 0, 0);
  2529.                nak_oldest_unacked (k, -1);      /* Send NAK */
  2530. #endif
  2531.                return (X_OK);
  2532.             }
  2533.             else
  2534.             {
  2535.                debug (DB_MSG, "resending earliest", 0, 0);
  2536.                ret = resend (k, -1);
  2537.                debug (DB_LOG, "resend ret", 0, ret);
  2538.                return (ret);
  2539.             }
  2540.          }
  2541.          debug (DB_MSG, "HDR CHKSUM OK", 0, 0);
  2542.          pdf[2] = c;            /* Put checksum back */
  2543.             if(k->bcta3)
  2544.                 k->bct = 3;
  2545.          datalen = xunchar (pdf[0]) * 95 + xunchar (pdf[1]) - k->bct;   /* Data length */
  2546.  
  2547.          debug (DB_LOG, "  long packet datalen", 0, datalen);
  2548.          debug (DB_LOG, "  rlen", 0, rlen);
  2549.          debug (DB_LOG, "  rseq", 0, rseq);
  2550.  
  2551.          pdf += 3;              /* Fix data pointer */
  2552.       }
  2553.       else
  2554.       {                         /* Regular packet */
  2555.             if(k->bcta3)
  2556.                 k->bct = 3;
  2557.          datalen = rlen - k->bct - 2;   /* Data length */
  2558.          debug (DB_LOG, "regular packet datalen", 0, datalen);
  2559.       }
  2560.  
  2561.  
  2562.       if (rtyp == 'S' || k->state == S_INIT)
  2563.       {                         /* S-packet was
  2564.                                  * retransmitted? */
  2565.             if(k->bcta3){
  2566.                 chklen = 3;
  2567.                 datalen = rlen - 5;
  2568.             }
  2569.             else{
  2570.                 chklen = 1;            /* Block check is always type 1 */
  2571.                 datalen = rlen - 3;
  2572.             }
  2573.          debug (DB_LOG, "S-packet datalen", 0, datalen);
  2574.       }
  2575.       else
  2576.       {
  2577.             if(k->bcta3)
  2578.                 k->bct = 3;
  2579.          chklen = k->bct;
  2580.       }
  2581.  
  2582.       debug (DB_LOG, "DO_RXD state", 0, k->state);
  2583.         debug (DB_MSG, "  These are unverified: (before blk chk tested)",0,0);
  2584.       debug (DB_LOG, "  rlen", 0, rlen);
  2585.       debug (DB_LOG, "  rseq", 0, rseq);
  2586.       debug (DB_CHR, "  rtyp", 0, rtyp);
  2587.       debug (DB_LOG, "  bct", 0, k->bct);
  2588.       debug (DB_LOG, "  bcta3", 0, k->bcta3);
  2589.       debug (DB_LOG, "  datalen", 0, datalen);
  2590.       debug (DB_LOG, "  chklen", 0, chklen);
  2591.  
  2592.       if (datalen < 0 || datalen + chklen + 1 >= P_BUFLEN)
  2593.       {
  2594.          debug (DB_MSG, "DO_RXD datalen out of bounds", 0, 0);
  2595.          if (k->what == W_RECV)
  2596.          {
  2597. #ifdef USE_NAK_OLDEST_UNACKED
  2598.             debug (DB_LOG, "  NAK oldest or rseq", 0, rseq);
  2599.             nak_oldest_unacked (k, rseq);
  2600. #endif
  2601.          }
  2602.          else
  2603.          {
  2604.             debug (DB_MSG, "  resend earliest", 0, 0);
  2605.             resend (k, -1);
  2606.          }
  2607.          return (X_OK);
  2608.       }
  2609.  
  2610.       for (i = 0; i < chklen; i++)      /* Copy the block check */
  2611.          pbc[i] = pdf[datalen + i];
  2612.       pbc[i] = '\0';            /* Null-terminate block check string */
  2613.       pdf[datalen] = '\0';      /* and the packet DATA field. */
  2614.       switch (chklen)
  2615.       {                         /* Check the block check */
  2616.       case 1:                  /* Type 1, 6-bit checksum */
  2617.          ok = (xunchar (*pbc) == chk1 (qdf, k));
  2618. #ifdef DEBUG
  2619.          if (ok && xerror ())
  2620.             ok = 0;
  2621. #endif /* DEBUG */
  2622.          if (!ok)
  2623.          {
  2624.             debug (DB_CHR, "6-bit checksum ERROR rtyp", 0, rtyp);
  2625.             debug (DB_LOG, "  rseq", 0, rseq);
  2626.             debug (DB_PKT, "  k->ipktbuf", k->ipktbuf, 0);
  2627.             if (k->what == W_RECV)
  2628.             {
  2629. #ifdef USE_NAK_OLDEST_UNACKED
  2630.                debug (DB_LOG, "  NAK oldest or rseq", 0, rseq);
  2631.                nak_oldest_unacked (k, rseq);
  2632. #endif
  2633.             }
  2634.             else
  2635.             {
  2636.                debug (DB_MSG, "  resend earliest", 0, 0);
  2637.                resend (k, -1);
  2638.             }
  2639.             return (X_OK);
  2640.          }
  2641.          break;
  2642.  
  2643.       case 2:                  /* Type 2, 12-bit checksum */
  2644.          i = xunchar (*pbc) << 6 | xunchar (pbc[1]);
  2645.          ok = (i == chk2 (qdf, k));
  2646. #ifdef DEBUG
  2647.          if (ok && xerror ())
  2648.             ok = 0;
  2649. #endif /* DEBUG */
  2650.          if (!ok)
  2651.          {                      /* No match */
  2652.             debug (DB_CHR, "12-bit checksum ERROR rtyp", 0, rtyp);
  2653.             debug (DB_LOG, "  rseq", 0, rseq);
  2654.             debug (DB_PKT, "  k->ipktbuf", k->ipktbuf, 0);
  2655.             if (rtyp == 'E')
  2656.             {                   /* Allow E packets to have type 1 */
  2657.                int j;
  2658.                j = datalen;
  2659.                pdf[j++] = pbc[0];
  2660.                pdf[j] = '\0';
  2661.                if (xunchar (pbc[1]) == chk1 (qdf, k))
  2662.                   break;
  2663.                else
  2664.                   pdf[--j] = '\0';
  2665.             }
  2666.             if (k->what == W_RECV)
  2667.             {
  2668. #ifdef USE_NAK_OLDEST_UNACKED
  2669.                debug (DB_LOG, "  NAK oldest or rseq", 0, rseq);
  2670.                nak_oldest_unacked (k, rseq);
  2671. #endif
  2672.             }
  2673.             else
  2674.             {
  2675.                debug (DB_MSG, "  resend earliest", 0, 0);
  2676.                resend (k, -1);
  2677.             }
  2678.             return (X_OK);
  2679.          }
  2680.          break;
  2681.  
  2682.       case 3:                  /* Type 3, 16-bit CRC */
  2683.          crc = (xunchar (pbc[0]) << 12)
  2684.             | (xunchar (pbc[1]) << 6) | (xunchar (pbc[2]));
  2685.          ok = (crc == chk3 (qdf, k));
  2686. #ifdef DEBUG
  2687.          if (ok && xerror ())
  2688.          {
  2689.             ok = 0;
  2690.             debug (DB_MSG, "INPUT CRC ERROR INJECTED", 0, 0);
  2691.          }
  2692. #endif /* DEBUG */
  2693.          if (!ok)
  2694.          {
  2695.             debug (DB_MSG, "Packet blk chk3 bad", 0, 0);
  2696.             debug (DB_LOG, "  rseq", 0, rseq);
  2697.  
  2698.             if (rtyp == 'E')
  2699.             {                   /* Allow E packets to have type 1 */
  2700.                int j;
  2701.                j = datalen;
  2702.                pdf[j++] = pbc[0];
  2703.                pdf[j++] = pbc[1];
  2704.                pdf[j] = '\0';
  2705.                if (xunchar (pbc[2]) == chk1 (qdf, k))
  2706.                   break;
  2707.                else
  2708.                {
  2709.                   j -= 2;
  2710.                   pdf[j] = '\0';
  2711.                }
  2712.             }
  2713.             if (k->what == W_RECV)
  2714.             {
  2715. #ifdef USE_NAK_OLDEST_UNACKED
  2716.                debug (DB_LOG, "  NAK oldest or rseq", 0, rseq);
  2717.                nak_oldest_unacked (k, rseq);
  2718. #endif
  2719.             }
  2720.             else
  2721.             {
  2722.                debug (DB_MSG, "  resend earliest", 0, 0);
  2723.                resend (k, -1);
  2724.             }
  2725.             return (X_OK);
  2726.          }
  2727.       }
  2728.  
  2729.       // now the packet has a good block check
  2730.       debug (DB_MSG, "Packet blk chk good", 0, 0);
  2731.       debug (DB_CHR, "  rtyp", 0, rtyp);
  2732.       debug (DB_LOG, "  rseq", 0, rseq);
  2733.       debug (DB_LOG, "  k->r_seq", 0, k->r_seq);
  2734.       debug (DB_LOG, "  k->state", 0, k->state);
  2735.       debug (DB_LOG, "  chklen", 0, chklen);
  2736.       debug (DB_LOG, "  datalen", 0, datalen);
  2737.  
  2738.       if (rtyp == 'E')          /* (AND CLOSE FILES?) */
  2739.       {
  2740.          debug (DB_MSG, "error msg received from other kermit", 0, 0);
  2741.          debug (DB_PKT, "EPKT", pdf, 0);
  2742.          return (X_ERROR);
  2743.       }
  2744.  
  2745.       if (k->what == W_SEND)    /* Sending, check for ACK */
  2746.       {
  2747.          if (rtyp != 'Y')
  2748.          {
  2749.             int ret;
  2750.  
  2751.             debug (DB_CHR, "rtyp not Y, rtyp", 0, rtyp);
  2752.  
  2753.             if (k->state == S_DATA &&
  2754.                 rtyp == 'N' && rseq == ((k->r_seq + 1) & 63))
  2755.             {
  2756.                debug (DB_MSG,
  2757.                       "FYI got NAK for packet just after last one sent", 0,
  2758.                       0);
  2759. #ifdef DEBUG
  2760.                show_sslots (k);
  2761. #endif
  2762.  
  2763. # if 0
  2764.                debug (DB_MSG, "W_SEND Freeing all send slots", 0, 0);
  2765.                for (i = 0; i < k->wslots; i++)
  2766.                   free_sslot (k, i);
  2767.                k->sw_full = 0;
  2768. # endif
  2769.             }
  2770.  
  2771.             if (nused_sslots (k) == 0)
  2772.             {
  2773.                debug (DB_LOG, "sending requested rseq", 0, rseq);
  2774.  
  2775.                nxtpkt (k);
  2776.                if (k->s_seq != rseq)
  2777.                {
  2778.                   debug (DB_LOG, "error confused s_seq", 0, k->s_seq);
  2779.                   debug (DB_LOG, "  rseq", 0, rseq);
  2780.                   epkt ("EKSW W_SEND confused", k);
  2781.                   exit (1);
  2782.                   return (X_ERROR);
  2783.                }
  2784.                buf = get_sslot (k, &s_slot);    // get a new send slot
  2785.                k->s_pw[k->s_seq] = s_slot;
  2786.  
  2787.                rc = sdata (k, r);       /* Send next data packet */
  2788.                if (rc == X_ERROR)
  2789.                   return (rc);
  2790.                if (rc == 0)
  2791.                {                /* If there was no data to send */
  2792.                   k->closef (k, 0, 1);  /* Close input file */
  2793.                   k->state = S_EOF;     /* And wait for ACK */
  2794.                   r->rstatus = S_EOF;
  2795.                   k->s_seq--;
  2796.                   if (k->s_seq < 0)
  2797.                      k->s_seq += 64;
  2798.                   free_sslot (k, s_slot);
  2799.                }                /* Otherwise stay in data state */
  2800.                k->r_seq = k->s_seq;     /* Sequence number to wait for */
  2801.                return (X_OK);
  2802.             }
  2803.             else
  2804.             {
  2805.                debug (DB_LOG, "6: resending rseq", 0, rseq);
  2806.                // resend will send rseq if it's in the send table
  2807.                // otherwise it will resend earliest unacked packet
  2808.                ret = resend (k, rseq);
  2809.                debug (DB_LOG, "ret", 0, ret);
  2810.                return (ret);
  2811.             }
  2812.          }
  2813.  
  2814.          // we're sending, received an ACK and packet block check OK
  2815.  
  2816.          s_slot = k->s_pw[rseq];
  2817.          if (s_slot < 0 || s_slot >= k->wslots)
  2818.          {
  2819.             debug (DB_LOG, "ignoring ack -- not in table rseq", 0, rseq);
  2820. #ifdef DEBUG
  2821.             show_sslots (k);
  2822. #endif
  2823.             return (X_OK);
  2824.          }
  2825.  
  2826.          // set ACK'd flag for that send slot
  2827.          k->opktinfo[s_slot].flg = 1;
  2828.  
  2829.          // remove earliest and subsequent
  2830.          free_sslot_easca (k);
  2831.  
  2832. #ifdef DEBUG
  2833.          // check that send table sequence numbers are in order
  2834.          chk_sseq_nos (k);
  2835. #endif
  2836.  
  2837.          if (k->state == S_DATA)
  2838.          {                      /* ACK to Data packet? */
  2839.             if (k->cancel ||    /* Cancellation requested by caller? */
  2840.                 *pdf == 'X' || *pdf == 'Z')
  2841.             {                   /* Or by receiver? */
  2842.                k->closef (k, 0, 1);     /* Close input file */
  2843.                nxtpkt (k);      /* Next packet sequence number */
  2844.                buf = get_sslot (k, &s_slot);    // get a new send slot
  2845.                k->s_pw[k->s_seq] = s_slot;
  2846.  
  2847.                debug (DB_LOG, "Cancellation: sending Z pkt s_seq", 0,
  2848.                       k->s_seq);
  2849.                debug (DB_LOG, "  s_slot", 0, s_slot);
  2850.  
  2851.                rc = spkt ('Z', k->s_seq, 0, (UCHAR *) 0, k);
  2852.                debug (DB_LOG, "  rc", 0, rc);
  2853.                if (rc != X_OK)
  2854.                {
  2855.                   return (rc);
  2856.                }
  2857.                if (*pdf == 'Z' || k->cancel == I_GROUP)
  2858.                {                /* Cancel Group? */
  2859.                   debug (DB_MSG, "Group Cancel (Send)", 0, 0);
  2860.                   while (*(k->filelist))
  2861.                   {             /* Go to end of file list */
  2862.                      debug (DB_LOG, "Skip", *(k->filelist), 0);
  2863.                      (k->filelist)++;
  2864.                   }
  2865.                }
  2866.                k->state = S_EOF;        /* Wait for ACK to EOF */
  2867.                r->rstatus = S_EOF;
  2868.                k->r_seq = k->s_seq;     /* Sequence number of packet we want */
  2869.                return (X_OK);
  2870.             }
  2871.          }
  2872.          r->sofar = r->sofar_rumor;
  2873.  
  2874.          debug (DB_LOG, "end of W_SEND rseq", 0, rseq);
  2875.          debug (DB_LOG, "  k->r_seq", 0, k->r_seq);
  2876.          debug (DB_LOG, "  k->state", 0, k->state);
  2877.          debug (DB_CHR, "  rtyp", 0, rtyp);
  2878.       }                         // if (k->what == W_SEND)
  2879.    }                            // if(k->do_rxd)
  2880.  
  2881.    switch (k->state)
  2882.    {                            /* Kermit protocol state switcher */
  2883.  
  2884.    case S_INIT:                /* Got other Kermit's parameters */
  2885.    case S_EOF:                 /* got ack to Z packet */
  2886.       if (k->state == S_INIT)
  2887.       {                         /* Got ACK to S packet? */
  2888.          debug (DB_MSG, "S_INIT", 0, 0);
  2889.          spar (k, pdf, datalen);        /* Set negotiated parameters */
  2890.          debug (DB_CHR, "Parity", 0, k->parity);
  2891.          debug (DB_LOG, "Ebqflg", 0, k->ebqflg);
  2892.          debug (DB_CHR, "Ebq", 0, k->ebq);
  2893.       }
  2894.       else
  2895.       {
  2896.          debug (DB_LOG, "S_EOF rseq", 0, rseq);
  2897.  
  2898. # if 0                          // don't use k->r_seq to switch states -- use nused_sslots()
  2899.          debug (DB_LOG, "S_EOF k->r_seq", 0, k->r_seq);
  2900.          if (rseq != k->r_seq)  // keep reading until last packet ACKed
  2901.             return (X_OK);
  2902. # endif
  2903.  
  2904.          if ((i = nused_sslots (k)) > 0)
  2905.          {
  2906.             debug (DB_LOG, "keep reading until all sslots free.  nused", 0,
  2907.                    i);
  2908. #ifdef DEBUG
  2909.             show_sslots (k);
  2910. #endif
  2911.             return (X_OK);
  2912.          }
  2913.       }
  2914.  
  2915.       nxtpkt (k);               /* Get next packet number etc */
  2916.  
  2917.       k->filename = *(k->filelist);     /* Get next filename */
  2918.       if (k->filename)
  2919.       {                         /* If there is one */
  2920.          int i;
  2921.          for (i = 0; i < FN_MAX; i++)
  2922.          {                      /* Copy name to result struct 
  2923.                                  */
  2924.             r->filename[i] = k->filename[i];
  2925.             if (!(r->filename[i]))
  2926.                break;
  2927.          }
  2928.          (k->filelist)++;
  2929.          debug (DB_LOG, "Filename", k->filename, 0);
  2930.          if ((rc = (k->openf) (k, k->filename, 1)) != X_OK)     /* Try to open */
  2931.          {
  2932.             debug (DB_LOG, "k->openf failed rc", 0, rc);
  2933.             return (rc);
  2934.          }
  2935.  
  2936.          encstr (k->filename, k, r);    /* Encode the name for transmission */
  2937.  
  2938.          buf = get_sslot (k, &s_slot);  // get a new send slot
  2939.          k->s_pw[k->s_seq] = s_slot;
  2940.  
  2941.          debug (DB_LOG, "sending F pkt k->s_seq", 0, k->s_seq);
  2942.          debug (DB_LOG, "sending F pkt s_slot", 0, s_slot);
  2943.          k->sw_full = 0;
  2944.          if ((rc = spkt ('F', k->s_seq, -1, k->xdata, k)) != X_OK)
  2945.          {
  2946.             return (rc);        /* Send F packet */
  2947.          }
  2948.          r->sofar = 0L;
  2949.          r->sofar_rumor = 0L;
  2950.          k->state = S_FILE;     /* Wait for ACK */
  2951.          r->rstatus = S_FILE;
  2952.       }
  2953.       else
  2954.       {                         /* No more files - we're done */
  2955.  
  2956. # if 0                          // this was a hack but should not be needed
  2957.  
  2958.          int nused = nused_sslots (k);
  2959.          debug (DB_LOG, "want to send B pkt nused", 0, nused);
  2960.  
  2961.          if (nused == k->wslots)
  2962.          {
  2963.             short rm_sseq;
  2964.             short rm_sslot;
  2965.  
  2966.             rm_sseq = earliest_sseq (k);
  2967.             if (rm_sseq < 0 || rm_sseq >= k->wslots)
  2968.             {
  2969.                debug (DB_LOG, "want to send B pkt error rm_sseq", 0, rm_sseq);
  2970.                return (X_ERROR);
  2971.             }
  2972.  
  2973.             rm_sslot = k->s_pw[rm_sseq];
  2974.             if (rm_sslot < 0 || rm_sslot >= k->wslots)
  2975.             {
  2976.                debug (DB_LOG, "want to send B pkt error rm_sslot", 0,
  2977.                       rm_sslot);
  2978.                debug (DB_LOG, "  rm_sseq", 0, rm_sseq);
  2979.                return (X_ERROR);
  2980.             }
  2981.  
  2982.             free_sslot (k, rm_sslot);
  2983.          }
  2984. # endif
  2985.  
  2986.          // all sslots are supposed to be free at this point
  2987.  
  2988.          buf = get_sslot (k, &s_slot);  // get a new send slot
  2989.          if (s_slot < 0 || s_slot >= k->wslots)
  2990.          {
  2991.             debug (DB_LOG, "want to send B pkt error s_slot", 0, s_slot);
  2992.             debug (DB_LOG, "  k->s_seq", 0, k->s_seq);
  2993.             return (X_ERROR);
  2994.          }
  2995.          k->s_pw[k->s_seq] = s_slot;
  2996.  
  2997.          debug (DB_LOG, "sending B pkt k->s_seq", 0, k->s_seq);
  2998.          debug (DB_LOG, "  s_slot", 0, s_slot);
  2999.  
  3000.          if ((rc = spkt ('B', k->s_seq, 0, (UCHAR *) 0, k)) != X_OK)
  3001.          {
  3002.             return (rc);        /* Send EOT packet */
  3003.          }
  3004.          k->state = S_EOT;      /* Wait for ACK to B packet, end of transmission */
  3005.          r->rstatus = S_EOT;
  3006.       }
  3007.       k->r_seq = k->s_seq;      /* Sequence number of packet we want */
  3008.       return (X_OK);            /* Return to control program */
  3009.  
  3010.    case S_FILE:                /* Got ACK to F packet */
  3011.       nxtpkt (k);               /* Get next packet number etc */
  3012.       if (k->capas & CAP_AT)
  3013.       {                         /* A-packets negotiated? */
  3014.          if ((rc = sattr (k, r)) != X_OK)       /* Yes, send Attribute packet */
  3015.          {
  3016.             debug (DB_LOG, "SATTR failed rc", 0, rc);
  3017.             return (rc);
  3018.          }
  3019.          k->state = S_ATTR;     /* And wait for its ACK */
  3020.          r->rstatus = S_ATTR;
  3021.          did_a_pkt = 1;
  3022.       }
  3023.       else
  3024.       {
  3025.          did_a_pkt = 0;
  3026.       }
  3027.  
  3028.       if (did_a_pkt == 0)
  3029.       {
  3030.          /* No A packets - send first data */
  3031.  
  3032.          buf = get_sslot (k, &s_slot);  // get a new send slot
  3033.          k->s_pw[k->s_seq] = s_slot;
  3034.  
  3035.          debug (DB_LOG, "sending first D k->s_seq", 0, k->s_seq);
  3036.          debug (DB_LOG, "  s_slot", 0, s_slot);
  3037.  
  3038.          rc = sdata (k, r);     /* Send next data packet */
  3039.          if (rc == X_ERROR)
  3040.             return (rc);
  3041.          if (rc == 0)
  3042.          {
  3043.             /* File is empty so send EOF packet */
  3044.             buf = get_sslot (k, &s_slot);       // get a new send slot
  3045.             k->s_pw[k->s_seq] = s_slot;
  3046.             debug (DB_LOG, "empty file: sending Z pkt k->s_seq", 0, k->s_seq);
  3047.             debug (DB_LOG, "  s_slot", 0, s_slot);
  3048.             if ((rc = spkt ('Z', k->s_seq, 0, (UCHAR *) 0, k)) != X_OK)
  3049.             {
  3050.                return (rc);
  3051.             }
  3052.             k->closef (k, 0, 1);        /* Close input file */
  3053.             k->state = S_EOF;   /* Wait for ACK to EOF */
  3054.             r->rstatus = S_EOF;
  3055.          }
  3056.          else
  3057.          {                      /* Sent some data */
  3058.             k->state = S_DATA;  /* Wait for ACK to first data */
  3059.             r->rstatus = S_DATA;
  3060.          }
  3061.       }                         // if(did_a_pkt==0)
  3062.  
  3063.       k->r_seq = k->s_seq;      /* Sequence number to wait for */
  3064.       return (X_OK);
  3065.  
  3066.    case S_ATTR:                /* Got ACK to A packet */
  3067.    case S_DATA:                /* Got ACK to D packet */
  3068.       if (k->state == S_ATTR)
  3069.       {
  3070.          /*
  3071.           * CHECK ATTRIBUTE RESPONSE 
  3072.           */
  3073.          /*
  3074.           * IF REJECTED do the right thing...
  3075.           * Left as an exersise for the reader...
  3076.           */
  3077.          k->state = S_DATA;
  3078.          r->rstatus = S_DATA;
  3079.       }
  3080.  
  3081.       buf = get_sslot (k, &s_slot);     // get a new send slot
  3082.       if (s_slot < 0)
  3083.       {
  3084.          debug (DB_LOG, "window full k->state", 0, k->state);
  3085.          k->sw_full = 1;
  3086.          return (X_OK);
  3087.       }
  3088.       nxtpkt (k);               /* Get next packet number */
  3089.       k->s_pw[k->s_seq] = s_slot;
  3090.  
  3091.       k->sw_full = 0;
  3092.  
  3093.       debug (DB_LOG, "S_DATA sending k->s_seq", 0, k->s_seq);
  3094.       debug (DB_LOG, "  s_slot", 0, s_slot);
  3095.  
  3096.       rc = sdata (k, r);        /* Send first or next data packet */
  3097.  
  3098.       debug (DB_LOG, "S_DATA sdata rc", 0, rc);
  3099.       debug (DB_LOG, "  k->s_seq", 0, k->s_seq);
  3100.  
  3101.       if (rc == X_ERROR)
  3102.          return (rc);
  3103.  
  3104.       if (rc == 0)
  3105.       {                         /* If there was no data to send */
  3106.          k->closef (k, 0, 1);   /* Close input file */
  3107.  
  3108.          debug (DB_LOG, "NUSED_SSLOTS", 0, nused_sslots (k));
  3109.  
  3110.          k->state = S_EOF;      /* And wait for ACK to Z pkt */
  3111.          r->rstatus = S_EOF;
  3112.  
  3113.          // sdata() above did not send D pkt so send Z pkt in its place
  3114.          debug (DB_LOG, "EOF so sending Z pkt k->s_seq", 0, k->s_seq);
  3115.          debug (DB_LOG, "  s_slot", 0, s_slot);
  3116.  
  3117.          if ((rc = spkt ('Z', k->s_seq, 0, (UCHAR *) 0, k)) != X_OK)
  3118.             return (rc);
  3119.  
  3120.       }                         /* Otherwise stay in data state */
  3121.       k->r_seq = k->s_seq;      /* Sequence number to wait for */
  3122.       return (X_OK);
  3123.  
  3124.    case S_EOT:                 /* Get ACK to EOT packet */
  3125.       debug (DB_MSG, "S_EOT X_DONE", 0, 0);
  3126.       return (X_DONE);          /* (or X_ERROR) */
  3127.  
  3128.    case R_WAIT:                /* Waiting for the S packet */
  3129.       debug (DB_CHR, "R_WAIT rtyp", 0, rtyp);
  3130.       debug (DB_LOG, "  rseq", 0, rseq);
  3131.       debug (DB_LOG, "  what", 0, k->what);
  3132.       if (rtyp == 'S')
  3133.       {                         /* Got it */
  3134.          spar (k, pdf, datalen);        /* Set parameters from it */
  3135.  
  3136.          debug (DB_MSG, "R_WAIT rtyp==S after spar", 0, 0);
  3137.          debug (DB_CHR, "  Parity", 0, k->parity);
  3138.          debug (DB_LOG, "  Ebqflg", 0, k->ebqflg);
  3139.          debug (DB_CHR, "  Ebq", 0, k->ebq);
  3140.  
  3141.          rc = rpar (k, 'Y');    /* ACK with my parameters */
  3142.  
  3143.          debug (DB_LOG, "R_WAIT rtyp==S after rpar rc", 0, rc);
  3144.          debug (DB_LOG, "  k->capas & CAP_LP", 0, k->capas & CAP_LP);
  3145.          debug (DB_LOG, "  k->capas & CAP_SW", 0, k->capas & CAP_SW);
  3146.          debug (DB_LOG, "  k->capas & CAP_AT", 0, k->capas & CAP_AT);
  3147.          debug (DB_LOG, "  k->capas & CAP_RS", 0, k->capas & CAP_RS);
  3148.          debug (DB_LOG, "  k->capas & CAP_LS", 0, k->capas & CAP_LS);
  3149.          debug (DB_CHR, "  k->ebq           ", 0, k->ebq);
  3150.          debug (DB_LOG, "  k->ebqflg        ", 0, k->ebqflg);
  3151.          debug (DB_LOG, "  k->parity        ", 0, k->parity);
  3152.          debug (DB_LOG, "  k->s_eom         ", 0, k->s_eom);
  3153.          debug (DB_LOG, "  k->r_timo        ", 0, k->r_timo);
  3154.          debug (DB_LOG, "  k->s_timo        ", 0, k->s_timo);
  3155.          debug (DB_CHR, "  k->r_ctlq        ", 0, k->r_ctlq);
  3156.          debug (DB_CHR, "  k->s_ctlq        ", 0, k->s_ctlq);
  3157.          debug (DB_CHR, "  k->rptq          ", 0, k->rptq);
  3158.          debug (DB_LOG, "  k->rptflg        ", 0, k->rptflg);
  3159.          debug (DB_LOG, "  k->bct           ", 0, k->bct);
  3160.          debug (DB_LOG, "  k->bcta3           ", 0, k->bcta3);
  3161.          debug (DB_LOG, "  k->r_maxlen      ", 0, k->r_maxlen);
  3162.          debug (DB_LOG, "  k->s_maxlen      ", 0, k->s_maxlen);
  3163.          debug (DB_LOG, "  k->wslots        ", 0, k->wslots);
  3164.          debug (DB_LOG, "  k->binary        ", 0, k->binary);
  3165.          debug (DB_LOG, "  k->retry         ", 0, k->retry);
  3166.  
  3167.          if (rc != X_OK)
  3168.          {
  3169.             debug (DB_MSG, "R_WAIT error rpar(Y) failed", 0, 0);
  3170.             return (X_ERROR);   /* I/O error, quit. */
  3171.          }
  3172.          k->state = R_FILE;     /* All OK, switch states */
  3173.          r->rstatus = R_FILE;
  3174.          k->what = W_RECV;
  3175.       }
  3176.       else if (k->what == W_GET)
  3177.       {
  3178.          debug (DB_MSG, "  what==W_GET so resending R-packet", 0, 0);
  3179.          rc = resend (k, 0);
  3180.       }
  3181.       else
  3182.       {
  3183.          debug (DB_MSG, "  unexpected packet so send NAK for seq 0", 0, 0);
  3184.          rc = nak (k, 0, -1);
  3185.       }
  3186.       if (rc != X_OK)
  3187.          debug (DB_LOG, "R_WAIT rc not X_OK: rc", 0, rc);
  3188.       return (rc);
  3189.  
  3190.    case R_FILE:                /* Want an F or B packet, may get Z and S pkt */
  3191.       debug (DB_CHR, "R_FILE rtyp", 0, rtyp);
  3192.       if (rtyp == 'F')
  3193.       {                         /* File name */
  3194.          if ((rc = decode (k, r, 0, pdf, -1)) == X_OK)  /* Decode and save */
  3195.             k->state = R_ATTR;  /* Switch to next state */
  3196.          r->rstatus = k->state;
  3197.          debug (DB_LOG, "R_FILE decode rc", 0, rc);
  3198.          debug (DB_LOG, "R_FILE FILENAME", r->filename, 0);
  3199.          if (rc == X_OK)
  3200.          {                      /* All OK so far */
  3201.             r->filedate[0] = '\0';      /* No file date yet */
  3202.             r->filesize = 0L;   /* Or file size */
  3203.             r->sofar = 0L;      /* Or bytes transferred yet */
  3204.             r->sofar_rumor = 0L;        /* Or bytes transferred yet */
  3205.             rc = ack (k, rseq, r->filename);    /* so ACK the F packet */
  3206.          }
  3207.          else
  3208.          {
  3209.             epkt ("eksw Filename error", k);    /* Error decoding filename */
  3210.             return (rc);
  3211.          }
  3212.       }
  3213.       else if (rtyp == 'B')
  3214.       {                         /* Break, end of transaction */
  3215.          ack (k, rseq, (UCHAR *) 0);
  3216.          ack (k, rseq, (UCHAR *) 0);
  3217.          rc = (ack (k, rseq, (UCHAR *) 0) == X_OK) ? X_DONE : X_ERROR;
  3218.       }
  3219.       else if (rtyp == 'Z')
  3220.       {                         /* End of file again */
  3221.          rc = ack (k, rseq, (UCHAR *) 0);
  3222.       }
  3223.       else if (rtyp == 'S')
  3224.       {                         /* got S pkt again */
  3225.          spar (k, pdf, datalen);        /* Set parameters from it */
  3226.  
  3227.          debug (DB_MSG, "R_FILE rtyp==S again", 0, 0);
  3228.          debug (DB_CHR, "  Parity", 0, k->parity);
  3229.          debug (DB_LOG, "  Ebqflg", 0, k->ebqflg);
  3230.          debug (DB_CHR, "  Ebq", 0, k->ebq);
  3231.  
  3232.          rc = rpar (k, 'Y');    /* ACK with my parameters */
  3233.  
  3234.          debug (DB_LOG, "R_FILE after rpar rc", 0, rc);
  3235.          if (rc != X_OK)
  3236.          {
  3237.             debug (DB_MSG, "R_FILE error rpar(Y) failed", 0, 0);
  3238.             return (X_ERROR);   /* I/O error, quit. */
  3239.          }
  3240.          k->state = R_FILE;     /* All OK, switch states */
  3241.          r->rstatus = R_FILE;
  3242.          k->what = W_RECV;
  3243.       }
  3244.       else
  3245.       {
  3246.          rc = X_ERROR;
  3247.       }
  3248.       debug (DB_LOG, "rc", 0, rc);
  3249.       return (rc);
  3250.  
  3251.    case R_ATTR:                /* Want A, D, or Z packet */
  3252.       debug (DB_CHR, "R_ATTR rtyp", 0, rtyp);
  3253.       if (rtyp == 'A')
  3254.       {                         /* Attribute packet */
  3255.          int x;
  3256.          x = gattr (k, pdf, r); /* Read the attributes */
  3257.          if (x > -1)
  3258.             k->binary = x;
  3259.          ack (k, rseq, (UCHAR *) "Y");  /* Always accept the file */
  3260.          return (X_OK);
  3261.       }
  3262.       else if (rtyp == 'D')
  3263.       {                         /* First data packet */
  3264.          k->obufpos = 0;        /* Initialize output buffer */
  3265.          k->filename = r->filename;
  3266.          r->sofar = 0L;
  3267.          r->sofar_rumor = 0L;
  3268.          if ((rc = (*(k->openf)) (k, r->filename, 2)) == X_OK)
  3269.          {
  3270.             k->state = R_DATA;  /* Switch to Data state */
  3271.             r->rstatus = k->state;
  3272.             rc = handle_good_rpkt (k, r, rseq, pdf);
  3273.          }
  3274.          else
  3275.          {
  3276.             debug (DB_MSG, "eksw cannot open output file", 0, 0);
  3277.             epkt ("eksw cannot open output file", k);
  3278.             return (rc);
  3279.          }
  3280.  
  3281.          if (rc == X_OK)
  3282.          {
  3283.          }
  3284.          else
  3285.          {
  3286.             debug (DB_MSG, "eksw Error writing data to file", 0, 0);
  3287.             epkt ("eksw Error writing data to file", k);
  3288.          }
  3289.          return (rc);
  3290.       }
  3291.       else if (rtyp == 'Z')
  3292.       {                         /* Empty file */
  3293.          debug (DB_LOG, "R_ATTR empty file", r->filename, 0);
  3294.          k->obufpos = 0;        /* Initialize output buffer */
  3295.          k->filename = r->filename;
  3296.          r->sofar = 0L;         /* Open and close the file */
  3297.          r->sofar_rumor = 0L;   /* Open and close the file */
  3298.          if ((rc = (*(k->openf)) (k, r->filename, 2)) == X_OK)
  3299.          {
  3300.             if (((rc = (*(k->closef)) (k, *pdf, 2)) == X_OK))
  3301.             {
  3302.                k->state = R_FILE;
  3303.                rc = ack (k, rseq, s);
  3304.             }
  3305.             else
  3306.             {
  3307.                debug (DB_MSG, "eksw Error closing empty file", 0, 0);
  3308.                epkt ("eksw Error closing empty file", k);
  3309.                return (rc);
  3310.             }
  3311.          }
  3312.          else
  3313.          {
  3314.             debug (DB_LOG, "eksw File refused or cannot be opened rc", 0, rc);
  3315.             epkt ("eksw File refused or cannot be opened", k);
  3316.             return (rc);
  3317.          }
  3318.          r->rstatus = k->state;
  3319.          return (X_OK);
  3320.       }
  3321.       else if (rtyp == 'F')     // received F pkt again
  3322.       {
  3323.          ack (k, rseq, (UCHAR *) 0);
  3324.          return (X_OK);
  3325.       }
  3326.       else
  3327.       {
  3328.          debug (DB_CHR, "R_ATTR error 3 unexpected packet rtyp", 0, rtyp);
  3329.          epkt ("eksw R_ATTR error 3 unexpected packet type", k);
  3330.          exit (1);
  3331.          return (X_ERROR);
  3332.       }
  3333.       break;
  3334.  
  3335.    case R_DATA:                /* Want a D or Z packet */
  3336.       debug (DB_CHR, "R_DATA rtyp", 0, rtyp);
  3337.       debug (DB_LOG, "R_DATA rseq", 0, rseq);
  3338.       if (rtyp == 'D')
  3339.       {                         /* Data */
  3340.          rc = handle_good_rpkt (k, r, rseq, pdf);
  3341.          debug (DB_LOG, "R_DATA hgr rc", 0, rc);
  3342.       }
  3343.       else if (rtyp == 'Z')
  3344.       {                         /* End of file */
  3345.          debug (DB_LOG, "R_DATA Z pkt obufpos", 0, k->obufpos);
  3346.          flush_to_file (k, r);
  3347.          if (((rc = (*(k->closef)) (k, *pdf, 2)) == X_OK) && (rc == X_OK))
  3348.             k->state = R_FILE;
  3349.          debug (DB_LOG, "R_DATA closef rc", 0, rc);
  3350.          r->rstatus = k->state;
  3351.       }
  3352.       else
  3353.       {
  3354.          debug (DB_CHR, "R_DATA error 4 unexpected packet rtyp", 0, rtyp);
  3355.          epkt ("eksw error 4 unexpected packet type", k);
  3356.          exit (1);
  3357.          return (X_ERROR);
  3358.       }
  3359.  
  3360.       if (rc == X_OK)
  3361.       {
  3362.          if (rtyp == 'Z')
  3363.          {
  3364.             rc = ack (k, rseq, s);      // usual ACK to D or Z packet
  3365.          }
  3366.       }
  3367.       else
  3368.       {
  3369.          debug (DB_CHR, "Error 12: rc != X_OK rtyp", 0, rtyp);
  3370.          epkt (rtyp == 'Z' ? "eksw Can't close file" : "eksw hgr failed", k);
  3371.       }
  3372.  
  3373.       return (rc);
  3374.  
  3375.    case R_ERROR:               /* Canceled from above */
  3376.       debug (DB_LOG, "R_ERROR error so sending E pkt k->state", 0, k->state);
  3377.    default:
  3378.       debug (DB_LOG, "  default error so sending E pkt k->state", 0,
  3379.              k->state);
  3380.       epkt (msg, k);
  3381.       return (X_ERROR);
  3382.    }
  3383.  
  3384.    // not supposed to get here
  3385.    debug (DB_LOG, "Kermit logic error k->state", 0, k->state);
  3386.    exit (1);
  3387.    return (X_ERROR);            /* make compiler happy */
  3388. }
  3389.