From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on inbox.vuxu.org X-Spam-Level: X-Spam-Status: No, score=-0.8 required=5.0 tests=DKIM_ADSP_CUSTOM_MED, DKIM_INVALID,DKIM_SIGNED,FREEMAIL_FROM,MAILING_LIST_MULTI, RCVD_IN_MSPIKE_H2 autolearn=ham autolearn_force=no version=3.4.4 Received: (qmail 7686 invoked from network); 15 Apr 2023 12:29:16 -0000 Received: from second.openwall.net (193.110.157.125) by inbox.vuxu.org with ESMTPUTF8; 15 Apr 2023 12:29:16 -0000 Received: (qmail 7276 invoked by uid 550); 15 Apr 2023 12:29:14 -0000 Mailing-List: contact musl-help@lists.openwall.com; run by ezmlm Precedence: bulk List-Post: List-Help: List-Unsubscribe: List-Subscribe: List-ID: Reply-To: musl@lists.openwall.com Received: (qmail 7237 invoked from network); 15 Apr 2023 12:29:13 -0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20221208; t=1681561741; x=1684153741; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=+0KkN8JWhnFqQ8KbN7zf/KbD9uVCvkx0Hvyg8i5/YoE=; b=eZtgtlp+Tnc+tin1HynaSDzlrpXefjOVODB724KbJahUqNWGVqO0dvbw0ts9H3HxRL BgxrhoeO6oQdJKIQU301/SCmUs09wlGFNI2OZgOPvWHBGmhmbYtZRHVn3YkolsAQBLjO ERylqU93LsI6dnYuH500if6PuS1zMwPgWgNDoKcyP+RHDf/rccUEaHDy4Wh3WbdO1LiE yi664SuuU+A3u8sFDTlVvIAUF1IsXQd2AfOIXWZVv1CHxI/l+bIjRcVsG2ZrCuzPKXt4 IBdW2y9GLzaWWaNyIAK7F8C7DZZWPBvXwdTKKfefm60uQ0iCVUF3t51cFS1U5nypxAob 537A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1681561741; x=1684153741; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=+0KkN8JWhnFqQ8KbN7zf/KbD9uVCvkx0Hvyg8i5/YoE=; b=dsWve/JyZj62RWc2jGV8U6uv6nqv+pOIn17t88+qj5YMv0xVoP2/7O80VtHtjb4TMY vfze2RcbS/voNI/FYi/zaq92/2ekxXl2UvgmnGCeg2+OhdeTRaNMX6DQyegJ3Ld3gsSr uVxa+KtifEIWX3kKLtBTIIXgO0g/sRs4hkuAmxfb3pcsqWAGr0UMOi7+5QVQxY1Q7T+d dIiKu6795ToQYS7UYFLjtABsCcw0L+cm2+j7Lq5c0HuSxxsrnwek2EEGHNskGJQXPfy/ HwwcIR5x4JvssdiN+p00kahpSJeuGxXtZAAaMcaHUTEOD4WV/4FPdtUYR393G4Qe2O3z EFYg== X-Gm-Message-State: AAQBX9dfh3BWOW3bdCL760ZLzCpE8UxWJSSBLJ4TMkmJh2q5YRPheMZ+ gbA/lSodVaO6yjioMg73wOroiTCmRLLm9A== X-Google-Smtp-Source: AKy350botwADq/bNJJkEhGT4LkZgm3nFK+ETcTVlHVbxuXSEB21nVqaW+emThX9O58GPaBbfF7kYXw== X-Received: by 2002:a1c:7314:0:b0:3f1:4971:5cd1 with SMTP id d20-20020a1c7314000000b003f149715cd1mr3199217wmb.21.1681561741460; Sat, 15 Apr 2023 05:29:01 -0700 (PDT) From: Gabriel Ravier To: musl@lists.openwall.com Cc: Gabriel Ravier Date: Sat, 15 Apr 2023 14:28:28 +0200 Message-Id: X-Mailer: git-send-email 2.39.2 In-Reply-To: References: <20220908163649.634728-1-gabravier@gmail.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Subject: [musl] [PATCH v2 1/1] vfprintf: support C2x %b and %B conversion specifiers These specifiers allow for formatted input/output of binary integers, and have been added to C2x with N2630. The uppercase B specifier is not formally required by C2x, as only lowercase specifiers were reserved by C, and thus an implementation could have been using uppercase B for an extension of their own, but C2x still has a note saying that it is recommended practice to implement it as the logical counterpart to b. --- src/stdio/vfprintf.c | 28 ++++++++++++++++++++++++---- 1 file changed, 24 insertions(+), 4 deletions(-) diff --git a/src/stdio/vfprintf.c b/src/stdio/vfprintf.c index a712d80f..3d7e0aeb 100644 --- a/src/stdio/vfprintf.c +++ b/src/stdio/vfprintf.c @@ -49,7 +49,7 @@ enum { static const unsigned char states[]['z'-'A'+1] = { { /* 0: bare types */ S('d') = INT, S('i') = INT, - S('o') = UINT, S('u') = UINT, S('x') = UINT, S('X') = UINT, + S('o') = UINT, S('u') = UINT, S('x') = UINT, S('X') = UINT, S('b') = UINT, S('B') = UINT, S('e') = DBL, S('f') = DBL, S('g') = DBL, S('a') = DBL, S('E') = DBL, S('F') = DBL, S('G') = DBL, S('A') = DBL, S('c') = CHAR, S('C') = INT, @@ -59,7 +59,7 @@ static const unsigned char states[]['z'-'A'+1] = { S('z') = ZTPRE, S('j') = JPRE, S('t') = ZTPRE, }, { /* 1: l-prefixed */ S('d') = LONG, S('i') = LONG, - S('o') = ULONG, S('u') = ULONG, S('x') = ULONG, S('X') = ULONG, + S('o') = ULONG, S('u') = ULONG, S('x') = ULONG, S('X') = ULONG, S('b') = ULONG, S('B') = ULONG, S('e') = DBL, S('f') = DBL, S('g') = DBL, S('a') = DBL, S('E') = DBL, S('F') = DBL, S('G') = DBL, S('A') = DBL, S('c') = INT, S('s') = PTR, S('n') = PTR, @@ -68,17 +68,20 @@ static const unsigned char states[]['z'-'A'+1] = { S('d') = LLONG, S('i') = LLONG, S('o') = ULLONG, S('u') = ULLONG, S('x') = ULLONG, S('X') = ULLONG, + S('b') = ULLONG, S('B') = ULLONG, S('n') = PTR, }, { /* 3: h-prefixed */ S('d') = SHORT, S('i') = SHORT, S('o') = USHORT, S('u') = USHORT, S('x') = USHORT, S('X') = USHORT, + S('b') = USHORT, S('B') = USHORT, S('n') = PTR, S('h') = HHPRE, }, { /* 4: hh-prefixed */ S('d') = CHAR, S('i') = CHAR, S('o') = UCHAR, S('u') = UCHAR, S('x') = UCHAR, S('X') = UCHAR, + S('b') = UCHAR, S('B') = UCHAR, S('n') = PTR, }, { /* 5: L-prefixed */ S('e') = LDBL, S('f') = LDBL, S('g') = LDBL, S('a') = LDBL, @@ -88,11 +91,13 @@ static const unsigned char states[]['z'-'A'+1] = { S('d') = PDIFF, S('i') = PDIFF, S('o') = SIZET, S('u') = SIZET, S('x') = SIZET, S('X') = SIZET, + S('b') = SIZET, S('B') = SIZET, S('n') = PTR, }, { /* 7: j-prefixed */ S('d') = IMAX, S('i') = IMAX, S('o') = UMAX, S('u') = UMAX, S('x') = UMAX, S('X') = UMAX, + S('b') = UMAX, S('B') = UMAX, S('n') = PTR, } }; @@ -162,6 +167,12 @@ static char *fmt_o(uintmax_t x, char *s) return s; } +static char *fmt_b(uintmax_t x, char *s) +{ + for (; x; x>>=1) *--s = '0' + (x&1); + return s; +} + static char *fmt_u(uintmax_t x, char *s) { unsigned long y; @@ -437,7 +448,12 @@ static int printf_core(FILE *f, const char *fmt, va_list *ap, union arg *nl_arg, unsigned st, ps; int cnt=0, l=0; size_t i; - char buf[sizeof(uintmax_t)*3+3+LDBL_MANT_DIG/4]; + /* This buffer is used for integer conversions. As such, it needs + * to be able to contain the full representation of a number in base 2, + * 8, 10 or 16, with base 2 having the largest possible requirement of + * as many characters as the amount of bits in the largest possible + * integer type */ + char buf[sizeof(uintmax_t)*CHAR_BIT]; const char *prefix; int t, pl; wchar_t wc[2], *ws; @@ -534,7 +550,7 @@ static int printf_core(FILE *f, const char *fmt, va_list *ap, union arg *nl_arg, if (ferror(f)) return -1; z = buf + sizeof(buf); - prefix = "-+ 0X0x"; + prefix = "-+ 0X0x0B0b"; pl = 0; t = s[-1]; @@ -564,6 +580,10 @@ static int printf_core(FILE *f, const char *fmt, va_list *ap, union arg *nl_arg, a = fmt_x(arg.i, z, t&32); if (arg.i && (fl & ALT_FORM)) prefix+=(t>>4), pl=2; if (0) { + case 'b': case 'B': + a = fmt_b(arg.i, z); + if (arg.i && (fl & ALT_FORM)) prefix+=9+((t=='b')<<1), pl=2; + } if (0) { case 'o': a = fmt_o(arg.i, z); if ((fl&ALT_FORM) && p