2N/A/*
2N/A * CDDL HEADER START
2N/A *
2N/A * The contents of this file are subject to the terms of the
2N/A * Common Development and Distribution License (the "License").
2N/A * You may not use this file except in compliance with the License.
2N/A *
2N/A * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
2N/A * or http://www.opensolaris.org/os/licensing.
2N/A * See the License for the specific language governing permissions
2N/A * and limitations under the License.
2N/A *
2N/A * When distributing Covered Code, include this CDDL HEADER in each
2N/A * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
2N/A * If applicable, add the following below this CDDL HEADER, with the
2N/A * fields enclosed by brackets "[]" replaced with your own identifying
2N/A * information: Portions Copyright [yyyy] [name of copyright owner]
2N/A *
2N/A * CDDL HEADER END
2N/A */
2N/A
2N/A/*
2N/A * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
2N/A * Use is subject to license terms.
2N/A */
2N/A
2N/A/* Copyright (c) 1986 AT&T */
2N/A/* All Rights Reserved */
2N/A
2N/A#pragma ident "%Z%%M% %I% %E% SMI"
2N/A
2N/A#ifndef _WCS_LONGLONG
2N/A#pragma weak _wcstoul = wcstoul
2N/A#endif
2N/A
2N/A#include "lint.h"
2N/A#include <limits.h>
2N/A#include <errno.h>
2N/A#include <wchar.h>
2N/A#define DIGIT(x) (iswdigit(x) ? (x) - L'0' : \
2N/A iswlower(x) ? (x) + 10 - L'a' : (x) + 10 - L'A')
2N/A#define MBASE (L'z' - L'a' + 1 + 10)
2N/A
2N/A#ifdef _WCS_LONGLONG
2N/A#define _WULONG_T unsigned long long
2N/A#define _WULONG_MAX ULLONG_MAX
2N/A#else /* _WCS_LONGLONG */
2N/A#define _WULONG_T unsigned long
2N/A#define _WULONG_MAX ULONG_MAX
2N/A#endif /* _WCS_LONGLONG */
2N/A
2N/A#ifdef _WCS_LONGLONG
2N/Aunsigned long long
2N/Awcstoull(const wchar_t *_RESTRICT_KYWD str, wchar_t **_RESTRICT_KYWD ptr,
2N/A int base)
2N/A#else /* _WCS_LONGLONG */
2N/Aunsigned long
2N/Awcstoul(const wchar_t *str, wchar_t **ptr, int base)
2N/A#endif /* _WCS_LONGLONG */
2N/A{
2N/A _WULONG_T val;
2N/A wchar_t c;
2N/A int xx, neg = 0;
2N/A _WULONG_T multmax;
2N/A
2N/A if (ptr != NULL)
2N/A *ptr = (wchar_t *)str; /* in case no number is formed */
2N/A if (base < 0 || base > MBASE) {
2N/A errno = EINVAL;
2N/A return (0); /* base is invalid -- should be a fatal error */
2N/A }
2N/A
2N/A if (!iswalnum(c = *str)) {
2N/A while (iswspace(c)) {
2N/A c = *++str;
2N/A }
2N/A switch (c) {
2N/A case L'-':
2N/A neg++;
2N/A /*FALLTHRU*/
2N/A case L'+':
2N/A c = *++str;
2N/A }
2N/A }
2N/A if (base == 0) {
2N/A if (c != L'0')
2N/A base = 10;
2N/A else if (str[1] == L'x' || str[1] == L'X')
2N/A base = 16;
2N/A else
2N/A base = 8;
2N/A }
2N/A /*
2N/A * for any base > 10, the digits incrementally following
2N/A * 9 are assumed to be "abc...z" or "ABC...Z"
2N/A */
2N/A if (!iswalnum(c) || (xx = DIGIT(c)) >= base) {
2N/A errno = EINVAL;
2N/A return (0); /* no number formed */
2N/A }
2N/A if (base == 16 && c == L'0' && iswxdigit(str[2]) &&
2N/A (str[1] == L'x' || str[1] == L'X')) {
2N/A c = *(str += 2); /* skip over leading "0x" or "0X" */
2N/A }
2N/A
2N/A multmax = _WULONG_MAX / (_WULONG_T)base;
2N/A val = DIGIT(c);
2N/A for (; iswalnum(c = *++str) && (xx = DIGIT(c)) < base; ) {
2N/A /* accumulate neg avoids surprises near MAXLONG */
2N/A if (val > multmax)
2N/A goto overflow;
2N/A val *= base;
2N/A if (_WULONG_MAX - val < xx)
2N/A goto overflow;
2N/A val += xx;
2N/A }
2N/A if (ptr != NULL)
2N/A *ptr = (wchar_t *)str;
2N/A return (neg ? -val : val);
2N/A
2N/Aoverflow:
2N/A while (iswalnum(c = *++str) && (xx = DIGIT(c)) < base)
2N/A ;
2N/A
2N/A if (ptr != NULL)
2N/A *ptr = (wchar_t *)str;
2N/A errno = ERANGE;
2N/A return (_WULONG_MAX);
2N/A}