home *** CD-ROM | disk | FTP | other *** search
/ The Hacker's Encyclopedia 1998 / hackers_encyclopedia.iso / etc / misc / wxmodem.doc < prev    next >
Encoding:
Text File  |  2003-06-11  |  62.2 KB  |  1,385 lines

  1. 9
  2.  
  3.  
  4.  
  5.  
  6.  
  7.  
  8.  
  9.  
  10.  
  11.  
  12.  
  13.  
  14.  
  15.  
  16.  
  17.  
  18.                            Xmodem, CRC Xmodem, Wxmodem
  19.  
  20.  
  21.  
  22.                              File Transfer Protocols
  23.  
  24.  
  25. More..      
  26.  
  27.  
  28.  
  29.  
  30.  
  31.  
  32.  
  33.  
  34.  
  35.  
  36.  
  37.  
  38.  
  39.  
  40.                Please circulate this document anyway that you see
  41.                fit without alteration except on the page at the
  42.                end titled: "Notes and Comments".  It is requested
  43.                that anyone using these protocols within a commer-
  44.                cial product not charge for them as an option or
  45.                surcharge, but include XMODEM and its derivations
  46.                as part of the basic product.
  47. More..      
  48.  
  49.  
  50.  
  51.  
  52.  
  53.  
  54.                                              Peter Boswell
  55.                                              June 20, 1986
  56.                                              People/Link email: TOPPER
  57.  
  58.  
  59.  
  60.  
  61.  
  62.  
  63.  
  64.           Xmodem, CRC Xmodem, WXmodem
  65.      June 20, 1986                                                 Page 2
  66.      ----------------------------------------------------------------------
  67.  
  68.                                 TABLE OF CONTENTS
  69. More..      
  70.  
  71.      1.   PREFACE  . . . . . . . . . . . . . . . . . . . . . . . . . . .   3
  72.  
  73.      2.   INTRODUCTION . . . . . . . . . . . . . . . . . . . . . . . . .   5
  74.  
  75.      3.   TERMINOLOGY  . . . . . . . . . . . . . . . . . . . . . . . . .   6
  76.  
  77.      4.   XMODEM . . . . . . . . . . . . . . . . . . . . . . . . . . . .   7
  78.           4.1. Xmodem Hardware Level Protocol  . . . . . . . . . . . . .   7
  79.           4.2. Xmodem Initiation . . . . . . . . . . . . . . . . . . . .   7
  80.           4.3. Xmodem Data Transmission  . . . . . . . . . . . . . . . .   8
  81.           4.4. Xmodem Cancellation . . . . . . . . . . . . . . . . . . .   9
  82.           4.5. Xmodem Error Recovery and Timing  . . . . . . . . . . . .   9
  83.  
  84.      5.   CRC XMODEM . . . . . . . . . . . . . . . . . . . . . . . . . .  13
  85.           5.1. CRC Calculation Rules . . . . . . . . . . . . . . . . . .  13
  86.           5.2. CRC Xmodem Initiation . . . . . . . . . . . . . . . . . .  14
  87.  
  88.      6.   WINDOWED XMODEM (WXMODEM)  . . . . . . . . . . . . . . . . . .  15
  89.           6.2. Transparency and Flow Control Rules (Byte Level Rules)  .  16
  90.           6.3. Initial Handshake Rules . . . . . . . . . . . . . . . . .  18
  91. More..                6.4. Window Packet Transmission Rules  . . . . . . . . . . . .  18
  92.           6.5. Notes for X.25 Hosts  . . . . . . . . . . . . . . . . . .  22
  93.  
  94.      7.   APPENDIX A - CRC CALCULATION RULES . . . . . . . . . . . . . .  23
  95.           7.1. IBM PC - 8088/8086 Data Structure . . . . . . . . . . . .  23
  96.           7.2. BASIC Implementation of Bit Shift Method  . . . . . . . .  23
  97.           7.3. BASIC Implementation of the Table Method  . . . . . . . .  26
  98.  
  99.      8.   NOTES AND COMMENTS . . . . . . . . . . . . . . . . . . . . . .  28
  100.  
  101.  
  102.  
  103.  
  104.  
  105.  
  106.  
  107.           Xmodem, CRC Xmodem, WXmodem
  108.      June 20, 1986                                                 Page 3
  109.      ----------------------------------------------------------------------
  110.  
  111.      1.   PREFACE
  112.  
  113. More..           In the years that have past since Xmodem was first developed as a file
  114.      transfer protocol, many thousands of people have been involved in
  115.      finding reasonable ways to move data via asynchronous telephone commun-
  116.      ications.  I appreciate the opportunity that I have had to meet and
  117.      learn from many of these people.  There is nothing in this document
  118.      that did not actually come from someone else.  Indeed, whether it is
  119.      WXMODEM, X.PC, Synchronous dial-up X.25, SNA, ZMODEM, Blast, Kermit or
  120.      any other protocol that becomes the dominant dial-up file transfer
  121.      protocol for personal and home computers is just not important.  What
  122.      is important is that the public domain have a high speed file transfer
  123.      protocol that is reasonably popular and  commonly available for many
  124.      types of personal computers, for bulletin boards and for services such
  125.      as People/Link, Delphi, CompuServe, GEnie and The Source.
  126.  
  127.      Here are a few people that all of us should thank and I would espec-
  128.      ially like to recognize:
  129.  
  130.           Ward Christensen  Ward, a true pioneer in the microcomputer
  131.           communications area, is the author of the original Checksum
  132.           Xmodem protocol.  Thanks for reminding me to "keep it simple
  133.           stupid".
  134.  
  135. More..                Chuck Forsberg  Chuck has edited perhaps the best work on
  136.           Xmodem and has provided both YMODEM (1K Xmodem) and ZMODEM
  137.           (Windowed YMODEM) to the public domain.  Thanks for showing
  138.           me a protocol which would deal with the X-On/X-Off problem
  139.           and reminding me that there is such a thing as a DLE char-
  140.           acter.
  141.  
  142.           Richard (Scott) McGinnis  Scott is the architect, the moving
  143.           force, for the People/Link software system.  His ideas,
  144.           comments and encouragement have been wonderful.  Wait until
  145.           you see his visual conference program for the IBM PC!
  146.           Thanks for showing me how to use a DLE.
  147.  
  148.           Gene Plantz  Gene operates a major IBM PC bulletin board in
  149.           the Chicago area and has been active in the National SYSOP
  150.           Association.  Thanks for pushing me to do something about
  151.           performance.
  152.  
  153.      In a historical perspective, there seems to be a common pattern in all
  154.      computer systems development that can shed some light on where we stand
  155.      and how we got here.  The pattern is function first, then integrity and
  156.      finally performance.
  157. More..      
  158.      Any kind of software must first do something worthwhile.  There is no
  159.      point in being error free, or inexpensive to operate if we do not want
  160.      the function.  Back in 1977, Ward Christensen had a need to move data
  161.  
  162.  
  163.  
  164.  
  165.  
  166.  
  167.           Xmodem, CRC Xmodem, WXmodem
  168.      June 20, 1986                                                 Page 4
  169.      ----------------------------------------------------------------------
  170.  
  171.      between microcomputers.  Within a year it became obvious that the
  172.      function Xmodem provided met a real need to many microcomputer users.
  173.  
  174.      Once we have a new function and we accept it, there is a normal desire
  175.      for the function to be correct.  No one can't count the times that new
  176.      software users have pointed out ... "that new function is super, but
  177.      the results are wrong".  The effort changes from providing new function
  178.      to providing integrity.  The development of CRC Xmodem is a clear
  179. More..           response to the integrity phase of a service as it reduced undetected
  180.      transmission errors by many orders of magnitude.
  181.  
  182.      After the integrity has been accepted, people begin to look toward cost
  183.      and performance.  XMODEM entered this phase in 1984-1985.  Chuck
  184.      Forsberg's YMODEM is a major step in this effort providing larger
  185.      block sizes, batch mode and more.  His ZMODEM is a major step toward
  186.      making XMODEM derivative protocols work effectively with Public Data
  187.      Networks and most importantly, provides for restart of a file transfer
  188.      at the point of failure.  WXMODEM, presented here, is an alternate
  189.      solution to ZMODEM which is, hopefully, an easier solution to the most
  190.      important performance problems.
  191.  
  192.      No one really knows where XMODEM and the file transfer function will go
  193.      in the coming years.  Perhaps X.PC from Tymnet, MNP from Microcom or
  194.      Synchronous X.25 will slowly push XMODEM, et. al, into history.  I
  195.      think this will happen, but not for maybe 5 to 10 years.  Perhaps when
  196.      50% of the households outgrow the Commodore 64, or when modem manufac-
  197.      turers can provide a $50 synchronous modem we will see the beginning of
  198.      the end for XMODEM, but not today.
  199.  
  200.  
  201. More..      
  202.  
  203.  
  204.  
  205.           Xmodem, CRC Xmodem, WXmodem
  206.      June 20, 1986                                                 Page 5
  207.      ----------------------------------------------------------------------
  208.  
  209.      2.   INTRODUCTION
  210.  
  211.      XMODEM and its derivatives have become the primary method for file
  212.      transfer for personal computers.  Hopefully this document will help
  213.      people to understand these protocols and to implement them on their
  214.      own.  In particular, this document presents an additional XMODEM
  215.      derivation to the public domain: WXMODEM.
  216.  
  217.      Why develop another file transfer protocol?
  218.  
  219.      After working with bulletin boards, Public Data Networks such as Tymnet
  220.      and Telenet, and commercial host systems such as People/Link, Delphi,
  221.      CompuServe and others, a number of people came to believe that hobby-
  222.      ist, home and business users would benefit significantly from a new,
  223. More..           conceptually simple file transfer protocol which would provide improved
  224.      performance and fully support the public data networks such as Tymnet,
  225.      Telenet and Datapac.
  226.  
  227.      But before WXMODEM can be presented, XMODEM and CRC XMODEM must be
  228.      described in detail.
  229.  
  230.  
  231.  
  232.  
  233.  
  234.  
  235.  
  236.           Xmodem, CRC Xmodem, WXmodem
  237.      June 20, 1986                                                 Page 6
  238.      ----------------------------------------------------------------------
  239.  
  240.      3.   TERMINOLOGY
  241.  
  242.      I've elected to use two special terms: transmitter and receiver.  The
  243.      transmitter is the computer/software which is transmitting data packets
  244.      and receiving acknowledgement characters.  The receiver is the com-
  245. More..           puter/software receiving the data packets and transmitting acknowledge-
  246.      ment characters.
  247.  
  248.      Here is a table of special ASCII characters that are used throughout
  249.      this paper:
  250.  
  251.           Name      Decimal        Hexadecimal    Description
  252.  
  253.           SOH          01           H001          Start Of Header
  254.           EOT          04           H004          End Of Transmission
  255.           ACK          06           H006          Acknowledge (positive)
  256.           DLE          16           H010          Data Link Escape
  257.           X-On (DC1)   17           H011          Transmit On
  258.           X-Off(DC3)   19           H013          Transmit Off
  259.           NAK          21           H015          Negative Acknowledge
  260.           SYN          22           H016          Synchronous idle
  261.           CAN          24           H018          Cancel
  262.  
  263.  
  264.  
  265.  
  266.  
  267. More..      
  268.  
  269.           Xmodem, CRC Xmodem, WXmodem
  270.      June 20, 1986                                                 Page 7
  271.      ----------------------------------------------------------------------
  272.  
  273.      4.   XMODEM
  274.  
  275.      Xmodem is a popular error recovery type protocol for transferring files
  276.      between computers via serial, asynchronous communications.   Before
  277.      learning more about Xmodem, it is important to hear what its author has
  278.      to say:
  279.  
  280.           "It was a quick hack I threw together, very unplanned (like
  281.           everything I do), to satisfy a personal need to communicate
  282.           with some other people.  ONLY the fact that it was done in
  283.           8/77, and that I put it in the public domain immediately,
  284.           made it become the standard that it is"....."People who
  285.           suggest I make SIGNIFICANT changes to the protocol, such as
  286.           'full duplex', 'multiple outstanding blocks', 'multiple
  287.           destinations', etc etc don't understand that the incredible
  288.           simplicity of the protocol is one of the reasons it survived
  289. More..                to this day in as many machines and programs as it may be
  290.           found in!"a
  291.  
  292.           4.1. Xmodem Hardware Level Protocol
  293.  
  294.           The protocol is Asynchronous, 8 data bits, no parity bit, one stop
  295.           bit.  Modems which are commonly used are AT&T 103 (300 baud), AT&T
  296.           212A (1200 baud) and CCITT V.22 (2400 baud).
  297.  
  298.           Typically, the data in a file is transmitted without change (if a
  299.           7 bit machine, the left most, high order, bit is always zero)
  300.           except that CP/M and MS/DOS operating systems want a ^Z (decimal
  301.           26) to represent end-of-file.
  302.  
  303.           4.2. Xmodem Initiation
  304.  
  305.           Prior to entering the protocol, both the transmitting and receiv-
  306.           ing computer must know where to get the data (what file is to be
  307.           transmitted) and where to put the data (file to store the data or
  308.           buffer area).  In Xmodem one side of the file transmission is
  309.           always in charge (local computer), asking the other side (remote
  310.           computer) to either transmit a file or to accept a file.  Through
  311. More..                a dialog outside of Xmodem the local computer (your PC) first
  312.           sends commands to the remote computer to select a file name
  313.           to prepare to transmit or receive a file via XMODEM.  Once this is
  314.           completed the remote computer enters the XMODEM protocol.  Now the
  315.           local computer must be told what file to transmit or receive and
  316.           it enters the XMODEM protocol, and hopefully data starts moving.
  317.  
  318.  
  319.           a    Ward Christensen, quoted from a message posted on CompuServe
  320.                in 1985.  Edited by Chuck Forsberg, "X/Ymodem Protocol
  321.                Reference", unpublished, 10/20/1985.
  322.  
  323.  
  324.  
  325.  
  326.  
  327.  
  328.           Xmodem, CRC Xmodem, WXmodem
  329.      June 20, 1986                                                 Page 8
  330.      ----------------------------------------------------------------------
  331.  
  332.           Upon entering the Xmodem protocol, the transmitting computer waits
  333. More..                between 10 seconds and a minute to receive an NAK character from
  334.           the receiving computer.  The receiving computer is said to drive
  335.           the protocol.  The transmitter may retry any number of times.  If
  336.           any character other than a NAK or CAN is read by the transmitter,
  337.           it is ignored.  The CAN character implies cancellation of the
  338.           Xmodem file transfer and that the transmitter should leave the
  339.           Xmodem protocol.  Once the receiver has sent a NAK, it will wait
  340.           10 seconds for data to begin to arrive.  If none arrives in 10
  341.           seconds, the receiver will send another NAK and continue to repeat
  342.           10 times at which point the receiver will leave the Xmodem
  343.           protocol (typically with a super cryptic error message such as
  344.           "aborted", "NAK retry maximum exceeded").
  345.  
  346.           Transmitter                        Receiver
  347.  
  348.           [wait for one minute]         <    [NAK]
  349.  
  350.           [begin block transmission]    >
  351.  
  352.           4.3. Xmodem Data Transmission
  353.  
  354.           The transmitter takes the data, divides it into 128, 8 bit byte
  355. More..                pieces and places it in an Xmodem Packet.
  356.  
  357.           The Xmodem Packet looks like this:
  358.  
  359.                [SOH] [seq] [cmpl [seq] [128 data bytes] [csum]
  360.  
  361.                SOH       Start of header character (decimal 1).
  362.  
  363.                seq       one byte sequence number which starts at 1, and
  364.                          increments by one until it reaches 255 and then
  365.                          wraps around to zero.
  366.  
  367.                cmpl seq  one byte 1's complement of seq.  This can be
  368.                          calculated as cmpl = 255 - (255 and seq) or using
  369.                          xor as cmpl = (255 and seq) xor 255.
  370.  
  371.                data      128, 8 bit bytes of data.  Note than when sending
  372.                          CP/M and MS/DOS files a ^Z (decimal 26) must be
  373.                          added to then end of the file.  If the last block
  374.                          of data is less than 128 bytes, the Xmodem packet
  375.                          must be padded with characters, usually ^Z's.
  376.  
  377. More..                     csum      one byte sum of all of the data bytes where any
  378.                          overflow or carry is discarded immediately.  For
  379.                          example, if the first 3 bytes are 255, 5 and 6 the
  380.                          checksum after the first 3 bytes will be 10.
  381.  
  382.  
  383.  
  384.  
  385.  
  386.  
  387.  
  388.           Xmodem, CRC Xmodem, WXmodem
  389.      June 20, 1986                                                 Page 9
  390.      ----------------------------------------------------------------------
  391.  
  392.           Once Xmodem Initiation has completed, the transmitter sends the
  393.           first Xmodem packet and then waits.  After the receiver has the
  394.           full packet, it will compare its own checksum calculation with the
  395.           checksum that was sent by the transmitter.  If the checksums
  396.           match, the receiver will send an ACK.  If the checksums are
  397.           different, the receiver will send a NAK.
  398.  
  399. More..                After receiving an ACK the transmitter will send the next Xmodem
  400.           packet.  If a NAK is received, the transmitter will resend the
  401.           same XMODEM packet again.
  402.  
  403.           Once the transmitter has sent the last Xmodem packet and has
  404.           received an ACK, the transmitter will send an EOT and then wait
  405.           for a final ACK from the receiver before leaving the Xmodem
  406.           protocol.  When the receiver sees an EOT instead of an SOH (the
  407.           first character the next packet), the receiver transmits an ACK
  408.           character, closes its files and leaves the Xmodem protocol.
  409.  
  410.           Let's look at a three block file transfer:
  411.  
  412.                Transmitter                                  Receiver
  413.  
  414.                                              <<<<<          [NAK]
  415.                [SOH][001][255][...][csum]    >>>>>
  416.                                              <<<<<          [ACK]
  417.                [SOH][002][254][...][csum]    >>>>>
  418.                                              <<<<<          [ACK]
  419.                [SOH][003][253][...][csum]    >>>>>
  420.                                              <<<<<          [ACK]
  421. More..                     [EOT]                         >>>>>
  422.                                              <<<<<          [ACK]
  423.  
  424.           Seems easy, right?  And it is, until something goes wrong.
  425.  
  426.           4.4. Xmodem Cancellation
  427.  
  428.           It has become a defacto standard that the receiver may cancel the
  429.           file transfer by sending a CAN character and then leaving the
  430.           protocol.  If the transmitter receives a CAN character when
  431.           expecting either a NAK or ACK, the transmitter is to termin-
  432.           ate and leave the protocol.  Likewise, if the receiver sees a CAN
  433.           when expecting an SOH (start of packet) it should terminate the
  434.           file transfer.  Many implementations now require two CAN char-
  435.           acters before recognizing a cancel condition.
  436.  
  437.           4.5. Xmodem Error Recovery and Timing
  438.  
  439.           Error detection and recovery are the primary purposes of the
  440.           Xmodem protocol.  The transmitter and receiver should continue to
  441.           retry until 10 errors in a row have occurred.  Some of the common
  442.  
  443. More..      
  444.  
  445.  
  446.  
  447.  
  448.           Xmodem, CRC Xmodem, WXmodem
  449.      June 20, 1986                                                 Page 10
  450.      ----------------------------------------------------------------------
  451.  
  452.           error conditions are listed below:
  453.  
  454.                4.5.1.    Complement Error
  455.  
  456.                If the sequence number does not match the complement
  457.                sequence number, the packet must be discarded and a NAK
  458.                sent to the transmitter.
  459.  
  460.                4.5.2.    Duplicate packet condition
  461.  
  462.                If the sequence number is the same as the sequence
  463.                number of the last packet received, the packet should be
  464.                discarded and an ACK sent to the transmitter.
  465. More..      
  466.                4.5.3.    Out of sequence error
  467.  
  468.                If the sequence number matches the complement sequence
  469.                number and it is neither the expected sequence number
  470.                nor the last sequence number, the receiver should send
  471.                two CAN characters and leave the Xmodem protocol
  472.                (e. g. abort the file transfer).
  473.  
  474.                4.5.4.    Receive timeout errors
  475.  
  476.                When expecting data, if 10 seconds ever pass without
  477.                receipt of a character, the receiver should send another
  478.                NAK.  This should be repeated 10 times.  Some implement-
  479.                ations will timeout after 10 seconds waiting for the
  480.                first character of a packet, SOH, and then reduce the
  481.                timeout for characters in a packet.  The timeout should
  482.                not go below 5 seconds if the protocol is to be used
  483.                with public data networks.
  484.  
  485.                4.5.5.    Transmit timeout errors
  486.  
  487. More..                     In the original protocol, the transmitter would wait 10
  488.                seconds for an ACK, NAK or CAN and then retransmit the
  489.                last Xmodem packet as if a NAK had been received.  Most
  490.                implementations either have the transmitter wait for a
  491.                very long time (30 seconds to a minute) and then
  492.                terminate the file transfer if an ACK, NAK or CAN has
  493.                not been receive or wait about 30 seconds and retransmit
  494.                the last packet.
  495.  
  496.                4.5.6.    Packet synchronization errors
  497.  
  498.                Since extraneous characters are frequently generated
  499.                when using asynchronous communications, the receiver
  500.                should not count on receiving exactly 132 characters for
  501.                each Xmodem packet.  One algorithm for re-synchroniz-
  502.  
  503.  
  504.  
  505.  
  506.  
  507.  
  508.           Xmodem, CRC Xmodem, WXmodem
  509. More..           June 20, 1986                                                 Page 11
  510.      ----------------------------------------------------------------------
  511.  
  512.                ation goes as follows:
  513.  
  514.                     Assume that the checksum algorithm will cause re-trans-
  515.                     mission of Xmodem packets which contain extraneous
  516.                     characters.
  517.  
  518.                     If the character received when expecting the start of a
  519.                     packet is not a SOH then ignore the character and
  520.                     continue to search for a SOH.
  521.  
  522.                     Once a SOH is found, assume that the next two characters
  523.                     will be a valid sequence number and complement.  If they
  524.                     are complements then assume that the packet has begun.
  525.                     If they are not complements, continue to search for a
  526.                     SOH.
  527.  
  528.                     Send a NAK if a timeout occurs while attempting to
  529.                     re-synchronize (e.g. continue to process timeouts as
  530.                     described above).
  531. More..      
  532.                     If no re-synchronization occurs within 135 characters
  533.                     then send a NAK character and retry receiving the
  534.                     packet.
  535.  
  536.                4.5.7.    False EOT condition
  537.  
  538.                When the receiver sees an EOT (which was not sent by the
  539.                transmitter, but generated out of a communications error)
  540.                instead of a SOH character, the receiver assumes incorrectly
  541.                that the complete file has been transmitted.  This is
  542.                typically an unrecoverable error and it does occur especially
  543.                when the transmitting and receiving UARTs are clocked
  544.                slightly differently.  An algorithm to detect false EOT might
  545.                return a NAK for the first EOT received and only assume true
  546.                end of transmission after receiving two EOT's.
  547.  
  548.                     Transmitter                   Receiver
  549.  
  550.                     [last block .. ]    >>>>>
  551.                                         <<<<<     [ACK]
  552.                     [EOT]               >>>>>
  553. More..                                              <<<<<     [NAK]
  554.                     [EOT]               >>>>>
  555.                                         <<<<<     [ACK]
  556.  
  557.                Just in case the transmitter was not prepared to resend the
  558.                EOT, it might be wise to set the timeout to about 3 seconds
  559.                and retransmit the NAK up to 3 times and then issue a warning
  560.                message but assume end of transmission.
  561.  
  562.  
  563.  
  564.  
  565.  
  566.  
  567.  
  568.           Xmodem, CRC Xmodem, WXmodem
  569.      June 20, 1986                                                 Page 12
  570.      ----------------------------------------------------------------------
  571.  
  572.                4.5.8.    False CAN condition
  573.  
  574.                Some Xmodem implementations will terminate on a single CAN
  575. More..                     character.  Occasionally a CAN character will be generated by
  576.                a communications error and if this occurs and is seen by the
  577.                receiver between packets or is ever seen by the transmitter,
  578.                the file transfer will be incorrectly canceled.  Many
  579.                implementations now require two CAN characters in a row
  580.                before assuming that the file transfer is to be aborted.
  581.  
  582.  
  583.  
  584.  
  585.  
  586.  
  587.  
  588.           Xmodem, CRC Xmodem, WXmodem
  589.      June 20, 1986                                                 Page 13
  590.      ----------------------------------------------------------------------
  591.  
  592.      5.   CRC XMODEM
  593.  
  594.      CRC Xmodem is very similar to Checksum Xmodem.  The protocol initiation
  595.      has changed and the 8 bit checksum has been replaced by a 16 bit CRC.
  596.      Only theses changes are presented.
  597. More..      
  598.      One of the earliest and most persistent problems with Xmodem were
  599.      transmission errors which were not caught by the checksum algorithm.
  600.      Assuming that there is no bias in asynchronous communications errors,
  601.      we would expect that 1 out of every 256 erroneous complete or oversized
  602.      Xmodem packets to have a valid checksum.  With the same assumption, if
  603.      the checksum were 16 bits, we would expect 1 out of every 65,536
  604.      erroneous complete or oversized packets would have a valid checksum.
  605.  
  606.           5.1. CRC Calculation Rules
  607.  
  608.           Considerable theoretical research has shown that a 16 bit cyclical
  609.           redundancy check character (CRC/16) will detect a much higher
  610.           percent of errors such that it would only allow 1 undetected
  611.           bit in error for every 10^14 bits transmitted.  That's 1 un-
  612.           detected error per 30 years of constant transmission at 1 mega-
  613.           bit per second.  However, my personal experience indicates that
  614.           something around 10^9 to 10^10 is more realistic.  Why such a vast
  615.           improvement over the checksum algorithm?  It is caused by the
  616.           unique properties that prime numbers have when being divided into
  617.           integers.  Simply stated, if an integer is divided by a prime
  618.           number, the remainder is unique.  The CRC/16 algorithm treats all
  619. More..                1024 data bits in an Xmodem packet as an integer, multiples that
  620.           integer by 2^16 and then divides that 1040 bit number by a 17 bit
  621.           prime number.  The low order 16 bits of the remainder becomes the
  622.           16 bit CRC.
  623.  
  624.           The 17 bit prime number in CRC Xmodem is 2^16 + 2^12 + 2^5 + 1 or
  625.           65536 + 4096 + 32 + 1 = 69665.  So calculating the CRC is simple,
  626.           just multiply the 128 byte data number by 65536, divide by 69665
  627.           and the low order 16 bits of the remainder are the CRC.  The only
  628.           problem is, I've never seen a computer which has instructions to
  629.           support 130 byte integer arithmetic!  Fortunately for us, Seephan
  630.           Satchell, Satchell Evaluations, published a specification a very
  631.           efficient algorithm to calculate the CRC without either 130 byte
  632.           arithmetic or bit manipulation.  Appendix A contains the source
  633.           code, in IBM/PC BASIC, for the calculation of a CRC.
  634.  
  635.           Other methods of calculating CRC's for Xmodem involve bit level
  636.           logic. a.
  637.  
  638.  
  639.           a    Chuck Forsberg, "X/Ymodem Protocol Reference", available on
  640.                many bulletin boards.
  641. More..      
  642.  
  643.  
  644.  
  645.  
  646.  
  647.           Xmodem, CRC Xmodem, WXmodem
  648.      June 20, 1986                                                 Page 14
  649.      ----------------------------------------------------------------------
  650.  
  651.           5.2. CRC Xmodem Initiation
  652.  
  653.           The initiation of CRC Xmodem was designed to provide for automatic
  654.           fall back to Checksum Xmodem if the transmitter does not support
  655.           the CRC version.
  656.  
  657.           The receiver requests CRC Xmodem by sending the letter C (decimal
  658.           67) instead of a NAK.  If the transmitter supports CRC Xmodem, it
  659.           will begin transmission of the first Xmodem packet upon receipt of
  660.           the C.  If the transmitter does not support CRC Xmodem, it will
  661.           ignore the C.  The receiver should timeout after 3 seconds and
  662.           repeat sending the C.  After 3 timeouts, the receiver should fall
  663. More..                back to the checksum Xmodem protocol and send a NAK.
  664.  
  665.  
  666.  
  667.  
  668.  
  669.  
  670.  
  671.  
  672.           Xmodem, CRC Xmodem, WXmodem
  673.      June 20, 1986                                                 Page 15
  674.      ----------------------------------------------------------------------
  675.  
  676.      6.   WINDOWED XMODEM (WXMODEM)a
  677.  
  678.      First, Xmodem provided the basic file transfer function, then CRC
  679.      Xmodem improved the data integrity, now we come to WXmodem which
  680.      provides better cost/performance.
  681.  
  682.           6.1. WXmodem Design Criteria
  683.  
  684.           A few people began discussing improvements to Xmodem with me in
  685. More..                late 1985, over time we developed the following criteria:
  686.  
  687.                6.1.1.    The protocol must be as similar as possible to the
  688.                          XMODEM originally developed by Ward Christensen.
  689.                          The popularity of XMODEM, I believe, is based on
  690.                          its conceptual simplicity.  More software writers
  691.                          are familiar with this protocol than any other.
  692.                          More files are transferred everyday by this
  693.                          protocol than any other asynchronous protocol.
  694.                          Simplicity here implies a limited number of rules
  695.                          for timing, error recovery and initiation.
  696.  
  697.                6.1.2.    The protocol must overcome the propagation delay
  698.                          that is characteristic of the public data net-
  699.                          works.  While the cost of long distance communi-
  700.                          cation is 50 to 90% less via the public data
  701.                          networks than via the public voice networks, the
  702.                          propagation delays inherent in public data networks
  703.                          both reduces the cost savings and increases the
  704.                          aggravation that occurs while watching a computer
  705.                          slowly perform a file transfer.
  706.  
  707. More..                     6.1.3.    The protocol must overcome the flow control
  708.                          problems which are characteristic in many public
  709.                          data network situations.  Basically, in most
  710.                          situations, the X-On and X-Off characters must
  711.                          always be used for flow control and only for flow
  712.                          control when using public data networks.
  713.  
  714.                6.1.4.    The protocol should improve error recovery by
  715.                          simplifying the manner in which a programmer can
  716.                          determine the beginning of an XMODEM block.  Since
  717.                          the Start of Header character (SOH) can appear in
  718.                          the data or CRC, the programmer must use a rela-
  719.                          tively sophisticated method to determine if a SOH
  720.                          actually represents the beginning of a XMODEM
  721.                          block.
  722.  
  723.  
  724.  
  725.           a    This section assumes that the reader is already familiar with
  726.                Xmodem and CRC Xmodem presented above.
  727.  
  728.  
  729. More..      
  730.  
  731.  
  732.  
  733.           Xmodem, CRC Xmodem, WXmodem
  734.      June 20, 1986                                                 Page 16
  735.      ----------------------------------------------------------------------
  736.  
  737.  
  738.           6.2. Transparency and Flow Control Rules (Byte Level Rules)
  739.  
  740.           This protocol provides special public data network support for
  741.           non-X.25 hosts and PC-Pursuit access to bulletin boards.  In order
  742.           to accomplish this, the transmitter is not permitted to transmit
  743.           the X-On and X-Off characters in the Xmodem packets.  The reason
  744.           for this restriction is simple:
  745.  
  746.                By the very nature of X.25 public data networks, without
  747.                flow control, buffer overruns and lost data are inevit-
  748.                able from time to time at any baud rate.
  749.  
  750.                To avoid data loss public data networks must always
  751. More..                     assume that any X-Off and X-On character is a flow
  752.                control character when supporting PC-Pursuit for
  753.                bulletin boards and when supporting non-X.25 host
  754.                systems.
  755.  
  756.           Since many non-X.25 hosts, bulletin boards and communications
  757.           programs use X-On and X-Off as flow control characters, public
  758.           data networks must support those X-Off and X-On requests at the
  759.           point of connection where the X-Off is received by the public data
  760.           network.  Otherwise, as many as several hundred characters backed
  761.           up in the network would be transmitted by the public data network
  762.           before the X-Off used for flow control reached the transmitter.
  763.           The public data network has no way to know whether an X-On/X-Off
  764.           protocol or Xmodem is operational at any point in time.  Therefore
  765.           a Xmodem packet which contains an X-Off character and no succeed-
  766.           ing X-On character will cause the public data network to stop
  767.           forwarding the ACK or NAK.
  768.  
  769.           In addition, error recovery requires sophisticated programming for
  770.           the receiver to determine the start of an XMODEM packet.  This
  771.           protocol simplifies this task by dedicating a special character as
  772.           an indicator that an XMODEM packet is about to begin.  The
  773. More..                SYN (synch, decimal 22) character is used for this purpose.
  774.           The presence of one or more SYN characters in a data stream always
  775.           indicates that the next non SYN character is the beginning of an
  776.           XMODEM packet (e.g. SOH).
  777.  
  778.           The method used here to handle these situations is through the
  779.           insertion of the DLE character (H010, decimal 16, data link escape
  780.           character) as an indicator that the character following the DLE is
  781.           in fact a modified DLE, SYN, X-On, or X-Off character.
  782.  
  783.           Rules:
  784.  
  785.                6.2.1.    Whenever an X-On, X-Off, SYN or DLE character is
  786.                          about to be transmitted as any part of an actual
  787.  
  788.  
  789.  
  790.  
  791.  
  792.  
  793.           Xmodem, CRC Xmodem, WXmodem
  794.      June 20, 1986                                                 Page 17
  795. More..           ----------------------------------------------------------------------
  796.  
  797.                          XMODEM packet including the CRC, the transmitter
  798.                          will transmit instead a DLE character followed by
  799.                          the original character which has been modified by
  800.                          exclusive or'ing it with 64 to its value. 1
  801.  
  802.                6.2.2.    The inserted DLE characters are not counted in the
  803.                          128 byte data length of the data portion of the
  804.                          XMODEM packet.  Indeed, it would be possible to
  805.                          have a packet which is physically 264 bytes in
  806.                          length because the Xmodem block sequence number
  807.                          (or its complement), all of the 128 data characters
  808.                          and two CRC characters are all either X-On, X-Off,
  809.                          SYN or DLE characters.
  810.  
  811.                6.2.3.    Neither the DLE nor the adjusted characters are
  812.                          used in the CRC calculation, rather the original
  813.                          character is always used in the CRC calculation.
  814.  
  815.  
  816.                6.2.4.    When the receiver sees a DLE character, it does not
  817. More..                               count it in the XMODEM block length calculation,
  818.                          nor compute it in the CRC calculation but discards
  819.                          it and then remembers to exclusive or the next
  820.                          character with 64 and to verify that the result
  821.                          character is either a DLE, SYN, X-On or X-Off (the
  822.                          receiver will reject the packet unconditionally, if
  823.                          not one of those four characters) and then include
  824.                          the character as part of the packet.
  825.  
  826.                6.2.5.    Prior to transmission of a XMODEM packet, the
  827.                          transmitter will send one or more SYN characters
  828.                          (recommend two) as a positive indicator to the
  829.                          receiver of the beginning of a Xmodem packet.2
  830.  
  831.                6.2.6.    Except for the character received after a DLE, the
  832.                          receiver will test each incoming character to see
  833.                          if it is a SYN character.  If it is, it will
  834.                          discard the character and assume that the next
  835.                          character will be another SYN or SOH.  If a SYN
  836.                          character is received in the middle of a packet,
  837.                          the receiver will NAK that packet.  The purpose of
  838.                          the SYN character is to simplify recognition of the
  839. More..                               beginning of a XMODEM packet by the receiver.  Once
  840.                          an out of synch condition occurs on incoming
  841.                          data, the receiver can just ignore every incoming
  842.                          character until it sees a SYN.  Existing XMODEM
  843.                          code which already properly deals with this
  844.                          situation could just always discard any SYN
  845.                          character at time of receipt with no further
  846.                          action.
  847.  
  848.  
  849.  
  850.  
  851.  
  852.  
  853.           Xmodem, CRC Xmodem, WXmodem
  854.      June 20, 1986                                                 Page 18
  855.      ----------------------------------------------------------------------
  856.  
  857.  
  858.                6.2.7.    The transmitter must support flow control char-
  859.                          acters (X-On, and X-Off) during transmission of
  860.                          packets.  Upon receipt of an X-Off it will wait 10
  861. More..                               seconds for an X-On and will start transmission
  862.                          again after 10 seconds or an X-On is received,
  863.                          whichever occurs first.  Any extraneous X-On
  864.                          characters received by the transmitter will be
  865.                          ignored and discarded.  (Note that this does NOT
  866.                          apply to X.25 host computers which use X.25 L2 and
  867.                          L3 windows for flow control.)
  868.  
  869.           6.3. Initial Handshake Rules
  870.  
  871.           An initial handshake is provided to permit the receiver to
  872.           indicate to the transmitter whether it can support checksum
  873.      Xmodem, CRC Xmodem, or Windowed Xmodem:
  874.  
  875.                6.3.1.    WXMODEM - The receiver will send a character W
  876.                          (decimal 87) and wait 3 seconds for the beginning
  877.                          of a Xmodem packet.  This will be repeated 3 times
  878.                          and then the receiver will drop down to CRC Xmodem.
  879.  
  880.                6.3.2.    CRC XMODEM - The receiver will send a character C
  881.                          (decimal 67) and wait 3 seconds for the beginning
  882.                          of a Xmodem packet.  This will be repeated 3 times
  883. More..                               and then the receiver will drop down to Checksum
  884.                          Xmodem.
  885.  
  886.                6.3.3.    Checksum XMODEM -  The receivers will send a NAK
  887.                          and wait up to 3 seconds for the beginning of a
  888.                          Xmodem packet.  This will be repeated 4 times and
  889.                          if no valid SOH is received, the receiver will
  890.                          abort the file transfer request.
  891.  
  892.           6.4. Window Packet Transmission Rules
  893.  
  894.           In order to overcome the propagation delays inherent with public
  895.           data networks such as Tymnet, Telenet, Datapac, IPSS, Transpac and
  896.           dozens more, the protocol must permit the transmitter to send more
  897.           than one packet before receiving an acknowledgement from the
  898.           receiver.  The number of packets that the transmitter will send
  899.           before stopping transmission if an acknowledgement has not been
  900.           received is called the "window".  WXmodem uses a window of 4
  901.           packets for several reasons.  Most importantly, it uses a single
  902.           set of timing rules which would deal reasonably well with a wide
  903.           range of baud rates (that implied keeping the window fairly
  904.           small).  Secondly, the window sequence number is directly related
  905. More..                to the Xmodem packet sequence number which, hopefully, will
  906.           simplify implementation of windowing.
  907.  
  908.  
  909.  
  910.  
  911.  
  912.  
  913.           Xmodem, CRC Xmodem, WXmodem
  914.      June 20, 1986                                                 Page 19
  915.      ----------------------------------------------------------------------
  916.  
  917.  
  918.           Rules:
  919.  
  920.                6.4.1.    The window is always 4 Xmodem packets.  That is,
  921.                          the transmitter will send 4 unacknowledged pack-
  922.                          ets.  Transmission will not cease and the time out
  923.                          interval will not begin until 4 unacknowledged
  924.                          packets have been transmitted.  Note that the
  925.                          window may be less than 4 Xmodem packets for short
  926.                          files or at end-of-file.
  927. More..      
  928.                6.4.2.    The receiver will transmit acknowledgements in the
  929.                          form:
  930.  
  931.                               ACK[sequence]
  932.  
  933.                          The [sequence] field is an 8 bit number where the
  934.                          high order or most significant 6 bits are always
  935.                          zero and the low order or least significant 2 bits
  936.                          are always the same as the low order 2 bits of the
  937.                          XMODEM block sequence number of the XMODEM packet
  938.                          being acknowledged (value in decimal may range
  939.                          from 0 to 3).
  940.  
  941.                6.4.3.    The receiver does not have to acknowledge every
  942.                          packet, but must acknowledge at minimum every
  943.                          fourth packet.  The transmitter will accept one
  944.                          ACK[sequence] for multiple XMODEM packets.  For
  945.                          example, after an unknown number of packets:
  946.  
  947.                          Transmitter                             Receiver
  948.  
  949. More..                               ....
  950.                          ....
  951.                          ....
  952.                          [Block Sequence Number H0FE]
  953.                          [Block Sequence Number H0FF]            ACK[H002]
  954.                          [Block Sequence Number H000]            ACK[H003]
  955.                          [Block Sequence Number H001]
  956.                          [Block Sequence Number H002]            ACK[H001]
  957.                          .....
  958.  
  959.                          Since some transmitters must close the window and
  960.                          cease all communications before doing disk I/O to
  961.                          read more data, it is suggested that acknowledge-
  962.                          ments be sent for every packet (except when the
  963.                          receiver can easily determine that another packet
  964.                          is already being received at the point in time that
  965.                          the ACK[sequence] is about to be sent).3
  966.  
  967.  
  968.  
  969.  
  970.  
  971. More..      
  972.  
  973.           Xmodem, CRC Xmodem, WXmodem
  974.      June 20, 1986                                                 Page 20
  975.      ----------------------------------------------------------------------
  976.  
  977.                6.4.4.    The receiver will reject a packet (request re-
  978.                          transmission) by sending:
  979.  
  980.                               NAK[sequence]
  981.  
  982.                          Where [sequence] is then next window sequence
  983.                          number (between H000 and H003) after the [sequence]
  984.                          of the last good block.  The receiver will discard
  985.                          up to 3 Xmodem packets received after the NAK is
  986.                          transmitted until it receives the packet with the
  987.                          sequence number that had previously been nak'ed by
  988.                          the receiver.  The receiver will not send a second
  989.                          NAK until another packet with the same sequence
  990.                          number is received which is also invalid or a
  991.                          timeout has occurred.
  992.  
  993. More..                     6.4.5.    When the transmitter receives a NAK[sequence], it
  994.                          will complete transmission of any XMODEM block
  995.                          currently being transmitted and then begin re-
  996.                          transmission starting with the block which was
  997.                          nak'ed.
  998.  
  999.                6.4.6.    The receiver will discard duplicate packets but
  1000.                          count them in the window for purposes of deter-
  1001.                          mining the maximum receive window without an ACK in
  1002.                          response.  For example, if the receiver gets packet
  1003.                          sequence number 127 four times in a row, it must
  1004.                          send an ACK H003 even if the receiver has previous-
  1005.                          ly acked that block.
  1006.  
  1007.                6.4.7.    The timeout intervals at various points in process-
  1008.                          ing are:
  1009.  
  1010.                          Waiting for a character on receive, start of packet
  1011.                          not yet recognized:      15 seconds
  1012.  
  1013.                          Waiting for a character on receive, start of packet
  1014.                          has been recognized:     15 seconds
  1015. More..      
  1016.                          Waiting for an Ack or Nak on transmit side after
  1017.                          the window has closed:   15 seconds4
  1018.  
  1019.                          Waiting for an X-On after receipt of an X-Off by
  1020.                          the transmitter:         10 seconds
  1021.  
  1022.                6.4.8.    When the transmitter times out waiting for an ACK
  1023.                          or NAK when the window is closed (e.g. four blocks
  1024.                          have been transmitted), the transmitter will
  1025.                          retransmit the last block transmitted and wait
  1026.                          again.  Only after 10 consecutive timeouts have
  1027.  
  1028.  
  1029.  
  1030.  
  1031.  
  1032.  
  1033.           Xmodem, CRC Xmodem, WXmodem
  1034.      June 20, 1986                                                 Page 21
  1035.      ----------------------------------------------------------------------
  1036.  
  1037. More..                               occurred will the transmitter cancel the trans-
  1038.                          mission.
  1039.  
  1040.                6.4.9.    Where possible, it is recommended that the receiver
  1041.                          return an ACK[sequence] for every packet or at
  1042.                          least 50% of the Xmodem packets.  When the receiver
  1043.                          must wait for the window to close (e.g. receive 4
  1044.                          Xmodem packets without an acknowledgement),
  1045.                          some performance benefit will be lost.
  1046.  
  1047.           If the receiver cannot overlap disk I/O and communications
  1048.           I/O, the receiver can temporarily stop transmission by either:
  1049.  
  1050.                "Closing the window" (e.g. receiving 4 blocks without sending
  1051.                an ACK[sequence]) performing the disk I/O and then sending an
  1052.                ACK[sequence].
  1053.  
  1054.                Transmitting an X-Off followed by an X-On when the receiver
  1055.                is ready to resume accepting data.  Note that the receiver
  1056.                should be prepared to accept data for about a 1/4 of a second
  1057.                after the X-Off is sent to cover situations where satellite
  1058.                propagation delay may occur.  One possible implementation
  1059. More..                     would let the computer user set the "X-Off delay time" so
  1060.                that in the normal case the X-Off delay could be set to 25
  1061.                milleseconds.  A sophisticated implementation might set the
  1062.                initial X-Off delay at 250 milleseconds and then reduce it
  1063.                based on experience during the file transfer.
  1064.  
  1065.                Each approach has its advantages, but the X-Off approach will
  1066.                provide the best performance in most cases especially when
  1067.                using a public data network.  Note, however, that some
  1068.                computers, notably the Commodore 64 and the IBM PC Jr cannot
  1069.                receive communications data while writing to disk.
  1070.  
  1071.  
  1072.  
  1073.  
  1074.  
  1075.  
  1076.  
  1077.           Xmodem, CRC Xmodem, WXmodem
  1078.      June 20, 1986                                                 Page 22
  1079.      ----------------------------------------------------------------------
  1080.  
  1081. More..                6.5. Notes for X.25 Hosts
  1082.  
  1083.           Host computer systems which utilize the X.25 protocol
  1084.           (examples: People/Link, Delphi, CompuServe, The Source) to
  1085.           interface with the various public data networks may send special
  1086.           control packets which change the manner in which the network will
  1087.           communicate with the remote personal computer, bulletin board or
  1088.           terminal.  For the purposes of this paper, it is assumed that the
  1089.           X.25 host can already support CRC and/or Checksum Xmodem and
  1090.           present only the changes for WXMODEM.
  1091.  
  1092.                6.5.1.    When an X.25 Host is the transmitter, it must be
  1093.                          sure to set the distant X.3 PAD parameters to
  1094.                          assure that the receiver can use X-Off/X-On for
  1095.                          flow control.  This is accomplished by sending a
  1096.                          Q-Bit command packet to set X.3 parameter 12 to a 1
  1097.                          prior to the initial handshake.  Note that if the
  1098.                          receiver cannot support WXMODEM, the X.25 Host must
  1099.                          send the appropriate Q-Bit packet to reset para-
  1100.                          meter 12 to a 0 before transmitting the first CRC
  1101.                          or Checksum Xmodem packet.
  1102.  
  1103. More..                     6.5.2.    When an X.25 Host is the receiver and in WXMODEM
  1104.                          mode, it must be sure to set the distant X.3 PAD
  1105.                          parameters to assure that the network will use
  1106.                          X-Off/X-On for flow control between the network and
  1107.                          the transmitter to prevent its buffers from
  1108.                          overflowing.  This is accomplished by sending a
  1109.                          Q-Bit command packet to set X.3 parameter 5 to a 1
  1110.                          prior to the initial handshake.
  1111.  
  1112.  
  1113.  
  1114.  
  1115.  
  1116.  
  1117.  
  1118.           Xmodem, CRC Xmodem, WXmodem
  1119.      June 20, 1986                                                 Page 23
  1120.      ----------------------------------------------------------------------
  1121.  
  1122.      7.   APPENDIX A - CRC CALCULATION RULES
  1123.  
  1124.      The purpose of this appendix is to give non-technical and non mathema-
  1125. More..           tical software writers a cook book approach to calculating the CRC-16
  1126.      used in Xmodem.  We have half accomplished that goal.  The BASIC code
  1127.      in the examples below has been tested on an IBM PC and found to work
  1128.      effectively even at 9600 with compiled Basic.  Some BASIC languages do
  1129.      not offer an XOR function and others do not have MKI$ and CVI functions
  1130.      which simplified the movement of data between data types.  Someday we
  1131.      hope to provide a Commodore C-64/C-128 implementation which simulates
  1132.      XOR, but not today!
  1133.  
  1134.      My thanks go to Chuck Forsberg, Joe Noonan, John Byrns and Stephen
  1135.      Satchell.  Without their help and public domain documents, this would
  1136.      have never been possible.
  1137.  
  1138.           7.1. IBM PC - 8088/8086 Data Structure
  1139.  
  1140.           The Intel 8080 and upward has a feature, convenient only to some
  1141.           electrical engineer somewhere, which places 2 byte (16) bit
  1142.           integers in BYTE REVERSE order in memory.  That is, the least
  1143.           significant byte is placed in memory before the most significant
  1144.           byte for integer operations.  If A$ is one byte containing the
  1145.           number 52 and it is assigned to I% using the ASC function, the
  1146.           binary value (52) ends up in the first byte of I% and the second
  1147. More..                byte is zero.
  1148.                                    Result
  1149.  
  1150.                I%=0                [x'0000']
  1151.                I%=1                [x'0100']
  1152.                A$="A"              [x'41']
  1153.                I%=ASC(A$)          [x'4100']
  1154.                B$=MKI$(I%)         [x'4100']  letter "A" then binary zero
  1155.                I%=CVI(CHR$(0)+A$)  [x'0041']
  1156.                A$=CHR$(65)         [x'41']
  1157.  
  1158.           Once this is understood, many problems with these algorithms goes
  1159.           away.
  1160.  
  1161.           7.2. BASIC Implementation of Bit Shift Method
  1162.  
  1163.           The bit shift method here was converted from the "C" logic
  1164.           presented in Chuck Forsberg's "Xmodem/Ymodem" protocol reference
  1165.           and from an old IBM two page reference guide that Joe Noonan
  1166.           carries with him in his appointment calendar!
  1167.  
  1168.  
  1169. More..      
  1170.  
  1171.  
  1172.  
  1173.  
  1174.           Xmodem, CRC Xmodem, WXmodem
  1175.      June 20, 1986                                                 Page 24
  1176.      ----------------------------------------------------------------------
  1177.  
  1178.  
  1179.           Chucks' "C" code:
  1180.  
  1181.      /*
  1182.       * This function calculates the CRC used by the XMODEM/CRC Protocol
  1183.       * The first argument is a pointer to the message block.
  1184.       * The second argument is the number of bytes in the message block.
  1185.       * The function returns an integer which contains the CRC.
  1186.       * The low order 16 bits are the coefficients of the CRC.
  1187.       */
  1188.      int calcrc(ptr, count)
  1189.      char *ptr;
  1190.      int count;
  1191. More..           {
  1192.          int crc, i;
  1193.  
  1194.          crc = 0;
  1195.          while (--count >= 0) {
  1196.           crc = crc ^ (int)*ptr++ << 8;
  1197.           for (i = 0; i < 8; ++i)
  1198.               if (crc & 0x8000)
  1199.                crc = crc << 1 ^ 0x1021;
  1200.               else
  1201.                crc = crc << 1;
  1202.           }
  1203.          return (crc & 0xFFFF);
  1204.      }
  1205.  
  1206.  
  1207.  
  1208.  
  1209.  
  1210.  
  1211.  
  1212.           Xmodem, CRC Xmodem, WXmodem
  1213. More..           June 20, 1986                                                 Page 25
  1214.      ----------------------------------------------------------------------
  1215.  
  1216.  
  1217.           But in IBM PC BASIC, our implementation looks like:
  1218.  
  1219.      100 DEFINT A-Z 'DEFAULT IS TWO BYTE INTEGERS
  1220.      2000 REM * V$ CONTAINS 133 CHARACTER COMPLETE XMODEM PACKET
  1221.      2010 REM * CRC$ IS TWO BYTE CRC WITH MOST SIGNIFICANT BYTE FIRST
  1222.      2020 CRC$=CHR$(0)+CHR$(0)                      'START AT ZERO
  1223.      2030 FOR I2=4 TO 131
  1224.      2040   A$=MID$(V$,I2,1)
  1225.      2050   GOSUB 4000
  1226.      2060 NEXT I2
  1227.      2070 REM * CRC$ CONTAINS CALCULATED CRC!
  1228.  
  1229.      3000 IF CRC$=MID$(V$,132,2) THEN ....    'IT'S GOOD!!!
  1230.  
  1231.      4000 REM * CRC BITWISE CALCULATION (WHAT A JOKE!)
  1232.      4010 CRCH1=ASC(LEFT$(CRC$,1)) XOR ASC(A$)
  1233.      4020 CRCL1=ASC(RIGHT$(CRC$,1))
  1234.      4030 FOR I3 = 0 TO 7
  1235. More..           4040   CARRY=0 : IF CRCH1 > 127 THEN CARRY=-1  'IS HIGH BIT ON IN CRC?
  1236.      4050   CRCH1=(CRCH1*2) AND 255                 'CRCH << 1 AND 255
  1237.      4060   IF CRCL1>127 THEN CRCH1=CRCH1+1 'IF CRCL CARRIES THEN INCR CRCH
  1238.      4070   CRCL1=(CRCL1*2) AND 255                 'CRCL << 1 AND 255
  1239.      4080   IF CARRY=0 THEN GOTO 4105               'IF HIGH BIT WAS ON,
  1240.      4090   CRCH1=CRCH1 XOR 16                      'XOR WITH &H1021
  1241.      4100   CRCL1=CRCL1 XOR 33
  1242.      4110 NEXT I3
  1243.      4130 CRC$=CHR$(CRCH1)+CHR$(CRCL1)
  1244.      4140 RETURN 'WHEW
  1245.  
  1246.  
  1247.           That routine will execute 128 * 7 + 128 * 9 * 8 BASIC statements
  1248.           for each Xmodem packet or 10112 statements per Xmodem packet!  It
  1249.           will work for low baud rates in compiled BASIC, but just is too
  1250.           much for interpretive BASIC.
  1251.  
  1252.  
  1253.  
  1254.  
  1255.  
  1256.  
  1257. More..      
  1258.           Xmodem, CRC Xmodem, WXmodem
  1259.      June 20, 1986                                                 Page 26
  1260.      ----------------------------------------------------------------------
  1261.  
  1262.           7.3. BASIC Implementation of the Table Method
  1263.  
  1264.           This method is based on routine M4 in Steven Satchell's paper,
  1265.           "Test of CRC Routines for CRC-CCITT", but has some very signifi-
  1266.           cant differences.  A table of 256 CRC's, originally calculated
  1267.           with the bit shift method is used to avoid performing the bit
  1268.           shift during communications.  The table contains the CRC's for
  1269.           each byte value from 0 to 255 when the original CRC is zero.  The
  1270.           result of this calculation is included in the DATA statements in
  1271.           the code.
  1272.  
  1273.           The comments are intended to show what is logically happening
  1274.           rather than physically.  Because of the "byte reverse" nature of
  1275.           integers in the 8088, a logical shift of 8 bits to the left is a
  1276.           physical shift of eight bits to the right!
  1277.  
  1278.  
  1279. More..           200 DEFINT A-Z  'ALL INTEGERS
  1280.      210 DIM CRCTB(256)
  1281.  
  1282.      300 GOSUB 9000 'INITIALIZE CRC TABLES
  1283.  
  1284.      6200 REM * CRC CALCULATION USING TABLE METHOD, V$=XMODEM PACKET
  1285.      6210 CRC$=CHR$(0)+CHR$(0)                 'INITIALIZE TO ZERO
  1286.      6220 FOR Q=4 TO 131
  1287.      6230   CRCH1=ASC(LEFT$(CRC$,1))           'CRC >> 8 AND 255
  1288.      6240   CRCL2=CVI(CHR$(0)+RIGHT$(CRC$,1))  'CRC << 8 AND 255
  1289.      6250   CRC1$=MKI$(CRCTB(CRCH1 XOR ASC(MID$(V$,Q,1))) XOR CRCL2)
  1290.      6260   CRC$=RIGHT$(CRC1$,1)+LEFT$(CRC1$,1) 'SET IT BACK!
  1291.      6270 NEXT Q
  1292.      6280 IF CRC$ <> MID$(V$,N,2) THEN ....... 'GOTO ERROR ROUTINE
  1293.      6290 REM * END OF CRC CALC
  1294.  
  1295.      9000 FOR I%=0 TO 255 ' INITIALIZE CRC TABLE
  1296.      9010   READ CRCTB(I%)
  1297.      9020 NEXT I%
  1298.      9025 RETURN
  1299.      9030 DATA 0, 4129, 8258, 12387, 16516, 20645, 24774, 28903
  1300.      9040 DATA -32504,-28375,-24246,-20117,-15988,-11859,-7730,-3601
  1301. More..           9050 DATA 4657, 528, 12915, 8786, 21173, 17044, 29431, 25302
  1302.      9060 DATA -27847,-31976,-19589,-23718,-11331,-15460,-3073,-7202
  1303.      9070 DATA 9314, 13379, 1056, 5121, 25830, 29895, 17572, 21637
  1304.      9080 DATA -23190,-19125,-31448,-27383,-6674,-2609,-14932,-10867
  1305.      9090 DATA 13907, 9842, 5649, 1584, 30423, 26358, 22165, 18100
  1306.      9100 DATA -18597,-22662,-26855,-30920,-2081,-6146,-10339,-14404
  1307.      9110 DATA 18628, 22757, 26758, 30887, 2112, 6241, 10242, 14371
  1308.      9120 DATA -13876,-9747,-5746,-1617,-30392,-26263,-22262,-18133
  1309.      9130 DATA 23285, 19156, 31415, 27286, 6769, 2640, 14899, 10770
  1310.      9140 DATA -9219,-13348,-1089,-5218,-25735,-29864,-17605,-21734
  1311.      9150 DATA 27814, 31879, 19684, 23749, 11298, 15363, 3168, 7233
  1312.  
  1313.  
  1314.  
  1315.  
  1316.  
  1317.  
  1318.           Xmodem, CRC Xmodem, WXmodem
  1319.      June 20, 1986                                                 Page 27
  1320.      ----------------------------------------------------------------------
  1321.  
  1322.      9160 DATA -4690,-625,-12820,-8755,-21206,-17141,-29336,-25271
  1323. More..           9170 DATA 32407, 28342, 24277, 20212, 15891, 11826, 7761, 3696
  1324.      9180 DATA -97,-4162,-8227,-12292,-16613,-20678,-24743,-28808
  1325.      9190 DATA -28280,-32343,-20022,-24085,-12020,-16083,-3762,-7825
  1326.      9200 DATA 4224, 161, 12482, 8419, 20484, 16421, 28742, 24679
  1327.      9210 DATA -31815,-27752,-23557,-19494,-15555,-11492,-7297,-3234
  1328.      9300 DATA 689, 4752, 8947, 13010, 16949, 21012, 25207, 29270
  1329.      9310 DATA -18966,-23093,-27224,-31351,-2706,-6833,-10964,-15091
  1330.      9320 DATA 13538, 9411, 5280, 1153, 29798, 25671, 21540, 17413
  1331.      9330 DATA -22565,-18438,-30823,-26696,-6305,-2178,-14563,-10436
  1332.      9340 DATA 9939, 14066, 1681, 5808, 26199, 30326, 17941, 22068
  1333.      9350 DATA -9908,-13971,-1778,-5841,-26168,-30231,-18038,-22101
  1334.      9360 DATA 22596, 18533, 30726, 26663, 6336, 2273, 14466, 10403
  1335.      9370 DATA -13443,-9380,-5313,-1250,-29703,-25640,-21573,-17510
  1336.      9380 DATA 19061, 23124, 27191, 31254, 2801, 6864, 10931, 14994
  1337.      9390 DATA -722,-4849,-8852,-12979,-16982,-21109,-25112,-29239
  1338.      9400 DATA 31782, 27655, 23652, 19525, 15522, 11395, 7392, 3265
  1339.      9410 DATA -4321,-194,-12451,-8324,-20581,-16454,-28711,-24584
  1340.      9420 DATA 28183, 32310, 20053, 24180, 11923, 16050, 3793, 7920
  1341.  
  1342.  
  1343.           This method uses 128 * 6 BASIC statements per Xmodem packet or a
  1344.           miserly 768 BASIC statements per packet.  And, if you want, the
  1345. More..                code can be tightened still more.  Unfortunately, any further
  1346.           tightening that we could see would eliminate most of the already
  1347.           limited readability of the code.
  1348.  
  1349.      Xmodem, CRC Xmodem, WXmodem
  1350.      June 20, 1986                                                 Page 28
  1351.      ----------------------------------------------------------------------
  1352.  
  1353.      8.   NOTES AND COMMENTS
  1354.  
  1355.      Please add your notes and comments here or send them to me and I'll get
  1356.      them added to the current copy on People/Link.
  1357.  
  1358.      1.   This was originally set up to ADD 32 to the character on transmit
  1359.           and SUBTRACT 32 on receive.  By using exclusive or with 64, the
  1360.           logic is the same on transmit and receive.
  1361.  
  1362.      2.   The use of the SYN character was added at the request of several
  1363.           people who have coded Xmodem routines and have struggled valiantly
  1364.           to improve their error recovery routines.  Peter Boswell 6/10/86
  1365.  
  1366.      3.   The suggestion that ACK[sequence] be sent for every block received
  1367. More..                was added.          Peter Boswell       6/10/86
  1368.  
  1369.      4.   The original value for the ACK/NAK timeout was 10 seconds.  This
  1370.           was changed to 15 seconds the situation where the receiver is
  1371.           operating at 300 baud and using X-Off to stop receipt of characters
  1372.           during disk I/O.  Peter Boswell, 6/10/86
  1373.  
  1374.  
  1375.  
  1376. You have exceeded your time limit
  1377. Goodbye
  1378.  
  1379. Disconnecting...+ 
  1380.  
  1381.  
  1382.  
  1383.  
  1384.  
  1385.