1248613Sdes/*	$OpenBSD: strtoull.c,v 1.5 2005/08/08 08:05:37 espie Exp $ */
2248613Sdes/*-
3248613Sdes * Copyright (c) 1992 The Regents of the University of California.
4248613Sdes * All rights reserved.
5248613Sdes *
6248613Sdes * Redistribution and use in source and binary forms, with or without
7248613Sdes * modification, are permitted provided that the following conditions
8248613Sdes * are met:
9248613Sdes * 1. Redistributions of source code must retain the above copyright
10248613Sdes *    notice, this list of conditions and the following disclaimer.
11248613Sdes * 2. Redistributions in binary form must reproduce the above copyright
12248613Sdes *    notice, this list of conditions and the following disclaimer in the
13248613Sdes *    documentation and/or other materials provided with the distribution.
14248613Sdes * 3. Neither the name of the University nor the names of its contributors
15248613Sdes *    may be used to endorse or promote products derived from this software
16248613Sdes *    without specific prior written permission.
17248613Sdes *
18248613Sdes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
19248613Sdes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20248613Sdes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21248613Sdes * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
22248613Sdes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23248613Sdes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24248613Sdes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25248613Sdes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26248613Sdes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27248613Sdes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28248613Sdes * SUCH DAMAGE.
29248613Sdes */
30248613Sdes
31248613Sdes/* OPENBSD ORIGINAL: lib/libc/stdlib/strtoull.c */
32248613Sdes
33248613Sdes#include "includes.h"
34248613Sdes#ifndef HAVE_STRTOULL
35248613Sdes
36248613Sdes#include <sys/types.h>
37248613Sdes
38248613Sdes#include <ctype.h>
39248613Sdes#include <errno.h>
40248613Sdes#include <limits.h>
41248613Sdes#include <stdlib.h>
42248613Sdes
43248613Sdes/*
44248613Sdes * Convert a string to an unsigned long long.
45248613Sdes *
46248613Sdes * Ignores `locale' stuff.  Assumes that the upper and lower case
47248613Sdes * alphabets and digits are each contiguous.
48248613Sdes */
49248613Sdesunsigned long long
50248613Sdesstrtoull(const char *nptr, char **endptr, int base)
51248613Sdes{
52248613Sdes	const char *s;
53248613Sdes	unsigned long long acc, cutoff;
54248613Sdes	int c;
55248613Sdes	int neg, any, cutlim;
56248613Sdes
57248613Sdes	/*
58248613Sdes	 * See strtoq for comments as to the logic used.
59248613Sdes	 */
60248613Sdes	s = nptr;
61248613Sdes	do {
62248613Sdes		c = (unsigned char) *s++;
63248613Sdes	} while (isspace(c));
64248613Sdes	if (c == '-') {
65248613Sdes		neg = 1;
66248613Sdes		c = *s++;
67248613Sdes	} else {
68248613Sdes		neg = 0;
69248613Sdes		if (c == '+')
70248613Sdes			c = *s++;
71248613Sdes	}
72248613Sdes	if ((base == 0 || base == 16) &&
73248613Sdes	    c == '0' && (*s == 'x' || *s == 'X')) {
74248613Sdes		c = s[1];
75248613Sdes		s += 2;
76248613Sdes		base = 16;
77248613Sdes	}
78248613Sdes	if (base == 0)
79248613Sdes		base = c == '0' ? 8 : 10;
80248613Sdes
81248613Sdes	cutoff = ULLONG_MAX / (unsigned long long)base;
82248613Sdes	cutlim = ULLONG_MAX % (unsigned long long)base;
83248613Sdes	for (acc = 0, any = 0;; c = (unsigned char) *s++) {
84248613Sdes		if (isdigit(c))
85248613Sdes			c -= '0';
86248613Sdes		else if (isalpha(c))
87248613Sdes			c -= isupper(c) ? 'A' - 10 : 'a' - 10;
88248613Sdes		else
89248613Sdes			break;
90248613Sdes		if (c >= base)
91248613Sdes			break;
92248613Sdes		if (any < 0)
93248613Sdes			continue;
94248613Sdes		if (acc > cutoff || (acc == cutoff && c > cutlim)) {
95248613Sdes			any = -1;
96248613Sdes			acc = ULLONG_MAX;
97248613Sdes			errno = ERANGE;
98248613Sdes		} else {
99248613Sdes			any = 1;
100248613Sdes			acc *= (unsigned long long)base;
101248613Sdes			acc += c;
102248613Sdes		}
103248613Sdes	}
104248613Sdes	if (neg && any > 0)
105248613Sdes		acc = -acc;
106248613Sdes	if (endptr != 0)
107248613Sdes		*endptr = (char *) (any ? s - 1 : nptr);
108248613Sdes	return (acc);
109248613Sdes}
110248613Sdes#endif /* !HAVE_STRTOULL */
111