Blame snmplib/strtoul.c

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