home *** CD-ROM | disk | FTP | other *** search
/ Columbia Kermit / kermit.zip / protocol / locking-shifts.txt < prev    next >
Text File  |  2020-01-01  |  34KB  |  706 lines

  1.       A LOCKING SHIFT MECHANISM FOR THE KERMIT FILE TRANSFER PROTOCOL
  2.  
  3.                 Christine M. Gianone
  4.                    Frank da Cruz
  5.  
  6.                 Columbia University
  7.                  New York, NY  USA
  8.  
  9.                   DRAFT 4.2
  10.  
  11.                    October 2, 1991
  12.  
  13. ABSTRACT
  14.  
  15. 7-bit communication channels remain quite common: they are in use on IBM
  16. mainframes, public data networks, in virtual terminal protocols like
  17. TCP/IP TELNET, and on any connection in which a device uses parity.
  18.  
  19. The Kermit file transfer protocol achieves transparency over hostile
  20. communication environments by encoding all data as printable characters.  In
  21. the 7-bit communications environment, 8-bit data is encoded in 7-bit form
  22. using the single shift; the "&" character acts as a prefix, meaning that the
  23. following character should have its 8th bit set to 1 after decoding.  Kermit's
  24. single-shift 8th-bit quoting mechanism can add excessive transmission overhead
  25. to certain kinds of files, particularly text encoded in character sets like
  26. ISO 8859 Cyrillic, Greek, Hebrew, or Arabic, or 8-bit Japanese Kanji codes
  27. like EUC in which most data bytes have their 8th bit set to 1, resulting in
  28. 8th-bit quoting overhead up to 100%.
  29.  
  30. A new locking shift mechanism is proposed to allow 8-bit data to be
  31. transferred more efficiently.  This mechanism is an adaptation of the
  32. familiar Shift-In / Shift-Out scheme combined with Kermit's present
  33. single-shift technique, with some quoting rules added.
  34.  
  35. This proposal was prompted not only by the longstanding need for increased
  36. efficiency in this area, but by a conference between the authors and Dr.
  37. Hirofumi Fujii of the Japan National Laboratory for High Energy Physics
  38. regarding the establishment of an official Kermit transfer syntax for
  39. Japanese, the subject of a separate proposal, and subsequent meetings in
  40. Japan.  The algorithm and user interface were designed by Gianone and
  41. the detailed protocol design was contributed by da Cruz in the course of
  42. programming a trial implementation.
  43.  
  44. The reader is assumed to be familiar with the Kermit file transfer protocol
  45. and with commonly used computer character sets.
  46.  
  47. TERMINOLOGY
  48.  
  49. In this proposal, the term "character" refers to an 8-bit byte, or octet,
  50. even if the data is encoded in a multibyte character set, or if it is not
  51. encoded in any character set at all (such as a binary file).
  52.  
  53. An "8-bit character" is a data byte with its 8th bit set to 1.  A "7-bit
  54. character" is one whose 8th bit is set to 0.
  55.  
  56. A "control character" is a byte in the range 0-31 or 127 decimal (the "C0"
  57. set) or 128-159 or 255 (the "C1" set).
  58.  
  59. A "printable character" is any character that is not a control character.
  60.  
  61. NOTATION
  62.  
  63. Numbers are written in decimal.
  64.  
  65. "<XXX>" stands for an ASCII control character.  "XXX" is replaced by the
  66. character's name, for example "<SOH>" for Start of Heading (Control-A).
  67.  
  68. "<1>X" stands for an 8-bit character.  The "X" can be a literal printable
  69. character (for example, "<1>A" is the ASCII letter A with its 8th bit set to
  70. 1) or a control character (for example "<1><SOH>" is a Control-A with its 8th
  71. bit set to 1).
  72.  
  73. Similarly, "<0>X" stands for a 7-bit character.
  74.  
  75. BACKGROUND
  76.  
  77. The Kermit protocol presently specifies three separate prefix characters to
  78. be used within Kermit packets for transparency, compression, and quoting:
  79.  
  80. The Control Prefix
  81.    For transparency on serial communication links that are sensitive to
  82.    control characters, the file sender precedes each C0 and C1 control with
  83.    the control prefix, normally "#" (ASCII 35), and then encodes the control
  84.    character itself by "exclusive-ORing" it with 64 decimal (i.e. inverting
  85.    bit 6) to produce a character in the printable ASCII range.  For example,
  86.    Control-C (ASCII 3) becomes "#C" (3 XOR 64 = 67, which is the ASCII code
  87.    for the letter C).  Similarly, NUL becomes "#@", Control-A becomes "#A",
  88.    Control-Z becomes "#Z", Escape becomes "#[", and DEL becomes "#?".  The
  89.    receiver decodes by discarding the prefix and XORing the character with
  90.    64 again.  For example, in "#C", C = ASCII 67, and 67 XOR 64 = 3 =
  91.    Control-C.  Control prefixing is mandatory.  The control prefix is also
  92.    used for quoting prefix characters that occur in the data itself; see
  93.    "The Prefix Quote" below.
  94.  
  95. The 8th-bit Prefix
  96.    When one or both of the two Kermit programs knows that the connection
  97.    between them is not transparent to the 8th bit (e.g. because the Kermit
  98.    PARITY variable is not NONE, or because the program always operates that
  99.    way), a feature called "8th-bit prefixing" is used if the two Kermit
  100.    programs negotiate an agreement to do so.  The 8th-bit prefix is Kermit's
  101.    single shift, normally the ampersand character "&" (ASCII 38).  When the
  102.    file sender encounters an 8-bit character, it inserts the "&" prefix in
  103.    front of it, and then inserts the data character itself with its 8th bit
  104.    set to 0.  If the data character is a control character, it is inserted
  105.    after the 8th-bit prefix in control-prefixed form.  Examples: an "A" with
  106.    its 8th bit set to 1 ("<1>A") becomes "&A"; a Control-A with its 8th bit
  107.    set to 1 ("<1><SOH>") becomes "&#A".
  108.  
  109. The Repeat-Count Prefix
  110.    The repeat-count prefix provides a simple form of data compression.  It
  111.    is used only when both Kermit programs support this feature and agree to
  112.    use it.  This prefix, normally tilde "~" (ASCII 126), precedes a repeat
  113.    count, which can range from 0 to 94.  The repeat count is encoded as a
  114.    printable ASCII character in the range SP (32) - tilde (126) by adding
  115.    32.  For example, a series of 36 G's would be encoded as "~DG" (D = ASCII
  116.    68 - 32 = 36).  The repeat-count prefix applies to the following prefixed
  117.    sequence, which may be a single character ("~DG"), an 8th-bit prefixed
  118.    character ("~D&G" = 36 Control-G characters with their 8th bits set to
  119.    1), a control-prefixed character ("~D#M" = 36 Control-M's), or an
  120.    8th-bit-and-control-prefixed character ("~~&#Z" = 94 Control-Z's with
  121.    their 8th bits set to 1).
  122.  
  123. The Prefix Quote
  124.    The control prefix, normally "#", is also used to quote the control prefix
  125.    itself if it occurs in the data: "##", means that the "#" character should
  126.    be taken literally.  If 8th-bit prefixing is in effect, the control prefix
  127.    also quotes the 8th-bit prefix: "#&", so "#&D" stands for "&D" rather than
  128.    "<1>D".  If repeat count prefixing is in effect, the control prefix is also
  129.    used to quote the repeat count prefix: "#~", so "#~CG" stands for "~CG"
  130.    rather than 35 "G" characters.  So the complete meaning of the "#" prefix
  131.    is: if the value of the following character is 63-95 or 191-223, the
  132.    prefixed character is to be XORed with 64, otherwise it is to be taken
  133.    literally.  The prefix quote can also be used harmlessly to quote 8th-bit
  134.    or repeat-count prefixing characters even when these types of prefixing are
  135.    not in effect.
  136.  
  137. On a 7-bit connection the file sender, after encoding the data, adds the
  138. appropriate parity bit to all characters -- prefixes as well as data -- before
  139. transmission, and the file receiver strips the parity bit from all received
  140. characters before processing them.
  141.  
  142. On an 8-bit-clean connection, 8th-bit prefixing need not be (and normally is
  143. not) done, and data characters retain their original 8th bit.  For example,
  144. "A" with its 8th bit set to 1 is transmitted literally, without any
  145. prefixing ("<1>A").  Control-A with its 8th bit set to 1 is transmitted as
  146. "#" followed by the letter A with its 8th bit set to 1 ("#<1>A") because
  147. control prefixing is always in effect.
  148.  
  149. SINGLE AND LOCKING SHIFTS
  150.  
  151. The shift key on a typewriter lets the regular keys do "double duty".  A
  152. given key produces different results depending on whether the shift key is
  153. up or down.  Kermit's single shift (8th-bit prefix) is like the shift key:
  154. just as you must press two keys on the typewriter for every uppercase
  155. letter, Kermit must send two 7-bit characters for every 8-bit character when
  156. 8th-bit prefixing is in effect.
  157.  
  158. Certain types of files have many 8-bit characters in a row.  When this is
  159. the case, the overhead of single shifting could be as high as 100%.
  160. Efficiency could be much improved by the use of "locking shifts": the file
  161. sender tells the file receiver "Here comes a sequence of 8-bit characters"
  162. and then sends these characters in 7-bit form, relying on the receiver to
  163. put their 8th bits back before storing them.
  164.  
  165. The locking shift behaves like the shift-lock key on a typewriter: to type a
  166. series of uppercase letters, you press the shift lock key once and then type
  167. the letters, one key per letter, rather than two.  To go back to lowercase
  168. letters, release the shift lock key and then type more letters.
  169.  
  170. When the data communications "shift-lock" key is active, 7-bit characters
  171. are said to be "shifted": they are not what they appear to be, but instead
  172. represent 8-bit characters.  When the locking shift is not in effect, 7-bit
  173. characters stand for themselves; they are "unshifted".
  174.  
  175. The locking shift characters are SO (Shift Out, Control-N, ASCII 14), and SI
  176. (Shift In, Control-O, ASCII 15).  SO is sent at the beginning of a shifted
  177. sequence, SI is sent to return to normal unshifted operation.  For example,
  178. on a 7-bit connection, the following string of characters (written using our
  179. notation):
  180.  
  181.    <0>A<0>B<0>C<1>D<1>E<1>F<1>G<1>H<1>I<0>J<0>K<0>L<0>M  (13 characters)
  182.  
  183. would be transmitted like this with single shifts:
  184.  
  185.    ABC&D&E&F&G&H&IJKLM                                   (19 characters)
  186.  
  187. and like this with locking shifts:
  188.  
  189.    ABC<SO>DEFGHI<SI>JKLM                                 (15 characters)
  190.  
  191. On an 8-bit connection, of course, the string of 13 characters can be
  192. transmitted as-is, with no overhead at all.
  193.  
  194. Now suppose we have the following character sequence:
  195.  
  196.    <1>A<1>B<1>C<0>D<1>E<1>F<1>G<0>H<1>I<1>J<1>K<0>L<1>M  (13 characters)
  197.  
  198. Here several isolated 7-bit characters are found in the middle of a long run
  199. of 8-bit characters.  Using locking shifts alone, this would be encoded as:
  200.  
  201.   <SO>ABC<SI>D<SO>EFG<SI>H<SO>IJK<SI>L<SO>M              (20 characters)
  202.  
  203. But using a combination of locking and single shifts, it can be encoded more
  204. compactly, as in this example, in which "&" is the single-shift character:
  205.  
  206.   <SO>ABC&DEFG&HIJK&LM                                   (17 characters)
  207.  
  208. This proposal adds the locking Shift-In/Shift-Out mechanism to the Kermit
  209. file transfer in a way that it can be used in conjunction with single shifts
  210. for maximum efficiency.
  211.  
  212. NEGOTIATION
  213.  
  214. Locking shifts are, like all new additions, an optional feature of the
  215. Kermit protocol.  To allow old Kermit programs to interoperate transparently
  216. with the new ones that implement locking shifts, the use of this feature
  217. must be negotiated and agreed upon by both Kermit programs before it can be
  218. used.
  219.  
  220. Two Kermit programs agree to use the locking shift extension via a new
  221. capability bit, together with the existing 8th-bit prefixing (QBIN) field.
  222. The capabilities mask is the 10th character in the initialization string.  It
  223. contains a bit mask encoded as a printable character by adding 32 (ASCII
  224. Space).
  225.  
  226. Capability number 1 (bit 5, which until now has been reserved for future
  227. use) will be used to indicate the locking shift capability: 1 if enabled, 0
  228. if not.  Thus old Kermits automatically disable the use of locking shifts
  229. because they never set this bit.  The format of Kermit's capability mask is:
  230.  
  231.     bit7 bit6 bit5 bit4 bit3 bit2 bit1 bit0
  232.    +----+----+----+----+----+----+----+----+
  233.    |  X |  X |  1 |  2 |  3 |  4 |  5 |  Z |
  234.    +----+----+----+----+----+----+----+----+
  235.  
  236. where:
  237.  
  238.    X = Must not be used
  239.    1 = Locking Shift Capability  
  240.    2 = Extra-Long Packet Capability (9025-857374)
  241.    3 = Attribute Packet Capability
  242.    4 = Sliding Window Capability
  243.    5 = Long Packet Capability (95-9024)
  244.    Z = Capability Mask Extension Bit (allows addition of new mask bytes)
  245.  
  246. The locking shift protocol is used if and only if:
  247.  
  248.    1. The file sender sets the Locking Shift Capability bit in the S (Send
  249.       Initialization) packet;
  250.  
  251.    2. The file receiver also sets the same bit in its acknowledgement to the S
  252.       packet; and 
  253.  
  254.    3. The parties have agreed to use single shifts via the QBIN field.
  255.  
  256. Thus, locking shifts REQUIRE 8th-bit prefixing.  This is reasonable because
  257. (a) 8th-bit prefixing is easy to program; (b) all the popular Kermit programs
  258. already implement it; (c) little is gained by using locking shifts without
  259. single shifts; (d) it simplifies the user interface and the negotiation
  260. process; and (e) it allows the file receiver as well as the sender to request
  261. locking shifts.
  262.  
  263. ENCODING RULES
  264.  
  265. Kermit's locking shift protocol uses the C0 control character Shift Out (SO,
  266. Control-N, ASCII 14) to precede a sequence of 8-bit characters, and Shift In
  267. (SI, Control-O, ASCII 15) to precede a sequence of 7-bit characters.
  268.  
  269. Whether or not locking shift protocol is in effect, all of Kermit's normal or
  270. negotiated prefixing rules also remain in effect, so SO appears in the packet
  271. as "#N" and SI appears as "#O".
  272.  
  273. Each Kermit program maintains a SHIFT-STATE, which may be SHIFTED (shifted
  274. out) or UNSHIFTED (shifted in).  SHIFTED means that 8-bit characters are
  275. being transmitted in 7-bit form (preceded by a Shift-Out character) and
  276. UNSHIFTED means that 7-bit characters represent themselves.  For each file,
  277. the initial SHIFT-STATE is defined to be UNSHIFTED, so there is no need for
  278. the sender to transmit an initial Shift-In (but it does no harm).
  279.  
  280. A. When the file sender's SHIFT-STATE is UNSHIFTED and it reads a 7-bit
  281.    character, it adds the character to the packet according to Kermit's
  282.    other prefixing rules (control and repeat count), and adds the
  283.    appropriate parity bits.  Thus, any number of 7-bit characters can be
  284.    transmitted in a row.
  285.  
  286. B. When the file sender's SHIFT-STATE is UNSHIFTED and it reads an 8-bit
  287.    data character, there are two possibilities:
  288.  
  289.    1. If single-shifting (8th-bit prefixing) is in effect, insert a
  290.       single-shift character ("&") with the appropriate parity bit before
  291.       the 8-bit data character, and add the data character itself with its
  292.       8th bit replaced by the appropriate parity bit.
  293.  
  294.       OR:
  295.  
  296.    2. Insert a Shift Out (SO) character into the packet (encoded as "#N"
  297.       with the appropriate parity bits), change the SHIFT-STATE to SHIFTED,
  298.       and then add the data character with its 8th bit replaced by the
  299.       appropriate parity bit.
  300.  
  301. C. When the file sender's SHIFT-STATE is SHIFTED and it reads an 8-bit
  302.    character, it adds the character to the packet according to Kermit's
  303.    other prefixing rules (control and repeat count), replacing the
  304.    character's 8th bit by the appropriate parity bit.  Thus, any number of
  305.    8-bit characters may be transmitted in a row in 7-bit form after the SO.
  306.  
  307. D. When the file sender's SHIFT-STATE is SHIFTED and a 7-bit character is
  308.    encountered, there are two possibilities:
  309.  
  310.    1. If single-shifting is in effect, insert a single-shift character ("&")
  311.       before the 7-bit character and add the appropriate parity bits.
  312.  
  313.       OR:
  314.  
  315.    2. Insert a Shift-In (SI) character (encoded as "#O" with the appropriate
  316.       parity bits) into the packet and change the SHIFT-STATE to UNSHIFTED,
  317.       and then insert the data character itself with the appropriate parity
  318.       bit.
  319.  
  320. E. If a repeated sequence of characters occurs where the shift state changes,
  321.    the locking shift is encoded BEFORE the repeat-count sequence: #O~xA,
  322.    not ~x#OA.
  323.  
  324. F. If the file ends in SHIFTED state, there is no need to issue a Shift-In
  325.    code at the end of the file, but it does no harm either.
  326.  
  327. SINGLE AND LOCKING SHIFTS
  328.  
  329. When locking shifts and single shifts are in effect, the meaning of the
  330. single-shift character is reversed when the SHIFT-STATE is SHIFTED.  Single
  331. shifts can be used to efficiently encode isolated characters that don't fit
  332. the current SHIFT-STATE.  For example:
  333.  
  334.    Data                                   Encoding
  335.  
  336. 1. ABCABC<1>EBCABC                        ABCABC&EBCABC  
  337. 2. <1>A<1>B<1>C<1>A<1>BXY<1>B<1>C<1>A     #NABCAB&X&YBCA
  338.  
  339. In (1) the single shift "&" sets the 8th bit of "E" to 1 (normal Kermit
  340. practice), but in (2) the single shift sets the 8th bit of "X" and "Y" to 0
  341. because the SHIFT-STATE is SHIFTED (#N).
  342.  
  343. The file sender can decide whether to use single or locking shifts by
  344. looking ahead in the input file data.  Single shifts are more efficient when
  345. there are one, two, or three n-bit characters in a row; locking shifts are
  346. more efficient when there are five or more n-bit characters in a row (n is
  347. either 7 or 8):
  348.  
  349.    Single Shift         Locking Shift
  350.    &A          (2)      #OA#N     (5)  (worse)
  351.    &A&B        (4)      #OAB#N    (6)  (worse)
  352.    &A&B&C      (6)      #OABC#N   (7)  (worse)
  353.    &A&B&C&D    (8)      #OABCD#N  (8)  (same)
  354.    &A&B&C&D&E (10)      #OABCDE#N (9)  (better)
  355.  
  356. Thus five-character lookahead is sufficient to make the best decision.
  357.  
  358. REPEAT COUNTS AND LOCKING SHIFTS
  359.  
  360. A repeated sequence of 8-bit characters that occurs while in UNSHIFTED state,
  361. for example abc<1>X<1>X<1>X<1>X, can be encoded by using a single shift:
  362.  
  363.    abc~$&X
  364.  
  365. A repeated sequence of 8-bit characters that occurs while in SHIFTED
  366. state, for example:
  367.  
  368.    abc<1>A<1>B<1>C<1>X<1>X<1>X<1>X<1>X<1>X<1>X<1>X<1>D<1>E<1>F
  369.  
  370. is encoded using the same repeat-count notation:
  371.  
  372.    abc#NABC~(XDEF
  373.  
  374. Just as the # and & prefixes are used as prefixes in both UNSHIFTED and
  375. SHIFTED states, so is the repeat-count prefix, ~.  The same sequence could
  376. also be encoded less efficiently as:
  377.  
  378.    abc#NABC#O~$&X#NDEF
  379.  
  380. PREFIX CHARACTERS THAT OCCUR IN THE DATA
  381.  
  382. Since Kermit prefix characters can occur within file data, they must be
  383. prefixed to distinguish them from true prefixes.  The following encoding
  384. is used:
  385.           STATE..............
  386.   CHARACTER   UNSHIFTED   SHIFTED
  387.       #         ##          &##
  388.       &         #&          &#&
  389.       ~         #~          &#~
  390.    <1>#         &##         ##
  391.    <1>&         &#&         #&
  392.    <1>~         &#~         #~
  393.  
  394. QUOTING THE LOCKING SHIFT CHARACTERS
  395.  
  396. Since Control-O and Control-N can appear within file data, there has to be
  397. a way to distinguish the use of these characters as locking shifts from
  398. their use as data characters.
  399.  
  400. When (and only when) locking shift protocol is in effect, SO and SI
  401. characters that appear in the data must be prefixed by Data Link Escape
  402. (DLE, Control-P, ASCII 16), normally encoded as "#P".  If DLE itself appears
  403. in the file, it too must be prefixed by DLE.
  404.  
  405. The DLE character applies to the ENTIRE PREFIXED SEQUENCE that follows it.
  406. This may be a single character, a control-prefixed character, an 8th-bit
  407. prefixed character, or a repeat-count-prefixed sequence of any combination
  408. of these.  To illustrate the difference between quoting by "#" and DLE,
  409. "##O" indicates a literal "#" character followed by the letter "O", whereas
  410. "<DLE>#O" indicates a literal Control-O.  In practice, the file sender
  411. should use DLE only to prefix SO, SI, and itself, but the receiver should
  412. treat DLE as a general "prefixed sequence" quote: it should discard the DLE,
  413. decode the following prefixed sequence, and treat the result as data rather
  414. than Kermit protocol information.
  415.  
  416. Should a repeated sequence of SO's, SI's, or DLE's occur within the data,
  417. the entire sequence may be encoded with a repeat count and prefixed by a
  418. single DLE, which applies to all copies of the repeated character.  For
  419. example, "#P~A#N" indicates 33 SO characters in a row that are not to be
  420. treated as locking shifts.
  421.  
  422. When locking shift protocol is in effect, we must handle the C1 counterparts
  423. of SO, SI, and DLE (that is, using our notation, <1>SO, <1>SI, and <1>DLE).
  424. These characters would be inserted into the packet in their 7-bit form when
  425. the SHIFT-STATE is SHIFTED, and the receiver would have no way of
  426. distinguishing a data #O from a Shift-In #O, or a data #N from a Shift-Out #N,
  427. or a data #P from a quoting #P.  Therefore these characters too should be
  428. prefixed by DLE when in SHIFTED state.
  429.  
  430. If a 7-bit SO, SI, or DLE appears in the data during SHIFTED state, the file
  431. sender can "single-shift" it in the normal manner, for example "&#O".  The
  432. file receiver must treat such sequences as literal data characters, as if
  433. they had been prefixed by DLE, not as shifts and quotes.
  434.  
  435. The rule, therefore, is that if #O, #N, and #P have no prefix of any kind,
  436. then they are used for shifting and quoting.  When these characters are
  437. prefixed by either "&" or DLE, no matter what the SHIFT-STATE is, they are
  438. data characters:
  439.  
  440.   File                   SHIFT-STATE
  441.   Character        UNSHIFTED       SHIFTED
  442.    SI               #P#O            &#O or #P&#O
  443.    <1>SI            &#O or #P&#O    #P#O
  444.    SO               #P#N            &#N or #P&#N
  445.    <1>SO            &#N or #P&#N    #P#N
  446.    DLE              #P#P            &#P or #P&#P
  447.    <1>DLE           &#P or #P&#P    #P#P
  448.  
  449. The "&#O" form need not be prefixed by "#P", but no harm is done if it is.
  450. The packet receiver must respond to these prefixed sequences as follows:
  451.  
  452.   Packet                 SHIFT-STATE
  453.   Sequence          UNSHIFTED      SHIFTED
  454.    #O                Discard*       Shift Out
  455.    #P#O              Literal SI     Literal <1>SI
  456.    &#O or #P&#O      Literal <1>SI  Literal SI
  457.    #N                Shift In       Discard*
  458.    #P#N              Literal SO     Literal <1>SO
  459.    &#N or #P&#N      Literal <1>SO  Literal SO
  460.    #P                Quote          Quote
  461.    #P#P              Literal DLE    Literal <1>DLE
  462.    &#P or #P&#P      Literal <1>DLE Literal DLE
  463.  
  464. The "Discard*" entries are for when a redundant shift is received, for
  465. example an unprefixed Shift-Out when the Kermit receiver is already shifted
  466. out.  Redundant shifts do not affect the current SHIFT-STATE and are not
  467. interpreted as data; they are simply ignored and discarded by the receiver.
  468.  
  469. BOUNDARY CONDITIONS
  470.  
  471. Although sequences of characters prefixed by "#", "&", or "~" may not be
  472. broken across packet boundaries, locking shifts are effective across packet
  473. boundaries.  However, locking shifts are not effective across file
  474. boundaries; when a group of files is being transferred, the SHIFT-STATE must
  475. be set to UNSHIFTED at the beginning of each file.
  476.  
  477. THE FILE RECEIVER
  478.  
  479. The file receiver has no decisions to make, it is totally driven by the
  480. sequence of characters in each packet it receives.  The receiver operates as
  481. it does without the locking shift protocol, but with additional rules: it must
  482. recognize the locking shift indicators "#N" and "#O", set the SHIFT-STATE to
  483. SHIFTED when it sees "#N" and to UNSHIFTED when it sees "O", and set the value
  484. of the 8th bit of each data character according to the current SHIFT-STATE.
  485. It must treat #, &, and ~ as prefix characters even when the SHIFT-STATE is
  486. SHIFTED, remembering that the meaning of the single-shift prefix "&" is
  487. inverted.  (The file receiver can also store the shift characters as is -- see
  488. the COMMANDS section below.)
  489.  
  490. COMMANDS
  491.  
  492. One new command is required:
  493.  
  494.    SET TRANSFER LOCKING-SHIFT { ON, OFF, FORCED }
  495.  
  496. The options are as follows:
  497.  
  498. ON: Enables the use of locking shifts.  The Kermit program sets the locking
  499.    shift capability bit in any S or I packets it sends, or in any
  500.    acknowledgement to an S or I packet.  Locking shifts are actually used if
  501.    and only if both Kermits set this bit AND single-shifts are successfully
  502.    negotiated.  If a Kermit program implements the locking shift protocol,
  503.    the default TRANSFER LOCKING-SHIFT setting should be ON.
  504.  
  505. OFF: Disables the use of locking shifts.  The Kermit program sets the
  506.    locking shift capability bit to zero in all negotiation packets, and
  507.    treats SO, SI, and DLE as ordinary data characters in Kermit data
  508.    packets.
  509.  
  510. FORCED: Forces the use of locking shifts, regardless of the PARITY setting and
  511.    capability negotiation.  The file sender sets the locking shift bit in the
  512.    capability mask, sets the QBIN (8th-bit prefix) field to "N", and ignores
  513.    the receiver's reply.  The file receiver sets the same values, regardless
  514.    of the sender's values.  A Kermit program that has been given this command
  515.    acts as if locking shift protocol had been successfully negotiated and
  516.    single shifts have been disabled.
  517.  
  518. With these facilities and defaults in effect, the Kermit user will get
  519. locking shift protocol automatically whenever PARITY is not NONE and both
  520. Kermits support locking shifts (which implies they also support single shifts
  521. and that single shifts were negotiated successfully).
  522.  
  523. SET TRANSFER LOCKING-SHIFT FORCED can be used to force the file sender to
  524. use locking shifts even if the receiver doesn't understand this protocol, or
  525. to force the file receiver to treat SO/SI/DLE codes in arriving files as
  526. prescribed by this proposal.  This allows an 8-bit data file to be sent
  527. through a 7-bit connection to a Kermit program that does not implement
  528. 8th-bit prefixing or locking shifts.  The result can displayed on terminals
  529. or printers that respond appropriately to Shift-In/Shift-Out codes, sent
  530. through e-mail, or postprocessed with a simple SO/SI filter to reconstruct
  531. it, provided the original file does not contain SO, SI, or DLE characters.
  532. If a file containing SO/SI codes is sent to a Kermit program with SET
  533. TRANSFER LOCKING-SHIFT FORCED in effect, the data is reconstructed according
  534. to the imbedded shifts.
  535.  
  536. The SET TRANSFER LOCKING-SHIFT FORCED option is, of course, risky, and can
  537. result in undesired effects if used improperly.  For example, if the file
  538. contains SO or SI characters as data, the shift state can become inverted.
  539. Furthermore, DLE does not serve to "quote" SO or SI characters in ordinary
  540. data communication; SO and SI usually act as locking shifts even when
  541. preceded by DLE (or any other character).  For example, when the sequence
  542. "<SI>ABC<DLE><SO>DEF" is sent to a VT300 terminal, the DLE is ignored and
  543. the characters DEF are shifted.
  544.  
  545. Here are the possible SET TRANSFER LOCKING-SHIFT combinations and their
  546. effects.  The OFF entries also apply to Kermit programs that don't implement
  547. locking shift protocol at all:
  548.  
  549.   Sender   Receiver   Effect
  550.    ON       ON         Locking shift protocol done if single shifts negotiated
  551.    ON       OFF        No locking shifts
  552.    ON       FORCED     SO/SI/DLE in data interpreted as shifts by receiver
  553.    OFF      ON         No locking shifts
  554.    OFF      OFF        No locking shifts
  555.    OFF      FORCED     SO/SI/DLE in data interpreted as shifts by receiver
  556.    FORCED   ON         Sender adds shifts, receiver stores them as data (*)
  557.    FORCED   OFF        Sender adds shifts, receiver stores them as data
  558.    FORCED   FORCED     Locking shift protocol is done with no single shifts
  559.  
  560. (*) Sender announces that it WON'T do single shifts, which disables
  561.     the receiver's locking-shift protocol.
  562.  
  563. CHARACTER SET TRANSLATION
  564.  
  565. SET TRANSFER LOCKING-SHIFT FORCED (or any other LOCKING-SHIFT settting)
  566. does not affect character set translation.  Translation is still done if the
  567. user has elected to do it.
  568.  
  569. Here are the possibilities when the sender has SET LOCKING-SHIFT FORCED and
  570. has announced an 8-bit transfer character set in the Attribute packet, and the
  571. receiver supports character-set translation, but is not doing LS protocol:
  572.  
  573. 1. Receiver translates the transfer character set into an 8-bit file
  574.    character set whose first 128 characters are ASCII, such as an IBM code
  575.    page, KOI-8, the Apple or NeXT character set, etc.  In this case, the
  576.    desired effect is achieved automatically.
  577.  
  578. 2. Receiver translates the transfer character set into a 7-bit file
  579.    character set such as an ISO 646 NRC or Short KOI.  In this case the
  580.    result is garbage.  Locking shifts should not be used here.  For the
  581.    languages covered by ISO 646 NRCs, single shifts are more efficient.
  582.  
  583. 3. The receiver does not understand the transfer character set.  The
  584.    situation here is no different with locking shifts than without them.
  585.  
  586. PERFORMANCE
  587.  
  588. A preliminary implementation of the shifting algorithms described in this
  589. proposal was coded and tested on a large number of text and binary files and
  590. worked correctly: the result of encoding and then decoding each file was
  591. identical to the original.  All combinations of single shift, locking shift,
  592. and repeat-count compression were tested successfully in both text and
  593. binary file mode.
  594.  
  595. The following table shows the number of characters required to encode files of
  596. different representative types (taken from a much larger sample) using
  597. different combinations of single shifts (SS) and locking shifts (LS), but
  598. without repeat-count compression (R).  For comparison, the final column
  599. includes repeat-count compression.  The number in parentheses is the
  600. "expansion factor" showing how much the data grew in the encoding process.
  601. The .TXT files were encoded in text mode, the others were encoded in binary
  602. mode.
  603.  
  604. File               Encoding..................................................
  605. Name       Length  SS...........  LS...........  LS+SS........  LS+SS+R......
  606.  
  607. ASCII.TXT  190689  202173 (1.06)  202126 (1.06)  202173 (1.06)  194938 (1.02)
  608. GERMAN.TXT  39611   42159 (1.06)   43336 (1.09)   42169 (1.06)   41558 (1.05)
  609. FRENCH.TXT 108021  116426 (1.08)  124446 (1.15)  116446 (1.08)  115531 (1.07)
  610.  
  611. CYRILL1.TXT 52046   95700 (1.84)   80998 (1.56)   64602 (1.24)   64476 (1.24)
  612. CYRILL2.TXT 13699   25293 (1.85)   23429 (1.71)   18306 (1.34)   18078 (1.32) 
  613. CYRILL3.TXT 28434   49834 (1.75)   43029 (1.51)   37104 (1.30)   35519 (1.25)
  614. CYRILL4.TXT 51011   89419 (1.75)   78217 (1.53)   63157 (1.24)   63010 (1.24)
  615.   Cyrillic
  616.   Totals   145190  260246 (1.79)  225673 (1.55)  183169 (1.26)  181083 (1.25)
  617.  
  618. KANJI.TXT   29706   59494 (2.00)   32527 (1.09)   32629 (1.10)   32648 (1.10)
  619. KANJIA.TXT 106943  157536 (1.47)  122043 (1.14)  121822 (1.14)  118563 (1.11)
  620.   Kanji
  621.   Totals   136649  217030 (1.59)  154570 (1.13)  154451 (1.13)  151211 (1.11)
  622.  
  623. MSVIBM.EXE 146989  247766 (1.69)  302348 (2.06)  248991 (1.69)  210598 (1.43)
  624. WERMIT     419861  737812 (1.76)  923451 (2.20)  760912 (1.81)  713830 (1.70)
  625. FILE.ZIP    96911  173145 (1.79)  226407 (2.34)  172627 (1.78)  172841 (1.78) 
  626.  
  627. ASCII.TXT is a plain US ASCII text file containing English prose and no
  628. 8-bit characters.  GERMAN.TXT and FRENCH.TXT are German- and French-language
  629. documents coded in ISO 8859-1 Latin Alphabet 1.  
  630.  
  631. CYRILL1.TXT is a chapter from a Russian computer book, containing only a few
  632. English words.  CYRILL2.TXT is a poem, The Bronze Horseman by Pushkin; its
  633. lines are short and there are many blank lines so there is a higher CRLF-to-
  634. text ratio.  CYRILL3.TXT is "Murphy's Laws" in Russian, in which lines tend to
  635. be short, blank, or indented.  CYRILL4 is a RussTeX source file in which the
  636. TeX commands are ASCII and the text is Cyrillic.  The Cyrillic text in all
  637. these files is ISO 8859-5 Latin/Cyrillic 8-bit text.
  638.  
  639. KANJI.TXT is a Japanese-language text file encoded in the Japanese EUC code.
  640. KANJIA.TXT contains a mixture of ASCII English and Japanese Kanji encoded in
  641. EUC.
  642.  
  643. MSVIBM.EXE is an IBM PC binary executable program image.  WERMIT is a SUN-4
  644. (Sparc) binary executable program image.  FILE.ZIP is a binary MS-DOS ZIP
  645. archive.
  646.  
  647. ANALYSIS
  648.  
  649. For binary files, locking (combined) shifts generally provide no benefit
  650. over single shifts.  These files tend to have a high percentage of bytes in
  651. the C0 and C1 ranges, and therefore suffer high overhead from control
  652. prefixing.  Furthermore, they rarely have long runs of 8-bit characters.
  653. The reason the combined shift is less efficient than the single shift is the
  654. necessity to quote SO, SI, and DLE characters that occur in the data.
  655.  
  656. For text files encoded in "left-handed" 8-bit character sets such as ISO
  657. 8859 Latin Alphabets 1-4 and 9 (for languages based on Roman characters),
  658. 8-bit characters generally occur only in isolation, and so locking
  659. (combined) shifts provide no significant benefit over single shifts.
  660.  
  661. Locking and combined shifts provide a substantial performance improvement
  662. over single shifts for text files written in "right-handed" 8-bit character
  663. sets like the Latin Arabic, Cyrillic, Greek, and Hebrew alphabets where long
  664. sequences of 8-bit bytes predominate, and for certain multibyte character
  665. sets like as Japanese EUC, in which all Kanji-character bytes have their 8th
  666. bits set to 1.
  667.  
  668. CONCLUSION
  669.  
  670. The locking shift algorithm is easy to program and is inexpensive in both
  671. execution time and code space.  Implementation of locking shift protocol is
  672. recommended for Kermit programs that must transfer files likely to contain
  673. many sequences of 5 or more consecutive 8-bit GR bytes over 7-bit
  674. communication channels.  Such files tend to be text files encoded in the ISO
  675. character sets for non-Roman alphabets and in EUC Kanji codes, but there might
  676. be other candidates too: binary image (raster) data, spreadsheet data, etc.
  677. For such files, the efficiency improvement can approach 100%.
  678.  
  679. REFERENCES
  680.  
  681. Gianone, Christine M., "A Kermit Protocol Extension for International
  682. Character Sets", Columbia University (1990).
  683.  
  684. da Cruz, Frank, "Kermit, A File Transfer Protocol", Digital Press (1987).
  685.  
  686. ANSI X3.4 (1986), "Coded Character Sets - 7-bit American Standard Code for
  687. Information Interchange".
  688.  
  689. ISO 2022, "Information processing - ISO 7-bit and 8-bit coded character
  690. sets - Code extension techniques" (1985).
  691.  
  692. ISO 8859, "Information processing - 8-bit single-byte coded graphic
  693. character sets", parts 1-9 (1987-present)
  694.  
  695. "JIS X 0212 Study Group Interim Report"
  696.  
  697. ACKNOWLEDGEMENTS
  698.  
  699. Thanks to John Chandler, John Klensin, Paul Placeway, and Konstantin
  700. Vinogradov for their detailed comments on this proposal, and to Gisbert W.
  701. Selke for the German file, Andre' Pirard for the French, Konstantin
  702. Vinogradov and Dimitri Vulis for the Russian files, and Hirofumi Fujii for
  703. the Japanese files.
  704.  
  705. (The End)
  706.