nsprpub/pr/src/misc/praton.c

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

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

mercurial