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 (the "License").
6 * You may not use this file except in compliance with the License.
7 *
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.
12 *
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]
18 *
19 * CDDL HEADER END
20 */
21
22 /*
23 * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
24 * Use is subject to license terms.
25 */
26
27 /* Copyright (c) 1986 AT&T */
28 /* All Rights Reserved */
29
30 #ifndef _WCS_LONGLONG
31 #pragma weak _wcstoul = wcstoul
32 #endif
33
34 #include "lint.h"
35 #include <limits.h>
36 #include <errno.h>
37 #include <wchar.h>
38 #define DIGIT(x) (iswdigit(x) ? (x) - L'0' : \
39 iswlower(x) ? (x) + 10 - L'a' : (x) + 10 - L'A')
40 #define MBASE (L'z' - L'a' + 1 + 10)
41
42 #ifdef _WCS_LONGLONG
43 #define _WULONG_T unsigned long long
44 #define _WULONG_MAX ULLONG_MAX
45 #else /* _WCS_LONGLONG */
46 #define _WULONG_T unsigned long
47 #define _WULONG_MAX ULONG_MAX
48 #endif /* _WCS_LONGLONG */
49
50 #ifdef _WCS_LONGLONG
51 unsigned long long
wcstoull(const wchar_t * _RESTRICT_KYWD str,wchar_t ** _RESTRICT_KYWD ptr,int base)52 wcstoull(const wchar_t *_RESTRICT_KYWD str, wchar_t **_RESTRICT_KYWD ptr,
53 int base)
54 #else /* _WCS_LONGLONG */
55 unsigned long
56 wcstoul(const wchar_t *str, wchar_t **ptr, int base)
57 #endif /* _WCS_LONGLONG */
58 {
59 _WULONG_T val;
60 wchar_t c;
61 int xx, neg = 0;
62 _WULONG_T multmax;
63
64 if (ptr != NULL)
65 *ptr = (wchar_t *)str; /* in case no number is formed */
66 if (base < 0 || base > MBASE) {
67 errno = EINVAL;
68 return (0); /* base is invalid -- should be a fatal error */
69 }
70
71 if (!iswalnum(c = *str)) {
72 while (iswspace(c)) {
73 c = *++str;
74 }
75 switch (c) {
76 case L'-':
77 neg++;
78 /*FALLTHRU*/
79 case L'+':
80 c = *++str;
81 }
82 }
83 if (base == 0) {
84 if (c != L'0')
85 base = 10;
86 else if (str[1] == L'x' || str[1] == L'X')
87 base = 16;
88 else
89 base = 8;
90 }
91 /*
92 * for any base > 10, the digits incrementally following
93 * 9 are assumed to be "abc...z" or "ABC...Z"
94 */
95 if (!iswalnum(c) || (xx = DIGIT(c)) >= base) {
96 errno = EINVAL;
97 return (0); /* no number formed */
98 }
99 if (base == 16 && c == L'0' && iswxdigit(str[2]) &&
100 (str[1] == L'x' || str[1] == L'X')) {
101 c = *(str += 2); /* skip over leading "0x" or "0X" */
102 }
103
104 multmax = _WULONG_MAX / (_WULONG_T)base;
105 val = DIGIT(c);
106 for (; iswalnum(c = *++str) && (xx = DIGIT(c)) < base; ) {
107 /* accumulate neg avoids surprises near MAXLONG */
108 if (val > multmax)
109 goto overflow;
110 val *= base;
111 if (_WULONG_MAX - val < xx)
112 goto overflow;
113 val += xx;
114 }
115 if (ptr != NULL)
116 *ptr = (wchar_t *)str;
117 return (neg ? -val : val);
118
119 overflow:
120 while (iswalnum(c = *++str) && (xx = DIGIT(c)) < base)
121 ;
122
123 if (ptr != NULL)
124 *ptr = (wchar_t *)str;
125 errno = ERANGE;
126 return (_WULONG_MAX);
127 }
128