Skip to main content

c/
mbyte.rs

1// Copyright (c) 2026 Meng Zhuo
2// SPDX-License-Identifier: BSD-3-Clause
3
4// ============================================================
5// multibyte (UTF-8, musl state machine)
6// ============================================================
7
8const SA: u8 = 0xC2;
9const SB: u8 = 0xF4;
10
11const BITTAB: [u32; 51] = [
12    0xC000_0002, 0xC000_0003, 0xC000_0004, 0xC000_0005, 0xC000_0006, 0xC000_0007,
13    0xC000_0008, 0xC000_0009, 0xC000_000A, 0xC000_000B, 0xC000_000C, 0xC000_000D,
14    0xC000_000E, 0xC000_000F, 0xC000_0010, 0xC000_0011, 0xC000_0012, 0xC000_0013,
15    0xC000_0014, 0xC000_0015, 0xC000_0016, 0xC000_0017, 0xC000_0018, 0xC000_0019,
16    0xC000_001A, 0xC000_001B, 0xC000_001C, 0xC000_001D, 0xC000_001E, 0xC000_001F,
17    0xB300_0000, 0xC300_0001, 0xC300_0002, 0xC300_0003, 0xC300_0004, 0xC300_0005,
18    0xC300_0006, 0xC300_0007, 0xC300_0008, 0xC300_0009, 0xC300_000A, 0xC300_000B,
19    0xC300_000C, 0xD300_000D, 0xC300_000E, 0xC300_000F, 0xBB0C_0000, 0xC30C_0001,
20    0xC30C_0002, 0xC30C_0003, 0xDB0C_0004,
21];
22
23#[inline]
24fn mb_oob(c: u32, b: u8) -> u32 {
25    let b3 = (b >> 3) as u32;
26    ((b3.wrapping_sub(0x10)) | (b3.wrapping_add((c as i32 >> 26) as u32))) & !7
27}
28
29static mut MB_STATE: c_uint = 0;
30
31/// Select the caller's multibyte state or the process-global fallback used
32/// when `st` is null.
33#[inline]
34unsafe fn mb_state_or(st: *mut c_uint) -> &'static mut c_uint {
35    if st.is_null() { &mut *(&raw mut MB_STATE) } else { &mut *st }
36}
37
38const MB_CUR_MAX_VAL_UTF8: usize = 4;
39const MB_CUR_MAX_VAL_C: usize = 1;
40
41#[no_mangle]
42pub extern "C" fn __ctype_get_mb_cur_max() -> usize {
43    unsafe { if LOCALE_CTYPE_UTF8 { MB_CUR_MAX_VAL_UTF8 } else { MB_CUR_MAX_VAL_C } }
44}
45
46#[no_mangle]
47pub unsafe extern "C" fn mbrtowc(
48    wc: *mut c_int,
49    src: *const c_char,
50    n: usize,
51    st: *mut c_uint,
52) -> usize {
53    let s = src as *const u8;
54    let n0 = n;
55    let mut dummy: c_int = 0;
56    let wc = if wc.is_null() { &mut dummy } else { &mut *wc };
57    let st = mb_state_or(st);
58
59    let mut c: u32 = *st;
60    let mut s = s;
61    let mut n = n;
62
63    if s.is_null() {
64        if c != 0 { *st = 0; ERRNO!() = EILSEQ; return !0usize; }
65        return 0;
66    }
67
68    if n == 0 { return !1usize; }
69
70    if unsafe { !LOCALE_CTYPE_UTF8 } {
71        let b = *s;
72        *wc = c_locale_codeunit(b) as c_int;
73        return if b == 0 { 0 } else { 1 };
74    }
75
76    if c == 0 {
77        if *s < 0x80 {
78            let r = if *s == 0 { 0 } else { 1 };
79            *wc = *s as c_int;
80            return r;
81        }
82        if (*s).wrapping_sub(SA) as u32 > (SB - SA) as u32 {
83            *st = 0; ERRNO!() = EILSEQ; return !0usize;
84        }
85        c = BITTAB[*s as usize - SA as usize];
86        s = s.add(1);
87        n -= 1;
88    }
89
90    if n > 0 {
91        if mb_oob(c, *s) != 0 {
92            *st = 0; ERRNO!() = EILSEQ; return !0usize;
93        }
94        loop {
95            c = (c << 6) | (*s as u32).wrapping_sub(0x80);
96            s = s.add(1);
97            n -= 1;
98            if c & (1u32 << 31) == 0 {
99                *st = 0;
100                *wc = c as c_int;
101                return n0 - n;
102            }
103            if n == 0 { break; }
104            if (*s).wrapping_sub(0x80) >= 0x40 {
105                *st = 0; ERRNO!() = EILSEQ; return !0usize;
106            }
107        }
108    }
109
110    *st = c;
111    !1usize
112}
113
114#[no_mangle]
115pub unsafe extern "C" fn wcrtomb(
116    s: *mut c_char,
117    wc: c_int,
118    _st: *mut c_uint,
119) -> usize {
120    if s.is_null() { return 1; }
121    let wc_u = wc as u32;
122    if unsafe { !LOCALE_CTYPE_UTF8 } {
123        if wc_u < 0x80 {
124            *s = wc as c_char;
125            return 1;
126        }
127        if c_locale_is_codeunit(wc_u) {
128            *s = (wc_u & 0xFF) as c_char;
129            return 1;
130        }
131        ERRNO!() = EILSEQ;
132        return !0usize;
133    }
134    if wc_u < 0x80 {
135        *s = wc as c_char;
136        return 1;
137    }
138    if wc_u < 0x800 {
139        *s = (0xC0 | (wc_u >> 6)) as c_char;
140        *s.add(1) = (0x80 | (wc_u & 0x3F)) as c_char;
141        return 2;
142    }
143    if wc_u < UNICODE_SURROGATE_START || wc_u.wrapping_sub(UNICODE_SURROGATE_END_EXCL) < 0x2000 {
144        *s = (0xE0 | (wc_u >> 12)) as c_char;
145        *s.add(1) = (0x80 | ((wc_u >> 6) & 0x3F)) as c_char;
146        *s.add(2) = (0x80 | (wc_u & 0x3F)) as c_char;
147        return 3;
148    }
149    if wc_u.wrapping_sub(UNICODE_SUPPLEMENTARY_BASE) < UNICODE_SUPPLEMENTARY_SIZE {
150        *s = (0xF0 | (wc_u >> 18)) as c_char;
151        *s.add(1) = (0x80 | ((wc_u >> 12) & 0x3F)) as c_char;
152        *s.add(2) = (0x80 | ((wc_u >> 6) & 0x3F)) as c_char;
153        *s.add(3) = (0x80 | (wc_u & 0x3F)) as c_char;
154        return 4;
155    }
156    ERRNO!() = EILSEQ;
157    !0usize
158}
159
160#[no_mangle]
161pub unsafe extern "C" fn mblen(s: *const c_char, n: usize) -> c_int {
162    if s.is_null() { return 0; }
163    mbtowc(core::ptr::null_mut(), s, n)
164}
165
166#[no_mangle]
167pub unsafe extern "C" fn mbtowc(
168    wc: *mut c_int,
169    src: *const c_char,
170    n: usize,
171) -> c_int {
172    if src.is_null() { return 0; }
173    if n == 0 { ERRNO!() = EILSEQ; return -1; }
174    let mut state: c_uint = 0;
175    let mut w: c_int = 0;
176    let r = mbrtowc(&mut w, src, n, &mut state);
177    if r == !0usize { return -1; }
178    if !wc.is_null() { *wc = w; }
179    r as c_int
180}
181
182#[no_mangle]
183pub unsafe extern "C" fn wctomb(s: *mut c_char, wc: c_int) -> c_int {
184    if s.is_null() { return 0; }
185    wcrtomb(s, wc, core::ptr::null_mut()) as c_int
186}
187
188#[no_mangle]
189pub unsafe extern "C" fn mbsrtowcs(
190    ws: *mut c_int,
191    src: *mut *const c_char,
192    wn: usize,
193    st: *mut c_uint,
194) -> usize {
195    let st = mb_state_or(st);
196    let mut s = *src as *const u8;
197    let wn0 = wn;
198    let mut wn = wn;
199
200    if ws.is_null() {
201        while *s != 0 {
202            if *s < 0x80 {
203                s = s.add(1);
204                wn = wn.wrapping_sub(1);
205                continue;
206            }
207            let mut wc: c_int = 0;
208            let r = mbrtowc(&mut wc, s as *const c_char, 4, st);
209            if r == !0usize { return !0usize; }
210            if r == !1usize { return wn0 - wn; }
211            s = s.add(r);
212            wn = wn.wrapping_sub(1);
213        }
214        wn0 - wn
215    } else {
216        let mut w = ws;
217        while wn > 0 {
218            if *s < 0x80 {
219                let c = *s as c_int;
220                *w = c;
221                if c == 0 {
222                    *src = core::ptr::null();
223                    return wn0 - wn;
224                }
225                w = w.add(1);
226                s = s.add(1);
227                wn -= 1;
228                continue;
229            }
230            let mut wc: c_int = 0;
231            let r = mbrtowc(&mut wc, s as *const c_char, 4, st);
232            if r == !0usize {
233                *src = s as *const c_char;
234                return !0usize;
235            }
236            if r == !1usize {
237                *src = s as *const c_char;
238                return wn0 - wn;
239            }
240            *w = wc;
241            w = w.add(1);
242            s = s.add(r);
243            wn -= 1;
244        }
245        *src = s as *const c_char;
246        wn0 - wn
247    }
248}
249
250#[no_mangle]
251pub unsafe extern "C" fn wcsrtombs(
252    s: *mut c_char,
253    ws: *mut *const c_int,
254    n: usize,
255    _st: *mut c_uint,
256) -> usize {
257    let mut w = *ws;
258    let n0 = n;
259    let mut n = n;
260
261    if s.is_null() {
262        let mut count = 0usize;
263        while *w != 0 {
264            if (*w as u32) < 0x80 {
265                count += 1;
266            } else {
267                let mut buf = [0u8; 4];
268                let r = wcrtomb(buf.as_mut_ptr() as *mut c_char, *w, core::ptr::null_mut());
269                if r == !0usize { return !0usize; }
270                count += r;
271            }
272            w = w.add(1);
273        }
274        return count;
275    }
276
277    let mut dst = s;
278    while n > 0 {
279        if (*w as u32) < 0x80 {
280            *dst = *w as c_char;
281            if *w == 0 {
282                *ws = core::ptr::null();
283                return n0 - n;
284            }
285            dst = dst.add(1);
286            n -= 1;
287        } else {
288            // wcrtomb has no length argument: convert into a temporary first,
289            // then check whether the whole character fits (musl does the same).
290            let mut tmp = [0u8; 4];
291            let r = wcrtomb(tmp.as_mut_ptr() as *mut c_char, *w, core::ptr::null_mut());
292            if r == !0usize {
293                *ws = w;
294                return !0usize;
295            }
296            if r > n {
297                *ws = w;
298                return n0 - n;
299            }
300            core::ptr::copy_nonoverlapping(tmp.as_ptr(), dst as *mut u8, r);
301            dst = dst.add(r);
302            n -= r;
303        }
304        w = w.add(1);
305    }
306    *ws = w;
307    n0
308}
309
310#[no_mangle]
311pub unsafe extern "C" fn mbstowcs(
312    ws: *mut c_int,
313    src: *const c_char,
314    wn: usize,
315) -> usize {
316    let mut src_ptr = src;
317    mbsrtowcs(ws, &mut src_ptr, wn, core::ptr::null_mut())
318}
319
320#[no_mangle]
321pub unsafe extern "C" fn wcstombs(
322    s: *mut c_char,
323    ws: *const c_int,
324    n: usize,
325) -> usize {
326    let mut ws_ptr = ws;
327    wcsrtombs(s, &mut ws_ptr, n, core::ptr::null_mut())
328}
329
330// ============================================================
331// wchar: btowc / wctob / mbsinit / mbrlen
332// ============================================================
333
334// musl's C locale represents bytes >= 0x80 as the single-byte "code units"
335// 0xDF80..0xDFFF so they are distinct from Unicode code points.
336#[inline]
337fn c_locale_codeunit(b: u8) -> u32 {
338    0xdfff & (b as i8 as i32) as u32
339}
340
341#[inline]
342fn c_locale_is_codeunit(c: u32) -> bool {
343    c.wrapping_sub(0xDF80) < 0x80
344}
345
346#[no_mangle]
347pub extern "C" fn btowc(c: c_int) -> wint_t {
348    if c == -1 { return WEOF; }
349    unsafe {
350        if LOCALE_CTYPE_UTF8 {
351            if (c as u32) < 128 { c as wint_t } else { WEOF }
352        } else {
353            c_locale_codeunit(c as u8) as wint_t
354        }
355    }
356}
357
358#[no_mangle]
359pub extern "C" fn wctob(c: wint_t) -> c_int {
360    unsafe {
361        if LOCALE_CTYPE_UTF8 {
362            if c < 128 { c as c_int } else { -1 }
363        } else if c < 128 {
364            c as c_int
365        } else if c_locale_is_codeunit(c) {
366            (c & 0xFF) as c_int
367        } else {
368            -1
369        }
370    }
371}
372
373#[no_mangle]
374pub unsafe extern "C" fn mbsinit(s: *const c_uint) -> c_int {
375    if s.is_null() || *s == 0 { 1 } else { 0 }
376}
377
378#[no_mangle]
379pub unsafe extern "C" fn mbrlen(s: *const c_char, n: usize, ps: *mut c_uint) -> usize {
380    mbrtowc(core::ptr::null_mut(), s, n, ps)
381}
382