1*0Sstevel@tonic-gate /* 2*0Sstevel@tonic-gate * CDDL HEADER START 3*0Sstevel@tonic-gate * 4*0Sstevel@tonic-gate * The contents of this file are subject to the terms of the 5*0Sstevel@tonic-gate * Common Development and Distribution License, Version 1.0 only 6*0Sstevel@tonic-gate * (the "License"). You may not use this file except in compliance 7*0Sstevel@tonic-gate * with the License. 8*0Sstevel@tonic-gate * 9*0Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 10*0Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 11*0Sstevel@tonic-gate * See the License for the specific language governing permissions 12*0Sstevel@tonic-gate * and limitations under the License. 13*0Sstevel@tonic-gate * 14*0Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 15*0Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 16*0Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 17*0Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 18*0Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 19*0Sstevel@tonic-gate * 20*0Sstevel@tonic-gate * CDDL HEADER END 21*0Sstevel@tonic-gate */ 22*0Sstevel@tonic-gate /* 23*0Sstevel@tonic-gate * Copyright 2004 Sun Microsystems, Inc. All rights reserved. 24*0Sstevel@tonic-gate * Use is subject to license terms. 25*0Sstevel@tonic-gate */ 26*0Sstevel@tonic-gate 27*0Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 28*0Sstevel@tonic-gate 29*0Sstevel@tonic-gate /* Copyright (c) 1986 AT&T */ 30*0Sstevel@tonic-gate /* All Rights Reserved */ 31*0Sstevel@tonic-gate 32*0Sstevel@tonic-gate 33*0Sstevel@tonic-gate #ifndef _WCS_LONGLONG 34*0Sstevel@tonic-gate #pragma weak wcstoul = _wcstoul 35*0Sstevel@tonic-gate #endif 36*0Sstevel@tonic-gate 37*0Sstevel@tonic-gate #include "lint.h" 38*0Sstevel@tonic-gate #include <limits.h> 39*0Sstevel@tonic-gate #include <errno.h> 40*0Sstevel@tonic-gate #include <wchar.h> 41*0Sstevel@tonic-gate #define DIGIT(x) (iswdigit(x) ? (x) - L'0' : \ 42*0Sstevel@tonic-gate iswlower(x) ? (x) + 10 - L'a' : (x) + 10 - L'A') 43*0Sstevel@tonic-gate #define MBASE (L'z' - L'a' + 1 + 10) 44*0Sstevel@tonic-gate 45*0Sstevel@tonic-gate #ifdef _WCS_LONGLONG 46*0Sstevel@tonic-gate #define _WULONG_T unsigned long long 47*0Sstevel@tonic-gate #define _WULONG_MAX ULLONG_MAX 48*0Sstevel@tonic-gate #else /* _WCS_LONGLONG */ 49*0Sstevel@tonic-gate #define _WULONG_T unsigned long 50*0Sstevel@tonic-gate #define _WULONG_MAX ULONG_MAX 51*0Sstevel@tonic-gate #endif /* _WCS_LONGLONG */ 52*0Sstevel@tonic-gate 53*0Sstevel@tonic-gate #ifdef _WCS_LONGLONG 54*0Sstevel@tonic-gate unsigned long long 55*0Sstevel@tonic-gate wcstoull(const wchar_t *_RESTRICT_KYWD str, wchar_t **_RESTRICT_KYWD ptr, 56*0Sstevel@tonic-gate int base) 57*0Sstevel@tonic-gate #else /* _WCS_LONGLONG */ 58*0Sstevel@tonic-gate unsigned long 59*0Sstevel@tonic-gate _wcstoul(const wchar_t *str, wchar_t **ptr, int base) 60*0Sstevel@tonic-gate #endif /* _WCS_LONGLONG */ 61*0Sstevel@tonic-gate { 62*0Sstevel@tonic-gate _WULONG_T val; 63*0Sstevel@tonic-gate wchar_t c; 64*0Sstevel@tonic-gate int xx, neg = 0; 65*0Sstevel@tonic-gate _WULONG_T multmax; 66*0Sstevel@tonic-gate 67*0Sstevel@tonic-gate if (ptr != NULL) 68*0Sstevel@tonic-gate *ptr = (wchar_t *)str; /* in case no number is formed */ 69*0Sstevel@tonic-gate if (base < 0 || base > MBASE) { 70*0Sstevel@tonic-gate errno = EINVAL; 71*0Sstevel@tonic-gate return (0); /* base is invalid -- should be a fatal error */ 72*0Sstevel@tonic-gate } 73*0Sstevel@tonic-gate 74*0Sstevel@tonic-gate if (!iswalnum(c = *str)) { 75*0Sstevel@tonic-gate while (iswspace(c)) { 76*0Sstevel@tonic-gate c = *++str; 77*0Sstevel@tonic-gate } 78*0Sstevel@tonic-gate switch (c) { 79*0Sstevel@tonic-gate case L'-': 80*0Sstevel@tonic-gate neg++; 81*0Sstevel@tonic-gate /*FALLTHRU*/ 82*0Sstevel@tonic-gate case L'+': 83*0Sstevel@tonic-gate c = *++str; 84*0Sstevel@tonic-gate } 85*0Sstevel@tonic-gate } 86*0Sstevel@tonic-gate if (base == 0) { 87*0Sstevel@tonic-gate if (c != L'0') 88*0Sstevel@tonic-gate base = 10; 89*0Sstevel@tonic-gate else if (str[1] == L'x' || str[1] == L'X') 90*0Sstevel@tonic-gate base = 16; 91*0Sstevel@tonic-gate else 92*0Sstevel@tonic-gate base = 8; 93*0Sstevel@tonic-gate } 94*0Sstevel@tonic-gate /* 95*0Sstevel@tonic-gate * for any base > 10, the digits incrementally following 96*0Sstevel@tonic-gate * 9 are assumed to be "abc...z" or "ABC...Z" 97*0Sstevel@tonic-gate */ 98*0Sstevel@tonic-gate if (!iswalnum(c) || (xx = DIGIT(c)) >= base) { 99*0Sstevel@tonic-gate errno = EINVAL; 100*0Sstevel@tonic-gate return (0); /* no number formed */ 101*0Sstevel@tonic-gate } 102*0Sstevel@tonic-gate if (base == 16 && c == L'0' && iswxdigit(str[2]) && 103*0Sstevel@tonic-gate (str[1] == L'x' || str[1] == L'X')) { 104*0Sstevel@tonic-gate c = *(str += 2); /* skip over leading "0x" or "0X" */ 105*0Sstevel@tonic-gate } 106*0Sstevel@tonic-gate 107*0Sstevel@tonic-gate multmax = _WULONG_MAX / (_WULONG_T)base; 108*0Sstevel@tonic-gate val = DIGIT(c); 109*0Sstevel@tonic-gate for (; iswalnum(c = *++str) && (xx = DIGIT(c)) < base; ) { 110*0Sstevel@tonic-gate /* accumulate neg avoids surprises near MAXLONG */ 111*0Sstevel@tonic-gate if (val > multmax) 112*0Sstevel@tonic-gate goto overflow; 113*0Sstevel@tonic-gate val *= base; 114*0Sstevel@tonic-gate if (_WULONG_MAX - val < xx) 115*0Sstevel@tonic-gate goto overflow; 116*0Sstevel@tonic-gate val += xx; 117*0Sstevel@tonic-gate } 118*0Sstevel@tonic-gate if (ptr != NULL) 119*0Sstevel@tonic-gate *ptr = (wchar_t *)str; 120*0Sstevel@tonic-gate return (neg ? -val : val); 121*0Sstevel@tonic-gate 122*0Sstevel@tonic-gate overflow: 123*0Sstevel@tonic-gate while (iswalnum(c = *++str) && (xx = DIGIT(c)) < base) 124*0Sstevel@tonic-gate ; 125*0Sstevel@tonic-gate 126*0Sstevel@tonic-gate if (ptr != NULL) 127*0Sstevel@tonic-gate *ptr = (wchar_t *)str; 128*0Sstevel@tonic-gate errno = ERANGE; 129*0Sstevel@tonic-gate return (_WULONG_MAX); 130*0Sstevel@tonic-gate } 131