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.
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.
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]
23 * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
24 * Use is subject to license terms.
27 /* Copyright (c) 1986 AT&T */
28 /* All Rights Reserved */
30 #pragma ident "%Z%%M% %I% %E% SMI"
33 #pragma weak _wcstoul = wcstoul
40 #define DIGIT(x) (iswdigit(x) ? (x) - L'0' : \
41 iswlower(x) ? (x) + 10 - L'a' : (x) + 10 - L'A')
42 #define MBASE (L'z' - L'a' + 1 + 10)
45 #define _WULONG_T unsigned long long
46 #define _WULONG_MAX ULLONG_MAX
47 #else /* _WCS_LONGLONG */
48 #define _WULONG_T unsigned long
49 #define _WULONG_MAX ULONG_MAX
50 #endif /* _WCS_LONGLONG */
54 wcstoull(const wchar_t *_RESTRICT_KYWD str
, wchar_t **_RESTRICT_KYWD ptr
,
56 #else /* _WCS_LONGLONG */
58 wcstoul(const wchar_t *str
, wchar_t **ptr
, int base
)
59 #endif /* _WCS_LONGLONG */
67 *ptr
= (wchar_t *)str
; /* in case no number is formed */
68 if (base
< 0 || base
> MBASE
) {
70 return (0); /* base is invalid -- should be a fatal error */
73 if (!iswalnum(c
= *str
)) {
88 else if (str
[1] == L
'x' || str
[1] == L
'X')
94 * for any base > 10, the digits incrementally following
95 * 9 are assumed to be "abc...z" or "ABC...Z"
97 if (!iswalnum(c
) || (xx
= DIGIT(c
)) >= base
) {
99 return (0); /* no number formed */
101 if (base
== 16 && c
== L
'0' && iswxdigit(str
[2]) &&
102 (str
[1] == L
'x' || str
[1] == L
'X')) {
103 c
= *(str
+= 2); /* skip over leading "0x" or "0X" */
106 multmax
= _WULONG_MAX
/ (_WULONG_T
)base
;
108 for (; iswalnum(c
= *++str
) && (xx
= DIGIT(c
)) < base
; ) {
109 /* accumulate neg avoids surprises near MAXLONG */
113 if (_WULONG_MAX
- val
< xx
)
118 *ptr
= (wchar_t *)str
;
119 return (neg
? -val
: val
);
122 while (iswalnum(c
= *++str
) && (xx
= DIGIT(c
)) < base
)
126 *ptr
= (wchar_t *)str
;
128 return (_WULONG_MAX
);