"Fossies" - the Fresh Open Source Software Archive

Member "bind-9.11.23/lib/isc/strtoul.c" (7 Sep 2020, 3569 Bytes) of package /linux/misc/dns/bind9/9.11.23/bind-9.11.23.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "strtoul.c" see the Fossies "Dox" file reference documentation.

    1 /*
    2  * Copyright (C) Internet Systems Consortium, Inc. ("ISC")
    3  *
    4  * This Source Code Form is subject to the terms of the Mozilla Public
    5  * License, v. 2.0. If a copy of the MPL was not distributed with this
    6  * file, You can obtain one at http://mozilla.org/MPL/2.0/.
    7  *
    8  * See the COPYRIGHT file distributed with this work for additional
    9  * information regarding copyright ownership.
   10  */
   11 
   12 /*
   13  * Copyright (c) 1990, 1993
   14  *  The Regents of the University of California.  All rights reserved.
   15  *
   16  * Redistribution and use in source and binary forms, with or without
   17  * modification, are permitted provided that the following conditions
   18  * are met:
   19  * 1. Redistributions of source code must retain the above copyright
   20  *    notice, this list of conditions and the following disclaimer.
   21  * 2. Redistributions in binary form must reproduce the above copyright
   22  *    notice, this list of conditions and the following disclaimer in the
   23  *    documentation and/or other materials provided with the distribution.
   24  * 3. Neither the name of the University nor the names of its contributors
   25  *    may be used to endorse or promote products derived from this software
   26  *    without specific prior written permission.
   27  *
   28  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
   29  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
   30  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
   31  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
   32  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
   33  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
   34  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
   35  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
   36  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
   37  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   38  * SUCH DAMAGE.
   39  */
   40 
   41 #include <config.h>
   42 
   43 /*! \file */
   44 #if defined(LIBC_SCCS) && !defined(lint)
   45 static char sccsid[] = "@(#)strtoul.c   8.1 (Berkeley) 6/4/93";
   46 #endif /* LIBC_SCCS and not lint */
   47 
   48 #include <limits.h>
   49 #include <ctype.h>
   50 #include <errno.h>
   51 
   52 #include <isc/stdlib.h>
   53 #include <isc/util.h>
   54 
   55 /*!
   56  * Convert a string to an unsigned long integer.
   57  *
   58  * Ignores `locale' stuff.  Assumes that the upper and lower case
   59  * alphabets and digits are each contiguous.
   60  */
   61 unsigned long
   62 isc_strtoul(const char *nptr, char **endptr, int base) {
   63     const char *s = nptr;
   64     unsigned long acc;
   65     unsigned char c;
   66     unsigned long cutoff;
   67     int neg = 0, any, cutlim;
   68 
   69     /*
   70      * See strtol for comments as to the logic used.
   71      */
   72     do {
   73         c = *s++;
   74     } while (isspace(c));
   75     if (c == '-') {
   76         neg = 1;
   77         c = *s++;
   78     } else if (c == '+')
   79         c = *s++;
   80     if ((base == 0 || base == 16) &&
   81         c == '0' && (*s == 'x' || *s == 'X')) {
   82         c = s[1];
   83         s += 2;
   84         base = 16;
   85     }
   86     if (base == 0)
   87         base = c == '0' ? 8 : 10;
   88     cutoff = (unsigned long)ULONG_MAX / (unsigned long)base;
   89     cutlim = (unsigned long)ULONG_MAX % (unsigned long)base;
   90     for (acc = 0, any = 0;; c = *s++) {
   91         if (!isascii(c))
   92             break;
   93         if (isdigit(c))
   94             c -= '0';
   95         else if (isalpha(c))
   96             c -= isupper(c) ? 'A' - 10 : 'a' - 10;
   97         else
   98             break;
   99         if (c >= base)
  100             break;
  101         if (any < 0 || acc > cutoff || (acc == cutoff && c > cutlim))
  102             any = -1;
  103         else {
  104             any = 1;
  105             acc *= base;
  106             acc += c;
  107         }
  108     }
  109     if (any < 0) {
  110         acc = ULONG_MAX;
  111         errno = ERANGE;
  112     } else if (neg)
  113         acc = -acc;
  114     if (endptr != 0)
  115         DE_CONST(any ? s - 1 : nptr, *endptr);
  116     return (acc);
  117 }