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