keel-libc 0.1.5

A Rust no_std musl-compatible libc implementation
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
// Copyright (c) 2026 Meng Zhuo
// SPDX-License-Identifier: BSD-3-Clause

// ============================================================
// wchar: wide string functions
// ============================================================

#[no_mangle]
pub unsafe extern "C" fn wcslen(s: *const wchar_t) -> usize {
    let mut len = 0;
    while *s.add(len) != 0 { len += 1; }
    len
}

#[no_mangle]
pub unsafe extern "C" fn wcschr(s: *const wchar_t, c: wchar_t) -> *mut wchar_t {
    let mut i = 0;
    loop {
        let ch = *s.add(i);
        if ch == c { return s.add(i) as *mut wchar_t; }
        if ch == 0 { return null_mut(); }
        i += 1;
    }
}

#[no_mangle]
pub unsafe extern "C" fn wcsrchr(s: *const wchar_t, c: wchar_t) -> *mut wchar_t {
    let mut i = 0;
    let mut last: *mut wchar_t = null_mut();
    loop {
        let ch = *s.add(i);
        if ch == c { last = s.add(i) as *mut wchar_t; }
        if ch == 0 { return last; }
        i += 1;
    }
}

#[no_mangle]
pub unsafe extern "C" fn wcsstr(haystack: *const wchar_t, needle: *const wchar_t) -> *mut wchar_t {
    if *needle == 0 { return haystack as *mut wchar_t; }
    let mut i = 0;
    loop {
        let h = *haystack.add(i);
        if h == 0 { return null_mut(); }
        if h == *needle {
            let mut j = 0;
            loop {
                let n = *needle.add(j);
                if n == 0 { return haystack.add(i) as *mut wchar_t; }
                if *haystack.add(i + j) != n { break; }
                j += 1;
            }
        }
        i += 1;
    }
}

#[no_mangle]
pub unsafe extern "C" fn wcscmp(s1: *const wchar_t, s2: *const wchar_t) -> c_int {
    let mut i = 0;
    loop {
        let a = *s1.add(i);
        let b = *s2.add(i);
        if a != b { return a - b; }
        if a == 0 { return 0; }
        i += 1;
    }
}

#[no_mangle]
pub unsafe extern "C" fn wcsncmp(s1: *const wchar_t, s2: *const wchar_t, n: usize) -> c_int {
    let mut i = 0;
    while i < n {
        let a = *s1.add(i);
        let b = *s2.add(i);
        if a != b { return a - b; }
        if a == 0 { return 0; }
        i += 1;
    }
    0
}

#[no_mangle]
pub unsafe extern "C" fn wcscpy(dst: *mut wchar_t, src: *const wchar_t) -> *mut wchar_t {
    let mut i = 0;
    loop {
        let c = *src.add(i);
        *dst.add(i) = c;
        if c == 0 { break; }
        i += 1;
    }
    dst
}

#[no_mangle]
pub unsafe extern "C" fn wcsncpy(dst: *mut wchar_t, src: *const wchar_t, n: usize) -> *mut wchar_t {
    let mut i = 0;
    while i < n && *src.add(i) != 0 { *dst.add(i) = *src.add(i); i += 1; }
    while i < n { *dst.add(i) = 0; i += 1; }
    dst
}

#[no_mangle]
pub unsafe extern "C" fn wcscat(dst: *mut wchar_t, src: *const wchar_t) -> *mut wchar_t {
    let mut i = 0;
    while *dst.add(i) != 0 { i += 1; }
    let mut j = 0;
    loop {
        let c = *src.add(j);
        *dst.add(i + j) = c;
        if c == 0 { break; }
        j += 1;
    }
    dst
}

#[no_mangle]
pub unsafe extern "C" fn wcsncat(dst: *mut wchar_t, src: *const wchar_t, n: usize) -> *mut wchar_t {
    let mut i = 0;
    while *dst.add(i) != 0 { i += 1; }
    let mut j = 0;
    while j < n && *src.add(j) != 0 { *dst.add(i + j) = *src.add(j); j += 1; }
    *dst.add(i + j) = 0;
    dst
}

#[no_mangle]
pub unsafe extern "C" fn wcsdup(s: *const wchar_t) -> *mut wchar_t {
    let len = wcslen(s);
    let p = malloc((len + 1) * core::mem::size_of::<wchar_t>()) as *mut wchar_t;
    if p.is_null() { return null_mut(); }
    core::ptr::copy_nonoverlapping(s, p, len + 1);
    p
}

