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 /* 23*2ef9abdcSjv227347 * 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 30*2ef9abdcSjv227347 #if defined(_KERNEL) && !defined(_BOOT) 31*2ef9abdcSjv227347 #include <sys/errno.h> 32*2ef9abdcSjv227347 #else /* _KERNEL && !_BOOT */ 337c478bd9Sstevel@tonic-gate #if !defined(_BOOT) && !defined(_KMDB) 347257d1b4Sraf #include "lint.h" 357c478bd9Sstevel@tonic-gate #endif /* !_BOOT && !_KMDB */ 367c478bd9Sstevel@tonic-gate #include <errno.h> 377c478bd9Sstevel@tonic-gate #include <ctype.h> 387c478bd9Sstevel@tonic-gate #include <limits.h> 397c478bd9Sstevel@tonic-gate #include <stdlib.h> 40*2ef9abdcSjv227347 #endif /* _KERNEL && !_BOOT */ 41*2ef9abdcSjv227347 #include "strtolctype.h" 427c478bd9Sstevel@tonic-gate #include <sys/types.h> 437c478bd9Sstevel@tonic-gate 44*2ef9abdcSjv227347 #if defined(_KERNEL) && !defined(_BOOT) 45*2ef9abdcSjv227347 int 46*2ef9abdcSjv227347 ddi_strtoul(const char *str, char **nptr, int base, unsigned long *result) 47*2ef9abdcSjv227347 #else /* _KERNEL && !_BOOT */ 487c478bd9Sstevel@tonic-gate unsigned long 497c478bd9Sstevel@tonic-gate strtoul(const char *str, char **nptr, int base) 50*2ef9abdcSjv227347 #endif /* _KERNEL && !_BOOT */ 517c478bd9Sstevel@tonic-gate { 527c478bd9Sstevel@tonic-gate unsigned long val; 537c478bd9Sstevel@tonic-gate int c; 547c478bd9Sstevel@tonic-gate int xx; 557c478bd9Sstevel@tonic-gate int neg = 0; 56*2ef9abdcSjv227347 unsigned long multmax; 577c478bd9Sstevel@tonic-gate const char **ptr = (const char **)nptr; 587c478bd9Sstevel@tonic-gate const unsigned char *ustr = (const unsigned char *)str; 597c478bd9Sstevel@tonic-gate 607c478bd9Sstevel@tonic-gate if (ptr != (const char **)0) 617c478bd9Sstevel@tonic-gate *ptr = (char *)ustr; /* in case no number is formed */ 627c478bd9Sstevel@tonic-gate if (base < 0 || base > MBASE || base == 1) { 63*2ef9abdcSjv227347 /* base is invalid -- should be a fatal error */ 64*2ef9abdcSjv227347 #if defined(_KERNEL) && !defined(_BOOT) 65*2ef9abdcSjv227347 return (EINVAL); 66*2ef9abdcSjv227347 #else /* _KERNEL && !_BOOT */ 677c478bd9Sstevel@tonic-gate errno = EINVAL; 68*2ef9abdcSjv227347 return (0); 69*2ef9abdcSjv227347 #endif /* _KERNEL && !_BOOT */ 707c478bd9Sstevel@tonic-gate } 717c478bd9Sstevel@tonic-gate if (!isalnum(c = *ustr)) { 727c478bd9Sstevel@tonic-gate while (isspace(c)) 737c478bd9Sstevel@tonic-gate c = *++ustr; 747c478bd9Sstevel@tonic-gate switch (c) { 757c478bd9Sstevel@tonic-gate case '-': 767c478bd9Sstevel@tonic-gate neg++; 777c478bd9Sstevel@tonic-gate /* FALLTHROUGH */ 787c478bd9Sstevel@tonic-gate case '+': 797c478bd9Sstevel@tonic-gate c = *++ustr; 807c478bd9Sstevel@tonic-gate } 817c478bd9Sstevel@tonic-gate } 827c478bd9Sstevel@tonic-gate if (base == 0) 837c478bd9Sstevel@tonic-gate if (c != '0') 847c478bd9Sstevel@tonic-gate base = 10; 857c478bd9Sstevel@tonic-gate else if (ustr[1] == 'x' || ustr[1] == 'X') 867c478bd9Sstevel@tonic-gate base = 16; 877c478bd9Sstevel@tonic-gate else 887c478bd9Sstevel@tonic-gate base = 8; 897c478bd9Sstevel@tonic-gate /* 907c478bd9Sstevel@tonic-gate * for any base > 10, the digits incrementally following 917c478bd9Sstevel@tonic-gate * 9 are assumed to be "abc...z" or "ABC...Z" 927c478bd9Sstevel@tonic-gate */ 93*2ef9abdcSjv227347 if (!lisalnum(c) || (xx = DIGIT(c)) >= base) { 94*2ef9abdcSjv227347 /* no number formed */ 95*2ef9abdcSjv227347 #if defined(_KERNEL) && !defined(_BOOT) 96*2ef9abdcSjv227347 return (EINVAL); 97*2ef9abdcSjv227347 #else /* _KERNEL && !_BOOT */ 98*2ef9abdcSjv227347 return (0); 99*2ef9abdcSjv227347 #endif /* _KERNEL && !_BOOT */ 100*2ef9abdcSjv227347 } 1017c478bd9Sstevel@tonic-gate if (base == 16 && c == '0' && (ustr[1] == 'x' || ustr[1] == 'X') && 1027c478bd9Sstevel@tonic-gate isxdigit(ustr[2])) 1037c478bd9Sstevel@tonic-gate c = *(ustr += 2); /* skip over leading "0x" or "0X" */ 1047c478bd9Sstevel@tonic-gate 1057c478bd9Sstevel@tonic-gate multmax = ULONG_MAX / (unsigned long)base; 1067c478bd9Sstevel@tonic-gate val = DIGIT(c); 1077c478bd9Sstevel@tonic-gate for (c = *++ustr; lisalnum(c) && (xx = DIGIT(c)) < base; ) { 1087c478bd9Sstevel@tonic-gate if (val > multmax) 1097c478bd9Sstevel@tonic-gate goto overflow; 1107c478bd9Sstevel@tonic-gate val *= base; 1117c478bd9Sstevel@tonic-gate if (ULONG_MAX - val < xx) 1127c478bd9Sstevel@tonic-gate goto overflow; 1137c478bd9Sstevel@tonic-gate val += xx; 1147c478bd9Sstevel@tonic-gate c = *++ustr; 1157c478bd9Sstevel@tonic-gate } 1167c478bd9Sstevel@tonic-gate if (ptr != (const char **)0) 1177c478bd9Sstevel@tonic-gate *ptr = (char *)ustr; 118*2ef9abdcSjv227347 #if defined(_KERNEL) && !defined(_BOOT) 119*2ef9abdcSjv227347 *result = neg ? -val : val; 120*2ef9abdcSjv227347 return (0); 121*2ef9abdcSjv227347 #else /* _KERNEL && !_BOOT */ 1227c478bd9Sstevel@tonic-gate return (neg ? -val : val); 123*2ef9abdcSjv227347 #endif /* _KERNEL && !_BOOT */ 1247c478bd9Sstevel@tonic-gate 1257c478bd9Sstevel@tonic-gate overflow: 1267c478bd9Sstevel@tonic-gate for (c = *++ustr; lisalnum(c) && (xx = DIGIT(c)) < base; (c = *++ustr)) 1277c478bd9Sstevel@tonic-gate ; 1287c478bd9Sstevel@tonic-gate if (ptr != (const char **)0) 1297c478bd9Sstevel@tonic-gate *ptr = (char *)ustr; 130*2ef9abdcSjv227347 #if defined(_KERNEL) && !defined(_BOOT) 131*2ef9abdcSjv227347 return (ERANGE); 132*2ef9abdcSjv227347 #else /* _KERNEL && !_BOOT */ 1337c478bd9Sstevel@tonic-gate errno = ERANGE; 1347c478bd9Sstevel@tonic-gate return (ULONG_MAX); 135*2ef9abdcSjv227347 #endif /* _KERNEL && !_BOOT */ 1367c478bd9Sstevel@tonic-gate } 137