Home | History | Annotate | Line # | Download | only in libutil
snprintb.c revision 1.37
      1  1.37    rillig /*	$NetBSD: snprintb.c,v 1.37 2024/02/20 20:31:56 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.37    rillig __RCSID("$NetBSD: snprintb.c,v 1.37 2024/02/20 20:31:56 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.37    rillig __KERNEL_RCSID(0, "$NetBSD: snprintb.c,v 1.37 2024/02/20 20:31:56 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.35    rillig typedef struct {
     63  1.35    rillig 	char *const buf;
     64  1.35    rillig 	size_t const bufsize;
     65  1.35    rillig 	const char *bitfmt;
     66  1.35    rillig 	uint64_t const val;
     67  1.35    rillig 	size_t const line_max;
     68  1.35    rillig 
     69  1.35    rillig 	const char *const num_fmt;
     70  1.35    rillig 	unsigned const val_len;
     71  1.35    rillig 	unsigned total_len;
     72  1.37    rillig 	unsigned line_pos;
     73  1.37    rillig 	unsigned comma_pos;
     74  1.35    rillig 	char sep;
     75  1.35    rillig } state;
     76  1.35    rillig 
     77  1.35    rillig static void
     78  1.35    rillig store(state *s, char c)
     79  1.35    rillig {
     80  1.35    rillig 	if (s->total_len < s->bufsize)
     81  1.35    rillig 		s->buf[s->total_len] = c;
     82  1.35    rillig 	s->total_len++;
     83  1.35    rillig }
     84  1.35    rillig 
     85  1.37    rillig static int
     86  1.37    rillig store_num(state *s, const char *fmt, uintmax_t num)
     87  1.35    rillig {
     88  1.37    rillig 	int num_len = s->total_len < s->bufsize
     89  1.37    rillig 	    ? snprintf(s->buf + s->total_len, s->bufsize - s->total_len,
     90  1.37    rillig 		fmt, num)
     91  1.37    rillig 	    : snprintf(NULL, 0, fmt, num);
     92  1.37    rillig 	if (num_len > 0)
     93  1.37    rillig 		s->total_len += num_len;
     94  1.37    rillig 	return num_len;
     95  1.35    rillig }
     96  1.35    rillig 
     97  1.35    rillig static void
     98  1.37    rillig put_eol(state *s)
     99  1.35    rillig {
    100  1.37    rillig 	if (s->total_len - s->line_pos > s->line_max) {
    101  1.37    rillig 		s->total_len = (unsigned)(s->line_pos + s->line_max - 1);
    102  1.37    rillig 		store(s, '#');
    103  1.35    rillig 	}
    104  1.37    rillig 	store(s, '\0');
    105  1.37    rillig 	s->line_pos = s->total_len;
    106  1.37    rillig 	s->comma_pos = 0;
    107  1.37    rillig 	s->sep = '<';
    108  1.35    rillig }
    109  1.35    rillig 
    110  1.35    rillig static void
    111  1.37    rillig put_sep(state *s)
    112  1.35    rillig {
    113  1.37    rillig 	if (s->sep == ',') {
    114  1.37    rillig 		s->comma_pos = s->total_len;
    115  1.37    rillig 		store(s, ',');
    116  1.35    rillig 	} else {
    117  1.37    rillig 		store(s, '<');
    118  1.37    rillig 		s->sep = ',';
    119  1.35    rillig 	}
    120  1.35    rillig }
    121  1.35    rillig 
    122  1.35    rillig static void
    123  1.37    rillig wrap_if_necessary(state *s, const char *bitfmt)
    124  1.35    rillig {
    125  1.37    rillig 	if (s->line_max > 0
    126  1.37    rillig 	    && s->comma_pos > 0
    127  1.37    rillig 	    && s->total_len - s->line_pos >= s->line_max) {
    128  1.37    rillig 		s->total_len = s->comma_pos;
    129  1.37    rillig 		store(s, '>');
    130  1.37    rillig 		put_eol(s);
    131  1.37    rillig 		store_num(s, s->num_fmt, s->val);
    132  1.37    rillig 		s->bitfmt = bitfmt;
    133  1.35    rillig 	}
    134  1.35    rillig }
    135  1.35    rillig 
    136  1.35    rillig static int
    137  1.35    rillig old_style(state *s)
    138  1.35    rillig {
    139  1.37    rillig 	while (*s->bitfmt != '\0') {
    140  1.37    rillig 		const char *cur_bitfmt = s->bitfmt;
    141  1.37    rillig 		uint8_t bit = *s->bitfmt;
    142  1.37    rillig 		if (bit > ' ')
    143  1.37    rillig 			return -1;
    144  1.35    rillig 		if (s->val & (1U << (bit - 1))) {
    145  1.35    rillig 			put_sep(s);
    146  1.37    rillig 			while (*++s->bitfmt > ' ')
    147  1.37    rillig 				store(s, *s->bitfmt);
    148  1.37    rillig 			wrap_if_necessary(s, cur_bitfmt);
    149  1.35    rillig 		} else
    150  1.37    rillig 			while (*++s->bitfmt > ' ')
    151  1.35    rillig 				continue;
    152  1.35    rillig 	}
    153  1.37    rillig 	return 0;
    154  1.35    rillig }
    155  1.35    rillig 
    156  1.35    rillig static int
    157  1.35    rillig new_style(state *s)
    158  1.35    rillig {
    159  1.35    rillig 	uint64_t field = s->val;
    160  1.35    rillig 	int matched = 1;
    161  1.37    rillig 	const char *prev_bitfmt = s->bitfmt;
    162  1.35    rillig 	while (*s->bitfmt != '\0') {
    163  1.37    rillig 		const char *cur_bitfmt = s->bitfmt;
    164  1.35    rillig 		uint8_t kind = *s->bitfmt++;
    165  1.35    rillig 		uint8_t bit = *s->bitfmt++;
    166  1.35    rillig 		switch (kind) {
    167  1.35    rillig 		case 'b':
    168  1.37    rillig 			prev_bitfmt = cur_bitfmt;
    169  1.35    rillig 			if (((s->val >> bit) & 1) == 0)
    170  1.37    rillig 				goto skip_description;
    171  1.35    rillig 			put_sep(s);
    172  1.37    rillig 			while (*s->bitfmt++ != '\0')
    173  1.37    rillig 				store(s, s->bitfmt[-1]);
    174  1.37    rillig 			wrap_if_necessary(s, cur_bitfmt);
    175  1.35    rillig 			break;
    176  1.35    rillig 		case 'f':
    177  1.35    rillig 		case 'F':
    178  1.37    rillig 			prev_bitfmt = cur_bitfmt;
    179  1.35    rillig 			matched = 0;
    180  1.35    rillig 			field = (s->val >> bit) &
    181  1.37    rillig 			    (((uint64_t)1 << (uint8_t)*s->bitfmt++) - 1);
    182  1.35    rillig 			put_sep(s);
    183  1.35    rillig 			if (kind == 'F')
    184  1.37    rillig 				goto skip_description;
    185  1.37    rillig 			while (*s->bitfmt++ != '\0')
    186  1.37    rillig 				store(s, s->bitfmt[-1]);
    187  1.37    rillig 			store(s, '=');
    188  1.37    rillig 			store_num(s, s->num_fmt, field);
    189  1.37    rillig 			wrap_if_necessary(s, cur_bitfmt);
    190  1.35    rillig 			break;
    191  1.35    rillig 		case '=':
    192  1.35    rillig 		case ':':
    193  1.35    rillig 			/* Here "bit" is actually a value instead. */
    194  1.35    rillig 			if (field != bit)
    195  1.37    rillig 				goto skip_description;
    196  1.35    rillig 			matched = 1;
    197  1.35    rillig 			if (kind == '=')
    198  1.37    rillig 				store(s, '=');
    199  1.37    rillig 			while (*s->bitfmt++ != '\0')
    200  1.37    rillig 				store(s, s->bitfmt[-1]);
    201  1.37    rillig 			wrap_if_necessary(s, prev_bitfmt);
    202  1.35    rillig 			break;
    203  1.35    rillig 		case '*':
    204  1.35    rillig 			s->bitfmt--;
    205  1.37    rillig 			if (matched)
    206  1.37    rillig 				goto skip_description;
    207  1.37    rillig 			matched = 1;
    208  1.37    rillig 			if (store_num(s, s->bitfmt, field) < 0)
    209  1.37    rillig 				return -1;
    210  1.37    rillig 			wrap_if_necessary(s, prev_bitfmt);
    211  1.35    rillig 			/*FALLTHROUGH*/
    212  1.35    rillig 		default:
    213  1.37    rillig 		skip_description:
    214  1.35    rillig 			while (*s->bitfmt++ != '\0')
    215  1.35    rillig 				continue;
    216  1.35    rillig 			break;
    217  1.35    rillig 		}
    218  1.35    rillig 	}
    219  1.35    rillig 	return 0;
    220  1.35    rillig }
    221  1.35    rillig 
    222   1.1  christos int
    223  1.24    rillig snprintb_m(char *buf, size_t bufsize, const char *bitfmt, uint64_t val,
    224  1.27    rillig 	   size_t line_max)
    225   1.1  christos {
    226   1.1  christos #ifdef _KERNEL
    227   1.1  christos 	/*
    228   1.1  christos 	 * For safety; no other *s*printf() do this, but in the kernel
    229   1.1  christos 	 * we don't usually check the return value
    230   1.1  christos 	 */
    231  1.24    rillig 	(void)memset(buf, 0, bufsize);
    232   1.1  christos #endif /* _KERNEL */
    233   1.1  christos 
    234  1.35    rillig 
    235  1.35    rillig 	int old = *bitfmt != '\177';
    236  1.35    rillig 	if (!old)
    237  1.28    rillig 		bitfmt++;
    238  1.35    rillig 
    239  1.35    rillig 	const char *num_fmt;
    240  1.28    rillig 	switch (*bitfmt++) {
    241   1.1  christos 	case 8:
    242  1.27    rillig 		num_fmt = "%#jo";
    243   1.1  christos 		break;
    244   1.1  christos 	case 10:
    245  1.27    rillig 		num_fmt = "%ju";
    246   1.1  christos 		break;
    247   1.1  christos 	case 16:
    248  1.27    rillig 		num_fmt = "%#jx";
    249   1.1  christos 		break;
    250   1.1  christos 	default:
    251   1.1  christos 		goto internal;
    252   1.1  christos 	}
    253   1.1  christos 
    254  1.33    rillig 	int val_len = snprintf(buf, bufsize, num_fmt, (uintmax_t)val);
    255  1.32    rillig 	if (val_len < 0)
    256   1.1  christos 		goto internal;
    257   1.1  christos 
    258  1.35    rillig 	state s = {
    259  1.35    rillig 		.buf = buf,
    260  1.35    rillig 		.bufsize = bufsize,
    261  1.35    rillig 		.bitfmt = bitfmt,
    262  1.35    rillig 		.val = val,
    263  1.35    rillig 		.line_max = line_max,
    264  1.35    rillig 
    265  1.35    rillig 		.num_fmt = num_fmt,
    266  1.35    rillig 		.val_len = val_len,
    267  1.35    rillig 		.total_len = val_len,
    268  1.35    rillig 
    269  1.35    rillig 		.sep = '<',
    270  1.35    rillig 	};
    271  1.35    rillig 
    272  1.37    rillig 	if ((old ? old_style(&s) : new_style(&s)) < 0)
    273  1.35    rillig 		goto internal;
    274   1.5  pgoyette 
    275  1.35    rillig 	if (s.sep != '<')
    276  1.35    rillig 		store(&s, '>');
    277  1.35    rillig 	if (s.line_max > 0) {
    278  1.37    rillig 		put_eol(&s);
    279  1.35    rillig 		if (s.bufsize >= 2 && s.total_len > s.bufsize - 2)
    280  1.35    rillig 			s.buf[s.bufsize - 2] = '\0';
    281   1.1  christos 	}
    282  1.35    rillig 	store(&s, '\0');
    283  1.35    rillig 	if (s.bufsize >= 1 && s.total_len > s.bufsize - 1)
    284  1.35    rillig 		s.buf[s.bufsize - 1] = '\0';
    285  1.35    rillig 	return (int)(s.total_len - 1);
    286   1.1  christos internal:
    287   1.1  christos #ifndef _KERNEL
    288   1.1  christos 	errno = EINVAL;
    289   1.1  christos #endif
    290   1.1  christos 	return -1;
    291   1.1  christos }
    292   1.5  pgoyette 
    293   1.5  pgoyette int
    294  1.24    rillig snprintb(char *buf, size_t bufsize, const char *bitfmt, uint64_t val)
    295   1.5  pgoyette {
    296  1.24    rillig 	return snprintb_m(buf, bufsize, bitfmt, val, 0);
    297   1.5  pgoyette }
    298   1.9       apb # endif /* ! HAVE_SNPRINTB_M */
    299   1.9       apb #endif /* ! _STANDALONE */
    300