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.

     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"
    10 /*
    11  * Copyright (c) 1983, 1990, 1993
    12  *    The Regents of the University of California.  All rights reserved.
    13  * 
    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.
    25  * 
    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.
    37  */
    39 /*
    40  * Portions Copyright (c) 1993 by Digital Equipment Corporation.
    41  * 
    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.
    48  * 
    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.
    57  */
    59 /*
    60  * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC")
    61  * Portions Copyright (c) 1996-1999 by Internet Software Consortium.
    62  *
    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.
    66  *
    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.
    74  */
    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,
    79     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,XX,XX, XX,XX,XX,XX,
    81      0, 1, 2, 3,  4, 5, 6, 7,  8, 9,XX,XX, XX,XX,XX,XX,
    82     XX,10,11,12, 13,14,15,XX, XX,XX,XX,XX, XX,XX,XX,XX,
    83     XX,XX,XX,XX, XX,XX,XX,XX, XX,XX,XX,XX, XX,XX,XX,XX,
    84     XX,10,11,12, 13,14,15,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,
    86     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,
    88     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,
    90     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,XX,XX,XX, XX,XX,XX,XX,
    92     XX,XX,XX,XX, XX,XX,XX,XX, XX,XX,XX,XX, XX,XX,XX,XX,
    93     XX,XX,XX,XX, XX,XX,XX,XX, XX,XX,XX,XX, XX,XX,XX,XX,
    94 };
    96 static PRBool _isdigit(char c) { return c >= '0' && c <= '9'; }
    97 static PRBool _isxdigit(char c) { return index_hex[(unsigned char) c] != XX; }
    98 static PRBool _isspace(char c) { return c == ' ' || (c >= '\t' && c <= '\r'); }
    99 #undef XX
   101 int
   102 pr_inet_aton(const char *cp, PRUint32 *addr)
   103 {
   104     PRUint32 val;
   105     int base, n;
   106     char c;
   107     PRUint8 parts[4];
   108     PRUint8 *pp = parts;
   109     int digit;
   111     c = *cp;
   112     for (;;) {
   113         /*
   114          * Collect number up to ``.''.
   115          * Values are specified as for C:
   116          * 0x=hex, 0=octal, isdigit=decimal.
   117          */
   118         if (!_isdigit(c))
   119             return (0);
   120         val = 0; base = 10; digit = 0;
   121         if (c == '0') {
   122             c = *++cp;
   123             if (c == 'x' || c == 'X')
   124                 base = 16, c = *++cp;
   125             else {
   126                 base = 8;
   127                 digit = 1;
   128             }
   129         }
   130         for (;;) {
   131             if (_isdigit(c)) {
   132                 if (base == 8 && (c == '8' || c == '9'))
   133                     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;
   143         }
   144         if (c == '.') {
   145             /*
   146              * Internet format:
   147              *    a.b.c.d
   148              *    a.b.c    (with c treated as 16 bits)
   149              *    a.b    (with b treated as 24 bits)
   150              */
   151             if (pp >= parts + 3 || val > 0xffU)
   152                 return (0);
   153             *pp++ = val;
   154             c = *++cp;
   155         } else
   156             break;
   157     }
   158     /*
   159      * Check for trailing characters.
   160      */
   161     if (c != '\0' && !_isspace(c))
   162         return (0);
   163     /*
   164      * Did we get a valid digit?
   165      */
   166     if (!digit)
   167         return (0);
   168     /*
   169      * Concoct the address according to
   170      * the number of parts specified.
   171      */
   172     n = pp - parts + 1;
   173     switch (n) {
   174     case 1:                /*%< a -- 32 bits */
   175         break;
   177     case 2:                /*%< a.b -- 8.24 bits */
   178         if (val > 0xffffffU)
   179             return (0);
   180         val |= parts[0] << 24;
   181         break;
   183     case 3:                /*%< a.b.c -- 8.8.16 bits */
   184         if (val > 0xffffU)
   185             return (0);
   186         val |= (parts[0] << 24) | (parts[1] << 16);
   187         break;
   189     case 4:                /*%< a.b.c.d -- 8.8.8.8 bits */
   190         if (val > 0xffU)
   191             return (0);
   192         val |= (parts[0] << 24) | (parts[1] << 16) | (parts[2] << 8);
   193         break;
   194     }
   195     *addr = PR_htonl(val);
   196     return (1);
   197 }

mercurial