1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/nsprpub/pr/src/misc/praton.c Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,198 @@ 1.4 +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ 1.5 +/******************************************************************************* 1.6 + * The following function pr_inet_aton is based on the BSD function inet_aton 1.7 + * with some modifications. The license and copyright notices applying to this 1.8 + * function appear below. Modifications are also according to the license below. 1.9 + ******************************************************************************/ 1.10 + 1.11 +#include "prnetdb.h" 1.12 + 1.13 +/* 1.14 + * Copyright (c) 1983, 1990, 1993 1.15 + * The Regents of the University of California. All rights reserved. 1.16 + * 1.17 + * Redistribution and use in source and binary forms, with or without 1.18 + * modification, are permitted provided that the following conditions 1.19 + * are met: 1.20 + * 1. Redistributions of source code must retain the above copyright 1.21 + * notice, this list of conditions and the following disclaimer. 1.22 + * 2. Redistributions in binary form must reproduce the above copyright 1.23 + * notice, this list of conditions and the following disclaimer in the 1.24 + * documentation and/or other materials provided with the distribution. 1.25 + * 4. Neither the name of the University nor the names of its contributors 1.26 + * may be used to endorse or promote products derived from this software 1.27 + * without specific prior written permission. 1.28 + * 1.29 + * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 1.30 + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 1.31 + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 1.32 + * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 1.33 + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 1.34 + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 1.35 + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 1.36 + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 1.37 + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 1.38 + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 1.39 + * SUCH DAMAGE. 1.40 + */ 1.41 + 1.42 +/* 1.43 + * Portions Copyright (c) 1993 by Digital Equipment Corporation. 1.44 + * 1.45 + * Permission to use, copy, modify, and distribute this software for any 1.46 + * purpose with or without fee is hereby granted, provided that the above 1.47 + * copyright notice and this permission notice appear in all copies, and that 1.48 + * the name of Digital Equipment Corporation not be used in advertising or 1.49 + * publicity pertaining to distribution of the document or software without 1.50 + * specific, written prior permission. 1.51 + * 1.52 + * THE SOFTWARE IS PROVIDED "AS IS" AND DIGITAL EQUIPMENT CORP. DISCLAIMS ALL 1.53 + * WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES 1.54 + * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL DIGITAL EQUIPMENT 1.55 + * CORPORATION BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL 1.56 + * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR 1.57 + * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS 1.58 + * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS 1.59 + * SOFTWARE. 1.60 + */ 1.61 + 1.62 +/* 1.63 + * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC") 1.64 + * Portions Copyright (c) 1996-1999 by Internet Software Consortium. 1.65 + * 1.66 + * Permission to use, copy, modify, and distribute this software for any 1.67 + * purpose with or without fee is hereby granted, provided that the above 1.68 + * copyright notice and this permission notice appear in all copies. 1.69 + * 1.70 + * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES 1.71 + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 1.72 + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR 1.73 + * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 1.74 + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 1.75 + * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT 1.76 + * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 1.77 + */ 1.78 + 1.79 +#define XX 127 1.80 +static const unsigned char index_hex[256] = { 1.81 + XX,XX,XX,XX, XX,XX,XX,XX, XX,XX,XX,XX, XX,XX,XX,XX, 1.82 + XX,XX,XX,XX, XX,XX,XX,XX, XX,XX,XX,XX, XX,XX,XX,XX, 1.83 + XX,XX,XX,XX, XX,XX,XX,XX, XX,XX,XX,XX, XX,XX,XX,XX, 1.84 + 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,XX,XX, XX,XX,XX,XX, 1.85 + XX,10,11,12, 13,14,15,XX, XX,XX,XX,XX, XX,XX,XX,XX, 1.86 + XX,XX,XX,XX, XX,XX,XX,XX, XX,XX,XX,XX, XX,XX,XX,XX, 1.87 + XX,10,11,12, 13,14,15,XX, XX,XX,XX,XX, XX,XX,XX,XX, 1.88 + XX,XX,XX,XX, XX,XX,XX,XX, XX,XX,XX,XX, XX,XX,XX,XX, 1.89 + XX,XX,XX,XX, XX,XX,XX,XX, XX,XX,XX,XX, XX,XX,XX,XX, 1.90 + XX,XX,XX,XX, XX,XX,XX,XX, XX,XX,XX,XX, XX,XX,XX,XX, 1.91 + XX,XX,XX,XX, XX,XX,XX,XX, XX,XX,XX,XX, XX,XX,XX,XX, 1.92 + XX,XX,XX,XX, XX,XX,XX,XX, XX,XX,XX,XX, XX,XX,XX,XX, 1.93 + XX,XX,XX,XX, XX,XX,XX,XX, XX,XX,XX,XX, XX,XX,XX,XX, 1.94 + XX,XX,XX,XX, XX,XX,XX,XX, XX,XX,XX,XX, XX,XX,XX,XX, 1.95 + XX,XX,XX,XX, XX,XX,XX,XX, XX,XX,XX,XX, XX,XX,XX,XX, 1.96 + XX,XX,XX,XX, XX,XX,XX,XX, XX,XX,XX,XX, XX,XX,XX,XX, 1.97 +}; 1.98 + 1.99 +static PRBool _isdigit(char c) { return c >= '0' && c <= '9'; } 1.100 +static PRBool _isxdigit(char c) { return index_hex[(unsigned char) c] != XX; } 1.101 +static PRBool _isspace(char c) { return c == ' ' || (c >= '\t' && c <= '\r'); } 1.102 +#undef XX 1.103 + 1.104 +int 1.105 +pr_inet_aton(const char *cp, PRUint32 *addr) 1.106 +{ 1.107 + PRUint32 val; 1.108 + int base, n; 1.109 + char c; 1.110 + PRUint8 parts[4]; 1.111 + PRUint8 *pp = parts; 1.112 + int digit; 1.113 + 1.114 + c = *cp; 1.115 + for (;;) { 1.116 + /* 1.117 + * Collect number up to ``.''. 1.118 + * Values are specified as for C: 1.119 + * 0x=hex, 0=octal, isdigit=decimal. 1.120 + */ 1.121 + if (!_isdigit(c)) 1.122 + return (0); 1.123 + val = 0; base = 10; digit = 0; 1.124 + if (c == '0') { 1.125 + c = *++cp; 1.126 + if (c == 'x' || c == 'X') 1.127 + base = 16, c = *++cp; 1.128 + else { 1.129 + base = 8; 1.130 + digit = 1; 1.131 + } 1.132 + } 1.133 + for (;;) { 1.134 + if (_isdigit(c)) { 1.135 + if (base == 8 && (c == '8' || c == '9')) 1.136 + return (0); 1.137 + val = (val * base) + (c - '0'); 1.138 + c = *++cp; 1.139 + digit = 1; 1.140 + } else if (base == 16 && _isxdigit(c)) { 1.141 + val = (val << 4) + index_hex[(unsigned char) c]; 1.142 + c = *++cp; 1.143 + digit = 1; 1.144 + } else 1.145 + break; 1.146 + } 1.147 + if (c == '.') { 1.148 + /* 1.149 + * Internet format: 1.150 + * a.b.c.d 1.151 + * a.b.c (with c treated as 16 bits) 1.152 + * a.b (with b treated as 24 bits) 1.153 + */ 1.154 + if (pp >= parts + 3 || val > 0xffU) 1.155 + return (0); 1.156 + *pp++ = val; 1.157 + c = *++cp; 1.158 + } else 1.159 + break; 1.160 + } 1.161 + /* 1.162 + * Check for trailing characters. 1.163 + */ 1.164 + if (c != '\0' && !_isspace(c)) 1.165 + return (0); 1.166 + /* 1.167 + * Did we get a valid digit? 1.168 + */ 1.169 + if (!digit) 1.170 + return (0); 1.171 + /* 1.172 + * Concoct the address according to 1.173 + * the number of parts specified. 1.174 + */ 1.175 + n = pp - parts + 1; 1.176 + switch (n) { 1.177 + case 1: /*%< a -- 32 bits */ 1.178 + break; 1.179 + 1.180 + case 2: /*%< a.b -- 8.24 bits */ 1.181 + if (val > 0xffffffU) 1.182 + return (0); 1.183 + val |= parts[0] << 24; 1.184 + break; 1.185 + 1.186 + case 3: /*%< a.b.c -- 8.8.16 bits */ 1.187 + if (val > 0xffffU) 1.188 + return (0); 1.189 + val |= (parts[0] << 24) | (parts[1] << 16); 1.190 + break; 1.191 + 1.192 + case 4: /*%< a.b.c.d -- 8.8.8.8 bits */ 1.193 + if (val > 0xffU) 1.194 + return (0); 1.195 + val |= (parts[0] << 24) | (parts[1] << 16) | (parts[2] << 8); 1.196 + break; 1.197 + } 1.198 + *addr = PR_htonl(val); 1.199 + return (1); 1.200 +} 1.201 +