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 #include "libuutil_common.h"
28*7c478bd9Sstevel@tonic-gate 
29*7c478bd9Sstevel@tonic-gate #include <limits.h>
30*7c478bd9Sstevel@tonic-gate #include <ctype.h>
31*7c478bd9Sstevel@tonic-gate 
32*7c478bd9Sstevel@tonic-gate #define	MAX_BASE	36
33*7c478bd9Sstevel@tonic-gate 
34*7c478bd9Sstevel@tonic-gate #define	IS_DIGIT(x)	((x) >= '0' && (x) <= '9')
35*7c478bd9Sstevel@tonic-gate 
36*7c478bd9Sstevel@tonic-gate #define	CTOI(x) (((x) >= '0' && (x) <= '9') ? (x) - '0' : \
37*7c478bd9Sstevel@tonic-gate 	    ((x) >= 'a' && (x) <= 'z') ? (x) + 10 - 'a' : (x) + 10 - 'A')
38*7c478bd9Sstevel@tonic-gate 
39*7c478bd9Sstevel@tonic-gate static int
strtoint(const char * s_arg,uint64_t * out,uint32_t base,int sign)40*7c478bd9Sstevel@tonic-gate strtoint(const char *s_arg, uint64_t *out, uint32_t base, int sign)
41*7c478bd9Sstevel@tonic-gate {
42*7c478bd9Sstevel@tonic-gate 	const unsigned char *s = (const unsigned char *)s_arg;
43*7c478bd9Sstevel@tonic-gate 
44*7c478bd9Sstevel@tonic-gate 	uint64_t val = 0;
45*7c478bd9Sstevel@tonic-gate 	uint64_t multmax;
46*7c478bd9Sstevel@tonic-gate 
47*7c478bd9Sstevel@tonic-gate 	unsigned c, i;
48*7c478bd9Sstevel@tonic-gate 
49*7c478bd9Sstevel@tonic-gate 	int neg = 0;
50*7c478bd9Sstevel@tonic-gate 
51*7c478bd9Sstevel@tonic-gate 	int bad_digit = 0;
52*7c478bd9Sstevel@tonic-gate 	int bad_char = 0;
53*7c478bd9Sstevel@tonic-gate 	int overflow = 0;
54*7c478bd9Sstevel@tonic-gate 
55*7c478bd9Sstevel@tonic-gate 	if (s == NULL || base == 1 || base > MAX_BASE) {
56*7c478bd9Sstevel@tonic-gate 		uu_set_error(UU_ERROR_INVALID_ARGUMENT);
57*7c478bd9Sstevel@tonic-gate 		return (-1);
58*7c478bd9Sstevel@tonic-gate 	}
59*7c478bd9Sstevel@tonic-gate 
60*7c478bd9Sstevel@tonic-gate 	while ((c = *s) != 0 && isspace(c))
61*7c478bd9Sstevel@tonic-gate 		s++;
62*7c478bd9Sstevel@tonic-gate 
63*7c478bd9Sstevel@tonic-gate 	switch (c) {
64*7c478bd9Sstevel@tonic-gate 	case '-':
65*7c478bd9Sstevel@tonic-gate 		if (!sign)
66*7c478bd9Sstevel@tonic-gate 			overflow = 1;		/* becomes underflow below */
67*7c478bd9Sstevel@tonic-gate 		neg = 1;
68*7c478bd9Sstevel@tonic-gate 		/*FALLTHRU*/
69*7c478bd9Sstevel@tonic-gate 	case '+':
70*7c478bd9Sstevel@tonic-gate 		c = *++s;
71*7c478bd9Sstevel@tonic-gate 		break;
72*7c478bd9Sstevel@tonic-gate 	default:
73*7c478bd9Sstevel@tonic-gate 		break;
74*7c478bd9Sstevel@tonic-gate 	}
75*7c478bd9Sstevel@tonic-gate 
76*7c478bd9Sstevel@tonic-gate 	if (c == '\0') {
77*7c478bd9Sstevel@tonic-gate 		uu_set_error(UU_ERROR_EMPTY);
78*7c478bd9Sstevel@tonic-gate 		return (-1);
79*7c478bd9Sstevel@tonic-gate 	}
80*7c478bd9Sstevel@tonic-gate 
81*7c478bd9Sstevel@tonic-gate 	if (base == 0) {
82*7c478bd9Sstevel@tonic-gate 		if (c != '0')
83*7c478bd9Sstevel@tonic-gate 			base = 10;
84*7c478bd9Sstevel@tonic-gate 		else if (s[1] == 'x' || s[1] == 'X')
85*7c478bd9Sstevel@tonic-gate 			base = 16;
86*7c478bd9Sstevel@tonic-gate 		else
87*7c478bd9Sstevel@tonic-gate 			base = 8;
88*7c478bd9Sstevel@tonic-gate 	}
89*7c478bd9Sstevel@tonic-gate 
90*7c478bd9Sstevel@tonic-gate 	if (base == 16 && c == '0' && (s[1] == 'x' || s[1] == 'X'))
91*7c478bd9Sstevel@tonic-gate 		c = *(s += 2);
92*7c478bd9Sstevel@tonic-gate 
93*7c478bd9Sstevel@tonic-gate 	if ((val = CTOI(c)) >= base) {
94*7c478bd9Sstevel@tonic-gate 		if (IS_DIGIT(c))
95*7c478bd9Sstevel@tonic-gate 			bad_digit = 1;
96*7c478bd9Sstevel@tonic-gate 		else
97*7c478bd9Sstevel@tonic-gate 			bad_char = 1;
98*7c478bd9Sstevel@tonic-gate 		val = 0;
99*7c478bd9Sstevel@tonic-gate 	}
100*7c478bd9Sstevel@tonic-gate 
101*7c478bd9Sstevel@tonic-gate 	multmax = (uint64_t)UINT64_MAX / (uint64_t)base;
102*7c478bd9Sstevel@tonic-gate 
103*7c478bd9Sstevel@tonic-gate 	for (c = *++s; c != '\0'; c = *++s) {
104*7c478bd9Sstevel@tonic-gate 		if ((i = CTOI(c)) >= base) {
105*7c478bd9Sstevel@tonic-gate 			if (isspace(c))
106*7c478bd9Sstevel@tonic-gate 				break;
107*7c478bd9Sstevel@tonic-gate 			if (IS_DIGIT(c))
108*7c478bd9Sstevel@tonic-gate 				bad_digit = 1;
109*7c478bd9Sstevel@tonic-gate 			else
110*7c478bd9Sstevel@tonic-gate 				bad_char = 1;
111*7c478bd9Sstevel@tonic-gate 			i = 0;
112*7c478bd9Sstevel@tonic-gate 		}
113*7c478bd9Sstevel@tonic-gate 
114*7c478bd9Sstevel@tonic-gate 		if (val > multmax)
115*7c478bd9Sstevel@tonic-gate 			overflow = 1;
116*7c478bd9Sstevel@tonic-gate 
117*7c478bd9Sstevel@tonic-gate 		val *= base;
118*7c478bd9Sstevel@tonic-gate 		if ((uint64_t)UINT64_MAX - val < (uint64_t)i)
119*7c478bd9Sstevel@tonic-gate 			overflow = 1;
120*7c478bd9Sstevel@tonic-gate 
121*7c478bd9Sstevel@tonic-gate 		val += i;
122*7c478bd9Sstevel@tonic-gate 	}
123*7c478bd9Sstevel@tonic-gate 
124*7c478bd9Sstevel@tonic-gate 	while ((c = *s) != 0) {
125*7c478bd9Sstevel@tonic-gate 		if (!isspace(c))
126*7c478bd9Sstevel@tonic-gate 			bad_char = 1;
127*7c478bd9Sstevel@tonic-gate 		s++;
128*7c478bd9Sstevel@tonic-gate 	}
129*7c478bd9Sstevel@tonic-gate 
130*7c478bd9Sstevel@tonic-gate 	if (sign) {
131*7c478bd9Sstevel@tonic-gate 		if (neg) {
132*7c478bd9Sstevel@tonic-gate 			if (val > -(uint64_t)INT64_MIN)
133*7c478bd9Sstevel@tonic-gate 				overflow = 1;
134*7c478bd9Sstevel@tonic-gate 		} else {
135*7c478bd9Sstevel@tonic-gate 			if (val > INT64_MAX)
136*7c478bd9Sstevel@tonic-gate 				overflow = 1;
137*7c478bd9Sstevel@tonic-gate 		}
138*7c478bd9Sstevel@tonic-gate 	}
139*7c478bd9Sstevel@tonic-gate 
140*7c478bd9Sstevel@tonic-gate 	if (neg)
141*7c478bd9Sstevel@tonic-gate 		val = -val;
142*7c478bd9Sstevel@tonic-gate 
143*7c478bd9Sstevel@tonic-gate 	if (bad_char | bad_digit | overflow) {
144*7c478bd9Sstevel@tonic-gate 		if (bad_char)
145*7c478bd9Sstevel@tonic-gate 			uu_set_error(UU_ERROR_INVALID_CHAR);
146*7c478bd9Sstevel@tonic-gate 		else if (bad_digit)
147*7c478bd9Sstevel@tonic-gate 			uu_set_error(UU_ERROR_INVALID_DIGIT);
148*7c478bd9Sstevel@tonic-gate 		else if (overflow) {
149*7c478bd9Sstevel@tonic-gate 			if (neg)
150*7c478bd9Sstevel@tonic-gate 				uu_set_error(UU_ERROR_UNDERFLOW);
151*7c478bd9Sstevel@tonic-gate 			else
152*7c478bd9Sstevel@tonic-gate 				uu_set_error(UU_ERROR_OVERFLOW);
153*7c478bd9Sstevel@tonic-gate 		}
154*7c478bd9Sstevel@tonic-gate 		return (-1);
155*7c478bd9Sstevel@tonic-gate 	}
156*7c478bd9Sstevel@tonic-gate 
157*7c478bd9Sstevel@tonic-gate 	*out = val;
158*7c478bd9Sstevel@tonic-gate 	return (0);
159*7c478bd9Sstevel@tonic-gate }
160*7c478bd9Sstevel@tonic-gate 
161*7c478bd9Sstevel@tonic-gate int
uu_strtoint(const char * s,void * v,size_t sz,int base,int64_t min,int64_t max)162*7c478bd9Sstevel@tonic-gate uu_strtoint(const char *s, void *v, size_t sz, int base,
163*7c478bd9Sstevel@tonic-gate     int64_t min, int64_t max)
164*7c478bd9Sstevel@tonic-gate {
165*7c478bd9Sstevel@tonic-gate 	uint64_t val_u;
166*7c478bd9Sstevel@tonic-gate 	int64_t val;
167*7c478bd9Sstevel@tonic-gate 
168*7c478bd9Sstevel@tonic-gate 	if (min > max)
169*7c478bd9Sstevel@tonic-gate 		goto bad_argument;
170*7c478bd9Sstevel@tonic-gate 
171*7c478bd9Sstevel@tonic-gate 	switch (sz) {
172*7c478bd9Sstevel@tonic-gate 	case 1:
173*7c478bd9Sstevel@tonic-gate 		if (max > INT8_MAX || min < INT8_MIN)
174*7c478bd9Sstevel@tonic-gate 			goto bad_argument;
175*7c478bd9Sstevel@tonic-gate 		break;
176*7c478bd9Sstevel@tonic-gate 	case 2:
177*7c478bd9Sstevel@tonic-gate 		if (max > INT16_MAX || min < INT16_MIN)
178*7c478bd9Sstevel@tonic-gate 			goto bad_argument;
179*7c478bd9Sstevel@tonic-gate 		break;
180*7c478bd9Sstevel@tonic-gate 	case 4:
181*7c478bd9Sstevel@tonic-gate 		if (max > INT32_MAX || min < INT32_MIN)
182*7c478bd9Sstevel@tonic-gate 			goto bad_argument;
183*7c478bd9Sstevel@tonic-gate 		break;
184*7c478bd9Sstevel@tonic-gate 	case 8:
185*7c478bd9Sstevel@tonic-gate 		if (max > INT64_MAX || min < INT64_MIN)
186*7c478bd9Sstevel@tonic-gate 			goto bad_argument;
187*7c478bd9Sstevel@tonic-gate 		break;
188*7c478bd9Sstevel@tonic-gate 	default:
189*7c478bd9Sstevel@tonic-gate 		goto bad_argument;
190*7c478bd9Sstevel@tonic-gate 	}
191*7c478bd9Sstevel@tonic-gate 
192*7c478bd9Sstevel@tonic-gate 	if (min == 0 && max == 0) {
193*7c478bd9Sstevel@tonic-gate 		min = -(1ULL << (8 * sz - 1));
194*7c478bd9Sstevel@tonic-gate 		max = (1ULL << (8 * sz - 1)) - 1;
195*7c478bd9Sstevel@tonic-gate 	}
196*7c478bd9Sstevel@tonic-gate 
197*7c478bd9Sstevel@tonic-gate 	if (strtoint(s, &val_u, base, 1) == -1)
198*7c478bd9Sstevel@tonic-gate 		return (-1);
199*7c478bd9Sstevel@tonic-gate 
200*7c478bd9Sstevel@tonic-gate 	val = (int64_t)val_u;
201*7c478bd9Sstevel@tonic-gate 
202*7c478bd9Sstevel@tonic-gate 	if (val < min) {
203*7c478bd9Sstevel@tonic-gate 		uu_set_error(UU_ERROR_UNDERFLOW);
204*7c478bd9Sstevel@tonic-gate 		return (-1);
205*7c478bd9Sstevel@tonic-gate 	} else if (val > max) {
206*7c478bd9Sstevel@tonic-gate 		uu_set_error(UU_ERROR_OVERFLOW);
207*7c478bd9Sstevel@tonic-gate 		return (-1);
208*7c478bd9Sstevel@tonic-gate 	}
209*7c478bd9Sstevel@tonic-gate 
210*7c478bd9Sstevel@tonic-gate 	switch (sz) {
211*7c478bd9Sstevel@tonic-gate 	case 1:
212*7c478bd9Sstevel@tonic-gate 		*(int8_t *)v = val;
213*7c478bd9Sstevel@tonic-gate 		return (0);
214*7c478bd9Sstevel@tonic-gate 	case 2:
215*7c478bd9Sstevel@tonic-gate 		*(int16_t *)v = val;
216*7c478bd9Sstevel@tonic-gate 		return (0);
217*7c478bd9Sstevel@tonic-gate 	case 4:
218*7c478bd9Sstevel@tonic-gate 		*(int32_t *)v = val;
219*7c478bd9Sstevel@tonic-gate 		return (0);
220*7c478bd9Sstevel@tonic-gate 	case 8:
221*7c478bd9Sstevel@tonic-gate 		*(int64_t *)v = val;
222*7c478bd9Sstevel@tonic-gate 		return (0);
223*7c478bd9Sstevel@tonic-gate 	default:
224*7c478bd9Sstevel@tonic-gate 		break;		/* fall through to bad_argument */
225*7c478bd9Sstevel@tonic-gate 	}
226*7c478bd9Sstevel@tonic-gate 
227*7c478bd9Sstevel@tonic-gate bad_argument:
228*7c478bd9Sstevel@tonic-gate 	uu_set_error(UU_ERROR_INVALID_ARGUMENT);
229*7c478bd9Sstevel@tonic-gate 	return (-1);
230*7c478bd9Sstevel@tonic-gate }
231*7c478bd9Sstevel@tonic-gate 
232*7c478bd9Sstevel@tonic-gate int
uu_strtouint(const char * s,void * v,size_t sz,int base,uint64_t min,uint64_t max)233*7c478bd9Sstevel@tonic-gate uu_strtouint(const char *s, void *v, size_t sz, int base,
234*7c478bd9Sstevel@tonic-gate     uint64_t min, uint64_t max)
235*7c478bd9Sstevel@tonic-gate {
236*7c478bd9Sstevel@tonic-gate 	uint64_t val;
237*7c478bd9Sstevel@tonic-gate 
238*7c478bd9Sstevel@tonic-gate 	if (min > max)
239*7c478bd9Sstevel@tonic-gate 		goto bad_argument;
240*7c478bd9Sstevel@tonic-gate 
241*7c478bd9Sstevel@tonic-gate 	switch (sz) {
242*7c478bd9Sstevel@tonic-gate 	case 1:
243*7c478bd9Sstevel@tonic-gate 		if (max > UINT8_MAX)
244*7c478bd9Sstevel@tonic-gate 			goto bad_argument;
245*7c478bd9Sstevel@tonic-gate 		break;
246*7c478bd9Sstevel@tonic-gate 	case 2:
247*7c478bd9Sstevel@tonic-gate 		if (max > UINT16_MAX)
248*7c478bd9Sstevel@tonic-gate 			goto bad_argument;
249*7c478bd9Sstevel@tonic-gate 		break;
250*7c478bd9Sstevel@tonic-gate 	case 4:
251*7c478bd9Sstevel@tonic-gate 		if (max > UINT32_MAX)
252*7c478bd9Sstevel@tonic-gate 			goto bad_argument;
253*7c478bd9Sstevel@tonic-gate 		break;
254*7c478bd9Sstevel@tonic-gate 	case 8:
255*7c478bd9Sstevel@tonic-gate 		if (max > UINT64_MAX)
256*7c478bd9Sstevel@tonic-gate 			goto bad_argument;
257*7c478bd9Sstevel@tonic-gate 		break;
258*7c478bd9Sstevel@tonic-gate 	default:
259*7c478bd9Sstevel@tonic-gate 		goto bad_argument;
260*7c478bd9Sstevel@tonic-gate 	}
261*7c478bd9Sstevel@tonic-gate 
262*7c478bd9Sstevel@tonic-gate 	if (min == 0 && max == 0) {
263*7c478bd9Sstevel@tonic-gate 		/* we have to be careful, since << can overflow */
264*7c478bd9Sstevel@tonic-gate 		max = (1ULL << (8 * sz - 1)) * 2 - 1;
265*7c478bd9Sstevel@tonic-gate 	}
266*7c478bd9Sstevel@tonic-gate 
267*7c478bd9Sstevel@tonic-gate 	if (strtoint(s, &val, base, 0) == -1)
268*7c478bd9Sstevel@tonic-gate 		return (-1);
269*7c478bd9Sstevel@tonic-gate 
270*7c478bd9Sstevel@tonic-gate 	if (val < min) {
271*7c478bd9Sstevel@tonic-gate 		uu_set_error(UU_ERROR_UNDERFLOW);
272*7c478bd9Sstevel@tonic-gate 		return (-1);
273*7c478bd9Sstevel@tonic-gate 	} else if (val > max) {
274*7c478bd9Sstevel@tonic-gate 		uu_set_error(UU_ERROR_OVERFLOW);
275*7c478bd9Sstevel@tonic-gate 		return (-1);
276*7c478bd9Sstevel@tonic-gate 	}
277*7c478bd9Sstevel@tonic-gate 
278*7c478bd9Sstevel@tonic-gate 	switch (sz) {
279*7c478bd9Sstevel@tonic-gate 	case 1:
280*7c478bd9Sstevel@tonic-gate 		*(uint8_t *)v = val;
281*7c478bd9Sstevel@tonic-gate 		return (0);
282*7c478bd9Sstevel@tonic-gate 	case 2:
283*7c478bd9Sstevel@tonic-gate 		*(uint16_t *)v = val;
284*7c478bd9Sstevel@tonic-gate 		return (0);
285*7c478bd9Sstevel@tonic-gate 	case 4:
286*7c478bd9Sstevel@tonic-gate 		*(uint32_t *)v = val;
287*7c478bd9Sstevel@tonic-gate 		return (0);
288*7c478bd9Sstevel@tonic-gate 	case 8:
289*7c478bd9Sstevel@tonic-gate 		*(uint64_t *)v = val;
290*7c478bd9Sstevel@tonic-gate 		return (0);
291*7c478bd9Sstevel@tonic-gate 	default:
292*7c478bd9Sstevel@tonic-gate 		break;		/* shouldn't happen, fall through */
293*7c478bd9Sstevel@tonic-gate 	}
294*7c478bd9Sstevel@tonic-gate 
295*7c478bd9Sstevel@tonic-gate bad_argument:
296*7c478bd9Sstevel@tonic-gate 	uu_set_error(UU_ERROR_INVALID_ARGUMENT);
297*7c478bd9Sstevel@tonic-gate 	return (-1);
298*7c478bd9Sstevel@tonic-gate }
299