xref: /illumos-gate/usr/src/common/util/strtoull.c (revision 6ffde572)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
57257d1b4Sraf  * Common Development and Distribution License (the "License").
67257d1b4Sraf  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
217257d1b4Sraf 
227c478bd9Sstevel@tonic-gate /*
232ef9abdcSjv  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
247c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
257c478bd9Sstevel@tonic-gate  */
267c478bd9Sstevel@tonic-gate 
277c478bd9Sstevel@tonic-gate /*	Copyright (c) 1988 AT&T	*/
2828de4f3cSToomas Soome /*	  All Rights Reserved	*/
297c478bd9Sstevel@tonic-gate 
30*6ffde572SToomas Soome #if	defined(_KERNEL)
3128de4f3cSToomas Soome #include <sys/null.h>
32*6ffde572SToomas Soome #endif	/* _KERNEL */
33*6ffde572SToomas Soome 
34*6ffde572SToomas Soome #if	defined(_KERNEL) && !defined(_BOOT)
352ef9abdcSjv #include <sys/errno.h>
362ef9abdcSjv #else	/* _KERNEL && !_BOOT */
3728de4f3cSToomas Soome #if	!defined(_BOOT) && !defined(_KMDB) && !defined(_STANDALONE)
387257d1b4Sraf #include "lint.h"
3928de4f3cSToomas Soome #endif	/* !_BOOT && !_KMDB && !_STANDALONE */
4028de4f3cSToomas Soome #if	defined(_STANDALONE)
4128de4f3cSToomas Soome #include <sys/cdefs.h>
4228de4f3cSToomas Soome #include <stand.h>
4328de4f3cSToomas Soome #include <limits.h>
4428de4f3cSToomas Soome 
4528de4f3cSToomas Soome typedef unsigned long long u_longlong_t;
4628de4f3cSToomas Soome #else
477c478bd9Sstevel@tonic-gate #include <errno.h>
487c478bd9Sstevel@tonic-gate #include <ctype.h>
497c478bd9Sstevel@tonic-gate #include <limits.h>
507c478bd9Sstevel@tonic-gate #include <stdlib.h>
5128de4f3cSToomas Soome #endif	/* _STANDALONE */
522ef9abdcSjv #endif	/* _KERNEL && !_BOOT */
532ef9abdcSjv #include "strtolctype.h"
542ef9abdcSjv #include <sys/types.h>
557c478bd9Sstevel@tonic-gate 
562ef9abdcSjv #if	defined(_KERNEL) && !defined(_BOOT)
572ef9abdcSjv int
ddi_strtoull(const char * str,char ** nptr,int base,u_longlong_t * result)582ef9abdcSjv ddi_strtoull(const char *str, char **nptr, int base, u_longlong_t *result)
592ef9abdcSjv #else	/* _KERNEL && !_BOOT */
607c478bd9Sstevel@tonic-gate u_longlong_t
617c478bd9Sstevel@tonic-gate strtoull(const char *str, char **nptr, int base)
622ef9abdcSjv #endif	/* _KERNEL && !_BOOT */
637c478bd9Sstevel@tonic-gate {
647c478bd9Sstevel@tonic-gate 	u_longlong_t val;
657c478bd9Sstevel@tonic-gate 	int c;
667c478bd9Sstevel@tonic-gate 	int xx;
677c478bd9Sstevel@tonic-gate 	int neg = 0;
682ef9abdcSjv 	u_longlong_t multmax;
692ef9abdcSjv 	const char **ptr = (const char **)nptr;
702ef9abdcSjv 	const unsigned char *ustr = (const unsigned char *)str;
717c478bd9Sstevel@tonic-gate 
7228de4f3cSToomas Soome 	if (ptr != NULL)
737c478bd9Sstevel@tonic-gate 		*ptr = (char *)ustr; /* in case no number is formed */
747c478bd9Sstevel@tonic-gate 	if (base < 0 || base > MBASE || base == 1) {
752ef9abdcSjv 		/* base is invalid -- should be a fatal error */
762ef9abdcSjv #if	defined(_KERNEL) && !defined(_BOOT)
772ef9abdcSjv 		return (EINVAL);
782ef9abdcSjv #else	/* _KERNEL && !_BOOT */
797c478bd9Sstevel@tonic-gate 		errno = EINVAL;
802ef9abdcSjv 		return (0);
812ef9abdcSjv #endif	/* _KERNEL && !_BOOT */
827c478bd9Sstevel@tonic-gate 	}
837c478bd9Sstevel@tonic-gate 	if (!isalnum(c = *ustr)) {
847c478bd9Sstevel@tonic-gate 		while (isspace(c))
857c478bd9Sstevel@tonic-gate 			c = *++ustr;
867c478bd9Sstevel@tonic-gate 		switch (c) {
877c478bd9Sstevel@tonic-gate 		case '-':
887c478bd9Sstevel@tonic-gate 			neg++;
897c478bd9Sstevel@tonic-gate 			/* FALLTHROUGH */
907c478bd9Sstevel@tonic-gate 		case '+':
917c478bd9Sstevel@tonic-gate 			c = *++ustr;
927c478bd9Sstevel@tonic-gate 		}
937c478bd9Sstevel@tonic-gate 	}
9428de4f3cSToomas Soome 	if (base == 0) {
957c478bd9Sstevel@tonic-gate 		if (c != '0')
967c478bd9Sstevel@tonic-gate 			base = 10;
977c478bd9Sstevel@tonic-gate 		else if (ustr[1] == 'x' || ustr[1] == 'X')
987c478bd9Sstevel@tonic-gate 			base = 16;
997c478bd9Sstevel@tonic-gate 		else
1007c478bd9Sstevel@tonic-gate 			base = 8;
10128de4f3cSToomas Soome 	}
1027c478bd9Sstevel@tonic-gate 	/*
1037c478bd9Sstevel@tonic-gate 	 * for any base > 10, the digits incrementally following
1047c478bd9Sstevel@tonic-gate 	 *	9 are assumed to be "abc...z" or "ABC...Z"
1057c478bd9Sstevel@tonic-gate 	 */
1062ef9abdcSjv 	if (!lisalnum(c) || (xx = DIGIT(c)) >= base) {
1072ef9abdcSjv 		/* no number formed */
1082ef9abdcSjv #if	defined(_KERNEL) && !defined(_BOOT)
1092ef9abdcSjv 		return (EINVAL);
1102ef9abdcSjv #else	/* _KERNEL && !_BOOT */
1112ef9abdcSjv 		return (0);
1122ef9abdcSjv #endif	/* _KERNEL && !_BOOT */
1132ef9abdcSjv 	}
1147c478bd9Sstevel@tonic-gate 	if (base == 16 && c == '0' && (ustr[1] == 'x' || ustr[1] == 'X') &&
1157257d1b4Sraf 	    isxdigit(ustr[2]))
1167c478bd9Sstevel@tonic-gate 		c = *(ustr += 2); /* skip over leading "0x" or "0X" */
1177c478bd9Sstevel@tonic-gate 
1182ef9abdcSjv 	multmax = ULLONG_MAX / (u_longlong_t)base;
1197c478bd9Sstevel@tonic-gate 	val = DIGIT(c);
1207c478bd9Sstevel@tonic-gate 	for (c = *++ustr; lisalnum(c) && (xx = DIGIT(c)) < base; ) {
1217c478bd9Sstevel@tonic-gate 		if (val > multmax)
1227c478bd9Sstevel@tonic-gate 			goto overflow;
1237c478bd9Sstevel@tonic-gate 		val *= base;
12428de4f3cSToomas Soome 		if (ULLONG_MAX - val < (unsigned long long)xx)
1257c478bd9Sstevel@tonic-gate 			goto overflow;
1267c478bd9Sstevel@tonic-gate 		val += xx;
1277c478bd9Sstevel@tonic-gate 		c = *++ustr;
1287c478bd9Sstevel@tonic-gate 	}
12928de4f3cSToomas Soome 	if (ptr != NULL)
1307c478bd9Sstevel@tonic-gate 		*ptr = (char *)ustr;
1312ef9abdcSjv #if	defined(_KERNEL) && !defined(_BOOT)
1322ef9abdcSjv 	*result = neg ? -val : val;
1332ef9abdcSjv 	return (0);
1342ef9abdcSjv #else	/* _KERNEL && !_BOOT */
1357c478bd9Sstevel@tonic-gate 	return (neg ? -val : val);
1362ef9abdcSjv #endif	/* _KERNEL && !_BOOT */
1377c478bd9Sstevel@tonic-gate 
1387c478bd9Sstevel@tonic-gate overflow:
1397c478bd9Sstevel@tonic-gate 	for (c = *++ustr; lisalnum(c) && (xx = DIGIT(c)) < base; (c = *++ustr))
1407c478bd9Sstevel@tonic-gate 		;
14128de4f3cSToomas Soome 	if (ptr != NULL)
1427c478bd9Sstevel@tonic-gate 		*ptr = (char *)ustr;
1432ef9abdcSjv #if	defined(_KERNEL) && !defined(_BOOT)
1442ef9abdcSjv 	return (ERANGE);
1452ef9abdcSjv #else	/* _KERNEL && !_BOOT */
1467c478bd9Sstevel@tonic-gate 	errno = ERANGE;
1472ef9abdcSjv 	return (ULLONG_MAX);
1482ef9abdcSjv #endif	/* _KERNEL && !_BOOT */
1497c478bd9Sstevel@tonic-gate }
150