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

  1. /* protj.c
  2.    The 'j' protocol.
  3.  
  4.    Copyright (C) 1992, 1994 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.    The author of the program may be contacted at ian@airs.com or
  23.    c/o Cygnus Support, 48 Grove Street, Somerville, MA 02144.
  24.    */
  25.  
  26. #include "uucp.h"
  27.  
  28. #if USE_RCS_ID
  29. const char protj_rcsid[] = "$Id: protj.c,v 1.6 1995/06/21 19:15:32 ian Rel $";
  30. #endif
  31.  
  32. #include <ctype.h>
  33. #include <errno.h>
  34.  
  35. #include "uudefs.h"
  36. #include "uuconf.h"
  37. #include "conn.h"
  38. #include "trans.h"
  39. #include "system.h"
  40. #include "prot.h"
  41.  
  42. /* The 'j' protocol.
  43.  
  44.    The 'j' protocol is a wrapper around the 'i' protocol, which avoids
  45.    the use of certain characters, such as XON and XOFF.
  46.  
  47.    Each 'j' protocol packet begins with a '^' character, followed by a
  48.    two byte encoded size giving the total number of bytes in the
  49.    packet.  The first byte is HIGH, the second byte is LOW, and the
  50.    number of bytes is (HIGH - 32) * 64 + (LOW - 32), where 32 <= HIGH
  51.    < 127 and 32 <= LOW < 96 (i.e., HIGH and LOW are printable ASCII
  52.    characters).  This is followed by a '=' character.  The next two
  53.    bytes are the number of data bytes in the packet, using the same
  54.    encoding.  This is followed by a '@' character, and then that
  55.    number of data bytes.  The remaining bytes in the packet are
  56.    indices of bytes which must be transformed, followed by a trailing
  57.    '~' character.  The indices are encoded in the following overly
  58.    complex format.
  59.  
  60.    Each byte index is two bytes long.  The first byte in the index is
  61.    INDEX-HIGH and the second is INDEX-LOW.  If 32 <= INDEX-HIGH < 126,
  62.    the byte index refers to the byte at position (INDEX-HIGH - 32) *
  63.    32 + INDEX-LOW % 32 in the actual data, where 32 <= INDEX-LOW <
  64.    127.  If 32 <= INDEX-LOW < 64, then 128 must be added to the
  65.    indexed byte.  If 64 <= INDEX-LOW < 96, then the indexed byte must
  66.    be exclusive or'red with 32.  If 96 <= INDEX-LOW < 127, both
  67.    operations must be performed.  If INDEX-HIGH == 126, then the byte
  68.    index refers to the byte at position (INDEX-LOW - 32) * 32 + 31,
  69.    where 32 <= INDEX-LOW < 126.  128 must be added to the byte, and it
  70.    must be exclusive or'red with 32.  This unfortunately requires a
  71.    special test (when encoding INDEX-LOW must be checked for 127; when
  72.    decoding INDEX-HIGH must be checked for 126).  It does, however,
  73.    permit the byte indices field to consist exclusively of printable
  74.    ASCII characters.
  75.  
  76.    The maximum value for a byte index is (125 - 32) * 32 + 31 == 3007,
  77.    so the is the maximum number of data bytes permitted.  Since it is
  78.    convenient to have each 'j' protocol packet correspond to each 'i'
  79.    protocol packet, we restrict the 'i' protocol accordingly.
  80.  
  81.    Note that this encoding method assumes that we can send all
  82.    printable ASCII characters.  */
  83.  
  84. /* The first byte of each packet.  I just picked these values
  85.    randomly, trying to get characters that were perhaps slightly less
  86.    likely to appear in normal text.  */
  87. #define FIRST '\136'
  88.  
  89. /* The fourth byte of each packet.  */
  90. #define FOURTH '\075'
  91.  
  92. /* The seventh byte of each packet.  */
  93. #define SEVENTH '\100'
  94.  
  95. /* The trailing byte of each packet.  */
  96. #define TRAILER '\176'
  97.  
  98. /* The length of the header.  */
  99. #define CHDRLEN (7)
  100.  
  101. /* Get a number of bytes encoded in a two byte length at the start of
  102.    a packet.  */
  103. #define CGETLENGTH(b1, b2) (((b1) - 32) * 64 + ((b2) - 32))
  104.  
  105. /* Set the high and low bytes of a two byte length at the start of a
  106.    packet.  */
  107. #define ISETLENGTH_FIRST(i) ((i) / 64 + 32)
  108. #define ISETLENGTH_SECOND(i) ((i) % 64 + 32)
  109.  
  110. /* The maximum packet size we support, as determined by the byte
  111.    indices.  */
  112. #define IMAXPACKSIZE ((125 - 32) * 32 + 31)
  113.  
  114. /* Amount to offset the bytes in the byte index by.  */
  115. #define INDEX_OFFSET (32)
  116.  
  117. /* Maximum value of INDEX-LOW, before offsetting.  */
  118. #define INDEX_MAX_LOW (32)
  119.  
  120. /* Maximum value of INDEX-HIGH, before offsetting. */
  121. #define INDEX_MAX_HIGH (94)
  122.  
  123. /* The set of characters to avoid.  */
  124. static char *zJavoid;
  125.  
  126. /* The number of characters to avoid.  */
  127. static size_t cJavoid;
  128.  
  129. /* A buffer used when sending data.  */
  130. static char *zJbuf;
  131.  
  132. /* The end of the undecoded data in abPrecbuf.  */
  133. static int iJrecend;
  134.  
  135. /* Local functions.  */
  136. static boolean fjsend_data P((struct sconnection *qconn, const char *zsend,
  137.                   size_t csend, boolean fdoread));
  138. static boolean fjreceive_data P((struct sconnection *qconn, size_t cneed,
  139.                  size_t *pcrec, int ctimeout,
  140.                  boolean freport));
  141. static boolean fjprocess_data P((size_t *pcneed));
  142.  
  143. /* Start the protocol.  We first send over the list of characters to
  144.    avoid as an escape sequence, starting with FIRST and ending with
  145.    TRAILER.  There is no error checking done on this string.  */
  146.  
  147. boolean
  148. fjstart (qdaemon, pzlog)
  149.      struct sdaemon *qdaemon;
  150.      char **pzlog;
  151. {
  152.   size_t clen;
  153.   char *zsend;
  154.   int b;
  155.   size_t cbuf, cgot;
  156.   char *zbuf;
  157.   int i;
  158.  
  159.   /* Send the characters we want to avoid to the other side.  */
  160.   clen = strlen (zJavoid_parameter);
  161.   zsend = zbufalc (clen + 3);
  162.   zsend[0] = FIRST;
  163.   memcpy (zsend + 1, zJavoid_parameter, clen);
  164.   zsend[clen + 1] = TRAILER;
  165.   zsend[clen + 2] = '\0';
  166.   if (! fsend_data (qdaemon->qconn, zsend, clen + 2, TRUE))
  167.     {
  168.       ubuffree (zsend);
  169.       return FALSE;
  170.     }
  171.   ubuffree (zsend);
  172.  
  173.   /* Read the characters the other side wants to avoid.  */
  174.   while ((b = breceive_char (qdaemon->qconn, cIsync_timeout, TRUE))
  175.      != FIRST)
  176.     {
  177.       if (b < 0)
  178.     {
  179.       if (b == -1)
  180.         ulog (LOG_ERROR, "Timed out in 'j' protocol startup");
  181.       return FALSE;
  182.     }
  183.     }
  184.  
  185.   cbuf = 20;
  186.   zbuf = zbufalc (cbuf);
  187.   cgot = 0;
  188.   while ((b = breceive_char (qdaemon->qconn, cIsync_timeout, TRUE))
  189.      != TRAILER)
  190.     {
  191.       if (b < 0)
  192.     {
  193.       ubuffree (zbuf);
  194.       if (b == -1)
  195.         ulog (LOG_ERROR, "Timed out in 'j' protocol startup");
  196.       return FALSE;
  197.     }
  198.       if (cgot + 1 >= cbuf)
  199.     {
  200.       char *znew;
  201.  
  202.       cbuf += 20;
  203.       znew = zbufalc (cbuf);
  204.       memcpy (znew, zbuf, cgot);
  205.       ubuffree (zbuf);
  206.       zbuf = znew;
  207.     }
  208.       zbuf[cgot] = b;
  209.       ++cgot;
  210.     }
  211.   zbuf[cgot] = '\0';
  212.  
  213.   /* Merge the local and remote avoid bytes into one list, translated
  214.      into bytes.  */
  215.   cgot = cescape (zbuf);
  216.  
  217.   clen = strlen (zJavoid_parameter);
  218.   zJavoid = zbufalc (clen + cgot + 1);
  219.   memcpy (zJavoid, zJavoid_parameter, clen + 1);
  220.   cJavoid = cescape (zJavoid);
  221.  
  222.   for (i = 0; i < cgot; i++)
  223.     {
  224.       if (memchr (zJavoid, zbuf[i], cJavoid) == NULL)
  225.     {
  226.       zJavoid[cJavoid] = zbuf[i];
  227.       ++cJavoid;
  228.     }
  229.     }
  230.  
  231.   ubuffree (zbuf);
  232.  
  233.   /* We can't avoid ASCII printable characters, since the encoding
  234.      method assumes that they can always be sent.  If it ever turns
  235.      out to be important, a different encoding method could be used,
  236.      perhaps keyed by a different FIRST character.  */
  237.   if (cJavoid == 0)
  238.     {
  239.       ulog (LOG_ERROR, "No characters to avoid in 'j' protocol");
  240.       return FALSE;
  241.     }
  242.   for (i = 0; i < cJavoid; i++)
  243.     {
  244.       if (zJavoid[i] >= 32 && zJavoid[i] <= 126)
  245.     {
  246.       ulog (LOG_ERROR, "'j' protocol can't avoid character '\\%03o'",
  247.         zJavoid[i]);
  248.       return FALSE;
  249.     }
  250.     }
  251.  
  252.   /* If we are avoiding XON and XOFF, use XON/XOFF handshaking.  */
  253.   if (memchr (zJavoid, '\021', cJavoid) != NULL
  254.       && memchr (zJavoid, '\023', cJavoid) != NULL)
  255.     {
  256.       if (! fconn_set (qdaemon->qconn, PARITYSETTING_NONE,
  257.                STRIPSETTING_EIGHTBITS, XONXOFF_ON))
  258.     return FALSE;
  259.     }
  260.  
  261.   /* Let the port settle.  */
  262.   usysdep_sleep (2);
  263.  
  264.   /* Allocate a buffer we use when sending data.  We will probably
  265.      never actually need one this big; if this code is ported to a
  266.      computer with small amounts of memory, this should be changed to
  267.      increase the buffer size as needed.  */
  268.   zJbuf = zbufalc (CHDRLEN + IMAXPACKSIZE * 3 + 1);
  269.   zJbuf[0] = FIRST;
  270.   zJbuf[3] = FOURTH;
  271.   zJbuf[6] = SEVENTH;
  272.  
  273.   /* iJrecend is the end of the undecoded data, and iPrecend is the
  274.      end of the decoded data.  At this point there is no decoded data,
  275.      and we must initialize the variables accordingly.  */
  276.   iJrecend = iPrecend;
  277.   iPrecend = iPrecstart;
  278.  
  279.   /* Now do the 'i' protocol startup.  */
  280.   return fijstart (qdaemon, pzlog, IMAXPACKSIZE, fjsend_data,
  281.            fjreceive_data);
  282. }
  283.  
  284. /* Shut down the protocol.  */
  285.  
  286. boolean
  287. fjshutdown (qdaemon)
  288.      struct sdaemon *qdaemon;
  289. {
  290.   boolean fret;
  291.  
  292.   fret = fishutdown (qdaemon);
  293.   ubuffree (zJavoid);
  294.   ubuffree (zJbuf);
  295.   return fret;
  296. }
  297.  
  298. /* Encode a packet of data and send it.  This copies the data, which
  299.    is a waste of time, but calling fsend_data three times (for the
  300.    header, the body, and the trailer) would waste even more time.  */
  301.  
  302. static boolean
  303. fjsend_data (qconn, zsend, csend, fdoread)
  304.      struct sconnection *qconn;
  305.      const char *zsend;
  306.      size_t csend;
  307.      boolean fdoread;
  308. {
  309.   char *zput, *zindex;
  310.   const char *zfrom, *zend;
  311.   char bfirst, bsecond;
  312.   int iprecendhold;
  313.   boolean fret;
  314.  
  315.   zput = zJbuf + CHDRLEN;
  316.   zindex = zput + csend;
  317.   zfrom = zsend;
  318.   zend = zsend + csend;
  319.  
  320.   /* Optimize for the common case of avoiding two characters.  */
  321.   bfirst = zJavoid[0];
  322.   if (cJavoid <= 1)
  323.     bsecond = bfirst;
  324.   else
  325.     bsecond = zJavoid[1];
  326.   while (zfrom < zend)
  327.     {
  328.       char b;
  329.       boolean f128, f32;
  330.       int i, ihigh, ilow;
  331.  
  332.       b = *zfrom++;
  333.       if (b != bfirst && b != bsecond)
  334.     {
  335.       int ca;
  336.       char *za;
  337.  
  338.       if (cJavoid <= 2)
  339.         {
  340.           *zput++ = b;
  341.           continue;
  342.         }
  343.  
  344.       ca = cJavoid - 2;
  345.       za = zJavoid + 2;
  346.       while (ca-- != 0)
  347.         if (*za++ == b)
  348.           break;
  349.  
  350.       if (ca < 0)
  351.         {
  352.           *zput++ = b;
  353.           continue;
  354.         }
  355.     }
  356.  
  357.       if ((b & 0x80) == 0)
  358.     f128 = FALSE;
  359.       else
  360.     {
  361.       b &=~ 0x80;
  362.       f128 = TRUE;
  363.     }
  364.       if (b >= 32 && b != 127)
  365.     f32 = FALSE;
  366.       else
  367.     {
  368.       b ^= 0x20;
  369.       f32 = TRUE;
  370.     }
  371.  
  372.       /* We must now put the byte index into the buffer.  The byte
  373.      index is encoded similarly to the length of the actual data,
  374.      but the byte index also encodes the operations that must be
  375.      performed on the byte.  The first byte in the index is the
  376.      most significant bits.  If we only had to subtract 128 from
  377.      the byte, we use the second byte directly.  If we had to xor
  378.      the byte with 32, we add 32 to the second byte index.  If we
  379.      had to perform both operations, we add 64 to the second byte
  380.      index.  However, if we had to perform both operations, and
  381.      the second byte index was 31, then after adding 64 and
  382.      offsetting by 32 we would come up with 127, which we are not
  383.      permitted to use.  Therefore, in this special case we set the
  384.      first byte of the index to 126 and put the original first
  385.      byte into the second byte position instead.  This is why we
  386.      could not permit the high byte of the length of the actual
  387.      data to be 126.  We can get away with the switch because both
  388.      the value of the second byte index (31) and the operations to
  389.      perform (both) are known.  */
  390.       i = zput - (zJbuf + CHDRLEN);
  391.       ihigh = i / INDEX_MAX_LOW;
  392.       ilow = i % INDEX_MAX_LOW;
  393.  
  394.       if (f128 && ! f32)
  395.     ;
  396.       else if (f32 && ! f128)
  397.     ilow += INDEX_MAX_LOW;
  398.       else
  399.     {
  400.       /* Both operations had to be performed.  */
  401.       if (ilow != INDEX_MAX_LOW - 1)
  402.         ilow += 2 * INDEX_MAX_LOW;
  403.       else
  404.         {
  405.           ilow = ihigh;
  406.           ihigh = INDEX_MAX_HIGH;
  407.         }
  408.     }
  409.  
  410.       *zindex++ = ihigh + INDEX_OFFSET;
  411.       *zindex++ = ilow + INDEX_OFFSET;
  412.       *zput++ = b;
  413.     }
  414.  
  415.   *zindex++ = TRAILER;
  416.  
  417.   /* Set the lengths into the buffer.  zJbuf[0,3,6] were set when
  418.      zJbuf was allocated, and are never changed thereafter.  */
  419.   zJbuf[1] = ISETLENGTH_FIRST (zindex - zJbuf);
  420.   zJbuf[2] = ISETLENGTH_SECOND (zindex - zJbuf);
  421.   zJbuf[4] = ISETLENGTH_FIRST (csend);
  422.   zJbuf[5] = ISETLENGTH_SECOND (csend);
  423.  
  424.   /* Send the data over the line.  We must preserve iPrecend as
  425.      discussed in fjreceive_data.  */
  426.   iprecendhold = iPrecend;
  427.   iPrecend = iJrecend;
  428.   fret = fsend_data (qconn, zJbuf, (size_t) (zindex - zJbuf), fdoread);
  429.   iJrecend = iPrecend;
  430.   iPrecend = iprecendhold;
  431.  
  432.   /* Process any bytes that may have been placed in abPrecbuf.  */
  433.   if (fret && iPrecend != iJrecend)
  434.     {
  435.       if (! fjprocess_data ((size_t *) NULL))
  436.     return FALSE;
  437.     }
  438.  
  439.   return fret;
  440. }
  441.  
  442. /* Receive and decode data.  This is called by fiwait_for_packet.  We
  443.    need to be able to return decoded data between iPrecstart and
  444.    iPrecend, while not losing any undecoded partial packets we may
  445.    have read.  We use iJrecend as a pointer to the end of the
  446.    undecoded data, and set iPrecend for the decoded data.  iPrecend
  447.    points to the start of the undecoded data.  */
  448.  
  449. static boolean
  450. fjreceive_data (qconn, cineed, pcrec, ctimeout, freport)
  451.      struct sconnection *qconn;
  452.      size_t cineed;
  453.      size_t *pcrec;
  454.      int ctimeout;
  455.      boolean freport;
  456. {
  457.   int iprecendstart;
  458.   size_t cjneed;
  459.   size_t crec;
  460.   int cnew;
  461.  
  462.   iprecendstart = iPrecend;
  463.  
  464.   /* Figure out how many bytes we need to decode the next packet.  */
  465.   if (! fjprocess_data (&cjneed))
  466.     return FALSE;
  467.  
  468.   /* As we long as we read some data but don't have enough to decode a
  469.      packet, we try to read some more.  We decrease the timeout each
  470.      time so that we will not wait forever if the connection starts
  471.      dribbling data.  */
  472.   do
  473.     {
  474.       int iprecendhold;
  475.       size_t cneed;
  476.  
  477.       if (cjneed > cineed)
  478.     cneed = cjneed;
  479.       else
  480.     cneed = cineed;
  481.  
  482.       /* We are setting iPrecend to the end of the decoded data for
  483.      the 'i' protocol.  When we do the actual read, we have to set
  484.      it to the end of the undecoded data so that any undecoded
  485.      data we have received is not overwritten.  */
  486.       iprecendhold = iPrecend;
  487.       iPrecend = iJrecend;
  488.       if (! freceive_data (qconn, cneed, &crec, ctimeout, freport))
  489.     return FALSE;
  490.       iJrecend = iPrecend;
  491.       iPrecend = iprecendhold;
  492.  
  493.       /* Process any data we have received.  This will set iPrecend to
  494.      the end of the new decoded data.  */
  495.       if (! fjprocess_data (&cjneed))
  496.     return FALSE;
  497.  
  498.       cnew = iPrecend - iprecendstart;
  499.       if (cnew < 0)
  500.     cnew += CRECBUFLEN;
  501.  
  502.       if (cnew > cineed)
  503.     cineed = 0;
  504.       else
  505.     cineed -= cnew;
  506.  
  507.       --ctimeout;
  508.     }
  509.   while (cnew == 0 && crec > 0 && ctimeout > 0);
  510.  
  511.   DEBUG_MESSAGE1 (DEBUG_PROTO, "fjreceive_data: Got %d decoded bytes",
  512.           cnew);
  513.  
  514.   *pcrec = cnew;
  515.   return TRUE;
  516. }
  517.  
  518. /* Decode the data in the buffer, optionally returning the number of
  519.    bytes needed to complete the next packet.  */
  520.  
  521. static boolean
  522. fjprocess_data (pcneed)
  523.      size_t *pcneed;
  524. {
  525.   int istart;
  526.  
  527.   istart = iPrecend;
  528.   while (istart != iJrecend)
  529.     {
  530.       int i, iget;
  531.       char ab[CHDRLEN];
  532.       int cpacket, cdata, chave;
  533.       int iindex, iendindex;
  534.  
  535.       /* Find the next occurrence of FIRST.  If we have to skip some
  536.      garbage bytes to get to it, zero them out (so they don't
  537.      confuse the 'i' protocol) and advance iPrecend.  This will
  538.      save us from looking at them again.  */
  539.       if (abPrecbuf[istart] != FIRST)
  540.     {
  541.       int cintro;
  542.       char *zintro;
  543.       size_t cskipped;
  544.  
  545.       cintro = iJrecend - istart;
  546.       if (cintro < 0)
  547.         cintro = CRECBUFLEN - istart;
  548.  
  549.       zintro = memchr (abPrecbuf + istart, FIRST, (size_t) cintro);
  550.       if (zintro == NULL)
  551.         {
  552.           bzero (abPrecbuf + istart, (size_t) cintro);
  553.           istart = (istart + cintro) % CRECBUFLEN;
  554.           iPrecend = istart;
  555.           continue;
  556.         }
  557.  
  558.       cskipped = zintro - (abPrecbuf + istart);
  559.       bzero (abPrecbuf + istart, cskipped);
  560.       istart += cskipped;
  561.       iPrecend = istart;
  562.     }
  563.  
  564.       for (i = 0, iget = istart;
  565.        i < CHDRLEN && iget != iJrecend;
  566.        ++i, iget = (iget + 1) % CRECBUFLEN)
  567.     ab[i] = abPrecbuf[iget];
  568.  
  569.       if (i < CHDRLEN)
  570.     {
  571.       if (pcneed != NULL)
  572.         *pcneed = CHDRLEN - i;
  573.       return TRUE;
  574.     }
  575.  
  576.       cpacket = CGETLENGTH (ab[1], ab[2]);
  577.       cdata = CGETLENGTH (ab[4], ab[5]);
  578.  
  579.       /* Make sure the header has the right magic characters, that the
  580.      data is not larger than the packet, and that we have an even
  581.      number of byte index characters.  */
  582.       if (ab[3] != FOURTH
  583.       || ab[6] != SEVENTH
  584.       || cdata > cpacket - CHDRLEN - 1
  585.       || (cpacket - cdata - CHDRLEN - 1) % 2 == 1)
  586.     {
  587.       istart = (istart + 1) % CRECBUFLEN;
  588.       continue;
  589.     }
  590.  
  591.       chave = iJrecend - istart;
  592.       if (chave < 0)
  593.     chave += CRECBUFLEN;
  594.  
  595.       if (chave < cpacket)
  596.     {
  597.       if (pcneed != NULL)
  598.         *pcneed = cpacket - chave;
  599.       return TRUE;
  600.     }
  601.  
  602.       /* Figure out where the byte indices start and end.  */
  603.       iindex = (istart + CHDRLEN + cdata) % CRECBUFLEN;
  604.       iendindex = (istart + cpacket - 1) % CRECBUFLEN;
  605.  
  606.       /* Make sure the magic trailer character is there.  */
  607.       if (abPrecbuf[iendindex] != TRAILER)
  608.     {
  609.       istart = (istart + 1) % CRECBUFLEN;
  610.       continue;
  611.     }
  612.  
  613.       /* We have a packet to decode.  The decoding process is simpler
  614.      than the encoding process, since all we have to do is examine
  615.      the byte indices.  We zero out the byte indices as we go, so
  616.      that they will not confuse the 'i' protocol.  */
  617.       while (iindex != iendindex)
  618.     {
  619.       int ihigh, ilow;
  620.       boolean f32, f128;
  621.       int iset;
  622.  
  623.       ihigh = abPrecbuf[iindex] - INDEX_OFFSET;
  624.       abPrecbuf[iindex] = 0;
  625.       iindex = (iindex + 1) % CRECBUFLEN;
  626.       ilow = abPrecbuf[iindex] - INDEX_OFFSET;
  627.       abPrecbuf[iindex] = 0;
  628.       iindex = (iindex + 1) % CRECBUFLEN;
  629.  
  630.       /* Now we must undo the encoding, by adding 128 and xoring
  631.          with 32 as appropriate.  Which to do is encoded in the
  632.          low byte, except that if the high byte is the special
  633.          value 126, then the low byte is actually the high byte
  634.          and both operations are performed.  */
  635.       f128 = TRUE;
  636.       f32 = TRUE;
  637.       if (ihigh == INDEX_MAX_HIGH)
  638.         iset = ilow * INDEX_MAX_LOW + INDEX_MAX_LOW - 1;
  639.       else
  640.         {
  641.           iset = ihigh * INDEX_MAX_LOW + ilow % INDEX_MAX_LOW;
  642.           if (ilow < INDEX_MAX_LOW)
  643.         f32 = FALSE;
  644.           else if (ilow < 2 * INDEX_MAX_LOW)
  645.         f128 = FALSE;
  646.         }
  647.  
  648.       /* Now iset is the index from the start of the data to the
  649.          byte to modify; adjust it to an index in abPrecbuf.  */
  650.       iset = (istart + CHDRLEN + iset) % CRECBUFLEN;
  651.  
  652.       if (f128)
  653.         abPrecbuf[iset] |= 0x80;
  654.       if (f32)
  655.         abPrecbuf[iset] ^= 0x20;
  656.     }
  657.  
  658.       /* Zero out the header and trailer to avoid confusing the 'i'
  659.      protocol, and update iPrecend to the end of decoded data.  */
  660.       for (i = 0, iget = istart;
  661.        i < CHDRLEN && iget != iJrecend;
  662.        ++i, iget = (iget + 1) % CRECBUFLEN)
  663.     abPrecbuf[iget] = 0;
  664.       abPrecbuf[iendindex] = 0;
  665.       iPrecend = (iendindex + 1) % CRECBUFLEN;
  666.       istart = iPrecend;
  667.     }
  668.  
  669.   if (pcneed != NULL)
  670.     *pcneed = CHDRLEN + 1;
  671.   return TRUE;
  672. }
  673.