strtoul.c

Go to the documentation of this file.
00001 /*
00002  * Copyright (C) 2004, 2005, 2007, 2014  Internet Systems Consortium, Inc. ("ISC")
00003  * Copyright (C) 2003  Internet Software Consortium.
00004  *
00005  * Permission to use, copy, modify, and/or distribute this software for any
00006  * purpose with or without fee is hereby granted, provided that the above
00007  * copyright notice and this permission notice appear in all copies.
00008  *
00009  * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
00010  * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
00011  * AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
00012  * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
00013  * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
00014  * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
00015  * PERFORMANCE OF THIS SOFTWARE.
00016  */
00017 
00018 /*
00019  * Copyright (c) 1990, 1993
00020  *      The Regents of the University of California.  All rights reserved.
00021  *
00022  * Redistribution and use in source and binary forms, with or without
00023  * modification, are permitted provided that the following conditions
00024  * are met:
00025  * 1. Redistributions of source code must retain the above copyright
00026  *    notice, this list of conditions and the following disclaimer.
00027  * 2. Redistributions in binary form must reproduce the above copyright
00028  *    notice, this list of conditions and the following disclaimer in the
00029  *    documentation and/or other materials provided with the distribution.
00030  * 3. Neither the name of the University nor the names of its contributors
00031  *    may be used to endorse or promote products derived from this software
00032  *    without specific prior written permission.
00033  *
00034  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
00035  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00036  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00037  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
00038  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
00039  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
00040  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
00041  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00042  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
00043  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
00044  * SUCH DAMAGE.
00045  */
00046 
00047 /*! \file */
00048 #if defined(LIBC_SCCS) && !defined(lint)
00049 static char sccsid[] = "@(#)strtoul.c   8.1 (Berkeley) 6/4/93";
00050 #endif /* LIBC_SCCS and not lint */
00051 
00052 /* $Id: strtoul.c,v 1.7 2007/06/19 23:47:17 tbox Exp $ */
00053 
00054 #include <config.h>
00055 
00056 #include <limits.h>
00057 #include <ctype.h>
00058 #include <errno.h>
00059 
00060 #include <isc/stdlib.h>
00061 #include <isc/util.h>
00062 
00063 /*!
00064  * Convert a string to an unsigned long integer.
00065  *
00066  * Ignores `locale' stuff.  Assumes that the upper and lower case
00067  * alphabets and digits are each contiguous.
00068  */
00069 unsigned long
00070 isc_strtoul(const char *nptr, char **endptr, int base) {
00071         const char *s = nptr;
00072         unsigned long acc;
00073         unsigned char c;
00074         unsigned long cutoff;
00075         int neg = 0, any, cutlim;
00076 
00077         /*
00078          * See strtol for comments as to the logic used.
00079          */
00080         do {
00081                 c = *s++;
00082         } while (isspace(c));
00083         if (c == '-') {
00084                 neg = 1;
00085                 c = *s++;
00086         } else if (c == '+')
00087                 c = *s++;
00088         if ((base == 0 || base == 16) &&
00089             c == '0' && (*s == 'x' || *s == 'X')) {
00090                 c = s[1];
00091                 s += 2;
00092                 base = 16;
00093         }
00094         if (base == 0)
00095                 base = c == '0' ? 8 : 10;
00096         cutoff = (unsigned long)ULONG_MAX / (unsigned long)base;
00097         cutlim = (unsigned long)ULONG_MAX % (unsigned long)base;
00098         for (acc = 0, any = 0;; c = *s++) {
00099                 if (!isascii(c))
00100                         break;
00101                 if (isdigit(c))
00102                         c -= '0';
00103                 else if (isalpha(c))
00104                         c -= isupper(c) ? 'A' - 10 : 'a' - 10;
00105                 else
00106                         break;
00107                 if (c >= base)
00108                         break;
00109                 if (any < 0 || acc > cutoff || (acc == cutoff && c > cutlim))
00110                         any = -1;
00111                 else {
00112                         any = 1;
00113                         acc *= base;
00114                         acc += c;
00115                 }
00116         }
00117         if (any < 0) {
00118                 acc = ULONG_MAX;
00119                 errno = ERANGE;
00120         } else if (neg)
00121                 acc = -acc;
00122         if (endptr != 0)
00123                 DE_CONST(any ? s - 1 : nptr, *endptr);
00124         return (acc);
00125 }

Generated on Tue Apr 28 17:41:05 2015 by Doxygen 1.5.4 for BIND9 Internals 9.11.0pre-alpha