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=-3.1 required=5.0 tests=DKIM_ADSP_CUSTOM_MED, DKIM_INVALID,DKIM_SIGNED,FREEMAIL_FROM,MAILING_LIST_MULTI, RCVD_IN_DNSWL_MED,RCVD_IN_MSPIKE_H3,RCVD_IN_MSPIKE_WL, T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.4 Received: (qmail 3525 invoked from network); 8 Sep 2022 19:26:29 -0000 Received: from mother.openwall.net (HELO second.openwall.net) (195.42.179.200) by inbox.vuxu.org with ESMTPUTF8; 8 Sep 2022 19:26:29 -0000 Received: (qmail 25893 invoked by uid 550); 8 Sep 2022 16:37:48 -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 25858 invoked from network); 8 Sep 2022 16:37:48 -0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date; bh=1AKC6UJr5GcUlp7PLOHiLjYAgB7Q2nKlMkDvqeulcek=; b=F2MdNDn78OcpKBOhGWaWExmSRZLJ4rvDgCNYWu0DPJyJuhqLuzXn9RnDKM5VwIqQwZ okDNxvzluKCQl5ZwDa+TOeuTVRy0+WdWQB3uiSNJ9JSNYvS7HPVRTkJzv59CTq8OnxBS Tp1FSoA/zlxKokC4lgz4FmyJXBtFOoT11qiXStn4Q6LUa2BeIzAA10BqHJ+H0k2fTYte EVfuOAWvywg/rwn3wnGkHk8gwJdQ+SzzimcQgzeZEspQamQ/7q4W0M2VBCKdk9jfSeLC pehZ4T+KUacFYRmasZaLPU82cgHAy3kM0vXsDh5RirUwO4oq6CGtBAoxujhwbjqN3nOt Lc4w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-message-state:from:to:cc:subject:date; bh=1AKC6UJr5GcUlp7PLOHiLjYAgB7Q2nKlMkDvqeulcek=; b=ElYy5KQFJQRkl0VsCG7h1Wxr+jIyUISsAQ5qCltugyf4vDdk8vOUivWRJOidqUZnSF ouhLg4n3cay4hznL9ezyHUDI8yZdKLWT75aExAnw2q2eYcQgGN5gUOSiK93QVp92L6qg DziNLK+BxPcnSyYIsQAWfTvGPf1hcQGSLfOELpuZVLkzH8iFg0CpgRIEPgUvrZKg3ByL YdoaTC2nkKOsbMgomsM83ECjt9IwoulskZgM7pdXfEBEU9L+/B6jMKhRR1rGnPbuneJI XMAlTTin4LA3RQaHJQOABxtUHYWVsPK46IncFsMZjEXEDgKEjUEAskxrRFsDhUVDDnJs KueQ== X-Gm-Message-State: ACgBeo1xxVLIAn3wSwWWIaRb1TwKyBuiLoepEf/e8PAXocI/vUnh7GQt DUGy2mZqMD3qSbCdKwTy/N6M1qeVjhmZtw== X-Google-Smtp-Source: AA6agR7euzw0KgqLoyOOdAuMTKv5PAi4tAARy3mnwDLjnbLihw/5yZc1u0ciBAcQ/J0R3SRRhaI5VQ== X-Received: by 2002:a1c:f217:0:b0:3a6:61f2:a9c2 with SMTP id s23-20020a1cf217000000b003a661f2a9c2mr2859205wmc.88.1662655018601; Thu, 08 Sep 2022 09:36:58 -0700 (PDT) From: Gabriel Ravier To: gravier@gmail.com, musl@lists.openwall.com Cc: Gabriel Ravier Date: Thu, 8 Sep 2022 18:36:49 +0200 Message-Id: <20220908163649.634728-1-gabravier@gmail.com> X-Mailer: git-send-email 2.37.3 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Subject: [musl] [PATCH] 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. I have tested this on: - glibc's tests for %b and %B - The libc testsuite I'm developing over at https://github.com/GabrielRavier/yalibct - musl's libc-test - musl's libc-testsuite and observed no regressions. --- src/stdio/vfprintf.c | 21 ++++++++++++++++++--- 1 file changed, 18 insertions(+), 3 deletions(-) diff --git a/src/stdio/vfprintf.c b/src/stdio/vfprintf.c index 9b961e7f..89426b72 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; @@ -529,7 +540,7 @@ static int printf_core(FILE *f, const char *fmt, va_list *ap, union arg *nl_arg, if (!f) continue; z = buf + sizeof(buf); - prefix = "-+ 0X0x"; + prefix = "-+ 0X0x0B0b"; pl = 0; t = s[-1]; @@ -559,6 +570,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