#[no_mangle]
pub unsafe extern "C" fn wcsnlen(s: *const wchar_t, maxlen: usize) -> usize {
    let mut i = 0;
    while i < maxlen && *s.add(i) != 0 { i += 1; }
    i
}

#[no_mangle]
pub unsafe extern "C" fn wcsspn(s: *const wchar_t, accept: *const wchar_t) -> usize {
    let mut i = 0;
    loop {
        let c = *s.add(i);
        if c == 0 { return i; }
        let mut found = false;
        let mut j = 0;
        loop {
            let a = *accept.add(j);
            if a == 0 { break; }
            if c == a { found = true; break; }
            j += 1;
        }
        if !found { return i; }
        i += 1;
    }
}

#[no_mangle]
pub unsafe extern "C" fn wcscspn(s: *const wchar_t, reject: *const wchar_t) -> usize {
    let mut i = 0;
    loop {
        let c = *s.add(i);
        if c == 0 { return i; }
        let mut j = 0;
        loop {
            let r = *reject.add(j);
            if r == 0 { break; }
            if c == r { return i; }
            j += 1;
        }
        i += 1;
    }
}

#[no_mangle]
pub unsafe extern "C" fn wcspbrk(s: *const wchar_t, accept: *const wchar_t) -> *mut wchar_t {
    let mut i = 0;
    loop {
        let c = *s.add(i);
        if c == 0 { return null_mut(); }
        let mut j = 0;
        loop {
            let a = *accept.add(j);
            if a == 0 { break; }
            if c == a { return s.add(i) as *mut wchar_t; }
            j += 1;
        }
        i += 1;
    }
}

#[no_mangle]
pub unsafe extern "C" fn wcsxfrm(dst: *mut wchar_t, src: *const wchar_t, n: usize) -> usize {
    let len = wcslen(src);
    if !dst.is_null() {
        let copy = if len < n { len } else { if n > 0 { n - 1 } else { 0 } };
        for i in 0..copy { *dst.add(i) = *src.add(i); }
        if n > 0 { *dst.add(copy) = 0; }
    }
    len
}

// ============================================================
// wchar: wcsto* number conversions
// ============================================================

unsafe fn wcs_to_bytes(ws: *const wchar_t, buf: *mut u8, bufsz: usize) -> usize {
    let mut i = 0;
    while i < bufsz - 1 {
        let c = *ws.add(i);
        if c == 0 { break; }
        if (c as u32) > 127 { break; }
        *buf.add(i) = c as u8;
        i += 1;
    }
    *buf.add(i) = 0;
    i
}

macro_rules! wcsto_int {
    ($name:ident, $ret:ty, $conv:ident) => {
        #[no_mangle]
        pub unsafe extern "C" fn $name(
            s: *const wchar_t,
            endptr: *mut *mut wchar_t,
            base: c_int,
        ) -> $ret {
            let cap = wcslen(s).saturating_add(1);
            let heap = cap > 256;
            let mut stack = [0u8; 256];
            let buf = if heap {
                crate::malloc(cap) as *mut u8
            } else {
                stack.as_mut_ptr()
            };
            if buf.is_null() {
                ERRNO!() = ENOMEM;
                return 0;
            }
            wcs_to_bytes(s, buf, cap);
            let mut end: *mut c_char = core::ptr::null_mut();
            let r = $conv(buf as *const c_char, &mut end, base);
            if !endptr.is_null() {
                let offset = end as usize - buf as usize;
                *endptr = s.add(offset) as *mut wchar_t;
            }
            if heap {
                crate::free(buf as *mut c_void);
            }
            r
        }
    };
}

wcsto_int!(wcstol, c_long, strtol);
wcsto_int!(wcstoul, c_ulong, strtoul);
wcsto_int!(wcstoll, c_longlong, strtoll);
wcsto_int!(wcstoull, c_ulonglong, strtoull);

#[no_mangle]
pub unsafe extern "C" fn wcstod(s: *const wchar_t, endptr: *mut *mut wchar_t) -> f64 {
    let cap = wcslen(s).saturating_add(1);
    let heap = cap > 256;
    let mut stack = [0u8; 256];
    let buf = if heap {
        crate::malloc(cap) as *mut u8
    } else {
        stack.as_mut_ptr()
    };
    if buf.is_null() {
        ERRNO!() = ENOMEM;
        return 0.0;
    }
    wcs_to_bytes(s, buf, cap);
    let mut end: *mut c_char = core::ptr::null_mut();
    let r = strtod(buf as *const c_char, &mut end);
    if !endptr.is_null() {
        let offset = end as usize - buf as usize;
        *endptr = s.add(offset) as *mut wchar_t;
    }
    if heap {
        crate::free(buf as *mut c_void);
    }
    r
}

