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 <_ansi.h>
126 #include <limits.h>
127 #include <wchar.h>
128 #include <wctype.h>
129 #include <errno.h>
130 #include "../locale/setlocale.h"
131
132 /* Make up for older non-compliant limits.h. (This is a C99/POSIX function,
133 * and both require ULLONG_MAX in limits.h.) */
134 #if !defined(ULLONG_MAX)
135 # define ULLONG_MAX ULONG_LONG_MAX
136 #endif
137
138 /*
139 * Convert a wide string to an unsigned long long integer.
140 */
141 #ifndef _REENT_ONLY
142
143 unsigned long long
wcstoull_l(const wchar_t * nptr,wchar_t ** endptr,int base,locale_t loc)144 wcstoull_l (const wchar_t *nptr, wchar_t **endptr,
145 int base, locale_t loc)
146 {
147 register const wchar_t *s = nptr;
148 register unsigned long long acc;
149 register wchar_t c;
150 register unsigned long long cutoff;
151 register int neg = 0, any, cutlim;
152
153 if(base < 0 || base == 1 || base > 36) {
154 _REENT_ERRNO(rptr) = EINVAL;
155 return(0ULL);
156 }
157 /*
158 * See strtol for comments as to the logic used.
159 */
160 do {
161 c = *s++;
162 } while (iswspace_l(c, loc));
163 if (c == L'-') {
164 neg = 1;
165 c = *s++;
166 } else if (c == L'+')
167 c = *s++;
168 if ((base == 0 || base == 16) &&
169 c == L'0' && (*s == L'x' || *s == L'X')) {
170 c = s[1];
171 s += 2;
172 base = 16;
173 }
174 if (base == 0)
175 base = c == L'0' ? 8 : 10;
176 cutoff = (unsigned long long)ULLONG_MAX / (unsigned long long)base;
177 cutlim = (unsigned long long)ULLONG_MAX % (unsigned long long)base;
178 for (acc = 0, any = 0;; c = *s++) {
179 if (c >= L'0' && c <= L'9')
180 c -= L'0';
181 else if (c >= L'A' && c <= L'Z')
182 c -= L'A' - 10;
183 else if (c >= L'a' && c <= L'z')
184 c -= L'a' - 10;
185 else
186 break;
187 if ((int) c >= base)
188 break;
189 if (any < 0 || acc > cutoff || (acc == cutoff && (int) c > cutlim))
190 any = -1;
191 else {
192 any = 1;
193 acc *= base;
194 acc += (unsigned long long) c;
195 }
196 }
197 if (any < 0) {
198 acc = ULLONG_MAX;
199 _REENT_ERRNO(rptr) = ERANGE;
200 } else if (neg)
201 acc = -acc;
202 if (endptr != 0)
203 *endptr = (wchar_t *) (any ? s - 1 : nptr);
204 return (acc);
205 }
206
207 unsigned long long
wcstoull(const wchar_t * __restrict s,wchar_t ** __restrict ptr,int base)208 wcstoull (const wchar_t *__restrict s,
209 wchar_t **__restrict ptr,
210 int base)
211 {
212 return wcstoull_l (s, ptr, base, __get_current_locale ());
213 }
214
215 #endif
216