xref: /titanic_52/usr/src/lib/libuutil/common/uu_strtoint.c (revision 7c478bd95313f5f23a4c958a745db2134aa03244)
1*7c478bd9Sstevel@tonic-gate /*
2*7c478bd9Sstevel@tonic-gate  * CDDL HEADER START
3*7c478bd9Sstevel@tonic-gate  *
4*7c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*7c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*7c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*7c478bd9Sstevel@tonic-gate  * with the License.
8*7c478bd9Sstevel@tonic-gate  *
9*7c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*7c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*7c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*7c478bd9Sstevel@tonic-gate  * and limitations under the License.
13*7c478bd9Sstevel@tonic-gate  *
14*7c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*7c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*7c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*7c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*7c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*7c478bd9Sstevel@tonic-gate  *
20*7c478bd9Sstevel@tonic-gate  * CDDL HEADER END
21*7c478bd9Sstevel@tonic-gate  */
22*7c478bd9Sstevel@tonic-gate /*
23*7c478bd9Sstevel@tonic-gate  * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
24*7c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
25*7c478bd9Sstevel@tonic-gate  */
26*7c478bd9Sstevel@tonic-gate 
27*7c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
28*7c478bd9Sstevel@tonic-gate 
29*7c478bd9Sstevel@tonic-gate #include "libuutil_common.h"
30*7c478bd9Sstevel@tonic-gate 
31*7c478bd9Sstevel@tonic-gate #include <limits.h>
32*7c478bd9Sstevel@tonic-gate #include <ctype.h>
33*7c478bd9Sstevel@tonic-gate 
34*7c478bd9Sstevel@tonic-gate #define	MAX_BASE	36
35*7c478bd9Sstevel@tonic-gate 
36*7c478bd9Sstevel@tonic-gate #define	IS_DIGIT(x)	((x) >= '0' && (x) <= '9')
37*7c478bd9Sstevel@tonic-gate 
38*7c478bd9Sstevel@tonic-gate #define	CTOI(x) (((x) >= '0' && (x) <= '9') ? (x) - '0' : \
39*7c478bd9Sstevel@tonic-gate 	    ((x) >= 'a' && (x) <= 'z') ? (x) + 10 - 'a' : (x) + 10 - 'A')
40*7c478bd9Sstevel@tonic-gate 
41*7c478bd9Sstevel@tonic-gate static int
42*7c478bd9Sstevel@tonic-gate strtoint(const char *s_arg, uint64_t *out, uint32_t base, int sign)
43*7c478bd9Sstevel@tonic-gate {
44*7c478bd9Sstevel@tonic-gate 	const unsigned char *s = (const unsigned char *)s_arg;
45*7c478bd9Sstevel@tonic-gate 
46*7c478bd9Sstevel@tonic-gate 	uint64_t val = 0;
47*7c478bd9Sstevel@tonic-gate 	uint64_t multmax;
48*7c478bd9Sstevel@tonic-gate 
49*7c478bd9Sstevel@tonic-gate 	unsigned c, i;
50*7c478bd9Sstevel@tonic-gate 
51*7c478bd9Sstevel@tonic-gate 	int neg = 0;
52*7c478bd9Sstevel@tonic-gate 
53*7c478bd9Sstevel@tonic-gate 	int bad_digit = 0;
54*7c478bd9Sstevel@tonic-gate 	int bad_char = 0;
55*7c478bd9Sstevel@tonic-gate 	int overflow = 0;
56*7c478bd9Sstevel@tonic-gate 
57*7c478bd9Sstevel@tonic-gate 	if (s == NULL || base == 1 || base > MAX_BASE) {
58*7c478bd9Sstevel@tonic-gate 		uu_set_error(UU_ERROR_INVALID_ARGUMENT);
59*7c478bd9Sstevel@tonic-gate 		return (-1);
60*7c478bd9Sstevel@tonic-gate 	}
61*7c478bd9Sstevel@tonic-gate 
62*7c478bd9Sstevel@tonic-gate 	while ((c = *s) != 0 && isspace(c))
63*7c478bd9Sstevel@tonic-gate 		s++;
64*7c478bd9Sstevel@tonic-gate 
65*7c478bd9Sstevel@tonic-gate 	switch (c) {
66*7c478bd9Sstevel@tonic-gate 	case '-':
67*7c478bd9Sstevel@tonic-gate 		if (!sign)
68*7c478bd9Sstevel@tonic-gate 			overflow = 1;		/* becomes underflow below */
69*7c478bd9Sstevel@tonic-gate 		neg = 1;
70*7c478bd9Sstevel@tonic-gate 		/*FALLTHRU*/
71*7c478bd9Sstevel@tonic-gate 	case '+':
72*7c478bd9Sstevel@tonic-gate 		c = *++s;
73*7c478bd9Sstevel@tonic-gate 		break;
74*7c478bd9Sstevel@tonic-gate 	default:
75*7c478bd9Sstevel@tonic-gate 		break;
76*7c478bd9Sstevel@tonic-gate 	}
77*7c478bd9Sstevel@tonic-gate 
78*7c478bd9Sstevel@tonic-gate 	if (c == '\0') {
79*7c478bd9Sstevel@tonic-gate 		uu_set_error(UU_ERROR_EMPTY);
80*7c478bd9Sstevel@tonic-gate 		return (-1);
81*7c478bd9Sstevel@tonic-gate 	}
82*7c478bd9Sstevel@tonic-gate 
83*7c478bd9Sstevel@tonic-gate 	if (base == 0) {
84*7c478bd9Sstevel@tonic-gate 		if (c != '0')
85*7c478bd9Sstevel@tonic-gate 			base = 10;
86*7c478bd9Sstevel@tonic-gate 		else if (s[1] == 'x' || s[1] == 'X')
87*7c478bd9Sstevel@tonic-gate 			base = 16;
88*7c478bd9Sstevel@tonic-gate 		else
89*7c478bd9Sstevel@tonic-gate 			base = 8;
90*7c478bd9Sstevel@tonic-gate 	}
91*7c478bd9Sstevel@tonic-gate 
92*7c478bd9Sstevel@tonic-gate 	if (base == 16 && c == '0' && (s[1] == 'x' || s[1] == 'X'))
93*7c478bd9Sstevel@tonic-gate 		c = *(s += 2);
94*7c478bd9Sstevel@tonic-gate 
95*7c478bd9Sstevel@tonic-gate 	if ((val = CTOI(c)) >= base) {
96*7c478bd9Sstevel@tonic-gate 		if (IS_DIGIT(c))
97*7c478bd9Sstevel@tonic-gate 			bad_digit = 1;
98*7c478bd9Sstevel@tonic-gate 		else
99*7c478bd9Sstevel@tonic-gate 			bad_char = 1;
100*7c478bd9Sstevel@tonic-gate 		val = 0;
101*7c478bd9Sstevel@tonic-gate 	}
102*7c478bd9Sstevel@tonic-gate 
103*7c478bd9Sstevel@tonic-gate 	multmax = (uint64_t)UINT64_MAX / (uint64_t)base;
104*7c478bd9Sstevel@tonic-gate 
105*7c478bd9Sstevel@tonic-gate 	for (c = *++s; c != '\0'; c = *++s) {
106*7c478bd9Sstevel@tonic-gate 		if ((i = CTOI(c)) >= base) {
107*7c478bd9Sstevel@tonic-gate 			if (isspace(c))
108*7c478bd9Sstevel@tonic-gate 				break;
109*7c478bd9Sstevel@tonic-gate 			if (IS_DIGIT(c))
110*7c478bd9Sstevel@tonic-gate 				bad_digit = 1;
111*7c478bd9Sstevel@tonic-gate 			else
112*7c478bd9Sstevel@tonic-gate 				bad_char = 1;
113*7c478bd9Sstevel@tonic-gate 			i = 0;
114*7c478bd9Sstevel@tonic-gate 		}
115*7c478bd9Sstevel@tonic-gate 
116*7c478bd9Sstevel@tonic-gate 		if (val > multmax)
117*7c478bd9Sstevel@tonic-gate 			overflow = 1;
118*7c478bd9Sstevel@tonic-gate 
119*7c478bd9Sstevel@tonic-gate 		val *= base;
120*7c478bd9Sstevel@tonic-gate 		if ((uint64_t)UINT64_MAX - val < (uint64_t)i)
121*7c478bd9Sstevel@tonic-gate 			overflow = 1;
122*7c478bd9Sstevel@tonic-gate 
123*7c478bd9Sstevel@tonic-gate 		val += i;
124*7c478bd9Sstevel@tonic-gate 	}
125*7c478bd9Sstevel@tonic-gate 
126*7c478bd9Sstevel@tonic-gate 	while ((c = *s) != 0) {
127*7c478bd9Sstevel@tonic-gate 		if (!isspace(c))
128*7c478bd9Sstevel@tonic-gate 			bad_char = 1;
129*7c478bd9Sstevel@tonic-gate 		s++;
130*7c478bd9Sstevel@tonic-gate 	}
131*7c478bd9Sstevel@tonic-gate 
132*7c478bd9Sstevel@tonic-gate 	if (sign) {
133*7c478bd9Sstevel@tonic-gate 		if (neg) {
134*7c478bd9Sstevel@tonic-gate 			if (val > -(uint64_t)INT64_MIN)
135*7c478bd9Sstevel@tonic-gate 				overflow = 1;
136*7c478bd9Sstevel@tonic-gate 		} else {
137*7c478bd9Sstevel@tonic-gate 			if (val > INT64_MAX)
138*7c478bd9Sstevel@tonic-gate 				overflow = 1;
139*7c478bd9Sstevel@tonic-gate 		}
140*7c478bd9Sstevel@tonic-gate 	}
141*7c478bd9Sstevel@tonic-gate 
142*7c478bd9Sstevel@tonic-gate 	if (neg)
143*7c478bd9Sstevel@tonic-gate 		val = -val;
144*7c478bd9Sstevel@tonic-gate 
145*7c478bd9Sstevel@tonic-gate 	if (bad_char | bad_digit | overflow) {
146*7c478bd9Sstevel@tonic-gate 		if (bad_char)
147*7c478bd9Sstevel@tonic-gate 			uu_set_error(UU_ERROR_INVALID_CHAR);
148*7c478bd9Sstevel@tonic-gate 		else if (bad_digit)
149*7c478bd9Sstevel@tonic-gate 			uu_set_error(UU_ERROR_INVALID_DIGIT);
150*7c478bd9Sstevel@tonic-gate 		else if (overflow) {
151*7c478bd9Sstevel@tonic-gate 			if (neg)
152*7c478bd9Sstevel@tonic-gate 				uu_set_error(UU_ERROR_UNDERFLOW);
153*7c478bd9Sstevel@tonic-gate 			else
154*7c478bd9Sstevel@tonic-gate 				uu_set_error(UU_ERROR_OVERFLOW);
155*7c478bd9Sstevel@tonic-gate 		}
156*7c478bd9Sstevel@tonic-gate 		return (-1);
157*7c478bd9Sstevel@tonic-gate 	}
158*7c478bd9Sstevel@tonic-gate 
159*7c478bd9Sstevel@tonic-gate 	*out = val;
160*7c478bd9Sstevel@tonic-gate 	return (0);
161*7c478bd9Sstevel@tonic-gate }
162*7c478bd9Sstevel@tonic-gate 
163*7c478bd9Sstevel@tonic-gate int
164*7c478bd9Sstevel@tonic-gate uu_strtoint(const char *s, void *v, size_t sz, int base,
165*7c478bd9Sstevel@tonic-gate     int64_t min, int64_t max)
166*7c478bd9Sstevel@tonic-gate {
167*7c478bd9Sstevel@tonic-gate 	uint64_t val_u;
168*7c478bd9Sstevel@tonic-gate 	int64_t val;
169*7c478bd9Sstevel@tonic-gate 
170*7c478bd9Sstevel@tonic-gate 	if (min > max)
171*7c478bd9Sstevel@tonic-gate 		goto bad_argument;
172*7c478bd9Sstevel@tonic-gate 
173*7c478bd9Sstevel@tonic-gate 	switch (sz) {
174*7c478bd9Sstevel@tonic-gate 	case 1:
175*7c478bd9Sstevel@tonic-gate 		if (max > INT8_MAX || min < INT8_MIN)
176*7c478bd9Sstevel@tonic-gate 			goto bad_argument;
177*7c478bd9Sstevel@tonic-gate 		break;
178*7c478bd9Sstevel@tonic-gate 	case 2:
179*7c478bd9Sstevel@tonic-gate 		if (max > INT16_MAX || min < INT16_MIN)
180*7c478bd9Sstevel@tonic-gate 			goto bad_argument;
181*7c478bd9Sstevel@tonic-gate 		break;
182*7c478bd9Sstevel@tonic-gate 	case 4:
183*7c478bd9Sstevel@tonic-gate 		if (max > INT32_MAX || min < INT32_MIN)
184*7c478bd9Sstevel@tonic-gate 			goto bad_argument;
185*7c478bd9Sstevel@tonic-gate 		break;
186*7c478bd9Sstevel@tonic-gate 	case 8:
187*7c478bd9Sstevel@tonic-gate 		if (max > INT64_MAX || min < INT64_MIN)
188*7c478bd9Sstevel@tonic-gate 			goto bad_argument;
189*7c478bd9Sstevel@tonic-gate 		break;
190*7c478bd9Sstevel@tonic-gate 	default:
191*7c478bd9Sstevel@tonic-gate 		goto bad_argument;
192*7c478bd9Sstevel@tonic-gate 	}
193*7c478bd9Sstevel@tonic-gate 
194*7c478bd9Sstevel@tonic-gate 	if (min == 0 && max == 0) {
195*7c478bd9Sstevel@tonic-gate 		min = -(1ULL << (8 * sz - 1));
196*7c478bd9Sstevel@tonic-gate 		max = (1ULL << (8 * sz - 1)) - 1;
197*7c478bd9Sstevel@tonic-gate 	}
198*7c478bd9Sstevel@tonic-gate 
199*7c478bd9Sstevel@tonic-gate 	if (strtoint(s, &val_u, base, 1) == -1)
200*7c478bd9Sstevel@tonic-gate 		return (-1);
201*7c478bd9Sstevel@tonic-gate 
202*7c478bd9Sstevel@tonic-gate 	val = (int64_t)val_u;
203*7c478bd9Sstevel@tonic-gate 
204*7c478bd9Sstevel@tonic-gate 	if (val < min) {
205*7c478bd9Sstevel@tonic-gate 		uu_set_error(UU_ERROR_UNDERFLOW);
206*7c478bd9Sstevel@tonic-gate 		return (-1);
207*7c478bd9Sstevel@tonic-gate 	} else if (val > max) {
208*7c478bd9Sstevel@tonic-gate 		uu_set_error(UU_ERROR_OVERFLOW);
209*7c478bd9Sstevel@tonic-gate 		return (-1);
210*7c478bd9Sstevel@tonic-gate 	}
211*7c478bd9Sstevel@tonic-gate 
212*7c478bd9Sstevel@tonic-gate 	switch (sz) {
213*7c478bd9Sstevel@tonic-gate 	case 1:
214*7c478bd9Sstevel@tonic-gate 		*(int8_t *)v = val;
215*7c478bd9Sstevel@tonic-gate 		return (0);
216*7c478bd9Sstevel@tonic-gate 	case 2:
217*7c478bd9Sstevel@tonic-gate 		*(int16_t *)v = val;
218*7c478bd9Sstevel@tonic-gate 		return (0);
219*7c478bd9Sstevel@tonic-gate 	case 4:
220*7c478bd9Sstevel@tonic-gate 		*(int32_t *)v = val;
221*7c478bd9Sstevel@tonic-gate 		return (0);
222*7c478bd9Sstevel@tonic-gate 	case 8:
223*7c478bd9Sstevel@tonic-gate 		*(int64_t *)v = val;
224*7c478bd9Sstevel@tonic-gate 		return (0);
225*7c478bd9Sstevel@tonic-gate 	default:
226*7c478bd9Sstevel@tonic-gate 		break;		/* fall through to bad_argument */
227*7c478bd9Sstevel@tonic-gate 	}
228*7c478bd9Sstevel@tonic-gate 
229*7c478bd9Sstevel@tonic-gate bad_argument:
230*7c478bd9Sstevel@tonic-gate 	uu_set_error(UU_ERROR_INVALID_ARGUMENT);
231*7c478bd9Sstevel@tonic-gate 	return (-1);
232*7c478bd9Sstevel@tonic-gate }
233*7c478bd9Sstevel@tonic-gate 
234*7c478bd9Sstevel@tonic-gate int
235*7c478bd9Sstevel@tonic-gate uu_strtouint(const char *s, void *v, size_t sz, int base,
236*7c478bd9Sstevel@tonic-gate     uint64_t min, uint64_t max)
237*7c478bd9Sstevel@tonic-gate {
238*7c478bd9Sstevel@tonic-gate 	uint64_t val;
239*7c478bd9Sstevel@tonic-gate 
240*7c478bd9Sstevel@tonic-gate 	if (min > max)
241*7c478bd9Sstevel@tonic-gate 		goto bad_argument;
242*7c478bd9Sstevel@tonic-gate 
243*7c478bd9Sstevel@tonic-gate 	switch (sz) {
244*7c478bd9Sstevel@tonic-gate 	case 1:
245*7c478bd9Sstevel@tonic-gate 		if (max > UINT8_MAX)
246*7c478bd9Sstevel@tonic-gate 			goto bad_argument;
247*7c478bd9Sstevel@tonic-gate 		break;
248*7c478bd9Sstevel@tonic-gate 	case 2:
249*7c478bd9Sstevel@tonic-gate 		if (max > UINT16_MAX)
250*7c478bd9Sstevel@tonic-gate 			goto bad_argument;
251*7c478bd9Sstevel@tonic-gate 		break;
252*7c478bd9Sstevel@tonic-gate 	case 4:
253*7c478bd9Sstevel@tonic-gate 		if (max > UINT32_MAX)
254*7c478bd9Sstevel@tonic-gate 			goto bad_argument;
255*7c478bd9Sstevel@tonic-gate 		break;
256*7c478bd9Sstevel@tonic-gate 	case 8:
257*7c478bd9Sstevel@tonic-gate 		if (max > UINT64_MAX)
258*7c478bd9Sstevel@tonic-gate 			goto bad_argument;
259*7c478bd9Sstevel@tonic-gate 		break;
260*7c478bd9Sstevel@tonic-gate 	default:
261*7c478bd9Sstevel@tonic-gate 		goto bad_argument;
262*7c478bd9Sstevel@tonic-gate 	}
263*7c478bd9Sstevel@tonic-gate 
264*7c478bd9Sstevel@tonic-gate 	if (min == 0 && max == 0) {
265*7c478bd9Sstevel@tonic-gate 		/* we have to be careful, since << can overflow */
266*7c478bd9Sstevel@tonic-gate 		max = (1ULL << (8 * sz - 1)) * 2 - 1;
267*7c478bd9Sstevel@tonic-gate 	}
268*7c478bd9Sstevel@tonic-gate 
269*7c478bd9Sstevel@tonic-gate 	if (strtoint(s, &val, base, 0) == -1)
270*7c478bd9Sstevel@tonic-gate 		return (-1);
271*7c478bd9Sstevel@tonic-gate 
272*7c478bd9Sstevel@tonic-gate 	if (val < min) {
273*7c478bd9Sstevel@tonic-gate 		uu_set_error(UU_ERROR_UNDERFLOW);
274*7c478bd9Sstevel@tonic-gate 		return (-1);
275*7c478bd9Sstevel@tonic-gate 	} else if (val > max) {
276*7c478bd9Sstevel@tonic-gate 		uu_set_error(UU_ERROR_OVERFLOW);
277*7c478bd9Sstevel@tonic-gate 		return (-1);
278*7c478bd9Sstevel@tonic-gate 	}
279*7c478bd9Sstevel@tonic-gate 
280*7c478bd9Sstevel@tonic-gate 	switch (sz) {
281*7c478bd9Sstevel@tonic-gate 	case 1:
282*7c478bd9Sstevel@tonic-gate 		*(uint8_t *)v = val;
283*7c478bd9Sstevel@tonic-gate 		return (0);
284*7c478bd9Sstevel@tonic-gate 	case 2:
285*7c478bd9Sstevel@tonic-gate 		*(uint16_t *)v = val;
286*7c478bd9Sstevel@tonic-gate 		return (0);
287*7c478bd9Sstevel@tonic-gate 	case 4:
288*7c478bd9Sstevel@tonic-gate 		*(uint32_t *)v = val;
289*7c478bd9Sstevel@tonic-gate 		return (0);
290*7c478bd9Sstevel@tonic-gate 	case 8:
291*7c478bd9Sstevel@tonic-gate 		*(uint64_t *)v = val;
292*7c478bd9Sstevel@tonic-gate 		return (0);
293*7c478bd9Sstevel@tonic-gate 	default:
294*7c478bd9Sstevel@tonic-gate 		break;		/* shouldn't happen, fall through */
295*7c478bd9Sstevel@tonic-gate 	}
296*7c478bd9Sstevel@tonic-gate 
297*7c478bd9Sstevel@tonic-gate bad_argument:
298*7c478bd9Sstevel@tonic-gate 	uu_set_error(UU_ERROR_INVALID_ARGUMENT);
299*7c478bd9Sstevel@tonic-gate 	return (-1);
300*7c478bd9Sstevel@tonic-gate }
301