]> CyberLeo.Net >> Repos - FreeBSD/releng/9.3.git/blob - contrib/bind9/lib/lwres/strtoul.c
Copy stable/9 to releng/9.3 as part of the 9.3-RELEASE cycle.
[FreeBSD/releng/9.3.git] / contrib / bind9 / lib / lwres / strtoul.c
1 /*
2  * Copyright (C) 2004, 2005, 2007, 2014  Internet Systems Consortium, Inc. ("ISC")
3  * Copyright (C) 2003  Internet Software Consortium.
4  *
5  * Permission to use, copy, modify, and/or distribute this software for any
6  * purpose with or without fee is hereby granted, provided that the above
7  * copyright notice and this permission notice appear in all copies.
8  *
9  * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
10  * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
11  * AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
12  * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
13  * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
14  * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
15  * PERFORMANCE OF THIS SOFTWARE.
16  */
17
18 /*
19  * Copyright (c) 1990, 1993
20  *      The Regents of the University of California.  All rights reserved.
21  *
22  * Redistribution and use in source and binary forms, with or without
23  * modification, are permitted provided that the following conditions
24  * are met:
25  * 1. Redistributions of source code must retain the above copyright
26  *    notice, this list of conditions and the following disclaimer.
27  * 2. Redistributions in binary form must reproduce the above copyright
28  *    notice, this list of conditions and the following disclaimer in the
29  *    documentation and/or other materials provided with the distribution.
30  * 3. Neither the name of the University nor the names of its contributors
31  *    may be used to endorse or promote products derived from this software
32  *    without specific prior written permission.
33  *
34  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
35  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
36  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
37  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
38  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
39  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
40  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
41  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
42  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
43  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
44  * SUCH DAMAGE.
45  */
46
47 /*! \file */
48 #if defined(LIBC_SCCS) && !defined(lint)
49 static char sccsid[] = "@(#)strtoul.c   8.1 (Berkeley) 6/4/93";
50 #endif /* LIBC_SCCS and not lint */
51
52 /* $Id: strtoul.c,v 1.4 2007/06/19 23:47:22 tbox Exp $ */
53
54 #include <config.h>
55
56 #include <limits.h>
57 #include <ctype.h>
58 #include <errno.h>
59
60 #include <lwres/stdlib.h>
61
62 #define DE_CONST(konst, var) \
63         do { \
64                 union { const void *k; void *v; } _u; \
65                 _u.k = konst; \
66                 var = _u.v; \
67         } while (0)
68
69 /*!
70  * Convert a string to an unsigned long integer.
71  *
72  * Ignores `locale' stuff.  Assumes that the upper and lower case
73  * alphabets and digits are each contiguous.
74  */
75 unsigned long
76 lwres_strtoul(const char *nptr, char **endptr, int base) {
77         const char *s = nptr;
78         unsigned long acc;
79         unsigned char c;
80         unsigned long cutoff;
81         int neg = 0, any, cutlim;
82
83         /*
84          * See strtol for comments as to the logic used.
85          */
86         do {
87                 c = *s++;
88         } while (isspace(c));
89         if (c == '-') {
90                 neg = 1;
91                 c = *s++;
92         } else if (c == '+')
93                 c = *s++;
94         if ((base == 0 || base == 16) &&
95             c == '0' && (*s == 'x' || *s == 'X')) {
96                 c = s[1];
97                 s += 2;
98                 base = 16;
99         }
100         if (base == 0)
101                 base = c == '0' ? 8 : 10;
102         cutoff = (unsigned long)ULONG_MAX / (unsigned long)base;
103         cutlim = (unsigned long)ULONG_MAX % (unsigned long)base;
104         for (acc = 0, any = 0;; c = *s++) {
105                 if (!isascii(c))
106                         break;
107                 if (isdigit(c))
108                         c -= '0';
109                 else if (isalpha(c))
110                         c -= isupper(c) ? 'A' - 10 : 'a' - 10;
111                 else
112                         break;
113                 if (c >= base)
114                         break;
115                 if (any < 0 || acc > cutoff || (acc == cutoff && c > cutlim))
116                         any = -1;
117                 else {
118                         any = 1;
119                         acc *= base;
120                         acc += c;
121                 }
122         }
123         if (any < 0) {
124                 acc = ULONG_MAX;
125                 errno = ERANGE;
126         } else if (neg)
127                 acc = -acc;
128         if (endptr != 0)
129                 DE_CONST(any ? s - 1 : nptr, *endptr);
130         return (acc);
131 }