xref: /titanic_51/usr/src/lib/libc/port/i18n/wcstoul.c (revision 8eea8e29cc4374d1ee24c25a07f45af132db3499)
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  * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 #pragma ident	"%Z%%M%	%I%	%E% SMI"
28 
29 /*	Copyright (c) 1986 AT&T	*/
30 /*	  All Rights Reserved  	*/
31 
32 
33 #ifndef	_WCS_LONGLONG
34 #pragma weak wcstoul = _wcstoul
35 #endif
36 
37 #include "lint.h"
38 #include <limits.h>
39 #include <errno.h>
40 #include <wchar.h>
41 #define	DIGIT(x)	(iswdigit(x) ? (x) - L'0' : \
42 			iswlower(x) ? (x) + 10 - L'a' : (x) + 10 - L'A')
43 #define	MBASE	(L'z' - L'a' + 1 + 10)
44 
45 #ifdef	_WCS_LONGLONG
46 #define	_WULONG_T	unsigned long long
47 #define	_WULONG_MAX	ULLONG_MAX
48 #else  /* _WCS_LONGLONG */
49 #define	_WULONG_T	unsigned long
50 #define	_WULONG_MAX	ULONG_MAX
51 #endif /* _WCS_LONGLONG */
52 
53 #ifdef	_WCS_LONGLONG
54 unsigned long long
55 wcstoull(const wchar_t *_RESTRICT_KYWD str, wchar_t **_RESTRICT_KYWD ptr,
56     int base)
57 #else /* _WCS_LONGLONG */
58 unsigned long
59 _wcstoul(const wchar_t *str, wchar_t **ptr, int base)
60 #endif /* _WCS_LONGLONG */
61 {
62 	_WULONG_T	val;
63 	wchar_t	c;
64 	int	xx, neg = 0;
65 	_WULONG_T	multmax;
66 
67 	if (ptr != NULL)
68 		*ptr = (wchar_t *)str; /* in case no number is formed */
69 	if (base < 0 || base > MBASE) {
70 		errno = EINVAL;
71 		return (0); /* base is invalid -- should be a fatal error */
72 	}
73 
74 	if (!iswalnum(c = *str)) {
75 		while (iswspace(c)) {
76 			c = *++str;
77 		}
78 		switch (c) {
79 		case L'-':
80 			neg++;
81 			/*FALLTHRU*/
82 		case L'+':
83 			c = *++str;
84 		}
85 	}
86 	if (base == 0) {
87 		if (c != L'0')
88 			base = 10;
89 		else if (str[1] == L'x' || str[1] == L'X')
90 			base = 16;
91 		else
92 			base = 8;
93 	}
94 	/*
95 	 * for any base > 10, the digits incrementally following
96 	 *	9 are assumed to be "abc...z" or "ABC...Z"
97 	 */
98 	if (!iswalnum(c) || (xx = DIGIT(c)) >= base) {
99 		errno = EINVAL;
100 		return (0); /* no number formed */
101 	}
102 	if (base == 16 && c == L'0' && iswxdigit(str[2]) &&
103 	    (str[1] == L'x' || str[1] == L'X')) {
104 		c = *(str += 2); /* skip over leading "0x" or "0X" */
105 	}
106 
107 	multmax = _WULONG_MAX / (_WULONG_T)base;
108 	val = DIGIT(c);
109 	for (; iswalnum(c = *++str) && (xx = DIGIT(c)) < base; ) {
110 		/* accumulate neg avoids surprises near MAXLONG */
111 		if (val > multmax)
112 			goto overflow;
113 		val *= base;
114 		if (_WULONG_MAX - val < xx)
115 			goto overflow;
116 		val += xx;
117 	}
118 	if (ptr != NULL)
119 		*ptr = (wchar_t *)str;
120 	return (neg ? -val : val);
121 
122 overflow:
123 	while (iswalnum(c = *++str) && (xx = DIGIT(c)) < base)
124 		;
125 
126 	if (ptr != NULL)
127 		*ptr = (wchar_t *)str;
128 	errno = ERANGE;
129 	return (_WULONG_MAX);
130 }
131