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