Blame lib/isc/strtoul.c

Packit Service ae04f2
/*
Packit Service ae04f2
 * Copyright (C) Internet Systems Consortium, Inc. ("ISC")
Packit Service ae04f2
 *
Packit Service ae04f2
 * This Source Code Form is subject to the terms of the Mozilla Public
Packit Service ae04f2
 * License, v. 2.0. If a copy of the MPL was not distributed with this
Packit Service ae04f2
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
Packit Service ae04f2
 *
Packit Service ae04f2
 * See the COPYRIGHT file distributed with this work for additional
Packit Service ae04f2
 * information regarding copyright ownership.
Packit Service ae04f2
 */
Packit Service ae04f2
Packit Service ae04f2
/*
Packit Service ae04f2
 * Copyright (c) 1990, 1993
Packit Service ae04f2
 *	The Regents of the University of California.  All rights reserved.
Packit Service ae04f2
 *
Packit Service ae04f2
 * Redistribution and use in source and binary forms, with or without
Packit Service ae04f2
 * modification, are permitted provided that the following conditions
Packit Service ae04f2
 * are met:
Packit Service ae04f2
 * 1. Redistributions of source code must retain the above copyright
Packit Service ae04f2
 *    notice, this list of conditions and the following disclaimer.
Packit Service ae04f2
 * 2. Redistributions in binary form must reproduce the above copyright
Packit Service ae04f2
 *    notice, this list of conditions and the following disclaimer in the
Packit Service ae04f2
 *    documentation and/or other materials provided with the distribution.
Packit Service ae04f2
 * 3. Neither the name of the University nor the names of its contributors
Packit Service ae04f2
 *    may be used to endorse or promote products derived from this software
Packit Service ae04f2
 *    without specific prior written permission.
Packit Service ae04f2
 *
Packit Service ae04f2
 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
Packit Service ae04f2
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
Packit Service ae04f2
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
Packit Service ae04f2
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
Packit Service ae04f2
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
Packit Service ae04f2
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
Packit Service ae04f2
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
Packit Service ae04f2
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
Packit Service ae04f2
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
Packit Service ae04f2
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
Packit Service ae04f2
 * SUCH DAMAGE.
Packit Service ae04f2
 */
Packit Service ae04f2
Packit Service ae04f2
#include <config.h>
Packit Service ae04f2
Packit Service ae04f2
/*! \file */
Packit Service ae04f2
#if defined(LIBC_SCCS) && !defined(lint)
Packit Service ae04f2
static char sccsid[] = "@(#)strtoul.c	8.1 (Berkeley) 6/4/93";
Packit Service ae04f2
#endif /* LIBC_SCCS and not lint */
Packit Service ae04f2
Packit Service ae04f2
#include <limits.h>
Packit Service ae04f2
#include <ctype.h>
Packit Service ae04f2
#include <errno.h>
Packit Service ae04f2
Packit Service ae04f2
#include <isc/stdlib.h>
Packit Service ae04f2
#include <isc/util.h>
Packit Service ae04f2
Packit Service ae04f2
/*!
Packit Service ae04f2
 * Convert a string to an unsigned long integer.
Packit Service ae04f2
 *
Packit Service ae04f2
 * Ignores `locale' stuff.  Assumes that the upper and lower case
Packit Service ae04f2
 * alphabets and digits are each contiguous.
Packit Service ae04f2
 */
Packit Service ae04f2
unsigned long
Packit Service ae04f2
isc_strtoul(const char *nptr, char **endptr, int base) {
Packit Service ae04f2
	const char *s = nptr;
Packit Service ae04f2
	unsigned long acc;
Packit Service ae04f2
	unsigned char c;
Packit Service ae04f2
	unsigned long cutoff;
Packit Service ae04f2
	int neg = 0, any, cutlim;
Packit Service ae04f2
Packit Service ae04f2
	/*
Packit Service ae04f2
	 * See strtol for comments as to the logic used.
Packit Service ae04f2
	 */
Packit Service ae04f2
	do {
Packit Service ae04f2
		c = *s++;
Packit Service ae04f2
	} while (isspace(c));
Packit Service ae04f2
	if (c == '-') {
Packit Service ae04f2
		neg = 1;
Packit Service ae04f2
		c = *s++;
Packit Service ae04f2
	} else if (c == '+')
Packit Service ae04f2
		c = *s++;
Packit Service ae04f2
	if ((base == 0 || base == 16) &&
Packit Service ae04f2
	    c == '0' && (*s == 'x' || *s == 'X')) {
Packit Service ae04f2
		c = s[1];
Packit Service ae04f2
		s += 2;
Packit Service ae04f2
		base = 16;
Packit Service ae04f2
	}
Packit Service ae04f2
	if (base == 0)
Packit Service ae04f2
		base = c == '0' ? 8 : 10;
Packit Service ae04f2
	cutoff = (unsigned long)ULONG_MAX / (unsigned long)base;
Packit Service ae04f2
	cutlim = (unsigned long)ULONG_MAX % (unsigned long)base;
Packit Service ae04f2
	for (acc = 0, any = 0;; c = *s++) {
Packit Service ae04f2
		if (!isascii(c))
Packit Service ae04f2
			break;
Packit Service ae04f2
		if (isdigit(c))
Packit Service ae04f2
			c -= '0';
Packit Service ae04f2
		else if (isalpha(c))
Packit Service ae04f2
			c -= isupper(c) ? 'A' - 10 : 'a' - 10;
Packit Service ae04f2
		else
Packit Service ae04f2
			break;
Packit Service ae04f2
		if (c >= base)
Packit Service ae04f2
			break;
Packit Service ae04f2
		if (any < 0 || acc > cutoff || (acc == cutoff && c > cutlim))
Packit Service ae04f2
			any = -1;
Packit Service ae04f2
		else {
Packit Service ae04f2
			any = 1;
Packit Service ae04f2
			acc *= base;
Packit Service ae04f2
			acc += c;
Packit Service ae04f2
		}
Packit Service ae04f2
	}
Packit Service ae04f2
	if (any < 0) {
Packit Service ae04f2
		acc = ULONG_MAX;
Packit Service ae04f2
		errno = ERANGE;
Packit Service ae04f2
	} else if (neg)
Packit Service ae04f2
		acc = -acc;
Packit Service ae04f2
	if (endptr != 0)
Packit Service ae04f2
		DE_CONST(any ? s - 1 : nptr, *endptr);
Packit Service ae04f2
	return (acc);
Packit Service ae04f2
}