Remove building with NOCRYPTO option
[minix.git] / external / bsd / bind / dist / lib / lwres / lwinetaton.c
blobe145541f83c33c7f1127eb8c88c3c3cfa578367d
1 /* $NetBSD: lwinetaton.c,v 1.7 2014/12/10 04:38:02 christos Exp $ */
3 /*
4 * Portions Copyright (C) 2004, 2005, 2007, 2012-2014 Internet Systems Consortium, Inc. ("ISC")
5 * Portions Copyright (C) 1996-2001, 2003 Internet Software Consortium.
7 * Permission to use, copy, modify, and/or distribute this software for any
8 * purpose with or without fee is hereby granted, provided that the above
9 * copyright notice and this permission notice appear in all copies.
11 * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
12 * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
13 * AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
14 * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
15 * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
16 * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
17 * PERFORMANCE OF THIS SOFTWARE.
21 * Copyright (c) 1983, 1990, 1993
22 * The Regents of the University of California. All rights reserved.
24 * Redistribution and use in source and binary forms, with or without
25 * modification, are permitted provided that the following conditions
26 * are met:
27 * 1. Redistributions of source code must retain the above copyright
28 * notice, this list of conditions and the following disclaimer.
29 * 2. Redistributions in binary form must reproduce the above copyright
30 * notice, this list of conditions and the following disclaimer in the
31 * documentation and/or other materials provided with the distribution.
32 * 3. Neither the name of the University nor the names of its contributors
33 * may be used to endorse or promote products derived from this software
34 * without specific prior written permission.
36 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
37 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
38 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
39 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
40 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
41 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
42 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
43 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
44 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
45 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
46 * SUCH DAMAGE.
50 * Portions Copyright (c) 1993 by Digital Equipment Corporation.
52 * Permission to use, copy, modify, and distribute this software for any
53 * purpose with or without fee is hereby granted, provided that the above
54 * copyright notice and this permission notice appear in all copies, and that
55 * the name of Digital Equipment Corporation not be used in advertising or
56 * publicity pertaining to distribution of the document or software without
57 * specific, written prior permission.
59 * THE SOFTWARE IS PROVIDED "AS IS" AND DIGITAL EQUIPMENT CORP. DISCLAIMS ALL
60 * WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES
61 * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL DIGITAL EQUIPMENT
62 * CORPORATION BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
63 * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
64 * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
65 * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
66 * SOFTWARE.
69 /*! \file lwinetaton.c
71 #if defined(LIBC_SCCS) && !defined(lint)
72 static char sccsid[] = "@(#)inet_addr.c 8.1 (Berkeley) 6/17/93";
73 static char rcsid[] = "Id: lwinetaton.c,v 1.16 2007/06/19 23:47:22 tbox Exp ";
74 #endif /* LIBC_SCCS and not lint */
76 #include <config.h>
78 #include <ctype.h>
80 #include <stddef.h>
82 #include <lwres/int.h>
83 #include <lwres/net.h>
85 #include "assert_p.h"
87 /*!
88 * Check whether "cp" is a valid ascii representation
89 * of an Internet address and convert to a binary address.
90 * Returns 1 if the address is valid, 0 if not.
91 * This replaces inet_addr, the return value from which
92 * cannot distinguish between failure and a local broadcast address.
94 int
95 lwres_net_aton(const char *cp, struct in_addr *addr) {
96 lwres_uint32_t val;
97 int base;
98 ptrdiff_t n;
99 unsigned char c;
100 lwres_uint8_t parts[4];
101 lwres_uint8_t *pp = parts;
102 int digit;
104 REQUIRE(cp != NULL);
106 c = *cp;
107 for (;;) {
109 * Collect number up to ``.''.
110 * Values are specified as for C:
111 * 0x=hex, 0=octal, isdigit=decimal.
113 if (!isdigit(c & 0xff))
114 return (0);
115 val = 0;
116 base = 10;
117 digit = 0;
118 if (c == '0') {
119 c = *++cp;
120 if (c == 'x' || c == 'X') {
121 base = 16;
122 c = *++cp;
123 } else {
124 base = 8;
125 digit = 1;
128 for (;;) {
130 * isascii() is valid for all integer values, and
131 * when it is true, c is known to be in scope
132 * for isdigit(). No cast necessary. Similar
133 * comment applies for later ctype uses.
135 if (isascii(c) && isdigit(c)) {
136 if (base == 8 && (c == '8' || c == '9'))
137 return (0);
138 val = (val * base) + (c - '0');
139 c = *++cp;
140 digit = 1;
141 } else if (base == 16 && isascii(c) && isxdigit(c)) {
142 val = (val << 4) |
143 (c + 10 - (islower(c) ? 'a' : 'A'));
144 c = *++cp;
145 digit = 1;
146 } else
147 break;
149 if (c == '.') {
151 * Internet format:
152 * a.b.c.d
153 * a.b.c (with c treated as 16 bits)
154 * a.b (with b treated as 24 bits)
156 if (pp >= parts + 3 || val > 0xffU)
157 return (0);
158 *pp++ = (lwres_uint8_t)val;
159 c = *++cp;
160 } else
161 break;
164 * Check for trailing characters.
166 if (c != '\0' && (!isascii(c) || !isspace(c)))
167 return (0);
169 * Did we get a valid digit?
171 if (!digit)
172 return (0);
174 * Concoct the address according to
175 * the number of parts specified.
177 n = pp - parts + 1;
178 switch (n) {
179 case 1: /* a -- 32 bits */
180 break;
182 case 2: /* a.b -- 8.24 bits */
183 if (val > 0xffffffU)
184 return (0);
185 val |= parts[0] << 24;
186 break;
188 case 3: /* a.b.c -- 8.8.16 bits */
189 if (val > 0xffffU)
190 return (0);
191 val |= (parts[0] << 24) | (parts[1] << 16);
192 break;
194 case 4: /* a.b.c.d -- 8.8.8.8 bits */
195 if (val > 0xffU)
196 return (0);
197 val |= (parts[0] << 24) | (parts[1] << 16) | (parts[2] << 8);
198 break;
200 if (addr != NULL)
201 addr->s_addr = htonl(val);
203 return (1);