7 Network Working Group F. Yergeau
8 Request for Comments: 2279 Alis Technologies
9 Obsoletes: 2044 January 1998
10 Category: Standards Track
13 UTF-8, a transformation format of ISO 10646
17 This document specifies an Internet standards track protocol for the
18 Internet community, and requests discussion and suggestions for
19 improvements. Please refer to the current edition of the "Internet
20 Official Protocol Standards" (STD 1) for the standardization state
21 and status of this protocol. Distribution of this memo is unlimited.
25 Copyright (C) The Internet Society (1998). All Rights Reserved.
29 ISO/IEC 10646-1 defines a multi-octet character set called the
30 Universal Character Set (UCS) which encompasses most of the world's
31 writing systems. Multi-octet characters, however, are not compatible
32 with many current applications and protocols, and this has led to the
33 development of a few so-called UCS transformation formats (UTF), each
34 with different characteristics. UTF-8, the object of this memo, has
35 the characteristic of preserving the full US-ASCII range, providing
36 compatibility with file systems, parsers and other software that rely
37 on US-ASCII values but are transparent to other values. This memo
38 updates and replaces RFC 2044, in particular addressing the question
39 of versions of the relevant standards.
43 ISO/IEC 10646-1 [ISO-10646] defines a multi-octet character set
44 called the Universal Character Set (UCS), which encompasses most of
45 the world's writing systems. Two multi-octet encodings are defined,
46 a four-octet per character encoding called UCS-4 and a two-octet per
47 character encoding called UCS-2, able to address only the first 64K
48 characters of the UCS (the Basic Multilingual Plane, BMP), outside of
49 which there are currently no assignments.
51 It is noteworthy that the same set of characters is defined by the
52 Unicode standard [UNICODE], which further defines additional
53 character properties and other application details of great interest
54 to implementors, but does not have the UCS-4 encoding. Up to the
58 Yergeau Standards Track [Page 1]
60 RFC 2279 UTF-8 January 1998
63 present time, changes in Unicode and amendments to ISO/IEC 10646 have
64 tracked each other, so that the character repertoires and code point
65 assignments have remained in sync. The relevant standardization
66 committees have committed to maintain this very useful synchronism.
68 The UCS-2 and UCS-4 encodings, however, are hard to use in many
69 current applications and protocols that assume 8 or even 7 bit
70 characters. Even newer systems able to deal with 16 bit characters
71 cannot process UCS-4 data. This situation has led to the development
72 of so-called UCS transformation formats (UTF), each with different
75 UTF-1 has only historical interest, having been removed from ISO/IEC
76 10646. UTF-7 has the quality of encoding the full BMP repertoire
77 using only octets with the high-order bit clear (7 bit US-ASCII
78 values, [US-ASCII]), and is thus deemed a mail-safe encoding
79 ([RFC2152]). UTF-8, the object of this memo, uses all bits of an
80 octet, but has the quality of preserving the full US-ASCII range:
81 US-ASCII characters are encoded in one octet having the normal US-
82 ASCII value, and any octet with such a value can only stand for an
83 US-ASCII character, and nothing else.
85 UTF-16 is a scheme for transforming a subset of the UCS-4 repertoire
86 into pairs of UCS-2 values from a reserved range. UTF-16 impacts
87 UTF-8 in that UCS-2 values from the reserved range must be treated
88 specially in the UTF-8 transformation.
90 UTF-8 encodes UCS-2 or UCS-4 characters as a varying number of
91 octets, where the number of octets, and the value of each, depend on
92 the integer value assigned to the character in ISO/IEC 10646. This
93 transformation format has the following characteristics (all values
96 - Character values from 0000 0000 to 0000 007F (US-ASCII repertoire)
97 correspond to octets 00 to 7F (7 bit US-ASCII values). A direct
98 consequence is that a plain ASCII string is also a valid UTF-8
101 - US-ASCII values do not appear otherwise in a UTF-8 encoded
102 character stream. This provides compatibility with file systems
103 or other software (e.g. the printf() function in C libraries) that
104 parse based on US-ASCII values but are transparent to other
107 - Round-trip conversion is easy between UTF-8 and either of UCS-4,
114 Yergeau Standards Track [Page 2]
116 RFC 2279 UTF-8 January 1998
119 - The first octet of a multi-octet sequence indicates the number of
120 octets in the sequence.
122 - The octet values FE and FF never appear.
124 - Character boundaries are easily found from anywhere in an octet
127 - The lexicographic sorting order of UCS-4 strings is preserved. Of
128 course this is of limited interest since the sort order is not
129 culturally valid in either case.
131 - The Boyer-Moore fast search algorithm can be used with UTF-8 data.
133 - UTF-8 strings can be fairly reliably recognized as such by a
134 simple algorithm, i.e. the probability that a string of characters
135 in any other encoding appears as valid UTF-8 is low, diminishing
136 with increasing string length.
138 UTF-8 was originally a project of the X/Open Joint
139 Internationalization Group XOJIG with the objective to specify a File
140 System Safe UCS Transformation Format [FSS-UTF] that is compatible
141 with UNIX systems, supporting multilingual text in a single encoding.
142 The original authors were Gary Miller, Greger Leijonhufvud and John
143 Entenmann. Later, Ken Thompson and Rob Pike did significant work for
146 A description can also be found in Unicode Technical Report #4 and in
147 the Unicode Standard, version 2.0 [UNICODE]. The definitive
148 reference, including provisions for UTF-16 data within UTF-8, is
149 Annex R of ISO/IEC 10646-1 [ISO-10646].
153 In UTF-8, characters are encoded using sequences of 1 to 6 octets.
154 The only octet of a "sequence" of one has the higher-order bit set to
155 0, the remaining 7 bits being used to encode the character value. In
156 a sequence of n octets, n>1, the initial octet has the n higher-order
157 bits set to 1, followed by a bit set to 0. The remaining bit(s) of
158 that octet contain bits from the value of the character to be
159 encoded. The following octet(s) all have the higher-order bit set to
160 1 and the following bit set to 0, leaving 6 bits in each to contain
161 bits from the character to be encoded.
163 The table below summarizes the format of these different octet types.
164 The letter x indicates bits available for encoding bits of the UCS-4
170 Yergeau Standards Track [Page 3]
172 RFC 2279 UTF-8 January 1998
175 UCS-4 range (hex.) UTF-8 octet sequence (binary)
176 0000 0000-0000 007F 0xxxxxxx
177 0000 0080-0000 07FF 110xxxxx 10xxxxxx
178 0000 0800-0000 FFFF 1110xxxx 10xxxxxx 10xxxxxx
180 0001 0000-001F FFFF 11110xxx 10xxxxxx 10xxxxxx 10xxxxxx
181 0020 0000-03FF FFFF 111110xx 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx
182 0400 0000-7FFF FFFF 1111110x 10xxxxxx ... 10xxxxxx
184 Encoding from UCS-4 to UTF-8 proceeds as follows:
186 1) Determine the number of octets required from the character value
187 and the first column of the table above. It is important to note
188 that the rows of the table are mutually exclusive, i.e. there is
189 only one valid way to encode a given UCS-4 character.
191 2) Prepare the high-order bits of the octets as per the second column
194 3) Fill in the bits marked x from the bits of the character value,
195 starting from the lower-order bits of the character value and
196 putting them first in the last octet of the sequence, then the
197 next to last, etc. until all x bits are filled in.
199 The algorithm for encoding UCS-2 (or Unicode) to UTF-8 can be
200 obtained from the above, in principle, by simply extending each
201 UCS-2 character with two zero-valued octets. However, pairs of
202 UCS-2 values between D800 and DFFF (surrogate pairs in Unicode
203 parlance), being actually UCS-4 characters transformed through
204 UTF-16, need special treatment: the UTF-16 transformation must be
205 undone, yielding a UCS-4 character that is then transformed as
208 Decoding from UTF-8 to UCS-4 proceeds as follows:
210 1) Initialize the 4 octets of the UCS-4 character with all bits set
213 2) Determine which bits encode the character value from the number of
214 octets in the sequence and the second column of the table above
217 3) Distribute the bits from the sequence to the UCS-4 character,
218 first the lower-order bits from the last octet of the sequence and
219 proceeding to the left until no x bits are left.
221 If the UTF-8 sequence is no more than three octets long, decoding
222 can proceed directly to UCS-2.
226 Yergeau Standards Track [Page 4]
228 RFC 2279 UTF-8 January 1998
231 NOTE -- actual implementations of the decoding algorithm above
232 should protect against decoding invalid sequences. For
233 instance, a naive implementation may (wrongly) decode the
234 invalid UTF-8 sequence C0 80 into the character U+0000, which
235 may have security consequences and/or cause other problems. See
236 the Security Considerations section below.
238 A more detailed algorithm and formulae can be found in [FSS_UTF],
239 [UNICODE] or Annex R to [ISO-10646].
241 3. Versions of the standards
243 ISO/IEC 10646 is updated from time to time by published amendments;
244 similarly, different versions of the Unicode standard exist: 1.0, 1.1
245 and 2.0 as of this writing. Each new version obsoletes and replaces
246 the previous one, but implementations, and more significantly data,
247 are not updated instantly.
249 In general, the changes amount to adding new characters, which does
250 not pose particular problems with old data. Amendment 5 to ISO/IEC
251 10646, however, has moved and expanded the Korean Hangul block,
252 thereby making any previous data containing Hangul characters invalid
253 under the new version. Unicode 2.0 has the same difference from
254 Unicode 1.1. The official justification for allowing such an
255 incompatible change was that no implementations and no data
256 containing Hangul existed, a statement that is likely to be true but
257 remains unprovable. The incident has been dubbed the "Korean mess",
258 and the relevant committees have pledged to never, ever again make
259 such an incompatible change.
261 New versions, and in particular any incompatible changes, have q
262 conseuences regarding MIME character encoding labels, to be discussed
267 The UCS-2 sequence "A<NOT IDENTICAL TO><ALPHA>." (0041, 2262, 0391,
268 002E) may be encoded in UTF-8 as follows:
272 The UCS-2 sequence representing the Hangul characters for the Korean
273 word "hangugo" (D55C, AD6D, C5B4) may be encoded as follows:
275 ED 95 9C EA B5 AD EC 96 B4
282 Yergeau Standards Track [Page 5]
284 RFC 2279 UTF-8 January 1998
287 The UCS-2 sequence representing the Han characters for the Japanese
288 word "nihongo" (65E5, 672C, 8A9E) may be encoded as follows:
290 E6 97 A5 E6 9C AC E8 AA 9E
294 This memo is meant to serve as the basis for registration of a MIME
295 character set parameter (charset) [CHARSET-REG]. The proposed
296 charset parameter value is "UTF-8". This string labels media types
297 containing text consisting of characters from the repertoire of
298 ISO/IEC 10646 including all amendments at least up to amendment 5
299 (Korean block), encoded to a sequence of octets using the encoding
300 scheme outlined above. UTF-8 is suitable for use in MIME content
301 types under the "text" top-level type.
303 It is noteworthy that the label "UTF-8" does not contain a version
304 identification, referring generically to ISO/IEC 10646. This is
305 intentional, the rationale being as follows:
307 A MIME charset label is designed to give just the information needed
308 to interpret a sequence of bytes received on the wire into a sequence
309 of characters, nothing more (see RFC 2045, section 2.2, in [MIME]).
310 As long as a character set standard does not change incompatibly,
311 version numbers serve no purpose, because one gains nothing by
312 learning from the tag that newly assigned characters may be received
313 that one doesn't know about. The tag itself doesn't teach anything
314 about the new characters, which are going to be received anyway.
316 Hence, as long as the standards evolve compatibly, the apparent
317 advantage of having labels that identify the versions is only that,
318 apparent. But there is a disadvantage to such version-dependent
319 labels: when an older application receives data accompanied by a
320 newer, unknown label, it may fail to recognize the label and be
321 completely unable to deal with the data, whereas a generic, known
322 label would have triggered mostly correct processing of the data,
323 which may well not contain any new characters.
325 Now the "Korean mess" (ISO/IEC 10646 amendment 5) is an incompatible
326 change, in principle contradicting the appropriateness of a version
327 independent MIME charset label as described above. But the
328 compatibility problem can only appear with data containing Korean
329 Hangul characters encoded according to Unicode 1.1 (or equivalently
330 ISO/IEC 10646 before amendment 5), and there is arguably no such data
331 to worry about, this being the very reason the incompatible change
332 was deemed acceptable.
338 Yergeau Standards Track [Page 6]
340 RFC 2279 UTF-8 January 1998
343 In practice, then, a version-independent label is warranted, provided
344 the label is understood to refer to all versions after Amendment 5,
345 and provided no incompatible change actually occurs. Should
346 incompatible changes occur in a later version of ISO/IEC 10646, the
347 MIME charset label defined here will stay aligned with the previous
348 version until and unless the IETF specifically decides otherwise.
350 It is also proposed to register the charset parameter value
351 "UNICODE-1-1-UTF-8", for the exclusive purpose of labelling text data
352 containing Hangul syllables encoded to UTF-8 without taking into
353 account Amendment 5 of ISO/IEC 10646 (i.e. using the pre-amendment 5
354 code point assignments). Any other UTF-8 data SHOULD NOT use this
355 label, in particular data not containing any Hangul syllables, and it
356 is felt important to strongly recommend against creating any new
357 Hangul-containing data without taking Amendment 5 of ISO/IEC 10646
360 6. Security Considerations
362 Implementors of UTF-8 need to consider the security aspects of how
363 they handle illegal UTF-8 sequences. It is conceivable that in some
364 circumstances an attacker would be able to exploit an incautious
365 UTF-8 parser by sending it an octet sequence that is not permitted by
368 A particularly subtle form of this attack could be carried out
369 against a parser which performs security-critical validity checks
370 against the UTF-8 encoded form of its input, but interprets certain
371 illegal octet sequences as characters. For example, a parser might
372 prohibit the NUL character when encoded as the single-octet sequence
373 00, but allow the illegal two-octet sequence C0 80 and interpret it
374 as a NUL character. Another example might be a parser which
375 prohibits the octet sequence 2F 2E 2E 2F ("/../"), yet permits the
376 illegal octet sequence 2F C0 AE 2E 2F.
380 The following have participated in the drafting and discussion of
383 James E. Agenbroad Andries Brouwer
384 Martin J. D|rst Ned Freed
385 David Goldsmith Edwin F. Hart
386 Kent Karlsson Markus Kuhn
387 Michael Kung Alain LaBonte
388 John Gardiner Myers Murray Sargent
389 Keld Simonsen Arnold Winkler
394 Yergeau Standards Track [Page 7]
396 RFC 2279 UTF-8 January 1998
401 [CHARSET-REG] Freed, N., and J. Postel, "IANA Charset Registration
402 Procedures", BCP 19, RFC 2278, January 1998.
404 [FSS_UTF] X/Open CAE Specification C501 ISBN 1-85912-082-2 28cm.
405 22p. pbk. 172g. 4/95, X/Open Company Ltd., "File
406 System Safe UCS Transformation Format (FSS_UTF)",
407 X/Open Preleminary Specification, Document Number
408 P316. Also published in Unicode Technical Report #4.
410 [ISO-10646] ISO/IEC 10646-1:1993. International Standard --
411 Information technology -- Universal Multiple-Octet
412 Coded Character Set (UCS) -- Part 1: Architecture and
413 Basic Multilingual Plane. Five amendments and a
414 technical corrigendum have been published up to now.
415 UTF-8 is described in Annex R, published as Amendment
416 2. UTF-16 is described in Annex Q, published as
417 Amendment 1. 17 other amendments are currently at
418 various stages of standardization.
420 [MIME] Freed, N., and N. Borenstein, "Multipurpose Internet
421 Mail Extensions (MIME) Part One: Format of Internet
422 Message Bodies", RFC 2045. N. Freed, N. Borenstein,
423 "Multipurpose Internet Mail Extensions (MIME) Part
424 Two: Media Types", RFC 2046. K. Moore, "MIME
425 (Multipurpose Internet Mail Extensions) Part Three:
426 Message Header Extensions for Non-ASCII Text", RFC
427 2047. N. Freed, J. Klensin, J. Postel, "Multipurpose
428 Internet Mail Extensions (MIME) Part Four:
429 Registration Procedures", RFC 2048. N. Freed, N.
430 Borenstein, " Multipurpose Internet Mail Extensions
431 (MIME) Part Five: Conformance Criteria and Examples",
432 RFC 2049. All November 1996.
434 [RFC2152] Goldsmith, D., and M. Davis, "UTF-7: A Mail-safe
435 Transformation Format of Unicode", RFC 1642, Taligent
436 inc., May 1997. (Obsoletes RFC1642)
438 [UNICODE] The Unicode Consortium, "The Unicode Standard --
439 Version 2.0", Addison-Wesley, 1996.
441 [US-ASCII] Coded Character Set--7-bit American Standard Code for
442 Information Interchange, ANSI X3.4-1986.
450 Yergeau Standards Track [Page 8]
452 RFC 2279 UTF-8 January 1998
459 100, boul. Alexis-Nihon
464 Phone: +1 (514) 747-2547
465 Fax: +1 (514) 747-2561
466 EMail: fyergeau@alis.com
506 Yergeau Standards Track [Page 9]
508 RFC 2279 UTF-8 January 1998
511 Full Copyright Statement
513 Copyright (C) The Internet Society (1998). All Rights Reserved.
515 This document and translations of it may be copied and furnished to
516 others, and derivative works that comment on or otherwise explain it
517 or assist in its implementation may be prepared, copied, published
518 and distributed, in whole or in part, without restriction of any
519 kind, provided that the above copyright notice and this paragraph are
520 included on all such copies and derivative works. However, this
521 document itself may not be modified in any way, such as by removing
522 the copyright notice or references to the Internet Society or other
523 Internet organizations, except as needed for the purpose of
524 developing Internet standards in which case the procedures for
525 copyrights defined in the Internet Standards process must be
526 followed, or as required to translate it into languages other than
529 The limited permissions granted above are perpetual and will not be
530 revoked by the Internet Society or its successors or assigns.
532 This document and the information contained herein is provided on an
533 "AS IS" basis and THE INTERNET SOCIETY AND THE INTERNET ENGINEERING
534 TASK FORCE DISCLAIMS ALL WARRANTIES, EXPRESS OR IMPLIED, INCLUDING
535 BUT NOT LIMITED TO ANY WARRANTY THAT THE USE OF THE INFORMATION
536 HEREIN WILL NOT INFRINGE ANY RIGHTS OR ANY IMPLIED WARRANTIES OF
537 MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
562 Yergeau Standards Track [Page 10]