snprintb.c revision 1.30 1 /* $NetBSD: snprintb.c,v 1.30 2024/02/16 18:17:10 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.30 2024/02/16 18:17:10 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.30 2024/02/16 18:17:10 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 line_max)
65 {
66 char *bp = buf, *sep_bp = NULL;
67 const char *num_fmt, *cur_bitfmt, *sep_bitfmt = NULL;
68 int total_len, val_len, line_len, sep_line_len = 0;
69 char sep;
70 int restart = 0;
71
72 #ifdef _KERNEL
73 /*
74 * For safety; no other *s*printf() do this, but in the kernel
75 * we don't usually check the return value
76 */
77 (void)memset(buf, 0, bufsize);
78 #endif /* _KERNEL */
79
80 int old_style = *bitfmt != '\177';
81 if (!old_style)
82 bitfmt++;
83 switch (*bitfmt++) {
84 case 8:
85 num_fmt = "%#jo";
86 break;
87 case 10:
88 num_fmt = "%ju";
89 break;
90 case 16:
91 num_fmt = "%#jx";
92 break;
93 default:
94 goto internal;
95 }
96
97 /* Reserve space for trailing blank line if needed */
98 if (line_max > 0)
99 bufsize--;
100
101 total_len = snprintf(bp, bufsize, num_fmt, (uintmax_t)val);
102 if (total_len < 0)
103 goto internal;
104
105 val_len = line_len = total_len;
106
107 if ((size_t)total_len < bufsize)
108 bp += total_len;
109 else
110 bp += bufsize - 1;
111
112 #define STORE(c) do { \
113 line_len++; \
114 if ((size_t)(++total_len) < bufsize) \
115 *bp++ = (c); \
116 } while (0)
117
118 #define BACKUP() do { \
119 if (sep_bp != NULL) { \
120 bp = sep_bp; \
121 sep_bp = NULL; \
122 total_len -= line_len - sep_line_len; \
123 restart = 1; \
124 bitfmt = sep_bitfmt; \
125 } \
126 STORE('>'); \
127 STORE('\0'); \
128 if ((size_t)total_len < bufsize) \
129 snprintf(bp, bufsize - total_len, num_fmt, \
130 (uintmax_t)val); \
131 total_len += val_len; \
132 line_len = val_len; \
133 bp += val_len; \
134 } while (0)
135
136 #define PUTSEP() do { \
137 if (line_max > 0 && (size_t)line_len >= line_max) { \
138 BACKUP(); \
139 STORE('<'); \
140 } else { \
141 /* Remember separator location */ \
142 if (line_max > 0 && sep != '<') { \
143 sep_line_len = line_len; \
144 sep_bp = bp; \
145 sep_bitfmt = cur_bitfmt; \
146 } \
147 STORE(sep); \
148 restart = 0; \
149 } \
150 } while (0)
151
152 #define PUTCHR(c) do { \
153 if (line_max > 0 && (size_t)line_len >= line_max - 1) { \
154 BACKUP(); \
155 if (restart == 0) \
156 STORE(c); \
157 else \
158 sep = '<'; \
159 } else { \
160 STORE(c); \
161 restart = 0; \
162 } \
163 } while (0)
164
165 #define PUTS(s) do { \
166 while ((*(s)++) != 0) { \
167 PUTCHR((s)[-1]); \
168 if (restart) \
169 break; \
170 } \
171 } while (0)
172
173 #define FMTSTR(sb, f) do { \
174 int fmt_len = snprintf(bp, bufsize - total_len, sb, \
175 (uintmax_t)f); \
176 if (fmt_len < 0) \
177 goto internal; \
178 total_len += fmt_len; \
179 line_len += fmt_len; \
180 if ((size_t)total_len < bufsize) \
181 bp += fmt_len; \
182 } while (0)
183
184 sep = '<';
185 if (old_style) {
186 /* old-style format, 32-bit, 1-origin. */
187 for (uint8_t bit; (bit = *bitfmt) != 0;) {
188 cur_bitfmt = bitfmt++;
189 if (val & (1U << (bit - 1))) {
190 PUTSEP();
191 if (restart)
192 continue;
193 sep = ',';
194 for (; *bitfmt > ' '; ++bitfmt) {
195 PUTCHR(*bitfmt);
196 if (restart)
197 break;
198 }
199 } else
200 for (; *bitfmt > ' '; ++bitfmt)
201 continue;
202 }
203 } else {
204 /* new-style format, 64-bit, 0-origin; also does fields. */
205 uint64_t field = val;
206 int matched = 1;
207 while (*bitfmt != '\0') {
208 uint8_t kind = *bitfmt++;
209 uint8_t bit = *bitfmt++;
210 switch (kind) {
211 case 'b':
212 if (((val >> bit) & 1) == 0)
213 goto skip;
214 cur_bitfmt = bitfmt - 2;
215 PUTSEP();
216 if (restart)
217 break;
218 PUTS(bitfmt);
219 if (restart == 0)
220 sep = ',';
221 break;
222 case 'f':
223 case 'F':
224 matched = 0;
225 cur_bitfmt = bitfmt - 2;
226 uint8_t field_width = *bitfmt++;
227 field = (val >> bit) &
228 (((uint64_t)1 << field_width) - 1);
229 PUTSEP();
230 if (restart == 0)
231 sep = ',';
232 if (kind == 'F') { /* just extract */
233 /* duplicate PUTS() effect on bitfmt */
234 while (*bitfmt++ != '\0')
235 continue;
236 break;
237 }
238 if (restart == 0)
239 PUTS(bitfmt);
240 if (restart == 0)
241 PUTCHR('=');
242 if (restart == 0) {
243 FMTSTR(num_fmt, field);
244 if (line_max > 0
245 && (size_t)line_len > line_max)
246 PUTCHR('#');
247 }
248 break;
249 case '=':
250 case ':':
251 /*
252 * Here "bit" is actually a value instead,
253 * to be compared against the last field.
254 * This only works for values in [0..255],
255 * of course.
256 */
257 if (field != bit)
258 goto skip;
259 matched = 1;
260 if (kind == '=')
261 PUTCHR('=');
262 PUTS(bitfmt);
263 break;
264 case '*':
265 bitfmt--;
266 if (!matched) {
267 matched = 1;
268 FMTSTR(bitfmt, field);
269 }
270 /*FALLTHROUGH*/
271 default:
272 skip:
273 while (*bitfmt++ != '\0')
274 continue;
275 break;
276 }
277 }
278 }
279 if (sep != '<')
280 STORE('>');
281 if (line_max > 0) {
282 bufsize++;
283 STORE('\0');
284 if ((size_t)total_len >= bufsize && bufsize > 1)
285 buf[bufsize - 2] = '\0';
286 }
287 STORE('\0');
288 if ((size_t)total_len >= bufsize && bufsize > 0)
289 buf[bufsize - 1] = '\0';
290 return total_len - 1;
291 internal:
292 #ifndef _KERNEL
293 errno = EINVAL;
294 #endif
295 return -1;
296 }
297
298 int
299 snprintb(char *buf, size_t bufsize, const char *bitfmt, uint64_t val)
300 {
301 return snprintb_m(buf, bufsize, bitfmt, val, 0);
302 }
303 # endif /* ! HAVE_SNPRINTB_M */
304 #endif /* ! _STANDALONE */
305