xref: /freebsd/lib/libc/locale/wcstoull.c (revision 9fc0ff9d)
11302dabdSTim J. Robbins /*-
28a16b7a1SPedro F. Giffuni  * SPDX-License-Identifier: BSD-3-Clause
38a16b7a1SPedro F. Giffuni  *
41302dabdSTim J. Robbins  * Copyright (c) 1992, 1993
51302dabdSTim J. Robbins  *	The Regents of the University of California.  All rights reserved.
61302dabdSTim J. Robbins  *
73c87aa1dSDavid Chisnall  * Copyright (c) 2011 The FreeBSD Foundation
85b5fa75aSEd Maste  *
93c87aa1dSDavid Chisnall  * Portions of this software were developed by David Chisnall
103c87aa1dSDavid Chisnall  * under sponsorship from the FreeBSD Foundation.
113c87aa1dSDavid Chisnall  *
121302dabdSTim J. Robbins  * Redistribution and use in source and binary forms, with or without
131302dabdSTim J. Robbins  * modification, are permitted provided that the following conditions
141302dabdSTim J. Robbins  * are met:
151302dabdSTim J. Robbins  * 1. Redistributions of source code must retain the above copyright
161302dabdSTim J. Robbins  *    notice, this list of conditions and the following disclaimer.
171302dabdSTim J. Robbins  * 2. Redistributions in binary form must reproduce the above copyright
181302dabdSTim J. Robbins  *    notice, this list of conditions and the following disclaimer in the
191302dabdSTim J. Robbins  *    documentation and/or other materials provided with the distribution.
20fbbd9655SWarner Losh  * 3. Neither the name of the University nor the names of its contributors
211302dabdSTim J. Robbins  *    may be used to endorse or promote products derived from this software
221302dabdSTim J. Robbins  *    without specific prior written permission.
231302dabdSTim J. Robbins  *
241302dabdSTim J. Robbins  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
251302dabdSTim J. Robbins  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
261302dabdSTim J. Robbins  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
271302dabdSTim J. Robbins  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
281302dabdSTim J. Robbins  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
291302dabdSTim J. Robbins  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
301302dabdSTim J. Robbins  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
311302dabdSTim J. Robbins  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
321302dabdSTim J. Robbins  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
331302dabdSTim J. Robbins  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
341302dabdSTim J. Robbins  * SUCH DAMAGE.
351302dabdSTim J. Robbins  */
361302dabdSTim J. Robbins 
371302dabdSTim J. Robbins #include <errno.h>
381302dabdSTim J. Robbins #include <limits.h>
391302dabdSTim J. Robbins #include <stdlib.h>
401302dabdSTim J. Robbins #include <wchar.h>
411302dabdSTim J. Robbins #include <wctype.h>
423c87aa1dSDavid Chisnall #include "xlocale_private.h"
431302dabdSTim J. Robbins 
441302dabdSTim J. Robbins /*
451302dabdSTim J. Robbins  * Convert a wide character string to an unsigned long long integer.
461302dabdSTim J. Robbins  */
471302dabdSTim J. Robbins unsigned long long
wcstoull_l(const wchar_t * __restrict nptr,wchar_t ** __restrict endptr,int base,locale_t locale)483c87aa1dSDavid Chisnall wcstoull_l(const wchar_t * __restrict nptr, wchar_t ** __restrict endptr,
493c87aa1dSDavid Chisnall     int base, locale_t locale)
501302dabdSTim J. Robbins {
511302dabdSTim J. Robbins 	const wchar_t *s;
521302dabdSTim J. Robbins 	unsigned long long acc;
531302dabdSTim J. Robbins 	wchar_t c;
541302dabdSTim J. Robbins 	unsigned long long cutoff;
551302dabdSTim J. Robbins 	int neg, any, cutlim;
563c87aa1dSDavid Chisnall 	FIX_LOCALE(locale);
571302dabdSTim J. Robbins 
581302dabdSTim J. Robbins 	/*
591302dabdSTim J. Robbins 	 * See strtoull for comments as to the logic used.
601302dabdSTim J. Robbins 	 */
611302dabdSTim J. Robbins 	s = nptr;
621302dabdSTim J. Robbins 	do {
631302dabdSTim J. Robbins 		c = *s++;
643c87aa1dSDavid Chisnall 	} while (iswspace_l(c, locale));
651302dabdSTim J. Robbins 	if (c == L'-') {
661302dabdSTim J. Robbins 		neg = 1;
671302dabdSTim J. Robbins 		c = *s++;
681302dabdSTim J. Robbins 	} else {
691302dabdSTim J. Robbins 		neg = 0;
701302dabdSTim J. Robbins 		if (c == L'+')
711302dabdSTim J. Robbins 			c = *s++;
721302dabdSTim J. Robbins 	}
731302dabdSTim J. Robbins 	if ((base == 0 || base == 16) &&
74b8b6bef4SDag-Erling Smørgrav 	    c == L'0' && (*s == L'x' || *s == L'X') &&
75b8b6bef4SDag-Erling Smørgrav 	    ((s[1] >= L'0' && s[1] <= L'9') ||
76b8b6bef4SDag-Erling Smørgrav 	    (s[1] >= L'A' && s[1] <= L'F') ||
77b8b6bef4SDag-Erling Smørgrav 	    (s[1] >= L'a' && s[1] <= L'f'))) {
781302dabdSTim J. Robbins 		c = s[1];
791302dabdSTim J. Robbins 		s += 2;
801302dabdSTim J. Robbins 		base = 16;
811302dabdSTim J. Robbins 	}
82d9dc1603SDag-Erling Smørgrav 	if ((base == 0 || base == 2) &&
83d9dc1603SDag-Erling Smørgrav 	    c == L'0' && (*s == L'b' || *s == L'B') &&
84d9dc1603SDag-Erling Smørgrav 	    (s[1] >= L'0' && s[1] <= L'1')) {
85d9dc1603SDag-Erling Smørgrav 		c = s[1];
86d9dc1603SDag-Erling Smørgrav 		s += 2;
87d9dc1603SDag-Erling Smørgrav 		base = 2;
88d9dc1603SDag-Erling Smørgrav 	}
891302dabdSTim J. Robbins 	if (base == 0)
901302dabdSTim J. Robbins 		base = c == L'0' ? 8 : 10;
911302dabdSTim J. Robbins 	acc = any = 0;
921302dabdSTim J. Robbins 	if (base < 2 || base > 36)
931302dabdSTim J. Robbins 		goto noconv;
941302dabdSTim J. Robbins 
951302dabdSTim J. Robbins 	cutoff = ULLONG_MAX / base;
961302dabdSTim J. Robbins 	cutlim = ULLONG_MAX % base;
971302dabdSTim J. Robbins 	for ( ; ; c = *s++) {
981302dabdSTim J. Robbins #ifdef notyet
993c87aa1dSDavid Chisnall 		if (iswdigit_l(c, locale))
1003c87aa1dSDavid Chisnall 			c = digittoint_l(c, locale);
1011302dabdSTim J. Robbins 		else
1021302dabdSTim J. Robbins #endif
1031302dabdSTim J. Robbins 		if (c >= L'0' && c <= L'9')
1041302dabdSTim J. Robbins 			c -= L'0';
1051302dabdSTim J. Robbins 		else if (c >= L'A' && c <= L'Z')
1061302dabdSTim J. Robbins 			c -= L'A' - 10;
1071302dabdSTim J. Robbins 		else if (c >= L'a' && c <= L'z')
1081302dabdSTim J. Robbins 			c -= L'a' - 10;
1091302dabdSTim J. Robbins 		else
1101302dabdSTim J. Robbins 			break;
1111302dabdSTim J. Robbins 		if (c >= base)
1121302dabdSTim J. Robbins 			break;
1131302dabdSTim J. Robbins 		if (any < 0 || acc > cutoff || (acc == cutoff && c > cutlim))
1141302dabdSTim J. Robbins 			any = -1;
1151302dabdSTim J. Robbins 		else {
1161302dabdSTim J. Robbins 			any = 1;
1171302dabdSTim J. Robbins 			acc *= base;
1181302dabdSTim J. Robbins 			acc += c;
1191302dabdSTim J. Robbins 		}
1201302dabdSTim J. Robbins 	}
1211302dabdSTim J. Robbins 	if (any < 0) {
1221302dabdSTim J. Robbins 		acc = ULLONG_MAX;
1231302dabdSTim J. Robbins 		errno = ERANGE;
1241302dabdSTim J. Robbins 	} else if (!any) {
1251302dabdSTim J. Robbins noconv:
1261302dabdSTim J. Robbins 		errno = EINVAL;
1271302dabdSTim J. Robbins 	} else if (neg)
1281302dabdSTim J. Robbins 		acc = -acc;
1291302dabdSTim J. Robbins 	if (endptr != NULL)
1301302dabdSTim J. Robbins 		*endptr = (wchar_t *)(any ? s - 1 : nptr);
1311302dabdSTim J. Robbins 	return (acc);
1321302dabdSTim J. Robbins }
1333c87aa1dSDavid Chisnall unsigned long long
wcstoull(const wchar_t * __restrict nptr,wchar_t ** __restrict endptr,int base)1343c87aa1dSDavid Chisnall wcstoull(const wchar_t * __restrict nptr, wchar_t ** __restrict endptr,
1353c87aa1dSDavid Chisnall     int base)
1363c87aa1dSDavid Chisnall {
1373c87aa1dSDavid Chisnall 	return wcstoull_l(nptr, endptr, base, __get_locale());
1383c87aa1dSDavid Chisnall }
139