1/*-
2 * Copyright (c) 1992, 1993
3 *	The Regents of the University of California.  All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 *    notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 *    notice, this list of conditions and the following disclaimer in the
12 *    documentation and/or other materials provided with the distribution.
13 * 4. Neither the name of the University nor the names of its contributors
14 *    may be used to endorse or promote products derived from this software
15 *    without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * SUCH DAMAGE.
28 */
29
30
31#include <limits.h>
32#ifdef HAVE_ERRNO_H
33#include <errno.h>
34#endif
35#include <stdlib.h>
36#ifdef DBUS_WINCE
37#include <windows.h>
38#endif
39
40#ifndef isspace
41#define isspace(c) ((c) == ' ' || (c) == '\t' || (c) == '\r' || (c) == '\n')
42#endif
43
44/* Minimum and maximum values a `signed long long int' can hold.  */
45#ifndef LLONG_MAX
46#   define LLONG_MAX	9223372036854775807LL
47#endif
48
49#ifndef LLONG_MIN
50#   define LLONG_MIN	(-LLONG_MAX - 1LL)
51#endif
52/* Maximum value an `unsigned long long int' can hold.  (Minimum is 0.)  */
53#ifndef ULLONG_MAX
54#   define ULLONG_MAX	18446744073709551615ULL
55#endif
56
57/*
58 * Convert a string to an unsigned long long integer.
59 *
60 * Assumes that the upper and lower case
61 * alphabets and digits are each contiguous.
62 */
63unsigned long long strtoull (const char *, char **, int);
64
65unsigned long long
66strtoull(const char * nptr, char ** endptr, int base)
67{
68	const char *s;
69	unsigned long long acc;
70	char c;
71	unsigned long long cutoff;
72	int neg, any, cutlim;
73
74	/*
75	 * See strtoq for comments as to the logic used.
76	 */
77	s = nptr;
78	do {
79		c = *s++;
80	} while (isspace((unsigned char)c));
81	if (c == '-') {
82		neg = 1;
83		c = *s++;
84	} else {
85		neg = 0;
86		if (c == '+')
87			c = *s++;
88	}
89	if ((base == 0 || base == 16) &&
90	    c == '0' && (*s == 'x' || *s == 'X') &&
91	    ((s[1] >= '0' && s[1] <= '9') ||
92	    (s[1] >= 'A' && s[1] <= 'F') ||
93	    (s[1] >= 'a' && s[1] <= 'f'))) {
94		c = s[1];
95		s += 2;
96		base = 16;
97	}
98	if (base == 0)
99		base = c == '0' ? 8 : 10;
100	acc = any = 0;
101	if (base < 2 || base > 36)
102		goto noconv;
103
104	cutoff = ULLONG_MAX / base;
105	cutlim = ULLONG_MAX % base;
106	for ( ; ; c = *s++) {
107		if (c >= '0' && c <= '9')
108			c -= '0';
109		else if (c >= 'A' && c <= 'Z')
110			c -= 'A' - 10;
111		else if (c >= 'a' && c <= 'z')
112			c -= 'a' - 10;
113		else
114			break;
115		if (c >= base)
116			break;
117		if (any < 0 || acc > cutoff || (acc == cutoff && c > cutlim))
118			any = -1;
119		else {
120			any = 1;
121			acc *= base;
122			acc += c;
123		}
124	}
125	if (any < 0) {
126		acc = ULLONG_MAX;
127#ifdef DBUS_WINCE
128		SetLastError (ERROR_ARITHMETIC_OVERFLOW);
129#else
130		errno = ERANGE;
131#endif
132	} else if (!any) {
133noconv:
134#ifdef DBUS_WINCE
135		SetLastError (ERROR_INVALID_PARAMETER);
136#else
137		errno = EINVAL;
138#endif
139	} else if (neg)
140		acc = -acc;
141	if (endptr != NULL)
142		*endptr = (char *)(any ? s - 1 : nptr);
143	return (acc);
144}
145