4. Syntax von UTF-8-Byte-Sequenzen
Zur Bequemlichkeit von Implementierern, die ABNF verwenden, wird hier eine Definition von UTF-8 in ABNF-Syntax gegeben.
UTF-8-Zeichenfolgen-Definition (UTF-8 String Definition)
Eine UTF-8-Zeichenfolge ist eine Sequenz von Bytes, die eine Sequenz von UCS-Zeichen darstellt. Eine Byte-Sequenz ist nur dann gültiges UTF-8, wenn sie mit der folgenden Syntax übereinstimmt, die von den UTF-8-Kodierungsregeln abgeleitet ist und im ABNF von [RFC2234] ausgedrückt wird.
ABNF-Syntax
UTF8-octets = *( UTF8-char )
UTF8-char = UTF8-1 / UTF8-2 / UTF8-3 / UTF8-4
UTF8-1 = %x00-7F
UTF8-2 = %xC2-DF UTF8-tail
UTF8-3 = %xE0 %xA0-BF UTF8-tail / %xE1-EC 2( UTF8-tail ) /
%xED %x80-9F UTF8-tail / %xEE-EF 2( UTF8-tail )
UTF8-4 = %xF0 %x90-BF 2( UTF8-tail ) / %xF1-F3 3( UTF8-tail ) /
%xF4 %x80-8F 2( UTF8-tail )
UTF8-tail = %x80-BF
Syntaxerklärung (Syntax Explanation)
UTF8-1: Ein-Byte-Sequenzen
%x00-7F
- Bereich: 0x00 bis 0x7F (0-127)
- Kodierung: Vollständiger ASCII-Bereich
- Muster:
0xxxxxxx
UTF8-2: Zwei-Byte-Sequenzen
%xC2-DF UTF8-tail
- Erstes Byte: 0xC2 bis 0xDF (194-223)
- Folgebyte: 0x80 bis 0xBF (128-191)
- Muster:
110xxxxx 10xxxxxx - Hinweis: Das erste Byte kann nicht 0xC0 oder 0xC1 sein (würde überlange Kodierung erzeugen)
UTF8-3: Drei-Byte-Sequenzen
Vier Fälle:
Fall 1: %xE0 %xA0-BF UTF8-tail
- Erstes Byte: 0xE0
- Zweites Byte: 0xA0 bis 0xBF
- Drittes Byte: 0x80 bis 0xBF
- Kodierungsbereich: U+0800 bis U+0FFF
Fall 2: %xE1-EC 2( UTF8-tail )
- Erstes Byte: 0xE1 bis 0xEC
- Folgende Bytes: Zwei UTF8-tail (0x80-0xBF)
- Kodierungsbereich: U+1000 bis U+CFFF
Fall 3: %xED %x80-9F UTF8-tail
- Erstes Byte: 0xED
- Zweites Byte: 0x80 bis 0x9F
- Drittes Byte: 0x80 bis 0xBF
- Kodierungsbereich: U+D000 bis U+D7FF
- Hinweis: Vermeidet den Ersatzpaar-Bereich (U+D800-U+DFFF)
Fall 4: %xEE-EF 2( UTF8-tail )
- Erstes Byte: 0xEE bis 0xEF
- Folgende Bytes: Zwei UTF8-tail
- Kodierungsbereich: U+E000 bis U+FFFF
UTF8-4: Vier-Byte-Sequenzen
Drei Fälle:
Fall 1: %xF0 %x90-BF 2( UTF8-tail )
- Erstes Byte: 0xF0
- Zweites Byte: 0x90 bis 0xBF
- Folgende Bytes: Zwei UTF8-tail
- Kodierungsbereich: U+10000 bis U+3FFFF
Fall 2: %xF1-F3 3( UTF8-tail )
- Erstes Byte: 0xF1 bis 0xF3
- Folgende Bytes: Drei UTF8-tail
- Kodierungsbereich: U+40000 bis U+FFFFF
Fall 3: %xF4 %x80-8F 2( UTF8-tail )
- Erstes Byte: 0xF4
- Zweites Byte: 0x80 bis 0x8F
- Folgende Bytes: Zwei UTF8-tail
- Kodierungsbereich: U+100000 bis U+10FFFF
Ungültige Byte-Werte (Invalid Byte Values)
Die folgenden Byte-Werte erscheinen niemals in gültigen UTF-8-Sequenzen:
Prohibited byte values:
- 0xC0, 0xC1 (would produce overlong 2-byte sequences)
- 0xF5 - 0xFF (beyond Unicode range)
Vollständige Übersicht der Bytebereiche
| Bytewertbereich | Bedeutung | Gültigkeit |
|---|---|---|
| 0x00-0x7F | Einzelbyte-Zeichen (ASCII) | ✅ Gültig |
| 0x80-0xBF | Folgebyte | ✅ Nur als Folgebyte gültig |
| 0xC0-0xC1 | Verboten | ❌ Ungültig |
| 0xC2-0xDF | Erstes Byte einer 2-Byte-Sequenz | ✅ Gültig |
| 0xE0-0xEF | Erstes Byte einer 3-Byte-Sequenz | ✅ Gültig |
| 0xF0-0xF4 | Erstes Byte einer 4-Byte-Sequenz | ✅ Gültig |
| 0xF5-0xFF | Verboten | ❌ Ungültig |
Validierungsbeispiele
Gültige Sequenzen
Example 1: 0x41
Check: 0x41 in [0x00-0x7F] → UTF8-1 → ✅ Valid
Character: 'A'
Example 2: 0xC2 0xA9
Check: 0xC2 in [0xC2-0xDF], 0xA9 in [0x80-0xBF] → UTF8-2 → ✅ Valid
Character: '©'
Example 3: 0xE4 0xBD 0xA0
Check: 0xE4 in [0xE1-0xEC], next two bytes in [0x80-0xBF] → UTF8-3 → ✅ Valid
Character: '你'
Example 4: 0xF0 0x9F 0x98 0x80
Check: 0xF0 followed by 0x9F in [0x90-0xBF], next two bytes in [0x80-0xBF] → UTF8-4 → ✅ Valid
Character: '😀'
Ungültige Sequenzen
Example 1: 0xC0 0x80
Problem: 0xC0 is prohibited → ❌ Invalid (overlong encoding)
Example 2: 0xED 0xA0 0x80
Problem: 0xED followed by 0xA0 not in [0x80-0x9F] → ❌ Invalid (surrogate pair range)
Example 3: 0xF5 0x80 0x80 0x80
Problem: 0xF5 is prohibited → ❌ Invalid (beyond Unicode range)
Example 4: 0xE4 0xBD
Problem: 3-byte sequence incomplete → ❌ Invalid (truncated)
⚠️ Wichtiger Hinweis
HINWEIS -- Die autoritative Definition von UTF-8 findet sich in [UNICODE]. Diese Grammatik soll dasselbe beschreiben, was Unicode beschreibt, erhebt jedoch keinen Anspruch auf Autorität. Implementierer werden aufgefordert, sich auf die autoritative Quelle zu verlassen, anstatt auf dieses ABNF.
Implementierungsvorschlag
Validierungsalgorithmus-Pseudocode
def is_valid_utf8(bytes):
i = 0
while i < len(bytes):
b = bytes[i]
if b <= 0x7F: # UTF8-1
i += 1
elif 0xC2 <= b <= 0xDF: # UTF8-2
if i + 1 >= len(bytes) or not (0x80 <= bytes[i+1] <= 0xBF):
return False
i += 2
elif b == 0xE0: # UTF8-3 Fall 1
if i + 2 >= len(bytes):
return False
if not (0xA0 <= bytes[i+1] <= 0xBF and 0x80 <= bytes[i+2] <= 0xBF):
return False
i += 3
elif 0xE1 <= b <= 0xEC: # UTF8-3 Fall 2
if i + 2 >= len(bytes):
return False
if not (0x80 <= bytes[i+1] <= 0xBF and 0x80 <= bytes[i+2] <= 0xBF):
return False
i += 3
elif b == 0xED: # UTF8-3 Fall 3
if i + 2 >= len(bytes):
return False
if not (0x80 <= bytes[i+1] <= 0x9F and 0x80 <= bytes[i+2] <= 0xBF):
return False
i += 3
elif 0xEE <= b <= 0xEF: # UTF8-3 Fall 4
if i + 2 >= len(bytes):
return False
if not (0x80 <= bytes[i+1] <= 0xBF and 0x80 <= bytes[i+2] <= 0xBF):
return False
i += 3
elif b == 0xF0: # UTF8-4 Fall 1
if i + 3 >= len(bytes):
return False
if not (0x90 <= bytes[i+1] <= 0xBF and
0x80 <= bytes[i+2] <= 0xBF and
0x80 <= bytes[i+3] <= 0xBF):
return False
i += 4
elif 0xF1 <= b <= 0xF3: # UTF8-4 Fall 2
if i + 3 >= len(bytes):
return False
if not (0x80 <= bytes[i+1] <= 0xBF and
0x80 <= bytes[i+2] <= 0xBF and
0x80 <= bytes[i+3] <= 0xBF):
return False
i += 4
elif b == 0xF4: # UTF8-4 Fall 3
if i + 3 >= len(bytes):
return False
if not (0x80 <= bytes[i+1] <= 0x8F and
0x80 <= bytes[i+2] <= 0xBF and
0x80 <= bytes[i+3] <= 0xBF):
return False
i += 4
else:
return False # Ungültiges Byte
return True
Verwandte Links
- Zurück: 3. UTF-8-Definition
- Zurück zur RFC 3629 Startseite
- Weiter: 5. Versionen der Standards