xref: /illumos-gate/usr/src/common/util/strtoull.c (revision 20a7641f9918de8574b8b3b47dbe35c4bfc78df1)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 
22 /*
23  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 /*	Copyright (c) 1988 AT&T	*/
28 /*	  All Rights Reserved	*/
29 
30 #if	defined(_KERNEL)
31 #include <sys/null.h>
32 #endif	/* _KERNEL */
33 
34 #if	defined(_KERNEL) && !defined(_BOOT)
35 #include <sys/errno.h>
36 #else	/* _KERNEL && !_BOOT */
37 #if	!defined(_BOOT) && !defined(_KMDB) && !defined(_STANDALONE)
38 #include "lint.h"
39 #endif	/* !_BOOT && !_KMDB && !_STANDALONE */
40 #if	defined(_STANDALONE)
41 #include <sys/cdefs.h>
42 #include <stand.h>
43 #include <limits.h>
44 
45 typedef unsigned long long u_longlong_t;
46 #else
47 #include <errno.h>
48 #include <ctype.h>
49 #include <limits.h>
50 #include <stdlib.h>
51 #endif	/* _STANDALONE */
52 #endif	/* _KERNEL && !_BOOT */
53 #include "strtolctype.h"
54 #include <sys/types.h>
55 
56 #if	defined(_KERNEL) && !defined(_BOOT)
57 int
58 ddi_strtoull(const char *str, char **nptr, int base, u_longlong_t *result)
59 #else	/* _KERNEL && !_BOOT */
60 u_longlong_t
61 strtoull(const char *str, char **nptr, int base)
62 #endif	/* _KERNEL && !_BOOT */
63 {
64 	u_longlong_t val;
65 	int c;
66 	int xx;
67 	int neg = 0;
68 	u_longlong_t multmax;
69 	const char **ptr = (const char **)nptr;
70 	const unsigned char *ustr = (const unsigned char *)str;
71 
72 	if (ptr != NULL)
73 		*ptr = (char *)ustr; /* in case no number is formed */
74 	if (base < 0 || base > MBASE || base == 1) {
75 		/* base is invalid -- should be a fatal error */
76 #if	defined(_KERNEL) && !defined(_BOOT)
77 		return (EINVAL);
78 #else	/* _KERNEL && !_BOOT */
79 		errno = EINVAL;
80 		return (0);
81 #endif	/* _KERNEL && !_BOOT */
82 	}
83 	if (!isalnum(c = *ustr)) {
84 		while (isspace(c))
85 			c = *++ustr;
86 		switch (c) {
87 		case '-':
88 			neg++;
89 			/* FALLTHROUGH */
90 		case '+':
91 			c = *++ustr;
92 		}
93 	}
94 	if (base == 0) {
95 		if (c != '0')
96 			base = 10;
97 		else if (ustr[1] == 'x' || ustr[1] == 'X')
98 			base = 16;
99 		else
100 			base = 8;
101 	}
102 	/*
103 	 * for any base > 10, the digits incrementally following
104 	 *	9 are assumed to be "abc...z" or "ABC...Z"
105 	 */
106 	if (!lisalnum(c) || (xx = DIGIT(c)) >= base) {
107 		/* no number formed */
108 #if	defined(_KERNEL) && !defined(_BOOT)
109 		return (EINVAL);
110 #else	/* _KERNEL && !_BOOT */
111 		return (0);
112 #endif	/* _KERNEL && !_BOOT */
113 	}
114 	if (base == 16 && c == '0' && (ustr[1] == 'x' || ustr[1] == 'X') &&
115 	    isxdigit(ustr[2]))
116 		c = *(ustr += 2); /* skip over leading "0x" or "0X" */
117 
118 	multmax = ULLONG_MAX / (u_longlong_t)base;
119 	val = DIGIT(c);
120 	for (c = *++ustr; lisalnum(c) && (xx = DIGIT(c)) < base; ) {
121 		if (val > multmax)
122 			goto overflow;
123 		val *= base;
124 		if (ULLONG_MAX - val < (unsigned long long)xx)
125 			goto overflow;
126 		val += xx;
127 		c = *++ustr;
128 	}
129 	if (ptr != NULL)
130 		*ptr = (char *)ustr;
131 #if	defined(_KERNEL) && !defined(_BOOT)
132 	*result = neg ? -val : val;
133 	return (0);
134 #else	/* _KERNEL && !_BOOT */
135 	return (neg ? -val : val);
136 #endif	/* _KERNEL && !_BOOT */
137 
138 overflow:
139 	for (c = *++ustr; lisalnum(c) && (xx = DIGIT(c)) < base; (c = *++ustr))
140 		;
141 	if (ptr != NULL)
142 		*ptr = (char *)ustr;
143 #if	defined(_KERNEL) && !defined(_BOOT)
144 	return (ERANGE);
145 #else	/* _KERNEL && !_BOOT */
146 	errno = ERANGE;
147 	return (ULLONG_MAX);
148 #endif	/* _KERNEL && !_BOOT */
149 }
150