Home | History | Annotate | Line # | Download | only in libutil
snprintb.c revision 1.32
      1  1.32    rillig /*	$NetBSD: snprintb.c,v 1.32 2024/02/16 19:31:25 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.32    rillig __RCSID("$NetBSD: snprintb.c,v 1.32 2024/02/16 19:31:25 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.32    rillig __KERNEL_RCSID(0, "$NetBSD: snprintb.c,v 1.32 2024/02/16 19:31:25 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 	char sep;
     69  1.27    rillig 	int restart = 0;
     70   1.1  christos 
     71   1.1  christos #ifdef _KERNEL
     72   1.1  christos 	/*
     73   1.1  christos 	 * For safety; no other *s*printf() do this, but in the kernel
     74   1.1  christos 	 * we don't usually check the return value
     75   1.1  christos 	 */
     76  1.24    rillig 	(void)memset(buf, 0, bufsize);
     77   1.1  christos #endif /* _KERNEL */
     78   1.1  christos 
     79  1.28    rillig 	int old_style = *bitfmt != '\177';
     80  1.28    rillig 	if (!old_style)
     81  1.28    rillig 		bitfmt++;
     82  1.28    rillig 	switch (*bitfmt++) {
     83   1.1  christos 	case 8:
     84  1.27    rillig 		num_fmt = "%#jo";
     85   1.1  christos 		break;
     86   1.1  christos 	case 10:
     87  1.27    rillig 		num_fmt = "%ju";
     88   1.1  christos 		break;
     89   1.1  christos 	case 16:
     90  1.27    rillig 		num_fmt = "%#jx";
     91   1.1  christos 		break;
     92   1.1  christos 	default:
     93   1.1  christos 		goto internal;
     94   1.1  christos 	}
     95   1.1  christos 
     96   1.5  pgoyette 	/* Reserve space for trailing blank line if needed */
     97  1.27    rillig 	if (line_max > 0)
     98  1.24    rillig 		bufsize--;
     99   1.5  pgoyette 
    100  1.32    rillig 	int val_len = snprintf(bp, bufsize, num_fmt, (uintmax_t)val);
    101  1.32    rillig 	if (val_len < 0)
    102   1.1  christos 		goto internal;
    103   1.1  christos 
    104  1.32    rillig 	size_t total_len = val_len, line_len = val_len, sep_line_len = 0;
    105   1.5  pgoyette 
    106  1.32    rillig 	if (total_len < bufsize)
    107  1.27    rillig 		bp += total_len;
    108   1.1  christos 	else
    109  1.24    rillig 		bp += bufsize - 1;
    110   1.1  christos 
    111  1.26    rillig #define	STORE(c) do {							\
    112  1.27    rillig 		line_len++;						\
    113  1.32    rillig 		if (++total_len < bufsize)				\
    114  1.26    rillig 			*bp++ = (c);					\
    115  1.26    rillig 	} while (0)
    116  1.26    rillig 
    117  1.26    rillig #define	BACKUP() do {							\
    118  1.27    rillig 		if (sep_bp != NULL) {					\
    119  1.27    rillig 			bp = sep_bp;					\
    120  1.27    rillig 			sep_bp = NULL;					\
    121  1.27    rillig 			total_len -= line_len - sep_line_len;		\
    122   1.5  pgoyette 			restart = 1;					\
    123  1.28    rillig 			bitfmt = sep_bitfmt;				\
    124  1.26    rillig 		}							\
    125  1.26    rillig 		STORE('>');						\
    126  1.26    rillig 		STORE('\0');						\
    127  1.32    rillig 		if (total_len < bufsize)				\
    128  1.27    rillig 			snprintf(bp, bufsize - total_len, num_fmt,	\
    129  1.27    rillig 			    (uintmax_t)val);				\
    130  1.27    rillig 		total_len += val_len;					\
    131  1.27    rillig 		line_len = val_len;					\
    132  1.27    rillig 		bp += val_len;						\
    133  1.26    rillig 	} while (0)
    134  1.26    rillig 
    135  1.26    rillig #define	PUTSEP() do {							\
    136  1.32    rillig 		if (line_max > 0 && line_len >= line_max) {		\
    137  1.26    rillig 			BACKUP();					\
    138  1.26    rillig 			STORE('<');					\
    139  1.26    rillig 		} else {						\
    140  1.26    rillig 			/* Remember separator location */		\
    141  1.27    rillig 			if (line_max > 0 && sep != '<') {		\
    142  1.27    rillig 				sep_line_len = line_len;		\
    143  1.27    rillig 				sep_bp = bp;				\
    144  1.28    rillig 				sep_bitfmt = cur_bitfmt;		\
    145   1.5  pgoyette 			}						\
    146  1.26    rillig 			STORE(sep);					\
    147  1.26    rillig 			restart = 0;					\
    148  1.26    rillig 		}							\
    149  1.26    rillig 	} while (0)
    150  1.12       mrg 
    151  1.12       mrg #define	PUTCHR(c) do {							\
    152  1.32    rillig 		if (line_max > 0 && line_len >= line_max - 1) {		\
    153  1.26    rillig 			BACKUP();					\
    154  1.26    rillig 			if (restart == 0)				\
    155   1.5  pgoyette 				STORE(c);				\
    156  1.26    rillig 			else						\
    157  1.26    rillig 				sep = '<';				\
    158  1.26    rillig 		} else {						\
    159  1.26    rillig 			STORE(c);					\
    160  1.26    rillig 			restart = 0;					\
    161  1.26    rillig 		}							\
    162  1.26    rillig 	} while (0)
    163   1.5  pgoyette 
    164  1.26    rillig #define	PUTS(s) do {							\
    165  1.28    rillig 		while ((*(s)++) != 0) {					\
    166  1.28    rillig 			PUTCHR((s)[-1]);				\
    167   1.5  pgoyette 			if (restart)					\
    168   1.5  pgoyette 				break;					\
    169  1.26    rillig 		}							\
    170  1.26    rillig 	} while (0)
    171  1.26    rillig 
    172  1.26    rillig #define	FMTSTR(sb, f) do {						\
    173  1.32    rillig 		size_t n = total_len < bufsize				\
    174  1.31    rillig 		    ? bufsize - total_len : 0;				\
    175  1.31    rillig 		int fmt_len = snprintf(bp, n, sb, (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.32    rillig 		if (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.30    rillig 		for (uint8_t 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.30    rillig 			uint8_t 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.30    rillig 				uint8_t 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.32    rillig 					    && 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.32    rillig 		if (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.32    rillig 	if (total_len >= bufsize && bufsize > 0)
    289  1.25    rillig 		buf[bufsize - 1] = '\0';
    290  1.32    rillig 	return (int)(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