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