xref: /titanic_41/usr/src/lib/libc/port/i18n/wcstoul.c (revision d89fccd8788afe1e920f842edd883fe192a1b8fe)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 
23 /*
24  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
25  * Use is subject to license terms.
26  */
27 
28 #pragma ident	"%Z%%M%	%I%	%E% SMI"
29 
30 /*	Copyright (c) 1986 AT&T	*/
31 /*	  All Rights Reserved  	*/
32 
33 
34 #ifndef	_WCS_LONGLONG
35 #pragma weak wcstoul = _wcstoul
36 #endif
37 
38 #include "synonyms.h"
39 #include <limits.h>
40 #include <errno.h>
41 #include <wchar.h>
42 #define	DIGIT(x)	(iswdigit(x) ? (x) - L'0' : \
43 			iswlower(x) ? (x) + 10 - L'a' : (x) + 10 - L'A')
44 #define	MBASE	(L'z' - L'a' + 1 + 10)
45 
46 #ifdef	_WCS_LONGLONG
47 #define	_WULONG_T	unsigned long long
48 #define	_WULONG_MAX	ULLONG_MAX
49 #else  /* _WCS_LONGLONG */
50 #define	_WULONG_T	unsigned long
51 #define	_WULONG_MAX	ULONG_MAX
52 #endif /* _WCS_LONGLONG */
53 
54 #ifdef	_WCS_LONGLONG
55 unsigned long long
56 wcstoull(const wchar_t *_RESTRICT_KYWD str, wchar_t **_RESTRICT_KYWD ptr,
57     int base)
58 #else /* _WCS_LONGLONG */
59 unsigned long
60 _wcstoul(const wchar_t *str, wchar_t **ptr, int base)
61 #endif /* _WCS_LONGLONG */
62 {
63 	_WULONG_T	val;
64 	wchar_t	c;
65 	int	xx, neg = 0;
66 	_WULONG_T	multmax;
67 
68 	if (ptr != NULL)
69 		*ptr = (wchar_t *)str; /* in case no number is formed */
70 	if (base < 0 || base > MBASE) {
71 		errno = EINVAL;
72 		return (0); /* base is invalid -- should be a fatal error */
73 	}
74 
75 	if (!iswalnum(c = *str)) {
76 		while (iswspace(c)) {
77 			c = *++str;
78 		}
79 		switch (c) {
80 		case L'-':
81 			neg++;
82 			/*FALLTHRU*/
83 		case L'+':
84 			c = *++str;
85 		}
86 	}
87 	if (base == 0) {
88 		if (c != L'0')
89 			base = 10;
90 		else if (str[1] == L'x' || str[1] == L'X')
91 			base = 16;
92 		else
93 			base = 8;
94 	}
95 	/*
96 	 * for any base > 10, the digits incrementally following
97 	 *	9 are assumed to be "abc...z" or "ABC...Z"
98 	 */
99 	if (!iswalnum(c) || (xx = DIGIT(c)) >= base) {
100 		errno = EINVAL;
101 		return (0); /* no number formed */
102 	}
103 	if (base == 16 && c == L'0' && iswxdigit(str[2]) &&
104 	    (str[1] == L'x' || str[1] == L'X')) {
105 		c = *(str += 2); /* skip over leading "0x" or "0X" */
106 	}
107 
108 	multmax = _WULONG_MAX / (_WULONG_T)base;
109 	val = DIGIT(c);
110 	for (; iswalnum(c = *++str) && (xx = DIGIT(c)) < base; ) {
111 		/* accumulate neg avoids surprises near MAXLONG */
112 		if (val > multmax)
113 			goto overflow;
114 		val *= base;
115 		if (_WULONG_MAX - val < xx)
116 			goto overflow;
117 		val += xx;
118 	}
119 	if (ptr != NULL)
120 		*ptr = (wchar_t *)str;
121 	return (neg ? -val : val);
122 
123 overflow:
124 	while (iswalnum(c = *++str) && (xx = DIGIT(c)) < base)
125 		;
126 
127 	if (ptr != NULL)
128 		*ptr = (wchar_t *)str;
129 	errno = ERANGE;
130 	return (_WULONG_MAX);
131 }
132