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 */ 217c478bd9Sstevel@tonic-gate 227c478bd9Sstevel@tonic-gate /* 232ef9abdcSjv227347 * Copyright 2009 Sun Microsystems, Inc. All rights reserved. 247c478bd9Sstevel@tonic-gate * Use is subject to license terms. 257c478bd9Sstevel@tonic-gate */ 267c478bd9Sstevel@tonic-gate 277257d1b4Sraf /* Copyright (c) 1988 AT&T */ 287257d1b4Sraf /* All Rights Reserved */ 297257d1b4Sraf 302ef9abdcSjv227347 #if defined(_KERNEL) && !defined(_BOOT) 31*28de4f3cSToomas Soome #include <sys/null.h> 322ef9abdcSjv227347 #include <sys/errno.h> 332ef9abdcSjv227347 #else /* _KERNEL && !_BOOT */ 34*28de4f3cSToomas Soome #if !defined(_BOOT) && !defined(_KMDB) && !defined(_STANDALONE) 357257d1b4Sraf #include "lint.h" 36*28de4f3cSToomas Soome #endif /* !_BOOT && !_KMDB && !_STANDALONE */ 37*28de4f3cSToomas Soome #if defined(_STANDALONE) 38*28de4f3cSToomas Soome #include <sys/cdefs.h> 39*28de4f3cSToomas Soome #include <stand.h> 40*28de4f3cSToomas Soome #include <limits.h> 41*28de4f3cSToomas Soome #else 427c478bd9Sstevel@tonic-gate #include <errno.h> 437c478bd9Sstevel@tonic-gate #include <ctype.h> 447c478bd9Sstevel@tonic-gate #include <limits.h> 457c478bd9Sstevel@tonic-gate #include <stdlib.h> 46*28de4f3cSToomas Soome #endif /* _STANDALONE */ 472ef9abdcSjv227347 #endif /* _KERNEL && !_BOOT */ 482ef9abdcSjv227347 #include "strtolctype.h" 497c478bd9Sstevel@tonic-gate #include <sys/types.h> 507c478bd9Sstevel@tonic-gate 512ef9abdcSjv227347 #if defined(_KERNEL) && !defined(_BOOT) 522ef9abdcSjv227347 int 532ef9abdcSjv227347 ddi_strtoul(const char *str, char **nptr, int base, unsigned long *result) 542ef9abdcSjv227347 #else /* _KERNEL && !_BOOT */ 557c478bd9Sstevel@tonic-gate unsigned long 567c478bd9Sstevel@tonic-gate strtoul(const char *str, char **nptr, int base) 572ef9abdcSjv227347 #endif /* _KERNEL && !_BOOT */ 587c478bd9Sstevel@tonic-gate { 597c478bd9Sstevel@tonic-gate unsigned long val; 607c478bd9Sstevel@tonic-gate int c; 617c478bd9Sstevel@tonic-gate int xx; 627c478bd9Sstevel@tonic-gate int neg = 0; 632ef9abdcSjv227347 unsigned long multmax; 647c478bd9Sstevel@tonic-gate const char **ptr = (const char **)nptr; 657c478bd9Sstevel@tonic-gate const unsigned char *ustr = (const unsigned char *)str; 667c478bd9Sstevel@tonic-gate 67*28de4f3cSToomas Soome if (ptr != NULL) 687c478bd9Sstevel@tonic-gate *ptr = (char *)ustr; /* in case no number is formed */ 697c478bd9Sstevel@tonic-gate if (base < 0 || base > MBASE || base == 1) { 702ef9abdcSjv227347 /* base is invalid -- should be a fatal error */ 712ef9abdcSjv227347 #if defined(_KERNEL) && !defined(_BOOT) 722ef9abdcSjv227347 return (EINVAL); 732ef9abdcSjv227347 #else /* _KERNEL && !_BOOT */ 747c478bd9Sstevel@tonic-gate errno = EINVAL; 752ef9abdcSjv227347 return (0); 762ef9abdcSjv227347 #endif /* _KERNEL && !_BOOT */ 777c478bd9Sstevel@tonic-gate } 787c478bd9Sstevel@tonic-gate if (!isalnum(c = *ustr)) { 797c478bd9Sstevel@tonic-gate while (isspace(c)) 807c478bd9Sstevel@tonic-gate c = *++ustr; 817c478bd9Sstevel@tonic-gate switch (c) { 827c478bd9Sstevel@tonic-gate case '-': 837c478bd9Sstevel@tonic-gate neg++; 847c478bd9Sstevel@tonic-gate /* FALLTHROUGH */ 857c478bd9Sstevel@tonic-gate case '+': 867c478bd9Sstevel@tonic-gate c = *++ustr; 877c478bd9Sstevel@tonic-gate } 887c478bd9Sstevel@tonic-gate } 89*28de4f3cSToomas Soome if (base == 0) { 907c478bd9Sstevel@tonic-gate if (c != '0') 917c478bd9Sstevel@tonic-gate base = 10; 927c478bd9Sstevel@tonic-gate else if (ustr[1] == 'x' || ustr[1] == 'X') 937c478bd9Sstevel@tonic-gate base = 16; 947c478bd9Sstevel@tonic-gate else 957c478bd9Sstevel@tonic-gate base = 8; 96*28de4f3cSToomas Soome } 977c478bd9Sstevel@tonic-gate /* 987c478bd9Sstevel@tonic-gate * for any base > 10, the digits incrementally following 997c478bd9Sstevel@tonic-gate * 9 are assumed to be "abc...z" or "ABC...Z" 1007c478bd9Sstevel@tonic-gate */ 1012ef9abdcSjv227347 if (!lisalnum(c) || (xx = DIGIT(c)) >= base) { 1022ef9abdcSjv227347 /* no number formed */ 1032ef9abdcSjv227347 #if defined(_KERNEL) && !defined(_BOOT) 1042ef9abdcSjv227347 return (EINVAL); 1052ef9abdcSjv227347 #else /* _KERNEL && !_BOOT */ 1062ef9abdcSjv227347 return (0); 1072ef9abdcSjv227347 #endif /* _KERNEL && !_BOOT */ 1082ef9abdcSjv227347 } 1097c478bd9Sstevel@tonic-gate if (base == 16 && c == '0' && (ustr[1] == 'x' || ustr[1] == 'X') && 1107c478bd9Sstevel@tonic-gate isxdigit(ustr[2])) 1117c478bd9Sstevel@tonic-gate c = *(ustr += 2); /* skip over leading "0x" or "0X" */ 1127c478bd9Sstevel@tonic-gate 1137c478bd9Sstevel@tonic-gate multmax = ULONG_MAX / (unsigned long)base; 1147c478bd9Sstevel@tonic-gate val = DIGIT(c); 1157c478bd9Sstevel@tonic-gate for (c = *++ustr; lisalnum(c) && (xx = DIGIT(c)) < base; ) { 1167c478bd9Sstevel@tonic-gate if (val > multmax) 1177c478bd9Sstevel@tonic-gate goto overflow; 1187c478bd9Sstevel@tonic-gate val *= base; 119*28de4f3cSToomas Soome if (ULONG_MAX - val < (unsigned long)xx) 1207c478bd9Sstevel@tonic-gate goto overflow; 1217c478bd9Sstevel@tonic-gate val += xx; 1227c478bd9Sstevel@tonic-gate c = *++ustr; 1237c478bd9Sstevel@tonic-gate } 124*28de4f3cSToomas Soome if (ptr != NULL) 1257c478bd9Sstevel@tonic-gate *ptr = (char *)ustr; 1262ef9abdcSjv227347 #if defined(_KERNEL) && !defined(_BOOT) 1272ef9abdcSjv227347 *result = neg ? -val : val; 1282ef9abdcSjv227347 return (0); 1292ef9abdcSjv227347 #else /* _KERNEL && !_BOOT */ 1307c478bd9Sstevel@tonic-gate return (neg ? -val : val); 1312ef9abdcSjv227347 #endif /* _KERNEL && !_BOOT */ 1327c478bd9Sstevel@tonic-gate 1337c478bd9Sstevel@tonic-gate overflow: 1347c478bd9Sstevel@tonic-gate for (c = *++ustr; lisalnum(c) && (xx = DIGIT(c)) < base; (c = *++ustr)) 1357c478bd9Sstevel@tonic-gate ; 136*28de4f3cSToomas Soome if (ptr != NULL) 1377c478bd9Sstevel@tonic-gate *ptr = (char *)ustr; 1382ef9abdcSjv227347 #if defined(_KERNEL) && !defined(_BOOT) 1392ef9abdcSjv227347 return (ERANGE); 1402ef9abdcSjv227347 #else /* _KERNEL && !_BOOT */ 1417c478bd9Sstevel@tonic-gate errno = ERANGE; 1427c478bd9Sstevel@tonic-gate return (ULONG_MAX); 1432ef9abdcSjv227347 #endif /* _KERNEL && !_BOOT */ 1447c478bd9Sstevel@tonic-gate } 145