Home | History | Annotate | Line # | Download | only in stdlib
      1 /*	$NetBSD: strsuftoll.c,v 1.10 2024/06/09 18:55:00 mrg Exp $	*/
      2 /*-
      3  * Copyright (c) 2001-2002,2004 The NetBSD Foundation, Inc.
      4  * All rights reserved.
      5  *
      6  * This code is derived from software contributed to The NetBSD Foundation
      7  * by Luke Mewburn.
      8  *
      9  * Redistribution and use in source and binary forms, with or without
     10  * modification, are permitted provided that the following conditions
     11  * are met:
     12  * 1. Redistributions of source code must retain the above copyright
     13  *    notice, this list of conditions and the following disclaimer.
     14  * 2. Redistributions in binary form must reproduce the above copyright
     15  *    notice, this list of conditions and the following disclaimer in the
     16  *    documentation and/or other materials provided with the distribution.
     17  *
     18  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     19  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     20  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     21  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     22  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     23  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     24  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     25  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     26  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     27  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     28  * POSSIBILITY OF SUCH DAMAGE.
     29  */
     30 /*-
     31  * Copyright (c) 1991, 1993, 1994
     32  *	The Regents of the University of California.  All rights reserved.
     33  *
     34  * This code is derived from software contributed to Berkeley by
     35  * Keith Muller of the University of California, San Diego and Lance
     36  * Visser of Convex Computer Corporation.
     37  *
     38  * Redistribution and use in source and binary forms, with or without
     39  * modification, are permitted provided that the following conditions
     40  * are met:
     41  * 1. Redistributions of source code must retain the above copyright
     42  *    notice, this list of conditions and the following disclaimer.
     43  * 2. Redistributions in binary form must reproduce the above copyright
     44  *    notice, this list of conditions and the following disclaimer in the
     45  *    documentation and/or other materials provided with the distribution.
     46  * 3. Neither the name of the University nor the names of its contributors
     47  *    may be used to endorse or promote products derived from this software
     48  *    without specific prior written permission.
     49  *
     50  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     51  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     52  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     53  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     54  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     55  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     56  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     57  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     58  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     59  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     60  * SUCH DAMAGE.
     61  */
     62 
     63 #if HAVE_NBTOOL_CONFIG_H
     64 #include "nbtool_config.h"
     65 #endif
     66 
     67 #include <sys/cdefs.h>
     68 
     69 #if defined(LIBC_SCCS) && !defined(lint)
     70 __RCSID("$NetBSD: strsuftoll.c,v 1.10 2024/06/09 18:55:00 mrg Exp $");
     71 #endif /* LIBC_SCCS and not lint */
     72 
     73 #ifdef _LIBC
     74 #include "namespace.h"
     75 #endif
     76 
     77 #if !HAVE_STRSUFTOLL
     78 
     79 #include <sys/types.h>
     80 #include <sys/time.h>
     81 
     82 #include <assert.h>
     83 #include <ctype.h>
     84 #include <err.h>
     85 #include <errno.h>
     86 #include <limits.h>
     87 #include <stdio.h>
     88 #include <stdlib.h>
     89 #include <string.h>
     90 
     91 #ifdef _LIBC
     92 # ifdef __weak_alias
     93 __weak_alias(strsuftoll, _strsuftoll)
     94 __weak_alias(strsuftollx, _strsuftollx)
     95 # endif
     96 #endif /* LIBC */
     97 
     98 /*
     99  * Convert an expression of the following forms to a (u)int64_t.
    100  * 	1) A positive decimal number.
    101  *	2) A positive decimal number followed by a b (mult by 512).
    102  *	3) A positive decimal number followed by a k (mult by 1024).
    103  *	4) A positive decimal number followed by a m (mult by 1048576).
    104  *	5) A positive decimal number followed by a g (mult by 1073741824).
    105  *	6) A positive decimal number followed by a t (mult by 1099511627776).
    106  *	7) A positive decimal number followed by a w (mult by sizeof int)
    107  *	8) Two or more positive decimal numbers (with/without k,b or w).
    108  *	   separated by x (also * for backwards compatibility), specifying
    109  *	   the product of the indicated values.
    110  * Returns the result upon successful conversion, or exits with an
    111  * appropriate error.
    112  *
    113  */
    114 #ifdef __m68k__	/* See doc/HACKS. */
    115 __attribute((__optimize__("-fno-stack-protector")))
    116 #endif
    117 /* LONGLONG */
    118 long long
    119 strsuftoll(const char *desc, const char *val,
    120     long long min, long long max)
    121 {
    122 	long long result;
    123 	char	errbuf[100];
    124 
    125 	result = strsuftollx(desc, val, min, max, errbuf, sizeof(errbuf));
    126 	if (*errbuf != '\0')
    127 		errx(EXIT_FAILURE, "%s", errbuf);
    128 	return result;
    129 }
    130 
    131 /*
    132  * As strsuftoll(), but returns the error message into the provided buffer
    133  * rather than exiting with it.
    134  */
    135 /* LONGLONG */
    136 static long long
    137 __strsuftollx(const char *desc, const char *val,
    138     long long min, long long max, char *ebuf, size_t ebuflen, size_t depth)
    139 {
    140 	long long num, t;
    141 	char	*expr;
    142 
    143 	_DIAGASSERT(desc != NULL);
    144 	_DIAGASSERT(val != NULL);
    145 	_DIAGASSERT(ebuf != NULL);
    146 
    147 	if (depth > 16) {
    148 		snprintf(ebuf, ebuflen, "%s: Recursion limit exceeded", desc);
    149 		return 0;
    150 	}
    151 
    152 	while (isspace((unsigned char)*val))	/* Skip leading space */
    153 		val++;
    154 
    155 	errno = 0;
    156 	num = strtoll(val, &expr, 10);
    157 	if (errno == ERANGE)
    158 		goto erange;			/* Overflow */
    159 
    160 	if (expr == val)			/* No digits */
    161 		goto badnum;
    162 
    163 	switch (*expr) {
    164 	case 'b':
    165 		t = num;
    166 		num *= 512;			/* 1 block */
    167 		if (t > num)
    168 			goto erange;
    169 		++expr;
    170 		break;
    171 	case 'k':
    172 		t = num;
    173 		num *= 1024;			/* 1 kibibyte */
    174 		if (t > num)
    175 			goto erange;
    176 		++expr;
    177 		break;
    178 	case 'm':
    179 		t = num;
    180 		num *= 1048576;			/* 1 mebibyte */
    181 		if (t > num)
    182 			goto erange;
    183 		++expr;
    184 		break;
    185 	case 'g':
    186 		t = num;
    187 		num *= 1073741824;		/* 1 gibibyte */
    188 		if (t > num)
    189 			goto erange;
    190 		++expr;
    191 		break;
    192 	case 't':
    193 		t = num;
    194 		num *= 1099511627776LL;		/* 1 tebibyte */
    195 		if (t > num)
    196 			goto erange;
    197 		++expr;
    198 		break;
    199 	case 'w':
    200 		t = num;
    201 		num *= sizeof(int);		/* 1 word */
    202 		if (t > num)
    203 			goto erange;
    204 		++expr;
    205 		break;
    206 	}
    207 
    208 	switch (*expr) {
    209 	case '\0':
    210 		break;
    211 	case '*':				/* Backward compatible */
    212 	case 'x':
    213 		t = num;
    214 		num *= __strsuftollx(desc, expr + 1, min, max, ebuf, ebuflen,
    215 			depth + 1);
    216 		if (*ebuf != '\0')
    217 			return 0;
    218 		if (t > num) {
    219  erange:
    220 			errno = ERANGE;
    221 			snprintf(ebuf, ebuflen, "%s: %s", desc, strerror(errno));
    222 			return 0;
    223 		}
    224 		break;
    225 	default:
    226  badnum:
    227 		snprintf(ebuf, ebuflen, "%s `%s': illegal number", desc, val);
    228 		return 0;
    229 	}
    230 	if (num < min) {
    231 		/* LONGLONG */
    232 		snprintf(ebuf, ebuflen, "%s %lld is less than %lld.",
    233 		    desc, (long long)num, (long long)min);
    234 		return 0;
    235 	}
    236 	if (num > max) {
    237 		/* LONGLONG */
    238 		snprintf(ebuf, ebuflen, "%s %lld is greater than %lld.",
    239 		    desc, (long long)num, (long long)max);
    240 		return 0;
    241 	}
    242 	*ebuf = '\0';
    243 	return num;
    244 }
    245 
    246 long long
    247 strsuftollx(const char *desc, const char *val,
    248     long long min, long long max, char *ebuf, size_t ebuflen)
    249 {
    250 	return __strsuftollx(desc, val, min, max, ebuf, ebuflen, 0);
    251 }
    252 #endif /* !HAVE_STRSUFTOLL */
    253