1- From b4160476f4d1f02e0ea4550b66c9f9b478ab55fa Mon Sep 17 00:00:00 2001
1+ From 10e4288b75bcaa72e84cabe7af331cb95cbb1c86 Mon Sep 17 00:00:00 2001
22From: Matt Taylor <mstaveleytaylor@gmail.com>
33Date: Fri, 17 Jun 2022 15:28:34 +0100
44Subject: [PATCH] abi-bits: add domainname to utsname
55
66---
7- ABI_BREAKS.md | 1 +
8- abis/linux/utsname.h | 1 +
9- options/glibc/generic/execinfo.cpp | 6 ++++--
10- 3 files changed, 6 insertions(+), 2 deletions(-)
7+ ABI_BREAKS.md | 1 +
8+ abis/linux/utsname.h | 1 +
9+ options/ansi/generic/stdlib-stubs.cpp | 100 ++++++++++++++++++++++----
10+ options/glibc/generic/execinfo.cpp | 6 +-
11+ 4 files changed, 94 insertions(+), 14 deletions(-)
1112
1213diff --git a/ABI_BREAKS.md b/ABI_BREAKS.md
1314index 0cd3993b..d2a0bb7d 100644
@@ -30,6 +31,117 @@ index 2cd22265..9875a46e 100644
3031 };
3132
3233 #endif // _ABIBITS_UTSNAME_T_H
34+ diff --git a/options/ansi/generic/stdlib-stubs.cpp b/options/ansi/generic/stdlib-stubs.cpp
35+ index 375d4d72..2b4934d9 100644
36+ --- a/options/ansi/generic/stdlib-stubs.cpp
37+ +++ b/options/ansi/generic/stdlib-stubs.cpp
38+ @@ -374,18 +374,94 @@ int mblen(const char *mbs, size_t mb_limit) {
39+ return nseq.it - mbs;
40+ }
41+
42+ - int mbtowc(wchar_t *__restrict wc, const char *__restrict mbs, size_t max_size) {
43+ - mlibc::infoLogger() << "mlibc: Broken mbtowc() called" << frg::endlog;
44+ - __ensure(max_size);
45+ -
46+ - if(wc && mbs){
47+ - if(*mbs){
48+ - *wc = *mbs;
49+ - } else {
50+ - return 0; // When mbs is a null byte, return 0
51+ - }
52+ - }
53+ - return 1;
54+ + // Upper 6 state bits are a negative integer offset to bound-check next byte
55+ + // equivalent to: ( (b-0x80) | (b+offset) ) & ~0x3f
56+ + #define OOB(c, b) (((((b) >> 3) - 0x10) | (((b) >> 3) + ((int32_t)(c) >> 26))) & ~7)
57+ +
58+ + // Interval [a,b). Either a must be 80 or b must be c0, lower 3 bits clear.
59+ + #define R(a, b) ((uint32_t)((a == 0x80 ? 0x40u - b : 0u - a) << 23))
60+ + #define FAILSTATE R(0x80, 0x80)
61+ +
62+ + #define SA 0xc2u
63+ + #define SB 0xf4u
64+ +
65+ + // Arbitrary encoding for representing code units instead of characters.
66+ + #define CODEUNIT(c) (0xdfff & (signed char)(c))
67+ + #define IS_CODEUNIT(c) ((unsigned)(c)-0xdf80 < 0x80)
68+ +
69+ + #define C(x) ( x<2 ? -1 : ( R(0x80,0xc0) | x ) )
70+ + #define D(x) C((x+16))
71+ + #define E(x) ( ( x==0 ? R(0xa0,0xc0) : \
72+ + x==0xd ? R(0x80,0xa0) : \
73+ + R(0x80,0xc0) ) \
74+ + | ( R(0x80,0xc0) >> 6 ) \
75+ + | x )
76+ + #define F(x) ( ( x>=5 ? 0 : \
77+ + x==0 ? R(0x90,0xc0) : \
78+ + x==4 ? R(0x80,0xa0) : \
79+ + R(0x80,0xc0) ) \
80+ + | ( R(0x80,0xc0) >> 6 ) \
81+ + | ( R(0x80,0xc0) >> 12 ) \
82+ + | x )
83+ +
84+ + const uint32_t bittab[] = {
85+ + C(0x2),C(0x3),C(0x4),C(0x5),C(0x6),C(0x7),
86+ + C(0x8),C(0x9),C(0xa),C(0xb),C(0xc),C(0xd),C(0xe),C(0xf),
87+ + D(0x0),D(0x1),D(0x2),D(0x3),D(0x4),D(0x5),D(0x6),D(0x7),
88+ + D(0x8),D(0x9),D(0xa),D(0xb),D(0xc),D(0xd),D(0xe),D(0xf),
89+ + E(0x0),E(0x1),E(0x2),E(0x3),E(0x4),E(0x5),E(0x6),E(0x7),
90+ + E(0x8),E(0x9),E(0xa),E(0xb),E(0xc),E(0xd),E(0xe),E(0xf),
91+ + F(0x0),F(0x1),F(0x2),F(0x3),F(0x4)
92+ + };
93+ +
94+ + // Converts a multibyte sequence to a wide character.
95+ + //
96+ + // Credits - MUSL
97+ + int mbtowc(wchar_t *__restrict wc, const char *__restrict src, size_t n) {
98+ + unsigned c;
99+ + const unsigned char *s = static_cast<const unsigned char *>((const void *)src);
100+ + wchar_t dummy;
101+ +
102+ + if (!s) return 0;
103+ + if (!n) goto ilseq;
104+ + if (!wc) wc = &dummy;
105+ +
106+ + if (*s < 0x80) return !!(*wc = *s);
107+ + if (MB_CUR_MAX == 1) return (*wc = CODEUNIT(*s)), 1;
108+ + if (*s - SA > SB - SA) goto ilseq;
109+ +
110+ + c = bittab[*s++ - SA];
111+ +
112+ + // Avoid excessive checks against n: If shifting the state n-1
113+ + // times does not clear the high bit, then the value of n is
114+ + // insufficient to read a character.
115+ + if (n < 4 && ((c << (6 * n - 6)) & (1U << 31))) goto ilseq;
116+ + if (OOB(c, *s)) goto ilseq;
117+ +
118+ + c = c << 6 | (*s++ - 0x80);
119+ +
120+ + if (!(c & (1U << 31))) {
121+ + *wc = c;
122+ + return 2;
123+ + }
124+ +
125+ + if (*s - 0x80u >= 0x40) goto ilseq;
126+ +
127+ + c = c << 6 | (*s++ - 0x80);
128+ +
129+ + if (!(c & (1U << 31))) {
130+ + *wc = c;
131+ + return 3;
132+ + }
133+ +
134+ + if (*s - 0x80u >= 0x40) goto ilseq;
135+ +
136+ + *wc = c << 6 | (*s++ - 0x80);
137+ + return 4;
138+ +
139+ + ilseq:
140+ + errno = EILSEQ;
141+ + return -1;
142+ }
143+
144+ int wctomb(char *, wchar_t) {
33145diff --git a/options/glibc/generic/execinfo.cpp b/options/glibc/generic/execinfo.cpp
34146index 3474615e..c0081e30 100644
35147--- a/options/glibc/generic/execinfo.cpp
0 commit comments