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