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 2008 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 #pragma ident "%Z%%M% %I% %E% SMI" 31 32 #include "lint.h" 33 #include <errno.h> 34 #include <ctype.h> 35 #include <limits.h> 36 #include <sys/types.h> 37 #include <stdlib.h> 38 39 #define DIGIT(x) \ 40 (isdigit(x) ? (x) - '0' : islower(x) ? (x) + 10 - 'a' : (x) + 10 - 'A') 41 42 #define MBASE ('z' - 'a' + 1 + 10) 43 44 45 /* 46 * The following macro is a local version of isalnum() which limits 47 * alphabetic characters to the ranges a-z and A-Z; locale dependent 48 * characters will not return 1. The members of a-z and A-Z are 49 * assumed to be in ascending order and contiguous 50 */ 51 #define lisalnum(x) \ 52 (isdigit(x) || ((x) >= 'a' && (x) <= 'z') || ((x) >= 'A' && (x) <= 'Z')) 53 54 u_longlong_t 55 strtoull(const char *str, char **nptr, int base) 56 { 57 u_longlong_t val; 58 int c; 59 int xx; 60 u_longlong_t multmax; 61 u_longlong_t ullong_max; 62 int neg = 0; 63 const char **ptr = (const char **)nptr; 64 const unsigned char *ustr = (const unsigned char *)str; 65 66 if (ptr != (const char **)0) 67 *ptr = (char *)ustr; /* in case no number is formed */ 68 69 ullong_max = ULLONG_MAX; /* from a local version of limits.h */ 70 71 if (base < 0 || base > MBASE || base == 1) { 72 errno = EINVAL; 73 return (0); /* base is invalid -- should be a fatal error */ 74 } 75 if (!isalnum(c = *ustr)) { 76 while (isspace(c)) 77 c = *++ustr; 78 switch (c) { 79 case '-': 80 neg++; 81 /* FALLTHROUGH */ 82 case '+': 83 c = *++ustr; 84 } 85 } 86 if (base == 0) 87 if (c != '0') 88 base = 10; 89 else if (ustr[1] == 'x' || ustr[1] == 'X') 90 base = 16; 91 else 92 base = 8; 93 /* 94 * for any base > 10, the digits incrementally following 95 * 9 are assumed to be "abc...z" or "ABC...Z" 96 */ 97 if (!lisalnum(c) || (xx = DIGIT(c)) >= base) 98 return (0); /* no number formed */ 99 if (base == 16 && c == '0' && (ustr[1] == 'x' || ustr[1] == 'X') && 100 isxdigit(ustr[2])) 101 c = *(ustr += 2); /* skip over leading "0x" or "0X" */ 102 103 multmax = ullong_max / (u_longlong_t)base; 104 val = DIGIT(c); 105 for (c = *++ustr; lisalnum(c) && (xx = DIGIT(c)) < base; ) { 106 if (val > multmax) 107 goto overflow; 108 val *= base; 109 if (ullong_max - val < xx) 110 goto overflow; 111 val += xx; 112 c = *++ustr; 113 } 114 if (ptr != (const char **)0) 115 *ptr = (char *)ustr; 116 return (neg ? -val : val); 117 118 overflow: 119 for (c = *++ustr; lisalnum(c) && (xx = DIGIT(c)) < base; (c = *++ustr)) 120 ; 121 if (ptr != (const char **)0) 122 *ptr = (char *)ustr; 123 errno = ERANGE; 124 return (ullong_max); 125 } 126