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