Coverage Report

Created: 2025-07-01 06:25

/src/nspr/pr/src/misc/praton.c
Line
Count
Source (jump to first uncovered line)
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
 ******************************************************************************/
7
8
#include "prnetdb.h"
9
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
 */
38
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
 */
58
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
 */
75
76
0
#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,
92
};
93
94
0
static PRBool _isdigit(char c) { return c >= '0' && c <= '9'; }
95
0
static PRBool _isxdigit(char c) { return index_hex[(unsigned char)c] != XX; }
96
0
static PRBool _isspace(char c) { return c == ' ' || (c >= '\t' && c <= '\r'); }
97
#undef XX
98
99
0
int pr_inet_aton(const char* cp, PRUint32* addr) {
100
0
  PRUint32 val;
101
0
  int base, n;
102
0
  char c;
103
0
  PRUint8 parts[4];
104
0
  PRUint8* pp = parts;
105
0
  int digit;
106
107
0
  c = *cp;
108
0
  for (;;) {
109
    /*
110
     * Collect number up to ``.''.
111
     * Values are specified as for C:
112
     * 0x=hex, 0=octal, isdigit=decimal.
113
     */
114
0
    if (!_isdigit(c)) {
115
0
      return (0);
116
0
    }
117
0
    val = 0;
118
0
    base = 10;
119
0
    digit = 0;
120
0
    if (c == '0') {
121
0
      c = *++cp;
122
0
      if (c == 'x' || c == 'X') {
123
0
        base = 16, c = *++cp;
124
0
      } else {
125
0
        base = 8;
126
0
        digit = 1;
127
0
      }
128
0
    }
129
0
    for (;;) {
130
0
      if (_isdigit(c)) {
131
0
        if (base == 8 && (c == '8' || c == '9')) {
132
0
          return (0);
133
0
        }
134
0
        val = (val * base) + (c - '0');
135
0
        c = *++cp;
136
0
        digit = 1;
137
0
      } else if (base == 16 && _isxdigit(c)) {
138
0
        val = (val << 4) + index_hex[(unsigned char)c];
139
0
        c = *++cp;
140
0
        digit = 1;
141
0
      } else {
142
0
        break;
143
0
      }
144
0
    }
145
0
    if (c == '.') {
146
      /*
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)
151
       */
152
0
      if (pp >= parts + 3 || val > 0xffU) {
153
0
        return (0);
154
0
      }
155
0
      *pp++ = val;
156
0
      c = *++cp;
157
0
    } else {
158
0
      break;
159
0
    }
160
0
  }
161
  /*
162
   * Check for trailing characters.
163
   */
164
0
  if (c != '\0' && !_isspace(c)) {
165
0
    return (0);
166
0
  }
167
  /*
168
   * Did we get a valid digit?
169
   */
170
0
  if (!digit) {
171
0
    return (0);
172
0
  }
173
  /*
174
   * Concoct the address according to
175
   * the number of parts specified.
176
   */
177
0
  n = pp - parts + 1;
178
0
  switch (n) {
179
0
    case 1: /*%< a -- 32 bits */
180
0
      break;
181
182
0
    case 2: /*%< a.b -- 8.24 bits */
183
0
      if (val > 0xffffffU) {
184
0
        return (0);
185
0
      }
186
0
      val |= (unsigned int)parts[0] << 24;
187
0
      break;
188
189
0
    case 3: /*%< a.b.c -- 8.8.16 bits */
190
0
      if (val > 0xffffU) {
191
0
        return (0);
192
0
      }
193
0
      val |= ((unsigned int)parts[0] << 24) | ((unsigned int)parts[1] << 16);
194
0
      break;
195
196
0
    case 4: /*%< a.b.c.d -- 8.8.8.8 bits */
197
0
      if (val > 0xffU) {
198
0
        return (0);
199
0
      }
200
0
      val |= ((unsigned int)parts[0] << 24) | ((unsigned int)parts[1] << 16) |
201
0
             ((unsigned int)parts[2] << 8);
202
0
      break;
203
0
  }
204
0
  *addr = PR_htonl(val);
205
0
  return (1);
206
0
}