Home | History | Annotate | Line # | Download | only in libutil
snprintb.c revision 1.46
      1 /*	$NetBSD: snprintb.c,v 1.46 2024/04/07 10:10:54 rillig Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 2002, 2024 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     10  * 1. Redistributions of source code must retain the above copyright
     11  *    notice, this list of conditions and the following disclaimer.
     12  * 2. Redistributions in binary form must reproduce the above copyright
     13  *    notice, this list of conditions and the following disclaimer in the
     14  *    documentation and/or other materials provided with the distribution.
     15  *
     16  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     17  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     18  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     19  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     20  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     21  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     22  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     23  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     24  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     25  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     26  * POSSIBILITY OF SUCH DAMAGE.
     27  */
     28 
     29 #ifndef _STANDALONE
     30 # ifndef _KERNEL
     31 
     32 #  if HAVE_NBTOOL_CONFIG_H
     33 #   include "nbtool_config.h"
     34 #  endif
     35 
     36 #  include <sys/cdefs.h>
     37 #  if defined(LIBC_SCCS)
     38 __RCSID("$NetBSD: snprintb.c,v 1.46 2024/04/07 10:10:54 rillig Exp $");
     39 #  endif
     40 
     41 #  include <sys/types.h>
     42 #  include <inttypes.h>
     43 #  include <stdio.h>
     44 #  include <string.h>
     45 #  include <util.h>
     46 #  include <errno.h>
     47 # else /* ! _KERNEL */
     48 #  include <sys/cdefs.h>
     49 __KERNEL_RCSID(0, "$NetBSD: snprintb.c,v 1.46 2024/04/07 10:10:54 rillig Exp $");
     50 #  include <sys/param.h>
     51 #  include <sys/inttypes.h>
     52 #  include <sys/systm.h>
     53 #  include <lib/libkern/libkern.h>
     54 # endif /* ! _KERNEL */
     55 
     56 # ifndef HAVE_SNPRINTB_M
     57 typedef struct {
     58 	char *const buf;
     59 	size_t const bufsize;
     60 	const char *bitfmt;
     61 	uint64_t const val;
     62 	size_t const line_max;
     63 
     64 	char num_fmt[5];
     65 	size_t total_len;
     66 	size_t line_pos;
     67 	size_t comma_pos;
     68 	int in_angle_brackets;
     69 } state;
     70 
     71 static void
     72 store(state *s, char c)
     73 {
     74 	if (s->total_len < s->bufsize)
     75 		s->buf[s->total_len] = c;
     76 	s->total_len++;
     77 }
     78 
     79 static int
     80 store_num(state *s, const char *fmt, uintmax_t num)
     81 {
     82 	int num_len = s->total_len < s->bufsize
     83 	    ? snprintf(s->buf + s->total_len, s->bufsize - s->total_len,
     84 		fmt, num)
     85 	    : snprintf(NULL, 0, fmt, num);
     86 	if (num_len > 0)
     87 		s->total_len += num_len;
     88 	return num_len;
     89 }
     90 
     91 static void
     92 store_eol(state *s)
     93 {
     94 	if (s->total_len - s->line_pos > s->line_max) {
     95 		s->total_len = s->line_pos + s->line_max - 1;
     96 		store(s, '#');
     97 	}
     98 	store(s, '\0');
     99 	s->line_pos = s->total_len;
    100 	s->comma_pos = 0;
    101 	s->in_angle_brackets = 0;
    102 }
    103 
    104 static void
    105 store_delimiter(state *s)
    106 {
    107 	if (s->in_angle_brackets) {
    108 		s->comma_pos = s->total_len;
    109 		store(s, ',');
    110 	} else {
    111 		store(s, '<');
    112 		s->in_angle_brackets = 1;
    113 	}
    114 }
    115 
    116 static void
    117 maybe_wrap_line(state *s, const char *bitfmt)
    118 {
    119 	if (s->line_max > 0
    120 	    && s->comma_pos > 0
    121 	    && s->total_len - s->line_pos >= s->line_max) {
    122 		s->total_len = s->comma_pos;
    123 		store(s, '>');
    124 		store_eol(s);
    125 		store_num(s, s->num_fmt, s->val);
    126 		s->bitfmt = bitfmt;
    127 	}
    128 }
    129 
    130 static int
    131 old_style(state *s)
    132 {
    133 	while (*s->bitfmt != '\0') {
    134 		const char *cur_bitfmt = s->bitfmt;
    135 		uint8_t bit = *s->bitfmt;
    136 		if (bit > ' ')
    137 			return -1;
    138 		if (s->val & (1U << (bit - 1))) {
    139 			store_delimiter(s);
    140 			while ((uint8_t)*++s->bitfmt > ' ')
    141 				store(s, *s->bitfmt);
    142 			maybe_wrap_line(s, cur_bitfmt);
    143 		} else
    144 			while ((uint8_t)*++s->bitfmt > ' ')
    145 				continue;
    146 	}
    147 	return 0;
    148 }
    149 
    150 static int
    151 new_style(state *s)
    152 {
    153 	uint64_t field = s->val;
    154 	int matched = 1;
    155 	const char *prev_bitfmt = s->bitfmt;
    156 	while (*s->bitfmt != '\0') {
    157 		const char *cur_bitfmt = s->bitfmt;
    158 		uint8_t kind = cur_bitfmt[0];
    159 		switch (kind) {
    160 		case 'b':
    161 			prev_bitfmt = cur_bitfmt;
    162 			uint8_t b_bit = cur_bitfmt[1];
    163 			if (b_bit >= 64)
    164 				return -1;
    165 			if (cur_bitfmt[2] == '\0')
    166 				return -1;
    167 			s->bitfmt += 2;
    168 			if (((s->val >> b_bit) & 1) == 0)
    169 				goto skip_description;
    170 			store_delimiter(s);
    171 			while (*s->bitfmt++ != '\0')
    172 				store(s, s->bitfmt[-1]);
    173 			maybe_wrap_line(s, cur_bitfmt);
    174 			break;
    175 		case 'f':
    176 		case 'F':
    177 			prev_bitfmt = cur_bitfmt;
    178 			matched = 0;
    179 			uint8_t f_lsb = cur_bitfmt[1];
    180 			if (f_lsb >= 64)
    181 				return -1;
    182 			uint8_t f_width = cur_bitfmt[2];
    183 			if (f_width > 64)
    184 				return -1;
    185 			if (kind == 'f' && cur_bitfmt[3] == '\0')
    186 				return -1;
    187 			field = s->val >> f_lsb;
    188 			if (f_width < 64)
    189 				field &= ((uint64_t) 1 << f_width) - 1;
    190 			s->bitfmt += 3;
    191 			store_delimiter(s);
    192 			if (kind == 'F')
    193 				goto skip_description;
    194 			while (*s->bitfmt++ != '\0')
    195 				store(s, s->bitfmt[-1]);
    196 			store(s, '=');
    197 			store_num(s, s->num_fmt, field);
    198 			maybe_wrap_line(s, cur_bitfmt);
    199 			break;
    200 		case '=':
    201 		case ':':
    202 			s->bitfmt += 2;
    203 			uint8_t cmp = cur_bitfmt[1];
    204 			if (cur_bitfmt[2] == '\0')
    205 				return -1;
    206 			if (field != cmp)
    207 				goto skip_description;
    208 			matched = 1;
    209 			if (kind == '=')
    210 				store(s, '=');
    211 			while (*s->bitfmt++ != '\0')
    212 				store(s, s->bitfmt[-1]);
    213 			maybe_wrap_line(s, prev_bitfmt);
    214 			break;
    215 		case '*':
    216 			if (cur_bitfmt[1] == '\0')
    217 				return -1;
    218 			s->bitfmt++;
    219 			if (matched)
    220 				goto skip_description;
    221 			matched = 1;
    222 			if (store_num(s, s->bitfmt, field) < 0)
    223 				return -1;
    224 			maybe_wrap_line(s, prev_bitfmt);
    225 			goto skip_description;
    226 		default:
    227 			return -1;
    228 		skip_description:
    229 			while (*s->bitfmt++ != '\0')
    230 				continue;
    231 			break;
    232 		}
    233 	}
    234 	return 0;
    235 }
    236 
    237 static void
    238 finish_buffer(state *s)
    239 {
    240 	if (s->line_max > 0) {
    241 		store_eol(s);
    242 		store(s, '\0');
    243 		if (s->bufsize >= 3 && s->total_len > s->bufsize)
    244 			s->buf[s->bufsize - 3] = '#';
    245 		if (s->bufsize >= 2 && s->total_len > s->bufsize)
    246 			s->buf[s->bufsize - 2] = '\0';
    247 		if (s->bufsize >= 1 && s->total_len > s->bufsize)
    248 			s->buf[s->bufsize - 1] = '\0';
    249 	} else {
    250 		store(s, '\0');
    251 		if (s->bufsize >= 2 && s->total_len > s->bufsize)
    252 			s->buf[s->bufsize - 2] = '#';
    253 		if (s->bufsize >= 1 && s->total_len > s->bufsize)
    254 			s->buf[s->bufsize - 1] = '\0';
    255 	}
    256 }
    257 
    258 int
    259 snprintb_m(char *buf, size_t bufsize, const char *bitfmt, uint64_t val,
    260 	   size_t line_max)
    261 {
    262 	int old = *bitfmt != '\177';
    263 	if (!old)
    264 		bitfmt++;
    265 
    266 	state s = {
    267 		.buf = buf,
    268 		.bufsize = bufsize,
    269 		.bitfmt = bitfmt,
    270 		.val = val,
    271 		.line_max = line_max,
    272 	};
    273 	int had_error = 0;
    274 
    275 	switch (*s.bitfmt++) {
    276 	case 8:
    277 		memcpy(s.num_fmt, "%#jo", 4);
    278 		break;
    279 	case 10:
    280 		memcpy(s.num_fmt, "%ju", 4);
    281 		break;
    282 	case 16:
    283 		memcpy(s.num_fmt, "%#jx", 4);
    284 		break;
    285 	default:
    286 		goto had_error;
    287 	}
    288 
    289 	store_num(&s, s.num_fmt, val);
    290 
    291 	if ((old ? old_style(&s) : new_style(&s)) < 0) {
    292 had_error:
    293 #ifndef _KERNEL
    294 		errno = EINVAL;
    295 #endif
    296 		had_error = 1;
    297 		store(&s, '#');
    298 	} else if (s.in_angle_brackets)
    299 		store(&s, '>');
    300 	finish_buffer(&s);
    301 	return had_error ? -1 : (int)(s.total_len - 1);
    302 }
    303 
    304 int
    305 snprintb(char *buf, size_t bufsize, const char *bitfmt, uint64_t val)
    306 {
    307 	return snprintb_m(buf, bufsize, bitfmt, val, 0);
    308 }
    309 # endif /* ! HAVE_SNPRINTB_M */
    310 #endif /* ! _STANDALONE */
    311