xref: /illumos-gate/usr/src/common/util/strtoull.c (revision 28de4f3c)
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) && !defined(_BOOT)
31 #include <sys/null.h>
32 #include <sys/errno.h>
33 #else	/* _KERNEL && !_BOOT */
34 #if	!defined(_BOOT) && !defined(_KMDB) && !defined(_STANDALONE)
35 #include "lint.h"
36 #endif	/* !_BOOT && !_KMDB && !_STANDALONE */
37 #if	defined(_STANDALONE)
38 #include <sys/cdefs.h>
39 #include <stand.h>
40 #include <limits.h>
41 
42 typedef unsigned long long u_longlong_t;
43 #else
44 #include <errno.h>
45 #include <ctype.h>
46 #include <limits.h>
47 #include <stdlib.h>
48 #endif	/* _STANDALONE */
49 #endif	/* _KERNEL && !_BOOT */
50 #include "strtolctype.h"
51 #include <sys/types.h>
52 
53 #if	defined(_KERNEL) && !defined(_BOOT)
54 int
55 ddi_strtoull(const char *str, char **nptr, int base, u_longlong_t *result)
56 #else	/* _KERNEL && !_BOOT */
57 u_longlong_t
58 strtoull(const char *str, char **nptr, int base)
59 #endif	/* _KERNEL && !_BOOT */
60 {
61 	u_longlong_t val;
62 	int c;
63 	int xx;
64 	int neg = 0;
65 	u_longlong_t multmax;
66 	const char **ptr = (const char **)nptr;
67 	const unsigned char *ustr = (const unsigned char *)str;
68 
69 	if (ptr != NULL)
70 		*ptr = (char *)ustr; /* in case no number is formed */
71 	if (base < 0 || base > MBASE || base == 1) {
72 		/* base is invalid -- should be a fatal error */
73 #if	defined(_KERNEL) && !defined(_BOOT)
74 		return (EINVAL);
75 #else	/* _KERNEL && !_BOOT */
76 		errno = EINVAL;
77 		return (0);
78 #endif	/* _KERNEL && !_BOOT */
79 	}
80 	if (!isalnum(c = *ustr)) {
81 		while (isspace(c))
82 			c = *++ustr;
83 		switch (c) {
84 		case '-':
85 			neg++;
86 			/* FALLTHROUGH */
87 		case '+':
88 			c = *++ustr;
89 		}
90 	}
91 	if (base == 0) {
92 		if (c != '0')
93 			base = 10;
94 		else if (ustr[1] == 'x' || ustr[1] == 'X')
95 			base = 16;
96 		else
97 			base = 8;
98 	}
99 	/*
100 	 * for any base > 10, the digits incrementally following
101 	 *	9 are assumed to be "abc...z" or "ABC...Z"
102 	 */
103 	if (!lisalnum(c) || (xx = DIGIT(c)) >= base) {
104 		/* no number formed */
105 #if	defined(_KERNEL) && !defined(_BOOT)
106 		return (EINVAL);
107 #else	/* _KERNEL && !_BOOT */
108 		return (0);
109 #endif	/* _KERNEL && !_BOOT */
110 	}
111 	if (base == 16 && c == '0' && (ustr[1] == 'x' || ustr[1] == 'X') &&
112 	    isxdigit(ustr[2]))
113 		c = *(ustr += 2); /* skip over leading "0x" or "0X" */
114 
115 	multmax = ULLONG_MAX / (u_longlong_t)base;
116 	val = DIGIT(c);
117 	for (c = *++ustr; lisalnum(c) && (xx = DIGIT(c)) < base; ) {
118 		if (val > multmax)
119 			goto overflow;
120 		val *= base;
121 		if (ULLONG_MAX - val < (unsigned long long)xx)
122 			goto overflow;
123 		val += xx;
124 		c = *++ustr;
125 	}
126 	if (ptr != NULL)
127 		*ptr = (char *)ustr;
128 #if	defined(_KERNEL) && !defined(_BOOT)
129 	*result = neg ? -val : val;
130 	return (0);
131 #else	/* _KERNEL && !_BOOT */
132 	return (neg ? -val : val);
133 #endif	/* _KERNEL && !_BOOT */
134 
135 overflow:
136 	for (c = *++ustr; lisalnum(c) && (xx = DIGIT(c)) < base; (c = *++ustr))
137 		;
138 	if (ptr != NULL)
139 		*ptr = (char *)ustr;
140 #if	defined(_KERNEL) && !defined(_BOOT)
141 	return (ERANGE);
142 #else	/* _KERNEL && !_BOOT */
143 	errno = ERANGE;
144 	return (ULLONG_MAX);
145 #endif	/* _KERNEL && !_BOOT */
146 }
147