#[no_mangle]
pub unsafe extern "C" fn wcstof(s: *const wchar_t, endptr: *mut *mut wchar_t) -> f32 { wcstod(s, endptr) as f32 }

#[no_mangle]
#[cfg(target_arch = "x86_64")]
pub unsafe extern "C" fn wcstold(s: *const wchar_t, endptr: *mut *mut wchar_t) -> f64 { wcstod(s, endptr) }

#[no_mangle]
#[cfg(target_arch = "aarch64")]
pub unsafe extern "C" fn wcstold(s: *const wchar_t, endptr: *mut *mut wchar_t) -> f128 { f64_to_f128(wcstod(s, endptr)) }

#[no_mangle]
#[cfg(any(target_arch = "riscv64", target_arch = "powerpc64", target_arch = "loongarch64"))]
pub unsafe extern "C" fn wcstold(s: *const wchar_t, endptr: *mut *mut wchar_t) -> f128 { f64_to_f128(wcstod(s, endptr)) }

#[no_mangle]
pub unsafe extern "C" fn wcstoimax(s: *const wchar_t, endptr: *mut *mut wchar_t, base: c_int) -> c_longlong { wcstoll(s, endptr, base) }

#[no_mangle]
pub unsafe extern "C" fn wcstoumax(s: *const wchar_t, endptr: *mut *mut wchar_t, base: c_int) -> c_ulonglong { wcstoull(s, endptr, base) }

// ============================================================
// wctype: classification functions
// ============================================================

const WC_ALPHA: c_int = 1;
const WC_DIGIT: c_int = 2;
const WC_SPACE: c_int = 4;
const WC_UPPER: c_int = 8;
const WC_LOWER: c_int = 16;
const WC_ALNUM: c_int = WC_ALPHA | WC_DIGIT;
const WC_BLANK: c_int = 32;
const WC_CNTRL: c_int = 64;
const WC_PUNCT: c_int = 128;
const WC_GRAPH: c_int = WC_ALNUM | WC_PUNCT;
const WC_PRINT: c_int = WC_GRAPH | WC_BLANK;
const WC_XDIGIT: c_int = 256;

#[inline]
fn wctable_bit(table: &[u8], wc: wint_t) -> bool {
    let wc = wc as u32;
    let page = table[(wc >> 8) as usize] as usize;
    (table[page * 32 + ((wc & 255) >> 3) as usize] >> (wc & 7)) & 1 != 0
}

/// Port of musl's iswalpha: table lookup below U+20000, then a broad
/// alphabetic range up to U+2FFFD.
#[no_mangle]
pub extern "C" fn iswalpha(c: wint_t) -> c_int {
    let wc = c as u32;
    if wc < 0x20000 {
        return wctable_bit(&WCTYPE_ALPHA_TABLE, c) as c_int;
    }
    if wc < 0x2fffe { 1 } else { 0 }
}

/// Port of musl's iswpunct.
#[no_mangle]
pub extern "C" fn iswpunct(c: wint_t) -> c_int {
    let wc = c as u32;
    if wc < 0x20000 {
        wctable_bit(&WCTYPE_PUNCT_TABLE, c) as c_int
    } else {
        0
    }
}

/// Port of musl's iswprint.
#[no_mangle]
pub extern "C" fn iswprint(c: wint_t) -> c_int {
    let wc = c as u32;
    if wc < 0xff {
        return (((wc + 1) & 0x7f) >= 0x21) as c_int;
    }
    if wc < 0x2028
        || wc.wrapping_sub(0x202a) < 0xd800 - 0x202a
        || wc.wrapping_sub(0xe000) < 0xfff9 - 0xe000
    {
        return 1;
    }
    if wc.wrapping_sub(0xfffc) > 0x10ffff - 0xfffc || (wc & 0xfffe) == 0xfffe {
        return 0;
    }
    1
}

