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