RFC 2152 INFORMATIONAL EMAIL · UNICODE · ENCODING

UTF-7 A Mail-Safe Transformation Format of Unicode

A way to write any Unicode text using only 7-bit ASCII, so it survives old mail systems.

On rfc-editor.org ↗
PUBLISHED
May 1997
AUTHORS
D. Goldsmith, M. Davis
STREAM
Legacy
BUILDS ON
RFC 1642 — UTF-7
SHIFT CHARACTER
+ (decimal 43)
TERMINATOR
- (decimal 45), absorbed
LITERAL PLUS
+-
MIME CHARSET TAG
UTF-7
AVERAGE COST OUTSIDE ASCII
2 2/3 octets per character plus shift octets
UNICODE ENCODING
16 bit
DOI
10.17487/RFC2152
ERRATA
Reported errata

RFC text

Contents
Original text · RFC Editor View on rfc-editor.org ↗
Network Working Group                                       D. Goldsmith
Request for Comments: 2152                          Apple Computer, Inc.
Obsoletes: RFC 1642                                             M. Davis
Category: Informational                                   Taligent, Inc.
                                                                May 1997


                                 UTF-7

              A Mail-Safe Transformation Format of Unicode

Status of this Memo

   This memo provides information for the Internet community.  This memo
   does not specify an Internet standard of any kind.  Distribution of
   this memo is unlimited.

Abstract

   The Unicode Standard, version 2.0, and ISO/IEC 10646-1:1993(E) (as
   amended) jointly define a character set (hereafter referred to as
   Unicode) which encompasses most of the world's writing systems.
   However, Internet mail (STD 11, RFC 822) currently supports only 7-
   bit US ASCII as a character set. MIME (RFC 2045 through 2049) extends
   Internet mail to support different media types and character sets,
   and thus could support Unicode in mail messages. MIME neither defines
   Unicode as a permitted character set nor specifies how it would be
   encoded, although it does provide for the registration of additional
   character sets over time.

   This document describes a transformation format of Unicode that
   contains only 7-bit ASCII octets and is intended to be readable by
   humans in the limiting case that the document consists of characters
   from the US-ASCII repertoire. It also specifies how this
   transformation format is used in the context of MIME and RFC 1641,
   "Using Unicode with MIME".

Motivation

   Although other transformation formats of Unicode exist and could
   conceivably be used in this context (most notably UTF-8, also known
   as UTF-2 or UTF-FSS), they suffer the disadvantage that they use
   octets in the range decimal 128 through 255 to encode Unicode
   characters outside the US-ASCII range. Thus, in the context of mail,
   those octets must themselves be encoded. This requires putting text
   through two successive encoding processes, and leads to a significant
   expansion of characters outside the US-ASCII range, putting non-
   English speakers at a disadvantage. For example, using UTF-8 together
   with the Quoted-Printable content transfer encoding of MIME
   represents US-ASCII characters in one octet, but other characters may
   require up to nine octets.

Overview

   UTF-7 encodes Unicode characters as US-ASCII octets, together with
   shift sequences to encode characters outside that range. For this
   purpose, one of the characters in the US-ASCII repertoire is reserved
   for use as a shift character.

   Many mail gateways and systems cannot handle the entire US-ASCII
   character set (those based on EBCDIC, for example), and so UTF-7
   contains provisions for encoding characters within US-ASCII in a way
   that all mail systems can accomodate.

   UTF-7 should normally be used only in the context of 7 bit
   transports, such as mail. In other contexts, straight Unicode or
   UTF-8 is preferred.

   See RFC 1641, "Using Unicode with MIME" for the overall specification
   on usage of Unicode transformation formats with MIME.

