Merge pull request #3186 from FernetMenta/aefixes
[vuplus_xbmc] / xbmc / cores / DllLoader / exports / emu_socket / inet_aton.c
1 /*-
2  * Copyright (c) 2001 Charles Mott <cm@linktel.net>
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  */
26
27 #ifdef TARGET_WINDOWS
28 #include <windows.h>
29 typedef unsigned long in_addr_t;
30 #endif
31 #include <stdlib.h>
32 #include <ctype.h>
33 #include <limits.h>
34 #ifndef TARGET_WINDOWS
35 #include <netinet/in.h>
36 #endif
37
38 #ifdef __cplusplus
39 extern "C" {
40 #endif
41
42 #ifndef INADDR_NONE
43 #define INADDR_NONE 0xffffffff
44 #endif
45
46 int
47 inet_aton(const char *cp, struct in_addr *addr)
48 {
49     u_long parts[4];
50     in_addr_t val;
51     const char *c;
52     char *endptr;
53     int gotend, n;
54
55     c = (const char *)cp;
56     n = 0;
57
58     /*
59      * Run through the string, grabbing numbers until
60      * the end of the string, or some error
61      */
62     gotend = 0;
63     while (!gotend) {
64         unsigned long l;
65
66         l = strtoul(c, &endptr, 0);
67
68         if (l == ULONG_MAX || (l == 0 && endptr == c))
69             return (0);
70
71         val = (in_addr_t)l;
72
73         /*
74          * If the whole string is invalid, endptr will equal
75          * c.. this way we can make sure someone hasn't
76          * gone '.12' or something which would get past
77          * the next check.
78          */
79         if (endptr == c)
80             return (0);
81         parts[n] = val;
82         c = endptr;
83
84         /* Check the next character past the previous number's end */
85         switch (*c) {
86         case '.' :
87
88             /* Make sure we only do 3 dots .. */
89             if (n == 3) /* Whoops. Quit. */
90                 return (0);
91             n++;
92             c++;
93             break;
94
95         case '\0':
96             gotend = 1;
97             break;
98
99         default:
100             if (isspace((unsigned char)*c)) {
101                 gotend = 1;
102                 break;
103             } else {
104
105                 /* Invalid character, then fail. */
106                 return (0);
107             }
108         }
109
110     }
111
112     /* Concoct the address according to the number of parts specified. */
113     switch (n) {
114     case 0:             /* a -- 32 bits */
115
116         /*
117          * Nothing is necessary here.  Overflow checking was
118          * already done in strtoul().
119          */
120         break;
121     case 1:             /* a.b -- 8.24 bits */
122         if (val > 0xffffff || parts[0] > 0xff)
123             return (0);
124         val |= parts[0] << 24;
125         break;
126
127     case 2:             /* a.b.c -- 8.8.16 bits */
128         if (val > 0xffff || parts[0] > 0xff || parts[1] > 0xff)
129             return (0);
130         val |= (parts[0] << 24) | (parts[1] << 16);
131         break;
132
133     case 3:             /* a.b.c.d -- 8.8.8.8 bits */
134         if (val > 0xff || parts[0] > 0xff || parts[1] > 0xff ||
135             parts[2] > 0xff)
136             return (0);
137         val |= (parts[0] << 24) | (parts[1] << 16) | (parts[2] << 8);
138         break;
139     }
140
141     if (addr != NULL)
142         addr->s_addr = htonl(val);
143     return (1);
144 }
145
146 #ifdef __cplusplus
147 }
148 #endif