_strtoul.h revision 1.10
133965Sjdp/* $NetBSD: _strtoul.h,v 1.10 2016/11/05 21:11:30 riastradh Exp $ */
2218822Sdim
3218822Sdim/*-
433965Sjdp * Copyright (c) 1990, 1993
533965Sjdp *	The Regents of the University of California.  All rights reserved.
6104834Sobrien *
733965Sjdp * Redistribution and use in source and binary forms, with or without
8104834Sobrien * modification, are permitted provided that the following conditions
9104834Sobrien * are met:
10104834Sobrien * 1. Redistributions of source code must retain the above copyright
11104834Sobrien *    notice, this list of conditions and the following disclaimer.
1233965Sjdp * 2. Redistributions in binary form must reproduce the above copyright
13104834Sobrien *    notice, this list of conditions and the following disclaimer in the
14104834Sobrien *    documentation and/or other materials provided with the distribution.
15104834Sobrien * 3. Neither the name of the University nor the names of its contributors
16104834Sobrien *    may be used to endorse or promote products derived from this software
1733965Sjdp *    without specific prior written permission.
18104834Sobrien *
19104834Sobrien * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20218822Sdim * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2133965Sjdp * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2233965Sjdp * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
2333965Sjdp * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2433965Sjdp * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2533965Sjdp * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2633965Sjdp * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2733965Sjdp * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2833965Sjdp * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2933965Sjdp * SUCH DAMAGE.
3033965Sjdp *
3133965Sjdp * Original version ID:
3233965Sjdp * NetBSD: src/lib/libc/locale/_wcstoul.h,v 1.2 2003/08/07 16:43:03 agc Exp
3333965Sjdp */
3433965Sjdp
3533965Sjdp/*
3633965Sjdp * function template for strtoul, strtoull and strtoumax.
3733965Sjdp *
3889857Sobrien * parameters:
3989857Sobrien *	_FUNCNAME  : function name
4033965Sjdp *      __UINT     : return type
4133965Sjdp *      __UINT_MAX : upper limit of the return type
4233965Sjdp */
4333965Sjdp#if defined(_KERNEL) || defined(_STANDALONE) || \
4489857Sobrien    defined(HAVE_NBTOOL_CONFIG_H) || defined(BCS_ONLY)
4589857Sobrien__UINT
4689857Sobrien_FUNCNAME(const char *nptr, char **endptr, int base)
4789857Sobrien#else
4889857Sobrien#include <locale.h>
4989857Sobrien#include "setlocale_local.h"
5089857Sobrien#define INT_FUNCNAME_(pre, name, post)	pre ## name ## post
5189857Sobrien#define INT_FUNCNAME(pre, name, post)	INT_FUNCNAME_(pre, name, post)
5289857Sobrien
5333965Sjdpstatic __UINT
5433965SjdpINT_FUNCNAME(_int_, _FUNCNAME, _l)(const char *nptr, char **endptr,
5533965Sjdp				   int base, locale_t loc)
5633965Sjdp#endif
5733965Sjdp{
5833965Sjdp	const char *s;
5933965Sjdp	__UINT acc, cutoff;
6033965Sjdp	unsigned char c;
6133965Sjdp	int i, neg, any, cutlim;
6233965Sjdp
6333965Sjdp	_DIAGASSERT(nptr != NULL);
6433965Sjdp	/* endptr may be NULL */
6533965Sjdp
6633965Sjdp	/* check base value */
6733965Sjdp	if (base && (base < 2 || base > 36)) {
6833965Sjdp#if !defined(_KERNEL) && !defined(_STANDALONE)
69130561Sobrien		errno = EINVAL;
70130561Sobrien		if (endptr != NULL)
71130561Sobrien			/* LINTED interface specification */
72130561Sobrien			*endptr = __UNCONST(nptr);
73130561Sobrien		return 0;
74130561Sobrien#else
75130561Sobrien		panic("%s: invalid base %d", __func__, base);
76130561Sobrien#endif
7733965Sjdp	}
7833965Sjdp
7933965Sjdp	/*
8033965Sjdp	 * Skip white space and pick up leading +/- sign if any.
8133965Sjdp	 * If base is 0, allow 0x for hex and 0 for octal, else
8233965Sjdp	 * assume decimal; if base is already 16, allow 0x.
8333965Sjdp	 */
84130561Sobrien	s = nptr;
8533965Sjdp#if defined(_KERNEL) || defined(_STANDALONE) || \
8633965Sjdp    defined(HAVE_NBTOOL_CONFIG_H) || defined(BCS_ONLY)
8733965Sjdp	do {
8833965Sjdp		c = *s++;
8933965Sjdp	} while (isspace(c));
9033965Sjdp#else
9133965Sjdp	do {
9233965Sjdp		c = *s++;
9333965Sjdp	} while (isspace_l(c, loc));
9433965Sjdp#endif
95218822Sdim	if (c == '-') {
96218822Sdim		neg = 1;
97218822Sdim		c = *s++;
98218822Sdim	} else {
99218822Sdim		neg = 0;
100218822Sdim		if (c == '+')
101218822Sdim			c = *s++;
102218822Sdim	}
103218822Sdim	if ((base == 0 || base == 16) &&
104218822Sdim	    c == '0' && (*s == 'x' || *s == 'X')) {
105218822Sdim		c = s[1];
106218822Sdim		s += 2;
107218822Sdim		base = 16;
108218822Sdim#if 0
109218822Sdim	} else if ((base == 0 || base == 2) &&
110218822Sdim	    c == '0' && (*s == 'b' || *s == 'B')) {
111218822Sdim		c = s[1];
112218822Sdim		s += 2;
113218822Sdim		base = 2;
114218822Sdim#endif
11533965Sjdp	} else if (base == 0)
116218822Sdim		base = (c == '0' ? 8 : 10);
11733965Sjdp
11833965Sjdp	/*
11933965Sjdp	 * See strtol for comments as to the logic used.
12033965Sjdp	 */
121218822Sdim	cutoff = ((__UINT)__UINT_MAX / (__UINT)base);
122218822Sdim	cutlim = (int)((__UINT)__UINT_MAX % (__UINT)base);
12333965Sjdp	for (acc = 0, any = 0;; c = *s++) {
12433965Sjdp		if (c >= '0' && c <= '9')
12533965Sjdp			i = c - '0';
12633965Sjdp		else if (c >= 'a' && c <= 'z')
12733965Sjdp			i = (c - 'a') + 10;
128218822Sdim		else if (c >= 'A' && c <= 'Z')
12933965Sjdp			i = (c - 'A') + 10;
13033965Sjdp		else
13133965Sjdp			break;
13233965Sjdp		if (i >= base)
13333965Sjdp			break;
13433965Sjdp		if (any < 0)
135218822Sdim			continue;
13633965Sjdp		if (acc > cutoff || (acc == cutoff && i > cutlim)) {
137130561Sobrien			acc = __UINT_MAX;
138130561Sobrien#if !defined(_KERNEL) && !defined(_STANDALONE)
139130561Sobrien			any = -1;
140130561Sobrien			errno = ERANGE;
141130561Sobrien#else
142130561Sobrien			any = 0;
143130561Sobrien			break;
144130561Sobrien#endif
14533965Sjdp		} else {
14633965Sjdp			any = 1;
14733965Sjdp			acc *= (__UINT)base;
14833965Sjdp			acc += i;
14933965Sjdp		}
150218822Sdim	}
15133965Sjdp	if (neg && any > 0)
15233965Sjdp		acc = -acc;
15333965Sjdp	if (endptr != NULL)
15433965Sjdp		/* LINTED interface specification */
15533965Sjdp		*endptr = __UNCONST(any ? s - 1 : nptr);
15633965Sjdp	return(acc);
15733965Sjdp}
15833965Sjdp
15933965Sjdp#if !defined(_KERNEL) && !defined(_STANDALONE) && \
16033965Sjdp    !defined(HAVE_NBTOOL_CONFIG_H) && !defined(BCS_ONLY)
16133965Sjdp__UINT
16233965Sjdp_FUNCNAME(const char *nptr, char **endptr, int base)
16333965Sjdp{
16433965Sjdp	return INT_FUNCNAME(_int_, _FUNCNAME, _l)(nptr, endptr, base, _current_locale());
16533965Sjdp}
16633965Sjdp
16733965Sjdp__UINT
16833965SjdpINT_FUNCNAME(, _FUNCNAME, _l)(const char *nptr, char **endptr, int base, locale_t loc)
16933965Sjdp{
17033965Sjdp	return INT_FUNCNAME(_int_, _FUNCNAME, _l)(nptr, endptr, base, loc);
17133965Sjdp}
172130561Sobrien#endif
17389857Sobrien