10Sstevel@tonic-gate /* 20Sstevel@tonic-gate * CDDL HEADER START 30Sstevel@tonic-gate * 40Sstevel@tonic-gate * The contents of this file are subject to the terms of the 50Sstevel@tonic-gate * Common Development and Distribution License, Version 1.0 only 60Sstevel@tonic-gate * (the "License"). You may not use this file except in compliance 70Sstevel@tonic-gate * with the License. 80Sstevel@tonic-gate * 90Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 100Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 110Sstevel@tonic-gate * See the License for the specific language governing permissions 120Sstevel@tonic-gate * and limitations under the License. 130Sstevel@tonic-gate * 140Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 150Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 160Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 170Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 180Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 190Sstevel@tonic-gate * 200Sstevel@tonic-gate * CDDL HEADER END 210Sstevel@tonic-gate */ 22*1219Sraf 230Sstevel@tonic-gate /* 24*1219Sraf * Copyright 2006 Sun Microsystems, Inc. All rights reserved. 250Sstevel@tonic-gate * Use is subject to license terms. 260Sstevel@tonic-gate */ 270Sstevel@tonic-gate 280Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 290Sstevel@tonic-gate 300Sstevel@tonic-gate /* Copyright (c) 1986 AT&T */ 310Sstevel@tonic-gate /* All Rights Reserved */ 320Sstevel@tonic-gate 330Sstevel@tonic-gate 340Sstevel@tonic-gate #ifndef _WCS_LONGLONG 350Sstevel@tonic-gate #pragma weak wcstoul = _wcstoul 360Sstevel@tonic-gate #endif 370Sstevel@tonic-gate 38*1219Sraf #include "synonyms.h" 390Sstevel@tonic-gate #include <limits.h> 400Sstevel@tonic-gate #include <errno.h> 410Sstevel@tonic-gate #include <wchar.h> 420Sstevel@tonic-gate #define DIGIT(x) (iswdigit(x) ? (x) - L'0' : \ 430Sstevel@tonic-gate iswlower(x) ? (x) + 10 - L'a' : (x) + 10 - L'A') 440Sstevel@tonic-gate #define MBASE (L'z' - L'a' + 1 + 10) 450Sstevel@tonic-gate 460Sstevel@tonic-gate #ifdef _WCS_LONGLONG 470Sstevel@tonic-gate #define _WULONG_T unsigned long long 480Sstevel@tonic-gate #define _WULONG_MAX ULLONG_MAX 490Sstevel@tonic-gate #else /* _WCS_LONGLONG */ 500Sstevel@tonic-gate #define _WULONG_T unsigned long 510Sstevel@tonic-gate #define _WULONG_MAX ULONG_MAX 520Sstevel@tonic-gate #endif /* _WCS_LONGLONG */ 530Sstevel@tonic-gate 540Sstevel@tonic-gate #ifdef _WCS_LONGLONG 550Sstevel@tonic-gate unsigned long long 560Sstevel@tonic-gate wcstoull(const wchar_t *_RESTRICT_KYWD str, wchar_t **_RESTRICT_KYWD ptr, 570Sstevel@tonic-gate int base) 580Sstevel@tonic-gate #else /* _WCS_LONGLONG */ 590Sstevel@tonic-gate unsigned long 600Sstevel@tonic-gate _wcstoul(const wchar_t *str, wchar_t **ptr, int base) 610Sstevel@tonic-gate #endif /* _WCS_LONGLONG */ 620Sstevel@tonic-gate { 630Sstevel@tonic-gate _WULONG_T val; 640Sstevel@tonic-gate wchar_t c; 650Sstevel@tonic-gate int xx, neg = 0; 660Sstevel@tonic-gate _WULONG_T multmax; 670Sstevel@tonic-gate 680Sstevel@tonic-gate if (ptr != NULL) 690Sstevel@tonic-gate *ptr = (wchar_t *)str; /* in case no number is formed */ 700Sstevel@tonic-gate if (base < 0 || base > MBASE) { 710Sstevel@tonic-gate errno = EINVAL; 720Sstevel@tonic-gate return (0); /* base is invalid -- should be a fatal error */ 730Sstevel@tonic-gate } 740Sstevel@tonic-gate 750Sstevel@tonic-gate if (!iswalnum(c = *str)) { 760Sstevel@tonic-gate while (iswspace(c)) { 770Sstevel@tonic-gate c = *++str; 780Sstevel@tonic-gate } 790Sstevel@tonic-gate switch (c) { 800Sstevel@tonic-gate case L'-': 810Sstevel@tonic-gate neg++; 820Sstevel@tonic-gate /*FALLTHRU*/ 830Sstevel@tonic-gate case L'+': 840Sstevel@tonic-gate c = *++str; 850Sstevel@tonic-gate } 860Sstevel@tonic-gate } 870Sstevel@tonic-gate if (base == 0) { 880Sstevel@tonic-gate if (c != L'0') 890Sstevel@tonic-gate base = 10; 900Sstevel@tonic-gate else if (str[1] == L'x' || str[1] == L'X') 910Sstevel@tonic-gate base = 16; 920Sstevel@tonic-gate else 930Sstevel@tonic-gate base = 8; 940Sstevel@tonic-gate } 950Sstevel@tonic-gate /* 960Sstevel@tonic-gate * for any base > 10, the digits incrementally following 970Sstevel@tonic-gate * 9 are assumed to be "abc...z" or "ABC...Z" 980Sstevel@tonic-gate */ 990Sstevel@tonic-gate if (!iswalnum(c) || (xx = DIGIT(c)) >= base) { 1000Sstevel@tonic-gate errno = EINVAL; 1010Sstevel@tonic-gate return (0); /* no number formed */ 1020Sstevel@tonic-gate } 1030Sstevel@tonic-gate if (base == 16 && c == L'0' && iswxdigit(str[2]) && 1040Sstevel@tonic-gate (str[1] == L'x' || str[1] == L'X')) { 1050Sstevel@tonic-gate c = *(str += 2); /* skip over leading "0x" or "0X" */ 1060Sstevel@tonic-gate } 1070Sstevel@tonic-gate 1080Sstevel@tonic-gate multmax = _WULONG_MAX / (_WULONG_T)base; 1090Sstevel@tonic-gate val = DIGIT(c); 1100Sstevel@tonic-gate for (; iswalnum(c = *++str) && (xx = DIGIT(c)) < base; ) { 1110Sstevel@tonic-gate /* accumulate neg avoids surprises near MAXLONG */ 1120Sstevel@tonic-gate if (val > multmax) 1130Sstevel@tonic-gate goto overflow; 1140Sstevel@tonic-gate val *= base; 1150Sstevel@tonic-gate if (_WULONG_MAX - val < xx) 1160Sstevel@tonic-gate goto overflow; 1170Sstevel@tonic-gate val += xx; 1180Sstevel@tonic-gate } 1190Sstevel@tonic-gate if (ptr != NULL) 1200Sstevel@tonic-gate *ptr = (wchar_t *)str; 1210Sstevel@tonic-gate return (neg ? -val : val); 1220Sstevel@tonic-gate 1230Sstevel@tonic-gate overflow: 1240Sstevel@tonic-gate while (iswalnum(c = *++str) && (xx = DIGIT(c)) < base) 1250Sstevel@tonic-gate ; 1260Sstevel@tonic-gate 1270Sstevel@tonic-gate if (ptr != NULL) 1280Sstevel@tonic-gate *ptr = (wchar_t *)str; 1290Sstevel@tonic-gate errno = ERANGE; 1300Sstevel@tonic-gate return (_WULONG_MAX); 1310Sstevel@tonic-gate } 132