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