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