UTF-8(7) Linux Programmer’s Manual UTF-8(7) 名前 UTF-8 - ASCII と互換性のある多バイト Unicode の符号化 説明 ユニコード (Unicode) 3.0 文字集合は 16 ビットのコード空間を占める。最も 単純な Unicode の符号化方法 (UCS-2) では、文字は 16 ビット・ワード (16 ビット文字の列) で構成される。この列には、 '\0' や '/' のような (ファイ ル名や C のライブラリ関数の引き数の内部で) 特殊な意味を持つ 16 ビット文 字が含まれることがある。さらに、ほとんどの Unix ツールは ASCII ファイル を入力として期待するので、大幅な変更なしには 16 ビットワードを文字と し て読むことができない。これらの理由から、 UCS-2 はファイル名・テキストフ ァイル・環境変数などに用いる、外部用の Unicode 符号としては不適切である 。 Unicode の スーパーセットである ISO 10646 Universal Character Set (UCS) は 31 ビットのコード空間を占めるが、その最も単純な符号化 で あ る UCS-4 にも (32 ビット・ワードの列として) 同じ問題がある。 Unicode と UCS の UTF-8 符号化にはこれらの問題がないので、Unix 形式の OS 上で Unicode 文字集合を使用するための一般的な方法となっている。 性質 UTF-8 符号化は以下のような素晴しい性質を備えている: * UCS 文字のうち 0x00000000 から 0x0000007f まで (古典的な US-ASCII の 文 字) は (ASCII との互換性のために) 単純に 0x00 から 0x7f のバイトに 符号化する。これは 7 ビット ASCII 文字のみを含むファイルや文字列に 関 しては、 ASCII と UTF-8 で同じ符号化を行なうことを意味する。 * 0x7f より大きいのすべての UCS 文字は、 0x80 から 0xfd までの範囲のバ イトのみを含む多バイト文字列に符号化される。したがって文字列に ASCII バイトが含まれることがなく、'\0' や '/' の問題は発生しない。 * UCS-4 文字列では辞書的ソートの順序が保たれる。 * 2^31 ビットのすべての UCS コード が UTF-8 を使用して符号化できる。 * UTF-8 符号化では 0xfe と 0xff のバイトは絶対に使用しない。 * ASCII で な い UCS 文字の多バイト列の最初のバイトは、常に 0xc0 から 0xfd の範囲で表現され、その文字が何バイトで構成されているかを示す。多 バ イト列の残りの部分のバイトは、それぞれ 0x80 から 0xbf の範囲にある 。これにより同期が容易になり、ステートレスな符号化が可能になり、バ イ トの紛失に対して堅固になる。 * UTF-8 を使用した UCS 文字の符号化は最大 6 バイトの長さになる。しかし 、 Unicode 規格では 0x10ffff より先の文字を指定しないので、Unicode 文 字は UTF-8 では 4 バイトまでにしかならない。 符号化 以 下のバイト列が文字の表現に使用される。どのバイト列を使用するかは文字 の UCS コード番号に依存する: 0x00000000 - 0x0000007F: 0xxxxxxx 0x00000080 - 0x000007FF: 110xxxxx 10xxxxxx 0x00000800 - 0x0000FFFF: 1110xxxx 10xxxxxx 10xxxxxx 0x00010000 - 0x001FFFFF: 11110xxx 10xxxxxx 10xxxxxx 10xxxxxx 0x00200000 - 0x03FFFFFF: 111110xx 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx 0x04000000 - 0x7FFFFFFF: 1111110x 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx xxx ビットの部分には 2 進数で表わした文字コードのビット部分が対応する。 その文字を表現するのに最も短いバイト列のみが使用できる。 0xd800–0xdfff (UTF-16 サロゲート) や 0xfffe, 0xffff (UCS の non-charac- ter) という UCS コードの値は、 UTF-8 に準拠したストリームに入れるべきで はない。 例 Unicode 文字の 0xa9 = 1010 1001 (コピーライト・マーク) は UTF-8 で符号 化すると 11000010 10101001 = 0xc2 0xa9 になる。 0x2260 = 0010 0010 0110 0000 (不等号) は 11100010 10001001 10100000 = 0xe2 0x89 0xa0 になる。 アプリケーションにおける注意 ユーザーはアプリケーションの UTF-8 サポートを有効にするために、以下のよ うにして UTF-8 ロケールを選択しなければならない。 export LANG=en_GB.UTF-8 使 用されている文字符号化を分かっていなければならないアプリケーションソ フトウェアは、以下のようにして常にロケールを設定すべきである。 setlocale(LC_CTYPE, "") また UTF-8 ロケールが選択されていて、プレーンテキストの標準入出力・端末 間通信・プレーンテキストファイルの内容・ファイル名・環境変数が UTF-8 で 符号化されているかをチェックするために、プログラマーは以下のような式 を 試すことができる。 strcmp(nl_langinfo(CODESET), "UTF-8") == 0 US-ASCII や ISO 8859 といったシングルバイトの符号化が習慣になっているプ ログラマーは、これまでの 2 つの仮定が UTF-8 ロケールにおいては最早有 効 ではなくなったことを知っておくべきだ。 1 番目の変更点は、1 バイトが必ず しも 1 つの文字に対応しないという点である。 2 番目の変更点は、最近の 端 末 エミュレータは UTF-8 モードにおいて中国語・日本語・韓国朝鮮語の 全角 文字やスペースが入らない (non-spacing) 合成文字 (combining characters) に 対応しているので、 ASCII のときのように 1 文字出力した後でカーソルを 必ずしも 1 つだけ進めるわけではないという点である。今日では、文字やカー ソルの位置を数えるのに mbsrtowcs(3) や wcswidth(3) といったライブラリ関 数を使うべきである。 (VT100 端末などで使われる) ISO 2022 符号化形式から UTF-8 へ切替える公式 な エ ス ケ ー プシーケンスは ESC % G ("\x1b%G") である。これに対応する UTF-8 から ISO 2022 へのリターンシーケンスは ESC % @ ("\x1b%@") であ る 。 (G0 セットと G1 セットを切替えるといった) その他の ISO 2022 シーケン スは、UTF-8 モードでは使えない。 予知できる将来では、POSIX システム上の一般的な文字符号化の全てのレベ ル で UTF-8 が ASCII と ISO 8859 を置き換え、プレーンテキストを扱う非常に 優れた環境が作られることが期待できる。 セキュリティ Unicode と UCS の規格では、 UTF-8 の生成者はできるだけ短い形式を用い る よう要求している。例えば、先頭バイトが 0xc0 であるような 2 バイト列を生 成するのは準拠しているとはいえない。 Unicode 3.1 では、規格に準拠するプ ロ グラムは最短の表現形式ではない入力を受け付けない、という要求事項が追 加された。これはセキュリティ上の理由による。ユーザー入力がセキュリテ ィ 上の危険に対しチェックされる場合、プログラムは ASCII 版の "/../" や ";" や "NUL" だけをチェックし、最短に符号化されてないこれらの文字を見過ごし てしまうかもしれないからである。なぜなら、最短ではない UTF-8 符号化では 、これらの文字を表現するような様々な ASCII 以外の形式が存在するためであ る。 準拠 ISO/IEC 10646-1:2000, Unicode 3.1, RFC 2279, Plan 9. 関連項目 nl_langinfo(3), setlocale(3), charsets(7), unicode(7) GNU 2001-05-11 UTF-8(7)
UTF-8(7) Linux Programmer’s Manual UTF-8(7) NAME UTF-8 - an ASCII compatible multi-byte Unicode encoding DESCRIPTION The Unicode 3.0 character set occupies a 16-bit code space. The most obvious Unicode encoding (known as UCS-2) consists of a sequence of 16-bit words. Such strings can contain as parts of many 16-bit charac- ters bytes like '\0' or '/' which have a special meaning in filenames and other C library function arguments. In addition, the majority of Unix tools expects ASCII files and can’t read 16-bit words as charac- ters without major modifications. For these reasons, UCS-2 is not a suitable external encoding of Unicode in filenames, text files, envi- ronment variables, etc. The ISO 10646 Universal Character Set (UCS), a superset of Unicode, occupies even a 31-bit code space and the obvious UCS-4 encoding for it (a sequence of 32-bit words) has the same prob- lems. The UTF-8 encoding of Unicode and UCS does not have these problems and is the common way in which Unicode is used on Unix-style operating sys- tems. Properties The UTF-8 encoding has the following nice properties: * UCS characters 0x00000000 to 0x0000007f (the classic US-ASCII charac- ters) are encoded simply as bytes 0x00 to 0x7f (ASCII compatibility). This means that files and strings which contain only 7-bit ASCII characters have the same encoding under both ASCII and UTF-8. * All UCS characters greater than 0x7f are encoded as a multi-byte sequence consisting only of bytes in the range 0x80 to 0xfd, so no ASCII byte can appear as part of another character and there are no problems with, for example, '\0' or '/'. * The lexicographic sorting order of UCS-4 strings is preserved. * All possible 2^31 UCS codes can be encoded using UTF-8. * The bytes 0xfe and 0xff are never used in the UTF-8 encoding. * The first byte of a multi-byte sequence which represents a single non-ASCII UCS character is always in the range 0xc0 to 0xfd and indi- cates how long this multi-byte sequence is. All further bytes in a multi-byte sequence are in the range 0x80 to 0xbf. This allows easy resynchronization and makes the encoding stateless and robust against missing bytes. * UTF-8 encoded UCS characters may be up to six bytes long, however the Unicode standard specifies no characters above 0x10ffff, so Unicode characters can only be up to four bytes long in UTF-8. Encoding The following byte sequences are used to represent a character. The sequence to be used depends on the UCS code number of the character: 0x00000000 - 0x0000007F: 0xxxxxxx 0x00000080 - 0x000007FF: 110xxxxx 10xxxxxx 0x00000800 - 0x0000FFFF: 1110xxxx 10xxxxxx 10xxxxxx 0x00010000 - 0x001FFFFF: 11110xxx 10xxxxxx 10xxxxxx 10xxxxxx 0x00200000 - 0x03FFFFFF: 111110xx 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx 0x04000000 - 0x7FFFFFFF: 1111110x 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx The xxx bit positions are filled with the bits of the character code number in binary representation. Only the shortest possible multi-byte sequence which can represent the code number of the character can be used. The UCS code values 0xd800–0xdfff (UTF-16 surrogates) as well as 0xfffe and 0xffff (UCS non-characters) should not appear in conforming UTF-8 streams. Example The Unicode character 0xa9 = 1010 1001 (the copyright sign) is encoded in UTF-8 as 11000010 10101001 = 0xc2 0xa9 and character 0x2260 = 0010 0010 0110 0000 (the "not equal" symbol) is encoded as: 11100010 10001001 10100000 = 0xe2 0x89 0xa0 Application Notes Users have to select a UTF-8 locale, for example with export LANG=en_GB.UTF-8 in order to activate the UTF-8 support in applications. Application software that has to be aware of the used character encod- ing should always set the locale with for example setlocale(LC_CTYPE, "") and programmers can then test the expression strcmp(nl_langinfo(CODESET), "UTF-8") == 0 to determine whether a UTF-8 locale has been selected and whether therefore all plaintext standard input and output, terminal communica- tion, plaintext file content, filenames and environment variables are encoded in UTF-8. Programmers accustomed to single-byte encodings such as US-ASCII or ISO 8859 have to be aware that two assumptions made so far are no longer valid in UTF-8 locales. Firstly, a single byte does not necessarily correspond any more to a single character. Secondly, since modern ter- minal emulators in UTF-8 mode also support Chinese, Japanese, and Korean double-width characters as well as non-spacing combining charac- ters, outputting a single character does not necessarily advance the cursor by one position as it did in ASCII. Library functions such as mbsrtowcs(3) and wcswidth(3) should be used today to count characters and cursor positions. The official ESC sequence to switch from an ISO 2022 encoding scheme (as used for instance by VT100 terminals) to UTF-8 is ESC % G ("\x1b%G"). The corresponding return sequence from UTF-8 to ISO 2022 is ESC % @ ("\x1b%@"). Other ISO 2022 sequences (such as for switching the G0 and G1 sets) are not applicable in UTF-8 mode. It can be hoped that in the foreseeable future, UTF-8 will replace ASCII and ISO 8859 at all levels as the common character encoding on POSIX systems, leading to a significantly richer environment for han- dling plain text. Security The Unicode and UCS standards require that producers of UTF-8 shall use the shortest form possible, for example, producing a two-byte sequence with first byte 0xc0 is non-conforming. Unicode 3.1 has added the requirement that conforming programs must not accept non-shortest forms in their input. This is for security reasons: if user input is checked for possible security violations, a program might check only for the ASCII version of "/../" or ";" or NUL and overlook that there are many non-ASCII ways to represent these things in a non-shortest UTF-8 encod- ing. Standards ISO/IEC 10646-1:2000, Unicode 3.1, RFC 2279, Plan 9. SEE ALSO nl_langinfo(3), setlocale(3), charsets(7), unicode(7) COLOPHON This page is part of release 3.22 of the Linux man-pages project. A description of the project, and information about reporting bugs, can be found at http://www.kernel.org/doc/man-pages/. GNU 2001-05-11 UTF-8(7)
Copyright(C) linux-cmd.com All Rights Reserved. Author Takayuki Yukawa