1 /*
2 FUNCTION
3 	<<wcstoul>>---wide string to unsigned long
4 
5 INDEX
6 	wcstoul
7 INDEX
8 	_wcstoul_r
9 
10 ANSI_SYNOPSIS
11 	#include <wchar.h>
12         unsigned long wcstoul(const wchar_t *__restrict <[s]>,
13         		      wchar_t **__restrict <[ptr]>, int <[base]>);
14 
15         unsigned long _wcstoul_r(void *<[reent]>, const wchar_t *<[s]>,
16                               wchar_t **<[ptr]>, int <[base]>);
17 
18 TRAD_SYNOPSIS
19 	#include <wchar.h>
20         unsigned long wcstoul(<[s]>, <[ptr]>, <[base]>)
21         wchar_t *__restrict <[s]>;
22         wchar_t **__restrict <[ptr]>;
23         int <[base]>;
24 
25         unsigned long _wcstoul_r(<[reent]>, <[s]>, <[ptr]>, <[base]>)
26 	wchar_t *<[reent]>;
27         wchar_t *<[s]>;
28         wchar_t **<[ptr]>;
29         int <[base]>;
30 
31 DESCRIPTION
32 The function <<wcstoul>> converts the wide string <<*<[s]>>> to
33 an <<unsigned long>>. First, it breaks down the string into three parts:
34 leading whitespace, which is ignored; a subject string consisting
35 of the digits meaningful in the radix specified by <[base]>
36 (for example, <<0>> through <<7>> if the value of <[base]> is 8);
37 and a trailing portion consisting of one or more unparseable characters,
38 which always includes the terminating null character. Then, it attempts
39 to convert the subject string into an unsigned long integer, and returns the
40 result.
41 
42 If the value of <[base]> is zero, the subject string is expected to look
43 like a normal C integer constant (save that no optional sign is permitted):
44 a possible <<0x>> indicating hexadecimal radix, and a number.
45 If <[base]> is between 2 and 36, the expected form of the subject is a
46 sequence of digits (which may include letters, depending on the
47 base) representing an integer in the radix specified by <[base]>.
48 The letters <<a>>--<<z>> (or <<A>>--<<Z>>) are used as digits valued from
49 10 to 35. If <[base]> is 16, a leading <<0x>> is permitted.
50 
51 The subject sequence is the longest initial sequence of the input
52 string that has the expected form, starting with the first
53 non-whitespace character.  If the string is empty or consists entirely
54 of whitespace, or if the first non-whitespace character is not a
55 permissible digit, the subject string is empty.
56 
57 If the subject string is acceptable, and the value of <[base]> is zero,
58 <<wcstoul>> attempts to determine the radix from the input string. A
59 string with a leading <<0x>> is treated as a hexadecimal value; a string with
60 a leading <<0>> and no <<x>> is treated as octal; all other strings are
61 treated as decimal. If <[base]> is between 2 and 36, it is used as the
62 conversion radix, as described above. Finally, a pointer to the first
63 character past the converted subject string is stored in <[ptr]>, if
64 <[ptr]> is not <<NULL>>.
65 
66 If the subject string is empty (that is, if <<*>><[s]> does not start
67 with a substring in acceptable form), no conversion
68 is performed and the value of <[s]> is stored in <[ptr]> (if <[ptr]> is
69 not <<NULL>>).
70 
71 The alternate function <<_wcstoul_r>> is a reentrant version.  The
72 extra argument <[reent]> is a pointer to a reentrancy structure.
73 
74 
75 RETURNS
76 <<wcstoul>> returns the converted value, if any. If no conversion was
77 made, <<0>> is returned.
78 
79 <<wcstoul>> returns <<ULONG_MAX>> if the magnitude of the converted
80 value is too large, and sets <<errno>> to <<ERANGE>>.
81 
82 PORTABILITY
83 <<wcstoul>> is ANSI.
84 
85 <<wcstoul>> requires no supporting OS subroutines.
86 */
87 
88 /*
89  * Copyright (c) 1990 Regents of the University of California.
90  * All rights reserved.
91  *
92  * Redistribution and use in source and binary forms, with or without
93  * modification, are permitted provided that the following conditions
94  * are met:
95  * 1. Redistributions of source code must retain the above copyright
96  *    notice, this list of conditions and the following disclaimer.
97  * 2. Redistributions in binary form must reproduce the above copyright
98  *    notice, this list of conditions and the following disclaimer in the
99  *    documentation and/or other materials provided with the distribution.
100  * 3. All advertising materials mentioning features or use of this software
101  *    must display the following acknowledgement:
102  *	This product includes software developed by the University of
103  *	California, Berkeley and its contributors.
104  * 4. Neither the name of the University nor the names of its contributors
105  *    may be used to endorse or promote products derived from this software
106  *    without specific prior written permission.
107  *
108  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
109  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
110  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
111  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
112  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
113  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
114  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
115  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
116  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
117  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
118  * SUCH DAMAGE.
119  */
120 
121 #include <_ansi.h>
122 #include <limits.h>
123 #include <wctype.h>
124 #include <wchar.h>
125 #include <errno.h>
126 #include <stdlib.h>
127 #include <reent.h>
128 
129 #ifndef ULONG_MIN
130 #ifdef _LP64
131 #define ULONG_MAX 0xffffffffffffffffUL
132 #else
133 #define ULONG_MAX 0xffffffffUL
134 #endif
135 #endif
136 
137 /*
138  * Convert a wide string to an unsigned long integer.
139  *
140  * Ignores `locale' stuff.  Assumes that the upper and lower case
141  * alphabets and digits are each contiguous.
142  */
143 unsigned long
144 _DEFUN (_wcstoul_r, (rptr, nptr, endptr, base),
145 	struct _reent *rptr _AND
146 	_CONST wchar_t *nptr _AND
147 	wchar_t **endptr _AND
148 	int base)
149 {
150 	register const wchar_t *s = nptr;
151 	register unsigned long acc;
152 	register int c;
153 	register unsigned long cutoff;
154 	register int neg = 0, any, cutlim;
155 
156 	/*
157 	 * See strtol for comments as to the logic used.
158 	 */
159 	do {
160 		c = *s++;
161 	} while (iswspace(c));
162 	if (c == L'-') {
163 		neg = 1;
164 		c = *s++;
165 	} else if (c == L'+')
166 		c = *s++;
167 	if ((base == 0 || base == 16) &&
168 	    c == L'0' && (*s == L'x' || *s == L'X')) {
169 		c = s[1];
170 		s += 2;
171 		base = 16;
172 	}
173 	if (base == 0)
174 		base = c == L'0' ? 8 : 10;
175 	cutoff = (unsigned long)ULONG_MAX / (unsigned long)base;
176 	cutlim = (unsigned long)ULONG_MAX % (unsigned long)base;
177 	for (acc = 0, any = 0;; c = *s++) {
178 		if (iswdigit(c))
179 			c -= L'0';
180 		else if (iswalpha(c))
181 			c -= iswupper(c) ? L'A' - 10 : L'a' - 10;
182 		else
183 			break;
184 		if (c >= base)
185 			break;
186                if (any < 0 || acc > cutoff || (acc == cutoff && c > cutlim))
187 			any = -1;
188 		else {
189 			any = 1;
190 			acc *= base;
191 			acc += c;
192 		}
193 	}
194 	if (any < 0) {
195 		acc = ULONG_MAX;
196 		rptr->_errno = ERANGE;
197 	} else if (neg)
198 		acc = -acc;
199 	if (endptr != 0)
200 		*endptr = (wchar_t *) (any ? s - 1 : nptr);
201 	return (acc);
202 }
203 
204 #ifndef _REENT_ONLY
205 
206 unsigned long
207 _DEFUN (wcstoul, (s, ptr, base),
208 	_CONST wchar_t *__restrict s _AND
209 	wchar_t **__restrict ptr _AND
210 	int base)
211 {
212 	return _wcstoul_r (_REENT, s, ptr, base);
213 }
214 
215 #endif
216