snprintb.c revision 1.26 1 /* $NetBSD: snprintb.c,v 1.26 2024/02/16 01:57:50 rillig Exp $ */
2
3 /*-
4 * Copyright (c) 2002 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
17 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
18 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
20 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 * POSSIBILITY OF SUCH DAMAGE.
27 */
28
29 /*
30 * snprintb: print an interpreted bitmask to a buffer
31 *
32 * => returns the length of the buffer that would be required to print the
33 * string minus the terminating NUL.
34 */
35 #ifndef _STANDALONE
36 # ifndef _KERNEL
37
38 # if HAVE_NBTOOL_CONFIG_H
39 # include "nbtool_config.h"
40 # endif
41
42 # include <sys/cdefs.h>
43 # if defined(LIBC_SCCS) && !defined(lint)
44 __RCSID("$NetBSD: snprintb.c,v 1.26 2024/02/16 01:57:50 rillig Exp $");
45 # endif
46
47 # include <sys/types.h>
48 # include <inttypes.h>
49 # include <stdio.h>
50 # include <util.h>
51 # include <errno.h>
52 # else /* ! _KERNEL */
53 # include <sys/cdefs.h>
54 __KERNEL_RCSID(0, "$NetBSD: snprintb.c,v 1.26 2024/02/16 01:57:50 rillig Exp $");
55 # include <sys/param.h>
56 # include <sys/inttypes.h>
57 # include <sys/systm.h>
58 # include <lib/libkern/libkern.h>
59 # endif /* ! _KERNEL */
60
61 # ifndef HAVE_SNPRINTB_M
62 int
63 snprintb_m(char *buf, size_t bufsize, const char *bitfmt, uint64_t val,
64 size_t l_max)
65 {
66 char *bp = buf, *s_bp = NULL;
67 const char *c_fmt, *s_fmt = NULL, *cur_fmt;
68 const char *sbase;
69 int bit, ch, t_len, s_len = 0, l_len, f_len, v_len, sep;
70 int restart = 0, matched = 1;
71 uint64_t field;
72
73 #ifdef _KERNEL
74 /*
75 * For safety; no other *s*printf() do this, but in the kernel
76 * we don't usually check the return value
77 */
78 (void)memset(buf, 0, bufsize);
79 #endif /* _KERNEL */
80
81 ch = *bitfmt++;
82 switch (ch != '\177' ? ch : *bitfmt++) {
83 case 8:
84 sbase = "%#jo";
85 break;
86 case 10:
87 sbase = "%ju";
88 break;
89 case 16:
90 sbase = "%#jx";
91 break;
92 default:
93 goto internal;
94 }
95
96 /* Reserve space for trailing blank line if needed */
97 if (l_max > 0)
98 bufsize--;
99
100 t_len = snprintf(bp, bufsize, sbase, (uintmax_t)val);
101 if (t_len < 0)
102 goto internal;
103
104 v_len = l_len = t_len;
105
106 if ((size_t)t_len < bufsize)
107 bp += t_len;
108 else
109 bp += bufsize - 1;
110
111 /*
112 * If the value we printed was 0 and we're using the old-style format,
113 * we're done.
114 */
115 if (val == 0 && ch != '\177')
116 goto terminate;
117
118 #define STORE(c) do { \
119 l_len++; \
120 if ((size_t)(++t_len) < bufsize) \
121 *bp++ = (c); \
122 } while (0)
123
124 #define BACKUP() do { \
125 if (s_bp != NULL) { \
126 bp = s_bp; \
127 s_bp = NULL; \
128 t_len -= l_len - s_len; \
129 restart = 1; \
130 bitfmt = s_fmt; \
131 } \
132 STORE('>'); \
133 STORE('\0'); \
134 if ((size_t)t_len < bufsize) \
135 snprintf(bp, bufsize - t_len, sbase, (uintmax_t)val);\
136 t_len += v_len; \
137 l_len = v_len; \
138 bp += v_len; \
139 } while (0)
140
141 #define PUTSEP() do { \
142 if (l_max > 0 && (size_t)l_len >= l_max) { \
143 BACKUP(); \
144 STORE('<'); \
145 } else { \
146 /* Remember separator location */ \
147 if (l_max > 0 && sep != '<') { \
148 s_len = l_len; \
149 s_bp = bp; \
150 s_fmt = cur_fmt; \
151 } \
152 STORE(sep); \
153 restart = 0; \
154 } \
155 } while (0)
156
157 #define PUTCHR(c) do { \
158 if (l_max > 0 && (size_t)l_len >= l_max - 1) { \
159 BACKUP(); \
160 if (restart == 0) \
161 STORE(c); \
162 else \
163 sep = '<'; \
164 } else { \
165 STORE(c); \
166 restart = 0; \
167 } \
168 } while (0)
169
170 #define PUTS(s) do { \
171 while ((ch = *(s)++) != 0) { \
172 PUTCHR(ch); \
173 if (restart) \
174 break; \
175 } \
176 } while (0)
177
178 #define FMTSTR(sb, f) do { \
179 f_len = snprintf(bp, bufsize - t_len, sb, (uintmax_t)f); \
180 if (f_len < 0) \
181 goto internal; \
182 t_len += f_len; \
183 l_len += f_len; \
184 if ((size_t)t_len < bufsize) \
185 bp += f_len; \
186 } while (0)
187
188 /*
189 * Chris Torek's new bitmask format is identified by a leading \177
190 */
191 sep = '<';
192 if (ch != '\177') {
193 /* old (standard) format. */
194 while ((bit = *bitfmt) != 0) {
195 cur_fmt = bitfmt++;
196 if (val & (1U << (bit - 1))) {
197 PUTSEP();
198 if (restart)
199 continue;
200 sep = ',';
201 for (; (ch = *bitfmt) > ' '; ++bitfmt) {
202 PUTCHR(ch);
203 if (restart)
204 break;
205 }
206 } else
207 for (; *bitfmt > ' '; ++bitfmt)
208 continue;
209 }
210 } else {
211 /* new quad-capable format; also does fields. */
212 field = val;
213 while (c_fmt = bitfmt, (ch = *bitfmt++) != '\0') {
214 bit = *bitfmt++; /* now 0-origin */
215 switch (ch) {
216 case 'b':
217 if (((val >> bit) & 1) == 0)
218 goto skip;
219 cur_fmt = c_fmt;
220 PUTSEP();
221 if (restart)
222 break;
223 PUTS(bitfmt);
224 if (restart == 0)
225 sep = ',';
226 break;
227 case 'f':
228 case 'F':
229 matched = 0;
230 cur_fmt = c_fmt;
231 f_len = *bitfmt++; /* field length */
232 field = (val >> bit) &
233 (((uint64_t)1 << f_len) - 1);
234 PUTSEP();
235 if (restart == 0)
236 sep = ',';
237 if (ch == 'F') { /* just extract */
238 /* duplicate PUTS() effect on bitfmt */
239 while (*bitfmt++ != '\0')
240 continue;
241 break;
242 }
243 if (restart == 0)
244 PUTS(bitfmt);
245 if (restart == 0)
246 PUTCHR('=');
247 if (restart == 0) {
248 FMTSTR(sbase, field);
249 if (l_max > 0 && (size_t)l_len > l_max)
250 PUTCHR('#');
251 }
252 break;
253 case '=':
254 case ':':
255 /*
256 * Here "bit" is actually a value instead,
257 * to be compared against the last field.
258 * This only works for values in [0..255],
259 * of course.
260 */
261 if ((int)field != bit)
262 goto skip;
263 matched = 1;
264 if (ch == '=')
265 PUTCHR('=');
266 PUTS(bitfmt);
267 break;
268 case '*':
269 bitfmt--;
270 if (!matched) {
271 matched = 1;
272 FMTSTR(bitfmt, field);
273 }
274 /*FALLTHROUGH*/
275 default:
276 skip:
277 while (*bitfmt++ != '\0')
278 continue;
279 break;
280 }
281 }
282 }
283 if (sep != '<')
284 STORE('>');
285 terminate:
286 if (l_max > 0) {
287 bufsize++;
288 STORE('\0');
289 if ((size_t)t_len >= bufsize && bufsize > 1)
290 buf[bufsize - 2] = '\0';
291 }
292 STORE('\0');
293 if ((size_t)t_len >= bufsize && bufsize > 0)
294 buf[bufsize - 1] = '\0';
295 return t_len - 1;
296 internal:
297 #ifndef _KERNEL
298 errno = EINVAL;
299 #endif
300 return -1;
301 }
302
303 int
304 snprintb(char *buf, size_t bufsize, const char *bitfmt, uint64_t val)
305 {
306 return snprintb_m(buf, bufsize, bitfmt, val, 0);
307 }
308 # endif /* ! HAVE_SNPRINTB_M */
309 #endif /* ! _STANDALONE */
310