1219019Sgabor/* $FreeBSD$ */
2219019Sgabor/* $NetBSD: _strtol.h,v 1.2 2009/05/20 22:03:29 christos Exp $ */
3219019Sgabor
4219019Sgabor/*-
5219019Sgabor * Copyright (c) 1990, 1993
6219019Sgabor *	The Regents of the University of California.  All rights reserved.
7219019Sgabor *
8219019Sgabor * Redistribution and use in source and binary forms, with or without
9219019Sgabor * modification, are permitted provided that the following conditions
10219019Sgabor * are met:
11219019Sgabor * 1. Redistributions of source code must retain the above copyright
12219019Sgabor *    notice, this list of conditions and the following disclaimer.
13219019Sgabor * 2. Redistributions in binary form must reproduce the above copyright
14219019Sgabor *    notice, this list of conditions and the following disclaimer in the
15219019Sgabor *    documentation and/or other materials provided with the distribution.
16219019Sgabor * 3. Neither the name of the University nor the names of its contributors
17219019Sgabor *    may be used to endorse or promote products derived from this software
18219019Sgabor *    without specific prior written permission.
19219019Sgabor *
20219019Sgabor * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21219019Sgabor * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22219019Sgabor * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23219019Sgabor * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24219019Sgabor * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25219019Sgabor * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26219019Sgabor * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27219019Sgabor * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28219019Sgabor * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29219019Sgabor * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30219019Sgabor * SUCH DAMAGE.
31219019Sgabor *
32219019Sgabor * Original version ID:
33219019Sgabor * NetBSD: src/lib/libc/locale/_wcstol.h,v 1.2 2003/08/07 16:43:03 agc Exp
34219019Sgabor */
35219019Sgabor
36219019Sgabor/*
37219019Sgabor * function template for strtol, strtoll and strtoimax.
38219019Sgabor *
39219019Sgabor * parameters:
40219019Sgabor *	_FUNCNAME : function name
41219019Sgabor *      __INT     : return type
42219019Sgabor *      __INT_MIN : lower limit of the return type
43219019Sgabor *      __INT_MAX : upper limit of the return type
44219019Sgabor */
45219019Sgabor
46219019Sgabor__INT
47219019Sgabor_FUNCNAME(const char *nptr, char **endptr, int base)
48219019Sgabor{
49219019Sgabor	const char *s;
50219019Sgabor	__INT acc, cutoff;
51219019Sgabor	unsigned char c;
52219019Sgabor	int any, cutlim, i, neg;
53219019Sgabor
54219019Sgabor	/* check base value */
55219019Sgabor	if (base && (base < 2 || base > 36)) {
56219019Sgabor#if !defined(_KERNEL) && !defined(_STANDALONE)
57219019Sgabor		errno = EINVAL;
58219019Sgabor		if (endptr != NULL)
59219019Sgabor			/* LINTED interface specification */
60219019Sgabor			*endptr = __DECONST(void *, nptr);
61219019Sgabor		return (0);
62219019Sgabor#else
63219019Sgabor		panic("%s: invalid base %d", __func__, base);
64219019Sgabor#endif
65219019Sgabor	}
66219019Sgabor
67219019Sgabor	/*
68219019Sgabor	 * Skip white space and pick up leading +/- sign if any.
69219019Sgabor	 * If base is 0, allow 0x for hex and 0 for octal, else
70219019Sgabor	 * assume decimal; if base is already 16, allow 0x.
71219019Sgabor	 */
72219019Sgabor	s = nptr;
73219019Sgabor	do {
74219019Sgabor		c = *s++;
75219019Sgabor	} while (isspace(c));
76219019Sgabor	if (c == '-') {
77219019Sgabor		neg = 1;
78219019Sgabor		c = *s++;
79219019Sgabor	} else {
80219019Sgabor		neg = 0;
81219019Sgabor		if (c == '+')
82219019Sgabor			c = *s++;
83219019Sgabor	}
84219019Sgabor	if ((base == 0 || base == 16) &&
85219019Sgabor	    c == '0' && (*s == 'x' || *s == 'X')) {
86219019Sgabor		c = s[1];
87219019Sgabor		s += 2;
88219019Sgabor		base = 16;
89219019Sgabor	}
90219019Sgabor	if (base == 0)
91219019Sgabor		base = (c == '0' ? 8 : 10);
92219019Sgabor
93219019Sgabor	/*
94219019Sgabor	 * Compute the cutoff value between legal numbers and illegal
95219019Sgabor	 * numbers.  That is the largest legal value, divided by the
96219019Sgabor	 * base.  An input number that is greater than this value, if
97219019Sgabor	 * followed by a legal input character, is too big.  One that
98219019Sgabor	 * is equal to this value may be valid or not; the limit
99219019Sgabor	 * between valid and invalid numbers is then based on the last
100219019Sgabor	 * digit.  For instance, if the range for longs is
101219019Sgabor	 * [-2147483648..2147483647] and the input base is 10,
102219019Sgabor	 * cutoff will be set to 214748364 and cutlim to either
103219019Sgabor	 * 7 (neg==0) or 8 (neg==1), meaning that if we have accumulated
104219019Sgabor	 * a value > 214748364, or equal but the next digit is > 7 (or 8),
105219019Sgabor	 * the number is too big, and we will return a range error.
106219019Sgabor	 *
107219019Sgabor	 * Set any if any `digits' consumed; make it negative to indicate
108219019Sgabor	 * overflow.
109219019Sgabor	 */
110219019Sgabor	cutoff = (neg ? __INT_MIN : __INT_MAX);
111219019Sgabor	cutlim = (int)(cutoff % base);
112219019Sgabor	cutoff /= base;
113219019Sgabor	if (neg) {
114219019Sgabor		if (cutlim > 0) {
115219019Sgabor			cutlim -= base;
116219019Sgabor			cutoff += 1;
117219019Sgabor		}
118219019Sgabor		cutlim = -cutlim;
119219019Sgabor	}
120219019Sgabor	for (acc = 0, any = 0;; c = *s++) {
121219019Sgabor		if (isdigit(c))
122219019Sgabor			i = c - '0';
123219019Sgabor		else if (isalpha(c))
124219019Sgabor			i = c - (isupper(c) ? 'A' - 10 : 'a' - 10);
125219019Sgabor		else
126219019Sgabor			break;
127219019Sgabor		if (i >= base)
128219019Sgabor			break;
129219019Sgabor		if (any < 0)
130219019Sgabor			continue;
131219019Sgabor		if (neg) {
132219019Sgabor			if (acc < cutoff || (acc == cutoff && i > cutlim)) {
133219019Sgabor				acc = __INT_MIN;
134219019Sgabor#if !defined(_KERNEL) && !defined(_STANDALONE)
135219019Sgabor				any = -1;
136219019Sgabor				errno = ERANGE;
137219019Sgabor#else
138219019Sgabor				any = 0;
139219019Sgabor				break;
140219019Sgabor#endif
141219019Sgabor			} else {
142219019Sgabor				any = 1;
143219019Sgabor				acc *= base;
144219019Sgabor				acc -= i;
145219019Sgabor			}
146219019Sgabor		} else {
147219019Sgabor			if (acc > cutoff || (acc == cutoff && i > cutlim)) {
148219019Sgabor				acc = __INT_MAX;
149219019Sgabor#if !defined(_KERNEL) && !defined(_STANDALONE)
150219019Sgabor				any = -1;
151219019Sgabor				errno = ERANGE;
152219019Sgabor#else
153219019Sgabor				any = 0;
154219019Sgabor				break;
155219019Sgabor#endif
156219019Sgabor			} else {
157219019Sgabor				any = 1;
158219019Sgabor				acc *= base;
159219019Sgabor				acc += i;
160219019Sgabor			}
161219019Sgabor		}
162219019Sgabor	}
163219019Sgabor	if (endptr != NULL)
164219019Sgabor		/* LINTED interface specification */
165219019Sgabor		*endptr = __DECONST(void *, any ? s - 1 : nptr);
166219019Sgabor	return(acc);
167219019Sgabor}
168