Backed out changeset b71c8c052463 (bug 1943846) for causing mass failures. CLOSED...
[gecko.git] / nsprpub / pr / src / misc / praton.c
blobe1abcd73a7c5bcf107518f62536b4d251b1b714e
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /*******************************************************************************
3 * The following function pr_inet_aton is based on the BSD function inet_aton
4 * with some modifications. The license and copyright notices applying to this
5 * function appear below. Modifications are also according to the license below.
6 ******************************************************************************/
8 #include "prnetdb.h"
11 * Copyright (c) 1983, 1990, 1993
12 * The Regents of the University of California. All rights reserved.
14 * Redistribution and use in source and binary forms, with or without
15 * modification, are permitted provided that the following conditions
16 * are met:
17 * 1. Redistributions of source code must retain the above copyright
18 * notice, this list of conditions and the following disclaimer.
19 * 2. Redistributions in binary form must reproduce the above copyright
20 * notice, this list of conditions and the following disclaimer in the
21 * documentation and/or other materials provided with the distribution.
22 * 4. Neither the name of the University nor the names of its contributors
23 * may be used to endorse or promote products derived from this software
24 * without specific prior written permission.
26 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
27 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
30 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 * SUCH DAMAGE.
40 * Portions Copyright (c) 1993 by Digital Equipment Corporation.
42 * Permission to use, copy, modify, and distribute this software for any
43 * purpose with or without fee is hereby granted, provided that the above
44 * copyright notice and this permission notice appear in all copies, and that
45 * the name of Digital Equipment Corporation not be used in advertising or
46 * publicity pertaining to distribution of the document or software without
47 * specific, written prior permission.
49 * THE SOFTWARE IS PROVIDED "AS IS" AND DIGITAL EQUIPMENT CORP. DISCLAIMS ALL
50 * WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES
51 * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL DIGITAL EQUIPMENT
52 * CORPORATION BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
53 * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
54 * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
55 * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
56 * SOFTWARE.
60 * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC")
61 * Portions Copyright (c) 1996-1999 by Internet Software Consortium.
63 * Permission to use, copy, modify, and distribute this software for any
64 * purpose with or without fee is hereby granted, provided that the above
65 * copyright notice and this permission notice appear in all copies.
67 * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES
68 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
69 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR
70 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
71 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
72 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
73 * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
76 #define XX 127
77 static const unsigned char index_hex[256] = {
78 XX, XX, XX, XX, XX, XX, XX, XX, XX, XX, XX, XX, XX, XX, XX, XX, XX, XX, XX,
79 XX, XX, XX, XX, XX, XX, XX, XX, XX, XX, XX, XX, XX, XX, XX, XX, XX, XX, XX,
80 XX, XX, XX, XX, XX, XX, XX, XX, XX, XX, 0, 1, 2, 3, 4, 5, 6, 7, 8,
81 9, XX, XX, XX, XX, XX, XX, XX, 10, 11, 12, 13, 14, 15, XX, XX, XX, XX, XX,
82 XX, XX, XX, XX, XX, XX, XX, XX, XX, XX, XX, XX, XX, XX, XX, XX, XX, XX, XX,
83 XX, XX, 10, 11, 12, 13, 14, 15, XX, XX, XX, XX, XX, XX, XX, XX, XX, XX, XX,
84 XX, XX, XX, XX, XX, XX, XX, XX, XX, XX, XX, XX, XX, XX, XX, XX, XX, XX, XX,
85 XX, XX, XX, XX, XX, XX, XX, XX, XX, XX, XX, XX, XX, XX, XX, XX, XX, XX, XX,
86 XX, XX, XX, XX, XX, XX, XX, XX, XX, XX, XX, XX, XX, XX, XX, XX, XX, XX, XX,
87 XX, XX, XX, XX, XX, XX, XX, XX, XX, XX, XX, XX, XX, XX, XX, XX, XX, XX, XX,
88 XX, XX, XX, XX, XX, XX, XX, XX, XX, XX, XX, XX, XX, XX, XX, XX, XX, XX, XX,
89 XX, XX, XX, XX, XX, XX, XX, XX, XX, XX, XX, XX, XX, XX, XX, XX, XX, XX, XX,
90 XX, XX, XX, XX, XX, XX, XX, XX, XX, XX, XX, XX, XX, XX, XX, XX, XX, XX, XX,
91 XX, XX, XX, XX, XX, XX, XX, XX, XX,
94 static PRBool _isdigit(char c) { return c >= '0' && c <= '9'; }
95 static PRBool _isxdigit(char c) { return index_hex[(unsigned char)c] != XX; }
96 static PRBool _isspace(char c) { return c == ' ' || (c >= '\t' && c <= '\r'); }
97 #undef XX
99 int pr_inet_aton(const char* cp, PRUint32* addr) {
100 PRUint32 val;
101 int base, n;
102 char c;
103 PRUint8 parts[4];
104 PRUint8* pp = parts;
105 int digit;
107 c = *cp;
108 for (;;) {
110 * Collect number up to ``.''.
111 * Values are specified as for C:
112 * 0x=hex, 0=octal, isdigit=decimal.
114 if (!_isdigit(c)) {
115 return (0);
117 val = 0;
118 base = 10;
119 digit = 0;
120 if (c == '0') {
121 c = *++cp;
122 if (c == 'x' || c == 'X') {
123 base = 16, c = *++cp;
124 } else {
125 base = 8;
126 digit = 1;
129 for (;;) {
130 if (_isdigit(c)) {
131 if (base == 8 && (c == '8' || c == '9')) {
132 return (0);
134 val = (val * base) + (c - '0');
135 c = *++cp;
136 digit = 1;
137 } else if (base == 16 && _isxdigit(c)) {
138 val = (val << 4) + index_hex[(unsigned char)c];
139 c = *++cp;
140 digit = 1;
141 } else {
142 break;
145 if (c == '.') {
147 * Internet format:
148 * a.b.c.d
149 * a.b.c (with c treated as 16 bits)
150 * a.b (with b treated as 24 bits)
152 if (pp >= parts + 3 || val > 0xffU) {
153 return (0);
155 *pp++ = val;
156 c = *++cp;
157 } else {
158 break;
162 * Check for trailing characters.
164 if (c != '\0' && !_isspace(c)) {
165 return (0);
168 * Did we get a valid digit?
170 if (!digit) {
171 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);
186 val |= (unsigned int)parts[0] << 24;
187 break;
189 case 3: /*%< a.b.c -- 8.8.16 bits */
190 if (val > 0xffffU) {
191 return (0);
193 val |= ((unsigned int)parts[0] << 24) | ((unsigned int)parts[1] << 16);
194 break;
196 case 4: /*%< a.b.c.d -- 8.8.8.8 bits */
197 if (val > 0xffU) {
198 return (0);
200 val |= ((unsigned int)parts[0] << 24) | ((unsigned int)parts[1] << 16) |
201 ((unsigned int)parts[2] << 8);
202 break;
204 *addr = PR_htonl(val);
205 return (1);