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, Version 1.0 only 6 * (the "License"). You may not use this file except in compliance 7 * with the License. 8 * 9 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 10 * or http://www.opensolaris.org/os/licensing. 11 * See the License for the specific language governing permissions 12 * and limitations under the License. 13 * 14 * When distributing Covered Code, include this CDDL HEADER in each 15 * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 16 * If applicable, add the following below this CDDL HEADER, with the 17 * fields enclosed by brackets "[]" replaced with your own identifying 18 * information: Portions Copyright [yyyy] [name of copyright owner] 19 * 20 * CDDL HEADER END 21 */ 22 /* 23 * Copyright 2004 Sun Microsystems, Inc. All rights reserved. 24 * Use is subject to license terms. 25 */ 26 27 #pragma ident "%Z%%M% %I% %E% SMI" 28 29 /* Copyright (c) 1988 AT&T */ 30 /* All Rights Reserved */ 31 32 33 #pragma weak strtoull = _strtoull 34 35 #include "synonyms.h" 36 #include <errno.h> 37 #include <ctype.h> 38 #include <limits.h> 39 #include <sys/types.h> 40 #include <stdlib.h> 41 42 #define DIGIT(x) \ 43 (isdigit(x) ? (x) - '0' : islower(x) ? (x) + 10 - 'a' : (x) + 10 - 'A') 44 45 #define MBASE ('z' - 'a' + 1 + 10) 46 47 48 /* 49 * The following macro is a local version of isalnum() which limits 50 * alphabetic characters to the ranges a-z and A-Z; locale dependent 51 * characters will not return 1. The members of a-z and A-Z are 52 * assumed to be in ascending order and contiguous 53 */ 54 #define lisalnum(x) \ 55 (isdigit(x) || ((x) >= 'a' && (x) <= 'z') || ((x) >= 'A' && (x) <= 'Z')) 56 57 u_longlong_t 58 strtoull(const char *str, char **nptr, int base) 59 { 60 u_longlong_t val; 61 int c; 62 int xx; 63 u_longlong_t multmax; 64 u_longlong_t ullong_max; 65 int neg = 0; 66 const char **ptr = (const char **)nptr; 67 const unsigned char *ustr = (const unsigned char *)str; 68 69 if (ptr != (const char **)0) 70 *ptr = (char *)ustr; /* in case no number is formed */ 71 72 ullong_max = ULLONG_MAX; /* from a local version of limits.h */ 73 74 if (base < 0 || base > MBASE || base == 1) { 75 errno = EINVAL; 76 return (0); /* base is invalid -- should be a fatal error */ 77 } 78 if (!isalnum(c = *ustr)) { 79 while (isspace(c)) 80 c = *++ustr; 81 switch (c) { 82 case '-': 83 neg++; 84 /* FALLTHROUGH */ 85 case '+': 86 c = *++ustr; 87 } 88 } 89 if (base == 0) 90 if (c != '0') 91 base = 10; 92 else if (ustr[1] == 'x' || ustr[1] == 'X') 93 base = 16; 94 else 95 base = 8; 96 /* 97 * for any base > 10, the digits incrementally following 98 * 9 are assumed to be "abc...z" or "ABC...Z" 99 */ 100 if (!lisalnum(c) || (xx = DIGIT(c)) >= base) 101 return (0); /* no number formed */ 102 if (base == 16 && c == '0' && (ustr[1] == 'x' || ustr[1] == 'X') && 103 isxdigit(ustr[2])) 104 c = *(ustr += 2); /* skip over leading "0x" or "0X" */ 105 106 multmax = ullong_max / (u_longlong_t)base; 107 val = DIGIT(c); 108 for (c = *++ustr; lisalnum(c) && (xx = DIGIT(c)) < base; ) { 109 if (val > multmax) 110 goto overflow; 111 val *= base; 112 if (ullong_max - val < xx) 113 goto overflow; 114 val += xx; 115 c = *++ustr; 116 } 117 if (ptr != (const char **)0) 118 *ptr = (char *)ustr; 119 return (neg ? -val : val); 120 121 overflow: 122 for (c = *++ustr; lisalnum(c) && (xx = DIGIT(c)) < base; (c = *++ustr)) 123 ; 124 if (ptr != (const char **)0) 125 *ptr = (char *)ustr; 126 errno = ERANGE; 127 return (ullong_max); 128 } 129