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