Definitions

   First, the definition of Unicode:

      The 16 bit character set Unicode is defined by "The Unicode
      Standard, Version 2.0". This character set is identical with the
      character repertoire and coding of the international standard
      ISO/IEC 10646-1:1993(E); Coded Representation Form=UCS-2;
      Subset=300; Implementation Level=3, including the first 7
      amendments to 10646 plus editorial corrections.

      Note. Unicode 2.0 further specifies the use and interaction of
      these character codes beyond the ISO standard. However, any valid
      10646 sequence is a valid Unicode sequence, and vice versa;
      Unicode supplies interpretations of sequences on which the ISO
      standard is silent as to interpretation.

   Next, some handy definitions of US-ASCII character subsets:

      Set D (directly encoded characters) consists of the following
      characters (derived from RFC 1521, Appendix B, which no longer
      appears in RFC 2045): the upper and lower case letters A through Z
      and a through z, the 10 digits 0-9, and the following nine special
      characters (note that "+" and "=" are omitted):

               Character   ASCII & Unicode Value (decimal)
                  '           39
                  (           40
                  )           41
                  ,           44
                  -           45
                  .           46
                  /           47
                  :           58
                  ?           63

      Set O (optional direct characters) consists of the following
      characters (note that "\" and "~" are omitted):

               Character   ASCII & Unicode Value (decimal)
                  !           33
                  "           34
                  #           35
                  $           36
                  %           37
                  &           38
                  *           42
                  ;           59
                  <           60
                  =           61
                  >           62
                  @           64
                  [           91
                  ]           93
                  ^           94
                  _           95
                  '           96
                  {           123
                  |           124
                  }           125

   Rationale. The characters "\" and "~" are omitted because they are
   often redefined in variants of ASCII.

   Set B (Modified Base 64) is the set of characters in the Base64
   alphabet defined in RFC 2045, excluding the pad character "="
   (decimal value 61).

   Rationale. The pad character = is excluded because UTF-7 is designed
   for use within header fields as set forth in RFC 2047. Since the only
   readable encoding in RFC 2047 is "Q" (based on RFC 2045's Quoted-
   Printable), the "=" character is not available for use (without a lot
   of escape sequences). This was very unfortunate but unavoidable. The
   "=" character could otherwise have been used as the UTF-7 escape
   character as well (rather than using "+").

   Note that all characters in US-ASCII have the same value in Unicode
   when zero-extended to 16 bits.

UTF-7 Definition

   A UTF-7 stream represents 16-bit Unicode characters using 7-bit US-
   ASCII octets as follows:

      Rule 1: (direct encoding) Unicode characters in set D above may be
      encoded directly as their ASCII equivalents. Unicode characters in
      Set O may optionally be encoded directly as their ASCII
      equivalents, bearing in mind that many of these characters are
      illegal in header fields, or may not pass correctly through some
      mail gateways.

      Rule 2: (Unicode shifted encoding) Any Unicode character sequence
      may be encoded using a sequence of characters in set B, when
      preceded by the shift character "+" (US-ASCII character value
      decimal 43). The "+" signals that subsequent octets are to be
      interpreted as elements of the Modified Base64 alphabet until a
      character not in that alphabet is encountered. Such characters
      include control characters such as carriage returns and line
      feeds; thus, a Unicode shifted sequence always terminates at the
      of a line. As a special case, if the sequence terminates with the
      character "-" (US-ASCII decimal 45) then that character is
      absorbed; other terminating characters are not absorbed and are
      processed normally.

      Note that if the first character after the shifted sequence is "-"
      then an extra "-" must be present to terminate the shifted
      sequence so that the actual "-" is not itself absorbed.

      Rationale. A terminating character is necessary for cases where
      the next character after the Modified Base64 sequence is part of
      character set B or is itself the terminating character. It can
      also enhance readability by delimiting encoded sequences.

      Also as a special case, the sequence "+-" may be used to encode
      the character "+". A "+" character followed immediately by any
      character other than members of set B or "-" is an ill-formed
      sequence.

      Unicode is encoded using Modified Base64 by first converting
      Unicode 16-bit quantities to an octet stream (with the most
      significant octet first). Surrogate pairs (UTF-16) are converted
      by treating each half of the pair as a separate 16 bit quantity
      (i.e., no special treatment). Text with an odd number of octets is
      ill-formed. ISO 10646 characters outside the range addressable via
      surrogate pairs cannot be encoded.

      Rationale. ISO/IEC 10646-1:1993(E) specifies that when characters
      the UCS-2 form are serialized as octets, that the most significant
      octet appear first.  This is also in keeping with common network
      practice of choosing a canonical format for transmission.

      Rationale. The policy for code point allocation within ISO 10646
      and Unicode is that the repertoires be kept synchronized. No code
      points will be allocated in ISO 10646 outside the range
      addressable by surrogate pairs.

      Next, the octet stream is encoded by applying the Base64 content
      transfer encoding algorithm as defined in RFC 2045, modified to
      omit the "=" pad character. Instead, when encoding, zero bits are
      added to pad to a Base64 character boundary. When decoding, any
      bits at the end of the Modified Base64 sequence that do not
      constitute a complete 16-bit Unicode character are discarded. If
      such discarded bits are non-zero the sequence is ill-formed.

      Rationale. The pad character "=" is not used when encoding
      Modified Base64 because of the conflict with its use as an escape
      character for the Q content transfer encoding in RFC 2047 header
      fields, as mentioned above.

      Rule 3: The space (decimal 32), tab (decimal 9), carriage return
      (decimal 13), and line feed (decimal 10) characters may be
      directly represented by their ASCII equivalents. However, note
      that MIME content transfer encodings have rules concerning the use
      of such characters. Usage that does not conform to the
      restrictions of RFC 822, for example, would have to be encoded
      using MIME content transfer encodings other than 7bit or 8bit,
      such as quoted-printable, binary, or base64.

   Given this set of rules, Unicode characters which may be encoded via
   rules 1 or 3 take one octet per character, and other Unicode
   characters are encoded on average with 2 2/3 octets per character
   plus one octet to switch into Modified Base64 and an optional octet
   to switch out.

      Example. The Unicode sequence "A<NOT IDENTICAL TO><ALPHA>."
      (hexadecimal 0041,2262,0391,002E) may be encoded as follows:

            A+ImIDkQ.

      Example. The Unicode sequence "Hi Mom -<WHITE SMILING FACE>-!"
      (hexadecimal 0048, 0069, 0020, 004D, 006F, 006D, 0020, 002D, 263A,
       002D, 0021) may be encoded as follows:

            Hi Mom -+Jjo--!

      Example. The Unicode sequence representing the Han characters for
      the Japanese word "nihongo" (hexadecimal 65E5,672C,8A9E) may be
      encoded as follows:

            +ZeVnLIqe-

Use of Character Set UTF-7 Within MIME

   Character set UTF-7 is safe for mail transmission and therefore may
   be used with any content transfer encoding in MIME (except where line
   length and line break restrictions are violated). Specifically, the 7
   bit encoding for bodies and the Q encoding for headers are both
   acceptable. The MIME character set tag is UTF-7. This signifies any
   version of Unicode equal to or greater than 2.0.

      Example. Here is a text portion of a MIME message containing the
      Unicode sequence "Hi Mom <WHITE SMILING FACE>!" (hexadecimal 0048,
      0069, 0020, 004D, 006F, 006D, 0020, 263A, 0021).

      Content-Type: text/plain; charset=UTF-7

      Hi Mom +Jjo-!

      Example. Here is a text portion of a MIME message containing the
      Unicode sequence representing the Han characters for the Japanese
      word "nihongo" (hexadecimal 65E5,672C,8A9E).

      Content-Type: text/plain; charset=UTF-7

      +ZeVnLIqe-

      Example. Here is a text portion of a MIME message containing the
      Unicode sequence "A<NOT IDENTICAL TO><ALPHA>." (hexadecimal
      0041,2262,0391,002E).

      Content-Type: text/plain; charset=utf-7

      A+ImIDkQ.

      Example. Here is a text portion of a MIME message containing the
      Unicode sequence "Item 3 is <POUND SIGN>1."  (hexadecimal 0049,
      0074, 0065, 006D, 0020, 0033, 0020, 0069, 0073, 0020, 00A3, 0031,
      002E).

      Content-Type: text/plain; charset=UTF-7

      Item 3 is +AKM-1.

   Note that to achieve the best interoperability with systems that may
   not support Unicode or MIME, when preparing text for mail
   transmission line breaks should follow Internet conventions. This
   means that lines should be short and terminated with the proper SMTP
   CRLF sequence. Unicode LINE SEPARATOR (hexadecimal 2028) and
   PARAGRAPH SEPARATOR (hexadecimal 2029) should be converted to SMTP
   line breaks. Ideally, this would be handled transparently by a
   Unicode-aware user agent.

   This preparation is not absolutely necessary, since UTF-7 and the
   appropriate MIME content transfer encoding can handle text that does
   not follow Internet conventions, but readability by systems without
   Unicode or MIME will be impaired. See RFC 2045 for a discussion of
   mail interoperability issues.

   Lines should never be broken in the middle of a UTF-7 shifted
   sequence, since such sequences may not cross line breaks. Therefore,
   UTF-7 encoding should take place after line breaking. If a line
   containing a shifted sequence is too long after encoding, a MIME
   content transfer encoding such as Quoted Printable can be used to
   encode the text. Another possibility is to perform line breaking and
   UTF-7 encoding at the same time, so that lines containing shifted
   sequences already conform to length restrictions.

Discussion

   In this section we will motivate the introduction of UTF-7 as opposed
   to the alternative of using the existing transformation formats of
   Unicode (e.g., UTF-8) with MIME's content transfer encodings. Before
   discussing this, it will be useful to list some assumptions about
   character frequency within typical natural language text strings that
   we use to estimate typical storage requirements:

   1. Most Western European languages use roughly 7/8 of their letters
      from US-ASCII and 1/8 from Latin 1 (ISO-8859-1).

   2. Most non-Roman alphabet-based languages (e.g., Greek) use about
      1/6 of their letters from ASCII (since white space is in the 7-bit
      area) and the rest from their alphabets.

   3. East Asian ideographic-based languages (including Japanese) use
      essentially all of their characters from the Han or CJK syllabary
      area.

   4. Non-directly encoded punctuation characters do not occur
      frequently enough to affect the results.

   Notice that current 8 bit standards, such as ISO-8859-x, require use
   of a content transfer encoding. For comparison with the subsequent
   discussion, the costs break down as follows (note that many of these
   figures are approximate since they depend on the exact composition of
   the text):

   8859-x in Base64

      Text type          Average octets/character
      All                      1.33

   8859-x in Quoted Printable

      Text type          Average octets/character
      US-ASCII                 1
      Western European         1.25
      Other                    2.67

   Note also that Unicode encoded in Base64 takes a constant 2.67 octets
   per character. For purposes of comparison, we will look at UTF-8 in
   Base64 and Quoted Printable, and UTF-7. Also note that fixed overhead
   for long strings is relative to 1/n, where n is the encoded string
   length in octets.

   UTF-8 in Base64

      Text type          Average octets/character
      US-ASCII                 1.33
      Western European         1.5
      Some Alphabetics         2.44
      All others               4
   UTF-8 in Quoted Printable

      Text type          Average octets/character
      US-ASCII                 1
      Western European         1.63
      Some Alphabetics         5.17
      All others               7-9

   UTF-7

      Text type          Average octets/character
      Most US-ASCII            1
      Western European         1.5
      All others               2.67+2/n

   We feel that the UTF-8 in Quoted Printable option is not viable due
   to the very large expansion of all text except Western European. This
   would only be viable in texts consisting of large expanses of US-
   ASCII or Latin characters with occasional other characters
   interspersed. We would prefer to introduce one encoding that works
   reasonably well for all users.

   We also feel that UTF-8 in Base64 has high expansion for non-
   Western-European users, and is less desirable because it cannot be
   read directly, even when the content is largely US-ASCII. The base
   encoding of UTF-7 gives competitive results and is readable for ASCII
   text.

   UTF-7 gives results competitive with ISO-8859-x, with access to all
   of the Unicode character set. We believe this justifies the
   introduction of a new transformation format of Unicode.

   As an alternative to use of UTF-7, it might be possible to intermix
   Unicode characters with other character sets using an existing MIME
   mechanism, the multipart/mixed content type, ignoring for the moment
   the issues with line breaks (thanks to Nathaniel Borenstein for
   suggesting this). For instance (repeating an earlier example):

      Content-type: multipart/mixed; boundary=foo
      Content-Disposition: inline

      --foo
      Content-type: text/plain; charset=us-ascii

      Hi Mom
      --foo
      Content-type: text/plain; charset=UNICODE-2-0
      Content-transfer-encoding: base64

      Jjo=
      --foo
      Content-type: text/plain; charset=us-ascii

      !
      --foo--

   Theoretically, this removes the need for UTF-7 in message bodies
   (multipart may not be used in header fields). However, we feel that
   as use of the Unicode character set becomes more widespread,
   intermittent use of specialized Unicode characters (such as dingbats
   and mathematical symbols) will occur, and that text will also
   typically include small snippets from other scripts, such as
   Cyrillic, Greek, or East Asian languages (anything in the Roman
   script is already handled adequately by existing MIME character
   sets). Although the multipart technique works well for large chunks
   of text in alternating character sets, we feel it does not adequately
   support the kinds of uses just discussed, and so we still believe the
   introduction of UTF-7 is justified.

Summary

   The UTF-7 encoding allows Unicode characters to be encoded within the
   US-ASCII 7 bit character set. It is most effective for Unicode
   sequences which contain relatively long strings of US-ASCII
   characters interspersed with either single Unicode characters or
   strings of Unicode characters, as it allows the US-ASCII portions to
   be read on systems without direct Unicode support.

   UTF-7 should only be used with 7 bit transports such as mail. In
   other contexts, use of straight Unicode or UTF-8 is preferred.

Acknowledgements

   Many thanks to the following people for their contributions,
   comments, and suggestions. If we have omitted anyone it was through
   oversight and not intentionally.

         Glenn Adams
         Harald T. Alvestrand
         Nathaniel Borenstein
         Lee Collins
         Jim Conklin
         Dave Crocker
         Steve Dorner
         Dana S. Emery
         Ned Freed
         Kari E. Hurtta
         John H. Jenkins
         John C. Klensin
         Valdis Kletnieks
         Keith Moore
         Masataka Ohta
         Einar Stefferud
         Erik M. van der Poel

Appendix A -- Examples

   Here is a longer example, taken from a document originally in Big5
   code. It has been condensed for brevity. There are two versions: the
   first uses optional characters from set O (and so may not pass
   through some mail gateways), and the second does not.

   Content-type: text/plain; charset=utf-7

   Below is the full Chinese text of the Analects (+itaKng-).

   The sources for the text are:

   "The sayings of Confucius," James R. Ware, trans.  +U/BTFw-:
   +ZYeB9FH6ckh5Pg-, 1980.  (Chinese text with English translation)

   +Vttm+E6UfZM-, +W4tRQ066bOg-, +UxdOrA-:  +Ti1XC2b4Xpc-, 1990.

   "The Chinese Classics with a Translation, Critical and Exegetical
   Notes, Prolegomena, and Copius Indexes," James Legge, trans., Taipei:
   Southern Materials Center Publishing, Inc., 1991.  (Chinese text with
   English translation)

   Big Five and GB versions of the text are being made available
   separately.

   Neither the Big Five nor GB contain all the characters used in this
   text.  Missing characters have been indicated using their Unicode/ISO
   10646 code points.  "U+-" followed by four hexadecimal digits
   indicates a Unicode/10646 code (e.g., U+-9F08).  There is no good
   solution to the problem of the small size of the Big Five/GB
   character sets; this represents the solution I find personally most
   satisfactory.

   (omitted...)

   I have tried to minimize this problem by using variant characters
   where they were available and the character actually in the text was
   not.  Only variants listed as such in the +XrdxmVtXUXg- were used.

   (omitted...)

   John H. Jenkins +TpVPXGBG- jenkins@apple.com 5 January 1993
   (omitted...)

   Content-type: text/plain; charset=utf-7

   Below is the full Chinese text of the Analects (+itaKng-).

   The sources for the text are:

   +ACI-The sayings of Confucius,+ACI- James R. Ware, trans.  +U/BTFw-:
   +ZYeB9FH6ckh5Pg-, 1980.  (Chinese text with English translation)

   +Vttm+E6UfZM-, +W4tRQ066bOg-, +UxdOrA-:  +Ti1XC2b4Xpc-, 1990.

   +ACI-The Chinese Classics with a Translation, Critical and Exegetical
   Notes, Prolegomena, and Copius Indexes,+ACI- James Legge, trans.,
   Taipei:  Southern Materials Center Publishing, Inc., 1991.  (Chinese
   text with English translation)

   Big Five and GB versions of the text are being made available
   separately.

   Neither the Big Five nor GB contain all the characters used in this
   text.  Missing characters have been indicated using their Unicode/ISO
   10646 code points.  +ACI-U+-+ACI- followed by four hexadecimal digits
   indicates a Unicode/10646 code (e.g., U+-9F08).  There is no good
   solution to the problem of the small size of the Big Five/GB
   character sets+ADs- this represents the solution I find personally
   most satisfactory.

   (omitted...)

   I have tried to minimize this problem by using variant characters
   where they were available and the character actually in the text was
   not.  Only variants listed as such in the +XrdxmVtXUXg- were used.
   (omitted...)

   John H. Jenkins +TpVPXGBG- jenkins+AEA-apple.com 5 January 1993
   (omitted...)

Security Considerations

   Security issues are not discussed in this memo.

References

[UNICODE 2.0]  "The Unicode Standard, Version 2.0", The Unicode
               Consortium, Addison-Wesley, 1996. ISBN 0-201-48345-9.

[ISO 10646]    ISO/IEC 10646-1:1993(E) Information Technology--Universal
               Multiple-octet Coded Character Set (UCS). See also
               amendments 1 through 7, plus editorial corrections.

[RFC-1641]     Goldsmith, D., and M. Davis, "Using Unicode with MIME",
               RFC 1641, Taligent, Inc., July 1994.

[US-ASCII]     Coded Character Set--7-bit American Standard Code for
               Information Interchange, ANSI X3.4-1986.

[ISO-8859]     Information Processing -- 8-bit Single-Byte Coded Graphic
               Character Sets -- Part 1: Latin Alphabet No. 1, ISO
               8859-1:1987.  Part 2: Latin alphabet No.  2, ISO 8859-2,
               1987.  Part 3: Latin alphabet No. 3, ISO 8859-3, 1988.
               Part 4: Latin alphabet No.  4, ISO 8859-4, 1988.  Part 5:
               Latin/Cyrillic alphabet, ISO 8859-5, 1988.  Part 6:
               Latin/Arabic alphabet, ISO 8859-6, 1987.  Part 7:
               Latin/Greek alphabet, ISO 8859-7, 1987.  Part 8:
               Latin/Hebrew alphabet, ISO 8859-8, 1988.  Part 9: Latin
               alphabet No. 5, ISO 8859-9, 1990.

[RFC822]       Crocker, D., "Standard for the Format of ARPA Internet
               Text Messages", STD 11, RFC 822, UDEL, August 1982.

[MIME]         Borenstein N., N. Freed, K. Moore, J. Klensin, and J.
               Postel, "MIME (Multipurpose Internet Mail Extensions)
               Parts One through Five", RFC 2045, 2046, 2047, 2048, and
               2049, November 1996.

Authors' Addresses

   David Goldsmith
   Apple Computer, Inc.
   2 Infinite Loop, MS: 302-2IS
   Cupertino, CA 95014

   Phone: 408-974-1957
   Fax: 408-862-4566
   EMail: goldsmith@apple.com
   Mark Davis
   Taligent, Inc.
   10201 N. DeAnza Blvd.
   Cupertino, CA 95014-2233

   Phone: 408-777-5116
   Fax: 408-777-5081
   EMail: mark_davis@taligent.com

ISO Plain Language

On this page
PROBLEMInternet mail carries only 7-bit US-ASCII, but Unicode covers most of the world's writing systems. UTF-8 sends non-ASCII characters as octets in the range decimal 128 through 255, which mail must then encode a second time, bloating non-English text.
MECHANISMUTF-7 writes plain ASCII characters as themselves and wraps everything else in a shifted run: a plus sign, the 16-bit Unicode values in a modified Base64, and an optional minus sign to end the run.
THE CATCHThe result is mail-safe, readable where the text is mostly ASCII and compact for other scripts. The RFC says UTF-7 should be used only on 7-bit transports such as mail, and that straight Unicode or UTF-8 is preferred elsewhere.

§1Why UTF-7 Exists

Unicode, as defined by The Unicode Standard version 2.0 and ISO/IEC 10646-1:1993(E), defines a character set that encompasses most of the world's writing systems. Internet mail, however, supports only 7-bit US ASCII as a character set. MIME extends mail to support different media types and character sets and could therefore carry Unicode, but MIME neither defines Unicode as a permitted character set nor specifies how it would be encoded.

The problem is that other transformation formats of Unicode, most notably UTF-8, use octets in the range decimal 128 through 255 for characters outside the US-ASCII range. In mail, those octets must themselves be encoded, so the text goes through two successive encoding processes and expands significantly. That expansion puts non-English speakers at a disadvantage: using UTF-8 together with the Quoted-Printable content transfer encoding of MIME represents US-ASCII characters in one octet, but other characters may require up to nine octets.

UTF-7 encodes Unicode characters as US-ASCII octets together with shift sequences for characters outside that range, and one character in the US-ASCII repertoire is reserved as the shift character. Many mail gateways and systems cannot handle the entire US-ASCII character set, so UTF-7 also contains provisions for encoding characters within US-ASCII in a way that all mail systems can accommodate. The memo says UTF-7 should normally be used only in the context of 7-bit transports such as mail, and that in other contexts straight Unicode or UTF-8 is preferred.

Old mail systems only understand plain ASCII text. To send anything else you have to hide it inside characters they do understand. UTF-7 is one way of doing that, and it keeps most of the message readable to a human or an old program.

§2The Encoding Rules

Unicode here is the 16-bit character set of The Unicode Standard version 2.0. The memo defines three subsets of US-ASCII. Set D, the directly encoded characters, holds the upper and lower case letters, the ten digits, and nine special characters: the apostrophe, the open and close parenthesis, the comma, the hyphen, the period, the slash, the colon and the question mark. Set O, the optional direct characters, holds twenty further characters, including the exclamation mark, the number sign, the dollar sign, the ampersand and the brackets, which may be written directly or shifted; the backslash and the tilde are left out because they are often redefined in variants of ASCII. Set B, Modified Base 64, is the Base64 alphabet of RFC 2045 without the pad character, which is omitted because the only readable encoding for header fields in RFC 2047 is Q, and that encoding already uses the character.

Rule 1 is direct encoding: characters in set D may be encoded as their ASCII equivalents, and characters in set O may optionally be encoded that way, bearing in mind that many of them are illegal in header fields or may not pass correctly through some mail gateways.

Rule 2 is the Unicode shifted encoding. Any sequence of Unicode characters may be encoded using characters of set B when preceded by the shift character, the plus sign, whose ASCII value is decimal 43. The plus sign means that the octets that follow are to be read as elements of the Modified Base64 alphabet until a character outside that alphabet is encountered. Because control characters such as carriage returns and line feeds are not in the alphabet, a shifted sequence always terminates at the end of a line. If the sequence ends with a hyphen, that character is absorbed; other terminating characters are not absorbed and are processed normally. The special sequence +- encodes a literal plus sign, and a plus sign followed immediately by any character other than a member of set B or a hyphen is an ill-formed sequence.

To build the shifted part, each 16-bit Unicode value is first converted to an octet stream with the most significant octet first, and surrogate pairs are treated as two separate 16-bit quantities with no special treatment. Text with an odd number of octets is ill-formed. The octet stream is then encoded with the Base64 algorithm of RFC 2045, modified to leave out the pad character and to add zero bits instead, and when decoding, trailing bits that do not form a complete 16-bit character are discarded, and the sequence is ill-formed if those bits are not zero. Rule 3 allows the space, tab, carriage return and line feed to be written directly, subject to the rules of the MIME content transfer encoding in use.

Ordinary letters and digits are left alone. Everything else goes inside a run that starts with a plus sign and holds the character values written in Base64 without its padding. A minus sign closes the run, and two special cases avoid ambiguity: the pair +- means a literal plus sign, and a run that ends with a real hyphen needs a second hyphen to close it.

THE THREE SETS OF CHARACTERS UTF-7 WORKS WITH
  • Set D, directly encodedThe letters, the ten digits, and nine special characters: apostrophe, parentheses, comma, hyphen, period, slash, colon and question mark.
  • Set O, optional direct charactersTwenty further characters, including the exclamation mark, number sign, dollar sign, ampersand and brackets, which may be written directly or shifted.
  • Set B, Modified Base 64The Base64 alphabet of RFC 2045 without the pad character, used to hold the shifted values.

§3Worked Examples and Use in Mail

The memo gives worked examples. The Unicode sequence A, NOT IDENTICAL TO, ALPHA and a full stop, with hexadecimal values 0041, 2262, 0391 and 002E, is encoded as A+ImIDkQ.. The sequence Hi Mom followed by a hyphen, a white smiling face, another hyphen and an exclamation mark, with hexadecimal values 0048, 0069, 0020, 004D, 006F, 006D, 0020, 002D, 263A, 002D and 0021, is encoded as Hi Mom -+Jjo--!. The three Han characters for the Japanese word nihongo, with hexadecimal values 65E5, 672C and 8A9E, are encoded as +ZeVnLIqe-. The text Item 3 is, a pound sign and 1., is encoded as Item 3 is +AKM-1.

The character set UTF-7 is safe for mail transmission and may therefore be used with any content transfer encoding in MIME, except where line length and line break restrictions are violated. Specifically, the 7-bit encoding for bodies and the Q encoding for headers are both acceptable, and the MIME character set tag is UTF-7, which signifies any version of Unicode equal to or greater than 2.0. A message carrying the white smiling face is sent with the header Content-Type: text/plain; charset=UTF-7 and the body Hi Mom +Jjo-!.

For the best interoperability with systems that support neither Unicode nor MIME, line breaks should follow Internet conventions, so lines should be short and terminated with the SMTP CRLF sequence, and the Unicode LINE SEPARATOR (hexadecimal 2028) and PARAGRAPH SEPARATOR (hexadecimal 2029) should be converted to SMTP line breaks. Lines should never be broken in the middle of a UTF-7 shifted sequence, since such sequences may not cross line breaks, so UTF-7 encoding should take place after line breaking. If a line containing a shifted sequence is too long after encoding, a content transfer encoding such as Quoted Printable can be used on the text, or line breaking and UTF-7 encoding can be done at the same time.

Most of the message stays as plain text. Only the characters that cannot be written directly are replaced by a short run of safe letters and digits, and a reader that knows UTF-7 turns them back into the original characters.

THE RFC'S EXAMPLES: UNICODE VALUES AND THEIR UTF-7 FORM
Three examples
Unicode 0041 2262 0391 002E   ->   A+ImIDkQ.
Unicode 65E5 672C 8A9E        ->   +ZeVnLIqe-
Unicode 0049 0069 00A3 0031 002E ->   Item 3 is +AKM-1.

§4What the Encoding Costs

The memo compares what different encodings cost, using assumptions about how often characters from the ASCII range appear in ordinary text. Most Western European languages use roughly seven eighths of their letters from US-ASCII and one eighth from Latin 1, while most non-Roman alphabets such as Greek use about one sixth of their letters from ASCII, the rest coming from their own alphabets, and East Asian languages use essentially all their characters from the Han or CJK area.

For ISO-8859-x in Base64 the average is 1.33 octets per character for all text, and for ISO-8859-x in Quoted Printable it is 1 for US-ASCII, 1.25 for Western European and 2.67 for other text. Unicode encoded in Base64 takes a constant 2.67 octets per character. UTF-8 in Base64 costs 1.33 octets per character for US-ASCII, 1.5 for Western European, 2.44 for some alphabetics and 4 for all others. UTF-8 in Quoted Printable costs 1 for US-ASCII, 1.63 for Western European, 5.17 for some alphabetics and 7 to 9 for all others. UTF-7 costs 1 for most US-ASCII, 1.5 for Western European and 2.67 plus two divided by the length for all others, where the extra covers switching into and out of the shifted form.

The authors judge that UTF-8 in Quoted Printable is not viable, because of the very large expansion of all text except Western European, and that it would only work for texts made of large expanses of US-ASCII or Latin characters with occasional other characters interspersed. UTF-8 in Base64 also has high expansion for non-Western-European users and cannot be read directly even when the content is largely US-ASCII. They prefer one encoding that works reasonably well for all users, and conclude that UTF-7 gives results competitive with ISO-8859-x while giving access to all of the Unicode character set.

As an alternative to UTF-7, the authors consider mixing Unicode parts with other character sets using the existing MIME multipart/mixed content type. Theoretically this removes the need for UTF-7 in message bodies, since multipart may not be used in header fields, but they feel it does not adequately support the intermittent use of specialised characters and short snippets from other scripts that they expect to become common.

The point of the comparison is fairness. A method that is cheap for English but makes other scripts much bigger is a poor choice for a global network, and UTF-7 was designed to avoid that.

AVERAGE OCTETS PER CHARACTER, AS GIVEN IN THE RFC
Encoding and textAverage octets per character
UTF-8 in Quoted Printable, US-ASCII1
UTF-8 in Quoted Printable, Western European1.63
UTF-8 in Quoted Printable, some alphabetics5.17
UTF-8 in Quoted Printable, all others7-9
UTF-7, most US-ASCII THIS RFC1
UTF-7, Western European THIS RFC1.5
UTF-7, all others THIS RFC2.67+2/n

§5When to Use UTF-7

The UTF-7 encoding allows Unicode characters to be encoded within the US-ASCII 7-bit character set. The memo says it is most effective for Unicode sequences that contain relatively long strings of US-ASCII characters interspersed with either single Unicode characters or strings of Unicode characters, because the US-ASCII portions can be read on systems without direct Unicode support.

UTF-7 should only be used with 7-bit transports such as mail. In other contexts, the use of straight Unicode or UTF-8 is preferred. The memo is Informational: it does not specify an Internet standard of any kind, and it replaces the earlier RFC 1642.

Use UTF-7 when the text has to cross a system that only accepts plain ASCII. Everywhere else, the usual encodings are simpler and better supported.

§6References