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