/// Port of musl's iswspace: Unicode White_Space minus U+00A0/U+2007/U+202F,
/// plus U+1680 and U+180E are excluded (they have non-blank glyphs).
#[no_mangle]
pub extern "C" fn iswspace(c: wint_t) -> c_int {
    matches!(
        c as u32,
        0x20 | 0x09 | 0x0a | 0x0d | 0x0b | 0x0c | 0x85
            | 0x2000..=0x2006 | 0x2008..=0x200a
            | 0x2028 | 0x2029 | 0x205f | 0x3000
    ) as c_int
}

/// Port of musl's iswcntrl.
#[no_mangle]
pub extern "C" fn iswcntrl(c: wint_t) -> c_int {
    let wc = c as u32;
    ((wc < 32)
        || wc.wrapping_sub(0x7f) < 33
        || wc.wrapping_sub(0x2028) < 2
        || wc.wrapping_sub(0xfff9) < 3) as c_int
}

#[no_mangle]
pub extern "C" fn iswdigit(c: wint_t) -> c_int {
    ((c as u32).wrapping_sub(b'0' as u32) < 10) as c_int
}

#[no_mangle]
pub extern "C" fn iswxdigit(c: wint_t) -> c_int {
    let wc = c as u32;
    ((wc.wrapping_sub(b'0' as u32) < 10) || ((wc | 32).wrapping_sub(b'a' as u32) < 6)) as c_int
}

#[no_mangle]
pub extern "C" fn iswalnum(c: wint_t) -> c_int {
    (iswdigit(c) != 0 || iswalpha(c) != 0) as c_int
}

#[no_mangle]
pub extern "C" fn iswblank(c: wint_t) -> c_int {
    (c == b' ' as wint_t || c == b'\t' as wint_t) as c_int
}

#[no_mangle]
pub extern "C" fn iswgraph(c: wint_t) -> c_int {
    (iswspace(c) == 0 && iswprint(c) != 0) as c_int
}

#[no_mangle]
pub extern "C" fn iswlower(c: wint_t) -> c_int {
    (unsafe { towupper(c) } != c) as c_int
}

#[no_mangle]
pub extern "C" fn iswupper(c: wint_t) -> c_int {
    (unsafe { towlower(c) } != c) as c_int
}

static WCTYPE_ALNUM: c_int = WC_ALNUM;
static WCTYPE_ALPHA: c_int = WC_ALPHA;
static WCTYPE_BLANK: c_int = WC_BLANK;
static WCTYPE_CNTRL: c_int = WC_CNTRL;
static WCTYPE_DIGIT: c_int = WC_DIGIT;
static WCTYPE_GRAPH: c_int = WC_GRAPH;
static WCTYPE_LOWER: c_int = WC_LOWER;
static WCTYPE_PRINT: c_int = WC_PRINT;
static WCTYPE_PUNCT: c_int = WC_PUNCT;
static WCTYPE_SPACE: c_int = WC_SPACE;
static WCTYPE_UPPER: c_int = WC_UPPER;
static WCTYPE_XDIGIT: c_int = WC_XDIGIT;

#[no_mangle]
pub unsafe extern "C" fn wctype(name: *const c_char) -> wctype_t {
    if name.is_null() { return core::ptr::null(); }
    let n = name as *const u8;
    if strcmp(n, b"alnum\0".as_ptr()) == 0 { return &WCTYPE_ALNUM; }
    if strcmp(n, b"alpha\0".as_ptr()) == 0 { return &WCTYPE_ALPHA; }
    if strcmp(n, b"blank\0".as_ptr()) == 0 { return &WCTYPE_BLANK; }
    if strcmp(n, b"cntrl\0".as_ptr()) == 0 { return &WCTYPE_CNTRL; }
    if strcmp(n, b"digit\0".as_ptr()) == 0 { return &WCTYPE_DIGIT; }
    if strcmp(n, b"graph\0".as_ptr()) == 0 { return &WCTYPE_GRAPH; }
    if strcmp(n, b"lower\0".as_ptr()) == 0 { return &WCTYPE_LOWER; }
    if strcmp(n, b"print\0".as_ptr()) == 0 { return &WCTYPE_PRINT; }
    if strcmp(n, b"punct\0".as_ptr()) == 0 { return &WCTYPE_PUNCT; }
    if strcmp(n, b"space\0".as_ptr()) == 0 { return &WCTYPE_SPACE; }
    if strcmp(n, b"upper\0".as_ptr()) == 0 { return &WCTYPE_UPPER; }
    if strcmp(n, b"xdigit\0".as_ptr()) == 0 { return &WCTYPE_XDIGIT; }
    core::ptr::null()
}

