Blame man7/utf-8.7

Packit 7cfc04
.\" Copyright (C) Markus Kuhn, 1996, 2001
Packit 7cfc04
.\"
Packit 7cfc04
.\" %%%LICENSE_START(GPLv2+_DOC_FULL)
Packit 7cfc04
.\" This is free documentation; you can redistribute it and/or
Packit 7cfc04
.\" modify it under the terms of the GNU General Public License as
Packit 7cfc04
.\" published by the Free Software Foundation; either version 2 of
Packit 7cfc04
.\" the License, or (at your option) any later version.
Packit 7cfc04
.\"
Packit 7cfc04
.\" The GNU General Public License's references to "object code"
Packit 7cfc04
.\" and "executables" are to be interpreted as the output of any
Packit 7cfc04
.\" document formatting or typesetting system, including
Packit 7cfc04
.\" intermediate and printed output.
Packit 7cfc04
.\"
Packit 7cfc04
.\" This manual is distributed in the hope that it will be useful,
Packit 7cfc04
.\" but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit 7cfc04
.\" MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
Packit 7cfc04
.\" GNU General Public License for more details.
Packit 7cfc04
.\"
Packit 7cfc04
.\" You should have received a copy of the GNU General Public
Packit 7cfc04
.\" License along with this manual; if not, see
Packit 7cfc04
.\" <http://www.gnu.org/licenses/>.
Packit 7cfc04
.\" %%%LICENSE_END
Packit 7cfc04
.\"
Packit 7cfc04
.\" 1995-11-26  Markus Kuhn <mskuhn@cip.informatik.uni-erlangen.de>
Packit 7cfc04
.\"      First version written
Packit 7cfc04
.\" 2001-05-11  Markus Kuhn <mgk25@cl.cam.ac.uk>
Packit 7cfc04
.\"      Update
Packit 7cfc04
.\"
Packit 7cfc04
.TH UTF-8 7 2016-07-17 "GNU" "Linux Programmer's Manual"
Packit 7cfc04
.SH NAME
Packit 7cfc04
UTF-8 \- an ASCII compatible multibyte Unicode encoding
Packit 7cfc04
.SH DESCRIPTION
Packit 7cfc04
The Unicode 3.0 character set occupies a 16-bit code space.
Packit 7cfc04
The most obvious
Packit 7cfc04
Unicode encoding (known as UCS-2)
Packit 7cfc04
consists of a sequence of 16-bit words.
Packit 7cfc04
Such strings can contain\(emas part of many 16-bit characters\(embytes
Packit 7cfc04
such as \(aq\\0\(aq or \(aq/\(aq, which have a
Packit 7cfc04
special meaning in filenames and other C library function arguments.
Packit 7cfc04
In addition, the majority of UNIX tools expect ASCII files and can't
Packit 7cfc04
read 16-bit words as characters without major modifications.
Packit 7cfc04
For these reasons,
Packit 7cfc04
UCS-2 is not a suitable external encoding of Unicode
Packit 7cfc04
in filenames, text files, environment variables, and so on.
Packit 7cfc04
The ISO 10646 Universal Character Set (UCS),
Packit 7cfc04
a superset of Unicode, occupies an even larger code
Packit 7cfc04
space\(em31\ bits\(emand the obvious
Packit 7cfc04
UCS-4 encoding for it (a sequence of 32-bit words) has the same problems.
Packit 7cfc04
.PP
Packit 7cfc04
The UTF-8 encoding of Unicode and UCS
Packit 7cfc04
does not have these problems and is the common way in which
Packit 7cfc04
Unicode is used on UNIX-style operating systems.
Packit 7cfc04
.SS Properties
Packit 7cfc04
The UTF-8 encoding has the following nice properties:
Packit 7cfc04
.TP 0.2i
Packit 7cfc04
*
Packit 7cfc04
UCS
Packit 7cfc04
characters 0x00000000 to 0x0000007f (the classic US-ASCII
Packit 7cfc04
characters) are encoded simply as bytes 0x00 to 0x7f (ASCII
Packit 7cfc04
compatibility).
Packit 7cfc04
This means that files and strings which contain only
Packit 7cfc04
7-bit ASCII characters have the same encoding under both
Packit 7cfc04
ASCII
Packit 7cfc04
and
Packit 7cfc04
UTF-8 .
Packit 7cfc04
.TP
Packit 7cfc04
*
Packit 7cfc04
All UCS characters greater than 0x7f are encoded as a multibyte sequence
Packit 7cfc04
consisting only of bytes in the range 0x80 to 0xfd, so no ASCII
Packit 7cfc04
byte can appear as part of another character and there are no
Packit 7cfc04
problems with, for example,  \(aq\\0\(aq or \(aq/\(aq.
Packit 7cfc04
.TP
Packit 7cfc04
*
Packit 7cfc04
The lexicographic sorting order of UCS-4 strings is preserved.
Packit 7cfc04
.TP
Packit 7cfc04
*
Packit 7cfc04
All possible 2^31 UCS codes can be encoded using UTF-8.
Packit 7cfc04
.TP
Packit 7cfc04
*
Packit 7cfc04
The bytes 0xc0, 0xc1, 0xfe, and 0xff are never used in the UTF-8 encoding.
Packit 7cfc04
.TP
Packit 7cfc04
*
Packit 7cfc04
The first byte of a multibyte sequence which represents a single non-ASCII
Packit 7cfc04
UCS character is always in the range 0xc2 to 0xfd and indicates how long
Packit 7cfc04
this multibyte sequence is.
Packit 7cfc04
All further bytes in a multibyte sequence
Packit 7cfc04
are in the range 0x80 to 0xbf.
Packit 7cfc04
This allows easy resynchronization and
Packit 7cfc04
makes the encoding stateless and robust against missing bytes.
Packit 7cfc04
.TP
Packit 7cfc04
*
Packit 7cfc04
UTF-8 encoded UCS characters may be up to six bytes long, however the
Packit 7cfc04
Unicode standard specifies no characters above 0x10ffff, so Unicode characters
Packit 7cfc04
can be only up to four bytes long in
Packit 7cfc04
UTF-8.
Packit 7cfc04
.SS Encoding
Packit 7cfc04
The following byte sequences are used to represent a character.
Packit 7cfc04
The sequence to be used depends on the UCS code number of the character:
Packit 7cfc04
.TP 0.4i
Packit 7cfc04
0x00000000 \- 0x0000007F:
Packit 7cfc04
.RI 0 xxxxxxx
Packit 7cfc04
.TP
Packit 7cfc04
0x00000080 \- 0x000007FF:
Packit 7cfc04
.RI 110 xxxxx
Packit 7cfc04
.RI 10 xxxxxx
Packit 7cfc04
.TP
Packit 7cfc04
0x00000800 \- 0x0000FFFF:
Packit 7cfc04
.RI 1110 xxxx
Packit 7cfc04
.RI 10 xxxxxx
Packit 7cfc04
.RI 10 xxxxxx
Packit 7cfc04
.TP
Packit 7cfc04
0x00010000 \- 0x001FFFFF:
Packit 7cfc04
.RI 11110 xxx
Packit 7cfc04
.RI 10 xxxxxx
Packit 7cfc04
.RI 10 xxxxxx
Packit 7cfc04
.RI 10 xxxxxx
Packit 7cfc04
.TP
Packit 7cfc04
0x00200000 \- 0x03FFFFFF:
Packit 7cfc04
.RI 111110 xx
Packit 7cfc04
.RI 10 xxxxxx
Packit 7cfc04
.RI 10 xxxxxx
Packit 7cfc04
.RI 10 xxxxxx
Packit 7cfc04
.RI 10 xxxxxx
Packit 7cfc04
.TP
Packit 7cfc04
0x04000000 \- 0x7FFFFFFF:
Packit 7cfc04
.RI 1111110 x
Packit 7cfc04
.RI 10 xxxxxx
Packit 7cfc04
.RI 10 xxxxxx
Packit 7cfc04
.RI 10 xxxxxx
Packit 7cfc04
.RI 10 xxxxxx
Packit 7cfc04
.RI 10 xxxxxx
Packit 7cfc04
.PP
Packit 7cfc04
The
Packit 7cfc04
.I xxx
Packit 7cfc04
bit positions are filled with the bits of the character code number in
Packit 7cfc04
binary representation, most significant bit first (big-endian).
Packit 7cfc04
Only the shortest possible multibyte sequence
Packit 7cfc04
which can represent the code number of the character can be used.
Packit 7cfc04
.PP
Packit 7cfc04
The UCS code values 0xd800\(en0xdfff (UTF-16 surrogates) as well as 0xfffe and
Packit 7cfc04
0xffff (UCS noncharacters) should not appear in conforming UTF-8 streams. According
Packit 7cfc04
to RFC 3629 no point above U+10FFFF should be used, which limits characters to four
Packit 7cfc04
bytes.
Packit 7cfc04
.SS Example
Packit 7cfc04
The Unicode character 0xa9 = 1010 1001 (the copyright sign) is encoded
Packit 7cfc04
in UTF-8 as
Packit 7cfc04
.PP
Packit 7cfc04
.RS
Packit 7cfc04
11000010 10101001 = 0xc2 0xa9
Packit 7cfc04
.RE
Packit 7cfc04
.PP
Packit 7cfc04
and character 0x2260 = 0010 0010 0110 0000 (the "not equal" symbol) is
Packit 7cfc04
encoded as:
Packit 7cfc04
.PP
Packit 7cfc04
.RS
Packit 7cfc04
11100010 10001001 10100000 = 0xe2 0x89 0xa0
Packit 7cfc04
.RE
Packit 7cfc04
.SS Application notes
Packit 7cfc04
Users have to select a UTF-8 locale, for example with
Packit 7cfc04
.PP
Packit 7cfc04
.RS
Packit 7cfc04
export LANG=en_GB.UTF-8
Packit 7cfc04
.RE
Packit 7cfc04
.PP
Packit 7cfc04
in order to activate the UTF-8 support in applications.
Packit 7cfc04
.PP
Packit 7cfc04
Application software that has to be aware of the used character
Packit 7cfc04
encoding should always set the locale with for example
Packit 7cfc04
.PP
Packit 7cfc04
.RS
Packit 7cfc04
setlocale(LC_CTYPE, "")
Packit 7cfc04
.RE
Packit 7cfc04
.PP
Packit 7cfc04
and programmers can then test the expression
Packit 7cfc04
.PP
Packit 7cfc04
.RS
Packit 7cfc04
strcmp(nl_langinfo(CODESET), "UTF-8") == 0
Packit 7cfc04
.RE
Packit 7cfc04
.PP
Packit 7cfc04
to determine whether a UTF-8 locale has been selected and whether
Packit 7cfc04
therefore all plaintext standard input and output, terminal
Packit 7cfc04
communication, plaintext file content, filenames and environment
Packit 7cfc04
variables are encoded in UTF-8.
Packit 7cfc04
.PP
Packit 7cfc04
Programmers accustomed to single-byte encodings such as US-ASCII or ISO 8859
Packit 7cfc04
have to be aware that two assumptions made so far are no longer valid
Packit 7cfc04
in UTF-8 locales.
Packit 7cfc04
Firstly, a single byte does not necessarily correspond any
Packit 7cfc04
more to a single character.
Packit 7cfc04
Secondly, since modern terminal emulators in UTF-8
Packit 7cfc04
mode also support Chinese, Japanese, and Korean
Packit 7cfc04
double-width characters as well as nonspacing combining characters,
Packit 7cfc04
outputting a single character does not necessarily advance the cursor
Packit 7cfc04
by one position as it did in ASCII.
Packit 7cfc04
Library functions such as
Packit 7cfc04
.BR mbsrtowcs (3)
Packit 7cfc04
and
Packit 7cfc04
.BR wcswidth (3)
Packit 7cfc04
should be used today to count characters and cursor positions.
Packit 7cfc04
.PP
Packit 7cfc04
The official ESC sequence to switch from an ISO 2022
Packit 7cfc04
encoding scheme (as used for instance by VT100 terminals) to
Packit 7cfc04
UTF-8 is ESC % G
Packit 7cfc04
("\\x1b%G").
Packit 7cfc04
The corresponding return sequence from
Packit 7cfc04
UTF-8 to ISO 2022 is ESC % @ ("\\x1b%@").
Packit 7cfc04
Other ISO 2022 sequences (such as
Packit 7cfc04
for switching the G0 and G1 sets) are not applicable in UTF-8 mode.
Packit 7cfc04
.SS Security
Packit 7cfc04
The Unicode and UCS standards require that producers of UTF-8
Packit 7cfc04
shall use the shortest form possible, for example, producing a two-byte
Packit 7cfc04
sequence with first byte 0xc0 is nonconforming.
Packit 7cfc04
Unicode 3.1 has added the requirement that conforming programs must not accept
Packit 7cfc04
non-shortest forms in their input.
Packit 7cfc04
This is for security reasons: if
Packit 7cfc04
user input is checked for possible security violations, a program
Packit 7cfc04
might check only for the ASCII
Packit 7cfc04
version of "/../" or ";" or NUL and overlook that there are many
Packit 7cfc04
non-ASCII ways to represent these things in a non-shortest UTF-8
Packit 7cfc04
encoding.
Packit 7cfc04
.SS Standards
Packit 7cfc04
ISO/IEC 10646-1:2000, Unicode 3.1, RFC\ 3629, Plan 9.
Packit 7cfc04
.\" .SH AUTHOR
Packit 7cfc04
.\" Markus Kuhn <mgk25@cl.cam.ac.uk>
Packit 7cfc04
.SH SEE ALSO
Packit 7cfc04
.BR locale (1),
Packit 7cfc04
.BR nl_langinfo (3),
Packit 7cfc04
.BR setlocale (3),
Packit 7cfc04
.BR charsets (7),
Packit 7cfc04
.BR unicode (7)
Packit 7cfc04
.SH COLOPHON
Packit 7cfc04
This page is part of release 4.15 of the Linux
Packit 7cfc04
.I man-pages
Packit 7cfc04
project.
Packit 7cfc04
A description of the project,
Packit 7cfc04
information about reporting bugs,
Packit 7cfc04
and the latest version of this page,
Packit 7cfc04
can be found at
Packit 7cfc04
\%https://www.kernel.org/doc/man\-pages/.