nx_utf8.nx
buildroot/runtime/nx_utf8.nx
about
utf8.nx -- UTF-8 validator + codepoint decoder (RFC 3629).
Needed by: X.509 UTF8String fields, HTTP headers, JSON strings,
NishiLang source code itself. Without a validator we either
accept malformed sequences (security problem: overlong forms
can hide dangerous characters) or reject valid input (bugs).
UTF-8 byte patterns (RFC 3629 §3):
0xxxxxxx 1 byte, U+0000..U+007F
110xxxxx 10xxxxxx 2 bytes, U+0080..U+07FF
1110xxxx 10xxxxxx 10xxxxxx 3 bytes, U+0800..U+FFFF
11110xxx 10xxxxxx 10xxxxxx 10xxxxxx 4 bytes, U+10000..U+10FFFF
RFC 3629 §4 additional constraints:
- Reject overlong encodings (fewer bytes than minimum for
the codepoint). E.g., U+0000 must be `00`, not `C0 80`.
- Reject surrogates U+D800..U+DFFF (reserved for UTF-16).
- Reject codepoints > U+10FFFF.
Invariants:
U1 utf8_validate returns exact byte position of first
invalid byte, or input length on clean success.
U2 utf8_decode_one advances the caller's position by the
number of bytes consumed; returns negative codepoint
on invalid sequence.
U3 No reads beyond the input's declared length; no buffer
overruns possible from malformed input.
dependencies 1 imports · 5 importers
imports: nx_syscalls.nx
imported by: nx_caption_render_uni.nxnx_cyrillic_render_gate.nxnx_live_xlate_call_gate.nxnx_multilang_call_gate.nxnx_multilang_render_gate.nx
call flow from main pre-order; caps 40 nodes / depth 6 declared; ↻ = already shown
structs
| none |
consts
| 38 | const UTF8_ERR_INVALID_BYTE: i64 = -1 |
| 39 | const UTF8_ERR_TRUNCATED: i64 = -2 |
| 40 | const UTF8_ERR_OVERLONG: i64 = -3 |
| 41 | const UTF8_ERR_SURROGATE: i64 = -4 |
| 42 | const UTF8_ERR_OUT_OF_RANGE: i64 = -5 |
functions
| 49 | func utf8_validate(bytes: *u8, n: i64) -> i64 called by 1: main |
| 121 | func utf8_decode_one(bytes: *u8, len: i64, pos: *i64) -> i64 |
| 174 | func utf8_encode_one(cp: i64, out: *u8, pos: *i64) -> i64 called by 1: main |
| 209 | func main() -> i64 |