17c478bdstevel@tonic-gate/*
27c478bdstevel@tonic-gate * CDDL HEADER START
37c478bdstevel@tonic-gate *
47c478bdstevel@tonic-gate * The contents of this file are subject to the terms of the
57257d1braf * Common Development and Distribution License (the "License").
67257d1braf * You may not use this file except in compliance with the License.
77c478bdstevel@tonic-gate *
87c478bdstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bdstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
107c478bdstevel@tonic-gate * See the License for the specific language governing permissions
117c478bdstevel@tonic-gate * and limitations under the License.
127c478bdstevel@tonic-gate *
137c478bdstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
147c478bdstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bdstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
167c478bdstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
177c478bdstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bdstevel@tonic-gate *
197c478bdstevel@tonic-gate * CDDL HEADER END
207c478bdstevel@tonic-gate */
217257d1braf
227c478bdstevel@tonic-gate/*
232ef9abdjv * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
247c478bdstevel@tonic-gate * Use is subject to license terms.
257c478bdstevel@tonic-gate */
267c478bdstevel@tonic-gate
277c478bdstevel@tonic-gate/*	Copyright (c) 1988 AT&T	*/
287c478bdstevel@tonic-gate/*	  All Rights Reserved  	*/
297c478bdstevel@tonic-gate
302ef9abdjv#if	defined(_KERNEL) && !defined(_BOOT)
312ef9abdjv#include <sys/errno.h>
322ef9abdjv#else	/* _KERNEL && !_BOOT */
332ef9abdjv#if	!defined(_BOOT) && !defined(_KMDB)
347257d1braf#include "lint.h"
352ef9abdjv#endif	/* !_BOOT && !_KMDB */
367c478bdstevel@tonic-gate#include <errno.h>
377c478bdstevel@tonic-gate#include <ctype.h>
387c478bdstevel@tonic-gate#include <limits.h>
397c478bdstevel@tonic-gate#include <stdlib.h>
402ef9abdjv#endif	/* _KERNEL && !_BOOT */
412ef9abdjv#include "strtolctype.h"
422ef9abdjv#include <sys/types.h>
437c478bdstevel@tonic-gate
442ef9abdjv#if	defined(_KERNEL) && !defined(_BOOT)
452ef9abdjvint
462ef9abdjvddi_strtoull(const char *str, char **nptr, int base, u_longlong_t *result)
472ef9abdjv#else	/* _KERNEL && !_BOOT */
487c478bdstevel@tonic-gateu_longlong_t
497c478bdstevel@tonic-gatestrtoull(const char *str, char **nptr, int base)
502ef9abdjv#endif	/* _KERNEL && !_BOOT */
517c478bdstevel@tonic-gate{
527c478bdstevel@tonic-gate	u_longlong_t val;
537c478bdstevel@tonic-gate	int c;
547c478bdstevel@tonic-gate	int xx;
557c478bdstevel@tonic-gate	int neg = 0;
562ef9abdjv	u_longlong_t multmax;
572ef9abdjv	const char **ptr = (const char **)nptr;
582ef9abdjv	const unsigned char *ustr = (const unsigned char *)str;
597c478bdstevel@tonic-gate
607c478bdstevel@tonic-gate	if (ptr != (const char **)0)
617c478bdstevel@tonic-gate		*ptr = (char *)ustr; /* in case no number is formed */
627c478bdstevel@tonic-gate	if (base < 0 || base > MBASE || base == 1) {
632ef9abdjv		/* base is invalid -- should be a fatal error */
642ef9abdjv#if	defined(_KERNEL) && !defined(_BOOT)
652ef9abdjv		return (EINVAL);
662ef9abdjv#else	/* _KERNEL && !_BOOT */
677c478bdstevel@tonic-gate		errno = EINVAL;
682ef9abdjv		return (0);
692ef9abdjv#endif	/* _KERNEL && !_BOOT */
707c478bdstevel@tonic-gate	}
717c478bdstevel@tonic-gate	if (!isalnum(c = *ustr)) {
727c478bdstevel@tonic-gate		while (isspace(c))
737c478bdstevel@tonic-gate			c = *++ustr;
747c478bdstevel@tonic-gate		switch (c) {
757c478bdstevel@tonic-gate		case '-':
767c478bdstevel@tonic-gate			neg++;
777c478bdstevel@tonic-gate			/* FALLTHROUGH */
787c478bdstevel@tonic-gate		case '+':
797c478bdstevel@tonic-gate			c = *++ustr;
807c478bdstevel@tonic-gate		}
817c478bdstevel@tonic-gate	}
827c478bdstevel@tonic-gate	if (base == 0)
837c478bdstevel@tonic-gate		if (c != '0')
847c478bdstevel@tonic-gate			base = 10;
857c478bdstevel@tonic-gate		else if (ustr[1] == 'x' || ustr[1] == 'X')
867c478bdstevel@tonic-gate			base = 16;
877c478bdstevel@tonic-gate		else
887c478bdstevel@tonic-gate			base = 8;
897c478bdstevel@tonic-gate	/*
907c478bdstevel@tonic-gate	 * for any base > 10, the digits incrementally following
917c478bdstevel@tonic-gate	 *	9 are assumed to be "abc...z" or "ABC...Z"
927c478bdstevel@tonic-gate	 */
932ef9abdjv	if (!lisalnum(c) || (xx = DIGIT(c)) >= base) {
942ef9abdjv		/* no number formed */
952ef9abdjv#if	defined(_KERNEL) && !defined(_BOOT)
962ef9abdjv		return (EINVAL);
972ef9abdjv#else	/* _KERNEL && !_BOOT */
982ef9abdjv		return (0);
992ef9abdjv#endif	/* _KERNEL && !_BOOT */
1002ef9abdjv	}
1017c478bdstevel@tonic-gate	if (base == 16 && c == '0' && (ustr[1] == 'x' || ustr[1] == 'X') &&
1027257d1braf	    isxdigit(ustr[2]))
1037c478bdstevel@tonic-gate		c = *(ustr += 2); /* skip over leading "0x" or "0X" */
1047c478bdstevel@tonic-gate
1052ef9abdjv	multmax = ULLONG_MAX / (u_longlong_t)base;
1067c478bdstevel@tonic-gate	val = DIGIT(c);
1077c478bdstevel@tonic-gate	for (c = *++ustr; lisalnum(c) && (xx = DIGIT(c)) < base; ) {
1087c478bdstevel@tonic-gate		if (val > multmax)
1097c478bdstevel@tonic-gate			goto overflow;
1107c478bdstevel@tonic-gate		val *= base;
1112ef9abdjv		if (ULLONG_MAX - val < xx)
1127c478bdstevel@tonic-gate			goto overflow;
1137c478bdstevel@tonic-gate		val += xx;
1147c478bdstevel@tonic-gate		c = *++ustr;
1157c478bdstevel@tonic-gate	}
1167c478bdstevel@tonic-gate	if (ptr != (const char **)0)
1177c478bdstevel@tonic-gate		*ptr = (char *)ustr;
1182ef9abdjv#if	defined(_KERNEL) && !defined(_BOOT)
1192ef9abdjv	*result = neg ? -val : val;
1202ef9abdjv	return (0);
1212ef9abdjv#else	/* _KERNEL && !_BOOT */
1227c478bdstevel@tonic-gate	return (neg ? -val : val);
1232ef9abdjv#endif	/* _KERNEL && !_BOOT */
1247c478bdstevel@tonic-gate
1257c478bdstevel@tonic-gateoverflow:
1267c478bdstevel@tonic-gate	for (c = *++ustr; lisalnum(c) && (xx = DIGIT(c)) < base; (c = *++ustr))
1277c478bdstevel@tonic-gate		;
1287c478bdstevel@tonic-gate	if (ptr != (const char **)0)
1297c478bdstevel@tonic-gate		*ptr = (char *)ustr;
1302ef9abdjv#if	defined(_KERNEL) && !defined(_BOOT)
1312ef9abdjv	return (ERANGE);
1322ef9abdjv#else	/* _KERNEL && !_BOOT */
1337c478bdstevel@tonic-gate	errno = ERANGE;
1342ef9abdjv	return (ULLONG_MAX);
1352ef9abdjv#endif	/* _KERNEL && !_BOOT */
1367c478bdstevel@tonic-gate}
137