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