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 ******************************************************************************/
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
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
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
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.
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'); }
99 int pr_inet_aton(const char* cp
, PRUint32
* addr
) {
110 * Collect number up to ``.''.
111 * Values are specified as for C:
112 * 0x=hex, 0=octal, isdigit=decimal.
122 if (c
== 'x' || c
== 'X') {
123 base
= 16, c
= *++cp
;
131 if (base
== 8 && (c
== '8' || c
== '9')) {
134 val
= (val
* base
) + (c
- '0');
137 } else if (base
== 16 && _isxdigit(c
)) {
138 val
= (val
<< 4) + index_hex
[(unsigned char)c
];
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
) {
162 * Check for trailing characters.
164 if (c
!= '\0' && !_isspace(c
)) {
168 * Did we get a valid digit?
174 * Concoct the address according to
175 * the number of parts specified.
179 case 1: /*%< a -- 32 bits */
182 case 2: /*%< a.b -- 8.24 bits */
183 if (val
> 0xffffffU
) {
186 val
|= (unsigned int)parts
[0] << 24;
189 case 3: /*%< a.b.c -- 8.8.16 bits */
193 val
|= ((unsigned int)parts
[0] << 24) | ((unsigned int)parts
[1] << 16);
196 case 4: /*%< a.b.c.d -- 8.8.8.8 bits */
200 val
|= ((unsigned int)parts
[0] << 24) | ((unsigned int)parts
[1] << 16) |
201 ((unsigned int)parts
[2] << 8);
204 *addr
= PR_htonl(val
);