#[no_mangle]
pub unsafe extern "C" fn iswctype(c: wint_t, desc: wctype_t) -> c_int {
    if desc.is_null() { return 0; }
    if desc == core::ptr::addr_of!(WCTYPE_ALNUM) { return iswalnum(c); }
    if desc == core::ptr::addr_of!(WCTYPE_ALPHA) { return iswalpha(c); }
    if desc == core::ptr::addr_of!(WCTYPE_BLANK) { return iswblank(c); }
    if desc == core::ptr::addr_of!(WCTYPE_CNTRL) { return iswcntrl(c); }
    if desc == core::ptr::addr_of!(WCTYPE_DIGIT) { return iswdigit(c); }
    if desc == core::ptr::addr_of!(WCTYPE_GRAPH) { return iswgraph(c); }
    if desc == core::ptr::addr_of!(WCTYPE_LOWER) { return iswlower(c); }
    if desc == core::ptr::addr_of!(WCTYPE_PRINT) { return iswprint(c); }
    if desc == core::ptr::addr_of!(WCTYPE_PUNCT) { return iswpunct(c); }
    if desc == core::ptr::addr_of!(WCTYPE_SPACE) { return iswspace(c); }
    if desc == core::ptr::addr_of!(WCTYPE_UPPER) { return iswupper(c); }
    if desc == core::ptr::addr_of!(WCTYPE_XDIGIT) { return iswxdigit(c); }
    0
}

static WCTRANS_LOWER: c_int = 1;
static WCTRANS_UPPER: c_int = 2;

/// Port of musl's casemap() from ctype/towctrans.c.
fn casemap(c: wint_t, dir: i32) -> wint_t {
    let c0 = c;
    if c0 >= 0x20000 {
        return c0;
    }
    let b = (c0 >> 8) as usize;
    let cc = c0 & 255;
    let x = (cc / 3) as usize;
    let y = (cc % 3) as usize;
    const MT: [u32; 3] = [2048, 342, 57];
    let v = WCTYPE_CASEMAP_TAB[WCTYPE_CASEMAP_TAB[b] as usize * 86 + x] as u32;
    let v = (v * MT[y] >> 11) % 6;
    let r = WCTYPE_CASEMAP_RULES[(WCTYPE_CASEMAP_RULEBASES[b] + v as u8) as usize];
    let rt = (r & 255) as i32;
    let rd = r >> 8;
    if rt < 2 {
        return (c0 as i32 + (rd & -((rt) ^ dir))) as wint_t;
    }
    let mut xn = (rd & 0xff) as u32;
    let mut xb = (rd as u32) >> 8;
    while xn != 0 {
        let idx = (xb + xn / 2) as usize;
        let try_ = WCTYPE_CASEMAP_EXCEPTIONS[idx][0] as u32;
        if try_ == cc {
            let r2 = WCTYPE_CASEMAP_RULES[WCTYPE_CASEMAP_EXCEPTIONS[idx][1] as usize];
            let rt2 = (r2 & 255) as i32;
            let rd2 = r2 >> 8;
            if rt2 < 2 {
                return (c0 as i32 + (rd2 & -((rt2) ^ dir))) as wint_t;
            }
            return (c0 as i32 + if dir != 0 { -1 } else { 1 }) as wint_t;
        } else if try_ > cc {
            xn /= 2;
        } else {
            xb += xn / 2;
            xn -= xn / 2;
        }
    }
    c0
}

#[no_mangle]
pub unsafe extern "C" fn towlower(c: wint_t) -> wint_t {
    casemap(c, 0)
}

#[no_mangle]
pub unsafe extern "C" fn towupper(c: wint_t) -> wint_t {
    casemap(c, 1)
}

#[no_mangle]
pub unsafe extern "C" fn wctrans(name: *const c_char) -> wctrans_t {
    if name.is_null() { return core::ptr::null(); }
    let n = name as *const u8;
    if strcmp(n, b"tolower\0".as_ptr()) == 0 { return &WCTRANS_LOWER; }
    if strcmp(n, b"toupper\0".as_ptr()) == 0 { return &WCTRANS_UPPER; }
    core::ptr::null()
}

#[no_mangle]
pub unsafe extern "C" fn towctrans(c: wint_t, desc: wctrans_t) -> wint_t {
    if desc.is_null() { return c; }
    match *desc {
        1 => towlower(c),
        2 => towupper(c),
        _ => c,
    }
}