home *** CD-ROM | disk | FTP | other *** search
/ OpenStep 4.2J (Developer) / os42jdev.iso / NextDeveloper / Source / GNU / uucp / uucico / proty.c < prev    next >
C/C++ Source or Header  |  1995-08-20  |  17KB  |  661 lines

  1. /* proty.c
  2.    The 'y' protocol.
  3.  
  4.    Copyright (C) 1994, 1995 Jorge Cwik and Ian Lance Taylor
  5.  
  6.    This file is part of the Taylor UUCP package.
  7.  
  8.    This program is free software; you can redistribute it and/or
  9.    modify it under the terms of the GNU General Public License as
  10.    published by the Free Software Foundation; either version 2 of the
  11.    License, or (at your option) any later version.
  12.  
  13.    This program is distributed in the hope that it will be useful, but
  14.    WITHOUT ANY WARRANTY; without even the implied warranty of
  15.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  16.    General Public License for more details.
  17.  
  18.    You should have received a copy of the GNU General Public License
  19.    along with this program; if not, write to the Free Software
  20.    Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
  21.    */
  22.  
  23. #include "uucp.h"
  24.  
  25. #if USE_RCS_ID
  26. const char proty_id[] = "$Id: proty.c,v 1.4 1995/06/21 19:15:40 ian Rel $";
  27. #endif
  28.  
  29. #include "uudefs.h"
  30. #include "uuconf.h"
  31. #include "conn.h"
  32. #include "trans.h"
  33. #include "system.h"
  34. #include "prot.h"
  35.  
  36. /* The 'y' protocol, and this implementation, was written and designed
  37.    by Jorge Cwik <jorge@satlink.net>.  Some of the routines, and the
  38.    coding style in general, were taken verbatim or adapted from other
  39.    Taylor UUCP modules.  Mark Delany made the initial testings and
  40.    helped in portability issues.
  41.  
  42.    This protocol does not perform any kind of error correction or flow
  43.    control.  It does do error checking.  It does not require an end to
  44.    end reliable link.  It is recommended for error-free (also called
  45.    semi-reliable) connections as provided by error correcting modems.
  46.    It needs an eight bit clean channel and some kind of flow control
  47.    at the lower layers, typically RTS/CTS hardware flow control.
  48.  
  49.    The flow of the file transmission is completely unidirectional.
  50.    There are no ACKs or NAKs outside file boundaries. This makes it
  51.    very suitable for half duplex modulations (like PEP) and
  52.    connections with very long delays, like multihop satellite links.  */
  53.  
  54. /* This protocol uses 16 bit little-endian ints in the packet header.  */
  55. #define    FROMLITTLE(p)  (((p)[0] & 0xff) + (((p)[1] & 0xff) << 8))
  56. #define    TOLITTLE(p, i) ((p)[0] = (i) & 0xff, (p)[1] = ((i) >> 8) & 0xff)
  57.  
  58. /* The buffer and packet size we use.  */
  59. #define CYBUFSIZE (1024)
  60. #define IYPACKSIZE (1024)
  61.  
  62. /* The offset in the buffer to the data.  */
  63. #define CYFRAMELEN (6)
  64.  
  65. /* Offsets in a packet header.  */
  66. #define YFRAME_SEQ_OFF (0)
  67. #define YFRAME_LEN_OFF (2)
  68. #define YFRAME_CTL_OFF (2)
  69. #define YFRAME_CHK_OFF (4)
  70.  
  71. /* Offsets in a packet header viewed as an array of shorts.  */
  72. #define YFRAME_SEQ (0)
  73. #define YFRAME_LEN (1)
  74. #define YFRAME_CTL (1)
  75. #define YFRAME_CHK (2)
  76.  
  77. /* The default timeout.  */
  78. #define    CYTIMEOUT (60)
  79.  
  80. /* Control packet types.  */
  81. #define YPKT_ACK (0xFFFE)
  82. #define YPKT_ERR (0xFFFD)
  83. #define YPKT_BAD (0xFFFC)
  84.  
  85. /* The protocol version number.  */
  86. #define Y_VERSION (1)
  87.  
  88. /* When the protocol starts up, it transmit the following information:
  89.      1 byte version
  90.      1 byte packet size
  91.      2 byte flags (none currently defined)
  92.    Future revision may expand the structure as long as these members
  93.    keep their current offset.  */
  94. #define Y_INIT_HDR_LEN (4)
  95. #define Y_INIT_HDR_VERSION_OFF (0)
  96. #define Y_INIT_HDR_PKTSIZE_OFF (1)
  97. #define Y_INIT_HDR_FLAGS_OFF (2)
  98.  
  99. /* The initialization length of the lowest accepted version.  */
  100. #define MIN_Y_SYNC (4)
  101.  
  102. /* Not strictly needed, but I would not want to accept a 32k sync pkt.  */
  103. #define MAX_Y_SYNC IYPACKSIZE
  104.  
  105. /* Local and remote packet sizes (we actually use the same value for
  106.    both).  */
  107. static size_t iYlocal_packsize = IYPACKSIZE;
  108. static size_t iYremote_packsize = IYPACKSIZE;
  109.  
  110. /* Local and remote packet sequence numbers.  */
  111. static unsigned short iYlocal_pktnum;
  112. static unsigned short iYremote_pktnum;
  113.  
  114. /* The timeout.  */
  115. static int cYtimeout = CYTIMEOUT;
  116.  
  117. /* Transmitter buffer.  */
  118. static char *zYbuf;
  119.  
  120. /* Protocol parameters.  */
  121.  
  122. struct uuconf_cmdtab asYproto_params[] =
  123. {
  124.   { "timeout", UUCONF_CMDTABTYPE_INT, (pointer) &cYtimeout, NULL },
  125.   { "packet-size", UUCONF_CMDTABTYPE_INT, (pointer) &iYlocal_packsize, NULL },
  126.   { NULL, 0, NULL, NULL }
  127. };
  128.  
  129. /* Local functions.  */
  130.  
  131. static boolean fywait_for_packet P((struct sdaemon *qdaemon,
  132.                     boolean *pfexit));
  133. static unsigned short iychecksum P((const char *z, size_t c));
  134. static unsigned short iychecksum2 P((const char *zfirst, size_t cfirst,
  135.                      const char *zsecond, size_t csecond));
  136. static boolean fywait_for_header P((struct sdaemon *qdaemon,
  137.                     unsigned short header[3], int timeout));
  138. static boolean fysend_pkt P((struct sdaemon *qdaemon,
  139.                  const void *zdata, size_t cdata));
  140. static boolean fysend_control P((struct sdaemon *qdaemon,
  141.                  int itype));
  142. static boolean fyread_data P((struct sdaemon *qdaemon, size_t clen,
  143.                   int timeout));
  144.  
  145. /* Exchange sync packets at protocol startup.  */
  146.  
  147. static boolean
  148. fyxchg_syncs (qdaemon)
  149.      struct sdaemon *qdaemon;
  150. {
  151.   char inithdr[Y_INIT_HDR_LEN];
  152.   unsigned short header[3];
  153.   unsigned short ichk;
  154.   size_t clen, cfirst;
  155.   int rpktsize;
  156.  
  157.   /* Send our configuration.  We could use only one array (for local
  158.      and remote).  But this is safer in case the code changes and
  159.      depend on separate ones.  */
  160.  
  161.   inithdr[Y_INIT_HDR_VERSION_OFF] = Y_VERSION;
  162.   inithdr[Y_INIT_HDR_PKTSIZE_OFF] = iYlocal_packsize >> 8;
  163.   TOLITTLE (inithdr + Y_INIT_HDR_FLAGS_OFF, 0);
  164.  
  165.   if (! fysend_pkt (qdaemon, inithdr, Y_INIT_HDR_LEN))
  166.     return FALSE;
  167.  
  168.   if (! fywait_for_header (qdaemon, header, cYtimeout))
  169.     return FALSE;
  170.  
  171.   DEBUG_MESSAGE0 (DEBUG_UUCP_PROTO, "fyxchg_syncs: Got sync header");
  172.   clen = header[YFRAME_LEN];
  173.  
  174.   if (clen < MIN_Y_SYNC || clen > MAX_Y_SYNC)
  175.     {
  176.       ulog (LOG_ERROR, "Bad 'y' protocol sync packet length");
  177.       return FALSE;
  178.     }
  179.  
  180.   /* It may be better to integrate this code with fywait_for_packet.  */
  181.   if (! fyread_data (qdaemon, clen, cYtimeout))
  182.     return FALSE;
  183.  
  184.   cfirst = CRECBUFLEN - iPrecstart;
  185.   ichk = iychecksum2 (abPrecbuf + iPrecstart, cfirst,
  186.               abPrecbuf, clen - cfirst);
  187.  
  188.   rpktsize = BUCHAR (abPrecbuf[(iPrecstart + 1) % CRECBUFLEN]);
  189.  
  190.   /* Future versions of the protocol may need to check and react
  191.      according to the version number.  */
  192.  
  193.   if (rpktsize == 0 || header[YFRAME_CHK] != ichk)
  194.     {
  195.       ulog (LOG_ERROR, "Bad 'y' protocol sync packet");
  196.       return FALSE;
  197.     }
  198.  
  199.   iYremote_packsize = rpktsize << 8;
  200.  
  201.   /* Some may want to do this different and in effect the protocol
  202.      support this.  But I like the idea that the packet size would be
  203.      the same in both directions.  This allows the caller to select
  204.      both packet sizes without changing the configuration at the
  205.      server.  */
  206.   if (iYremote_packsize > iYlocal_packsize)
  207.     iYremote_packsize = iYlocal_packsize;
  208.  
  209.   iPrecstart = (iPrecstart + clen) % CRECBUFLEN;
  210.   return TRUE;
  211. }
  212.  
  213. /* Start the protocol.  */
  214.  
  215. boolean
  216. fystart (qdaemon, pzlog)
  217.      struct sdaemon *qdaemon;
  218.      char **pzlog;
  219. {
  220.   *pzlog = NULL;
  221.  
  222.   /* This should force, or at least enable if available, RTS/CTS
  223.      hardware flow control !! */
  224.  
  225.   /* The 'y' protocol requires an eight bit clean link */
  226.   if (! fconn_set (qdaemon->qconn, PARITYSETTING_NONE,
  227.            STRIPSETTING_EIGHTBITS, XONXOFF_OFF))
  228.     return FALSE;
  229.  
  230.   iYlocal_pktnum = iYremote_pktnum = 0;
  231.  
  232.   /* Only multiple of 256 sizes are allowed */
  233.   iYlocal_packsize &= ~0xff;
  234.   if (iYlocal_packsize < 256 || iYlocal_packsize > (16*1024))
  235.     iYlocal_packsize = IYPACKSIZE;
  236.  
  237.   /* Exhange SYNC packets */
  238.   if (! fyxchg_syncs (qdaemon))
  239.     {
  240.       /* Restore defaults */
  241.       cYtimeout = CYTIMEOUT;
  242.       iYlocal_packsize = IYPACKSIZE;
  243.       return FALSE;
  244.     }
  245.  
  246.   zYbuf = (char *) xmalloc (CYBUFSIZE + CYFRAMELEN);
  247.   return TRUE;
  248. }
  249.  
  250. /* Shutdown the protocol.  */
  251.  
  252. boolean 
  253. fyshutdown (qdaemon)
  254.      struct sdaemon *qdaemon;
  255. {
  256.   xfree ((pointer) zYbuf);
  257.   zYbuf = NULL;
  258.   cYtimeout = CYTIMEOUT;
  259.   iYlocal_packsize = IYPACKSIZE;
  260.   return TRUE;
  261. }
  262.  
  263. /* Send a command string.  We send packets containing the string until
  264.    the entire string has been sent, including the zero terminator.  */
  265.  
  266. /*ARGSUSED*/
  267. boolean
  268. fysendcmd (qdaemon, z, ilocal, iremote)
  269.      struct sdaemon *qdaemon;
  270.      const char *z;
  271.      int ilocal;
  272.      int iremote;
  273. {
  274.   size_t clen;
  275.  
  276.   DEBUG_MESSAGE1 (DEBUG_UUCP_PROTO, "fysendcmd: Sending command \"%s\"", z);
  277.  
  278.   clen = strlen (z) + 1;
  279.  
  280.   while (clen > 0)
  281.     {
  282.       size_t csize;
  283.  
  284.       csize = clen;
  285.       if (csize > iYremote_packsize)
  286.     csize = iYremote_packsize;
  287.  
  288.       if (! fysend_pkt (qdaemon, z, csize))
  289.     return FALSE;
  290.  
  291.       z += csize;
  292.       clen -= csize;
  293.     }
  294.  
  295.   return TRUE;
  296. }
  297.  
  298. /* Get space to be filled with data.  We always use zYbuf, which was
  299.    allocated from the heap.  */
  300.  
  301. char *
  302. zygetspace (qdaemon, pclen)
  303.      struct sdaemon *qdaemon;
  304.      size_t *pclen;
  305. {
  306.   *pclen = iYremote_packsize;
  307.   return zYbuf + CYFRAMELEN;
  308. }
  309.  
  310. /* Send out a data packet.  */
  311.  
  312. boolean
  313. fysenddata (qdaemon, zdata, cdata, ilocal, iremote, ipos)
  314.      struct sdaemon *qdaemon;
  315.      char *zdata;
  316.      size_t cdata;
  317.      int ilocal;
  318.      int iremote;
  319.      long ipos;
  320. {
  321. #if DEBUG > 0
  322.   if (cdata > iYremote_packsize)
  323.     ulog (LOG_FATAL, "fysend_packet: Packet size too large");
  324. #endif
  325.  
  326.   TOLITTLE (zYbuf + YFRAME_SEQ_OFF, iYlocal_pktnum);
  327.   ++iYlocal_pktnum;
  328.   TOLITTLE (zYbuf + YFRAME_LEN_OFF, cdata);
  329.   TOLITTLE (zYbuf + YFRAME_CHK_OFF, iychecksum (zdata, cdata));
  330.  
  331.   /* We pass FALSE to fsend_data since we don't expect the other side
  332.      to be sending us anything just now.  */
  333.   return fsend_data (qdaemon->qconn, zYbuf, cdata + CYFRAMELEN, FALSE);
  334. }
  335.  
  336. /* Wait for data to come in and process it until we've finished a
  337.    command or a file.  */
  338.  
  339. boolean
  340. fywait (qdaemon)
  341.      struct sdaemon *qdaemon;
  342. {
  343.   boolean fexit = FALSE;
  344.  
  345.   while (! fexit)
  346.     {
  347.       if (! fywait_for_packet (qdaemon, &fexit))
  348.     return FALSE;
  349.     }
  350.   return TRUE;
  351. }
  352.  
  353. /* File level routines
  354.    We could handle this inside the other public routines,
  355.    but this is cleaner and better for future expansions */
  356.  
  357. boolean
  358. fyfile (qdaemon, qtrans, fstart, fsend, cbytes, pfhandled)
  359.      struct sdaemon *qdaemon;
  360.      struct stransfer *qtrans;
  361.      boolean fstart;
  362.      boolean fsend;
  363.      long cbytes;
  364.      boolean *pfhandled;
  365. {
  366.   unsigned short header[3];
  367.  
  368.   *pfhandled = FALSE;
  369.  
  370.   if (! fstart)
  371.     {
  372.       if (fsend)
  373.     {
  374.       /* It is critical that the timeout here would be long
  375.          enough.  We have just sent a full file without any kind
  376.          of flow control at the protocol level.  The traffic may
  377.          be buffered in many places of the link, and the remote
  378.          may take a while until cathing up.  */
  379.       if (! fywait_for_header (qdaemon, header, cYtimeout * 2))
  380.         return FALSE;
  381.  
  382.       if (header[YFRAME_CTL] != (unsigned short) YPKT_ACK)
  383.         {
  384.           DEBUG_MESSAGE1 (DEBUG_PROTO | DEBUG_ABNORMAL,
  385.                   "fyfile: Error from remote: 0x%04X", header[1]);
  386.           ulog (LOG_ERROR, "Received 'y' protocol error from remote");
  387.           return FALSE;
  388.         }
  389.     }
  390.       else
  391.     {
  392.       /* This is technically not requireed.  But I've put this in
  393.          the protocol to allow easier expansions.  */
  394.       return fysend_control (qdaemon, YPKT_ACK);
  395.     }
  396.     }
  397.  
  398.   return TRUE;
  399. }
  400.  
  401. /* Send a control packet, not used during the normal file
  402.    transmission.  */
  403.  
  404. static boolean
  405. fysend_control (qdaemon, itype)
  406.      struct sdaemon *qdaemon;
  407.      int itype;
  408. {
  409.   char header[CYFRAMELEN];
  410.  
  411.   TOLITTLE (header + YFRAME_SEQ_OFF, iYlocal_pktnum);
  412.   iYlocal_pktnum++;
  413.   TOLITTLE (header + YFRAME_CTL_OFF, itype);
  414.   TOLITTLE (header + YFRAME_CHK_OFF, 0);
  415.  
  416.   return fsend_data (qdaemon->qconn, header, CYFRAMELEN, FALSE);
  417. }
  418.  
  419. /* Private function to send a packet.  This one doesn't need the data
  420.    to be in the buffer provided by zygetspace.  I've found it worth
  421.    for avoiding memory copies.  Somebody may want to do it otherwise */
  422.  
  423. static boolean
  424. fysend_pkt (qdaemon, zdata, cdata)
  425.      struct sdaemon *qdaemon;
  426.      const void *zdata;
  427.      size_t cdata;
  428. {
  429.   char header[CYFRAMELEN];
  430.  
  431.   TOLITTLE (header + YFRAME_SEQ_OFF, iYlocal_pktnum);
  432.   iYlocal_pktnum++;
  433.   TOLITTLE (header + YFRAME_LEN_OFF, cdata);
  434.   TOLITTLE (header + YFRAME_CHK_OFF, iychecksum (zdata, cdata));
  435.  
  436.   if (! fsend_data (qdaemon->qconn, header, CYFRAMELEN, FALSE))
  437.     return FALSE;
  438.   return fsend_data (qdaemon->qconn, zdata, cdata, FALSE);
  439. }
  440.  
  441. /* Wait until enough data arrived from the comm line.  This protocol
  442.    doesn't need to perform any kind of action while waiting.  */
  443.  
  444. static boolean
  445. fyread_data (qdaemon, clen, timeout)
  446.      struct sdaemon *qdaemon;
  447.      size_t clen;
  448.      int timeout;
  449. {
  450.   int cinbuf;
  451.   size_t crec;
  452.  
  453.   cinbuf = iPrecend - iPrecstart;
  454.   if (cinbuf < 0)
  455.     cinbuf += CRECBUFLEN;
  456.  
  457.   if (cinbuf < clen)
  458.     {
  459.       if (! freceive_data (qdaemon->qconn, clen - cinbuf, &crec,
  460.                timeout, TRUE))
  461.     return FALSE;
  462.       cinbuf += crec;
  463.       if (cinbuf < clen)
  464.     {
  465.       if (! freceive_data (qdaemon->qconn, clen - cinbuf, &crec,
  466.                    timeout, TRUE))
  467.         return FALSE;
  468.     }
  469.       cinbuf += crec;
  470.       if (cinbuf < clen)
  471.     {
  472.       ulog (LOG_ERROR, "Timed out waiting for data");
  473.       return FALSE;
  474.     }
  475.     }
  476.  
  477.   return TRUE;
  478. }
  479.  
  480. /* Receive a remote packet header, check for correct sequence number.  */
  481.  
  482. static boolean
  483. fywait_for_header (qdaemon, header, timeout)
  484.      struct sdaemon *qdaemon;
  485.      unsigned short header[3];
  486.      int timeout;
  487. {
  488.   if (! fyread_data (qdaemon, CYFRAMELEN, timeout))
  489.     return FALSE;
  490.  
  491.   /* Somebody may want to optimize this in a portable way.  I'm not
  492.      sure it's worth, but the output by gcc for the portable construct
  493.      is so bad (even with optimization), that I couldn't resist.  */
  494.  
  495.   if (iPrecstart <= (CRECBUFLEN - CYFRAMELEN))
  496.     {
  497.       header[0] = FROMLITTLE (abPrecbuf + iPrecstart);
  498.       header[1] = FROMLITTLE (abPrecbuf + iPrecstart + 2);
  499.       header[2] = FROMLITTLE (abPrecbuf + iPrecstart + 4);
  500.     }
  501.   else
  502.     {
  503.       register int i, j;
  504.  
  505.       for (i = j = 0; j < CYFRAMELEN; i++, j += 2)
  506.     {
  507.       header[i] =
  508.         (((abPrecbuf[(iPrecstart + j + 1) % CRECBUFLEN] & 0xff) << 8)
  509.          + (abPrecbuf[(iPrecstart + j) % CRECBUFLEN] & 0xff));
  510.     }
  511.     }
  512.  
  513.   iPrecstart = (iPrecstart + CYFRAMELEN) % CRECBUFLEN;
  514.  
  515.   DEBUG_MESSAGE3 (DEBUG_UUCP_PROTO,
  516.           "fywait_for_header: Got header: 0x%04X, 0x%04X, 0x%04X",
  517.           header[0], header[1], header[2]);
  518.  
  519.   if (header[YFRAME_SEQ] != iYremote_pktnum++)
  520.     {
  521.       ulog (LOG_ERROR, "Incorrect 'y' packet sequence");
  522.       fysend_control (qdaemon, YPKT_BAD);
  523.       return FALSE;
  524.     }
  525.  
  526.   return TRUE;
  527. }
  528.  
  529. /* Receive a remote data packet */
  530.  
  531. static boolean
  532. fywait_for_packet (qdaemon, pfexit)
  533.      struct sdaemon *qdaemon;
  534.      boolean *pfexit;
  535. {
  536.   unsigned short header[3], ichk;
  537.   size_t clen, cfirst;
  538.  
  539.   if (! fywait_for_header (qdaemon, header, cYtimeout))
  540.     return FALSE;
  541.  
  542.   clen = header[YFRAME_LEN];
  543.   if (clen == 0 && pfexit != NULL)
  544.     {
  545.       /* I Suppose the pointers could be NULL ??? */
  546.       return fgot_data (qdaemon, abPrecbuf, 0, abPrecbuf, 0,
  547.             -1, -1, (long) -1, TRUE, pfexit);
  548.     }
  549.  
  550.   if (clen & 0x8000)
  551.     {
  552.       DEBUG_MESSAGE1 (DEBUG_PROTO | DEBUG_ABNORMAL,
  553.               "fywait_for_packet: Error from remote: 0x%04X",
  554.               header[YFRAME_CTL]);
  555.       ulog (LOG_ERROR, "Remote error packet");
  556.       return FALSE;
  557.     }
  558.  
  559.   /* This is really not neccessary.  But if this check is removed,
  560.      take in mind that the packet may be up to 32k long.  */
  561.   if (clen > iYlocal_packsize)
  562.     {
  563.       ulog (LOG_ERROR, "Packet too large");
  564.       return FALSE;
  565.     }
  566.  
  567.   if (! fyread_data (qdaemon, clen, cYtimeout))
  568.     return FALSE;
  569.  
  570.   cfirst = CRECBUFLEN - iPrecstart;
  571.   if (cfirst > clen)
  572.     cfirst = clen;
  573.  
  574.   if (cfirst == clen)
  575.     ichk = iychecksum (abPrecbuf + iPrecstart, clen);
  576.   else
  577.     ichk = iychecksum2 (abPrecbuf + iPrecstart, cfirst,
  578.             abPrecbuf, clen - cfirst);
  579.   if (header[YFRAME_CHK] != ichk)
  580.     {
  581.       DEBUG_MESSAGE2 (DEBUG_PROTO | DEBUG_ABNORMAL,
  582.               "fywait_for_packet: Bad checksum 0x%x != 0x%x",
  583.               header[YFRAME_CHK], ichk);
  584.       fysend_control (qdaemon, YPKT_ERR);
  585.       ulog (LOG_ERROR, "Checksum error");
  586.       return FALSE;
  587.     }
  588.  
  589.   if (pfexit != NULL
  590.       && ! fgot_data (qdaemon, abPrecbuf + iPrecstart, cfirst, 
  591.               abPrecbuf, clen - cfirst,
  592.               -1, -1, (long) -1, TRUE, pfexit))
  593.     return FALSE;
  594.  
  595.   iPrecstart = (iPrecstart + clen) % CRECBUFLEN;
  596.  
  597.   return TRUE;
  598. }
  599.  
  600. /* Compute 16 bit checksum */
  601.  
  602. #ifdef __GNUC__
  603. #ifdef __i386__
  604. #define I386_ASM
  605. #endif
  606. #endif
  607.  
  608. #ifdef I386_ASM
  609. #define ROTATE(i) \
  610.     asm ("rolw $1,%0" : "=g" (i) : "g" (i))
  611. #else
  612. #define ROTATE(i) i += i + ((i & 0x8000) >> 15)
  613. #endif
  614.  
  615. static unsigned short
  616. iychecksum (z, c)
  617.      register const char *z;
  618.      register size_t c;
  619. {
  620.   register unsigned short ichk;
  621.  
  622.   ichk = 0xffff;
  623.  
  624.   while (c-- > 0)
  625.     {
  626.       ROTATE (ichk);
  627.       ichk += BUCHAR (*z++);
  628.     }
  629.  
  630.   return ichk;
  631. }
  632.  
  633. static unsigned short
  634. iychecksum2 (zfirst, cfirst, zsecond, csecond)
  635.     const char *zfirst;
  636.     size_t cfirst;
  637.     const char *zsecond;
  638.     size_t csecond;
  639. {
  640.   register unsigned short ichk;
  641.   register const char *z;
  642.   register size_t c;
  643.  
  644.   z = zfirst;
  645.   c = cfirst + csecond;
  646.  
  647.   ichk = 0xffff;
  648.  
  649.   while (c-- > 0)
  650.     {
  651.       ROTATE (ichk);
  652.       ichk += BUCHAR (*z++);
  653.  
  654.       /* If the first buffer has been finished, switch to the second.  */
  655.       if (--cfirst == 0)
  656.     z = zsecond;
  657.     }
  658.  
  659.   return ichk;
  660. }
  661.